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 ++hammer_count_fsyncs;
293 vfsync(ap->a_vp, waitfor, 1, NULL, NULL);
294 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
295 if (waitfor == MNT_WAIT) {
297 hammer_wait_inode(ip);
298 vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY);
300 lwkt_reltoken(&hmp->fs_token);
305 * hammer_vop_read { vp, uio, ioflag, cred }
307 * MPSAFE (for the cache safe does not require fs_token)
311 hammer_vop_read(struct vop_read_args *ap)
313 struct hammer_transaction trans;
327 if (ap->a_vp->v_type != VREG)
335 * Allow the UIO's size to override the sequential heuristic.
337 blksize = hammer_blocksize(uio->uio_offset);
338 seqcount = (uio->uio_resid + (BKVASIZE - 1)) / BKVASIZE;
339 ioseqcount = (ap->a_ioflag >> 16);
340 if (seqcount < ioseqcount)
341 seqcount = ioseqcount;
344 * If reading or writing a huge amount of data we have to break
345 * atomicy and allow the operation to be interrupted by a signal
346 * or it can DOS the machine.
348 bigread = (uio->uio_resid > 100 * 1024 * 1024);
352 * Access the data typically in HAMMER_BUFSIZE blocks via the
353 * buffer cache, but HAMMER may use a variable block size based
356 * XXX Temporary hack, delay the start transaction while we remain
357 * MPSAFE. NOTE: ino_data.size cannot change while vnode is
360 while (uio->uio_resid > 0 && uio->uio_offset < ip->ino_data.size) {
364 blksize = hammer_blocksize(uio->uio_offset);
365 offset = (int)uio->uio_offset & (blksize - 1);
366 base_offset = uio->uio_offset - offset;
368 if (bigread && (error = hammer_signal_check(ip->hmp)) != 0)
374 bp = getcacheblk(ap->a_vp, base_offset, blksize);
379 if (ap->a_ioflag & IO_NRDELAY)
380 return (EWOULDBLOCK);
386 if (got_fstoken == 0) {
387 lwkt_gettoken(&hmp->fs_token);
389 hammer_start_transaction(&trans, ip->hmp);
392 if (hammer_cluster_enable) {
394 * Use file_limit to prevent cluster_read() from
395 * creating buffers of the wrong block size past
398 file_limit = ip->ino_data.size;
399 if (base_offset < HAMMER_XDEMARC &&
400 file_limit > HAMMER_XDEMARC) {
401 file_limit = HAMMER_XDEMARC;
403 error = cluster_read(ap->a_vp,
404 file_limit, base_offset,
405 blksize, uio->uio_resid,
406 seqcount * BKVASIZE, &bp);
408 error = bread(ap->a_vp, base_offset, blksize, &bp);
415 if ((hammer_debug_io & 0x0001) && (bp->b_flags & B_IODEBUG)) {
416 kprintf("doff %016jx read file %016jx@%016jx\n",
417 (intmax_t)bp->b_bio2.bio_offset,
418 (intmax_t)ip->obj_id,
419 (intmax_t)bp->b_loffset);
421 bp->b_flags &= ~B_IODEBUG;
423 /* bp->b_flags |= B_CLUSTEROK; temporarily disabled */
424 n = blksize - offset;
425 if (n > uio->uio_resid)
427 if (n > ip->ino_data.size - uio->uio_offset)
428 n = (int)(ip->ino_data.size - uio->uio_offset);
430 lwkt_reltoken(&hmp->fs_token);
433 * Set B_AGE, data has a lower priority than meta-data.
435 * Use a hold/unlock/drop sequence to run the uiomove
436 * with the buffer unlocked, avoiding deadlocks against
437 * read()s on mmap()'d spaces.
439 bp->b_flags |= B_AGE;
442 error = uiomove((char *)bp->b_data + offset, n, uio);
446 lwkt_gettoken(&hmp->fs_token);
450 hammer_stats_file_read += n;
454 * XXX only update the atime if we had to get the MP lock.
455 * XXX hack hack hack, fixme.
458 if ((ip->flags & HAMMER_INODE_RO) == 0 &&
459 (ip->hmp->mp->mnt_flag & MNT_NOATIME) == 0) {
460 ip->ino_data.atime = trans.time;
461 hammer_modify_inode(&trans, ip, HAMMER_INODE_ATIME);
463 hammer_done_transaction(&trans);
464 lwkt_reltoken(&hmp->fs_token);
470 * hammer_vop_write { vp, uio, ioflag, cred }
474 hammer_vop_write(struct vop_write_args *ap)
476 struct hammer_transaction trans;
477 struct hammer_inode *ip;
490 if (ap->a_vp->v_type != VREG)
496 seqcount = ap->a_ioflag >> 16;
498 if (ip->flags & HAMMER_INODE_RO)
502 * Create a transaction to cover the operations we perform.
504 lwkt_gettoken(&hmp->fs_token);
505 hammer_start_transaction(&trans, hmp);
511 if (ap->a_ioflag & IO_APPEND)
512 uio->uio_offset = ip->ino_data.size;
515 * Check for illegal write offsets. Valid range is 0...2^63-1.
517 * NOTE: the base_off assignment is required to work around what
518 * I consider to be a GCC-4 optimization bug.
520 if (uio->uio_offset < 0) {
521 hammer_done_transaction(&trans);
522 lwkt_reltoken(&hmp->fs_token);
525 base_offset = uio->uio_offset + uio->uio_resid; /* work around gcc-4 */
526 if (uio->uio_resid > 0 && base_offset <= uio->uio_offset) {
527 hammer_done_transaction(&trans);
528 lwkt_reltoken(&hmp->fs_token);
533 * If reading or writing a huge amount of data we have to break
534 * atomicy and allow the operation to be interrupted by a signal
535 * or it can DOS the machine.
537 * Preset redo_count so we stop generating REDOs earlier if the
540 bigwrite = (uio->uio_resid > 100 * 1024 * 1024);
541 if ((ip->flags & HAMMER_INODE_REDO) &&
542 ip->redo_count < hammer_limit_redo) {
543 ip->redo_count += uio->uio_resid;
547 * Access the data typically in HAMMER_BUFSIZE blocks via the
548 * buffer cache, but HAMMER may use a variable block size based
551 while (uio->uio_resid > 0) {
559 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_WRITE)) != 0)
561 if (bigwrite && (error = hammer_signal_check(hmp)) != 0)
564 blksize = hammer_blocksize(uio->uio_offset);
567 * Do not allow HAMMER to blow out the buffer cache. Very
568 * large UIOs can lockout other processes due to bwillwrite()
571 * The hammer inode is not locked during these operations.
572 * The vnode is locked which can interfere with the pageout
573 * daemon for non-UIO_NOCOPY writes but should not interfere
574 * with the buffer cache. Even so, we cannot afford to
575 * allow the pageout daemon to build up too many dirty buffer
578 * Only call this if we aren't being recursively called from
579 * a virtual disk device (vn), else we may deadlock.
581 if ((ap->a_ioflag & IO_RECURSE) == 0)
585 * Control the number of pending records associated with
586 * this inode. If too many have accumulated start a
587 * flush. Try to maintain a pipeline with the flusher.
589 * NOTE: It is possible for other sources to grow the
590 * records but not necessarily issue another flush,
591 * so use a timeout and ensure that a re-flush occurs.
593 if (ip->rsv_recs >= hammer_limit_inode_recs) {
594 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
595 while (ip->rsv_recs >= hammer_limit_inode_recs * 2) {
596 ip->flags |= HAMMER_INODE_RECSW;
597 tsleep(&ip->rsv_recs, 0, "hmrwww", hz);
598 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
604 * Do not allow HAMMER to blow out system memory by
605 * accumulating too many records. Records are so well
606 * decoupled from the buffer cache that it is possible
607 * for userland to push data out to the media via
608 * direct-write, but build up the records queued to the
609 * backend faster then the backend can flush them out.
610 * HAMMER has hit its write limit but the frontend has
611 * no pushback to slow it down.
613 if (hmp->rsv_recs > hammer_limit_recs / 2) {
615 * Get the inode on the flush list
617 if (ip->rsv_recs >= 64)
618 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
619 else if (ip->rsv_recs >= 16)
620 hammer_flush_inode(ip, 0);
623 * Keep the flusher going if the system keeps
626 delta = hmp->count_newrecords -
627 hmp->last_newrecords;
628 if (delta < 0 || delta > hammer_limit_recs / 2) {
629 hmp->last_newrecords = hmp->count_newrecords;
630 hammer_sync_hmp(hmp, MNT_NOWAIT);
634 * If we have gotten behind start slowing
637 delta = (hmp->rsv_recs - hammer_limit_recs) *
638 hz / hammer_limit_recs;
640 tsleep(&trans, 0, "hmrslo", delta);
645 * Calculate the blocksize at the current offset and figure
646 * out how much we can actually write.
648 blkmask = blksize - 1;
649 offset = (int)uio->uio_offset & blkmask;
650 base_offset = uio->uio_offset & ~(int64_t)blkmask;
651 n = blksize - offset;
652 if (n > uio->uio_resid) {
658 nsize = uio->uio_offset + n;
659 if (nsize > ip->ino_data.size) {
660 if (uio->uio_offset > ip->ino_data.size)
664 nvextendbuf(ap->a_vp,
667 hammer_blocksize(ip->ino_data.size),
668 hammer_blocksize(nsize),
669 hammer_blockoff(ip->ino_data.size),
670 hammer_blockoff(nsize),
673 kflags |= NOTE_EXTEND;
676 if (uio->uio_segflg == UIO_NOCOPY) {
678 * Issuing a write with the same data backing the
679 * buffer. Instantiate the buffer to collect the
680 * backing vm pages, then read-in any missing bits.
682 * This case is used by vop_stdputpages().
684 bp = getblk(ap->a_vp, base_offset,
685 blksize, GETBLK_BHEAVY, 0);
686 if ((bp->b_flags & B_CACHE) == 0) {
688 error = bread(ap->a_vp, base_offset,
691 } else if (offset == 0 && uio->uio_resid >= blksize) {
693 * Even though we are entirely overwriting the buffer
694 * we may still have to zero it out to avoid a
695 * mmap/write visibility issue.
697 bp = getblk(ap->a_vp, base_offset, blksize, GETBLK_BHEAVY, 0);
698 if ((bp->b_flags & B_CACHE) == 0)
700 } else if (base_offset >= ip->ino_data.size) {
702 * If the base offset of the buffer is beyond the
703 * file EOF, we don't have to issue a read.
705 bp = getblk(ap->a_vp, base_offset,
706 blksize, GETBLK_BHEAVY, 0);
710 * Partial overwrite, read in any missing bits then
711 * replace the portion being written.
713 error = bread(ap->a_vp, base_offset, blksize, &bp);
718 lwkt_reltoken(&hmp->fs_token);
719 error = uiomove(bp->b_data + offset, n, uio);
720 lwkt_gettoken(&hmp->fs_token);
724 * Generate REDO records if enabled and redo_count will not
725 * exceeded the limit.
727 * If redo_count exceeds the limit we stop generating records
728 * and clear HAMMER_INODE_REDO. This will cause the next
729 * fsync() to do a full meta-data sync instead of just an
730 * UNDO/REDO fifo update.
732 * When clearing HAMMER_INODE_REDO any pre-existing REDOs
733 * will still be tracked. The tracks will be terminated
734 * when the related meta-data (including possible data
735 * modifications which are not tracked via REDO) is
738 if ((ip->flags & HAMMER_INODE_REDO) && error == 0) {
739 if (ip->redo_count < hammer_limit_redo) {
740 bp->b_flags |= B_VFSFLAG1;
741 error = hammer_generate_redo(&trans, ip,
742 base_offset + offset,
747 ip->flags &= ~HAMMER_INODE_REDO;
752 * If we screwed up we have to undo any VM size changes we
758 nvtruncbuf(ap->a_vp, ip->ino_data.size,
759 hammer_blocksize(ip->ino_data.size),
760 hammer_blockoff(ip->ino_data.size));
764 kflags |= NOTE_WRITE;
765 hammer_stats_file_write += n;
766 /* bp->b_flags |= B_CLUSTEROK; temporarily disabled */
767 if (ip->ino_data.size < uio->uio_offset) {
768 ip->ino_data.size = uio->uio_offset;
769 flags = HAMMER_INODE_SDIRTY;
773 ip->ino_data.mtime = trans.time;
774 flags |= HAMMER_INODE_MTIME | HAMMER_INODE_BUFS;
775 hammer_modify_inode(&trans, ip, flags);
778 * Once we dirty the buffer any cached zone-X offset
779 * becomes invalid. HAMMER NOTE: no-history mode cannot
780 * allow overwriting over the same data sector unless
781 * we provide UNDOs for the old data, which we don't.
783 bp->b_bio2.bio_offset = NOOFFSET;
786 * Final buffer disposition.
788 * Because meta-data updates are deferred, HAMMER is
789 * especially sensitive to excessive bdwrite()s because
790 * the I/O stream is not broken up by disk reads. So the
791 * buffer cache simply cannot keep up.
793 * WARNING! blksize is variable. cluster_write() is
794 * expected to not blow up if it encounters
795 * buffers that do not match the passed blksize.
797 * NOTE! Hammer shouldn't need to bawrite()/cluster_write().
798 * The ip->rsv_recs check should burst-flush the data.
799 * If we queue it immediately the buf could be left
800 * locked on the device queue for a very long time.
802 * NOTE! To avoid degenerate stalls due to mismatched block
803 * sizes we only honor IO_DIRECT on the write which
804 * abuts the end of the buffer. However, we must
805 * honor IO_SYNC in case someone is silly enough to
806 * configure a HAMMER file as swap, or when HAMMER
807 * is serving NFS (for commits). Ick ick.
809 bp->b_flags |= B_AGE;
810 if (ap->a_ioflag & IO_SYNC) {
812 } else if ((ap->a_ioflag & IO_DIRECT) && endofblk) {
816 if (offset + n == blksize) {
817 if (hammer_cluster_enable == 0 ||
818 (ap->a_vp->v_mount->mnt_flag & MNT_NOCLUSTERW)) {
821 cluster_write(bp, ip->ino_data.size,
829 hammer_done_transaction(&trans);
830 hammer_knote(ap->a_vp, kflags);
831 lwkt_reltoken(&hmp->fs_token);
836 * hammer_vop_access { vp, mode, cred }
838 * MPSAFE - does not require fs_token
842 hammer_vop_access(struct vop_access_args *ap)
844 struct hammer_inode *ip = VTOI(ap->a_vp);
849 ++hammer_stats_file_iopsr;
850 uid = hammer_to_unix_xid(&ip->ino_data.uid);
851 gid = hammer_to_unix_xid(&ip->ino_data.gid);
853 error = vop_helper_access(ap, uid, gid, ip->ino_data.mode,
854 ip->ino_data.uflags);
859 * hammer_vop_advlock { vp, id, op, fl, flags }
861 * MPSAFE - does not require fs_token
865 hammer_vop_advlock(struct vop_advlock_args *ap)
867 hammer_inode_t ip = VTOI(ap->a_vp);
869 return (lf_advlock(ap, &ip->advlock, ip->ino_data.size));
873 * hammer_vop_close { vp, fflag }
875 * We can only sync-on-close for normal closes. XXX disabled for now.
879 hammer_vop_close(struct vop_close_args *ap)
882 struct vnode *vp = ap->a_vp;
883 hammer_inode_t ip = VTOI(vp);
885 if (ip->flags & (HAMMER_INODE_CLOSESYNC|HAMMER_INODE_CLOSEASYNC)) {
886 if (vn_islocked(vp) == LK_EXCLUSIVE &&
887 (vp->v_flag & (VINACTIVE|VRECLAIMED)) == 0) {
888 if (ip->flags & HAMMER_INODE_CLOSESYNC)
891 waitfor = MNT_NOWAIT;
892 ip->flags &= ~(HAMMER_INODE_CLOSESYNC |
893 HAMMER_INODE_CLOSEASYNC);
894 VOP_FSYNC(vp, MNT_NOWAIT, waitfor);
898 return (vop_stdclose(ap));
902 * hammer_vop_ncreate { nch, dvp, vpp, cred, vap }
904 * The operating system has already ensured that the directory entry
905 * does not exist and done all appropriate namespace locking.
909 hammer_vop_ncreate(struct vop_ncreate_args *ap)
911 struct hammer_transaction trans;
912 struct hammer_inode *dip;
913 struct hammer_inode *nip;
914 struct nchandle *nch;
919 dip = VTOI(ap->a_dvp);
922 if (dip->flags & HAMMER_INODE_RO)
924 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0)
928 * Create a transaction to cover the operations we perform.
930 lwkt_gettoken(&hmp->fs_token);
931 hammer_start_transaction(&trans, hmp);
932 ++hammer_stats_file_iopsw;
935 * Create a new filesystem object of the requested type. The
936 * returned inode will be referenced and shared-locked to prevent
937 * it from being moved to the flusher.
939 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
940 dip, nch->ncp->nc_name, nch->ncp->nc_nlen,
943 hkprintf("hammer_create_inode error %d\n", error);
944 hammer_done_transaction(&trans);
946 lwkt_reltoken(&hmp->fs_token);
951 * Add the new filesystem object to the directory. This will also
952 * bump the inode's link count.
954 error = hammer_ip_add_directory(&trans, dip,
955 nch->ncp->nc_name, nch->ncp->nc_nlen,
958 hkprintf("hammer_ip_add_directory error %d\n", error);
964 hammer_rel_inode(nip, 0);
965 hammer_done_transaction(&trans);
968 error = hammer_get_vnode(nip, ap->a_vpp);
969 hammer_done_transaction(&trans);
970 hammer_rel_inode(nip, 0);
972 cache_setunresolved(ap->a_nch);
973 cache_setvp(ap->a_nch, *ap->a_vpp);
975 hammer_knote(ap->a_dvp, NOTE_WRITE);
977 lwkt_reltoken(&hmp->fs_token);
982 * hammer_vop_getattr { vp, vap }
984 * Retrieve an inode's attribute information. When accessing inodes
985 * historically we fake the atime field to ensure consistent results.
986 * The atime field is stored in the B-Tree element and allowed to be
987 * updated without cycling the element.
989 * MPSAFE - does not require fs_token
993 hammer_vop_getattr(struct vop_getattr_args *ap)
995 struct hammer_inode *ip = VTOI(ap->a_vp);
996 struct vattr *vap = ap->a_vap;
999 * We want the fsid to be different when accessing a filesystem
1000 * with different as-of's so programs like diff don't think
1001 * the files are the same.
1003 * We also want the fsid to be the same when comparing snapshots,
1004 * or when comparing mirrors (which might be backed by different
1005 * physical devices). HAMMER fsids are based on the PFS's
1006 * shared_uuid field.
1008 * XXX there is a chance of collision here. The va_fsid reported
1009 * by stat is different from the more involved fsid used in the
1012 ++hammer_stats_file_iopsr;
1013 hammer_lock_sh(&ip->lock);
1014 vap->va_fsid = ip->pfsm->fsid_udev ^ (u_int32_t)ip->obj_asof ^
1015 (u_int32_t)(ip->obj_asof >> 32);
1017 vap->va_fileid = ip->ino_leaf.base.obj_id;
1018 vap->va_mode = ip->ino_data.mode;
1019 vap->va_nlink = ip->ino_data.nlinks;
1020 vap->va_uid = hammer_to_unix_xid(&ip->ino_data.uid);
1021 vap->va_gid = hammer_to_unix_xid(&ip->ino_data.gid);
1024 vap->va_size = ip->ino_data.size;
1027 * Special case for @@PFS softlinks. The actual size of the
1028 * expanded softlink is "@@0x%016llx:%05d" == 26 bytes.
1029 * or for MAX_TID is "@@-1:%05d" == 10 bytes.
1031 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_SOFTLINK &&
1032 ip->ino_data.size == 10 &&
1033 ip->obj_asof == HAMMER_MAX_TID &&
1034 ip->obj_localization == 0 &&
1035 strncmp(ip->ino_data.ext.symlink, "@@PFS", 5) == 0) {
1036 if (ip->pfsm->pfsd.mirror_flags & HAMMER_PFSD_SLAVE)
1043 * We must provide a consistent atime and mtime for snapshots
1044 * so people can do a 'tar cf - ... | md5' on them and get
1045 * consistent results.
1047 if (ip->flags & HAMMER_INODE_RO) {
1048 hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_atime);
1049 hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_mtime);
1051 hammer_time_to_timespec(ip->ino_data.atime, &vap->va_atime);
1052 hammer_time_to_timespec(ip->ino_data.mtime, &vap->va_mtime);
1054 hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_ctime);
1055 vap->va_flags = ip->ino_data.uflags;
1056 vap->va_gen = 1; /* hammer inums are unique for all time */
1057 vap->va_blocksize = HAMMER_BUFSIZE;
1058 if (ip->ino_data.size >= HAMMER_XDEMARC) {
1059 vap->va_bytes = (ip->ino_data.size + HAMMER_XBUFMASK64) &
1061 } else if (ip->ino_data.size > HAMMER_BUFSIZE / 2) {
1062 vap->va_bytes = (ip->ino_data.size + HAMMER_BUFMASK64) &
1065 vap->va_bytes = (ip->ino_data.size + 15) & ~15;
1068 vap->va_type = hammer_get_vnode_type(ip->ino_data.obj_type);
1069 vap->va_filerev = 0; /* XXX */
1070 vap->va_uid_uuid = ip->ino_data.uid;
1071 vap->va_gid_uuid = ip->ino_data.gid;
1072 vap->va_fsid_uuid = ip->hmp->fsid;
1073 vap->va_vaflags = VA_UID_UUID_VALID | VA_GID_UUID_VALID |
1076 switch (ip->ino_data.obj_type) {
1077 case HAMMER_OBJTYPE_CDEV:
1078 case HAMMER_OBJTYPE_BDEV:
1079 vap->va_rmajor = ip->ino_data.rmajor;
1080 vap->va_rminor = ip->ino_data.rminor;
1085 hammer_unlock(&ip->lock);
1090 * hammer_vop_nresolve { nch, dvp, cred }
1092 * Locate the requested directory entry.
1096 hammer_vop_nresolve(struct vop_nresolve_args *ap)
1098 struct hammer_transaction trans;
1099 struct namecache *ncp;
1104 struct hammer_cursor cursor;
1113 u_int32_t localization;
1114 u_int32_t max_iterations;
1117 * Misc initialization, plus handle as-of name extensions. Look for
1118 * the '@@' extension. Note that as-of files and directories cannot
1121 dip = VTOI(ap->a_dvp);
1122 ncp = ap->a_nch->ncp;
1123 asof = dip->obj_asof;
1124 localization = dip->obj_localization; /* for code consistency */
1125 nlen = ncp->nc_nlen;
1126 flags = dip->flags & HAMMER_INODE_RO;
1130 lwkt_gettoken(&hmp->fs_token);
1131 hammer_simple_transaction(&trans, hmp);
1132 ++hammer_stats_file_iopsr;
1134 for (i = 0; i < nlen; ++i) {
1135 if (ncp->nc_name[i] == '@' && ncp->nc_name[i+1] == '@') {
1136 error = hammer_str_to_tid(ncp->nc_name + i + 2,
1137 &ispfs, &asof, &localization);
1142 if (asof != HAMMER_MAX_TID)
1143 flags |= HAMMER_INODE_RO;
1150 * If this is a PFS softlink we dive into the PFS
1152 if (ispfs && nlen == 0) {
1153 ip = hammer_get_inode(&trans, dip, HAMMER_OBJID_ROOT,
1157 error = hammer_get_vnode(ip, &vp);
1158 hammer_rel_inode(ip, 0);
1164 cache_setvp(ap->a_nch, vp);
1171 * If there is no path component the time extension is relative to dip.
1172 * e.g. "fubar/@@<snapshot>"
1174 * "." is handled by the kernel, but ".@@<snapshot>" is not.
1175 * e.g. "fubar/.@@<snapshot>"
1177 * ".." is handled by the kernel. We do not currently handle
1180 if (nlen == 0 || (nlen == 1 && ncp->nc_name[0] == '.')) {
1181 ip = hammer_get_inode(&trans, dip, dip->obj_id,
1182 asof, dip->obj_localization,
1185 error = hammer_get_vnode(ip, &vp);
1186 hammer_rel_inode(ip, 0);
1192 cache_setvp(ap->a_nch, vp);
1199 * Calculate the namekey and setup the key range for the scan. This
1200 * works kinda like a chained hash table where the lower 32 bits
1201 * of the namekey synthesize the chain.
1203 * The key range is inclusive of both key_beg and key_end.
1205 namekey = hammer_directory_namekey(dip, ncp->nc_name, nlen,
1208 error = hammer_init_cursor(&trans, &cursor, &dip->cache[1], dip);
1209 cursor.key_beg.localization = dip->obj_localization +
1210 hammer_dir_localization(dip);
1211 cursor.key_beg.obj_id = dip->obj_id;
1212 cursor.key_beg.key = namekey;
1213 cursor.key_beg.create_tid = 0;
1214 cursor.key_beg.delete_tid = 0;
1215 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1216 cursor.key_beg.obj_type = 0;
1218 cursor.key_end = cursor.key_beg;
1219 cursor.key_end.key += max_iterations;
1221 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
1224 * Scan all matching records (the chain), locate the one matching
1225 * the requested path component.
1227 * The hammer_ip_*() functions merge in-memory records with on-disk
1228 * records for the purposes of the search.
1231 localization = HAMMER_DEF_LOCALIZATION;
1234 error = hammer_ip_first(&cursor);
1235 while (error == 0) {
1236 error = hammer_ip_resolve_data(&cursor);
1239 if (nlen == cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF &&
1240 bcmp(ncp->nc_name, cursor.data->entry.name, nlen) == 0) {
1241 obj_id = cursor.data->entry.obj_id;
1242 localization = cursor.data->entry.localization;
1245 error = hammer_ip_next(&cursor);
1248 hammer_done_cursor(&cursor);
1251 * Lookup the obj_id. This should always succeed. If it does not
1252 * the filesystem may be damaged and we return a dummy inode.
1255 ip = hammer_get_inode(&trans, dip, obj_id,
1258 if (error == ENOENT) {
1259 kprintf("HAMMER: WARNING: Missing "
1260 "inode for dirent \"%s\"\n"
1261 "\tobj_id = %016llx, asof=%016llx, lo=%08x\n",
1263 (long long)obj_id, (long long)asof,
1266 ip = hammer_get_dummy_inode(&trans, dip, obj_id,
1271 error = hammer_get_vnode(ip, &vp);
1272 hammer_rel_inode(ip, 0);
1278 cache_setvp(ap->a_nch, vp);
1281 } else if (error == ENOENT) {
1282 cache_setvp(ap->a_nch, NULL);
1285 hammer_done_transaction(&trans);
1286 lwkt_reltoken(&hmp->fs_token);
1291 * hammer_vop_nlookupdotdot { dvp, vpp, cred }
1293 * Locate the parent directory of a directory vnode.
1295 * dvp is referenced but not locked. *vpp must be returned referenced and
1296 * locked. A parent_obj_id of 0 does not necessarily indicate that we are
1297 * at the root, instead it could indicate that the directory we were in was
1300 * NOTE: as-of sequences are not linked into the directory structure. If
1301 * we are at the root with a different asof then the mount point, reload
1302 * the same directory with the mount point's asof. I'm not sure what this
1303 * will do to NFS. We encode ASOF stamps in NFS file handles so it might not
1304 * get confused, but it hasn't been tested.
1308 hammer_vop_nlookupdotdot(struct vop_nlookupdotdot_args *ap)
1310 struct hammer_transaction trans;
1311 struct hammer_inode *dip;
1312 struct hammer_inode *ip;
1314 int64_t parent_obj_id;
1315 u_int32_t parent_obj_localization;
1319 dip = VTOI(ap->a_dvp);
1320 asof = dip->obj_asof;
1324 * Whos are parent? This could be the root of a pseudo-filesystem
1325 * whos parent is in another localization domain.
1327 lwkt_gettoken(&hmp->fs_token);
1328 parent_obj_id = dip->ino_data.parent_obj_id;
1329 if (dip->obj_id == HAMMER_OBJID_ROOT)
1330 parent_obj_localization = dip->ino_data.ext.obj.parent_obj_localization;
1332 parent_obj_localization = dip->obj_localization;
1334 if (parent_obj_id == 0) {
1335 if (dip->obj_id == HAMMER_OBJID_ROOT &&
1336 asof != hmp->asof) {
1337 parent_obj_id = dip->obj_id;
1339 *ap->a_fakename = kmalloc(19, M_TEMP, M_WAITOK);
1340 ksnprintf(*ap->a_fakename, 19, "0x%016llx",
1341 (long long)dip->obj_asof);
1344 lwkt_reltoken(&hmp->fs_token);
1349 hammer_simple_transaction(&trans, hmp);
1350 ++hammer_stats_file_iopsr;
1352 ip = hammer_get_inode(&trans, dip, parent_obj_id,
1353 asof, parent_obj_localization,
1354 dip->flags, &error);
1356 error = hammer_get_vnode(ip, ap->a_vpp);
1357 hammer_rel_inode(ip, 0);
1361 hammer_done_transaction(&trans);
1362 lwkt_reltoken(&hmp->fs_token);
1367 * hammer_vop_nlink { nch, dvp, vp, cred }
1371 hammer_vop_nlink(struct vop_nlink_args *ap)
1373 struct hammer_transaction trans;
1374 struct hammer_inode *dip;
1375 struct hammer_inode *ip;
1376 struct nchandle *nch;
1380 if (ap->a_dvp->v_mount != ap->a_vp->v_mount)
1384 dip = VTOI(ap->a_dvp);
1385 ip = VTOI(ap->a_vp);
1388 if (dip->obj_localization != ip->obj_localization)
1391 if (dip->flags & HAMMER_INODE_RO)
1393 if (ip->flags & HAMMER_INODE_RO)
1395 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0)
1399 * Create a transaction to cover the operations we perform.
1401 lwkt_gettoken(&hmp->fs_token);
1402 hammer_start_transaction(&trans, hmp);
1403 ++hammer_stats_file_iopsw;
1406 * Add the filesystem object to the directory. Note that neither
1407 * dip nor ip are referenced or locked, but their vnodes are
1408 * referenced. This function will bump the inode's link count.
1410 error = hammer_ip_add_directory(&trans, dip,
1411 nch->ncp->nc_name, nch->ncp->nc_nlen,
1418 cache_setunresolved(nch);
1419 cache_setvp(nch, ap->a_vp);
1421 hammer_done_transaction(&trans);
1422 hammer_knote(ap->a_vp, NOTE_LINK);
1423 hammer_knote(ap->a_dvp, NOTE_WRITE);
1424 lwkt_reltoken(&hmp->fs_token);
1429 * hammer_vop_nmkdir { nch, dvp, vpp, cred, vap }
1431 * The operating system has already ensured that the directory entry
1432 * does not exist and done all appropriate namespace locking.
1436 hammer_vop_nmkdir(struct vop_nmkdir_args *ap)
1438 struct hammer_transaction trans;
1439 struct hammer_inode *dip;
1440 struct hammer_inode *nip;
1441 struct nchandle *nch;
1446 dip = VTOI(ap->a_dvp);
1449 if (dip->flags & HAMMER_INODE_RO)
1451 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0)
1455 * Create a transaction to cover the operations we perform.
1457 lwkt_gettoken(&hmp->fs_token);
1458 hammer_start_transaction(&trans, hmp);
1459 ++hammer_stats_file_iopsw;
1462 * Create a new filesystem object of the requested type. The
1463 * returned inode will be referenced but not locked.
1465 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
1466 dip, nch->ncp->nc_name, nch->ncp->nc_nlen,
1469 hkprintf("hammer_mkdir error %d\n", error);
1470 hammer_done_transaction(&trans);
1472 lwkt_reltoken(&hmp->fs_token);
1476 * Add the new filesystem object to the directory. This will also
1477 * bump the inode's link count.
1479 error = hammer_ip_add_directory(&trans, dip,
1480 nch->ncp->nc_name, nch->ncp->nc_nlen,
1483 hkprintf("hammer_mkdir (add) error %d\n", error);
1489 hammer_rel_inode(nip, 0);
1492 error = hammer_get_vnode(nip, ap->a_vpp);
1493 hammer_rel_inode(nip, 0);
1495 cache_setunresolved(ap->a_nch);
1496 cache_setvp(ap->a_nch, *ap->a_vpp);
1499 hammer_done_transaction(&trans);
1501 hammer_knote(ap->a_dvp, NOTE_WRITE | NOTE_LINK);
1502 lwkt_reltoken(&hmp->fs_token);
1507 * hammer_vop_nmknod { nch, dvp, vpp, cred, vap }
1509 * The operating system has already ensured that the directory entry
1510 * does not exist and done all appropriate namespace locking.
1514 hammer_vop_nmknod(struct vop_nmknod_args *ap)
1516 struct hammer_transaction trans;
1517 struct hammer_inode *dip;
1518 struct hammer_inode *nip;
1519 struct nchandle *nch;
1524 dip = VTOI(ap->a_dvp);
1527 if (dip->flags & HAMMER_INODE_RO)
1529 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0)
1533 * Create a transaction to cover the operations we perform.
1535 lwkt_gettoken(&hmp->fs_token);
1536 hammer_start_transaction(&trans, hmp);
1537 ++hammer_stats_file_iopsw;
1540 * Create a new filesystem object of the requested type. The
1541 * returned inode will be referenced but not locked.
1543 * If mknod specifies a directory a pseudo-fs is created.
1545 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
1546 dip, nch->ncp->nc_name, nch->ncp->nc_nlen,
1549 hammer_done_transaction(&trans);
1551 lwkt_reltoken(&hmp->fs_token);
1556 * Add the new filesystem object to the directory. This will also
1557 * bump the inode's link count.
1559 error = hammer_ip_add_directory(&trans, dip,
1560 nch->ncp->nc_name, nch->ncp->nc_nlen,
1567 hammer_rel_inode(nip, 0);
1570 error = hammer_get_vnode(nip, ap->a_vpp);
1571 hammer_rel_inode(nip, 0);
1573 cache_setunresolved(ap->a_nch);
1574 cache_setvp(ap->a_nch, *ap->a_vpp);
1577 hammer_done_transaction(&trans);
1579 hammer_knote(ap->a_dvp, NOTE_WRITE);
1580 lwkt_reltoken(&hmp->fs_token);
1585 * hammer_vop_open { vp, mode, cred, fp }
1587 * MPSAFE (does not require fs_token)
1591 hammer_vop_open(struct vop_open_args *ap)
1595 ++hammer_stats_file_iopsr;
1596 ip = VTOI(ap->a_vp);
1598 if ((ap->a_mode & FWRITE) && (ip->flags & HAMMER_INODE_RO))
1600 return(vop_stdopen(ap));
1604 * hammer_vop_print { vp }
1608 hammer_vop_print(struct vop_print_args *ap)
1614 * hammer_vop_readdir { vp, uio, cred, *eofflag, *ncookies, off_t **cookies }
1618 hammer_vop_readdir(struct vop_readdir_args *ap)
1620 struct hammer_transaction trans;
1621 struct hammer_cursor cursor;
1622 struct hammer_inode *ip;
1625 hammer_base_elm_t base;
1634 ++hammer_stats_file_iopsr;
1635 ip = VTOI(ap->a_vp);
1637 saveoff = uio->uio_offset;
1640 if (ap->a_ncookies) {
1641 ncookies = uio->uio_resid / 16 + 1;
1642 if (ncookies > 1024)
1644 cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
1652 lwkt_gettoken(&hmp->fs_token);
1653 hammer_simple_transaction(&trans, hmp);
1656 * Handle artificial entries
1658 * It should be noted that the minimum value for a directory
1659 * hash key on-media is 0x0000000100000000, so we can use anything
1660 * less then that to represent our 'special' key space.
1664 r = vop_write_dirent(&error, uio, ip->obj_id, DT_DIR, 1, ".");
1668 cookies[cookie_index] = saveoff;
1671 if (cookie_index == ncookies)
1675 if (ip->ino_data.parent_obj_id) {
1676 r = vop_write_dirent(&error, uio,
1677 ip->ino_data.parent_obj_id,
1680 r = vop_write_dirent(&error, uio,
1681 ip->obj_id, DT_DIR, 2, "..");
1686 cookies[cookie_index] = saveoff;
1689 if (cookie_index == ncookies)
1694 * Key range (begin and end inclusive) to scan. Directory keys
1695 * directly translate to a 64 bit 'seek' position.
1697 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
1698 cursor.key_beg.localization = ip->obj_localization +
1699 hammer_dir_localization(ip);
1700 cursor.key_beg.obj_id = ip->obj_id;
1701 cursor.key_beg.create_tid = 0;
1702 cursor.key_beg.delete_tid = 0;
1703 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1704 cursor.key_beg.obj_type = 0;
1705 cursor.key_beg.key = saveoff;
1707 cursor.key_end = cursor.key_beg;
1708 cursor.key_end.key = HAMMER_MAX_KEY;
1709 cursor.asof = ip->obj_asof;
1710 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
1712 error = hammer_ip_first(&cursor);
1714 while (error == 0) {
1715 error = hammer_ip_resolve_data(&cursor);
1718 base = &cursor.leaf->base;
1719 saveoff = base->key;
1720 KKASSERT(cursor.leaf->data_len > HAMMER_ENTRY_NAME_OFF);
1722 if (base->obj_id != ip->obj_id)
1723 panic("readdir: bad record at %p", cursor.node);
1726 * Convert pseudo-filesystems into softlinks
1728 dtype = hammer_get_dtype(cursor.leaf->base.obj_type);
1729 r = vop_write_dirent(
1730 &error, uio, cursor.data->entry.obj_id,
1732 cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF ,
1733 (void *)cursor.data->entry.name);
1738 cookies[cookie_index] = base->key;
1740 if (cookie_index == ncookies)
1742 error = hammer_ip_next(&cursor);
1744 hammer_done_cursor(&cursor);
1747 hammer_done_transaction(&trans);
1750 *ap->a_eofflag = (error == ENOENT);
1751 uio->uio_offset = saveoff;
1752 if (error && cookie_index == 0) {
1753 if (error == ENOENT)
1756 kfree(cookies, M_TEMP);
1757 *ap->a_ncookies = 0;
1758 *ap->a_cookies = NULL;
1761 if (error == ENOENT)
1764 *ap->a_ncookies = cookie_index;
1765 *ap->a_cookies = cookies;
1768 lwkt_reltoken(&hmp->fs_token);
1773 * hammer_vop_readlink { vp, uio, cred }
1777 hammer_vop_readlink(struct vop_readlink_args *ap)
1779 struct hammer_transaction trans;
1780 struct hammer_cursor cursor;
1781 struct hammer_inode *ip;
1784 u_int32_t localization;
1785 hammer_pseudofs_inmem_t pfsm;
1788 ip = VTOI(ap->a_vp);
1791 lwkt_gettoken(&hmp->fs_token);
1794 * Shortcut if the symlink data was stuffed into ino_data.
1796 * Also expand special "@@PFS%05d" softlinks (expansion only
1797 * occurs for non-historical (current) accesses made from the
1798 * primary filesystem).
1800 if (ip->ino_data.size <= HAMMER_INODE_BASESYMLEN) {
1804 ptr = ip->ino_data.ext.symlink;
1805 bytes = (int)ip->ino_data.size;
1807 ip->obj_asof == HAMMER_MAX_TID &&
1808 ip->obj_localization == 0 &&
1809 strncmp(ptr, "@@PFS", 5) == 0) {
1810 hammer_simple_transaction(&trans, hmp);
1811 bcopy(ptr + 5, buf, 5);
1813 localization = strtoul(buf, NULL, 10) << 16;
1814 pfsm = hammer_load_pseudofs(&trans, localization,
1817 if (pfsm->pfsd.mirror_flags &
1818 HAMMER_PFSD_SLAVE) {
1819 /* vap->va_size == 26 */
1820 ksnprintf(buf, sizeof(buf),
1822 (long long)pfsm->pfsd.sync_end_tid,
1823 localization >> 16);
1825 /* vap->va_size == 10 */
1826 ksnprintf(buf, sizeof(buf),
1828 localization >> 16);
1830 ksnprintf(buf, sizeof(buf),
1832 (long long)HAMMER_MAX_TID,
1833 localization >> 16);
1837 bytes = strlen(buf);
1840 hammer_rel_pseudofs(hmp, pfsm);
1841 hammer_done_transaction(&trans);
1843 error = uiomove(ptr, bytes, ap->a_uio);
1844 lwkt_reltoken(&hmp->fs_token);
1851 hammer_simple_transaction(&trans, hmp);
1852 ++hammer_stats_file_iopsr;
1853 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
1856 * Key range (begin and end inclusive) to scan. Directory keys
1857 * directly translate to a 64 bit 'seek' position.
1859 cursor.key_beg.localization = ip->obj_localization +
1860 HAMMER_LOCALIZE_MISC;
1861 cursor.key_beg.obj_id = ip->obj_id;
1862 cursor.key_beg.create_tid = 0;
1863 cursor.key_beg.delete_tid = 0;
1864 cursor.key_beg.rec_type = HAMMER_RECTYPE_FIX;
1865 cursor.key_beg.obj_type = 0;
1866 cursor.key_beg.key = HAMMER_FIXKEY_SYMLINK;
1867 cursor.asof = ip->obj_asof;
1868 cursor.flags |= HAMMER_CURSOR_ASOF;
1870 error = hammer_ip_lookup(&cursor);
1872 error = hammer_ip_resolve_data(&cursor);
1874 KKASSERT(cursor.leaf->data_len >=
1875 HAMMER_SYMLINK_NAME_OFF);
1876 error = uiomove(cursor.data->symlink.name,
1877 cursor.leaf->data_len -
1878 HAMMER_SYMLINK_NAME_OFF,
1882 hammer_done_cursor(&cursor);
1883 hammer_done_transaction(&trans);
1884 lwkt_reltoken(&hmp->fs_token);
1889 * hammer_vop_nremove { nch, dvp, cred }
1893 hammer_vop_nremove(struct vop_nremove_args *ap)
1895 struct hammer_transaction trans;
1896 struct hammer_inode *dip;
1900 dip = VTOI(ap->a_dvp);
1903 if (hammer_nohistory(dip) == 0 &&
1904 (error = hammer_checkspace(hmp, HAMMER_CHKSPC_REMOVE)) != 0) {
1908 lwkt_gettoken(&hmp->fs_token);
1909 hammer_start_transaction(&trans, hmp);
1910 ++hammer_stats_file_iopsw;
1911 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp, ap->a_cred, 0, 0);
1912 hammer_done_transaction(&trans);
1914 hammer_knote(ap->a_dvp, NOTE_WRITE);
1915 lwkt_reltoken(&hmp->fs_token);
1920 * hammer_vop_nrename { fnch, tnch, fdvp, tdvp, cred }
1924 hammer_vop_nrename(struct vop_nrename_args *ap)
1926 struct hammer_transaction trans;
1927 struct namecache *fncp;
1928 struct namecache *tncp;
1929 struct hammer_inode *fdip;
1930 struct hammer_inode *tdip;
1931 struct hammer_inode *ip;
1933 struct hammer_cursor cursor;
1935 u_int32_t max_iterations;
1938 if (ap->a_fdvp->v_mount != ap->a_tdvp->v_mount)
1940 if (ap->a_fdvp->v_mount != ap->a_fnch->ncp->nc_vp->v_mount)
1943 fdip = VTOI(ap->a_fdvp);
1944 tdip = VTOI(ap->a_tdvp);
1945 fncp = ap->a_fnch->ncp;
1946 tncp = ap->a_tnch->ncp;
1947 ip = VTOI(fncp->nc_vp);
1948 KKASSERT(ip != NULL);
1952 if (fdip->obj_localization != tdip->obj_localization)
1954 if (fdip->obj_localization != ip->obj_localization)
1957 if (fdip->flags & HAMMER_INODE_RO)
1959 if (tdip->flags & HAMMER_INODE_RO)
1961 if (ip->flags & HAMMER_INODE_RO)
1963 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0)
1966 lwkt_gettoken(&hmp->fs_token);
1967 hammer_start_transaction(&trans, hmp);
1968 ++hammer_stats_file_iopsw;
1971 * Remove tncp from the target directory and then link ip as
1972 * tncp. XXX pass trans to dounlink
1974 * Force the inode sync-time to match the transaction so it is
1975 * in-sync with the creation of the target directory entry.
1977 error = hammer_dounlink(&trans, ap->a_tnch, ap->a_tdvp,
1979 if (error == 0 || error == ENOENT) {
1980 error = hammer_ip_add_directory(&trans, tdip,
1981 tncp->nc_name, tncp->nc_nlen,
1984 ip->ino_data.parent_obj_id = tdip->obj_id;
1985 ip->ino_data.ctime = trans.time;
1986 hammer_modify_inode(&trans, ip, HAMMER_INODE_DDIRTY);
1990 goto failed; /* XXX */
1993 * Locate the record in the originating directory and remove it.
1995 * Calculate the namekey and setup the key range for the scan. This
1996 * works kinda like a chained hash table where the lower 32 bits
1997 * of the namekey synthesize the chain.
1999 * The key range is inclusive of both key_beg and key_end.
2001 namekey = hammer_directory_namekey(fdip, fncp->nc_name, fncp->nc_nlen,
2004 hammer_init_cursor(&trans, &cursor, &fdip->cache[1], fdip);
2005 cursor.key_beg.localization = fdip->obj_localization +
2006 hammer_dir_localization(fdip);
2007 cursor.key_beg.obj_id = fdip->obj_id;
2008 cursor.key_beg.key = namekey;
2009 cursor.key_beg.create_tid = 0;
2010 cursor.key_beg.delete_tid = 0;
2011 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
2012 cursor.key_beg.obj_type = 0;
2014 cursor.key_end = cursor.key_beg;
2015 cursor.key_end.key += max_iterations;
2016 cursor.asof = fdip->obj_asof;
2017 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
2020 * Scan all matching records (the chain), locate the one matching
2021 * the requested path component.
2023 * The hammer_ip_*() functions merge in-memory records with on-disk
2024 * records for the purposes of the search.
2026 error = hammer_ip_first(&cursor);
2027 while (error == 0) {
2028 if (hammer_ip_resolve_data(&cursor) != 0)
2030 nlen = cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF;
2032 if (fncp->nc_nlen == nlen &&
2033 bcmp(fncp->nc_name, cursor.data->entry.name, nlen) == 0) {
2036 error = hammer_ip_next(&cursor);
2040 * If all is ok we have to get the inode so we can adjust nlinks.
2042 * WARNING: hammer_ip_del_directory() may have to terminate the
2043 * cursor to avoid a recursion. It's ok to call hammer_done_cursor()
2047 error = hammer_ip_del_directory(&trans, &cursor, fdip, ip);
2050 * XXX A deadlock here will break rename's atomicy for the purposes
2051 * of crash recovery.
2053 if (error == EDEADLK) {
2054 hammer_done_cursor(&cursor);
2059 * Cleanup and tell the kernel that the rename succeeded.
2061 * NOTE: ip->vp, if non-NULL, cannot be directly referenced
2062 * without formally acquiring the vp since the vp might
2063 * have zero refs on it, or in the middle of a reclaim,
2066 hammer_done_cursor(&cursor);
2068 cache_rename(ap->a_fnch, ap->a_tnch);
2069 hammer_knote(ap->a_fdvp, NOTE_WRITE);
2070 hammer_knote(ap->a_tdvp, NOTE_WRITE);
2074 error = hammer_get_vnode(ip, &vp);
2075 if (error == 0 && vp) {
2077 hammer_knote(ip->vp, NOTE_RENAME);
2081 kprintf("Debug: HAMMER ip/vp race2 avoided\n");
2086 hammer_done_transaction(&trans);
2087 lwkt_reltoken(&hmp->fs_token);
2092 * hammer_vop_nrmdir { nch, dvp, cred }
2096 hammer_vop_nrmdir(struct vop_nrmdir_args *ap)
2098 struct hammer_transaction trans;
2099 struct hammer_inode *dip;
2103 dip = VTOI(ap->a_dvp);
2106 if (hammer_nohistory(dip) == 0 &&
2107 (error = hammer_checkspace(hmp, HAMMER_CHKSPC_REMOVE)) != 0) {
2111 lwkt_gettoken(&hmp->fs_token);
2112 hammer_start_transaction(&trans, hmp);
2113 ++hammer_stats_file_iopsw;
2114 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp, ap->a_cred, 0, 1);
2115 hammer_done_transaction(&trans);
2117 hammer_knote(ap->a_dvp, NOTE_WRITE | NOTE_LINK);
2118 lwkt_reltoken(&hmp->fs_token);
2123 * hammer_vop_markatime { vp, cred }
2127 hammer_vop_markatime(struct vop_markatime_args *ap)
2129 struct hammer_transaction trans;
2130 struct hammer_inode *ip;
2133 ip = VTOI(ap->a_vp);
2134 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
2136 if (ip->flags & HAMMER_INODE_RO)
2139 if (hmp->mp->mnt_flag & MNT_NOATIME)
2141 lwkt_gettoken(&hmp->fs_token);
2142 hammer_start_transaction(&trans, hmp);
2143 ++hammer_stats_file_iopsw;
2145 ip->ino_data.atime = trans.time;
2146 hammer_modify_inode(&trans, ip, HAMMER_INODE_ATIME);
2147 hammer_done_transaction(&trans);
2148 hammer_knote(ap->a_vp, NOTE_ATTRIB);
2149 lwkt_reltoken(&hmp->fs_token);
2154 * hammer_vop_setattr { vp, vap, cred }
2158 hammer_vop_setattr(struct vop_setattr_args *ap)
2160 struct hammer_transaction trans;
2161 struct hammer_inode *ip;
2170 int64_t aligned_size;
2175 ip = ap->a_vp->v_data;
2180 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
2182 if (ip->flags & HAMMER_INODE_RO)
2184 if (hammer_nohistory(ip) == 0 &&
2185 (error = hammer_checkspace(hmp, HAMMER_CHKSPC_REMOVE)) != 0) {
2189 lwkt_gettoken(&hmp->fs_token);
2190 hammer_start_transaction(&trans, hmp);
2191 ++hammer_stats_file_iopsw;
2194 if (vap->va_flags != VNOVAL) {
2195 flags = ip->ino_data.uflags;
2196 error = vop_helper_setattr_flags(&flags, vap->va_flags,
2197 hammer_to_unix_xid(&ip->ino_data.uid),
2200 if (ip->ino_data.uflags != flags) {
2201 ip->ino_data.uflags = flags;
2202 ip->ino_data.ctime = trans.time;
2203 modflags |= HAMMER_INODE_DDIRTY;
2204 kflags |= NOTE_ATTRIB;
2206 if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) {
2213 if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) {
2217 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
2218 mode_t cur_mode = ip->ino_data.mode;
2219 uid_t cur_uid = hammer_to_unix_xid(&ip->ino_data.uid);
2220 gid_t cur_gid = hammer_to_unix_xid(&ip->ino_data.gid);
2224 error = vop_helper_chown(ap->a_vp, vap->va_uid, vap->va_gid,
2226 &cur_uid, &cur_gid, &cur_mode);
2228 hammer_guid_to_uuid(&uuid_uid, cur_uid);
2229 hammer_guid_to_uuid(&uuid_gid, cur_gid);
2230 if (bcmp(&uuid_uid, &ip->ino_data.uid,
2231 sizeof(uuid_uid)) ||
2232 bcmp(&uuid_gid, &ip->ino_data.gid,
2233 sizeof(uuid_gid)) ||
2234 ip->ino_data.mode != cur_mode
2236 ip->ino_data.uid = uuid_uid;
2237 ip->ino_data.gid = uuid_gid;
2238 ip->ino_data.mode = cur_mode;
2239 ip->ino_data.ctime = trans.time;
2240 modflags |= HAMMER_INODE_DDIRTY;
2242 kflags |= NOTE_ATTRIB;
2245 while (vap->va_size != VNOVAL && ip->ino_data.size != vap->va_size) {
2246 switch(ap->a_vp->v_type) {
2248 if (vap->va_size == ip->ino_data.size)
2252 * Log the operation if in fast-fsync mode or if
2253 * there are unterminated redo write records present.
2255 * The second check is needed so the recovery code
2256 * properly truncates write redos even if nominal
2257 * REDO operations is turned off due to excessive
2258 * writes, because the related records might be
2259 * destroyed and never lay down a TERM_WRITE.
2261 if ((ip->flags & HAMMER_INODE_REDO) ||
2262 (ip->flags & HAMMER_INODE_RDIRTY)) {
2263 error = hammer_generate_redo(&trans, ip,
2268 blksize = hammer_blocksize(vap->va_size);
2271 * XXX break atomicy, we can deadlock the backend
2272 * if we do not release the lock. Probably not a
2275 if (vap->va_size < ip->ino_data.size) {
2276 nvtruncbuf(ap->a_vp, vap->va_size,
2278 hammer_blockoff(vap->va_size));
2280 kflags |= NOTE_WRITE;
2282 nvextendbuf(ap->a_vp,
2285 hammer_blocksize(ip->ino_data.size),
2286 hammer_blocksize(vap->va_size),
2287 hammer_blockoff(ip->ino_data.size),
2288 hammer_blockoff(vap->va_size),
2291 kflags |= NOTE_WRITE | NOTE_EXTEND;
2293 ip->ino_data.size = vap->va_size;
2294 ip->ino_data.mtime = trans.time;
2295 /* XXX safe to use SDIRTY instead of DDIRTY here? */
2296 modflags |= HAMMER_INODE_MTIME | HAMMER_INODE_DDIRTY;
2299 * On-media truncation is cached in the inode until
2300 * the inode is synchronized. We must immediately
2301 * handle any frontend records.
2304 hammer_ip_frontend_trunc(ip, vap->va_size);
2305 #ifdef DEBUG_TRUNCATE
2306 if (HammerTruncIp == NULL)
2309 if ((ip->flags & HAMMER_INODE_TRUNCATED) == 0) {
2310 ip->flags |= HAMMER_INODE_TRUNCATED;
2311 ip->trunc_off = vap->va_size;
2312 #ifdef DEBUG_TRUNCATE
2313 if (ip == HammerTruncIp)
2314 kprintf("truncate1 %016llx\n",
2315 (long long)ip->trunc_off);
2317 } else if (ip->trunc_off > vap->va_size) {
2318 ip->trunc_off = vap->va_size;
2319 #ifdef DEBUG_TRUNCATE
2320 if (ip == HammerTruncIp)
2321 kprintf("truncate2 %016llx\n",
2322 (long long)ip->trunc_off);
2325 #ifdef DEBUG_TRUNCATE
2326 if (ip == HammerTruncIp)
2327 kprintf("truncate3 %016llx (ignored)\n",
2328 (long long)vap->va_size);
2335 * When truncating, nvtruncbuf() may have cleaned out
2336 * a portion of the last block on-disk in the buffer
2337 * cache. We must clean out any frontend records
2338 * for blocks beyond the new last block.
2340 aligned_size = (vap->va_size + (blksize - 1)) &
2341 ~(int64_t)(blksize - 1);
2342 if (truncating && vap->va_size < aligned_size) {
2343 aligned_size -= blksize;
2344 hammer_ip_frontend_trunc(ip, aligned_size);
2349 if ((ip->flags & HAMMER_INODE_TRUNCATED) == 0) {
2350 ip->flags |= HAMMER_INODE_TRUNCATED;
2351 ip->trunc_off = vap->va_size;
2352 } else if (ip->trunc_off > vap->va_size) {
2353 ip->trunc_off = vap->va_size;
2355 hammer_ip_frontend_trunc(ip, vap->va_size);
2356 ip->ino_data.size = vap->va_size;
2357 ip->ino_data.mtime = trans.time;
2358 modflags |= HAMMER_INODE_MTIME | HAMMER_INODE_DDIRTY;
2359 kflags |= NOTE_ATTRIB;
2367 if (vap->va_atime.tv_sec != VNOVAL) {
2368 ip->ino_data.atime = hammer_timespec_to_time(&vap->va_atime);
2369 modflags |= HAMMER_INODE_ATIME;
2370 kflags |= NOTE_ATTRIB;
2372 if (vap->va_mtime.tv_sec != VNOVAL) {
2373 ip->ino_data.mtime = hammer_timespec_to_time(&vap->va_mtime);
2374 modflags |= HAMMER_INODE_MTIME;
2375 kflags |= NOTE_ATTRIB;
2377 if (vap->va_mode != (mode_t)VNOVAL) {
2378 mode_t cur_mode = ip->ino_data.mode;
2379 uid_t cur_uid = hammer_to_unix_xid(&ip->ino_data.uid);
2380 gid_t cur_gid = hammer_to_unix_xid(&ip->ino_data.gid);
2382 error = vop_helper_chmod(ap->a_vp, vap->va_mode, ap->a_cred,
2383 cur_uid, cur_gid, &cur_mode);
2384 if (error == 0 && ip->ino_data.mode != cur_mode) {
2385 ip->ino_data.mode = cur_mode;
2386 ip->ino_data.ctime = trans.time;
2387 modflags |= HAMMER_INODE_DDIRTY;
2388 kflags |= NOTE_ATTRIB;
2393 hammer_modify_inode(&trans, ip, modflags);
2394 hammer_done_transaction(&trans);
2395 hammer_knote(ap->a_vp, kflags);
2396 lwkt_reltoken(&hmp->fs_token);
2401 * hammer_vop_nsymlink { nch, dvp, vpp, cred, vap, target }
2405 hammer_vop_nsymlink(struct vop_nsymlink_args *ap)
2407 struct hammer_transaction trans;
2408 struct hammer_inode *dip;
2409 struct hammer_inode *nip;
2410 hammer_record_t record;
2411 struct nchandle *nch;
2416 ap->a_vap->va_type = VLNK;
2419 dip = VTOI(ap->a_dvp);
2422 if (dip->flags & HAMMER_INODE_RO)
2424 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0)
2428 * Create a transaction to cover the operations we perform.
2430 lwkt_gettoken(&hmp->fs_token);
2431 hammer_start_transaction(&trans, hmp);
2432 ++hammer_stats_file_iopsw;
2435 * Create a new filesystem object of the requested type. The
2436 * returned inode will be referenced but not locked.
2439 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
2440 dip, nch->ncp->nc_name, nch->ncp->nc_nlen,
2443 hammer_done_transaction(&trans);
2445 lwkt_reltoken(&hmp->fs_token);
2450 * Add a record representing the symlink. symlink stores the link
2451 * as pure data, not a string, and is no \0 terminated.
2454 bytes = strlen(ap->a_target);
2456 if (bytes <= HAMMER_INODE_BASESYMLEN) {
2457 bcopy(ap->a_target, nip->ino_data.ext.symlink, bytes);
2459 record = hammer_alloc_mem_record(nip, bytes);
2460 record->type = HAMMER_MEM_RECORD_GENERAL;
2462 record->leaf.base.localization = nip->obj_localization +
2463 HAMMER_LOCALIZE_MISC;
2464 record->leaf.base.key = HAMMER_FIXKEY_SYMLINK;
2465 record->leaf.base.rec_type = HAMMER_RECTYPE_FIX;
2466 record->leaf.data_len = bytes;
2467 KKASSERT(HAMMER_SYMLINK_NAME_OFF == 0);
2468 bcopy(ap->a_target, record->data->symlink.name, bytes);
2469 error = hammer_ip_add_record(&trans, record);
2473 * Set the file size to the length of the link.
2476 nip->ino_data.size = bytes;
2477 hammer_modify_inode(&trans, nip, HAMMER_INODE_DDIRTY);
2481 error = hammer_ip_add_directory(&trans, dip, nch->ncp->nc_name,
2482 nch->ncp->nc_nlen, nip);
2488 hammer_rel_inode(nip, 0);
2491 error = hammer_get_vnode(nip, ap->a_vpp);
2492 hammer_rel_inode(nip, 0);
2494 cache_setunresolved(ap->a_nch);
2495 cache_setvp(ap->a_nch, *ap->a_vpp);
2496 hammer_knote(ap->a_dvp, NOTE_WRITE);
2499 hammer_done_transaction(&trans);
2500 lwkt_reltoken(&hmp->fs_token);
2505 * hammer_vop_nwhiteout { nch, dvp, cred, flags }
2509 hammer_vop_nwhiteout(struct vop_nwhiteout_args *ap)
2511 struct hammer_transaction trans;
2512 struct hammer_inode *dip;
2516 dip = VTOI(ap->a_dvp);
2519 if (hammer_nohistory(dip) == 0 &&
2520 (error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0) {
2524 lwkt_gettoken(&hmp->fs_token);
2525 hammer_start_transaction(&trans, hmp);
2526 ++hammer_stats_file_iopsw;
2527 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp,
2528 ap->a_cred, ap->a_flags, -1);
2529 hammer_done_transaction(&trans);
2530 lwkt_reltoken(&hmp->fs_token);
2536 * hammer_vop_ioctl { vp, command, data, fflag, cred }
2540 hammer_vop_ioctl(struct vop_ioctl_args *ap)
2542 struct hammer_inode *ip = ap->a_vp->v_data;
2543 hammer_mount_t hmp = ip->hmp;
2546 ++hammer_stats_file_iopsr;
2547 lwkt_gettoken(&hmp->fs_token);
2548 error = hammer_ioctl(ip, ap->a_command, ap->a_data,
2549 ap->a_fflag, ap->a_cred);
2550 lwkt_reltoken(&hmp->fs_token);
2556 hammer_vop_mountctl(struct vop_mountctl_args *ap)
2558 static const struct mountctl_opt extraopt[] = {
2559 { HMNT_NOHISTORY, "nohistory" },
2560 { HMNT_MASTERID, "master" },
2564 struct hammer_mount *hmp;
2571 mp = ap->a_head.a_ops->head.vv_mount;
2572 KKASSERT(mp->mnt_data != NULL);
2573 hmp = (struct hammer_mount *)mp->mnt_data;
2575 lwkt_gettoken(&hmp->fs_token);
2578 case MOUNTCTL_SET_EXPORT:
2579 if (ap->a_ctllen != sizeof(struct export_args))
2582 error = hammer_vfs_export(mp, ap->a_op,
2583 (const struct export_args *)ap->a_ctl);
2585 case MOUNTCTL_MOUNTFLAGS:
2588 * Call standard mountctl VOP function
2589 * so we get user mount flags.
2591 error = vop_stdmountctl(ap);
2595 usedbytes = *ap->a_res;
2597 if (usedbytes > 0 && usedbytes < ap->a_buflen) {
2598 usedbytes += vfs_flagstostr(hmp->hflags, extraopt,
2600 ap->a_buflen - usedbytes,
2604 *ap->a_res += usedbytes;
2608 error = vop_stdmountctl(ap);
2611 lwkt_reltoken(&hmp->fs_token);
2616 * hammer_vop_strategy { vp, bio }
2618 * Strategy call, used for regular file read & write only. Note that the
2619 * bp may represent a cluster.
2621 * To simplify operation and allow better optimizations in the future,
2622 * this code does not make any assumptions with regards to buffer alignment
2627 hammer_vop_strategy(struct vop_strategy_args *ap)
2632 bp = ap->a_bio->bio_buf;
2636 error = hammer_vop_strategy_read(ap);
2639 error = hammer_vop_strategy_write(ap);
2642 bp->b_error = error = EINVAL;
2643 bp->b_flags |= B_ERROR;
2648 /* hammer_dump_dedup_cache(((hammer_inode_t)ap->a_vp->v_data)->hmp); */
2654 * Read from a regular file. Iterate the related records and fill in the
2655 * BIO/BUF. Gaps are zero-filled.
2657 * The support code in hammer_object.c should be used to deal with mixed
2658 * in-memory and on-disk records.
2660 * NOTE: Can be called from the cluster code with an oversized buf.
2666 hammer_vop_strategy_read(struct vop_strategy_args *ap)
2668 struct hammer_transaction trans;
2669 struct hammer_inode *ip;
2670 struct hammer_inode *dip;
2672 struct hammer_cursor cursor;
2673 hammer_base_elm_t base;
2674 hammer_off_t disk_offset;
2689 ip = ap->a_vp->v_data;
2693 * The zone-2 disk offset may have been set by the cluster code via
2694 * a BMAP operation, or else should be NOOFFSET.
2696 * Checking the high bits for a match against zone-2 should suffice.
2698 * In cases where a lot of data duplication is present it may be
2699 * more beneficial to drop through and doubule-buffer through the
2702 nbio = push_bio(bio);
2703 if ((nbio->bio_offset & HAMMER_OFF_ZONE_MASK) ==
2704 HAMMER_ZONE_LARGE_DATA) {
2705 if (hammer_double_buffer == 0) {
2706 lwkt_gettoken(&hmp->fs_token);
2707 error = hammer_io_direct_read(hmp, nbio, NULL);
2708 lwkt_reltoken(&hmp->fs_token);
2713 * Try to shortcut requests for double_buffer mode too.
2714 * Since this mode runs through the device buffer cache
2715 * only compatible buffer sizes (meaning those generated
2716 * by normal filesystem buffers) are legal.
2718 if (hammer_live_dedup == 0 && (bp->b_flags & B_PAGING) == 0) {
2719 error = hammer_io_indirect_read(hmp, nbio, NULL);
2725 * Well, that sucked. Do it the hard way. If all the stars are
2726 * aligned we may still be able to issue a direct-read.
2728 lwkt_gettoken(&hmp->fs_token);
2729 hammer_simple_transaction(&trans, hmp);
2730 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
2733 * Key range (begin and end inclusive) to scan. Note that the key's
2734 * stored in the actual records represent BASE+LEN, not BASE. The
2735 * first record containing bio_offset will have a key > bio_offset.
2737 cursor.key_beg.localization = ip->obj_localization +
2738 HAMMER_LOCALIZE_MISC;
2739 cursor.key_beg.obj_id = ip->obj_id;
2740 cursor.key_beg.create_tid = 0;
2741 cursor.key_beg.delete_tid = 0;
2742 cursor.key_beg.obj_type = 0;
2743 cursor.key_beg.key = bio->bio_offset + 1;
2744 cursor.asof = ip->obj_asof;
2745 cursor.flags |= HAMMER_CURSOR_ASOF;
2747 cursor.key_end = cursor.key_beg;
2748 KKASSERT(ip->ino_data.obj_type == HAMMER_OBJTYPE_REGFILE);
2750 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_DBFILE) {
2751 cursor.key_beg.rec_type = HAMMER_RECTYPE_DB;
2752 cursor.key_end.rec_type = HAMMER_RECTYPE_DB;
2753 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
2757 ran_end = bio->bio_offset + bp->b_bufsize;
2758 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
2759 cursor.key_end.rec_type = HAMMER_RECTYPE_DATA;
2760 tmp64 = ran_end + MAXPHYS + 1; /* work-around GCC-4 bug */
2761 if (tmp64 < ran_end)
2762 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
2764 cursor.key_end.key = ran_end + MAXPHYS + 1;
2766 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
2769 * Set NOSWAPCACHE for cursor data extraction if double buffering
2770 * is disabled or (if the file is not marked cacheable via chflags
2771 * and vm.swapcache_use_chflags is enabled).
2773 if (hammer_double_buffer == 0 ||
2774 ((ap->a_vp->v_flag & VSWAPCACHE) == 0 &&
2775 vm_swapcache_use_chflags)) {
2776 cursor.flags |= HAMMER_CURSOR_NOSWAPCACHE;
2779 error = hammer_ip_first(&cursor);
2782 while (error == 0) {
2784 * Get the base file offset of the record. The key for
2785 * data records is (base + bytes) rather then (base).
2787 base = &cursor.leaf->base;
2788 rec_offset = base->key - cursor.leaf->data_len;
2791 * Calculate the gap, if any, and zero-fill it.
2793 * n is the offset of the start of the record verses our
2794 * current seek offset in the bio.
2796 n = (int)(rec_offset - (bio->bio_offset + boff));
2798 if (n > bp->b_bufsize - boff)
2799 n = bp->b_bufsize - boff;
2800 bzero((char *)bp->b_data + boff, n);
2806 * Calculate the data offset in the record and the number
2807 * of bytes we can copy.
2809 * There are two degenerate cases. First, boff may already
2810 * be at bp->b_bufsize. Secondly, the data offset within
2811 * the record may exceed the record's size.
2815 n = cursor.leaf->data_len - roff;
2817 kprintf("strategy_read: bad n=%d roff=%d\n", n, roff);
2819 } else if (n > bp->b_bufsize - boff) {
2820 n = bp->b_bufsize - boff;
2824 * Deal with cached truncations. This cool bit of code
2825 * allows truncate()/ftruncate() to avoid having to sync
2828 * If the frontend is truncated then all backend records are
2829 * subject to the frontend's truncation.
2831 * If the backend is truncated then backend records on-disk
2832 * (but not in-memory) are subject to the backend's
2833 * truncation. In-memory records owned by the backend
2834 * represent data written after the truncation point on the
2835 * backend and must not be truncated.
2837 * Truncate operations deal with frontend buffer cache
2838 * buffers and frontend-owned in-memory records synchronously.
2840 if (ip->flags & HAMMER_INODE_TRUNCATED) {
2841 if (hammer_cursor_ondisk(&cursor)/* ||
2842 cursor.iprec->flush_state == HAMMER_FST_FLUSH*/) {
2843 if (ip->trunc_off <= rec_offset)
2845 else if (ip->trunc_off < rec_offset + n)
2846 n = (int)(ip->trunc_off - rec_offset);
2849 if (ip->sync_flags & HAMMER_INODE_TRUNCATED) {
2850 if (hammer_cursor_ondisk(&cursor)) {
2851 if (ip->sync_trunc_off <= rec_offset)
2853 else if (ip->sync_trunc_off < rec_offset + n)
2854 n = (int)(ip->sync_trunc_off - rec_offset);
2859 * Try to issue a direct read into our bio if possible,
2860 * otherwise resolve the element data into a hammer_buffer
2863 * The buffer on-disk should be zerod past any real
2864 * truncation point, but may not be for any synthesized
2865 * truncation point from above.
2867 * NOTE: disk_offset is only valid if the cursor data is
2870 disk_offset = cursor.leaf->data_offset + roff;
2871 isdedupable = (boff == 0 && n == bp->b_bufsize &&
2872 hammer_cursor_ondisk(&cursor) &&
2873 ((int)disk_offset & HAMMER_BUFMASK) == 0);
2875 if (isdedupable && hammer_double_buffer == 0) {
2879 KKASSERT((disk_offset & HAMMER_OFF_ZONE_MASK) ==
2880 HAMMER_ZONE_LARGE_DATA);
2881 nbio->bio_offset = disk_offset;
2882 error = hammer_io_direct_read(hmp, nbio, cursor.leaf);
2883 if (hammer_live_dedup && error == 0)
2884 hammer_dedup_cache_add(ip, cursor.leaf);
2886 } else if (isdedupable) {
2888 * Async I/O case for reading from backing store
2889 * and copying the data to the filesystem buffer.
2890 * live-dedup has to verify the data anyway if it
2891 * gets a hit later so we can just add the entry
2894 KKASSERT((disk_offset & HAMMER_OFF_ZONE_MASK) ==
2895 HAMMER_ZONE_LARGE_DATA);
2896 nbio->bio_offset = disk_offset;
2897 if (hammer_live_dedup)
2898 hammer_dedup_cache_add(ip, cursor.leaf);
2899 error = hammer_io_indirect_read(hmp, nbio, cursor.leaf);
2902 error = hammer_ip_resolve_data(&cursor);
2904 if (hammer_live_dedup && isdedupable)
2905 hammer_dedup_cache_add(ip, cursor.leaf);
2906 bcopy((char *)cursor.data + roff,
2907 (char *)bp->b_data + boff, n);
2914 * We have to be sure that the only elements added to the
2915 * dedup cache are those which are already on-media.
2917 if (hammer_live_dedup && hammer_cursor_ondisk(&cursor))
2918 hammer_dedup_cache_add(ip, cursor.leaf);
2921 * Iterate until we have filled the request.
2924 if (boff == bp->b_bufsize)
2926 error = hammer_ip_next(&cursor);
2930 * There may have been a gap after the last record
2932 if (error == ENOENT)
2934 if (error == 0 && boff != bp->b_bufsize) {
2935 KKASSERT(boff < bp->b_bufsize);
2936 bzero((char *)bp->b_data + boff, bp->b_bufsize - boff);
2937 /* boff = bp->b_bufsize; */
2941 * Disallow swapcache operation on the vnode buffer if double
2942 * buffering is enabled, the swapcache will get the data via
2943 * the block device buffer.
2945 if (hammer_double_buffer)
2946 bp->b_flags |= B_NOTMETA;
2952 bp->b_error = error;
2954 bp->b_flags |= B_ERROR;
2959 * Cache the b-tree node for the last data read in cache[1].
2961 * If we hit the file EOF then also cache the node in the
2962 * governing director's cache[3], it will be used to initialize
2963 * the inode's cache[1] for any inodes looked up via the directory.
2965 * This doesn't reduce disk accesses since the B-Tree chain is
2966 * likely cached, but it does reduce cpu overhead when looking
2967 * up file offsets for cpdup/tar/cpio style iterations.
2970 hammer_cache_node(&ip->cache[1], cursor.node);
2971 if (ran_end >= ip->ino_data.size) {
2972 dip = hammer_find_inode(&trans, ip->ino_data.parent_obj_id,
2973 ip->obj_asof, ip->obj_localization);
2975 hammer_cache_node(&dip->cache[3], cursor.node);
2976 hammer_rel_inode(dip, 0);
2979 hammer_done_cursor(&cursor);
2980 hammer_done_transaction(&trans);
2981 lwkt_reltoken(&hmp->fs_token);
2986 * BMAP operation - used to support cluster_read() only.
2988 * (struct vnode *vp, off_t loffset, off_t *doffsetp, int *runp, int *runb)
2990 * This routine may return EOPNOTSUPP if the opration is not supported for
2991 * the specified offset. The contents of the pointer arguments do not
2992 * need to be initialized in that case.
2994 * If a disk address is available and properly aligned return 0 with
2995 * *doffsetp set to the zone-2 address, and *runp / *runb set appropriately
2996 * to the run-length relative to that offset. Callers may assume that
2997 * *doffsetp is valid if 0 is returned, even if *runp is not sufficiently
2998 * large, so return EOPNOTSUPP if it is not sufficiently large.
3002 hammer_vop_bmap(struct vop_bmap_args *ap)
3004 struct hammer_transaction trans;
3005 struct hammer_inode *ip;
3007 struct hammer_cursor cursor;
3008 hammer_base_elm_t base;
3012 int64_t base_offset;
3013 int64_t base_disk_offset;
3014 int64_t last_offset;
3015 hammer_off_t last_disk_offset;
3016 hammer_off_t disk_offset;
3021 ++hammer_stats_file_iopsr;
3022 ip = ap->a_vp->v_data;
3026 * We can only BMAP regular files. We can't BMAP database files,
3029 if (ip->ino_data.obj_type != HAMMER_OBJTYPE_REGFILE)
3033 * bmap is typically called with runp/runb both NULL when used
3034 * for writing. We do not support BMAP for writing atm.
3036 if (ap->a_cmd != BUF_CMD_READ)
3040 * Scan the B-Tree to acquire blockmap addresses, then translate
3043 lwkt_gettoken(&hmp->fs_token);
3044 hammer_simple_transaction(&trans, hmp);
3046 kprintf("bmap_beg %016llx ip->cache %p\n",
3047 (long long)ap->a_loffset, ip->cache[1]);
3049 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
3052 * Key range (begin and end inclusive) to scan. Note that the key's
3053 * stored in the actual records represent BASE+LEN, not BASE. The
3054 * first record containing bio_offset will have a key > bio_offset.
3056 cursor.key_beg.localization = ip->obj_localization +
3057 HAMMER_LOCALIZE_MISC;
3058 cursor.key_beg.obj_id = ip->obj_id;
3059 cursor.key_beg.create_tid = 0;
3060 cursor.key_beg.delete_tid = 0;
3061 cursor.key_beg.obj_type = 0;
3063 cursor.key_beg.key = ap->a_loffset - MAXPHYS + 1;
3065 cursor.key_beg.key = ap->a_loffset + 1;
3066 if (cursor.key_beg.key < 0)
3067 cursor.key_beg.key = 0;
3068 cursor.asof = ip->obj_asof;
3069 cursor.flags |= HAMMER_CURSOR_ASOF;
3071 cursor.key_end = cursor.key_beg;
3072 KKASSERT(ip->ino_data.obj_type == HAMMER_OBJTYPE_REGFILE);
3074 ran_end = ap->a_loffset + MAXPHYS;
3075 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
3076 cursor.key_end.rec_type = HAMMER_RECTYPE_DATA;
3077 tmp64 = ran_end + MAXPHYS + 1; /* work-around GCC-4 bug */
3078 if (tmp64 < ran_end)
3079 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
3081 cursor.key_end.key = ran_end + MAXPHYS + 1;
3083 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
3085 error = hammer_ip_first(&cursor);
3086 base_offset = last_offset = 0;
3087 base_disk_offset = last_disk_offset = 0;
3089 while (error == 0) {
3091 * Get the base file offset of the record. The key for
3092 * data records is (base + bytes) rather then (base).
3094 * NOTE: rec_offset + rec_len may exceed the end-of-file.
3095 * The extra bytes should be zero on-disk and the BMAP op
3096 * should still be ok.
3098 base = &cursor.leaf->base;
3099 rec_offset = base->key - cursor.leaf->data_len;
3100 rec_len = cursor.leaf->data_len;
3103 * Incorporate any cached truncation.
3105 * NOTE: Modifications to rec_len based on synthesized
3106 * truncation points remove the guarantee that any extended
3107 * data on disk is zero (since the truncations may not have
3108 * taken place on-media yet).
3110 if (ip->flags & HAMMER_INODE_TRUNCATED) {
3111 if (hammer_cursor_ondisk(&cursor) ||
3112 cursor.iprec->flush_state == HAMMER_FST_FLUSH) {
3113 if (ip->trunc_off <= rec_offset)
3115 else if (ip->trunc_off < rec_offset + rec_len)
3116 rec_len = (int)(ip->trunc_off - rec_offset);
3119 if (ip->sync_flags & HAMMER_INODE_TRUNCATED) {
3120 if (hammer_cursor_ondisk(&cursor)) {
3121 if (ip->sync_trunc_off <= rec_offset)
3123 else if (ip->sync_trunc_off < rec_offset + rec_len)
3124 rec_len = (int)(ip->sync_trunc_off - rec_offset);
3129 * Accumulate information. If we have hit a discontiguous
3130 * block reset base_offset unless we are already beyond the
3131 * requested offset. If we are, that's it, we stop.
3135 if (hammer_cursor_ondisk(&cursor)) {
3136 disk_offset = cursor.leaf->data_offset;
3137 if (rec_offset != last_offset ||
3138 disk_offset != last_disk_offset) {
3139 if (rec_offset > ap->a_loffset)
3141 base_offset = rec_offset;
3142 base_disk_offset = disk_offset;
3144 last_offset = rec_offset + rec_len;
3145 last_disk_offset = disk_offset + rec_len;
3147 if (hammer_live_dedup)
3148 hammer_dedup_cache_add(ip, cursor.leaf);
3151 error = hammer_ip_next(&cursor);
3155 kprintf("BMAP %016llx: %016llx - %016llx\n",
3156 (long long)ap->a_loffset,
3157 (long long)base_offset,
3158 (long long)last_offset);
3159 kprintf("BMAP %16s: %016llx - %016llx\n", "",
3160 (long long)base_disk_offset,
3161 (long long)last_disk_offset);
3165 hammer_cache_node(&ip->cache[1], cursor.node);
3167 kprintf("bmap_end2 %016llx ip->cache %p\n",
3168 (long long)ap->a_loffset, ip->cache[1]);
3171 hammer_done_cursor(&cursor);
3172 hammer_done_transaction(&trans);
3173 lwkt_reltoken(&hmp->fs_token);
3176 * If we couldn't find any records or the records we did find were
3177 * all behind the requested offset, return failure. A forward
3178 * truncation can leave a hole w/ no on-disk records.
3180 if (last_offset == 0 || last_offset < ap->a_loffset)
3181 return (EOPNOTSUPP);
3184 * Figure out the block size at the requested offset and adjust
3185 * our limits so the cluster_read() does not create inappropriately
3186 * sized buffer cache buffers.
3188 blksize = hammer_blocksize(ap->a_loffset);
3189 if (hammer_blocksize(base_offset) != blksize) {
3190 base_offset = hammer_blockdemarc(base_offset, ap->a_loffset);
3192 if (last_offset != ap->a_loffset &&
3193 hammer_blocksize(last_offset - 1) != blksize) {
3194 last_offset = hammer_blockdemarc(ap->a_loffset,
3199 * Returning EOPNOTSUPP simply prevents the direct-IO optimization
3202 disk_offset = base_disk_offset + (ap->a_loffset - base_offset);
3204 if ((disk_offset & HAMMER_OFF_ZONE_MASK) != HAMMER_ZONE_LARGE_DATA) {
3206 * Only large-data zones can be direct-IOd
3209 } else if ((disk_offset & HAMMER_BUFMASK) ||
3210 (last_offset - ap->a_loffset) < blksize) {
3212 * doffsetp is not aligned or the forward run size does
3213 * not cover a whole buffer, disallow the direct I/O.
3220 *ap->a_doffsetp = disk_offset;
3222 *ap->a_runb = ap->a_loffset - base_offset;
3223 KKASSERT(*ap->a_runb >= 0);
3226 *ap->a_runp = last_offset - ap->a_loffset;
3227 KKASSERT(*ap->a_runp >= 0);
3235 * Write to a regular file. Because this is a strategy call the OS is
3236 * trying to actually get data onto the media.
3240 hammer_vop_strategy_write(struct vop_strategy_args *ap)
3242 hammer_record_t record;
3253 ip = ap->a_vp->v_data;
3256 blksize = hammer_blocksize(bio->bio_offset);
3257 KKASSERT(bp->b_bufsize == blksize);
3259 if (ip->flags & HAMMER_INODE_RO) {
3260 bp->b_error = EROFS;
3261 bp->b_flags |= B_ERROR;
3266 lwkt_gettoken(&hmp->fs_token);
3269 * Disallow swapcache operation on the vnode buffer if double
3270 * buffering is enabled, the swapcache will get the data via
3271 * the block device buffer.
3273 if (hammer_double_buffer)
3274 bp->b_flags |= B_NOTMETA;
3277 * Interlock with inode destruction (no in-kernel or directory
3278 * topology visibility). If we queue new IO while trying to
3279 * destroy the inode we can deadlock the vtrunc call in
3280 * hammer_inode_unloadable_check().
3282 * Besides, there's no point flushing a bp associated with an
3283 * inode that is being destroyed on-media and has no kernel
3286 if ((ip->flags | ip->sync_flags) &
3287 (HAMMER_INODE_DELETING|HAMMER_INODE_DELETED)) {
3290 lwkt_reltoken(&hmp->fs_token);
3295 * Reserve space and issue a direct-write from the front-end.
3296 * NOTE: The direct_io code will hammer_bread/bcopy smaller
3299 * An in-memory record will be installed to reference the storage
3300 * until the flusher can get to it.
3302 * Since we own the high level bio the front-end will not try to
3303 * do a direct-read until the write completes.
3305 * NOTE: The only time we do not reserve a full-sized buffers
3306 * worth of data is if the file is small. We do not try to
3307 * allocate a fragment (from the small-data zone) at the end of
3308 * an otherwise large file as this can lead to wildly separated
3311 KKASSERT((bio->bio_offset & HAMMER_BUFMASK) == 0);
3312 KKASSERT(bio->bio_offset < ip->ino_data.size);
3313 if (bio->bio_offset || ip->ino_data.size > HAMMER_BUFSIZE / 2)
3314 bytes = bp->b_bufsize;
3316 bytes = ((int)ip->ino_data.size + 15) & ~15;
3318 record = hammer_ip_add_bulk(ip, bio->bio_offset, bp->b_data,
3322 * B_VFSFLAG1 indicates that a REDO_WRITE entry was generated
3323 * in hammer_vop_write(). We must flag the record so the proper
3324 * REDO_TERM_WRITE entry is generated during the flush.
3327 if (bp->b_flags & B_VFSFLAG1) {
3328 record->flags |= HAMMER_RECF_REDO;
3329 bp->b_flags &= ~B_VFSFLAG1;
3331 if (record->flags & HAMMER_RECF_DEDUPED) {
3333 hammer_ip_replace_bulk(hmp, record);
3336 hammer_io_direct_write(hmp, bio, record);
3338 if (ip->rsv_recs > 1 && hmp->rsv_recs > hammer_limit_recs)
3339 hammer_flush_inode(ip, 0);
3341 bp->b_bio2.bio_offset = NOOFFSET;
3342 bp->b_error = error;
3343 bp->b_flags |= B_ERROR;
3346 lwkt_reltoken(&hmp->fs_token);
3351 * dounlink - disconnect a directory entry
3353 * XXX whiteout support not really in yet
3356 hammer_dounlink(hammer_transaction_t trans, struct nchandle *nch,
3357 struct vnode *dvp, struct ucred *cred,
3358 int flags, int isdir)
3360 struct namecache *ncp;
3364 struct hammer_cursor cursor;
3366 u_int32_t max_iterations;
3370 * Calculate the namekey and setup the key range for the scan. This
3371 * works kinda like a chained hash table where the lower 32 bits
3372 * of the namekey synthesize the chain.
3374 * The key range is inclusive of both key_beg and key_end.
3380 if (dip->flags & HAMMER_INODE_RO)
3383 namekey = hammer_directory_namekey(dip, ncp->nc_name, ncp->nc_nlen,
3386 hammer_init_cursor(trans, &cursor, &dip->cache[1], dip);
3387 cursor.key_beg.localization = dip->obj_localization +
3388 hammer_dir_localization(dip);
3389 cursor.key_beg.obj_id = dip->obj_id;
3390 cursor.key_beg.key = namekey;
3391 cursor.key_beg.create_tid = 0;
3392 cursor.key_beg.delete_tid = 0;
3393 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
3394 cursor.key_beg.obj_type = 0;
3396 cursor.key_end = cursor.key_beg;
3397 cursor.key_end.key += max_iterations;
3398 cursor.asof = dip->obj_asof;
3399 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
3402 * Scan all matching records (the chain), locate the one matching
3403 * the requested path component. info->last_error contains the
3404 * error code on search termination and could be 0, ENOENT, or
3407 * The hammer_ip_*() functions merge in-memory records with on-disk
3408 * records for the purposes of the search.
3410 error = hammer_ip_first(&cursor);
3412 while (error == 0) {
3413 error = hammer_ip_resolve_data(&cursor);
3416 nlen = cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF;
3418 if (ncp->nc_nlen == nlen &&
3419 bcmp(ncp->nc_name, cursor.data->entry.name, nlen) == 0) {
3422 error = hammer_ip_next(&cursor);
3426 * If all is ok we have to get the inode so we can adjust nlinks.
3427 * To avoid a deadlock with the flusher we must release the inode
3428 * lock on the directory when acquiring the inode for the entry.
3430 * If the target is a directory, it must be empty.
3433 hammer_unlock(&cursor.ip->lock);
3434 ip = hammer_get_inode(trans, dip, cursor.data->entry.obj_id,
3436 cursor.data->entry.localization,
3438 hammer_lock_sh(&cursor.ip->lock);
3439 if (error == ENOENT) {
3440 kprintf("HAMMER: WARNING: Removing "
3441 "dirent w/missing inode \"%s\"\n"
3442 "\tobj_id = %016llx\n",
3444 (long long)cursor.data->entry.obj_id);
3449 * If isdir >= 0 we validate that the entry is or is not a
3450 * directory. If isdir < 0 we don't care.
3452 if (error == 0 && isdir >= 0 && ip) {
3454 ip->ino_data.obj_type != HAMMER_OBJTYPE_DIRECTORY) {
3456 } else if (isdir == 0 &&
3457 ip->ino_data.obj_type == HAMMER_OBJTYPE_DIRECTORY) {
3463 * If we are trying to remove a directory the directory must
3466 * The check directory code can loop and deadlock/retry. Our
3467 * own cursor's node locks must be released to avoid a 3-way
3468 * deadlock with the flusher if the check directory code
3471 * If any changes whatsoever have been made to the cursor
3472 * set EDEADLK and retry.
3474 * WARNING: See warnings in hammer_unlock_cursor()
3477 if (error == 0 && ip && ip->ino_data.obj_type ==
3478 HAMMER_OBJTYPE_DIRECTORY) {
3479 hammer_unlock_cursor(&cursor);
3480 error = hammer_ip_check_directory_empty(trans, ip);
3481 hammer_lock_cursor(&cursor);
3482 if (cursor.flags & HAMMER_CURSOR_RETEST) {
3483 kprintf("HAMMER: Warning: avoided deadlock "
3491 * Delete the directory entry.
3493 * WARNING: hammer_ip_del_directory() may have to terminate
3494 * the cursor to avoid a deadlock. It is ok to call
3495 * hammer_done_cursor() twice.
3498 error = hammer_ip_del_directory(trans, &cursor,
3501 hammer_done_cursor(&cursor);
3503 cache_setunresolved(nch);
3504 cache_setvp(nch, NULL);
3507 * NOTE: ip->vp, if non-NULL, cannot be directly
3508 * referenced without formally acquiring the
3509 * vp since the vp might have zero refs on it,
3510 * or in the middle of a reclaim, etc.
3512 * NOTE: The cache_setunresolved() can rip the vp
3513 * out from under us since the vp may not have
3514 * any refs, in which case ip->vp will be NULL
3517 while (ip && ip->vp) {
3520 error = hammer_get_vnode(ip, &vp);
3521 if (error == 0 && vp) {
3523 hammer_knote(ip->vp, NOTE_DELETE);
3524 cache_inval_vp(ip->vp, CINV_DESTROY);
3528 kprintf("Debug: HAMMER ip/vp race1 avoided\n");
3532 hammer_rel_inode(ip, 0);
3534 hammer_done_cursor(&cursor);
3536 if (error == EDEADLK)
3542 /************************************************************************
3543 * FIFO AND SPECFS OPS *
3544 ************************************************************************
3548 hammer_vop_fifoclose (struct vop_close_args *ap)
3550 /* XXX update itimes */
3551 return (VOCALL(&fifo_vnode_vops, &ap->a_head));
3555 hammer_vop_fiforead (struct vop_read_args *ap)
3559 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
3560 /* XXX update access time */
3565 hammer_vop_fifowrite (struct vop_write_args *ap)
3569 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
3570 /* XXX update access time */
3576 hammer_vop_fifokqfilter(struct vop_kqfilter_args *ap)
3580 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
3582 error = hammer_vop_kqfilter(ap);
3586 /************************************************************************
3588 ************************************************************************
3591 static void filt_hammerdetach(struct knote *kn);
3592 static int filt_hammerread(struct knote *kn, long hint);
3593 static int filt_hammerwrite(struct knote *kn, long hint);
3594 static int filt_hammervnode(struct knote *kn, long hint);
3596 static struct filterops hammerread_filtops =
3597 { FILTEROP_ISFD, NULL, filt_hammerdetach, filt_hammerread };
3598 static struct filterops hammerwrite_filtops =
3599 { FILTEROP_ISFD, NULL, filt_hammerdetach, filt_hammerwrite };
3600 static struct filterops hammervnode_filtops =
3601 { FILTEROP_ISFD, NULL, filt_hammerdetach, filt_hammervnode };
3605 hammer_vop_kqfilter(struct vop_kqfilter_args *ap)
3607 struct vnode *vp = ap->a_vp;
3608 struct knote *kn = ap->a_kn;
3610 switch (kn->kn_filter) {
3612 kn->kn_fop = &hammerread_filtops;
3615 kn->kn_fop = &hammerwrite_filtops;
3618 kn->kn_fop = &hammervnode_filtops;
3621 return (EOPNOTSUPP);
3624 kn->kn_hook = (caddr_t)vp;
3626 knote_insert(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
3632 filt_hammerdetach(struct knote *kn)
3634 struct vnode *vp = (void *)kn->kn_hook;
3636 knote_remove(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
3640 filt_hammerread(struct knote *kn, long hint)
3642 struct vnode *vp = (void *)kn->kn_hook;
3643 hammer_inode_t ip = VTOI(vp);
3644 hammer_mount_t hmp = ip->hmp;
3647 if (hint == NOTE_REVOKE) {
3648 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
3651 lwkt_gettoken(&hmp->fs_token); /* XXX use per-ip-token */
3652 off = ip->ino_data.size - kn->kn_fp->f_offset;
3653 kn->kn_data = (off < INTPTR_MAX) ? off : INTPTR_MAX;
3654 lwkt_reltoken(&hmp->fs_token);
3655 if (kn->kn_sfflags & NOTE_OLDAPI)
3657 return (kn->kn_data != 0);
3661 filt_hammerwrite(struct knote *kn, long hint)
3663 if (hint == NOTE_REVOKE)
3664 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
3670 filt_hammervnode(struct knote *kn, long hint)
3672 if (kn->kn_sfflags & hint)
3673 kn->kn_fflags |= hint;
3674 if (hint == NOTE_REVOKE) {
3675 kn->kn_flags |= EV_EOF;
3678 return (kn->kn_fflags != 0);