2 * Copyright (c) 2007-2008 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/mountctl.h>
36 #include <sys/namecache.h>
38 #include <vfs/fifofs/fifo.h>
45 static int hammer_vop_fsync(struct vop_fsync_args *);
46 static int hammer_vop_read(struct vop_read_args *);
47 static int hammer_vop_write(struct vop_write_args *);
48 static int hammer_vop_access(struct vop_access_args *);
49 static int hammer_vop_advlock(struct vop_advlock_args *);
50 static int hammer_vop_close(struct vop_close_args *);
51 static int hammer_vop_ncreate(struct vop_ncreate_args *);
52 static int hammer_vop_getattr(struct vop_getattr_args *);
53 static int hammer_vop_nresolve(struct vop_nresolve_args *);
54 static int hammer_vop_nlookupdotdot(struct vop_nlookupdotdot_args *);
55 static int hammer_vop_nlink(struct vop_nlink_args *);
56 static int hammer_vop_nmkdir(struct vop_nmkdir_args *);
57 static int hammer_vop_nmknod(struct vop_nmknod_args *);
58 static int hammer_vop_open(struct vop_open_args *);
59 static int hammer_vop_print(struct vop_print_args *);
60 static int hammer_vop_readdir(struct vop_readdir_args *);
61 static int hammer_vop_readlink(struct vop_readlink_args *);
62 static int hammer_vop_nremove(struct vop_nremove_args *);
63 static int hammer_vop_nrename(struct vop_nrename_args *);
64 static int hammer_vop_nrmdir(struct vop_nrmdir_args *);
65 static int hammer_vop_markatime(struct vop_markatime_args *);
66 static int hammer_vop_setattr(struct vop_setattr_args *);
67 static int hammer_vop_strategy(struct vop_strategy_args *);
68 static int hammer_vop_bmap(struct vop_bmap_args *ap);
69 static int hammer_vop_nsymlink(struct vop_nsymlink_args *);
70 static int hammer_vop_nwhiteout(struct vop_nwhiteout_args *);
71 static int hammer_vop_ioctl(struct vop_ioctl_args *);
72 static int hammer_vop_mountctl(struct vop_mountctl_args *);
73 static int hammer_vop_kqfilter (struct vop_kqfilter_args *);
75 static int hammer_vop_fifoclose (struct vop_close_args *);
76 static int hammer_vop_fiforead (struct vop_read_args *);
77 static int hammer_vop_fifowrite (struct vop_write_args *);
78 static int hammer_vop_fifokqfilter (struct vop_kqfilter_args *);
80 struct vop_ops hammer_vnode_vops = {
81 .vop_default = vop_defaultop,
82 .vop_fsync = hammer_vop_fsync,
83 .vop_getpages = vop_stdgetpages,
84 .vop_putpages = vop_stdputpages,
85 .vop_read = hammer_vop_read,
86 .vop_write = hammer_vop_write,
87 .vop_access = hammer_vop_access,
88 .vop_advlock = hammer_vop_advlock,
89 .vop_close = hammer_vop_close,
90 .vop_ncreate = hammer_vop_ncreate,
91 .vop_getattr = hammer_vop_getattr,
92 .vop_inactive = hammer_vop_inactive,
93 .vop_reclaim = hammer_vop_reclaim,
94 .vop_nresolve = hammer_vop_nresolve,
95 .vop_nlookupdotdot = hammer_vop_nlookupdotdot,
96 .vop_nlink = hammer_vop_nlink,
97 .vop_nmkdir = hammer_vop_nmkdir,
98 .vop_nmknod = hammer_vop_nmknod,
99 .vop_open = hammer_vop_open,
100 .vop_pathconf = vop_stdpathconf,
101 .vop_print = hammer_vop_print,
102 .vop_readdir = hammer_vop_readdir,
103 .vop_readlink = hammer_vop_readlink,
104 .vop_nremove = hammer_vop_nremove,
105 .vop_nrename = hammer_vop_nrename,
106 .vop_nrmdir = hammer_vop_nrmdir,
107 .vop_markatime = hammer_vop_markatime,
108 .vop_setattr = hammer_vop_setattr,
109 .vop_bmap = hammer_vop_bmap,
110 .vop_strategy = hammer_vop_strategy,
111 .vop_nsymlink = hammer_vop_nsymlink,
112 .vop_nwhiteout = hammer_vop_nwhiteout,
113 .vop_ioctl = hammer_vop_ioctl,
114 .vop_mountctl = hammer_vop_mountctl,
115 .vop_kqfilter = hammer_vop_kqfilter
118 struct vop_ops hammer_spec_vops = {
119 .vop_default = vop_defaultop,
120 .vop_fsync = hammer_vop_fsync,
121 .vop_read = vop_stdnoread,
122 .vop_write = vop_stdnowrite,
123 .vop_access = hammer_vop_access,
124 .vop_close = hammer_vop_close,
125 .vop_markatime = hammer_vop_markatime,
126 .vop_getattr = hammer_vop_getattr,
127 .vop_inactive = hammer_vop_inactive,
128 .vop_reclaim = hammer_vop_reclaim,
129 .vop_setattr = hammer_vop_setattr
132 struct vop_ops hammer_fifo_vops = {
133 .vop_default = fifo_vnoperate,
134 .vop_fsync = hammer_vop_fsync,
135 .vop_read = hammer_vop_fiforead,
136 .vop_write = hammer_vop_fifowrite,
137 .vop_access = hammer_vop_access,
138 .vop_close = hammer_vop_fifoclose,
139 .vop_markatime = hammer_vop_markatime,
140 .vop_getattr = hammer_vop_getattr,
141 .vop_inactive = hammer_vop_inactive,
142 .vop_reclaim = hammer_vop_reclaim,
143 .vop_setattr = hammer_vop_setattr,
144 .vop_kqfilter = hammer_vop_fifokqfilter
149 hammer_knote(struct vnode *vp, int flags)
152 KNOTE(&vp->v_pollinfo.vpi_kqinfo.ki_note, flags);
155 #ifdef DEBUG_TRUNCATE
156 struct hammer_inode *HammerTruncIp;
159 static int hammer_dounlink(hammer_transaction_t trans, struct nchandle *nch,
160 struct vnode *dvp, struct ucred *cred,
161 int flags, int isdir);
162 static int hammer_vop_strategy_read(struct vop_strategy_args *ap);
163 static int hammer_vop_strategy_write(struct vop_strategy_args *ap);
166 * hammer_vop_fsync { vp, waitfor }
168 * fsync() an inode to disk and wait for it to be completely committed
169 * such that the information would not be undone if a crash occured after
172 * NOTE: HAMMER's fsync()'s are going to remain expensive until we implement
173 * a REDO log. A sysctl is provided to relax HAMMER's fsync()
176 * Ultimately the combination of a REDO log and use of fast storage
177 * to front-end cluster caches will make fsync fast, but it aint
178 * here yet. And, in anycase, we need real transactional
179 * all-or-nothing features which are not restricted to a single file.
183 hammer_vop_fsync(struct vop_fsync_args *ap)
185 hammer_inode_t ip = VTOI(ap->a_vp);
186 hammer_mount_t hmp = ip->hmp;
187 int waitfor = ap->a_waitfor;
190 lwkt_gettoken(&hmp->fs_token);
193 * Fsync rule relaxation (default is either full synchronous flush
194 * or REDO semantics with synchronous flush).
196 if (ap->a_flags & VOP_FSYNC_SYSCALL) {
197 switch(hammer_fsync_mode) {
200 /* no REDO, full synchronous flush */
204 /* no REDO, full asynchronous flush */
205 if (waitfor == MNT_WAIT)
206 waitfor = MNT_NOWAIT;
209 /* REDO semantics, synchronous flush */
210 if (hmp->version < HAMMER_VOL_VERSION_FOUR)
212 mode = HAMMER_FLUSH_UNDOS_AUTO;
215 /* REDO semantics, relaxed asynchronous flush */
216 if (hmp->version < HAMMER_VOL_VERSION_FOUR)
218 mode = HAMMER_FLUSH_UNDOS_RELAXED;
219 if (waitfor == MNT_WAIT)
220 waitfor = MNT_NOWAIT;
223 /* ignore the fsync() system call */
224 lwkt_reltoken(&hmp->fs_token);
227 /* we have to do something */
228 mode = HAMMER_FLUSH_UNDOS_RELAXED;
229 if (waitfor == MNT_WAIT)
230 waitfor = MNT_NOWAIT;
235 * Fast fsync only needs to flush the UNDO/REDO fifo if
236 * HAMMER_INODE_REDO is non-zero and the only modifications
237 * made to the file are write or write-extends.
239 if ((ip->flags & HAMMER_INODE_REDO) &&
240 (ip->flags & HAMMER_INODE_MODMASK_NOREDO) == 0) {
241 ++hammer_count_fsyncs;
242 hammer_flusher_flush_undos(hmp, mode);
244 if (ip->vp && (ip->flags & HAMMER_INODE_MODMASK) == 0)
246 lwkt_reltoken(&hmp->fs_token);
251 * REDO is enabled by fsync(), the idea being we really only
252 * want to lay down REDO records when programs are using
253 * fsync() heavily. The first fsync() on the file starts
254 * the gravy train going and later fsync()s keep it hot by
255 * resetting the redo_count.
257 * We weren't running REDOs before now so we have to fall
258 * through and do a full fsync of what we have.
260 if (hmp->version >= HAMMER_VOL_VERSION_FOUR &&
261 (hmp->flags & HAMMER_MOUNT_REDO_RECOVERY_RUN) == 0) {
262 ip->flags |= HAMMER_INODE_REDO;
269 * Do a full flush sequence.
271 * Attempt to release the vnode while waiting for the inode to
272 * finish flushing. This can really mess up inactive->reclaim
273 * sequences so only do it if the vnode is active.
275 * WARNING! The VX lock functions must be used. vn_lock() will
276 * fail when this is part of a VOP_RECLAIM sequence.
278 ++hammer_count_fsyncs;
279 vfsync(ap->a_vp, waitfor, 1, NULL, NULL);
280 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
281 if (waitfor == MNT_WAIT) {
284 if ((ap->a_vp->v_flag & VRECLAIMED) == 0) {
290 hammer_wait_inode(ip);
294 if (ip->vp && (ip->flags & HAMMER_INODE_MODMASK) == 0)
296 lwkt_reltoken(&hmp->fs_token);
301 * hammer_vop_read { vp, uio, ioflag, cred }
303 * MPSAFE (for the cache safe does not require fs_token)
307 hammer_vop_read(struct vop_read_args *ap)
309 struct hammer_transaction trans;
324 if (ap->a_vp->v_type != VREG)
333 * Attempt to shortcut directly to the VM object using lwbufs.
334 * This is much faster than instantiating buffer cache buffers.
336 resid = uio->uio_resid;
337 error = vop_helper_read_shortcut(ap);
338 hammer_stats_file_read += resid - uio->uio_resid;
341 if (uio->uio_resid == 0)
345 * Allow the UIO's size to override the sequential heuristic.
347 blksize = hammer_blocksize(uio->uio_offset);
348 seqcount = (uio->uio_resid + (BKVASIZE - 1)) / BKVASIZE;
349 ioseqcount = (ap->a_ioflag >> 16);
350 if (seqcount < ioseqcount)
351 seqcount = ioseqcount;
354 * If reading or writing a huge amount of data we have to break
355 * atomicy and allow the operation to be interrupted by a signal
356 * or it can DOS the machine.
358 bigread = (uio->uio_resid > 100 * 1024 * 1024);
361 * Access the data typically in HAMMER_BUFSIZE blocks via the
362 * buffer cache, but HAMMER may use a variable block size based
365 * XXX Temporary hack, delay the start transaction while we remain
366 * MPSAFE. NOTE: ino_data.size cannot change while vnode is
369 while (uio->uio_resid > 0 && uio->uio_offset < ip->ino_data.size) {
373 blksize = hammer_blocksize(uio->uio_offset);
374 offset = (int)uio->uio_offset & (blksize - 1);
375 base_offset = uio->uio_offset - offset;
377 if (bigread && (error = hammer_signal_check(ip->hmp)) != 0)
383 bp = getblk(ap->a_vp, base_offset, blksize, 0, 0);
384 if ((bp->b_flags & (B_INVAL | B_CACHE | B_RAM)) == B_CACHE) {
385 bp->b_flags &= ~B_AGE;
389 if (ap->a_ioflag & IO_NRDELAY) {
391 return (EWOULDBLOCK);
397 if (got_trans == 0) {
398 hammer_start_transaction(&trans, ip->hmp);
403 * NOTE: A valid bp has already been acquired, but was not
406 if (hammer_cluster_enable) {
408 * Use file_limit to prevent cluster_read() from
409 * creating buffers of the wrong block size past
412 file_limit = ip->ino_data.size;
413 if (base_offset < HAMMER_XDEMARC &&
414 file_limit > HAMMER_XDEMARC) {
415 file_limit = HAMMER_XDEMARC;
417 error = cluster_readx(ap->a_vp,
418 file_limit, base_offset,
419 blksize, uio->uio_resid,
420 seqcount * BKVASIZE, &bp);
422 error = breadnx(ap->a_vp, base_offset, blksize,
430 if ((hammer_debug_io & 0x0001) && (bp->b_flags & B_IODEBUG)) {
431 kprintf("doff %016jx read file %016jx@%016jx\n",
432 (intmax_t)bp->b_bio2.bio_offset,
433 (intmax_t)ip->obj_id,
434 (intmax_t)bp->b_loffset);
436 bp->b_flags &= ~B_IODEBUG;
437 if (blksize == HAMMER_XBUFSIZE)
438 bp->b_flags |= B_CLUSTEROK;
440 n = blksize - offset;
441 if (n > uio->uio_resid)
443 if (n > ip->ino_data.size - uio->uio_offset)
444 n = (int)(ip->ino_data.size - uio->uio_offset);
447 * Set B_AGE, data has a lower priority than meta-data.
449 * Use a hold/unlock/drop sequence to run the uiomove
450 * with the buffer unlocked, avoiding deadlocks against
451 * read()s on mmap()'d spaces.
453 bp->b_flags |= B_AGE;
454 error = uiomovebp(bp, (char *)bp->b_data + offset, n, uio);
459 hammer_stats_file_read += n;
465 * Try to update the atime with just the inode lock for maximum
466 * concurrency. If we can't shortcut it we have to get the full
469 if (got_trans == 0 && hammer_update_atime_quick(ip) < 0) {
470 hammer_start_transaction(&trans, ip->hmp);
475 if ((ip->flags & HAMMER_INODE_RO) == 0 &&
476 (ip->hmp->mp->mnt_flag & MNT_NOATIME) == 0) {
477 lwkt_gettoken(&hmp->fs_token);
478 ip->ino_data.atime = trans.time;
479 hammer_modify_inode(&trans, ip, HAMMER_INODE_ATIME);
480 hammer_done_transaction(&trans);
481 lwkt_reltoken(&hmp->fs_token);
483 hammer_done_transaction(&trans);
490 * hammer_vop_write { vp, uio, ioflag, cred }
494 hammer_vop_write(struct vop_write_args *ap)
496 struct hammer_transaction trans;
497 struct hammer_inode *ip;
512 if (ap->a_vp->v_type != VREG)
518 seqcount = ap->a_ioflag >> 16;
520 if (ip->flags & HAMMER_INODE_RO)
524 * Create a transaction to cover the operations we perform.
526 hammer_start_transaction(&trans, hmp);
532 if (ap->a_ioflag & IO_APPEND)
533 uio->uio_offset = ip->ino_data.size;
536 * Check for illegal write offsets. Valid range is 0...2^63-1.
538 * NOTE: the base_off assignment is required to work around what
539 * I consider to be a GCC-4 optimization bug.
541 if (uio->uio_offset < 0) {
542 hammer_done_transaction(&trans);
545 base_offset = uio->uio_offset + uio->uio_resid; /* work around gcc-4 */
546 if (uio->uio_resid > 0 && base_offset <= uio->uio_offset) {
547 hammer_done_transaction(&trans);
551 if (uio->uio_resid > 0 && (td = uio->uio_td) != NULL && td->td_proc &&
552 base_offset > td->td_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
553 hammer_done_transaction(&trans);
554 lwpsignal(td->td_proc, td->td_lwp, SIGXFSZ);
559 * If reading or writing a huge amount of data we have to break
560 * atomicy and allow the operation to be interrupted by a signal
561 * or it can DOS the machine.
563 * Preset redo_count so we stop generating REDOs earlier if the
566 * redo_count is heuristical, SMP races are ok
568 bigwrite = (uio->uio_resid > 100 * 1024 * 1024);
569 if ((ip->flags & HAMMER_INODE_REDO) &&
570 ip->redo_count < hammer_limit_redo) {
571 ip->redo_count += uio->uio_resid;
575 * Access the data typically in HAMMER_BUFSIZE blocks via the
576 * buffer cache, but HAMMER may use a variable block size based
579 while (uio->uio_resid > 0) {
587 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_WRITE)) != 0)
589 if (bigwrite && (error = hammer_signal_check(hmp)) != 0)
592 blksize = hammer_blocksize(uio->uio_offset);
595 * Control the number of pending records associated with
596 * this inode. If too many have accumulated start a
597 * flush. Try to maintain a pipeline with the flusher.
599 * NOTE: It is possible for other sources to grow the
600 * records but not necessarily issue another flush,
601 * so use a timeout and ensure that a re-flush occurs.
603 if (ip->rsv_recs >= hammer_limit_inode_recs) {
604 lwkt_gettoken(&hmp->fs_token);
605 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
606 while (ip->rsv_recs >= hammer_limit_inode_recs * 2) {
607 ip->flags |= HAMMER_INODE_RECSW;
608 tsleep(&ip->rsv_recs, 0, "hmrwww", hz);
609 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
611 lwkt_reltoken(&hmp->fs_token);
615 * Do not allow HAMMER to blow out the buffer cache. Very
616 * large UIOs can lockout other processes due to bwillwrite()
619 * The hammer inode is not locked during these operations.
620 * The vnode is locked which can interfere with the pageout
621 * daemon for non-UIO_NOCOPY writes but should not interfere
622 * with the buffer cache. Even so, we cannot afford to
623 * allow the pageout daemon to build up too many dirty buffer
626 * Only call this if we aren't being recursively called from
627 * a virtual disk device (vn), else we may deadlock.
629 if ((ap->a_ioflag & IO_RECURSE) == 0)
633 * Calculate the blocksize at the current offset and figure
634 * out how much we can actually write.
636 blkmask = blksize - 1;
637 offset = (int)uio->uio_offset & blkmask;
638 base_offset = uio->uio_offset & ~(int64_t)blkmask;
639 n = blksize - offset;
640 if (n > uio->uio_resid) {
646 nsize = uio->uio_offset + n;
647 if (nsize > ip->ino_data.size) {
648 if (uio->uio_offset > ip->ino_data.size)
652 nvextendbuf(ap->a_vp,
655 hammer_blocksize(ip->ino_data.size),
656 hammer_blocksize(nsize),
657 hammer_blockoff(ip->ino_data.size),
658 hammer_blockoff(nsize),
661 kflags |= NOTE_EXTEND;
664 if (uio->uio_segflg == UIO_NOCOPY) {
666 * Issuing a write with the same data backing the
667 * buffer. Instantiate the buffer to collect the
668 * backing vm pages, then read-in any missing bits.
670 * This case is used by vop_stdputpages().
672 bp = getblk(ap->a_vp, base_offset,
673 blksize, GETBLK_BHEAVY, 0);
674 if ((bp->b_flags & B_CACHE) == 0) {
676 error = bread(ap->a_vp, base_offset,
679 } else if (offset == 0 && uio->uio_resid >= blksize) {
681 * Even though we are entirely overwriting the buffer
682 * we may still have to zero it out to avoid a
683 * mmap/write visibility issue.
685 bp = getblk(ap->a_vp, base_offset, blksize, GETBLK_BHEAVY, 0);
686 if ((bp->b_flags & B_CACHE) == 0)
688 } else if (base_offset >= ip->ino_data.size) {
690 * If the base offset of the buffer is beyond the
691 * file EOF, we don't have to issue a read.
693 bp = getblk(ap->a_vp, base_offset,
694 blksize, GETBLK_BHEAVY, 0);
698 * Partial overwrite, read in any missing bits then
699 * replace the portion being written.
701 error = bread(ap->a_vp, base_offset, blksize, &bp);
706 error = uiomovebp(bp, bp->b_data + offset, n, uio);
708 lwkt_gettoken(&hmp->fs_token);
711 * Generate REDO records if enabled and redo_count will not
712 * exceeded the limit.
714 * If redo_count exceeds the limit we stop generating records
715 * and clear HAMMER_INODE_REDO. This will cause the next
716 * fsync() to do a full meta-data sync instead of just an
717 * UNDO/REDO fifo update.
719 * When clearing HAMMER_INODE_REDO any pre-existing REDOs
720 * will still be tracked. The tracks will be terminated
721 * when the related meta-data (including possible data
722 * modifications which are not tracked via REDO) is
725 if ((ip->flags & HAMMER_INODE_REDO) && error == 0) {
726 if (ip->redo_count < hammer_limit_redo) {
727 bp->b_flags |= B_VFSFLAG1;
728 error = hammer_generate_redo(&trans, ip,
729 base_offset + offset,
734 ip->flags &= ~HAMMER_INODE_REDO;
739 * If we screwed up we have to undo any VM size changes we
745 nvtruncbuf(ap->a_vp, ip->ino_data.size,
746 hammer_blocksize(ip->ino_data.size),
747 hammer_blockoff(ip->ino_data.size),
750 lwkt_reltoken(&hmp->fs_token);
753 kflags |= NOTE_WRITE;
754 hammer_stats_file_write += n;
755 if (blksize == HAMMER_XBUFSIZE)
756 bp->b_flags |= B_CLUSTEROK;
757 if (ip->ino_data.size < uio->uio_offset) {
758 ip->ino_data.size = uio->uio_offset;
759 flags = HAMMER_INODE_SDIRTY;
763 ip->ino_data.mtime = trans.time;
764 flags |= HAMMER_INODE_MTIME | HAMMER_INODE_BUFS;
765 hammer_modify_inode(&trans, ip, flags);
768 * Once we dirty the buffer any cached zone-X offset
769 * becomes invalid. HAMMER NOTE: no-history mode cannot
770 * allow overwriting over the same data sector unless
771 * we provide UNDOs for the old data, which we don't.
773 bp->b_bio2.bio_offset = NOOFFSET;
775 lwkt_reltoken(&hmp->fs_token);
778 * Final buffer disposition.
780 * Because meta-data updates are deferred, HAMMER is
781 * especially sensitive to excessive bdwrite()s because
782 * the I/O stream is not broken up by disk reads. So the
783 * buffer cache simply cannot keep up.
785 * WARNING! blksize is variable. cluster_write() is
786 * expected to not blow up if it encounters
787 * buffers that do not match the passed blksize.
789 * NOTE! Hammer shouldn't need to bawrite()/cluster_write().
790 * The ip->rsv_recs check should burst-flush the data.
791 * If we queue it immediately the buf could be left
792 * locked on the device queue for a very long time.
794 * However, failing to flush a dirty buffer out when
795 * issued from the pageout daemon can result in a low
796 * memory deadlock against bio_page_alloc(), so we
797 * have to bawrite() on IO_ASYNC as well.
799 * NOTE! To avoid degenerate stalls due to mismatched block
800 * sizes we only honor IO_DIRECT on the write which
801 * abuts the end of the buffer. However, we must
802 * honor IO_SYNC in case someone is silly enough to
803 * configure a HAMMER file as swap, or when HAMMER
804 * is serving NFS (for commits). Ick ick.
806 bp->b_flags |= B_AGE;
807 if (blksize == HAMMER_XBUFSIZE)
808 bp->b_flags |= B_CLUSTEROK;
810 if (ap->a_ioflag & IO_SYNC) {
812 } else if ((ap->a_ioflag & IO_DIRECT) && endofblk) {
814 } else if (ap->a_ioflag & IO_ASYNC) {
816 } else if (hammer_cluster_enable &&
817 !(ap->a_vp->v_mount->mnt_flag & MNT_NOCLUSTERW)) {
818 if (base_offset < HAMMER_XDEMARC)
819 cluster_eof = hammer_blockdemarc(base_offset,
822 cluster_eof = ip->ino_data.size;
823 cluster_write(bp, cluster_eof, blksize, seqcount);
828 hammer_done_transaction(&trans);
829 hammer_knote(ap->a_vp, kflags);
835 * hammer_vop_access { vp, mode, cred }
837 * MPSAFE - does not require fs_token
841 hammer_vop_access(struct vop_access_args *ap)
843 struct hammer_inode *ip = VTOI(ap->a_vp);
848 ++hammer_stats_file_iopsr;
849 uid = hammer_to_unix_xid(&ip->ino_data.uid);
850 gid = hammer_to_unix_xid(&ip->ino_data.gid);
852 error = vop_helper_access(ap, uid, gid, ip->ino_data.mode,
853 ip->ino_data.uflags);
858 * hammer_vop_advlock { vp, id, op, fl, flags }
860 * MPSAFE - does not require fs_token
864 hammer_vop_advlock(struct vop_advlock_args *ap)
866 hammer_inode_t ip = VTOI(ap->a_vp);
868 return (lf_advlock(ap, &ip->advlock, ip->ino_data.size));
872 * hammer_vop_close { vp, fflag }
874 * We can only sync-on-close for normal closes. XXX disabled for now.
878 hammer_vop_close(struct vop_close_args *ap)
881 struct vnode *vp = ap->a_vp;
882 hammer_inode_t ip = VTOI(vp);
884 if (ip->flags & (HAMMER_INODE_CLOSESYNC|HAMMER_INODE_CLOSEASYNC)) {
885 if (vn_islocked(vp) == LK_EXCLUSIVE &&
886 (vp->v_flag & (VINACTIVE|VRECLAIMED)) == 0) {
887 if (ip->flags & HAMMER_INODE_CLOSESYNC)
890 waitfor = MNT_NOWAIT;
891 ip->flags &= ~(HAMMER_INODE_CLOSESYNC |
892 HAMMER_INODE_CLOSEASYNC);
893 VOP_FSYNC(vp, MNT_NOWAIT, waitfor);
897 return (vop_stdclose(ap));
901 * hammer_vop_ncreate { nch, dvp, vpp, cred, vap }
903 * The operating system has already ensured that the directory entry
904 * does not exist and done all appropriate namespace locking.
908 hammer_vop_ncreate(struct vop_ncreate_args *ap)
910 struct hammer_transaction trans;
911 struct hammer_inode *dip;
912 struct hammer_inode *nip;
913 struct nchandle *nch;
918 dip = VTOI(ap->a_dvp);
921 if (dip->flags & HAMMER_INODE_RO)
923 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0)
927 * Create a transaction to cover the operations we perform.
929 lwkt_gettoken(&hmp->fs_token);
930 hammer_start_transaction(&trans, hmp);
931 ++hammer_stats_file_iopsw;
934 * Create a new filesystem object of the requested type. The
935 * returned inode will be referenced and shared-locked to prevent
936 * it from being moved to the flusher.
938 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
939 dip, nch->ncp->nc_name, nch->ncp->nc_nlen,
942 hkprintf("hammer_create_inode error %d\n", error);
943 hammer_done_transaction(&trans);
945 lwkt_reltoken(&hmp->fs_token);
950 * Add the new filesystem object to the directory. This will also
951 * bump the inode's link count.
953 error = hammer_ip_add_directory(&trans, dip,
954 nch->ncp->nc_name, nch->ncp->nc_nlen,
957 hkprintf("hammer_ip_add_directory error %d\n", error);
963 hammer_rel_inode(nip, 0);
964 hammer_done_transaction(&trans);
967 error = hammer_get_vnode(nip, ap->a_vpp);
968 hammer_done_transaction(&trans);
969 hammer_rel_inode(nip, 0);
971 cache_setunresolved(ap->a_nch);
972 cache_setvp(ap->a_nch, *ap->a_vpp);
974 hammer_knote(ap->a_dvp, NOTE_WRITE);
976 lwkt_reltoken(&hmp->fs_token);
981 * hammer_vop_getattr { vp, vap }
983 * Retrieve an inode's attribute information. When accessing inodes
984 * historically we fake the atime field to ensure consistent results.
985 * The atime field is stored in the B-Tree element and allowed to be
986 * updated without cycling the element.
988 * MPSAFE - does not require fs_token
992 hammer_vop_getattr(struct vop_getattr_args *ap)
994 struct hammer_inode *ip = VTOI(ap->a_vp);
995 struct vattr *vap = ap->a_vap;
998 * We want the fsid to be different when accessing a filesystem
999 * with different as-of's so programs like diff don't think
1000 * the files are the same.
1002 * We also want the fsid to be the same when comparing snapshots,
1003 * or when comparing mirrors (which might be backed by different
1004 * physical devices). HAMMER fsids are based on the PFS's
1005 * shared_uuid field.
1007 * XXX there is a chance of collision here. The va_fsid reported
1008 * by stat is different from the more involved fsid used in the
1011 ++hammer_stats_file_iopsr;
1012 hammer_lock_sh(&ip->lock);
1013 vap->va_fsid = ip->pfsm->fsid_udev ^ (u_int32_t)ip->obj_asof ^
1014 (u_int32_t)(ip->obj_asof >> 32);
1016 vap->va_fileid = ip->ino_leaf.base.obj_id;
1017 vap->va_mode = ip->ino_data.mode;
1018 vap->va_nlink = ip->ino_data.nlinks;
1019 vap->va_uid = hammer_to_unix_xid(&ip->ino_data.uid);
1020 vap->va_gid = hammer_to_unix_xid(&ip->ino_data.gid);
1023 vap->va_size = ip->ino_data.size;
1026 * Special case for @@PFS softlinks. The actual size of the
1027 * expanded softlink is "@@0x%016llx:%05d" == 26 bytes.
1028 * or for MAX_TID is "@@-1:%05d" == 10 bytes.
1030 * Note that userspace hammer command does not allow users to
1031 * create a @@PFS softlink under an existing other PFS (id!=0)
1032 * so the ip localization here for @@PFS softlink is always 0.
1034 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_SOFTLINK &&
1035 ip->ino_data.size == 10 &&
1036 ip->obj_asof == HAMMER_MAX_TID &&
1037 ip->obj_localization == 0 &&
1038 strncmp(ip->ino_data.ext.symlink, "@@PFS", 5) == 0) {
1039 if (ip->pfsm->pfsd.mirror_flags & HAMMER_PFSD_SLAVE)
1046 * We must provide a consistent atime and mtime for snapshots
1047 * so people can do a 'tar cf - ... | md5' on them and get
1048 * consistent results.
1050 if (ip->flags & HAMMER_INODE_RO) {
1051 hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_atime);
1052 hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_mtime);
1054 hammer_time_to_timespec(ip->ino_data.atime, &vap->va_atime);
1055 hammer_time_to_timespec(ip->ino_data.mtime, &vap->va_mtime);
1057 hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_ctime);
1058 vap->va_flags = ip->ino_data.uflags;
1059 vap->va_gen = 1; /* hammer inums are unique for all time */
1060 vap->va_blocksize = HAMMER_BUFSIZE;
1061 if (ip->ino_data.size >= HAMMER_XDEMARC) {
1062 vap->va_bytes = (ip->ino_data.size + HAMMER_XBUFMASK64) &
1064 } else if (ip->ino_data.size > HAMMER_HBUFSIZE) {
1065 vap->va_bytes = (ip->ino_data.size + HAMMER_BUFMASK64) &
1068 vap->va_bytes = (ip->ino_data.size + 15) & ~15;
1071 vap->va_type = hammer_get_vnode_type(ip->ino_data.obj_type);
1072 vap->va_filerev = 0; /* XXX */
1073 vap->va_uid_uuid = ip->ino_data.uid;
1074 vap->va_gid_uuid = ip->ino_data.gid;
1075 vap->va_fsid_uuid = ip->hmp->fsid;
1076 vap->va_vaflags = VA_UID_UUID_VALID | VA_GID_UUID_VALID |
1079 switch (ip->ino_data.obj_type) {
1080 case HAMMER_OBJTYPE_CDEV:
1081 case HAMMER_OBJTYPE_BDEV:
1082 vap->va_rmajor = ip->ino_data.rmajor;
1083 vap->va_rminor = ip->ino_data.rminor;
1088 hammer_unlock(&ip->lock);
1093 * hammer_vop_nresolve { nch, dvp, cred }
1095 * Locate the requested directory entry.
1099 hammer_vop_nresolve(struct vop_nresolve_args *ap)
1101 struct hammer_transaction trans;
1102 struct namecache *ncp;
1107 struct hammer_cursor cursor;
1116 u_int32_t localization;
1117 u_int32_t max_iterations;
1120 * Misc initialization, plus handle as-of name extensions. Look for
1121 * the '@@' extension. Note that as-of files and directories cannot
1124 dip = VTOI(ap->a_dvp);
1125 ncp = ap->a_nch->ncp;
1126 asof = dip->obj_asof;
1127 localization = dip->obj_localization; /* for code consistency */
1128 nlen = ncp->nc_nlen;
1129 flags = dip->flags & HAMMER_INODE_RO;
1133 lwkt_gettoken(&hmp->fs_token);
1134 hammer_simple_transaction(&trans, hmp);
1135 ++hammer_stats_file_iopsr;
1137 for (i = 0; i < nlen; ++i) {
1138 if (ncp->nc_name[i] == '@' && ncp->nc_name[i+1] == '@') {
1139 error = hammer_str_to_tid(ncp->nc_name + i + 2,
1140 &ispfs, &asof, &localization);
1145 if (asof != HAMMER_MAX_TID)
1146 flags |= HAMMER_INODE_RO;
1153 * If this is a PFS softlink we dive into the PFS
1155 if (ispfs && nlen == 0) {
1156 ip = hammer_get_inode(&trans, dip, HAMMER_OBJID_ROOT,
1160 error = hammer_get_vnode(ip, &vp);
1161 hammer_rel_inode(ip, 0);
1167 cache_setvp(ap->a_nch, vp);
1174 * If there is no path component the time extension is relative to dip.
1175 * e.g. "fubar/@@<snapshot>"
1177 * "." is handled by the kernel, but ".@@<snapshot>" is not.
1178 * e.g. "fubar/.@@<snapshot>"
1180 * ".." is handled by the kernel. We do not currently handle
1183 if (nlen == 0 || (nlen == 1 && ncp->nc_name[0] == '.')) {
1184 ip = hammer_get_inode(&trans, dip, dip->obj_id,
1185 asof, dip->obj_localization,
1188 error = hammer_get_vnode(ip, &vp);
1189 hammer_rel_inode(ip, 0);
1195 cache_setvp(ap->a_nch, vp);
1202 * Calculate the namekey and setup the key range for the scan. This
1203 * works kinda like a chained hash table where the lower 32 bits
1204 * of the namekey synthesize the chain.
1206 * The key range is inclusive of both key_beg and key_end.
1208 namekey = hammer_directory_namekey(dip, ncp->nc_name, nlen,
1211 error = hammer_init_cursor(&trans, &cursor, &dip->cache[1], dip);
1212 cursor.key_beg.localization = dip->obj_localization +
1213 hammer_dir_localization(dip);
1214 cursor.key_beg.obj_id = dip->obj_id;
1215 cursor.key_beg.key = namekey;
1216 cursor.key_beg.create_tid = 0;
1217 cursor.key_beg.delete_tid = 0;
1218 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1219 cursor.key_beg.obj_type = 0;
1221 cursor.key_end = cursor.key_beg;
1222 cursor.key_end.key += max_iterations;
1224 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
1227 * Scan all matching records (the chain), locate the one matching
1228 * the requested path component.
1230 * The hammer_ip_*() functions merge in-memory records with on-disk
1231 * records for the purposes of the search.
1234 localization = HAMMER_DEF_LOCALIZATION;
1237 error = hammer_ip_first(&cursor);
1238 while (error == 0) {
1239 error = hammer_ip_resolve_data(&cursor);
1242 if (nlen == cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF &&
1243 bcmp(ncp->nc_name, cursor.data->entry.name, nlen) == 0) {
1244 obj_id = cursor.data->entry.obj_id;
1245 localization = cursor.data->entry.localization;
1248 error = hammer_ip_next(&cursor);
1251 hammer_done_cursor(&cursor);
1254 * Lookup the obj_id. This should always succeed. If it does not
1255 * the filesystem may be damaged and we return a dummy inode.
1258 ip = hammer_get_inode(&trans, dip, obj_id,
1261 if (error == ENOENT) {
1262 kprintf("HAMMER: WARNING: Missing "
1263 "inode for dirent \"%s\"\n"
1264 "\tobj_id = %016llx, asof=%016llx, lo=%08x\n",
1266 (long long)obj_id, (long long)asof,
1269 ip = hammer_get_dummy_inode(&trans, dip, obj_id,
1274 error = hammer_get_vnode(ip, &vp);
1275 hammer_rel_inode(ip, 0);
1281 cache_setvp(ap->a_nch, vp);
1284 } else if (error == ENOENT) {
1285 cache_setvp(ap->a_nch, NULL);
1288 hammer_done_transaction(&trans);
1289 lwkt_reltoken(&hmp->fs_token);
1294 * hammer_vop_nlookupdotdot { dvp, vpp, cred }
1296 * Locate the parent directory of a directory vnode.
1298 * dvp is referenced but not locked. *vpp must be returned referenced and
1299 * locked. A parent_obj_id of 0 does not necessarily indicate that we are
1300 * at the root, instead it could indicate that the directory we were in was
1303 * NOTE: as-of sequences are not linked into the directory structure. If
1304 * we are at the root with a different asof then the mount point, reload
1305 * the same directory with the mount point's asof. I'm not sure what this
1306 * will do to NFS. We encode ASOF stamps in NFS file handles so it might not
1307 * get confused, but it hasn't been tested.
1311 hammer_vop_nlookupdotdot(struct vop_nlookupdotdot_args *ap)
1313 struct hammer_transaction trans;
1314 struct hammer_inode *dip;
1315 struct hammer_inode *ip;
1317 int64_t parent_obj_id;
1318 u_int32_t parent_obj_localization;
1322 dip = VTOI(ap->a_dvp);
1323 asof = dip->obj_asof;
1327 * Whos are parent? This could be the root of a pseudo-filesystem
1328 * whos parent is in another localization domain.
1330 lwkt_gettoken(&hmp->fs_token);
1331 parent_obj_id = dip->ino_data.parent_obj_id;
1332 if (dip->obj_id == HAMMER_OBJID_ROOT)
1333 parent_obj_localization = dip->ino_data.ext.obj.parent_obj_localization;
1335 parent_obj_localization = dip->obj_localization;
1338 * It's probably a PFS root when dip->ino_data.parent_obj_id is 0.
1340 if (parent_obj_id == 0) {
1341 if (dip->obj_id == HAMMER_OBJID_ROOT &&
1342 asof != hmp->asof) {
1343 parent_obj_id = dip->obj_id;
1345 *ap->a_fakename = kmalloc(19, M_TEMP, M_WAITOK);
1346 ksnprintf(*ap->a_fakename, 19, "0x%016llx",
1347 (long long)dip->obj_asof);
1350 lwkt_reltoken(&hmp->fs_token);
1355 hammer_simple_transaction(&trans, hmp);
1356 ++hammer_stats_file_iopsr;
1358 ip = hammer_get_inode(&trans, dip, parent_obj_id,
1359 asof, parent_obj_localization,
1360 dip->flags, &error);
1362 error = hammer_get_vnode(ip, ap->a_vpp);
1363 hammer_rel_inode(ip, 0);
1367 hammer_done_transaction(&trans);
1368 lwkt_reltoken(&hmp->fs_token);
1373 * hammer_vop_nlink { nch, dvp, vp, cred }
1377 hammer_vop_nlink(struct vop_nlink_args *ap)
1379 struct hammer_transaction trans;
1380 struct hammer_inode *dip;
1381 struct hammer_inode *ip;
1382 struct nchandle *nch;
1386 if (ap->a_dvp->v_mount != ap->a_vp->v_mount)
1390 dip = VTOI(ap->a_dvp);
1391 ip = VTOI(ap->a_vp);
1394 if (dip->obj_localization != ip->obj_localization)
1397 if (dip->flags & HAMMER_INODE_RO)
1399 if (ip->flags & HAMMER_INODE_RO)
1401 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0)
1405 * Create a transaction to cover the operations we perform.
1407 lwkt_gettoken(&hmp->fs_token);
1408 hammer_start_transaction(&trans, hmp);
1409 ++hammer_stats_file_iopsw;
1412 * Add the filesystem object to the directory. Note that neither
1413 * dip nor ip are referenced or locked, but their vnodes are
1414 * referenced. This function will bump the inode's link count.
1416 error = hammer_ip_add_directory(&trans, dip,
1417 nch->ncp->nc_name, nch->ncp->nc_nlen,
1424 cache_setunresolved(nch);
1425 cache_setvp(nch, ap->a_vp);
1427 hammer_done_transaction(&trans);
1428 hammer_knote(ap->a_vp, NOTE_LINK);
1429 hammer_knote(ap->a_dvp, NOTE_WRITE);
1430 lwkt_reltoken(&hmp->fs_token);
1435 * hammer_vop_nmkdir { nch, dvp, vpp, cred, vap }
1437 * The operating system has already ensured that the directory entry
1438 * does not exist and done all appropriate namespace locking.
1442 hammer_vop_nmkdir(struct vop_nmkdir_args *ap)
1444 struct hammer_transaction trans;
1445 struct hammer_inode *dip;
1446 struct hammer_inode *nip;
1447 struct nchandle *nch;
1452 dip = VTOI(ap->a_dvp);
1455 if (dip->flags & HAMMER_INODE_RO)
1457 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0)
1461 * Create a transaction to cover the operations we perform.
1463 lwkt_gettoken(&hmp->fs_token);
1464 hammer_start_transaction(&trans, hmp);
1465 ++hammer_stats_file_iopsw;
1468 * Create a new filesystem object of the requested type. The
1469 * returned inode will be referenced but not locked.
1471 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
1472 dip, nch->ncp->nc_name, nch->ncp->nc_nlen,
1475 hkprintf("hammer_mkdir error %d\n", error);
1476 hammer_done_transaction(&trans);
1478 lwkt_reltoken(&hmp->fs_token);
1482 * Add the new filesystem object to the directory. This will also
1483 * bump the inode's link count.
1485 error = hammer_ip_add_directory(&trans, dip,
1486 nch->ncp->nc_name, nch->ncp->nc_nlen,
1489 hkprintf("hammer_mkdir (add) error %d\n", error);
1495 hammer_rel_inode(nip, 0);
1498 error = hammer_get_vnode(nip, ap->a_vpp);
1499 hammer_rel_inode(nip, 0);
1501 cache_setunresolved(ap->a_nch);
1502 cache_setvp(ap->a_nch, *ap->a_vpp);
1505 hammer_done_transaction(&trans);
1507 hammer_knote(ap->a_dvp, NOTE_WRITE | NOTE_LINK);
1508 lwkt_reltoken(&hmp->fs_token);
1513 * hammer_vop_nmknod { nch, dvp, vpp, cred, vap }
1515 * The operating system has already ensured that the directory entry
1516 * does not exist and done all appropriate namespace locking.
1520 hammer_vop_nmknod(struct vop_nmknod_args *ap)
1522 struct hammer_transaction trans;
1523 struct hammer_inode *dip;
1524 struct hammer_inode *nip;
1525 struct nchandle *nch;
1530 dip = VTOI(ap->a_dvp);
1533 if (dip->flags & HAMMER_INODE_RO)
1535 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0)
1539 * Create a transaction to cover the operations we perform.
1541 lwkt_gettoken(&hmp->fs_token);
1542 hammer_start_transaction(&trans, hmp);
1543 ++hammer_stats_file_iopsw;
1546 * Create a new filesystem object of the requested type. The
1547 * returned inode will be referenced but not locked.
1549 * If mknod specifies a directory a pseudo-fs is created.
1551 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
1552 dip, nch->ncp->nc_name, nch->ncp->nc_nlen,
1555 hammer_done_transaction(&trans);
1557 lwkt_reltoken(&hmp->fs_token);
1562 * Add the new filesystem object to the directory. This will also
1563 * bump the inode's link count.
1565 error = hammer_ip_add_directory(&trans, dip,
1566 nch->ncp->nc_name, nch->ncp->nc_nlen,
1573 hammer_rel_inode(nip, 0);
1576 error = hammer_get_vnode(nip, ap->a_vpp);
1577 hammer_rel_inode(nip, 0);
1579 cache_setunresolved(ap->a_nch);
1580 cache_setvp(ap->a_nch, *ap->a_vpp);
1583 hammer_done_transaction(&trans);
1585 hammer_knote(ap->a_dvp, NOTE_WRITE);
1586 lwkt_reltoken(&hmp->fs_token);
1591 * hammer_vop_open { vp, mode, cred, fp }
1593 * MPSAFE (does not require fs_token)
1597 hammer_vop_open(struct vop_open_args *ap)
1601 ++hammer_stats_file_iopsr;
1602 ip = VTOI(ap->a_vp);
1604 if ((ap->a_mode & FWRITE) && (ip->flags & HAMMER_INODE_RO))
1606 return(vop_stdopen(ap));
1610 * hammer_vop_print { vp }
1614 hammer_vop_print(struct vop_print_args *ap)
1620 * hammer_vop_readdir { vp, uio, cred, *eofflag, *ncookies, off_t **cookies }
1624 hammer_vop_readdir(struct vop_readdir_args *ap)
1626 struct hammer_transaction trans;
1627 struct hammer_cursor cursor;
1628 struct hammer_inode *ip;
1631 hammer_base_elm_t base;
1640 ++hammer_stats_file_iopsr;
1641 ip = VTOI(ap->a_vp);
1643 saveoff = uio->uio_offset;
1646 if (ap->a_ncookies) {
1647 ncookies = uio->uio_resid / 16 + 1;
1648 if (ncookies > 1024)
1650 cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
1658 lwkt_gettoken(&hmp->fs_token);
1659 hammer_simple_transaction(&trans, hmp);
1662 * Handle artificial entries
1664 * It should be noted that the minimum value for a directory
1665 * hash key on-media is 0x0000000100000000, so we can use anything
1666 * less then that to represent our 'special' key space.
1670 r = vop_write_dirent(&error, uio, ip->obj_id, DT_DIR, 1, ".");
1674 cookies[cookie_index] = saveoff;
1677 if (cookie_index == ncookies)
1681 if (ip->ino_data.parent_obj_id) {
1682 r = vop_write_dirent(&error, uio,
1683 ip->ino_data.parent_obj_id,
1686 r = vop_write_dirent(&error, uio,
1687 ip->obj_id, DT_DIR, 2, "..");
1692 cookies[cookie_index] = saveoff;
1695 if (cookie_index == ncookies)
1700 * Key range (begin and end inclusive) to scan. Directory keys
1701 * directly translate to a 64 bit 'seek' position.
1703 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
1704 cursor.key_beg.localization = ip->obj_localization +
1705 hammer_dir_localization(ip);
1706 cursor.key_beg.obj_id = ip->obj_id;
1707 cursor.key_beg.create_tid = 0;
1708 cursor.key_beg.delete_tid = 0;
1709 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1710 cursor.key_beg.obj_type = 0;
1711 cursor.key_beg.key = saveoff;
1713 cursor.key_end = cursor.key_beg;
1714 cursor.key_end.key = HAMMER_MAX_KEY;
1715 cursor.asof = ip->obj_asof;
1716 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
1718 error = hammer_ip_first(&cursor);
1720 while (error == 0) {
1721 error = hammer_ip_resolve_data(&cursor);
1724 base = &cursor.leaf->base;
1725 saveoff = base->key;
1726 KKASSERT(cursor.leaf->data_len > HAMMER_ENTRY_NAME_OFF);
1728 if (base->obj_id != ip->obj_id)
1729 panic("readdir: bad record at %p", cursor.node);
1732 * Convert pseudo-filesystems into softlinks
1734 dtype = hammer_get_dtype(cursor.leaf->base.obj_type);
1735 r = vop_write_dirent(
1736 &error, uio, cursor.data->entry.obj_id,
1738 cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF ,
1739 (void *)cursor.data->entry.name);
1744 cookies[cookie_index] = base->key;
1746 if (cookie_index == ncookies)
1748 error = hammer_ip_next(&cursor);
1750 hammer_done_cursor(&cursor);
1753 hammer_done_transaction(&trans);
1756 *ap->a_eofflag = (error == ENOENT);
1757 uio->uio_offset = saveoff;
1758 if (error && cookie_index == 0) {
1759 if (error == ENOENT)
1762 kfree(cookies, M_TEMP);
1763 *ap->a_ncookies = 0;
1764 *ap->a_cookies = NULL;
1767 if (error == ENOENT)
1770 *ap->a_ncookies = cookie_index;
1771 *ap->a_cookies = cookies;
1774 lwkt_reltoken(&hmp->fs_token);
1779 * hammer_vop_readlink { vp, uio, cred }
1783 hammer_vop_readlink(struct vop_readlink_args *ap)
1785 struct hammer_transaction trans;
1786 struct hammer_cursor cursor;
1787 struct hammer_inode *ip;
1790 u_int32_t localization;
1791 hammer_pseudofs_inmem_t pfsm;
1794 ip = VTOI(ap->a_vp);
1797 lwkt_gettoken(&hmp->fs_token);
1800 * Shortcut if the symlink data was stuffed into ino_data.
1802 * Also expand special "@@PFS%05d" softlinks (expansion only
1803 * occurs for non-historical (current) accesses made from the
1804 * primary filesystem).
1806 * Note that userspace hammer command does not allow users to
1807 * create a @@PFS softlink under an existing other PFS (id!=0)
1808 * so the ip localization here for @@PFS softlink is always 0.
1810 if (ip->ino_data.size <= HAMMER_INODE_BASESYMLEN) {
1814 ptr = ip->ino_data.ext.symlink;
1815 bytes = (int)ip->ino_data.size;
1817 ip->obj_asof == HAMMER_MAX_TID &&
1818 ip->obj_localization == 0 &&
1819 strncmp(ptr, "@@PFS", 5) == 0) {
1820 hammer_simple_transaction(&trans, hmp);
1821 bcopy(ptr + 5, buf, 5);
1823 localization = strtoul(buf, NULL, 10) << 16;
1824 pfsm = hammer_load_pseudofs(&trans, localization,
1827 if (pfsm->pfsd.mirror_flags &
1828 HAMMER_PFSD_SLAVE) {
1829 /* vap->va_size == 26 */
1830 ksnprintf(buf, sizeof(buf),
1832 (long long)pfsm->pfsd.sync_end_tid,
1833 localization >> 16);
1835 /* vap->va_size == 10 */
1836 ksnprintf(buf, sizeof(buf),
1838 localization >> 16);
1840 ksnprintf(buf, sizeof(buf),
1842 (long long)HAMMER_MAX_TID,
1843 localization >> 16);
1847 bytes = strlen(buf);
1850 hammer_rel_pseudofs(hmp, pfsm);
1851 hammer_done_transaction(&trans);
1853 error = uiomove(ptr, bytes, ap->a_uio);
1854 lwkt_reltoken(&hmp->fs_token);
1861 hammer_simple_transaction(&trans, hmp);
1862 ++hammer_stats_file_iopsr;
1863 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
1866 * Key range (begin and end inclusive) to scan. Directory keys
1867 * directly translate to a 64 bit 'seek' position.
1869 cursor.key_beg.localization = ip->obj_localization +
1870 HAMMER_LOCALIZE_MISC;
1871 cursor.key_beg.obj_id = ip->obj_id;
1872 cursor.key_beg.create_tid = 0;
1873 cursor.key_beg.delete_tid = 0;
1874 cursor.key_beg.rec_type = HAMMER_RECTYPE_FIX;
1875 cursor.key_beg.obj_type = 0;
1876 cursor.key_beg.key = HAMMER_FIXKEY_SYMLINK;
1877 cursor.asof = ip->obj_asof;
1878 cursor.flags |= HAMMER_CURSOR_ASOF;
1880 error = hammer_ip_lookup(&cursor);
1882 error = hammer_ip_resolve_data(&cursor);
1884 KKASSERT(cursor.leaf->data_len >=
1885 HAMMER_SYMLINK_NAME_OFF);
1886 error = uiomove(cursor.data->symlink.name,
1887 cursor.leaf->data_len -
1888 HAMMER_SYMLINK_NAME_OFF,
1892 hammer_done_cursor(&cursor);
1893 hammer_done_transaction(&trans);
1894 lwkt_reltoken(&hmp->fs_token);
1899 * hammer_vop_nremove { nch, dvp, cred }
1903 hammer_vop_nremove(struct vop_nremove_args *ap)
1905 struct hammer_transaction trans;
1906 struct hammer_inode *dip;
1910 dip = VTOI(ap->a_dvp);
1913 if (hammer_nohistory(dip) == 0 &&
1914 (error = hammer_checkspace(hmp, HAMMER_CHKSPC_REMOVE)) != 0) {
1918 lwkt_gettoken(&hmp->fs_token);
1919 hammer_start_transaction(&trans, hmp);
1920 ++hammer_stats_file_iopsw;
1921 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp, ap->a_cred, 0, 0);
1922 hammer_done_transaction(&trans);
1924 hammer_knote(ap->a_dvp, NOTE_WRITE);
1925 lwkt_reltoken(&hmp->fs_token);
1930 * hammer_vop_nrename { fnch, tnch, fdvp, tdvp, cred }
1934 hammer_vop_nrename(struct vop_nrename_args *ap)
1936 struct hammer_transaction trans;
1937 struct namecache *fncp;
1938 struct namecache *tncp;
1939 struct hammer_inode *fdip;
1940 struct hammer_inode *tdip;
1941 struct hammer_inode *ip;
1943 struct hammer_cursor cursor;
1945 u_int32_t max_iterations;
1948 if (ap->a_fdvp->v_mount != ap->a_tdvp->v_mount)
1950 if (ap->a_fdvp->v_mount != ap->a_fnch->ncp->nc_vp->v_mount)
1953 fdip = VTOI(ap->a_fdvp);
1954 tdip = VTOI(ap->a_tdvp);
1955 fncp = ap->a_fnch->ncp;
1956 tncp = ap->a_tnch->ncp;
1957 ip = VTOI(fncp->nc_vp);
1958 KKASSERT(ip != NULL);
1962 if (fdip->obj_localization != tdip->obj_localization)
1964 if (fdip->obj_localization != ip->obj_localization)
1967 if (fdip->flags & HAMMER_INODE_RO)
1969 if (tdip->flags & HAMMER_INODE_RO)
1971 if (ip->flags & HAMMER_INODE_RO)
1973 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0)
1976 lwkt_gettoken(&hmp->fs_token);
1977 hammer_start_transaction(&trans, hmp);
1978 ++hammer_stats_file_iopsw;
1981 * Remove tncp from the target directory and then link ip as
1982 * tncp. XXX pass trans to dounlink
1984 * Force the inode sync-time to match the transaction so it is
1985 * in-sync with the creation of the target directory entry.
1987 error = hammer_dounlink(&trans, ap->a_tnch, ap->a_tdvp,
1989 if (error == 0 || error == ENOENT) {
1990 error = hammer_ip_add_directory(&trans, tdip,
1991 tncp->nc_name, tncp->nc_nlen,
1994 ip->ino_data.parent_obj_id = tdip->obj_id;
1995 ip->ino_data.ctime = trans.time;
1996 hammer_modify_inode(&trans, ip, HAMMER_INODE_DDIRTY);
2000 goto failed; /* XXX */
2003 * Locate the record in the originating directory and remove it.
2005 * Calculate the namekey and setup the key range for the scan. This
2006 * works kinda like a chained hash table where the lower 32 bits
2007 * of the namekey synthesize the chain.
2009 * The key range is inclusive of both key_beg and key_end.
2011 namekey = hammer_directory_namekey(fdip, fncp->nc_name, fncp->nc_nlen,
2014 hammer_init_cursor(&trans, &cursor, &fdip->cache[1], fdip);
2015 cursor.key_beg.localization = fdip->obj_localization +
2016 hammer_dir_localization(fdip);
2017 cursor.key_beg.obj_id = fdip->obj_id;
2018 cursor.key_beg.key = namekey;
2019 cursor.key_beg.create_tid = 0;
2020 cursor.key_beg.delete_tid = 0;
2021 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
2022 cursor.key_beg.obj_type = 0;
2024 cursor.key_end = cursor.key_beg;
2025 cursor.key_end.key += max_iterations;
2026 cursor.asof = fdip->obj_asof;
2027 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
2030 * Scan all matching records (the chain), locate the one matching
2031 * the requested path component.
2033 * The hammer_ip_*() functions merge in-memory records with on-disk
2034 * records for the purposes of the search.
2036 error = hammer_ip_first(&cursor);
2037 while (error == 0) {
2038 if (hammer_ip_resolve_data(&cursor) != 0)
2040 nlen = cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF;
2042 if (fncp->nc_nlen == nlen &&
2043 bcmp(fncp->nc_name, cursor.data->entry.name, nlen) == 0) {
2046 error = hammer_ip_next(&cursor);
2050 * If all is ok we have to get the inode so we can adjust nlinks.
2052 * WARNING: hammer_ip_del_directory() may have to terminate the
2053 * cursor to avoid a recursion. It's ok to call hammer_done_cursor()
2057 error = hammer_ip_del_directory(&trans, &cursor, fdip, ip);
2060 * XXX A deadlock here will break rename's atomicy for the purposes
2061 * of crash recovery.
2063 if (error == EDEADLK) {
2064 hammer_done_cursor(&cursor);
2069 * Cleanup and tell the kernel that the rename succeeded.
2071 * NOTE: ip->vp, if non-NULL, cannot be directly referenced
2072 * without formally acquiring the vp since the vp might
2073 * have zero refs on it, or in the middle of a reclaim,
2076 hammer_done_cursor(&cursor);
2078 cache_rename(ap->a_fnch, ap->a_tnch);
2079 hammer_knote(ap->a_fdvp, NOTE_WRITE);
2080 hammer_knote(ap->a_tdvp, NOTE_WRITE);
2084 error = hammer_get_vnode(ip, &vp);
2085 if (error == 0 && vp) {
2087 hammer_knote(ip->vp, NOTE_RENAME);
2091 kprintf("Debug: HAMMER ip/vp race2 avoided\n");
2096 hammer_done_transaction(&trans);
2097 lwkt_reltoken(&hmp->fs_token);
2102 * hammer_vop_nrmdir { nch, dvp, cred }
2106 hammer_vop_nrmdir(struct vop_nrmdir_args *ap)
2108 struct hammer_transaction trans;
2109 struct hammer_inode *dip;
2113 dip = VTOI(ap->a_dvp);
2116 if (hammer_nohistory(dip) == 0 &&
2117 (error = hammer_checkspace(hmp, HAMMER_CHKSPC_REMOVE)) != 0) {
2121 lwkt_gettoken(&hmp->fs_token);
2122 hammer_start_transaction(&trans, hmp);
2123 ++hammer_stats_file_iopsw;
2124 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp, ap->a_cred, 0, 1);
2125 hammer_done_transaction(&trans);
2127 hammer_knote(ap->a_dvp, NOTE_WRITE | NOTE_LINK);
2128 lwkt_reltoken(&hmp->fs_token);
2133 * hammer_vop_markatime { vp, cred }
2137 hammer_vop_markatime(struct vop_markatime_args *ap)
2139 struct hammer_transaction trans;
2140 struct hammer_inode *ip;
2143 ip = VTOI(ap->a_vp);
2144 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
2146 if (ip->flags & HAMMER_INODE_RO)
2149 if (hmp->mp->mnt_flag & MNT_NOATIME)
2151 lwkt_gettoken(&hmp->fs_token);
2152 hammer_start_transaction(&trans, hmp);
2153 ++hammer_stats_file_iopsw;
2155 ip->ino_data.atime = trans.time;
2156 hammer_modify_inode(&trans, ip, HAMMER_INODE_ATIME);
2157 hammer_done_transaction(&trans);
2158 hammer_knote(ap->a_vp, NOTE_ATTRIB);
2159 lwkt_reltoken(&hmp->fs_token);
2164 * hammer_vop_setattr { vp, vap, cred }
2168 hammer_vop_setattr(struct vop_setattr_args *ap)
2170 struct hammer_transaction trans;
2171 struct hammer_inode *ip;
2180 int64_t aligned_size;
2185 ip = ap->a_vp->v_data;
2190 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
2192 if (ip->flags & HAMMER_INODE_RO)
2194 if (hammer_nohistory(ip) == 0 &&
2195 (error = hammer_checkspace(hmp, HAMMER_CHKSPC_REMOVE)) != 0) {
2199 lwkt_gettoken(&hmp->fs_token);
2200 hammer_start_transaction(&trans, hmp);
2201 ++hammer_stats_file_iopsw;
2204 if (vap->va_flags != VNOVAL) {
2205 flags = ip->ino_data.uflags;
2206 error = vop_helper_setattr_flags(&flags, vap->va_flags,
2207 hammer_to_unix_xid(&ip->ino_data.uid),
2210 if (ip->ino_data.uflags != flags) {
2211 ip->ino_data.uflags = flags;
2212 ip->ino_data.ctime = trans.time;
2213 modflags |= HAMMER_INODE_DDIRTY;
2214 kflags |= NOTE_ATTRIB;
2216 if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) {
2223 if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) {
2227 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
2228 mode_t cur_mode = ip->ino_data.mode;
2229 uid_t cur_uid = hammer_to_unix_xid(&ip->ino_data.uid);
2230 gid_t cur_gid = hammer_to_unix_xid(&ip->ino_data.gid);
2234 error = vop_helper_chown(ap->a_vp, vap->va_uid, vap->va_gid,
2236 &cur_uid, &cur_gid, &cur_mode);
2238 hammer_guid_to_uuid(&uuid_uid, cur_uid);
2239 hammer_guid_to_uuid(&uuid_gid, cur_gid);
2240 if (bcmp(&uuid_uid, &ip->ino_data.uid,
2241 sizeof(uuid_uid)) ||
2242 bcmp(&uuid_gid, &ip->ino_data.gid,
2243 sizeof(uuid_gid)) ||
2244 ip->ino_data.mode != cur_mode) {
2245 ip->ino_data.uid = uuid_uid;
2246 ip->ino_data.gid = uuid_gid;
2247 ip->ino_data.mode = cur_mode;
2248 ip->ino_data.ctime = trans.time;
2249 modflags |= HAMMER_INODE_DDIRTY;
2251 kflags |= NOTE_ATTRIB;
2254 while (vap->va_size != VNOVAL && ip->ino_data.size != vap->va_size) {
2255 switch(ap->a_vp->v_type) {
2257 if (vap->va_size == ip->ino_data.size)
2261 * Log the operation if in fast-fsync mode or if
2262 * there are unterminated redo write records present.
2264 * The second check is needed so the recovery code
2265 * properly truncates write redos even if nominal
2266 * REDO operations is turned off due to excessive
2267 * writes, because the related records might be
2268 * destroyed and never lay down a TERM_WRITE.
2270 if ((ip->flags & HAMMER_INODE_REDO) ||
2271 (ip->flags & HAMMER_INODE_RDIRTY)) {
2272 error = hammer_generate_redo(&trans, ip,
2277 blksize = hammer_blocksize(vap->va_size);
2280 * XXX break atomicy, we can deadlock the backend
2281 * if we do not release the lock. Probably not a
2284 if (vap->va_size < ip->ino_data.size) {
2285 nvtruncbuf(ap->a_vp, vap->va_size,
2287 hammer_blockoff(vap->va_size),
2290 kflags |= NOTE_WRITE;
2292 nvextendbuf(ap->a_vp,
2295 hammer_blocksize(ip->ino_data.size),
2296 hammer_blocksize(vap->va_size),
2297 hammer_blockoff(ip->ino_data.size),
2298 hammer_blockoff(vap->va_size),
2301 kflags |= NOTE_WRITE | NOTE_EXTEND;
2303 ip->ino_data.size = vap->va_size;
2304 ip->ino_data.mtime = trans.time;
2305 /* XXX safe to use SDIRTY instead of DDIRTY here? */
2306 modflags |= HAMMER_INODE_MTIME | HAMMER_INODE_DDIRTY;
2309 * On-media truncation is cached in the inode until
2310 * the inode is synchronized. We must immediately
2311 * handle any frontend records.
2314 hammer_ip_frontend_trunc(ip, vap->va_size);
2315 #ifdef DEBUG_TRUNCATE
2316 if (HammerTruncIp == NULL)
2319 if ((ip->flags & HAMMER_INODE_TRUNCATED) == 0) {
2320 ip->flags |= HAMMER_INODE_TRUNCATED;
2321 ip->trunc_off = vap->va_size;
2322 hammer_inode_dirty(ip);
2323 #ifdef DEBUG_TRUNCATE
2324 if (ip == HammerTruncIp)
2325 kprintf("truncate1 %016llx\n",
2326 (long long)ip->trunc_off);
2328 } else if (ip->trunc_off > vap->va_size) {
2329 ip->trunc_off = vap->va_size;
2330 #ifdef DEBUG_TRUNCATE
2331 if (ip == HammerTruncIp)
2332 kprintf("truncate2 %016llx\n",
2333 (long long)ip->trunc_off);
2336 #ifdef DEBUG_TRUNCATE
2337 if (ip == HammerTruncIp)
2338 kprintf("truncate3 %016llx (ignored)\n",
2339 (long long)vap->va_size);
2346 * When truncating, nvtruncbuf() may have cleaned out
2347 * a portion of the last block on-disk in the buffer
2348 * cache. We must clean out any frontend records
2349 * for blocks beyond the new last block.
2351 aligned_size = (vap->va_size + (blksize - 1)) &
2352 ~(int64_t)(blksize - 1);
2353 if (truncating && vap->va_size < aligned_size) {
2354 aligned_size -= blksize;
2355 hammer_ip_frontend_trunc(ip, aligned_size);
2360 if ((ip->flags & HAMMER_INODE_TRUNCATED) == 0) {
2361 ip->flags |= HAMMER_INODE_TRUNCATED;
2362 ip->trunc_off = vap->va_size;
2363 hammer_inode_dirty(ip);
2364 } else if (ip->trunc_off > vap->va_size) {
2365 ip->trunc_off = vap->va_size;
2367 hammer_ip_frontend_trunc(ip, vap->va_size);
2368 ip->ino_data.size = vap->va_size;
2369 ip->ino_data.mtime = trans.time;
2370 modflags |= HAMMER_INODE_MTIME | HAMMER_INODE_DDIRTY;
2371 kflags |= NOTE_ATTRIB;
2379 if (vap->va_atime.tv_sec != VNOVAL) {
2380 ip->ino_data.atime = hammer_timespec_to_time(&vap->va_atime);
2381 modflags |= HAMMER_INODE_ATIME;
2382 kflags |= NOTE_ATTRIB;
2384 if (vap->va_mtime.tv_sec != VNOVAL) {
2385 ip->ino_data.mtime = hammer_timespec_to_time(&vap->va_mtime);
2386 modflags |= HAMMER_INODE_MTIME;
2387 kflags |= NOTE_ATTRIB;
2389 if (vap->va_mode != (mode_t)VNOVAL) {
2390 mode_t cur_mode = ip->ino_data.mode;
2391 uid_t cur_uid = hammer_to_unix_xid(&ip->ino_data.uid);
2392 gid_t cur_gid = hammer_to_unix_xid(&ip->ino_data.gid);
2394 error = vop_helper_chmod(ap->a_vp, vap->va_mode, ap->a_cred,
2395 cur_uid, cur_gid, &cur_mode);
2396 if (error == 0 && ip->ino_data.mode != cur_mode) {
2397 ip->ino_data.mode = cur_mode;
2398 ip->ino_data.ctime = trans.time;
2399 modflags |= HAMMER_INODE_DDIRTY;
2400 kflags |= NOTE_ATTRIB;
2405 hammer_modify_inode(&trans, ip, modflags);
2406 hammer_done_transaction(&trans);
2407 hammer_knote(ap->a_vp, kflags);
2408 lwkt_reltoken(&hmp->fs_token);
2413 * hammer_vop_nsymlink { nch, dvp, vpp, cred, vap, target }
2417 hammer_vop_nsymlink(struct vop_nsymlink_args *ap)
2419 struct hammer_transaction trans;
2420 struct hammer_inode *dip;
2421 struct hammer_inode *nip;
2422 hammer_record_t record;
2423 struct nchandle *nch;
2428 ap->a_vap->va_type = VLNK;
2431 dip = VTOI(ap->a_dvp);
2434 if (dip->flags & HAMMER_INODE_RO)
2436 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0)
2440 * Create a transaction to cover the operations we perform.
2442 lwkt_gettoken(&hmp->fs_token);
2443 hammer_start_transaction(&trans, hmp);
2444 ++hammer_stats_file_iopsw;
2447 * Create a new filesystem object of the requested type. The
2448 * returned inode will be referenced but not locked.
2451 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
2452 dip, nch->ncp->nc_name, nch->ncp->nc_nlen,
2455 hammer_done_transaction(&trans);
2457 lwkt_reltoken(&hmp->fs_token);
2462 * Add a record representing the symlink. symlink stores the link
2463 * as pure data, not a string, and is no \0 terminated.
2466 bytes = strlen(ap->a_target);
2468 if (bytes <= HAMMER_INODE_BASESYMLEN) {
2469 bcopy(ap->a_target, nip->ino_data.ext.symlink, bytes);
2471 record = hammer_alloc_mem_record(nip, bytes);
2472 record->type = HAMMER_MEM_RECORD_GENERAL;
2474 record->leaf.base.localization = nip->obj_localization +
2475 HAMMER_LOCALIZE_MISC;
2476 record->leaf.base.key = HAMMER_FIXKEY_SYMLINK;
2477 record->leaf.base.rec_type = HAMMER_RECTYPE_FIX;
2478 record->leaf.data_len = bytes;
2479 KKASSERT(HAMMER_SYMLINK_NAME_OFF == 0);
2480 bcopy(ap->a_target, record->data->symlink.name, bytes);
2481 error = hammer_ip_add_record(&trans, record);
2485 * Set the file size to the length of the link.
2488 nip->ino_data.size = bytes;
2489 hammer_modify_inode(&trans, nip, HAMMER_INODE_DDIRTY);
2493 error = hammer_ip_add_directory(&trans, dip, nch->ncp->nc_name,
2494 nch->ncp->nc_nlen, nip);
2500 hammer_rel_inode(nip, 0);
2503 error = hammer_get_vnode(nip, ap->a_vpp);
2504 hammer_rel_inode(nip, 0);
2506 cache_setunresolved(ap->a_nch);
2507 cache_setvp(ap->a_nch, *ap->a_vpp);
2508 hammer_knote(ap->a_dvp, NOTE_WRITE);
2511 hammer_done_transaction(&trans);
2512 lwkt_reltoken(&hmp->fs_token);
2517 * hammer_vop_nwhiteout { nch, dvp, cred, flags }
2521 hammer_vop_nwhiteout(struct vop_nwhiteout_args *ap)
2523 struct hammer_transaction trans;
2524 struct hammer_inode *dip;
2528 dip = VTOI(ap->a_dvp);
2531 if (hammer_nohistory(dip) == 0 &&
2532 (error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0) {
2536 lwkt_gettoken(&hmp->fs_token);
2537 hammer_start_transaction(&trans, hmp);
2538 ++hammer_stats_file_iopsw;
2539 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp,
2540 ap->a_cred, ap->a_flags, -1);
2541 hammer_done_transaction(&trans);
2542 lwkt_reltoken(&hmp->fs_token);
2548 * hammer_vop_ioctl { vp, command, data, fflag, cred }
2552 hammer_vop_ioctl(struct vop_ioctl_args *ap)
2554 struct hammer_inode *ip = ap->a_vp->v_data;
2555 hammer_mount_t hmp = ip->hmp;
2558 ++hammer_stats_file_iopsr;
2559 lwkt_gettoken(&hmp->fs_token);
2560 error = hammer_ioctl(ip, ap->a_command, ap->a_data,
2561 ap->a_fflag, ap->a_cred);
2562 lwkt_reltoken(&hmp->fs_token);
2568 hammer_vop_mountctl(struct vop_mountctl_args *ap)
2570 static const struct mountctl_opt extraopt[] = {
2571 { HMNT_NOHISTORY, "nohistory" },
2572 { HMNT_MASTERID, "master" },
2576 struct hammer_mount *hmp;
2583 mp = ap->a_head.a_ops->head.vv_mount;
2584 KKASSERT(mp->mnt_data != NULL);
2585 hmp = (struct hammer_mount *)mp->mnt_data;
2587 lwkt_gettoken(&hmp->fs_token);
2590 case MOUNTCTL_SET_EXPORT:
2591 if (ap->a_ctllen != sizeof(struct export_args))
2594 error = hammer_vfs_export(mp, ap->a_op,
2595 (const struct export_args *)ap->a_ctl);
2597 case MOUNTCTL_MOUNTFLAGS:
2600 * Call standard mountctl VOP function
2601 * so we get user mount flags.
2603 error = vop_stdmountctl(ap);
2607 usedbytes = *ap->a_res;
2609 if (usedbytes > 0 && usedbytes < ap->a_buflen) {
2610 usedbytes += vfs_flagstostr(hmp->hflags, extraopt,
2612 ap->a_buflen - usedbytes,
2616 *ap->a_res += usedbytes;
2620 error = vop_stdmountctl(ap);
2623 lwkt_reltoken(&hmp->fs_token);
2628 * hammer_vop_strategy { vp, bio }
2630 * Strategy call, used for regular file read & write only. Note that the
2631 * bp may represent a cluster.
2633 * To simplify operation and allow better optimizations in the future,
2634 * this code does not make any assumptions with regards to buffer alignment
2639 hammer_vop_strategy(struct vop_strategy_args *ap)
2644 bp = ap->a_bio->bio_buf;
2648 error = hammer_vop_strategy_read(ap);
2651 error = hammer_vop_strategy_write(ap);
2654 bp->b_error = error = EINVAL;
2655 bp->b_flags |= B_ERROR;
2660 /* hammer_dump_dedup_cache(((hammer_inode_t)ap->a_vp->v_data)->hmp); */
2666 * Read from a regular file. Iterate the related records and fill in the
2667 * BIO/BUF. Gaps are zero-filled.
2669 * The support code in hammer_object.c should be used to deal with mixed
2670 * in-memory and on-disk records.
2672 * NOTE: Can be called from the cluster code with an oversized buf.
2678 hammer_vop_strategy_read(struct vop_strategy_args *ap)
2680 struct hammer_transaction trans;
2681 struct hammer_inode *ip;
2682 struct hammer_inode *dip;
2684 struct hammer_cursor cursor;
2685 hammer_base_elm_t base;
2686 hammer_off_t disk_offset;
2701 ip = ap->a_vp->v_data;
2705 * The zone-2 disk offset may have been set by the cluster code via
2706 * a BMAP operation, or else should be NOOFFSET.
2708 * Checking the high bits for a match against zone-2 should suffice.
2710 * In cases where a lot of data duplication is present it may be
2711 * more beneficial to drop through and doubule-buffer through the
2714 nbio = push_bio(bio);
2715 if ((nbio->bio_offset & HAMMER_OFF_ZONE_MASK) ==
2716 HAMMER_ZONE_LARGE_DATA) {
2717 if (hammer_double_buffer == 0) {
2718 lwkt_gettoken(&hmp->fs_token);
2719 error = hammer_io_direct_read(hmp, nbio, NULL);
2720 lwkt_reltoken(&hmp->fs_token);
2725 * Try to shortcut requests for double_buffer mode too.
2726 * Since this mode runs through the device buffer cache
2727 * only compatible buffer sizes (meaning those generated
2728 * by normal filesystem buffers) are legal.
2730 if (hammer_live_dedup == 0 && (bp->b_flags & B_PAGING) == 0) {
2731 lwkt_gettoken(&hmp->fs_token);
2732 error = hammer_io_indirect_read(hmp, nbio, NULL);
2733 lwkt_reltoken(&hmp->fs_token);
2739 * Well, that sucked. Do it the hard way. If all the stars are
2740 * aligned we may still be able to issue a direct-read.
2742 lwkt_gettoken(&hmp->fs_token);
2743 hammer_simple_transaction(&trans, hmp);
2744 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
2747 * Key range (begin and end inclusive) to scan. Note that the key's
2748 * stored in the actual records represent BASE+LEN, not BASE. The
2749 * first record containing bio_offset will have a key > bio_offset.
2751 cursor.key_beg.localization = ip->obj_localization +
2752 HAMMER_LOCALIZE_MISC;
2753 cursor.key_beg.obj_id = ip->obj_id;
2754 cursor.key_beg.create_tid = 0;
2755 cursor.key_beg.delete_tid = 0;
2756 cursor.key_beg.obj_type = 0;
2757 cursor.key_beg.key = bio->bio_offset + 1;
2758 cursor.asof = ip->obj_asof;
2759 cursor.flags |= HAMMER_CURSOR_ASOF;
2761 cursor.key_end = cursor.key_beg;
2762 KKASSERT(ip->ino_data.obj_type == HAMMER_OBJTYPE_REGFILE);
2764 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_DBFILE) {
2765 cursor.key_beg.rec_type = HAMMER_RECTYPE_DB;
2766 cursor.key_end.rec_type = HAMMER_RECTYPE_DB;
2767 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
2771 ran_end = bio->bio_offset + bp->b_bufsize;
2772 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
2773 cursor.key_end.rec_type = HAMMER_RECTYPE_DATA;
2774 tmp64 = ran_end + MAXPHYS + 1; /* work-around GCC-4 bug */
2775 if (tmp64 < ran_end)
2776 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
2778 cursor.key_end.key = ran_end + MAXPHYS + 1;
2780 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
2783 * Set NOSWAPCACHE for cursor data extraction if double buffering
2784 * is disabled or (if the file is not marked cacheable via chflags
2785 * and vm.swapcache_use_chflags is enabled).
2787 if (hammer_double_buffer == 0 ||
2788 ((ap->a_vp->v_flag & VSWAPCACHE) == 0 &&
2789 vm_swapcache_use_chflags)) {
2790 cursor.flags |= HAMMER_CURSOR_NOSWAPCACHE;
2793 error = hammer_ip_first(&cursor);
2796 while (error == 0) {
2798 * Get the base file offset of the record. The key for
2799 * data records is (base + bytes) rather then (base).
2801 base = &cursor.leaf->base;
2802 rec_offset = base->key - cursor.leaf->data_len;
2805 * Calculate the gap, if any, and zero-fill it.
2807 * n is the offset of the start of the record verses our
2808 * current seek offset in the bio.
2810 n = (int)(rec_offset - (bio->bio_offset + boff));
2812 if (n > bp->b_bufsize - boff)
2813 n = bp->b_bufsize - boff;
2814 bzero((char *)bp->b_data + boff, n);
2820 * Calculate the data offset in the record and the number
2821 * of bytes we can copy.
2823 * There are two degenerate cases. First, boff may already
2824 * be at bp->b_bufsize. Secondly, the data offset within
2825 * the record may exceed the record's size.
2829 n = cursor.leaf->data_len - roff;
2831 kprintf("strategy_read: bad n=%d roff=%d\n", n, roff);
2833 } else if (n > bp->b_bufsize - boff) {
2834 n = bp->b_bufsize - boff;
2838 * Deal with cached truncations. This cool bit of code
2839 * allows truncate()/ftruncate() to avoid having to sync
2842 * If the frontend is truncated then all backend records are
2843 * subject to the frontend's truncation.
2845 * If the backend is truncated then backend records on-disk
2846 * (but not in-memory) are subject to the backend's
2847 * truncation. In-memory records owned by the backend
2848 * represent data written after the truncation point on the
2849 * backend and must not be truncated.
2851 * Truncate operations deal with frontend buffer cache
2852 * buffers and frontend-owned in-memory records synchronously.
2854 if (ip->flags & HAMMER_INODE_TRUNCATED) {
2855 if (hammer_cursor_ondisk(&cursor)/* ||
2856 cursor.iprec->flush_state == HAMMER_FST_FLUSH*/) {
2857 if (ip->trunc_off <= rec_offset)
2859 else if (ip->trunc_off < rec_offset + n)
2860 n = (int)(ip->trunc_off - rec_offset);
2863 if (ip->sync_flags & HAMMER_INODE_TRUNCATED) {
2864 if (hammer_cursor_ondisk(&cursor)) {
2865 if (ip->sync_trunc_off <= rec_offset)
2867 else if (ip->sync_trunc_off < rec_offset + n)
2868 n = (int)(ip->sync_trunc_off - rec_offset);
2873 * Try to issue a direct read into our bio if possible,
2874 * otherwise resolve the element data into a hammer_buffer
2877 * The buffer on-disk should be zerod past any real
2878 * truncation point, but may not be for any synthesized
2879 * truncation point from above.
2881 * NOTE: disk_offset is only valid if the cursor data is
2884 disk_offset = cursor.leaf->data_offset + roff;
2885 isdedupable = (boff == 0 && n == bp->b_bufsize &&
2886 hammer_cursor_ondisk(&cursor) &&
2887 ((int)disk_offset & HAMMER_BUFMASK) == 0);
2889 if (isdedupable && hammer_double_buffer == 0) {
2893 KKASSERT((disk_offset & HAMMER_OFF_ZONE_MASK) ==
2894 HAMMER_ZONE_LARGE_DATA);
2895 nbio->bio_offset = disk_offset;
2896 error = hammer_io_direct_read(hmp, nbio, cursor.leaf);
2897 if (hammer_live_dedup && error == 0)
2898 hammer_dedup_cache_add(ip, cursor.leaf);
2900 } else if (isdedupable) {
2902 * Async I/O case for reading from backing store
2903 * and copying the data to the filesystem buffer.
2904 * live-dedup has to verify the data anyway if it
2905 * gets a hit later so we can just add the entry
2908 KKASSERT((disk_offset & HAMMER_OFF_ZONE_MASK) ==
2909 HAMMER_ZONE_LARGE_DATA);
2910 nbio->bio_offset = disk_offset;
2911 if (hammer_live_dedup)
2912 hammer_dedup_cache_add(ip, cursor.leaf);
2913 error = hammer_io_indirect_read(hmp, nbio, cursor.leaf);
2916 error = hammer_ip_resolve_data(&cursor);
2918 if (hammer_live_dedup && isdedupable)
2919 hammer_dedup_cache_add(ip, cursor.leaf);
2920 bcopy((char *)cursor.data + roff,
2921 (char *)bp->b_data + boff, n);
2928 * We have to be sure that the only elements added to the
2929 * dedup cache are those which are already on-media.
2931 if (hammer_live_dedup && hammer_cursor_ondisk(&cursor))
2932 hammer_dedup_cache_add(ip, cursor.leaf);
2935 * Iterate until we have filled the request.
2938 if (boff == bp->b_bufsize)
2940 error = hammer_ip_next(&cursor);
2944 * There may have been a gap after the last record
2946 if (error == ENOENT)
2948 if (error == 0 && boff != bp->b_bufsize) {
2949 KKASSERT(boff < bp->b_bufsize);
2950 bzero((char *)bp->b_data + boff, bp->b_bufsize - boff);
2951 /* boff = bp->b_bufsize; */
2955 * Disallow swapcache operation on the vnode buffer if double
2956 * buffering is enabled, the swapcache will get the data via
2957 * the block device buffer.
2959 if (hammer_double_buffer)
2960 bp->b_flags |= B_NOTMETA;
2966 bp->b_error = error;
2968 bp->b_flags |= B_ERROR;
2973 * Cache the b-tree node for the last data read in cache[1].
2975 * If we hit the file EOF then also cache the node in the
2976 * governing directory's cache[3], it will be used to initialize
2977 * the new inode's cache[1] for any inodes looked up via the directory.
2979 * This doesn't reduce disk accesses since the B-Tree chain is
2980 * likely cached, but it does reduce cpu overhead when looking
2981 * up file offsets for cpdup/tar/cpio style iterations.
2984 hammer_cache_node(&ip->cache[1], cursor.node);
2985 if (ran_end >= ip->ino_data.size) {
2986 dip = hammer_find_inode(&trans, ip->ino_data.parent_obj_id,
2987 ip->obj_asof, ip->obj_localization);
2989 hammer_cache_node(&dip->cache[3], cursor.node);
2990 hammer_rel_inode(dip, 0);
2993 hammer_done_cursor(&cursor);
2994 hammer_done_transaction(&trans);
2995 lwkt_reltoken(&hmp->fs_token);
3000 * BMAP operation - used to support cluster_read() only.
3002 * (struct vnode *vp, off_t loffset, off_t *doffsetp, int *runp, int *runb)
3004 * This routine may return EOPNOTSUPP if the opration is not supported for
3005 * the specified offset. The contents of the pointer arguments do not
3006 * need to be initialized in that case.
3008 * If a disk address is available and properly aligned return 0 with
3009 * *doffsetp set to the zone-2 address, and *runp / *runb set appropriately
3010 * to the run-length relative to that offset. Callers may assume that
3011 * *doffsetp is valid if 0 is returned, even if *runp is not sufficiently
3012 * large, so return EOPNOTSUPP if it is not sufficiently large.
3016 hammer_vop_bmap(struct vop_bmap_args *ap)
3018 struct hammer_transaction trans;
3019 struct hammer_inode *ip;
3021 struct hammer_cursor cursor;
3022 hammer_base_elm_t base;
3026 int64_t base_offset;
3027 int64_t base_disk_offset;
3028 int64_t last_offset;
3029 hammer_off_t last_disk_offset;
3030 hammer_off_t disk_offset;
3035 ++hammer_stats_file_iopsr;
3036 ip = ap->a_vp->v_data;
3040 * We can only BMAP regular files. We can't BMAP database files,
3043 if (ip->ino_data.obj_type != HAMMER_OBJTYPE_REGFILE)
3047 * bmap is typically called with runp/runb both NULL when used
3048 * for writing. We do not support BMAP for writing atm.
3050 if (ap->a_cmd != BUF_CMD_READ)
3054 * Scan the B-Tree to acquire blockmap addresses, then translate
3057 lwkt_gettoken(&hmp->fs_token);
3058 hammer_simple_transaction(&trans, hmp);
3060 kprintf("bmap_beg %016llx ip->cache %p\n",
3061 (long long)ap->a_loffset, ip->cache[1]);
3063 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
3066 * Key range (begin and end inclusive) to scan. Note that the key's
3067 * stored in the actual records represent BASE+LEN, not BASE. The
3068 * first record containing bio_offset will have a key > bio_offset.
3070 cursor.key_beg.localization = ip->obj_localization +
3071 HAMMER_LOCALIZE_MISC;
3072 cursor.key_beg.obj_id = ip->obj_id;
3073 cursor.key_beg.create_tid = 0;
3074 cursor.key_beg.delete_tid = 0;
3075 cursor.key_beg.obj_type = 0;
3077 cursor.key_beg.key = ap->a_loffset - MAXPHYS + 1;
3079 cursor.key_beg.key = ap->a_loffset + 1;
3080 if (cursor.key_beg.key < 0)
3081 cursor.key_beg.key = 0;
3082 cursor.asof = ip->obj_asof;
3083 cursor.flags |= HAMMER_CURSOR_ASOF;
3085 cursor.key_end = cursor.key_beg;
3086 KKASSERT(ip->ino_data.obj_type == HAMMER_OBJTYPE_REGFILE);
3088 ran_end = ap->a_loffset + MAXPHYS;
3089 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
3090 cursor.key_end.rec_type = HAMMER_RECTYPE_DATA;
3091 tmp64 = ran_end + MAXPHYS + 1; /* work-around GCC-4 bug */
3092 if (tmp64 < ran_end)
3093 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
3095 cursor.key_end.key = ran_end + MAXPHYS + 1;
3097 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
3099 error = hammer_ip_first(&cursor);
3100 base_offset = last_offset = 0;
3101 base_disk_offset = last_disk_offset = 0;
3103 while (error == 0) {
3105 * Get the base file offset of the record. The key for
3106 * data records is (base + bytes) rather then (base).
3108 * NOTE: rec_offset + rec_len may exceed the end-of-file.
3109 * The extra bytes should be zero on-disk and the BMAP op
3110 * should still be ok.
3112 base = &cursor.leaf->base;
3113 rec_offset = base->key - cursor.leaf->data_len;
3114 rec_len = cursor.leaf->data_len;
3117 * Incorporate any cached truncation.
3119 * NOTE: Modifications to rec_len based on synthesized
3120 * truncation points remove the guarantee that any extended
3121 * data on disk is zero (since the truncations may not have
3122 * taken place on-media yet).
3124 if (ip->flags & HAMMER_INODE_TRUNCATED) {
3125 if (hammer_cursor_ondisk(&cursor) ||
3126 cursor.iprec->flush_state == HAMMER_FST_FLUSH) {
3127 if (ip->trunc_off <= rec_offset)
3129 else if (ip->trunc_off < rec_offset + rec_len)
3130 rec_len = (int)(ip->trunc_off - rec_offset);
3133 if (ip->sync_flags & HAMMER_INODE_TRUNCATED) {
3134 if (hammer_cursor_ondisk(&cursor)) {
3135 if (ip->sync_trunc_off <= rec_offset)
3137 else if (ip->sync_trunc_off < rec_offset + rec_len)
3138 rec_len = (int)(ip->sync_trunc_off - rec_offset);
3143 * Accumulate information. If we have hit a discontiguous
3144 * block reset base_offset unless we are already beyond the
3145 * requested offset. If we are, that's it, we stop.
3149 if (hammer_cursor_ondisk(&cursor)) {
3150 disk_offset = cursor.leaf->data_offset;
3151 if (rec_offset != last_offset ||
3152 disk_offset != last_disk_offset) {
3153 if (rec_offset > ap->a_loffset)
3155 base_offset = rec_offset;
3156 base_disk_offset = disk_offset;
3158 last_offset = rec_offset + rec_len;
3159 last_disk_offset = disk_offset + rec_len;
3161 if (hammer_live_dedup)
3162 hammer_dedup_cache_add(ip, cursor.leaf);
3165 error = hammer_ip_next(&cursor);
3169 kprintf("BMAP %016llx: %016llx - %016llx\n",
3170 (long long)ap->a_loffset,
3171 (long long)base_offset,
3172 (long long)last_offset);
3173 kprintf("BMAP %16s: %016llx - %016llx\n", "",
3174 (long long)base_disk_offset,
3175 (long long)last_disk_offset);
3179 hammer_cache_node(&ip->cache[1], cursor.node);
3181 kprintf("bmap_end2 %016llx ip->cache %p\n",
3182 (long long)ap->a_loffset, ip->cache[1]);
3185 hammer_done_cursor(&cursor);
3186 hammer_done_transaction(&trans);
3187 lwkt_reltoken(&hmp->fs_token);
3190 * If we couldn't find any records or the records we did find were
3191 * all behind the requested offset, return failure. A forward
3192 * truncation can leave a hole w/ no on-disk records.
3194 if (last_offset == 0 || last_offset < ap->a_loffset)
3195 return (EOPNOTSUPP);
3198 * Figure out the block size at the requested offset and adjust
3199 * our limits so the cluster_read() does not create inappropriately
3200 * sized buffer cache buffers.
3202 blksize = hammer_blocksize(ap->a_loffset);
3203 if (hammer_blocksize(base_offset) != blksize) {
3204 base_offset = hammer_blockdemarc(base_offset, ap->a_loffset);
3206 if (last_offset != ap->a_loffset &&
3207 hammer_blocksize(last_offset - 1) != blksize) {
3208 last_offset = hammer_blockdemarc(ap->a_loffset,
3213 * Returning EOPNOTSUPP simply prevents the direct-IO optimization
3216 disk_offset = base_disk_offset + (ap->a_loffset - base_offset);
3218 if ((disk_offset & HAMMER_OFF_ZONE_MASK) != HAMMER_ZONE_LARGE_DATA) {
3220 * Only large-data zones can be direct-IOd
3223 } else if ((disk_offset & HAMMER_BUFMASK) ||
3224 (last_offset - ap->a_loffset) < blksize) {
3226 * doffsetp is not aligned or the forward run size does
3227 * not cover a whole buffer, disallow the direct I/O.
3234 *ap->a_doffsetp = disk_offset;
3236 *ap->a_runb = ap->a_loffset - base_offset;
3237 KKASSERT(*ap->a_runb >= 0);
3240 *ap->a_runp = last_offset - ap->a_loffset;
3241 KKASSERT(*ap->a_runp >= 0);
3249 * Write to a regular file. Because this is a strategy call the OS is
3250 * trying to actually get data onto the media.
3254 hammer_vop_strategy_write(struct vop_strategy_args *ap)
3256 hammer_record_t record;
3261 int blksize __debugvar;
3267 ip = ap->a_vp->v_data;
3270 blksize = hammer_blocksize(bio->bio_offset);
3271 KKASSERT(bp->b_bufsize == blksize);
3273 if (ip->flags & HAMMER_INODE_RO) {
3274 bp->b_error = EROFS;
3275 bp->b_flags |= B_ERROR;
3280 lwkt_gettoken(&hmp->fs_token);
3283 * Disallow swapcache operation on the vnode buffer if double
3284 * buffering is enabled, the swapcache will get the data via
3285 * the block device buffer.
3287 if (hammer_double_buffer)
3288 bp->b_flags |= B_NOTMETA;
3291 * Interlock with inode destruction (no in-kernel or directory
3292 * topology visibility). If we queue new IO while trying to
3293 * destroy the inode we can deadlock the vtrunc call in
3294 * hammer_inode_unloadable_check().
3296 * Besides, there's no point flushing a bp associated with an
3297 * inode that is being destroyed on-media and has no kernel
3300 if ((ip->flags | ip->sync_flags) &
3301 (HAMMER_INODE_DELETING|HAMMER_INODE_DELETED)) {
3304 lwkt_reltoken(&hmp->fs_token);
3309 * Reserve space and issue a direct-write from the front-end.
3310 * NOTE: The direct_io code will hammer_bread/bcopy smaller
3313 * An in-memory record will be installed to reference the storage
3314 * until the flusher can get to it.
3316 * Since we own the high level bio the front-end will not try to
3317 * do a direct-read until the write completes.
3319 * NOTE: The only time we do not reserve a full-sized buffers
3320 * worth of data is if the file is small. We do not try to
3321 * allocate a fragment (from the small-data zone) at the end of
3322 * an otherwise large file as this can lead to wildly separated
3325 KKASSERT((bio->bio_offset & HAMMER_BUFMASK) == 0);
3326 KKASSERT(bio->bio_offset < ip->ino_data.size);
3327 if (bio->bio_offset || ip->ino_data.size > HAMMER_HBUFSIZE)
3328 bytes = bp->b_bufsize;
3330 bytes = ((int)ip->ino_data.size + 15) & ~15;
3332 record = hammer_ip_add_bulk(ip, bio->bio_offset, bp->b_data,
3336 * B_VFSFLAG1 indicates that a REDO_WRITE entry was generated
3337 * in hammer_vop_write(). We must flag the record so the proper
3338 * REDO_TERM_WRITE entry is generated during the flush.
3341 if (bp->b_flags & B_VFSFLAG1) {
3342 record->flags |= HAMMER_RECF_REDO;
3343 bp->b_flags &= ~B_VFSFLAG1;
3345 if (record->flags & HAMMER_RECF_DEDUPED) {
3347 hammer_ip_replace_bulk(hmp, record);
3350 hammer_io_direct_write(hmp, bio, record);
3352 if (ip->rsv_recs > 1 && hmp->rsv_recs > hammer_limit_recs)
3353 hammer_flush_inode(ip, 0);
3355 bp->b_bio2.bio_offset = NOOFFSET;
3356 bp->b_error = error;
3357 bp->b_flags |= B_ERROR;
3360 lwkt_reltoken(&hmp->fs_token);
3365 * dounlink - disconnect a directory entry
3367 * XXX whiteout support not really in yet
3370 hammer_dounlink(hammer_transaction_t trans, struct nchandle *nch,
3371 struct vnode *dvp, struct ucred *cred,
3372 int flags, int isdir)
3374 struct namecache *ncp;
3378 struct hammer_cursor cursor;
3380 u_int32_t max_iterations;
3384 * Calculate the namekey and setup the key range for the scan. This
3385 * works kinda like a chained hash table where the lower 32 bits
3386 * of the namekey synthesize the chain.
3388 * The key range is inclusive of both key_beg and key_end.
3394 if (dip->flags & HAMMER_INODE_RO)
3397 namekey = hammer_directory_namekey(dip, ncp->nc_name, ncp->nc_nlen,
3400 hammer_init_cursor(trans, &cursor, &dip->cache[1], dip);
3401 cursor.key_beg.localization = dip->obj_localization +
3402 hammer_dir_localization(dip);
3403 cursor.key_beg.obj_id = dip->obj_id;
3404 cursor.key_beg.key = namekey;
3405 cursor.key_beg.create_tid = 0;
3406 cursor.key_beg.delete_tid = 0;
3407 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
3408 cursor.key_beg.obj_type = 0;
3410 cursor.key_end = cursor.key_beg;
3411 cursor.key_end.key += max_iterations;
3412 cursor.asof = dip->obj_asof;
3413 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
3416 * Scan all matching records (the chain), locate the one matching
3417 * the requested path component. info->last_error contains the
3418 * error code on search termination and could be 0, ENOENT, or
3421 * The hammer_ip_*() functions merge in-memory records with on-disk
3422 * records for the purposes of the search.
3424 error = hammer_ip_first(&cursor);
3426 while (error == 0) {
3427 error = hammer_ip_resolve_data(&cursor);
3430 nlen = cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF;
3432 if (ncp->nc_nlen == nlen &&
3433 bcmp(ncp->nc_name, cursor.data->entry.name, nlen) == 0) {
3436 error = hammer_ip_next(&cursor);
3440 * If all is ok we have to get the inode so we can adjust nlinks.
3441 * To avoid a deadlock with the flusher we must release the inode
3442 * lock on the directory when acquiring the inode for the entry.
3444 * If the target is a directory, it must be empty.
3447 hammer_unlock(&cursor.ip->lock);
3448 ip = hammer_get_inode(trans, dip, cursor.data->entry.obj_id,
3450 cursor.data->entry.localization,
3452 hammer_lock_sh(&cursor.ip->lock);
3453 if (error == ENOENT) {
3454 kprintf("HAMMER: WARNING: Removing "
3455 "dirent w/missing inode \"%s\"\n"
3456 "\tobj_id = %016llx\n",
3458 (long long)cursor.data->entry.obj_id);
3463 * If isdir >= 0 we validate that the entry is or is not a
3464 * directory. If isdir < 0 we don't care.
3466 if (error == 0 && isdir >= 0 && ip) {
3468 ip->ino_data.obj_type != HAMMER_OBJTYPE_DIRECTORY) {
3470 } else if (isdir == 0 &&
3471 ip->ino_data.obj_type == HAMMER_OBJTYPE_DIRECTORY) {
3477 * If we are trying to remove a directory the directory must
3480 * The check directory code can loop and deadlock/retry. Our
3481 * own cursor's node locks must be released to avoid a 3-way
3482 * deadlock with the flusher if the check directory code
3485 * If any changes whatsoever have been made to the cursor
3486 * set EDEADLK and retry.
3488 * WARNING: See warnings in hammer_unlock_cursor()
3491 if (error == 0 && ip && ip->ino_data.obj_type ==
3492 HAMMER_OBJTYPE_DIRECTORY) {
3493 hammer_unlock_cursor(&cursor);
3494 error = hammer_ip_check_directory_empty(trans, ip);
3495 hammer_lock_cursor(&cursor);
3496 if (cursor.flags & HAMMER_CURSOR_RETEST) {
3497 kprintf("HAMMER: Warning: avoided deadlock "
3505 * Delete the directory entry.
3507 * WARNING: hammer_ip_del_directory() may have to terminate
3508 * the cursor to avoid a deadlock. It is ok to call
3509 * hammer_done_cursor() twice.
3512 error = hammer_ip_del_directory(trans, &cursor,
3515 hammer_done_cursor(&cursor);
3518 * Tell the namecache that we are now unlinked.
3523 * NOTE: ip->vp, if non-NULL, cannot be directly
3524 * referenced without formally acquiring the
3525 * vp since the vp might have zero refs on it,
3526 * or in the middle of a reclaim, etc.
3528 * NOTE: The cache_setunresolved() can rip the vp
3529 * out from under us since the vp may not have
3530 * any refs, in which case ip->vp will be NULL
3533 while (ip && ip->vp) {
3536 error = hammer_get_vnode(ip, &vp);
3537 if (error == 0 && vp) {
3539 hammer_knote(ip->vp, NOTE_DELETE);
3542 * Don't do this, it can deadlock
3543 * on concurrent rm's of hardlinks.
3544 * Shouldn't be needed any more.
3546 cache_inval_vp(ip->vp, CINV_DESTROY);
3551 kprintf("Debug: HAMMER ip/vp race1 avoided\n");
3555 hammer_rel_inode(ip, 0);
3557 hammer_done_cursor(&cursor);
3559 if (error == EDEADLK)
3565 /************************************************************************
3566 * FIFO AND SPECFS OPS *
3567 ************************************************************************
3571 hammer_vop_fifoclose (struct vop_close_args *ap)
3573 /* XXX update itimes */
3574 return (VOCALL(&fifo_vnode_vops, &ap->a_head));
3578 hammer_vop_fiforead (struct vop_read_args *ap)
3582 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
3583 /* XXX update access time */
3588 hammer_vop_fifowrite (struct vop_write_args *ap)
3592 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
3593 /* XXX update access time */
3599 hammer_vop_fifokqfilter(struct vop_kqfilter_args *ap)
3603 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
3605 error = hammer_vop_kqfilter(ap);
3609 /************************************************************************
3611 ************************************************************************
3614 static void filt_hammerdetach(struct knote *kn);
3615 static int filt_hammerread(struct knote *kn, long hint);
3616 static int filt_hammerwrite(struct knote *kn, long hint);
3617 static int filt_hammervnode(struct knote *kn, long hint);
3619 static struct filterops hammerread_filtops =
3620 { FILTEROP_ISFD | FILTEROP_MPSAFE,
3621 NULL, filt_hammerdetach, filt_hammerread };
3622 static struct filterops hammerwrite_filtops =
3623 { FILTEROP_ISFD | FILTEROP_MPSAFE,
3624 NULL, filt_hammerdetach, filt_hammerwrite };
3625 static struct filterops hammervnode_filtops =
3626 { FILTEROP_ISFD | FILTEROP_MPSAFE,
3627 NULL, filt_hammerdetach, filt_hammervnode };
3631 hammer_vop_kqfilter(struct vop_kqfilter_args *ap)
3633 struct vnode *vp = ap->a_vp;
3634 struct knote *kn = ap->a_kn;
3636 switch (kn->kn_filter) {
3638 kn->kn_fop = &hammerread_filtops;
3641 kn->kn_fop = &hammerwrite_filtops;
3644 kn->kn_fop = &hammervnode_filtops;
3647 return (EOPNOTSUPP);
3650 kn->kn_hook = (caddr_t)vp;
3652 knote_insert(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
3658 filt_hammerdetach(struct knote *kn)
3660 struct vnode *vp = (void *)kn->kn_hook;
3662 knote_remove(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
3666 filt_hammerread(struct knote *kn, long hint)
3668 struct vnode *vp = (void *)kn->kn_hook;
3669 hammer_inode_t ip = VTOI(vp);
3670 hammer_mount_t hmp = ip->hmp;
3673 if (hint == NOTE_REVOKE) {
3674 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
3677 lwkt_gettoken(&hmp->fs_token); /* XXX use per-ip-token */
3678 off = ip->ino_data.size - kn->kn_fp->f_offset;
3679 kn->kn_data = (off < INTPTR_MAX) ? off : INTPTR_MAX;
3680 lwkt_reltoken(&hmp->fs_token);
3681 if (kn->kn_sfflags & NOTE_OLDAPI)
3683 return (kn->kn_data != 0);
3687 filt_hammerwrite(struct knote *kn, long hint)
3689 if (hint == NOTE_REVOKE)
3690 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
3696 filt_hammervnode(struct knote *kn, long hint)
3698 if (kn->kn_sfflags & hint)
3699 kn->kn_fflags |= hint;
3700 if (hint == NOTE_REVOKE) {
3701 kn->kn_flags |= (EV_EOF | EV_NODATA);
3704 return (kn->kn_fflags != 0);