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/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/fcntl.h>
39 #include <sys/namecache.h>
40 #include <sys/vnode.h>
41 #include <sys/lockf.h>
42 #include <sys/event.h>
44 #include <sys/dirent.h>
46 #include <vm/vm_extern.h>
47 #include <vm/swap_pager.h>
48 #include <vfs/fifofs/fifo.h>
55 /*static int hammer_vop_vnoperate(struct vop_generic_args *);*/
56 static int hammer_vop_fsync(struct vop_fsync_args *);
57 static int hammer_vop_read(struct vop_read_args *);
58 static int hammer_vop_write(struct vop_write_args *);
59 static int hammer_vop_access(struct vop_access_args *);
60 static int hammer_vop_advlock(struct vop_advlock_args *);
61 static int hammer_vop_close(struct vop_close_args *);
62 static int hammer_vop_ncreate(struct vop_ncreate_args *);
63 static int hammer_vop_getattr(struct vop_getattr_args *);
64 static int hammer_vop_nresolve(struct vop_nresolve_args *);
65 static int hammer_vop_nlookupdotdot(struct vop_nlookupdotdot_args *);
66 static int hammer_vop_nlink(struct vop_nlink_args *);
67 static int hammer_vop_nmkdir(struct vop_nmkdir_args *);
68 static int hammer_vop_nmknod(struct vop_nmknod_args *);
69 static int hammer_vop_open(struct vop_open_args *);
70 static int hammer_vop_print(struct vop_print_args *);
71 static int hammer_vop_readdir(struct vop_readdir_args *);
72 static int hammer_vop_readlink(struct vop_readlink_args *);
73 static int hammer_vop_nremove(struct vop_nremove_args *);
74 static int hammer_vop_nrename(struct vop_nrename_args *);
75 static int hammer_vop_nrmdir(struct vop_nrmdir_args *);
76 static int hammer_vop_markatime(struct vop_markatime_args *);
77 static int hammer_vop_setattr(struct vop_setattr_args *);
78 static int hammer_vop_strategy(struct vop_strategy_args *);
79 static int hammer_vop_bmap(struct vop_bmap_args *ap);
80 static int hammer_vop_nsymlink(struct vop_nsymlink_args *);
81 static int hammer_vop_nwhiteout(struct vop_nwhiteout_args *);
82 static int hammer_vop_ioctl(struct vop_ioctl_args *);
83 static int hammer_vop_mountctl(struct vop_mountctl_args *);
84 static int hammer_vop_kqfilter (struct vop_kqfilter_args *);
86 static int hammer_vop_fifoclose (struct vop_close_args *);
87 static int hammer_vop_fiforead (struct vop_read_args *);
88 static int hammer_vop_fifowrite (struct vop_write_args *);
89 static int hammer_vop_fifokqfilter (struct vop_kqfilter_args *);
91 struct vop_ops hammer_vnode_vops = {
92 .vop_default = vop_defaultop,
93 .vop_fsync = hammer_vop_fsync,
94 .vop_getpages = vop_stdgetpages,
95 .vop_putpages = vop_stdputpages,
96 .vop_read = hammer_vop_read,
97 .vop_write = hammer_vop_write,
98 .vop_access = hammer_vop_access,
99 .vop_advlock = hammer_vop_advlock,
100 .vop_close = hammer_vop_close,
101 .vop_ncreate = hammer_vop_ncreate,
102 .vop_getattr = hammer_vop_getattr,
103 .vop_inactive = hammer_vop_inactive,
104 .vop_reclaim = hammer_vop_reclaim,
105 .vop_nresolve = hammer_vop_nresolve,
106 .vop_nlookupdotdot = hammer_vop_nlookupdotdot,
107 .vop_nlink = hammer_vop_nlink,
108 .vop_nmkdir = hammer_vop_nmkdir,
109 .vop_nmknod = hammer_vop_nmknod,
110 .vop_open = hammer_vop_open,
111 .vop_pathconf = vop_stdpathconf,
112 .vop_print = hammer_vop_print,
113 .vop_readdir = hammer_vop_readdir,
114 .vop_readlink = hammer_vop_readlink,
115 .vop_nremove = hammer_vop_nremove,
116 .vop_nrename = hammer_vop_nrename,
117 .vop_nrmdir = hammer_vop_nrmdir,
118 .vop_markatime = hammer_vop_markatime,
119 .vop_setattr = hammer_vop_setattr,
120 .vop_bmap = hammer_vop_bmap,
121 .vop_strategy = hammer_vop_strategy,
122 .vop_nsymlink = hammer_vop_nsymlink,
123 .vop_nwhiteout = hammer_vop_nwhiteout,
124 .vop_ioctl = hammer_vop_ioctl,
125 .vop_mountctl = hammer_vop_mountctl,
126 .vop_kqfilter = hammer_vop_kqfilter
129 struct vop_ops hammer_spec_vops = {
130 .vop_default = vop_defaultop,
131 .vop_fsync = hammer_vop_fsync,
132 .vop_read = vop_stdnoread,
133 .vop_write = vop_stdnowrite,
134 .vop_access = hammer_vop_access,
135 .vop_close = hammer_vop_close,
136 .vop_markatime = hammer_vop_markatime,
137 .vop_getattr = hammer_vop_getattr,
138 .vop_inactive = hammer_vop_inactive,
139 .vop_reclaim = hammer_vop_reclaim,
140 .vop_setattr = hammer_vop_setattr
143 struct vop_ops hammer_fifo_vops = {
144 .vop_default = fifo_vnoperate,
145 .vop_fsync = hammer_vop_fsync,
146 .vop_read = hammer_vop_fiforead,
147 .vop_write = hammer_vop_fifowrite,
148 .vop_access = hammer_vop_access,
149 .vop_close = hammer_vop_fifoclose,
150 .vop_markatime = hammer_vop_markatime,
151 .vop_getattr = hammer_vop_getattr,
152 .vop_inactive = hammer_vop_inactive,
153 .vop_reclaim = hammer_vop_reclaim,
154 .vop_setattr = hammer_vop_setattr,
155 .vop_kqfilter = hammer_vop_fifokqfilter
160 hammer_knote(struct vnode *vp, int flags)
163 KNOTE(&vp->v_pollinfo.vpi_kqinfo.ki_note, flags);
166 #ifdef DEBUG_TRUNCATE
167 struct hammer_inode *HammerTruncIp;
170 static int hammer_dounlink(hammer_transaction_t trans, struct nchandle *nch,
171 struct vnode *dvp, struct ucred *cred,
172 int flags, int isdir);
173 static int hammer_vop_strategy_read(struct vop_strategy_args *ap);
174 static int hammer_vop_strategy_write(struct vop_strategy_args *ap);
179 hammer_vop_vnoperate(struct vop_generic_args *)
181 return (VOCALL(&hammer_vnode_vops, ap));
186 * hammer_vop_fsync { vp, waitfor }
188 * fsync() an inode to disk and wait for it to be completely committed
189 * such that the information would not be undone if a crash occured after
192 * NOTE: HAMMER's fsync()'s are going to remain expensive until we implement
193 * a REDO log. A sysctl is provided to relax HAMMER's fsync()
196 * Ultimately the combination of a REDO log and use of fast storage
197 * to front-end cluster caches will make fsync fast, but it aint
198 * here yet. And, in anycase, we need real transactional
199 * all-or-nothing features which are not restricted to a single file.
203 hammer_vop_fsync(struct vop_fsync_args *ap)
205 hammer_inode_t ip = VTOI(ap->a_vp);
206 hammer_mount_t hmp = ip->hmp;
207 int waitfor = ap->a_waitfor;
210 lwkt_gettoken(&hmp->fs_token);
213 * Fsync rule relaxation (default is either full synchronous flush
214 * or REDO semantics with synchronous flush).
216 if (ap->a_flags & VOP_FSYNC_SYSCALL) {
217 switch(hammer_fsync_mode) {
220 /* no REDO, full synchronous flush */
224 /* no REDO, full asynchronous flush */
225 if (waitfor == MNT_WAIT)
226 waitfor = MNT_NOWAIT;
229 /* REDO semantics, synchronous flush */
230 if (hmp->version < HAMMER_VOL_VERSION_FOUR)
232 mode = HAMMER_FLUSH_UNDOS_AUTO;
235 /* REDO semantics, relaxed asynchronous flush */
236 if (hmp->version < HAMMER_VOL_VERSION_FOUR)
238 mode = HAMMER_FLUSH_UNDOS_RELAXED;
239 if (waitfor == MNT_WAIT)
240 waitfor = MNT_NOWAIT;
243 /* ignore the fsync() system call */
244 lwkt_reltoken(&hmp->fs_token);
247 /* we have to do something */
248 mode = HAMMER_FLUSH_UNDOS_RELAXED;
249 if (waitfor == MNT_WAIT)
250 waitfor = MNT_NOWAIT;
255 * Fast fsync only needs to flush the UNDO/REDO fifo if
256 * HAMMER_INODE_REDO is non-zero and the only modifications
257 * made to the file are write or write-extends.
259 if ((ip->flags & HAMMER_INODE_REDO) &&
260 (ip->flags & HAMMER_INODE_MODMASK_NOREDO) == 0) {
261 ++hammer_count_fsyncs;
262 hammer_flusher_flush_undos(hmp, mode);
264 if (ip->vp && (ip->flags & HAMMER_INODE_MODMASK) == 0)
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 * Attempt to release the vnode while waiting for the inode to
292 * finish flushing. This can really mess up inactive->reclaim
293 * sequences so only do it if the vnode is active.
295 * WARNING! The VX lock functions must be used. vn_lock() will
296 * fail when this is part of a VOP_RECLAIM sequence.
298 ++hammer_count_fsyncs;
299 vfsync(ap->a_vp, waitfor, 1, NULL, NULL);
300 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
301 if (waitfor == MNT_WAIT) {
304 if ((ap->a_vp->v_flag & VRECLAIMED) == 0) {
310 hammer_wait_inode(ip);
314 if (ip->vp && (ip->flags & HAMMER_INODE_MODMASK) == 0)
316 lwkt_reltoken(&hmp->fs_token);
321 * hammer_vop_read { vp, uio, ioflag, cred }
323 * MPSAFE (for the cache safe does not require fs_token)
327 hammer_vop_read(struct vop_read_args *ap)
329 struct hammer_transaction trans;
344 if (ap->a_vp->v_type != VREG)
353 * Attempt to shortcut directly to the VM object using lwbufs.
354 * This is much faster than instantiating buffer cache buffers.
356 resid = uio->uio_resid;
357 error = vop_helper_read_shortcut(ap);
358 hammer_stats_file_read += resid - uio->uio_resid;
361 if (uio->uio_resid == 0)
365 * Allow the UIO's size to override the sequential heuristic.
367 blksize = hammer_blocksize(uio->uio_offset);
368 seqcount = (uio->uio_resid + (BKVASIZE - 1)) / BKVASIZE;
369 ioseqcount = (ap->a_ioflag >> 16);
370 if (seqcount < ioseqcount)
371 seqcount = ioseqcount;
374 * If reading or writing a huge amount of data we have to break
375 * atomicy and allow the operation to be interrupted by a signal
376 * or it can DOS the machine.
378 bigread = (uio->uio_resid > 100 * 1024 * 1024);
381 * Access the data typically in HAMMER_BUFSIZE blocks via the
382 * buffer cache, but HAMMER may use a variable block size based
385 * XXX Temporary hack, delay the start transaction while we remain
386 * MPSAFE. NOTE: ino_data.size cannot change while vnode is
389 while (uio->uio_resid > 0 && uio->uio_offset < ip->ino_data.size) {
393 blksize = hammer_blocksize(uio->uio_offset);
394 offset = (int)uio->uio_offset & (blksize - 1);
395 base_offset = uio->uio_offset - offset;
397 if (bigread && (error = hammer_signal_check(ip->hmp)) != 0)
403 bp = getblk(ap->a_vp, base_offset, blksize, 0, 0);
404 if ((bp->b_flags & (B_INVAL | B_CACHE | B_RAM)) == B_CACHE) {
405 bp->b_flags &= ~B_AGE;
409 if (ap->a_ioflag & IO_NRDELAY) {
411 return (EWOULDBLOCK);
417 if (got_trans == 0) {
418 hammer_start_transaction(&trans, ip->hmp);
423 * NOTE: A valid bp has already been acquired, but was not
426 if (hammer_cluster_enable) {
428 * Use file_limit to prevent cluster_read() from
429 * creating buffers of the wrong block size past
432 file_limit = ip->ino_data.size;
433 if (base_offset < HAMMER_XDEMARC &&
434 file_limit > HAMMER_XDEMARC) {
435 file_limit = HAMMER_XDEMARC;
437 error = cluster_readx(ap->a_vp,
438 file_limit, base_offset,
439 blksize, uio->uio_resid,
440 seqcount * BKVASIZE, &bp);
442 error = breadnx(ap->a_vp, base_offset, blksize,
450 if ((hammer_debug_io & 0x0001) && (bp->b_flags & B_IODEBUG)) {
451 kprintf("doff %016jx read file %016jx@%016jx\n",
452 (intmax_t)bp->b_bio2.bio_offset,
453 (intmax_t)ip->obj_id,
454 (intmax_t)bp->b_loffset);
456 bp->b_flags &= ~B_IODEBUG;
457 if (blksize == HAMMER_XBUFSIZE)
458 bp->b_flags |= B_CLUSTEROK;
460 n = blksize - offset;
461 if (n > uio->uio_resid)
463 if (n > ip->ino_data.size - uio->uio_offset)
464 n = (int)(ip->ino_data.size - uio->uio_offset);
467 * Set B_AGE, data has a lower priority than meta-data.
469 * Use a hold/unlock/drop sequence to run the uiomove
470 * with the buffer unlocked, avoiding deadlocks against
471 * read()s on mmap()'d spaces.
473 bp->b_flags |= B_AGE;
474 error = uiomovebp(bp, (char *)bp->b_data + offset, n, uio);
479 hammer_stats_file_read += n;
485 * Try to update the atime with just the inode lock for maximum
486 * concurrency. If we can't shortcut it we have to get the full
489 if (got_trans == 0 && hammer_update_atime_quick(ip) < 0) {
490 hammer_start_transaction(&trans, ip->hmp);
495 if ((ip->flags & HAMMER_INODE_RO) == 0 &&
496 (ip->hmp->mp->mnt_flag & MNT_NOATIME) == 0) {
497 lwkt_gettoken(&hmp->fs_token);
498 ip->ino_data.atime = trans.time;
499 hammer_modify_inode(&trans, ip, HAMMER_INODE_ATIME);
500 hammer_done_transaction(&trans);
501 lwkt_reltoken(&hmp->fs_token);
503 hammer_done_transaction(&trans);
510 * hammer_vop_write { vp, uio, ioflag, cred }
514 hammer_vop_write(struct vop_write_args *ap)
516 struct hammer_transaction trans;
517 struct hammer_inode *ip;
532 if (ap->a_vp->v_type != VREG)
538 seqcount = ap->a_ioflag >> 16;
540 if (ip->flags & HAMMER_INODE_RO)
544 * Create a transaction to cover the operations we perform.
546 hammer_start_transaction(&trans, hmp);
552 if (ap->a_ioflag & IO_APPEND)
553 uio->uio_offset = ip->ino_data.size;
556 * Check for illegal write offsets. Valid range is 0...2^63-1.
558 * NOTE: the base_off assignment is required to work around what
559 * I consider to be a GCC-4 optimization bug.
561 if (uio->uio_offset < 0) {
562 hammer_done_transaction(&trans);
565 base_offset = uio->uio_offset + uio->uio_resid; /* work around gcc-4 */
566 if (uio->uio_resid > 0 && base_offset <= uio->uio_offset) {
567 hammer_done_transaction(&trans);
571 if (uio->uio_resid > 0 && (td = uio->uio_td) != NULL && td->td_proc &&
572 base_offset > td->td_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
573 hammer_done_transaction(&trans);
574 lwpsignal(td->td_proc, td->td_lwp, SIGXFSZ);
579 * If reading or writing a huge amount of data we have to break
580 * atomicy and allow the operation to be interrupted by a signal
581 * or it can DOS the machine.
583 * Preset redo_count so we stop generating REDOs earlier if the
586 * redo_count is heuristical, SMP races are ok
588 bigwrite = (uio->uio_resid > 100 * 1024 * 1024);
589 if ((ip->flags & HAMMER_INODE_REDO) &&
590 ip->redo_count < hammer_limit_redo) {
591 ip->redo_count += uio->uio_resid;
595 * Access the data typically in HAMMER_BUFSIZE blocks via the
596 * buffer cache, but HAMMER may use a variable block size based
599 while (uio->uio_resid > 0) {
607 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_WRITE)) != 0)
609 if (bigwrite && (error = hammer_signal_check(hmp)) != 0)
612 blksize = hammer_blocksize(uio->uio_offset);
615 * Control the number of pending records associated with
616 * this inode. If too many have accumulated start a
617 * flush. Try to maintain a pipeline with the flusher.
619 * NOTE: It is possible for other sources to grow the
620 * records but not necessarily issue another flush,
621 * so use a timeout and ensure that a re-flush occurs.
623 if (ip->rsv_recs >= hammer_limit_inode_recs) {
624 lwkt_gettoken(&hmp->fs_token);
625 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
626 while (ip->rsv_recs >= hammer_limit_inode_recs * 2) {
627 ip->flags |= HAMMER_INODE_RECSW;
628 tsleep(&ip->rsv_recs, 0, "hmrwww", hz);
629 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
631 lwkt_reltoken(&hmp->fs_token);
635 * Do not allow HAMMER to blow out the buffer cache. Very
636 * large UIOs can lockout other processes due to bwillwrite()
639 * The hammer inode is not locked during these operations.
640 * The vnode is locked which can interfere with the pageout
641 * daemon for non-UIO_NOCOPY writes but should not interfere
642 * with the buffer cache. Even so, we cannot afford to
643 * allow the pageout daemon to build up too many dirty buffer
646 * Only call this if we aren't being recursively called from
647 * a virtual disk device (vn), else we may deadlock.
649 if ((ap->a_ioflag & IO_RECURSE) == 0)
653 * Calculate the blocksize at the current offset and figure
654 * out how much we can actually write.
656 blkmask = blksize - 1;
657 offset = (int)uio->uio_offset & blkmask;
658 base_offset = uio->uio_offset & ~(int64_t)blkmask;
659 n = blksize - offset;
660 if (n > uio->uio_resid) {
666 nsize = uio->uio_offset + n;
667 if (nsize > ip->ino_data.size) {
668 if (uio->uio_offset > ip->ino_data.size)
672 nvextendbuf(ap->a_vp,
675 hammer_blocksize(ip->ino_data.size),
676 hammer_blocksize(nsize),
677 hammer_blockoff(ip->ino_data.size),
678 hammer_blockoff(nsize),
681 kflags |= NOTE_EXTEND;
684 if (uio->uio_segflg == UIO_NOCOPY) {
686 * Issuing a write with the same data backing the
687 * buffer. Instantiate the buffer to collect the
688 * backing vm pages, then read-in any missing bits.
690 * This case is used by vop_stdputpages().
692 bp = getblk(ap->a_vp, base_offset,
693 blksize, GETBLK_BHEAVY, 0);
694 if ((bp->b_flags & B_CACHE) == 0) {
696 error = bread(ap->a_vp, base_offset,
699 } else if (offset == 0 && uio->uio_resid >= blksize) {
701 * Even though we are entirely overwriting the buffer
702 * we may still have to zero it out to avoid a
703 * mmap/write visibility issue.
705 bp = getblk(ap->a_vp, base_offset, blksize, GETBLK_BHEAVY, 0);
706 if ((bp->b_flags & B_CACHE) == 0)
708 } else if (base_offset >= ip->ino_data.size) {
710 * If the base offset of the buffer is beyond the
711 * file EOF, we don't have to issue a read.
713 bp = getblk(ap->a_vp, base_offset,
714 blksize, GETBLK_BHEAVY, 0);
718 * Partial overwrite, read in any missing bits then
719 * replace the portion being written.
721 error = bread(ap->a_vp, base_offset, blksize, &bp);
726 error = uiomovebp(bp, bp->b_data + offset, n, uio);
728 lwkt_gettoken(&hmp->fs_token);
731 * Generate REDO records if enabled and redo_count will not
732 * exceeded the limit.
734 * If redo_count exceeds the limit we stop generating records
735 * and clear HAMMER_INODE_REDO. This will cause the next
736 * fsync() to do a full meta-data sync instead of just an
737 * UNDO/REDO fifo update.
739 * When clearing HAMMER_INODE_REDO any pre-existing REDOs
740 * will still be tracked. The tracks will be terminated
741 * when the related meta-data (including possible data
742 * modifications which are not tracked via REDO) is
745 if ((ip->flags & HAMMER_INODE_REDO) && error == 0) {
746 if (ip->redo_count < hammer_limit_redo) {
747 bp->b_flags |= B_VFSFLAG1;
748 error = hammer_generate_redo(&trans, ip,
749 base_offset + offset,
754 ip->flags &= ~HAMMER_INODE_REDO;
759 * If we screwed up we have to undo any VM size changes we
765 nvtruncbuf(ap->a_vp, ip->ino_data.size,
766 hammer_blocksize(ip->ino_data.size),
767 hammer_blockoff(ip->ino_data.size),
770 lwkt_reltoken(&hmp->fs_token);
773 kflags |= NOTE_WRITE;
774 hammer_stats_file_write += n;
775 if (blksize == HAMMER_XBUFSIZE)
776 bp->b_flags |= B_CLUSTEROK;
777 if (ip->ino_data.size < uio->uio_offset) {
778 ip->ino_data.size = uio->uio_offset;
779 flags = HAMMER_INODE_SDIRTY;
783 ip->ino_data.mtime = trans.time;
784 flags |= HAMMER_INODE_MTIME | HAMMER_INODE_BUFS;
785 hammer_modify_inode(&trans, ip, flags);
788 * Once we dirty the buffer any cached zone-X offset
789 * becomes invalid. HAMMER NOTE: no-history mode cannot
790 * allow overwriting over the same data sector unless
791 * we provide UNDOs for the old data, which we don't.
793 bp->b_bio2.bio_offset = NOOFFSET;
795 lwkt_reltoken(&hmp->fs_token);
798 * Final buffer disposition.
800 * Because meta-data updates are deferred, HAMMER is
801 * especially sensitive to excessive bdwrite()s because
802 * the I/O stream is not broken up by disk reads. So the
803 * buffer cache simply cannot keep up.
805 * WARNING! blksize is variable. cluster_write() is
806 * expected to not blow up if it encounters
807 * buffers that do not match the passed blksize.
809 * NOTE! Hammer shouldn't need to bawrite()/cluster_write().
810 * The ip->rsv_recs check should burst-flush the data.
811 * If we queue it immediately the buf could be left
812 * locked on the device queue for a very long time.
814 * However, failing to flush a dirty buffer out when
815 * issued from the pageout daemon can result in a low
816 * memory deadlock against bio_page_alloc(), so we
817 * have to bawrite() on IO_ASYNC as well.
819 * NOTE! To avoid degenerate stalls due to mismatched block
820 * sizes we only honor IO_DIRECT on the write which
821 * abuts the end of the buffer. However, we must
822 * honor IO_SYNC in case someone is silly enough to
823 * configure a HAMMER file as swap, or when HAMMER
824 * is serving NFS (for commits). Ick ick.
826 bp->b_flags |= B_AGE;
827 if (blksize == HAMMER_XBUFSIZE)
828 bp->b_flags |= B_CLUSTEROK;
830 if (ap->a_ioflag & IO_SYNC) {
832 } else if ((ap->a_ioflag & IO_DIRECT) && endofblk) {
834 } else if (ap->a_ioflag & IO_ASYNC) {
836 } else if (hammer_cluster_enable &&
837 !(ap->a_vp->v_mount->mnt_flag & MNT_NOCLUSTERW)) {
838 if (base_offset < HAMMER_XDEMARC)
839 cluster_eof = hammer_blockdemarc(base_offset,
842 cluster_eof = ip->ino_data.size;
843 cluster_write(bp, cluster_eof, blksize, seqcount);
848 hammer_done_transaction(&trans);
849 hammer_knote(ap->a_vp, kflags);
855 * hammer_vop_access { vp, mode, cred }
857 * MPSAFE - does not require fs_token
861 hammer_vop_access(struct vop_access_args *ap)
863 struct hammer_inode *ip = VTOI(ap->a_vp);
868 ++hammer_stats_file_iopsr;
869 uid = hammer_to_unix_xid(&ip->ino_data.uid);
870 gid = hammer_to_unix_xid(&ip->ino_data.gid);
872 error = vop_helper_access(ap, uid, gid, ip->ino_data.mode,
873 ip->ino_data.uflags);
878 * hammer_vop_advlock { vp, id, op, fl, flags }
880 * MPSAFE - does not require fs_token
884 hammer_vop_advlock(struct vop_advlock_args *ap)
886 hammer_inode_t ip = VTOI(ap->a_vp);
888 return (lf_advlock(ap, &ip->advlock, ip->ino_data.size));
892 * hammer_vop_close { vp, fflag }
894 * We can only sync-on-close for normal closes. XXX disabled for now.
898 hammer_vop_close(struct vop_close_args *ap)
901 struct vnode *vp = ap->a_vp;
902 hammer_inode_t ip = VTOI(vp);
904 if (ip->flags & (HAMMER_INODE_CLOSESYNC|HAMMER_INODE_CLOSEASYNC)) {
905 if (vn_islocked(vp) == LK_EXCLUSIVE &&
906 (vp->v_flag & (VINACTIVE|VRECLAIMED)) == 0) {
907 if (ip->flags & HAMMER_INODE_CLOSESYNC)
910 waitfor = MNT_NOWAIT;
911 ip->flags &= ~(HAMMER_INODE_CLOSESYNC |
912 HAMMER_INODE_CLOSEASYNC);
913 VOP_FSYNC(vp, MNT_NOWAIT, waitfor);
917 return (vop_stdclose(ap));
921 * hammer_vop_ncreate { nch, dvp, vpp, cred, vap }
923 * The operating system has already ensured that the directory entry
924 * does not exist and done all appropriate namespace locking.
928 hammer_vop_ncreate(struct vop_ncreate_args *ap)
930 struct hammer_transaction trans;
931 struct hammer_inode *dip;
932 struct hammer_inode *nip;
933 struct nchandle *nch;
938 dip = VTOI(ap->a_dvp);
941 if (dip->flags & HAMMER_INODE_RO)
943 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0)
947 * Create a transaction to cover the operations we perform.
949 lwkt_gettoken(&hmp->fs_token);
950 hammer_start_transaction(&trans, hmp);
951 ++hammer_stats_file_iopsw;
954 * Create a new filesystem object of the requested type. The
955 * returned inode will be referenced and shared-locked to prevent
956 * it from being moved to the flusher.
958 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
959 dip, nch->ncp->nc_name, nch->ncp->nc_nlen,
962 hkprintf("hammer_create_inode error %d\n", error);
963 hammer_done_transaction(&trans);
965 lwkt_reltoken(&hmp->fs_token);
970 * Add the new filesystem object to the directory. This will also
971 * bump the inode's link count.
973 error = hammer_ip_add_directory(&trans, dip,
974 nch->ncp->nc_name, nch->ncp->nc_nlen,
977 hkprintf("hammer_ip_add_directory error %d\n", error);
983 hammer_rel_inode(nip, 0);
984 hammer_done_transaction(&trans);
987 error = hammer_get_vnode(nip, ap->a_vpp);
988 hammer_done_transaction(&trans);
989 hammer_rel_inode(nip, 0);
991 cache_setunresolved(ap->a_nch);
992 cache_setvp(ap->a_nch, *ap->a_vpp);
994 hammer_knote(ap->a_dvp, NOTE_WRITE);
996 lwkt_reltoken(&hmp->fs_token);
1001 * hammer_vop_getattr { vp, vap }
1003 * Retrieve an inode's attribute information. When accessing inodes
1004 * historically we fake the atime field to ensure consistent results.
1005 * The atime field is stored in the B-Tree element and allowed to be
1006 * updated without cycling the element.
1008 * MPSAFE - does not require fs_token
1012 hammer_vop_getattr(struct vop_getattr_args *ap)
1014 struct hammer_inode *ip = VTOI(ap->a_vp);
1015 struct vattr *vap = ap->a_vap;
1018 * We want the fsid to be different when accessing a filesystem
1019 * with different as-of's so programs like diff don't think
1020 * the files are the same.
1022 * We also want the fsid to be the same when comparing snapshots,
1023 * or when comparing mirrors (which might be backed by different
1024 * physical devices). HAMMER fsids are based on the PFS's
1025 * shared_uuid field.
1027 * XXX there is a chance of collision here. The va_fsid reported
1028 * by stat is different from the more involved fsid used in the
1031 ++hammer_stats_file_iopsr;
1032 hammer_lock_sh(&ip->lock);
1033 vap->va_fsid = ip->pfsm->fsid_udev ^ (u_int32_t)ip->obj_asof ^
1034 (u_int32_t)(ip->obj_asof >> 32);
1036 vap->va_fileid = ip->ino_leaf.base.obj_id;
1037 vap->va_mode = ip->ino_data.mode;
1038 vap->va_nlink = ip->ino_data.nlinks;
1039 vap->va_uid = hammer_to_unix_xid(&ip->ino_data.uid);
1040 vap->va_gid = hammer_to_unix_xid(&ip->ino_data.gid);
1043 vap->va_size = ip->ino_data.size;
1046 * Special case for @@PFS softlinks. The actual size of the
1047 * expanded softlink is "@@0x%016llx:%05d" == 26 bytes.
1048 * or for MAX_TID is "@@-1:%05d" == 10 bytes.
1050 * Note that userspace hammer command does not allow users to
1051 * create a @@PFS softlink under an existing other PFS (id!=0)
1052 * so the ip localization here for @@PFS softlink is always 0.
1054 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_SOFTLINK &&
1055 ip->ino_data.size == 10 &&
1056 ip->obj_asof == HAMMER_MAX_TID &&
1057 ip->obj_localization == 0 &&
1058 strncmp(ip->ino_data.ext.symlink, "@@PFS", 5) == 0) {
1059 if (ip->pfsm->pfsd.mirror_flags & HAMMER_PFSD_SLAVE)
1066 * We must provide a consistent atime and mtime for snapshots
1067 * so people can do a 'tar cf - ... | md5' on them and get
1068 * consistent results.
1070 if (ip->flags & HAMMER_INODE_RO) {
1071 hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_atime);
1072 hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_mtime);
1074 hammer_time_to_timespec(ip->ino_data.atime, &vap->va_atime);
1075 hammer_time_to_timespec(ip->ino_data.mtime, &vap->va_mtime);
1077 hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_ctime);
1078 vap->va_flags = ip->ino_data.uflags;
1079 vap->va_gen = 1; /* hammer inums are unique for all time */
1080 vap->va_blocksize = HAMMER_BUFSIZE;
1081 if (ip->ino_data.size >= HAMMER_XDEMARC) {
1082 vap->va_bytes = (ip->ino_data.size + HAMMER_XBUFMASK64) &
1084 } else if (ip->ino_data.size > HAMMER_BUFSIZE / 2) {
1085 vap->va_bytes = (ip->ino_data.size + HAMMER_BUFMASK64) &
1088 vap->va_bytes = (ip->ino_data.size + 15) & ~15;
1091 vap->va_type = hammer_get_vnode_type(ip->ino_data.obj_type);
1092 vap->va_filerev = 0; /* XXX */
1093 vap->va_uid_uuid = ip->ino_data.uid;
1094 vap->va_gid_uuid = ip->ino_data.gid;
1095 vap->va_fsid_uuid = ip->hmp->fsid;
1096 vap->va_vaflags = VA_UID_UUID_VALID | VA_GID_UUID_VALID |
1099 switch (ip->ino_data.obj_type) {
1100 case HAMMER_OBJTYPE_CDEV:
1101 case HAMMER_OBJTYPE_BDEV:
1102 vap->va_rmajor = ip->ino_data.rmajor;
1103 vap->va_rminor = ip->ino_data.rminor;
1108 hammer_unlock(&ip->lock);
1113 * hammer_vop_nresolve { nch, dvp, cred }
1115 * Locate the requested directory entry.
1119 hammer_vop_nresolve(struct vop_nresolve_args *ap)
1121 struct hammer_transaction trans;
1122 struct namecache *ncp;
1127 struct hammer_cursor cursor;
1136 u_int32_t localization;
1137 u_int32_t max_iterations;
1140 * Misc initialization, plus handle as-of name extensions. Look for
1141 * the '@@' extension. Note that as-of files and directories cannot
1144 dip = VTOI(ap->a_dvp);
1145 ncp = ap->a_nch->ncp;
1146 asof = dip->obj_asof;
1147 localization = dip->obj_localization; /* for code consistency */
1148 nlen = ncp->nc_nlen;
1149 flags = dip->flags & HAMMER_INODE_RO;
1153 lwkt_gettoken(&hmp->fs_token);
1154 hammer_simple_transaction(&trans, hmp);
1155 ++hammer_stats_file_iopsr;
1157 for (i = 0; i < nlen; ++i) {
1158 if (ncp->nc_name[i] == '@' && ncp->nc_name[i+1] == '@') {
1159 error = hammer_str_to_tid(ncp->nc_name + i + 2,
1160 &ispfs, &asof, &localization);
1165 if (asof != HAMMER_MAX_TID)
1166 flags |= HAMMER_INODE_RO;
1173 * If this is a PFS softlink we dive into the PFS
1175 if (ispfs && nlen == 0) {
1176 ip = hammer_get_inode(&trans, dip, HAMMER_OBJID_ROOT,
1180 error = hammer_get_vnode(ip, &vp);
1181 hammer_rel_inode(ip, 0);
1187 cache_setvp(ap->a_nch, vp);
1194 * If there is no path component the time extension is relative to dip.
1195 * e.g. "fubar/@@<snapshot>"
1197 * "." is handled by the kernel, but ".@@<snapshot>" is not.
1198 * e.g. "fubar/.@@<snapshot>"
1200 * ".." is handled by the kernel. We do not currently handle
1203 if (nlen == 0 || (nlen == 1 && ncp->nc_name[0] == '.')) {
1204 ip = hammer_get_inode(&trans, dip, dip->obj_id,
1205 asof, dip->obj_localization,
1208 error = hammer_get_vnode(ip, &vp);
1209 hammer_rel_inode(ip, 0);
1215 cache_setvp(ap->a_nch, vp);
1222 * Calculate the namekey and setup the key range for the scan. This
1223 * works kinda like a chained hash table where the lower 32 bits
1224 * of the namekey synthesize the chain.
1226 * The key range is inclusive of both key_beg and key_end.
1228 namekey = hammer_directory_namekey(dip, ncp->nc_name, nlen,
1231 error = hammer_init_cursor(&trans, &cursor, &dip->cache[1], dip);
1232 cursor.key_beg.localization = dip->obj_localization +
1233 hammer_dir_localization(dip);
1234 cursor.key_beg.obj_id = dip->obj_id;
1235 cursor.key_beg.key = namekey;
1236 cursor.key_beg.create_tid = 0;
1237 cursor.key_beg.delete_tid = 0;
1238 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1239 cursor.key_beg.obj_type = 0;
1241 cursor.key_end = cursor.key_beg;
1242 cursor.key_end.key += max_iterations;
1244 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
1247 * Scan all matching records (the chain), locate the one matching
1248 * the requested path component.
1250 * The hammer_ip_*() functions merge in-memory records with on-disk
1251 * records for the purposes of the search.
1254 localization = HAMMER_DEF_LOCALIZATION;
1257 error = hammer_ip_first(&cursor);
1258 while (error == 0) {
1259 error = hammer_ip_resolve_data(&cursor);
1262 if (nlen == cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF &&
1263 bcmp(ncp->nc_name, cursor.data->entry.name, nlen) == 0) {
1264 obj_id = cursor.data->entry.obj_id;
1265 localization = cursor.data->entry.localization;
1268 error = hammer_ip_next(&cursor);
1271 hammer_done_cursor(&cursor);
1274 * Lookup the obj_id. This should always succeed. If it does not
1275 * the filesystem may be damaged and we return a dummy inode.
1278 ip = hammer_get_inode(&trans, dip, obj_id,
1281 if (error == ENOENT) {
1282 kprintf("HAMMER: WARNING: Missing "
1283 "inode for dirent \"%s\"\n"
1284 "\tobj_id = %016llx, asof=%016llx, lo=%08x\n",
1286 (long long)obj_id, (long long)asof,
1289 ip = hammer_get_dummy_inode(&trans, dip, obj_id,
1294 error = hammer_get_vnode(ip, &vp);
1295 hammer_rel_inode(ip, 0);
1301 cache_setvp(ap->a_nch, vp);
1304 } else if (error == ENOENT) {
1305 cache_setvp(ap->a_nch, NULL);
1308 hammer_done_transaction(&trans);
1309 lwkt_reltoken(&hmp->fs_token);
1314 * hammer_vop_nlookupdotdot { dvp, vpp, cred }
1316 * Locate the parent directory of a directory vnode.
1318 * dvp is referenced but not locked. *vpp must be returned referenced and
1319 * locked. A parent_obj_id of 0 does not necessarily indicate that we are
1320 * at the root, instead it could indicate that the directory we were in was
1323 * NOTE: as-of sequences are not linked into the directory structure. If
1324 * we are at the root with a different asof then the mount point, reload
1325 * the same directory with the mount point's asof. I'm not sure what this
1326 * will do to NFS. We encode ASOF stamps in NFS file handles so it might not
1327 * get confused, but it hasn't been tested.
1331 hammer_vop_nlookupdotdot(struct vop_nlookupdotdot_args *ap)
1333 struct hammer_transaction trans;
1334 struct hammer_inode *dip;
1335 struct hammer_inode *ip;
1337 int64_t parent_obj_id;
1338 u_int32_t parent_obj_localization;
1342 dip = VTOI(ap->a_dvp);
1343 asof = dip->obj_asof;
1347 * Whos are parent? This could be the root of a pseudo-filesystem
1348 * whos parent is in another localization domain.
1350 lwkt_gettoken(&hmp->fs_token);
1351 parent_obj_id = dip->ino_data.parent_obj_id;
1352 if (dip->obj_id == HAMMER_OBJID_ROOT)
1353 parent_obj_localization = dip->ino_data.ext.obj.parent_obj_localization;
1355 parent_obj_localization = dip->obj_localization;
1358 * It's probably a PFS root when dip->ino_data.parent_obj_id is 0.
1360 if (parent_obj_id == 0) {
1361 if (dip->obj_id == HAMMER_OBJID_ROOT &&
1362 asof != hmp->asof) {
1363 parent_obj_id = dip->obj_id;
1365 *ap->a_fakename = kmalloc(19, M_TEMP, M_WAITOK);
1366 ksnprintf(*ap->a_fakename, 19, "0x%016llx",
1367 (long long)dip->obj_asof);
1370 lwkt_reltoken(&hmp->fs_token);
1375 hammer_simple_transaction(&trans, hmp);
1376 ++hammer_stats_file_iopsr;
1378 ip = hammer_get_inode(&trans, dip, parent_obj_id,
1379 asof, parent_obj_localization,
1380 dip->flags, &error);
1382 error = hammer_get_vnode(ip, ap->a_vpp);
1383 hammer_rel_inode(ip, 0);
1387 hammer_done_transaction(&trans);
1388 lwkt_reltoken(&hmp->fs_token);
1393 * hammer_vop_nlink { nch, dvp, vp, cred }
1397 hammer_vop_nlink(struct vop_nlink_args *ap)
1399 struct hammer_transaction trans;
1400 struct hammer_inode *dip;
1401 struct hammer_inode *ip;
1402 struct nchandle *nch;
1406 if (ap->a_dvp->v_mount != ap->a_vp->v_mount)
1410 dip = VTOI(ap->a_dvp);
1411 ip = VTOI(ap->a_vp);
1414 if (dip->obj_localization != ip->obj_localization)
1417 if (dip->flags & HAMMER_INODE_RO)
1419 if (ip->flags & HAMMER_INODE_RO)
1421 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0)
1425 * Create a transaction to cover the operations we perform.
1427 lwkt_gettoken(&hmp->fs_token);
1428 hammer_start_transaction(&trans, hmp);
1429 ++hammer_stats_file_iopsw;
1432 * Add the filesystem object to the directory. Note that neither
1433 * dip nor ip are referenced or locked, but their vnodes are
1434 * referenced. This function will bump the inode's link count.
1436 error = hammer_ip_add_directory(&trans, dip,
1437 nch->ncp->nc_name, nch->ncp->nc_nlen,
1444 cache_setunresolved(nch);
1445 cache_setvp(nch, ap->a_vp);
1447 hammer_done_transaction(&trans);
1448 hammer_knote(ap->a_vp, NOTE_LINK);
1449 hammer_knote(ap->a_dvp, NOTE_WRITE);
1450 lwkt_reltoken(&hmp->fs_token);
1455 * hammer_vop_nmkdir { nch, dvp, vpp, cred, vap }
1457 * The operating system has already ensured that the directory entry
1458 * does not exist and done all appropriate namespace locking.
1462 hammer_vop_nmkdir(struct vop_nmkdir_args *ap)
1464 struct hammer_transaction trans;
1465 struct hammer_inode *dip;
1466 struct hammer_inode *nip;
1467 struct nchandle *nch;
1472 dip = VTOI(ap->a_dvp);
1475 if (dip->flags & HAMMER_INODE_RO)
1477 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0)
1481 * Create a transaction to cover the operations we perform.
1483 lwkt_gettoken(&hmp->fs_token);
1484 hammer_start_transaction(&trans, hmp);
1485 ++hammer_stats_file_iopsw;
1488 * Create a new filesystem object of the requested type. The
1489 * returned inode will be referenced but not locked.
1491 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
1492 dip, nch->ncp->nc_name, nch->ncp->nc_nlen,
1495 hkprintf("hammer_mkdir error %d\n", error);
1496 hammer_done_transaction(&trans);
1498 lwkt_reltoken(&hmp->fs_token);
1502 * Add the new filesystem object to the directory. This will also
1503 * bump the inode's link count.
1505 error = hammer_ip_add_directory(&trans, dip,
1506 nch->ncp->nc_name, nch->ncp->nc_nlen,
1509 hkprintf("hammer_mkdir (add) error %d\n", error);
1515 hammer_rel_inode(nip, 0);
1518 error = hammer_get_vnode(nip, ap->a_vpp);
1519 hammer_rel_inode(nip, 0);
1521 cache_setunresolved(ap->a_nch);
1522 cache_setvp(ap->a_nch, *ap->a_vpp);
1525 hammer_done_transaction(&trans);
1527 hammer_knote(ap->a_dvp, NOTE_WRITE | NOTE_LINK);
1528 lwkt_reltoken(&hmp->fs_token);
1533 * hammer_vop_nmknod { nch, dvp, vpp, cred, vap }
1535 * The operating system has already ensured that the directory entry
1536 * does not exist and done all appropriate namespace locking.
1540 hammer_vop_nmknod(struct vop_nmknod_args *ap)
1542 struct hammer_transaction trans;
1543 struct hammer_inode *dip;
1544 struct hammer_inode *nip;
1545 struct nchandle *nch;
1550 dip = VTOI(ap->a_dvp);
1553 if (dip->flags & HAMMER_INODE_RO)
1555 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0)
1559 * Create a transaction to cover the operations we perform.
1561 lwkt_gettoken(&hmp->fs_token);
1562 hammer_start_transaction(&trans, hmp);
1563 ++hammer_stats_file_iopsw;
1566 * Create a new filesystem object of the requested type. The
1567 * returned inode will be referenced but not locked.
1569 * If mknod specifies a directory a pseudo-fs is created.
1571 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
1572 dip, nch->ncp->nc_name, nch->ncp->nc_nlen,
1575 hammer_done_transaction(&trans);
1577 lwkt_reltoken(&hmp->fs_token);
1582 * Add the new filesystem object to the directory. This will also
1583 * bump the inode's link count.
1585 error = hammer_ip_add_directory(&trans, dip,
1586 nch->ncp->nc_name, nch->ncp->nc_nlen,
1593 hammer_rel_inode(nip, 0);
1596 error = hammer_get_vnode(nip, ap->a_vpp);
1597 hammer_rel_inode(nip, 0);
1599 cache_setunresolved(ap->a_nch);
1600 cache_setvp(ap->a_nch, *ap->a_vpp);
1603 hammer_done_transaction(&trans);
1605 hammer_knote(ap->a_dvp, NOTE_WRITE);
1606 lwkt_reltoken(&hmp->fs_token);
1611 * hammer_vop_open { vp, mode, cred, fp }
1613 * MPSAFE (does not require fs_token)
1617 hammer_vop_open(struct vop_open_args *ap)
1621 ++hammer_stats_file_iopsr;
1622 ip = VTOI(ap->a_vp);
1624 if ((ap->a_mode & FWRITE) && (ip->flags & HAMMER_INODE_RO))
1626 return(vop_stdopen(ap));
1630 * hammer_vop_print { vp }
1634 hammer_vop_print(struct vop_print_args *ap)
1640 * hammer_vop_readdir { vp, uio, cred, *eofflag, *ncookies, off_t **cookies }
1644 hammer_vop_readdir(struct vop_readdir_args *ap)
1646 struct hammer_transaction trans;
1647 struct hammer_cursor cursor;
1648 struct hammer_inode *ip;
1651 hammer_base_elm_t base;
1660 ++hammer_stats_file_iopsr;
1661 ip = VTOI(ap->a_vp);
1663 saveoff = uio->uio_offset;
1666 if (ap->a_ncookies) {
1667 ncookies = uio->uio_resid / 16 + 1;
1668 if (ncookies > 1024)
1670 cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
1678 lwkt_gettoken(&hmp->fs_token);
1679 hammer_simple_transaction(&trans, hmp);
1682 * Handle artificial entries
1684 * It should be noted that the minimum value for a directory
1685 * hash key on-media is 0x0000000100000000, so we can use anything
1686 * less then that to represent our 'special' key space.
1690 r = vop_write_dirent(&error, uio, ip->obj_id, DT_DIR, 1, ".");
1694 cookies[cookie_index] = saveoff;
1697 if (cookie_index == ncookies)
1701 if (ip->ino_data.parent_obj_id) {
1702 r = vop_write_dirent(&error, uio,
1703 ip->ino_data.parent_obj_id,
1706 r = vop_write_dirent(&error, uio,
1707 ip->obj_id, DT_DIR, 2, "..");
1712 cookies[cookie_index] = saveoff;
1715 if (cookie_index == ncookies)
1720 * Key range (begin and end inclusive) to scan. Directory keys
1721 * directly translate to a 64 bit 'seek' position.
1723 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
1724 cursor.key_beg.localization = ip->obj_localization +
1725 hammer_dir_localization(ip);
1726 cursor.key_beg.obj_id = ip->obj_id;
1727 cursor.key_beg.create_tid = 0;
1728 cursor.key_beg.delete_tid = 0;
1729 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1730 cursor.key_beg.obj_type = 0;
1731 cursor.key_beg.key = saveoff;
1733 cursor.key_end = cursor.key_beg;
1734 cursor.key_end.key = HAMMER_MAX_KEY;
1735 cursor.asof = ip->obj_asof;
1736 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
1738 error = hammer_ip_first(&cursor);
1740 while (error == 0) {
1741 error = hammer_ip_resolve_data(&cursor);
1744 base = &cursor.leaf->base;
1745 saveoff = base->key;
1746 KKASSERT(cursor.leaf->data_len > HAMMER_ENTRY_NAME_OFF);
1748 if (base->obj_id != ip->obj_id)
1749 panic("readdir: bad record at %p", cursor.node);
1752 * Convert pseudo-filesystems into softlinks
1754 dtype = hammer_get_dtype(cursor.leaf->base.obj_type);
1755 r = vop_write_dirent(
1756 &error, uio, cursor.data->entry.obj_id,
1758 cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF ,
1759 (void *)cursor.data->entry.name);
1764 cookies[cookie_index] = base->key;
1766 if (cookie_index == ncookies)
1768 error = hammer_ip_next(&cursor);
1770 hammer_done_cursor(&cursor);
1773 hammer_done_transaction(&trans);
1776 *ap->a_eofflag = (error == ENOENT);
1777 uio->uio_offset = saveoff;
1778 if (error && cookie_index == 0) {
1779 if (error == ENOENT)
1782 kfree(cookies, M_TEMP);
1783 *ap->a_ncookies = 0;
1784 *ap->a_cookies = NULL;
1787 if (error == ENOENT)
1790 *ap->a_ncookies = cookie_index;
1791 *ap->a_cookies = cookies;
1794 lwkt_reltoken(&hmp->fs_token);
1799 * hammer_vop_readlink { vp, uio, cred }
1803 hammer_vop_readlink(struct vop_readlink_args *ap)
1805 struct hammer_transaction trans;
1806 struct hammer_cursor cursor;
1807 struct hammer_inode *ip;
1810 u_int32_t localization;
1811 hammer_pseudofs_inmem_t pfsm;
1814 ip = VTOI(ap->a_vp);
1817 lwkt_gettoken(&hmp->fs_token);
1820 * Shortcut if the symlink data was stuffed into ino_data.
1822 * Also expand special "@@PFS%05d" softlinks (expansion only
1823 * occurs for non-historical (current) accesses made from the
1824 * primary filesystem).
1826 * Note that userspace hammer command does not allow users to
1827 * create a @@PFS softlink under an existing other PFS (id!=0)
1828 * so the ip localization here for @@PFS softlink is always 0.
1830 if (ip->ino_data.size <= HAMMER_INODE_BASESYMLEN) {
1834 ptr = ip->ino_data.ext.symlink;
1835 bytes = (int)ip->ino_data.size;
1837 ip->obj_asof == HAMMER_MAX_TID &&
1838 ip->obj_localization == 0 &&
1839 strncmp(ptr, "@@PFS", 5) == 0) {
1840 hammer_simple_transaction(&trans, hmp);
1841 bcopy(ptr + 5, buf, 5);
1843 localization = strtoul(buf, NULL, 10) << 16;
1844 pfsm = hammer_load_pseudofs(&trans, localization,
1847 if (pfsm->pfsd.mirror_flags &
1848 HAMMER_PFSD_SLAVE) {
1849 /* vap->va_size == 26 */
1850 ksnprintf(buf, sizeof(buf),
1852 (long long)pfsm->pfsd.sync_end_tid,
1853 localization >> 16);
1855 /* vap->va_size == 10 */
1856 ksnprintf(buf, sizeof(buf),
1858 localization >> 16);
1860 ksnprintf(buf, sizeof(buf),
1862 (long long)HAMMER_MAX_TID,
1863 localization >> 16);
1867 bytes = strlen(buf);
1870 hammer_rel_pseudofs(hmp, pfsm);
1871 hammer_done_transaction(&trans);
1873 error = uiomove(ptr, bytes, ap->a_uio);
1874 lwkt_reltoken(&hmp->fs_token);
1881 hammer_simple_transaction(&trans, hmp);
1882 ++hammer_stats_file_iopsr;
1883 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
1886 * Key range (begin and end inclusive) to scan. Directory keys
1887 * directly translate to a 64 bit 'seek' position.
1889 cursor.key_beg.localization = ip->obj_localization +
1890 HAMMER_LOCALIZE_MISC;
1891 cursor.key_beg.obj_id = ip->obj_id;
1892 cursor.key_beg.create_tid = 0;
1893 cursor.key_beg.delete_tid = 0;
1894 cursor.key_beg.rec_type = HAMMER_RECTYPE_FIX;
1895 cursor.key_beg.obj_type = 0;
1896 cursor.key_beg.key = HAMMER_FIXKEY_SYMLINK;
1897 cursor.asof = ip->obj_asof;
1898 cursor.flags |= HAMMER_CURSOR_ASOF;
1900 error = hammer_ip_lookup(&cursor);
1902 error = hammer_ip_resolve_data(&cursor);
1904 KKASSERT(cursor.leaf->data_len >=
1905 HAMMER_SYMLINK_NAME_OFF);
1906 error = uiomove(cursor.data->symlink.name,
1907 cursor.leaf->data_len -
1908 HAMMER_SYMLINK_NAME_OFF,
1912 hammer_done_cursor(&cursor);
1913 hammer_done_transaction(&trans);
1914 lwkt_reltoken(&hmp->fs_token);
1919 * hammer_vop_nremove { nch, dvp, cred }
1923 hammer_vop_nremove(struct vop_nremove_args *ap)
1925 struct hammer_transaction trans;
1926 struct hammer_inode *dip;
1930 dip = VTOI(ap->a_dvp);
1933 if (hammer_nohistory(dip) == 0 &&
1934 (error = hammer_checkspace(hmp, HAMMER_CHKSPC_REMOVE)) != 0) {
1938 lwkt_gettoken(&hmp->fs_token);
1939 hammer_start_transaction(&trans, hmp);
1940 ++hammer_stats_file_iopsw;
1941 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp, ap->a_cred, 0, 0);
1942 hammer_done_transaction(&trans);
1944 hammer_knote(ap->a_dvp, NOTE_WRITE);
1945 lwkt_reltoken(&hmp->fs_token);
1950 * hammer_vop_nrename { fnch, tnch, fdvp, tdvp, cred }
1954 hammer_vop_nrename(struct vop_nrename_args *ap)
1956 struct hammer_transaction trans;
1957 struct namecache *fncp;
1958 struct namecache *tncp;
1959 struct hammer_inode *fdip;
1960 struct hammer_inode *tdip;
1961 struct hammer_inode *ip;
1963 struct hammer_cursor cursor;
1965 u_int32_t max_iterations;
1968 if (ap->a_fdvp->v_mount != ap->a_tdvp->v_mount)
1970 if (ap->a_fdvp->v_mount != ap->a_fnch->ncp->nc_vp->v_mount)
1973 fdip = VTOI(ap->a_fdvp);
1974 tdip = VTOI(ap->a_tdvp);
1975 fncp = ap->a_fnch->ncp;
1976 tncp = ap->a_tnch->ncp;
1977 ip = VTOI(fncp->nc_vp);
1978 KKASSERT(ip != NULL);
1982 if (fdip->obj_localization != tdip->obj_localization)
1984 if (fdip->obj_localization != ip->obj_localization)
1987 if (fdip->flags & HAMMER_INODE_RO)
1989 if (tdip->flags & HAMMER_INODE_RO)
1991 if (ip->flags & HAMMER_INODE_RO)
1993 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0)
1996 lwkt_gettoken(&hmp->fs_token);
1997 hammer_start_transaction(&trans, hmp);
1998 ++hammer_stats_file_iopsw;
2001 * Remove tncp from the target directory and then link ip as
2002 * tncp. XXX pass trans to dounlink
2004 * Force the inode sync-time to match the transaction so it is
2005 * in-sync with the creation of the target directory entry.
2007 error = hammer_dounlink(&trans, ap->a_tnch, ap->a_tdvp,
2009 if (error == 0 || error == ENOENT) {
2010 error = hammer_ip_add_directory(&trans, tdip,
2011 tncp->nc_name, tncp->nc_nlen,
2014 ip->ino_data.parent_obj_id = tdip->obj_id;
2015 ip->ino_data.ctime = trans.time;
2016 hammer_modify_inode(&trans, ip, HAMMER_INODE_DDIRTY);
2020 goto failed; /* XXX */
2023 * Locate the record in the originating directory and remove it.
2025 * Calculate the namekey and setup the key range for the scan. This
2026 * works kinda like a chained hash table where the lower 32 bits
2027 * of the namekey synthesize the chain.
2029 * The key range is inclusive of both key_beg and key_end.
2031 namekey = hammer_directory_namekey(fdip, fncp->nc_name, fncp->nc_nlen,
2034 hammer_init_cursor(&trans, &cursor, &fdip->cache[1], fdip);
2035 cursor.key_beg.localization = fdip->obj_localization +
2036 hammer_dir_localization(fdip);
2037 cursor.key_beg.obj_id = fdip->obj_id;
2038 cursor.key_beg.key = namekey;
2039 cursor.key_beg.create_tid = 0;
2040 cursor.key_beg.delete_tid = 0;
2041 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
2042 cursor.key_beg.obj_type = 0;
2044 cursor.key_end = cursor.key_beg;
2045 cursor.key_end.key += max_iterations;
2046 cursor.asof = fdip->obj_asof;
2047 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
2050 * Scan all matching records (the chain), locate the one matching
2051 * the requested path component.
2053 * The hammer_ip_*() functions merge in-memory records with on-disk
2054 * records for the purposes of the search.
2056 error = hammer_ip_first(&cursor);
2057 while (error == 0) {
2058 if (hammer_ip_resolve_data(&cursor) != 0)
2060 nlen = cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF;
2062 if (fncp->nc_nlen == nlen &&
2063 bcmp(fncp->nc_name, cursor.data->entry.name, nlen) == 0) {
2066 error = hammer_ip_next(&cursor);
2070 * If all is ok we have to get the inode so we can adjust nlinks.
2072 * WARNING: hammer_ip_del_directory() may have to terminate the
2073 * cursor to avoid a recursion. It's ok to call hammer_done_cursor()
2077 error = hammer_ip_del_directory(&trans, &cursor, fdip, ip);
2080 * XXX A deadlock here will break rename's atomicy for the purposes
2081 * of crash recovery.
2083 if (error == EDEADLK) {
2084 hammer_done_cursor(&cursor);
2089 * Cleanup and tell the kernel that the rename succeeded.
2091 * NOTE: ip->vp, if non-NULL, cannot be directly referenced
2092 * without formally acquiring the vp since the vp might
2093 * have zero refs on it, or in the middle of a reclaim,
2096 hammer_done_cursor(&cursor);
2098 cache_rename(ap->a_fnch, ap->a_tnch);
2099 hammer_knote(ap->a_fdvp, NOTE_WRITE);
2100 hammer_knote(ap->a_tdvp, NOTE_WRITE);
2104 error = hammer_get_vnode(ip, &vp);
2105 if (error == 0 && vp) {
2107 hammer_knote(ip->vp, NOTE_RENAME);
2111 kprintf("Debug: HAMMER ip/vp race2 avoided\n");
2116 hammer_done_transaction(&trans);
2117 lwkt_reltoken(&hmp->fs_token);
2122 * hammer_vop_nrmdir { nch, dvp, cred }
2126 hammer_vop_nrmdir(struct vop_nrmdir_args *ap)
2128 struct hammer_transaction trans;
2129 struct hammer_inode *dip;
2133 dip = VTOI(ap->a_dvp);
2136 if (hammer_nohistory(dip) == 0 &&
2137 (error = hammer_checkspace(hmp, HAMMER_CHKSPC_REMOVE)) != 0) {
2141 lwkt_gettoken(&hmp->fs_token);
2142 hammer_start_transaction(&trans, hmp);
2143 ++hammer_stats_file_iopsw;
2144 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp, ap->a_cred, 0, 1);
2145 hammer_done_transaction(&trans);
2147 hammer_knote(ap->a_dvp, NOTE_WRITE | NOTE_LINK);
2148 lwkt_reltoken(&hmp->fs_token);
2153 * hammer_vop_markatime { vp, cred }
2157 hammer_vop_markatime(struct vop_markatime_args *ap)
2159 struct hammer_transaction trans;
2160 struct hammer_inode *ip;
2163 ip = VTOI(ap->a_vp);
2164 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
2166 if (ip->flags & HAMMER_INODE_RO)
2169 if (hmp->mp->mnt_flag & MNT_NOATIME)
2171 lwkt_gettoken(&hmp->fs_token);
2172 hammer_start_transaction(&trans, hmp);
2173 ++hammer_stats_file_iopsw;
2175 ip->ino_data.atime = trans.time;
2176 hammer_modify_inode(&trans, ip, HAMMER_INODE_ATIME);
2177 hammer_done_transaction(&trans);
2178 hammer_knote(ap->a_vp, NOTE_ATTRIB);
2179 lwkt_reltoken(&hmp->fs_token);
2184 * hammer_vop_setattr { vp, vap, cred }
2188 hammer_vop_setattr(struct vop_setattr_args *ap)
2190 struct hammer_transaction trans;
2191 struct hammer_inode *ip;
2200 int64_t aligned_size;
2205 ip = ap->a_vp->v_data;
2210 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
2212 if (ip->flags & HAMMER_INODE_RO)
2214 if (hammer_nohistory(ip) == 0 &&
2215 (error = hammer_checkspace(hmp, HAMMER_CHKSPC_REMOVE)) != 0) {
2219 lwkt_gettoken(&hmp->fs_token);
2220 hammer_start_transaction(&trans, hmp);
2221 ++hammer_stats_file_iopsw;
2224 if (vap->va_flags != VNOVAL) {
2225 flags = ip->ino_data.uflags;
2226 error = vop_helper_setattr_flags(&flags, vap->va_flags,
2227 hammer_to_unix_xid(&ip->ino_data.uid),
2230 if (ip->ino_data.uflags != flags) {
2231 ip->ino_data.uflags = flags;
2232 ip->ino_data.ctime = trans.time;
2233 modflags |= HAMMER_INODE_DDIRTY;
2234 kflags |= NOTE_ATTRIB;
2236 if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) {
2243 if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) {
2247 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
2248 mode_t cur_mode = ip->ino_data.mode;
2249 uid_t cur_uid = hammer_to_unix_xid(&ip->ino_data.uid);
2250 gid_t cur_gid = hammer_to_unix_xid(&ip->ino_data.gid);
2254 error = vop_helper_chown(ap->a_vp, vap->va_uid, vap->va_gid,
2256 &cur_uid, &cur_gid, &cur_mode);
2258 hammer_guid_to_uuid(&uuid_uid, cur_uid);
2259 hammer_guid_to_uuid(&uuid_gid, cur_gid);
2260 if (bcmp(&uuid_uid, &ip->ino_data.uid,
2261 sizeof(uuid_uid)) ||
2262 bcmp(&uuid_gid, &ip->ino_data.gid,
2263 sizeof(uuid_gid)) ||
2264 ip->ino_data.mode != cur_mode) {
2265 ip->ino_data.uid = uuid_uid;
2266 ip->ino_data.gid = uuid_gid;
2267 ip->ino_data.mode = cur_mode;
2268 ip->ino_data.ctime = trans.time;
2269 modflags |= HAMMER_INODE_DDIRTY;
2271 kflags |= NOTE_ATTRIB;
2274 while (vap->va_size != VNOVAL && ip->ino_data.size != vap->va_size) {
2275 switch(ap->a_vp->v_type) {
2277 if (vap->va_size == ip->ino_data.size)
2281 * Log the operation if in fast-fsync mode or if
2282 * there are unterminated redo write records present.
2284 * The second check is needed so the recovery code
2285 * properly truncates write redos even if nominal
2286 * REDO operations is turned off due to excessive
2287 * writes, because the related records might be
2288 * destroyed and never lay down a TERM_WRITE.
2290 if ((ip->flags & HAMMER_INODE_REDO) ||
2291 (ip->flags & HAMMER_INODE_RDIRTY)) {
2292 error = hammer_generate_redo(&trans, ip,
2297 blksize = hammer_blocksize(vap->va_size);
2300 * XXX break atomicy, we can deadlock the backend
2301 * if we do not release the lock. Probably not a
2304 if (vap->va_size < ip->ino_data.size) {
2305 nvtruncbuf(ap->a_vp, vap->va_size,
2307 hammer_blockoff(vap->va_size),
2310 kflags |= NOTE_WRITE;
2312 nvextendbuf(ap->a_vp,
2315 hammer_blocksize(ip->ino_data.size),
2316 hammer_blocksize(vap->va_size),
2317 hammer_blockoff(ip->ino_data.size),
2318 hammer_blockoff(vap->va_size),
2321 kflags |= NOTE_WRITE | NOTE_EXTEND;
2323 ip->ino_data.size = vap->va_size;
2324 ip->ino_data.mtime = trans.time;
2325 /* XXX safe to use SDIRTY instead of DDIRTY here? */
2326 modflags |= HAMMER_INODE_MTIME | HAMMER_INODE_DDIRTY;
2329 * On-media truncation is cached in the inode until
2330 * the inode is synchronized. We must immediately
2331 * handle any frontend records.
2334 hammer_ip_frontend_trunc(ip, vap->va_size);
2335 #ifdef DEBUG_TRUNCATE
2336 if (HammerTruncIp == NULL)
2339 if ((ip->flags & HAMMER_INODE_TRUNCATED) == 0) {
2340 ip->flags |= HAMMER_INODE_TRUNCATED;
2341 ip->trunc_off = vap->va_size;
2342 hammer_inode_dirty(ip);
2343 #ifdef DEBUG_TRUNCATE
2344 if (ip == HammerTruncIp)
2345 kprintf("truncate1 %016llx\n",
2346 (long long)ip->trunc_off);
2348 } else if (ip->trunc_off > vap->va_size) {
2349 ip->trunc_off = vap->va_size;
2350 #ifdef DEBUG_TRUNCATE
2351 if (ip == HammerTruncIp)
2352 kprintf("truncate2 %016llx\n",
2353 (long long)ip->trunc_off);
2356 #ifdef DEBUG_TRUNCATE
2357 if (ip == HammerTruncIp)
2358 kprintf("truncate3 %016llx (ignored)\n",
2359 (long long)vap->va_size);
2366 * When truncating, nvtruncbuf() may have cleaned out
2367 * a portion of the last block on-disk in the buffer
2368 * cache. We must clean out any frontend records
2369 * for blocks beyond the new last block.
2371 aligned_size = (vap->va_size + (blksize - 1)) &
2372 ~(int64_t)(blksize - 1);
2373 if (truncating && vap->va_size < aligned_size) {
2374 aligned_size -= blksize;
2375 hammer_ip_frontend_trunc(ip, aligned_size);
2380 if ((ip->flags & HAMMER_INODE_TRUNCATED) == 0) {
2381 ip->flags |= HAMMER_INODE_TRUNCATED;
2382 ip->trunc_off = vap->va_size;
2383 hammer_inode_dirty(ip);
2384 } else if (ip->trunc_off > vap->va_size) {
2385 ip->trunc_off = vap->va_size;
2387 hammer_ip_frontend_trunc(ip, vap->va_size);
2388 ip->ino_data.size = vap->va_size;
2389 ip->ino_data.mtime = trans.time;
2390 modflags |= HAMMER_INODE_MTIME | HAMMER_INODE_DDIRTY;
2391 kflags |= NOTE_ATTRIB;
2399 if (vap->va_atime.tv_sec != VNOVAL) {
2400 ip->ino_data.atime = hammer_timespec_to_time(&vap->va_atime);
2401 modflags |= HAMMER_INODE_ATIME;
2402 kflags |= NOTE_ATTRIB;
2404 if (vap->va_mtime.tv_sec != VNOVAL) {
2405 ip->ino_data.mtime = hammer_timespec_to_time(&vap->va_mtime);
2406 modflags |= HAMMER_INODE_MTIME;
2407 kflags |= NOTE_ATTRIB;
2409 if (vap->va_mode != (mode_t)VNOVAL) {
2410 mode_t cur_mode = ip->ino_data.mode;
2411 uid_t cur_uid = hammer_to_unix_xid(&ip->ino_data.uid);
2412 gid_t cur_gid = hammer_to_unix_xid(&ip->ino_data.gid);
2414 error = vop_helper_chmod(ap->a_vp, vap->va_mode, ap->a_cred,
2415 cur_uid, cur_gid, &cur_mode);
2416 if (error == 0 && ip->ino_data.mode != cur_mode) {
2417 ip->ino_data.mode = cur_mode;
2418 ip->ino_data.ctime = trans.time;
2419 modflags |= HAMMER_INODE_DDIRTY;
2420 kflags |= NOTE_ATTRIB;
2425 hammer_modify_inode(&trans, ip, modflags);
2426 hammer_done_transaction(&trans);
2427 hammer_knote(ap->a_vp, kflags);
2428 lwkt_reltoken(&hmp->fs_token);
2433 * hammer_vop_nsymlink { nch, dvp, vpp, cred, vap, target }
2437 hammer_vop_nsymlink(struct vop_nsymlink_args *ap)
2439 struct hammer_transaction trans;
2440 struct hammer_inode *dip;
2441 struct hammer_inode *nip;
2442 hammer_record_t record;
2443 struct nchandle *nch;
2448 ap->a_vap->va_type = VLNK;
2451 dip = VTOI(ap->a_dvp);
2454 if (dip->flags & HAMMER_INODE_RO)
2456 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0)
2460 * Create a transaction to cover the operations we perform.
2462 lwkt_gettoken(&hmp->fs_token);
2463 hammer_start_transaction(&trans, hmp);
2464 ++hammer_stats_file_iopsw;
2467 * Create a new filesystem object of the requested type. The
2468 * returned inode will be referenced but not locked.
2471 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
2472 dip, nch->ncp->nc_name, nch->ncp->nc_nlen,
2475 hammer_done_transaction(&trans);
2477 lwkt_reltoken(&hmp->fs_token);
2482 * Add a record representing the symlink. symlink stores the link
2483 * as pure data, not a string, and is no \0 terminated.
2486 bytes = strlen(ap->a_target);
2488 if (bytes <= HAMMER_INODE_BASESYMLEN) {
2489 bcopy(ap->a_target, nip->ino_data.ext.symlink, bytes);
2491 record = hammer_alloc_mem_record(nip, bytes);
2492 record->type = HAMMER_MEM_RECORD_GENERAL;
2494 record->leaf.base.localization = nip->obj_localization +
2495 HAMMER_LOCALIZE_MISC;
2496 record->leaf.base.key = HAMMER_FIXKEY_SYMLINK;
2497 record->leaf.base.rec_type = HAMMER_RECTYPE_FIX;
2498 record->leaf.data_len = bytes;
2499 KKASSERT(HAMMER_SYMLINK_NAME_OFF == 0);
2500 bcopy(ap->a_target, record->data->symlink.name, bytes);
2501 error = hammer_ip_add_record(&trans, record);
2505 * Set the file size to the length of the link.
2508 nip->ino_data.size = bytes;
2509 hammer_modify_inode(&trans, nip, HAMMER_INODE_DDIRTY);
2513 error = hammer_ip_add_directory(&trans, dip, nch->ncp->nc_name,
2514 nch->ncp->nc_nlen, nip);
2520 hammer_rel_inode(nip, 0);
2523 error = hammer_get_vnode(nip, ap->a_vpp);
2524 hammer_rel_inode(nip, 0);
2526 cache_setunresolved(ap->a_nch);
2527 cache_setvp(ap->a_nch, *ap->a_vpp);
2528 hammer_knote(ap->a_dvp, NOTE_WRITE);
2531 hammer_done_transaction(&trans);
2532 lwkt_reltoken(&hmp->fs_token);
2537 * hammer_vop_nwhiteout { nch, dvp, cred, flags }
2541 hammer_vop_nwhiteout(struct vop_nwhiteout_args *ap)
2543 struct hammer_transaction trans;
2544 struct hammer_inode *dip;
2548 dip = VTOI(ap->a_dvp);
2551 if (hammer_nohistory(dip) == 0 &&
2552 (error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0) {
2556 lwkt_gettoken(&hmp->fs_token);
2557 hammer_start_transaction(&trans, hmp);
2558 ++hammer_stats_file_iopsw;
2559 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp,
2560 ap->a_cred, ap->a_flags, -1);
2561 hammer_done_transaction(&trans);
2562 lwkt_reltoken(&hmp->fs_token);
2568 * hammer_vop_ioctl { vp, command, data, fflag, cred }
2572 hammer_vop_ioctl(struct vop_ioctl_args *ap)
2574 struct hammer_inode *ip = ap->a_vp->v_data;
2575 hammer_mount_t hmp = ip->hmp;
2578 ++hammer_stats_file_iopsr;
2579 lwkt_gettoken(&hmp->fs_token);
2580 error = hammer_ioctl(ip, ap->a_command, ap->a_data,
2581 ap->a_fflag, ap->a_cred);
2582 lwkt_reltoken(&hmp->fs_token);
2588 hammer_vop_mountctl(struct vop_mountctl_args *ap)
2590 static const struct mountctl_opt extraopt[] = {
2591 { HMNT_NOHISTORY, "nohistory" },
2592 { HMNT_MASTERID, "master" },
2596 struct hammer_mount *hmp;
2603 mp = ap->a_head.a_ops->head.vv_mount;
2604 KKASSERT(mp->mnt_data != NULL);
2605 hmp = (struct hammer_mount *)mp->mnt_data;
2607 lwkt_gettoken(&hmp->fs_token);
2610 case MOUNTCTL_SET_EXPORT:
2611 if (ap->a_ctllen != sizeof(struct export_args))
2614 error = hammer_vfs_export(mp, ap->a_op,
2615 (const struct export_args *)ap->a_ctl);
2617 case MOUNTCTL_MOUNTFLAGS:
2620 * Call standard mountctl VOP function
2621 * so we get user mount flags.
2623 error = vop_stdmountctl(ap);
2627 usedbytes = *ap->a_res;
2629 if (usedbytes > 0 && usedbytes < ap->a_buflen) {
2630 usedbytes += vfs_flagstostr(hmp->hflags, extraopt,
2632 ap->a_buflen - usedbytes,
2636 *ap->a_res += usedbytes;
2640 error = vop_stdmountctl(ap);
2643 lwkt_reltoken(&hmp->fs_token);
2648 * hammer_vop_strategy { vp, bio }
2650 * Strategy call, used for regular file read & write only. Note that the
2651 * bp may represent a cluster.
2653 * To simplify operation and allow better optimizations in the future,
2654 * this code does not make any assumptions with regards to buffer alignment
2659 hammer_vop_strategy(struct vop_strategy_args *ap)
2664 bp = ap->a_bio->bio_buf;
2668 error = hammer_vop_strategy_read(ap);
2671 error = hammer_vop_strategy_write(ap);
2674 bp->b_error = error = EINVAL;
2675 bp->b_flags |= B_ERROR;
2680 /* hammer_dump_dedup_cache(((hammer_inode_t)ap->a_vp->v_data)->hmp); */
2686 * Read from a regular file. Iterate the related records and fill in the
2687 * BIO/BUF. Gaps are zero-filled.
2689 * The support code in hammer_object.c should be used to deal with mixed
2690 * in-memory and on-disk records.
2692 * NOTE: Can be called from the cluster code with an oversized buf.
2698 hammer_vop_strategy_read(struct vop_strategy_args *ap)
2700 struct hammer_transaction trans;
2701 struct hammer_inode *ip;
2702 struct hammer_inode *dip;
2704 struct hammer_cursor cursor;
2705 hammer_base_elm_t base;
2706 hammer_off_t disk_offset;
2721 ip = ap->a_vp->v_data;
2725 * The zone-2 disk offset may have been set by the cluster code via
2726 * a BMAP operation, or else should be NOOFFSET.
2728 * Checking the high bits for a match against zone-2 should suffice.
2730 * In cases where a lot of data duplication is present it may be
2731 * more beneficial to drop through and doubule-buffer through the
2734 nbio = push_bio(bio);
2735 if ((nbio->bio_offset & HAMMER_OFF_ZONE_MASK) ==
2736 HAMMER_ZONE_LARGE_DATA) {
2737 if (hammer_double_buffer == 0) {
2738 lwkt_gettoken(&hmp->fs_token);
2739 error = hammer_io_direct_read(hmp, nbio, NULL);
2740 lwkt_reltoken(&hmp->fs_token);
2745 * Try to shortcut requests for double_buffer mode too.
2746 * Since this mode runs through the device buffer cache
2747 * only compatible buffer sizes (meaning those generated
2748 * by normal filesystem buffers) are legal.
2750 if (hammer_live_dedup == 0 && (bp->b_flags & B_PAGING) == 0) {
2751 lwkt_gettoken(&hmp->fs_token);
2752 error = hammer_io_indirect_read(hmp, nbio, NULL);
2753 lwkt_reltoken(&hmp->fs_token);
2759 * Well, that sucked. Do it the hard way. If all the stars are
2760 * aligned we may still be able to issue a direct-read.
2762 lwkt_gettoken(&hmp->fs_token);
2763 hammer_simple_transaction(&trans, hmp);
2764 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
2767 * Key range (begin and end inclusive) to scan. Note that the key's
2768 * stored in the actual records represent BASE+LEN, not BASE. The
2769 * first record containing bio_offset will have a key > bio_offset.
2771 cursor.key_beg.localization = ip->obj_localization +
2772 HAMMER_LOCALIZE_MISC;
2773 cursor.key_beg.obj_id = ip->obj_id;
2774 cursor.key_beg.create_tid = 0;
2775 cursor.key_beg.delete_tid = 0;
2776 cursor.key_beg.obj_type = 0;
2777 cursor.key_beg.key = bio->bio_offset + 1;
2778 cursor.asof = ip->obj_asof;
2779 cursor.flags |= HAMMER_CURSOR_ASOF;
2781 cursor.key_end = cursor.key_beg;
2782 KKASSERT(ip->ino_data.obj_type == HAMMER_OBJTYPE_REGFILE);
2784 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_DBFILE) {
2785 cursor.key_beg.rec_type = HAMMER_RECTYPE_DB;
2786 cursor.key_end.rec_type = HAMMER_RECTYPE_DB;
2787 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
2791 ran_end = bio->bio_offset + bp->b_bufsize;
2792 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
2793 cursor.key_end.rec_type = HAMMER_RECTYPE_DATA;
2794 tmp64 = ran_end + MAXPHYS + 1; /* work-around GCC-4 bug */
2795 if (tmp64 < ran_end)
2796 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
2798 cursor.key_end.key = ran_end + MAXPHYS + 1;
2800 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
2803 * Set NOSWAPCACHE for cursor data extraction if double buffering
2804 * is disabled or (if the file is not marked cacheable via chflags
2805 * and vm.swapcache_use_chflags is enabled).
2807 if (hammer_double_buffer == 0 ||
2808 ((ap->a_vp->v_flag & VSWAPCACHE) == 0 &&
2809 vm_swapcache_use_chflags)) {
2810 cursor.flags |= HAMMER_CURSOR_NOSWAPCACHE;
2813 error = hammer_ip_first(&cursor);
2816 while (error == 0) {
2818 * Get the base file offset of the record. The key for
2819 * data records is (base + bytes) rather then (base).
2821 base = &cursor.leaf->base;
2822 rec_offset = base->key - cursor.leaf->data_len;
2825 * Calculate the gap, if any, and zero-fill it.
2827 * n is the offset of the start of the record verses our
2828 * current seek offset in the bio.
2830 n = (int)(rec_offset - (bio->bio_offset + boff));
2832 if (n > bp->b_bufsize - boff)
2833 n = bp->b_bufsize - boff;
2834 bzero((char *)bp->b_data + boff, n);
2840 * Calculate the data offset in the record and the number
2841 * of bytes we can copy.
2843 * There are two degenerate cases. First, boff may already
2844 * be at bp->b_bufsize. Secondly, the data offset within
2845 * the record may exceed the record's size.
2849 n = cursor.leaf->data_len - roff;
2851 kprintf("strategy_read: bad n=%d roff=%d\n", n, roff);
2853 } else if (n > bp->b_bufsize - boff) {
2854 n = bp->b_bufsize - boff;
2858 * Deal with cached truncations. This cool bit of code
2859 * allows truncate()/ftruncate() to avoid having to sync
2862 * If the frontend is truncated then all backend records are
2863 * subject to the frontend's truncation.
2865 * If the backend is truncated then backend records on-disk
2866 * (but not in-memory) are subject to the backend's
2867 * truncation. In-memory records owned by the backend
2868 * represent data written after the truncation point on the
2869 * backend and must not be truncated.
2871 * Truncate operations deal with frontend buffer cache
2872 * buffers and frontend-owned in-memory records synchronously.
2874 if (ip->flags & HAMMER_INODE_TRUNCATED) {
2875 if (hammer_cursor_ondisk(&cursor)/* ||
2876 cursor.iprec->flush_state == HAMMER_FST_FLUSH*/) {
2877 if (ip->trunc_off <= rec_offset)
2879 else if (ip->trunc_off < rec_offset + n)
2880 n = (int)(ip->trunc_off - rec_offset);
2883 if (ip->sync_flags & HAMMER_INODE_TRUNCATED) {
2884 if (hammer_cursor_ondisk(&cursor)) {
2885 if (ip->sync_trunc_off <= rec_offset)
2887 else if (ip->sync_trunc_off < rec_offset + n)
2888 n = (int)(ip->sync_trunc_off - rec_offset);
2893 * Try to issue a direct read into our bio if possible,
2894 * otherwise resolve the element data into a hammer_buffer
2897 * The buffer on-disk should be zerod past any real
2898 * truncation point, but may not be for any synthesized
2899 * truncation point from above.
2901 * NOTE: disk_offset is only valid if the cursor data is
2904 disk_offset = cursor.leaf->data_offset + roff;
2905 isdedupable = (boff == 0 && n == bp->b_bufsize &&
2906 hammer_cursor_ondisk(&cursor) &&
2907 ((int)disk_offset & HAMMER_BUFMASK) == 0);
2909 if (isdedupable && hammer_double_buffer == 0) {
2913 KKASSERT((disk_offset & HAMMER_OFF_ZONE_MASK) ==
2914 HAMMER_ZONE_LARGE_DATA);
2915 nbio->bio_offset = disk_offset;
2916 error = hammer_io_direct_read(hmp, nbio, cursor.leaf);
2917 if (hammer_live_dedup && error == 0)
2918 hammer_dedup_cache_add(ip, cursor.leaf);
2920 } else if (isdedupable) {
2922 * Async I/O case for reading from backing store
2923 * and copying the data to the filesystem buffer.
2924 * live-dedup has to verify the data anyway if it
2925 * gets a hit later so we can just add the entry
2928 KKASSERT((disk_offset & HAMMER_OFF_ZONE_MASK) ==
2929 HAMMER_ZONE_LARGE_DATA);
2930 nbio->bio_offset = disk_offset;
2931 if (hammer_live_dedup)
2932 hammer_dedup_cache_add(ip, cursor.leaf);
2933 error = hammer_io_indirect_read(hmp, nbio, cursor.leaf);
2936 error = hammer_ip_resolve_data(&cursor);
2938 if (hammer_live_dedup && isdedupable)
2939 hammer_dedup_cache_add(ip, cursor.leaf);
2940 bcopy((char *)cursor.data + roff,
2941 (char *)bp->b_data + boff, n);
2948 * We have to be sure that the only elements added to the
2949 * dedup cache are those which are already on-media.
2951 if (hammer_live_dedup && hammer_cursor_ondisk(&cursor))
2952 hammer_dedup_cache_add(ip, cursor.leaf);
2955 * Iterate until we have filled the request.
2958 if (boff == bp->b_bufsize)
2960 error = hammer_ip_next(&cursor);
2964 * There may have been a gap after the last record
2966 if (error == ENOENT)
2968 if (error == 0 && boff != bp->b_bufsize) {
2969 KKASSERT(boff < bp->b_bufsize);
2970 bzero((char *)bp->b_data + boff, bp->b_bufsize - boff);
2971 /* boff = bp->b_bufsize; */
2975 * Disallow swapcache operation on the vnode buffer if double
2976 * buffering is enabled, the swapcache will get the data via
2977 * the block device buffer.
2979 if (hammer_double_buffer)
2980 bp->b_flags |= B_NOTMETA;
2986 bp->b_error = error;
2988 bp->b_flags |= B_ERROR;
2993 * Cache the b-tree node for the last data read in cache[1].
2995 * If we hit the file EOF then also cache the node in the
2996 * governing director's cache[3], it will be used to initialize
2997 * the inode's cache[1] for any inodes looked up via the directory.
2999 * This doesn't reduce disk accesses since the B-Tree chain is
3000 * likely cached, but it does reduce cpu overhead when looking
3001 * up file offsets for cpdup/tar/cpio style iterations.
3004 hammer_cache_node(&ip->cache[1], cursor.node);
3005 if (ran_end >= ip->ino_data.size) {
3006 dip = hammer_find_inode(&trans, ip->ino_data.parent_obj_id,
3007 ip->obj_asof, ip->obj_localization);
3009 hammer_cache_node(&dip->cache[3], cursor.node);
3010 hammer_rel_inode(dip, 0);
3013 hammer_done_cursor(&cursor);
3014 hammer_done_transaction(&trans);
3015 lwkt_reltoken(&hmp->fs_token);
3020 * BMAP operation - used to support cluster_read() only.
3022 * (struct vnode *vp, off_t loffset, off_t *doffsetp, int *runp, int *runb)
3024 * This routine may return EOPNOTSUPP if the opration is not supported for
3025 * the specified offset. The contents of the pointer arguments do not
3026 * need to be initialized in that case.
3028 * If a disk address is available and properly aligned return 0 with
3029 * *doffsetp set to the zone-2 address, and *runp / *runb set appropriately
3030 * to the run-length relative to that offset. Callers may assume that
3031 * *doffsetp is valid if 0 is returned, even if *runp is not sufficiently
3032 * large, so return EOPNOTSUPP if it is not sufficiently large.
3036 hammer_vop_bmap(struct vop_bmap_args *ap)
3038 struct hammer_transaction trans;
3039 struct hammer_inode *ip;
3041 struct hammer_cursor cursor;
3042 hammer_base_elm_t base;
3046 int64_t base_offset;
3047 int64_t base_disk_offset;
3048 int64_t last_offset;
3049 hammer_off_t last_disk_offset;
3050 hammer_off_t disk_offset;
3055 ++hammer_stats_file_iopsr;
3056 ip = ap->a_vp->v_data;
3060 * We can only BMAP regular files. We can't BMAP database files,
3063 if (ip->ino_data.obj_type != HAMMER_OBJTYPE_REGFILE)
3067 * bmap is typically called with runp/runb both NULL when used
3068 * for writing. We do not support BMAP for writing atm.
3070 if (ap->a_cmd != BUF_CMD_READ)
3074 * Scan the B-Tree to acquire blockmap addresses, then translate
3077 lwkt_gettoken(&hmp->fs_token);
3078 hammer_simple_transaction(&trans, hmp);
3080 kprintf("bmap_beg %016llx ip->cache %p\n",
3081 (long long)ap->a_loffset, ip->cache[1]);
3083 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
3086 * Key range (begin and end inclusive) to scan. Note that the key's
3087 * stored in the actual records represent BASE+LEN, not BASE. The
3088 * first record containing bio_offset will have a key > bio_offset.
3090 cursor.key_beg.localization = ip->obj_localization +
3091 HAMMER_LOCALIZE_MISC;
3092 cursor.key_beg.obj_id = ip->obj_id;
3093 cursor.key_beg.create_tid = 0;
3094 cursor.key_beg.delete_tid = 0;
3095 cursor.key_beg.obj_type = 0;
3097 cursor.key_beg.key = ap->a_loffset - MAXPHYS + 1;
3099 cursor.key_beg.key = ap->a_loffset + 1;
3100 if (cursor.key_beg.key < 0)
3101 cursor.key_beg.key = 0;
3102 cursor.asof = ip->obj_asof;
3103 cursor.flags |= HAMMER_CURSOR_ASOF;
3105 cursor.key_end = cursor.key_beg;
3106 KKASSERT(ip->ino_data.obj_type == HAMMER_OBJTYPE_REGFILE);
3108 ran_end = ap->a_loffset + MAXPHYS;
3109 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
3110 cursor.key_end.rec_type = HAMMER_RECTYPE_DATA;
3111 tmp64 = ran_end + MAXPHYS + 1; /* work-around GCC-4 bug */
3112 if (tmp64 < ran_end)
3113 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
3115 cursor.key_end.key = ran_end + MAXPHYS + 1;
3117 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
3119 error = hammer_ip_first(&cursor);
3120 base_offset = last_offset = 0;
3121 base_disk_offset = last_disk_offset = 0;
3123 while (error == 0) {
3125 * Get the base file offset of the record. The key for
3126 * data records is (base + bytes) rather then (base).
3128 * NOTE: rec_offset + rec_len may exceed the end-of-file.
3129 * The extra bytes should be zero on-disk and the BMAP op
3130 * should still be ok.
3132 base = &cursor.leaf->base;
3133 rec_offset = base->key - cursor.leaf->data_len;
3134 rec_len = cursor.leaf->data_len;
3137 * Incorporate any cached truncation.
3139 * NOTE: Modifications to rec_len based on synthesized
3140 * truncation points remove the guarantee that any extended
3141 * data on disk is zero (since the truncations may not have
3142 * taken place on-media yet).
3144 if (ip->flags & HAMMER_INODE_TRUNCATED) {
3145 if (hammer_cursor_ondisk(&cursor) ||
3146 cursor.iprec->flush_state == HAMMER_FST_FLUSH) {
3147 if (ip->trunc_off <= rec_offset)
3149 else if (ip->trunc_off < rec_offset + rec_len)
3150 rec_len = (int)(ip->trunc_off - rec_offset);
3153 if (ip->sync_flags & HAMMER_INODE_TRUNCATED) {
3154 if (hammer_cursor_ondisk(&cursor)) {
3155 if (ip->sync_trunc_off <= rec_offset)
3157 else if (ip->sync_trunc_off < rec_offset + rec_len)
3158 rec_len = (int)(ip->sync_trunc_off - rec_offset);
3163 * Accumulate information. If we have hit a discontiguous
3164 * block reset base_offset unless we are already beyond the
3165 * requested offset. If we are, that's it, we stop.
3169 if (hammer_cursor_ondisk(&cursor)) {
3170 disk_offset = cursor.leaf->data_offset;
3171 if (rec_offset != last_offset ||
3172 disk_offset != last_disk_offset) {
3173 if (rec_offset > ap->a_loffset)
3175 base_offset = rec_offset;
3176 base_disk_offset = disk_offset;
3178 last_offset = rec_offset + rec_len;
3179 last_disk_offset = disk_offset + rec_len;
3181 if (hammer_live_dedup)
3182 hammer_dedup_cache_add(ip, cursor.leaf);
3185 error = hammer_ip_next(&cursor);
3189 kprintf("BMAP %016llx: %016llx - %016llx\n",
3190 (long long)ap->a_loffset,
3191 (long long)base_offset,
3192 (long long)last_offset);
3193 kprintf("BMAP %16s: %016llx - %016llx\n", "",
3194 (long long)base_disk_offset,
3195 (long long)last_disk_offset);
3199 hammer_cache_node(&ip->cache[1], cursor.node);
3201 kprintf("bmap_end2 %016llx ip->cache %p\n",
3202 (long long)ap->a_loffset, ip->cache[1]);
3205 hammer_done_cursor(&cursor);
3206 hammer_done_transaction(&trans);
3207 lwkt_reltoken(&hmp->fs_token);
3210 * If we couldn't find any records or the records we did find were
3211 * all behind the requested offset, return failure. A forward
3212 * truncation can leave a hole w/ no on-disk records.
3214 if (last_offset == 0 || last_offset < ap->a_loffset)
3215 return (EOPNOTSUPP);
3218 * Figure out the block size at the requested offset and adjust
3219 * our limits so the cluster_read() does not create inappropriately
3220 * sized buffer cache buffers.
3222 blksize = hammer_blocksize(ap->a_loffset);
3223 if (hammer_blocksize(base_offset) != blksize) {
3224 base_offset = hammer_blockdemarc(base_offset, ap->a_loffset);
3226 if (last_offset != ap->a_loffset &&
3227 hammer_blocksize(last_offset - 1) != blksize) {
3228 last_offset = hammer_blockdemarc(ap->a_loffset,
3233 * Returning EOPNOTSUPP simply prevents the direct-IO optimization
3236 disk_offset = base_disk_offset + (ap->a_loffset - base_offset);
3238 if ((disk_offset & HAMMER_OFF_ZONE_MASK) != HAMMER_ZONE_LARGE_DATA) {
3240 * Only large-data zones can be direct-IOd
3243 } else if ((disk_offset & HAMMER_BUFMASK) ||
3244 (last_offset - ap->a_loffset) < blksize) {
3246 * doffsetp is not aligned or the forward run size does
3247 * not cover a whole buffer, disallow the direct I/O.
3254 *ap->a_doffsetp = disk_offset;
3256 *ap->a_runb = ap->a_loffset - base_offset;
3257 KKASSERT(*ap->a_runb >= 0);
3260 *ap->a_runp = last_offset - ap->a_loffset;
3261 KKASSERT(*ap->a_runp >= 0);
3269 * Write to a regular file. Because this is a strategy call the OS is
3270 * trying to actually get data onto the media.
3274 hammer_vop_strategy_write(struct vop_strategy_args *ap)
3276 hammer_record_t record;
3281 int blksize __debugvar;
3287 ip = ap->a_vp->v_data;
3290 blksize = hammer_blocksize(bio->bio_offset);
3291 KKASSERT(bp->b_bufsize == blksize);
3293 if (ip->flags & HAMMER_INODE_RO) {
3294 bp->b_error = EROFS;
3295 bp->b_flags |= B_ERROR;
3300 lwkt_gettoken(&hmp->fs_token);
3303 * Disallow swapcache operation on the vnode buffer if double
3304 * buffering is enabled, the swapcache will get the data via
3305 * the block device buffer.
3307 if (hammer_double_buffer)
3308 bp->b_flags |= B_NOTMETA;
3311 * Interlock with inode destruction (no in-kernel or directory
3312 * topology visibility). If we queue new IO while trying to
3313 * destroy the inode we can deadlock the vtrunc call in
3314 * hammer_inode_unloadable_check().
3316 * Besides, there's no point flushing a bp associated with an
3317 * inode that is being destroyed on-media and has no kernel
3320 if ((ip->flags | ip->sync_flags) &
3321 (HAMMER_INODE_DELETING|HAMMER_INODE_DELETED)) {
3324 lwkt_reltoken(&hmp->fs_token);
3329 * Reserve space and issue a direct-write from the front-end.
3330 * NOTE: The direct_io code will hammer_bread/bcopy smaller
3333 * An in-memory record will be installed to reference the storage
3334 * until the flusher can get to it.
3336 * Since we own the high level bio the front-end will not try to
3337 * do a direct-read until the write completes.
3339 * NOTE: The only time we do not reserve a full-sized buffers
3340 * worth of data is if the file is small. We do not try to
3341 * allocate a fragment (from the small-data zone) at the end of
3342 * an otherwise large file as this can lead to wildly separated
3345 KKASSERT((bio->bio_offset & HAMMER_BUFMASK) == 0);
3346 KKASSERT(bio->bio_offset < ip->ino_data.size);
3347 if (bio->bio_offset || ip->ino_data.size > HAMMER_BUFSIZE / 2)
3348 bytes = bp->b_bufsize;
3350 bytes = ((int)ip->ino_data.size + 15) & ~15;
3352 record = hammer_ip_add_bulk(ip, bio->bio_offset, bp->b_data,
3356 * B_VFSFLAG1 indicates that a REDO_WRITE entry was generated
3357 * in hammer_vop_write(). We must flag the record so the proper
3358 * REDO_TERM_WRITE entry is generated during the flush.
3361 if (bp->b_flags & B_VFSFLAG1) {
3362 record->flags |= HAMMER_RECF_REDO;
3363 bp->b_flags &= ~B_VFSFLAG1;
3365 if (record->flags & HAMMER_RECF_DEDUPED) {
3367 hammer_ip_replace_bulk(hmp, record);
3370 hammer_io_direct_write(hmp, bio, record);
3372 if (ip->rsv_recs > 1 && hmp->rsv_recs > hammer_limit_recs)
3373 hammer_flush_inode(ip, 0);
3375 bp->b_bio2.bio_offset = NOOFFSET;
3376 bp->b_error = error;
3377 bp->b_flags |= B_ERROR;
3380 lwkt_reltoken(&hmp->fs_token);
3385 * dounlink - disconnect a directory entry
3387 * XXX whiteout support not really in yet
3390 hammer_dounlink(hammer_transaction_t trans, struct nchandle *nch,
3391 struct vnode *dvp, struct ucred *cred,
3392 int flags, int isdir)
3394 struct namecache *ncp;
3398 struct hammer_cursor cursor;
3400 u_int32_t max_iterations;
3404 * Calculate the namekey and setup the key range for the scan. This
3405 * works kinda like a chained hash table where the lower 32 bits
3406 * of the namekey synthesize the chain.
3408 * The key range is inclusive of both key_beg and key_end.
3414 if (dip->flags & HAMMER_INODE_RO)
3417 namekey = hammer_directory_namekey(dip, ncp->nc_name, ncp->nc_nlen,
3420 hammer_init_cursor(trans, &cursor, &dip->cache[1], dip);
3421 cursor.key_beg.localization = dip->obj_localization +
3422 hammer_dir_localization(dip);
3423 cursor.key_beg.obj_id = dip->obj_id;
3424 cursor.key_beg.key = namekey;
3425 cursor.key_beg.create_tid = 0;
3426 cursor.key_beg.delete_tid = 0;
3427 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
3428 cursor.key_beg.obj_type = 0;
3430 cursor.key_end = cursor.key_beg;
3431 cursor.key_end.key += max_iterations;
3432 cursor.asof = dip->obj_asof;
3433 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
3436 * Scan all matching records (the chain), locate the one matching
3437 * the requested path component. info->last_error contains the
3438 * error code on search termination and could be 0, ENOENT, or
3441 * The hammer_ip_*() functions merge in-memory records with on-disk
3442 * records for the purposes of the search.
3444 error = hammer_ip_first(&cursor);
3446 while (error == 0) {
3447 error = hammer_ip_resolve_data(&cursor);
3450 nlen = cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF;
3452 if (ncp->nc_nlen == nlen &&
3453 bcmp(ncp->nc_name, cursor.data->entry.name, nlen) == 0) {
3456 error = hammer_ip_next(&cursor);
3460 * If all is ok we have to get the inode so we can adjust nlinks.
3461 * To avoid a deadlock with the flusher we must release the inode
3462 * lock on the directory when acquiring the inode for the entry.
3464 * If the target is a directory, it must be empty.
3467 hammer_unlock(&cursor.ip->lock);
3468 ip = hammer_get_inode(trans, dip, cursor.data->entry.obj_id,
3470 cursor.data->entry.localization,
3472 hammer_lock_sh(&cursor.ip->lock);
3473 if (error == ENOENT) {
3474 kprintf("HAMMER: WARNING: Removing "
3475 "dirent w/missing inode \"%s\"\n"
3476 "\tobj_id = %016llx\n",
3478 (long long)cursor.data->entry.obj_id);
3483 * If isdir >= 0 we validate that the entry is or is not a
3484 * directory. If isdir < 0 we don't care.
3486 if (error == 0 && isdir >= 0 && ip) {
3488 ip->ino_data.obj_type != HAMMER_OBJTYPE_DIRECTORY) {
3490 } else if (isdir == 0 &&
3491 ip->ino_data.obj_type == HAMMER_OBJTYPE_DIRECTORY) {
3497 * If we are trying to remove a directory the directory must
3500 * The check directory code can loop and deadlock/retry. Our
3501 * own cursor's node locks must be released to avoid a 3-way
3502 * deadlock with the flusher if the check directory code
3505 * If any changes whatsoever have been made to the cursor
3506 * set EDEADLK and retry.
3508 * WARNING: See warnings in hammer_unlock_cursor()
3511 if (error == 0 && ip && ip->ino_data.obj_type ==
3512 HAMMER_OBJTYPE_DIRECTORY) {
3513 hammer_unlock_cursor(&cursor);
3514 error = hammer_ip_check_directory_empty(trans, ip);
3515 hammer_lock_cursor(&cursor);
3516 if (cursor.flags & HAMMER_CURSOR_RETEST) {
3517 kprintf("HAMMER: Warning: avoided deadlock "
3525 * Delete the directory entry.
3527 * WARNING: hammer_ip_del_directory() may have to terminate
3528 * the cursor to avoid a deadlock. It is ok to call
3529 * hammer_done_cursor() twice.
3532 error = hammer_ip_del_directory(trans, &cursor,
3535 hammer_done_cursor(&cursor);
3538 * Tell the namecache that we are now unlinked.
3543 * NOTE: ip->vp, if non-NULL, cannot be directly
3544 * referenced without formally acquiring the
3545 * vp since the vp might have zero refs on it,
3546 * or in the middle of a reclaim, etc.
3548 * NOTE: The cache_setunresolved() can rip the vp
3549 * out from under us since the vp may not have
3550 * any refs, in which case ip->vp will be NULL
3553 while (ip && ip->vp) {
3556 error = hammer_get_vnode(ip, &vp);
3557 if (error == 0 && vp) {
3559 hammer_knote(ip->vp, NOTE_DELETE);
3562 * Don't do this, it can deadlock
3563 * on concurrent rm's of hardlinks.
3564 * Shouldn't be needed any more.
3566 cache_inval_vp(ip->vp, CINV_DESTROY);
3571 kprintf("Debug: HAMMER ip/vp race1 avoided\n");
3575 hammer_rel_inode(ip, 0);
3577 hammer_done_cursor(&cursor);
3579 if (error == EDEADLK)
3585 /************************************************************************
3586 * FIFO AND SPECFS OPS *
3587 ************************************************************************
3591 hammer_vop_fifoclose (struct vop_close_args *ap)
3593 /* XXX update itimes */
3594 return (VOCALL(&fifo_vnode_vops, &ap->a_head));
3598 hammer_vop_fiforead (struct vop_read_args *ap)
3602 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
3603 /* XXX update access time */
3608 hammer_vop_fifowrite (struct vop_write_args *ap)
3612 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
3613 /* XXX update access time */
3619 hammer_vop_fifokqfilter(struct vop_kqfilter_args *ap)
3623 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
3625 error = hammer_vop_kqfilter(ap);
3629 /************************************************************************
3631 ************************************************************************
3634 static void filt_hammerdetach(struct knote *kn);
3635 static int filt_hammerread(struct knote *kn, long hint);
3636 static int filt_hammerwrite(struct knote *kn, long hint);
3637 static int filt_hammervnode(struct knote *kn, long hint);
3639 static struct filterops hammerread_filtops =
3640 { FILTEROP_ISFD | FILTEROP_MPSAFE,
3641 NULL, filt_hammerdetach, filt_hammerread };
3642 static struct filterops hammerwrite_filtops =
3643 { FILTEROP_ISFD | FILTEROP_MPSAFE,
3644 NULL, filt_hammerdetach, filt_hammerwrite };
3645 static struct filterops hammervnode_filtops =
3646 { FILTEROP_ISFD | FILTEROP_MPSAFE,
3647 NULL, filt_hammerdetach, filt_hammervnode };
3651 hammer_vop_kqfilter(struct vop_kqfilter_args *ap)
3653 struct vnode *vp = ap->a_vp;
3654 struct knote *kn = ap->a_kn;
3656 switch (kn->kn_filter) {
3658 kn->kn_fop = &hammerread_filtops;
3661 kn->kn_fop = &hammerwrite_filtops;
3664 kn->kn_fop = &hammervnode_filtops;
3667 return (EOPNOTSUPP);
3670 kn->kn_hook = (caddr_t)vp;
3672 knote_insert(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
3678 filt_hammerdetach(struct knote *kn)
3680 struct vnode *vp = (void *)kn->kn_hook;
3682 knote_remove(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
3686 filt_hammerread(struct knote *kn, long hint)
3688 struct vnode *vp = (void *)kn->kn_hook;
3689 hammer_inode_t ip = VTOI(vp);
3690 hammer_mount_t hmp = ip->hmp;
3693 if (hint == NOTE_REVOKE) {
3694 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
3697 lwkt_gettoken(&hmp->fs_token); /* XXX use per-ip-token */
3698 off = ip->ino_data.size - kn->kn_fp->f_offset;
3699 kn->kn_data = (off < INTPTR_MAX) ? off : INTPTR_MAX;
3700 lwkt_reltoken(&hmp->fs_token);
3701 if (kn->kn_sfflags & NOTE_OLDAPI)
3703 return (kn->kn_data != 0);
3707 filt_hammerwrite(struct knote *kn, long hint)
3709 if (hint == NOTE_REVOKE)
3710 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
3716 filt_hammervnode(struct knote *kn, long hint)
3718 if (kn->kn_sfflags & hint)
3719 kn->kn_fflags |= hint;
3720 if (hint == NOTE_REVOKE) {
3721 kn->kn_flags |= (EV_EOF | EV_NODATA);
3724 return (kn->kn_fflags != 0);