2 * Copyright (c) 2007-2008 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * $DragonFly: src/sys/vfs/hammer/hammer_vnops.c,v 1.102 2008/10/16 17:24:16 dillon Exp $
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/fcntl.h>
41 #include <sys/namecache.h>
42 #include <sys/vnode.h>
43 #include <sys/lockf.h>
44 #include <sys/event.h>
46 #include <sys/dirent.h>
48 #include <vm/vm_extern.h>
49 #include <vfs/fifofs/fifo.h>
51 #include <sys/mplock2.h>
58 /*static int hammer_vop_vnoperate(struct vop_generic_args *);*/
59 static int hammer_vop_fsync(struct vop_fsync_args *);
60 static int hammer_vop_read(struct vop_read_args *);
61 static int hammer_vop_write(struct vop_write_args *);
62 static int hammer_vop_access(struct vop_access_args *);
63 static int hammer_vop_advlock(struct vop_advlock_args *);
64 static int hammer_vop_close(struct vop_close_args *);
65 static int hammer_vop_ncreate(struct vop_ncreate_args *);
66 static int hammer_vop_getattr(struct vop_getattr_args *);
67 static int hammer_vop_nresolve(struct vop_nresolve_args *);
68 static int hammer_vop_nlookupdotdot(struct vop_nlookupdotdot_args *);
69 static int hammer_vop_nlink(struct vop_nlink_args *);
70 static int hammer_vop_nmkdir(struct vop_nmkdir_args *);
71 static int hammer_vop_nmknod(struct vop_nmknod_args *);
72 static int hammer_vop_open(struct vop_open_args *);
73 static int hammer_vop_print(struct vop_print_args *);
74 static int hammer_vop_readdir(struct vop_readdir_args *);
75 static int hammer_vop_readlink(struct vop_readlink_args *);
76 static int hammer_vop_nremove(struct vop_nremove_args *);
77 static int hammer_vop_nrename(struct vop_nrename_args *);
78 static int hammer_vop_nrmdir(struct vop_nrmdir_args *);
79 static int hammer_vop_markatime(struct vop_markatime_args *);
80 static int hammer_vop_setattr(struct vop_setattr_args *);
81 static int hammer_vop_strategy(struct vop_strategy_args *);
82 static int hammer_vop_bmap(struct vop_bmap_args *ap);
83 static int hammer_vop_nsymlink(struct vop_nsymlink_args *);
84 static int hammer_vop_nwhiteout(struct vop_nwhiteout_args *);
85 static int hammer_vop_ioctl(struct vop_ioctl_args *);
86 static int hammer_vop_mountctl(struct vop_mountctl_args *);
87 static int hammer_vop_kqfilter (struct vop_kqfilter_args *);
89 static int hammer_vop_fifoclose (struct vop_close_args *);
90 static int hammer_vop_fiforead (struct vop_read_args *);
91 static int hammer_vop_fifowrite (struct vop_write_args *);
92 static int hammer_vop_fifokqfilter (struct vop_kqfilter_args *);
94 struct vop_ops hammer_vnode_vops = {
95 .vop_default = vop_defaultop,
96 .vop_fsync = hammer_vop_fsync,
97 .vop_getpages = vop_stdgetpages,
98 .vop_putpages = vop_stdputpages,
99 .vop_read = hammer_vop_read,
100 .vop_write = hammer_vop_write,
101 .vop_access = hammer_vop_access,
102 .vop_advlock = hammer_vop_advlock,
103 .vop_close = hammer_vop_close,
104 .vop_ncreate = hammer_vop_ncreate,
105 .vop_getattr = hammer_vop_getattr,
106 .vop_inactive = hammer_vop_inactive,
107 .vop_reclaim = hammer_vop_reclaim,
108 .vop_nresolve = hammer_vop_nresolve,
109 .vop_nlookupdotdot = hammer_vop_nlookupdotdot,
110 .vop_nlink = hammer_vop_nlink,
111 .vop_nmkdir = hammer_vop_nmkdir,
112 .vop_nmknod = hammer_vop_nmknod,
113 .vop_open = hammer_vop_open,
114 .vop_pathconf = vop_stdpathconf,
115 .vop_print = hammer_vop_print,
116 .vop_readdir = hammer_vop_readdir,
117 .vop_readlink = hammer_vop_readlink,
118 .vop_nremove = hammer_vop_nremove,
119 .vop_nrename = hammer_vop_nrename,
120 .vop_nrmdir = hammer_vop_nrmdir,
121 .vop_markatime = hammer_vop_markatime,
122 .vop_setattr = hammer_vop_setattr,
123 .vop_bmap = hammer_vop_bmap,
124 .vop_strategy = hammer_vop_strategy,
125 .vop_nsymlink = hammer_vop_nsymlink,
126 .vop_nwhiteout = hammer_vop_nwhiteout,
127 .vop_ioctl = hammer_vop_ioctl,
128 .vop_mountctl = hammer_vop_mountctl,
129 .vop_kqfilter = hammer_vop_kqfilter
132 struct vop_ops hammer_spec_vops = {
133 .vop_default = vop_defaultop,
134 .vop_fsync = hammer_vop_fsync,
135 .vop_read = vop_stdnoread,
136 .vop_write = vop_stdnowrite,
137 .vop_access = hammer_vop_access,
138 .vop_close = hammer_vop_close,
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
146 struct vop_ops hammer_fifo_vops = {
147 .vop_default = fifo_vnoperate,
148 .vop_fsync = hammer_vop_fsync,
149 .vop_read = hammer_vop_fiforead,
150 .vop_write = hammer_vop_fifowrite,
151 .vop_access = hammer_vop_access,
152 .vop_close = hammer_vop_fifoclose,
153 .vop_markatime = hammer_vop_markatime,
154 .vop_getattr = hammer_vop_getattr,
155 .vop_inactive = hammer_vop_inactive,
156 .vop_reclaim = hammer_vop_reclaim,
157 .vop_setattr = hammer_vop_setattr,
158 .vop_kqfilter = hammer_vop_fifokqfilter
163 hammer_knote(struct vnode *vp, int flags)
166 KNOTE(&vp->v_pollinfo.vpi_kqinfo.ki_note, flags);
169 #ifdef DEBUG_TRUNCATE
170 struct hammer_inode *HammerTruncIp;
173 static int hammer_dounlink(hammer_transaction_t trans, struct nchandle *nch,
174 struct vnode *dvp, struct ucred *cred,
175 int flags, int isdir);
176 static int hammer_vop_strategy_read(struct vop_strategy_args *ap);
177 static int hammer_vop_strategy_write(struct vop_strategy_args *ap);
182 hammer_vop_vnoperate(struct vop_generic_args *)
184 return (VOCALL(&hammer_vnode_vops, ap));
189 * hammer_vop_fsync { vp, waitfor }
191 * fsync() an inode to disk and wait for it to be completely committed
192 * such that the information would not be undone if a crash occured after
195 * NOTE: HAMMER's fsync()'s are going to remain expensive until we implement
196 * a REDO log. A sysctl is provided to relax HAMMER's fsync()
199 * Ultimately the combination of a REDO log and use of fast storage
200 * to front-end cluster caches will make fsync fast, but it aint
201 * here yet. And, in anycase, we need real transactional
202 * all-or-nothing features which are not restricted to a single file.
206 hammer_vop_fsync(struct vop_fsync_args *ap)
208 hammer_inode_t ip = VTOI(ap->a_vp);
209 hammer_mount_t hmp = ip->hmp;
210 int waitfor = ap->a_waitfor;
213 lwkt_gettoken(&hmp->fs_token);
216 * Fsync rule relaxation (default is either full synchronous flush
217 * or REDO semantics with synchronous flush).
219 if (ap->a_flags & VOP_FSYNC_SYSCALL) {
220 switch(hammer_fsync_mode) {
223 /* no REDO, full synchronous flush */
227 /* no REDO, full asynchronous flush */
228 if (waitfor == MNT_WAIT)
229 waitfor = MNT_NOWAIT;
232 /* REDO semantics, synchronous flush */
233 if (hmp->version < HAMMER_VOL_VERSION_FOUR)
235 mode = HAMMER_FLUSH_UNDOS_AUTO;
238 /* REDO semantics, relaxed asynchronous flush */
239 if (hmp->version < HAMMER_VOL_VERSION_FOUR)
241 mode = HAMMER_FLUSH_UNDOS_RELAXED;
242 if (waitfor == MNT_WAIT)
243 waitfor = MNT_NOWAIT;
246 /* ignore the fsync() system call */
247 lwkt_reltoken(&hmp->fs_token);
250 /* we have to do something */
251 mode = HAMMER_FLUSH_UNDOS_RELAXED;
252 if (waitfor == MNT_WAIT)
253 waitfor = MNT_NOWAIT;
258 * Fast fsync only needs to flush the UNDO/REDO fifo if
259 * HAMMER_INODE_REDO is non-zero and the only modifications
260 * made to the file are write or write-extends.
262 if ((ip->flags & HAMMER_INODE_REDO) &&
263 (ip->flags & HAMMER_INODE_MODMASK_NOREDO) == 0
265 ++hammer_count_fsyncs;
266 hammer_flusher_flush_undos(hmp, mode);
268 lwkt_reltoken(&hmp->fs_token);
273 * REDO is enabled by fsync(), the idea being we really only
274 * want to lay down REDO records when programs are using
275 * fsync() heavily. The first fsync() on the file starts
276 * the gravy train going and later fsync()s keep it hot by
277 * resetting the redo_count.
279 * We weren't running REDOs before now so we have to fall
280 * through and do a full fsync of what we have.
282 if (hmp->version >= HAMMER_VOL_VERSION_FOUR &&
283 (hmp->flags & HAMMER_MOUNT_REDO_RECOVERY_RUN) == 0) {
284 ip->flags |= HAMMER_INODE_REDO;
291 * Do a full flush sequence.
293 ++hammer_count_fsyncs;
294 vfsync(ap->a_vp, waitfor, 1, NULL, NULL);
295 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
296 if (waitfor == MNT_WAIT) {
298 hammer_wait_inode(ip);
299 vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY);
301 lwkt_reltoken(&hmp->fs_token);
306 * hammer_vop_read { vp, uio, ioflag, cred }
308 * MPSAFE (for the cache safe does not require fs_token)
312 hammer_vop_read(struct vop_read_args *ap)
314 struct hammer_transaction trans;
328 if (ap->a_vp->v_type != VREG)
336 * Allow the UIO's size to override the sequential heuristic.
338 blksize = hammer_blocksize(uio->uio_offset);
339 seqcount = (uio->uio_resid + (BKVASIZE - 1)) / BKVASIZE;
340 ioseqcount = (ap->a_ioflag >> 16);
341 if (seqcount < ioseqcount)
342 seqcount = ioseqcount;
345 * If reading or writing a huge amount of data we have to break
346 * atomicy and allow the operation to be interrupted by a signal
347 * or it can DOS the machine.
349 bigread = (uio->uio_resid > 100 * 1024 * 1024);
353 * Access the data typically in HAMMER_BUFSIZE blocks via the
354 * buffer cache, but HAMMER may use a variable block size based
357 * XXX Temporary hack, delay the start transaction while we remain
358 * MPSAFE. NOTE: ino_data.size cannot change while vnode is
361 while (uio->uio_resid > 0 && uio->uio_offset < ip->ino_data.size) {
365 blksize = hammer_blocksize(uio->uio_offset);
366 offset = (int)uio->uio_offset & (blksize - 1);
367 base_offset = uio->uio_offset - offset;
369 if (bigread && (error = hammer_signal_check(ip->hmp)) != 0)
375 bp = getcacheblk(ap->a_vp, base_offset, blksize);
384 if (got_fstoken == 0) {
385 lwkt_gettoken(&hmp->fs_token);
387 hammer_start_transaction(&trans, ip->hmp);
390 if (hammer_cluster_enable) {
392 * Use file_limit to prevent cluster_read() from
393 * creating buffers of the wrong block size past
396 file_limit = ip->ino_data.size;
397 if (base_offset < HAMMER_XDEMARC &&
398 file_limit > HAMMER_XDEMARC) {
399 file_limit = HAMMER_XDEMARC;
401 error = cluster_read(ap->a_vp,
402 file_limit, base_offset,
403 blksize, uio->uio_resid,
404 seqcount * BKVASIZE, &bp);
406 error = bread(ap->a_vp, base_offset, blksize, &bp);
413 if ((hammer_debug_io & 0x0001) && (bp->b_flags & B_IODEBUG)) {
414 kprintf("doff %016jx read file %016jx@%016jx\n",
415 (intmax_t)bp->b_bio2.bio_offset,
416 (intmax_t)ip->obj_id,
417 (intmax_t)bp->b_loffset);
419 bp->b_flags &= ~B_IODEBUG;
421 /* bp->b_flags |= B_CLUSTEROK; temporarily disabled */
422 n = blksize - offset;
423 if (n > uio->uio_resid)
425 if (n > ip->ino_data.size - uio->uio_offset)
426 n = (int)(ip->ino_data.size - uio->uio_offset);
428 lwkt_reltoken(&hmp->fs_token);
429 error = uiomove((char *)bp->b_data + offset, n, uio);
431 lwkt_gettoken(&hmp->fs_token);
433 /* data has a lower priority then meta-data */
434 bp->b_flags |= B_AGE;
438 hammer_stats_file_read += n;
442 * XXX only update the atime if we had to get the MP lock.
443 * XXX hack hack hack, fixme.
446 if ((ip->flags & HAMMER_INODE_RO) == 0 &&
447 (ip->hmp->mp->mnt_flag & MNT_NOATIME) == 0) {
448 ip->ino_data.atime = trans.time;
449 hammer_modify_inode(&trans, ip, HAMMER_INODE_ATIME);
451 hammer_done_transaction(&trans);
452 lwkt_reltoken(&hmp->fs_token);
458 * hammer_vop_write { vp, uio, ioflag, cred }
462 hammer_vop_write(struct vop_write_args *ap)
464 struct hammer_transaction trans;
465 struct hammer_inode *ip;
478 if (ap->a_vp->v_type != VREG)
484 seqcount = ap->a_ioflag >> 16;
486 if (ip->flags & HAMMER_INODE_RO)
490 * Create a transaction to cover the operations we perform.
492 lwkt_gettoken(&hmp->fs_token);
493 hammer_start_transaction(&trans, hmp);
499 if (ap->a_ioflag & IO_APPEND)
500 uio->uio_offset = ip->ino_data.size;
503 * Check for illegal write offsets. Valid range is 0...2^63-1.
505 * NOTE: the base_off assignment is required to work around what
506 * I consider to be a GCC-4 optimization bug.
508 if (uio->uio_offset < 0) {
509 hammer_done_transaction(&trans);
510 lwkt_reltoken(&hmp->fs_token);
513 base_offset = uio->uio_offset + uio->uio_resid; /* work around gcc-4 */
514 if (uio->uio_resid > 0 && base_offset <= uio->uio_offset) {
515 hammer_done_transaction(&trans);
516 lwkt_reltoken(&hmp->fs_token);
521 * If reading or writing a huge amount of data we have to break
522 * atomicy and allow the operation to be interrupted by a signal
523 * or it can DOS the machine.
525 * Preset redo_count so we stop generating REDOs earlier if the
528 bigwrite = (uio->uio_resid > 100 * 1024 * 1024);
529 if ((ip->flags & HAMMER_INODE_REDO) &&
530 ip->redo_count < hammer_limit_redo) {
531 ip->redo_count += uio->uio_resid;
535 * Access the data typically in HAMMER_BUFSIZE blocks via the
536 * buffer cache, but HAMMER may use a variable block size based
539 while (uio->uio_resid > 0) {
547 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_WRITE)) != 0)
549 if (bigwrite && (error = hammer_signal_check(hmp)) != 0)
552 blksize = hammer_blocksize(uio->uio_offset);
555 * Do not allow HAMMER to blow out the buffer cache. Very
556 * large UIOs can lockout other processes due to bwillwrite()
559 * The hammer inode is not locked during these operations.
560 * The vnode is locked which can interfere with the pageout
561 * daemon for non-UIO_NOCOPY writes but should not interfere
562 * with the buffer cache. Even so, we cannot afford to
563 * allow the pageout daemon to build up too many dirty buffer
566 * Only call this if we aren't being recursively called from
567 * a virtual disk device (vn), else we may deadlock.
569 if ((ap->a_ioflag & IO_RECURSE) == 0)
573 * Control the number of pending records associated with
574 * this inode. If too many have accumulated start a
575 * flush. Try to maintain a pipeline with the flusher.
577 if (ip->rsv_recs >= hammer_limit_inode_recs) {
578 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
580 if (ip->rsv_recs >= hammer_limit_inode_recs * 2) {
581 while (ip->rsv_recs >= hammer_limit_inode_recs) {
582 tsleep(&ip->rsv_recs, 0, "hmrwww", hz);
584 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
589 * Do not allow HAMMER to blow out system memory by
590 * accumulating too many records. Records are so well
591 * decoupled from the buffer cache that it is possible
592 * for userland to push data out to the media via
593 * direct-write, but build up the records queued to the
594 * backend faster then the backend can flush them out.
595 * HAMMER has hit its write limit but the frontend has
596 * no pushback to slow it down.
598 if (hmp->rsv_recs > hammer_limit_recs / 2) {
600 * Get the inode on the flush list
602 if (ip->rsv_recs >= 64)
603 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
604 else if (ip->rsv_recs >= 16)
605 hammer_flush_inode(ip, 0);
608 * Keep the flusher going if the system keeps
611 delta = hmp->count_newrecords -
612 hmp->last_newrecords;
613 if (delta < 0 || delta > hammer_limit_recs / 2) {
614 hmp->last_newrecords = hmp->count_newrecords;
615 hammer_sync_hmp(hmp, MNT_NOWAIT);
619 * If we have gotten behind start slowing
622 delta = (hmp->rsv_recs - hammer_limit_recs) *
623 hz / hammer_limit_recs;
625 tsleep(&trans, 0, "hmrslo", delta);
630 * Calculate the blocksize at the current offset and figure
631 * out how much we can actually write.
633 blkmask = blksize - 1;
634 offset = (int)uio->uio_offset & blkmask;
635 base_offset = uio->uio_offset & ~(int64_t)blkmask;
636 n = blksize - offset;
637 if (n > uio->uio_resid) {
643 nsize = uio->uio_offset + n;
644 if (nsize > ip->ino_data.size) {
645 if (uio->uio_offset > ip->ino_data.size)
649 nvextendbuf(ap->a_vp,
652 hammer_blocksize(ip->ino_data.size),
653 hammer_blocksize(nsize),
654 hammer_blockoff(ip->ino_data.size),
655 hammer_blockoff(nsize),
658 kflags |= NOTE_EXTEND;
661 if (uio->uio_segflg == UIO_NOCOPY) {
663 * Issuing a write with the same data backing the
664 * buffer. Instantiate the buffer to collect the
665 * backing vm pages, then read-in any missing bits.
667 * This case is used by vop_stdputpages().
669 bp = getblk(ap->a_vp, base_offset,
670 blksize, GETBLK_BHEAVY, 0);
671 if ((bp->b_flags & B_CACHE) == 0) {
673 error = bread(ap->a_vp, base_offset,
676 } else if (offset == 0 && uio->uio_resid >= blksize) {
678 * Even though we are entirely overwriting the buffer
679 * we may still have to zero it out to avoid a
680 * mmap/write visibility issue.
682 bp = getblk(ap->a_vp, base_offset, blksize, GETBLK_BHEAVY, 0);
683 if ((bp->b_flags & B_CACHE) == 0)
685 } else if (base_offset >= ip->ino_data.size) {
687 * If the base offset of the buffer is beyond the
688 * file EOF, we don't have to issue a read.
690 bp = getblk(ap->a_vp, base_offset,
691 blksize, GETBLK_BHEAVY, 0);
695 * Partial overwrite, read in any missing bits then
696 * replace the portion being written.
698 error = bread(ap->a_vp, base_offset, blksize, &bp);
703 lwkt_reltoken(&hmp->fs_token);
704 error = uiomove(bp->b_data + offset, n, uio);
705 lwkt_gettoken(&hmp->fs_token);
709 * Generate REDO records if enabled and redo_count will not
710 * exceeded the limit.
712 * If redo_count exceeds the limit we stop generating records
713 * and clear HAMMER_INODE_REDO. This will cause the next
714 * fsync() to do a full meta-data sync instead of just an
715 * UNDO/REDO fifo update.
717 * When clearing HAMMER_INODE_REDO any pre-existing REDOs
718 * will still be tracked. The tracks will be terminated
719 * when the related meta-data (including possible data
720 * modifications which are not tracked via REDO) is
723 if ((ip->flags & HAMMER_INODE_REDO) && error == 0) {
724 if (ip->redo_count < hammer_limit_redo) {
725 bp->b_flags |= B_VFSFLAG1;
726 error = hammer_generate_redo(&trans, ip,
727 base_offset + offset,
732 ip->flags &= ~HAMMER_INODE_REDO;
737 * If we screwed up we have to undo any VM size changes we
743 nvtruncbuf(ap->a_vp, ip->ino_data.size,
744 hammer_blocksize(ip->ino_data.size),
745 hammer_blockoff(ip->ino_data.size));
749 kflags |= NOTE_WRITE;
750 hammer_stats_file_write += n;
751 /* bp->b_flags |= B_CLUSTEROK; temporarily disabled */
752 if (ip->ino_data.size < uio->uio_offset) {
753 ip->ino_data.size = uio->uio_offset;
754 flags = HAMMER_INODE_SDIRTY;
758 ip->ino_data.mtime = trans.time;
759 flags |= HAMMER_INODE_MTIME | HAMMER_INODE_BUFS;
760 hammer_modify_inode(&trans, ip, flags);
763 * Once we dirty the buffer any cached zone-X offset
764 * becomes invalid. HAMMER NOTE: no-history mode cannot
765 * allow overwriting over the same data sector unless
766 * we provide UNDOs for the old data, which we don't.
768 bp->b_bio2.bio_offset = NOOFFSET;
771 * Final buffer disposition.
773 * Because meta-data updates are deferred, HAMMER is
774 * especially sensitive to excessive bdwrite()s because
775 * the I/O stream is not broken up by disk reads. So the
776 * buffer cache simply cannot keep up.
778 * WARNING! blksize is variable. cluster_write() is
779 * expected to not blow up if it encounters
780 * buffers that do not match the passed blksize.
782 * NOTE! Hammer shouldn't need to bawrite()/cluster_write().
783 * The ip->rsv_recs check should burst-flush the data.
784 * If we queue it immediately the buf could be left
785 * locked on the device queue for a very long time.
787 * NOTE! To avoid degenerate stalls due to mismatched block
788 * sizes we only honor IO_DIRECT on the write which
789 * abuts the end of the buffer. However, we must
790 * honor IO_SYNC in case someone is silly enough to
791 * configure a HAMMER file as swap, or when HAMMER
792 * is serving NFS (for commits). Ick ick.
794 bp->b_flags |= B_AGE;
795 if (ap->a_ioflag & IO_SYNC) {
797 } else if ((ap->a_ioflag & IO_DIRECT) && endofblk) {
801 if (offset + n == blksize) {
802 if (hammer_cluster_enable == 0 ||
803 (ap->a_vp->v_mount->mnt_flag & MNT_NOCLUSTERW)) {
806 cluster_write(bp, ip->ino_data.size,
814 hammer_done_transaction(&trans);
815 hammer_knote(ap->a_vp, kflags);
816 lwkt_reltoken(&hmp->fs_token);
821 * hammer_vop_access { vp, mode, cred }
823 * MPSAFE - does not require fs_token
827 hammer_vop_access(struct vop_access_args *ap)
829 struct hammer_inode *ip = VTOI(ap->a_vp);
834 ++hammer_stats_file_iopsr;
835 uid = hammer_to_unix_xid(&ip->ino_data.uid);
836 gid = hammer_to_unix_xid(&ip->ino_data.gid);
838 error = vop_helper_access(ap, uid, gid, ip->ino_data.mode,
839 ip->ino_data.uflags);
844 * hammer_vop_advlock { vp, id, op, fl, flags }
846 * MPSAFE - does not require fs_token
850 hammer_vop_advlock(struct vop_advlock_args *ap)
852 hammer_inode_t ip = VTOI(ap->a_vp);
854 return (lf_advlock(ap, &ip->advlock, ip->ino_data.size));
858 * hammer_vop_close { vp, fflag }
860 * We can only sync-on-close for normal closes. XXX disabled for now.
864 hammer_vop_close(struct vop_close_args *ap)
867 struct vnode *vp = ap->a_vp;
868 hammer_inode_t ip = VTOI(vp);
870 if (ip->flags & (HAMMER_INODE_CLOSESYNC|HAMMER_INODE_CLOSEASYNC)) {
871 if (vn_islocked(vp) == LK_EXCLUSIVE &&
872 (vp->v_flag & (VINACTIVE|VRECLAIMED)) == 0) {
873 if (ip->flags & HAMMER_INODE_CLOSESYNC)
876 waitfor = MNT_NOWAIT;
877 ip->flags &= ~(HAMMER_INODE_CLOSESYNC |
878 HAMMER_INODE_CLOSEASYNC);
879 VOP_FSYNC(vp, MNT_NOWAIT, waitfor);
883 return (vop_stdclose(ap));
887 * hammer_vop_ncreate { nch, dvp, vpp, cred, vap }
889 * The operating system has already ensured that the directory entry
890 * does not exist and done all appropriate namespace locking.
894 hammer_vop_ncreate(struct vop_ncreate_args *ap)
896 struct hammer_transaction trans;
897 struct hammer_inode *dip;
898 struct hammer_inode *nip;
899 struct nchandle *nch;
904 dip = VTOI(ap->a_dvp);
907 if (dip->flags & HAMMER_INODE_RO)
909 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0)
913 * Create a transaction to cover the operations we perform.
915 lwkt_gettoken(&hmp->fs_token);
916 hammer_start_transaction(&trans, hmp);
917 ++hammer_stats_file_iopsw;
920 * Create a new filesystem object of the requested type. The
921 * returned inode will be referenced and shared-locked to prevent
922 * it from being moved to the flusher.
924 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
925 dip, nch->ncp->nc_name, nch->ncp->nc_nlen,
928 hkprintf("hammer_create_inode error %d\n", error);
929 hammer_done_transaction(&trans);
931 lwkt_reltoken(&hmp->fs_token);
936 * Add the new filesystem object to the directory. This will also
937 * bump the inode's link count.
939 error = hammer_ip_add_directory(&trans, dip,
940 nch->ncp->nc_name, nch->ncp->nc_nlen,
943 hkprintf("hammer_ip_add_directory error %d\n", error);
949 hammer_rel_inode(nip, 0);
950 hammer_done_transaction(&trans);
953 error = hammer_get_vnode(nip, ap->a_vpp);
954 hammer_done_transaction(&trans);
955 hammer_rel_inode(nip, 0);
957 cache_setunresolved(ap->a_nch);
958 cache_setvp(ap->a_nch, *ap->a_vpp);
960 hammer_knote(ap->a_dvp, NOTE_WRITE);
962 lwkt_reltoken(&hmp->fs_token);
967 * hammer_vop_getattr { vp, vap }
969 * Retrieve an inode's attribute information. When accessing inodes
970 * historically we fake the atime field to ensure consistent results.
971 * The atime field is stored in the B-Tree element and allowed to be
972 * updated without cycling the element.
974 * MPSAFE - does not require fs_token
978 hammer_vop_getattr(struct vop_getattr_args *ap)
980 struct hammer_inode *ip = VTOI(ap->a_vp);
981 struct vattr *vap = ap->a_vap;
984 * We want the fsid to be different when accessing a filesystem
985 * with different as-of's so programs like diff don't think
986 * the files are the same.
988 * We also want the fsid to be the same when comparing snapshots,
989 * or when comparing mirrors (which might be backed by different
990 * physical devices). HAMMER fsids are based on the PFS's
993 * XXX there is a chance of collision here. The va_fsid reported
994 * by stat is different from the more involved fsid used in the
997 ++hammer_stats_file_iopsr;
998 hammer_lock_sh(&ip->lock);
999 vap->va_fsid = ip->pfsm->fsid_udev ^ (u_int32_t)ip->obj_asof ^
1000 (u_int32_t)(ip->obj_asof >> 32);
1002 vap->va_fileid = ip->ino_leaf.base.obj_id;
1003 vap->va_mode = ip->ino_data.mode;
1004 vap->va_nlink = ip->ino_data.nlinks;
1005 vap->va_uid = hammer_to_unix_xid(&ip->ino_data.uid);
1006 vap->va_gid = hammer_to_unix_xid(&ip->ino_data.gid);
1009 vap->va_size = ip->ino_data.size;
1012 * Special case for @@PFS softlinks. The actual size of the
1013 * expanded softlink is "@@0x%016llx:%05d" == 26 bytes.
1014 * or for MAX_TID is "@@-1:%05d" == 10 bytes.
1016 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_SOFTLINK &&
1017 ip->ino_data.size == 10 &&
1018 ip->obj_asof == HAMMER_MAX_TID &&
1019 ip->obj_localization == 0 &&
1020 strncmp(ip->ino_data.ext.symlink, "@@PFS", 5) == 0) {
1021 if (ip->pfsm->pfsd.mirror_flags & HAMMER_PFSD_SLAVE)
1028 * We must provide a consistent atime and mtime for snapshots
1029 * so people can do a 'tar cf - ... | md5' on them and get
1030 * consistent results.
1032 if (ip->flags & HAMMER_INODE_RO) {
1033 hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_atime);
1034 hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_mtime);
1036 hammer_time_to_timespec(ip->ino_data.atime, &vap->va_atime);
1037 hammer_time_to_timespec(ip->ino_data.mtime, &vap->va_mtime);
1039 hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_ctime);
1040 vap->va_flags = ip->ino_data.uflags;
1041 vap->va_gen = 1; /* hammer inums are unique for all time */
1042 vap->va_blocksize = HAMMER_BUFSIZE;
1043 if (ip->ino_data.size >= HAMMER_XDEMARC) {
1044 vap->va_bytes = (ip->ino_data.size + HAMMER_XBUFMASK64) &
1046 } else if (ip->ino_data.size > HAMMER_BUFSIZE / 2) {
1047 vap->va_bytes = (ip->ino_data.size + HAMMER_BUFMASK64) &
1050 vap->va_bytes = (ip->ino_data.size + 15) & ~15;
1053 vap->va_type = hammer_get_vnode_type(ip->ino_data.obj_type);
1054 vap->va_filerev = 0; /* XXX */
1055 vap->va_uid_uuid = ip->ino_data.uid;
1056 vap->va_gid_uuid = ip->ino_data.gid;
1057 vap->va_fsid_uuid = ip->hmp->fsid;
1058 vap->va_vaflags = VA_UID_UUID_VALID | VA_GID_UUID_VALID |
1061 switch (ip->ino_data.obj_type) {
1062 case HAMMER_OBJTYPE_CDEV:
1063 case HAMMER_OBJTYPE_BDEV:
1064 vap->va_rmajor = ip->ino_data.rmajor;
1065 vap->va_rminor = ip->ino_data.rminor;
1070 hammer_unlock(&ip->lock);
1075 * hammer_vop_nresolve { nch, dvp, cred }
1077 * Locate the requested directory entry.
1081 hammer_vop_nresolve(struct vop_nresolve_args *ap)
1083 struct hammer_transaction trans;
1084 struct namecache *ncp;
1089 struct hammer_cursor cursor;
1098 u_int32_t localization;
1099 u_int32_t max_iterations;
1102 * Misc initialization, plus handle as-of name extensions. Look for
1103 * the '@@' extension. Note that as-of files and directories cannot
1106 dip = VTOI(ap->a_dvp);
1107 ncp = ap->a_nch->ncp;
1108 asof = dip->obj_asof;
1109 localization = dip->obj_localization; /* for code consistency */
1110 nlen = ncp->nc_nlen;
1111 flags = dip->flags & HAMMER_INODE_RO;
1115 lwkt_gettoken(&hmp->fs_token);
1116 hammer_simple_transaction(&trans, hmp);
1117 ++hammer_stats_file_iopsr;
1119 for (i = 0; i < nlen; ++i) {
1120 if (ncp->nc_name[i] == '@' && ncp->nc_name[i+1] == '@') {
1121 error = hammer_str_to_tid(ncp->nc_name + i + 2,
1122 &ispfs, &asof, &localization);
1127 if (asof != HAMMER_MAX_TID)
1128 flags |= HAMMER_INODE_RO;
1135 * If this is a PFS softlink we dive into the PFS
1137 if (ispfs && nlen == 0) {
1138 ip = hammer_get_inode(&trans, dip, HAMMER_OBJID_ROOT,
1142 error = hammer_get_vnode(ip, &vp);
1143 hammer_rel_inode(ip, 0);
1149 cache_setvp(ap->a_nch, vp);
1156 * If there is no path component the time extension is relative to dip.
1157 * e.g. "fubar/@@<snapshot>"
1159 * "." is handled by the kernel, but ".@@<snapshot>" is not.
1160 * e.g. "fubar/.@@<snapshot>"
1162 * ".." is handled by the kernel. We do not currently handle
1165 if (nlen == 0 || (nlen == 1 && ncp->nc_name[0] == '.')) {
1166 ip = hammer_get_inode(&trans, dip, dip->obj_id,
1167 asof, dip->obj_localization,
1170 error = hammer_get_vnode(ip, &vp);
1171 hammer_rel_inode(ip, 0);
1177 cache_setvp(ap->a_nch, vp);
1184 * Calculate the namekey and setup the key range for the scan. This
1185 * works kinda like a chained hash table where the lower 32 bits
1186 * of the namekey synthesize the chain.
1188 * The key range is inclusive of both key_beg and key_end.
1190 namekey = hammer_directory_namekey(dip, ncp->nc_name, nlen,
1193 error = hammer_init_cursor(&trans, &cursor, &dip->cache[1], dip);
1194 cursor.key_beg.localization = dip->obj_localization +
1195 hammer_dir_localization(dip);
1196 cursor.key_beg.obj_id = dip->obj_id;
1197 cursor.key_beg.key = namekey;
1198 cursor.key_beg.create_tid = 0;
1199 cursor.key_beg.delete_tid = 0;
1200 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1201 cursor.key_beg.obj_type = 0;
1203 cursor.key_end = cursor.key_beg;
1204 cursor.key_end.key += max_iterations;
1206 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
1209 * Scan all matching records (the chain), locate the one matching
1210 * the requested path component.
1212 * The hammer_ip_*() functions merge in-memory records with on-disk
1213 * records for the purposes of the search.
1216 localization = HAMMER_DEF_LOCALIZATION;
1219 error = hammer_ip_first(&cursor);
1220 while (error == 0) {
1221 error = hammer_ip_resolve_data(&cursor);
1224 if (nlen == cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF &&
1225 bcmp(ncp->nc_name, cursor.data->entry.name, nlen) == 0) {
1226 obj_id = cursor.data->entry.obj_id;
1227 localization = cursor.data->entry.localization;
1230 error = hammer_ip_next(&cursor);
1233 hammer_done_cursor(&cursor);
1236 * Lookup the obj_id. This should always succeed. If it does not
1237 * the filesystem may be damaged and we return a dummy inode.
1240 ip = hammer_get_inode(&trans, dip, obj_id,
1243 if (error == ENOENT) {
1244 kprintf("HAMMER: WARNING: Missing "
1245 "inode for dirent \"%s\"\n"
1246 "\tobj_id = %016llx, asof=%016llx, lo=%08x\n",
1248 (long long)obj_id, (long long)asof,
1251 ip = hammer_get_dummy_inode(&trans, dip, obj_id,
1256 error = hammer_get_vnode(ip, &vp);
1257 hammer_rel_inode(ip, 0);
1263 cache_setvp(ap->a_nch, vp);
1266 } else if (error == ENOENT) {
1267 cache_setvp(ap->a_nch, NULL);
1270 hammer_done_transaction(&trans);
1271 lwkt_reltoken(&hmp->fs_token);
1276 * hammer_vop_nlookupdotdot { dvp, vpp, cred }
1278 * Locate the parent directory of a directory vnode.
1280 * dvp is referenced but not locked. *vpp must be returned referenced and
1281 * locked. A parent_obj_id of 0 does not necessarily indicate that we are
1282 * at the root, instead it could indicate that the directory we were in was
1285 * NOTE: as-of sequences are not linked into the directory structure. If
1286 * we are at the root with a different asof then the mount point, reload
1287 * the same directory with the mount point's asof. I'm not sure what this
1288 * will do to NFS. We encode ASOF stamps in NFS file handles so it might not
1289 * get confused, but it hasn't been tested.
1293 hammer_vop_nlookupdotdot(struct vop_nlookupdotdot_args *ap)
1295 struct hammer_transaction trans;
1296 struct hammer_inode *dip;
1297 struct hammer_inode *ip;
1299 int64_t parent_obj_id;
1300 u_int32_t parent_obj_localization;
1304 dip = VTOI(ap->a_dvp);
1305 asof = dip->obj_asof;
1309 * Whos are parent? This could be the root of a pseudo-filesystem
1310 * whos parent is in another localization domain.
1312 lwkt_gettoken(&hmp->fs_token);
1313 parent_obj_id = dip->ino_data.parent_obj_id;
1314 if (dip->obj_id == HAMMER_OBJID_ROOT)
1315 parent_obj_localization = dip->ino_data.ext.obj.parent_obj_localization;
1317 parent_obj_localization = dip->obj_localization;
1319 if (parent_obj_id == 0) {
1320 if (dip->obj_id == HAMMER_OBJID_ROOT &&
1321 asof != hmp->asof) {
1322 parent_obj_id = dip->obj_id;
1324 *ap->a_fakename = kmalloc(19, M_TEMP, M_WAITOK);
1325 ksnprintf(*ap->a_fakename, 19, "0x%016llx",
1326 (long long)dip->obj_asof);
1329 lwkt_reltoken(&hmp->fs_token);
1334 hammer_simple_transaction(&trans, hmp);
1335 ++hammer_stats_file_iopsr;
1337 ip = hammer_get_inode(&trans, dip, parent_obj_id,
1338 asof, parent_obj_localization,
1339 dip->flags, &error);
1341 error = hammer_get_vnode(ip, ap->a_vpp);
1342 hammer_rel_inode(ip, 0);
1346 hammer_done_transaction(&trans);
1347 lwkt_reltoken(&hmp->fs_token);
1352 * hammer_vop_nlink { nch, dvp, vp, cred }
1356 hammer_vop_nlink(struct vop_nlink_args *ap)
1358 struct hammer_transaction trans;
1359 struct hammer_inode *dip;
1360 struct hammer_inode *ip;
1361 struct nchandle *nch;
1365 if (ap->a_dvp->v_mount != ap->a_vp->v_mount)
1369 dip = VTOI(ap->a_dvp);
1370 ip = VTOI(ap->a_vp);
1373 if (dip->obj_localization != ip->obj_localization)
1376 if (dip->flags & HAMMER_INODE_RO)
1378 if (ip->flags & HAMMER_INODE_RO)
1380 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0)
1384 * Create a transaction to cover the operations we perform.
1386 lwkt_gettoken(&hmp->fs_token);
1387 hammer_start_transaction(&trans, hmp);
1388 ++hammer_stats_file_iopsw;
1391 * Add the filesystem object to the directory. Note that neither
1392 * dip nor ip are referenced or locked, but their vnodes are
1393 * referenced. This function will bump the inode's link count.
1395 error = hammer_ip_add_directory(&trans, dip,
1396 nch->ncp->nc_name, nch->ncp->nc_nlen,
1403 cache_setunresolved(nch);
1404 cache_setvp(nch, ap->a_vp);
1406 hammer_done_transaction(&trans);
1407 hammer_knote(ap->a_vp, NOTE_LINK);
1408 hammer_knote(ap->a_dvp, NOTE_WRITE);
1409 lwkt_reltoken(&hmp->fs_token);
1414 * hammer_vop_nmkdir { nch, dvp, vpp, cred, vap }
1416 * The operating system has already ensured that the directory entry
1417 * does not exist and done all appropriate namespace locking.
1421 hammer_vop_nmkdir(struct vop_nmkdir_args *ap)
1423 struct hammer_transaction trans;
1424 struct hammer_inode *dip;
1425 struct hammer_inode *nip;
1426 struct nchandle *nch;
1431 dip = VTOI(ap->a_dvp);
1434 if (dip->flags & HAMMER_INODE_RO)
1436 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0)
1440 * Create a transaction to cover the operations we perform.
1442 lwkt_gettoken(&hmp->fs_token);
1443 hammer_start_transaction(&trans, hmp);
1444 ++hammer_stats_file_iopsw;
1447 * Create a new filesystem object of the requested type. The
1448 * returned inode will be referenced but not locked.
1450 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
1451 dip, nch->ncp->nc_name, nch->ncp->nc_nlen,
1454 hkprintf("hammer_mkdir error %d\n", error);
1455 hammer_done_transaction(&trans);
1457 lwkt_reltoken(&hmp->fs_token);
1461 * Add the new filesystem object to the directory. This will also
1462 * bump the inode's link count.
1464 error = hammer_ip_add_directory(&trans, dip,
1465 nch->ncp->nc_name, nch->ncp->nc_nlen,
1468 hkprintf("hammer_mkdir (add) error %d\n", error);
1474 hammer_rel_inode(nip, 0);
1477 error = hammer_get_vnode(nip, ap->a_vpp);
1478 hammer_rel_inode(nip, 0);
1480 cache_setunresolved(ap->a_nch);
1481 cache_setvp(ap->a_nch, *ap->a_vpp);
1484 hammer_done_transaction(&trans);
1486 hammer_knote(ap->a_dvp, NOTE_WRITE | NOTE_LINK);
1487 lwkt_reltoken(&hmp->fs_token);
1492 * hammer_vop_nmknod { nch, dvp, vpp, cred, vap }
1494 * The operating system has already ensured that the directory entry
1495 * does not exist and done all appropriate namespace locking.
1499 hammer_vop_nmknod(struct vop_nmknod_args *ap)
1501 struct hammer_transaction trans;
1502 struct hammer_inode *dip;
1503 struct hammer_inode *nip;
1504 struct nchandle *nch;
1509 dip = VTOI(ap->a_dvp);
1512 if (dip->flags & HAMMER_INODE_RO)
1514 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0)
1518 * Create a transaction to cover the operations we perform.
1520 lwkt_gettoken(&hmp->fs_token);
1521 hammer_start_transaction(&trans, hmp);
1522 ++hammer_stats_file_iopsw;
1525 * Create a new filesystem object of the requested type. The
1526 * returned inode will be referenced but not locked.
1528 * If mknod specifies a directory a pseudo-fs is created.
1530 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
1531 dip, nch->ncp->nc_name, nch->ncp->nc_nlen,
1534 hammer_done_transaction(&trans);
1536 lwkt_reltoken(&hmp->fs_token);
1541 * Add the new filesystem object to the directory. This will also
1542 * bump the inode's link count.
1544 error = hammer_ip_add_directory(&trans, dip,
1545 nch->ncp->nc_name, nch->ncp->nc_nlen,
1552 hammer_rel_inode(nip, 0);
1555 error = hammer_get_vnode(nip, ap->a_vpp);
1556 hammer_rel_inode(nip, 0);
1558 cache_setunresolved(ap->a_nch);
1559 cache_setvp(ap->a_nch, *ap->a_vpp);
1562 hammer_done_transaction(&trans);
1564 hammer_knote(ap->a_dvp, NOTE_WRITE);
1565 lwkt_reltoken(&hmp->fs_token);
1570 * hammer_vop_open { vp, mode, cred, fp }
1572 * MPSAFE (does not require fs_token)
1576 hammer_vop_open(struct vop_open_args *ap)
1580 ++hammer_stats_file_iopsr;
1581 ip = VTOI(ap->a_vp);
1583 if ((ap->a_mode & FWRITE) && (ip->flags & HAMMER_INODE_RO))
1585 return(vop_stdopen(ap));
1589 * hammer_vop_print { vp }
1593 hammer_vop_print(struct vop_print_args *ap)
1599 * hammer_vop_readdir { vp, uio, cred, *eofflag, *ncookies, off_t **cookies }
1603 hammer_vop_readdir(struct vop_readdir_args *ap)
1605 struct hammer_transaction trans;
1606 struct hammer_cursor cursor;
1607 struct hammer_inode *ip;
1610 hammer_base_elm_t base;
1619 ++hammer_stats_file_iopsr;
1620 ip = VTOI(ap->a_vp);
1622 saveoff = uio->uio_offset;
1625 if (ap->a_ncookies) {
1626 ncookies = uio->uio_resid / 16 + 1;
1627 if (ncookies > 1024)
1629 cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
1637 lwkt_gettoken(&hmp->fs_token);
1638 hammer_simple_transaction(&trans, hmp);
1641 * Handle artificial entries
1643 * It should be noted that the minimum value for a directory
1644 * hash key on-media is 0x0000000100000000, so we can use anything
1645 * less then that to represent our 'special' key space.
1649 r = vop_write_dirent(&error, uio, ip->obj_id, DT_DIR, 1, ".");
1653 cookies[cookie_index] = saveoff;
1656 if (cookie_index == ncookies)
1660 if (ip->ino_data.parent_obj_id) {
1661 r = vop_write_dirent(&error, uio,
1662 ip->ino_data.parent_obj_id,
1665 r = vop_write_dirent(&error, uio,
1666 ip->obj_id, DT_DIR, 2, "..");
1671 cookies[cookie_index] = saveoff;
1674 if (cookie_index == ncookies)
1679 * Key range (begin and end inclusive) to scan. Directory keys
1680 * directly translate to a 64 bit 'seek' position.
1682 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
1683 cursor.key_beg.localization = ip->obj_localization +
1684 hammer_dir_localization(ip);
1685 cursor.key_beg.obj_id = ip->obj_id;
1686 cursor.key_beg.create_tid = 0;
1687 cursor.key_beg.delete_tid = 0;
1688 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1689 cursor.key_beg.obj_type = 0;
1690 cursor.key_beg.key = saveoff;
1692 cursor.key_end = cursor.key_beg;
1693 cursor.key_end.key = HAMMER_MAX_KEY;
1694 cursor.asof = ip->obj_asof;
1695 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
1697 error = hammer_ip_first(&cursor);
1699 while (error == 0) {
1700 error = hammer_ip_resolve_data(&cursor);
1703 base = &cursor.leaf->base;
1704 saveoff = base->key;
1705 KKASSERT(cursor.leaf->data_len > HAMMER_ENTRY_NAME_OFF);
1707 if (base->obj_id != ip->obj_id)
1708 panic("readdir: bad record at %p", cursor.node);
1711 * Convert pseudo-filesystems into softlinks
1713 dtype = hammer_get_dtype(cursor.leaf->base.obj_type);
1714 r = vop_write_dirent(
1715 &error, uio, cursor.data->entry.obj_id,
1717 cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF ,
1718 (void *)cursor.data->entry.name);
1723 cookies[cookie_index] = base->key;
1725 if (cookie_index == ncookies)
1727 error = hammer_ip_next(&cursor);
1729 hammer_done_cursor(&cursor);
1732 hammer_done_transaction(&trans);
1735 *ap->a_eofflag = (error == ENOENT);
1736 uio->uio_offset = saveoff;
1737 if (error && cookie_index == 0) {
1738 if (error == ENOENT)
1741 kfree(cookies, M_TEMP);
1742 *ap->a_ncookies = 0;
1743 *ap->a_cookies = NULL;
1746 if (error == ENOENT)
1749 *ap->a_ncookies = cookie_index;
1750 *ap->a_cookies = cookies;
1753 lwkt_reltoken(&hmp->fs_token);
1758 * hammer_vop_readlink { vp, uio, cred }
1762 hammer_vop_readlink(struct vop_readlink_args *ap)
1764 struct hammer_transaction trans;
1765 struct hammer_cursor cursor;
1766 struct hammer_inode *ip;
1769 u_int32_t localization;
1770 hammer_pseudofs_inmem_t pfsm;
1773 ip = VTOI(ap->a_vp);
1776 lwkt_gettoken(&hmp->fs_token);
1779 * Shortcut if the symlink data was stuffed into ino_data.
1781 * Also expand special "@@PFS%05d" softlinks (expansion only
1782 * occurs for non-historical (current) accesses made from the
1783 * primary filesystem).
1785 if (ip->ino_data.size <= HAMMER_INODE_BASESYMLEN) {
1789 ptr = ip->ino_data.ext.symlink;
1790 bytes = (int)ip->ino_data.size;
1792 ip->obj_asof == HAMMER_MAX_TID &&
1793 ip->obj_localization == 0 &&
1794 strncmp(ptr, "@@PFS", 5) == 0) {
1795 hammer_simple_transaction(&trans, hmp);
1796 bcopy(ptr + 5, buf, 5);
1798 localization = strtoul(buf, NULL, 10) << 16;
1799 pfsm = hammer_load_pseudofs(&trans, localization,
1802 if (pfsm->pfsd.mirror_flags &
1803 HAMMER_PFSD_SLAVE) {
1804 /* vap->va_size == 26 */
1805 ksnprintf(buf, sizeof(buf),
1807 (long long)pfsm->pfsd.sync_end_tid,
1808 localization >> 16);
1810 /* vap->va_size == 10 */
1811 ksnprintf(buf, sizeof(buf),
1813 localization >> 16);
1815 ksnprintf(buf, sizeof(buf),
1817 (long long)HAMMER_MAX_TID,
1818 localization >> 16);
1822 bytes = strlen(buf);
1825 hammer_rel_pseudofs(hmp, pfsm);
1826 hammer_done_transaction(&trans);
1828 error = uiomove(ptr, bytes, ap->a_uio);
1829 lwkt_reltoken(&hmp->fs_token);
1836 hammer_simple_transaction(&trans, hmp);
1837 ++hammer_stats_file_iopsr;
1838 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
1841 * Key range (begin and end inclusive) to scan. Directory keys
1842 * directly translate to a 64 bit 'seek' position.
1844 cursor.key_beg.localization = ip->obj_localization +
1845 HAMMER_LOCALIZE_MISC;
1846 cursor.key_beg.obj_id = ip->obj_id;
1847 cursor.key_beg.create_tid = 0;
1848 cursor.key_beg.delete_tid = 0;
1849 cursor.key_beg.rec_type = HAMMER_RECTYPE_FIX;
1850 cursor.key_beg.obj_type = 0;
1851 cursor.key_beg.key = HAMMER_FIXKEY_SYMLINK;
1852 cursor.asof = ip->obj_asof;
1853 cursor.flags |= HAMMER_CURSOR_ASOF;
1855 error = hammer_ip_lookup(&cursor);
1857 error = hammer_ip_resolve_data(&cursor);
1859 KKASSERT(cursor.leaf->data_len >=
1860 HAMMER_SYMLINK_NAME_OFF);
1861 error = uiomove(cursor.data->symlink.name,
1862 cursor.leaf->data_len -
1863 HAMMER_SYMLINK_NAME_OFF,
1867 hammer_done_cursor(&cursor);
1868 hammer_done_transaction(&trans);
1869 lwkt_reltoken(&hmp->fs_token);
1874 * hammer_vop_nremove { nch, dvp, cred }
1878 hammer_vop_nremove(struct vop_nremove_args *ap)
1880 struct hammer_transaction trans;
1881 struct hammer_inode *dip;
1885 dip = VTOI(ap->a_dvp);
1888 if (hammer_nohistory(dip) == 0 &&
1889 (error = hammer_checkspace(hmp, HAMMER_CHKSPC_REMOVE)) != 0) {
1893 lwkt_gettoken(&hmp->fs_token);
1894 hammer_start_transaction(&trans, hmp);
1895 ++hammer_stats_file_iopsw;
1896 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp, ap->a_cred, 0, 0);
1897 hammer_done_transaction(&trans);
1899 hammer_knote(ap->a_dvp, NOTE_WRITE);
1900 lwkt_reltoken(&hmp->fs_token);
1905 * hammer_vop_nrename { fnch, tnch, fdvp, tdvp, cred }
1909 hammer_vop_nrename(struct vop_nrename_args *ap)
1911 struct hammer_transaction trans;
1912 struct namecache *fncp;
1913 struct namecache *tncp;
1914 struct hammer_inode *fdip;
1915 struct hammer_inode *tdip;
1916 struct hammer_inode *ip;
1918 struct hammer_cursor cursor;
1920 u_int32_t max_iterations;
1923 if (ap->a_fdvp->v_mount != ap->a_tdvp->v_mount)
1925 if (ap->a_fdvp->v_mount != ap->a_fnch->ncp->nc_vp->v_mount)
1928 fdip = VTOI(ap->a_fdvp);
1929 tdip = VTOI(ap->a_tdvp);
1930 fncp = ap->a_fnch->ncp;
1931 tncp = ap->a_tnch->ncp;
1932 ip = VTOI(fncp->nc_vp);
1933 KKASSERT(ip != NULL);
1937 if (fdip->obj_localization != tdip->obj_localization)
1939 if (fdip->obj_localization != ip->obj_localization)
1942 if (fdip->flags & HAMMER_INODE_RO)
1944 if (tdip->flags & HAMMER_INODE_RO)
1946 if (ip->flags & HAMMER_INODE_RO)
1948 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0)
1951 lwkt_gettoken(&hmp->fs_token);
1952 hammer_start_transaction(&trans, hmp);
1953 ++hammer_stats_file_iopsw;
1956 * Remove tncp from the target directory and then link ip as
1957 * tncp. XXX pass trans to dounlink
1959 * Force the inode sync-time to match the transaction so it is
1960 * in-sync with the creation of the target directory entry.
1962 error = hammer_dounlink(&trans, ap->a_tnch, ap->a_tdvp,
1964 if (error == 0 || error == ENOENT) {
1965 error = hammer_ip_add_directory(&trans, tdip,
1966 tncp->nc_name, tncp->nc_nlen,
1969 ip->ino_data.parent_obj_id = tdip->obj_id;
1970 ip->ino_data.ctime = trans.time;
1971 hammer_modify_inode(&trans, ip, HAMMER_INODE_DDIRTY);
1975 goto failed; /* XXX */
1978 * Locate the record in the originating directory and remove it.
1980 * Calculate the namekey and setup the key range for the scan. This
1981 * works kinda like a chained hash table where the lower 32 bits
1982 * of the namekey synthesize the chain.
1984 * The key range is inclusive of both key_beg and key_end.
1986 namekey = hammer_directory_namekey(fdip, fncp->nc_name, fncp->nc_nlen,
1989 hammer_init_cursor(&trans, &cursor, &fdip->cache[1], fdip);
1990 cursor.key_beg.localization = fdip->obj_localization +
1991 hammer_dir_localization(fdip);
1992 cursor.key_beg.obj_id = fdip->obj_id;
1993 cursor.key_beg.key = namekey;
1994 cursor.key_beg.create_tid = 0;
1995 cursor.key_beg.delete_tid = 0;
1996 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1997 cursor.key_beg.obj_type = 0;
1999 cursor.key_end = cursor.key_beg;
2000 cursor.key_end.key += max_iterations;
2001 cursor.asof = fdip->obj_asof;
2002 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
2005 * Scan all matching records (the chain), locate the one matching
2006 * the requested path component.
2008 * The hammer_ip_*() functions merge in-memory records with on-disk
2009 * records for the purposes of the search.
2011 error = hammer_ip_first(&cursor);
2012 while (error == 0) {
2013 if (hammer_ip_resolve_data(&cursor) != 0)
2015 nlen = cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF;
2017 if (fncp->nc_nlen == nlen &&
2018 bcmp(fncp->nc_name, cursor.data->entry.name, nlen) == 0) {
2021 error = hammer_ip_next(&cursor);
2025 * If all is ok we have to get the inode so we can adjust nlinks.
2027 * WARNING: hammer_ip_del_directory() may have to terminate the
2028 * cursor to avoid a recursion. It's ok to call hammer_done_cursor()
2032 error = hammer_ip_del_directory(&trans, &cursor, fdip, ip);
2035 * XXX A deadlock here will break rename's atomicy for the purposes
2036 * of crash recovery.
2038 if (error == EDEADLK) {
2039 hammer_done_cursor(&cursor);
2044 * Cleanup and tell the kernel that the rename succeeded.
2046 * NOTE: ip->vp, if non-NULL, cannot be directly referenced
2047 * without formally acquiring the vp since the vp might
2048 * have zero refs on it, or in the middle of a reclaim,
2051 hammer_done_cursor(&cursor);
2053 cache_rename(ap->a_fnch, ap->a_tnch);
2054 hammer_knote(ap->a_fdvp, NOTE_WRITE);
2055 hammer_knote(ap->a_tdvp, NOTE_WRITE);
2059 error = hammer_get_vnode(ip, &vp);
2060 if (error == 0 && vp) {
2062 hammer_knote(ip->vp, NOTE_RENAME);
2066 kprintf("Debug: HAMMER ip/vp race2 avoided\n");
2071 hammer_done_transaction(&trans);
2072 lwkt_reltoken(&hmp->fs_token);
2077 * hammer_vop_nrmdir { nch, dvp, cred }
2081 hammer_vop_nrmdir(struct vop_nrmdir_args *ap)
2083 struct hammer_transaction trans;
2084 struct hammer_inode *dip;
2088 dip = VTOI(ap->a_dvp);
2091 if (hammer_nohistory(dip) == 0 &&
2092 (error = hammer_checkspace(hmp, HAMMER_CHKSPC_REMOVE)) != 0) {
2096 lwkt_gettoken(&hmp->fs_token);
2097 hammer_start_transaction(&trans, hmp);
2098 ++hammer_stats_file_iopsw;
2099 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp, ap->a_cred, 0, 1);
2100 hammer_done_transaction(&trans);
2102 hammer_knote(ap->a_dvp, NOTE_WRITE | NOTE_LINK);
2103 lwkt_reltoken(&hmp->fs_token);
2108 * hammer_vop_markatime { vp, cred }
2112 hammer_vop_markatime(struct vop_markatime_args *ap)
2114 struct hammer_transaction trans;
2115 struct hammer_inode *ip;
2118 ip = VTOI(ap->a_vp);
2119 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
2121 if (ip->flags & HAMMER_INODE_RO)
2124 if (hmp->mp->mnt_flag & MNT_NOATIME)
2126 lwkt_gettoken(&hmp->fs_token);
2127 hammer_start_transaction(&trans, hmp);
2128 ++hammer_stats_file_iopsw;
2130 ip->ino_data.atime = trans.time;
2131 hammer_modify_inode(&trans, ip, HAMMER_INODE_ATIME);
2132 hammer_done_transaction(&trans);
2133 hammer_knote(ap->a_vp, NOTE_ATTRIB);
2134 lwkt_reltoken(&hmp->fs_token);
2139 * hammer_vop_setattr { vp, vap, cred }
2143 hammer_vop_setattr(struct vop_setattr_args *ap)
2145 struct hammer_transaction trans;
2146 struct hammer_inode *ip;
2155 int64_t aligned_size;
2160 ip = ap->a_vp->v_data;
2165 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
2167 if (ip->flags & HAMMER_INODE_RO)
2169 if (hammer_nohistory(ip) == 0 &&
2170 (error = hammer_checkspace(hmp, HAMMER_CHKSPC_REMOVE)) != 0) {
2174 lwkt_gettoken(&hmp->fs_token);
2175 hammer_start_transaction(&trans, hmp);
2176 ++hammer_stats_file_iopsw;
2179 if (vap->va_flags != VNOVAL) {
2180 flags = ip->ino_data.uflags;
2181 error = vop_helper_setattr_flags(&flags, vap->va_flags,
2182 hammer_to_unix_xid(&ip->ino_data.uid),
2185 if (ip->ino_data.uflags != flags) {
2186 ip->ino_data.uflags = flags;
2187 ip->ino_data.ctime = trans.time;
2188 modflags |= HAMMER_INODE_DDIRTY;
2189 kflags |= NOTE_ATTRIB;
2191 if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) {
2198 if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) {
2202 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
2203 mode_t cur_mode = ip->ino_data.mode;
2204 uid_t cur_uid = hammer_to_unix_xid(&ip->ino_data.uid);
2205 gid_t cur_gid = hammer_to_unix_xid(&ip->ino_data.gid);
2209 error = vop_helper_chown(ap->a_vp, vap->va_uid, vap->va_gid,
2211 &cur_uid, &cur_gid, &cur_mode);
2213 hammer_guid_to_uuid(&uuid_uid, cur_uid);
2214 hammer_guid_to_uuid(&uuid_gid, cur_gid);
2215 if (bcmp(&uuid_uid, &ip->ino_data.uid,
2216 sizeof(uuid_uid)) ||
2217 bcmp(&uuid_gid, &ip->ino_data.gid,
2218 sizeof(uuid_gid)) ||
2219 ip->ino_data.mode != cur_mode
2221 ip->ino_data.uid = uuid_uid;
2222 ip->ino_data.gid = uuid_gid;
2223 ip->ino_data.mode = cur_mode;
2224 ip->ino_data.ctime = trans.time;
2225 modflags |= HAMMER_INODE_DDIRTY;
2227 kflags |= NOTE_ATTRIB;
2230 while (vap->va_size != VNOVAL && ip->ino_data.size != vap->va_size) {
2231 switch(ap->a_vp->v_type) {
2233 if (vap->va_size == ip->ino_data.size)
2237 * Log the operation if in fast-fsync mode or if
2238 * there are unterminated redo write records present.
2240 * The second check is needed so the recovery code
2241 * properly truncates write redos even if nominal
2242 * REDO operations is turned off due to excessive
2243 * writes, because the related records might be
2244 * destroyed and never lay down a TERM_WRITE.
2246 if ((ip->flags & HAMMER_INODE_REDO) ||
2247 (ip->flags & HAMMER_INODE_RDIRTY)) {
2248 error = hammer_generate_redo(&trans, ip,
2253 blksize = hammer_blocksize(vap->va_size);
2256 * XXX break atomicy, we can deadlock the backend
2257 * if we do not release the lock. Probably not a
2260 if (vap->va_size < ip->ino_data.size) {
2261 nvtruncbuf(ap->a_vp, vap->va_size,
2263 hammer_blockoff(vap->va_size));
2265 kflags |= NOTE_WRITE;
2267 nvextendbuf(ap->a_vp,
2270 hammer_blocksize(ip->ino_data.size),
2271 hammer_blocksize(vap->va_size),
2272 hammer_blockoff(ip->ino_data.size),
2273 hammer_blockoff(vap->va_size),
2276 kflags |= NOTE_WRITE | NOTE_EXTEND;
2278 ip->ino_data.size = vap->va_size;
2279 ip->ino_data.mtime = trans.time;
2280 /* XXX safe to use SDIRTY instead of DDIRTY here? */
2281 modflags |= HAMMER_INODE_MTIME | HAMMER_INODE_DDIRTY;
2284 * On-media truncation is cached in the inode until
2285 * the inode is synchronized. We must immediately
2286 * handle any frontend records.
2289 hammer_ip_frontend_trunc(ip, vap->va_size);
2290 #ifdef DEBUG_TRUNCATE
2291 if (HammerTruncIp == NULL)
2294 if ((ip->flags & HAMMER_INODE_TRUNCATED) == 0) {
2295 ip->flags |= HAMMER_INODE_TRUNCATED;
2296 ip->trunc_off = vap->va_size;
2297 #ifdef DEBUG_TRUNCATE
2298 if (ip == HammerTruncIp)
2299 kprintf("truncate1 %016llx\n",
2300 (long long)ip->trunc_off);
2302 } else if (ip->trunc_off > vap->va_size) {
2303 ip->trunc_off = vap->va_size;
2304 #ifdef DEBUG_TRUNCATE
2305 if (ip == HammerTruncIp)
2306 kprintf("truncate2 %016llx\n",
2307 (long long)ip->trunc_off);
2310 #ifdef DEBUG_TRUNCATE
2311 if (ip == HammerTruncIp)
2312 kprintf("truncate3 %016llx (ignored)\n",
2313 (long long)vap->va_size);
2320 * When truncating, nvtruncbuf() may have cleaned out
2321 * a portion of the last block on-disk in the buffer
2322 * cache. We must clean out any frontend records
2323 * for blocks beyond the new last block.
2325 aligned_size = (vap->va_size + (blksize - 1)) &
2326 ~(int64_t)(blksize - 1);
2327 if (truncating && vap->va_size < aligned_size) {
2328 aligned_size -= blksize;
2329 hammer_ip_frontend_trunc(ip, aligned_size);
2334 if ((ip->flags & HAMMER_INODE_TRUNCATED) == 0) {
2335 ip->flags |= HAMMER_INODE_TRUNCATED;
2336 ip->trunc_off = vap->va_size;
2337 } else if (ip->trunc_off > vap->va_size) {
2338 ip->trunc_off = vap->va_size;
2340 hammer_ip_frontend_trunc(ip, vap->va_size);
2341 ip->ino_data.size = vap->va_size;
2342 ip->ino_data.mtime = trans.time;
2343 modflags |= HAMMER_INODE_MTIME | HAMMER_INODE_DDIRTY;
2344 kflags |= NOTE_ATTRIB;
2352 if (vap->va_atime.tv_sec != VNOVAL) {
2353 ip->ino_data.atime = hammer_timespec_to_time(&vap->va_atime);
2354 modflags |= HAMMER_INODE_ATIME;
2355 kflags |= NOTE_ATTRIB;
2357 if (vap->va_mtime.tv_sec != VNOVAL) {
2358 ip->ino_data.mtime = hammer_timespec_to_time(&vap->va_mtime);
2359 modflags |= HAMMER_INODE_MTIME;
2360 kflags |= NOTE_ATTRIB;
2362 if (vap->va_mode != (mode_t)VNOVAL) {
2363 mode_t cur_mode = ip->ino_data.mode;
2364 uid_t cur_uid = hammer_to_unix_xid(&ip->ino_data.uid);
2365 gid_t cur_gid = hammer_to_unix_xid(&ip->ino_data.gid);
2367 error = vop_helper_chmod(ap->a_vp, vap->va_mode, ap->a_cred,
2368 cur_uid, cur_gid, &cur_mode);
2369 if (error == 0 && ip->ino_data.mode != cur_mode) {
2370 ip->ino_data.mode = cur_mode;
2371 ip->ino_data.ctime = trans.time;
2372 modflags |= HAMMER_INODE_DDIRTY;
2373 kflags |= NOTE_ATTRIB;
2378 hammer_modify_inode(&trans, ip, modflags);
2379 hammer_done_transaction(&trans);
2380 hammer_knote(ap->a_vp, kflags);
2381 lwkt_reltoken(&hmp->fs_token);
2386 * hammer_vop_nsymlink { nch, dvp, vpp, cred, vap, target }
2390 hammer_vop_nsymlink(struct vop_nsymlink_args *ap)
2392 struct hammer_transaction trans;
2393 struct hammer_inode *dip;
2394 struct hammer_inode *nip;
2395 hammer_record_t record;
2396 struct nchandle *nch;
2401 ap->a_vap->va_type = VLNK;
2404 dip = VTOI(ap->a_dvp);
2407 if (dip->flags & HAMMER_INODE_RO)
2409 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0)
2413 * Create a transaction to cover the operations we perform.
2415 lwkt_gettoken(&hmp->fs_token);
2416 hammer_start_transaction(&trans, hmp);
2417 ++hammer_stats_file_iopsw;
2420 * Create a new filesystem object of the requested type. The
2421 * returned inode will be referenced but not locked.
2424 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
2425 dip, nch->ncp->nc_name, nch->ncp->nc_nlen,
2428 hammer_done_transaction(&trans);
2430 lwkt_reltoken(&hmp->fs_token);
2435 * Add a record representing the symlink. symlink stores the link
2436 * as pure data, not a string, and is no \0 terminated.
2439 bytes = strlen(ap->a_target);
2441 if (bytes <= HAMMER_INODE_BASESYMLEN) {
2442 bcopy(ap->a_target, nip->ino_data.ext.symlink, bytes);
2444 record = hammer_alloc_mem_record(nip, bytes);
2445 record->type = HAMMER_MEM_RECORD_GENERAL;
2447 record->leaf.base.localization = nip->obj_localization +
2448 HAMMER_LOCALIZE_MISC;
2449 record->leaf.base.key = HAMMER_FIXKEY_SYMLINK;
2450 record->leaf.base.rec_type = HAMMER_RECTYPE_FIX;
2451 record->leaf.data_len = bytes;
2452 KKASSERT(HAMMER_SYMLINK_NAME_OFF == 0);
2453 bcopy(ap->a_target, record->data->symlink.name, bytes);
2454 error = hammer_ip_add_record(&trans, record);
2458 * Set the file size to the length of the link.
2461 nip->ino_data.size = bytes;
2462 hammer_modify_inode(&trans, nip, HAMMER_INODE_DDIRTY);
2466 error = hammer_ip_add_directory(&trans, dip, nch->ncp->nc_name,
2467 nch->ncp->nc_nlen, nip);
2473 hammer_rel_inode(nip, 0);
2476 error = hammer_get_vnode(nip, ap->a_vpp);
2477 hammer_rel_inode(nip, 0);
2479 cache_setunresolved(ap->a_nch);
2480 cache_setvp(ap->a_nch, *ap->a_vpp);
2481 hammer_knote(ap->a_dvp, NOTE_WRITE);
2484 hammer_done_transaction(&trans);
2485 lwkt_reltoken(&hmp->fs_token);
2490 * hammer_vop_nwhiteout { nch, dvp, cred, flags }
2494 hammer_vop_nwhiteout(struct vop_nwhiteout_args *ap)
2496 struct hammer_transaction trans;
2497 struct hammer_inode *dip;
2501 dip = VTOI(ap->a_dvp);
2504 if (hammer_nohistory(dip) == 0 &&
2505 (error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0) {
2509 lwkt_gettoken(&hmp->fs_token);
2510 hammer_start_transaction(&trans, hmp);
2511 ++hammer_stats_file_iopsw;
2512 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp,
2513 ap->a_cred, ap->a_flags, -1);
2514 hammer_done_transaction(&trans);
2515 lwkt_reltoken(&hmp->fs_token);
2521 * hammer_vop_ioctl { vp, command, data, fflag, cred }
2525 hammer_vop_ioctl(struct vop_ioctl_args *ap)
2527 struct hammer_inode *ip = ap->a_vp->v_data;
2528 hammer_mount_t hmp = ip->hmp;
2531 ++hammer_stats_file_iopsr;
2532 lwkt_gettoken(&hmp->fs_token);
2533 error = hammer_ioctl(ip, ap->a_command, ap->a_data,
2534 ap->a_fflag, ap->a_cred);
2535 lwkt_reltoken(&hmp->fs_token);
2541 hammer_vop_mountctl(struct vop_mountctl_args *ap)
2543 static const struct mountctl_opt extraopt[] = {
2544 { HMNT_NOHISTORY, "nohistory" },
2545 { HMNT_MASTERID, "master" },
2549 struct hammer_mount *hmp;
2556 mp = ap->a_head.a_ops->head.vv_mount;
2557 KKASSERT(mp->mnt_data != NULL);
2558 hmp = (struct hammer_mount *)mp->mnt_data;
2560 lwkt_gettoken(&hmp->fs_token);
2563 case MOUNTCTL_SET_EXPORT:
2564 if (ap->a_ctllen != sizeof(struct export_args))
2567 error = hammer_vfs_export(mp, ap->a_op,
2568 (const struct export_args *)ap->a_ctl);
2570 case MOUNTCTL_MOUNTFLAGS:
2573 * Call standard mountctl VOP function
2574 * so we get user mount flags.
2576 error = vop_stdmountctl(ap);
2580 usedbytes = *ap->a_res;
2582 if (usedbytes > 0 && usedbytes < ap->a_buflen) {
2583 usedbytes += vfs_flagstostr(hmp->hflags, extraopt,
2585 ap->a_buflen - usedbytes,
2589 *ap->a_res += usedbytes;
2593 error = vop_stdmountctl(ap);
2596 lwkt_reltoken(&hmp->fs_token);
2601 * hammer_vop_strategy { vp, bio }
2603 * Strategy call, used for regular file read & write only. Note that the
2604 * bp may represent a cluster.
2606 * To simplify operation and allow better optimizations in the future,
2607 * this code does not make any assumptions with regards to buffer alignment
2612 hammer_vop_strategy(struct vop_strategy_args *ap)
2617 bp = ap->a_bio->bio_buf;
2621 error = hammer_vop_strategy_read(ap);
2624 error = hammer_vop_strategy_write(ap);
2627 bp->b_error = error = EINVAL;
2628 bp->b_flags |= B_ERROR;
2633 /* hammer_dump_dedup_cache(((hammer_inode_t)ap->a_vp->v_data)->hmp); */
2639 * Read from a regular file. Iterate the related records and fill in the
2640 * BIO/BUF. Gaps are zero-filled.
2642 * The support code in hammer_object.c should be used to deal with mixed
2643 * in-memory and on-disk records.
2645 * NOTE: Can be called from the cluster code with an oversized buf.
2651 hammer_vop_strategy_read(struct vop_strategy_args *ap)
2653 struct hammer_transaction trans;
2654 struct hammer_inode *ip;
2655 struct hammer_inode *dip;
2657 struct hammer_cursor cursor;
2658 hammer_base_elm_t base;
2659 hammer_off_t disk_offset;
2674 ip = ap->a_vp->v_data;
2678 * The zone-2 disk offset may have been set by the cluster code via
2679 * a BMAP operation, or else should be NOOFFSET.
2681 * Checking the high bits for a match against zone-2 should suffice.
2683 * In cases where a lot of data duplication is present it may be
2684 * more beneficial to drop through and doubule-buffer through the
2687 nbio = push_bio(bio);
2688 if (hammer_double_buffer == 0 &&
2689 (nbio->bio_offset & HAMMER_OFF_ZONE_MASK) ==
2690 HAMMER_ZONE_LARGE_DATA) {
2691 lwkt_gettoken(&hmp->fs_token);
2692 error = hammer_io_direct_read(hmp, nbio, NULL);
2693 lwkt_reltoken(&hmp->fs_token);
2698 * Well, that sucked. Do it the hard way. If all the stars are
2699 * aligned we may still be able to issue a direct-read.
2701 lwkt_gettoken(&hmp->fs_token);
2702 hammer_simple_transaction(&trans, hmp);
2703 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
2706 * Key range (begin and end inclusive) to scan. Note that the key's
2707 * stored in the actual records represent BASE+LEN, not BASE. The
2708 * first record containing bio_offset will have a key > bio_offset.
2710 cursor.key_beg.localization = ip->obj_localization +
2711 HAMMER_LOCALIZE_MISC;
2712 cursor.key_beg.obj_id = ip->obj_id;
2713 cursor.key_beg.create_tid = 0;
2714 cursor.key_beg.delete_tid = 0;
2715 cursor.key_beg.obj_type = 0;
2716 cursor.key_beg.key = bio->bio_offset + 1;
2717 cursor.asof = ip->obj_asof;
2718 cursor.flags |= HAMMER_CURSOR_ASOF;
2720 cursor.key_end = cursor.key_beg;
2721 KKASSERT(ip->ino_data.obj_type == HAMMER_OBJTYPE_REGFILE);
2723 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_DBFILE) {
2724 cursor.key_beg.rec_type = HAMMER_RECTYPE_DB;
2725 cursor.key_end.rec_type = HAMMER_RECTYPE_DB;
2726 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
2730 ran_end = bio->bio_offset + bp->b_bufsize;
2731 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
2732 cursor.key_end.rec_type = HAMMER_RECTYPE_DATA;
2733 tmp64 = ran_end + MAXPHYS + 1; /* work-around GCC-4 bug */
2734 if (tmp64 < ran_end)
2735 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
2737 cursor.key_end.key = ran_end + MAXPHYS + 1;
2739 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
2741 error = hammer_ip_first(&cursor);
2744 while (error == 0) {
2746 * Get the base file offset of the record. The key for
2747 * data records is (base + bytes) rather then (base).
2749 base = &cursor.leaf->base;
2750 rec_offset = base->key - cursor.leaf->data_len;
2753 * Calculate the gap, if any, and zero-fill it.
2755 * n is the offset of the start of the record verses our
2756 * current seek offset in the bio.
2758 n = (int)(rec_offset - (bio->bio_offset + boff));
2760 if (n > bp->b_bufsize - boff)
2761 n = bp->b_bufsize - boff;
2762 bzero((char *)bp->b_data + boff, n);
2768 * Calculate the data offset in the record and the number
2769 * of bytes we can copy.
2771 * There are two degenerate cases. First, boff may already
2772 * be at bp->b_bufsize. Secondly, the data offset within
2773 * the record may exceed the record's size.
2777 n = cursor.leaf->data_len - roff;
2779 kprintf("strategy_read: bad n=%d roff=%d\n", n, roff);
2781 } else if (n > bp->b_bufsize - boff) {
2782 n = bp->b_bufsize - boff;
2786 * Deal with cached truncations. This cool bit of code
2787 * allows truncate()/ftruncate() to avoid having to sync
2790 * If the frontend is truncated then all backend records are
2791 * subject to the frontend's truncation.
2793 * If the backend is truncated then backend records on-disk
2794 * (but not in-memory) are subject to the backend's
2795 * truncation. In-memory records owned by the backend
2796 * represent data written after the truncation point on the
2797 * backend and must not be truncated.
2799 * Truncate operations deal with frontend buffer cache
2800 * buffers and frontend-owned in-memory records synchronously.
2802 if (ip->flags & HAMMER_INODE_TRUNCATED) {
2803 if (hammer_cursor_ondisk(&cursor)/* ||
2804 cursor.iprec->flush_state == HAMMER_FST_FLUSH*/) {
2805 if (ip->trunc_off <= rec_offset)
2807 else if (ip->trunc_off < rec_offset + n)
2808 n = (int)(ip->trunc_off - rec_offset);
2811 if (ip->sync_flags & HAMMER_INODE_TRUNCATED) {
2812 if (hammer_cursor_ondisk(&cursor)) {
2813 if (ip->sync_trunc_off <= rec_offset)
2815 else if (ip->sync_trunc_off < rec_offset + n)
2816 n = (int)(ip->sync_trunc_off - rec_offset);
2821 * Try to issue a direct read into our bio if possible,
2822 * otherwise resolve the element data into a hammer_buffer
2825 * The buffer on-disk should be zerod past any real
2826 * truncation point, but may not be for any synthesized
2827 * truncation point from above.
2829 disk_offset = cursor.leaf->data_offset + roff;
2830 isdedupable = (boff == 0 && n == bp->b_bufsize &&
2831 hammer_cursor_ondisk(&cursor) &&
2832 ((int)disk_offset & HAMMER_BUFMASK) == 0);
2834 if (isdedupable && hammer_double_buffer == 0) {
2835 KKASSERT((disk_offset & HAMMER_OFF_ZONE_MASK) ==
2836 HAMMER_ZONE_LARGE_DATA);
2837 nbio->bio_offset = disk_offset;
2838 error = hammer_io_direct_read(hmp, nbio, cursor.leaf);
2839 if (hammer_live_dedup && error == 0)
2840 hammer_dedup_cache_add(ip, cursor.leaf);
2843 error = hammer_ip_resolve_data(&cursor);
2845 if (hammer_live_dedup && isdedupable)
2846 hammer_dedup_cache_add(ip, cursor.leaf);
2847 bcopy((char *)cursor.data + roff,
2848 (char *)bp->b_data + boff, n);
2855 * We have to be sure that the only elements added to the
2856 * dedup cache are those which are already on-media.
2858 if (hammer_live_dedup && hammer_cursor_ondisk(&cursor))
2859 hammer_dedup_cache_add(ip, cursor.leaf);
2862 * Iterate until we have filled the request.
2865 if (boff == bp->b_bufsize)
2867 error = hammer_ip_next(&cursor);
2871 * There may have been a gap after the last record
2873 if (error == ENOENT)
2875 if (error == 0 && boff != bp->b_bufsize) {
2876 KKASSERT(boff < bp->b_bufsize);
2877 bzero((char *)bp->b_data + boff, bp->b_bufsize - boff);
2878 /* boff = bp->b_bufsize; */
2881 bp->b_error = error;
2883 bp->b_flags |= B_ERROR;
2888 * Cache the b-tree node for the last data read in cache[1].
2890 * If we hit the file EOF then also cache the node in the
2891 * governing director's cache[3], it will be used to initialize
2892 * the inode's cache[1] for any inodes looked up via the directory.
2894 * This doesn't reduce disk accesses since the B-Tree chain is
2895 * likely cached, but it does reduce cpu overhead when looking
2896 * up file offsets for cpdup/tar/cpio style iterations.
2899 hammer_cache_node(&ip->cache[1], cursor.node);
2900 if (ran_end >= ip->ino_data.size) {
2901 dip = hammer_find_inode(&trans, ip->ino_data.parent_obj_id,
2902 ip->obj_asof, ip->obj_localization);
2904 hammer_cache_node(&dip->cache[3], cursor.node);
2905 hammer_rel_inode(dip, 0);
2908 hammer_done_cursor(&cursor);
2909 hammer_done_transaction(&trans);
2910 lwkt_reltoken(&hmp->fs_token);
2915 * BMAP operation - used to support cluster_read() only.
2917 * (struct vnode *vp, off_t loffset, off_t *doffsetp, int *runp, int *runb)
2919 * This routine may return EOPNOTSUPP if the opration is not supported for
2920 * the specified offset. The contents of the pointer arguments do not
2921 * need to be initialized in that case.
2923 * If a disk address is available and properly aligned return 0 with
2924 * *doffsetp set to the zone-2 address, and *runp / *runb set appropriately
2925 * to the run-length relative to that offset. Callers may assume that
2926 * *doffsetp is valid if 0 is returned, even if *runp is not sufficiently
2927 * large, so return EOPNOTSUPP if it is not sufficiently large.
2931 hammer_vop_bmap(struct vop_bmap_args *ap)
2933 struct hammer_transaction trans;
2934 struct hammer_inode *ip;
2936 struct hammer_cursor cursor;
2937 hammer_base_elm_t base;
2941 int64_t base_offset;
2942 int64_t base_disk_offset;
2943 int64_t last_offset;
2944 hammer_off_t last_disk_offset;
2945 hammer_off_t disk_offset;
2950 ++hammer_stats_file_iopsr;
2951 ip = ap->a_vp->v_data;
2955 * We can only BMAP regular files. We can't BMAP database files,
2958 if (ip->ino_data.obj_type != HAMMER_OBJTYPE_REGFILE)
2962 * bmap is typically called with runp/runb both NULL when used
2963 * for writing. We do not support BMAP for writing atm.
2965 if (ap->a_cmd != BUF_CMD_READ)
2969 * Scan the B-Tree to acquire blockmap addresses, then translate
2972 lwkt_gettoken(&hmp->fs_token);
2973 hammer_simple_transaction(&trans, hmp);
2975 kprintf("bmap_beg %016llx ip->cache %p\n",
2976 (long long)ap->a_loffset, ip->cache[1]);
2978 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
2981 * Key range (begin and end inclusive) to scan. Note that the key's
2982 * stored in the actual records represent BASE+LEN, not BASE. The
2983 * first record containing bio_offset will have a key > bio_offset.
2985 cursor.key_beg.localization = ip->obj_localization +
2986 HAMMER_LOCALIZE_MISC;
2987 cursor.key_beg.obj_id = ip->obj_id;
2988 cursor.key_beg.create_tid = 0;
2989 cursor.key_beg.delete_tid = 0;
2990 cursor.key_beg.obj_type = 0;
2992 cursor.key_beg.key = ap->a_loffset - MAXPHYS + 1;
2994 cursor.key_beg.key = ap->a_loffset + 1;
2995 if (cursor.key_beg.key < 0)
2996 cursor.key_beg.key = 0;
2997 cursor.asof = ip->obj_asof;
2998 cursor.flags |= HAMMER_CURSOR_ASOF;
3000 cursor.key_end = cursor.key_beg;
3001 KKASSERT(ip->ino_data.obj_type == HAMMER_OBJTYPE_REGFILE);
3003 ran_end = ap->a_loffset + MAXPHYS;
3004 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
3005 cursor.key_end.rec_type = HAMMER_RECTYPE_DATA;
3006 tmp64 = ran_end + MAXPHYS + 1; /* work-around GCC-4 bug */
3007 if (tmp64 < ran_end)
3008 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
3010 cursor.key_end.key = ran_end + MAXPHYS + 1;
3012 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
3014 error = hammer_ip_first(&cursor);
3015 base_offset = last_offset = 0;
3016 base_disk_offset = last_disk_offset = 0;
3018 while (error == 0) {
3020 * Get the base file offset of the record. The key for
3021 * data records is (base + bytes) rather then (base).
3023 * NOTE: rec_offset + rec_len may exceed the end-of-file.
3024 * The extra bytes should be zero on-disk and the BMAP op
3025 * should still be ok.
3027 base = &cursor.leaf->base;
3028 rec_offset = base->key - cursor.leaf->data_len;
3029 rec_len = cursor.leaf->data_len;
3032 * Incorporate any cached truncation.
3034 * NOTE: Modifications to rec_len based on synthesized
3035 * truncation points remove the guarantee that any extended
3036 * data on disk is zero (since the truncations may not have
3037 * taken place on-media yet).
3039 if (ip->flags & HAMMER_INODE_TRUNCATED) {
3040 if (hammer_cursor_ondisk(&cursor) ||
3041 cursor.iprec->flush_state == HAMMER_FST_FLUSH) {
3042 if (ip->trunc_off <= rec_offset)
3044 else if (ip->trunc_off < rec_offset + rec_len)
3045 rec_len = (int)(ip->trunc_off - rec_offset);
3048 if (ip->sync_flags & HAMMER_INODE_TRUNCATED) {
3049 if (hammer_cursor_ondisk(&cursor)) {
3050 if (ip->sync_trunc_off <= rec_offset)
3052 else if (ip->sync_trunc_off < rec_offset + rec_len)
3053 rec_len = (int)(ip->sync_trunc_off - rec_offset);
3058 * Accumulate information. If we have hit a discontiguous
3059 * block reset base_offset unless we are already beyond the
3060 * requested offset. If we are, that's it, we stop.
3064 if (hammer_cursor_ondisk(&cursor)) {
3065 disk_offset = cursor.leaf->data_offset;
3066 if (rec_offset != last_offset ||
3067 disk_offset != last_disk_offset) {
3068 if (rec_offset > ap->a_loffset)
3070 base_offset = rec_offset;
3071 base_disk_offset = disk_offset;
3073 last_offset = rec_offset + rec_len;
3074 last_disk_offset = disk_offset + rec_len;
3076 if (hammer_live_dedup)
3077 hammer_dedup_cache_add(ip, cursor.leaf);
3080 error = hammer_ip_next(&cursor);
3084 kprintf("BMAP %016llx: %016llx - %016llx\n",
3085 (long long)ap->a_loffset,
3086 (long long)base_offset,
3087 (long long)last_offset);
3088 kprintf("BMAP %16s: %016llx - %016llx\n", "",
3089 (long long)base_disk_offset,
3090 (long long)last_disk_offset);
3094 hammer_cache_node(&ip->cache[1], cursor.node);
3096 kprintf("bmap_end2 %016llx ip->cache %p\n",
3097 (long long)ap->a_loffset, ip->cache[1]);
3100 hammer_done_cursor(&cursor);
3101 hammer_done_transaction(&trans);
3102 lwkt_reltoken(&hmp->fs_token);
3105 * If we couldn't find any records or the records we did find were
3106 * all behind the requested offset, return failure. A forward
3107 * truncation can leave a hole w/ no on-disk records.
3109 if (last_offset == 0 || last_offset < ap->a_loffset)
3110 return (EOPNOTSUPP);
3113 * Figure out the block size at the requested offset and adjust
3114 * our limits so the cluster_read() does not create inappropriately
3115 * sized buffer cache buffers.
3117 blksize = hammer_blocksize(ap->a_loffset);
3118 if (hammer_blocksize(base_offset) != blksize) {
3119 base_offset = hammer_blockdemarc(base_offset, ap->a_loffset);
3121 if (last_offset != ap->a_loffset &&
3122 hammer_blocksize(last_offset - 1) != blksize) {
3123 last_offset = hammer_blockdemarc(ap->a_loffset,
3128 * Returning EOPNOTSUPP simply prevents the direct-IO optimization
3131 disk_offset = base_disk_offset + (ap->a_loffset - base_offset);
3133 if ((disk_offset & HAMMER_OFF_ZONE_MASK) != HAMMER_ZONE_LARGE_DATA) {
3135 * Only large-data zones can be direct-IOd
3138 } else if ((disk_offset & HAMMER_BUFMASK) ||
3139 (last_offset - ap->a_loffset) < blksize) {
3141 * doffsetp is not aligned or the forward run size does
3142 * not cover a whole buffer, disallow the direct I/O.
3149 *ap->a_doffsetp = disk_offset;
3151 *ap->a_runb = ap->a_loffset - base_offset;
3152 KKASSERT(*ap->a_runb >= 0);
3155 *ap->a_runp = last_offset - ap->a_loffset;
3156 KKASSERT(*ap->a_runp >= 0);
3164 * Write to a regular file. Because this is a strategy call the OS is
3165 * trying to actually get data onto the media.
3169 hammer_vop_strategy_write(struct vop_strategy_args *ap)
3171 hammer_record_t record;
3182 ip = ap->a_vp->v_data;
3185 blksize = hammer_blocksize(bio->bio_offset);
3186 KKASSERT(bp->b_bufsize == blksize);
3188 if (ip->flags & HAMMER_INODE_RO) {
3189 bp->b_error = EROFS;
3190 bp->b_flags |= B_ERROR;
3195 lwkt_gettoken(&hmp->fs_token);
3198 * Interlock with inode destruction (no in-kernel or directory
3199 * topology visibility). If we queue new IO while trying to
3200 * destroy the inode we can deadlock the vtrunc call in
3201 * hammer_inode_unloadable_check().
3203 * Besides, there's no point flushing a bp associated with an
3204 * inode that is being destroyed on-media and has no kernel
3207 if ((ip->flags | ip->sync_flags) &
3208 (HAMMER_INODE_DELETING|HAMMER_INODE_DELETED)) {
3211 lwkt_reltoken(&hmp->fs_token);
3216 * Reserve space and issue a direct-write from the front-end.
3217 * NOTE: The direct_io code will hammer_bread/bcopy smaller
3220 * An in-memory record will be installed to reference the storage
3221 * until the flusher can get to it.
3223 * Since we own the high level bio the front-end will not try to
3224 * do a direct-read until the write completes.
3226 * NOTE: The only time we do not reserve a full-sized buffers
3227 * worth of data is if the file is small. We do not try to
3228 * allocate a fragment (from the small-data zone) at the end of
3229 * an otherwise large file as this can lead to wildly separated
3232 KKASSERT((bio->bio_offset & HAMMER_BUFMASK) == 0);
3233 KKASSERT(bio->bio_offset < ip->ino_data.size);
3234 if (bio->bio_offset || ip->ino_data.size > HAMMER_BUFSIZE / 2)
3235 bytes = bp->b_bufsize;
3237 bytes = ((int)ip->ino_data.size + 15) & ~15;
3239 record = hammer_ip_add_bulk(ip, bio->bio_offset, bp->b_data,
3243 * B_VFSFLAG1 indicates that a REDO_WRITE entry was generated
3244 * in hammer_vop_write(). We must flag the record so the proper
3245 * REDO_TERM_WRITE entry is generated during the flush.
3248 if (bp->b_flags & B_VFSFLAG1) {
3249 record->flags |= HAMMER_RECF_REDO;
3250 bp->b_flags &= ~B_VFSFLAG1;
3252 if (record->flags & HAMMER_RECF_DEDUPED) {
3254 hammer_ip_replace_bulk(hmp, record);
3257 hammer_io_direct_write(hmp, bio, record);
3259 if (ip->rsv_recs > 1 && hmp->rsv_recs > hammer_limit_recs)
3260 hammer_flush_inode(ip, 0);
3262 bp->b_bio2.bio_offset = NOOFFSET;
3263 bp->b_error = error;
3264 bp->b_flags |= B_ERROR;
3267 lwkt_reltoken(&hmp->fs_token);
3272 * dounlink - disconnect a directory entry
3274 * XXX whiteout support not really in yet
3277 hammer_dounlink(hammer_transaction_t trans, struct nchandle *nch,
3278 struct vnode *dvp, struct ucred *cred,
3279 int flags, int isdir)
3281 struct namecache *ncp;
3285 struct hammer_cursor cursor;
3287 u_int32_t max_iterations;
3291 * Calculate the namekey and setup the key range for the scan. This
3292 * works kinda like a chained hash table where the lower 32 bits
3293 * of the namekey synthesize the chain.
3295 * The key range is inclusive of both key_beg and key_end.
3301 if (dip->flags & HAMMER_INODE_RO)
3304 namekey = hammer_directory_namekey(dip, ncp->nc_name, ncp->nc_nlen,
3307 hammer_init_cursor(trans, &cursor, &dip->cache[1], dip);
3308 cursor.key_beg.localization = dip->obj_localization +
3309 hammer_dir_localization(dip);
3310 cursor.key_beg.obj_id = dip->obj_id;
3311 cursor.key_beg.key = namekey;
3312 cursor.key_beg.create_tid = 0;
3313 cursor.key_beg.delete_tid = 0;
3314 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
3315 cursor.key_beg.obj_type = 0;
3317 cursor.key_end = cursor.key_beg;
3318 cursor.key_end.key += max_iterations;
3319 cursor.asof = dip->obj_asof;
3320 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
3323 * Scan all matching records (the chain), locate the one matching
3324 * the requested path component. info->last_error contains the
3325 * error code on search termination and could be 0, ENOENT, or
3328 * The hammer_ip_*() functions merge in-memory records with on-disk
3329 * records for the purposes of the search.
3331 error = hammer_ip_first(&cursor);
3333 while (error == 0) {
3334 error = hammer_ip_resolve_data(&cursor);
3337 nlen = cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF;
3339 if (ncp->nc_nlen == nlen &&
3340 bcmp(ncp->nc_name, cursor.data->entry.name, nlen) == 0) {
3343 error = hammer_ip_next(&cursor);
3347 * If all is ok we have to get the inode so we can adjust nlinks.
3348 * To avoid a deadlock with the flusher we must release the inode
3349 * lock on the directory when acquiring the inode for the entry.
3351 * If the target is a directory, it must be empty.
3354 hammer_unlock(&cursor.ip->lock);
3355 ip = hammer_get_inode(trans, dip, cursor.data->entry.obj_id,
3357 cursor.data->entry.localization,
3359 hammer_lock_sh(&cursor.ip->lock);
3360 if (error == ENOENT) {
3361 kprintf("HAMMER: WARNING: Removing "
3362 "dirent w/missing inode \"%s\"\n"
3363 "\tobj_id = %016llx\n",
3365 (long long)cursor.data->entry.obj_id);
3370 * If isdir >= 0 we validate that the entry is or is not a
3371 * directory. If isdir < 0 we don't care.
3373 if (error == 0 && isdir >= 0 && ip) {
3375 ip->ino_data.obj_type != HAMMER_OBJTYPE_DIRECTORY) {
3377 } else if (isdir == 0 &&
3378 ip->ino_data.obj_type == HAMMER_OBJTYPE_DIRECTORY) {
3384 * If we are trying to remove a directory the directory must
3387 * The check directory code can loop and deadlock/retry. Our
3388 * own cursor's node locks must be released to avoid a 3-way
3389 * deadlock with the flusher if the check directory code
3392 * If any changes whatsoever have been made to the cursor
3393 * set EDEADLK and retry.
3395 * WARNING: See warnings in hammer_unlock_cursor()
3398 if (error == 0 && ip && ip->ino_data.obj_type ==
3399 HAMMER_OBJTYPE_DIRECTORY) {
3400 hammer_unlock_cursor(&cursor);
3401 error = hammer_ip_check_directory_empty(trans, ip);
3402 hammer_lock_cursor(&cursor);
3403 if (cursor.flags & HAMMER_CURSOR_RETEST) {
3404 kprintf("HAMMER: Warning: avoided deadlock "
3412 * Delete the directory entry.
3414 * WARNING: hammer_ip_del_directory() may have to terminate
3415 * the cursor to avoid a deadlock. It is ok to call
3416 * hammer_done_cursor() twice.
3419 error = hammer_ip_del_directory(trans, &cursor,
3422 hammer_done_cursor(&cursor);
3424 cache_setunresolved(nch);
3425 cache_setvp(nch, NULL);
3428 * NOTE: ip->vp, if non-NULL, cannot be directly
3429 * referenced without formally acquiring the
3430 * vp since the vp might have zero refs on it,
3431 * or in the middle of a reclaim, etc.
3433 * NOTE: The cache_setunresolved() can rip the vp
3434 * out from under us since the vp may not have
3435 * any refs, in which case ip->vp will be NULL
3438 while (ip && ip->vp) {
3441 error = hammer_get_vnode(ip, &vp);
3442 if (error == 0 && vp) {
3444 hammer_knote(ip->vp, NOTE_DELETE);
3445 cache_inval_vp(ip->vp, CINV_DESTROY);
3449 kprintf("Debug: HAMMER ip/vp race1 avoided\n");
3453 hammer_rel_inode(ip, 0);
3455 hammer_done_cursor(&cursor);
3457 if (error == EDEADLK)
3463 /************************************************************************
3464 * FIFO AND SPECFS OPS *
3465 ************************************************************************
3469 hammer_vop_fifoclose (struct vop_close_args *ap)
3471 /* XXX update itimes */
3472 return (VOCALL(&fifo_vnode_vops, &ap->a_head));
3476 hammer_vop_fiforead (struct vop_read_args *ap)
3480 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
3481 /* XXX update access time */
3486 hammer_vop_fifowrite (struct vop_write_args *ap)
3490 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
3491 /* XXX update access time */
3497 hammer_vop_fifokqfilter(struct vop_kqfilter_args *ap)
3501 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
3503 error = hammer_vop_kqfilter(ap);
3507 /************************************************************************
3509 ************************************************************************
3512 static void filt_hammerdetach(struct knote *kn);
3513 static int filt_hammerread(struct knote *kn, long hint);
3514 static int filt_hammerwrite(struct knote *kn, long hint);
3515 static int filt_hammervnode(struct knote *kn, long hint);
3517 static struct filterops hammerread_filtops =
3518 { FILTEROP_ISFD, NULL, filt_hammerdetach, filt_hammerread };
3519 static struct filterops hammerwrite_filtops =
3520 { FILTEROP_ISFD, NULL, filt_hammerdetach, filt_hammerwrite };
3521 static struct filterops hammervnode_filtops =
3522 { FILTEROP_ISFD, NULL, filt_hammerdetach, filt_hammervnode };
3526 hammer_vop_kqfilter(struct vop_kqfilter_args *ap)
3528 struct vnode *vp = ap->a_vp;
3529 struct knote *kn = ap->a_kn;
3531 switch (kn->kn_filter) {
3533 kn->kn_fop = &hammerread_filtops;
3536 kn->kn_fop = &hammerwrite_filtops;
3539 kn->kn_fop = &hammervnode_filtops;
3542 return (EOPNOTSUPP);
3545 kn->kn_hook = (caddr_t)vp;
3547 knote_insert(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
3553 filt_hammerdetach(struct knote *kn)
3555 struct vnode *vp = (void *)kn->kn_hook;
3557 knote_remove(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
3561 filt_hammerread(struct knote *kn, long hint)
3563 struct vnode *vp = (void *)kn->kn_hook;
3564 hammer_inode_t ip = VTOI(vp);
3565 hammer_mount_t hmp = ip->hmp;
3568 if (hint == NOTE_REVOKE) {
3569 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
3572 lwkt_gettoken(&hmp->fs_token); /* XXX use per-ip-token */
3573 off = ip->ino_data.size - kn->kn_fp->f_offset;
3574 kn->kn_data = (off < INTPTR_MAX) ? off : INTPTR_MAX;
3575 lwkt_reltoken(&hmp->fs_token);
3576 if (kn->kn_sfflags & NOTE_OLDAPI)
3578 return (kn->kn_data != 0);
3582 filt_hammerwrite(struct knote *kn, long hint)
3584 if (hint == NOTE_REVOKE)
3585 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
3591 filt_hammervnode(struct knote *kn, long hint)
3593 if (kn->kn_sfflags & hint)
3594 kn->kn_fflags |= hint;
3595 if (hint == NOTE_REVOKE) {
3596 kn->kn_flags |= EV_EOF;
3599 return (kn->kn_fflags != 0);