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.12 2007/12/30 08:49:20 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 kprintf("hammer_create_inode error %d\n", error);
434 hammer_abort_transaction(&trans);
440 * Add the new filesystem object to the directory. This will also
441 * bump the inode's link count.
443 error = hammer_ip_add_directory(&trans, dip, nch->ncp, nip);
445 kprintf("hammer_ip_add_directory error %d\n", error);
451 hammer_rel_inode(nip, 0);
452 hammer_abort_transaction(&trans);
455 hammer_commit_transaction(&trans);
456 error = hammer_get_vnode(nip, LK_EXCLUSIVE, ap->a_vpp);
457 hammer_rel_inode(nip, 0);
459 cache_setunresolved(ap->a_nch);
460 cache_setvp(ap->a_nch, *ap->a_vpp);
467 * hammer_vop_getattr { vp, vap }
471 hammer_vop_getattr(struct vop_getattr_args *ap)
473 struct hammer_inode *ip = VTOI(ap->a_vp);
474 struct vattr *vap = ap->a_vap;
477 if (cache_check_fsmid_vp(ap->a_vp, &ip->fsmid) &&
478 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0 &&
483 hammer_itimes(ap->a_vp);
486 vap->va_fsid = ip->hmp->fsid_udev;
487 vap->va_fileid = ip->ino_rec.base.base.obj_id;
488 vap->va_mode = ip->ino_data.mode;
489 vap->va_nlink = ip->ino_rec.ino_nlinks;
490 vap->va_uid = hammer_to_unix_xid(&ip->ino_data.uid);
491 vap->va_gid = hammer_to_unix_xid(&ip->ino_data.gid);
494 vap->va_size = ip->ino_rec.ino_size;
495 hammer_to_timespec(ip->ino_rec.ino_atime, &vap->va_atime);
496 hammer_to_timespec(ip->ino_rec.ino_mtime, &vap->va_mtime);
497 hammer_to_timespec(ip->ino_data.ctime, &vap->va_ctime);
498 vap->va_flags = ip->ino_data.uflags;
499 vap->va_gen = 1; /* hammer inums are unique for all time */
500 vap->va_blocksize = 32768; /* XXX - extract from root volume */
501 vap->va_bytes = ip->ino_rec.ino_size;
502 vap->va_type = hammer_get_vnode_type(ip->ino_rec.base.base.obj_type);
503 vap->va_filerev = 0; /* XXX */
504 /* mtime uniquely identifies any adjustments made to the file */
505 vap->va_fsmid = ip->ino_rec.ino_mtime;
506 vap->va_uid_uuid = ip->ino_data.uid;
507 vap->va_gid_uuid = ip->ino_data.gid;
508 vap->va_fsid_uuid = ip->hmp->fsid;
509 vap->va_vaflags = VA_UID_UUID_VALID | VA_GID_UUID_VALID |
512 switch (ip->ino_rec.base.base.obj_type) {
513 case HAMMER_OBJTYPE_CDEV:
514 case HAMMER_OBJTYPE_BDEV:
515 vap->va_rmajor = ip->ino_data.rmajor;
516 vap->va_rminor = ip->ino_data.rminor;
526 * hammer_vop_nresolve { nch, dvp, cred }
528 * Locate the requested directory entry.
532 hammer_vop_nresolve(struct vop_nresolve_args *ap)
534 struct namecache *ncp;
538 struct hammer_cursor cursor;
539 union hammer_record_ondisk *rec;
547 * Misc initialization, plus handle as-of name extensions. Look for
548 * the '@@' extension. Note that as-of files and directories cannot
553 dip = VTOI(ap->a_dvp);
554 ncp = ap->a_nch->ncp;
555 asof = dip->obj_asof;
558 for (i = 0; i < nlen; ++i) {
559 if (ncp->nc_name[i] == '@' && ncp->nc_name[i+1] == '@') {
560 asof = hammer_now_tid() -
561 strtoq(ncp->nc_name + i + 2, NULL, 0) *
563 kprintf("ASOF %016llx\n", asof);
570 * Calculate the namekey and setup the key range for the scan. This
571 * works kinda like a chained hash table where the lower 32 bits
572 * of the namekey synthesize the chain.
574 * The key range is inclusive of both key_beg and key_end.
576 namekey = hammer_directory_namekey(ncp->nc_name, nlen);
578 hammer_init_cursor_ip(&cursor, dip);
579 cursor.key_beg.obj_id = dip->obj_id;
580 cursor.key_beg.key = namekey;
581 cursor.key_beg.create_tid = asof;
582 cursor.key_beg.delete_tid = 0;
583 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
584 cursor.key_beg.obj_type = 0;
586 cursor.key_end = cursor.key_beg;
587 cursor.key_end.key |= 0xFFFFFFFFULL;
588 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
591 * Scan all matching records (the chain), locate the one matching
592 * the requested path component.
594 * The hammer_ip_*() functions merge in-memory records with on-disk
595 * records for the purposes of the search.
597 error = hammer_ip_first(&cursor, dip);
600 error = hammer_ip_resolve_data(&cursor);
604 if (nlen == rec->entry.base.data_len &&
605 bcmp(ncp->nc_name, cursor.data, nlen) == 0) {
608 error = hammer_ip_next(&cursor);
611 ip = hammer_get_inode(dip->hmp, rec->entry.obj_id,
614 error = hammer_get_vnode(ip, LK_EXCLUSIVE, &vp);
615 hammer_rel_inode(ip, 0);
621 cache_setvp(ap->a_nch, vp);
624 } else if (error == ENOENT) {
625 cache_setvp(ap->a_nch, NULL);
627 hammer_done_cursor(&cursor);
632 * hammer_vop_nlookupdotdot { dvp, vpp, cred }
634 * Locate the parent directory of a directory vnode.
636 * dvp is referenced but not locked. *vpp must be returned referenced and
637 * locked. A parent_obj_id of 0 does not necessarily indicate that we are
638 * at the root, instead it could indicate that the directory we were in was
643 hammer_vop_nlookupdotdot(struct vop_nlookupdotdot_args *ap)
645 struct hammer_inode *dip;
646 u_int64_t parent_obj_id;
648 dip = VTOI(ap->a_dvp);
649 if ((parent_obj_id = dip->ino_data.parent_obj_id) == 0) {
653 return(hammer_vfs_vget(dip->hmp->mp, parent_obj_id, ap->a_vpp));
657 * hammer_vop_nlink { nch, dvp, vp, cred }
661 hammer_vop_nlink(struct vop_nlink_args *ap)
663 struct hammer_transaction trans;
664 struct hammer_inode *dip;
665 struct hammer_inode *ip;
666 struct nchandle *nch;
670 dip = VTOI(ap->a_dvp);
674 * Create a transaction to cover the operations we perform.
676 hammer_start_transaction(&trans, dip->hmp);
679 * Add the filesystem object to the directory. Note that neither
680 * dip nor ip are referenced or locked, but their vnodes are
681 * referenced. This function will bump the inode's link count.
683 error = hammer_ip_add_directory(&trans, dip, nch->ncp, ip);
689 hammer_abort_transaction(&trans);
691 cache_setunresolved(nch);
692 cache_setvp(nch, ap->a_vp);
693 hammer_commit_transaction(&trans);
699 * hammer_vop_nmkdir { nch, dvp, vpp, cred, vap }
701 * The operating system has already ensured that the directory entry
702 * does not exist and done all appropriate namespace locking.
706 hammer_vop_nmkdir(struct vop_nmkdir_args *ap)
708 struct hammer_transaction trans;
709 struct hammer_inode *dip;
710 struct hammer_inode *nip;
711 struct nchandle *nch;
715 dip = VTOI(ap->a_dvp);
718 * Create a transaction to cover the operations we perform.
720 hammer_start_transaction(&trans, dip->hmp);
723 * Create a new filesystem object of the requested type. The
724 * returned inode will be referenced but not locked.
726 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred, dip, &nip);
728 kprintf("hammer_mkdir error %d\n", error);
730 hammer_abort_transaction(&trans);
736 * Add the new filesystem object to the directory. This will also
737 * bump the inode's link count.
739 error = hammer_ip_add_directory(&trans, dip, nch->ncp, nip);
741 kprintf("hammer_mkdir (add) error %d\n", error);
747 hammer_rel_inode(nip, 0);
748 hammer_abort_transaction(&trans);
751 hammer_commit_transaction(&trans);
752 error = hammer_get_vnode(nip, LK_EXCLUSIVE, ap->a_vpp);
753 hammer_rel_inode(nip, 0);
755 cache_setunresolved(ap->a_nch);
756 cache_setvp(ap->a_nch, *ap->a_vpp);
763 * hammer_vop_nmknod { nch, dvp, vpp, cred, vap }
765 * The operating system has already ensured that the directory entry
766 * does not exist and done all appropriate namespace locking.
770 hammer_vop_nmknod(struct vop_nmknod_args *ap)
772 struct hammer_transaction trans;
773 struct hammer_inode *dip;
774 struct hammer_inode *nip;
775 struct nchandle *nch;
779 dip = VTOI(ap->a_dvp);
782 * Create a transaction to cover the operations we perform.
784 hammer_start_transaction(&trans, dip->hmp);
787 * Create a new filesystem object of the requested type. The
788 * returned inode will be referenced but not locked.
790 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred, dip, &nip);
792 hammer_abort_transaction(&trans);
798 * Add the new filesystem object to the directory. This will also
799 * bump the inode's link count.
801 error = hammer_ip_add_directory(&trans, dip, nch->ncp, nip);
807 hammer_rel_inode(nip, 0);
808 hammer_abort_transaction(&trans);
811 hammer_commit_transaction(&trans);
812 error = hammer_get_vnode(nip, LK_EXCLUSIVE, ap->a_vpp);
813 hammer_rel_inode(nip, 0);
815 cache_setunresolved(ap->a_nch);
816 cache_setvp(ap->a_nch, *ap->a_vpp);
823 * hammer_vop_open { vp, mode, cred, fp }
827 hammer_vop_open(struct vop_open_args *ap)
829 return(vop_stdopen(ap));
833 * hammer_vop_pathconf { vp, name, retval }
837 hammer_vop_pathconf(struct vop_pathconf_args *ap)
843 * hammer_vop_print { vp }
847 hammer_vop_print(struct vop_print_args *ap)
853 * hammer_vop_readdir { vp, uio, cred, *eofflag, *ncookies, off_t **cookies }
857 hammer_vop_readdir(struct vop_readdir_args *ap)
859 struct hammer_cursor cursor;
860 struct hammer_inode *ip;
862 hammer_record_ondisk_t rec;
863 hammer_base_elm_t base;
873 hammer_init_cursor_ip(&cursor, ip);
876 * Key range (begin and end inclusive) to scan. Directory keys
877 * directly translate to a 64 bit 'seek' position.
879 cursor.key_beg.obj_id = ip->obj_id;
880 cursor.key_beg.create_tid = ip->obj_asof;
881 cursor.key_beg.delete_tid = 0;
882 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
883 cursor.key_beg.obj_type = 0;
884 cursor.key_beg.key = uio->uio_offset;
886 cursor.key_end = cursor.key_beg;
887 cursor.key_end.key = HAMMER_MAX_KEY;
888 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
890 if (ap->a_ncookies) {
891 ncookies = uio->uio_resid / 16 + 1;
894 cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
902 saveoff = cursor.key_beg.key;
903 error = hammer_ip_first(&cursor, ip);
906 error = hammer_ip_resolve_data(&cursor);
910 base = &rec->base.base;
913 if (base->obj_id != ip->obj_id)
914 panic("readdir: bad record at %p", cursor.node);
916 r = vop_write_dirent(
917 &error, uio, rec->entry.obj_id,
918 hammer_get_dtype(rec->entry.base.base.obj_type),
919 rec->entry.base.data_len,
920 (void *)cursor.data);
925 cookies[cookie_index] = base->key;
927 if (cookie_index == ncookies)
929 error = hammer_ip_next(&cursor);
931 hammer_done_cursor(&cursor);
934 *ap->a_eofflag = (error == ENOENT);
935 uio->uio_offset = saveoff;
936 if (error && cookie_index == 0) {
938 kfree(cookies, M_TEMP);
940 *ap->a_cookies = NULL;
946 *ap->a_ncookies = cookie_index;
947 *ap->a_cookies = cookies;
954 * hammer_vop_readlink { vp, uio, cred }
958 hammer_vop_readlink(struct vop_readlink_args *ap)
960 struct hammer_cursor cursor;
961 struct hammer_inode *ip;
965 hammer_init_cursor_ip(&cursor, ip);
968 * Key range (begin and end inclusive) to scan. Directory keys
969 * directly translate to a 64 bit 'seek' position.
971 cursor.key_beg.obj_id = ip->obj_id;
972 cursor.key_beg.create_tid = ip->obj_asof;
973 cursor.key_beg.delete_tid = 0;
974 cursor.key_beg.rec_type = HAMMER_RECTYPE_FIX;
975 cursor.key_beg.obj_type = 0;
976 cursor.key_beg.key = HAMMER_FIXKEY_SYMLINK;
978 error = hammer_ip_lookup(&cursor, ip);
980 error = hammer_ip_resolve_data(&cursor);
982 error = uiomove((char *)cursor.data,
983 cursor.record->generic.base.data_len,
987 hammer_done_cursor(&cursor);
992 * hammer_vop_nremove { nch, dvp, cred }
996 hammer_vop_nremove(struct vop_nremove_args *ap)
998 return(hammer_dounlink(ap->a_nch, ap->a_dvp, ap->a_cred, 0));
1002 * hammer_vop_nrename { fnch, tnch, fdvp, tdvp, cred }
1006 hammer_vop_nrename(struct vop_nrename_args *ap)
1008 struct hammer_transaction trans;
1009 struct namecache *fncp;
1010 struct namecache *tncp;
1011 struct hammer_inode *fdip;
1012 struct hammer_inode *tdip;
1013 struct hammer_inode *ip;
1014 struct hammer_cursor cursor;
1015 union hammer_record_ondisk *rec;
1019 fdip = VTOI(ap->a_fdvp);
1020 tdip = VTOI(ap->a_tdvp);
1021 fncp = ap->a_fnch->ncp;
1022 tncp = ap->a_tnch->ncp;
1023 hammer_start_transaction(&trans, fdip->hmp);
1026 * Extract the hammer_inode from fncp and add link to the target
1029 ip = VTOI(fncp->nc_vp);
1030 KKASSERT(ip != NULL);
1032 error = hammer_ip_add_directory(&trans, tdip, tncp, ip);
1035 * Locate the record in the originating directory and remove it.
1037 * Calculate the namekey and setup the key range for the scan. This
1038 * works kinda like a chained hash table where the lower 32 bits
1039 * of the namekey synthesize the chain.
1041 * The key range is inclusive of both key_beg and key_end.
1043 namekey = hammer_directory_namekey(fncp->nc_name, fncp->nc_nlen);
1045 hammer_init_cursor_ip(&cursor, fdip);
1046 cursor.key_beg.obj_id = fdip->obj_id;
1047 cursor.key_beg.key = namekey;
1048 cursor.key_beg.create_tid = fdip->obj_asof;
1049 cursor.key_beg.delete_tid = 0;
1050 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1051 cursor.key_beg.obj_type = 0;
1053 cursor.key_end = cursor.key_beg;
1054 cursor.key_end.key |= 0xFFFFFFFFULL;
1055 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
1058 * Scan all matching records (the chain), locate the one matching
1059 * the requested path component.
1061 * The hammer_ip_*() functions merge in-memory records with on-disk
1062 * records for the purposes of the search.
1064 error = hammer_ip_first(&cursor, fdip);
1065 while (error == 0) {
1066 if (hammer_ip_resolve_data(&cursor) != 0)
1068 rec = cursor.record;
1069 if (fncp->nc_nlen == rec->entry.base.data_len &&
1070 bcmp(fncp->nc_name, cursor.data, fncp->nc_nlen) == 0) {
1073 error = hammer_ip_next(&cursor);
1077 * If all is ok we have to get the inode so we can adjust nlinks.
1081 error = hammer_ip_del_directory(&trans, &cursor, fdip, ip);
1083 cache_rename(ap->a_fnch, ap->a_tnch);
1084 cache_setvp(ap->a_tnch, ip->vp);
1087 hammer_done_cursor(&cursor);
1089 hammer_commit_transaction(&trans);
1091 hammer_abort_transaction(&trans);
1097 * hammer_vop_nrmdir { nch, dvp, cred }
1101 hammer_vop_nrmdir(struct vop_nrmdir_args *ap)
1103 /* XXX check that directory is empty */
1105 return(hammer_dounlink(ap->a_nch, ap->a_dvp, ap->a_cred, 0));
1109 * hammer_vop_setattr { vp, vap, cred }
1113 hammer_vop_setattr(struct vop_setattr_args *ap)
1115 struct hammer_transaction trans;
1116 struct hammer_cursor *spike = NULL;
1118 struct hammer_inode *ip;
1121 int64_t aligned_size;
1126 ip = ap->a_vp->v_data;
1129 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
1132 hammer_start_transaction(&trans, ip->hmp);
1135 if (vap->va_flags != VNOVAL) {
1136 flags = ip->ino_data.uflags;
1137 error = vop_helper_setattr_flags(&flags, vap->va_flags,
1138 hammer_to_unix_xid(&ip->ino_data.uid),
1141 if (ip->ino_data.uflags != flags) {
1142 ip->ino_data.uflags = flags;
1143 modflags |= HAMMER_INODE_DDIRTY;
1145 if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) {
1152 if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) {
1156 if (vap->va_uid != (uid_t)VNOVAL) {
1157 hammer_guid_to_uuid(&uuid, vap->va_uid);
1158 if (bcmp(&uuid, &ip->ino_data.uid, sizeof(uuid)) != 0) {
1159 ip->ino_data.uid = uuid;
1160 modflags |= HAMMER_INODE_DDIRTY;
1163 if (vap->va_gid != (uid_t)VNOVAL) {
1164 hammer_guid_to_uuid(&uuid, vap->va_gid);
1165 if (bcmp(&uuid, &ip->ino_data.gid, sizeof(uuid)) != 0) {
1166 ip->ino_data.gid = uuid;
1167 modflags |= HAMMER_INODE_DDIRTY;
1170 while (vap->va_size != VNOVAL && ip->ino_rec.ino_size != vap->va_size) {
1171 switch(ap->a_vp->v_type) {
1173 if (vap->va_size < ip->ino_rec.ino_size) {
1174 vtruncbuf(ap->a_vp, vap->va_size,
1176 } else if (vap->va_size > ip->ino_rec.ino_size) {
1177 vnode_pager_setsize(ap->a_vp, vap->va_size);
1179 aligned_size = (vap->va_size + HAMMER_BUFMASK) &
1180 ~(int64_t)HAMMER_BUFMASK;
1181 error = hammer_ip_delete_range(&trans, ip,
1183 0x7FFFFFFFFFFFFFFFLL,
1185 ip->ino_rec.ino_size = vap->va_size;
1186 modflags |= HAMMER_INODE_RDIRTY;
1189 error = hammer_ip_delete_range(&trans, ip,
1191 0x7FFFFFFFFFFFFFFFLL,
1193 ip->ino_rec.ino_size = vap->va_size;
1194 modflags |= HAMMER_INODE_RDIRTY;
1200 if (error == ENOSPC) {
1201 error = hammer_spike(&spike);
1205 KKASSERT(spike == NULL);
1208 if (vap->va_atime.tv_sec != VNOVAL) {
1209 ip->ino_rec.ino_atime =
1210 hammer_timespec_to_transid(&vap->va_atime);
1211 modflags |= HAMMER_INODE_ITIMES;
1213 if (vap->va_mtime.tv_sec != VNOVAL) {
1214 ip->ino_rec.ino_mtime =
1215 hammer_timespec_to_transid(&vap->va_mtime);
1216 modflags |= HAMMER_INODE_ITIMES;
1218 if (vap->va_mode != (mode_t)VNOVAL) {
1219 if (ip->ino_data.mode != vap->va_mode) {
1220 ip->ino_data.mode = vap->va_mode;
1221 modflags |= HAMMER_INODE_DDIRTY;
1226 hammer_abort_transaction(&trans);
1228 if (modflags & (HAMMER_INODE_RDIRTY | HAMMER_INODE_DDIRTY))
1229 modflags |= HAMMER_INODE_TID;
1230 hammer_modify_inode(&trans, ip, modflags);
1231 hammer_commit_transaction(&trans);
1237 * hammer_vop_nsymlink { nch, dvp, vpp, cred, vap, target }
1241 hammer_vop_nsymlink(struct vop_nsymlink_args *ap)
1243 struct hammer_transaction trans;
1244 struct hammer_inode *dip;
1245 struct hammer_inode *nip;
1246 struct nchandle *nch;
1247 hammer_record_t record;
1251 ap->a_vap->va_type = VLNK;
1254 dip = VTOI(ap->a_dvp);
1257 * Create a transaction to cover the operations we perform.
1259 hammer_start_transaction(&trans, dip->hmp);
1262 * Create a new filesystem object of the requested type. The
1263 * returned inode will be referenced but not locked.
1266 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred, dip, &nip);
1268 hammer_abort_transaction(&trans);
1274 * Add the new filesystem object to the directory. This will also
1275 * bump the inode's link count.
1277 error = hammer_ip_add_directory(&trans, dip, nch->ncp, nip);
1280 * Add a record representing the symlink. symlink stores the link
1281 * as pure data, not a string, and is no \0 terminated.
1284 record = hammer_alloc_mem_record(nip);
1285 bytes = strlen(ap->a_target);
1287 record->rec.generic.base.base.key = HAMMER_FIXKEY_SYMLINK;
1288 record->rec.generic.base.base.rec_type = HAMMER_RECTYPE_FIX;
1289 record->rec.generic.base.data_len = bytes;
1290 if (bytes <= sizeof(record->rec.generic.filler)) {
1291 record->data = (void *)record->rec.generic.filler;
1292 bcopy(ap->a_target, record->data, bytes);
1294 record->data = (void *)ap->a_target;
1295 /* will be reallocated by routine below */
1297 error = hammer_ip_add_record(&trans, record);
1304 hammer_rel_inode(nip, 0);
1305 hammer_abort_transaction(&trans);
1308 hammer_commit_transaction(&trans);
1309 error = hammer_get_vnode(nip, LK_EXCLUSIVE, ap->a_vpp);
1310 hammer_rel_inode(nip, 0);
1312 cache_setunresolved(ap->a_nch);
1313 cache_setvp(ap->a_nch, *ap->a_vpp);
1320 * hammer_vop_nwhiteout { nch, dvp, cred, flags }
1324 hammer_vop_nwhiteout(struct vop_nwhiteout_args *ap)
1326 return(hammer_dounlink(ap->a_nch, ap->a_dvp, ap->a_cred, ap->a_flags));
1330 * hammer_vop_strategy { vp, bio }
1332 * Strategy call, used for regular file read & write only. Note that the
1333 * bp may represent a cluster.
1335 * To simplify operation and allow better optimizations in the future,
1336 * this code does not make any assumptions with regards to buffer alignment
1341 hammer_vop_strategy(struct vop_strategy_args *ap)
1346 bp = ap->a_bio->bio_buf;
1350 error = hammer_vop_strategy_read(ap);
1353 error = hammer_vop_strategy_write(ap);
1359 bp->b_error = error;
1361 bp->b_flags |= B_ERROR;
1367 * Read from a regular file. Iterate the related records and fill in the
1368 * BIO/BUF. Gaps are zero-filled.
1370 * The support code in hammer_object.c should be used to deal with mixed
1371 * in-memory and on-disk records.
1377 hammer_vop_strategy_read(struct vop_strategy_args *ap)
1379 struct hammer_inode *ip = ap->a_vp->v_data;
1380 struct hammer_cursor cursor;
1381 hammer_record_ondisk_t rec;
1382 hammer_base_elm_t base;
1396 hammer_init_cursor_ip(&cursor, ip);
1399 * Key range (begin and end inclusive) to scan. Note that the key's
1400 * stored in the actual records represent BASE+LEN, not BASE. The
1401 * first record containing bio_offset will have a key > bio_offset.
1403 cursor.key_beg.obj_id = ip->obj_id;
1404 cursor.key_beg.create_tid = ip->obj_asof;
1405 cursor.key_beg.delete_tid = 0;
1406 cursor.key_beg.obj_type = 0;
1407 cursor.key_beg.key = bio->bio_offset + 1;
1409 cursor.key_end = cursor.key_beg;
1410 if (ip->ino_rec.base.base.obj_type == HAMMER_OBJTYPE_DBFILE) {
1411 cursor.key_beg.rec_type = HAMMER_RECTYPE_DB;
1412 cursor.key_end.rec_type = HAMMER_RECTYPE_DB;
1413 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
1415 ran_end = bio->bio_offset + bp->b_bufsize;
1416 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
1417 cursor.key_end.rec_type = HAMMER_RECTYPE_DATA;
1418 tmp64 = ran_end + MAXPHYS + 1; /* work-around GCC-4 bug */
1419 if (tmp64 < ran_end)
1420 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
1422 cursor.key_end.key = ran_end + MAXPHYS + 1;
1424 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
1426 error = hammer_ip_first(&cursor, ip);
1429 while (error == 0) {
1430 error = hammer_ip_resolve_data(&cursor);
1433 rec = cursor.record;
1434 base = &rec->base.base;
1436 rec_offset = base->key - rec->data.base.data_len;
1439 * Calculate the gap, if any, and zero-fill it.
1441 n = (int)(rec_offset - (bio->bio_offset + boff));
1443 if (n > bp->b_bufsize - boff)
1444 n = bp->b_bufsize - boff;
1445 kprintf("zfill %d bytes\n", n);
1446 bzero((char *)bp->b_data + boff, n);
1452 * Calculate the data offset in the record and the number
1453 * of bytes we can copy.
1455 * Note there is a degenerate case here where boff may
1456 * already be at bp->b_bufsize.
1459 n = rec->data.base.data_len - roff;
1461 if (n > bp->b_bufsize - boff)
1462 n = bp->b_bufsize - boff;
1463 bcopy((char *)cursor.data + roff, (char *)bp->b_data + boff, n);
1465 if (boff == bp->b_bufsize)
1467 error = hammer_ip_next(&cursor);
1469 hammer_done_cursor(&cursor);
1472 * There may have been a gap after the last record
1474 if (error == ENOENT)
1476 if (error == 0 && boff != bp->b_bufsize) {
1477 KKASSERT(boff < bp->b_bufsize);
1478 bzero((char *)bp->b_data + boff, bp->b_bufsize - boff);
1479 /* boff = bp->b_bufsize; */
1486 * Write to a regular file. Iterate the related records and mark for
1487 * deletion. If existing edge records (left and right side) overlap our
1488 * write they have to be marked deleted and new records created, usually
1489 * referencing a portion of the original data. Then add a record to
1490 * represent the buffer.
1492 * The support code in hammer_object.c should be used to deal with mixed
1493 * in-memory and on-disk records.
1497 hammer_vop_strategy_write(struct vop_strategy_args *ap)
1499 struct hammer_transaction trans;
1500 struct hammer_cursor *spike = NULL;
1508 ip = ap->a_vp->v_data;
1509 hammer_start_transaction(&trans, ip->hmp);
1513 * Delete any records overlapping our range. This function will
1514 * (eventually) properly truncate partial overlaps.
1516 if (ip->ino_rec.base.base.obj_type == HAMMER_OBJTYPE_DBFILE) {
1517 error = hammer_ip_delete_range(&trans, ip, bio->bio_offset,
1518 bio->bio_offset, &spike);
1520 error = hammer_ip_delete_range(&trans, ip, bio->bio_offset,
1527 * Add a single record to cover the write
1530 error = hammer_ip_sync_data(&trans, ip, bio->bio_offset,
1531 bp->b_data, bp->b_bufsize,
1536 * If we ran out of space the spike structure will be filled in
1537 * and we must call hammer_spike with it, then retry.
1539 if (error == ENOSPC) {
1540 error = hammer_spike(&spike);
1544 KKASSERT(spike == NULL);
1547 * If an error occured abort the transaction
1550 /* XXX undo deletion */
1551 hammer_abort_transaction(&trans);
1552 bp->b_resid = bp->b_bufsize;
1554 hammer_commit_transaction(&trans);
1561 * dounlink - disconnect a directory entry
1563 * XXX whiteout support not really in yet
1566 hammer_dounlink(struct nchandle *nch, struct vnode *dvp, struct ucred *cred,
1569 struct hammer_transaction trans;
1570 struct namecache *ncp;
1573 hammer_record_ondisk_t rec;
1574 struct hammer_cursor cursor;
1579 * Calculate the namekey and setup the key range for the scan. This
1580 * works kinda like a chained hash table where the lower 32 bits
1581 * of the namekey synthesize the chain.
1583 * The key range is inclusive of both key_beg and key_end.
1587 namekey = hammer_directory_namekey(ncp->nc_name, ncp->nc_nlen);
1589 hammer_init_cursor_ip(&cursor, dip);
1590 cursor.key_beg.obj_id = dip->obj_id;
1591 cursor.key_beg.key = namekey;
1592 cursor.key_beg.create_tid = dip->obj_asof;
1593 cursor.key_beg.delete_tid = 0;
1594 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1595 cursor.key_beg.obj_type = 0;
1597 cursor.key_end = cursor.key_beg;
1598 cursor.key_end.key |= 0xFFFFFFFFULL;
1599 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
1601 hammer_start_transaction(&trans, dip->hmp);
1604 * Scan all matching records (the chain), locate the one matching
1605 * the requested path component. info->last_error contains the
1606 * error code on search termination and could be 0, ENOENT, or
1609 * The hammer_ip_*() functions merge in-memory records with on-disk
1610 * records for the purposes of the search.
1612 error = hammer_ip_first(&cursor, dip);
1613 while (error == 0) {
1614 error = hammer_ip_resolve_data(&cursor);
1617 rec = cursor.record;
1618 if (ncp->nc_nlen == rec->entry.base.data_len &&
1619 bcmp(ncp->nc_name, cursor.data, ncp->nc_nlen) == 0) {
1622 error = hammer_ip_next(&cursor);
1626 * If all is ok we have to get the inode so we can adjust nlinks.
1629 ip = hammer_get_inode(dip->hmp, rec->entry.obj_id,
1630 dip->hmp->asof, &error);
1632 error = hammer_ip_del_directory(&trans, &cursor, dip, ip);
1634 cache_setunresolved(nch);
1635 cache_setvp(nch, NULL);
1638 cache_inval_vp(ip->vp, CINV_DESTROY);
1640 hammer_rel_inode(ip, 0);
1643 hammer_commit_transaction(&trans);
1645 hammer_abort_transaction(&trans);
1648 hammer_done_cursor(&cursor);
1652 /************************************************************************
1653 * FIFO AND SPECFS OPS *
1654 ************************************************************************
1659 hammer_vop_fifoclose (struct vop_close_args *ap)
1661 /* XXX update itimes */
1662 return (VOCALL(&fifo_vnode_vops, &ap->a_head));
1666 hammer_vop_fiforead (struct vop_read_args *ap)
1670 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
1671 /* XXX update access time */
1676 hammer_vop_fifowrite (struct vop_write_args *ap)
1680 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
1681 /* XXX update access time */
1686 hammer_vop_specclose (struct vop_close_args *ap)
1688 /* XXX update itimes */
1689 return (VOCALL(&spec_vnode_vops, &ap->a_head));
1693 hammer_vop_specread (struct vop_read_args *ap)
1695 /* XXX update access time */
1696 return (VOCALL(&spec_vnode_vops, &ap->a_head));
1700 hammer_vop_specwrite (struct vop_write_args *ap)
1702 /* XXX update last change time */
1703 return (VOCALL(&spec_vnode_vops, &ap->a_head));