2 * Copyright (c) 2007 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.26 2008/02/05 07:58:43 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 *);
82 static int hammer_vop_fifoclose (struct vop_close_args *);
83 static int hammer_vop_fiforead (struct vop_read_args *);
84 static int hammer_vop_fifowrite (struct vop_write_args *);
86 static int hammer_vop_specclose (struct vop_close_args *);
87 static int hammer_vop_specread (struct vop_read_args *);
88 static int hammer_vop_specwrite (struct vop_write_args *);
90 struct vop_ops hammer_vnode_vops = {
91 .vop_default = vop_defaultop,
92 .vop_fsync = hammer_vop_fsync,
93 .vop_getpages = vop_stdgetpages,
94 .vop_putpages = vop_stdputpages,
95 .vop_read = hammer_vop_read,
96 .vop_write = hammer_vop_write,
97 .vop_access = hammer_vop_access,
98 .vop_advlock = hammer_vop_advlock,
99 .vop_close = hammer_vop_close,
100 .vop_ncreate = hammer_vop_ncreate,
101 .vop_getattr = hammer_vop_getattr,
102 .vop_inactive = hammer_vop_inactive,
103 .vop_reclaim = hammer_vop_reclaim,
104 .vop_nresolve = hammer_vop_nresolve,
105 .vop_nlookupdotdot = hammer_vop_nlookupdotdot,
106 .vop_nlink = hammer_vop_nlink,
107 .vop_nmkdir = hammer_vop_nmkdir,
108 .vop_nmknod = hammer_vop_nmknod,
109 .vop_open = hammer_vop_open,
110 .vop_pathconf = hammer_vop_pathconf,
111 .vop_print = hammer_vop_print,
112 .vop_readdir = hammer_vop_readdir,
113 .vop_readlink = hammer_vop_readlink,
114 .vop_nremove = hammer_vop_nremove,
115 .vop_nrename = hammer_vop_nrename,
116 .vop_nrmdir = hammer_vop_nrmdir,
117 .vop_setattr = hammer_vop_setattr,
118 .vop_strategy = hammer_vop_strategy,
119 .vop_nsymlink = hammer_vop_nsymlink,
120 .vop_nwhiteout = hammer_vop_nwhiteout,
121 .vop_ioctl = hammer_vop_ioctl
124 struct vop_ops hammer_spec_vops = {
125 .vop_default = spec_vnoperate,
126 .vop_fsync = hammer_vop_fsync,
127 .vop_read = hammer_vop_specread,
128 .vop_write = hammer_vop_specwrite,
129 .vop_access = hammer_vop_access,
130 .vop_close = hammer_vop_specclose,
131 .vop_getattr = hammer_vop_getattr,
132 .vop_inactive = hammer_vop_inactive,
133 .vop_reclaim = hammer_vop_reclaim,
134 .vop_setattr = hammer_vop_setattr
137 struct vop_ops hammer_fifo_vops = {
138 .vop_default = fifo_vnoperate,
139 .vop_fsync = hammer_vop_fsync,
140 .vop_read = hammer_vop_fiforead,
141 .vop_write = hammer_vop_fifowrite,
142 .vop_access = hammer_vop_access,
143 .vop_close = hammer_vop_fifoclose,
144 .vop_getattr = hammer_vop_getattr,
145 .vop_inactive = hammer_vop_inactive,
146 .vop_reclaim = hammer_vop_reclaim,
147 .vop_setattr = hammer_vop_setattr
150 static int hammer_dounlink(struct nchandle *nch, struct vnode *dvp,
151 struct ucred *cred, int flags);
152 static int hammer_vop_strategy_read(struct vop_strategy_args *ap);
153 static int hammer_vop_strategy_write(struct vop_strategy_args *ap);
158 hammer_vop_vnoperate(struct vop_generic_args *)
160 return (VOCALL(&hammer_vnode_vops, ap));
165 * hammer_vop_fsync { vp, waitfor }
169 hammer_vop_fsync(struct vop_fsync_args *ap)
175 error = hammer_sync_inode(ip, ap->a_waitfor, 0);
180 * hammer_vop_read { vp, uio, ioflag, cred }
184 hammer_vop_read(struct vop_read_args *ap)
186 struct hammer_transaction trans;
195 if (ap->a_vp->v_type != VREG)
199 seqcount = ap->a_ioflag >> 16;
201 hammer_start_transaction(&trans, ip->hmp);
204 * Access the data in HAMMER_BUFSIZE blocks via the buffer cache.
207 while (uio->uio_resid > 0 && uio->uio_offset < ip->ino_rec.ino_size) {
208 offset = uio->uio_offset & HAMMER_BUFMASK;
210 error = cluster_read(ap->a_vp, ip->ino_rec.ino_size,
211 uio->uio_offset - offset, HAMMER_BUFSIZE,
212 MAXBSIZE, seqcount, &bp);
214 error = bread(ap->a_vp, uio->uio_offset - offset,
215 HAMMER_BUFSIZE, &bp);
220 /* bp->b_flags |= B_CLUSTEROK; temporarily disabled */
221 n = HAMMER_BUFSIZE - offset;
222 if (n > uio->uio_resid)
224 if (n > ip->ino_rec.ino_size - uio->uio_offset)
225 n = (int)(ip->ino_rec.ino_size - uio->uio_offset);
226 error = uiomove((char *)bp->b_data + offset, n, uio);
231 if ((ip->flags & HAMMER_INODE_RO) == 0) {
232 ip->ino_rec.ino_atime = trans.tid;
233 hammer_modify_inode(&trans, ip, HAMMER_INODE_ITIMES);
237 hammer_commit_transaction(&trans);
242 * hammer_vop_write { vp, uio, ioflag, cred }
246 hammer_vop_write(struct vop_write_args *ap)
248 struct hammer_transaction trans;
249 struct hammer_inode *ip;
257 if (ap->a_vp->v_type != VREG)
262 if (ip->flags & HAMMER_INODE_RO)
266 * Create a transaction to cover the operations we perform.
268 hammer_start_transaction(&trans, ip->hmp);
274 if (ap->a_ioflag & IO_APPEND)
275 uio->uio_offset = ip->ino_rec.ino_size;
278 * Check for illegal write offsets. Valid range is 0...2^63-1
280 if (uio->uio_offset < 0 || uio->uio_offset + uio->uio_resid <= 0) {
281 hammer_commit_transaction(&trans);
286 * Access the data in HAMMER_BUFSIZE blocks via the buffer cache.
288 while (uio->uio_resid > 0) {
291 offset = uio->uio_offset & HAMMER_BUFMASK;
292 n = HAMMER_BUFSIZE - offset;
293 if (n > uio->uio_resid)
295 if (uio->uio_offset + n > ip->ino_rec.ino_size) {
296 vnode_pager_setsize(ap->a_vp, uio->uio_offset + n);
300 if (uio->uio_segflg == UIO_NOCOPY) {
302 * Issuing a write with the same data backing the
303 * buffer. Instantiate the buffer to collect the
304 * backing vm pages, then read-in any missing bits.
306 * This case is used by vop_stdputpages().
308 bp = getblk(ap->a_vp, uio->uio_offset - offset,
309 HAMMER_BUFSIZE, GETBLK_BHEAVY, 0);
310 if ((bp->b_flags & B_CACHE) == 0) {
312 error = bread(ap->a_vp,
313 uio->uio_offset - offset,
314 HAMMER_BUFSIZE, &bp);
316 } else if (offset == 0 && uio->uio_resid >= HAMMER_BUFSIZE) {
318 * entirely overwrite the buffer
320 bp = getblk(ap->a_vp, uio->uio_offset - offset,
321 HAMMER_BUFSIZE, GETBLK_BHEAVY, 0);
322 } else if (offset == 0 && uio->uio_offset >= ip->ino_rec.ino_size) {
326 bp = getblk(ap->a_vp, uio->uio_offset - offset,
327 HAMMER_BUFSIZE, GETBLK_BHEAVY, 0);
331 * Partial overwrite, read in any missing bits then
332 * replace the portion being written.
334 error = bread(ap->a_vp, uio->uio_offset - offset,
335 HAMMER_BUFSIZE, &bp);
340 error = uiomove((char *)bp->b_data + offset, n, uio);
343 * If we screwed up we have to undo any VM size changes we
349 vtruncbuf(ap->a_vp, ip->ino_rec.ino_size,
354 /* bp->b_flags |= B_CLUSTEROK; temporarily disabled */
355 if (ip->ino_rec.ino_size < uio->uio_offset) {
356 ip->ino_rec.ino_size = uio->uio_offset;
357 flags = HAMMER_INODE_RDIRTY;
358 vnode_pager_setsize(ap->a_vp, ip->ino_rec.ino_size);
362 ip->ino_rec.ino_mtime = trans.tid;
363 flags |= HAMMER_INODE_ITIMES | HAMMER_INODE_BUFS;
364 hammer_modify_inode(&trans, ip, flags);
367 * The file write must be tagged with the same TID as the
368 * inode, for consistency in case the inode changed size.
369 * This guarantees the on-disk data records will have a
370 * TID <= the inode TID representing the size change.
372 * If a prior write has not yet flushed, retain its TID.
375 bp->b_tid = ip->last_tid;
377 if (ap->a_ioflag & IO_SYNC) {
379 } else if (ap->a_ioflag & IO_DIRECT) {
386 hammer_abort_transaction(&trans);
388 hammer_commit_transaction(&trans);
393 * hammer_vop_access { vp, mode, cred }
397 hammer_vop_access(struct vop_access_args *ap)
399 struct hammer_inode *ip = VTOI(ap->a_vp);
404 uid = hammer_to_unix_xid(&ip->ino_data.uid);
405 gid = hammer_to_unix_xid(&ip->ino_data.gid);
407 error = vop_helper_access(ap, uid, gid, ip->ino_data.mode,
408 ip->ino_data.uflags);
413 * hammer_vop_advlock { vp, id, op, fl, flags }
417 hammer_vop_advlock(struct vop_advlock_args *ap)
419 struct hammer_inode *ip = VTOI(ap->a_vp);
421 return (lf_advlock(ap, &ip->advlock, ip->ino_rec.ino_size));
425 * hammer_vop_close { vp, fflag }
429 hammer_vop_close(struct vop_close_args *ap)
431 return (vop_stdclose(ap));
435 * hammer_vop_ncreate { nch, dvp, vpp, cred, vap }
437 * The operating system has already ensured that the directory entry
438 * does not exist and done all appropriate namespace locking.
442 hammer_vop_ncreate(struct vop_ncreate_args *ap)
444 struct hammer_transaction trans;
445 struct hammer_inode *dip;
446 struct hammer_inode *nip;
447 struct nchandle *nch;
451 dip = VTOI(ap->a_dvp);
453 if (dip->flags & HAMMER_INODE_RO)
457 * Create a transaction to cover the operations we perform.
459 hammer_start_transaction(&trans, dip->hmp);
462 * Create a new filesystem object of the requested type. The
463 * returned inode will be referenced but not locked.
466 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred, dip, &nip);
468 kprintf("hammer_create_inode error %d\n", error);
470 hammer_abort_transaction(&trans);
476 * Add the new filesystem object to the directory. This will also
477 * bump the inode's link count.
479 error = hammer_ip_add_directory(&trans, dip, nch->ncp, nip);
481 kprintf("hammer_ip_add_directory error %d\n", error);
487 hammer_rel_inode(nip, 0);
488 hammer_abort_transaction(&trans);
491 hammer_commit_transaction(&trans);
492 error = hammer_get_vnode(nip, LK_EXCLUSIVE, ap->a_vpp);
493 hammer_rel_inode(nip, 0);
495 cache_setunresolved(ap->a_nch);
496 cache_setvp(ap->a_nch, *ap->a_vpp);
503 * hammer_vop_getattr { vp, vap }
507 hammer_vop_getattr(struct vop_getattr_args *ap)
509 struct hammer_inode *ip = VTOI(ap->a_vp);
510 struct vattr *vap = ap->a_vap;
513 if (cache_check_fsmid_vp(ap->a_vp, &ip->fsmid) &&
514 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0 &&
519 hammer_itimes(ap->a_vp);
522 vap->va_fsid = ip->hmp->fsid_udev;
523 vap->va_fileid = ip->ino_rec.base.base.obj_id;
524 vap->va_mode = ip->ino_data.mode;
525 vap->va_nlink = ip->ino_rec.ino_nlinks;
526 vap->va_uid = hammer_to_unix_xid(&ip->ino_data.uid);
527 vap->va_gid = hammer_to_unix_xid(&ip->ino_data.gid);
530 vap->va_size = ip->ino_rec.ino_size;
531 hammer_to_timespec(ip->ino_rec.ino_atime, &vap->va_atime);
532 hammer_to_timespec(ip->ino_rec.ino_mtime, &vap->va_mtime);
533 hammer_to_timespec(ip->ino_data.ctime, &vap->va_ctime);
534 vap->va_flags = ip->ino_data.uflags;
535 vap->va_gen = 1; /* hammer inums are unique for all time */
536 vap->va_blocksize = 32768; /* XXX - extract from root volume */
537 vap->va_bytes = ip->ino_rec.ino_size;
538 vap->va_type = hammer_get_vnode_type(ip->ino_rec.base.base.obj_type);
539 vap->va_filerev = 0; /* XXX */
540 /* mtime uniquely identifies any adjustments made to the file */
541 vap->va_fsmid = ip->ino_rec.ino_mtime;
542 vap->va_uid_uuid = ip->ino_data.uid;
543 vap->va_gid_uuid = ip->ino_data.gid;
544 vap->va_fsid_uuid = ip->hmp->fsid;
545 vap->va_vaflags = VA_UID_UUID_VALID | VA_GID_UUID_VALID |
548 switch (ip->ino_rec.base.base.obj_type) {
549 case HAMMER_OBJTYPE_CDEV:
550 case HAMMER_OBJTYPE_BDEV:
551 vap->va_rmajor = ip->ino_data.rmajor;
552 vap->va_rminor = ip->ino_data.rminor;
562 * hammer_vop_nresolve { nch, dvp, cred }
564 * Locate the requested directory entry.
568 hammer_vop_nresolve(struct vop_nresolve_args *ap)
570 struct namecache *ncp;
574 struct hammer_cursor cursor;
575 union hammer_record_ondisk *rec;
585 * Misc initialization, plus handle as-of name extensions. Look for
586 * the '@@' extension. Note that as-of files and directories cannot
589 dip = VTOI(ap->a_dvp);
590 ncp = ap->a_nch->ncp;
591 asof = dip->obj_asof;
595 for (i = 0; i < nlen; ++i) {
596 if (ncp->nc_name[i] == '@' && ncp->nc_name[i+1] == '@') {
597 asof = hammer_str_to_tid(ncp->nc_name + i + 2);
598 flags |= HAMMER_INODE_RO;
605 * If there is no path component the time extension is relative to
609 ip = hammer_get_inode(dip->hmp, &dip->cache[1], dip->obj_id,
610 asof, flags, &error);
612 error = hammer_get_vnode(ip, LK_EXCLUSIVE, &vp);
613 hammer_rel_inode(ip, 0);
619 cache_setvp(ap->a_nch, vp);
626 * Calculate the namekey and setup the key range for the scan. This
627 * works kinda like a chained hash table where the lower 32 bits
628 * of the namekey synthesize the chain.
630 * The key range is inclusive of both key_beg and key_end.
632 namekey = hammer_directory_namekey(ncp->nc_name, nlen);
634 error = hammer_init_cursor_hmp(&cursor, &dip->cache[0], dip->hmp);
635 cursor.key_beg.obj_id = dip->obj_id;
636 cursor.key_beg.key = namekey;
637 cursor.key_beg.create_tid = 0;
638 cursor.key_beg.delete_tid = 0;
639 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
640 cursor.key_beg.obj_type = 0;
642 cursor.key_end = cursor.key_beg;
643 cursor.key_end.key |= 0xFFFFFFFFULL;
645 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
648 * Scan all matching records (the chain), locate the one matching
649 * the requested path component.
651 * The hammer_ip_*() functions merge in-memory records with on-disk
652 * records for the purposes of the search.
655 error = hammer_ip_first(&cursor, dip);
661 error = hammer_ip_resolve_data(&cursor);
665 if (nlen == rec->entry.base.data_len &&
666 bcmp(ncp->nc_name, cursor.data, nlen) == 0) {
667 obj_id = rec->entry.obj_id;
670 error = hammer_ip_next(&cursor);
672 hammer_done_cursor(&cursor);
674 ip = hammer_get_inode(dip->hmp, &dip->cache[1],
675 obj_id, asof, flags, &error);
677 error = hammer_get_vnode(ip, LK_EXCLUSIVE, &vp);
678 hammer_rel_inode(ip, 0);
684 cache_setvp(ap->a_nch, vp);
687 } else if (error == ENOENT) {
688 cache_setvp(ap->a_nch, NULL);
694 * hammer_vop_nlookupdotdot { dvp, vpp, cred }
696 * Locate the parent directory of a directory vnode.
698 * dvp is referenced but not locked. *vpp must be returned referenced and
699 * locked. A parent_obj_id of 0 does not necessarily indicate that we are
700 * at the root, instead it could indicate that the directory we were in was
705 hammer_vop_nlookupdotdot(struct vop_nlookupdotdot_args *ap)
707 struct hammer_inode *dip;
708 struct hammer_inode *ip;
709 u_int64_t parent_obj_id;
712 dip = VTOI(ap->a_dvp);
713 if ((parent_obj_id = dip->ino_data.parent_obj_id) == 0) {
718 ip = hammer_get_inode(dip->hmp, &dip->cache[1], parent_obj_id,
719 dip->obj_asof, dip->flags, &error);
724 error = hammer_get_vnode(ip, LK_EXCLUSIVE, ap->a_vpp);
725 hammer_rel_inode(ip, 0);
730 * hammer_vop_nlink { nch, dvp, vp, cred }
734 hammer_vop_nlink(struct vop_nlink_args *ap)
736 struct hammer_transaction trans;
737 struct hammer_inode *dip;
738 struct hammer_inode *ip;
739 struct nchandle *nch;
743 dip = VTOI(ap->a_dvp);
746 if (dip->flags & HAMMER_INODE_RO)
748 if (ip->flags & HAMMER_INODE_RO)
752 * Create a transaction to cover the operations we perform.
754 hammer_start_transaction(&trans, dip->hmp);
757 * Add the filesystem object to the directory. Note that neither
758 * dip nor ip are referenced or locked, but their vnodes are
759 * referenced. This function will bump the inode's link count.
761 error = hammer_ip_add_directory(&trans, dip, nch->ncp, ip);
767 hammer_abort_transaction(&trans);
769 cache_setunresolved(nch);
770 cache_setvp(nch, ap->a_vp);
771 hammer_commit_transaction(&trans);
777 * hammer_vop_nmkdir { nch, dvp, vpp, cred, vap }
779 * The operating system has already ensured that the directory entry
780 * does not exist and done all appropriate namespace locking.
784 hammer_vop_nmkdir(struct vop_nmkdir_args *ap)
786 struct hammer_transaction trans;
787 struct hammer_inode *dip;
788 struct hammer_inode *nip;
789 struct nchandle *nch;
793 dip = VTOI(ap->a_dvp);
795 if (dip->flags & HAMMER_INODE_RO)
799 * Create a transaction to cover the operations we perform.
801 hammer_start_transaction(&trans, dip->hmp);
804 * Create a new filesystem object of the requested type. The
805 * returned inode will be referenced but not locked.
807 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred, dip, &nip);
809 kprintf("hammer_mkdir error %d\n", error);
811 hammer_abort_transaction(&trans);
817 * Add the new filesystem object to the directory. This will also
818 * bump the inode's link count.
820 error = hammer_ip_add_directory(&trans, dip, nch->ncp, nip);
822 kprintf("hammer_mkdir (add) error %d\n", error);
828 hammer_rel_inode(nip, 0);
829 hammer_abort_transaction(&trans);
832 hammer_commit_transaction(&trans);
833 error = hammer_get_vnode(nip, LK_EXCLUSIVE, ap->a_vpp);
834 hammer_rel_inode(nip, 0);
836 cache_setunresolved(ap->a_nch);
837 cache_setvp(ap->a_nch, *ap->a_vpp);
844 * hammer_vop_nmknod { nch, dvp, vpp, cred, vap }
846 * The operating system has already ensured that the directory entry
847 * does not exist and done all appropriate namespace locking.
851 hammer_vop_nmknod(struct vop_nmknod_args *ap)
853 struct hammer_transaction trans;
854 struct hammer_inode *dip;
855 struct hammer_inode *nip;
856 struct nchandle *nch;
860 dip = VTOI(ap->a_dvp);
862 if (dip->flags & HAMMER_INODE_RO)
866 * Create a transaction to cover the operations we perform.
868 hammer_start_transaction(&trans, dip->hmp);
871 * Create a new filesystem object of the requested type. The
872 * returned inode will be referenced but not locked.
874 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred, dip, &nip);
876 hammer_abort_transaction(&trans);
882 * Add the new filesystem object to the directory. This will also
883 * bump the inode's link count.
885 error = hammer_ip_add_directory(&trans, dip, nch->ncp, nip);
891 hammer_rel_inode(nip, 0);
892 hammer_abort_transaction(&trans);
895 hammer_commit_transaction(&trans);
896 error = hammer_get_vnode(nip, LK_EXCLUSIVE, ap->a_vpp);
897 hammer_rel_inode(nip, 0);
899 cache_setunresolved(ap->a_nch);
900 cache_setvp(ap->a_nch, *ap->a_vpp);
907 * hammer_vop_open { vp, mode, cred, fp }
911 hammer_vop_open(struct vop_open_args *ap)
913 if ((ap->a_mode & FWRITE) && (VTOI(ap->a_vp)->flags & HAMMER_INODE_RO))
916 return(vop_stdopen(ap));
920 * hammer_vop_pathconf { vp, name, retval }
924 hammer_vop_pathconf(struct vop_pathconf_args *ap)
930 * hammer_vop_print { vp }
934 hammer_vop_print(struct vop_print_args *ap)
940 * hammer_vop_readdir { vp, uio, cred, *eofflag, *ncookies, off_t **cookies }
944 hammer_vop_readdir(struct vop_readdir_args *ap)
946 struct hammer_cursor cursor;
947 struct hammer_inode *ip;
949 hammer_record_ondisk_t rec;
950 hammer_base_elm_t base;
960 saveoff = uio->uio_offset;
962 if (ap->a_ncookies) {
963 ncookies = uio->uio_resid / 16 + 1;
966 cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
975 * Handle artificial entries
979 r = vop_write_dirent(&error, uio, ip->obj_id, DT_DIR, 1, ".");
983 cookies[cookie_index] = saveoff;
986 if (cookie_index == ncookies)
990 if (ip->ino_data.parent_obj_id) {
991 r = vop_write_dirent(&error, uio,
992 ip->ino_data.parent_obj_id,
995 r = vop_write_dirent(&error, uio,
996 ip->obj_id, DT_DIR, 2, "..");
1001 cookies[cookie_index] = saveoff;
1004 if (cookie_index == ncookies)
1009 * Key range (begin and end inclusive) to scan. Directory keys
1010 * directly translate to a 64 bit 'seek' position.
1012 hammer_init_cursor_hmp(&cursor, &ip->cache[0], ip->hmp);
1013 cursor.key_beg.obj_id = ip->obj_id;
1014 cursor.key_beg.create_tid = 0;
1015 cursor.key_beg.delete_tid = 0;
1016 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1017 cursor.key_beg.obj_type = 0;
1018 cursor.key_beg.key = saveoff;
1020 cursor.key_end = cursor.key_beg;
1021 cursor.key_end.key = HAMMER_MAX_KEY;
1022 cursor.asof = ip->obj_asof;
1023 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
1025 error = hammer_ip_first(&cursor, ip);
1027 while (error == 0) {
1028 error = hammer_ip_resolve_data(&cursor);
1031 rec = cursor.record;
1032 base = &rec->base.base;
1033 saveoff = base->key;
1035 if (base->obj_id != ip->obj_id)
1036 panic("readdir: bad record at %p", cursor.node);
1038 r = vop_write_dirent(
1039 &error, uio, rec->entry.obj_id,
1040 hammer_get_dtype(rec->entry.base.base.obj_type),
1041 rec->entry.base.data_len,
1042 (void *)cursor.data);
1047 cookies[cookie_index] = base->key;
1049 if (cookie_index == ncookies)
1051 error = hammer_ip_next(&cursor);
1053 hammer_done_cursor(&cursor);
1057 *ap->a_eofflag = (error == ENOENT);
1058 uio->uio_offset = saveoff;
1059 if (error && cookie_index == 0) {
1060 if (error == ENOENT)
1063 kfree(cookies, M_TEMP);
1064 *ap->a_ncookies = 0;
1065 *ap->a_cookies = NULL;
1068 if (error == ENOENT)
1071 *ap->a_ncookies = cookie_index;
1072 *ap->a_cookies = cookies;
1079 * hammer_vop_readlink { vp, uio, cred }
1083 hammer_vop_readlink(struct vop_readlink_args *ap)
1085 struct hammer_cursor cursor;
1086 struct hammer_inode *ip;
1089 ip = VTOI(ap->a_vp);
1090 hammer_init_cursor_hmp(&cursor, &ip->cache[0], ip->hmp);
1093 * Key range (begin and end inclusive) to scan. Directory keys
1094 * directly translate to a 64 bit 'seek' position.
1096 cursor.key_beg.obj_id = ip->obj_id;
1097 cursor.key_beg.create_tid = 0;
1098 cursor.key_beg.delete_tid = 0;
1099 cursor.key_beg.rec_type = HAMMER_RECTYPE_FIX;
1100 cursor.key_beg.obj_type = 0;
1101 cursor.key_beg.key = HAMMER_FIXKEY_SYMLINK;
1102 cursor.asof = ip->obj_asof;
1103 cursor.flags |= HAMMER_CURSOR_ASOF;
1105 error = hammer_ip_lookup(&cursor, ip);
1107 error = hammer_ip_resolve_data(&cursor);
1109 error = uiomove((char *)cursor.data,
1110 cursor.record->generic.base.data_len,
1114 hammer_done_cursor(&cursor);
1119 * hammer_vop_nremove { nch, dvp, cred }
1123 hammer_vop_nremove(struct vop_nremove_args *ap)
1125 return(hammer_dounlink(ap->a_nch, ap->a_dvp, ap->a_cred, 0));
1129 * hammer_vop_nrename { fnch, tnch, fdvp, tdvp, cred }
1133 hammer_vop_nrename(struct vop_nrename_args *ap)
1135 struct hammer_transaction trans;
1136 struct namecache *fncp;
1137 struct namecache *tncp;
1138 struct hammer_inode *fdip;
1139 struct hammer_inode *tdip;
1140 struct hammer_inode *ip;
1141 struct hammer_cursor cursor;
1142 union hammer_record_ondisk *rec;
1146 fdip = VTOI(ap->a_fdvp);
1147 tdip = VTOI(ap->a_tdvp);
1148 fncp = ap->a_fnch->ncp;
1149 tncp = ap->a_tnch->ncp;
1150 ip = VTOI(fncp->nc_vp);
1151 KKASSERT(ip != NULL);
1153 if (fdip->flags & HAMMER_INODE_RO)
1155 if (tdip->flags & HAMMER_INODE_RO)
1157 if (ip->flags & HAMMER_INODE_RO)
1160 hammer_start_transaction(&trans, fdip->hmp);
1163 * Remove tncp from the target directory and then link ip as
1164 * tncp. XXX pass trans to dounlink
1166 error = hammer_dounlink(ap->a_tnch, ap->a_tdvp, ap->a_cred, 0);
1167 if (error == 0 || error == ENOENT)
1168 error = hammer_ip_add_directory(&trans, tdip, tncp, ip);
1170 goto failed; /* XXX */
1173 * Locate the record in the originating directory and remove it.
1175 * Calculate the namekey and setup the key range for the scan. This
1176 * works kinda like a chained hash table where the lower 32 bits
1177 * of the namekey synthesize the chain.
1179 * The key range is inclusive of both key_beg and key_end.
1181 namekey = hammer_directory_namekey(fncp->nc_name, fncp->nc_nlen);
1183 hammer_init_cursor_hmp(&cursor, &fdip->cache[0], fdip->hmp);
1184 cursor.key_beg.obj_id = fdip->obj_id;
1185 cursor.key_beg.key = namekey;
1186 cursor.key_beg.create_tid = 0;
1187 cursor.key_beg.delete_tid = 0;
1188 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1189 cursor.key_beg.obj_type = 0;
1191 cursor.key_end = cursor.key_beg;
1192 cursor.key_end.key |= 0xFFFFFFFFULL;
1193 cursor.asof = fdip->obj_asof;
1194 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
1197 * Scan all matching records (the chain), locate the one matching
1198 * the requested path component.
1200 * The hammer_ip_*() functions merge in-memory records with on-disk
1201 * records for the purposes of the search.
1203 error = hammer_ip_first(&cursor, fdip);
1204 while (error == 0) {
1205 if (hammer_ip_resolve_data(&cursor) != 0)
1207 rec = cursor.record;
1208 if (fncp->nc_nlen == rec->entry.base.data_len &&
1209 bcmp(fncp->nc_name, cursor.data, fncp->nc_nlen) == 0) {
1212 error = hammer_ip_next(&cursor);
1216 * If all is ok we have to get the inode so we can adjust nlinks.
1218 * WARNING: hammer_ip_del_directory() may have to terminate the
1219 * cursor to avoid a recursion. It's ok to call hammer_done_cursor()
1223 error = hammer_ip_del_directory(&trans, &cursor, fdip, ip);
1224 hammer_done_cursor(&cursor);
1226 cache_rename(ap->a_fnch, ap->a_tnch);
1227 if (error == EDEADLK)
1231 hammer_commit_transaction(&trans);
1233 hammer_abort_transaction(&trans);
1239 * hammer_vop_nrmdir { nch, dvp, cred }
1243 hammer_vop_nrmdir(struct vop_nrmdir_args *ap)
1245 return(hammer_dounlink(ap->a_nch, ap->a_dvp, ap->a_cred, 0));
1249 * hammer_vop_setattr { vp, vap, cred }
1253 hammer_vop_setattr(struct vop_setattr_args *ap)
1255 struct hammer_transaction trans;
1256 struct hammer_cursor *spike = NULL;
1258 struct hammer_inode *ip;
1262 int64_t aligned_size;
1267 ip = ap->a_vp->v_data;
1270 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
1272 if (ip->flags & HAMMER_INODE_RO)
1275 hammer_start_transaction(&trans, ip->hmp);
1278 if (vap->va_flags != VNOVAL) {
1279 flags = ip->ino_data.uflags;
1280 error = vop_helper_setattr_flags(&flags, vap->va_flags,
1281 hammer_to_unix_xid(&ip->ino_data.uid),
1284 if (ip->ino_data.uflags != flags) {
1285 ip->ino_data.uflags = flags;
1286 modflags |= HAMMER_INODE_DDIRTY;
1288 if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) {
1295 if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) {
1299 if (vap->va_uid != (uid_t)VNOVAL) {
1300 hammer_guid_to_uuid(&uuid, vap->va_uid);
1301 if (bcmp(&uuid, &ip->ino_data.uid, sizeof(uuid)) != 0) {
1302 ip->ino_data.uid = uuid;
1303 modflags |= HAMMER_INODE_DDIRTY;
1306 if (vap->va_gid != (uid_t)VNOVAL) {
1307 hammer_guid_to_uuid(&uuid, vap->va_gid);
1308 if (bcmp(&uuid, &ip->ino_data.gid, sizeof(uuid)) != 0) {
1309 ip->ino_data.gid = uuid;
1310 modflags |= HAMMER_INODE_DDIRTY;
1313 while (vap->va_size != VNOVAL && ip->ino_rec.ino_size != vap->va_size) {
1314 switch(ap->a_vp->v_type) {
1316 if (vap->va_size == ip->ino_rec.ino_size)
1318 if (vap->va_size < ip->ino_rec.ino_size) {
1319 vtruncbuf(ap->a_vp, vap->va_size,
1323 vnode_pager_setsize(ap->a_vp, vap->va_size);
1326 ip->ino_rec.ino_size = vap->va_size;
1327 modflags |= HAMMER_INODE_RDIRTY;
1328 aligned_size = (vap->va_size + HAMMER_BUFMASK) &
1329 ~(int64_t)HAMMER_BUFMASK;
1332 error = hammer_ip_delete_range(&trans, ip,
1334 0x7FFFFFFFFFFFFFFFLL,
1338 * If truncating we have to clean out a portion of
1339 * the last block on-disk.
1341 if (truncating && error == 0 &&
1342 vap->va_size < aligned_size) {
1346 offset = vap->va_size & HAMMER_BUFMASK;
1347 error = bread(ap->a_vp,
1348 aligned_size - HAMMER_BUFSIZE,
1349 HAMMER_BUFSIZE, &bp);
1351 bzero(bp->b_data + offset,
1352 HAMMER_BUFSIZE - offset);
1360 error = hammer_ip_delete_range(&trans, ip,
1362 0x7FFFFFFFFFFFFFFFLL,
1364 ip->ino_rec.ino_size = vap->va_size;
1365 modflags |= HAMMER_INODE_RDIRTY;
1371 if (error == ENOSPC) {
1372 error = hammer_spike(&spike);
1376 KKASSERT(spike == NULL);
1379 if (vap->va_atime.tv_sec != VNOVAL) {
1380 ip->ino_rec.ino_atime =
1381 hammer_timespec_to_transid(&vap->va_atime);
1382 modflags |= HAMMER_INODE_ITIMES;
1384 if (vap->va_mtime.tv_sec != VNOVAL) {
1385 ip->ino_rec.ino_mtime =
1386 hammer_timespec_to_transid(&vap->va_mtime);
1387 modflags |= HAMMER_INODE_ITIMES;
1389 if (vap->va_mode != (mode_t)VNOVAL) {
1390 if (ip->ino_data.mode != vap->va_mode) {
1391 ip->ino_data.mode = vap->va_mode;
1392 modflags |= HAMMER_INODE_DDIRTY;
1397 hammer_abort_transaction(&trans);
1399 hammer_modify_inode(&trans, ip, modflags);
1400 hammer_commit_transaction(&trans);
1406 * hammer_vop_nsymlink { nch, dvp, vpp, cred, vap, target }
1410 hammer_vop_nsymlink(struct vop_nsymlink_args *ap)
1412 struct hammer_transaction trans;
1413 struct hammer_inode *dip;
1414 struct hammer_inode *nip;
1415 struct nchandle *nch;
1416 hammer_record_t record;
1420 ap->a_vap->va_type = VLNK;
1423 dip = VTOI(ap->a_dvp);
1425 if (dip->flags & HAMMER_INODE_RO)
1429 * Create a transaction to cover the operations we perform.
1431 hammer_start_transaction(&trans, dip->hmp);
1434 * Create a new filesystem object of the requested type. The
1435 * returned inode will be referenced but not locked.
1438 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred, dip, &nip);
1440 hammer_abort_transaction(&trans);
1446 * Add the new filesystem object to the directory. This will also
1447 * bump the inode's link count.
1449 error = hammer_ip_add_directory(&trans, dip, nch->ncp, nip);
1452 * Add a record representing the symlink. symlink stores the link
1453 * as pure data, not a string, and is no \0 terminated.
1456 record = hammer_alloc_mem_record(nip);
1457 bytes = strlen(ap->a_target);
1459 record->rec.generic.base.base.key = HAMMER_FIXKEY_SYMLINK;
1460 record->rec.generic.base.base.rec_type = HAMMER_RECTYPE_FIX;
1461 record->rec.generic.base.data_len = bytes;
1462 if (bytes <= sizeof(record->rec.generic.filler)) {
1463 record->data = (void *)record->rec.generic.filler;
1464 bcopy(ap->a_target, record->data, bytes);
1466 record->data = (void *)ap->a_target;
1467 /* will be reallocated by routine below */
1469 error = hammer_ip_add_record(&trans, record);
1476 hammer_rel_inode(nip, 0);
1477 hammer_abort_transaction(&trans);
1480 hammer_commit_transaction(&trans);
1481 error = hammer_get_vnode(nip, LK_EXCLUSIVE, ap->a_vpp);
1482 hammer_rel_inode(nip, 0);
1484 cache_setunresolved(ap->a_nch);
1485 cache_setvp(ap->a_nch, *ap->a_vpp);
1492 * hammer_vop_nwhiteout { nch, dvp, cred, flags }
1496 hammer_vop_nwhiteout(struct vop_nwhiteout_args *ap)
1498 return(hammer_dounlink(ap->a_nch, ap->a_dvp, ap->a_cred, ap->a_flags));
1502 * hammer_vop_ioctl { vp, command, data, fflag, cred }
1506 hammer_vop_ioctl(struct vop_ioctl_args *ap)
1508 struct hammer_inode *ip = ap->a_vp->v_data;
1510 return(hammer_ioctl(ip, ap->a_command, ap->a_data,
1511 ap->a_fflag, ap->a_cred));
1515 * hammer_vop_strategy { vp, bio }
1517 * Strategy call, used for regular file read & write only. Note that the
1518 * bp may represent a cluster.
1520 * To simplify operation and allow better optimizations in the future,
1521 * this code does not make any assumptions with regards to buffer alignment
1526 hammer_vop_strategy(struct vop_strategy_args *ap)
1531 bp = ap->a_bio->bio_buf;
1535 error = hammer_vop_strategy_read(ap);
1538 error = hammer_vop_strategy_write(ap);
1544 bp->b_error = error;
1546 bp->b_flags |= B_ERROR;
1552 * Read from a regular file. Iterate the related records and fill in the
1553 * BIO/BUF. Gaps are zero-filled.
1555 * The support code in hammer_object.c should be used to deal with mixed
1556 * in-memory and on-disk records.
1562 hammer_vop_strategy_read(struct vop_strategy_args *ap)
1564 struct hammer_inode *ip = ap->a_vp->v_data;
1565 struct hammer_cursor cursor;
1566 hammer_record_ondisk_t rec;
1567 hammer_base_elm_t base;
1581 hammer_init_cursor_hmp(&cursor, &ip->cache[0], ip->hmp);
1584 * Key range (begin and end inclusive) to scan. Note that the key's
1585 * stored in the actual records represent BASE+LEN, not BASE. The
1586 * first record containing bio_offset will have a key > bio_offset.
1588 cursor.key_beg.obj_id = ip->obj_id;
1589 cursor.key_beg.create_tid = 0;
1590 cursor.key_beg.delete_tid = 0;
1591 cursor.key_beg.obj_type = 0;
1592 cursor.key_beg.key = bio->bio_offset + 1;
1593 cursor.asof = ip->obj_asof;
1594 cursor.flags |= HAMMER_CURSOR_ASOF;
1596 cursor.key_end = cursor.key_beg;
1597 if (ip->ino_rec.base.base.obj_type == HAMMER_OBJTYPE_DBFILE) {
1598 cursor.key_beg.rec_type = HAMMER_RECTYPE_DB;
1599 cursor.key_end.rec_type = HAMMER_RECTYPE_DB;
1600 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
1602 ran_end = bio->bio_offset + bp->b_bufsize;
1603 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
1604 cursor.key_end.rec_type = HAMMER_RECTYPE_DATA;
1605 tmp64 = ran_end + MAXPHYS + 1; /* work-around GCC-4 bug */
1606 if (tmp64 < ran_end)
1607 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
1609 cursor.key_end.key = ran_end + MAXPHYS + 1;
1611 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
1613 error = hammer_ip_first(&cursor, ip);
1616 while (error == 0) {
1617 error = hammer_ip_resolve_data(&cursor);
1620 rec = cursor.record;
1621 base = &rec->base.base;
1623 rec_offset = base->key - rec->data.base.data_len;
1626 * Calculate the gap, if any, and zero-fill it.
1628 n = (int)(rec_offset - (bio->bio_offset + boff));
1630 if (n > bp->b_bufsize - boff)
1631 n = bp->b_bufsize - boff;
1632 bzero((char *)bp->b_data + boff, n);
1638 * Calculate the data offset in the record and the number
1639 * of bytes we can copy.
1641 * Note there is a degenerate case here where boff may
1642 * already be at bp->b_bufsize.
1645 n = rec->data.base.data_len - roff;
1647 if (n > bp->b_bufsize - boff)
1648 n = bp->b_bufsize - boff;
1649 bcopy((char *)cursor.data + roff, (char *)bp->b_data + boff, n);
1651 if (boff == bp->b_bufsize)
1653 error = hammer_ip_next(&cursor);
1655 hammer_done_cursor(&cursor);
1658 * There may have been a gap after the last record
1660 if (error == ENOENT)
1662 if (error == 0 && boff != bp->b_bufsize) {
1663 KKASSERT(boff < bp->b_bufsize);
1664 bzero((char *)bp->b_data + boff, bp->b_bufsize - boff);
1665 /* boff = bp->b_bufsize; */
1672 * Write to a regular file. Iterate the related records and mark for
1673 * deletion. If existing edge records (left and right side) overlap our
1674 * write they have to be marked deleted and new records created, usually
1675 * referencing a portion of the original data. Then add a record to
1676 * represent the buffer.
1678 * The support code in hammer_object.c should be used to deal with mixed
1679 * in-memory and on-disk records.
1683 hammer_vop_strategy_write(struct vop_strategy_args *ap)
1685 struct hammer_transaction trans;
1686 struct hammer_cursor *spike = NULL;
1694 ip = ap->a_vp->v_data;
1696 if (ip->flags & HAMMER_INODE_RO)
1700 * Start a transaction using the TID stored with the bp.
1702 KKASSERT(bp->b_tid != 0);
1703 hammer_start_transaction_tid(&trans, ip->hmp, bp->b_tid);
1707 * Delete any records overlapping our range. This function will
1708 * (eventually) properly truncate partial overlaps.
1710 if (ip->ino_rec.base.base.obj_type == HAMMER_OBJTYPE_DBFILE) {
1711 error = hammer_ip_delete_range(&trans, ip, bio->bio_offset,
1712 bio->bio_offset, &spike);
1714 error = hammer_ip_delete_range(&trans, ip, bio->bio_offset,
1721 * Add a single record to cover the write
1724 error = hammer_ip_sync_data(&trans, ip, bio->bio_offset,
1725 bp->b_data, bp->b_bufsize,
1730 * If we ran out of space the spike structure will be filled in
1731 * and we must call hammer_spike with it, then retry.
1733 if (error == ENOSPC) {
1734 error = hammer_spike(&spike);
1738 KKASSERT(spike == NULL);
1741 * If an error occured abort the transaction
1744 /* XXX undo deletion */
1745 hammer_abort_transaction(&trans);
1746 bp->b_resid = bp->b_bufsize;
1748 hammer_commit_transaction(&trans);
1756 * dounlink - disconnect a directory entry
1758 * XXX whiteout support not really in yet
1761 hammer_dounlink(struct nchandle *nch, struct vnode *dvp, struct ucred *cred,
1764 struct hammer_transaction trans;
1765 struct namecache *ncp;
1768 hammer_record_ondisk_t rec;
1769 struct hammer_cursor cursor;
1774 * Calculate the namekey and setup the key range for the scan. This
1775 * works kinda like a chained hash table where the lower 32 bits
1776 * of the namekey synthesize the chain.
1778 * The key range is inclusive of both key_beg and key_end.
1783 if (dip->flags & HAMMER_INODE_RO)
1786 hammer_start_transaction(&trans, dip->hmp);
1788 namekey = hammer_directory_namekey(ncp->nc_name, ncp->nc_nlen);
1790 hammer_init_cursor_hmp(&cursor, &dip->cache[0], dip->hmp);
1791 cursor.key_beg.obj_id = dip->obj_id;
1792 cursor.key_beg.key = namekey;
1793 cursor.key_beg.create_tid = 0;
1794 cursor.key_beg.delete_tid = 0;
1795 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1796 cursor.key_beg.obj_type = 0;
1798 cursor.key_end = cursor.key_beg;
1799 cursor.key_end.key |= 0xFFFFFFFFULL;
1800 cursor.asof = dip->obj_asof;
1801 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
1804 * Scan all matching records (the chain), locate the one matching
1805 * the requested path component. info->last_error contains the
1806 * error code on search termination and could be 0, ENOENT, or
1809 * The hammer_ip_*() functions merge in-memory records with on-disk
1810 * records for the purposes of the search.
1812 error = hammer_ip_first(&cursor, dip);
1813 while (error == 0) {
1814 error = hammer_ip_resolve_data(&cursor);
1817 rec = cursor.record;
1818 if (ncp->nc_nlen == rec->entry.base.data_len &&
1819 bcmp(ncp->nc_name, cursor.data, ncp->nc_nlen) == 0) {
1822 error = hammer_ip_next(&cursor);
1826 * If all is ok we have to get the inode so we can adjust nlinks.
1828 * If the target is a directory, it must be empty.
1831 ip = hammer_get_inode(dip->hmp, &dip->cache[1],
1833 dip->hmp->asof, 0, &error);
1834 if (error == ENOENT) {
1835 kprintf("obj_id %016llx\n", rec->entry.obj_id);
1836 Debugger("ENOENT unlinking object that should exist, cont to sync");
1837 hammer_sync_hmp(dip->hmp, MNT_NOWAIT);
1838 Debugger("ENOENT - sync done");
1840 if (error == 0 && ip->ino_rec.base.base.obj_type ==
1841 HAMMER_OBJTYPE_DIRECTORY) {
1842 error = hammer_ip_check_directory_empty(&trans, ip);
1845 * WARNING: hammer_ip_del_directory() may have to terminate
1846 * the cursor to avoid a lock recursion. It's ok to call
1847 * hammer_done_cursor() twice.
1850 error = hammer_ip_del_directory(&trans, &cursor, dip, ip);
1852 cache_setunresolved(nch);
1853 cache_setvp(nch, NULL);
1856 cache_inval_vp(ip->vp, CINV_DESTROY);
1858 hammer_rel_inode(ip, 0);
1860 hammer_done_cursor(&cursor);
1861 if (error == EDEADLK)
1865 hammer_commit_transaction(&trans);
1867 hammer_abort_transaction(&trans);
1871 /************************************************************************
1872 * FIFO AND SPECFS OPS *
1873 ************************************************************************
1878 hammer_vop_fifoclose (struct vop_close_args *ap)
1880 /* XXX update itimes */
1881 return (VOCALL(&fifo_vnode_vops, &ap->a_head));
1885 hammer_vop_fiforead (struct vop_read_args *ap)
1889 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
1890 /* XXX update access time */
1895 hammer_vop_fifowrite (struct vop_write_args *ap)
1899 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
1900 /* XXX update access time */
1905 hammer_vop_specclose (struct vop_close_args *ap)
1907 /* XXX update itimes */
1908 return (VOCALL(&spec_vnode_vops, &ap->a_head));
1912 hammer_vop_specread (struct vop_read_args *ap)
1914 /* XXX update access time */
1915 return (VOCALL(&spec_vnode_vops, &ap->a_head));
1919 hammer_vop_specwrite (struct vop_write_args *ap)
1921 /* XXX update last change time */
1922 return (VOCALL(&spec_vnode_vops, &ap->a_head));