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;
386 if (ap->a_vp->v_type != VREG)
392 seqcount = ap->a_ioflag >> 16;
394 if (ip->flags & HAMMER_INODE_RO)
398 * Create a transaction to cover the operations we perform.
400 hammer_start_transaction(&trans, hmp);
406 if (ap->a_ioflag & IO_APPEND)
407 uio->uio_offset = ip->ino_data.size;
410 * Check for illegal write offsets. Valid range is 0...2^63-1.
412 * NOTE: the base_off assignment is required to work around what
413 * I consider to be a GCC-4 optimization bug.
415 if (uio->uio_offset < 0) {
416 hammer_done_transaction(&trans);
419 base_offset = uio->uio_offset + uio->uio_resid; /* work around gcc-4 */
420 if (uio->uio_resid > 0 && base_offset <= uio->uio_offset) {
421 hammer_done_transaction(&trans);
426 * If reading or writing a huge amount of data we have to break
427 * atomicy and allow the operation to be interrupted by a signal
428 * or it can DOS the machine.
430 bigwrite = (uio->uio_resid > 100 * 1024 * 1024);
433 * Access the data typically in HAMMER_BUFSIZE blocks via the
434 * buffer cache, but HAMMER may use a variable block size based
437 while (uio->uio_resid > 0) {
442 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_WRITE)) != 0)
444 if (bigwrite && (error = hammer_signal_check(hmp)) != 0)
447 blksize = hammer_blocksize(uio->uio_offset);
450 * Do not allow HAMMER to blow out the buffer cache. Very
451 * large UIOs can lockout other processes due to bwillwrite()
454 * The hammer inode is not locked during these operations.
455 * The vnode is locked which can interfere with the pageout
456 * daemon for non-UIO_NOCOPY writes but should not interfere
457 * with the buffer cache. Even so, we cannot afford to
458 * allow the pageout daemon to build up too many dirty buffer
461 * Only call this if we aren't being recursively called from
462 * a virtual disk device (vn), else we may deadlock.
464 if ((ap->a_ioflag & IO_RECURSE) == 0)
468 * Do not allow HAMMER to blow out system memory by
469 * accumulating too many records. Records are so well
470 * decoupled from the buffer cache that it is possible
471 * for userland to push data out to the media via
472 * direct-write, but build up the records queued to the
473 * backend faster then the backend can flush them out.
474 * HAMMER has hit its write limit but the frontend has
475 * no pushback to slow it down.
477 if (hmp->rsv_recs > hammer_limit_recs / 2) {
479 * Get the inode on the flush list
481 if (ip->rsv_recs >= 64)
482 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
483 else if (ip->rsv_recs >= 16)
484 hammer_flush_inode(ip, 0);
487 * Keep the flusher going if the system keeps
490 delta = hmp->count_newrecords -
491 hmp->last_newrecords;
492 if (delta < 0 || delta > hammer_limit_recs / 2) {
493 hmp->last_newrecords = hmp->count_newrecords;
494 hammer_sync_hmp(hmp, MNT_NOWAIT);
498 * If we have gotten behind start slowing
501 delta = (hmp->rsv_recs - hammer_limit_recs) *
502 hz / hammer_limit_recs;
504 tsleep(&trans, 0, "hmrslo", delta);
508 * Calculate the blocksize at the current offset and figure
509 * out how much we can actually write.
511 blkmask = blksize - 1;
512 offset = (int)uio->uio_offset & blkmask;
513 base_offset = uio->uio_offset & ~(int64_t)blkmask;
514 n = blksize - offset;
515 if (n > uio->uio_resid)
517 if (uio->uio_offset + n > ip->ino_data.size) {
518 vnode_pager_setsize(ap->a_vp, uio->uio_offset + n);
520 kflags |= NOTE_EXTEND;
523 if (uio->uio_segflg == UIO_NOCOPY) {
525 * Issuing a write with the same data backing the
526 * buffer. Instantiate the buffer to collect the
527 * backing vm pages, then read-in any missing bits.
529 * This case is used by vop_stdputpages().
531 bp = getblk(ap->a_vp, base_offset,
532 blksize, GETBLK_BHEAVY, 0);
533 if ((bp->b_flags & B_CACHE) == 0) {
535 error = bread(ap->a_vp, base_offset,
538 } else if (offset == 0 && uio->uio_resid >= blksize) {
540 * Even though we are entirely overwriting the buffer
541 * we may still have to zero it out to avoid a
542 * mmap/write visibility issue.
544 bp = getblk(ap->a_vp, base_offset, blksize, GETBLK_BHEAVY, 0);
545 if ((bp->b_flags & B_CACHE) == 0)
547 } else if (base_offset >= ip->ino_data.size) {
549 * If the base offset of the buffer is beyond the
550 * file EOF, we don't have to issue a read.
552 bp = getblk(ap->a_vp, base_offset,
553 blksize, GETBLK_BHEAVY, 0);
557 * Partial overwrite, read in any missing bits then
558 * replace the portion being written.
560 error = bread(ap->a_vp, base_offset, blksize, &bp);
565 error = uiomove((char *)bp->b_data + offset,
570 * If we screwed up we have to undo any VM size changes we
576 vtruncbuf(ap->a_vp, ip->ino_data.size,
577 hammer_blocksize(ip->ino_data.size));
581 kflags |= NOTE_WRITE;
582 hammer_stats_file_write += n;
583 /* bp->b_flags |= B_CLUSTEROK; temporarily disabled */
584 if (ip->ino_data.size < uio->uio_offset) {
585 ip->ino_data.size = uio->uio_offset;
586 flags = HAMMER_INODE_DDIRTY;
587 vnode_pager_setsize(ap->a_vp, ip->ino_data.size);
591 ip->ino_data.mtime = trans.time;
592 flags |= HAMMER_INODE_MTIME | HAMMER_INODE_BUFS;
593 hammer_modify_inode(ip, flags);
596 * Once we dirty the buffer any cached zone-X offset
597 * becomes invalid. HAMMER NOTE: no-history mode cannot
598 * allow overwriting over the same data sector unless
599 * we provide UNDOs for the old data, which we don't.
601 bp->b_bio2.bio_offset = NOOFFSET;
604 * Final buffer disposition.
606 bp->b_flags |= B_AGE;
607 if (ap->a_ioflag & IO_SYNC) {
609 } else if (ap->a_ioflag & IO_DIRECT) {
615 hammer_done_transaction(&trans);
616 hammer_knote(ap->a_vp, kflags);
621 * hammer_vop_access { vp, mode, cred }
625 hammer_vop_access(struct vop_access_args *ap)
627 struct hammer_inode *ip = VTOI(ap->a_vp);
632 ++hammer_stats_file_iopsr;
633 uid = hammer_to_unix_xid(&ip->ino_data.uid);
634 gid = hammer_to_unix_xid(&ip->ino_data.gid);
636 error = vop_helper_access(ap, uid, gid, ip->ino_data.mode,
637 ip->ino_data.uflags);
642 * hammer_vop_advlock { vp, id, op, fl, flags }
646 hammer_vop_advlock(struct vop_advlock_args *ap)
648 hammer_inode_t ip = VTOI(ap->a_vp);
650 return (lf_advlock(ap, &ip->advlock, ip->ino_data.size));
654 * hammer_vop_close { vp, fflag }
658 hammer_vop_close(struct vop_close_args *ap)
660 /*hammer_inode_t ip = VTOI(ap->a_vp);*/
661 return (vop_stdclose(ap));
665 * hammer_vop_ncreate { nch, dvp, vpp, cred, vap }
667 * The operating system has already ensured that the directory entry
668 * does not exist and done all appropriate namespace locking.
672 hammer_vop_ncreate(struct vop_ncreate_args *ap)
674 struct hammer_transaction trans;
675 struct hammer_inode *dip;
676 struct hammer_inode *nip;
677 struct nchandle *nch;
681 dip = VTOI(ap->a_dvp);
683 if (dip->flags & HAMMER_INODE_RO)
685 if ((error = hammer_checkspace(dip->hmp, HAMMER_CHKSPC_CREATE)) != 0)
689 * Create a transaction to cover the operations we perform.
691 hammer_start_transaction(&trans, dip->hmp);
692 ++hammer_stats_file_iopsw;
695 * Create a new filesystem object of the requested type. The
696 * returned inode will be referenced and shared-locked to prevent
697 * it from being moved to the flusher.
699 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
700 dip, nch->ncp->nc_name, nch->ncp->nc_nlen,
703 hkprintf("hammer_create_inode error %d\n", error);
704 hammer_done_transaction(&trans);
710 * Add the new filesystem object to the directory. This will also
711 * bump the inode's link count.
713 error = hammer_ip_add_directory(&trans, dip,
714 nch->ncp->nc_name, nch->ncp->nc_nlen,
717 hkprintf("hammer_ip_add_directory error %d\n", error);
723 hammer_rel_inode(nip, 0);
724 hammer_done_transaction(&trans);
727 error = hammer_get_vnode(nip, ap->a_vpp);
728 hammer_done_transaction(&trans);
729 hammer_rel_inode(nip, 0);
731 cache_setunresolved(ap->a_nch);
732 cache_setvp(ap->a_nch, *ap->a_vpp);
734 hammer_knote(ap->a_dvp, NOTE_WRITE);
740 * hammer_vop_getattr { vp, vap }
742 * Retrieve an inode's attribute information. When accessing inodes
743 * historically we fake the atime field to ensure consistent results.
744 * The atime field is stored in the B-Tree element and allowed to be
745 * updated without cycling the element.
751 hammer_vop_getattr(struct vop_getattr_args *ap)
753 struct hammer_inode *ip = VTOI(ap->a_vp);
754 struct vattr *vap = ap->a_vap;
757 * We want the fsid to be different when accessing a filesystem
758 * with different as-of's so programs like diff don't think
759 * the files are the same.
761 * We also want the fsid to be the same when comparing snapshots,
762 * or when comparing mirrors (which might be backed by different
763 * physical devices). HAMMER fsids are based on the PFS's
766 * XXX there is a chance of collision here. The va_fsid reported
767 * by stat is different from the more involved fsid used in the
770 ++hammer_stats_file_iopsr;
771 hammer_lock_sh(&ip->lock);
772 vap->va_fsid = ip->pfsm->fsid_udev ^ (u_int32_t)ip->obj_asof ^
773 (u_int32_t)(ip->obj_asof >> 32);
775 vap->va_fileid = ip->ino_leaf.base.obj_id;
776 vap->va_mode = ip->ino_data.mode;
777 vap->va_nlink = ip->ino_data.nlinks;
778 vap->va_uid = hammer_to_unix_xid(&ip->ino_data.uid);
779 vap->va_gid = hammer_to_unix_xid(&ip->ino_data.gid);
782 vap->va_size = ip->ino_data.size;
785 * Special case for @@PFS softlinks. The actual size of the
786 * expanded softlink is "@@0x%016llx:%05d" == 26 bytes.
787 * or for MAX_TID is "@@-1:%05d" == 10 bytes.
789 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_SOFTLINK &&
790 ip->ino_data.size == 10 &&
791 ip->obj_asof == HAMMER_MAX_TID &&
792 ip->obj_localization == 0 &&
793 strncmp(ip->ino_data.ext.symlink, "@@PFS", 5) == 0) {
794 if (ip->pfsm->pfsd.mirror_flags & HAMMER_PFSD_SLAVE)
801 * We must provide a consistent atime and mtime for snapshots
802 * so people can do a 'tar cf - ... | md5' on them and get
803 * consistent results.
805 if (ip->flags & HAMMER_INODE_RO) {
806 hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_atime);
807 hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_mtime);
809 hammer_time_to_timespec(ip->ino_data.atime, &vap->va_atime);
810 hammer_time_to_timespec(ip->ino_data.mtime, &vap->va_mtime);
812 hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_ctime);
813 vap->va_flags = ip->ino_data.uflags;
814 vap->va_gen = 1; /* hammer inums are unique for all time */
815 vap->va_blocksize = HAMMER_BUFSIZE;
816 if (ip->ino_data.size >= HAMMER_XDEMARC) {
817 vap->va_bytes = (ip->ino_data.size + HAMMER_XBUFMASK64) &
819 } else if (ip->ino_data.size > HAMMER_BUFSIZE / 2) {
820 vap->va_bytes = (ip->ino_data.size + HAMMER_BUFMASK64) &
823 vap->va_bytes = (ip->ino_data.size + 15) & ~15;
826 vap->va_type = hammer_get_vnode_type(ip->ino_data.obj_type);
827 vap->va_filerev = 0; /* XXX */
828 /* mtime uniquely identifies any adjustments made to the file XXX */
829 vap->va_fsmid = ip->ino_data.mtime;
830 vap->va_uid_uuid = ip->ino_data.uid;
831 vap->va_gid_uuid = ip->ino_data.gid;
832 vap->va_fsid_uuid = ip->hmp->fsid;
833 vap->va_vaflags = VA_UID_UUID_VALID | VA_GID_UUID_VALID |
836 switch (ip->ino_data.obj_type) {
837 case HAMMER_OBJTYPE_CDEV:
838 case HAMMER_OBJTYPE_BDEV:
839 vap->va_rmajor = ip->ino_data.rmajor;
840 vap->va_rminor = ip->ino_data.rminor;
845 hammer_unlock(&ip->lock);
850 * hammer_vop_nresolve { nch, dvp, cred }
852 * Locate the requested directory entry.
856 hammer_vop_nresolve(struct vop_nresolve_args *ap)
858 struct hammer_transaction trans;
859 struct namecache *ncp;
863 struct hammer_cursor cursor;
872 u_int32_t localization;
873 u_int32_t max_iterations;
876 * Misc initialization, plus handle as-of name extensions. Look for
877 * the '@@' extension. Note that as-of files and directories cannot
880 dip = VTOI(ap->a_dvp);
881 ncp = ap->a_nch->ncp;
882 asof = dip->obj_asof;
883 localization = dip->obj_localization; /* for code consistency */
885 flags = dip->flags & HAMMER_INODE_RO;
888 hammer_simple_transaction(&trans, dip->hmp);
889 ++hammer_stats_file_iopsr;
891 for (i = 0; i < nlen; ++i) {
892 if (ncp->nc_name[i] == '@' && ncp->nc_name[i+1] == '@') {
893 error = hammer_str_to_tid(ncp->nc_name + i + 2,
894 &ispfs, &asof, &localization);
899 if (asof != HAMMER_MAX_TID)
900 flags |= HAMMER_INODE_RO;
907 * If this is a PFS softlink we dive into the PFS
909 if (ispfs && nlen == 0) {
910 ip = hammer_get_inode(&trans, dip, HAMMER_OBJID_ROOT,
914 error = hammer_get_vnode(ip, &vp);
915 hammer_rel_inode(ip, 0);
921 cache_setvp(ap->a_nch, vp);
928 * If there is no path component the time extension is relative to dip.
929 * e.g. "fubar/@@<snapshot>"
931 * "." is handled by the kernel, but ".@@<snapshot>" is not.
932 * e.g. "fubar/.@@<snapshot>"
934 * ".." is handled by the kernel. We do not currently handle
937 if (nlen == 0 || (nlen == 1 && ncp->nc_name[0] == '.')) {
938 ip = hammer_get_inode(&trans, dip, dip->obj_id,
939 asof, dip->obj_localization,
942 error = hammer_get_vnode(ip, &vp);
943 hammer_rel_inode(ip, 0);
949 cache_setvp(ap->a_nch, vp);
956 * Calculate the namekey and setup the key range for the scan. This
957 * works kinda like a chained hash table where the lower 32 bits
958 * of the namekey synthesize the chain.
960 * The key range is inclusive of both key_beg and key_end.
962 namekey = hammer_directory_namekey(dip, ncp->nc_name, nlen,
965 error = hammer_init_cursor(&trans, &cursor, &dip->cache[1], dip);
966 cursor.key_beg.localization = dip->obj_localization +
967 hammer_dir_localization(dip);
968 cursor.key_beg.obj_id = dip->obj_id;
969 cursor.key_beg.key = namekey;
970 cursor.key_beg.create_tid = 0;
971 cursor.key_beg.delete_tid = 0;
972 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
973 cursor.key_beg.obj_type = 0;
975 cursor.key_end = cursor.key_beg;
976 cursor.key_end.key += max_iterations;
978 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
981 * Scan all matching records (the chain), locate the one matching
982 * the requested path component.
984 * The hammer_ip_*() functions merge in-memory records with on-disk
985 * records for the purposes of the search.
988 localization = HAMMER_DEF_LOCALIZATION;
991 error = hammer_ip_first(&cursor);
993 error = hammer_ip_resolve_data(&cursor);
996 if (nlen == cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF &&
997 bcmp(ncp->nc_name, cursor.data->entry.name, nlen) == 0) {
998 obj_id = cursor.data->entry.obj_id;
999 localization = cursor.data->entry.localization;
1002 error = hammer_ip_next(&cursor);
1005 hammer_done_cursor(&cursor);
1008 * Lookup the obj_id. This should always succeed. If it does not
1009 * the filesystem may be damaged and we return a dummy inode.
1012 ip = hammer_get_inode(&trans, dip, obj_id,
1015 if (error == ENOENT) {
1016 kprintf("HAMMER: WARNING: Missing "
1017 "inode for dirent \"%s\"\n"
1018 "\tobj_id = %016llx, asof=%016llx, lo=%08x\n",
1020 (long long)obj_id, (long long)asof,
1023 ip = hammer_get_dummy_inode(&trans, dip, obj_id,
1028 error = hammer_get_vnode(ip, &vp);
1029 hammer_rel_inode(ip, 0);
1035 cache_setvp(ap->a_nch, vp);
1038 } else if (error == ENOENT) {
1039 cache_setvp(ap->a_nch, NULL);
1042 hammer_done_transaction(&trans);
1047 * hammer_vop_nlookupdotdot { dvp, vpp, cred }
1049 * Locate the parent directory of a directory vnode.
1051 * dvp is referenced but not locked. *vpp must be returned referenced and
1052 * locked. A parent_obj_id of 0 does not necessarily indicate that we are
1053 * at the root, instead it could indicate that the directory we were in was
1056 * NOTE: as-of sequences are not linked into the directory structure. If
1057 * we are at the root with a different asof then the mount point, reload
1058 * the same directory with the mount point's asof. I'm not sure what this
1059 * will do to NFS. We encode ASOF stamps in NFS file handles so it might not
1060 * get confused, but it hasn't been tested.
1064 hammer_vop_nlookupdotdot(struct vop_nlookupdotdot_args *ap)
1066 struct hammer_transaction trans;
1067 struct hammer_inode *dip;
1068 struct hammer_inode *ip;
1069 int64_t parent_obj_id;
1070 u_int32_t parent_obj_localization;
1074 dip = VTOI(ap->a_dvp);
1075 asof = dip->obj_asof;
1078 * Whos are parent? This could be the root of a pseudo-filesystem
1079 * whos parent is in another localization domain.
1081 parent_obj_id = dip->ino_data.parent_obj_id;
1082 if (dip->obj_id == HAMMER_OBJID_ROOT)
1083 parent_obj_localization = dip->ino_data.ext.obj.parent_obj_localization;
1085 parent_obj_localization = dip->obj_localization;
1087 if (parent_obj_id == 0) {
1088 if (dip->obj_id == HAMMER_OBJID_ROOT &&
1089 asof != dip->hmp->asof) {
1090 parent_obj_id = dip->obj_id;
1091 asof = dip->hmp->asof;
1092 *ap->a_fakename = kmalloc(19, M_TEMP, M_WAITOK);
1093 ksnprintf(*ap->a_fakename, 19, "0x%016llx",
1094 (long long)dip->obj_asof);
1101 hammer_simple_transaction(&trans, dip->hmp);
1102 ++hammer_stats_file_iopsr;
1104 ip = hammer_get_inode(&trans, dip, parent_obj_id,
1105 asof, parent_obj_localization,
1106 dip->flags, &error);
1108 error = hammer_get_vnode(ip, ap->a_vpp);
1109 hammer_rel_inode(ip, 0);
1113 hammer_done_transaction(&trans);
1118 * hammer_vop_nlink { nch, dvp, vp, cred }
1122 hammer_vop_nlink(struct vop_nlink_args *ap)
1124 struct hammer_transaction trans;
1125 struct hammer_inode *dip;
1126 struct hammer_inode *ip;
1127 struct nchandle *nch;
1130 if (ap->a_dvp->v_mount != ap->a_vp->v_mount)
1134 dip = VTOI(ap->a_dvp);
1135 ip = VTOI(ap->a_vp);
1137 if (dip->obj_localization != ip->obj_localization)
1140 if (dip->flags & HAMMER_INODE_RO)
1142 if (ip->flags & HAMMER_INODE_RO)
1144 if ((error = hammer_checkspace(dip->hmp, HAMMER_CHKSPC_CREATE)) != 0)
1148 * Create a transaction to cover the operations we perform.
1150 hammer_start_transaction(&trans, dip->hmp);
1151 ++hammer_stats_file_iopsw;
1154 * Add the filesystem object to the directory. Note that neither
1155 * dip nor ip are referenced or locked, but their vnodes are
1156 * referenced. This function will bump the inode's link count.
1158 error = hammer_ip_add_directory(&trans, dip,
1159 nch->ncp->nc_name, nch->ncp->nc_nlen,
1166 cache_setunresolved(nch);
1167 cache_setvp(nch, ap->a_vp);
1169 hammer_done_transaction(&trans);
1170 hammer_knote(ap->a_vp, NOTE_LINK);
1171 hammer_knote(ap->a_dvp, NOTE_WRITE);
1176 * hammer_vop_nmkdir { nch, dvp, vpp, cred, vap }
1178 * The operating system has already ensured that the directory entry
1179 * does not exist and done all appropriate namespace locking.
1183 hammer_vop_nmkdir(struct vop_nmkdir_args *ap)
1185 struct hammer_transaction trans;
1186 struct hammer_inode *dip;
1187 struct hammer_inode *nip;
1188 struct nchandle *nch;
1192 dip = VTOI(ap->a_dvp);
1194 if (dip->flags & HAMMER_INODE_RO)
1196 if ((error = hammer_checkspace(dip->hmp, HAMMER_CHKSPC_CREATE)) != 0)
1200 * Create a transaction to cover the operations we perform.
1202 hammer_start_transaction(&trans, dip->hmp);
1203 ++hammer_stats_file_iopsw;
1206 * Create a new filesystem object of the requested type. The
1207 * returned inode will be referenced but not locked.
1209 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
1210 dip, nch->ncp->nc_name, nch->ncp->nc_nlen,
1213 hkprintf("hammer_mkdir error %d\n", error);
1214 hammer_done_transaction(&trans);
1219 * Add the new filesystem object to the directory. This will also
1220 * bump the inode's link count.
1222 error = hammer_ip_add_directory(&trans, dip,
1223 nch->ncp->nc_name, nch->ncp->nc_nlen,
1226 hkprintf("hammer_mkdir (add) error %d\n", error);
1232 hammer_rel_inode(nip, 0);
1235 error = hammer_get_vnode(nip, ap->a_vpp);
1236 hammer_rel_inode(nip, 0);
1238 cache_setunresolved(ap->a_nch);
1239 cache_setvp(ap->a_nch, *ap->a_vpp);
1242 hammer_done_transaction(&trans);
1244 hammer_knote(ap->a_dvp, NOTE_WRITE | NOTE_LINK);
1249 * hammer_vop_nmknod { nch, dvp, vpp, cred, vap }
1251 * The operating system has already ensured that the directory entry
1252 * does not exist and done all appropriate namespace locking.
1256 hammer_vop_nmknod(struct vop_nmknod_args *ap)
1258 struct hammer_transaction trans;
1259 struct hammer_inode *dip;
1260 struct hammer_inode *nip;
1261 struct nchandle *nch;
1265 dip = VTOI(ap->a_dvp);
1267 if (dip->flags & HAMMER_INODE_RO)
1269 if ((error = hammer_checkspace(dip->hmp, HAMMER_CHKSPC_CREATE)) != 0)
1273 * Create a transaction to cover the operations we perform.
1275 hammer_start_transaction(&trans, dip->hmp);
1276 ++hammer_stats_file_iopsw;
1279 * Create a new filesystem object of the requested type. The
1280 * returned inode will be referenced but not locked.
1282 * If mknod specifies a directory a pseudo-fs is created.
1284 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
1285 dip, nch->ncp->nc_name, nch->ncp->nc_nlen,
1288 hammer_done_transaction(&trans);
1294 * Add the new filesystem object to the directory. This will also
1295 * bump the inode's link count.
1297 error = hammer_ip_add_directory(&trans, dip,
1298 nch->ncp->nc_name, nch->ncp->nc_nlen,
1305 hammer_rel_inode(nip, 0);
1308 error = hammer_get_vnode(nip, ap->a_vpp);
1309 hammer_rel_inode(nip, 0);
1311 cache_setunresolved(ap->a_nch);
1312 cache_setvp(ap->a_nch, *ap->a_vpp);
1315 hammer_done_transaction(&trans);
1317 hammer_knote(ap->a_dvp, NOTE_WRITE);
1322 * hammer_vop_open { vp, mode, cred, fp }
1326 hammer_vop_open(struct vop_open_args *ap)
1330 ++hammer_stats_file_iopsr;
1331 ip = VTOI(ap->a_vp);
1333 if ((ap->a_mode & FWRITE) && (ip->flags & HAMMER_INODE_RO))
1335 return(vop_stdopen(ap));
1339 * hammer_vop_print { vp }
1343 hammer_vop_print(struct vop_print_args *ap)
1349 * hammer_vop_readdir { vp, uio, cred, *eofflag, *ncookies, off_t **cookies }
1353 hammer_vop_readdir(struct vop_readdir_args *ap)
1355 struct hammer_transaction trans;
1356 struct hammer_cursor cursor;
1357 struct hammer_inode *ip;
1359 hammer_base_elm_t base;
1368 ++hammer_stats_file_iopsr;
1369 ip = VTOI(ap->a_vp);
1371 saveoff = uio->uio_offset;
1373 if (ap->a_ncookies) {
1374 ncookies = uio->uio_resid / 16 + 1;
1375 if (ncookies > 1024)
1377 cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
1385 hammer_simple_transaction(&trans, ip->hmp);
1388 * Handle artificial entries
1390 * It should be noted that the minimum value for a directory
1391 * hash key on-media is 0x0000000100000000, so we can use anything
1392 * less then that to represent our 'special' key space.
1396 r = vop_write_dirent(&error, uio, ip->obj_id, DT_DIR, 1, ".");
1400 cookies[cookie_index] = saveoff;
1403 if (cookie_index == ncookies)
1407 if (ip->ino_data.parent_obj_id) {
1408 r = vop_write_dirent(&error, uio,
1409 ip->ino_data.parent_obj_id,
1412 r = vop_write_dirent(&error, uio,
1413 ip->obj_id, DT_DIR, 2, "..");
1418 cookies[cookie_index] = saveoff;
1421 if (cookie_index == ncookies)
1426 * Key range (begin and end inclusive) to scan. Directory keys
1427 * directly translate to a 64 bit 'seek' position.
1429 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
1430 cursor.key_beg.localization = ip->obj_localization +
1431 hammer_dir_localization(ip);
1432 cursor.key_beg.obj_id = ip->obj_id;
1433 cursor.key_beg.create_tid = 0;
1434 cursor.key_beg.delete_tid = 0;
1435 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1436 cursor.key_beg.obj_type = 0;
1437 cursor.key_beg.key = saveoff;
1439 cursor.key_end = cursor.key_beg;
1440 cursor.key_end.key = HAMMER_MAX_KEY;
1441 cursor.asof = ip->obj_asof;
1442 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
1444 error = hammer_ip_first(&cursor);
1446 while (error == 0) {
1447 error = hammer_ip_resolve_data(&cursor);
1450 base = &cursor.leaf->base;
1451 saveoff = base->key;
1452 KKASSERT(cursor.leaf->data_len > HAMMER_ENTRY_NAME_OFF);
1454 if (base->obj_id != ip->obj_id)
1455 panic("readdir: bad record at %p", cursor.node);
1458 * Convert pseudo-filesystems into softlinks
1460 dtype = hammer_get_dtype(cursor.leaf->base.obj_type);
1461 r = vop_write_dirent(
1462 &error, uio, cursor.data->entry.obj_id,
1464 cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF ,
1465 (void *)cursor.data->entry.name);
1470 cookies[cookie_index] = base->key;
1472 if (cookie_index == ncookies)
1474 error = hammer_ip_next(&cursor);
1476 hammer_done_cursor(&cursor);
1479 hammer_done_transaction(&trans);
1482 *ap->a_eofflag = (error == ENOENT);
1483 uio->uio_offset = saveoff;
1484 if (error && cookie_index == 0) {
1485 if (error == ENOENT)
1488 kfree(cookies, M_TEMP);
1489 *ap->a_ncookies = 0;
1490 *ap->a_cookies = NULL;
1493 if (error == ENOENT)
1496 *ap->a_ncookies = cookie_index;
1497 *ap->a_cookies = cookies;
1504 * hammer_vop_readlink { vp, uio, cred }
1508 hammer_vop_readlink(struct vop_readlink_args *ap)
1510 struct hammer_transaction trans;
1511 struct hammer_cursor cursor;
1512 struct hammer_inode *ip;
1514 u_int32_t localization;
1515 hammer_pseudofs_inmem_t pfsm;
1518 ip = VTOI(ap->a_vp);
1521 * Shortcut if the symlink data was stuffed into ino_data.
1523 * Also expand special "@@PFS%05d" softlinks (expansion only
1524 * occurs for non-historical (current) accesses made from the
1525 * primary filesystem).
1527 if (ip->ino_data.size <= HAMMER_INODE_BASESYMLEN) {
1531 ptr = ip->ino_data.ext.symlink;
1532 bytes = (int)ip->ino_data.size;
1534 ip->obj_asof == HAMMER_MAX_TID &&
1535 ip->obj_localization == 0 &&
1536 strncmp(ptr, "@@PFS", 5) == 0) {
1537 hammer_simple_transaction(&trans, ip->hmp);
1538 bcopy(ptr + 5, buf, 5);
1540 localization = strtoul(buf, NULL, 10) << 16;
1541 pfsm = hammer_load_pseudofs(&trans, localization,
1544 if (pfsm->pfsd.mirror_flags &
1545 HAMMER_PFSD_SLAVE) {
1546 /* vap->va_size == 26 */
1547 ksnprintf(buf, sizeof(buf),
1549 (long long)pfsm->pfsd.sync_end_tid,
1550 localization >> 16);
1552 /* vap->va_size == 10 */
1553 ksnprintf(buf, sizeof(buf),
1555 localization >> 16);
1557 ksnprintf(buf, sizeof(buf),
1559 (long long)HAMMER_MAX_TID,
1560 localization >> 16);
1564 bytes = strlen(buf);
1567 hammer_rel_pseudofs(trans.hmp, pfsm);
1568 hammer_done_transaction(&trans);
1570 error = uiomove(ptr, bytes, ap->a_uio);
1577 hammer_simple_transaction(&trans, ip->hmp);
1578 ++hammer_stats_file_iopsr;
1579 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
1582 * Key range (begin and end inclusive) to scan. Directory keys
1583 * directly translate to a 64 bit 'seek' position.
1585 cursor.key_beg.localization = ip->obj_localization +
1586 HAMMER_LOCALIZE_MISC;
1587 cursor.key_beg.obj_id = ip->obj_id;
1588 cursor.key_beg.create_tid = 0;
1589 cursor.key_beg.delete_tid = 0;
1590 cursor.key_beg.rec_type = HAMMER_RECTYPE_FIX;
1591 cursor.key_beg.obj_type = 0;
1592 cursor.key_beg.key = HAMMER_FIXKEY_SYMLINK;
1593 cursor.asof = ip->obj_asof;
1594 cursor.flags |= HAMMER_CURSOR_ASOF;
1596 error = hammer_ip_lookup(&cursor);
1598 error = hammer_ip_resolve_data(&cursor);
1600 KKASSERT(cursor.leaf->data_len >=
1601 HAMMER_SYMLINK_NAME_OFF);
1602 error = uiomove(cursor.data->symlink.name,
1603 cursor.leaf->data_len -
1604 HAMMER_SYMLINK_NAME_OFF,
1608 hammer_done_cursor(&cursor);
1609 hammer_done_transaction(&trans);
1614 * hammer_vop_nremove { nch, dvp, cred }
1618 hammer_vop_nremove(struct vop_nremove_args *ap)
1620 struct hammer_transaction trans;
1621 struct hammer_inode *dip;
1624 dip = VTOI(ap->a_dvp);
1626 if (hammer_nohistory(dip) == 0 &&
1627 (error = hammer_checkspace(dip->hmp, HAMMER_CHKSPC_REMOVE)) != 0) {
1631 hammer_start_transaction(&trans, dip->hmp);
1632 ++hammer_stats_file_iopsw;
1633 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp, ap->a_cred, 0, 0);
1634 hammer_done_transaction(&trans);
1636 hammer_knote(ap->a_dvp, NOTE_WRITE);
1641 * hammer_vop_nrename { fnch, tnch, fdvp, tdvp, cred }
1645 hammer_vop_nrename(struct vop_nrename_args *ap)
1647 struct hammer_transaction trans;
1648 struct namecache *fncp;
1649 struct namecache *tncp;
1650 struct hammer_inode *fdip;
1651 struct hammer_inode *tdip;
1652 struct hammer_inode *ip;
1653 struct hammer_cursor cursor;
1655 u_int32_t max_iterations;
1658 if (ap->a_fdvp->v_mount != ap->a_tdvp->v_mount)
1660 if (ap->a_fdvp->v_mount != ap->a_fnch->ncp->nc_vp->v_mount)
1663 fdip = VTOI(ap->a_fdvp);
1664 tdip = VTOI(ap->a_tdvp);
1665 fncp = ap->a_fnch->ncp;
1666 tncp = ap->a_tnch->ncp;
1667 ip = VTOI(fncp->nc_vp);
1668 KKASSERT(ip != NULL);
1670 if (fdip->obj_localization != tdip->obj_localization)
1672 if (fdip->obj_localization != ip->obj_localization)
1675 if (fdip->flags & HAMMER_INODE_RO)
1677 if (tdip->flags & HAMMER_INODE_RO)
1679 if (ip->flags & HAMMER_INODE_RO)
1681 if ((error = hammer_checkspace(fdip->hmp, HAMMER_CHKSPC_CREATE)) != 0)
1684 hammer_start_transaction(&trans, fdip->hmp);
1685 ++hammer_stats_file_iopsw;
1688 * Remove tncp from the target directory and then link ip as
1689 * tncp. XXX pass trans to dounlink
1691 * Force the inode sync-time to match the transaction so it is
1692 * in-sync with the creation of the target directory entry.
1694 error = hammer_dounlink(&trans, ap->a_tnch, ap->a_tdvp,
1696 if (error == 0 || error == ENOENT) {
1697 error = hammer_ip_add_directory(&trans, tdip,
1698 tncp->nc_name, tncp->nc_nlen,
1701 ip->ino_data.parent_obj_id = tdip->obj_id;
1702 ip->ino_data.ctime = trans.time;
1703 hammer_modify_inode(ip, HAMMER_INODE_DDIRTY);
1707 goto failed; /* XXX */
1710 * Locate the record in the originating directory and remove it.
1712 * Calculate the namekey and setup the key range for the scan. This
1713 * works kinda like a chained hash table where the lower 32 bits
1714 * of the namekey synthesize the chain.
1716 * The key range is inclusive of both key_beg and key_end.
1718 namekey = hammer_directory_namekey(fdip, fncp->nc_name, fncp->nc_nlen,
1721 hammer_init_cursor(&trans, &cursor, &fdip->cache[1], fdip);
1722 cursor.key_beg.localization = fdip->obj_localization +
1723 hammer_dir_localization(fdip);
1724 cursor.key_beg.obj_id = fdip->obj_id;
1725 cursor.key_beg.key = namekey;
1726 cursor.key_beg.create_tid = 0;
1727 cursor.key_beg.delete_tid = 0;
1728 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1729 cursor.key_beg.obj_type = 0;
1731 cursor.key_end = cursor.key_beg;
1732 cursor.key_end.key += max_iterations;
1733 cursor.asof = fdip->obj_asof;
1734 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
1737 * Scan all matching records (the chain), locate the one matching
1738 * the requested path component.
1740 * The hammer_ip_*() functions merge in-memory records with on-disk
1741 * records for the purposes of the search.
1743 error = hammer_ip_first(&cursor);
1744 while (error == 0) {
1745 if (hammer_ip_resolve_data(&cursor) != 0)
1747 nlen = cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF;
1749 if (fncp->nc_nlen == nlen &&
1750 bcmp(fncp->nc_name, cursor.data->entry.name, nlen) == 0) {
1753 error = hammer_ip_next(&cursor);
1757 * If all is ok we have to get the inode so we can adjust nlinks.
1759 * WARNING: hammer_ip_del_directory() may have to terminate the
1760 * cursor to avoid a recursion. It's ok to call hammer_done_cursor()
1764 error = hammer_ip_del_directory(&trans, &cursor, fdip, ip);
1767 * XXX A deadlock here will break rename's atomicy for the purposes
1768 * of crash recovery.
1770 if (error == EDEADLK) {
1771 hammer_done_cursor(&cursor);
1776 * Cleanup and tell the kernel that the rename succeeded.
1778 hammer_done_cursor(&cursor);
1780 cache_rename(ap->a_fnch, ap->a_tnch);
1781 hammer_knote(ap->a_fdvp, NOTE_WRITE);
1782 hammer_knote(ap->a_tdvp, NOTE_WRITE);
1784 hammer_knote(ip->vp, NOTE_RENAME);
1788 hammer_done_transaction(&trans);
1793 * hammer_vop_nrmdir { nch, dvp, cred }
1797 hammer_vop_nrmdir(struct vop_nrmdir_args *ap)
1799 struct hammer_transaction trans;
1800 struct hammer_inode *dip;
1803 dip = VTOI(ap->a_dvp);
1805 if (hammer_nohistory(dip) == 0 &&
1806 (error = hammer_checkspace(dip->hmp, HAMMER_CHKSPC_REMOVE)) != 0) {
1810 hammer_start_transaction(&trans, dip->hmp);
1811 ++hammer_stats_file_iopsw;
1812 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp, ap->a_cred, 0, 1);
1813 hammer_done_transaction(&trans);
1815 hammer_knote(ap->a_dvp, NOTE_WRITE | NOTE_LINK);
1820 * hammer_vop_markatime { vp, cred }
1824 hammer_vop_markatime(struct vop_markatime_args *ap)
1826 struct hammer_transaction trans;
1827 struct hammer_inode *ip;
1829 ip = VTOI(ap->a_vp);
1830 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
1832 if (ip->flags & HAMMER_INODE_RO)
1834 if (ip->hmp->mp->mnt_flag & MNT_NOATIME)
1836 hammer_start_transaction(&trans, ip->hmp);
1837 ++hammer_stats_file_iopsw;
1839 ip->ino_data.atime = trans.time;
1840 hammer_modify_inode(ip, HAMMER_INODE_ATIME);
1841 hammer_done_transaction(&trans);
1842 hammer_knote(ap->a_vp, NOTE_ATTRIB);
1847 * hammer_vop_setattr { vp, vap, cred }
1851 hammer_vop_setattr(struct vop_setattr_args *ap)
1853 struct hammer_transaction trans;
1855 struct hammer_inode *ip;
1861 int64_t aligned_size;
1865 ip = ap->a_vp->v_data;
1869 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
1871 if (ip->flags & HAMMER_INODE_RO)
1873 if (hammer_nohistory(ip) == 0 &&
1874 (error = hammer_checkspace(ip->hmp, HAMMER_CHKSPC_REMOVE)) != 0) {
1878 hammer_start_transaction(&trans, ip->hmp);
1879 ++hammer_stats_file_iopsw;
1882 if (vap->va_flags != VNOVAL) {
1883 flags = ip->ino_data.uflags;
1884 error = vop_helper_setattr_flags(&flags, vap->va_flags,
1885 hammer_to_unix_xid(&ip->ino_data.uid),
1888 if (ip->ino_data.uflags != flags) {
1889 ip->ino_data.uflags = flags;
1890 ip->ino_data.ctime = trans.time;
1891 modflags |= HAMMER_INODE_DDIRTY;
1892 kflags |= NOTE_ATTRIB;
1894 if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) {
1901 if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) {
1905 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
1906 mode_t cur_mode = ip->ino_data.mode;
1907 uid_t cur_uid = hammer_to_unix_xid(&ip->ino_data.uid);
1908 gid_t cur_gid = hammer_to_unix_xid(&ip->ino_data.gid);
1912 error = vop_helper_chown(ap->a_vp, vap->va_uid, vap->va_gid,
1914 &cur_uid, &cur_gid, &cur_mode);
1916 hammer_guid_to_uuid(&uuid_uid, cur_uid);
1917 hammer_guid_to_uuid(&uuid_gid, cur_gid);
1918 if (bcmp(&uuid_uid, &ip->ino_data.uid,
1919 sizeof(uuid_uid)) ||
1920 bcmp(&uuid_gid, &ip->ino_data.gid,
1921 sizeof(uuid_gid)) ||
1922 ip->ino_data.mode != cur_mode
1924 ip->ino_data.uid = uuid_uid;
1925 ip->ino_data.gid = uuid_gid;
1926 ip->ino_data.mode = cur_mode;
1927 ip->ino_data.ctime = trans.time;
1928 modflags |= HAMMER_INODE_DDIRTY;
1930 kflags |= NOTE_ATTRIB;
1933 while (vap->va_size != VNOVAL && ip->ino_data.size != vap->va_size) {
1934 switch(ap->a_vp->v_type) {
1936 if (vap->va_size == ip->ino_data.size)
1939 * XXX break atomicy, we can deadlock the backend
1940 * if we do not release the lock. Probably not a
1943 blksize = hammer_blocksize(vap->va_size);
1944 if (vap->va_size < ip->ino_data.size) {
1945 vtruncbuf(ap->a_vp, vap->va_size, blksize);
1947 kflags |= NOTE_WRITE;
1949 vnode_pager_setsize(ap->a_vp, vap->va_size);
1951 kflags |= NOTE_WRITE | NOTE_EXTEND;
1953 ip->ino_data.size = vap->va_size;
1954 ip->ino_data.mtime = trans.time;
1955 modflags |= HAMMER_INODE_MTIME | HAMMER_INODE_DDIRTY;
1958 * on-media truncation is cached in the inode until
1959 * the inode is synchronized.
1962 hammer_ip_frontend_trunc(ip, vap->va_size);
1963 #ifdef DEBUG_TRUNCATE
1964 if (HammerTruncIp == NULL)
1967 if ((ip->flags & HAMMER_INODE_TRUNCATED) == 0) {
1968 ip->flags |= HAMMER_INODE_TRUNCATED;
1969 ip->trunc_off = vap->va_size;
1970 #ifdef DEBUG_TRUNCATE
1971 if (ip == HammerTruncIp)
1972 kprintf("truncate1 %016llx\n",
1973 (long long)ip->trunc_off);
1975 } else if (ip->trunc_off > vap->va_size) {
1976 ip->trunc_off = vap->va_size;
1977 #ifdef DEBUG_TRUNCATE
1978 if (ip == HammerTruncIp)
1979 kprintf("truncate2 %016llx\n",
1980 (long long)ip->trunc_off);
1983 #ifdef DEBUG_TRUNCATE
1984 if (ip == HammerTruncIp)
1985 kprintf("truncate3 %016llx (ignored)\n",
1986 (long long)vap->va_size);
1992 * If truncating we have to clean out a portion of
1993 * the last block on-disk. We do this in the
1994 * front-end buffer cache.
1996 aligned_size = (vap->va_size + (blksize - 1)) &
1997 ~(int64_t)(blksize - 1);
1998 if (truncating && vap->va_size < aligned_size) {
2002 aligned_size -= blksize;
2004 offset = (int)vap->va_size & (blksize - 1);
2005 error = bread(ap->a_vp, aligned_size,
2007 hammer_ip_frontend_trunc(ip, aligned_size);
2009 bzero(bp->b_data + offset,
2011 /* must de-cache direct-io offset */
2012 bp->b_bio2.bio_offset = NOOFFSET;
2015 kprintf("ERROR %d\n", error);
2021 if ((ip->flags & HAMMER_INODE_TRUNCATED) == 0) {
2022 ip->flags |= HAMMER_INODE_TRUNCATED;
2023 ip->trunc_off = vap->va_size;
2024 } else if (ip->trunc_off > vap->va_size) {
2025 ip->trunc_off = vap->va_size;
2027 hammer_ip_frontend_trunc(ip, vap->va_size);
2028 ip->ino_data.size = vap->va_size;
2029 ip->ino_data.mtime = trans.time;
2030 modflags |= HAMMER_INODE_MTIME | HAMMER_INODE_DDIRTY;
2031 kflags |= NOTE_ATTRIB;
2039 if (vap->va_atime.tv_sec != VNOVAL) {
2040 ip->ino_data.atime = hammer_timespec_to_time(&vap->va_atime);
2041 modflags |= HAMMER_INODE_ATIME;
2042 kflags |= NOTE_ATTRIB;
2044 if (vap->va_mtime.tv_sec != VNOVAL) {
2045 ip->ino_data.mtime = hammer_timespec_to_time(&vap->va_mtime);
2046 modflags |= HAMMER_INODE_MTIME;
2047 kflags |= NOTE_ATTRIB;
2049 if (vap->va_mode != (mode_t)VNOVAL) {
2050 mode_t cur_mode = ip->ino_data.mode;
2051 uid_t cur_uid = hammer_to_unix_xid(&ip->ino_data.uid);
2052 gid_t cur_gid = hammer_to_unix_xid(&ip->ino_data.gid);
2054 error = vop_helper_chmod(ap->a_vp, vap->va_mode, ap->a_cred,
2055 cur_uid, cur_gid, &cur_mode);
2056 if (error == 0 && ip->ino_data.mode != cur_mode) {
2057 ip->ino_data.mode = cur_mode;
2058 ip->ino_data.ctime = trans.time;
2059 modflags |= HAMMER_INODE_DDIRTY;
2060 kflags |= NOTE_ATTRIB;
2065 hammer_modify_inode(ip, modflags);
2066 hammer_done_transaction(&trans);
2067 hammer_knote(ap->a_vp, kflags);
2072 * hammer_vop_nsymlink { nch, dvp, vpp, cred, vap, target }
2076 hammer_vop_nsymlink(struct vop_nsymlink_args *ap)
2078 struct hammer_transaction trans;
2079 struct hammer_inode *dip;
2080 struct hammer_inode *nip;
2081 struct nchandle *nch;
2082 hammer_record_t record;
2086 ap->a_vap->va_type = VLNK;
2089 dip = VTOI(ap->a_dvp);
2091 if (dip->flags & HAMMER_INODE_RO)
2093 if ((error = hammer_checkspace(dip->hmp, HAMMER_CHKSPC_CREATE)) != 0)
2097 * Create a transaction to cover the operations we perform.
2099 hammer_start_transaction(&trans, dip->hmp);
2100 ++hammer_stats_file_iopsw;
2103 * Create a new filesystem object of the requested type. The
2104 * returned inode will be referenced but not locked.
2107 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
2108 dip, nch->ncp->nc_name, nch->ncp->nc_nlen,
2111 hammer_done_transaction(&trans);
2117 * Add a record representing the symlink. symlink stores the link
2118 * as pure data, not a string, and is no \0 terminated.
2121 bytes = strlen(ap->a_target);
2123 if (bytes <= HAMMER_INODE_BASESYMLEN) {
2124 bcopy(ap->a_target, nip->ino_data.ext.symlink, bytes);
2126 record = hammer_alloc_mem_record(nip, bytes);
2127 record->type = HAMMER_MEM_RECORD_GENERAL;
2129 record->leaf.base.localization = nip->obj_localization +
2130 HAMMER_LOCALIZE_MISC;
2131 record->leaf.base.key = HAMMER_FIXKEY_SYMLINK;
2132 record->leaf.base.rec_type = HAMMER_RECTYPE_FIX;
2133 record->leaf.data_len = bytes;
2134 KKASSERT(HAMMER_SYMLINK_NAME_OFF == 0);
2135 bcopy(ap->a_target, record->data->symlink.name, bytes);
2136 error = hammer_ip_add_record(&trans, record);
2140 * Set the file size to the length of the link.
2143 nip->ino_data.size = bytes;
2144 hammer_modify_inode(nip, HAMMER_INODE_DDIRTY);
2148 error = hammer_ip_add_directory(&trans, dip, nch->ncp->nc_name,
2149 nch->ncp->nc_nlen, nip);
2155 hammer_rel_inode(nip, 0);
2158 error = hammer_get_vnode(nip, ap->a_vpp);
2159 hammer_rel_inode(nip, 0);
2161 cache_setunresolved(ap->a_nch);
2162 cache_setvp(ap->a_nch, *ap->a_vpp);
2163 hammer_knote(ap->a_dvp, NOTE_WRITE);
2166 hammer_done_transaction(&trans);
2171 * hammer_vop_nwhiteout { nch, dvp, cred, flags }
2175 hammer_vop_nwhiteout(struct vop_nwhiteout_args *ap)
2177 struct hammer_transaction trans;
2178 struct hammer_inode *dip;
2181 dip = VTOI(ap->a_dvp);
2183 if (hammer_nohistory(dip) == 0 &&
2184 (error = hammer_checkspace(dip->hmp, HAMMER_CHKSPC_CREATE)) != 0) {
2188 hammer_start_transaction(&trans, dip->hmp);
2189 ++hammer_stats_file_iopsw;
2190 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp,
2191 ap->a_cred, ap->a_flags, -1);
2192 hammer_done_transaction(&trans);
2198 * hammer_vop_ioctl { vp, command, data, fflag, cred }
2202 hammer_vop_ioctl(struct vop_ioctl_args *ap)
2204 struct hammer_inode *ip = ap->a_vp->v_data;
2206 ++hammer_stats_file_iopsr;
2207 return(hammer_ioctl(ip, ap->a_command, ap->a_data,
2208 ap->a_fflag, ap->a_cred));
2213 hammer_vop_mountctl(struct vop_mountctl_args *ap)
2215 static const struct mountctl_opt extraopt[] = {
2216 { HMNT_NOHISTORY, "nohistory" },
2217 { HMNT_MASTERID, "master" },
2221 struct hammer_mount *hmp;
2229 mp = ap->a_head.a_ops->head.vv_mount;
2230 KKASSERT(mp->mnt_data != NULL);
2231 hmp = (struct hammer_mount *)mp->mnt_data;
2235 case MOUNTCTL_SET_EXPORT:
2236 if (ap->a_ctllen != sizeof(struct export_args))
2239 error = hammer_vfs_export(mp, ap->a_op,
2240 (const struct export_args *)ap->a_ctl);
2242 case MOUNTCTL_MOUNTFLAGS:
2245 * Call standard mountctl VOP function
2246 * so we get user mount flags.
2248 error = vop_stdmountctl(ap);
2252 usedbytes = *ap->a_res;
2254 if (usedbytes && usedbytes < ap->a_buflen) {
2255 pos = (char *)ap->a_buf + usedbytes;
2256 *pos++ = ','; /* Overwrite trailing \0 */
2259 usedbytes += vfs_flagstostr(hmp->hflags, extraopt, ap->a_buf,
2260 ap->a_buflen - usedbytes,
2263 /* Remove trailing comma if no HAMMER flags returned */
2264 if (usedbytes == *ap->a_res) {
2271 *ap->a_res += usedbytes;
2275 error = vop_stdmountctl(ap);
2282 * hammer_vop_strategy { vp, bio }
2284 * Strategy call, used for regular file read & write only. Note that the
2285 * bp may represent a cluster.
2287 * To simplify operation and allow better optimizations in the future,
2288 * this code does not make any assumptions with regards to buffer alignment
2293 hammer_vop_strategy(struct vop_strategy_args *ap)
2298 bp = ap->a_bio->bio_buf;
2302 error = hammer_vop_strategy_read(ap);
2305 error = hammer_vop_strategy_write(ap);
2308 bp->b_error = error = EINVAL;
2309 bp->b_flags |= B_ERROR;
2317 * Read from a regular file. Iterate the related records and fill in the
2318 * BIO/BUF. Gaps are zero-filled.
2320 * The support code in hammer_object.c should be used to deal with mixed
2321 * in-memory and on-disk records.
2323 * NOTE: Can be called from the cluster code with an oversized buf.
2329 hammer_vop_strategy_read(struct vop_strategy_args *ap)
2331 struct hammer_transaction trans;
2332 struct hammer_inode *ip;
2333 struct hammer_inode *dip;
2334 struct hammer_cursor cursor;
2335 hammer_base_elm_t base;
2336 hammer_off_t disk_offset;
2350 ip = ap->a_vp->v_data;
2353 * The zone-2 disk offset may have been set by the cluster code via
2354 * a BMAP operation, or else should be NOOFFSET.
2356 * Checking the high bits for a match against zone-2 should suffice.
2358 nbio = push_bio(bio);
2359 if ((nbio->bio_offset & HAMMER_OFF_ZONE_MASK) ==
2360 HAMMER_ZONE_LARGE_DATA) {
2361 error = hammer_io_direct_read(ip->hmp, nbio, NULL);
2366 * Well, that sucked. Do it the hard way. If all the stars are
2367 * aligned we may still be able to issue a direct-read.
2369 hammer_simple_transaction(&trans, ip->hmp);
2370 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
2373 * Key range (begin and end inclusive) to scan. Note that the key's
2374 * stored in the actual records represent BASE+LEN, not BASE. The
2375 * first record containing bio_offset will have a key > bio_offset.
2377 cursor.key_beg.localization = ip->obj_localization +
2378 HAMMER_LOCALIZE_MISC;
2379 cursor.key_beg.obj_id = ip->obj_id;
2380 cursor.key_beg.create_tid = 0;
2381 cursor.key_beg.delete_tid = 0;
2382 cursor.key_beg.obj_type = 0;
2383 cursor.key_beg.key = bio->bio_offset + 1;
2384 cursor.asof = ip->obj_asof;
2385 cursor.flags |= HAMMER_CURSOR_ASOF;
2387 cursor.key_end = cursor.key_beg;
2388 KKASSERT(ip->ino_data.obj_type == HAMMER_OBJTYPE_REGFILE);
2390 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_DBFILE) {
2391 cursor.key_beg.rec_type = HAMMER_RECTYPE_DB;
2392 cursor.key_end.rec_type = HAMMER_RECTYPE_DB;
2393 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
2397 ran_end = bio->bio_offset + bp->b_bufsize;
2398 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
2399 cursor.key_end.rec_type = HAMMER_RECTYPE_DATA;
2400 tmp64 = ran_end + MAXPHYS + 1; /* work-around GCC-4 bug */
2401 if (tmp64 < ran_end)
2402 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
2404 cursor.key_end.key = ran_end + MAXPHYS + 1;
2406 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
2408 error = hammer_ip_first(&cursor);
2411 while (error == 0) {
2413 * Get the base file offset of the record. The key for
2414 * data records is (base + bytes) rather then (base).
2416 base = &cursor.leaf->base;
2417 rec_offset = base->key - cursor.leaf->data_len;
2420 * Calculate the gap, if any, and zero-fill it.
2422 * n is the offset of the start of the record verses our
2423 * current seek offset in the bio.
2425 n = (int)(rec_offset - (bio->bio_offset + boff));
2427 if (n > bp->b_bufsize - boff)
2428 n = bp->b_bufsize - boff;
2429 bzero((char *)bp->b_data + boff, n);
2435 * Calculate the data offset in the record and the number
2436 * of bytes we can copy.
2438 * There are two degenerate cases. First, boff may already
2439 * be at bp->b_bufsize. Secondly, the data offset within
2440 * the record may exceed the record's size.
2444 n = cursor.leaf->data_len - roff;
2446 kprintf("strategy_read: bad n=%d roff=%d\n", n, roff);
2448 } else if (n > bp->b_bufsize - boff) {
2449 n = bp->b_bufsize - boff;
2453 * Deal with cached truncations. This cool bit of code
2454 * allows truncate()/ftruncate() to avoid having to sync
2457 * If the frontend is truncated then all backend records are
2458 * subject to the frontend's truncation.
2460 * If the backend is truncated then backend records on-disk
2461 * (but not in-memory) are subject to the backend's
2462 * truncation. In-memory records owned by the backend
2463 * represent data written after the truncation point on the
2464 * backend and must not be truncated.
2466 * Truncate operations deal with frontend buffer cache
2467 * buffers and frontend-owned in-memory records synchronously.
2469 if (ip->flags & HAMMER_INODE_TRUNCATED) {
2470 if (hammer_cursor_ondisk(&cursor) ||
2471 cursor.iprec->flush_state == HAMMER_FST_FLUSH) {
2472 if (ip->trunc_off <= rec_offset)
2474 else if (ip->trunc_off < rec_offset + n)
2475 n = (int)(ip->trunc_off - rec_offset);
2478 if (ip->sync_flags & HAMMER_INODE_TRUNCATED) {
2479 if (hammer_cursor_ondisk(&cursor)) {
2480 if (ip->sync_trunc_off <= rec_offset)
2482 else if (ip->sync_trunc_off < rec_offset + n)
2483 n = (int)(ip->sync_trunc_off - rec_offset);
2488 * Try to issue a direct read into our bio if possible,
2489 * otherwise resolve the element data into a hammer_buffer
2492 * The buffer on-disk should be zerod past any real
2493 * truncation point, but may not be for any synthesized
2494 * truncation point from above.
2496 disk_offset = cursor.leaf->data_offset + roff;
2497 if (boff == 0 && n == bp->b_bufsize &&
2498 hammer_cursor_ondisk(&cursor) &&
2499 (disk_offset & HAMMER_BUFMASK) == 0) {
2500 KKASSERT((disk_offset & HAMMER_OFF_ZONE_MASK) ==
2501 HAMMER_ZONE_LARGE_DATA);
2502 nbio->bio_offset = disk_offset;
2503 error = hammer_io_direct_read(trans.hmp, nbio,
2507 error = hammer_ip_resolve_data(&cursor);
2509 bcopy((char *)cursor.data + roff,
2510 (char *)bp->b_data + boff, n);
2517 * Iterate until we have filled the request.
2520 if (boff == bp->b_bufsize)
2522 error = hammer_ip_next(&cursor);
2526 * There may have been a gap after the last record
2528 if (error == ENOENT)
2530 if (error == 0 && boff != bp->b_bufsize) {
2531 KKASSERT(boff < bp->b_bufsize);
2532 bzero((char *)bp->b_data + boff, bp->b_bufsize - boff);
2533 /* boff = bp->b_bufsize; */
2536 bp->b_error = error;
2538 bp->b_flags |= B_ERROR;
2543 * Cache the b-tree node for the last data read in cache[1].
2545 * If we hit the file EOF then also cache the node in the
2546 * governing director's cache[3], it will be used to initialize
2547 * the inode's cache[1] for any inodes looked up via the directory.
2549 * This doesn't reduce disk accesses since the B-Tree chain is
2550 * likely cached, but it does reduce cpu overhead when looking
2551 * up file offsets for cpdup/tar/cpio style iterations.
2554 hammer_cache_node(&ip->cache[1], cursor.node);
2555 if (ran_end >= ip->ino_data.size) {
2556 dip = hammer_find_inode(&trans, ip->ino_data.parent_obj_id,
2557 ip->obj_asof, ip->obj_localization);
2559 hammer_cache_node(&dip->cache[3], cursor.node);
2560 hammer_rel_inode(dip, 0);
2563 hammer_done_cursor(&cursor);
2564 hammer_done_transaction(&trans);
2569 * BMAP operation - used to support cluster_read() only.
2571 * (struct vnode *vp, off_t loffset, off_t *doffsetp, int *runp, int *runb)
2573 * This routine may return EOPNOTSUPP if the opration is not supported for
2574 * the specified offset. The contents of the pointer arguments do not
2575 * need to be initialized in that case.
2577 * If a disk address is available and properly aligned return 0 with
2578 * *doffsetp set to the zone-2 address, and *runp / *runb set appropriately
2579 * to the run-length relative to that offset. Callers may assume that
2580 * *doffsetp is valid if 0 is returned, even if *runp is not sufficiently
2581 * large, so return EOPNOTSUPP if it is not sufficiently large.
2585 hammer_vop_bmap(struct vop_bmap_args *ap)
2587 struct hammer_transaction trans;
2588 struct hammer_inode *ip;
2589 struct hammer_cursor cursor;
2590 hammer_base_elm_t base;
2594 int64_t base_offset;
2595 int64_t base_disk_offset;
2596 int64_t last_offset;
2597 hammer_off_t last_disk_offset;
2598 hammer_off_t disk_offset;
2603 ++hammer_stats_file_iopsr;
2604 ip = ap->a_vp->v_data;
2607 * We can only BMAP regular files. We can't BMAP database files,
2610 if (ip->ino_data.obj_type != HAMMER_OBJTYPE_REGFILE)
2614 * bmap is typically called with runp/runb both NULL when used
2615 * for writing. We do not support BMAP for writing atm.
2617 if (ap->a_cmd != BUF_CMD_READ)
2621 * Scan the B-Tree to acquire blockmap addresses, then translate
2624 hammer_simple_transaction(&trans, ip->hmp);
2626 kprintf("bmap_beg %016llx ip->cache %p\n",
2627 (long long)ap->a_loffset, ip->cache[1]);
2629 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
2632 * Key range (begin and end inclusive) to scan. Note that the key's
2633 * stored in the actual records represent BASE+LEN, not BASE. The
2634 * first record containing bio_offset will have a key > bio_offset.
2636 cursor.key_beg.localization = ip->obj_localization +
2637 HAMMER_LOCALIZE_MISC;
2638 cursor.key_beg.obj_id = ip->obj_id;
2639 cursor.key_beg.create_tid = 0;
2640 cursor.key_beg.delete_tid = 0;
2641 cursor.key_beg.obj_type = 0;
2643 cursor.key_beg.key = ap->a_loffset - MAXPHYS + 1;
2645 cursor.key_beg.key = ap->a_loffset + 1;
2646 if (cursor.key_beg.key < 0)
2647 cursor.key_beg.key = 0;
2648 cursor.asof = ip->obj_asof;
2649 cursor.flags |= HAMMER_CURSOR_ASOF;
2651 cursor.key_end = cursor.key_beg;
2652 KKASSERT(ip->ino_data.obj_type == HAMMER_OBJTYPE_REGFILE);
2654 ran_end = ap->a_loffset + MAXPHYS;
2655 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
2656 cursor.key_end.rec_type = HAMMER_RECTYPE_DATA;
2657 tmp64 = ran_end + MAXPHYS + 1; /* work-around GCC-4 bug */
2658 if (tmp64 < ran_end)
2659 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
2661 cursor.key_end.key = ran_end + MAXPHYS + 1;
2663 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
2665 error = hammer_ip_first(&cursor);
2666 base_offset = last_offset = 0;
2667 base_disk_offset = last_disk_offset = 0;
2669 while (error == 0) {
2671 * Get the base file offset of the record. The key for
2672 * data records is (base + bytes) rather then (base).
2674 * NOTE: rec_offset + rec_len may exceed the end-of-file.
2675 * The extra bytes should be zero on-disk and the BMAP op
2676 * should still be ok.
2678 base = &cursor.leaf->base;
2679 rec_offset = base->key - cursor.leaf->data_len;
2680 rec_len = cursor.leaf->data_len;
2683 * Incorporate any cached truncation.
2685 * NOTE: Modifications to rec_len based on synthesized
2686 * truncation points remove the guarantee that any extended
2687 * data on disk is zero (since the truncations may not have
2688 * taken place on-media yet).
2690 if (ip->flags & HAMMER_INODE_TRUNCATED) {
2691 if (hammer_cursor_ondisk(&cursor) ||
2692 cursor.iprec->flush_state == HAMMER_FST_FLUSH) {
2693 if (ip->trunc_off <= rec_offset)
2695 else if (ip->trunc_off < rec_offset + rec_len)
2696 rec_len = (int)(ip->trunc_off - rec_offset);
2699 if (ip->sync_flags & HAMMER_INODE_TRUNCATED) {
2700 if (hammer_cursor_ondisk(&cursor)) {
2701 if (ip->sync_trunc_off <= rec_offset)
2703 else if (ip->sync_trunc_off < rec_offset + rec_len)
2704 rec_len = (int)(ip->sync_trunc_off - rec_offset);
2709 * Accumulate information. If we have hit a discontiguous
2710 * block reset base_offset unless we are already beyond the
2711 * requested offset. If we are, that's it, we stop.
2715 if (hammer_cursor_ondisk(&cursor)) {
2716 disk_offset = cursor.leaf->data_offset;
2717 if (rec_offset != last_offset ||
2718 disk_offset != last_disk_offset) {
2719 if (rec_offset > ap->a_loffset)
2721 base_offset = rec_offset;
2722 base_disk_offset = disk_offset;
2724 last_offset = rec_offset + rec_len;
2725 last_disk_offset = disk_offset + rec_len;
2727 error = hammer_ip_next(&cursor);
2731 kprintf("BMAP %016llx: %016llx - %016llx\n",
2732 (long long)ap->a_loffset,
2733 (long long)base_offset,
2734 (long long)last_offset);
2735 kprintf("BMAP %16s: %016llx - %016llx\n", "",
2736 (long long)base_disk_offset,
2737 (long long)last_disk_offset);
2741 hammer_cache_node(&ip->cache[1], cursor.node);
2743 kprintf("bmap_end2 %016llx ip->cache %p\n",
2744 (long long)ap->a_loffset, ip->cache[1]);
2747 hammer_done_cursor(&cursor);
2748 hammer_done_transaction(&trans);
2751 * If we couldn't find any records or the records we did find were
2752 * all behind the requested offset, return failure. A forward
2753 * truncation can leave a hole w/ no on-disk records.
2755 if (last_offset == 0 || last_offset < ap->a_loffset)
2756 return (EOPNOTSUPP);
2759 * Figure out the block size at the requested offset and adjust
2760 * our limits so the cluster_read() does not create inappropriately
2761 * sized buffer cache buffers.
2763 blksize = hammer_blocksize(ap->a_loffset);
2764 if (hammer_blocksize(base_offset) != blksize) {
2765 base_offset = hammer_blockdemarc(base_offset, ap->a_loffset);
2767 if (last_offset != ap->a_loffset &&
2768 hammer_blocksize(last_offset - 1) != blksize) {
2769 last_offset = hammer_blockdemarc(ap->a_loffset,
2774 * Returning EOPNOTSUPP simply prevents the direct-IO optimization
2777 disk_offset = base_disk_offset + (ap->a_loffset - base_offset);
2779 if ((disk_offset & HAMMER_OFF_ZONE_MASK) != HAMMER_ZONE_LARGE_DATA) {
2781 * Only large-data zones can be direct-IOd
2784 } else if ((disk_offset & HAMMER_BUFMASK) ||
2785 (last_offset - ap->a_loffset) < blksize) {
2787 * doffsetp is not aligned or the forward run size does
2788 * not cover a whole buffer, disallow the direct I/O.
2795 *ap->a_doffsetp = disk_offset;
2797 *ap->a_runb = ap->a_loffset - base_offset;
2798 KKASSERT(*ap->a_runb >= 0);
2801 *ap->a_runp = last_offset - ap->a_loffset;
2802 KKASSERT(*ap->a_runp >= 0);
2810 * Write to a regular file. Because this is a strategy call the OS is
2811 * trying to actually get data onto the media.
2815 hammer_vop_strategy_write(struct vop_strategy_args *ap)
2817 hammer_record_t record;
2828 ip = ap->a_vp->v_data;
2831 blksize = hammer_blocksize(bio->bio_offset);
2832 KKASSERT(bp->b_bufsize == blksize);
2834 if (ip->flags & HAMMER_INODE_RO) {
2835 bp->b_error = EROFS;
2836 bp->b_flags |= B_ERROR;
2842 * Interlock with inode destruction (no in-kernel or directory
2843 * topology visibility). If we queue new IO while trying to
2844 * destroy the inode we can deadlock the vtrunc call in
2845 * hammer_inode_unloadable_check().
2847 * Besides, there's no point flushing a bp associated with an
2848 * inode that is being destroyed on-media and has no kernel
2851 if ((ip->flags | ip->sync_flags) &
2852 (HAMMER_INODE_DELETING|HAMMER_INODE_DELETED)) {
2859 * Reserve space and issue a direct-write from the front-end.
2860 * NOTE: The direct_io code will hammer_bread/bcopy smaller
2863 * An in-memory record will be installed to reference the storage
2864 * until the flusher can get to it.
2866 * Since we own the high level bio the front-end will not try to
2867 * do a direct-read until the write completes.
2869 * NOTE: The only time we do not reserve a full-sized buffers
2870 * worth of data is if the file is small. We do not try to
2871 * allocate a fragment (from the small-data zone) at the end of
2872 * an otherwise large file as this can lead to wildly separated
2875 KKASSERT((bio->bio_offset & HAMMER_BUFMASK) == 0);
2876 KKASSERT(bio->bio_offset < ip->ino_data.size);
2877 if (bio->bio_offset || ip->ino_data.size > HAMMER_BUFSIZE / 2)
2878 bytes = bp->b_bufsize;
2880 bytes = ((int)ip->ino_data.size + 15) & ~15;
2882 record = hammer_ip_add_bulk(ip, bio->bio_offset, bp->b_data,
2885 hammer_io_direct_write(hmp, record, bio);
2886 if (ip->rsv_recs > 1 && hmp->rsv_recs > hammer_limit_recs)
2887 hammer_flush_inode(ip, 0);
2889 bp->b_bio2.bio_offset = NOOFFSET;
2890 bp->b_error = error;
2891 bp->b_flags |= B_ERROR;
2898 * dounlink - disconnect a directory entry
2900 * XXX whiteout support not really in yet
2903 hammer_dounlink(hammer_transaction_t trans, struct nchandle *nch,
2904 struct vnode *dvp, struct ucred *cred,
2905 int flags, int isdir)
2907 struct namecache *ncp;
2910 struct hammer_cursor cursor;
2912 u_int32_t max_iterations;
2916 * Calculate the namekey and setup the key range for the scan. This
2917 * works kinda like a chained hash table where the lower 32 bits
2918 * of the namekey synthesize the chain.
2920 * The key range is inclusive of both key_beg and key_end.
2925 if (dip->flags & HAMMER_INODE_RO)
2928 namekey = hammer_directory_namekey(dip, ncp->nc_name, ncp->nc_nlen,
2931 hammer_init_cursor(trans, &cursor, &dip->cache[1], dip);
2932 cursor.key_beg.localization = dip->obj_localization +
2933 hammer_dir_localization(dip);
2934 cursor.key_beg.obj_id = dip->obj_id;
2935 cursor.key_beg.key = namekey;
2936 cursor.key_beg.create_tid = 0;
2937 cursor.key_beg.delete_tid = 0;
2938 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
2939 cursor.key_beg.obj_type = 0;
2941 cursor.key_end = cursor.key_beg;
2942 cursor.key_end.key += max_iterations;
2943 cursor.asof = dip->obj_asof;
2944 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
2947 * Scan all matching records (the chain), locate the one matching
2948 * the requested path component. info->last_error contains the
2949 * error code on search termination and could be 0, ENOENT, or
2952 * The hammer_ip_*() functions merge in-memory records with on-disk
2953 * records for the purposes of the search.
2955 error = hammer_ip_first(&cursor);
2957 while (error == 0) {
2958 error = hammer_ip_resolve_data(&cursor);
2961 nlen = cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF;
2963 if (ncp->nc_nlen == nlen &&
2964 bcmp(ncp->nc_name, cursor.data->entry.name, nlen) == 0) {
2967 error = hammer_ip_next(&cursor);
2971 * If all is ok we have to get the inode so we can adjust nlinks.
2972 * To avoid a deadlock with the flusher we must release the inode
2973 * lock on the directory when acquiring the inode for the entry.
2975 * If the target is a directory, it must be empty.
2978 hammer_unlock(&cursor.ip->lock);
2979 ip = hammer_get_inode(trans, dip, cursor.data->entry.obj_id,
2981 cursor.data->entry.localization,
2983 hammer_lock_sh(&cursor.ip->lock);
2984 if (error == ENOENT) {
2985 kprintf("HAMMER: WARNING: Removing "
2986 "dirent w/missing inode \"%s\"\n"
2987 "\tobj_id = %016llx\n",
2989 (long long)cursor.data->entry.obj_id);
2994 * If isdir >= 0 we validate that the entry is or is not a
2995 * directory. If isdir < 0 we don't care.
2997 if (error == 0 && isdir >= 0 && ip) {
2999 ip->ino_data.obj_type != HAMMER_OBJTYPE_DIRECTORY) {
3001 } else if (isdir == 0 &&
3002 ip->ino_data.obj_type == HAMMER_OBJTYPE_DIRECTORY) {
3008 * If we are trying to remove a directory the directory must
3011 * The check directory code can loop and deadlock/retry. Our
3012 * own cursor's node locks must be released to avoid a 3-way
3013 * deadlock with the flusher if the check directory code
3016 * If any changes whatsoever have been made to the cursor
3017 * set EDEADLK and retry.
3019 if (error == 0 && ip && ip->ino_data.obj_type ==
3020 HAMMER_OBJTYPE_DIRECTORY) {
3021 hammer_unlock_cursor(&cursor);
3022 error = hammer_ip_check_directory_empty(trans, ip);
3023 hammer_lock_cursor(&cursor);
3024 if (cursor.flags & HAMMER_CURSOR_RETEST) {
3025 kprintf("HAMMER: Warning: avoided deadlock "
3033 * Delete the directory entry.
3035 * WARNING: hammer_ip_del_directory() may have to terminate
3036 * the cursor to avoid a deadlock. It is ok to call
3037 * hammer_done_cursor() twice.
3040 error = hammer_ip_del_directory(trans, &cursor,
3043 hammer_done_cursor(&cursor);
3045 cache_setunresolved(nch);
3046 cache_setvp(nch, NULL);
3049 hammer_knote(ip->vp, NOTE_DELETE);
3050 cache_inval_vp(ip->vp, CINV_DESTROY);
3054 hammer_rel_inode(ip, 0);
3056 hammer_done_cursor(&cursor);
3058 if (error == EDEADLK)
3064 /************************************************************************
3065 * FIFO AND SPECFS OPS *
3066 ************************************************************************
3071 hammer_vop_fifoclose (struct vop_close_args *ap)
3073 /* XXX update itimes */
3074 return (VOCALL(&fifo_vnode_vops, &ap->a_head));
3078 hammer_vop_fiforead (struct vop_read_args *ap)
3082 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
3083 /* XXX update access time */
3088 hammer_vop_fifowrite (struct vop_write_args *ap)
3092 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
3093 /* XXX update access time */
3099 hammer_vop_fifokqfilter(struct vop_kqfilter_args *ap)
3103 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
3105 error = hammer_vop_kqfilter(ap);
3109 /************************************************************************
3111 ************************************************************************
3114 static void filt_hammerdetach(struct knote *kn);
3115 static int filt_hammerread(struct knote *kn, long hint);
3116 static int filt_hammerwrite(struct knote *kn, long hint);
3117 static int filt_hammervnode(struct knote *kn, long hint);
3119 static struct filterops hammerread_filtops =
3120 { 1, NULL, filt_hammerdetach, filt_hammerread };
3121 static struct filterops hammerwrite_filtops =
3122 { 1, NULL, filt_hammerdetach, filt_hammerwrite };
3123 static struct filterops hammervnode_filtops =
3124 { 1, NULL, filt_hammerdetach, filt_hammervnode };
3128 hammer_vop_kqfilter(struct vop_kqfilter_args *ap)
3130 struct vnode *vp = ap->a_vp;
3131 struct knote *kn = ap->a_kn;
3134 switch (kn->kn_filter) {
3136 kn->kn_fop = &hammerread_filtops;
3139 kn->kn_fop = &hammerwrite_filtops;
3142 kn->kn_fop = &hammervnode_filtops;
3148 kn->kn_hook = (caddr_t)vp;
3150 lwkt_gettoken(&vlock, &vp->v_token);
3151 SLIST_INSERT_HEAD(&vp->v_pollinfo.vpi_selinfo.si_note, kn, kn_selnext);
3152 lwkt_reltoken(&vlock);
3158 filt_hammerdetach(struct knote *kn)
3160 struct vnode *vp = (void *)kn->kn_hook;
3163 lwkt_gettoken(&vlock, &vp->v_token);
3164 SLIST_REMOVE(&vp->v_pollinfo.vpi_selinfo.si_note,
3165 kn, knote, kn_selnext);
3166 lwkt_reltoken(&vlock);
3170 filt_hammerread(struct knote *kn, long hint)
3172 struct vnode *vp = (void *)kn->kn_hook;
3173 hammer_inode_t ip = VTOI(vp);
3175 if (hint == NOTE_REVOKE) {
3176 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
3179 kn->kn_data = ip->ino_data.size - kn->kn_fp->f_offset;
3180 return (kn->kn_data != 0);
3184 filt_hammerwrite(struct knote *kn, long hint)
3186 if (hint == NOTE_REVOKE)
3187 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
3193 filt_hammervnode(struct knote *kn, long hint)
3195 if (kn->kn_sfflags & hint)
3196 kn->kn_fflags |= hint;
3197 if (hint == NOTE_REVOKE) {
3198 kn->kn_flags |= EV_EOF;
3201 return (kn->kn_fflags != 0);