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.78 2008/06/28 18:10:55 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)
362 blksize = hammer_blocksize(uio->uio_offset);
365 * Do not allow HAMMER to blow out the buffer cache. Very
366 * large UIOs can lockout other processes due to bwillwrite()
369 * Do not allow HAMMER to blow out system memory by
370 * accumulating too many records. Records are so well
371 * decoupled from the buffer cache that it is possible
372 * for userland to push data out to the media via
373 * direct-write, but build up the records queued to the
374 * backend faster then the backend can flush them out.
375 * HAMMER has hit its write limit but the frontend has
376 * no pushback to slow it down.
378 * Always check at the beginning so separate writes are
379 * not able to bypass this code (count++).
381 * WARNING: Cannot unlock vp when doing a NOCOPY write as
382 * part of a putpages operation. Doing so could cause us
383 * to deadlock against the VM system when we try to re-lock.
385 if ((count++ & 15) == 0 || count > 64) {
389 if (uio->uio_segflg != UIO_NOCOPY) {
391 if ((ap->a_ioflag & IO_NOBWILL) == 0)
396 * Pending record flush check.
398 if (hmp->rsv_recs > hammer_limit_recs / 2) {
400 * Get the inode on the flush list
402 if (ip->rsv_recs >= 64)
403 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
404 else if (ip->rsv_recs >= 16)
405 hammer_flush_inode(ip, 0);
408 * Keep the flusher going if the system keeps
411 delta = hmp->count_newrecords -
412 hmp->last_newrecords;
413 if (delta < 0 || delta > hammer_limit_recs / 2) {
414 hmp->last_newrecords = hmp->count_newrecords;
415 hammer_sync_hmp(hmp, MNT_NOWAIT);
419 * If we have gotten behind start slowing
422 delta = (hmp->rsv_recs - hammer_limit_recs) *
423 hz / hammer_limit_recs;
425 tsleep(&trans, 0, "hmrslo", delta);
428 if (uio->uio_segflg != UIO_NOCOPY)
429 vn_lock(ap->a_vp, LK_EXCLUSIVE|LK_RETRY);
433 * Calculate the blocksize at the current offset and figure
434 * out how much we can actually write.
436 blkmask = blksize - 1;
437 offset = (int)uio->uio_offset & blkmask;
438 base_offset = uio->uio_offset & ~(int64_t)blkmask;
439 n = blksize - offset;
440 if (n > uio->uio_resid)
442 if (uio->uio_offset + n > ip->ino_data.size) {
443 vnode_pager_setsize(ap->a_vp, uio->uio_offset + n);
447 if (uio->uio_segflg == UIO_NOCOPY) {
449 * Issuing a write with the same data backing the
450 * buffer. Instantiate the buffer to collect the
451 * backing vm pages, then read-in any missing bits.
453 * This case is used by vop_stdputpages().
455 bp = getblk(ap->a_vp, base_offset,
456 blksize, GETBLK_BHEAVY, 0);
457 if ((bp->b_flags & B_CACHE) == 0) {
459 error = bread(ap->a_vp, base_offset,
462 } else if (offset == 0 && uio->uio_resid >= blksize) {
464 * Even though we are entirely overwriting the buffer
465 * we may still have to zero it out to avoid a
466 * mmap/write visibility issue.
468 bp = getblk(ap->a_vp, base_offset, blksize, GETBLK_BHEAVY, 0);
469 if ((bp->b_flags & B_CACHE) == 0)
471 } else if (base_offset >= ip->ino_data.size) {
473 * If the base offset of the buffer is beyond the
474 * file EOF, we don't have to issue a read.
476 bp = getblk(ap->a_vp, base_offset,
477 blksize, GETBLK_BHEAVY, 0);
481 * Partial overwrite, read in any missing bits then
482 * replace the portion being written.
484 error = bread(ap->a_vp, base_offset, blksize, &bp);
489 error = uiomove((char *)bp->b_data + offset,
494 * If we screwed up we have to undo any VM size changes we
500 vtruncbuf(ap->a_vp, ip->ino_data.size,
501 hammer_blocksize(ip->ino_data.size));
505 /* bp->b_flags |= B_CLUSTEROK; temporarily disabled */
506 if (ip->ino_data.size < uio->uio_offset) {
507 ip->ino_data.size = uio->uio_offset;
508 flags = HAMMER_INODE_DDIRTY;
509 vnode_pager_setsize(ap->a_vp, ip->ino_data.size);
513 ip->ino_data.mtime = trans.time;
514 flags |= HAMMER_INODE_MTIME | HAMMER_INODE_BUFS;
515 hammer_modify_inode(ip, flags);
518 * Try to keep track of cached dirty data.
520 if ((bp->b_flags & B_DIRTY) == 0) {
526 * Final buffer disposition.
529 bp->b_flags |= B_AGE;
530 if (ap->a_ioflag & IO_SYNC) {
532 } else if (ap->a_ioflag & IO_DIRECT) {
538 hammer_done_transaction(&trans);
543 * hammer_vop_access { vp, mode, cred }
547 hammer_vop_access(struct vop_access_args *ap)
549 struct hammer_inode *ip = VTOI(ap->a_vp);
554 uid = hammer_to_unix_xid(&ip->ino_data.uid);
555 gid = hammer_to_unix_xid(&ip->ino_data.gid);
557 error = vop_helper_access(ap, uid, gid, ip->ino_data.mode,
558 ip->ino_data.uflags);
563 * hammer_vop_advlock { vp, id, op, fl, flags }
567 hammer_vop_advlock(struct vop_advlock_args *ap)
569 hammer_inode_t ip = VTOI(ap->a_vp);
571 return (lf_advlock(ap, &ip->advlock, ip->ino_data.size));
575 * hammer_vop_close { vp, fflag }
579 hammer_vop_close(struct vop_close_args *ap)
581 hammer_inode_t ip = VTOI(ap->a_vp);
583 if ((ip->flags | ip->sync_flags) & HAMMER_INODE_MODMASK)
584 hammer_inode_waitreclaims(ip->hmp);
585 return (vop_stdclose(ap));
589 * hammer_vop_ncreate { nch, dvp, vpp, cred, vap }
591 * The operating system has already ensured that the directory entry
592 * does not exist and done all appropriate namespace locking.
596 hammer_vop_ncreate(struct vop_ncreate_args *ap)
598 struct hammer_transaction trans;
599 struct hammer_inode *dip;
600 struct hammer_inode *nip;
601 struct nchandle *nch;
605 dip = VTOI(ap->a_dvp);
607 if (dip->flags & HAMMER_INODE_RO)
609 if ((error = hammer_checkspace(dip->hmp)) != 0)
613 * Create a transaction to cover the operations we perform.
615 hammer_start_transaction(&trans, dip->hmp);
618 * Create a new filesystem object of the requested type. The
619 * returned inode will be referenced and shared-locked to prevent
620 * it from being moved to the flusher.
623 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
626 hkprintf("hammer_create_inode error %d\n", error);
627 hammer_done_transaction(&trans);
633 * Add the new filesystem object to the directory. This will also
634 * bump the inode's link count.
636 error = hammer_ip_add_directory(&trans, dip,
637 nch->ncp->nc_name, nch->ncp->nc_nlen,
640 hkprintf("hammer_ip_add_directory error %d\n", error);
646 hammer_rel_inode(nip, 0);
647 hammer_done_transaction(&trans);
650 error = hammer_get_vnode(nip, ap->a_vpp);
651 hammer_done_transaction(&trans);
652 hammer_rel_inode(nip, 0);
654 cache_setunresolved(ap->a_nch);
655 cache_setvp(ap->a_nch, *ap->a_vpp);
662 * hammer_vop_getattr { vp, vap }
664 * Retrieve an inode's attribute information. When accessing inodes
665 * historically we fake the atime field to ensure consistent results.
666 * The atime field is stored in the B-Tree element and allowed to be
667 * updated without cycling the element.
671 hammer_vop_getattr(struct vop_getattr_args *ap)
673 struct hammer_inode *ip = VTOI(ap->a_vp);
674 struct vattr *vap = ap->a_vap;
676 vap->va_fsid = ip->hmp->fsid_udev;
678 * XXX munge the device if we are in a pseudo-fs, so user utilities
679 * do not think its the same 'filesystem'.
681 if (ip->obj_localization)
682 vap->va_fsid += ip->obj_localization;
683 vap->va_fileid = ip->ino_leaf.base.obj_id;
684 vap->va_mode = ip->ino_data.mode;
685 vap->va_nlink = ip->ino_data.nlinks;
686 vap->va_uid = hammer_to_unix_xid(&ip->ino_data.uid);
687 vap->va_gid = hammer_to_unix_xid(&ip->ino_data.gid);
690 vap->va_size = ip->ino_data.size;
693 * We must provide a consistent atime and mtime for snapshots
694 * so people can do a 'tar cf - ... | md5' on them and get
695 * consistent results.
697 if (ip->flags & HAMMER_INODE_RO) {
698 hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_atime);
699 hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_mtime);
701 hammer_time_to_timespec(ip->ino_data.atime, &vap->va_atime);
702 hammer_time_to_timespec(ip->ino_data.mtime, &vap->va_mtime);
704 hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_ctime);
705 vap->va_flags = ip->ino_data.uflags;
706 vap->va_gen = 1; /* hammer inums are unique for all time */
707 vap->va_blocksize = HAMMER_BUFSIZE;
708 if (ip->ino_data.size >= HAMMER_XDEMARC) {
709 vap->va_bytes = (ip->ino_data.size + HAMMER_XBUFMASK64) &
711 } else if (ip->ino_data.size > HAMMER_BUFSIZE / 2) {
712 vap->va_bytes = (ip->ino_data.size + HAMMER_BUFMASK64) &
715 vap->va_bytes = (ip->ino_data.size + 15) & ~15;
717 vap->va_type = hammer_get_vnode_type(ip->ino_data.obj_type);
718 vap->va_filerev = 0; /* XXX */
719 /* mtime uniquely identifies any adjustments made to the file XXX */
720 vap->va_fsmid = ip->ino_data.mtime;
721 vap->va_uid_uuid = ip->ino_data.uid;
722 vap->va_gid_uuid = ip->ino_data.gid;
723 vap->va_fsid_uuid = ip->hmp->fsid;
724 vap->va_vaflags = VA_UID_UUID_VALID | VA_GID_UUID_VALID |
727 switch (ip->ino_data.obj_type) {
728 case HAMMER_OBJTYPE_CDEV:
729 case HAMMER_OBJTYPE_BDEV:
730 vap->va_rmajor = ip->ino_data.rmajor;
731 vap->va_rminor = ip->ino_data.rminor;
741 * hammer_vop_nresolve { nch, dvp, cred }
743 * Locate the requested directory entry.
747 hammer_vop_nresolve(struct vop_nresolve_args *ap)
749 struct hammer_transaction trans;
750 struct namecache *ncp;
754 struct hammer_cursor cursor;
762 u_int32_t localization;
765 * Misc initialization, plus handle as-of name extensions. Look for
766 * the '@@' extension. Note that as-of files and directories cannot
769 dip = VTOI(ap->a_dvp);
770 ncp = ap->a_nch->ncp;
771 asof = dip->obj_asof;
775 hammer_simple_transaction(&trans, dip->hmp);
777 for (i = 0; i < nlen; ++i) {
778 if (ncp->nc_name[i] == '@' && ncp->nc_name[i+1] == '@') {
779 asof = hammer_str_to_tid(ncp->nc_name + i + 2);
780 flags |= HAMMER_INODE_RO;
787 * If there is no path component the time extension is relative to
791 ip = hammer_get_inode(&trans, dip, dip->obj_id,
792 asof, dip->obj_localization,
795 error = hammer_get_vnode(ip, &vp);
796 hammer_rel_inode(ip, 0);
802 cache_setvp(ap->a_nch, vp);
809 * Calculate the namekey and setup the key range for the scan. This
810 * works kinda like a chained hash table where the lower 32 bits
811 * of the namekey synthesize the chain.
813 * The key range is inclusive of both key_beg and key_end.
815 namekey = hammer_directory_namekey(ncp->nc_name, nlen);
817 error = hammer_init_cursor(&trans, &cursor, &dip->cache[1], dip);
818 cursor.key_beg.localization = dip->obj_localization +
819 HAMMER_LOCALIZE_MISC;
820 cursor.key_beg.obj_id = dip->obj_id;
821 cursor.key_beg.key = namekey;
822 cursor.key_beg.create_tid = 0;
823 cursor.key_beg.delete_tid = 0;
824 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
825 cursor.key_beg.obj_type = 0;
827 cursor.key_end = cursor.key_beg;
828 cursor.key_end.key |= 0xFFFFFFFFULL;
830 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
833 * Scan all matching records (the chain), locate the one matching
834 * the requested path component.
836 * The hammer_ip_*() functions merge in-memory records with on-disk
837 * records for the purposes of the search.
840 localization = HAMMER_DEF_LOCALIZATION;
843 error = hammer_ip_first(&cursor);
845 error = hammer_ip_resolve_data(&cursor);
848 if (nlen == cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF &&
849 bcmp(ncp->nc_name, cursor.data->entry.name, nlen) == 0) {
850 obj_id = cursor.data->entry.obj_id;
851 localization = cursor.data->entry.localization;
854 error = hammer_ip_next(&cursor);
857 hammer_done_cursor(&cursor);
859 ip = hammer_get_inode(&trans, dip, obj_id,
863 error = hammer_get_vnode(ip, &vp);
864 hammer_rel_inode(ip, 0);
870 cache_setvp(ap->a_nch, vp);
873 } else if (error == ENOENT) {
874 cache_setvp(ap->a_nch, NULL);
877 hammer_done_transaction(&trans);
882 * hammer_vop_nlookupdotdot { dvp, vpp, cred }
884 * Locate the parent directory of a directory vnode.
886 * dvp is referenced but not locked. *vpp must be returned referenced and
887 * locked. A parent_obj_id of 0 does not necessarily indicate that we are
888 * at the root, instead it could indicate that the directory we were in was
891 * NOTE: as-of sequences are not linked into the directory structure. If
892 * we are at the root with a different asof then the mount point, reload
893 * the same directory with the mount point's asof. I'm not sure what this
894 * will do to NFS. We encode ASOF stamps in NFS file handles so it might not
895 * get confused, but it hasn't been tested.
899 hammer_vop_nlookupdotdot(struct vop_nlookupdotdot_args *ap)
901 struct hammer_transaction trans;
902 struct hammer_inode *dip;
903 struct hammer_inode *ip;
904 int64_t parent_obj_id;
905 u_int32_t parent_obj_localization;
909 dip = VTOI(ap->a_dvp);
910 asof = dip->obj_asof;
913 * Whos are parent? This could be the root of a pseudo-filesystem
914 * whos parent is in another localization domain.
916 parent_obj_id = dip->ino_data.parent_obj_id;
917 if (dip->obj_id == HAMMER_OBJID_ROOT)
918 parent_obj_localization = dip->ino_data.ext.obj.parent_obj_localization;
920 parent_obj_localization = dip->obj_localization;
922 if (parent_obj_id == 0) {
923 if (dip->obj_id == HAMMER_OBJID_ROOT &&
924 asof != dip->hmp->asof) {
925 parent_obj_id = dip->obj_id;
926 asof = dip->hmp->asof;
927 *ap->a_fakename = kmalloc(19, M_TEMP, M_WAITOK);
928 ksnprintf(*ap->a_fakename, 19, "0x%016llx",
936 hammer_simple_transaction(&trans, dip->hmp);
938 ip = hammer_get_inode(&trans, dip, parent_obj_id,
939 asof, parent_obj_localization,
942 error = hammer_get_vnode(ip, ap->a_vpp);
943 hammer_rel_inode(ip, 0);
947 hammer_done_transaction(&trans);
952 * hammer_vop_nlink { nch, dvp, vp, cred }
956 hammer_vop_nlink(struct vop_nlink_args *ap)
958 struct hammer_transaction trans;
959 struct hammer_inode *dip;
960 struct hammer_inode *ip;
961 struct nchandle *nch;
965 dip = VTOI(ap->a_dvp);
968 if (dip->flags & HAMMER_INODE_RO)
970 if (ip->flags & HAMMER_INODE_RO)
972 if ((error = hammer_checkspace(dip->hmp)) != 0)
976 * Create a transaction to cover the operations we perform.
978 hammer_start_transaction(&trans, dip->hmp);
981 * Add the filesystem object to the directory. Note that neither
982 * dip nor ip are referenced or locked, but their vnodes are
983 * referenced. This function will bump the inode's link count.
985 error = hammer_ip_add_directory(&trans, dip,
986 nch->ncp->nc_name, nch->ncp->nc_nlen,
993 cache_setunresolved(nch);
994 cache_setvp(nch, ap->a_vp);
996 hammer_done_transaction(&trans);
1001 * hammer_vop_nmkdir { nch, dvp, vpp, cred, vap }
1003 * The operating system has already ensured that the directory entry
1004 * does not exist and done all appropriate namespace locking.
1008 hammer_vop_nmkdir(struct vop_nmkdir_args *ap)
1010 struct hammer_transaction trans;
1011 struct hammer_inode *dip;
1012 struct hammer_inode *nip;
1013 struct nchandle *nch;
1017 dip = VTOI(ap->a_dvp);
1019 if (dip->flags & HAMMER_INODE_RO)
1021 if ((error = hammer_checkspace(dip->hmp)) != 0)
1025 * Create a transaction to cover the operations we perform.
1027 hammer_start_transaction(&trans, dip->hmp);
1030 * Create a new filesystem object of the requested type. The
1031 * returned inode will be referenced but not locked.
1033 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
1036 hkprintf("hammer_mkdir error %d\n", error);
1037 hammer_done_transaction(&trans);
1042 * Add the new filesystem object to the directory. This will also
1043 * bump the inode's link count.
1045 error = hammer_ip_add_directory(&trans, dip,
1046 nch->ncp->nc_name, nch->ncp->nc_nlen,
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;
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 * If mknod specifies a directory a pseudo-fs is created.
1105 pseudofs = (ap->a_vap->va_type == VDIR);
1106 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
1107 dip, pseudofs, &nip);
1109 hammer_done_transaction(&trans);
1115 * Add the new filesystem object to the directory. This will also
1116 * bump the inode's link count.
1118 error = hammer_ip_add_directory(&trans, dip,
1119 nch->ncp->nc_name, nch->ncp->nc_nlen,
1126 hammer_rel_inode(nip, 0);
1129 error = hammer_get_vnode(nip, ap->a_vpp);
1130 hammer_rel_inode(nip, 0);
1132 cache_setunresolved(ap->a_nch);
1133 cache_setvp(ap->a_nch, *ap->a_vpp);
1136 hammer_done_transaction(&trans);
1141 * hammer_vop_open { vp, mode, cred, fp }
1145 hammer_vop_open(struct vop_open_args *ap)
1149 ip = VTOI(ap->a_vp);
1151 if ((ap->a_mode & FWRITE) && (ip->flags & HAMMER_INODE_RO))
1153 return(vop_stdopen(ap));
1157 * hammer_vop_pathconf { vp, name, retval }
1161 hammer_vop_pathconf(struct vop_pathconf_args *ap)
1167 * hammer_vop_print { vp }
1171 hammer_vop_print(struct vop_print_args *ap)
1177 * hammer_vop_readdir { vp, uio, cred, *eofflag, *ncookies, off_t **cookies }
1181 hammer_vop_readdir(struct vop_readdir_args *ap)
1183 struct hammer_transaction trans;
1184 struct hammer_cursor cursor;
1185 struct hammer_inode *ip;
1187 hammer_base_elm_t base;
1195 ip = VTOI(ap->a_vp);
1197 saveoff = uio->uio_offset;
1199 if (ap->a_ncookies) {
1200 ncookies = uio->uio_resid / 16 + 1;
1201 if (ncookies > 1024)
1203 cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
1211 hammer_simple_transaction(&trans, ip->hmp);
1214 * Handle artificial entries
1218 r = vop_write_dirent(&error, uio, ip->obj_id, DT_DIR, 1, ".");
1222 cookies[cookie_index] = saveoff;
1225 if (cookie_index == ncookies)
1229 if (ip->ino_data.parent_obj_id) {
1230 r = vop_write_dirent(&error, uio,
1231 ip->ino_data.parent_obj_id,
1234 r = vop_write_dirent(&error, uio,
1235 ip->obj_id, DT_DIR, 2, "..");
1240 cookies[cookie_index] = saveoff;
1243 if (cookie_index == ncookies)
1248 * Key range (begin and end inclusive) to scan. Directory keys
1249 * directly translate to a 64 bit 'seek' position.
1251 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
1252 cursor.key_beg.localization = ip->obj_localization +
1253 HAMMER_LOCALIZE_MISC;
1254 cursor.key_beg.obj_id = ip->obj_id;
1255 cursor.key_beg.create_tid = 0;
1256 cursor.key_beg.delete_tid = 0;
1257 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1258 cursor.key_beg.obj_type = 0;
1259 cursor.key_beg.key = saveoff;
1261 cursor.key_end = cursor.key_beg;
1262 cursor.key_end.key = HAMMER_MAX_KEY;
1263 cursor.asof = ip->obj_asof;
1264 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
1266 error = hammer_ip_first(&cursor);
1268 while (error == 0) {
1269 error = hammer_ip_resolve_data(&cursor);
1272 base = &cursor.leaf->base;
1273 saveoff = base->key;
1274 KKASSERT(cursor.leaf->data_len > HAMMER_ENTRY_NAME_OFF);
1276 if (base->obj_id != ip->obj_id)
1277 panic("readdir: bad record at %p", cursor.node);
1279 r = vop_write_dirent(
1280 &error, uio, cursor.data->entry.obj_id,
1281 hammer_get_dtype(cursor.leaf->base.obj_type),
1282 cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF ,
1283 (void *)cursor.data->entry.name);
1288 cookies[cookie_index] = base->key;
1290 if (cookie_index == ncookies)
1292 error = hammer_ip_next(&cursor);
1294 hammer_done_cursor(&cursor);
1297 hammer_done_transaction(&trans);
1300 *ap->a_eofflag = (error == ENOENT);
1301 uio->uio_offset = saveoff;
1302 if (error && cookie_index == 0) {
1303 if (error == ENOENT)
1306 kfree(cookies, M_TEMP);
1307 *ap->a_ncookies = 0;
1308 *ap->a_cookies = NULL;
1311 if (error == ENOENT)
1314 *ap->a_ncookies = cookie_index;
1315 *ap->a_cookies = cookies;
1322 * hammer_vop_readlink { vp, uio, cred }
1326 hammer_vop_readlink(struct vop_readlink_args *ap)
1328 struct hammer_transaction trans;
1329 struct hammer_cursor cursor;
1330 struct hammer_inode *ip;
1333 ip = VTOI(ap->a_vp);
1336 * Shortcut if the symlink data was stuffed into ino_data.
1338 if (ip->ino_data.size <= HAMMER_INODE_BASESYMLEN) {
1339 error = uiomove(ip->ino_data.ext.symlink,
1340 ip->ino_data.size, ap->a_uio);
1347 hammer_simple_transaction(&trans, ip->hmp);
1348 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
1351 * Key range (begin and end inclusive) to scan. Directory keys
1352 * directly translate to a 64 bit 'seek' position.
1354 cursor.key_beg.localization = ip->obj_localization +
1355 HAMMER_LOCALIZE_MISC;
1356 cursor.key_beg.obj_id = ip->obj_id;
1357 cursor.key_beg.create_tid = 0;
1358 cursor.key_beg.delete_tid = 0;
1359 cursor.key_beg.rec_type = HAMMER_RECTYPE_FIX;
1360 cursor.key_beg.obj_type = 0;
1361 cursor.key_beg.key = HAMMER_FIXKEY_SYMLINK;
1362 cursor.asof = ip->obj_asof;
1363 cursor.flags |= HAMMER_CURSOR_ASOF;
1365 error = hammer_ip_lookup(&cursor);
1367 error = hammer_ip_resolve_data(&cursor);
1369 KKASSERT(cursor.leaf->data_len >=
1370 HAMMER_SYMLINK_NAME_OFF);
1371 error = uiomove(cursor.data->symlink.name,
1372 cursor.leaf->data_len -
1373 HAMMER_SYMLINK_NAME_OFF,
1377 hammer_done_cursor(&cursor);
1378 hammer_done_transaction(&trans);
1383 * hammer_vop_nremove { nch, dvp, cred }
1387 hammer_vop_nremove(struct vop_nremove_args *ap)
1389 struct hammer_transaction trans;
1390 struct hammer_inode *dip;
1393 dip = VTOI(ap->a_dvp);
1395 if (hammer_nohistory(dip) == 0 &&
1396 (error = hammer_checkspace(dip->hmp)) != 0) {
1400 hammer_start_transaction(&trans, dip->hmp);
1401 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp, ap->a_cred, 0);
1402 hammer_done_transaction(&trans);
1408 * hammer_vop_nrename { fnch, tnch, fdvp, tdvp, cred }
1412 hammer_vop_nrename(struct vop_nrename_args *ap)
1414 struct hammer_transaction trans;
1415 struct namecache *fncp;
1416 struct namecache *tncp;
1417 struct hammer_inode *fdip;
1418 struct hammer_inode *tdip;
1419 struct hammer_inode *ip;
1420 struct hammer_cursor cursor;
1424 fdip = VTOI(ap->a_fdvp);
1425 tdip = VTOI(ap->a_tdvp);
1426 fncp = ap->a_fnch->ncp;
1427 tncp = ap->a_tnch->ncp;
1428 ip = VTOI(fncp->nc_vp);
1429 KKASSERT(ip != NULL);
1431 if (fdip->flags & HAMMER_INODE_RO)
1433 if (tdip->flags & HAMMER_INODE_RO)
1435 if (ip->flags & HAMMER_INODE_RO)
1437 if ((error = hammer_checkspace(fdip->hmp)) != 0)
1440 hammer_start_transaction(&trans, fdip->hmp);
1443 * Remove tncp from the target directory and then link ip as
1444 * tncp. XXX pass trans to dounlink
1446 * Force the inode sync-time to match the transaction so it is
1447 * in-sync with the creation of the target directory entry.
1449 error = hammer_dounlink(&trans, ap->a_tnch, ap->a_tdvp, ap->a_cred, 0);
1450 if (error == 0 || error == ENOENT) {
1451 error = hammer_ip_add_directory(&trans, tdip,
1452 tncp->nc_name, tncp->nc_nlen,
1455 ip->ino_data.parent_obj_id = tdip->obj_id;
1456 hammer_modify_inode(ip, HAMMER_INODE_DDIRTY);
1460 goto failed; /* XXX */
1463 * Locate the record in the originating directory and remove it.
1465 * Calculate the namekey and setup the key range for the scan. This
1466 * works kinda like a chained hash table where the lower 32 bits
1467 * of the namekey synthesize the chain.
1469 * The key range is inclusive of both key_beg and key_end.
1471 namekey = hammer_directory_namekey(fncp->nc_name, fncp->nc_nlen);
1473 hammer_init_cursor(&trans, &cursor, &fdip->cache[1], fdip);
1474 cursor.key_beg.localization = fdip->obj_localization +
1475 HAMMER_LOCALIZE_MISC;
1476 cursor.key_beg.obj_id = fdip->obj_id;
1477 cursor.key_beg.key = namekey;
1478 cursor.key_beg.create_tid = 0;
1479 cursor.key_beg.delete_tid = 0;
1480 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1481 cursor.key_beg.obj_type = 0;
1483 cursor.key_end = cursor.key_beg;
1484 cursor.key_end.key |= 0xFFFFFFFFULL;
1485 cursor.asof = fdip->obj_asof;
1486 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
1489 * Scan all matching records (the chain), locate the one matching
1490 * the requested path component.
1492 * The hammer_ip_*() functions merge in-memory records with on-disk
1493 * records for the purposes of the search.
1495 error = hammer_ip_first(&cursor);
1496 while (error == 0) {
1497 if (hammer_ip_resolve_data(&cursor) != 0)
1499 nlen = cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF;
1501 if (fncp->nc_nlen == nlen &&
1502 bcmp(fncp->nc_name, cursor.data->entry.name, nlen) == 0) {
1505 error = hammer_ip_next(&cursor);
1509 * If all is ok we have to get the inode so we can adjust nlinks.
1511 * WARNING: hammer_ip_del_directory() may have to terminate the
1512 * cursor to avoid a recursion. It's ok to call hammer_done_cursor()
1516 error = hammer_ip_del_directory(&trans, &cursor, fdip, ip);
1519 * XXX A deadlock here will break rename's atomicy for the purposes
1520 * of crash recovery.
1522 if (error == EDEADLK) {
1523 hammer_done_cursor(&cursor);
1528 * Cleanup and tell the kernel that the rename succeeded.
1530 hammer_done_cursor(&cursor);
1532 cache_rename(ap->a_fnch, ap->a_tnch);
1535 hammer_done_transaction(&trans);
1540 * hammer_vop_nrmdir { nch, dvp, cred }
1544 hammer_vop_nrmdir(struct vop_nrmdir_args *ap)
1546 struct hammer_transaction trans;
1547 struct hammer_inode *dip;
1550 dip = VTOI(ap->a_dvp);
1552 if (hammer_nohistory(dip) == 0 &&
1553 (error = hammer_checkspace(dip->hmp)) != 0) {
1557 hammer_start_transaction(&trans, dip->hmp);
1558 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp, ap->a_cred, 0);
1559 hammer_done_transaction(&trans);
1565 * hammer_vop_setattr { vp, vap, cred }
1569 hammer_vop_setattr(struct vop_setattr_args *ap)
1571 struct hammer_transaction trans;
1573 struct hammer_inode *ip;
1578 int64_t aligned_size;
1582 ip = ap->a_vp->v_data;
1585 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
1587 if (ip->flags & HAMMER_INODE_RO)
1589 if (hammer_nohistory(ip) == 0 &&
1590 (error = hammer_checkspace(ip->hmp)) != 0) {
1594 hammer_start_transaction(&trans, ip->hmp);
1597 if (vap->va_flags != VNOVAL) {
1598 flags = ip->ino_data.uflags;
1599 error = vop_helper_setattr_flags(&flags, vap->va_flags,
1600 hammer_to_unix_xid(&ip->ino_data.uid),
1603 if (ip->ino_data.uflags != flags) {
1604 ip->ino_data.uflags = flags;
1605 modflags |= HAMMER_INODE_DDIRTY;
1607 if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) {
1614 if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) {
1618 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
1619 mode_t cur_mode = ip->ino_data.mode;
1620 uid_t cur_uid = hammer_to_unix_xid(&ip->ino_data.uid);
1621 gid_t cur_gid = hammer_to_unix_xid(&ip->ino_data.gid);
1625 error = vop_helper_chown(ap->a_vp, vap->va_uid, vap->va_gid,
1627 &cur_uid, &cur_gid, &cur_mode);
1629 hammer_guid_to_uuid(&uuid_uid, cur_uid);
1630 hammer_guid_to_uuid(&uuid_gid, cur_gid);
1631 if (bcmp(&uuid_uid, &ip->ino_data.uid,
1632 sizeof(uuid_uid)) ||
1633 bcmp(&uuid_gid, &ip->ino_data.gid,
1634 sizeof(uuid_gid)) ||
1635 ip->ino_data.mode != cur_mode
1637 ip->ino_data.uid = uuid_uid;
1638 ip->ino_data.gid = uuid_gid;
1639 ip->ino_data.mode = cur_mode;
1641 modflags |= HAMMER_INODE_DDIRTY;
1644 while (vap->va_size != VNOVAL && ip->ino_data.size != vap->va_size) {
1645 switch(ap->a_vp->v_type) {
1647 if (vap->va_size == ip->ino_data.size)
1650 * XXX break atomicy, we can deadlock the backend
1651 * if we do not release the lock. Probably not a
1654 blksize = hammer_blocksize(vap->va_size);
1655 if (vap->va_size < ip->ino_data.size) {
1656 vtruncbuf(ap->a_vp, vap->va_size, blksize);
1659 vnode_pager_setsize(ap->a_vp, vap->va_size);
1662 ip->ino_data.size = vap->va_size;
1663 modflags |= HAMMER_INODE_DDIRTY;
1666 * on-media truncation is cached in the inode until
1667 * the inode is synchronized.
1670 hammer_ip_frontend_trunc(ip, vap->va_size);
1671 hammer_update_rsv_databufs(ip);
1672 #ifdef DEBUG_TRUNCATE
1673 if (HammerTruncIp == NULL)
1676 if ((ip->flags & HAMMER_INODE_TRUNCATED) == 0) {
1677 ip->flags |= HAMMER_INODE_TRUNCATED;
1678 ip->trunc_off = vap->va_size;
1679 #ifdef DEBUG_TRUNCATE
1680 if (ip == HammerTruncIp)
1681 kprintf("truncate1 %016llx\n", ip->trunc_off);
1683 } else if (ip->trunc_off > vap->va_size) {
1684 ip->trunc_off = vap->va_size;
1685 #ifdef DEBUG_TRUNCATE
1686 if (ip == HammerTruncIp)
1687 kprintf("truncate2 %016llx\n", ip->trunc_off);
1690 #ifdef DEBUG_TRUNCATE
1691 if (ip == HammerTruncIp)
1692 kprintf("truncate3 %016llx (ignored)\n", vap->va_size);
1698 * If truncating we have to clean out a portion of
1699 * the last block on-disk. We do this in the
1700 * front-end buffer cache.
1702 aligned_size = (vap->va_size + (blksize - 1)) &
1703 ~(int64_t)(blksize - 1);
1704 if (truncating && vap->va_size < aligned_size) {
1708 aligned_size -= blksize;
1710 offset = (int)vap->va_size & (blksize - 1);
1711 error = bread(ap->a_vp, aligned_size,
1713 hammer_ip_frontend_trunc(ip, aligned_size);
1715 bzero(bp->b_data + offset,
1719 kprintf("ERROR %d\n", error);
1725 if ((ip->flags & HAMMER_INODE_TRUNCATED) == 0) {
1726 ip->flags |= HAMMER_INODE_TRUNCATED;
1727 ip->trunc_off = vap->va_size;
1728 } else if (ip->trunc_off > vap->va_size) {
1729 ip->trunc_off = vap->va_size;
1731 hammer_ip_frontend_trunc(ip, vap->va_size);
1732 ip->ino_data.size = vap->va_size;
1733 modflags |= HAMMER_INODE_DDIRTY;
1741 if (vap->va_atime.tv_sec != VNOVAL) {
1742 ip->ino_data.atime =
1743 hammer_timespec_to_time(&vap->va_atime);
1744 modflags |= HAMMER_INODE_ATIME;
1746 if (vap->va_mtime.tv_sec != VNOVAL) {
1747 ip->ino_data.mtime =
1748 hammer_timespec_to_time(&vap->va_mtime);
1749 modflags |= HAMMER_INODE_MTIME;
1751 if (vap->va_mode != (mode_t)VNOVAL) {
1752 mode_t cur_mode = ip->ino_data.mode;
1753 uid_t cur_uid = hammer_to_unix_xid(&ip->ino_data.uid);
1754 gid_t cur_gid = hammer_to_unix_xid(&ip->ino_data.gid);
1756 error = vop_helper_chmod(ap->a_vp, vap->va_mode, ap->a_cred,
1757 cur_uid, cur_gid, &cur_mode);
1758 if (error == 0 && ip->ino_data.mode != cur_mode) {
1759 ip->ino_data.mode = cur_mode;
1760 modflags |= HAMMER_INODE_DDIRTY;
1765 hammer_modify_inode(ip, modflags);
1766 hammer_done_transaction(&trans);
1771 * hammer_vop_nsymlink { nch, dvp, vpp, cred, vap, target }
1775 hammer_vop_nsymlink(struct vop_nsymlink_args *ap)
1777 struct hammer_transaction trans;
1778 struct hammer_inode *dip;
1779 struct hammer_inode *nip;
1780 struct nchandle *nch;
1781 hammer_record_t record;
1785 ap->a_vap->va_type = VLNK;
1788 dip = VTOI(ap->a_dvp);
1790 if (dip->flags & HAMMER_INODE_RO)
1792 if ((error = hammer_checkspace(dip->hmp)) != 0)
1796 * Create a transaction to cover the operations we perform.
1798 hammer_start_transaction(&trans, dip->hmp);
1801 * Create a new filesystem object of the requested type. The
1802 * returned inode will be referenced but not locked.
1805 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
1808 hammer_done_transaction(&trans);
1814 * Add a record representing the symlink. symlink stores the link
1815 * as pure data, not a string, and is no \0 terminated.
1818 bytes = strlen(ap->a_target);
1820 if (bytes <= HAMMER_INODE_BASESYMLEN) {
1821 bcopy(ap->a_target, nip->ino_data.ext.symlink, bytes);
1823 record = hammer_alloc_mem_record(nip, bytes);
1824 record->type = HAMMER_MEM_RECORD_GENERAL;
1826 record->leaf.base.localization = nip->obj_localization +
1827 HAMMER_LOCALIZE_MISC;
1828 record->leaf.base.key = HAMMER_FIXKEY_SYMLINK;
1829 record->leaf.base.rec_type = HAMMER_RECTYPE_FIX;
1830 record->leaf.data_len = bytes;
1831 KKASSERT(HAMMER_SYMLINK_NAME_OFF == 0);
1832 bcopy(ap->a_target, record->data->symlink.name, bytes);
1833 error = hammer_ip_add_record(&trans, record);
1837 * Set the file size to the length of the link.
1840 nip->ino_data.size = bytes;
1841 hammer_modify_inode(nip, HAMMER_INODE_DDIRTY);
1845 error = hammer_ip_add_directory(&trans, dip, nch->ncp->nc_name,
1846 nch->ncp->nc_nlen, nip);
1852 hammer_rel_inode(nip, 0);
1855 error = hammer_get_vnode(nip, ap->a_vpp);
1856 hammer_rel_inode(nip, 0);
1858 cache_setunresolved(ap->a_nch);
1859 cache_setvp(ap->a_nch, *ap->a_vpp);
1862 hammer_done_transaction(&trans);
1867 * hammer_vop_nwhiteout { nch, dvp, cred, flags }
1871 hammer_vop_nwhiteout(struct vop_nwhiteout_args *ap)
1873 struct hammer_transaction trans;
1874 struct hammer_inode *dip;
1877 dip = VTOI(ap->a_dvp);
1879 if (hammer_nohistory(dip) == 0 &&
1880 (error = hammer_checkspace(dip->hmp)) != 0) {
1884 hammer_start_transaction(&trans, dip->hmp);
1885 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp,
1886 ap->a_cred, ap->a_flags);
1887 hammer_done_transaction(&trans);
1893 * hammer_vop_ioctl { vp, command, data, fflag, cred }
1897 hammer_vop_ioctl(struct vop_ioctl_args *ap)
1899 struct hammer_inode *ip = ap->a_vp->v_data;
1901 return(hammer_ioctl(ip, ap->a_command, ap->a_data,
1902 ap->a_fflag, ap->a_cred));
1907 hammer_vop_mountctl(struct vop_mountctl_args *ap)
1912 mp = ap->a_head.a_ops->head.vv_mount;
1915 case MOUNTCTL_SET_EXPORT:
1916 if (ap->a_ctllen != sizeof(struct export_args))
1918 error = hammer_vfs_export(mp, ap->a_op,
1919 (const struct export_args *)ap->a_ctl);
1922 error = journal_mountctl(ap);
1929 * hammer_vop_strategy { vp, bio }
1931 * Strategy call, used for regular file read & write only. Note that the
1932 * bp may represent a cluster.
1934 * To simplify operation and allow better optimizations in the future,
1935 * this code does not make any assumptions with regards to buffer alignment
1940 hammer_vop_strategy(struct vop_strategy_args *ap)
1945 bp = ap->a_bio->bio_buf;
1949 error = hammer_vop_strategy_read(ap);
1952 error = hammer_vop_strategy_write(ap);
1955 bp->b_error = error = EINVAL;
1956 bp->b_flags |= B_ERROR;
1964 * Read from a regular file. Iterate the related records and fill in the
1965 * BIO/BUF. Gaps are zero-filled.
1967 * The support code in hammer_object.c should be used to deal with mixed
1968 * in-memory and on-disk records.
1970 * NOTE: Can be called from the cluster code with an oversized buf.
1976 hammer_vop_strategy_read(struct vop_strategy_args *ap)
1978 struct hammer_transaction trans;
1979 struct hammer_inode *ip;
1980 struct hammer_cursor cursor;
1981 hammer_base_elm_t base;
1982 hammer_off_t disk_offset;
1996 ip = ap->a_vp->v_data;
1999 * The zone-2 disk offset may have been set by the cluster code via
2000 * a BMAP operation, or else should be NOOFFSET.
2002 * Checking the high bits for a match against zone-2 should suffice.
2004 nbio = push_bio(bio);
2005 if ((nbio->bio_offset & HAMMER_OFF_ZONE_MASK) ==
2006 HAMMER_ZONE_RAW_BUFFER) {
2007 error = hammer_io_direct_read(ip->hmp, nbio);
2012 * Well, that sucked. Do it the hard way. If all the stars are
2013 * aligned we may still be able to issue a direct-read.
2015 hammer_simple_transaction(&trans, ip->hmp);
2016 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
2019 * Key range (begin and end inclusive) to scan. Note that the key's
2020 * stored in the actual records represent BASE+LEN, not BASE. The
2021 * first record containing bio_offset will have a key > bio_offset.
2023 cursor.key_beg.localization = ip->obj_localization +
2024 HAMMER_LOCALIZE_MISC;
2025 cursor.key_beg.obj_id = ip->obj_id;
2026 cursor.key_beg.create_tid = 0;
2027 cursor.key_beg.delete_tid = 0;
2028 cursor.key_beg.obj_type = 0;
2029 cursor.key_beg.key = bio->bio_offset + 1;
2030 cursor.asof = ip->obj_asof;
2031 cursor.flags |= HAMMER_CURSOR_ASOF;
2033 cursor.key_end = cursor.key_beg;
2034 KKASSERT(ip->ino_data.obj_type == HAMMER_OBJTYPE_REGFILE);
2036 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_DBFILE) {
2037 cursor.key_beg.rec_type = HAMMER_RECTYPE_DB;
2038 cursor.key_end.rec_type = HAMMER_RECTYPE_DB;
2039 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
2043 ran_end = bio->bio_offset + bp->b_bufsize;
2044 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
2045 cursor.key_end.rec_type = HAMMER_RECTYPE_DATA;
2046 tmp64 = ran_end + MAXPHYS + 1; /* work-around GCC-4 bug */
2047 if (tmp64 < ran_end)
2048 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
2050 cursor.key_end.key = ran_end + MAXPHYS + 1;
2052 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
2054 error = hammer_ip_first(&cursor);
2057 while (error == 0) {
2059 * Get the base file offset of the record. The key for
2060 * data records is (base + bytes) rather then (base).
2062 base = &cursor.leaf->base;
2063 rec_offset = base->key - cursor.leaf->data_len;
2066 * Calculate the gap, if any, and zero-fill it.
2068 * n is the offset of the start of the record verses our
2069 * current seek offset in the bio.
2071 n = (int)(rec_offset - (bio->bio_offset + boff));
2073 if (n > bp->b_bufsize - boff)
2074 n = bp->b_bufsize - boff;
2075 bzero((char *)bp->b_data + boff, n);
2081 * Calculate the data offset in the record and the number
2082 * of bytes we can copy.
2084 * There are two degenerate cases. First, boff may already
2085 * be at bp->b_bufsize. Secondly, the data offset within
2086 * the record may exceed the record's size.
2090 n = cursor.leaf->data_len - roff;
2092 kprintf("strategy_read: bad n=%d roff=%d\n", n, roff);
2094 } else if (n > bp->b_bufsize - boff) {
2095 n = bp->b_bufsize - boff;
2099 * Deal with cached truncations. This cool bit of code
2100 * allows truncate()/ftruncate() to avoid having to sync
2103 * If the frontend is truncated then all backend records are
2104 * subject to the frontend's truncation.
2106 * If the backend is truncated then backend records on-disk
2107 * (but not in-memory) are subject to the backend's
2108 * truncation. In-memory records owned by the backend
2109 * represent data written after the truncation point on the
2110 * backend and must not be truncated.
2112 * Truncate operations deal with frontend buffer cache
2113 * buffers and frontend-owned in-memory records synchronously.
2115 if (ip->flags & HAMMER_INODE_TRUNCATED) {
2116 if (hammer_cursor_ondisk(&cursor) ||
2117 cursor.iprec->flush_state == HAMMER_FST_FLUSH) {
2118 if (ip->trunc_off <= rec_offset)
2120 else if (ip->trunc_off < rec_offset + n)
2121 n = (int)(ip->trunc_off - rec_offset);
2124 if (ip->sync_flags & HAMMER_INODE_TRUNCATED) {
2125 if (hammer_cursor_ondisk(&cursor)) {
2126 if (ip->sync_trunc_off <= rec_offset)
2128 else if (ip->sync_trunc_off < rec_offset + n)
2129 n = (int)(ip->sync_trunc_off - rec_offset);
2134 * Try to issue a direct read into our bio if possible,
2135 * otherwise resolve the element data into a hammer_buffer
2138 * The buffer on-disk should be zerod past any real
2139 * truncation point, but may not be for any synthesized
2140 * truncation point from above.
2142 if (boff == 0 && n == bp->b_bufsize &&
2143 ((cursor.leaf->data_offset + roff) & HAMMER_BUFMASK) == 0) {
2144 disk_offset = hammer_blockmap_lookup(
2146 cursor.leaf->data_offset + roff,
2150 nbio->bio_offset = disk_offset;
2151 error = hammer_io_direct_read(trans.hmp, nbio);
2154 error = hammer_ip_resolve_data(&cursor);
2156 bcopy((char *)cursor.data + roff,
2157 (char *)bp->b_data + boff, n);
2164 * Iterate until we have filled the request.
2167 if (boff == bp->b_bufsize)
2169 error = hammer_ip_next(&cursor);
2173 * There may have been a gap after the last record
2175 if (error == ENOENT)
2177 if (error == 0 && boff != bp->b_bufsize) {
2178 KKASSERT(boff < bp->b_bufsize);
2179 bzero((char *)bp->b_data + boff, bp->b_bufsize - boff);
2180 /* boff = bp->b_bufsize; */
2183 bp->b_error = error;
2185 bp->b_flags |= B_ERROR;
2190 hammer_cache_node(&ip->cache[1], cursor.node);
2191 hammer_done_cursor(&cursor);
2192 hammer_done_transaction(&trans);
2197 * BMAP operation - used to support cluster_read() only.
2199 * (struct vnode *vp, off_t loffset, off_t *doffsetp, int *runp, int *runb)
2201 * This routine may return EOPNOTSUPP if the opration is not supported for
2202 * the specified offset. The contents of the pointer arguments do not
2203 * need to be initialized in that case.
2205 * If a disk address is available and properly aligned return 0 with
2206 * *doffsetp set to the zone-2 address, and *runp / *runb set appropriately
2207 * to the run-length relative to that offset. Callers may assume that
2208 * *doffsetp is valid if 0 is returned, even if *runp is not sufficiently
2209 * large, so return EOPNOTSUPP if it is not sufficiently large.
2213 hammer_vop_bmap(struct vop_bmap_args *ap)
2215 struct hammer_transaction trans;
2216 struct hammer_inode *ip;
2217 struct hammer_cursor cursor;
2218 hammer_base_elm_t base;
2222 int64_t base_offset;
2223 int64_t base_disk_offset;
2224 int64_t last_offset;
2225 hammer_off_t last_disk_offset;
2226 hammer_off_t disk_offset;
2231 ip = ap->a_vp->v_data;
2234 * We can only BMAP regular files. We can't BMAP database files,
2237 if (ip->ino_data.obj_type != HAMMER_OBJTYPE_REGFILE)
2241 * bmap is typically called with runp/runb both NULL when used
2242 * for writing. We do not support BMAP for writing atm.
2244 if (ap->a_cmd != BUF_CMD_READ)
2248 * Scan the B-Tree to acquire blockmap addresses, then translate
2251 hammer_simple_transaction(&trans, ip->hmp);
2253 kprintf("bmap_beg %016llx ip->cache %p\n", ap->a_loffset, ip->cache[1]);
2255 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
2258 * Key range (begin and end inclusive) to scan. Note that the key's
2259 * stored in the actual records represent BASE+LEN, not BASE. The
2260 * first record containing bio_offset will have a key > bio_offset.
2262 cursor.key_beg.localization = ip->obj_localization +
2263 HAMMER_LOCALIZE_MISC;
2264 cursor.key_beg.obj_id = ip->obj_id;
2265 cursor.key_beg.create_tid = 0;
2266 cursor.key_beg.delete_tid = 0;
2267 cursor.key_beg.obj_type = 0;
2269 cursor.key_beg.key = ap->a_loffset - MAXPHYS + 1;
2271 cursor.key_beg.key = ap->a_loffset + 1;
2272 if (cursor.key_beg.key < 0)
2273 cursor.key_beg.key = 0;
2274 cursor.asof = ip->obj_asof;
2275 cursor.flags |= HAMMER_CURSOR_ASOF;
2277 cursor.key_end = cursor.key_beg;
2278 KKASSERT(ip->ino_data.obj_type == HAMMER_OBJTYPE_REGFILE);
2280 ran_end = ap->a_loffset + MAXPHYS;
2281 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
2282 cursor.key_end.rec_type = HAMMER_RECTYPE_DATA;
2283 tmp64 = ran_end + MAXPHYS + 1; /* work-around GCC-4 bug */
2284 if (tmp64 < ran_end)
2285 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
2287 cursor.key_end.key = ran_end + MAXPHYS + 1;
2289 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
2291 error = hammer_ip_first(&cursor);
2292 base_offset = last_offset = 0;
2293 base_disk_offset = last_disk_offset = 0;
2295 while (error == 0) {
2297 * Get the base file offset of the record. The key for
2298 * data records is (base + bytes) rather then (base).
2300 * NOTE: rec_offset + rec_len may exceed the end-of-file.
2301 * The extra bytes should be zero on-disk and the BMAP op
2302 * should still be ok.
2304 base = &cursor.leaf->base;
2305 rec_offset = base->key - cursor.leaf->data_len;
2306 rec_len = cursor.leaf->data_len;
2309 * Incorporate any cached truncation.
2311 * NOTE: Modifications to rec_len based on synthesized
2312 * truncation points remove the guarantee that any extended
2313 * data on disk is zero (since the truncations may not have
2314 * taken place on-media yet).
2316 if (ip->flags & HAMMER_INODE_TRUNCATED) {
2317 if (hammer_cursor_ondisk(&cursor) ||
2318 cursor.iprec->flush_state == HAMMER_FST_FLUSH) {
2319 if (ip->trunc_off <= rec_offset)
2321 else if (ip->trunc_off < rec_offset + rec_len)
2322 rec_len = (int)(ip->trunc_off - rec_offset);
2325 if (ip->sync_flags & HAMMER_INODE_TRUNCATED) {
2326 if (hammer_cursor_ondisk(&cursor)) {
2327 if (ip->sync_trunc_off <= rec_offset)
2329 else if (ip->sync_trunc_off < rec_offset + rec_len)
2330 rec_len = (int)(ip->sync_trunc_off - rec_offset);
2335 * Accumulate information. If we have hit a discontiguous
2336 * block reset base_offset unless we are already beyond the
2337 * requested offset. If we are, that's it, we stop.
2339 disk_offset = hammer_blockmap_lookup(trans.hmp,
2340 cursor.leaf->data_offset,
2344 if (rec_offset != last_offset ||
2345 disk_offset != last_disk_offset) {
2346 if (rec_offset > ap->a_loffset)
2348 base_offset = rec_offset;
2349 base_disk_offset = disk_offset;
2351 last_offset = rec_offset + rec_len;
2352 last_disk_offset = disk_offset + rec_len;
2354 error = hammer_ip_next(&cursor);
2358 kprintf("BMAP %016llx: %016llx - %016llx\n",
2359 ap->a_loffset, base_offset, last_offset);
2360 kprintf("BMAP %16s: %016llx - %016llx\n",
2361 "", base_disk_offset, last_disk_offset);
2365 hammer_cache_node(&ip->cache[1], cursor.node);
2367 kprintf("bmap_end2 %016llx ip->cache %p\n", ap->a_loffset, ip->cache[1]);
2370 hammer_done_cursor(&cursor);
2371 hammer_done_transaction(&trans);
2374 * If we couldn't find any records or the records we did find were
2375 * all behind the requested offset, return failure. A forward
2376 * truncation can leave a hole w/ no on-disk records.
2378 if (last_offset == 0 || last_offset < ap->a_loffset)
2379 return (EOPNOTSUPP);
2382 * Figure out the block size at the requested offset and adjust
2383 * our limits so the cluster_read() does not create inappropriately
2384 * sized buffer cache buffers.
2386 blksize = hammer_blocksize(ap->a_loffset);
2387 if (hammer_blocksize(base_offset) != blksize) {
2388 base_offset = hammer_blockdemarc(base_offset, ap->a_loffset);
2390 if (last_offset != ap->a_loffset &&
2391 hammer_blocksize(last_offset - 1) != blksize) {
2392 last_offset = hammer_blockdemarc(ap->a_loffset,
2397 * Returning EOPNOTSUPP simply prevents the direct-IO optimization
2400 disk_offset = base_disk_offset + (ap->a_loffset - base_offset);
2403 * If doffsetp is not aligned or the forward run size does
2404 * not cover a whole buffer, disallow the direct I/O.
2406 if ((disk_offset & HAMMER_BUFMASK) ||
2407 (last_offset - ap->a_loffset) < blksize) {
2410 *ap->a_doffsetp = disk_offset;
2412 *ap->a_runb = ap->a_loffset - base_offset;
2413 KKASSERT(*ap->a_runb >= 0);
2416 *ap->a_runp = last_offset - ap->a_loffset;
2417 KKASSERT(*ap->a_runp >= 0);
2425 * Write to a regular file. Because this is a strategy call the OS is
2426 * trying to actually get data onto the media.
2430 hammer_vop_strategy_write(struct vop_strategy_args *ap)
2432 hammer_record_t record;
2442 ip = ap->a_vp->v_data;
2445 KKASSERT(bp->b_bufsize == hammer_blocksize(bio->bio_offset));
2447 if (ip->flags & HAMMER_INODE_RO) {
2448 bp->b_error = EROFS;
2449 bp->b_flags |= B_ERROR;
2451 hammer_cleanup_write_io(ip);
2456 * Interlock with inode destruction (no in-kernel or directory
2457 * topology visibility). If we queue new IO while trying to
2458 * destroy the inode we can deadlock the vtrunc call in
2459 * hammer_inode_unloadable_check().
2461 if (ip->flags & (HAMMER_INODE_DELETING|HAMMER_INODE_DELETED)) {
2464 hammer_cleanup_write_io(ip);
2469 * Reserve space and issue a direct-write from the front-end.
2470 * NOTE: The direct_io code will hammer_bread/bcopy smaller
2473 * An in-memory record will be installed to reference the storage
2474 * until the flusher can get to it.
2476 * Since we own the high level bio the front-end will not try to
2477 * do a direct-read until the write completes.
2479 * NOTE: The only time we do not reserve a full-sized buffers
2480 * worth of data is if the file is small. We do not try to
2481 * allocate a fragment (from the small-data zone) at the end of
2482 * an otherwise large file as this can lead to wildly separated
2485 KKASSERT((bio->bio_offset & HAMMER_BUFMASK) == 0);
2486 KKASSERT(bio->bio_offset < ip->ino_data.size);
2487 if (bio->bio_offset || ip->ino_data.size > HAMMER_BUFSIZE / 2)
2488 bytes = bp->b_bufsize;
2490 bytes = ((int)ip->ino_data.size + 15) & ~15;
2492 record = hammer_ip_add_bulk(ip, bio->bio_offset, bp->b_data,
2495 hammer_io_direct_write(hmp, &record->leaf, bio);
2496 hammer_rel_mem_record(record);
2497 if (ip->rsv_recs > 1 && hmp->rsv_recs > hammer_limit_recs)
2498 hammer_flush_inode(ip, 0);
2500 bp->b_bio2.bio_offset = NOOFFSET;
2501 bp->b_error = error;
2502 bp->b_flags |= B_ERROR;
2505 hammer_cleanup_write_io(ip);
2510 * Clean-up after disposing of a dirty frontend buffer's data.
2511 * This is somewhat heuristical so try to be robust.
2514 hammer_cleanup_write_io(hammer_inode_t ip)
2516 if (ip->rsv_databufs) {
2518 --ip->hmp->rsv_databufs;
2523 * We can lose track of dirty buffer cache buffers if we truncate, this
2524 * routine will resynchronize the count.
2528 hammer_update_rsv_databufs(hammer_inode_t ip)
2536 RB_FOREACH(bp, buf_rb_tree, &ip->vp->v_rbdirty_tree) {
2542 delta = n - ip->rsv_databufs;
2543 ip->rsv_databufs += delta;
2544 ip->hmp->rsv_databufs += delta;
2548 * dounlink - disconnect a directory entry
2550 * XXX whiteout support not really in yet
2553 hammer_dounlink(hammer_transaction_t trans, struct nchandle *nch,
2554 struct vnode *dvp, struct ucred *cred, int flags)
2556 struct namecache *ncp;
2559 struct hammer_cursor cursor;
2564 * Calculate the namekey and setup the key range for the scan. This
2565 * works kinda like a chained hash table where the lower 32 bits
2566 * of the namekey synthesize the chain.
2568 * The key range is inclusive of both key_beg and key_end.
2573 if (dip->flags & HAMMER_INODE_RO)
2576 namekey = hammer_directory_namekey(ncp->nc_name, ncp->nc_nlen);
2578 hammer_init_cursor(trans, &cursor, &dip->cache[1], dip);
2579 cursor.key_beg.localization = dip->obj_localization +
2580 HAMMER_LOCALIZE_MISC;
2581 cursor.key_beg.obj_id = dip->obj_id;
2582 cursor.key_beg.key = namekey;
2583 cursor.key_beg.create_tid = 0;
2584 cursor.key_beg.delete_tid = 0;
2585 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
2586 cursor.key_beg.obj_type = 0;
2588 cursor.key_end = cursor.key_beg;
2589 cursor.key_end.key |= 0xFFFFFFFFULL;
2590 cursor.asof = dip->obj_asof;
2591 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
2594 * Scan all matching records (the chain), locate the one matching
2595 * the requested path component. info->last_error contains the
2596 * error code on search termination and could be 0, ENOENT, or
2599 * The hammer_ip_*() functions merge in-memory records with on-disk
2600 * records for the purposes of the search.
2602 error = hammer_ip_first(&cursor);
2604 while (error == 0) {
2605 error = hammer_ip_resolve_data(&cursor);
2608 nlen = cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF;
2610 if (ncp->nc_nlen == nlen &&
2611 bcmp(ncp->nc_name, cursor.data->entry.name, nlen) == 0) {
2614 error = hammer_ip_next(&cursor);
2618 * If all is ok we have to get the inode so we can adjust nlinks.
2619 * To avoid a deadlock with the flusher we must release the inode
2620 * lock on the directory when acquiring the inode for the entry.
2622 * If the target is a directory, it must be empty.
2625 hammer_unlock(&cursor.ip->lock);
2626 ip = hammer_get_inode(trans, dip, cursor.data->entry.obj_id,
2628 cursor.data->entry.localization,
2630 hammer_lock_sh(&cursor.ip->lock);
2631 if (error == ENOENT) {
2632 kprintf("obj_id %016llx\n", cursor.data->entry.obj_id);
2633 Debugger("ENOENT unlinking object that should exist");
2637 * If we are trying to remove a directory the directory must
2640 * WARNING: hammer_ip_check_directory_empty() may have to
2641 * terminate the cursor to avoid a deadlock. It is ok to
2642 * call hammer_done_cursor() twice.
2644 if (error == 0 && ip->ino_data.obj_type ==
2645 HAMMER_OBJTYPE_DIRECTORY) {
2646 error = hammer_ip_check_directory_empty(trans, ip);
2650 * Delete the directory entry.
2652 * WARNING: hammer_ip_del_directory() may have to terminate
2653 * the cursor to avoid a deadlock. It is ok to call
2654 * hammer_done_cursor() twice.
2657 error = hammer_ip_del_directory(trans, &cursor,
2660 hammer_done_cursor(&cursor);
2662 cache_setunresolved(nch);
2663 cache_setvp(nch, NULL);
2666 cache_inval_vp(ip->vp, CINV_DESTROY);
2669 hammer_rel_inode(ip, 0);
2671 hammer_done_cursor(&cursor);
2673 if (error == EDEADLK)
2679 /************************************************************************
2680 * FIFO AND SPECFS OPS *
2681 ************************************************************************
2686 hammer_vop_fifoclose (struct vop_close_args *ap)
2688 /* XXX update itimes */
2689 return (VOCALL(&fifo_vnode_vops, &ap->a_head));
2693 hammer_vop_fiforead (struct vop_read_args *ap)
2697 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
2698 /* XXX update access time */
2703 hammer_vop_fifowrite (struct vop_write_args *ap)
2707 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
2708 /* XXX update access time */
2713 hammer_vop_specclose (struct vop_close_args *ap)
2715 /* XXX update itimes */
2716 return (VOCALL(&spec_vnode_vops, &ap->a_head));
2720 hammer_vop_specread (struct vop_read_args *ap)
2722 /* XXX update access time */
2723 return (VOCALL(&spec_vnode_vops, &ap->a_head));
2727 hammer_vop_specwrite (struct vop_write_args *ap)
2729 /* XXX update last change time */
2730 return (VOCALL(&spec_vnode_vops, &ap->a_head));