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;
435 if (blksize == HAMMER_XBUFSIZE)
436 bp->b_flags |= B_CLUSTEROK;
438 n = blksize - offset;
439 if (n > uio->uio_resid)
441 if (n > ip->ino_data.size - uio->uio_offset)
442 n = (int)(ip->ino_data.size - uio->uio_offset);
444 lwkt_reltoken(&hmp->fs_token);
447 * Set B_AGE, data has a lower priority than meta-data.
449 * Use a hold/unlock/drop sequence to run the uiomove
450 * with the buffer unlocked, avoiding deadlocks against
451 * read()s on mmap()'d spaces.
453 bp->b_flags |= B_AGE;
456 error = uiomove((char *)bp->b_data + offset, n, uio);
460 lwkt_gettoken(&hmp->fs_token);
464 hammer_stats_file_read += n;
468 * Try to update the atime with just the inode lock for maximum
469 * concurrency. If we can't shortcut it we have to get the full
472 if (got_fstoken == 0 && hammer_update_atime_quick(ip) < 0) {
473 lwkt_gettoken(&hmp->fs_token);
475 hammer_start_transaction(&trans, ip->hmp);
479 if ((ip->flags & HAMMER_INODE_RO) == 0 &&
480 (ip->hmp->mp->mnt_flag & MNT_NOATIME) == 0) {
481 ip->ino_data.atime = trans.time;
482 hammer_modify_inode(&trans, ip, HAMMER_INODE_ATIME);
484 hammer_done_transaction(&trans);
485 lwkt_reltoken(&hmp->fs_token);
491 * hammer_vop_write { vp, uio, ioflag, cred }
495 hammer_vop_write(struct vop_write_args *ap)
497 struct hammer_transaction trans;
498 struct hammer_inode *ip;
513 if (ap->a_vp->v_type != VREG)
519 seqcount = ap->a_ioflag >> 16;
521 if (ip->flags & HAMMER_INODE_RO)
525 * Create a transaction to cover the operations we perform.
527 lwkt_gettoken(&hmp->fs_token);
528 hammer_start_transaction(&trans, hmp);
534 if (ap->a_ioflag & IO_APPEND)
535 uio->uio_offset = ip->ino_data.size;
538 * Check for illegal write offsets. Valid range is 0...2^63-1.
540 * NOTE: the base_off assignment is required to work around what
541 * I consider to be a GCC-4 optimization bug.
543 if (uio->uio_offset < 0) {
544 hammer_done_transaction(&trans);
545 lwkt_reltoken(&hmp->fs_token);
548 base_offset = uio->uio_offset + uio->uio_resid; /* work around gcc-4 */
549 if (uio->uio_resid > 0 && base_offset <= uio->uio_offset) {
550 hammer_done_transaction(&trans);
551 lwkt_reltoken(&hmp->fs_token);
555 if (uio->uio_resid > 0 && (td = uio->uio_td) != NULL && td->td_proc &&
556 base_offset > td->td_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
557 hammer_done_transaction(&trans);
558 lwkt_reltoken(&hmp->fs_token);
559 lwpsignal(td->td_proc, td->td_lwp, SIGXFSZ);
564 * If reading or writing a huge amount of data we have to break
565 * atomicy and allow the operation to be interrupted by a signal
566 * or it can DOS the machine.
568 * Preset redo_count so we stop generating REDOs earlier if the
571 bigwrite = (uio->uio_resid > 100 * 1024 * 1024);
572 if ((ip->flags & HAMMER_INODE_REDO) &&
573 ip->redo_count < hammer_limit_redo) {
574 ip->redo_count += uio->uio_resid;
578 * Access the data typically in HAMMER_BUFSIZE blocks via the
579 * buffer cache, but HAMMER may use a variable block size based
582 while (uio->uio_resid > 0) {
590 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_WRITE)) != 0)
592 if (bigwrite && (error = hammer_signal_check(hmp)) != 0)
595 blksize = hammer_blocksize(uio->uio_offset);
598 * Do not allow HAMMER to blow out the buffer cache. Very
599 * large UIOs can lockout other processes due to bwillwrite()
602 * The hammer inode is not locked during these operations.
603 * The vnode is locked which can interfere with the pageout
604 * daemon for non-UIO_NOCOPY writes but should not interfere
605 * with the buffer cache. Even so, we cannot afford to
606 * allow the pageout daemon to build up too many dirty buffer
609 * Only call this if we aren't being recursively called from
610 * a virtual disk device (vn), else we may deadlock.
612 if ((ap->a_ioflag & IO_RECURSE) == 0)
616 * Control the number of pending records associated with
617 * this inode. If too many have accumulated start a
618 * flush. Try to maintain a pipeline with the flusher.
620 * NOTE: It is possible for other sources to grow the
621 * records but not necessarily issue another flush,
622 * so use a timeout and ensure that a re-flush occurs.
624 if (ip->rsv_recs >= hammer_limit_inode_recs) {
625 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
626 while (ip->rsv_recs >= hammer_limit_inode_recs * 2) {
627 ip->flags |= HAMMER_INODE_RECSW;
628 tsleep(&ip->rsv_recs, 0, "hmrwww", hz);
629 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
635 * Do not allow HAMMER to blow out system memory by
636 * accumulating too many records. Records are so well
637 * decoupled from the buffer cache that it is possible
638 * for userland to push data out to the media via
639 * direct-write, but build up the records queued to the
640 * backend faster then the backend can flush them out.
641 * HAMMER has hit its write limit but the frontend has
642 * no pushback to slow it down.
644 if (hmp->rsv_recs > hammer_limit_recs / 2) {
646 * Get the inode on the flush list
648 if (ip->rsv_recs >= 64)
649 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
650 else if (ip->rsv_recs >= 16)
651 hammer_flush_inode(ip, 0);
654 * Keep the flusher going if the system keeps
657 delta = hmp->count_newrecords -
658 hmp->last_newrecords;
659 if (delta < 0 || delta > hammer_limit_recs / 2) {
660 hmp->last_newrecords = hmp->count_newrecords;
661 hammer_sync_hmp(hmp, MNT_NOWAIT);
665 * If we have gotten behind start slowing
668 delta = (hmp->rsv_recs - hammer_limit_recs) *
669 hz / hammer_limit_recs;
671 tsleep(&trans, 0, "hmrslo", delta);
676 * Calculate the blocksize at the current offset and figure
677 * out how much we can actually write.
679 blkmask = blksize - 1;
680 offset = (int)uio->uio_offset & blkmask;
681 base_offset = uio->uio_offset & ~(int64_t)blkmask;
682 n = blksize - offset;
683 if (n > uio->uio_resid) {
689 nsize = uio->uio_offset + n;
690 if (nsize > ip->ino_data.size) {
691 if (uio->uio_offset > ip->ino_data.size)
695 nvextendbuf(ap->a_vp,
698 hammer_blocksize(ip->ino_data.size),
699 hammer_blocksize(nsize),
700 hammer_blockoff(ip->ino_data.size),
701 hammer_blockoff(nsize),
704 kflags |= NOTE_EXTEND;
707 if (uio->uio_segflg == UIO_NOCOPY) {
709 * Issuing a write with the same data backing the
710 * buffer. Instantiate the buffer to collect the
711 * backing vm pages, then read-in any missing bits.
713 * This case is used by vop_stdputpages().
715 bp = getblk(ap->a_vp, base_offset,
716 blksize, GETBLK_BHEAVY, 0);
717 if ((bp->b_flags & B_CACHE) == 0) {
719 error = bread(ap->a_vp, base_offset,
722 } else if (offset == 0 && uio->uio_resid >= blksize) {
724 * Even though we are entirely overwriting the buffer
725 * we may still have to zero it out to avoid a
726 * mmap/write visibility issue.
728 bp = getblk(ap->a_vp, base_offset, blksize, GETBLK_BHEAVY, 0);
729 if ((bp->b_flags & B_CACHE) == 0)
731 } else if (base_offset >= ip->ino_data.size) {
733 * If the base offset of the buffer is beyond the
734 * file EOF, we don't have to issue a read.
736 bp = getblk(ap->a_vp, base_offset,
737 blksize, GETBLK_BHEAVY, 0);
741 * Partial overwrite, read in any missing bits then
742 * replace the portion being written.
744 error = bread(ap->a_vp, base_offset, blksize, &bp);
749 lwkt_reltoken(&hmp->fs_token);
750 error = uiomove(bp->b_data + offset, n, uio);
751 lwkt_gettoken(&hmp->fs_token);
755 * Generate REDO records if enabled and redo_count will not
756 * exceeded the limit.
758 * If redo_count exceeds the limit we stop generating records
759 * and clear HAMMER_INODE_REDO. This will cause the next
760 * fsync() to do a full meta-data sync instead of just an
761 * UNDO/REDO fifo update.
763 * When clearing HAMMER_INODE_REDO any pre-existing REDOs
764 * will still be tracked. The tracks will be terminated
765 * when the related meta-data (including possible data
766 * modifications which are not tracked via REDO) is
769 if ((ip->flags & HAMMER_INODE_REDO) && error == 0) {
770 if (ip->redo_count < hammer_limit_redo) {
771 bp->b_flags |= B_VFSFLAG1;
772 error = hammer_generate_redo(&trans, ip,
773 base_offset + offset,
778 ip->flags &= ~HAMMER_INODE_REDO;
783 * If we screwed up we have to undo any VM size changes we
789 nvtruncbuf(ap->a_vp, ip->ino_data.size,
790 hammer_blocksize(ip->ino_data.size),
791 hammer_blockoff(ip->ino_data.size),
796 kflags |= NOTE_WRITE;
797 hammer_stats_file_write += n;
798 if (blksize == HAMMER_XBUFSIZE)
799 bp->b_flags |= B_CLUSTEROK;
800 if (ip->ino_data.size < uio->uio_offset) {
801 ip->ino_data.size = uio->uio_offset;
802 flags = HAMMER_INODE_SDIRTY;
806 ip->ino_data.mtime = trans.time;
807 flags |= HAMMER_INODE_MTIME | HAMMER_INODE_BUFS;
808 hammer_modify_inode(&trans, ip, flags);
811 * Once we dirty the buffer any cached zone-X offset
812 * becomes invalid. HAMMER NOTE: no-history mode cannot
813 * allow overwriting over the same data sector unless
814 * we provide UNDOs for the old data, which we don't.
816 bp->b_bio2.bio_offset = NOOFFSET;
819 * Final buffer disposition.
821 * Because meta-data updates are deferred, HAMMER is
822 * especially sensitive to excessive bdwrite()s because
823 * the I/O stream is not broken up by disk reads. So the
824 * buffer cache simply cannot keep up.
826 * WARNING! blksize is variable. cluster_write() is
827 * expected to not blow up if it encounters
828 * buffers that do not match the passed blksize.
830 * NOTE! Hammer shouldn't need to bawrite()/cluster_write().
831 * The ip->rsv_recs check should burst-flush the data.
832 * If we queue it immediately the buf could be left
833 * locked on the device queue for a very long time.
835 * However, failing to flush a dirty buffer out when
836 * issued from the pageout daemon can result in a low
837 * memory deadlock against bio_page_alloc(), so we
838 * have to bawrite() on IO_ASYNC as well.
840 * NOTE! To avoid degenerate stalls due to mismatched block
841 * sizes we only honor IO_DIRECT on the write which
842 * abuts the end of the buffer. However, we must
843 * honor IO_SYNC in case someone is silly enough to
844 * configure a HAMMER file as swap, or when HAMMER
845 * is serving NFS (for commits). Ick ick.
847 bp->b_flags |= B_AGE;
848 if (blksize == HAMMER_XBUFSIZE)
849 bp->b_flags |= B_CLUSTEROK;
851 if (ap->a_ioflag & IO_SYNC) {
853 } else if ((ap->a_ioflag & IO_DIRECT) && endofblk) {
855 } else if (ap->a_ioflag & IO_ASYNC) {
857 } else if (hammer_cluster_enable &&
858 !(ap->a_vp->v_mount->mnt_flag & MNT_NOCLUSTERW)) {
859 if (base_offset < HAMMER_XDEMARC)
860 cluster_eof = hammer_blockdemarc(base_offset,
863 cluster_eof = ip->ino_data.size;
864 cluster_write(bp, cluster_eof, blksize, seqcount);
869 hammer_done_transaction(&trans);
870 hammer_knote(ap->a_vp, kflags);
871 lwkt_reltoken(&hmp->fs_token);
876 * hammer_vop_access { vp, mode, cred }
878 * MPSAFE - does not require fs_token
882 hammer_vop_access(struct vop_access_args *ap)
884 struct hammer_inode *ip = VTOI(ap->a_vp);
889 ++hammer_stats_file_iopsr;
890 uid = hammer_to_unix_xid(&ip->ino_data.uid);
891 gid = hammer_to_unix_xid(&ip->ino_data.gid);
893 error = vop_helper_access(ap, uid, gid, ip->ino_data.mode,
894 ip->ino_data.uflags);
899 * hammer_vop_advlock { vp, id, op, fl, flags }
901 * MPSAFE - does not require fs_token
905 hammer_vop_advlock(struct vop_advlock_args *ap)
907 hammer_inode_t ip = VTOI(ap->a_vp);
909 return (lf_advlock(ap, &ip->advlock, ip->ino_data.size));
913 * hammer_vop_close { vp, fflag }
915 * We can only sync-on-close for normal closes. XXX disabled for now.
919 hammer_vop_close(struct vop_close_args *ap)
922 struct vnode *vp = ap->a_vp;
923 hammer_inode_t ip = VTOI(vp);
925 if (ip->flags & (HAMMER_INODE_CLOSESYNC|HAMMER_INODE_CLOSEASYNC)) {
926 if (vn_islocked(vp) == LK_EXCLUSIVE &&
927 (vp->v_flag & (VINACTIVE|VRECLAIMED)) == 0) {
928 if (ip->flags & HAMMER_INODE_CLOSESYNC)
931 waitfor = MNT_NOWAIT;
932 ip->flags &= ~(HAMMER_INODE_CLOSESYNC |
933 HAMMER_INODE_CLOSEASYNC);
934 VOP_FSYNC(vp, MNT_NOWAIT, waitfor);
938 return (vop_stdclose(ap));
942 * hammer_vop_ncreate { nch, dvp, vpp, cred, vap }
944 * The operating system has already ensured that the directory entry
945 * does not exist and done all appropriate namespace locking.
949 hammer_vop_ncreate(struct vop_ncreate_args *ap)
951 struct hammer_transaction trans;
952 struct hammer_inode *dip;
953 struct hammer_inode *nip;
954 struct nchandle *nch;
959 dip = VTOI(ap->a_dvp);
962 if (dip->flags & HAMMER_INODE_RO)
964 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0)
968 * Create a transaction to cover the operations we perform.
970 lwkt_gettoken(&hmp->fs_token);
971 hammer_start_transaction(&trans, hmp);
972 ++hammer_stats_file_iopsw;
975 * Create a new filesystem object of the requested type. The
976 * returned inode will be referenced and shared-locked to prevent
977 * it from being moved to the flusher.
979 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
980 dip, nch->ncp->nc_name, nch->ncp->nc_nlen,
983 hkprintf("hammer_create_inode error %d\n", error);
984 hammer_done_transaction(&trans);
986 lwkt_reltoken(&hmp->fs_token);
991 * Add the new filesystem object to the directory. This will also
992 * bump the inode's link count.
994 error = hammer_ip_add_directory(&trans, dip,
995 nch->ncp->nc_name, nch->ncp->nc_nlen,
998 hkprintf("hammer_ip_add_directory error %d\n", error);
1004 hammer_rel_inode(nip, 0);
1005 hammer_done_transaction(&trans);
1008 error = hammer_get_vnode(nip, ap->a_vpp);
1009 hammer_done_transaction(&trans);
1010 hammer_rel_inode(nip, 0);
1012 cache_setunresolved(ap->a_nch);
1013 cache_setvp(ap->a_nch, *ap->a_vpp);
1015 hammer_knote(ap->a_dvp, NOTE_WRITE);
1017 lwkt_reltoken(&hmp->fs_token);
1022 * hammer_vop_getattr { vp, vap }
1024 * Retrieve an inode's attribute information. When accessing inodes
1025 * historically we fake the atime field to ensure consistent results.
1026 * The atime field is stored in the B-Tree element and allowed to be
1027 * updated without cycling the element.
1029 * MPSAFE - does not require fs_token
1033 hammer_vop_getattr(struct vop_getattr_args *ap)
1035 struct hammer_inode *ip = VTOI(ap->a_vp);
1036 struct vattr *vap = ap->a_vap;
1039 * We want the fsid to be different when accessing a filesystem
1040 * with different as-of's so programs like diff don't think
1041 * the files are the same.
1043 * We also want the fsid to be the same when comparing snapshots,
1044 * or when comparing mirrors (which might be backed by different
1045 * physical devices). HAMMER fsids are based on the PFS's
1046 * shared_uuid field.
1048 * XXX there is a chance of collision here. The va_fsid reported
1049 * by stat is different from the more involved fsid used in the
1052 ++hammer_stats_file_iopsr;
1053 hammer_lock_sh(&ip->lock);
1054 vap->va_fsid = ip->pfsm->fsid_udev ^ (u_int32_t)ip->obj_asof ^
1055 (u_int32_t)(ip->obj_asof >> 32);
1057 vap->va_fileid = ip->ino_leaf.base.obj_id;
1058 vap->va_mode = ip->ino_data.mode;
1059 vap->va_nlink = ip->ino_data.nlinks;
1060 vap->va_uid = hammer_to_unix_xid(&ip->ino_data.uid);
1061 vap->va_gid = hammer_to_unix_xid(&ip->ino_data.gid);
1064 vap->va_size = ip->ino_data.size;
1067 * Special case for @@PFS softlinks. The actual size of the
1068 * expanded softlink is "@@0x%016llx:%05d" == 26 bytes.
1069 * or for MAX_TID is "@@-1:%05d" == 10 bytes.
1071 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_SOFTLINK &&
1072 ip->ino_data.size == 10 &&
1073 ip->obj_asof == HAMMER_MAX_TID &&
1074 ip->obj_localization == 0 &&
1075 strncmp(ip->ino_data.ext.symlink, "@@PFS", 5) == 0) {
1076 if (ip->pfsm->pfsd.mirror_flags & HAMMER_PFSD_SLAVE)
1083 * We must provide a consistent atime and mtime for snapshots
1084 * so people can do a 'tar cf - ... | md5' on them and get
1085 * consistent results.
1087 if (ip->flags & HAMMER_INODE_RO) {
1088 hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_atime);
1089 hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_mtime);
1091 hammer_time_to_timespec(ip->ino_data.atime, &vap->va_atime);
1092 hammer_time_to_timespec(ip->ino_data.mtime, &vap->va_mtime);
1094 hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_ctime);
1095 vap->va_flags = ip->ino_data.uflags;
1096 vap->va_gen = 1; /* hammer inums are unique for all time */
1097 vap->va_blocksize = HAMMER_BUFSIZE;
1098 if (ip->ino_data.size >= HAMMER_XDEMARC) {
1099 vap->va_bytes = (ip->ino_data.size + HAMMER_XBUFMASK64) &
1101 } else if (ip->ino_data.size > HAMMER_BUFSIZE / 2) {
1102 vap->va_bytes = (ip->ino_data.size + HAMMER_BUFMASK64) &
1105 vap->va_bytes = (ip->ino_data.size + 15) & ~15;
1108 vap->va_type = hammer_get_vnode_type(ip->ino_data.obj_type);
1109 vap->va_filerev = 0; /* XXX */
1110 vap->va_uid_uuid = ip->ino_data.uid;
1111 vap->va_gid_uuid = ip->ino_data.gid;
1112 vap->va_fsid_uuid = ip->hmp->fsid;
1113 vap->va_vaflags = VA_UID_UUID_VALID | VA_GID_UUID_VALID |
1116 switch (ip->ino_data.obj_type) {
1117 case HAMMER_OBJTYPE_CDEV:
1118 case HAMMER_OBJTYPE_BDEV:
1119 vap->va_rmajor = ip->ino_data.rmajor;
1120 vap->va_rminor = ip->ino_data.rminor;
1125 hammer_unlock(&ip->lock);
1130 * hammer_vop_nresolve { nch, dvp, cred }
1132 * Locate the requested directory entry.
1136 hammer_vop_nresolve(struct vop_nresolve_args *ap)
1138 struct hammer_transaction trans;
1139 struct namecache *ncp;
1144 struct hammer_cursor cursor;
1153 u_int32_t localization;
1154 u_int32_t max_iterations;
1157 * Misc initialization, plus handle as-of name extensions. Look for
1158 * the '@@' extension. Note that as-of files and directories cannot
1161 dip = VTOI(ap->a_dvp);
1162 ncp = ap->a_nch->ncp;
1163 asof = dip->obj_asof;
1164 localization = dip->obj_localization; /* for code consistency */
1165 nlen = ncp->nc_nlen;
1166 flags = dip->flags & HAMMER_INODE_RO;
1170 lwkt_gettoken(&hmp->fs_token);
1171 hammer_simple_transaction(&trans, hmp);
1172 ++hammer_stats_file_iopsr;
1174 for (i = 0; i < nlen; ++i) {
1175 if (ncp->nc_name[i] == '@' && ncp->nc_name[i+1] == '@') {
1176 error = hammer_str_to_tid(ncp->nc_name + i + 2,
1177 &ispfs, &asof, &localization);
1182 if (asof != HAMMER_MAX_TID)
1183 flags |= HAMMER_INODE_RO;
1190 * If this is a PFS softlink we dive into the PFS
1192 if (ispfs && nlen == 0) {
1193 ip = hammer_get_inode(&trans, dip, HAMMER_OBJID_ROOT,
1197 error = hammer_get_vnode(ip, &vp);
1198 hammer_rel_inode(ip, 0);
1204 cache_setvp(ap->a_nch, vp);
1211 * If there is no path component the time extension is relative to dip.
1212 * e.g. "fubar/@@<snapshot>"
1214 * "." is handled by the kernel, but ".@@<snapshot>" is not.
1215 * e.g. "fubar/.@@<snapshot>"
1217 * ".." is handled by the kernel. We do not currently handle
1220 if (nlen == 0 || (nlen == 1 && ncp->nc_name[0] == '.')) {
1221 ip = hammer_get_inode(&trans, dip, dip->obj_id,
1222 asof, dip->obj_localization,
1225 error = hammer_get_vnode(ip, &vp);
1226 hammer_rel_inode(ip, 0);
1232 cache_setvp(ap->a_nch, vp);
1239 * Calculate the namekey and setup the key range for the scan. This
1240 * works kinda like a chained hash table where the lower 32 bits
1241 * of the namekey synthesize the chain.
1243 * The key range is inclusive of both key_beg and key_end.
1245 namekey = hammer_directory_namekey(dip, ncp->nc_name, nlen,
1248 error = hammer_init_cursor(&trans, &cursor, &dip->cache[1], dip);
1249 cursor.key_beg.localization = dip->obj_localization +
1250 hammer_dir_localization(dip);
1251 cursor.key_beg.obj_id = dip->obj_id;
1252 cursor.key_beg.key = namekey;
1253 cursor.key_beg.create_tid = 0;
1254 cursor.key_beg.delete_tid = 0;
1255 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1256 cursor.key_beg.obj_type = 0;
1258 cursor.key_end = cursor.key_beg;
1259 cursor.key_end.key += max_iterations;
1261 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
1264 * Scan all matching records (the chain), locate the one matching
1265 * the requested path component.
1267 * The hammer_ip_*() functions merge in-memory records with on-disk
1268 * records for the purposes of the search.
1271 localization = HAMMER_DEF_LOCALIZATION;
1274 error = hammer_ip_first(&cursor);
1275 while (error == 0) {
1276 error = hammer_ip_resolve_data(&cursor);
1279 if (nlen == cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF &&
1280 bcmp(ncp->nc_name, cursor.data->entry.name, nlen) == 0) {
1281 obj_id = cursor.data->entry.obj_id;
1282 localization = cursor.data->entry.localization;
1285 error = hammer_ip_next(&cursor);
1288 hammer_done_cursor(&cursor);
1291 * Lookup the obj_id. This should always succeed. If it does not
1292 * the filesystem may be damaged and we return a dummy inode.
1295 ip = hammer_get_inode(&trans, dip, obj_id,
1298 if (error == ENOENT) {
1299 kprintf("HAMMER: WARNING: Missing "
1300 "inode for dirent \"%s\"\n"
1301 "\tobj_id = %016llx, asof=%016llx, lo=%08x\n",
1303 (long long)obj_id, (long long)asof,
1306 ip = hammer_get_dummy_inode(&trans, dip, obj_id,
1311 error = hammer_get_vnode(ip, &vp);
1312 hammer_rel_inode(ip, 0);
1318 cache_setvp(ap->a_nch, vp);
1321 } else if (error == ENOENT) {
1322 cache_setvp(ap->a_nch, NULL);
1325 hammer_done_transaction(&trans);
1326 lwkt_reltoken(&hmp->fs_token);
1331 * hammer_vop_nlookupdotdot { dvp, vpp, cred }
1333 * Locate the parent directory of a directory vnode.
1335 * dvp is referenced but not locked. *vpp must be returned referenced and
1336 * locked. A parent_obj_id of 0 does not necessarily indicate that we are
1337 * at the root, instead it could indicate that the directory we were in was
1340 * NOTE: as-of sequences are not linked into the directory structure. If
1341 * we are at the root with a different asof then the mount point, reload
1342 * the same directory with the mount point's asof. I'm not sure what this
1343 * will do to NFS. We encode ASOF stamps in NFS file handles so it might not
1344 * get confused, but it hasn't been tested.
1348 hammer_vop_nlookupdotdot(struct vop_nlookupdotdot_args *ap)
1350 struct hammer_transaction trans;
1351 struct hammer_inode *dip;
1352 struct hammer_inode *ip;
1354 int64_t parent_obj_id;
1355 u_int32_t parent_obj_localization;
1359 dip = VTOI(ap->a_dvp);
1360 asof = dip->obj_asof;
1364 * Whos are parent? This could be the root of a pseudo-filesystem
1365 * whos parent is in another localization domain.
1367 lwkt_gettoken(&hmp->fs_token);
1368 parent_obj_id = dip->ino_data.parent_obj_id;
1369 if (dip->obj_id == HAMMER_OBJID_ROOT)
1370 parent_obj_localization = dip->ino_data.ext.obj.parent_obj_localization;
1372 parent_obj_localization = dip->obj_localization;
1374 if (parent_obj_id == 0) {
1375 if (dip->obj_id == HAMMER_OBJID_ROOT &&
1376 asof != hmp->asof) {
1377 parent_obj_id = dip->obj_id;
1379 *ap->a_fakename = kmalloc(19, M_TEMP, M_WAITOK);
1380 ksnprintf(*ap->a_fakename, 19, "0x%016llx",
1381 (long long)dip->obj_asof);
1384 lwkt_reltoken(&hmp->fs_token);
1389 hammer_simple_transaction(&trans, hmp);
1390 ++hammer_stats_file_iopsr;
1392 ip = hammer_get_inode(&trans, dip, parent_obj_id,
1393 asof, parent_obj_localization,
1394 dip->flags, &error);
1396 error = hammer_get_vnode(ip, ap->a_vpp);
1397 hammer_rel_inode(ip, 0);
1401 hammer_done_transaction(&trans);
1402 lwkt_reltoken(&hmp->fs_token);
1407 * hammer_vop_nlink { nch, dvp, vp, cred }
1411 hammer_vop_nlink(struct vop_nlink_args *ap)
1413 struct hammer_transaction trans;
1414 struct hammer_inode *dip;
1415 struct hammer_inode *ip;
1416 struct nchandle *nch;
1420 if (ap->a_dvp->v_mount != ap->a_vp->v_mount)
1424 dip = VTOI(ap->a_dvp);
1425 ip = VTOI(ap->a_vp);
1428 if (dip->obj_localization != ip->obj_localization)
1431 if (dip->flags & HAMMER_INODE_RO)
1433 if (ip->flags & HAMMER_INODE_RO)
1435 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0)
1439 * Create a transaction to cover the operations we perform.
1441 lwkt_gettoken(&hmp->fs_token);
1442 hammer_start_transaction(&trans, hmp);
1443 ++hammer_stats_file_iopsw;
1446 * Add the filesystem object to the directory. Note that neither
1447 * dip nor ip are referenced or locked, but their vnodes are
1448 * referenced. This function will bump the inode's link count.
1450 error = hammer_ip_add_directory(&trans, dip,
1451 nch->ncp->nc_name, nch->ncp->nc_nlen,
1458 cache_setunresolved(nch);
1459 cache_setvp(nch, ap->a_vp);
1461 hammer_done_transaction(&trans);
1462 hammer_knote(ap->a_vp, NOTE_LINK);
1463 hammer_knote(ap->a_dvp, NOTE_WRITE);
1464 lwkt_reltoken(&hmp->fs_token);
1469 * hammer_vop_nmkdir { nch, dvp, vpp, cred, vap }
1471 * The operating system has already ensured that the directory entry
1472 * does not exist and done all appropriate namespace locking.
1476 hammer_vop_nmkdir(struct vop_nmkdir_args *ap)
1478 struct hammer_transaction trans;
1479 struct hammer_inode *dip;
1480 struct hammer_inode *nip;
1481 struct nchandle *nch;
1486 dip = VTOI(ap->a_dvp);
1489 if (dip->flags & HAMMER_INODE_RO)
1491 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0)
1495 * Create a transaction to cover the operations we perform.
1497 lwkt_gettoken(&hmp->fs_token);
1498 hammer_start_transaction(&trans, hmp);
1499 ++hammer_stats_file_iopsw;
1502 * Create a new filesystem object of the requested type. The
1503 * returned inode will be referenced but not locked.
1505 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
1506 dip, nch->ncp->nc_name, nch->ncp->nc_nlen,
1509 hkprintf("hammer_mkdir error %d\n", error);
1510 hammer_done_transaction(&trans);
1512 lwkt_reltoken(&hmp->fs_token);
1516 * Add the new filesystem object to the directory. This will also
1517 * bump the inode's link count.
1519 error = hammer_ip_add_directory(&trans, dip,
1520 nch->ncp->nc_name, nch->ncp->nc_nlen,
1523 hkprintf("hammer_mkdir (add) error %d\n", error);
1529 hammer_rel_inode(nip, 0);
1532 error = hammer_get_vnode(nip, ap->a_vpp);
1533 hammer_rel_inode(nip, 0);
1535 cache_setunresolved(ap->a_nch);
1536 cache_setvp(ap->a_nch, *ap->a_vpp);
1539 hammer_done_transaction(&trans);
1541 hammer_knote(ap->a_dvp, NOTE_WRITE | NOTE_LINK);
1542 lwkt_reltoken(&hmp->fs_token);
1547 * hammer_vop_nmknod { nch, dvp, vpp, cred, vap }
1549 * The operating system has already ensured that the directory entry
1550 * does not exist and done all appropriate namespace locking.
1554 hammer_vop_nmknod(struct vop_nmknod_args *ap)
1556 struct hammer_transaction trans;
1557 struct hammer_inode *dip;
1558 struct hammer_inode *nip;
1559 struct nchandle *nch;
1564 dip = VTOI(ap->a_dvp);
1567 if (dip->flags & HAMMER_INODE_RO)
1569 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0)
1573 * Create a transaction to cover the operations we perform.
1575 lwkt_gettoken(&hmp->fs_token);
1576 hammer_start_transaction(&trans, hmp);
1577 ++hammer_stats_file_iopsw;
1580 * Create a new filesystem object of the requested type. The
1581 * returned inode will be referenced but not locked.
1583 * If mknod specifies a directory a pseudo-fs is created.
1585 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
1586 dip, nch->ncp->nc_name, nch->ncp->nc_nlen,
1589 hammer_done_transaction(&trans);
1591 lwkt_reltoken(&hmp->fs_token);
1596 * Add the new filesystem object to the directory. This will also
1597 * bump the inode's link count.
1599 error = hammer_ip_add_directory(&trans, dip,
1600 nch->ncp->nc_name, nch->ncp->nc_nlen,
1607 hammer_rel_inode(nip, 0);
1610 error = hammer_get_vnode(nip, ap->a_vpp);
1611 hammer_rel_inode(nip, 0);
1613 cache_setunresolved(ap->a_nch);
1614 cache_setvp(ap->a_nch, *ap->a_vpp);
1617 hammer_done_transaction(&trans);
1619 hammer_knote(ap->a_dvp, NOTE_WRITE);
1620 lwkt_reltoken(&hmp->fs_token);
1625 * hammer_vop_open { vp, mode, cred, fp }
1627 * MPSAFE (does not require fs_token)
1631 hammer_vop_open(struct vop_open_args *ap)
1635 ++hammer_stats_file_iopsr;
1636 ip = VTOI(ap->a_vp);
1638 if ((ap->a_mode & FWRITE) && (ip->flags & HAMMER_INODE_RO))
1640 return(vop_stdopen(ap));
1644 * hammer_vop_print { vp }
1648 hammer_vop_print(struct vop_print_args *ap)
1654 * hammer_vop_readdir { vp, uio, cred, *eofflag, *ncookies, off_t **cookies }
1658 hammer_vop_readdir(struct vop_readdir_args *ap)
1660 struct hammer_transaction trans;
1661 struct hammer_cursor cursor;
1662 struct hammer_inode *ip;
1665 hammer_base_elm_t base;
1674 ++hammer_stats_file_iopsr;
1675 ip = VTOI(ap->a_vp);
1677 saveoff = uio->uio_offset;
1680 if (ap->a_ncookies) {
1681 ncookies = uio->uio_resid / 16 + 1;
1682 if (ncookies > 1024)
1684 cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
1692 lwkt_gettoken(&hmp->fs_token);
1693 hammer_simple_transaction(&trans, hmp);
1696 * Handle artificial entries
1698 * It should be noted that the minimum value for a directory
1699 * hash key on-media is 0x0000000100000000, so we can use anything
1700 * less then that to represent our 'special' key space.
1704 r = vop_write_dirent(&error, uio, ip->obj_id, DT_DIR, 1, ".");
1708 cookies[cookie_index] = saveoff;
1711 if (cookie_index == ncookies)
1715 if (ip->ino_data.parent_obj_id) {
1716 r = vop_write_dirent(&error, uio,
1717 ip->ino_data.parent_obj_id,
1720 r = vop_write_dirent(&error, uio,
1721 ip->obj_id, DT_DIR, 2, "..");
1726 cookies[cookie_index] = saveoff;
1729 if (cookie_index == ncookies)
1734 * Key range (begin and end inclusive) to scan. Directory keys
1735 * directly translate to a 64 bit 'seek' position.
1737 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
1738 cursor.key_beg.localization = ip->obj_localization +
1739 hammer_dir_localization(ip);
1740 cursor.key_beg.obj_id = ip->obj_id;
1741 cursor.key_beg.create_tid = 0;
1742 cursor.key_beg.delete_tid = 0;
1743 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1744 cursor.key_beg.obj_type = 0;
1745 cursor.key_beg.key = saveoff;
1747 cursor.key_end = cursor.key_beg;
1748 cursor.key_end.key = HAMMER_MAX_KEY;
1749 cursor.asof = ip->obj_asof;
1750 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
1752 error = hammer_ip_first(&cursor);
1754 while (error == 0) {
1755 error = hammer_ip_resolve_data(&cursor);
1758 base = &cursor.leaf->base;
1759 saveoff = base->key;
1760 KKASSERT(cursor.leaf->data_len > HAMMER_ENTRY_NAME_OFF);
1762 if (base->obj_id != ip->obj_id)
1763 panic("readdir: bad record at %p", cursor.node);
1766 * Convert pseudo-filesystems into softlinks
1768 dtype = hammer_get_dtype(cursor.leaf->base.obj_type);
1769 r = vop_write_dirent(
1770 &error, uio, cursor.data->entry.obj_id,
1772 cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF ,
1773 (void *)cursor.data->entry.name);
1778 cookies[cookie_index] = base->key;
1780 if (cookie_index == ncookies)
1782 error = hammer_ip_next(&cursor);
1784 hammer_done_cursor(&cursor);
1787 hammer_done_transaction(&trans);
1790 *ap->a_eofflag = (error == ENOENT);
1791 uio->uio_offset = saveoff;
1792 if (error && cookie_index == 0) {
1793 if (error == ENOENT)
1796 kfree(cookies, M_TEMP);
1797 *ap->a_ncookies = 0;
1798 *ap->a_cookies = NULL;
1801 if (error == ENOENT)
1804 *ap->a_ncookies = cookie_index;
1805 *ap->a_cookies = cookies;
1808 lwkt_reltoken(&hmp->fs_token);
1813 * hammer_vop_readlink { vp, uio, cred }
1817 hammer_vop_readlink(struct vop_readlink_args *ap)
1819 struct hammer_transaction trans;
1820 struct hammer_cursor cursor;
1821 struct hammer_inode *ip;
1824 u_int32_t localization;
1825 hammer_pseudofs_inmem_t pfsm;
1828 ip = VTOI(ap->a_vp);
1831 lwkt_gettoken(&hmp->fs_token);
1834 * Shortcut if the symlink data was stuffed into ino_data.
1836 * Also expand special "@@PFS%05d" softlinks (expansion only
1837 * occurs for non-historical (current) accesses made from the
1838 * primary filesystem).
1840 if (ip->ino_data.size <= HAMMER_INODE_BASESYMLEN) {
1844 ptr = ip->ino_data.ext.symlink;
1845 bytes = (int)ip->ino_data.size;
1847 ip->obj_asof == HAMMER_MAX_TID &&
1848 ip->obj_localization == 0 &&
1849 strncmp(ptr, "@@PFS", 5) == 0) {
1850 hammer_simple_transaction(&trans, hmp);
1851 bcopy(ptr + 5, buf, 5);
1853 localization = strtoul(buf, NULL, 10) << 16;
1854 pfsm = hammer_load_pseudofs(&trans, localization,
1857 if (pfsm->pfsd.mirror_flags &
1858 HAMMER_PFSD_SLAVE) {
1859 /* vap->va_size == 26 */
1860 ksnprintf(buf, sizeof(buf),
1862 (long long)pfsm->pfsd.sync_end_tid,
1863 localization >> 16);
1865 /* vap->va_size == 10 */
1866 ksnprintf(buf, sizeof(buf),
1868 localization >> 16);
1870 ksnprintf(buf, sizeof(buf),
1872 (long long)HAMMER_MAX_TID,
1873 localization >> 16);
1877 bytes = strlen(buf);
1880 hammer_rel_pseudofs(hmp, pfsm);
1881 hammer_done_transaction(&trans);
1883 error = uiomove(ptr, bytes, ap->a_uio);
1884 lwkt_reltoken(&hmp->fs_token);
1891 hammer_simple_transaction(&trans, hmp);
1892 ++hammer_stats_file_iopsr;
1893 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
1896 * Key range (begin and end inclusive) to scan. Directory keys
1897 * directly translate to a 64 bit 'seek' position.
1899 cursor.key_beg.localization = ip->obj_localization +
1900 HAMMER_LOCALIZE_MISC;
1901 cursor.key_beg.obj_id = ip->obj_id;
1902 cursor.key_beg.create_tid = 0;
1903 cursor.key_beg.delete_tid = 0;
1904 cursor.key_beg.rec_type = HAMMER_RECTYPE_FIX;
1905 cursor.key_beg.obj_type = 0;
1906 cursor.key_beg.key = HAMMER_FIXKEY_SYMLINK;
1907 cursor.asof = ip->obj_asof;
1908 cursor.flags |= HAMMER_CURSOR_ASOF;
1910 error = hammer_ip_lookup(&cursor);
1912 error = hammer_ip_resolve_data(&cursor);
1914 KKASSERT(cursor.leaf->data_len >=
1915 HAMMER_SYMLINK_NAME_OFF);
1916 error = uiomove(cursor.data->symlink.name,
1917 cursor.leaf->data_len -
1918 HAMMER_SYMLINK_NAME_OFF,
1922 hammer_done_cursor(&cursor);
1923 hammer_done_transaction(&trans);
1924 lwkt_reltoken(&hmp->fs_token);
1929 * hammer_vop_nremove { nch, dvp, cred }
1933 hammer_vop_nremove(struct vop_nremove_args *ap)
1935 struct hammer_transaction trans;
1936 struct hammer_inode *dip;
1940 dip = VTOI(ap->a_dvp);
1943 if (hammer_nohistory(dip) == 0 &&
1944 (error = hammer_checkspace(hmp, HAMMER_CHKSPC_REMOVE)) != 0) {
1948 lwkt_gettoken(&hmp->fs_token);
1949 hammer_start_transaction(&trans, hmp);
1950 ++hammer_stats_file_iopsw;
1951 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp, ap->a_cred, 0, 0);
1952 hammer_done_transaction(&trans);
1954 hammer_knote(ap->a_dvp, NOTE_WRITE);
1955 lwkt_reltoken(&hmp->fs_token);
1960 * hammer_vop_nrename { fnch, tnch, fdvp, tdvp, cred }
1964 hammer_vop_nrename(struct vop_nrename_args *ap)
1966 struct hammer_transaction trans;
1967 struct namecache *fncp;
1968 struct namecache *tncp;
1969 struct hammer_inode *fdip;
1970 struct hammer_inode *tdip;
1971 struct hammer_inode *ip;
1973 struct hammer_cursor cursor;
1975 u_int32_t max_iterations;
1978 if (ap->a_fdvp->v_mount != ap->a_tdvp->v_mount)
1980 if (ap->a_fdvp->v_mount != ap->a_fnch->ncp->nc_vp->v_mount)
1983 fdip = VTOI(ap->a_fdvp);
1984 tdip = VTOI(ap->a_tdvp);
1985 fncp = ap->a_fnch->ncp;
1986 tncp = ap->a_tnch->ncp;
1987 ip = VTOI(fncp->nc_vp);
1988 KKASSERT(ip != NULL);
1992 if (fdip->obj_localization != tdip->obj_localization)
1994 if (fdip->obj_localization != ip->obj_localization)
1997 if (fdip->flags & HAMMER_INODE_RO)
1999 if (tdip->flags & HAMMER_INODE_RO)
2001 if (ip->flags & HAMMER_INODE_RO)
2003 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0)
2006 lwkt_gettoken(&hmp->fs_token);
2007 hammer_start_transaction(&trans, hmp);
2008 ++hammer_stats_file_iopsw;
2011 * Remove tncp from the target directory and then link ip as
2012 * tncp. XXX pass trans to dounlink
2014 * Force the inode sync-time to match the transaction so it is
2015 * in-sync with the creation of the target directory entry.
2017 error = hammer_dounlink(&trans, ap->a_tnch, ap->a_tdvp,
2019 if (error == 0 || error == ENOENT) {
2020 error = hammer_ip_add_directory(&trans, tdip,
2021 tncp->nc_name, tncp->nc_nlen,
2024 ip->ino_data.parent_obj_id = tdip->obj_id;
2025 ip->ino_data.ctime = trans.time;
2026 hammer_modify_inode(&trans, ip, HAMMER_INODE_DDIRTY);
2030 goto failed; /* XXX */
2033 * Locate the record in the originating directory and remove it.
2035 * Calculate the namekey and setup the key range for the scan. This
2036 * works kinda like a chained hash table where the lower 32 bits
2037 * of the namekey synthesize the chain.
2039 * The key range is inclusive of both key_beg and key_end.
2041 namekey = hammer_directory_namekey(fdip, fncp->nc_name, fncp->nc_nlen,
2044 hammer_init_cursor(&trans, &cursor, &fdip->cache[1], fdip);
2045 cursor.key_beg.localization = fdip->obj_localization +
2046 hammer_dir_localization(fdip);
2047 cursor.key_beg.obj_id = fdip->obj_id;
2048 cursor.key_beg.key = namekey;
2049 cursor.key_beg.create_tid = 0;
2050 cursor.key_beg.delete_tid = 0;
2051 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
2052 cursor.key_beg.obj_type = 0;
2054 cursor.key_end = cursor.key_beg;
2055 cursor.key_end.key += max_iterations;
2056 cursor.asof = fdip->obj_asof;
2057 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
2060 * Scan all matching records (the chain), locate the one matching
2061 * the requested path component.
2063 * The hammer_ip_*() functions merge in-memory records with on-disk
2064 * records for the purposes of the search.
2066 error = hammer_ip_first(&cursor);
2067 while (error == 0) {
2068 if (hammer_ip_resolve_data(&cursor) != 0)
2070 nlen = cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF;
2072 if (fncp->nc_nlen == nlen &&
2073 bcmp(fncp->nc_name, cursor.data->entry.name, nlen) == 0) {
2076 error = hammer_ip_next(&cursor);
2080 * If all is ok we have to get the inode so we can adjust nlinks.
2082 * WARNING: hammer_ip_del_directory() may have to terminate the
2083 * cursor to avoid a recursion. It's ok to call hammer_done_cursor()
2087 error = hammer_ip_del_directory(&trans, &cursor, fdip, ip);
2090 * XXX A deadlock here will break rename's atomicy for the purposes
2091 * of crash recovery.
2093 if (error == EDEADLK) {
2094 hammer_done_cursor(&cursor);
2099 * Cleanup and tell the kernel that the rename succeeded.
2101 * NOTE: ip->vp, if non-NULL, cannot be directly referenced
2102 * without formally acquiring the vp since the vp might
2103 * have zero refs on it, or in the middle of a reclaim,
2106 hammer_done_cursor(&cursor);
2108 cache_rename(ap->a_fnch, ap->a_tnch);
2109 hammer_knote(ap->a_fdvp, NOTE_WRITE);
2110 hammer_knote(ap->a_tdvp, NOTE_WRITE);
2114 error = hammer_get_vnode(ip, &vp);
2115 if (error == 0 && vp) {
2117 hammer_knote(ip->vp, NOTE_RENAME);
2121 kprintf("Debug: HAMMER ip/vp race2 avoided\n");
2126 hammer_done_transaction(&trans);
2127 lwkt_reltoken(&hmp->fs_token);
2132 * hammer_vop_nrmdir { nch, dvp, cred }
2136 hammer_vop_nrmdir(struct vop_nrmdir_args *ap)
2138 struct hammer_transaction trans;
2139 struct hammer_inode *dip;
2143 dip = VTOI(ap->a_dvp);
2146 if (hammer_nohistory(dip) == 0 &&
2147 (error = hammer_checkspace(hmp, HAMMER_CHKSPC_REMOVE)) != 0) {
2151 lwkt_gettoken(&hmp->fs_token);
2152 hammer_start_transaction(&trans, hmp);
2153 ++hammer_stats_file_iopsw;
2154 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp, ap->a_cred, 0, 1);
2155 hammer_done_transaction(&trans);
2157 hammer_knote(ap->a_dvp, NOTE_WRITE | NOTE_LINK);
2158 lwkt_reltoken(&hmp->fs_token);
2163 * hammer_vop_markatime { vp, cred }
2167 hammer_vop_markatime(struct vop_markatime_args *ap)
2169 struct hammer_transaction trans;
2170 struct hammer_inode *ip;
2173 ip = VTOI(ap->a_vp);
2174 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
2176 if (ip->flags & HAMMER_INODE_RO)
2179 if (hmp->mp->mnt_flag & MNT_NOATIME)
2181 lwkt_gettoken(&hmp->fs_token);
2182 hammer_start_transaction(&trans, hmp);
2183 ++hammer_stats_file_iopsw;
2185 ip->ino_data.atime = trans.time;
2186 hammer_modify_inode(&trans, ip, HAMMER_INODE_ATIME);
2187 hammer_done_transaction(&trans);
2188 hammer_knote(ap->a_vp, NOTE_ATTRIB);
2189 lwkt_reltoken(&hmp->fs_token);
2194 * hammer_vop_setattr { vp, vap, cred }
2198 hammer_vop_setattr(struct vop_setattr_args *ap)
2200 struct hammer_transaction trans;
2201 struct hammer_inode *ip;
2210 int64_t aligned_size;
2215 ip = ap->a_vp->v_data;
2220 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
2222 if (ip->flags & HAMMER_INODE_RO)
2224 if (hammer_nohistory(ip) == 0 &&
2225 (error = hammer_checkspace(hmp, HAMMER_CHKSPC_REMOVE)) != 0) {
2229 lwkt_gettoken(&hmp->fs_token);
2230 hammer_start_transaction(&trans, hmp);
2231 ++hammer_stats_file_iopsw;
2234 if (vap->va_flags != VNOVAL) {
2235 flags = ip->ino_data.uflags;
2236 error = vop_helper_setattr_flags(&flags, vap->va_flags,
2237 hammer_to_unix_xid(&ip->ino_data.uid),
2240 if (ip->ino_data.uflags != flags) {
2241 ip->ino_data.uflags = flags;
2242 ip->ino_data.ctime = trans.time;
2243 modflags |= HAMMER_INODE_DDIRTY;
2244 kflags |= NOTE_ATTRIB;
2246 if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) {
2253 if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) {
2257 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
2258 mode_t cur_mode = ip->ino_data.mode;
2259 uid_t cur_uid = hammer_to_unix_xid(&ip->ino_data.uid);
2260 gid_t cur_gid = hammer_to_unix_xid(&ip->ino_data.gid);
2264 error = vop_helper_chown(ap->a_vp, vap->va_uid, vap->va_gid,
2266 &cur_uid, &cur_gid, &cur_mode);
2268 hammer_guid_to_uuid(&uuid_uid, cur_uid);
2269 hammer_guid_to_uuid(&uuid_gid, cur_gid);
2270 if (bcmp(&uuid_uid, &ip->ino_data.uid,
2271 sizeof(uuid_uid)) ||
2272 bcmp(&uuid_gid, &ip->ino_data.gid,
2273 sizeof(uuid_gid)) ||
2274 ip->ino_data.mode != cur_mode
2276 ip->ino_data.uid = uuid_uid;
2277 ip->ino_data.gid = uuid_gid;
2278 ip->ino_data.mode = cur_mode;
2279 ip->ino_data.ctime = trans.time;
2280 modflags |= HAMMER_INODE_DDIRTY;
2282 kflags |= NOTE_ATTRIB;
2285 while (vap->va_size != VNOVAL && ip->ino_data.size != vap->va_size) {
2286 switch(ap->a_vp->v_type) {
2288 if (vap->va_size == ip->ino_data.size)
2292 * Log the operation if in fast-fsync mode or if
2293 * there are unterminated redo write records present.
2295 * The second check is needed so the recovery code
2296 * properly truncates write redos even if nominal
2297 * REDO operations is turned off due to excessive
2298 * writes, because the related records might be
2299 * destroyed and never lay down a TERM_WRITE.
2301 if ((ip->flags & HAMMER_INODE_REDO) ||
2302 (ip->flags & HAMMER_INODE_RDIRTY)) {
2303 error = hammer_generate_redo(&trans, ip,
2308 blksize = hammer_blocksize(vap->va_size);
2311 * XXX break atomicy, we can deadlock the backend
2312 * if we do not release the lock. Probably not a
2315 if (vap->va_size < ip->ino_data.size) {
2316 nvtruncbuf(ap->a_vp, vap->va_size,
2318 hammer_blockoff(vap->va_size),
2321 kflags |= NOTE_WRITE;
2323 nvextendbuf(ap->a_vp,
2326 hammer_blocksize(ip->ino_data.size),
2327 hammer_blocksize(vap->va_size),
2328 hammer_blockoff(ip->ino_data.size),
2329 hammer_blockoff(vap->va_size),
2332 kflags |= NOTE_WRITE | NOTE_EXTEND;
2334 ip->ino_data.size = vap->va_size;
2335 ip->ino_data.mtime = trans.time;
2336 /* XXX safe to use SDIRTY instead of DDIRTY here? */
2337 modflags |= HAMMER_INODE_MTIME | HAMMER_INODE_DDIRTY;
2340 * On-media truncation is cached in the inode until
2341 * the inode is synchronized. We must immediately
2342 * handle any frontend records.
2345 hammer_ip_frontend_trunc(ip, vap->va_size);
2346 #ifdef DEBUG_TRUNCATE
2347 if (HammerTruncIp == NULL)
2350 if ((ip->flags & HAMMER_INODE_TRUNCATED) == 0) {
2351 ip->flags |= HAMMER_INODE_TRUNCATED;
2352 ip->trunc_off = vap->va_size;
2353 #ifdef DEBUG_TRUNCATE
2354 if (ip == HammerTruncIp)
2355 kprintf("truncate1 %016llx\n",
2356 (long long)ip->trunc_off);
2358 } else if (ip->trunc_off > vap->va_size) {
2359 ip->trunc_off = vap->va_size;
2360 #ifdef DEBUG_TRUNCATE
2361 if (ip == HammerTruncIp)
2362 kprintf("truncate2 %016llx\n",
2363 (long long)ip->trunc_off);
2366 #ifdef DEBUG_TRUNCATE
2367 if (ip == HammerTruncIp)
2368 kprintf("truncate3 %016llx (ignored)\n",
2369 (long long)vap->va_size);
2376 * When truncating, nvtruncbuf() may have cleaned out
2377 * a portion of the last block on-disk in the buffer
2378 * cache. We must clean out any frontend records
2379 * for blocks beyond the new last block.
2381 aligned_size = (vap->va_size + (blksize - 1)) &
2382 ~(int64_t)(blksize - 1);
2383 if (truncating && vap->va_size < aligned_size) {
2384 aligned_size -= blksize;
2385 hammer_ip_frontend_trunc(ip, aligned_size);
2390 if ((ip->flags & HAMMER_INODE_TRUNCATED) == 0) {
2391 ip->flags |= HAMMER_INODE_TRUNCATED;
2392 ip->trunc_off = vap->va_size;
2393 } else if (ip->trunc_off > vap->va_size) {
2394 ip->trunc_off = vap->va_size;
2396 hammer_ip_frontend_trunc(ip, vap->va_size);
2397 ip->ino_data.size = vap->va_size;
2398 ip->ino_data.mtime = trans.time;
2399 modflags |= HAMMER_INODE_MTIME | HAMMER_INODE_DDIRTY;
2400 kflags |= NOTE_ATTRIB;
2408 if (vap->va_atime.tv_sec != VNOVAL) {
2409 ip->ino_data.atime = hammer_timespec_to_time(&vap->va_atime);
2410 modflags |= HAMMER_INODE_ATIME;
2411 kflags |= NOTE_ATTRIB;
2413 if (vap->va_mtime.tv_sec != VNOVAL) {
2414 ip->ino_data.mtime = hammer_timespec_to_time(&vap->va_mtime);
2415 modflags |= HAMMER_INODE_MTIME;
2416 kflags |= NOTE_ATTRIB;
2418 if (vap->va_mode != (mode_t)VNOVAL) {
2419 mode_t cur_mode = ip->ino_data.mode;
2420 uid_t cur_uid = hammer_to_unix_xid(&ip->ino_data.uid);
2421 gid_t cur_gid = hammer_to_unix_xid(&ip->ino_data.gid);
2423 error = vop_helper_chmod(ap->a_vp, vap->va_mode, ap->a_cred,
2424 cur_uid, cur_gid, &cur_mode);
2425 if (error == 0 && ip->ino_data.mode != cur_mode) {
2426 ip->ino_data.mode = cur_mode;
2427 ip->ino_data.ctime = trans.time;
2428 modflags |= HAMMER_INODE_DDIRTY;
2429 kflags |= NOTE_ATTRIB;
2434 hammer_modify_inode(&trans, ip, modflags);
2435 hammer_done_transaction(&trans);
2436 hammer_knote(ap->a_vp, kflags);
2437 lwkt_reltoken(&hmp->fs_token);
2442 * hammer_vop_nsymlink { nch, dvp, vpp, cred, vap, target }
2446 hammer_vop_nsymlink(struct vop_nsymlink_args *ap)
2448 struct hammer_transaction trans;
2449 struct hammer_inode *dip;
2450 struct hammer_inode *nip;
2451 hammer_record_t record;
2452 struct nchandle *nch;
2457 ap->a_vap->va_type = VLNK;
2460 dip = VTOI(ap->a_dvp);
2463 if (dip->flags & HAMMER_INODE_RO)
2465 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0)
2469 * Create a transaction to cover the operations we perform.
2471 lwkt_gettoken(&hmp->fs_token);
2472 hammer_start_transaction(&trans, hmp);
2473 ++hammer_stats_file_iopsw;
2476 * Create a new filesystem object of the requested type. The
2477 * returned inode will be referenced but not locked.
2480 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
2481 dip, nch->ncp->nc_name, nch->ncp->nc_nlen,
2484 hammer_done_transaction(&trans);
2486 lwkt_reltoken(&hmp->fs_token);
2491 * Add a record representing the symlink. symlink stores the link
2492 * as pure data, not a string, and is no \0 terminated.
2495 bytes = strlen(ap->a_target);
2497 if (bytes <= HAMMER_INODE_BASESYMLEN) {
2498 bcopy(ap->a_target, nip->ino_data.ext.symlink, bytes);
2500 record = hammer_alloc_mem_record(nip, bytes);
2501 record->type = HAMMER_MEM_RECORD_GENERAL;
2503 record->leaf.base.localization = nip->obj_localization +
2504 HAMMER_LOCALIZE_MISC;
2505 record->leaf.base.key = HAMMER_FIXKEY_SYMLINK;
2506 record->leaf.base.rec_type = HAMMER_RECTYPE_FIX;
2507 record->leaf.data_len = bytes;
2508 KKASSERT(HAMMER_SYMLINK_NAME_OFF == 0);
2509 bcopy(ap->a_target, record->data->symlink.name, bytes);
2510 error = hammer_ip_add_record(&trans, record);
2514 * Set the file size to the length of the link.
2517 nip->ino_data.size = bytes;
2518 hammer_modify_inode(&trans, nip, HAMMER_INODE_DDIRTY);
2522 error = hammer_ip_add_directory(&trans, dip, nch->ncp->nc_name,
2523 nch->ncp->nc_nlen, nip);
2529 hammer_rel_inode(nip, 0);
2532 error = hammer_get_vnode(nip, ap->a_vpp);
2533 hammer_rel_inode(nip, 0);
2535 cache_setunresolved(ap->a_nch);
2536 cache_setvp(ap->a_nch, *ap->a_vpp);
2537 hammer_knote(ap->a_dvp, NOTE_WRITE);
2540 hammer_done_transaction(&trans);
2541 lwkt_reltoken(&hmp->fs_token);
2546 * hammer_vop_nwhiteout { nch, dvp, cred, flags }
2550 hammer_vop_nwhiteout(struct vop_nwhiteout_args *ap)
2552 struct hammer_transaction trans;
2553 struct hammer_inode *dip;
2557 dip = VTOI(ap->a_dvp);
2560 if (hammer_nohistory(dip) == 0 &&
2561 (error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0) {
2565 lwkt_gettoken(&hmp->fs_token);
2566 hammer_start_transaction(&trans, hmp);
2567 ++hammer_stats_file_iopsw;
2568 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp,
2569 ap->a_cred, ap->a_flags, -1);
2570 hammer_done_transaction(&trans);
2571 lwkt_reltoken(&hmp->fs_token);
2577 * hammer_vop_ioctl { vp, command, data, fflag, cred }
2581 hammer_vop_ioctl(struct vop_ioctl_args *ap)
2583 struct hammer_inode *ip = ap->a_vp->v_data;
2584 hammer_mount_t hmp = ip->hmp;
2587 ++hammer_stats_file_iopsr;
2588 lwkt_gettoken(&hmp->fs_token);
2589 error = hammer_ioctl(ip, ap->a_command, ap->a_data,
2590 ap->a_fflag, ap->a_cred);
2591 lwkt_reltoken(&hmp->fs_token);
2597 hammer_vop_mountctl(struct vop_mountctl_args *ap)
2599 static const struct mountctl_opt extraopt[] = {
2600 { HMNT_NOHISTORY, "nohistory" },
2601 { HMNT_MASTERID, "master" },
2605 struct hammer_mount *hmp;
2612 mp = ap->a_head.a_ops->head.vv_mount;
2613 KKASSERT(mp->mnt_data != NULL);
2614 hmp = (struct hammer_mount *)mp->mnt_data;
2616 lwkt_gettoken(&hmp->fs_token);
2619 case MOUNTCTL_SET_EXPORT:
2620 if (ap->a_ctllen != sizeof(struct export_args))
2623 error = hammer_vfs_export(mp, ap->a_op,
2624 (const struct export_args *)ap->a_ctl);
2626 case MOUNTCTL_MOUNTFLAGS:
2629 * Call standard mountctl VOP function
2630 * so we get user mount flags.
2632 error = vop_stdmountctl(ap);
2636 usedbytes = *ap->a_res;
2638 if (usedbytes > 0 && usedbytes < ap->a_buflen) {
2639 usedbytes += vfs_flagstostr(hmp->hflags, extraopt,
2641 ap->a_buflen - usedbytes,
2645 *ap->a_res += usedbytes;
2649 error = vop_stdmountctl(ap);
2652 lwkt_reltoken(&hmp->fs_token);
2657 * hammer_vop_strategy { vp, bio }
2659 * Strategy call, used for regular file read & write only. Note that the
2660 * bp may represent a cluster.
2662 * To simplify operation and allow better optimizations in the future,
2663 * this code does not make any assumptions with regards to buffer alignment
2668 hammer_vop_strategy(struct vop_strategy_args *ap)
2673 bp = ap->a_bio->bio_buf;
2677 error = hammer_vop_strategy_read(ap);
2680 error = hammer_vop_strategy_write(ap);
2683 bp->b_error = error = EINVAL;
2684 bp->b_flags |= B_ERROR;
2689 /* hammer_dump_dedup_cache(((hammer_inode_t)ap->a_vp->v_data)->hmp); */
2695 * Read from a regular file. Iterate the related records and fill in the
2696 * BIO/BUF. Gaps are zero-filled.
2698 * The support code in hammer_object.c should be used to deal with mixed
2699 * in-memory and on-disk records.
2701 * NOTE: Can be called from the cluster code with an oversized buf.
2707 hammer_vop_strategy_read(struct vop_strategy_args *ap)
2709 struct hammer_transaction trans;
2710 struct hammer_inode *ip;
2711 struct hammer_inode *dip;
2713 struct hammer_cursor cursor;
2714 hammer_base_elm_t base;
2715 hammer_off_t disk_offset;
2730 ip = ap->a_vp->v_data;
2734 * The zone-2 disk offset may have been set by the cluster code via
2735 * a BMAP operation, or else should be NOOFFSET.
2737 * Checking the high bits for a match against zone-2 should suffice.
2739 * In cases where a lot of data duplication is present it may be
2740 * more beneficial to drop through and doubule-buffer through the
2743 nbio = push_bio(bio);
2744 if ((nbio->bio_offset & HAMMER_OFF_ZONE_MASK) ==
2745 HAMMER_ZONE_LARGE_DATA) {
2746 if (hammer_double_buffer == 0) {
2747 lwkt_gettoken(&hmp->fs_token);
2748 error = hammer_io_direct_read(hmp, nbio, NULL);
2749 lwkt_reltoken(&hmp->fs_token);
2754 * Try to shortcut requests for double_buffer mode too.
2755 * Since this mode runs through the device buffer cache
2756 * only compatible buffer sizes (meaning those generated
2757 * by normal filesystem buffers) are legal.
2759 if (hammer_live_dedup == 0 && (bp->b_flags & B_PAGING) == 0) {
2760 error = hammer_io_indirect_read(hmp, nbio, NULL);
2766 * Well, that sucked. Do it the hard way. If all the stars are
2767 * aligned we may still be able to issue a direct-read.
2769 lwkt_gettoken(&hmp->fs_token);
2770 hammer_simple_transaction(&trans, hmp);
2771 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
2774 * Key range (begin and end inclusive) to scan. Note that the key's
2775 * stored in the actual records represent BASE+LEN, not BASE. The
2776 * first record containing bio_offset will have a key > bio_offset.
2778 cursor.key_beg.localization = ip->obj_localization +
2779 HAMMER_LOCALIZE_MISC;
2780 cursor.key_beg.obj_id = ip->obj_id;
2781 cursor.key_beg.create_tid = 0;
2782 cursor.key_beg.delete_tid = 0;
2783 cursor.key_beg.obj_type = 0;
2784 cursor.key_beg.key = bio->bio_offset + 1;
2785 cursor.asof = ip->obj_asof;
2786 cursor.flags |= HAMMER_CURSOR_ASOF;
2788 cursor.key_end = cursor.key_beg;
2789 KKASSERT(ip->ino_data.obj_type == HAMMER_OBJTYPE_REGFILE);
2791 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_DBFILE) {
2792 cursor.key_beg.rec_type = HAMMER_RECTYPE_DB;
2793 cursor.key_end.rec_type = HAMMER_RECTYPE_DB;
2794 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
2798 ran_end = bio->bio_offset + bp->b_bufsize;
2799 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
2800 cursor.key_end.rec_type = HAMMER_RECTYPE_DATA;
2801 tmp64 = ran_end + MAXPHYS + 1; /* work-around GCC-4 bug */
2802 if (tmp64 < ran_end)
2803 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
2805 cursor.key_end.key = ran_end + MAXPHYS + 1;
2807 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
2810 * Set NOSWAPCACHE for cursor data extraction if double buffering
2811 * is disabled or (if the file is not marked cacheable via chflags
2812 * and vm.swapcache_use_chflags is enabled).
2814 if (hammer_double_buffer == 0 ||
2815 ((ap->a_vp->v_flag & VSWAPCACHE) == 0 &&
2816 vm_swapcache_use_chflags)) {
2817 cursor.flags |= HAMMER_CURSOR_NOSWAPCACHE;
2820 error = hammer_ip_first(&cursor);
2823 while (error == 0) {
2825 * Get the base file offset of the record. The key for
2826 * data records is (base + bytes) rather then (base).
2828 base = &cursor.leaf->base;
2829 rec_offset = base->key - cursor.leaf->data_len;
2832 * Calculate the gap, if any, and zero-fill it.
2834 * n is the offset of the start of the record verses our
2835 * current seek offset in the bio.
2837 n = (int)(rec_offset - (bio->bio_offset + boff));
2839 if (n > bp->b_bufsize - boff)
2840 n = bp->b_bufsize - boff;
2841 bzero((char *)bp->b_data + boff, n);
2847 * Calculate the data offset in the record and the number
2848 * of bytes we can copy.
2850 * There are two degenerate cases. First, boff may already
2851 * be at bp->b_bufsize. Secondly, the data offset within
2852 * the record may exceed the record's size.
2856 n = cursor.leaf->data_len - roff;
2858 kprintf("strategy_read: bad n=%d roff=%d\n", n, roff);
2860 } else if (n > bp->b_bufsize - boff) {
2861 n = bp->b_bufsize - boff;
2865 * Deal with cached truncations. This cool bit of code
2866 * allows truncate()/ftruncate() to avoid having to sync
2869 * If the frontend is truncated then all backend records are
2870 * subject to the frontend's truncation.
2872 * If the backend is truncated then backend records on-disk
2873 * (but not in-memory) are subject to the backend's
2874 * truncation. In-memory records owned by the backend
2875 * represent data written after the truncation point on the
2876 * backend and must not be truncated.
2878 * Truncate operations deal with frontend buffer cache
2879 * buffers and frontend-owned in-memory records synchronously.
2881 if (ip->flags & HAMMER_INODE_TRUNCATED) {
2882 if (hammer_cursor_ondisk(&cursor)/* ||
2883 cursor.iprec->flush_state == HAMMER_FST_FLUSH*/) {
2884 if (ip->trunc_off <= rec_offset)
2886 else if (ip->trunc_off < rec_offset + n)
2887 n = (int)(ip->trunc_off - rec_offset);
2890 if (ip->sync_flags & HAMMER_INODE_TRUNCATED) {
2891 if (hammer_cursor_ondisk(&cursor)) {
2892 if (ip->sync_trunc_off <= rec_offset)
2894 else if (ip->sync_trunc_off < rec_offset + n)
2895 n = (int)(ip->sync_trunc_off - rec_offset);
2900 * Try to issue a direct read into our bio if possible,
2901 * otherwise resolve the element data into a hammer_buffer
2904 * The buffer on-disk should be zerod past any real
2905 * truncation point, but may not be for any synthesized
2906 * truncation point from above.
2908 * NOTE: disk_offset is only valid if the cursor data is
2911 disk_offset = cursor.leaf->data_offset + roff;
2912 isdedupable = (boff == 0 && n == bp->b_bufsize &&
2913 hammer_cursor_ondisk(&cursor) &&
2914 ((int)disk_offset & HAMMER_BUFMASK) == 0);
2916 if (isdedupable && hammer_double_buffer == 0) {
2920 KKASSERT((disk_offset & HAMMER_OFF_ZONE_MASK) ==
2921 HAMMER_ZONE_LARGE_DATA);
2922 nbio->bio_offset = disk_offset;
2923 error = hammer_io_direct_read(hmp, nbio, cursor.leaf);
2924 if (hammer_live_dedup && error == 0)
2925 hammer_dedup_cache_add(ip, cursor.leaf);
2927 } else if (isdedupable) {
2929 * Async I/O case for reading from backing store
2930 * and copying the data to the filesystem buffer.
2931 * live-dedup has to verify the data anyway if it
2932 * gets a hit later so we can just add the entry
2935 KKASSERT((disk_offset & HAMMER_OFF_ZONE_MASK) ==
2936 HAMMER_ZONE_LARGE_DATA);
2937 nbio->bio_offset = disk_offset;
2938 if (hammer_live_dedup)
2939 hammer_dedup_cache_add(ip, cursor.leaf);
2940 error = hammer_io_indirect_read(hmp, nbio, cursor.leaf);
2943 error = hammer_ip_resolve_data(&cursor);
2945 if (hammer_live_dedup && isdedupable)
2946 hammer_dedup_cache_add(ip, cursor.leaf);
2947 bcopy((char *)cursor.data + roff,
2948 (char *)bp->b_data + boff, n);
2955 * We have to be sure that the only elements added to the
2956 * dedup cache are those which are already on-media.
2958 if (hammer_live_dedup && hammer_cursor_ondisk(&cursor))
2959 hammer_dedup_cache_add(ip, cursor.leaf);
2962 * Iterate until we have filled the request.
2965 if (boff == bp->b_bufsize)
2967 error = hammer_ip_next(&cursor);
2971 * There may have been a gap after the last record
2973 if (error == ENOENT)
2975 if (error == 0 && boff != bp->b_bufsize) {
2976 KKASSERT(boff < bp->b_bufsize);
2977 bzero((char *)bp->b_data + boff, bp->b_bufsize - boff);
2978 /* boff = bp->b_bufsize; */
2982 * Disallow swapcache operation on the vnode buffer if double
2983 * buffering is enabled, the swapcache will get the data via
2984 * the block device buffer.
2986 if (hammer_double_buffer)
2987 bp->b_flags |= B_NOTMETA;
2993 bp->b_error = error;
2995 bp->b_flags |= B_ERROR;
3000 * Cache the b-tree node for the last data read in cache[1].
3002 * If we hit the file EOF then also cache the node in the
3003 * governing director's cache[3], it will be used to initialize
3004 * the inode's cache[1] for any inodes looked up via the directory.
3006 * This doesn't reduce disk accesses since the B-Tree chain is
3007 * likely cached, but it does reduce cpu overhead when looking
3008 * up file offsets for cpdup/tar/cpio style iterations.
3011 hammer_cache_node(&ip->cache[1], cursor.node);
3012 if (ran_end >= ip->ino_data.size) {
3013 dip = hammer_find_inode(&trans, ip->ino_data.parent_obj_id,
3014 ip->obj_asof, ip->obj_localization);
3016 hammer_cache_node(&dip->cache[3], cursor.node);
3017 hammer_rel_inode(dip, 0);
3020 hammer_done_cursor(&cursor);
3021 hammer_done_transaction(&trans);
3022 lwkt_reltoken(&hmp->fs_token);
3027 * BMAP operation - used to support cluster_read() only.
3029 * (struct vnode *vp, off_t loffset, off_t *doffsetp, int *runp, int *runb)
3031 * This routine may return EOPNOTSUPP if the opration is not supported for
3032 * the specified offset. The contents of the pointer arguments do not
3033 * need to be initialized in that case.
3035 * If a disk address is available and properly aligned return 0 with
3036 * *doffsetp set to the zone-2 address, and *runp / *runb set appropriately
3037 * to the run-length relative to that offset. Callers may assume that
3038 * *doffsetp is valid if 0 is returned, even if *runp is not sufficiently
3039 * large, so return EOPNOTSUPP if it is not sufficiently large.
3043 hammer_vop_bmap(struct vop_bmap_args *ap)
3045 struct hammer_transaction trans;
3046 struct hammer_inode *ip;
3048 struct hammer_cursor cursor;
3049 hammer_base_elm_t base;
3053 int64_t base_offset;
3054 int64_t base_disk_offset;
3055 int64_t last_offset;
3056 hammer_off_t last_disk_offset;
3057 hammer_off_t disk_offset;
3062 ++hammer_stats_file_iopsr;
3063 ip = ap->a_vp->v_data;
3067 * We can only BMAP regular files. We can't BMAP database files,
3070 if (ip->ino_data.obj_type != HAMMER_OBJTYPE_REGFILE)
3074 * bmap is typically called with runp/runb both NULL when used
3075 * for writing. We do not support BMAP for writing atm.
3077 if (ap->a_cmd != BUF_CMD_READ)
3081 * Scan the B-Tree to acquire blockmap addresses, then translate
3084 lwkt_gettoken(&hmp->fs_token);
3085 hammer_simple_transaction(&trans, hmp);
3087 kprintf("bmap_beg %016llx ip->cache %p\n",
3088 (long long)ap->a_loffset, ip->cache[1]);
3090 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
3093 * Key range (begin and end inclusive) to scan. Note that the key's
3094 * stored in the actual records represent BASE+LEN, not BASE. The
3095 * first record containing bio_offset will have a key > bio_offset.
3097 cursor.key_beg.localization = ip->obj_localization +
3098 HAMMER_LOCALIZE_MISC;
3099 cursor.key_beg.obj_id = ip->obj_id;
3100 cursor.key_beg.create_tid = 0;
3101 cursor.key_beg.delete_tid = 0;
3102 cursor.key_beg.obj_type = 0;
3104 cursor.key_beg.key = ap->a_loffset - MAXPHYS + 1;
3106 cursor.key_beg.key = ap->a_loffset + 1;
3107 if (cursor.key_beg.key < 0)
3108 cursor.key_beg.key = 0;
3109 cursor.asof = ip->obj_asof;
3110 cursor.flags |= HAMMER_CURSOR_ASOF;
3112 cursor.key_end = cursor.key_beg;
3113 KKASSERT(ip->ino_data.obj_type == HAMMER_OBJTYPE_REGFILE);
3115 ran_end = ap->a_loffset + MAXPHYS;
3116 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
3117 cursor.key_end.rec_type = HAMMER_RECTYPE_DATA;
3118 tmp64 = ran_end + MAXPHYS + 1; /* work-around GCC-4 bug */
3119 if (tmp64 < ran_end)
3120 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
3122 cursor.key_end.key = ran_end + MAXPHYS + 1;
3124 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
3126 error = hammer_ip_first(&cursor);
3127 base_offset = last_offset = 0;
3128 base_disk_offset = last_disk_offset = 0;
3130 while (error == 0) {
3132 * Get the base file offset of the record. The key for
3133 * data records is (base + bytes) rather then (base).
3135 * NOTE: rec_offset + rec_len may exceed the end-of-file.
3136 * The extra bytes should be zero on-disk and the BMAP op
3137 * should still be ok.
3139 base = &cursor.leaf->base;
3140 rec_offset = base->key - cursor.leaf->data_len;
3141 rec_len = cursor.leaf->data_len;
3144 * Incorporate any cached truncation.
3146 * NOTE: Modifications to rec_len based on synthesized
3147 * truncation points remove the guarantee that any extended
3148 * data on disk is zero (since the truncations may not have
3149 * taken place on-media yet).
3151 if (ip->flags & HAMMER_INODE_TRUNCATED) {
3152 if (hammer_cursor_ondisk(&cursor) ||
3153 cursor.iprec->flush_state == HAMMER_FST_FLUSH) {
3154 if (ip->trunc_off <= rec_offset)
3156 else if (ip->trunc_off < rec_offset + rec_len)
3157 rec_len = (int)(ip->trunc_off - rec_offset);
3160 if (ip->sync_flags & HAMMER_INODE_TRUNCATED) {
3161 if (hammer_cursor_ondisk(&cursor)) {
3162 if (ip->sync_trunc_off <= rec_offset)
3164 else if (ip->sync_trunc_off < rec_offset + rec_len)
3165 rec_len = (int)(ip->sync_trunc_off - rec_offset);
3170 * Accumulate information. If we have hit a discontiguous
3171 * block reset base_offset unless we are already beyond the
3172 * requested offset. If we are, that's it, we stop.
3176 if (hammer_cursor_ondisk(&cursor)) {
3177 disk_offset = cursor.leaf->data_offset;
3178 if (rec_offset != last_offset ||
3179 disk_offset != last_disk_offset) {
3180 if (rec_offset > ap->a_loffset)
3182 base_offset = rec_offset;
3183 base_disk_offset = disk_offset;
3185 last_offset = rec_offset + rec_len;
3186 last_disk_offset = disk_offset + rec_len;
3188 if (hammer_live_dedup)
3189 hammer_dedup_cache_add(ip, cursor.leaf);
3192 error = hammer_ip_next(&cursor);
3196 kprintf("BMAP %016llx: %016llx - %016llx\n",
3197 (long long)ap->a_loffset,
3198 (long long)base_offset,
3199 (long long)last_offset);
3200 kprintf("BMAP %16s: %016llx - %016llx\n", "",
3201 (long long)base_disk_offset,
3202 (long long)last_disk_offset);
3206 hammer_cache_node(&ip->cache[1], cursor.node);
3208 kprintf("bmap_end2 %016llx ip->cache %p\n",
3209 (long long)ap->a_loffset, ip->cache[1]);
3212 hammer_done_cursor(&cursor);
3213 hammer_done_transaction(&trans);
3214 lwkt_reltoken(&hmp->fs_token);
3217 * If we couldn't find any records or the records we did find were
3218 * all behind the requested offset, return failure. A forward
3219 * truncation can leave a hole w/ no on-disk records.
3221 if (last_offset == 0 || last_offset < ap->a_loffset)
3222 return (EOPNOTSUPP);
3225 * Figure out the block size at the requested offset and adjust
3226 * our limits so the cluster_read() does not create inappropriately
3227 * sized buffer cache buffers.
3229 blksize = hammer_blocksize(ap->a_loffset);
3230 if (hammer_blocksize(base_offset) != blksize) {
3231 base_offset = hammer_blockdemarc(base_offset, ap->a_loffset);
3233 if (last_offset != ap->a_loffset &&
3234 hammer_blocksize(last_offset - 1) != blksize) {
3235 last_offset = hammer_blockdemarc(ap->a_loffset,
3240 * Returning EOPNOTSUPP simply prevents the direct-IO optimization
3243 disk_offset = base_disk_offset + (ap->a_loffset - base_offset);
3245 if ((disk_offset & HAMMER_OFF_ZONE_MASK) != HAMMER_ZONE_LARGE_DATA) {
3247 * Only large-data zones can be direct-IOd
3250 } else if ((disk_offset & HAMMER_BUFMASK) ||
3251 (last_offset - ap->a_loffset) < blksize) {
3253 * doffsetp is not aligned or the forward run size does
3254 * not cover a whole buffer, disallow the direct I/O.
3261 *ap->a_doffsetp = disk_offset;
3263 *ap->a_runb = ap->a_loffset - base_offset;
3264 KKASSERT(*ap->a_runb >= 0);
3267 *ap->a_runp = last_offset - ap->a_loffset;
3268 KKASSERT(*ap->a_runp >= 0);
3276 * Write to a regular file. Because this is a strategy call the OS is
3277 * trying to actually get data onto the media.
3281 hammer_vop_strategy_write(struct vop_strategy_args *ap)
3283 hammer_record_t record;
3294 ip = ap->a_vp->v_data;
3297 blksize = hammer_blocksize(bio->bio_offset);
3298 KKASSERT(bp->b_bufsize == blksize);
3300 if (ip->flags & HAMMER_INODE_RO) {
3301 bp->b_error = EROFS;
3302 bp->b_flags |= B_ERROR;
3307 lwkt_gettoken(&hmp->fs_token);
3310 * Disallow swapcache operation on the vnode buffer if double
3311 * buffering is enabled, the swapcache will get the data via
3312 * the block device buffer.
3314 if (hammer_double_buffer)
3315 bp->b_flags |= B_NOTMETA;
3318 * Interlock with inode destruction (no in-kernel or directory
3319 * topology visibility). If we queue new IO while trying to
3320 * destroy the inode we can deadlock the vtrunc call in
3321 * hammer_inode_unloadable_check().
3323 * Besides, there's no point flushing a bp associated with an
3324 * inode that is being destroyed on-media and has no kernel
3327 if ((ip->flags | ip->sync_flags) &
3328 (HAMMER_INODE_DELETING|HAMMER_INODE_DELETED)) {
3331 lwkt_reltoken(&hmp->fs_token);
3336 * Reserve space and issue a direct-write from the front-end.
3337 * NOTE: The direct_io code will hammer_bread/bcopy smaller
3340 * An in-memory record will be installed to reference the storage
3341 * until the flusher can get to it.
3343 * Since we own the high level bio the front-end will not try to
3344 * do a direct-read until the write completes.
3346 * NOTE: The only time we do not reserve a full-sized buffers
3347 * worth of data is if the file is small. We do not try to
3348 * allocate a fragment (from the small-data zone) at the end of
3349 * an otherwise large file as this can lead to wildly separated
3352 KKASSERT((bio->bio_offset & HAMMER_BUFMASK) == 0);
3353 KKASSERT(bio->bio_offset < ip->ino_data.size);
3354 if (bio->bio_offset || ip->ino_data.size > HAMMER_BUFSIZE / 2)
3355 bytes = bp->b_bufsize;
3357 bytes = ((int)ip->ino_data.size + 15) & ~15;
3359 record = hammer_ip_add_bulk(ip, bio->bio_offset, bp->b_data,
3363 * B_VFSFLAG1 indicates that a REDO_WRITE entry was generated
3364 * in hammer_vop_write(). We must flag the record so the proper
3365 * REDO_TERM_WRITE entry is generated during the flush.
3368 if (bp->b_flags & B_VFSFLAG1) {
3369 record->flags |= HAMMER_RECF_REDO;
3370 bp->b_flags &= ~B_VFSFLAG1;
3372 if (record->flags & HAMMER_RECF_DEDUPED) {
3374 hammer_ip_replace_bulk(hmp, record);
3377 hammer_io_direct_write(hmp, bio, record);
3379 if (ip->rsv_recs > 1 && hmp->rsv_recs > hammer_limit_recs)
3380 hammer_flush_inode(ip, 0);
3382 bp->b_bio2.bio_offset = NOOFFSET;
3383 bp->b_error = error;
3384 bp->b_flags |= B_ERROR;
3387 lwkt_reltoken(&hmp->fs_token);
3392 * dounlink - disconnect a directory entry
3394 * XXX whiteout support not really in yet
3397 hammer_dounlink(hammer_transaction_t trans, struct nchandle *nch,
3398 struct vnode *dvp, struct ucred *cred,
3399 int flags, int isdir)
3401 struct namecache *ncp;
3405 struct hammer_cursor cursor;
3407 u_int32_t max_iterations;
3411 * Calculate the namekey and setup the key range for the scan. This
3412 * works kinda like a chained hash table where the lower 32 bits
3413 * of the namekey synthesize the chain.
3415 * The key range is inclusive of both key_beg and key_end.
3421 if (dip->flags & HAMMER_INODE_RO)
3424 namekey = hammer_directory_namekey(dip, ncp->nc_name, ncp->nc_nlen,
3427 hammer_init_cursor(trans, &cursor, &dip->cache[1], dip);
3428 cursor.key_beg.localization = dip->obj_localization +
3429 hammer_dir_localization(dip);
3430 cursor.key_beg.obj_id = dip->obj_id;
3431 cursor.key_beg.key = namekey;
3432 cursor.key_beg.create_tid = 0;
3433 cursor.key_beg.delete_tid = 0;
3434 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
3435 cursor.key_beg.obj_type = 0;
3437 cursor.key_end = cursor.key_beg;
3438 cursor.key_end.key += max_iterations;
3439 cursor.asof = dip->obj_asof;
3440 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
3443 * Scan all matching records (the chain), locate the one matching
3444 * the requested path component. info->last_error contains the
3445 * error code on search termination and could be 0, ENOENT, or
3448 * The hammer_ip_*() functions merge in-memory records with on-disk
3449 * records for the purposes of the search.
3451 error = hammer_ip_first(&cursor);
3453 while (error == 0) {
3454 error = hammer_ip_resolve_data(&cursor);
3457 nlen = cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF;
3459 if (ncp->nc_nlen == nlen &&
3460 bcmp(ncp->nc_name, cursor.data->entry.name, nlen) == 0) {
3463 error = hammer_ip_next(&cursor);
3467 * If all is ok we have to get the inode so we can adjust nlinks.
3468 * To avoid a deadlock with the flusher we must release the inode
3469 * lock on the directory when acquiring the inode for the entry.
3471 * If the target is a directory, it must be empty.
3474 hammer_unlock(&cursor.ip->lock);
3475 ip = hammer_get_inode(trans, dip, cursor.data->entry.obj_id,
3477 cursor.data->entry.localization,
3479 hammer_lock_sh(&cursor.ip->lock);
3480 if (error == ENOENT) {
3481 kprintf("HAMMER: WARNING: Removing "
3482 "dirent w/missing inode \"%s\"\n"
3483 "\tobj_id = %016llx\n",
3485 (long long)cursor.data->entry.obj_id);
3490 * If isdir >= 0 we validate that the entry is or is not a
3491 * directory. If isdir < 0 we don't care.
3493 if (error == 0 && isdir >= 0 && ip) {
3495 ip->ino_data.obj_type != HAMMER_OBJTYPE_DIRECTORY) {
3497 } else if (isdir == 0 &&
3498 ip->ino_data.obj_type == HAMMER_OBJTYPE_DIRECTORY) {
3504 * If we are trying to remove a directory the directory must
3507 * The check directory code can loop and deadlock/retry. Our
3508 * own cursor's node locks must be released to avoid a 3-way
3509 * deadlock with the flusher if the check directory code
3512 * If any changes whatsoever have been made to the cursor
3513 * set EDEADLK and retry.
3515 * WARNING: See warnings in hammer_unlock_cursor()
3518 if (error == 0 && ip && ip->ino_data.obj_type ==
3519 HAMMER_OBJTYPE_DIRECTORY) {
3520 hammer_unlock_cursor(&cursor);
3521 error = hammer_ip_check_directory_empty(trans, ip);
3522 hammer_lock_cursor(&cursor);
3523 if (cursor.flags & HAMMER_CURSOR_RETEST) {
3524 kprintf("HAMMER: Warning: avoided deadlock "
3532 * Delete the directory entry.
3534 * WARNING: hammer_ip_del_directory() may have to terminate
3535 * the cursor to avoid a deadlock. It is ok to call
3536 * hammer_done_cursor() twice.
3539 error = hammer_ip_del_directory(trans, &cursor,
3542 hammer_done_cursor(&cursor);
3544 cache_setunresolved(nch);
3545 cache_setvp(nch, NULL);
3548 * NOTE: ip->vp, if non-NULL, cannot be directly
3549 * referenced without formally acquiring the
3550 * vp since the vp might have zero refs on it,
3551 * or in the middle of a reclaim, etc.
3553 * NOTE: The cache_setunresolved() can rip the vp
3554 * out from under us since the vp may not have
3555 * any refs, in which case ip->vp will be NULL
3558 while (ip && ip->vp) {
3561 error = hammer_get_vnode(ip, &vp);
3562 if (error == 0 && vp) {
3564 hammer_knote(ip->vp, NOTE_DELETE);
3565 cache_inval_vp(ip->vp, CINV_DESTROY);
3569 kprintf("Debug: HAMMER ip/vp race1 avoided\n");
3573 hammer_rel_inode(ip, 0);
3575 hammer_done_cursor(&cursor);
3577 if (error == EDEADLK)
3583 /************************************************************************
3584 * FIFO AND SPECFS OPS *
3585 ************************************************************************
3589 hammer_vop_fifoclose (struct vop_close_args *ap)
3591 /* XXX update itimes */
3592 return (VOCALL(&fifo_vnode_vops, &ap->a_head));
3596 hammer_vop_fiforead (struct vop_read_args *ap)
3600 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
3601 /* XXX update access time */
3606 hammer_vop_fifowrite (struct vop_write_args *ap)
3610 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
3611 /* XXX update access time */
3617 hammer_vop_fifokqfilter(struct vop_kqfilter_args *ap)
3621 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
3623 error = hammer_vop_kqfilter(ap);
3627 /************************************************************************
3629 ************************************************************************
3632 static void filt_hammerdetach(struct knote *kn);
3633 static int filt_hammerread(struct knote *kn, long hint);
3634 static int filt_hammerwrite(struct knote *kn, long hint);
3635 static int filt_hammervnode(struct knote *kn, long hint);
3637 static struct filterops hammerread_filtops =
3638 { FILTEROP_ISFD, NULL, filt_hammerdetach, filt_hammerread };
3639 static struct filterops hammerwrite_filtops =
3640 { FILTEROP_ISFD, NULL, filt_hammerdetach, filt_hammerwrite };
3641 static struct filterops hammervnode_filtops =
3642 { FILTEROP_ISFD, NULL, filt_hammerdetach, filt_hammervnode };
3646 hammer_vop_kqfilter(struct vop_kqfilter_args *ap)
3648 struct vnode *vp = ap->a_vp;
3649 struct knote *kn = ap->a_kn;
3651 switch (kn->kn_filter) {
3653 kn->kn_fop = &hammerread_filtops;
3656 kn->kn_fop = &hammerwrite_filtops;
3659 kn->kn_fop = &hammervnode_filtops;
3662 return (EOPNOTSUPP);
3665 kn->kn_hook = (caddr_t)vp;
3667 knote_insert(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
3673 filt_hammerdetach(struct knote *kn)
3675 struct vnode *vp = (void *)kn->kn_hook;
3677 knote_remove(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
3681 filt_hammerread(struct knote *kn, long hint)
3683 struct vnode *vp = (void *)kn->kn_hook;
3684 hammer_inode_t ip = VTOI(vp);
3685 hammer_mount_t hmp = ip->hmp;
3688 if (hint == NOTE_REVOKE) {
3689 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
3692 lwkt_gettoken(&hmp->fs_token); /* XXX use per-ip-token */
3693 off = ip->ino_data.size - kn->kn_fp->f_offset;
3694 kn->kn_data = (off < INTPTR_MAX) ? off : INTPTR_MAX;
3695 lwkt_reltoken(&hmp->fs_token);
3696 if (kn->kn_sfflags & NOTE_OLDAPI)
3698 return (kn->kn_data != 0);
3702 filt_hammerwrite(struct knote *kn, long hint)
3704 if (hint == NOTE_REVOKE)
3705 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
3711 filt_hammervnode(struct knote *kn, long hint)
3713 if (kn->kn_sfflags & hint)
3714 kn->kn_fflags |= hint;
3715 if (hint == NOTE_REVOKE) {
3716 kn->kn_flags |= (EV_EOF | EV_NODATA);
3719 return (kn->kn_fflags != 0);