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;
333 if (ap->a_vp->v_type != VREG)
341 * Allow the UIO's size to override the sequential heuristic.
343 blksize = hammer_blocksize(uio->uio_offset);
344 seqcount = (uio->uio_resid + (BKVASIZE - 1)) / BKVASIZE;
345 ioseqcount = (ap->a_ioflag >> 16);
346 if (seqcount < ioseqcount)
347 seqcount = ioseqcount;
350 * If reading or writing a huge amount of data we have to break
351 * atomicy and allow the operation to be interrupted by a signal
352 * or it can DOS the machine.
354 bigread = (uio->uio_resid > 100 * 1024 * 1024);
358 * Access the data typically in HAMMER_BUFSIZE blocks via the
359 * buffer cache, but HAMMER may use a variable block size based
362 * XXX Temporary hack, delay the start transaction while we remain
363 * MPSAFE. NOTE: ino_data.size cannot change while vnode is
366 while (uio->uio_resid > 0 && uio->uio_offset < ip->ino_data.size) {
370 blksize = hammer_blocksize(uio->uio_offset);
371 offset = (int)uio->uio_offset & (blksize - 1);
372 base_offset = uio->uio_offset - offset;
374 if (bigread && (error = hammer_signal_check(ip->hmp)) != 0)
380 bp = getblk(ap->a_vp, base_offset, blksize, 0, 0);
381 if ((bp->b_flags & (B_INVAL | B_CACHE | B_RAM)) == B_CACHE) {
382 bp->b_flags &= ~B_AGE;
386 if (ap->a_ioflag & IO_NRDELAY) {
388 return (EWOULDBLOCK);
394 if (got_fstoken == 0) {
395 lwkt_gettoken(&hmp->fs_token);
397 hammer_start_transaction(&trans, ip->hmp);
401 * NOTE: A valid bp has already been acquired, but was not
404 if (hammer_cluster_enable) {
406 * Use file_limit to prevent cluster_read() from
407 * creating buffers of the wrong block size past
410 file_limit = ip->ino_data.size;
411 if (base_offset < HAMMER_XDEMARC &&
412 file_limit > HAMMER_XDEMARC) {
413 file_limit = HAMMER_XDEMARC;
415 error = cluster_readx(ap->a_vp,
416 file_limit, base_offset,
417 blksize, uio->uio_resid,
418 seqcount * BKVASIZE, &bp);
420 error = breadnx(ap->a_vp, base_offset, blksize,
428 if ((hammer_debug_io & 0x0001) && (bp->b_flags & B_IODEBUG)) {
429 kprintf("doff %016jx read file %016jx@%016jx\n",
430 (intmax_t)bp->b_bio2.bio_offset,
431 (intmax_t)ip->obj_id,
432 (intmax_t)bp->b_loffset);
434 bp->b_flags &= ~B_IODEBUG;
436 bp->b_flags |= B_CLUSTEROK;
437 n = blksize - offset;
438 if (n > uio->uio_resid)
440 if (n > ip->ino_data.size - uio->uio_offset)
441 n = (int)(ip->ino_data.size - uio->uio_offset);
443 lwkt_reltoken(&hmp->fs_token);
446 * Set B_AGE, data has a lower priority than meta-data.
448 * Use a hold/unlock/drop sequence to run the uiomove
449 * with the buffer unlocked, avoiding deadlocks against
450 * read()s on mmap()'d spaces.
452 bp->b_flags |= B_AGE;
455 error = uiomove((char *)bp->b_data + offset, n, uio);
459 lwkt_gettoken(&hmp->fs_token);
463 hammer_stats_file_read += n;
467 * Try to update the atime with just the inode lock for maximum
468 * concurrency. If we can't shortcut it we have to get the full
471 if (got_fstoken == 0 && hammer_update_atime_quick(ip) < 0) {
472 lwkt_gettoken(&hmp->fs_token);
474 hammer_start_transaction(&trans, ip->hmp);
478 if ((ip->flags & HAMMER_INODE_RO) == 0 &&
479 (ip->hmp->mp->mnt_flag & MNT_NOATIME) == 0) {
480 ip->ino_data.atime = trans.time;
481 hammer_modify_inode(&trans, ip, HAMMER_INODE_ATIME);
483 hammer_done_transaction(&trans);
484 lwkt_reltoken(&hmp->fs_token);
490 * hammer_vop_write { vp, uio, ioflag, cred }
494 hammer_vop_write(struct vop_write_args *ap)
496 struct hammer_transaction trans;
497 struct hammer_inode *ip;
512 if (ap->a_vp->v_type != VREG)
518 seqcount = ap->a_ioflag >> 16;
520 if (ip->flags & HAMMER_INODE_RO)
524 * Create a transaction to cover the operations we perform.
526 lwkt_gettoken(&hmp->fs_token);
527 hammer_start_transaction(&trans, hmp);
533 if (ap->a_ioflag & IO_APPEND)
534 uio->uio_offset = ip->ino_data.size;
537 * Check for illegal write offsets. Valid range is 0...2^63-1.
539 * NOTE: the base_off assignment is required to work around what
540 * I consider to be a GCC-4 optimization bug.
542 if (uio->uio_offset < 0) {
543 hammer_done_transaction(&trans);
544 lwkt_reltoken(&hmp->fs_token);
547 base_offset = uio->uio_offset + uio->uio_resid; /* work around gcc-4 */
548 if (uio->uio_resid > 0 && base_offset <= uio->uio_offset) {
549 hammer_done_transaction(&trans);
550 lwkt_reltoken(&hmp->fs_token);
554 if (uio->uio_resid > 0 && (td = uio->uio_td) != NULL && td->td_proc &&
555 base_offset > td->td_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
556 hammer_done_transaction(&trans);
557 lwkt_reltoken(&hmp->fs_token);
558 lwpsignal(td->td_proc, td->td_lwp, SIGXFSZ);
563 * If reading or writing a huge amount of data we have to break
564 * atomicy and allow the operation to be interrupted by a signal
565 * or it can DOS the machine.
567 * Preset redo_count so we stop generating REDOs earlier if the
570 bigwrite = (uio->uio_resid > 100 * 1024 * 1024);
571 if ((ip->flags & HAMMER_INODE_REDO) &&
572 ip->redo_count < hammer_limit_redo) {
573 ip->redo_count += uio->uio_resid;
577 * Access the data typically in HAMMER_BUFSIZE blocks via the
578 * buffer cache, but HAMMER may use a variable block size based
581 while (uio->uio_resid > 0) {
589 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_WRITE)) != 0)
591 if (bigwrite && (error = hammer_signal_check(hmp)) != 0)
594 blksize = hammer_blocksize(uio->uio_offset);
597 * Do not allow HAMMER to blow out the buffer cache. Very
598 * large UIOs can lockout other processes due to bwillwrite()
601 * The hammer inode is not locked during these operations.
602 * The vnode is locked which can interfere with the pageout
603 * daemon for non-UIO_NOCOPY writes but should not interfere
604 * with the buffer cache. Even so, we cannot afford to
605 * allow the pageout daemon to build up too many dirty buffer
608 * Only call this if we aren't being recursively called from
609 * a virtual disk device (vn), else we may deadlock.
611 if ((ap->a_ioflag & IO_RECURSE) == 0)
615 * Control the number of pending records associated with
616 * this inode. If too many have accumulated start a
617 * flush. Try to maintain a pipeline with the flusher.
619 * NOTE: It is possible for other sources to grow the
620 * records but not necessarily issue another flush,
621 * so use a timeout and ensure that a re-flush occurs.
623 if (ip->rsv_recs >= hammer_limit_inode_recs) {
624 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
625 while (ip->rsv_recs >= hammer_limit_inode_recs * 2) {
626 ip->flags |= HAMMER_INODE_RECSW;
627 tsleep(&ip->rsv_recs, 0, "hmrwww", hz);
628 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
634 * Do not allow HAMMER to blow out system memory by
635 * accumulating too many records. Records are so well
636 * decoupled from the buffer cache that it is possible
637 * for userland to push data out to the media via
638 * direct-write, but build up the records queued to the
639 * backend faster then the backend can flush them out.
640 * HAMMER has hit its write limit but the frontend has
641 * no pushback to slow it down.
643 if (hmp->rsv_recs > hammer_limit_recs / 2) {
645 * Get the inode on the flush list
647 if (ip->rsv_recs >= 64)
648 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
649 else if (ip->rsv_recs >= 16)
650 hammer_flush_inode(ip, 0);
653 * Keep the flusher going if the system keeps
656 delta = hmp->count_newrecords -
657 hmp->last_newrecords;
658 if (delta < 0 || delta > hammer_limit_recs / 2) {
659 hmp->last_newrecords = hmp->count_newrecords;
660 hammer_sync_hmp(hmp, MNT_NOWAIT);
664 * If we have gotten behind start slowing
667 delta = (hmp->rsv_recs - hammer_limit_recs) *
668 hz / hammer_limit_recs;
670 tsleep(&trans, 0, "hmrslo", delta);
675 * Calculate the blocksize at the current offset and figure
676 * out how much we can actually write.
678 blkmask = blksize - 1;
679 offset = (int)uio->uio_offset & blkmask;
680 base_offset = uio->uio_offset & ~(int64_t)blkmask;
681 n = blksize - offset;
682 if (n > uio->uio_resid) {
688 nsize = uio->uio_offset + n;
689 if (nsize > ip->ino_data.size) {
690 if (uio->uio_offset > ip->ino_data.size)
694 nvextendbuf(ap->a_vp,
697 hammer_blocksize(ip->ino_data.size),
698 hammer_blocksize(nsize),
699 hammer_blockoff(ip->ino_data.size),
700 hammer_blockoff(nsize),
703 kflags |= NOTE_EXTEND;
706 if (uio->uio_segflg == UIO_NOCOPY) {
708 * Issuing a write with the same data backing the
709 * buffer. Instantiate the buffer to collect the
710 * backing vm pages, then read-in any missing bits.
712 * This case is used by vop_stdputpages().
714 bp = getblk(ap->a_vp, base_offset,
715 blksize, GETBLK_BHEAVY, 0);
716 if ((bp->b_flags & B_CACHE) == 0) {
718 error = bread(ap->a_vp, base_offset,
721 } else if (offset == 0 && uio->uio_resid >= blksize) {
723 * Even though we are entirely overwriting the buffer
724 * we may still have to zero it out to avoid a
725 * mmap/write visibility issue.
727 bp = getblk(ap->a_vp, base_offset, blksize, GETBLK_BHEAVY, 0);
728 if ((bp->b_flags & B_CACHE) == 0)
730 } else if (base_offset >= ip->ino_data.size) {
732 * If the base offset of the buffer is beyond the
733 * file EOF, we don't have to issue a read.
735 bp = getblk(ap->a_vp, base_offset,
736 blksize, GETBLK_BHEAVY, 0);
740 * Partial overwrite, read in any missing bits then
741 * replace the portion being written.
743 error = bread(ap->a_vp, base_offset, blksize, &bp);
748 lwkt_reltoken(&hmp->fs_token);
749 error = uiomove(bp->b_data + offset, n, uio);
750 lwkt_gettoken(&hmp->fs_token);
754 * Generate REDO records if enabled and redo_count will not
755 * exceeded the limit.
757 * If redo_count exceeds the limit we stop generating records
758 * and clear HAMMER_INODE_REDO. This will cause the next
759 * fsync() to do a full meta-data sync instead of just an
760 * UNDO/REDO fifo update.
762 * When clearing HAMMER_INODE_REDO any pre-existing REDOs
763 * will still be tracked. The tracks will be terminated
764 * when the related meta-data (including possible data
765 * modifications which are not tracked via REDO) is
768 if ((ip->flags & HAMMER_INODE_REDO) && error == 0) {
769 if (ip->redo_count < hammer_limit_redo) {
770 bp->b_flags |= B_VFSFLAG1;
771 error = hammer_generate_redo(&trans, ip,
772 base_offset + offset,
777 ip->flags &= ~HAMMER_INODE_REDO;
782 * If we screwed up we have to undo any VM size changes we
788 nvtruncbuf(ap->a_vp, ip->ino_data.size,
789 hammer_blocksize(ip->ino_data.size),
790 hammer_blockoff(ip->ino_data.size));
794 kflags |= NOTE_WRITE;
795 hammer_stats_file_write += n;
796 bp->b_flags |= B_CLUSTEROK;
797 if (ip->ino_data.size < uio->uio_offset) {
798 ip->ino_data.size = uio->uio_offset;
799 flags = HAMMER_INODE_SDIRTY;
803 ip->ino_data.mtime = trans.time;
804 flags |= HAMMER_INODE_MTIME | HAMMER_INODE_BUFS;
805 hammer_modify_inode(&trans, ip, flags);
808 * Once we dirty the buffer any cached zone-X offset
809 * becomes invalid. HAMMER NOTE: no-history mode cannot
810 * allow overwriting over the same data sector unless
811 * we provide UNDOs for the old data, which we don't.
813 bp->b_bio2.bio_offset = NOOFFSET;
816 * Final buffer disposition.
818 * Because meta-data updates are deferred, HAMMER is
819 * especially sensitive to excessive bdwrite()s because
820 * the I/O stream is not broken up by disk reads. So the
821 * buffer cache simply cannot keep up.
823 * WARNING! blksize is variable. cluster_write() is
824 * expected to not blow up if it encounters
825 * buffers that do not match the passed blksize.
827 * NOTE! Hammer shouldn't need to bawrite()/cluster_write().
828 * The ip->rsv_recs check should burst-flush the data.
829 * If we queue it immediately the buf could be left
830 * locked on the device queue for a very long time.
832 * However, failing to flush a dirty buffer out when
833 * issued from the pageout daemon can result in a low
834 * memory deadlock against bio_page_alloc(), so we
835 * have to bawrite() on IO_ASYNC as well.
837 * NOTE! To avoid degenerate stalls due to mismatched block
838 * sizes we only honor IO_DIRECT on the write which
839 * abuts the end of the buffer. However, we must
840 * honor IO_SYNC in case someone is silly enough to
841 * configure a HAMMER file as swap, or when HAMMER
842 * is serving NFS (for commits). Ick ick.
844 bp->b_flags |= B_AGE | B_CLUSTEROK;
845 if (ap->a_ioflag & IO_SYNC) {
847 } else if ((ap->a_ioflag & IO_DIRECT) && endofblk) {
849 } else if (ap->a_ioflag & IO_ASYNC) {
851 } else if (hammer_cluster_enable &&
852 !(ap->a_vp->v_mount->mnt_flag & MNT_NOCLUSTERW)) {
853 if (base_offset < HAMMER_XDEMARC)
854 cluster_eof = hammer_blockdemarc(base_offset,
857 cluster_eof = ip->ino_data.size;
858 cluster_write(bp, cluster_eof, blksize, seqcount);
863 hammer_done_transaction(&trans);
864 hammer_knote(ap->a_vp, kflags);
865 lwkt_reltoken(&hmp->fs_token);
870 * hammer_vop_access { vp, mode, cred }
872 * MPSAFE - does not require fs_token
876 hammer_vop_access(struct vop_access_args *ap)
878 struct hammer_inode *ip = VTOI(ap->a_vp);
883 ++hammer_stats_file_iopsr;
884 uid = hammer_to_unix_xid(&ip->ino_data.uid);
885 gid = hammer_to_unix_xid(&ip->ino_data.gid);
887 error = vop_helper_access(ap, uid, gid, ip->ino_data.mode,
888 ip->ino_data.uflags);
893 * hammer_vop_advlock { vp, id, op, fl, flags }
895 * MPSAFE - does not require fs_token
899 hammer_vop_advlock(struct vop_advlock_args *ap)
901 hammer_inode_t ip = VTOI(ap->a_vp);
903 return (lf_advlock(ap, &ip->advlock, ip->ino_data.size));
907 * hammer_vop_close { vp, fflag }
909 * We can only sync-on-close for normal closes. XXX disabled for now.
913 hammer_vop_close(struct vop_close_args *ap)
916 struct vnode *vp = ap->a_vp;
917 hammer_inode_t ip = VTOI(vp);
919 if (ip->flags & (HAMMER_INODE_CLOSESYNC|HAMMER_INODE_CLOSEASYNC)) {
920 if (vn_islocked(vp) == LK_EXCLUSIVE &&
921 (vp->v_flag & (VINACTIVE|VRECLAIMED)) == 0) {
922 if (ip->flags & HAMMER_INODE_CLOSESYNC)
925 waitfor = MNT_NOWAIT;
926 ip->flags &= ~(HAMMER_INODE_CLOSESYNC |
927 HAMMER_INODE_CLOSEASYNC);
928 VOP_FSYNC(vp, MNT_NOWAIT, waitfor);
932 return (vop_stdclose(ap));
936 * hammer_vop_ncreate { nch, dvp, vpp, cred, vap }
938 * The operating system has already ensured that the directory entry
939 * does not exist and done all appropriate namespace locking.
943 hammer_vop_ncreate(struct vop_ncreate_args *ap)
945 struct hammer_transaction trans;
946 struct hammer_inode *dip;
947 struct hammer_inode *nip;
948 struct nchandle *nch;
953 dip = VTOI(ap->a_dvp);
956 if (dip->flags & HAMMER_INODE_RO)
958 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0)
962 * Create a transaction to cover the operations we perform.
964 lwkt_gettoken(&hmp->fs_token);
965 hammer_start_transaction(&trans, hmp);
966 ++hammer_stats_file_iopsw;
969 * Create a new filesystem object of the requested type. The
970 * returned inode will be referenced and shared-locked to prevent
971 * it from being moved to the flusher.
973 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
974 dip, nch->ncp->nc_name, nch->ncp->nc_nlen,
977 hkprintf("hammer_create_inode error %d\n", error);
978 hammer_done_transaction(&trans);
980 lwkt_reltoken(&hmp->fs_token);
985 * Add the new filesystem object to the directory. This will also
986 * bump the inode's link count.
988 error = hammer_ip_add_directory(&trans, dip,
989 nch->ncp->nc_name, nch->ncp->nc_nlen,
992 hkprintf("hammer_ip_add_directory error %d\n", error);
998 hammer_rel_inode(nip, 0);
999 hammer_done_transaction(&trans);
1002 error = hammer_get_vnode(nip, ap->a_vpp);
1003 hammer_done_transaction(&trans);
1004 hammer_rel_inode(nip, 0);
1006 cache_setunresolved(ap->a_nch);
1007 cache_setvp(ap->a_nch, *ap->a_vpp);
1009 hammer_knote(ap->a_dvp, NOTE_WRITE);
1011 lwkt_reltoken(&hmp->fs_token);
1016 * hammer_vop_getattr { vp, vap }
1018 * Retrieve an inode's attribute information. When accessing inodes
1019 * historically we fake the atime field to ensure consistent results.
1020 * The atime field is stored in the B-Tree element and allowed to be
1021 * updated without cycling the element.
1023 * MPSAFE - does not require fs_token
1027 hammer_vop_getattr(struct vop_getattr_args *ap)
1029 struct hammer_inode *ip = VTOI(ap->a_vp);
1030 struct vattr *vap = ap->a_vap;
1033 * We want the fsid to be different when accessing a filesystem
1034 * with different as-of's so programs like diff don't think
1035 * the files are the same.
1037 * We also want the fsid to be the same when comparing snapshots,
1038 * or when comparing mirrors (which might be backed by different
1039 * physical devices). HAMMER fsids are based on the PFS's
1040 * shared_uuid field.
1042 * XXX there is a chance of collision here. The va_fsid reported
1043 * by stat is different from the more involved fsid used in the
1046 ++hammer_stats_file_iopsr;
1047 hammer_lock_sh(&ip->lock);
1048 vap->va_fsid = ip->pfsm->fsid_udev ^ (u_int32_t)ip->obj_asof ^
1049 (u_int32_t)(ip->obj_asof >> 32);
1051 vap->va_fileid = ip->ino_leaf.base.obj_id;
1052 vap->va_mode = ip->ino_data.mode;
1053 vap->va_nlink = ip->ino_data.nlinks;
1054 vap->va_uid = hammer_to_unix_xid(&ip->ino_data.uid);
1055 vap->va_gid = hammer_to_unix_xid(&ip->ino_data.gid);
1058 vap->va_size = ip->ino_data.size;
1061 * Special case for @@PFS softlinks. The actual size of the
1062 * expanded softlink is "@@0x%016llx:%05d" == 26 bytes.
1063 * or for MAX_TID is "@@-1:%05d" == 10 bytes.
1065 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_SOFTLINK &&
1066 ip->ino_data.size == 10 &&
1067 ip->obj_asof == HAMMER_MAX_TID &&
1068 ip->obj_localization == 0 &&
1069 strncmp(ip->ino_data.ext.symlink, "@@PFS", 5) == 0) {
1070 if (ip->pfsm->pfsd.mirror_flags & HAMMER_PFSD_SLAVE)
1077 * We must provide a consistent atime and mtime for snapshots
1078 * so people can do a 'tar cf - ... | md5' on them and get
1079 * consistent results.
1081 if (ip->flags & HAMMER_INODE_RO) {
1082 hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_atime);
1083 hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_mtime);
1085 hammer_time_to_timespec(ip->ino_data.atime, &vap->va_atime);
1086 hammer_time_to_timespec(ip->ino_data.mtime, &vap->va_mtime);
1088 hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_ctime);
1089 vap->va_flags = ip->ino_data.uflags;
1090 vap->va_gen = 1; /* hammer inums are unique for all time */
1091 vap->va_blocksize = HAMMER_BUFSIZE;
1092 if (ip->ino_data.size >= HAMMER_XDEMARC) {
1093 vap->va_bytes = (ip->ino_data.size + HAMMER_XBUFMASK64) &
1095 } else if (ip->ino_data.size > HAMMER_BUFSIZE / 2) {
1096 vap->va_bytes = (ip->ino_data.size + HAMMER_BUFMASK64) &
1099 vap->va_bytes = (ip->ino_data.size + 15) & ~15;
1102 vap->va_type = hammer_get_vnode_type(ip->ino_data.obj_type);
1103 vap->va_filerev = 0; /* XXX */
1104 vap->va_uid_uuid = ip->ino_data.uid;
1105 vap->va_gid_uuid = ip->ino_data.gid;
1106 vap->va_fsid_uuid = ip->hmp->fsid;
1107 vap->va_vaflags = VA_UID_UUID_VALID | VA_GID_UUID_VALID |
1110 switch (ip->ino_data.obj_type) {
1111 case HAMMER_OBJTYPE_CDEV:
1112 case HAMMER_OBJTYPE_BDEV:
1113 vap->va_rmajor = ip->ino_data.rmajor;
1114 vap->va_rminor = ip->ino_data.rminor;
1119 hammer_unlock(&ip->lock);
1124 * hammer_vop_nresolve { nch, dvp, cred }
1126 * Locate the requested directory entry.
1130 hammer_vop_nresolve(struct vop_nresolve_args *ap)
1132 struct hammer_transaction trans;
1133 struct namecache *ncp;
1138 struct hammer_cursor cursor;
1147 u_int32_t localization;
1148 u_int32_t max_iterations;
1151 * Misc initialization, plus handle as-of name extensions. Look for
1152 * the '@@' extension. Note that as-of files and directories cannot
1155 dip = VTOI(ap->a_dvp);
1156 ncp = ap->a_nch->ncp;
1157 asof = dip->obj_asof;
1158 localization = dip->obj_localization; /* for code consistency */
1159 nlen = ncp->nc_nlen;
1160 flags = dip->flags & HAMMER_INODE_RO;
1164 lwkt_gettoken(&hmp->fs_token);
1165 hammer_simple_transaction(&trans, hmp);
1166 ++hammer_stats_file_iopsr;
1168 for (i = 0; i < nlen; ++i) {
1169 if (ncp->nc_name[i] == '@' && ncp->nc_name[i+1] == '@') {
1170 error = hammer_str_to_tid(ncp->nc_name + i + 2,
1171 &ispfs, &asof, &localization);
1176 if (asof != HAMMER_MAX_TID)
1177 flags |= HAMMER_INODE_RO;
1184 * If this is a PFS softlink we dive into the PFS
1186 if (ispfs && nlen == 0) {
1187 ip = hammer_get_inode(&trans, dip, HAMMER_OBJID_ROOT,
1191 error = hammer_get_vnode(ip, &vp);
1192 hammer_rel_inode(ip, 0);
1198 cache_setvp(ap->a_nch, vp);
1205 * If there is no path component the time extension is relative to dip.
1206 * e.g. "fubar/@@<snapshot>"
1208 * "." is handled by the kernel, but ".@@<snapshot>" is not.
1209 * e.g. "fubar/.@@<snapshot>"
1211 * ".." is handled by the kernel. We do not currently handle
1214 if (nlen == 0 || (nlen == 1 && ncp->nc_name[0] == '.')) {
1215 ip = hammer_get_inode(&trans, dip, dip->obj_id,
1216 asof, dip->obj_localization,
1219 error = hammer_get_vnode(ip, &vp);
1220 hammer_rel_inode(ip, 0);
1226 cache_setvp(ap->a_nch, vp);
1233 * Calculate the namekey and setup the key range for the scan. This
1234 * works kinda like a chained hash table where the lower 32 bits
1235 * of the namekey synthesize the chain.
1237 * The key range is inclusive of both key_beg and key_end.
1239 namekey = hammer_directory_namekey(dip, ncp->nc_name, nlen,
1242 error = hammer_init_cursor(&trans, &cursor, &dip->cache[1], dip);
1243 cursor.key_beg.localization = dip->obj_localization +
1244 hammer_dir_localization(dip);
1245 cursor.key_beg.obj_id = dip->obj_id;
1246 cursor.key_beg.key = namekey;
1247 cursor.key_beg.create_tid = 0;
1248 cursor.key_beg.delete_tid = 0;
1249 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1250 cursor.key_beg.obj_type = 0;
1252 cursor.key_end = cursor.key_beg;
1253 cursor.key_end.key += max_iterations;
1255 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
1258 * Scan all matching records (the chain), locate the one matching
1259 * the requested path component.
1261 * The hammer_ip_*() functions merge in-memory records with on-disk
1262 * records for the purposes of the search.
1265 localization = HAMMER_DEF_LOCALIZATION;
1268 error = hammer_ip_first(&cursor);
1269 while (error == 0) {
1270 error = hammer_ip_resolve_data(&cursor);
1273 if (nlen == cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF &&
1274 bcmp(ncp->nc_name, cursor.data->entry.name, nlen) == 0) {
1275 obj_id = cursor.data->entry.obj_id;
1276 localization = cursor.data->entry.localization;
1279 error = hammer_ip_next(&cursor);
1282 hammer_done_cursor(&cursor);
1285 * Lookup the obj_id. This should always succeed. If it does not
1286 * the filesystem may be damaged and we return a dummy inode.
1289 ip = hammer_get_inode(&trans, dip, obj_id,
1292 if (error == ENOENT) {
1293 kprintf("HAMMER: WARNING: Missing "
1294 "inode for dirent \"%s\"\n"
1295 "\tobj_id = %016llx, asof=%016llx, lo=%08x\n",
1297 (long long)obj_id, (long long)asof,
1300 ip = hammer_get_dummy_inode(&trans, dip, obj_id,
1305 error = hammer_get_vnode(ip, &vp);
1306 hammer_rel_inode(ip, 0);
1312 cache_setvp(ap->a_nch, vp);
1315 } else if (error == ENOENT) {
1316 cache_setvp(ap->a_nch, NULL);
1319 hammer_done_transaction(&trans);
1320 lwkt_reltoken(&hmp->fs_token);
1325 * hammer_vop_nlookupdotdot { dvp, vpp, cred }
1327 * Locate the parent directory of a directory vnode.
1329 * dvp is referenced but not locked. *vpp must be returned referenced and
1330 * locked. A parent_obj_id of 0 does not necessarily indicate that we are
1331 * at the root, instead it could indicate that the directory we were in was
1334 * NOTE: as-of sequences are not linked into the directory structure. If
1335 * we are at the root with a different asof then the mount point, reload
1336 * the same directory with the mount point's asof. I'm not sure what this
1337 * will do to NFS. We encode ASOF stamps in NFS file handles so it might not
1338 * get confused, but it hasn't been tested.
1342 hammer_vop_nlookupdotdot(struct vop_nlookupdotdot_args *ap)
1344 struct hammer_transaction trans;
1345 struct hammer_inode *dip;
1346 struct hammer_inode *ip;
1348 int64_t parent_obj_id;
1349 u_int32_t parent_obj_localization;
1353 dip = VTOI(ap->a_dvp);
1354 asof = dip->obj_asof;
1358 * Whos are parent? This could be the root of a pseudo-filesystem
1359 * whos parent is in another localization domain.
1361 lwkt_gettoken(&hmp->fs_token);
1362 parent_obj_id = dip->ino_data.parent_obj_id;
1363 if (dip->obj_id == HAMMER_OBJID_ROOT)
1364 parent_obj_localization = dip->ino_data.ext.obj.parent_obj_localization;
1366 parent_obj_localization = dip->obj_localization;
1368 if (parent_obj_id == 0) {
1369 if (dip->obj_id == HAMMER_OBJID_ROOT &&
1370 asof != hmp->asof) {
1371 parent_obj_id = dip->obj_id;
1373 *ap->a_fakename = kmalloc(19, M_TEMP, M_WAITOK);
1374 ksnprintf(*ap->a_fakename, 19, "0x%016llx",
1375 (long long)dip->obj_asof);
1378 lwkt_reltoken(&hmp->fs_token);
1383 hammer_simple_transaction(&trans, hmp);
1384 ++hammer_stats_file_iopsr;
1386 ip = hammer_get_inode(&trans, dip, parent_obj_id,
1387 asof, parent_obj_localization,
1388 dip->flags, &error);
1390 error = hammer_get_vnode(ip, ap->a_vpp);
1391 hammer_rel_inode(ip, 0);
1395 hammer_done_transaction(&trans);
1396 lwkt_reltoken(&hmp->fs_token);
1401 * hammer_vop_nlink { nch, dvp, vp, cred }
1405 hammer_vop_nlink(struct vop_nlink_args *ap)
1407 struct hammer_transaction trans;
1408 struct hammer_inode *dip;
1409 struct hammer_inode *ip;
1410 struct nchandle *nch;
1414 if (ap->a_dvp->v_mount != ap->a_vp->v_mount)
1418 dip = VTOI(ap->a_dvp);
1419 ip = VTOI(ap->a_vp);
1422 if (dip->obj_localization != ip->obj_localization)
1425 if (dip->flags & HAMMER_INODE_RO)
1427 if (ip->flags & HAMMER_INODE_RO)
1429 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0)
1433 * Create a transaction to cover the operations we perform.
1435 lwkt_gettoken(&hmp->fs_token);
1436 hammer_start_transaction(&trans, hmp);
1437 ++hammer_stats_file_iopsw;
1440 * Add the filesystem object to the directory. Note that neither
1441 * dip nor ip are referenced or locked, but their vnodes are
1442 * referenced. This function will bump the inode's link count.
1444 error = hammer_ip_add_directory(&trans, dip,
1445 nch->ncp->nc_name, nch->ncp->nc_nlen,
1452 cache_setunresolved(nch);
1453 cache_setvp(nch, ap->a_vp);
1455 hammer_done_transaction(&trans);
1456 hammer_knote(ap->a_vp, NOTE_LINK);
1457 hammer_knote(ap->a_dvp, NOTE_WRITE);
1458 lwkt_reltoken(&hmp->fs_token);
1463 * hammer_vop_nmkdir { nch, dvp, vpp, cred, vap }
1465 * The operating system has already ensured that the directory entry
1466 * does not exist and done all appropriate namespace locking.
1470 hammer_vop_nmkdir(struct vop_nmkdir_args *ap)
1472 struct hammer_transaction trans;
1473 struct hammer_inode *dip;
1474 struct hammer_inode *nip;
1475 struct nchandle *nch;
1480 dip = VTOI(ap->a_dvp);
1483 if (dip->flags & HAMMER_INODE_RO)
1485 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0)
1489 * Create a transaction to cover the operations we perform.
1491 lwkt_gettoken(&hmp->fs_token);
1492 hammer_start_transaction(&trans, hmp);
1493 ++hammer_stats_file_iopsw;
1496 * Create a new filesystem object of the requested type. The
1497 * returned inode will be referenced but not locked.
1499 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
1500 dip, nch->ncp->nc_name, nch->ncp->nc_nlen,
1503 hkprintf("hammer_mkdir error %d\n", error);
1504 hammer_done_transaction(&trans);
1506 lwkt_reltoken(&hmp->fs_token);
1510 * Add the new filesystem object to the directory. This will also
1511 * bump the inode's link count.
1513 error = hammer_ip_add_directory(&trans, dip,
1514 nch->ncp->nc_name, nch->ncp->nc_nlen,
1517 hkprintf("hammer_mkdir (add) error %d\n", error);
1523 hammer_rel_inode(nip, 0);
1526 error = hammer_get_vnode(nip, ap->a_vpp);
1527 hammer_rel_inode(nip, 0);
1529 cache_setunresolved(ap->a_nch);
1530 cache_setvp(ap->a_nch, *ap->a_vpp);
1533 hammer_done_transaction(&trans);
1535 hammer_knote(ap->a_dvp, NOTE_WRITE | NOTE_LINK);
1536 lwkt_reltoken(&hmp->fs_token);
1541 * hammer_vop_nmknod { nch, dvp, vpp, cred, vap }
1543 * The operating system has already ensured that the directory entry
1544 * does not exist and done all appropriate namespace locking.
1548 hammer_vop_nmknod(struct vop_nmknod_args *ap)
1550 struct hammer_transaction trans;
1551 struct hammer_inode *dip;
1552 struct hammer_inode *nip;
1553 struct nchandle *nch;
1558 dip = VTOI(ap->a_dvp);
1561 if (dip->flags & HAMMER_INODE_RO)
1563 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0)
1567 * Create a transaction to cover the operations we perform.
1569 lwkt_gettoken(&hmp->fs_token);
1570 hammer_start_transaction(&trans, hmp);
1571 ++hammer_stats_file_iopsw;
1574 * Create a new filesystem object of the requested type. The
1575 * returned inode will be referenced but not locked.
1577 * If mknod specifies a directory a pseudo-fs is created.
1579 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
1580 dip, nch->ncp->nc_name, nch->ncp->nc_nlen,
1583 hammer_done_transaction(&trans);
1585 lwkt_reltoken(&hmp->fs_token);
1590 * Add the new filesystem object to the directory. This will also
1591 * bump the inode's link count.
1593 error = hammer_ip_add_directory(&trans, dip,
1594 nch->ncp->nc_name, nch->ncp->nc_nlen,
1601 hammer_rel_inode(nip, 0);
1604 error = hammer_get_vnode(nip, ap->a_vpp);
1605 hammer_rel_inode(nip, 0);
1607 cache_setunresolved(ap->a_nch);
1608 cache_setvp(ap->a_nch, *ap->a_vpp);
1611 hammer_done_transaction(&trans);
1613 hammer_knote(ap->a_dvp, NOTE_WRITE);
1614 lwkt_reltoken(&hmp->fs_token);
1619 * hammer_vop_open { vp, mode, cred, fp }
1621 * MPSAFE (does not require fs_token)
1625 hammer_vop_open(struct vop_open_args *ap)
1629 ++hammer_stats_file_iopsr;
1630 ip = VTOI(ap->a_vp);
1632 if ((ap->a_mode & FWRITE) && (ip->flags & HAMMER_INODE_RO))
1634 return(vop_stdopen(ap));
1638 * hammer_vop_print { vp }
1642 hammer_vop_print(struct vop_print_args *ap)
1648 * hammer_vop_readdir { vp, uio, cred, *eofflag, *ncookies, off_t **cookies }
1652 hammer_vop_readdir(struct vop_readdir_args *ap)
1654 struct hammer_transaction trans;
1655 struct hammer_cursor cursor;
1656 struct hammer_inode *ip;
1659 hammer_base_elm_t base;
1668 ++hammer_stats_file_iopsr;
1669 ip = VTOI(ap->a_vp);
1671 saveoff = uio->uio_offset;
1674 if (ap->a_ncookies) {
1675 ncookies = uio->uio_resid / 16 + 1;
1676 if (ncookies > 1024)
1678 cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
1686 lwkt_gettoken(&hmp->fs_token);
1687 hammer_simple_transaction(&trans, hmp);
1690 * Handle artificial entries
1692 * It should be noted that the minimum value for a directory
1693 * hash key on-media is 0x0000000100000000, so we can use anything
1694 * less then that to represent our 'special' key space.
1698 r = vop_write_dirent(&error, uio, ip->obj_id, DT_DIR, 1, ".");
1702 cookies[cookie_index] = saveoff;
1705 if (cookie_index == ncookies)
1709 if (ip->ino_data.parent_obj_id) {
1710 r = vop_write_dirent(&error, uio,
1711 ip->ino_data.parent_obj_id,
1714 r = vop_write_dirent(&error, uio,
1715 ip->obj_id, DT_DIR, 2, "..");
1720 cookies[cookie_index] = saveoff;
1723 if (cookie_index == ncookies)
1728 * Key range (begin and end inclusive) to scan. Directory keys
1729 * directly translate to a 64 bit 'seek' position.
1731 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
1732 cursor.key_beg.localization = ip->obj_localization +
1733 hammer_dir_localization(ip);
1734 cursor.key_beg.obj_id = ip->obj_id;
1735 cursor.key_beg.create_tid = 0;
1736 cursor.key_beg.delete_tid = 0;
1737 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1738 cursor.key_beg.obj_type = 0;
1739 cursor.key_beg.key = saveoff;
1741 cursor.key_end = cursor.key_beg;
1742 cursor.key_end.key = HAMMER_MAX_KEY;
1743 cursor.asof = ip->obj_asof;
1744 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
1746 error = hammer_ip_first(&cursor);
1748 while (error == 0) {
1749 error = hammer_ip_resolve_data(&cursor);
1752 base = &cursor.leaf->base;
1753 saveoff = base->key;
1754 KKASSERT(cursor.leaf->data_len > HAMMER_ENTRY_NAME_OFF);
1756 if (base->obj_id != ip->obj_id)
1757 panic("readdir: bad record at %p", cursor.node);
1760 * Convert pseudo-filesystems into softlinks
1762 dtype = hammer_get_dtype(cursor.leaf->base.obj_type);
1763 r = vop_write_dirent(
1764 &error, uio, cursor.data->entry.obj_id,
1766 cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF ,
1767 (void *)cursor.data->entry.name);
1772 cookies[cookie_index] = base->key;
1774 if (cookie_index == ncookies)
1776 error = hammer_ip_next(&cursor);
1778 hammer_done_cursor(&cursor);
1781 hammer_done_transaction(&trans);
1784 *ap->a_eofflag = (error == ENOENT);
1785 uio->uio_offset = saveoff;
1786 if (error && cookie_index == 0) {
1787 if (error == ENOENT)
1790 kfree(cookies, M_TEMP);
1791 *ap->a_ncookies = 0;
1792 *ap->a_cookies = NULL;
1795 if (error == ENOENT)
1798 *ap->a_ncookies = cookie_index;
1799 *ap->a_cookies = cookies;
1802 lwkt_reltoken(&hmp->fs_token);
1807 * hammer_vop_readlink { vp, uio, cred }
1811 hammer_vop_readlink(struct vop_readlink_args *ap)
1813 struct hammer_transaction trans;
1814 struct hammer_cursor cursor;
1815 struct hammer_inode *ip;
1818 u_int32_t localization;
1819 hammer_pseudofs_inmem_t pfsm;
1822 ip = VTOI(ap->a_vp);
1825 lwkt_gettoken(&hmp->fs_token);
1828 * Shortcut if the symlink data was stuffed into ino_data.
1830 * Also expand special "@@PFS%05d" softlinks (expansion only
1831 * occurs for non-historical (current) accesses made from the
1832 * primary filesystem).
1834 if (ip->ino_data.size <= HAMMER_INODE_BASESYMLEN) {
1838 ptr = ip->ino_data.ext.symlink;
1839 bytes = (int)ip->ino_data.size;
1841 ip->obj_asof == HAMMER_MAX_TID &&
1842 ip->obj_localization == 0 &&
1843 strncmp(ptr, "@@PFS", 5) == 0) {
1844 hammer_simple_transaction(&trans, hmp);
1845 bcopy(ptr + 5, buf, 5);
1847 localization = strtoul(buf, NULL, 10) << 16;
1848 pfsm = hammer_load_pseudofs(&trans, localization,
1851 if (pfsm->pfsd.mirror_flags &
1852 HAMMER_PFSD_SLAVE) {
1853 /* vap->va_size == 26 */
1854 ksnprintf(buf, sizeof(buf),
1856 (long long)pfsm->pfsd.sync_end_tid,
1857 localization >> 16);
1859 /* vap->va_size == 10 */
1860 ksnprintf(buf, sizeof(buf),
1862 localization >> 16);
1864 ksnprintf(buf, sizeof(buf),
1866 (long long)HAMMER_MAX_TID,
1867 localization >> 16);
1871 bytes = strlen(buf);
1874 hammer_rel_pseudofs(hmp, pfsm);
1875 hammer_done_transaction(&trans);
1877 error = uiomove(ptr, bytes, ap->a_uio);
1878 lwkt_reltoken(&hmp->fs_token);
1885 hammer_simple_transaction(&trans, hmp);
1886 ++hammer_stats_file_iopsr;
1887 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
1890 * Key range (begin and end inclusive) to scan. Directory keys
1891 * directly translate to a 64 bit 'seek' position.
1893 cursor.key_beg.localization = ip->obj_localization +
1894 HAMMER_LOCALIZE_MISC;
1895 cursor.key_beg.obj_id = ip->obj_id;
1896 cursor.key_beg.create_tid = 0;
1897 cursor.key_beg.delete_tid = 0;
1898 cursor.key_beg.rec_type = HAMMER_RECTYPE_FIX;
1899 cursor.key_beg.obj_type = 0;
1900 cursor.key_beg.key = HAMMER_FIXKEY_SYMLINK;
1901 cursor.asof = ip->obj_asof;
1902 cursor.flags |= HAMMER_CURSOR_ASOF;
1904 error = hammer_ip_lookup(&cursor);
1906 error = hammer_ip_resolve_data(&cursor);
1908 KKASSERT(cursor.leaf->data_len >=
1909 HAMMER_SYMLINK_NAME_OFF);
1910 error = uiomove(cursor.data->symlink.name,
1911 cursor.leaf->data_len -
1912 HAMMER_SYMLINK_NAME_OFF,
1916 hammer_done_cursor(&cursor);
1917 hammer_done_transaction(&trans);
1918 lwkt_reltoken(&hmp->fs_token);
1923 * hammer_vop_nremove { nch, dvp, cred }
1927 hammer_vop_nremove(struct vop_nremove_args *ap)
1929 struct hammer_transaction trans;
1930 struct hammer_inode *dip;
1934 dip = VTOI(ap->a_dvp);
1937 if (hammer_nohistory(dip) == 0 &&
1938 (error = hammer_checkspace(hmp, HAMMER_CHKSPC_REMOVE)) != 0) {
1942 lwkt_gettoken(&hmp->fs_token);
1943 hammer_start_transaction(&trans, hmp);
1944 ++hammer_stats_file_iopsw;
1945 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp, ap->a_cred, 0, 0);
1946 hammer_done_transaction(&trans);
1948 hammer_knote(ap->a_dvp, NOTE_WRITE);
1949 lwkt_reltoken(&hmp->fs_token);
1954 * hammer_vop_nrename { fnch, tnch, fdvp, tdvp, cred }
1958 hammer_vop_nrename(struct vop_nrename_args *ap)
1960 struct hammer_transaction trans;
1961 struct namecache *fncp;
1962 struct namecache *tncp;
1963 struct hammer_inode *fdip;
1964 struct hammer_inode *tdip;
1965 struct hammer_inode *ip;
1967 struct hammer_cursor cursor;
1969 u_int32_t max_iterations;
1972 if (ap->a_fdvp->v_mount != ap->a_tdvp->v_mount)
1974 if (ap->a_fdvp->v_mount != ap->a_fnch->ncp->nc_vp->v_mount)
1977 fdip = VTOI(ap->a_fdvp);
1978 tdip = VTOI(ap->a_tdvp);
1979 fncp = ap->a_fnch->ncp;
1980 tncp = ap->a_tnch->ncp;
1981 ip = VTOI(fncp->nc_vp);
1982 KKASSERT(ip != NULL);
1986 if (fdip->obj_localization != tdip->obj_localization)
1988 if (fdip->obj_localization != ip->obj_localization)
1991 if (fdip->flags & HAMMER_INODE_RO)
1993 if (tdip->flags & HAMMER_INODE_RO)
1995 if (ip->flags & HAMMER_INODE_RO)
1997 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0)
2000 lwkt_gettoken(&hmp->fs_token);
2001 hammer_start_transaction(&trans, hmp);
2002 ++hammer_stats_file_iopsw;
2005 * Remove tncp from the target directory and then link ip as
2006 * tncp. XXX pass trans to dounlink
2008 * Force the inode sync-time to match the transaction so it is
2009 * in-sync with the creation of the target directory entry.
2011 error = hammer_dounlink(&trans, ap->a_tnch, ap->a_tdvp,
2013 if (error == 0 || error == ENOENT) {
2014 error = hammer_ip_add_directory(&trans, tdip,
2015 tncp->nc_name, tncp->nc_nlen,
2018 ip->ino_data.parent_obj_id = tdip->obj_id;
2019 ip->ino_data.ctime = trans.time;
2020 hammer_modify_inode(&trans, ip, HAMMER_INODE_DDIRTY);
2024 goto failed; /* XXX */
2027 * Locate the record in the originating directory and remove it.
2029 * Calculate the namekey and setup the key range for the scan. This
2030 * works kinda like a chained hash table where the lower 32 bits
2031 * of the namekey synthesize the chain.
2033 * The key range is inclusive of both key_beg and key_end.
2035 namekey = hammer_directory_namekey(fdip, fncp->nc_name, fncp->nc_nlen,
2038 hammer_init_cursor(&trans, &cursor, &fdip->cache[1], fdip);
2039 cursor.key_beg.localization = fdip->obj_localization +
2040 hammer_dir_localization(fdip);
2041 cursor.key_beg.obj_id = fdip->obj_id;
2042 cursor.key_beg.key = namekey;
2043 cursor.key_beg.create_tid = 0;
2044 cursor.key_beg.delete_tid = 0;
2045 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
2046 cursor.key_beg.obj_type = 0;
2048 cursor.key_end = cursor.key_beg;
2049 cursor.key_end.key += max_iterations;
2050 cursor.asof = fdip->obj_asof;
2051 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
2054 * Scan all matching records (the chain), locate the one matching
2055 * the requested path component.
2057 * The hammer_ip_*() functions merge in-memory records with on-disk
2058 * records for the purposes of the search.
2060 error = hammer_ip_first(&cursor);
2061 while (error == 0) {
2062 if (hammer_ip_resolve_data(&cursor) != 0)
2064 nlen = cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF;
2066 if (fncp->nc_nlen == nlen &&
2067 bcmp(fncp->nc_name, cursor.data->entry.name, nlen) == 0) {
2070 error = hammer_ip_next(&cursor);
2074 * If all is ok we have to get the inode so we can adjust nlinks.
2076 * WARNING: hammer_ip_del_directory() may have to terminate the
2077 * cursor to avoid a recursion. It's ok to call hammer_done_cursor()
2081 error = hammer_ip_del_directory(&trans, &cursor, fdip, ip);
2084 * XXX A deadlock here will break rename's atomicy for the purposes
2085 * of crash recovery.
2087 if (error == EDEADLK) {
2088 hammer_done_cursor(&cursor);
2093 * Cleanup and tell the kernel that the rename succeeded.
2095 * NOTE: ip->vp, if non-NULL, cannot be directly referenced
2096 * without formally acquiring the vp since the vp might
2097 * have zero refs on it, or in the middle of a reclaim,
2100 hammer_done_cursor(&cursor);
2102 cache_rename(ap->a_fnch, ap->a_tnch);
2103 hammer_knote(ap->a_fdvp, NOTE_WRITE);
2104 hammer_knote(ap->a_tdvp, NOTE_WRITE);
2108 error = hammer_get_vnode(ip, &vp);
2109 if (error == 0 && vp) {
2111 hammer_knote(ip->vp, NOTE_RENAME);
2115 kprintf("Debug: HAMMER ip/vp race2 avoided\n");
2120 hammer_done_transaction(&trans);
2121 lwkt_reltoken(&hmp->fs_token);
2126 * hammer_vop_nrmdir { nch, dvp, cred }
2130 hammer_vop_nrmdir(struct vop_nrmdir_args *ap)
2132 struct hammer_transaction trans;
2133 struct hammer_inode *dip;
2137 dip = VTOI(ap->a_dvp);
2140 if (hammer_nohistory(dip) == 0 &&
2141 (error = hammer_checkspace(hmp, HAMMER_CHKSPC_REMOVE)) != 0) {
2145 lwkt_gettoken(&hmp->fs_token);
2146 hammer_start_transaction(&trans, hmp);
2147 ++hammer_stats_file_iopsw;
2148 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp, ap->a_cred, 0, 1);
2149 hammer_done_transaction(&trans);
2151 hammer_knote(ap->a_dvp, NOTE_WRITE | NOTE_LINK);
2152 lwkt_reltoken(&hmp->fs_token);
2157 * hammer_vop_markatime { vp, cred }
2161 hammer_vop_markatime(struct vop_markatime_args *ap)
2163 struct hammer_transaction trans;
2164 struct hammer_inode *ip;
2167 ip = VTOI(ap->a_vp);
2168 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
2170 if (ip->flags & HAMMER_INODE_RO)
2173 if (hmp->mp->mnt_flag & MNT_NOATIME)
2175 lwkt_gettoken(&hmp->fs_token);
2176 hammer_start_transaction(&trans, hmp);
2177 ++hammer_stats_file_iopsw;
2179 ip->ino_data.atime = trans.time;
2180 hammer_modify_inode(&trans, ip, HAMMER_INODE_ATIME);
2181 hammer_done_transaction(&trans);
2182 hammer_knote(ap->a_vp, NOTE_ATTRIB);
2183 lwkt_reltoken(&hmp->fs_token);
2188 * hammer_vop_setattr { vp, vap, cred }
2192 hammer_vop_setattr(struct vop_setattr_args *ap)
2194 struct hammer_transaction trans;
2195 struct hammer_inode *ip;
2204 int64_t aligned_size;
2209 ip = ap->a_vp->v_data;
2214 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
2216 if (ip->flags & HAMMER_INODE_RO)
2218 if (hammer_nohistory(ip) == 0 &&
2219 (error = hammer_checkspace(hmp, HAMMER_CHKSPC_REMOVE)) != 0) {
2223 lwkt_gettoken(&hmp->fs_token);
2224 hammer_start_transaction(&trans, hmp);
2225 ++hammer_stats_file_iopsw;
2228 if (vap->va_flags != VNOVAL) {
2229 flags = ip->ino_data.uflags;
2230 error = vop_helper_setattr_flags(&flags, vap->va_flags,
2231 hammer_to_unix_xid(&ip->ino_data.uid),
2234 if (ip->ino_data.uflags != flags) {
2235 ip->ino_data.uflags = flags;
2236 ip->ino_data.ctime = trans.time;
2237 modflags |= HAMMER_INODE_DDIRTY;
2238 kflags |= NOTE_ATTRIB;
2240 if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) {
2247 if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) {
2251 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
2252 mode_t cur_mode = ip->ino_data.mode;
2253 uid_t cur_uid = hammer_to_unix_xid(&ip->ino_data.uid);
2254 gid_t cur_gid = hammer_to_unix_xid(&ip->ino_data.gid);
2258 error = vop_helper_chown(ap->a_vp, vap->va_uid, vap->va_gid,
2260 &cur_uid, &cur_gid, &cur_mode);
2262 hammer_guid_to_uuid(&uuid_uid, cur_uid);
2263 hammer_guid_to_uuid(&uuid_gid, cur_gid);
2264 if (bcmp(&uuid_uid, &ip->ino_data.uid,
2265 sizeof(uuid_uid)) ||
2266 bcmp(&uuid_gid, &ip->ino_data.gid,
2267 sizeof(uuid_gid)) ||
2268 ip->ino_data.mode != cur_mode
2270 ip->ino_data.uid = uuid_uid;
2271 ip->ino_data.gid = uuid_gid;
2272 ip->ino_data.mode = cur_mode;
2273 ip->ino_data.ctime = trans.time;
2274 modflags |= HAMMER_INODE_DDIRTY;
2276 kflags |= NOTE_ATTRIB;
2279 while (vap->va_size != VNOVAL && ip->ino_data.size != vap->va_size) {
2280 switch(ap->a_vp->v_type) {
2282 if (vap->va_size == ip->ino_data.size)
2286 * Log the operation if in fast-fsync mode or if
2287 * there are unterminated redo write records present.
2289 * The second check is needed so the recovery code
2290 * properly truncates write redos even if nominal
2291 * REDO operations is turned off due to excessive
2292 * writes, because the related records might be
2293 * destroyed and never lay down a TERM_WRITE.
2295 if ((ip->flags & HAMMER_INODE_REDO) ||
2296 (ip->flags & HAMMER_INODE_RDIRTY)) {
2297 error = hammer_generate_redo(&trans, ip,
2302 blksize = hammer_blocksize(vap->va_size);
2305 * XXX break atomicy, we can deadlock the backend
2306 * if we do not release the lock. Probably not a
2309 if (vap->va_size < ip->ino_data.size) {
2310 nvtruncbuf(ap->a_vp, vap->va_size,
2312 hammer_blockoff(vap->va_size));
2314 kflags |= NOTE_WRITE;
2316 nvextendbuf(ap->a_vp,
2319 hammer_blocksize(ip->ino_data.size),
2320 hammer_blocksize(vap->va_size),
2321 hammer_blockoff(ip->ino_data.size),
2322 hammer_blockoff(vap->va_size),
2325 kflags |= NOTE_WRITE | NOTE_EXTEND;
2327 ip->ino_data.size = vap->va_size;
2328 ip->ino_data.mtime = trans.time;
2329 /* XXX safe to use SDIRTY instead of DDIRTY here? */
2330 modflags |= HAMMER_INODE_MTIME | HAMMER_INODE_DDIRTY;
2333 * On-media truncation is cached in the inode until
2334 * the inode is synchronized. We must immediately
2335 * handle any frontend records.
2338 hammer_ip_frontend_trunc(ip, vap->va_size);
2339 #ifdef DEBUG_TRUNCATE
2340 if (HammerTruncIp == NULL)
2343 if ((ip->flags & HAMMER_INODE_TRUNCATED) == 0) {
2344 ip->flags |= HAMMER_INODE_TRUNCATED;
2345 ip->trunc_off = vap->va_size;
2346 #ifdef DEBUG_TRUNCATE
2347 if (ip == HammerTruncIp)
2348 kprintf("truncate1 %016llx\n",
2349 (long long)ip->trunc_off);
2351 } else if (ip->trunc_off > vap->va_size) {
2352 ip->trunc_off = vap->va_size;
2353 #ifdef DEBUG_TRUNCATE
2354 if (ip == HammerTruncIp)
2355 kprintf("truncate2 %016llx\n",
2356 (long long)ip->trunc_off);
2359 #ifdef DEBUG_TRUNCATE
2360 if (ip == HammerTruncIp)
2361 kprintf("truncate3 %016llx (ignored)\n",
2362 (long long)vap->va_size);
2369 * When truncating, nvtruncbuf() may have cleaned out
2370 * a portion of the last block on-disk in the buffer
2371 * cache. We must clean out any frontend records
2372 * for blocks beyond the new last block.
2374 aligned_size = (vap->va_size + (blksize - 1)) &
2375 ~(int64_t)(blksize - 1);
2376 if (truncating && vap->va_size < aligned_size) {
2377 aligned_size -= blksize;
2378 hammer_ip_frontend_trunc(ip, aligned_size);
2383 if ((ip->flags & HAMMER_INODE_TRUNCATED) == 0) {
2384 ip->flags |= HAMMER_INODE_TRUNCATED;
2385 ip->trunc_off = vap->va_size;
2386 } else if (ip->trunc_off > vap->va_size) {
2387 ip->trunc_off = vap->va_size;
2389 hammer_ip_frontend_trunc(ip, vap->va_size);
2390 ip->ino_data.size = vap->va_size;
2391 ip->ino_data.mtime = trans.time;
2392 modflags |= HAMMER_INODE_MTIME | HAMMER_INODE_DDIRTY;
2393 kflags |= NOTE_ATTRIB;
2401 if (vap->va_atime.tv_sec != VNOVAL) {
2402 ip->ino_data.atime = hammer_timespec_to_time(&vap->va_atime);
2403 modflags |= HAMMER_INODE_ATIME;
2404 kflags |= NOTE_ATTRIB;
2406 if (vap->va_mtime.tv_sec != VNOVAL) {
2407 ip->ino_data.mtime = hammer_timespec_to_time(&vap->va_mtime);
2408 modflags |= HAMMER_INODE_MTIME;
2409 kflags |= NOTE_ATTRIB;
2411 if (vap->va_mode != (mode_t)VNOVAL) {
2412 mode_t cur_mode = ip->ino_data.mode;
2413 uid_t cur_uid = hammer_to_unix_xid(&ip->ino_data.uid);
2414 gid_t cur_gid = hammer_to_unix_xid(&ip->ino_data.gid);
2416 error = vop_helper_chmod(ap->a_vp, vap->va_mode, ap->a_cred,
2417 cur_uid, cur_gid, &cur_mode);
2418 if (error == 0 && ip->ino_data.mode != cur_mode) {
2419 ip->ino_data.mode = cur_mode;
2420 ip->ino_data.ctime = trans.time;
2421 modflags |= HAMMER_INODE_DDIRTY;
2422 kflags |= NOTE_ATTRIB;
2427 hammer_modify_inode(&trans, ip, modflags);
2428 hammer_done_transaction(&trans);
2429 hammer_knote(ap->a_vp, kflags);
2430 lwkt_reltoken(&hmp->fs_token);
2435 * hammer_vop_nsymlink { nch, dvp, vpp, cred, vap, target }
2439 hammer_vop_nsymlink(struct vop_nsymlink_args *ap)
2441 struct hammer_transaction trans;
2442 struct hammer_inode *dip;
2443 struct hammer_inode *nip;
2444 hammer_record_t record;
2445 struct nchandle *nch;
2450 ap->a_vap->va_type = VLNK;
2453 dip = VTOI(ap->a_dvp);
2456 if (dip->flags & HAMMER_INODE_RO)
2458 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0)
2462 * Create a transaction to cover the operations we perform.
2464 lwkt_gettoken(&hmp->fs_token);
2465 hammer_start_transaction(&trans, hmp);
2466 ++hammer_stats_file_iopsw;
2469 * Create a new filesystem object of the requested type. The
2470 * returned inode will be referenced but not locked.
2473 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
2474 dip, nch->ncp->nc_name, nch->ncp->nc_nlen,
2477 hammer_done_transaction(&trans);
2479 lwkt_reltoken(&hmp->fs_token);
2484 * Add a record representing the symlink. symlink stores the link
2485 * as pure data, not a string, and is no \0 terminated.
2488 bytes = strlen(ap->a_target);
2490 if (bytes <= HAMMER_INODE_BASESYMLEN) {
2491 bcopy(ap->a_target, nip->ino_data.ext.symlink, bytes);
2493 record = hammer_alloc_mem_record(nip, bytes);
2494 record->type = HAMMER_MEM_RECORD_GENERAL;
2496 record->leaf.base.localization = nip->obj_localization +
2497 HAMMER_LOCALIZE_MISC;
2498 record->leaf.base.key = HAMMER_FIXKEY_SYMLINK;
2499 record->leaf.base.rec_type = HAMMER_RECTYPE_FIX;
2500 record->leaf.data_len = bytes;
2501 KKASSERT(HAMMER_SYMLINK_NAME_OFF == 0);
2502 bcopy(ap->a_target, record->data->symlink.name, bytes);
2503 error = hammer_ip_add_record(&trans, record);
2507 * Set the file size to the length of the link.
2510 nip->ino_data.size = bytes;
2511 hammer_modify_inode(&trans, nip, HAMMER_INODE_DDIRTY);
2515 error = hammer_ip_add_directory(&trans, dip, nch->ncp->nc_name,
2516 nch->ncp->nc_nlen, nip);
2522 hammer_rel_inode(nip, 0);
2525 error = hammer_get_vnode(nip, ap->a_vpp);
2526 hammer_rel_inode(nip, 0);
2528 cache_setunresolved(ap->a_nch);
2529 cache_setvp(ap->a_nch, *ap->a_vpp);
2530 hammer_knote(ap->a_dvp, NOTE_WRITE);
2533 hammer_done_transaction(&trans);
2534 lwkt_reltoken(&hmp->fs_token);
2539 * hammer_vop_nwhiteout { nch, dvp, cred, flags }
2543 hammer_vop_nwhiteout(struct vop_nwhiteout_args *ap)
2545 struct hammer_transaction trans;
2546 struct hammer_inode *dip;
2550 dip = VTOI(ap->a_dvp);
2553 if (hammer_nohistory(dip) == 0 &&
2554 (error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0) {
2558 lwkt_gettoken(&hmp->fs_token);
2559 hammer_start_transaction(&trans, hmp);
2560 ++hammer_stats_file_iopsw;
2561 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp,
2562 ap->a_cred, ap->a_flags, -1);
2563 hammer_done_transaction(&trans);
2564 lwkt_reltoken(&hmp->fs_token);
2570 * hammer_vop_ioctl { vp, command, data, fflag, cred }
2574 hammer_vop_ioctl(struct vop_ioctl_args *ap)
2576 struct hammer_inode *ip = ap->a_vp->v_data;
2577 hammer_mount_t hmp = ip->hmp;
2580 ++hammer_stats_file_iopsr;
2581 lwkt_gettoken(&hmp->fs_token);
2582 error = hammer_ioctl(ip, ap->a_command, ap->a_data,
2583 ap->a_fflag, ap->a_cred);
2584 lwkt_reltoken(&hmp->fs_token);
2590 hammer_vop_mountctl(struct vop_mountctl_args *ap)
2592 static const struct mountctl_opt extraopt[] = {
2593 { HMNT_NOHISTORY, "nohistory" },
2594 { HMNT_MASTERID, "master" },
2598 struct hammer_mount *hmp;
2605 mp = ap->a_head.a_ops->head.vv_mount;
2606 KKASSERT(mp->mnt_data != NULL);
2607 hmp = (struct hammer_mount *)mp->mnt_data;
2609 lwkt_gettoken(&hmp->fs_token);
2612 case MOUNTCTL_SET_EXPORT:
2613 if (ap->a_ctllen != sizeof(struct export_args))
2616 error = hammer_vfs_export(mp, ap->a_op,
2617 (const struct export_args *)ap->a_ctl);
2619 case MOUNTCTL_MOUNTFLAGS:
2622 * Call standard mountctl VOP function
2623 * so we get user mount flags.
2625 error = vop_stdmountctl(ap);
2629 usedbytes = *ap->a_res;
2631 if (usedbytes > 0 && usedbytes < ap->a_buflen) {
2632 usedbytes += vfs_flagstostr(hmp->hflags, extraopt,
2634 ap->a_buflen - usedbytes,
2638 *ap->a_res += usedbytes;
2642 error = vop_stdmountctl(ap);
2645 lwkt_reltoken(&hmp->fs_token);
2650 * hammer_vop_strategy { vp, bio }
2652 * Strategy call, used for regular file read & write only. Note that the
2653 * bp may represent a cluster.
2655 * To simplify operation and allow better optimizations in the future,
2656 * this code does not make any assumptions with regards to buffer alignment
2661 hammer_vop_strategy(struct vop_strategy_args *ap)
2666 bp = ap->a_bio->bio_buf;
2670 error = hammer_vop_strategy_read(ap);
2673 error = hammer_vop_strategy_write(ap);
2676 bp->b_error = error = EINVAL;
2677 bp->b_flags |= B_ERROR;
2682 /* hammer_dump_dedup_cache(((hammer_inode_t)ap->a_vp->v_data)->hmp); */
2688 * Read from a regular file. Iterate the related records and fill in the
2689 * BIO/BUF. Gaps are zero-filled.
2691 * The support code in hammer_object.c should be used to deal with mixed
2692 * in-memory and on-disk records.
2694 * NOTE: Can be called from the cluster code with an oversized buf.
2700 hammer_vop_strategy_read(struct vop_strategy_args *ap)
2702 struct hammer_transaction trans;
2703 struct hammer_inode *ip;
2704 struct hammer_inode *dip;
2706 struct hammer_cursor cursor;
2707 hammer_base_elm_t base;
2708 hammer_off_t disk_offset;
2723 ip = ap->a_vp->v_data;
2727 * The zone-2 disk offset may have been set by the cluster code via
2728 * a BMAP operation, or else should be NOOFFSET.
2730 * Checking the high bits for a match against zone-2 should suffice.
2732 * In cases where a lot of data duplication is present it may be
2733 * more beneficial to drop through and doubule-buffer through the
2736 nbio = push_bio(bio);
2737 if ((nbio->bio_offset & HAMMER_OFF_ZONE_MASK) ==
2738 HAMMER_ZONE_LARGE_DATA) {
2739 if (hammer_double_buffer == 0) {
2740 lwkt_gettoken(&hmp->fs_token);
2741 error = hammer_io_direct_read(hmp, nbio, NULL);
2742 lwkt_reltoken(&hmp->fs_token);
2747 * Try to shortcut requests for double_buffer mode too.
2748 * Since this mode runs through the device buffer cache
2749 * only compatible buffer sizes (meaning those generated
2750 * by normal filesystem buffers) are legal.
2752 if (hammer_live_dedup == 0 && (bp->b_flags & B_PAGING) == 0) {
2753 error = hammer_io_indirect_read(hmp, nbio, NULL);
2759 * Well, that sucked. Do it the hard way. If all the stars are
2760 * aligned we may still be able to issue a direct-read.
2762 lwkt_gettoken(&hmp->fs_token);
2763 hammer_simple_transaction(&trans, hmp);
2764 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
2767 * Key range (begin and end inclusive) to scan. Note that the key's
2768 * stored in the actual records represent BASE+LEN, not BASE. The
2769 * first record containing bio_offset will have a key > bio_offset.
2771 cursor.key_beg.localization = ip->obj_localization +
2772 HAMMER_LOCALIZE_MISC;
2773 cursor.key_beg.obj_id = ip->obj_id;
2774 cursor.key_beg.create_tid = 0;
2775 cursor.key_beg.delete_tid = 0;
2776 cursor.key_beg.obj_type = 0;
2777 cursor.key_beg.key = bio->bio_offset + 1;
2778 cursor.asof = ip->obj_asof;
2779 cursor.flags |= HAMMER_CURSOR_ASOF;
2781 cursor.key_end = cursor.key_beg;
2782 KKASSERT(ip->ino_data.obj_type == HAMMER_OBJTYPE_REGFILE);
2784 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_DBFILE) {
2785 cursor.key_beg.rec_type = HAMMER_RECTYPE_DB;
2786 cursor.key_end.rec_type = HAMMER_RECTYPE_DB;
2787 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
2791 ran_end = bio->bio_offset + bp->b_bufsize;
2792 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
2793 cursor.key_end.rec_type = HAMMER_RECTYPE_DATA;
2794 tmp64 = ran_end + MAXPHYS + 1; /* work-around GCC-4 bug */
2795 if (tmp64 < ran_end)
2796 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
2798 cursor.key_end.key = ran_end + MAXPHYS + 1;
2800 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
2803 * Set NOSWAPCACHE for cursor data extraction if double buffering
2804 * is disabled or (if the file is not marked cacheable via chflags
2805 * and vm.swapcache_use_chflags is enabled).
2807 if (hammer_double_buffer == 0 ||
2808 ((ap->a_vp->v_flag & VSWAPCACHE) == 0 &&
2809 vm_swapcache_use_chflags)) {
2810 cursor.flags |= HAMMER_CURSOR_NOSWAPCACHE;
2813 error = hammer_ip_first(&cursor);
2816 while (error == 0) {
2818 * Get the base file offset of the record. The key for
2819 * data records is (base + bytes) rather then (base).
2821 base = &cursor.leaf->base;
2822 rec_offset = base->key - cursor.leaf->data_len;
2825 * Calculate the gap, if any, and zero-fill it.
2827 * n is the offset of the start of the record verses our
2828 * current seek offset in the bio.
2830 n = (int)(rec_offset - (bio->bio_offset + boff));
2832 if (n > bp->b_bufsize - boff)
2833 n = bp->b_bufsize - boff;
2834 bzero((char *)bp->b_data + boff, n);
2840 * Calculate the data offset in the record and the number
2841 * of bytes we can copy.
2843 * There are two degenerate cases. First, boff may already
2844 * be at bp->b_bufsize. Secondly, the data offset within
2845 * the record may exceed the record's size.
2849 n = cursor.leaf->data_len - roff;
2851 kprintf("strategy_read: bad n=%d roff=%d\n", n, roff);
2853 } else if (n > bp->b_bufsize - boff) {
2854 n = bp->b_bufsize - boff;
2858 * Deal with cached truncations. This cool bit of code
2859 * allows truncate()/ftruncate() to avoid having to sync
2862 * If the frontend is truncated then all backend records are
2863 * subject to the frontend's truncation.
2865 * If the backend is truncated then backend records on-disk
2866 * (but not in-memory) are subject to the backend's
2867 * truncation. In-memory records owned by the backend
2868 * represent data written after the truncation point on the
2869 * backend and must not be truncated.
2871 * Truncate operations deal with frontend buffer cache
2872 * buffers and frontend-owned in-memory records synchronously.
2874 if (ip->flags & HAMMER_INODE_TRUNCATED) {
2875 if (hammer_cursor_ondisk(&cursor)/* ||
2876 cursor.iprec->flush_state == HAMMER_FST_FLUSH*/) {
2877 if (ip->trunc_off <= rec_offset)
2879 else if (ip->trunc_off < rec_offset + n)
2880 n = (int)(ip->trunc_off - rec_offset);
2883 if (ip->sync_flags & HAMMER_INODE_TRUNCATED) {
2884 if (hammer_cursor_ondisk(&cursor)) {
2885 if (ip->sync_trunc_off <= rec_offset)
2887 else if (ip->sync_trunc_off < rec_offset + n)
2888 n = (int)(ip->sync_trunc_off - rec_offset);
2893 * Try to issue a direct read into our bio if possible,
2894 * otherwise resolve the element data into a hammer_buffer
2897 * The buffer on-disk should be zerod past any real
2898 * truncation point, but may not be for any synthesized
2899 * truncation point from above.
2901 * NOTE: disk_offset is only valid if the cursor data is
2904 disk_offset = cursor.leaf->data_offset + roff;
2905 isdedupable = (boff == 0 && n == bp->b_bufsize &&
2906 hammer_cursor_ondisk(&cursor) &&
2907 ((int)disk_offset & HAMMER_BUFMASK) == 0);
2909 if (isdedupable && hammer_double_buffer == 0) {
2913 KKASSERT((disk_offset & HAMMER_OFF_ZONE_MASK) ==
2914 HAMMER_ZONE_LARGE_DATA);
2915 nbio->bio_offset = disk_offset;
2916 error = hammer_io_direct_read(hmp, nbio, cursor.leaf);
2917 if (hammer_live_dedup && error == 0)
2918 hammer_dedup_cache_add(ip, cursor.leaf);
2920 } else if (isdedupable) {
2922 * Async I/O case for reading from backing store
2923 * and copying the data to the filesystem buffer.
2924 * live-dedup has to verify the data anyway if it
2925 * gets a hit later so we can just add the entry
2928 KKASSERT((disk_offset & HAMMER_OFF_ZONE_MASK) ==
2929 HAMMER_ZONE_LARGE_DATA);
2930 nbio->bio_offset = disk_offset;
2931 if (hammer_live_dedup)
2932 hammer_dedup_cache_add(ip, cursor.leaf);
2933 error = hammer_io_indirect_read(hmp, nbio, cursor.leaf);
2936 error = hammer_ip_resolve_data(&cursor);
2938 if (hammer_live_dedup && isdedupable)
2939 hammer_dedup_cache_add(ip, cursor.leaf);
2940 bcopy((char *)cursor.data + roff,
2941 (char *)bp->b_data + boff, n);
2948 * We have to be sure that the only elements added to the
2949 * dedup cache are those which are already on-media.
2951 if (hammer_live_dedup && hammer_cursor_ondisk(&cursor))
2952 hammer_dedup_cache_add(ip, cursor.leaf);
2955 * Iterate until we have filled the request.
2958 if (boff == bp->b_bufsize)
2960 error = hammer_ip_next(&cursor);
2964 * There may have been a gap after the last record
2966 if (error == ENOENT)
2968 if (error == 0 && boff != bp->b_bufsize) {
2969 KKASSERT(boff < bp->b_bufsize);
2970 bzero((char *)bp->b_data + boff, bp->b_bufsize - boff);
2971 /* boff = bp->b_bufsize; */
2975 * Disallow swapcache operation on the vnode buffer if double
2976 * buffering is enabled, the swapcache will get the data via
2977 * the block device buffer.
2979 if (hammer_double_buffer)
2980 bp->b_flags |= B_NOTMETA;
2986 bp->b_error = error;
2988 bp->b_flags |= B_ERROR;
2993 * Cache the b-tree node for the last data read in cache[1].
2995 * If we hit the file EOF then also cache the node in the
2996 * governing director's cache[3], it will be used to initialize
2997 * the inode's cache[1] for any inodes looked up via the directory.
2999 * This doesn't reduce disk accesses since the B-Tree chain is
3000 * likely cached, but it does reduce cpu overhead when looking
3001 * up file offsets for cpdup/tar/cpio style iterations.
3004 hammer_cache_node(&ip->cache[1], cursor.node);
3005 if (ran_end >= ip->ino_data.size) {
3006 dip = hammer_find_inode(&trans, ip->ino_data.parent_obj_id,
3007 ip->obj_asof, ip->obj_localization);
3009 hammer_cache_node(&dip->cache[3], cursor.node);
3010 hammer_rel_inode(dip, 0);
3013 hammer_done_cursor(&cursor);
3014 hammer_done_transaction(&trans);
3015 lwkt_reltoken(&hmp->fs_token);
3020 * BMAP operation - used to support cluster_read() only.
3022 * (struct vnode *vp, off_t loffset, off_t *doffsetp, int *runp, int *runb)
3024 * This routine may return EOPNOTSUPP if the opration is not supported for
3025 * the specified offset. The contents of the pointer arguments do not
3026 * need to be initialized in that case.
3028 * If a disk address is available and properly aligned return 0 with
3029 * *doffsetp set to the zone-2 address, and *runp / *runb set appropriately
3030 * to the run-length relative to that offset. Callers may assume that
3031 * *doffsetp is valid if 0 is returned, even if *runp is not sufficiently
3032 * large, so return EOPNOTSUPP if it is not sufficiently large.
3036 hammer_vop_bmap(struct vop_bmap_args *ap)
3038 struct hammer_transaction trans;
3039 struct hammer_inode *ip;
3041 struct hammer_cursor cursor;
3042 hammer_base_elm_t base;
3046 int64_t base_offset;
3047 int64_t base_disk_offset;
3048 int64_t last_offset;
3049 hammer_off_t last_disk_offset;
3050 hammer_off_t disk_offset;
3055 ++hammer_stats_file_iopsr;
3056 ip = ap->a_vp->v_data;
3060 * We can only BMAP regular files. We can't BMAP database files,
3063 if (ip->ino_data.obj_type != HAMMER_OBJTYPE_REGFILE)
3067 * bmap is typically called with runp/runb both NULL when used
3068 * for writing. We do not support BMAP for writing atm.
3070 if (ap->a_cmd != BUF_CMD_READ)
3074 * Scan the B-Tree to acquire blockmap addresses, then translate
3077 lwkt_gettoken(&hmp->fs_token);
3078 hammer_simple_transaction(&trans, hmp);
3080 kprintf("bmap_beg %016llx ip->cache %p\n",
3081 (long long)ap->a_loffset, ip->cache[1]);
3083 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
3086 * Key range (begin and end inclusive) to scan. Note that the key's
3087 * stored in the actual records represent BASE+LEN, not BASE. The
3088 * first record containing bio_offset will have a key > bio_offset.
3090 cursor.key_beg.localization = ip->obj_localization +
3091 HAMMER_LOCALIZE_MISC;
3092 cursor.key_beg.obj_id = ip->obj_id;
3093 cursor.key_beg.create_tid = 0;
3094 cursor.key_beg.delete_tid = 0;
3095 cursor.key_beg.obj_type = 0;
3097 cursor.key_beg.key = ap->a_loffset - MAXPHYS + 1;
3099 cursor.key_beg.key = ap->a_loffset + 1;
3100 if (cursor.key_beg.key < 0)
3101 cursor.key_beg.key = 0;
3102 cursor.asof = ip->obj_asof;
3103 cursor.flags |= HAMMER_CURSOR_ASOF;
3105 cursor.key_end = cursor.key_beg;
3106 KKASSERT(ip->ino_data.obj_type == HAMMER_OBJTYPE_REGFILE);
3108 ran_end = ap->a_loffset + MAXPHYS;
3109 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
3110 cursor.key_end.rec_type = HAMMER_RECTYPE_DATA;
3111 tmp64 = ran_end + MAXPHYS + 1; /* work-around GCC-4 bug */
3112 if (tmp64 < ran_end)
3113 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
3115 cursor.key_end.key = ran_end + MAXPHYS + 1;
3117 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
3119 error = hammer_ip_first(&cursor);
3120 base_offset = last_offset = 0;
3121 base_disk_offset = last_disk_offset = 0;
3123 while (error == 0) {
3125 * Get the base file offset of the record. The key for
3126 * data records is (base + bytes) rather then (base).
3128 * NOTE: rec_offset + rec_len may exceed the end-of-file.
3129 * The extra bytes should be zero on-disk and the BMAP op
3130 * should still be ok.
3132 base = &cursor.leaf->base;
3133 rec_offset = base->key - cursor.leaf->data_len;
3134 rec_len = cursor.leaf->data_len;
3137 * Incorporate any cached truncation.
3139 * NOTE: Modifications to rec_len based on synthesized
3140 * truncation points remove the guarantee that any extended
3141 * data on disk is zero (since the truncations may not have
3142 * taken place on-media yet).
3144 if (ip->flags & HAMMER_INODE_TRUNCATED) {
3145 if (hammer_cursor_ondisk(&cursor) ||
3146 cursor.iprec->flush_state == HAMMER_FST_FLUSH) {
3147 if (ip->trunc_off <= rec_offset)
3149 else if (ip->trunc_off < rec_offset + rec_len)
3150 rec_len = (int)(ip->trunc_off - rec_offset);
3153 if (ip->sync_flags & HAMMER_INODE_TRUNCATED) {
3154 if (hammer_cursor_ondisk(&cursor)) {
3155 if (ip->sync_trunc_off <= rec_offset)
3157 else if (ip->sync_trunc_off < rec_offset + rec_len)
3158 rec_len = (int)(ip->sync_trunc_off - rec_offset);
3163 * Accumulate information. If we have hit a discontiguous
3164 * block reset base_offset unless we are already beyond the
3165 * requested offset. If we are, that's it, we stop.
3169 if (hammer_cursor_ondisk(&cursor)) {
3170 disk_offset = cursor.leaf->data_offset;
3171 if (rec_offset != last_offset ||
3172 disk_offset != last_disk_offset) {
3173 if (rec_offset > ap->a_loffset)
3175 base_offset = rec_offset;
3176 base_disk_offset = disk_offset;
3178 last_offset = rec_offset + rec_len;
3179 last_disk_offset = disk_offset + rec_len;
3181 if (hammer_live_dedup)
3182 hammer_dedup_cache_add(ip, cursor.leaf);
3185 error = hammer_ip_next(&cursor);
3189 kprintf("BMAP %016llx: %016llx - %016llx\n",
3190 (long long)ap->a_loffset,
3191 (long long)base_offset,
3192 (long long)last_offset);
3193 kprintf("BMAP %16s: %016llx - %016llx\n", "",
3194 (long long)base_disk_offset,
3195 (long long)last_disk_offset);
3199 hammer_cache_node(&ip->cache[1], cursor.node);
3201 kprintf("bmap_end2 %016llx ip->cache %p\n",
3202 (long long)ap->a_loffset, ip->cache[1]);
3205 hammer_done_cursor(&cursor);
3206 hammer_done_transaction(&trans);
3207 lwkt_reltoken(&hmp->fs_token);
3210 * If we couldn't find any records or the records we did find were
3211 * all behind the requested offset, return failure. A forward
3212 * truncation can leave a hole w/ no on-disk records.
3214 if (last_offset == 0 || last_offset < ap->a_loffset)
3215 return (EOPNOTSUPP);
3218 * Figure out the block size at the requested offset and adjust
3219 * our limits so the cluster_read() does not create inappropriately
3220 * sized buffer cache buffers.
3222 blksize = hammer_blocksize(ap->a_loffset);
3223 if (hammer_blocksize(base_offset) != blksize) {
3224 base_offset = hammer_blockdemarc(base_offset, ap->a_loffset);
3226 if (last_offset != ap->a_loffset &&
3227 hammer_blocksize(last_offset - 1) != blksize) {
3228 last_offset = hammer_blockdemarc(ap->a_loffset,
3233 * Returning EOPNOTSUPP simply prevents the direct-IO optimization
3236 disk_offset = base_disk_offset + (ap->a_loffset - base_offset);
3238 if ((disk_offset & HAMMER_OFF_ZONE_MASK) != HAMMER_ZONE_LARGE_DATA) {
3240 * Only large-data zones can be direct-IOd
3243 } else if ((disk_offset & HAMMER_BUFMASK) ||
3244 (last_offset - ap->a_loffset) < blksize) {
3246 * doffsetp is not aligned or the forward run size does
3247 * not cover a whole buffer, disallow the direct I/O.
3254 *ap->a_doffsetp = disk_offset;
3256 *ap->a_runb = ap->a_loffset - base_offset;
3257 KKASSERT(*ap->a_runb >= 0);
3260 *ap->a_runp = last_offset - ap->a_loffset;
3261 KKASSERT(*ap->a_runp >= 0);
3269 * Write to a regular file. Because this is a strategy call the OS is
3270 * trying to actually get data onto the media.
3274 hammer_vop_strategy_write(struct vop_strategy_args *ap)
3276 hammer_record_t record;
3287 ip = ap->a_vp->v_data;
3290 blksize = hammer_blocksize(bio->bio_offset);
3291 KKASSERT(bp->b_bufsize == blksize);
3293 if (ip->flags & HAMMER_INODE_RO) {
3294 bp->b_error = EROFS;
3295 bp->b_flags |= B_ERROR;
3300 lwkt_gettoken(&hmp->fs_token);
3303 * Disallow swapcache operation on the vnode buffer if double
3304 * buffering is enabled, the swapcache will get the data via
3305 * the block device buffer.
3307 if (hammer_double_buffer)
3308 bp->b_flags |= B_NOTMETA;
3311 * Interlock with inode destruction (no in-kernel or directory
3312 * topology visibility). If we queue new IO while trying to
3313 * destroy the inode we can deadlock the vtrunc call in
3314 * hammer_inode_unloadable_check().
3316 * Besides, there's no point flushing a bp associated with an
3317 * inode that is being destroyed on-media and has no kernel
3320 if ((ip->flags | ip->sync_flags) &
3321 (HAMMER_INODE_DELETING|HAMMER_INODE_DELETED)) {
3324 lwkt_reltoken(&hmp->fs_token);
3329 * Reserve space and issue a direct-write from the front-end.
3330 * NOTE: The direct_io code will hammer_bread/bcopy smaller
3333 * An in-memory record will be installed to reference the storage
3334 * until the flusher can get to it.
3336 * Since we own the high level bio the front-end will not try to
3337 * do a direct-read until the write completes.
3339 * NOTE: The only time we do not reserve a full-sized buffers
3340 * worth of data is if the file is small. We do not try to
3341 * allocate a fragment (from the small-data zone) at the end of
3342 * an otherwise large file as this can lead to wildly separated
3345 KKASSERT((bio->bio_offset & HAMMER_BUFMASK) == 0);
3346 KKASSERT(bio->bio_offset < ip->ino_data.size);
3347 if (bio->bio_offset || ip->ino_data.size > HAMMER_BUFSIZE / 2)
3348 bytes = bp->b_bufsize;
3350 bytes = ((int)ip->ino_data.size + 15) & ~15;
3352 record = hammer_ip_add_bulk(ip, bio->bio_offset, bp->b_data,
3356 * B_VFSFLAG1 indicates that a REDO_WRITE entry was generated
3357 * in hammer_vop_write(). We must flag the record so the proper
3358 * REDO_TERM_WRITE entry is generated during the flush.
3361 if (bp->b_flags & B_VFSFLAG1) {
3362 record->flags |= HAMMER_RECF_REDO;
3363 bp->b_flags &= ~B_VFSFLAG1;
3365 if (record->flags & HAMMER_RECF_DEDUPED) {
3367 hammer_ip_replace_bulk(hmp, record);
3370 hammer_io_direct_write(hmp, bio, record);
3372 if (ip->rsv_recs > 1 && hmp->rsv_recs > hammer_limit_recs)
3373 hammer_flush_inode(ip, 0);
3375 bp->b_bio2.bio_offset = NOOFFSET;
3376 bp->b_error = error;
3377 bp->b_flags |= B_ERROR;
3380 lwkt_reltoken(&hmp->fs_token);
3385 * dounlink - disconnect a directory entry
3387 * XXX whiteout support not really in yet
3390 hammer_dounlink(hammer_transaction_t trans, struct nchandle *nch,
3391 struct vnode *dvp, struct ucred *cred,
3392 int flags, int isdir)
3394 struct namecache *ncp;
3398 struct hammer_cursor cursor;
3400 u_int32_t max_iterations;
3404 * Calculate the namekey and setup the key range for the scan. This
3405 * works kinda like a chained hash table where the lower 32 bits
3406 * of the namekey synthesize the chain.
3408 * The key range is inclusive of both key_beg and key_end.
3414 if (dip->flags & HAMMER_INODE_RO)
3417 namekey = hammer_directory_namekey(dip, ncp->nc_name, ncp->nc_nlen,
3420 hammer_init_cursor(trans, &cursor, &dip->cache[1], dip);
3421 cursor.key_beg.localization = dip->obj_localization +
3422 hammer_dir_localization(dip);
3423 cursor.key_beg.obj_id = dip->obj_id;
3424 cursor.key_beg.key = namekey;
3425 cursor.key_beg.create_tid = 0;
3426 cursor.key_beg.delete_tid = 0;
3427 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
3428 cursor.key_beg.obj_type = 0;
3430 cursor.key_end = cursor.key_beg;
3431 cursor.key_end.key += max_iterations;
3432 cursor.asof = dip->obj_asof;
3433 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
3436 * Scan all matching records (the chain), locate the one matching
3437 * the requested path component. info->last_error contains the
3438 * error code on search termination and could be 0, ENOENT, or
3441 * The hammer_ip_*() functions merge in-memory records with on-disk
3442 * records for the purposes of the search.
3444 error = hammer_ip_first(&cursor);
3446 while (error == 0) {
3447 error = hammer_ip_resolve_data(&cursor);
3450 nlen = cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF;
3452 if (ncp->nc_nlen == nlen &&
3453 bcmp(ncp->nc_name, cursor.data->entry.name, nlen) == 0) {
3456 error = hammer_ip_next(&cursor);
3460 * If all is ok we have to get the inode so we can adjust nlinks.
3461 * To avoid a deadlock with the flusher we must release the inode
3462 * lock on the directory when acquiring the inode for the entry.
3464 * If the target is a directory, it must be empty.
3467 hammer_unlock(&cursor.ip->lock);
3468 ip = hammer_get_inode(trans, dip, cursor.data->entry.obj_id,
3470 cursor.data->entry.localization,
3472 hammer_lock_sh(&cursor.ip->lock);
3473 if (error == ENOENT) {
3474 kprintf("HAMMER: WARNING: Removing "
3475 "dirent w/missing inode \"%s\"\n"
3476 "\tobj_id = %016llx\n",
3478 (long long)cursor.data->entry.obj_id);
3483 * If isdir >= 0 we validate that the entry is or is not a
3484 * directory. If isdir < 0 we don't care.
3486 if (error == 0 && isdir >= 0 && ip) {
3488 ip->ino_data.obj_type != HAMMER_OBJTYPE_DIRECTORY) {
3490 } else if (isdir == 0 &&
3491 ip->ino_data.obj_type == HAMMER_OBJTYPE_DIRECTORY) {
3497 * If we are trying to remove a directory the directory must
3500 * The check directory code can loop and deadlock/retry. Our
3501 * own cursor's node locks must be released to avoid a 3-way
3502 * deadlock with the flusher if the check directory code
3505 * If any changes whatsoever have been made to the cursor
3506 * set EDEADLK and retry.
3508 * WARNING: See warnings in hammer_unlock_cursor()
3511 if (error == 0 && ip && ip->ino_data.obj_type ==
3512 HAMMER_OBJTYPE_DIRECTORY) {
3513 hammer_unlock_cursor(&cursor);
3514 error = hammer_ip_check_directory_empty(trans, ip);
3515 hammer_lock_cursor(&cursor);
3516 if (cursor.flags & HAMMER_CURSOR_RETEST) {
3517 kprintf("HAMMER: Warning: avoided deadlock "
3525 * Delete the directory entry.
3527 * WARNING: hammer_ip_del_directory() may have to terminate
3528 * the cursor to avoid a deadlock. It is ok to call
3529 * hammer_done_cursor() twice.
3532 error = hammer_ip_del_directory(trans, &cursor,
3535 hammer_done_cursor(&cursor);
3537 cache_setunresolved(nch);
3538 cache_setvp(nch, NULL);
3541 * NOTE: ip->vp, if non-NULL, cannot be directly
3542 * referenced without formally acquiring the
3543 * vp since the vp might have zero refs on it,
3544 * or in the middle of a reclaim, etc.
3546 * NOTE: The cache_setunresolved() can rip the vp
3547 * out from under us since the vp may not have
3548 * any refs, in which case ip->vp will be NULL
3551 while (ip && ip->vp) {
3554 error = hammer_get_vnode(ip, &vp);
3555 if (error == 0 && vp) {
3557 hammer_knote(ip->vp, NOTE_DELETE);
3558 cache_inval_vp(ip->vp, CINV_DESTROY);
3562 kprintf("Debug: HAMMER ip/vp race1 avoided\n");
3566 hammer_rel_inode(ip, 0);
3568 hammer_done_cursor(&cursor);
3570 if (error == EDEADLK)
3576 /************************************************************************
3577 * FIFO AND SPECFS OPS *
3578 ************************************************************************
3582 hammer_vop_fifoclose (struct vop_close_args *ap)
3584 /* XXX update itimes */
3585 return (VOCALL(&fifo_vnode_vops, &ap->a_head));
3589 hammer_vop_fiforead (struct vop_read_args *ap)
3593 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
3594 /* XXX update access time */
3599 hammer_vop_fifowrite (struct vop_write_args *ap)
3603 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
3604 /* XXX update access time */
3610 hammer_vop_fifokqfilter(struct vop_kqfilter_args *ap)
3614 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
3616 error = hammer_vop_kqfilter(ap);
3620 /************************************************************************
3622 ************************************************************************
3625 static void filt_hammerdetach(struct knote *kn);
3626 static int filt_hammerread(struct knote *kn, long hint);
3627 static int filt_hammerwrite(struct knote *kn, long hint);
3628 static int filt_hammervnode(struct knote *kn, long hint);
3630 static struct filterops hammerread_filtops =
3631 { FILTEROP_ISFD, NULL, filt_hammerdetach, filt_hammerread };
3632 static struct filterops hammerwrite_filtops =
3633 { FILTEROP_ISFD, NULL, filt_hammerdetach, filt_hammerwrite };
3634 static struct filterops hammervnode_filtops =
3635 { FILTEROP_ISFD, NULL, filt_hammerdetach, filt_hammervnode };
3639 hammer_vop_kqfilter(struct vop_kqfilter_args *ap)
3641 struct vnode *vp = ap->a_vp;
3642 struct knote *kn = ap->a_kn;
3644 switch (kn->kn_filter) {
3646 kn->kn_fop = &hammerread_filtops;
3649 kn->kn_fop = &hammerwrite_filtops;
3652 kn->kn_fop = &hammervnode_filtops;
3655 return (EOPNOTSUPP);
3658 kn->kn_hook = (caddr_t)vp;
3660 knote_insert(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
3666 filt_hammerdetach(struct knote *kn)
3668 struct vnode *vp = (void *)kn->kn_hook;
3670 knote_remove(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
3674 filt_hammerread(struct knote *kn, long hint)
3676 struct vnode *vp = (void *)kn->kn_hook;
3677 hammer_inode_t ip = VTOI(vp);
3678 hammer_mount_t hmp = ip->hmp;
3681 if (hint == NOTE_REVOKE) {
3682 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
3685 lwkt_gettoken(&hmp->fs_token); /* XXX use per-ip-token */
3686 off = ip->ino_data.size - kn->kn_fp->f_offset;
3687 kn->kn_data = (off < INTPTR_MAX) ? off : INTPTR_MAX;
3688 lwkt_reltoken(&hmp->fs_token);
3689 if (kn->kn_sfflags & NOTE_OLDAPI)
3691 return (kn->kn_data != 0);
3695 filt_hammerwrite(struct knote *kn, long hint)
3697 if (hint == NOTE_REVOKE)
3698 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
3704 filt_hammervnode(struct knote *kn, long hint)
3706 if (kn->kn_sfflags & hint)
3707 kn->kn_fflags |= hint;
3708 if (hint == NOTE_REVOKE) {
3709 kn->kn_flags |= (EV_EOF | EV_NODATA);
3712 return (kn->kn_fflags != 0);