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.56 2008/05/25 18:41: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_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 static int hammer_dounlink(hammer_transaction_t trans, struct nchandle *nch,
153 struct vnode *dvp, struct ucred *cred, int flags);
154 static int hammer_vop_strategy_read(struct vop_strategy_args *ap);
155 static int hammer_vop_strategy_write(struct vop_strategy_args *ap);
160 hammer_vop_vnoperate(struct vop_generic_args *)
162 return (VOCALL(&hammer_vnode_vops, ap));
167 * hammer_vop_fsync { vp, waitfor }
171 hammer_vop_fsync(struct vop_fsync_args *ap)
173 hammer_inode_t ip = VTOI(ap->a_vp);
175 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
176 vfsync(ap->a_vp, ap->a_waitfor, 1, NULL, NULL);
177 if (ap->a_waitfor == MNT_WAIT)
178 hammer_wait_inode(ip);
183 * hammer_vop_read { vp, uio, ioflag, cred }
187 hammer_vop_read(struct vop_read_args *ap)
189 struct hammer_transaction trans;
198 if (ap->a_vp->v_type != VREG)
202 seqcount = ap->a_ioflag >> 16;
204 hammer_start_transaction(&trans, ip->hmp);
207 * Access the data in HAMMER_BUFSIZE blocks via the buffer cache.
210 while (uio->uio_resid > 0 && uio->uio_offset < ip->ino_data.size) {
211 offset = uio->uio_offset & HAMMER_BUFMASK;
213 error = cluster_read(ap->a_vp, ip->ino_data.size,
214 uio->uio_offset - offset, HAMMER_BUFSIZE,
215 MAXBSIZE, seqcount, &bp);
217 error = bread(ap->a_vp, uio->uio_offset - offset,
218 HAMMER_BUFSIZE, &bp);
223 /* bp->b_flags |= B_CLUSTEROK; temporarily disabled */
224 n = HAMMER_BUFSIZE - offset;
225 if (n > uio->uio_resid)
227 if (n > ip->ino_data.size - uio->uio_offset)
228 n = (int)(ip->ino_data.size - uio->uio_offset);
229 error = uiomove((char *)bp->b_data + offset, n, uio);
236 if ((ip->flags & HAMMER_INODE_RO) == 0 &&
237 (ip->hmp->mp->mnt_flag & MNT_NOATIME) == 0) {
238 ip->ino_leaf.atime = trans.time;
239 hammer_modify_inode(&trans, ip, HAMMER_INODE_ITIMES);
241 hammer_done_transaction(&trans);
246 * hammer_vop_write { vp, uio, ioflag, cred }
250 hammer_vop_write(struct vop_write_args *ap)
252 struct hammer_transaction trans;
253 struct hammer_inode *ip;
262 if (ap->a_vp->v_type != VREG)
267 if (ip->flags & HAMMER_INODE_RO)
271 * Create a transaction to cover the operations we perform.
273 hammer_start_transaction(&trans, ip->hmp);
279 if (ap->a_ioflag & IO_APPEND)
280 uio->uio_offset = ip->ino_data.size;
283 * Check for illegal write offsets. Valid range is 0...2^63-1
285 if (uio->uio_offset < 0 || uio->uio_offset + uio->uio_resid <= 0) {
286 hammer_done_transaction(&trans);
291 * Access the data in HAMMER_BUFSIZE blocks via the buffer cache.
294 while (uio->uio_resid > 0) {
298 * Do not allow huge writes to deadlock the buffer cache
300 if ((++count & 15) == 0) {
302 if ((ap->a_ioflag & IO_NOBWILL) == 0)
304 vn_lock(ap->a_vp, LK_EXCLUSIVE|LK_RETRY);
307 offset = uio->uio_offset & HAMMER_BUFMASK;
308 n = HAMMER_BUFSIZE - offset;
309 if (n > uio->uio_resid)
311 if (uio->uio_offset + n > ip->ino_data.size) {
312 vnode_pager_setsize(ap->a_vp, uio->uio_offset + n);
316 if (uio->uio_segflg == UIO_NOCOPY) {
318 * Issuing a write with the same data backing the
319 * buffer. Instantiate the buffer to collect the
320 * backing vm pages, then read-in any missing bits.
322 * This case is used by vop_stdputpages().
324 bp = getblk(ap->a_vp, uio->uio_offset - offset,
325 HAMMER_BUFSIZE, GETBLK_BHEAVY, 0);
326 if ((bp->b_flags & B_CACHE) == 0) {
328 error = bread(ap->a_vp,
329 uio->uio_offset - offset,
330 HAMMER_BUFSIZE, &bp);
332 } else if (offset == 0 && uio->uio_resid >= HAMMER_BUFSIZE) {
334 * Even though we are entirely overwriting the buffer
335 * we may still have to zero it out to avoid a
336 * mmap/write visibility issue.
338 bp = getblk(ap->a_vp, uio->uio_offset - offset,
339 HAMMER_BUFSIZE, GETBLK_BHEAVY, 0);
340 if ((bp->b_flags & B_CACHE) == 0)
342 } else if (uio->uio_offset - offset >= ip->ino_data.size) {
344 * If the base offset of the buffer is beyond the
345 * file EOF, we don't have to issue a read.
347 bp = getblk(ap->a_vp, uio->uio_offset - offset,
348 HAMMER_BUFSIZE, GETBLK_BHEAVY, 0);
352 * Partial overwrite, read in any missing bits then
353 * replace the portion being written.
355 error = bread(ap->a_vp, uio->uio_offset - offset,
356 HAMMER_BUFSIZE, &bp);
361 error = uiomove((char *)bp->b_data + offset, n, uio);
364 * If we screwed up we have to undo any VM size changes we
370 vtruncbuf(ap->a_vp, ip->ino_data.size,
375 /* bp->b_flags |= B_CLUSTEROK; temporarily disabled */
376 if (ip->ino_data.size < uio->uio_offset) {
377 ip->ino_data.size = uio->uio_offset;
378 flags = HAMMER_INODE_DDIRTY;
379 vnode_pager_setsize(ap->a_vp, ip->ino_data.size);
383 ip->ino_data.mtime = trans.time;
384 flags |= HAMMER_INODE_ITIMES | HAMMER_INODE_BUFS;
385 flags |= HAMMER_INODE_DDIRTY; /* XXX mtime */
386 hammer_modify_inode(&trans, ip, flags);
388 if (ap->a_ioflag & IO_SYNC) {
390 } else if (ap->a_ioflag & IO_DIRECT) {
393 } else if ((ap->a_ioflag >> 16) == IO_SEQMAX &&
394 (uio->uio_offset & HAMMER_BUFMASK) == 0) {
396 * XXX HAMMER can only fsync the whole inode,
397 * doing it on every buffer would be a bad idea.
400 * If seqcount indicates sequential operation and
401 * we just finished filling a buffer, push it out
402 * now to prevent the buffer cache from becoming
403 * too full, which would trigger non-optimal
412 hammer_done_transaction(&trans);
417 * hammer_vop_access { vp, mode, cred }
421 hammer_vop_access(struct vop_access_args *ap)
423 struct hammer_inode *ip = VTOI(ap->a_vp);
428 uid = hammer_to_unix_xid(&ip->ino_data.uid);
429 gid = hammer_to_unix_xid(&ip->ino_data.gid);
431 error = vop_helper_access(ap, uid, gid, ip->ino_data.mode,
432 ip->ino_data.uflags);
437 * hammer_vop_advlock { vp, id, op, fl, flags }
441 hammer_vop_advlock(struct vop_advlock_args *ap)
443 struct hammer_inode *ip = VTOI(ap->a_vp);
445 return (lf_advlock(ap, &ip->advlock, ip->ino_data.size));
449 * hammer_vop_close { vp, fflag }
453 hammer_vop_close(struct vop_close_args *ap)
455 return (vop_stdclose(ap));
459 * hammer_vop_ncreate { nch, dvp, vpp, cred, vap }
461 * The operating system has already ensured that the directory entry
462 * does not exist and done all appropriate namespace locking.
466 hammer_vop_ncreate(struct vop_ncreate_args *ap)
468 struct hammer_transaction trans;
469 struct hammer_inode *dip;
470 struct hammer_inode *nip;
471 struct nchandle *nch;
475 dip = VTOI(ap->a_dvp);
477 if (dip->flags & HAMMER_INODE_RO)
481 * Create a transaction to cover the operations we perform.
483 hammer_start_transaction(&trans, dip->hmp);
486 * Create a new filesystem object of the requested type. The
487 * returned inode will be referenced and shared-locked to prevent
488 * it from being moved to the flusher.
491 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred, dip, &nip);
493 hkprintf("hammer_create_inode error %d\n", error);
494 hammer_done_transaction(&trans);
500 * Add the new filesystem object to the directory. This will also
501 * bump the inode's link count.
503 error = hammer_ip_add_directory(&trans, dip, nch->ncp, nip);
505 hkprintf("hammer_ip_add_directory error %d\n", error);
511 hammer_rel_inode(nip, 0);
512 hammer_done_transaction(&trans);
515 error = hammer_get_vnode(nip, ap->a_vpp);
516 hammer_done_transaction(&trans);
517 hammer_rel_inode(nip, 0);
519 cache_setunresolved(ap->a_nch);
520 cache_setvp(ap->a_nch, *ap->a_vpp);
527 * hammer_vop_getattr { vp, vap }
529 * Retrieve an inode's attribute information. When accessing inodes
530 * historically we fake the atime field to ensure consistent results.
531 * The atime field is stored in the B-Tree element and allowed to be
532 * updated without cycling the element.
536 hammer_vop_getattr(struct vop_getattr_args *ap)
538 struct hammer_inode *ip = VTOI(ap->a_vp);
539 struct vattr *vap = ap->a_vap;
542 if (cache_check_fsmid_vp(ap->a_vp, &ip->fsmid) &&
543 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0 &&
548 hammer_itimes(ap->a_vp);
551 vap->va_fsid = ip->hmp->fsid_udev;
552 vap->va_fileid = ip->ino_leaf.base.obj_id;
553 vap->va_mode = ip->ino_data.mode;
554 vap->va_nlink = ip->ino_data.nlinks;
555 vap->va_uid = hammer_to_unix_xid(&ip->ino_data.uid);
556 vap->va_gid = hammer_to_unix_xid(&ip->ino_data.gid);
559 vap->va_size = ip->ino_data.size;
560 if (ip->flags & HAMMER_INODE_RO)
561 hammer_to_timespec(ip->ino_data.mtime, &vap->va_atime);
563 hammer_to_timespec(ip->ino_leaf.atime, &vap->va_atime);
564 hammer_to_timespec(ip->ino_data.mtime, &vap->va_mtime);
565 hammer_to_timespec(ip->ino_data.ctime, &vap->va_ctime);
566 vap->va_flags = ip->ino_data.uflags;
567 vap->va_gen = 1; /* hammer inums are unique for all time */
568 vap->va_blocksize = HAMMER_BUFSIZE;
569 vap->va_bytes = (ip->ino_data.size + 63) & ~63;
570 vap->va_type = hammer_get_vnode_type(ip->ino_data.obj_type);
571 vap->va_filerev = 0; /* XXX */
572 /* mtime uniquely identifies any adjustments made to the file */
573 vap->va_fsmid = ip->ino_data.mtime;
574 vap->va_uid_uuid = ip->ino_data.uid;
575 vap->va_gid_uuid = ip->ino_data.gid;
576 vap->va_fsid_uuid = ip->hmp->fsid;
577 vap->va_vaflags = VA_UID_UUID_VALID | VA_GID_UUID_VALID |
580 switch (ip->ino_data.obj_type) {
581 case HAMMER_OBJTYPE_CDEV:
582 case HAMMER_OBJTYPE_BDEV:
583 vap->va_rmajor = ip->ino_data.rmajor;
584 vap->va_rminor = ip->ino_data.rminor;
594 * hammer_vop_nresolve { nch, dvp, cred }
596 * Locate the requested directory entry.
600 hammer_vop_nresolve(struct vop_nresolve_args *ap)
602 struct hammer_transaction trans;
603 struct namecache *ncp;
607 struct hammer_cursor cursor;
617 * Misc initialization, plus handle as-of name extensions. Look for
618 * the '@@' extension. Note that as-of files and directories cannot
621 dip = VTOI(ap->a_dvp);
622 ncp = ap->a_nch->ncp;
623 asof = dip->obj_asof;
627 hammer_simple_transaction(&trans, dip->hmp);
629 for (i = 0; i < nlen; ++i) {
630 if (ncp->nc_name[i] == '@' && ncp->nc_name[i+1] == '@') {
631 asof = hammer_str_to_tid(ncp->nc_name + i + 2);
632 flags |= HAMMER_INODE_RO;
639 * If there is no path component the time extension is relative to
643 ip = hammer_get_inode(&trans, &dip->cache[1], dip->obj_id,
644 asof, flags, &error);
646 error = hammer_get_vnode(ip, &vp);
647 hammer_rel_inode(ip, 0);
653 cache_setvp(ap->a_nch, vp);
660 * Calculate the namekey and setup the key range for the scan. This
661 * works kinda like a chained hash table where the lower 32 bits
662 * of the namekey synthesize the chain.
664 * The key range is inclusive of both key_beg and key_end.
666 namekey = hammer_directory_namekey(ncp->nc_name, nlen);
668 error = hammer_init_cursor(&trans, &cursor, &dip->cache[0], dip);
669 cursor.key_beg.localization = HAMMER_LOCALIZE_MISC;
670 cursor.key_beg.obj_id = dip->obj_id;
671 cursor.key_beg.key = namekey;
672 cursor.key_beg.create_tid = 0;
673 cursor.key_beg.delete_tid = 0;
674 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
675 cursor.key_beg.obj_type = 0;
677 cursor.key_end = cursor.key_beg;
678 cursor.key_end.key |= 0xFFFFFFFFULL;
680 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
683 * Scan all matching records (the chain), locate the one matching
684 * the requested path component.
686 * The hammer_ip_*() functions merge in-memory records with on-disk
687 * records for the purposes of the search.
692 error = hammer_ip_first(&cursor);
694 error = hammer_ip_resolve_data(&cursor);
697 if (nlen == cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF &&
698 bcmp(ncp->nc_name, cursor.data->entry.name, nlen) == 0) {
699 obj_id = cursor.data->entry.obj_id;
702 error = hammer_ip_next(&cursor);
705 hammer_done_cursor(&cursor);
707 ip = hammer_get_inode(&trans, &dip->cache[1],
708 obj_id, asof, flags, &error);
710 error = hammer_get_vnode(ip, &vp);
711 hammer_rel_inode(ip, 0);
717 cache_setvp(ap->a_nch, vp);
720 } else if (error == ENOENT) {
721 cache_setvp(ap->a_nch, NULL);
724 hammer_done_transaction(&trans);
729 * hammer_vop_nlookupdotdot { dvp, vpp, cred }
731 * Locate the parent directory of a directory vnode.
733 * dvp is referenced but not locked. *vpp must be returned referenced and
734 * locked. A parent_obj_id of 0 does not necessarily indicate that we are
735 * at the root, instead it could indicate that the directory we were in was
738 * NOTE: as-of sequences are not linked into the directory structure. If
739 * we are at the root with a different asof then the mount point, reload
740 * the same directory with the mount point's asof. I'm not sure what this
741 * will do to NFS. We encode ASOF stamps in NFS file handles so it might not
742 * get confused, but it hasn't been tested.
746 hammer_vop_nlookupdotdot(struct vop_nlookupdotdot_args *ap)
748 struct hammer_transaction trans;
749 struct hammer_inode *dip;
750 struct hammer_inode *ip;
751 int64_t parent_obj_id;
755 dip = VTOI(ap->a_dvp);
756 asof = dip->obj_asof;
757 parent_obj_id = dip->ino_data.parent_obj_id;
759 if (parent_obj_id == 0) {
760 if (dip->obj_id == HAMMER_OBJID_ROOT &&
761 asof != dip->hmp->asof) {
762 parent_obj_id = dip->obj_id;
763 asof = dip->hmp->asof;
764 *ap->a_fakename = kmalloc(19, M_TEMP, M_WAITOK);
765 ksnprintf(*ap->a_fakename, 19, "0x%016llx",
773 hammer_simple_transaction(&trans, dip->hmp);
775 ip = hammer_get_inode(&trans, &dip->cache[1], parent_obj_id,
776 asof, dip->flags, &error);
778 error = hammer_get_vnode(ip, ap->a_vpp);
779 hammer_rel_inode(ip, 0);
783 hammer_done_transaction(&trans);
788 * hammer_vop_nlink { nch, dvp, vp, cred }
792 hammer_vop_nlink(struct vop_nlink_args *ap)
794 struct hammer_transaction trans;
795 struct hammer_inode *dip;
796 struct hammer_inode *ip;
797 struct nchandle *nch;
801 dip = VTOI(ap->a_dvp);
804 if (dip->flags & HAMMER_INODE_RO)
806 if (ip->flags & HAMMER_INODE_RO)
810 * Create a transaction to cover the operations we perform.
812 hammer_start_transaction(&trans, dip->hmp);
815 * Add the filesystem object to the directory. Note that neither
816 * dip nor ip are referenced or locked, but their vnodes are
817 * referenced. This function will bump the inode's link count.
819 error = hammer_ip_add_directory(&trans, dip, nch->ncp, ip);
825 cache_setunresolved(nch);
826 cache_setvp(nch, ap->a_vp);
828 hammer_done_transaction(&trans);
833 * hammer_vop_nmkdir { nch, dvp, vpp, cred, vap }
835 * The operating system has already ensured that the directory entry
836 * does not exist and done all appropriate namespace locking.
840 hammer_vop_nmkdir(struct vop_nmkdir_args *ap)
842 struct hammer_transaction trans;
843 struct hammer_inode *dip;
844 struct hammer_inode *nip;
845 struct nchandle *nch;
849 dip = VTOI(ap->a_dvp);
851 if (dip->flags & HAMMER_INODE_RO)
855 * Create a transaction to cover the operations we perform.
857 hammer_start_transaction(&trans, dip->hmp);
860 * Create a new filesystem object of the requested type. The
861 * returned inode will be referenced but not locked.
863 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred, dip, &nip);
865 hkprintf("hammer_mkdir error %d\n", error);
866 hammer_done_transaction(&trans);
871 * Add the new filesystem object to the directory. This will also
872 * bump the inode's link count.
874 error = hammer_ip_add_directory(&trans, dip, nch->ncp, nip);
876 hkprintf("hammer_mkdir (add) error %d\n", error);
882 hammer_rel_inode(nip, 0);
885 error = hammer_get_vnode(nip, ap->a_vpp);
886 hammer_rel_inode(nip, 0);
888 cache_setunresolved(ap->a_nch);
889 cache_setvp(ap->a_nch, *ap->a_vpp);
892 hammer_done_transaction(&trans);
897 * hammer_vop_nmknod { nch, dvp, vpp, cred, vap }
899 * The operating system has already ensured that the directory entry
900 * does not exist and done all appropriate namespace locking.
904 hammer_vop_nmknod(struct vop_nmknod_args *ap)
906 struct hammer_transaction trans;
907 struct hammer_inode *dip;
908 struct hammer_inode *nip;
909 struct nchandle *nch;
913 dip = VTOI(ap->a_dvp);
915 if (dip->flags & HAMMER_INODE_RO)
919 * Create a transaction to cover the operations we perform.
921 hammer_start_transaction(&trans, dip->hmp);
924 * Create a new filesystem object of the requested type. The
925 * returned inode will be referenced but not locked.
927 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred, dip, &nip);
929 hammer_done_transaction(&trans);
935 * Add the new filesystem object to the directory. This will also
936 * bump the inode's link count.
938 error = hammer_ip_add_directory(&trans, dip, nch->ncp, nip);
944 hammer_rel_inode(nip, 0);
947 error = hammer_get_vnode(nip, ap->a_vpp);
948 hammer_rel_inode(nip, 0);
950 cache_setunresolved(ap->a_nch);
951 cache_setvp(ap->a_nch, *ap->a_vpp);
954 hammer_done_transaction(&trans);
959 * hammer_vop_open { vp, mode, cred, fp }
963 hammer_vop_open(struct vop_open_args *ap)
965 if ((ap->a_mode & FWRITE) && (VTOI(ap->a_vp)->flags & HAMMER_INODE_RO))
968 return(vop_stdopen(ap));
972 * hammer_vop_pathconf { vp, name, retval }
976 hammer_vop_pathconf(struct vop_pathconf_args *ap)
982 * hammer_vop_print { vp }
986 hammer_vop_print(struct vop_print_args *ap)
992 * hammer_vop_readdir { vp, uio, cred, *eofflag, *ncookies, off_t **cookies }
996 hammer_vop_readdir(struct vop_readdir_args *ap)
998 struct hammer_transaction trans;
999 struct hammer_cursor cursor;
1000 struct hammer_inode *ip;
1002 hammer_base_elm_t base;
1010 ip = VTOI(ap->a_vp);
1012 saveoff = uio->uio_offset;
1014 if (ap->a_ncookies) {
1015 ncookies = uio->uio_resid / 16 + 1;
1016 if (ncookies > 1024)
1018 cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
1026 hammer_simple_transaction(&trans, ip->hmp);
1029 * Handle artificial entries
1033 r = vop_write_dirent(&error, uio, ip->obj_id, DT_DIR, 1, ".");
1037 cookies[cookie_index] = saveoff;
1040 if (cookie_index == ncookies)
1044 if (ip->ino_data.parent_obj_id) {
1045 r = vop_write_dirent(&error, uio,
1046 ip->ino_data.parent_obj_id,
1049 r = vop_write_dirent(&error, uio,
1050 ip->obj_id, DT_DIR, 2, "..");
1055 cookies[cookie_index] = saveoff;
1058 if (cookie_index == ncookies)
1063 * Key range (begin and end inclusive) to scan. Directory keys
1064 * directly translate to a 64 bit 'seek' position.
1066 hammer_init_cursor(&trans, &cursor, &ip->cache[0], ip);
1067 cursor.key_beg.localization = HAMMER_LOCALIZE_MISC;
1068 cursor.key_beg.obj_id = ip->obj_id;
1069 cursor.key_beg.create_tid = 0;
1070 cursor.key_beg.delete_tid = 0;
1071 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1072 cursor.key_beg.obj_type = 0;
1073 cursor.key_beg.key = saveoff;
1075 cursor.key_end = cursor.key_beg;
1076 cursor.key_end.key = HAMMER_MAX_KEY;
1077 cursor.asof = ip->obj_asof;
1078 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
1080 error = hammer_ip_first(&cursor);
1082 while (error == 0) {
1083 error = hammer_ip_resolve_data(&cursor);
1086 base = &cursor.leaf->base;
1087 saveoff = base->key;
1088 KKASSERT(cursor.leaf->data_len > HAMMER_ENTRY_NAME_OFF);
1090 if (base->obj_id != ip->obj_id)
1091 panic("readdir: bad record at %p", cursor.node);
1093 r = vop_write_dirent(
1094 &error, uio, cursor.data->entry.obj_id,
1095 hammer_get_dtype(cursor.leaf->base.obj_type),
1096 cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF ,
1097 (void *)cursor.data->entry.name);
1102 cookies[cookie_index] = base->key;
1104 if (cookie_index == ncookies)
1106 error = hammer_ip_next(&cursor);
1108 hammer_done_cursor(&cursor);
1111 hammer_done_transaction(&trans);
1114 *ap->a_eofflag = (error == ENOENT);
1115 uio->uio_offset = saveoff;
1116 if (error && cookie_index == 0) {
1117 if (error == ENOENT)
1120 kfree(cookies, M_TEMP);
1121 *ap->a_ncookies = 0;
1122 *ap->a_cookies = NULL;
1125 if (error == ENOENT)
1128 *ap->a_ncookies = cookie_index;
1129 *ap->a_cookies = cookies;
1136 * hammer_vop_readlink { vp, uio, cred }
1140 hammer_vop_readlink(struct vop_readlink_args *ap)
1142 struct hammer_transaction trans;
1143 struct hammer_cursor cursor;
1144 struct hammer_inode *ip;
1147 ip = VTOI(ap->a_vp);
1150 * Shortcut if the symlink data was stuffed into ino_data.
1152 if (ip->ino_data.size <= HAMMER_INODE_BASESYMLEN) {
1153 error = uiomove(ip->ino_data.ext.symlink,
1154 ip->ino_data.size, ap->a_uio);
1161 hammer_simple_transaction(&trans, ip->hmp);
1162 hammer_init_cursor(&trans, &cursor, &ip->cache[0], ip);
1165 * Key range (begin and end inclusive) to scan. Directory keys
1166 * directly translate to a 64 bit 'seek' position.
1168 cursor.key_beg.localization = HAMMER_LOCALIZE_MISC; /* XXX */
1169 cursor.key_beg.obj_id = ip->obj_id;
1170 cursor.key_beg.create_tid = 0;
1171 cursor.key_beg.delete_tid = 0;
1172 cursor.key_beg.rec_type = HAMMER_RECTYPE_FIX;
1173 cursor.key_beg.obj_type = 0;
1174 cursor.key_beg.key = HAMMER_FIXKEY_SYMLINK;
1175 cursor.asof = ip->obj_asof;
1176 cursor.flags |= HAMMER_CURSOR_ASOF;
1178 error = hammer_ip_lookup(&cursor);
1180 error = hammer_ip_resolve_data(&cursor);
1182 KKASSERT(cursor.leaf->data_len >=
1183 HAMMER_SYMLINK_NAME_OFF);
1184 error = uiomove(cursor.data->symlink.name,
1185 cursor.leaf->data_len -
1186 HAMMER_SYMLINK_NAME_OFF,
1190 hammer_done_cursor(&cursor);
1191 hammer_done_transaction(&trans);
1196 * hammer_vop_nremove { nch, dvp, cred }
1200 hammer_vop_nremove(struct vop_nremove_args *ap)
1202 struct hammer_transaction trans;
1205 hammer_start_transaction(&trans, VTOI(ap->a_dvp)->hmp);
1206 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp, ap->a_cred, 0);
1207 hammer_done_transaction(&trans);
1213 * hammer_vop_nrename { fnch, tnch, fdvp, tdvp, cred }
1217 hammer_vop_nrename(struct vop_nrename_args *ap)
1219 struct hammer_transaction trans;
1220 struct namecache *fncp;
1221 struct namecache *tncp;
1222 struct hammer_inode *fdip;
1223 struct hammer_inode *tdip;
1224 struct hammer_inode *ip;
1225 struct hammer_cursor cursor;
1229 fdip = VTOI(ap->a_fdvp);
1230 tdip = VTOI(ap->a_tdvp);
1231 fncp = ap->a_fnch->ncp;
1232 tncp = ap->a_tnch->ncp;
1233 ip = VTOI(fncp->nc_vp);
1234 KKASSERT(ip != NULL);
1236 if (fdip->flags & HAMMER_INODE_RO)
1238 if (tdip->flags & HAMMER_INODE_RO)
1240 if (ip->flags & HAMMER_INODE_RO)
1243 hammer_start_transaction(&trans, fdip->hmp);
1246 * Remove tncp from the target directory and then link ip as
1247 * tncp. XXX pass trans to dounlink
1249 * Force the inode sync-time to match the transaction so it is
1250 * in-sync with the creation of the target directory entry.
1252 error = hammer_dounlink(&trans, ap->a_tnch, ap->a_tdvp, ap->a_cred, 0);
1253 if (error == 0 || error == ENOENT) {
1254 error = hammer_ip_add_directory(&trans, tdip, tncp, ip);
1256 ip->ino_data.parent_obj_id = tdip->obj_id;
1257 hammer_modify_inode(&trans, ip, HAMMER_INODE_DDIRTY);
1261 goto failed; /* XXX */
1264 * Locate the record in the originating directory and remove it.
1266 * Calculate the namekey and setup the key range for the scan. This
1267 * works kinda like a chained hash table where the lower 32 bits
1268 * of the namekey synthesize the chain.
1270 * The key range is inclusive of both key_beg and key_end.
1272 namekey = hammer_directory_namekey(fncp->nc_name, fncp->nc_nlen);
1274 hammer_init_cursor(&trans, &cursor, &fdip->cache[0], fdip);
1275 cursor.key_beg.localization = HAMMER_LOCALIZE_MISC;
1276 cursor.key_beg.obj_id = fdip->obj_id;
1277 cursor.key_beg.key = namekey;
1278 cursor.key_beg.create_tid = 0;
1279 cursor.key_beg.delete_tid = 0;
1280 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1281 cursor.key_beg.obj_type = 0;
1283 cursor.key_end = cursor.key_beg;
1284 cursor.key_end.key |= 0xFFFFFFFFULL;
1285 cursor.asof = fdip->obj_asof;
1286 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
1289 * Scan all matching records (the chain), locate the one matching
1290 * the requested path component.
1292 * The hammer_ip_*() functions merge in-memory records with on-disk
1293 * records for the purposes of the search.
1295 error = hammer_ip_first(&cursor);
1296 while (error == 0) {
1297 if (hammer_ip_resolve_data(&cursor) != 0)
1299 nlen = cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF;
1301 if (fncp->nc_nlen == nlen &&
1302 bcmp(fncp->nc_name, cursor.data->entry.name, nlen) == 0) {
1305 error = hammer_ip_next(&cursor);
1309 * If all is ok we have to get the inode so we can adjust nlinks.
1311 * WARNING: hammer_ip_del_directory() may have to terminate the
1312 * cursor to avoid a recursion. It's ok to call hammer_done_cursor()
1316 error = hammer_ip_del_directory(&trans, &cursor, fdip, ip);
1319 * XXX A deadlock here will break rename's atomicy for the purposes
1320 * of crash recovery.
1322 if (error == EDEADLK) {
1323 hammer_done_cursor(&cursor);
1328 * Cleanup and tell the kernel that the rename succeeded.
1330 hammer_done_cursor(&cursor);
1332 cache_rename(ap->a_fnch, ap->a_tnch);
1335 hammer_done_transaction(&trans);
1340 * hammer_vop_nrmdir { nch, dvp, cred }
1344 hammer_vop_nrmdir(struct vop_nrmdir_args *ap)
1346 struct hammer_transaction trans;
1349 hammer_start_transaction(&trans, VTOI(ap->a_dvp)->hmp);
1350 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp, ap->a_cred, 0);
1351 hammer_done_transaction(&trans);
1357 * hammer_vop_setattr { vp, vap, cred }
1361 hammer_vop_setattr(struct vop_setattr_args *ap)
1363 struct hammer_transaction trans;
1365 struct hammer_inode *ip;
1373 ip = ap->a_vp->v_data;
1376 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
1378 if (ip->flags & HAMMER_INODE_RO)
1381 hammer_start_transaction(&trans, ip->hmp);
1384 if (vap->va_flags != VNOVAL) {
1385 flags = ip->ino_data.uflags;
1386 error = vop_helper_setattr_flags(&flags, vap->va_flags,
1387 hammer_to_unix_xid(&ip->ino_data.uid),
1390 if (ip->ino_data.uflags != flags) {
1391 ip->ino_data.uflags = flags;
1392 modflags |= HAMMER_INODE_DDIRTY;
1394 if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) {
1401 if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) {
1405 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
1406 mode_t cur_mode = ip->ino_data.mode;
1407 uid_t cur_uid = hammer_to_unix_xid(&ip->ino_data.uid);
1408 gid_t cur_gid = hammer_to_unix_xid(&ip->ino_data.gid);
1412 error = vop_helper_chown(ap->a_vp, vap->va_uid, vap->va_gid,
1414 &cur_uid, &cur_gid, &cur_mode);
1416 hammer_guid_to_uuid(&uuid_uid, cur_uid);
1417 hammer_guid_to_uuid(&uuid_gid, cur_gid);
1418 if (bcmp(&uuid_uid, &ip->ino_data.uid,
1419 sizeof(uuid_uid)) ||
1420 bcmp(&uuid_gid, &ip->ino_data.gid,
1421 sizeof(uuid_gid)) ||
1422 ip->ino_data.mode != cur_mode
1424 ip->ino_data.uid = uuid_uid;
1425 ip->ino_data.gid = uuid_gid;
1426 ip->ino_data.mode = cur_mode;
1428 modflags |= HAMMER_INODE_DDIRTY;
1431 while (vap->va_size != VNOVAL && ip->ino_data.size != vap->va_size) {
1432 switch(ap->a_vp->v_type) {
1434 if (vap->va_size == ip->ino_data.size)
1437 * XXX break atomicy, we can deadlock the backend
1438 * if we do not release the lock. Probably not a
1441 if (vap->va_size < ip->ino_data.size) {
1442 vtruncbuf(ap->a_vp, vap->va_size,
1446 vnode_pager_setsize(ap->a_vp, vap->va_size);
1449 ip->ino_data.size = vap->va_size;
1450 modflags |= HAMMER_INODE_DDIRTY;
1451 aligned_size = (vap->va_size + HAMMER_BUFMASK) &
1455 * on-media truncation is cached in the inode until
1456 * the inode is synchronized.
1459 if ((ip->flags & HAMMER_INODE_TRUNCATED) == 0) {
1460 ip->flags |= HAMMER_INODE_TRUNCATED;
1461 ip->trunc_off = vap->va_size;
1462 } else if (ip->trunc_off > vap->va_size) {
1463 ip->trunc_off = vap->va_size;
1468 * If truncating we have to clean out a portion of
1469 * the last block on-disk. We do this in the
1470 * front-end buffer cache.
1472 if (truncating && vap->va_size < aligned_size) {
1476 offset = vap->va_size & HAMMER_BUFMASK;
1477 error = bread(ap->a_vp,
1478 aligned_size - HAMMER_BUFSIZE,
1479 HAMMER_BUFSIZE, &bp);
1481 bzero(bp->b_data + offset,
1482 HAMMER_BUFSIZE - offset);
1490 if ((ip->flags & HAMMER_INODE_TRUNCATED) == 0) {
1491 ip->flags |= HAMMER_INODE_TRUNCATED;
1492 ip->trunc_off = vap->va_size;
1493 } else if (ip->trunc_off > vap->va_size) {
1494 ip->trunc_off = vap->va_size;
1496 ip->ino_data.size = vap->va_size;
1497 modflags |= HAMMER_INODE_DDIRTY;
1505 if (vap->va_atime.tv_sec != VNOVAL) {
1506 ip->ino_leaf.atime =
1507 hammer_timespec_to_transid(&vap->va_atime);
1508 modflags |= HAMMER_INODE_ITIMES;
1510 if (vap->va_mtime.tv_sec != VNOVAL) {
1511 ip->ino_data.mtime =
1512 hammer_timespec_to_transid(&vap->va_mtime);
1513 modflags |= HAMMER_INODE_ITIMES;
1514 modflags |= HAMMER_INODE_DDIRTY; /* XXX mtime */
1516 if (vap->va_mode != (mode_t)VNOVAL) {
1517 mode_t cur_mode = ip->ino_data.mode;
1518 uid_t cur_uid = hammer_to_unix_xid(&ip->ino_data.uid);
1519 gid_t cur_gid = hammer_to_unix_xid(&ip->ino_data.gid);
1521 error = vop_helper_chmod(ap->a_vp, vap->va_mode, ap->a_cred,
1522 cur_uid, cur_gid, &cur_mode);
1523 if (error == 0 && ip->ino_data.mode != cur_mode) {
1524 ip->ino_data.mode = cur_mode;
1525 modflags |= HAMMER_INODE_DDIRTY;
1530 hammer_modify_inode(&trans, ip, modflags);
1531 hammer_done_transaction(&trans);
1536 * hammer_vop_nsymlink { nch, dvp, vpp, cred, vap, target }
1540 hammer_vop_nsymlink(struct vop_nsymlink_args *ap)
1542 struct hammer_transaction trans;
1543 struct hammer_inode *dip;
1544 struct hammer_inode *nip;
1545 struct nchandle *nch;
1546 hammer_record_t record;
1550 ap->a_vap->va_type = VLNK;
1553 dip = VTOI(ap->a_dvp);
1555 if (dip->flags & HAMMER_INODE_RO)
1559 * Create a transaction to cover the operations we perform.
1561 hammer_start_transaction(&trans, dip->hmp);
1564 * Create a new filesystem object of the requested type. The
1565 * returned inode will be referenced but not locked.
1568 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred, dip, &nip);
1570 hammer_done_transaction(&trans);
1576 * Add a record representing the symlink. symlink stores the link
1577 * as pure data, not a string, and is no \0 terminated.
1580 bytes = strlen(ap->a_target);
1582 if (bytes <= HAMMER_INODE_BASESYMLEN) {
1583 bcopy(ap->a_target, nip->ino_data.ext.symlink, bytes);
1585 record = hammer_alloc_mem_record(nip, bytes);
1586 record->type = HAMMER_MEM_RECORD_GENERAL;
1588 record->leaf.base.localization = HAMMER_LOCALIZE_MISC;
1589 record->leaf.base.key = HAMMER_FIXKEY_SYMLINK;
1590 record->leaf.base.rec_type = HAMMER_RECTYPE_FIX;
1591 record->leaf.data_len = bytes;
1592 KKASSERT(HAMMER_SYMLINK_NAME_OFF == 0);
1593 bcopy(ap->a_target, record->data->symlink.name, bytes);
1594 error = hammer_ip_add_record(&trans, record);
1598 * Set the file size to the length of the link.
1601 nip->ino_data.size = bytes;
1602 hammer_modify_inode(&trans, nip, HAMMER_INODE_DDIRTY);
1606 error = hammer_ip_add_directory(&trans, dip, nch->ncp, nip);
1612 hammer_rel_inode(nip, 0);
1615 error = hammer_get_vnode(nip, ap->a_vpp);
1616 hammer_rel_inode(nip, 0);
1618 cache_setunresolved(ap->a_nch);
1619 cache_setvp(ap->a_nch, *ap->a_vpp);
1622 hammer_done_transaction(&trans);
1627 * hammer_vop_nwhiteout { nch, dvp, cred, flags }
1631 hammer_vop_nwhiteout(struct vop_nwhiteout_args *ap)
1633 struct hammer_transaction trans;
1636 hammer_start_transaction(&trans, VTOI(ap->a_dvp)->hmp);
1637 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp,
1638 ap->a_cred, ap->a_flags);
1639 hammer_done_transaction(&trans);
1645 * hammer_vop_ioctl { vp, command, data, fflag, cred }
1649 hammer_vop_ioctl(struct vop_ioctl_args *ap)
1651 struct hammer_inode *ip = ap->a_vp->v_data;
1653 return(hammer_ioctl(ip, ap->a_command, ap->a_data,
1654 ap->a_fflag, ap->a_cred));
1659 hammer_vop_mountctl(struct vop_mountctl_args *ap)
1664 mp = ap->a_head.a_ops->head.vv_mount;
1667 case MOUNTCTL_SET_EXPORT:
1668 if (ap->a_ctllen != sizeof(struct export_args))
1670 error = hammer_vfs_export(mp, ap->a_op,
1671 (const struct export_args *)ap->a_ctl);
1674 error = journal_mountctl(ap);
1681 * hammer_vop_strategy { vp, bio }
1683 * Strategy call, used for regular file read & write only. Note that the
1684 * bp may represent a cluster.
1686 * To simplify operation and allow better optimizations in the future,
1687 * this code does not make any assumptions with regards to buffer alignment
1692 hammer_vop_strategy(struct vop_strategy_args *ap)
1697 bp = ap->a_bio->bio_buf;
1701 error = hammer_vop_strategy_read(ap);
1704 error = hammer_vop_strategy_write(ap);
1707 bp->b_error = error = EINVAL;
1708 bp->b_flags |= B_ERROR;
1716 * Read from a regular file. Iterate the related records and fill in the
1717 * BIO/BUF. Gaps are zero-filled.
1719 * The support code in hammer_object.c should be used to deal with mixed
1720 * in-memory and on-disk records.
1726 hammer_vop_strategy_read(struct vop_strategy_args *ap)
1728 struct hammer_transaction trans;
1729 struct hammer_inode *ip;
1730 struct hammer_cursor cursor;
1731 hammer_base_elm_t base;
1744 ip = ap->a_vp->v_data;
1746 hammer_simple_transaction(&trans, ip->hmp);
1747 hammer_init_cursor(&trans, &cursor, &ip->cache[0], ip);
1750 * Key range (begin and end inclusive) to scan. Note that the key's
1751 * stored in the actual records represent BASE+LEN, not BASE. The
1752 * first record containing bio_offset will have a key > bio_offset.
1754 cursor.key_beg.localization = HAMMER_LOCALIZE_MISC;
1755 cursor.key_beg.obj_id = ip->obj_id;
1756 cursor.key_beg.create_tid = 0;
1757 cursor.key_beg.delete_tid = 0;
1758 cursor.key_beg.obj_type = 0;
1759 cursor.key_beg.key = bio->bio_offset + 1;
1760 cursor.asof = ip->obj_asof;
1761 cursor.flags |= HAMMER_CURSOR_ASOF | HAMMER_CURSOR_DATAEXTOK;
1763 cursor.key_end = cursor.key_beg;
1764 KKASSERT(ip->ino_data.obj_type == HAMMER_OBJTYPE_REGFILE);
1766 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_DBFILE) {
1767 cursor.key_beg.rec_type = HAMMER_RECTYPE_DB;
1768 cursor.key_end.rec_type = HAMMER_RECTYPE_DB;
1769 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
1773 ran_end = bio->bio_offset + bp->b_bufsize;
1774 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
1775 cursor.key_end.rec_type = HAMMER_RECTYPE_DATA;
1776 tmp64 = ran_end + MAXPHYS + 1; /* work-around GCC-4 bug */
1777 if (tmp64 < ran_end)
1778 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
1780 cursor.key_end.key = ran_end + MAXPHYS + 1;
1782 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
1784 error = hammer_ip_first(&cursor);
1787 while (error == 0) {
1788 error = hammer_ip_resolve_data(&cursor);
1791 base = &cursor.leaf->base;
1793 rec_offset = base->key - cursor.leaf->data_len;
1796 * Calculate the gap, if any, and zero-fill it.
1798 n = (int)(rec_offset - (bio->bio_offset + boff));
1800 if (n > bp->b_bufsize - boff)
1801 n = bp->b_bufsize - boff;
1802 bzero((char *)bp->b_data + boff, n);
1808 * Calculate the data offset in the record and the number
1809 * of bytes we can copy.
1811 * Note there is a degenerate case here where boff may
1812 * already be at bp->b_bufsize.
1816 n = cursor.leaf->data_len - roff;
1818 if (n > bp->b_bufsize - boff)
1819 n = bp->b_bufsize - boff;
1822 * If we cached a truncation point on our front-end the
1823 * on-disk version may still have physical records beyond
1824 * that point. Truncate visibility.
1826 if (ip->trunc_off <= rec_offset)
1828 else if (ip->trunc_off < rec_offset + n)
1829 n = (int)(ip->trunc_off - rec_offset);
1835 bcopy((char *)cursor.data + roff,
1836 (char *)bp->b_data + boff, n);
1839 if (boff == bp->b_bufsize)
1841 error = hammer_ip_next(&cursor);
1843 hammer_done_cursor(&cursor);
1844 hammer_done_transaction(&trans);
1847 * There may have been a gap after the last record
1849 if (error == ENOENT)
1851 if (error == 0 && boff != bp->b_bufsize) {
1852 KKASSERT(boff < bp->b_bufsize);
1853 bzero((char *)bp->b_data + boff, bp->b_bufsize - boff);
1854 /* boff = bp->b_bufsize; */
1857 bp->b_error = error;
1859 bp->b_flags |= B_ERROR;
1865 * Write to a regular file. Because this is a strategy call the OS is
1866 * trying to actually sync data to the media. HAMMER can only flush
1867 * the entire inode (so the TID remains properly synchronized).
1869 * Basically all we do here is place the bio on the inode's flush queue
1870 * and activate the flusher.
1874 hammer_vop_strategy_write(struct vop_strategy_args *ap)
1882 ip = ap->a_vp->v_data;
1884 if (ip->flags & HAMMER_INODE_RO) {
1885 bp->b_error = EROFS;
1886 bp->b_flags |= B_ERROR;
1892 * Interlock with inode destruction (no in-kernel or directory
1893 * topology visibility). If we queue new IO while trying to
1894 * destroy the inode we can deadlock the vtrunc call in
1895 * hammer_inode_unloadable_check().
1897 if (ip->flags & (HAMMER_INODE_DELETING|HAMMER_INODE_DELETED)) {
1904 * If the inode is being flushed we cannot re-queue buffers
1905 * it may have already flushed, or it could result in duplicate
1906 * records in the database.
1909 if (ip->flags & HAMMER_INODE_WRITE_ALT)
1910 TAILQ_INSERT_TAIL(&ip->bio_alt_list, bio, bio_act);
1912 TAILQ_INSERT_TAIL(&ip->bio_list, bio, bio_act);
1914 hammer_modify_inode(NULL, ip, HAMMER_INODE_BUFS);
1916 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
1921 * If the write was not part of an integrated flush operation then
1924 if (ip->flush_state != HAMMER_FST_FLUSH ||
1925 (ip->flags & HAMMER_INODE_WRITE_ALT)) {
1926 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
1933 * Backend code which actually performs the write to the media. This
1934 * routine is typically called from the flusher. The bio will be disposed
1935 * of (biodone'd) by this routine.
1937 * Iterate the related records and mark for deletion. If existing edge
1938 * records (left and right side) overlap our write they have to be marked
1939 * deleted and new records created, usually referencing a portion of the
1940 * original data. Then add a record to represent the buffer.
1943 hammer_dowrite(hammer_cursor_t cursor, hammer_inode_t ip, struct bio *bio)
1945 struct buf *bp = bio->bio_buf;
1948 KKASSERT(ip->flush_state == HAMMER_FST_FLUSH);
1951 * If the inode is going or gone, just throw away any frontend
1954 if (ip->flags & HAMMER_INODE_DELETED) {
1962 * Delete any records overlapping our range. This function will
1963 * (eventually) properly truncate partial overlaps.
1965 if (ip->sync_ino_data.obj_type == HAMMER_OBJTYPE_DBFILE) {
1966 error = hammer_ip_delete_range(cursor, ip, bio->bio_offset,
1969 error = hammer_ip_delete_range(cursor, ip, bio->bio_offset,
1975 * Add a single record to cover the write. We can write a record
1976 * with only the actual file data - for example, a small 200 byte
1977 * file does not have to write out a 16K record.
1979 * While the data size does not have to be aligned, we still do it
1980 * to reduce fragmentation in a future allocation model.
1985 if (ip->sync_ino_data.size - bio->bio_offset >
1987 limit_size = bp->b_bufsize;
1989 limit_size = (int)(ip->sync_ino_data.size -
1991 KKASSERT(limit_size >= 0);
1992 limit_size = (limit_size + 63) & ~63;
1995 error = hammer_ip_sync_data(cursor, ip, bio->bio_offset,
1996 bp->b_data, limit_size);
2000 Debugger("hammer_dowrite: error");
2003 bp->b_resid = bp->b_bufsize;
2004 bp->b_error = error;
2005 bp->b_flags |= B_ERROR;
2015 * dounlink - disconnect a directory entry
2017 * XXX whiteout support not really in yet
2020 hammer_dounlink(hammer_transaction_t trans, struct nchandle *nch,
2021 struct vnode *dvp, struct ucred *cred, int flags)
2023 struct namecache *ncp;
2026 struct hammer_cursor cursor;
2031 * Calculate the namekey and setup the key range for the scan. This
2032 * works kinda like a chained hash table where the lower 32 bits
2033 * of the namekey synthesize the chain.
2035 * The key range is inclusive of both key_beg and key_end.
2040 if (dip->flags & HAMMER_INODE_RO)
2043 namekey = hammer_directory_namekey(ncp->nc_name, ncp->nc_nlen);
2045 hammer_init_cursor(trans, &cursor, &dip->cache[0], dip);
2046 cursor.key_beg.localization = HAMMER_LOCALIZE_MISC;
2047 cursor.key_beg.obj_id = dip->obj_id;
2048 cursor.key_beg.key = namekey;
2049 cursor.key_beg.create_tid = 0;
2050 cursor.key_beg.delete_tid = 0;
2051 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
2052 cursor.key_beg.obj_type = 0;
2054 cursor.key_end = cursor.key_beg;
2055 cursor.key_end.key |= 0xFFFFFFFFULL;
2056 cursor.asof = dip->obj_asof;
2057 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
2060 * Scan all matching records (the chain), locate the one matching
2061 * the requested path component. info->last_error contains the
2062 * error code on search termination and could be 0, ENOENT, or
2065 * The hammer_ip_*() functions merge in-memory records with on-disk
2066 * records for the purposes of the search.
2068 error = hammer_ip_first(&cursor);
2070 while (error == 0) {
2071 error = hammer_ip_resolve_data(&cursor);
2074 nlen = cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF;
2076 if (ncp->nc_nlen == nlen &&
2077 bcmp(ncp->nc_name, cursor.data->entry.name, nlen) == 0) {
2080 error = hammer_ip_next(&cursor);
2084 * If all is ok we have to get the inode so we can adjust nlinks.
2086 * If the target is a directory, it must be empty.
2089 ip = hammer_get_inode(trans, &dip->cache[1],
2090 cursor.data->entry.obj_id,
2091 dip->hmp->asof, 0, &error);
2092 if (error == ENOENT) {
2093 kprintf("obj_id %016llx\n", cursor.data->entry.obj_id);
2094 Debugger("ENOENT unlinking object that should exist");
2098 * If we are trying to remove a directory the directory must
2101 * WARNING: hammer_ip_check_directory_empty() may have to
2102 * terminate the cursor to avoid a deadlock. It is ok to
2103 * call hammer_done_cursor() twice.
2105 if (error == 0 && ip->ino_data.obj_type ==
2106 HAMMER_OBJTYPE_DIRECTORY) {
2107 error = hammer_ip_check_directory_empty(trans, ip);
2111 * Delete the directory entry.
2113 * WARNING: hammer_ip_del_directory() may have to terminate
2114 * the cursor to avoid a deadlock. It is ok to call
2115 * hammer_done_cursor() twice.
2118 error = hammer_ip_del_directory(trans, &cursor,
2122 cache_setunresolved(nch);
2123 cache_setvp(nch, NULL);
2126 cache_inval_vp(ip->vp, CINV_DESTROY);
2128 hammer_rel_inode(ip, 0);
2130 hammer_done_cursor(&cursor);
2131 if (error == EDEADLK)
2137 /************************************************************************
2138 * FIFO AND SPECFS OPS *
2139 ************************************************************************
2144 hammer_vop_fifoclose (struct vop_close_args *ap)
2146 /* XXX update itimes */
2147 return (VOCALL(&fifo_vnode_vops, &ap->a_head));
2151 hammer_vop_fiforead (struct vop_read_args *ap)
2155 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
2156 /* XXX update access time */
2161 hammer_vop_fifowrite (struct vop_write_args *ap)
2165 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
2166 /* XXX update access time */
2171 hammer_vop_specclose (struct vop_close_args *ap)
2173 /* XXX update itimes */
2174 return (VOCALL(&spec_vnode_vops, &ap->a_head));
2178 hammer_vop_specread (struct vop_read_args *ap)
2180 /* XXX update access time */
2181 return (VOCALL(&spec_vnode_vops, &ap->a_head));
2185 hammer_vop_specwrite (struct vop_write_args *ap)
2187 /* XXX update last change time */
2188 return (VOCALL(&spec_vnode_vops, &ap->a_head));