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
262 ++hammer_count_fsyncs;
263 hammer_flusher_flush_undos(hmp, mode);
265 if (ip->vp && (ip->flags & HAMMER_INODE_MODMASK) == 0)
267 lwkt_reltoken(&hmp->fs_token);
272 * REDO is enabled by fsync(), the idea being we really only
273 * want to lay down REDO records when programs are using
274 * fsync() heavily. The first fsync() on the file starts
275 * the gravy train going and later fsync()s keep it hot by
276 * resetting the redo_count.
278 * We weren't running REDOs before now so we have to fall
279 * through and do a full fsync of what we have.
281 if (hmp->version >= HAMMER_VOL_VERSION_FOUR &&
282 (hmp->flags & HAMMER_MOUNT_REDO_RECOVERY_RUN) == 0) {
283 ip->flags |= HAMMER_INODE_REDO;
290 * Do a full flush sequence.
292 * Attempt to release the vnode while waiting for the inode to
293 * finish flushing. This can really mess up inactive->reclaim
294 * sequences so only do it if the vnode is active.
296 * WARNING! The VX lock functions must be used. vn_lock() will
297 * fail when this is part of a VOP_RECLAIM sequence.
299 ++hammer_count_fsyncs;
300 vfsync(ap->a_vp, waitfor, 1, NULL, NULL);
301 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
302 if (waitfor == MNT_WAIT) {
305 if ((ap->a_vp->v_flag & VRECLAIMED) == 0) {
311 hammer_wait_inode(ip);
315 if (ip->vp && (ip->flags & HAMMER_INODE_MODMASK) == 0)
317 lwkt_reltoken(&hmp->fs_token);
322 * hammer_vop_read { vp, uio, ioflag, cred }
324 * MPSAFE (for the cache safe does not require fs_token)
328 hammer_vop_read(struct vop_read_args *ap)
330 struct hammer_transaction trans;
345 if (ap->a_vp->v_type != VREG)
354 * Attempt to shortcut directly to the VM object using lwbufs.
355 * This is much faster than instantiating buffer cache buffers.
357 resid = uio->uio_resid;
358 error = vop_helper_read_shortcut(ap);
359 hammer_stats_file_read += resid - uio->uio_resid;
362 if (uio->uio_resid == 0)
366 * Allow the UIO's size to override the sequential heuristic.
368 blksize = hammer_blocksize(uio->uio_offset);
369 seqcount = (uio->uio_resid + (BKVASIZE - 1)) / BKVASIZE;
370 ioseqcount = (ap->a_ioflag >> 16);
371 if (seqcount < ioseqcount)
372 seqcount = ioseqcount;
375 * If reading or writing a huge amount of data we have to break
376 * atomicy and allow the operation to be interrupted by a signal
377 * or it can DOS the machine.
379 bigread = (uio->uio_resid > 100 * 1024 * 1024);
382 * Access the data typically in HAMMER_BUFSIZE blocks via the
383 * buffer cache, but HAMMER may use a variable block size based
386 * XXX Temporary hack, delay the start transaction while we remain
387 * MPSAFE. NOTE: ino_data.size cannot change while vnode is
390 while (uio->uio_resid > 0 && uio->uio_offset < ip->ino_data.size) {
394 blksize = hammer_blocksize(uio->uio_offset);
395 offset = (int)uio->uio_offset & (blksize - 1);
396 base_offset = uio->uio_offset - offset;
398 if (bigread && (error = hammer_signal_check(ip->hmp)) != 0)
404 bp = getblk(ap->a_vp, base_offset, blksize, 0, 0);
405 if ((bp->b_flags & (B_INVAL | B_CACHE | B_RAM)) == B_CACHE) {
406 bp->b_flags &= ~B_AGE;
410 if (ap->a_ioflag & IO_NRDELAY) {
412 return (EWOULDBLOCK);
418 if (got_fstoken == 0) {
419 lwkt_gettoken(&hmp->fs_token);
421 hammer_start_transaction(&trans, ip->hmp);
425 * NOTE: A valid bp has already been acquired, but was not
428 if (hammer_cluster_enable) {
430 * Use file_limit to prevent cluster_read() from
431 * creating buffers of the wrong block size past
434 file_limit = ip->ino_data.size;
435 if (base_offset < HAMMER_XDEMARC &&
436 file_limit > HAMMER_XDEMARC) {
437 file_limit = HAMMER_XDEMARC;
439 error = cluster_readx(ap->a_vp,
440 file_limit, base_offset,
441 blksize, uio->uio_resid,
442 seqcount * BKVASIZE, &bp);
444 error = breadnx(ap->a_vp, base_offset, blksize,
452 if ((hammer_debug_io & 0x0001) && (bp->b_flags & B_IODEBUG)) {
453 kprintf("doff %016jx read file %016jx@%016jx\n",
454 (intmax_t)bp->b_bio2.bio_offset,
455 (intmax_t)ip->obj_id,
456 (intmax_t)bp->b_loffset);
458 bp->b_flags &= ~B_IODEBUG;
459 if (blksize == HAMMER_XBUFSIZE)
460 bp->b_flags |= B_CLUSTEROK;
462 n = blksize - offset;
463 if (n > uio->uio_resid)
465 if (n > ip->ino_data.size - uio->uio_offset)
466 n = (int)(ip->ino_data.size - uio->uio_offset);
468 lwkt_reltoken(&hmp->fs_token);
471 * Set B_AGE, data has a lower priority than meta-data.
473 * Use a hold/unlock/drop sequence to run the uiomove
474 * with the buffer unlocked, avoiding deadlocks against
475 * read()s on mmap()'d spaces.
477 bp->b_flags |= B_AGE;
478 error = uiomovebp(bp, (char *)bp->b_data + offset, n, uio);
482 lwkt_gettoken(&hmp->fs_token);
486 hammer_stats_file_read += n;
492 * Try to update the atime with just the inode lock for maximum
493 * concurrency. If we can't shortcut it we have to get the full
496 if (got_fstoken == 0 && hammer_update_atime_quick(ip) < 0) {
497 lwkt_gettoken(&hmp->fs_token);
499 hammer_start_transaction(&trans, ip->hmp);
503 if ((ip->flags & HAMMER_INODE_RO) == 0 &&
504 (ip->hmp->mp->mnt_flag & MNT_NOATIME) == 0) {
505 ip->ino_data.atime = trans.time;
506 hammer_modify_inode(&trans, ip, HAMMER_INODE_ATIME);
508 hammer_done_transaction(&trans);
509 lwkt_reltoken(&hmp->fs_token);
515 * hammer_vop_write { vp, uio, ioflag, cred }
519 hammer_vop_write(struct vop_write_args *ap)
521 struct hammer_transaction trans;
522 struct hammer_inode *ip;
537 if (ap->a_vp->v_type != VREG)
543 seqcount = ap->a_ioflag >> 16;
545 if (ip->flags & HAMMER_INODE_RO)
549 * Create a transaction to cover the operations we perform.
551 lwkt_gettoken(&hmp->fs_token);
552 hammer_start_transaction(&trans, hmp);
558 if (ap->a_ioflag & IO_APPEND)
559 uio->uio_offset = ip->ino_data.size;
562 * Check for illegal write offsets. Valid range is 0...2^63-1.
564 * NOTE: the base_off assignment is required to work around what
565 * I consider to be a GCC-4 optimization bug.
567 if (uio->uio_offset < 0) {
568 hammer_done_transaction(&trans);
569 lwkt_reltoken(&hmp->fs_token);
572 base_offset = uio->uio_offset + uio->uio_resid; /* work around gcc-4 */
573 if (uio->uio_resid > 0 && base_offset <= uio->uio_offset) {
574 hammer_done_transaction(&trans);
575 lwkt_reltoken(&hmp->fs_token);
579 if (uio->uio_resid > 0 && (td = uio->uio_td) != NULL && td->td_proc &&
580 base_offset > td->td_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
581 hammer_done_transaction(&trans);
582 lwkt_reltoken(&hmp->fs_token);
583 lwpsignal(td->td_proc, td->td_lwp, SIGXFSZ);
588 * If reading or writing a huge amount of data we have to break
589 * atomicy and allow the operation to be interrupted by a signal
590 * or it can DOS the machine.
592 * Preset redo_count so we stop generating REDOs earlier if the
595 bigwrite = (uio->uio_resid > 100 * 1024 * 1024);
596 if ((ip->flags & HAMMER_INODE_REDO) &&
597 ip->redo_count < hammer_limit_redo) {
598 ip->redo_count += uio->uio_resid;
602 * Access the data typically in HAMMER_BUFSIZE blocks via the
603 * buffer cache, but HAMMER may use a variable block size based
606 while (uio->uio_resid > 0) {
614 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_WRITE)) != 0)
616 if (bigwrite && (error = hammer_signal_check(hmp)) != 0)
619 blksize = hammer_blocksize(uio->uio_offset);
622 * Do not allow HAMMER to blow out the buffer cache. Very
623 * large UIOs can lockout other processes due to bwillwrite()
626 * The hammer inode is not locked during these operations.
627 * The vnode is locked which can interfere with the pageout
628 * daemon for non-UIO_NOCOPY writes but should not interfere
629 * with the buffer cache. Even so, we cannot afford to
630 * allow the pageout daemon to build up too many dirty buffer
633 * Only call this if we aren't being recursively called from
634 * a virtual disk device (vn), else we may deadlock.
636 if ((ap->a_ioflag & IO_RECURSE) == 0)
640 * Control the number of pending records associated with
641 * this inode. If too many have accumulated start a
642 * flush. Try to maintain a pipeline with the flusher.
644 * NOTE: It is possible for other sources to grow the
645 * records but not necessarily issue another flush,
646 * so use a timeout and ensure that a re-flush occurs.
648 if (ip->rsv_recs >= hammer_limit_inode_recs) {
649 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
650 while (ip->rsv_recs >= hammer_limit_inode_recs * 2) {
651 ip->flags |= HAMMER_INODE_RECSW;
652 tsleep(&ip->rsv_recs, 0, "hmrwww", hz);
653 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
659 * Do not allow HAMMER to blow out system memory by
660 * accumulating too many records. Records are so well
661 * decoupled from the buffer cache that it is possible
662 * for userland to push data out to the media via
663 * direct-write, but build up the records queued to the
664 * backend faster then the backend can flush them out.
665 * HAMMER has hit its write limit but the frontend has
666 * no pushback to slow it down.
668 if (hmp->rsv_recs > hammer_limit_recs / 2) {
670 * Get the inode on the flush list
672 if (ip->rsv_recs >= 64)
673 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
674 else if (ip->rsv_recs >= 16)
675 hammer_flush_inode(ip, 0);
678 * Keep the flusher going if the system keeps
681 delta = hmp->count_newrecords -
682 hmp->last_newrecords;
683 if (delta < 0 || delta > hammer_limit_recs / 2) {
684 hmp->last_newrecords = hmp->count_newrecords;
685 hammer_sync_hmp(hmp, MNT_NOWAIT);
689 * If we have gotten behind start slowing
692 delta = (hmp->rsv_recs - hammer_limit_recs) *
693 hz / hammer_limit_recs;
695 tsleep(&trans, 0, "hmrslo", delta);
700 * Calculate the blocksize at the current offset and figure
701 * out how much we can actually write.
703 blkmask = blksize - 1;
704 offset = (int)uio->uio_offset & blkmask;
705 base_offset = uio->uio_offset & ~(int64_t)blkmask;
706 n = blksize - offset;
707 if (n > uio->uio_resid) {
713 nsize = uio->uio_offset + n;
714 if (nsize > ip->ino_data.size) {
715 if (uio->uio_offset > ip->ino_data.size)
719 nvextendbuf(ap->a_vp,
722 hammer_blocksize(ip->ino_data.size),
723 hammer_blocksize(nsize),
724 hammer_blockoff(ip->ino_data.size),
725 hammer_blockoff(nsize),
728 kflags |= NOTE_EXTEND;
731 if (uio->uio_segflg == UIO_NOCOPY) {
733 * Issuing a write with the same data backing the
734 * buffer. Instantiate the buffer to collect the
735 * backing vm pages, then read-in any missing bits.
737 * This case is used by vop_stdputpages().
739 bp = getblk(ap->a_vp, base_offset,
740 blksize, GETBLK_BHEAVY, 0);
741 if ((bp->b_flags & B_CACHE) == 0) {
743 error = bread(ap->a_vp, base_offset,
746 } else if (offset == 0 && uio->uio_resid >= blksize) {
748 * Even though we are entirely overwriting the buffer
749 * we may still have to zero it out to avoid a
750 * mmap/write visibility issue.
752 bp = getblk(ap->a_vp, base_offset, blksize, GETBLK_BHEAVY, 0);
753 if ((bp->b_flags & B_CACHE) == 0)
755 } else if (base_offset >= ip->ino_data.size) {
757 * If the base offset of the buffer is beyond the
758 * file EOF, we don't have to issue a read.
760 bp = getblk(ap->a_vp, base_offset,
761 blksize, GETBLK_BHEAVY, 0);
765 * Partial overwrite, read in any missing bits then
766 * replace the portion being written.
768 error = bread(ap->a_vp, base_offset, blksize, &bp);
773 lwkt_reltoken(&hmp->fs_token);
774 error = uiomovebp(bp, bp->b_data + offset, n, uio);
775 lwkt_gettoken(&hmp->fs_token);
779 * Generate REDO records if enabled and redo_count will not
780 * exceeded the limit.
782 * If redo_count exceeds the limit we stop generating records
783 * and clear HAMMER_INODE_REDO. This will cause the next
784 * fsync() to do a full meta-data sync instead of just an
785 * UNDO/REDO fifo update.
787 * When clearing HAMMER_INODE_REDO any pre-existing REDOs
788 * will still be tracked. The tracks will be terminated
789 * when the related meta-data (including possible data
790 * modifications which are not tracked via REDO) is
793 if ((ip->flags & HAMMER_INODE_REDO) && error == 0) {
794 if (ip->redo_count < hammer_limit_redo) {
795 bp->b_flags |= B_VFSFLAG1;
796 error = hammer_generate_redo(&trans, ip,
797 base_offset + offset,
802 ip->flags &= ~HAMMER_INODE_REDO;
807 * If we screwed up we have to undo any VM size changes we
813 nvtruncbuf(ap->a_vp, ip->ino_data.size,
814 hammer_blocksize(ip->ino_data.size),
815 hammer_blockoff(ip->ino_data.size),
820 kflags |= NOTE_WRITE;
821 hammer_stats_file_write += n;
822 if (blksize == HAMMER_XBUFSIZE)
823 bp->b_flags |= B_CLUSTEROK;
824 if (ip->ino_data.size < uio->uio_offset) {
825 ip->ino_data.size = uio->uio_offset;
826 flags = HAMMER_INODE_SDIRTY;
830 ip->ino_data.mtime = trans.time;
831 flags |= HAMMER_INODE_MTIME | HAMMER_INODE_BUFS;
832 hammer_modify_inode(&trans, ip, flags);
835 * Once we dirty the buffer any cached zone-X offset
836 * becomes invalid. HAMMER NOTE: no-history mode cannot
837 * allow overwriting over the same data sector unless
838 * we provide UNDOs for the old data, which we don't.
840 bp->b_bio2.bio_offset = NOOFFSET;
843 * Final buffer disposition.
845 * Because meta-data updates are deferred, HAMMER is
846 * especially sensitive to excessive bdwrite()s because
847 * the I/O stream is not broken up by disk reads. So the
848 * buffer cache simply cannot keep up.
850 * WARNING! blksize is variable. cluster_write() is
851 * expected to not blow up if it encounters
852 * buffers that do not match the passed blksize.
854 * NOTE! Hammer shouldn't need to bawrite()/cluster_write().
855 * The ip->rsv_recs check should burst-flush the data.
856 * If we queue it immediately the buf could be left
857 * locked on the device queue for a very long time.
859 * However, failing to flush a dirty buffer out when
860 * issued from the pageout daemon can result in a low
861 * memory deadlock against bio_page_alloc(), so we
862 * have to bawrite() on IO_ASYNC as well.
864 * NOTE! To avoid degenerate stalls due to mismatched block
865 * sizes we only honor IO_DIRECT on the write which
866 * abuts the end of the buffer. However, we must
867 * honor IO_SYNC in case someone is silly enough to
868 * configure a HAMMER file as swap, or when HAMMER
869 * is serving NFS (for commits). Ick ick.
871 bp->b_flags |= B_AGE;
872 if (blksize == HAMMER_XBUFSIZE)
873 bp->b_flags |= B_CLUSTEROK;
875 if (ap->a_ioflag & IO_SYNC) {
877 } else if ((ap->a_ioflag & IO_DIRECT) && endofblk) {
879 } else if (ap->a_ioflag & IO_ASYNC) {
881 } else if (hammer_cluster_enable &&
882 !(ap->a_vp->v_mount->mnt_flag & MNT_NOCLUSTERW)) {
883 if (base_offset < HAMMER_XDEMARC)
884 cluster_eof = hammer_blockdemarc(base_offset,
887 cluster_eof = ip->ino_data.size;
888 cluster_write(bp, cluster_eof, blksize, seqcount);
893 hammer_done_transaction(&trans);
894 hammer_knote(ap->a_vp, kflags);
895 lwkt_reltoken(&hmp->fs_token);
900 * hammer_vop_access { vp, mode, cred }
902 * MPSAFE - does not require fs_token
906 hammer_vop_access(struct vop_access_args *ap)
908 struct hammer_inode *ip = VTOI(ap->a_vp);
913 ++hammer_stats_file_iopsr;
914 uid = hammer_to_unix_xid(&ip->ino_data.uid);
915 gid = hammer_to_unix_xid(&ip->ino_data.gid);
917 error = vop_helper_access(ap, uid, gid, ip->ino_data.mode,
918 ip->ino_data.uflags);
923 * hammer_vop_advlock { vp, id, op, fl, flags }
925 * MPSAFE - does not require fs_token
929 hammer_vop_advlock(struct vop_advlock_args *ap)
931 hammer_inode_t ip = VTOI(ap->a_vp);
933 return (lf_advlock(ap, &ip->advlock, ip->ino_data.size));
937 * hammer_vop_close { vp, fflag }
939 * We can only sync-on-close for normal closes. XXX disabled for now.
943 hammer_vop_close(struct vop_close_args *ap)
946 struct vnode *vp = ap->a_vp;
947 hammer_inode_t ip = VTOI(vp);
949 if (ip->flags & (HAMMER_INODE_CLOSESYNC|HAMMER_INODE_CLOSEASYNC)) {
950 if (vn_islocked(vp) == LK_EXCLUSIVE &&
951 (vp->v_flag & (VINACTIVE|VRECLAIMED)) == 0) {
952 if (ip->flags & HAMMER_INODE_CLOSESYNC)
955 waitfor = MNT_NOWAIT;
956 ip->flags &= ~(HAMMER_INODE_CLOSESYNC |
957 HAMMER_INODE_CLOSEASYNC);
958 VOP_FSYNC(vp, MNT_NOWAIT, waitfor);
962 return (vop_stdclose(ap));
966 * hammer_vop_ncreate { nch, dvp, vpp, cred, vap }
968 * The operating system has already ensured that the directory entry
969 * does not exist and done all appropriate namespace locking.
973 hammer_vop_ncreate(struct vop_ncreate_args *ap)
975 struct hammer_transaction trans;
976 struct hammer_inode *dip;
977 struct hammer_inode *nip;
978 struct nchandle *nch;
983 dip = VTOI(ap->a_dvp);
986 if (dip->flags & HAMMER_INODE_RO)
988 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0)
992 * Create a transaction to cover the operations we perform.
994 lwkt_gettoken(&hmp->fs_token);
995 hammer_start_transaction(&trans, hmp);
996 ++hammer_stats_file_iopsw;
999 * Create a new filesystem object of the requested type. The
1000 * returned inode will be referenced and shared-locked to prevent
1001 * it from being moved to the flusher.
1003 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
1004 dip, nch->ncp->nc_name, nch->ncp->nc_nlen,
1007 hkprintf("hammer_create_inode error %d\n", error);
1008 hammer_done_transaction(&trans);
1010 lwkt_reltoken(&hmp->fs_token);
1015 * Add the new filesystem object to the directory. This will also
1016 * bump the inode's link count.
1018 error = hammer_ip_add_directory(&trans, dip,
1019 nch->ncp->nc_name, nch->ncp->nc_nlen,
1022 hkprintf("hammer_ip_add_directory error %d\n", error);
1028 hammer_rel_inode(nip, 0);
1029 hammer_done_transaction(&trans);
1032 error = hammer_get_vnode(nip, ap->a_vpp);
1033 hammer_done_transaction(&trans);
1034 hammer_rel_inode(nip, 0);
1036 cache_setunresolved(ap->a_nch);
1037 cache_setvp(ap->a_nch, *ap->a_vpp);
1039 hammer_knote(ap->a_dvp, NOTE_WRITE);
1041 lwkt_reltoken(&hmp->fs_token);
1046 * hammer_vop_getattr { vp, vap }
1048 * Retrieve an inode's attribute information. When accessing inodes
1049 * historically we fake the atime field to ensure consistent results.
1050 * The atime field is stored in the B-Tree element and allowed to be
1051 * updated without cycling the element.
1053 * MPSAFE - does not require fs_token
1057 hammer_vop_getattr(struct vop_getattr_args *ap)
1059 struct hammer_inode *ip = VTOI(ap->a_vp);
1060 struct vattr *vap = ap->a_vap;
1063 * We want the fsid to be different when accessing a filesystem
1064 * with different as-of's so programs like diff don't think
1065 * the files are the same.
1067 * We also want the fsid to be the same when comparing snapshots,
1068 * or when comparing mirrors (which might be backed by different
1069 * physical devices). HAMMER fsids are based on the PFS's
1070 * shared_uuid field.
1072 * XXX there is a chance of collision here. The va_fsid reported
1073 * by stat is different from the more involved fsid used in the
1076 ++hammer_stats_file_iopsr;
1077 hammer_lock_sh(&ip->lock);
1078 vap->va_fsid = ip->pfsm->fsid_udev ^ (u_int32_t)ip->obj_asof ^
1079 (u_int32_t)(ip->obj_asof >> 32);
1081 vap->va_fileid = ip->ino_leaf.base.obj_id;
1082 vap->va_mode = ip->ino_data.mode;
1083 vap->va_nlink = ip->ino_data.nlinks;
1084 vap->va_uid = hammer_to_unix_xid(&ip->ino_data.uid);
1085 vap->va_gid = hammer_to_unix_xid(&ip->ino_data.gid);
1088 vap->va_size = ip->ino_data.size;
1091 * Special case for @@PFS softlinks. The actual size of the
1092 * expanded softlink is "@@0x%016llx:%05d" == 26 bytes.
1093 * or for MAX_TID is "@@-1:%05d" == 10 bytes.
1095 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_SOFTLINK &&
1096 ip->ino_data.size == 10 &&
1097 ip->obj_asof == HAMMER_MAX_TID &&
1098 ip->obj_localization == 0 &&
1099 strncmp(ip->ino_data.ext.symlink, "@@PFS", 5) == 0) {
1100 if (ip->pfsm->pfsd.mirror_flags & HAMMER_PFSD_SLAVE)
1107 * We must provide a consistent atime and mtime for snapshots
1108 * so people can do a 'tar cf - ... | md5' on them and get
1109 * consistent results.
1111 if (ip->flags & HAMMER_INODE_RO) {
1112 hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_atime);
1113 hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_mtime);
1115 hammer_time_to_timespec(ip->ino_data.atime, &vap->va_atime);
1116 hammer_time_to_timespec(ip->ino_data.mtime, &vap->va_mtime);
1118 hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_ctime);
1119 vap->va_flags = ip->ino_data.uflags;
1120 vap->va_gen = 1; /* hammer inums are unique for all time */
1121 vap->va_blocksize = HAMMER_BUFSIZE;
1122 if (ip->ino_data.size >= HAMMER_XDEMARC) {
1123 vap->va_bytes = (ip->ino_data.size + HAMMER_XBUFMASK64) &
1125 } else if (ip->ino_data.size > HAMMER_BUFSIZE / 2) {
1126 vap->va_bytes = (ip->ino_data.size + HAMMER_BUFMASK64) &
1129 vap->va_bytes = (ip->ino_data.size + 15) & ~15;
1132 vap->va_type = hammer_get_vnode_type(ip->ino_data.obj_type);
1133 vap->va_filerev = 0; /* XXX */
1134 vap->va_uid_uuid = ip->ino_data.uid;
1135 vap->va_gid_uuid = ip->ino_data.gid;
1136 vap->va_fsid_uuid = ip->hmp->fsid;
1137 vap->va_vaflags = VA_UID_UUID_VALID | VA_GID_UUID_VALID |
1140 switch (ip->ino_data.obj_type) {
1141 case HAMMER_OBJTYPE_CDEV:
1142 case HAMMER_OBJTYPE_BDEV:
1143 vap->va_rmajor = ip->ino_data.rmajor;
1144 vap->va_rminor = ip->ino_data.rminor;
1149 hammer_unlock(&ip->lock);
1154 * hammer_vop_nresolve { nch, dvp, cred }
1156 * Locate the requested directory entry.
1160 hammer_vop_nresolve(struct vop_nresolve_args *ap)
1162 struct hammer_transaction trans;
1163 struct namecache *ncp;
1168 struct hammer_cursor cursor;
1177 u_int32_t localization;
1178 u_int32_t max_iterations;
1181 * Misc initialization, plus handle as-of name extensions. Look for
1182 * the '@@' extension. Note that as-of files and directories cannot
1185 dip = VTOI(ap->a_dvp);
1186 ncp = ap->a_nch->ncp;
1187 asof = dip->obj_asof;
1188 localization = dip->obj_localization; /* for code consistency */
1189 nlen = ncp->nc_nlen;
1190 flags = dip->flags & HAMMER_INODE_RO;
1194 lwkt_gettoken(&hmp->fs_token);
1195 hammer_simple_transaction(&trans, hmp);
1196 ++hammer_stats_file_iopsr;
1198 for (i = 0; i < nlen; ++i) {
1199 if (ncp->nc_name[i] == '@' && ncp->nc_name[i+1] == '@') {
1200 error = hammer_str_to_tid(ncp->nc_name + i + 2,
1201 &ispfs, &asof, &localization);
1206 if (asof != HAMMER_MAX_TID)
1207 flags |= HAMMER_INODE_RO;
1214 * If this is a PFS softlink we dive into the PFS
1216 if (ispfs && nlen == 0) {
1217 ip = hammer_get_inode(&trans, dip, HAMMER_OBJID_ROOT,
1221 error = hammer_get_vnode(ip, &vp);
1222 hammer_rel_inode(ip, 0);
1228 cache_setvp(ap->a_nch, vp);
1235 * If there is no path component the time extension is relative to dip.
1236 * e.g. "fubar/@@<snapshot>"
1238 * "." is handled by the kernel, but ".@@<snapshot>" is not.
1239 * e.g. "fubar/.@@<snapshot>"
1241 * ".." is handled by the kernel. We do not currently handle
1244 if (nlen == 0 || (nlen == 1 && ncp->nc_name[0] == '.')) {
1245 ip = hammer_get_inode(&trans, dip, dip->obj_id,
1246 asof, dip->obj_localization,
1249 error = hammer_get_vnode(ip, &vp);
1250 hammer_rel_inode(ip, 0);
1256 cache_setvp(ap->a_nch, vp);
1263 * Calculate the namekey and setup the key range for the scan. This
1264 * works kinda like a chained hash table where the lower 32 bits
1265 * of the namekey synthesize the chain.
1267 * The key range is inclusive of both key_beg and key_end.
1269 namekey = hammer_directory_namekey(dip, ncp->nc_name, nlen,
1272 error = hammer_init_cursor(&trans, &cursor, &dip->cache[1], dip);
1273 cursor.key_beg.localization = dip->obj_localization +
1274 hammer_dir_localization(dip);
1275 cursor.key_beg.obj_id = dip->obj_id;
1276 cursor.key_beg.key = namekey;
1277 cursor.key_beg.create_tid = 0;
1278 cursor.key_beg.delete_tid = 0;
1279 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1280 cursor.key_beg.obj_type = 0;
1282 cursor.key_end = cursor.key_beg;
1283 cursor.key_end.key += max_iterations;
1285 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
1288 * Scan all matching records (the chain), locate the one matching
1289 * the requested path component.
1291 * The hammer_ip_*() functions merge in-memory records with on-disk
1292 * records for the purposes of the search.
1295 localization = HAMMER_DEF_LOCALIZATION;
1298 error = hammer_ip_first(&cursor);
1299 while (error == 0) {
1300 error = hammer_ip_resolve_data(&cursor);
1303 if (nlen == cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF &&
1304 bcmp(ncp->nc_name, cursor.data->entry.name, nlen) == 0) {
1305 obj_id = cursor.data->entry.obj_id;
1306 localization = cursor.data->entry.localization;
1309 error = hammer_ip_next(&cursor);
1312 hammer_done_cursor(&cursor);
1315 * Lookup the obj_id. This should always succeed. If it does not
1316 * the filesystem may be damaged and we return a dummy inode.
1319 ip = hammer_get_inode(&trans, dip, obj_id,
1322 if (error == ENOENT) {
1323 kprintf("HAMMER: WARNING: Missing "
1324 "inode for dirent \"%s\"\n"
1325 "\tobj_id = %016llx, asof=%016llx, lo=%08x\n",
1327 (long long)obj_id, (long long)asof,
1330 ip = hammer_get_dummy_inode(&trans, dip, obj_id,
1335 error = hammer_get_vnode(ip, &vp);
1336 hammer_rel_inode(ip, 0);
1342 cache_setvp(ap->a_nch, vp);
1345 } else if (error == ENOENT) {
1346 cache_setvp(ap->a_nch, NULL);
1349 hammer_done_transaction(&trans);
1350 lwkt_reltoken(&hmp->fs_token);
1355 * hammer_vop_nlookupdotdot { dvp, vpp, cred }
1357 * Locate the parent directory of a directory vnode.
1359 * dvp is referenced but not locked. *vpp must be returned referenced and
1360 * locked. A parent_obj_id of 0 does not necessarily indicate that we are
1361 * at the root, instead it could indicate that the directory we were in was
1364 * NOTE: as-of sequences are not linked into the directory structure. If
1365 * we are at the root with a different asof then the mount point, reload
1366 * the same directory with the mount point's asof. I'm not sure what this
1367 * will do to NFS. We encode ASOF stamps in NFS file handles so it might not
1368 * get confused, but it hasn't been tested.
1372 hammer_vop_nlookupdotdot(struct vop_nlookupdotdot_args *ap)
1374 struct hammer_transaction trans;
1375 struct hammer_inode *dip;
1376 struct hammer_inode *ip;
1378 int64_t parent_obj_id;
1379 u_int32_t parent_obj_localization;
1383 dip = VTOI(ap->a_dvp);
1384 asof = dip->obj_asof;
1388 * Whos are parent? This could be the root of a pseudo-filesystem
1389 * whos parent is in another localization domain.
1391 lwkt_gettoken(&hmp->fs_token);
1392 parent_obj_id = dip->ino_data.parent_obj_id;
1393 if (dip->obj_id == HAMMER_OBJID_ROOT)
1394 parent_obj_localization = dip->ino_data.ext.obj.parent_obj_localization;
1396 parent_obj_localization = dip->obj_localization;
1398 if (parent_obj_id == 0) {
1399 if (dip->obj_id == HAMMER_OBJID_ROOT &&
1400 asof != hmp->asof) {
1401 parent_obj_id = dip->obj_id;
1403 *ap->a_fakename = kmalloc(19, M_TEMP, M_WAITOK);
1404 ksnprintf(*ap->a_fakename, 19, "0x%016llx",
1405 (long long)dip->obj_asof);
1408 lwkt_reltoken(&hmp->fs_token);
1413 hammer_simple_transaction(&trans, hmp);
1414 ++hammer_stats_file_iopsr;
1416 ip = hammer_get_inode(&trans, dip, parent_obj_id,
1417 asof, parent_obj_localization,
1418 dip->flags, &error);
1420 error = hammer_get_vnode(ip, ap->a_vpp);
1421 hammer_rel_inode(ip, 0);
1425 hammer_done_transaction(&trans);
1426 lwkt_reltoken(&hmp->fs_token);
1431 * hammer_vop_nlink { nch, dvp, vp, cred }
1435 hammer_vop_nlink(struct vop_nlink_args *ap)
1437 struct hammer_transaction trans;
1438 struct hammer_inode *dip;
1439 struct hammer_inode *ip;
1440 struct nchandle *nch;
1444 if (ap->a_dvp->v_mount != ap->a_vp->v_mount)
1448 dip = VTOI(ap->a_dvp);
1449 ip = VTOI(ap->a_vp);
1452 if (dip->obj_localization != ip->obj_localization)
1455 if (dip->flags & HAMMER_INODE_RO)
1457 if (ip->flags & HAMMER_INODE_RO)
1459 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0)
1463 * Create a transaction to cover the operations we perform.
1465 lwkt_gettoken(&hmp->fs_token);
1466 hammer_start_transaction(&trans, hmp);
1467 ++hammer_stats_file_iopsw;
1470 * Add the filesystem object to the directory. Note that neither
1471 * dip nor ip are referenced or locked, but their vnodes are
1472 * referenced. This function will bump the inode's link count.
1474 error = hammer_ip_add_directory(&trans, dip,
1475 nch->ncp->nc_name, nch->ncp->nc_nlen,
1482 cache_setunresolved(nch);
1483 cache_setvp(nch, ap->a_vp);
1485 hammer_done_transaction(&trans);
1486 hammer_knote(ap->a_vp, NOTE_LINK);
1487 hammer_knote(ap->a_dvp, NOTE_WRITE);
1488 lwkt_reltoken(&hmp->fs_token);
1493 * hammer_vop_nmkdir { nch, dvp, vpp, cred, vap }
1495 * The operating system has already ensured that the directory entry
1496 * does not exist and done all appropriate namespace locking.
1500 hammer_vop_nmkdir(struct vop_nmkdir_args *ap)
1502 struct hammer_transaction trans;
1503 struct hammer_inode *dip;
1504 struct hammer_inode *nip;
1505 struct nchandle *nch;
1510 dip = VTOI(ap->a_dvp);
1513 if (dip->flags & HAMMER_INODE_RO)
1515 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0)
1519 * Create a transaction to cover the operations we perform.
1521 lwkt_gettoken(&hmp->fs_token);
1522 hammer_start_transaction(&trans, hmp);
1523 ++hammer_stats_file_iopsw;
1526 * Create a new filesystem object of the requested type. The
1527 * returned inode will be referenced but not locked.
1529 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
1530 dip, nch->ncp->nc_name, nch->ncp->nc_nlen,
1533 hkprintf("hammer_mkdir error %d\n", error);
1534 hammer_done_transaction(&trans);
1536 lwkt_reltoken(&hmp->fs_token);
1540 * Add the new filesystem object to the directory. This will also
1541 * bump the inode's link count.
1543 error = hammer_ip_add_directory(&trans, dip,
1544 nch->ncp->nc_name, nch->ncp->nc_nlen,
1547 hkprintf("hammer_mkdir (add) error %d\n", error);
1553 hammer_rel_inode(nip, 0);
1556 error = hammer_get_vnode(nip, ap->a_vpp);
1557 hammer_rel_inode(nip, 0);
1559 cache_setunresolved(ap->a_nch);
1560 cache_setvp(ap->a_nch, *ap->a_vpp);
1563 hammer_done_transaction(&trans);
1565 hammer_knote(ap->a_dvp, NOTE_WRITE | NOTE_LINK);
1566 lwkt_reltoken(&hmp->fs_token);
1571 * hammer_vop_nmknod { nch, dvp, vpp, cred, vap }
1573 * The operating system has already ensured that the directory entry
1574 * does not exist and done all appropriate namespace locking.
1578 hammer_vop_nmknod(struct vop_nmknod_args *ap)
1580 struct hammer_transaction trans;
1581 struct hammer_inode *dip;
1582 struct hammer_inode *nip;
1583 struct nchandle *nch;
1588 dip = VTOI(ap->a_dvp);
1591 if (dip->flags & HAMMER_INODE_RO)
1593 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0)
1597 * Create a transaction to cover the operations we perform.
1599 lwkt_gettoken(&hmp->fs_token);
1600 hammer_start_transaction(&trans, hmp);
1601 ++hammer_stats_file_iopsw;
1604 * Create a new filesystem object of the requested type. The
1605 * returned inode will be referenced but not locked.
1607 * If mknod specifies a directory a pseudo-fs is created.
1609 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
1610 dip, nch->ncp->nc_name, nch->ncp->nc_nlen,
1613 hammer_done_transaction(&trans);
1615 lwkt_reltoken(&hmp->fs_token);
1620 * Add the new filesystem object to the directory. This will also
1621 * bump the inode's link count.
1623 error = hammer_ip_add_directory(&trans, dip,
1624 nch->ncp->nc_name, nch->ncp->nc_nlen,
1631 hammer_rel_inode(nip, 0);
1634 error = hammer_get_vnode(nip, ap->a_vpp);
1635 hammer_rel_inode(nip, 0);
1637 cache_setunresolved(ap->a_nch);
1638 cache_setvp(ap->a_nch, *ap->a_vpp);
1641 hammer_done_transaction(&trans);
1643 hammer_knote(ap->a_dvp, NOTE_WRITE);
1644 lwkt_reltoken(&hmp->fs_token);
1649 * hammer_vop_open { vp, mode, cred, fp }
1651 * MPSAFE (does not require fs_token)
1655 hammer_vop_open(struct vop_open_args *ap)
1659 ++hammer_stats_file_iopsr;
1660 ip = VTOI(ap->a_vp);
1662 if ((ap->a_mode & FWRITE) && (ip->flags & HAMMER_INODE_RO))
1664 return(vop_stdopen(ap));
1668 * hammer_vop_print { vp }
1672 hammer_vop_print(struct vop_print_args *ap)
1678 * hammer_vop_readdir { vp, uio, cred, *eofflag, *ncookies, off_t **cookies }
1682 hammer_vop_readdir(struct vop_readdir_args *ap)
1684 struct hammer_transaction trans;
1685 struct hammer_cursor cursor;
1686 struct hammer_inode *ip;
1689 hammer_base_elm_t base;
1698 ++hammer_stats_file_iopsr;
1699 ip = VTOI(ap->a_vp);
1701 saveoff = uio->uio_offset;
1704 if (ap->a_ncookies) {
1705 ncookies = uio->uio_resid / 16 + 1;
1706 if (ncookies > 1024)
1708 cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
1716 lwkt_gettoken(&hmp->fs_token);
1717 hammer_simple_transaction(&trans, hmp);
1720 * Handle artificial entries
1722 * It should be noted that the minimum value for a directory
1723 * hash key on-media is 0x0000000100000000, so we can use anything
1724 * less then that to represent our 'special' key space.
1728 r = vop_write_dirent(&error, uio, ip->obj_id, DT_DIR, 1, ".");
1732 cookies[cookie_index] = saveoff;
1735 if (cookie_index == ncookies)
1739 if (ip->ino_data.parent_obj_id) {
1740 r = vop_write_dirent(&error, uio,
1741 ip->ino_data.parent_obj_id,
1744 r = vop_write_dirent(&error, uio,
1745 ip->obj_id, DT_DIR, 2, "..");
1750 cookies[cookie_index] = saveoff;
1753 if (cookie_index == ncookies)
1758 * Key range (begin and end inclusive) to scan. Directory keys
1759 * directly translate to a 64 bit 'seek' position.
1761 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
1762 cursor.key_beg.localization = ip->obj_localization +
1763 hammer_dir_localization(ip);
1764 cursor.key_beg.obj_id = ip->obj_id;
1765 cursor.key_beg.create_tid = 0;
1766 cursor.key_beg.delete_tid = 0;
1767 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1768 cursor.key_beg.obj_type = 0;
1769 cursor.key_beg.key = saveoff;
1771 cursor.key_end = cursor.key_beg;
1772 cursor.key_end.key = HAMMER_MAX_KEY;
1773 cursor.asof = ip->obj_asof;
1774 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
1776 error = hammer_ip_first(&cursor);
1778 while (error == 0) {
1779 error = hammer_ip_resolve_data(&cursor);
1782 base = &cursor.leaf->base;
1783 saveoff = base->key;
1784 KKASSERT(cursor.leaf->data_len > HAMMER_ENTRY_NAME_OFF);
1786 if (base->obj_id != ip->obj_id)
1787 panic("readdir: bad record at %p", cursor.node);
1790 * Convert pseudo-filesystems into softlinks
1792 dtype = hammer_get_dtype(cursor.leaf->base.obj_type);
1793 r = vop_write_dirent(
1794 &error, uio, cursor.data->entry.obj_id,
1796 cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF ,
1797 (void *)cursor.data->entry.name);
1802 cookies[cookie_index] = base->key;
1804 if (cookie_index == ncookies)
1806 error = hammer_ip_next(&cursor);
1808 hammer_done_cursor(&cursor);
1811 hammer_done_transaction(&trans);
1814 *ap->a_eofflag = (error == ENOENT);
1815 uio->uio_offset = saveoff;
1816 if (error && cookie_index == 0) {
1817 if (error == ENOENT)
1820 kfree(cookies, M_TEMP);
1821 *ap->a_ncookies = 0;
1822 *ap->a_cookies = NULL;
1825 if (error == ENOENT)
1828 *ap->a_ncookies = cookie_index;
1829 *ap->a_cookies = cookies;
1832 lwkt_reltoken(&hmp->fs_token);
1837 * hammer_vop_readlink { vp, uio, cred }
1841 hammer_vop_readlink(struct vop_readlink_args *ap)
1843 struct hammer_transaction trans;
1844 struct hammer_cursor cursor;
1845 struct hammer_inode *ip;
1848 u_int32_t localization;
1849 hammer_pseudofs_inmem_t pfsm;
1852 ip = VTOI(ap->a_vp);
1855 lwkt_gettoken(&hmp->fs_token);
1858 * Shortcut if the symlink data was stuffed into ino_data.
1860 * Also expand special "@@PFS%05d" softlinks (expansion only
1861 * occurs for non-historical (current) accesses made from the
1862 * primary filesystem).
1864 if (ip->ino_data.size <= HAMMER_INODE_BASESYMLEN) {
1868 ptr = ip->ino_data.ext.symlink;
1869 bytes = (int)ip->ino_data.size;
1871 ip->obj_asof == HAMMER_MAX_TID &&
1872 ip->obj_localization == 0 &&
1873 strncmp(ptr, "@@PFS", 5) == 0) {
1874 hammer_simple_transaction(&trans, hmp);
1875 bcopy(ptr + 5, buf, 5);
1877 localization = strtoul(buf, NULL, 10) << 16;
1878 pfsm = hammer_load_pseudofs(&trans, localization,
1881 if (pfsm->pfsd.mirror_flags &
1882 HAMMER_PFSD_SLAVE) {
1883 /* vap->va_size == 26 */
1884 ksnprintf(buf, sizeof(buf),
1886 (long long)pfsm->pfsd.sync_end_tid,
1887 localization >> 16);
1889 /* vap->va_size == 10 */
1890 ksnprintf(buf, sizeof(buf),
1892 localization >> 16);
1894 ksnprintf(buf, sizeof(buf),
1896 (long long)HAMMER_MAX_TID,
1897 localization >> 16);
1901 bytes = strlen(buf);
1904 hammer_rel_pseudofs(hmp, pfsm);
1905 hammer_done_transaction(&trans);
1907 error = uiomove(ptr, bytes, ap->a_uio);
1908 lwkt_reltoken(&hmp->fs_token);
1915 hammer_simple_transaction(&trans, hmp);
1916 ++hammer_stats_file_iopsr;
1917 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
1920 * Key range (begin and end inclusive) to scan. Directory keys
1921 * directly translate to a 64 bit 'seek' position.
1923 cursor.key_beg.localization = ip->obj_localization +
1924 HAMMER_LOCALIZE_MISC;
1925 cursor.key_beg.obj_id = ip->obj_id;
1926 cursor.key_beg.create_tid = 0;
1927 cursor.key_beg.delete_tid = 0;
1928 cursor.key_beg.rec_type = HAMMER_RECTYPE_FIX;
1929 cursor.key_beg.obj_type = 0;
1930 cursor.key_beg.key = HAMMER_FIXKEY_SYMLINK;
1931 cursor.asof = ip->obj_asof;
1932 cursor.flags |= HAMMER_CURSOR_ASOF;
1934 error = hammer_ip_lookup(&cursor);
1936 error = hammer_ip_resolve_data(&cursor);
1938 KKASSERT(cursor.leaf->data_len >=
1939 HAMMER_SYMLINK_NAME_OFF);
1940 error = uiomove(cursor.data->symlink.name,
1941 cursor.leaf->data_len -
1942 HAMMER_SYMLINK_NAME_OFF,
1946 hammer_done_cursor(&cursor);
1947 hammer_done_transaction(&trans);
1948 lwkt_reltoken(&hmp->fs_token);
1953 * hammer_vop_nremove { nch, dvp, cred }
1957 hammer_vop_nremove(struct vop_nremove_args *ap)
1959 struct hammer_transaction trans;
1960 struct hammer_inode *dip;
1964 dip = VTOI(ap->a_dvp);
1967 if (hammer_nohistory(dip) == 0 &&
1968 (error = hammer_checkspace(hmp, HAMMER_CHKSPC_REMOVE)) != 0) {
1972 lwkt_gettoken(&hmp->fs_token);
1973 hammer_start_transaction(&trans, hmp);
1974 ++hammer_stats_file_iopsw;
1975 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp, ap->a_cred, 0, 0);
1976 hammer_done_transaction(&trans);
1978 hammer_knote(ap->a_dvp, NOTE_WRITE);
1979 lwkt_reltoken(&hmp->fs_token);
1984 * hammer_vop_nrename { fnch, tnch, fdvp, tdvp, cred }
1988 hammer_vop_nrename(struct vop_nrename_args *ap)
1990 struct hammer_transaction trans;
1991 struct namecache *fncp;
1992 struct namecache *tncp;
1993 struct hammer_inode *fdip;
1994 struct hammer_inode *tdip;
1995 struct hammer_inode *ip;
1997 struct hammer_cursor cursor;
1999 u_int32_t max_iterations;
2002 if (ap->a_fdvp->v_mount != ap->a_tdvp->v_mount)
2004 if (ap->a_fdvp->v_mount != ap->a_fnch->ncp->nc_vp->v_mount)
2007 fdip = VTOI(ap->a_fdvp);
2008 tdip = VTOI(ap->a_tdvp);
2009 fncp = ap->a_fnch->ncp;
2010 tncp = ap->a_tnch->ncp;
2011 ip = VTOI(fncp->nc_vp);
2012 KKASSERT(ip != NULL);
2016 if (fdip->obj_localization != tdip->obj_localization)
2018 if (fdip->obj_localization != ip->obj_localization)
2021 if (fdip->flags & HAMMER_INODE_RO)
2023 if (tdip->flags & HAMMER_INODE_RO)
2025 if (ip->flags & HAMMER_INODE_RO)
2027 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0)
2030 lwkt_gettoken(&hmp->fs_token);
2031 hammer_start_transaction(&trans, hmp);
2032 ++hammer_stats_file_iopsw;
2035 * Remove tncp from the target directory and then link ip as
2036 * tncp. XXX pass trans to dounlink
2038 * Force the inode sync-time to match the transaction so it is
2039 * in-sync with the creation of the target directory entry.
2041 error = hammer_dounlink(&trans, ap->a_tnch, ap->a_tdvp,
2043 if (error == 0 || error == ENOENT) {
2044 error = hammer_ip_add_directory(&trans, tdip,
2045 tncp->nc_name, tncp->nc_nlen,
2048 ip->ino_data.parent_obj_id = tdip->obj_id;
2049 ip->ino_data.ctime = trans.time;
2050 hammer_modify_inode(&trans, ip, HAMMER_INODE_DDIRTY);
2054 goto failed; /* XXX */
2057 * Locate the record in the originating directory and remove it.
2059 * Calculate the namekey and setup the key range for the scan. This
2060 * works kinda like a chained hash table where the lower 32 bits
2061 * of the namekey synthesize the chain.
2063 * The key range is inclusive of both key_beg and key_end.
2065 namekey = hammer_directory_namekey(fdip, fncp->nc_name, fncp->nc_nlen,
2068 hammer_init_cursor(&trans, &cursor, &fdip->cache[1], fdip);
2069 cursor.key_beg.localization = fdip->obj_localization +
2070 hammer_dir_localization(fdip);
2071 cursor.key_beg.obj_id = fdip->obj_id;
2072 cursor.key_beg.key = namekey;
2073 cursor.key_beg.create_tid = 0;
2074 cursor.key_beg.delete_tid = 0;
2075 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
2076 cursor.key_beg.obj_type = 0;
2078 cursor.key_end = cursor.key_beg;
2079 cursor.key_end.key += max_iterations;
2080 cursor.asof = fdip->obj_asof;
2081 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
2084 * Scan all matching records (the chain), locate the one matching
2085 * the requested path component.
2087 * The hammer_ip_*() functions merge in-memory records with on-disk
2088 * records for the purposes of the search.
2090 error = hammer_ip_first(&cursor);
2091 while (error == 0) {
2092 if (hammer_ip_resolve_data(&cursor) != 0)
2094 nlen = cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF;
2096 if (fncp->nc_nlen == nlen &&
2097 bcmp(fncp->nc_name, cursor.data->entry.name, nlen) == 0) {
2100 error = hammer_ip_next(&cursor);
2104 * If all is ok we have to get the inode so we can adjust nlinks.
2106 * WARNING: hammer_ip_del_directory() may have to terminate the
2107 * cursor to avoid a recursion. It's ok to call hammer_done_cursor()
2111 error = hammer_ip_del_directory(&trans, &cursor, fdip, ip);
2114 * XXX A deadlock here will break rename's atomicy for the purposes
2115 * of crash recovery.
2117 if (error == EDEADLK) {
2118 hammer_done_cursor(&cursor);
2123 * Cleanup and tell the kernel that the rename succeeded.
2125 * NOTE: ip->vp, if non-NULL, cannot be directly referenced
2126 * without formally acquiring the vp since the vp might
2127 * have zero refs on it, or in the middle of a reclaim,
2130 hammer_done_cursor(&cursor);
2132 cache_rename(ap->a_fnch, ap->a_tnch);
2133 hammer_knote(ap->a_fdvp, NOTE_WRITE);
2134 hammer_knote(ap->a_tdvp, NOTE_WRITE);
2138 error = hammer_get_vnode(ip, &vp);
2139 if (error == 0 && vp) {
2141 hammer_knote(ip->vp, NOTE_RENAME);
2145 kprintf("Debug: HAMMER ip/vp race2 avoided\n");
2150 hammer_done_transaction(&trans);
2151 lwkt_reltoken(&hmp->fs_token);
2156 * hammer_vop_nrmdir { nch, dvp, cred }
2160 hammer_vop_nrmdir(struct vop_nrmdir_args *ap)
2162 struct hammer_transaction trans;
2163 struct hammer_inode *dip;
2167 dip = VTOI(ap->a_dvp);
2170 if (hammer_nohistory(dip) == 0 &&
2171 (error = hammer_checkspace(hmp, HAMMER_CHKSPC_REMOVE)) != 0) {
2175 lwkt_gettoken(&hmp->fs_token);
2176 hammer_start_transaction(&trans, hmp);
2177 ++hammer_stats_file_iopsw;
2178 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp, ap->a_cred, 0, 1);
2179 hammer_done_transaction(&trans);
2181 hammer_knote(ap->a_dvp, NOTE_WRITE | NOTE_LINK);
2182 lwkt_reltoken(&hmp->fs_token);
2187 * hammer_vop_markatime { vp, cred }
2191 hammer_vop_markatime(struct vop_markatime_args *ap)
2193 struct hammer_transaction trans;
2194 struct hammer_inode *ip;
2197 ip = VTOI(ap->a_vp);
2198 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
2200 if (ip->flags & HAMMER_INODE_RO)
2203 if (hmp->mp->mnt_flag & MNT_NOATIME)
2205 lwkt_gettoken(&hmp->fs_token);
2206 hammer_start_transaction(&trans, hmp);
2207 ++hammer_stats_file_iopsw;
2209 ip->ino_data.atime = trans.time;
2210 hammer_modify_inode(&trans, ip, HAMMER_INODE_ATIME);
2211 hammer_done_transaction(&trans);
2212 hammer_knote(ap->a_vp, NOTE_ATTRIB);
2213 lwkt_reltoken(&hmp->fs_token);
2218 * hammer_vop_setattr { vp, vap, cred }
2222 hammer_vop_setattr(struct vop_setattr_args *ap)
2224 struct hammer_transaction trans;
2225 struct hammer_inode *ip;
2234 int64_t aligned_size;
2239 ip = ap->a_vp->v_data;
2244 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
2246 if (ip->flags & HAMMER_INODE_RO)
2248 if (hammer_nohistory(ip) == 0 &&
2249 (error = hammer_checkspace(hmp, HAMMER_CHKSPC_REMOVE)) != 0) {
2253 lwkt_gettoken(&hmp->fs_token);
2254 hammer_start_transaction(&trans, hmp);
2255 ++hammer_stats_file_iopsw;
2258 if (vap->va_flags != VNOVAL) {
2259 flags = ip->ino_data.uflags;
2260 error = vop_helper_setattr_flags(&flags, vap->va_flags,
2261 hammer_to_unix_xid(&ip->ino_data.uid),
2264 if (ip->ino_data.uflags != flags) {
2265 ip->ino_data.uflags = flags;
2266 ip->ino_data.ctime = trans.time;
2267 modflags |= HAMMER_INODE_DDIRTY;
2268 kflags |= NOTE_ATTRIB;
2270 if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) {
2277 if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) {
2281 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
2282 mode_t cur_mode = ip->ino_data.mode;
2283 uid_t cur_uid = hammer_to_unix_xid(&ip->ino_data.uid);
2284 gid_t cur_gid = hammer_to_unix_xid(&ip->ino_data.gid);
2288 error = vop_helper_chown(ap->a_vp, vap->va_uid, vap->va_gid,
2290 &cur_uid, &cur_gid, &cur_mode);
2292 hammer_guid_to_uuid(&uuid_uid, cur_uid);
2293 hammer_guid_to_uuid(&uuid_gid, cur_gid);
2294 if (bcmp(&uuid_uid, &ip->ino_data.uid,
2295 sizeof(uuid_uid)) ||
2296 bcmp(&uuid_gid, &ip->ino_data.gid,
2297 sizeof(uuid_gid)) ||
2298 ip->ino_data.mode != cur_mode
2300 ip->ino_data.uid = uuid_uid;
2301 ip->ino_data.gid = uuid_gid;
2302 ip->ino_data.mode = cur_mode;
2303 ip->ino_data.ctime = trans.time;
2304 modflags |= HAMMER_INODE_DDIRTY;
2306 kflags |= NOTE_ATTRIB;
2309 while (vap->va_size != VNOVAL && ip->ino_data.size != vap->va_size) {
2310 switch(ap->a_vp->v_type) {
2312 if (vap->va_size == ip->ino_data.size)
2316 * Log the operation if in fast-fsync mode or if
2317 * there are unterminated redo write records present.
2319 * The second check is needed so the recovery code
2320 * properly truncates write redos even if nominal
2321 * REDO operations is turned off due to excessive
2322 * writes, because the related records might be
2323 * destroyed and never lay down a TERM_WRITE.
2325 if ((ip->flags & HAMMER_INODE_REDO) ||
2326 (ip->flags & HAMMER_INODE_RDIRTY)) {
2327 error = hammer_generate_redo(&trans, ip,
2332 blksize = hammer_blocksize(vap->va_size);
2335 * XXX break atomicy, we can deadlock the backend
2336 * if we do not release the lock. Probably not a
2339 if (vap->va_size < ip->ino_data.size) {
2340 nvtruncbuf(ap->a_vp, vap->va_size,
2342 hammer_blockoff(vap->va_size),
2345 kflags |= NOTE_WRITE;
2347 nvextendbuf(ap->a_vp,
2350 hammer_blocksize(ip->ino_data.size),
2351 hammer_blocksize(vap->va_size),
2352 hammer_blockoff(ip->ino_data.size),
2353 hammer_blockoff(vap->va_size),
2356 kflags |= NOTE_WRITE | NOTE_EXTEND;
2358 ip->ino_data.size = vap->va_size;
2359 ip->ino_data.mtime = trans.time;
2360 /* XXX safe to use SDIRTY instead of DDIRTY here? */
2361 modflags |= HAMMER_INODE_MTIME | HAMMER_INODE_DDIRTY;
2364 * On-media truncation is cached in the inode until
2365 * the inode is synchronized. We must immediately
2366 * handle any frontend records.
2369 hammer_ip_frontend_trunc(ip, vap->va_size);
2370 #ifdef DEBUG_TRUNCATE
2371 if (HammerTruncIp == NULL)
2374 if ((ip->flags & HAMMER_INODE_TRUNCATED) == 0) {
2375 ip->flags |= HAMMER_INODE_TRUNCATED;
2376 ip->trunc_off = vap->va_size;
2377 hammer_inode_dirty(ip);
2378 #ifdef DEBUG_TRUNCATE
2379 if (ip == HammerTruncIp)
2380 kprintf("truncate1 %016llx\n",
2381 (long long)ip->trunc_off);
2383 } else if (ip->trunc_off > vap->va_size) {
2384 ip->trunc_off = vap->va_size;
2385 #ifdef DEBUG_TRUNCATE
2386 if (ip == HammerTruncIp)
2387 kprintf("truncate2 %016llx\n",
2388 (long long)ip->trunc_off);
2391 #ifdef DEBUG_TRUNCATE
2392 if (ip == HammerTruncIp)
2393 kprintf("truncate3 %016llx (ignored)\n",
2394 (long long)vap->va_size);
2401 * When truncating, nvtruncbuf() may have cleaned out
2402 * a portion of the last block on-disk in the buffer
2403 * cache. We must clean out any frontend records
2404 * for blocks beyond the new last block.
2406 aligned_size = (vap->va_size + (blksize - 1)) &
2407 ~(int64_t)(blksize - 1);
2408 if (truncating && vap->va_size < aligned_size) {
2409 aligned_size -= blksize;
2410 hammer_ip_frontend_trunc(ip, aligned_size);
2415 if ((ip->flags & HAMMER_INODE_TRUNCATED) == 0) {
2416 ip->flags |= HAMMER_INODE_TRUNCATED;
2417 ip->trunc_off = vap->va_size;
2418 hammer_inode_dirty(ip);
2419 } else if (ip->trunc_off > vap->va_size) {
2420 ip->trunc_off = vap->va_size;
2422 hammer_ip_frontend_trunc(ip, vap->va_size);
2423 ip->ino_data.size = vap->va_size;
2424 ip->ino_data.mtime = trans.time;
2425 modflags |= HAMMER_INODE_MTIME | HAMMER_INODE_DDIRTY;
2426 kflags |= NOTE_ATTRIB;
2434 if (vap->va_atime.tv_sec != VNOVAL) {
2435 ip->ino_data.atime = hammer_timespec_to_time(&vap->va_atime);
2436 modflags |= HAMMER_INODE_ATIME;
2437 kflags |= NOTE_ATTRIB;
2439 if (vap->va_mtime.tv_sec != VNOVAL) {
2440 ip->ino_data.mtime = hammer_timespec_to_time(&vap->va_mtime);
2441 modflags |= HAMMER_INODE_MTIME;
2442 kflags |= NOTE_ATTRIB;
2444 if (vap->va_mode != (mode_t)VNOVAL) {
2445 mode_t cur_mode = ip->ino_data.mode;
2446 uid_t cur_uid = hammer_to_unix_xid(&ip->ino_data.uid);
2447 gid_t cur_gid = hammer_to_unix_xid(&ip->ino_data.gid);
2449 error = vop_helper_chmod(ap->a_vp, vap->va_mode, ap->a_cred,
2450 cur_uid, cur_gid, &cur_mode);
2451 if (error == 0 && ip->ino_data.mode != cur_mode) {
2452 ip->ino_data.mode = cur_mode;
2453 ip->ino_data.ctime = trans.time;
2454 modflags |= HAMMER_INODE_DDIRTY;
2455 kflags |= NOTE_ATTRIB;
2460 hammer_modify_inode(&trans, ip, modflags);
2461 hammer_done_transaction(&trans);
2462 hammer_knote(ap->a_vp, kflags);
2463 lwkt_reltoken(&hmp->fs_token);
2468 * hammer_vop_nsymlink { nch, dvp, vpp, cred, vap, target }
2472 hammer_vop_nsymlink(struct vop_nsymlink_args *ap)
2474 struct hammer_transaction trans;
2475 struct hammer_inode *dip;
2476 struct hammer_inode *nip;
2477 hammer_record_t record;
2478 struct nchandle *nch;
2483 ap->a_vap->va_type = VLNK;
2486 dip = VTOI(ap->a_dvp);
2489 if (dip->flags & HAMMER_INODE_RO)
2491 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0)
2495 * Create a transaction to cover the operations we perform.
2497 lwkt_gettoken(&hmp->fs_token);
2498 hammer_start_transaction(&trans, hmp);
2499 ++hammer_stats_file_iopsw;
2502 * Create a new filesystem object of the requested type. The
2503 * returned inode will be referenced but not locked.
2506 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
2507 dip, nch->ncp->nc_name, nch->ncp->nc_nlen,
2510 hammer_done_transaction(&trans);
2512 lwkt_reltoken(&hmp->fs_token);
2517 * Add a record representing the symlink. symlink stores the link
2518 * as pure data, not a string, and is no \0 terminated.
2521 bytes = strlen(ap->a_target);
2523 if (bytes <= HAMMER_INODE_BASESYMLEN) {
2524 bcopy(ap->a_target, nip->ino_data.ext.symlink, bytes);
2526 record = hammer_alloc_mem_record(nip, bytes);
2527 record->type = HAMMER_MEM_RECORD_GENERAL;
2529 record->leaf.base.localization = nip->obj_localization +
2530 HAMMER_LOCALIZE_MISC;
2531 record->leaf.base.key = HAMMER_FIXKEY_SYMLINK;
2532 record->leaf.base.rec_type = HAMMER_RECTYPE_FIX;
2533 record->leaf.data_len = bytes;
2534 KKASSERT(HAMMER_SYMLINK_NAME_OFF == 0);
2535 bcopy(ap->a_target, record->data->symlink.name, bytes);
2536 error = hammer_ip_add_record(&trans, record);
2540 * Set the file size to the length of the link.
2543 nip->ino_data.size = bytes;
2544 hammer_modify_inode(&trans, nip, HAMMER_INODE_DDIRTY);
2548 error = hammer_ip_add_directory(&trans, dip, nch->ncp->nc_name,
2549 nch->ncp->nc_nlen, nip);
2555 hammer_rel_inode(nip, 0);
2558 error = hammer_get_vnode(nip, ap->a_vpp);
2559 hammer_rel_inode(nip, 0);
2561 cache_setunresolved(ap->a_nch);
2562 cache_setvp(ap->a_nch, *ap->a_vpp);
2563 hammer_knote(ap->a_dvp, NOTE_WRITE);
2566 hammer_done_transaction(&trans);
2567 lwkt_reltoken(&hmp->fs_token);
2572 * hammer_vop_nwhiteout { nch, dvp, cred, flags }
2576 hammer_vop_nwhiteout(struct vop_nwhiteout_args *ap)
2578 struct hammer_transaction trans;
2579 struct hammer_inode *dip;
2583 dip = VTOI(ap->a_dvp);
2586 if (hammer_nohistory(dip) == 0 &&
2587 (error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0) {
2591 lwkt_gettoken(&hmp->fs_token);
2592 hammer_start_transaction(&trans, hmp);
2593 ++hammer_stats_file_iopsw;
2594 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp,
2595 ap->a_cred, ap->a_flags, -1);
2596 hammer_done_transaction(&trans);
2597 lwkt_reltoken(&hmp->fs_token);
2603 * hammer_vop_ioctl { vp, command, data, fflag, cred }
2607 hammer_vop_ioctl(struct vop_ioctl_args *ap)
2609 struct hammer_inode *ip = ap->a_vp->v_data;
2610 hammer_mount_t hmp = ip->hmp;
2613 ++hammer_stats_file_iopsr;
2614 lwkt_gettoken(&hmp->fs_token);
2615 error = hammer_ioctl(ip, ap->a_command, ap->a_data,
2616 ap->a_fflag, ap->a_cred);
2617 lwkt_reltoken(&hmp->fs_token);
2623 hammer_vop_mountctl(struct vop_mountctl_args *ap)
2625 static const struct mountctl_opt extraopt[] = {
2626 { HMNT_NOHISTORY, "nohistory" },
2627 { HMNT_MASTERID, "master" },
2631 struct hammer_mount *hmp;
2638 mp = ap->a_head.a_ops->head.vv_mount;
2639 KKASSERT(mp->mnt_data != NULL);
2640 hmp = (struct hammer_mount *)mp->mnt_data;
2642 lwkt_gettoken(&hmp->fs_token);
2645 case MOUNTCTL_SET_EXPORT:
2646 if (ap->a_ctllen != sizeof(struct export_args))
2649 error = hammer_vfs_export(mp, ap->a_op,
2650 (const struct export_args *)ap->a_ctl);
2652 case MOUNTCTL_MOUNTFLAGS:
2655 * Call standard mountctl VOP function
2656 * so we get user mount flags.
2658 error = vop_stdmountctl(ap);
2662 usedbytes = *ap->a_res;
2664 if (usedbytes > 0 && usedbytes < ap->a_buflen) {
2665 usedbytes += vfs_flagstostr(hmp->hflags, extraopt,
2667 ap->a_buflen - usedbytes,
2671 *ap->a_res += usedbytes;
2675 error = vop_stdmountctl(ap);
2678 lwkt_reltoken(&hmp->fs_token);
2683 * hammer_vop_strategy { vp, bio }
2685 * Strategy call, used for regular file read & write only. Note that the
2686 * bp may represent a cluster.
2688 * To simplify operation and allow better optimizations in the future,
2689 * this code does not make any assumptions with regards to buffer alignment
2694 hammer_vop_strategy(struct vop_strategy_args *ap)
2699 bp = ap->a_bio->bio_buf;
2703 error = hammer_vop_strategy_read(ap);
2706 error = hammer_vop_strategy_write(ap);
2709 bp->b_error = error = EINVAL;
2710 bp->b_flags |= B_ERROR;
2715 /* hammer_dump_dedup_cache(((hammer_inode_t)ap->a_vp->v_data)->hmp); */
2721 * Read from a regular file. Iterate the related records and fill in the
2722 * BIO/BUF. Gaps are zero-filled.
2724 * The support code in hammer_object.c should be used to deal with mixed
2725 * in-memory and on-disk records.
2727 * NOTE: Can be called from the cluster code with an oversized buf.
2733 hammer_vop_strategy_read(struct vop_strategy_args *ap)
2735 struct hammer_transaction trans;
2736 struct hammer_inode *ip;
2737 struct hammer_inode *dip;
2739 struct hammer_cursor cursor;
2740 hammer_base_elm_t base;
2741 hammer_off_t disk_offset;
2756 ip = ap->a_vp->v_data;
2760 * The zone-2 disk offset may have been set by the cluster code via
2761 * a BMAP operation, or else should be NOOFFSET.
2763 * Checking the high bits for a match against zone-2 should suffice.
2765 * In cases where a lot of data duplication is present it may be
2766 * more beneficial to drop through and doubule-buffer through the
2769 nbio = push_bio(bio);
2770 if ((nbio->bio_offset & HAMMER_OFF_ZONE_MASK) ==
2771 HAMMER_ZONE_LARGE_DATA) {
2772 if (hammer_double_buffer == 0) {
2773 lwkt_gettoken(&hmp->fs_token);
2774 error = hammer_io_direct_read(hmp, nbio, NULL);
2775 lwkt_reltoken(&hmp->fs_token);
2780 * Try to shortcut requests for double_buffer mode too.
2781 * Since this mode runs through the device buffer cache
2782 * only compatible buffer sizes (meaning those generated
2783 * by normal filesystem buffers) are legal.
2785 if (hammer_live_dedup == 0 && (bp->b_flags & B_PAGING) == 0) {
2786 error = hammer_io_indirect_read(hmp, nbio, NULL);
2792 * Well, that sucked. Do it the hard way. If all the stars are
2793 * aligned we may still be able to issue a direct-read.
2795 lwkt_gettoken(&hmp->fs_token);
2796 hammer_simple_transaction(&trans, hmp);
2797 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
2800 * Key range (begin and end inclusive) to scan. Note that the key's
2801 * stored in the actual records represent BASE+LEN, not BASE. The
2802 * first record containing bio_offset will have a key > bio_offset.
2804 cursor.key_beg.localization = ip->obj_localization +
2805 HAMMER_LOCALIZE_MISC;
2806 cursor.key_beg.obj_id = ip->obj_id;
2807 cursor.key_beg.create_tid = 0;
2808 cursor.key_beg.delete_tid = 0;
2809 cursor.key_beg.obj_type = 0;
2810 cursor.key_beg.key = bio->bio_offset + 1;
2811 cursor.asof = ip->obj_asof;
2812 cursor.flags |= HAMMER_CURSOR_ASOF;
2814 cursor.key_end = cursor.key_beg;
2815 KKASSERT(ip->ino_data.obj_type == HAMMER_OBJTYPE_REGFILE);
2817 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_DBFILE) {
2818 cursor.key_beg.rec_type = HAMMER_RECTYPE_DB;
2819 cursor.key_end.rec_type = HAMMER_RECTYPE_DB;
2820 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
2824 ran_end = bio->bio_offset + bp->b_bufsize;
2825 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
2826 cursor.key_end.rec_type = HAMMER_RECTYPE_DATA;
2827 tmp64 = ran_end + MAXPHYS + 1; /* work-around GCC-4 bug */
2828 if (tmp64 < ran_end)
2829 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
2831 cursor.key_end.key = ran_end + MAXPHYS + 1;
2833 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
2836 * Set NOSWAPCACHE for cursor data extraction if double buffering
2837 * is disabled or (if the file is not marked cacheable via chflags
2838 * and vm.swapcache_use_chflags is enabled).
2840 if (hammer_double_buffer == 0 ||
2841 ((ap->a_vp->v_flag & VSWAPCACHE) == 0 &&
2842 vm_swapcache_use_chflags)) {
2843 cursor.flags |= HAMMER_CURSOR_NOSWAPCACHE;
2846 error = hammer_ip_first(&cursor);
2849 while (error == 0) {
2851 * Get the base file offset of the record. The key for
2852 * data records is (base + bytes) rather then (base).
2854 base = &cursor.leaf->base;
2855 rec_offset = base->key - cursor.leaf->data_len;
2858 * Calculate the gap, if any, and zero-fill it.
2860 * n is the offset of the start of the record verses our
2861 * current seek offset in the bio.
2863 n = (int)(rec_offset - (bio->bio_offset + boff));
2865 if (n > bp->b_bufsize - boff)
2866 n = bp->b_bufsize - boff;
2867 bzero((char *)bp->b_data + boff, n);
2873 * Calculate the data offset in the record and the number
2874 * of bytes we can copy.
2876 * There are two degenerate cases. First, boff may already
2877 * be at bp->b_bufsize. Secondly, the data offset within
2878 * the record may exceed the record's size.
2882 n = cursor.leaf->data_len - roff;
2884 kprintf("strategy_read: bad n=%d roff=%d\n", n, roff);
2886 } else if (n > bp->b_bufsize - boff) {
2887 n = bp->b_bufsize - boff;
2891 * Deal with cached truncations. This cool bit of code
2892 * allows truncate()/ftruncate() to avoid having to sync
2895 * If the frontend is truncated then all backend records are
2896 * subject to the frontend's truncation.
2898 * If the backend is truncated then backend records on-disk
2899 * (but not in-memory) are subject to the backend's
2900 * truncation. In-memory records owned by the backend
2901 * represent data written after the truncation point on the
2902 * backend and must not be truncated.
2904 * Truncate operations deal with frontend buffer cache
2905 * buffers and frontend-owned in-memory records synchronously.
2907 if (ip->flags & HAMMER_INODE_TRUNCATED) {
2908 if (hammer_cursor_ondisk(&cursor)/* ||
2909 cursor.iprec->flush_state == HAMMER_FST_FLUSH*/) {
2910 if (ip->trunc_off <= rec_offset)
2912 else if (ip->trunc_off < rec_offset + n)
2913 n = (int)(ip->trunc_off - rec_offset);
2916 if (ip->sync_flags & HAMMER_INODE_TRUNCATED) {
2917 if (hammer_cursor_ondisk(&cursor)) {
2918 if (ip->sync_trunc_off <= rec_offset)
2920 else if (ip->sync_trunc_off < rec_offset + n)
2921 n = (int)(ip->sync_trunc_off - rec_offset);
2926 * Try to issue a direct read into our bio if possible,
2927 * otherwise resolve the element data into a hammer_buffer
2930 * The buffer on-disk should be zerod past any real
2931 * truncation point, but may not be for any synthesized
2932 * truncation point from above.
2934 * NOTE: disk_offset is only valid if the cursor data is
2937 disk_offset = cursor.leaf->data_offset + roff;
2938 isdedupable = (boff == 0 && n == bp->b_bufsize &&
2939 hammer_cursor_ondisk(&cursor) &&
2940 ((int)disk_offset & HAMMER_BUFMASK) == 0);
2942 if (isdedupable && hammer_double_buffer == 0) {
2946 KKASSERT((disk_offset & HAMMER_OFF_ZONE_MASK) ==
2947 HAMMER_ZONE_LARGE_DATA);
2948 nbio->bio_offset = disk_offset;
2949 error = hammer_io_direct_read(hmp, nbio, cursor.leaf);
2950 if (hammer_live_dedup && error == 0)
2951 hammer_dedup_cache_add(ip, cursor.leaf);
2953 } else if (isdedupable) {
2955 * Async I/O case for reading from backing store
2956 * and copying the data to the filesystem buffer.
2957 * live-dedup has to verify the data anyway if it
2958 * gets a hit later so we can just add the entry
2961 KKASSERT((disk_offset & HAMMER_OFF_ZONE_MASK) ==
2962 HAMMER_ZONE_LARGE_DATA);
2963 nbio->bio_offset = disk_offset;
2964 if (hammer_live_dedup)
2965 hammer_dedup_cache_add(ip, cursor.leaf);
2966 error = hammer_io_indirect_read(hmp, nbio, cursor.leaf);
2969 error = hammer_ip_resolve_data(&cursor);
2971 if (hammer_live_dedup && isdedupable)
2972 hammer_dedup_cache_add(ip, cursor.leaf);
2973 bcopy((char *)cursor.data + roff,
2974 (char *)bp->b_data + boff, n);
2981 * We have to be sure that the only elements added to the
2982 * dedup cache are those which are already on-media.
2984 if (hammer_live_dedup && hammer_cursor_ondisk(&cursor))
2985 hammer_dedup_cache_add(ip, cursor.leaf);
2988 * Iterate until we have filled the request.
2991 if (boff == bp->b_bufsize)
2993 error = hammer_ip_next(&cursor);
2997 * There may have been a gap after the last record
2999 if (error == ENOENT)
3001 if (error == 0 && boff != bp->b_bufsize) {
3002 KKASSERT(boff < bp->b_bufsize);
3003 bzero((char *)bp->b_data + boff, bp->b_bufsize - boff);
3004 /* boff = bp->b_bufsize; */
3008 * Disallow swapcache operation on the vnode buffer if double
3009 * buffering is enabled, the swapcache will get the data via
3010 * the block device buffer.
3012 if (hammer_double_buffer)
3013 bp->b_flags |= B_NOTMETA;
3019 bp->b_error = error;
3021 bp->b_flags |= B_ERROR;
3026 * Cache the b-tree node for the last data read in cache[1].
3028 * If we hit the file EOF then also cache the node in the
3029 * governing director's cache[3], it will be used to initialize
3030 * the inode's cache[1] for any inodes looked up via the directory.
3032 * This doesn't reduce disk accesses since the B-Tree chain is
3033 * likely cached, but it does reduce cpu overhead when looking
3034 * up file offsets for cpdup/tar/cpio style iterations.
3037 hammer_cache_node(&ip->cache[1], cursor.node);
3038 if (ran_end >= ip->ino_data.size) {
3039 dip = hammer_find_inode(&trans, ip->ino_data.parent_obj_id,
3040 ip->obj_asof, ip->obj_localization);
3042 hammer_cache_node(&dip->cache[3], cursor.node);
3043 hammer_rel_inode(dip, 0);
3046 hammer_done_cursor(&cursor);
3047 hammer_done_transaction(&trans);
3048 lwkt_reltoken(&hmp->fs_token);
3053 * BMAP operation - used to support cluster_read() only.
3055 * (struct vnode *vp, off_t loffset, off_t *doffsetp, int *runp, int *runb)
3057 * This routine may return EOPNOTSUPP if the opration is not supported for
3058 * the specified offset. The contents of the pointer arguments do not
3059 * need to be initialized in that case.
3061 * If a disk address is available and properly aligned return 0 with
3062 * *doffsetp set to the zone-2 address, and *runp / *runb set appropriately
3063 * to the run-length relative to that offset. Callers may assume that
3064 * *doffsetp is valid if 0 is returned, even if *runp is not sufficiently
3065 * large, so return EOPNOTSUPP if it is not sufficiently large.
3069 hammer_vop_bmap(struct vop_bmap_args *ap)
3071 struct hammer_transaction trans;
3072 struct hammer_inode *ip;
3074 struct hammer_cursor cursor;
3075 hammer_base_elm_t base;
3079 int64_t base_offset;
3080 int64_t base_disk_offset;
3081 int64_t last_offset;
3082 hammer_off_t last_disk_offset;
3083 hammer_off_t disk_offset;
3088 ++hammer_stats_file_iopsr;
3089 ip = ap->a_vp->v_data;
3093 * We can only BMAP regular files. We can't BMAP database files,
3096 if (ip->ino_data.obj_type != HAMMER_OBJTYPE_REGFILE)
3100 * bmap is typically called with runp/runb both NULL when used
3101 * for writing. We do not support BMAP for writing atm.
3103 if (ap->a_cmd != BUF_CMD_READ)
3107 * Scan the B-Tree to acquire blockmap addresses, then translate
3110 lwkt_gettoken(&hmp->fs_token);
3111 hammer_simple_transaction(&trans, hmp);
3113 kprintf("bmap_beg %016llx ip->cache %p\n",
3114 (long long)ap->a_loffset, ip->cache[1]);
3116 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
3119 * Key range (begin and end inclusive) to scan. Note that the key's
3120 * stored in the actual records represent BASE+LEN, not BASE. The
3121 * first record containing bio_offset will have a key > bio_offset.
3123 cursor.key_beg.localization = ip->obj_localization +
3124 HAMMER_LOCALIZE_MISC;
3125 cursor.key_beg.obj_id = ip->obj_id;
3126 cursor.key_beg.create_tid = 0;
3127 cursor.key_beg.delete_tid = 0;
3128 cursor.key_beg.obj_type = 0;
3130 cursor.key_beg.key = ap->a_loffset - MAXPHYS + 1;
3132 cursor.key_beg.key = ap->a_loffset + 1;
3133 if (cursor.key_beg.key < 0)
3134 cursor.key_beg.key = 0;
3135 cursor.asof = ip->obj_asof;
3136 cursor.flags |= HAMMER_CURSOR_ASOF;
3138 cursor.key_end = cursor.key_beg;
3139 KKASSERT(ip->ino_data.obj_type == HAMMER_OBJTYPE_REGFILE);
3141 ran_end = ap->a_loffset + MAXPHYS;
3142 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
3143 cursor.key_end.rec_type = HAMMER_RECTYPE_DATA;
3144 tmp64 = ran_end + MAXPHYS + 1; /* work-around GCC-4 bug */
3145 if (tmp64 < ran_end)
3146 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
3148 cursor.key_end.key = ran_end + MAXPHYS + 1;
3150 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
3152 error = hammer_ip_first(&cursor);
3153 base_offset = last_offset = 0;
3154 base_disk_offset = last_disk_offset = 0;
3156 while (error == 0) {
3158 * Get the base file offset of the record. The key for
3159 * data records is (base + bytes) rather then (base).
3161 * NOTE: rec_offset + rec_len may exceed the end-of-file.
3162 * The extra bytes should be zero on-disk and the BMAP op
3163 * should still be ok.
3165 base = &cursor.leaf->base;
3166 rec_offset = base->key - cursor.leaf->data_len;
3167 rec_len = cursor.leaf->data_len;
3170 * Incorporate any cached truncation.
3172 * NOTE: Modifications to rec_len based on synthesized
3173 * truncation points remove the guarantee that any extended
3174 * data on disk is zero (since the truncations may not have
3175 * taken place on-media yet).
3177 if (ip->flags & HAMMER_INODE_TRUNCATED) {
3178 if (hammer_cursor_ondisk(&cursor) ||
3179 cursor.iprec->flush_state == HAMMER_FST_FLUSH) {
3180 if (ip->trunc_off <= rec_offset)
3182 else if (ip->trunc_off < rec_offset + rec_len)
3183 rec_len = (int)(ip->trunc_off - rec_offset);
3186 if (ip->sync_flags & HAMMER_INODE_TRUNCATED) {
3187 if (hammer_cursor_ondisk(&cursor)) {
3188 if (ip->sync_trunc_off <= rec_offset)
3190 else if (ip->sync_trunc_off < rec_offset + rec_len)
3191 rec_len = (int)(ip->sync_trunc_off - rec_offset);
3196 * Accumulate information. If we have hit a discontiguous
3197 * block reset base_offset unless we are already beyond the
3198 * requested offset. If we are, that's it, we stop.
3202 if (hammer_cursor_ondisk(&cursor)) {
3203 disk_offset = cursor.leaf->data_offset;
3204 if (rec_offset != last_offset ||
3205 disk_offset != last_disk_offset) {
3206 if (rec_offset > ap->a_loffset)
3208 base_offset = rec_offset;
3209 base_disk_offset = disk_offset;
3211 last_offset = rec_offset + rec_len;
3212 last_disk_offset = disk_offset + rec_len;
3214 if (hammer_live_dedup)
3215 hammer_dedup_cache_add(ip, cursor.leaf);
3218 error = hammer_ip_next(&cursor);
3222 kprintf("BMAP %016llx: %016llx - %016llx\n",
3223 (long long)ap->a_loffset,
3224 (long long)base_offset,
3225 (long long)last_offset);
3226 kprintf("BMAP %16s: %016llx - %016llx\n", "",
3227 (long long)base_disk_offset,
3228 (long long)last_disk_offset);
3232 hammer_cache_node(&ip->cache[1], cursor.node);
3234 kprintf("bmap_end2 %016llx ip->cache %p\n",
3235 (long long)ap->a_loffset, ip->cache[1]);
3238 hammer_done_cursor(&cursor);
3239 hammer_done_transaction(&trans);
3240 lwkt_reltoken(&hmp->fs_token);
3243 * If we couldn't find any records or the records we did find were
3244 * all behind the requested offset, return failure. A forward
3245 * truncation can leave a hole w/ no on-disk records.
3247 if (last_offset == 0 || last_offset < ap->a_loffset)
3248 return (EOPNOTSUPP);
3251 * Figure out the block size at the requested offset and adjust
3252 * our limits so the cluster_read() does not create inappropriately
3253 * sized buffer cache buffers.
3255 blksize = hammer_blocksize(ap->a_loffset);
3256 if (hammer_blocksize(base_offset) != blksize) {
3257 base_offset = hammer_blockdemarc(base_offset, ap->a_loffset);
3259 if (last_offset != ap->a_loffset &&
3260 hammer_blocksize(last_offset - 1) != blksize) {
3261 last_offset = hammer_blockdemarc(ap->a_loffset,
3266 * Returning EOPNOTSUPP simply prevents the direct-IO optimization
3269 disk_offset = base_disk_offset + (ap->a_loffset - base_offset);
3271 if ((disk_offset & HAMMER_OFF_ZONE_MASK) != HAMMER_ZONE_LARGE_DATA) {
3273 * Only large-data zones can be direct-IOd
3276 } else if ((disk_offset & HAMMER_BUFMASK) ||
3277 (last_offset - ap->a_loffset) < blksize) {
3279 * doffsetp is not aligned or the forward run size does
3280 * not cover a whole buffer, disallow the direct I/O.
3287 *ap->a_doffsetp = disk_offset;
3289 *ap->a_runb = ap->a_loffset - base_offset;
3290 KKASSERT(*ap->a_runb >= 0);
3293 *ap->a_runp = last_offset - ap->a_loffset;
3294 KKASSERT(*ap->a_runp >= 0);
3302 * Write to a regular file. Because this is a strategy call the OS is
3303 * trying to actually get data onto the media.
3307 hammer_vop_strategy_write(struct vop_strategy_args *ap)
3309 hammer_record_t record;
3314 int blksize __debugvar;
3320 ip = ap->a_vp->v_data;
3323 blksize = hammer_blocksize(bio->bio_offset);
3324 KKASSERT(bp->b_bufsize == blksize);
3326 if (ip->flags & HAMMER_INODE_RO) {
3327 bp->b_error = EROFS;
3328 bp->b_flags |= B_ERROR;
3333 lwkt_gettoken(&hmp->fs_token);
3336 * Disallow swapcache operation on the vnode buffer if double
3337 * buffering is enabled, the swapcache will get the data via
3338 * the block device buffer.
3340 if (hammer_double_buffer)
3341 bp->b_flags |= B_NOTMETA;
3344 * Interlock with inode destruction (no in-kernel or directory
3345 * topology visibility). If we queue new IO while trying to
3346 * destroy the inode we can deadlock the vtrunc call in
3347 * hammer_inode_unloadable_check().
3349 * Besides, there's no point flushing a bp associated with an
3350 * inode that is being destroyed on-media and has no kernel
3353 if ((ip->flags | ip->sync_flags) &
3354 (HAMMER_INODE_DELETING|HAMMER_INODE_DELETED)) {
3357 lwkt_reltoken(&hmp->fs_token);
3362 * Reserve space and issue a direct-write from the front-end.
3363 * NOTE: The direct_io code will hammer_bread/bcopy smaller
3366 * An in-memory record will be installed to reference the storage
3367 * until the flusher can get to it.
3369 * Since we own the high level bio the front-end will not try to
3370 * do a direct-read until the write completes.
3372 * NOTE: The only time we do not reserve a full-sized buffers
3373 * worth of data is if the file is small. We do not try to
3374 * allocate a fragment (from the small-data zone) at the end of
3375 * an otherwise large file as this can lead to wildly separated
3378 KKASSERT((bio->bio_offset & HAMMER_BUFMASK) == 0);
3379 KKASSERT(bio->bio_offset < ip->ino_data.size);
3380 if (bio->bio_offset || ip->ino_data.size > HAMMER_BUFSIZE / 2)
3381 bytes = bp->b_bufsize;
3383 bytes = ((int)ip->ino_data.size + 15) & ~15;
3385 record = hammer_ip_add_bulk(ip, bio->bio_offset, bp->b_data,
3389 * B_VFSFLAG1 indicates that a REDO_WRITE entry was generated
3390 * in hammer_vop_write(). We must flag the record so the proper
3391 * REDO_TERM_WRITE entry is generated during the flush.
3394 if (bp->b_flags & B_VFSFLAG1) {
3395 record->flags |= HAMMER_RECF_REDO;
3396 bp->b_flags &= ~B_VFSFLAG1;
3398 if (record->flags & HAMMER_RECF_DEDUPED) {
3400 hammer_ip_replace_bulk(hmp, record);
3403 hammer_io_direct_write(hmp, bio, record);
3405 if (ip->rsv_recs > 1 && hmp->rsv_recs > hammer_limit_recs)
3406 hammer_flush_inode(ip, 0);
3408 bp->b_bio2.bio_offset = NOOFFSET;
3409 bp->b_error = error;
3410 bp->b_flags |= B_ERROR;
3413 lwkt_reltoken(&hmp->fs_token);
3418 * dounlink - disconnect a directory entry
3420 * XXX whiteout support not really in yet
3423 hammer_dounlink(hammer_transaction_t trans, struct nchandle *nch,
3424 struct vnode *dvp, struct ucred *cred,
3425 int flags, int isdir)
3427 struct namecache *ncp;
3431 struct hammer_cursor cursor;
3433 u_int32_t max_iterations;
3437 * Calculate the namekey and setup the key range for the scan. This
3438 * works kinda like a chained hash table where the lower 32 bits
3439 * of the namekey synthesize the chain.
3441 * The key range is inclusive of both key_beg and key_end.
3447 if (dip->flags & HAMMER_INODE_RO)
3450 namekey = hammer_directory_namekey(dip, ncp->nc_name, ncp->nc_nlen,
3453 hammer_init_cursor(trans, &cursor, &dip->cache[1], dip);
3454 cursor.key_beg.localization = dip->obj_localization +
3455 hammer_dir_localization(dip);
3456 cursor.key_beg.obj_id = dip->obj_id;
3457 cursor.key_beg.key = namekey;
3458 cursor.key_beg.create_tid = 0;
3459 cursor.key_beg.delete_tid = 0;
3460 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
3461 cursor.key_beg.obj_type = 0;
3463 cursor.key_end = cursor.key_beg;
3464 cursor.key_end.key += max_iterations;
3465 cursor.asof = dip->obj_asof;
3466 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
3469 * Scan all matching records (the chain), locate the one matching
3470 * the requested path component. info->last_error contains the
3471 * error code on search termination and could be 0, ENOENT, or
3474 * The hammer_ip_*() functions merge in-memory records with on-disk
3475 * records for the purposes of the search.
3477 error = hammer_ip_first(&cursor);
3479 while (error == 0) {
3480 error = hammer_ip_resolve_data(&cursor);
3483 nlen = cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF;
3485 if (ncp->nc_nlen == nlen &&
3486 bcmp(ncp->nc_name, cursor.data->entry.name, nlen) == 0) {
3489 error = hammer_ip_next(&cursor);
3493 * If all is ok we have to get the inode so we can adjust nlinks.
3494 * To avoid a deadlock with the flusher we must release the inode
3495 * lock on the directory when acquiring the inode for the entry.
3497 * If the target is a directory, it must be empty.
3500 hammer_unlock(&cursor.ip->lock);
3501 ip = hammer_get_inode(trans, dip, cursor.data->entry.obj_id,
3503 cursor.data->entry.localization,
3505 hammer_lock_sh(&cursor.ip->lock);
3506 if (error == ENOENT) {
3507 kprintf("HAMMER: WARNING: Removing "
3508 "dirent w/missing inode \"%s\"\n"
3509 "\tobj_id = %016llx\n",
3511 (long long)cursor.data->entry.obj_id);
3516 * If isdir >= 0 we validate that the entry is or is not a
3517 * directory. If isdir < 0 we don't care.
3519 if (error == 0 && isdir >= 0 && ip) {
3521 ip->ino_data.obj_type != HAMMER_OBJTYPE_DIRECTORY) {
3523 } else if (isdir == 0 &&
3524 ip->ino_data.obj_type == HAMMER_OBJTYPE_DIRECTORY) {
3530 * If we are trying to remove a directory the directory must
3533 * The check directory code can loop and deadlock/retry. Our
3534 * own cursor's node locks must be released to avoid a 3-way
3535 * deadlock with the flusher if the check directory code
3538 * If any changes whatsoever have been made to the cursor
3539 * set EDEADLK and retry.
3541 * WARNING: See warnings in hammer_unlock_cursor()
3544 if (error == 0 && ip && ip->ino_data.obj_type ==
3545 HAMMER_OBJTYPE_DIRECTORY) {
3546 hammer_unlock_cursor(&cursor);
3547 error = hammer_ip_check_directory_empty(trans, ip);
3548 hammer_lock_cursor(&cursor);
3549 if (cursor.flags & HAMMER_CURSOR_RETEST) {
3550 kprintf("HAMMER: Warning: avoided deadlock "
3558 * Delete the directory entry.
3560 * WARNING: hammer_ip_del_directory() may have to terminate
3561 * the cursor to avoid a deadlock. It is ok to call
3562 * hammer_done_cursor() twice.
3565 error = hammer_ip_del_directory(trans, &cursor,
3568 hammer_done_cursor(&cursor);
3571 * Tell the namecache that we are now unlinked.
3576 * NOTE: ip->vp, if non-NULL, cannot be directly
3577 * referenced without formally acquiring the
3578 * vp since the vp might have zero refs on it,
3579 * or in the middle of a reclaim, etc.
3581 * NOTE: The cache_setunresolved() can rip the vp
3582 * out from under us since the vp may not have
3583 * any refs, in which case ip->vp will be NULL
3586 while (ip && ip->vp) {
3589 error = hammer_get_vnode(ip, &vp);
3590 if (error == 0 && vp) {
3592 hammer_knote(ip->vp, NOTE_DELETE);
3593 cache_inval_vp(ip->vp, CINV_DESTROY);
3597 kprintf("Debug: HAMMER ip/vp race1 avoided\n");
3601 hammer_rel_inode(ip, 0);
3603 hammer_done_cursor(&cursor);
3605 if (error == EDEADLK)
3611 /************************************************************************
3612 * FIFO AND SPECFS OPS *
3613 ************************************************************************
3617 hammer_vop_fifoclose (struct vop_close_args *ap)
3619 /* XXX update itimes */
3620 return (VOCALL(&fifo_vnode_vops, &ap->a_head));
3624 hammer_vop_fiforead (struct vop_read_args *ap)
3628 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
3629 /* XXX update access time */
3634 hammer_vop_fifowrite (struct vop_write_args *ap)
3638 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
3639 /* XXX update access time */
3645 hammer_vop_fifokqfilter(struct vop_kqfilter_args *ap)
3649 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
3651 error = hammer_vop_kqfilter(ap);
3655 /************************************************************************
3657 ************************************************************************
3660 static void filt_hammerdetach(struct knote *kn);
3661 static int filt_hammerread(struct knote *kn, long hint);
3662 static int filt_hammerwrite(struct knote *kn, long hint);
3663 static int filt_hammervnode(struct knote *kn, long hint);
3665 static struct filterops hammerread_filtops =
3666 { FILTEROP_ISFD | FILTEROP_MPSAFE,
3667 NULL, filt_hammerdetach, filt_hammerread };
3668 static struct filterops hammerwrite_filtops =
3669 { FILTEROP_ISFD | FILTEROP_MPSAFE,
3670 NULL, filt_hammerdetach, filt_hammerwrite };
3671 static struct filterops hammervnode_filtops =
3672 { FILTEROP_ISFD | FILTEROP_MPSAFE,
3673 NULL, filt_hammerdetach, filt_hammervnode };
3677 hammer_vop_kqfilter(struct vop_kqfilter_args *ap)
3679 struct vnode *vp = ap->a_vp;
3680 struct knote *kn = ap->a_kn;
3682 switch (kn->kn_filter) {
3684 kn->kn_fop = &hammerread_filtops;
3687 kn->kn_fop = &hammerwrite_filtops;
3690 kn->kn_fop = &hammervnode_filtops;
3693 return (EOPNOTSUPP);
3696 kn->kn_hook = (caddr_t)vp;
3698 knote_insert(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
3704 filt_hammerdetach(struct knote *kn)
3706 struct vnode *vp = (void *)kn->kn_hook;
3708 knote_remove(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
3712 filt_hammerread(struct knote *kn, long hint)
3714 struct vnode *vp = (void *)kn->kn_hook;
3715 hammer_inode_t ip = VTOI(vp);
3716 hammer_mount_t hmp = ip->hmp;
3719 if (hint == NOTE_REVOKE) {
3720 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
3723 lwkt_gettoken(&hmp->fs_token); /* XXX use per-ip-token */
3724 off = ip->ino_data.size - kn->kn_fp->f_offset;
3725 kn->kn_data = (off < INTPTR_MAX) ? off : INTPTR_MAX;
3726 lwkt_reltoken(&hmp->fs_token);
3727 if (kn->kn_sfflags & NOTE_OLDAPI)
3729 return (kn->kn_data != 0);
3733 filt_hammerwrite(struct knote *kn, long hint)
3735 if (hint == NOTE_REVOKE)
3736 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
3742 filt_hammervnode(struct knote *kn, long hint)
3744 if (kn->kn_sfflags & hint)
3745 kn->kn_fflags |= hint;
3746 if (hint == NOTE_REVOKE) {
3747 kn->kn_flags |= (EV_EOF | EV_NODATA);
3750 return (kn->kn_fflags != 0);