2 * Copyright (c) 2007-2008 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * $DragonFly: src/sys/vfs/hammer/hammer_vnops.c,v 1.102 2008/10/16 17:24:16 dillon Exp $
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/fcntl.h>
41 #include <sys/namecache.h>
42 #include <sys/vnode.h>
43 #include <sys/lockf.h>
44 #include <sys/event.h>
46 #include <sys/dirent.h>
48 #include <vm/vm_extern.h>
49 #include <vfs/fifofs/fifo.h>
55 /*static int hammer_vop_vnoperate(struct vop_generic_args *);*/
56 static int hammer_vop_fsync(struct vop_fsync_args *);
57 static int hammer_vop_read(struct vop_read_args *);
58 static int hammer_vop_write(struct vop_write_args *);
59 static int hammer_vop_access(struct vop_access_args *);
60 static int hammer_vop_advlock(struct vop_advlock_args *);
61 static int hammer_vop_close(struct vop_close_args *);
62 static int hammer_vop_ncreate(struct vop_ncreate_args *);
63 static int hammer_vop_getattr(struct vop_getattr_args *);
64 static int hammer_vop_nresolve(struct vop_nresolve_args *);
65 static int hammer_vop_nlookupdotdot(struct vop_nlookupdotdot_args *);
66 static int hammer_vop_nlink(struct vop_nlink_args *);
67 static int hammer_vop_nmkdir(struct vop_nmkdir_args *);
68 static int hammer_vop_nmknod(struct vop_nmknod_args *);
69 static int hammer_vop_open(struct vop_open_args *);
70 static int hammer_vop_print(struct vop_print_args *);
71 static int hammer_vop_readdir(struct vop_readdir_args *);
72 static int hammer_vop_readlink(struct vop_readlink_args *);
73 static int hammer_vop_nremove(struct vop_nremove_args *);
74 static int hammer_vop_nrename(struct vop_nrename_args *);
75 static int hammer_vop_nrmdir(struct vop_nrmdir_args *);
76 static int hammer_vop_markatime(struct vop_markatime_args *);
77 static int hammer_vop_setattr(struct vop_setattr_args *);
78 static int hammer_vop_strategy(struct vop_strategy_args *);
79 static int hammer_vop_bmap(struct vop_bmap_args *ap);
80 static int hammer_vop_nsymlink(struct vop_nsymlink_args *);
81 static int hammer_vop_nwhiteout(struct vop_nwhiteout_args *);
82 static int hammer_vop_ioctl(struct vop_ioctl_args *);
83 static int hammer_vop_mountctl(struct vop_mountctl_args *);
84 static int hammer_vop_kqfilter (struct vop_kqfilter_args *);
86 static int hammer_vop_fifoclose (struct vop_close_args *);
87 static int hammer_vop_fiforead (struct vop_read_args *);
88 static int hammer_vop_fifowrite (struct vop_write_args *);
89 static int hammer_vop_fifokqfilter (struct vop_kqfilter_args *);
91 static int hammer_vop_specclose (struct vop_close_args *);
92 static int hammer_vop_specread (struct vop_read_args *);
93 static int hammer_vop_specwrite (struct vop_write_args *);
95 struct vop_ops hammer_vnode_vops = {
96 .vop_default = vop_defaultop,
97 .vop_fsync = hammer_vop_fsync,
98 .vop_getpages = vop_stdgetpages,
99 .vop_putpages = vop_stdputpages,
100 .vop_read = hammer_vop_read,
101 .vop_write = hammer_vop_write,
102 .vop_access = hammer_vop_access,
103 .vop_advlock = hammer_vop_advlock,
104 .vop_close = hammer_vop_close,
105 .vop_ncreate = hammer_vop_ncreate,
106 .vop_getattr = hammer_vop_getattr,
107 .vop_inactive = hammer_vop_inactive,
108 .vop_reclaim = hammer_vop_reclaim,
109 .vop_nresolve = hammer_vop_nresolve,
110 .vop_nlookupdotdot = hammer_vop_nlookupdotdot,
111 .vop_nlink = hammer_vop_nlink,
112 .vop_nmkdir = hammer_vop_nmkdir,
113 .vop_nmknod = hammer_vop_nmknod,
114 .vop_open = hammer_vop_open,
115 .vop_pathconf = vop_stdpathconf,
116 .vop_print = hammer_vop_print,
117 .vop_readdir = hammer_vop_readdir,
118 .vop_readlink = hammer_vop_readlink,
119 .vop_nremove = hammer_vop_nremove,
120 .vop_nrename = hammer_vop_nrename,
121 .vop_nrmdir = hammer_vop_nrmdir,
122 .vop_markatime = hammer_vop_markatime,
123 .vop_setattr = hammer_vop_setattr,
124 .vop_bmap = hammer_vop_bmap,
125 .vop_strategy = hammer_vop_strategy,
126 .vop_nsymlink = hammer_vop_nsymlink,
127 .vop_nwhiteout = hammer_vop_nwhiteout,
128 .vop_ioctl = hammer_vop_ioctl,
129 .vop_mountctl = hammer_vop_mountctl,
130 .vop_kqfilter = hammer_vop_kqfilter
133 struct vop_ops hammer_spec_vops = {
134 .vop_default = spec_vnoperate,
135 .vop_fsync = hammer_vop_fsync,
136 .vop_read = hammer_vop_specread,
137 .vop_write = hammer_vop_specwrite,
138 .vop_access = hammer_vop_access,
139 .vop_close = hammer_vop_specclose,
140 .vop_markatime = hammer_vop_markatime,
141 .vop_getattr = hammer_vop_getattr,
142 .vop_inactive = hammer_vop_inactive,
143 .vop_reclaim = hammer_vop_reclaim,
144 .vop_setattr = hammer_vop_setattr
147 struct vop_ops hammer_fifo_vops = {
148 .vop_default = fifo_vnoperate,
149 .vop_fsync = hammer_vop_fsync,
150 .vop_read = hammer_vop_fiforead,
151 .vop_write = hammer_vop_fifowrite,
152 .vop_access = hammer_vop_access,
153 .vop_close = hammer_vop_fifoclose,
154 .vop_markatime = hammer_vop_markatime,
155 .vop_getattr = hammer_vop_getattr,
156 .vop_inactive = hammer_vop_inactive,
157 .vop_reclaim = hammer_vop_reclaim,
158 .vop_setattr = hammer_vop_setattr,
159 .vop_kqfilter = hammer_vop_fifokqfilter
164 hammer_knote(struct vnode *vp, int flags)
167 KNOTE(&vp->v_pollinfo.vpi_selinfo.si_note, flags);
170 #ifdef DEBUG_TRUNCATE
171 struct hammer_inode *HammerTruncIp;
174 static int hammer_dounlink(hammer_transaction_t trans, struct nchandle *nch,
175 struct vnode *dvp, struct ucred *cred,
176 int flags, int isdir);
177 static int hammer_vop_strategy_read(struct vop_strategy_args *ap);
178 static int hammer_vop_strategy_write(struct vop_strategy_args *ap);
183 hammer_vop_vnoperate(struct vop_generic_args *)
185 return (VOCALL(&hammer_vnode_vops, ap));
190 * hammer_vop_fsync { vp, waitfor }
192 * fsync() an inode to disk and wait for it to be completely committed
193 * such that the information would not be undone if a crash occured after
198 hammer_vop_fsync(struct vop_fsync_args *ap)
200 hammer_inode_t ip = VTOI(ap->a_vp);
202 ++hammer_count_fsyncs;
203 vfsync(ap->a_vp, ap->a_waitfor, 1, NULL, NULL);
204 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
205 if (ap->a_waitfor == MNT_WAIT) {
207 hammer_wait_inode(ip);
208 vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY);
214 * hammer_vop_read { vp, uio, ioflag, cred }
218 hammer_vop_read(struct vop_read_args *ap)
220 struct hammer_transaction trans;
231 if (ap->a_vp->v_type != VREG)
238 * Allow the UIO's size to override the sequential heuristic.
240 blksize = hammer_blocksize(uio->uio_offset);
241 seqcount = (uio->uio_resid + (blksize - 1)) / blksize;
242 ioseqcount = ap->a_ioflag >> 16;
243 if (seqcount < ioseqcount)
244 seqcount = ioseqcount;
246 hammer_start_transaction(&trans, ip->hmp);
249 * Access the data typically in HAMMER_BUFSIZE blocks via the
250 * buffer cache, but HAMMER may use a variable block size based
253 while (uio->uio_resid > 0 && uio->uio_offset < ip->ino_data.size) {
257 blksize = hammer_blocksize(uio->uio_offset);
258 offset = (int)uio->uio_offset & (blksize - 1);
259 base_offset = uio->uio_offset - offset;
261 if (hammer_cluster_enable) {
263 * Use file_limit to prevent cluster_read() from
264 * creating buffers of the wrong block size past
267 file_limit = ip->ino_data.size;
268 if (base_offset < HAMMER_XDEMARC &&
269 file_limit > HAMMER_XDEMARC) {
270 file_limit = HAMMER_XDEMARC;
272 error = cluster_read(ap->a_vp,
273 file_limit, base_offset,
277 error = bread(ap->a_vp, base_offset, blksize, &bp);
280 kprintf("error %d\n", error);
285 /* bp->b_flags |= B_CLUSTEROK; temporarily disabled */
286 n = blksize - offset;
287 if (n > uio->uio_resid)
289 if (n > ip->ino_data.size - uio->uio_offset)
290 n = (int)(ip->ino_data.size - uio->uio_offset);
291 error = uiomove((char *)bp->b_data + offset, n, uio);
293 /* data has a lower priority then meta-data */
294 bp->b_flags |= B_AGE;
298 hammer_stats_file_read += n;
300 if ((ip->flags & HAMMER_INODE_RO) == 0 &&
301 (ip->hmp->mp->mnt_flag & MNT_NOATIME) == 0) {
302 ip->ino_data.atime = trans.time;
303 hammer_modify_inode(ip, HAMMER_INODE_ATIME);
305 hammer_done_transaction(&trans);
310 * hammer_vop_write { vp, uio, ioflag, cred }
314 hammer_vop_write(struct vop_write_args *ap)
316 struct hammer_transaction trans;
317 struct hammer_inode *ip;
330 if (ap->a_vp->v_type != VREG)
336 seqcount = ap->a_ioflag >> 16;
338 if (ip->flags & HAMMER_INODE_RO)
342 * Create a transaction to cover the operations we perform.
344 hammer_start_transaction(&trans, hmp);
350 if (ap->a_ioflag & IO_APPEND)
351 uio->uio_offset = ip->ino_data.size;
354 * Check for illegal write offsets. Valid range is 0...2^63-1.
356 * NOTE: the base_off assignment is required to work around what
357 * I consider to be a GCC-4 optimization bug.
359 if (uio->uio_offset < 0) {
360 hammer_done_transaction(&trans);
363 base_offset = uio->uio_offset + uio->uio_resid; /* work around gcc-4 */
364 if (uio->uio_resid > 0 && base_offset <= 0) {
365 hammer_done_transaction(&trans);
370 * Access the data typically in HAMMER_BUFSIZE blocks via the
371 * buffer cache, but HAMMER may use a variable block size based
374 while (uio->uio_resid > 0) {
379 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_WRITE)) != 0)
382 blksize = hammer_blocksize(uio->uio_offset);
385 * Do not allow HAMMER to blow out the buffer cache. Very
386 * large UIOs can lockout other processes due to bwillwrite()
389 * The hammer inode is not locked during these operations.
390 * The vnode is locked which can interfere with the pageout
391 * daemon for non-UIO_NOCOPY writes but should not interfere
392 * with the buffer cache. Even so, we cannot afford to
393 * allow the pageout daemon to build up too many dirty buffer
396 * Only call this if we aren't being recursively called from
397 * a virtual disk device (vn), else we may deadlock.
399 if ((ap->a_ioflag & IO_RECURSE) == 0)
403 * Do not allow HAMMER to blow out system memory by
404 * accumulating too many records. Records are so well
405 * decoupled from the buffer cache that it is possible
406 * for userland to push data out to the media via
407 * direct-write, but build up the records queued to the
408 * backend faster then the backend can flush them out.
409 * HAMMER has hit its write limit but the frontend has
410 * no pushback to slow it down.
412 if (hmp->rsv_recs > hammer_limit_recs / 2) {
414 * Get the inode on the flush list
416 if (ip->rsv_recs >= 64)
417 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
418 else if (ip->rsv_recs >= 16)
419 hammer_flush_inode(ip, 0);
422 * Keep the flusher going if the system keeps
425 delta = hmp->count_newrecords -
426 hmp->last_newrecords;
427 if (delta < 0 || delta > hammer_limit_recs / 2) {
428 hmp->last_newrecords = hmp->count_newrecords;
429 hammer_sync_hmp(hmp, MNT_NOWAIT);
433 * If we have gotten behind start slowing
436 delta = (hmp->rsv_recs - hammer_limit_recs) *
437 hz / hammer_limit_recs;
439 tsleep(&trans, 0, "hmrslo", delta);
443 * Calculate the blocksize at the current offset and figure
444 * out how much we can actually write.
446 blkmask = blksize - 1;
447 offset = (int)uio->uio_offset & blkmask;
448 base_offset = uio->uio_offset & ~(int64_t)blkmask;
449 n = blksize - offset;
450 if (n > uio->uio_resid)
452 if (uio->uio_offset + n > ip->ino_data.size) {
453 vnode_pager_setsize(ap->a_vp, uio->uio_offset + n);
455 kflags |= NOTE_EXTEND;
458 if (uio->uio_segflg == UIO_NOCOPY) {
460 * Issuing a write with the same data backing the
461 * buffer. Instantiate the buffer to collect the
462 * backing vm pages, then read-in any missing bits.
464 * This case is used by vop_stdputpages().
466 bp = getblk(ap->a_vp, base_offset,
467 blksize, GETBLK_BHEAVY, 0);
468 if ((bp->b_flags & B_CACHE) == 0) {
470 error = bread(ap->a_vp, base_offset,
473 } else if (offset == 0 && uio->uio_resid >= blksize) {
475 * Even though we are entirely overwriting the buffer
476 * we may still have to zero it out to avoid a
477 * mmap/write visibility issue.
479 bp = getblk(ap->a_vp, base_offset, blksize, GETBLK_BHEAVY, 0);
480 if ((bp->b_flags & B_CACHE) == 0)
482 } else if (base_offset >= ip->ino_data.size) {
484 * If the base offset of the buffer is beyond the
485 * file EOF, we don't have to issue a read.
487 bp = getblk(ap->a_vp, base_offset,
488 blksize, GETBLK_BHEAVY, 0);
492 * Partial overwrite, read in any missing bits then
493 * replace the portion being written.
495 error = bread(ap->a_vp, base_offset, blksize, &bp);
500 error = uiomove((char *)bp->b_data + offset,
505 * If we screwed up we have to undo any VM size changes we
511 vtruncbuf(ap->a_vp, ip->ino_data.size,
512 hammer_blocksize(ip->ino_data.size));
516 kflags |= NOTE_WRITE;
517 hammer_stats_file_write += n;
518 /* bp->b_flags |= B_CLUSTEROK; temporarily disabled */
519 if (ip->ino_data.size < uio->uio_offset) {
520 ip->ino_data.size = uio->uio_offset;
521 flags = HAMMER_INODE_DDIRTY;
522 vnode_pager_setsize(ap->a_vp, ip->ino_data.size);
526 ip->ino_data.mtime = trans.time;
527 flags |= HAMMER_INODE_MTIME | HAMMER_INODE_BUFS;
528 hammer_modify_inode(ip, flags);
531 * Once we dirty the buffer any cached zone-X offset
532 * becomes invalid. HAMMER NOTE: no-history mode cannot
533 * allow overwriting over the same data sector unless
534 * we provide UNDOs for the old data, which we don't.
536 bp->b_bio2.bio_offset = NOOFFSET;
539 * Final buffer disposition.
541 bp->b_flags |= B_AGE;
542 if (ap->a_ioflag & IO_SYNC) {
544 } else if (ap->a_ioflag & IO_DIRECT) {
550 hammer_done_transaction(&trans);
551 hammer_knote(ap->a_vp, kflags);
556 * hammer_vop_access { vp, mode, cred }
560 hammer_vop_access(struct vop_access_args *ap)
562 struct hammer_inode *ip = VTOI(ap->a_vp);
567 ++hammer_stats_file_iopsr;
568 uid = hammer_to_unix_xid(&ip->ino_data.uid);
569 gid = hammer_to_unix_xid(&ip->ino_data.gid);
571 error = vop_helper_access(ap, uid, gid, ip->ino_data.mode,
572 ip->ino_data.uflags);
577 * hammer_vop_advlock { vp, id, op, fl, flags }
581 hammer_vop_advlock(struct vop_advlock_args *ap)
583 hammer_inode_t ip = VTOI(ap->a_vp);
585 return (lf_advlock(ap, &ip->advlock, ip->ino_data.size));
589 * hammer_vop_close { vp, fflag }
593 hammer_vop_close(struct vop_close_args *ap)
595 /*hammer_inode_t ip = VTOI(ap->a_vp);*/
596 return (vop_stdclose(ap));
600 * hammer_vop_ncreate { nch, dvp, vpp, cred, vap }
602 * The operating system has already ensured that the directory entry
603 * does not exist and done all appropriate namespace locking.
607 hammer_vop_ncreate(struct vop_ncreate_args *ap)
609 struct hammer_transaction trans;
610 struct hammer_inode *dip;
611 struct hammer_inode *nip;
612 struct nchandle *nch;
616 dip = VTOI(ap->a_dvp);
618 if (dip->flags & HAMMER_INODE_RO)
620 if ((error = hammer_checkspace(dip->hmp, HAMMER_CHKSPC_CREATE)) != 0)
624 * Create a transaction to cover the operations we perform.
626 hammer_start_transaction(&trans, dip->hmp);
627 ++hammer_stats_file_iopsw;
630 * Create a new filesystem object of the requested type. The
631 * returned inode will be referenced and shared-locked to prevent
632 * it from being moved to the flusher.
635 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
638 hkprintf("hammer_create_inode error %d\n", error);
639 hammer_done_transaction(&trans);
645 * Add the new filesystem object to the directory. This will also
646 * bump the inode's link count.
648 error = hammer_ip_add_directory(&trans, dip,
649 nch->ncp->nc_name, nch->ncp->nc_nlen,
652 hkprintf("hammer_ip_add_directory error %d\n", error);
658 hammer_rel_inode(nip, 0);
659 hammer_done_transaction(&trans);
662 error = hammer_get_vnode(nip, ap->a_vpp);
663 hammer_done_transaction(&trans);
664 hammer_rel_inode(nip, 0);
666 cache_setunresolved(ap->a_nch);
667 cache_setvp(ap->a_nch, *ap->a_vpp);
669 hammer_knote(ap->a_dvp, NOTE_WRITE);
675 * hammer_vop_getattr { vp, vap }
677 * Retrieve an inode's attribute information. When accessing inodes
678 * historically we fake the atime field to ensure consistent results.
679 * The atime field is stored in the B-Tree element and allowed to be
680 * updated without cycling the element.
684 hammer_vop_getattr(struct vop_getattr_args *ap)
686 struct hammer_inode *ip = VTOI(ap->a_vp);
687 struct vattr *vap = ap->a_vap;
690 * We want the fsid to be different when accessing a filesystem
691 * with different as-of's so programs like diff don't think
692 * the files are the same.
694 * We also want the fsid to be the same when comparing snapshots,
695 * or when comparing mirrors (which might be backed by different
696 * physical devices). HAMMER fsids are based on the PFS's
699 * XXX there is a chance of collision here. The va_fsid reported
700 * by stat is different from the more involved fsid used in the
703 ++hammer_stats_file_iopsr;
704 vap->va_fsid = ip->pfsm->fsid_udev ^ (u_int32_t)ip->obj_asof ^
705 (u_int32_t)(ip->obj_asof >> 32);
707 vap->va_fileid = ip->ino_leaf.base.obj_id;
708 vap->va_mode = ip->ino_data.mode;
709 vap->va_nlink = ip->ino_data.nlinks;
710 vap->va_uid = hammer_to_unix_xid(&ip->ino_data.uid);
711 vap->va_gid = hammer_to_unix_xid(&ip->ino_data.gid);
714 vap->va_size = ip->ino_data.size;
717 * Special case for @@PFS softlinks. The actual size of the
718 * expanded softlink is "@@0x%016llx:%05d" == 26 bytes.
719 * or for MAX_TID is "@@-1:%05d" == 10 bytes.
721 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_SOFTLINK &&
722 ip->ino_data.size == 10 &&
723 ip->obj_asof == HAMMER_MAX_TID &&
724 ip->obj_localization == 0 &&
725 strncmp(ip->ino_data.ext.symlink, "@@PFS", 5) == 0) {
726 if (ip->pfsm->pfsd.mirror_flags & HAMMER_PFSD_SLAVE)
733 * We must provide a consistent atime and mtime for snapshots
734 * so people can do a 'tar cf - ... | md5' on them and get
735 * consistent results.
737 if (ip->flags & HAMMER_INODE_RO) {
738 hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_atime);
739 hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_mtime);
741 hammer_time_to_timespec(ip->ino_data.atime, &vap->va_atime);
742 hammer_time_to_timespec(ip->ino_data.mtime, &vap->va_mtime);
744 hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_ctime);
745 vap->va_flags = ip->ino_data.uflags;
746 vap->va_gen = 1; /* hammer inums are unique for all time */
747 vap->va_blocksize = HAMMER_BUFSIZE;
748 if (ip->ino_data.size >= HAMMER_XDEMARC) {
749 vap->va_bytes = (ip->ino_data.size + HAMMER_XBUFMASK64) &
751 } else if (ip->ino_data.size > HAMMER_BUFSIZE / 2) {
752 vap->va_bytes = (ip->ino_data.size + HAMMER_BUFMASK64) &
755 vap->va_bytes = (ip->ino_data.size + 15) & ~15;
758 vap->va_type = hammer_get_vnode_type(ip->ino_data.obj_type);
759 vap->va_filerev = 0; /* XXX */
760 /* mtime uniquely identifies any adjustments made to the file XXX */
761 vap->va_fsmid = ip->ino_data.mtime;
762 vap->va_uid_uuid = ip->ino_data.uid;
763 vap->va_gid_uuid = ip->ino_data.gid;
764 vap->va_fsid_uuid = ip->hmp->fsid;
765 vap->va_vaflags = VA_UID_UUID_VALID | VA_GID_UUID_VALID |
768 switch (ip->ino_data.obj_type) {
769 case HAMMER_OBJTYPE_CDEV:
770 case HAMMER_OBJTYPE_BDEV:
771 vap->va_rmajor = ip->ino_data.rmajor;
772 vap->va_rminor = ip->ino_data.rminor;
781 * hammer_vop_nresolve { nch, dvp, cred }
783 * Locate the requested directory entry.
787 hammer_vop_nresolve(struct vop_nresolve_args *ap)
789 struct hammer_transaction trans;
790 struct namecache *ncp;
794 struct hammer_cursor cursor;
803 u_int32_t localization;
804 u_int32_t max_iterations;
807 * Misc initialization, plus handle as-of name extensions. Look for
808 * the '@@' extension. Note that as-of files and directories cannot
811 dip = VTOI(ap->a_dvp);
812 ncp = ap->a_nch->ncp;
813 asof = dip->obj_asof;
814 localization = dip->obj_localization; /* for code consistency */
816 flags = dip->flags & HAMMER_INODE_RO;
819 hammer_simple_transaction(&trans, dip->hmp);
820 ++hammer_stats_file_iopsr;
822 for (i = 0; i < nlen; ++i) {
823 if (ncp->nc_name[i] == '@' && ncp->nc_name[i+1] == '@') {
824 error = hammer_str_to_tid(ncp->nc_name + i + 2,
825 &ispfs, &asof, &localization);
830 if (asof != HAMMER_MAX_TID)
831 flags |= HAMMER_INODE_RO;
838 * If this is a PFS softlink we dive into the PFS
840 if (ispfs && nlen == 0) {
841 ip = hammer_get_inode(&trans, dip, HAMMER_OBJID_ROOT,
845 error = hammer_get_vnode(ip, &vp);
846 hammer_rel_inode(ip, 0);
852 cache_setvp(ap->a_nch, vp);
859 * If there is no path component the time extension is relative to dip.
860 * e.g. "fubar/@@<snapshot>"
862 * "." is handled by the kernel, but ".@@<snapshot>" is not.
863 * e.g. "fubar/.@@<snapshot>"
865 * ".." is handled by the kernel. We do not currently handle
868 if (nlen == 0 || (nlen == 1 && ncp->nc_name[0] == '.')) {
869 ip = hammer_get_inode(&trans, dip, dip->obj_id,
870 asof, dip->obj_localization,
873 error = hammer_get_vnode(ip, &vp);
874 hammer_rel_inode(ip, 0);
880 cache_setvp(ap->a_nch, vp);
887 * Calculate the namekey and setup the key range for the scan. This
888 * works kinda like a chained hash table where the lower 32 bits
889 * of the namekey synthesize the chain.
891 * The key range is inclusive of both key_beg and key_end.
893 namekey = hammer_directory_namekey(dip, ncp->nc_name, nlen,
896 error = hammer_init_cursor(&trans, &cursor, &dip->cache[1], dip);
897 cursor.key_beg.localization = dip->obj_localization +
898 HAMMER_LOCALIZE_MISC;
899 cursor.key_beg.obj_id = dip->obj_id;
900 cursor.key_beg.key = namekey;
901 cursor.key_beg.create_tid = 0;
902 cursor.key_beg.delete_tid = 0;
903 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
904 cursor.key_beg.obj_type = 0;
906 cursor.key_end = cursor.key_beg;
907 cursor.key_end.key += max_iterations;
909 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
912 * Scan all matching records (the chain), locate the one matching
913 * the requested path component.
915 * The hammer_ip_*() functions merge in-memory records with on-disk
916 * records for the purposes of the search.
919 localization = HAMMER_DEF_LOCALIZATION;
922 error = hammer_ip_first(&cursor);
924 error = hammer_ip_resolve_data(&cursor);
927 if (nlen == cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF &&
928 bcmp(ncp->nc_name, cursor.data->entry.name, nlen) == 0) {
929 obj_id = cursor.data->entry.obj_id;
930 localization = cursor.data->entry.localization;
933 error = hammer_ip_next(&cursor);
936 hammer_done_cursor(&cursor);
939 * Lookup the obj_id. This should always succeed. If it does not
940 * the filesystem may be damaged and we return a dummy inode.
943 ip = hammer_get_inode(&trans, dip, obj_id,
946 if (error == ENOENT) {
947 kprintf("HAMMER: WARNING: Missing "
948 "inode for dirent \"%s\"\n"
949 "\tobj_id = %016llx\n",
950 ncp->nc_name, (long long)obj_id);
952 ip = hammer_get_dummy_inode(&trans, dip, obj_id,
957 error = hammer_get_vnode(ip, &vp);
958 hammer_rel_inode(ip, 0);
964 cache_setvp(ap->a_nch, vp);
967 } else if (error == ENOENT) {
968 cache_setvp(ap->a_nch, NULL);
971 hammer_done_transaction(&trans);
976 * hammer_vop_nlookupdotdot { dvp, vpp, cred }
978 * Locate the parent directory of a directory vnode.
980 * dvp is referenced but not locked. *vpp must be returned referenced and
981 * locked. A parent_obj_id of 0 does not necessarily indicate that we are
982 * at the root, instead it could indicate that the directory we were in was
985 * NOTE: as-of sequences are not linked into the directory structure. If
986 * we are at the root with a different asof then the mount point, reload
987 * the same directory with the mount point's asof. I'm not sure what this
988 * will do to NFS. We encode ASOF stamps in NFS file handles so it might not
989 * get confused, but it hasn't been tested.
993 hammer_vop_nlookupdotdot(struct vop_nlookupdotdot_args *ap)
995 struct hammer_transaction trans;
996 struct hammer_inode *dip;
997 struct hammer_inode *ip;
998 int64_t parent_obj_id;
999 u_int32_t parent_obj_localization;
1003 dip = VTOI(ap->a_dvp);
1004 asof = dip->obj_asof;
1007 * Whos are parent? This could be the root of a pseudo-filesystem
1008 * whos parent is in another localization domain.
1010 parent_obj_id = dip->ino_data.parent_obj_id;
1011 if (dip->obj_id == HAMMER_OBJID_ROOT)
1012 parent_obj_localization = dip->ino_data.ext.obj.parent_obj_localization;
1014 parent_obj_localization = dip->obj_localization;
1016 if (parent_obj_id == 0) {
1017 if (dip->obj_id == HAMMER_OBJID_ROOT &&
1018 asof != dip->hmp->asof) {
1019 parent_obj_id = dip->obj_id;
1020 asof = dip->hmp->asof;
1021 *ap->a_fakename = kmalloc(19, M_TEMP, M_WAITOK);
1022 ksnprintf(*ap->a_fakename, 19, "0x%016llx",
1030 hammer_simple_transaction(&trans, dip->hmp);
1031 ++hammer_stats_file_iopsr;
1033 ip = hammer_get_inode(&trans, dip, parent_obj_id,
1034 asof, parent_obj_localization,
1035 dip->flags, &error);
1037 error = hammer_get_vnode(ip, ap->a_vpp);
1038 hammer_rel_inode(ip, 0);
1042 hammer_done_transaction(&trans);
1047 * hammer_vop_nlink { nch, dvp, vp, cred }
1051 hammer_vop_nlink(struct vop_nlink_args *ap)
1053 struct hammer_transaction trans;
1054 struct hammer_inode *dip;
1055 struct hammer_inode *ip;
1056 struct nchandle *nch;
1059 if (ap->a_dvp->v_mount != ap->a_vp->v_mount)
1063 dip = VTOI(ap->a_dvp);
1064 ip = VTOI(ap->a_vp);
1066 if (dip->obj_localization != ip->obj_localization)
1069 if (dip->flags & HAMMER_INODE_RO)
1071 if (ip->flags & HAMMER_INODE_RO)
1073 if ((error = hammer_checkspace(dip->hmp, HAMMER_CHKSPC_CREATE)) != 0)
1077 * Create a transaction to cover the operations we perform.
1079 hammer_start_transaction(&trans, dip->hmp);
1080 ++hammer_stats_file_iopsw;
1083 * Add the filesystem object to the directory. Note that neither
1084 * dip nor ip are referenced or locked, but their vnodes are
1085 * referenced. This function will bump the inode's link count.
1087 error = hammer_ip_add_directory(&trans, dip,
1088 nch->ncp->nc_name, nch->ncp->nc_nlen,
1095 cache_setunresolved(nch);
1096 cache_setvp(nch, ap->a_vp);
1098 hammer_done_transaction(&trans);
1099 hammer_knote(ap->a_vp, NOTE_LINK);
1100 hammer_knote(ap->a_dvp, NOTE_WRITE);
1105 * hammer_vop_nmkdir { nch, dvp, vpp, cred, vap }
1107 * The operating system has already ensured that the directory entry
1108 * does not exist and done all appropriate namespace locking.
1112 hammer_vop_nmkdir(struct vop_nmkdir_args *ap)
1114 struct hammer_transaction trans;
1115 struct hammer_inode *dip;
1116 struct hammer_inode *nip;
1117 struct nchandle *nch;
1121 dip = VTOI(ap->a_dvp);
1123 if (dip->flags & HAMMER_INODE_RO)
1125 if ((error = hammer_checkspace(dip->hmp, HAMMER_CHKSPC_CREATE)) != 0)
1129 * Create a transaction to cover the operations we perform.
1131 hammer_start_transaction(&trans, dip->hmp);
1132 ++hammer_stats_file_iopsw;
1135 * Create a new filesystem object of the requested type. The
1136 * returned inode will be referenced but not locked.
1138 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
1141 hkprintf("hammer_mkdir error %d\n", error);
1142 hammer_done_transaction(&trans);
1147 * Add the new filesystem object to the directory. This will also
1148 * bump the inode's link count.
1150 error = hammer_ip_add_directory(&trans, dip,
1151 nch->ncp->nc_name, nch->ncp->nc_nlen,
1154 hkprintf("hammer_mkdir (add) error %d\n", error);
1160 hammer_rel_inode(nip, 0);
1163 error = hammer_get_vnode(nip, ap->a_vpp);
1164 hammer_rel_inode(nip, 0);
1166 cache_setunresolved(ap->a_nch);
1167 cache_setvp(ap->a_nch, *ap->a_vpp);
1170 hammer_done_transaction(&trans);
1172 hammer_knote(ap->a_dvp, NOTE_WRITE | NOTE_LINK);
1177 * hammer_vop_nmknod { nch, dvp, vpp, cred, vap }
1179 * The operating system has already ensured that the directory entry
1180 * does not exist and done all appropriate namespace locking.
1184 hammer_vop_nmknod(struct vop_nmknod_args *ap)
1186 struct hammer_transaction trans;
1187 struct hammer_inode *dip;
1188 struct hammer_inode *nip;
1189 struct nchandle *nch;
1193 dip = VTOI(ap->a_dvp);
1195 if (dip->flags & HAMMER_INODE_RO)
1197 if ((error = hammer_checkspace(dip->hmp, HAMMER_CHKSPC_CREATE)) != 0)
1201 * Create a transaction to cover the operations we perform.
1203 hammer_start_transaction(&trans, dip->hmp);
1204 ++hammer_stats_file_iopsw;
1207 * Create a new filesystem object of the requested type. The
1208 * returned inode will be referenced but not locked.
1210 * If mknod specifies a directory a pseudo-fs is created.
1212 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
1215 hammer_done_transaction(&trans);
1221 * Add the new filesystem object to the directory. This will also
1222 * bump the inode's link count.
1224 error = hammer_ip_add_directory(&trans, dip,
1225 nch->ncp->nc_name, nch->ncp->nc_nlen,
1232 hammer_rel_inode(nip, 0);
1235 error = hammer_get_vnode(nip, ap->a_vpp);
1236 hammer_rel_inode(nip, 0);
1238 cache_setunresolved(ap->a_nch);
1239 cache_setvp(ap->a_nch, *ap->a_vpp);
1242 hammer_done_transaction(&trans);
1244 hammer_knote(ap->a_dvp, NOTE_WRITE);
1249 * hammer_vop_open { vp, mode, cred, fp }
1253 hammer_vop_open(struct vop_open_args *ap)
1257 ++hammer_stats_file_iopsr;
1258 ip = VTOI(ap->a_vp);
1260 if ((ap->a_mode & FWRITE) && (ip->flags & HAMMER_INODE_RO))
1262 return(vop_stdopen(ap));
1266 * hammer_vop_print { vp }
1270 hammer_vop_print(struct vop_print_args *ap)
1276 * hammer_vop_readdir { vp, uio, cred, *eofflag, *ncookies, off_t **cookies }
1280 hammer_vop_readdir(struct vop_readdir_args *ap)
1282 struct hammer_transaction trans;
1283 struct hammer_cursor cursor;
1284 struct hammer_inode *ip;
1286 hammer_base_elm_t base;
1295 ++hammer_stats_file_iopsr;
1296 ip = VTOI(ap->a_vp);
1298 saveoff = uio->uio_offset;
1300 if (ap->a_ncookies) {
1301 ncookies = uio->uio_resid / 16 + 1;
1302 if (ncookies > 1024)
1304 cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
1312 hammer_simple_transaction(&trans, ip->hmp);
1315 * Handle artificial entries
1317 * It should be noted that the minimum value for a directory
1318 * hash key on-media is 0x0000000100000000, so we can use anything
1319 * less then that to represent our 'special' key space.
1323 r = vop_write_dirent(&error, uio, ip->obj_id, DT_DIR, 1, ".");
1327 cookies[cookie_index] = saveoff;
1330 if (cookie_index == ncookies)
1334 if (ip->ino_data.parent_obj_id) {
1335 r = vop_write_dirent(&error, uio,
1336 ip->ino_data.parent_obj_id,
1339 r = vop_write_dirent(&error, uio,
1340 ip->obj_id, DT_DIR, 2, "..");
1345 cookies[cookie_index] = saveoff;
1348 if (cookie_index == ncookies)
1353 * Key range (begin and end inclusive) to scan. Directory keys
1354 * directly translate to a 64 bit 'seek' position.
1356 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
1357 cursor.key_beg.localization = ip->obj_localization +
1358 HAMMER_LOCALIZE_MISC;
1359 cursor.key_beg.obj_id = ip->obj_id;
1360 cursor.key_beg.create_tid = 0;
1361 cursor.key_beg.delete_tid = 0;
1362 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1363 cursor.key_beg.obj_type = 0;
1364 cursor.key_beg.key = saveoff;
1366 cursor.key_end = cursor.key_beg;
1367 cursor.key_end.key = HAMMER_MAX_KEY;
1368 cursor.asof = ip->obj_asof;
1369 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
1371 error = hammer_ip_first(&cursor);
1373 while (error == 0) {
1374 error = hammer_ip_resolve_data(&cursor);
1377 base = &cursor.leaf->base;
1378 saveoff = base->key;
1379 KKASSERT(cursor.leaf->data_len > HAMMER_ENTRY_NAME_OFF);
1381 if (base->obj_id != ip->obj_id)
1382 panic("readdir: bad record at %p", cursor.node);
1385 * Convert pseudo-filesystems into softlinks
1387 dtype = hammer_get_dtype(cursor.leaf->base.obj_type);
1388 r = vop_write_dirent(
1389 &error, uio, cursor.data->entry.obj_id,
1391 cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF ,
1392 (void *)cursor.data->entry.name);
1397 cookies[cookie_index] = base->key;
1399 if (cookie_index == ncookies)
1401 error = hammer_ip_next(&cursor);
1403 hammer_done_cursor(&cursor);
1406 hammer_done_transaction(&trans);
1409 *ap->a_eofflag = (error == ENOENT);
1410 uio->uio_offset = saveoff;
1411 if (error && cookie_index == 0) {
1412 if (error == ENOENT)
1415 kfree(cookies, M_TEMP);
1416 *ap->a_ncookies = 0;
1417 *ap->a_cookies = NULL;
1420 if (error == ENOENT)
1423 *ap->a_ncookies = cookie_index;
1424 *ap->a_cookies = cookies;
1431 * hammer_vop_readlink { vp, uio, cred }
1435 hammer_vop_readlink(struct vop_readlink_args *ap)
1437 struct hammer_transaction trans;
1438 struct hammer_cursor cursor;
1439 struct hammer_inode *ip;
1441 u_int32_t localization;
1442 hammer_pseudofs_inmem_t pfsm;
1445 ip = VTOI(ap->a_vp);
1448 * Shortcut if the symlink data was stuffed into ino_data.
1450 * Also expand special "@@PFS%05d" softlinks (expansion only
1451 * occurs for non-historical (current) accesses made from the
1452 * primary filesystem).
1454 if (ip->ino_data.size <= HAMMER_INODE_BASESYMLEN) {
1458 ptr = ip->ino_data.ext.symlink;
1459 bytes = (int)ip->ino_data.size;
1461 ip->obj_asof == HAMMER_MAX_TID &&
1462 ip->obj_localization == 0 &&
1463 strncmp(ptr, "@@PFS", 5) == 0) {
1464 hammer_simple_transaction(&trans, ip->hmp);
1465 bcopy(ptr + 5, buf, 5);
1467 localization = strtoul(buf, NULL, 10) << 16;
1468 pfsm = hammer_load_pseudofs(&trans, localization,
1471 if (pfsm->pfsd.mirror_flags &
1472 HAMMER_PFSD_SLAVE) {
1473 /* vap->va_size == 26 */
1474 ksnprintf(buf, sizeof(buf),
1476 pfsm->pfsd.sync_end_tid,
1477 localization >> 16);
1479 /* vap->va_size == 10 */
1480 ksnprintf(buf, sizeof(buf),
1482 localization >> 16);
1484 ksnprintf(buf, sizeof(buf),
1487 localization >> 16);
1491 bytes = strlen(buf);
1494 hammer_rel_pseudofs(trans.hmp, pfsm);
1495 hammer_done_transaction(&trans);
1497 error = uiomove(ptr, bytes, ap->a_uio);
1504 hammer_simple_transaction(&trans, ip->hmp);
1505 ++hammer_stats_file_iopsr;
1506 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
1509 * Key range (begin and end inclusive) to scan. Directory keys
1510 * directly translate to a 64 bit 'seek' position.
1512 cursor.key_beg.localization = ip->obj_localization +
1513 HAMMER_LOCALIZE_MISC;
1514 cursor.key_beg.obj_id = ip->obj_id;
1515 cursor.key_beg.create_tid = 0;
1516 cursor.key_beg.delete_tid = 0;
1517 cursor.key_beg.rec_type = HAMMER_RECTYPE_FIX;
1518 cursor.key_beg.obj_type = 0;
1519 cursor.key_beg.key = HAMMER_FIXKEY_SYMLINK;
1520 cursor.asof = ip->obj_asof;
1521 cursor.flags |= HAMMER_CURSOR_ASOF;
1523 error = hammer_ip_lookup(&cursor);
1525 error = hammer_ip_resolve_data(&cursor);
1527 KKASSERT(cursor.leaf->data_len >=
1528 HAMMER_SYMLINK_NAME_OFF);
1529 error = uiomove(cursor.data->symlink.name,
1530 cursor.leaf->data_len -
1531 HAMMER_SYMLINK_NAME_OFF,
1535 hammer_done_cursor(&cursor);
1536 hammer_done_transaction(&trans);
1541 * hammer_vop_nremove { nch, dvp, cred }
1545 hammer_vop_nremove(struct vop_nremove_args *ap)
1547 struct hammer_transaction trans;
1548 struct hammer_inode *dip;
1551 dip = VTOI(ap->a_dvp);
1553 if (hammer_nohistory(dip) == 0 &&
1554 (error = hammer_checkspace(dip->hmp, HAMMER_CHKSPC_REMOVE)) != 0) {
1558 hammer_start_transaction(&trans, dip->hmp);
1559 ++hammer_stats_file_iopsw;
1560 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp, ap->a_cred, 0, 0);
1561 hammer_done_transaction(&trans);
1563 hammer_knote(ap->a_dvp, NOTE_WRITE);
1568 * hammer_vop_nrename { fnch, tnch, fdvp, tdvp, cred }
1572 hammer_vop_nrename(struct vop_nrename_args *ap)
1574 struct hammer_transaction trans;
1575 struct namecache *fncp;
1576 struct namecache *tncp;
1577 struct hammer_inode *fdip;
1578 struct hammer_inode *tdip;
1579 struct hammer_inode *ip;
1580 struct hammer_cursor cursor;
1582 u_int32_t max_iterations;
1585 if (ap->a_fdvp->v_mount != ap->a_tdvp->v_mount)
1587 if (ap->a_fdvp->v_mount != ap->a_fnch->ncp->nc_vp->v_mount)
1590 fdip = VTOI(ap->a_fdvp);
1591 tdip = VTOI(ap->a_tdvp);
1592 fncp = ap->a_fnch->ncp;
1593 tncp = ap->a_tnch->ncp;
1594 ip = VTOI(fncp->nc_vp);
1595 KKASSERT(ip != NULL);
1597 if (fdip->obj_localization != tdip->obj_localization)
1599 if (fdip->obj_localization != ip->obj_localization)
1602 if (fdip->flags & HAMMER_INODE_RO)
1604 if (tdip->flags & HAMMER_INODE_RO)
1606 if (ip->flags & HAMMER_INODE_RO)
1608 if ((error = hammer_checkspace(fdip->hmp, HAMMER_CHKSPC_CREATE)) != 0)
1611 hammer_start_transaction(&trans, fdip->hmp);
1612 ++hammer_stats_file_iopsw;
1615 * Remove tncp from the target directory and then link ip as
1616 * tncp. XXX pass trans to dounlink
1618 * Force the inode sync-time to match the transaction so it is
1619 * in-sync with the creation of the target directory entry.
1621 error = hammer_dounlink(&trans, ap->a_tnch, ap->a_tdvp,
1623 if (error == 0 || error == ENOENT) {
1624 error = hammer_ip_add_directory(&trans, tdip,
1625 tncp->nc_name, tncp->nc_nlen,
1628 ip->ino_data.parent_obj_id = tdip->obj_id;
1629 hammer_modify_inode(ip, HAMMER_INODE_DDIRTY);
1633 goto failed; /* XXX */
1636 * Locate the record in the originating directory and remove it.
1638 * Calculate the namekey and setup the key range for the scan. This
1639 * works kinda like a chained hash table where the lower 32 bits
1640 * of the namekey synthesize the chain.
1642 * The key range is inclusive of both key_beg and key_end.
1644 namekey = hammer_directory_namekey(fdip, fncp->nc_name, fncp->nc_nlen,
1647 hammer_init_cursor(&trans, &cursor, &fdip->cache[1], fdip);
1648 cursor.key_beg.localization = fdip->obj_localization +
1649 HAMMER_LOCALIZE_MISC;
1650 cursor.key_beg.obj_id = fdip->obj_id;
1651 cursor.key_beg.key = namekey;
1652 cursor.key_beg.create_tid = 0;
1653 cursor.key_beg.delete_tid = 0;
1654 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1655 cursor.key_beg.obj_type = 0;
1657 cursor.key_end = cursor.key_beg;
1658 cursor.key_end.key += max_iterations;
1659 cursor.asof = fdip->obj_asof;
1660 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
1663 * Scan all matching records (the chain), locate the one matching
1664 * the requested path component.
1666 * The hammer_ip_*() functions merge in-memory records with on-disk
1667 * records for the purposes of the search.
1669 error = hammer_ip_first(&cursor);
1670 while (error == 0) {
1671 if (hammer_ip_resolve_data(&cursor) != 0)
1673 nlen = cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF;
1675 if (fncp->nc_nlen == nlen &&
1676 bcmp(fncp->nc_name, cursor.data->entry.name, nlen) == 0) {
1679 error = hammer_ip_next(&cursor);
1683 * If all is ok we have to get the inode so we can adjust nlinks.
1685 * WARNING: hammer_ip_del_directory() may have to terminate the
1686 * cursor to avoid a recursion. It's ok to call hammer_done_cursor()
1690 error = hammer_ip_del_directory(&trans, &cursor, fdip, ip);
1693 * XXX A deadlock here will break rename's atomicy for the purposes
1694 * of crash recovery.
1696 if (error == EDEADLK) {
1697 hammer_done_cursor(&cursor);
1702 * Cleanup and tell the kernel that the rename succeeded.
1704 hammer_done_cursor(&cursor);
1706 cache_rename(ap->a_fnch, ap->a_tnch);
1707 hammer_knote(ap->a_fdvp, NOTE_WRITE);
1708 hammer_knote(ap->a_tdvp, NOTE_WRITE);
1710 hammer_knote(ip->vp, NOTE_RENAME);
1714 hammer_done_transaction(&trans);
1719 * hammer_vop_nrmdir { nch, dvp, cred }
1723 hammer_vop_nrmdir(struct vop_nrmdir_args *ap)
1725 struct hammer_transaction trans;
1726 struct hammer_inode *dip;
1729 dip = VTOI(ap->a_dvp);
1731 if (hammer_nohistory(dip) == 0 &&
1732 (error = hammer_checkspace(dip->hmp, HAMMER_CHKSPC_REMOVE)) != 0) {
1736 hammer_start_transaction(&trans, dip->hmp);
1737 ++hammer_stats_file_iopsw;
1738 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp, ap->a_cred, 0, 1);
1739 hammer_done_transaction(&trans);
1741 hammer_knote(ap->a_dvp, NOTE_WRITE | NOTE_LINK);
1746 * hammer_vop_markatime { vp, cred }
1750 hammer_vop_markatime(struct vop_markatime_args *ap)
1752 struct hammer_transaction trans;
1753 struct hammer_inode *ip;
1755 ip = VTOI(ap->a_vp);
1756 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
1758 if (ip->flags & HAMMER_INODE_RO)
1760 if (ip->hmp->mp->mnt_flag & MNT_NOATIME)
1762 hammer_start_transaction(&trans, ip->hmp);
1763 ++hammer_stats_file_iopsw;
1765 ip->ino_data.atime = trans.time;
1766 hammer_modify_inode(ip, HAMMER_INODE_ATIME);
1767 hammer_done_transaction(&trans);
1768 hammer_knote(ap->a_vp, NOTE_ATTRIB);
1773 * hammer_vop_setattr { vp, vap, cred }
1777 hammer_vop_setattr(struct vop_setattr_args *ap)
1779 struct hammer_transaction trans;
1781 struct hammer_inode *ip;
1787 int64_t aligned_size;
1791 ip = ap->a_vp->v_data;
1795 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
1797 if (ip->flags & HAMMER_INODE_RO)
1799 if (hammer_nohistory(ip) == 0 &&
1800 (error = hammer_checkspace(ip->hmp, HAMMER_CHKSPC_REMOVE)) != 0) {
1804 hammer_start_transaction(&trans, ip->hmp);
1805 ++hammer_stats_file_iopsw;
1808 if (vap->va_flags != VNOVAL) {
1809 flags = ip->ino_data.uflags;
1810 error = vop_helper_setattr_flags(&flags, vap->va_flags,
1811 hammer_to_unix_xid(&ip->ino_data.uid),
1814 if (ip->ino_data.uflags != flags) {
1815 ip->ino_data.uflags = flags;
1816 modflags |= HAMMER_INODE_DDIRTY;
1817 kflags |= NOTE_ATTRIB;
1819 if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) {
1826 if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) {
1830 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
1831 mode_t cur_mode = ip->ino_data.mode;
1832 uid_t cur_uid = hammer_to_unix_xid(&ip->ino_data.uid);
1833 gid_t cur_gid = hammer_to_unix_xid(&ip->ino_data.gid);
1837 error = vop_helper_chown(ap->a_vp, vap->va_uid, vap->va_gid,
1839 &cur_uid, &cur_gid, &cur_mode);
1841 hammer_guid_to_uuid(&uuid_uid, cur_uid);
1842 hammer_guid_to_uuid(&uuid_gid, cur_gid);
1843 if (bcmp(&uuid_uid, &ip->ino_data.uid,
1844 sizeof(uuid_uid)) ||
1845 bcmp(&uuid_gid, &ip->ino_data.gid,
1846 sizeof(uuid_gid)) ||
1847 ip->ino_data.mode != cur_mode
1849 ip->ino_data.uid = uuid_uid;
1850 ip->ino_data.gid = uuid_gid;
1851 ip->ino_data.mode = cur_mode;
1853 modflags |= HAMMER_INODE_DDIRTY;
1854 kflags |= NOTE_ATTRIB;
1857 while (vap->va_size != VNOVAL && ip->ino_data.size != vap->va_size) {
1858 switch(ap->a_vp->v_type) {
1860 if (vap->va_size == ip->ino_data.size)
1863 * XXX break atomicy, we can deadlock the backend
1864 * if we do not release the lock. Probably not a
1867 blksize = hammer_blocksize(vap->va_size);
1868 if (vap->va_size < ip->ino_data.size) {
1869 vtruncbuf(ap->a_vp, vap->va_size, blksize);
1871 kflags |= NOTE_WRITE;
1873 vnode_pager_setsize(ap->a_vp, vap->va_size);
1875 kflags |= NOTE_WRITE | NOTE_EXTEND;
1877 ip->ino_data.size = vap->va_size;
1878 modflags |= HAMMER_INODE_DDIRTY;
1881 * on-media truncation is cached in the inode until
1882 * the inode is synchronized.
1885 hammer_ip_frontend_trunc(ip, vap->va_size);
1886 #ifdef DEBUG_TRUNCATE
1887 if (HammerTruncIp == NULL)
1890 if ((ip->flags & HAMMER_INODE_TRUNCATED) == 0) {
1891 ip->flags |= HAMMER_INODE_TRUNCATED;
1892 ip->trunc_off = vap->va_size;
1893 #ifdef DEBUG_TRUNCATE
1894 if (ip == HammerTruncIp)
1895 kprintf("truncate1 %016llx\n", ip->trunc_off);
1897 } else if (ip->trunc_off > vap->va_size) {
1898 ip->trunc_off = vap->va_size;
1899 #ifdef DEBUG_TRUNCATE
1900 if (ip == HammerTruncIp)
1901 kprintf("truncate2 %016llx\n", ip->trunc_off);
1904 #ifdef DEBUG_TRUNCATE
1905 if (ip == HammerTruncIp)
1906 kprintf("truncate3 %016llx (ignored)\n", vap->va_size);
1912 * If truncating we have to clean out a portion of
1913 * the last block on-disk. We do this in the
1914 * front-end buffer cache.
1916 aligned_size = (vap->va_size + (blksize - 1)) &
1917 ~(int64_t)(blksize - 1);
1918 if (truncating && vap->va_size < aligned_size) {
1922 aligned_size -= blksize;
1924 offset = (int)vap->va_size & (blksize - 1);
1925 error = bread(ap->a_vp, aligned_size,
1927 hammer_ip_frontend_trunc(ip, aligned_size);
1929 bzero(bp->b_data + offset,
1931 /* must de-cache direct-io offset */
1932 bp->b_bio2.bio_offset = NOOFFSET;
1935 kprintf("ERROR %d\n", error);
1941 if ((ip->flags & HAMMER_INODE_TRUNCATED) == 0) {
1942 ip->flags |= HAMMER_INODE_TRUNCATED;
1943 ip->trunc_off = vap->va_size;
1944 } else if (ip->trunc_off > vap->va_size) {
1945 ip->trunc_off = vap->va_size;
1947 hammer_ip_frontend_trunc(ip, vap->va_size);
1948 ip->ino_data.size = vap->va_size;
1949 modflags |= HAMMER_INODE_DDIRTY;
1950 kflags |= NOTE_ATTRIB;
1958 if (vap->va_atime.tv_sec != VNOVAL) {
1959 ip->ino_data.atime =
1960 hammer_timespec_to_time(&vap->va_atime);
1961 modflags |= HAMMER_INODE_ATIME;
1962 kflags |= NOTE_ATTRIB;
1964 if (vap->va_mtime.tv_sec != VNOVAL) {
1965 ip->ino_data.mtime =
1966 hammer_timespec_to_time(&vap->va_mtime);
1967 modflags |= HAMMER_INODE_MTIME;
1968 kflags |= NOTE_ATTRIB;
1970 if (vap->va_mode != (mode_t)VNOVAL) {
1971 mode_t cur_mode = ip->ino_data.mode;
1972 uid_t cur_uid = hammer_to_unix_xid(&ip->ino_data.uid);
1973 gid_t cur_gid = hammer_to_unix_xid(&ip->ino_data.gid);
1975 error = vop_helper_chmod(ap->a_vp, vap->va_mode, ap->a_cred,
1976 cur_uid, cur_gid, &cur_mode);
1977 if (error == 0 && ip->ino_data.mode != cur_mode) {
1978 ip->ino_data.mode = cur_mode;
1979 modflags |= HAMMER_INODE_DDIRTY;
1980 kflags |= NOTE_ATTRIB;
1985 hammer_modify_inode(ip, modflags);
1986 hammer_done_transaction(&trans);
1987 hammer_knote(ap->a_vp, kflags);
1992 * hammer_vop_nsymlink { nch, dvp, vpp, cred, vap, target }
1996 hammer_vop_nsymlink(struct vop_nsymlink_args *ap)
1998 struct hammer_transaction trans;
1999 struct hammer_inode *dip;
2000 struct hammer_inode *nip;
2001 struct nchandle *nch;
2002 hammer_record_t record;
2006 ap->a_vap->va_type = VLNK;
2009 dip = VTOI(ap->a_dvp);
2011 if (dip->flags & HAMMER_INODE_RO)
2013 if ((error = hammer_checkspace(dip->hmp, HAMMER_CHKSPC_CREATE)) != 0)
2017 * Create a transaction to cover the operations we perform.
2019 hammer_start_transaction(&trans, dip->hmp);
2020 ++hammer_stats_file_iopsw;
2023 * Create a new filesystem object of the requested type. The
2024 * returned inode will be referenced but not locked.
2027 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
2030 hammer_done_transaction(&trans);
2036 * Add a record representing the symlink. symlink stores the link
2037 * as pure data, not a string, and is no \0 terminated.
2040 bytes = strlen(ap->a_target);
2042 if (bytes <= HAMMER_INODE_BASESYMLEN) {
2043 bcopy(ap->a_target, nip->ino_data.ext.symlink, bytes);
2045 record = hammer_alloc_mem_record(nip, bytes);
2046 record->type = HAMMER_MEM_RECORD_GENERAL;
2048 record->leaf.base.localization = nip->obj_localization +
2049 HAMMER_LOCALIZE_MISC;
2050 record->leaf.base.key = HAMMER_FIXKEY_SYMLINK;
2051 record->leaf.base.rec_type = HAMMER_RECTYPE_FIX;
2052 record->leaf.data_len = bytes;
2053 KKASSERT(HAMMER_SYMLINK_NAME_OFF == 0);
2054 bcopy(ap->a_target, record->data->symlink.name, bytes);
2055 error = hammer_ip_add_record(&trans, record);
2059 * Set the file size to the length of the link.
2062 nip->ino_data.size = bytes;
2063 hammer_modify_inode(nip, HAMMER_INODE_DDIRTY);
2067 error = hammer_ip_add_directory(&trans, dip, nch->ncp->nc_name,
2068 nch->ncp->nc_nlen, nip);
2074 hammer_rel_inode(nip, 0);
2077 error = hammer_get_vnode(nip, ap->a_vpp);
2078 hammer_rel_inode(nip, 0);
2080 cache_setunresolved(ap->a_nch);
2081 cache_setvp(ap->a_nch, *ap->a_vpp);
2082 hammer_knote(ap->a_dvp, NOTE_WRITE);
2085 hammer_done_transaction(&trans);
2090 * hammer_vop_nwhiteout { nch, dvp, cred, flags }
2094 hammer_vop_nwhiteout(struct vop_nwhiteout_args *ap)
2096 struct hammer_transaction trans;
2097 struct hammer_inode *dip;
2100 dip = VTOI(ap->a_dvp);
2102 if (hammer_nohistory(dip) == 0 &&
2103 (error = hammer_checkspace(dip->hmp, HAMMER_CHKSPC_CREATE)) != 0) {
2107 hammer_start_transaction(&trans, dip->hmp);
2108 ++hammer_stats_file_iopsw;
2109 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp,
2110 ap->a_cred, ap->a_flags, -1);
2111 hammer_done_transaction(&trans);
2117 * hammer_vop_ioctl { vp, command, data, fflag, cred }
2121 hammer_vop_ioctl(struct vop_ioctl_args *ap)
2123 struct hammer_inode *ip = ap->a_vp->v_data;
2125 ++hammer_stats_file_iopsr;
2126 return(hammer_ioctl(ip, ap->a_command, ap->a_data,
2127 ap->a_fflag, ap->a_cred));
2132 hammer_vop_mountctl(struct vop_mountctl_args *ap)
2137 mp = ap->a_head.a_ops->head.vv_mount;
2140 case MOUNTCTL_SET_EXPORT:
2141 if (ap->a_ctllen != sizeof(struct export_args))
2144 error = hammer_vfs_export(mp, ap->a_op,
2145 (const struct export_args *)ap->a_ctl);
2148 error = journal_mountctl(ap);
2155 * hammer_vop_strategy { vp, bio }
2157 * Strategy call, used for regular file read & write only. Note that the
2158 * bp may represent a cluster.
2160 * To simplify operation and allow better optimizations in the future,
2161 * this code does not make any assumptions with regards to buffer alignment
2166 hammer_vop_strategy(struct vop_strategy_args *ap)
2171 bp = ap->a_bio->bio_buf;
2175 error = hammer_vop_strategy_read(ap);
2178 error = hammer_vop_strategy_write(ap);
2181 bp->b_error = error = EINVAL;
2182 bp->b_flags |= B_ERROR;
2190 * Read from a regular file. Iterate the related records and fill in the
2191 * BIO/BUF. Gaps are zero-filled.
2193 * The support code in hammer_object.c should be used to deal with mixed
2194 * in-memory and on-disk records.
2196 * NOTE: Can be called from the cluster code with an oversized buf.
2202 hammer_vop_strategy_read(struct vop_strategy_args *ap)
2204 struct hammer_transaction trans;
2205 struct hammer_inode *ip;
2206 struct hammer_cursor cursor;
2207 hammer_base_elm_t base;
2208 hammer_off_t disk_offset;
2222 ip = ap->a_vp->v_data;
2225 * The zone-2 disk offset may have been set by the cluster code via
2226 * a BMAP operation, or else should be NOOFFSET.
2228 * Checking the high bits for a match against zone-2 should suffice.
2230 nbio = push_bio(bio);
2231 if ((nbio->bio_offset & HAMMER_OFF_ZONE_MASK) ==
2232 HAMMER_ZONE_LARGE_DATA) {
2233 error = hammer_io_direct_read(ip->hmp, nbio, NULL);
2238 * Well, that sucked. Do it the hard way. If all the stars are
2239 * aligned we may still be able to issue a direct-read.
2241 hammer_simple_transaction(&trans, ip->hmp);
2242 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
2245 * Key range (begin and end inclusive) to scan. Note that the key's
2246 * stored in the actual records represent BASE+LEN, not BASE. The
2247 * first record containing bio_offset will have a key > bio_offset.
2249 cursor.key_beg.localization = ip->obj_localization +
2250 HAMMER_LOCALIZE_MISC;
2251 cursor.key_beg.obj_id = ip->obj_id;
2252 cursor.key_beg.create_tid = 0;
2253 cursor.key_beg.delete_tid = 0;
2254 cursor.key_beg.obj_type = 0;
2255 cursor.key_beg.key = bio->bio_offset + 1;
2256 cursor.asof = ip->obj_asof;
2257 cursor.flags |= HAMMER_CURSOR_ASOF;
2259 cursor.key_end = cursor.key_beg;
2260 KKASSERT(ip->ino_data.obj_type == HAMMER_OBJTYPE_REGFILE);
2262 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_DBFILE) {
2263 cursor.key_beg.rec_type = HAMMER_RECTYPE_DB;
2264 cursor.key_end.rec_type = HAMMER_RECTYPE_DB;
2265 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
2269 ran_end = bio->bio_offset + bp->b_bufsize;
2270 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
2271 cursor.key_end.rec_type = HAMMER_RECTYPE_DATA;
2272 tmp64 = ran_end + MAXPHYS + 1; /* work-around GCC-4 bug */
2273 if (tmp64 < ran_end)
2274 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
2276 cursor.key_end.key = ran_end + MAXPHYS + 1;
2278 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
2280 error = hammer_ip_first(&cursor);
2283 while (error == 0) {
2285 * Get the base file offset of the record. The key for
2286 * data records is (base + bytes) rather then (base).
2288 base = &cursor.leaf->base;
2289 rec_offset = base->key - cursor.leaf->data_len;
2292 * Calculate the gap, if any, and zero-fill it.
2294 * n is the offset of the start of the record verses our
2295 * current seek offset in the bio.
2297 n = (int)(rec_offset - (bio->bio_offset + boff));
2299 if (n > bp->b_bufsize - boff)
2300 n = bp->b_bufsize - boff;
2301 bzero((char *)bp->b_data + boff, n);
2307 * Calculate the data offset in the record and the number
2308 * of bytes we can copy.
2310 * There are two degenerate cases. First, boff may already
2311 * be at bp->b_bufsize. Secondly, the data offset within
2312 * the record may exceed the record's size.
2316 n = cursor.leaf->data_len - roff;
2318 kprintf("strategy_read: bad n=%d roff=%d\n", n, roff);
2320 } else if (n > bp->b_bufsize - boff) {
2321 n = bp->b_bufsize - boff;
2325 * Deal with cached truncations. This cool bit of code
2326 * allows truncate()/ftruncate() to avoid having to sync
2329 * If the frontend is truncated then all backend records are
2330 * subject to the frontend's truncation.
2332 * If the backend is truncated then backend records on-disk
2333 * (but not in-memory) are subject to the backend's
2334 * truncation. In-memory records owned by the backend
2335 * represent data written after the truncation point on the
2336 * backend and must not be truncated.
2338 * Truncate operations deal with frontend buffer cache
2339 * buffers and frontend-owned in-memory records synchronously.
2341 if (ip->flags & HAMMER_INODE_TRUNCATED) {
2342 if (hammer_cursor_ondisk(&cursor) ||
2343 cursor.iprec->flush_state == HAMMER_FST_FLUSH) {
2344 if (ip->trunc_off <= rec_offset)
2346 else if (ip->trunc_off < rec_offset + n)
2347 n = (int)(ip->trunc_off - rec_offset);
2350 if (ip->sync_flags & HAMMER_INODE_TRUNCATED) {
2351 if (hammer_cursor_ondisk(&cursor)) {
2352 if (ip->sync_trunc_off <= rec_offset)
2354 else if (ip->sync_trunc_off < rec_offset + n)
2355 n = (int)(ip->sync_trunc_off - rec_offset);
2360 * Try to issue a direct read into our bio if possible,
2361 * otherwise resolve the element data into a hammer_buffer
2364 * The buffer on-disk should be zerod past any real
2365 * truncation point, but may not be for any synthesized
2366 * truncation point from above.
2368 disk_offset = cursor.leaf->data_offset + roff;
2369 if (boff == 0 && n == bp->b_bufsize &&
2370 hammer_cursor_ondisk(&cursor) &&
2371 (disk_offset & HAMMER_BUFMASK) == 0) {
2372 KKASSERT((disk_offset & HAMMER_OFF_ZONE_MASK) ==
2373 HAMMER_ZONE_LARGE_DATA);
2374 nbio->bio_offset = disk_offset;
2375 error = hammer_io_direct_read(trans.hmp, nbio,
2379 error = hammer_ip_resolve_data(&cursor);
2381 bcopy((char *)cursor.data + roff,
2382 (char *)bp->b_data + boff, n);
2389 * Iterate until we have filled the request.
2392 if (boff == bp->b_bufsize)
2394 error = hammer_ip_next(&cursor);
2398 * There may have been a gap after the last record
2400 if (error == ENOENT)
2402 if (error == 0 && boff != bp->b_bufsize) {
2403 KKASSERT(boff < bp->b_bufsize);
2404 bzero((char *)bp->b_data + boff, bp->b_bufsize - boff);
2405 /* boff = bp->b_bufsize; */
2408 bp->b_error = error;
2410 bp->b_flags |= B_ERROR;
2415 hammer_cache_node(&ip->cache[1], cursor.node);
2416 hammer_done_cursor(&cursor);
2417 hammer_done_transaction(&trans);
2422 * BMAP operation - used to support cluster_read() only.
2424 * (struct vnode *vp, off_t loffset, off_t *doffsetp, int *runp, int *runb)
2426 * This routine may return EOPNOTSUPP if the opration is not supported for
2427 * the specified offset. The contents of the pointer arguments do not
2428 * need to be initialized in that case.
2430 * If a disk address is available and properly aligned return 0 with
2431 * *doffsetp set to the zone-2 address, and *runp / *runb set appropriately
2432 * to the run-length relative to that offset. Callers may assume that
2433 * *doffsetp is valid if 0 is returned, even if *runp is not sufficiently
2434 * large, so return EOPNOTSUPP if it is not sufficiently large.
2438 hammer_vop_bmap(struct vop_bmap_args *ap)
2440 struct hammer_transaction trans;
2441 struct hammer_inode *ip;
2442 struct hammer_cursor cursor;
2443 hammer_base_elm_t base;
2447 int64_t base_offset;
2448 int64_t base_disk_offset;
2449 int64_t last_offset;
2450 hammer_off_t last_disk_offset;
2451 hammer_off_t disk_offset;
2456 ++hammer_stats_file_iopsr;
2457 ip = ap->a_vp->v_data;
2460 * We can only BMAP regular files. We can't BMAP database files,
2463 if (ip->ino_data.obj_type != HAMMER_OBJTYPE_REGFILE)
2467 * bmap is typically called with runp/runb both NULL when used
2468 * for writing. We do not support BMAP for writing atm.
2470 if (ap->a_cmd != BUF_CMD_READ)
2474 * Scan the B-Tree to acquire blockmap addresses, then translate
2477 hammer_simple_transaction(&trans, ip->hmp);
2479 kprintf("bmap_beg %016llx ip->cache %p\n", ap->a_loffset, ip->cache[1]);
2481 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
2484 * Key range (begin and end inclusive) to scan. Note that the key's
2485 * stored in the actual records represent BASE+LEN, not BASE. The
2486 * first record containing bio_offset will have a key > bio_offset.
2488 cursor.key_beg.localization = ip->obj_localization +
2489 HAMMER_LOCALIZE_MISC;
2490 cursor.key_beg.obj_id = ip->obj_id;
2491 cursor.key_beg.create_tid = 0;
2492 cursor.key_beg.delete_tid = 0;
2493 cursor.key_beg.obj_type = 0;
2495 cursor.key_beg.key = ap->a_loffset - MAXPHYS + 1;
2497 cursor.key_beg.key = ap->a_loffset + 1;
2498 if (cursor.key_beg.key < 0)
2499 cursor.key_beg.key = 0;
2500 cursor.asof = ip->obj_asof;
2501 cursor.flags |= HAMMER_CURSOR_ASOF;
2503 cursor.key_end = cursor.key_beg;
2504 KKASSERT(ip->ino_data.obj_type == HAMMER_OBJTYPE_REGFILE);
2506 ran_end = ap->a_loffset + MAXPHYS;
2507 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
2508 cursor.key_end.rec_type = HAMMER_RECTYPE_DATA;
2509 tmp64 = ran_end + MAXPHYS + 1; /* work-around GCC-4 bug */
2510 if (tmp64 < ran_end)
2511 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
2513 cursor.key_end.key = ran_end + MAXPHYS + 1;
2515 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
2517 error = hammer_ip_first(&cursor);
2518 base_offset = last_offset = 0;
2519 base_disk_offset = last_disk_offset = 0;
2521 while (error == 0) {
2523 * Get the base file offset of the record. The key for
2524 * data records is (base + bytes) rather then (base).
2526 * NOTE: rec_offset + rec_len may exceed the end-of-file.
2527 * The extra bytes should be zero on-disk and the BMAP op
2528 * should still be ok.
2530 base = &cursor.leaf->base;
2531 rec_offset = base->key - cursor.leaf->data_len;
2532 rec_len = cursor.leaf->data_len;
2535 * Incorporate any cached truncation.
2537 * NOTE: Modifications to rec_len based on synthesized
2538 * truncation points remove the guarantee that any extended
2539 * data on disk is zero (since the truncations may not have
2540 * taken place on-media yet).
2542 if (ip->flags & HAMMER_INODE_TRUNCATED) {
2543 if (hammer_cursor_ondisk(&cursor) ||
2544 cursor.iprec->flush_state == HAMMER_FST_FLUSH) {
2545 if (ip->trunc_off <= rec_offset)
2547 else if (ip->trunc_off < rec_offset + rec_len)
2548 rec_len = (int)(ip->trunc_off - rec_offset);
2551 if (ip->sync_flags & HAMMER_INODE_TRUNCATED) {
2552 if (hammer_cursor_ondisk(&cursor)) {
2553 if (ip->sync_trunc_off <= rec_offset)
2555 else if (ip->sync_trunc_off < rec_offset + rec_len)
2556 rec_len = (int)(ip->sync_trunc_off - rec_offset);
2561 * Accumulate information. If we have hit a discontiguous
2562 * block reset base_offset unless we are already beyond the
2563 * requested offset. If we are, that's it, we stop.
2567 if (hammer_cursor_ondisk(&cursor)) {
2568 disk_offset = cursor.leaf->data_offset;
2569 if (rec_offset != last_offset ||
2570 disk_offset != last_disk_offset) {
2571 if (rec_offset > ap->a_loffset)
2573 base_offset = rec_offset;
2574 base_disk_offset = disk_offset;
2576 last_offset = rec_offset + rec_len;
2577 last_disk_offset = disk_offset + rec_len;
2579 error = hammer_ip_next(&cursor);
2583 kprintf("BMAP %016llx: %016llx - %016llx\n",
2584 ap->a_loffset, base_offset, last_offset);
2585 kprintf("BMAP %16s: %016llx - %016llx\n",
2586 "", base_disk_offset, last_disk_offset);
2590 hammer_cache_node(&ip->cache[1], cursor.node);
2592 kprintf("bmap_end2 %016llx ip->cache %p\n", ap->a_loffset, ip->cache[1]);
2595 hammer_done_cursor(&cursor);
2596 hammer_done_transaction(&trans);
2599 * If we couldn't find any records or the records we did find were
2600 * all behind the requested offset, return failure. A forward
2601 * truncation can leave a hole w/ no on-disk records.
2603 if (last_offset == 0 || last_offset < ap->a_loffset)
2604 return (EOPNOTSUPP);
2607 * Figure out the block size at the requested offset and adjust
2608 * our limits so the cluster_read() does not create inappropriately
2609 * sized buffer cache buffers.
2611 blksize = hammer_blocksize(ap->a_loffset);
2612 if (hammer_blocksize(base_offset) != blksize) {
2613 base_offset = hammer_blockdemarc(base_offset, ap->a_loffset);
2615 if (last_offset != ap->a_loffset &&
2616 hammer_blocksize(last_offset - 1) != blksize) {
2617 last_offset = hammer_blockdemarc(ap->a_loffset,
2622 * Returning EOPNOTSUPP simply prevents the direct-IO optimization
2625 disk_offset = base_disk_offset + (ap->a_loffset - base_offset);
2627 if ((disk_offset & HAMMER_OFF_ZONE_MASK) != HAMMER_ZONE_LARGE_DATA) {
2629 * Only large-data zones can be direct-IOd
2632 } else if ((disk_offset & HAMMER_BUFMASK) ||
2633 (last_offset - ap->a_loffset) < blksize) {
2635 * doffsetp is not aligned or the forward run size does
2636 * not cover a whole buffer, disallow the direct I/O.
2643 *ap->a_doffsetp = disk_offset;
2645 *ap->a_runb = ap->a_loffset - base_offset;
2646 KKASSERT(*ap->a_runb >= 0);
2649 *ap->a_runp = last_offset - ap->a_loffset;
2650 KKASSERT(*ap->a_runp >= 0);
2658 * Write to a regular file. Because this is a strategy call the OS is
2659 * trying to actually get data onto the media.
2663 hammer_vop_strategy_write(struct vop_strategy_args *ap)
2665 hammer_record_t record;
2676 ip = ap->a_vp->v_data;
2679 blksize = hammer_blocksize(bio->bio_offset);
2680 KKASSERT(bp->b_bufsize == blksize);
2682 if (ip->flags & HAMMER_INODE_RO) {
2683 bp->b_error = EROFS;
2684 bp->b_flags |= B_ERROR;
2690 * Interlock with inode destruction (no in-kernel or directory
2691 * topology visibility). If we queue new IO while trying to
2692 * destroy the inode we can deadlock the vtrunc call in
2693 * hammer_inode_unloadable_check().
2695 * Besides, there's no point flushing a bp associated with an
2696 * inode that is being destroyed on-media and has no kernel
2699 if ((ip->flags | ip->sync_flags) &
2700 (HAMMER_INODE_DELETING|HAMMER_INODE_DELETED)) {
2707 * Reserve space and issue a direct-write from the front-end.
2708 * NOTE: The direct_io code will hammer_bread/bcopy smaller
2711 * An in-memory record will be installed to reference the storage
2712 * until the flusher can get to it.
2714 * Since we own the high level bio the front-end will not try to
2715 * do a direct-read until the write completes.
2717 * NOTE: The only time we do not reserve a full-sized buffers
2718 * worth of data is if the file is small. We do not try to
2719 * allocate a fragment (from the small-data zone) at the end of
2720 * an otherwise large file as this can lead to wildly separated
2723 KKASSERT((bio->bio_offset & HAMMER_BUFMASK) == 0);
2724 KKASSERT(bio->bio_offset < ip->ino_data.size);
2725 if (bio->bio_offset || ip->ino_data.size > HAMMER_BUFSIZE / 2)
2726 bytes = bp->b_bufsize;
2728 bytes = ((int)ip->ino_data.size + 15) & ~15;
2730 record = hammer_ip_add_bulk(ip, bio->bio_offset, bp->b_data,
2733 hammer_io_direct_write(hmp, record, bio);
2734 if (ip->rsv_recs > 1 && hmp->rsv_recs > hammer_limit_recs)
2735 hammer_flush_inode(ip, 0);
2737 bp->b_bio2.bio_offset = NOOFFSET;
2738 bp->b_error = error;
2739 bp->b_flags |= B_ERROR;
2746 * dounlink - disconnect a directory entry
2748 * XXX whiteout support not really in yet
2751 hammer_dounlink(hammer_transaction_t trans, struct nchandle *nch,
2752 struct vnode *dvp, struct ucred *cred,
2753 int flags, int isdir)
2755 struct namecache *ncp;
2758 struct hammer_cursor cursor;
2760 u_int32_t max_iterations;
2764 * Calculate the namekey and setup the key range for the scan. This
2765 * works kinda like a chained hash table where the lower 32 bits
2766 * of the namekey synthesize the chain.
2768 * The key range is inclusive of both key_beg and key_end.
2773 if (dip->flags & HAMMER_INODE_RO)
2776 namekey = hammer_directory_namekey(dip, ncp->nc_name, ncp->nc_nlen,
2779 hammer_init_cursor(trans, &cursor, &dip->cache[1], dip);
2780 cursor.key_beg.localization = dip->obj_localization +
2781 HAMMER_LOCALIZE_MISC;
2782 cursor.key_beg.obj_id = dip->obj_id;
2783 cursor.key_beg.key = namekey;
2784 cursor.key_beg.create_tid = 0;
2785 cursor.key_beg.delete_tid = 0;
2786 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
2787 cursor.key_beg.obj_type = 0;
2789 cursor.key_end = cursor.key_beg;
2790 cursor.key_end.key += max_iterations;
2791 cursor.asof = dip->obj_asof;
2792 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
2795 * Scan all matching records (the chain), locate the one matching
2796 * the requested path component. info->last_error contains the
2797 * error code on search termination and could be 0, ENOENT, or
2800 * The hammer_ip_*() functions merge in-memory records with on-disk
2801 * records for the purposes of the search.
2803 error = hammer_ip_first(&cursor);
2805 while (error == 0) {
2806 error = hammer_ip_resolve_data(&cursor);
2809 nlen = cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF;
2811 if (ncp->nc_nlen == nlen &&
2812 bcmp(ncp->nc_name, cursor.data->entry.name, nlen) == 0) {
2815 error = hammer_ip_next(&cursor);
2819 * If all is ok we have to get the inode so we can adjust nlinks.
2820 * To avoid a deadlock with the flusher we must release the inode
2821 * lock on the directory when acquiring the inode for the entry.
2823 * If the target is a directory, it must be empty.
2826 hammer_unlock(&cursor.ip->lock);
2827 ip = hammer_get_inode(trans, dip, cursor.data->entry.obj_id,
2829 cursor.data->entry.localization,
2831 hammer_lock_sh(&cursor.ip->lock);
2832 if (error == ENOENT) {
2833 kprintf("HAMMER: WARNING: Removing "
2834 "dirent w/missing inode \"%s\"\n"
2835 "\tobj_id = %016llx\n",
2837 (long long)cursor.data->entry.obj_id);
2842 * If isdir >= 0 we validate that the entry is or is not a
2843 * directory. If isdir < 0 we don't care.
2845 if (error == 0 && isdir >= 0 && ip) {
2847 ip->ino_data.obj_type != HAMMER_OBJTYPE_DIRECTORY) {
2849 } else if (isdir == 0 &&
2850 ip->ino_data.obj_type == HAMMER_OBJTYPE_DIRECTORY) {
2856 * If we are trying to remove a directory the directory must
2859 * The check directory code can loop and deadlock/retry. Our
2860 * own cursor's node locks must be released to avoid a 3-way
2861 * deadlock with the flusher if the check directory code
2864 * If any changes whatsoever have been made to the cursor
2865 * set EDEADLK and retry.
2867 if (error == 0 && ip && ip->ino_data.obj_type ==
2868 HAMMER_OBJTYPE_DIRECTORY) {
2869 hammer_unlock_cursor(&cursor);
2870 error = hammer_ip_check_directory_empty(trans, ip);
2871 hammer_lock_cursor(&cursor);
2872 if (cursor.flags & HAMMER_CURSOR_RETEST) {
2873 kprintf("HAMMER: Warning: avoided deadlock "
2881 * Delete the directory entry.
2883 * WARNING: hammer_ip_del_directory() may have to terminate
2884 * the cursor to avoid a deadlock. It is ok to call
2885 * hammer_done_cursor() twice.
2888 error = hammer_ip_del_directory(trans, &cursor,
2891 hammer_done_cursor(&cursor);
2893 cache_setunresolved(nch);
2894 cache_setvp(nch, NULL);
2897 hammer_knote(ip->vp, NOTE_DELETE);
2898 cache_inval_vp(ip->vp, CINV_DESTROY);
2902 hammer_rel_inode(ip, 0);
2904 hammer_done_cursor(&cursor);
2906 if (error == EDEADLK)
2912 /************************************************************************
2913 * FIFO AND SPECFS OPS *
2914 ************************************************************************
2919 hammer_vop_fifoclose (struct vop_close_args *ap)
2921 /* XXX update itimes */
2922 return (VOCALL(&fifo_vnode_vops, &ap->a_head));
2926 hammer_vop_fiforead (struct vop_read_args *ap)
2930 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
2931 /* XXX update access time */
2936 hammer_vop_fifowrite (struct vop_write_args *ap)
2940 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
2941 /* XXX update access time */
2947 hammer_vop_fifokqfilter(struct vop_kqfilter_args *ap)
2951 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
2953 error = hammer_vop_kqfilter(ap);
2958 hammer_vop_specclose (struct vop_close_args *ap)
2960 /* XXX update itimes */
2961 return (VOCALL(&spec_vnode_vops, &ap->a_head));
2965 hammer_vop_specread (struct vop_read_args *ap)
2967 /* XXX update access time */
2968 return (VOCALL(&spec_vnode_vops, &ap->a_head));
2972 hammer_vop_specwrite (struct vop_write_args *ap)
2974 /* XXX update last change time */
2975 return (VOCALL(&spec_vnode_vops, &ap->a_head));
2978 /************************************************************************
2980 ************************************************************************
2983 static void filt_hammerdetach(struct knote *kn);
2984 static int filt_hammerread(struct knote *kn, long hint);
2985 static int filt_hammerwrite(struct knote *kn, long hint);
2986 static int filt_hammervnode(struct knote *kn, long hint);
2988 static struct filterops hammerread_filtops =
2989 { 1, NULL, filt_hammerdetach, filt_hammerread };
2990 static struct filterops hammerwrite_filtops =
2991 { 1, NULL, filt_hammerdetach, filt_hammerwrite };
2992 static struct filterops hammervnode_filtops =
2993 { 1, NULL, filt_hammerdetach, filt_hammervnode };
2997 hammer_vop_kqfilter(struct vop_kqfilter_args *ap)
2999 struct vnode *vp = ap->a_vp;
3000 struct knote *kn = ap->a_kn;
3003 switch (kn->kn_filter) {
3005 kn->kn_fop = &hammerread_filtops;
3008 kn->kn_fop = &hammerwrite_filtops;
3011 kn->kn_fop = &hammervnode_filtops;
3017 kn->kn_hook = (caddr_t)vp;
3019 lwkt_gettoken(&ilock, &vp->v_pollinfo.vpi_token);
3020 SLIST_INSERT_HEAD(&vp->v_pollinfo.vpi_selinfo.si_note, kn, kn_selnext);
3021 lwkt_reltoken(&ilock);
3027 filt_hammerdetach(struct knote *kn)
3029 struct vnode *vp = (void *)kn->kn_hook;
3032 lwkt_gettoken(&ilock, &vp->v_pollinfo.vpi_token);
3033 SLIST_REMOVE(&vp->v_pollinfo.vpi_selinfo.si_note,
3034 kn, knote, kn_selnext);
3035 lwkt_reltoken(&ilock);
3039 filt_hammerread(struct knote *kn, long hint)
3041 struct vnode *vp = (void *)kn->kn_hook;
3042 hammer_inode_t ip = VTOI(vp);
3044 if (hint == NOTE_REVOKE) {
3045 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
3048 kn->kn_data = ip->ino_data.size - kn->kn_fp->f_offset;
3049 return (kn->kn_data != 0);
3053 filt_hammerwrite(struct knote *kn, long hint)
3055 if (hint == NOTE_REVOKE)
3056 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
3062 filt_hammervnode(struct knote *kn, long hint)
3064 if (kn->kn_sfflags & hint)
3065 kn->kn_fflags |= hint;
3066 if (hint == NOTE_REVOKE) {
3067 kn->kn_flags |= EV_EOF;
3070 return (kn->kn_fflags != 0);