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.14 2007/12/31 05:44:33 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 *);
81 static int hammer_vop_fifoclose (struct vop_close_args *);
82 static int hammer_vop_fiforead (struct vop_read_args *);
83 static int hammer_vop_fifowrite (struct vop_write_args *);
85 static int hammer_vop_specclose (struct vop_close_args *);
86 static int hammer_vop_specread (struct vop_read_args *);
87 static int hammer_vop_specwrite (struct vop_write_args *);
89 struct vop_ops hammer_vnode_vops = {
90 .vop_default = vop_defaultop,
91 .vop_fsync = hammer_vop_fsync,
92 .vop_getpages = vop_stdgetpages,
93 .vop_putpages = vop_stdputpages,
94 .vop_read = hammer_vop_read,
95 .vop_write = hammer_vop_write,
96 .vop_access = hammer_vop_access,
97 .vop_advlock = hammer_vop_advlock,
98 .vop_close = hammer_vop_close,
99 .vop_ncreate = hammer_vop_ncreate,
100 .vop_getattr = hammer_vop_getattr,
101 .vop_inactive = hammer_vop_inactive,
102 .vop_reclaim = hammer_vop_reclaim,
103 .vop_nresolve = hammer_vop_nresolve,
104 .vop_nlookupdotdot = hammer_vop_nlookupdotdot,
105 .vop_nlink = hammer_vop_nlink,
106 .vop_nmkdir = hammer_vop_nmkdir,
107 .vop_nmknod = hammer_vop_nmknod,
108 .vop_open = hammer_vop_open,
109 .vop_pathconf = hammer_vop_pathconf,
110 .vop_print = hammer_vop_print,
111 .vop_readdir = hammer_vop_readdir,
112 .vop_readlink = hammer_vop_readlink,
113 .vop_nremove = hammer_vop_nremove,
114 .vop_nrename = hammer_vop_nrename,
115 .vop_nrmdir = hammer_vop_nrmdir,
116 .vop_setattr = hammer_vop_setattr,
117 .vop_strategy = hammer_vop_strategy,
118 .vop_nsymlink = hammer_vop_nsymlink,
119 .vop_nwhiteout = hammer_vop_nwhiteout
122 struct vop_ops hammer_spec_vops = {
123 .vop_default = spec_vnoperate,
124 .vop_fsync = hammer_vop_fsync,
125 .vop_read = hammer_vop_specread,
126 .vop_write = hammer_vop_specwrite,
127 .vop_access = hammer_vop_access,
128 .vop_close = hammer_vop_specclose,
129 .vop_getattr = hammer_vop_getattr,
130 .vop_inactive = hammer_vop_inactive,
131 .vop_reclaim = hammer_vop_reclaim,
132 .vop_setattr = hammer_vop_setattr
135 struct vop_ops hammer_fifo_vops = {
136 .vop_default = fifo_vnoperate,
137 .vop_fsync = hammer_vop_fsync,
138 .vop_read = hammer_vop_fiforead,
139 .vop_write = hammer_vop_fifowrite,
140 .vop_access = hammer_vop_access,
141 .vop_close = hammer_vop_fifoclose,
142 .vop_getattr = hammer_vop_getattr,
143 .vop_inactive = hammer_vop_inactive,
144 .vop_reclaim = hammer_vop_reclaim,
145 .vop_setattr = hammer_vop_setattr
148 static int hammer_dounlink(struct nchandle *nch, struct vnode *dvp,
149 struct ucred *cred, int flags);
150 static int hammer_vop_strategy_read(struct vop_strategy_args *ap);
151 static int hammer_vop_strategy_write(struct vop_strategy_args *ap);
156 hammer_vop_vnoperate(struct vop_generic_args *)
158 return (VOCALL(&hammer_vnode_vops, ap));
163 * hammer_vop_fsync { vp, waitfor }
167 hammer_vop_fsync(struct vop_fsync_args *ap)
173 error = hammer_sync_inode(ip, ap->a_waitfor, 0);
178 * hammer_vop_read { vp, uio, ioflag, cred }
182 hammer_vop_read(struct vop_read_args *ap)
184 struct hammer_transaction trans;
193 if (ap->a_vp->v_type != VREG)
197 seqcount = ap->a_ioflag >> 16;
199 hammer_start_transaction(&trans, ip->hmp);
202 * Access the data in HAMMER_BUFSIZE blocks via the buffer cache.
205 while (uio->uio_resid > 0 && uio->uio_offset < ip->ino_rec.ino_size) {
206 offset = uio->uio_offset & HAMMER_BUFMASK;
208 error = cluster_read(ap->a_vp, ip->ino_rec.ino_size,
209 uio->uio_offset - offset, HAMMER_BUFSIZE,
210 MAXBSIZE, seqcount, &bp);
212 error = bread(ap->a_vp, uio->uio_offset - offset,
213 HAMMER_BUFSIZE, &bp);
218 /* bp->b_flags |= B_CLUSTEROK; temporarily disabled */
219 n = HAMMER_BUFSIZE - offset;
220 if (n > uio->uio_resid)
222 if (n > ip->ino_rec.ino_size - uio->uio_offset)
223 n = (int)(ip->ino_rec.ino_size - uio->uio_offset);
224 error = uiomove((char *)bp->b_data + offset, n, uio);
229 ip->ino_rec.ino_atime = trans.tid;
230 hammer_modify_inode(&trans, ip, HAMMER_INODE_ITIMES);
233 hammer_commit_transaction(&trans);
238 * hammer_vop_write { vp, uio, ioflag, cred }
242 hammer_vop_write(struct vop_write_args *ap)
244 struct hammer_transaction trans;
245 struct hammer_inode *ip;
253 if (ap->a_vp->v_type != VREG)
259 * Create a transaction to cover the operations we perform.
261 hammer_start_transaction(&trans, ip->hmp);
267 if (ap->a_ioflag & IO_APPEND)
268 uio->uio_offset = ip->ino_rec.ino_size;
271 * Check for illegal write offsets. Valid range is 0...2^63-1
273 if (uio->uio_offset < 0 || uio->uio_offset + uio->uio_resid <= 0) {
274 hammer_commit_transaction(&trans);
279 * Access the data in HAMMER_BUFSIZE blocks via the buffer cache.
281 while (uio->uio_resid > 0) {
282 offset = uio->uio_offset & HAMMER_BUFMASK;
283 if (uio->uio_segflg == UIO_NOCOPY) {
285 * Issuing a write with the same data backing the
286 * buffer. Instantiate the buffer to collect the
287 * backing vm pages, then read-in any missing bits.
289 * This case is used by vop_stdputpages().
291 bp = getblk(ap->a_vp, uio->uio_offset, HAMMER_BUFSIZE,
293 if ((bp->b_flags & B_CACHE) == 0) {
295 error = bread(ap->a_vp,
296 uio->uio_offset - offset,
297 HAMMER_BUFSIZE, &bp);
303 } else if (offset == 0 && uio->uio_resid >= HAMMER_BUFSIZE) {
305 * entirely overwrite the buffer
307 bp = getblk(ap->a_vp, uio->uio_offset, HAMMER_BUFSIZE,
309 } else if (offset == 0 && uio->uio_offset >= ip->ino_rec.ino_size) {
313 bp = getblk(ap->a_vp, uio->uio_offset, HAMMER_BUFSIZE,
318 * Partial overwrite, read in any missing bits then
319 * replace the portion being written.
321 error = bread(ap->a_vp, uio->uio_offset - offset,
322 HAMMER_BUFSIZE, &bp);
328 n = HAMMER_BUFSIZE - offset;
329 if (n > uio->uio_resid)
331 error = uiomove((char *)bp->b_data + offset, n, uio);
336 /* bp->b_flags |= B_CLUSTEROK; temporarily disabled */
337 if (ip->ino_rec.ino_size < uio->uio_offset) {
338 ip->ino_rec.ino_size = uio->uio_offset;
339 ip->ino_rec.ino_mtime = trans.tid;
340 flags = HAMMER_INODE_RDIRTY | HAMMER_INODE_ITIMES |
342 vnode_pager_setsize(ap->a_vp, ip->ino_rec.ino_size);
344 flags = HAMMER_INODE_TID;
346 hammer_modify_inode(&trans, ip, flags);
347 if (ap->a_ioflag & IO_SYNC) {
349 } else if (ap->a_ioflag & IO_DIRECT) {
356 hammer_abort_transaction(&trans);
358 hammer_commit_transaction(&trans);
363 * hammer_vop_access { vp, mode, cred }
367 hammer_vop_access(struct vop_access_args *ap)
369 struct hammer_inode *ip = VTOI(ap->a_vp);
374 uid = hammer_to_unix_xid(&ip->ino_data.uid);
375 gid = hammer_to_unix_xid(&ip->ino_data.gid);
377 error = vop_helper_access(ap, uid, gid, ip->ino_data.mode,
378 ip->ino_data.uflags);
383 * hammer_vop_advlock { vp, id, op, fl, flags }
387 hammer_vop_advlock(struct vop_advlock_args *ap)
389 struct hammer_inode *ip = VTOI(ap->a_vp);
391 return (lf_advlock(ap, &ip->advlock, ip->ino_rec.ino_size));
395 * hammer_vop_close { vp, fflag }
399 hammer_vop_close(struct vop_close_args *ap)
401 return (vop_stdclose(ap));
405 * hammer_vop_ncreate { nch, dvp, vpp, cred, vap }
407 * The operating system has already ensured that the directory entry
408 * does not exist and done all appropriate namespace locking.
412 hammer_vop_ncreate(struct vop_ncreate_args *ap)
414 struct hammer_transaction trans;
415 struct hammer_inode *dip;
416 struct hammer_inode *nip;
417 struct nchandle *nch;
421 dip = VTOI(ap->a_dvp);
424 * Create a transaction to cover the operations we perform.
426 hammer_start_transaction(&trans, dip->hmp);
429 * Create a new filesystem object of the requested type. The
430 * returned inode will be referenced but not locked.
433 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred, dip, &nip);
435 kprintf("hammer_create_inode error %d\n", error);
437 hammer_abort_transaction(&trans);
443 * Add the new filesystem object to the directory. This will also
444 * bump the inode's link count.
446 error = hammer_ip_add_directory(&trans, dip, nch->ncp, nip);
448 kprintf("hammer_ip_add_directory error %d\n", error);
454 hammer_rel_inode(nip, 0);
455 hammer_abort_transaction(&trans);
458 hammer_commit_transaction(&trans);
459 error = hammer_get_vnode(nip, LK_EXCLUSIVE, ap->a_vpp);
460 hammer_rel_inode(nip, 0);
462 cache_setunresolved(ap->a_nch);
463 cache_setvp(ap->a_nch, *ap->a_vpp);
470 * hammer_vop_getattr { vp, vap }
474 hammer_vop_getattr(struct vop_getattr_args *ap)
476 struct hammer_inode *ip = VTOI(ap->a_vp);
477 struct vattr *vap = ap->a_vap;
480 if (cache_check_fsmid_vp(ap->a_vp, &ip->fsmid) &&
481 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0 &&
486 hammer_itimes(ap->a_vp);
489 vap->va_fsid = ip->hmp->fsid_udev;
490 vap->va_fileid = ip->ino_rec.base.base.obj_id;
491 vap->va_mode = ip->ino_data.mode;
492 vap->va_nlink = ip->ino_rec.ino_nlinks;
493 vap->va_uid = hammer_to_unix_xid(&ip->ino_data.uid);
494 vap->va_gid = hammer_to_unix_xid(&ip->ino_data.gid);
497 vap->va_size = ip->ino_rec.ino_size;
498 hammer_to_timespec(ip->ino_rec.ino_atime, &vap->va_atime);
499 hammer_to_timespec(ip->ino_rec.ino_mtime, &vap->va_mtime);
500 hammer_to_timespec(ip->ino_data.ctime, &vap->va_ctime);
501 vap->va_flags = ip->ino_data.uflags;
502 vap->va_gen = 1; /* hammer inums are unique for all time */
503 vap->va_blocksize = 32768; /* XXX - extract from root volume */
504 vap->va_bytes = ip->ino_rec.ino_size;
505 vap->va_type = hammer_get_vnode_type(ip->ino_rec.base.base.obj_type);
506 vap->va_filerev = 0; /* XXX */
507 /* mtime uniquely identifies any adjustments made to the file */
508 vap->va_fsmid = ip->ino_rec.ino_mtime;
509 vap->va_uid_uuid = ip->ino_data.uid;
510 vap->va_gid_uuid = ip->ino_data.gid;
511 vap->va_fsid_uuid = ip->hmp->fsid;
512 vap->va_vaflags = VA_UID_UUID_VALID | VA_GID_UUID_VALID |
515 switch (ip->ino_rec.base.base.obj_type) {
516 case HAMMER_OBJTYPE_CDEV:
517 case HAMMER_OBJTYPE_BDEV:
518 vap->va_rmajor = ip->ino_data.rmajor;
519 vap->va_rminor = ip->ino_data.rminor;
529 * hammer_vop_nresolve { nch, dvp, cred }
531 * Locate the requested directory entry.
535 hammer_vop_nresolve(struct vop_nresolve_args *ap)
537 struct namecache *ncp;
541 struct hammer_cursor cursor;
542 union hammer_record_ondisk *rec;
550 * Misc initialization, plus handle as-of name extensions. Look for
551 * the '@@' extension. Note that as-of files and directories cannot
556 dip = VTOI(ap->a_dvp);
557 ncp = ap->a_nch->ncp;
558 asof = dip->obj_asof;
561 for (i = 0; i < nlen; ++i) {
562 if (ncp->nc_name[i] == '@' && ncp->nc_name[i+1] == '@') {
563 asof = hammer_now_tid() -
564 strtoq(ncp->nc_name + i + 2, NULL, 0) *
566 kprintf("ASOF %016llx\n", asof);
573 * Calculate the namekey and setup the key range for the scan. This
574 * works kinda like a chained hash table where the lower 32 bits
575 * of the namekey synthesize the chain.
577 * The key range is inclusive of both key_beg and key_end.
579 namekey = hammer_directory_namekey(ncp->nc_name, nlen);
581 hammer_init_cursor_ip(&cursor, dip);
582 cursor.key_beg.obj_id = dip->obj_id;
583 cursor.key_beg.key = namekey;
584 cursor.key_beg.create_tid = asof;
585 cursor.key_beg.delete_tid = 0;
586 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
587 cursor.key_beg.obj_type = 0;
589 cursor.key_end = cursor.key_beg;
590 cursor.key_end.key |= 0xFFFFFFFFULL;
591 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
594 * Scan all matching records (the chain), locate the one matching
595 * the requested path component.
597 * The hammer_ip_*() functions merge in-memory records with on-disk
598 * records for the purposes of the search.
600 error = hammer_ip_first(&cursor, dip);
603 error = hammer_ip_resolve_data(&cursor);
607 if (nlen == rec->entry.base.data_len &&
608 bcmp(ncp->nc_name, cursor.data, nlen) == 0) {
611 error = hammer_ip_next(&cursor);
614 ip = hammer_get_inode(dip->hmp, rec->entry.obj_id,
617 error = hammer_get_vnode(ip, LK_EXCLUSIVE, &vp);
618 hammer_rel_inode(ip, 0);
624 cache_setvp(ap->a_nch, vp);
627 } else if (error == ENOENT) {
628 cache_setvp(ap->a_nch, NULL);
630 hammer_done_cursor(&cursor);
635 * hammer_vop_nlookupdotdot { dvp, vpp, cred }
637 * Locate the parent directory of a directory vnode.
639 * dvp is referenced but not locked. *vpp must be returned referenced and
640 * locked. A parent_obj_id of 0 does not necessarily indicate that we are
641 * at the root, instead it could indicate that the directory we were in was
646 hammer_vop_nlookupdotdot(struct vop_nlookupdotdot_args *ap)
648 struct hammer_inode *dip;
649 u_int64_t parent_obj_id;
651 dip = VTOI(ap->a_dvp);
652 if ((parent_obj_id = dip->ino_data.parent_obj_id) == 0) {
656 return(hammer_vfs_vget(dip->hmp->mp, parent_obj_id, ap->a_vpp));
660 * hammer_vop_nlink { nch, dvp, vp, cred }
664 hammer_vop_nlink(struct vop_nlink_args *ap)
666 struct hammer_transaction trans;
667 struct hammer_inode *dip;
668 struct hammer_inode *ip;
669 struct nchandle *nch;
673 dip = VTOI(ap->a_dvp);
677 * Create a transaction to cover the operations we perform.
679 hammer_start_transaction(&trans, dip->hmp);
682 * Add the filesystem object to the directory. Note that neither
683 * dip nor ip are referenced or locked, but their vnodes are
684 * referenced. This function will bump the inode's link count.
686 error = hammer_ip_add_directory(&trans, dip, nch->ncp, ip);
692 hammer_abort_transaction(&trans);
694 cache_setunresolved(nch);
695 cache_setvp(nch, ap->a_vp);
696 hammer_commit_transaction(&trans);
702 * hammer_vop_nmkdir { nch, dvp, vpp, cred, vap }
704 * The operating system has already ensured that the directory entry
705 * does not exist and done all appropriate namespace locking.
709 hammer_vop_nmkdir(struct vop_nmkdir_args *ap)
711 struct hammer_transaction trans;
712 struct hammer_inode *dip;
713 struct hammer_inode *nip;
714 struct nchandle *nch;
718 dip = VTOI(ap->a_dvp);
721 * Create a transaction to cover the operations we perform.
723 hammer_start_transaction(&trans, dip->hmp);
726 * Create a new filesystem object of the requested type. The
727 * returned inode will be referenced but not locked.
729 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred, dip, &nip);
731 kprintf("hammer_mkdir error %d\n", error);
733 hammer_abort_transaction(&trans);
739 * Add the new filesystem object to the directory. This will also
740 * bump the inode's link count.
742 error = hammer_ip_add_directory(&trans, dip, nch->ncp, nip);
744 kprintf("hammer_mkdir (add) error %d\n", error);
750 hammer_rel_inode(nip, 0);
751 hammer_abort_transaction(&trans);
754 hammer_commit_transaction(&trans);
755 error = hammer_get_vnode(nip, LK_EXCLUSIVE, ap->a_vpp);
756 hammer_rel_inode(nip, 0);
758 cache_setunresolved(ap->a_nch);
759 cache_setvp(ap->a_nch, *ap->a_vpp);
766 * hammer_vop_nmknod { nch, dvp, vpp, cred, vap }
768 * The operating system has already ensured that the directory entry
769 * does not exist and done all appropriate namespace locking.
773 hammer_vop_nmknod(struct vop_nmknod_args *ap)
775 struct hammer_transaction trans;
776 struct hammer_inode *dip;
777 struct hammer_inode *nip;
778 struct nchandle *nch;
782 dip = VTOI(ap->a_dvp);
785 * Create a transaction to cover the operations we perform.
787 hammer_start_transaction(&trans, dip->hmp);
790 * Create a new filesystem object of the requested type. The
791 * returned inode will be referenced but not locked.
793 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred, dip, &nip);
795 hammer_abort_transaction(&trans);
801 * Add the new filesystem object to the directory. This will also
802 * bump the inode's link count.
804 error = hammer_ip_add_directory(&trans, dip, nch->ncp, nip);
810 hammer_rel_inode(nip, 0);
811 hammer_abort_transaction(&trans);
814 hammer_commit_transaction(&trans);
815 error = hammer_get_vnode(nip, LK_EXCLUSIVE, ap->a_vpp);
816 hammer_rel_inode(nip, 0);
818 cache_setunresolved(ap->a_nch);
819 cache_setvp(ap->a_nch, *ap->a_vpp);
826 * hammer_vop_open { vp, mode, cred, fp }
830 hammer_vop_open(struct vop_open_args *ap)
832 return(vop_stdopen(ap));
836 * hammer_vop_pathconf { vp, name, retval }
840 hammer_vop_pathconf(struct vop_pathconf_args *ap)
846 * hammer_vop_print { vp }
850 hammer_vop_print(struct vop_print_args *ap)
856 * hammer_vop_readdir { vp, uio, cred, *eofflag, *ncookies, off_t **cookies }
860 hammer_vop_readdir(struct vop_readdir_args *ap)
862 struct hammer_cursor cursor;
863 struct hammer_inode *ip;
865 hammer_record_ondisk_t rec;
866 hammer_base_elm_t base;
876 saveoff = uio->uio_offset;
878 if (ap->a_ncookies) {
879 ncookies = uio->uio_resid / 16 + 1;
882 cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
891 * Handle artificial entries
895 r = vop_write_dirent(&error, uio, ip->obj_id, DT_DIR, 1, ".");
899 cookies[cookie_index] = saveoff;
902 if (cookie_index == ncookies)
906 if (ip->ino_data.parent_obj_id) {
907 r = vop_write_dirent(&error, uio,
908 ip->ino_data.parent_obj_id,
911 r = vop_write_dirent(&error, uio,
912 ip->obj_id, DT_DIR, 2, "..");
917 cookies[cookie_index] = saveoff;
920 if (cookie_index == ncookies)
925 * Key range (begin and end inclusive) to scan. Directory keys
926 * directly translate to a 64 bit 'seek' position.
928 hammer_init_cursor_ip(&cursor, ip);
929 cursor.key_beg.obj_id = ip->obj_id;
930 cursor.key_beg.create_tid = ip->obj_asof;
931 cursor.key_beg.delete_tid = 0;
932 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
933 cursor.key_beg.obj_type = 0;
934 cursor.key_beg.key = saveoff;
936 cursor.key_end = cursor.key_beg;
937 cursor.key_end.key = HAMMER_MAX_KEY;
938 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
940 error = hammer_ip_first(&cursor, ip);
943 error = hammer_ip_resolve_data(&cursor);
947 base = &rec->base.base;
950 if (base->obj_id != ip->obj_id)
951 panic("readdir: bad record at %p", cursor.node);
953 r = vop_write_dirent(
954 &error, uio, rec->entry.obj_id,
955 hammer_get_dtype(rec->entry.base.base.obj_type),
956 rec->entry.base.data_len,
957 (void *)cursor.data);
962 cookies[cookie_index] = base->key;
964 if (cookie_index == ncookies)
966 error = hammer_ip_next(&cursor);
968 hammer_done_cursor(&cursor);
972 *ap->a_eofflag = (error == ENOENT);
973 uio->uio_offset = saveoff;
974 if (error && cookie_index == 0) {
978 kfree(cookies, M_TEMP);
980 *ap->a_cookies = NULL;
986 *ap->a_ncookies = cookie_index;
987 *ap->a_cookies = cookies;
994 * hammer_vop_readlink { vp, uio, cred }
998 hammer_vop_readlink(struct vop_readlink_args *ap)
1000 struct hammer_cursor cursor;
1001 struct hammer_inode *ip;
1004 ip = VTOI(ap->a_vp);
1005 hammer_init_cursor_ip(&cursor, ip);
1008 * Key range (begin and end inclusive) to scan. Directory keys
1009 * directly translate to a 64 bit 'seek' position.
1011 cursor.key_beg.obj_id = ip->obj_id;
1012 cursor.key_beg.create_tid = ip->obj_asof;
1013 cursor.key_beg.delete_tid = 0;
1014 cursor.key_beg.rec_type = HAMMER_RECTYPE_FIX;
1015 cursor.key_beg.obj_type = 0;
1016 cursor.key_beg.key = HAMMER_FIXKEY_SYMLINK;
1018 error = hammer_ip_lookup(&cursor, ip);
1020 error = hammer_ip_resolve_data(&cursor);
1022 error = uiomove((char *)cursor.data,
1023 cursor.record->generic.base.data_len,
1027 hammer_done_cursor(&cursor);
1032 * hammer_vop_nremove { nch, dvp, cred }
1036 hammer_vop_nremove(struct vop_nremove_args *ap)
1038 return(hammer_dounlink(ap->a_nch, ap->a_dvp, ap->a_cred, 0));
1042 * hammer_vop_nrename { fnch, tnch, fdvp, tdvp, cred }
1046 hammer_vop_nrename(struct vop_nrename_args *ap)
1048 struct hammer_transaction trans;
1049 struct namecache *fncp;
1050 struct namecache *tncp;
1051 struct hammer_inode *fdip;
1052 struct hammer_inode *tdip;
1053 struct hammer_inode *ip;
1054 struct hammer_cursor cursor;
1055 union hammer_record_ondisk *rec;
1059 fdip = VTOI(ap->a_fdvp);
1060 tdip = VTOI(ap->a_tdvp);
1061 fncp = ap->a_fnch->ncp;
1062 tncp = ap->a_tnch->ncp;
1063 ip = VTOI(fncp->nc_vp);
1064 KKASSERT(ip != NULL);
1065 hammer_start_transaction(&trans, fdip->hmp);
1068 * Remove tncp from the target directory and then link ip as
1069 * tncp. XXX pass trans to dounlink
1071 error = hammer_dounlink(ap->a_tnch, ap->a_tdvp, ap->a_cred, 0);
1072 if (error == 0 || error == ENOENT)
1073 error = hammer_ip_add_directory(&trans, tdip, tncp, ip);
1075 goto failed; /* XXX */
1078 * Locate the record in the originating directory and remove it.
1080 * Calculate the namekey and setup the key range for the scan. This
1081 * works kinda like a chained hash table where the lower 32 bits
1082 * of the namekey synthesize the chain.
1084 * The key range is inclusive of both key_beg and key_end.
1086 namekey = hammer_directory_namekey(fncp->nc_name, fncp->nc_nlen);
1088 hammer_init_cursor_ip(&cursor, fdip);
1089 cursor.key_beg.obj_id = fdip->obj_id;
1090 cursor.key_beg.key = namekey;
1091 cursor.key_beg.create_tid = fdip->obj_asof;
1092 cursor.key_beg.delete_tid = 0;
1093 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1094 cursor.key_beg.obj_type = 0;
1096 cursor.key_end = cursor.key_beg;
1097 cursor.key_end.key |= 0xFFFFFFFFULL;
1098 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
1101 * Scan all matching records (the chain), locate the one matching
1102 * the requested path component.
1104 * The hammer_ip_*() functions merge in-memory records with on-disk
1105 * records for the purposes of the search.
1107 error = hammer_ip_first(&cursor, fdip);
1108 while (error == 0) {
1109 if (hammer_ip_resolve_data(&cursor) != 0)
1111 rec = cursor.record;
1112 if (fncp->nc_nlen == rec->entry.base.data_len &&
1113 bcmp(fncp->nc_name, cursor.data, fncp->nc_nlen) == 0) {
1116 error = hammer_ip_next(&cursor);
1120 * If all is ok we have to get the inode so we can adjust nlinks.
1124 error = hammer_ip_del_directory(&trans, &cursor, fdip, ip);
1127 cache_rename(ap->a_fnch, ap->a_tnch);
1128 cache_setvp(ap->a_tnch, ip->vp);
1131 hammer_done_cursor(&cursor);
1134 hammer_commit_transaction(&trans);
1136 hammer_abort_transaction(&trans);
1142 * hammer_vop_nrmdir { nch, dvp, cred }
1146 hammer_vop_nrmdir(struct vop_nrmdir_args *ap)
1148 return(hammer_dounlink(ap->a_nch, ap->a_dvp, ap->a_cred, 0));
1152 * hammer_vop_setattr { vp, vap, cred }
1156 hammer_vop_setattr(struct vop_setattr_args *ap)
1158 struct hammer_transaction trans;
1159 struct hammer_cursor *spike = NULL;
1161 struct hammer_inode *ip;
1164 int64_t aligned_size;
1169 ip = ap->a_vp->v_data;
1172 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
1175 hammer_start_transaction(&trans, ip->hmp);
1178 if (vap->va_flags != VNOVAL) {
1179 flags = ip->ino_data.uflags;
1180 error = vop_helper_setattr_flags(&flags, vap->va_flags,
1181 hammer_to_unix_xid(&ip->ino_data.uid),
1184 if (ip->ino_data.uflags != flags) {
1185 ip->ino_data.uflags = flags;
1186 modflags |= HAMMER_INODE_DDIRTY;
1188 if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) {
1195 if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) {
1199 if (vap->va_uid != (uid_t)VNOVAL) {
1200 hammer_guid_to_uuid(&uuid, vap->va_uid);
1201 if (bcmp(&uuid, &ip->ino_data.uid, sizeof(uuid)) != 0) {
1202 ip->ino_data.uid = uuid;
1203 modflags |= HAMMER_INODE_DDIRTY;
1206 if (vap->va_gid != (uid_t)VNOVAL) {
1207 hammer_guid_to_uuid(&uuid, vap->va_gid);
1208 if (bcmp(&uuid, &ip->ino_data.gid, sizeof(uuid)) != 0) {
1209 ip->ino_data.gid = uuid;
1210 modflags |= HAMMER_INODE_DDIRTY;
1213 while (vap->va_size != VNOVAL && ip->ino_rec.ino_size != vap->va_size) {
1214 switch(ap->a_vp->v_type) {
1216 if (vap->va_size < ip->ino_rec.ino_size) {
1217 vtruncbuf(ap->a_vp, vap->va_size,
1219 } else if (vap->va_size > ip->ino_rec.ino_size) {
1220 vnode_pager_setsize(ap->a_vp, vap->va_size);
1222 aligned_size = (vap->va_size + HAMMER_BUFMASK) &
1223 ~(int64_t)HAMMER_BUFMASK;
1224 error = hammer_ip_delete_range(&trans, ip,
1226 0x7FFFFFFFFFFFFFFFLL,
1228 ip->ino_rec.ino_size = vap->va_size;
1229 modflags |= HAMMER_INODE_RDIRTY;
1232 error = hammer_ip_delete_range(&trans, ip,
1234 0x7FFFFFFFFFFFFFFFLL,
1236 ip->ino_rec.ino_size = vap->va_size;
1237 modflags |= HAMMER_INODE_RDIRTY;
1243 if (error == ENOSPC) {
1244 error = hammer_spike(&spike);
1248 KKASSERT(spike == NULL);
1251 if (vap->va_atime.tv_sec != VNOVAL) {
1252 ip->ino_rec.ino_atime =
1253 hammer_timespec_to_transid(&vap->va_atime);
1254 modflags |= HAMMER_INODE_ITIMES;
1256 if (vap->va_mtime.tv_sec != VNOVAL) {
1257 ip->ino_rec.ino_mtime =
1258 hammer_timespec_to_transid(&vap->va_mtime);
1259 modflags |= HAMMER_INODE_ITIMES;
1261 if (vap->va_mode != (mode_t)VNOVAL) {
1262 if (ip->ino_data.mode != vap->va_mode) {
1263 ip->ino_data.mode = vap->va_mode;
1264 modflags |= HAMMER_INODE_DDIRTY;
1269 hammer_abort_transaction(&trans);
1271 if (modflags & (HAMMER_INODE_RDIRTY | HAMMER_INODE_DDIRTY))
1272 modflags |= HAMMER_INODE_TID;
1273 hammer_modify_inode(&trans, ip, modflags);
1274 hammer_commit_transaction(&trans);
1280 * hammer_vop_nsymlink { nch, dvp, vpp, cred, vap, target }
1284 hammer_vop_nsymlink(struct vop_nsymlink_args *ap)
1286 struct hammer_transaction trans;
1287 struct hammer_inode *dip;
1288 struct hammer_inode *nip;
1289 struct nchandle *nch;
1290 hammer_record_t record;
1294 ap->a_vap->va_type = VLNK;
1297 dip = VTOI(ap->a_dvp);
1300 * Create a transaction to cover the operations we perform.
1302 hammer_start_transaction(&trans, dip->hmp);
1305 * Create a new filesystem object of the requested type. The
1306 * returned inode will be referenced but not locked.
1309 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred, dip, &nip);
1311 hammer_abort_transaction(&trans);
1317 * Add the new filesystem object to the directory. This will also
1318 * bump the inode's link count.
1320 error = hammer_ip_add_directory(&trans, dip, nch->ncp, nip);
1323 * Add a record representing the symlink. symlink stores the link
1324 * as pure data, not a string, and is no \0 terminated.
1327 record = hammer_alloc_mem_record(nip);
1328 bytes = strlen(ap->a_target);
1330 record->rec.generic.base.base.key = HAMMER_FIXKEY_SYMLINK;
1331 record->rec.generic.base.base.rec_type = HAMMER_RECTYPE_FIX;
1332 record->rec.generic.base.data_len = bytes;
1333 if (bytes <= sizeof(record->rec.generic.filler)) {
1334 record->data = (void *)record->rec.generic.filler;
1335 bcopy(ap->a_target, record->data, bytes);
1337 record->data = (void *)ap->a_target;
1338 /* will be reallocated by routine below */
1340 error = hammer_ip_add_record(&trans, record);
1347 hammer_rel_inode(nip, 0);
1348 hammer_abort_transaction(&trans);
1351 hammer_commit_transaction(&trans);
1352 error = hammer_get_vnode(nip, LK_EXCLUSIVE, ap->a_vpp);
1353 hammer_rel_inode(nip, 0);
1355 cache_setunresolved(ap->a_nch);
1356 cache_setvp(ap->a_nch, *ap->a_vpp);
1363 * hammer_vop_nwhiteout { nch, dvp, cred, flags }
1367 hammer_vop_nwhiteout(struct vop_nwhiteout_args *ap)
1369 return(hammer_dounlink(ap->a_nch, ap->a_dvp, ap->a_cred, ap->a_flags));
1373 * hammer_vop_strategy { vp, bio }
1375 * Strategy call, used for regular file read & write only. Note that the
1376 * bp may represent a cluster.
1378 * To simplify operation and allow better optimizations in the future,
1379 * this code does not make any assumptions with regards to buffer alignment
1384 hammer_vop_strategy(struct vop_strategy_args *ap)
1389 bp = ap->a_bio->bio_buf;
1393 error = hammer_vop_strategy_read(ap);
1396 error = hammer_vop_strategy_write(ap);
1402 bp->b_error = error;
1404 bp->b_flags |= B_ERROR;
1410 * Read from a regular file. Iterate the related records and fill in the
1411 * BIO/BUF. Gaps are zero-filled.
1413 * The support code in hammer_object.c should be used to deal with mixed
1414 * in-memory and on-disk records.
1420 hammer_vop_strategy_read(struct vop_strategy_args *ap)
1422 struct hammer_inode *ip = ap->a_vp->v_data;
1423 struct hammer_cursor cursor;
1424 hammer_record_ondisk_t rec;
1425 hammer_base_elm_t base;
1439 hammer_init_cursor_ip(&cursor, ip);
1442 * Key range (begin and end inclusive) to scan. Note that the key's
1443 * stored in the actual records represent BASE+LEN, not BASE. The
1444 * first record containing bio_offset will have a key > bio_offset.
1446 cursor.key_beg.obj_id = ip->obj_id;
1447 cursor.key_beg.create_tid = ip->obj_asof;
1448 cursor.key_beg.delete_tid = 0;
1449 cursor.key_beg.obj_type = 0;
1450 cursor.key_beg.key = bio->bio_offset + 1;
1452 cursor.key_end = cursor.key_beg;
1453 if (ip->ino_rec.base.base.obj_type == HAMMER_OBJTYPE_DBFILE) {
1454 cursor.key_beg.rec_type = HAMMER_RECTYPE_DB;
1455 cursor.key_end.rec_type = HAMMER_RECTYPE_DB;
1456 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
1458 ran_end = bio->bio_offset + bp->b_bufsize;
1459 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
1460 cursor.key_end.rec_type = HAMMER_RECTYPE_DATA;
1461 tmp64 = ran_end + MAXPHYS + 1; /* work-around GCC-4 bug */
1462 if (tmp64 < ran_end)
1463 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
1465 cursor.key_end.key = ran_end + MAXPHYS + 1;
1467 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
1469 error = hammer_ip_first(&cursor, ip);
1472 while (error == 0) {
1473 error = hammer_ip_resolve_data(&cursor);
1476 rec = cursor.record;
1477 base = &rec->base.base;
1479 rec_offset = base->key - rec->data.base.data_len;
1482 * Calculate the gap, if any, and zero-fill it.
1484 n = (int)(rec_offset - (bio->bio_offset + boff));
1486 if (n > bp->b_bufsize - boff)
1487 n = bp->b_bufsize - boff;
1488 kprintf("zfill %d bytes\n", n);
1489 bzero((char *)bp->b_data + boff, n);
1495 * Calculate the data offset in the record and the number
1496 * of bytes we can copy.
1498 * Note there is a degenerate case here where boff may
1499 * already be at bp->b_bufsize.
1502 n = rec->data.base.data_len - roff;
1504 if (n > bp->b_bufsize - boff)
1505 n = bp->b_bufsize - boff;
1506 bcopy((char *)cursor.data + roff, (char *)bp->b_data + boff, n);
1508 if (boff == bp->b_bufsize)
1510 error = hammer_ip_next(&cursor);
1512 hammer_done_cursor(&cursor);
1515 * There may have been a gap after the last record
1517 if (error == ENOENT)
1519 if (error == 0 && boff != bp->b_bufsize) {
1520 KKASSERT(boff < bp->b_bufsize);
1521 bzero((char *)bp->b_data + boff, bp->b_bufsize - boff);
1522 /* boff = bp->b_bufsize; */
1529 * Write to a regular file. Iterate the related records and mark for
1530 * deletion. If existing edge records (left and right side) overlap our
1531 * write they have to be marked deleted and new records created, usually
1532 * referencing a portion of the original data. Then add a record to
1533 * represent the buffer.
1535 * The support code in hammer_object.c should be used to deal with mixed
1536 * in-memory and on-disk records.
1540 hammer_vop_strategy_write(struct vop_strategy_args *ap)
1542 struct hammer_transaction trans;
1543 struct hammer_cursor *spike = NULL;
1551 ip = ap->a_vp->v_data;
1552 hammer_start_transaction(&trans, ip->hmp);
1556 * Delete any records overlapping our range. This function will
1557 * (eventually) properly truncate partial overlaps.
1559 if (ip->ino_rec.base.base.obj_type == HAMMER_OBJTYPE_DBFILE) {
1560 error = hammer_ip_delete_range(&trans, ip, bio->bio_offset,
1561 bio->bio_offset, &spike);
1563 error = hammer_ip_delete_range(&trans, ip, bio->bio_offset,
1570 * Add a single record to cover the write
1573 error = hammer_ip_sync_data(&trans, ip, bio->bio_offset,
1574 bp->b_data, bp->b_bufsize,
1579 * If we ran out of space the spike structure will be filled in
1580 * and we must call hammer_spike with it, then retry.
1582 if (error == ENOSPC) {
1583 error = hammer_spike(&spike);
1587 KKASSERT(spike == NULL);
1590 * If an error occured abort the transaction
1593 /* XXX undo deletion */
1594 hammer_abort_transaction(&trans);
1595 bp->b_resid = bp->b_bufsize;
1597 hammer_commit_transaction(&trans);
1604 * dounlink - disconnect a directory entry
1606 * XXX whiteout support not really in yet
1609 hammer_dounlink(struct nchandle *nch, struct vnode *dvp, struct ucred *cred,
1612 struct hammer_transaction trans;
1613 struct namecache *ncp;
1616 hammer_record_ondisk_t rec;
1617 struct hammer_cursor cursor;
1622 * Calculate the namekey and setup the key range for the scan. This
1623 * works kinda like a chained hash table where the lower 32 bits
1624 * of the namekey synthesize the chain.
1626 * The key range is inclusive of both key_beg and key_end.
1630 namekey = hammer_directory_namekey(ncp->nc_name, ncp->nc_nlen);
1632 hammer_init_cursor_ip(&cursor, dip);
1633 cursor.key_beg.obj_id = dip->obj_id;
1634 cursor.key_beg.key = namekey;
1635 cursor.key_beg.create_tid = dip->obj_asof;
1636 cursor.key_beg.delete_tid = 0;
1637 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1638 cursor.key_beg.obj_type = 0;
1640 cursor.key_end = cursor.key_beg;
1641 cursor.key_end.key |= 0xFFFFFFFFULL;
1642 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
1644 hammer_start_transaction(&trans, dip->hmp);
1647 * Scan all matching records (the chain), locate the one matching
1648 * the requested path component. info->last_error contains the
1649 * error code on search termination and could be 0, ENOENT, or
1652 * The hammer_ip_*() functions merge in-memory records with on-disk
1653 * records for the purposes of the search.
1655 error = hammer_ip_first(&cursor, dip);
1656 while (error == 0) {
1657 error = hammer_ip_resolve_data(&cursor);
1660 rec = cursor.record;
1661 if (ncp->nc_nlen == rec->entry.base.data_len &&
1662 bcmp(ncp->nc_name, cursor.data, ncp->nc_nlen) == 0) {
1665 error = hammer_ip_next(&cursor);
1669 * If all is ok we have to get the inode so we can adjust nlinks.
1671 * If the target is a directory, it must be empty.
1674 ip = hammer_get_inode(dip->hmp, rec->entry.obj_id,
1675 dip->hmp->asof, &error);
1676 if (error == 0 && ip->ino_rec.base.base.obj_type ==
1677 HAMMER_OBJTYPE_DIRECTORY) {
1678 error = hammer_ip_check_directory_empty(&trans, ip);
1681 error = hammer_ip_del_directory(&trans, &cursor, dip, ip);
1683 cache_setunresolved(nch);
1684 cache_setvp(nch, NULL);
1687 cache_inval_vp(ip->vp, CINV_DESTROY);
1689 hammer_rel_inode(ip, 0);
1693 hammer_commit_transaction(&trans);
1695 hammer_abort_transaction(&trans);
1696 hammer_done_cursor(&cursor);
1700 /************************************************************************
1701 * FIFO AND SPECFS OPS *
1702 ************************************************************************
1707 hammer_vop_fifoclose (struct vop_close_args *ap)
1709 /* XXX update itimes */
1710 return (VOCALL(&fifo_vnode_vops, &ap->a_head));
1714 hammer_vop_fiforead (struct vop_read_args *ap)
1718 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
1719 /* XXX update access time */
1724 hammer_vop_fifowrite (struct vop_write_args *ap)
1728 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
1729 /* XXX update access time */
1734 hammer_vop_specclose (struct vop_close_args *ap)
1736 /* XXX update itimes */
1737 return (VOCALL(&spec_vnode_vops, &ap->a_head));
1741 hammer_vop_specread (struct vop_read_args *ap)
1743 /* XXX update access time */
1744 return (VOCALL(&spec_vnode_vops, &ap->a_head));
1748 hammer_vop_specwrite (struct vop_write_args *ap)
1750 /* XXX update last change time */
1751 return (VOCALL(&spec_vnode_vops, &ap->a_head));