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 <vfs/fifofs/fifo.h>
55 /*static int hammer_vop_vnoperate(struct vop_generic_args *);*/
56 static int hammer_vop_fsync(struct vop_fsync_args *);
57 static int hammer_vop_read(struct vop_read_args *);
58 static int hammer_vop_write(struct vop_write_args *);
59 static int hammer_vop_access(struct vop_access_args *);
60 static int hammer_vop_advlock(struct vop_advlock_args *);
61 static int hammer_vop_close(struct vop_close_args *);
62 static int hammer_vop_ncreate(struct vop_ncreate_args *);
63 static int hammer_vop_getattr(struct vop_getattr_args *);
64 static int hammer_vop_nresolve(struct vop_nresolve_args *);
65 static int hammer_vop_nlookupdotdot(struct vop_nlookupdotdot_args *);
66 static int hammer_vop_nlink(struct vop_nlink_args *);
67 static int hammer_vop_nmkdir(struct vop_nmkdir_args *);
68 static int hammer_vop_nmknod(struct vop_nmknod_args *);
69 static int hammer_vop_open(struct vop_open_args *);
70 static int hammer_vop_print(struct vop_print_args *);
71 static int hammer_vop_readdir(struct vop_readdir_args *);
72 static int hammer_vop_readlink(struct vop_readlink_args *);
73 static int hammer_vop_nremove(struct vop_nremove_args *);
74 static int hammer_vop_nrename(struct vop_nrename_args *);
75 static int hammer_vop_nrmdir(struct vop_nrmdir_args *);
76 static int hammer_vop_markatime(struct vop_markatime_args *);
77 static int hammer_vop_setattr(struct vop_setattr_args *);
78 static int hammer_vop_strategy(struct vop_strategy_args *);
79 static int hammer_vop_bmap(struct vop_bmap_args *ap);
80 static int hammer_vop_nsymlink(struct vop_nsymlink_args *);
81 static int hammer_vop_nwhiteout(struct vop_nwhiteout_args *);
82 static int hammer_vop_ioctl(struct vop_ioctl_args *);
83 static int hammer_vop_mountctl(struct vop_mountctl_args *);
84 static int hammer_vop_kqfilter (struct vop_kqfilter_args *);
86 static int hammer_vop_fifoclose (struct vop_close_args *);
87 static int hammer_vop_fiforead (struct vop_read_args *);
88 static int hammer_vop_fifowrite (struct vop_write_args *);
89 static int hammer_vop_fifokqfilter (struct vop_kqfilter_args *);
91 struct vop_ops hammer_vnode_vops = {
92 .vop_default = vop_defaultop,
93 .vop_fsync = hammer_vop_fsync,
94 .vop_getpages = vop_stdgetpages,
95 .vop_putpages = vop_stdputpages,
96 .vop_read = hammer_vop_read,
97 .vop_write = hammer_vop_write,
98 .vop_access = hammer_vop_access,
99 .vop_advlock = hammer_vop_advlock,
100 .vop_close = hammer_vop_close,
101 .vop_ncreate = hammer_vop_ncreate,
102 .vop_getattr = hammer_vop_getattr,
103 .vop_inactive = hammer_vop_inactive,
104 .vop_reclaim = hammer_vop_reclaim,
105 .vop_nresolve = hammer_vop_nresolve,
106 .vop_nlookupdotdot = hammer_vop_nlookupdotdot,
107 .vop_nlink = hammer_vop_nlink,
108 .vop_nmkdir = hammer_vop_nmkdir,
109 .vop_nmknod = hammer_vop_nmknod,
110 .vop_open = hammer_vop_open,
111 .vop_pathconf = vop_stdpathconf,
112 .vop_print = hammer_vop_print,
113 .vop_readdir = hammer_vop_readdir,
114 .vop_readlink = hammer_vop_readlink,
115 .vop_nremove = hammer_vop_nremove,
116 .vop_nrename = hammer_vop_nrename,
117 .vop_nrmdir = hammer_vop_nrmdir,
118 .vop_markatime = hammer_vop_markatime,
119 .vop_setattr = hammer_vop_setattr,
120 .vop_bmap = hammer_vop_bmap,
121 .vop_strategy = hammer_vop_strategy,
122 .vop_nsymlink = hammer_vop_nsymlink,
123 .vop_nwhiteout = hammer_vop_nwhiteout,
124 .vop_ioctl = hammer_vop_ioctl,
125 .vop_mountctl = hammer_vop_mountctl,
126 .vop_kqfilter = hammer_vop_kqfilter
129 struct vop_ops hammer_spec_vops = {
130 .vop_default = vop_defaultop,
131 .vop_fsync = hammer_vop_fsync,
132 .vop_read = vop_stdnoread,
133 .vop_write = vop_stdnowrite,
134 .vop_access = hammer_vop_access,
135 .vop_close = hammer_vop_close,
136 .vop_markatime = hammer_vop_markatime,
137 .vop_getattr = hammer_vop_getattr,
138 .vop_inactive = hammer_vop_inactive,
139 .vop_reclaim = hammer_vop_reclaim,
140 .vop_setattr = hammer_vop_setattr
143 struct vop_ops hammer_fifo_vops = {
144 .vop_default = fifo_vnoperate,
145 .vop_fsync = hammer_vop_fsync,
146 .vop_read = hammer_vop_fiforead,
147 .vop_write = hammer_vop_fifowrite,
148 .vop_access = hammer_vop_access,
149 .vop_close = hammer_vop_fifoclose,
150 .vop_markatime = hammer_vop_markatime,
151 .vop_getattr = hammer_vop_getattr,
152 .vop_inactive = hammer_vop_inactive,
153 .vop_reclaim = hammer_vop_reclaim,
154 .vop_setattr = hammer_vop_setattr,
155 .vop_kqfilter = hammer_vop_fifokqfilter
160 hammer_knote(struct vnode *vp, int flags)
163 KNOTE(&vp->v_pollinfo.vpi_selinfo.si_note, flags);
166 #ifdef DEBUG_TRUNCATE
167 struct hammer_inode *HammerTruncIp;
170 static int hammer_dounlink(hammer_transaction_t trans, struct nchandle *nch,
171 struct vnode *dvp, struct ucred *cred,
172 int flags, int isdir);
173 static int hammer_vop_strategy_read(struct vop_strategy_args *ap);
174 static int hammer_vop_strategy_write(struct vop_strategy_args *ap);
179 hammer_vop_vnoperate(struct vop_generic_args *)
181 return (VOCALL(&hammer_vnode_vops, ap));
186 * hammer_vop_fsync { vp, waitfor }
188 * fsync() an inode to disk and wait for it to be completely committed
189 * such that the information would not be undone if a crash occured after
194 hammer_vop_fsync(struct vop_fsync_args *ap)
196 hammer_inode_t ip = VTOI(ap->a_vp);
198 ++hammer_count_fsyncs;
199 vfsync(ap->a_vp, ap->a_waitfor, 1, NULL, NULL);
200 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
201 if (ap->a_waitfor == MNT_WAIT) {
203 hammer_wait_inode(ip);
204 vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY);
210 * hammer_vop_read { vp, uio, ioflag, cred }
216 hammer_vop_read(struct vop_read_args *ap)
218 struct hammer_transaction trans;
231 if (ap->a_vp->v_type != VREG)
238 * Allow the UIO's size to override the sequential heuristic.
240 blksize = hammer_blocksize(uio->uio_offset);
241 seqcount = (uio->uio_resid + (blksize - 1)) / blksize;
242 ioseqcount = ap->a_ioflag >> 16;
243 if (seqcount < ioseqcount)
244 seqcount = ioseqcount;
247 * Temporary hack until more of HAMMER can be made MPSAFE.
250 if (curthread->td_mpcount) {
252 hammer_start_transaction(&trans, ip->hmp);
257 hammer_start_transaction(&trans, ip->hmp);
262 * If reading or writing a huge amount of data we have to break
263 * atomicy and allow the operation to be interrupted by a signal
264 * or it can DOS the machine.
266 bigread = (uio->uio_resid > 100 * 1024 * 1024);
269 * Access the data typically in HAMMER_BUFSIZE blocks via the
270 * buffer cache, but HAMMER may use a variable block size based
273 * XXX Temporary hack, delay the start transaction while we remain
274 * MPSAFE. NOTE: ino_data.size cannot change while vnode is
277 while (uio->uio_resid > 0 && uio->uio_offset < ip->ino_data.size) {
281 blksize = hammer_blocksize(uio->uio_offset);
282 offset = (int)uio->uio_offset & (blksize - 1);
283 base_offset = uio->uio_offset - offset;
285 if (bigread && (error = hammer_signal_check(ip->hmp)) != 0)
291 bp = getcacheblk(ap->a_vp, base_offset);
300 if (got_mplock == 0) {
303 hammer_start_transaction(&trans, ip->hmp);
306 if (hammer_cluster_enable) {
308 * Use file_limit to prevent cluster_read() from
309 * creating buffers of the wrong block size past
312 file_limit = ip->ino_data.size;
313 if (base_offset < HAMMER_XDEMARC &&
314 file_limit > HAMMER_XDEMARC) {
315 file_limit = HAMMER_XDEMARC;
317 error = cluster_read(ap->a_vp,
318 file_limit, base_offset,
322 error = bread(ap->a_vp, base_offset, blksize, &bp);
325 kprintf("error %d\n", error);
331 /* bp->b_flags |= B_CLUSTEROK; temporarily disabled */
332 n = blksize - offset;
333 if (n > uio->uio_resid)
335 if (n > ip->ino_data.size - uio->uio_offset)
336 n = (int)(ip->ino_data.size - uio->uio_offset);
337 error = uiomove((char *)bp->b_data + offset, n, uio);
339 /* data has a lower priority then meta-data */
340 bp->b_flags |= B_AGE;
344 hammer_stats_file_read += n;
348 * XXX only update the atime if we had to get the MP lock.
349 * XXX hack hack hack, fixme.
352 if ((ip->flags & HAMMER_INODE_RO) == 0 &&
353 (ip->hmp->mp->mnt_flag & MNT_NOATIME) == 0) {
354 ip->ino_data.atime = trans.time;
355 hammer_modify_inode(ip, HAMMER_INODE_ATIME);
357 hammer_done_transaction(&trans);
365 * hammer_vop_write { vp, uio, ioflag, cred }
369 hammer_vop_write(struct vop_write_args *ap)
371 struct hammer_transaction trans;
372 struct hammer_inode *ip;
385 if (ap->a_vp->v_type != VREG)
391 seqcount = ap->a_ioflag >> 16;
393 if (ip->flags & HAMMER_INODE_RO)
397 * Create a transaction to cover the operations we perform.
399 hammer_start_transaction(&trans, hmp);
405 if (ap->a_ioflag & IO_APPEND)
406 uio->uio_offset = ip->ino_data.size;
409 * Check for illegal write offsets. Valid range is 0...2^63-1.
411 * NOTE: the base_off assignment is required to work around what
412 * I consider to be a GCC-4 optimization bug.
414 if (uio->uio_offset < 0) {
415 hammer_done_transaction(&trans);
418 base_offset = uio->uio_offset + uio->uio_resid; /* work around gcc-4 */
419 if (uio->uio_resid > 0 && base_offset <= uio->uio_offset) {
420 hammer_done_transaction(&trans);
425 * If reading or writing a huge amount of data we have to break
426 * atomicy and allow the operation to be interrupted by a signal
427 * or it can DOS the machine.
429 bigwrite = (uio->uio_resid > 100 * 1024 * 1024);
432 * Access the data typically in HAMMER_BUFSIZE blocks via the
433 * buffer cache, but HAMMER may use a variable block size based
436 while (uio->uio_resid > 0) {
441 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_WRITE)) != 0)
443 if (bigwrite && (error = hammer_signal_check(hmp)) != 0)
446 blksize = hammer_blocksize(uio->uio_offset);
449 * Do not allow HAMMER to blow out the buffer cache. Very
450 * large UIOs can lockout other processes due to bwillwrite()
453 * The hammer inode is not locked during these operations.
454 * The vnode is locked which can interfere with the pageout
455 * daemon for non-UIO_NOCOPY writes but should not interfere
456 * with the buffer cache. Even so, we cannot afford to
457 * allow the pageout daemon to build up too many dirty buffer
460 * Only call this if we aren't being recursively called from
461 * a virtual disk device (vn), else we may deadlock.
463 if ((ap->a_ioflag & IO_RECURSE) == 0)
467 * Control the number of pending records associated with
468 * this inode. If too many have accumulated start a
469 * flush. Try to maintain a pipeline with the flusher.
471 if (ip->rsv_recs >= hammer_limit_inode_recs) {
472 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
474 if (ip->rsv_recs >= hammer_limit_inode_recs * 2) {
475 while (ip->rsv_recs >= hammer_limit_inode_recs) {
476 tsleep(&ip->rsv_recs, 0, "hmrwww", hz);
478 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
483 * Do not allow HAMMER to blow out system memory by
484 * accumulating too many records. Records are so well
485 * decoupled from the buffer cache that it is possible
486 * for userland to push data out to the media via
487 * direct-write, but build up the records queued to the
488 * backend faster then the backend can flush them out.
489 * HAMMER has hit its write limit but the frontend has
490 * no pushback to slow it down.
492 if (hmp->rsv_recs > hammer_limit_recs / 2) {
494 * Get the inode on the flush list
496 if (ip->rsv_recs >= 64)
497 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
498 else if (ip->rsv_recs >= 16)
499 hammer_flush_inode(ip, 0);
502 * Keep the flusher going if the system keeps
505 delta = hmp->count_newrecords -
506 hmp->last_newrecords;
507 if (delta < 0 || delta > hammer_limit_recs / 2) {
508 hmp->last_newrecords = hmp->count_newrecords;
509 hammer_sync_hmp(hmp, MNT_NOWAIT);
513 * If we have gotten behind start slowing
516 delta = (hmp->rsv_recs - hammer_limit_recs) *
517 hz / hammer_limit_recs;
519 tsleep(&trans, 0, "hmrslo", delta);
524 * Calculate the blocksize at the current offset and figure
525 * out how much we can actually write.
527 blkmask = blksize - 1;
528 offset = (int)uio->uio_offset & blkmask;
529 base_offset = uio->uio_offset & ~(int64_t)blkmask;
530 n = blksize - offset;
531 if (n > uio->uio_resid)
533 if (uio->uio_offset + n > ip->ino_data.size) {
534 vnode_pager_setsize(ap->a_vp, uio->uio_offset + n);
536 kflags |= NOTE_EXTEND;
539 if (uio->uio_segflg == UIO_NOCOPY) {
541 * Issuing a write with the same data backing the
542 * buffer. Instantiate the buffer to collect the
543 * backing vm pages, then read-in any missing bits.
545 * This case is used by vop_stdputpages().
547 bp = getblk(ap->a_vp, base_offset,
548 blksize, GETBLK_BHEAVY, 0);
549 if ((bp->b_flags & B_CACHE) == 0) {
551 error = bread(ap->a_vp, base_offset,
554 } else if (offset == 0 && uio->uio_resid >= blksize) {
556 * Even though we are entirely overwriting the buffer
557 * we may still have to zero it out to avoid a
558 * mmap/write visibility issue.
560 bp = getblk(ap->a_vp, base_offset, blksize, GETBLK_BHEAVY, 0);
561 if ((bp->b_flags & B_CACHE) == 0)
563 } else if (base_offset >= ip->ino_data.size) {
565 * If the base offset of the buffer is beyond the
566 * file EOF, we don't have to issue a read.
568 bp = getblk(ap->a_vp, base_offset,
569 blksize, GETBLK_BHEAVY, 0);
573 * Partial overwrite, read in any missing bits then
574 * replace the portion being written.
576 error = bread(ap->a_vp, base_offset, blksize, &bp);
581 error = uiomove((char *)bp->b_data + offset,
586 * If we screwed up we have to undo any VM size changes we
592 vtruncbuf(ap->a_vp, ip->ino_data.size,
593 hammer_blocksize(ip->ino_data.size));
597 kflags |= NOTE_WRITE;
598 hammer_stats_file_write += n;
599 /* bp->b_flags |= B_CLUSTEROK; temporarily disabled */
600 if (ip->ino_data.size < uio->uio_offset) {
601 ip->ino_data.size = uio->uio_offset;
602 flags = HAMMER_INODE_DDIRTY;
603 vnode_pager_setsize(ap->a_vp, ip->ino_data.size);
607 ip->ino_data.mtime = trans.time;
608 flags |= HAMMER_INODE_MTIME | HAMMER_INODE_BUFS;
609 hammer_modify_inode(ip, flags);
612 * Once we dirty the buffer any cached zone-X offset
613 * becomes invalid. HAMMER NOTE: no-history mode cannot
614 * allow overwriting over the same data sector unless
615 * we provide UNDOs for the old data, which we don't.
617 bp->b_bio2.bio_offset = NOOFFSET;
620 * Final buffer disposition.
622 * Because meta-data updates are deferred, HAMMER is
623 * especially sensitive to excessive bdwrite()s because
624 * the I/O stream is not broken up by disk reads. So the
625 * buffer cache simply cannot keep up.
627 * WARNING! blksize is variable. cluster_write() is
628 * expected to not blow up if it encounters buffers that
629 * do not match the passed blksize.
631 * NOTE! Hammer shouldn't need to bawrite()/cluster_write().
632 * The ip->rsv_recs check should burst-flush the data.
633 * If we queue it immediately the buf could be left
634 * locked on the device queue for a very long time.
636 bp->b_flags |= B_AGE;
637 if (ap->a_ioflag & IO_SYNC) {
639 } else if (ap->a_ioflag & IO_DIRECT) {
643 if (offset + n == blksize) {
644 if (hammer_cluster_enable == 0 ||
645 (ap->a_vp->v_mount->mnt_flag & MNT_NOCLUSTERW)) {
648 cluster_write(bp, ip->ino_data.size,
656 hammer_done_transaction(&trans);
657 hammer_knote(ap->a_vp, kflags);
662 * hammer_vop_access { vp, mode, cred }
666 hammer_vop_access(struct vop_access_args *ap)
668 struct hammer_inode *ip = VTOI(ap->a_vp);
673 ++hammer_stats_file_iopsr;
674 uid = hammer_to_unix_xid(&ip->ino_data.uid);
675 gid = hammer_to_unix_xid(&ip->ino_data.gid);
677 error = vop_helper_access(ap, uid, gid, ip->ino_data.mode,
678 ip->ino_data.uflags);
683 * hammer_vop_advlock { vp, id, op, fl, flags }
687 hammer_vop_advlock(struct vop_advlock_args *ap)
689 hammer_inode_t ip = VTOI(ap->a_vp);
691 return (lf_advlock(ap, &ip->advlock, ip->ino_data.size));
695 * hammer_vop_close { vp, fflag }
699 hammer_vop_close(struct vop_close_args *ap)
701 /*hammer_inode_t ip = VTOI(ap->a_vp);*/
702 return (vop_stdclose(ap));
706 * hammer_vop_ncreate { nch, dvp, vpp, cred, vap }
708 * The operating system has already ensured that the directory entry
709 * does not exist and done all appropriate namespace locking.
713 hammer_vop_ncreate(struct vop_ncreate_args *ap)
715 struct hammer_transaction trans;
716 struct hammer_inode *dip;
717 struct hammer_inode *nip;
718 struct nchandle *nch;
722 dip = VTOI(ap->a_dvp);
724 if (dip->flags & HAMMER_INODE_RO)
726 if ((error = hammer_checkspace(dip->hmp, HAMMER_CHKSPC_CREATE)) != 0)
730 * Create a transaction to cover the operations we perform.
732 hammer_start_transaction(&trans, dip->hmp);
733 ++hammer_stats_file_iopsw;
736 * Create a new filesystem object of the requested type. The
737 * returned inode will be referenced and shared-locked to prevent
738 * it from being moved to the flusher.
740 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
741 dip, nch->ncp->nc_name, nch->ncp->nc_nlen,
744 hkprintf("hammer_create_inode error %d\n", error);
745 hammer_done_transaction(&trans);
751 * Add the new filesystem object to the directory. This will also
752 * bump the inode's link count.
754 error = hammer_ip_add_directory(&trans, dip,
755 nch->ncp->nc_name, nch->ncp->nc_nlen,
758 hkprintf("hammer_ip_add_directory error %d\n", error);
764 hammer_rel_inode(nip, 0);
765 hammer_done_transaction(&trans);
768 error = hammer_get_vnode(nip, ap->a_vpp);
769 hammer_done_transaction(&trans);
770 hammer_rel_inode(nip, 0);
772 cache_setunresolved(ap->a_nch);
773 cache_setvp(ap->a_nch, *ap->a_vpp);
775 hammer_knote(ap->a_dvp, NOTE_WRITE);
781 * hammer_vop_getattr { vp, vap }
783 * Retrieve an inode's attribute information. When accessing inodes
784 * historically we fake the atime field to ensure consistent results.
785 * The atime field is stored in the B-Tree element and allowed to be
786 * updated without cycling the element.
792 hammer_vop_getattr(struct vop_getattr_args *ap)
794 struct hammer_inode *ip = VTOI(ap->a_vp);
795 struct vattr *vap = ap->a_vap;
798 * We want the fsid to be different when accessing a filesystem
799 * with different as-of's so programs like diff don't think
800 * the files are the same.
802 * We also want the fsid to be the same when comparing snapshots,
803 * or when comparing mirrors (which might be backed by different
804 * physical devices). HAMMER fsids are based on the PFS's
807 * XXX there is a chance of collision here. The va_fsid reported
808 * by stat is different from the more involved fsid used in the
811 ++hammer_stats_file_iopsr;
812 hammer_lock_sh(&ip->lock);
813 vap->va_fsid = ip->pfsm->fsid_udev ^ (u_int32_t)ip->obj_asof ^
814 (u_int32_t)(ip->obj_asof >> 32);
816 vap->va_fileid = ip->ino_leaf.base.obj_id;
817 vap->va_mode = ip->ino_data.mode;
818 vap->va_nlink = ip->ino_data.nlinks;
819 vap->va_uid = hammer_to_unix_xid(&ip->ino_data.uid);
820 vap->va_gid = hammer_to_unix_xid(&ip->ino_data.gid);
823 vap->va_size = ip->ino_data.size;
826 * Special case for @@PFS softlinks. The actual size of the
827 * expanded softlink is "@@0x%016llx:%05d" == 26 bytes.
828 * or for MAX_TID is "@@-1:%05d" == 10 bytes.
830 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_SOFTLINK &&
831 ip->ino_data.size == 10 &&
832 ip->obj_asof == HAMMER_MAX_TID &&
833 ip->obj_localization == 0 &&
834 strncmp(ip->ino_data.ext.symlink, "@@PFS", 5) == 0) {
835 if (ip->pfsm->pfsd.mirror_flags & HAMMER_PFSD_SLAVE)
842 * We must provide a consistent atime and mtime for snapshots
843 * so people can do a 'tar cf - ... | md5' on them and get
844 * consistent results.
846 if (ip->flags & HAMMER_INODE_RO) {
847 hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_atime);
848 hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_mtime);
850 hammer_time_to_timespec(ip->ino_data.atime, &vap->va_atime);
851 hammer_time_to_timespec(ip->ino_data.mtime, &vap->va_mtime);
853 hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_ctime);
854 vap->va_flags = ip->ino_data.uflags;
855 vap->va_gen = 1; /* hammer inums are unique for all time */
856 vap->va_blocksize = HAMMER_BUFSIZE;
857 if (ip->ino_data.size >= HAMMER_XDEMARC) {
858 vap->va_bytes = (ip->ino_data.size + HAMMER_XBUFMASK64) &
860 } else if (ip->ino_data.size > HAMMER_BUFSIZE / 2) {
861 vap->va_bytes = (ip->ino_data.size + HAMMER_BUFMASK64) &
864 vap->va_bytes = (ip->ino_data.size + 15) & ~15;
867 vap->va_type = hammer_get_vnode_type(ip->ino_data.obj_type);
868 vap->va_filerev = 0; /* XXX */
869 /* mtime uniquely identifies any adjustments made to the file XXX */
870 vap->va_fsmid = ip->ino_data.mtime;
871 vap->va_uid_uuid = ip->ino_data.uid;
872 vap->va_gid_uuid = ip->ino_data.gid;
873 vap->va_fsid_uuid = ip->hmp->fsid;
874 vap->va_vaflags = VA_UID_UUID_VALID | VA_GID_UUID_VALID |
877 switch (ip->ino_data.obj_type) {
878 case HAMMER_OBJTYPE_CDEV:
879 case HAMMER_OBJTYPE_BDEV:
880 vap->va_rmajor = ip->ino_data.rmajor;
881 vap->va_rminor = ip->ino_data.rminor;
886 hammer_unlock(&ip->lock);
891 * hammer_vop_nresolve { nch, dvp, cred }
893 * Locate the requested directory entry.
897 hammer_vop_nresolve(struct vop_nresolve_args *ap)
899 struct hammer_transaction trans;
900 struct namecache *ncp;
904 struct hammer_cursor cursor;
913 u_int32_t localization;
914 u_int32_t max_iterations;
917 * Misc initialization, plus handle as-of name extensions. Look for
918 * the '@@' extension. Note that as-of files and directories cannot
921 dip = VTOI(ap->a_dvp);
922 ncp = ap->a_nch->ncp;
923 asof = dip->obj_asof;
924 localization = dip->obj_localization; /* for code consistency */
926 flags = dip->flags & HAMMER_INODE_RO;
929 hammer_simple_transaction(&trans, dip->hmp);
930 ++hammer_stats_file_iopsr;
932 for (i = 0; i < nlen; ++i) {
933 if (ncp->nc_name[i] == '@' && ncp->nc_name[i+1] == '@') {
934 error = hammer_str_to_tid(ncp->nc_name + i + 2,
935 &ispfs, &asof, &localization);
940 if (asof != HAMMER_MAX_TID)
941 flags |= HAMMER_INODE_RO;
948 * If this is a PFS softlink we dive into the PFS
950 if (ispfs && nlen == 0) {
951 ip = hammer_get_inode(&trans, dip, HAMMER_OBJID_ROOT,
955 error = hammer_get_vnode(ip, &vp);
956 hammer_rel_inode(ip, 0);
962 cache_setvp(ap->a_nch, vp);
969 * If there is no path component the time extension is relative to dip.
970 * e.g. "fubar/@@<snapshot>"
972 * "." is handled by the kernel, but ".@@<snapshot>" is not.
973 * e.g. "fubar/.@@<snapshot>"
975 * ".." is handled by the kernel. We do not currently handle
978 if (nlen == 0 || (nlen == 1 && ncp->nc_name[0] == '.')) {
979 ip = hammer_get_inode(&trans, dip, dip->obj_id,
980 asof, dip->obj_localization,
983 error = hammer_get_vnode(ip, &vp);
984 hammer_rel_inode(ip, 0);
990 cache_setvp(ap->a_nch, vp);
997 * Calculate the namekey and setup the key range for the scan. This
998 * works kinda like a chained hash table where the lower 32 bits
999 * of the namekey synthesize the chain.
1001 * The key range is inclusive of both key_beg and key_end.
1003 namekey = hammer_directory_namekey(dip, ncp->nc_name, nlen,
1006 error = hammer_init_cursor(&trans, &cursor, &dip->cache[1], dip);
1007 cursor.key_beg.localization = dip->obj_localization +
1008 hammer_dir_localization(dip);
1009 cursor.key_beg.obj_id = dip->obj_id;
1010 cursor.key_beg.key = namekey;
1011 cursor.key_beg.create_tid = 0;
1012 cursor.key_beg.delete_tid = 0;
1013 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1014 cursor.key_beg.obj_type = 0;
1016 cursor.key_end = cursor.key_beg;
1017 cursor.key_end.key += max_iterations;
1019 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
1022 * Scan all matching records (the chain), locate the one matching
1023 * the requested path component.
1025 * The hammer_ip_*() functions merge in-memory records with on-disk
1026 * records for the purposes of the search.
1029 localization = HAMMER_DEF_LOCALIZATION;
1032 error = hammer_ip_first(&cursor);
1033 while (error == 0) {
1034 error = hammer_ip_resolve_data(&cursor);
1037 if (nlen == cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF &&
1038 bcmp(ncp->nc_name, cursor.data->entry.name, nlen) == 0) {
1039 obj_id = cursor.data->entry.obj_id;
1040 localization = cursor.data->entry.localization;
1043 error = hammer_ip_next(&cursor);
1046 hammer_done_cursor(&cursor);
1049 * Lookup the obj_id. This should always succeed. If it does not
1050 * the filesystem may be damaged and we return a dummy inode.
1053 ip = hammer_get_inode(&trans, dip, obj_id,
1056 if (error == ENOENT) {
1057 kprintf("HAMMER: WARNING: Missing "
1058 "inode for dirent \"%s\"\n"
1059 "\tobj_id = %016llx, asof=%016llx, lo=%08x\n",
1061 (long long)obj_id, (long long)asof,
1064 ip = hammer_get_dummy_inode(&trans, dip, obj_id,
1069 error = hammer_get_vnode(ip, &vp);
1070 hammer_rel_inode(ip, 0);
1076 cache_setvp(ap->a_nch, vp);
1079 } else if (error == ENOENT) {
1080 cache_setvp(ap->a_nch, NULL);
1083 hammer_done_transaction(&trans);
1088 * hammer_vop_nlookupdotdot { dvp, vpp, cred }
1090 * Locate the parent directory of a directory vnode.
1092 * dvp is referenced but not locked. *vpp must be returned referenced and
1093 * locked. A parent_obj_id of 0 does not necessarily indicate that we are
1094 * at the root, instead it could indicate that the directory we were in was
1097 * NOTE: as-of sequences are not linked into the directory structure. If
1098 * we are at the root with a different asof then the mount point, reload
1099 * the same directory with the mount point's asof. I'm not sure what this
1100 * will do to NFS. We encode ASOF stamps in NFS file handles so it might not
1101 * get confused, but it hasn't been tested.
1105 hammer_vop_nlookupdotdot(struct vop_nlookupdotdot_args *ap)
1107 struct hammer_transaction trans;
1108 struct hammer_inode *dip;
1109 struct hammer_inode *ip;
1110 int64_t parent_obj_id;
1111 u_int32_t parent_obj_localization;
1115 dip = VTOI(ap->a_dvp);
1116 asof = dip->obj_asof;
1119 * Whos are parent? This could be the root of a pseudo-filesystem
1120 * whos parent is in another localization domain.
1122 parent_obj_id = dip->ino_data.parent_obj_id;
1123 if (dip->obj_id == HAMMER_OBJID_ROOT)
1124 parent_obj_localization = dip->ino_data.ext.obj.parent_obj_localization;
1126 parent_obj_localization = dip->obj_localization;
1128 if (parent_obj_id == 0) {
1129 if (dip->obj_id == HAMMER_OBJID_ROOT &&
1130 asof != dip->hmp->asof) {
1131 parent_obj_id = dip->obj_id;
1132 asof = dip->hmp->asof;
1133 *ap->a_fakename = kmalloc(19, M_TEMP, M_WAITOK);
1134 ksnprintf(*ap->a_fakename, 19, "0x%016llx",
1135 (long long)dip->obj_asof);
1142 hammer_simple_transaction(&trans, dip->hmp);
1143 ++hammer_stats_file_iopsr;
1145 ip = hammer_get_inode(&trans, dip, parent_obj_id,
1146 asof, parent_obj_localization,
1147 dip->flags, &error);
1149 error = hammer_get_vnode(ip, ap->a_vpp);
1150 hammer_rel_inode(ip, 0);
1154 hammer_done_transaction(&trans);
1159 * hammer_vop_nlink { nch, dvp, vp, cred }
1163 hammer_vop_nlink(struct vop_nlink_args *ap)
1165 struct hammer_transaction trans;
1166 struct hammer_inode *dip;
1167 struct hammer_inode *ip;
1168 struct nchandle *nch;
1171 if (ap->a_dvp->v_mount != ap->a_vp->v_mount)
1175 dip = VTOI(ap->a_dvp);
1176 ip = VTOI(ap->a_vp);
1178 if (dip->obj_localization != ip->obj_localization)
1181 if (dip->flags & HAMMER_INODE_RO)
1183 if (ip->flags & HAMMER_INODE_RO)
1185 if ((error = hammer_checkspace(dip->hmp, HAMMER_CHKSPC_CREATE)) != 0)
1189 * Create a transaction to cover the operations we perform.
1191 hammer_start_transaction(&trans, dip->hmp);
1192 ++hammer_stats_file_iopsw;
1195 * Add the filesystem object to the directory. Note that neither
1196 * dip nor ip are referenced or locked, but their vnodes are
1197 * referenced. This function will bump the inode's link count.
1199 error = hammer_ip_add_directory(&trans, dip,
1200 nch->ncp->nc_name, nch->ncp->nc_nlen,
1207 cache_setunresolved(nch);
1208 cache_setvp(nch, ap->a_vp);
1210 hammer_done_transaction(&trans);
1211 hammer_knote(ap->a_vp, NOTE_LINK);
1212 hammer_knote(ap->a_dvp, NOTE_WRITE);
1217 * hammer_vop_nmkdir { nch, dvp, vpp, cred, vap }
1219 * The operating system has already ensured that the directory entry
1220 * does not exist and done all appropriate namespace locking.
1224 hammer_vop_nmkdir(struct vop_nmkdir_args *ap)
1226 struct hammer_transaction trans;
1227 struct hammer_inode *dip;
1228 struct hammer_inode *nip;
1229 struct nchandle *nch;
1233 dip = VTOI(ap->a_dvp);
1235 if (dip->flags & HAMMER_INODE_RO)
1237 if ((error = hammer_checkspace(dip->hmp, HAMMER_CHKSPC_CREATE)) != 0)
1241 * Create a transaction to cover the operations we perform.
1243 hammer_start_transaction(&trans, dip->hmp);
1244 ++hammer_stats_file_iopsw;
1247 * Create a new filesystem object of the requested type. The
1248 * returned inode will be referenced but not locked.
1250 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
1251 dip, nch->ncp->nc_name, nch->ncp->nc_nlen,
1254 hkprintf("hammer_mkdir error %d\n", error);
1255 hammer_done_transaction(&trans);
1260 * Add the new filesystem object to the directory. This will also
1261 * bump the inode's link count.
1263 error = hammer_ip_add_directory(&trans, dip,
1264 nch->ncp->nc_name, nch->ncp->nc_nlen,
1267 hkprintf("hammer_mkdir (add) error %d\n", error);
1273 hammer_rel_inode(nip, 0);
1276 error = hammer_get_vnode(nip, ap->a_vpp);
1277 hammer_rel_inode(nip, 0);
1279 cache_setunresolved(ap->a_nch);
1280 cache_setvp(ap->a_nch, *ap->a_vpp);
1283 hammer_done_transaction(&trans);
1285 hammer_knote(ap->a_dvp, NOTE_WRITE | NOTE_LINK);
1290 * hammer_vop_nmknod { nch, dvp, vpp, cred, vap }
1292 * The operating system has already ensured that the directory entry
1293 * does not exist and done all appropriate namespace locking.
1297 hammer_vop_nmknod(struct vop_nmknod_args *ap)
1299 struct hammer_transaction trans;
1300 struct hammer_inode *dip;
1301 struct hammer_inode *nip;
1302 struct nchandle *nch;
1306 dip = VTOI(ap->a_dvp);
1308 if (dip->flags & HAMMER_INODE_RO)
1310 if ((error = hammer_checkspace(dip->hmp, HAMMER_CHKSPC_CREATE)) != 0)
1314 * Create a transaction to cover the operations we perform.
1316 hammer_start_transaction(&trans, dip->hmp);
1317 ++hammer_stats_file_iopsw;
1320 * Create a new filesystem object of the requested type. The
1321 * returned inode will be referenced but not locked.
1323 * If mknod specifies a directory a pseudo-fs is created.
1325 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
1326 dip, nch->ncp->nc_name, nch->ncp->nc_nlen,
1329 hammer_done_transaction(&trans);
1335 * Add the new filesystem object to the directory. This will also
1336 * bump the inode's link count.
1338 error = hammer_ip_add_directory(&trans, dip,
1339 nch->ncp->nc_name, nch->ncp->nc_nlen,
1346 hammer_rel_inode(nip, 0);
1349 error = hammer_get_vnode(nip, ap->a_vpp);
1350 hammer_rel_inode(nip, 0);
1352 cache_setunresolved(ap->a_nch);
1353 cache_setvp(ap->a_nch, *ap->a_vpp);
1356 hammer_done_transaction(&trans);
1358 hammer_knote(ap->a_dvp, NOTE_WRITE);
1363 * hammer_vop_open { vp, mode, cred, fp }
1367 hammer_vop_open(struct vop_open_args *ap)
1371 ++hammer_stats_file_iopsr;
1372 ip = VTOI(ap->a_vp);
1374 if ((ap->a_mode & FWRITE) && (ip->flags & HAMMER_INODE_RO))
1376 return(vop_stdopen(ap));
1380 * hammer_vop_print { vp }
1384 hammer_vop_print(struct vop_print_args *ap)
1390 * hammer_vop_readdir { vp, uio, cred, *eofflag, *ncookies, off_t **cookies }
1394 hammer_vop_readdir(struct vop_readdir_args *ap)
1396 struct hammer_transaction trans;
1397 struct hammer_cursor cursor;
1398 struct hammer_inode *ip;
1400 hammer_base_elm_t base;
1409 ++hammer_stats_file_iopsr;
1410 ip = VTOI(ap->a_vp);
1412 saveoff = uio->uio_offset;
1414 if (ap->a_ncookies) {
1415 ncookies = uio->uio_resid / 16 + 1;
1416 if (ncookies > 1024)
1418 cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
1426 hammer_simple_transaction(&trans, ip->hmp);
1429 * Handle artificial entries
1431 * It should be noted that the minimum value for a directory
1432 * hash key on-media is 0x0000000100000000, so we can use anything
1433 * less then that to represent our 'special' key space.
1437 r = vop_write_dirent(&error, uio, ip->obj_id, DT_DIR, 1, ".");
1441 cookies[cookie_index] = saveoff;
1444 if (cookie_index == ncookies)
1448 if (ip->ino_data.parent_obj_id) {
1449 r = vop_write_dirent(&error, uio,
1450 ip->ino_data.parent_obj_id,
1453 r = vop_write_dirent(&error, uio,
1454 ip->obj_id, DT_DIR, 2, "..");
1459 cookies[cookie_index] = saveoff;
1462 if (cookie_index == ncookies)
1467 * Key range (begin and end inclusive) to scan. Directory keys
1468 * directly translate to a 64 bit 'seek' position.
1470 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
1471 cursor.key_beg.localization = ip->obj_localization +
1472 hammer_dir_localization(ip);
1473 cursor.key_beg.obj_id = ip->obj_id;
1474 cursor.key_beg.create_tid = 0;
1475 cursor.key_beg.delete_tid = 0;
1476 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1477 cursor.key_beg.obj_type = 0;
1478 cursor.key_beg.key = saveoff;
1480 cursor.key_end = cursor.key_beg;
1481 cursor.key_end.key = HAMMER_MAX_KEY;
1482 cursor.asof = ip->obj_asof;
1483 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
1485 error = hammer_ip_first(&cursor);
1487 while (error == 0) {
1488 error = hammer_ip_resolve_data(&cursor);
1491 base = &cursor.leaf->base;
1492 saveoff = base->key;
1493 KKASSERT(cursor.leaf->data_len > HAMMER_ENTRY_NAME_OFF);
1495 if (base->obj_id != ip->obj_id)
1496 panic("readdir: bad record at %p", cursor.node);
1499 * Convert pseudo-filesystems into softlinks
1501 dtype = hammer_get_dtype(cursor.leaf->base.obj_type);
1502 r = vop_write_dirent(
1503 &error, uio, cursor.data->entry.obj_id,
1505 cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF ,
1506 (void *)cursor.data->entry.name);
1511 cookies[cookie_index] = base->key;
1513 if (cookie_index == ncookies)
1515 error = hammer_ip_next(&cursor);
1517 hammer_done_cursor(&cursor);
1520 hammer_done_transaction(&trans);
1523 *ap->a_eofflag = (error == ENOENT);
1524 uio->uio_offset = saveoff;
1525 if (error && cookie_index == 0) {
1526 if (error == ENOENT)
1529 kfree(cookies, M_TEMP);
1530 *ap->a_ncookies = 0;
1531 *ap->a_cookies = NULL;
1534 if (error == ENOENT)
1537 *ap->a_ncookies = cookie_index;
1538 *ap->a_cookies = cookies;
1545 * hammer_vop_readlink { vp, uio, cred }
1549 hammer_vop_readlink(struct vop_readlink_args *ap)
1551 struct hammer_transaction trans;
1552 struct hammer_cursor cursor;
1553 struct hammer_inode *ip;
1555 u_int32_t localization;
1556 hammer_pseudofs_inmem_t pfsm;
1559 ip = VTOI(ap->a_vp);
1562 * Shortcut if the symlink data was stuffed into ino_data.
1564 * Also expand special "@@PFS%05d" softlinks (expansion only
1565 * occurs for non-historical (current) accesses made from the
1566 * primary filesystem).
1568 if (ip->ino_data.size <= HAMMER_INODE_BASESYMLEN) {
1572 ptr = ip->ino_data.ext.symlink;
1573 bytes = (int)ip->ino_data.size;
1575 ip->obj_asof == HAMMER_MAX_TID &&
1576 ip->obj_localization == 0 &&
1577 strncmp(ptr, "@@PFS", 5) == 0) {
1578 hammer_simple_transaction(&trans, ip->hmp);
1579 bcopy(ptr + 5, buf, 5);
1581 localization = strtoul(buf, NULL, 10) << 16;
1582 pfsm = hammer_load_pseudofs(&trans, localization,
1585 if (pfsm->pfsd.mirror_flags &
1586 HAMMER_PFSD_SLAVE) {
1587 /* vap->va_size == 26 */
1588 ksnprintf(buf, sizeof(buf),
1590 (long long)pfsm->pfsd.sync_end_tid,
1591 localization >> 16);
1593 /* vap->va_size == 10 */
1594 ksnprintf(buf, sizeof(buf),
1596 localization >> 16);
1598 ksnprintf(buf, sizeof(buf),
1600 (long long)HAMMER_MAX_TID,
1601 localization >> 16);
1605 bytes = strlen(buf);
1608 hammer_rel_pseudofs(trans.hmp, pfsm);
1609 hammer_done_transaction(&trans);
1611 error = uiomove(ptr, bytes, ap->a_uio);
1618 hammer_simple_transaction(&trans, ip->hmp);
1619 ++hammer_stats_file_iopsr;
1620 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
1623 * Key range (begin and end inclusive) to scan. Directory keys
1624 * directly translate to a 64 bit 'seek' position.
1626 cursor.key_beg.localization = ip->obj_localization +
1627 HAMMER_LOCALIZE_MISC;
1628 cursor.key_beg.obj_id = ip->obj_id;
1629 cursor.key_beg.create_tid = 0;
1630 cursor.key_beg.delete_tid = 0;
1631 cursor.key_beg.rec_type = HAMMER_RECTYPE_FIX;
1632 cursor.key_beg.obj_type = 0;
1633 cursor.key_beg.key = HAMMER_FIXKEY_SYMLINK;
1634 cursor.asof = ip->obj_asof;
1635 cursor.flags |= HAMMER_CURSOR_ASOF;
1637 error = hammer_ip_lookup(&cursor);
1639 error = hammer_ip_resolve_data(&cursor);
1641 KKASSERT(cursor.leaf->data_len >=
1642 HAMMER_SYMLINK_NAME_OFF);
1643 error = uiomove(cursor.data->symlink.name,
1644 cursor.leaf->data_len -
1645 HAMMER_SYMLINK_NAME_OFF,
1649 hammer_done_cursor(&cursor);
1650 hammer_done_transaction(&trans);
1655 * hammer_vop_nremove { nch, dvp, cred }
1659 hammer_vop_nremove(struct vop_nremove_args *ap)
1661 struct hammer_transaction trans;
1662 struct hammer_inode *dip;
1665 dip = VTOI(ap->a_dvp);
1667 if (hammer_nohistory(dip) == 0 &&
1668 (error = hammer_checkspace(dip->hmp, HAMMER_CHKSPC_REMOVE)) != 0) {
1672 hammer_start_transaction(&trans, dip->hmp);
1673 ++hammer_stats_file_iopsw;
1674 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp, ap->a_cred, 0, 0);
1675 hammer_done_transaction(&trans);
1677 hammer_knote(ap->a_dvp, NOTE_WRITE);
1682 * hammer_vop_nrename { fnch, tnch, fdvp, tdvp, cred }
1686 hammer_vop_nrename(struct vop_nrename_args *ap)
1688 struct hammer_transaction trans;
1689 struct namecache *fncp;
1690 struct namecache *tncp;
1691 struct hammer_inode *fdip;
1692 struct hammer_inode *tdip;
1693 struct hammer_inode *ip;
1694 struct hammer_cursor cursor;
1696 u_int32_t max_iterations;
1699 if (ap->a_fdvp->v_mount != ap->a_tdvp->v_mount)
1701 if (ap->a_fdvp->v_mount != ap->a_fnch->ncp->nc_vp->v_mount)
1704 fdip = VTOI(ap->a_fdvp);
1705 tdip = VTOI(ap->a_tdvp);
1706 fncp = ap->a_fnch->ncp;
1707 tncp = ap->a_tnch->ncp;
1708 ip = VTOI(fncp->nc_vp);
1709 KKASSERT(ip != NULL);
1711 if (fdip->obj_localization != tdip->obj_localization)
1713 if (fdip->obj_localization != ip->obj_localization)
1716 if (fdip->flags & HAMMER_INODE_RO)
1718 if (tdip->flags & HAMMER_INODE_RO)
1720 if (ip->flags & HAMMER_INODE_RO)
1722 if ((error = hammer_checkspace(fdip->hmp, HAMMER_CHKSPC_CREATE)) != 0)
1725 hammer_start_transaction(&trans, fdip->hmp);
1726 ++hammer_stats_file_iopsw;
1729 * Remove tncp from the target directory and then link ip as
1730 * tncp. XXX pass trans to dounlink
1732 * Force the inode sync-time to match the transaction so it is
1733 * in-sync with the creation of the target directory entry.
1735 error = hammer_dounlink(&trans, ap->a_tnch, ap->a_tdvp,
1737 if (error == 0 || error == ENOENT) {
1738 error = hammer_ip_add_directory(&trans, tdip,
1739 tncp->nc_name, tncp->nc_nlen,
1742 ip->ino_data.parent_obj_id = tdip->obj_id;
1743 ip->ino_data.ctime = trans.time;
1744 hammer_modify_inode(ip, HAMMER_INODE_DDIRTY);
1748 goto failed; /* XXX */
1751 * Locate the record in the originating directory and remove it.
1753 * Calculate the namekey and setup the key range for the scan. This
1754 * works kinda like a chained hash table where the lower 32 bits
1755 * of the namekey synthesize the chain.
1757 * The key range is inclusive of both key_beg and key_end.
1759 namekey = hammer_directory_namekey(fdip, fncp->nc_name, fncp->nc_nlen,
1762 hammer_init_cursor(&trans, &cursor, &fdip->cache[1], fdip);
1763 cursor.key_beg.localization = fdip->obj_localization +
1764 hammer_dir_localization(fdip);
1765 cursor.key_beg.obj_id = fdip->obj_id;
1766 cursor.key_beg.key = namekey;
1767 cursor.key_beg.create_tid = 0;
1768 cursor.key_beg.delete_tid = 0;
1769 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1770 cursor.key_beg.obj_type = 0;
1772 cursor.key_end = cursor.key_beg;
1773 cursor.key_end.key += max_iterations;
1774 cursor.asof = fdip->obj_asof;
1775 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
1778 * Scan all matching records (the chain), locate the one matching
1779 * the requested path component.
1781 * The hammer_ip_*() functions merge in-memory records with on-disk
1782 * records for the purposes of the search.
1784 error = hammer_ip_first(&cursor);
1785 while (error == 0) {
1786 if (hammer_ip_resolve_data(&cursor) != 0)
1788 nlen = cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF;
1790 if (fncp->nc_nlen == nlen &&
1791 bcmp(fncp->nc_name, cursor.data->entry.name, nlen) == 0) {
1794 error = hammer_ip_next(&cursor);
1798 * If all is ok we have to get the inode so we can adjust nlinks.
1800 * WARNING: hammer_ip_del_directory() may have to terminate the
1801 * cursor to avoid a recursion. It's ok to call hammer_done_cursor()
1805 error = hammer_ip_del_directory(&trans, &cursor, fdip, ip);
1808 * XXX A deadlock here will break rename's atomicy for the purposes
1809 * of crash recovery.
1811 if (error == EDEADLK) {
1812 hammer_done_cursor(&cursor);
1817 * Cleanup and tell the kernel that the rename succeeded.
1819 hammer_done_cursor(&cursor);
1821 cache_rename(ap->a_fnch, ap->a_tnch);
1822 hammer_knote(ap->a_fdvp, NOTE_WRITE);
1823 hammer_knote(ap->a_tdvp, NOTE_WRITE);
1825 hammer_knote(ip->vp, NOTE_RENAME);
1829 hammer_done_transaction(&trans);
1834 * hammer_vop_nrmdir { nch, dvp, cred }
1838 hammer_vop_nrmdir(struct vop_nrmdir_args *ap)
1840 struct hammer_transaction trans;
1841 struct hammer_inode *dip;
1844 dip = VTOI(ap->a_dvp);
1846 if (hammer_nohistory(dip) == 0 &&
1847 (error = hammer_checkspace(dip->hmp, HAMMER_CHKSPC_REMOVE)) != 0) {
1851 hammer_start_transaction(&trans, dip->hmp);
1852 ++hammer_stats_file_iopsw;
1853 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp, ap->a_cred, 0, 1);
1854 hammer_done_transaction(&trans);
1856 hammer_knote(ap->a_dvp, NOTE_WRITE | NOTE_LINK);
1861 * hammer_vop_markatime { vp, cred }
1865 hammer_vop_markatime(struct vop_markatime_args *ap)
1867 struct hammer_transaction trans;
1868 struct hammer_inode *ip;
1870 ip = VTOI(ap->a_vp);
1871 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
1873 if (ip->flags & HAMMER_INODE_RO)
1875 if (ip->hmp->mp->mnt_flag & MNT_NOATIME)
1877 hammer_start_transaction(&trans, ip->hmp);
1878 ++hammer_stats_file_iopsw;
1880 ip->ino_data.atime = trans.time;
1881 hammer_modify_inode(ip, HAMMER_INODE_ATIME);
1882 hammer_done_transaction(&trans);
1883 hammer_knote(ap->a_vp, NOTE_ATTRIB);
1888 * hammer_vop_setattr { vp, vap, cred }
1892 hammer_vop_setattr(struct vop_setattr_args *ap)
1894 struct hammer_transaction trans;
1896 struct hammer_inode *ip;
1902 int64_t aligned_size;
1906 ip = ap->a_vp->v_data;
1910 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
1912 if (ip->flags & HAMMER_INODE_RO)
1914 if (hammer_nohistory(ip) == 0 &&
1915 (error = hammer_checkspace(ip->hmp, HAMMER_CHKSPC_REMOVE)) != 0) {
1919 hammer_start_transaction(&trans, ip->hmp);
1920 ++hammer_stats_file_iopsw;
1923 if (vap->va_flags != VNOVAL) {
1924 flags = ip->ino_data.uflags;
1925 error = vop_helper_setattr_flags(&flags, vap->va_flags,
1926 hammer_to_unix_xid(&ip->ino_data.uid),
1929 if (ip->ino_data.uflags != flags) {
1930 ip->ino_data.uflags = flags;
1931 ip->ino_data.ctime = trans.time;
1932 modflags |= HAMMER_INODE_DDIRTY;
1933 kflags |= NOTE_ATTRIB;
1935 if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) {
1942 if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) {
1946 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
1947 mode_t cur_mode = ip->ino_data.mode;
1948 uid_t cur_uid = hammer_to_unix_xid(&ip->ino_data.uid);
1949 gid_t cur_gid = hammer_to_unix_xid(&ip->ino_data.gid);
1953 error = vop_helper_chown(ap->a_vp, vap->va_uid, vap->va_gid,
1955 &cur_uid, &cur_gid, &cur_mode);
1957 hammer_guid_to_uuid(&uuid_uid, cur_uid);
1958 hammer_guid_to_uuid(&uuid_gid, cur_gid);
1959 if (bcmp(&uuid_uid, &ip->ino_data.uid,
1960 sizeof(uuid_uid)) ||
1961 bcmp(&uuid_gid, &ip->ino_data.gid,
1962 sizeof(uuid_gid)) ||
1963 ip->ino_data.mode != cur_mode
1965 ip->ino_data.uid = uuid_uid;
1966 ip->ino_data.gid = uuid_gid;
1967 ip->ino_data.mode = cur_mode;
1968 ip->ino_data.ctime = trans.time;
1969 modflags |= HAMMER_INODE_DDIRTY;
1971 kflags |= NOTE_ATTRIB;
1974 while (vap->va_size != VNOVAL && ip->ino_data.size != vap->va_size) {
1975 switch(ap->a_vp->v_type) {
1977 if (vap->va_size == ip->ino_data.size)
1980 * XXX break atomicy, we can deadlock the backend
1981 * if we do not release the lock. Probably not a
1984 blksize = hammer_blocksize(vap->va_size);
1985 if (vap->va_size < ip->ino_data.size) {
1986 vtruncbuf(ap->a_vp, vap->va_size, blksize);
1988 kflags |= NOTE_WRITE;
1990 vnode_pager_setsize(ap->a_vp, vap->va_size);
1992 kflags |= NOTE_WRITE | NOTE_EXTEND;
1994 ip->ino_data.size = vap->va_size;
1995 ip->ino_data.mtime = trans.time;
1996 modflags |= HAMMER_INODE_MTIME | HAMMER_INODE_DDIRTY;
1999 * on-media truncation is cached in the inode until
2000 * the inode is synchronized.
2003 hammer_ip_frontend_trunc(ip, vap->va_size);
2004 #ifdef DEBUG_TRUNCATE
2005 if (HammerTruncIp == NULL)
2008 if ((ip->flags & HAMMER_INODE_TRUNCATED) == 0) {
2009 ip->flags |= HAMMER_INODE_TRUNCATED;
2010 ip->trunc_off = vap->va_size;
2011 #ifdef DEBUG_TRUNCATE
2012 if (ip == HammerTruncIp)
2013 kprintf("truncate1 %016llx\n",
2014 (long long)ip->trunc_off);
2016 } else if (ip->trunc_off > vap->va_size) {
2017 ip->trunc_off = vap->va_size;
2018 #ifdef DEBUG_TRUNCATE
2019 if (ip == HammerTruncIp)
2020 kprintf("truncate2 %016llx\n",
2021 (long long)ip->trunc_off);
2024 #ifdef DEBUG_TRUNCATE
2025 if (ip == HammerTruncIp)
2026 kprintf("truncate3 %016llx (ignored)\n",
2027 (long long)vap->va_size);
2033 * If truncating we have to clean out a portion of
2034 * the last block on-disk. We do this in the
2035 * front-end buffer cache.
2037 aligned_size = (vap->va_size + (blksize - 1)) &
2038 ~(int64_t)(blksize - 1);
2039 if (truncating && vap->va_size < aligned_size) {
2043 aligned_size -= blksize;
2045 offset = (int)vap->va_size & (blksize - 1);
2046 error = bread(ap->a_vp, aligned_size,
2048 hammer_ip_frontend_trunc(ip, aligned_size);
2050 bzero(bp->b_data + offset,
2052 /* must de-cache direct-io offset */
2053 bp->b_bio2.bio_offset = NOOFFSET;
2056 kprintf("ERROR %d\n", error);
2062 if ((ip->flags & HAMMER_INODE_TRUNCATED) == 0) {
2063 ip->flags |= HAMMER_INODE_TRUNCATED;
2064 ip->trunc_off = vap->va_size;
2065 } else if (ip->trunc_off > vap->va_size) {
2066 ip->trunc_off = vap->va_size;
2068 hammer_ip_frontend_trunc(ip, vap->va_size);
2069 ip->ino_data.size = vap->va_size;
2070 ip->ino_data.mtime = trans.time;
2071 modflags |= HAMMER_INODE_MTIME | HAMMER_INODE_DDIRTY;
2072 kflags |= NOTE_ATTRIB;
2080 if (vap->va_atime.tv_sec != VNOVAL) {
2081 ip->ino_data.atime = hammer_timespec_to_time(&vap->va_atime);
2082 modflags |= HAMMER_INODE_ATIME;
2083 kflags |= NOTE_ATTRIB;
2085 if (vap->va_mtime.tv_sec != VNOVAL) {
2086 ip->ino_data.mtime = hammer_timespec_to_time(&vap->va_mtime);
2087 modflags |= HAMMER_INODE_MTIME;
2088 kflags |= NOTE_ATTRIB;
2090 if (vap->va_mode != (mode_t)VNOVAL) {
2091 mode_t cur_mode = ip->ino_data.mode;
2092 uid_t cur_uid = hammer_to_unix_xid(&ip->ino_data.uid);
2093 gid_t cur_gid = hammer_to_unix_xid(&ip->ino_data.gid);
2095 error = vop_helper_chmod(ap->a_vp, vap->va_mode, ap->a_cred,
2096 cur_uid, cur_gid, &cur_mode);
2097 if (error == 0 && ip->ino_data.mode != cur_mode) {
2098 ip->ino_data.mode = cur_mode;
2099 ip->ino_data.ctime = trans.time;
2100 modflags |= HAMMER_INODE_DDIRTY;
2101 kflags |= NOTE_ATTRIB;
2106 hammer_modify_inode(ip, modflags);
2107 hammer_done_transaction(&trans);
2108 hammer_knote(ap->a_vp, kflags);
2113 * hammer_vop_nsymlink { nch, dvp, vpp, cred, vap, target }
2117 hammer_vop_nsymlink(struct vop_nsymlink_args *ap)
2119 struct hammer_transaction trans;
2120 struct hammer_inode *dip;
2121 struct hammer_inode *nip;
2122 struct nchandle *nch;
2123 hammer_record_t record;
2127 ap->a_vap->va_type = VLNK;
2130 dip = VTOI(ap->a_dvp);
2132 if (dip->flags & HAMMER_INODE_RO)
2134 if ((error = hammer_checkspace(dip->hmp, HAMMER_CHKSPC_CREATE)) != 0)
2138 * Create a transaction to cover the operations we perform.
2140 hammer_start_transaction(&trans, dip->hmp);
2141 ++hammer_stats_file_iopsw;
2144 * Create a new filesystem object of the requested type. The
2145 * returned inode will be referenced but not locked.
2148 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
2149 dip, nch->ncp->nc_name, nch->ncp->nc_nlen,
2152 hammer_done_transaction(&trans);
2158 * Add a record representing the symlink. symlink stores the link
2159 * as pure data, not a string, and is no \0 terminated.
2162 bytes = strlen(ap->a_target);
2164 if (bytes <= HAMMER_INODE_BASESYMLEN) {
2165 bcopy(ap->a_target, nip->ino_data.ext.symlink, bytes);
2167 record = hammer_alloc_mem_record(nip, bytes);
2168 record->type = HAMMER_MEM_RECORD_GENERAL;
2170 record->leaf.base.localization = nip->obj_localization +
2171 HAMMER_LOCALIZE_MISC;
2172 record->leaf.base.key = HAMMER_FIXKEY_SYMLINK;
2173 record->leaf.base.rec_type = HAMMER_RECTYPE_FIX;
2174 record->leaf.data_len = bytes;
2175 KKASSERT(HAMMER_SYMLINK_NAME_OFF == 0);
2176 bcopy(ap->a_target, record->data->symlink.name, bytes);
2177 error = hammer_ip_add_record(&trans, record);
2181 * Set the file size to the length of the link.
2184 nip->ino_data.size = bytes;
2185 hammer_modify_inode(nip, HAMMER_INODE_DDIRTY);
2189 error = hammer_ip_add_directory(&trans, dip, nch->ncp->nc_name,
2190 nch->ncp->nc_nlen, nip);
2196 hammer_rel_inode(nip, 0);
2199 error = hammer_get_vnode(nip, ap->a_vpp);
2200 hammer_rel_inode(nip, 0);
2202 cache_setunresolved(ap->a_nch);
2203 cache_setvp(ap->a_nch, *ap->a_vpp);
2204 hammer_knote(ap->a_dvp, NOTE_WRITE);
2207 hammer_done_transaction(&trans);
2212 * hammer_vop_nwhiteout { nch, dvp, cred, flags }
2216 hammer_vop_nwhiteout(struct vop_nwhiteout_args *ap)
2218 struct hammer_transaction trans;
2219 struct hammer_inode *dip;
2222 dip = VTOI(ap->a_dvp);
2224 if (hammer_nohistory(dip) == 0 &&
2225 (error = hammer_checkspace(dip->hmp, HAMMER_CHKSPC_CREATE)) != 0) {
2229 hammer_start_transaction(&trans, dip->hmp);
2230 ++hammer_stats_file_iopsw;
2231 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp,
2232 ap->a_cred, ap->a_flags, -1);
2233 hammer_done_transaction(&trans);
2239 * hammer_vop_ioctl { vp, command, data, fflag, cred }
2243 hammer_vop_ioctl(struct vop_ioctl_args *ap)
2245 struct hammer_inode *ip = ap->a_vp->v_data;
2247 ++hammer_stats_file_iopsr;
2248 return(hammer_ioctl(ip, ap->a_command, ap->a_data,
2249 ap->a_fflag, ap->a_cred));
2254 hammer_vop_mountctl(struct vop_mountctl_args *ap)
2256 static const struct mountctl_opt extraopt[] = {
2257 { HMNT_NOHISTORY, "nohistory" },
2258 { HMNT_MASTERID, "master" },
2262 struct hammer_mount *hmp;
2269 mp = ap->a_head.a_ops->head.vv_mount;
2270 KKASSERT(mp->mnt_data != NULL);
2271 hmp = (struct hammer_mount *)mp->mnt_data;
2275 case MOUNTCTL_SET_EXPORT:
2276 if (ap->a_ctllen != sizeof(struct export_args))
2279 error = hammer_vfs_export(mp, ap->a_op,
2280 (const struct export_args *)ap->a_ctl);
2282 case MOUNTCTL_MOUNTFLAGS:
2285 * Call standard mountctl VOP function
2286 * so we get user mount flags.
2288 error = vop_stdmountctl(ap);
2292 usedbytes = *ap->a_res;
2294 if (usedbytes > 0 && usedbytes < ap->a_buflen) {
2295 usedbytes += vfs_flagstostr(hmp->hflags, extraopt, ap->a_buf,
2296 ap->a_buflen - usedbytes,
2300 *ap->a_res += usedbytes;
2304 error = vop_stdmountctl(ap);
2311 * hammer_vop_strategy { vp, bio }
2313 * Strategy call, used for regular file read & write only. Note that the
2314 * bp may represent a cluster.
2316 * To simplify operation and allow better optimizations in the future,
2317 * this code does not make any assumptions with regards to buffer alignment
2322 hammer_vop_strategy(struct vop_strategy_args *ap)
2327 bp = ap->a_bio->bio_buf;
2331 error = hammer_vop_strategy_read(ap);
2334 error = hammer_vop_strategy_write(ap);
2337 bp->b_error = error = EINVAL;
2338 bp->b_flags |= B_ERROR;
2346 * Read from a regular file. Iterate the related records and fill in the
2347 * BIO/BUF. Gaps are zero-filled.
2349 * The support code in hammer_object.c should be used to deal with mixed
2350 * in-memory and on-disk records.
2352 * NOTE: Can be called from the cluster code with an oversized buf.
2358 hammer_vop_strategy_read(struct vop_strategy_args *ap)
2360 struct hammer_transaction trans;
2361 struct hammer_inode *ip;
2362 struct hammer_inode *dip;
2363 struct hammer_cursor cursor;
2364 hammer_base_elm_t base;
2365 hammer_off_t disk_offset;
2379 ip = ap->a_vp->v_data;
2382 * The zone-2 disk offset may have been set by the cluster code via
2383 * a BMAP operation, or else should be NOOFFSET.
2385 * Checking the high bits for a match against zone-2 should suffice.
2387 nbio = push_bio(bio);
2388 if ((nbio->bio_offset & HAMMER_OFF_ZONE_MASK) ==
2389 HAMMER_ZONE_LARGE_DATA) {
2390 error = hammer_io_direct_read(ip->hmp, nbio, NULL);
2395 * Well, that sucked. Do it the hard way. If all the stars are
2396 * aligned we may still be able to issue a direct-read.
2398 hammer_simple_transaction(&trans, ip->hmp);
2399 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
2402 * Key range (begin and end inclusive) to scan. Note that the key's
2403 * stored in the actual records represent BASE+LEN, not BASE. The
2404 * first record containing bio_offset will have a key > bio_offset.
2406 cursor.key_beg.localization = ip->obj_localization +
2407 HAMMER_LOCALIZE_MISC;
2408 cursor.key_beg.obj_id = ip->obj_id;
2409 cursor.key_beg.create_tid = 0;
2410 cursor.key_beg.delete_tid = 0;
2411 cursor.key_beg.obj_type = 0;
2412 cursor.key_beg.key = bio->bio_offset + 1;
2413 cursor.asof = ip->obj_asof;
2414 cursor.flags |= HAMMER_CURSOR_ASOF;
2416 cursor.key_end = cursor.key_beg;
2417 KKASSERT(ip->ino_data.obj_type == HAMMER_OBJTYPE_REGFILE);
2419 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_DBFILE) {
2420 cursor.key_beg.rec_type = HAMMER_RECTYPE_DB;
2421 cursor.key_end.rec_type = HAMMER_RECTYPE_DB;
2422 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
2426 ran_end = bio->bio_offset + bp->b_bufsize;
2427 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
2428 cursor.key_end.rec_type = HAMMER_RECTYPE_DATA;
2429 tmp64 = ran_end + MAXPHYS + 1; /* work-around GCC-4 bug */
2430 if (tmp64 < ran_end)
2431 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
2433 cursor.key_end.key = ran_end + MAXPHYS + 1;
2435 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
2437 error = hammer_ip_first(&cursor);
2440 while (error == 0) {
2442 * Get the base file offset of the record. The key for
2443 * data records is (base + bytes) rather then (base).
2445 base = &cursor.leaf->base;
2446 rec_offset = base->key - cursor.leaf->data_len;
2449 * Calculate the gap, if any, and zero-fill it.
2451 * n is the offset of the start of the record verses our
2452 * current seek offset in the bio.
2454 n = (int)(rec_offset - (bio->bio_offset + boff));
2456 if (n > bp->b_bufsize - boff)
2457 n = bp->b_bufsize - boff;
2458 bzero((char *)bp->b_data + boff, n);
2464 * Calculate the data offset in the record and the number
2465 * of bytes we can copy.
2467 * There are two degenerate cases. First, boff may already
2468 * be at bp->b_bufsize. Secondly, the data offset within
2469 * the record may exceed the record's size.
2473 n = cursor.leaf->data_len - roff;
2475 kprintf("strategy_read: bad n=%d roff=%d\n", n, roff);
2477 } else if (n > bp->b_bufsize - boff) {
2478 n = bp->b_bufsize - boff;
2482 * Deal with cached truncations. This cool bit of code
2483 * allows truncate()/ftruncate() to avoid having to sync
2486 * If the frontend is truncated then all backend records are
2487 * subject to the frontend's truncation.
2489 * If the backend is truncated then backend records on-disk
2490 * (but not in-memory) are subject to the backend's
2491 * truncation. In-memory records owned by the backend
2492 * represent data written after the truncation point on the
2493 * backend and must not be truncated.
2495 * Truncate operations deal with frontend buffer cache
2496 * buffers and frontend-owned in-memory records synchronously.
2498 if (ip->flags & HAMMER_INODE_TRUNCATED) {
2499 if (hammer_cursor_ondisk(&cursor) ||
2500 cursor.iprec->flush_state == HAMMER_FST_FLUSH) {
2501 if (ip->trunc_off <= rec_offset)
2503 else if (ip->trunc_off < rec_offset + n)
2504 n = (int)(ip->trunc_off - rec_offset);
2507 if (ip->sync_flags & HAMMER_INODE_TRUNCATED) {
2508 if (hammer_cursor_ondisk(&cursor)) {
2509 if (ip->sync_trunc_off <= rec_offset)
2511 else if (ip->sync_trunc_off < rec_offset + n)
2512 n = (int)(ip->sync_trunc_off - rec_offset);
2517 * Try to issue a direct read into our bio if possible,
2518 * otherwise resolve the element data into a hammer_buffer
2521 * The buffer on-disk should be zerod past any real
2522 * truncation point, but may not be for any synthesized
2523 * truncation point from above.
2525 disk_offset = cursor.leaf->data_offset + roff;
2526 if (boff == 0 && n == bp->b_bufsize &&
2527 hammer_cursor_ondisk(&cursor) &&
2528 (disk_offset & HAMMER_BUFMASK) == 0) {
2529 KKASSERT((disk_offset & HAMMER_OFF_ZONE_MASK) ==
2530 HAMMER_ZONE_LARGE_DATA);
2531 nbio->bio_offset = disk_offset;
2532 error = hammer_io_direct_read(trans.hmp, nbio,
2536 error = hammer_ip_resolve_data(&cursor);
2538 bcopy((char *)cursor.data + roff,
2539 (char *)bp->b_data + boff, n);
2546 * Iterate until we have filled the request.
2549 if (boff == bp->b_bufsize)
2551 error = hammer_ip_next(&cursor);
2555 * There may have been a gap after the last record
2557 if (error == ENOENT)
2559 if (error == 0 && boff != bp->b_bufsize) {
2560 KKASSERT(boff < bp->b_bufsize);
2561 bzero((char *)bp->b_data + boff, bp->b_bufsize - boff);
2562 /* boff = bp->b_bufsize; */
2565 bp->b_error = error;
2567 bp->b_flags |= B_ERROR;
2572 * Cache the b-tree node for the last data read in cache[1].
2574 * If we hit the file EOF then also cache the node in the
2575 * governing director's cache[3], it will be used to initialize
2576 * the inode's cache[1] for any inodes looked up via the directory.
2578 * This doesn't reduce disk accesses since the B-Tree chain is
2579 * likely cached, but it does reduce cpu overhead when looking
2580 * up file offsets for cpdup/tar/cpio style iterations.
2583 hammer_cache_node(&ip->cache[1], cursor.node);
2584 if (ran_end >= ip->ino_data.size) {
2585 dip = hammer_find_inode(&trans, ip->ino_data.parent_obj_id,
2586 ip->obj_asof, ip->obj_localization);
2588 hammer_cache_node(&dip->cache[3], cursor.node);
2589 hammer_rel_inode(dip, 0);
2592 hammer_done_cursor(&cursor);
2593 hammer_done_transaction(&trans);
2598 * BMAP operation - used to support cluster_read() only.
2600 * (struct vnode *vp, off_t loffset, off_t *doffsetp, int *runp, int *runb)
2602 * This routine may return EOPNOTSUPP if the opration is not supported for
2603 * the specified offset. The contents of the pointer arguments do not
2604 * need to be initialized in that case.
2606 * If a disk address is available and properly aligned return 0 with
2607 * *doffsetp set to the zone-2 address, and *runp / *runb set appropriately
2608 * to the run-length relative to that offset. Callers may assume that
2609 * *doffsetp is valid if 0 is returned, even if *runp is not sufficiently
2610 * large, so return EOPNOTSUPP if it is not sufficiently large.
2614 hammer_vop_bmap(struct vop_bmap_args *ap)
2616 struct hammer_transaction trans;
2617 struct hammer_inode *ip;
2618 struct hammer_cursor cursor;
2619 hammer_base_elm_t base;
2623 int64_t base_offset;
2624 int64_t base_disk_offset;
2625 int64_t last_offset;
2626 hammer_off_t last_disk_offset;
2627 hammer_off_t disk_offset;
2632 ++hammer_stats_file_iopsr;
2633 ip = ap->a_vp->v_data;
2636 * We can only BMAP regular files. We can't BMAP database files,
2639 if (ip->ino_data.obj_type != HAMMER_OBJTYPE_REGFILE)
2643 * bmap is typically called with runp/runb both NULL when used
2644 * for writing. We do not support BMAP for writing atm.
2646 if (ap->a_cmd != BUF_CMD_READ)
2650 * Scan the B-Tree to acquire blockmap addresses, then translate
2653 hammer_simple_transaction(&trans, ip->hmp);
2655 kprintf("bmap_beg %016llx ip->cache %p\n",
2656 (long long)ap->a_loffset, ip->cache[1]);
2658 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
2661 * Key range (begin and end inclusive) to scan. Note that the key's
2662 * stored in the actual records represent BASE+LEN, not BASE. The
2663 * first record containing bio_offset will have a key > bio_offset.
2665 cursor.key_beg.localization = ip->obj_localization +
2666 HAMMER_LOCALIZE_MISC;
2667 cursor.key_beg.obj_id = ip->obj_id;
2668 cursor.key_beg.create_tid = 0;
2669 cursor.key_beg.delete_tid = 0;
2670 cursor.key_beg.obj_type = 0;
2672 cursor.key_beg.key = ap->a_loffset - MAXPHYS + 1;
2674 cursor.key_beg.key = ap->a_loffset + 1;
2675 if (cursor.key_beg.key < 0)
2676 cursor.key_beg.key = 0;
2677 cursor.asof = ip->obj_asof;
2678 cursor.flags |= HAMMER_CURSOR_ASOF;
2680 cursor.key_end = cursor.key_beg;
2681 KKASSERT(ip->ino_data.obj_type == HAMMER_OBJTYPE_REGFILE);
2683 ran_end = ap->a_loffset + MAXPHYS;
2684 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
2685 cursor.key_end.rec_type = HAMMER_RECTYPE_DATA;
2686 tmp64 = ran_end + MAXPHYS + 1; /* work-around GCC-4 bug */
2687 if (tmp64 < ran_end)
2688 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
2690 cursor.key_end.key = ran_end + MAXPHYS + 1;
2692 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
2694 error = hammer_ip_first(&cursor);
2695 base_offset = last_offset = 0;
2696 base_disk_offset = last_disk_offset = 0;
2698 while (error == 0) {
2700 * Get the base file offset of the record. The key for
2701 * data records is (base + bytes) rather then (base).
2703 * NOTE: rec_offset + rec_len may exceed the end-of-file.
2704 * The extra bytes should be zero on-disk and the BMAP op
2705 * should still be ok.
2707 base = &cursor.leaf->base;
2708 rec_offset = base->key - cursor.leaf->data_len;
2709 rec_len = cursor.leaf->data_len;
2712 * Incorporate any cached truncation.
2714 * NOTE: Modifications to rec_len based on synthesized
2715 * truncation points remove the guarantee that any extended
2716 * data on disk is zero (since the truncations may not have
2717 * taken place on-media yet).
2719 if (ip->flags & HAMMER_INODE_TRUNCATED) {
2720 if (hammer_cursor_ondisk(&cursor) ||
2721 cursor.iprec->flush_state == HAMMER_FST_FLUSH) {
2722 if (ip->trunc_off <= rec_offset)
2724 else if (ip->trunc_off < rec_offset + rec_len)
2725 rec_len = (int)(ip->trunc_off - rec_offset);
2728 if (ip->sync_flags & HAMMER_INODE_TRUNCATED) {
2729 if (hammer_cursor_ondisk(&cursor)) {
2730 if (ip->sync_trunc_off <= rec_offset)
2732 else if (ip->sync_trunc_off < rec_offset + rec_len)
2733 rec_len = (int)(ip->sync_trunc_off - rec_offset);
2738 * Accumulate information. If we have hit a discontiguous
2739 * block reset base_offset unless we are already beyond the
2740 * requested offset. If we are, that's it, we stop.
2744 if (hammer_cursor_ondisk(&cursor)) {
2745 disk_offset = cursor.leaf->data_offset;
2746 if (rec_offset != last_offset ||
2747 disk_offset != last_disk_offset) {
2748 if (rec_offset > ap->a_loffset)
2750 base_offset = rec_offset;
2751 base_disk_offset = disk_offset;
2753 last_offset = rec_offset + rec_len;
2754 last_disk_offset = disk_offset + rec_len;
2756 error = hammer_ip_next(&cursor);
2760 kprintf("BMAP %016llx: %016llx - %016llx\n",
2761 (long long)ap->a_loffset,
2762 (long long)base_offset,
2763 (long long)last_offset);
2764 kprintf("BMAP %16s: %016llx - %016llx\n", "",
2765 (long long)base_disk_offset,
2766 (long long)last_disk_offset);
2770 hammer_cache_node(&ip->cache[1], cursor.node);
2772 kprintf("bmap_end2 %016llx ip->cache %p\n",
2773 (long long)ap->a_loffset, ip->cache[1]);
2776 hammer_done_cursor(&cursor);
2777 hammer_done_transaction(&trans);
2780 * If we couldn't find any records or the records we did find were
2781 * all behind the requested offset, return failure. A forward
2782 * truncation can leave a hole w/ no on-disk records.
2784 if (last_offset == 0 || last_offset < ap->a_loffset)
2785 return (EOPNOTSUPP);
2788 * Figure out the block size at the requested offset and adjust
2789 * our limits so the cluster_read() does not create inappropriately
2790 * sized buffer cache buffers.
2792 blksize = hammer_blocksize(ap->a_loffset);
2793 if (hammer_blocksize(base_offset) != blksize) {
2794 base_offset = hammer_blockdemarc(base_offset, ap->a_loffset);
2796 if (last_offset != ap->a_loffset &&
2797 hammer_blocksize(last_offset - 1) != blksize) {
2798 last_offset = hammer_blockdemarc(ap->a_loffset,
2803 * Returning EOPNOTSUPP simply prevents the direct-IO optimization
2806 disk_offset = base_disk_offset + (ap->a_loffset - base_offset);
2808 if ((disk_offset & HAMMER_OFF_ZONE_MASK) != HAMMER_ZONE_LARGE_DATA) {
2810 * Only large-data zones can be direct-IOd
2813 } else if ((disk_offset & HAMMER_BUFMASK) ||
2814 (last_offset - ap->a_loffset) < blksize) {
2816 * doffsetp is not aligned or the forward run size does
2817 * not cover a whole buffer, disallow the direct I/O.
2824 *ap->a_doffsetp = disk_offset;
2826 *ap->a_runb = ap->a_loffset - base_offset;
2827 KKASSERT(*ap->a_runb >= 0);
2830 *ap->a_runp = last_offset - ap->a_loffset;
2831 KKASSERT(*ap->a_runp >= 0);
2839 * Write to a regular file. Because this is a strategy call the OS is
2840 * trying to actually get data onto the media.
2844 hammer_vop_strategy_write(struct vop_strategy_args *ap)
2846 hammer_record_t record;
2857 ip = ap->a_vp->v_data;
2860 blksize = hammer_blocksize(bio->bio_offset);
2861 KKASSERT(bp->b_bufsize == blksize);
2863 if (ip->flags & HAMMER_INODE_RO) {
2864 bp->b_error = EROFS;
2865 bp->b_flags |= B_ERROR;
2871 * Interlock with inode destruction (no in-kernel or directory
2872 * topology visibility). If we queue new IO while trying to
2873 * destroy the inode we can deadlock the vtrunc call in
2874 * hammer_inode_unloadable_check().
2876 * Besides, there's no point flushing a bp associated with an
2877 * inode that is being destroyed on-media and has no kernel
2880 if ((ip->flags | ip->sync_flags) &
2881 (HAMMER_INODE_DELETING|HAMMER_INODE_DELETED)) {
2888 * Reserve space and issue a direct-write from the front-end.
2889 * NOTE: The direct_io code will hammer_bread/bcopy smaller
2892 * An in-memory record will be installed to reference the storage
2893 * until the flusher can get to it.
2895 * Since we own the high level bio the front-end will not try to
2896 * do a direct-read until the write completes.
2898 * NOTE: The only time we do not reserve a full-sized buffers
2899 * worth of data is if the file is small. We do not try to
2900 * allocate a fragment (from the small-data zone) at the end of
2901 * an otherwise large file as this can lead to wildly separated
2904 KKASSERT((bio->bio_offset & HAMMER_BUFMASK) == 0);
2905 KKASSERT(bio->bio_offset < ip->ino_data.size);
2906 if (bio->bio_offset || ip->ino_data.size > HAMMER_BUFSIZE / 2)
2907 bytes = bp->b_bufsize;
2909 bytes = ((int)ip->ino_data.size + 15) & ~15;
2911 record = hammer_ip_add_bulk(ip, bio->bio_offset, bp->b_data,
2914 hammer_io_direct_write(hmp, record, bio);
2915 if (ip->rsv_recs > 1 && hmp->rsv_recs > hammer_limit_recs)
2916 hammer_flush_inode(ip, 0);
2918 bp->b_bio2.bio_offset = NOOFFSET;
2919 bp->b_error = error;
2920 bp->b_flags |= B_ERROR;
2927 * dounlink - disconnect a directory entry
2929 * XXX whiteout support not really in yet
2932 hammer_dounlink(hammer_transaction_t trans, struct nchandle *nch,
2933 struct vnode *dvp, struct ucred *cred,
2934 int flags, int isdir)
2936 struct namecache *ncp;
2939 struct hammer_cursor cursor;
2941 u_int32_t max_iterations;
2945 * Calculate the namekey and setup the key range for the scan. This
2946 * works kinda like a chained hash table where the lower 32 bits
2947 * of the namekey synthesize the chain.
2949 * The key range is inclusive of both key_beg and key_end.
2954 if (dip->flags & HAMMER_INODE_RO)
2957 namekey = hammer_directory_namekey(dip, ncp->nc_name, ncp->nc_nlen,
2960 hammer_init_cursor(trans, &cursor, &dip->cache[1], dip);
2961 cursor.key_beg.localization = dip->obj_localization +
2962 hammer_dir_localization(dip);
2963 cursor.key_beg.obj_id = dip->obj_id;
2964 cursor.key_beg.key = namekey;
2965 cursor.key_beg.create_tid = 0;
2966 cursor.key_beg.delete_tid = 0;
2967 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
2968 cursor.key_beg.obj_type = 0;
2970 cursor.key_end = cursor.key_beg;
2971 cursor.key_end.key += max_iterations;
2972 cursor.asof = dip->obj_asof;
2973 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
2976 * Scan all matching records (the chain), locate the one matching
2977 * the requested path component. info->last_error contains the
2978 * error code on search termination and could be 0, ENOENT, or
2981 * The hammer_ip_*() functions merge in-memory records with on-disk
2982 * records for the purposes of the search.
2984 error = hammer_ip_first(&cursor);
2986 while (error == 0) {
2987 error = hammer_ip_resolve_data(&cursor);
2990 nlen = cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF;
2992 if (ncp->nc_nlen == nlen &&
2993 bcmp(ncp->nc_name, cursor.data->entry.name, nlen) == 0) {
2996 error = hammer_ip_next(&cursor);
3000 * If all is ok we have to get the inode so we can adjust nlinks.
3001 * To avoid a deadlock with the flusher we must release the inode
3002 * lock on the directory when acquiring the inode for the entry.
3004 * If the target is a directory, it must be empty.
3007 hammer_unlock(&cursor.ip->lock);
3008 ip = hammer_get_inode(trans, dip, cursor.data->entry.obj_id,
3010 cursor.data->entry.localization,
3012 hammer_lock_sh(&cursor.ip->lock);
3013 if (error == ENOENT) {
3014 kprintf("HAMMER: WARNING: Removing "
3015 "dirent w/missing inode \"%s\"\n"
3016 "\tobj_id = %016llx\n",
3018 (long long)cursor.data->entry.obj_id);
3023 * If isdir >= 0 we validate that the entry is or is not a
3024 * directory. If isdir < 0 we don't care.
3026 if (error == 0 && isdir >= 0 && ip) {
3028 ip->ino_data.obj_type != HAMMER_OBJTYPE_DIRECTORY) {
3030 } else if (isdir == 0 &&
3031 ip->ino_data.obj_type == HAMMER_OBJTYPE_DIRECTORY) {
3037 * If we are trying to remove a directory the directory must
3040 * The check directory code can loop and deadlock/retry. Our
3041 * own cursor's node locks must be released to avoid a 3-way
3042 * deadlock with the flusher if the check directory code
3045 * If any changes whatsoever have been made to the cursor
3046 * set EDEADLK and retry.
3048 if (error == 0 && ip && ip->ino_data.obj_type ==
3049 HAMMER_OBJTYPE_DIRECTORY) {
3050 hammer_unlock_cursor(&cursor);
3051 error = hammer_ip_check_directory_empty(trans, ip);
3052 hammer_lock_cursor(&cursor);
3053 if (cursor.flags & HAMMER_CURSOR_RETEST) {
3054 kprintf("HAMMER: Warning: avoided deadlock "
3062 * Delete the directory entry.
3064 * WARNING: hammer_ip_del_directory() may have to terminate
3065 * the cursor to avoid a deadlock. It is ok to call
3066 * hammer_done_cursor() twice.
3069 error = hammer_ip_del_directory(trans, &cursor,
3072 hammer_done_cursor(&cursor);
3074 cache_setunresolved(nch);
3075 cache_setvp(nch, NULL);
3078 hammer_knote(ip->vp, NOTE_DELETE);
3079 cache_inval_vp(ip->vp, CINV_DESTROY);
3083 hammer_rel_inode(ip, 0);
3085 hammer_done_cursor(&cursor);
3087 if (error == EDEADLK)
3093 /************************************************************************
3094 * FIFO AND SPECFS OPS *
3095 ************************************************************************
3100 hammer_vop_fifoclose (struct vop_close_args *ap)
3102 /* XXX update itimes */
3103 return (VOCALL(&fifo_vnode_vops, &ap->a_head));
3107 hammer_vop_fiforead (struct vop_read_args *ap)
3111 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
3112 /* XXX update access time */
3117 hammer_vop_fifowrite (struct vop_write_args *ap)
3121 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
3122 /* XXX update access time */
3128 hammer_vop_fifokqfilter(struct vop_kqfilter_args *ap)
3132 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
3134 error = hammer_vop_kqfilter(ap);
3138 /************************************************************************
3140 ************************************************************************
3143 static void filt_hammerdetach(struct knote *kn);
3144 static int filt_hammerread(struct knote *kn, long hint);
3145 static int filt_hammerwrite(struct knote *kn, long hint);
3146 static int filt_hammervnode(struct knote *kn, long hint);
3148 static struct filterops hammerread_filtops =
3149 { 1, NULL, filt_hammerdetach, filt_hammerread };
3150 static struct filterops hammerwrite_filtops =
3151 { 1, NULL, filt_hammerdetach, filt_hammerwrite };
3152 static struct filterops hammervnode_filtops =
3153 { 1, NULL, filt_hammerdetach, filt_hammervnode };
3157 hammer_vop_kqfilter(struct vop_kqfilter_args *ap)
3159 struct vnode *vp = ap->a_vp;
3160 struct knote *kn = ap->a_kn;
3163 switch (kn->kn_filter) {
3165 kn->kn_fop = &hammerread_filtops;
3168 kn->kn_fop = &hammerwrite_filtops;
3171 kn->kn_fop = &hammervnode_filtops;
3177 kn->kn_hook = (caddr_t)vp;
3179 lwkt_gettoken(&vlock, &vp->v_token);
3180 SLIST_INSERT_HEAD(&vp->v_pollinfo.vpi_selinfo.si_note, kn, kn_selnext);
3181 lwkt_reltoken(&vlock);
3187 filt_hammerdetach(struct knote *kn)
3189 struct vnode *vp = (void *)kn->kn_hook;
3192 lwkt_gettoken(&vlock, &vp->v_token);
3193 SLIST_REMOVE(&vp->v_pollinfo.vpi_selinfo.si_note,
3194 kn, knote, kn_selnext);
3195 lwkt_reltoken(&vlock);
3199 filt_hammerread(struct knote *kn, long hint)
3201 struct vnode *vp = (void *)kn->kn_hook;
3202 hammer_inode_t ip = VTOI(vp);
3204 if (hint == NOTE_REVOKE) {
3205 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
3208 kn->kn_data = ip->ino_data.size - kn->kn_fp->f_offset;
3209 return (kn->kn_data != 0);
3213 filt_hammerwrite(struct knote *kn, long hint)
3215 if (hint == NOTE_REVOKE)
3216 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
3222 filt_hammervnode(struct knote *kn, long hint)
3224 if (kn->kn_sfflags & hint)
3225 kn->kn_fflags |= hint;
3226 if (hint == NOTE_REVOKE) {
3227 kn->kn_flags |= EV_EOF;
3230 return (kn->kn_fflags != 0);