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.65 2008/06/10 22:30:21 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_nsymlink(struct vop_nsymlink_args *);
79 static int hammer_vop_nwhiteout(struct vop_nwhiteout_args *);
80 static int hammer_vop_ioctl(struct vop_ioctl_args *);
81 static int hammer_vop_mountctl(struct vop_mountctl_args *);
83 static int hammer_vop_fifoclose (struct vop_close_args *);
84 static int hammer_vop_fiforead (struct vop_read_args *);
85 static int hammer_vop_fifowrite (struct vop_write_args *);
87 static int hammer_vop_specclose (struct vop_close_args *);
88 static int hammer_vop_specread (struct vop_read_args *);
89 static int hammer_vop_specwrite (struct vop_write_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 = hammer_vop_pathconf,
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_setattr = hammer_vop_setattr,
119 .vop_strategy = hammer_vop_strategy,
120 .vop_nsymlink = hammer_vop_nsymlink,
121 .vop_nwhiteout = hammer_vop_nwhiteout,
122 .vop_ioctl = hammer_vop_ioctl,
123 .vop_mountctl = hammer_vop_mountctl
126 struct vop_ops hammer_spec_vops = {
127 .vop_default = spec_vnoperate,
128 .vop_fsync = hammer_vop_fsync,
129 .vop_read = hammer_vop_specread,
130 .vop_write = hammer_vop_specwrite,
131 .vop_access = hammer_vop_access,
132 .vop_close = hammer_vop_specclose,
133 .vop_getattr = hammer_vop_getattr,
134 .vop_inactive = hammer_vop_inactive,
135 .vop_reclaim = hammer_vop_reclaim,
136 .vop_setattr = hammer_vop_setattr
139 struct vop_ops hammer_fifo_vops = {
140 .vop_default = fifo_vnoperate,
141 .vop_fsync = hammer_vop_fsync,
142 .vop_read = hammer_vop_fiforead,
143 .vop_write = hammer_vop_fifowrite,
144 .vop_access = hammer_vop_access,
145 .vop_close = hammer_vop_fifoclose,
146 .vop_getattr = hammer_vop_getattr,
147 .vop_inactive = hammer_vop_inactive,
148 .vop_reclaim = hammer_vop_reclaim,
149 .vop_setattr = hammer_vop_setattr
152 #ifdef DEBUG_TRUNCATE
153 struct hammer_inode *HammerTruncIp;
156 static int hammer_dounlink(hammer_transaction_t trans, struct nchandle *nch,
157 struct vnode *dvp, struct ucred *cred, int flags);
158 static int hammer_vop_strategy_read(struct vop_strategy_args *ap);
159 static int hammer_vop_strategy_write(struct vop_strategy_args *ap);
160 static void hammer_cleanup_write_io(hammer_inode_t ip);
161 static void hammer_update_rsv_databufs(hammer_inode_t ip);
166 hammer_vop_vnoperate(struct vop_generic_args *)
168 return (VOCALL(&hammer_vnode_vops, ap));
173 * hammer_vop_fsync { vp, waitfor }
177 hammer_vop_fsync(struct vop_fsync_args *ap)
179 hammer_inode_t ip = VTOI(ap->a_vp);
181 vfsync(ap->a_vp, ap->a_waitfor, 1, NULL, NULL);
182 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
183 if (ap->a_waitfor == MNT_WAIT)
184 hammer_wait_inode(ip);
189 * hammer_vop_read { vp, uio, ioflag, cred }
193 hammer_vop_read(struct vop_read_args *ap)
195 struct hammer_transaction trans;
204 if (ap->a_vp->v_type != VREG)
208 seqcount = ap->a_ioflag >> 16;
210 hammer_start_transaction(&trans, ip->hmp);
213 * Access the data in HAMMER_BUFSIZE blocks via the buffer cache.
216 while (uio->uio_resid > 0 && uio->uio_offset < ip->ino_data.size) {
217 offset = uio->uio_offset & HAMMER_BUFMASK;
219 error = cluster_read(ap->a_vp, ip->ino_data.size,
220 uio->uio_offset - offset, HAMMER_BUFSIZE,
221 MAXBSIZE, seqcount, &bp);
223 error = bread(ap->a_vp, uio->uio_offset - offset,
224 HAMMER_BUFSIZE, &bp);
229 /* bp->b_flags |= B_CLUSTEROK; temporarily disabled */
230 n = HAMMER_BUFSIZE - offset;
231 if (n > uio->uio_resid)
233 if (n > ip->ino_data.size - uio->uio_offset)
234 n = (int)(ip->ino_data.size - uio->uio_offset);
235 error = uiomove((char *)bp->b_data + offset, n, uio);
240 if ((ip->flags & HAMMER_INODE_RO) == 0 &&
241 (ip->hmp->mp->mnt_flag & MNT_NOATIME) == 0) {
242 ip->ino_leaf.atime = trans.time;
243 hammer_modify_inode(ip, HAMMER_INODE_ITIMES);
245 hammer_done_transaction(&trans);
250 * hammer_vop_write { vp, uio, ioflag, cred }
254 hammer_vop_write(struct vop_write_args *ap)
256 struct hammer_transaction trans;
257 struct hammer_inode *ip;
267 if (ap->a_vp->v_type != VREG)
272 if (ip->flags & HAMMER_INODE_RO)
276 * Create a transaction to cover the operations we perform.
278 hammer_start_transaction(&trans, ip->hmp);
284 if (ap->a_ioflag & IO_APPEND)
285 uio->uio_offset = ip->ino_data.size;
288 * Check for illegal write offsets. Valid range is 0...2^63-1.
290 * NOTE: the base_off assignment is required to work around what
291 * I consider to be a GCC-4 optimization bug.
293 if (uio->uio_offset < 0) {
294 hammer_done_transaction(&trans);
297 base_offset = uio->uio_offset + uio->uio_resid; /* work around gcc-4 */
298 if (uio->uio_resid > 0 && base_offset <= 0) {
299 hammer_done_transaction(&trans);
304 * Access the data in HAMMER_BUFSIZE blocks via the buffer cache.
307 while (uio->uio_resid > 0) {
310 if ((error = hammer_checkspace(trans.hmp)) != 0)
314 * Do not allow HAMMER to blow out the buffer cache.
316 * Do not allow HAMMER to blow out system memory by
317 * accumulating too many records. Records are decoupled
318 * from the buffer cache.
320 * Always check at the beginning so separate writes are
321 * not able to bypass this code.
323 * WARNING: Cannot unlock vp when doing a NOCOPY write as
324 * part of a putpages operation. Doing so could cause us
325 * to deadlock against the VM system when we try to re-lock.
327 if ((count++ & 15) == 0) {
328 if (uio->uio_segflg != UIO_NOCOPY) {
330 if ((ap->a_ioflag & IO_NOBWILL) == 0)
333 if (ip->rsv_recs > hammer_limit_irecs) {
334 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
335 hammer_wait_inode(ip);
337 if (uio->uio_segflg != UIO_NOCOPY)
338 vn_lock(ap->a_vp, LK_EXCLUSIVE|LK_RETRY);
341 rel_offset = (int)(uio->uio_offset & HAMMER_BUFMASK);
342 base_offset = uio->uio_offset & ~HAMMER_BUFMASK64;
343 n = HAMMER_BUFSIZE - rel_offset;
344 if (n > uio->uio_resid)
346 if (uio->uio_offset + n > ip->ino_data.size) {
347 vnode_pager_setsize(ap->a_vp, uio->uio_offset + n);
351 if (uio->uio_segflg == UIO_NOCOPY) {
353 * Issuing a write with the same data backing the
354 * buffer. Instantiate the buffer to collect the
355 * backing vm pages, then read-in any missing bits.
357 * This case is used by vop_stdputpages().
359 bp = getblk(ap->a_vp, base_offset,
360 HAMMER_BUFSIZE, GETBLK_BHEAVY, 0);
361 if ((bp->b_flags & B_CACHE) == 0) {
363 error = bread(ap->a_vp, base_offset,
364 HAMMER_BUFSIZE, &bp);
366 } else if (rel_offset == 0 && uio->uio_resid >= HAMMER_BUFSIZE) {
368 * Even though we are entirely overwriting the buffer
369 * we may still have to zero it out to avoid a
370 * mmap/write visibility issue.
372 bp = getblk(ap->a_vp, base_offset,
373 HAMMER_BUFSIZE, GETBLK_BHEAVY, 0);
374 if ((bp->b_flags & B_CACHE) == 0)
376 } else if (base_offset >= ip->ino_data.size) {
378 * If the base offset of the buffer is beyond the
379 * file EOF, we don't have to issue a read.
381 bp = getblk(ap->a_vp, base_offset,
382 HAMMER_BUFSIZE, GETBLK_BHEAVY, 0);
386 * Partial overwrite, read in any missing bits then
387 * replace the portion being written.
389 error = bread(ap->a_vp, base_offset,
390 HAMMER_BUFSIZE, &bp);
395 error = uiomove((char *)bp->b_data + rel_offset,
400 * If we screwed up we have to undo any VM size changes we
406 vtruncbuf(ap->a_vp, ip->ino_data.size,
411 /* bp->b_flags |= B_CLUSTEROK; temporarily disabled */
412 if (ip->ino_data.size < uio->uio_offset) {
413 ip->ino_data.size = uio->uio_offset;
414 flags = HAMMER_INODE_DDIRTY;
415 vnode_pager_setsize(ap->a_vp, ip->ino_data.size);
419 ip->ino_data.mtime = trans.time;
420 flags |= HAMMER_INODE_ITIMES | HAMMER_INODE_BUFS;
421 flags |= HAMMER_INODE_DDIRTY; /* XXX mtime */
422 hammer_modify_inode(ip, flags);
425 * Try to keep track of cached dirty data.
427 if ((bp->b_flags & B_DIRTY) == 0) {
429 ++ip->hmp->rsv_databufs;
433 * Final buffer disposition.
435 if (ap->a_ioflag & IO_SYNC) {
437 } else if (ap->a_ioflag & IO_DIRECT) {
440 } else if ((ap->a_ioflag >> 16) == IO_SEQMAX &&
441 (uio->uio_offset & HAMMER_BUFMASK) == 0) {
443 * If seqcount indicates sequential operation and
444 * we just finished filling a buffer, push it out
445 * now to prevent the buffer cache from becoming
446 * too full, which would trigger non-optimal
455 hammer_done_transaction(&trans);
460 * hammer_vop_access { vp, mode, cred }
464 hammer_vop_access(struct vop_access_args *ap)
466 struct hammer_inode *ip = VTOI(ap->a_vp);
471 uid = hammer_to_unix_xid(&ip->ino_data.uid);
472 gid = hammer_to_unix_xid(&ip->ino_data.gid);
474 error = vop_helper_access(ap, uid, gid, ip->ino_data.mode,
475 ip->ino_data.uflags);
480 * hammer_vop_advlock { vp, id, op, fl, flags }
484 hammer_vop_advlock(struct vop_advlock_args *ap)
486 struct hammer_inode *ip = VTOI(ap->a_vp);
488 return (lf_advlock(ap, &ip->advlock, ip->ino_data.size));
492 * hammer_vop_close { vp, fflag }
496 hammer_vop_close(struct vop_close_args *ap)
498 return (vop_stdclose(ap));
502 * hammer_vop_ncreate { nch, dvp, vpp, cred, vap }
504 * The operating system has already ensured that the directory entry
505 * does not exist and done all appropriate namespace locking.
509 hammer_vop_ncreate(struct vop_ncreate_args *ap)
511 struct hammer_transaction trans;
512 struct hammer_inode *dip;
513 struct hammer_inode *nip;
514 struct nchandle *nch;
518 dip = VTOI(ap->a_dvp);
520 if (dip->flags & HAMMER_INODE_RO)
522 if ((error = hammer_checkspace(dip->hmp)) != 0)
526 * Create a transaction to cover the operations we perform.
528 hammer_start_transaction(&trans, dip->hmp);
531 * Create a new filesystem object of the requested type. The
532 * returned inode will be referenced and shared-locked to prevent
533 * it from being moved to the flusher.
536 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred, dip, &nip);
538 hkprintf("hammer_create_inode error %d\n", error);
539 hammer_done_transaction(&trans);
545 * Add the new filesystem object to the directory. This will also
546 * bump the inode's link count.
548 error = hammer_ip_add_directory(&trans, dip, nch->ncp, nip);
550 hkprintf("hammer_ip_add_directory error %d\n", error);
556 hammer_rel_inode(nip, 0);
557 hammer_done_transaction(&trans);
560 error = hammer_get_vnode(nip, ap->a_vpp);
561 hammer_done_transaction(&trans);
562 hammer_rel_inode(nip, 0);
564 cache_setunresolved(ap->a_nch);
565 cache_setvp(ap->a_nch, *ap->a_vpp);
572 * hammer_vop_getattr { vp, vap }
574 * Retrieve an inode's attribute information. When accessing inodes
575 * historically we fake the atime field to ensure consistent results.
576 * The atime field is stored in the B-Tree element and allowed to be
577 * updated without cycling the element.
581 hammer_vop_getattr(struct vop_getattr_args *ap)
583 struct hammer_inode *ip = VTOI(ap->a_vp);
584 struct vattr *vap = ap->a_vap;
587 if (cache_check_fsmid_vp(ap->a_vp, &ip->fsmid) &&
588 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0 &&
593 hammer_itimes(ap->a_vp);
596 vap->va_fsid = ip->hmp->fsid_udev;
597 vap->va_fileid = ip->ino_leaf.base.obj_id;
598 vap->va_mode = ip->ino_data.mode;
599 vap->va_nlink = ip->ino_data.nlinks;
600 vap->va_uid = hammer_to_unix_xid(&ip->ino_data.uid);
601 vap->va_gid = hammer_to_unix_xid(&ip->ino_data.gid);
604 vap->va_size = ip->ino_data.size;
605 if (ip->flags & HAMMER_INODE_RO)
606 hammer_to_timespec(ip->ino_data.mtime, &vap->va_atime);
608 hammer_to_timespec(ip->ino_leaf.atime, &vap->va_atime);
609 hammer_to_timespec(ip->ino_data.mtime, &vap->va_mtime);
610 hammer_to_timespec(ip->ino_data.ctime, &vap->va_ctime);
611 vap->va_flags = ip->ino_data.uflags;
612 vap->va_gen = 1; /* hammer inums are unique for all time */
613 vap->va_blocksize = HAMMER_BUFSIZE;
614 vap->va_bytes = (ip->ino_data.size + 63) & ~63;
615 vap->va_type = hammer_get_vnode_type(ip->ino_data.obj_type);
616 vap->va_filerev = 0; /* XXX */
617 /* mtime uniquely identifies any adjustments made to the file */
618 vap->va_fsmid = ip->ino_data.mtime;
619 vap->va_uid_uuid = ip->ino_data.uid;
620 vap->va_gid_uuid = ip->ino_data.gid;
621 vap->va_fsid_uuid = ip->hmp->fsid;
622 vap->va_vaflags = VA_UID_UUID_VALID | VA_GID_UUID_VALID |
625 switch (ip->ino_data.obj_type) {
626 case HAMMER_OBJTYPE_CDEV:
627 case HAMMER_OBJTYPE_BDEV:
628 vap->va_rmajor = ip->ino_data.rmajor;
629 vap->va_rminor = ip->ino_data.rminor;
639 * hammer_vop_nresolve { nch, dvp, cred }
641 * Locate the requested directory entry.
645 hammer_vop_nresolve(struct vop_nresolve_args *ap)
647 struct hammer_transaction trans;
648 struct namecache *ncp;
652 struct hammer_cursor cursor;
662 * Misc initialization, plus handle as-of name extensions. Look for
663 * the '@@' extension. Note that as-of files and directories cannot
666 dip = VTOI(ap->a_dvp);
667 ncp = ap->a_nch->ncp;
668 asof = dip->obj_asof;
672 hammer_simple_transaction(&trans, dip->hmp);
674 for (i = 0; i < nlen; ++i) {
675 if (ncp->nc_name[i] == '@' && ncp->nc_name[i+1] == '@') {
676 asof = hammer_str_to_tid(ncp->nc_name + i + 2);
677 flags |= HAMMER_INODE_RO;
684 * If there is no path component the time extension is relative to
688 ip = hammer_get_inode(&trans, &dip->cache[1], dip->obj_id,
689 asof, flags, &error);
691 error = hammer_get_vnode(ip, &vp);
692 hammer_rel_inode(ip, 0);
698 cache_setvp(ap->a_nch, vp);
705 * Calculate the namekey and setup the key range for the scan. This
706 * works kinda like a chained hash table where the lower 32 bits
707 * of the namekey synthesize the chain.
709 * The key range is inclusive of both key_beg and key_end.
711 namekey = hammer_directory_namekey(ncp->nc_name, nlen);
713 error = hammer_init_cursor(&trans, &cursor, &dip->cache[0], dip);
714 cursor.key_beg.localization = HAMMER_LOCALIZE_MISC;
715 cursor.key_beg.obj_id = dip->obj_id;
716 cursor.key_beg.key = namekey;
717 cursor.key_beg.create_tid = 0;
718 cursor.key_beg.delete_tid = 0;
719 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
720 cursor.key_beg.obj_type = 0;
722 cursor.key_end = cursor.key_beg;
723 cursor.key_end.key |= 0xFFFFFFFFULL;
725 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
728 * Scan all matching records (the chain), locate the one matching
729 * the requested path component.
731 * The hammer_ip_*() functions merge in-memory records with on-disk
732 * records for the purposes of the search.
737 error = hammer_ip_first(&cursor);
739 error = hammer_ip_resolve_data(&cursor);
742 if (nlen == cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF &&
743 bcmp(ncp->nc_name, cursor.data->entry.name, nlen) == 0) {
744 obj_id = cursor.data->entry.obj_id;
747 error = hammer_ip_next(&cursor);
750 hammer_done_cursor(&cursor);
752 ip = hammer_get_inode(&trans, &dip->cache[1],
753 obj_id, asof, flags, &error);
755 error = hammer_get_vnode(ip, &vp);
756 hammer_rel_inode(ip, 0);
762 cache_setvp(ap->a_nch, vp);
765 } else if (error == ENOENT) {
766 cache_setvp(ap->a_nch, NULL);
769 hammer_done_transaction(&trans);
774 * hammer_vop_nlookupdotdot { dvp, vpp, cred }
776 * Locate the parent directory of a directory vnode.
778 * dvp is referenced but not locked. *vpp must be returned referenced and
779 * locked. A parent_obj_id of 0 does not necessarily indicate that we are
780 * at the root, instead it could indicate that the directory we were in was
783 * NOTE: as-of sequences are not linked into the directory structure. If
784 * we are at the root with a different asof then the mount point, reload
785 * the same directory with the mount point's asof. I'm not sure what this
786 * will do to NFS. We encode ASOF stamps in NFS file handles so it might not
787 * get confused, but it hasn't been tested.
791 hammer_vop_nlookupdotdot(struct vop_nlookupdotdot_args *ap)
793 struct hammer_transaction trans;
794 struct hammer_inode *dip;
795 struct hammer_inode *ip;
796 int64_t parent_obj_id;
800 dip = VTOI(ap->a_dvp);
801 asof = dip->obj_asof;
802 parent_obj_id = dip->ino_data.parent_obj_id;
804 if (parent_obj_id == 0) {
805 if (dip->obj_id == HAMMER_OBJID_ROOT &&
806 asof != dip->hmp->asof) {
807 parent_obj_id = dip->obj_id;
808 asof = dip->hmp->asof;
809 *ap->a_fakename = kmalloc(19, M_TEMP, M_WAITOK);
810 ksnprintf(*ap->a_fakename, 19, "0x%016llx",
818 hammer_simple_transaction(&trans, dip->hmp);
820 ip = hammer_get_inode(&trans, &dip->cache[1], parent_obj_id,
821 asof, dip->flags, &error);
823 error = hammer_get_vnode(ip, ap->a_vpp);
824 hammer_rel_inode(ip, 0);
828 hammer_done_transaction(&trans);
833 * hammer_vop_nlink { nch, dvp, vp, cred }
837 hammer_vop_nlink(struct vop_nlink_args *ap)
839 struct hammer_transaction trans;
840 struct hammer_inode *dip;
841 struct hammer_inode *ip;
842 struct nchandle *nch;
846 dip = VTOI(ap->a_dvp);
849 if (dip->flags & HAMMER_INODE_RO)
851 if (ip->flags & HAMMER_INODE_RO)
853 if ((error = hammer_checkspace(dip->hmp)) != 0)
857 * Create a transaction to cover the operations we perform.
859 hammer_start_transaction(&trans, dip->hmp);
862 * Add the filesystem object to the directory. Note that neither
863 * dip nor ip are referenced or locked, but their vnodes are
864 * referenced. This function will bump the inode's link count.
866 error = hammer_ip_add_directory(&trans, dip, nch->ncp, ip);
872 cache_setunresolved(nch);
873 cache_setvp(nch, ap->a_vp);
875 hammer_done_transaction(&trans);
880 * hammer_vop_nmkdir { nch, dvp, vpp, cred, vap }
882 * The operating system has already ensured that the directory entry
883 * does not exist and done all appropriate namespace locking.
887 hammer_vop_nmkdir(struct vop_nmkdir_args *ap)
889 struct hammer_transaction trans;
890 struct hammer_inode *dip;
891 struct hammer_inode *nip;
892 struct nchandle *nch;
896 dip = VTOI(ap->a_dvp);
898 if (dip->flags & HAMMER_INODE_RO)
900 if ((error = hammer_checkspace(dip->hmp)) != 0)
904 * Create a transaction to cover the operations we perform.
906 hammer_start_transaction(&trans, dip->hmp);
909 * Create a new filesystem object of the requested type. The
910 * returned inode will be referenced but not locked.
912 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred, dip, &nip);
914 hkprintf("hammer_mkdir error %d\n", error);
915 hammer_done_transaction(&trans);
920 * Add the new filesystem object to the directory. This will also
921 * bump the inode's link count.
923 error = hammer_ip_add_directory(&trans, dip, nch->ncp, nip);
925 hkprintf("hammer_mkdir (add) error %d\n", error);
931 hammer_rel_inode(nip, 0);
934 error = hammer_get_vnode(nip, ap->a_vpp);
935 hammer_rel_inode(nip, 0);
937 cache_setunresolved(ap->a_nch);
938 cache_setvp(ap->a_nch, *ap->a_vpp);
941 hammer_done_transaction(&trans);
946 * hammer_vop_nmknod { nch, dvp, vpp, cred, vap }
948 * The operating system has already ensured that the directory entry
949 * does not exist and done all appropriate namespace locking.
953 hammer_vop_nmknod(struct vop_nmknod_args *ap)
955 struct hammer_transaction trans;
956 struct hammer_inode *dip;
957 struct hammer_inode *nip;
958 struct nchandle *nch;
962 dip = VTOI(ap->a_dvp);
964 if (dip->flags & HAMMER_INODE_RO)
966 if ((error = hammer_checkspace(dip->hmp)) != 0)
970 * Create a transaction to cover the operations we perform.
972 hammer_start_transaction(&trans, dip->hmp);
975 * Create a new filesystem object of the requested type. The
976 * returned inode will be referenced but not locked.
978 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred, dip, &nip);
980 hammer_done_transaction(&trans);
986 * Add the new filesystem object to the directory. This will also
987 * bump the inode's link count.
989 error = hammer_ip_add_directory(&trans, dip, nch->ncp, nip);
995 hammer_rel_inode(nip, 0);
998 error = hammer_get_vnode(nip, ap->a_vpp);
999 hammer_rel_inode(nip, 0);
1001 cache_setunresolved(ap->a_nch);
1002 cache_setvp(ap->a_nch, *ap->a_vpp);
1005 hammer_done_transaction(&trans);
1010 * hammer_vop_open { vp, mode, cred, fp }
1014 hammer_vop_open(struct vop_open_args *ap)
1018 ip = VTOI(ap->a_vp);
1020 if ((ap->a_mode & FWRITE) && (ip->flags & HAMMER_INODE_RO))
1022 return(vop_stdopen(ap));
1026 * hammer_vop_pathconf { vp, name, retval }
1030 hammer_vop_pathconf(struct vop_pathconf_args *ap)
1036 * hammer_vop_print { vp }
1040 hammer_vop_print(struct vop_print_args *ap)
1046 * hammer_vop_readdir { vp, uio, cred, *eofflag, *ncookies, off_t **cookies }
1050 hammer_vop_readdir(struct vop_readdir_args *ap)
1052 struct hammer_transaction trans;
1053 struct hammer_cursor cursor;
1054 struct hammer_inode *ip;
1056 hammer_base_elm_t base;
1064 ip = VTOI(ap->a_vp);
1066 saveoff = uio->uio_offset;
1068 if (ap->a_ncookies) {
1069 ncookies = uio->uio_resid / 16 + 1;
1070 if (ncookies > 1024)
1072 cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
1080 hammer_simple_transaction(&trans, ip->hmp);
1083 * Handle artificial entries
1087 r = vop_write_dirent(&error, uio, ip->obj_id, DT_DIR, 1, ".");
1091 cookies[cookie_index] = saveoff;
1094 if (cookie_index == ncookies)
1098 if (ip->ino_data.parent_obj_id) {
1099 r = vop_write_dirent(&error, uio,
1100 ip->ino_data.parent_obj_id,
1103 r = vop_write_dirent(&error, uio,
1104 ip->obj_id, DT_DIR, 2, "..");
1109 cookies[cookie_index] = saveoff;
1112 if (cookie_index == ncookies)
1117 * Key range (begin and end inclusive) to scan. Directory keys
1118 * directly translate to a 64 bit 'seek' position.
1120 hammer_init_cursor(&trans, &cursor, &ip->cache[0], ip);
1121 cursor.key_beg.localization = HAMMER_LOCALIZE_MISC;
1122 cursor.key_beg.obj_id = ip->obj_id;
1123 cursor.key_beg.create_tid = 0;
1124 cursor.key_beg.delete_tid = 0;
1125 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1126 cursor.key_beg.obj_type = 0;
1127 cursor.key_beg.key = saveoff;
1129 cursor.key_end = cursor.key_beg;
1130 cursor.key_end.key = HAMMER_MAX_KEY;
1131 cursor.asof = ip->obj_asof;
1132 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
1134 error = hammer_ip_first(&cursor);
1136 while (error == 0) {
1137 error = hammer_ip_resolve_data(&cursor);
1140 base = &cursor.leaf->base;
1141 saveoff = base->key;
1142 KKASSERT(cursor.leaf->data_len > HAMMER_ENTRY_NAME_OFF);
1144 if (base->obj_id != ip->obj_id)
1145 panic("readdir: bad record at %p", cursor.node);
1147 r = vop_write_dirent(
1148 &error, uio, cursor.data->entry.obj_id,
1149 hammer_get_dtype(cursor.leaf->base.obj_type),
1150 cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF ,
1151 (void *)cursor.data->entry.name);
1156 cookies[cookie_index] = base->key;
1158 if (cookie_index == ncookies)
1160 error = hammer_ip_next(&cursor);
1162 hammer_done_cursor(&cursor);
1165 hammer_done_transaction(&trans);
1168 *ap->a_eofflag = (error == ENOENT);
1169 uio->uio_offset = saveoff;
1170 if (error && cookie_index == 0) {
1171 if (error == ENOENT)
1174 kfree(cookies, M_TEMP);
1175 *ap->a_ncookies = 0;
1176 *ap->a_cookies = NULL;
1179 if (error == ENOENT)
1182 *ap->a_ncookies = cookie_index;
1183 *ap->a_cookies = cookies;
1190 * hammer_vop_readlink { vp, uio, cred }
1194 hammer_vop_readlink(struct vop_readlink_args *ap)
1196 struct hammer_transaction trans;
1197 struct hammer_cursor cursor;
1198 struct hammer_inode *ip;
1201 ip = VTOI(ap->a_vp);
1204 * Shortcut if the symlink data was stuffed into ino_data.
1206 if (ip->ino_data.size <= HAMMER_INODE_BASESYMLEN) {
1207 error = uiomove(ip->ino_data.ext.symlink,
1208 ip->ino_data.size, ap->a_uio);
1215 hammer_simple_transaction(&trans, ip->hmp);
1216 hammer_init_cursor(&trans, &cursor, &ip->cache[0], ip);
1219 * Key range (begin and end inclusive) to scan. Directory keys
1220 * directly translate to a 64 bit 'seek' position.
1222 cursor.key_beg.localization = HAMMER_LOCALIZE_MISC; /* XXX */
1223 cursor.key_beg.obj_id = ip->obj_id;
1224 cursor.key_beg.create_tid = 0;
1225 cursor.key_beg.delete_tid = 0;
1226 cursor.key_beg.rec_type = HAMMER_RECTYPE_FIX;
1227 cursor.key_beg.obj_type = 0;
1228 cursor.key_beg.key = HAMMER_FIXKEY_SYMLINK;
1229 cursor.asof = ip->obj_asof;
1230 cursor.flags |= HAMMER_CURSOR_ASOF;
1232 error = hammer_ip_lookup(&cursor);
1234 error = hammer_ip_resolve_data(&cursor);
1236 KKASSERT(cursor.leaf->data_len >=
1237 HAMMER_SYMLINK_NAME_OFF);
1238 error = uiomove(cursor.data->symlink.name,
1239 cursor.leaf->data_len -
1240 HAMMER_SYMLINK_NAME_OFF,
1244 hammer_done_cursor(&cursor);
1245 hammer_done_transaction(&trans);
1250 * hammer_vop_nremove { nch, dvp, cred }
1254 hammer_vop_nremove(struct vop_nremove_args *ap)
1256 struct hammer_transaction trans;
1257 struct hammer_inode *dip;
1260 dip = VTOI(ap->a_dvp);
1262 if (hammer_nohistory(dip) == 0 &&
1263 (error = hammer_checkspace(dip->hmp)) != 0) {
1267 hammer_start_transaction(&trans, dip->hmp);
1268 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp, ap->a_cred, 0);
1269 hammer_done_transaction(&trans);
1275 * hammer_vop_nrename { fnch, tnch, fdvp, tdvp, cred }
1279 hammer_vop_nrename(struct vop_nrename_args *ap)
1281 struct hammer_transaction trans;
1282 struct namecache *fncp;
1283 struct namecache *tncp;
1284 struct hammer_inode *fdip;
1285 struct hammer_inode *tdip;
1286 struct hammer_inode *ip;
1287 struct hammer_cursor cursor;
1291 fdip = VTOI(ap->a_fdvp);
1292 tdip = VTOI(ap->a_tdvp);
1293 fncp = ap->a_fnch->ncp;
1294 tncp = ap->a_tnch->ncp;
1295 ip = VTOI(fncp->nc_vp);
1296 KKASSERT(ip != NULL);
1298 if (fdip->flags & HAMMER_INODE_RO)
1300 if (tdip->flags & HAMMER_INODE_RO)
1302 if (ip->flags & HAMMER_INODE_RO)
1304 if ((error = hammer_checkspace(fdip->hmp)) != 0)
1307 hammer_start_transaction(&trans, fdip->hmp);
1310 * Remove tncp from the target directory and then link ip as
1311 * tncp. XXX pass trans to dounlink
1313 * Force the inode sync-time to match the transaction so it is
1314 * in-sync with the creation of the target directory entry.
1316 error = hammer_dounlink(&trans, ap->a_tnch, ap->a_tdvp, ap->a_cred, 0);
1317 if (error == 0 || error == ENOENT) {
1318 error = hammer_ip_add_directory(&trans, tdip, tncp, ip);
1320 ip->ino_data.parent_obj_id = tdip->obj_id;
1321 hammer_modify_inode(ip, HAMMER_INODE_DDIRTY);
1325 goto failed; /* XXX */
1328 * Locate the record in the originating directory and remove it.
1330 * Calculate the namekey and setup the key range for the scan. This
1331 * works kinda like a chained hash table where the lower 32 bits
1332 * of the namekey synthesize the chain.
1334 * The key range is inclusive of both key_beg and key_end.
1336 namekey = hammer_directory_namekey(fncp->nc_name, fncp->nc_nlen);
1338 hammer_init_cursor(&trans, &cursor, &fdip->cache[0], fdip);
1339 cursor.key_beg.localization = HAMMER_LOCALIZE_MISC;
1340 cursor.key_beg.obj_id = fdip->obj_id;
1341 cursor.key_beg.key = namekey;
1342 cursor.key_beg.create_tid = 0;
1343 cursor.key_beg.delete_tid = 0;
1344 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1345 cursor.key_beg.obj_type = 0;
1347 cursor.key_end = cursor.key_beg;
1348 cursor.key_end.key |= 0xFFFFFFFFULL;
1349 cursor.asof = fdip->obj_asof;
1350 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
1353 * Scan all matching records (the chain), locate the one matching
1354 * the requested path component.
1356 * The hammer_ip_*() functions merge in-memory records with on-disk
1357 * records for the purposes of the search.
1359 error = hammer_ip_first(&cursor);
1360 while (error == 0) {
1361 if (hammer_ip_resolve_data(&cursor) != 0)
1363 nlen = cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF;
1365 if (fncp->nc_nlen == nlen &&
1366 bcmp(fncp->nc_name, cursor.data->entry.name, nlen) == 0) {
1369 error = hammer_ip_next(&cursor);
1373 * If all is ok we have to get the inode so we can adjust nlinks.
1375 * WARNING: hammer_ip_del_directory() may have to terminate the
1376 * cursor to avoid a recursion. It's ok to call hammer_done_cursor()
1380 error = hammer_ip_del_directory(&trans, &cursor, fdip, ip);
1383 * XXX A deadlock here will break rename's atomicy for the purposes
1384 * of crash recovery.
1386 if (error == EDEADLK) {
1387 hammer_done_cursor(&cursor);
1392 * Cleanup and tell the kernel that the rename succeeded.
1394 hammer_done_cursor(&cursor);
1396 cache_rename(ap->a_fnch, ap->a_tnch);
1399 hammer_done_transaction(&trans);
1404 * hammer_vop_nrmdir { nch, dvp, cred }
1408 hammer_vop_nrmdir(struct vop_nrmdir_args *ap)
1410 struct hammer_transaction trans;
1411 struct hammer_inode *dip;
1414 dip = VTOI(ap->a_dvp);
1416 if (hammer_nohistory(dip) == 0 &&
1417 (error = hammer_checkspace(dip->hmp)) != 0) {
1421 hammer_start_transaction(&trans, dip->hmp);
1422 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp, ap->a_cred, 0);
1423 hammer_done_transaction(&trans);
1429 * hammer_vop_setattr { vp, vap, cred }
1433 hammer_vop_setattr(struct vop_setattr_args *ap)
1435 struct hammer_transaction trans;
1437 struct hammer_inode *ip;
1445 ip = ap->a_vp->v_data;
1448 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
1450 if (ip->flags & HAMMER_INODE_RO)
1452 if (hammer_nohistory(ip) == 0 &&
1453 (error = hammer_checkspace(ip->hmp)) != 0) {
1457 hammer_start_transaction(&trans, ip->hmp);
1460 if (vap->va_flags != VNOVAL) {
1461 flags = ip->ino_data.uflags;
1462 error = vop_helper_setattr_flags(&flags, vap->va_flags,
1463 hammer_to_unix_xid(&ip->ino_data.uid),
1466 if (ip->ino_data.uflags != flags) {
1467 ip->ino_data.uflags = flags;
1468 modflags |= HAMMER_INODE_DDIRTY;
1470 if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) {
1477 if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) {
1481 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
1482 mode_t cur_mode = ip->ino_data.mode;
1483 uid_t cur_uid = hammer_to_unix_xid(&ip->ino_data.uid);
1484 gid_t cur_gid = hammer_to_unix_xid(&ip->ino_data.gid);
1488 error = vop_helper_chown(ap->a_vp, vap->va_uid, vap->va_gid,
1490 &cur_uid, &cur_gid, &cur_mode);
1492 hammer_guid_to_uuid(&uuid_uid, cur_uid);
1493 hammer_guid_to_uuid(&uuid_gid, cur_gid);
1494 if (bcmp(&uuid_uid, &ip->ino_data.uid,
1495 sizeof(uuid_uid)) ||
1496 bcmp(&uuid_gid, &ip->ino_data.gid,
1497 sizeof(uuid_gid)) ||
1498 ip->ino_data.mode != cur_mode
1500 ip->ino_data.uid = uuid_uid;
1501 ip->ino_data.gid = uuid_gid;
1502 ip->ino_data.mode = cur_mode;
1504 modflags |= HAMMER_INODE_DDIRTY;
1507 while (vap->va_size != VNOVAL && ip->ino_data.size != vap->va_size) {
1508 switch(ap->a_vp->v_type) {
1510 if (vap->va_size == ip->ino_data.size)
1513 * XXX break atomicy, we can deadlock the backend
1514 * if we do not release the lock. Probably not a
1517 if (vap->va_size < ip->ino_data.size) {
1518 vtruncbuf(ap->a_vp, vap->va_size,
1522 vnode_pager_setsize(ap->a_vp, vap->va_size);
1525 ip->ino_data.size = vap->va_size;
1526 modflags |= HAMMER_INODE_DDIRTY;
1527 aligned_size = (vap->va_size + HAMMER_BUFMASK) &
1531 * on-media truncation is cached in the inode until
1532 * the inode is synchronized.
1535 hammer_ip_frontend_trunc(ip, vap->va_size);
1536 hammer_update_rsv_databufs(ip);
1537 #ifdef DEBUG_TRUNCATE
1538 if (HammerTruncIp == NULL)
1541 if ((ip->flags & HAMMER_INODE_TRUNCATED) == 0) {
1542 ip->flags |= HAMMER_INODE_TRUNCATED;
1543 ip->trunc_off = vap->va_size;
1544 #ifdef DEBUG_TRUNCATE
1545 if (ip == HammerTruncIp)
1546 kprintf("truncate1 %016llx\n", ip->trunc_off);
1548 } else if (ip->trunc_off > vap->va_size) {
1549 ip->trunc_off = vap->va_size;
1550 #ifdef DEBUG_TRUNCATE
1551 if (ip == HammerTruncIp)
1552 kprintf("truncate2 %016llx\n", ip->trunc_off);
1555 #ifdef DEBUG_TRUNCATE
1556 if (ip == HammerTruncIp)
1557 kprintf("truncate3 %016llx (ignored)\n", vap->va_size);
1563 * If truncating we have to clean out a portion of
1564 * the last block on-disk. We do this in the
1565 * front-end buffer cache.
1567 if (truncating && vap->va_size < aligned_size) {
1571 aligned_size -= HAMMER_BUFSIZE;
1573 offset = vap->va_size & HAMMER_BUFMASK;
1574 error = bread(ap->a_vp, aligned_size,
1575 HAMMER_BUFSIZE, &bp);
1576 hammer_ip_frontend_trunc(ip, aligned_size);
1578 bzero(bp->b_data + offset,
1579 HAMMER_BUFSIZE - offset);
1582 kprintf("ERROR %d\n", error);
1588 if ((ip->flags & HAMMER_INODE_TRUNCATED) == 0) {
1589 ip->flags |= HAMMER_INODE_TRUNCATED;
1590 ip->trunc_off = vap->va_size;
1591 } else if (ip->trunc_off > vap->va_size) {
1592 ip->trunc_off = vap->va_size;
1594 hammer_ip_frontend_trunc(ip, vap->va_size);
1595 ip->ino_data.size = vap->va_size;
1596 modflags |= HAMMER_INODE_DDIRTY;
1604 if (vap->va_atime.tv_sec != VNOVAL) {
1605 ip->ino_leaf.atime =
1606 hammer_timespec_to_transid(&vap->va_atime);
1607 modflags |= HAMMER_INODE_ITIMES;
1609 if (vap->va_mtime.tv_sec != VNOVAL) {
1610 ip->ino_data.mtime =
1611 hammer_timespec_to_transid(&vap->va_mtime);
1612 modflags |= HAMMER_INODE_ITIMES;
1613 modflags |= HAMMER_INODE_DDIRTY; /* XXX mtime */
1615 if (vap->va_mode != (mode_t)VNOVAL) {
1616 mode_t cur_mode = ip->ino_data.mode;
1617 uid_t cur_uid = hammer_to_unix_xid(&ip->ino_data.uid);
1618 gid_t cur_gid = hammer_to_unix_xid(&ip->ino_data.gid);
1620 error = vop_helper_chmod(ap->a_vp, vap->va_mode, ap->a_cred,
1621 cur_uid, cur_gid, &cur_mode);
1622 if (error == 0 && ip->ino_data.mode != cur_mode) {
1623 ip->ino_data.mode = cur_mode;
1624 modflags |= HAMMER_INODE_DDIRTY;
1629 hammer_modify_inode(ip, modflags);
1630 hammer_done_transaction(&trans);
1635 * hammer_vop_nsymlink { nch, dvp, vpp, cred, vap, target }
1639 hammer_vop_nsymlink(struct vop_nsymlink_args *ap)
1641 struct hammer_transaction trans;
1642 struct hammer_inode *dip;
1643 struct hammer_inode *nip;
1644 struct nchandle *nch;
1645 hammer_record_t record;
1649 ap->a_vap->va_type = VLNK;
1652 dip = VTOI(ap->a_dvp);
1654 if (dip->flags & HAMMER_INODE_RO)
1656 if ((error = hammer_checkspace(dip->hmp)) != 0)
1660 * Create a transaction to cover the operations we perform.
1662 hammer_start_transaction(&trans, dip->hmp);
1665 * Create a new filesystem object of the requested type. The
1666 * returned inode will be referenced but not locked.
1669 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred, dip, &nip);
1671 hammer_done_transaction(&trans);
1677 * Add a record representing the symlink. symlink stores the link
1678 * as pure data, not a string, and is no \0 terminated.
1681 bytes = strlen(ap->a_target);
1683 if (bytes <= HAMMER_INODE_BASESYMLEN) {
1684 bcopy(ap->a_target, nip->ino_data.ext.symlink, bytes);
1686 record = hammer_alloc_mem_record(nip, bytes);
1687 record->type = HAMMER_MEM_RECORD_GENERAL;
1689 record->leaf.base.localization = HAMMER_LOCALIZE_MISC;
1690 record->leaf.base.key = HAMMER_FIXKEY_SYMLINK;
1691 record->leaf.base.rec_type = HAMMER_RECTYPE_FIX;
1692 record->leaf.data_len = bytes;
1693 KKASSERT(HAMMER_SYMLINK_NAME_OFF == 0);
1694 bcopy(ap->a_target, record->data->symlink.name, bytes);
1695 error = hammer_ip_add_record(&trans, record);
1699 * Set the file size to the length of the link.
1702 nip->ino_data.size = bytes;
1703 hammer_modify_inode(nip, HAMMER_INODE_DDIRTY);
1707 error = hammer_ip_add_directory(&trans, dip, nch->ncp, nip);
1713 hammer_rel_inode(nip, 0);
1716 error = hammer_get_vnode(nip, ap->a_vpp);
1717 hammer_rel_inode(nip, 0);
1719 cache_setunresolved(ap->a_nch);
1720 cache_setvp(ap->a_nch, *ap->a_vpp);
1723 hammer_done_transaction(&trans);
1728 * hammer_vop_nwhiteout { nch, dvp, cred, flags }
1732 hammer_vop_nwhiteout(struct vop_nwhiteout_args *ap)
1734 struct hammer_transaction trans;
1735 struct hammer_inode *dip;
1738 dip = VTOI(ap->a_dvp);
1740 if (hammer_nohistory(dip) == 0 &&
1741 (error = hammer_checkspace(dip->hmp)) != 0) {
1745 hammer_start_transaction(&trans, dip->hmp);
1746 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp,
1747 ap->a_cred, ap->a_flags);
1748 hammer_done_transaction(&trans);
1754 * hammer_vop_ioctl { vp, command, data, fflag, cred }
1758 hammer_vop_ioctl(struct vop_ioctl_args *ap)
1760 struct hammer_inode *ip = ap->a_vp->v_data;
1762 return(hammer_ioctl(ip, ap->a_command, ap->a_data,
1763 ap->a_fflag, ap->a_cred));
1768 hammer_vop_mountctl(struct vop_mountctl_args *ap)
1773 mp = ap->a_head.a_ops->head.vv_mount;
1776 case MOUNTCTL_SET_EXPORT:
1777 if (ap->a_ctllen != sizeof(struct export_args))
1779 error = hammer_vfs_export(mp, ap->a_op,
1780 (const struct export_args *)ap->a_ctl);
1783 error = journal_mountctl(ap);
1790 * hammer_vop_strategy { vp, bio }
1792 * Strategy call, used for regular file read & write only. Note that the
1793 * bp may represent a cluster.
1795 * To simplify operation and allow better optimizations in the future,
1796 * this code does not make any assumptions with regards to buffer alignment
1801 hammer_vop_strategy(struct vop_strategy_args *ap)
1806 bp = ap->a_bio->bio_buf;
1810 error = hammer_vop_strategy_read(ap);
1813 error = hammer_vop_strategy_write(ap);
1816 bp->b_error = error = EINVAL;
1817 bp->b_flags |= B_ERROR;
1825 * Read from a regular file. Iterate the related records and fill in the
1826 * BIO/BUF. Gaps are zero-filled.
1828 * The support code in hammer_object.c should be used to deal with mixed
1829 * in-memory and on-disk records.
1835 hammer_vop_strategy_read(struct vop_strategy_args *ap)
1837 struct hammer_transaction trans;
1838 struct hammer_inode *ip;
1839 struct hammer_cursor cursor;
1840 hammer_base_elm_t base;
1853 ip = ap->a_vp->v_data;
1855 hammer_simple_transaction(&trans, ip->hmp);
1856 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
1859 * Key range (begin and end inclusive) to scan. Note that the key's
1860 * stored in the actual records represent BASE+LEN, not BASE. The
1861 * first record containing bio_offset will have a key > bio_offset.
1863 cursor.key_beg.localization = HAMMER_LOCALIZE_MISC;
1864 cursor.key_beg.obj_id = ip->obj_id;
1865 cursor.key_beg.create_tid = 0;
1866 cursor.key_beg.delete_tid = 0;
1867 cursor.key_beg.obj_type = 0;
1868 cursor.key_beg.key = bio->bio_offset + 1;
1869 cursor.asof = ip->obj_asof;
1870 cursor.flags |= HAMMER_CURSOR_ASOF | HAMMER_CURSOR_DATAEXTOK;
1872 cursor.key_end = cursor.key_beg;
1873 KKASSERT(ip->ino_data.obj_type == HAMMER_OBJTYPE_REGFILE);
1875 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_DBFILE) {
1876 cursor.key_beg.rec_type = HAMMER_RECTYPE_DB;
1877 cursor.key_end.rec_type = HAMMER_RECTYPE_DB;
1878 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
1882 ran_end = bio->bio_offset + bp->b_bufsize;
1883 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
1884 cursor.key_end.rec_type = HAMMER_RECTYPE_DATA;
1885 tmp64 = ran_end + MAXPHYS + 1; /* work-around GCC-4 bug */
1886 if (tmp64 < ran_end)
1887 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
1889 cursor.key_end.key = ran_end + MAXPHYS + 1;
1891 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
1893 error = hammer_ip_first(&cursor);
1896 while (error == 0) {
1898 * Get the base file offset of the record. The key for
1899 * data records is (base + bytes) rather then (base).
1901 base = &cursor.leaf->base;
1902 rec_offset = base->key - cursor.leaf->data_len;
1905 * Calculate the gap, if any, and zero-fill it.
1907 * n is the offset of the start of the record verses our
1908 * current seek offset in the bio.
1910 n = (int)(rec_offset - (bio->bio_offset + boff));
1912 if (n > bp->b_bufsize - boff)
1913 n = bp->b_bufsize - boff;
1914 bzero((char *)bp->b_data + boff, n);
1920 * Calculate the data offset in the record and the number
1921 * of bytes we can copy.
1923 * There are two degenerate cases. First, boff may already
1924 * be at bp->b_bufsize. Secondly, the data offset within
1925 * the record may exceed the record's size.
1929 n = cursor.leaf->data_len - roff;
1931 kprintf("strategy_read: bad n=%d roff=%d\n", n, roff);
1933 } else if (n > bp->b_bufsize - boff) {
1934 n = bp->b_bufsize - boff;
1938 * Deal with cached truncations. This cool bit of code
1939 * allows truncate()/ftruncate() to avoid having to sync
1942 * If the frontend is truncated then all backend records are
1943 * subject to the frontend's truncation.
1945 * If the backend is truncated then backend records on-disk
1946 * (but not in-memory) are subject to the backend's
1947 * truncation. In-memory records owned by the backend
1948 * represent data written after the truncation point on the
1949 * backend and must not be truncated.
1951 * Truncate operations deal with frontend buffer cache
1952 * buffers and frontend-owned in-memory records synchronously.
1954 if (ip->flags & HAMMER_INODE_TRUNCATED) {
1955 if (hammer_cursor_ondisk(&cursor) ||
1956 cursor.iprec->flush_state == HAMMER_FST_FLUSH) {
1957 if (ip->trunc_off <= rec_offset)
1959 else if (ip->trunc_off < rec_offset + n)
1960 n = (int)(ip->trunc_off - rec_offset);
1963 if (ip->sync_flags & HAMMER_INODE_TRUNCATED) {
1964 if (hammer_cursor_ondisk(&cursor)) {
1965 if (ip->sync_trunc_off <= rec_offset)
1967 else if (ip->sync_trunc_off < rec_offset + n)
1968 n = (int)(ip->sync_trunc_off - rec_offset);
1973 * Try to issue a direct read into our bio if possible,
1974 * otherwise resolve the element data into a hammer_buffer
1977 * WARNING: If we hit the else clause.
1979 if (roff == 0 && boff == 0 && n == bp->b_bufsize &&
1980 (rec_offset & HAMMER_BUFMASK) == 0) {
1981 error = hammer_io_direct_read(trans.hmp, cursor.leaf,
1985 error = hammer_ip_resolve_data(&cursor);
1987 bcopy((char *)cursor.data + roff,
1988 (char *)bp->b_data + boff, n);
1995 * Iterate until we have filled the request.
1998 if (boff == bp->b_bufsize)
2000 error = hammer_ip_next(&cursor);
2004 * There may have been a gap after the last record
2006 if (error == ENOENT)
2008 if (error == 0 && boff != bp->b_bufsize) {
2009 KKASSERT(boff < bp->b_bufsize);
2010 bzero((char *)bp->b_data + boff, bp->b_bufsize - boff);
2011 /* boff = bp->b_bufsize; */
2014 bp->b_error = error;
2016 bp->b_flags |= B_ERROR;
2021 hammer_cache_node(cursor.node, &ip->cache[1]);
2022 hammer_done_cursor(&cursor);
2023 hammer_done_transaction(&trans);
2028 * Write to a regular file. Because this is a strategy call the OS is
2029 * trying to actually sync data to the media. HAMMER can only flush
2030 * the entire inode (so the TID remains properly synchronized).
2032 * Basically all we do here is place the bio on the inode's flush queue
2033 * and activate the flusher.
2037 hammer_vop_strategy_write(struct vop_strategy_args *ap)
2039 hammer_record_t record;
2049 ip = ap->a_vp->v_data;
2052 if (ip->flags & HAMMER_INODE_RO) {
2053 bp->b_error = EROFS;
2054 bp->b_flags |= B_ERROR;
2056 hammer_cleanup_write_io(ip);
2061 * Interlock with inode destruction (no in-kernel or directory
2062 * topology visibility). If we queue new IO while trying to
2063 * destroy the inode we can deadlock the vtrunc call in
2064 * hammer_inode_unloadable_check().
2066 if (ip->flags & (HAMMER_INODE_DELETING|HAMMER_INODE_DELETED)) {
2069 hammer_cleanup_write_io(ip);
2074 * Attempt to reserve space and issue a direct-write from the
2075 * front-end. If we can't we will queue the BIO to the flusher.
2076 * The bulk/direct-write code will still bcopy if writing less
2077 * then full-sized blocks (at the end of a file).
2079 * If we can the I/O can be issued and an in-memory record will
2080 * be installed to reference the storage until the flusher can get to
2083 * Since we own the high level bio the front-end will not try to
2084 * do a direct-read until the write completes.
2086 KKASSERT((bio->bio_offset & HAMMER_BUFMASK) == 0);
2087 KKASSERT(bio->bio_offset < ip->ino_data.size);
2088 if (bio->bio_offset + bp->b_bufsize <= ip->ino_data.size)
2089 bytes = bp->b_bufsize;
2091 bytes = (int)(ip->ino_data.size - bio->bio_offset);
2093 record = hammer_ip_add_bulk(ip, bio->bio_offset, bp->b_data,
2096 hammer_io_direct_write(hmp, &record->leaf, bio);
2097 hammer_rel_mem_record(record);
2098 if (hmp->rsv_recs > hammer_limit_recs &&
2099 ip->rsv_recs > hammer_limit_irecs / 10) {
2100 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
2101 } else if (ip->rsv_recs > hammer_limit_irecs) {
2102 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
2105 bp->b_error = error;
2106 bp->b_flags |= B_ERROR;
2109 hammer_cleanup_write_io(ip);
2114 * Clean-up after disposing of a dirty frontend buffer's data.
2115 * This is somewhat heuristical so try to be robust.
2118 hammer_cleanup_write_io(hammer_inode_t ip)
2120 if (ip->rsv_databufs) {
2122 --ip->hmp->rsv_databufs;
2127 * We can lose track of dirty buffer cache buffers if we truncate, this
2128 * routine will resynchronize the count.
2132 hammer_update_rsv_databufs(hammer_inode_t ip)
2140 RB_FOREACH(bp, buf_rb_tree, &ip->vp->v_rbdirty_tree) {
2146 delta = n - ip->rsv_databufs;
2147 ip->rsv_databufs += delta;
2148 ip->hmp->rsv_databufs += delta;
2152 * dounlink - disconnect a directory entry
2154 * XXX whiteout support not really in yet
2157 hammer_dounlink(hammer_transaction_t trans, struct nchandle *nch,
2158 struct vnode *dvp, struct ucred *cred, int flags)
2160 struct namecache *ncp;
2163 struct hammer_cursor cursor;
2168 * Calculate the namekey and setup the key range for the scan. This
2169 * works kinda like a chained hash table where the lower 32 bits
2170 * of the namekey synthesize the chain.
2172 * The key range is inclusive of both key_beg and key_end.
2177 if (dip->flags & HAMMER_INODE_RO)
2180 namekey = hammer_directory_namekey(ncp->nc_name, ncp->nc_nlen);
2182 hammer_init_cursor(trans, &cursor, &dip->cache[0], dip);
2183 cursor.key_beg.localization = HAMMER_LOCALIZE_MISC;
2184 cursor.key_beg.obj_id = dip->obj_id;
2185 cursor.key_beg.key = namekey;
2186 cursor.key_beg.create_tid = 0;
2187 cursor.key_beg.delete_tid = 0;
2188 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
2189 cursor.key_beg.obj_type = 0;
2191 cursor.key_end = cursor.key_beg;
2192 cursor.key_end.key |= 0xFFFFFFFFULL;
2193 cursor.asof = dip->obj_asof;
2194 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
2197 * Scan all matching records (the chain), locate the one matching
2198 * the requested path component. info->last_error contains the
2199 * error code on search termination and could be 0, ENOENT, or
2202 * The hammer_ip_*() functions merge in-memory records with on-disk
2203 * records for the purposes of the search.
2205 error = hammer_ip_first(&cursor);
2207 while (error == 0) {
2208 error = hammer_ip_resolve_data(&cursor);
2211 nlen = cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF;
2213 if (ncp->nc_nlen == nlen &&
2214 bcmp(ncp->nc_name, cursor.data->entry.name, nlen) == 0) {
2217 error = hammer_ip_next(&cursor);
2221 * If all is ok we have to get the inode so we can adjust nlinks.
2222 * To avoid a deadlock with the flusher we must release the inode
2223 * lock on the directory when acquiring the inode for the entry.
2225 * If the target is a directory, it must be empty.
2228 hammer_unlock(&cursor.ip->lock);
2229 ip = hammer_get_inode(trans, &dip->cache[1],
2230 cursor.data->entry.obj_id,
2231 dip->hmp->asof, 0, &error);
2232 hammer_lock_sh(&cursor.ip->lock);
2233 if (error == ENOENT) {
2234 kprintf("obj_id %016llx\n", cursor.data->entry.obj_id);
2235 Debugger("ENOENT unlinking object that should exist");
2239 * If we are trying to remove a directory the directory must
2242 * WARNING: hammer_ip_check_directory_empty() may have to
2243 * terminate the cursor to avoid a deadlock. It is ok to
2244 * call hammer_done_cursor() twice.
2246 if (error == 0 && ip->ino_data.obj_type ==
2247 HAMMER_OBJTYPE_DIRECTORY) {
2248 error = hammer_ip_check_directory_empty(trans, ip);
2252 * Delete the directory entry.
2254 * WARNING: hammer_ip_del_directory() may have to terminate
2255 * the cursor to avoid a deadlock. It is ok to call
2256 * hammer_done_cursor() twice.
2259 error = hammer_ip_del_directory(trans, &cursor,
2262 hammer_done_cursor(&cursor);
2264 cache_setunresolved(nch);
2265 cache_setvp(nch, NULL);
2268 cache_inval_vp(ip->vp, CINV_DESTROY);
2271 hammer_rel_inode(ip, 0);
2273 hammer_done_cursor(&cursor);
2275 if (error == EDEADLK)
2281 /************************************************************************
2282 * FIFO AND SPECFS OPS *
2283 ************************************************************************
2288 hammer_vop_fifoclose (struct vop_close_args *ap)
2290 /* XXX update itimes */
2291 return (VOCALL(&fifo_vnode_vops, &ap->a_head));
2295 hammer_vop_fiforead (struct vop_read_args *ap)
2299 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
2300 /* XXX update access time */
2305 hammer_vop_fifowrite (struct vop_write_args *ap)
2309 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
2310 /* XXX update access time */
2315 hammer_vop_specclose (struct vop_close_args *ap)
2317 /* XXX update itimes */
2318 return (VOCALL(&spec_vnode_vops, &ap->a_head));
2322 hammer_vop_specread (struct vop_read_args *ap)
2324 /* XXX update access time */
2325 return (VOCALL(&spec_vnode_vops, &ap->a_head));
2329 hammer_vop_specwrite (struct vop_write_args *ap)
2331 /* XXX update last change time */
2332 return (VOCALL(&spec_vnode_vops, &ap->a_head));