sys/vfs/hammer2: vnops need to return unix errno
[dragonfly.git] / sys / vfs / hammer2 / hammer2_vnops.c
blob1180711aec3885ba64608c3b16a872f9f2b6867f
1 /*
2 * Copyright (c) 2011-2018 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>
7 * by Daniel Flores (GSOC 2013 - mentored by Matthew Dillon, compression)
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 * 3. Neither the name of The DragonFly Project nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific, prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
31 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
37 * Kernel Filesystem interface
39 * NOTE! local ipdata pointers must be reloaded on any modifying operation
40 * to the inode as its underlying chain may have changed.
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/fcntl.h>
47 #include <sys/buf.h>
48 #include <sys/proc.h>
49 #include <sys/mount.h>
50 #include <sys/vnode.h>
51 #include <sys/mountctl.h>
52 #include <sys/dirent.h>
53 #include <sys/uio.h>
54 #include <sys/objcache.h>
55 #include <sys/event.h>
56 #include <sys/file.h>
57 #include <vfs/fifofs/fifo.h>
59 #include "hammer2.h"
61 static int hammer2_read_file(hammer2_inode_t *ip, struct uio *uio,
62 int seqcount);
63 static int hammer2_write_file(hammer2_inode_t *ip, struct uio *uio,
64 int ioflag, int seqcount);
65 static void hammer2_extend_file(hammer2_inode_t *ip, hammer2_key_t nsize);
66 static void hammer2_truncate_file(hammer2_inode_t *ip, hammer2_key_t nsize);
69 * Last reference to a vnode is going away but it is still cached.
71 static
72 int
73 hammer2_vop_inactive(struct vop_inactive_args *ap)
75 hammer2_inode_t *ip;
76 struct vnode *vp;
78 vp = ap->a_vp;
79 ip = VTOI(vp);
82 * Degenerate case
84 if (ip == NULL) {
85 vrecycle(vp);
86 return (0);
90 * Aquire the inode lock to interlock against vp updates via
91 * the inode path and file deletions and such (which can be
92 * namespace-only operations that might not hold the vnode).
94 hammer2_inode_lock(ip, 0);
95 if (ip->flags & HAMMER2_INODE_ISUNLINKED) {
96 hammer2_key_t lbase;
97 int nblksize;
100 * If the inode has been unlinked we can throw away all
101 * buffers (dirty or not) and clean the file out.
103 * Because vrecycle() calls are not guaranteed, try to
104 * dispose of the inode as much as possible right here.
106 nblksize = hammer2_calc_logical(ip, 0, &lbase, NULL);
107 nvtruncbuf(vp, 0, nblksize, 0, 0);
110 * Delete the file on-media.
112 if ((ip->flags & HAMMER2_INODE_DELETING) == 0) {
113 atomic_set_int(&ip->flags, HAMMER2_INODE_DELETING);
114 hammer2_inode_delayed_sideq(ip);
116 hammer2_inode_unlock(ip);
119 * Recycle immediately if possible
121 vrecycle(vp);
122 } else {
123 hammer2_inode_unlock(ip);
125 return (0);
129 * Reclaim a vnode so that it can be reused; after the inode is
130 * disassociated, the filesystem must manage it alone.
132 static
134 hammer2_vop_reclaim(struct vop_reclaim_args *ap)
136 hammer2_inode_t *ip;
137 struct vnode *vp;
139 vp = ap->a_vp;
140 ip = VTOI(vp);
141 if (ip == NULL)
142 return(0);
145 * NOTE! We do not attempt to flush chains here, flushing is
146 * really fragile and could also deadlock.
148 vclrisdirty(vp);
151 * The inode lock is required to disconnect it.
153 hammer2_inode_lock(ip, 0);
154 vp->v_data = NULL;
155 ip->vp = NULL;
158 * Delete the file on-media. This should have been handled by the
159 * inactivation. The operation is likely still queued on the inode
160 * though so only complain if the stars don't align.
162 if ((ip->flags & (HAMMER2_INODE_ISUNLINKED | HAMMER2_INODE_DELETING)) ==
163 HAMMER2_INODE_ISUNLINKED)
165 atomic_set_int(&ip->flags, HAMMER2_INODE_DELETING);
166 hammer2_inode_delayed_sideq(ip);
167 kprintf("hammer2: vp=%p ip=%p unlinked but not disposed\n",
168 vp, ip);
170 hammer2_inode_unlock(ip);
173 * Modified inodes will already be on SIDEQ or SYNCQ, no further
174 * action is needed.
176 * We cannot safely synchronize the inode from inside the reclaim
177 * due to potentially deep locks held as-of when the reclaim occurs.
178 * Interactions and potential deadlocks abound. We also can't do it
179 * here without desynchronizing from the related directory entrie(s).
181 hammer2_inode_drop(ip); /* vp ref */
184 * XXX handle background sync when ip dirty, kernel will no longer
185 * notify us regarding this inode because there is no longer a
186 * vnode attached to it.
189 return (0);
193 * Currently this function synchronizes the front-end inode state to the
194 * backend chain topology, then flushes the inode's chain and sub-topology
195 * to backend media. This function does not flush the root topology down to
196 * the inode.
198 static
200 hammer2_vop_fsync(struct vop_fsync_args *ap)
202 hammer2_inode_t *ip;
203 struct vnode *vp;
204 int error1;
205 int error2;
207 vp = ap->a_vp;
208 ip = VTOI(vp);
209 error1 = 0;
211 hammer2_trans_init(ip->pmp, 0);
214 * Flush dirty buffers in the file's logical buffer cache.
215 * It is best to wait for the strategy code to commit the
216 * buffers to the device's backing buffer cache before
217 * then trying to flush the inode.
219 * This should be quick, but certain inode modifications cached
220 * entirely in the hammer2_inode structure may not trigger a
221 * buffer read until the flush so the fsync can wind up also
222 * doing scattered reads.
224 vfsync(vp, ap->a_waitfor, 1, NULL, NULL);
225 bio_track_wait(&vp->v_track_write, 0, 0);
228 * Flush any inode changes
230 hammer2_inode_lock(ip, 0);
231 if (ip->flags & (HAMMER2_INODE_RESIZED|HAMMER2_INODE_MODIFIED))
232 error1 = hammer2_inode_chain_sync(ip);
235 * Flush dirty chains related to the inode.
237 * NOTE! We are not in a flush transaction. The inode remains on
238 * the sideq so the filesystem syncer can synchronize it to
239 * the volume root.
241 error2 = hammer2_inode_chain_flush(ip, HAMMER2_XOP_INODE_STOP);
242 if (error2)
243 error1 = error2;
246 * We may be able to clear the vnode dirty flag.
248 if ((ip->flags & (HAMMER2_INODE_MODIFIED |
249 HAMMER2_INODE_RESIZED |
250 HAMMER2_INODE_DIRTYDATA)) == 0 &&
251 RB_EMPTY(&vp->v_rbdirty_tree) &&
252 !bio_track_active(&vp->v_track_write)) {
253 vclrisdirty(vp);
255 hammer2_inode_unlock(ip);
256 hammer2_trans_done(ip->pmp, 0);
258 return (error1);
262 * No lock needed, just handle ip->update
264 static
266 hammer2_vop_access(struct vop_access_args *ap)
268 hammer2_inode_t *ip = VTOI(ap->a_vp);
269 uid_t uid;
270 gid_t gid;
271 mode_t mode;
272 uint32_t uflags;
273 int error;
274 int update;
276 retry:
277 update = spin_access_start(&ip->cluster_spin);
279 /*hammer2_inode_lock(ip, HAMMER2_RESOLVE_SHARED);*/
280 uid = hammer2_to_unix_xid(&ip->meta.uid);
281 gid = hammer2_to_unix_xid(&ip->meta.gid);
282 mode = ip->meta.mode;
283 uflags = ip->meta.uflags;
284 /*hammer2_inode_unlock(ip);*/
286 if (__predict_false(spin_access_end(&ip->cluster_spin, update)))
287 goto retry;
289 error = vop_helper_access(ap, uid, gid, mode, uflags);
291 return (error);
294 static
296 hammer2_vop_getattr(struct vop_getattr_args *ap)
298 hammer2_pfs_t *pmp;
299 hammer2_inode_t *ip;
300 struct vnode *vp;
301 struct vattr *vap;
302 int update;
304 vp = ap->a_vp;
305 vap = ap->a_vap;
307 ip = VTOI(vp);
308 pmp = ip->pmp;
310 retry:
311 update = spin_access_start(&ip->cluster_spin);
313 vap->va_fsid = pmp->mp->mnt_stat.f_fsid.val[0];
314 vap->va_fileid = ip->meta.inum;
315 vap->va_mode = ip->meta.mode;
316 vap->va_nlink = ip->meta.nlinks;
317 vap->va_uid = hammer2_to_unix_xid(&ip->meta.uid);
318 vap->va_gid = hammer2_to_unix_xid(&ip->meta.gid);
319 vap->va_rmajor = 0;
320 vap->va_rminor = 0;
321 vap->va_size = ip->meta.size; /* protected by shared lock */
322 vap->va_blocksize = HAMMER2_PBUFSIZE;
323 vap->va_flags = ip->meta.uflags;
324 hammer2_time_to_timespec(ip->meta.ctime, &vap->va_ctime);
325 hammer2_time_to_timespec(ip->meta.mtime, &vap->va_mtime);
326 hammer2_time_to_timespec(ip->meta.mtime, &vap->va_atime);
327 vap->va_gen = 1;
328 vap->va_bytes = 0;
329 if (ip->meta.type == HAMMER2_OBJTYPE_DIRECTORY) {
331 * Can't really calculate directory use sans the files under
332 * it, just assume one block for now.
334 vap->va_bytes += HAMMER2_INODE_BYTES;
335 } else {
336 vap->va_bytes = hammer2_inode_data_count(ip);
338 vap->va_type = hammer2_get_vtype(ip->meta.type);
339 vap->va_filerev = 0;
340 vap->va_uid_uuid = ip->meta.uid;
341 vap->va_gid_uuid = ip->meta.gid;
342 vap->va_vaflags = VA_UID_UUID_VALID | VA_GID_UUID_VALID |
343 VA_FSID_UUID_VALID;
345 if (__predict_false(spin_access_end(&ip->cluster_spin, update)))
346 goto retry;
348 return (0);
351 static
353 hammer2_vop_getattr_lite(struct vop_getattr_lite_args *ap)
355 hammer2_pfs_t *pmp;
356 hammer2_inode_t *ip;
357 struct vnode *vp;
358 struct vattr_lite *lvap;
359 int update;
361 vp = ap->a_vp;
362 lvap = ap->a_lvap;
364 ip = VTOI(vp);
365 pmp = ip->pmp;
367 retry:
368 update = spin_access_start(&ip->cluster_spin);
370 #if 0
371 vap->va_fsid = pmp->mp->mnt_stat.f_fsid.val[0];
372 vap->va_fileid = ip->meta.inum;
373 #endif
374 lvap->va_mode = ip->meta.mode;
375 lvap->va_nlink = ip->meta.nlinks;
376 lvap->va_uid = hammer2_to_unix_xid(&ip->meta.uid);
377 lvap->va_gid = hammer2_to_unix_xid(&ip->meta.gid);
378 #if 0
379 vap->va_rmajor = 0;
380 vap->va_rminor = 0;
381 #endif
382 lvap->va_size = ip->meta.size;
383 #if 0
384 vap->va_blocksize = HAMMER2_PBUFSIZE;
385 #endif
386 lvap->va_flags = ip->meta.uflags;
387 lvap->va_type = hammer2_get_vtype(ip->meta.type);
388 #if 0
389 vap->va_filerev = 0;
390 vap->va_uid_uuid = ip->meta.uid;
391 vap->va_gid_uuid = ip->meta.gid;
392 vap->va_vaflags = VA_UID_UUID_VALID | VA_GID_UUID_VALID |
393 VA_FSID_UUID_VALID;
394 #endif
396 if (__predict_false(spin_access_end(&ip->cluster_spin, update)))
397 goto retry;
399 return (0);
402 static
404 hammer2_vop_setattr(struct vop_setattr_args *ap)
406 hammer2_inode_t *ip;
407 struct vnode *vp;
408 struct vattr *vap;
409 int error;
410 int kflags = 0;
411 uint64_t ctime;
413 vp = ap->a_vp;
414 vap = ap->a_vap;
415 hammer2_update_time(&ctime);
417 ip = VTOI(vp);
419 if (ip->pmp->ronly)
420 return (EROFS);
423 * Normally disallow setattr if there is no space, unless we
424 * are in emergency mode (might be needed to chflags -R noschg
425 * files prior to removal).
427 if ((ip->pmp->flags & HAMMER2_PMPF_EMERG) == 0 &&
428 hammer2_vfs_enospace(ip, 0, ap->a_cred) > 1) {
429 return (ENOSPC);
432 hammer2_trans_init(ip->pmp, 0);
433 hammer2_inode_lock(ip, 0);
434 error = 0;
436 if (vap->va_flags != VNOVAL) {
437 uint32_t flags;
439 flags = ip->meta.uflags;
440 error = vop_helper_setattr_flags(&flags, vap->va_flags,
441 hammer2_to_unix_xid(&ip->meta.uid),
442 ap->a_cred);
443 if (error == 0) {
444 if (ip->meta.uflags != flags) {
445 hammer2_inode_modify(ip);
446 hammer2_spin_lock_update(&ip->cluster_spin);
447 ip->meta.uflags = flags;
448 ip->meta.ctime = ctime;
449 hammer2_spin_unlock_update(&ip->cluster_spin);
450 kflags |= NOTE_ATTRIB;
452 if (ip->meta.uflags & (IMMUTABLE | APPEND)) {
453 error = 0;
454 goto done;
457 goto done;
459 if (ip->meta.uflags & (IMMUTABLE | APPEND)) {
460 error = EPERM;
461 goto done;
463 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
464 mode_t cur_mode = ip->meta.mode;
465 uid_t cur_uid = hammer2_to_unix_xid(&ip->meta.uid);
466 gid_t cur_gid = hammer2_to_unix_xid(&ip->meta.gid);
467 uuid_t uuid_uid;
468 uuid_t uuid_gid;
470 error = vop_helper_chown(ap->a_vp, vap->va_uid, vap->va_gid,
471 ap->a_cred,
472 &cur_uid, &cur_gid, &cur_mode);
473 if (error == 0) {
474 hammer2_guid_to_uuid(&uuid_uid, cur_uid);
475 hammer2_guid_to_uuid(&uuid_gid, cur_gid);
476 if (bcmp(&uuid_uid, &ip->meta.uid, sizeof(uuid_uid)) ||
477 bcmp(&uuid_gid, &ip->meta.gid, sizeof(uuid_gid)) ||
478 ip->meta.mode != cur_mode
480 hammer2_inode_modify(ip);
481 hammer2_spin_lock_update(&ip->cluster_spin);
482 ip->meta.uid = uuid_uid;
483 ip->meta.gid = uuid_gid;
484 ip->meta.mode = cur_mode;
485 ip->meta.ctime = ctime;
486 hammer2_spin_unlock_update(&ip->cluster_spin);
488 kflags |= NOTE_ATTRIB;
493 * Resize the file
495 if (vap->va_size != VNOVAL && ip->meta.size != vap->va_size) {
496 switch(vp->v_type) {
497 case VREG:
498 if (vap->va_size == ip->meta.size)
499 break;
500 if (vap->va_size < ip->meta.size) {
501 hammer2_mtx_ex(&ip->truncate_lock);
502 hammer2_truncate_file(ip, vap->va_size);
503 hammer2_mtx_unlock(&ip->truncate_lock);
504 kflags |= NOTE_WRITE;
505 } else {
506 hammer2_extend_file(ip, vap->va_size);
507 kflags |= NOTE_WRITE | NOTE_EXTEND;
509 hammer2_inode_modify(ip);
510 ip->meta.mtime = ctime;
511 vclrflags(vp, VLASTWRITETS);
512 break;
513 default:
514 error = EINVAL;
515 goto done;
518 #if 0
519 /* atime not supported */
520 if (vap->va_atime.tv_sec != VNOVAL) {
521 hammer2_inode_modify(ip);
522 ip->meta.atime = hammer2_timespec_to_time(&vap->va_atime);
523 kflags |= NOTE_ATTRIB;
525 #endif
526 if (vap->va_mode != (mode_t)VNOVAL) {
527 mode_t cur_mode = ip->meta.mode;
528 uid_t cur_uid = hammer2_to_unix_xid(&ip->meta.uid);
529 gid_t cur_gid = hammer2_to_unix_xid(&ip->meta.gid);
531 error = vop_helper_chmod(ap->a_vp, vap->va_mode, ap->a_cred,
532 cur_uid, cur_gid, &cur_mode);
533 if (error == 0) {
534 hammer2_inode_modify(ip);
535 hammer2_spin_lock_update(&ip->cluster_spin);
536 ip->meta.mode = cur_mode;
537 ip->meta.ctime = ctime;
538 hammer2_spin_unlock_update(&ip->cluster_spin);
539 kflags |= NOTE_ATTRIB;
543 if (vap->va_mtime.tv_sec != VNOVAL) {
544 hammer2_inode_modify(ip);
545 ip->meta.mtime = hammer2_timespec_to_time(&vap->va_mtime);
546 kflags |= NOTE_ATTRIB;
547 vclrflags(vp, VLASTWRITETS);
550 done:
552 * If a truncation occurred we must call chain_sync() now in order
553 * to trim the related data chains, otherwise a later expansion can
554 * cause havoc.
556 * If an extend occured that changed the DIRECTDATA state, we must
557 * call inode_chain_sync now in order to prepare the inode's indirect
558 * block table.
560 * WARNING! This means we are making an adjustment to the inode's
561 * chain outside of sync/fsync, and not just to inode->meta, which
562 * may result in some consistency issues if a crash were to occur
563 * at just the wrong time.
565 if (ip->flags & HAMMER2_INODE_RESIZED)
566 hammer2_inode_chain_sync(ip);
569 * Cleanup.
571 hammer2_inode_unlock(ip);
572 hammer2_trans_done(ip->pmp, HAMMER2_TRANS_SIDEQ);
573 hammer2_knote(ip->vp, kflags);
575 return (error);
578 static
580 hammer2_vop_readdir(struct vop_readdir_args *ap)
582 hammer2_xop_readdir_t *xop;
583 hammer2_blockref_t bref;
584 hammer2_inode_t *ip;
585 hammer2_tid_t inum;
586 hammer2_key_t lkey;
587 struct uio *uio;
588 off_t *cookies;
589 off_t saveoff;
590 int cookie_index;
591 int ncookies;
592 int error;
593 int eofflag;
594 int r;
596 ip = VTOI(ap->a_vp);
597 uio = ap->a_uio;
598 saveoff = uio->uio_offset;
599 eofflag = 0;
600 error = 0;
603 * Setup cookies directory entry cookies if requested
605 if (ap->a_ncookies) {
606 ncookies = uio->uio_resid / 16 + 1;
607 if (ncookies > 1024)
608 ncookies = 1024;
609 cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
610 } else {
611 ncookies = -1;
612 cookies = NULL;
614 cookie_index = 0;
616 hammer2_inode_lock(ip, HAMMER2_RESOLVE_SHARED);
619 * Handle artificial entries. To ensure that only positive 64 bit
620 * quantities are returned to userland we always strip off bit 63.
621 * The hash code is designed such that codes 0x0000-0x7FFF are not
622 * used, allowing us to use these codes for articial entries.
624 * Entry 0 is used for '.' and entry 1 is used for '..'. Do not
625 * allow '..' to cross the mount point into (e.g.) the super-root.
627 if (saveoff == 0) {
628 inum = ip->meta.inum & HAMMER2_DIRHASH_USERMSK;
629 r = vop_write_dirent(&error, uio, inum, DT_DIR, 1, ".");
630 if (r)
631 goto done;
632 if (cookies)
633 cookies[cookie_index] = saveoff;
634 ++saveoff;
635 ++cookie_index;
636 if (cookie_index == ncookies)
637 goto done;
640 if (saveoff == 1) {
641 inum = ip->meta.inum & HAMMER2_DIRHASH_USERMSK;
642 if (ip != ip->pmp->iroot)
643 inum = ip->meta.iparent & HAMMER2_DIRHASH_USERMSK;
644 r = vop_write_dirent(&error, uio, inum, DT_DIR, 2, "..");
645 if (r)
646 goto done;
647 if (cookies)
648 cookies[cookie_index] = saveoff;
649 ++saveoff;
650 ++cookie_index;
651 if (cookie_index == ncookies)
652 goto done;
655 lkey = saveoff | HAMMER2_DIRHASH_VISIBLE;
656 if (hammer2_debug & 0x0020)
657 kprintf("readdir: lkey %016jx\n", lkey);
658 if (error)
659 goto done;
661 xop = hammer2_xop_alloc(ip, 0);
662 xop->lkey = lkey;
663 hammer2_xop_start(&xop->head, &hammer2_readdir_desc);
665 for (;;) {
666 const hammer2_inode_data_t *ripdata;
667 const char *dname;
668 int dtype;
670 error = hammer2_xop_collect(&xop->head, 0);
671 error = hammer2_error_to_errno(error);
672 if (error) {
673 break;
675 if (cookie_index == ncookies)
676 break;
677 if (hammer2_debug & 0x0020)
678 kprintf("cluster chain %p %p\n",
679 xop->head.cluster.focus,
680 (xop->head.cluster.focus ?
681 xop->head.cluster.focus->data : (void *)-1));
682 hammer2_cluster_bref(&xop->head.cluster, &bref);
684 if (bref.type == HAMMER2_BREF_TYPE_INODE) {
685 ripdata = &hammer2_xop_gdata(&xop->head)->ipdata;
686 dtype = hammer2_get_dtype(ripdata->meta.type);
687 saveoff = bref.key & HAMMER2_DIRHASH_USERMSK;
688 r = vop_write_dirent(&error, uio,
689 ripdata->meta.inum &
690 HAMMER2_DIRHASH_USERMSK,
691 dtype,
692 ripdata->meta.name_len,
693 ripdata->filename);
694 hammer2_xop_pdata(&xop->head);
695 if (r)
696 break;
697 if (cookies)
698 cookies[cookie_index] = saveoff;
699 ++cookie_index;
700 } else if (bref.type == HAMMER2_BREF_TYPE_DIRENT) {
701 uint16_t namlen;
703 dtype = hammer2_get_dtype(bref.embed.dirent.type);
704 saveoff = bref.key & HAMMER2_DIRHASH_USERMSK;
705 namlen = bref.embed.dirent.namlen;
706 if (namlen <= sizeof(bref.check.buf)) {
707 dname = bref.check.buf;
708 } else {
709 dname = hammer2_xop_gdata(&xop->head)->buf;
711 r = vop_write_dirent(&error, uio,
712 bref.embed.dirent.inum, dtype,
713 namlen, dname);
714 if (namlen > sizeof(bref.check.buf))
715 hammer2_xop_pdata(&xop->head);
716 if (r)
717 break;
718 if (cookies)
719 cookies[cookie_index] = saveoff;
720 ++cookie_index;
721 } else {
722 /* XXX chain error */
723 kprintf("bad chain type readdir %d\n", bref.type);
726 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
727 if (error == ENOENT) {
728 error = 0;
729 eofflag = 1;
730 saveoff = (hammer2_key_t)-1;
731 } else {
732 saveoff = bref.key & HAMMER2_DIRHASH_USERMSK;
734 done:
735 hammer2_inode_unlock(ip);
736 if (ap->a_eofflag)
737 *ap->a_eofflag = eofflag;
738 if (hammer2_debug & 0x0020)
739 kprintf("readdir: done at %016jx\n", saveoff);
740 uio->uio_offset = saveoff & ~HAMMER2_DIRHASH_VISIBLE;
741 if (error && cookie_index == 0) {
742 if (cookies) {
743 kfree(cookies, M_TEMP);
744 *ap->a_ncookies = 0;
745 *ap->a_cookies = NULL;
747 } else {
748 if (cookies) {
749 *ap->a_ncookies = cookie_index;
750 *ap->a_cookies = cookies;
753 return (error);
757 * hammer2_vop_readlink { vp, uio, cred }
759 static
761 hammer2_vop_readlink(struct vop_readlink_args *ap)
763 struct vnode *vp;
764 hammer2_inode_t *ip;
765 int error;
767 vp = ap->a_vp;
768 if (vp->v_type != VLNK)
769 return (EINVAL);
770 ip = VTOI(vp);
772 error = hammer2_read_file(ip, ap->a_uio, 0);
773 return (error);
776 static
778 hammer2_vop_read(struct vop_read_args *ap)
780 struct vnode *vp;
781 hammer2_inode_t *ip;
782 struct uio *uio;
783 int error;
784 int seqcount;
787 * Read operations supported on this vnode?
789 vp = ap->a_vp;
790 if (vp->v_type == VDIR)
791 return (EISDIR);
792 if (vp->v_type != VREG)
793 return (EINVAL);
796 * Misc
798 ip = VTOI(vp);
799 uio = ap->a_uio;
800 error = 0;
802 seqcount = ap->a_ioflag >> IO_SEQSHIFT;
804 error = hammer2_read_file(ip, uio, seqcount);
805 return (error);
808 static
810 hammer2_vop_write(struct vop_write_args *ap)
812 hammer2_inode_t *ip;
813 thread_t td;
814 struct vnode *vp;
815 struct uio *uio;
816 int error;
817 int seqcount;
818 int ioflag;
821 * Read operations supported on this vnode?
823 vp = ap->a_vp;
824 if (vp->v_type != VREG)
825 return (EINVAL);
828 * Misc
830 ip = VTOI(vp);
831 ioflag = ap->a_ioflag;
832 uio = ap->a_uio;
833 error = 0;
834 if (ip->pmp->ronly || (ip->pmp->flags & HAMMER2_PMPF_EMERG))
835 return (EROFS);
836 switch (hammer2_vfs_enospace(ip, uio->uio_resid, ap->a_cred)) {
837 case 2:
838 return (ENOSPC);
839 case 1:
840 ioflag |= IO_DIRECT; /* semi-synchronous */
841 /* fall through */
842 default:
843 break;
846 seqcount = ioflag >> IO_SEQSHIFT;
849 * Check resource limit
851 if (uio->uio_resid > 0 && (td = uio->uio_td) != NULL && td->td_proc &&
852 uio->uio_offset + uio->uio_resid >
853 td->td_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
854 lwpsignal(td->td_proc, td->td_lwp, SIGXFSZ);
855 return (EFBIG);
859 * The transaction interlocks against flush initiations
860 * (note: but will run concurrently with the actual flush).
862 * To avoid deadlocking against the VM system, we must flag any
863 * transaction related to the buffer cache or other direct
864 * VM page manipulation.
866 if (uio->uio_segflg == UIO_NOCOPY) {
867 hammer2_trans_init(ip->pmp, HAMMER2_TRANS_BUFCACHE);
868 } else {
869 hammer2_trans_init(ip->pmp, 0);
871 error = hammer2_write_file(ip, uio, ioflag, seqcount);
872 if (uio->uio_segflg == UIO_NOCOPY)
873 hammer2_trans_done(ip->pmp, HAMMER2_TRANS_BUFCACHE |
874 HAMMER2_TRANS_SIDEQ);
875 else
876 hammer2_trans_done(ip->pmp, HAMMER2_TRANS_SIDEQ);
878 return (error);
882 * Perform read operations on a file or symlink given an UNLOCKED
883 * inode and uio.
885 * The passed ip is not locked.
887 static
889 hammer2_read_file(hammer2_inode_t *ip, struct uio *uio, int seqcount)
891 hammer2_off_t size;
892 struct buf *bp;
893 int error;
895 error = 0;
898 * UIO read loop.
900 * WARNING! Assumes that the kernel interlocks size changes at the
901 * vnode level.
903 hammer2_mtx_sh(&ip->lock);
904 hammer2_mtx_sh(&ip->truncate_lock);
905 size = ip->meta.size;
906 hammer2_mtx_unlock(&ip->lock);
908 while (uio->uio_resid > 0 && uio->uio_offset < size) {
909 hammer2_key_t lbase;
910 hammer2_key_t leof;
911 int lblksize;
912 int loff;
913 int n;
915 lblksize = hammer2_calc_logical(ip, uio->uio_offset,
916 &lbase, &leof);
918 #if 1
919 bp = NULL;
920 error = cluster_readx(ip->vp, leof, lbase, lblksize,
921 B_NOTMETA | B_KVABIO,
922 uio->uio_resid,
923 seqcount * MAXBSIZE,
924 &bp);
925 #else
926 if (uio->uio_segflg == UIO_NOCOPY) {
927 bp = getblk(ip->vp, lbase, lblksize,
928 GETBLK_BHEAVY | GETBLK_KVABIO, 0);
929 if (bp->b_flags & B_CACHE) {
930 int i;
931 int j = 0;
932 if (bp->b_xio.xio_npages != 16)
933 kprintf("NPAGES BAD\n");
934 for (i = 0; i < bp->b_xio.xio_npages; ++i) {
935 vm_page_t m;
936 m = bp->b_xio.xio_pages[i];
937 if (m == NULL || m->valid == 0) {
938 kprintf("bp %016jx %016jx pg %d inv",
939 lbase, leof, i);
940 if (m)
941 kprintf("m->object %p/%p", m->object, ip->vp->v_object);
942 kprintf("\n");
943 j = 1;
946 if (j)
947 kprintf("b_flags %08x, b_error %d\n", bp->b_flags, bp->b_error);
949 bqrelse(bp);
951 error = bread_kvabio(ip->vp, lbase, lblksize, &bp);
952 #endif
953 if (error) {
954 brelse(bp);
955 break;
957 bkvasync(bp);
958 loff = (int)(uio->uio_offset - lbase);
959 n = lblksize - loff;
960 if (n > uio->uio_resid)
961 n = uio->uio_resid;
962 if (n > size - uio->uio_offset)
963 n = (int)(size - uio->uio_offset);
964 bp->b_flags |= B_AGE;
965 uiomovebp(bp, bp->b_data + loff, n, uio);
966 bqrelse(bp);
968 hammer2_mtx_unlock(&ip->truncate_lock);
970 return (error);
974 * Write to the file represented by the inode via the logical buffer cache.
975 * The inode may represent a regular file or a symlink.
977 * The inode must not be locked.
979 static
981 hammer2_write_file(hammer2_inode_t *ip, struct uio *uio,
982 int ioflag, int seqcount)
984 hammer2_key_t old_eof;
985 hammer2_key_t new_eof;
986 struct buf *bp;
987 int kflags;
988 int error;
989 int modified;
992 * Setup if append
994 * WARNING! Assumes that the kernel interlocks size changes at the
995 * vnode level.
997 hammer2_mtx_ex(&ip->lock);
998 hammer2_mtx_sh(&ip->truncate_lock);
999 if (ioflag & IO_APPEND)
1000 uio->uio_offset = ip->meta.size;
1001 old_eof = ip->meta.size;
1004 * Extend the file if necessary. If the write fails at some point
1005 * we will truncate it back down to cover as much as we were able
1006 * to write.
1008 * Doing this now makes it easier to calculate buffer sizes in
1009 * the loop.
1011 kflags = 0;
1012 error = 0;
1013 modified = 0;
1015 if (uio->uio_offset + uio->uio_resid > old_eof) {
1016 new_eof = uio->uio_offset + uio->uio_resid;
1017 modified = 1;
1018 hammer2_extend_file(ip, new_eof);
1019 kflags |= NOTE_EXTEND;
1020 } else {
1021 new_eof = old_eof;
1023 hammer2_mtx_unlock(&ip->lock);
1026 * UIO write loop
1028 while (uio->uio_resid > 0) {
1029 hammer2_key_t lbase;
1030 int trivial;
1031 int endofblk;
1032 int lblksize;
1033 int loff;
1034 int n;
1037 * Don't allow the buffer build to blow out the buffer
1038 * cache.
1040 if ((ioflag & IO_RECURSE) == 0)
1041 bwillwrite(HAMMER2_PBUFSIZE);
1044 * This nominally tells us how much we can cluster and
1045 * what the logical buffer size needs to be. Currently
1046 * we don't try to cluster the write and just handle one
1047 * block at a time.
1049 lblksize = hammer2_calc_logical(ip, uio->uio_offset,
1050 &lbase, NULL);
1051 loff = (int)(uio->uio_offset - lbase);
1053 KKASSERT(lblksize <= MAXBSIZE);
1056 * Calculate bytes to copy this transfer and whether the
1057 * copy completely covers the buffer or not.
1059 trivial = 0;
1060 n = lblksize - loff;
1061 if (n > uio->uio_resid) {
1062 n = uio->uio_resid;
1063 if (loff == lbase && uio->uio_offset + n == new_eof)
1064 trivial = 1;
1065 endofblk = 0;
1066 } else {
1067 if (loff == 0)
1068 trivial = 1;
1069 endofblk = 1;
1071 if (lbase >= new_eof)
1072 trivial = 1;
1075 * Get the buffer
1077 if (uio->uio_segflg == UIO_NOCOPY) {
1079 * Issuing a write with the same data backing the
1080 * buffer. Instantiate the buffer to collect the
1081 * backing vm pages, then read-in any missing bits.
1083 * This case is used by vop_stdputpages().
1085 bp = getblk(ip->vp, lbase, lblksize,
1086 GETBLK_BHEAVY | GETBLK_KVABIO, 0);
1087 if ((bp->b_flags & B_CACHE) == 0) {
1088 bqrelse(bp);
1089 error = bread_kvabio(ip->vp, lbase,
1090 lblksize, &bp);
1092 } else if (trivial) {
1094 * Even though we are entirely overwriting the buffer
1095 * we may still have to zero it out to avoid a
1096 * mmap/write visibility issue.
1098 bp = getblk(ip->vp, lbase, lblksize,
1099 GETBLK_BHEAVY | GETBLK_KVABIO, 0);
1100 if ((bp->b_flags & B_CACHE) == 0)
1101 vfs_bio_clrbuf(bp);
1102 } else {
1104 * Partial overwrite, read in any missing bits then
1105 * replace the portion being written.
1107 * (The strategy code will detect zero-fill physical
1108 * blocks for this case).
1110 error = bread_kvabio(ip->vp, lbase, lblksize, &bp);
1111 if (error == 0)
1112 bheavy(bp);
1115 if (error) {
1116 brelse(bp);
1117 break;
1121 * Ok, copy the data in
1123 bkvasync(bp);
1124 error = uiomovebp(bp, bp->b_data + loff, n, uio);
1125 kflags |= NOTE_WRITE;
1126 modified = 1;
1127 if (error) {
1128 brelse(bp);
1129 break;
1133 * WARNING: Pageout daemon will issue UIO_NOCOPY writes
1134 * with IO_SYNC or IO_ASYNC set. These writes
1135 * must be handled as the pageout daemon expects.
1137 * NOTE! H2 relies on cluster_write() here because it
1138 * cannot preallocate disk blocks at the logical
1139 * level due to not knowing what the compression
1140 * size will be at this time.
1142 * We must use cluster_write() here and we depend
1143 * on the write-behind feature to flush buffers
1144 * appropriately. If we let the buffer daemons do
1145 * it the block allocations will be all over the
1146 * map.
1148 if (ioflag & IO_SYNC) {
1149 bwrite(bp);
1150 } else if ((ioflag & IO_DIRECT) && endofblk) {
1151 bawrite(bp);
1152 } else if (ioflag & IO_ASYNC) {
1153 bawrite(bp);
1154 } else if (ip->vp->v_mount->mnt_flag & MNT_NOCLUSTERW) {
1155 bdwrite(bp);
1156 } else {
1157 #if 1
1158 bp->b_flags |= B_CLUSTEROK;
1159 cluster_write(bp, new_eof, lblksize, seqcount);
1160 #else
1161 bp->b_flags |= B_CLUSTEROK;
1162 bdwrite(bp);
1163 #endif
1168 * Cleanup. If we extended the file EOF but failed to write through
1169 * the entire write is a failure and we have to back-up.
1171 if (error && new_eof != old_eof) {
1172 hammer2_mtx_unlock(&ip->truncate_lock);
1173 hammer2_mtx_ex(&ip->lock); /* note lock order */
1174 hammer2_mtx_ex(&ip->truncate_lock); /* note lock order */
1175 hammer2_truncate_file(ip, old_eof);
1176 if (ip->flags & HAMMER2_INODE_MODIFIED)
1177 hammer2_inode_chain_sync(ip);
1178 hammer2_mtx_unlock(&ip->lock);
1179 } else if (modified) {
1180 struct vnode *vp = ip->vp;
1182 hammer2_mtx_ex(&ip->lock);
1183 hammer2_inode_modify(ip);
1184 if (uio->uio_segflg == UIO_NOCOPY) {
1185 if (vp->v_flag & VLASTWRITETS) {
1186 ip->meta.mtime =
1187 (unsigned long)vp->v_lastwrite_ts.tv_sec *
1188 1000000 +
1189 vp->v_lastwrite_ts.tv_nsec / 1000;
1191 } else {
1192 hammer2_update_time(&ip->meta.mtime);
1193 vclrflags(vp, VLASTWRITETS);
1196 #if 0
1198 * REMOVED - handled by hammer2_extend_file(). Do not issue
1199 * a chain_sync() outside of a sync/fsync except for DIRECTDATA
1200 * state changes.
1202 * Under normal conditions we only issue a chain_sync if
1203 * the inode's DIRECTDATA state changed.
1205 if (ip->flags & HAMMER2_INODE_RESIZED)
1206 hammer2_inode_chain_sync(ip);
1207 #endif
1208 hammer2_mtx_unlock(&ip->lock);
1209 hammer2_knote(ip->vp, kflags);
1211 hammer2_trans_assert_strategy(ip->pmp);
1212 hammer2_mtx_unlock(&ip->truncate_lock);
1214 return error;
1218 * Truncate the size of a file. The inode must be locked.
1220 * We must unconditionally set HAMMER2_INODE_RESIZED to properly
1221 * ensure that any on-media data beyond the new file EOF has been destroyed.
1223 * WARNING: nvtruncbuf() can only be safely called without the inode lock
1224 * held due to the way our write thread works. If the truncation
1225 * occurs in the middle of a buffer, nvtruncbuf() is responsible
1226 * for dirtying that buffer and zeroing out trailing bytes.
1228 * WARNING! Assumes that the kernel interlocks size changes at the
1229 * vnode level.
1231 * WARNING! Caller assumes responsibility for removing dead blocks
1232 * if INODE_RESIZED is set.
1234 static
1235 void
1236 hammer2_truncate_file(hammer2_inode_t *ip, hammer2_key_t nsize)
1238 hammer2_key_t lbase;
1239 int nblksize;
1241 hammer2_mtx_unlock(&ip->lock);
1242 if (ip->vp) {
1243 nblksize = hammer2_calc_logical(ip, nsize, &lbase, NULL);
1244 nvtruncbuf(ip->vp, nsize,
1245 nblksize, (int)nsize & (nblksize - 1),
1248 hammer2_mtx_ex(&ip->lock);
1249 KKASSERT((ip->flags & HAMMER2_INODE_RESIZED) == 0);
1250 ip->osize = ip->meta.size;
1251 ip->meta.size = nsize;
1252 atomic_set_int(&ip->flags, HAMMER2_INODE_RESIZED);
1253 hammer2_inode_modify(ip);
1257 * Extend the size of a file. The inode must be locked.
1259 * Even though the file size is changing, we do not have to set the
1260 * INODE_RESIZED bit unless the file size crosses the EMBEDDED_BYTES
1261 * boundary. When this occurs a hammer2_inode_chain_sync() is required
1262 * to prepare the inode cluster's indirect block table, otherwise
1263 * async execution of the strategy code will implode on us.
1265 * WARNING! Assumes that the kernel interlocks size changes at the
1266 * vnode level.
1268 * WARNING! Caller assumes responsibility for transitioning out
1269 * of the inode DIRECTDATA mode if INODE_RESIZED is set.
1271 static
1272 void
1273 hammer2_extend_file(hammer2_inode_t *ip, hammer2_key_t nsize)
1275 hammer2_key_t lbase;
1276 hammer2_key_t osize;
1277 int oblksize;
1278 int nblksize;
1279 int error;
1281 KKASSERT((ip->flags & HAMMER2_INODE_RESIZED) == 0);
1282 hammer2_inode_modify(ip);
1283 osize = ip->meta.size;
1284 ip->osize = osize;
1285 ip->meta.size = nsize;
1288 * We must issue a chain_sync() when the DIRECTDATA state changes
1289 * to prevent confusion between the flush code and the in-memory
1290 * state. This is not perfect because we are doing it outside of
1291 * a sync/fsync operation, so it might not be fully synchronized
1292 * with the meta-data topology flush.
1294 * We must retain and re-dirty the buffer cache buffer containing
1295 * the direct data so it can be written to a real block. It should
1296 * not be possible for a bread error to occur since the original data
1297 * is extracted from the inode structure directly.
1299 if (osize <= HAMMER2_EMBEDDED_BYTES && nsize > HAMMER2_EMBEDDED_BYTES) {
1300 if (osize) {
1301 struct buf *bp;
1303 oblksize = hammer2_calc_logical(ip, 0, NULL, NULL);
1304 error = bread_kvabio(ip->vp, 0, oblksize, &bp);
1305 atomic_set_int(&ip->flags, HAMMER2_INODE_RESIZED);
1306 hammer2_inode_chain_sync(ip);
1307 if (error == 0) {
1308 bheavy(bp);
1309 bdwrite(bp);
1310 } else {
1311 brelse(bp);
1313 } else {
1314 atomic_set_int(&ip->flags, HAMMER2_INODE_RESIZED);
1315 hammer2_inode_chain_sync(ip);
1318 hammer2_mtx_unlock(&ip->lock);
1319 if (ip->vp) {
1320 oblksize = hammer2_calc_logical(ip, osize, &lbase, NULL);
1321 nblksize = hammer2_calc_logical(ip, nsize, &lbase, NULL);
1322 nvextendbuf(ip->vp,
1323 osize, nsize,
1324 oblksize, nblksize,
1325 -1, -1, 0);
1327 hammer2_mtx_ex(&ip->lock);
1330 static
1332 hammer2_vop_nresolve(struct vop_nresolve_args *ap)
1334 hammer2_xop_nresolve_t *xop;
1335 hammer2_inode_t *ip;
1336 hammer2_inode_t *dip;
1337 struct namecache *ncp;
1338 struct vnode *vp;
1339 int error;
1341 dip = VTOI(ap->a_dvp);
1342 xop = hammer2_xop_alloc(dip, 0);
1344 ncp = ap->a_nch->ncp;
1345 hammer2_xop_setname(&xop->head, ncp->nc_name, ncp->nc_nlen);
1348 * Note: In DragonFly the kernel handles '.' and '..'.
1350 hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);
1351 hammer2_xop_start(&xop->head, &hammer2_nresolve_desc);
1353 error = hammer2_xop_collect(&xop->head, 0);
1354 error = hammer2_error_to_errno(error);
1355 if (error) {
1356 ip = NULL;
1357 } else {
1358 ip = hammer2_inode_get(dip->pmp, &xop->head, -1, -1);
1360 hammer2_inode_unlock(dip);
1363 * Acquire the related vnode
1365 * NOTE: For error processing, only ENOENT resolves the namecache
1366 * entry to NULL, otherwise we just return the error and
1367 * leave the namecache unresolved.
1369 * WARNING: inode structure is locked exclusively via inode_get
1370 * but chain was locked shared. inode_unlock()
1371 * will handle it properly.
1373 if (ip) {
1374 vp = hammer2_igetv(ip, &error); /* error set to UNIX error */
1375 if (error == 0) {
1376 vn_unlock(vp);
1377 cache_setvp(ap->a_nch, vp);
1378 } else if (error == ENOENT) {
1379 cache_setvp(ap->a_nch, NULL);
1381 hammer2_inode_unlock(ip);
1384 * The vp should not be released until after we've disposed
1385 * of our locks, because it might cause vop_inactive() to
1386 * be called.
1388 if (vp)
1389 vrele(vp);
1390 } else {
1391 error = ENOENT;
1392 cache_setvp(ap->a_nch, NULL);
1394 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
1395 KASSERT(error || ap->a_nch->ncp->nc_vp != NULL,
1396 ("resolve error %d/%p ap %p\n",
1397 error, ap->a_nch->ncp->nc_vp, ap));
1399 return error;
1402 static
1404 hammer2_vop_nlookupdotdot(struct vop_nlookupdotdot_args *ap)
1406 hammer2_inode_t *dip;
1407 hammer2_tid_t inum;
1408 int error;
1410 dip = VTOI(ap->a_dvp);
1411 inum = dip->meta.iparent;
1412 *ap->a_vpp = NULL;
1414 if (inum) {
1415 error = hammer2_vfs_vget(ap->a_dvp->v_mount, NULL,
1416 inum, ap->a_vpp);
1417 } else {
1418 error = ENOENT;
1420 return error;
1423 static
1425 hammer2_vop_nmkdir(struct vop_nmkdir_args *ap)
1427 hammer2_inode_t *dip;
1428 hammer2_inode_t *nip;
1429 struct namecache *ncp;
1430 const char *name;
1431 size_t name_len;
1432 hammer2_tid_t inum;
1433 int error;
1435 dip = VTOI(ap->a_dvp);
1436 if (dip->pmp->ronly || (dip->pmp->flags & HAMMER2_PMPF_EMERG))
1437 return (EROFS);
1438 if (hammer2_vfs_enospace(dip, 0, ap->a_cred) > 1)
1439 return (ENOSPC);
1441 ncp = ap->a_nch->ncp;
1442 name = ncp->nc_name;
1443 name_len = ncp->nc_nlen;
1445 hammer2_trans_init(dip->pmp, 0);
1447 inum = hammer2_trans_newinum(dip->pmp);
1450 * Create the actual inode as a hidden file in the iroot, then
1451 * create the directory entry. The creation of the actual inode
1452 * sets its nlinks to 1 which is the value we desire.
1454 * dip must be locked before nip to avoid deadlock.
1456 hammer2_inode_lock(dip, 0);
1457 nip = hammer2_inode_create_normal(dip, ap->a_vap, ap->a_cred,
1458 inum, &error);
1459 if (error) {
1460 error = hammer2_error_to_errno(error);
1461 } else {
1462 error = hammer2_dirent_create(dip, name, name_len,
1463 nip->meta.inum, nip->meta.type);
1464 /* returns UNIX error code */
1466 if (error) {
1467 if (nip) {
1468 hammer2_inode_unlink_finisher(nip, NULL);
1469 hammer2_inode_unlock(nip);
1470 nip = NULL;
1472 *ap->a_vpp = NULL;
1473 } else {
1475 * inode_depend() must occur before the igetv() because
1476 * the igetv() can temporarily release the inode lock.
1478 hammer2_inode_depend(dip, nip); /* before igetv */
1479 *ap->a_vpp = hammer2_igetv(nip, &error);
1480 hammer2_inode_unlock(nip);
1484 * Update dip's mtime
1486 * We can use a shared inode lock and allow the meta.mtime update
1487 * SMP race. hammer2_inode_modify() is MPSAFE w/a shared lock.
1489 if (error == 0) {
1490 uint64_t mtime;
1492 /*hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);*/
1493 hammer2_update_time(&mtime);
1494 hammer2_inode_modify(dip);
1495 dip->meta.mtime = mtime;
1496 /*hammer2_inode_unlock(dip);*/
1498 hammer2_inode_unlock(dip);
1500 hammer2_trans_done(dip->pmp, HAMMER2_TRANS_SIDEQ);
1502 if (error == 0) {
1503 cache_setunresolved(ap->a_nch);
1504 cache_setvp(ap->a_nch, *ap->a_vpp);
1505 hammer2_knote(ap->a_dvp, NOTE_WRITE | NOTE_LINK);
1507 return error;
1510 static
1512 hammer2_vop_open(struct vop_open_args *ap)
1514 return vop_stdopen(ap);
1518 * hammer2_vop_advlock { vp, id, op, fl, flags }
1520 static
1522 hammer2_vop_advlock(struct vop_advlock_args *ap)
1524 hammer2_inode_t *ip = VTOI(ap->a_vp);
1525 hammer2_off_t size;
1527 size = ip->meta.size;
1528 return (lf_advlock(ap, &ip->advlock, size));
1531 static
1533 hammer2_vop_close(struct vop_close_args *ap)
1535 return vop_stdclose(ap);
1539 * hammer2_vop_nlink { nch, dvp, vp, cred }
1541 * Create a hardlink from (vp) to {dvp, nch}.
1543 static
1545 hammer2_vop_nlink(struct vop_nlink_args *ap)
1547 hammer2_inode_t *tdip; /* target directory to create link in */
1548 hammer2_inode_t *ip; /* inode we are hardlinking to */
1549 struct namecache *ncp;
1550 const char *name;
1551 size_t name_len;
1552 int error;
1553 uint64_t cmtime;
1555 if (ap->a_dvp->v_mount != ap->a_vp->v_mount)
1556 return(EXDEV);
1558 tdip = VTOI(ap->a_dvp);
1559 if (tdip->pmp->ronly || (tdip->pmp->flags & HAMMER2_PMPF_EMERG))
1560 return (EROFS);
1561 if (hammer2_vfs_enospace(tdip, 0, ap->a_cred) > 1)
1562 return (ENOSPC);
1564 ncp = ap->a_nch->ncp;
1565 name = ncp->nc_name;
1566 name_len = ncp->nc_nlen;
1569 * ip represents the file being hardlinked. The file could be a
1570 * normal file or a hardlink target if it has already been hardlinked.
1571 * (with the new semantics, it will almost always be a hardlink
1572 * target).
1574 * Bump nlinks and potentially also create or move the hardlink
1575 * target in the parent directory common to (ip) and (tdip). The
1576 * consolidation code can modify ip->cluster. The returned cluster
1577 * is locked.
1579 ip = VTOI(ap->a_vp);
1580 KASSERT(ip->pmp, ("ip->pmp is NULL %p %p", ip, ip->pmp));
1581 hammer2_trans_init(ip->pmp, 0);
1584 * Target should be an indexed inode or there's no way we will ever
1585 * be able to find it!
1587 KKASSERT((ip->meta.name_key & HAMMER2_DIRHASH_VISIBLE) == 0);
1589 error = 0;
1592 * Can return NULL and error == EXDEV if the common parent
1593 * crosses a directory with the xlink flag set.
1595 hammer2_inode_lock4(tdip, ip, NULL, NULL);
1597 hammer2_update_time(&cmtime);
1600 * Create the directory entry and bump nlinks.
1601 * Also update ip's ctime.
1603 if (error == 0) {
1604 error = hammer2_dirent_create(tdip, name, name_len,
1605 ip->meta.inum, ip->meta.type);
1606 hammer2_inode_modify(ip);
1607 ++ip->meta.nlinks;
1608 ip->meta.ctime = cmtime;
1610 if (error == 0) {
1612 * Update dip's [cm]time
1614 hammer2_inode_modify(tdip);
1615 tdip->meta.mtime = cmtime;
1616 tdip->meta.ctime = cmtime;
1618 cache_setunresolved(ap->a_nch);
1619 cache_setvp(ap->a_nch, ap->a_vp);
1621 hammer2_inode_unlock(ip);
1622 hammer2_inode_unlock(tdip);
1624 hammer2_trans_done(ip->pmp, HAMMER2_TRANS_SIDEQ);
1625 hammer2_knote(ap->a_vp, NOTE_LINK);
1626 hammer2_knote(ap->a_dvp, NOTE_WRITE);
1628 return error;
1632 * hammer2_vop_ncreate { nch, dvp, vpp, cred, vap }
1634 * The operating system has already ensured that the directory entry
1635 * does not exist and done all appropriate namespace locking.
1637 static
1639 hammer2_vop_ncreate(struct vop_ncreate_args *ap)
1641 hammer2_inode_t *dip;
1642 hammer2_inode_t *nip;
1643 struct namecache *ncp;
1644 const char *name;
1645 size_t name_len;
1646 hammer2_tid_t inum;
1647 int error;
1649 dip = VTOI(ap->a_dvp);
1650 if (dip->pmp->ronly || (dip->pmp->flags & HAMMER2_PMPF_EMERG))
1651 return (EROFS);
1652 if (hammer2_vfs_enospace(dip, 0, ap->a_cred) > 1)
1653 return (ENOSPC);
1655 ncp = ap->a_nch->ncp;
1656 name = ncp->nc_name;
1657 name_len = ncp->nc_nlen;
1658 hammer2_trans_init(dip->pmp, 0);
1660 inum = hammer2_trans_newinum(dip->pmp);
1663 * Create the actual inode as a hidden file in the iroot, then
1664 * create the directory entry. The creation of the actual inode
1665 * sets its nlinks to 1 which is the value we desire.
1667 * dip must be locked before nip to avoid deadlock.
1669 hammer2_inode_lock(dip, 0);
1670 nip = hammer2_inode_create_normal(dip, ap->a_vap, ap->a_cred,
1671 inum, &error);
1672 if (error) {
1673 error = hammer2_error_to_errno(error);
1674 } else {
1675 error = hammer2_dirent_create(dip, name, name_len,
1676 nip->meta.inum, nip->meta.type);
1678 if (error) {
1679 if (nip) {
1680 hammer2_inode_unlink_finisher(nip, NULL);
1681 hammer2_inode_unlock(nip);
1682 nip = NULL;
1684 *ap->a_vpp = NULL;
1685 } else {
1686 hammer2_inode_depend(dip, nip); /* before igetv */
1687 *ap->a_vpp = hammer2_igetv(nip, &error);
1688 hammer2_inode_unlock(nip);
1692 * Update dip's mtime
1694 if (error == 0) {
1695 uint64_t mtime;
1697 /*hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);*/
1698 hammer2_update_time(&mtime);
1699 hammer2_inode_modify(dip);
1700 dip->meta.mtime = mtime;
1701 /*hammer2_inode_unlock(dip);*/
1703 hammer2_inode_unlock(dip);
1705 hammer2_trans_done(dip->pmp, HAMMER2_TRANS_SIDEQ);
1707 if (error == 0) {
1708 cache_setunresolved(ap->a_nch);
1709 cache_setvp(ap->a_nch, *ap->a_vpp);
1710 hammer2_knote(ap->a_dvp, NOTE_WRITE);
1712 return error;
1716 * Make a device node (typically a fifo)
1718 static
1720 hammer2_vop_nmknod(struct vop_nmknod_args *ap)
1722 hammer2_inode_t *dip;
1723 hammer2_inode_t *nip;
1724 struct namecache *ncp;
1725 const char *name;
1726 size_t name_len;
1727 hammer2_tid_t inum;
1728 int error;
1730 dip = VTOI(ap->a_dvp);
1731 if (dip->pmp->ronly || (dip->pmp->flags & HAMMER2_PMPF_EMERG))
1732 return (EROFS);
1733 if (hammer2_vfs_enospace(dip, 0, ap->a_cred) > 1)
1734 return (ENOSPC);
1736 ncp = ap->a_nch->ncp;
1737 name = ncp->nc_name;
1738 name_len = ncp->nc_nlen;
1739 hammer2_trans_init(dip->pmp, 0);
1742 * Create the device inode and then create the directory entry.
1744 * dip must be locked before nip to avoid deadlock.
1746 inum = hammer2_trans_newinum(dip->pmp);
1748 hammer2_inode_lock(dip, 0);
1749 nip = hammer2_inode_create_normal(dip, ap->a_vap, ap->a_cred,
1750 inum, &error);
1751 if (error) {
1752 error = hammer2_error_to_errno(error);
1753 } else {
1754 error = hammer2_dirent_create(dip, name, name_len,
1755 nip->meta.inum, nip->meta.type);
1757 if (error) {
1758 if (nip) {
1759 hammer2_inode_unlink_finisher(nip, NULL);
1760 hammer2_inode_unlock(nip);
1761 nip = NULL;
1763 *ap->a_vpp = NULL;
1764 } else {
1765 hammer2_inode_depend(dip, nip); /* before igetv */
1766 *ap->a_vpp = hammer2_igetv(nip, &error);
1767 hammer2_inode_unlock(nip);
1771 * Update dip's mtime
1773 if (error == 0) {
1774 uint64_t mtime;
1776 /*hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);*/
1777 hammer2_update_time(&mtime);
1778 hammer2_inode_modify(dip);
1779 dip->meta.mtime = mtime;
1780 /*hammer2_inode_unlock(dip);*/
1782 hammer2_inode_unlock(dip);
1784 hammer2_trans_done(dip->pmp, HAMMER2_TRANS_SIDEQ);
1786 if (error == 0) {
1787 cache_setunresolved(ap->a_nch);
1788 cache_setvp(ap->a_nch, *ap->a_vpp);
1789 hammer2_knote(ap->a_dvp, NOTE_WRITE);
1791 return error;
1795 * hammer2_vop_nsymlink { nch, dvp, vpp, cred, vap, target }
1797 static
1799 hammer2_vop_nsymlink(struct vop_nsymlink_args *ap)
1801 hammer2_inode_t *dip;
1802 hammer2_inode_t *nip;
1803 struct namecache *ncp;
1804 const char *name;
1805 size_t name_len;
1806 hammer2_tid_t inum;
1807 int error;
1809 dip = VTOI(ap->a_dvp);
1810 if (dip->pmp->ronly || (dip->pmp->flags & HAMMER2_PMPF_EMERG))
1811 return (EROFS);
1812 if (hammer2_vfs_enospace(dip, 0, ap->a_cred) > 1)
1813 return (ENOSPC);
1815 ncp = ap->a_nch->ncp;
1816 name = ncp->nc_name;
1817 name_len = ncp->nc_nlen;
1818 hammer2_trans_init(dip->pmp, 0);
1820 ap->a_vap->va_type = VLNK; /* enforce type */
1823 * Create the softlink as an inode and then create the directory
1824 * entry.
1826 * dip must be locked before nip to avoid deadlock.
1828 inum = hammer2_trans_newinum(dip->pmp);
1830 hammer2_inode_lock(dip, 0);
1831 nip = hammer2_inode_create_normal(dip, ap->a_vap, ap->a_cred,
1832 inum, &error);
1833 if (error) {
1834 error = hammer2_error_to_errno(error);
1835 } else {
1836 error = hammer2_dirent_create(dip, name, name_len,
1837 nip->meta.inum, nip->meta.type);
1839 if (error) {
1840 if (nip) {
1841 hammer2_inode_unlink_finisher(nip, NULL);
1842 hammer2_inode_unlock(nip);
1843 nip = NULL;
1845 *ap->a_vpp = NULL;
1846 hammer2_inode_unlock(dip);
1847 hammer2_trans_done(dip->pmp, HAMMER2_TRANS_SIDEQ);
1848 return error;
1850 hammer2_inode_depend(dip, nip); /* before igetv */
1851 *ap->a_vpp = hammer2_igetv(nip, &error);
1854 * Build the softlink (~like file data) and finalize the namecache.
1856 if (error == 0) {
1857 size_t bytes;
1858 struct uio auio;
1859 struct iovec aiov;
1861 bytes = strlen(ap->a_target);
1863 hammer2_inode_unlock(nip);
1864 bzero(&auio, sizeof(auio));
1865 bzero(&aiov, sizeof(aiov));
1866 auio.uio_iov = &aiov;
1867 auio.uio_segflg = UIO_SYSSPACE;
1868 auio.uio_rw = UIO_WRITE;
1869 auio.uio_resid = bytes;
1870 auio.uio_iovcnt = 1;
1871 auio.uio_td = curthread;
1872 aiov.iov_base = ap->a_target;
1873 aiov.iov_len = bytes;
1874 error = hammer2_write_file(nip, &auio, IO_APPEND, 0);
1875 /* XXX handle error */
1876 error = 0;
1877 } else {
1878 hammer2_inode_unlock(nip);
1882 * Update dip's mtime
1884 if (error == 0) {
1885 uint64_t mtime;
1887 /*hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);*/
1888 hammer2_update_time(&mtime);
1889 hammer2_inode_modify(dip);
1890 dip->meta.mtime = mtime;
1891 /*hammer2_inode_unlock(dip);*/
1893 hammer2_inode_unlock(dip);
1895 hammer2_trans_done(dip->pmp, HAMMER2_TRANS_SIDEQ);
1898 * Finalize namecache
1900 if (error == 0) {
1901 cache_setunresolved(ap->a_nch);
1902 cache_setvp(ap->a_nch, *ap->a_vpp);
1903 hammer2_knote(ap->a_dvp, NOTE_WRITE);
1905 return error;
1909 * hammer2_vop_nremove { nch, dvp, cred }
1911 static
1913 hammer2_vop_nremove(struct vop_nremove_args *ap)
1915 hammer2_xop_unlink_t *xop;
1916 hammer2_inode_t *dip;
1917 hammer2_inode_t *ip;
1918 struct vnode *vprecycle;
1919 struct namecache *ncp;
1920 int error;
1922 dip = VTOI(ap->a_dvp);
1923 if (dip->pmp->ronly)
1924 return (EROFS);
1925 #if 0
1926 /* allow removals, except user to also bulkfree */
1927 if (hammer2_vfs_enospace(dip, 0, ap->a_cred) > 1)
1928 return (ENOSPC);
1929 #endif
1931 ncp = ap->a_nch->ncp;
1933 if (hammer2_debug_inode && dip->meta.inum == hammer2_debug_inode) {
1934 kprintf("hammer2: attempt to delete inside debug inode: %s\n",
1935 ncp->nc_name);
1936 while (hammer2_debug_inode &&
1937 dip->meta.inum == hammer2_debug_inode) {
1938 tsleep(&hammer2_debug_inode, 0, "h2debug", hz*5);
1942 hammer2_trans_init(dip->pmp, 0);
1943 hammer2_inode_lock(dip, 0);
1946 * The unlink XOP unlinks the path from the directory and
1947 * locates and returns the cluster associated with the real inode.
1948 * We have to handle nlinks here on the frontend.
1950 xop = hammer2_xop_alloc(dip, HAMMER2_XOP_MODIFYING);
1951 hammer2_xop_setname(&xop->head, ncp->nc_name, ncp->nc_nlen);
1953 xop->isdir = 0;
1954 xop->dopermanent = 0;
1955 hammer2_xop_start(&xop->head, &hammer2_unlink_desc);
1958 * Collect the real inode and adjust nlinks, destroy the real
1959 * inode if nlinks transitions to 0 and it was the real inode
1960 * (else it has already been removed).
1962 error = hammer2_xop_collect(&xop->head, 0);
1963 error = hammer2_error_to_errno(error);
1964 vprecycle = NULL;
1966 if (error == 0) {
1967 ip = hammer2_inode_get(dip->pmp, &xop->head, -1, -1);
1968 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
1969 if (ip) {
1970 if (hammer2_debug_inode &&
1971 ip->meta.inum == hammer2_debug_inode) {
1972 kprintf("hammer2: attempt to delete debug "
1973 "inode!\n");
1974 while (hammer2_debug_inode &&
1975 ip->meta.inum == hammer2_debug_inode) {
1976 tsleep(&hammer2_debug_inode, 0,
1977 "h2debug", hz*5);
1980 hammer2_inode_unlink_finisher(ip, &vprecycle);
1981 hammer2_inode_depend(dip, ip); /* after modified */
1982 hammer2_inode_unlock(ip);
1984 } else {
1985 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
1989 * Update dip's mtime
1991 if (error == 0) {
1992 uint64_t mtime;
1994 /*hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);*/
1995 hammer2_update_time(&mtime);
1996 hammer2_inode_modify(dip);
1997 dip->meta.mtime = mtime;
1998 /*hammer2_inode_unlock(dip);*/
2000 hammer2_inode_unlock(dip);
2002 hammer2_trans_done(dip->pmp, HAMMER2_TRANS_SIDEQ);
2003 if (error == 0) {
2004 cache_unlink(ap->a_nch);
2005 hammer2_knote(ap->a_dvp, NOTE_WRITE);
2007 if (vprecycle)
2008 hammer2_inode_vprecycle(vprecycle);
2010 return (error);
2014 * hammer2_vop_nrmdir { nch, dvp, cred }
2016 static
2018 hammer2_vop_nrmdir(struct vop_nrmdir_args *ap)
2020 hammer2_xop_unlink_t *xop;
2021 hammer2_inode_t *dip;
2022 hammer2_inode_t *ip;
2023 struct namecache *ncp;
2024 struct vnode *vprecycle;
2025 int error;
2027 dip = VTOI(ap->a_dvp);
2028 if (dip->pmp->ronly)
2029 return (EROFS);
2030 #if 0
2031 /* allow removals, except user to also bulkfree */
2032 if (hammer2_vfs_enospace(dip, 0, ap->a_cred) > 1)
2033 return (ENOSPC);
2034 #endif
2036 hammer2_trans_init(dip->pmp, 0);
2037 hammer2_inode_lock(dip, 0);
2039 xop = hammer2_xop_alloc(dip, HAMMER2_XOP_MODIFYING);
2041 ncp = ap->a_nch->ncp;
2042 hammer2_xop_setname(&xop->head, ncp->nc_name, ncp->nc_nlen);
2043 xop->isdir = 1;
2044 xop->dopermanent = 0;
2045 hammer2_xop_start(&xop->head, &hammer2_unlink_desc);
2048 * Collect the real inode and adjust nlinks, destroy the real
2049 * inode if nlinks transitions to 0 and it was the real inode
2050 * (else it has already been removed).
2052 error = hammer2_xop_collect(&xop->head, 0);
2053 error = hammer2_error_to_errno(error);
2054 vprecycle = NULL;
2056 if (error == 0) {
2057 ip = hammer2_inode_get(dip->pmp, &xop->head, -1, -1);
2058 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
2059 if (ip) {
2060 hammer2_inode_unlink_finisher(ip, &vprecycle);
2061 hammer2_inode_depend(dip, ip); /* after modified */
2062 hammer2_inode_unlock(ip);
2064 } else {
2065 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
2069 * Update dip's mtime
2071 if (error == 0) {
2072 uint64_t mtime;
2074 /*hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);*/
2075 hammer2_update_time(&mtime);
2076 hammer2_inode_modify(dip);
2077 dip->meta.mtime = mtime;
2078 /*hammer2_inode_unlock(dip);*/
2080 hammer2_inode_unlock(dip);
2082 hammer2_trans_done(dip->pmp, HAMMER2_TRANS_SIDEQ);
2083 if (error == 0) {
2084 cache_unlink(ap->a_nch);
2085 hammer2_knote(ap->a_dvp, NOTE_WRITE | NOTE_LINK);
2087 if (vprecycle)
2088 hammer2_inode_vprecycle(vprecycle);
2089 return (error);
2093 * hammer2_vop_nrename { fnch, tnch, fdvp, tdvp, cred }
2095 static
2097 hammer2_vop_nrename(struct vop_nrename_args *ap)
2099 struct namecache *fncp;
2100 struct namecache *tncp;
2101 hammer2_inode_t *fdip; /* source directory */
2102 hammer2_inode_t *tdip; /* target directory */
2103 hammer2_inode_t *ip; /* file being renamed */
2104 hammer2_inode_t *tip; /* replaced target during rename or NULL */
2105 struct vnode *vprecycle;
2106 const char *fname;
2107 size_t fname_len;
2108 const char *tname;
2109 size_t tname_len;
2110 int error;
2111 int update_tdip;
2112 int update_fdip;
2113 hammer2_key_t tlhc;
2115 if (ap->a_fdvp->v_mount != ap->a_tdvp->v_mount)
2116 return(EXDEV);
2117 if (ap->a_fdvp->v_mount != ap->a_fnch->ncp->nc_vp->v_mount)
2118 return(EXDEV);
2120 fdip = VTOI(ap->a_fdvp); /* source directory */
2121 tdip = VTOI(ap->a_tdvp); /* target directory */
2123 if (fdip->pmp->ronly || (fdip->pmp->flags & HAMMER2_PMPF_EMERG))
2124 return (EROFS);
2125 if (hammer2_vfs_enospace(fdip, 0, ap->a_cred) > 1)
2126 return (ENOSPC);
2128 fncp = ap->a_fnch->ncp; /* entry name in source */
2129 fname = fncp->nc_name;
2130 fname_len = fncp->nc_nlen;
2132 tncp = ap->a_tnch->ncp; /* entry name in target */
2133 tname = tncp->nc_name;
2134 tname_len = tncp->nc_nlen;
2136 hammer2_trans_init(tdip->pmp, 0);
2138 update_tdip = 0;
2139 update_fdip = 0;
2141 ip = VTOI(fncp->nc_vp);
2142 hammer2_inode_ref(ip); /* extra ref */
2145 * Lookup the target name to determine if a directory entry
2146 * is being overwritten. We only hold related inode locks
2147 * temporarily, the operating system is expected to protect
2148 * against rename races.
2150 tip = tncp->nc_vp ? VTOI(tncp->nc_vp) : NULL;
2151 if (tip)
2152 hammer2_inode_ref(tip); /* extra ref */
2155 * Can return NULL and error == EXDEV if the common parent
2156 * crosses a directory with the xlink flag set.
2158 * For now try to avoid deadlocks with a simple pointer address
2159 * test. (tip) can be NULL.
2161 error = 0;
2163 hammer2_inode_t *ip1 = fdip;
2164 hammer2_inode_t *ip2 = tdip;
2165 hammer2_inode_t *ip3 = ip;
2166 hammer2_inode_t *ip4 = tip; /* may be NULL */
2168 if (fdip > tdip) {
2169 ip1 = tdip;
2170 ip2 = fdip;
2172 if (tip && ip > tip) {
2173 ip3 = tip;
2174 ip4 = ip;
2176 hammer2_inode_lock4(ip1, ip2, ip3, ip4);
2180 * Resolve the collision space for (tdip, tname, tname_len)
2182 * tdip must be held exclusively locked to prevent races since
2183 * multiple filenames can end up in the same collision space.
2186 hammer2_xop_scanlhc_t *sxop;
2187 hammer2_tid_t lhcbase;
2189 tlhc = hammer2_dirhash(tname, tname_len);
2190 lhcbase = tlhc;
2191 sxop = hammer2_xop_alloc(tdip, HAMMER2_XOP_MODIFYING);
2192 sxop->lhc = tlhc;
2193 hammer2_xop_start(&sxop->head, &hammer2_scanlhc_desc);
2194 while ((error = hammer2_xop_collect(&sxop->head, 0)) == 0) {
2195 if (tlhc != sxop->head.cluster.focus->bref.key)
2196 break;
2197 ++tlhc;
2199 error = hammer2_error_to_errno(error);
2200 hammer2_xop_retire(&sxop->head, HAMMER2_XOPMASK_VOP);
2202 if (error) {
2203 if (error != ENOENT)
2204 goto done2;
2205 ++tlhc;
2206 error = 0;
2208 if ((lhcbase ^ tlhc) & ~HAMMER2_DIRHASH_LOMASK) {
2209 error = ENOSPC;
2210 goto done2;
2215 * Ready to go, issue the rename to the backend. Note that meta-data
2216 * updates to the related inodes occur separately from the rename
2217 * operation.
2219 * NOTE: While it is not necessary to update ip->meta.name*, doing
2220 * so aids catastrophic recovery and debugging.
2222 if (error == 0) {
2223 hammer2_xop_nrename_t *xop4;
2225 xop4 = hammer2_xop_alloc(fdip, HAMMER2_XOP_MODIFYING);
2226 xop4->lhc = tlhc;
2227 xop4->ip_key = ip->meta.name_key;
2228 hammer2_xop_setip2(&xop4->head, ip);
2229 hammer2_xop_setip3(&xop4->head, tdip);
2230 if (tip && tip->meta.type == HAMMER2_OBJTYPE_DIRECTORY)
2231 hammer2_xop_setip4(&xop4->head, tip);
2232 hammer2_xop_setname(&xop4->head, fname, fname_len);
2233 hammer2_xop_setname2(&xop4->head, tname, tname_len);
2234 hammer2_xop_start(&xop4->head, &hammer2_nrename_desc);
2236 error = hammer2_xop_collect(&xop4->head, 0);
2237 error = hammer2_error_to_errno(error);
2238 hammer2_xop_retire(&xop4->head, HAMMER2_XOPMASK_VOP);
2240 if (error == ENOENT)
2241 error = 0;
2244 * Update inode meta-data.
2246 * WARNING! The in-memory inode (ip) structure does not
2247 * maintain a copy of the inode's filename buffer.
2249 if (error == 0 &&
2250 (ip->meta.name_key & HAMMER2_DIRHASH_VISIBLE)) {
2251 hammer2_inode_modify(ip);
2252 ip->meta.name_len = tname_len;
2253 ip->meta.name_key = tlhc;
2255 if (error == 0) {
2256 hammer2_inode_modify(ip);
2257 ip->meta.iparent = tdip->meta.inum;
2259 update_fdip = 1;
2260 update_tdip = 1;
2263 done2:
2265 * If no error, the backend has replaced the target directory entry.
2266 * We must adjust nlinks on the original replace target if it exists.
2268 vprecycle = NULL;
2269 if (error == 0 && tip) {
2270 hammer2_inode_unlink_finisher(tip, &vprecycle);
2274 * Update directory mtimes to represent the something changed.
2276 if (update_fdip || update_tdip) {
2277 uint64_t mtime;
2279 hammer2_update_time(&mtime);
2280 if (update_fdip) {
2281 hammer2_inode_modify(fdip);
2282 fdip->meta.mtime = mtime;
2284 if (update_tdip) {
2285 hammer2_inode_modify(tdip);
2286 tdip->meta.mtime = mtime;
2289 if (tip) {
2290 hammer2_inode_unlock(tip);
2291 hammer2_inode_drop(tip);
2293 hammer2_inode_unlock(ip);
2294 hammer2_inode_unlock(tdip);
2295 hammer2_inode_unlock(fdip);
2296 hammer2_inode_drop(ip);
2297 hammer2_trans_done(tdip->pmp, HAMMER2_TRANS_SIDEQ);
2300 * Issue the namecache update after unlocking all the internal
2301 * hammer2 structures, otherwise we might deadlock.
2303 * WARNING! The target namespace must be updated atomically,
2304 * and we depend on cache_rename() to handle that for
2305 * us. Do not do a separate cache_unlink() because
2306 * that leaves a small window of opportunity for other
2307 * threads to allocate the target namespace before we
2308 * manage to complete our rename.
2310 * WARNING! cache_rename() (and cache_unlink()) will properly
2311 * set VREF_FINALIZE on any attached vnode. Do not
2312 * call cache_setunresolved() manually before-hand as
2313 * this will prevent the flag from being set later via
2314 * cache_rename(). If VREF_FINALIZE is not properly set
2315 * and the inode is no longer in the topology, related
2316 * chains can remain dirty indefinitely.
2318 if (error == 0 && tip) {
2319 /*cache_unlink(ap->a_tnch); see above */
2320 /*cache_setunresolved(ap->a_tnch); see above */
2322 if (error == 0) {
2323 cache_rename(ap->a_fnch, ap->a_tnch);
2324 hammer2_knote(ap->a_fdvp, NOTE_WRITE);
2325 hammer2_knote(ap->a_tdvp, NOTE_WRITE);
2326 hammer2_knote(fncp->nc_vp, NOTE_RENAME);
2328 if (vprecycle)
2329 hammer2_inode_vprecycle(vprecycle);
2331 return (error);
2335 * hammer2_vop_ioctl { vp, command, data, fflag, cred }
2337 static
2339 hammer2_vop_ioctl(struct vop_ioctl_args *ap)
2341 hammer2_inode_t *ip;
2342 int error;
2344 ip = VTOI(ap->a_vp);
2346 error = hammer2_ioctl(ip, ap->a_command, (void *)ap->a_data,
2347 ap->a_fflag, ap->a_cred);
2348 return (error);
2351 static
2353 hammer2_vop_mountctl(struct vop_mountctl_args *ap)
2355 struct mount *mp;
2356 hammer2_pfs_t *pmp;
2357 int rc;
2359 switch (ap->a_op) {
2360 case (MOUNTCTL_SET_EXPORT):
2361 mp = ap->a_head.a_ops->head.vv_mount;
2362 pmp = MPTOPMP(mp);
2364 if (ap->a_ctllen != sizeof(struct export_args))
2365 rc = (EINVAL);
2366 else
2367 rc = vfs_export(mp, &pmp->export,
2368 (const struct export_args *)ap->a_ctl);
2369 break;
2370 default:
2371 rc = vop_stdmountctl(ap);
2372 break;
2374 return (rc);
2378 * KQFILTER
2380 static void filt_hammer2detach(struct knote *kn);
2381 static int filt_hammer2read(struct knote *kn, long hint);
2382 static int filt_hammer2write(struct knote *kn, long hint);
2383 static int filt_hammer2vnode(struct knote *kn, long hint);
2385 static struct filterops hammer2read_filtops =
2386 { FILTEROP_ISFD | FILTEROP_MPSAFE,
2387 NULL, filt_hammer2detach, filt_hammer2read };
2388 static struct filterops hammer2write_filtops =
2389 { FILTEROP_ISFD | FILTEROP_MPSAFE,
2390 NULL, filt_hammer2detach, filt_hammer2write };
2391 static struct filterops hammer2vnode_filtops =
2392 { FILTEROP_ISFD | FILTEROP_MPSAFE,
2393 NULL, filt_hammer2detach, filt_hammer2vnode };
2395 static
2397 hammer2_vop_kqfilter(struct vop_kqfilter_args *ap)
2399 struct vnode *vp = ap->a_vp;
2400 struct knote *kn = ap->a_kn;
2402 switch (kn->kn_filter) {
2403 case EVFILT_READ:
2404 kn->kn_fop = &hammer2read_filtops;
2405 break;
2406 case EVFILT_WRITE:
2407 kn->kn_fop = &hammer2write_filtops;
2408 break;
2409 case EVFILT_VNODE:
2410 kn->kn_fop = &hammer2vnode_filtops;
2411 break;
2412 default:
2413 return (EOPNOTSUPP);
2416 kn->kn_hook = (caddr_t)vp;
2418 knote_insert(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
2420 return(0);
2423 static void
2424 filt_hammer2detach(struct knote *kn)
2426 struct vnode *vp = (void *)kn->kn_hook;
2428 knote_remove(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
2431 static int
2432 filt_hammer2read(struct knote *kn, long hint)
2434 struct vnode *vp = (void *)kn->kn_hook;
2435 hammer2_inode_t *ip = VTOI(vp);
2436 off_t off;
2438 if (hint == NOTE_REVOKE) {
2439 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
2440 return(1);
2442 off = ip->meta.size - kn->kn_fp->f_offset;
2443 kn->kn_data = (off < INTPTR_MAX) ? off : INTPTR_MAX;
2444 if (kn->kn_sfflags & NOTE_OLDAPI)
2445 return(1);
2446 return (kn->kn_data != 0);
2450 static int
2451 filt_hammer2write(struct knote *kn, long hint)
2453 if (hint == NOTE_REVOKE)
2454 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
2455 kn->kn_data = 0;
2456 return (1);
2459 static int
2460 filt_hammer2vnode(struct knote *kn, long hint)
2462 if (kn->kn_sfflags & hint)
2463 kn->kn_fflags |= hint;
2464 if (hint == NOTE_REVOKE) {
2465 kn->kn_flags |= (EV_EOF | EV_NODATA);
2466 return (1);
2468 return (kn->kn_fflags != 0);
2472 * FIFO VOPS
2474 static
2476 hammer2_vop_markatime(struct vop_markatime_args *ap)
2478 hammer2_inode_t *ip;
2479 struct vnode *vp;
2481 vp = ap->a_vp;
2482 ip = VTOI(vp);
2484 if (ip->pmp->ronly || (ip->pmp->flags & HAMMER2_PMPF_EMERG))
2485 return (EROFS);
2486 return(0);
2489 static
2491 hammer2_vop_fifokqfilter(struct vop_kqfilter_args *ap)
2493 int error;
2495 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
2496 if (error)
2497 error = hammer2_vop_kqfilter(ap);
2498 return(error);
2502 * VOPS vector
2504 struct vop_ops hammer2_vnode_vops = {
2505 .vop_default = vop_defaultop,
2506 .vop_fsync = hammer2_vop_fsync,
2507 .vop_getpages = vop_stdgetpages,
2508 .vop_putpages = vop_stdputpages,
2509 .vop_access = hammer2_vop_access,
2510 .vop_advlock = hammer2_vop_advlock,
2511 .vop_close = hammer2_vop_close,
2512 .vop_nlink = hammer2_vop_nlink,
2513 .vop_ncreate = hammer2_vop_ncreate,
2514 .vop_nsymlink = hammer2_vop_nsymlink,
2515 .vop_nremove = hammer2_vop_nremove,
2516 .vop_nrmdir = hammer2_vop_nrmdir,
2517 .vop_nrename = hammer2_vop_nrename,
2518 .vop_getattr = hammer2_vop_getattr,
2519 .vop_getattr_lite = hammer2_vop_getattr_lite,
2520 .vop_setattr = hammer2_vop_setattr,
2521 .vop_readdir = hammer2_vop_readdir,
2522 .vop_readlink = hammer2_vop_readlink,
2523 .vop_read = hammer2_vop_read,
2524 .vop_write = hammer2_vop_write,
2525 .vop_open = hammer2_vop_open,
2526 .vop_inactive = hammer2_vop_inactive,
2527 .vop_reclaim = hammer2_vop_reclaim,
2528 .vop_nresolve = hammer2_vop_nresolve,
2529 .vop_nlookupdotdot = hammer2_vop_nlookupdotdot,
2530 .vop_nmkdir = hammer2_vop_nmkdir,
2531 .vop_nmknod = hammer2_vop_nmknod,
2532 .vop_ioctl = hammer2_vop_ioctl,
2533 .vop_mountctl = hammer2_vop_mountctl,
2534 .vop_bmap = hammer2_vop_bmap,
2535 .vop_strategy = hammer2_vop_strategy,
2536 .vop_kqfilter = hammer2_vop_kqfilter
2539 struct vop_ops hammer2_spec_vops = {
2540 .vop_default = vop_defaultop,
2541 .vop_fsync = hammer2_vop_fsync,
2542 .vop_read = vop_stdnoread,
2543 .vop_write = vop_stdnowrite,
2544 .vop_access = hammer2_vop_access,
2545 .vop_close = hammer2_vop_close,
2546 .vop_markatime = hammer2_vop_markatime,
2547 .vop_getattr = hammer2_vop_getattr,
2548 .vop_inactive = hammer2_vop_inactive,
2549 .vop_reclaim = hammer2_vop_reclaim,
2550 .vop_setattr = hammer2_vop_setattr
2553 struct vop_ops hammer2_fifo_vops = {
2554 .vop_default = fifo_vnoperate,
2555 .vop_fsync = hammer2_vop_fsync,
2556 #if 0
2557 .vop_read = hammer2_vop_fiforead,
2558 .vop_write = hammer2_vop_fifowrite,
2559 #endif
2560 .vop_access = hammer2_vop_access,
2561 #if 0
2562 .vop_close = hammer2_vop_fifoclose,
2563 #endif
2564 .vop_markatime = hammer2_vop_markatime,
2565 .vop_getattr = hammer2_vop_getattr,
2566 .vop_inactive = hammer2_vop_inactive,
2567 .vop_reclaim = hammer2_vop_reclaim,
2568 .vop_setattr = hammer2_vop_setattr,
2569 .vop_kqfilter = hammer2_vop_fifokqfilter