2 * Copyright (c) 2007-2008 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * $DragonFly: src/sys/vfs/hammer/hammer_vnops.c,v 1.102 2008/10/16 17:24:16 dillon Exp $
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/fcntl.h>
41 #include <sys/namecache.h>
42 #include <sys/vnode.h>
43 #include <sys/lockf.h>
44 #include <sys/event.h>
46 #include <sys/dirent.h>
48 #include <vm/vm_extern.h>
49 #include <vm/swap_pager.h>
50 #include <vfs/fifofs/fifo.h>
57 /*static int hammer_vop_vnoperate(struct vop_generic_args *);*/
58 static int hammer_vop_fsync(struct vop_fsync_args *);
59 static int hammer_vop_read(struct vop_read_args *);
60 static int hammer_vop_write(struct vop_write_args *);
61 static int hammer_vop_access(struct vop_access_args *);
62 static int hammer_vop_advlock(struct vop_advlock_args *);
63 static int hammer_vop_close(struct vop_close_args *);
64 static int hammer_vop_ncreate(struct vop_ncreate_args *);
65 static int hammer_vop_getattr(struct vop_getattr_args *);
66 static int hammer_vop_nresolve(struct vop_nresolve_args *);
67 static int hammer_vop_nlookupdotdot(struct vop_nlookupdotdot_args *);
68 static int hammer_vop_nlink(struct vop_nlink_args *);
69 static int hammer_vop_nmkdir(struct vop_nmkdir_args *);
70 static int hammer_vop_nmknod(struct vop_nmknod_args *);
71 static int hammer_vop_open(struct vop_open_args *);
72 static int hammer_vop_print(struct vop_print_args *);
73 static int hammer_vop_readdir(struct vop_readdir_args *);
74 static int hammer_vop_readlink(struct vop_readlink_args *);
75 static int hammer_vop_nremove(struct vop_nremove_args *);
76 static int hammer_vop_nrename(struct vop_nrename_args *);
77 static int hammer_vop_nrmdir(struct vop_nrmdir_args *);
78 static int hammer_vop_markatime(struct vop_markatime_args *);
79 static int hammer_vop_setattr(struct vop_setattr_args *);
80 static int hammer_vop_strategy(struct vop_strategy_args *);
81 static int hammer_vop_bmap(struct vop_bmap_args *ap);
82 static int hammer_vop_nsymlink(struct vop_nsymlink_args *);
83 static int hammer_vop_nwhiteout(struct vop_nwhiteout_args *);
84 static int hammer_vop_ioctl(struct vop_ioctl_args *);
85 static int hammer_vop_mountctl(struct vop_mountctl_args *);
86 static int hammer_vop_kqfilter (struct vop_kqfilter_args *);
88 static int hammer_vop_fifoclose (struct vop_close_args *);
89 static int hammer_vop_fiforead (struct vop_read_args *);
90 static int hammer_vop_fifowrite (struct vop_write_args *);
91 static int hammer_vop_fifokqfilter (struct vop_kqfilter_args *);
93 struct vop_ops hammer_vnode_vops = {
94 .vop_default = vop_defaultop,
95 .vop_fsync = hammer_vop_fsync,
96 .vop_getpages = vop_stdgetpages,
97 .vop_putpages = vop_stdputpages,
98 .vop_read = hammer_vop_read,
99 .vop_write = hammer_vop_write,
100 .vop_access = hammer_vop_access,
101 .vop_advlock = hammer_vop_advlock,
102 .vop_close = hammer_vop_close,
103 .vop_ncreate = hammer_vop_ncreate,
104 .vop_getattr = hammer_vop_getattr,
105 .vop_inactive = hammer_vop_inactive,
106 .vop_reclaim = hammer_vop_reclaim,
107 .vop_nresolve = hammer_vop_nresolve,
108 .vop_nlookupdotdot = hammer_vop_nlookupdotdot,
109 .vop_nlink = hammer_vop_nlink,
110 .vop_nmkdir = hammer_vop_nmkdir,
111 .vop_nmknod = hammer_vop_nmknod,
112 .vop_open = hammer_vop_open,
113 .vop_pathconf = vop_stdpathconf,
114 .vop_print = hammer_vop_print,
115 .vop_readdir = hammer_vop_readdir,
116 .vop_readlink = hammer_vop_readlink,
117 .vop_nremove = hammer_vop_nremove,
118 .vop_nrename = hammer_vop_nrename,
119 .vop_nrmdir = hammer_vop_nrmdir,
120 .vop_markatime = hammer_vop_markatime,
121 .vop_setattr = hammer_vop_setattr,
122 .vop_bmap = hammer_vop_bmap,
123 .vop_strategy = hammer_vop_strategy,
124 .vop_nsymlink = hammer_vop_nsymlink,
125 .vop_nwhiteout = hammer_vop_nwhiteout,
126 .vop_ioctl = hammer_vop_ioctl,
127 .vop_mountctl = hammer_vop_mountctl,
128 .vop_kqfilter = hammer_vop_kqfilter
131 struct vop_ops hammer_spec_vops = {
132 .vop_default = vop_defaultop,
133 .vop_fsync = hammer_vop_fsync,
134 .vop_read = vop_stdnoread,
135 .vop_write = vop_stdnowrite,
136 .vop_access = hammer_vop_access,
137 .vop_close = hammer_vop_close,
138 .vop_markatime = hammer_vop_markatime,
139 .vop_getattr = hammer_vop_getattr,
140 .vop_inactive = hammer_vop_inactive,
141 .vop_reclaim = hammer_vop_reclaim,
142 .vop_setattr = hammer_vop_setattr
145 struct vop_ops hammer_fifo_vops = {
146 .vop_default = fifo_vnoperate,
147 .vop_fsync = hammer_vop_fsync,
148 .vop_read = hammer_vop_fiforead,
149 .vop_write = hammer_vop_fifowrite,
150 .vop_access = hammer_vop_access,
151 .vop_close = hammer_vop_fifoclose,
152 .vop_markatime = hammer_vop_markatime,
153 .vop_getattr = hammer_vop_getattr,
154 .vop_inactive = hammer_vop_inactive,
155 .vop_reclaim = hammer_vop_reclaim,
156 .vop_setattr = hammer_vop_setattr,
157 .vop_kqfilter = hammer_vop_fifokqfilter
162 hammer_knote(struct vnode *vp, int flags)
165 KNOTE(&vp->v_pollinfo.vpi_kqinfo.ki_note, flags);
168 #ifdef DEBUG_TRUNCATE
169 struct hammer_inode *HammerTruncIp;
172 static int hammer_dounlink(hammer_transaction_t trans, struct nchandle *nch,
173 struct vnode *dvp, struct ucred *cred,
174 int flags, int isdir);
175 static int hammer_vop_strategy_read(struct vop_strategy_args *ap);
176 static int hammer_vop_strategy_write(struct vop_strategy_args *ap);
181 hammer_vop_vnoperate(struct vop_generic_args *)
183 return (VOCALL(&hammer_vnode_vops, ap));
188 * hammer_vop_fsync { vp, waitfor }
190 * fsync() an inode to disk and wait for it to be completely committed
191 * such that the information would not be undone if a crash occured after
194 * NOTE: HAMMER's fsync()'s are going to remain expensive until we implement
195 * a REDO log. A sysctl is provided to relax HAMMER's fsync()
198 * Ultimately the combination of a REDO log and use of fast storage
199 * to front-end cluster caches will make fsync fast, but it aint
200 * here yet. And, in anycase, we need real transactional
201 * all-or-nothing features which are not restricted to a single file.
205 hammer_vop_fsync(struct vop_fsync_args *ap)
207 hammer_inode_t ip = VTOI(ap->a_vp);
208 hammer_mount_t hmp = ip->hmp;
209 int waitfor = ap->a_waitfor;
212 lwkt_gettoken(&hmp->fs_token);
215 * Fsync rule relaxation (default is either full synchronous flush
216 * or REDO semantics with synchronous flush).
218 if (ap->a_flags & VOP_FSYNC_SYSCALL) {
219 switch(hammer_fsync_mode) {
222 /* no REDO, full synchronous flush */
226 /* no REDO, full asynchronous flush */
227 if (waitfor == MNT_WAIT)
228 waitfor = MNT_NOWAIT;
231 /* REDO semantics, synchronous flush */
232 if (hmp->version < HAMMER_VOL_VERSION_FOUR)
234 mode = HAMMER_FLUSH_UNDOS_AUTO;
237 /* REDO semantics, relaxed asynchronous flush */
238 if (hmp->version < HAMMER_VOL_VERSION_FOUR)
240 mode = HAMMER_FLUSH_UNDOS_RELAXED;
241 if (waitfor == MNT_WAIT)
242 waitfor = MNT_NOWAIT;
245 /* ignore the fsync() system call */
246 lwkt_reltoken(&hmp->fs_token);
249 /* we have to do something */
250 mode = HAMMER_FLUSH_UNDOS_RELAXED;
251 if (waitfor == MNT_WAIT)
252 waitfor = MNT_NOWAIT;
257 * Fast fsync only needs to flush the UNDO/REDO fifo if
258 * HAMMER_INODE_REDO is non-zero and the only modifications
259 * made to the file are write or write-extends.
261 if ((ip->flags & HAMMER_INODE_REDO) &&
262 (ip->flags & HAMMER_INODE_MODMASK_NOREDO) == 0
264 ++hammer_count_fsyncs;
265 hammer_flusher_flush_undos(hmp, mode);
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 ++hammer_count_fsyncs;
297 vfsync(ap->a_vp, waitfor, 1, NULL, NULL);
298 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
299 if (waitfor == MNT_WAIT) {
300 if ((ap->a_vp->v_flag & VINACTIVE) == 0)
302 hammer_wait_inode(ip);
303 if ((ap->a_vp->v_flag & VINACTIVE) == 0)
304 vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY);
306 lwkt_reltoken(&hmp->fs_token);
311 * hammer_vop_read { vp, uio, ioflag, cred }
313 * MPSAFE (for the cache safe does not require fs_token)
317 hammer_vop_read(struct vop_read_args *ap)
319 struct hammer_transaction trans;
334 if (ap->a_vp->v_type != VREG)
343 * Attempt to shortcut directly to the VM object using lwbufs.
344 * This is much faster than instantiating buffer cache buffers.
346 resid = uio->uio_resid;
347 error = vop_helper_read_shortcut(ap);
348 hammer_stats_file_read += resid - uio->uio_resid;
351 if (uio->uio_resid == 0)
355 * Allow the UIO's size to override the sequential heuristic.
357 blksize = hammer_blocksize(uio->uio_offset);
358 seqcount = (uio->uio_resid + (BKVASIZE - 1)) / BKVASIZE;
359 ioseqcount = (ap->a_ioflag >> 16);
360 if (seqcount < ioseqcount)
361 seqcount = ioseqcount;
364 * If reading or writing a huge amount of data we have to break
365 * atomicy and allow the operation to be interrupted by a signal
366 * or it can DOS the machine.
368 bigread = (uio->uio_resid > 100 * 1024 * 1024);
371 * Access the data typically in HAMMER_BUFSIZE blocks via the
372 * buffer cache, but HAMMER may use a variable block size based
375 * XXX Temporary hack, delay the start transaction while we remain
376 * MPSAFE. NOTE: ino_data.size cannot change while vnode is
379 while (uio->uio_resid > 0 && uio->uio_offset < ip->ino_data.size) {
383 blksize = hammer_blocksize(uio->uio_offset);
384 offset = (int)uio->uio_offset & (blksize - 1);
385 base_offset = uio->uio_offset - offset;
387 if (bigread && (error = hammer_signal_check(ip->hmp)) != 0)
393 bp = getblk(ap->a_vp, base_offset, blksize, 0, 0);
394 if ((bp->b_flags & (B_INVAL | B_CACHE | B_RAM)) == B_CACHE) {
395 bp->b_flags &= ~B_AGE;
399 if (ap->a_ioflag & IO_NRDELAY) {
401 return (EWOULDBLOCK);
407 if (got_fstoken == 0) {
408 lwkt_gettoken(&hmp->fs_token);
410 hammer_start_transaction(&trans, ip->hmp);
414 * NOTE: A valid bp has already been acquired, but was not
417 if (hammer_cluster_enable) {
419 * Use file_limit to prevent cluster_read() from
420 * creating buffers of the wrong block size past
423 file_limit = ip->ino_data.size;
424 if (base_offset < HAMMER_XDEMARC &&
425 file_limit > HAMMER_XDEMARC) {
426 file_limit = HAMMER_XDEMARC;
428 error = cluster_readx(ap->a_vp,
429 file_limit, base_offset,
430 blksize, uio->uio_resid,
431 seqcount * BKVASIZE, &bp);
433 error = breadnx(ap->a_vp, base_offset, blksize,
441 if ((hammer_debug_io & 0x0001) && (bp->b_flags & B_IODEBUG)) {
442 kprintf("doff %016jx read file %016jx@%016jx\n",
443 (intmax_t)bp->b_bio2.bio_offset,
444 (intmax_t)ip->obj_id,
445 (intmax_t)bp->b_loffset);
447 bp->b_flags &= ~B_IODEBUG;
448 if (blksize == HAMMER_XBUFSIZE)
449 bp->b_flags |= B_CLUSTEROK;
451 n = blksize - offset;
452 if (n > uio->uio_resid)
454 if (n > ip->ino_data.size - uio->uio_offset)
455 n = (int)(ip->ino_data.size - uio->uio_offset);
457 lwkt_reltoken(&hmp->fs_token);
460 * Set B_AGE, data has a lower priority than meta-data.
462 * Use a hold/unlock/drop sequence to run the uiomove
463 * with the buffer unlocked, avoiding deadlocks against
464 * read()s on mmap()'d spaces.
466 bp->b_flags |= B_AGE;
467 error = uiomovebp(bp, (char *)bp->b_data + offset, n, uio);
471 lwkt_gettoken(&hmp->fs_token);
475 hammer_stats_file_read += n;
481 * Try to update the atime with just the inode lock for maximum
482 * concurrency. If we can't shortcut it we have to get the full
485 if (got_fstoken == 0 && hammer_update_atime_quick(ip) < 0) {
486 lwkt_gettoken(&hmp->fs_token);
488 hammer_start_transaction(&trans, ip->hmp);
492 if ((ip->flags & HAMMER_INODE_RO) == 0 &&
493 (ip->hmp->mp->mnt_flag & MNT_NOATIME) == 0) {
494 ip->ino_data.atime = trans.time;
495 hammer_modify_inode(&trans, ip, HAMMER_INODE_ATIME);
497 hammer_done_transaction(&trans);
498 lwkt_reltoken(&hmp->fs_token);
504 * hammer_vop_write { vp, uio, ioflag, cred }
508 hammer_vop_write(struct vop_write_args *ap)
510 struct hammer_transaction trans;
511 struct hammer_inode *ip;
526 if (ap->a_vp->v_type != VREG)
532 seqcount = ap->a_ioflag >> 16;
534 if (ip->flags & HAMMER_INODE_RO)
538 * Create a transaction to cover the operations we perform.
540 lwkt_gettoken(&hmp->fs_token);
541 hammer_start_transaction(&trans, hmp);
547 if (ap->a_ioflag & IO_APPEND)
548 uio->uio_offset = ip->ino_data.size;
551 * Check for illegal write offsets. Valid range is 0...2^63-1.
553 * NOTE: the base_off assignment is required to work around what
554 * I consider to be a GCC-4 optimization bug.
556 if (uio->uio_offset < 0) {
557 hammer_done_transaction(&trans);
558 lwkt_reltoken(&hmp->fs_token);
561 base_offset = uio->uio_offset + uio->uio_resid; /* work around gcc-4 */
562 if (uio->uio_resid > 0 && base_offset <= uio->uio_offset) {
563 hammer_done_transaction(&trans);
564 lwkt_reltoken(&hmp->fs_token);
568 if (uio->uio_resid > 0 && (td = uio->uio_td) != NULL && td->td_proc &&
569 base_offset > td->td_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
570 hammer_done_transaction(&trans);
571 lwkt_reltoken(&hmp->fs_token);
572 lwpsignal(td->td_proc, td->td_lwp, SIGXFSZ);
577 * If reading or writing a huge amount of data we have to break
578 * atomicy and allow the operation to be interrupted by a signal
579 * or it can DOS the machine.
581 * Preset redo_count so we stop generating REDOs earlier if the
584 bigwrite = (uio->uio_resid > 100 * 1024 * 1024);
585 if ((ip->flags & HAMMER_INODE_REDO) &&
586 ip->redo_count < hammer_limit_redo) {
587 ip->redo_count += uio->uio_resid;
591 * Access the data typically in HAMMER_BUFSIZE blocks via the
592 * buffer cache, but HAMMER may use a variable block size based
595 while (uio->uio_resid > 0) {
603 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_WRITE)) != 0)
605 if (bigwrite && (error = hammer_signal_check(hmp)) != 0)
608 blksize = hammer_blocksize(uio->uio_offset);
611 * Do not allow HAMMER to blow out the buffer cache. Very
612 * large UIOs can lockout other processes due to bwillwrite()
615 * The hammer inode is not locked during these operations.
616 * The vnode is locked which can interfere with the pageout
617 * daemon for non-UIO_NOCOPY writes but should not interfere
618 * with the buffer cache. Even so, we cannot afford to
619 * allow the pageout daemon to build up too many dirty buffer
622 * Only call this if we aren't being recursively called from
623 * a virtual disk device (vn), else we may deadlock.
625 if ((ap->a_ioflag & IO_RECURSE) == 0)
629 * Control the number of pending records associated with
630 * this inode. If too many have accumulated start a
631 * flush. Try to maintain a pipeline with the flusher.
633 * NOTE: It is possible for other sources to grow the
634 * records but not necessarily issue another flush,
635 * so use a timeout and ensure that a re-flush occurs.
637 if (ip->rsv_recs >= hammer_limit_inode_recs) {
638 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
639 while (ip->rsv_recs >= hammer_limit_inode_recs * 2) {
640 ip->flags |= HAMMER_INODE_RECSW;
641 tsleep(&ip->rsv_recs, 0, "hmrwww", hz);
642 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
648 * Do not allow HAMMER to blow out system memory by
649 * accumulating too many records. Records are so well
650 * decoupled from the buffer cache that it is possible
651 * for userland to push data out to the media via
652 * direct-write, but build up the records queued to the
653 * backend faster then the backend can flush them out.
654 * HAMMER has hit its write limit but the frontend has
655 * no pushback to slow it down.
657 if (hmp->rsv_recs > hammer_limit_recs / 2) {
659 * Get the inode on the flush list
661 if (ip->rsv_recs >= 64)
662 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
663 else if (ip->rsv_recs >= 16)
664 hammer_flush_inode(ip, 0);
667 * Keep the flusher going if the system keeps
670 delta = hmp->count_newrecords -
671 hmp->last_newrecords;
672 if (delta < 0 || delta > hammer_limit_recs / 2) {
673 hmp->last_newrecords = hmp->count_newrecords;
674 hammer_sync_hmp(hmp, MNT_NOWAIT);
678 * If we have gotten behind start slowing
681 delta = (hmp->rsv_recs - hammer_limit_recs) *
682 hz / hammer_limit_recs;
684 tsleep(&trans, 0, "hmrslo", delta);
689 * Calculate the blocksize at the current offset and figure
690 * out how much we can actually write.
692 blkmask = blksize - 1;
693 offset = (int)uio->uio_offset & blkmask;
694 base_offset = uio->uio_offset & ~(int64_t)blkmask;
695 n = blksize - offset;
696 if (n > uio->uio_resid) {
702 nsize = uio->uio_offset + n;
703 if (nsize > ip->ino_data.size) {
704 if (uio->uio_offset > ip->ino_data.size)
708 nvextendbuf(ap->a_vp,
711 hammer_blocksize(ip->ino_data.size),
712 hammer_blocksize(nsize),
713 hammer_blockoff(ip->ino_data.size),
714 hammer_blockoff(nsize),
717 kflags |= NOTE_EXTEND;
720 if (uio->uio_segflg == UIO_NOCOPY) {
722 * Issuing a write with the same data backing the
723 * buffer. Instantiate the buffer to collect the
724 * backing vm pages, then read-in any missing bits.
726 * This case is used by vop_stdputpages().
728 bp = getblk(ap->a_vp, base_offset,
729 blksize, GETBLK_BHEAVY, 0);
730 if ((bp->b_flags & B_CACHE) == 0) {
732 error = bread(ap->a_vp, base_offset,
735 } else if (offset == 0 && uio->uio_resid >= blksize) {
737 * Even though we are entirely overwriting the buffer
738 * we may still have to zero it out to avoid a
739 * mmap/write visibility issue.
741 bp = getblk(ap->a_vp, base_offset, blksize, GETBLK_BHEAVY, 0);
742 if ((bp->b_flags & B_CACHE) == 0)
744 } else if (base_offset >= ip->ino_data.size) {
746 * If the base offset of the buffer is beyond the
747 * file EOF, we don't have to issue a read.
749 bp = getblk(ap->a_vp, base_offset,
750 blksize, GETBLK_BHEAVY, 0);
754 * Partial overwrite, read in any missing bits then
755 * replace the portion being written.
757 error = bread(ap->a_vp, base_offset, blksize, &bp);
762 lwkt_reltoken(&hmp->fs_token);
763 error = uiomovebp(bp, bp->b_data + offset, n, uio);
764 lwkt_gettoken(&hmp->fs_token);
768 * Generate REDO records if enabled and redo_count will not
769 * exceeded the limit.
771 * If redo_count exceeds the limit we stop generating records
772 * and clear HAMMER_INODE_REDO. This will cause the next
773 * fsync() to do a full meta-data sync instead of just an
774 * UNDO/REDO fifo update.
776 * When clearing HAMMER_INODE_REDO any pre-existing REDOs
777 * will still be tracked. The tracks will be terminated
778 * when the related meta-data (including possible data
779 * modifications which are not tracked via REDO) is
782 if ((ip->flags & HAMMER_INODE_REDO) && error == 0) {
783 if (ip->redo_count < hammer_limit_redo) {
784 bp->b_flags |= B_VFSFLAG1;
785 error = hammer_generate_redo(&trans, ip,
786 base_offset + offset,
791 ip->flags &= ~HAMMER_INODE_REDO;
796 * If we screwed up we have to undo any VM size changes we
802 nvtruncbuf(ap->a_vp, ip->ino_data.size,
803 hammer_blocksize(ip->ino_data.size),
804 hammer_blockoff(ip->ino_data.size),
809 kflags |= NOTE_WRITE;
810 hammer_stats_file_write += n;
811 if (blksize == HAMMER_XBUFSIZE)
812 bp->b_flags |= B_CLUSTEROK;
813 if (ip->ino_data.size < uio->uio_offset) {
814 ip->ino_data.size = uio->uio_offset;
815 flags = HAMMER_INODE_SDIRTY;
819 ip->ino_data.mtime = trans.time;
820 flags |= HAMMER_INODE_MTIME | HAMMER_INODE_BUFS;
821 hammer_modify_inode(&trans, ip, flags);
824 * Once we dirty the buffer any cached zone-X offset
825 * becomes invalid. HAMMER NOTE: no-history mode cannot
826 * allow overwriting over the same data sector unless
827 * we provide UNDOs for the old data, which we don't.
829 bp->b_bio2.bio_offset = NOOFFSET;
832 * Final buffer disposition.
834 * Because meta-data updates are deferred, HAMMER is
835 * especially sensitive to excessive bdwrite()s because
836 * the I/O stream is not broken up by disk reads. So the
837 * buffer cache simply cannot keep up.
839 * WARNING! blksize is variable. cluster_write() is
840 * expected to not blow up if it encounters
841 * buffers that do not match the passed blksize.
843 * NOTE! Hammer shouldn't need to bawrite()/cluster_write().
844 * The ip->rsv_recs check should burst-flush the data.
845 * If we queue it immediately the buf could be left
846 * locked on the device queue for a very long time.
848 * However, failing to flush a dirty buffer out when
849 * issued from the pageout daemon can result in a low
850 * memory deadlock against bio_page_alloc(), so we
851 * have to bawrite() on IO_ASYNC as well.
853 * NOTE! To avoid degenerate stalls due to mismatched block
854 * sizes we only honor IO_DIRECT on the write which
855 * abuts the end of the buffer. However, we must
856 * honor IO_SYNC in case someone is silly enough to
857 * configure a HAMMER file as swap, or when HAMMER
858 * is serving NFS (for commits). Ick ick.
860 bp->b_flags |= B_AGE;
861 if (blksize == HAMMER_XBUFSIZE)
862 bp->b_flags |= B_CLUSTEROK;
864 if (ap->a_ioflag & IO_SYNC) {
866 } else if ((ap->a_ioflag & IO_DIRECT) && endofblk) {
868 } else if (ap->a_ioflag & IO_ASYNC) {
870 } else if (hammer_cluster_enable &&
871 !(ap->a_vp->v_mount->mnt_flag & MNT_NOCLUSTERW)) {
872 if (base_offset < HAMMER_XDEMARC)
873 cluster_eof = hammer_blockdemarc(base_offset,
876 cluster_eof = ip->ino_data.size;
877 cluster_write(bp, cluster_eof, blksize, seqcount);
882 hammer_done_transaction(&trans);
883 hammer_knote(ap->a_vp, kflags);
884 lwkt_reltoken(&hmp->fs_token);
889 * hammer_vop_access { vp, mode, cred }
891 * MPSAFE - does not require fs_token
895 hammer_vop_access(struct vop_access_args *ap)
897 struct hammer_inode *ip = VTOI(ap->a_vp);
902 ++hammer_stats_file_iopsr;
903 uid = hammer_to_unix_xid(&ip->ino_data.uid);
904 gid = hammer_to_unix_xid(&ip->ino_data.gid);
906 error = vop_helper_access(ap, uid, gid, ip->ino_data.mode,
907 ip->ino_data.uflags);
912 * hammer_vop_advlock { vp, id, op, fl, flags }
914 * MPSAFE - does not require fs_token
918 hammer_vop_advlock(struct vop_advlock_args *ap)
920 hammer_inode_t ip = VTOI(ap->a_vp);
922 return (lf_advlock(ap, &ip->advlock, ip->ino_data.size));
926 * hammer_vop_close { vp, fflag }
928 * We can only sync-on-close for normal closes. XXX disabled for now.
932 hammer_vop_close(struct vop_close_args *ap)
935 struct vnode *vp = ap->a_vp;
936 hammer_inode_t ip = VTOI(vp);
938 if (ip->flags & (HAMMER_INODE_CLOSESYNC|HAMMER_INODE_CLOSEASYNC)) {
939 if (vn_islocked(vp) == LK_EXCLUSIVE &&
940 (vp->v_flag & (VINACTIVE|VRECLAIMED)) == 0) {
941 if (ip->flags & HAMMER_INODE_CLOSESYNC)
944 waitfor = MNT_NOWAIT;
945 ip->flags &= ~(HAMMER_INODE_CLOSESYNC |
946 HAMMER_INODE_CLOSEASYNC);
947 VOP_FSYNC(vp, MNT_NOWAIT, waitfor);
951 return (vop_stdclose(ap));
955 * hammer_vop_ncreate { nch, dvp, vpp, cred, vap }
957 * The operating system has already ensured that the directory entry
958 * does not exist and done all appropriate namespace locking.
962 hammer_vop_ncreate(struct vop_ncreate_args *ap)
964 struct hammer_transaction trans;
965 struct hammer_inode *dip;
966 struct hammer_inode *nip;
967 struct nchandle *nch;
972 dip = VTOI(ap->a_dvp);
975 if (dip->flags & HAMMER_INODE_RO)
977 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0)
981 * Create a transaction to cover the operations we perform.
983 lwkt_gettoken(&hmp->fs_token);
984 hammer_start_transaction(&trans, hmp);
985 ++hammer_stats_file_iopsw;
988 * Create a new filesystem object of the requested type. The
989 * returned inode will be referenced and shared-locked to prevent
990 * it from being moved to the flusher.
992 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
993 dip, nch->ncp->nc_name, nch->ncp->nc_nlen,
996 hkprintf("hammer_create_inode error %d\n", error);
997 hammer_done_transaction(&trans);
999 lwkt_reltoken(&hmp->fs_token);
1004 * Add the new filesystem object to the directory. This will also
1005 * bump the inode's link count.
1007 error = hammer_ip_add_directory(&trans, dip,
1008 nch->ncp->nc_name, nch->ncp->nc_nlen,
1011 hkprintf("hammer_ip_add_directory error %d\n", error);
1017 hammer_rel_inode(nip, 0);
1018 hammer_done_transaction(&trans);
1021 error = hammer_get_vnode(nip, ap->a_vpp);
1022 hammer_done_transaction(&trans);
1023 hammer_rel_inode(nip, 0);
1025 cache_setunresolved(ap->a_nch);
1026 cache_setvp(ap->a_nch, *ap->a_vpp);
1028 hammer_knote(ap->a_dvp, NOTE_WRITE);
1030 lwkt_reltoken(&hmp->fs_token);
1035 * hammer_vop_getattr { vp, vap }
1037 * Retrieve an inode's attribute information. When accessing inodes
1038 * historically we fake the atime field to ensure consistent results.
1039 * The atime field is stored in the B-Tree element and allowed to be
1040 * updated without cycling the element.
1042 * MPSAFE - does not require fs_token
1046 hammer_vop_getattr(struct vop_getattr_args *ap)
1048 struct hammer_inode *ip = VTOI(ap->a_vp);
1049 struct vattr *vap = ap->a_vap;
1052 * We want the fsid to be different when accessing a filesystem
1053 * with different as-of's so programs like diff don't think
1054 * the files are the same.
1056 * We also want the fsid to be the same when comparing snapshots,
1057 * or when comparing mirrors (which might be backed by different
1058 * physical devices). HAMMER fsids are based on the PFS's
1059 * shared_uuid field.
1061 * XXX there is a chance of collision here. The va_fsid reported
1062 * by stat is different from the more involved fsid used in the
1065 ++hammer_stats_file_iopsr;
1066 hammer_lock_sh(&ip->lock);
1067 vap->va_fsid = ip->pfsm->fsid_udev ^ (u_int32_t)ip->obj_asof ^
1068 (u_int32_t)(ip->obj_asof >> 32);
1070 vap->va_fileid = ip->ino_leaf.base.obj_id;
1071 vap->va_mode = ip->ino_data.mode;
1072 vap->va_nlink = ip->ino_data.nlinks;
1073 vap->va_uid = hammer_to_unix_xid(&ip->ino_data.uid);
1074 vap->va_gid = hammer_to_unix_xid(&ip->ino_data.gid);
1077 vap->va_size = ip->ino_data.size;
1080 * Special case for @@PFS softlinks. The actual size of the
1081 * expanded softlink is "@@0x%016llx:%05d" == 26 bytes.
1082 * or for MAX_TID is "@@-1:%05d" == 10 bytes.
1084 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_SOFTLINK &&
1085 ip->ino_data.size == 10 &&
1086 ip->obj_asof == HAMMER_MAX_TID &&
1087 ip->obj_localization == 0 &&
1088 strncmp(ip->ino_data.ext.symlink, "@@PFS", 5) == 0) {
1089 if (ip->pfsm->pfsd.mirror_flags & HAMMER_PFSD_SLAVE)
1096 * We must provide a consistent atime and mtime for snapshots
1097 * so people can do a 'tar cf - ... | md5' on them and get
1098 * consistent results.
1100 if (ip->flags & HAMMER_INODE_RO) {
1101 hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_atime);
1102 hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_mtime);
1104 hammer_time_to_timespec(ip->ino_data.atime, &vap->va_atime);
1105 hammer_time_to_timespec(ip->ino_data.mtime, &vap->va_mtime);
1107 hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_ctime);
1108 vap->va_flags = ip->ino_data.uflags;
1109 vap->va_gen = 1; /* hammer inums are unique for all time */
1110 vap->va_blocksize = HAMMER_BUFSIZE;
1111 if (ip->ino_data.size >= HAMMER_XDEMARC) {
1112 vap->va_bytes = (ip->ino_data.size + HAMMER_XBUFMASK64) &
1114 } else if (ip->ino_data.size > HAMMER_BUFSIZE / 2) {
1115 vap->va_bytes = (ip->ino_data.size + HAMMER_BUFMASK64) &
1118 vap->va_bytes = (ip->ino_data.size + 15) & ~15;
1121 vap->va_type = hammer_get_vnode_type(ip->ino_data.obj_type);
1122 vap->va_filerev = 0; /* XXX */
1123 vap->va_uid_uuid = ip->ino_data.uid;
1124 vap->va_gid_uuid = ip->ino_data.gid;
1125 vap->va_fsid_uuid = ip->hmp->fsid;
1126 vap->va_vaflags = VA_UID_UUID_VALID | VA_GID_UUID_VALID |
1129 switch (ip->ino_data.obj_type) {
1130 case HAMMER_OBJTYPE_CDEV:
1131 case HAMMER_OBJTYPE_BDEV:
1132 vap->va_rmajor = ip->ino_data.rmajor;
1133 vap->va_rminor = ip->ino_data.rminor;
1138 hammer_unlock(&ip->lock);
1143 * hammer_vop_nresolve { nch, dvp, cred }
1145 * Locate the requested directory entry.
1149 hammer_vop_nresolve(struct vop_nresolve_args *ap)
1151 struct hammer_transaction trans;
1152 struct namecache *ncp;
1157 struct hammer_cursor cursor;
1166 u_int32_t localization;
1167 u_int32_t max_iterations;
1170 * Misc initialization, plus handle as-of name extensions. Look for
1171 * the '@@' extension. Note that as-of files and directories cannot
1174 dip = VTOI(ap->a_dvp);
1175 ncp = ap->a_nch->ncp;
1176 asof = dip->obj_asof;
1177 localization = dip->obj_localization; /* for code consistency */
1178 nlen = ncp->nc_nlen;
1179 flags = dip->flags & HAMMER_INODE_RO;
1183 lwkt_gettoken(&hmp->fs_token);
1184 hammer_simple_transaction(&trans, hmp);
1185 ++hammer_stats_file_iopsr;
1187 for (i = 0; i < nlen; ++i) {
1188 if (ncp->nc_name[i] == '@' && ncp->nc_name[i+1] == '@') {
1189 error = hammer_str_to_tid(ncp->nc_name + i + 2,
1190 &ispfs, &asof, &localization);
1195 if (asof != HAMMER_MAX_TID)
1196 flags |= HAMMER_INODE_RO;
1203 * If this is a PFS softlink we dive into the PFS
1205 if (ispfs && nlen == 0) {
1206 ip = hammer_get_inode(&trans, dip, HAMMER_OBJID_ROOT,
1210 error = hammer_get_vnode(ip, &vp);
1211 hammer_rel_inode(ip, 0);
1217 cache_setvp(ap->a_nch, vp);
1224 * If there is no path component the time extension is relative to dip.
1225 * e.g. "fubar/@@<snapshot>"
1227 * "." is handled by the kernel, but ".@@<snapshot>" is not.
1228 * e.g. "fubar/.@@<snapshot>"
1230 * ".." is handled by the kernel. We do not currently handle
1233 if (nlen == 0 || (nlen == 1 && ncp->nc_name[0] == '.')) {
1234 ip = hammer_get_inode(&trans, dip, dip->obj_id,
1235 asof, dip->obj_localization,
1238 error = hammer_get_vnode(ip, &vp);
1239 hammer_rel_inode(ip, 0);
1245 cache_setvp(ap->a_nch, vp);
1252 * Calculate the namekey and setup the key range for the scan. This
1253 * works kinda like a chained hash table where the lower 32 bits
1254 * of the namekey synthesize the chain.
1256 * The key range is inclusive of both key_beg and key_end.
1258 namekey = hammer_directory_namekey(dip, ncp->nc_name, nlen,
1261 error = hammer_init_cursor(&trans, &cursor, &dip->cache[1], dip);
1262 cursor.key_beg.localization = dip->obj_localization +
1263 hammer_dir_localization(dip);
1264 cursor.key_beg.obj_id = dip->obj_id;
1265 cursor.key_beg.key = namekey;
1266 cursor.key_beg.create_tid = 0;
1267 cursor.key_beg.delete_tid = 0;
1268 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1269 cursor.key_beg.obj_type = 0;
1271 cursor.key_end = cursor.key_beg;
1272 cursor.key_end.key += max_iterations;
1274 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
1277 * Scan all matching records (the chain), locate the one matching
1278 * the requested path component.
1280 * The hammer_ip_*() functions merge in-memory records with on-disk
1281 * records for the purposes of the search.
1284 localization = HAMMER_DEF_LOCALIZATION;
1287 error = hammer_ip_first(&cursor);
1288 while (error == 0) {
1289 error = hammer_ip_resolve_data(&cursor);
1292 if (nlen == cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF &&
1293 bcmp(ncp->nc_name, cursor.data->entry.name, nlen) == 0) {
1294 obj_id = cursor.data->entry.obj_id;
1295 localization = cursor.data->entry.localization;
1298 error = hammer_ip_next(&cursor);
1301 hammer_done_cursor(&cursor);
1304 * Lookup the obj_id. This should always succeed. If it does not
1305 * the filesystem may be damaged and we return a dummy inode.
1308 ip = hammer_get_inode(&trans, dip, obj_id,
1311 if (error == ENOENT) {
1312 kprintf("HAMMER: WARNING: Missing "
1313 "inode for dirent \"%s\"\n"
1314 "\tobj_id = %016llx, asof=%016llx, lo=%08x\n",
1316 (long long)obj_id, (long long)asof,
1319 ip = hammer_get_dummy_inode(&trans, dip, obj_id,
1324 error = hammer_get_vnode(ip, &vp);
1325 hammer_rel_inode(ip, 0);
1331 cache_setvp(ap->a_nch, vp);
1334 } else if (error == ENOENT) {
1335 cache_setvp(ap->a_nch, NULL);
1338 hammer_done_transaction(&trans);
1339 lwkt_reltoken(&hmp->fs_token);
1344 * hammer_vop_nlookupdotdot { dvp, vpp, cred }
1346 * Locate the parent directory of a directory vnode.
1348 * dvp is referenced but not locked. *vpp must be returned referenced and
1349 * locked. A parent_obj_id of 0 does not necessarily indicate that we are
1350 * at the root, instead it could indicate that the directory we were in was
1353 * NOTE: as-of sequences are not linked into the directory structure. If
1354 * we are at the root with a different asof then the mount point, reload
1355 * the same directory with the mount point's asof. I'm not sure what this
1356 * will do to NFS. We encode ASOF stamps in NFS file handles so it might not
1357 * get confused, but it hasn't been tested.
1361 hammer_vop_nlookupdotdot(struct vop_nlookupdotdot_args *ap)
1363 struct hammer_transaction trans;
1364 struct hammer_inode *dip;
1365 struct hammer_inode *ip;
1367 int64_t parent_obj_id;
1368 u_int32_t parent_obj_localization;
1372 dip = VTOI(ap->a_dvp);
1373 asof = dip->obj_asof;
1377 * Whos are parent? This could be the root of a pseudo-filesystem
1378 * whos parent is in another localization domain.
1380 lwkt_gettoken(&hmp->fs_token);
1381 parent_obj_id = dip->ino_data.parent_obj_id;
1382 if (dip->obj_id == HAMMER_OBJID_ROOT)
1383 parent_obj_localization = dip->ino_data.ext.obj.parent_obj_localization;
1385 parent_obj_localization = dip->obj_localization;
1387 if (parent_obj_id == 0) {
1388 if (dip->obj_id == HAMMER_OBJID_ROOT &&
1389 asof != hmp->asof) {
1390 parent_obj_id = dip->obj_id;
1392 *ap->a_fakename = kmalloc(19, M_TEMP, M_WAITOK);
1393 ksnprintf(*ap->a_fakename, 19, "0x%016llx",
1394 (long long)dip->obj_asof);
1397 lwkt_reltoken(&hmp->fs_token);
1402 hammer_simple_transaction(&trans, hmp);
1403 ++hammer_stats_file_iopsr;
1405 ip = hammer_get_inode(&trans, dip, parent_obj_id,
1406 asof, parent_obj_localization,
1407 dip->flags, &error);
1409 error = hammer_get_vnode(ip, ap->a_vpp);
1410 hammer_rel_inode(ip, 0);
1414 hammer_done_transaction(&trans);
1415 lwkt_reltoken(&hmp->fs_token);
1420 * hammer_vop_nlink { nch, dvp, vp, cred }
1424 hammer_vop_nlink(struct vop_nlink_args *ap)
1426 struct hammer_transaction trans;
1427 struct hammer_inode *dip;
1428 struct hammer_inode *ip;
1429 struct nchandle *nch;
1433 if (ap->a_dvp->v_mount != ap->a_vp->v_mount)
1437 dip = VTOI(ap->a_dvp);
1438 ip = VTOI(ap->a_vp);
1441 if (dip->obj_localization != ip->obj_localization)
1444 if (dip->flags & HAMMER_INODE_RO)
1446 if (ip->flags & HAMMER_INODE_RO)
1448 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0)
1452 * Create a transaction to cover the operations we perform.
1454 lwkt_gettoken(&hmp->fs_token);
1455 hammer_start_transaction(&trans, hmp);
1456 ++hammer_stats_file_iopsw;
1459 * Add the filesystem object to the directory. Note that neither
1460 * dip nor ip are referenced or locked, but their vnodes are
1461 * referenced. This function will bump the inode's link count.
1463 error = hammer_ip_add_directory(&trans, dip,
1464 nch->ncp->nc_name, nch->ncp->nc_nlen,
1471 cache_setunresolved(nch);
1472 cache_setvp(nch, ap->a_vp);
1474 hammer_done_transaction(&trans);
1475 hammer_knote(ap->a_vp, NOTE_LINK);
1476 hammer_knote(ap->a_dvp, NOTE_WRITE);
1477 lwkt_reltoken(&hmp->fs_token);
1482 * hammer_vop_nmkdir { nch, dvp, vpp, cred, vap }
1484 * The operating system has already ensured that the directory entry
1485 * does not exist and done all appropriate namespace locking.
1489 hammer_vop_nmkdir(struct vop_nmkdir_args *ap)
1491 struct hammer_transaction trans;
1492 struct hammer_inode *dip;
1493 struct hammer_inode *nip;
1494 struct nchandle *nch;
1499 dip = VTOI(ap->a_dvp);
1502 if (dip->flags & HAMMER_INODE_RO)
1504 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0)
1508 * Create a transaction to cover the operations we perform.
1510 lwkt_gettoken(&hmp->fs_token);
1511 hammer_start_transaction(&trans, hmp);
1512 ++hammer_stats_file_iopsw;
1515 * Create a new filesystem object of the requested type. The
1516 * returned inode will be referenced but not locked.
1518 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
1519 dip, nch->ncp->nc_name, nch->ncp->nc_nlen,
1522 hkprintf("hammer_mkdir error %d\n", error);
1523 hammer_done_transaction(&trans);
1525 lwkt_reltoken(&hmp->fs_token);
1529 * Add the new filesystem object to the directory. This will also
1530 * bump the inode's link count.
1532 error = hammer_ip_add_directory(&trans, dip,
1533 nch->ncp->nc_name, nch->ncp->nc_nlen,
1536 hkprintf("hammer_mkdir (add) error %d\n", error);
1542 hammer_rel_inode(nip, 0);
1545 error = hammer_get_vnode(nip, ap->a_vpp);
1546 hammer_rel_inode(nip, 0);
1548 cache_setunresolved(ap->a_nch);
1549 cache_setvp(ap->a_nch, *ap->a_vpp);
1552 hammer_done_transaction(&trans);
1554 hammer_knote(ap->a_dvp, NOTE_WRITE | NOTE_LINK);
1555 lwkt_reltoken(&hmp->fs_token);
1560 * hammer_vop_nmknod { nch, dvp, vpp, cred, vap }
1562 * The operating system has already ensured that the directory entry
1563 * does not exist and done all appropriate namespace locking.
1567 hammer_vop_nmknod(struct vop_nmknod_args *ap)
1569 struct hammer_transaction trans;
1570 struct hammer_inode *dip;
1571 struct hammer_inode *nip;
1572 struct nchandle *nch;
1577 dip = VTOI(ap->a_dvp);
1580 if (dip->flags & HAMMER_INODE_RO)
1582 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0)
1586 * Create a transaction to cover the operations we perform.
1588 lwkt_gettoken(&hmp->fs_token);
1589 hammer_start_transaction(&trans, hmp);
1590 ++hammer_stats_file_iopsw;
1593 * Create a new filesystem object of the requested type. The
1594 * returned inode will be referenced but not locked.
1596 * If mknod specifies a directory a pseudo-fs is created.
1598 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
1599 dip, nch->ncp->nc_name, nch->ncp->nc_nlen,
1602 hammer_done_transaction(&trans);
1604 lwkt_reltoken(&hmp->fs_token);
1609 * Add the new filesystem object to the directory. This will also
1610 * bump the inode's link count.
1612 error = hammer_ip_add_directory(&trans, dip,
1613 nch->ncp->nc_name, nch->ncp->nc_nlen,
1620 hammer_rel_inode(nip, 0);
1623 error = hammer_get_vnode(nip, ap->a_vpp);
1624 hammer_rel_inode(nip, 0);
1626 cache_setunresolved(ap->a_nch);
1627 cache_setvp(ap->a_nch, *ap->a_vpp);
1630 hammer_done_transaction(&trans);
1632 hammer_knote(ap->a_dvp, NOTE_WRITE);
1633 lwkt_reltoken(&hmp->fs_token);
1638 * hammer_vop_open { vp, mode, cred, fp }
1640 * MPSAFE (does not require fs_token)
1644 hammer_vop_open(struct vop_open_args *ap)
1648 ++hammer_stats_file_iopsr;
1649 ip = VTOI(ap->a_vp);
1651 if ((ap->a_mode & FWRITE) && (ip->flags & HAMMER_INODE_RO))
1653 return(vop_stdopen(ap));
1657 * hammer_vop_print { vp }
1661 hammer_vop_print(struct vop_print_args *ap)
1667 * hammer_vop_readdir { vp, uio, cred, *eofflag, *ncookies, off_t **cookies }
1671 hammer_vop_readdir(struct vop_readdir_args *ap)
1673 struct hammer_transaction trans;
1674 struct hammer_cursor cursor;
1675 struct hammer_inode *ip;
1678 hammer_base_elm_t base;
1687 ++hammer_stats_file_iopsr;
1688 ip = VTOI(ap->a_vp);
1690 saveoff = uio->uio_offset;
1693 if (ap->a_ncookies) {
1694 ncookies = uio->uio_resid / 16 + 1;
1695 if (ncookies > 1024)
1697 cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
1705 lwkt_gettoken(&hmp->fs_token);
1706 hammer_simple_transaction(&trans, hmp);
1709 * Handle artificial entries
1711 * It should be noted that the minimum value for a directory
1712 * hash key on-media is 0x0000000100000000, so we can use anything
1713 * less then that to represent our 'special' key space.
1717 r = vop_write_dirent(&error, uio, ip->obj_id, DT_DIR, 1, ".");
1721 cookies[cookie_index] = saveoff;
1724 if (cookie_index == ncookies)
1728 if (ip->ino_data.parent_obj_id) {
1729 r = vop_write_dirent(&error, uio,
1730 ip->ino_data.parent_obj_id,
1733 r = vop_write_dirent(&error, uio,
1734 ip->obj_id, DT_DIR, 2, "..");
1739 cookies[cookie_index] = saveoff;
1742 if (cookie_index == ncookies)
1747 * Key range (begin and end inclusive) to scan. Directory keys
1748 * directly translate to a 64 bit 'seek' position.
1750 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
1751 cursor.key_beg.localization = ip->obj_localization +
1752 hammer_dir_localization(ip);
1753 cursor.key_beg.obj_id = ip->obj_id;
1754 cursor.key_beg.create_tid = 0;
1755 cursor.key_beg.delete_tid = 0;
1756 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1757 cursor.key_beg.obj_type = 0;
1758 cursor.key_beg.key = saveoff;
1760 cursor.key_end = cursor.key_beg;
1761 cursor.key_end.key = HAMMER_MAX_KEY;
1762 cursor.asof = ip->obj_asof;
1763 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
1765 error = hammer_ip_first(&cursor);
1767 while (error == 0) {
1768 error = hammer_ip_resolve_data(&cursor);
1771 base = &cursor.leaf->base;
1772 saveoff = base->key;
1773 KKASSERT(cursor.leaf->data_len > HAMMER_ENTRY_NAME_OFF);
1775 if (base->obj_id != ip->obj_id)
1776 panic("readdir: bad record at %p", cursor.node);
1779 * Convert pseudo-filesystems into softlinks
1781 dtype = hammer_get_dtype(cursor.leaf->base.obj_type);
1782 r = vop_write_dirent(
1783 &error, uio, cursor.data->entry.obj_id,
1785 cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF ,
1786 (void *)cursor.data->entry.name);
1791 cookies[cookie_index] = base->key;
1793 if (cookie_index == ncookies)
1795 error = hammer_ip_next(&cursor);
1797 hammer_done_cursor(&cursor);
1800 hammer_done_transaction(&trans);
1803 *ap->a_eofflag = (error == ENOENT);
1804 uio->uio_offset = saveoff;
1805 if (error && cookie_index == 0) {
1806 if (error == ENOENT)
1809 kfree(cookies, M_TEMP);
1810 *ap->a_ncookies = 0;
1811 *ap->a_cookies = NULL;
1814 if (error == ENOENT)
1817 *ap->a_ncookies = cookie_index;
1818 *ap->a_cookies = cookies;
1821 lwkt_reltoken(&hmp->fs_token);
1826 * hammer_vop_readlink { vp, uio, cred }
1830 hammer_vop_readlink(struct vop_readlink_args *ap)
1832 struct hammer_transaction trans;
1833 struct hammer_cursor cursor;
1834 struct hammer_inode *ip;
1837 u_int32_t localization;
1838 hammer_pseudofs_inmem_t pfsm;
1841 ip = VTOI(ap->a_vp);
1844 lwkt_gettoken(&hmp->fs_token);
1847 * Shortcut if the symlink data was stuffed into ino_data.
1849 * Also expand special "@@PFS%05d" softlinks (expansion only
1850 * occurs for non-historical (current) accesses made from the
1851 * primary filesystem).
1853 if (ip->ino_data.size <= HAMMER_INODE_BASESYMLEN) {
1857 ptr = ip->ino_data.ext.symlink;
1858 bytes = (int)ip->ino_data.size;
1860 ip->obj_asof == HAMMER_MAX_TID &&
1861 ip->obj_localization == 0 &&
1862 strncmp(ptr, "@@PFS", 5) == 0) {
1863 hammer_simple_transaction(&trans, hmp);
1864 bcopy(ptr + 5, buf, 5);
1866 localization = strtoul(buf, NULL, 10) << 16;
1867 pfsm = hammer_load_pseudofs(&trans, localization,
1870 if (pfsm->pfsd.mirror_flags &
1871 HAMMER_PFSD_SLAVE) {
1872 /* vap->va_size == 26 */
1873 ksnprintf(buf, sizeof(buf),
1875 (long long)pfsm->pfsd.sync_end_tid,
1876 localization >> 16);
1878 /* vap->va_size == 10 */
1879 ksnprintf(buf, sizeof(buf),
1881 localization >> 16);
1883 ksnprintf(buf, sizeof(buf),
1885 (long long)HAMMER_MAX_TID,
1886 localization >> 16);
1890 bytes = strlen(buf);
1893 hammer_rel_pseudofs(hmp, pfsm);
1894 hammer_done_transaction(&trans);
1896 error = uiomove(ptr, bytes, ap->a_uio);
1897 lwkt_reltoken(&hmp->fs_token);
1904 hammer_simple_transaction(&trans, hmp);
1905 ++hammer_stats_file_iopsr;
1906 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
1909 * Key range (begin and end inclusive) to scan. Directory keys
1910 * directly translate to a 64 bit 'seek' position.
1912 cursor.key_beg.localization = ip->obj_localization +
1913 HAMMER_LOCALIZE_MISC;
1914 cursor.key_beg.obj_id = ip->obj_id;
1915 cursor.key_beg.create_tid = 0;
1916 cursor.key_beg.delete_tid = 0;
1917 cursor.key_beg.rec_type = HAMMER_RECTYPE_FIX;
1918 cursor.key_beg.obj_type = 0;
1919 cursor.key_beg.key = HAMMER_FIXKEY_SYMLINK;
1920 cursor.asof = ip->obj_asof;
1921 cursor.flags |= HAMMER_CURSOR_ASOF;
1923 error = hammer_ip_lookup(&cursor);
1925 error = hammer_ip_resolve_data(&cursor);
1927 KKASSERT(cursor.leaf->data_len >=
1928 HAMMER_SYMLINK_NAME_OFF);
1929 error = uiomove(cursor.data->symlink.name,
1930 cursor.leaf->data_len -
1931 HAMMER_SYMLINK_NAME_OFF,
1935 hammer_done_cursor(&cursor);
1936 hammer_done_transaction(&trans);
1937 lwkt_reltoken(&hmp->fs_token);
1942 * hammer_vop_nremove { nch, dvp, cred }
1946 hammer_vop_nremove(struct vop_nremove_args *ap)
1948 struct hammer_transaction trans;
1949 struct hammer_inode *dip;
1953 dip = VTOI(ap->a_dvp);
1956 if (hammer_nohistory(dip) == 0 &&
1957 (error = hammer_checkspace(hmp, HAMMER_CHKSPC_REMOVE)) != 0) {
1961 lwkt_gettoken(&hmp->fs_token);
1962 hammer_start_transaction(&trans, hmp);
1963 ++hammer_stats_file_iopsw;
1964 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp, ap->a_cred, 0, 0);
1965 hammer_done_transaction(&trans);
1967 hammer_knote(ap->a_dvp, NOTE_WRITE);
1968 lwkt_reltoken(&hmp->fs_token);
1973 * hammer_vop_nrename { fnch, tnch, fdvp, tdvp, cred }
1977 hammer_vop_nrename(struct vop_nrename_args *ap)
1979 struct hammer_transaction trans;
1980 struct namecache *fncp;
1981 struct namecache *tncp;
1982 struct hammer_inode *fdip;
1983 struct hammer_inode *tdip;
1984 struct hammer_inode *ip;
1986 struct hammer_cursor cursor;
1988 u_int32_t max_iterations;
1991 if (ap->a_fdvp->v_mount != ap->a_tdvp->v_mount)
1993 if (ap->a_fdvp->v_mount != ap->a_fnch->ncp->nc_vp->v_mount)
1996 fdip = VTOI(ap->a_fdvp);
1997 tdip = VTOI(ap->a_tdvp);
1998 fncp = ap->a_fnch->ncp;
1999 tncp = ap->a_tnch->ncp;
2000 ip = VTOI(fncp->nc_vp);
2001 KKASSERT(ip != NULL);
2005 if (fdip->obj_localization != tdip->obj_localization)
2007 if (fdip->obj_localization != ip->obj_localization)
2010 if (fdip->flags & HAMMER_INODE_RO)
2012 if (tdip->flags & HAMMER_INODE_RO)
2014 if (ip->flags & HAMMER_INODE_RO)
2016 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0)
2019 lwkt_gettoken(&hmp->fs_token);
2020 hammer_start_transaction(&trans, hmp);
2021 ++hammer_stats_file_iopsw;
2024 * Remove tncp from the target directory and then link ip as
2025 * tncp. XXX pass trans to dounlink
2027 * Force the inode sync-time to match the transaction so it is
2028 * in-sync with the creation of the target directory entry.
2030 error = hammer_dounlink(&trans, ap->a_tnch, ap->a_tdvp,
2032 if (error == 0 || error == ENOENT) {
2033 error = hammer_ip_add_directory(&trans, tdip,
2034 tncp->nc_name, tncp->nc_nlen,
2037 ip->ino_data.parent_obj_id = tdip->obj_id;
2038 ip->ino_data.ctime = trans.time;
2039 hammer_modify_inode(&trans, ip, HAMMER_INODE_DDIRTY);
2043 goto failed; /* XXX */
2046 * Locate the record in the originating directory and remove it.
2048 * Calculate the namekey and setup the key range for the scan. This
2049 * works kinda like a chained hash table where the lower 32 bits
2050 * of the namekey synthesize the chain.
2052 * The key range is inclusive of both key_beg and key_end.
2054 namekey = hammer_directory_namekey(fdip, fncp->nc_name, fncp->nc_nlen,
2057 hammer_init_cursor(&trans, &cursor, &fdip->cache[1], fdip);
2058 cursor.key_beg.localization = fdip->obj_localization +
2059 hammer_dir_localization(fdip);
2060 cursor.key_beg.obj_id = fdip->obj_id;
2061 cursor.key_beg.key = namekey;
2062 cursor.key_beg.create_tid = 0;
2063 cursor.key_beg.delete_tid = 0;
2064 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
2065 cursor.key_beg.obj_type = 0;
2067 cursor.key_end = cursor.key_beg;
2068 cursor.key_end.key += max_iterations;
2069 cursor.asof = fdip->obj_asof;
2070 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
2073 * Scan all matching records (the chain), locate the one matching
2074 * the requested path component.
2076 * The hammer_ip_*() functions merge in-memory records with on-disk
2077 * records for the purposes of the search.
2079 error = hammer_ip_first(&cursor);
2080 while (error == 0) {
2081 if (hammer_ip_resolve_data(&cursor) != 0)
2083 nlen = cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF;
2085 if (fncp->nc_nlen == nlen &&
2086 bcmp(fncp->nc_name, cursor.data->entry.name, nlen) == 0) {
2089 error = hammer_ip_next(&cursor);
2093 * If all is ok we have to get the inode so we can adjust nlinks.
2095 * WARNING: hammer_ip_del_directory() may have to terminate the
2096 * cursor to avoid a recursion. It's ok to call hammer_done_cursor()
2100 error = hammer_ip_del_directory(&trans, &cursor, fdip, ip);
2103 * XXX A deadlock here will break rename's atomicy for the purposes
2104 * of crash recovery.
2106 if (error == EDEADLK) {
2107 hammer_done_cursor(&cursor);
2112 * Cleanup and tell the kernel that the rename succeeded.
2114 * NOTE: ip->vp, if non-NULL, cannot be directly referenced
2115 * without formally acquiring the vp since the vp might
2116 * have zero refs on it, or in the middle of a reclaim,
2119 hammer_done_cursor(&cursor);
2121 cache_rename(ap->a_fnch, ap->a_tnch);
2122 hammer_knote(ap->a_fdvp, NOTE_WRITE);
2123 hammer_knote(ap->a_tdvp, NOTE_WRITE);
2127 error = hammer_get_vnode(ip, &vp);
2128 if (error == 0 && vp) {
2130 hammer_knote(ip->vp, NOTE_RENAME);
2134 kprintf("Debug: HAMMER ip/vp race2 avoided\n");
2139 hammer_done_transaction(&trans);
2140 lwkt_reltoken(&hmp->fs_token);
2145 * hammer_vop_nrmdir { nch, dvp, cred }
2149 hammer_vop_nrmdir(struct vop_nrmdir_args *ap)
2151 struct hammer_transaction trans;
2152 struct hammer_inode *dip;
2156 dip = VTOI(ap->a_dvp);
2159 if (hammer_nohistory(dip) == 0 &&
2160 (error = hammer_checkspace(hmp, HAMMER_CHKSPC_REMOVE)) != 0) {
2164 lwkt_gettoken(&hmp->fs_token);
2165 hammer_start_transaction(&trans, hmp);
2166 ++hammer_stats_file_iopsw;
2167 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp, ap->a_cred, 0, 1);
2168 hammer_done_transaction(&trans);
2170 hammer_knote(ap->a_dvp, NOTE_WRITE | NOTE_LINK);
2171 lwkt_reltoken(&hmp->fs_token);
2176 * hammer_vop_markatime { vp, cred }
2180 hammer_vop_markatime(struct vop_markatime_args *ap)
2182 struct hammer_transaction trans;
2183 struct hammer_inode *ip;
2186 ip = VTOI(ap->a_vp);
2187 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
2189 if (ip->flags & HAMMER_INODE_RO)
2192 if (hmp->mp->mnt_flag & MNT_NOATIME)
2194 lwkt_gettoken(&hmp->fs_token);
2195 hammer_start_transaction(&trans, hmp);
2196 ++hammer_stats_file_iopsw;
2198 ip->ino_data.atime = trans.time;
2199 hammer_modify_inode(&trans, ip, HAMMER_INODE_ATIME);
2200 hammer_done_transaction(&trans);
2201 hammer_knote(ap->a_vp, NOTE_ATTRIB);
2202 lwkt_reltoken(&hmp->fs_token);
2207 * hammer_vop_setattr { vp, vap, cred }
2211 hammer_vop_setattr(struct vop_setattr_args *ap)
2213 struct hammer_transaction trans;
2214 struct hammer_inode *ip;
2223 int64_t aligned_size;
2228 ip = ap->a_vp->v_data;
2233 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
2235 if (ip->flags & HAMMER_INODE_RO)
2237 if (hammer_nohistory(ip) == 0 &&
2238 (error = hammer_checkspace(hmp, HAMMER_CHKSPC_REMOVE)) != 0) {
2242 lwkt_gettoken(&hmp->fs_token);
2243 hammer_start_transaction(&trans, hmp);
2244 ++hammer_stats_file_iopsw;
2247 if (vap->va_flags != VNOVAL) {
2248 flags = ip->ino_data.uflags;
2249 error = vop_helper_setattr_flags(&flags, vap->va_flags,
2250 hammer_to_unix_xid(&ip->ino_data.uid),
2253 if (ip->ino_data.uflags != flags) {
2254 ip->ino_data.uflags = flags;
2255 ip->ino_data.ctime = trans.time;
2256 modflags |= HAMMER_INODE_DDIRTY;
2257 kflags |= NOTE_ATTRIB;
2259 if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) {
2266 if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) {
2270 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
2271 mode_t cur_mode = ip->ino_data.mode;
2272 uid_t cur_uid = hammer_to_unix_xid(&ip->ino_data.uid);
2273 gid_t cur_gid = hammer_to_unix_xid(&ip->ino_data.gid);
2277 error = vop_helper_chown(ap->a_vp, vap->va_uid, vap->va_gid,
2279 &cur_uid, &cur_gid, &cur_mode);
2281 hammer_guid_to_uuid(&uuid_uid, cur_uid);
2282 hammer_guid_to_uuid(&uuid_gid, cur_gid);
2283 if (bcmp(&uuid_uid, &ip->ino_data.uid,
2284 sizeof(uuid_uid)) ||
2285 bcmp(&uuid_gid, &ip->ino_data.gid,
2286 sizeof(uuid_gid)) ||
2287 ip->ino_data.mode != cur_mode
2289 ip->ino_data.uid = uuid_uid;
2290 ip->ino_data.gid = uuid_gid;
2291 ip->ino_data.mode = cur_mode;
2292 ip->ino_data.ctime = trans.time;
2293 modflags |= HAMMER_INODE_DDIRTY;
2295 kflags |= NOTE_ATTRIB;
2298 while (vap->va_size != VNOVAL && ip->ino_data.size != vap->va_size) {
2299 switch(ap->a_vp->v_type) {
2301 if (vap->va_size == ip->ino_data.size)
2305 * Log the operation if in fast-fsync mode or if
2306 * there are unterminated redo write records present.
2308 * The second check is needed so the recovery code
2309 * properly truncates write redos even if nominal
2310 * REDO operations is turned off due to excessive
2311 * writes, because the related records might be
2312 * destroyed and never lay down a TERM_WRITE.
2314 if ((ip->flags & HAMMER_INODE_REDO) ||
2315 (ip->flags & HAMMER_INODE_RDIRTY)) {
2316 error = hammer_generate_redo(&trans, ip,
2321 blksize = hammer_blocksize(vap->va_size);
2324 * XXX break atomicy, we can deadlock the backend
2325 * if we do not release the lock. Probably not a
2328 if (vap->va_size < ip->ino_data.size) {
2329 nvtruncbuf(ap->a_vp, vap->va_size,
2331 hammer_blockoff(vap->va_size),
2334 kflags |= NOTE_WRITE;
2336 nvextendbuf(ap->a_vp,
2339 hammer_blocksize(ip->ino_data.size),
2340 hammer_blocksize(vap->va_size),
2341 hammer_blockoff(ip->ino_data.size),
2342 hammer_blockoff(vap->va_size),
2345 kflags |= NOTE_WRITE | NOTE_EXTEND;
2347 ip->ino_data.size = vap->va_size;
2348 ip->ino_data.mtime = trans.time;
2349 /* XXX safe to use SDIRTY instead of DDIRTY here? */
2350 modflags |= HAMMER_INODE_MTIME | HAMMER_INODE_DDIRTY;
2353 * On-media truncation is cached in the inode until
2354 * the inode is synchronized. We must immediately
2355 * handle any frontend records.
2358 hammer_ip_frontend_trunc(ip, vap->va_size);
2359 #ifdef DEBUG_TRUNCATE
2360 if (HammerTruncIp == NULL)
2363 if ((ip->flags & HAMMER_INODE_TRUNCATED) == 0) {
2364 ip->flags |= HAMMER_INODE_TRUNCATED;
2365 ip->trunc_off = vap->va_size;
2366 #ifdef DEBUG_TRUNCATE
2367 if (ip == HammerTruncIp)
2368 kprintf("truncate1 %016llx\n",
2369 (long long)ip->trunc_off);
2371 } else if (ip->trunc_off > vap->va_size) {
2372 ip->trunc_off = vap->va_size;
2373 #ifdef DEBUG_TRUNCATE
2374 if (ip == HammerTruncIp)
2375 kprintf("truncate2 %016llx\n",
2376 (long long)ip->trunc_off);
2379 #ifdef DEBUG_TRUNCATE
2380 if (ip == HammerTruncIp)
2381 kprintf("truncate3 %016llx (ignored)\n",
2382 (long long)vap->va_size);
2389 * When truncating, nvtruncbuf() may have cleaned out
2390 * a portion of the last block on-disk in the buffer
2391 * cache. We must clean out any frontend records
2392 * for blocks beyond the new last block.
2394 aligned_size = (vap->va_size + (blksize - 1)) &
2395 ~(int64_t)(blksize - 1);
2396 if (truncating && vap->va_size < aligned_size) {
2397 aligned_size -= blksize;
2398 hammer_ip_frontend_trunc(ip, aligned_size);
2403 if ((ip->flags & HAMMER_INODE_TRUNCATED) == 0) {
2404 ip->flags |= HAMMER_INODE_TRUNCATED;
2405 ip->trunc_off = vap->va_size;
2406 } else if (ip->trunc_off > vap->va_size) {
2407 ip->trunc_off = vap->va_size;
2409 hammer_ip_frontend_trunc(ip, vap->va_size);
2410 ip->ino_data.size = vap->va_size;
2411 ip->ino_data.mtime = trans.time;
2412 modflags |= HAMMER_INODE_MTIME | HAMMER_INODE_DDIRTY;
2413 kflags |= NOTE_ATTRIB;
2421 if (vap->va_atime.tv_sec != VNOVAL) {
2422 ip->ino_data.atime = hammer_timespec_to_time(&vap->va_atime);
2423 modflags |= HAMMER_INODE_ATIME;
2424 kflags |= NOTE_ATTRIB;
2426 if (vap->va_mtime.tv_sec != VNOVAL) {
2427 ip->ino_data.mtime = hammer_timespec_to_time(&vap->va_mtime);
2428 modflags |= HAMMER_INODE_MTIME;
2429 kflags |= NOTE_ATTRIB;
2431 if (vap->va_mode != (mode_t)VNOVAL) {
2432 mode_t cur_mode = ip->ino_data.mode;
2433 uid_t cur_uid = hammer_to_unix_xid(&ip->ino_data.uid);
2434 gid_t cur_gid = hammer_to_unix_xid(&ip->ino_data.gid);
2436 error = vop_helper_chmod(ap->a_vp, vap->va_mode, ap->a_cred,
2437 cur_uid, cur_gid, &cur_mode);
2438 if (error == 0 && ip->ino_data.mode != cur_mode) {
2439 ip->ino_data.mode = cur_mode;
2440 ip->ino_data.ctime = trans.time;
2441 modflags |= HAMMER_INODE_DDIRTY;
2442 kflags |= NOTE_ATTRIB;
2447 hammer_modify_inode(&trans, ip, modflags);
2448 hammer_done_transaction(&trans);
2449 hammer_knote(ap->a_vp, kflags);
2450 lwkt_reltoken(&hmp->fs_token);
2455 * hammer_vop_nsymlink { nch, dvp, vpp, cred, vap, target }
2459 hammer_vop_nsymlink(struct vop_nsymlink_args *ap)
2461 struct hammer_transaction trans;
2462 struct hammer_inode *dip;
2463 struct hammer_inode *nip;
2464 hammer_record_t record;
2465 struct nchandle *nch;
2470 ap->a_vap->va_type = VLNK;
2473 dip = VTOI(ap->a_dvp);
2476 if (dip->flags & HAMMER_INODE_RO)
2478 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0)
2482 * Create a transaction to cover the operations we perform.
2484 lwkt_gettoken(&hmp->fs_token);
2485 hammer_start_transaction(&trans, hmp);
2486 ++hammer_stats_file_iopsw;
2489 * Create a new filesystem object of the requested type. The
2490 * returned inode will be referenced but not locked.
2493 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
2494 dip, nch->ncp->nc_name, nch->ncp->nc_nlen,
2497 hammer_done_transaction(&trans);
2499 lwkt_reltoken(&hmp->fs_token);
2504 * Add a record representing the symlink. symlink stores the link
2505 * as pure data, not a string, and is no \0 terminated.
2508 bytes = strlen(ap->a_target);
2510 if (bytes <= HAMMER_INODE_BASESYMLEN) {
2511 bcopy(ap->a_target, nip->ino_data.ext.symlink, bytes);
2513 record = hammer_alloc_mem_record(nip, bytes);
2514 record->type = HAMMER_MEM_RECORD_GENERAL;
2516 record->leaf.base.localization = nip->obj_localization +
2517 HAMMER_LOCALIZE_MISC;
2518 record->leaf.base.key = HAMMER_FIXKEY_SYMLINK;
2519 record->leaf.base.rec_type = HAMMER_RECTYPE_FIX;
2520 record->leaf.data_len = bytes;
2521 KKASSERT(HAMMER_SYMLINK_NAME_OFF == 0);
2522 bcopy(ap->a_target, record->data->symlink.name, bytes);
2523 error = hammer_ip_add_record(&trans, record);
2527 * Set the file size to the length of the link.
2530 nip->ino_data.size = bytes;
2531 hammer_modify_inode(&trans, nip, HAMMER_INODE_DDIRTY);
2535 error = hammer_ip_add_directory(&trans, dip, nch->ncp->nc_name,
2536 nch->ncp->nc_nlen, nip);
2542 hammer_rel_inode(nip, 0);
2545 error = hammer_get_vnode(nip, ap->a_vpp);
2546 hammer_rel_inode(nip, 0);
2548 cache_setunresolved(ap->a_nch);
2549 cache_setvp(ap->a_nch, *ap->a_vpp);
2550 hammer_knote(ap->a_dvp, NOTE_WRITE);
2553 hammer_done_transaction(&trans);
2554 lwkt_reltoken(&hmp->fs_token);
2559 * hammer_vop_nwhiteout { nch, dvp, cred, flags }
2563 hammer_vop_nwhiteout(struct vop_nwhiteout_args *ap)
2565 struct hammer_transaction trans;
2566 struct hammer_inode *dip;
2570 dip = VTOI(ap->a_dvp);
2573 if (hammer_nohistory(dip) == 0 &&
2574 (error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0) {
2578 lwkt_gettoken(&hmp->fs_token);
2579 hammer_start_transaction(&trans, hmp);
2580 ++hammer_stats_file_iopsw;
2581 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp,
2582 ap->a_cred, ap->a_flags, -1);
2583 hammer_done_transaction(&trans);
2584 lwkt_reltoken(&hmp->fs_token);
2590 * hammer_vop_ioctl { vp, command, data, fflag, cred }
2594 hammer_vop_ioctl(struct vop_ioctl_args *ap)
2596 struct hammer_inode *ip = ap->a_vp->v_data;
2597 hammer_mount_t hmp = ip->hmp;
2600 ++hammer_stats_file_iopsr;
2601 lwkt_gettoken(&hmp->fs_token);
2602 error = hammer_ioctl(ip, ap->a_command, ap->a_data,
2603 ap->a_fflag, ap->a_cred);
2604 lwkt_reltoken(&hmp->fs_token);
2610 hammer_vop_mountctl(struct vop_mountctl_args *ap)
2612 static const struct mountctl_opt extraopt[] = {
2613 { HMNT_NOHISTORY, "nohistory" },
2614 { HMNT_MASTERID, "master" },
2618 struct hammer_mount *hmp;
2625 mp = ap->a_head.a_ops->head.vv_mount;
2626 KKASSERT(mp->mnt_data != NULL);
2627 hmp = (struct hammer_mount *)mp->mnt_data;
2629 lwkt_gettoken(&hmp->fs_token);
2632 case MOUNTCTL_SET_EXPORT:
2633 if (ap->a_ctllen != sizeof(struct export_args))
2636 error = hammer_vfs_export(mp, ap->a_op,
2637 (const struct export_args *)ap->a_ctl);
2639 case MOUNTCTL_MOUNTFLAGS:
2642 * Call standard mountctl VOP function
2643 * so we get user mount flags.
2645 error = vop_stdmountctl(ap);
2649 usedbytes = *ap->a_res;
2651 if (usedbytes > 0 && usedbytes < ap->a_buflen) {
2652 usedbytes += vfs_flagstostr(hmp->hflags, extraopt,
2654 ap->a_buflen - usedbytes,
2658 *ap->a_res += usedbytes;
2662 error = vop_stdmountctl(ap);
2665 lwkt_reltoken(&hmp->fs_token);
2670 * hammer_vop_strategy { vp, bio }
2672 * Strategy call, used for regular file read & write only. Note that the
2673 * bp may represent a cluster.
2675 * To simplify operation and allow better optimizations in the future,
2676 * this code does not make any assumptions with regards to buffer alignment
2681 hammer_vop_strategy(struct vop_strategy_args *ap)
2686 bp = ap->a_bio->bio_buf;
2690 error = hammer_vop_strategy_read(ap);
2693 error = hammer_vop_strategy_write(ap);
2696 bp->b_error = error = EINVAL;
2697 bp->b_flags |= B_ERROR;
2702 /* hammer_dump_dedup_cache(((hammer_inode_t)ap->a_vp->v_data)->hmp); */
2708 * Read from a regular file. Iterate the related records and fill in the
2709 * BIO/BUF. Gaps are zero-filled.
2711 * The support code in hammer_object.c should be used to deal with mixed
2712 * in-memory and on-disk records.
2714 * NOTE: Can be called from the cluster code with an oversized buf.
2720 hammer_vop_strategy_read(struct vop_strategy_args *ap)
2722 struct hammer_transaction trans;
2723 struct hammer_inode *ip;
2724 struct hammer_inode *dip;
2726 struct hammer_cursor cursor;
2727 hammer_base_elm_t base;
2728 hammer_off_t disk_offset;
2743 ip = ap->a_vp->v_data;
2747 * The zone-2 disk offset may have been set by the cluster code via
2748 * a BMAP operation, or else should be NOOFFSET.
2750 * Checking the high bits for a match against zone-2 should suffice.
2752 * In cases where a lot of data duplication is present it may be
2753 * more beneficial to drop through and doubule-buffer through the
2756 nbio = push_bio(bio);
2757 if ((nbio->bio_offset & HAMMER_OFF_ZONE_MASK) ==
2758 HAMMER_ZONE_LARGE_DATA) {
2759 if (hammer_double_buffer == 0) {
2760 lwkt_gettoken(&hmp->fs_token);
2761 error = hammer_io_direct_read(hmp, nbio, NULL);
2762 lwkt_reltoken(&hmp->fs_token);
2767 * Try to shortcut requests for double_buffer mode too.
2768 * Since this mode runs through the device buffer cache
2769 * only compatible buffer sizes (meaning those generated
2770 * by normal filesystem buffers) are legal.
2772 if (hammer_live_dedup == 0 && (bp->b_flags & B_PAGING) == 0) {
2773 error = hammer_io_indirect_read(hmp, nbio, NULL);
2779 * Well, that sucked. Do it the hard way. If all the stars are
2780 * aligned we may still be able to issue a direct-read.
2782 lwkt_gettoken(&hmp->fs_token);
2783 hammer_simple_transaction(&trans, hmp);
2784 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
2787 * Key range (begin and end inclusive) to scan. Note that the key's
2788 * stored in the actual records represent BASE+LEN, not BASE. The
2789 * first record containing bio_offset will have a key > bio_offset.
2791 cursor.key_beg.localization = ip->obj_localization +
2792 HAMMER_LOCALIZE_MISC;
2793 cursor.key_beg.obj_id = ip->obj_id;
2794 cursor.key_beg.create_tid = 0;
2795 cursor.key_beg.delete_tid = 0;
2796 cursor.key_beg.obj_type = 0;
2797 cursor.key_beg.key = bio->bio_offset + 1;
2798 cursor.asof = ip->obj_asof;
2799 cursor.flags |= HAMMER_CURSOR_ASOF;
2801 cursor.key_end = cursor.key_beg;
2802 KKASSERT(ip->ino_data.obj_type == HAMMER_OBJTYPE_REGFILE);
2804 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_DBFILE) {
2805 cursor.key_beg.rec_type = HAMMER_RECTYPE_DB;
2806 cursor.key_end.rec_type = HAMMER_RECTYPE_DB;
2807 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
2811 ran_end = bio->bio_offset + bp->b_bufsize;
2812 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
2813 cursor.key_end.rec_type = HAMMER_RECTYPE_DATA;
2814 tmp64 = ran_end + MAXPHYS + 1; /* work-around GCC-4 bug */
2815 if (tmp64 < ran_end)
2816 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
2818 cursor.key_end.key = ran_end + MAXPHYS + 1;
2820 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
2823 * Set NOSWAPCACHE for cursor data extraction if double buffering
2824 * is disabled or (if the file is not marked cacheable via chflags
2825 * and vm.swapcache_use_chflags is enabled).
2827 if (hammer_double_buffer == 0 ||
2828 ((ap->a_vp->v_flag & VSWAPCACHE) == 0 &&
2829 vm_swapcache_use_chflags)) {
2830 cursor.flags |= HAMMER_CURSOR_NOSWAPCACHE;
2833 error = hammer_ip_first(&cursor);
2836 while (error == 0) {
2838 * Get the base file offset of the record. The key for
2839 * data records is (base + bytes) rather then (base).
2841 base = &cursor.leaf->base;
2842 rec_offset = base->key - cursor.leaf->data_len;
2845 * Calculate the gap, if any, and zero-fill it.
2847 * n is the offset of the start of the record verses our
2848 * current seek offset in the bio.
2850 n = (int)(rec_offset - (bio->bio_offset + boff));
2852 if (n > bp->b_bufsize - boff)
2853 n = bp->b_bufsize - boff;
2854 bzero((char *)bp->b_data + boff, n);
2860 * Calculate the data offset in the record and the number
2861 * of bytes we can copy.
2863 * There are two degenerate cases. First, boff may already
2864 * be at bp->b_bufsize. Secondly, the data offset within
2865 * the record may exceed the record's size.
2869 n = cursor.leaf->data_len - roff;
2871 kprintf("strategy_read: bad n=%d roff=%d\n", n, roff);
2873 } else if (n > bp->b_bufsize - boff) {
2874 n = bp->b_bufsize - boff;
2878 * Deal with cached truncations. This cool bit of code
2879 * allows truncate()/ftruncate() to avoid having to sync
2882 * If the frontend is truncated then all backend records are
2883 * subject to the frontend's truncation.
2885 * If the backend is truncated then backend records on-disk
2886 * (but not in-memory) are subject to the backend's
2887 * truncation. In-memory records owned by the backend
2888 * represent data written after the truncation point on the
2889 * backend and must not be truncated.
2891 * Truncate operations deal with frontend buffer cache
2892 * buffers and frontend-owned in-memory records synchronously.
2894 if (ip->flags & HAMMER_INODE_TRUNCATED) {
2895 if (hammer_cursor_ondisk(&cursor)/* ||
2896 cursor.iprec->flush_state == HAMMER_FST_FLUSH*/) {
2897 if (ip->trunc_off <= rec_offset)
2899 else if (ip->trunc_off < rec_offset + n)
2900 n = (int)(ip->trunc_off - rec_offset);
2903 if (ip->sync_flags & HAMMER_INODE_TRUNCATED) {
2904 if (hammer_cursor_ondisk(&cursor)) {
2905 if (ip->sync_trunc_off <= rec_offset)
2907 else if (ip->sync_trunc_off < rec_offset + n)
2908 n = (int)(ip->sync_trunc_off - rec_offset);
2913 * Try to issue a direct read into our bio if possible,
2914 * otherwise resolve the element data into a hammer_buffer
2917 * The buffer on-disk should be zerod past any real
2918 * truncation point, but may not be for any synthesized
2919 * truncation point from above.
2921 * NOTE: disk_offset is only valid if the cursor data is
2924 disk_offset = cursor.leaf->data_offset + roff;
2925 isdedupable = (boff == 0 && n == bp->b_bufsize &&
2926 hammer_cursor_ondisk(&cursor) &&
2927 ((int)disk_offset & HAMMER_BUFMASK) == 0);
2929 if (isdedupable && hammer_double_buffer == 0) {
2933 KKASSERT((disk_offset & HAMMER_OFF_ZONE_MASK) ==
2934 HAMMER_ZONE_LARGE_DATA);
2935 nbio->bio_offset = disk_offset;
2936 error = hammer_io_direct_read(hmp, nbio, cursor.leaf);
2937 if (hammer_live_dedup && error == 0)
2938 hammer_dedup_cache_add(ip, cursor.leaf);
2940 } else if (isdedupable) {
2942 * Async I/O case for reading from backing store
2943 * and copying the data to the filesystem buffer.
2944 * live-dedup has to verify the data anyway if it
2945 * gets a hit later so we can just add the entry
2948 KKASSERT((disk_offset & HAMMER_OFF_ZONE_MASK) ==
2949 HAMMER_ZONE_LARGE_DATA);
2950 nbio->bio_offset = disk_offset;
2951 if (hammer_live_dedup)
2952 hammer_dedup_cache_add(ip, cursor.leaf);
2953 error = hammer_io_indirect_read(hmp, nbio, cursor.leaf);
2956 error = hammer_ip_resolve_data(&cursor);
2958 if (hammer_live_dedup && isdedupable)
2959 hammer_dedup_cache_add(ip, cursor.leaf);
2960 bcopy((char *)cursor.data + roff,
2961 (char *)bp->b_data + boff, n);
2968 * We have to be sure that the only elements added to the
2969 * dedup cache are those which are already on-media.
2971 if (hammer_live_dedup && hammer_cursor_ondisk(&cursor))
2972 hammer_dedup_cache_add(ip, cursor.leaf);
2975 * Iterate until we have filled the request.
2978 if (boff == bp->b_bufsize)
2980 error = hammer_ip_next(&cursor);
2984 * There may have been a gap after the last record
2986 if (error == ENOENT)
2988 if (error == 0 && boff != bp->b_bufsize) {
2989 KKASSERT(boff < bp->b_bufsize);
2990 bzero((char *)bp->b_data + boff, bp->b_bufsize - boff);
2991 /* boff = bp->b_bufsize; */
2995 * Disallow swapcache operation on the vnode buffer if double
2996 * buffering is enabled, the swapcache will get the data via
2997 * the block device buffer.
2999 if (hammer_double_buffer)
3000 bp->b_flags |= B_NOTMETA;
3006 bp->b_error = error;
3008 bp->b_flags |= B_ERROR;
3013 * Cache the b-tree node for the last data read in cache[1].
3015 * If we hit the file EOF then also cache the node in the
3016 * governing director's cache[3], it will be used to initialize
3017 * the inode's cache[1] for any inodes looked up via the directory.
3019 * This doesn't reduce disk accesses since the B-Tree chain is
3020 * likely cached, but it does reduce cpu overhead when looking
3021 * up file offsets for cpdup/tar/cpio style iterations.
3024 hammer_cache_node(&ip->cache[1], cursor.node);
3025 if (ran_end >= ip->ino_data.size) {
3026 dip = hammer_find_inode(&trans, ip->ino_data.parent_obj_id,
3027 ip->obj_asof, ip->obj_localization);
3029 hammer_cache_node(&dip->cache[3], cursor.node);
3030 hammer_rel_inode(dip, 0);
3033 hammer_done_cursor(&cursor);
3034 hammer_done_transaction(&trans);
3035 lwkt_reltoken(&hmp->fs_token);
3040 * BMAP operation - used to support cluster_read() only.
3042 * (struct vnode *vp, off_t loffset, off_t *doffsetp, int *runp, int *runb)
3044 * This routine may return EOPNOTSUPP if the opration is not supported for
3045 * the specified offset. The contents of the pointer arguments do not
3046 * need to be initialized in that case.
3048 * If a disk address is available and properly aligned return 0 with
3049 * *doffsetp set to the zone-2 address, and *runp / *runb set appropriately
3050 * to the run-length relative to that offset. Callers may assume that
3051 * *doffsetp is valid if 0 is returned, even if *runp is not sufficiently
3052 * large, so return EOPNOTSUPP if it is not sufficiently large.
3056 hammer_vop_bmap(struct vop_bmap_args *ap)
3058 struct hammer_transaction trans;
3059 struct hammer_inode *ip;
3061 struct hammer_cursor cursor;
3062 hammer_base_elm_t base;
3066 int64_t base_offset;
3067 int64_t base_disk_offset;
3068 int64_t last_offset;
3069 hammer_off_t last_disk_offset;
3070 hammer_off_t disk_offset;
3075 ++hammer_stats_file_iopsr;
3076 ip = ap->a_vp->v_data;
3080 * We can only BMAP regular files. We can't BMAP database files,
3083 if (ip->ino_data.obj_type != HAMMER_OBJTYPE_REGFILE)
3087 * bmap is typically called with runp/runb both NULL when used
3088 * for writing. We do not support BMAP for writing atm.
3090 if (ap->a_cmd != BUF_CMD_READ)
3094 * Scan the B-Tree to acquire blockmap addresses, then translate
3097 lwkt_gettoken(&hmp->fs_token);
3098 hammer_simple_transaction(&trans, hmp);
3100 kprintf("bmap_beg %016llx ip->cache %p\n",
3101 (long long)ap->a_loffset, ip->cache[1]);
3103 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
3106 * Key range (begin and end inclusive) to scan. Note that the key's
3107 * stored in the actual records represent BASE+LEN, not BASE. The
3108 * first record containing bio_offset will have a key > bio_offset.
3110 cursor.key_beg.localization = ip->obj_localization +
3111 HAMMER_LOCALIZE_MISC;
3112 cursor.key_beg.obj_id = ip->obj_id;
3113 cursor.key_beg.create_tid = 0;
3114 cursor.key_beg.delete_tid = 0;
3115 cursor.key_beg.obj_type = 0;
3117 cursor.key_beg.key = ap->a_loffset - MAXPHYS + 1;
3119 cursor.key_beg.key = ap->a_loffset + 1;
3120 if (cursor.key_beg.key < 0)
3121 cursor.key_beg.key = 0;
3122 cursor.asof = ip->obj_asof;
3123 cursor.flags |= HAMMER_CURSOR_ASOF;
3125 cursor.key_end = cursor.key_beg;
3126 KKASSERT(ip->ino_data.obj_type == HAMMER_OBJTYPE_REGFILE);
3128 ran_end = ap->a_loffset + MAXPHYS;
3129 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
3130 cursor.key_end.rec_type = HAMMER_RECTYPE_DATA;
3131 tmp64 = ran_end + MAXPHYS + 1; /* work-around GCC-4 bug */
3132 if (tmp64 < ran_end)
3133 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
3135 cursor.key_end.key = ran_end + MAXPHYS + 1;
3137 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
3139 error = hammer_ip_first(&cursor);
3140 base_offset = last_offset = 0;
3141 base_disk_offset = last_disk_offset = 0;
3143 while (error == 0) {
3145 * Get the base file offset of the record. The key for
3146 * data records is (base + bytes) rather then (base).
3148 * NOTE: rec_offset + rec_len may exceed the end-of-file.
3149 * The extra bytes should be zero on-disk and the BMAP op
3150 * should still be ok.
3152 base = &cursor.leaf->base;
3153 rec_offset = base->key - cursor.leaf->data_len;
3154 rec_len = cursor.leaf->data_len;
3157 * Incorporate any cached truncation.
3159 * NOTE: Modifications to rec_len based on synthesized
3160 * truncation points remove the guarantee that any extended
3161 * data on disk is zero (since the truncations may not have
3162 * taken place on-media yet).
3164 if (ip->flags & HAMMER_INODE_TRUNCATED) {
3165 if (hammer_cursor_ondisk(&cursor) ||
3166 cursor.iprec->flush_state == HAMMER_FST_FLUSH) {
3167 if (ip->trunc_off <= rec_offset)
3169 else if (ip->trunc_off < rec_offset + rec_len)
3170 rec_len = (int)(ip->trunc_off - rec_offset);
3173 if (ip->sync_flags & HAMMER_INODE_TRUNCATED) {
3174 if (hammer_cursor_ondisk(&cursor)) {
3175 if (ip->sync_trunc_off <= rec_offset)
3177 else if (ip->sync_trunc_off < rec_offset + rec_len)
3178 rec_len = (int)(ip->sync_trunc_off - rec_offset);
3183 * Accumulate information. If we have hit a discontiguous
3184 * block reset base_offset unless we are already beyond the
3185 * requested offset. If we are, that's it, we stop.
3189 if (hammer_cursor_ondisk(&cursor)) {
3190 disk_offset = cursor.leaf->data_offset;
3191 if (rec_offset != last_offset ||
3192 disk_offset != last_disk_offset) {
3193 if (rec_offset > ap->a_loffset)
3195 base_offset = rec_offset;
3196 base_disk_offset = disk_offset;
3198 last_offset = rec_offset + rec_len;
3199 last_disk_offset = disk_offset + rec_len;
3201 if (hammer_live_dedup)
3202 hammer_dedup_cache_add(ip, cursor.leaf);
3205 error = hammer_ip_next(&cursor);
3209 kprintf("BMAP %016llx: %016llx - %016llx\n",
3210 (long long)ap->a_loffset,
3211 (long long)base_offset,
3212 (long long)last_offset);
3213 kprintf("BMAP %16s: %016llx - %016llx\n", "",
3214 (long long)base_disk_offset,
3215 (long long)last_disk_offset);
3219 hammer_cache_node(&ip->cache[1], cursor.node);
3221 kprintf("bmap_end2 %016llx ip->cache %p\n",
3222 (long long)ap->a_loffset, ip->cache[1]);
3225 hammer_done_cursor(&cursor);
3226 hammer_done_transaction(&trans);
3227 lwkt_reltoken(&hmp->fs_token);
3230 * If we couldn't find any records or the records we did find were
3231 * all behind the requested offset, return failure. A forward
3232 * truncation can leave a hole w/ no on-disk records.
3234 if (last_offset == 0 || last_offset < ap->a_loffset)
3235 return (EOPNOTSUPP);
3238 * Figure out the block size at the requested offset and adjust
3239 * our limits so the cluster_read() does not create inappropriately
3240 * sized buffer cache buffers.
3242 blksize = hammer_blocksize(ap->a_loffset);
3243 if (hammer_blocksize(base_offset) != blksize) {
3244 base_offset = hammer_blockdemarc(base_offset, ap->a_loffset);
3246 if (last_offset != ap->a_loffset &&
3247 hammer_blocksize(last_offset - 1) != blksize) {
3248 last_offset = hammer_blockdemarc(ap->a_loffset,
3253 * Returning EOPNOTSUPP simply prevents the direct-IO optimization
3256 disk_offset = base_disk_offset + (ap->a_loffset - base_offset);
3258 if ((disk_offset & HAMMER_OFF_ZONE_MASK) != HAMMER_ZONE_LARGE_DATA) {
3260 * Only large-data zones can be direct-IOd
3263 } else if ((disk_offset & HAMMER_BUFMASK) ||
3264 (last_offset - ap->a_loffset) < blksize) {
3266 * doffsetp is not aligned or the forward run size does
3267 * not cover a whole buffer, disallow the direct I/O.
3274 *ap->a_doffsetp = disk_offset;
3276 *ap->a_runb = ap->a_loffset - base_offset;
3277 KKASSERT(*ap->a_runb >= 0);
3280 *ap->a_runp = last_offset - ap->a_loffset;
3281 KKASSERT(*ap->a_runp >= 0);
3289 * Write to a regular file. Because this is a strategy call the OS is
3290 * trying to actually get data onto the media.
3294 hammer_vop_strategy_write(struct vop_strategy_args *ap)
3296 hammer_record_t record;
3307 ip = ap->a_vp->v_data;
3310 blksize = hammer_blocksize(bio->bio_offset);
3311 KKASSERT(bp->b_bufsize == blksize);
3313 if (ip->flags & HAMMER_INODE_RO) {
3314 bp->b_error = EROFS;
3315 bp->b_flags |= B_ERROR;
3320 lwkt_gettoken(&hmp->fs_token);
3323 * Disallow swapcache operation on the vnode buffer if double
3324 * buffering is enabled, the swapcache will get the data via
3325 * the block device buffer.
3327 if (hammer_double_buffer)
3328 bp->b_flags |= B_NOTMETA;
3331 * Interlock with inode destruction (no in-kernel or directory
3332 * topology visibility). If we queue new IO while trying to
3333 * destroy the inode we can deadlock the vtrunc call in
3334 * hammer_inode_unloadable_check().
3336 * Besides, there's no point flushing a bp associated with an
3337 * inode that is being destroyed on-media and has no kernel
3340 if ((ip->flags | ip->sync_flags) &
3341 (HAMMER_INODE_DELETING|HAMMER_INODE_DELETED)) {
3344 lwkt_reltoken(&hmp->fs_token);
3349 * Reserve space and issue a direct-write from the front-end.
3350 * NOTE: The direct_io code will hammer_bread/bcopy smaller
3353 * An in-memory record will be installed to reference the storage
3354 * until the flusher can get to it.
3356 * Since we own the high level bio the front-end will not try to
3357 * do a direct-read until the write completes.
3359 * NOTE: The only time we do not reserve a full-sized buffers
3360 * worth of data is if the file is small. We do not try to
3361 * allocate a fragment (from the small-data zone) at the end of
3362 * an otherwise large file as this can lead to wildly separated
3365 KKASSERT((bio->bio_offset & HAMMER_BUFMASK) == 0);
3366 KKASSERT(bio->bio_offset < ip->ino_data.size);
3367 if (bio->bio_offset || ip->ino_data.size > HAMMER_BUFSIZE / 2)
3368 bytes = bp->b_bufsize;
3370 bytes = ((int)ip->ino_data.size + 15) & ~15;
3372 record = hammer_ip_add_bulk(ip, bio->bio_offset, bp->b_data,
3376 * B_VFSFLAG1 indicates that a REDO_WRITE entry was generated
3377 * in hammer_vop_write(). We must flag the record so the proper
3378 * REDO_TERM_WRITE entry is generated during the flush.
3381 if (bp->b_flags & B_VFSFLAG1) {
3382 record->flags |= HAMMER_RECF_REDO;
3383 bp->b_flags &= ~B_VFSFLAG1;
3385 if (record->flags & HAMMER_RECF_DEDUPED) {
3387 hammer_ip_replace_bulk(hmp, record);
3390 hammer_io_direct_write(hmp, bio, record);
3392 if (ip->rsv_recs > 1 && hmp->rsv_recs > hammer_limit_recs)
3393 hammer_flush_inode(ip, 0);
3395 bp->b_bio2.bio_offset = NOOFFSET;
3396 bp->b_error = error;
3397 bp->b_flags |= B_ERROR;
3400 lwkt_reltoken(&hmp->fs_token);
3405 * dounlink - disconnect a directory entry
3407 * XXX whiteout support not really in yet
3410 hammer_dounlink(hammer_transaction_t trans, struct nchandle *nch,
3411 struct vnode *dvp, struct ucred *cred,
3412 int flags, int isdir)
3414 struct namecache *ncp;
3418 struct hammer_cursor cursor;
3420 u_int32_t max_iterations;
3424 * Calculate the namekey and setup the key range for the scan. This
3425 * works kinda like a chained hash table where the lower 32 bits
3426 * of the namekey synthesize the chain.
3428 * The key range is inclusive of both key_beg and key_end.
3434 if (dip->flags & HAMMER_INODE_RO)
3437 namekey = hammer_directory_namekey(dip, ncp->nc_name, ncp->nc_nlen,
3440 hammer_init_cursor(trans, &cursor, &dip->cache[1], dip);
3441 cursor.key_beg.localization = dip->obj_localization +
3442 hammer_dir_localization(dip);
3443 cursor.key_beg.obj_id = dip->obj_id;
3444 cursor.key_beg.key = namekey;
3445 cursor.key_beg.create_tid = 0;
3446 cursor.key_beg.delete_tid = 0;
3447 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
3448 cursor.key_beg.obj_type = 0;
3450 cursor.key_end = cursor.key_beg;
3451 cursor.key_end.key += max_iterations;
3452 cursor.asof = dip->obj_asof;
3453 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
3456 * Scan all matching records (the chain), locate the one matching
3457 * the requested path component. info->last_error contains the
3458 * error code on search termination and could be 0, ENOENT, or
3461 * The hammer_ip_*() functions merge in-memory records with on-disk
3462 * records for the purposes of the search.
3464 error = hammer_ip_first(&cursor);
3466 while (error == 0) {
3467 error = hammer_ip_resolve_data(&cursor);
3470 nlen = cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF;
3472 if (ncp->nc_nlen == nlen &&
3473 bcmp(ncp->nc_name, cursor.data->entry.name, nlen) == 0) {
3476 error = hammer_ip_next(&cursor);
3480 * If all is ok we have to get the inode so we can adjust nlinks.
3481 * To avoid a deadlock with the flusher we must release the inode
3482 * lock on the directory when acquiring the inode for the entry.
3484 * If the target is a directory, it must be empty.
3487 hammer_unlock(&cursor.ip->lock);
3488 ip = hammer_get_inode(trans, dip, cursor.data->entry.obj_id,
3490 cursor.data->entry.localization,
3492 hammer_lock_sh(&cursor.ip->lock);
3493 if (error == ENOENT) {
3494 kprintf("HAMMER: WARNING: Removing "
3495 "dirent w/missing inode \"%s\"\n"
3496 "\tobj_id = %016llx\n",
3498 (long long)cursor.data->entry.obj_id);
3503 * If isdir >= 0 we validate that the entry is or is not a
3504 * directory. If isdir < 0 we don't care.
3506 if (error == 0 && isdir >= 0 && ip) {
3508 ip->ino_data.obj_type != HAMMER_OBJTYPE_DIRECTORY) {
3510 } else if (isdir == 0 &&
3511 ip->ino_data.obj_type == HAMMER_OBJTYPE_DIRECTORY) {
3517 * If we are trying to remove a directory the directory must
3520 * The check directory code can loop and deadlock/retry. Our
3521 * own cursor's node locks must be released to avoid a 3-way
3522 * deadlock with the flusher if the check directory code
3525 * If any changes whatsoever have been made to the cursor
3526 * set EDEADLK and retry.
3528 * WARNING: See warnings in hammer_unlock_cursor()
3531 if (error == 0 && ip && ip->ino_data.obj_type ==
3532 HAMMER_OBJTYPE_DIRECTORY) {
3533 hammer_unlock_cursor(&cursor);
3534 error = hammer_ip_check_directory_empty(trans, ip);
3535 hammer_lock_cursor(&cursor);
3536 if (cursor.flags & HAMMER_CURSOR_RETEST) {
3537 kprintf("HAMMER: Warning: avoided deadlock "
3545 * Delete the directory entry.
3547 * WARNING: hammer_ip_del_directory() may have to terminate
3548 * the cursor to avoid a deadlock. It is ok to call
3549 * hammer_done_cursor() twice.
3552 error = hammer_ip_del_directory(trans, &cursor,
3555 hammer_done_cursor(&cursor);
3558 * Tell the namecache that we are now unlinked.
3563 * NOTE: ip->vp, if non-NULL, cannot be directly
3564 * referenced without formally acquiring the
3565 * vp since the vp might have zero refs on it,
3566 * or in the middle of a reclaim, etc.
3568 * NOTE: The cache_setunresolved() can rip the vp
3569 * out from under us since the vp may not have
3570 * any refs, in which case ip->vp will be NULL
3573 while (ip && ip->vp) {
3576 error = hammer_get_vnode(ip, &vp);
3577 if (error == 0 && vp) {
3579 hammer_knote(ip->vp, NOTE_DELETE);
3580 cache_inval_vp(ip->vp, CINV_DESTROY);
3584 kprintf("Debug: HAMMER ip/vp race1 avoided\n");
3588 hammer_rel_inode(ip, 0);
3590 hammer_done_cursor(&cursor);
3592 if (error == EDEADLK)
3598 /************************************************************************
3599 * FIFO AND SPECFS OPS *
3600 ************************************************************************
3604 hammer_vop_fifoclose (struct vop_close_args *ap)
3606 /* XXX update itimes */
3607 return (VOCALL(&fifo_vnode_vops, &ap->a_head));
3611 hammer_vop_fiforead (struct vop_read_args *ap)
3615 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
3616 /* XXX update access time */
3621 hammer_vop_fifowrite (struct vop_write_args *ap)
3625 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
3626 /* XXX update access time */
3632 hammer_vop_fifokqfilter(struct vop_kqfilter_args *ap)
3636 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
3638 error = hammer_vop_kqfilter(ap);
3642 /************************************************************************
3644 ************************************************************************
3647 static void filt_hammerdetach(struct knote *kn);
3648 static int filt_hammerread(struct knote *kn, long hint);
3649 static int filt_hammerwrite(struct knote *kn, long hint);
3650 static int filt_hammervnode(struct knote *kn, long hint);
3652 static struct filterops hammerread_filtops =
3653 { FILTEROP_ISFD, NULL, filt_hammerdetach, filt_hammerread };
3654 static struct filterops hammerwrite_filtops =
3655 { FILTEROP_ISFD, NULL, filt_hammerdetach, filt_hammerwrite };
3656 static struct filterops hammervnode_filtops =
3657 { FILTEROP_ISFD, NULL, filt_hammerdetach, filt_hammervnode };
3661 hammer_vop_kqfilter(struct vop_kqfilter_args *ap)
3663 struct vnode *vp = ap->a_vp;
3664 struct knote *kn = ap->a_kn;
3666 switch (kn->kn_filter) {
3668 kn->kn_fop = &hammerread_filtops;
3671 kn->kn_fop = &hammerwrite_filtops;
3674 kn->kn_fop = &hammervnode_filtops;
3677 return (EOPNOTSUPP);
3680 kn->kn_hook = (caddr_t)vp;
3682 knote_insert(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
3688 filt_hammerdetach(struct knote *kn)
3690 struct vnode *vp = (void *)kn->kn_hook;
3692 knote_remove(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
3696 filt_hammerread(struct knote *kn, long hint)
3698 struct vnode *vp = (void *)kn->kn_hook;
3699 hammer_inode_t ip = VTOI(vp);
3700 hammer_mount_t hmp = ip->hmp;
3703 if (hint == NOTE_REVOKE) {
3704 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
3707 lwkt_gettoken(&hmp->fs_token); /* XXX use per-ip-token */
3708 off = ip->ino_data.size - kn->kn_fp->f_offset;
3709 kn->kn_data = (off < INTPTR_MAX) ? off : INTPTR_MAX;
3710 lwkt_reltoken(&hmp->fs_token);
3711 if (kn->kn_sfflags & NOTE_OLDAPI)
3713 return (kn->kn_data != 0);
3717 filt_hammerwrite(struct knote *kn, long hint)
3719 if (hint == NOTE_REVOKE)
3720 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
3726 filt_hammervnode(struct knote *kn, long hint)
3728 if (kn->kn_sfflags & hint)
3729 kn->kn_fflags |= hint;
3730 if (hint == NOTE_REVOKE) {
3731 kn->kn_flags |= (EV_EOF | EV_NODATA);
3734 return (kn->kn_fflags != 0);