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.69 2008/06/13 00:25:33 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>
47 #include <vm/vm_extern.h>
48 #include <vfs/fifofs/fifo.h>
54 /*static int hammer_vop_vnoperate(struct vop_generic_args *);*/
55 static int hammer_vop_fsync(struct vop_fsync_args *);
56 static int hammer_vop_read(struct vop_read_args *);
57 static int hammer_vop_write(struct vop_write_args *);
58 static int hammer_vop_access(struct vop_access_args *);
59 static int hammer_vop_advlock(struct vop_advlock_args *);
60 static int hammer_vop_close(struct vop_close_args *);
61 static int hammer_vop_ncreate(struct vop_ncreate_args *);
62 static int hammer_vop_getattr(struct vop_getattr_args *);
63 static int hammer_vop_nresolve(struct vop_nresolve_args *);
64 static int hammer_vop_nlookupdotdot(struct vop_nlookupdotdot_args *);
65 static int hammer_vop_nlink(struct vop_nlink_args *);
66 static int hammer_vop_nmkdir(struct vop_nmkdir_args *);
67 static int hammer_vop_nmknod(struct vop_nmknod_args *);
68 static int hammer_vop_open(struct vop_open_args *);
69 static int hammer_vop_pathconf(struct vop_pathconf_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_setattr(struct vop_setattr_args *);
77 static int hammer_vop_strategy(struct vop_strategy_args *);
78 static int hammer_vop_bmap(struct vop_bmap_args *ap);
79 static int hammer_vop_nsymlink(struct vop_nsymlink_args *);
80 static int hammer_vop_nwhiteout(struct vop_nwhiteout_args *);
81 static int hammer_vop_ioctl(struct vop_ioctl_args *);
82 static int hammer_vop_mountctl(struct vop_mountctl_args *);
84 static int hammer_vop_fifoclose (struct vop_close_args *);
85 static int hammer_vop_fiforead (struct vop_read_args *);
86 static int hammer_vop_fifowrite (struct vop_write_args *);
88 static int hammer_vop_specclose (struct vop_close_args *);
89 static int hammer_vop_specread (struct vop_read_args *);
90 static int hammer_vop_specwrite (struct vop_write_args *);
92 struct vop_ops hammer_vnode_vops = {
93 .vop_default = vop_defaultop,
94 .vop_fsync = hammer_vop_fsync,
95 .vop_getpages = vop_stdgetpages,
96 .vop_putpages = vop_stdputpages,
97 .vop_read = hammer_vop_read,
98 .vop_write = hammer_vop_write,
99 .vop_access = hammer_vop_access,
100 .vop_advlock = hammer_vop_advlock,
101 .vop_close = hammer_vop_close,
102 .vop_ncreate = hammer_vop_ncreate,
103 .vop_getattr = hammer_vop_getattr,
104 .vop_inactive = hammer_vop_inactive,
105 .vop_reclaim = hammer_vop_reclaim,
106 .vop_nresolve = hammer_vop_nresolve,
107 .vop_nlookupdotdot = hammer_vop_nlookupdotdot,
108 .vop_nlink = hammer_vop_nlink,
109 .vop_nmkdir = hammer_vop_nmkdir,
110 .vop_nmknod = hammer_vop_nmknod,
111 .vop_open = hammer_vop_open,
112 .vop_pathconf = hammer_vop_pathconf,
113 .vop_print = hammer_vop_print,
114 .vop_readdir = hammer_vop_readdir,
115 .vop_readlink = hammer_vop_readlink,
116 .vop_nremove = hammer_vop_nremove,
117 .vop_nrename = hammer_vop_nrename,
118 .vop_nrmdir = hammer_vop_nrmdir,
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
128 struct vop_ops hammer_spec_vops = {
129 .vop_default = spec_vnoperate,
130 .vop_fsync = hammer_vop_fsync,
131 .vop_read = hammer_vop_specread,
132 .vop_write = hammer_vop_specwrite,
133 .vop_access = hammer_vop_access,
134 .vop_close = hammer_vop_specclose,
135 .vop_getattr = hammer_vop_getattr,
136 .vop_inactive = hammer_vop_inactive,
137 .vop_reclaim = hammer_vop_reclaim,
138 .vop_setattr = hammer_vop_setattr
141 struct vop_ops hammer_fifo_vops = {
142 .vop_default = fifo_vnoperate,
143 .vop_fsync = hammer_vop_fsync,
144 .vop_read = hammer_vop_fiforead,
145 .vop_write = hammer_vop_fifowrite,
146 .vop_access = hammer_vop_access,
147 .vop_close = hammer_vop_fifoclose,
148 .vop_getattr = hammer_vop_getattr,
149 .vop_inactive = hammer_vop_inactive,
150 .vop_reclaim = hammer_vop_reclaim,
151 .vop_setattr = hammer_vop_setattr
154 #ifdef DEBUG_TRUNCATE
155 struct hammer_inode *HammerTruncIp;
158 static int hammer_dounlink(hammer_transaction_t trans, struct nchandle *nch,
159 struct vnode *dvp, struct ucred *cred, int flags);
160 static int hammer_vop_strategy_read(struct vop_strategy_args *ap);
161 static int hammer_vop_strategy_write(struct vop_strategy_args *ap);
162 static void hammer_cleanup_write_io(hammer_inode_t ip);
163 static void hammer_update_rsv_databufs(hammer_inode_t ip);
168 hammer_vop_vnoperate(struct vop_generic_args *)
170 return (VOCALL(&hammer_vnode_vops, ap));
175 * hammer_vop_fsync { vp, waitfor }
179 hammer_vop_fsync(struct vop_fsync_args *ap)
181 hammer_inode_t ip = VTOI(ap->a_vp);
183 vfsync(ap->a_vp, ap->a_waitfor, 1, NULL, NULL);
184 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
185 if (ap->a_waitfor == MNT_WAIT)
186 hammer_wait_inode(ip);
191 * hammer_vop_read { vp, uio, ioflag, cred }
195 hammer_vop_read(struct vop_read_args *ap)
197 struct hammer_transaction trans;
206 if (ap->a_vp->v_type != VREG)
210 seqcount = ap->a_ioflag >> 16;
212 hammer_start_transaction(&trans, ip->hmp);
215 * Access the data in HAMMER_BUFSIZE blocks via the buffer cache.
218 while (uio->uio_resid > 0 && uio->uio_offset < ip->ino_data.size) {
219 offset = uio->uio_offset & HAMMER_BUFMASK;
220 if (hammer_debug_cluster_enable) {
221 error = cluster_read(ap->a_vp, ip->ino_data.size,
222 uio->uio_offset - offset,
224 MAXBSIZE, seqcount, &bp);
226 error = bread(ap->a_vp, uio->uio_offset - offset,
227 HAMMER_BUFSIZE, &bp);
234 /* bp->b_flags |= B_CLUSTEROK; temporarily disabled */
235 n = HAMMER_BUFSIZE - offset;
236 if (n > uio->uio_resid)
238 if (n > ip->ino_data.size - uio->uio_offset)
239 n = (int)(ip->ino_data.size - uio->uio_offset);
240 error = uiomove((char *)bp->b_data + offset, n, uio);
242 /* data has a lower priority then meta-data */
243 bp->b_flags |= B_AGE;
248 if ((ip->flags & HAMMER_INODE_RO) == 0 &&
249 (ip->hmp->mp->mnt_flag & MNT_NOATIME) == 0) {
250 ip->ino_leaf.atime = trans.time;
251 hammer_modify_inode(ip, HAMMER_INODE_ITIMES);
253 hammer_done_transaction(&trans);
258 * hammer_vop_write { vp, uio, ioflag, cred }
262 hammer_vop_write(struct vop_write_args *ap)
264 struct hammer_transaction trans;
265 struct hammer_inode *ip;
275 if (ap->a_vp->v_type != VREG)
280 if (ip->flags & HAMMER_INODE_RO)
284 * Create a transaction to cover the operations we perform.
286 hammer_start_transaction(&trans, ip->hmp);
292 if (ap->a_ioflag & IO_APPEND)
293 uio->uio_offset = ip->ino_data.size;
296 * Check for illegal write offsets. Valid range is 0...2^63-1.
298 * NOTE: the base_off assignment is required to work around what
299 * I consider to be a GCC-4 optimization bug.
301 if (uio->uio_offset < 0) {
302 hammer_done_transaction(&trans);
305 base_offset = uio->uio_offset + uio->uio_resid; /* work around gcc-4 */
306 if (uio->uio_resid > 0 && base_offset <= 0) {
307 hammer_done_transaction(&trans);
312 * Access the data in HAMMER_BUFSIZE blocks via the buffer cache.
315 while (uio->uio_resid > 0) {
318 if ((error = hammer_checkspace(trans.hmp)) != 0)
322 * Do not allow HAMMER to blow out the buffer cache.
324 * Do not allow HAMMER to blow out system memory by
325 * accumulating too many records. Records are decoupled
326 * from the buffer cache.
328 * Always check at the beginning so separate writes are
329 * not able to bypass this code.
331 * WARNING: Cannot unlock vp when doing a NOCOPY write as
332 * part of a putpages operation. Doing so could cause us
333 * to deadlock against the VM system when we try to re-lock.
335 if ((count++ & 15) == 0) {
336 if (uio->uio_segflg != UIO_NOCOPY) {
338 if ((ap->a_ioflag & IO_NOBWILL) == 0)
341 if (ip->rsv_recs > hammer_limit_irecs)
342 hammer_wait_inode_recs(ip);
343 if (uio->uio_segflg != UIO_NOCOPY)
344 vn_lock(ap->a_vp, LK_EXCLUSIVE|LK_RETRY);
347 rel_offset = (int)(uio->uio_offset & HAMMER_BUFMASK);
348 base_offset = uio->uio_offset & ~HAMMER_BUFMASK64;
349 n = HAMMER_BUFSIZE - rel_offset;
350 if (n > uio->uio_resid)
352 if (uio->uio_offset + n > ip->ino_data.size) {
353 vnode_pager_setsize(ap->a_vp, uio->uio_offset + n);
357 if (uio->uio_segflg == UIO_NOCOPY) {
359 * Issuing a write with the same data backing the
360 * buffer. Instantiate the buffer to collect the
361 * backing vm pages, then read-in any missing bits.
363 * This case is used by vop_stdputpages().
365 bp = getblk(ap->a_vp, base_offset,
366 HAMMER_BUFSIZE, GETBLK_BHEAVY, 0);
367 if ((bp->b_flags & B_CACHE) == 0) {
369 error = bread(ap->a_vp, base_offset,
370 HAMMER_BUFSIZE, &bp);
372 } else if (rel_offset == 0 && uio->uio_resid >= HAMMER_BUFSIZE) {
374 * Even though we are entirely overwriting the buffer
375 * we may still have to zero it out to avoid a
376 * mmap/write visibility issue.
378 bp = getblk(ap->a_vp, base_offset,
379 HAMMER_BUFSIZE, GETBLK_BHEAVY, 0);
380 if ((bp->b_flags & B_CACHE) == 0)
382 } else if (base_offset >= ip->ino_data.size) {
384 * If the base offset of the buffer is beyond the
385 * file EOF, we don't have to issue a read.
387 bp = getblk(ap->a_vp, base_offset,
388 HAMMER_BUFSIZE, GETBLK_BHEAVY, 0);
392 * Partial overwrite, read in any missing bits then
393 * replace the portion being written.
395 error = bread(ap->a_vp, base_offset,
396 HAMMER_BUFSIZE, &bp);
401 error = uiomove((char *)bp->b_data + rel_offset,
406 * If we screwed up we have to undo any VM size changes we
412 vtruncbuf(ap->a_vp, ip->ino_data.size,
417 /* bp->b_flags |= B_CLUSTEROK; temporarily disabled */
418 if (ip->ino_data.size < uio->uio_offset) {
419 ip->ino_data.size = uio->uio_offset;
420 flags = HAMMER_INODE_DDIRTY;
421 vnode_pager_setsize(ap->a_vp, ip->ino_data.size);
425 ip->ino_data.mtime = trans.time;
426 flags |= HAMMER_INODE_ITIMES | HAMMER_INODE_BUFS;
427 flags |= HAMMER_INODE_DDIRTY; /* XXX mtime */
428 hammer_modify_inode(ip, flags);
431 * Try to keep track of cached dirty data.
433 if ((bp->b_flags & B_DIRTY) == 0) {
435 ++ip->hmp->rsv_databufs;
439 * Final buffer disposition.
441 if (ap->a_ioflag & IO_SYNC) {
443 } else if (ap->a_ioflag & IO_DIRECT) {
446 } else if ((ap->a_ioflag >> 16) == IO_SEQMAX &&
447 (uio->uio_offset & HAMMER_BUFMASK) == 0) {
449 * If seqcount indicates sequential operation and
450 * we just finished filling a buffer, push it out
451 * now to prevent the buffer cache from becoming
452 * too full, which would trigger non-optimal
461 hammer_done_transaction(&trans);
466 * hammer_vop_access { vp, mode, cred }
470 hammer_vop_access(struct vop_access_args *ap)
472 struct hammer_inode *ip = VTOI(ap->a_vp);
477 uid = hammer_to_unix_xid(&ip->ino_data.uid);
478 gid = hammer_to_unix_xid(&ip->ino_data.gid);
480 error = vop_helper_access(ap, uid, gid, ip->ino_data.mode,
481 ip->ino_data.uflags);
486 * hammer_vop_advlock { vp, id, op, fl, flags }
490 hammer_vop_advlock(struct vop_advlock_args *ap)
492 struct hammer_inode *ip = VTOI(ap->a_vp);
494 return (lf_advlock(ap, &ip->advlock, ip->ino_data.size));
498 * hammer_vop_close { vp, fflag }
502 hammer_vop_close(struct vop_close_args *ap)
504 return (vop_stdclose(ap));
508 * hammer_vop_ncreate { nch, dvp, vpp, cred, vap }
510 * The operating system has already ensured that the directory entry
511 * does not exist and done all appropriate namespace locking.
515 hammer_vop_ncreate(struct vop_ncreate_args *ap)
517 struct hammer_transaction trans;
518 struct hammer_inode *dip;
519 struct hammer_inode *nip;
520 struct nchandle *nch;
524 dip = VTOI(ap->a_dvp);
526 if (dip->flags & HAMMER_INODE_RO)
528 if ((error = hammer_checkspace(dip->hmp)) != 0)
532 * Create a transaction to cover the operations we perform.
534 hammer_start_transaction(&trans, dip->hmp);
537 * Create a new filesystem object of the requested type. The
538 * returned inode will be referenced and shared-locked to prevent
539 * it from being moved to the flusher.
542 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred, dip, &nip);
544 hkprintf("hammer_create_inode error %d\n", error);
545 hammer_done_transaction(&trans);
551 * Add the new filesystem object to the directory. This will also
552 * bump the inode's link count.
554 error = hammer_ip_add_directory(&trans, dip, nch->ncp, nip);
556 hkprintf("hammer_ip_add_directory error %d\n", error);
562 hammer_rel_inode(nip, 0);
563 hammer_done_transaction(&trans);
566 error = hammer_get_vnode(nip, ap->a_vpp);
567 hammer_done_transaction(&trans);
568 hammer_rel_inode(nip, 0);
570 cache_setunresolved(ap->a_nch);
571 cache_setvp(ap->a_nch, *ap->a_vpp);
578 * hammer_vop_getattr { vp, vap }
580 * Retrieve an inode's attribute information. When accessing inodes
581 * historically we fake the atime field to ensure consistent results.
582 * The atime field is stored in the B-Tree element and allowed to be
583 * updated without cycling the element.
587 hammer_vop_getattr(struct vop_getattr_args *ap)
589 struct hammer_inode *ip = VTOI(ap->a_vp);
590 struct vattr *vap = ap->a_vap;
593 if (cache_check_fsmid_vp(ap->a_vp, &ip->fsmid) &&
594 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0 &&
599 hammer_itimes(ap->a_vp);
602 vap->va_fsid = ip->hmp->fsid_udev;
603 vap->va_fileid = ip->ino_leaf.base.obj_id;
604 vap->va_mode = ip->ino_data.mode;
605 vap->va_nlink = ip->ino_data.nlinks;
606 vap->va_uid = hammer_to_unix_xid(&ip->ino_data.uid);
607 vap->va_gid = hammer_to_unix_xid(&ip->ino_data.gid);
610 vap->va_size = ip->ino_data.size;
611 if (ip->flags & HAMMER_INODE_RO)
612 hammer_to_timespec(ip->ino_data.mtime, &vap->va_atime);
614 hammer_to_timespec(ip->ino_leaf.atime, &vap->va_atime);
615 hammer_to_timespec(ip->ino_data.mtime, &vap->va_mtime);
616 hammer_to_timespec(ip->ino_data.ctime, &vap->va_ctime);
617 vap->va_flags = ip->ino_data.uflags;
618 vap->va_gen = 1; /* hammer inums are unique for all time */
619 vap->va_blocksize = HAMMER_BUFSIZE;
620 vap->va_bytes = (ip->ino_data.size + 63) & ~63;
621 vap->va_type = hammer_get_vnode_type(ip->ino_data.obj_type);
622 vap->va_filerev = 0; /* XXX */
623 /* mtime uniquely identifies any adjustments made to the file */
624 vap->va_fsmid = ip->ino_data.mtime;
625 vap->va_uid_uuid = ip->ino_data.uid;
626 vap->va_gid_uuid = ip->ino_data.gid;
627 vap->va_fsid_uuid = ip->hmp->fsid;
628 vap->va_vaflags = VA_UID_UUID_VALID | VA_GID_UUID_VALID |
631 switch (ip->ino_data.obj_type) {
632 case HAMMER_OBJTYPE_CDEV:
633 case HAMMER_OBJTYPE_BDEV:
634 vap->va_rmajor = ip->ino_data.rmajor;
635 vap->va_rminor = ip->ino_data.rminor;
645 * hammer_vop_nresolve { nch, dvp, cred }
647 * Locate the requested directory entry.
651 hammer_vop_nresolve(struct vop_nresolve_args *ap)
653 struct hammer_transaction trans;
654 struct namecache *ncp;
658 struct hammer_cursor cursor;
668 * Misc initialization, plus handle as-of name extensions. Look for
669 * the '@@' extension. Note that as-of files and directories cannot
672 dip = VTOI(ap->a_dvp);
673 ncp = ap->a_nch->ncp;
674 asof = dip->obj_asof;
678 hammer_simple_transaction(&trans, dip->hmp);
680 for (i = 0; i < nlen; ++i) {
681 if (ncp->nc_name[i] == '@' && ncp->nc_name[i+1] == '@') {
682 asof = hammer_str_to_tid(ncp->nc_name + i + 2);
683 flags |= HAMMER_INODE_RO;
690 * If there is no path component the time extension is relative to
694 ip = hammer_get_inode(&trans, &dip->cache[1], dip->obj_id,
695 asof, flags, &error);
697 error = hammer_get_vnode(ip, &vp);
698 hammer_rel_inode(ip, 0);
704 cache_setvp(ap->a_nch, vp);
711 * Calculate the namekey and setup the key range for the scan. This
712 * works kinda like a chained hash table where the lower 32 bits
713 * of the namekey synthesize the chain.
715 * The key range is inclusive of both key_beg and key_end.
717 namekey = hammer_directory_namekey(ncp->nc_name, nlen);
719 error = hammer_init_cursor(&trans, &cursor, &dip->cache[0], dip);
720 cursor.key_beg.localization = HAMMER_LOCALIZE_MISC;
721 cursor.key_beg.obj_id = dip->obj_id;
722 cursor.key_beg.key = namekey;
723 cursor.key_beg.create_tid = 0;
724 cursor.key_beg.delete_tid = 0;
725 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
726 cursor.key_beg.obj_type = 0;
728 cursor.key_end = cursor.key_beg;
729 cursor.key_end.key |= 0xFFFFFFFFULL;
731 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
734 * Scan all matching records (the chain), locate the one matching
735 * the requested path component.
737 * The hammer_ip_*() functions merge in-memory records with on-disk
738 * records for the purposes of the search.
743 error = hammer_ip_first(&cursor);
745 error = hammer_ip_resolve_data(&cursor);
748 if (nlen == cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF &&
749 bcmp(ncp->nc_name, cursor.data->entry.name, nlen) == 0) {
750 obj_id = cursor.data->entry.obj_id;
753 error = hammer_ip_next(&cursor);
756 hammer_done_cursor(&cursor);
758 ip = hammer_get_inode(&trans, &dip->cache[1],
759 obj_id, asof, flags, &error);
761 error = hammer_get_vnode(ip, &vp);
762 hammer_rel_inode(ip, 0);
768 cache_setvp(ap->a_nch, vp);
771 } else if (error == ENOENT) {
772 cache_setvp(ap->a_nch, NULL);
775 hammer_done_transaction(&trans);
780 * hammer_vop_nlookupdotdot { dvp, vpp, cred }
782 * Locate the parent directory of a directory vnode.
784 * dvp is referenced but not locked. *vpp must be returned referenced and
785 * locked. A parent_obj_id of 0 does not necessarily indicate that we are
786 * at the root, instead it could indicate that the directory we were in was
789 * NOTE: as-of sequences are not linked into the directory structure. If
790 * we are at the root with a different asof then the mount point, reload
791 * the same directory with the mount point's asof. I'm not sure what this
792 * will do to NFS. We encode ASOF stamps in NFS file handles so it might not
793 * get confused, but it hasn't been tested.
797 hammer_vop_nlookupdotdot(struct vop_nlookupdotdot_args *ap)
799 struct hammer_transaction trans;
800 struct hammer_inode *dip;
801 struct hammer_inode *ip;
802 int64_t parent_obj_id;
806 dip = VTOI(ap->a_dvp);
807 asof = dip->obj_asof;
808 parent_obj_id = dip->ino_data.parent_obj_id;
810 if (parent_obj_id == 0) {
811 if (dip->obj_id == HAMMER_OBJID_ROOT &&
812 asof != dip->hmp->asof) {
813 parent_obj_id = dip->obj_id;
814 asof = dip->hmp->asof;
815 *ap->a_fakename = kmalloc(19, M_TEMP, M_WAITOK);
816 ksnprintf(*ap->a_fakename, 19, "0x%016llx",
824 hammer_simple_transaction(&trans, dip->hmp);
826 ip = hammer_get_inode(&trans, &dip->cache[1], parent_obj_id,
827 asof, dip->flags, &error);
829 error = hammer_get_vnode(ip, ap->a_vpp);
830 hammer_rel_inode(ip, 0);
834 hammer_done_transaction(&trans);
839 * hammer_vop_nlink { nch, dvp, vp, cred }
843 hammer_vop_nlink(struct vop_nlink_args *ap)
845 struct hammer_transaction trans;
846 struct hammer_inode *dip;
847 struct hammer_inode *ip;
848 struct nchandle *nch;
852 dip = VTOI(ap->a_dvp);
855 if (dip->flags & HAMMER_INODE_RO)
857 if (ip->flags & HAMMER_INODE_RO)
859 if ((error = hammer_checkspace(dip->hmp)) != 0)
863 * Create a transaction to cover the operations we perform.
865 hammer_start_transaction(&trans, dip->hmp);
868 * Add the filesystem object to the directory. Note that neither
869 * dip nor ip are referenced or locked, but their vnodes are
870 * referenced. This function will bump the inode's link count.
872 error = hammer_ip_add_directory(&trans, dip, nch->ncp, ip);
878 cache_setunresolved(nch);
879 cache_setvp(nch, ap->a_vp);
881 hammer_done_transaction(&trans);
886 * hammer_vop_nmkdir { nch, dvp, vpp, cred, vap }
888 * The operating system has already ensured that the directory entry
889 * does not exist and done all appropriate namespace locking.
893 hammer_vop_nmkdir(struct vop_nmkdir_args *ap)
895 struct hammer_transaction trans;
896 struct hammer_inode *dip;
897 struct hammer_inode *nip;
898 struct nchandle *nch;
902 dip = VTOI(ap->a_dvp);
904 if (dip->flags & HAMMER_INODE_RO)
906 if ((error = hammer_checkspace(dip->hmp)) != 0)
910 * Create a transaction to cover the operations we perform.
912 hammer_start_transaction(&trans, dip->hmp);
915 * Create a new filesystem object of the requested type. The
916 * returned inode will be referenced but not locked.
918 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred, dip, &nip);
920 hkprintf("hammer_mkdir error %d\n", error);
921 hammer_done_transaction(&trans);
926 * Add the new filesystem object to the directory. This will also
927 * bump the inode's link count.
929 error = hammer_ip_add_directory(&trans, dip, nch->ncp, nip);
931 hkprintf("hammer_mkdir (add) error %d\n", error);
937 hammer_rel_inode(nip, 0);
940 error = hammer_get_vnode(nip, ap->a_vpp);
941 hammer_rel_inode(nip, 0);
943 cache_setunresolved(ap->a_nch);
944 cache_setvp(ap->a_nch, *ap->a_vpp);
947 hammer_done_transaction(&trans);
952 * hammer_vop_nmknod { nch, dvp, vpp, cred, vap }
954 * The operating system has already ensured that the directory entry
955 * does not exist and done all appropriate namespace locking.
959 hammer_vop_nmknod(struct vop_nmknod_args *ap)
961 struct hammer_transaction trans;
962 struct hammer_inode *dip;
963 struct hammer_inode *nip;
964 struct nchandle *nch;
968 dip = VTOI(ap->a_dvp);
970 if (dip->flags & HAMMER_INODE_RO)
972 if ((error = hammer_checkspace(dip->hmp)) != 0)
976 * Create a transaction to cover the operations we perform.
978 hammer_start_transaction(&trans, dip->hmp);
981 * Create a new filesystem object of the requested type. The
982 * returned inode will be referenced but not locked.
984 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred, dip, &nip);
986 hammer_done_transaction(&trans);
992 * Add the new filesystem object to the directory. This will also
993 * bump the inode's link count.
995 error = hammer_ip_add_directory(&trans, dip, nch->ncp, nip);
1001 hammer_rel_inode(nip, 0);
1004 error = hammer_get_vnode(nip, ap->a_vpp);
1005 hammer_rel_inode(nip, 0);
1007 cache_setunresolved(ap->a_nch);
1008 cache_setvp(ap->a_nch, *ap->a_vpp);
1011 hammer_done_transaction(&trans);
1016 * hammer_vop_open { vp, mode, cred, fp }
1020 hammer_vop_open(struct vop_open_args *ap)
1024 ip = VTOI(ap->a_vp);
1026 if ((ap->a_mode & FWRITE) && (ip->flags & HAMMER_INODE_RO))
1028 return(vop_stdopen(ap));
1032 * hammer_vop_pathconf { vp, name, retval }
1036 hammer_vop_pathconf(struct vop_pathconf_args *ap)
1042 * hammer_vop_print { vp }
1046 hammer_vop_print(struct vop_print_args *ap)
1052 * hammer_vop_readdir { vp, uio, cred, *eofflag, *ncookies, off_t **cookies }
1056 hammer_vop_readdir(struct vop_readdir_args *ap)
1058 struct hammer_transaction trans;
1059 struct hammer_cursor cursor;
1060 struct hammer_inode *ip;
1062 hammer_base_elm_t base;
1070 ip = VTOI(ap->a_vp);
1072 saveoff = uio->uio_offset;
1074 if (ap->a_ncookies) {
1075 ncookies = uio->uio_resid / 16 + 1;
1076 if (ncookies > 1024)
1078 cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
1086 hammer_simple_transaction(&trans, ip->hmp);
1089 * Handle artificial entries
1093 r = vop_write_dirent(&error, uio, ip->obj_id, DT_DIR, 1, ".");
1097 cookies[cookie_index] = saveoff;
1100 if (cookie_index == ncookies)
1104 if (ip->ino_data.parent_obj_id) {
1105 r = vop_write_dirent(&error, uio,
1106 ip->ino_data.parent_obj_id,
1109 r = vop_write_dirent(&error, uio,
1110 ip->obj_id, DT_DIR, 2, "..");
1115 cookies[cookie_index] = saveoff;
1118 if (cookie_index == ncookies)
1123 * Key range (begin and end inclusive) to scan. Directory keys
1124 * directly translate to a 64 bit 'seek' position.
1126 hammer_init_cursor(&trans, &cursor, &ip->cache[0], ip);
1127 cursor.key_beg.localization = HAMMER_LOCALIZE_MISC;
1128 cursor.key_beg.obj_id = ip->obj_id;
1129 cursor.key_beg.create_tid = 0;
1130 cursor.key_beg.delete_tid = 0;
1131 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1132 cursor.key_beg.obj_type = 0;
1133 cursor.key_beg.key = saveoff;
1135 cursor.key_end = cursor.key_beg;
1136 cursor.key_end.key = HAMMER_MAX_KEY;
1137 cursor.asof = ip->obj_asof;
1138 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
1140 error = hammer_ip_first(&cursor);
1142 while (error == 0) {
1143 error = hammer_ip_resolve_data(&cursor);
1146 base = &cursor.leaf->base;
1147 saveoff = base->key;
1148 KKASSERT(cursor.leaf->data_len > HAMMER_ENTRY_NAME_OFF);
1150 if (base->obj_id != ip->obj_id)
1151 panic("readdir: bad record at %p", cursor.node);
1153 r = vop_write_dirent(
1154 &error, uio, cursor.data->entry.obj_id,
1155 hammer_get_dtype(cursor.leaf->base.obj_type),
1156 cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF ,
1157 (void *)cursor.data->entry.name);
1162 cookies[cookie_index] = base->key;
1164 if (cookie_index == ncookies)
1166 error = hammer_ip_next(&cursor);
1168 hammer_done_cursor(&cursor);
1171 hammer_done_transaction(&trans);
1174 *ap->a_eofflag = (error == ENOENT);
1175 uio->uio_offset = saveoff;
1176 if (error && cookie_index == 0) {
1177 if (error == ENOENT)
1180 kfree(cookies, M_TEMP);
1181 *ap->a_ncookies = 0;
1182 *ap->a_cookies = NULL;
1185 if (error == ENOENT)
1188 *ap->a_ncookies = cookie_index;
1189 *ap->a_cookies = cookies;
1196 * hammer_vop_readlink { vp, uio, cred }
1200 hammer_vop_readlink(struct vop_readlink_args *ap)
1202 struct hammer_transaction trans;
1203 struct hammer_cursor cursor;
1204 struct hammer_inode *ip;
1207 ip = VTOI(ap->a_vp);
1210 * Shortcut if the symlink data was stuffed into ino_data.
1212 if (ip->ino_data.size <= HAMMER_INODE_BASESYMLEN) {
1213 error = uiomove(ip->ino_data.ext.symlink,
1214 ip->ino_data.size, ap->a_uio);
1221 hammer_simple_transaction(&trans, ip->hmp);
1222 hammer_init_cursor(&trans, &cursor, &ip->cache[0], ip);
1225 * Key range (begin and end inclusive) to scan. Directory keys
1226 * directly translate to a 64 bit 'seek' position.
1228 cursor.key_beg.localization = HAMMER_LOCALIZE_MISC; /* XXX */
1229 cursor.key_beg.obj_id = ip->obj_id;
1230 cursor.key_beg.create_tid = 0;
1231 cursor.key_beg.delete_tid = 0;
1232 cursor.key_beg.rec_type = HAMMER_RECTYPE_FIX;
1233 cursor.key_beg.obj_type = 0;
1234 cursor.key_beg.key = HAMMER_FIXKEY_SYMLINK;
1235 cursor.asof = ip->obj_asof;
1236 cursor.flags |= HAMMER_CURSOR_ASOF;
1238 error = hammer_ip_lookup(&cursor);
1240 error = hammer_ip_resolve_data(&cursor);
1242 KKASSERT(cursor.leaf->data_len >=
1243 HAMMER_SYMLINK_NAME_OFF);
1244 error = uiomove(cursor.data->symlink.name,
1245 cursor.leaf->data_len -
1246 HAMMER_SYMLINK_NAME_OFF,
1250 hammer_done_cursor(&cursor);
1251 hammer_done_transaction(&trans);
1256 * hammer_vop_nremove { nch, dvp, cred }
1260 hammer_vop_nremove(struct vop_nremove_args *ap)
1262 struct hammer_transaction trans;
1263 struct hammer_inode *dip;
1266 dip = VTOI(ap->a_dvp);
1268 if (hammer_nohistory(dip) == 0 &&
1269 (error = hammer_checkspace(dip->hmp)) != 0) {
1273 hammer_start_transaction(&trans, dip->hmp);
1274 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp, ap->a_cred, 0);
1275 hammer_done_transaction(&trans);
1281 * hammer_vop_nrename { fnch, tnch, fdvp, tdvp, cred }
1285 hammer_vop_nrename(struct vop_nrename_args *ap)
1287 struct hammer_transaction trans;
1288 struct namecache *fncp;
1289 struct namecache *tncp;
1290 struct hammer_inode *fdip;
1291 struct hammer_inode *tdip;
1292 struct hammer_inode *ip;
1293 struct hammer_cursor cursor;
1297 fdip = VTOI(ap->a_fdvp);
1298 tdip = VTOI(ap->a_tdvp);
1299 fncp = ap->a_fnch->ncp;
1300 tncp = ap->a_tnch->ncp;
1301 ip = VTOI(fncp->nc_vp);
1302 KKASSERT(ip != NULL);
1304 if (fdip->flags & HAMMER_INODE_RO)
1306 if (tdip->flags & HAMMER_INODE_RO)
1308 if (ip->flags & HAMMER_INODE_RO)
1310 if ((error = hammer_checkspace(fdip->hmp)) != 0)
1313 hammer_start_transaction(&trans, fdip->hmp);
1316 * Remove tncp from the target directory and then link ip as
1317 * tncp. XXX pass trans to dounlink
1319 * Force the inode sync-time to match the transaction so it is
1320 * in-sync with the creation of the target directory entry.
1322 error = hammer_dounlink(&trans, ap->a_tnch, ap->a_tdvp, ap->a_cred, 0);
1323 if (error == 0 || error == ENOENT) {
1324 error = hammer_ip_add_directory(&trans, tdip, tncp, ip);
1326 ip->ino_data.parent_obj_id = tdip->obj_id;
1327 hammer_modify_inode(ip, HAMMER_INODE_DDIRTY);
1331 goto failed; /* XXX */
1334 * Locate the record in the originating directory and remove it.
1336 * Calculate the namekey and setup the key range for the scan. This
1337 * works kinda like a chained hash table where the lower 32 bits
1338 * of the namekey synthesize the chain.
1340 * The key range is inclusive of both key_beg and key_end.
1342 namekey = hammer_directory_namekey(fncp->nc_name, fncp->nc_nlen);
1344 hammer_init_cursor(&trans, &cursor, &fdip->cache[0], fdip);
1345 cursor.key_beg.localization = HAMMER_LOCALIZE_MISC;
1346 cursor.key_beg.obj_id = fdip->obj_id;
1347 cursor.key_beg.key = namekey;
1348 cursor.key_beg.create_tid = 0;
1349 cursor.key_beg.delete_tid = 0;
1350 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1351 cursor.key_beg.obj_type = 0;
1353 cursor.key_end = cursor.key_beg;
1354 cursor.key_end.key |= 0xFFFFFFFFULL;
1355 cursor.asof = fdip->obj_asof;
1356 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
1359 * Scan all matching records (the chain), locate the one matching
1360 * the requested path component.
1362 * The hammer_ip_*() functions merge in-memory records with on-disk
1363 * records for the purposes of the search.
1365 error = hammer_ip_first(&cursor);
1366 while (error == 0) {
1367 if (hammer_ip_resolve_data(&cursor) != 0)
1369 nlen = cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF;
1371 if (fncp->nc_nlen == nlen &&
1372 bcmp(fncp->nc_name, cursor.data->entry.name, nlen) == 0) {
1375 error = hammer_ip_next(&cursor);
1379 * If all is ok we have to get the inode so we can adjust nlinks.
1381 * WARNING: hammer_ip_del_directory() may have to terminate the
1382 * cursor to avoid a recursion. It's ok to call hammer_done_cursor()
1386 error = hammer_ip_del_directory(&trans, &cursor, fdip, ip);
1389 * XXX A deadlock here will break rename's atomicy for the purposes
1390 * of crash recovery.
1392 if (error == EDEADLK) {
1393 hammer_done_cursor(&cursor);
1398 * Cleanup and tell the kernel that the rename succeeded.
1400 hammer_done_cursor(&cursor);
1402 cache_rename(ap->a_fnch, ap->a_tnch);
1405 hammer_done_transaction(&trans);
1410 * hammer_vop_nrmdir { nch, dvp, cred }
1414 hammer_vop_nrmdir(struct vop_nrmdir_args *ap)
1416 struct hammer_transaction trans;
1417 struct hammer_inode *dip;
1420 dip = VTOI(ap->a_dvp);
1422 if (hammer_nohistory(dip) == 0 &&
1423 (error = hammer_checkspace(dip->hmp)) != 0) {
1427 hammer_start_transaction(&trans, dip->hmp);
1428 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp, ap->a_cred, 0);
1429 hammer_done_transaction(&trans);
1435 * hammer_vop_setattr { vp, vap, cred }
1439 hammer_vop_setattr(struct vop_setattr_args *ap)
1441 struct hammer_transaction trans;
1443 struct hammer_inode *ip;
1451 ip = ap->a_vp->v_data;
1454 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
1456 if (ip->flags & HAMMER_INODE_RO)
1458 if (hammer_nohistory(ip) == 0 &&
1459 (error = hammer_checkspace(ip->hmp)) != 0) {
1463 hammer_start_transaction(&trans, ip->hmp);
1466 if (vap->va_flags != VNOVAL) {
1467 flags = ip->ino_data.uflags;
1468 error = vop_helper_setattr_flags(&flags, vap->va_flags,
1469 hammer_to_unix_xid(&ip->ino_data.uid),
1472 if (ip->ino_data.uflags != flags) {
1473 ip->ino_data.uflags = flags;
1474 modflags |= HAMMER_INODE_DDIRTY;
1476 if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) {
1483 if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) {
1487 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
1488 mode_t cur_mode = ip->ino_data.mode;
1489 uid_t cur_uid = hammer_to_unix_xid(&ip->ino_data.uid);
1490 gid_t cur_gid = hammer_to_unix_xid(&ip->ino_data.gid);
1494 error = vop_helper_chown(ap->a_vp, vap->va_uid, vap->va_gid,
1496 &cur_uid, &cur_gid, &cur_mode);
1498 hammer_guid_to_uuid(&uuid_uid, cur_uid);
1499 hammer_guid_to_uuid(&uuid_gid, cur_gid);
1500 if (bcmp(&uuid_uid, &ip->ino_data.uid,
1501 sizeof(uuid_uid)) ||
1502 bcmp(&uuid_gid, &ip->ino_data.gid,
1503 sizeof(uuid_gid)) ||
1504 ip->ino_data.mode != cur_mode
1506 ip->ino_data.uid = uuid_uid;
1507 ip->ino_data.gid = uuid_gid;
1508 ip->ino_data.mode = cur_mode;
1510 modflags |= HAMMER_INODE_DDIRTY;
1513 while (vap->va_size != VNOVAL && ip->ino_data.size != vap->va_size) {
1514 switch(ap->a_vp->v_type) {
1516 if (vap->va_size == ip->ino_data.size)
1519 * XXX break atomicy, we can deadlock the backend
1520 * if we do not release the lock. Probably not a
1523 if (vap->va_size < ip->ino_data.size) {
1524 vtruncbuf(ap->a_vp, vap->va_size,
1528 vnode_pager_setsize(ap->a_vp, vap->va_size);
1531 ip->ino_data.size = vap->va_size;
1532 modflags |= HAMMER_INODE_DDIRTY;
1533 aligned_size = (vap->va_size + HAMMER_BUFMASK) &
1537 * on-media truncation is cached in the inode until
1538 * the inode is synchronized.
1541 hammer_ip_frontend_trunc(ip, vap->va_size);
1542 hammer_update_rsv_databufs(ip);
1543 #ifdef DEBUG_TRUNCATE
1544 if (HammerTruncIp == NULL)
1547 if ((ip->flags & HAMMER_INODE_TRUNCATED) == 0) {
1548 ip->flags |= HAMMER_INODE_TRUNCATED;
1549 ip->trunc_off = vap->va_size;
1550 #ifdef DEBUG_TRUNCATE
1551 if (ip == HammerTruncIp)
1552 kprintf("truncate1 %016llx\n", ip->trunc_off);
1554 } else if (ip->trunc_off > vap->va_size) {
1555 ip->trunc_off = vap->va_size;
1556 #ifdef DEBUG_TRUNCATE
1557 if (ip == HammerTruncIp)
1558 kprintf("truncate2 %016llx\n", ip->trunc_off);
1561 #ifdef DEBUG_TRUNCATE
1562 if (ip == HammerTruncIp)
1563 kprintf("truncate3 %016llx (ignored)\n", vap->va_size);
1569 * If truncating we have to clean out a portion of
1570 * the last block on-disk. We do this in the
1571 * front-end buffer cache.
1573 if (truncating && vap->va_size < aligned_size) {
1577 aligned_size -= HAMMER_BUFSIZE;
1579 offset = vap->va_size & HAMMER_BUFMASK;
1580 error = bread(ap->a_vp, aligned_size,
1581 HAMMER_BUFSIZE, &bp);
1582 hammer_ip_frontend_trunc(ip, aligned_size);
1584 bzero(bp->b_data + offset,
1585 HAMMER_BUFSIZE - offset);
1588 kprintf("ERROR %d\n", error);
1594 if ((ip->flags & HAMMER_INODE_TRUNCATED) == 0) {
1595 ip->flags |= HAMMER_INODE_TRUNCATED;
1596 ip->trunc_off = vap->va_size;
1597 } else if (ip->trunc_off > vap->va_size) {
1598 ip->trunc_off = vap->va_size;
1600 hammer_ip_frontend_trunc(ip, vap->va_size);
1601 ip->ino_data.size = vap->va_size;
1602 modflags |= HAMMER_INODE_DDIRTY;
1610 if (vap->va_atime.tv_sec != VNOVAL) {
1611 ip->ino_leaf.atime =
1612 hammer_timespec_to_transid(&vap->va_atime);
1613 modflags |= HAMMER_INODE_ITIMES;
1615 if (vap->va_mtime.tv_sec != VNOVAL) {
1616 ip->ino_data.mtime =
1617 hammer_timespec_to_transid(&vap->va_mtime);
1618 modflags |= HAMMER_INODE_ITIMES;
1619 modflags |= HAMMER_INODE_DDIRTY; /* XXX mtime */
1621 if (vap->va_mode != (mode_t)VNOVAL) {
1622 mode_t cur_mode = ip->ino_data.mode;
1623 uid_t cur_uid = hammer_to_unix_xid(&ip->ino_data.uid);
1624 gid_t cur_gid = hammer_to_unix_xid(&ip->ino_data.gid);
1626 error = vop_helper_chmod(ap->a_vp, vap->va_mode, ap->a_cred,
1627 cur_uid, cur_gid, &cur_mode);
1628 if (error == 0 && ip->ino_data.mode != cur_mode) {
1629 ip->ino_data.mode = cur_mode;
1630 modflags |= HAMMER_INODE_DDIRTY;
1635 hammer_modify_inode(ip, modflags);
1636 hammer_done_transaction(&trans);
1641 * hammer_vop_nsymlink { nch, dvp, vpp, cred, vap, target }
1645 hammer_vop_nsymlink(struct vop_nsymlink_args *ap)
1647 struct hammer_transaction trans;
1648 struct hammer_inode *dip;
1649 struct hammer_inode *nip;
1650 struct nchandle *nch;
1651 hammer_record_t record;
1655 ap->a_vap->va_type = VLNK;
1658 dip = VTOI(ap->a_dvp);
1660 if (dip->flags & HAMMER_INODE_RO)
1662 if ((error = hammer_checkspace(dip->hmp)) != 0)
1666 * Create a transaction to cover the operations we perform.
1668 hammer_start_transaction(&trans, dip->hmp);
1671 * Create a new filesystem object of the requested type. The
1672 * returned inode will be referenced but not locked.
1675 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred, dip, &nip);
1677 hammer_done_transaction(&trans);
1683 * Add a record representing the symlink. symlink stores the link
1684 * as pure data, not a string, and is no \0 terminated.
1687 bytes = strlen(ap->a_target);
1689 if (bytes <= HAMMER_INODE_BASESYMLEN) {
1690 bcopy(ap->a_target, nip->ino_data.ext.symlink, bytes);
1692 record = hammer_alloc_mem_record(nip, bytes);
1693 record->type = HAMMER_MEM_RECORD_GENERAL;
1695 record->leaf.base.localization = HAMMER_LOCALIZE_MISC;
1696 record->leaf.base.key = HAMMER_FIXKEY_SYMLINK;
1697 record->leaf.base.rec_type = HAMMER_RECTYPE_FIX;
1698 record->leaf.data_len = bytes;
1699 KKASSERT(HAMMER_SYMLINK_NAME_OFF == 0);
1700 bcopy(ap->a_target, record->data->symlink.name, bytes);
1701 error = hammer_ip_add_record(&trans, record);
1705 * Set the file size to the length of the link.
1708 nip->ino_data.size = bytes;
1709 hammer_modify_inode(nip, HAMMER_INODE_DDIRTY);
1713 error = hammer_ip_add_directory(&trans, dip, nch->ncp, nip);
1719 hammer_rel_inode(nip, 0);
1722 error = hammer_get_vnode(nip, ap->a_vpp);
1723 hammer_rel_inode(nip, 0);
1725 cache_setunresolved(ap->a_nch);
1726 cache_setvp(ap->a_nch, *ap->a_vpp);
1729 hammer_done_transaction(&trans);
1734 * hammer_vop_nwhiteout { nch, dvp, cred, flags }
1738 hammer_vop_nwhiteout(struct vop_nwhiteout_args *ap)
1740 struct hammer_transaction trans;
1741 struct hammer_inode *dip;
1744 dip = VTOI(ap->a_dvp);
1746 if (hammer_nohistory(dip) == 0 &&
1747 (error = hammer_checkspace(dip->hmp)) != 0) {
1751 hammer_start_transaction(&trans, dip->hmp);
1752 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp,
1753 ap->a_cred, ap->a_flags);
1754 hammer_done_transaction(&trans);
1760 * hammer_vop_ioctl { vp, command, data, fflag, cred }
1764 hammer_vop_ioctl(struct vop_ioctl_args *ap)
1766 struct hammer_inode *ip = ap->a_vp->v_data;
1768 return(hammer_ioctl(ip, ap->a_command, ap->a_data,
1769 ap->a_fflag, ap->a_cred));
1774 hammer_vop_mountctl(struct vop_mountctl_args *ap)
1779 mp = ap->a_head.a_ops->head.vv_mount;
1782 case MOUNTCTL_SET_EXPORT:
1783 if (ap->a_ctllen != sizeof(struct export_args))
1785 error = hammer_vfs_export(mp, ap->a_op,
1786 (const struct export_args *)ap->a_ctl);
1789 error = journal_mountctl(ap);
1796 * hammer_vop_strategy { vp, bio }
1798 * Strategy call, used for regular file read & write only. Note that the
1799 * bp may represent a cluster.
1801 * To simplify operation and allow better optimizations in the future,
1802 * this code does not make any assumptions with regards to buffer alignment
1807 hammer_vop_strategy(struct vop_strategy_args *ap)
1812 bp = ap->a_bio->bio_buf;
1816 error = hammer_vop_strategy_read(ap);
1819 error = hammer_vop_strategy_write(ap);
1822 bp->b_error = error = EINVAL;
1823 bp->b_flags |= B_ERROR;
1831 * Read from a regular file. Iterate the related records and fill in the
1832 * BIO/BUF. Gaps are zero-filled.
1834 * The support code in hammer_object.c should be used to deal with mixed
1835 * in-memory and on-disk records.
1841 hammer_vop_strategy_read(struct vop_strategy_args *ap)
1843 struct hammer_transaction trans;
1844 struct hammer_inode *ip;
1845 struct hammer_cursor cursor;
1846 hammer_base_elm_t base;
1860 ip = ap->a_vp->v_data;
1863 * The zone-2 disk offset may have been set by the cluster code via
1864 * a BMAP operation. Take care not to confuse it with the bio_offset
1865 * set by hammer_io_direct_write(), which is a device-relative offset.
1867 * Checking the high bits should suffice.
1869 nbio = push_bio(bio);
1870 if ((nbio->bio_offset & HAMMER_OFF_ZONE_MASK) ==
1871 HAMMER_ZONE_RAW_BUFFER) {
1872 error = hammer_io_direct_read(ip->hmp, nbio->bio_offset, bio);
1879 hammer_simple_transaction(&trans, ip->hmp);
1880 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
1883 * Key range (begin and end inclusive) to scan. Note that the key's
1884 * stored in the actual records represent BASE+LEN, not BASE. The
1885 * first record containing bio_offset will have a key > bio_offset.
1887 cursor.key_beg.localization = HAMMER_LOCALIZE_MISC;
1888 cursor.key_beg.obj_id = ip->obj_id;
1889 cursor.key_beg.create_tid = 0;
1890 cursor.key_beg.delete_tid = 0;
1891 cursor.key_beg.obj_type = 0;
1892 cursor.key_beg.key = bio->bio_offset + 1;
1893 cursor.asof = ip->obj_asof;
1894 cursor.flags |= HAMMER_CURSOR_ASOF | HAMMER_CURSOR_DATAEXTOK;
1896 cursor.key_end = cursor.key_beg;
1897 KKASSERT(ip->ino_data.obj_type == HAMMER_OBJTYPE_REGFILE);
1899 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_DBFILE) {
1900 cursor.key_beg.rec_type = HAMMER_RECTYPE_DB;
1901 cursor.key_end.rec_type = HAMMER_RECTYPE_DB;
1902 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
1906 ran_end = bio->bio_offset + bp->b_bufsize;
1907 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
1908 cursor.key_end.rec_type = HAMMER_RECTYPE_DATA;
1909 tmp64 = ran_end + MAXPHYS + 1; /* work-around GCC-4 bug */
1910 if (tmp64 < ran_end)
1911 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
1913 cursor.key_end.key = ran_end + MAXPHYS + 1;
1915 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
1917 error = hammer_ip_first(&cursor);
1920 while (error == 0) {
1922 * Get the base file offset of the record. The key for
1923 * data records is (base + bytes) rather then (base).
1925 base = &cursor.leaf->base;
1926 rec_offset = base->key - cursor.leaf->data_len;
1929 * Calculate the gap, if any, and zero-fill it.
1931 * n is the offset of the start of the record verses our
1932 * current seek offset in the bio.
1934 n = (int)(rec_offset - (bio->bio_offset + boff));
1936 if (n > bp->b_bufsize - boff)
1937 n = bp->b_bufsize - boff;
1938 bzero((char *)bp->b_data + boff, n);
1944 * Calculate the data offset in the record and the number
1945 * of bytes we can copy.
1947 * There are two degenerate cases. First, boff may already
1948 * be at bp->b_bufsize. Secondly, the data offset within
1949 * the record may exceed the record's size.
1953 n = cursor.leaf->data_len - roff;
1955 kprintf("strategy_read: bad n=%d roff=%d\n", n, roff);
1957 } else if (n > bp->b_bufsize - boff) {
1958 n = bp->b_bufsize - boff;
1962 * Deal with cached truncations. This cool bit of code
1963 * allows truncate()/ftruncate() to avoid having to sync
1966 * If the frontend is truncated then all backend records are
1967 * subject to the frontend's truncation.
1969 * If the backend is truncated then backend records on-disk
1970 * (but not in-memory) are subject to the backend's
1971 * truncation. In-memory records owned by the backend
1972 * represent data written after the truncation point on the
1973 * backend and must not be truncated.
1975 * Truncate operations deal with frontend buffer cache
1976 * buffers and frontend-owned in-memory records synchronously.
1978 if (ip->flags & HAMMER_INODE_TRUNCATED) {
1979 if (hammer_cursor_ondisk(&cursor) ||
1980 cursor.iprec->flush_state == HAMMER_FST_FLUSH) {
1981 if (ip->trunc_off <= rec_offset)
1983 else if (ip->trunc_off < rec_offset + n)
1984 n = (int)(ip->trunc_off - rec_offset);
1987 if (ip->sync_flags & HAMMER_INODE_TRUNCATED) {
1988 if (hammer_cursor_ondisk(&cursor)) {
1989 if (ip->sync_trunc_off <= rec_offset)
1991 else if (ip->sync_trunc_off < rec_offset + n)
1992 n = (int)(ip->sync_trunc_off - rec_offset);
1997 * Try to issue a direct read into our bio if possible,
1998 * otherwise resolve the element data into a hammer_buffer
2001 if (n && boff == 0 &&
2002 ((cursor.leaf->data_offset + roff) & HAMMER_BUFMASK) == 0) {
2003 error = hammer_io_direct_read(
2005 cursor.leaf->data_offset + roff,
2009 error = hammer_ip_resolve_data(&cursor);
2011 bcopy((char *)cursor.data + roff,
2012 (char *)bp->b_data + boff, n);
2019 * Iterate until we have filled the request.
2022 if (boff == bp->b_bufsize)
2024 error = hammer_ip_next(&cursor);
2028 * There may have been a gap after the last record
2030 if (error == ENOENT)
2032 if (error == 0 && boff != bp->b_bufsize) {
2033 KKASSERT(boff < bp->b_bufsize);
2034 bzero((char *)bp->b_data + boff, bp->b_bufsize - boff);
2035 /* boff = bp->b_bufsize; */
2038 bp->b_error = error;
2040 bp->b_flags |= B_ERROR;
2045 hammer_cache_node(cursor.node, &ip->cache[1]);
2046 hammer_done_cursor(&cursor);
2047 hammer_done_transaction(&trans);
2052 * BMAP operation - used to support cluster_read() only.
2054 * (struct vnode *vp, off_t loffset, off_t *doffsetp, int *runp, int *runb)
2056 * This routine may return EOPNOTSUPP if the opration is not supported for
2057 * the specified offset. The contents of the pointer arguments do not
2058 * need to be initialized in that case.
2060 * If a disk address is available and properly aligned return 0 with
2061 * *doffsetp set to the zone-2 address, and *runp / *runb set appropriately
2062 * to the run-length relative to that offset. Callers may assume that
2063 * *doffsetp is valid if 0 is returned, even if *runp is not sufficiently
2064 * large, so return EOPNOTSUPP if it is not sufficiently large.
2068 hammer_vop_bmap(struct vop_bmap_args *ap)
2070 struct hammer_transaction trans;
2071 struct hammer_inode *ip;
2072 struct hammer_cursor cursor;
2073 hammer_base_elm_t base;
2077 int64_t base_offset;
2078 int64_t base_disk_offset;
2079 int64_t last_offset;
2080 hammer_off_t last_disk_offset;
2081 hammer_off_t disk_offset;
2085 ip = ap->a_vp->v_data;
2088 * We can only BMAP regular files. We can't BMAP database files,
2091 if (ip->ino_data.obj_type != HAMMER_OBJTYPE_REGFILE)
2095 * bmap is typically called with runp/runb both NULL when used
2096 * for writing. We do not support BMAP for writing atm.
2098 if (ap->a_runp == NULL && ap->a_runb == NULL)
2102 * Scan the B-Tree to acquire blockmap addresses, then translate
2105 hammer_simple_transaction(&trans, ip->hmp);
2106 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
2109 * Key range (begin and end inclusive) to scan. Note that the key's
2110 * stored in the actual records represent BASE+LEN, not BASE. The
2111 * first record containing bio_offset will have a key > bio_offset.
2113 cursor.key_beg.localization = HAMMER_LOCALIZE_MISC;
2114 cursor.key_beg.obj_id = ip->obj_id;
2115 cursor.key_beg.create_tid = 0;
2116 cursor.key_beg.delete_tid = 0;
2117 cursor.key_beg.obj_type = 0;
2119 cursor.key_beg.key = ap->a_loffset - MAXPHYS + 1;
2121 cursor.key_beg.key = ap->a_loffset + 1;
2122 if (cursor.key_beg.key < 0)
2123 cursor.key_beg.key = 0;
2124 cursor.asof = ip->obj_asof;
2125 cursor.flags |= HAMMER_CURSOR_ASOF | HAMMER_CURSOR_DATAEXTOK;
2127 cursor.key_end = cursor.key_beg;
2128 KKASSERT(ip->ino_data.obj_type == HAMMER_OBJTYPE_REGFILE);
2130 ran_end = ap->a_loffset + MAXPHYS;
2131 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
2132 cursor.key_end.rec_type = HAMMER_RECTYPE_DATA;
2133 tmp64 = ran_end + MAXPHYS + 1; /* work-around GCC-4 bug */
2134 if (tmp64 < ran_end)
2135 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
2137 cursor.key_end.key = ran_end + MAXPHYS + 1;
2139 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
2141 error = hammer_ip_first(&cursor);
2142 base_offset = last_offset = 0;
2143 base_disk_offset = last_disk_offset = 0;
2145 while (error == 0) {
2147 * Get the base file offset of the record. The key for
2148 * data records is (base + bytes) rather then (base).
2150 base = &cursor.leaf->base;
2151 rec_offset = base->key - cursor.leaf->data_len;
2152 rec_len = cursor.leaf->data_len;
2155 * Incorporate any cached truncation
2157 if (ip->flags & HAMMER_INODE_TRUNCATED) {
2158 if (hammer_cursor_ondisk(&cursor) ||
2159 cursor.iprec->flush_state == HAMMER_FST_FLUSH) {
2160 if (ip->trunc_off <= rec_offset)
2162 else if (ip->trunc_off < rec_offset + rec_len)
2163 rec_len = (int)(ip->trunc_off - rec_offset);
2166 if (ip->sync_flags & HAMMER_INODE_TRUNCATED) {
2167 if (hammer_cursor_ondisk(&cursor)) {
2168 if (ip->sync_trunc_off <= rec_offset)
2170 else if (ip->sync_trunc_off < rec_offset + rec_len)
2171 rec_len = (int)(ip->sync_trunc_off - rec_offset);
2176 * Accumulate information. If we have hit a discontiguous
2177 * block reset base_offset unless we are already beyond the
2178 * requested offset. If we are, that's it, we stop.
2180 disk_offset = hammer_blockmap_lookup(trans.hmp,
2181 cursor.leaf->data_offset,
2185 if (rec_offset != last_offset ||
2186 disk_offset != last_disk_offset) {
2187 if (rec_offset > ap->a_loffset)
2189 base_offset = rec_offset;
2190 base_disk_offset = disk_offset;
2192 last_offset = rec_offset + rec_len;
2193 last_disk_offset = disk_offset + rec_len;
2195 error = hammer_ip_next(&cursor);
2199 kprintf("BMAP %016llx: %016llx - %016llx\n",
2200 ap->a_loffset, base_offset, last_offset);
2201 kprintf("BMAP %16s: %016llx - %016llx\n",
2202 "", base_disk_offset, last_disk_offset);
2206 hammer_cache_node(cursor.node, &ip->cache[1]);
2207 hammer_done_cursor(&cursor);
2208 hammer_done_transaction(&trans);
2210 if (base_offset == 0 || base_offset > ap->a_loffset ||
2211 last_offset < ap->a_loffset) {
2214 disk_offset = base_disk_offset + (ap->a_loffset - base_offset);
2217 * If doffsetp is not aligned or the forward run size does
2218 * not cover a whole buffer, disallow the direct I/O.
2220 if ((disk_offset & HAMMER_BUFMASK) ||
2221 (last_offset - ap->a_loffset) < HAMMER_BUFSIZE) {
2224 *ap->a_doffsetp = disk_offset;
2226 *ap->a_runb = ap->a_loffset - base_offset;
2228 *ap->a_runp = last_offset - ap->a_loffset;
2236 * Write to a regular file. Because this is a strategy call the OS is
2237 * trying to actually sync data to the media. HAMMER can only flush
2238 * the entire inode (so the TID remains properly synchronized).
2240 * Basically all we do here is place the bio on the inode's flush queue
2241 * and activate the flusher.
2245 hammer_vop_strategy_write(struct vop_strategy_args *ap)
2247 hammer_record_t record;
2257 ip = ap->a_vp->v_data;
2260 if (ip->flags & HAMMER_INODE_RO) {
2261 bp->b_error = EROFS;
2262 bp->b_flags |= B_ERROR;
2264 hammer_cleanup_write_io(ip);
2269 * Interlock with inode destruction (no in-kernel or directory
2270 * topology visibility). If we queue new IO while trying to
2271 * destroy the inode we can deadlock the vtrunc call in
2272 * hammer_inode_unloadable_check().
2274 if (ip->flags & (HAMMER_INODE_DELETING|HAMMER_INODE_DELETED)) {
2277 hammer_cleanup_write_io(ip);
2282 * Reserve space and issue a direct-write from the front-end.
2283 * NOTE: The direct_io code will hammer_bread/bcopy smaller
2286 * An in-memory record will be installed to reference the storage
2287 * until the flusher can get to it.
2289 * Since we own the high level bio the front-end will not try to
2290 * do a direct-read until the write completes.
2292 * NOTE: The only time we do not reserve a full-sized buffers
2293 * worth of data is if the file is small. We do not try to
2294 * allocate a fragment (from the small-data zone) at the end of
2295 * an otherwise large file as this can lead to wildly separated
2298 KKASSERT((bio->bio_offset & HAMMER_BUFMASK) == 0);
2299 KKASSERT(bio->bio_offset < ip->ino_data.size);
2300 if (bio->bio_offset || ip->ino_data.size > HAMMER_BUFSIZE / 2)
2301 bytes = (bp->b_bufsize + HAMMER_BUFMASK) & ~HAMMER_BUFMASK;
2303 bytes = ((int)ip->ino_data.size + 15) & ~15;
2305 record = hammer_ip_add_bulk(ip, bio->bio_offset, bp->b_data,
2308 hammer_io_direct_write(hmp, &record->leaf, bio);
2309 hammer_rel_mem_record(record);
2310 if (hmp->rsv_recs > hammer_limit_recs &&
2311 ip->rsv_recs > hammer_limit_irecs / 10) {
2312 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
2313 } else if (ip->rsv_recs > hammer_limit_irecs / 2) {
2314 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
2317 bp->b_bio2.bio_offset = NOOFFSET;
2318 bp->b_error = error;
2319 bp->b_flags |= B_ERROR;
2322 hammer_cleanup_write_io(ip);
2327 * Clean-up after disposing of a dirty frontend buffer's data.
2328 * This is somewhat heuristical so try to be robust.
2331 hammer_cleanup_write_io(hammer_inode_t ip)
2333 if (ip->rsv_databufs) {
2335 --ip->hmp->rsv_databufs;
2340 * We can lose track of dirty buffer cache buffers if we truncate, this
2341 * routine will resynchronize the count.
2345 hammer_update_rsv_databufs(hammer_inode_t ip)
2353 RB_FOREACH(bp, buf_rb_tree, &ip->vp->v_rbdirty_tree) {
2359 delta = n - ip->rsv_databufs;
2360 ip->rsv_databufs += delta;
2361 ip->hmp->rsv_databufs += delta;
2365 * dounlink - disconnect a directory entry
2367 * XXX whiteout support not really in yet
2370 hammer_dounlink(hammer_transaction_t trans, struct nchandle *nch,
2371 struct vnode *dvp, struct ucred *cred, int flags)
2373 struct namecache *ncp;
2376 struct hammer_cursor cursor;
2381 * Calculate the namekey and setup the key range for the scan. This
2382 * works kinda like a chained hash table where the lower 32 bits
2383 * of the namekey synthesize the chain.
2385 * The key range is inclusive of both key_beg and key_end.
2390 if (dip->flags & HAMMER_INODE_RO)
2393 namekey = hammer_directory_namekey(ncp->nc_name, ncp->nc_nlen);
2395 hammer_init_cursor(trans, &cursor, &dip->cache[0], dip);
2396 cursor.key_beg.localization = HAMMER_LOCALIZE_MISC;
2397 cursor.key_beg.obj_id = dip->obj_id;
2398 cursor.key_beg.key = namekey;
2399 cursor.key_beg.create_tid = 0;
2400 cursor.key_beg.delete_tid = 0;
2401 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
2402 cursor.key_beg.obj_type = 0;
2404 cursor.key_end = cursor.key_beg;
2405 cursor.key_end.key |= 0xFFFFFFFFULL;
2406 cursor.asof = dip->obj_asof;
2407 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
2410 * Scan all matching records (the chain), locate the one matching
2411 * the requested path component. info->last_error contains the
2412 * error code on search termination and could be 0, ENOENT, or
2415 * The hammer_ip_*() functions merge in-memory records with on-disk
2416 * records for the purposes of the search.
2418 error = hammer_ip_first(&cursor);
2420 while (error == 0) {
2421 error = hammer_ip_resolve_data(&cursor);
2424 nlen = cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF;
2426 if (ncp->nc_nlen == nlen &&
2427 bcmp(ncp->nc_name, cursor.data->entry.name, nlen) == 0) {
2430 error = hammer_ip_next(&cursor);
2434 * If all is ok we have to get the inode so we can adjust nlinks.
2435 * To avoid a deadlock with the flusher we must release the inode
2436 * lock on the directory when acquiring the inode for the entry.
2438 * If the target is a directory, it must be empty.
2441 hammer_unlock(&cursor.ip->lock);
2442 ip = hammer_get_inode(trans, &dip->cache[1],
2443 cursor.data->entry.obj_id,
2444 dip->hmp->asof, 0, &error);
2445 hammer_lock_sh(&cursor.ip->lock);
2446 if (error == ENOENT) {
2447 kprintf("obj_id %016llx\n", cursor.data->entry.obj_id);
2448 Debugger("ENOENT unlinking object that should exist");
2452 * If we are trying to remove a directory the directory must
2455 * WARNING: hammer_ip_check_directory_empty() may have to
2456 * terminate the cursor to avoid a deadlock. It is ok to
2457 * call hammer_done_cursor() twice.
2459 if (error == 0 && ip->ino_data.obj_type ==
2460 HAMMER_OBJTYPE_DIRECTORY) {
2461 error = hammer_ip_check_directory_empty(trans, ip);
2465 * Delete the directory entry.
2467 * WARNING: hammer_ip_del_directory() may have to terminate
2468 * the cursor to avoid a deadlock. It is ok to call
2469 * hammer_done_cursor() twice.
2472 error = hammer_ip_del_directory(trans, &cursor,
2475 hammer_done_cursor(&cursor);
2477 cache_setunresolved(nch);
2478 cache_setvp(nch, NULL);
2481 cache_inval_vp(ip->vp, CINV_DESTROY);
2484 hammer_rel_inode(ip, 0);
2486 hammer_done_cursor(&cursor);
2488 if (error == EDEADLK)
2494 /************************************************************************
2495 * FIFO AND SPECFS OPS *
2496 ************************************************************************
2501 hammer_vop_fifoclose (struct vop_close_args *ap)
2503 /* XXX update itimes */
2504 return (VOCALL(&fifo_vnode_vops, &ap->a_head));
2508 hammer_vop_fiforead (struct vop_read_args *ap)
2512 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
2513 /* XXX update access time */
2518 hammer_vop_fifowrite (struct vop_write_args *ap)
2522 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
2523 /* XXX update access time */
2528 hammer_vop_specclose (struct vop_close_args *ap)
2530 /* XXX update itimes */
2531 return (VOCALL(&spec_vnode_vops, &ap->a_head));
2535 hammer_vop_specread (struct vop_read_args *ap)
2537 /* XXX update access time */
2538 return (VOCALL(&spec_vnode_vops, &ap->a_head));
2542 hammer_vop_specwrite (struct vop_write_args *ap)
2544 /* XXX update last change time */
2545 return (VOCALL(&spec_vnode_vops, &ap->a_head));