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,
415 blksize, uio->uio_resid,
416 seqcount * MAXBSIZE, &bp);
418 error = breadnx(ap->a_vp, base_offset, blksize,
426 if ((hammer_debug_io & 0x0001) && (bp->b_flags & B_IOISSUED)) {
427 hdkprintf("zone2_offset %016jx read file %016jx@%016jx\n",
428 (intmax_t)bp->b_bio2.bio_offset,
429 (intmax_t)ip->obj_id,
430 (intmax_t)bp->b_loffset);
432 bp->b_flags &= ~B_IOISSUED;
433 if (blksize == HAMMER_XBUFSIZE)
434 bp->b_flags |= B_CLUSTEROK;
436 n = blksize - offset;
437 if (n > uio->uio_resid)
439 if (n > ip->ino_data.size - uio->uio_offset)
440 n = (int)(ip->ino_data.size - uio->uio_offset);
443 * Set B_AGE, data has a lower priority than meta-data.
445 * Use a hold/unlock/drop sequence to run the uiomove
446 * with the buffer unlocked, avoiding deadlocks against
447 * read()s on mmap()'d spaces.
449 bp->b_flags |= B_AGE;
450 error = uiomovebp(bp, (char *)bp->b_data + offset, n, uio);
455 hammer_stats_file_read += n;
461 * Try to update the atime with just the inode lock for maximum
462 * concurrency. If we can't shortcut it we have to get the full
465 if (got_trans == 0 && hammer_update_atime_quick(ip) < 0) {
466 hammer_start_transaction(&trans, ip->hmp);
471 if ((ip->flags & HAMMER_INODE_RO) == 0 &&
472 (ip->hmp->mp->mnt_flag & MNT_NOATIME) == 0) {
473 lwkt_gettoken(&hmp->fs_token);
474 ip->ino_data.atime = trans.time;
475 hammer_modify_inode(&trans, ip, HAMMER_INODE_ATIME);
476 hammer_done_transaction(&trans);
477 lwkt_reltoken(&hmp->fs_token);
479 hammer_done_transaction(&trans);
486 * hammer_vop_write { vp, uio, ioflag, cred }
490 hammer_vop_write(struct vop_write_args *ap)
492 struct hammer_transaction trans;
508 if (ap->a_vp->v_type != VREG)
514 seqcount = ap->a_ioflag >> 16;
516 if (ip->flags & HAMMER_INODE_RO)
520 * Create a transaction to cover the operations we perform.
522 hammer_start_transaction(&trans, hmp);
528 if (ap->a_ioflag & IO_APPEND)
529 uio->uio_offset = ip->ino_data.size;
532 * Check for illegal write offsets. Valid range is 0...2^63-1.
534 * NOTE: the base_off assignment is required to work around what
535 * I consider to be a GCC-4 optimization bug.
537 if (uio->uio_offset < 0) {
538 hammer_done_transaction(&trans);
541 base_offset = uio->uio_offset + uio->uio_resid; /* work around gcc-4 */
542 if (uio->uio_resid > 0 && base_offset <= uio->uio_offset) {
543 hammer_done_transaction(&trans);
547 if (uio->uio_resid > 0 && (td = uio->uio_td) != NULL && td->td_proc &&
548 base_offset > td->td_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
549 hammer_done_transaction(&trans);
550 lwpsignal(td->td_proc, td->td_lwp, SIGXFSZ);
555 * If reading or writing a huge amount of data we have to break
556 * atomicy and allow the operation to be interrupted by a signal
557 * or it can DOS the machine.
559 * Preset redo_count so we stop generating REDOs earlier if the
562 * redo_count is heuristical, SMP races are ok
564 bigwrite = (uio->uio_resid > 100 * 1024 * 1024);
565 if ((ip->flags & HAMMER_INODE_REDO) &&
566 ip->redo_count < hammer_limit_redo) {
567 ip->redo_count += uio->uio_resid;
571 * Access the data typically in HAMMER_BUFSIZE blocks via the
572 * buffer cache, but HAMMER may use a variable block size based
575 while (uio->uio_resid > 0) {
583 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_WRITE)) != 0)
585 if (bigwrite && (error = hammer_signal_check(hmp)) != 0)
588 blksize = hammer_blocksize(uio->uio_offset);
591 * Control the number of pending records associated with
592 * this inode. If too many have accumulated start a
593 * flush. Try to maintain a pipeline with the flusher.
595 * NOTE: It is possible for other sources to grow the
596 * records but not necessarily issue another flush,
597 * so use a timeout and ensure that a re-flush occurs.
599 if (ip->rsv_recs >= hammer_limit_inode_recs) {
600 lwkt_gettoken(&hmp->fs_token);
601 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
602 while (ip->rsv_recs >= hammer_limit_inode_recs * 2) {
603 ip->flags |= HAMMER_INODE_RECSW;
604 tsleep(&ip->rsv_recs, 0, "hmrwww", hz);
605 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
607 lwkt_reltoken(&hmp->fs_token);
611 * Do not allow HAMMER to blow out the buffer cache. Very
612 * large UIOs can lockout other processes due to bwillwrite()
615 * The hammer inode is not locked during these operations.
616 * The vnode is locked which can interfere with the pageout
617 * daemon for non-UIO_NOCOPY writes but should not interfere
618 * with the buffer cache. Even so, we cannot afford to
619 * allow the pageout daemon to build up too many dirty buffer
622 * Only call this if we aren't being recursively called from
623 * a virtual disk device (vn), else we may deadlock.
625 if ((ap->a_ioflag & IO_RECURSE) == 0)
629 * Calculate the blocksize at the current offset and figure
630 * out how much we can actually write.
632 blkmask = blksize - 1;
633 offset = (int)uio->uio_offset & blkmask;
634 base_offset = uio->uio_offset & ~(int64_t)blkmask;
635 n = blksize - offset;
636 if (n > uio->uio_resid) {
642 nsize = uio->uio_offset + n;
643 if (nsize > ip->ino_data.size) {
644 if (uio->uio_offset > ip->ino_data.size)
648 nvextendbuf(ap->a_vp,
651 hammer_blocksize(ip->ino_data.size),
652 hammer_blocksize(nsize),
653 hammer_blockoff(ip->ino_data.size),
654 hammer_blockoff(nsize),
657 kflags |= NOTE_EXTEND;
660 if (uio->uio_segflg == UIO_NOCOPY) {
662 * Issuing a write with the same data backing the
663 * buffer. Instantiate the buffer to collect the
664 * backing vm pages, then read-in any missing bits.
666 * This case is used by vop_stdputpages().
668 bp = getblk(ap->a_vp, base_offset,
669 blksize, GETBLK_BHEAVY, 0);
670 if ((bp->b_flags & B_CACHE) == 0) {
672 error = bread(ap->a_vp, base_offset,
675 } else if (offset == 0 && uio->uio_resid >= blksize) {
677 * Even though we are entirely overwriting the buffer
678 * we may still have to zero it out to avoid a
679 * mmap/write visibility issue.
681 bp = getblk(ap->a_vp, base_offset, blksize, GETBLK_BHEAVY, 0);
682 if ((bp->b_flags & B_CACHE) == 0)
684 } else if (base_offset >= ip->ino_data.size) {
686 * If the base offset of the buffer is beyond the
687 * file EOF, we don't have to issue a read.
689 bp = getblk(ap->a_vp, base_offset,
690 blksize, GETBLK_BHEAVY, 0);
694 * Partial overwrite, read in any missing bits then
695 * replace the portion being written.
697 error = bread(ap->a_vp, base_offset, blksize, &bp);
702 error = uiomovebp(bp, bp->b_data + offset, n, uio);
704 lwkt_gettoken(&hmp->fs_token);
707 * Generate REDO records if enabled and redo_count will not
708 * exceeded the limit.
710 * If redo_count exceeds the limit we stop generating records
711 * and clear HAMMER_INODE_REDO. This will cause the next
712 * fsync() to do a full meta-data sync instead of just an
713 * UNDO/REDO fifo update.
715 * When clearing HAMMER_INODE_REDO any pre-existing REDOs
716 * will still be tracked. The tracks will be terminated
717 * when the related meta-data (including possible data
718 * modifications which are not tracked via REDO) is
721 if ((ip->flags & HAMMER_INODE_REDO) && error == 0) {
722 if (ip->redo_count < hammer_limit_redo) {
723 bp->b_flags |= B_VFSFLAG1;
724 error = hammer_generate_redo(&trans, ip,
725 base_offset + offset,
730 ip->flags &= ~HAMMER_INODE_REDO;
735 * If we screwed up we have to undo any VM size changes we
741 nvtruncbuf(ap->a_vp, ip->ino_data.size,
742 hammer_blocksize(ip->ino_data.size),
743 hammer_blockoff(ip->ino_data.size),
746 lwkt_reltoken(&hmp->fs_token);
749 kflags |= NOTE_WRITE;
750 hammer_stats_file_write += n;
751 if (blksize == HAMMER_XBUFSIZE)
752 bp->b_flags |= B_CLUSTEROK;
753 if (ip->ino_data.size < uio->uio_offset) {
754 ip->ino_data.size = uio->uio_offset;
755 flags = HAMMER_INODE_SDIRTY;
759 ip->ino_data.mtime = trans.time;
760 flags |= HAMMER_INODE_MTIME | HAMMER_INODE_BUFS;
761 hammer_modify_inode(&trans, ip, flags);
764 * Once we dirty the buffer any cached zone-X offset
765 * becomes invalid. HAMMER NOTE: no-history mode cannot
766 * allow overwriting over the same data sector unless
767 * we provide UNDOs for the old data, which we don't.
769 bp->b_bio2.bio_offset = NOOFFSET;
771 lwkt_reltoken(&hmp->fs_token);
774 * Final buffer disposition.
776 * Because meta-data updates are deferred, HAMMER is
777 * especially sensitive to excessive bdwrite()s because
778 * the I/O stream is not broken up by disk reads. So the
779 * buffer cache simply cannot keep up.
781 * WARNING! blksize is variable. cluster_write() is
782 * expected to not blow up if it encounters
783 * buffers that do not match the passed blksize.
785 * NOTE! Hammer shouldn't need to bawrite()/cluster_write().
786 * The ip->rsv_recs check should burst-flush the data.
787 * If we queue it immediately the buf could be left
788 * locked on the device queue for a very long time.
790 * However, failing to flush a dirty buffer out when
791 * issued from the pageout daemon can result in a low
792 * memory deadlock against bio_page_alloc(), so we
793 * have to bawrite() on IO_ASYNC as well.
795 * NOTE! To avoid degenerate stalls due to mismatched block
796 * sizes we only honor IO_DIRECT on the write which
797 * abuts the end of the buffer. However, we must
798 * honor IO_SYNC in case someone is silly enough to
799 * configure a HAMMER file as swap, or when HAMMER
800 * is serving NFS (for commits). Ick ick.
802 bp->b_flags |= B_AGE;
803 if (blksize == HAMMER_XBUFSIZE)
804 bp->b_flags |= B_CLUSTEROK;
806 if (ap->a_ioflag & IO_SYNC) {
808 } else if ((ap->a_ioflag & IO_DIRECT) && endofblk) {
810 } else if (ap->a_ioflag & IO_ASYNC) {
812 } else if (hammer_cluster_enable &&
813 !(ap->a_vp->v_mount->mnt_flag & MNT_NOCLUSTERW)) {
814 if (base_offset < HAMMER_XDEMARC)
815 cluster_eof = hammer_blockdemarc(base_offset,
818 cluster_eof = ip->ino_data.size;
819 cluster_write(bp, cluster_eof, blksize, seqcount);
824 hammer_done_transaction(&trans);
825 hammer_knote(ap->a_vp, kflags);
831 * hammer_vop_access { vp, mode, cred }
833 * MPSAFE - does not require fs_token
837 hammer_vop_access(struct vop_access_args *ap)
839 hammer_inode_t ip = VTOI(ap->a_vp);
844 ++hammer_stats_file_iopsr;
845 uid = hammer_to_unix_xid(&ip->ino_data.uid);
846 gid = hammer_to_unix_xid(&ip->ino_data.gid);
848 error = vop_helper_access(ap, uid, gid, ip->ino_data.mode,
849 ip->ino_data.uflags);
854 * hammer_vop_advlock { vp, id, op, fl, flags }
856 * MPSAFE - does not require fs_token
860 hammer_vop_advlock(struct vop_advlock_args *ap)
862 hammer_inode_t ip = VTOI(ap->a_vp);
864 return (lf_advlock(ap, &ip->advlock, ip->ino_data.size));
868 * hammer_vop_close { vp, fflag }
870 * We can only sync-on-close for normal closes. XXX disabled for now.
874 hammer_vop_close(struct vop_close_args *ap)
877 struct vnode *vp = ap->a_vp;
878 hammer_inode_t ip = VTOI(vp);
880 if (ip->flags & (HAMMER_INODE_CLOSESYNC|HAMMER_INODE_CLOSEASYNC)) {
881 if (vn_islocked(vp) == LK_EXCLUSIVE &&
882 (vp->v_flag & (VINACTIVE|VRECLAIMED)) == 0) {
883 if (ip->flags & HAMMER_INODE_CLOSESYNC)
886 waitfor = MNT_NOWAIT;
887 ip->flags &= ~(HAMMER_INODE_CLOSESYNC |
888 HAMMER_INODE_CLOSEASYNC);
889 VOP_FSYNC(vp, MNT_NOWAIT, waitfor);
893 return (vop_stdclose(ap));
897 * hammer_vop_ncreate { nch, dvp, vpp, cred, vap }
899 * The operating system has already ensured that the directory entry
900 * does not exist and done all appropriate namespace locking.
904 hammer_vop_ncreate(struct vop_ncreate_args *ap)
906 struct hammer_transaction trans;
909 struct nchandle *nch;
914 dip = VTOI(ap->a_dvp);
917 if (dip->flags & HAMMER_INODE_RO)
919 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0)
923 * Create a transaction to cover the operations we perform.
925 lwkt_gettoken(&hmp->fs_token);
926 hammer_start_transaction(&trans, hmp);
927 ++hammer_stats_file_iopsw;
930 * Create a new filesystem object of the requested type. The
931 * returned inode will be referenced and shared-locked to prevent
932 * it from being moved to the flusher.
934 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
935 dip, nch->ncp->nc_name, nch->ncp->nc_nlen,
938 hkprintf("hammer_create_inode error %d\n", error);
939 hammer_done_transaction(&trans);
941 lwkt_reltoken(&hmp->fs_token);
946 * Add the new filesystem object to the directory. This will also
947 * bump the inode's link count.
949 error = hammer_ip_add_direntry(&trans, dip,
950 nch->ncp->nc_name, nch->ncp->nc_nlen,
953 hkprintf("hammer_ip_add_direntry error %d\n", error);
959 hammer_rel_inode(nip, 0);
960 hammer_done_transaction(&trans);
963 error = hammer_get_vnode(nip, ap->a_vpp);
964 hammer_done_transaction(&trans);
965 hammer_rel_inode(nip, 0);
967 cache_setunresolved(ap->a_nch);
968 cache_setvp(ap->a_nch, *ap->a_vpp);
970 hammer_knote(ap->a_dvp, NOTE_WRITE);
972 lwkt_reltoken(&hmp->fs_token);
977 * hammer_vop_getattr { vp, vap }
979 * Retrieve an inode's attribute information. When accessing inodes
980 * historically we fake the atime field to ensure consistent results.
981 * The atime field is stored in the B-Tree element and allowed to be
982 * updated without cycling the element.
984 * MPSAFE - does not require fs_token
988 hammer_vop_getattr(struct vop_getattr_args *ap)
990 hammer_inode_t ip = VTOI(ap->a_vp);
991 struct vattr *vap = ap->a_vap;
994 * We want the fsid to be different when accessing a filesystem
995 * with different as-of's so programs like diff don't think
996 * the files are the same.
998 * We also want the fsid to be the same when comparing snapshots,
999 * or when comparing mirrors (which might be backed by different
1000 * physical devices). HAMMER fsids are based on the PFS's
1001 * shared_uuid field.
1003 * XXX there is a chance of collision here. The va_fsid reported
1004 * by stat is different from the more involved fsid used in the
1007 ++hammer_stats_file_iopsr;
1008 hammer_lock_sh(&ip->lock);
1009 vap->va_fsid = ip->pfsm->fsid_udev ^ (uint32_t)ip->obj_asof ^
1010 (uint32_t)(ip->obj_asof >> 32);
1012 vap->va_fileid = ip->ino_leaf.base.obj_id;
1013 vap->va_mode = ip->ino_data.mode;
1014 vap->va_nlink = ip->ino_data.nlinks;
1015 vap->va_uid = hammer_to_unix_xid(&ip->ino_data.uid);
1016 vap->va_gid = hammer_to_unix_xid(&ip->ino_data.gid);
1019 vap->va_size = ip->ino_data.size;
1022 * Special case for @@PFS softlinks. The actual size of the
1023 * expanded softlink is "@@0x%016llx:%05d" == 26 bytes.
1024 * or for MAX_TID is "@@-1:%05d" == 10 bytes.
1026 * Note that userspace hammer command does not allow users to
1027 * create a @@PFS softlink under an existing other PFS (id!=0)
1028 * so the ip localization here for @@PFS softlink is always 0.
1030 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_SOFTLINK &&
1031 ip->ino_data.size == 10 &&
1032 ip->obj_asof == HAMMER_MAX_TID &&
1033 ip->obj_localization == HAMMER_DEF_LOCALIZATION &&
1034 strncmp(ip->ino_data.ext.symlink, "@@PFS", 5) == 0) {
1035 if (hammer_is_pfs_slave(&ip->pfsm->pfsd))
1042 * We must provide a consistent atime and mtime for snapshots
1043 * so people can do a 'tar cf - ... | md5' on them and get
1044 * consistent results.
1046 if (ip->flags & HAMMER_INODE_RO) {
1047 hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_atime);
1048 hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_mtime);
1050 hammer_time_to_timespec(ip->ino_data.atime, &vap->va_atime);
1051 hammer_time_to_timespec(ip->ino_data.mtime, &vap->va_mtime);
1053 hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_ctime);
1054 vap->va_flags = ip->ino_data.uflags;
1055 vap->va_gen = 1; /* hammer inums are unique for all time */
1056 vap->va_blocksize = HAMMER_BUFSIZE;
1057 if (ip->ino_data.size >= HAMMER_XDEMARC) {
1058 vap->va_bytes = (ip->ino_data.size + HAMMER_XBUFMASK64) &
1060 } else if (ip->ino_data.size > HAMMER_HBUFSIZE) {
1061 vap->va_bytes = (ip->ino_data.size + HAMMER_BUFMASK64) &
1064 vap->va_bytes = (ip->ino_data.size + 15) & ~15;
1067 vap->va_type = hammer_get_vnode_type(ip->ino_data.obj_type);
1068 vap->va_filerev = 0; /* XXX */
1069 vap->va_uid_uuid = ip->ino_data.uid;
1070 vap->va_gid_uuid = ip->ino_data.gid;
1071 vap->va_fsid_uuid = ip->hmp->fsid;
1072 vap->va_vaflags = VA_UID_UUID_VALID | VA_GID_UUID_VALID |
1075 switch (ip->ino_data.obj_type) {
1076 case HAMMER_OBJTYPE_CDEV:
1077 case HAMMER_OBJTYPE_BDEV:
1078 vap->va_rmajor = ip->ino_data.rmajor;
1079 vap->va_rminor = ip->ino_data.rminor;
1084 hammer_unlock(&ip->lock);
1089 * hammer_vop_nresolve { nch, dvp, cred }
1091 * Locate the requested directory entry.
1095 hammer_vop_nresolve(struct vop_nresolve_args *ap)
1097 struct hammer_transaction trans;
1098 struct namecache *ncp;
1103 struct hammer_cursor cursor;
1112 uint32_t localization;
1113 uint32_t max_iterations;
1116 * Misc initialization, plus handle as-of name extensions. Look for
1117 * the '@@' extension. Note that as-of files and directories cannot
1120 dip = VTOI(ap->a_dvp);
1121 ncp = ap->a_nch->ncp;
1122 asof = dip->obj_asof;
1123 localization = dip->obj_localization; /* for code consistency */
1124 nlen = ncp->nc_nlen;
1125 flags = dip->flags & HAMMER_INODE_RO;
1129 lwkt_gettoken(&hmp->fs_token);
1130 hammer_simple_transaction(&trans, hmp);
1131 ++hammer_stats_file_iopsr;
1133 for (i = 0; i < nlen; ++i) {
1134 if (ncp->nc_name[i] == '@' && ncp->nc_name[i+1] == '@') {
1135 error = hammer_str_to_tid(ncp->nc_name + i + 2,
1136 &ispfs, &asof, &localization);
1141 if (asof != HAMMER_MAX_TID)
1142 flags |= HAMMER_INODE_RO;
1149 * If this is a PFS softlink we dive into the PFS
1151 if (ispfs && nlen == 0) {
1152 ip = hammer_get_inode(&trans, dip, HAMMER_OBJID_ROOT,
1156 error = hammer_get_vnode(ip, &vp);
1157 hammer_rel_inode(ip, 0);
1163 cache_setvp(ap->a_nch, vp);
1170 * If there is no path component the time extension is relative to dip.
1171 * e.g. "fubar/@@<snapshot>"
1173 * "." is handled by the kernel, but ".@@<snapshot>" is not.
1174 * e.g. "fubar/.@@<snapshot>"
1176 * ".." is handled by the kernel. We do not currently handle
1179 if (nlen == 0 || (nlen == 1 && ncp->nc_name[0] == '.')) {
1180 ip = hammer_get_inode(&trans, dip, dip->obj_id,
1181 asof, dip->obj_localization,
1184 error = hammer_get_vnode(ip, &vp);
1185 hammer_rel_inode(ip, 0);
1191 cache_setvp(ap->a_nch, vp);
1198 * Calculate the namekey and setup the key range for the scan. This
1199 * works kinda like a chained hash table where the lower 32 bits
1200 * of the namekey synthesize the chain.
1202 * The key range is inclusive of both key_beg and key_end.
1204 namekey = hammer_direntry_namekey(dip, ncp->nc_name, nlen,
1207 error = hammer_init_cursor(&trans, &cursor, &dip->cache[1], dip);
1208 cursor.key_beg.localization = dip->obj_localization |
1209 hammer_dir_localization(dip);
1210 cursor.key_beg.obj_id = dip->obj_id;
1211 cursor.key_beg.key = namekey;
1212 cursor.key_beg.create_tid = 0;
1213 cursor.key_beg.delete_tid = 0;
1214 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1215 cursor.key_beg.obj_type = 0;
1217 cursor.key_end = cursor.key_beg;
1218 cursor.key_end.key += max_iterations;
1220 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
1223 * Scan all matching records (the chain), locate the one matching
1224 * the requested path component.
1226 * The hammer_ip_*() functions merge in-memory records with on-disk
1227 * records for the purposes of the search.
1230 localization = HAMMER_DEF_LOCALIZATION;
1233 error = hammer_ip_first(&cursor);
1234 while (error == 0) {
1235 error = hammer_ip_resolve_data(&cursor);
1238 if (nlen == cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF &&
1239 bcmp(ncp->nc_name, cursor.data->entry.name, nlen) == 0) {
1240 obj_id = cursor.data->entry.obj_id;
1241 localization = cursor.data->entry.localization;
1244 error = hammer_ip_next(&cursor);
1247 hammer_done_cursor(&cursor);
1250 * Lookup the obj_id. This should always succeed. If it does not
1251 * the filesystem may be damaged and we return a dummy inode.
1254 ip = hammer_get_inode(&trans, dip, obj_id,
1257 if (error == ENOENT) {
1258 hkprintf("WARNING: Missing inode for dirent \"%s\"\n"
1259 "\tobj_id = %016jx, asof=%016jx, lo=%08x\n",
1261 (intmax_t)obj_id, (intmax_t)asof,
1264 ip = hammer_get_dummy_inode(&trans, dip, obj_id,
1269 error = hammer_get_vnode(ip, &vp);
1270 hammer_rel_inode(ip, 0);
1276 cache_setvp(ap->a_nch, vp);
1279 } else if (error == ENOENT) {
1280 cache_setvp(ap->a_nch, NULL);
1283 hammer_done_transaction(&trans);
1284 lwkt_reltoken(&hmp->fs_token);
1289 * hammer_vop_nlookupdotdot { dvp, vpp, cred }
1291 * Locate the parent directory of a directory vnode.
1293 * dvp is referenced but not locked. *vpp must be returned referenced and
1294 * locked. A parent_obj_id of 0 indicates that we are at the root.
1296 * NOTE: as-of sequences are not linked into the directory structure. If
1297 * we are at the root with a different asof then the mount point, reload
1298 * the same directory with the mount point's asof. I'm not sure what this
1299 * will do to NFS. We encode ASOF stamps in NFS file handles so it might not
1300 * get confused, but it hasn't been tested.
1304 hammer_vop_nlookupdotdot(struct vop_nlookupdotdot_args *ap)
1306 struct hammer_transaction trans;
1310 int64_t parent_obj_id;
1311 uint32_t parent_obj_localization;
1315 dip = VTOI(ap->a_dvp);
1316 asof = dip->obj_asof;
1320 * Whos are parent? This could be the root of a pseudo-filesystem
1321 * whos parent is in another localization domain.
1323 lwkt_gettoken(&hmp->fs_token);
1324 parent_obj_id = dip->ino_data.parent_obj_id;
1325 if (dip->obj_id == HAMMER_OBJID_ROOT)
1326 parent_obj_localization = HAMMER_DEF_LOCALIZATION;
1328 parent_obj_localization = dip->obj_localization;
1331 * It's probably a PFS root when dip->ino_data.parent_obj_id is 0.
1333 if (parent_obj_id == 0) {
1334 if (dip->obj_id == HAMMER_OBJID_ROOT &&
1335 asof != hmp->asof) {
1336 parent_obj_id = dip->obj_id;
1338 *ap->a_fakename = kmalloc(19, M_TEMP, M_WAITOK);
1339 ksnprintf(*ap->a_fakename, 19, "0x%016jx",
1340 (intmax_t)dip->obj_asof);
1343 lwkt_reltoken(&hmp->fs_token);
1348 hammer_simple_transaction(&trans, hmp);
1349 ++hammer_stats_file_iopsr;
1351 ip = hammer_get_inode(&trans, dip, parent_obj_id,
1352 asof, parent_obj_localization,
1353 dip->flags, &error);
1355 error = hammer_get_vnode(ip, ap->a_vpp);
1356 hammer_rel_inode(ip, 0);
1360 hammer_done_transaction(&trans);
1361 lwkt_reltoken(&hmp->fs_token);
1366 * hammer_vop_nlink { nch, dvp, vp, cred }
1370 hammer_vop_nlink(struct vop_nlink_args *ap)
1372 struct hammer_transaction trans;
1375 struct nchandle *nch;
1379 if (ap->a_dvp->v_mount != ap->a_vp->v_mount)
1383 dip = VTOI(ap->a_dvp);
1384 ip = VTOI(ap->a_vp);
1387 if (dip->obj_localization != ip->obj_localization)
1390 if (dip->flags & HAMMER_INODE_RO)
1392 if (ip->flags & HAMMER_INODE_RO)
1394 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0)
1398 * Create a transaction to cover the operations we perform.
1400 lwkt_gettoken(&hmp->fs_token);
1401 hammer_start_transaction(&trans, hmp);
1402 ++hammer_stats_file_iopsw;
1405 * Add the filesystem object to the directory. Note that neither
1406 * dip nor ip are referenced or locked, but their vnodes are
1407 * referenced. This function will bump the inode's link count.
1409 error = hammer_ip_add_direntry(&trans, dip,
1410 nch->ncp->nc_name, nch->ncp->nc_nlen,
1417 cache_setunresolved(nch);
1418 cache_setvp(nch, ap->a_vp);
1420 hammer_done_transaction(&trans);
1421 hammer_knote(ap->a_vp, NOTE_LINK);
1422 hammer_knote(ap->a_dvp, NOTE_WRITE);
1423 lwkt_reltoken(&hmp->fs_token);
1428 * hammer_vop_nmkdir { nch, dvp, vpp, cred, vap }
1430 * The operating system has already ensured that the directory entry
1431 * does not exist and done all appropriate namespace locking.
1435 hammer_vop_nmkdir(struct vop_nmkdir_args *ap)
1437 struct hammer_transaction trans;
1440 struct nchandle *nch;
1445 dip = VTOI(ap->a_dvp);
1448 if (dip->flags & HAMMER_INODE_RO)
1450 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0)
1454 * Create a transaction to cover the operations we perform.
1456 lwkt_gettoken(&hmp->fs_token);
1457 hammer_start_transaction(&trans, hmp);
1458 ++hammer_stats_file_iopsw;
1461 * Create a new filesystem object of the requested type. The
1462 * returned inode will be referenced but not locked.
1464 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
1465 dip, nch->ncp->nc_name, nch->ncp->nc_nlen,
1468 hammer_done_transaction(&trans);
1470 lwkt_reltoken(&hmp->fs_token);
1474 * Add the new filesystem object to the directory. This will also
1475 * bump the inode's link count.
1477 error = hammer_ip_add_direntry(&trans, dip,
1478 nch->ncp->nc_name, nch->ncp->nc_nlen,
1481 hkprintf("hammer_mkdir (add) error %d\n", error);
1487 hammer_rel_inode(nip, 0);
1490 error = hammer_get_vnode(nip, ap->a_vpp);
1491 hammer_rel_inode(nip, 0);
1493 cache_setunresolved(ap->a_nch);
1494 cache_setvp(ap->a_nch, *ap->a_vpp);
1497 hammer_done_transaction(&trans);
1499 hammer_knote(ap->a_dvp, NOTE_WRITE | NOTE_LINK);
1500 lwkt_reltoken(&hmp->fs_token);
1505 * hammer_vop_nmknod { nch, dvp, vpp, cred, vap }
1507 * The operating system has already ensured that the directory entry
1508 * does not exist and done all appropriate namespace locking.
1512 hammer_vop_nmknod(struct vop_nmknod_args *ap)
1514 struct hammer_transaction trans;
1517 struct nchandle *nch;
1522 dip = VTOI(ap->a_dvp);
1525 if (dip->flags & HAMMER_INODE_RO)
1527 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0)
1531 * Create a transaction to cover the operations we perform.
1533 lwkt_gettoken(&hmp->fs_token);
1534 hammer_start_transaction(&trans, hmp);
1535 ++hammer_stats_file_iopsw;
1538 * Create a new filesystem object of the requested type. The
1539 * returned inode will be referenced but not locked.
1541 * If mknod specifies a directory a pseudo-fs is created.
1543 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
1544 dip, nch->ncp->nc_name, nch->ncp->nc_nlen,
1547 hammer_done_transaction(&trans);
1549 lwkt_reltoken(&hmp->fs_token);
1554 * Add the new filesystem object to the directory. This will also
1555 * bump the inode's link count.
1557 error = hammer_ip_add_direntry(&trans, dip,
1558 nch->ncp->nc_name, nch->ncp->nc_nlen,
1565 hammer_rel_inode(nip, 0);
1568 error = hammer_get_vnode(nip, ap->a_vpp);
1569 hammer_rel_inode(nip, 0);
1571 cache_setunresolved(ap->a_nch);
1572 cache_setvp(ap->a_nch, *ap->a_vpp);
1575 hammer_done_transaction(&trans);
1577 hammer_knote(ap->a_dvp, NOTE_WRITE);
1578 lwkt_reltoken(&hmp->fs_token);
1583 * hammer_vop_open { vp, mode, cred, fp }
1585 * MPSAFE (does not require fs_token)
1589 hammer_vop_open(struct vop_open_args *ap)
1593 ++hammer_stats_file_iopsr;
1594 ip = VTOI(ap->a_vp);
1596 if ((ap->a_mode & FWRITE) && (ip->flags & HAMMER_INODE_RO))
1598 return(vop_stdopen(ap));
1602 * hammer_vop_print { vp }
1606 hammer_vop_print(struct vop_print_args *ap)
1612 * hammer_vop_readdir { vp, uio, cred, *eofflag, *ncookies, off_t **cookies }
1616 hammer_vop_readdir(struct vop_readdir_args *ap)
1618 struct hammer_transaction trans;
1619 struct hammer_cursor cursor;
1623 hammer_base_elm_t base;
1632 ++hammer_stats_file_iopsr;
1633 ip = VTOI(ap->a_vp);
1635 saveoff = uio->uio_offset;
1638 if (ap->a_ncookies) {
1639 ncookies = uio->uio_resid / 16 + 1;
1640 if (ncookies > 1024)
1642 cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
1650 lwkt_gettoken(&hmp->fs_token);
1651 hammer_simple_transaction(&trans, hmp);
1654 * Handle artificial entries
1656 * It should be noted that the minimum value for a directory
1657 * hash key on-media is 0x0000000100000000, so we can use anything
1658 * less then that to represent our 'special' key space.
1662 r = vop_write_dirent(&error, uio, ip->obj_id, DT_DIR, 1, ".");
1666 cookies[cookie_index] = saveoff;
1669 if (cookie_index == ncookies)
1673 if (ip->ino_data.parent_obj_id) {
1674 r = vop_write_dirent(&error, uio,
1675 ip->ino_data.parent_obj_id,
1678 r = vop_write_dirent(&error, uio,
1679 ip->obj_id, DT_DIR, 2, "..");
1684 cookies[cookie_index] = saveoff;
1687 if (cookie_index == ncookies)
1692 * Key range (begin and end inclusive) to scan. Directory keys
1693 * directly translate to a 64 bit 'seek' position.
1695 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
1696 cursor.key_beg.localization = ip->obj_localization |
1697 hammer_dir_localization(ip);
1698 cursor.key_beg.obj_id = ip->obj_id;
1699 cursor.key_beg.create_tid = 0;
1700 cursor.key_beg.delete_tid = 0;
1701 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1702 cursor.key_beg.obj_type = 0;
1703 cursor.key_beg.key = saveoff;
1705 cursor.key_end = cursor.key_beg;
1706 cursor.key_end.key = HAMMER_MAX_KEY;
1707 cursor.asof = ip->obj_asof;
1708 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
1710 error = hammer_ip_first(&cursor);
1712 while (error == 0) {
1713 error = hammer_ip_resolve_data(&cursor);
1716 base = &cursor.leaf->base;
1717 saveoff = base->key;
1718 KKASSERT(cursor.leaf->data_len > HAMMER_ENTRY_NAME_OFF);
1720 if (base->obj_id != ip->obj_id)
1721 hpanic("bad record at %p", cursor.node);
1723 dtype = hammer_get_dtype(cursor.leaf->base.obj_type);
1724 r = vop_write_dirent(
1725 &error, uio, cursor.data->entry.obj_id,
1727 cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF ,
1728 (void *)cursor.data->entry.name);
1733 cookies[cookie_index] = base->key;
1735 if (cookie_index == ncookies)
1737 error = hammer_ip_next(&cursor);
1739 hammer_done_cursor(&cursor);
1742 hammer_done_transaction(&trans);
1745 *ap->a_eofflag = (error == ENOENT);
1746 uio->uio_offset = saveoff;
1747 if (error && cookie_index == 0) {
1748 if (error == ENOENT)
1751 kfree(cookies, M_TEMP);
1752 *ap->a_ncookies = 0;
1753 *ap->a_cookies = NULL;
1756 if (error == ENOENT)
1759 *ap->a_ncookies = cookie_index;
1760 *ap->a_cookies = cookies;
1763 lwkt_reltoken(&hmp->fs_token);
1768 * hammer_vop_readlink { vp, uio, cred }
1772 hammer_vop_readlink(struct vop_readlink_args *ap)
1774 struct hammer_transaction trans;
1775 struct hammer_cursor cursor;
1779 uint32_t localization;
1780 hammer_pseudofs_inmem_t pfsm;
1783 ip = VTOI(ap->a_vp);
1786 lwkt_gettoken(&hmp->fs_token);
1789 * Shortcut if the symlink data was stuffed into ino_data.
1791 * Also expand special "@@PFS%05d" softlinks (expansion only
1792 * occurs for non-historical (current) accesses made from the
1793 * primary filesystem).
1795 * Note that userspace hammer command does not allow users to
1796 * create a @@PFS softlink under an existing other PFS (id!=0)
1797 * so the ip localization here for @@PFS softlink is always 0.
1799 if (ip->ino_data.size <= HAMMER_INODE_BASESYMLEN) {
1803 ptr = ip->ino_data.ext.symlink;
1804 bytes = (int)ip->ino_data.size;
1806 ip->obj_asof == HAMMER_MAX_TID &&
1807 ip->obj_localization == HAMMER_DEF_LOCALIZATION &&
1808 strncmp(ptr, "@@PFS", 5) == 0) {
1809 hammer_simple_transaction(&trans, hmp);
1810 bcopy(ptr + 5, buf, 5);
1812 localization = pfs_to_lo(strtoul(buf, NULL, 10));
1813 pfsm = hammer_load_pseudofs(&trans, localization,
1816 if (hammer_is_pfs_slave(&pfsm->pfsd)) {
1817 /* vap->va_size == 26 */
1818 ksnprintf(buf, sizeof(buf),
1820 (intmax_t)pfsm->pfsd.sync_end_tid,
1821 lo_to_pfs(localization));
1823 /* vap->va_size == 10 */
1824 ksnprintf(buf, sizeof(buf),
1826 lo_to_pfs(localization));
1829 bytes = strlen(buf);
1832 hammer_rel_pseudofs(hmp, pfsm);
1833 hammer_done_transaction(&trans);
1835 error = uiomove(ptr, bytes, ap->a_uio);
1836 lwkt_reltoken(&hmp->fs_token);
1843 hammer_simple_transaction(&trans, hmp);
1844 ++hammer_stats_file_iopsr;
1845 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
1848 * Key range (begin and end inclusive) to scan. Directory keys
1849 * directly translate to a 64 bit 'seek' position.
1851 cursor.key_beg.localization = ip->obj_localization |
1852 HAMMER_LOCALIZE_MISC;
1853 cursor.key_beg.obj_id = ip->obj_id;
1854 cursor.key_beg.create_tid = 0;
1855 cursor.key_beg.delete_tid = 0;
1856 cursor.key_beg.rec_type = HAMMER_RECTYPE_FIX;
1857 cursor.key_beg.obj_type = 0;
1858 cursor.key_beg.key = HAMMER_FIXKEY_SYMLINK;
1859 cursor.asof = ip->obj_asof;
1860 cursor.flags |= HAMMER_CURSOR_ASOF;
1862 error = hammer_ip_lookup(&cursor);
1864 error = hammer_ip_resolve_data(&cursor);
1866 KKASSERT(cursor.leaf->data_len >=
1867 HAMMER_SYMLINK_NAME_OFF);
1868 error = uiomove(cursor.data->symlink.name,
1869 cursor.leaf->data_len -
1870 HAMMER_SYMLINK_NAME_OFF,
1874 hammer_done_cursor(&cursor);
1875 hammer_done_transaction(&trans);
1876 lwkt_reltoken(&hmp->fs_token);
1881 * hammer_vop_nremove { nch, dvp, cred }
1885 hammer_vop_nremove(struct vop_nremove_args *ap)
1887 struct hammer_transaction trans;
1892 dip = VTOI(ap->a_dvp);
1895 if (hammer_nohistory(dip) == 0 &&
1896 (error = hammer_checkspace(hmp, HAMMER_CHKSPC_REMOVE)) != 0) {
1900 lwkt_gettoken(&hmp->fs_token);
1901 hammer_start_transaction(&trans, hmp);
1902 ++hammer_stats_file_iopsw;
1903 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp, ap->a_cred, 0, 0);
1904 hammer_done_transaction(&trans);
1906 hammer_knote(ap->a_dvp, NOTE_WRITE);
1907 lwkt_reltoken(&hmp->fs_token);
1912 * hammer_vop_nrename { fnch, tnch, fdvp, tdvp, cred }
1916 hammer_vop_nrename(struct vop_nrename_args *ap)
1918 struct hammer_transaction trans;
1919 struct namecache *fncp;
1920 struct namecache *tncp;
1921 hammer_inode_t fdip;
1922 hammer_inode_t tdip;
1925 struct hammer_cursor cursor;
1927 uint32_t max_iterations;
1930 if (ap->a_fdvp->v_mount != ap->a_tdvp->v_mount)
1932 if (ap->a_fdvp->v_mount != ap->a_fnch->ncp->nc_vp->v_mount)
1935 fdip = VTOI(ap->a_fdvp);
1936 tdip = VTOI(ap->a_tdvp);
1937 fncp = ap->a_fnch->ncp;
1938 tncp = ap->a_tnch->ncp;
1939 ip = VTOI(fncp->nc_vp);
1940 KKASSERT(ip != NULL);
1944 if (fdip->obj_localization != tdip->obj_localization)
1946 if (fdip->obj_localization != ip->obj_localization)
1949 if (fdip->flags & HAMMER_INODE_RO)
1951 if (tdip->flags & HAMMER_INODE_RO)
1953 if (ip->flags & HAMMER_INODE_RO)
1955 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0)
1958 lwkt_gettoken(&hmp->fs_token);
1959 hammer_start_transaction(&trans, hmp);
1960 ++hammer_stats_file_iopsw;
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 ++hammer_stats_file_iopsw;
2106 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp, ap->a_cred, 0, 1);
2107 hammer_done_transaction(&trans);
2109 hammer_knote(ap->a_dvp, NOTE_WRITE | NOTE_LINK);
2110 lwkt_reltoken(&hmp->fs_token);
2115 * hammer_vop_markatime { vp, cred }
2119 hammer_vop_markatime(struct vop_markatime_args *ap)
2121 struct hammer_transaction trans;
2125 ip = VTOI(ap->a_vp);
2126 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
2128 if (ip->flags & HAMMER_INODE_RO)
2131 if (hmp->mp->mnt_flag & MNT_NOATIME)
2133 lwkt_gettoken(&hmp->fs_token);
2134 hammer_start_transaction(&trans, hmp);
2135 ++hammer_stats_file_iopsw;
2137 ip->ino_data.atime = trans.time;
2138 hammer_modify_inode(&trans, ip, HAMMER_INODE_ATIME);
2139 hammer_done_transaction(&trans);
2140 hammer_knote(ap->a_vp, NOTE_ATTRIB);
2141 lwkt_reltoken(&hmp->fs_token);
2146 * hammer_vop_setattr { vp, vap, cred }
2150 hammer_vop_setattr(struct vop_setattr_args *ap)
2152 struct hammer_transaction trans;
2162 int64_t aligned_size;
2167 ip = ap->a_vp->v_data;
2172 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
2174 if (ip->flags & HAMMER_INODE_RO)
2176 if (hammer_nohistory(ip) == 0 &&
2177 (error = hammer_checkspace(hmp, HAMMER_CHKSPC_REMOVE)) != 0) {
2181 lwkt_gettoken(&hmp->fs_token);
2182 hammer_start_transaction(&trans, hmp);
2183 ++hammer_stats_file_iopsw;
2186 if (vap->va_flags != VNOVAL) {
2187 flags = ip->ino_data.uflags;
2188 error = vop_helper_setattr_flags(&flags, vap->va_flags,
2189 hammer_to_unix_xid(&ip->ino_data.uid),
2192 if (ip->ino_data.uflags != flags) {
2193 ip->ino_data.uflags = flags;
2194 ip->ino_data.ctime = trans.time;
2195 modflags |= HAMMER_INODE_DDIRTY;
2196 kflags |= NOTE_ATTRIB;
2198 if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) {
2205 if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) {
2209 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
2210 mode_t cur_mode = ip->ino_data.mode;
2211 uid_t cur_uid = hammer_to_unix_xid(&ip->ino_data.uid);
2212 gid_t cur_gid = hammer_to_unix_xid(&ip->ino_data.gid);
2216 error = vop_helper_chown(ap->a_vp, vap->va_uid, vap->va_gid,
2218 &cur_uid, &cur_gid, &cur_mode);
2220 hammer_guid_to_uuid(&uuid_uid, cur_uid);
2221 hammer_guid_to_uuid(&uuid_gid, cur_gid);
2222 if (bcmp(&uuid_uid, &ip->ino_data.uid,
2223 sizeof(uuid_uid)) ||
2224 bcmp(&uuid_gid, &ip->ino_data.gid,
2225 sizeof(uuid_gid)) ||
2226 ip->ino_data.mode != cur_mode) {
2227 ip->ino_data.uid = uuid_uid;
2228 ip->ino_data.gid = uuid_gid;
2229 ip->ino_data.mode = cur_mode;
2230 ip->ino_data.ctime = trans.time;
2231 modflags |= HAMMER_INODE_DDIRTY;
2233 kflags |= NOTE_ATTRIB;
2236 while (vap->va_size != VNOVAL && ip->ino_data.size != vap->va_size) {
2237 switch(ap->a_vp->v_type) {
2239 if (vap->va_size == ip->ino_data.size)
2243 * Log the operation if in fast-fsync mode or if
2244 * there are unterminated redo write records present.
2246 * The second check is needed so the recovery code
2247 * properly truncates write redos even if nominal
2248 * REDO operations is turned off due to excessive
2249 * writes, because the related records might be
2250 * destroyed and never lay down a TERM_WRITE.
2252 if ((ip->flags & HAMMER_INODE_REDO) ||
2253 (ip->flags & HAMMER_INODE_RDIRTY)) {
2254 error = hammer_generate_redo(&trans, ip,
2259 blksize = hammer_blocksize(vap->va_size);
2262 * XXX break atomicy, we can deadlock the backend
2263 * if we do not release the lock. Probably not a
2266 if (vap->va_size < ip->ino_data.size) {
2267 nvtruncbuf(ap->a_vp, vap->va_size,
2269 hammer_blockoff(vap->va_size),
2272 kflags |= NOTE_WRITE;
2274 nvextendbuf(ap->a_vp,
2277 hammer_blocksize(ip->ino_data.size),
2278 hammer_blocksize(vap->va_size),
2279 hammer_blockoff(ip->ino_data.size),
2280 hammer_blockoff(vap->va_size),
2283 kflags |= NOTE_WRITE | NOTE_EXTEND;
2285 ip->ino_data.size = vap->va_size;
2286 ip->ino_data.mtime = trans.time;
2287 /* XXX safe to use SDIRTY instead of DDIRTY here? */
2288 modflags |= HAMMER_INODE_MTIME | HAMMER_INODE_DDIRTY;
2291 * On-media truncation is cached in the inode until
2292 * the inode is synchronized. We must immediately
2293 * handle any frontend records.
2296 hammer_ip_frontend_trunc(ip, vap->va_size);
2297 if ((ip->flags & HAMMER_INODE_TRUNCATED) == 0) {
2298 ip->flags |= HAMMER_INODE_TRUNCATED;
2299 ip->trunc_off = vap->va_size;
2300 hammer_inode_dirty(ip);
2301 } else if (ip->trunc_off > vap->va_size) {
2302 ip->trunc_off = vap->va_size;
2308 * When truncating, nvtruncbuf() may have cleaned out
2309 * a portion of the last block on-disk in the buffer
2310 * cache. We must clean out any frontend records
2311 * for blocks beyond the new last block.
2313 aligned_size = (vap->va_size + (blksize - 1)) &
2314 ~(int64_t)(blksize - 1);
2315 if (truncating && vap->va_size < aligned_size) {
2316 aligned_size -= blksize;
2317 hammer_ip_frontend_trunc(ip, aligned_size);
2322 if ((ip->flags & HAMMER_INODE_TRUNCATED) == 0) {
2323 ip->flags |= HAMMER_INODE_TRUNCATED;
2324 ip->trunc_off = vap->va_size;
2325 hammer_inode_dirty(ip);
2326 } else if (ip->trunc_off > vap->va_size) {
2327 ip->trunc_off = vap->va_size;
2329 hammer_ip_frontend_trunc(ip, vap->va_size);
2330 ip->ino_data.size = vap->va_size;
2331 ip->ino_data.mtime = trans.time;
2332 modflags |= HAMMER_INODE_MTIME | HAMMER_INODE_DDIRTY;
2333 kflags |= NOTE_ATTRIB;
2341 if (vap->va_atime.tv_sec != VNOVAL) {
2342 ip->ino_data.atime = hammer_timespec_to_time(&vap->va_atime);
2343 modflags |= HAMMER_INODE_ATIME;
2344 kflags |= NOTE_ATTRIB;
2346 if (vap->va_mtime.tv_sec != VNOVAL) {
2347 ip->ino_data.mtime = hammer_timespec_to_time(&vap->va_mtime);
2348 modflags |= HAMMER_INODE_MTIME;
2349 kflags |= NOTE_ATTRIB;
2351 if (vap->va_mode != (mode_t)VNOVAL) {
2352 mode_t cur_mode = ip->ino_data.mode;
2353 uid_t cur_uid = hammer_to_unix_xid(&ip->ino_data.uid);
2354 gid_t cur_gid = hammer_to_unix_xid(&ip->ino_data.gid);
2356 error = vop_helper_chmod(ap->a_vp, vap->va_mode, ap->a_cred,
2357 cur_uid, cur_gid, &cur_mode);
2358 if (error == 0 && ip->ino_data.mode != cur_mode) {
2359 ip->ino_data.mode = cur_mode;
2360 ip->ino_data.ctime = trans.time;
2361 modflags |= HAMMER_INODE_DDIRTY;
2362 kflags |= NOTE_ATTRIB;
2367 hammer_modify_inode(&trans, ip, modflags);
2368 hammer_done_transaction(&trans);
2369 hammer_knote(ap->a_vp, kflags);
2370 lwkt_reltoken(&hmp->fs_token);
2375 * hammer_vop_nsymlink { nch, dvp, vpp, cred, vap, target }
2379 hammer_vop_nsymlink(struct vop_nsymlink_args *ap)
2381 struct hammer_transaction trans;
2384 hammer_record_t record;
2385 struct nchandle *nch;
2390 ap->a_vap->va_type = VLNK;
2393 dip = VTOI(ap->a_dvp);
2396 if (dip->flags & HAMMER_INODE_RO)
2398 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0)
2402 * Create a transaction to cover the operations we perform.
2404 lwkt_gettoken(&hmp->fs_token);
2405 hammer_start_transaction(&trans, hmp);
2406 ++hammer_stats_file_iopsw;
2409 * Create a new filesystem object of the requested type. The
2410 * returned inode will be referenced but not locked.
2413 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
2414 dip, nch->ncp->nc_name, nch->ncp->nc_nlen,
2417 hammer_done_transaction(&trans);
2419 lwkt_reltoken(&hmp->fs_token);
2424 * Add a record representing the symlink. symlink stores the link
2425 * as pure data, not a string, and is no \0 terminated.
2428 bytes = strlen(ap->a_target);
2430 if (bytes <= HAMMER_INODE_BASESYMLEN) {
2431 bcopy(ap->a_target, nip->ino_data.ext.symlink, bytes);
2433 record = hammer_alloc_mem_record(nip, bytes);
2434 record->type = HAMMER_MEM_RECORD_GENERAL;
2436 record->leaf.base.localization = nip->obj_localization |
2437 HAMMER_LOCALIZE_MISC;
2438 record->leaf.base.key = HAMMER_FIXKEY_SYMLINK;
2439 record->leaf.base.rec_type = HAMMER_RECTYPE_FIX;
2440 record->leaf.data_len = bytes;
2441 KKASSERT(HAMMER_SYMLINK_NAME_OFF == 0);
2442 bcopy(ap->a_target, record->data->symlink.name, bytes);
2443 error = hammer_ip_add_record(&trans, record);
2447 * Set the file size to the length of the link.
2450 nip->ino_data.size = bytes;
2451 hammer_modify_inode(&trans, nip, HAMMER_INODE_DDIRTY);
2455 error = hammer_ip_add_direntry(&trans, dip, nch->ncp->nc_name,
2456 nch->ncp->nc_nlen, nip);
2462 hammer_rel_inode(nip, 0);
2465 error = hammer_get_vnode(nip, ap->a_vpp);
2466 hammer_rel_inode(nip, 0);
2468 cache_setunresolved(ap->a_nch);
2469 cache_setvp(ap->a_nch, *ap->a_vpp);
2470 hammer_knote(ap->a_dvp, NOTE_WRITE);
2473 hammer_done_transaction(&trans);
2474 lwkt_reltoken(&hmp->fs_token);
2479 * hammer_vop_nwhiteout { nch, dvp, cred, flags }
2483 hammer_vop_nwhiteout(struct vop_nwhiteout_args *ap)
2485 struct hammer_transaction trans;
2490 dip = VTOI(ap->a_dvp);
2493 if (hammer_nohistory(dip) == 0 &&
2494 (error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0) {
2498 lwkt_gettoken(&hmp->fs_token);
2499 hammer_start_transaction(&trans, hmp);
2500 ++hammer_stats_file_iopsw;
2501 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp,
2502 ap->a_cred, ap->a_flags, -1);
2503 hammer_done_transaction(&trans);
2504 lwkt_reltoken(&hmp->fs_token);
2510 * hammer_vop_ioctl { vp, command, data, fflag, cred }
2514 hammer_vop_ioctl(struct vop_ioctl_args *ap)
2516 hammer_inode_t ip = ap->a_vp->v_data;
2517 hammer_mount_t hmp = ip->hmp;
2520 ++hammer_stats_file_iopsr;
2521 lwkt_gettoken(&hmp->fs_token);
2522 error = hammer_ioctl(ip, ap->a_command, ap->a_data,
2523 ap->a_fflag, ap->a_cred);
2524 lwkt_reltoken(&hmp->fs_token);
2530 hammer_vop_mountctl(struct vop_mountctl_args *ap)
2532 static const struct mountctl_opt extraopt[] = {
2533 { HMNT_NOHISTORY, "nohistory" },
2534 { HMNT_MASTERID, "master" },
2535 { HMNT_NOMIRROR, "nomirror" },
2546 mp = ap->a_head.a_ops->head.vv_mount;
2547 KKASSERT(mp->mnt_data != NULL);
2548 hmp = (hammer_mount_t)mp->mnt_data;
2550 lwkt_gettoken(&hmp->fs_token);
2553 case MOUNTCTL_SET_EXPORT:
2554 if (ap->a_ctllen != sizeof(struct export_args))
2557 error = hammer_vfs_export(mp, ap->a_op,
2558 (const struct export_args *)ap->a_ctl);
2560 case MOUNTCTL_MOUNTFLAGS:
2562 * Call standard mountctl VOP function
2563 * so we get user mount flags.
2565 error = vop_stdmountctl(ap);
2569 usedbytes = *ap->a_res;
2571 if (usedbytes > 0 && usedbytes < ap->a_buflen) {
2572 usedbytes += vfs_flagstostr(hmp->hflags, extraopt,
2574 ap->a_buflen - usedbytes,
2578 *ap->a_res += usedbytes;
2581 error = vop_stdmountctl(ap);
2584 lwkt_reltoken(&hmp->fs_token);
2589 * hammer_vop_strategy { vp, bio }
2591 * Strategy call, used for regular file read & write only. Note that the
2592 * bp may represent a cluster.
2594 * To simplify operation and allow better optimizations in the future,
2595 * this code does not make any assumptions with regards to buffer alignment
2600 hammer_vop_strategy(struct vop_strategy_args *ap)
2605 bp = ap->a_bio->bio_buf;
2609 error = hammer_vop_strategy_read(ap);
2612 error = hammer_vop_strategy_write(ap);
2615 bp->b_error = error = EINVAL;
2616 bp->b_flags |= B_ERROR;
2621 /* hammer_dump_dedup_cache(((hammer_inode_t)ap->a_vp->v_data)->hmp); */
2627 * Read from a regular file. Iterate the related records and fill in the
2628 * BIO/BUF. Gaps are zero-filled.
2630 * The support code in hammer_object.c should be used to deal with mixed
2631 * in-memory and on-disk records.
2633 * NOTE: Can be called from the cluster code with an oversized buf.
2639 hammer_vop_strategy_read(struct vop_strategy_args *ap)
2641 struct hammer_transaction trans;
2645 struct hammer_cursor cursor;
2646 hammer_base_elm_t base;
2647 hammer_off_t disk_offset;
2662 ip = ap->a_vp->v_data;
2666 * The zone-2 disk offset may have been set by the cluster code via
2667 * a BMAP operation, or else should be NOOFFSET.
2669 * Checking the high bits for a match against zone-2 should suffice.
2671 * In cases where a lot of data duplication is present it may be
2672 * more beneficial to drop through and doubule-buffer through the
2675 nbio = push_bio(bio);
2676 if (hammer_is_zone_large_data(nbio->bio_offset)) {
2677 if (hammer_double_buffer == 0) {
2678 lwkt_gettoken(&hmp->fs_token);
2679 error = hammer_io_direct_read(hmp, nbio, NULL);
2680 lwkt_reltoken(&hmp->fs_token);
2685 * Try to shortcut requests for double_buffer mode too.
2686 * Since this mode runs through the device buffer cache
2687 * only compatible buffer sizes (meaning those generated
2688 * by normal filesystem buffers) are legal.
2690 if (hammer_live_dedup == 0 && (bp->b_flags & B_PAGING) == 0) {
2691 lwkt_gettoken(&hmp->fs_token);
2692 error = hammer_io_indirect_read(hmp, nbio, NULL);
2693 lwkt_reltoken(&hmp->fs_token);
2699 * Well, that sucked. Do it the hard way. If all the stars are
2700 * aligned we may still be able to issue a direct-read.
2702 lwkt_gettoken(&hmp->fs_token);
2703 hammer_simple_transaction(&trans, hmp);
2704 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
2707 * Key range (begin and end inclusive) to scan. Note that the key's
2708 * stored in the actual records represent BASE+LEN, not BASE. The
2709 * first record containing bio_offset will have a key > bio_offset.
2711 cursor.key_beg.localization = ip->obj_localization |
2712 HAMMER_LOCALIZE_MISC;
2713 cursor.key_beg.obj_id = ip->obj_id;
2714 cursor.key_beg.create_tid = 0;
2715 cursor.key_beg.delete_tid = 0;
2716 cursor.key_beg.obj_type = 0;
2717 cursor.key_beg.key = bio->bio_offset + 1;
2718 cursor.asof = ip->obj_asof;
2719 cursor.flags |= HAMMER_CURSOR_ASOF;
2721 cursor.key_end = cursor.key_beg;
2722 KKASSERT(ip->ino_data.obj_type == HAMMER_OBJTYPE_REGFILE);
2724 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_DBFILE) {
2725 cursor.key_beg.rec_type = HAMMER_RECTYPE_DB;
2726 cursor.key_end.rec_type = HAMMER_RECTYPE_DB;
2727 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
2731 ran_end = bio->bio_offset + bp->b_bufsize;
2732 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
2733 cursor.key_end.rec_type = HAMMER_RECTYPE_DATA;
2734 tmp64 = ran_end + MAXPHYS + 1; /* work-around GCC-4 bug */
2735 if (tmp64 < ran_end)
2736 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
2738 cursor.key_end.key = ran_end + MAXPHYS + 1;
2740 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
2743 * Set NOSWAPCACHE for cursor data extraction if double buffering
2744 * is disabled or (if the file is not marked cacheable via chflags
2745 * and vm.swapcache_use_chflags is enabled).
2747 if (hammer_double_buffer == 0 ||
2748 ((ap->a_vp->v_flag & VSWAPCACHE) == 0 &&
2749 vm_swapcache_use_chflags)) {
2750 cursor.flags |= HAMMER_CURSOR_NOSWAPCACHE;
2753 error = hammer_ip_first(&cursor);
2756 while (error == 0) {
2758 * Get the base file offset of the record. The key for
2759 * data records is (base + bytes) rather then (base).
2761 base = &cursor.leaf->base;
2762 rec_offset = base->key - cursor.leaf->data_len;
2765 * Calculate the gap, if any, and zero-fill it.
2767 * n is the offset of the start of the record verses our
2768 * current seek offset in the bio.
2770 n = (int)(rec_offset - (bio->bio_offset + boff));
2772 if (n > bp->b_bufsize - boff)
2773 n = bp->b_bufsize - boff;
2774 bzero((char *)bp->b_data + boff, n);
2780 * Calculate the data offset in the record and the number
2781 * of bytes we can copy.
2783 * There are two degenerate cases. First, boff may already
2784 * be at bp->b_bufsize. Secondly, the data offset within
2785 * the record may exceed the record's size.
2789 n = cursor.leaf->data_len - roff;
2791 hdkprintf("bad n=%d roff=%d\n", n, roff);
2793 } else if (n > bp->b_bufsize - boff) {
2794 n = bp->b_bufsize - boff;
2798 * Deal with cached truncations. This cool bit of code
2799 * allows truncate()/ftruncate() to avoid having to sync
2802 * If the frontend is truncated then all backend records are
2803 * subject to the frontend's truncation.
2805 * If the backend is truncated then backend records on-disk
2806 * (but not in-memory) are subject to the backend's
2807 * truncation. In-memory records owned by the backend
2808 * represent data written after the truncation point on the
2809 * backend and must not be truncated.
2811 * Truncate operations deal with frontend buffer cache
2812 * buffers and frontend-owned in-memory records synchronously.
2814 if (ip->flags & HAMMER_INODE_TRUNCATED) {
2815 if (hammer_cursor_ondisk(&cursor)/* ||
2816 cursor.iprec->flush_state == HAMMER_FST_FLUSH*/) {
2817 if (ip->trunc_off <= rec_offset)
2819 else if (ip->trunc_off < rec_offset + n)
2820 n = (int)(ip->trunc_off - rec_offset);
2823 if (ip->sync_flags & HAMMER_INODE_TRUNCATED) {
2824 if (hammer_cursor_ondisk(&cursor)) {
2825 if (ip->sync_trunc_off <= rec_offset)
2827 else if (ip->sync_trunc_off < rec_offset + n)
2828 n = (int)(ip->sync_trunc_off - rec_offset);
2833 * Try to issue a direct read into our bio if possible,
2834 * otherwise resolve the element data into a hammer_buffer
2837 * The buffer on-disk should be zerod past any real
2838 * truncation point, but may not be for any synthesized
2839 * truncation point from above.
2841 * NOTE: disk_offset is only valid if the cursor data is
2844 disk_offset = cursor.leaf->data_offset + roff;
2845 isdedupable = (boff == 0 && n == bp->b_bufsize &&
2846 hammer_cursor_ondisk(&cursor) &&
2847 ((int)disk_offset & HAMMER_BUFMASK) == 0);
2849 if (isdedupable && hammer_double_buffer == 0) {
2853 KKASSERT(hammer_is_zone_large_data(disk_offset));
2854 nbio->bio_offset = disk_offset;
2855 error = hammer_io_direct_read(hmp, nbio, cursor.leaf);
2856 if (hammer_live_dedup && error == 0)
2857 hammer_dedup_cache_add(ip, cursor.leaf);
2859 } else if (isdedupable) {
2861 * Async I/O case for reading from backing store
2862 * and copying the data to the filesystem buffer.
2863 * live-dedup has to verify the data anyway if it
2864 * gets a hit later so we can just add the entry
2867 KKASSERT(hammer_is_zone_large_data(disk_offset));
2868 nbio->bio_offset = disk_offset;
2869 if (hammer_live_dedup)
2870 hammer_dedup_cache_add(ip, cursor.leaf);
2871 error = hammer_io_indirect_read(hmp, nbio, cursor.leaf);
2874 error = hammer_ip_resolve_data(&cursor);
2876 if (hammer_live_dedup && isdedupable)
2877 hammer_dedup_cache_add(ip, cursor.leaf);
2878 bcopy((char *)cursor.data + roff,
2879 (char *)bp->b_data + boff, n);
2886 * We have to be sure that the only elements added to the
2887 * dedup cache are those which are already on-media.
2889 if (hammer_live_dedup && hammer_cursor_ondisk(&cursor))
2890 hammer_dedup_cache_add(ip, cursor.leaf);
2893 * Iterate until we have filled the request.
2896 if (boff == bp->b_bufsize)
2898 error = hammer_ip_next(&cursor);
2902 * There may have been a gap after the last record
2904 if (error == ENOENT)
2906 if (error == 0 && boff != bp->b_bufsize) {
2907 KKASSERT(boff < bp->b_bufsize);
2908 bzero((char *)bp->b_data + boff, bp->b_bufsize - boff);
2909 /* boff = bp->b_bufsize; */
2913 * Disallow swapcache operation on the vnode buffer if double
2914 * buffering is enabled, the swapcache will get the data via
2915 * the block device buffer.
2917 if (hammer_double_buffer)
2918 bp->b_flags |= B_NOTMETA;
2924 bp->b_error = error;
2926 bp->b_flags |= B_ERROR;
2931 * Cache the b-tree node for the last data read in cache[1].
2933 * If we hit the file EOF then also cache the node in the
2934 * governing directory's cache[3], it will be used to initialize
2935 * the new inode's cache[1] for any inodes looked up via the directory.
2937 * This doesn't reduce disk accesses since the B-Tree chain is
2938 * likely cached, but it does reduce cpu overhead when looking
2939 * up file offsets for cpdup/tar/cpio style iterations.
2942 hammer_cache_node(&ip->cache[1], cursor.node);
2943 if (ran_end >= ip->ino_data.size) {
2944 dip = hammer_find_inode(&trans, ip->ino_data.parent_obj_id,
2945 ip->obj_asof, ip->obj_localization);
2947 hammer_cache_node(&dip->cache[3], cursor.node);
2948 hammer_rel_inode(dip, 0);
2951 hammer_done_cursor(&cursor);
2952 hammer_done_transaction(&trans);
2953 lwkt_reltoken(&hmp->fs_token);
2958 * BMAP operation - used to support cluster_read() only.
2960 * (struct vnode *vp, off_t loffset, off_t *doffsetp, int *runp, int *runb)
2962 * This routine may return EOPNOTSUPP if the opration is not supported for
2963 * the specified offset. The contents of the pointer arguments do not
2964 * need to be initialized in that case.
2966 * If a disk address is available and properly aligned return 0 with
2967 * *doffsetp set to the zone-2 address, and *runp / *runb set appropriately
2968 * to the run-length relative to that offset. Callers may assume that
2969 * *doffsetp is valid if 0 is returned, even if *runp is not sufficiently
2970 * large, so return EOPNOTSUPP if it is not sufficiently large.
2974 hammer_vop_bmap(struct vop_bmap_args *ap)
2976 struct hammer_transaction trans;
2979 struct hammer_cursor cursor;
2980 hammer_base_elm_t base;
2984 int64_t base_offset;
2985 int64_t base_disk_offset;
2986 int64_t last_offset;
2987 hammer_off_t last_disk_offset;
2988 hammer_off_t disk_offset;
2993 ++hammer_stats_file_iopsr;
2994 ip = ap->a_vp->v_data;
2998 * We can only BMAP regular files. We can't BMAP database files,
3001 if (ip->ino_data.obj_type != HAMMER_OBJTYPE_REGFILE)
3005 * bmap is typically called with runp/runb both NULL when used
3006 * for writing. We do not support BMAP for writing atm.
3008 if (ap->a_cmd != BUF_CMD_READ)
3012 * Scan the B-Tree to acquire blockmap addresses, then translate
3015 lwkt_gettoken(&hmp->fs_token);
3016 hammer_simple_transaction(&trans, hmp);
3018 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
3021 * Key range (begin and end inclusive) to scan. Note that the key's
3022 * stored in the actual records represent BASE+LEN, not BASE. The
3023 * first record containing bio_offset will have a key > bio_offset.
3025 cursor.key_beg.localization = ip->obj_localization |
3026 HAMMER_LOCALIZE_MISC;
3027 cursor.key_beg.obj_id = ip->obj_id;
3028 cursor.key_beg.create_tid = 0;
3029 cursor.key_beg.delete_tid = 0;
3030 cursor.key_beg.obj_type = 0;
3032 cursor.key_beg.key = ap->a_loffset - MAXPHYS + 1;
3034 cursor.key_beg.key = ap->a_loffset + 1;
3035 if (cursor.key_beg.key < 0)
3036 cursor.key_beg.key = 0;
3037 cursor.asof = ip->obj_asof;
3038 cursor.flags |= HAMMER_CURSOR_ASOF;
3040 cursor.key_end = cursor.key_beg;
3041 KKASSERT(ip->ino_data.obj_type == HAMMER_OBJTYPE_REGFILE);
3043 ran_end = ap->a_loffset + MAXPHYS;
3044 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
3045 cursor.key_end.rec_type = HAMMER_RECTYPE_DATA;
3046 tmp64 = ran_end + MAXPHYS + 1; /* work-around GCC-4 bug */
3047 if (tmp64 < ran_end)
3048 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
3050 cursor.key_end.key = ran_end + MAXPHYS + 1;
3052 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
3054 error = hammer_ip_first(&cursor);
3055 base_offset = last_offset = 0;
3056 base_disk_offset = last_disk_offset = 0;
3058 while (error == 0) {
3060 * Get the base file offset of the record. The key for
3061 * data records is (base + bytes) rather then (base).
3063 * NOTE: rec_offset + rec_len may exceed the end-of-file.
3064 * The extra bytes should be zero on-disk and the BMAP op
3065 * should still be ok.
3067 base = &cursor.leaf->base;
3068 rec_offset = base->key - cursor.leaf->data_len;
3069 rec_len = cursor.leaf->data_len;
3072 * Incorporate any cached truncation.
3074 * NOTE: Modifications to rec_len based on synthesized
3075 * truncation points remove the guarantee that any extended
3076 * data on disk is zero (since the truncations may not have
3077 * taken place on-media yet).
3079 if (ip->flags & HAMMER_INODE_TRUNCATED) {
3080 if (hammer_cursor_ondisk(&cursor) ||
3081 cursor.iprec->flush_state == HAMMER_FST_FLUSH) {
3082 if (ip->trunc_off <= rec_offset)
3084 else if (ip->trunc_off < rec_offset + rec_len)
3085 rec_len = (int)(ip->trunc_off - rec_offset);
3088 if (ip->sync_flags & HAMMER_INODE_TRUNCATED) {
3089 if (hammer_cursor_ondisk(&cursor)) {
3090 if (ip->sync_trunc_off <= rec_offset)
3092 else if (ip->sync_trunc_off < rec_offset + rec_len)
3093 rec_len = (int)(ip->sync_trunc_off - rec_offset);
3098 * Accumulate information. If we have hit a discontiguous
3099 * block reset base_offset unless we are already beyond the
3100 * requested offset. If we are, that's it, we stop.
3104 if (hammer_cursor_ondisk(&cursor)) {
3105 disk_offset = cursor.leaf->data_offset;
3106 if (rec_offset != last_offset ||
3107 disk_offset != last_disk_offset) {
3108 if (rec_offset > ap->a_loffset)
3110 base_offset = rec_offset;
3111 base_disk_offset = disk_offset;
3113 last_offset = rec_offset + rec_len;
3114 last_disk_offset = disk_offset + rec_len;
3116 if (hammer_live_dedup)
3117 hammer_dedup_cache_add(ip, cursor.leaf);
3120 error = hammer_ip_next(&cursor);
3124 hammer_cache_node(&ip->cache[1], cursor.node);
3126 hammer_done_cursor(&cursor);
3127 hammer_done_transaction(&trans);
3128 lwkt_reltoken(&hmp->fs_token);
3131 * If we couldn't find any records or the records we did find were
3132 * all behind the requested offset, return failure. A forward
3133 * truncation can leave a hole w/ no on-disk records.
3135 if (last_offset == 0 || last_offset < ap->a_loffset)
3136 return (EOPNOTSUPP);
3139 * Figure out the block size at the requested offset and adjust
3140 * our limits so the cluster_read() does not create inappropriately
3141 * sized buffer cache buffers.
3143 blksize = hammer_blocksize(ap->a_loffset);
3144 if (hammer_blocksize(base_offset) != blksize) {
3145 base_offset = hammer_blockdemarc(base_offset, ap->a_loffset);
3147 if (last_offset != ap->a_loffset &&
3148 hammer_blocksize(last_offset - 1) != blksize) {
3149 last_offset = hammer_blockdemarc(ap->a_loffset,
3154 * Returning EOPNOTSUPP simply prevents the direct-IO optimization
3157 disk_offset = base_disk_offset + (ap->a_loffset - base_offset);
3159 if (!hammer_is_zone_large_data(disk_offset)) {
3161 * Only large-data zones can be direct-IOd
3164 } else if ((disk_offset & HAMMER_BUFMASK) ||
3165 (last_offset - ap->a_loffset) < blksize) {
3167 * doffsetp is not aligned or the forward run size does
3168 * not cover a whole buffer, disallow the direct I/O.
3175 *ap->a_doffsetp = disk_offset;
3177 *ap->a_runb = ap->a_loffset - base_offset;
3178 KKASSERT(*ap->a_runb >= 0);
3181 *ap->a_runp = last_offset - ap->a_loffset;
3182 KKASSERT(*ap->a_runp >= 0);
3190 * Write to a regular file. Because this is a strategy call the OS is
3191 * trying to actually get data onto the media.
3195 hammer_vop_strategy_write(struct vop_strategy_args *ap)
3197 hammer_record_t record;
3202 int blksize __debugvar;
3208 ip = ap->a_vp->v_data;
3211 blksize = hammer_blocksize(bio->bio_offset);
3212 KKASSERT(bp->b_bufsize == blksize);
3214 if (ip->flags & HAMMER_INODE_RO) {
3215 bp->b_error = EROFS;
3216 bp->b_flags |= B_ERROR;
3221 lwkt_gettoken(&hmp->fs_token);
3224 * Disallow swapcache operation on the vnode buffer if double
3225 * buffering is enabled, the swapcache will get the data via
3226 * the block device buffer.
3228 if (hammer_double_buffer)
3229 bp->b_flags |= B_NOTMETA;
3232 * Interlock with inode destruction (no in-kernel or directory
3233 * topology visibility). If we queue new IO while trying to
3234 * destroy the inode we can deadlock the vtrunc call in
3235 * hammer_inode_unloadable_check().
3237 * Besides, there's no point flushing a bp associated with an
3238 * inode that is being destroyed on-media and has no kernel
3241 if ((ip->flags | ip->sync_flags) &
3242 (HAMMER_INODE_DELETING|HAMMER_INODE_DELETED)) {
3245 lwkt_reltoken(&hmp->fs_token);
3250 * Reserve space and issue a direct-write from the front-end.
3251 * NOTE: The direct_io code will hammer_bread/bcopy smaller
3254 * An in-memory record will be installed to reference the storage
3255 * until the flusher can get to it.
3257 * Since we own the high level bio the front-end will not try to
3258 * do a direct-read until the write completes.
3260 * NOTE: The only time we do not reserve a full-sized buffers
3261 * worth of data is if the file is small. We do not try to
3262 * allocate a fragment (from the small-data zone) at the end of
3263 * an otherwise large file as this can lead to wildly separated
3266 KKASSERT((bio->bio_offset & HAMMER_BUFMASK) == 0);
3267 KKASSERT(bio->bio_offset < ip->ino_data.size);
3268 if (bio->bio_offset || ip->ino_data.size > HAMMER_HBUFSIZE)
3269 bytes = bp->b_bufsize;
3271 bytes = ((int)ip->ino_data.size + 15) & ~15;
3273 record = hammer_ip_add_bulk(ip, bio->bio_offset, bp->b_data,
3277 * B_VFSFLAG1 indicates that a REDO_WRITE entry was generated
3278 * in hammer_vop_write(). We must flag the record so the proper
3279 * REDO_TERM_WRITE entry is generated during the flush.
3282 if (bp->b_flags & B_VFSFLAG1) {
3283 record->flags |= HAMMER_RECF_REDO;
3284 bp->b_flags &= ~B_VFSFLAG1;
3286 if (record->flags & HAMMER_RECF_DEDUPED) {
3288 hammer_ip_replace_bulk(hmp, record);
3291 hammer_io_direct_write(hmp, bio, record);
3293 if (ip->rsv_recs > 1 && hmp->rsv_recs > hammer_limit_recs)
3294 hammer_flush_inode(ip, 0);
3296 bp->b_bio2.bio_offset = NOOFFSET;
3297 bp->b_error = error;
3298 bp->b_flags |= B_ERROR;
3301 lwkt_reltoken(&hmp->fs_token);
3306 * dounlink - disconnect a directory entry
3308 * XXX whiteout support not really in yet
3311 hammer_dounlink(hammer_transaction_t trans, struct nchandle *nch,
3312 struct vnode *dvp, struct ucred *cred,
3313 int flags, int isdir)
3315 struct namecache *ncp;
3319 struct hammer_cursor cursor;
3321 uint32_t max_iterations;
3325 * Calculate the namekey and setup the key range for the scan. This
3326 * works kinda like a chained hash table where the lower 32 bits
3327 * of the namekey synthesize the chain.
3329 * The key range is inclusive of both key_beg and key_end.
3335 if (dip->flags & HAMMER_INODE_RO)
3338 namekey = hammer_direntry_namekey(dip, ncp->nc_name, ncp->nc_nlen,
3341 hammer_init_cursor(trans, &cursor, &dip->cache[1], dip);
3342 cursor.key_beg.localization = dip->obj_localization |
3343 hammer_dir_localization(dip);
3344 cursor.key_beg.obj_id = dip->obj_id;
3345 cursor.key_beg.key = namekey;
3346 cursor.key_beg.create_tid = 0;
3347 cursor.key_beg.delete_tid = 0;
3348 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
3349 cursor.key_beg.obj_type = 0;
3351 cursor.key_end = cursor.key_beg;
3352 cursor.key_end.key += max_iterations;
3353 cursor.asof = dip->obj_asof;
3354 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
3357 * Scan all matching records (the chain), locate the one matching
3358 * the requested path component. info->last_error contains the
3359 * error code on search termination and could be 0, ENOENT, or
3362 * The hammer_ip_*() functions merge in-memory records with on-disk
3363 * records for the purposes of the search.
3365 error = hammer_ip_first(&cursor);
3367 while (error == 0) {
3368 error = hammer_ip_resolve_data(&cursor);
3371 nlen = cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF;
3373 if (ncp->nc_nlen == nlen &&
3374 bcmp(ncp->nc_name, cursor.data->entry.name, nlen) == 0) {
3377 error = hammer_ip_next(&cursor);
3381 * If all is ok we have to get the inode so we can adjust nlinks.
3382 * To avoid a deadlock with the flusher we must release the inode
3383 * lock on the directory when acquiring the inode for the entry.
3385 * If the target is a directory, it must be empty.
3388 hammer_unlock(&cursor.ip->lock);
3389 ip = hammer_get_inode(trans, dip, cursor.data->entry.obj_id,
3391 cursor.data->entry.localization,
3393 hammer_lock_sh(&cursor.ip->lock);
3394 if (error == ENOENT) {
3395 hkprintf("WARNING: Removing dirent w/missing inode "
3397 "\tobj_id = %016jx\n",
3399 (intmax_t)cursor.data->entry.obj_id);
3404 * If isdir >= 0 we validate that the entry is or is not a
3405 * directory. If isdir < 0 we don't care.
3407 if (error == 0 && isdir >= 0 && ip) {
3409 ip->ino_data.obj_type != HAMMER_OBJTYPE_DIRECTORY) {
3411 } else if (isdir == 0 &&
3412 ip->ino_data.obj_type == HAMMER_OBJTYPE_DIRECTORY) {
3418 * If we are trying to remove a directory the directory must
3421 * The check directory code can loop and deadlock/retry. Our
3422 * own cursor's node locks must be released to avoid a 3-way
3423 * deadlock with the flusher if the check directory code
3426 * If any changes whatsoever have been made to the cursor
3427 * set EDEADLK and retry.
3429 * WARNING: See warnings in hammer_unlock_cursor()
3432 if (error == 0 && ip && ip->ino_data.obj_type ==
3433 HAMMER_OBJTYPE_DIRECTORY) {
3434 hammer_unlock_cursor(&cursor);
3435 error = hammer_ip_check_directory_empty(trans, ip);
3436 hammer_lock_cursor(&cursor);
3437 if (cursor.flags & HAMMER_CURSOR_RETEST) {
3438 hkprintf("Warning: avoided deadlock "
3446 * Delete the directory entry.
3448 * WARNING: hammer_ip_del_direntry() may have to terminate
3449 * the cursor to avoid a deadlock. It is ok to call
3450 * hammer_done_cursor() twice.
3453 error = hammer_ip_del_direntry(trans, &cursor,
3456 hammer_done_cursor(&cursor);
3459 * Tell the namecache that we are now unlinked.
3464 * NOTE: ip->vp, if non-NULL, cannot be directly
3465 * referenced without formally acquiring the
3466 * vp since the vp might have zero refs on it,
3467 * or in the middle of a reclaim, etc.
3469 * NOTE: The cache_setunresolved() can rip the vp
3470 * out from under us since the vp may not have
3471 * any refs, in which case ip->vp will be NULL
3474 while (ip && ip->vp) {
3477 error = hammer_get_vnode(ip, &vp);
3478 if (error == 0 && vp) {
3480 hammer_knote(ip->vp, NOTE_DELETE);
3483 * Don't do this, it can deadlock
3484 * on concurrent rm's of hardlinks.
3485 * Shouldn't be needed any more.
3487 cache_inval_vp(ip->vp, CINV_DESTROY);
3492 hdkprintf("ip/vp race1 avoided\n");
3496 hammer_rel_inode(ip, 0);
3498 hammer_done_cursor(&cursor);
3500 if (error == EDEADLK)
3506 /************************************************************************
3507 * FIFO AND SPECFS OPS *
3508 ************************************************************************
3512 hammer_vop_fifoclose (struct vop_close_args *ap)
3514 /* XXX update itimes */
3515 return (VOCALL(&fifo_vnode_vops, &ap->a_head));
3519 hammer_vop_fiforead (struct vop_read_args *ap)
3523 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
3524 /* XXX update access time */
3529 hammer_vop_fifowrite (struct vop_write_args *ap)
3533 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
3534 /* XXX update access time */
3540 hammer_vop_fifokqfilter(struct vop_kqfilter_args *ap)
3544 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
3546 error = hammer_vop_kqfilter(ap);
3550 /************************************************************************
3552 ************************************************************************
3555 static void filt_hammerdetach(struct knote *kn);
3556 static int filt_hammerread(struct knote *kn, long hint);
3557 static int filt_hammerwrite(struct knote *kn, long hint);
3558 static int filt_hammervnode(struct knote *kn, long hint);
3560 static struct filterops hammerread_filtops =
3561 { FILTEROP_ISFD | FILTEROP_MPSAFE,
3562 NULL, filt_hammerdetach, filt_hammerread };
3563 static struct filterops hammerwrite_filtops =
3564 { FILTEROP_ISFD | FILTEROP_MPSAFE,
3565 NULL, filt_hammerdetach, filt_hammerwrite };
3566 static struct filterops hammervnode_filtops =
3567 { FILTEROP_ISFD | FILTEROP_MPSAFE,
3568 NULL, filt_hammerdetach, filt_hammervnode };
3572 hammer_vop_kqfilter(struct vop_kqfilter_args *ap)
3574 struct vnode *vp = ap->a_vp;
3575 struct knote *kn = ap->a_kn;
3577 switch (kn->kn_filter) {
3579 kn->kn_fop = &hammerread_filtops;
3582 kn->kn_fop = &hammerwrite_filtops;
3585 kn->kn_fop = &hammervnode_filtops;
3588 return (EOPNOTSUPP);
3591 kn->kn_hook = (caddr_t)vp;
3593 knote_insert(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
3599 filt_hammerdetach(struct knote *kn)
3601 struct vnode *vp = (void *)kn->kn_hook;
3603 knote_remove(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
3607 filt_hammerread(struct knote *kn, long hint)
3609 struct vnode *vp = (void *)kn->kn_hook;
3610 hammer_inode_t ip = VTOI(vp);
3611 hammer_mount_t hmp = ip->hmp;
3614 if (hint == NOTE_REVOKE) {
3615 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
3618 lwkt_gettoken(&hmp->fs_token); /* XXX use per-ip-token */
3619 off = ip->ino_data.size - kn->kn_fp->f_offset;
3620 kn->kn_data = (off < INTPTR_MAX) ? off : INTPTR_MAX;
3621 lwkt_reltoken(&hmp->fs_token);
3622 if (kn->kn_sfflags & NOTE_OLDAPI)
3624 return (kn->kn_data != 0);
3628 filt_hammerwrite(struct knote *kn, long hint)
3630 if (hint == NOTE_REVOKE)
3631 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
3637 filt_hammervnode(struct knote *kn, long hint)
3639 if (kn->kn_sfflags & hint)
3640 kn->kn_fflags |= hint;
3641 if (hint == NOTE_REVOKE) {
3642 kn->kn_flags |= (EV_EOF | EV_NODATA);
3645 return (kn->kn_fflags != 0);