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.11 2007/12/30 00:47:22 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 <vm/vm_extern.h>
47 #include <vfs/fifofs/fifo.h>
53 /*static int hammer_vop_vnoperate(struct vop_generic_args *);*/
54 static int hammer_vop_fsync(struct vop_fsync_args *);
55 static int hammer_vop_read(struct vop_read_args *);
56 static int hammer_vop_write(struct vop_write_args *);
57 static int hammer_vop_access(struct vop_access_args *);
58 static int hammer_vop_advlock(struct vop_advlock_args *);
59 static int hammer_vop_close(struct vop_close_args *);
60 static int hammer_vop_ncreate(struct vop_ncreate_args *);
61 static int hammer_vop_getattr(struct vop_getattr_args *);
62 static int hammer_vop_nresolve(struct vop_nresolve_args *);
63 static int hammer_vop_nlookupdotdot(struct vop_nlookupdotdot_args *);
64 static int hammer_vop_nlink(struct vop_nlink_args *);
65 static int hammer_vop_nmkdir(struct vop_nmkdir_args *);
66 static int hammer_vop_nmknod(struct vop_nmknod_args *);
67 static int hammer_vop_open(struct vop_open_args *);
68 static int hammer_vop_pathconf(struct vop_pathconf_args *);
69 static int hammer_vop_print(struct vop_print_args *);
70 static int hammer_vop_readdir(struct vop_readdir_args *);
71 static int hammer_vop_readlink(struct vop_readlink_args *);
72 static int hammer_vop_nremove(struct vop_nremove_args *);
73 static int hammer_vop_nrename(struct vop_nrename_args *);
74 static int hammer_vop_nrmdir(struct vop_nrmdir_args *);
75 static int hammer_vop_setattr(struct vop_setattr_args *);
76 static int hammer_vop_strategy(struct vop_strategy_args *);
77 static int hammer_vop_nsymlink(struct vop_nsymlink_args *);
78 static int hammer_vop_nwhiteout(struct vop_nwhiteout_args *);
80 static int hammer_vop_fifoclose (struct vop_close_args *);
81 static int hammer_vop_fiforead (struct vop_read_args *);
82 static int hammer_vop_fifowrite (struct vop_write_args *);
84 static int hammer_vop_specclose (struct vop_close_args *);
85 static int hammer_vop_specread (struct vop_read_args *);
86 static int hammer_vop_specwrite (struct vop_write_args *);
88 struct vop_ops hammer_vnode_vops = {
89 .vop_default = vop_defaultop,
90 .vop_fsync = hammer_vop_fsync,
91 .vop_getpages = vop_stdgetpages,
92 .vop_putpages = vop_stdputpages,
93 .vop_read = hammer_vop_read,
94 .vop_write = hammer_vop_write,
95 .vop_access = hammer_vop_access,
96 .vop_advlock = hammer_vop_advlock,
97 .vop_close = hammer_vop_close,
98 .vop_ncreate = hammer_vop_ncreate,
99 .vop_getattr = hammer_vop_getattr,
100 .vop_inactive = hammer_vop_inactive,
101 .vop_reclaim = hammer_vop_reclaim,
102 .vop_nresolve = hammer_vop_nresolve,
103 .vop_nlookupdotdot = hammer_vop_nlookupdotdot,
104 .vop_nlink = hammer_vop_nlink,
105 .vop_nmkdir = hammer_vop_nmkdir,
106 .vop_nmknod = hammer_vop_nmknod,
107 .vop_open = hammer_vop_open,
108 .vop_pathconf = hammer_vop_pathconf,
109 .vop_print = hammer_vop_print,
110 .vop_readdir = hammer_vop_readdir,
111 .vop_readlink = hammer_vop_readlink,
112 .vop_nremove = hammer_vop_nremove,
113 .vop_nrename = hammer_vop_nrename,
114 .vop_nrmdir = hammer_vop_nrmdir,
115 .vop_setattr = hammer_vop_setattr,
116 .vop_strategy = hammer_vop_strategy,
117 .vop_nsymlink = hammer_vop_nsymlink,
118 .vop_nwhiteout = hammer_vop_nwhiteout
121 struct vop_ops hammer_spec_vops = {
122 .vop_default = spec_vnoperate,
123 .vop_fsync = hammer_vop_fsync,
124 .vop_read = hammer_vop_specread,
125 .vop_write = hammer_vop_specwrite,
126 .vop_access = hammer_vop_access,
127 .vop_close = hammer_vop_specclose,
128 .vop_getattr = hammer_vop_getattr,
129 .vop_inactive = hammer_vop_inactive,
130 .vop_reclaim = hammer_vop_reclaim,
131 .vop_setattr = hammer_vop_setattr
134 struct vop_ops hammer_fifo_vops = {
135 .vop_default = fifo_vnoperate,
136 .vop_fsync = hammer_vop_fsync,
137 .vop_read = hammer_vop_fiforead,
138 .vop_write = hammer_vop_fifowrite,
139 .vop_access = hammer_vop_access,
140 .vop_close = hammer_vop_fifoclose,
141 .vop_getattr = hammer_vop_getattr,
142 .vop_inactive = hammer_vop_inactive,
143 .vop_reclaim = hammer_vop_reclaim,
144 .vop_setattr = hammer_vop_setattr
147 static int hammer_dounlink(struct nchandle *nch, struct vnode *dvp,
148 struct ucred *cred, int flags);
149 static int hammer_vop_strategy_read(struct vop_strategy_args *ap);
150 static int hammer_vop_strategy_write(struct vop_strategy_args *ap);
155 hammer_vop_vnoperate(struct vop_generic_args *)
157 return (VOCALL(&hammer_vnode_vops, ap));
162 * hammer_vop_fsync { vp, waitfor }
166 hammer_vop_fsync(struct vop_fsync_args *ap)
172 error = hammer_sync_inode(ip, ap->a_waitfor, 0);
177 * hammer_vop_read { vp, uio, ioflag, cred }
181 hammer_vop_read(struct vop_read_args *ap)
183 struct hammer_transaction trans;
192 if (ap->a_vp->v_type != VREG)
196 seqcount = ap->a_ioflag >> 16;
198 hammer_start_transaction(&trans, ip->hmp);
201 * Access the data in HAMMER_BUFSIZE blocks via the buffer cache.
204 while (uio->uio_resid > 0 && uio->uio_offset < ip->ino_rec.ino_size) {
205 offset = uio->uio_offset & HAMMER_BUFMASK;
207 error = cluster_read(ap->a_vp, ip->ino_rec.ino_size,
208 uio->uio_offset - offset, HAMMER_BUFSIZE,
209 MAXBSIZE, seqcount, &bp);
211 error = bread(ap->a_vp, uio->uio_offset - offset,
212 HAMMER_BUFSIZE, &bp);
217 /* bp->b_flags |= B_CLUSTEROK; temporarily disabled */
218 n = HAMMER_BUFSIZE - offset;
219 if (n > uio->uio_resid)
221 if (n > ip->ino_rec.ino_size - uio->uio_offset)
222 n = (int)(ip->ino_rec.ino_size - uio->uio_offset);
223 error = uiomove((char *)bp->b_data + offset, n, uio);
228 ip->ino_rec.ino_atime = trans.tid;
229 hammer_modify_inode(&trans, ip, HAMMER_INODE_ITIMES);
232 hammer_commit_transaction(&trans);
237 * hammer_vop_write { vp, uio, ioflag, cred }
241 hammer_vop_write(struct vop_write_args *ap)
243 struct hammer_transaction trans;
244 struct hammer_inode *ip;
252 if (ap->a_vp->v_type != VREG)
258 * Create a transaction to cover the operations we perform.
260 hammer_start_transaction(&trans, ip->hmp);
266 if (ap->a_ioflag & IO_APPEND)
267 uio->uio_offset = ip->ino_rec.ino_size;
270 * Check for illegal write offsets. Valid range is 0...2^63-1
272 if (uio->uio_offset < 0 || uio->uio_offset + uio->uio_resid <= 0)
276 * Access the data in HAMMER_BUFSIZE blocks via the buffer cache.
278 while (uio->uio_resid > 0) {
279 offset = uio->uio_offset & HAMMER_BUFMASK;
280 if (uio->uio_segflg == UIO_NOCOPY) {
282 * Issuing a write with the same data backing the
283 * buffer. Instantiate the buffer to collect the
284 * backing vm pages, then read-in any missing bits.
286 * This case is used by vop_stdputpages().
288 bp = getblk(ap->a_vp, uio->uio_offset, HAMMER_BUFSIZE,
290 if ((bp->b_flags & B_CACHE) == 0) {
292 error = bread(ap->a_vp,
293 uio->uio_offset - offset,
294 HAMMER_BUFSIZE, &bp);
300 } else if (offset == 0 && uio->uio_resid >= HAMMER_BUFSIZE) {
302 * entirely overwrite the buffer
304 bp = getblk(ap->a_vp, uio->uio_offset, HAMMER_BUFSIZE,
306 } else if (offset == 0 && uio->uio_offset >= ip->ino_rec.ino_size) {
310 bp = getblk(ap->a_vp, uio->uio_offset, HAMMER_BUFSIZE,
315 * Partial overwrite, read in any missing bits then
316 * replace the portion being written.
318 error = bread(ap->a_vp, uio->uio_offset - offset,
319 HAMMER_BUFSIZE, &bp);
325 n = HAMMER_BUFSIZE - offset;
326 if (n > uio->uio_resid)
328 error = uiomove((char *)bp->b_data + offset, n, uio);
333 /* bp->b_flags |= B_CLUSTEROK; temporarily disabled */
334 if (ip->ino_rec.ino_size < uio->uio_offset) {
335 ip->ino_rec.ino_size = uio->uio_offset;
336 ip->ino_rec.ino_mtime = trans.tid;
337 flags = HAMMER_INODE_RDIRTY | HAMMER_INODE_ITIMES |
339 vnode_pager_setsize(ap->a_vp, ip->ino_rec.ino_size);
341 flags = HAMMER_INODE_TID;
343 hammer_modify_inode(&trans, ip, flags);
344 if (ap->a_ioflag & IO_SYNC) {
346 } else if (ap->a_ioflag & IO_DIRECT) {
353 hammer_abort_transaction(&trans);
355 hammer_commit_transaction(&trans);
360 * hammer_vop_access { vp, mode, cred }
364 hammer_vop_access(struct vop_access_args *ap)
366 struct hammer_inode *ip = VTOI(ap->a_vp);
371 uid = hammer_to_unix_xid(&ip->ino_data.uid);
372 gid = hammer_to_unix_xid(&ip->ino_data.gid);
374 error = vop_helper_access(ap, uid, gid, ip->ino_data.mode,
375 ip->ino_data.uflags);
380 * hammer_vop_advlock { vp, id, op, fl, flags }
384 hammer_vop_advlock(struct vop_advlock_args *ap)
386 struct hammer_inode *ip = VTOI(ap->a_vp);
388 return (lf_advlock(ap, &ip->advlock, ip->ino_rec.ino_size));
392 * hammer_vop_close { vp, fflag }
396 hammer_vop_close(struct vop_close_args *ap)
398 return (vop_stdclose(ap));
402 * hammer_vop_ncreate { nch, dvp, vpp, cred, vap }
404 * The operating system has already ensured that the directory entry
405 * does not exist and done all appropriate namespace locking.
409 hammer_vop_ncreate(struct vop_ncreate_args *ap)
411 struct hammer_transaction trans;
412 struct hammer_inode *dip;
413 struct hammer_inode *nip;
414 struct nchandle *nch;
418 dip = VTOI(ap->a_dvp);
421 * Create a transaction to cover the operations we perform.
423 hammer_start_transaction(&trans, dip->hmp);
426 * Create a new filesystem object of the requested type. The
427 * returned inode will be referenced but not locked.
430 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred, dip, &nip);
432 hammer_abort_transaction(&trans);
438 * Add the new filesystem object to the directory. This will also
439 * bump the inode's link count.
441 error = hammer_ip_add_directory(&trans, dip, nch->ncp, nip);
447 hammer_rel_inode(nip, 0);
448 hammer_abort_transaction(&trans);
451 hammer_commit_transaction(&trans);
452 error = hammer_get_vnode(nip, LK_EXCLUSIVE, ap->a_vpp);
453 hammer_rel_inode(nip, 0);
455 cache_setunresolved(ap->a_nch);
456 cache_setvp(ap->a_nch, *ap->a_vpp);
463 * hammer_vop_getattr { vp, vap }
467 hammer_vop_getattr(struct vop_getattr_args *ap)
469 struct hammer_inode *ip = VTOI(ap->a_vp);
470 struct vattr *vap = ap->a_vap;
473 if (cache_check_fsmid_vp(ap->a_vp, &ip->fsmid) &&
474 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0 &&
479 hammer_itimes(ap->a_vp);
482 vap->va_fsid = ip->hmp->fsid_udev;
483 vap->va_fileid = ip->ino_rec.base.base.obj_id;
484 vap->va_mode = ip->ino_data.mode;
485 vap->va_nlink = ip->ino_rec.ino_nlinks;
486 vap->va_uid = hammer_to_unix_xid(&ip->ino_data.uid);
487 vap->va_gid = hammer_to_unix_xid(&ip->ino_data.gid);
490 vap->va_size = ip->ino_rec.ino_size;
491 hammer_to_timespec(ip->ino_rec.ino_atime, &vap->va_atime);
492 hammer_to_timespec(ip->ino_rec.ino_mtime, &vap->va_mtime);
493 hammer_to_timespec(ip->ino_data.ctime, &vap->va_ctime);
494 vap->va_flags = ip->ino_data.uflags;
495 vap->va_gen = 1; /* hammer inums are unique for all time */
496 vap->va_blocksize = 32768; /* XXX - extract from root volume */
497 vap->va_bytes = ip->ino_rec.ino_size;
498 vap->va_type = hammer_get_vnode_type(ip->ino_rec.base.base.obj_type);
499 vap->va_filerev = 0; /* XXX */
500 /* mtime uniquely identifies any adjustments made to the file */
501 vap->va_fsmid = ip->ino_rec.ino_mtime;
502 vap->va_uid_uuid = ip->ino_data.uid;
503 vap->va_gid_uuid = ip->ino_data.gid;
504 vap->va_fsid_uuid = ip->hmp->fsid;
505 vap->va_vaflags = VA_UID_UUID_VALID | VA_GID_UUID_VALID |
508 switch (ip->ino_rec.base.base.obj_type) {
509 case HAMMER_OBJTYPE_CDEV:
510 case HAMMER_OBJTYPE_BDEV:
511 vap->va_rmajor = ip->ino_data.rmajor;
512 vap->va_rminor = ip->ino_data.rminor;
522 * hammer_vop_nresolve { nch, dvp, cred }
524 * Locate the requested directory entry.
528 hammer_vop_nresolve(struct vop_nresolve_args *ap)
530 struct namecache *ncp;
534 struct hammer_cursor cursor;
535 union hammer_record_ondisk *rec;
543 * Misc initialization, plus handle as-of name extensions. Look for
544 * the '@@' extension. Note that as-of files and directories cannot
549 dip = VTOI(ap->a_dvp);
550 ncp = ap->a_nch->ncp;
551 asof = dip->obj_asof;
554 for (i = 0; i < nlen; ++i) {
555 if (ncp->nc_name[i] == '@' && ncp->nc_name[i+1] == '@') {
556 asof = hammer_now_tid() -
557 strtoq(ncp->nc_name + i + 2, NULL, 0) *
559 kprintf("ASOF %016llx\n", asof);
566 * Calculate the namekey and setup the key range for the scan. This
567 * works kinda like a chained hash table where the lower 32 bits
568 * of the namekey synthesize the chain.
570 * The key range is inclusive of both key_beg and key_end.
572 namekey = hammer_directory_namekey(ncp->nc_name, nlen);
574 hammer_init_cursor_ip(&cursor, dip);
575 cursor.key_beg.obj_id = dip->obj_id;
576 cursor.key_beg.key = namekey;
577 cursor.key_beg.create_tid = asof;
578 cursor.key_beg.delete_tid = 0;
579 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
580 cursor.key_beg.obj_type = 0;
582 cursor.key_end = cursor.key_beg;
583 cursor.key_end.key |= 0xFFFFFFFFULL;
584 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
587 * Scan all matching records (the chain), locate the one matching
588 * the requested path component.
590 * The hammer_ip_*() functions merge in-memory records with on-disk
591 * records for the purposes of the search.
593 error = hammer_ip_first(&cursor, dip);
595 error = hammer_ip_resolve_data(&cursor);
599 if (nlen == rec->entry.base.data_len &&
600 bcmp(ncp->nc_name, cursor.data, nlen) == 0) {
603 error = hammer_ip_next(&cursor);
606 ip = hammer_get_inode(dip->hmp, rec->entry.obj_id,
609 error = hammer_get_vnode(ip, LK_EXCLUSIVE, &vp);
610 hammer_rel_inode(ip, 0);
616 cache_setvp(ap->a_nch, vp);
619 } else if (error == ENOENT) {
620 cache_setvp(ap->a_nch, NULL);
622 hammer_done_cursor(&cursor);
627 * hammer_vop_nlookupdotdot { dvp, vpp, cred }
629 * Locate the parent directory of a directory vnode.
631 * dvp is referenced but not locked. *vpp must be returned referenced and
632 * locked. A parent_obj_id of 0 does not necessarily indicate that we are
633 * at the root, instead it could indicate that the directory we were in was
638 hammer_vop_nlookupdotdot(struct vop_nlookupdotdot_args *ap)
640 struct hammer_inode *dip;
641 u_int64_t parent_obj_id;
643 dip = VTOI(ap->a_dvp);
644 if ((parent_obj_id = dip->ino_data.parent_obj_id) == 0) {
648 return(hammer_vfs_vget(dip->hmp->mp, parent_obj_id, ap->a_vpp));
652 * hammer_vop_nlink { nch, dvp, vp, cred }
656 hammer_vop_nlink(struct vop_nlink_args *ap)
658 struct hammer_transaction trans;
659 struct hammer_inode *dip;
660 struct hammer_inode *ip;
661 struct nchandle *nch;
665 dip = VTOI(ap->a_dvp);
669 * Create a transaction to cover the operations we perform.
671 hammer_start_transaction(&trans, dip->hmp);
674 * Add the filesystem object to the directory. Note that neither
675 * dip nor ip are referenced or locked, but their vnodes are
676 * referenced. This function will bump the inode's link count.
678 error = hammer_ip_add_directory(&trans, dip, nch->ncp, ip);
684 hammer_abort_transaction(&trans);
686 cache_setunresolved(nch);
687 cache_setvp(nch, ap->a_vp);
688 hammer_commit_transaction(&trans);
694 * hammer_vop_nmkdir { nch, dvp, vpp, cred, vap }
696 * The operating system has already ensured that the directory entry
697 * does not exist and done all appropriate namespace locking.
701 hammer_vop_nmkdir(struct vop_nmkdir_args *ap)
703 struct hammer_transaction trans;
704 struct hammer_inode *dip;
705 struct hammer_inode *nip;
706 struct nchandle *nch;
710 dip = VTOI(ap->a_dvp);
713 * Create a transaction to cover the operations we perform.
715 hammer_start_transaction(&trans, dip->hmp);
718 * Create a new filesystem object of the requested type. The
719 * returned inode will be referenced but not locked.
721 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred, dip, &nip);
723 hammer_abort_transaction(&trans);
729 * Add the new filesystem object to the directory. This will also
730 * bump the inode's link count.
732 error = hammer_ip_add_directory(&trans, dip, nch->ncp, nip);
738 hammer_rel_inode(nip, 0);
739 hammer_abort_transaction(&trans);
742 hammer_commit_transaction(&trans);
743 error = hammer_get_vnode(nip, LK_EXCLUSIVE, ap->a_vpp);
744 hammer_rel_inode(nip, 0);
746 cache_setunresolved(ap->a_nch);
747 cache_setvp(ap->a_nch, *ap->a_vpp);
754 * hammer_vop_nmknod { nch, dvp, vpp, cred, vap }
756 * The operating system has already ensured that the directory entry
757 * does not exist and done all appropriate namespace locking.
761 hammer_vop_nmknod(struct vop_nmknod_args *ap)
763 struct hammer_transaction trans;
764 struct hammer_inode *dip;
765 struct hammer_inode *nip;
766 struct nchandle *nch;
770 dip = VTOI(ap->a_dvp);
773 * Create a transaction to cover the operations we perform.
775 hammer_start_transaction(&trans, dip->hmp);
778 * Create a new filesystem object of the requested type. The
779 * returned inode will be referenced but not locked.
781 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred, dip, &nip);
783 hammer_abort_transaction(&trans);
789 * Add the new filesystem object to the directory. This will also
790 * bump the inode's link count.
792 error = hammer_ip_add_directory(&trans, dip, nch->ncp, nip);
798 hammer_rel_inode(nip, 0);
799 hammer_abort_transaction(&trans);
802 hammer_commit_transaction(&trans);
803 error = hammer_get_vnode(nip, LK_EXCLUSIVE, ap->a_vpp);
804 hammer_rel_inode(nip, 0);
806 cache_setunresolved(ap->a_nch);
807 cache_setvp(ap->a_nch, *ap->a_vpp);
814 * hammer_vop_open { vp, mode, cred, fp }
818 hammer_vop_open(struct vop_open_args *ap)
820 return(vop_stdopen(ap));
824 * hammer_vop_pathconf { vp, name, retval }
828 hammer_vop_pathconf(struct vop_pathconf_args *ap)
834 * hammer_vop_print { vp }
838 hammer_vop_print(struct vop_print_args *ap)
844 * hammer_vop_readdir { vp, uio, cred, *eofflag, *ncookies, off_t **cookies }
848 hammer_vop_readdir(struct vop_readdir_args *ap)
850 struct hammer_cursor cursor;
851 struct hammer_inode *ip;
853 hammer_record_ondisk_t rec;
854 hammer_base_elm_t base;
864 hammer_init_cursor_ip(&cursor, ip);
867 * Key range (begin and end inclusive) to scan. Directory keys
868 * directly translate to a 64 bit 'seek' position.
870 cursor.key_beg.obj_id = ip->obj_id;
871 cursor.key_beg.create_tid = ip->obj_asof;
872 cursor.key_beg.delete_tid = 0;
873 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
874 cursor.key_beg.obj_type = 0;
875 cursor.key_beg.key = uio->uio_offset;
877 cursor.key_end = cursor.key_beg;
878 cursor.key_end.key = HAMMER_MAX_KEY;
879 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
881 if (ap->a_ncookies) {
882 ncookies = uio->uio_resid / 16 + 1;
885 cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
893 saveoff = cursor.key_beg.key;
894 error = hammer_ip_first(&cursor, ip);
897 error = hammer_ip_resolve_data(&cursor);
901 base = &rec->base.base;
904 if (base->obj_id != ip->obj_id)
905 panic("readdir: bad record at %p", cursor.node);
907 r = vop_write_dirent(
908 &error, uio, rec->entry.obj_id,
909 hammer_get_dtype(rec->entry.base.base.obj_type),
910 rec->entry.base.data_len,
911 (void *)cursor.data);
916 cookies[cookie_index] = base->key;
918 if (cookie_index == ncookies)
920 error = hammer_ip_next(&cursor);
922 hammer_done_cursor(&cursor);
925 *ap->a_eofflag = (error == ENOENT);
926 uio->uio_offset = saveoff;
927 if (error && cookie_index == 0) {
929 kfree(cookies, M_TEMP);
931 *ap->a_cookies = NULL;
937 *ap->a_ncookies = cookie_index;
938 *ap->a_cookies = cookies;
945 * hammer_vop_readlink { vp, uio, cred }
949 hammer_vop_readlink(struct vop_readlink_args *ap)
951 struct hammer_cursor cursor;
952 struct hammer_inode *ip;
956 hammer_init_cursor_ip(&cursor, ip);
959 * Key range (begin and end inclusive) to scan. Directory keys
960 * directly translate to a 64 bit 'seek' position.
962 cursor.key_beg.obj_id = ip->obj_id;
963 cursor.key_beg.create_tid = ip->obj_asof;
964 cursor.key_beg.delete_tid = 0;
965 cursor.key_beg.rec_type = HAMMER_RECTYPE_FIX;
966 cursor.key_beg.obj_type = 0;
967 cursor.key_beg.key = HAMMER_FIXKEY_SYMLINK;
969 error = hammer_ip_lookup(&cursor, ip);
971 error = hammer_ip_resolve_data(&cursor);
973 error = uiomove((char *)cursor.data,
974 cursor.record->generic.base.data_len,
978 hammer_done_cursor(&cursor);
983 * hammer_vop_nremove { nch, dvp, cred }
987 hammer_vop_nremove(struct vop_nremove_args *ap)
989 return(hammer_dounlink(ap->a_nch, ap->a_dvp, ap->a_cred, 0));
993 * hammer_vop_nrename { fnch, tnch, fdvp, tdvp, cred }
997 hammer_vop_nrename(struct vop_nrename_args *ap)
999 struct hammer_transaction trans;
1000 struct namecache *fncp;
1001 struct namecache *tncp;
1002 struct hammer_inode *fdip;
1003 struct hammer_inode *tdip;
1004 struct hammer_inode *ip;
1005 struct hammer_cursor cursor;
1006 union hammer_record_ondisk *rec;
1010 fdip = VTOI(ap->a_fdvp);
1011 tdip = VTOI(ap->a_tdvp);
1012 fncp = ap->a_fnch->ncp;
1013 tncp = ap->a_tnch->ncp;
1014 hammer_start_transaction(&trans, fdip->hmp);
1017 * Extract the hammer_inode from fncp and add link to the target
1020 ip = VTOI(fncp->nc_vp);
1021 KKASSERT(ip != NULL);
1023 error = hammer_ip_add_directory(&trans, tdip, tncp, ip);
1026 * Locate the record in the originating directory and remove it.
1028 * Calculate the namekey and setup the key range for the scan. This
1029 * works kinda like a chained hash table where the lower 32 bits
1030 * of the namekey synthesize the chain.
1032 * The key range is inclusive of both key_beg and key_end.
1034 namekey = hammer_directory_namekey(fncp->nc_name, fncp->nc_nlen);
1036 hammer_init_cursor_ip(&cursor, fdip);
1037 cursor.key_beg.obj_id = fdip->obj_id;
1038 cursor.key_beg.key = namekey;
1039 cursor.key_beg.create_tid = fdip->obj_asof;
1040 cursor.key_beg.delete_tid = 0;
1041 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1042 cursor.key_beg.obj_type = 0;
1044 cursor.key_end = cursor.key_beg;
1045 cursor.key_end.key |= 0xFFFFFFFFULL;
1046 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
1049 * Scan all matching records (the chain), locate the one matching
1050 * the requested path component.
1052 * The hammer_ip_*() functions merge in-memory records with on-disk
1053 * records for the purposes of the search.
1055 error = hammer_ip_first(&cursor, fdip);
1056 while (error == 0) {
1057 if (hammer_ip_resolve_data(&cursor) != 0)
1059 rec = cursor.record;
1060 if (fncp->nc_nlen == rec->entry.base.data_len &&
1061 bcmp(fncp->nc_name, cursor.data, fncp->nc_nlen) == 0) {
1064 error = hammer_ip_next(&cursor);
1068 * If all is ok we have to get the inode so we can adjust nlinks.
1072 error = hammer_ip_del_directory(&trans, &cursor, fdip, ip);
1074 cache_rename(ap->a_fnch, ap->a_tnch);
1075 cache_setvp(ap->a_tnch, ip->vp);
1078 hammer_done_cursor(&cursor);
1080 hammer_commit_transaction(&trans);
1082 hammer_abort_transaction(&trans);
1088 * hammer_vop_nrmdir { nch, dvp, cred }
1092 hammer_vop_nrmdir(struct vop_nrmdir_args *ap)
1094 /* XXX check that directory is empty */
1096 return(hammer_dounlink(ap->a_nch, ap->a_dvp, ap->a_cred, 0));
1100 * hammer_vop_setattr { vp, vap, cred }
1104 hammer_vop_setattr(struct vop_setattr_args *ap)
1106 struct hammer_transaction trans;
1107 struct hammer_cursor *spike = NULL;
1109 struct hammer_inode *ip;
1112 int64_t aligned_size;
1117 ip = ap->a_vp->v_data;
1120 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
1123 hammer_start_transaction(&trans, ip->hmp);
1126 if (vap->va_flags != VNOVAL) {
1127 flags = ip->ino_data.uflags;
1128 error = vop_helper_setattr_flags(&flags, vap->va_flags,
1129 hammer_to_unix_xid(&ip->ino_data.uid),
1132 if (ip->ino_data.uflags != flags) {
1133 ip->ino_data.uflags = flags;
1134 modflags |= HAMMER_INODE_DDIRTY;
1136 if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) {
1143 if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) {
1147 if (vap->va_uid != (uid_t)VNOVAL) {
1148 hammer_guid_to_uuid(&uuid, vap->va_uid);
1149 if (bcmp(&uuid, &ip->ino_data.uid, sizeof(uuid)) != 0) {
1150 ip->ino_data.uid = uuid;
1151 modflags |= HAMMER_INODE_DDIRTY;
1154 if (vap->va_gid != (uid_t)VNOVAL) {
1155 hammer_guid_to_uuid(&uuid, vap->va_gid);
1156 if (bcmp(&uuid, &ip->ino_data.gid, sizeof(uuid)) != 0) {
1157 ip->ino_data.gid = uuid;
1158 modflags |= HAMMER_INODE_DDIRTY;
1161 while (vap->va_size != VNOVAL && ip->ino_rec.ino_size != vap->va_size) {
1162 switch(ap->a_vp->v_type) {
1164 if (vap->va_size < ip->ino_rec.ino_size) {
1165 vtruncbuf(ap->a_vp, vap->va_size,
1167 } else if (vap->va_size > ip->ino_rec.ino_size) {
1168 vnode_pager_setsize(ap->a_vp, vap->va_size);
1170 aligned_size = (vap->va_size + HAMMER_BUFMASK) &
1171 ~(int64_t)HAMMER_BUFMASK;
1172 error = hammer_ip_delete_range(&trans, ip,
1174 0x7FFFFFFFFFFFFFFFLL,
1176 ip->ino_rec.ino_size = vap->va_size;
1177 modflags |= HAMMER_INODE_RDIRTY;
1180 error = hammer_ip_delete_range(&trans, ip,
1182 0x7FFFFFFFFFFFFFFFLL,
1184 ip->ino_rec.ino_size = vap->va_size;
1185 modflags |= HAMMER_INODE_RDIRTY;
1191 if (error == ENOSPC) {
1192 error = hammer_spike(&spike);
1196 KKASSERT(spike == NULL);
1199 if (vap->va_atime.tv_sec != VNOVAL) {
1200 ip->ino_rec.ino_atime =
1201 hammer_timespec_to_transid(&vap->va_atime);
1202 modflags |= HAMMER_INODE_ITIMES;
1204 if (vap->va_mtime.tv_sec != VNOVAL) {
1205 ip->ino_rec.ino_mtime =
1206 hammer_timespec_to_transid(&vap->va_mtime);
1207 modflags |= HAMMER_INODE_ITIMES;
1209 if (vap->va_mode != (mode_t)VNOVAL) {
1210 if (ip->ino_data.mode != vap->va_mode) {
1211 ip->ino_data.mode = vap->va_mode;
1212 modflags |= HAMMER_INODE_DDIRTY;
1217 hammer_abort_transaction(&trans);
1219 if (modflags & (HAMMER_INODE_RDIRTY | HAMMER_INODE_DDIRTY))
1220 modflags |= HAMMER_INODE_TID;
1221 hammer_modify_inode(&trans, ip, modflags);
1222 hammer_commit_transaction(&trans);
1228 * hammer_vop_nsymlink { nch, dvp, vpp, cred, vap, target }
1232 hammer_vop_nsymlink(struct vop_nsymlink_args *ap)
1234 struct hammer_transaction trans;
1235 struct hammer_inode *dip;
1236 struct hammer_inode *nip;
1237 struct nchandle *nch;
1238 hammer_record_t record;
1242 ap->a_vap->va_type = VLNK;
1245 dip = VTOI(ap->a_dvp);
1248 * Create a transaction to cover the operations we perform.
1250 hammer_start_transaction(&trans, dip->hmp);
1253 * Create a new filesystem object of the requested type. The
1254 * returned inode will be referenced but not locked.
1257 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred, dip, &nip);
1259 hammer_abort_transaction(&trans);
1265 * Add the new filesystem object to the directory. This will also
1266 * bump the inode's link count.
1268 error = hammer_ip_add_directory(&trans, dip, nch->ncp, nip);
1271 * Add a record representing the symlink. symlink stores the link
1272 * as pure data, not a string, and is no \0 terminated.
1275 record = hammer_alloc_mem_record(nip);
1276 bytes = strlen(ap->a_target);
1278 record->rec.generic.base.base.key = HAMMER_FIXKEY_SYMLINK;
1279 record->rec.generic.base.base.rec_type = HAMMER_RECTYPE_FIX;
1280 record->rec.generic.base.data_len = bytes;
1281 if (bytes <= sizeof(record->rec.generic.filler)) {
1282 record->data = (void *)record->rec.generic.filler;
1283 bcopy(ap->a_target, record->data, bytes);
1285 record->data = (void *)ap->a_target;
1286 /* will be reallocated by routine below */
1288 error = hammer_ip_add_record(&trans, record);
1295 hammer_rel_inode(nip, 0);
1296 hammer_abort_transaction(&trans);
1299 hammer_commit_transaction(&trans);
1300 error = hammer_get_vnode(nip, LK_EXCLUSIVE, ap->a_vpp);
1301 hammer_rel_inode(nip, 0);
1303 cache_setunresolved(ap->a_nch);
1304 cache_setvp(ap->a_nch, *ap->a_vpp);
1311 * hammer_vop_nwhiteout { nch, dvp, cred, flags }
1315 hammer_vop_nwhiteout(struct vop_nwhiteout_args *ap)
1317 return(hammer_dounlink(ap->a_nch, ap->a_dvp, ap->a_cred, ap->a_flags));
1321 * hammer_vop_strategy { vp, bio }
1323 * Strategy call, used for regular file read & write only. Note that the
1324 * bp may represent a cluster.
1326 * To simplify operation and allow better optimizations in the future,
1327 * this code does not make any assumptions with regards to buffer alignment
1332 hammer_vop_strategy(struct vop_strategy_args *ap)
1337 bp = ap->a_bio->bio_buf;
1341 error = hammer_vop_strategy_read(ap);
1344 error = hammer_vop_strategy_write(ap);
1350 bp->b_error = error;
1352 bp->b_flags |= B_ERROR;
1358 * Read from a regular file. Iterate the related records and fill in the
1359 * BIO/BUF. Gaps are zero-filled.
1361 * The support code in hammer_object.c should be used to deal with mixed
1362 * in-memory and on-disk records.
1368 hammer_vop_strategy_read(struct vop_strategy_args *ap)
1370 struct hammer_inode *ip = ap->a_vp->v_data;
1371 struct hammer_cursor cursor;
1372 hammer_record_ondisk_t rec;
1373 hammer_base_elm_t base;
1387 hammer_init_cursor_ip(&cursor, ip);
1390 * Key range (begin and end inclusive) to scan. Note that the key's
1391 * stored in the actual records represent BASE+LEN, not BASE. The
1392 * first record containing bio_offset will have a key > bio_offset.
1394 cursor.key_beg.obj_id = ip->obj_id;
1395 cursor.key_beg.create_tid = ip->obj_asof;
1396 cursor.key_beg.delete_tid = 0;
1397 cursor.key_beg.obj_type = 0;
1398 cursor.key_beg.key = bio->bio_offset + 1;
1400 cursor.key_end = cursor.key_beg;
1401 if (ip->ino_rec.base.base.obj_type == HAMMER_OBJTYPE_DBFILE) {
1402 cursor.key_beg.rec_type = HAMMER_RECTYPE_DB;
1403 cursor.key_end.rec_type = HAMMER_RECTYPE_DB;
1404 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
1406 ran_end = bio->bio_offset + bp->b_bufsize;
1407 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
1408 cursor.key_end.rec_type = HAMMER_RECTYPE_DATA;
1409 tmp64 = ran_end + MAXPHYS + 1; /* work-around GCC-4 bug */
1410 if (tmp64 < ran_end)
1411 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
1413 cursor.key_end.key = ran_end + MAXPHYS + 1;
1415 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
1417 error = hammer_ip_first(&cursor, ip);
1420 while (error == 0) {
1421 error = hammer_ip_resolve_data(&cursor);
1424 rec = cursor.record;
1425 base = &rec->base.base;
1427 rec_offset = base->key - rec->data.base.data_len;
1430 * Calculate the gap, if any, and zero-fill it.
1432 n = (int)(rec_offset - (bio->bio_offset + boff));
1434 if (n > bp->b_bufsize - boff)
1435 n = bp->b_bufsize - boff;
1436 kprintf("zfill %d bytes\n", n);
1437 bzero((char *)bp->b_data + boff, n);
1443 * Calculate the data offset in the record and the number
1444 * of bytes we can copy.
1446 * Note there is a degenerate case here where boff may
1447 * already be at bp->b_bufsize.
1450 n = rec->data.base.data_len - roff;
1452 if (n > bp->b_bufsize - boff)
1453 n = bp->b_bufsize - boff;
1454 bcopy((char *)cursor.data + roff, (char *)bp->b_data + boff, n);
1456 if (boff == bp->b_bufsize)
1458 error = hammer_ip_next(&cursor);
1460 hammer_done_cursor(&cursor);
1463 * There may have been a gap after the last record
1465 if (error == ENOENT)
1467 if (error == 0 && boff != bp->b_bufsize) {
1468 KKASSERT(boff < bp->b_bufsize);
1469 bzero((char *)bp->b_data + boff, bp->b_bufsize - boff);
1470 /* boff = bp->b_bufsize; */
1477 * Write to a regular file. Iterate the related records and mark for
1478 * deletion. If existing edge records (left and right side) overlap our
1479 * write they have to be marked deleted and new records created, usually
1480 * referencing a portion of the original data. Then add a record to
1481 * represent the buffer.
1483 * The support code in hammer_object.c should be used to deal with mixed
1484 * in-memory and on-disk records.
1488 hammer_vop_strategy_write(struct vop_strategy_args *ap)
1490 struct hammer_transaction trans;
1491 struct hammer_cursor *spike = NULL;
1499 ip = ap->a_vp->v_data;
1500 hammer_start_transaction(&trans, ip->hmp);
1504 * Delete any records overlapping our range. This function will
1505 * (eventually) properly truncate partial overlaps.
1507 if (ip->ino_rec.base.base.obj_type == HAMMER_OBJTYPE_DBFILE) {
1508 error = hammer_ip_delete_range(&trans, ip, bio->bio_offset,
1509 bio->bio_offset, &spike);
1511 error = hammer_ip_delete_range(&trans, ip, bio->bio_offset,
1518 * Add a single record to cover the write
1521 error = hammer_ip_sync_data(&trans, ip, bio->bio_offset,
1522 bp->b_data, bp->b_bufsize,
1527 * If we ran out of space the spike structure will be filled in
1528 * and we must call hammer_spike with it, then retry.
1530 if (error == ENOSPC) {
1531 error = hammer_spike(&spike);
1535 KKASSERT(spike == NULL);
1538 * If an error occured abort the transaction
1541 /* XXX undo deletion */
1542 hammer_abort_transaction(&trans);
1543 bp->b_resid = bp->b_bufsize;
1545 hammer_commit_transaction(&trans);
1552 * dounlink - disconnect a directory entry
1554 * XXX whiteout support not really in yet
1557 hammer_dounlink(struct nchandle *nch, struct vnode *dvp, struct ucred *cred,
1560 struct hammer_transaction trans;
1561 struct namecache *ncp;
1564 hammer_record_ondisk_t rec;
1565 struct hammer_cursor cursor;
1570 * Calculate the namekey and setup the key range for the scan. This
1571 * works kinda like a chained hash table where the lower 32 bits
1572 * of the namekey synthesize the chain.
1574 * The key range is inclusive of both key_beg and key_end.
1578 namekey = hammer_directory_namekey(ncp->nc_name, ncp->nc_nlen);
1580 hammer_init_cursor_ip(&cursor, dip);
1581 cursor.key_beg.obj_id = dip->obj_id;
1582 cursor.key_beg.key = namekey;
1583 cursor.key_beg.create_tid = dip->obj_asof;
1584 cursor.key_beg.delete_tid = 0;
1585 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1586 cursor.key_beg.obj_type = 0;
1588 cursor.key_end = cursor.key_beg;
1589 cursor.key_end.key |= 0xFFFFFFFFULL;
1590 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
1592 hammer_start_transaction(&trans, dip->hmp);
1595 * Scan all matching records (the chain), locate the one matching
1596 * the requested path component. info->last_error contains the
1597 * error code on search termination and could be 0, ENOENT, or
1600 * The hammer_ip_*() functions merge in-memory records with on-disk
1601 * records for the purposes of the search.
1603 error = hammer_ip_first(&cursor, dip);
1604 while (error == 0) {
1605 error = hammer_ip_resolve_data(&cursor);
1608 rec = cursor.record;
1609 if (ncp->nc_nlen == rec->entry.base.data_len &&
1610 bcmp(ncp->nc_name, cursor.data, ncp->nc_nlen) == 0) {
1613 error = hammer_ip_next(&cursor);
1617 * If all is ok we have to get the inode so we can adjust nlinks.
1620 ip = hammer_get_inode(dip->hmp, rec->entry.obj_id,
1621 dip->hmp->asof, &error);
1623 error = hammer_ip_del_directory(&trans, &cursor, dip, ip);
1625 cache_setunresolved(nch);
1626 cache_setvp(nch, NULL);
1629 cache_inval_vp(ip->vp, CINV_DESTROY);
1631 hammer_rel_inode(ip, 0);
1634 hammer_commit_transaction(&trans);
1636 hammer_abort_transaction(&trans);
1639 hammer_done_cursor(&cursor);
1643 /************************************************************************
1644 * FIFO AND SPECFS OPS *
1645 ************************************************************************
1650 hammer_vop_fifoclose (struct vop_close_args *ap)
1652 /* XXX update itimes */
1653 return (VOCALL(&fifo_vnode_vops, &ap->a_head));
1657 hammer_vop_fiforead (struct vop_read_args *ap)
1661 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
1662 /* XXX update access time */
1667 hammer_vop_fifowrite (struct vop_write_args *ap)
1671 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
1672 /* XXX update access time */
1677 hammer_vop_specclose (struct vop_close_args *ap)
1679 /* XXX update itimes */
1680 return (VOCALL(&spec_vnode_vops, &ap->a_head));
1684 hammer_vop_specread (struct vop_read_args *ap)
1686 /* XXX update access time */
1687 return (VOCALL(&spec_vnode_vops, &ap->a_head));
1691 hammer_vop_specwrite (struct vop_write_args *ap)
1693 /* XXX update last change time */
1694 return (VOCALL(&spec_vnode_vops, &ap->a_head));