2 * Copyright (c) 2011-2012 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@dragonflybsd.org>
6 * by Venkatesh Srinivas <vsrinivas@dragonflybsd.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
18 * 3. Neither the name of The DragonFly Project nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific, prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/fcntl.h>
41 #include <sys/namei.h>
42 #include <sys/mount.h>
43 #include <sys/vnode.h>
44 #include <sys/mountctl.h>
45 #include <sys/dirent.h>
50 #define ZFOFFSET (-2LL)
52 static int hammer2_read_file(hammer2_inode_t *ip, struct uio *uio,
54 static int hammer2_write_file(hammer2_inode_t *ip, hammer2_chain_t **chainp,
55 struct uio *uio, int ioflag, int seqcount);
56 static hammer2_off_t hammer2_assign_physical(hammer2_inode_t *ip,
57 hammer2_key_t lbase, int lblksize, int *errorp);
58 static void hammer2_extend_file(hammer2_inode_t *ip, hammer2_key_t nsize);
59 static void hammer2_truncate_file(hammer2_inode_t *ip, hammer2_key_t nsize);
63 hammer2_knote(struct vnode *vp, int flags)
66 KNOTE(&vp->v_pollinfo.vpi_kqinfo.ki_note, flags);
70 * Last reference to a vnode is going away but it is still cached.
74 hammer2_vop_inactive(struct vop_inactive_args *ap)
76 hammer2_chain_t *chain;
80 struct hammer2_mount *hmp;
95 * Detect updates to the embedded data which may be synchronized by
96 * the strategy code. Simply mark the inode modified so it gets
97 * picked up by our normal flush.
99 chain = hammer2_inode_lock_ex(ip);
100 if (ip->flags & HAMMER2_INODE_DIRTYEMBED) {
101 atomic_clear_int(&ip->flags, HAMMER2_INODE_DIRTYEMBED);
102 hammer2_chain_modify(ip->hmp, chain, 0);
106 * Check for deleted inodes and recycle immediately.
108 if (chain && (chain->flags & HAMMER2_CHAIN_DELETED)) {
109 hammer2_inode_unlock_ex(ip, chain);
112 hammer2_inode_unlock_ex(ip, chain);
118 * Reclaim a vnode so that it can be reused; after the inode is
119 * disassociated, the filesystem must manage it alone.
123 hammer2_vop_reclaim(struct vop_reclaim_args *ap)
125 hammer2_chain_t *chain;
127 hammer2_mount_t *hmp;
137 * Set SUBMODIFIED so we can detect and propagate the DESTROYED
138 * bit in the flush code.
140 chain = hammer2_inode_lock_ex(ip);
143 if (chain->flags & HAMMER2_CHAIN_DELETED) {
144 KKASSERT(chain->flags & HAMMER2_CHAIN_DELETED);
145 atomic_set_int(&chain->flags, HAMMER2_CHAIN_DESTROYED |
146 HAMMER2_CHAIN_SUBMODIFIED);
148 hammer2_chain_flush(hmp, chain, 0);
149 if (ip->refs > 2) /* (our lock + vp ref) */
150 hammer2_inode_unlock_ex(ip, chain); /* unlock */
152 hammer2_inode_put(ip, chain); /* unlock & disconnect */
153 hammer2_inode_drop(ip); /* vp ref */
156 * XXX handle background sync when ip dirty, kernel will no longer
157 * notify us regarding this inode because there is no longer a
158 * vnode attached to it.
166 hammer2_vop_fsync(struct vop_fsync_args *ap)
168 hammer2_chain_t *chain;
170 hammer2_mount_t *hmp;
177 chain = hammer2_inode_lock_ex(ip);
178 vfsync(vp, ap->a_waitfor, 1, NULL, NULL);
181 * Detect updates to the embedded data which may be synchronized by
182 * the strategy code. Simply mark the inode modified so it gets
183 * picked up by our normal flush.
185 if (ip->flags & HAMMER2_INODE_DIRTYEMBED) {
186 atomic_clear_int(&ip->flags, HAMMER2_INODE_DIRTYEMBED);
187 hammer2_chain_modify(hmp, chain, 0);
191 * Calling chain_flush here creates a lot of duplicative
192 * COW operations due to non-optimal vnode ordering.
194 * Only do it for an actual fsync() syscall. The other forms
195 * which call this function will eventually call chain_flush
196 * on the volume root as a catch-all, which is far more optimal.
198 atomic_clear_int(&ip->flags, HAMMER2_INODE_MODIFIED);
199 if (ap->a_flags & VOP_FSYNC_SYSCALL)
200 hammer2_chain_flush(hmp, chain, 0);
201 hammer2_inode_unlock_ex(ip, chain);
207 hammer2_vop_access(struct vop_access_args *ap)
209 hammer2_inode_t *ip = VTOI(ap->a_vp);
210 hammer2_chain_t *chain;
211 hammer2_inode_data_t *ipdata;
216 chain = hammer2_inode_lock_sh(ip);
217 ipdata = &chain->data->ipdata;
218 uid = hammer2_to_unix_xid(&ipdata->uid);
219 gid = hammer2_to_unix_xid(&ipdata->gid);
220 error = vop_helper_access(ap, uid, gid, ipdata->mode, ipdata->uflags);
221 hammer2_inode_unlock_sh(ip, chain);
228 hammer2_vop_getattr(struct vop_getattr_args *ap)
230 hammer2_inode_data_t *ipdata;
231 hammer2_pfsmount_t *pmp;
233 hammer2_chain_t *chain;
243 chain = hammer2_inode_lock_sh(ip);
244 ipdata = &chain->data->ipdata;
246 vap->va_fsid = pmp->mp->mnt_stat.f_fsid.val[0];
247 vap->va_fileid = ipdata->inum;
248 vap->va_mode = ipdata->mode;
249 vap->va_nlink = ipdata->nlinks;
250 vap->va_uid = hammer2_to_unix_xid(&ipdata->uid);
251 vap->va_gid = hammer2_to_unix_xid(&ipdata->gid);
254 vap->va_size = ipdata->size;
255 vap->va_blocksize = HAMMER2_PBUFSIZE;
256 vap->va_flags = ipdata->uflags;
257 hammer2_time_to_timespec(ipdata->ctime, &vap->va_ctime);
258 hammer2_time_to_timespec(ipdata->mtime, &vap->va_mtime);
259 hammer2_time_to_timespec(ipdata->mtime, &vap->va_atime);
261 vap->va_bytes = vap->va_size; /* XXX */
262 vap->va_type = hammer2_get_vtype(chain);
264 vap->va_uid_uuid = ipdata->uid;
265 vap->va_gid_uuid = ipdata->gid;
266 vap->va_vaflags = VA_UID_UUID_VALID | VA_GID_UUID_VALID |
269 hammer2_inode_unlock_sh(ip, chain);
276 hammer2_vop_setattr(struct vop_setattr_args *ap)
278 hammer2_inode_data_t *ipdata;
279 hammer2_chain_t *chain;
281 hammer2_mount_t *hmp;
291 hammer2_update_time(&ctime);
299 chain = hammer2_inode_lock_ex(ip);
300 ipdata = &chain->data->ipdata;
303 if (vap->va_flags != VNOVAL) {
306 flags = ipdata->uflags;
307 error = vop_helper_setattr_flags(&flags, vap->va_flags,
308 hammer2_to_unix_xid(&ipdata->uid),
311 if (ipdata->uflags != flags) {
312 hammer2_chain_modify(hmp, chain, 0);
313 ipdata->uflags = flags;
314 ipdata->ctime = ctime;
315 kflags |= NOTE_ATTRIB;
317 if (ipdata->uflags & (IMMUTABLE | APPEND)) {
324 if (ipdata->uflags & (IMMUTABLE | APPEND)) {
328 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
329 mode_t cur_mode = ipdata->mode;
330 uid_t cur_uid = hammer2_to_unix_xid(&ipdata->uid);
331 gid_t cur_gid = hammer2_to_unix_xid(&ipdata->gid);
335 error = vop_helper_chown(ap->a_vp, vap->va_uid, vap->va_gid,
337 &cur_uid, &cur_gid, &cur_mode);
339 hammer2_guid_to_uuid(&uuid_uid, cur_uid);
340 hammer2_guid_to_uuid(&uuid_gid, cur_gid);
341 if (bcmp(&uuid_uid, &ipdata->uid, sizeof(uuid_uid)) ||
342 bcmp(&uuid_gid, &ipdata->gid, sizeof(uuid_gid)) ||
343 ipdata->mode != cur_mode
345 hammer2_chain_modify(hmp, chain, 0);
346 ipdata->uid = uuid_uid;
347 ipdata->gid = uuid_gid;
348 ipdata->mode = cur_mode;
349 ipdata->ctime = ctime;
351 kflags |= NOTE_ATTRIB;
358 if (vap->va_size != VNOVAL && ipdata->size != vap->va_size) {
361 if (vap->va_size == ipdata->size)
363 if (vap->va_size < ipdata->size) {
364 hammer2_truncate_file(ip, vap->va_size);
366 hammer2_extend_file(ip, vap->va_size);
376 /* atime not supported */
377 if (vap->va_atime.tv_sec != VNOVAL) {
378 hammer2_chain_modify(hmp, chain, 0);
379 ipdata->atime = hammer2_timespec_to_time(&vap->va_atime);
380 kflags |= NOTE_ATTRIB;
383 if (vap->va_mtime.tv_sec != VNOVAL) {
384 hammer2_chain_modify(hmp, chain, 0);
385 ipdata->mtime = hammer2_timespec_to_time(&vap->va_mtime);
386 kflags |= NOTE_ATTRIB;
388 if (vap->va_mode != (mode_t)VNOVAL) {
389 mode_t cur_mode = ipdata->mode;
390 uid_t cur_uid = hammer2_to_unix_xid(&ipdata->uid);
391 gid_t cur_gid = hammer2_to_unix_xid(&ipdata->gid);
393 error = vop_helper_chmod(ap->a_vp, vap->va_mode, ap->a_cred,
394 cur_uid, cur_gid, &cur_mode);
395 if (error == 0 && ipdata->mode != cur_mode) {
396 hammer2_chain_modify(hmp, chain, 0);
397 ipdata->mode = cur_mode;
398 ipdata->ctime = ctime;
399 kflags |= NOTE_ATTRIB;
403 hammer2_inode_unlock_ex(ip, chain);
409 hammer2_vop_readdir(struct vop_readdir_args *ap)
411 hammer2_inode_data_t *ipdata;
412 hammer2_mount_t *hmp;
414 hammer2_inode_t *xip;
415 hammer2_chain_t *parent;
416 hammer2_chain_t *xparent;
417 hammer2_chain_t *chain;
432 saveoff = uio->uio_offset;
435 * Setup cookies directory entry cookies if requested
437 if (ap->a_ncookies) {
438 ncookies = uio->uio_resid / 16 + 1;
441 cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
448 parent = hammer2_inode_lock_sh(ip);
449 ipdata = &parent->data->ipdata;
452 * Handle artificial entries. To ensure that only positive 64 bit
453 * quantities are returned to userland we always strip off bit 63.
454 * The hash code is designed such that codes 0x0000-0x7FFF are not
455 * used, allowing us to use these codes for articial entries.
457 * Entry 0 is used for '.' and entry 1 is used for '..'. Do not
458 * allow '..' to cross the mount point into (e.g.) the super-root.
461 chain = (void *)(intptr_t)-1; /* non-NULL for early goto done case */
464 inum = ipdata->inum & HAMMER2_DIRHASH_USERMSK;
465 r = vop_write_dirent(&error, uio, inum, DT_DIR, 1, ".");
469 cookies[cookie_index] = saveoff;
472 if (cookie_index == ncookies)
478 * Be careful with lockorder when accessing ".."
480 * (parent is the current dir. xip is the parent dir).
482 inum = parent->data->ipdata.inum & HAMMER2_DIRHASH_USERMSK;
483 while (ip->pip != NULL && ip != ip->pmp->iroot) {
485 hammer2_inode_ref(xip);
486 hammer2_inode_unlock_sh(ip, parent);
487 xparent = hammer2_inode_lock_sh(xip);
488 parent = hammer2_inode_lock_sh(ip);
489 hammer2_inode_drop(xip);
490 if (xip == ip->pip) {
491 inum = xparent->data->ipdata.inum &
492 HAMMER2_DIRHASH_USERMSK;
493 hammer2_inode_unlock_sh(xip, xparent);
496 hammer2_inode_unlock_sh(xip, xparent);
498 r = vop_write_dirent(&error, uio, inum, DT_DIR, 2, "..");
502 cookies[cookie_index] = saveoff;
505 if (cookie_index == ncookies)
509 lkey = saveoff | HAMMER2_DIRHASH_VISIBLE;
512 * parent is the inode chain, already locked for us. Don't
513 * double lock shared locks as this will screw up upgrades.
518 chain = hammer2_chain_lookup(hmp, &parent, lkey, lkey,
519 HAMMER2_LOOKUP_SHARED);
521 chain = hammer2_chain_lookup(hmp, &parent,
522 lkey, (hammer2_key_t)-1,
523 HAMMER2_LOOKUP_SHARED);
526 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE) {
527 dtype = hammer2_get_dtype(chain);
528 saveoff = chain->bref.key & HAMMER2_DIRHASH_USERMSK;
529 r = vop_write_dirent(&error, uio,
530 chain->data->ipdata.inum &
531 HAMMER2_DIRHASH_USERMSK,
533 chain->data->ipdata.name_len,
534 chain->data->ipdata.filename);
538 cookies[cookie_index] = saveoff;
541 /* XXX chain error */
542 kprintf("bad chain type readdir %d\n",
547 * Keys may not be returned in order so once we have a
548 * placemarker (chain) the scan must allow the full range
549 * or some entries will be missed.
551 chain = hammer2_chain_next(hmp, &parent, chain,
552 HAMMER2_DIRHASH_VISIBLE,
554 HAMMER2_LOOKUP_SHARED);
556 saveoff = (chain->bref.key &
557 HAMMER2_DIRHASH_USERMSK) + 1;
559 saveoff = (hammer2_key_t)-1;
561 if (cookie_index == ncookies)
565 hammer2_chain_unlock(hmp, chain);
567 hammer2_inode_unlock_sh(ip, parent);
569 *ap->a_eofflag = (chain == NULL);
570 uio->uio_offset = saveoff & ~HAMMER2_DIRHASH_VISIBLE;
571 if (error && cookie_index == 0) {
573 kfree(cookies, M_TEMP);
575 *ap->a_cookies = NULL;
579 *ap->a_ncookies = cookie_index;
580 *ap->a_cookies = cookies;
587 * hammer2_vop_readlink { vp, uio, cred }
591 hammer2_vop_readlink(struct vop_readlink_args *ap)
594 hammer2_mount_t *hmp;
599 if (vp->v_type != VLNK)
604 error = hammer2_read_file(ip, ap->a_uio, 0);
610 hammer2_vop_read(struct vop_read_args *ap)
613 hammer2_mount_t *hmp;
621 * Read operations supported on this vnode?
624 if (vp->v_type != VREG)
635 seqcount = ap->a_ioflag >> 16;
636 bigread = (uio->uio_resid > 100 * 1024 * 1024);
638 error = hammer2_read_file(ip, uio, seqcount);
644 hammer2_vop_write(struct vop_write_args *ap)
646 hammer2_chain_t *chain;
647 hammer2_mount_t *hmp;
657 * Read operations supported on this vnode?
660 if (vp->v_type != VREG)
673 seqcount = ap->a_ioflag >> 16;
674 bigwrite = (uio->uio_resid > 100 * 1024 * 1024);
677 * Check resource limit
679 if (uio->uio_resid > 0 && (td = uio->uio_td) != NULL && td->td_proc &&
680 uio->uio_offset + uio->uio_resid >
681 td->td_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
682 lwpsignal(td->td_proc, td->td_lwp, SIGXFSZ);
686 bigwrite = (uio->uio_resid > 100 * 1024 * 1024);
689 * ip must be locked if extending the file.
690 * ip must be locked to avoid racing a truncation.
692 * ip must be marked modified, particularly because the write
693 * might wind up being copied into the embedded data area.
695 chain = hammer2_inode_lock_ex(ip);
696 error = hammer2_write_file(ip, &chain, uio, ap->a_ioflag, seqcount);
697 hammer2_inode_unlock_ex(ip, chain);
702 * Perform read operations on a file or symlink given an UNLOCKED
705 * The passed ip is not locked.
709 hammer2_read_file(hammer2_inode_t *ip, struct uio *uio, int seqcount)
711 hammer2_chain_t *chain;
721 * We can't hold a shared lock on ip's chain across file bread's
722 * because the bread operation will itself obtain a shared lock,
723 * resulting in one thread holding 2 shared refs. This will deadlock
724 * against temporary lock upgrades. Temporary lock upgrades are
725 * needed to insert new chain structures into a parent's RB tree.
727 * We should be able to safely retain the shared lock on ip itself.
729 chain = hammer2_inode_lock_sh(ip);
730 size = chain->data->ipdata.size;
731 hammer2_chain_unlock(ip->hmp, chain);
734 while (uio->uio_resid > 0 && uio->uio_offset < size) {
741 lblksize = hammer2_calc_logical(ip, uio->uio_offset,
744 error = cluster_read(ip->vp, leof, lbase, lblksize,
745 uio->uio_resid, seqcount * BKVASIZE,
750 loff = (int)(uio->uio_offset - lbase);
752 if (n > uio->uio_resid)
754 if (n > size - uio->uio_offset)
755 n = (int)(size - uio->uio_offset);
756 bp->b_flags |= B_AGE;
757 uiomove((char *)bp->b_data + loff, n, uio);
760 hammer2_inode_unlock_sh(ip, chain);
765 * Called with a locked (ip) to do the underlying write to a file or
766 * to build the symlink target.
770 hammer2_write_file(hammer2_inode_t *ip, hammer2_chain_t **chainp,
772 int ioflag, int seqcount)
774 hammer2_inode_data_t *ipdata;
775 hammer2_key_t old_eof;
784 ipdata = &ip->chain->data->ipdata;
785 if (ioflag & IO_APPEND)
786 uio->uio_offset = ipdata->size;
791 * Extend the file if necessary. If the write fails at some point
792 * we will truncate it back down to cover as much as we were able
795 * Doing this now makes it easier to calculate buffer sizes in
798 old_eof = ipdata->size;
799 if (uio->uio_offset + uio->uio_resid > ipdata->size) {
801 hammer2_extend_file(ip, uio->uio_offset + uio->uio_resid);
802 kflags |= NOTE_EXTEND;
808 while (uio->uio_resid > 0) {
817 * Don't allow the buffer build to blow out the buffer
820 if ((ioflag & IO_RECURSE) == 0) {
822 * XXX should try to leave this unlocked through
825 hammer2_inode_unlock_ex(ip, *chainp);
826 bwillwrite(HAMMER2_PBUFSIZE);
827 *chainp = hammer2_inode_lock_ex(ip);
828 ipdata = &(*chainp)->data->ipdata; /* reload */
831 /* XXX bigwrite & signal check test */
834 * This nominally tells us how much we can cluster and
835 * what the logical buffer size needs to be. Currently
836 * we don't try to cluster the write and just handle one
839 lblksize = hammer2_calc_logical(ip, uio->uio_offset,
841 loff = (int)(uio->uio_offset - lbase);
844 * Calculate bytes to copy this transfer and whether the
845 * copy completely covers the buffer or not.
849 if (n > uio->uio_resid) {
851 if (uio->uio_offset + n == ipdata->size)
853 } else if (loff == 0) {
860 if (uio->uio_segflg == UIO_NOCOPY) {
862 * Issuing a write with the same data backing the
863 * buffer. Instantiate the buffer to collect the
864 * backing vm pages, then read-in any missing bits.
866 * This case is used by vop_stdputpages().
868 bp = getblk(ip->vp, lbase, lblksize, GETBLK_BHEAVY, 0);
869 if ((bp->b_flags & B_CACHE) == 0) {
871 error = bread(ip->vp, lbase, lblksize, &bp);
873 } else if (trivial) {
875 * Even though we are entirely overwriting the buffer
876 * we may still have to zero it out to avoid a
877 * mmap/write visibility issue.
879 bp = getblk(ip->vp, lbase, lblksize, GETBLK_BHEAVY, 0);
880 if ((bp->b_flags & B_CACHE) == 0)
884 * Partial overwrite, read in any missing bits then
885 * replace the portion being written.
887 * (The strategy code will detect zero-fill physical
888 * blocks for this case).
890 error = bread(ip->vp, lbase, lblksize, &bp);
901 * We have to assign physical storage to the buffer we intend
902 * to dirty or write now to avoid deadlocks in the strategy
905 * This can return NOOFFSET for inode-embedded data. The
906 * strategy code will take care of it in that case.
908 bp->b_bio2.bio_offset =
909 hammer2_assign_physical(ip, lbase, lblksize, &error);
916 * Ok, copy the data in
918 hammer2_inode_unlock_ex(ip, *chainp);
919 error = uiomove(bp->b_data + loff, n, uio);
920 *chainp = hammer2_inode_lock_ex(ip);
921 ipdata = &(*chainp)->data->ipdata; /* reload */
922 kflags |= NOTE_WRITE;
930 /* XXX update ip_data.mtime */
933 * Once we dirty a buffer any cached offset becomes invalid.
935 * NOTE: For cluster_write() always use the trailing block
936 * size, which is HAMMER2_PBUFSIZE. lblksize is the
937 * eof-straddling blocksize and is incorrect.
939 bp->b_flags |= B_AGE;
940 if (ioflag & IO_SYNC) {
942 } else if ((ioflag & IO_DIRECT) && loff + n == lblksize) {
943 if (bp->b_bcount == HAMMER2_PBUFSIZE)
944 bp->b_flags |= B_CLUSTEROK;
946 } else if (ioflag & IO_ASYNC) {
948 } else if (hammer2_cluster_enable) {
949 if (bp->b_bcount == HAMMER2_PBUFSIZE)
950 bp->b_flags |= B_CLUSTEROK;
951 cluster_write(bp, leof, HAMMER2_PBUFSIZE, seqcount);
953 if (bp->b_bcount == HAMMER2_PBUFSIZE)
954 bp->b_flags |= B_CLUSTEROK;
960 * Cleanup. If we extended the file EOF but failed to write through
961 * the entire write is a failure and we have to back-up.
963 if (error && ipdata->size != old_eof) {
964 hammer2_truncate_file(ip, old_eof);
965 } else if (modified) {
966 KKASSERT(ip->chain == *chainp);
967 hammer2_chain_modify(ip->hmp, *chainp, 0);
968 hammer2_update_time(&ipdata->mtime);
970 hammer2_knote(ip->vp, kflags);
975 * Assign physical storage to a logical block.
977 * NOOFFSET is returned if the data is inode-embedded. In this case the
978 * strategy code will simply bcopy() the data into the inode.
980 * The inode's delta_dcount is adjusted.
984 hammer2_assign_physical(hammer2_inode_t *ip, hammer2_key_t lbase,
985 int lblksize, int *errorp)
987 hammer2_mount_t *hmp;
988 hammer2_chain_t *parent;
989 hammer2_chain_t *chain;
993 * Locate the chain associated with lbase, return a locked chain.
994 * However, do not instantiate any data reference (which utilizes a
995 * device buffer) because we will be using direct IO via the
996 * logical buffer cache buffer.
1001 parent = hammer2_inode_lock_ex(ip);
1002 chain = hammer2_chain_lookup(hmp, &parent,
1004 HAMMER2_LOOKUP_NODATA);
1006 if (chain == NULL) {
1008 * We found a hole, create a new chain entry.
1010 * NOTE: DATA chains are created without device backing
1011 * store (nor do we want any).
1013 chain = hammer2_chain_create(hmp, parent, NULL,
1014 lbase, HAMMER2_PBUFRADIX,
1015 HAMMER2_BREF_TYPE_DATA,
1017 if (chain == NULL) {
1018 KKASSERT(*errorp == EAGAIN); /* XXX */
1019 hammer2_inode_unlock_ex(ip, parent);
1023 pbase = chain->bref.data_off & ~HAMMER2_OFF_MASK_RADIX;
1024 /*ip->delta_dcount += lblksize;*/
1026 switch (chain->bref.type) {
1027 case HAMMER2_BREF_TYPE_INODE:
1029 * The data is embedded in the inode. The
1030 * caller is responsible for marking the inode
1031 * modified and copying the data to the embedded
1036 case HAMMER2_BREF_TYPE_DATA:
1037 if (chain->bytes != lblksize) {
1038 panic("hammer2_assign_physical: "
1039 "size mismatch %d/%d\n",
1040 lblksize, chain->bytes);
1042 hammer2_chain_modify(hmp, chain,
1043 HAMMER2_MODIFY_OPTDATA);
1044 pbase = chain->bref.data_off & ~HAMMER2_OFF_MASK_RADIX;
1047 panic("hammer2_assign_physical: bad type");
1055 hammer2_chain_unlock(hmp, chain);
1056 hammer2_inode_unlock_ex(ip, parent);
1062 * Truncate the size of a file.
1064 * This routine adjusts ipdata->size smaller, destroying any related
1065 * data beyond the new EOF and potentially resizing the block straddling
1068 * The inode must be locked.
1072 hammer2_truncate_file(hammer2_inode_t *ip, hammer2_key_t nsize)
1074 hammer2_inode_data_t *ipdata;
1075 hammer2_chain_t *parent;
1076 hammer2_chain_t *chain;
1077 hammer2_mount_t *hmp = ip->hmp;
1078 hammer2_key_t lbase;
1086 hammer2_chain_modify(hmp, ip->chain, 0);
1088 ipdata = &ip->chain->data->ipdata;
1091 * Destroy any logical buffer cache buffers beyond the file EOF.
1093 * We call nvtruncbuf() w/ trivial == 1 to prevent it from messing
1094 * around with the buffer straddling EOF, because we need to assign
1095 * a new physical offset to it.
1098 nvtruncbuf(ip->vp, nsize,
1099 HAMMER2_PBUFSIZE, (int)nsize & HAMMER2_PBUFMASK,
1104 * Setup for lookup/search
1107 error = hammer2_chain_lock(hmp, parent, HAMMER2_RESOLVE_ALWAYS);
1109 hammer2_chain_unlock(hmp, parent);
1110 /* XXX error reporting */
1115 * Handle the case where a chain/logical-buffer straddles the new
1116 * EOF. We told nvtruncbuf() above not to mess with the logical
1117 * buffer straddling the EOF because we need to reassign its storage
1118 * and can't let the strategy code do it for us.
1120 loff = (int)nsize & HAMMER2_PBUFMASK;
1121 if (loff && ip->vp) {
1122 oblksize = hammer2_calc_logical(ip, nsize, &lbase, &leof);
1123 error = bread(ip->vp, lbase, oblksize, &bp);
1124 KKASSERT(error == 0);
1126 ipdata->size = nsize;
1127 nblksize = hammer2_calc_logical(ip, nsize, &lbase, &leof);
1130 * Fixup the chain element. If we have a logical buffer in-hand
1131 * we don't want to create a conflicting device buffer.
1134 chain = hammer2_chain_lookup(hmp, &parent, lbase, lbase,
1135 HAMMER2_LOOKUP_NODATA);
1137 allocbuf(bp, nblksize);
1138 switch(chain->bref.type) {
1139 case HAMMER2_BREF_TYPE_DATA:
1140 hammer2_chain_resize(ip, chain,
1141 hammer2_allocsize(nblksize),
1142 HAMMER2_MODIFY_OPTDATA);
1143 bzero(bp->b_data + loff, nblksize - loff);
1144 bp->b_bio2.bio_offset = chain->bref.data_off &
1147 case HAMMER2_BREF_TYPE_INODE:
1148 bzero(bp->b_data + loff, nblksize - loff);
1149 bp->b_bio2.bio_offset = NOOFFSET;
1152 panic("hammer2_truncate_file: bad type");
1155 hammer2_chain_unlock(hmp, chain);
1156 if (bp->b_bcount == HAMMER2_PBUFSIZE)
1157 bp->b_flags |= B_CLUSTEROK;
1161 * Destroy clean buffer w/ wrong buffer size. Retain
1164 bp->b_flags |= B_RELBUF;
1165 KKASSERT(bp->b_bio2.bio_offset == NOOFFSET);
1166 KKASSERT((bp->b_flags & B_DIRTY) == 0);
1171 * WARNING: This utilizes a device buffer for the data.
1173 * This case should not occur because file truncations without
1174 * a vnode (and hence no logical buffer cache) should only
1175 * always truncate to 0-length.
1177 panic("hammer2_truncate_file: non-zero truncation, no-vnode");
1179 chain = hammer2_chain_lookup(hmp, &parent, lbase, lbase, 0);
1181 switch(chain->bref.type) {
1182 case HAMMER2_BREF_TYPE_DATA:
1183 hammer2_chain_resize(ip, chain,
1184 hammer2_allocsize(nblksize),
1186 hammer2_chain_modify(hmp, chain, 0);
1187 bzero(chain->data->buf + loff, nblksize - loff);
1189 case HAMMER2_BREF_TYPE_INODE:
1190 if (loff < HAMMER2_EMBEDDED_BYTES) {
1191 hammer2_chain_modify(hmp, chain, 0);
1192 bzero(chain->data->ipdata.u.data + loff,
1193 HAMMER2_EMBEDDED_BYTES - loff);
1197 hammer2_chain_unlock(hmp, chain);
1203 * Clean up any fragmentory VM pages now that we have properly
1204 * resized the straddling buffer. These pages are no longer
1205 * part of the buffer.
1208 nvtruncbuf(ip->vp, nsize,
1209 nblksize, (int)nsize & (nblksize - 1),
1214 * Destroy any physical blocks after the new EOF point.
1216 lbase = (nsize + HAMMER2_PBUFMASK64) & ~HAMMER2_PBUFMASK64;
1217 chain = hammer2_chain_lookup(hmp, &parent,
1218 lbase, (hammer2_key_t)-1,
1219 HAMMER2_LOOKUP_NODATA);
1222 * Degenerate embedded data case, nothing to loop on.
1224 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE) {
1225 hammer2_chain_unlock(hmp, chain);
1230 * Delete physical data blocks past the file EOF.
1232 if (chain->bref.type == HAMMER2_BREF_TYPE_DATA) {
1233 /*ip->delta_dcount -= chain->bytes;*/
1234 hammer2_chain_delete(hmp, parent, chain, 0);
1236 /* XXX check parent if empty indirect block & delete */
1237 chain = hammer2_chain_next(hmp, &parent, chain,
1238 lbase, (hammer2_key_t)-1,
1239 HAMMER2_LOOKUP_NODATA);
1241 hammer2_chain_unlock(hmp, parent);
1245 * Extend the size of a file. The inode must be locked.
1247 * We may have to resize the block straddling the old EOF.
1251 hammer2_extend_file(hammer2_inode_t *ip, hammer2_key_t nsize)
1253 hammer2_inode_data_t *ipdata;
1254 hammer2_mount_t *hmp;
1255 hammer2_chain_t *parent;
1256 hammer2_chain_t *chain;
1258 hammer2_key_t osize;
1259 hammer2_key_t obase;
1260 hammer2_key_t nbase;
1270 hammer2_chain_modify(hmp, ip->chain, 0);
1271 ipdata = &ip->chain->data->ipdata;
1274 * Nothing to do if the direct-data case is still intact
1276 if ((ipdata->op_flags & HAMMER2_OPFLAG_DIRECTDATA) &&
1277 nsize <= HAMMER2_EMBEDDED_BYTES) {
1278 ipdata->size = nsize;
1280 ipdata->size, nsize,
1281 0, HAMMER2_EMBEDDED_BYTES,
1288 * Calculate the blocksize at the original EOF and resize the block
1289 * if necessary. Adjust the file size in the inode.
1291 osize = ipdata->size;
1292 oblksize = hammer2_calc_logical(ip, osize, &obase, &leof);
1293 ipdata->size = nsize;
1294 nblksize = hammer2_calc_logical(ip, osize, &nbase, &leof);
1297 * Do all required vnode operations, but do not mess with the
1298 * buffer straddling the orignal EOF.
1301 ipdata->size, nsize,
1303 0, (int)nsize & HAMMER2_PBUFMASK,
1307 * Early return if we have no more work to do.
1309 if (obase == nbase && oblksize == nblksize &&
1310 (ipdata->op_flags & HAMMER2_OPFLAG_DIRECTDATA) == 0) {
1315 * We have work to do, including possibly resizing the buffer
1316 * at the previous EOF point and turning off DIRECTDATA mode.
1319 if (((int)osize & HAMMER2_PBUFMASK)) {
1320 error = bread(ip->vp, obase, oblksize, &bp);
1321 KKASSERT(error == 0);
1323 if (obase != nbase) {
1324 if (oblksize != HAMMER2_PBUFSIZE)
1325 allocbuf(bp, HAMMER2_PBUFSIZE);
1327 if (oblksize != nblksize)
1328 allocbuf(bp, nblksize);
1333 * Disable direct-data mode by loading up a buffer cache buffer
1334 * with the data, then converting the inode data area into the
1335 * inode indirect block array area.
1337 if (ipdata->op_flags & HAMMER2_OPFLAG_DIRECTDATA) {
1338 ipdata->op_flags &= ~HAMMER2_OPFLAG_DIRECTDATA;
1339 bzero(&ipdata->u.blockset, sizeof(ipdata->u.blockset));
1343 * Resize the chain element at the old EOF.
1345 if (((int)osize & HAMMER2_PBUFMASK)) {
1348 error = hammer2_chain_lock(hmp, parent, HAMMER2_RESOLVE_ALWAYS);
1349 KKASSERT(error == 0);
1351 nradix = hammer2_allocsize(nblksize);
1353 chain = hammer2_chain_lookup(hmp, &parent,
1355 HAMMER2_LOOKUP_NODATA);
1356 if (chain == NULL) {
1357 chain = hammer2_chain_create(hmp, parent, NULL,
1359 HAMMER2_BREF_TYPE_DATA,
1361 if (chain == NULL) {
1362 KKASSERT(error == EAGAIN);
1363 hammer2_chain_unlock(hmp, parent);
1366 /*ip->delta_dcount += nblksize;*/
1368 KKASSERT(chain->bref.type == HAMMER2_BREF_TYPE_DATA);
1369 hammer2_chain_resize(ip, chain, nradix,
1370 HAMMER2_MODIFY_OPTDATA);
1372 bp->b_bio2.bio_offset = chain->bref.data_off &
1374 hammer2_chain_unlock(hmp, chain);
1375 if (bp->b_bcount == HAMMER2_PBUFSIZE)
1376 bp->b_flags |= B_CLUSTEROK;
1378 hammer2_chain_unlock(hmp, parent);
1384 hammer2_vop_nresolve(struct vop_nresolve_args *ap)
1386 hammer2_inode_t *ip;
1387 hammer2_inode_t *dip;
1388 hammer2_mount_t *hmp;
1389 hammer2_chain_t *parent;
1390 hammer2_chain_t *chain;
1391 hammer2_chain_t *ochain;
1392 struct namecache *ncp;
1393 const uint8_t *name;
1399 dip = VTOI(ap->a_dvp);
1401 ncp = ap->a_nch->ncp;
1402 name = ncp->nc_name;
1403 name_len = ncp->nc_nlen;
1404 lhc = hammer2_dirhash(name, name_len);
1407 * Note: In DragonFly the kernel handles '.' and '..'.
1409 parent = hammer2_inode_lock_sh(dip);
1410 chain = hammer2_chain_lookup(hmp, &parent,
1411 lhc, lhc + HAMMER2_DIRHASH_LOMASK,
1412 HAMMER2_LOOKUP_SHARED);
1414 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE &&
1415 name_len == chain->data->ipdata.name_len &&
1416 bcmp(name, chain->data->ipdata.filename, name_len) == 0) {
1419 chain = hammer2_chain_next(hmp, &parent, chain,
1420 lhc, lhc + HAMMER2_DIRHASH_LOMASK,
1421 HAMMER2_LOOKUP_SHARED);
1423 hammer2_inode_unlock_sh(dip, parent);
1426 * If the inode represents a forwarding entry for a hardlink we have
1427 * to locate the actual inode. The original ip is saved for possible
1428 * deconsolidation. (ip) will only be set to non-NULL when we have
1429 * to locate the real file via a hardlink. ip will be referenced but
1430 * not locked in that situation. chain is passed in locked and
1433 * XXX what kind of chain lock?
1436 if (chain && chain->data->ipdata.type == HAMMER2_OBJTYPE_HARDLINK) {
1437 error = hammer2_hardlink_find(dip, &chain, &ochain);
1439 kprintf("hammer2: unable to find hardlink\n");
1441 hammer2_chain_unlock(hmp, chain);
1449 * Deconsolidate any hardlink whos nlinks == 1. Ignore errors.
1450 * If an error occurs chain and ip are left alone.
1452 * XXX upgrade shared lock?
1454 if (ochain && chain && chain->data->ipdata.nlinks == 1 && !hmp->ronly) {
1455 kprintf("hammer2: need to unconsolidate hardlink for %s\n",
1456 chain->data->ipdata.filename);
1457 /* XXX retain shared lock on dip? (currently not held) */
1458 hammer2_hardlink_deconsolidate(dip, &chain, &ochain);
1462 * Acquire the related vnode
1464 * NOTE: For error processing, only ENOENT resolves the namecache
1465 * entry to NULL, otherwise we just return the error and
1466 * leave the namecache unresolved.
1468 * NOTE: multiple hammer2_inode structures can be aliased to the
1469 * same chain element, for example for hardlinks. This
1470 * use case does not 'reattach' inode associations that
1471 * might already exist, but always allocates a new one.
1474 ip = hammer2_inode_get(dip->hmp, dip->pmp, dip, chain);
1475 vp = hammer2_igetv(ip, &error);
1478 cache_setvp(ap->a_nch, vp);
1479 } else if (error == ENOENT) {
1480 cache_setvp(ap->a_nch, NULL);
1483 * don't break the API, chain is locked shared so unlock
1484 * it separately even though unlock_ex() currently doesn't
1487 hammer2_inode_unlock_ex(ip, NULL);
1488 hammer2_chain_unlock(hmp, chain);
1491 * The vp should not be released until after we've disposed
1492 * of our locks, because it might cause vop_inactive() to
1499 cache_setvp(ap->a_nch, NULL);
1502 KASSERT(error || ap->a_nch->ncp->nc_vp != NULL,
1503 ("resolve error %d/%p chain %p ap %p\n",
1504 error, ap->a_nch->ncp->nc_vp, chain, ap));
1506 hammer2_chain_drop(hmp, ochain);
1512 hammer2_vop_nlookupdotdot(struct vop_nlookupdotdot_args *ap)
1514 hammer2_chain_t *chain;
1515 hammer2_inode_t *dip;
1516 hammer2_inode_t *ip;
1517 hammer2_mount_t *hmp;
1520 dip = VTOI(ap->a_dvp);
1523 if ((ip = dip->pip) == NULL) {
1527 chain = hammer2_inode_lock_ex(ip);
1528 *ap->a_vpp = hammer2_igetv(ip, &error);
1529 hammer2_inode_unlock_ex(ip, chain);
1536 hammer2_vop_nmkdir(struct vop_nmkdir_args *ap)
1538 hammer2_chain_t *nchain;
1539 hammer2_mount_t *hmp;
1540 hammer2_inode_t *dip;
1541 hammer2_inode_t *nip;
1542 struct namecache *ncp;
1543 const uint8_t *name;
1547 dip = VTOI(ap->a_dvp);
1552 ncp = ap->a_nch->ncp;
1553 name = ncp->nc_name;
1554 name_len = ncp->nc_nlen;
1556 error = hammer2_inode_create(dip, ap->a_vap, ap->a_cred,
1557 name, name_len, &nip, &nchain);
1559 KKASSERT(nip == NULL);
1563 *ap->a_vpp = hammer2_igetv(nip, &error);
1564 hammer2_inode_unlock_ex(nip, nchain);
1567 cache_setunresolved(ap->a_nch);
1568 cache_setvp(ap->a_nch, *ap->a_vpp);
1574 * Return the largest contiguous physical disk range for the logical
1577 * (struct vnode *vp, off_t loffset, off_t *doffsetp, int *runp, int *runb)
1581 hammer2_vop_bmap(struct vop_bmap_args *ap)
1584 hammer2_mount_t *hmp;
1585 hammer2_inode_t *ip;
1586 hammer2_chain_t *parent;
1587 hammer2_chain_t *chain;
1591 hammer2_off_t pbytes;
1592 hammer2_off_t array[HAMMER2_BMAP_COUNT][2];
1597 * Only supported on regular files
1599 * Only supported for read operations (required for cluster_read).
1600 * The block allocation is delayed for write operations.
1603 if (vp->v_type != VREG)
1604 return (EOPNOTSUPP);
1605 if (ap->a_cmd != BUF_CMD_READ)
1606 return (EOPNOTSUPP);
1610 bzero(array, sizeof(array));
1613 * Calculate logical range
1615 KKASSERT((ap->a_loffset & HAMMER2_LBUFMASK64) == 0);
1616 lbeg = ap->a_loffset & HAMMER2_OFF_MASK_HI;
1617 lend = lbeg + HAMMER2_BMAP_COUNT * HAMMER2_PBUFSIZE - 1;
1620 loff = ap->a_loffset & HAMMER2_OFF_MASK_LO;
1622 parent = hammer2_inode_lock_sh(ip);
1623 chain = hammer2_chain_lookup(hmp, &parent,
1625 HAMMER2_LOOKUP_NODATA |
1626 HAMMER2_LOOKUP_SHARED);
1627 if (chain == NULL) {
1628 *ap->a_doffsetp = ZFOFFSET;
1629 hammer2_inode_unlock_sh(ip, parent);
1634 if (chain->bref.type == HAMMER2_BREF_TYPE_DATA) {
1635 ai = (chain->bref.key - lbeg) / HAMMER2_PBUFSIZE;
1636 KKASSERT(ai >= 0 && ai < HAMMER2_BMAP_COUNT);
1637 array[ai][0] = chain->bref.data_off & HAMMER2_OFF_MASK;
1638 array[ai][1] = chain->bytes;
1640 chain = hammer2_chain_next(hmp, &parent, chain,
1642 HAMMER2_LOOKUP_NODATA |
1643 HAMMER2_LOOKUP_SHARED);
1645 hammer2_inode_unlock_sh(ip, parent);
1648 * If the requested loffset is not mappable physically we can't
1649 * bmap. The caller will have to access the file data via a
1652 if (array[0][0] == 0 || array[0][1] < loff + HAMMER2_LBUFSIZE) {
1653 *ap->a_doffsetp = NOOFFSET;
1658 * Calculate the physical disk offset range for array[0]
1660 pbeg = array[0][0] + loff;
1661 pbytes = array[0][1] - loff;
1663 for (ai = 1; ai < HAMMER2_BMAP_COUNT; ++ai) {
1664 if (array[ai][0] != pbeg + pbytes)
1666 pbytes += array[ai][1];
1669 *ap->a_doffsetp = pbeg;
1671 *ap->a_runp = pbytes;
1677 hammer2_vop_open(struct vop_open_args *ap)
1679 return vop_stdopen(ap);
1683 * hammer2_vop_advlock { vp, id, op, fl, flags }
1687 hammer2_vop_advlock(struct vop_advlock_args *ap)
1689 hammer2_inode_t *ip = VTOI(ap->a_vp);
1690 hammer2_chain_t *chain;
1693 chain = hammer2_inode_lock_sh(ip);
1694 size = chain->data->ipdata.size;
1695 hammer2_inode_unlock_sh(ip, chain);
1696 return (lf_advlock(ap, &ip->advlock, size));
1702 hammer2_vop_close(struct vop_close_args *ap)
1704 return vop_stdclose(ap);
1708 * hammer2_vop_nlink { nch, dvp, vp, cred }
1710 * Create a hardlink from (vp) to {dvp, nch}.
1714 hammer2_vop_nlink(struct vop_nlink_args *ap)
1716 hammer2_inode_t *dip; /* target directory to create link in */
1717 hammer2_inode_t *ip; /* inode we are hardlinking to */
1718 hammer2_inode_t *oip;
1719 hammer2_mount_t *hmp;
1720 hammer2_chain_t *chain;
1721 hammer2_chain_t *ochain;
1722 struct namecache *ncp;
1723 const uint8_t *name;
1727 dip = VTOI(ap->a_dvp);
1733 * (ip) is the inode we are linking to.
1735 ip = oip = VTOI(ap->a_vp);
1736 hammer2_inode_ref(ip);
1738 ncp = ap->a_nch->ncp;
1739 name = ncp->nc_name;
1740 name_len = ncp->nc_nlen;
1743 * Create a consolidated real file for the hardlink, adjust (ip),
1744 * and move the nlinks lock if necessary. Tell the function to
1745 * bump the hardlink count on the consolidated file.
1747 error = hammer2_hardlink_consolidate(&ip, dip);
1752 * If the consolidation changed ip to a HARDLINK pointer we have
1753 * to adjust the vnode to point to the actual ip.
1755 * XXX this can race against concurrent vnode ops.
1758 hammer2_inode_ref(ip); /* vp ref+ */
1759 chain = hammer2_inode_lock_ex(ip);
1760 ochain = hammer2_inode_lock_ex(oip);
1762 KKASSERT(ip->vp == ap->a_vp);
1763 hammer2_inode_drop(ip); /* vp already ref'd */
1766 ap->a_vp->v_data = ip;
1769 KKASSERT(oip->vp == ap->a_vp);
1771 hammer2_inode_drop(oip); /* vp ref- */
1773 hammer2_inode_unlock_ex(oip, ochain);
1774 hammer2_inode_unlock_ex(ip, chain);
1778 * The act of connecting the existing (ip) will properly bump the
1779 * nlinks count. However, vp will incorrectly point at the old
1780 * inode which has now been turned into a OBJTYPE_HARDLINK pointer.
1782 * We must reconnect the vp.
1784 error = hammer2_inode_connect(dip, ip, name, name_len);
1786 cache_setunresolved(ap->a_nch);
1787 cache_setvp(ap->a_nch, ap->a_vp);
1790 hammer2_inode_drop(ip);
1795 * hammer2_vop_ncreate { nch, dvp, vpp, cred, vap }
1797 * The operating system has already ensured that the directory entry
1798 * does not exist and done all appropriate namespace locking.
1802 hammer2_vop_ncreate(struct vop_ncreate_args *ap)
1804 hammer2_mount_t *hmp;
1805 hammer2_inode_t *dip;
1806 hammer2_inode_t *nip;
1807 hammer2_chain_t *nchain;
1808 struct namecache *ncp;
1809 const uint8_t *name;
1813 dip = VTOI(ap->a_dvp);
1818 ncp = ap->a_nch->ncp;
1819 name = ncp->nc_name;
1820 name_len = ncp->nc_nlen;
1822 error = hammer2_inode_create(dip, ap->a_vap, ap->a_cred,
1823 name, name_len, &nip, &nchain);
1825 KKASSERT(nip == NULL);
1829 *ap->a_vpp = hammer2_igetv(nip, &error);
1830 hammer2_inode_unlock_ex(nip, nchain);
1833 cache_setunresolved(ap->a_nch);
1834 cache_setvp(ap->a_nch, *ap->a_vpp);
1840 * hammer2_vop_nsymlink { nch, dvp, vpp, cred, vap, target }
1844 hammer2_vop_nsymlink(struct vop_nsymlink_args *ap)
1846 hammer2_mount_t *hmp;
1847 hammer2_inode_t *dip;
1848 hammer2_inode_t *nip;
1849 hammer2_chain_t *nchain;
1850 struct namecache *ncp;
1851 const uint8_t *name;
1855 dip = VTOI(ap->a_dvp);
1860 ncp = ap->a_nch->ncp;
1861 name = ncp->nc_name;
1862 name_len = ncp->nc_nlen;
1864 ap->a_vap->va_type = VLNK; /* enforce type */
1866 error = hammer2_inode_create(dip, ap->a_vap, ap->a_cred,
1867 name, name_len, &nip, &nchain);
1869 KKASSERT(nip == NULL);
1873 *ap->a_vpp = hammer2_igetv(nip, &error);
1876 * Build the softlink (~like file data) and finalize the namecache.
1882 hammer2_inode_data_t *nipdata;
1884 nipdata = &nchain->data->ipdata;
1885 bytes = strlen(ap->a_target);
1887 if (bytes <= HAMMER2_EMBEDDED_BYTES) {
1888 KKASSERT(nipdata->op_flags &
1889 HAMMER2_OPFLAG_DIRECTDATA);
1890 bcopy(ap->a_target, nipdata->u.data, bytes);
1891 nipdata->size = bytes;
1893 bzero(&auio, sizeof(auio));
1894 bzero(&aiov, sizeof(aiov));
1895 auio.uio_iov = &aiov;
1896 auio.uio_segflg = UIO_SYSSPACE;
1897 auio.uio_rw = UIO_WRITE;
1898 auio.uio_resid = bytes;
1899 auio.uio_iovcnt = 1;
1900 auio.uio_td = curthread;
1901 aiov.iov_base = ap->a_target;
1902 aiov.iov_len = bytes;
1903 error = hammer2_write_file(nip, &nchain,
1904 &auio, IO_APPEND, 0);
1905 /* XXX handle error */
1909 hammer2_inode_unlock_ex(nip, nchain);
1912 * Finalize namecache
1915 cache_setunresolved(ap->a_nch);
1916 cache_setvp(ap->a_nch, *ap->a_vpp);
1917 /* hammer2_knote(ap->a_dvp, NOTE_WRITE); */
1923 * hammer2_vop_nremove { nch, dvp, cred }
1927 hammer2_vop_nremove(struct vop_nremove_args *ap)
1929 hammer2_inode_t *dip;
1930 hammer2_mount_t *hmp;
1931 struct namecache *ncp;
1932 const uint8_t *name;
1936 dip = VTOI(ap->a_dvp);
1941 ncp = ap->a_nch->ncp;
1942 name = ncp->nc_name;
1943 name_len = ncp->nc_nlen;
1945 error = hammer2_unlink_file(dip, name, name_len, 0, NULL);
1947 cache_unlink(ap->a_nch);
1953 * hammer2_vop_nrmdir { nch, dvp, cred }
1957 hammer2_vop_nrmdir(struct vop_nrmdir_args *ap)
1959 hammer2_inode_t *dip;
1960 hammer2_mount_t *hmp;
1961 struct namecache *ncp;
1962 const uint8_t *name;
1966 dip = VTOI(ap->a_dvp);
1971 ncp = ap->a_nch->ncp;
1972 name = ncp->nc_name;
1973 name_len = ncp->nc_nlen;
1975 error = hammer2_unlink_file(dip, name, name_len, 1, NULL);
1977 cache_unlink(ap->a_nch);
1983 * hammer2_vop_nrename { fnch, tnch, fdvp, tdvp, cred }
1987 hammer2_vop_nrename(struct vop_nrename_args *ap)
1989 struct namecache *fncp;
1990 struct namecache *tncp;
1991 hammer2_inode_t *fdip;
1992 hammer2_inode_t *tdip;
1993 hammer2_inode_t *ip;
1994 hammer2_chain_t *chain;
1995 hammer2_mount_t *hmp;
1996 const uint8_t *fname;
1998 const uint8_t *tname;
2002 if (ap->a_fdvp->v_mount != ap->a_tdvp->v_mount)
2004 if (ap->a_fdvp->v_mount != ap->a_fnch->ncp->nc_vp->v_mount)
2007 fdip = VTOI(ap->a_fdvp); /* source directory */
2008 tdip = VTOI(ap->a_tdvp); /* target directory */
2010 hmp = fdip->hmp; /* check read-only filesystem */
2014 fncp = ap->a_fnch->ncp; /* entry name in source */
2015 fname = fncp->nc_name;
2016 fname_len = fncp->nc_nlen;
2018 tncp = ap->a_tnch->ncp; /* entry name in target */
2019 tname = tncp->nc_name;
2020 tname_len = tncp->nc_nlen;
2023 * ip is the inode being removed. If this is a hardlink then
2024 * ip represents the actual file and not the hardlink marker.
2026 ip = VTOI(fncp->nc_vp);
2029 * Keep a tight grip on the inode as removing it should disconnect
2030 * it and we don't want to destroy it.
2032 * NOTE: To avoid deadlocks we cannot lock (ip) while we are
2033 * unlinking elements from their directories. Locking
2034 * the nlinks field does not lock the whole inode.
2036 hammer2_inode_ref(ip);
2039 * Remove target if it exists
2041 error = hammer2_unlink_file(tdip, tname, tname_len, -1, NULL);
2042 if (error && error != ENOENT)
2044 cache_setunresolved(ap->a_tnch);
2047 * Disconnect (fdip, fname) from the source directory. This will
2048 * disconnect (ip) if it represents a direct file. If (ip) represents
2049 * a hardlink the HARDLINK pointer object will be removed but the
2050 * hardlink will stay intact.
2052 * If (ip) is already hardlinked we have to resolve to a consolidated
2053 * file but we do not bump the nlinks count. (ip) must hold the nlinks
2054 * lock & ref for the operation. If the consolidated file has been
2055 * relocated (ip) will be adjusted and the related nlinks lock moved
2058 * If (ip) does not have multiple links we can just copy the physical
2059 * contents of the inode.
2061 chain = hammer2_inode_lock_sh(ip);
2062 hammer2_chain_ref(hmp, chain); /* for unlink file */
2063 if (chain->data->ipdata.nlinks > 1) {
2064 hammer2_inode_unlock_sh(ip, chain);
2065 error = hammer2_hardlink_consolidate(&ip, tdip);
2069 hammer2_inode_unlock_sh(ip, chain);
2071 /* chain ref still intact */
2074 * NOTE! Because we are retaining (ip) the unlink can fail with
2078 error = hammer2_unlink_file(fdip, fname, fname_len, -1, chain);
2079 if (error != EAGAIN)
2081 kprintf("hammer2_vop_nrename: unlink race %s\n", fname);
2082 tsleep(fdip, 0, "h2renr", 1);
2084 hammer2_chain_drop(hmp, chain); /* drop temporary ref */
2089 * Reconnect ip to target directory.
2091 * WARNING: chain locks can lock buffer cache buffers, to avoid
2092 * deadlocks we want to unlock before issuing a cache_*()
2093 * op (that might have to lock a vnode).
2095 error = hammer2_inode_connect(tdip, ip, tname, tname_len);
2097 cache_rename(ap->a_fnch, ap->a_tnch);
2100 hammer2_inode_drop(ip);
2105 static int hammer2_strategy_read(struct vop_strategy_args *ap);
2106 static int hammer2_strategy_write(struct vop_strategy_args *ap);
2110 hammer2_vop_strategy(struct vop_strategy_args *ap)
2121 error = hammer2_strategy_read(ap);
2122 ++hammer2_iod_file_read;
2125 error = hammer2_strategy_write(ap);
2126 ++hammer2_iod_file_write;
2129 bp->b_error = error = EINVAL;
2130 bp->b_flags |= B_ERROR;
2140 hammer2_strategy_read(struct vop_strategy_args *ap)
2145 hammer2_mount_t *hmp;
2146 hammer2_inode_t *ip;
2147 hammer2_chain_t *parent;
2148 hammer2_chain_t *chain;
2149 hammer2_key_t lbase;
2153 ip = VTOI(ap->a_vp);
2155 nbio = push_bio(bio);
2157 lbase = bio->bio_offset;
2159 KKASSERT(((int)lbase & HAMMER2_PBUFMASK) == 0);
2162 * We must characterize the logical->physical translation if it
2163 * has not already been cached.
2165 * Physical data references < LBUFSIZE are never cached. This
2166 * includes both small-block allocations and inode-embedded data.
2168 if (nbio->bio_offset == NOOFFSET) {
2169 parent = hammer2_inode_lock_sh(ip);
2171 chain = hammer2_chain_lookup(hmp, &parent, lbase, lbase,
2172 HAMMER2_LOOKUP_NODATA |
2173 HAMMER2_LOOKUP_SHARED);
2174 if (chain == NULL) {
2178 nbio->bio_offset = ZFOFFSET;
2179 } else if (chain->bref.type == HAMMER2_BREF_TYPE_INODE) {
2181 * Data is embedded in the inode (do nothing)
2183 KKASSERT(chain == parent);
2184 hammer2_chain_unlock(hmp, chain);
2185 } else if (chain->bref.type == HAMMER2_BREF_TYPE_DATA) {
2189 KKASSERT(bp->b_bcount == chain->bytes);
2190 nbio->bio_offset = chain->bref.data_off &
2192 hammer2_chain_unlock(hmp, chain);
2193 KKASSERT(nbio->bio_offset != 0);
2195 panic("hammer2_strategy_read: unknown bref type");
2197 hammer2_inode_unlock_sh(ip, parent);
2200 if (hammer2_debug & 0x0020) {
2201 kprintf("read %016jx %016jx\n",
2202 bio->bio_offset, nbio->bio_offset);
2205 if (nbio->bio_offset == ZFOFFSET) {
2211 bzero(bp->b_data, bp->b_bcount);
2213 } else if (nbio->bio_offset != NOOFFSET) {
2215 * Forward direct IO to the device
2217 vn_strategy(hmp->devvp, nbio);
2220 * Data is embedded in inode.
2222 bcopy(chain->data->ipdata.u.data, bp->b_data,
2223 HAMMER2_EMBEDDED_BYTES);
2224 bzero(bp->b_data + HAMMER2_EMBEDDED_BYTES,
2225 bp->b_bcount - HAMMER2_EMBEDDED_BYTES);
2235 hammer2_strategy_write(struct vop_strategy_args *ap)
2240 hammer2_mount_t *hmp;
2241 hammer2_inode_t *ip;
2245 ip = VTOI(ap->a_vp);
2247 nbio = push_bio(bio);
2249 KKASSERT((bio->bio_offset & HAMMER2_PBUFMASK64) == 0);
2250 KKASSERT(nbio->bio_offset != 0 && nbio->bio_offset != ZFOFFSET);
2252 if (nbio->bio_offset == NOOFFSET) {
2254 * Must be embedded in the inode.
2256 * Because the inode is dirty, the chain must exist whether
2257 * the inode is locked or not. XXX
2259 KKASSERT(bio->bio_offset == 0);
2260 KKASSERT(ip->chain && ip->chain->data);
2261 bcopy(bp->b_data, ip->chain->data->ipdata.u.data,
2262 HAMMER2_EMBEDDED_BYTES);
2268 * This special flag does not follow the normal MODIFY rules
2269 * because we might deadlock on ip. Instead we depend on
2270 * VOP_FSYNC() to detect the case.
2272 atomic_set_int(&ip->flags, HAMMER2_INODE_DIRTYEMBED);
2275 * Forward direct IO to the device
2277 vn_strategy(hmp->devvp, nbio);
2283 * hammer2_vop_ioctl { vp, command, data, fflag, cred }
2287 hammer2_vop_ioctl(struct vop_ioctl_args *ap)
2289 hammer2_mount_t *hmp;
2290 hammer2_inode_t *ip;
2293 ip = VTOI(ap->a_vp);
2296 error = hammer2_ioctl(ip, ap->a_command, (void *)ap->a_data,
2297 ap->a_fflag, ap->a_cred);
2303 hammer2_vop_mountctl(struct vop_mountctl_args *ap)
2306 hammer2_pfsmount_t *pmp;
2310 case (MOUNTCTL_SET_EXPORT):
2311 mp = ap->a_head.a_ops->head.vv_mount;
2314 if (ap->a_ctllen != sizeof(struct export_args))
2317 rc = vfs_export(mp, &pmp->export,
2318 (const struct export_args *)ap->a_ctl);
2321 rc = vop_stdmountctl(ap);
2327 struct vop_ops hammer2_vnode_vops = {
2328 .vop_default = vop_defaultop,
2329 .vop_fsync = hammer2_vop_fsync,
2330 .vop_getpages = vop_stdgetpages,
2331 .vop_putpages = vop_stdputpages,
2332 .vop_access = hammer2_vop_access,
2333 .vop_advlock = hammer2_vop_advlock,
2334 .vop_close = hammer2_vop_close,
2335 .vop_nlink = hammer2_vop_nlink,
2336 .vop_ncreate = hammer2_vop_ncreate,
2337 .vop_nsymlink = hammer2_vop_nsymlink,
2338 .vop_nremove = hammer2_vop_nremove,
2339 .vop_nrmdir = hammer2_vop_nrmdir,
2340 .vop_nrename = hammer2_vop_nrename,
2341 .vop_getattr = hammer2_vop_getattr,
2342 .vop_setattr = hammer2_vop_setattr,
2343 .vop_readdir = hammer2_vop_readdir,
2344 .vop_readlink = hammer2_vop_readlink,
2345 .vop_getpages = vop_stdgetpages,
2346 .vop_putpages = vop_stdputpages,
2347 .vop_read = hammer2_vop_read,
2348 .vop_write = hammer2_vop_write,
2349 .vop_open = hammer2_vop_open,
2350 .vop_inactive = hammer2_vop_inactive,
2351 .vop_reclaim = hammer2_vop_reclaim,
2352 .vop_nresolve = hammer2_vop_nresolve,
2353 .vop_nlookupdotdot = hammer2_vop_nlookupdotdot,
2354 .vop_nmkdir = hammer2_vop_nmkdir,
2355 .vop_ioctl = hammer2_vop_ioctl,
2356 .vop_mountctl = hammer2_vop_mountctl,
2357 .vop_bmap = hammer2_vop_bmap,
2358 .vop_strategy = hammer2_vop_strategy,
2361 struct vop_ops hammer2_spec_vops = {
2365 struct vop_ops hammer2_fifo_vops = {