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.73 2008/06/20 05:38:26 dillon Exp $
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/fcntl.h>
41 #include <sys/namecache.h>
42 #include <sys/vnode.h>
43 #include <sys/lockf.h>
44 #include <sys/event.h>
46 #include <sys/dirent.h>
47 #include <vm/vm_extern.h>
48 #include <vfs/fifofs/fifo.h>
54 /*static int hammer_vop_vnoperate(struct vop_generic_args *);*/
55 static int hammer_vop_fsync(struct vop_fsync_args *);
56 static int hammer_vop_read(struct vop_read_args *);
57 static int hammer_vop_write(struct vop_write_args *);
58 static int hammer_vop_access(struct vop_access_args *);
59 static int hammer_vop_advlock(struct vop_advlock_args *);
60 static int hammer_vop_close(struct vop_close_args *);
61 static int hammer_vop_ncreate(struct vop_ncreate_args *);
62 static int hammer_vop_getattr(struct vop_getattr_args *);
63 static int hammer_vop_nresolve(struct vop_nresolve_args *);
64 static int hammer_vop_nlookupdotdot(struct vop_nlookupdotdot_args *);
65 static int hammer_vop_nlink(struct vop_nlink_args *);
66 static int hammer_vop_nmkdir(struct vop_nmkdir_args *);
67 static int hammer_vop_nmknod(struct vop_nmknod_args *);
68 static int hammer_vop_open(struct vop_open_args *);
69 static int hammer_vop_pathconf(struct vop_pathconf_args *);
70 static int hammer_vop_print(struct vop_print_args *);
71 static int hammer_vop_readdir(struct vop_readdir_args *);
72 static int hammer_vop_readlink(struct vop_readlink_args *);
73 static int hammer_vop_nremove(struct vop_nremove_args *);
74 static int hammer_vop_nrename(struct vop_nrename_args *);
75 static int hammer_vop_nrmdir(struct vop_nrmdir_args *);
76 static int hammer_vop_setattr(struct vop_setattr_args *);
77 static int hammer_vop_strategy(struct vop_strategy_args *);
78 static int hammer_vop_bmap(struct vop_bmap_args *ap);
79 static int hammer_vop_nsymlink(struct vop_nsymlink_args *);
80 static int hammer_vop_nwhiteout(struct vop_nwhiteout_args *);
81 static int hammer_vop_ioctl(struct vop_ioctl_args *);
82 static int hammer_vop_mountctl(struct vop_mountctl_args *);
84 static int hammer_vop_fifoclose (struct vop_close_args *);
85 static int hammer_vop_fiforead (struct vop_read_args *);
86 static int hammer_vop_fifowrite (struct vop_write_args *);
88 static int hammer_vop_specclose (struct vop_close_args *);
89 static int hammer_vop_specread (struct vop_read_args *);
90 static int hammer_vop_specwrite (struct vop_write_args *);
92 struct vop_ops hammer_vnode_vops = {
93 .vop_default = vop_defaultop,
94 .vop_fsync = hammer_vop_fsync,
95 .vop_getpages = vop_stdgetpages,
96 .vop_putpages = vop_stdputpages,
97 .vop_read = hammer_vop_read,
98 .vop_write = hammer_vop_write,
99 .vop_access = hammer_vop_access,
100 .vop_advlock = hammer_vop_advlock,
101 .vop_close = hammer_vop_close,
102 .vop_ncreate = hammer_vop_ncreate,
103 .vop_getattr = hammer_vop_getattr,
104 .vop_inactive = hammer_vop_inactive,
105 .vop_reclaim = hammer_vop_reclaim,
106 .vop_nresolve = hammer_vop_nresolve,
107 .vop_nlookupdotdot = hammer_vop_nlookupdotdot,
108 .vop_nlink = hammer_vop_nlink,
109 .vop_nmkdir = hammer_vop_nmkdir,
110 .vop_nmknod = hammer_vop_nmknod,
111 .vop_open = hammer_vop_open,
112 .vop_pathconf = hammer_vop_pathconf,
113 .vop_print = hammer_vop_print,
114 .vop_readdir = hammer_vop_readdir,
115 .vop_readlink = hammer_vop_readlink,
116 .vop_nremove = hammer_vop_nremove,
117 .vop_nrename = hammer_vop_nrename,
118 .vop_nrmdir = hammer_vop_nrmdir,
119 .vop_setattr = hammer_vop_setattr,
120 .vop_bmap = hammer_vop_bmap,
121 .vop_strategy = hammer_vop_strategy,
122 .vop_nsymlink = hammer_vop_nsymlink,
123 .vop_nwhiteout = hammer_vop_nwhiteout,
124 .vop_ioctl = hammer_vop_ioctl,
125 .vop_mountctl = hammer_vop_mountctl
128 struct vop_ops hammer_spec_vops = {
129 .vop_default = spec_vnoperate,
130 .vop_fsync = hammer_vop_fsync,
131 .vop_read = hammer_vop_specread,
132 .vop_write = hammer_vop_specwrite,
133 .vop_access = hammer_vop_access,
134 .vop_close = hammer_vop_specclose,
135 .vop_getattr = hammer_vop_getattr,
136 .vop_inactive = hammer_vop_inactive,
137 .vop_reclaim = hammer_vop_reclaim,
138 .vop_setattr = hammer_vop_setattr
141 struct vop_ops hammer_fifo_vops = {
142 .vop_default = fifo_vnoperate,
143 .vop_fsync = hammer_vop_fsync,
144 .vop_read = hammer_vop_fiforead,
145 .vop_write = hammer_vop_fifowrite,
146 .vop_access = hammer_vop_access,
147 .vop_close = hammer_vop_fifoclose,
148 .vop_getattr = hammer_vop_getattr,
149 .vop_inactive = hammer_vop_inactive,
150 .vop_reclaim = hammer_vop_reclaim,
151 .vop_setattr = hammer_vop_setattr
154 #ifdef DEBUG_TRUNCATE
155 struct hammer_inode *HammerTruncIp;
158 static int hammer_dounlink(hammer_transaction_t trans, struct nchandle *nch,
159 struct vnode *dvp, struct ucred *cred, int flags);
160 static int hammer_vop_strategy_read(struct vop_strategy_args *ap);
161 static int hammer_vop_strategy_write(struct vop_strategy_args *ap);
162 static void hammer_cleanup_write_io(hammer_inode_t ip);
163 static void hammer_update_rsv_databufs(hammer_inode_t ip);
168 hammer_vop_vnoperate(struct vop_generic_args *)
170 return (VOCALL(&hammer_vnode_vops, ap));
175 * hammer_vop_fsync { vp, waitfor }
179 hammer_vop_fsync(struct vop_fsync_args *ap)
181 hammer_inode_t ip = VTOI(ap->a_vp);
183 vfsync(ap->a_vp, ap->a_waitfor, 1, NULL, NULL);
184 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
185 if (ap->a_waitfor == MNT_WAIT)
186 hammer_wait_inode(ip);
191 * hammer_vop_read { vp, uio, ioflag, cred }
195 hammer_vop_read(struct vop_read_args *ap)
197 struct hammer_transaction trans;
208 if (ap->a_vp->v_type != VREG)
215 * Allow the UIO's size to override the sequential heuristic.
217 blksize = hammer_blocksize(uio->uio_offset);
218 seqcount = (uio->uio_resid + (blksize - 1)) / blksize;
219 ioseqcount = ap->a_ioflag >> 16;
220 if (seqcount < ioseqcount)
221 seqcount = ioseqcount;
223 hammer_start_transaction(&trans, ip->hmp);
226 * Access the data typically in HAMMER_BUFSIZE blocks via the
227 * buffer cache, but HAMMER may use a variable block size based
230 while (uio->uio_resid > 0 && uio->uio_offset < ip->ino_data.size) {
234 blksize = hammer_blocksize(uio->uio_offset);
235 offset = (int)uio->uio_offset & (blksize - 1);
236 base_offset = uio->uio_offset - offset;
238 if (hammer_debug_cluster_enable) {
240 * Use file_limit to prevent cluster_read() from
241 * creating buffers of the wrong block size past
244 file_limit = ip->ino_data.size;
245 if (base_offset < HAMMER_XDEMARC &&
246 file_limit > HAMMER_XDEMARC) {
247 file_limit = HAMMER_XDEMARC;
249 error = cluster_read(ap->a_vp,
250 file_limit, base_offset,
254 error = bread(ap->a_vp, base_offset, blksize, &bp);
257 kprintf("error %d\n", error);
262 /* bp->b_flags |= B_CLUSTEROK; temporarily disabled */
263 n = blksize - offset;
264 if (n > uio->uio_resid)
266 if (n > ip->ino_data.size - uio->uio_offset)
267 n = (int)(ip->ino_data.size - uio->uio_offset);
268 error = uiomove((char *)bp->b_data + offset, n, uio);
270 /* data has a lower priority then meta-data */
271 bp->b_flags |= B_AGE;
276 if ((ip->flags & HAMMER_INODE_RO) == 0 &&
277 (ip->hmp->mp->mnt_flag & MNT_NOATIME) == 0) {
278 ip->ino_data.atime = trans.time;
279 hammer_modify_inode(ip, HAMMER_INODE_ITIMES);
281 hammer_done_transaction(&trans);
286 * hammer_vop_write { vp, uio, ioflag, cred }
290 hammer_vop_write(struct vop_write_args *ap)
292 struct hammer_transaction trans;
293 struct hammer_inode *ip;
306 if (ap->a_vp->v_type != VREG)
311 seqcount = ap->a_ioflag >> 16;
313 if (ip->flags & HAMMER_INODE_RO)
317 * Create a transaction to cover the operations we perform.
319 hammer_start_transaction(&trans, hmp);
325 if (ap->a_ioflag & IO_APPEND)
326 uio->uio_offset = ip->ino_data.size;
329 * Check for illegal write offsets. Valid range is 0...2^63-1.
331 * NOTE: the base_off assignment is required to work around what
332 * I consider to be a GCC-4 optimization bug.
334 if (uio->uio_offset < 0) {
335 hammer_done_transaction(&trans);
338 base_offset = uio->uio_offset + uio->uio_resid; /* work around gcc-4 */
339 if (uio->uio_resid > 0 && base_offset <= 0) {
340 hammer_done_transaction(&trans);
345 * Access the data typically in HAMMER_BUFSIZE blocks via the
346 * buffer cache, but HAMMER may use a variable block size based
350 while (uio->uio_resid > 0) {
355 if ((error = hammer_checkspace(hmp)) != 0)
359 * Do not allow HAMMER to blow out the buffer cache. Very
360 * large UIOs can lockout other processes due to bwillwrite()
363 * Do not allow HAMMER to blow out system memory by
364 * accumulating too many records. Records are so well
365 * decoupled from the buffer cache that it is possible
366 * for userland to push data out to the media via
367 * direct-write, but build up the records queued to the
368 * backend faster then the backend can flush them out.
369 * HAMMER has hit its write limit but the frontend has
370 * no pushback to slow it down.
372 * Always check at the beginning so separate writes are
373 * not able to bypass this code (count++).
375 * WARNING: Cannot unlock vp when doing a NOCOPY write as
376 * part of a putpages operation. Doing so could cause us
377 * to deadlock against the VM system when we try to re-lock.
379 if ((count++ & 15) == 0 || count > 64) {
383 if (uio->uio_segflg != UIO_NOCOPY) {
385 if ((ap->a_ioflag & IO_NOBWILL) == 0)
390 * Pending record flush check.
392 if (hmp->rsv_recs > hammer_limit_recs / 2) {
394 * Get the inode on the flush list
396 if (ip->rsv_recs >= 64)
397 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
398 else if (ip->rsv_recs >= 16)
399 hammer_flush_inode(ip, 0);
402 * Keep the flusher going if the system keeps
405 delta = hmp->count_newrecords -
406 hmp->last_newrecords;
407 if (delta < 0 || delta > hammer_limit_recs / 2) {
408 hmp->last_newrecords = hmp->count_newrecords;
409 hammer_sync_hmp(hmp, MNT_NOWAIT);
413 * If we have gotten behind start slowing
416 delta = (hmp->rsv_recs - hammer_limit_recs) *
417 hz / hammer_limit_recs;
419 tsleep(&trans, 0, "hmrslo", delta);
422 if (uio->uio_segflg != UIO_NOCOPY)
423 vn_lock(ap->a_vp, LK_EXCLUSIVE|LK_RETRY);
427 * Calculate the blocksize at the current offset and figure
428 * out how much we can actually write.
430 blksize = hammer_blocksize(uio->uio_offset);
431 blkmask = blksize - 1;
432 offset = (int)uio->uio_offset & blkmask;
433 base_offset = uio->uio_offset & ~(int64_t)blkmask;
434 n = blksize - offset;
435 if (n > uio->uio_resid)
437 if (uio->uio_offset + n > ip->ino_data.size) {
438 vnode_pager_setsize(ap->a_vp, uio->uio_offset + n);
442 if (uio->uio_segflg == UIO_NOCOPY) {
444 * Issuing a write with the same data backing the
445 * buffer. Instantiate the buffer to collect the
446 * backing vm pages, then read-in any missing bits.
448 * This case is used by vop_stdputpages().
450 bp = getblk(ap->a_vp, base_offset,
451 blksize, GETBLK_BHEAVY, 0);
452 if ((bp->b_flags & B_CACHE) == 0) {
454 error = bread(ap->a_vp, base_offset,
457 } else if (offset == 0 && uio->uio_resid >= blksize) {
459 * Even though we are entirely overwriting the buffer
460 * we may still have to zero it out to avoid a
461 * mmap/write visibility issue.
463 bp = getblk(ap->a_vp, base_offset, blksize, GETBLK_BHEAVY, 0);
464 if ((bp->b_flags & B_CACHE) == 0)
466 } else if (base_offset >= ip->ino_data.size) {
468 * If the base offset of the buffer is beyond the
469 * file EOF, we don't have to issue a read.
471 bp = getblk(ap->a_vp, base_offset,
472 blksize, GETBLK_BHEAVY, 0);
476 * Partial overwrite, read in any missing bits then
477 * replace the portion being written.
479 error = bread(ap->a_vp, base_offset, blksize, &bp);
484 error = uiomove((char *)bp->b_data + offset,
489 * If we screwed up we have to undo any VM size changes we
495 vtruncbuf(ap->a_vp, ip->ino_data.size,
496 hammer_blocksize(ip->ino_data.size));
500 /* bp->b_flags |= B_CLUSTEROK; temporarily disabled */
501 if (ip->ino_data.size < uio->uio_offset) {
502 ip->ino_data.size = uio->uio_offset;
503 flags = HAMMER_INODE_DDIRTY;
504 vnode_pager_setsize(ap->a_vp, ip->ino_data.size);
508 ip->ino_data.mtime = trans.time;
509 flags |= HAMMER_INODE_ITIMES | HAMMER_INODE_BUFS;
510 flags |= HAMMER_INODE_DDIRTY; /* XXX mtime */
511 hammer_modify_inode(ip, flags);
514 * Try to keep track of cached dirty data.
516 if ((bp->b_flags & B_DIRTY) == 0) {
522 * Final buffer disposition.
524 * If write_mode is non-zero we call bawrite()
525 * unconditionally. Otherwise we only use bawrite()
526 * if the writes are clearly sequential.
528 bp->b_flags |= B_AGE;
529 if (ap->a_ioflag & IO_SYNC) {
531 } else if (ap->a_ioflag & IO_DIRECT) {
537 else if (hammer_write_mode &&
538 ((int)uio->uio_offset & blkmask) == 0) {
540 bp->b_flags |= B_CLUSTEROK;
541 cluster_write(bp, ip->ino_data.size, XXX seqcount);
545 } else if ((ap->a_ioflag >> 16) == IO_SEQMAX &&
546 ((int)uio->uio_offset & blkmask) == 0) {
548 * If seqcount indicates sequential operation and
549 * we just finished filling a buffer, push it out
550 * now to prevent the buffer cache from becoming
551 * too full, which would trigger non-optimal
560 hammer_done_transaction(&trans);
565 * hammer_vop_access { vp, mode, cred }
569 hammer_vop_access(struct vop_access_args *ap)
571 struct hammer_inode *ip = VTOI(ap->a_vp);
576 uid = hammer_to_unix_xid(&ip->ino_data.uid);
577 gid = hammer_to_unix_xid(&ip->ino_data.gid);
579 error = vop_helper_access(ap, uid, gid, ip->ino_data.mode,
580 ip->ino_data.uflags);
585 * hammer_vop_advlock { vp, id, op, fl, flags }
589 hammer_vop_advlock(struct vop_advlock_args *ap)
591 hammer_inode_t ip = VTOI(ap->a_vp);
593 return (lf_advlock(ap, &ip->advlock, ip->ino_data.size));
597 * hammer_vop_close { vp, fflag }
601 hammer_vop_close(struct vop_close_args *ap)
603 hammer_inode_t ip = VTOI(ap->a_vp);
605 if ((ip->flags | ip->sync_flags) & HAMMER_INODE_MODMASK)
606 hammer_inode_waitreclaims(ip->hmp);
607 return (vop_stdclose(ap));
611 * hammer_vop_ncreate { nch, dvp, vpp, cred, vap }
613 * The operating system has already ensured that the directory entry
614 * does not exist and done all appropriate namespace locking.
618 hammer_vop_ncreate(struct vop_ncreate_args *ap)
620 struct hammer_transaction trans;
621 struct hammer_inode *dip;
622 struct hammer_inode *nip;
623 struct nchandle *nch;
627 dip = VTOI(ap->a_dvp);
629 if (dip->flags & HAMMER_INODE_RO)
631 if ((error = hammer_checkspace(dip->hmp)) != 0)
635 * Create a transaction to cover the operations we perform.
637 hammer_start_transaction(&trans, dip->hmp);
640 * Create a new filesystem object of the requested type. The
641 * returned inode will be referenced and shared-locked to prevent
642 * it from being moved to the flusher.
645 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred, dip, &nip);
647 hkprintf("hammer_create_inode error %d\n", error);
648 hammer_done_transaction(&trans);
654 * Add the new filesystem object to the directory. This will also
655 * bump the inode's link count.
657 error = hammer_ip_add_directory(&trans, dip, nch->ncp, nip);
659 hkprintf("hammer_ip_add_directory error %d\n", error);
665 hammer_rel_inode(nip, 0);
666 hammer_done_transaction(&trans);
669 error = hammer_get_vnode(nip, ap->a_vpp);
670 hammer_done_transaction(&trans);
671 hammer_rel_inode(nip, 0);
673 cache_setunresolved(ap->a_nch);
674 cache_setvp(ap->a_nch, *ap->a_vpp);
681 * hammer_vop_getattr { vp, vap }
683 * Retrieve an inode's attribute information. When accessing inodes
684 * historically we fake the atime field to ensure consistent results.
685 * The atime field is stored in the B-Tree element and allowed to be
686 * updated without cycling the element.
690 hammer_vop_getattr(struct vop_getattr_args *ap)
692 struct hammer_inode *ip = VTOI(ap->a_vp);
693 struct vattr *vap = ap->a_vap;
696 if (cache_check_fsmid_vp(ap->a_vp, &ip->fsmid) &&
697 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0 &&
702 hammer_itimes(ap->a_vp);
705 vap->va_fsid = ip->hmp->fsid_udev;
706 vap->va_fileid = ip->ino_leaf.base.obj_id;
707 vap->va_mode = ip->ino_data.mode;
708 vap->va_nlink = ip->ino_data.nlinks;
709 vap->va_uid = hammer_to_unix_xid(&ip->ino_data.uid);
710 vap->va_gid = hammer_to_unix_xid(&ip->ino_data.gid);
713 vap->va_size = ip->ino_data.size;
716 * We must provide a consistent atime and mtime for snapshots
717 * so people can do a 'tar cf - ... | md5' on them and get
718 * consistent results.
720 if (ip->flags & HAMMER_INODE_RO) {
721 hammer_to_timespec(ip->ino_data.ctime, &vap->va_atime);
722 hammer_to_timespec(ip->ino_data.ctime, &vap->va_mtime);
724 hammer_to_timespec(ip->ino_data.atime, &vap->va_atime);
725 hammer_to_timespec(ip->ino_data.mtime, &vap->va_mtime);
727 hammer_to_timespec(ip->ino_data.ctime, &vap->va_ctime);
728 vap->va_flags = ip->ino_data.uflags;
729 vap->va_gen = 1; /* hammer inums are unique for all time */
730 vap->va_blocksize = HAMMER_BUFSIZE;
731 if (ip->ino_data.size >= HAMMER_XDEMARC) {
732 vap->va_bytes = (ip->ino_data.size + HAMMER_XBUFMASK64) &
734 } else if (ip->ino_data.size > HAMMER_BUFSIZE / 2) {
735 vap->va_bytes = (ip->ino_data.size + HAMMER_BUFMASK64) &
738 vap->va_bytes = (ip->ino_data.size + 15) & ~15;
740 vap->va_type = hammer_get_vnode_type(ip->ino_data.obj_type);
741 vap->va_filerev = 0; /* XXX */
742 /* mtime uniquely identifies any adjustments made to the file XXX */
743 vap->va_fsmid = ip->ino_data.mtime;
744 vap->va_uid_uuid = ip->ino_data.uid;
745 vap->va_gid_uuid = ip->ino_data.gid;
746 vap->va_fsid_uuid = ip->hmp->fsid;
747 vap->va_vaflags = VA_UID_UUID_VALID | VA_GID_UUID_VALID |
750 switch (ip->ino_data.obj_type) {
751 case HAMMER_OBJTYPE_CDEV:
752 case HAMMER_OBJTYPE_BDEV:
753 vap->va_rmajor = ip->ino_data.rmajor;
754 vap->va_rminor = ip->ino_data.rminor;
764 * hammer_vop_nresolve { nch, dvp, cred }
766 * Locate the requested directory entry.
770 hammer_vop_nresolve(struct vop_nresolve_args *ap)
772 struct hammer_transaction trans;
773 struct namecache *ncp;
777 struct hammer_cursor cursor;
787 * Misc initialization, plus handle as-of name extensions. Look for
788 * the '@@' extension. Note that as-of files and directories cannot
791 dip = VTOI(ap->a_dvp);
792 ncp = ap->a_nch->ncp;
793 asof = dip->obj_asof;
797 hammer_simple_transaction(&trans, dip->hmp);
799 for (i = 0; i < nlen; ++i) {
800 if (ncp->nc_name[i] == '@' && ncp->nc_name[i+1] == '@') {
801 asof = hammer_str_to_tid(ncp->nc_name + i + 2);
802 flags |= HAMMER_INODE_RO;
809 * If there is no path component the time extension is relative to
813 ip = hammer_get_inode(&trans, dip, dip->obj_id,
814 asof, flags, &error);
816 error = hammer_get_vnode(ip, &vp);
817 hammer_rel_inode(ip, 0);
823 cache_setvp(ap->a_nch, vp);
830 * Calculate the namekey and setup the key range for the scan. This
831 * works kinda like a chained hash table where the lower 32 bits
832 * of the namekey synthesize the chain.
834 * The key range is inclusive of both key_beg and key_end.
836 namekey = hammer_directory_namekey(ncp->nc_name, nlen);
838 error = hammer_init_cursor(&trans, &cursor, &dip->cache[1], dip);
839 cursor.key_beg.localization = HAMMER_LOCALIZE_MISC;
840 cursor.key_beg.obj_id = dip->obj_id;
841 cursor.key_beg.key = namekey;
842 cursor.key_beg.create_tid = 0;
843 cursor.key_beg.delete_tid = 0;
844 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
845 cursor.key_beg.obj_type = 0;
847 cursor.key_end = cursor.key_beg;
848 cursor.key_end.key |= 0xFFFFFFFFULL;
850 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
853 * Scan all matching records (the chain), locate the one matching
854 * the requested path component.
856 * The hammer_ip_*() functions merge in-memory records with on-disk
857 * records for the purposes of the search.
862 error = hammer_ip_first(&cursor);
864 error = hammer_ip_resolve_data(&cursor);
867 if (nlen == cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF &&
868 bcmp(ncp->nc_name, cursor.data->entry.name, nlen) == 0) {
869 obj_id = cursor.data->entry.obj_id;
872 error = hammer_ip_next(&cursor);
875 hammer_done_cursor(&cursor);
877 ip = hammer_get_inode(&trans, dip, obj_id,
878 asof, flags, &error);
880 error = hammer_get_vnode(ip, &vp);
881 hammer_rel_inode(ip, 0);
887 cache_setvp(ap->a_nch, vp);
890 } else if (error == ENOENT) {
891 cache_setvp(ap->a_nch, NULL);
894 hammer_done_transaction(&trans);
899 * hammer_vop_nlookupdotdot { dvp, vpp, cred }
901 * Locate the parent directory of a directory vnode.
903 * dvp is referenced but not locked. *vpp must be returned referenced and
904 * locked. A parent_obj_id of 0 does not necessarily indicate that we are
905 * at the root, instead it could indicate that the directory we were in was
908 * NOTE: as-of sequences are not linked into the directory structure. If
909 * we are at the root with a different asof then the mount point, reload
910 * the same directory with the mount point's asof. I'm not sure what this
911 * will do to NFS. We encode ASOF stamps in NFS file handles so it might not
912 * get confused, but it hasn't been tested.
916 hammer_vop_nlookupdotdot(struct vop_nlookupdotdot_args *ap)
918 struct hammer_transaction trans;
919 struct hammer_inode *dip;
920 struct hammer_inode *ip;
921 int64_t parent_obj_id;
925 dip = VTOI(ap->a_dvp);
926 asof = dip->obj_asof;
927 parent_obj_id = dip->ino_data.parent_obj_id;
929 if (parent_obj_id == 0) {
930 if (dip->obj_id == HAMMER_OBJID_ROOT &&
931 asof != dip->hmp->asof) {
932 parent_obj_id = dip->obj_id;
933 asof = dip->hmp->asof;
934 *ap->a_fakename = kmalloc(19, M_TEMP, M_WAITOK);
935 ksnprintf(*ap->a_fakename, 19, "0x%016llx",
943 hammer_simple_transaction(&trans, dip->hmp);
945 ip = hammer_get_inode(&trans, dip, parent_obj_id,
946 asof, dip->flags, &error);
948 error = hammer_get_vnode(ip, ap->a_vpp);
949 hammer_rel_inode(ip, 0);
953 hammer_done_transaction(&trans);
958 * hammer_vop_nlink { nch, dvp, vp, cred }
962 hammer_vop_nlink(struct vop_nlink_args *ap)
964 struct hammer_transaction trans;
965 struct hammer_inode *dip;
966 struct hammer_inode *ip;
967 struct nchandle *nch;
971 dip = VTOI(ap->a_dvp);
974 if (dip->flags & HAMMER_INODE_RO)
976 if (ip->flags & HAMMER_INODE_RO)
978 if ((error = hammer_checkspace(dip->hmp)) != 0)
982 * Create a transaction to cover the operations we perform.
984 hammer_start_transaction(&trans, dip->hmp);
987 * Add the filesystem object to the directory. Note that neither
988 * dip nor ip are referenced or locked, but their vnodes are
989 * referenced. This function will bump the inode's link count.
991 error = hammer_ip_add_directory(&trans, dip, nch->ncp, ip);
997 cache_setunresolved(nch);
998 cache_setvp(nch, ap->a_vp);
1000 hammer_done_transaction(&trans);
1005 * hammer_vop_nmkdir { nch, dvp, vpp, cred, vap }
1007 * The operating system has already ensured that the directory entry
1008 * does not exist and done all appropriate namespace locking.
1012 hammer_vop_nmkdir(struct vop_nmkdir_args *ap)
1014 struct hammer_transaction trans;
1015 struct hammer_inode *dip;
1016 struct hammer_inode *nip;
1017 struct nchandle *nch;
1021 dip = VTOI(ap->a_dvp);
1023 if (dip->flags & HAMMER_INODE_RO)
1025 if ((error = hammer_checkspace(dip->hmp)) != 0)
1029 * Create a transaction to cover the operations we perform.
1031 hammer_start_transaction(&trans, dip->hmp);
1034 * Create a new filesystem object of the requested type. The
1035 * returned inode will be referenced but not locked.
1037 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred, dip, &nip);
1039 hkprintf("hammer_mkdir error %d\n", error);
1040 hammer_done_transaction(&trans);
1045 * Add the new filesystem object to the directory. This will also
1046 * bump the inode's link count.
1048 error = hammer_ip_add_directory(&trans, dip, nch->ncp, nip);
1050 hkprintf("hammer_mkdir (add) error %d\n", error);
1056 hammer_rel_inode(nip, 0);
1059 error = hammer_get_vnode(nip, ap->a_vpp);
1060 hammer_rel_inode(nip, 0);
1062 cache_setunresolved(ap->a_nch);
1063 cache_setvp(ap->a_nch, *ap->a_vpp);
1066 hammer_done_transaction(&trans);
1071 * hammer_vop_nmknod { nch, dvp, vpp, cred, vap }
1073 * The operating system has already ensured that the directory entry
1074 * does not exist and done all appropriate namespace locking.
1078 hammer_vop_nmknod(struct vop_nmknod_args *ap)
1080 struct hammer_transaction trans;
1081 struct hammer_inode *dip;
1082 struct hammer_inode *nip;
1083 struct nchandle *nch;
1087 dip = VTOI(ap->a_dvp);
1089 if (dip->flags & HAMMER_INODE_RO)
1091 if ((error = hammer_checkspace(dip->hmp)) != 0)
1095 * Create a transaction to cover the operations we perform.
1097 hammer_start_transaction(&trans, dip->hmp);
1100 * Create a new filesystem object of the requested type. The
1101 * returned inode will be referenced but not locked.
1103 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred, dip, &nip);
1105 hammer_done_transaction(&trans);
1111 * Add the new filesystem object to the directory. This will also
1112 * bump the inode's link count.
1114 error = hammer_ip_add_directory(&trans, dip, nch->ncp, nip);
1120 hammer_rel_inode(nip, 0);
1123 error = hammer_get_vnode(nip, ap->a_vpp);
1124 hammer_rel_inode(nip, 0);
1126 cache_setunresolved(ap->a_nch);
1127 cache_setvp(ap->a_nch, *ap->a_vpp);
1130 hammer_done_transaction(&trans);
1135 * hammer_vop_open { vp, mode, cred, fp }
1139 hammer_vop_open(struct vop_open_args *ap)
1143 ip = VTOI(ap->a_vp);
1145 if ((ap->a_mode & FWRITE) && (ip->flags & HAMMER_INODE_RO))
1147 return(vop_stdopen(ap));
1151 * hammer_vop_pathconf { vp, name, retval }
1155 hammer_vop_pathconf(struct vop_pathconf_args *ap)
1161 * hammer_vop_print { vp }
1165 hammer_vop_print(struct vop_print_args *ap)
1171 * hammer_vop_readdir { vp, uio, cred, *eofflag, *ncookies, off_t **cookies }
1175 hammer_vop_readdir(struct vop_readdir_args *ap)
1177 struct hammer_transaction trans;
1178 struct hammer_cursor cursor;
1179 struct hammer_inode *ip;
1181 hammer_base_elm_t base;
1189 ip = VTOI(ap->a_vp);
1191 saveoff = uio->uio_offset;
1193 if (ap->a_ncookies) {
1194 ncookies = uio->uio_resid / 16 + 1;
1195 if (ncookies > 1024)
1197 cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
1205 hammer_simple_transaction(&trans, ip->hmp);
1208 * Handle artificial entries
1212 r = vop_write_dirent(&error, uio, ip->obj_id, DT_DIR, 1, ".");
1216 cookies[cookie_index] = saveoff;
1219 if (cookie_index == ncookies)
1223 if (ip->ino_data.parent_obj_id) {
1224 r = vop_write_dirent(&error, uio,
1225 ip->ino_data.parent_obj_id,
1228 r = vop_write_dirent(&error, uio,
1229 ip->obj_id, DT_DIR, 2, "..");
1234 cookies[cookie_index] = saveoff;
1237 if (cookie_index == ncookies)
1242 * Key range (begin and end inclusive) to scan. Directory keys
1243 * directly translate to a 64 bit 'seek' position.
1245 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
1246 cursor.key_beg.localization = HAMMER_LOCALIZE_MISC;
1247 cursor.key_beg.obj_id = ip->obj_id;
1248 cursor.key_beg.create_tid = 0;
1249 cursor.key_beg.delete_tid = 0;
1250 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1251 cursor.key_beg.obj_type = 0;
1252 cursor.key_beg.key = saveoff;
1254 cursor.key_end = cursor.key_beg;
1255 cursor.key_end.key = HAMMER_MAX_KEY;
1256 cursor.asof = ip->obj_asof;
1257 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
1259 error = hammer_ip_first(&cursor);
1261 while (error == 0) {
1262 error = hammer_ip_resolve_data(&cursor);
1265 base = &cursor.leaf->base;
1266 saveoff = base->key;
1267 KKASSERT(cursor.leaf->data_len > HAMMER_ENTRY_NAME_OFF);
1269 if (base->obj_id != ip->obj_id)
1270 panic("readdir: bad record at %p", cursor.node);
1272 r = vop_write_dirent(
1273 &error, uio, cursor.data->entry.obj_id,
1274 hammer_get_dtype(cursor.leaf->base.obj_type),
1275 cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF ,
1276 (void *)cursor.data->entry.name);
1281 cookies[cookie_index] = base->key;
1283 if (cookie_index == ncookies)
1285 error = hammer_ip_next(&cursor);
1287 hammer_done_cursor(&cursor);
1290 hammer_done_transaction(&trans);
1293 *ap->a_eofflag = (error == ENOENT);
1294 uio->uio_offset = saveoff;
1295 if (error && cookie_index == 0) {
1296 if (error == ENOENT)
1299 kfree(cookies, M_TEMP);
1300 *ap->a_ncookies = 0;
1301 *ap->a_cookies = NULL;
1304 if (error == ENOENT)
1307 *ap->a_ncookies = cookie_index;
1308 *ap->a_cookies = cookies;
1315 * hammer_vop_readlink { vp, uio, cred }
1319 hammer_vop_readlink(struct vop_readlink_args *ap)
1321 struct hammer_transaction trans;
1322 struct hammer_cursor cursor;
1323 struct hammer_inode *ip;
1326 ip = VTOI(ap->a_vp);
1329 * Shortcut if the symlink data was stuffed into ino_data.
1331 if (ip->ino_data.size <= HAMMER_INODE_BASESYMLEN) {
1332 error = uiomove(ip->ino_data.ext.symlink,
1333 ip->ino_data.size, ap->a_uio);
1340 hammer_simple_transaction(&trans, ip->hmp);
1341 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
1344 * Key range (begin and end inclusive) to scan. Directory keys
1345 * directly translate to a 64 bit 'seek' position.
1347 cursor.key_beg.localization = HAMMER_LOCALIZE_MISC; /* XXX */
1348 cursor.key_beg.obj_id = ip->obj_id;
1349 cursor.key_beg.create_tid = 0;
1350 cursor.key_beg.delete_tid = 0;
1351 cursor.key_beg.rec_type = HAMMER_RECTYPE_FIX;
1352 cursor.key_beg.obj_type = 0;
1353 cursor.key_beg.key = HAMMER_FIXKEY_SYMLINK;
1354 cursor.asof = ip->obj_asof;
1355 cursor.flags |= HAMMER_CURSOR_ASOF;
1357 error = hammer_ip_lookup(&cursor);
1359 error = hammer_ip_resolve_data(&cursor);
1361 KKASSERT(cursor.leaf->data_len >=
1362 HAMMER_SYMLINK_NAME_OFF);
1363 error = uiomove(cursor.data->symlink.name,
1364 cursor.leaf->data_len -
1365 HAMMER_SYMLINK_NAME_OFF,
1369 hammer_done_cursor(&cursor);
1370 hammer_done_transaction(&trans);
1375 * hammer_vop_nremove { nch, dvp, cred }
1379 hammer_vop_nremove(struct vop_nremove_args *ap)
1381 struct hammer_transaction trans;
1382 struct hammer_inode *dip;
1385 dip = VTOI(ap->a_dvp);
1387 if (hammer_nohistory(dip) == 0 &&
1388 (error = hammer_checkspace(dip->hmp)) != 0) {
1392 hammer_start_transaction(&trans, dip->hmp);
1393 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp, ap->a_cred, 0);
1394 hammer_done_transaction(&trans);
1400 * hammer_vop_nrename { fnch, tnch, fdvp, tdvp, cred }
1404 hammer_vop_nrename(struct vop_nrename_args *ap)
1406 struct hammer_transaction trans;
1407 struct namecache *fncp;
1408 struct namecache *tncp;
1409 struct hammer_inode *fdip;
1410 struct hammer_inode *tdip;
1411 struct hammer_inode *ip;
1412 struct hammer_cursor cursor;
1416 fdip = VTOI(ap->a_fdvp);
1417 tdip = VTOI(ap->a_tdvp);
1418 fncp = ap->a_fnch->ncp;
1419 tncp = ap->a_tnch->ncp;
1420 ip = VTOI(fncp->nc_vp);
1421 KKASSERT(ip != NULL);
1423 if (fdip->flags & HAMMER_INODE_RO)
1425 if (tdip->flags & HAMMER_INODE_RO)
1427 if (ip->flags & HAMMER_INODE_RO)
1429 if ((error = hammer_checkspace(fdip->hmp)) != 0)
1432 hammer_start_transaction(&trans, fdip->hmp);
1435 * Remove tncp from the target directory and then link ip as
1436 * tncp. XXX pass trans to dounlink
1438 * Force the inode sync-time to match the transaction so it is
1439 * in-sync with the creation of the target directory entry.
1441 error = hammer_dounlink(&trans, ap->a_tnch, ap->a_tdvp, ap->a_cred, 0);
1442 if (error == 0 || error == ENOENT) {
1443 error = hammer_ip_add_directory(&trans, tdip, tncp, ip);
1445 ip->ino_data.parent_obj_id = tdip->obj_id;
1446 hammer_modify_inode(ip, HAMMER_INODE_DDIRTY);
1450 goto failed; /* XXX */
1453 * Locate the record in the originating directory and remove it.
1455 * Calculate the namekey and setup the key range for the scan. This
1456 * works kinda like a chained hash table where the lower 32 bits
1457 * of the namekey synthesize the chain.
1459 * The key range is inclusive of both key_beg and key_end.
1461 namekey = hammer_directory_namekey(fncp->nc_name, fncp->nc_nlen);
1463 hammer_init_cursor(&trans, &cursor, &fdip->cache[1], fdip);
1464 cursor.key_beg.localization = HAMMER_LOCALIZE_MISC;
1465 cursor.key_beg.obj_id = fdip->obj_id;
1466 cursor.key_beg.key = namekey;
1467 cursor.key_beg.create_tid = 0;
1468 cursor.key_beg.delete_tid = 0;
1469 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1470 cursor.key_beg.obj_type = 0;
1472 cursor.key_end = cursor.key_beg;
1473 cursor.key_end.key |= 0xFFFFFFFFULL;
1474 cursor.asof = fdip->obj_asof;
1475 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
1478 * Scan all matching records (the chain), locate the one matching
1479 * the requested path component.
1481 * The hammer_ip_*() functions merge in-memory records with on-disk
1482 * records for the purposes of the search.
1484 error = hammer_ip_first(&cursor);
1485 while (error == 0) {
1486 if (hammer_ip_resolve_data(&cursor) != 0)
1488 nlen = cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF;
1490 if (fncp->nc_nlen == nlen &&
1491 bcmp(fncp->nc_name, cursor.data->entry.name, nlen) == 0) {
1494 error = hammer_ip_next(&cursor);
1498 * If all is ok we have to get the inode so we can adjust nlinks.
1500 * WARNING: hammer_ip_del_directory() may have to terminate the
1501 * cursor to avoid a recursion. It's ok to call hammer_done_cursor()
1505 error = hammer_ip_del_directory(&trans, &cursor, fdip, ip);
1508 * XXX A deadlock here will break rename's atomicy for the purposes
1509 * of crash recovery.
1511 if (error == EDEADLK) {
1512 hammer_done_cursor(&cursor);
1517 * Cleanup and tell the kernel that the rename succeeded.
1519 hammer_done_cursor(&cursor);
1521 cache_rename(ap->a_fnch, ap->a_tnch);
1524 hammer_done_transaction(&trans);
1529 * hammer_vop_nrmdir { nch, dvp, cred }
1533 hammer_vop_nrmdir(struct vop_nrmdir_args *ap)
1535 struct hammer_transaction trans;
1536 struct hammer_inode *dip;
1539 dip = VTOI(ap->a_dvp);
1541 if (hammer_nohistory(dip) == 0 &&
1542 (error = hammer_checkspace(dip->hmp)) != 0) {
1546 hammer_start_transaction(&trans, dip->hmp);
1547 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp, ap->a_cred, 0);
1548 hammer_done_transaction(&trans);
1554 * hammer_vop_setattr { vp, vap, cred }
1558 hammer_vop_setattr(struct vop_setattr_args *ap)
1560 struct hammer_transaction trans;
1562 struct hammer_inode *ip;
1567 int64_t aligned_size;
1571 ip = ap->a_vp->v_data;
1574 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
1576 if (ip->flags & HAMMER_INODE_RO)
1578 if (hammer_nohistory(ip) == 0 &&
1579 (error = hammer_checkspace(ip->hmp)) != 0) {
1583 hammer_start_transaction(&trans, ip->hmp);
1586 if (vap->va_flags != VNOVAL) {
1587 flags = ip->ino_data.uflags;
1588 error = vop_helper_setattr_flags(&flags, vap->va_flags,
1589 hammer_to_unix_xid(&ip->ino_data.uid),
1592 if (ip->ino_data.uflags != flags) {
1593 ip->ino_data.uflags = flags;
1594 modflags |= HAMMER_INODE_DDIRTY;
1596 if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) {
1603 if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) {
1607 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
1608 mode_t cur_mode = ip->ino_data.mode;
1609 uid_t cur_uid = hammer_to_unix_xid(&ip->ino_data.uid);
1610 gid_t cur_gid = hammer_to_unix_xid(&ip->ino_data.gid);
1614 error = vop_helper_chown(ap->a_vp, vap->va_uid, vap->va_gid,
1616 &cur_uid, &cur_gid, &cur_mode);
1618 hammer_guid_to_uuid(&uuid_uid, cur_uid);
1619 hammer_guid_to_uuid(&uuid_gid, cur_gid);
1620 if (bcmp(&uuid_uid, &ip->ino_data.uid,
1621 sizeof(uuid_uid)) ||
1622 bcmp(&uuid_gid, &ip->ino_data.gid,
1623 sizeof(uuid_gid)) ||
1624 ip->ino_data.mode != cur_mode
1626 ip->ino_data.uid = uuid_uid;
1627 ip->ino_data.gid = uuid_gid;
1628 ip->ino_data.mode = cur_mode;
1630 modflags |= HAMMER_INODE_DDIRTY;
1633 while (vap->va_size != VNOVAL && ip->ino_data.size != vap->va_size) {
1634 switch(ap->a_vp->v_type) {
1636 if (vap->va_size == ip->ino_data.size)
1639 * XXX break atomicy, we can deadlock the backend
1640 * if we do not release the lock. Probably not a
1643 blksize = hammer_blocksize(vap->va_size);
1644 if (vap->va_size < ip->ino_data.size) {
1645 vtruncbuf(ap->a_vp, vap->va_size, blksize);
1648 vnode_pager_setsize(ap->a_vp, vap->va_size);
1651 ip->ino_data.size = vap->va_size;
1652 modflags |= HAMMER_INODE_DDIRTY;
1655 * on-media truncation is cached in the inode until
1656 * the inode is synchronized.
1659 hammer_ip_frontend_trunc(ip, vap->va_size);
1660 hammer_update_rsv_databufs(ip);
1661 #ifdef DEBUG_TRUNCATE
1662 if (HammerTruncIp == NULL)
1665 if ((ip->flags & HAMMER_INODE_TRUNCATED) == 0) {
1666 ip->flags |= HAMMER_INODE_TRUNCATED;
1667 ip->trunc_off = vap->va_size;
1668 #ifdef DEBUG_TRUNCATE
1669 if (ip == HammerTruncIp)
1670 kprintf("truncate1 %016llx\n", ip->trunc_off);
1672 } else if (ip->trunc_off > vap->va_size) {
1673 ip->trunc_off = vap->va_size;
1674 #ifdef DEBUG_TRUNCATE
1675 if (ip == HammerTruncIp)
1676 kprintf("truncate2 %016llx\n", ip->trunc_off);
1679 #ifdef DEBUG_TRUNCATE
1680 if (ip == HammerTruncIp)
1681 kprintf("truncate3 %016llx (ignored)\n", vap->va_size);
1687 * If truncating we have to clean out a portion of
1688 * the last block on-disk. We do this in the
1689 * front-end buffer cache.
1691 aligned_size = (vap->va_size + (blksize - 1)) &
1692 ~(int64_t)(blksize - 1);
1693 if (truncating && vap->va_size < aligned_size) {
1697 aligned_size -= blksize;
1699 offset = (int)vap->va_size & (blksize - 1);
1700 error = bread(ap->a_vp, aligned_size,
1702 hammer_ip_frontend_trunc(ip, aligned_size);
1704 bzero(bp->b_data + offset,
1708 kprintf("ERROR %d\n", error);
1714 if ((ip->flags & HAMMER_INODE_TRUNCATED) == 0) {
1715 ip->flags |= HAMMER_INODE_TRUNCATED;
1716 ip->trunc_off = vap->va_size;
1717 } else if (ip->trunc_off > vap->va_size) {
1718 ip->trunc_off = vap->va_size;
1720 hammer_ip_frontend_trunc(ip, vap->va_size);
1721 ip->ino_data.size = vap->va_size;
1722 modflags |= HAMMER_INODE_DDIRTY;
1730 if (vap->va_atime.tv_sec != VNOVAL) {
1731 ip->ino_data.atime =
1732 hammer_timespec_to_transid(&vap->va_atime);
1733 modflags |= HAMMER_INODE_ITIMES;
1735 if (vap->va_mtime.tv_sec != VNOVAL) {
1736 ip->ino_data.mtime =
1737 hammer_timespec_to_transid(&vap->va_mtime);
1738 modflags |= HAMMER_INODE_ITIMES;
1739 modflags |= HAMMER_INODE_DDIRTY; /* XXX mtime */
1741 if (vap->va_mode != (mode_t)VNOVAL) {
1742 mode_t cur_mode = ip->ino_data.mode;
1743 uid_t cur_uid = hammer_to_unix_xid(&ip->ino_data.uid);
1744 gid_t cur_gid = hammer_to_unix_xid(&ip->ino_data.gid);
1746 error = vop_helper_chmod(ap->a_vp, vap->va_mode, ap->a_cred,
1747 cur_uid, cur_gid, &cur_mode);
1748 if (error == 0 && ip->ino_data.mode != cur_mode) {
1749 ip->ino_data.mode = cur_mode;
1750 modflags |= HAMMER_INODE_DDIRTY;
1755 hammer_modify_inode(ip, modflags);
1756 hammer_done_transaction(&trans);
1761 * hammer_vop_nsymlink { nch, dvp, vpp, cred, vap, target }
1765 hammer_vop_nsymlink(struct vop_nsymlink_args *ap)
1767 struct hammer_transaction trans;
1768 struct hammer_inode *dip;
1769 struct hammer_inode *nip;
1770 struct nchandle *nch;
1771 hammer_record_t record;
1775 ap->a_vap->va_type = VLNK;
1778 dip = VTOI(ap->a_dvp);
1780 if (dip->flags & HAMMER_INODE_RO)
1782 if ((error = hammer_checkspace(dip->hmp)) != 0)
1786 * Create a transaction to cover the operations we perform.
1788 hammer_start_transaction(&trans, dip->hmp);
1791 * Create a new filesystem object of the requested type. The
1792 * returned inode will be referenced but not locked.
1795 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred, dip, &nip);
1797 hammer_done_transaction(&trans);
1803 * Add a record representing the symlink. symlink stores the link
1804 * as pure data, not a string, and is no \0 terminated.
1807 bytes = strlen(ap->a_target);
1809 if (bytes <= HAMMER_INODE_BASESYMLEN) {
1810 bcopy(ap->a_target, nip->ino_data.ext.symlink, bytes);
1812 record = hammer_alloc_mem_record(nip, bytes);
1813 record->type = HAMMER_MEM_RECORD_GENERAL;
1815 record->leaf.base.localization = HAMMER_LOCALIZE_MISC;
1816 record->leaf.base.key = HAMMER_FIXKEY_SYMLINK;
1817 record->leaf.base.rec_type = HAMMER_RECTYPE_FIX;
1818 record->leaf.data_len = bytes;
1819 KKASSERT(HAMMER_SYMLINK_NAME_OFF == 0);
1820 bcopy(ap->a_target, record->data->symlink.name, bytes);
1821 error = hammer_ip_add_record(&trans, record);
1825 * Set the file size to the length of the link.
1828 nip->ino_data.size = bytes;
1829 hammer_modify_inode(nip, HAMMER_INODE_DDIRTY);
1833 error = hammer_ip_add_directory(&trans, dip, nch->ncp, nip);
1839 hammer_rel_inode(nip, 0);
1842 error = hammer_get_vnode(nip, ap->a_vpp);
1843 hammer_rel_inode(nip, 0);
1845 cache_setunresolved(ap->a_nch);
1846 cache_setvp(ap->a_nch, *ap->a_vpp);
1849 hammer_done_transaction(&trans);
1854 * hammer_vop_nwhiteout { nch, dvp, cred, flags }
1858 hammer_vop_nwhiteout(struct vop_nwhiteout_args *ap)
1860 struct hammer_transaction trans;
1861 struct hammer_inode *dip;
1864 dip = VTOI(ap->a_dvp);
1866 if (hammer_nohistory(dip) == 0 &&
1867 (error = hammer_checkspace(dip->hmp)) != 0) {
1871 hammer_start_transaction(&trans, dip->hmp);
1872 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp,
1873 ap->a_cred, ap->a_flags);
1874 hammer_done_transaction(&trans);
1880 * hammer_vop_ioctl { vp, command, data, fflag, cred }
1884 hammer_vop_ioctl(struct vop_ioctl_args *ap)
1886 struct hammer_inode *ip = ap->a_vp->v_data;
1888 return(hammer_ioctl(ip, ap->a_command, ap->a_data,
1889 ap->a_fflag, ap->a_cred));
1894 hammer_vop_mountctl(struct vop_mountctl_args *ap)
1899 mp = ap->a_head.a_ops->head.vv_mount;
1902 case MOUNTCTL_SET_EXPORT:
1903 if (ap->a_ctllen != sizeof(struct export_args))
1905 error = hammer_vfs_export(mp, ap->a_op,
1906 (const struct export_args *)ap->a_ctl);
1909 error = journal_mountctl(ap);
1916 * hammer_vop_strategy { vp, bio }
1918 * Strategy call, used for regular file read & write only. Note that the
1919 * bp may represent a cluster.
1921 * To simplify operation and allow better optimizations in the future,
1922 * this code does not make any assumptions with regards to buffer alignment
1927 hammer_vop_strategy(struct vop_strategy_args *ap)
1932 bp = ap->a_bio->bio_buf;
1936 error = hammer_vop_strategy_read(ap);
1939 error = hammer_vop_strategy_write(ap);
1942 bp->b_error = error = EINVAL;
1943 bp->b_flags |= B_ERROR;
1951 * Read from a regular file. Iterate the related records and fill in the
1952 * BIO/BUF. Gaps are zero-filled.
1954 * The support code in hammer_object.c should be used to deal with mixed
1955 * in-memory and on-disk records.
1957 * NOTE: Can be called from the cluster code with an oversized buf.
1963 hammer_vop_strategy_read(struct vop_strategy_args *ap)
1965 struct hammer_transaction trans;
1966 struct hammer_inode *ip;
1967 struct hammer_cursor cursor;
1968 hammer_base_elm_t base;
1969 hammer_off_t disk_offset;
1983 ip = ap->a_vp->v_data;
1986 * The zone-2 disk offset may have been set by the cluster code via
1987 * a BMAP operation, or else should be NOOFFSET.
1989 * Checking the high bits for a match against zone-2 should suffice.
1991 nbio = push_bio(bio);
1992 if ((nbio->bio_offset & HAMMER_OFF_ZONE_MASK) ==
1993 HAMMER_ZONE_RAW_BUFFER) {
1994 error = hammer_io_direct_read(ip->hmp, nbio);
1999 * Well, that sucked. Do it the hard way. If all the stars are
2000 * aligned we may still be able to issue a direct-read.
2002 hammer_simple_transaction(&trans, ip->hmp);
2003 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
2006 * Key range (begin and end inclusive) to scan. Note that the key's
2007 * stored in the actual records represent BASE+LEN, not BASE. The
2008 * first record containing bio_offset will have a key > bio_offset.
2010 cursor.key_beg.localization = HAMMER_LOCALIZE_MISC;
2011 cursor.key_beg.obj_id = ip->obj_id;
2012 cursor.key_beg.create_tid = 0;
2013 cursor.key_beg.delete_tid = 0;
2014 cursor.key_beg.obj_type = 0;
2015 cursor.key_beg.key = bio->bio_offset + 1;
2016 cursor.asof = ip->obj_asof;
2017 cursor.flags |= HAMMER_CURSOR_ASOF;
2019 cursor.key_end = cursor.key_beg;
2020 KKASSERT(ip->ino_data.obj_type == HAMMER_OBJTYPE_REGFILE);
2022 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_DBFILE) {
2023 cursor.key_beg.rec_type = HAMMER_RECTYPE_DB;
2024 cursor.key_end.rec_type = HAMMER_RECTYPE_DB;
2025 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
2029 ran_end = bio->bio_offset + bp->b_bufsize;
2030 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
2031 cursor.key_end.rec_type = HAMMER_RECTYPE_DATA;
2032 tmp64 = ran_end + MAXPHYS + 1; /* work-around GCC-4 bug */
2033 if (tmp64 < ran_end)
2034 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
2036 cursor.key_end.key = ran_end + MAXPHYS + 1;
2038 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
2040 error = hammer_ip_first(&cursor);
2043 while (error == 0) {
2045 * Get the base file offset of the record. The key for
2046 * data records is (base + bytes) rather then (base).
2048 base = &cursor.leaf->base;
2049 rec_offset = base->key - cursor.leaf->data_len;
2052 * Calculate the gap, if any, and zero-fill it.
2054 * n is the offset of the start of the record verses our
2055 * current seek offset in the bio.
2057 n = (int)(rec_offset - (bio->bio_offset + boff));
2059 if (n > bp->b_bufsize - boff)
2060 n = bp->b_bufsize - boff;
2061 bzero((char *)bp->b_data + boff, n);
2067 * Calculate the data offset in the record and the number
2068 * of bytes we can copy.
2070 * There are two degenerate cases. First, boff may already
2071 * be at bp->b_bufsize. Secondly, the data offset within
2072 * the record may exceed the record's size.
2076 n = cursor.leaf->data_len - roff;
2078 kprintf("strategy_read: bad n=%d roff=%d\n", n, roff);
2080 } else if (n > bp->b_bufsize - boff) {
2081 n = bp->b_bufsize - boff;
2085 * Deal with cached truncations. This cool bit of code
2086 * allows truncate()/ftruncate() to avoid having to sync
2089 * If the frontend is truncated then all backend records are
2090 * subject to the frontend's truncation.
2092 * If the backend is truncated then backend records on-disk
2093 * (but not in-memory) are subject to the backend's
2094 * truncation. In-memory records owned by the backend
2095 * represent data written after the truncation point on the
2096 * backend and must not be truncated.
2098 * Truncate operations deal with frontend buffer cache
2099 * buffers and frontend-owned in-memory records synchronously.
2101 if (ip->flags & HAMMER_INODE_TRUNCATED) {
2102 if (hammer_cursor_ondisk(&cursor) ||
2103 cursor.iprec->flush_state == HAMMER_FST_FLUSH) {
2104 if (ip->trunc_off <= rec_offset)
2106 else if (ip->trunc_off < rec_offset + n)
2107 n = (int)(ip->trunc_off - rec_offset);
2110 if (ip->sync_flags & HAMMER_INODE_TRUNCATED) {
2111 if (hammer_cursor_ondisk(&cursor)) {
2112 if (ip->sync_trunc_off <= rec_offset)
2114 else if (ip->sync_trunc_off < rec_offset + n)
2115 n = (int)(ip->sync_trunc_off - rec_offset);
2120 * Try to issue a direct read into our bio if possible,
2121 * otherwise resolve the element data into a hammer_buffer
2124 * The buffer on-disk should be zerod past any real
2125 * truncation point, but may not be for any synthesized
2126 * truncation point from above.
2128 if (boff == 0 && n == bp->b_bufsize &&
2129 ((cursor.leaf->data_offset + roff) & HAMMER_BUFMASK) == 0) {
2130 disk_offset = hammer_blockmap_lookup(
2132 cursor.leaf->data_offset + roff,
2136 nbio->bio_offset = disk_offset;
2137 error = hammer_io_direct_read(trans.hmp, nbio);
2140 error = hammer_ip_resolve_data(&cursor);
2142 bcopy((char *)cursor.data + roff,
2143 (char *)bp->b_data + boff, n);
2150 * Iterate until we have filled the request.
2153 if (boff == bp->b_bufsize)
2155 error = hammer_ip_next(&cursor);
2159 * There may have been a gap after the last record
2161 if (error == ENOENT)
2163 if (error == 0 && boff != bp->b_bufsize) {
2164 KKASSERT(boff < bp->b_bufsize);
2165 bzero((char *)bp->b_data + boff, bp->b_bufsize - boff);
2166 /* boff = bp->b_bufsize; */
2169 bp->b_error = error;
2171 bp->b_flags |= B_ERROR;
2176 hammer_cache_node(&ip->cache[1], cursor.node);
2177 hammer_done_cursor(&cursor);
2178 hammer_done_transaction(&trans);
2183 * BMAP operation - used to support cluster_read() only.
2185 * (struct vnode *vp, off_t loffset, off_t *doffsetp, int *runp, int *runb)
2187 * This routine may return EOPNOTSUPP if the opration is not supported for
2188 * the specified offset. The contents of the pointer arguments do not
2189 * need to be initialized in that case.
2191 * If a disk address is available and properly aligned return 0 with
2192 * *doffsetp set to the zone-2 address, and *runp / *runb set appropriately
2193 * to the run-length relative to that offset. Callers may assume that
2194 * *doffsetp is valid if 0 is returned, even if *runp is not sufficiently
2195 * large, so return EOPNOTSUPP if it is not sufficiently large.
2199 hammer_vop_bmap(struct vop_bmap_args *ap)
2201 struct hammer_transaction trans;
2202 struct hammer_inode *ip;
2203 struct hammer_cursor cursor;
2204 hammer_base_elm_t base;
2208 int64_t base_offset;
2209 int64_t base_disk_offset;
2210 int64_t last_offset;
2211 hammer_off_t last_disk_offset;
2212 hammer_off_t disk_offset;
2217 ip = ap->a_vp->v_data;
2220 * We can only BMAP regular files. We can't BMAP database files,
2223 if (ip->ino_data.obj_type != HAMMER_OBJTYPE_REGFILE)
2227 * bmap is typically called with runp/runb both NULL when used
2228 * for writing. We do not support BMAP for writing atm.
2230 if (ap->a_cmd != BUF_CMD_READ)
2234 * Scan the B-Tree to acquire blockmap addresses, then translate
2237 hammer_simple_transaction(&trans, ip->hmp);
2239 kprintf("bmap_beg %016llx ip->cache %p\n", ap->a_loffset, ip->cache[1]);
2241 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
2244 * Key range (begin and end inclusive) to scan. Note that the key's
2245 * stored in the actual records represent BASE+LEN, not BASE. The
2246 * first record containing bio_offset will have a key > bio_offset.
2248 cursor.key_beg.localization = HAMMER_LOCALIZE_MISC;
2249 cursor.key_beg.obj_id = ip->obj_id;
2250 cursor.key_beg.create_tid = 0;
2251 cursor.key_beg.delete_tid = 0;
2252 cursor.key_beg.obj_type = 0;
2254 cursor.key_beg.key = ap->a_loffset - MAXPHYS + 1;
2256 cursor.key_beg.key = ap->a_loffset + 1;
2257 if (cursor.key_beg.key < 0)
2258 cursor.key_beg.key = 0;
2259 cursor.asof = ip->obj_asof;
2260 cursor.flags |= HAMMER_CURSOR_ASOF;
2262 cursor.key_end = cursor.key_beg;
2263 KKASSERT(ip->ino_data.obj_type == HAMMER_OBJTYPE_REGFILE);
2265 ran_end = ap->a_loffset + MAXPHYS;
2266 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
2267 cursor.key_end.rec_type = HAMMER_RECTYPE_DATA;
2268 tmp64 = ran_end + MAXPHYS + 1; /* work-around GCC-4 bug */
2269 if (tmp64 < ran_end)
2270 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
2272 cursor.key_end.key = ran_end + MAXPHYS + 1;
2274 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
2276 error = hammer_ip_first(&cursor);
2277 base_offset = last_offset = 0;
2278 base_disk_offset = last_disk_offset = 0;
2280 while (error == 0) {
2282 * Get the base file offset of the record. The key for
2283 * data records is (base + bytes) rather then (base).
2285 * NOTE: rec_offset + rec_len may exceed the end-of-file.
2286 * The extra bytes should be zero on-disk and the BMAP op
2287 * should still be ok.
2289 base = &cursor.leaf->base;
2290 rec_offset = base->key - cursor.leaf->data_len;
2291 rec_len = cursor.leaf->data_len;
2294 * Incorporate any cached truncation.
2296 * NOTE: Modifications to rec_len based on synthesized
2297 * truncation points remove the guarantee that any extended
2298 * data on disk is zero (since the truncations may not have
2299 * taken place on-media yet).
2301 if (ip->flags & HAMMER_INODE_TRUNCATED) {
2302 if (hammer_cursor_ondisk(&cursor) ||
2303 cursor.iprec->flush_state == HAMMER_FST_FLUSH) {
2304 if (ip->trunc_off <= rec_offset)
2306 else if (ip->trunc_off < rec_offset + rec_len)
2307 rec_len = (int)(ip->trunc_off - rec_offset);
2310 if (ip->sync_flags & HAMMER_INODE_TRUNCATED) {
2311 if (hammer_cursor_ondisk(&cursor)) {
2312 if (ip->sync_trunc_off <= rec_offset)
2314 else if (ip->sync_trunc_off < rec_offset + rec_len)
2315 rec_len = (int)(ip->sync_trunc_off - rec_offset);
2320 * Accumulate information. If we have hit a discontiguous
2321 * block reset base_offset unless we are already beyond the
2322 * requested offset. If we are, that's it, we stop.
2324 disk_offset = hammer_blockmap_lookup(trans.hmp,
2325 cursor.leaf->data_offset,
2329 if (rec_offset != last_offset ||
2330 disk_offset != last_disk_offset) {
2331 if (rec_offset > ap->a_loffset)
2333 base_offset = rec_offset;
2334 base_disk_offset = disk_offset;
2336 last_offset = rec_offset + rec_len;
2337 last_disk_offset = disk_offset + rec_len;
2339 error = hammer_ip_next(&cursor);
2343 kprintf("BMAP %016llx: %016llx - %016llx\n",
2344 ap->a_loffset, base_offset, last_offset);
2345 kprintf("BMAP %16s: %016llx - %016llx\n",
2346 "", base_disk_offset, last_disk_offset);
2350 hammer_cache_node(&ip->cache[1], cursor.node);
2352 kprintf("bmap_end2 %016llx ip->cache %p\n", ap->a_loffset, ip->cache[1]);
2355 hammer_done_cursor(&cursor);
2356 hammer_done_transaction(&trans);
2359 * If we couldn't find any records or the records we did find were
2360 * all behind the requested offset, return failure. A forward
2361 * truncation can leave a hole w/ no on-disk records.
2363 if (last_offset == 0 || last_offset < ap->a_loffset)
2364 return (EOPNOTSUPP);
2367 * Figure out the block size at the requested offset and adjust
2368 * our limits so the cluster_read() does not create inappropriately
2369 * sized buffer cache buffers.
2371 blksize = hammer_blocksize(ap->a_loffset);
2372 if (hammer_blocksize(base_offset) != blksize) {
2373 base_offset = hammer_blockdemarc(base_offset, ap->a_loffset);
2375 if (last_offset != ap->a_loffset &&
2376 hammer_blocksize(last_offset - 1) != blksize) {
2377 last_offset = hammer_blockdemarc(ap->a_loffset,
2382 * Returning EOPNOTSUPP simply prevents the direct-IO optimization
2385 disk_offset = base_disk_offset + (ap->a_loffset - base_offset);
2388 * If doffsetp is not aligned or the forward run size does
2389 * not cover a whole buffer, disallow the direct I/O.
2391 if ((disk_offset & HAMMER_BUFMASK) ||
2392 (last_offset - ap->a_loffset) < blksize) {
2395 *ap->a_doffsetp = disk_offset;
2397 *ap->a_runb = ap->a_loffset - base_offset;
2398 KKASSERT(*ap->a_runb >= 0);
2401 *ap->a_runp = last_offset - ap->a_loffset;
2402 KKASSERT(*ap->a_runp >= 0);
2410 * Write to a regular file. Because this is a strategy call the OS is
2411 * trying to actually get data onto the media.
2415 hammer_vop_strategy_write(struct vop_strategy_args *ap)
2417 hammer_record_t record;
2427 ip = ap->a_vp->v_data;
2430 KKASSERT(bp->b_bufsize == hammer_blocksize(bio->bio_offset));
2432 if (ip->flags & HAMMER_INODE_RO) {
2433 bp->b_error = EROFS;
2434 bp->b_flags |= B_ERROR;
2436 hammer_cleanup_write_io(ip);
2441 * Interlock with inode destruction (no in-kernel or directory
2442 * topology visibility). If we queue new IO while trying to
2443 * destroy the inode we can deadlock the vtrunc call in
2444 * hammer_inode_unloadable_check().
2446 if (ip->flags & (HAMMER_INODE_DELETING|HAMMER_INODE_DELETED)) {
2449 hammer_cleanup_write_io(ip);
2454 * Reserve space and issue a direct-write from the front-end.
2455 * NOTE: The direct_io code will hammer_bread/bcopy smaller
2458 * An in-memory record will be installed to reference the storage
2459 * until the flusher can get to it.
2461 * Since we own the high level bio the front-end will not try to
2462 * do a direct-read until the write completes.
2464 * NOTE: The only time we do not reserve a full-sized buffers
2465 * worth of data is if the file is small. We do not try to
2466 * allocate a fragment (from the small-data zone) at the end of
2467 * an otherwise large file as this can lead to wildly separated
2470 KKASSERT((bio->bio_offset & HAMMER_BUFMASK) == 0);
2471 KKASSERT(bio->bio_offset < ip->ino_data.size);
2472 if (bio->bio_offset || ip->ino_data.size > HAMMER_BUFSIZE / 2)
2473 bytes = bp->b_bufsize;
2475 bytes = ((int)ip->ino_data.size + 15) & ~15;
2477 record = hammer_ip_add_bulk(ip, bio->bio_offset, bp->b_data,
2480 hammer_io_direct_write(hmp, &record->leaf, bio);
2481 hammer_rel_mem_record(record);
2482 if (ip->rsv_recs > 1 && hmp->rsv_recs > hammer_limit_recs)
2483 hammer_flush_inode(ip, 0);
2485 bp->b_bio2.bio_offset = NOOFFSET;
2486 bp->b_error = error;
2487 bp->b_flags |= B_ERROR;
2490 hammer_cleanup_write_io(ip);
2495 * Clean-up after disposing of a dirty frontend buffer's data.
2496 * This is somewhat heuristical so try to be robust.
2499 hammer_cleanup_write_io(hammer_inode_t ip)
2501 if (ip->rsv_databufs) {
2503 --ip->hmp->rsv_databufs;
2508 * We can lose track of dirty buffer cache buffers if we truncate, this
2509 * routine will resynchronize the count.
2513 hammer_update_rsv_databufs(hammer_inode_t ip)
2521 RB_FOREACH(bp, buf_rb_tree, &ip->vp->v_rbdirty_tree) {
2527 delta = n - ip->rsv_databufs;
2528 ip->rsv_databufs += delta;
2529 ip->hmp->rsv_databufs += delta;
2533 * dounlink - disconnect a directory entry
2535 * XXX whiteout support not really in yet
2538 hammer_dounlink(hammer_transaction_t trans, struct nchandle *nch,
2539 struct vnode *dvp, struct ucred *cred, int flags)
2541 struct namecache *ncp;
2544 struct hammer_cursor cursor;
2549 * Calculate the namekey and setup the key range for the scan. This
2550 * works kinda like a chained hash table where the lower 32 bits
2551 * of the namekey synthesize the chain.
2553 * The key range is inclusive of both key_beg and key_end.
2558 if (dip->flags & HAMMER_INODE_RO)
2561 namekey = hammer_directory_namekey(ncp->nc_name, ncp->nc_nlen);
2563 hammer_init_cursor(trans, &cursor, &dip->cache[1], dip);
2564 cursor.key_beg.localization = HAMMER_LOCALIZE_MISC;
2565 cursor.key_beg.obj_id = dip->obj_id;
2566 cursor.key_beg.key = namekey;
2567 cursor.key_beg.create_tid = 0;
2568 cursor.key_beg.delete_tid = 0;
2569 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
2570 cursor.key_beg.obj_type = 0;
2572 cursor.key_end = cursor.key_beg;
2573 cursor.key_end.key |= 0xFFFFFFFFULL;
2574 cursor.asof = dip->obj_asof;
2575 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
2578 * Scan all matching records (the chain), locate the one matching
2579 * the requested path component. info->last_error contains the
2580 * error code on search termination and could be 0, ENOENT, or
2583 * The hammer_ip_*() functions merge in-memory records with on-disk
2584 * records for the purposes of the search.
2586 error = hammer_ip_first(&cursor);
2588 while (error == 0) {
2589 error = hammer_ip_resolve_data(&cursor);
2592 nlen = cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF;
2594 if (ncp->nc_nlen == nlen &&
2595 bcmp(ncp->nc_name, cursor.data->entry.name, nlen) == 0) {
2598 error = hammer_ip_next(&cursor);
2602 * If all is ok we have to get the inode so we can adjust nlinks.
2603 * To avoid a deadlock with the flusher we must release the inode
2604 * lock on the directory when acquiring the inode for the entry.
2606 * If the target is a directory, it must be empty.
2609 hammer_unlock(&cursor.ip->lock);
2610 ip = hammer_get_inode(trans, dip, cursor.data->entry.obj_id,
2611 dip->hmp->asof, 0, &error);
2612 hammer_lock_sh(&cursor.ip->lock);
2613 if (error == ENOENT) {
2614 kprintf("obj_id %016llx\n", cursor.data->entry.obj_id);
2615 Debugger("ENOENT unlinking object that should exist");
2619 * If we are trying to remove a directory the directory must
2622 * WARNING: hammer_ip_check_directory_empty() may have to
2623 * terminate the cursor to avoid a deadlock. It is ok to
2624 * call hammer_done_cursor() twice.
2626 if (error == 0 && ip->ino_data.obj_type ==
2627 HAMMER_OBJTYPE_DIRECTORY) {
2628 error = hammer_ip_check_directory_empty(trans, ip);
2632 * Delete the directory entry.
2634 * WARNING: hammer_ip_del_directory() may have to terminate
2635 * the cursor to avoid a deadlock. It is ok to call
2636 * hammer_done_cursor() twice.
2639 error = hammer_ip_del_directory(trans, &cursor,
2642 hammer_done_cursor(&cursor);
2644 cache_setunresolved(nch);
2645 cache_setvp(nch, NULL);
2648 cache_inval_vp(ip->vp, CINV_DESTROY);
2651 hammer_rel_inode(ip, 0);
2653 hammer_done_cursor(&cursor);
2655 if (error == EDEADLK)
2661 /************************************************************************
2662 * FIFO AND SPECFS OPS *
2663 ************************************************************************
2668 hammer_vop_fifoclose (struct vop_close_args *ap)
2670 /* XXX update itimes */
2671 return (VOCALL(&fifo_vnode_vops, &ap->a_head));
2675 hammer_vop_fiforead (struct vop_read_args *ap)
2679 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
2680 /* XXX update access time */
2685 hammer_vop_fifowrite (struct vop_write_args *ap)
2689 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
2690 /* XXX update access time */
2695 hammer_vop_specclose (struct vop_close_args *ap)
2697 /* XXX update itimes */
2698 return (VOCALL(&spec_vnode_vops, &ap->a_head));
2702 hammer_vop_specread (struct vop_read_args *ap)
2704 /* XXX update access time */
2705 return (VOCALL(&spec_vnode_vops, &ap->a_head));
2709 hammer_vop_specwrite (struct vop_write_args *ap)
2711 /* XXX update last change time */
2712 return (VOCALL(&spec_vnode_vops, &ap->a_head));