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>
56 /*static int hammer_vop_vnoperate(struct vop_generic_args *);*/
57 static int hammer_vop_fsync(struct vop_fsync_args *);
58 static int hammer_vop_read(struct vop_read_args *);
59 static int hammer_vop_write(struct vop_write_args *);
60 static int hammer_vop_access(struct vop_access_args *);
61 static int hammer_vop_advlock(struct vop_advlock_args *);
62 static int hammer_vop_close(struct vop_close_args *);
63 static int hammer_vop_ncreate(struct vop_ncreate_args *);
64 static int hammer_vop_getattr(struct vop_getattr_args *);
65 static int hammer_vop_nresolve(struct vop_nresolve_args *);
66 static int hammer_vop_nlookupdotdot(struct vop_nlookupdotdot_args *);
67 static int hammer_vop_nlink(struct vop_nlink_args *);
68 static int hammer_vop_nmkdir(struct vop_nmkdir_args *);
69 static int hammer_vop_nmknod(struct vop_nmknod_args *);
70 static int hammer_vop_open(struct vop_open_args *);
71 static int hammer_vop_print(struct vop_print_args *);
72 static int hammer_vop_readdir(struct vop_readdir_args *);
73 static int hammer_vop_readlink(struct vop_readlink_args *);
74 static int hammer_vop_nremove(struct vop_nremove_args *);
75 static int hammer_vop_nrename(struct vop_nrename_args *);
76 static int hammer_vop_nrmdir(struct vop_nrmdir_args *);
77 static int hammer_vop_markatime(struct vop_markatime_args *);
78 static int hammer_vop_setattr(struct vop_setattr_args *);
79 static int hammer_vop_strategy(struct vop_strategy_args *);
80 static int hammer_vop_bmap(struct vop_bmap_args *ap);
81 static int hammer_vop_nsymlink(struct vop_nsymlink_args *);
82 static int hammer_vop_nwhiteout(struct vop_nwhiteout_args *);
83 static int hammer_vop_ioctl(struct vop_ioctl_args *);
84 static int hammer_vop_mountctl(struct vop_mountctl_args *);
85 static int hammer_vop_kqfilter (struct vop_kqfilter_args *);
87 static int hammer_vop_fifoclose (struct vop_close_args *);
88 static int hammer_vop_fiforead (struct vop_read_args *);
89 static int hammer_vop_fifowrite (struct vop_write_args *);
90 static int hammer_vop_fifokqfilter (struct vop_kqfilter_args *);
92 struct vop_ops hammer_vnode_vops = {
93 .vop_default = vop_defaultop,
94 .vop_fsync = hammer_vop_fsync,
95 .vop_getpages = vop_stdgetpages,
96 .vop_putpages = vop_stdputpages,
97 .vop_read = hammer_vop_read,
98 .vop_write = hammer_vop_write,
99 .vop_access = hammer_vop_access,
100 .vop_advlock = hammer_vop_advlock,
101 .vop_close = hammer_vop_close,
102 .vop_ncreate = hammer_vop_ncreate,
103 .vop_getattr = hammer_vop_getattr,
104 .vop_inactive = hammer_vop_inactive,
105 .vop_reclaim = hammer_vop_reclaim,
106 .vop_nresolve = hammer_vop_nresolve,
107 .vop_nlookupdotdot = hammer_vop_nlookupdotdot,
108 .vop_nlink = hammer_vop_nlink,
109 .vop_nmkdir = hammer_vop_nmkdir,
110 .vop_nmknod = hammer_vop_nmknod,
111 .vop_open = hammer_vop_open,
112 .vop_pathconf = vop_stdpathconf,
113 .vop_print = hammer_vop_print,
114 .vop_readdir = hammer_vop_readdir,
115 .vop_readlink = hammer_vop_readlink,
116 .vop_nremove = hammer_vop_nremove,
117 .vop_nrename = hammer_vop_nrename,
118 .vop_nrmdir = hammer_vop_nrmdir,
119 .vop_markatime = hammer_vop_markatime,
120 .vop_setattr = hammer_vop_setattr,
121 .vop_bmap = hammer_vop_bmap,
122 .vop_strategy = hammer_vop_strategy,
123 .vop_nsymlink = hammer_vop_nsymlink,
124 .vop_nwhiteout = hammer_vop_nwhiteout,
125 .vop_ioctl = hammer_vop_ioctl,
126 .vop_mountctl = hammer_vop_mountctl,
127 .vop_kqfilter = hammer_vop_kqfilter
130 struct vop_ops hammer_spec_vops = {
131 .vop_default = vop_defaultop,
132 .vop_fsync = hammer_vop_fsync,
133 .vop_read = vop_stdnoread,
134 .vop_write = vop_stdnowrite,
135 .vop_access = hammer_vop_access,
136 .vop_close = hammer_vop_close,
137 .vop_markatime = hammer_vop_markatime,
138 .vop_getattr = hammer_vop_getattr,
139 .vop_inactive = hammer_vop_inactive,
140 .vop_reclaim = hammer_vop_reclaim,
141 .vop_setattr = hammer_vop_setattr
144 struct vop_ops hammer_fifo_vops = {
145 .vop_default = fifo_vnoperate,
146 .vop_fsync = hammer_vop_fsync,
147 .vop_read = hammer_vop_fiforead,
148 .vop_write = hammer_vop_fifowrite,
149 .vop_access = hammer_vop_access,
150 .vop_close = hammer_vop_fifoclose,
151 .vop_markatime = hammer_vop_markatime,
152 .vop_getattr = hammer_vop_getattr,
153 .vop_inactive = hammer_vop_inactive,
154 .vop_reclaim = hammer_vop_reclaim,
155 .vop_setattr = hammer_vop_setattr,
156 .vop_kqfilter = hammer_vop_fifokqfilter
161 hammer_knote(struct vnode *vp, int flags)
164 KNOTE(&vp->v_pollinfo.vpi_kqinfo.ki_note, flags);
167 #ifdef DEBUG_TRUNCATE
168 struct hammer_inode *HammerTruncIp;
171 static int hammer_dounlink(hammer_transaction_t trans, struct nchandle *nch,
172 struct vnode *dvp, struct ucred *cred,
173 int flags, int isdir);
174 static int hammer_vop_strategy_read(struct vop_strategy_args *ap);
175 static int hammer_vop_strategy_write(struct vop_strategy_args *ap);
180 hammer_vop_vnoperate(struct vop_generic_args *)
182 return (VOCALL(&hammer_vnode_vops, ap));
187 * hammer_vop_fsync { vp, waitfor }
189 * fsync() an inode to disk and wait for it to be completely committed
190 * such that the information would not be undone if a crash occured after
193 * NOTE: HAMMER's fsync()'s are going to remain expensive until we implement
194 * a REDO log. A sysctl is provided to relax HAMMER's fsync()
197 * Ultimately the combination of a REDO log and use of fast storage
198 * to front-end cluster caches will make fsync fast, but it aint
199 * here yet. And, in anycase, we need real transactional
200 * all-or-nothing features which are not restricted to a single file.
204 hammer_vop_fsync(struct vop_fsync_args *ap)
206 hammer_inode_t ip = VTOI(ap->a_vp);
207 hammer_mount_t hmp = ip->hmp;
208 int waitfor = ap->a_waitfor;
211 lwkt_gettoken(&hmp->fs_token);
214 * Fsync rule relaxation (default is either full synchronous flush
215 * or REDO semantics with synchronous flush).
217 if (ap->a_flags & VOP_FSYNC_SYSCALL) {
218 switch(hammer_fsync_mode) {
221 /* no REDO, full synchronous flush */
225 /* no REDO, full asynchronous flush */
226 if (waitfor == MNT_WAIT)
227 waitfor = MNT_NOWAIT;
230 /* REDO semantics, synchronous flush */
231 if (hmp->version < HAMMER_VOL_VERSION_FOUR)
233 mode = HAMMER_FLUSH_UNDOS_AUTO;
236 /* REDO semantics, relaxed asynchronous flush */
237 if (hmp->version < HAMMER_VOL_VERSION_FOUR)
239 mode = HAMMER_FLUSH_UNDOS_RELAXED;
240 if (waitfor == MNT_WAIT)
241 waitfor = MNT_NOWAIT;
244 /* ignore the fsync() system call */
245 lwkt_reltoken(&hmp->fs_token);
248 /* we have to do something */
249 mode = HAMMER_FLUSH_UNDOS_RELAXED;
250 if (waitfor == MNT_WAIT)
251 waitfor = MNT_NOWAIT;
256 * Fast fsync only needs to flush the UNDO/REDO fifo if
257 * HAMMER_INODE_REDO is non-zero and the only modifications
258 * made to the file are write or write-extends.
260 if ((ip->flags & HAMMER_INODE_REDO) &&
261 (ip->flags & HAMMER_INODE_MODMASK_NOREDO) == 0
263 ++hammer_count_fsyncs;
264 hammer_flusher_flush_undos(hmp, mode);
266 lwkt_reltoken(&hmp->fs_token);
271 * REDO is enabled by fsync(), the idea being we really only
272 * want to lay down REDO records when programs are using
273 * fsync() heavily. The first fsync() on the file starts
274 * the gravy train going and later fsync()s keep it hot by
275 * resetting the redo_count.
277 * We weren't running REDOs before now so we have to fall
278 * through and do a full fsync of what we have.
280 if (hmp->version >= HAMMER_VOL_VERSION_FOUR &&
281 (hmp->flags & HAMMER_MOUNT_REDO_RECOVERY_RUN) == 0) {
282 ip->flags |= HAMMER_INODE_REDO;
289 * Do a full flush sequence.
291 ++hammer_count_fsyncs;
292 vfsync(ap->a_vp, waitfor, 1, NULL, NULL);
293 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
294 if (waitfor == MNT_WAIT) {
296 hammer_wait_inode(ip);
297 vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY);
299 lwkt_reltoken(&hmp->fs_token);
304 * hammer_vop_read { vp, uio, ioflag, cred }
306 * MPSAFE (for the cache safe does not require fs_token)
310 hammer_vop_read(struct vop_read_args *ap)
312 struct hammer_transaction trans;
326 if (ap->a_vp->v_type != VREG)
334 * Allow the UIO's size to override the sequential heuristic.
336 blksize = hammer_blocksize(uio->uio_offset);
337 seqcount = (uio->uio_resid + (BKVASIZE - 1)) / BKVASIZE;
338 ioseqcount = (ap->a_ioflag >> 16);
339 if (seqcount < ioseqcount)
340 seqcount = ioseqcount;
343 * If reading or writing a huge amount of data we have to break
344 * atomicy and allow the operation to be interrupted by a signal
345 * or it can DOS the machine.
347 bigread = (uio->uio_resid > 100 * 1024 * 1024);
351 * Access the data typically in HAMMER_BUFSIZE blocks via the
352 * buffer cache, but HAMMER may use a variable block size based
355 * XXX Temporary hack, delay the start transaction while we remain
356 * MPSAFE. NOTE: ino_data.size cannot change while vnode is
359 while (uio->uio_resid > 0 && uio->uio_offset < ip->ino_data.size) {
363 blksize = hammer_blocksize(uio->uio_offset);
364 offset = (int)uio->uio_offset & (blksize - 1);
365 base_offset = uio->uio_offset - offset;
367 if (bigread && (error = hammer_signal_check(ip->hmp)) != 0)
373 bp = getcacheblk(ap->a_vp, base_offset, blksize);
382 if (got_fstoken == 0) {
383 lwkt_gettoken(&hmp->fs_token);
385 hammer_start_transaction(&trans, ip->hmp);
388 if (hammer_cluster_enable) {
390 * Use file_limit to prevent cluster_read() from
391 * creating buffers of the wrong block size past
394 file_limit = ip->ino_data.size;
395 if (base_offset < HAMMER_XDEMARC &&
396 file_limit > HAMMER_XDEMARC) {
397 file_limit = HAMMER_XDEMARC;
399 error = cluster_read(ap->a_vp,
400 file_limit, base_offset,
401 blksize, uio->uio_resid,
402 seqcount * BKVASIZE, &bp);
404 error = bread(ap->a_vp, base_offset, blksize, &bp);
411 if ((hammer_debug_io & 0x0001) && (bp->b_flags & B_IODEBUG)) {
412 kprintf("doff %016jx read file %016jx@%016jx\n",
413 (intmax_t)bp->b_bio2.bio_offset,
414 (intmax_t)ip->obj_id,
415 (intmax_t)bp->b_loffset);
417 bp->b_flags &= ~B_IODEBUG;
419 /* bp->b_flags |= B_CLUSTEROK; temporarily disabled */
420 n = blksize - offset;
421 if (n > uio->uio_resid)
423 if (n > ip->ino_data.size - uio->uio_offset)
424 n = (int)(ip->ino_data.size - uio->uio_offset);
426 lwkt_reltoken(&hmp->fs_token);
429 * Set B_AGE, data has a lower priority than meta-data.
431 * Use a hold/unlock/drop sequence to run the uiomove
432 * with the buffer unlocked, avoiding deadlocks against
433 * read()s on mmap()'d spaces.
435 bp->b_flags |= B_AGE;
438 error = uiomove((char *)bp->b_data + offset, n, uio);
442 lwkt_gettoken(&hmp->fs_token);
446 hammer_stats_file_read += n;
450 * XXX only update the atime if we had to get the MP lock.
451 * XXX hack hack hack, fixme.
454 if ((ip->flags & HAMMER_INODE_RO) == 0 &&
455 (ip->hmp->mp->mnt_flag & MNT_NOATIME) == 0) {
456 ip->ino_data.atime = trans.time;
457 hammer_modify_inode(&trans, ip, HAMMER_INODE_ATIME);
459 hammer_done_transaction(&trans);
460 lwkt_reltoken(&hmp->fs_token);
466 * hammer_vop_write { vp, uio, ioflag, cred }
470 hammer_vop_write(struct vop_write_args *ap)
472 struct hammer_transaction trans;
473 struct hammer_inode *ip;
486 if (ap->a_vp->v_type != VREG)
492 seqcount = ap->a_ioflag >> 16;
494 if (ip->flags & HAMMER_INODE_RO)
498 * Create a transaction to cover the operations we perform.
500 lwkt_gettoken(&hmp->fs_token);
501 hammer_start_transaction(&trans, hmp);
507 if (ap->a_ioflag & IO_APPEND)
508 uio->uio_offset = ip->ino_data.size;
511 * Check for illegal write offsets. Valid range is 0...2^63-1.
513 * NOTE: the base_off assignment is required to work around what
514 * I consider to be a GCC-4 optimization bug.
516 if (uio->uio_offset < 0) {
517 hammer_done_transaction(&trans);
518 lwkt_reltoken(&hmp->fs_token);
521 base_offset = uio->uio_offset + uio->uio_resid; /* work around gcc-4 */
522 if (uio->uio_resid > 0 && base_offset <= uio->uio_offset) {
523 hammer_done_transaction(&trans);
524 lwkt_reltoken(&hmp->fs_token);
529 * If reading or writing a huge amount of data we have to break
530 * atomicy and allow the operation to be interrupted by a signal
531 * or it can DOS the machine.
533 * Preset redo_count so we stop generating REDOs earlier if the
536 bigwrite = (uio->uio_resid > 100 * 1024 * 1024);
537 if ((ip->flags & HAMMER_INODE_REDO) &&
538 ip->redo_count < hammer_limit_redo) {
539 ip->redo_count += uio->uio_resid;
543 * Access the data typically in HAMMER_BUFSIZE blocks via the
544 * buffer cache, but HAMMER may use a variable block size based
547 while (uio->uio_resid > 0) {
555 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_WRITE)) != 0)
557 if (bigwrite && (error = hammer_signal_check(hmp)) != 0)
560 blksize = hammer_blocksize(uio->uio_offset);
563 * Do not allow HAMMER to blow out the buffer cache. Very
564 * large UIOs can lockout other processes due to bwillwrite()
567 * The hammer inode is not locked during these operations.
568 * The vnode is locked which can interfere with the pageout
569 * daemon for non-UIO_NOCOPY writes but should not interfere
570 * with the buffer cache. Even so, we cannot afford to
571 * allow the pageout daemon to build up too many dirty buffer
574 * Only call this if we aren't being recursively called from
575 * a virtual disk device (vn), else we may deadlock.
577 if ((ap->a_ioflag & IO_RECURSE) == 0)
581 * Control the number of pending records associated with
582 * this inode. If too many have accumulated start a
583 * flush. Try to maintain a pipeline with the flusher.
585 * NOTE: It is possible for other sources to grow the
586 * records but not necessarily issue another flush,
587 * so use a timeout and ensure that a re-flush occurs.
589 if (ip->rsv_recs >= hammer_limit_inode_recs) {
590 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
591 while (ip->rsv_recs >= hammer_limit_inode_recs * 2) {
592 ip->flags |= HAMMER_INODE_RECSW;
593 tsleep(&ip->rsv_recs, 0, "hmrwww", hz);
594 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
600 * Do not allow HAMMER to blow out system memory by
601 * accumulating too many records. Records are so well
602 * decoupled from the buffer cache that it is possible
603 * for userland to push data out to the media via
604 * direct-write, but build up the records queued to the
605 * backend faster then the backend can flush them out.
606 * HAMMER has hit its write limit but the frontend has
607 * no pushback to slow it down.
609 if (hmp->rsv_recs > hammer_limit_recs / 2) {
611 * Get the inode on the flush list
613 if (ip->rsv_recs >= 64)
614 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
615 else if (ip->rsv_recs >= 16)
616 hammer_flush_inode(ip, 0);
619 * Keep the flusher going if the system keeps
622 delta = hmp->count_newrecords -
623 hmp->last_newrecords;
624 if (delta < 0 || delta > hammer_limit_recs / 2) {
625 hmp->last_newrecords = hmp->count_newrecords;
626 hammer_sync_hmp(hmp, MNT_NOWAIT);
630 * If we have gotten behind start slowing
633 delta = (hmp->rsv_recs - hammer_limit_recs) *
634 hz / hammer_limit_recs;
636 tsleep(&trans, 0, "hmrslo", delta);
641 * Calculate the blocksize at the current offset and figure
642 * out how much we can actually write.
644 blkmask = blksize - 1;
645 offset = (int)uio->uio_offset & blkmask;
646 base_offset = uio->uio_offset & ~(int64_t)blkmask;
647 n = blksize - offset;
648 if (n > uio->uio_resid) {
654 nsize = uio->uio_offset + n;
655 if (nsize > ip->ino_data.size) {
656 if (uio->uio_offset > ip->ino_data.size)
660 nvextendbuf(ap->a_vp,
663 hammer_blocksize(ip->ino_data.size),
664 hammer_blocksize(nsize),
665 hammer_blockoff(ip->ino_data.size),
666 hammer_blockoff(nsize),
669 kflags |= NOTE_EXTEND;
672 if (uio->uio_segflg == UIO_NOCOPY) {
674 * Issuing a write with the same data backing the
675 * buffer. Instantiate the buffer to collect the
676 * backing vm pages, then read-in any missing bits.
678 * This case is used by vop_stdputpages().
680 bp = getblk(ap->a_vp, base_offset,
681 blksize, GETBLK_BHEAVY, 0);
682 if ((bp->b_flags & B_CACHE) == 0) {
684 error = bread(ap->a_vp, base_offset,
687 } else if (offset == 0 && uio->uio_resid >= blksize) {
689 * Even though we are entirely overwriting the buffer
690 * we may still have to zero it out to avoid a
691 * mmap/write visibility issue.
693 bp = getblk(ap->a_vp, base_offset, blksize, GETBLK_BHEAVY, 0);
694 if ((bp->b_flags & B_CACHE) == 0)
696 } else if (base_offset >= ip->ino_data.size) {
698 * If the base offset of the buffer is beyond the
699 * file EOF, we don't have to issue a read.
701 bp = getblk(ap->a_vp, base_offset,
702 blksize, GETBLK_BHEAVY, 0);
706 * Partial overwrite, read in any missing bits then
707 * replace the portion being written.
709 error = bread(ap->a_vp, base_offset, blksize, &bp);
714 lwkt_reltoken(&hmp->fs_token);
715 error = uiomove(bp->b_data + offset, n, uio);
716 lwkt_gettoken(&hmp->fs_token);
720 * Generate REDO records if enabled and redo_count will not
721 * exceeded the limit.
723 * If redo_count exceeds the limit we stop generating records
724 * and clear HAMMER_INODE_REDO. This will cause the next
725 * fsync() to do a full meta-data sync instead of just an
726 * UNDO/REDO fifo update.
728 * When clearing HAMMER_INODE_REDO any pre-existing REDOs
729 * will still be tracked. The tracks will be terminated
730 * when the related meta-data (including possible data
731 * modifications which are not tracked via REDO) is
734 if ((ip->flags & HAMMER_INODE_REDO) && error == 0) {
735 if (ip->redo_count < hammer_limit_redo) {
736 bp->b_flags |= B_VFSFLAG1;
737 error = hammer_generate_redo(&trans, ip,
738 base_offset + offset,
743 ip->flags &= ~HAMMER_INODE_REDO;
748 * If we screwed up we have to undo any VM size changes we
754 nvtruncbuf(ap->a_vp, ip->ino_data.size,
755 hammer_blocksize(ip->ino_data.size),
756 hammer_blockoff(ip->ino_data.size));
760 kflags |= NOTE_WRITE;
761 hammer_stats_file_write += n;
762 /* bp->b_flags |= B_CLUSTEROK; temporarily disabled */
763 if (ip->ino_data.size < uio->uio_offset) {
764 ip->ino_data.size = uio->uio_offset;
765 flags = HAMMER_INODE_SDIRTY;
769 ip->ino_data.mtime = trans.time;
770 flags |= HAMMER_INODE_MTIME | HAMMER_INODE_BUFS;
771 hammer_modify_inode(&trans, ip, flags);
774 * Once we dirty the buffer any cached zone-X offset
775 * becomes invalid. HAMMER NOTE: no-history mode cannot
776 * allow overwriting over the same data sector unless
777 * we provide UNDOs for the old data, which we don't.
779 bp->b_bio2.bio_offset = NOOFFSET;
782 * Final buffer disposition.
784 * Because meta-data updates are deferred, HAMMER is
785 * especially sensitive to excessive bdwrite()s because
786 * the I/O stream is not broken up by disk reads. So the
787 * buffer cache simply cannot keep up.
789 * WARNING! blksize is variable. cluster_write() is
790 * expected to not blow up if it encounters
791 * buffers that do not match the passed blksize.
793 * NOTE! Hammer shouldn't need to bawrite()/cluster_write().
794 * The ip->rsv_recs check should burst-flush the data.
795 * If we queue it immediately the buf could be left
796 * locked on the device queue for a very long time.
798 * NOTE! To avoid degenerate stalls due to mismatched block
799 * sizes we only honor IO_DIRECT on the write which
800 * abuts the end of the buffer. However, we must
801 * honor IO_SYNC in case someone is silly enough to
802 * configure a HAMMER file as swap, or when HAMMER
803 * is serving NFS (for commits). Ick ick.
805 bp->b_flags |= B_AGE;
806 if (ap->a_ioflag & IO_SYNC) {
808 } else if ((ap->a_ioflag & IO_DIRECT) && endofblk) {
812 if (offset + n == blksize) {
813 if (hammer_cluster_enable == 0 ||
814 (ap->a_vp->v_mount->mnt_flag & MNT_NOCLUSTERW)) {
817 cluster_write(bp, ip->ino_data.size,
825 hammer_done_transaction(&trans);
826 hammer_knote(ap->a_vp, kflags);
827 lwkt_reltoken(&hmp->fs_token);
832 * hammer_vop_access { vp, mode, cred }
834 * MPSAFE - does not require fs_token
838 hammer_vop_access(struct vop_access_args *ap)
840 struct hammer_inode *ip = VTOI(ap->a_vp);
845 ++hammer_stats_file_iopsr;
846 uid = hammer_to_unix_xid(&ip->ino_data.uid);
847 gid = hammer_to_unix_xid(&ip->ino_data.gid);
849 error = vop_helper_access(ap, uid, gid, ip->ino_data.mode,
850 ip->ino_data.uflags);
855 * hammer_vop_advlock { vp, id, op, fl, flags }
857 * MPSAFE - does not require fs_token
861 hammer_vop_advlock(struct vop_advlock_args *ap)
863 hammer_inode_t ip = VTOI(ap->a_vp);
865 return (lf_advlock(ap, &ip->advlock, ip->ino_data.size));
869 * hammer_vop_close { vp, fflag }
871 * We can only sync-on-close for normal closes. XXX disabled for now.
875 hammer_vop_close(struct vop_close_args *ap)
878 struct vnode *vp = ap->a_vp;
879 hammer_inode_t ip = VTOI(vp);
881 if (ip->flags & (HAMMER_INODE_CLOSESYNC|HAMMER_INODE_CLOSEASYNC)) {
882 if (vn_islocked(vp) == LK_EXCLUSIVE &&
883 (vp->v_flag & (VINACTIVE|VRECLAIMED)) == 0) {
884 if (ip->flags & HAMMER_INODE_CLOSESYNC)
887 waitfor = MNT_NOWAIT;
888 ip->flags &= ~(HAMMER_INODE_CLOSESYNC |
889 HAMMER_INODE_CLOSEASYNC);
890 VOP_FSYNC(vp, MNT_NOWAIT, waitfor);
894 return (vop_stdclose(ap));
898 * hammer_vop_ncreate { nch, dvp, vpp, cred, vap }
900 * The operating system has already ensured that the directory entry
901 * does not exist and done all appropriate namespace locking.
905 hammer_vop_ncreate(struct vop_ncreate_args *ap)
907 struct hammer_transaction trans;
908 struct hammer_inode *dip;
909 struct hammer_inode *nip;
910 struct nchandle *nch;
915 dip = VTOI(ap->a_dvp);
918 if (dip->flags & HAMMER_INODE_RO)
920 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0)
924 * Create a transaction to cover the operations we perform.
926 lwkt_gettoken(&hmp->fs_token);
927 hammer_start_transaction(&trans, hmp);
928 ++hammer_stats_file_iopsw;
931 * Create a new filesystem object of the requested type. The
932 * returned inode will be referenced and shared-locked to prevent
933 * it from being moved to the flusher.
935 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
936 dip, nch->ncp->nc_name, nch->ncp->nc_nlen,
939 hkprintf("hammer_create_inode error %d\n", error);
940 hammer_done_transaction(&trans);
942 lwkt_reltoken(&hmp->fs_token);
947 * Add the new filesystem object to the directory. This will also
948 * bump the inode's link count.
950 error = hammer_ip_add_directory(&trans, dip,
951 nch->ncp->nc_name, nch->ncp->nc_nlen,
954 hkprintf("hammer_ip_add_directory error %d\n", error);
960 hammer_rel_inode(nip, 0);
961 hammer_done_transaction(&trans);
964 error = hammer_get_vnode(nip, ap->a_vpp);
965 hammer_done_transaction(&trans);
966 hammer_rel_inode(nip, 0);
968 cache_setunresolved(ap->a_nch);
969 cache_setvp(ap->a_nch, *ap->a_vpp);
971 hammer_knote(ap->a_dvp, NOTE_WRITE);
973 lwkt_reltoken(&hmp->fs_token);
978 * hammer_vop_getattr { vp, vap }
980 * Retrieve an inode's attribute information. When accessing inodes
981 * historically we fake the atime field to ensure consistent results.
982 * The atime field is stored in the B-Tree element and allowed to be
983 * updated without cycling the element.
985 * MPSAFE - does not require fs_token
989 hammer_vop_getattr(struct vop_getattr_args *ap)
991 struct hammer_inode *ip = VTOI(ap->a_vp);
992 struct vattr *vap = ap->a_vap;
995 * We want the fsid to be different when accessing a filesystem
996 * with different as-of's so programs like diff don't think
997 * the files are the same.
999 * We also want the fsid to be the same when comparing snapshots,
1000 * or when comparing mirrors (which might be backed by different
1001 * physical devices). HAMMER fsids are based on the PFS's
1002 * shared_uuid field.
1004 * XXX there is a chance of collision here. The va_fsid reported
1005 * by stat is different from the more involved fsid used in the
1008 ++hammer_stats_file_iopsr;
1009 hammer_lock_sh(&ip->lock);
1010 vap->va_fsid = ip->pfsm->fsid_udev ^ (u_int32_t)ip->obj_asof ^
1011 (u_int32_t)(ip->obj_asof >> 32);
1013 vap->va_fileid = ip->ino_leaf.base.obj_id;
1014 vap->va_mode = ip->ino_data.mode;
1015 vap->va_nlink = ip->ino_data.nlinks;
1016 vap->va_uid = hammer_to_unix_xid(&ip->ino_data.uid);
1017 vap->va_gid = hammer_to_unix_xid(&ip->ino_data.gid);
1020 vap->va_size = ip->ino_data.size;
1023 * Special case for @@PFS softlinks. The actual size of the
1024 * expanded softlink is "@@0x%016llx:%05d" == 26 bytes.
1025 * or for MAX_TID is "@@-1:%05d" == 10 bytes.
1027 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_SOFTLINK &&
1028 ip->ino_data.size == 10 &&
1029 ip->obj_asof == HAMMER_MAX_TID &&
1030 ip->obj_localization == 0 &&
1031 strncmp(ip->ino_data.ext.symlink, "@@PFS", 5) == 0) {
1032 if (ip->pfsm->pfsd.mirror_flags & HAMMER_PFSD_SLAVE)
1039 * We must provide a consistent atime and mtime for snapshots
1040 * so people can do a 'tar cf - ... | md5' on them and get
1041 * consistent results.
1043 if (ip->flags & HAMMER_INODE_RO) {
1044 hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_atime);
1045 hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_mtime);
1047 hammer_time_to_timespec(ip->ino_data.atime, &vap->va_atime);
1048 hammer_time_to_timespec(ip->ino_data.mtime, &vap->va_mtime);
1050 hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_ctime);
1051 vap->va_flags = ip->ino_data.uflags;
1052 vap->va_gen = 1; /* hammer inums are unique for all time */
1053 vap->va_blocksize = HAMMER_BUFSIZE;
1054 if (ip->ino_data.size >= HAMMER_XDEMARC) {
1055 vap->va_bytes = (ip->ino_data.size + HAMMER_XBUFMASK64) &
1057 } else if (ip->ino_data.size > HAMMER_BUFSIZE / 2) {
1058 vap->va_bytes = (ip->ino_data.size + HAMMER_BUFMASK64) &
1061 vap->va_bytes = (ip->ino_data.size + 15) & ~15;
1064 vap->va_type = hammer_get_vnode_type(ip->ino_data.obj_type);
1065 vap->va_filerev = 0; /* XXX */
1066 vap->va_uid_uuid = ip->ino_data.uid;
1067 vap->va_gid_uuid = ip->ino_data.gid;
1068 vap->va_fsid_uuid = ip->hmp->fsid;
1069 vap->va_vaflags = VA_UID_UUID_VALID | VA_GID_UUID_VALID |
1072 switch (ip->ino_data.obj_type) {
1073 case HAMMER_OBJTYPE_CDEV:
1074 case HAMMER_OBJTYPE_BDEV:
1075 vap->va_rmajor = ip->ino_data.rmajor;
1076 vap->va_rminor = ip->ino_data.rminor;
1081 hammer_unlock(&ip->lock);
1086 * hammer_vop_nresolve { nch, dvp, cred }
1088 * Locate the requested directory entry.
1092 hammer_vop_nresolve(struct vop_nresolve_args *ap)
1094 struct hammer_transaction trans;
1095 struct namecache *ncp;
1100 struct hammer_cursor cursor;
1109 u_int32_t localization;
1110 u_int32_t max_iterations;
1113 * Misc initialization, plus handle as-of name extensions. Look for
1114 * the '@@' extension. Note that as-of files and directories cannot
1117 dip = VTOI(ap->a_dvp);
1118 ncp = ap->a_nch->ncp;
1119 asof = dip->obj_asof;
1120 localization = dip->obj_localization; /* for code consistency */
1121 nlen = ncp->nc_nlen;
1122 flags = dip->flags & HAMMER_INODE_RO;
1126 lwkt_gettoken(&hmp->fs_token);
1127 hammer_simple_transaction(&trans, hmp);
1128 ++hammer_stats_file_iopsr;
1130 for (i = 0; i < nlen; ++i) {
1131 if (ncp->nc_name[i] == '@' && ncp->nc_name[i+1] == '@') {
1132 error = hammer_str_to_tid(ncp->nc_name + i + 2,
1133 &ispfs, &asof, &localization);
1138 if (asof != HAMMER_MAX_TID)
1139 flags |= HAMMER_INODE_RO;
1146 * If this is a PFS softlink we dive into the PFS
1148 if (ispfs && nlen == 0) {
1149 ip = hammer_get_inode(&trans, dip, HAMMER_OBJID_ROOT,
1153 error = hammer_get_vnode(ip, &vp);
1154 hammer_rel_inode(ip, 0);
1160 cache_setvp(ap->a_nch, vp);
1167 * If there is no path component the time extension is relative to dip.
1168 * e.g. "fubar/@@<snapshot>"
1170 * "." is handled by the kernel, but ".@@<snapshot>" is not.
1171 * e.g. "fubar/.@@<snapshot>"
1173 * ".." is handled by the kernel. We do not currently handle
1176 if (nlen == 0 || (nlen == 1 && ncp->nc_name[0] == '.')) {
1177 ip = hammer_get_inode(&trans, dip, dip->obj_id,
1178 asof, dip->obj_localization,
1181 error = hammer_get_vnode(ip, &vp);
1182 hammer_rel_inode(ip, 0);
1188 cache_setvp(ap->a_nch, vp);
1195 * Calculate the namekey and setup the key range for the scan. This
1196 * works kinda like a chained hash table where the lower 32 bits
1197 * of the namekey synthesize the chain.
1199 * The key range is inclusive of both key_beg and key_end.
1201 namekey = hammer_directory_namekey(dip, ncp->nc_name, nlen,
1204 error = hammer_init_cursor(&trans, &cursor, &dip->cache[1], dip);
1205 cursor.key_beg.localization = dip->obj_localization +
1206 hammer_dir_localization(dip);
1207 cursor.key_beg.obj_id = dip->obj_id;
1208 cursor.key_beg.key = namekey;
1209 cursor.key_beg.create_tid = 0;
1210 cursor.key_beg.delete_tid = 0;
1211 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1212 cursor.key_beg.obj_type = 0;
1214 cursor.key_end = cursor.key_beg;
1215 cursor.key_end.key += max_iterations;
1217 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
1220 * Scan all matching records (the chain), locate the one matching
1221 * the requested path component.
1223 * The hammer_ip_*() functions merge in-memory records with on-disk
1224 * records for the purposes of the search.
1227 localization = HAMMER_DEF_LOCALIZATION;
1230 error = hammer_ip_first(&cursor);
1231 while (error == 0) {
1232 error = hammer_ip_resolve_data(&cursor);
1235 if (nlen == cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF &&
1236 bcmp(ncp->nc_name, cursor.data->entry.name, nlen) == 0) {
1237 obj_id = cursor.data->entry.obj_id;
1238 localization = cursor.data->entry.localization;
1241 error = hammer_ip_next(&cursor);
1244 hammer_done_cursor(&cursor);
1247 * Lookup the obj_id. This should always succeed. If it does not
1248 * the filesystem may be damaged and we return a dummy inode.
1251 ip = hammer_get_inode(&trans, dip, obj_id,
1254 if (error == ENOENT) {
1255 kprintf("HAMMER: WARNING: Missing "
1256 "inode for dirent \"%s\"\n"
1257 "\tobj_id = %016llx, asof=%016llx, lo=%08x\n",
1259 (long long)obj_id, (long long)asof,
1262 ip = hammer_get_dummy_inode(&trans, dip, obj_id,
1267 error = hammer_get_vnode(ip, &vp);
1268 hammer_rel_inode(ip, 0);
1274 cache_setvp(ap->a_nch, vp);
1277 } else if (error == ENOENT) {
1278 cache_setvp(ap->a_nch, NULL);
1281 hammer_done_transaction(&trans);
1282 lwkt_reltoken(&hmp->fs_token);
1287 * hammer_vop_nlookupdotdot { dvp, vpp, cred }
1289 * Locate the parent directory of a directory vnode.
1291 * dvp is referenced but not locked. *vpp must be returned referenced and
1292 * locked. A parent_obj_id of 0 does not necessarily indicate that we are
1293 * at the root, instead it could indicate that the directory we were in was
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;
1307 struct hammer_inode *dip;
1308 struct hammer_inode *ip;
1310 int64_t parent_obj_id;
1311 u_int32_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 = dip->ino_data.ext.obj.parent_obj_localization;
1328 parent_obj_localization = dip->obj_localization;
1330 if (parent_obj_id == 0) {
1331 if (dip->obj_id == HAMMER_OBJID_ROOT &&
1332 asof != hmp->asof) {
1333 parent_obj_id = dip->obj_id;
1335 *ap->a_fakename = kmalloc(19, M_TEMP, M_WAITOK);
1336 ksnprintf(*ap->a_fakename, 19, "0x%016llx",
1337 (long long)dip->obj_asof);
1340 lwkt_reltoken(&hmp->fs_token);
1345 hammer_simple_transaction(&trans, hmp);
1346 ++hammer_stats_file_iopsr;
1348 ip = hammer_get_inode(&trans, dip, parent_obj_id,
1349 asof, parent_obj_localization,
1350 dip->flags, &error);
1352 error = hammer_get_vnode(ip, ap->a_vpp);
1353 hammer_rel_inode(ip, 0);
1357 hammer_done_transaction(&trans);
1358 lwkt_reltoken(&hmp->fs_token);
1363 * hammer_vop_nlink { nch, dvp, vp, cred }
1367 hammer_vop_nlink(struct vop_nlink_args *ap)
1369 struct hammer_transaction trans;
1370 struct hammer_inode *dip;
1371 struct hammer_inode *ip;
1372 struct nchandle *nch;
1376 if (ap->a_dvp->v_mount != ap->a_vp->v_mount)
1380 dip = VTOI(ap->a_dvp);
1381 ip = VTOI(ap->a_vp);
1384 if (dip->obj_localization != ip->obj_localization)
1387 if (dip->flags & HAMMER_INODE_RO)
1389 if (ip->flags & HAMMER_INODE_RO)
1391 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0)
1395 * Create a transaction to cover the operations we perform.
1397 lwkt_gettoken(&hmp->fs_token);
1398 hammer_start_transaction(&trans, hmp);
1399 ++hammer_stats_file_iopsw;
1402 * Add the filesystem object to the directory. Note that neither
1403 * dip nor ip are referenced or locked, but their vnodes are
1404 * referenced. This function will bump the inode's link count.
1406 error = hammer_ip_add_directory(&trans, dip,
1407 nch->ncp->nc_name, nch->ncp->nc_nlen,
1414 cache_setunresolved(nch);
1415 cache_setvp(nch, ap->a_vp);
1417 hammer_done_transaction(&trans);
1418 hammer_knote(ap->a_vp, NOTE_LINK);
1419 hammer_knote(ap->a_dvp, NOTE_WRITE);
1420 lwkt_reltoken(&hmp->fs_token);
1425 * hammer_vop_nmkdir { nch, dvp, vpp, cred, vap }
1427 * The operating system has already ensured that the directory entry
1428 * does not exist and done all appropriate namespace locking.
1432 hammer_vop_nmkdir(struct vop_nmkdir_args *ap)
1434 struct hammer_transaction trans;
1435 struct hammer_inode *dip;
1436 struct hammer_inode *nip;
1437 struct nchandle *nch;
1442 dip = VTOI(ap->a_dvp);
1445 if (dip->flags & HAMMER_INODE_RO)
1447 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0)
1451 * Create a transaction to cover the operations we perform.
1453 lwkt_gettoken(&hmp->fs_token);
1454 hammer_start_transaction(&trans, hmp);
1455 ++hammer_stats_file_iopsw;
1458 * Create a new filesystem object of the requested type. The
1459 * returned inode will be referenced but not locked.
1461 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
1462 dip, nch->ncp->nc_name, nch->ncp->nc_nlen,
1465 hkprintf("hammer_mkdir error %d\n", error);
1466 hammer_done_transaction(&trans);
1468 lwkt_reltoken(&hmp->fs_token);
1472 * Add the new filesystem object to the directory. This will also
1473 * bump the inode's link count.
1475 error = hammer_ip_add_directory(&trans, dip,
1476 nch->ncp->nc_name, nch->ncp->nc_nlen,
1479 hkprintf("hammer_mkdir (add) error %d\n", error);
1485 hammer_rel_inode(nip, 0);
1488 error = hammer_get_vnode(nip, ap->a_vpp);
1489 hammer_rel_inode(nip, 0);
1491 cache_setunresolved(ap->a_nch);
1492 cache_setvp(ap->a_nch, *ap->a_vpp);
1495 hammer_done_transaction(&trans);
1497 hammer_knote(ap->a_dvp, NOTE_WRITE | NOTE_LINK);
1498 lwkt_reltoken(&hmp->fs_token);
1503 * hammer_vop_nmknod { nch, dvp, vpp, cred, vap }
1505 * The operating system has already ensured that the directory entry
1506 * does not exist and done all appropriate namespace locking.
1510 hammer_vop_nmknod(struct vop_nmknod_args *ap)
1512 struct hammer_transaction trans;
1513 struct hammer_inode *dip;
1514 struct hammer_inode *nip;
1515 struct nchandle *nch;
1520 dip = VTOI(ap->a_dvp);
1523 if (dip->flags & HAMMER_INODE_RO)
1525 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0)
1529 * Create a transaction to cover the operations we perform.
1531 lwkt_gettoken(&hmp->fs_token);
1532 hammer_start_transaction(&trans, hmp);
1533 ++hammer_stats_file_iopsw;
1536 * Create a new filesystem object of the requested type. The
1537 * returned inode will be referenced but not locked.
1539 * If mknod specifies a directory a pseudo-fs is created.
1541 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
1542 dip, nch->ncp->nc_name, nch->ncp->nc_nlen,
1545 hammer_done_transaction(&trans);
1547 lwkt_reltoken(&hmp->fs_token);
1552 * Add the new filesystem object to the directory. This will also
1553 * bump the inode's link count.
1555 error = hammer_ip_add_directory(&trans, dip,
1556 nch->ncp->nc_name, nch->ncp->nc_nlen,
1563 hammer_rel_inode(nip, 0);
1566 error = hammer_get_vnode(nip, ap->a_vpp);
1567 hammer_rel_inode(nip, 0);
1569 cache_setunresolved(ap->a_nch);
1570 cache_setvp(ap->a_nch, *ap->a_vpp);
1573 hammer_done_transaction(&trans);
1575 hammer_knote(ap->a_dvp, NOTE_WRITE);
1576 lwkt_reltoken(&hmp->fs_token);
1581 * hammer_vop_open { vp, mode, cred, fp }
1583 * MPSAFE (does not require fs_token)
1587 hammer_vop_open(struct vop_open_args *ap)
1591 ++hammer_stats_file_iopsr;
1592 ip = VTOI(ap->a_vp);
1594 if ((ap->a_mode & FWRITE) && (ip->flags & HAMMER_INODE_RO))
1596 return(vop_stdopen(ap));
1600 * hammer_vop_print { vp }
1604 hammer_vop_print(struct vop_print_args *ap)
1610 * hammer_vop_readdir { vp, uio, cred, *eofflag, *ncookies, off_t **cookies }
1614 hammer_vop_readdir(struct vop_readdir_args *ap)
1616 struct hammer_transaction trans;
1617 struct hammer_cursor cursor;
1618 struct hammer_inode *ip;
1621 hammer_base_elm_t base;
1630 ++hammer_stats_file_iopsr;
1631 ip = VTOI(ap->a_vp);
1633 saveoff = uio->uio_offset;
1636 if (ap->a_ncookies) {
1637 ncookies = uio->uio_resid / 16 + 1;
1638 if (ncookies > 1024)
1640 cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
1648 lwkt_gettoken(&hmp->fs_token);
1649 hammer_simple_transaction(&trans, hmp);
1652 * Handle artificial entries
1654 * It should be noted that the minimum value for a directory
1655 * hash key on-media is 0x0000000100000000, so we can use anything
1656 * less then that to represent our 'special' key space.
1660 r = vop_write_dirent(&error, uio, ip->obj_id, DT_DIR, 1, ".");
1664 cookies[cookie_index] = saveoff;
1667 if (cookie_index == ncookies)
1671 if (ip->ino_data.parent_obj_id) {
1672 r = vop_write_dirent(&error, uio,
1673 ip->ino_data.parent_obj_id,
1676 r = vop_write_dirent(&error, uio,
1677 ip->obj_id, DT_DIR, 2, "..");
1682 cookies[cookie_index] = saveoff;
1685 if (cookie_index == ncookies)
1690 * Key range (begin and end inclusive) to scan. Directory keys
1691 * directly translate to a 64 bit 'seek' position.
1693 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
1694 cursor.key_beg.localization = ip->obj_localization +
1695 hammer_dir_localization(ip);
1696 cursor.key_beg.obj_id = ip->obj_id;
1697 cursor.key_beg.create_tid = 0;
1698 cursor.key_beg.delete_tid = 0;
1699 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1700 cursor.key_beg.obj_type = 0;
1701 cursor.key_beg.key = saveoff;
1703 cursor.key_end = cursor.key_beg;
1704 cursor.key_end.key = HAMMER_MAX_KEY;
1705 cursor.asof = ip->obj_asof;
1706 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
1708 error = hammer_ip_first(&cursor);
1710 while (error == 0) {
1711 error = hammer_ip_resolve_data(&cursor);
1714 base = &cursor.leaf->base;
1715 saveoff = base->key;
1716 KKASSERT(cursor.leaf->data_len > HAMMER_ENTRY_NAME_OFF);
1718 if (base->obj_id != ip->obj_id)
1719 panic("readdir: bad record at %p", cursor.node);
1722 * Convert pseudo-filesystems into softlinks
1724 dtype = hammer_get_dtype(cursor.leaf->base.obj_type);
1725 r = vop_write_dirent(
1726 &error, uio, cursor.data->entry.obj_id,
1728 cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF ,
1729 (void *)cursor.data->entry.name);
1734 cookies[cookie_index] = base->key;
1736 if (cookie_index == ncookies)
1738 error = hammer_ip_next(&cursor);
1740 hammer_done_cursor(&cursor);
1743 hammer_done_transaction(&trans);
1746 *ap->a_eofflag = (error == ENOENT);
1747 uio->uio_offset = saveoff;
1748 if (error && cookie_index == 0) {
1749 if (error == ENOENT)
1752 kfree(cookies, M_TEMP);
1753 *ap->a_ncookies = 0;
1754 *ap->a_cookies = NULL;
1757 if (error == ENOENT)
1760 *ap->a_ncookies = cookie_index;
1761 *ap->a_cookies = cookies;
1764 lwkt_reltoken(&hmp->fs_token);
1769 * hammer_vop_readlink { vp, uio, cred }
1773 hammer_vop_readlink(struct vop_readlink_args *ap)
1775 struct hammer_transaction trans;
1776 struct hammer_cursor cursor;
1777 struct hammer_inode *ip;
1780 u_int32_t localization;
1781 hammer_pseudofs_inmem_t pfsm;
1784 ip = VTOI(ap->a_vp);
1787 lwkt_gettoken(&hmp->fs_token);
1790 * Shortcut if the symlink data was stuffed into ino_data.
1792 * Also expand special "@@PFS%05d" softlinks (expansion only
1793 * occurs for non-historical (current) accesses made from the
1794 * primary filesystem).
1796 if (ip->ino_data.size <= HAMMER_INODE_BASESYMLEN) {
1800 ptr = ip->ino_data.ext.symlink;
1801 bytes = (int)ip->ino_data.size;
1803 ip->obj_asof == HAMMER_MAX_TID &&
1804 ip->obj_localization == 0 &&
1805 strncmp(ptr, "@@PFS", 5) == 0) {
1806 hammer_simple_transaction(&trans, hmp);
1807 bcopy(ptr + 5, buf, 5);
1809 localization = strtoul(buf, NULL, 10) << 16;
1810 pfsm = hammer_load_pseudofs(&trans, localization,
1813 if (pfsm->pfsd.mirror_flags &
1814 HAMMER_PFSD_SLAVE) {
1815 /* vap->va_size == 26 */
1816 ksnprintf(buf, sizeof(buf),
1818 (long long)pfsm->pfsd.sync_end_tid,
1819 localization >> 16);
1821 /* vap->va_size == 10 */
1822 ksnprintf(buf, sizeof(buf),
1824 localization >> 16);
1826 ksnprintf(buf, sizeof(buf),
1828 (long long)HAMMER_MAX_TID,
1829 localization >> 16);
1833 bytes = strlen(buf);
1836 hammer_rel_pseudofs(hmp, pfsm);
1837 hammer_done_transaction(&trans);
1839 error = uiomove(ptr, bytes, ap->a_uio);
1840 lwkt_reltoken(&hmp->fs_token);
1847 hammer_simple_transaction(&trans, hmp);
1848 ++hammer_stats_file_iopsr;
1849 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
1852 * Key range (begin and end inclusive) to scan. Directory keys
1853 * directly translate to a 64 bit 'seek' position.
1855 cursor.key_beg.localization = ip->obj_localization +
1856 HAMMER_LOCALIZE_MISC;
1857 cursor.key_beg.obj_id = ip->obj_id;
1858 cursor.key_beg.create_tid = 0;
1859 cursor.key_beg.delete_tid = 0;
1860 cursor.key_beg.rec_type = HAMMER_RECTYPE_FIX;
1861 cursor.key_beg.obj_type = 0;
1862 cursor.key_beg.key = HAMMER_FIXKEY_SYMLINK;
1863 cursor.asof = ip->obj_asof;
1864 cursor.flags |= HAMMER_CURSOR_ASOF;
1866 error = hammer_ip_lookup(&cursor);
1868 error = hammer_ip_resolve_data(&cursor);
1870 KKASSERT(cursor.leaf->data_len >=
1871 HAMMER_SYMLINK_NAME_OFF);
1872 error = uiomove(cursor.data->symlink.name,
1873 cursor.leaf->data_len -
1874 HAMMER_SYMLINK_NAME_OFF,
1878 hammer_done_cursor(&cursor);
1879 hammer_done_transaction(&trans);
1880 lwkt_reltoken(&hmp->fs_token);
1885 * hammer_vop_nremove { nch, dvp, cred }
1889 hammer_vop_nremove(struct vop_nremove_args *ap)
1891 struct hammer_transaction trans;
1892 struct hammer_inode *dip;
1896 dip = VTOI(ap->a_dvp);
1899 if (hammer_nohistory(dip) == 0 &&
1900 (error = hammer_checkspace(hmp, HAMMER_CHKSPC_REMOVE)) != 0) {
1904 lwkt_gettoken(&hmp->fs_token);
1905 hammer_start_transaction(&trans, hmp);
1906 ++hammer_stats_file_iopsw;
1907 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp, ap->a_cred, 0, 0);
1908 hammer_done_transaction(&trans);
1910 hammer_knote(ap->a_dvp, NOTE_WRITE);
1911 lwkt_reltoken(&hmp->fs_token);
1916 * hammer_vop_nrename { fnch, tnch, fdvp, tdvp, cred }
1920 hammer_vop_nrename(struct vop_nrename_args *ap)
1922 struct hammer_transaction trans;
1923 struct namecache *fncp;
1924 struct namecache *tncp;
1925 struct hammer_inode *fdip;
1926 struct hammer_inode *tdip;
1927 struct hammer_inode *ip;
1929 struct hammer_cursor cursor;
1931 u_int32_t max_iterations;
1934 if (ap->a_fdvp->v_mount != ap->a_tdvp->v_mount)
1936 if (ap->a_fdvp->v_mount != ap->a_fnch->ncp->nc_vp->v_mount)
1939 fdip = VTOI(ap->a_fdvp);
1940 tdip = VTOI(ap->a_tdvp);
1941 fncp = ap->a_fnch->ncp;
1942 tncp = ap->a_tnch->ncp;
1943 ip = VTOI(fncp->nc_vp);
1944 KKASSERT(ip != NULL);
1948 if (fdip->obj_localization != tdip->obj_localization)
1950 if (fdip->obj_localization != ip->obj_localization)
1953 if (fdip->flags & HAMMER_INODE_RO)
1955 if (tdip->flags & HAMMER_INODE_RO)
1957 if (ip->flags & HAMMER_INODE_RO)
1959 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0)
1962 lwkt_gettoken(&hmp->fs_token);
1963 hammer_start_transaction(&trans, hmp);
1964 ++hammer_stats_file_iopsw;
1967 * Remove tncp from the target directory and then link ip as
1968 * tncp. XXX pass trans to dounlink
1970 * Force the inode sync-time to match the transaction so it is
1971 * in-sync with the creation of the target directory entry.
1973 error = hammer_dounlink(&trans, ap->a_tnch, ap->a_tdvp,
1975 if (error == 0 || error == ENOENT) {
1976 error = hammer_ip_add_directory(&trans, tdip,
1977 tncp->nc_name, tncp->nc_nlen,
1980 ip->ino_data.parent_obj_id = tdip->obj_id;
1981 ip->ino_data.ctime = trans.time;
1982 hammer_modify_inode(&trans, ip, HAMMER_INODE_DDIRTY);
1986 goto failed; /* XXX */
1989 * Locate the record in the originating directory and remove it.
1991 * Calculate the namekey and setup the key range for the scan. This
1992 * works kinda like a chained hash table where the lower 32 bits
1993 * of the namekey synthesize the chain.
1995 * The key range is inclusive of both key_beg and key_end.
1997 namekey = hammer_directory_namekey(fdip, fncp->nc_name, fncp->nc_nlen,
2000 hammer_init_cursor(&trans, &cursor, &fdip->cache[1], fdip);
2001 cursor.key_beg.localization = fdip->obj_localization +
2002 hammer_dir_localization(fdip);
2003 cursor.key_beg.obj_id = fdip->obj_id;
2004 cursor.key_beg.key = namekey;
2005 cursor.key_beg.create_tid = 0;
2006 cursor.key_beg.delete_tid = 0;
2007 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
2008 cursor.key_beg.obj_type = 0;
2010 cursor.key_end = cursor.key_beg;
2011 cursor.key_end.key += max_iterations;
2012 cursor.asof = fdip->obj_asof;
2013 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
2016 * Scan all matching records (the chain), locate the one matching
2017 * the requested path component.
2019 * The hammer_ip_*() functions merge in-memory records with on-disk
2020 * records for the purposes of the search.
2022 error = hammer_ip_first(&cursor);
2023 while (error == 0) {
2024 if (hammer_ip_resolve_data(&cursor) != 0)
2026 nlen = cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF;
2028 if (fncp->nc_nlen == nlen &&
2029 bcmp(fncp->nc_name, cursor.data->entry.name, nlen) == 0) {
2032 error = hammer_ip_next(&cursor);
2036 * If all is ok we have to get the inode so we can adjust nlinks.
2038 * WARNING: hammer_ip_del_directory() may have to terminate the
2039 * cursor to avoid a recursion. It's ok to call hammer_done_cursor()
2043 error = hammer_ip_del_directory(&trans, &cursor, fdip, ip);
2046 * XXX A deadlock here will break rename's atomicy for the purposes
2047 * of crash recovery.
2049 if (error == EDEADLK) {
2050 hammer_done_cursor(&cursor);
2055 * Cleanup and tell the kernel that the rename succeeded.
2057 * NOTE: ip->vp, if non-NULL, cannot be directly referenced
2058 * without formally acquiring the vp since the vp might
2059 * have zero refs on it, or in the middle of a reclaim,
2062 hammer_done_cursor(&cursor);
2064 cache_rename(ap->a_fnch, ap->a_tnch);
2065 hammer_knote(ap->a_fdvp, NOTE_WRITE);
2066 hammer_knote(ap->a_tdvp, NOTE_WRITE);
2070 error = hammer_get_vnode(ip, &vp);
2071 if (error == 0 && vp) {
2073 hammer_knote(ip->vp, NOTE_RENAME);
2077 kprintf("Debug: HAMMER ip/vp race2 avoided\n");
2082 hammer_done_transaction(&trans);
2083 lwkt_reltoken(&hmp->fs_token);
2088 * hammer_vop_nrmdir { nch, dvp, cred }
2092 hammer_vop_nrmdir(struct vop_nrmdir_args *ap)
2094 struct hammer_transaction trans;
2095 struct hammer_inode *dip;
2099 dip = VTOI(ap->a_dvp);
2102 if (hammer_nohistory(dip) == 0 &&
2103 (error = hammer_checkspace(hmp, HAMMER_CHKSPC_REMOVE)) != 0) {
2107 lwkt_gettoken(&hmp->fs_token);
2108 hammer_start_transaction(&trans, hmp);
2109 ++hammer_stats_file_iopsw;
2110 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp, ap->a_cred, 0, 1);
2111 hammer_done_transaction(&trans);
2113 hammer_knote(ap->a_dvp, NOTE_WRITE | NOTE_LINK);
2114 lwkt_reltoken(&hmp->fs_token);
2119 * hammer_vop_markatime { vp, cred }
2123 hammer_vop_markatime(struct vop_markatime_args *ap)
2125 struct hammer_transaction trans;
2126 struct hammer_inode *ip;
2129 ip = VTOI(ap->a_vp);
2130 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
2132 if (ip->flags & HAMMER_INODE_RO)
2135 if (hmp->mp->mnt_flag & MNT_NOATIME)
2137 lwkt_gettoken(&hmp->fs_token);
2138 hammer_start_transaction(&trans, hmp);
2139 ++hammer_stats_file_iopsw;
2141 ip->ino_data.atime = trans.time;
2142 hammer_modify_inode(&trans, ip, HAMMER_INODE_ATIME);
2143 hammer_done_transaction(&trans);
2144 hammer_knote(ap->a_vp, NOTE_ATTRIB);
2145 lwkt_reltoken(&hmp->fs_token);
2150 * hammer_vop_setattr { vp, vap, cred }
2154 hammer_vop_setattr(struct vop_setattr_args *ap)
2156 struct hammer_transaction trans;
2157 struct hammer_inode *ip;
2166 int64_t aligned_size;
2171 ip = ap->a_vp->v_data;
2176 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
2178 if (ip->flags & HAMMER_INODE_RO)
2180 if (hammer_nohistory(ip) == 0 &&
2181 (error = hammer_checkspace(hmp, HAMMER_CHKSPC_REMOVE)) != 0) {
2185 lwkt_gettoken(&hmp->fs_token);
2186 hammer_start_transaction(&trans, hmp);
2187 ++hammer_stats_file_iopsw;
2190 if (vap->va_flags != VNOVAL) {
2191 flags = ip->ino_data.uflags;
2192 error = vop_helper_setattr_flags(&flags, vap->va_flags,
2193 hammer_to_unix_xid(&ip->ino_data.uid),
2196 if (ip->ino_data.uflags != flags) {
2197 ip->ino_data.uflags = flags;
2198 ip->ino_data.ctime = trans.time;
2199 modflags |= HAMMER_INODE_DDIRTY;
2200 kflags |= NOTE_ATTRIB;
2202 if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) {
2209 if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) {
2213 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
2214 mode_t cur_mode = ip->ino_data.mode;
2215 uid_t cur_uid = hammer_to_unix_xid(&ip->ino_data.uid);
2216 gid_t cur_gid = hammer_to_unix_xid(&ip->ino_data.gid);
2220 error = vop_helper_chown(ap->a_vp, vap->va_uid, vap->va_gid,
2222 &cur_uid, &cur_gid, &cur_mode);
2224 hammer_guid_to_uuid(&uuid_uid, cur_uid);
2225 hammer_guid_to_uuid(&uuid_gid, cur_gid);
2226 if (bcmp(&uuid_uid, &ip->ino_data.uid,
2227 sizeof(uuid_uid)) ||
2228 bcmp(&uuid_gid, &ip->ino_data.gid,
2229 sizeof(uuid_gid)) ||
2230 ip->ino_data.mode != cur_mode
2232 ip->ino_data.uid = uuid_uid;
2233 ip->ino_data.gid = uuid_gid;
2234 ip->ino_data.mode = cur_mode;
2235 ip->ino_data.ctime = trans.time;
2236 modflags |= HAMMER_INODE_DDIRTY;
2238 kflags |= NOTE_ATTRIB;
2241 while (vap->va_size != VNOVAL && ip->ino_data.size != vap->va_size) {
2242 switch(ap->a_vp->v_type) {
2244 if (vap->va_size == ip->ino_data.size)
2248 * Log the operation if in fast-fsync mode or if
2249 * there are unterminated redo write records present.
2251 * The second check is needed so the recovery code
2252 * properly truncates write redos even if nominal
2253 * REDO operations is turned off due to excessive
2254 * writes, because the related records might be
2255 * destroyed and never lay down a TERM_WRITE.
2257 if ((ip->flags & HAMMER_INODE_REDO) ||
2258 (ip->flags & HAMMER_INODE_RDIRTY)) {
2259 error = hammer_generate_redo(&trans, ip,
2264 blksize = hammer_blocksize(vap->va_size);
2267 * XXX break atomicy, we can deadlock the backend
2268 * if we do not release the lock. Probably not a
2271 if (vap->va_size < ip->ino_data.size) {
2272 nvtruncbuf(ap->a_vp, vap->va_size,
2274 hammer_blockoff(vap->va_size));
2276 kflags |= NOTE_WRITE;
2278 nvextendbuf(ap->a_vp,
2281 hammer_blocksize(ip->ino_data.size),
2282 hammer_blocksize(vap->va_size),
2283 hammer_blockoff(ip->ino_data.size),
2284 hammer_blockoff(vap->va_size),
2287 kflags |= NOTE_WRITE | NOTE_EXTEND;
2289 ip->ino_data.size = vap->va_size;
2290 ip->ino_data.mtime = trans.time;
2291 /* XXX safe to use SDIRTY instead of DDIRTY here? */
2292 modflags |= HAMMER_INODE_MTIME | HAMMER_INODE_DDIRTY;
2295 * On-media truncation is cached in the inode until
2296 * the inode is synchronized. We must immediately
2297 * handle any frontend records.
2300 hammer_ip_frontend_trunc(ip, vap->va_size);
2301 #ifdef DEBUG_TRUNCATE
2302 if (HammerTruncIp == NULL)
2305 if ((ip->flags & HAMMER_INODE_TRUNCATED) == 0) {
2306 ip->flags |= HAMMER_INODE_TRUNCATED;
2307 ip->trunc_off = vap->va_size;
2308 #ifdef DEBUG_TRUNCATE
2309 if (ip == HammerTruncIp)
2310 kprintf("truncate1 %016llx\n",
2311 (long long)ip->trunc_off);
2313 } else if (ip->trunc_off > vap->va_size) {
2314 ip->trunc_off = vap->va_size;
2315 #ifdef DEBUG_TRUNCATE
2316 if (ip == HammerTruncIp)
2317 kprintf("truncate2 %016llx\n",
2318 (long long)ip->trunc_off);
2321 #ifdef DEBUG_TRUNCATE
2322 if (ip == HammerTruncIp)
2323 kprintf("truncate3 %016llx (ignored)\n",
2324 (long long)vap->va_size);
2331 * When truncating, nvtruncbuf() may have cleaned out
2332 * a portion of the last block on-disk in the buffer
2333 * cache. We must clean out any frontend records
2334 * for blocks beyond the new last block.
2336 aligned_size = (vap->va_size + (blksize - 1)) &
2337 ~(int64_t)(blksize - 1);
2338 if (truncating && vap->va_size < aligned_size) {
2339 aligned_size -= blksize;
2340 hammer_ip_frontend_trunc(ip, aligned_size);
2345 if ((ip->flags & HAMMER_INODE_TRUNCATED) == 0) {
2346 ip->flags |= HAMMER_INODE_TRUNCATED;
2347 ip->trunc_off = vap->va_size;
2348 } else if (ip->trunc_off > vap->va_size) {
2349 ip->trunc_off = vap->va_size;
2351 hammer_ip_frontend_trunc(ip, vap->va_size);
2352 ip->ino_data.size = vap->va_size;
2353 ip->ino_data.mtime = trans.time;
2354 modflags |= HAMMER_INODE_MTIME | HAMMER_INODE_DDIRTY;
2355 kflags |= NOTE_ATTRIB;
2363 if (vap->va_atime.tv_sec != VNOVAL) {
2364 ip->ino_data.atime = hammer_timespec_to_time(&vap->va_atime);
2365 modflags |= HAMMER_INODE_ATIME;
2366 kflags |= NOTE_ATTRIB;
2368 if (vap->va_mtime.tv_sec != VNOVAL) {
2369 ip->ino_data.mtime = hammer_timespec_to_time(&vap->va_mtime);
2370 modflags |= HAMMER_INODE_MTIME;
2371 kflags |= NOTE_ATTRIB;
2373 if (vap->va_mode != (mode_t)VNOVAL) {
2374 mode_t cur_mode = ip->ino_data.mode;
2375 uid_t cur_uid = hammer_to_unix_xid(&ip->ino_data.uid);
2376 gid_t cur_gid = hammer_to_unix_xid(&ip->ino_data.gid);
2378 error = vop_helper_chmod(ap->a_vp, vap->va_mode, ap->a_cred,
2379 cur_uid, cur_gid, &cur_mode);
2380 if (error == 0 && ip->ino_data.mode != cur_mode) {
2381 ip->ino_data.mode = cur_mode;
2382 ip->ino_data.ctime = trans.time;
2383 modflags |= HAMMER_INODE_DDIRTY;
2384 kflags |= NOTE_ATTRIB;
2389 hammer_modify_inode(&trans, ip, modflags);
2390 hammer_done_transaction(&trans);
2391 hammer_knote(ap->a_vp, kflags);
2392 lwkt_reltoken(&hmp->fs_token);
2397 * hammer_vop_nsymlink { nch, dvp, vpp, cred, vap, target }
2401 hammer_vop_nsymlink(struct vop_nsymlink_args *ap)
2403 struct hammer_transaction trans;
2404 struct hammer_inode *dip;
2405 struct hammer_inode *nip;
2406 hammer_record_t record;
2407 struct nchandle *nch;
2412 ap->a_vap->va_type = VLNK;
2415 dip = VTOI(ap->a_dvp);
2418 if (dip->flags & HAMMER_INODE_RO)
2420 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0)
2424 * Create a transaction to cover the operations we perform.
2426 lwkt_gettoken(&hmp->fs_token);
2427 hammer_start_transaction(&trans, hmp);
2428 ++hammer_stats_file_iopsw;
2431 * Create a new filesystem object of the requested type. The
2432 * returned inode will be referenced but not locked.
2435 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
2436 dip, nch->ncp->nc_name, nch->ncp->nc_nlen,
2439 hammer_done_transaction(&trans);
2441 lwkt_reltoken(&hmp->fs_token);
2446 * Add a record representing the symlink. symlink stores the link
2447 * as pure data, not a string, and is no \0 terminated.
2450 bytes = strlen(ap->a_target);
2452 if (bytes <= HAMMER_INODE_BASESYMLEN) {
2453 bcopy(ap->a_target, nip->ino_data.ext.symlink, bytes);
2455 record = hammer_alloc_mem_record(nip, bytes);
2456 record->type = HAMMER_MEM_RECORD_GENERAL;
2458 record->leaf.base.localization = nip->obj_localization +
2459 HAMMER_LOCALIZE_MISC;
2460 record->leaf.base.key = HAMMER_FIXKEY_SYMLINK;
2461 record->leaf.base.rec_type = HAMMER_RECTYPE_FIX;
2462 record->leaf.data_len = bytes;
2463 KKASSERT(HAMMER_SYMLINK_NAME_OFF == 0);
2464 bcopy(ap->a_target, record->data->symlink.name, bytes);
2465 error = hammer_ip_add_record(&trans, record);
2469 * Set the file size to the length of the link.
2472 nip->ino_data.size = bytes;
2473 hammer_modify_inode(&trans, nip, HAMMER_INODE_DDIRTY);
2477 error = hammer_ip_add_directory(&trans, dip, nch->ncp->nc_name,
2478 nch->ncp->nc_nlen, nip);
2484 hammer_rel_inode(nip, 0);
2487 error = hammer_get_vnode(nip, ap->a_vpp);
2488 hammer_rel_inode(nip, 0);
2490 cache_setunresolved(ap->a_nch);
2491 cache_setvp(ap->a_nch, *ap->a_vpp);
2492 hammer_knote(ap->a_dvp, NOTE_WRITE);
2495 hammer_done_transaction(&trans);
2496 lwkt_reltoken(&hmp->fs_token);
2501 * hammer_vop_nwhiteout { nch, dvp, cred, flags }
2505 hammer_vop_nwhiteout(struct vop_nwhiteout_args *ap)
2507 struct hammer_transaction trans;
2508 struct hammer_inode *dip;
2512 dip = VTOI(ap->a_dvp);
2515 if (hammer_nohistory(dip) == 0 &&
2516 (error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0) {
2520 lwkt_gettoken(&hmp->fs_token);
2521 hammer_start_transaction(&trans, hmp);
2522 ++hammer_stats_file_iopsw;
2523 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp,
2524 ap->a_cred, ap->a_flags, -1);
2525 hammer_done_transaction(&trans);
2526 lwkt_reltoken(&hmp->fs_token);
2532 * hammer_vop_ioctl { vp, command, data, fflag, cred }
2536 hammer_vop_ioctl(struct vop_ioctl_args *ap)
2538 struct hammer_inode *ip = ap->a_vp->v_data;
2539 hammer_mount_t hmp = ip->hmp;
2542 ++hammer_stats_file_iopsr;
2543 lwkt_gettoken(&hmp->fs_token);
2544 error = hammer_ioctl(ip, ap->a_command, ap->a_data,
2545 ap->a_fflag, ap->a_cred);
2546 lwkt_reltoken(&hmp->fs_token);
2552 hammer_vop_mountctl(struct vop_mountctl_args *ap)
2554 static const struct mountctl_opt extraopt[] = {
2555 { HMNT_NOHISTORY, "nohistory" },
2556 { HMNT_MASTERID, "master" },
2560 struct hammer_mount *hmp;
2567 mp = ap->a_head.a_ops->head.vv_mount;
2568 KKASSERT(mp->mnt_data != NULL);
2569 hmp = (struct hammer_mount *)mp->mnt_data;
2571 lwkt_gettoken(&hmp->fs_token);
2574 case MOUNTCTL_SET_EXPORT:
2575 if (ap->a_ctllen != sizeof(struct export_args))
2578 error = hammer_vfs_export(mp, ap->a_op,
2579 (const struct export_args *)ap->a_ctl);
2581 case MOUNTCTL_MOUNTFLAGS:
2584 * Call standard mountctl VOP function
2585 * so we get user mount flags.
2587 error = vop_stdmountctl(ap);
2591 usedbytes = *ap->a_res;
2593 if (usedbytes > 0 && usedbytes < ap->a_buflen) {
2594 usedbytes += vfs_flagstostr(hmp->hflags, extraopt,
2596 ap->a_buflen - usedbytes,
2600 *ap->a_res += usedbytes;
2604 error = vop_stdmountctl(ap);
2607 lwkt_reltoken(&hmp->fs_token);
2612 * hammer_vop_strategy { vp, bio }
2614 * Strategy call, used for regular file read & write only. Note that the
2615 * bp may represent a cluster.
2617 * To simplify operation and allow better optimizations in the future,
2618 * this code does not make any assumptions with regards to buffer alignment
2623 hammer_vop_strategy(struct vop_strategy_args *ap)
2628 bp = ap->a_bio->bio_buf;
2632 error = hammer_vop_strategy_read(ap);
2635 error = hammer_vop_strategy_write(ap);
2638 bp->b_error = error = EINVAL;
2639 bp->b_flags |= B_ERROR;
2644 /* hammer_dump_dedup_cache(((hammer_inode_t)ap->a_vp->v_data)->hmp); */
2650 * Read from a regular file. Iterate the related records and fill in the
2651 * BIO/BUF. Gaps are zero-filled.
2653 * The support code in hammer_object.c should be used to deal with mixed
2654 * in-memory and on-disk records.
2656 * NOTE: Can be called from the cluster code with an oversized buf.
2662 hammer_vop_strategy_read(struct vop_strategy_args *ap)
2664 struct hammer_transaction trans;
2665 struct hammer_inode *ip;
2666 struct hammer_inode *dip;
2668 struct hammer_cursor cursor;
2669 hammer_base_elm_t base;
2670 hammer_off_t disk_offset;
2685 ip = ap->a_vp->v_data;
2689 * The zone-2 disk offset may have been set by the cluster code via
2690 * a BMAP operation, or else should be NOOFFSET.
2692 * Checking the high bits for a match against zone-2 should suffice.
2694 * In cases where a lot of data duplication is present it may be
2695 * more beneficial to drop through and doubule-buffer through the
2698 nbio = push_bio(bio);
2699 if (hammer_double_buffer == 0 &&
2700 (nbio->bio_offset & HAMMER_OFF_ZONE_MASK) ==
2701 HAMMER_ZONE_LARGE_DATA) {
2702 lwkt_gettoken(&hmp->fs_token);
2703 error = hammer_io_direct_read(hmp, nbio, NULL);
2704 lwkt_reltoken(&hmp->fs_token);
2709 * Well, that sucked. Do it the hard way. If all the stars are
2710 * aligned we may still be able to issue a direct-read.
2712 lwkt_gettoken(&hmp->fs_token);
2713 hammer_simple_transaction(&trans, hmp);
2714 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
2717 * Key range (begin and end inclusive) to scan. Note that the key's
2718 * stored in the actual records represent BASE+LEN, not BASE. The
2719 * first record containing bio_offset will have a key > bio_offset.
2721 cursor.key_beg.localization = ip->obj_localization +
2722 HAMMER_LOCALIZE_MISC;
2723 cursor.key_beg.obj_id = ip->obj_id;
2724 cursor.key_beg.create_tid = 0;
2725 cursor.key_beg.delete_tid = 0;
2726 cursor.key_beg.obj_type = 0;
2727 cursor.key_beg.key = bio->bio_offset + 1;
2728 cursor.asof = ip->obj_asof;
2729 cursor.flags |= HAMMER_CURSOR_ASOF;
2731 cursor.key_end = cursor.key_beg;
2732 KKASSERT(ip->ino_data.obj_type == HAMMER_OBJTYPE_REGFILE);
2734 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_DBFILE) {
2735 cursor.key_beg.rec_type = HAMMER_RECTYPE_DB;
2736 cursor.key_end.rec_type = HAMMER_RECTYPE_DB;
2737 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
2741 ran_end = bio->bio_offset + bp->b_bufsize;
2742 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
2743 cursor.key_end.rec_type = HAMMER_RECTYPE_DATA;
2744 tmp64 = ran_end + MAXPHYS + 1; /* work-around GCC-4 bug */
2745 if (tmp64 < ran_end)
2746 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
2748 cursor.key_end.key = ran_end + MAXPHYS + 1;
2750 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
2752 error = hammer_ip_first(&cursor);
2755 while (error == 0) {
2757 * Get the base file offset of the record. The key for
2758 * data records is (base + bytes) rather then (base).
2760 base = &cursor.leaf->base;
2761 rec_offset = base->key - cursor.leaf->data_len;
2764 * Calculate the gap, if any, and zero-fill it.
2766 * n is the offset of the start of the record verses our
2767 * current seek offset in the bio.
2769 n = (int)(rec_offset - (bio->bio_offset + boff));
2771 if (n > bp->b_bufsize - boff)
2772 n = bp->b_bufsize - boff;
2773 bzero((char *)bp->b_data + boff, n);
2779 * Calculate the data offset in the record and the number
2780 * of bytes we can copy.
2782 * There are two degenerate cases. First, boff may already
2783 * be at bp->b_bufsize. Secondly, the data offset within
2784 * the record may exceed the record's size.
2788 n = cursor.leaf->data_len - roff;
2790 kprintf("strategy_read: bad n=%d roff=%d\n", n, roff);
2792 } else if (n > bp->b_bufsize - boff) {
2793 n = bp->b_bufsize - boff;
2797 * Deal with cached truncations. This cool bit of code
2798 * allows truncate()/ftruncate() to avoid having to sync
2801 * If the frontend is truncated then all backend records are
2802 * subject to the frontend's truncation.
2804 * If the backend is truncated then backend records on-disk
2805 * (but not in-memory) are subject to the backend's
2806 * truncation. In-memory records owned by the backend
2807 * represent data written after the truncation point on the
2808 * backend and must not be truncated.
2810 * Truncate operations deal with frontend buffer cache
2811 * buffers and frontend-owned in-memory records synchronously.
2813 if (ip->flags & HAMMER_INODE_TRUNCATED) {
2814 if (hammer_cursor_ondisk(&cursor)/* ||
2815 cursor.iprec->flush_state == HAMMER_FST_FLUSH*/) {
2816 if (ip->trunc_off <= rec_offset)
2818 else if (ip->trunc_off < rec_offset + n)
2819 n = (int)(ip->trunc_off - rec_offset);
2822 if (ip->sync_flags & HAMMER_INODE_TRUNCATED) {
2823 if (hammer_cursor_ondisk(&cursor)) {
2824 if (ip->sync_trunc_off <= rec_offset)
2826 else if (ip->sync_trunc_off < rec_offset + n)
2827 n = (int)(ip->sync_trunc_off - rec_offset);
2832 * Try to issue a direct read into our bio if possible,
2833 * otherwise resolve the element data into a hammer_buffer
2836 * The buffer on-disk should be zerod past any real
2837 * truncation point, but may not be for any synthesized
2838 * truncation point from above.
2840 disk_offset = cursor.leaf->data_offset + roff;
2841 isdedupable = (boff == 0 && n == bp->b_bufsize &&
2842 hammer_cursor_ondisk(&cursor) &&
2843 ((int)disk_offset & HAMMER_BUFMASK) == 0);
2845 if (isdedupable && hammer_double_buffer == 0) {
2846 KKASSERT((disk_offset & HAMMER_OFF_ZONE_MASK) ==
2847 HAMMER_ZONE_LARGE_DATA);
2848 nbio->bio_offset = disk_offset;
2849 error = hammer_io_direct_read(hmp, nbio, cursor.leaf);
2850 if (hammer_live_dedup && error == 0)
2851 hammer_dedup_cache_add(ip, cursor.leaf);
2854 error = hammer_ip_resolve_data(&cursor);
2856 if (hammer_live_dedup && isdedupable)
2857 hammer_dedup_cache_add(ip, cursor.leaf);
2858 bcopy((char *)cursor.data + roff,
2859 (char *)bp->b_data + boff, n);
2866 * We have to be sure that the only elements added to the
2867 * dedup cache are those which are already on-media.
2869 if (hammer_live_dedup && hammer_cursor_ondisk(&cursor))
2870 hammer_dedup_cache_add(ip, cursor.leaf);
2873 * Iterate until we have filled the request.
2876 if (boff == bp->b_bufsize)
2878 error = hammer_ip_next(&cursor);
2882 * There may have been a gap after the last record
2884 if (error == ENOENT)
2886 if (error == 0 && boff != bp->b_bufsize) {
2887 KKASSERT(boff < bp->b_bufsize);
2888 bzero((char *)bp->b_data + boff, bp->b_bufsize - boff);
2889 /* boff = bp->b_bufsize; */
2892 bp->b_error = error;
2894 bp->b_flags |= B_ERROR;
2899 * Cache the b-tree node for the last data read in cache[1].
2901 * If we hit the file EOF then also cache the node in the
2902 * governing director's cache[3], it will be used to initialize
2903 * the inode's cache[1] for any inodes looked up via the directory.
2905 * This doesn't reduce disk accesses since the B-Tree chain is
2906 * likely cached, but it does reduce cpu overhead when looking
2907 * up file offsets for cpdup/tar/cpio style iterations.
2910 hammer_cache_node(&ip->cache[1], cursor.node);
2911 if (ran_end >= ip->ino_data.size) {
2912 dip = hammer_find_inode(&trans, ip->ino_data.parent_obj_id,
2913 ip->obj_asof, ip->obj_localization);
2915 hammer_cache_node(&dip->cache[3], cursor.node);
2916 hammer_rel_inode(dip, 0);
2919 hammer_done_cursor(&cursor);
2920 hammer_done_transaction(&trans);
2921 lwkt_reltoken(&hmp->fs_token);
2926 * BMAP operation - used to support cluster_read() only.
2928 * (struct vnode *vp, off_t loffset, off_t *doffsetp, int *runp, int *runb)
2930 * This routine may return EOPNOTSUPP if the opration is not supported for
2931 * the specified offset. The contents of the pointer arguments do not
2932 * need to be initialized in that case.
2934 * If a disk address is available and properly aligned return 0 with
2935 * *doffsetp set to the zone-2 address, and *runp / *runb set appropriately
2936 * to the run-length relative to that offset. Callers may assume that
2937 * *doffsetp is valid if 0 is returned, even if *runp is not sufficiently
2938 * large, so return EOPNOTSUPP if it is not sufficiently large.
2942 hammer_vop_bmap(struct vop_bmap_args *ap)
2944 struct hammer_transaction trans;
2945 struct hammer_inode *ip;
2947 struct hammer_cursor cursor;
2948 hammer_base_elm_t base;
2952 int64_t base_offset;
2953 int64_t base_disk_offset;
2954 int64_t last_offset;
2955 hammer_off_t last_disk_offset;
2956 hammer_off_t disk_offset;
2961 ++hammer_stats_file_iopsr;
2962 ip = ap->a_vp->v_data;
2966 * We can only BMAP regular files. We can't BMAP database files,
2969 if (ip->ino_data.obj_type != HAMMER_OBJTYPE_REGFILE)
2973 * bmap is typically called with runp/runb both NULL when used
2974 * for writing. We do not support BMAP for writing atm.
2976 if (ap->a_cmd != BUF_CMD_READ)
2980 * Scan the B-Tree to acquire blockmap addresses, then translate
2983 lwkt_gettoken(&hmp->fs_token);
2984 hammer_simple_transaction(&trans, hmp);
2986 kprintf("bmap_beg %016llx ip->cache %p\n",
2987 (long long)ap->a_loffset, ip->cache[1]);
2989 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
2992 * Key range (begin and end inclusive) to scan. Note that the key's
2993 * stored in the actual records represent BASE+LEN, not BASE. The
2994 * first record containing bio_offset will have a key > bio_offset.
2996 cursor.key_beg.localization = ip->obj_localization +
2997 HAMMER_LOCALIZE_MISC;
2998 cursor.key_beg.obj_id = ip->obj_id;
2999 cursor.key_beg.create_tid = 0;
3000 cursor.key_beg.delete_tid = 0;
3001 cursor.key_beg.obj_type = 0;
3003 cursor.key_beg.key = ap->a_loffset - MAXPHYS + 1;
3005 cursor.key_beg.key = ap->a_loffset + 1;
3006 if (cursor.key_beg.key < 0)
3007 cursor.key_beg.key = 0;
3008 cursor.asof = ip->obj_asof;
3009 cursor.flags |= HAMMER_CURSOR_ASOF;
3011 cursor.key_end = cursor.key_beg;
3012 KKASSERT(ip->ino_data.obj_type == HAMMER_OBJTYPE_REGFILE);
3014 ran_end = ap->a_loffset + MAXPHYS;
3015 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
3016 cursor.key_end.rec_type = HAMMER_RECTYPE_DATA;
3017 tmp64 = ran_end + MAXPHYS + 1; /* work-around GCC-4 bug */
3018 if (tmp64 < ran_end)
3019 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
3021 cursor.key_end.key = ran_end + MAXPHYS + 1;
3023 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
3025 error = hammer_ip_first(&cursor);
3026 base_offset = last_offset = 0;
3027 base_disk_offset = last_disk_offset = 0;
3029 while (error == 0) {
3031 * Get the base file offset of the record. The key for
3032 * data records is (base + bytes) rather then (base).
3034 * NOTE: rec_offset + rec_len may exceed the end-of-file.
3035 * The extra bytes should be zero on-disk and the BMAP op
3036 * should still be ok.
3038 base = &cursor.leaf->base;
3039 rec_offset = base->key - cursor.leaf->data_len;
3040 rec_len = cursor.leaf->data_len;
3043 * Incorporate any cached truncation.
3045 * NOTE: Modifications to rec_len based on synthesized
3046 * truncation points remove the guarantee that any extended
3047 * data on disk is zero (since the truncations may not have
3048 * taken place on-media yet).
3050 if (ip->flags & HAMMER_INODE_TRUNCATED) {
3051 if (hammer_cursor_ondisk(&cursor) ||
3052 cursor.iprec->flush_state == HAMMER_FST_FLUSH) {
3053 if (ip->trunc_off <= rec_offset)
3055 else if (ip->trunc_off < rec_offset + rec_len)
3056 rec_len = (int)(ip->trunc_off - rec_offset);
3059 if (ip->sync_flags & HAMMER_INODE_TRUNCATED) {
3060 if (hammer_cursor_ondisk(&cursor)) {
3061 if (ip->sync_trunc_off <= rec_offset)
3063 else if (ip->sync_trunc_off < rec_offset + rec_len)
3064 rec_len = (int)(ip->sync_trunc_off - rec_offset);
3069 * Accumulate information. If we have hit a discontiguous
3070 * block reset base_offset unless we are already beyond the
3071 * requested offset. If we are, that's it, we stop.
3075 if (hammer_cursor_ondisk(&cursor)) {
3076 disk_offset = cursor.leaf->data_offset;
3077 if (rec_offset != last_offset ||
3078 disk_offset != last_disk_offset) {
3079 if (rec_offset > ap->a_loffset)
3081 base_offset = rec_offset;
3082 base_disk_offset = disk_offset;
3084 last_offset = rec_offset + rec_len;
3085 last_disk_offset = disk_offset + rec_len;
3087 if (hammer_live_dedup)
3088 hammer_dedup_cache_add(ip, cursor.leaf);
3091 error = hammer_ip_next(&cursor);
3095 kprintf("BMAP %016llx: %016llx - %016llx\n",
3096 (long long)ap->a_loffset,
3097 (long long)base_offset,
3098 (long long)last_offset);
3099 kprintf("BMAP %16s: %016llx - %016llx\n", "",
3100 (long long)base_disk_offset,
3101 (long long)last_disk_offset);
3105 hammer_cache_node(&ip->cache[1], cursor.node);
3107 kprintf("bmap_end2 %016llx ip->cache %p\n",
3108 (long long)ap->a_loffset, ip->cache[1]);
3111 hammer_done_cursor(&cursor);
3112 hammer_done_transaction(&trans);
3113 lwkt_reltoken(&hmp->fs_token);
3116 * If we couldn't find any records or the records we did find were
3117 * all behind the requested offset, return failure. A forward
3118 * truncation can leave a hole w/ no on-disk records.
3120 if (last_offset == 0 || last_offset < ap->a_loffset)
3121 return (EOPNOTSUPP);
3124 * Figure out the block size at the requested offset and adjust
3125 * our limits so the cluster_read() does not create inappropriately
3126 * sized buffer cache buffers.
3128 blksize = hammer_blocksize(ap->a_loffset);
3129 if (hammer_blocksize(base_offset) != blksize) {
3130 base_offset = hammer_blockdemarc(base_offset, ap->a_loffset);
3132 if (last_offset != ap->a_loffset &&
3133 hammer_blocksize(last_offset - 1) != blksize) {
3134 last_offset = hammer_blockdemarc(ap->a_loffset,
3139 * Returning EOPNOTSUPP simply prevents the direct-IO optimization
3142 disk_offset = base_disk_offset + (ap->a_loffset - base_offset);
3144 if ((disk_offset & HAMMER_OFF_ZONE_MASK) != HAMMER_ZONE_LARGE_DATA) {
3146 * Only large-data zones can be direct-IOd
3149 } else if ((disk_offset & HAMMER_BUFMASK) ||
3150 (last_offset - ap->a_loffset) < blksize) {
3152 * doffsetp is not aligned or the forward run size does
3153 * not cover a whole buffer, disallow the direct I/O.
3160 *ap->a_doffsetp = disk_offset;
3162 *ap->a_runb = ap->a_loffset - base_offset;
3163 KKASSERT(*ap->a_runb >= 0);
3166 *ap->a_runp = last_offset - ap->a_loffset;
3167 KKASSERT(*ap->a_runp >= 0);
3175 * Write to a regular file. Because this is a strategy call the OS is
3176 * trying to actually get data onto the media.
3180 hammer_vop_strategy_write(struct vop_strategy_args *ap)
3182 hammer_record_t record;
3193 ip = ap->a_vp->v_data;
3196 blksize = hammer_blocksize(bio->bio_offset);
3197 KKASSERT(bp->b_bufsize == blksize);
3199 if (ip->flags & HAMMER_INODE_RO) {
3200 bp->b_error = EROFS;
3201 bp->b_flags |= B_ERROR;
3206 lwkt_gettoken(&hmp->fs_token);
3209 * Interlock with inode destruction (no in-kernel or directory
3210 * topology visibility). If we queue new IO while trying to
3211 * destroy the inode we can deadlock the vtrunc call in
3212 * hammer_inode_unloadable_check().
3214 * Besides, there's no point flushing a bp associated with an
3215 * inode that is being destroyed on-media and has no kernel
3218 if ((ip->flags | ip->sync_flags) &
3219 (HAMMER_INODE_DELETING|HAMMER_INODE_DELETED)) {
3222 lwkt_reltoken(&hmp->fs_token);
3227 * Reserve space and issue a direct-write from the front-end.
3228 * NOTE: The direct_io code will hammer_bread/bcopy smaller
3231 * An in-memory record will be installed to reference the storage
3232 * until the flusher can get to it.
3234 * Since we own the high level bio the front-end will not try to
3235 * do a direct-read until the write completes.
3237 * NOTE: The only time we do not reserve a full-sized buffers
3238 * worth of data is if the file is small. We do not try to
3239 * allocate a fragment (from the small-data zone) at the end of
3240 * an otherwise large file as this can lead to wildly separated
3243 KKASSERT((bio->bio_offset & HAMMER_BUFMASK) == 0);
3244 KKASSERT(bio->bio_offset < ip->ino_data.size);
3245 if (bio->bio_offset || ip->ino_data.size > HAMMER_BUFSIZE / 2)
3246 bytes = bp->b_bufsize;
3248 bytes = ((int)ip->ino_data.size + 15) & ~15;
3250 record = hammer_ip_add_bulk(ip, bio->bio_offset, bp->b_data,
3254 * B_VFSFLAG1 indicates that a REDO_WRITE entry was generated
3255 * in hammer_vop_write(). We must flag the record so the proper
3256 * REDO_TERM_WRITE entry is generated during the flush.
3259 if (bp->b_flags & B_VFSFLAG1) {
3260 record->flags |= HAMMER_RECF_REDO;
3261 bp->b_flags &= ~B_VFSFLAG1;
3263 if (record->flags & HAMMER_RECF_DEDUPED) {
3265 hammer_ip_replace_bulk(hmp, record);
3268 hammer_io_direct_write(hmp, bio, record);
3270 if (ip->rsv_recs > 1 && hmp->rsv_recs > hammer_limit_recs)
3271 hammer_flush_inode(ip, 0);
3273 bp->b_bio2.bio_offset = NOOFFSET;
3274 bp->b_error = error;
3275 bp->b_flags |= B_ERROR;
3278 lwkt_reltoken(&hmp->fs_token);
3283 * dounlink - disconnect a directory entry
3285 * XXX whiteout support not really in yet
3288 hammer_dounlink(hammer_transaction_t trans, struct nchandle *nch,
3289 struct vnode *dvp, struct ucred *cred,
3290 int flags, int isdir)
3292 struct namecache *ncp;
3296 struct hammer_cursor cursor;
3298 u_int32_t max_iterations;
3302 * Calculate the namekey and setup the key range for the scan. This
3303 * works kinda like a chained hash table where the lower 32 bits
3304 * of the namekey synthesize the chain.
3306 * The key range is inclusive of both key_beg and key_end.
3312 if (dip->flags & HAMMER_INODE_RO)
3315 namekey = hammer_directory_namekey(dip, ncp->nc_name, ncp->nc_nlen,
3318 hammer_init_cursor(trans, &cursor, &dip->cache[1], dip);
3319 cursor.key_beg.localization = dip->obj_localization +
3320 hammer_dir_localization(dip);
3321 cursor.key_beg.obj_id = dip->obj_id;
3322 cursor.key_beg.key = namekey;
3323 cursor.key_beg.create_tid = 0;
3324 cursor.key_beg.delete_tid = 0;
3325 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
3326 cursor.key_beg.obj_type = 0;
3328 cursor.key_end = cursor.key_beg;
3329 cursor.key_end.key += max_iterations;
3330 cursor.asof = dip->obj_asof;
3331 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
3334 * Scan all matching records (the chain), locate the one matching
3335 * the requested path component. info->last_error contains the
3336 * error code on search termination and could be 0, ENOENT, or
3339 * The hammer_ip_*() functions merge in-memory records with on-disk
3340 * records for the purposes of the search.
3342 error = hammer_ip_first(&cursor);
3344 while (error == 0) {
3345 error = hammer_ip_resolve_data(&cursor);
3348 nlen = cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF;
3350 if (ncp->nc_nlen == nlen &&
3351 bcmp(ncp->nc_name, cursor.data->entry.name, nlen) == 0) {
3354 error = hammer_ip_next(&cursor);
3358 * If all is ok we have to get the inode so we can adjust nlinks.
3359 * To avoid a deadlock with the flusher we must release the inode
3360 * lock on the directory when acquiring the inode for the entry.
3362 * If the target is a directory, it must be empty.
3365 hammer_unlock(&cursor.ip->lock);
3366 ip = hammer_get_inode(trans, dip, cursor.data->entry.obj_id,
3368 cursor.data->entry.localization,
3370 hammer_lock_sh(&cursor.ip->lock);
3371 if (error == ENOENT) {
3372 kprintf("HAMMER: WARNING: Removing "
3373 "dirent w/missing inode \"%s\"\n"
3374 "\tobj_id = %016llx\n",
3376 (long long)cursor.data->entry.obj_id);
3381 * If isdir >= 0 we validate that the entry is or is not a
3382 * directory. If isdir < 0 we don't care.
3384 if (error == 0 && isdir >= 0 && ip) {
3386 ip->ino_data.obj_type != HAMMER_OBJTYPE_DIRECTORY) {
3388 } else if (isdir == 0 &&
3389 ip->ino_data.obj_type == HAMMER_OBJTYPE_DIRECTORY) {
3395 * If we are trying to remove a directory the directory must
3398 * The check directory code can loop and deadlock/retry. Our
3399 * own cursor's node locks must be released to avoid a 3-way
3400 * deadlock with the flusher if the check directory code
3403 * If any changes whatsoever have been made to the cursor
3404 * set EDEADLK and retry.
3406 * WARNING: See warnings in hammer_unlock_cursor()
3409 if (error == 0 && ip && ip->ino_data.obj_type ==
3410 HAMMER_OBJTYPE_DIRECTORY) {
3411 hammer_unlock_cursor(&cursor);
3412 error = hammer_ip_check_directory_empty(trans, ip);
3413 hammer_lock_cursor(&cursor);
3414 if (cursor.flags & HAMMER_CURSOR_RETEST) {
3415 kprintf("HAMMER: Warning: avoided deadlock "
3423 * Delete the directory entry.
3425 * WARNING: hammer_ip_del_directory() may have to terminate
3426 * the cursor to avoid a deadlock. It is ok to call
3427 * hammer_done_cursor() twice.
3430 error = hammer_ip_del_directory(trans, &cursor,
3433 hammer_done_cursor(&cursor);
3435 cache_setunresolved(nch);
3436 cache_setvp(nch, NULL);
3439 * NOTE: ip->vp, if non-NULL, cannot be directly
3440 * referenced without formally acquiring the
3441 * vp since the vp might have zero refs on it,
3442 * or in the middle of a reclaim, etc.
3444 * NOTE: The cache_setunresolved() can rip the vp
3445 * out from under us since the vp may not have
3446 * any refs, in which case ip->vp will be NULL
3449 while (ip && ip->vp) {
3452 error = hammer_get_vnode(ip, &vp);
3453 if (error == 0 && vp) {
3455 hammer_knote(ip->vp, NOTE_DELETE);
3456 cache_inval_vp(ip->vp, CINV_DESTROY);
3460 kprintf("Debug: HAMMER ip/vp race1 avoided\n");
3464 hammer_rel_inode(ip, 0);
3466 hammer_done_cursor(&cursor);
3468 if (error == EDEADLK)
3474 /************************************************************************
3475 * FIFO AND SPECFS OPS *
3476 ************************************************************************
3480 hammer_vop_fifoclose (struct vop_close_args *ap)
3482 /* XXX update itimes */
3483 return (VOCALL(&fifo_vnode_vops, &ap->a_head));
3487 hammer_vop_fiforead (struct vop_read_args *ap)
3491 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
3492 /* XXX update access time */
3497 hammer_vop_fifowrite (struct vop_write_args *ap)
3501 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
3502 /* XXX update access time */
3508 hammer_vop_fifokqfilter(struct vop_kqfilter_args *ap)
3512 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
3514 error = hammer_vop_kqfilter(ap);
3518 /************************************************************************
3520 ************************************************************************
3523 static void filt_hammerdetach(struct knote *kn);
3524 static int filt_hammerread(struct knote *kn, long hint);
3525 static int filt_hammerwrite(struct knote *kn, long hint);
3526 static int filt_hammervnode(struct knote *kn, long hint);
3528 static struct filterops hammerread_filtops =
3529 { FILTEROP_ISFD, NULL, filt_hammerdetach, filt_hammerread };
3530 static struct filterops hammerwrite_filtops =
3531 { FILTEROP_ISFD, NULL, filt_hammerdetach, filt_hammerwrite };
3532 static struct filterops hammervnode_filtops =
3533 { FILTEROP_ISFD, NULL, filt_hammerdetach, filt_hammervnode };
3537 hammer_vop_kqfilter(struct vop_kqfilter_args *ap)
3539 struct vnode *vp = ap->a_vp;
3540 struct knote *kn = ap->a_kn;
3542 switch (kn->kn_filter) {
3544 kn->kn_fop = &hammerread_filtops;
3547 kn->kn_fop = &hammerwrite_filtops;
3550 kn->kn_fop = &hammervnode_filtops;
3553 return (EOPNOTSUPP);
3556 kn->kn_hook = (caddr_t)vp;
3558 knote_insert(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
3564 filt_hammerdetach(struct knote *kn)
3566 struct vnode *vp = (void *)kn->kn_hook;
3568 knote_remove(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
3572 filt_hammerread(struct knote *kn, long hint)
3574 struct vnode *vp = (void *)kn->kn_hook;
3575 hammer_inode_t ip = VTOI(vp);
3576 hammer_mount_t hmp = ip->hmp;
3579 if (hint == NOTE_REVOKE) {
3580 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
3583 lwkt_gettoken(&hmp->fs_token); /* XXX use per-ip-token */
3584 off = ip->ino_data.size - kn->kn_fp->f_offset;
3585 kn->kn_data = (off < INTPTR_MAX) ? off : INTPTR_MAX;
3586 lwkt_reltoken(&hmp->fs_token);
3587 if (kn->kn_sfflags & NOTE_OLDAPI)
3589 return (kn->kn_data != 0);
3593 filt_hammerwrite(struct knote *kn, long hint)
3595 if (hint == NOTE_REVOKE)
3596 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
3602 filt_hammervnode(struct knote *kn, long hint)
3604 if (kn->kn_sfflags & hint)
3605 kn->kn_fflags |= hint;
3606 if (hint == NOTE_REVOKE) {
3607 kn->kn_flags |= EV_EOF;
3610 return (kn->kn_fflags != 0);