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.102 2008/10/16 17:24:16 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>
48 #include <vm/vm_extern.h>
49 #include <vfs/fifofs/fifo.h>
55 /*static int hammer_vop_vnoperate(struct vop_generic_args *);*/
56 static int hammer_vop_fsync(struct vop_fsync_args *);
57 static int hammer_vop_read(struct vop_read_args *);
58 static int hammer_vop_write(struct vop_write_args *);
59 static int hammer_vop_access(struct vop_access_args *);
60 static int hammer_vop_advlock(struct vop_advlock_args *);
61 static int hammer_vop_close(struct vop_close_args *);
62 static int hammer_vop_ncreate(struct vop_ncreate_args *);
63 static int hammer_vop_getattr(struct vop_getattr_args *);
64 static int hammer_vop_nresolve(struct vop_nresolve_args *);
65 static int hammer_vop_nlookupdotdot(struct vop_nlookupdotdot_args *);
66 static int hammer_vop_nlink(struct vop_nlink_args *);
67 static int hammer_vop_nmkdir(struct vop_nmkdir_args *);
68 static int hammer_vop_nmknod(struct vop_nmknod_args *);
69 static int hammer_vop_open(struct vop_open_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_markatime(struct vop_markatime_args *);
77 static int hammer_vop_setattr(struct vop_setattr_args *);
78 static int hammer_vop_strategy(struct vop_strategy_args *);
79 static int hammer_vop_bmap(struct vop_bmap_args *ap);
80 static int hammer_vop_nsymlink(struct vop_nsymlink_args *);
81 static int hammer_vop_nwhiteout(struct vop_nwhiteout_args *);
82 static int hammer_vop_ioctl(struct vop_ioctl_args *);
83 static int hammer_vop_mountctl(struct vop_mountctl_args *);
84 static int hammer_vop_kqfilter (struct vop_kqfilter_args *);
86 static int hammer_vop_fifoclose (struct vop_close_args *);
87 static int hammer_vop_fiforead (struct vop_read_args *);
88 static int hammer_vop_fifowrite (struct vop_write_args *);
89 static int hammer_vop_fifokqfilter (struct vop_kqfilter_args *);
91 static int hammer_vop_specclose (struct vop_close_args *);
92 static int hammer_vop_specread (struct vop_read_args *);
93 static int hammer_vop_specwrite (struct vop_write_args *);
94 static int hammer_vop_specgetattr (struct vop_getattr_args *);
96 struct vop_ops hammer_vnode_vops = {
97 .vop_default = vop_defaultop,
98 .vop_fsync = hammer_vop_fsync,
99 .vop_getpages = vop_stdgetpages,
100 .vop_putpages = vop_stdputpages,
101 .vop_read = hammer_vop_read,
102 .vop_write = hammer_vop_write,
103 .vop_access = hammer_vop_access,
104 .vop_advlock = hammer_vop_advlock,
105 .vop_close = hammer_vop_close,
106 .vop_ncreate = hammer_vop_ncreate,
107 .vop_getattr = hammer_vop_getattr,
108 .vop_inactive = hammer_vop_inactive,
109 .vop_reclaim = hammer_vop_reclaim,
110 .vop_nresolve = hammer_vop_nresolve,
111 .vop_nlookupdotdot = hammer_vop_nlookupdotdot,
112 .vop_nlink = hammer_vop_nlink,
113 .vop_nmkdir = hammer_vop_nmkdir,
114 .vop_nmknod = hammer_vop_nmknod,
115 .vop_open = hammer_vop_open,
116 .vop_pathconf = vop_stdpathconf,
117 .vop_print = hammer_vop_print,
118 .vop_readdir = hammer_vop_readdir,
119 .vop_readlink = hammer_vop_readlink,
120 .vop_nremove = hammer_vop_nremove,
121 .vop_nrename = hammer_vop_nrename,
122 .vop_nrmdir = hammer_vop_nrmdir,
123 .vop_markatime = hammer_vop_markatime,
124 .vop_setattr = hammer_vop_setattr,
125 .vop_bmap = hammer_vop_bmap,
126 .vop_strategy = hammer_vop_strategy,
127 .vop_nsymlink = hammer_vop_nsymlink,
128 .vop_nwhiteout = hammer_vop_nwhiteout,
129 .vop_ioctl = hammer_vop_ioctl,
130 .vop_mountctl = hammer_vop_mountctl,
131 .vop_kqfilter = hammer_vop_kqfilter
134 struct vop_ops hammer_spec_vops = {
135 .vop_default = spec_vnoperate,
136 .vop_fsync = hammer_vop_fsync,
137 .vop_read = hammer_vop_specread,
138 .vop_write = hammer_vop_specwrite,
139 .vop_access = hammer_vop_access,
140 .vop_close = hammer_vop_specclose,
141 .vop_markatime = hammer_vop_markatime,
142 .vop_getattr = hammer_vop_specgetattr,
143 .vop_inactive = hammer_vop_inactive,
144 .vop_reclaim = hammer_vop_reclaim,
145 .vop_setattr = hammer_vop_setattr
148 struct vop_ops hammer_fifo_vops = {
149 .vop_default = fifo_vnoperate,
150 .vop_fsync = hammer_vop_fsync,
151 .vop_read = hammer_vop_fiforead,
152 .vop_write = hammer_vop_fifowrite,
153 .vop_access = hammer_vop_access,
154 .vop_close = hammer_vop_fifoclose,
155 .vop_markatime = hammer_vop_markatime,
156 .vop_getattr = hammer_vop_getattr,
157 .vop_inactive = hammer_vop_inactive,
158 .vop_reclaim = hammer_vop_reclaim,
159 .vop_setattr = hammer_vop_setattr,
160 .vop_kqfilter = hammer_vop_fifokqfilter
165 hammer_knote(struct vnode *vp, int flags)
168 KNOTE(&vp->v_pollinfo.vpi_selinfo.si_note, flags);
171 #ifdef DEBUG_TRUNCATE
172 struct hammer_inode *HammerTruncIp;
175 static int hammer_dounlink(hammer_transaction_t trans, struct nchandle *nch,
176 struct vnode *dvp, struct ucred *cred,
177 int flags, int isdir);
178 static int hammer_vop_strategy_read(struct vop_strategy_args *ap);
179 static int hammer_vop_strategy_write(struct vop_strategy_args *ap);
184 hammer_vop_vnoperate(struct vop_generic_args *)
186 return (VOCALL(&hammer_vnode_vops, ap));
191 * hammer_vop_fsync { vp, waitfor }
193 * fsync() an inode to disk and wait for it to be completely committed
194 * such that the information would not be undone if a crash occured after
199 hammer_vop_fsync(struct vop_fsync_args *ap)
201 hammer_inode_t ip = VTOI(ap->a_vp);
203 ++hammer_count_fsyncs;
204 vfsync(ap->a_vp, ap->a_waitfor, 1, NULL, NULL);
205 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
206 if (ap->a_waitfor == MNT_WAIT) {
208 hammer_wait_inode(ip);
209 vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY);
215 * hammer_vop_read { vp, uio, ioflag, cred }
219 hammer_vop_read(struct vop_read_args *ap)
221 struct hammer_transaction trans;
232 if (ap->a_vp->v_type != VREG)
239 * Allow the UIO's size to override the sequential heuristic.
241 blksize = hammer_blocksize(uio->uio_offset);
242 seqcount = (uio->uio_resid + (blksize - 1)) / blksize;
243 ioseqcount = ap->a_ioflag >> 16;
244 if (seqcount < ioseqcount)
245 seqcount = ioseqcount;
247 hammer_start_transaction(&trans, ip->hmp);
250 * Access the data typically in HAMMER_BUFSIZE blocks via the
251 * buffer cache, but HAMMER may use a variable block size based
254 while (uio->uio_resid > 0 && uio->uio_offset < ip->ino_data.size) {
258 blksize = hammer_blocksize(uio->uio_offset);
259 offset = (int)uio->uio_offset & (blksize - 1);
260 base_offset = uio->uio_offset - offset;
262 if (hammer_cluster_enable) {
264 * Use file_limit to prevent cluster_read() from
265 * creating buffers of the wrong block size past
268 file_limit = ip->ino_data.size;
269 if (base_offset < HAMMER_XDEMARC &&
270 file_limit > HAMMER_XDEMARC) {
271 file_limit = HAMMER_XDEMARC;
273 error = cluster_read(ap->a_vp,
274 file_limit, base_offset,
278 error = bread(ap->a_vp, base_offset, blksize, &bp);
281 kprintf("error %d\n", error);
286 /* bp->b_flags |= B_CLUSTEROK; temporarily disabled */
287 n = blksize - offset;
288 if (n > uio->uio_resid)
290 if (n > ip->ino_data.size - uio->uio_offset)
291 n = (int)(ip->ino_data.size - uio->uio_offset);
292 error = uiomove((char *)bp->b_data + offset, n, uio);
294 /* data has a lower priority then meta-data */
295 bp->b_flags |= B_AGE;
299 hammer_stats_file_read += n;
301 if ((ip->flags & HAMMER_INODE_RO) == 0 &&
302 (ip->hmp->mp->mnt_flag & MNT_NOATIME) == 0) {
303 ip->ino_data.atime = trans.time;
304 hammer_modify_inode(ip, HAMMER_INODE_ATIME);
306 hammer_done_transaction(&trans);
311 * hammer_vop_write { vp, uio, ioflag, cred }
315 hammer_vop_write(struct vop_write_args *ap)
317 struct hammer_transaction trans;
318 struct hammer_inode *ip;
331 if (ap->a_vp->v_type != VREG)
337 seqcount = ap->a_ioflag >> 16;
339 if (ip->flags & HAMMER_INODE_RO)
343 * Create a transaction to cover the operations we perform.
345 hammer_start_transaction(&trans, hmp);
351 if (ap->a_ioflag & IO_APPEND)
352 uio->uio_offset = ip->ino_data.size;
355 * Check for illegal write offsets. Valid range is 0...2^63-1.
357 * NOTE: the base_off assignment is required to work around what
358 * I consider to be a GCC-4 optimization bug.
360 if (uio->uio_offset < 0) {
361 hammer_done_transaction(&trans);
364 base_offset = uio->uio_offset + uio->uio_resid; /* work around gcc-4 */
365 if (uio->uio_resid > 0 && base_offset <= 0) {
366 hammer_done_transaction(&trans);
371 * Access the data typically in HAMMER_BUFSIZE blocks via the
372 * buffer cache, but HAMMER may use a variable block size based
375 while (uio->uio_resid > 0) {
380 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_WRITE)) != 0)
383 blksize = hammer_blocksize(uio->uio_offset);
386 * Do not allow HAMMER to blow out the buffer cache. Very
387 * large UIOs can lockout other processes due to bwillwrite()
390 * The hammer inode is not locked during these operations.
391 * The vnode is locked which can interfere with the pageout
392 * daemon for non-UIO_NOCOPY writes but should not interfere
393 * with the buffer cache. Even so, we cannot afford to
394 * allow the pageout daemon to build up too many dirty buffer
397 * Only call this if we aren't being recursively called from
398 * a virtual disk device (vn), else we may deadlock.
400 if ((ap->a_ioflag & IO_RECURSE) == 0)
404 * Do not allow HAMMER to blow out system memory by
405 * accumulating too many records. Records are so well
406 * decoupled from the buffer cache that it is possible
407 * for userland to push data out to the media via
408 * direct-write, but build up the records queued to the
409 * backend faster then the backend can flush them out.
410 * HAMMER has hit its write limit but the frontend has
411 * no pushback to slow it down.
413 if (hmp->rsv_recs > hammer_limit_recs / 2) {
415 * Get the inode on the flush list
417 if (ip->rsv_recs >= 64)
418 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
419 else if (ip->rsv_recs >= 16)
420 hammer_flush_inode(ip, 0);
423 * Keep the flusher going if the system keeps
426 delta = hmp->count_newrecords -
427 hmp->last_newrecords;
428 if (delta < 0 || delta > hammer_limit_recs / 2) {
429 hmp->last_newrecords = hmp->count_newrecords;
430 hammer_sync_hmp(hmp, MNT_NOWAIT);
434 * If we have gotten behind start slowing
437 delta = (hmp->rsv_recs - hammer_limit_recs) *
438 hz / hammer_limit_recs;
440 tsleep(&trans, 0, "hmrslo", delta);
444 * Calculate the blocksize at the current offset and figure
445 * out how much we can actually write.
447 blkmask = blksize - 1;
448 offset = (int)uio->uio_offset & blkmask;
449 base_offset = uio->uio_offset & ~(int64_t)blkmask;
450 n = blksize - offset;
451 if (n > uio->uio_resid)
453 if (uio->uio_offset + n > ip->ino_data.size) {
454 vnode_pager_setsize(ap->a_vp, uio->uio_offset + n);
456 kflags |= NOTE_EXTEND;
459 if (uio->uio_segflg == UIO_NOCOPY) {
461 * Issuing a write with the same data backing the
462 * buffer. Instantiate the buffer to collect the
463 * backing vm pages, then read-in any missing bits.
465 * This case is used by vop_stdputpages().
467 bp = getblk(ap->a_vp, base_offset,
468 blksize, GETBLK_BHEAVY, 0);
469 if ((bp->b_flags & B_CACHE) == 0) {
471 error = bread(ap->a_vp, base_offset,
474 } else if (offset == 0 && uio->uio_resid >= blksize) {
476 * Even though we are entirely overwriting the buffer
477 * we may still have to zero it out to avoid a
478 * mmap/write visibility issue.
480 bp = getblk(ap->a_vp, base_offset, blksize, GETBLK_BHEAVY, 0);
481 if ((bp->b_flags & B_CACHE) == 0)
483 } else if (base_offset >= ip->ino_data.size) {
485 * If the base offset of the buffer is beyond the
486 * file EOF, we don't have to issue a read.
488 bp = getblk(ap->a_vp, base_offset,
489 blksize, GETBLK_BHEAVY, 0);
493 * Partial overwrite, read in any missing bits then
494 * replace the portion being written.
496 error = bread(ap->a_vp, base_offset, blksize, &bp);
501 error = uiomove((char *)bp->b_data + offset,
506 * If we screwed up we have to undo any VM size changes we
512 vtruncbuf(ap->a_vp, ip->ino_data.size,
513 hammer_blocksize(ip->ino_data.size));
517 kflags |= NOTE_WRITE;
518 hammer_stats_file_write += n;
519 /* bp->b_flags |= B_CLUSTEROK; temporarily disabled */
520 if (ip->ino_data.size < uio->uio_offset) {
521 ip->ino_data.size = uio->uio_offset;
522 flags = HAMMER_INODE_DDIRTY;
523 vnode_pager_setsize(ap->a_vp, ip->ino_data.size);
527 ip->ino_data.mtime = trans.time;
528 flags |= HAMMER_INODE_MTIME | HAMMER_INODE_BUFS;
529 hammer_modify_inode(ip, flags);
532 * Once we dirty the buffer any cached zone-X offset
533 * becomes invalid. HAMMER NOTE: no-history mode cannot
534 * allow overwriting over the same data sector unless
535 * we provide UNDOs for the old data, which we don't.
537 bp->b_bio2.bio_offset = NOOFFSET;
540 * Final buffer disposition.
542 bp->b_flags |= B_AGE;
543 if (ap->a_ioflag & IO_SYNC) {
545 } else if (ap->a_ioflag & IO_DIRECT) {
551 hammer_done_transaction(&trans);
552 hammer_knote(ap->a_vp, kflags);
557 * hammer_vop_access { vp, mode, cred }
561 hammer_vop_access(struct vop_access_args *ap)
563 struct hammer_inode *ip = VTOI(ap->a_vp);
568 ++hammer_stats_file_iopsr;
569 uid = hammer_to_unix_xid(&ip->ino_data.uid);
570 gid = hammer_to_unix_xid(&ip->ino_data.gid);
572 error = vop_helper_access(ap, uid, gid, ip->ino_data.mode,
573 ip->ino_data.uflags);
578 * hammer_vop_advlock { vp, id, op, fl, flags }
582 hammer_vop_advlock(struct vop_advlock_args *ap)
584 hammer_inode_t ip = VTOI(ap->a_vp);
586 return (lf_advlock(ap, &ip->advlock, ip->ino_data.size));
590 * hammer_vop_close { vp, fflag }
594 hammer_vop_close(struct vop_close_args *ap)
596 /*hammer_inode_t ip = VTOI(ap->a_vp);*/
597 return (vop_stdclose(ap));
601 * hammer_vop_ncreate { nch, dvp, vpp, cred, vap }
603 * The operating system has already ensured that the directory entry
604 * does not exist and done all appropriate namespace locking.
608 hammer_vop_ncreate(struct vop_ncreate_args *ap)
610 struct hammer_transaction trans;
611 struct hammer_inode *dip;
612 struct hammer_inode *nip;
613 struct nchandle *nch;
617 dip = VTOI(ap->a_dvp);
619 if (dip->flags & HAMMER_INODE_RO)
621 if ((error = hammer_checkspace(dip->hmp, HAMMER_CHKSPC_CREATE)) != 0)
625 * Create a transaction to cover the operations we perform.
627 hammer_start_transaction(&trans, dip->hmp);
628 ++hammer_stats_file_iopsw;
631 * Create a new filesystem object of the requested type. The
632 * returned inode will be referenced and shared-locked to prevent
633 * it from being moved to the flusher.
635 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
636 dip, nch->ncp->nc_name, nch->ncp->nc_nlen,
639 hkprintf("hammer_create_inode error %d\n", error);
640 hammer_done_transaction(&trans);
646 * Add the new filesystem object to the directory. This will also
647 * bump the inode's link count.
649 error = hammer_ip_add_directory(&trans, dip,
650 nch->ncp->nc_name, nch->ncp->nc_nlen,
653 hkprintf("hammer_ip_add_directory error %d\n", error);
659 hammer_rel_inode(nip, 0);
660 hammer_done_transaction(&trans);
663 error = hammer_get_vnode(nip, ap->a_vpp);
664 hammer_done_transaction(&trans);
665 hammer_rel_inode(nip, 0);
667 cache_setunresolved(ap->a_nch);
668 cache_setvp(ap->a_nch, *ap->a_vpp);
670 hammer_knote(ap->a_dvp, NOTE_WRITE);
676 * hammer_vop_getattr { vp, vap }
678 * Retrieve an inode's attribute information. When accessing inodes
679 * historically we fake the atime field to ensure consistent results.
680 * The atime field is stored in the B-Tree element and allowed to be
681 * updated without cycling the element.
685 hammer_vop_getattr(struct vop_getattr_args *ap)
687 struct hammer_inode *ip = VTOI(ap->a_vp);
688 struct vattr *vap = ap->a_vap;
691 * We want the fsid to be different when accessing a filesystem
692 * with different as-of's so programs like diff don't think
693 * the files are the same.
695 * We also want the fsid to be the same when comparing snapshots,
696 * or when comparing mirrors (which might be backed by different
697 * physical devices). HAMMER fsids are based on the PFS's
700 * XXX there is a chance of collision here. The va_fsid reported
701 * by stat is different from the more involved fsid used in the
704 ++hammer_stats_file_iopsr;
705 vap->va_fsid = ip->pfsm->fsid_udev ^ (u_int32_t)ip->obj_asof ^
706 (u_int32_t)(ip->obj_asof >> 32);
708 vap->va_fileid = ip->ino_leaf.base.obj_id;
709 vap->va_mode = ip->ino_data.mode;
710 vap->va_nlink = ip->ino_data.nlinks;
711 vap->va_uid = hammer_to_unix_xid(&ip->ino_data.uid);
712 vap->va_gid = hammer_to_unix_xid(&ip->ino_data.gid);
715 vap->va_size = ip->ino_data.size;
718 * Special case for @@PFS softlinks. The actual size of the
719 * expanded softlink is "@@0x%016llx:%05d" == 26 bytes.
720 * or for MAX_TID is "@@-1:%05d" == 10 bytes.
722 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_SOFTLINK &&
723 ip->ino_data.size == 10 &&
724 ip->obj_asof == HAMMER_MAX_TID &&
725 ip->obj_localization == 0 &&
726 strncmp(ip->ino_data.ext.symlink, "@@PFS", 5) == 0) {
727 if (ip->pfsm->pfsd.mirror_flags & HAMMER_PFSD_SLAVE)
734 * We must provide a consistent atime and mtime for snapshots
735 * so people can do a 'tar cf - ... | md5' on them and get
736 * consistent results.
738 if (ip->flags & HAMMER_INODE_RO) {
739 hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_atime);
740 hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_mtime);
742 hammer_time_to_timespec(ip->ino_data.atime, &vap->va_atime);
743 hammer_time_to_timespec(ip->ino_data.mtime, &vap->va_mtime);
745 hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_ctime);
746 vap->va_flags = ip->ino_data.uflags;
747 vap->va_gen = 1; /* hammer inums are unique for all time */
748 vap->va_blocksize = HAMMER_BUFSIZE;
749 if (ip->ino_data.size >= HAMMER_XDEMARC) {
750 vap->va_bytes = (ip->ino_data.size + HAMMER_XBUFMASK64) &
752 } else if (ip->ino_data.size > HAMMER_BUFSIZE / 2) {
753 vap->va_bytes = (ip->ino_data.size + HAMMER_BUFMASK64) &
756 vap->va_bytes = (ip->ino_data.size + 15) & ~15;
759 vap->va_type = hammer_get_vnode_type(ip->ino_data.obj_type);
760 vap->va_filerev = 0; /* XXX */
761 /* mtime uniquely identifies any adjustments made to the file XXX */
762 vap->va_fsmid = ip->ino_data.mtime;
763 vap->va_uid_uuid = ip->ino_data.uid;
764 vap->va_gid_uuid = ip->ino_data.gid;
765 vap->va_fsid_uuid = ip->hmp->fsid;
766 vap->va_vaflags = VA_UID_UUID_VALID | VA_GID_UUID_VALID |
769 switch (ip->ino_data.obj_type) {
770 case HAMMER_OBJTYPE_CDEV:
771 case HAMMER_OBJTYPE_BDEV:
772 vap->va_rmajor = ip->ino_data.rmajor;
773 vap->va_rminor = ip->ino_data.rminor;
782 * hammer_vop_nresolve { nch, dvp, cred }
784 * Locate the requested directory entry.
788 hammer_vop_nresolve(struct vop_nresolve_args *ap)
790 struct hammer_transaction trans;
791 struct namecache *ncp;
795 struct hammer_cursor cursor;
804 u_int32_t localization;
805 u_int32_t max_iterations;
808 * Misc initialization, plus handle as-of name extensions. Look for
809 * the '@@' extension. Note that as-of files and directories cannot
812 dip = VTOI(ap->a_dvp);
813 ncp = ap->a_nch->ncp;
814 asof = dip->obj_asof;
815 localization = dip->obj_localization; /* for code consistency */
817 flags = dip->flags & HAMMER_INODE_RO;
820 hammer_simple_transaction(&trans, dip->hmp);
821 ++hammer_stats_file_iopsr;
823 for (i = 0; i < nlen; ++i) {
824 if (ncp->nc_name[i] == '@' && ncp->nc_name[i+1] == '@') {
825 error = hammer_str_to_tid(ncp->nc_name + i + 2,
826 &ispfs, &asof, &localization);
831 if (asof != HAMMER_MAX_TID)
832 flags |= HAMMER_INODE_RO;
839 * If this is a PFS softlink we dive into the PFS
841 if (ispfs && nlen == 0) {
842 ip = hammer_get_inode(&trans, dip, HAMMER_OBJID_ROOT,
846 error = hammer_get_vnode(ip, &vp);
847 hammer_rel_inode(ip, 0);
853 cache_setvp(ap->a_nch, vp);
860 * If there is no path component the time extension is relative to dip.
861 * e.g. "fubar/@@<snapshot>"
863 * "." is handled by the kernel, but ".@@<snapshot>" is not.
864 * e.g. "fubar/.@@<snapshot>"
866 * ".." is handled by the kernel. We do not currently handle
869 if (nlen == 0 || (nlen == 1 && ncp->nc_name[0] == '.')) {
870 ip = hammer_get_inode(&trans, dip, dip->obj_id,
871 asof, dip->obj_localization,
874 error = hammer_get_vnode(ip, &vp);
875 hammer_rel_inode(ip, 0);
881 cache_setvp(ap->a_nch, vp);
888 * Calculate the namekey and setup the key range for the scan. This
889 * works kinda like a chained hash table where the lower 32 bits
890 * of the namekey synthesize the chain.
892 * The key range is inclusive of both key_beg and key_end.
894 namekey = hammer_directory_namekey(dip, ncp->nc_name, nlen,
897 error = hammer_init_cursor(&trans, &cursor, &dip->cache[1], dip);
898 cursor.key_beg.localization = dip->obj_localization +
899 HAMMER_LOCALIZE_MISC;
900 cursor.key_beg.obj_id = dip->obj_id;
901 cursor.key_beg.key = namekey;
902 cursor.key_beg.create_tid = 0;
903 cursor.key_beg.delete_tid = 0;
904 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
905 cursor.key_beg.obj_type = 0;
907 cursor.key_end = cursor.key_beg;
908 cursor.key_end.key += max_iterations;
910 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
913 * Scan all matching records (the chain), locate the one matching
914 * the requested path component.
916 * The hammer_ip_*() functions merge in-memory records with on-disk
917 * records for the purposes of the search.
920 localization = HAMMER_DEF_LOCALIZATION;
923 error = hammer_ip_first(&cursor);
925 error = hammer_ip_resolve_data(&cursor);
928 if (nlen == cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF &&
929 bcmp(ncp->nc_name, cursor.data->entry.name, nlen) == 0) {
930 obj_id = cursor.data->entry.obj_id;
931 localization = cursor.data->entry.localization;
934 error = hammer_ip_next(&cursor);
937 hammer_done_cursor(&cursor);
940 * Lookup the obj_id. This should always succeed. If it does not
941 * the filesystem may be damaged and we return a dummy inode.
944 ip = hammer_get_inode(&trans, dip, obj_id,
947 if (error == ENOENT) {
948 kprintf("HAMMER: WARNING: Missing "
949 "inode for dirent \"%s\"\n"
950 "\tobj_id = %016llx\n",
951 ncp->nc_name, (long long)obj_id);
953 ip = hammer_get_dummy_inode(&trans, dip, obj_id,
958 error = hammer_get_vnode(ip, &vp);
959 hammer_rel_inode(ip, 0);
965 cache_setvp(ap->a_nch, vp);
968 } else if (error == ENOENT) {
969 cache_setvp(ap->a_nch, NULL);
972 hammer_done_transaction(&trans);
977 * hammer_vop_nlookupdotdot { dvp, vpp, cred }
979 * Locate the parent directory of a directory vnode.
981 * dvp is referenced but not locked. *vpp must be returned referenced and
982 * locked. A parent_obj_id of 0 does not necessarily indicate that we are
983 * at the root, instead it could indicate that the directory we were in was
986 * NOTE: as-of sequences are not linked into the directory structure. If
987 * we are at the root with a different asof then the mount point, reload
988 * the same directory with the mount point's asof. I'm not sure what this
989 * will do to NFS. We encode ASOF stamps in NFS file handles so it might not
990 * get confused, but it hasn't been tested.
994 hammer_vop_nlookupdotdot(struct vop_nlookupdotdot_args *ap)
996 struct hammer_transaction trans;
997 struct hammer_inode *dip;
998 struct hammer_inode *ip;
999 int64_t parent_obj_id;
1000 u_int32_t parent_obj_localization;
1004 dip = VTOI(ap->a_dvp);
1005 asof = dip->obj_asof;
1008 * Whos are parent? This could be the root of a pseudo-filesystem
1009 * whos parent is in another localization domain.
1011 parent_obj_id = dip->ino_data.parent_obj_id;
1012 if (dip->obj_id == HAMMER_OBJID_ROOT)
1013 parent_obj_localization = dip->ino_data.ext.obj.parent_obj_localization;
1015 parent_obj_localization = dip->obj_localization;
1017 if (parent_obj_id == 0) {
1018 if (dip->obj_id == HAMMER_OBJID_ROOT &&
1019 asof != dip->hmp->asof) {
1020 parent_obj_id = dip->obj_id;
1021 asof = dip->hmp->asof;
1022 *ap->a_fakename = kmalloc(19, M_TEMP, M_WAITOK);
1023 ksnprintf(*ap->a_fakename, 19, "0x%016llx",
1031 hammer_simple_transaction(&trans, dip->hmp);
1032 ++hammer_stats_file_iopsr;
1034 ip = hammer_get_inode(&trans, dip, parent_obj_id,
1035 asof, parent_obj_localization,
1036 dip->flags, &error);
1038 error = hammer_get_vnode(ip, ap->a_vpp);
1039 hammer_rel_inode(ip, 0);
1043 hammer_done_transaction(&trans);
1048 * hammer_vop_nlink { nch, dvp, vp, cred }
1052 hammer_vop_nlink(struct vop_nlink_args *ap)
1054 struct hammer_transaction trans;
1055 struct hammer_inode *dip;
1056 struct hammer_inode *ip;
1057 struct nchandle *nch;
1060 if (ap->a_dvp->v_mount != ap->a_vp->v_mount)
1064 dip = VTOI(ap->a_dvp);
1065 ip = VTOI(ap->a_vp);
1067 if (dip->obj_localization != ip->obj_localization)
1070 if (dip->flags & HAMMER_INODE_RO)
1072 if (ip->flags & HAMMER_INODE_RO)
1074 if ((error = hammer_checkspace(dip->hmp, HAMMER_CHKSPC_CREATE)) != 0)
1078 * Create a transaction to cover the operations we perform.
1080 hammer_start_transaction(&trans, dip->hmp);
1081 ++hammer_stats_file_iopsw;
1084 * Add the filesystem object to the directory. Note that neither
1085 * dip nor ip are referenced or locked, but their vnodes are
1086 * referenced. This function will bump the inode's link count.
1088 error = hammer_ip_add_directory(&trans, dip,
1089 nch->ncp->nc_name, nch->ncp->nc_nlen,
1096 cache_setunresolved(nch);
1097 cache_setvp(nch, ap->a_vp);
1099 hammer_done_transaction(&trans);
1100 hammer_knote(ap->a_vp, NOTE_LINK);
1101 hammer_knote(ap->a_dvp, NOTE_WRITE);
1106 * hammer_vop_nmkdir { nch, dvp, vpp, cred, vap }
1108 * The operating system has already ensured that the directory entry
1109 * does not exist and done all appropriate namespace locking.
1113 hammer_vop_nmkdir(struct vop_nmkdir_args *ap)
1115 struct hammer_transaction trans;
1116 struct hammer_inode *dip;
1117 struct hammer_inode *nip;
1118 struct nchandle *nch;
1122 dip = VTOI(ap->a_dvp);
1124 if (dip->flags & HAMMER_INODE_RO)
1126 if ((error = hammer_checkspace(dip->hmp, HAMMER_CHKSPC_CREATE)) != 0)
1130 * Create a transaction to cover the operations we perform.
1132 hammer_start_transaction(&trans, dip->hmp);
1133 ++hammer_stats_file_iopsw;
1136 * Create a new filesystem object of the requested type. The
1137 * returned inode will be referenced but not locked.
1139 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
1140 dip, nch->ncp->nc_name, nch->ncp->nc_nlen,
1143 hkprintf("hammer_mkdir error %d\n", error);
1144 hammer_done_transaction(&trans);
1149 * Add the new filesystem object to the directory. This will also
1150 * bump the inode's link count.
1152 error = hammer_ip_add_directory(&trans, dip,
1153 nch->ncp->nc_name, nch->ncp->nc_nlen,
1156 hkprintf("hammer_mkdir (add) error %d\n", error);
1162 hammer_rel_inode(nip, 0);
1165 error = hammer_get_vnode(nip, ap->a_vpp);
1166 hammer_rel_inode(nip, 0);
1168 cache_setunresolved(ap->a_nch);
1169 cache_setvp(ap->a_nch, *ap->a_vpp);
1172 hammer_done_transaction(&trans);
1174 hammer_knote(ap->a_dvp, NOTE_WRITE | NOTE_LINK);
1179 * hammer_vop_nmknod { nch, dvp, vpp, cred, vap }
1181 * The operating system has already ensured that the directory entry
1182 * does not exist and done all appropriate namespace locking.
1186 hammer_vop_nmknod(struct vop_nmknod_args *ap)
1188 struct hammer_transaction trans;
1189 struct hammer_inode *dip;
1190 struct hammer_inode *nip;
1191 struct nchandle *nch;
1195 dip = VTOI(ap->a_dvp);
1197 if (dip->flags & HAMMER_INODE_RO)
1199 if ((error = hammer_checkspace(dip->hmp, HAMMER_CHKSPC_CREATE)) != 0)
1203 * Create a transaction to cover the operations we perform.
1205 hammer_start_transaction(&trans, dip->hmp);
1206 ++hammer_stats_file_iopsw;
1209 * Create a new filesystem object of the requested type. The
1210 * returned inode will be referenced but not locked.
1212 * If mknod specifies a directory a pseudo-fs is created.
1214 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
1215 dip, nch->ncp->nc_name, nch->ncp->nc_nlen,
1218 hammer_done_transaction(&trans);
1224 * Add the new filesystem object to the directory. This will also
1225 * bump the inode's link count.
1227 error = hammer_ip_add_directory(&trans, dip,
1228 nch->ncp->nc_name, nch->ncp->nc_nlen,
1235 hammer_rel_inode(nip, 0);
1238 error = hammer_get_vnode(nip, ap->a_vpp);
1239 hammer_rel_inode(nip, 0);
1241 cache_setunresolved(ap->a_nch);
1242 cache_setvp(ap->a_nch, *ap->a_vpp);
1245 hammer_done_transaction(&trans);
1247 hammer_knote(ap->a_dvp, NOTE_WRITE);
1252 * hammer_vop_open { vp, mode, cred, fp }
1256 hammer_vop_open(struct vop_open_args *ap)
1260 ++hammer_stats_file_iopsr;
1261 ip = VTOI(ap->a_vp);
1263 if ((ap->a_mode & FWRITE) && (ip->flags & HAMMER_INODE_RO))
1265 return(vop_stdopen(ap));
1269 * hammer_vop_print { vp }
1273 hammer_vop_print(struct vop_print_args *ap)
1279 * hammer_vop_readdir { vp, uio, cred, *eofflag, *ncookies, off_t **cookies }
1283 hammer_vop_readdir(struct vop_readdir_args *ap)
1285 struct hammer_transaction trans;
1286 struct hammer_cursor cursor;
1287 struct hammer_inode *ip;
1289 hammer_base_elm_t base;
1298 ++hammer_stats_file_iopsr;
1299 ip = VTOI(ap->a_vp);
1301 saveoff = uio->uio_offset;
1303 if (ap->a_ncookies) {
1304 ncookies = uio->uio_resid / 16 + 1;
1305 if (ncookies > 1024)
1307 cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
1315 hammer_simple_transaction(&trans, ip->hmp);
1318 * Handle artificial entries
1320 * It should be noted that the minimum value for a directory
1321 * hash key on-media is 0x0000000100000000, so we can use anything
1322 * less then that to represent our 'special' key space.
1326 r = vop_write_dirent(&error, uio, ip->obj_id, DT_DIR, 1, ".");
1330 cookies[cookie_index] = saveoff;
1333 if (cookie_index == ncookies)
1337 if (ip->ino_data.parent_obj_id) {
1338 r = vop_write_dirent(&error, uio,
1339 ip->ino_data.parent_obj_id,
1342 r = vop_write_dirent(&error, uio,
1343 ip->obj_id, DT_DIR, 2, "..");
1348 cookies[cookie_index] = saveoff;
1351 if (cookie_index == ncookies)
1356 * Key range (begin and end inclusive) to scan. Directory keys
1357 * directly translate to a 64 bit 'seek' position.
1359 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
1360 cursor.key_beg.localization = ip->obj_localization +
1361 HAMMER_LOCALIZE_MISC;
1362 cursor.key_beg.obj_id = ip->obj_id;
1363 cursor.key_beg.create_tid = 0;
1364 cursor.key_beg.delete_tid = 0;
1365 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1366 cursor.key_beg.obj_type = 0;
1367 cursor.key_beg.key = saveoff;
1369 cursor.key_end = cursor.key_beg;
1370 cursor.key_end.key = HAMMER_MAX_KEY;
1371 cursor.asof = ip->obj_asof;
1372 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
1374 error = hammer_ip_first(&cursor);
1376 while (error == 0) {
1377 error = hammer_ip_resolve_data(&cursor);
1380 base = &cursor.leaf->base;
1381 saveoff = base->key;
1382 KKASSERT(cursor.leaf->data_len > HAMMER_ENTRY_NAME_OFF);
1384 if (base->obj_id != ip->obj_id)
1385 panic("readdir: bad record at %p", cursor.node);
1388 * Convert pseudo-filesystems into softlinks
1390 dtype = hammer_get_dtype(cursor.leaf->base.obj_type);
1391 r = vop_write_dirent(
1392 &error, uio, cursor.data->entry.obj_id,
1394 cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF ,
1395 (void *)cursor.data->entry.name);
1400 cookies[cookie_index] = base->key;
1402 if (cookie_index == ncookies)
1404 error = hammer_ip_next(&cursor);
1406 hammer_done_cursor(&cursor);
1409 hammer_done_transaction(&trans);
1412 *ap->a_eofflag = (error == ENOENT);
1413 uio->uio_offset = saveoff;
1414 if (error && cookie_index == 0) {
1415 if (error == ENOENT)
1418 kfree(cookies, M_TEMP);
1419 *ap->a_ncookies = 0;
1420 *ap->a_cookies = NULL;
1423 if (error == ENOENT)
1426 *ap->a_ncookies = cookie_index;
1427 *ap->a_cookies = cookies;
1434 * hammer_vop_readlink { vp, uio, cred }
1438 hammer_vop_readlink(struct vop_readlink_args *ap)
1440 struct hammer_transaction trans;
1441 struct hammer_cursor cursor;
1442 struct hammer_inode *ip;
1444 u_int32_t localization;
1445 hammer_pseudofs_inmem_t pfsm;
1448 ip = VTOI(ap->a_vp);
1451 * Shortcut if the symlink data was stuffed into ino_data.
1453 * Also expand special "@@PFS%05d" softlinks (expansion only
1454 * occurs for non-historical (current) accesses made from the
1455 * primary filesystem).
1457 if (ip->ino_data.size <= HAMMER_INODE_BASESYMLEN) {
1461 ptr = ip->ino_data.ext.symlink;
1462 bytes = (int)ip->ino_data.size;
1464 ip->obj_asof == HAMMER_MAX_TID &&
1465 ip->obj_localization == 0 &&
1466 strncmp(ptr, "@@PFS", 5) == 0) {
1467 hammer_simple_transaction(&trans, ip->hmp);
1468 bcopy(ptr + 5, buf, 5);
1470 localization = strtoul(buf, NULL, 10) << 16;
1471 pfsm = hammer_load_pseudofs(&trans, localization,
1474 if (pfsm->pfsd.mirror_flags &
1475 HAMMER_PFSD_SLAVE) {
1476 /* vap->va_size == 26 */
1477 ksnprintf(buf, sizeof(buf),
1479 pfsm->pfsd.sync_end_tid,
1480 localization >> 16);
1482 /* vap->va_size == 10 */
1483 ksnprintf(buf, sizeof(buf),
1485 localization >> 16);
1487 ksnprintf(buf, sizeof(buf),
1490 localization >> 16);
1494 bytes = strlen(buf);
1497 hammer_rel_pseudofs(trans.hmp, pfsm);
1498 hammer_done_transaction(&trans);
1500 error = uiomove(ptr, bytes, ap->a_uio);
1507 hammer_simple_transaction(&trans, ip->hmp);
1508 ++hammer_stats_file_iopsr;
1509 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
1512 * Key range (begin and end inclusive) to scan. Directory keys
1513 * directly translate to a 64 bit 'seek' position.
1515 cursor.key_beg.localization = ip->obj_localization +
1516 HAMMER_LOCALIZE_MISC;
1517 cursor.key_beg.obj_id = ip->obj_id;
1518 cursor.key_beg.create_tid = 0;
1519 cursor.key_beg.delete_tid = 0;
1520 cursor.key_beg.rec_type = HAMMER_RECTYPE_FIX;
1521 cursor.key_beg.obj_type = 0;
1522 cursor.key_beg.key = HAMMER_FIXKEY_SYMLINK;
1523 cursor.asof = ip->obj_asof;
1524 cursor.flags |= HAMMER_CURSOR_ASOF;
1526 error = hammer_ip_lookup(&cursor);
1528 error = hammer_ip_resolve_data(&cursor);
1530 KKASSERT(cursor.leaf->data_len >=
1531 HAMMER_SYMLINK_NAME_OFF);
1532 error = uiomove(cursor.data->symlink.name,
1533 cursor.leaf->data_len -
1534 HAMMER_SYMLINK_NAME_OFF,
1538 hammer_done_cursor(&cursor);
1539 hammer_done_transaction(&trans);
1544 * hammer_vop_nremove { nch, dvp, cred }
1548 hammer_vop_nremove(struct vop_nremove_args *ap)
1550 struct hammer_transaction trans;
1551 struct hammer_inode *dip;
1554 dip = VTOI(ap->a_dvp);
1556 if (hammer_nohistory(dip) == 0 &&
1557 (error = hammer_checkspace(dip->hmp, HAMMER_CHKSPC_REMOVE)) != 0) {
1561 hammer_start_transaction(&trans, dip->hmp);
1562 ++hammer_stats_file_iopsw;
1563 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp, ap->a_cred, 0, 0);
1564 hammer_done_transaction(&trans);
1566 hammer_knote(ap->a_dvp, NOTE_WRITE);
1571 * hammer_vop_nrename { fnch, tnch, fdvp, tdvp, cred }
1575 hammer_vop_nrename(struct vop_nrename_args *ap)
1577 struct hammer_transaction trans;
1578 struct namecache *fncp;
1579 struct namecache *tncp;
1580 struct hammer_inode *fdip;
1581 struct hammer_inode *tdip;
1582 struct hammer_inode *ip;
1583 struct hammer_cursor cursor;
1585 u_int32_t max_iterations;
1588 if (ap->a_fdvp->v_mount != ap->a_tdvp->v_mount)
1590 if (ap->a_fdvp->v_mount != ap->a_fnch->ncp->nc_vp->v_mount)
1593 fdip = VTOI(ap->a_fdvp);
1594 tdip = VTOI(ap->a_tdvp);
1595 fncp = ap->a_fnch->ncp;
1596 tncp = ap->a_tnch->ncp;
1597 ip = VTOI(fncp->nc_vp);
1598 KKASSERT(ip != NULL);
1600 if (fdip->obj_localization != tdip->obj_localization)
1602 if (fdip->obj_localization != ip->obj_localization)
1605 if (fdip->flags & HAMMER_INODE_RO)
1607 if (tdip->flags & HAMMER_INODE_RO)
1609 if (ip->flags & HAMMER_INODE_RO)
1611 if ((error = hammer_checkspace(fdip->hmp, HAMMER_CHKSPC_CREATE)) != 0)
1614 hammer_start_transaction(&trans, fdip->hmp);
1615 ++hammer_stats_file_iopsw;
1618 * Remove tncp from the target directory and then link ip as
1619 * tncp. XXX pass trans to dounlink
1621 * Force the inode sync-time to match the transaction so it is
1622 * in-sync with the creation of the target directory entry.
1624 error = hammer_dounlink(&trans, ap->a_tnch, ap->a_tdvp,
1626 if (error == 0 || error == ENOENT) {
1627 error = hammer_ip_add_directory(&trans, tdip,
1628 tncp->nc_name, tncp->nc_nlen,
1631 ip->ino_data.parent_obj_id = tdip->obj_id;
1632 ip->ino_data.ctime = trans.time;
1633 hammer_modify_inode(ip, HAMMER_INODE_DDIRTY);
1637 goto failed; /* XXX */
1640 * Locate the record in the originating directory and remove it.
1642 * Calculate the namekey and setup the key range for the scan. This
1643 * works kinda like a chained hash table where the lower 32 bits
1644 * of the namekey synthesize the chain.
1646 * The key range is inclusive of both key_beg and key_end.
1648 namekey = hammer_directory_namekey(fdip, fncp->nc_name, fncp->nc_nlen,
1651 hammer_init_cursor(&trans, &cursor, &fdip->cache[1], fdip);
1652 cursor.key_beg.localization = fdip->obj_localization +
1653 HAMMER_LOCALIZE_MISC;
1654 cursor.key_beg.obj_id = fdip->obj_id;
1655 cursor.key_beg.key = namekey;
1656 cursor.key_beg.create_tid = 0;
1657 cursor.key_beg.delete_tid = 0;
1658 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1659 cursor.key_beg.obj_type = 0;
1661 cursor.key_end = cursor.key_beg;
1662 cursor.key_end.key += max_iterations;
1663 cursor.asof = fdip->obj_asof;
1664 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
1667 * Scan all matching records (the chain), locate the one matching
1668 * the requested path component.
1670 * The hammer_ip_*() functions merge in-memory records with on-disk
1671 * records for the purposes of the search.
1673 error = hammer_ip_first(&cursor);
1674 while (error == 0) {
1675 if (hammer_ip_resolve_data(&cursor) != 0)
1677 nlen = cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF;
1679 if (fncp->nc_nlen == nlen &&
1680 bcmp(fncp->nc_name, cursor.data->entry.name, nlen) == 0) {
1683 error = hammer_ip_next(&cursor);
1687 * If all is ok we have to get the inode so we can adjust nlinks.
1689 * WARNING: hammer_ip_del_directory() may have to terminate the
1690 * cursor to avoid a recursion. It's ok to call hammer_done_cursor()
1694 error = hammer_ip_del_directory(&trans, &cursor, fdip, ip);
1697 * XXX A deadlock here will break rename's atomicy for the purposes
1698 * of crash recovery.
1700 if (error == EDEADLK) {
1701 hammer_done_cursor(&cursor);
1706 * Cleanup and tell the kernel that the rename succeeded.
1708 hammer_done_cursor(&cursor);
1710 cache_rename(ap->a_fnch, ap->a_tnch);
1711 hammer_knote(ap->a_fdvp, NOTE_WRITE);
1712 hammer_knote(ap->a_tdvp, NOTE_WRITE);
1714 hammer_knote(ip->vp, NOTE_RENAME);
1718 hammer_done_transaction(&trans);
1723 * hammer_vop_nrmdir { nch, dvp, cred }
1727 hammer_vop_nrmdir(struct vop_nrmdir_args *ap)
1729 struct hammer_transaction trans;
1730 struct hammer_inode *dip;
1733 dip = VTOI(ap->a_dvp);
1735 if (hammer_nohistory(dip) == 0 &&
1736 (error = hammer_checkspace(dip->hmp, HAMMER_CHKSPC_REMOVE)) != 0) {
1740 hammer_start_transaction(&trans, dip->hmp);
1741 ++hammer_stats_file_iopsw;
1742 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp, ap->a_cred, 0, 1);
1743 hammer_done_transaction(&trans);
1745 hammer_knote(ap->a_dvp, NOTE_WRITE | NOTE_LINK);
1750 * hammer_vop_markatime { vp, cred }
1754 hammer_vop_markatime(struct vop_markatime_args *ap)
1756 struct hammer_transaction trans;
1757 struct hammer_inode *ip;
1759 ip = VTOI(ap->a_vp);
1760 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
1762 if (ip->flags & HAMMER_INODE_RO)
1764 if (ip->hmp->mp->mnt_flag & MNT_NOATIME)
1766 hammer_start_transaction(&trans, ip->hmp);
1767 ++hammer_stats_file_iopsw;
1769 ip->ino_data.atime = trans.time;
1770 hammer_modify_inode(ip, HAMMER_INODE_ATIME);
1771 hammer_done_transaction(&trans);
1772 hammer_knote(ap->a_vp, NOTE_ATTRIB);
1777 * hammer_vop_setattr { vp, vap, cred }
1781 hammer_vop_setattr(struct vop_setattr_args *ap)
1783 struct hammer_transaction trans;
1785 struct hammer_inode *ip;
1791 int64_t aligned_size;
1795 ip = ap->a_vp->v_data;
1799 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
1801 if (ip->flags & HAMMER_INODE_RO)
1803 if (hammer_nohistory(ip) == 0 &&
1804 (error = hammer_checkspace(ip->hmp, HAMMER_CHKSPC_REMOVE)) != 0) {
1808 hammer_start_transaction(&trans, ip->hmp);
1809 ++hammer_stats_file_iopsw;
1812 if (vap->va_flags != VNOVAL) {
1813 flags = ip->ino_data.uflags;
1814 error = vop_helper_setattr_flags(&flags, vap->va_flags,
1815 hammer_to_unix_xid(&ip->ino_data.uid),
1818 if (ip->ino_data.uflags != flags) {
1819 ip->ino_data.uflags = flags;
1820 ip->ino_data.ctime = trans.time;
1821 modflags |= HAMMER_INODE_DDIRTY;
1822 kflags |= NOTE_ATTRIB;
1824 if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) {
1831 if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) {
1835 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
1836 mode_t cur_mode = ip->ino_data.mode;
1837 uid_t cur_uid = hammer_to_unix_xid(&ip->ino_data.uid);
1838 gid_t cur_gid = hammer_to_unix_xid(&ip->ino_data.gid);
1842 error = vop_helper_chown(ap->a_vp, vap->va_uid, vap->va_gid,
1844 &cur_uid, &cur_gid, &cur_mode);
1846 hammer_guid_to_uuid(&uuid_uid, cur_uid);
1847 hammer_guid_to_uuid(&uuid_gid, cur_gid);
1848 if (bcmp(&uuid_uid, &ip->ino_data.uid,
1849 sizeof(uuid_uid)) ||
1850 bcmp(&uuid_gid, &ip->ino_data.gid,
1851 sizeof(uuid_gid)) ||
1852 ip->ino_data.mode != cur_mode
1854 ip->ino_data.uid = uuid_uid;
1855 ip->ino_data.gid = uuid_gid;
1856 ip->ino_data.mode = cur_mode;
1857 ip->ino_data.ctime = trans.time;
1858 modflags |= HAMMER_INODE_DDIRTY;
1860 kflags |= NOTE_ATTRIB;
1863 while (vap->va_size != VNOVAL && ip->ino_data.size != vap->va_size) {
1864 switch(ap->a_vp->v_type) {
1866 if (vap->va_size == ip->ino_data.size)
1869 * XXX break atomicy, we can deadlock the backend
1870 * if we do not release the lock. Probably not a
1873 blksize = hammer_blocksize(vap->va_size);
1874 if (vap->va_size < ip->ino_data.size) {
1875 vtruncbuf(ap->a_vp, vap->va_size, blksize);
1877 kflags |= NOTE_WRITE;
1879 vnode_pager_setsize(ap->a_vp, vap->va_size);
1881 kflags |= NOTE_WRITE | NOTE_EXTEND;
1883 ip->ino_data.size = vap->va_size;
1884 ip->ino_data.mtime = trans.time;
1885 modflags |= HAMMER_INODE_MTIME | HAMMER_INODE_DDIRTY;
1888 * on-media truncation is cached in the inode until
1889 * the inode is synchronized.
1892 hammer_ip_frontend_trunc(ip, vap->va_size);
1893 #ifdef DEBUG_TRUNCATE
1894 if (HammerTruncIp == NULL)
1897 if ((ip->flags & HAMMER_INODE_TRUNCATED) == 0) {
1898 ip->flags |= HAMMER_INODE_TRUNCATED;
1899 ip->trunc_off = vap->va_size;
1900 #ifdef DEBUG_TRUNCATE
1901 if (ip == HammerTruncIp)
1902 kprintf("truncate1 %016llx\n", ip->trunc_off);
1904 } else if (ip->trunc_off > vap->va_size) {
1905 ip->trunc_off = vap->va_size;
1906 #ifdef DEBUG_TRUNCATE
1907 if (ip == HammerTruncIp)
1908 kprintf("truncate2 %016llx\n", ip->trunc_off);
1911 #ifdef DEBUG_TRUNCATE
1912 if (ip == HammerTruncIp)
1913 kprintf("truncate3 %016llx (ignored)\n", vap->va_size);
1919 * If truncating we have to clean out a portion of
1920 * the last block on-disk. We do this in the
1921 * front-end buffer cache.
1923 aligned_size = (vap->va_size + (blksize - 1)) &
1924 ~(int64_t)(blksize - 1);
1925 if (truncating && vap->va_size < aligned_size) {
1929 aligned_size -= blksize;
1931 offset = (int)vap->va_size & (blksize - 1);
1932 error = bread(ap->a_vp, aligned_size,
1934 hammer_ip_frontend_trunc(ip, aligned_size);
1936 bzero(bp->b_data + offset,
1938 /* must de-cache direct-io offset */
1939 bp->b_bio2.bio_offset = NOOFFSET;
1942 kprintf("ERROR %d\n", error);
1948 if ((ip->flags & HAMMER_INODE_TRUNCATED) == 0) {
1949 ip->flags |= HAMMER_INODE_TRUNCATED;
1950 ip->trunc_off = vap->va_size;
1951 } else if (ip->trunc_off > vap->va_size) {
1952 ip->trunc_off = vap->va_size;
1954 hammer_ip_frontend_trunc(ip, vap->va_size);
1955 ip->ino_data.size = vap->va_size;
1956 ip->ino_data.mtime = trans.time;
1957 modflags |= HAMMER_INODE_MTIME | HAMMER_INODE_DDIRTY;
1958 kflags |= NOTE_ATTRIB;
1966 if (vap->va_atime.tv_sec != VNOVAL) {
1967 ip->ino_data.atime = hammer_timespec_to_time(&vap->va_atime);
1968 modflags |= HAMMER_INODE_ATIME;
1969 kflags |= NOTE_ATTRIB;
1971 if (vap->va_mtime.tv_sec != VNOVAL) {
1972 ip->ino_data.mtime = hammer_timespec_to_time(&vap->va_mtime);
1973 modflags |= HAMMER_INODE_MTIME;
1974 kflags |= NOTE_ATTRIB;
1976 if (vap->va_mode != (mode_t)VNOVAL) {
1977 mode_t cur_mode = ip->ino_data.mode;
1978 uid_t cur_uid = hammer_to_unix_xid(&ip->ino_data.uid);
1979 gid_t cur_gid = hammer_to_unix_xid(&ip->ino_data.gid);
1981 error = vop_helper_chmod(ap->a_vp, vap->va_mode, ap->a_cred,
1982 cur_uid, cur_gid, &cur_mode);
1983 if (error == 0 && ip->ino_data.mode != cur_mode) {
1984 ip->ino_data.mode = cur_mode;
1985 ip->ino_data.ctime = trans.time;
1986 modflags |= HAMMER_INODE_DDIRTY;
1987 kflags |= NOTE_ATTRIB;
1992 hammer_modify_inode(ip, modflags);
1993 hammer_done_transaction(&trans);
1994 hammer_knote(ap->a_vp, kflags);
1999 * hammer_vop_nsymlink { nch, dvp, vpp, cred, vap, target }
2003 hammer_vop_nsymlink(struct vop_nsymlink_args *ap)
2005 struct hammer_transaction trans;
2006 struct hammer_inode *dip;
2007 struct hammer_inode *nip;
2008 struct nchandle *nch;
2009 hammer_record_t record;
2013 ap->a_vap->va_type = VLNK;
2016 dip = VTOI(ap->a_dvp);
2018 if (dip->flags & HAMMER_INODE_RO)
2020 if ((error = hammer_checkspace(dip->hmp, HAMMER_CHKSPC_CREATE)) != 0)
2024 * Create a transaction to cover the operations we perform.
2026 hammer_start_transaction(&trans, dip->hmp);
2027 ++hammer_stats_file_iopsw;
2030 * Create a new filesystem object of the requested type. The
2031 * returned inode will be referenced but not locked.
2034 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
2035 dip, nch->ncp->nc_name, nch->ncp->nc_nlen,
2038 hammer_done_transaction(&trans);
2044 * Add a record representing the symlink. symlink stores the link
2045 * as pure data, not a string, and is no \0 terminated.
2048 bytes = strlen(ap->a_target);
2050 if (bytes <= HAMMER_INODE_BASESYMLEN) {
2051 bcopy(ap->a_target, nip->ino_data.ext.symlink, bytes);
2053 record = hammer_alloc_mem_record(nip, bytes);
2054 record->type = HAMMER_MEM_RECORD_GENERAL;
2056 record->leaf.base.localization = nip->obj_localization +
2057 HAMMER_LOCALIZE_MISC;
2058 record->leaf.base.key = HAMMER_FIXKEY_SYMLINK;
2059 record->leaf.base.rec_type = HAMMER_RECTYPE_FIX;
2060 record->leaf.data_len = bytes;
2061 KKASSERT(HAMMER_SYMLINK_NAME_OFF == 0);
2062 bcopy(ap->a_target, record->data->symlink.name, bytes);
2063 error = hammer_ip_add_record(&trans, record);
2067 * Set the file size to the length of the link.
2070 nip->ino_data.size = bytes;
2071 hammer_modify_inode(nip, HAMMER_INODE_DDIRTY);
2075 error = hammer_ip_add_directory(&trans, dip, nch->ncp->nc_name,
2076 nch->ncp->nc_nlen, nip);
2082 hammer_rel_inode(nip, 0);
2085 error = hammer_get_vnode(nip, ap->a_vpp);
2086 hammer_rel_inode(nip, 0);
2088 cache_setunresolved(ap->a_nch);
2089 cache_setvp(ap->a_nch, *ap->a_vpp);
2090 hammer_knote(ap->a_dvp, NOTE_WRITE);
2093 hammer_done_transaction(&trans);
2098 * hammer_vop_nwhiteout { nch, dvp, cred, flags }
2102 hammer_vop_nwhiteout(struct vop_nwhiteout_args *ap)
2104 struct hammer_transaction trans;
2105 struct hammer_inode *dip;
2108 dip = VTOI(ap->a_dvp);
2110 if (hammer_nohistory(dip) == 0 &&
2111 (error = hammer_checkspace(dip->hmp, HAMMER_CHKSPC_CREATE)) != 0) {
2115 hammer_start_transaction(&trans, dip->hmp);
2116 ++hammer_stats_file_iopsw;
2117 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp,
2118 ap->a_cred, ap->a_flags, -1);
2119 hammer_done_transaction(&trans);
2125 * hammer_vop_ioctl { vp, command, data, fflag, cred }
2129 hammer_vop_ioctl(struct vop_ioctl_args *ap)
2131 struct hammer_inode *ip = ap->a_vp->v_data;
2133 ++hammer_stats_file_iopsr;
2134 return(hammer_ioctl(ip, ap->a_command, ap->a_data,
2135 ap->a_fflag, ap->a_cred));
2140 hammer_vop_mountctl(struct vop_mountctl_args *ap)
2145 mp = ap->a_head.a_ops->head.vv_mount;
2148 case MOUNTCTL_SET_EXPORT:
2149 if (ap->a_ctllen != sizeof(struct export_args))
2152 error = hammer_vfs_export(mp, ap->a_op,
2153 (const struct export_args *)ap->a_ctl);
2156 error = journal_mountctl(ap);
2163 * hammer_vop_strategy { vp, bio }
2165 * Strategy call, used for regular file read & write only. Note that the
2166 * bp may represent a cluster.
2168 * To simplify operation and allow better optimizations in the future,
2169 * this code does not make any assumptions with regards to buffer alignment
2174 hammer_vop_strategy(struct vop_strategy_args *ap)
2179 bp = ap->a_bio->bio_buf;
2183 error = hammer_vop_strategy_read(ap);
2186 error = hammer_vop_strategy_write(ap);
2189 bp->b_error = error = EINVAL;
2190 bp->b_flags |= B_ERROR;
2198 * Read from a regular file. Iterate the related records and fill in the
2199 * BIO/BUF. Gaps are zero-filled.
2201 * The support code in hammer_object.c should be used to deal with mixed
2202 * in-memory and on-disk records.
2204 * NOTE: Can be called from the cluster code with an oversized buf.
2210 hammer_vop_strategy_read(struct vop_strategy_args *ap)
2212 struct hammer_transaction trans;
2213 struct hammer_inode *ip;
2214 struct hammer_inode *dip;
2215 struct hammer_cursor cursor;
2216 hammer_base_elm_t base;
2217 hammer_off_t disk_offset;
2231 ip = ap->a_vp->v_data;
2234 * The zone-2 disk offset may have been set by the cluster code via
2235 * a BMAP operation, or else should be NOOFFSET.
2237 * Checking the high bits for a match against zone-2 should suffice.
2239 nbio = push_bio(bio);
2240 if ((nbio->bio_offset & HAMMER_OFF_ZONE_MASK) ==
2241 HAMMER_ZONE_LARGE_DATA) {
2242 error = hammer_io_direct_read(ip->hmp, nbio, NULL);
2247 * Well, that sucked. Do it the hard way. If all the stars are
2248 * aligned we may still be able to issue a direct-read.
2250 hammer_simple_transaction(&trans, ip->hmp);
2251 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
2254 * Key range (begin and end inclusive) to scan. Note that the key's
2255 * stored in the actual records represent BASE+LEN, not BASE. The
2256 * first record containing bio_offset will have a key > bio_offset.
2258 cursor.key_beg.localization = ip->obj_localization +
2259 HAMMER_LOCALIZE_MISC;
2260 cursor.key_beg.obj_id = ip->obj_id;
2261 cursor.key_beg.create_tid = 0;
2262 cursor.key_beg.delete_tid = 0;
2263 cursor.key_beg.obj_type = 0;
2264 cursor.key_beg.key = bio->bio_offset + 1;
2265 cursor.asof = ip->obj_asof;
2266 cursor.flags |= HAMMER_CURSOR_ASOF;
2268 cursor.key_end = cursor.key_beg;
2269 KKASSERT(ip->ino_data.obj_type == HAMMER_OBJTYPE_REGFILE);
2271 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_DBFILE) {
2272 cursor.key_beg.rec_type = HAMMER_RECTYPE_DB;
2273 cursor.key_end.rec_type = HAMMER_RECTYPE_DB;
2274 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
2278 ran_end = bio->bio_offset + bp->b_bufsize;
2279 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
2280 cursor.key_end.rec_type = HAMMER_RECTYPE_DATA;
2281 tmp64 = ran_end + MAXPHYS + 1; /* work-around GCC-4 bug */
2282 if (tmp64 < ran_end)
2283 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
2285 cursor.key_end.key = ran_end + MAXPHYS + 1;
2287 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
2289 error = hammer_ip_first(&cursor);
2292 while (error == 0) {
2294 * Get the base file offset of the record. The key for
2295 * data records is (base + bytes) rather then (base).
2297 base = &cursor.leaf->base;
2298 rec_offset = base->key - cursor.leaf->data_len;
2301 * Calculate the gap, if any, and zero-fill it.
2303 * n is the offset of the start of the record verses our
2304 * current seek offset in the bio.
2306 n = (int)(rec_offset - (bio->bio_offset + boff));
2308 if (n > bp->b_bufsize - boff)
2309 n = bp->b_bufsize - boff;
2310 bzero((char *)bp->b_data + boff, n);
2316 * Calculate the data offset in the record and the number
2317 * of bytes we can copy.
2319 * There are two degenerate cases. First, boff may already
2320 * be at bp->b_bufsize. Secondly, the data offset within
2321 * the record may exceed the record's size.
2325 n = cursor.leaf->data_len - roff;
2327 kprintf("strategy_read: bad n=%d roff=%d\n", n, roff);
2329 } else if (n > bp->b_bufsize - boff) {
2330 n = bp->b_bufsize - boff;
2334 * Deal with cached truncations. This cool bit of code
2335 * allows truncate()/ftruncate() to avoid having to sync
2338 * If the frontend is truncated then all backend records are
2339 * subject to the frontend's truncation.
2341 * If the backend is truncated then backend records on-disk
2342 * (but not in-memory) are subject to the backend's
2343 * truncation. In-memory records owned by the backend
2344 * represent data written after the truncation point on the
2345 * backend and must not be truncated.
2347 * Truncate operations deal with frontend buffer cache
2348 * buffers and frontend-owned in-memory records synchronously.
2350 if (ip->flags & HAMMER_INODE_TRUNCATED) {
2351 if (hammer_cursor_ondisk(&cursor) ||
2352 cursor.iprec->flush_state == HAMMER_FST_FLUSH) {
2353 if (ip->trunc_off <= rec_offset)
2355 else if (ip->trunc_off < rec_offset + n)
2356 n = (int)(ip->trunc_off - rec_offset);
2359 if (ip->sync_flags & HAMMER_INODE_TRUNCATED) {
2360 if (hammer_cursor_ondisk(&cursor)) {
2361 if (ip->sync_trunc_off <= rec_offset)
2363 else if (ip->sync_trunc_off < rec_offset + n)
2364 n = (int)(ip->sync_trunc_off - rec_offset);
2369 * Try to issue a direct read into our bio if possible,
2370 * otherwise resolve the element data into a hammer_buffer
2373 * The buffer on-disk should be zerod past any real
2374 * truncation point, but may not be for any synthesized
2375 * truncation point from above.
2377 disk_offset = cursor.leaf->data_offset + roff;
2378 if (boff == 0 && n == bp->b_bufsize &&
2379 hammer_cursor_ondisk(&cursor) &&
2380 (disk_offset & HAMMER_BUFMASK) == 0) {
2381 KKASSERT((disk_offset & HAMMER_OFF_ZONE_MASK) ==
2382 HAMMER_ZONE_LARGE_DATA);
2383 nbio->bio_offset = disk_offset;
2384 error = hammer_io_direct_read(trans.hmp, nbio,
2388 error = hammer_ip_resolve_data(&cursor);
2390 bcopy((char *)cursor.data + roff,
2391 (char *)bp->b_data + boff, n);
2398 * Iterate until we have filled the request.
2401 if (boff == bp->b_bufsize)
2403 error = hammer_ip_next(&cursor);
2407 * There may have been a gap after the last record
2409 if (error == ENOENT)
2411 if (error == 0 && boff != bp->b_bufsize) {
2412 KKASSERT(boff < bp->b_bufsize);
2413 bzero((char *)bp->b_data + boff, bp->b_bufsize - boff);
2414 /* boff = bp->b_bufsize; */
2417 bp->b_error = error;
2419 bp->b_flags |= B_ERROR;
2424 * Cache the b-tree node for the last data read in cache[1].
2426 * If we hit the file EOF then also cache the node in the
2427 * governing director's cache[3], it will be used to initialize
2428 * the inode's cache[1] for any inodes looked up via the directory.
2430 * This doesn't reduce disk accesses since the B-Tree chain is
2431 * likely cached, but it does reduce cpu overhead when looking
2432 * up file offsets for cpdup/tar/cpio style iterations.
2435 hammer_cache_node(&ip->cache[1], cursor.node);
2436 if (ran_end >= ip->ino_data.size) {
2437 dip = hammer_find_inode(&trans, ip->ino_data.parent_obj_id,
2438 ip->obj_asof, ip->obj_localization);
2440 hammer_cache_node(&dip->cache[3], cursor.node);
2441 hammer_rel_inode(dip, 0);
2444 hammer_done_cursor(&cursor);
2445 hammer_done_transaction(&trans);
2450 * BMAP operation - used to support cluster_read() only.
2452 * (struct vnode *vp, off_t loffset, off_t *doffsetp, int *runp, int *runb)
2454 * This routine may return EOPNOTSUPP if the opration is not supported for
2455 * the specified offset. The contents of the pointer arguments do not
2456 * need to be initialized in that case.
2458 * If a disk address is available and properly aligned return 0 with
2459 * *doffsetp set to the zone-2 address, and *runp / *runb set appropriately
2460 * to the run-length relative to that offset. Callers may assume that
2461 * *doffsetp is valid if 0 is returned, even if *runp is not sufficiently
2462 * large, so return EOPNOTSUPP if it is not sufficiently large.
2466 hammer_vop_bmap(struct vop_bmap_args *ap)
2468 struct hammer_transaction trans;
2469 struct hammer_inode *ip;
2470 struct hammer_cursor cursor;
2471 hammer_base_elm_t base;
2475 int64_t base_offset;
2476 int64_t base_disk_offset;
2477 int64_t last_offset;
2478 hammer_off_t last_disk_offset;
2479 hammer_off_t disk_offset;
2484 ++hammer_stats_file_iopsr;
2485 ip = ap->a_vp->v_data;
2488 * We can only BMAP regular files. We can't BMAP database files,
2491 if (ip->ino_data.obj_type != HAMMER_OBJTYPE_REGFILE)
2495 * bmap is typically called with runp/runb both NULL when used
2496 * for writing. We do not support BMAP for writing atm.
2498 if (ap->a_cmd != BUF_CMD_READ)
2502 * Scan the B-Tree to acquire blockmap addresses, then translate
2505 hammer_simple_transaction(&trans, ip->hmp);
2507 kprintf("bmap_beg %016llx ip->cache %p\n", ap->a_loffset, ip->cache[1]);
2509 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
2512 * Key range (begin and end inclusive) to scan. Note that the key's
2513 * stored in the actual records represent BASE+LEN, not BASE. The
2514 * first record containing bio_offset will have a key > bio_offset.
2516 cursor.key_beg.localization = ip->obj_localization +
2517 HAMMER_LOCALIZE_MISC;
2518 cursor.key_beg.obj_id = ip->obj_id;
2519 cursor.key_beg.create_tid = 0;
2520 cursor.key_beg.delete_tid = 0;
2521 cursor.key_beg.obj_type = 0;
2523 cursor.key_beg.key = ap->a_loffset - MAXPHYS + 1;
2525 cursor.key_beg.key = ap->a_loffset + 1;
2526 if (cursor.key_beg.key < 0)
2527 cursor.key_beg.key = 0;
2528 cursor.asof = ip->obj_asof;
2529 cursor.flags |= HAMMER_CURSOR_ASOF;
2531 cursor.key_end = cursor.key_beg;
2532 KKASSERT(ip->ino_data.obj_type == HAMMER_OBJTYPE_REGFILE);
2534 ran_end = ap->a_loffset + MAXPHYS;
2535 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
2536 cursor.key_end.rec_type = HAMMER_RECTYPE_DATA;
2537 tmp64 = ran_end + MAXPHYS + 1; /* work-around GCC-4 bug */
2538 if (tmp64 < ran_end)
2539 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
2541 cursor.key_end.key = ran_end + MAXPHYS + 1;
2543 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
2545 error = hammer_ip_first(&cursor);
2546 base_offset = last_offset = 0;
2547 base_disk_offset = last_disk_offset = 0;
2549 while (error == 0) {
2551 * Get the base file offset of the record. The key for
2552 * data records is (base + bytes) rather then (base).
2554 * NOTE: rec_offset + rec_len may exceed the end-of-file.
2555 * The extra bytes should be zero on-disk and the BMAP op
2556 * should still be ok.
2558 base = &cursor.leaf->base;
2559 rec_offset = base->key - cursor.leaf->data_len;
2560 rec_len = cursor.leaf->data_len;
2563 * Incorporate any cached truncation.
2565 * NOTE: Modifications to rec_len based on synthesized
2566 * truncation points remove the guarantee that any extended
2567 * data on disk is zero (since the truncations may not have
2568 * taken place on-media yet).
2570 if (ip->flags & HAMMER_INODE_TRUNCATED) {
2571 if (hammer_cursor_ondisk(&cursor) ||
2572 cursor.iprec->flush_state == HAMMER_FST_FLUSH) {
2573 if (ip->trunc_off <= rec_offset)
2575 else if (ip->trunc_off < rec_offset + rec_len)
2576 rec_len = (int)(ip->trunc_off - rec_offset);
2579 if (ip->sync_flags & HAMMER_INODE_TRUNCATED) {
2580 if (hammer_cursor_ondisk(&cursor)) {
2581 if (ip->sync_trunc_off <= rec_offset)
2583 else if (ip->sync_trunc_off < rec_offset + rec_len)
2584 rec_len = (int)(ip->sync_trunc_off - rec_offset);
2589 * Accumulate information. If we have hit a discontiguous
2590 * block reset base_offset unless we are already beyond the
2591 * requested offset. If we are, that's it, we stop.
2595 if (hammer_cursor_ondisk(&cursor)) {
2596 disk_offset = cursor.leaf->data_offset;
2597 if (rec_offset != last_offset ||
2598 disk_offset != last_disk_offset) {
2599 if (rec_offset > ap->a_loffset)
2601 base_offset = rec_offset;
2602 base_disk_offset = disk_offset;
2604 last_offset = rec_offset + rec_len;
2605 last_disk_offset = disk_offset + rec_len;
2607 error = hammer_ip_next(&cursor);
2611 kprintf("BMAP %016llx: %016llx - %016llx\n",
2612 ap->a_loffset, base_offset, last_offset);
2613 kprintf("BMAP %16s: %016llx - %016llx\n",
2614 "", base_disk_offset, last_disk_offset);
2618 hammer_cache_node(&ip->cache[1], cursor.node);
2620 kprintf("bmap_end2 %016llx ip->cache %p\n", ap->a_loffset, ip->cache[1]);
2623 hammer_done_cursor(&cursor);
2624 hammer_done_transaction(&trans);
2627 * If we couldn't find any records or the records we did find were
2628 * all behind the requested offset, return failure. A forward
2629 * truncation can leave a hole w/ no on-disk records.
2631 if (last_offset == 0 || last_offset < ap->a_loffset)
2632 return (EOPNOTSUPP);
2635 * Figure out the block size at the requested offset and adjust
2636 * our limits so the cluster_read() does not create inappropriately
2637 * sized buffer cache buffers.
2639 blksize = hammer_blocksize(ap->a_loffset);
2640 if (hammer_blocksize(base_offset) != blksize) {
2641 base_offset = hammer_blockdemarc(base_offset, ap->a_loffset);
2643 if (last_offset != ap->a_loffset &&
2644 hammer_blocksize(last_offset - 1) != blksize) {
2645 last_offset = hammer_blockdemarc(ap->a_loffset,
2650 * Returning EOPNOTSUPP simply prevents the direct-IO optimization
2653 disk_offset = base_disk_offset + (ap->a_loffset - base_offset);
2655 if ((disk_offset & HAMMER_OFF_ZONE_MASK) != HAMMER_ZONE_LARGE_DATA) {
2657 * Only large-data zones can be direct-IOd
2660 } else if ((disk_offset & HAMMER_BUFMASK) ||
2661 (last_offset - ap->a_loffset) < blksize) {
2663 * doffsetp is not aligned or the forward run size does
2664 * not cover a whole buffer, disallow the direct I/O.
2671 *ap->a_doffsetp = disk_offset;
2673 *ap->a_runb = ap->a_loffset - base_offset;
2674 KKASSERT(*ap->a_runb >= 0);
2677 *ap->a_runp = last_offset - ap->a_loffset;
2678 KKASSERT(*ap->a_runp >= 0);
2686 * Write to a regular file. Because this is a strategy call the OS is
2687 * trying to actually get data onto the media.
2691 hammer_vop_strategy_write(struct vop_strategy_args *ap)
2693 hammer_record_t record;
2704 ip = ap->a_vp->v_data;
2707 blksize = hammer_blocksize(bio->bio_offset);
2708 KKASSERT(bp->b_bufsize == blksize);
2710 if (ip->flags & HAMMER_INODE_RO) {
2711 bp->b_error = EROFS;
2712 bp->b_flags |= B_ERROR;
2718 * Interlock with inode destruction (no in-kernel or directory
2719 * topology visibility). If we queue new IO while trying to
2720 * destroy the inode we can deadlock the vtrunc call in
2721 * hammer_inode_unloadable_check().
2723 * Besides, there's no point flushing a bp associated with an
2724 * inode that is being destroyed on-media and has no kernel
2727 if ((ip->flags | ip->sync_flags) &
2728 (HAMMER_INODE_DELETING|HAMMER_INODE_DELETED)) {
2735 * Reserve space and issue a direct-write from the front-end.
2736 * NOTE: The direct_io code will hammer_bread/bcopy smaller
2739 * An in-memory record will be installed to reference the storage
2740 * until the flusher can get to it.
2742 * Since we own the high level bio the front-end will not try to
2743 * do a direct-read until the write completes.
2745 * NOTE: The only time we do not reserve a full-sized buffers
2746 * worth of data is if the file is small. We do not try to
2747 * allocate a fragment (from the small-data zone) at the end of
2748 * an otherwise large file as this can lead to wildly separated
2751 KKASSERT((bio->bio_offset & HAMMER_BUFMASK) == 0);
2752 KKASSERT(bio->bio_offset < ip->ino_data.size);
2753 if (bio->bio_offset || ip->ino_data.size > HAMMER_BUFSIZE / 2)
2754 bytes = bp->b_bufsize;
2756 bytes = ((int)ip->ino_data.size + 15) & ~15;
2758 record = hammer_ip_add_bulk(ip, bio->bio_offset, bp->b_data,
2761 hammer_io_direct_write(hmp, record, bio);
2762 if (ip->rsv_recs > 1 && hmp->rsv_recs > hammer_limit_recs)
2763 hammer_flush_inode(ip, 0);
2765 bp->b_bio2.bio_offset = NOOFFSET;
2766 bp->b_error = error;
2767 bp->b_flags |= B_ERROR;
2774 * dounlink - disconnect a directory entry
2776 * XXX whiteout support not really in yet
2779 hammer_dounlink(hammer_transaction_t trans, struct nchandle *nch,
2780 struct vnode *dvp, struct ucred *cred,
2781 int flags, int isdir)
2783 struct namecache *ncp;
2786 struct hammer_cursor cursor;
2788 u_int32_t max_iterations;
2792 * Calculate the namekey and setup the key range for the scan. This
2793 * works kinda like a chained hash table where the lower 32 bits
2794 * of the namekey synthesize the chain.
2796 * The key range is inclusive of both key_beg and key_end.
2801 if (dip->flags & HAMMER_INODE_RO)
2804 namekey = hammer_directory_namekey(dip, ncp->nc_name, ncp->nc_nlen,
2807 hammer_init_cursor(trans, &cursor, &dip->cache[1], dip);
2808 cursor.key_beg.localization = dip->obj_localization +
2809 HAMMER_LOCALIZE_MISC;
2810 cursor.key_beg.obj_id = dip->obj_id;
2811 cursor.key_beg.key = namekey;
2812 cursor.key_beg.create_tid = 0;
2813 cursor.key_beg.delete_tid = 0;
2814 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
2815 cursor.key_beg.obj_type = 0;
2817 cursor.key_end = cursor.key_beg;
2818 cursor.key_end.key += max_iterations;
2819 cursor.asof = dip->obj_asof;
2820 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
2823 * Scan all matching records (the chain), locate the one matching
2824 * the requested path component. info->last_error contains the
2825 * error code on search termination and could be 0, ENOENT, or
2828 * The hammer_ip_*() functions merge in-memory records with on-disk
2829 * records for the purposes of the search.
2831 error = hammer_ip_first(&cursor);
2833 while (error == 0) {
2834 error = hammer_ip_resolve_data(&cursor);
2837 nlen = cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF;
2839 if (ncp->nc_nlen == nlen &&
2840 bcmp(ncp->nc_name, cursor.data->entry.name, nlen) == 0) {
2843 error = hammer_ip_next(&cursor);
2847 * If all is ok we have to get the inode so we can adjust nlinks.
2848 * To avoid a deadlock with the flusher we must release the inode
2849 * lock on the directory when acquiring the inode for the entry.
2851 * If the target is a directory, it must be empty.
2854 hammer_unlock(&cursor.ip->lock);
2855 ip = hammer_get_inode(trans, dip, cursor.data->entry.obj_id,
2857 cursor.data->entry.localization,
2859 hammer_lock_sh(&cursor.ip->lock);
2860 if (error == ENOENT) {
2861 kprintf("HAMMER: WARNING: Removing "
2862 "dirent w/missing inode \"%s\"\n"
2863 "\tobj_id = %016llx\n",
2865 (long long)cursor.data->entry.obj_id);
2870 * If isdir >= 0 we validate that the entry is or is not a
2871 * directory. If isdir < 0 we don't care.
2873 if (error == 0 && isdir >= 0 && ip) {
2875 ip->ino_data.obj_type != HAMMER_OBJTYPE_DIRECTORY) {
2877 } else if (isdir == 0 &&
2878 ip->ino_data.obj_type == HAMMER_OBJTYPE_DIRECTORY) {
2884 * If we are trying to remove a directory the directory must
2887 * The check directory code can loop and deadlock/retry. Our
2888 * own cursor's node locks must be released to avoid a 3-way
2889 * deadlock with the flusher if the check directory code
2892 * If any changes whatsoever have been made to the cursor
2893 * set EDEADLK and retry.
2895 if (error == 0 && ip && ip->ino_data.obj_type ==
2896 HAMMER_OBJTYPE_DIRECTORY) {
2897 hammer_unlock_cursor(&cursor);
2898 error = hammer_ip_check_directory_empty(trans, ip);
2899 hammer_lock_cursor(&cursor);
2900 if (cursor.flags & HAMMER_CURSOR_RETEST) {
2901 kprintf("HAMMER: Warning: avoided deadlock "
2909 * Delete the directory entry.
2911 * WARNING: hammer_ip_del_directory() may have to terminate
2912 * the cursor to avoid a deadlock. It is ok to call
2913 * hammer_done_cursor() twice.
2916 error = hammer_ip_del_directory(trans, &cursor,
2919 hammer_done_cursor(&cursor);
2921 cache_setunresolved(nch);
2922 cache_setvp(nch, NULL);
2925 hammer_knote(ip->vp, NOTE_DELETE);
2926 cache_inval_vp(ip->vp, CINV_DESTROY);
2930 hammer_rel_inode(ip, 0);
2932 hammer_done_cursor(&cursor);
2934 if (error == EDEADLK)
2940 /************************************************************************
2941 * FIFO AND SPECFS OPS *
2942 ************************************************************************
2947 hammer_vop_fifoclose (struct vop_close_args *ap)
2949 /* XXX update itimes */
2950 return (VOCALL(&fifo_vnode_vops, &ap->a_head));
2954 hammer_vop_fiforead (struct vop_read_args *ap)
2958 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
2959 /* XXX update access time */
2964 hammer_vop_fifowrite (struct vop_write_args *ap)
2968 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
2969 /* XXX update access time */
2975 hammer_vop_fifokqfilter(struct vop_kqfilter_args *ap)
2979 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
2981 error = hammer_vop_kqfilter(ap);
2986 hammer_vop_specclose (struct vop_close_args *ap)
2988 /* XXX update itimes */
2989 return (VOCALL(&spec_vnode_vops, &ap->a_head));
2993 hammer_vop_specread (struct vop_read_args *ap)
2995 /* XXX update access time */
2996 return (VOCALL(&spec_vnode_vops, &ap->a_head));
3000 hammer_vop_specwrite (struct vop_write_args *ap)
3002 /* XXX update last change time */
3003 return (VOCALL(&spec_vnode_vops, &ap->a_head));
3007 * SPECFS's getattr will override fields as necessary, but does not fill
3008 * stuff in from scratch.
3012 hammer_vop_specgetattr (struct vop_getattr_args *ap)
3016 error = hammer_vop_getattr(ap);
3018 VOCALL(&spec_vnode_vops, &ap->a_head);
3023 /************************************************************************
3025 ************************************************************************
3028 static void filt_hammerdetach(struct knote *kn);
3029 static int filt_hammerread(struct knote *kn, long hint);
3030 static int filt_hammerwrite(struct knote *kn, long hint);
3031 static int filt_hammervnode(struct knote *kn, long hint);
3033 static struct filterops hammerread_filtops =
3034 { 1, NULL, filt_hammerdetach, filt_hammerread };
3035 static struct filterops hammerwrite_filtops =
3036 { 1, NULL, filt_hammerdetach, filt_hammerwrite };
3037 static struct filterops hammervnode_filtops =
3038 { 1, NULL, filt_hammerdetach, filt_hammervnode };
3042 hammer_vop_kqfilter(struct vop_kqfilter_args *ap)
3044 struct vnode *vp = ap->a_vp;
3045 struct knote *kn = ap->a_kn;
3048 switch (kn->kn_filter) {
3050 kn->kn_fop = &hammerread_filtops;
3053 kn->kn_fop = &hammerwrite_filtops;
3056 kn->kn_fop = &hammervnode_filtops;
3062 kn->kn_hook = (caddr_t)vp;
3064 lwkt_gettoken(&ilock, &vp->v_pollinfo.vpi_token);
3065 SLIST_INSERT_HEAD(&vp->v_pollinfo.vpi_selinfo.si_note, kn, kn_selnext);
3066 lwkt_reltoken(&ilock);
3072 filt_hammerdetach(struct knote *kn)
3074 struct vnode *vp = (void *)kn->kn_hook;
3077 lwkt_gettoken(&ilock, &vp->v_pollinfo.vpi_token);
3078 SLIST_REMOVE(&vp->v_pollinfo.vpi_selinfo.si_note,
3079 kn, knote, kn_selnext);
3080 lwkt_reltoken(&ilock);
3084 filt_hammerread(struct knote *kn, long hint)
3086 struct vnode *vp = (void *)kn->kn_hook;
3087 hammer_inode_t ip = VTOI(vp);
3089 if (hint == NOTE_REVOKE) {
3090 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
3093 kn->kn_data = ip->ino_data.size - kn->kn_fp->f_offset;
3094 return (kn->kn_data != 0);
3098 filt_hammerwrite(struct knote *kn, long hint)
3100 if (hint == NOTE_REVOKE)
3101 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
3107 filt_hammervnode(struct knote *kn, long hint)
3109 if (kn->kn_sfflags & hint)
3110 kn->kn_fflags |= hint;
3111 if (hint == NOTE_REVOKE) {
3112 kn->kn_flags |= EV_EOF;
3115 return (kn->kn_fflags != 0);