2 * Copyright (c) 2007 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.35 2008/03/18 05:19: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>
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_nsymlink(struct vop_nsymlink_args *);
79 static int hammer_vop_nwhiteout(struct vop_nwhiteout_args *);
80 static int hammer_vop_ioctl(struct vop_ioctl_args *);
81 static int hammer_vop_mountctl(struct vop_mountctl_args *);
83 static int hammer_vop_fifoclose (struct vop_close_args *);
84 static int hammer_vop_fiforead (struct vop_read_args *);
85 static int hammer_vop_fifowrite (struct vop_write_args *);
87 static int hammer_vop_specclose (struct vop_close_args *);
88 static int hammer_vop_specread (struct vop_read_args *);
89 static int hammer_vop_specwrite (struct vop_write_args *);
91 struct vop_ops hammer_vnode_vops = {
92 .vop_default = vop_defaultop,
93 .vop_fsync = hammer_vop_fsync,
94 .vop_getpages = vop_stdgetpages,
95 .vop_putpages = vop_stdputpages,
96 .vop_read = hammer_vop_read,
97 .vop_write = hammer_vop_write,
98 .vop_access = hammer_vop_access,
99 .vop_advlock = hammer_vop_advlock,
100 .vop_close = hammer_vop_close,
101 .vop_ncreate = hammer_vop_ncreate,
102 .vop_getattr = hammer_vop_getattr,
103 .vop_inactive = hammer_vop_inactive,
104 .vop_reclaim = hammer_vop_reclaim,
105 .vop_nresolve = hammer_vop_nresolve,
106 .vop_nlookupdotdot = hammer_vop_nlookupdotdot,
107 .vop_nlink = hammer_vop_nlink,
108 .vop_nmkdir = hammer_vop_nmkdir,
109 .vop_nmknod = hammer_vop_nmknod,
110 .vop_open = hammer_vop_open,
111 .vop_pathconf = hammer_vop_pathconf,
112 .vop_print = hammer_vop_print,
113 .vop_readdir = hammer_vop_readdir,
114 .vop_readlink = hammer_vop_readlink,
115 .vop_nremove = hammer_vop_nremove,
116 .vop_nrename = hammer_vop_nrename,
117 .vop_nrmdir = hammer_vop_nrmdir,
118 .vop_setattr = hammer_vop_setattr,
119 .vop_strategy = hammer_vop_strategy,
120 .vop_nsymlink = hammer_vop_nsymlink,
121 .vop_nwhiteout = hammer_vop_nwhiteout,
122 .vop_ioctl = hammer_vop_ioctl,
123 .vop_mountctl = hammer_vop_mountctl
126 struct vop_ops hammer_spec_vops = {
127 .vop_default = spec_vnoperate,
128 .vop_fsync = hammer_vop_fsync,
129 .vop_read = hammer_vop_specread,
130 .vop_write = hammer_vop_specwrite,
131 .vop_access = hammer_vop_access,
132 .vop_close = hammer_vop_specclose,
133 .vop_getattr = hammer_vop_getattr,
134 .vop_inactive = hammer_vop_inactive,
135 .vop_reclaim = hammer_vop_reclaim,
136 .vop_setattr = hammer_vop_setattr
139 struct vop_ops hammer_fifo_vops = {
140 .vop_default = fifo_vnoperate,
141 .vop_fsync = hammer_vop_fsync,
142 .vop_read = hammer_vop_fiforead,
143 .vop_write = hammer_vop_fifowrite,
144 .vop_access = hammer_vop_access,
145 .vop_close = hammer_vop_fifoclose,
146 .vop_getattr = hammer_vop_getattr,
147 .vop_inactive = hammer_vop_inactive,
148 .vop_reclaim = hammer_vop_reclaim,
149 .vop_setattr = hammer_vop_setattr
152 static int hammer_dounlink(struct nchandle *nch, struct vnode *dvp,
153 struct ucred *cred, int flags);
154 static int hammer_vop_strategy_read(struct vop_strategy_args *ap);
155 static int hammer_vop_strategy_write(struct vop_strategy_args *ap);
160 hammer_vop_vnoperate(struct vop_generic_args *)
162 return (VOCALL(&hammer_vnode_vops, ap));
167 * hammer_vop_fsync { vp, waitfor }
171 hammer_vop_fsync(struct vop_fsync_args *ap)
177 error = hammer_sync_inode(ip, ap->a_waitfor, 0);
182 * hammer_vop_read { vp, uio, ioflag, cred }
186 hammer_vop_read(struct vop_read_args *ap)
188 struct hammer_transaction trans;
197 if (ap->a_vp->v_type != VREG)
201 seqcount = ap->a_ioflag >> 16;
203 hammer_start_transaction(&trans, ip->hmp);
206 * Access the data in HAMMER_BUFSIZE blocks via the buffer cache.
209 while (uio->uio_resid > 0 && uio->uio_offset < ip->ino_rec.ino_size) {
210 offset = uio->uio_offset & HAMMER_BUFMASK;
212 error = cluster_read(ap->a_vp, ip->ino_rec.ino_size,
213 uio->uio_offset - offset, HAMMER_BUFSIZE,
214 MAXBSIZE, seqcount, &bp);
216 error = bread(ap->a_vp, uio->uio_offset - offset,
217 HAMMER_BUFSIZE, &bp);
222 /* bp->b_flags |= B_CLUSTEROK; temporarily disabled */
223 n = HAMMER_BUFSIZE - offset;
224 if (n > uio->uio_resid)
226 if (n > ip->ino_rec.ino_size - uio->uio_offset)
227 n = (int)(ip->ino_rec.ino_size - uio->uio_offset);
228 error = uiomove((char *)bp->b_data + offset, n, uio);
233 if ((ip->flags & HAMMER_INODE_RO) == 0 &&
234 (ip->hmp->mp->mnt_flag & MNT_NOATIME) == 0) {
235 ip->ino_rec.ino_atime = trans.tid;
236 hammer_modify_inode(&trans, ip, HAMMER_INODE_ITIMES);
240 hammer_commit_transaction(&trans);
245 * hammer_vop_write { vp, uio, ioflag, cred }
249 hammer_vop_write(struct vop_write_args *ap)
251 struct hammer_transaction trans;
252 struct hammer_inode *ip;
260 if (ap->a_vp->v_type != VREG)
265 if (ip->flags & HAMMER_INODE_RO)
269 * Create a transaction to cover the operations we perform.
271 hammer_start_transaction(&trans, ip->hmp);
277 if (ap->a_ioflag & IO_APPEND)
278 uio->uio_offset = ip->ino_rec.ino_size;
281 * Check for illegal write offsets. Valid range is 0...2^63-1
283 if (uio->uio_offset < 0 || uio->uio_offset + uio->uio_resid <= 0) {
284 hammer_commit_transaction(&trans);
289 * Access the data in HAMMER_BUFSIZE blocks via the buffer cache.
291 while (uio->uio_resid > 0) {
294 offset = uio->uio_offset & HAMMER_BUFMASK;
295 n = HAMMER_BUFSIZE - offset;
296 if (n > uio->uio_resid)
298 if (uio->uio_offset + n > ip->ino_rec.ino_size) {
299 vnode_pager_setsize(ap->a_vp, uio->uio_offset + n);
303 if (uio->uio_segflg == UIO_NOCOPY) {
305 * Issuing a write with the same data backing the
306 * buffer. Instantiate the buffer to collect the
307 * backing vm pages, then read-in any missing bits.
309 * This case is used by vop_stdputpages().
311 bp = getblk(ap->a_vp, uio->uio_offset - offset,
312 HAMMER_BUFSIZE, GETBLK_BHEAVY, 0);
313 if ((bp->b_flags & B_CACHE) == 0) {
315 error = bread(ap->a_vp,
316 uio->uio_offset - offset,
317 HAMMER_BUFSIZE, &bp);
319 } else if (offset == 0 && uio->uio_resid >= HAMMER_BUFSIZE) {
321 * entirely overwrite the buffer
323 bp = getblk(ap->a_vp, uio->uio_offset - offset,
324 HAMMER_BUFSIZE, GETBLK_BHEAVY, 0);
325 } else if (offset == 0 && uio->uio_offset >= ip->ino_rec.ino_size) {
329 bp = getblk(ap->a_vp, uio->uio_offset - offset,
330 HAMMER_BUFSIZE, GETBLK_BHEAVY, 0);
334 * Partial overwrite, read in any missing bits then
335 * replace the portion being written.
337 error = bread(ap->a_vp, uio->uio_offset - offset,
338 HAMMER_BUFSIZE, &bp);
343 error = uiomove((char *)bp->b_data + offset, n, uio);
346 * If we screwed up we have to undo any VM size changes we
352 vtruncbuf(ap->a_vp, ip->ino_rec.ino_size,
357 /* bp->b_flags |= B_CLUSTEROK; temporarily disabled */
358 if (ip->ino_rec.ino_size < uio->uio_offset) {
359 ip->ino_rec.ino_size = uio->uio_offset;
360 flags = HAMMER_INODE_RDIRTY;
361 vnode_pager_setsize(ap->a_vp, ip->ino_rec.ino_size);
365 ip->ino_rec.ino_mtime = trans.tid;
366 flags |= HAMMER_INODE_ITIMES | HAMMER_INODE_BUFS;
367 hammer_modify_inode(&trans, ip, flags);
371 * The file write must be tagged with the same TID as the
372 * inode, for consistency in case the inode changed size.
373 * This guarantees the on-disk data records will have a
374 * TID <= the inode TID representing the size change.
376 * If a prior write has not yet flushed, retain its TID.
379 bp->b_tid = ip->last_tid;
382 * For now we can't use ip->last_tid because we may wind
383 * up trying to flush the same buffer with the same TID
384 * (but different data) multiple times, which will cause
388 bp->b_tid = trans.tid;
390 if (ap->a_ioflag & IO_SYNC) {
392 } else if (ap->a_ioflag & IO_DIRECT) {
394 } else if ((ap->a_ioflag >> 16) > 1 &&
395 (uio->uio_offset & HAMMER_BUFMASK) == 0) {
397 * If seqcount indicates sequential operation and
398 * we just finished filling a buffer, push it out
399 * now to prevent the buffer cache from becoming
400 * too full, which would trigger non-optimal
409 hammer_abort_transaction(&trans);
411 hammer_commit_transaction(&trans);
416 * hammer_vop_access { vp, mode, cred }
420 hammer_vop_access(struct vop_access_args *ap)
422 struct hammer_inode *ip = VTOI(ap->a_vp);
427 uid = hammer_to_unix_xid(&ip->ino_data.uid);
428 gid = hammer_to_unix_xid(&ip->ino_data.gid);
430 error = vop_helper_access(ap, uid, gid, ip->ino_data.mode,
431 ip->ino_data.uflags);
436 * hammer_vop_advlock { vp, id, op, fl, flags }
440 hammer_vop_advlock(struct vop_advlock_args *ap)
442 struct hammer_inode *ip = VTOI(ap->a_vp);
444 return (lf_advlock(ap, &ip->advlock, ip->ino_rec.ino_size));
448 * hammer_vop_close { vp, fflag }
452 hammer_vop_close(struct vop_close_args *ap)
454 return (vop_stdclose(ap));
458 * hammer_vop_ncreate { nch, dvp, vpp, cred, vap }
460 * The operating system has already ensured that the directory entry
461 * does not exist and done all appropriate namespace locking.
465 hammer_vop_ncreate(struct vop_ncreate_args *ap)
467 struct hammer_transaction trans;
468 struct hammer_inode *dip;
469 struct hammer_inode *nip;
470 struct nchandle *nch;
474 dip = VTOI(ap->a_dvp);
476 if (dip->flags & HAMMER_INODE_RO)
480 * Create a transaction to cover the operations we perform.
482 hammer_start_transaction(&trans, dip->hmp);
485 * Create a new filesystem object of the requested type. The
486 * returned inode will be referenced but not locked.
489 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred, dip, &nip);
491 kprintf("hammer_create_inode error %d\n", error);
493 hammer_abort_transaction(&trans);
499 * Add the new filesystem object to the directory. This will also
500 * bump the inode's link count.
502 error = hammer_ip_add_directory(&trans, dip, nch->ncp, nip);
504 kprintf("hammer_ip_add_directory error %d\n", error);
510 hammer_rel_inode(nip, 0);
511 hammer_abort_transaction(&trans);
514 hammer_commit_transaction(&trans);
515 error = hammer_get_vnode(nip, LK_EXCLUSIVE, ap->a_vpp);
516 hammer_rel_inode(nip, 0);
518 cache_setunresolved(ap->a_nch);
519 cache_setvp(ap->a_nch, *ap->a_vpp);
526 * hammer_vop_getattr { vp, vap }
530 hammer_vop_getattr(struct vop_getattr_args *ap)
532 struct hammer_inode *ip = VTOI(ap->a_vp);
533 struct vattr *vap = ap->a_vap;
536 if (cache_check_fsmid_vp(ap->a_vp, &ip->fsmid) &&
537 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0 &&
542 hammer_itimes(ap->a_vp);
545 vap->va_fsid = ip->hmp->fsid_udev;
546 vap->va_fileid = ip->ino_rec.base.base.obj_id;
547 vap->va_mode = ip->ino_data.mode;
548 vap->va_nlink = ip->ino_rec.ino_nlinks;
549 vap->va_uid = hammer_to_unix_xid(&ip->ino_data.uid);
550 vap->va_gid = hammer_to_unix_xid(&ip->ino_data.gid);
553 vap->va_size = ip->ino_rec.ino_size;
554 hammer_to_timespec(ip->ino_rec.ino_atime, &vap->va_atime);
555 hammer_to_timespec(ip->ino_rec.ino_mtime, &vap->va_mtime);
556 hammer_to_timespec(ip->ino_data.ctime, &vap->va_ctime);
557 vap->va_flags = ip->ino_data.uflags;
558 vap->va_gen = 1; /* hammer inums are unique for all time */
559 vap->va_blocksize = HAMMER_BUFSIZE;
560 vap->va_bytes = (ip->ino_rec.ino_size + 63) & ~63;
561 vap->va_type = hammer_get_vnode_type(ip->ino_rec.base.base.obj_type);
562 vap->va_filerev = 0; /* XXX */
563 /* mtime uniquely identifies any adjustments made to the file */
564 vap->va_fsmid = ip->ino_rec.ino_mtime;
565 vap->va_uid_uuid = ip->ino_data.uid;
566 vap->va_gid_uuid = ip->ino_data.gid;
567 vap->va_fsid_uuid = ip->hmp->fsid;
568 vap->va_vaflags = VA_UID_UUID_VALID | VA_GID_UUID_VALID |
571 switch (ip->ino_rec.base.base.obj_type) {
572 case HAMMER_OBJTYPE_CDEV:
573 case HAMMER_OBJTYPE_BDEV:
574 vap->va_rmajor = ip->ino_data.rmajor;
575 vap->va_rminor = ip->ino_data.rminor;
585 * hammer_vop_nresolve { nch, dvp, cred }
587 * Locate the requested directory entry.
591 hammer_vop_nresolve(struct vop_nresolve_args *ap)
593 struct namecache *ncp;
597 struct hammer_cursor cursor;
598 union hammer_record_ondisk *rec;
608 * Misc initialization, plus handle as-of name extensions. Look for
609 * the '@@' extension. Note that as-of files and directories cannot
612 dip = VTOI(ap->a_dvp);
613 ncp = ap->a_nch->ncp;
614 asof = dip->obj_asof;
618 for (i = 0; i < nlen; ++i) {
619 if (ncp->nc_name[i] == '@' && ncp->nc_name[i+1] == '@') {
620 asof = hammer_str_to_tid(ncp->nc_name + i + 2);
621 flags |= HAMMER_INODE_RO;
628 * If there is no path component the time extension is relative to
632 ip = hammer_get_inode(dip->hmp, &dip->cache[1], dip->obj_id,
633 asof, flags, &error);
635 error = hammer_get_vnode(ip, LK_EXCLUSIVE, &vp);
636 hammer_rel_inode(ip, 0);
642 cache_setvp(ap->a_nch, vp);
649 * Calculate the namekey and setup the key range for the scan. This
650 * works kinda like a chained hash table where the lower 32 bits
651 * of the namekey synthesize the chain.
653 * The key range is inclusive of both key_beg and key_end.
655 namekey = hammer_directory_namekey(ncp->nc_name, nlen);
657 error = hammer_init_cursor_hmp(&cursor, &dip->cache[0], dip->hmp);
658 cursor.key_beg.obj_id = dip->obj_id;
659 cursor.key_beg.key = namekey;
660 cursor.key_beg.create_tid = 0;
661 cursor.key_beg.delete_tid = 0;
662 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
663 cursor.key_beg.obj_type = 0;
665 cursor.key_end = cursor.key_beg;
666 cursor.key_end.key |= 0xFFFFFFFFULL;
668 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
671 * Scan all matching records (the chain), locate the one matching
672 * the requested path component.
674 * The hammer_ip_*() functions merge in-memory records with on-disk
675 * records for the purposes of the search.
678 error = hammer_ip_first(&cursor, dip);
684 error = hammer_ip_resolve_data(&cursor);
688 if (nlen == rec->entry.base.data_len &&
689 bcmp(ncp->nc_name, cursor.data, nlen) == 0) {
690 obj_id = rec->entry.obj_id;
693 error = hammer_ip_next(&cursor);
695 hammer_done_cursor(&cursor);
697 ip = hammer_get_inode(dip->hmp, &dip->cache[1],
698 obj_id, asof, flags, &error);
700 error = hammer_get_vnode(ip, LK_EXCLUSIVE, &vp);
701 hammer_rel_inode(ip, 0);
707 cache_setvp(ap->a_nch, vp);
710 } else if (error == ENOENT) {
711 cache_setvp(ap->a_nch, NULL);
717 * hammer_vop_nlookupdotdot { dvp, vpp, cred }
719 * Locate the parent directory of a directory vnode.
721 * dvp is referenced but not locked. *vpp must be returned referenced and
722 * locked. A parent_obj_id of 0 does not necessarily indicate that we are
723 * at the root, instead it could indicate that the directory we were in was
726 * NOTE: as-of sequences are not linked into the directory structure. If
727 * we are at the root with a different asof then the mount point, reload
728 * the same directory with the mount point's asof. I'm not sure what this
729 * will do to NFS. We encode ASOF stamps in NFS file handles so it might not
730 * get confused, but it hasn't been tested.
734 hammer_vop_nlookupdotdot(struct vop_nlookupdotdot_args *ap)
736 struct hammer_inode *dip;
737 struct hammer_inode *ip;
738 int64_t parent_obj_id;
742 dip = VTOI(ap->a_dvp);
743 asof = dip->obj_asof;
744 parent_obj_id = dip->ino_data.parent_obj_id;
746 if (parent_obj_id == 0) {
747 if (dip->obj_id == HAMMER_OBJID_ROOT &&
748 asof != dip->hmp->asof) {
749 parent_obj_id = dip->obj_id;
750 asof = dip->hmp->asof;
751 *ap->a_fakename = kmalloc(19, M_TEMP, M_WAITOK);
752 ksnprintf(*ap->a_fakename, 19, "0x%016llx",
760 ip = hammer_get_inode(dip->hmp, &dip->cache[1], parent_obj_id,
761 asof, dip->flags, &error);
766 error = hammer_get_vnode(ip, LK_EXCLUSIVE, ap->a_vpp);
767 hammer_rel_inode(ip, 0);
772 * hammer_vop_nlink { nch, dvp, vp, cred }
776 hammer_vop_nlink(struct vop_nlink_args *ap)
778 struct hammer_transaction trans;
779 struct hammer_inode *dip;
780 struct hammer_inode *ip;
781 struct nchandle *nch;
785 dip = VTOI(ap->a_dvp);
788 if (dip->flags & HAMMER_INODE_RO)
790 if (ip->flags & HAMMER_INODE_RO)
794 * Create a transaction to cover the operations we perform.
796 hammer_start_transaction(&trans, dip->hmp);
799 * Add the filesystem object to the directory. Note that neither
800 * dip nor ip are referenced or locked, but their vnodes are
801 * referenced. This function will bump the inode's link count.
803 error = hammer_ip_add_directory(&trans, dip, nch->ncp, ip);
809 hammer_abort_transaction(&trans);
811 cache_setunresolved(nch);
812 cache_setvp(nch, ap->a_vp);
813 hammer_commit_transaction(&trans);
819 * hammer_vop_nmkdir { nch, dvp, vpp, cred, vap }
821 * The operating system has already ensured that the directory entry
822 * does not exist and done all appropriate namespace locking.
826 hammer_vop_nmkdir(struct vop_nmkdir_args *ap)
828 struct hammer_transaction trans;
829 struct hammer_inode *dip;
830 struct hammer_inode *nip;
831 struct nchandle *nch;
835 dip = VTOI(ap->a_dvp);
837 if (dip->flags & HAMMER_INODE_RO)
841 * Create a transaction to cover the operations we perform.
843 hammer_start_transaction(&trans, dip->hmp);
846 * Create a new filesystem object of the requested type. The
847 * returned inode will be referenced but not locked.
849 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred, dip, &nip);
851 kprintf("hammer_mkdir error %d\n", error);
853 hammer_abort_transaction(&trans);
859 * Add the new filesystem object to the directory. This will also
860 * bump the inode's link count.
862 error = hammer_ip_add_directory(&trans, dip, nch->ncp, nip);
864 kprintf("hammer_mkdir (add) error %d\n", error);
870 hammer_rel_inode(nip, 0);
871 hammer_abort_transaction(&trans);
874 hammer_commit_transaction(&trans);
875 error = hammer_get_vnode(nip, LK_EXCLUSIVE, ap->a_vpp);
876 hammer_rel_inode(nip, 0);
878 cache_setunresolved(ap->a_nch);
879 cache_setvp(ap->a_nch, *ap->a_vpp);
886 * hammer_vop_nmknod { nch, dvp, vpp, cred, vap }
888 * The operating system has already ensured that the directory entry
889 * does not exist and done all appropriate namespace locking.
893 hammer_vop_nmknod(struct vop_nmknod_args *ap)
895 struct hammer_transaction trans;
896 struct hammer_inode *dip;
897 struct hammer_inode *nip;
898 struct nchandle *nch;
902 dip = VTOI(ap->a_dvp);
904 if (dip->flags & HAMMER_INODE_RO)
908 * Create a transaction to cover the operations we perform.
910 hammer_start_transaction(&trans, dip->hmp);
913 * Create a new filesystem object of the requested type. The
914 * returned inode will be referenced but not locked.
916 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred, dip, &nip);
918 hammer_abort_transaction(&trans);
924 * Add the new filesystem object to the directory. This will also
925 * bump the inode's link count.
927 error = hammer_ip_add_directory(&trans, dip, nch->ncp, nip);
933 hammer_rel_inode(nip, 0);
934 hammer_abort_transaction(&trans);
937 hammer_commit_transaction(&trans);
938 error = hammer_get_vnode(nip, LK_EXCLUSIVE, ap->a_vpp);
939 hammer_rel_inode(nip, 0);
941 cache_setunresolved(ap->a_nch);
942 cache_setvp(ap->a_nch, *ap->a_vpp);
949 * hammer_vop_open { vp, mode, cred, fp }
953 hammer_vop_open(struct vop_open_args *ap)
955 if ((ap->a_mode & FWRITE) && (VTOI(ap->a_vp)->flags & HAMMER_INODE_RO))
958 return(vop_stdopen(ap));
962 * hammer_vop_pathconf { vp, name, retval }
966 hammer_vop_pathconf(struct vop_pathconf_args *ap)
972 * hammer_vop_print { vp }
976 hammer_vop_print(struct vop_print_args *ap)
982 * hammer_vop_readdir { vp, uio, cred, *eofflag, *ncookies, off_t **cookies }
986 hammer_vop_readdir(struct vop_readdir_args *ap)
988 struct hammer_cursor cursor;
989 struct hammer_inode *ip;
991 hammer_record_ondisk_t rec;
992 hammer_base_elm_t base;
1000 ip = VTOI(ap->a_vp);
1002 saveoff = uio->uio_offset;
1004 if (ap->a_ncookies) {
1005 ncookies = uio->uio_resid / 16 + 1;
1006 if (ncookies > 1024)
1008 cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
1017 * Handle artificial entries
1021 r = vop_write_dirent(&error, uio, ip->obj_id, DT_DIR, 1, ".");
1025 cookies[cookie_index] = saveoff;
1028 if (cookie_index == ncookies)
1032 if (ip->ino_data.parent_obj_id) {
1033 r = vop_write_dirent(&error, uio,
1034 ip->ino_data.parent_obj_id,
1037 r = vop_write_dirent(&error, uio,
1038 ip->obj_id, DT_DIR, 2, "..");
1043 cookies[cookie_index] = saveoff;
1046 if (cookie_index == ncookies)
1051 * Key range (begin and end inclusive) to scan. Directory keys
1052 * directly translate to a 64 bit 'seek' position.
1054 hammer_init_cursor_hmp(&cursor, &ip->cache[0], ip->hmp);
1055 cursor.key_beg.obj_id = ip->obj_id;
1056 cursor.key_beg.create_tid = 0;
1057 cursor.key_beg.delete_tid = 0;
1058 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1059 cursor.key_beg.obj_type = 0;
1060 cursor.key_beg.key = saveoff;
1062 cursor.key_end = cursor.key_beg;
1063 cursor.key_end.key = HAMMER_MAX_KEY;
1064 cursor.asof = ip->obj_asof;
1065 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
1067 error = hammer_ip_first(&cursor, ip);
1069 while (error == 0) {
1070 error = hammer_ip_resolve_record_and_data(&cursor);
1073 rec = cursor.record;
1074 base = &rec->base.base;
1075 saveoff = base->key;
1077 if (base->obj_id != ip->obj_id)
1078 panic("readdir: bad record at %p", cursor.node);
1080 r = vop_write_dirent(
1081 &error, uio, rec->entry.obj_id,
1082 hammer_get_dtype(rec->entry.base.base.obj_type),
1083 rec->entry.base.data_len,
1084 (void *)cursor.data);
1089 cookies[cookie_index] = base->key;
1091 if (cookie_index == ncookies)
1093 error = hammer_ip_next(&cursor);
1095 hammer_done_cursor(&cursor);
1099 *ap->a_eofflag = (error == ENOENT);
1100 uio->uio_offset = saveoff;
1101 if (error && cookie_index == 0) {
1102 if (error == ENOENT)
1105 kfree(cookies, M_TEMP);
1106 *ap->a_ncookies = 0;
1107 *ap->a_cookies = NULL;
1110 if (error == ENOENT)
1113 *ap->a_ncookies = cookie_index;
1114 *ap->a_cookies = cookies;
1121 * hammer_vop_readlink { vp, uio, cred }
1125 hammer_vop_readlink(struct vop_readlink_args *ap)
1127 struct hammer_cursor cursor;
1128 struct hammer_inode *ip;
1131 ip = VTOI(ap->a_vp);
1132 hammer_init_cursor_hmp(&cursor, &ip->cache[0], ip->hmp);
1135 * Key range (begin and end inclusive) to scan. Directory keys
1136 * directly translate to a 64 bit 'seek' position.
1138 cursor.key_beg.obj_id = ip->obj_id;
1139 cursor.key_beg.create_tid = 0;
1140 cursor.key_beg.delete_tid = 0;
1141 cursor.key_beg.rec_type = HAMMER_RECTYPE_FIX;
1142 cursor.key_beg.obj_type = 0;
1143 cursor.key_beg.key = HAMMER_FIXKEY_SYMLINK;
1144 cursor.asof = ip->obj_asof;
1145 cursor.flags |= HAMMER_CURSOR_ASOF;
1147 error = hammer_ip_lookup(&cursor, ip);
1149 error = hammer_ip_resolve_data(&cursor);
1151 error = uiomove((char *)cursor.data,
1152 cursor.record->base.data_len,
1156 hammer_done_cursor(&cursor);
1161 * hammer_vop_nremove { nch, dvp, cred }
1165 hammer_vop_nremove(struct vop_nremove_args *ap)
1167 return(hammer_dounlink(ap->a_nch, ap->a_dvp, ap->a_cred, 0));
1171 * hammer_vop_nrename { fnch, tnch, fdvp, tdvp, cred }
1175 hammer_vop_nrename(struct vop_nrename_args *ap)
1177 struct hammer_transaction trans;
1178 struct namecache *fncp;
1179 struct namecache *tncp;
1180 struct hammer_inode *fdip;
1181 struct hammer_inode *tdip;
1182 struct hammer_inode *ip;
1183 struct hammer_cursor cursor;
1184 union hammer_record_ondisk *rec;
1188 fdip = VTOI(ap->a_fdvp);
1189 tdip = VTOI(ap->a_tdvp);
1190 fncp = ap->a_fnch->ncp;
1191 tncp = ap->a_tnch->ncp;
1192 ip = VTOI(fncp->nc_vp);
1193 KKASSERT(ip != NULL);
1195 if (fdip->flags & HAMMER_INODE_RO)
1197 if (tdip->flags & HAMMER_INODE_RO)
1199 if (ip->flags & HAMMER_INODE_RO)
1202 hammer_start_transaction(&trans, fdip->hmp);
1205 * Remove tncp from the target directory and then link ip as
1206 * tncp. XXX pass trans to dounlink
1208 * Force the inode sync-time to match the transaction so it is
1209 * in-sync with the creation of the target directory entry.
1211 error = hammer_dounlink(ap->a_tnch, ap->a_tdvp, ap->a_cred, 0);
1212 if (error == 0 || error == ENOENT) {
1213 error = hammer_ip_add_directory(&trans, tdip, tncp, ip);
1215 ip->ino_data.parent_obj_id = tdip->obj_id;
1216 hammer_modify_inode(&trans, ip,
1217 HAMMER_INODE_DDIRTY | HAMMER_INODE_TIDLOCKED);
1221 goto failed; /* XXX */
1224 * Locate the record in the originating directory and remove it.
1226 * Calculate the namekey and setup the key range for the scan. This
1227 * works kinda like a chained hash table where the lower 32 bits
1228 * of the namekey synthesize the chain.
1230 * The key range is inclusive of both key_beg and key_end.
1232 namekey = hammer_directory_namekey(fncp->nc_name, fncp->nc_nlen);
1234 hammer_init_cursor_hmp(&cursor, &fdip->cache[0], fdip->hmp);
1235 cursor.key_beg.obj_id = fdip->obj_id;
1236 cursor.key_beg.key = namekey;
1237 cursor.key_beg.create_tid = 0;
1238 cursor.key_beg.delete_tid = 0;
1239 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1240 cursor.key_beg.obj_type = 0;
1242 cursor.key_end = cursor.key_beg;
1243 cursor.key_end.key |= 0xFFFFFFFFULL;
1244 cursor.asof = fdip->obj_asof;
1245 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
1248 * Scan all matching records (the chain), locate the one matching
1249 * the requested path component.
1251 * The hammer_ip_*() functions merge in-memory records with on-disk
1252 * records for the purposes of the search.
1254 error = hammer_ip_first(&cursor, fdip);
1255 while (error == 0) {
1256 if (hammer_ip_resolve_data(&cursor) != 0)
1258 rec = cursor.record;
1259 if (fncp->nc_nlen == rec->entry.base.data_len &&
1260 bcmp(fncp->nc_name, cursor.data, fncp->nc_nlen) == 0) {
1263 error = hammer_ip_next(&cursor);
1267 * If all is ok we have to get the inode so we can adjust nlinks.
1269 * WARNING: hammer_ip_del_directory() may have to terminate the
1270 * cursor to avoid a recursion. It's ok to call hammer_done_cursor()
1274 error = hammer_ip_del_directory(&trans, &cursor, fdip, ip);
1275 hammer_done_cursor(&cursor);
1277 cache_rename(ap->a_fnch, ap->a_tnch);
1278 if (error == EDEADLK)
1282 hammer_commit_transaction(&trans);
1284 hammer_abort_transaction(&trans);
1290 * hammer_vop_nrmdir { nch, dvp, cred }
1294 hammer_vop_nrmdir(struct vop_nrmdir_args *ap)
1296 return(hammer_dounlink(ap->a_nch, ap->a_dvp, ap->a_cred, 0));
1300 * hammer_vop_setattr { vp, vap, cred }
1304 hammer_vop_setattr(struct vop_setattr_args *ap)
1306 struct hammer_transaction trans;
1308 struct hammer_inode *ip;
1312 int64_t aligned_size;
1317 ip = ap->a_vp->v_data;
1320 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
1322 if (ip->flags & HAMMER_INODE_RO)
1325 hammer_start_transaction(&trans, ip->hmp);
1328 if (vap->va_flags != VNOVAL) {
1329 flags = ip->ino_data.uflags;
1330 error = vop_helper_setattr_flags(&flags, vap->va_flags,
1331 hammer_to_unix_xid(&ip->ino_data.uid),
1334 if (ip->ino_data.uflags != flags) {
1335 ip->ino_data.uflags = flags;
1336 modflags |= HAMMER_INODE_DDIRTY;
1338 if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) {
1345 if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) {
1349 if (vap->va_uid != (uid_t)VNOVAL) {
1350 hammer_guid_to_uuid(&uuid, vap->va_uid);
1351 if (bcmp(&uuid, &ip->ino_data.uid, sizeof(uuid)) != 0) {
1352 ip->ino_data.uid = uuid;
1353 modflags |= HAMMER_INODE_DDIRTY;
1356 if (vap->va_gid != (uid_t)VNOVAL) {
1357 hammer_guid_to_uuid(&uuid, vap->va_gid);
1358 if (bcmp(&uuid, &ip->ino_data.gid, sizeof(uuid)) != 0) {
1359 ip->ino_data.gid = uuid;
1360 modflags |= HAMMER_INODE_DDIRTY;
1363 while (vap->va_size != VNOVAL && ip->ino_rec.ino_size != vap->va_size) {
1364 switch(ap->a_vp->v_type) {
1366 if (vap->va_size == ip->ino_rec.ino_size)
1368 if (vap->va_size < ip->ino_rec.ino_size) {
1369 vtruncbuf(ap->a_vp, vap->va_size,
1373 vnode_pager_setsize(ap->a_vp, vap->va_size);
1376 ip->ino_rec.ino_size = vap->va_size;
1377 modflags |= HAMMER_INODE_RDIRTY;
1378 aligned_size = (vap->va_size + HAMMER_BUFMASK) &
1379 ~(int64_t)HAMMER_BUFMASK;
1382 error = hammer_ip_delete_range(&trans, ip,
1384 0x7FFFFFFFFFFFFFFFLL);
1387 * If truncating we have to clean out a portion of
1388 * the last block on-disk.
1390 if (truncating && error == 0 &&
1391 vap->va_size < aligned_size) {
1395 offset = vap->va_size & HAMMER_BUFMASK;
1396 error = bread(ap->a_vp,
1397 aligned_size - HAMMER_BUFSIZE,
1398 HAMMER_BUFSIZE, &bp);
1400 bzero(bp->b_data + offset,
1401 HAMMER_BUFSIZE - offset);
1403 bp->b_tid = trans.tid;
1411 error = hammer_ip_delete_range(&trans, ip,
1413 0x7FFFFFFFFFFFFFFFLL);
1414 ip->ino_rec.ino_size = vap->va_size;
1415 modflags |= HAMMER_INODE_RDIRTY;
1423 if (vap->va_atime.tv_sec != VNOVAL) {
1424 ip->ino_rec.ino_atime =
1425 hammer_timespec_to_transid(&vap->va_atime);
1426 modflags |= HAMMER_INODE_ITIMES;
1428 if (vap->va_mtime.tv_sec != VNOVAL) {
1429 ip->ino_rec.ino_mtime =
1430 hammer_timespec_to_transid(&vap->va_mtime);
1431 modflags |= HAMMER_INODE_ITIMES;
1433 if (vap->va_mode != (mode_t)VNOVAL) {
1434 if (ip->ino_data.mode != vap->va_mode) {
1435 ip->ino_data.mode = vap->va_mode;
1436 modflags |= HAMMER_INODE_DDIRTY;
1441 hammer_abort_transaction(&trans);
1443 hammer_modify_inode(&trans, ip, modflags);
1444 hammer_commit_transaction(&trans);
1450 * hammer_vop_nsymlink { nch, dvp, vpp, cred, vap, target }
1454 hammer_vop_nsymlink(struct vop_nsymlink_args *ap)
1456 struct hammer_transaction trans;
1457 struct hammer_inode *dip;
1458 struct hammer_inode *nip;
1459 struct nchandle *nch;
1460 hammer_record_t record;
1464 ap->a_vap->va_type = VLNK;
1467 dip = VTOI(ap->a_dvp);
1469 if (dip->flags & HAMMER_INODE_RO)
1473 * Create a transaction to cover the operations we perform.
1475 hammer_start_transaction(&trans, dip->hmp);
1478 * Create a new filesystem object of the requested type. The
1479 * returned inode will be referenced but not locked.
1482 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred, dip, &nip);
1484 hammer_abort_transaction(&trans);
1490 * Add the new filesystem object to the directory. This will also
1491 * bump the inode's link count.
1493 error = hammer_ip_add_directory(&trans, dip, nch->ncp, nip);
1496 * Add a record representing the symlink. symlink stores the link
1497 * as pure data, not a string, and is no \0 terminated.
1500 record = hammer_alloc_mem_record(nip);
1501 bytes = strlen(ap->a_target);
1503 record->rec.base.base.key = HAMMER_FIXKEY_SYMLINK;
1504 record->rec.base.base.rec_type = HAMMER_RECTYPE_FIX;
1505 record->rec.base.data_len = bytes;
1506 record->data = (void *)ap->a_target;
1507 /* will be reallocated by routine below */
1508 error = hammer_ip_add_record(&trans, record);
1511 * Set the file size to the length of the link.
1514 nip->ino_rec.ino_size = bytes;
1515 hammer_modify_inode(&trans, nip, HAMMER_INODE_RDIRTY);
1523 hammer_rel_inode(nip, 0);
1524 hammer_abort_transaction(&trans);
1527 hammer_commit_transaction(&trans);
1528 error = hammer_get_vnode(nip, LK_EXCLUSIVE, ap->a_vpp);
1529 hammer_rel_inode(nip, 0);
1531 cache_setunresolved(ap->a_nch);
1532 cache_setvp(ap->a_nch, *ap->a_vpp);
1539 * hammer_vop_nwhiteout { nch, dvp, cred, flags }
1543 hammer_vop_nwhiteout(struct vop_nwhiteout_args *ap)
1545 return(hammer_dounlink(ap->a_nch, ap->a_dvp, ap->a_cred, ap->a_flags));
1549 * hammer_vop_ioctl { vp, command, data, fflag, cred }
1553 hammer_vop_ioctl(struct vop_ioctl_args *ap)
1555 struct hammer_inode *ip = ap->a_vp->v_data;
1557 return(hammer_ioctl(ip, ap->a_command, ap->a_data,
1558 ap->a_fflag, ap->a_cred));
1563 hammer_vop_mountctl(struct vop_mountctl_args *ap)
1568 mp = ap->a_head.a_ops->head.vv_mount;
1571 case MOUNTCTL_SET_EXPORT:
1572 if (ap->a_ctllen != sizeof(struct export_args))
1574 error = hammer_vfs_export(mp, ap->a_op,
1575 (const struct export_args *)ap->a_ctl);
1578 error = journal_mountctl(ap);
1585 * hammer_vop_strategy { vp, bio }
1587 * Strategy call, used for regular file read & write only. Note that the
1588 * bp may represent a cluster.
1590 * To simplify operation and allow better optimizations in the future,
1591 * this code does not make any assumptions with regards to buffer alignment
1596 hammer_vop_strategy(struct vop_strategy_args *ap)
1601 bp = ap->a_bio->bio_buf;
1605 error = hammer_vop_strategy_read(ap);
1608 error = hammer_vop_strategy_write(ap);
1614 bp->b_error = error;
1616 bp->b_flags |= B_ERROR;
1622 * Read from a regular file. Iterate the related records and fill in the
1623 * BIO/BUF. Gaps are zero-filled.
1625 * The support code in hammer_object.c should be used to deal with mixed
1626 * in-memory and on-disk records.
1632 hammer_vop_strategy_read(struct vop_strategy_args *ap)
1634 struct hammer_inode *ip = ap->a_vp->v_data;
1635 struct hammer_cursor cursor;
1636 hammer_record_ondisk_t rec;
1637 hammer_base_elm_t base;
1651 hammer_init_cursor_hmp(&cursor, &ip->cache[0], ip->hmp);
1654 * Key range (begin and end inclusive) to scan. Note that the key's
1655 * stored in the actual records represent BASE+LEN, not BASE. The
1656 * first record containing bio_offset will have a key > bio_offset.
1658 cursor.key_beg.obj_id = ip->obj_id;
1659 cursor.key_beg.create_tid = 0;
1660 cursor.key_beg.delete_tid = 0;
1661 cursor.key_beg.obj_type = 0;
1662 cursor.key_beg.key = bio->bio_offset + 1;
1663 cursor.asof = ip->obj_asof;
1664 cursor.flags |= HAMMER_CURSOR_ASOF | HAMMER_CURSOR_DATAEXTOK;
1666 cursor.key_end = cursor.key_beg;
1667 if (ip->ino_rec.base.base.obj_type == HAMMER_OBJTYPE_DBFILE) {
1668 cursor.key_beg.rec_type = HAMMER_RECTYPE_DB;
1669 cursor.key_end.rec_type = HAMMER_RECTYPE_DB;
1670 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
1672 ran_end = bio->bio_offset + bp->b_bufsize;
1673 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
1674 cursor.key_end.rec_type = HAMMER_RECTYPE_DATA;
1675 tmp64 = ran_end + MAXPHYS + 1; /* work-around GCC-4 bug */
1676 if (tmp64 < ran_end)
1677 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
1679 cursor.key_end.key = ran_end + MAXPHYS + 1;
1681 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
1683 error = hammer_ip_first(&cursor, ip);
1686 while (error == 0) {
1687 error = hammer_ip_resolve_data(&cursor);
1690 rec = cursor.record;
1691 base = &rec->base.base;
1693 rec_offset = base->key - rec->data.base.data_len;
1696 * Calculate the gap, if any, and zero-fill it.
1698 n = (int)(rec_offset - (bio->bio_offset + boff));
1700 if (n > bp->b_bufsize - boff)
1701 n = bp->b_bufsize - boff;
1702 bzero((char *)bp->b_data + boff, n);
1708 * Calculate the data offset in the record and the number
1709 * of bytes we can copy.
1711 * Note there is a degenerate case here where boff may
1712 * already be at bp->b_bufsize.
1715 n = rec->data.base.data_len - roff;
1717 if (n > bp->b_bufsize - boff)
1718 n = bp->b_bufsize - boff;
1719 bcopy((char *)cursor.data + roff,
1720 (char *)bp->b_data + boff, n);
1722 if (boff == bp->b_bufsize)
1724 error = hammer_ip_next(&cursor);
1726 hammer_done_cursor(&cursor);
1729 * There may have been a gap after the last record
1731 if (error == ENOENT)
1733 if (error == 0 && boff != bp->b_bufsize) {
1734 KKASSERT(boff < bp->b_bufsize);
1735 bzero((char *)bp->b_data + boff, bp->b_bufsize - boff);
1736 /* boff = bp->b_bufsize; */
1743 * Write to a regular file. Iterate the related records and mark for
1744 * deletion. If existing edge records (left and right side) overlap our
1745 * write they have to be marked deleted and new records created, usually
1746 * referencing a portion of the original data. Then add a record to
1747 * represent the buffer.
1749 * The support code in hammer_object.c should be used to deal with mixed
1750 * in-memory and on-disk records.
1754 hammer_vop_strategy_write(struct vop_strategy_args *ap)
1756 struct hammer_transaction trans;
1764 ip = ap->a_vp->v_data;
1766 if (ip->flags & HAMMER_INODE_RO)
1770 * Start a transaction using the TID stored with the bp.
1772 KKASSERT(bp->b_tid != 0);
1773 hammer_start_transaction_tid(&trans, ip->hmp, bp->b_tid);
1776 * Delete any records overlapping our range. This function will
1777 * (eventually) properly truncate partial overlaps.
1779 if (ip->ino_rec.base.base.obj_type == HAMMER_OBJTYPE_DBFILE) {
1780 error = hammer_ip_delete_range(&trans, ip, bio->bio_offset,
1783 error = hammer_ip_delete_range(&trans, ip, bio->bio_offset,
1789 * Add a single record to cover the write. We can write a record
1790 * with only the actual file data - for example, a small 200 byte
1791 * file does not have to write out a 16K record.
1793 * While the data size does not have to be aligned, we still do it
1794 * to reduce fragmentation in a future allocation model.
1799 if (ip->ino_rec.ino_size - bio->bio_offset > bp->b_bufsize) {
1800 limit_size = bp->b_bufsize;
1802 limit_size = (int)(ip->ino_rec.ino_size -
1804 KKASSERT(limit_size >= 0);
1805 limit_size = (limit_size + 63) & ~63;
1807 error = hammer_ip_sync_data(&trans, ip, bio->bio_offset,
1808 bp->b_data, limit_size);
1812 * If an error occured abort the transaction
1815 /* XXX undo deletion */
1816 hammer_abort_transaction(&trans);
1817 bp->b_resid = bp->b_bufsize;
1819 hammer_commit_transaction(&trans);
1827 * dounlink - disconnect a directory entry
1829 * XXX whiteout support not really in yet
1832 hammer_dounlink(struct nchandle *nch, struct vnode *dvp, struct ucred *cred,
1835 struct hammer_transaction trans;
1836 struct namecache *ncp;
1839 hammer_record_ondisk_t rec;
1840 struct hammer_cursor cursor;
1845 * Calculate the namekey and setup the key range for the scan. This
1846 * works kinda like a chained hash table where the lower 32 bits
1847 * of the namekey synthesize the chain.
1849 * The key range is inclusive of both key_beg and key_end.
1854 if (dip->flags & HAMMER_INODE_RO)
1857 hammer_start_transaction(&trans, dip->hmp);
1859 namekey = hammer_directory_namekey(ncp->nc_name, ncp->nc_nlen);
1861 hammer_init_cursor_hmp(&cursor, &dip->cache[0], dip->hmp);
1862 cursor.key_beg.obj_id = dip->obj_id;
1863 cursor.key_beg.key = namekey;
1864 cursor.key_beg.create_tid = 0;
1865 cursor.key_beg.delete_tid = 0;
1866 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1867 cursor.key_beg.obj_type = 0;
1869 cursor.key_end = cursor.key_beg;
1870 cursor.key_end.key |= 0xFFFFFFFFULL;
1871 cursor.asof = dip->obj_asof;
1872 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
1875 * Scan all matching records (the chain), locate the one matching
1876 * the requested path component. info->last_error contains the
1877 * error code on search termination and could be 0, ENOENT, or
1880 * The hammer_ip_*() functions merge in-memory records with on-disk
1881 * records for the purposes of the search.
1883 error = hammer_ip_first(&cursor, dip);
1884 while (error == 0) {
1885 error = hammer_ip_resolve_data(&cursor);
1888 rec = cursor.record;
1889 if (ncp->nc_nlen == rec->entry.base.data_len &&
1890 bcmp(ncp->nc_name, cursor.data, ncp->nc_nlen) == 0) {
1893 error = hammer_ip_next(&cursor);
1897 * If all is ok we have to get the inode so we can adjust nlinks.
1899 * If the target is a directory, it must be empty.
1902 ip = hammer_get_inode(dip->hmp, &dip->cache[1],
1904 dip->hmp->asof, 0, &error);
1905 if (error == ENOENT) {
1906 kprintf("obj_id %016llx\n", rec->entry.obj_id);
1907 Debugger("ENOENT unlinking object that should exist, cont to sync");
1908 hammer_sync_hmp(dip->hmp, MNT_NOWAIT);
1909 Debugger("ENOENT - sync done");
1911 if (error == 0 && ip->ino_rec.base.base.obj_type ==
1912 HAMMER_OBJTYPE_DIRECTORY) {
1913 error = hammer_ip_check_directory_empty(&trans, ip);
1916 * WARNING: hammer_ip_del_directory() may have to terminate
1917 * the cursor to avoid a lock recursion. It's ok to call
1918 * hammer_done_cursor() twice.
1921 error = hammer_ip_del_directory(&trans, &cursor, dip, ip);
1923 cache_setunresolved(nch);
1924 cache_setvp(nch, NULL);
1927 cache_inval_vp(ip->vp, CINV_DESTROY);
1929 hammer_rel_inode(ip, 0);
1931 hammer_done_cursor(&cursor);
1932 if (error == EDEADLK)
1936 hammer_commit_transaction(&trans);
1938 hammer_abort_transaction(&trans);
1942 /************************************************************************
1943 * FIFO AND SPECFS OPS *
1944 ************************************************************************
1949 hammer_vop_fifoclose (struct vop_close_args *ap)
1951 /* XXX update itimes */
1952 return (VOCALL(&fifo_vnode_vops, &ap->a_head));
1956 hammer_vop_fiforead (struct vop_read_args *ap)
1960 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
1961 /* XXX update access time */
1966 hammer_vop_fifowrite (struct vop_write_args *ap)
1970 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
1971 /* XXX update access time */
1976 hammer_vop_specclose (struct vop_close_args *ap)
1978 /* XXX update itimes */
1979 return (VOCALL(&spec_vnode_vops, &ap->a_head));
1983 hammer_vop_specread (struct vop_read_args *ap)
1985 /* XXX update access time */
1986 return (VOCALL(&spec_vnode_vops, &ap->a_head));
1990 hammer_vop_specwrite (struct vop_write_args *ap)
1992 /* XXX update last change time */
1993 return (VOCALL(&spec_vnode_vops, &ap->a_head));