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.91 2008/07/14 03:20:49 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);
166 hammer_vop_vnoperate(struct vop_generic_args *)
168 return (VOCALL(&hammer_vnode_vops, ap));
173 * hammer_vop_fsync { vp, waitfor }
175 * fsync() an inode to disk and wait for it to be completely committed
176 * such that the information would not be undone if a crash occured after
181 hammer_vop_fsync(struct vop_fsync_args *ap)
183 hammer_inode_t ip = VTOI(ap->a_vp);
185 ++hammer_count_fsyncs;
186 vfsync(ap->a_vp, ap->a_waitfor, 1, NULL, NULL);
187 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
188 if (ap->a_waitfor == MNT_WAIT)
189 hammer_wait_inode(ip);
194 * hammer_vop_read { vp, uio, ioflag, cred }
198 hammer_vop_read(struct vop_read_args *ap)
200 struct hammer_transaction trans;
211 if (ap->a_vp->v_type != VREG)
218 * Allow the UIO's size to override the sequential heuristic.
220 blksize = hammer_blocksize(uio->uio_offset);
221 seqcount = (uio->uio_resid + (blksize - 1)) / blksize;
222 ioseqcount = ap->a_ioflag >> 16;
223 if (seqcount < ioseqcount)
224 seqcount = ioseqcount;
226 hammer_start_transaction(&trans, ip->hmp);
229 * Access the data typically in HAMMER_BUFSIZE blocks via the
230 * buffer cache, but HAMMER may use a variable block size based
233 while (uio->uio_resid > 0 && uio->uio_offset < ip->ino_data.size) {
237 blksize = hammer_blocksize(uio->uio_offset);
238 offset = (int)uio->uio_offset & (blksize - 1);
239 base_offset = uio->uio_offset - offset;
241 if (hammer_cluster_enable) {
243 * Use file_limit to prevent cluster_read() from
244 * creating buffers of the wrong block size past
247 file_limit = ip->ino_data.size;
248 if (base_offset < HAMMER_XDEMARC &&
249 file_limit > HAMMER_XDEMARC) {
250 file_limit = HAMMER_XDEMARC;
252 error = cluster_read(ap->a_vp,
253 file_limit, base_offset,
257 error = bread(ap->a_vp, base_offset, blksize, &bp);
260 kprintf("error %d\n", error);
265 /* bp->b_flags |= B_CLUSTEROK; temporarily disabled */
266 n = blksize - offset;
267 if (n > uio->uio_resid)
269 if (n > ip->ino_data.size - uio->uio_offset)
270 n = (int)(ip->ino_data.size - uio->uio_offset);
271 error = uiomove((char *)bp->b_data + offset, n, uio);
273 /* data has a lower priority then meta-data */
274 bp->b_flags |= B_AGE;
279 if ((ip->flags & HAMMER_INODE_RO) == 0 &&
280 (ip->hmp->mp->mnt_flag & MNT_NOATIME) == 0) {
281 ip->ino_data.atime = trans.time;
282 hammer_modify_inode(ip, HAMMER_INODE_ATIME);
284 hammer_done_transaction(&trans);
289 * hammer_vop_write { vp, uio, ioflag, cred }
293 hammer_vop_write(struct vop_write_args *ap)
295 struct hammer_transaction trans;
296 struct hammer_inode *ip;
308 if (ap->a_vp->v_type != VREG)
313 seqcount = ap->a_ioflag >> 16;
315 if (ip->flags & HAMMER_INODE_RO)
319 * Create a transaction to cover the operations we perform.
321 hammer_start_transaction(&trans, hmp);
327 if (ap->a_ioflag & IO_APPEND)
328 uio->uio_offset = ip->ino_data.size;
331 * Check for illegal write offsets. Valid range is 0...2^63-1.
333 * NOTE: the base_off assignment is required to work around what
334 * I consider to be a GCC-4 optimization bug.
336 if (uio->uio_offset < 0) {
337 hammer_done_transaction(&trans);
340 base_offset = uio->uio_offset + uio->uio_resid; /* work around gcc-4 */
341 if (uio->uio_resid > 0 && base_offset <= 0) {
342 hammer_done_transaction(&trans);
347 * Access the data typically in HAMMER_BUFSIZE blocks via the
348 * buffer cache, but HAMMER may use a variable block size based
351 while (uio->uio_resid > 0) {
356 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_WRITE)) != 0)
359 blksize = hammer_blocksize(uio->uio_offset);
362 * Do not allow HAMMER to blow out the buffer cache. Very
363 * large UIOs can lockout other processes due to bwillwrite()
366 * The hammer inode is not locked during these operations.
367 * The vnode is locked which can interfere with the pageout
368 * daemon for non-UIO_NOCOPY writes but should not interfere
369 * with the buffer cache. Even so, we cannot afford to
370 * allow the pageout daemon to build up too many dirty buffer
373 /*if (((int)uio->uio_offset & (blksize - 1)) == 0)*/
377 * Do not allow HAMMER to blow out system memory by
378 * accumulating too many records. Records are so well
379 * decoupled from the buffer cache that it is possible
380 * for userland to push data out to the media via
381 * direct-write, but build up the records queued to the
382 * backend faster then the backend can flush them out.
383 * HAMMER has hit its write limit but the frontend has
384 * no pushback to slow it down.
386 if (hmp->rsv_recs > hammer_limit_recs / 2) {
388 * Get the inode on the flush list
390 if (ip->rsv_recs >= 64)
391 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
392 else if (ip->rsv_recs >= 16)
393 hammer_flush_inode(ip, 0);
396 * Keep the flusher going if the system keeps
399 delta = hmp->count_newrecords -
400 hmp->last_newrecords;
401 if (delta < 0 || delta > hammer_limit_recs / 2) {
402 hmp->last_newrecords = hmp->count_newrecords;
403 hammer_sync_hmp(hmp, MNT_NOWAIT);
407 * If we have gotten behind start slowing
410 delta = (hmp->rsv_recs - hammer_limit_recs) *
411 hz / hammer_limit_recs;
413 tsleep(&trans, 0, "hmrslo", delta);
417 * Calculate the blocksize at the current offset and figure
418 * out how much we can actually write.
420 blkmask = blksize - 1;
421 offset = (int)uio->uio_offset & blkmask;
422 base_offset = uio->uio_offset & ~(int64_t)blkmask;
423 n = blksize - offset;
424 if (n > uio->uio_resid)
426 if (uio->uio_offset + n > ip->ino_data.size) {
427 vnode_pager_setsize(ap->a_vp, uio->uio_offset + n);
431 if (uio->uio_segflg == UIO_NOCOPY) {
433 * Issuing a write with the same data backing the
434 * buffer. Instantiate the buffer to collect the
435 * backing vm pages, then read-in any missing bits.
437 * This case is used by vop_stdputpages().
439 bp = getblk(ap->a_vp, base_offset,
440 blksize, GETBLK_BHEAVY, 0);
441 if ((bp->b_flags & B_CACHE) == 0) {
443 error = bread(ap->a_vp, base_offset,
446 } else if (offset == 0 && uio->uio_resid >= blksize) {
448 * Even though we are entirely overwriting the buffer
449 * we may still have to zero it out to avoid a
450 * mmap/write visibility issue.
452 bp = getblk(ap->a_vp, base_offset, blksize, GETBLK_BHEAVY, 0);
453 if ((bp->b_flags & B_CACHE) == 0)
455 } else if (base_offset >= ip->ino_data.size) {
457 * If the base offset of the buffer is beyond the
458 * file EOF, we don't have to issue a read.
460 bp = getblk(ap->a_vp, base_offset,
461 blksize, GETBLK_BHEAVY, 0);
465 * Partial overwrite, read in any missing bits then
466 * replace the portion being written.
468 error = bread(ap->a_vp, base_offset, blksize, &bp);
473 error = uiomove((char *)bp->b_data + offset,
478 * If we screwed up we have to undo any VM size changes we
484 vtruncbuf(ap->a_vp, ip->ino_data.size,
485 hammer_blocksize(ip->ino_data.size));
489 /* bp->b_flags |= B_CLUSTEROK; temporarily disabled */
490 if (ip->ino_data.size < uio->uio_offset) {
491 ip->ino_data.size = uio->uio_offset;
492 flags = HAMMER_INODE_DDIRTY;
493 vnode_pager_setsize(ap->a_vp, ip->ino_data.size);
497 ip->ino_data.mtime = trans.time;
498 flags |= HAMMER_INODE_MTIME | HAMMER_INODE_BUFS;
499 hammer_modify_inode(ip, flags);
502 * Once we dirty the buffer any cached zone-X offset
503 * becomes invalid. HAMMER NOTE: no-history mode cannot
504 * allow overwriting over the same data sector unless
505 * we provide UNDOs for the old data, which we don't.
507 bp->b_bio2.bio_offset = NOOFFSET;
510 * Final buffer disposition.
512 bp->b_flags |= B_AGE;
513 if (ap->a_ioflag & IO_SYNC) {
515 } else if (ap->a_ioflag & IO_DIRECT) {
521 hammer_done_transaction(&trans);
526 * hammer_vop_access { vp, mode, cred }
530 hammer_vop_access(struct vop_access_args *ap)
532 struct hammer_inode *ip = VTOI(ap->a_vp);
537 uid = hammer_to_unix_xid(&ip->ino_data.uid);
538 gid = hammer_to_unix_xid(&ip->ino_data.gid);
540 error = vop_helper_access(ap, uid, gid, ip->ino_data.mode,
541 ip->ino_data.uflags);
546 * hammer_vop_advlock { vp, id, op, fl, flags }
550 hammer_vop_advlock(struct vop_advlock_args *ap)
552 hammer_inode_t ip = VTOI(ap->a_vp);
554 return (lf_advlock(ap, &ip->advlock, ip->ino_data.size));
558 * hammer_vop_close { vp, fflag }
562 hammer_vop_close(struct vop_close_args *ap)
564 hammer_inode_t ip = VTOI(ap->a_vp);
566 if ((ip->flags | ip->sync_flags) & HAMMER_INODE_MODMASK)
567 hammer_inode_waitreclaims(ip->hmp);
568 return (vop_stdclose(ap));
572 * hammer_vop_ncreate { nch, dvp, vpp, cred, vap }
574 * The operating system has already ensured that the directory entry
575 * does not exist and done all appropriate namespace locking.
579 hammer_vop_ncreate(struct vop_ncreate_args *ap)
581 struct hammer_transaction trans;
582 struct hammer_inode *dip;
583 struct hammer_inode *nip;
584 struct nchandle *nch;
588 dip = VTOI(ap->a_dvp);
590 if (dip->flags & HAMMER_INODE_RO)
592 if ((error = hammer_checkspace(dip->hmp, HAMMER_CHKSPC_CREATE)) != 0)
596 * Create a transaction to cover the operations we perform.
598 hammer_start_transaction(&trans, dip->hmp);
601 * Create a new filesystem object of the requested type. The
602 * returned inode will be referenced and shared-locked to prevent
603 * it from being moved to the flusher.
606 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
609 hkprintf("hammer_create_inode error %d\n", error);
610 hammer_done_transaction(&trans);
616 * Add the new filesystem object to the directory. This will also
617 * bump the inode's link count.
619 error = hammer_ip_add_directory(&trans, dip,
620 nch->ncp->nc_name, nch->ncp->nc_nlen,
623 hkprintf("hammer_ip_add_directory error %d\n", error);
629 hammer_rel_inode(nip, 0);
630 hammer_done_transaction(&trans);
633 error = hammer_get_vnode(nip, ap->a_vpp);
634 hammer_done_transaction(&trans);
635 hammer_rel_inode(nip, 0);
637 cache_setunresolved(ap->a_nch);
638 cache_setvp(ap->a_nch, *ap->a_vpp);
645 * hammer_vop_getattr { vp, vap }
647 * Retrieve an inode's attribute information. When accessing inodes
648 * historically we fake the atime field to ensure consistent results.
649 * The atime field is stored in the B-Tree element and allowed to be
650 * updated without cycling the element.
654 hammer_vop_getattr(struct vop_getattr_args *ap)
656 struct hammer_inode *ip = VTOI(ap->a_vp);
657 struct vattr *vap = ap->a_vap;
660 * We want the fsid to be different when accessing a filesystem
661 * with different as-of's so programs like diff don't think
662 * the files are the same.
664 * We also want the fsid to be the same when comparing snapshots,
665 * or when comparing mirrors (which might be backed by different
666 * physical devices). HAMMER fsids are based on the PFS's
669 * XXX there is a chance of collision here. The va_fsid reported
670 * by stat is different from the more involved fsid used in the
673 vap->va_fsid = ip->pfsm->fsid_udev ^ (u_int32_t)ip->obj_asof ^
674 (u_int32_t)(ip->obj_asof >> 32);
676 vap->va_fileid = ip->ino_leaf.base.obj_id;
677 vap->va_mode = ip->ino_data.mode;
678 vap->va_nlink = ip->ino_data.nlinks;
679 vap->va_uid = hammer_to_unix_xid(&ip->ino_data.uid);
680 vap->va_gid = hammer_to_unix_xid(&ip->ino_data.gid);
683 vap->va_size = ip->ino_data.size;
686 * We must provide a consistent atime and mtime for snapshots
687 * so people can do a 'tar cf - ... | md5' on them and get
688 * consistent results.
690 if (ip->flags & HAMMER_INODE_RO) {
691 hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_atime);
692 hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_mtime);
694 hammer_time_to_timespec(ip->ino_data.atime, &vap->va_atime);
695 hammer_time_to_timespec(ip->ino_data.mtime, &vap->va_mtime);
697 hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_ctime);
698 vap->va_flags = ip->ino_data.uflags;
699 vap->va_gen = 1; /* hammer inums are unique for all time */
700 vap->va_blocksize = HAMMER_BUFSIZE;
701 if (ip->ino_data.size >= HAMMER_XDEMARC) {
702 vap->va_bytes = (ip->ino_data.size + HAMMER_XBUFMASK64) &
704 } else if (ip->ino_data.size > HAMMER_BUFSIZE / 2) {
705 vap->va_bytes = (ip->ino_data.size + HAMMER_BUFMASK64) &
708 vap->va_bytes = (ip->ino_data.size + 15) & ~15;
710 vap->va_type = hammer_get_vnode_type(ip->ino_data.obj_type);
711 vap->va_filerev = 0; /* XXX */
712 /* mtime uniquely identifies any adjustments made to the file XXX */
713 vap->va_fsmid = ip->ino_data.mtime;
714 vap->va_uid_uuid = ip->ino_data.uid;
715 vap->va_gid_uuid = ip->ino_data.gid;
716 vap->va_fsid_uuid = ip->hmp->fsid;
717 vap->va_vaflags = VA_UID_UUID_VALID | VA_GID_UUID_VALID |
720 switch (ip->ino_data.obj_type) {
721 case HAMMER_OBJTYPE_CDEV:
722 case HAMMER_OBJTYPE_BDEV:
723 vap->va_rmajor = ip->ino_data.rmajor;
724 vap->va_rminor = ip->ino_data.rminor;
733 * hammer_vop_nresolve { nch, dvp, cred }
735 * Locate the requested directory entry.
739 hammer_vop_nresolve(struct vop_nresolve_args *ap)
741 struct hammer_transaction trans;
742 struct namecache *ncp;
746 struct hammer_cursor cursor;
755 u_int32_t localization;
758 * Misc initialization, plus handle as-of name extensions. Look for
759 * the '@@' extension. Note that as-of files and directories cannot
762 dip = VTOI(ap->a_dvp);
763 ncp = ap->a_nch->ncp;
764 asof = dip->obj_asof;
766 flags = dip->flags & HAMMER_INODE_RO;
769 hammer_simple_transaction(&trans, dip->hmp);
771 for (i = 0; i < nlen; ++i) {
772 if (ncp->nc_name[i] == '@' && ncp->nc_name[i+1] == '@') {
773 asof = hammer_str_to_tid(ncp->nc_name + i + 2,
774 &ispfs, &localization);
775 if (asof != HAMMER_MAX_TID)
776 flags |= HAMMER_INODE_RO;
783 * If this is a PFS softlink we dive into the PFS
785 if (ispfs && nlen == 0) {
786 ip = hammer_get_inode(&trans, dip, HAMMER_OBJID_ROOT,
790 error = hammer_get_vnode(ip, &vp);
791 hammer_rel_inode(ip, 0);
797 cache_setvp(ap->a_nch, vp);
804 * If there is no path component the time extension is relative to
808 ip = hammer_get_inode(&trans, dip, dip->obj_id,
809 asof, dip->obj_localization,
812 error = hammer_get_vnode(ip, &vp);
813 hammer_rel_inode(ip, 0);
819 cache_setvp(ap->a_nch, vp);
826 * Calculate the namekey and setup the key range for the scan. This
827 * works kinda like a chained hash table where the lower 32 bits
828 * of the namekey synthesize the chain.
830 * The key range is inclusive of both key_beg and key_end.
832 namekey = hammer_directory_namekey(ncp->nc_name, nlen);
834 error = hammer_init_cursor(&trans, &cursor, &dip->cache[1], dip);
835 cursor.key_beg.localization = dip->obj_localization +
836 HAMMER_LOCALIZE_MISC;
837 cursor.key_beg.obj_id = dip->obj_id;
838 cursor.key_beg.key = namekey;
839 cursor.key_beg.create_tid = 0;
840 cursor.key_beg.delete_tid = 0;
841 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
842 cursor.key_beg.obj_type = 0;
844 cursor.key_end = cursor.key_beg;
845 cursor.key_end.key |= 0xFFFFFFFFULL;
847 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
850 * Scan all matching records (the chain), locate the one matching
851 * the requested path component.
853 * The hammer_ip_*() functions merge in-memory records with on-disk
854 * records for the purposes of the search.
857 localization = HAMMER_DEF_LOCALIZATION;
860 error = hammer_ip_first(&cursor);
862 error = hammer_ip_resolve_data(&cursor);
865 if (nlen == cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF &&
866 bcmp(ncp->nc_name, cursor.data->entry.name, nlen) == 0) {
867 obj_id = cursor.data->entry.obj_id;
868 localization = cursor.data->entry.localization;
871 error = hammer_ip_next(&cursor);
874 hammer_done_cursor(&cursor);
876 ip = hammer_get_inode(&trans, dip, obj_id,
880 error = hammer_get_vnode(ip, &vp);
881 hammer_rel_inode(ip, 0);
887 cache_setvp(ap->a_nch, vp);
890 } else if (error == ENOENT) {
891 cache_setvp(ap->a_nch, NULL);
894 hammer_done_transaction(&trans);
899 * hammer_vop_nlookupdotdot { dvp, vpp, cred }
901 * Locate the parent directory of a directory vnode.
903 * dvp is referenced but not locked. *vpp must be returned referenced and
904 * locked. A parent_obj_id of 0 does not necessarily indicate that we are
905 * at the root, instead it could indicate that the directory we were in was
908 * NOTE: as-of sequences are not linked into the directory structure. If
909 * we are at the root with a different asof then the mount point, reload
910 * the same directory with the mount point's asof. I'm not sure what this
911 * will do to NFS. We encode ASOF stamps in NFS file handles so it might not
912 * get confused, but it hasn't been tested.
916 hammer_vop_nlookupdotdot(struct vop_nlookupdotdot_args *ap)
918 struct hammer_transaction trans;
919 struct hammer_inode *dip;
920 struct hammer_inode *ip;
921 int64_t parent_obj_id;
922 u_int32_t parent_obj_localization;
926 dip = VTOI(ap->a_dvp);
927 asof = dip->obj_asof;
930 * Whos are parent? This could be the root of a pseudo-filesystem
931 * whos parent is in another localization domain.
933 parent_obj_id = dip->ino_data.parent_obj_id;
934 if (dip->obj_id == HAMMER_OBJID_ROOT)
935 parent_obj_localization = dip->ino_data.ext.obj.parent_obj_localization;
937 parent_obj_localization = dip->obj_localization;
939 if (parent_obj_id == 0) {
940 if (dip->obj_id == HAMMER_OBJID_ROOT &&
941 asof != dip->hmp->asof) {
942 parent_obj_id = dip->obj_id;
943 asof = dip->hmp->asof;
944 *ap->a_fakename = kmalloc(19, M_TEMP, M_WAITOK);
945 ksnprintf(*ap->a_fakename, 19, "0x%016llx",
953 hammer_simple_transaction(&trans, dip->hmp);
955 ip = hammer_get_inode(&trans, dip, parent_obj_id,
956 asof, parent_obj_localization,
959 error = hammer_get_vnode(ip, ap->a_vpp);
960 hammer_rel_inode(ip, 0);
964 hammer_done_transaction(&trans);
969 * hammer_vop_nlink { nch, dvp, vp, cred }
973 hammer_vop_nlink(struct vop_nlink_args *ap)
975 struct hammer_transaction trans;
976 struct hammer_inode *dip;
977 struct hammer_inode *ip;
978 struct nchandle *nch;
982 dip = VTOI(ap->a_dvp);
985 if (dip->flags & HAMMER_INODE_RO)
987 if (ip->flags & HAMMER_INODE_RO)
989 if ((error = hammer_checkspace(dip->hmp, HAMMER_CHKSPC_CREATE)) != 0)
993 * Create a transaction to cover the operations we perform.
995 hammer_start_transaction(&trans, dip->hmp);
998 * Add the filesystem object to the directory. Note that neither
999 * dip nor ip are referenced or locked, but their vnodes are
1000 * referenced. This function will bump the inode's link count.
1002 error = hammer_ip_add_directory(&trans, dip,
1003 nch->ncp->nc_name, nch->ncp->nc_nlen,
1010 cache_setunresolved(nch);
1011 cache_setvp(nch, ap->a_vp);
1013 hammer_done_transaction(&trans);
1018 * hammer_vop_nmkdir { nch, dvp, vpp, cred, vap }
1020 * The operating system has already ensured that the directory entry
1021 * does not exist and done all appropriate namespace locking.
1025 hammer_vop_nmkdir(struct vop_nmkdir_args *ap)
1027 struct hammer_transaction trans;
1028 struct hammer_inode *dip;
1029 struct hammer_inode *nip;
1030 struct nchandle *nch;
1034 dip = VTOI(ap->a_dvp);
1036 if (dip->flags & HAMMER_INODE_RO)
1038 if ((error = hammer_checkspace(dip->hmp, HAMMER_CHKSPC_CREATE)) != 0)
1042 * Create a transaction to cover the operations we perform.
1044 hammer_start_transaction(&trans, dip->hmp);
1047 * Create a new filesystem object of the requested type. The
1048 * returned inode will be referenced but not locked.
1050 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
1053 hkprintf("hammer_mkdir error %d\n", error);
1054 hammer_done_transaction(&trans);
1059 * Add the new filesystem object to the directory. This will also
1060 * bump the inode's link count.
1062 error = hammer_ip_add_directory(&trans, dip,
1063 nch->ncp->nc_name, nch->ncp->nc_nlen,
1066 hkprintf("hammer_mkdir (add) error %d\n", error);
1072 hammer_rel_inode(nip, 0);
1075 error = hammer_get_vnode(nip, ap->a_vpp);
1076 hammer_rel_inode(nip, 0);
1078 cache_setunresolved(ap->a_nch);
1079 cache_setvp(ap->a_nch, *ap->a_vpp);
1082 hammer_done_transaction(&trans);
1087 * hammer_vop_nmknod { nch, dvp, vpp, cred, vap }
1089 * The operating system has already ensured that the directory entry
1090 * does not exist and done all appropriate namespace locking.
1094 hammer_vop_nmknod(struct vop_nmknod_args *ap)
1096 struct hammer_transaction trans;
1097 struct hammer_inode *dip;
1098 struct hammer_inode *nip;
1099 struct nchandle *nch;
1103 dip = VTOI(ap->a_dvp);
1105 if (dip->flags & HAMMER_INODE_RO)
1107 if ((error = hammer_checkspace(dip->hmp, HAMMER_CHKSPC_CREATE)) != 0)
1111 * Create a transaction to cover the operations we perform.
1113 hammer_start_transaction(&trans, dip->hmp);
1116 * Create a new filesystem object of the requested type. The
1117 * returned inode will be referenced but not locked.
1119 * If mknod specifies a directory a pseudo-fs is created.
1121 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
1124 hammer_done_transaction(&trans);
1130 * Add the new filesystem object to the directory. This will also
1131 * bump the inode's link count.
1133 error = hammer_ip_add_directory(&trans, dip,
1134 nch->ncp->nc_name, nch->ncp->nc_nlen,
1141 hammer_rel_inode(nip, 0);
1144 error = hammer_get_vnode(nip, ap->a_vpp);
1145 hammer_rel_inode(nip, 0);
1147 cache_setunresolved(ap->a_nch);
1148 cache_setvp(ap->a_nch, *ap->a_vpp);
1151 hammer_done_transaction(&trans);
1156 * hammer_vop_open { vp, mode, cred, fp }
1160 hammer_vop_open(struct vop_open_args *ap)
1164 ip = VTOI(ap->a_vp);
1166 if ((ap->a_mode & FWRITE) && (ip->flags & HAMMER_INODE_RO))
1168 return(vop_stdopen(ap));
1172 * hammer_vop_pathconf { vp, name, retval }
1176 hammer_vop_pathconf(struct vop_pathconf_args *ap)
1182 * hammer_vop_print { vp }
1186 hammer_vop_print(struct vop_print_args *ap)
1192 * hammer_vop_readdir { vp, uio, cred, *eofflag, *ncookies, off_t **cookies }
1196 hammer_vop_readdir(struct vop_readdir_args *ap)
1198 struct hammer_transaction trans;
1199 struct hammer_cursor cursor;
1200 struct hammer_inode *ip;
1202 hammer_base_elm_t base;
1211 ip = VTOI(ap->a_vp);
1213 saveoff = uio->uio_offset;
1215 if (ap->a_ncookies) {
1216 ncookies = uio->uio_resid / 16 + 1;
1217 if (ncookies > 1024)
1219 cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
1227 hammer_simple_transaction(&trans, ip->hmp);
1230 * Handle artificial entries
1234 r = vop_write_dirent(&error, uio, ip->obj_id, DT_DIR, 1, ".");
1238 cookies[cookie_index] = saveoff;
1241 if (cookie_index == ncookies)
1245 if (ip->ino_data.parent_obj_id) {
1246 r = vop_write_dirent(&error, uio,
1247 ip->ino_data.parent_obj_id,
1250 r = vop_write_dirent(&error, uio,
1251 ip->obj_id, DT_DIR, 2, "..");
1256 cookies[cookie_index] = saveoff;
1259 if (cookie_index == ncookies)
1264 * Key range (begin and end inclusive) to scan. Directory keys
1265 * directly translate to a 64 bit 'seek' position.
1267 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
1268 cursor.key_beg.localization = ip->obj_localization +
1269 HAMMER_LOCALIZE_MISC;
1270 cursor.key_beg.obj_id = ip->obj_id;
1271 cursor.key_beg.create_tid = 0;
1272 cursor.key_beg.delete_tid = 0;
1273 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1274 cursor.key_beg.obj_type = 0;
1275 cursor.key_beg.key = saveoff;
1277 cursor.key_end = cursor.key_beg;
1278 cursor.key_end.key = HAMMER_MAX_KEY;
1279 cursor.asof = ip->obj_asof;
1280 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
1282 error = hammer_ip_first(&cursor);
1284 while (error == 0) {
1285 error = hammer_ip_resolve_data(&cursor);
1288 base = &cursor.leaf->base;
1289 saveoff = base->key;
1290 KKASSERT(cursor.leaf->data_len > HAMMER_ENTRY_NAME_OFF);
1292 if (base->obj_id != ip->obj_id)
1293 panic("readdir: bad record at %p", cursor.node);
1296 * Convert pseudo-filesystems into softlinks
1298 dtype = hammer_get_dtype(cursor.leaf->base.obj_type);
1299 r = vop_write_dirent(
1300 &error, uio, cursor.data->entry.obj_id,
1302 cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF ,
1303 (void *)cursor.data->entry.name);
1308 cookies[cookie_index] = base->key;
1310 if (cookie_index == ncookies)
1312 error = hammer_ip_next(&cursor);
1314 hammer_done_cursor(&cursor);
1317 hammer_done_transaction(&trans);
1320 *ap->a_eofflag = (error == ENOENT);
1321 uio->uio_offset = saveoff;
1322 if (error && cookie_index == 0) {
1323 if (error == ENOENT)
1326 kfree(cookies, M_TEMP);
1327 *ap->a_ncookies = 0;
1328 *ap->a_cookies = NULL;
1331 if (error == ENOENT)
1334 *ap->a_ncookies = cookie_index;
1335 *ap->a_cookies = cookies;
1342 * hammer_vop_readlink { vp, uio, cred }
1346 hammer_vop_readlink(struct vop_readlink_args *ap)
1348 struct hammer_transaction trans;
1349 struct hammer_cursor cursor;
1350 struct hammer_inode *ip;
1352 u_int32_t localization;
1353 hammer_pseudofs_inmem_t pfsm;
1356 ip = VTOI(ap->a_vp);
1359 * Shortcut if the symlink data was stuffed into ino_data.
1361 * Also expand special "@@PFS%05d" softlinks (expansion only
1362 * occurs for non-historical (current) accesses made from the
1363 * primary filesystem).
1365 if (ip->ino_data.size <= HAMMER_INODE_BASESYMLEN) {
1369 ptr = ip->ino_data.ext.symlink;
1370 bytes = (int)ip->ino_data.size;
1372 ip->obj_asof == HAMMER_MAX_TID &&
1373 ip->obj_localization == 0 &&
1374 strncmp(ptr, "@@PFS", 5) == 0) {
1375 hammer_simple_transaction(&trans, ip->hmp);
1376 bcopy(ptr + 5, buf, 5);
1378 localization = strtoul(buf, NULL, 10) << 16;
1379 pfsm = hammer_load_pseudofs(&trans, localization,
1382 if (pfsm->pfsd.mirror_flags &
1383 HAMMER_PFSD_SLAVE) {
1384 ksnprintf(buf, sizeof(buf),
1386 pfsm->pfsd.sync_end_tid,
1387 localization >> 16);
1389 ksnprintf(buf, sizeof(buf),
1392 localization >> 16);
1395 bytes = strlen(buf);
1398 hammer_rel_pseudofs(trans.hmp, pfsm);
1399 hammer_done_transaction(&trans);
1401 error = uiomove(ptr, bytes, ap->a_uio);
1408 hammer_simple_transaction(&trans, ip->hmp);
1409 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
1412 * Key range (begin and end inclusive) to scan. Directory keys
1413 * directly translate to a 64 bit 'seek' position.
1415 cursor.key_beg.localization = ip->obj_localization +
1416 HAMMER_LOCALIZE_MISC;
1417 cursor.key_beg.obj_id = ip->obj_id;
1418 cursor.key_beg.create_tid = 0;
1419 cursor.key_beg.delete_tid = 0;
1420 cursor.key_beg.rec_type = HAMMER_RECTYPE_FIX;
1421 cursor.key_beg.obj_type = 0;
1422 cursor.key_beg.key = HAMMER_FIXKEY_SYMLINK;
1423 cursor.asof = ip->obj_asof;
1424 cursor.flags |= HAMMER_CURSOR_ASOF;
1426 error = hammer_ip_lookup(&cursor);
1428 error = hammer_ip_resolve_data(&cursor);
1430 KKASSERT(cursor.leaf->data_len >=
1431 HAMMER_SYMLINK_NAME_OFF);
1432 error = uiomove(cursor.data->symlink.name,
1433 cursor.leaf->data_len -
1434 HAMMER_SYMLINK_NAME_OFF,
1438 hammer_done_cursor(&cursor);
1439 hammer_done_transaction(&trans);
1444 * hammer_vop_nremove { nch, dvp, cred }
1448 hammer_vop_nremove(struct vop_nremove_args *ap)
1450 struct hammer_transaction trans;
1451 struct hammer_inode *dip;
1454 dip = VTOI(ap->a_dvp);
1456 if (hammer_nohistory(dip) == 0 &&
1457 (error = hammer_checkspace(dip->hmp, HAMMER_CHKSPC_REMOVE)) != 0) {
1461 hammer_start_transaction(&trans, dip->hmp);
1462 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp, ap->a_cred, 0);
1463 hammer_done_transaction(&trans);
1469 * hammer_vop_nrename { fnch, tnch, fdvp, tdvp, cred }
1473 hammer_vop_nrename(struct vop_nrename_args *ap)
1475 struct hammer_transaction trans;
1476 struct namecache *fncp;
1477 struct namecache *tncp;
1478 struct hammer_inode *fdip;
1479 struct hammer_inode *tdip;
1480 struct hammer_inode *ip;
1481 struct hammer_cursor cursor;
1485 fdip = VTOI(ap->a_fdvp);
1486 tdip = VTOI(ap->a_tdvp);
1487 fncp = ap->a_fnch->ncp;
1488 tncp = ap->a_tnch->ncp;
1489 ip = VTOI(fncp->nc_vp);
1490 KKASSERT(ip != NULL);
1492 if (fdip->flags & HAMMER_INODE_RO)
1494 if (tdip->flags & HAMMER_INODE_RO)
1496 if (ip->flags & HAMMER_INODE_RO)
1498 if ((error = hammer_checkspace(fdip->hmp, HAMMER_CHKSPC_CREATE)) != 0)
1501 hammer_start_transaction(&trans, fdip->hmp);
1504 * Remove tncp from the target directory and then link ip as
1505 * tncp. XXX pass trans to dounlink
1507 * Force the inode sync-time to match the transaction so it is
1508 * in-sync with the creation of the target directory entry.
1510 error = hammer_dounlink(&trans, ap->a_tnch, ap->a_tdvp, ap->a_cred, 0);
1511 if (error == 0 || error == ENOENT) {
1512 error = hammer_ip_add_directory(&trans, tdip,
1513 tncp->nc_name, tncp->nc_nlen,
1516 ip->ino_data.parent_obj_id = tdip->obj_id;
1517 hammer_modify_inode(ip, HAMMER_INODE_DDIRTY);
1521 goto failed; /* XXX */
1524 * Locate the record in the originating directory and remove it.
1526 * Calculate the namekey and setup the key range for the scan. This
1527 * works kinda like a chained hash table where the lower 32 bits
1528 * of the namekey synthesize the chain.
1530 * The key range is inclusive of both key_beg and key_end.
1532 namekey = hammer_directory_namekey(fncp->nc_name, fncp->nc_nlen);
1534 hammer_init_cursor(&trans, &cursor, &fdip->cache[1], fdip);
1535 cursor.key_beg.localization = fdip->obj_localization +
1536 HAMMER_LOCALIZE_MISC;
1537 cursor.key_beg.obj_id = fdip->obj_id;
1538 cursor.key_beg.key = namekey;
1539 cursor.key_beg.create_tid = 0;
1540 cursor.key_beg.delete_tid = 0;
1541 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1542 cursor.key_beg.obj_type = 0;
1544 cursor.key_end = cursor.key_beg;
1545 cursor.key_end.key |= 0xFFFFFFFFULL;
1546 cursor.asof = fdip->obj_asof;
1547 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
1550 * Scan all matching records (the chain), locate the one matching
1551 * the requested path component.
1553 * The hammer_ip_*() functions merge in-memory records with on-disk
1554 * records for the purposes of the search.
1556 error = hammer_ip_first(&cursor);
1557 while (error == 0) {
1558 if (hammer_ip_resolve_data(&cursor) != 0)
1560 nlen = cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF;
1562 if (fncp->nc_nlen == nlen &&
1563 bcmp(fncp->nc_name, cursor.data->entry.name, nlen) == 0) {
1566 error = hammer_ip_next(&cursor);
1570 * If all is ok we have to get the inode so we can adjust nlinks.
1572 * WARNING: hammer_ip_del_directory() may have to terminate the
1573 * cursor to avoid a recursion. It's ok to call hammer_done_cursor()
1577 error = hammer_ip_del_directory(&trans, &cursor, fdip, ip);
1580 * XXX A deadlock here will break rename's atomicy for the purposes
1581 * of crash recovery.
1583 if (error == EDEADLK) {
1584 hammer_done_cursor(&cursor);
1589 * Cleanup and tell the kernel that the rename succeeded.
1591 hammer_done_cursor(&cursor);
1593 cache_rename(ap->a_fnch, ap->a_tnch);
1596 hammer_done_transaction(&trans);
1601 * hammer_vop_nrmdir { nch, dvp, cred }
1605 hammer_vop_nrmdir(struct vop_nrmdir_args *ap)
1607 struct hammer_transaction trans;
1608 struct hammer_inode *dip;
1611 dip = VTOI(ap->a_dvp);
1613 if (hammer_nohistory(dip) == 0 &&
1614 (error = hammer_checkspace(dip->hmp, HAMMER_CHKSPC_REMOVE)) != 0) {
1618 hammer_start_transaction(&trans, dip->hmp);
1619 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp, ap->a_cred, 0);
1620 hammer_done_transaction(&trans);
1626 * hammer_vop_setattr { vp, vap, cred }
1630 hammer_vop_setattr(struct vop_setattr_args *ap)
1632 struct hammer_transaction trans;
1634 struct hammer_inode *ip;
1639 int64_t aligned_size;
1643 ip = ap->a_vp->v_data;
1646 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
1648 if (ip->flags & HAMMER_INODE_RO)
1650 if (hammer_nohistory(ip) == 0 &&
1651 (error = hammer_checkspace(ip->hmp, HAMMER_CHKSPC_REMOVE)) != 0) {
1655 hammer_start_transaction(&trans, ip->hmp);
1658 if (vap->va_flags != VNOVAL) {
1659 flags = ip->ino_data.uflags;
1660 error = vop_helper_setattr_flags(&flags, vap->va_flags,
1661 hammer_to_unix_xid(&ip->ino_data.uid),
1664 if (ip->ino_data.uflags != flags) {
1665 ip->ino_data.uflags = flags;
1666 modflags |= HAMMER_INODE_DDIRTY;
1668 if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) {
1675 if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) {
1679 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
1680 mode_t cur_mode = ip->ino_data.mode;
1681 uid_t cur_uid = hammer_to_unix_xid(&ip->ino_data.uid);
1682 gid_t cur_gid = hammer_to_unix_xid(&ip->ino_data.gid);
1686 error = vop_helper_chown(ap->a_vp, vap->va_uid, vap->va_gid,
1688 &cur_uid, &cur_gid, &cur_mode);
1690 hammer_guid_to_uuid(&uuid_uid, cur_uid);
1691 hammer_guid_to_uuid(&uuid_gid, cur_gid);
1692 if (bcmp(&uuid_uid, &ip->ino_data.uid,
1693 sizeof(uuid_uid)) ||
1694 bcmp(&uuid_gid, &ip->ino_data.gid,
1695 sizeof(uuid_gid)) ||
1696 ip->ino_data.mode != cur_mode
1698 ip->ino_data.uid = uuid_uid;
1699 ip->ino_data.gid = uuid_gid;
1700 ip->ino_data.mode = cur_mode;
1702 modflags |= HAMMER_INODE_DDIRTY;
1705 while (vap->va_size != VNOVAL && ip->ino_data.size != vap->va_size) {
1706 switch(ap->a_vp->v_type) {
1708 if (vap->va_size == ip->ino_data.size)
1711 * XXX break atomicy, we can deadlock the backend
1712 * if we do not release the lock. Probably not a
1715 blksize = hammer_blocksize(vap->va_size);
1716 if (vap->va_size < ip->ino_data.size) {
1717 vtruncbuf(ap->a_vp, vap->va_size, blksize);
1720 vnode_pager_setsize(ap->a_vp, vap->va_size);
1723 ip->ino_data.size = vap->va_size;
1724 modflags |= HAMMER_INODE_DDIRTY;
1727 * on-media truncation is cached in the inode until
1728 * the inode is synchronized.
1731 hammer_ip_frontend_trunc(ip, vap->va_size);
1732 #ifdef DEBUG_TRUNCATE
1733 if (HammerTruncIp == NULL)
1736 if ((ip->flags & HAMMER_INODE_TRUNCATED) == 0) {
1737 ip->flags |= HAMMER_INODE_TRUNCATED;
1738 ip->trunc_off = vap->va_size;
1739 #ifdef DEBUG_TRUNCATE
1740 if (ip == HammerTruncIp)
1741 kprintf("truncate1 %016llx\n", ip->trunc_off);
1743 } else if (ip->trunc_off > vap->va_size) {
1744 ip->trunc_off = vap->va_size;
1745 #ifdef DEBUG_TRUNCATE
1746 if (ip == HammerTruncIp)
1747 kprintf("truncate2 %016llx\n", ip->trunc_off);
1750 #ifdef DEBUG_TRUNCATE
1751 if (ip == HammerTruncIp)
1752 kprintf("truncate3 %016llx (ignored)\n", vap->va_size);
1758 * If truncating we have to clean out a portion of
1759 * the last block on-disk. We do this in the
1760 * front-end buffer cache.
1762 aligned_size = (vap->va_size + (blksize - 1)) &
1763 ~(int64_t)(blksize - 1);
1764 if (truncating && vap->va_size < aligned_size) {
1768 aligned_size -= blksize;
1770 offset = (int)vap->va_size & (blksize - 1);
1771 error = bread(ap->a_vp, aligned_size,
1773 hammer_ip_frontend_trunc(ip, aligned_size);
1775 bzero(bp->b_data + offset,
1777 /* must de-cache direct-io offset */
1778 bp->b_bio2.bio_offset = NOOFFSET;
1781 kprintf("ERROR %d\n", error);
1787 if ((ip->flags & HAMMER_INODE_TRUNCATED) == 0) {
1788 ip->flags |= HAMMER_INODE_TRUNCATED;
1789 ip->trunc_off = vap->va_size;
1790 } else if (ip->trunc_off > vap->va_size) {
1791 ip->trunc_off = vap->va_size;
1793 hammer_ip_frontend_trunc(ip, vap->va_size);
1794 ip->ino_data.size = vap->va_size;
1795 modflags |= HAMMER_INODE_DDIRTY;
1803 if (vap->va_atime.tv_sec != VNOVAL) {
1804 ip->ino_data.atime =
1805 hammer_timespec_to_time(&vap->va_atime);
1806 modflags |= HAMMER_INODE_ATIME;
1808 if (vap->va_mtime.tv_sec != VNOVAL) {
1809 ip->ino_data.mtime =
1810 hammer_timespec_to_time(&vap->va_mtime);
1811 modflags |= HAMMER_INODE_MTIME;
1813 if (vap->va_mode != (mode_t)VNOVAL) {
1814 mode_t cur_mode = ip->ino_data.mode;
1815 uid_t cur_uid = hammer_to_unix_xid(&ip->ino_data.uid);
1816 gid_t cur_gid = hammer_to_unix_xid(&ip->ino_data.gid);
1818 error = vop_helper_chmod(ap->a_vp, vap->va_mode, ap->a_cred,
1819 cur_uid, cur_gid, &cur_mode);
1820 if (error == 0 && ip->ino_data.mode != cur_mode) {
1821 ip->ino_data.mode = cur_mode;
1822 modflags |= HAMMER_INODE_DDIRTY;
1827 hammer_modify_inode(ip, modflags);
1828 hammer_done_transaction(&trans);
1833 * hammer_vop_nsymlink { nch, dvp, vpp, cred, vap, target }
1837 hammer_vop_nsymlink(struct vop_nsymlink_args *ap)
1839 struct hammer_transaction trans;
1840 struct hammer_inode *dip;
1841 struct hammer_inode *nip;
1842 struct nchandle *nch;
1843 hammer_record_t record;
1847 ap->a_vap->va_type = VLNK;
1850 dip = VTOI(ap->a_dvp);
1852 if (dip->flags & HAMMER_INODE_RO)
1854 if ((error = hammer_checkspace(dip->hmp, HAMMER_CHKSPC_CREATE)) != 0)
1858 * Create a transaction to cover the operations we perform.
1860 hammer_start_transaction(&trans, dip->hmp);
1863 * Create a new filesystem object of the requested type. The
1864 * returned inode will be referenced but not locked.
1867 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
1870 hammer_done_transaction(&trans);
1876 * Add a record representing the symlink. symlink stores the link
1877 * as pure data, not a string, and is no \0 terminated.
1880 bytes = strlen(ap->a_target);
1882 if (bytes <= HAMMER_INODE_BASESYMLEN) {
1883 bcopy(ap->a_target, nip->ino_data.ext.symlink, bytes);
1885 record = hammer_alloc_mem_record(nip, bytes);
1886 record->type = HAMMER_MEM_RECORD_GENERAL;
1888 record->leaf.base.localization = nip->obj_localization +
1889 HAMMER_LOCALIZE_MISC;
1890 record->leaf.base.key = HAMMER_FIXKEY_SYMLINK;
1891 record->leaf.base.rec_type = HAMMER_RECTYPE_FIX;
1892 record->leaf.data_len = bytes;
1893 KKASSERT(HAMMER_SYMLINK_NAME_OFF == 0);
1894 bcopy(ap->a_target, record->data->symlink.name, bytes);
1895 error = hammer_ip_add_record(&trans, record);
1899 * Set the file size to the length of the link.
1902 nip->ino_data.size = bytes;
1903 hammer_modify_inode(nip, HAMMER_INODE_DDIRTY);
1907 error = hammer_ip_add_directory(&trans, dip, nch->ncp->nc_name,
1908 nch->ncp->nc_nlen, nip);
1914 hammer_rel_inode(nip, 0);
1917 error = hammer_get_vnode(nip, ap->a_vpp);
1918 hammer_rel_inode(nip, 0);
1920 cache_setunresolved(ap->a_nch);
1921 cache_setvp(ap->a_nch, *ap->a_vpp);
1924 hammer_done_transaction(&trans);
1929 * hammer_vop_nwhiteout { nch, dvp, cred, flags }
1933 hammer_vop_nwhiteout(struct vop_nwhiteout_args *ap)
1935 struct hammer_transaction trans;
1936 struct hammer_inode *dip;
1939 dip = VTOI(ap->a_dvp);
1941 if (hammer_nohistory(dip) == 0 &&
1942 (error = hammer_checkspace(dip->hmp, HAMMER_CHKSPC_CREATE)) != 0) {
1946 hammer_start_transaction(&trans, dip->hmp);
1947 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp,
1948 ap->a_cred, ap->a_flags);
1949 hammer_done_transaction(&trans);
1955 * hammer_vop_ioctl { vp, command, data, fflag, cred }
1959 hammer_vop_ioctl(struct vop_ioctl_args *ap)
1961 struct hammer_inode *ip = ap->a_vp->v_data;
1963 return(hammer_ioctl(ip, ap->a_command, ap->a_data,
1964 ap->a_fflag, ap->a_cred));
1969 hammer_vop_mountctl(struct vop_mountctl_args *ap)
1974 mp = ap->a_head.a_ops->head.vv_mount;
1977 case MOUNTCTL_SET_EXPORT:
1978 if (ap->a_ctllen != sizeof(struct export_args))
1980 error = hammer_vfs_export(mp, ap->a_op,
1981 (const struct export_args *)ap->a_ctl);
1984 error = journal_mountctl(ap);
1991 * hammer_vop_strategy { vp, bio }
1993 * Strategy call, used for regular file read & write only. Note that the
1994 * bp may represent a cluster.
1996 * To simplify operation and allow better optimizations in the future,
1997 * this code does not make any assumptions with regards to buffer alignment
2002 hammer_vop_strategy(struct vop_strategy_args *ap)
2007 bp = ap->a_bio->bio_buf;
2011 error = hammer_vop_strategy_read(ap);
2014 error = hammer_vop_strategy_write(ap);
2017 bp->b_error = error = EINVAL;
2018 bp->b_flags |= B_ERROR;
2026 * Read from a regular file. Iterate the related records and fill in the
2027 * BIO/BUF. Gaps are zero-filled.
2029 * The support code in hammer_object.c should be used to deal with mixed
2030 * in-memory and on-disk records.
2032 * NOTE: Can be called from the cluster code with an oversized buf.
2038 hammer_vop_strategy_read(struct vop_strategy_args *ap)
2040 struct hammer_transaction trans;
2041 struct hammer_inode *ip;
2042 struct hammer_cursor cursor;
2043 hammer_base_elm_t base;
2044 hammer_off_t disk_offset;
2058 ip = ap->a_vp->v_data;
2061 * The zone-2 disk offset may have been set by the cluster code via
2062 * a BMAP operation, or else should be NOOFFSET.
2064 * Checking the high bits for a match against zone-2 should suffice.
2066 nbio = push_bio(bio);
2067 if ((nbio->bio_offset & HAMMER_OFF_ZONE_MASK) ==
2068 HAMMER_ZONE_LARGE_DATA) {
2069 error = hammer_io_direct_read(ip->hmp, nbio, NULL);
2074 * Well, that sucked. Do it the hard way. If all the stars are
2075 * aligned we may still be able to issue a direct-read.
2077 hammer_simple_transaction(&trans, ip->hmp);
2078 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
2081 * Key range (begin and end inclusive) to scan. Note that the key's
2082 * stored in the actual records represent BASE+LEN, not BASE. The
2083 * first record containing bio_offset will have a key > bio_offset.
2085 cursor.key_beg.localization = ip->obj_localization +
2086 HAMMER_LOCALIZE_MISC;
2087 cursor.key_beg.obj_id = ip->obj_id;
2088 cursor.key_beg.create_tid = 0;
2089 cursor.key_beg.delete_tid = 0;
2090 cursor.key_beg.obj_type = 0;
2091 cursor.key_beg.key = bio->bio_offset + 1;
2092 cursor.asof = ip->obj_asof;
2093 cursor.flags |= HAMMER_CURSOR_ASOF;
2095 cursor.key_end = cursor.key_beg;
2096 KKASSERT(ip->ino_data.obj_type == HAMMER_OBJTYPE_REGFILE);
2098 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_DBFILE) {
2099 cursor.key_beg.rec_type = HAMMER_RECTYPE_DB;
2100 cursor.key_end.rec_type = HAMMER_RECTYPE_DB;
2101 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
2105 ran_end = bio->bio_offset + bp->b_bufsize;
2106 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
2107 cursor.key_end.rec_type = HAMMER_RECTYPE_DATA;
2108 tmp64 = ran_end + MAXPHYS + 1; /* work-around GCC-4 bug */
2109 if (tmp64 < ran_end)
2110 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
2112 cursor.key_end.key = ran_end + MAXPHYS + 1;
2114 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
2116 error = hammer_ip_first(&cursor);
2119 while (error == 0) {
2121 * Get the base file offset of the record. The key for
2122 * data records is (base + bytes) rather then (base).
2124 base = &cursor.leaf->base;
2125 rec_offset = base->key - cursor.leaf->data_len;
2128 * Calculate the gap, if any, and zero-fill it.
2130 * n is the offset of the start of the record verses our
2131 * current seek offset in the bio.
2133 n = (int)(rec_offset - (bio->bio_offset + boff));
2135 if (n > bp->b_bufsize - boff)
2136 n = bp->b_bufsize - boff;
2137 bzero((char *)bp->b_data + boff, n);
2143 * Calculate the data offset in the record and the number
2144 * of bytes we can copy.
2146 * There are two degenerate cases. First, boff may already
2147 * be at bp->b_bufsize. Secondly, the data offset within
2148 * the record may exceed the record's size.
2152 n = cursor.leaf->data_len - roff;
2154 kprintf("strategy_read: bad n=%d roff=%d\n", n, roff);
2156 } else if (n > bp->b_bufsize - boff) {
2157 n = bp->b_bufsize - boff;
2161 * Deal with cached truncations. This cool bit of code
2162 * allows truncate()/ftruncate() to avoid having to sync
2165 * If the frontend is truncated then all backend records are
2166 * subject to the frontend's truncation.
2168 * If the backend is truncated then backend records on-disk
2169 * (but not in-memory) are subject to the backend's
2170 * truncation. In-memory records owned by the backend
2171 * represent data written after the truncation point on the
2172 * backend and must not be truncated.
2174 * Truncate operations deal with frontend buffer cache
2175 * buffers and frontend-owned in-memory records synchronously.
2177 if (ip->flags & HAMMER_INODE_TRUNCATED) {
2178 if (hammer_cursor_ondisk(&cursor) ||
2179 cursor.iprec->flush_state == HAMMER_FST_FLUSH) {
2180 if (ip->trunc_off <= rec_offset)
2182 else if (ip->trunc_off < rec_offset + n)
2183 n = (int)(ip->trunc_off - rec_offset);
2186 if (ip->sync_flags & HAMMER_INODE_TRUNCATED) {
2187 if (hammer_cursor_ondisk(&cursor)) {
2188 if (ip->sync_trunc_off <= rec_offset)
2190 else if (ip->sync_trunc_off < rec_offset + n)
2191 n = (int)(ip->sync_trunc_off - rec_offset);
2196 * Try to issue a direct read into our bio if possible,
2197 * otherwise resolve the element data into a hammer_buffer
2200 * The buffer on-disk should be zerod past any real
2201 * truncation point, but may not be for any synthesized
2202 * truncation point from above.
2204 disk_offset = cursor.leaf->data_offset + roff;
2205 if (boff == 0 && n == bp->b_bufsize &&
2206 hammer_cursor_ondisk(&cursor) &&
2207 (disk_offset & HAMMER_BUFMASK) == 0) {
2208 KKASSERT((disk_offset & HAMMER_OFF_ZONE_MASK) ==
2209 HAMMER_ZONE_LARGE_DATA);
2210 nbio->bio_offset = disk_offset;
2211 error = hammer_io_direct_read(trans.hmp, nbio,
2215 error = hammer_ip_resolve_data(&cursor);
2217 bcopy((char *)cursor.data + roff,
2218 (char *)bp->b_data + boff, n);
2225 * Iterate until we have filled the request.
2228 if (boff == bp->b_bufsize)
2230 error = hammer_ip_next(&cursor);
2234 * There may have been a gap after the last record
2236 if (error == ENOENT)
2238 if (error == 0 && boff != bp->b_bufsize) {
2239 KKASSERT(boff < bp->b_bufsize);
2240 bzero((char *)bp->b_data + boff, bp->b_bufsize - boff);
2241 /* boff = bp->b_bufsize; */
2244 bp->b_error = error;
2246 bp->b_flags |= B_ERROR;
2251 hammer_cache_node(&ip->cache[1], cursor.node);
2252 hammer_done_cursor(&cursor);
2253 hammer_done_transaction(&trans);
2258 * BMAP operation - used to support cluster_read() only.
2260 * (struct vnode *vp, off_t loffset, off_t *doffsetp, int *runp, int *runb)
2262 * This routine may return EOPNOTSUPP if the opration is not supported for
2263 * the specified offset. The contents of the pointer arguments do not
2264 * need to be initialized in that case.
2266 * If a disk address is available and properly aligned return 0 with
2267 * *doffsetp set to the zone-2 address, and *runp / *runb set appropriately
2268 * to the run-length relative to that offset. Callers may assume that
2269 * *doffsetp is valid if 0 is returned, even if *runp is not sufficiently
2270 * large, so return EOPNOTSUPP if it is not sufficiently large.
2274 hammer_vop_bmap(struct vop_bmap_args *ap)
2276 struct hammer_transaction trans;
2277 struct hammer_inode *ip;
2278 struct hammer_cursor cursor;
2279 hammer_base_elm_t base;
2283 int64_t base_offset;
2284 int64_t base_disk_offset;
2285 int64_t last_offset;
2286 hammer_off_t last_disk_offset;
2287 hammer_off_t disk_offset;
2292 ip = ap->a_vp->v_data;
2295 * We can only BMAP regular files. We can't BMAP database files,
2298 if (ip->ino_data.obj_type != HAMMER_OBJTYPE_REGFILE)
2302 * bmap is typically called with runp/runb both NULL when used
2303 * for writing. We do not support BMAP for writing atm.
2305 if (ap->a_cmd != BUF_CMD_READ)
2309 * Scan the B-Tree to acquire blockmap addresses, then translate
2312 hammer_simple_transaction(&trans, ip->hmp);
2314 kprintf("bmap_beg %016llx ip->cache %p\n", ap->a_loffset, ip->cache[1]);
2316 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
2319 * Key range (begin and end inclusive) to scan. Note that the key's
2320 * stored in the actual records represent BASE+LEN, not BASE. The
2321 * first record containing bio_offset will have a key > bio_offset.
2323 cursor.key_beg.localization = ip->obj_localization +
2324 HAMMER_LOCALIZE_MISC;
2325 cursor.key_beg.obj_id = ip->obj_id;
2326 cursor.key_beg.create_tid = 0;
2327 cursor.key_beg.delete_tid = 0;
2328 cursor.key_beg.obj_type = 0;
2330 cursor.key_beg.key = ap->a_loffset - MAXPHYS + 1;
2332 cursor.key_beg.key = ap->a_loffset + 1;
2333 if (cursor.key_beg.key < 0)
2334 cursor.key_beg.key = 0;
2335 cursor.asof = ip->obj_asof;
2336 cursor.flags |= HAMMER_CURSOR_ASOF;
2338 cursor.key_end = cursor.key_beg;
2339 KKASSERT(ip->ino_data.obj_type == HAMMER_OBJTYPE_REGFILE);
2341 ran_end = ap->a_loffset + MAXPHYS;
2342 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
2343 cursor.key_end.rec_type = HAMMER_RECTYPE_DATA;
2344 tmp64 = ran_end + MAXPHYS + 1; /* work-around GCC-4 bug */
2345 if (tmp64 < ran_end)
2346 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
2348 cursor.key_end.key = ran_end + MAXPHYS + 1;
2350 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
2352 error = hammer_ip_first(&cursor);
2353 base_offset = last_offset = 0;
2354 base_disk_offset = last_disk_offset = 0;
2356 while (error == 0) {
2358 * Get the base file offset of the record. The key for
2359 * data records is (base + bytes) rather then (base).
2361 * NOTE: rec_offset + rec_len may exceed the end-of-file.
2362 * The extra bytes should be zero on-disk and the BMAP op
2363 * should still be ok.
2365 base = &cursor.leaf->base;
2366 rec_offset = base->key - cursor.leaf->data_len;
2367 rec_len = cursor.leaf->data_len;
2370 * Incorporate any cached truncation.
2372 * NOTE: Modifications to rec_len based on synthesized
2373 * truncation points remove the guarantee that any extended
2374 * data on disk is zero (since the truncations may not have
2375 * taken place on-media yet).
2377 if (ip->flags & HAMMER_INODE_TRUNCATED) {
2378 if (hammer_cursor_ondisk(&cursor) ||
2379 cursor.iprec->flush_state == HAMMER_FST_FLUSH) {
2380 if (ip->trunc_off <= rec_offset)
2382 else if (ip->trunc_off < rec_offset + rec_len)
2383 rec_len = (int)(ip->trunc_off - rec_offset);
2386 if (ip->sync_flags & HAMMER_INODE_TRUNCATED) {
2387 if (hammer_cursor_ondisk(&cursor)) {
2388 if (ip->sync_trunc_off <= rec_offset)
2390 else if (ip->sync_trunc_off < rec_offset + rec_len)
2391 rec_len = (int)(ip->sync_trunc_off - rec_offset);
2396 * Accumulate information. If we have hit a discontiguous
2397 * block reset base_offset unless we are already beyond the
2398 * requested offset. If we are, that's it, we stop.
2402 if (hammer_cursor_ondisk(&cursor)) {
2403 disk_offset = cursor.leaf->data_offset;
2404 if (rec_offset != last_offset ||
2405 disk_offset != last_disk_offset) {
2406 if (rec_offset > ap->a_loffset)
2408 base_offset = rec_offset;
2409 base_disk_offset = disk_offset;
2411 last_offset = rec_offset + rec_len;
2412 last_disk_offset = disk_offset + rec_len;
2414 error = hammer_ip_next(&cursor);
2418 kprintf("BMAP %016llx: %016llx - %016llx\n",
2419 ap->a_loffset, base_offset, last_offset);
2420 kprintf("BMAP %16s: %016llx - %016llx\n",
2421 "", base_disk_offset, last_disk_offset);
2425 hammer_cache_node(&ip->cache[1], cursor.node);
2427 kprintf("bmap_end2 %016llx ip->cache %p\n", ap->a_loffset, ip->cache[1]);
2430 hammer_done_cursor(&cursor);
2431 hammer_done_transaction(&trans);
2434 * If we couldn't find any records or the records we did find were
2435 * all behind the requested offset, return failure. A forward
2436 * truncation can leave a hole w/ no on-disk records.
2438 if (last_offset == 0 || last_offset < ap->a_loffset)
2439 return (EOPNOTSUPP);
2442 * Figure out the block size at the requested offset and adjust
2443 * our limits so the cluster_read() does not create inappropriately
2444 * sized buffer cache buffers.
2446 blksize = hammer_blocksize(ap->a_loffset);
2447 if (hammer_blocksize(base_offset) != blksize) {
2448 base_offset = hammer_blockdemarc(base_offset, ap->a_loffset);
2450 if (last_offset != ap->a_loffset &&
2451 hammer_blocksize(last_offset - 1) != blksize) {
2452 last_offset = hammer_blockdemarc(ap->a_loffset,
2457 * Returning EOPNOTSUPP simply prevents the direct-IO optimization
2460 disk_offset = base_disk_offset + (ap->a_loffset - base_offset);
2462 if ((disk_offset & HAMMER_OFF_ZONE_MASK) != HAMMER_ZONE_LARGE_DATA) {
2464 * Only large-data zones can be direct-IOd
2467 } else if ((disk_offset & HAMMER_BUFMASK) ||
2468 (last_offset - ap->a_loffset) < blksize) {
2470 * doffsetp is not aligned or the forward run size does
2471 * not cover a whole buffer, disallow the direct I/O.
2478 *ap->a_doffsetp = disk_offset;
2480 *ap->a_runb = ap->a_loffset - base_offset;
2481 KKASSERT(*ap->a_runb >= 0);
2484 *ap->a_runp = last_offset - ap->a_loffset;
2485 KKASSERT(*ap->a_runp >= 0);
2493 * Write to a regular file. Because this is a strategy call the OS is
2494 * trying to actually get data onto the media.
2498 hammer_vop_strategy_write(struct vop_strategy_args *ap)
2500 hammer_record_t record;
2511 ip = ap->a_vp->v_data;
2514 blksize = hammer_blocksize(bio->bio_offset);
2515 KKASSERT(bp->b_bufsize == blksize);
2517 if (ip->flags & HAMMER_INODE_RO) {
2518 bp->b_error = EROFS;
2519 bp->b_flags |= B_ERROR;
2525 * Interlock with inode destruction (no in-kernel or directory
2526 * topology visibility). If we queue new IO while trying to
2527 * destroy the inode we can deadlock the vtrunc call in
2528 * hammer_inode_unloadable_check().
2530 if (ip->flags & (HAMMER_INODE_DELETING|HAMMER_INODE_DELETED)) {
2537 * Reserve space and issue a direct-write from the front-end.
2538 * NOTE: The direct_io code will hammer_bread/bcopy smaller
2541 * An in-memory record will be installed to reference the storage
2542 * until the flusher can get to it.
2544 * Since we own the high level bio the front-end will not try to
2545 * do a direct-read until the write completes.
2547 * NOTE: The only time we do not reserve a full-sized buffers
2548 * worth of data is if the file is small. We do not try to
2549 * allocate a fragment (from the small-data zone) at the end of
2550 * an otherwise large file as this can lead to wildly separated
2553 KKASSERT((bio->bio_offset & HAMMER_BUFMASK) == 0);
2554 KKASSERT(bio->bio_offset < ip->ino_data.size);
2555 if (bio->bio_offset || ip->ino_data.size > HAMMER_BUFSIZE / 2)
2556 bytes = bp->b_bufsize;
2558 bytes = ((int)ip->ino_data.size + 15) & ~15;
2560 record = hammer_ip_add_bulk(ip, bio->bio_offset, bp->b_data,
2563 hammer_io_direct_write(hmp, record, bio);
2564 hammer_rel_mem_record(record);
2565 if (ip->rsv_recs > 1 && hmp->rsv_recs > hammer_limit_recs)
2566 hammer_flush_inode(ip, 0);
2568 bp->b_bio2.bio_offset = NOOFFSET;
2569 bp->b_error = error;
2570 bp->b_flags |= B_ERROR;
2577 * dounlink - disconnect a directory entry
2579 * XXX whiteout support not really in yet
2582 hammer_dounlink(hammer_transaction_t trans, struct nchandle *nch,
2583 struct vnode *dvp, struct ucred *cred, int flags)
2585 struct namecache *ncp;
2588 struct hammer_cursor cursor;
2593 * Calculate the namekey and setup the key range for the scan. This
2594 * works kinda like a chained hash table where the lower 32 bits
2595 * of the namekey synthesize the chain.
2597 * The key range is inclusive of both key_beg and key_end.
2602 if (dip->flags & HAMMER_INODE_RO)
2605 namekey = hammer_directory_namekey(ncp->nc_name, ncp->nc_nlen);
2607 hammer_init_cursor(trans, &cursor, &dip->cache[1], dip);
2608 cursor.key_beg.localization = dip->obj_localization +
2609 HAMMER_LOCALIZE_MISC;
2610 cursor.key_beg.obj_id = dip->obj_id;
2611 cursor.key_beg.key = namekey;
2612 cursor.key_beg.create_tid = 0;
2613 cursor.key_beg.delete_tid = 0;
2614 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
2615 cursor.key_beg.obj_type = 0;
2617 cursor.key_end = cursor.key_beg;
2618 cursor.key_end.key |= 0xFFFFFFFFULL;
2619 cursor.asof = dip->obj_asof;
2620 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
2623 * Scan all matching records (the chain), locate the one matching
2624 * the requested path component. info->last_error contains the
2625 * error code on search termination and could be 0, ENOENT, or
2628 * The hammer_ip_*() functions merge in-memory records with on-disk
2629 * records for the purposes of the search.
2631 error = hammer_ip_first(&cursor);
2633 while (error == 0) {
2634 error = hammer_ip_resolve_data(&cursor);
2637 nlen = cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF;
2639 if (ncp->nc_nlen == nlen &&
2640 bcmp(ncp->nc_name, cursor.data->entry.name, nlen) == 0) {
2643 error = hammer_ip_next(&cursor);
2647 * If all is ok we have to get the inode so we can adjust nlinks.
2648 * To avoid a deadlock with the flusher we must release the inode
2649 * lock on the directory when acquiring the inode for the entry.
2651 * If the target is a directory, it must be empty.
2654 hammer_unlock(&cursor.ip->lock);
2655 ip = hammer_get_inode(trans, dip, cursor.data->entry.obj_id,
2657 cursor.data->entry.localization,
2659 hammer_lock_sh(&cursor.ip->lock);
2660 if (error == ENOENT) {
2661 kprintf("obj_id %016llx\n", cursor.data->entry.obj_id);
2662 Debugger("ENOENT unlinking object that should exist");
2666 * If we are trying to remove a directory the directory must
2669 * WARNING: hammer_ip_check_directory_empty() may have to
2670 * terminate the cursor to avoid a deadlock. It is ok to
2671 * call hammer_done_cursor() twice.
2673 if (error == 0 && ip->ino_data.obj_type ==
2674 HAMMER_OBJTYPE_DIRECTORY) {
2675 error = hammer_ip_check_directory_empty(trans, ip);
2679 * Delete the directory entry.
2681 * WARNING: hammer_ip_del_directory() may have to terminate
2682 * the cursor to avoid a deadlock. It is ok to call
2683 * hammer_done_cursor() twice.
2686 error = hammer_ip_del_directory(trans, &cursor,
2689 hammer_done_cursor(&cursor);
2691 cache_setunresolved(nch);
2692 cache_setvp(nch, NULL);
2695 cache_inval_vp(ip->vp, CINV_DESTROY);
2698 hammer_rel_inode(ip, 0);
2700 hammer_done_cursor(&cursor);
2702 hammer_inode_waitreclaims(dip->hmp);
2703 if (error == EDEADLK)
2709 /************************************************************************
2710 * FIFO AND SPECFS OPS *
2711 ************************************************************************
2716 hammer_vop_fifoclose (struct vop_close_args *ap)
2718 /* XXX update itimes */
2719 return (VOCALL(&fifo_vnode_vops, &ap->a_head));
2723 hammer_vop_fiforead (struct vop_read_args *ap)
2727 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
2728 /* XXX update access time */
2733 hammer_vop_fifowrite (struct vop_write_args *ap)
2737 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
2738 /* XXX update access time */
2743 hammer_vop_specclose (struct vop_close_args *ap)
2745 /* XXX update itimes */
2746 return (VOCALL(&spec_vnode_vops, &ap->a_head));
2750 hammer_vop_specread (struct vop_read_args *ap)
2752 /* XXX update access time */
2753 return (VOCALL(&spec_vnode_vops, &ap->a_head));
2757 hammer_vop_specwrite (struct vop_write_args *ap)
2759 /* XXX update last change time */
2760 return (VOCALL(&spec_vnode_vops, &ap->a_head));