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.75 2008/06/21 20:21:58 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 }
177 * fsync() an inode to disk and wait for it to be completely committed
178 * such that the information would not be undone if a crash occured after
183 hammer_vop_fsync(struct vop_fsync_args *ap)
185 hammer_inode_t ip = VTOI(ap->a_vp);
187 vfsync(ap->a_vp, ap->a_waitfor, 1, NULL, NULL);
188 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
189 if (ap->a_waitfor == MNT_WAIT)
190 hammer_wait_inode(ip);
195 * hammer_vop_read { vp, uio, ioflag, cred }
199 hammer_vop_read(struct vop_read_args *ap)
201 struct hammer_transaction trans;
212 if (ap->a_vp->v_type != VREG)
219 * Allow the UIO's size to override the sequential heuristic.
221 blksize = hammer_blocksize(uio->uio_offset);
222 seqcount = (uio->uio_resid + (blksize - 1)) / blksize;
223 ioseqcount = ap->a_ioflag >> 16;
224 if (seqcount < ioseqcount)
225 seqcount = ioseqcount;
227 hammer_start_transaction(&trans, ip->hmp);
230 * Access the data typically in HAMMER_BUFSIZE blocks via the
231 * buffer cache, but HAMMER may use a variable block size based
234 while (uio->uio_resid > 0 && uio->uio_offset < ip->ino_data.size) {
238 blksize = hammer_blocksize(uio->uio_offset);
239 offset = (int)uio->uio_offset & (blksize - 1);
240 base_offset = uio->uio_offset - offset;
242 if (hammer_debug_cluster_enable) {
244 * Use file_limit to prevent cluster_read() from
245 * creating buffers of the wrong block size past
248 file_limit = ip->ino_data.size;
249 if (base_offset < HAMMER_XDEMARC &&
250 file_limit > HAMMER_XDEMARC) {
251 file_limit = HAMMER_XDEMARC;
253 error = cluster_read(ap->a_vp,
254 file_limit, base_offset,
258 error = bread(ap->a_vp, base_offset, blksize, &bp);
261 kprintf("error %d\n", error);
266 /* bp->b_flags |= B_CLUSTEROK; temporarily disabled */
267 n = blksize - offset;
268 if (n > uio->uio_resid)
270 if (n > ip->ino_data.size - uio->uio_offset)
271 n = (int)(ip->ino_data.size - uio->uio_offset);
272 error = uiomove((char *)bp->b_data + offset, n, uio);
274 /* data has a lower priority then meta-data */
275 bp->b_flags |= B_AGE;
280 if ((ip->flags & HAMMER_INODE_RO) == 0 &&
281 (ip->hmp->mp->mnt_flag & MNT_NOATIME) == 0) {
282 ip->ino_data.atime = trans.time;
283 hammer_modify_inode(ip, HAMMER_INODE_ATIME);
285 hammer_done_transaction(&trans);
290 * hammer_vop_write { vp, uio, ioflag, cred }
294 hammer_vop_write(struct vop_write_args *ap)
296 struct hammer_transaction trans;
297 struct hammer_inode *ip;
310 if (ap->a_vp->v_type != VREG)
315 seqcount = ap->a_ioflag >> 16;
317 if (ip->flags & HAMMER_INODE_RO)
321 * Create a transaction to cover the operations we perform.
323 hammer_start_transaction(&trans, hmp);
329 if (ap->a_ioflag & IO_APPEND)
330 uio->uio_offset = ip->ino_data.size;
333 * Check for illegal write offsets. Valid range is 0...2^63-1.
335 * NOTE: the base_off assignment is required to work around what
336 * I consider to be a GCC-4 optimization bug.
338 if (uio->uio_offset < 0) {
339 hammer_done_transaction(&trans);
342 base_offset = uio->uio_offset + uio->uio_resid; /* work around gcc-4 */
343 if (uio->uio_resid > 0 && base_offset <= 0) {
344 hammer_done_transaction(&trans);
349 * Access the data typically in HAMMER_BUFSIZE blocks via the
350 * buffer cache, but HAMMER may use a variable block size based
354 while (uio->uio_resid > 0) {
359 if ((error = hammer_checkspace(hmp)) != 0)
363 * Do not allow HAMMER to blow out the buffer cache. Very
364 * large UIOs can lockout other processes due to bwillwrite()
367 * Do not allow HAMMER to blow out system memory by
368 * accumulating too many records. Records are so well
369 * decoupled from the buffer cache that it is possible
370 * for userland to push data out to the media via
371 * direct-write, but build up the records queued to the
372 * backend faster then the backend can flush them out.
373 * HAMMER has hit its write limit but the frontend has
374 * no pushback to slow it down.
376 * Always check at the beginning so separate writes are
377 * not able to bypass this code (count++).
379 * WARNING: Cannot unlock vp when doing a NOCOPY write as
380 * part of a putpages operation. Doing so could cause us
381 * to deadlock against the VM system when we try to re-lock.
383 if ((count++ & 15) == 0 || count > 64) {
387 if (uio->uio_segflg != UIO_NOCOPY) {
389 if ((ap->a_ioflag & IO_NOBWILL) == 0)
394 * Pending record flush check.
396 if (hmp->rsv_recs > hammer_limit_recs / 2) {
398 * Get the inode on the flush list
400 if (ip->rsv_recs >= 64)
401 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
402 else if (ip->rsv_recs >= 16)
403 hammer_flush_inode(ip, 0);
406 * Keep the flusher going if the system keeps
409 delta = hmp->count_newrecords -
410 hmp->last_newrecords;
411 if (delta < 0 || delta > hammer_limit_recs / 2) {
412 hmp->last_newrecords = hmp->count_newrecords;
413 hammer_sync_hmp(hmp, MNT_NOWAIT);
417 * If we have gotten behind start slowing
420 delta = (hmp->rsv_recs - hammer_limit_recs) *
421 hz / hammer_limit_recs;
423 tsleep(&trans, 0, "hmrslo", delta);
426 if (uio->uio_segflg != UIO_NOCOPY)
427 vn_lock(ap->a_vp, LK_EXCLUSIVE|LK_RETRY);
431 * Calculate the blocksize at the current offset and figure
432 * out how much we can actually write.
434 blksize = hammer_blocksize(uio->uio_offset);
435 blkmask = blksize - 1;
436 offset = (int)uio->uio_offset & blkmask;
437 base_offset = uio->uio_offset & ~(int64_t)blkmask;
438 n = blksize - offset;
439 if (n > uio->uio_resid)
441 if (uio->uio_offset + n > ip->ino_data.size) {
442 vnode_pager_setsize(ap->a_vp, uio->uio_offset + n);
446 if (uio->uio_segflg == UIO_NOCOPY) {
448 * Issuing a write with the same data backing the
449 * buffer. Instantiate the buffer to collect the
450 * backing vm pages, then read-in any missing bits.
452 * This case is used by vop_stdputpages().
454 bp = getblk(ap->a_vp, base_offset,
455 blksize, GETBLK_BHEAVY, 0);
456 if ((bp->b_flags & B_CACHE) == 0) {
458 error = bread(ap->a_vp, base_offset,
461 } else if (offset == 0 && uio->uio_resid >= blksize) {
463 * Even though we are entirely overwriting the buffer
464 * we may still have to zero it out to avoid a
465 * mmap/write visibility issue.
467 bp = getblk(ap->a_vp, base_offset, blksize, GETBLK_BHEAVY, 0);
468 if ((bp->b_flags & B_CACHE) == 0)
470 } else if (base_offset >= ip->ino_data.size) {
472 * If the base offset of the buffer is beyond the
473 * file EOF, we don't have to issue a read.
475 bp = getblk(ap->a_vp, base_offset,
476 blksize, GETBLK_BHEAVY, 0);
480 * Partial overwrite, read in any missing bits then
481 * replace the portion being written.
483 error = bread(ap->a_vp, base_offset, blksize, &bp);
488 error = uiomove((char *)bp->b_data + offset,
493 * If we screwed up we have to undo any VM size changes we
499 vtruncbuf(ap->a_vp, ip->ino_data.size,
500 hammer_blocksize(ip->ino_data.size));
504 /* bp->b_flags |= B_CLUSTEROK; temporarily disabled */
505 if (ip->ino_data.size < uio->uio_offset) {
506 ip->ino_data.size = uio->uio_offset;
507 flags = HAMMER_INODE_DDIRTY;
508 vnode_pager_setsize(ap->a_vp, ip->ino_data.size);
512 ip->ino_data.mtime = trans.time;
513 flags |= HAMMER_INODE_MTIME | HAMMER_INODE_BUFS;
514 hammer_modify_inode(ip, flags);
517 * Try to keep track of cached dirty data.
519 if ((bp->b_flags & B_DIRTY) == 0) {
525 * Final buffer disposition.
527 * If write_mode is non-zero we call bawrite()
528 * unconditionally. Otherwise we only use bawrite()
529 * if the writes are clearly sequential.
531 bp->b_flags |= B_AGE;
532 if (ap->a_ioflag & IO_SYNC) {
534 } else if (ap->a_ioflag & IO_DIRECT) {
540 else if (hammer_write_mode &&
541 ((int)uio->uio_offset & blkmask) == 0) {
543 bp->b_flags |= B_CLUSTEROK;
544 cluster_write(bp, ip->ino_data.size, XXX seqcount);
548 } else if ((ap->a_ioflag >> 16) == IO_SEQMAX &&
549 ((int)uio->uio_offset & blkmask) == 0) {
551 * If seqcount indicates sequential operation and
552 * we just finished filling a buffer, push it out
553 * now to prevent the buffer cache from becoming
554 * too full, which would trigger non-optimal
563 hammer_done_transaction(&trans);
568 * hammer_vop_access { vp, mode, cred }
572 hammer_vop_access(struct vop_access_args *ap)
574 struct hammer_inode *ip = VTOI(ap->a_vp);
579 uid = hammer_to_unix_xid(&ip->ino_data.uid);
580 gid = hammer_to_unix_xid(&ip->ino_data.gid);
582 error = vop_helper_access(ap, uid, gid, ip->ino_data.mode,
583 ip->ino_data.uflags);
588 * hammer_vop_advlock { vp, id, op, fl, flags }
592 hammer_vop_advlock(struct vop_advlock_args *ap)
594 hammer_inode_t ip = VTOI(ap->a_vp);
596 return (lf_advlock(ap, &ip->advlock, ip->ino_data.size));
600 * hammer_vop_close { vp, fflag }
604 hammer_vop_close(struct vop_close_args *ap)
606 hammer_inode_t ip = VTOI(ap->a_vp);
608 if ((ip->flags | ip->sync_flags) & HAMMER_INODE_MODMASK)
609 hammer_inode_waitreclaims(ip->hmp);
610 return (vop_stdclose(ap));
614 * hammer_vop_ncreate { nch, dvp, vpp, cred, vap }
616 * The operating system has already ensured that the directory entry
617 * does not exist and done all appropriate namespace locking.
621 hammer_vop_ncreate(struct vop_ncreate_args *ap)
623 struct hammer_transaction trans;
624 struct hammer_inode *dip;
625 struct hammer_inode *nip;
626 struct nchandle *nch;
630 dip = VTOI(ap->a_dvp);
632 if (dip->flags & HAMMER_INODE_RO)
634 if ((error = hammer_checkspace(dip->hmp)) != 0)
638 * Create a transaction to cover the operations we perform.
640 hammer_start_transaction(&trans, dip->hmp);
643 * Create a new filesystem object of the requested type. The
644 * returned inode will be referenced and shared-locked to prevent
645 * it from being moved to the flusher.
648 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred, dip, &nip);
650 hkprintf("hammer_create_inode error %d\n", error);
651 hammer_done_transaction(&trans);
657 * Add the new filesystem object to the directory. This will also
658 * bump the inode's link count.
660 error = hammer_ip_add_directory(&trans, dip, nch->ncp, nip);
662 hkprintf("hammer_ip_add_directory error %d\n", error);
668 hammer_rel_inode(nip, 0);
669 hammer_done_transaction(&trans);
672 error = hammer_get_vnode(nip, ap->a_vpp);
673 hammer_done_transaction(&trans);
674 hammer_rel_inode(nip, 0);
676 cache_setunresolved(ap->a_nch);
677 cache_setvp(ap->a_nch, *ap->a_vpp);
684 * hammer_vop_getattr { vp, vap }
686 * Retrieve an inode's attribute information. When accessing inodes
687 * historically we fake the atime field to ensure consistent results.
688 * The atime field is stored in the B-Tree element and allowed to be
689 * updated without cycling the element.
693 hammer_vop_getattr(struct vop_getattr_args *ap)
695 struct hammer_inode *ip = VTOI(ap->a_vp);
696 struct vattr *vap = ap->a_vap;
698 vap->va_fsid = ip->hmp->fsid_udev;
699 vap->va_fileid = ip->ino_leaf.base.obj_id;
700 vap->va_mode = ip->ino_data.mode;
701 vap->va_nlink = ip->ino_data.nlinks;
702 vap->va_uid = hammer_to_unix_xid(&ip->ino_data.uid);
703 vap->va_gid = hammer_to_unix_xid(&ip->ino_data.gid);
706 vap->va_size = ip->ino_data.size;
709 * We must provide a consistent atime and mtime for snapshots
710 * so people can do a 'tar cf - ... | md5' on them and get
711 * consistent results.
713 if (ip->flags & HAMMER_INODE_RO) {
714 hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_atime);
715 hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_mtime);
717 hammer_time_to_timespec(ip->ino_data.atime, &vap->va_atime);
718 hammer_time_to_timespec(ip->ino_data.mtime, &vap->va_mtime);
720 hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_ctime);
721 vap->va_flags = ip->ino_data.uflags;
722 vap->va_gen = 1; /* hammer inums are unique for all time */
723 vap->va_blocksize = HAMMER_BUFSIZE;
724 if (ip->ino_data.size >= HAMMER_XDEMARC) {
725 vap->va_bytes = (ip->ino_data.size + HAMMER_XBUFMASK64) &
727 } else if (ip->ino_data.size > HAMMER_BUFSIZE / 2) {
728 vap->va_bytes = (ip->ino_data.size + HAMMER_BUFMASK64) &
731 vap->va_bytes = (ip->ino_data.size + 15) & ~15;
733 vap->va_type = hammer_get_vnode_type(ip->ino_data.obj_type);
734 vap->va_filerev = 0; /* XXX */
735 /* mtime uniquely identifies any adjustments made to the file XXX */
736 vap->va_fsmid = ip->ino_data.mtime;
737 vap->va_uid_uuid = ip->ino_data.uid;
738 vap->va_gid_uuid = ip->ino_data.gid;
739 vap->va_fsid_uuid = ip->hmp->fsid;
740 vap->va_vaflags = VA_UID_UUID_VALID | VA_GID_UUID_VALID |
743 switch (ip->ino_data.obj_type) {
744 case HAMMER_OBJTYPE_CDEV:
745 case HAMMER_OBJTYPE_BDEV:
746 vap->va_rmajor = ip->ino_data.rmajor;
747 vap->va_rminor = ip->ino_data.rminor;
757 * hammer_vop_nresolve { nch, dvp, cred }
759 * Locate the requested directory entry.
763 hammer_vop_nresolve(struct vop_nresolve_args *ap)
765 struct hammer_transaction trans;
766 struct namecache *ncp;
770 struct hammer_cursor cursor;
778 u_int32_t localization;
781 * Misc initialization, plus handle as-of name extensions. Look for
782 * the '@@' extension. Note that as-of files and directories cannot
785 dip = VTOI(ap->a_dvp);
786 ncp = ap->a_nch->ncp;
787 asof = dip->obj_asof;
791 hammer_simple_transaction(&trans, dip->hmp);
793 for (i = 0; i < nlen; ++i) {
794 if (ncp->nc_name[i] == '@' && ncp->nc_name[i+1] == '@') {
795 asof = hammer_str_to_tid(ncp->nc_name + i + 2);
796 flags |= HAMMER_INODE_RO;
803 * If there is no path component the time extension is relative to
807 ip = hammer_get_inode(&trans, dip, dip->obj_id,
808 asof, dip->obj_localization,
811 error = hammer_get_vnode(ip, &vp);
812 hammer_rel_inode(ip, 0);
818 cache_setvp(ap->a_nch, vp);
825 * Calculate the namekey and setup the key range for the scan. This
826 * works kinda like a chained hash table where the lower 32 bits
827 * of the namekey synthesize the chain.
829 * The key range is inclusive of both key_beg and key_end.
831 namekey = hammer_directory_namekey(ncp->nc_name, nlen);
833 error = hammer_init_cursor(&trans, &cursor, &dip->cache[1], dip);
834 cursor.key_beg.localization = HAMMER_LOCALIZE_MISC;
835 cursor.key_beg.obj_id = dip->obj_id;
836 cursor.key_beg.key = namekey;
837 cursor.key_beg.create_tid = 0;
838 cursor.key_beg.delete_tid = 0;
839 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
840 cursor.key_beg.obj_type = 0;
842 cursor.key_end = cursor.key_beg;
843 cursor.key_end.key |= 0xFFFFFFFFULL;
845 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
848 * Scan all matching records (the chain), locate the one matching
849 * the requested path component.
851 * The hammer_ip_*() functions merge in-memory records with on-disk
852 * records for the purposes of the search.
855 localization = HAMMER_DEF_LOCALIZATION;
858 error = hammer_ip_first(&cursor);
860 error = hammer_ip_resolve_data(&cursor);
863 if (nlen == cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF &&
864 bcmp(ncp->nc_name, cursor.data->entry.name, nlen) == 0) {
865 obj_id = cursor.data->entry.obj_id;
866 localization = cursor.data->entry.localization;
869 error = hammer_ip_next(&cursor);
872 hammer_done_cursor(&cursor);
874 ip = hammer_get_inode(&trans, dip, obj_id,
878 error = hammer_get_vnode(ip, &vp);
879 hammer_rel_inode(ip, 0);
885 cache_setvp(ap->a_nch, vp);
888 } else if (error == ENOENT) {
889 cache_setvp(ap->a_nch, NULL);
892 hammer_done_transaction(&trans);
897 * hammer_vop_nlookupdotdot { dvp, vpp, cred }
899 * Locate the parent directory of a directory vnode.
901 * dvp is referenced but not locked. *vpp must be returned referenced and
902 * locked. A parent_obj_id of 0 does not necessarily indicate that we are
903 * at the root, instead it could indicate that the directory we were in was
906 * NOTE: as-of sequences are not linked into the directory structure. If
907 * we are at the root with a different asof then the mount point, reload
908 * the same directory with the mount point's asof. I'm not sure what this
909 * will do to NFS. We encode ASOF stamps in NFS file handles so it might not
910 * get confused, but it hasn't been tested.
914 hammer_vop_nlookupdotdot(struct vop_nlookupdotdot_args *ap)
916 struct hammer_transaction trans;
917 struct hammer_inode *dip;
918 struct hammer_inode *ip;
919 int64_t parent_obj_id;
923 dip = VTOI(ap->a_dvp);
924 asof = dip->obj_asof;
925 parent_obj_id = dip->ino_data.parent_obj_id;
927 if (parent_obj_id == 0) {
928 if (dip->obj_id == HAMMER_OBJID_ROOT &&
929 asof != dip->hmp->asof) {
930 parent_obj_id = dip->obj_id;
931 asof = dip->hmp->asof;
932 *ap->a_fakename = kmalloc(19, M_TEMP, M_WAITOK);
933 ksnprintf(*ap->a_fakename, 19, "0x%016llx",
941 hammer_simple_transaction(&trans, dip->hmp);
943 ip = hammer_get_inode(&trans, dip, parent_obj_id,
944 asof, HAMMER_DEF_LOCALIZATION,
947 error = hammer_get_vnode(ip, ap->a_vpp);
948 hammer_rel_inode(ip, 0);
952 hammer_done_transaction(&trans);
957 * hammer_vop_nlink { nch, dvp, vp, cred }
961 hammer_vop_nlink(struct vop_nlink_args *ap)
963 struct hammer_transaction trans;
964 struct hammer_inode *dip;
965 struct hammer_inode *ip;
966 struct nchandle *nch;
970 dip = VTOI(ap->a_dvp);
973 if (dip->flags & HAMMER_INODE_RO)
975 if (ip->flags & HAMMER_INODE_RO)
977 if ((error = hammer_checkspace(dip->hmp)) != 0)
981 * Create a transaction to cover the operations we perform.
983 hammer_start_transaction(&trans, dip->hmp);
986 * Add the filesystem object to the directory. Note that neither
987 * dip nor ip are referenced or locked, but their vnodes are
988 * referenced. This function will bump the inode's link count.
990 error = hammer_ip_add_directory(&trans, dip, nch->ncp, ip);
996 cache_setunresolved(nch);
997 cache_setvp(nch, ap->a_vp);
999 hammer_done_transaction(&trans);
1004 * hammer_vop_nmkdir { nch, dvp, vpp, cred, vap }
1006 * The operating system has already ensured that the directory entry
1007 * does not exist and done all appropriate namespace locking.
1011 hammer_vop_nmkdir(struct vop_nmkdir_args *ap)
1013 struct hammer_transaction trans;
1014 struct hammer_inode *dip;
1015 struct hammer_inode *nip;
1016 struct nchandle *nch;
1020 dip = VTOI(ap->a_dvp);
1022 if (dip->flags & HAMMER_INODE_RO)
1024 if ((error = hammer_checkspace(dip->hmp)) != 0)
1028 * Create a transaction to cover the operations we perform.
1030 hammer_start_transaction(&trans, dip->hmp);
1033 * Create a new filesystem object of the requested type. The
1034 * returned inode will be referenced but not locked.
1036 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred, dip, &nip);
1038 hkprintf("hammer_mkdir error %d\n", error);
1039 hammer_done_transaction(&trans);
1044 * Add the new filesystem object to the directory. This will also
1045 * bump the inode's link count.
1047 error = hammer_ip_add_directory(&trans, dip, nch->ncp, nip);
1049 hkprintf("hammer_mkdir (add) error %d\n", error);
1055 hammer_rel_inode(nip, 0);
1058 error = hammer_get_vnode(nip, ap->a_vpp);
1059 hammer_rel_inode(nip, 0);
1061 cache_setunresolved(ap->a_nch);
1062 cache_setvp(ap->a_nch, *ap->a_vpp);
1065 hammer_done_transaction(&trans);
1070 * hammer_vop_nmknod { nch, dvp, vpp, cred, vap }
1072 * The operating system has already ensured that the directory entry
1073 * does not exist and done all appropriate namespace locking.
1077 hammer_vop_nmknod(struct vop_nmknod_args *ap)
1079 struct hammer_transaction trans;
1080 struct hammer_inode *dip;
1081 struct hammer_inode *nip;
1082 struct nchandle *nch;
1086 dip = VTOI(ap->a_dvp);
1088 if (dip->flags & HAMMER_INODE_RO)
1090 if ((error = hammer_checkspace(dip->hmp)) != 0)
1094 * Create a transaction to cover the operations we perform.
1096 hammer_start_transaction(&trans, dip->hmp);
1099 * Create a new filesystem object of the requested type. The
1100 * returned inode will be referenced but not locked.
1102 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred, dip, &nip);
1104 hammer_done_transaction(&trans);
1110 * Add the new filesystem object to the directory. This will also
1111 * bump the inode's link count.
1113 error = hammer_ip_add_directory(&trans, dip, nch->ncp, nip);
1119 hammer_rel_inode(nip, 0);
1122 error = hammer_get_vnode(nip, ap->a_vpp);
1123 hammer_rel_inode(nip, 0);
1125 cache_setunresolved(ap->a_nch);
1126 cache_setvp(ap->a_nch, *ap->a_vpp);
1129 hammer_done_transaction(&trans);
1134 * hammer_vop_open { vp, mode, cred, fp }
1138 hammer_vop_open(struct vop_open_args *ap)
1142 ip = VTOI(ap->a_vp);
1144 if ((ap->a_mode & FWRITE) && (ip->flags & HAMMER_INODE_RO))
1146 return(vop_stdopen(ap));
1150 * hammer_vop_pathconf { vp, name, retval }
1154 hammer_vop_pathconf(struct vop_pathconf_args *ap)
1160 * hammer_vop_print { vp }
1164 hammer_vop_print(struct vop_print_args *ap)
1170 * hammer_vop_readdir { vp, uio, cred, *eofflag, *ncookies, off_t **cookies }
1174 hammer_vop_readdir(struct vop_readdir_args *ap)
1176 struct hammer_transaction trans;
1177 struct hammer_cursor cursor;
1178 struct hammer_inode *ip;
1180 hammer_base_elm_t base;
1188 ip = VTOI(ap->a_vp);
1190 saveoff = uio->uio_offset;
1192 if (ap->a_ncookies) {
1193 ncookies = uio->uio_resid / 16 + 1;
1194 if (ncookies > 1024)
1196 cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
1204 hammer_simple_transaction(&trans, ip->hmp);
1207 * Handle artificial entries
1211 r = vop_write_dirent(&error, uio, ip->obj_id, DT_DIR, 1, ".");
1215 cookies[cookie_index] = saveoff;
1218 if (cookie_index == ncookies)
1222 if (ip->ino_data.parent_obj_id) {
1223 r = vop_write_dirent(&error, uio,
1224 ip->ino_data.parent_obj_id,
1227 r = vop_write_dirent(&error, uio,
1228 ip->obj_id, DT_DIR, 2, "..");
1233 cookies[cookie_index] = saveoff;
1236 if (cookie_index == ncookies)
1241 * Key range (begin and end inclusive) to scan. Directory keys
1242 * directly translate to a 64 bit 'seek' position.
1244 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
1245 cursor.key_beg.localization = HAMMER_LOCALIZE_MISC;
1246 cursor.key_beg.obj_id = ip->obj_id;
1247 cursor.key_beg.create_tid = 0;
1248 cursor.key_beg.delete_tid = 0;
1249 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1250 cursor.key_beg.obj_type = 0;
1251 cursor.key_beg.key = saveoff;
1253 cursor.key_end = cursor.key_beg;
1254 cursor.key_end.key = HAMMER_MAX_KEY;
1255 cursor.asof = ip->obj_asof;
1256 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
1258 error = hammer_ip_first(&cursor);
1260 while (error == 0) {
1261 error = hammer_ip_resolve_data(&cursor);
1264 base = &cursor.leaf->base;
1265 saveoff = base->key;
1266 KKASSERT(cursor.leaf->data_len > HAMMER_ENTRY_NAME_OFF);
1268 if (base->obj_id != ip->obj_id)
1269 panic("readdir: bad record at %p", cursor.node);
1271 r = vop_write_dirent(
1272 &error, uio, cursor.data->entry.obj_id,
1273 hammer_get_dtype(cursor.leaf->base.obj_type),
1274 cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF ,
1275 (void *)cursor.data->entry.name);
1280 cookies[cookie_index] = base->key;
1282 if (cookie_index == ncookies)
1284 error = hammer_ip_next(&cursor);
1286 hammer_done_cursor(&cursor);
1289 hammer_done_transaction(&trans);
1292 *ap->a_eofflag = (error == ENOENT);
1293 uio->uio_offset = saveoff;
1294 if (error && cookie_index == 0) {
1295 if (error == ENOENT)
1298 kfree(cookies, M_TEMP);
1299 *ap->a_ncookies = 0;
1300 *ap->a_cookies = NULL;
1303 if (error == ENOENT)
1306 *ap->a_ncookies = cookie_index;
1307 *ap->a_cookies = cookies;
1314 * hammer_vop_readlink { vp, uio, cred }
1318 hammer_vop_readlink(struct vop_readlink_args *ap)
1320 struct hammer_transaction trans;
1321 struct hammer_cursor cursor;
1322 struct hammer_inode *ip;
1325 ip = VTOI(ap->a_vp);
1328 * Shortcut if the symlink data was stuffed into ino_data.
1330 if (ip->ino_data.size <= HAMMER_INODE_BASESYMLEN) {
1331 error = uiomove(ip->ino_data.ext.symlink,
1332 ip->ino_data.size, ap->a_uio);
1339 hammer_simple_transaction(&trans, ip->hmp);
1340 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
1343 * Key range (begin and end inclusive) to scan. Directory keys
1344 * directly translate to a 64 bit 'seek' position.
1346 cursor.key_beg.localization = HAMMER_LOCALIZE_MISC; /* XXX */
1347 cursor.key_beg.obj_id = ip->obj_id;
1348 cursor.key_beg.create_tid = 0;
1349 cursor.key_beg.delete_tid = 0;
1350 cursor.key_beg.rec_type = HAMMER_RECTYPE_FIX;
1351 cursor.key_beg.obj_type = 0;
1352 cursor.key_beg.key = HAMMER_FIXKEY_SYMLINK;
1353 cursor.asof = ip->obj_asof;
1354 cursor.flags |= HAMMER_CURSOR_ASOF;
1356 error = hammer_ip_lookup(&cursor);
1358 error = hammer_ip_resolve_data(&cursor);
1360 KKASSERT(cursor.leaf->data_len >=
1361 HAMMER_SYMLINK_NAME_OFF);
1362 error = uiomove(cursor.data->symlink.name,
1363 cursor.leaf->data_len -
1364 HAMMER_SYMLINK_NAME_OFF,
1368 hammer_done_cursor(&cursor);
1369 hammer_done_transaction(&trans);
1374 * hammer_vop_nremove { nch, dvp, cred }
1378 hammer_vop_nremove(struct vop_nremove_args *ap)
1380 struct hammer_transaction trans;
1381 struct hammer_inode *dip;
1384 dip = VTOI(ap->a_dvp);
1386 if (hammer_nohistory(dip) == 0 &&
1387 (error = hammer_checkspace(dip->hmp)) != 0) {
1391 hammer_start_transaction(&trans, dip->hmp);
1392 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp, ap->a_cred, 0);
1393 hammer_done_transaction(&trans);
1399 * hammer_vop_nrename { fnch, tnch, fdvp, tdvp, cred }
1403 hammer_vop_nrename(struct vop_nrename_args *ap)
1405 struct hammer_transaction trans;
1406 struct namecache *fncp;
1407 struct namecache *tncp;
1408 struct hammer_inode *fdip;
1409 struct hammer_inode *tdip;
1410 struct hammer_inode *ip;
1411 struct hammer_cursor cursor;
1415 fdip = VTOI(ap->a_fdvp);
1416 tdip = VTOI(ap->a_tdvp);
1417 fncp = ap->a_fnch->ncp;
1418 tncp = ap->a_tnch->ncp;
1419 ip = VTOI(fncp->nc_vp);
1420 KKASSERT(ip != NULL);
1422 if (fdip->flags & HAMMER_INODE_RO)
1424 if (tdip->flags & HAMMER_INODE_RO)
1426 if (ip->flags & HAMMER_INODE_RO)
1428 if ((error = hammer_checkspace(fdip->hmp)) != 0)
1431 hammer_start_transaction(&trans, fdip->hmp);
1434 * Remove tncp from the target directory and then link ip as
1435 * tncp. XXX pass trans to dounlink
1437 * Force the inode sync-time to match the transaction so it is
1438 * in-sync with the creation of the target directory entry.
1440 error = hammer_dounlink(&trans, ap->a_tnch, ap->a_tdvp, ap->a_cred, 0);
1441 if (error == 0 || error == ENOENT) {
1442 error = hammer_ip_add_directory(&trans, tdip, tncp, ip);
1444 ip->ino_data.parent_obj_id = tdip->obj_id;
1445 hammer_modify_inode(ip, HAMMER_INODE_DDIRTY);
1449 goto failed; /* XXX */
1452 * Locate the record in the originating directory and remove it.
1454 * Calculate the namekey and setup the key range for the scan. This
1455 * works kinda like a chained hash table where the lower 32 bits
1456 * of the namekey synthesize the chain.
1458 * The key range is inclusive of both key_beg and key_end.
1460 namekey = hammer_directory_namekey(fncp->nc_name, fncp->nc_nlen);
1462 hammer_init_cursor(&trans, &cursor, &fdip->cache[1], fdip);
1463 cursor.key_beg.localization = HAMMER_LOCALIZE_MISC;
1464 cursor.key_beg.obj_id = fdip->obj_id;
1465 cursor.key_beg.key = namekey;
1466 cursor.key_beg.create_tid = 0;
1467 cursor.key_beg.delete_tid = 0;
1468 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1469 cursor.key_beg.obj_type = 0;
1471 cursor.key_end = cursor.key_beg;
1472 cursor.key_end.key |= 0xFFFFFFFFULL;
1473 cursor.asof = fdip->obj_asof;
1474 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
1477 * Scan all matching records (the chain), locate the one matching
1478 * the requested path component.
1480 * The hammer_ip_*() functions merge in-memory records with on-disk
1481 * records for the purposes of the search.
1483 error = hammer_ip_first(&cursor);
1484 while (error == 0) {
1485 if (hammer_ip_resolve_data(&cursor) != 0)
1487 nlen = cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF;
1489 if (fncp->nc_nlen == nlen &&
1490 bcmp(fncp->nc_name, cursor.data->entry.name, nlen) == 0) {
1493 error = hammer_ip_next(&cursor);
1497 * If all is ok we have to get the inode so we can adjust nlinks.
1499 * WARNING: hammer_ip_del_directory() may have to terminate the
1500 * cursor to avoid a recursion. It's ok to call hammer_done_cursor()
1504 error = hammer_ip_del_directory(&trans, &cursor, fdip, ip);
1507 * XXX A deadlock here will break rename's atomicy for the purposes
1508 * of crash recovery.
1510 if (error == EDEADLK) {
1511 hammer_done_cursor(&cursor);
1516 * Cleanup and tell the kernel that the rename succeeded.
1518 hammer_done_cursor(&cursor);
1520 cache_rename(ap->a_fnch, ap->a_tnch);
1523 hammer_done_transaction(&trans);
1528 * hammer_vop_nrmdir { nch, dvp, cred }
1532 hammer_vop_nrmdir(struct vop_nrmdir_args *ap)
1534 struct hammer_transaction trans;
1535 struct hammer_inode *dip;
1538 dip = VTOI(ap->a_dvp);
1540 if (hammer_nohistory(dip) == 0 &&
1541 (error = hammer_checkspace(dip->hmp)) != 0) {
1545 hammer_start_transaction(&trans, dip->hmp);
1546 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp, ap->a_cred, 0);
1547 hammer_done_transaction(&trans);
1553 * hammer_vop_setattr { vp, vap, cred }
1557 hammer_vop_setattr(struct vop_setattr_args *ap)
1559 struct hammer_transaction trans;
1561 struct hammer_inode *ip;
1566 int64_t aligned_size;
1570 ip = ap->a_vp->v_data;
1573 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
1575 if (ip->flags & HAMMER_INODE_RO)
1577 if (hammer_nohistory(ip) == 0 &&
1578 (error = hammer_checkspace(ip->hmp)) != 0) {
1582 hammer_start_transaction(&trans, ip->hmp);
1585 if (vap->va_flags != VNOVAL) {
1586 flags = ip->ino_data.uflags;
1587 error = vop_helper_setattr_flags(&flags, vap->va_flags,
1588 hammer_to_unix_xid(&ip->ino_data.uid),
1591 if (ip->ino_data.uflags != flags) {
1592 ip->ino_data.uflags = flags;
1593 modflags |= HAMMER_INODE_DDIRTY;
1595 if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) {
1602 if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) {
1606 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
1607 mode_t cur_mode = ip->ino_data.mode;
1608 uid_t cur_uid = hammer_to_unix_xid(&ip->ino_data.uid);
1609 gid_t cur_gid = hammer_to_unix_xid(&ip->ino_data.gid);
1613 error = vop_helper_chown(ap->a_vp, vap->va_uid, vap->va_gid,
1615 &cur_uid, &cur_gid, &cur_mode);
1617 hammer_guid_to_uuid(&uuid_uid, cur_uid);
1618 hammer_guid_to_uuid(&uuid_gid, cur_gid);
1619 if (bcmp(&uuid_uid, &ip->ino_data.uid,
1620 sizeof(uuid_uid)) ||
1621 bcmp(&uuid_gid, &ip->ino_data.gid,
1622 sizeof(uuid_gid)) ||
1623 ip->ino_data.mode != cur_mode
1625 ip->ino_data.uid = uuid_uid;
1626 ip->ino_data.gid = uuid_gid;
1627 ip->ino_data.mode = cur_mode;
1629 modflags |= HAMMER_INODE_DDIRTY;
1632 while (vap->va_size != VNOVAL && ip->ino_data.size != vap->va_size) {
1633 switch(ap->a_vp->v_type) {
1635 if (vap->va_size == ip->ino_data.size)
1638 * XXX break atomicy, we can deadlock the backend
1639 * if we do not release the lock. Probably not a
1642 blksize = hammer_blocksize(vap->va_size);
1643 if (vap->va_size < ip->ino_data.size) {
1644 vtruncbuf(ap->a_vp, vap->va_size, blksize);
1647 vnode_pager_setsize(ap->a_vp, vap->va_size);
1650 ip->ino_data.size = vap->va_size;
1651 modflags |= HAMMER_INODE_DDIRTY;
1654 * on-media truncation is cached in the inode until
1655 * the inode is synchronized.
1658 hammer_ip_frontend_trunc(ip, vap->va_size);
1659 hammer_update_rsv_databufs(ip);
1660 #ifdef DEBUG_TRUNCATE
1661 if (HammerTruncIp == NULL)
1664 if ((ip->flags & HAMMER_INODE_TRUNCATED) == 0) {
1665 ip->flags |= HAMMER_INODE_TRUNCATED;
1666 ip->trunc_off = vap->va_size;
1667 #ifdef DEBUG_TRUNCATE
1668 if (ip == HammerTruncIp)
1669 kprintf("truncate1 %016llx\n", ip->trunc_off);
1671 } else if (ip->trunc_off > vap->va_size) {
1672 ip->trunc_off = vap->va_size;
1673 #ifdef DEBUG_TRUNCATE
1674 if (ip == HammerTruncIp)
1675 kprintf("truncate2 %016llx\n", ip->trunc_off);
1678 #ifdef DEBUG_TRUNCATE
1679 if (ip == HammerTruncIp)
1680 kprintf("truncate3 %016llx (ignored)\n", vap->va_size);
1686 * If truncating we have to clean out a portion of
1687 * the last block on-disk. We do this in the
1688 * front-end buffer cache.
1690 aligned_size = (vap->va_size + (blksize - 1)) &
1691 ~(int64_t)(blksize - 1);
1692 if (truncating && vap->va_size < aligned_size) {
1696 aligned_size -= blksize;
1698 offset = (int)vap->va_size & (blksize - 1);
1699 error = bread(ap->a_vp, aligned_size,
1701 hammer_ip_frontend_trunc(ip, aligned_size);
1703 bzero(bp->b_data + offset,
1707 kprintf("ERROR %d\n", error);
1713 if ((ip->flags & HAMMER_INODE_TRUNCATED) == 0) {
1714 ip->flags |= HAMMER_INODE_TRUNCATED;
1715 ip->trunc_off = vap->va_size;
1716 } else if (ip->trunc_off > vap->va_size) {
1717 ip->trunc_off = vap->va_size;
1719 hammer_ip_frontend_trunc(ip, vap->va_size);
1720 ip->ino_data.size = vap->va_size;
1721 modflags |= HAMMER_INODE_DDIRTY;
1729 if (vap->va_atime.tv_sec != VNOVAL) {
1730 ip->ino_data.atime =
1731 hammer_timespec_to_time(&vap->va_atime);
1732 modflags |= HAMMER_INODE_ATIME;
1734 if (vap->va_mtime.tv_sec != VNOVAL) {
1735 ip->ino_data.mtime =
1736 hammer_timespec_to_time(&vap->va_mtime);
1737 modflags |= HAMMER_INODE_MTIME;
1739 if (vap->va_mode != (mode_t)VNOVAL) {
1740 mode_t cur_mode = ip->ino_data.mode;
1741 uid_t cur_uid = hammer_to_unix_xid(&ip->ino_data.uid);
1742 gid_t cur_gid = hammer_to_unix_xid(&ip->ino_data.gid);
1744 error = vop_helper_chmod(ap->a_vp, vap->va_mode, ap->a_cred,
1745 cur_uid, cur_gid, &cur_mode);
1746 if (error == 0 && ip->ino_data.mode != cur_mode) {
1747 ip->ino_data.mode = cur_mode;
1748 modflags |= HAMMER_INODE_DDIRTY;
1753 hammer_modify_inode(ip, modflags);
1754 hammer_done_transaction(&trans);
1759 * hammer_vop_nsymlink { nch, dvp, vpp, cred, vap, target }
1763 hammer_vop_nsymlink(struct vop_nsymlink_args *ap)
1765 struct hammer_transaction trans;
1766 struct hammer_inode *dip;
1767 struct hammer_inode *nip;
1768 struct nchandle *nch;
1769 hammer_record_t record;
1773 ap->a_vap->va_type = VLNK;
1776 dip = VTOI(ap->a_dvp);
1778 if (dip->flags & HAMMER_INODE_RO)
1780 if ((error = hammer_checkspace(dip->hmp)) != 0)
1784 * Create a transaction to cover the operations we perform.
1786 hammer_start_transaction(&trans, dip->hmp);
1789 * Create a new filesystem object of the requested type. The
1790 * returned inode will be referenced but not locked.
1793 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred, dip, &nip);
1795 hammer_done_transaction(&trans);
1801 * Add a record representing the symlink. symlink stores the link
1802 * as pure data, not a string, and is no \0 terminated.
1805 bytes = strlen(ap->a_target);
1807 if (bytes <= HAMMER_INODE_BASESYMLEN) {
1808 bcopy(ap->a_target, nip->ino_data.ext.symlink, bytes);
1810 record = hammer_alloc_mem_record(nip, bytes);
1811 record->type = HAMMER_MEM_RECORD_GENERAL;
1813 record->leaf.base.localization = HAMMER_LOCALIZE_MISC;
1814 record->leaf.base.key = HAMMER_FIXKEY_SYMLINK;
1815 record->leaf.base.rec_type = HAMMER_RECTYPE_FIX;
1816 record->leaf.data_len = bytes;
1817 KKASSERT(HAMMER_SYMLINK_NAME_OFF == 0);
1818 bcopy(ap->a_target, record->data->symlink.name, bytes);
1819 error = hammer_ip_add_record(&trans, record);
1823 * Set the file size to the length of the link.
1826 nip->ino_data.size = bytes;
1827 hammer_modify_inode(nip, HAMMER_INODE_DDIRTY);
1831 error = hammer_ip_add_directory(&trans, dip, nch->ncp, nip);
1837 hammer_rel_inode(nip, 0);
1840 error = hammer_get_vnode(nip, ap->a_vpp);
1841 hammer_rel_inode(nip, 0);
1843 cache_setunresolved(ap->a_nch);
1844 cache_setvp(ap->a_nch, *ap->a_vpp);
1847 hammer_done_transaction(&trans);
1852 * hammer_vop_nwhiteout { nch, dvp, cred, flags }
1856 hammer_vop_nwhiteout(struct vop_nwhiteout_args *ap)
1858 struct hammer_transaction trans;
1859 struct hammer_inode *dip;
1862 dip = VTOI(ap->a_dvp);
1864 if (hammer_nohistory(dip) == 0 &&
1865 (error = hammer_checkspace(dip->hmp)) != 0) {
1869 hammer_start_transaction(&trans, dip->hmp);
1870 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp,
1871 ap->a_cred, ap->a_flags);
1872 hammer_done_transaction(&trans);
1878 * hammer_vop_ioctl { vp, command, data, fflag, cred }
1882 hammer_vop_ioctl(struct vop_ioctl_args *ap)
1884 struct hammer_inode *ip = ap->a_vp->v_data;
1886 return(hammer_ioctl(ip, ap->a_command, ap->a_data,
1887 ap->a_fflag, ap->a_cred));
1892 hammer_vop_mountctl(struct vop_mountctl_args *ap)
1897 mp = ap->a_head.a_ops->head.vv_mount;
1900 case MOUNTCTL_SET_EXPORT:
1901 if (ap->a_ctllen != sizeof(struct export_args))
1903 error = hammer_vfs_export(mp, ap->a_op,
1904 (const struct export_args *)ap->a_ctl);
1907 error = journal_mountctl(ap);
1914 * hammer_vop_strategy { vp, bio }
1916 * Strategy call, used for regular file read & write only. Note that the
1917 * bp may represent a cluster.
1919 * To simplify operation and allow better optimizations in the future,
1920 * this code does not make any assumptions with regards to buffer alignment
1925 hammer_vop_strategy(struct vop_strategy_args *ap)
1930 bp = ap->a_bio->bio_buf;
1934 error = hammer_vop_strategy_read(ap);
1937 error = hammer_vop_strategy_write(ap);
1940 bp->b_error = error = EINVAL;
1941 bp->b_flags |= B_ERROR;
1949 * Read from a regular file. Iterate the related records and fill in the
1950 * BIO/BUF. Gaps are zero-filled.
1952 * The support code in hammer_object.c should be used to deal with mixed
1953 * in-memory and on-disk records.
1955 * NOTE: Can be called from the cluster code with an oversized buf.
1961 hammer_vop_strategy_read(struct vop_strategy_args *ap)
1963 struct hammer_transaction trans;
1964 struct hammer_inode *ip;
1965 struct hammer_cursor cursor;
1966 hammer_base_elm_t base;
1967 hammer_off_t disk_offset;
1981 ip = ap->a_vp->v_data;
1984 * The zone-2 disk offset may have been set by the cluster code via
1985 * a BMAP operation, or else should be NOOFFSET.
1987 * Checking the high bits for a match against zone-2 should suffice.
1989 nbio = push_bio(bio);
1990 if ((nbio->bio_offset & HAMMER_OFF_ZONE_MASK) ==
1991 HAMMER_ZONE_RAW_BUFFER) {
1992 error = hammer_io_direct_read(ip->hmp, nbio);
1997 * Well, that sucked. Do it the hard way. If all the stars are
1998 * aligned we may still be able to issue a direct-read.
2000 hammer_simple_transaction(&trans, ip->hmp);
2001 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
2004 * Key range (begin and end inclusive) to scan. Note that the key's
2005 * stored in the actual records represent BASE+LEN, not BASE. The
2006 * first record containing bio_offset will have a key > bio_offset.
2008 cursor.key_beg.localization = HAMMER_LOCALIZE_MISC;
2009 cursor.key_beg.obj_id = ip->obj_id;
2010 cursor.key_beg.create_tid = 0;
2011 cursor.key_beg.delete_tid = 0;
2012 cursor.key_beg.obj_type = 0;
2013 cursor.key_beg.key = bio->bio_offset + 1;
2014 cursor.asof = ip->obj_asof;
2015 cursor.flags |= HAMMER_CURSOR_ASOF;
2017 cursor.key_end = cursor.key_beg;
2018 KKASSERT(ip->ino_data.obj_type == HAMMER_OBJTYPE_REGFILE);
2020 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_DBFILE) {
2021 cursor.key_beg.rec_type = HAMMER_RECTYPE_DB;
2022 cursor.key_end.rec_type = HAMMER_RECTYPE_DB;
2023 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
2027 ran_end = bio->bio_offset + bp->b_bufsize;
2028 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
2029 cursor.key_end.rec_type = HAMMER_RECTYPE_DATA;
2030 tmp64 = ran_end + MAXPHYS + 1; /* work-around GCC-4 bug */
2031 if (tmp64 < ran_end)
2032 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
2034 cursor.key_end.key = ran_end + MAXPHYS + 1;
2036 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
2038 error = hammer_ip_first(&cursor);
2041 while (error == 0) {
2043 * Get the base file offset of the record. The key for
2044 * data records is (base + bytes) rather then (base).
2046 base = &cursor.leaf->base;
2047 rec_offset = base->key - cursor.leaf->data_len;
2050 * Calculate the gap, if any, and zero-fill it.
2052 * n is the offset of the start of the record verses our
2053 * current seek offset in the bio.
2055 n = (int)(rec_offset - (bio->bio_offset + boff));
2057 if (n > bp->b_bufsize - boff)
2058 n = bp->b_bufsize - boff;
2059 bzero((char *)bp->b_data + boff, n);
2065 * Calculate the data offset in the record and the number
2066 * of bytes we can copy.
2068 * There are two degenerate cases. First, boff may already
2069 * be at bp->b_bufsize. Secondly, the data offset within
2070 * the record may exceed the record's size.
2074 n = cursor.leaf->data_len - roff;
2076 kprintf("strategy_read: bad n=%d roff=%d\n", n, roff);
2078 } else if (n > bp->b_bufsize - boff) {
2079 n = bp->b_bufsize - boff;
2083 * Deal with cached truncations. This cool bit of code
2084 * allows truncate()/ftruncate() to avoid having to sync
2087 * If the frontend is truncated then all backend records are
2088 * subject to the frontend's truncation.
2090 * If the backend is truncated then backend records on-disk
2091 * (but not in-memory) are subject to the backend's
2092 * truncation. In-memory records owned by the backend
2093 * represent data written after the truncation point on the
2094 * backend and must not be truncated.
2096 * Truncate operations deal with frontend buffer cache
2097 * buffers and frontend-owned in-memory records synchronously.
2099 if (ip->flags & HAMMER_INODE_TRUNCATED) {
2100 if (hammer_cursor_ondisk(&cursor) ||
2101 cursor.iprec->flush_state == HAMMER_FST_FLUSH) {
2102 if (ip->trunc_off <= rec_offset)
2104 else if (ip->trunc_off < rec_offset + n)
2105 n = (int)(ip->trunc_off - rec_offset);
2108 if (ip->sync_flags & HAMMER_INODE_TRUNCATED) {
2109 if (hammer_cursor_ondisk(&cursor)) {
2110 if (ip->sync_trunc_off <= rec_offset)
2112 else if (ip->sync_trunc_off < rec_offset + n)
2113 n = (int)(ip->sync_trunc_off - rec_offset);
2118 * Try to issue a direct read into our bio if possible,
2119 * otherwise resolve the element data into a hammer_buffer
2122 * The buffer on-disk should be zerod past any real
2123 * truncation point, but may not be for any synthesized
2124 * truncation point from above.
2126 if (boff == 0 && n == bp->b_bufsize &&
2127 ((cursor.leaf->data_offset + roff) & HAMMER_BUFMASK) == 0) {
2128 disk_offset = hammer_blockmap_lookup(
2130 cursor.leaf->data_offset + roff,
2134 nbio->bio_offset = disk_offset;
2135 error = hammer_io_direct_read(trans.hmp, nbio);
2138 error = hammer_ip_resolve_data(&cursor);
2140 bcopy((char *)cursor.data + roff,
2141 (char *)bp->b_data + boff, n);
2148 * Iterate until we have filled the request.
2151 if (boff == bp->b_bufsize)
2153 error = hammer_ip_next(&cursor);
2157 * There may have been a gap after the last record
2159 if (error == ENOENT)
2161 if (error == 0 && boff != bp->b_bufsize) {
2162 KKASSERT(boff < bp->b_bufsize);
2163 bzero((char *)bp->b_data + boff, bp->b_bufsize - boff);
2164 /* boff = bp->b_bufsize; */
2167 bp->b_error = error;
2169 bp->b_flags |= B_ERROR;
2174 hammer_cache_node(&ip->cache[1], cursor.node);
2175 hammer_done_cursor(&cursor);
2176 hammer_done_transaction(&trans);
2181 * BMAP operation - used to support cluster_read() only.
2183 * (struct vnode *vp, off_t loffset, off_t *doffsetp, int *runp, int *runb)
2185 * This routine may return EOPNOTSUPP if the opration is not supported for
2186 * the specified offset. The contents of the pointer arguments do not
2187 * need to be initialized in that case.
2189 * If a disk address is available and properly aligned return 0 with
2190 * *doffsetp set to the zone-2 address, and *runp / *runb set appropriately
2191 * to the run-length relative to that offset. Callers may assume that
2192 * *doffsetp is valid if 0 is returned, even if *runp is not sufficiently
2193 * large, so return EOPNOTSUPP if it is not sufficiently large.
2197 hammer_vop_bmap(struct vop_bmap_args *ap)
2199 struct hammer_transaction trans;
2200 struct hammer_inode *ip;
2201 struct hammer_cursor cursor;
2202 hammer_base_elm_t base;
2206 int64_t base_offset;
2207 int64_t base_disk_offset;
2208 int64_t last_offset;
2209 hammer_off_t last_disk_offset;
2210 hammer_off_t disk_offset;
2215 ip = ap->a_vp->v_data;
2218 * We can only BMAP regular files. We can't BMAP database files,
2221 if (ip->ino_data.obj_type != HAMMER_OBJTYPE_REGFILE)
2225 * bmap is typically called with runp/runb both NULL when used
2226 * for writing. We do not support BMAP for writing atm.
2228 if (ap->a_cmd != BUF_CMD_READ)
2232 * Scan the B-Tree to acquire blockmap addresses, then translate
2235 hammer_simple_transaction(&trans, ip->hmp);
2237 kprintf("bmap_beg %016llx ip->cache %p\n", ap->a_loffset, ip->cache[1]);
2239 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
2242 * Key range (begin and end inclusive) to scan. Note that the key's
2243 * stored in the actual records represent BASE+LEN, not BASE. The
2244 * first record containing bio_offset will have a key > bio_offset.
2246 cursor.key_beg.localization = HAMMER_LOCALIZE_MISC;
2247 cursor.key_beg.obj_id = ip->obj_id;
2248 cursor.key_beg.create_tid = 0;
2249 cursor.key_beg.delete_tid = 0;
2250 cursor.key_beg.obj_type = 0;
2252 cursor.key_beg.key = ap->a_loffset - MAXPHYS + 1;
2254 cursor.key_beg.key = ap->a_loffset + 1;
2255 if (cursor.key_beg.key < 0)
2256 cursor.key_beg.key = 0;
2257 cursor.asof = ip->obj_asof;
2258 cursor.flags |= HAMMER_CURSOR_ASOF;
2260 cursor.key_end = cursor.key_beg;
2261 KKASSERT(ip->ino_data.obj_type == HAMMER_OBJTYPE_REGFILE);
2263 ran_end = ap->a_loffset + MAXPHYS;
2264 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
2265 cursor.key_end.rec_type = HAMMER_RECTYPE_DATA;
2266 tmp64 = ran_end + MAXPHYS + 1; /* work-around GCC-4 bug */
2267 if (tmp64 < ran_end)
2268 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
2270 cursor.key_end.key = ran_end + MAXPHYS + 1;
2272 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
2274 error = hammer_ip_first(&cursor);
2275 base_offset = last_offset = 0;
2276 base_disk_offset = last_disk_offset = 0;
2278 while (error == 0) {
2280 * Get the base file offset of the record. The key for
2281 * data records is (base + bytes) rather then (base).
2283 * NOTE: rec_offset + rec_len may exceed the end-of-file.
2284 * The extra bytes should be zero on-disk and the BMAP op
2285 * should still be ok.
2287 base = &cursor.leaf->base;
2288 rec_offset = base->key - cursor.leaf->data_len;
2289 rec_len = cursor.leaf->data_len;
2292 * Incorporate any cached truncation.
2294 * NOTE: Modifications to rec_len based on synthesized
2295 * truncation points remove the guarantee that any extended
2296 * data on disk is zero (since the truncations may not have
2297 * taken place on-media yet).
2299 if (ip->flags & HAMMER_INODE_TRUNCATED) {
2300 if (hammer_cursor_ondisk(&cursor) ||
2301 cursor.iprec->flush_state == HAMMER_FST_FLUSH) {
2302 if (ip->trunc_off <= rec_offset)
2304 else if (ip->trunc_off < rec_offset + rec_len)
2305 rec_len = (int)(ip->trunc_off - rec_offset);
2308 if (ip->sync_flags & HAMMER_INODE_TRUNCATED) {
2309 if (hammer_cursor_ondisk(&cursor)) {
2310 if (ip->sync_trunc_off <= rec_offset)
2312 else if (ip->sync_trunc_off < rec_offset + rec_len)
2313 rec_len = (int)(ip->sync_trunc_off - rec_offset);
2318 * Accumulate information. If we have hit a discontiguous
2319 * block reset base_offset unless we are already beyond the
2320 * requested offset. If we are, that's it, we stop.
2322 disk_offset = hammer_blockmap_lookup(trans.hmp,
2323 cursor.leaf->data_offset,
2327 if (rec_offset != last_offset ||
2328 disk_offset != last_disk_offset) {
2329 if (rec_offset > ap->a_loffset)
2331 base_offset = rec_offset;
2332 base_disk_offset = disk_offset;
2334 last_offset = rec_offset + rec_len;
2335 last_disk_offset = disk_offset + rec_len;
2337 error = hammer_ip_next(&cursor);
2341 kprintf("BMAP %016llx: %016llx - %016llx\n",
2342 ap->a_loffset, base_offset, last_offset);
2343 kprintf("BMAP %16s: %016llx - %016llx\n",
2344 "", base_disk_offset, last_disk_offset);
2348 hammer_cache_node(&ip->cache[1], cursor.node);
2350 kprintf("bmap_end2 %016llx ip->cache %p\n", ap->a_loffset, ip->cache[1]);
2353 hammer_done_cursor(&cursor);
2354 hammer_done_transaction(&trans);
2357 * If we couldn't find any records or the records we did find were
2358 * all behind the requested offset, return failure. A forward
2359 * truncation can leave a hole w/ no on-disk records.
2361 if (last_offset == 0 || last_offset < ap->a_loffset)
2362 return (EOPNOTSUPP);
2365 * Figure out the block size at the requested offset and adjust
2366 * our limits so the cluster_read() does not create inappropriately
2367 * sized buffer cache buffers.
2369 blksize = hammer_blocksize(ap->a_loffset);
2370 if (hammer_blocksize(base_offset) != blksize) {
2371 base_offset = hammer_blockdemarc(base_offset, ap->a_loffset);
2373 if (last_offset != ap->a_loffset &&
2374 hammer_blocksize(last_offset - 1) != blksize) {
2375 last_offset = hammer_blockdemarc(ap->a_loffset,
2380 * Returning EOPNOTSUPP simply prevents the direct-IO optimization
2383 disk_offset = base_disk_offset + (ap->a_loffset - base_offset);
2386 * If doffsetp is not aligned or the forward run size does
2387 * not cover a whole buffer, disallow the direct I/O.
2389 if ((disk_offset & HAMMER_BUFMASK) ||
2390 (last_offset - ap->a_loffset) < blksize) {
2393 *ap->a_doffsetp = disk_offset;
2395 *ap->a_runb = ap->a_loffset - base_offset;
2396 KKASSERT(*ap->a_runb >= 0);
2399 *ap->a_runp = last_offset - ap->a_loffset;
2400 KKASSERT(*ap->a_runp >= 0);
2408 * Write to a regular file. Because this is a strategy call the OS is
2409 * trying to actually get data onto the media.
2413 hammer_vop_strategy_write(struct vop_strategy_args *ap)
2415 hammer_record_t record;
2425 ip = ap->a_vp->v_data;
2428 KKASSERT(bp->b_bufsize == hammer_blocksize(bio->bio_offset));
2430 if (ip->flags & HAMMER_INODE_RO) {
2431 bp->b_error = EROFS;
2432 bp->b_flags |= B_ERROR;
2434 hammer_cleanup_write_io(ip);
2439 * Interlock with inode destruction (no in-kernel or directory
2440 * topology visibility). If we queue new IO while trying to
2441 * destroy the inode we can deadlock the vtrunc call in
2442 * hammer_inode_unloadable_check().
2444 if (ip->flags & (HAMMER_INODE_DELETING|HAMMER_INODE_DELETED)) {
2447 hammer_cleanup_write_io(ip);
2452 * Reserve space and issue a direct-write from the front-end.
2453 * NOTE: The direct_io code will hammer_bread/bcopy smaller
2456 * An in-memory record will be installed to reference the storage
2457 * until the flusher can get to it.
2459 * Since we own the high level bio the front-end will not try to
2460 * do a direct-read until the write completes.
2462 * NOTE: The only time we do not reserve a full-sized buffers
2463 * worth of data is if the file is small. We do not try to
2464 * allocate a fragment (from the small-data zone) at the end of
2465 * an otherwise large file as this can lead to wildly separated
2468 KKASSERT((bio->bio_offset & HAMMER_BUFMASK) == 0);
2469 KKASSERT(bio->bio_offset < ip->ino_data.size);
2470 if (bio->bio_offset || ip->ino_data.size > HAMMER_BUFSIZE / 2)
2471 bytes = bp->b_bufsize;
2473 bytes = ((int)ip->ino_data.size + 15) & ~15;
2475 record = hammer_ip_add_bulk(ip, bio->bio_offset, bp->b_data,
2478 hammer_io_direct_write(hmp, &record->leaf, bio);
2479 hammer_rel_mem_record(record);
2480 if (ip->rsv_recs > 1 && hmp->rsv_recs > hammer_limit_recs)
2481 hammer_flush_inode(ip, 0);
2483 bp->b_bio2.bio_offset = NOOFFSET;
2484 bp->b_error = error;
2485 bp->b_flags |= B_ERROR;
2488 hammer_cleanup_write_io(ip);
2493 * Clean-up after disposing of a dirty frontend buffer's data.
2494 * This is somewhat heuristical so try to be robust.
2497 hammer_cleanup_write_io(hammer_inode_t ip)
2499 if (ip->rsv_databufs) {
2501 --ip->hmp->rsv_databufs;
2506 * We can lose track of dirty buffer cache buffers if we truncate, this
2507 * routine will resynchronize the count.
2511 hammer_update_rsv_databufs(hammer_inode_t ip)
2519 RB_FOREACH(bp, buf_rb_tree, &ip->vp->v_rbdirty_tree) {
2525 delta = n - ip->rsv_databufs;
2526 ip->rsv_databufs += delta;
2527 ip->hmp->rsv_databufs += delta;
2531 * dounlink - disconnect a directory entry
2533 * XXX whiteout support not really in yet
2536 hammer_dounlink(hammer_transaction_t trans, struct nchandle *nch,
2537 struct vnode *dvp, struct ucred *cred, int flags)
2539 struct namecache *ncp;
2542 struct hammer_cursor cursor;
2547 * Calculate the namekey and setup the key range for the scan. This
2548 * works kinda like a chained hash table where the lower 32 bits
2549 * of the namekey synthesize the chain.
2551 * The key range is inclusive of both key_beg and key_end.
2556 if (dip->flags & HAMMER_INODE_RO)
2559 namekey = hammer_directory_namekey(ncp->nc_name, ncp->nc_nlen);
2561 hammer_init_cursor(trans, &cursor, &dip->cache[1], dip);
2562 cursor.key_beg.localization = HAMMER_LOCALIZE_MISC;
2563 cursor.key_beg.obj_id = dip->obj_id;
2564 cursor.key_beg.key = namekey;
2565 cursor.key_beg.create_tid = 0;
2566 cursor.key_beg.delete_tid = 0;
2567 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
2568 cursor.key_beg.obj_type = 0;
2570 cursor.key_end = cursor.key_beg;
2571 cursor.key_end.key |= 0xFFFFFFFFULL;
2572 cursor.asof = dip->obj_asof;
2573 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
2576 * Scan all matching records (the chain), locate the one matching
2577 * the requested path component. info->last_error contains the
2578 * error code on search termination and could be 0, ENOENT, or
2581 * The hammer_ip_*() functions merge in-memory records with on-disk
2582 * records for the purposes of the search.
2584 error = hammer_ip_first(&cursor);
2586 while (error == 0) {
2587 error = hammer_ip_resolve_data(&cursor);
2590 nlen = cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF;
2592 if (ncp->nc_nlen == nlen &&
2593 bcmp(ncp->nc_name, cursor.data->entry.name, nlen) == 0) {
2596 error = hammer_ip_next(&cursor);
2600 * If all is ok we have to get the inode so we can adjust nlinks.
2601 * To avoid a deadlock with the flusher we must release the inode
2602 * lock on the directory when acquiring the inode for the entry.
2604 * If the target is a directory, it must be empty.
2607 hammer_unlock(&cursor.ip->lock);
2608 ip = hammer_get_inode(trans, dip, cursor.data->entry.obj_id,
2610 cursor.data->entry.localization,
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));