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
35 #include <sys/mountctl.h>
36 #include <sys/namecache.h>
38 #include <vfs/fifofs/fifo.h>
45 static int hammer_vop_fsync(struct vop_fsync_args *);
46 static int hammer_vop_read(struct vop_read_args *);
47 static int hammer_vop_write(struct vop_write_args *);
48 static int hammer_vop_access(struct vop_access_args *);
49 static int hammer_vop_advlock(struct vop_advlock_args *);
50 static int hammer_vop_close(struct vop_close_args *);
51 static int hammer_vop_ncreate(struct vop_ncreate_args *);
52 static int hammer_vop_getattr(struct vop_getattr_args *);
53 static int hammer_vop_nresolve(struct vop_nresolve_args *);
54 static int hammer_vop_nlookupdotdot(struct vop_nlookupdotdot_args *);
55 static int hammer_vop_nlink(struct vop_nlink_args *);
56 static int hammer_vop_nmkdir(struct vop_nmkdir_args *);
57 static int hammer_vop_nmknod(struct vop_nmknod_args *);
58 static int hammer_vop_open(struct vop_open_args *);
59 static int hammer_vop_print(struct vop_print_args *);
60 static int hammer_vop_readdir(struct vop_readdir_args *);
61 static int hammer_vop_readlink(struct vop_readlink_args *);
62 static int hammer_vop_nremove(struct vop_nremove_args *);
63 static int hammer_vop_nrename(struct vop_nrename_args *);
64 static int hammer_vop_nrmdir(struct vop_nrmdir_args *);
65 static int hammer_vop_markatime(struct vop_markatime_args *);
66 static int hammer_vop_setattr(struct vop_setattr_args *);
67 static int hammer_vop_strategy(struct vop_strategy_args *);
68 static int hammer_vop_bmap(struct vop_bmap_args *ap);
69 static int hammer_vop_nsymlink(struct vop_nsymlink_args *);
70 static int hammer_vop_nwhiteout(struct vop_nwhiteout_args *);
71 static int hammer_vop_ioctl(struct vop_ioctl_args *);
72 static int hammer_vop_mountctl(struct vop_mountctl_args *);
73 static int hammer_vop_kqfilter (struct vop_kqfilter_args *);
75 static int hammer_vop_fifoclose (struct vop_close_args *);
76 static int hammer_vop_fiforead (struct vop_read_args *);
77 static int hammer_vop_fifowrite (struct vop_write_args *);
78 static int hammer_vop_fifokqfilter (struct vop_kqfilter_args *);
80 struct vop_ops hammer_vnode_vops = {
81 .vop_default = vop_defaultop,
82 .vop_fsync = hammer_vop_fsync,
83 .vop_getpages = vop_stdgetpages,
84 .vop_putpages = vop_stdputpages,
85 .vop_read = hammer_vop_read,
86 .vop_write = hammer_vop_write,
87 .vop_access = hammer_vop_access,
88 .vop_advlock = hammer_vop_advlock,
89 .vop_close = hammer_vop_close,
90 .vop_ncreate = hammer_vop_ncreate,
91 .vop_getattr = hammer_vop_getattr,
92 .vop_inactive = hammer_vop_inactive,
93 .vop_reclaim = hammer_vop_reclaim,
94 .vop_nresolve = hammer_vop_nresolve,
95 .vop_nlookupdotdot = hammer_vop_nlookupdotdot,
96 .vop_nlink = hammer_vop_nlink,
97 .vop_nmkdir = hammer_vop_nmkdir,
98 .vop_nmknod = hammer_vop_nmknod,
99 .vop_open = hammer_vop_open,
100 .vop_pathconf = vop_stdpathconf,
101 .vop_print = hammer_vop_print,
102 .vop_readdir = hammer_vop_readdir,
103 .vop_readlink = hammer_vop_readlink,
104 .vop_nremove = hammer_vop_nremove,
105 .vop_nrename = hammer_vop_nrename,
106 .vop_nrmdir = hammer_vop_nrmdir,
107 .vop_markatime = hammer_vop_markatime,
108 .vop_setattr = hammer_vop_setattr,
109 .vop_bmap = hammer_vop_bmap,
110 .vop_strategy = hammer_vop_strategy,
111 .vop_nsymlink = hammer_vop_nsymlink,
112 .vop_nwhiteout = hammer_vop_nwhiteout,
113 .vop_ioctl = hammer_vop_ioctl,
114 .vop_mountctl = hammer_vop_mountctl,
115 .vop_kqfilter = hammer_vop_kqfilter
118 struct vop_ops hammer_spec_vops = {
119 .vop_default = vop_defaultop,
120 .vop_fsync = hammer_vop_fsync,
121 .vop_read = vop_stdnoread,
122 .vop_write = vop_stdnowrite,
123 .vop_access = hammer_vop_access,
124 .vop_close = hammer_vop_close,
125 .vop_markatime = hammer_vop_markatime,
126 .vop_getattr = hammer_vop_getattr,
127 .vop_inactive = hammer_vop_inactive,
128 .vop_reclaim = hammer_vop_reclaim,
129 .vop_setattr = hammer_vop_setattr
132 struct vop_ops hammer_fifo_vops = {
133 .vop_default = fifo_vnoperate,
134 .vop_fsync = hammer_vop_fsync,
135 .vop_read = hammer_vop_fiforead,
136 .vop_write = hammer_vop_fifowrite,
137 .vop_access = hammer_vop_access,
138 .vop_close = hammer_vop_fifoclose,
139 .vop_markatime = hammer_vop_markatime,
140 .vop_getattr = hammer_vop_getattr,
141 .vop_inactive = hammer_vop_inactive,
142 .vop_reclaim = hammer_vop_reclaim,
143 .vop_setattr = hammer_vop_setattr,
144 .vop_kqfilter = hammer_vop_fifokqfilter
149 hammer_knote(struct vnode *vp, int flags)
152 KNOTE(&vp->v_pollinfo.vpi_kqinfo.ki_note, flags);
155 static int hammer_dounlink(hammer_transaction_t trans, struct nchandle *nch,
156 struct vnode *dvp, struct ucred *cred,
157 int flags, int isdir);
158 static int hammer_vop_strategy_read(struct vop_strategy_args *ap);
159 static int hammer_vop_strategy_write(struct vop_strategy_args *ap);
162 * hammer_vop_fsync { vp, waitfor }
164 * fsync() an inode to disk and wait for it to be completely committed
165 * such that the information would not be undone if a crash occured after
168 * NOTE: HAMMER's fsync()'s are going to remain expensive until we implement
169 * a REDO log. A sysctl is provided to relax HAMMER's fsync()
172 * Ultimately the combination of a REDO log and use of fast storage
173 * to front-end cluster caches will make fsync fast, but it aint
174 * here yet. And, in anycase, we need real transactional
175 * all-or-nothing features which are not restricted to a single file.
179 hammer_vop_fsync(struct vop_fsync_args *ap)
181 hammer_inode_t ip = VTOI(ap->a_vp);
182 hammer_mount_t hmp = ip->hmp;
183 int waitfor = ap->a_waitfor;
186 lwkt_gettoken(&hmp->fs_token);
189 * Fsync rule relaxation (default is either full synchronous flush
190 * or REDO semantics with synchronous flush).
192 if (ap->a_flags & VOP_FSYNC_SYSCALL) {
193 switch(hammer_fsync_mode) {
196 /* no REDO, full synchronous flush */
200 /* no REDO, full asynchronous flush */
201 if (waitfor == MNT_WAIT)
202 waitfor = MNT_NOWAIT;
205 /* REDO semantics, synchronous flush */
206 if (hmp->version < HAMMER_VOL_VERSION_FOUR)
208 mode = HAMMER_FLUSH_UNDOS_AUTO;
211 /* REDO semantics, relaxed asynchronous flush */
212 if (hmp->version < HAMMER_VOL_VERSION_FOUR)
214 mode = HAMMER_FLUSH_UNDOS_RELAXED;
215 if (waitfor == MNT_WAIT)
216 waitfor = MNT_NOWAIT;
219 /* ignore the fsync() system call */
220 lwkt_reltoken(&hmp->fs_token);
223 /* we have to do something */
224 mode = HAMMER_FLUSH_UNDOS_RELAXED;
225 if (waitfor == MNT_WAIT)
226 waitfor = MNT_NOWAIT;
231 * Fast fsync only needs to flush the UNDO/REDO fifo if
232 * HAMMER_INODE_REDO is non-zero and the only modifications
233 * made to the file are write or write-extends.
235 if ((ip->flags & HAMMER_INODE_REDO) &&
236 (ip->flags & HAMMER_INODE_MODMASK_NOREDO) == 0) {
237 ++hammer_count_fsyncs;
238 hammer_flusher_flush_undos(hmp, mode);
240 if (ip->vp && (ip->flags & HAMMER_INODE_MODMASK) == 0)
242 lwkt_reltoken(&hmp->fs_token);
247 * REDO is enabled by fsync(), the idea being we really only
248 * want to lay down REDO records when programs are using
249 * fsync() heavily. The first fsync() on the file starts
250 * the gravy train going and later fsync()s keep it hot by
251 * resetting the redo_count.
253 * We weren't running REDOs before now so we have to fall
254 * through and do a full fsync of what we have.
256 if (hmp->version >= HAMMER_VOL_VERSION_FOUR &&
257 (hmp->flags & HAMMER_MOUNT_REDO_RECOVERY_RUN) == 0) {
258 ip->flags |= HAMMER_INODE_REDO;
265 * Do a full flush sequence.
267 * Attempt to release the vnode while waiting for the inode to
268 * finish flushing. This can really mess up inactive->reclaim
269 * sequences so only do it if the vnode is active.
271 * WARNING! The VX lock functions must be used. vn_lock() will
272 * fail when this is part of a VOP_RECLAIM sequence.
274 ++hammer_count_fsyncs;
275 vfsync(ap->a_vp, waitfor, 1, NULL, NULL);
276 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
277 if (waitfor == MNT_WAIT) {
280 if ((ap->a_vp->v_flag & VRECLAIMED) == 0) {
286 hammer_wait_inode(ip);
290 if (ip->vp && (ip->flags & HAMMER_INODE_MODMASK) == 0)
292 lwkt_reltoken(&hmp->fs_token);
297 * hammer_vop_read { vp, uio, ioflag, cred }
299 * MPSAFE (for the cache safe does not require fs_token)
303 hammer_vop_read(struct vop_read_args *ap)
305 struct hammer_transaction trans;
320 if (ap->a_vp->v_type != VREG)
329 * Attempt to shortcut directly to the VM object using lwbufs.
330 * This is much faster than instantiating buffer cache buffers.
332 resid = uio->uio_resid;
333 error = vop_helper_read_shortcut(ap);
334 hammer_stats_file_read += resid - uio->uio_resid;
337 if (uio->uio_resid == 0)
341 * Allow the UIO's size to override the sequential heuristic.
343 blksize = hammer_blocksize(uio->uio_offset);
344 seqcount = (uio->uio_resid + (MAXBSIZE - 1)) / MAXBSIZE;
345 ioseqcount = (ap->a_ioflag >> 16);
346 if (seqcount < ioseqcount)
347 seqcount = ioseqcount;
350 * If reading or writing a huge amount of data we have to break
351 * atomicy and allow the operation to be interrupted by a signal
352 * or it can DOS the machine.
354 bigread = (uio->uio_resid > 100 * 1024 * 1024);
357 * Access the data typically in HAMMER_BUFSIZE blocks via the
358 * buffer cache, but HAMMER may use a variable block size based
361 * XXX Temporary hack, delay the start transaction while we remain
362 * MPSAFE. NOTE: ino_data.size cannot change while vnode is
365 while (uio->uio_resid > 0 && uio->uio_offset < ip->ino_data.size) {
369 blksize = hammer_blocksize(uio->uio_offset);
370 offset = (int)uio->uio_offset & (blksize - 1);
371 base_offset = uio->uio_offset - offset;
373 if (bigread && (error = hammer_signal_check(ip->hmp)) != 0)
379 bp = getblk(ap->a_vp, base_offset, blksize, 0, 0);
380 if ((bp->b_flags & (B_INVAL | B_CACHE | B_RAM)) == B_CACHE) {
381 bp->b_flags &= ~B_AGE;
385 if (ap->a_ioflag & IO_NRDELAY) {
387 return (EWOULDBLOCK);
393 if (got_trans == 0) {
394 hammer_start_transaction(&trans, ip->hmp);
399 * NOTE: A valid bp has already been acquired, but was not
402 if (hammer_cluster_enable) {
404 * Use file_limit to prevent cluster_read() from
405 * creating buffers of the wrong block size past
408 file_limit = ip->ino_data.size;
409 if (base_offset < HAMMER_XDEMARC &&
410 file_limit > HAMMER_XDEMARC) {
411 file_limit = HAMMER_XDEMARC;
413 error = cluster_readx(ap->a_vp,
414 file_limit, base_offset,
420 error = breadnx(ap->a_vp, base_offset,
429 if ((hammer_debug_io & 0x0001) && (bp->b_flags & B_IOISSUED)) {
430 hdkprintf("zone2_offset %016jx read file %016jx@%016jx\n",
431 (intmax_t)bp->b_bio2.bio_offset,
432 (intmax_t)ip->obj_id,
433 (intmax_t)bp->b_loffset);
435 bp->b_flags &= ~B_IOISSUED;
436 if (blksize == HAMMER_XBUFSIZE)
437 bp->b_flags |= B_CLUSTEROK;
439 n = blksize - offset;
440 if (n > uio->uio_resid)
442 if (n > ip->ino_data.size - uio->uio_offset)
443 n = (int)(ip->ino_data.size - uio->uio_offset);
446 * Set B_AGE, data has a lower priority than meta-data.
448 * Use a hold/unlock/drop sequence to run the uiomove
449 * with the buffer unlocked, avoiding deadlocks against
450 * read()s on mmap()'d spaces.
452 bp->b_flags |= B_AGE;
453 error = uiomovebp(bp, (char *)bp->b_data + offset, n, uio);
458 hammer_stats_file_read += n;
464 * Try to update the atime with just the inode lock for maximum
465 * concurrency. If we can't shortcut it we have to get the full
468 if (got_trans == 0 && hammer_update_atime_quick(ip) < 0) {
469 hammer_start_transaction(&trans, ip->hmp);
474 if ((ip->flags & HAMMER_INODE_RO) == 0 &&
475 (ip->hmp->mp->mnt_flag & MNT_NOATIME) == 0) {
476 lwkt_gettoken(&hmp->fs_token);
477 ip->ino_data.atime = trans.time;
478 hammer_modify_inode(&trans, ip, HAMMER_INODE_ATIME);
479 hammer_done_transaction(&trans);
480 lwkt_reltoken(&hmp->fs_token);
482 hammer_done_transaction(&trans);
489 * hammer_vop_write { vp, uio, ioflag, cred }
493 hammer_vop_write(struct vop_write_args *ap)
495 struct hammer_transaction trans;
513 if (vp->v_type != VREG)
519 seqcount = ap->a_ioflag >> 16;
521 if (ip->flags & HAMMER_INODE_RO)
525 * Create a transaction to cover the operations we perform.
527 hammer_start_transaction(&trans, hmp);
531 * Use v_lastwrite_ts if file not open for writing
532 * (i.e. a late msync)
534 if (vp->v_writecount == 0) {
535 trans.time = vp->v_lastwrite_ts.tv_sec * 1000000 +
536 vp->v_lastwrite_ts.tv_nsec / 1000;
543 if (ap->a_ioflag & IO_APPEND)
544 uio->uio_offset = ip->ino_data.size;
547 * Check for illegal write offsets. Valid range is 0...2^63-1.
549 * NOTE: the base_off assignment is required to work around what
550 * I consider to be a GCC-4 optimization bug.
552 if (uio->uio_offset < 0) {
553 hammer_done_transaction(&trans);
556 base_offset = uio->uio_offset + uio->uio_resid; /* work around gcc-4 */
557 if (uio->uio_resid > 0 && base_offset <= uio->uio_offset) {
558 hammer_done_transaction(&trans);
562 if (uio->uio_resid > 0 && (td = uio->uio_td) != NULL && td->td_proc &&
563 base_offset > td->td_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
564 hammer_done_transaction(&trans);
565 lwpsignal(td->td_proc, td->td_lwp, SIGXFSZ);
570 * If reading or writing a huge amount of data we have to break
571 * atomicy and allow the operation to be interrupted by a signal
572 * or it can DOS the machine.
574 * Preset redo_count so we stop generating REDOs earlier if the
577 * redo_count is heuristical, SMP races are ok
579 bigwrite = (uio->uio_resid > 100 * 1024 * 1024);
580 if ((ip->flags & HAMMER_INODE_REDO) &&
581 ip->redo_count < hammer_limit_redo) {
582 ip->redo_count += uio->uio_resid;
586 * Access the data typically in HAMMER_BUFSIZE blocks via the
587 * buffer cache, but HAMMER may use a variable block size based
590 while (uio->uio_resid > 0) {
598 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_WRITE)) != 0)
600 if (bigwrite && (error = hammer_signal_check(hmp)) != 0)
603 blksize = hammer_blocksize(uio->uio_offset);
606 * Control the number of pending records associated with
607 * this inode. If too many have accumulated start a
608 * flush. Try to maintain a pipeline with the flusher.
610 * NOTE: It is possible for other sources to grow the
611 * records but not necessarily issue another flush,
612 * so use a timeout and ensure that a re-flush occurs.
614 if (ip->rsv_recs >= hammer_limit_inode_recs) {
615 lwkt_gettoken(&hmp->fs_token);
616 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
617 while (ip->rsv_recs >= hammer_limit_inode_recs * 2) {
618 ip->flags |= HAMMER_INODE_RECSW;
619 tsleep(&ip->rsv_recs, 0, "hmrwww", hz);
620 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
622 lwkt_reltoken(&hmp->fs_token);
626 * Do not allow HAMMER to blow out the buffer cache. Very
627 * large UIOs can lockout other processes due to bwillwrite()
630 * The hammer inode is not locked during these operations.
631 * The vnode is locked which can interfere with the pageout
632 * daemon for non-UIO_NOCOPY writes but should not interfere
633 * with the buffer cache. Even so, we cannot afford to
634 * allow the pageout daemon to build up too many dirty buffer
637 * Only call this if we aren't being recursively called from
638 * a virtual disk device (vn), else we may deadlock.
640 if ((ap->a_ioflag & IO_RECURSE) == 0)
644 * Calculate the blocksize at the current offset and figure
645 * out how much we can actually write.
647 blkmask = blksize - 1;
648 offset = (int)uio->uio_offset & blkmask;
649 base_offset = uio->uio_offset & ~(int64_t)blkmask;
650 n = blksize - offset;
651 if (n > uio->uio_resid) {
657 nsize = uio->uio_offset + n;
658 if (nsize > ip->ino_data.size) {
659 if (uio->uio_offset > ip->ino_data.size)
663 nvextendbuf(ap->a_vp,
666 hammer_blocksize(ip->ino_data.size),
667 hammer_blocksize(nsize),
668 hammer_blockoff(ip->ino_data.size),
669 hammer_blockoff(nsize),
672 kflags |= NOTE_EXTEND;
675 if (uio->uio_segflg == UIO_NOCOPY) {
677 * Issuing a write with the same data backing the
678 * buffer. Instantiate the buffer to collect the
679 * backing vm pages, then read-in any missing bits.
681 * This case is used by vop_stdputpages().
683 bp = getblk(ap->a_vp, base_offset,
684 blksize, GETBLK_BHEAVY, 0);
685 if ((bp->b_flags & B_CACHE) == 0) {
687 error = bread(ap->a_vp, base_offset,
690 } else if (offset == 0 && uio->uio_resid >= blksize) {
692 * Even though we are entirely overwriting the buffer
693 * we may still have to zero it out to avoid a
694 * mmap/write visibility issue.
696 bp = getblk(ap->a_vp, base_offset, blksize, GETBLK_BHEAVY, 0);
697 if ((bp->b_flags & B_CACHE) == 0)
699 } else if (base_offset >= ip->ino_data.size) {
701 * If the base offset of the buffer is beyond the
702 * file EOF, we don't have to issue a read.
704 bp = getblk(ap->a_vp, base_offset,
705 blksize, GETBLK_BHEAVY, 0);
709 * Partial overwrite, read in any missing bits then
710 * replace the portion being written.
712 error = bread(ap->a_vp, base_offset, blksize, &bp);
717 error = uiomovebp(bp, bp->b_data + offset, n, uio);
719 lwkt_gettoken(&hmp->fs_token);
722 * Generate REDO records if enabled and redo_count will not
723 * exceeded the limit.
725 * If redo_count exceeds the limit we stop generating records
726 * and clear HAMMER_INODE_REDO. This will cause the next
727 * fsync() to do a full meta-data sync instead of just an
728 * UNDO/REDO fifo update.
730 * When clearing HAMMER_INODE_REDO any pre-existing REDOs
731 * will still be tracked. The tracks will be terminated
732 * when the related meta-data (including possible data
733 * modifications which are not tracked via REDO) is
736 if ((ip->flags & HAMMER_INODE_REDO) && error == 0) {
737 if (ip->redo_count < hammer_limit_redo) {
738 bp->b_flags |= B_VFSFLAG1;
739 error = hammer_generate_redo(&trans, ip,
740 base_offset + offset,
745 ip->flags &= ~HAMMER_INODE_REDO;
750 * If we screwed up we have to undo any VM size changes we
756 nvtruncbuf(ap->a_vp, ip->ino_data.size,
757 hammer_blocksize(ip->ino_data.size),
758 hammer_blockoff(ip->ino_data.size),
761 lwkt_reltoken(&hmp->fs_token);
764 kflags |= NOTE_WRITE;
765 hammer_stats_file_write += n;
766 if (blksize == HAMMER_XBUFSIZE)
767 bp->b_flags |= B_CLUSTEROK;
768 if (ip->ino_data.size < uio->uio_offset) {
769 ip->ino_data.size = uio->uio_offset;
770 flags = HAMMER_INODE_SDIRTY;
774 ip->ino_data.mtime = trans.time;
775 flags |= HAMMER_INODE_MTIME | HAMMER_INODE_BUFS;
776 hammer_modify_inode(&trans, ip, flags);
779 * Once we dirty the buffer any cached zone-X offset
780 * becomes invalid. HAMMER NOTE: no-history mode cannot
781 * allow overwriting over the same data sector unless
782 * we provide UNDOs for the old data, which we don't.
784 bp->b_bio2.bio_offset = NOOFFSET;
786 lwkt_reltoken(&hmp->fs_token);
789 * Final buffer disposition.
791 * Because meta-data updates are deferred, HAMMER is
792 * especially sensitive to excessive bdwrite()s because
793 * the I/O stream is not broken up by disk reads. So the
794 * buffer cache simply cannot keep up.
796 * WARNING! blksize is variable. cluster_write() is
797 * expected to not blow up if it encounters
798 * buffers that do not match the passed blksize.
800 * NOTE! Hammer shouldn't need to bawrite()/cluster_write().
801 * The ip->rsv_recs check should burst-flush the data.
802 * If we queue it immediately the buf could be left
803 * locked on the device queue for a very long time.
805 * However, failing to flush a dirty buffer out when
806 * issued from the pageout daemon can result in a low
807 * memory deadlock against bio_page_alloc(), so we
808 * have to bawrite() on IO_ASYNC as well.
810 * NOTE! To avoid degenerate stalls due to mismatched block
811 * sizes we only honor IO_DIRECT on the write which
812 * abuts the end of the buffer. However, we must
813 * honor IO_SYNC in case someone is silly enough to
814 * configure a HAMMER file as swap, or when HAMMER
815 * is serving NFS (for commits). Ick ick.
817 bp->b_flags |= B_AGE;
818 if (blksize == HAMMER_XBUFSIZE)
819 bp->b_flags |= B_CLUSTEROK;
821 if (ap->a_ioflag & IO_SYNC) {
823 } else if ((ap->a_ioflag & IO_DIRECT) && endofblk) {
825 } else if (ap->a_ioflag & IO_ASYNC) {
827 } else if (hammer_cluster_enable &&
828 !(ap->a_vp->v_mount->mnt_flag & MNT_NOCLUSTERW)) {
829 if (base_offset < HAMMER_XDEMARC)
830 cluster_eof = hammer_blockdemarc(base_offset,
833 cluster_eof = ip->ino_data.size;
834 cluster_write(bp, cluster_eof, blksize, seqcount);
839 hammer_done_transaction(&trans);
840 hammer_knote(ap->a_vp, kflags);
846 * hammer_vop_access { vp, mode, cred }
848 * MPSAFE - does not require fs_token
852 hammer_vop_access(struct vop_access_args *ap)
854 hammer_inode_t ip = VTOI(ap->a_vp);
859 uid = hammer_to_unix_xid(&ip->ino_data.uid);
860 gid = hammer_to_unix_xid(&ip->ino_data.gid);
862 error = vop_helper_access(ap, uid, gid, ip->ino_data.mode,
863 ip->ino_data.uflags);
868 * hammer_vop_advlock { vp, id, op, fl, flags }
870 * MPSAFE - does not require fs_token
874 hammer_vop_advlock(struct vop_advlock_args *ap)
876 hammer_inode_t ip = VTOI(ap->a_vp);
878 return (lf_advlock(ap, &ip->advlock, ip->ino_data.size));
882 * hammer_vop_close { vp, fflag }
884 * We can only sync-on-close for normal closes. XXX disabled for now.
888 hammer_vop_close(struct vop_close_args *ap)
891 struct vnode *vp = ap->a_vp;
892 hammer_inode_t ip = VTOI(vp);
894 if (ip->flags & (HAMMER_INODE_CLOSESYNC|HAMMER_INODE_CLOSEASYNC)) {
895 if (vn_islocked(vp) == LK_EXCLUSIVE &&
896 (vp->v_flag & (VINACTIVE|VRECLAIMED)) == 0) {
897 if (ip->flags & HAMMER_INODE_CLOSESYNC)
900 waitfor = MNT_NOWAIT;
901 ip->flags &= ~(HAMMER_INODE_CLOSESYNC |
902 HAMMER_INODE_CLOSEASYNC);
903 VOP_FSYNC(vp, MNT_NOWAIT, waitfor);
907 return (vop_stdclose(ap));
911 * hammer_vop_ncreate { nch, dvp, vpp, cred, vap }
913 * The operating system has already ensured that the directory entry
914 * does not exist and done all appropriate namespace locking.
918 hammer_vop_ncreate(struct vop_ncreate_args *ap)
920 struct hammer_transaction trans;
923 struct nchandle *nch;
928 dip = VTOI(ap->a_dvp);
931 if (dip->flags & HAMMER_INODE_RO)
933 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0)
937 * Create a transaction to cover the operations we perform.
939 lwkt_gettoken(&hmp->fs_token);
940 hammer_start_transaction(&trans, hmp);
943 * Create a new filesystem object of the requested type. The
944 * returned inode will be referenced and shared-locked to prevent
945 * it from being moved to the flusher.
947 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
948 dip, nch->ncp->nc_name, nch->ncp->nc_nlen,
951 hkprintf("hammer_create_inode error %d\n", error);
952 hammer_done_transaction(&trans);
954 lwkt_reltoken(&hmp->fs_token);
959 * Add the new filesystem object to the directory. This will also
960 * bump the inode's link count.
962 error = hammer_ip_add_direntry(&trans, dip,
963 nch->ncp->nc_name, nch->ncp->nc_nlen,
966 hkprintf("hammer_ip_add_direntry error %d\n", error);
972 hammer_rel_inode(nip, 0);
973 hammer_done_transaction(&trans);
976 error = hammer_get_vnode(nip, ap->a_vpp);
977 hammer_done_transaction(&trans);
978 hammer_rel_inode(nip, 0);
980 cache_setunresolved(ap->a_nch);
981 cache_setvp(ap->a_nch, *ap->a_vpp);
983 hammer_knote(ap->a_dvp, NOTE_WRITE);
985 lwkt_reltoken(&hmp->fs_token);
990 * hammer_vop_getattr { vp, vap }
992 * Retrieve an inode's attribute information. When accessing inodes
993 * historically we fake the atime field to ensure consistent results.
994 * The atime field is stored in the B-Tree element and allowed to be
995 * updated without cycling the element.
997 * MPSAFE - does not require fs_token
1001 hammer_vop_getattr(struct vop_getattr_args *ap)
1003 hammer_inode_t ip = VTOI(ap->a_vp);
1004 struct vattr *vap = ap->a_vap;
1007 * We want the fsid to be different when accessing a filesystem
1008 * with different as-of's so programs like diff don't think
1009 * the files are the same.
1011 * We also want the fsid to be the same when comparing snapshots,
1012 * or when comparing mirrors (which might be backed by different
1013 * physical devices). HAMMER fsids are based on the PFS's
1014 * shared_uuid field.
1016 * XXX there is a chance of collision here. The va_fsid reported
1017 * by stat is different from the more involved fsid used in the
1020 hammer_lock_sh(&ip->lock);
1021 vap->va_fsid = ip->pfsm->fsid_udev ^ (uint32_t)ip->obj_asof ^
1022 (uint32_t)(ip->obj_asof >> 32);
1024 vap->va_fileid = ip->ino_leaf.base.obj_id;
1025 vap->va_mode = ip->ino_data.mode;
1026 vap->va_nlink = ip->ino_data.nlinks;
1027 vap->va_uid = hammer_to_unix_xid(&ip->ino_data.uid);
1028 vap->va_gid = hammer_to_unix_xid(&ip->ino_data.gid);
1031 vap->va_size = ip->ino_data.size;
1034 * Special case for @@PFS softlinks. The actual size of the
1035 * expanded softlink is "@@0x%016llx:%05d" == 26 bytes.
1036 * or for MAX_TID is "@@-1:%05d" == 10 bytes.
1038 * Note that userspace hammer command does not allow users to
1039 * create a @@PFS softlink under an existing other PFS (id!=0)
1040 * so the ip localization here for @@PFS softlink is always 0.
1042 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_SOFTLINK &&
1043 ip->ino_data.size == 10 &&
1044 ip->obj_asof == HAMMER_MAX_TID &&
1045 ip->obj_localization == HAMMER_DEF_LOCALIZATION &&
1046 strncmp(ip->ino_data.ext.symlink, "@@PFS", 5) == 0) {
1047 if (hammer_is_pfs_slave(&ip->pfsm->pfsd))
1054 * We must provide a consistent atime and mtime for snapshots
1055 * so people can do a 'tar cf - ... | md5' on them and get
1056 * consistent results.
1058 if (ip->flags & HAMMER_INODE_RO) {
1059 hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_atime);
1060 hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_mtime);
1062 hammer_time_to_timespec(ip->ino_data.atime, &vap->va_atime);
1063 hammer_time_to_timespec(ip->ino_data.mtime, &vap->va_mtime);
1065 hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_ctime);
1066 vap->va_flags = ip->ino_data.uflags;
1067 vap->va_gen = 1; /* hammer inums are unique for all time */
1068 vap->va_blocksize = HAMMER_BUFSIZE;
1069 if (ip->ino_data.size >= HAMMER_XDEMARC) {
1070 vap->va_bytes = HAMMER_XBUFSIZE64_DOALIGN(ip->ino_data.size);
1071 } else if (ip->ino_data.size > HAMMER_HBUFSIZE) {
1072 vap->va_bytes = HAMMER_BUFSIZE64_DOALIGN(ip->ino_data.size);
1074 vap->va_bytes = HAMMER_DATA_DOALIGN(ip->ino_data.size);
1077 vap->va_type = hammer_get_vnode_type(ip->ino_data.obj_type);
1078 vap->va_filerev = 0; /* XXX */
1079 vap->va_uid_uuid = ip->ino_data.uid;
1080 vap->va_gid_uuid = ip->ino_data.gid;
1081 vap->va_fsid_uuid = ip->hmp->fsid;
1082 vap->va_vaflags = VA_UID_UUID_VALID | VA_GID_UUID_VALID |
1085 switch (ip->ino_data.obj_type) {
1086 case HAMMER_OBJTYPE_CDEV:
1087 case HAMMER_OBJTYPE_BDEV:
1088 vap->va_rmajor = ip->ino_data.rmajor;
1089 vap->va_rminor = ip->ino_data.rminor;
1094 hammer_unlock(&ip->lock);
1099 * hammer_vop_nresolve { nch, dvp, cred }
1101 * Locate the requested directory entry.
1105 hammer_vop_nresolve(struct vop_nresolve_args *ap)
1107 struct hammer_transaction trans;
1108 struct namecache *ncp;
1113 struct hammer_cursor cursor;
1122 uint32_t localization;
1123 uint32_t max_iterations;
1126 * Misc initialization, plus handle as-of name extensions. Look for
1127 * the '@@' extension. Note that as-of files and directories cannot
1130 dip = VTOI(ap->a_dvp);
1131 ncp = ap->a_nch->ncp;
1132 asof = dip->obj_asof;
1133 localization = dip->obj_localization; /* for code consistency */
1134 nlen = ncp->nc_nlen;
1135 flags = dip->flags & HAMMER_INODE_RO;
1139 lwkt_gettoken(&hmp->fs_token);
1140 hammer_simple_transaction(&trans, hmp);
1142 for (i = 0; i < nlen; ++i) {
1143 if (ncp->nc_name[i] == '@' && ncp->nc_name[i+1] == '@') {
1144 error = hammer_str_to_tid(ncp->nc_name + i + 2,
1145 &ispfs, &asof, &localization);
1150 if (asof != HAMMER_MAX_TID)
1151 flags |= HAMMER_INODE_RO;
1158 * If this is a PFS we dive into the PFS root inode
1160 if (ispfs && nlen == 0) {
1161 ip = hammer_get_inode(&trans, dip, HAMMER_OBJID_ROOT,
1165 error = hammer_get_vnode(ip, &vp);
1166 hammer_rel_inode(ip, 0);
1172 cache_setvp(ap->a_nch, vp);
1179 * If there is no path component the time extension is relative to dip.
1180 * e.g. "fubar/@@<snapshot>"
1182 * "." is handled by the kernel, but ".@@<snapshot>" is not.
1183 * e.g. "fubar/.@@<snapshot>"
1185 * ".." is handled by the kernel. We do not currently handle
1188 if (nlen == 0 || (nlen == 1 && ncp->nc_name[0] == '.')) {
1189 ip = hammer_get_inode(&trans, dip, dip->obj_id,
1190 asof, dip->obj_localization,
1193 error = hammer_get_vnode(ip, &vp);
1194 hammer_rel_inode(ip, 0);
1200 cache_setvp(ap->a_nch, vp);
1207 * Calculate the namekey and setup the key range for the scan. This
1208 * works kinda like a chained hash table where the lower 32 bits
1209 * of the namekey synthesize the chain.
1211 * The key range is inclusive of both key_beg and key_end.
1213 namekey = hammer_direntry_namekey(dip, ncp->nc_name, nlen,
1216 error = hammer_init_cursor(&trans, &cursor, &dip->cache[1], dip);
1217 cursor.key_beg.localization = dip->obj_localization |
1218 hammer_dir_localization(dip);
1219 cursor.key_beg.obj_id = dip->obj_id;
1220 cursor.key_beg.key = namekey;
1221 cursor.key_beg.create_tid = 0;
1222 cursor.key_beg.delete_tid = 0;
1223 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1224 cursor.key_beg.obj_type = 0;
1226 cursor.key_end = cursor.key_beg;
1227 cursor.key_end.key += max_iterations;
1229 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
1232 * Scan all matching records (the chain), locate the one matching
1233 * the requested path component.
1235 * The hammer_ip_*() functions merge in-memory records with on-disk
1236 * records for the purposes of the search.
1239 localization = HAMMER_DEF_LOCALIZATION;
1242 error = hammer_ip_first(&cursor);
1243 while (error == 0) {
1244 error = hammer_ip_resolve_data(&cursor);
1247 if (nlen == cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF &&
1248 bcmp(ncp->nc_name, cursor.data->entry.name, nlen) == 0) {
1249 obj_id = cursor.data->entry.obj_id;
1250 localization = cursor.data->entry.localization;
1253 error = hammer_ip_next(&cursor);
1256 hammer_done_cursor(&cursor);
1259 * Lookup the obj_id. This should always succeed. If it does not
1260 * the filesystem may be damaged and we return a dummy inode.
1263 ip = hammer_get_inode(&trans, dip, obj_id,
1266 if (error == ENOENT) {
1267 hkprintf("WARNING: Missing inode for dirent \"%s\"\n"
1268 "\tobj_id = %016jx, asof=%016jx, lo=%08x\n",
1270 (intmax_t)obj_id, (intmax_t)asof,
1273 ip = hammer_get_dummy_inode(&trans, dip, obj_id,
1278 error = hammer_get_vnode(ip, &vp);
1279 hammer_rel_inode(ip, 0);
1285 cache_setvp(ap->a_nch, vp);
1288 } else if (error == ENOENT) {
1289 cache_setvp(ap->a_nch, NULL);
1292 hammer_done_transaction(&trans);
1293 lwkt_reltoken(&hmp->fs_token);
1298 * hammer_vop_nlookupdotdot { dvp, vpp, cred }
1300 * Locate the parent directory of a directory vnode.
1302 * dvp is referenced but not locked. *vpp must be returned referenced and
1303 * locked. A parent_obj_id of 0 indicates that we are at the root.
1305 * NOTE: as-of sequences are not linked into the directory structure. If
1306 * we are at the root with a different asof then the mount point, reload
1307 * the same directory with the mount point's asof. I'm not sure what this
1308 * will do to NFS. We encode ASOF stamps in NFS file handles so it might not
1309 * get confused, but it hasn't been tested.
1313 hammer_vop_nlookupdotdot(struct vop_nlookupdotdot_args *ap)
1315 struct hammer_transaction trans;
1319 int64_t parent_obj_id;
1320 uint32_t parent_obj_localization;
1324 dip = VTOI(ap->a_dvp);
1325 asof = dip->obj_asof;
1329 * Whos are parent? This could be the root of a pseudo-filesystem
1330 * whos parent is in another localization domain.
1332 lwkt_gettoken(&hmp->fs_token);
1333 parent_obj_id = dip->ino_data.parent_obj_id;
1334 if (dip->obj_id == HAMMER_OBJID_ROOT)
1335 parent_obj_localization = HAMMER_DEF_LOCALIZATION;
1337 parent_obj_localization = dip->obj_localization;
1340 * It's probably a PFS root when dip->ino_data.parent_obj_id is 0.
1342 if (parent_obj_id == 0) {
1343 if (dip->obj_id == HAMMER_OBJID_ROOT &&
1344 asof != hmp->asof) {
1345 parent_obj_id = dip->obj_id;
1347 *ap->a_fakename = kmalloc(19, M_TEMP, M_WAITOK);
1348 ksnprintf(*ap->a_fakename, 19, "0x%016jx",
1349 (intmax_t)dip->obj_asof);
1352 lwkt_reltoken(&hmp->fs_token);
1357 hammer_simple_transaction(&trans, hmp);
1359 ip = hammer_get_inode(&trans, dip, parent_obj_id,
1360 asof, parent_obj_localization,
1361 dip->flags, &error);
1363 error = hammer_get_vnode(ip, ap->a_vpp);
1364 hammer_rel_inode(ip, 0);
1368 hammer_done_transaction(&trans);
1369 lwkt_reltoken(&hmp->fs_token);
1374 * hammer_vop_nlink { nch, dvp, vp, cred }
1378 hammer_vop_nlink(struct vop_nlink_args *ap)
1380 struct hammer_transaction trans;
1383 struct nchandle *nch;
1387 if (ap->a_dvp->v_mount != ap->a_vp->v_mount)
1391 dip = VTOI(ap->a_dvp);
1392 ip = VTOI(ap->a_vp);
1395 if (dip->obj_localization != ip->obj_localization)
1398 if (dip->flags & HAMMER_INODE_RO)
1400 if (ip->flags & HAMMER_INODE_RO)
1402 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0)
1406 * Create a transaction to cover the operations we perform.
1408 lwkt_gettoken(&hmp->fs_token);
1409 hammer_start_transaction(&trans, hmp);
1412 * Add the filesystem object to the directory. Note that neither
1413 * dip nor ip are referenced or locked, but their vnodes are
1414 * referenced. This function will bump the inode's link count.
1416 error = hammer_ip_add_direntry(&trans, dip,
1417 nch->ncp->nc_name, nch->ncp->nc_nlen,
1424 cache_setunresolved(nch);
1425 cache_setvp(nch, ap->a_vp);
1427 hammer_done_transaction(&trans);
1428 hammer_knote(ap->a_vp, NOTE_LINK);
1429 hammer_knote(ap->a_dvp, NOTE_WRITE);
1430 lwkt_reltoken(&hmp->fs_token);
1435 * hammer_vop_nmkdir { nch, dvp, vpp, cred, vap }
1437 * The operating system has already ensured that the directory entry
1438 * does not exist and done all appropriate namespace locking.
1442 hammer_vop_nmkdir(struct vop_nmkdir_args *ap)
1444 struct hammer_transaction trans;
1447 struct nchandle *nch;
1452 dip = VTOI(ap->a_dvp);
1455 if (dip->flags & HAMMER_INODE_RO)
1457 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0)
1461 * Create a transaction to cover the operations we perform.
1463 lwkt_gettoken(&hmp->fs_token);
1464 hammer_start_transaction(&trans, hmp);
1467 * Create a new filesystem object of the requested type. The
1468 * returned inode will be referenced but not locked.
1470 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
1471 dip, nch->ncp->nc_name, nch->ncp->nc_nlen,
1474 hammer_done_transaction(&trans);
1476 lwkt_reltoken(&hmp->fs_token);
1480 * Add the new filesystem object to the directory. This will also
1481 * bump the inode's link count.
1483 error = hammer_ip_add_direntry(&trans, dip,
1484 nch->ncp->nc_name, nch->ncp->nc_nlen,
1487 hkprintf("hammer_mkdir (add) error %d\n", error);
1493 hammer_rel_inode(nip, 0);
1496 error = hammer_get_vnode(nip, ap->a_vpp);
1497 hammer_rel_inode(nip, 0);
1499 cache_setunresolved(ap->a_nch);
1500 cache_setvp(ap->a_nch, *ap->a_vpp);
1503 hammer_done_transaction(&trans);
1505 hammer_knote(ap->a_dvp, NOTE_WRITE | NOTE_LINK);
1506 lwkt_reltoken(&hmp->fs_token);
1511 * hammer_vop_nmknod { nch, dvp, vpp, cred, vap }
1513 * The operating system has already ensured that the directory entry
1514 * does not exist and done all appropriate namespace locking.
1518 hammer_vop_nmknod(struct vop_nmknod_args *ap)
1520 struct hammer_transaction trans;
1523 struct nchandle *nch;
1528 dip = VTOI(ap->a_dvp);
1531 if (dip->flags & HAMMER_INODE_RO)
1533 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0)
1537 * Create a transaction to cover the operations we perform.
1539 lwkt_gettoken(&hmp->fs_token);
1540 hammer_start_transaction(&trans, hmp);
1543 * Create a new filesystem object of the requested type. The
1544 * returned inode will be referenced but not locked.
1546 * If mknod specifies a directory a pseudo-fs is created.
1548 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
1549 dip, nch->ncp->nc_name, nch->ncp->nc_nlen,
1552 hammer_done_transaction(&trans);
1554 lwkt_reltoken(&hmp->fs_token);
1559 * Add the new filesystem object to the directory. This will also
1560 * bump the inode's link count.
1562 error = hammer_ip_add_direntry(&trans, dip,
1563 nch->ncp->nc_name, nch->ncp->nc_nlen,
1570 hammer_rel_inode(nip, 0);
1573 error = hammer_get_vnode(nip, ap->a_vpp);
1574 hammer_rel_inode(nip, 0);
1576 cache_setunresolved(ap->a_nch);
1577 cache_setvp(ap->a_nch, *ap->a_vpp);
1580 hammer_done_transaction(&trans);
1582 hammer_knote(ap->a_dvp, NOTE_WRITE);
1583 lwkt_reltoken(&hmp->fs_token);
1588 * hammer_vop_open { vp, mode, cred, fp }
1590 * MPSAFE (does not require fs_token)
1594 hammer_vop_open(struct vop_open_args *ap)
1598 ip = VTOI(ap->a_vp);
1600 if ((ap->a_mode & FWRITE) && (ip->flags & HAMMER_INODE_RO))
1602 return(vop_stdopen(ap));
1606 * hammer_vop_print { vp }
1610 hammer_vop_print(struct vop_print_args *ap)
1616 * hammer_vop_readdir { vp, uio, cred, *eofflag, *ncookies, off_t **cookies }
1620 hammer_vop_readdir(struct vop_readdir_args *ap)
1622 struct hammer_transaction trans;
1623 struct hammer_cursor cursor;
1627 hammer_base_elm_t base;
1636 ip = VTOI(ap->a_vp);
1638 saveoff = uio->uio_offset;
1641 if (ap->a_ncookies) {
1642 ncookies = uio->uio_resid / 16 + 1;
1643 if (ncookies > 1024)
1645 cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
1653 lwkt_gettoken(&hmp->fs_token);
1654 hammer_simple_transaction(&trans, hmp);
1657 * Handle artificial entries
1659 * It should be noted that the minimum value for a directory
1660 * hash key on-media is 0x0000000100000000, so we can use anything
1661 * less then that to represent our 'special' key space.
1665 r = vop_write_dirent(&error, uio, ip->obj_id, DT_DIR, 1, ".");
1669 cookies[cookie_index] = saveoff;
1672 if (cookie_index == ncookies)
1676 if (ip->ino_data.parent_obj_id) {
1677 r = vop_write_dirent(&error, uio,
1678 ip->ino_data.parent_obj_id,
1681 r = vop_write_dirent(&error, uio,
1682 ip->obj_id, DT_DIR, 2, "..");
1687 cookies[cookie_index] = saveoff;
1690 if (cookie_index == ncookies)
1695 * Key range (begin and end inclusive) to scan. Directory keys
1696 * directly translate to a 64 bit 'seek' position.
1698 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
1699 cursor.key_beg.localization = ip->obj_localization |
1700 hammer_dir_localization(ip);
1701 cursor.key_beg.obj_id = ip->obj_id;
1702 cursor.key_beg.create_tid = 0;
1703 cursor.key_beg.delete_tid = 0;
1704 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1705 cursor.key_beg.obj_type = 0;
1706 cursor.key_beg.key = saveoff;
1708 cursor.key_end = cursor.key_beg;
1709 cursor.key_end.key = HAMMER_MAX_KEY;
1710 cursor.asof = ip->obj_asof;
1711 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
1713 error = hammer_ip_first(&cursor);
1715 while (error == 0) {
1716 error = hammer_ip_resolve_data(&cursor);
1719 base = &cursor.leaf->base;
1720 saveoff = base->key;
1721 KKASSERT(cursor.leaf->data_len > HAMMER_ENTRY_NAME_OFF);
1723 if (base->obj_id != ip->obj_id)
1724 hpanic("bad record at %p", cursor.node);
1726 dtype = hammer_get_dtype(cursor.leaf->base.obj_type);
1727 r = vop_write_dirent(
1728 &error, uio, cursor.data->entry.obj_id,
1730 cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF ,
1731 (void *)cursor.data->entry.name);
1736 cookies[cookie_index] = base->key;
1738 if (cookie_index == ncookies)
1740 error = hammer_ip_next(&cursor);
1742 hammer_done_cursor(&cursor);
1745 hammer_done_transaction(&trans);
1748 *ap->a_eofflag = (error == ENOENT);
1749 uio->uio_offset = saveoff;
1750 if (error && cookie_index == 0) {
1751 if (error == ENOENT)
1754 kfree(cookies, M_TEMP);
1755 *ap->a_ncookies = 0;
1756 *ap->a_cookies = NULL;
1759 if (error == ENOENT)
1762 *ap->a_ncookies = cookie_index;
1763 *ap->a_cookies = cookies;
1766 lwkt_reltoken(&hmp->fs_token);
1771 * hammer_vop_readlink { vp, uio, cred }
1775 hammer_vop_readlink(struct vop_readlink_args *ap)
1777 struct hammer_transaction trans;
1778 struct hammer_cursor cursor;
1782 uint32_t localization;
1783 hammer_pseudofs_inmem_t pfsm;
1786 ip = VTOI(ap->a_vp);
1789 lwkt_gettoken(&hmp->fs_token);
1792 * Shortcut if the symlink data was stuffed into ino_data.
1794 * Also expand special "@@PFS%05d" softlinks (expansion only
1795 * occurs for non-historical (current) accesses made from the
1796 * primary filesystem).
1798 * Note that userspace hammer command does not allow users to
1799 * create a @@PFS softlink under an existing other PFS (id!=0)
1800 * so the ip localization here for @@PFS softlink is always 0.
1802 if (ip->ino_data.size <= HAMMER_INODE_BASESYMLEN) {
1806 ptr = ip->ino_data.ext.symlink;
1807 bytes = (int)ip->ino_data.size;
1809 ip->obj_asof == HAMMER_MAX_TID &&
1810 ip->obj_localization == HAMMER_DEF_LOCALIZATION &&
1811 strncmp(ptr, "@@PFS", 5) == 0) {
1812 hammer_simple_transaction(&trans, hmp);
1813 bcopy(ptr + 5, buf, 5);
1815 localization = pfs_to_lo(strtoul(buf, NULL, 10));
1816 pfsm = hammer_load_pseudofs(&trans, localization,
1819 if (hammer_is_pfs_slave(&pfsm->pfsd)) {
1820 /* vap->va_size == 26 */
1821 ksnprintf(buf, sizeof(buf),
1823 (intmax_t)pfsm->pfsd.sync_end_tid,
1824 lo_to_pfs(localization));
1826 /* vap->va_size == 10 */
1827 ksnprintf(buf, sizeof(buf),
1829 lo_to_pfs(localization));
1832 bytes = strlen(buf);
1835 hammer_rel_pseudofs(hmp, pfsm);
1836 hammer_done_transaction(&trans);
1838 error = uiomove(ptr, bytes, ap->a_uio);
1839 lwkt_reltoken(&hmp->fs_token);
1846 hammer_simple_transaction(&trans, hmp);
1847 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
1850 * Key range (begin and end inclusive) to scan. Directory keys
1851 * directly translate to a 64 bit 'seek' position.
1853 cursor.key_beg.localization = ip->obj_localization |
1854 HAMMER_LOCALIZE_MISC;
1855 cursor.key_beg.obj_id = ip->obj_id;
1856 cursor.key_beg.create_tid = 0;
1857 cursor.key_beg.delete_tid = 0;
1858 cursor.key_beg.rec_type = HAMMER_RECTYPE_FIX;
1859 cursor.key_beg.obj_type = 0;
1860 cursor.key_beg.key = HAMMER_FIXKEY_SYMLINK;
1861 cursor.asof = ip->obj_asof;
1862 cursor.flags |= HAMMER_CURSOR_ASOF;
1864 error = hammer_ip_lookup(&cursor);
1866 error = hammer_ip_resolve_data(&cursor);
1868 KKASSERT(cursor.leaf->data_len >=
1869 HAMMER_SYMLINK_NAME_OFF);
1870 error = uiomove(cursor.data->symlink.name,
1871 cursor.leaf->data_len -
1872 HAMMER_SYMLINK_NAME_OFF,
1876 hammer_done_cursor(&cursor);
1877 hammer_done_transaction(&trans);
1878 lwkt_reltoken(&hmp->fs_token);
1883 * hammer_vop_nremove { nch, dvp, cred }
1887 hammer_vop_nremove(struct vop_nremove_args *ap)
1889 struct hammer_transaction trans;
1894 dip = VTOI(ap->a_dvp);
1897 if (hammer_nohistory(dip) == 0 &&
1898 (error = hammer_checkspace(hmp, HAMMER_CHKSPC_REMOVE)) != 0) {
1902 lwkt_gettoken(&hmp->fs_token);
1903 hammer_start_transaction(&trans, hmp);
1904 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp, ap->a_cred, 0, 0);
1905 hammer_done_transaction(&trans);
1907 hammer_knote(ap->a_dvp, NOTE_WRITE);
1908 lwkt_reltoken(&hmp->fs_token);
1913 * hammer_vop_nrename { fnch, tnch, fdvp, tdvp, cred }
1917 hammer_vop_nrename(struct vop_nrename_args *ap)
1919 struct hammer_transaction trans;
1920 struct namecache *fncp;
1921 struct namecache *tncp;
1922 hammer_inode_t fdip;
1923 hammer_inode_t tdip;
1926 struct hammer_cursor cursor;
1928 uint32_t max_iterations;
1931 if (ap->a_fdvp->v_mount != ap->a_tdvp->v_mount)
1933 if (ap->a_fdvp->v_mount != ap->a_fnch->ncp->nc_vp->v_mount)
1936 fdip = VTOI(ap->a_fdvp);
1937 tdip = VTOI(ap->a_tdvp);
1938 fncp = ap->a_fnch->ncp;
1939 tncp = ap->a_tnch->ncp;
1940 ip = VTOI(fncp->nc_vp);
1941 KKASSERT(ip != NULL);
1945 if (fdip->obj_localization != tdip->obj_localization)
1947 if (fdip->obj_localization != ip->obj_localization)
1950 if (fdip->flags & HAMMER_INODE_RO)
1952 if (tdip->flags & HAMMER_INODE_RO)
1954 if (ip->flags & HAMMER_INODE_RO)
1956 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0)
1959 lwkt_gettoken(&hmp->fs_token);
1960 hammer_start_transaction(&trans, hmp);
1963 * Remove tncp from the target directory and then link ip as
1964 * tncp. XXX pass trans to dounlink
1966 * Force the inode sync-time to match the transaction so it is
1967 * in-sync with the creation of the target directory entry.
1969 error = hammer_dounlink(&trans, ap->a_tnch, ap->a_tdvp,
1971 if (error == 0 || error == ENOENT) {
1972 error = hammer_ip_add_direntry(&trans, tdip,
1973 tncp->nc_name, tncp->nc_nlen,
1976 ip->ino_data.parent_obj_id = tdip->obj_id;
1977 ip->ino_data.ctime = trans.time;
1978 hammer_modify_inode(&trans, ip, HAMMER_INODE_DDIRTY);
1982 goto failed; /* XXX */
1985 * Locate the record in the originating directory and remove it.
1987 * Calculate the namekey and setup the key range for the scan. This
1988 * works kinda like a chained hash table where the lower 32 bits
1989 * of the namekey synthesize the chain.
1991 * The key range is inclusive of both key_beg and key_end.
1993 namekey = hammer_direntry_namekey(fdip, fncp->nc_name, fncp->nc_nlen,
1996 hammer_init_cursor(&trans, &cursor, &fdip->cache[1], fdip);
1997 cursor.key_beg.localization = fdip->obj_localization |
1998 hammer_dir_localization(fdip);
1999 cursor.key_beg.obj_id = fdip->obj_id;
2000 cursor.key_beg.key = namekey;
2001 cursor.key_beg.create_tid = 0;
2002 cursor.key_beg.delete_tid = 0;
2003 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
2004 cursor.key_beg.obj_type = 0;
2006 cursor.key_end = cursor.key_beg;
2007 cursor.key_end.key += max_iterations;
2008 cursor.asof = fdip->obj_asof;
2009 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
2012 * Scan all matching records (the chain), locate the one matching
2013 * the requested path component.
2015 * The hammer_ip_*() functions merge in-memory records with on-disk
2016 * records for the purposes of the search.
2018 error = hammer_ip_first(&cursor);
2019 while (error == 0) {
2020 if (hammer_ip_resolve_data(&cursor) != 0)
2022 nlen = cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF;
2024 if (fncp->nc_nlen == nlen &&
2025 bcmp(fncp->nc_name, cursor.data->entry.name, nlen) == 0) {
2028 error = hammer_ip_next(&cursor);
2032 * If all is ok we have to get the inode so we can adjust nlinks.
2034 * WARNING: hammer_ip_del_direntry() may have to terminate the
2035 * cursor to avoid a recursion. It's ok to call hammer_done_cursor()
2039 error = hammer_ip_del_direntry(&trans, &cursor, fdip, ip);
2042 * XXX A deadlock here will break rename's atomicy for the purposes
2043 * of crash recovery.
2045 if (error == EDEADLK) {
2046 hammer_done_cursor(&cursor);
2051 * Cleanup and tell the kernel that the rename succeeded.
2053 * NOTE: ip->vp, if non-NULL, cannot be directly referenced
2054 * without formally acquiring the vp since the vp might
2055 * have zero refs on it, or in the middle of a reclaim,
2058 hammer_done_cursor(&cursor);
2060 cache_rename(ap->a_fnch, ap->a_tnch);
2061 hammer_knote(ap->a_fdvp, NOTE_WRITE);
2062 hammer_knote(ap->a_tdvp, NOTE_WRITE);
2066 error = hammer_get_vnode(ip, &vp);
2067 if (error == 0 && vp) {
2069 hammer_knote(ip->vp, NOTE_RENAME);
2073 hdkprintf("ip/vp race2 avoided\n");
2078 hammer_done_transaction(&trans);
2079 lwkt_reltoken(&hmp->fs_token);
2084 * hammer_vop_nrmdir { nch, dvp, cred }
2088 hammer_vop_nrmdir(struct vop_nrmdir_args *ap)
2090 struct hammer_transaction trans;
2095 dip = VTOI(ap->a_dvp);
2098 if (hammer_nohistory(dip) == 0 &&
2099 (error = hammer_checkspace(hmp, HAMMER_CHKSPC_REMOVE)) != 0) {
2103 lwkt_gettoken(&hmp->fs_token);
2104 hammer_start_transaction(&trans, hmp);
2105 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp, ap->a_cred, 0, 1);
2106 hammer_done_transaction(&trans);
2108 hammer_knote(ap->a_dvp, NOTE_WRITE | NOTE_LINK);
2109 lwkt_reltoken(&hmp->fs_token);
2114 * hammer_vop_markatime { vp, cred }
2118 hammer_vop_markatime(struct vop_markatime_args *ap)
2120 struct hammer_transaction trans;
2124 ip = VTOI(ap->a_vp);
2125 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
2127 if (ip->flags & HAMMER_INODE_RO)
2130 if (hmp->mp->mnt_flag & MNT_NOATIME)
2132 lwkt_gettoken(&hmp->fs_token);
2133 hammer_start_transaction(&trans, hmp);
2135 ip->ino_data.atime = trans.time;
2136 hammer_modify_inode(&trans, ip, HAMMER_INODE_ATIME);
2137 hammer_done_transaction(&trans);
2138 hammer_knote(ap->a_vp, NOTE_ATTRIB);
2139 lwkt_reltoken(&hmp->fs_token);
2144 * hammer_vop_setattr { vp, vap, cred }
2148 hammer_vop_setattr(struct vop_setattr_args *ap)
2150 struct hammer_transaction trans;
2160 int64_t aligned_size;
2165 ip = ap->a_vp->v_data;
2170 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
2172 if (ip->flags & HAMMER_INODE_RO)
2174 if (hammer_nohistory(ip) == 0 &&
2175 (error = hammer_checkspace(hmp, HAMMER_CHKSPC_REMOVE)) != 0) {
2179 lwkt_gettoken(&hmp->fs_token);
2180 hammer_start_transaction(&trans, hmp);
2183 if (vap->va_flags != VNOVAL) {
2184 flags = ip->ino_data.uflags;
2185 error = vop_helper_setattr_flags(&flags, vap->va_flags,
2186 hammer_to_unix_xid(&ip->ino_data.uid),
2189 if (ip->ino_data.uflags != flags) {
2190 ip->ino_data.uflags = flags;
2191 ip->ino_data.ctime = trans.time;
2192 modflags |= HAMMER_INODE_DDIRTY;
2193 kflags |= NOTE_ATTRIB;
2195 if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) {
2202 if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) {
2206 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
2207 mode_t cur_mode = ip->ino_data.mode;
2208 uid_t cur_uid = hammer_to_unix_xid(&ip->ino_data.uid);
2209 gid_t cur_gid = hammer_to_unix_xid(&ip->ino_data.gid);
2210 hammer_uuid_t uuid_uid;
2211 hammer_uuid_t uuid_gid;
2213 error = vop_helper_chown(ap->a_vp, vap->va_uid, vap->va_gid,
2215 &cur_uid, &cur_gid, &cur_mode);
2217 hammer_guid_to_uuid(&uuid_uid, cur_uid);
2218 hammer_guid_to_uuid(&uuid_gid, cur_gid);
2219 if (kuuid_compare(&uuid_uid, &ip->ino_data.uid) ||
2220 kuuid_compare(&uuid_gid, &ip->ino_data.gid) ||
2221 ip->ino_data.mode != cur_mode) {
2222 ip->ino_data.uid = uuid_uid;
2223 ip->ino_data.gid = uuid_gid;
2224 ip->ino_data.mode = cur_mode;
2225 ip->ino_data.ctime = trans.time;
2226 modflags |= HAMMER_INODE_DDIRTY;
2228 kflags |= NOTE_ATTRIB;
2231 while (vap->va_size != VNOVAL && ip->ino_data.size != vap->va_size) {
2232 switch(ap->a_vp->v_type) {
2234 if (vap->va_size == ip->ino_data.size)
2238 * Log the operation if in fast-fsync mode or if
2239 * there are unterminated redo write records present.
2241 * The second check is needed so the recovery code
2242 * properly truncates write redos even if nominal
2243 * REDO operations is turned off due to excessive
2244 * writes, because the related records might be
2245 * destroyed and never lay down a TERM_WRITE.
2247 if ((ip->flags & HAMMER_INODE_REDO) ||
2248 (ip->flags & HAMMER_INODE_RDIRTY)) {
2249 error = hammer_generate_redo(&trans, ip,
2254 blksize = hammer_blocksize(vap->va_size);
2257 * XXX break atomicy, we can deadlock the backend
2258 * if we do not release the lock. Probably not a
2261 if (vap->va_size < ip->ino_data.size) {
2262 nvtruncbuf(ap->a_vp, vap->va_size,
2264 hammer_blockoff(vap->va_size),
2267 kflags |= NOTE_WRITE;
2269 nvextendbuf(ap->a_vp,
2272 hammer_blocksize(ip->ino_data.size),
2273 hammer_blocksize(vap->va_size),
2274 hammer_blockoff(ip->ino_data.size),
2275 hammer_blockoff(vap->va_size),
2278 kflags |= NOTE_WRITE | NOTE_EXTEND;
2280 ip->ino_data.size = vap->va_size;
2281 ip->ino_data.mtime = trans.time;
2282 /* XXX safe to use SDIRTY instead of DDIRTY here? */
2283 modflags |= HAMMER_INODE_MTIME | HAMMER_INODE_DDIRTY;
2286 * On-media truncation is cached in the inode until
2287 * the inode is synchronized. We must immediately
2288 * handle any frontend records.
2291 hammer_ip_frontend_trunc(ip, vap->va_size);
2292 if ((ip->flags & HAMMER_INODE_TRUNCATED) == 0) {
2293 ip->flags |= HAMMER_INODE_TRUNCATED;
2294 ip->trunc_off = vap->va_size;
2295 hammer_inode_dirty(ip);
2296 } else if (ip->trunc_off > vap->va_size) {
2297 ip->trunc_off = vap->va_size;
2303 * When truncating, nvtruncbuf() may have cleaned out
2304 * a portion of the last block on-disk in the buffer
2305 * cache. We must clean out any frontend records
2306 * for blocks beyond the new last block.
2308 aligned_size = (vap->va_size + (blksize - 1)) &
2309 ~(int64_t)(blksize - 1);
2310 if (truncating && vap->va_size < aligned_size) {
2311 aligned_size -= blksize;
2312 hammer_ip_frontend_trunc(ip, aligned_size);
2317 if ((ip->flags & HAMMER_INODE_TRUNCATED) == 0) {
2318 ip->flags |= HAMMER_INODE_TRUNCATED;
2319 ip->trunc_off = vap->va_size;
2320 hammer_inode_dirty(ip);
2321 } else if (ip->trunc_off > vap->va_size) {
2322 ip->trunc_off = vap->va_size;
2324 hammer_ip_frontend_trunc(ip, vap->va_size);
2325 ip->ino_data.size = vap->va_size;
2326 ip->ino_data.mtime = trans.time;
2327 modflags |= HAMMER_INODE_MTIME | HAMMER_INODE_DDIRTY;
2328 kflags |= NOTE_ATTRIB;
2336 if (vap->va_atime.tv_sec != VNOVAL) {
2337 ip->ino_data.atime = hammer_timespec_to_time(&vap->va_atime);
2338 modflags |= HAMMER_INODE_ATIME;
2339 kflags |= NOTE_ATTRIB;
2341 if (vap->va_mtime.tv_sec != VNOVAL) {
2342 ip->ino_data.mtime = hammer_timespec_to_time(&vap->va_mtime);
2343 modflags |= HAMMER_INODE_MTIME;
2344 kflags |= NOTE_ATTRIB;
2346 if (vap->va_mode != (mode_t)VNOVAL) {
2347 mode_t cur_mode = ip->ino_data.mode;
2348 uid_t cur_uid = hammer_to_unix_xid(&ip->ino_data.uid);
2349 gid_t cur_gid = hammer_to_unix_xid(&ip->ino_data.gid);
2351 error = vop_helper_chmod(ap->a_vp, vap->va_mode, ap->a_cred,
2352 cur_uid, cur_gid, &cur_mode);
2353 if (error == 0 && ip->ino_data.mode != cur_mode) {
2354 ip->ino_data.mode = cur_mode;
2355 ip->ino_data.ctime = trans.time;
2356 modflags |= HAMMER_INODE_DDIRTY;
2357 kflags |= NOTE_ATTRIB;
2362 hammer_modify_inode(&trans, ip, modflags);
2363 hammer_done_transaction(&trans);
2364 hammer_knote(ap->a_vp, kflags);
2365 lwkt_reltoken(&hmp->fs_token);
2370 * hammer_vop_nsymlink { nch, dvp, vpp, cred, vap, target }
2374 hammer_vop_nsymlink(struct vop_nsymlink_args *ap)
2376 struct hammer_transaction trans;
2379 hammer_record_t record;
2380 struct nchandle *nch;
2385 ap->a_vap->va_type = VLNK;
2388 dip = VTOI(ap->a_dvp);
2391 if (dip->flags & HAMMER_INODE_RO)
2393 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0)
2397 * Create a transaction to cover the operations we perform.
2399 lwkt_gettoken(&hmp->fs_token);
2400 hammer_start_transaction(&trans, hmp);
2403 * Create a new filesystem object of the requested type. The
2404 * returned inode will be referenced but not locked.
2407 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
2408 dip, nch->ncp->nc_name, nch->ncp->nc_nlen,
2411 hammer_done_transaction(&trans);
2413 lwkt_reltoken(&hmp->fs_token);
2418 * Add a record representing the symlink. symlink stores the link
2419 * as pure data, not a string, and is no \0 terminated.
2422 bytes = strlen(ap->a_target);
2424 if (bytes <= HAMMER_INODE_BASESYMLEN) {
2425 bcopy(ap->a_target, nip->ino_data.ext.symlink, bytes);
2427 record = hammer_alloc_mem_record(nip, bytes);
2428 record->type = HAMMER_MEM_RECORD_GENERAL;
2430 record->leaf.base.localization = nip->obj_localization |
2431 HAMMER_LOCALIZE_MISC;
2432 record->leaf.base.key = HAMMER_FIXKEY_SYMLINK;
2433 record->leaf.base.rec_type = HAMMER_RECTYPE_FIX;
2434 record->leaf.data_len = bytes;
2435 KKASSERT(HAMMER_SYMLINK_NAME_OFF == 0);
2436 bcopy(ap->a_target, record->data->symlink.name, bytes);
2437 error = hammer_ip_add_record(&trans, record);
2441 * Set the file size to the length of the link.
2444 nip->ino_data.size = bytes;
2445 hammer_modify_inode(&trans, nip, HAMMER_INODE_DDIRTY);
2449 error = hammer_ip_add_direntry(&trans, dip, nch->ncp->nc_name,
2450 nch->ncp->nc_nlen, nip);
2456 hammer_rel_inode(nip, 0);
2459 error = hammer_get_vnode(nip, ap->a_vpp);
2460 hammer_rel_inode(nip, 0);
2462 cache_setunresolved(ap->a_nch);
2463 cache_setvp(ap->a_nch, *ap->a_vpp);
2464 hammer_knote(ap->a_dvp, NOTE_WRITE);
2467 hammer_done_transaction(&trans);
2468 lwkt_reltoken(&hmp->fs_token);
2473 * hammer_vop_nwhiteout { nch, dvp, cred, flags }
2477 hammer_vop_nwhiteout(struct vop_nwhiteout_args *ap)
2479 struct hammer_transaction trans;
2484 dip = VTOI(ap->a_dvp);
2487 if (hammer_nohistory(dip) == 0 &&
2488 (error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0) {
2492 lwkt_gettoken(&hmp->fs_token);
2493 hammer_start_transaction(&trans, hmp);
2494 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp,
2495 ap->a_cred, ap->a_flags, -1);
2496 hammer_done_transaction(&trans);
2497 lwkt_reltoken(&hmp->fs_token);
2503 * hammer_vop_ioctl { vp, command, data, fflag, cred }
2507 hammer_vop_ioctl(struct vop_ioctl_args *ap)
2509 hammer_inode_t ip = ap->a_vp->v_data;
2510 hammer_mount_t hmp = ip->hmp;
2513 lwkt_gettoken(&hmp->fs_token);
2514 error = hammer_ioctl(ip, ap->a_command, ap->a_data,
2515 ap->a_fflag, ap->a_cred);
2516 lwkt_reltoken(&hmp->fs_token);
2522 hammer_vop_mountctl(struct vop_mountctl_args *ap)
2524 static const struct mountctl_opt extraopt[] = {
2525 { HMNT_NOHISTORY, "nohistory" },
2526 { HMNT_MASTERID, "master" },
2527 { HMNT_NOMIRROR, "nomirror" },
2538 mp = ap->a_head.a_ops->head.vv_mount;
2539 KKASSERT(mp->mnt_data != NULL);
2540 hmp = (hammer_mount_t)mp->mnt_data;
2542 lwkt_gettoken(&hmp->fs_token);
2545 case MOUNTCTL_SET_EXPORT:
2546 if (ap->a_ctllen != sizeof(struct export_args))
2549 error = hammer_vfs_export(mp, ap->a_op,
2550 (const struct export_args *)ap->a_ctl);
2552 case MOUNTCTL_MOUNTFLAGS:
2554 * Call standard mountctl VOP function
2555 * so we get user mount flags.
2557 error = vop_stdmountctl(ap);
2561 usedbytes = *ap->a_res;
2563 if (usedbytes > 0 && usedbytes < ap->a_buflen) {
2564 usedbytes += vfs_flagstostr(hmp->hflags, extraopt,
2566 ap->a_buflen - usedbytes,
2570 *ap->a_res += usedbytes;
2573 error = vop_stdmountctl(ap);
2576 lwkt_reltoken(&hmp->fs_token);
2581 * hammer_vop_strategy { vp, bio }
2583 * Strategy call, used for regular file read & write only. Note that the
2584 * bp may represent a cluster.
2586 * To simplify operation and allow better optimizations in the future,
2587 * this code does not make any assumptions with regards to buffer alignment
2592 hammer_vop_strategy(struct vop_strategy_args *ap)
2597 bp = ap->a_bio->bio_buf;
2601 error = hammer_vop_strategy_read(ap);
2604 error = hammer_vop_strategy_write(ap);
2607 bp->b_error = error = EINVAL;
2608 bp->b_flags |= B_ERROR;
2613 /* hammer_dump_dedup_cache(((hammer_inode_t)ap->a_vp->v_data)->hmp); */
2619 * Read from a regular file. Iterate the related records and fill in the
2620 * BIO/BUF. Gaps are zero-filled.
2622 * The support code in hammer_object.c should be used to deal with mixed
2623 * in-memory and on-disk records.
2625 * NOTE: Can be called from the cluster code with an oversized buf.
2631 hammer_vop_strategy_read(struct vop_strategy_args *ap)
2633 struct hammer_transaction trans;
2637 struct hammer_cursor cursor;
2638 hammer_base_elm_t base;
2639 hammer_off_t disk_offset;
2654 ip = ap->a_vp->v_data;
2658 * The zone-2 disk offset may have been set by the cluster code via
2659 * a BMAP operation, or else should be NOOFFSET.
2661 * Checking the high bits for a match against zone-2 should suffice.
2663 * In cases where a lot of data duplication is present it may be
2664 * more beneficial to drop through and doubule-buffer through the
2667 nbio = push_bio(bio);
2668 if (hammer_is_zone_large_data(nbio->bio_offset)) {
2669 if (hammer_double_buffer == 0) {
2670 lwkt_gettoken(&hmp->fs_token);
2671 error = hammer_io_direct_read(hmp, nbio, NULL);
2672 lwkt_reltoken(&hmp->fs_token);
2677 * Try to shortcut requests for double_buffer mode too.
2678 * Since this mode runs through the device buffer cache
2679 * only compatible buffer sizes (meaning those generated
2680 * by normal filesystem buffers) are legal.
2682 if (hammer_live_dedup == 0 && (bp->b_flags & B_PAGING) == 0) {
2683 lwkt_gettoken(&hmp->fs_token);
2684 error = hammer_io_indirect_read(hmp, nbio, NULL);
2685 lwkt_reltoken(&hmp->fs_token);
2691 * Well, that sucked. Do it the hard way. If all the stars are
2692 * aligned we may still be able to issue a direct-read.
2694 lwkt_gettoken(&hmp->fs_token);
2695 hammer_simple_transaction(&trans, hmp);
2696 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
2699 * Key range (begin and end inclusive) to scan. Note that the key's
2700 * stored in the actual records represent BASE+LEN, not BASE. The
2701 * first record containing bio_offset will have a key > bio_offset.
2703 cursor.key_beg.localization = ip->obj_localization |
2704 HAMMER_LOCALIZE_MISC;
2705 cursor.key_beg.obj_id = ip->obj_id;
2706 cursor.key_beg.create_tid = 0;
2707 cursor.key_beg.delete_tid = 0;
2708 cursor.key_beg.obj_type = 0;
2709 cursor.key_beg.key = bio->bio_offset + 1;
2710 cursor.asof = ip->obj_asof;
2711 cursor.flags |= HAMMER_CURSOR_ASOF;
2713 cursor.key_end = cursor.key_beg;
2714 KKASSERT(ip->ino_data.obj_type == HAMMER_OBJTYPE_REGFILE);
2716 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_DBFILE) {
2717 cursor.key_beg.rec_type = HAMMER_RECTYPE_DB;
2718 cursor.key_end.rec_type = HAMMER_RECTYPE_DB;
2719 cursor.key_end.key = HAMMER_MAX_KEY;
2723 ran_end = bio->bio_offset + bp->b_bufsize;
2724 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
2725 cursor.key_end.rec_type = HAMMER_RECTYPE_DATA;
2726 tmp64 = ran_end + MAXPHYS + 1; /* work-around GCC-4 bug */
2727 if (tmp64 < ran_end)
2728 cursor.key_end.key = HAMMER_MAX_KEY;
2730 cursor.key_end.key = ran_end + MAXPHYS + 1;
2732 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
2735 * Set NOSWAPCACHE for cursor data extraction if double buffering
2736 * is disabled or (if the file is not marked cacheable via chflags
2737 * and vm.swapcache_use_chflags is enabled).
2739 if (hammer_double_buffer == 0 ||
2740 ((ap->a_vp->v_flag & VSWAPCACHE) == 0 &&
2741 vm_swapcache_use_chflags)) {
2742 cursor.flags |= HAMMER_CURSOR_NOSWAPCACHE;
2745 error = hammer_ip_first(&cursor);
2748 while (error == 0) {
2750 * Get the base file offset of the record. The key for
2751 * data records is (base + bytes) rather then (base).
2753 base = &cursor.leaf->base;
2754 rec_offset = base->key - cursor.leaf->data_len;
2757 * Calculate the gap, if any, and zero-fill it.
2759 * n is the offset of the start of the record verses our
2760 * current seek offset in the bio.
2762 n = (int)(rec_offset - (bio->bio_offset + boff));
2764 if (n > bp->b_bufsize - boff)
2765 n = bp->b_bufsize - boff;
2766 bzero((char *)bp->b_data + boff, n);
2772 * Calculate the data offset in the record and the number
2773 * of bytes we can copy.
2775 * There are two degenerate cases. First, boff may already
2776 * be at bp->b_bufsize. Secondly, the data offset within
2777 * the record may exceed the record's size.
2781 n = cursor.leaf->data_len - roff;
2783 hdkprintf("bad n=%d roff=%d\n", n, roff);
2785 } else if (n > bp->b_bufsize - boff) {
2786 n = bp->b_bufsize - boff;
2790 * Deal with cached truncations. This cool bit of code
2791 * allows truncate()/ftruncate() to avoid having to sync
2794 * If the frontend is truncated then all backend records are
2795 * subject to the frontend's truncation.
2797 * If the backend is truncated then backend records on-disk
2798 * (but not in-memory) are subject to the backend's
2799 * truncation. In-memory records owned by the backend
2800 * represent data written after the truncation point on the
2801 * backend and must not be truncated.
2803 * Truncate operations deal with frontend buffer cache
2804 * buffers and frontend-owned in-memory records synchronously.
2806 if (ip->flags & HAMMER_INODE_TRUNCATED) {
2807 if (hammer_cursor_ondisk(&cursor)/* ||
2808 cursor.iprec->flush_state == HAMMER_FST_FLUSH*/) {
2809 if (ip->trunc_off <= rec_offset)
2811 else if (ip->trunc_off < rec_offset + n)
2812 n = (int)(ip->trunc_off - rec_offset);
2815 if (ip->sync_flags & HAMMER_INODE_TRUNCATED) {
2816 if (hammer_cursor_ondisk(&cursor)) {
2817 if (ip->sync_trunc_off <= rec_offset)
2819 else if (ip->sync_trunc_off < rec_offset + n)
2820 n = (int)(ip->sync_trunc_off - rec_offset);
2825 * Try to issue a direct read into our bio if possible,
2826 * otherwise resolve the element data into a hammer_buffer
2829 * The buffer on-disk should be zerod past any real
2830 * truncation point, but may not be for any synthesized
2831 * truncation point from above.
2833 * NOTE: disk_offset is only valid if the cursor data is
2836 disk_offset = cursor.leaf->data_offset + roff;
2837 isdedupable = (boff == 0 && n == bp->b_bufsize &&
2838 hammer_cursor_ondisk(&cursor) &&
2839 ((int)disk_offset & HAMMER_BUFMASK) == 0);
2841 if (isdedupable && hammer_double_buffer == 0) {
2845 KKASSERT(hammer_is_zone_large_data(disk_offset));
2846 nbio->bio_offset = disk_offset;
2847 error = hammer_io_direct_read(hmp, nbio, cursor.leaf);
2848 if (hammer_live_dedup && error == 0)
2849 hammer_dedup_cache_add(ip, cursor.leaf);
2851 } else if (isdedupable) {
2853 * Async I/O case for reading from backing store
2854 * and copying the data to the filesystem buffer.
2855 * live-dedup has to verify the data anyway if it
2856 * gets a hit later so we can just add the entry
2859 KKASSERT(hammer_is_zone_large_data(disk_offset));
2860 nbio->bio_offset = disk_offset;
2861 if (hammer_live_dedup)
2862 hammer_dedup_cache_add(ip, cursor.leaf);
2863 error = hammer_io_indirect_read(hmp, nbio, cursor.leaf);
2866 error = hammer_ip_resolve_data(&cursor);
2868 if (hammer_live_dedup && isdedupable)
2869 hammer_dedup_cache_add(ip, cursor.leaf);
2870 bcopy((char *)cursor.data + roff,
2871 (char *)bp->b_data + boff, n);
2878 * We have to be sure that the only elements added to the
2879 * dedup cache are those which are already on-media.
2881 if (hammer_live_dedup && hammer_cursor_ondisk(&cursor))
2882 hammer_dedup_cache_add(ip, cursor.leaf);
2885 * Iterate until we have filled the request.
2888 if (boff == bp->b_bufsize)
2890 error = hammer_ip_next(&cursor);
2894 * There may have been a gap after the last record
2896 if (error == ENOENT)
2898 if (error == 0 && boff != bp->b_bufsize) {
2899 KKASSERT(boff < bp->b_bufsize);
2900 bzero((char *)bp->b_data + boff, bp->b_bufsize - boff);
2901 /* boff = bp->b_bufsize; */
2905 * Disallow swapcache operation on the vnode buffer if double
2906 * buffering is enabled, the swapcache will get the data via
2907 * the block device buffer.
2909 if (hammer_double_buffer)
2910 bp->b_flags |= B_NOTMETA;
2916 bp->b_error = error;
2918 bp->b_flags |= B_ERROR;
2923 * Cache the b-tree node for the last data read in cache[1].
2925 * If we hit the file EOF then also cache the node in the
2926 * governing directory's cache[3], it will be used to initialize
2927 * the new inode's cache[1] for any inodes looked up via the directory.
2929 * This doesn't reduce disk accesses since the B-Tree chain is
2930 * likely cached, but it does reduce cpu overhead when looking
2931 * up file offsets for cpdup/tar/cpio style iterations.
2934 hammer_cache_node(&ip->cache[1], cursor.node);
2935 if (ran_end >= ip->ino_data.size) {
2936 dip = hammer_find_inode(&trans, ip->ino_data.parent_obj_id,
2937 ip->obj_asof, ip->obj_localization);
2939 hammer_cache_node(&dip->cache[3], cursor.node);
2940 hammer_rel_inode(dip, 0);
2943 hammer_done_cursor(&cursor);
2944 hammer_done_transaction(&trans);
2945 lwkt_reltoken(&hmp->fs_token);
2950 * BMAP operation - used to support cluster_read() only.
2952 * (struct vnode *vp, off_t loffset, off_t *doffsetp, int *runp, int *runb)
2954 * This routine may return EOPNOTSUPP if the opration is not supported for
2955 * the specified offset. The contents of the pointer arguments do not
2956 * need to be initialized in that case.
2958 * If a disk address is available and properly aligned return 0 with
2959 * *doffsetp set to the zone-2 address, and *runp / *runb set appropriately
2960 * to the run-length relative to that offset. Callers may assume that
2961 * *doffsetp is valid if 0 is returned, even if *runp is not sufficiently
2962 * large, so return EOPNOTSUPP if it is not sufficiently large.
2966 hammer_vop_bmap(struct vop_bmap_args *ap)
2968 struct hammer_transaction trans;
2971 struct hammer_cursor cursor;
2972 hammer_base_elm_t base;
2976 int64_t base_offset;
2977 int64_t base_disk_offset;
2978 int64_t last_offset;
2979 hammer_off_t last_disk_offset;
2980 hammer_off_t disk_offset;
2985 ip = ap->a_vp->v_data;
2989 * We can only BMAP regular files. We can't BMAP database files,
2992 if (ip->ino_data.obj_type != HAMMER_OBJTYPE_REGFILE)
2996 * bmap is typically called with runp/runb both NULL when used
2997 * for writing. We do not support BMAP for writing atm.
2999 if (ap->a_cmd != BUF_CMD_READ)
3003 * Scan the B-Tree to acquire blockmap addresses, then translate
3006 lwkt_gettoken(&hmp->fs_token);
3007 hammer_simple_transaction(&trans, hmp);
3009 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
3012 * Key range (begin and end inclusive) to scan. Note that the key's
3013 * stored in the actual records represent BASE+LEN, not BASE. The
3014 * first record containing bio_offset will have a key > bio_offset.
3016 cursor.key_beg.localization = ip->obj_localization |
3017 HAMMER_LOCALIZE_MISC;
3018 cursor.key_beg.obj_id = ip->obj_id;
3019 cursor.key_beg.create_tid = 0;
3020 cursor.key_beg.delete_tid = 0;
3021 cursor.key_beg.obj_type = 0;
3023 cursor.key_beg.key = ap->a_loffset - MAXPHYS + 1;
3025 cursor.key_beg.key = ap->a_loffset + 1;
3026 if (cursor.key_beg.key < 0)
3027 cursor.key_beg.key = 0;
3028 cursor.asof = ip->obj_asof;
3029 cursor.flags |= HAMMER_CURSOR_ASOF;
3031 cursor.key_end = cursor.key_beg;
3032 KKASSERT(ip->ino_data.obj_type == HAMMER_OBJTYPE_REGFILE);
3034 ran_end = ap->a_loffset + MAXPHYS;
3035 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
3036 cursor.key_end.rec_type = HAMMER_RECTYPE_DATA;
3037 tmp64 = ran_end + MAXPHYS + 1; /* work-around GCC-4 bug */
3038 if (tmp64 < ran_end)
3039 cursor.key_end.key = HAMMER_MAX_KEY;
3041 cursor.key_end.key = ran_end + MAXPHYS + 1;
3043 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
3045 error = hammer_ip_first(&cursor);
3046 base_offset = last_offset = 0;
3047 base_disk_offset = last_disk_offset = 0;
3049 while (error == 0) {
3051 * Get the base file offset of the record. The key for
3052 * data records is (base + bytes) rather then (base).
3054 * NOTE: rec_offset + rec_len may exceed the end-of-file.
3055 * The extra bytes should be zero on-disk and the BMAP op
3056 * should still be ok.
3058 base = &cursor.leaf->base;
3059 rec_offset = base->key - cursor.leaf->data_len;
3060 rec_len = cursor.leaf->data_len;
3063 * Incorporate any cached truncation.
3065 * NOTE: Modifications to rec_len based on synthesized
3066 * truncation points remove the guarantee that any extended
3067 * data on disk is zero (since the truncations may not have
3068 * taken place on-media yet).
3070 if (ip->flags & HAMMER_INODE_TRUNCATED) {
3071 if (hammer_cursor_ondisk(&cursor) ||
3072 cursor.iprec->flush_state == HAMMER_FST_FLUSH) {
3073 if (ip->trunc_off <= rec_offset)
3075 else if (ip->trunc_off < rec_offset + rec_len)
3076 rec_len = (int)(ip->trunc_off - rec_offset);
3079 if (ip->sync_flags & HAMMER_INODE_TRUNCATED) {
3080 if (hammer_cursor_ondisk(&cursor)) {
3081 if (ip->sync_trunc_off <= rec_offset)
3083 else if (ip->sync_trunc_off < rec_offset + rec_len)
3084 rec_len = (int)(ip->sync_trunc_off - rec_offset);
3089 * Accumulate information. If we have hit a discontiguous
3090 * block reset base_offset unless we are already beyond the
3091 * requested offset. If we are, that's it, we stop.
3095 if (hammer_cursor_ondisk(&cursor)) {
3096 disk_offset = cursor.leaf->data_offset;
3097 if (rec_offset != last_offset ||
3098 disk_offset != last_disk_offset) {
3099 if (rec_offset > ap->a_loffset)
3101 base_offset = rec_offset;
3102 base_disk_offset = disk_offset;
3104 last_offset = rec_offset + rec_len;
3105 last_disk_offset = disk_offset + rec_len;
3107 if (hammer_live_dedup)
3108 hammer_dedup_cache_add(ip, cursor.leaf);
3111 error = hammer_ip_next(&cursor);
3115 hammer_cache_node(&ip->cache[1], cursor.node);
3117 hammer_done_cursor(&cursor);
3118 hammer_done_transaction(&trans);
3119 lwkt_reltoken(&hmp->fs_token);
3122 * If we couldn't find any records or the records we did find were
3123 * all behind the requested offset, return failure. A forward
3124 * truncation can leave a hole w/ no on-disk records.
3126 if (last_offset == 0 || last_offset < ap->a_loffset)
3127 return (EOPNOTSUPP);
3130 * Figure out the block size at the requested offset and adjust
3131 * our limits so the cluster_read() does not create inappropriately
3132 * sized buffer cache buffers.
3134 blksize = hammer_blocksize(ap->a_loffset);
3135 if (hammer_blocksize(base_offset) != blksize) {
3136 base_offset = hammer_blockdemarc(base_offset, ap->a_loffset);
3138 if (last_offset != ap->a_loffset &&
3139 hammer_blocksize(last_offset - 1) != blksize) {
3140 last_offset = hammer_blockdemarc(ap->a_loffset,
3145 * Returning EOPNOTSUPP simply prevents the direct-IO optimization
3148 disk_offset = base_disk_offset + (ap->a_loffset - base_offset);
3150 if (!hammer_is_zone_large_data(disk_offset)) {
3152 * Only large-data zones can be direct-IOd
3155 } else if ((disk_offset & HAMMER_BUFMASK) ||
3156 (last_offset - ap->a_loffset) < blksize) {
3158 * doffsetp is not aligned or the forward run size does
3159 * not cover a whole buffer, disallow the direct I/O.
3166 *ap->a_doffsetp = disk_offset;
3168 *ap->a_runb = ap->a_loffset - base_offset;
3169 KKASSERT(*ap->a_runb >= 0);
3172 *ap->a_runp = last_offset - ap->a_loffset;
3173 KKASSERT(*ap->a_runp >= 0);
3181 * Write to a regular file. Because this is a strategy call the OS is
3182 * trying to actually get data onto the media.
3186 hammer_vop_strategy_write(struct vop_strategy_args *ap)
3188 hammer_record_t record;
3193 int blksize __debugvar;
3199 ip = ap->a_vp->v_data;
3202 blksize = hammer_blocksize(bio->bio_offset);
3203 KKASSERT(bp->b_bufsize == blksize);
3205 if (ip->flags & HAMMER_INODE_RO) {
3206 bp->b_error = EROFS;
3207 bp->b_flags |= B_ERROR;
3212 lwkt_gettoken(&hmp->fs_token);
3215 * Disallow swapcache operation on the vnode buffer if double
3216 * buffering is enabled, the swapcache will get the data via
3217 * the block device buffer.
3219 if (hammer_double_buffer)
3220 bp->b_flags |= B_NOTMETA;
3223 * Interlock with inode destruction (no in-kernel or directory
3224 * topology visibility). If we queue new IO while trying to
3225 * destroy the inode we can deadlock the vtrunc call in
3226 * hammer_inode_unloadable_check().
3228 * Besides, there's no point flushing a bp associated with an
3229 * inode that is being destroyed on-media and has no kernel
3232 if ((ip->flags | ip->sync_flags) &
3233 (HAMMER_INODE_DELETING|HAMMER_INODE_DELETED)) {
3236 lwkt_reltoken(&hmp->fs_token);
3241 * Reserve space and issue a direct-write from the front-end.
3242 * NOTE: The direct_io code will hammer_bread/bcopy smaller
3245 * An in-memory record will be installed to reference the storage
3246 * until the flusher can get to it.
3248 * Since we own the high level bio the front-end will not try to
3249 * do a direct-read until the write completes.
3251 * NOTE: The only time we do not reserve a full-sized buffers
3252 * worth of data is if the file is small. We do not try to
3253 * allocate a fragment (from the small-data zone) at the end of
3254 * an otherwise large file as this can lead to wildly separated
3257 KKASSERT((bio->bio_offset & HAMMER_BUFMASK) == 0);
3258 KKASSERT(bio->bio_offset < ip->ino_data.size);
3259 if (bio->bio_offset || ip->ino_data.size > HAMMER_HBUFSIZE)
3260 bytes = bp->b_bufsize;
3262 bytes = HAMMER_DATA_DOALIGN_WITH(int, ip->ino_data.size);
3264 record = hammer_ip_add_bulk(ip, bio->bio_offset, bp->b_data,
3268 * B_VFSFLAG1 indicates that a REDO_WRITE entry was generated
3269 * in hammer_vop_write(). We must flag the record so the proper
3270 * REDO_TERM_WRITE entry is generated during the flush.
3273 if (bp->b_flags & B_VFSFLAG1) {
3274 record->flags |= HAMMER_RECF_REDO;
3275 bp->b_flags &= ~B_VFSFLAG1;
3277 if (record->flags & HAMMER_RECF_DEDUPED) {
3279 hammer_ip_replace_bulk(hmp, record);
3282 hammer_io_direct_write(hmp, bio, record);
3284 if (ip->rsv_recs > 1 && hmp->rsv_recs > hammer_limit_recs)
3285 hammer_flush_inode(ip, 0);
3287 bp->b_bio2.bio_offset = NOOFFSET;
3288 bp->b_error = error;
3289 bp->b_flags |= B_ERROR;
3292 lwkt_reltoken(&hmp->fs_token);
3297 * dounlink - disconnect a directory entry
3299 * XXX whiteout support not really in yet
3302 hammer_dounlink(hammer_transaction_t trans, struct nchandle *nch,
3303 struct vnode *dvp, struct ucred *cred,
3304 int flags, int isdir)
3306 struct namecache *ncp;
3310 struct hammer_cursor cursor;
3312 uint32_t max_iterations;
3316 * Calculate the namekey and setup the key range for the scan. This
3317 * works kinda like a chained hash table where the lower 32 bits
3318 * of the namekey synthesize the chain.
3320 * The key range is inclusive of both key_beg and key_end.
3326 if (dip->flags & HAMMER_INODE_RO)
3329 namekey = hammer_direntry_namekey(dip, ncp->nc_name, ncp->nc_nlen,
3332 hammer_init_cursor(trans, &cursor, &dip->cache[1], dip);
3333 cursor.key_beg.localization = dip->obj_localization |
3334 hammer_dir_localization(dip);
3335 cursor.key_beg.obj_id = dip->obj_id;
3336 cursor.key_beg.key = namekey;
3337 cursor.key_beg.create_tid = 0;
3338 cursor.key_beg.delete_tid = 0;
3339 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
3340 cursor.key_beg.obj_type = 0;
3342 cursor.key_end = cursor.key_beg;
3343 cursor.key_end.key += max_iterations;
3344 cursor.asof = dip->obj_asof;
3345 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
3348 * Scan all matching records (the chain), locate the one matching
3349 * the requested path component. info->last_error contains the
3350 * error code on search termination and could be 0, ENOENT, or
3353 * The hammer_ip_*() functions merge in-memory records with on-disk
3354 * records for the purposes of the search.
3356 error = hammer_ip_first(&cursor);
3358 while (error == 0) {
3359 error = hammer_ip_resolve_data(&cursor);
3362 nlen = cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF;
3364 if (ncp->nc_nlen == nlen &&
3365 bcmp(ncp->nc_name, cursor.data->entry.name, nlen) == 0) {
3368 error = hammer_ip_next(&cursor);
3372 * If all is ok we have to get the inode so we can adjust nlinks.
3373 * To avoid a deadlock with the flusher we must release the inode
3374 * lock on the directory when acquiring the inode for the entry.
3376 * If the target is a directory, it must be empty.
3379 hammer_unlock(&cursor.ip->lock);
3380 ip = hammer_get_inode(trans, dip, cursor.data->entry.obj_id,
3382 cursor.data->entry.localization,
3384 hammer_lock_sh(&cursor.ip->lock);
3385 if (error == ENOENT) {
3386 hkprintf("WARNING: Removing dirent w/missing inode "
3388 "\tobj_id = %016jx\n",
3390 (intmax_t)cursor.data->entry.obj_id);
3395 * If isdir >= 0 we validate that the entry is or is not a
3396 * directory. If isdir < 0 we don't care.
3398 if (error == 0 && isdir >= 0 && ip) {
3400 ip->ino_data.obj_type != HAMMER_OBJTYPE_DIRECTORY) {
3402 } else if (isdir == 0 &&
3403 ip->ino_data.obj_type == HAMMER_OBJTYPE_DIRECTORY) {
3409 * If we are trying to remove a directory the directory must
3412 * The check directory code can loop and deadlock/retry. Our
3413 * own cursor's node locks must be released to avoid a 3-way
3414 * deadlock with the flusher if the check directory code
3417 * If any changes whatsoever have been made to the cursor
3418 * set EDEADLK and retry.
3420 * WARNING: See warnings in hammer_unlock_cursor()
3423 if (error == 0 && ip && ip->ino_data.obj_type ==
3424 HAMMER_OBJTYPE_DIRECTORY) {
3425 hammer_unlock_cursor(&cursor);
3426 error = hammer_ip_check_directory_empty(trans, ip);
3427 hammer_lock_cursor(&cursor);
3428 if (cursor.flags & HAMMER_CURSOR_RETEST) {
3429 hkprintf("Warning: avoided deadlock "
3437 * Delete the directory entry.
3439 * WARNING: hammer_ip_del_direntry() may have to terminate
3440 * the cursor to avoid a deadlock. It is ok to call
3441 * hammer_done_cursor() twice.
3444 error = hammer_ip_del_direntry(trans, &cursor,
3447 hammer_done_cursor(&cursor);
3450 * Tell the namecache that we are now unlinked.
3455 * NOTE: ip->vp, if non-NULL, cannot be directly
3456 * referenced without formally acquiring the
3457 * vp since the vp might have zero refs on it,
3458 * or in the middle of a reclaim, etc.
3460 * NOTE: The cache_setunresolved() can rip the vp
3461 * out from under us since the vp may not have
3462 * any refs, in which case ip->vp will be NULL
3465 while (ip && ip->vp) {
3468 error = hammer_get_vnode(ip, &vp);
3469 if (error == 0 && vp) {
3471 hammer_knote(ip->vp, NOTE_DELETE);
3474 * Don't do this, it can deadlock
3475 * on concurrent rm's of hardlinks.
3476 * Shouldn't be needed any more.
3478 cache_inval_vp(ip->vp, CINV_DESTROY);
3483 hdkprintf("ip/vp race1 avoided\n");
3487 hammer_rel_inode(ip, 0);
3489 hammer_done_cursor(&cursor);
3491 if (error == EDEADLK)
3497 /************************************************************************
3498 * FIFO AND SPECFS OPS *
3499 ************************************************************************
3503 hammer_vop_fifoclose (struct vop_close_args *ap)
3505 /* XXX update itimes */
3506 return (VOCALL(&fifo_vnode_vops, &ap->a_head));
3510 hammer_vop_fiforead (struct vop_read_args *ap)
3514 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
3515 /* XXX update access time */
3520 hammer_vop_fifowrite (struct vop_write_args *ap)
3524 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
3525 /* XXX update access time */
3531 hammer_vop_fifokqfilter(struct vop_kqfilter_args *ap)
3535 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
3537 error = hammer_vop_kqfilter(ap);
3541 /************************************************************************
3543 ************************************************************************
3546 static void filt_hammerdetach(struct knote *kn);
3547 static int filt_hammerread(struct knote *kn, long hint);
3548 static int filt_hammerwrite(struct knote *kn, long hint);
3549 static int filt_hammervnode(struct knote *kn, long hint);
3551 static struct filterops hammerread_filtops =
3552 { FILTEROP_ISFD | FILTEROP_MPSAFE,
3553 NULL, filt_hammerdetach, filt_hammerread };
3554 static struct filterops hammerwrite_filtops =
3555 { FILTEROP_ISFD | FILTEROP_MPSAFE,
3556 NULL, filt_hammerdetach, filt_hammerwrite };
3557 static struct filterops hammervnode_filtops =
3558 { FILTEROP_ISFD | FILTEROP_MPSAFE,
3559 NULL, filt_hammerdetach, filt_hammervnode };
3563 hammer_vop_kqfilter(struct vop_kqfilter_args *ap)
3565 struct vnode *vp = ap->a_vp;
3566 struct knote *kn = ap->a_kn;
3568 switch (kn->kn_filter) {
3570 kn->kn_fop = &hammerread_filtops;
3573 kn->kn_fop = &hammerwrite_filtops;
3576 kn->kn_fop = &hammervnode_filtops;
3579 return (EOPNOTSUPP);
3582 kn->kn_hook = (caddr_t)vp;
3584 knote_insert(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
3590 filt_hammerdetach(struct knote *kn)
3592 struct vnode *vp = (void *)kn->kn_hook;
3594 knote_remove(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
3598 filt_hammerread(struct knote *kn, long hint)
3600 struct vnode *vp = (void *)kn->kn_hook;
3601 hammer_inode_t ip = VTOI(vp);
3602 hammer_mount_t hmp = ip->hmp;
3605 if (hint == NOTE_REVOKE) {
3606 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
3609 lwkt_gettoken(&hmp->fs_token); /* XXX use per-ip-token */
3610 off = ip->ino_data.size - kn->kn_fp->f_offset;
3611 kn->kn_data = (off < INTPTR_MAX) ? off : INTPTR_MAX;
3612 lwkt_reltoken(&hmp->fs_token);
3613 if (kn->kn_sfflags & NOTE_OLDAPI)
3615 return (kn->kn_data != 0);
3619 filt_hammerwrite(struct knote *kn, long hint)
3621 if (hint == NOTE_REVOKE)
3622 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
3628 filt_hammervnode(struct knote *kn, long hint)
3630 if (kn->kn_sfflags & hint)
3631 kn->kn_fflags |= hint;
3632 if (hint == NOTE_REVOKE) {
3633 kn->kn_flags |= (EV_EOF | EV_NODATA);
3636 return (kn->kn_fflags != 0);