hammer2 - Stabilization, optimization
[dragonfly.git] / sys / vfs / hammer2 / hammer2_vnops.c
1 /*
2  * Copyright (c) 2011-2015 The DragonFly Project.  All rights reserved.
3  *
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) 
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
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.
22  *
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.
35  */
36 /*
37  * Kernel Filesystem interface
38  *
39  * NOTE! local ipdata pointers must be reloaded on any modifying operation
40  *       to the inode as its underlying chain may have changed.
41  */
42
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/namei.h>
50 #include <sys/mount.h>
51 #include <sys/vnode.h>
52 #include <sys/mountctl.h>
53 #include <sys/dirent.h>
54 #include <sys/uio.h>
55 #include <sys/objcache.h>
56 #include <sys/event.h>
57 #include <sys/file.h>
58 #include <vfs/fifofs/fifo.h>
59
60 #include "hammer2.h"
61
62 static int hammer2_read_file(hammer2_inode_t *ip, struct uio *uio,
63                                 int seqcount);
64 static int hammer2_write_file(hammer2_inode_t *ip, struct uio *uio,
65                                 int ioflag, int seqcount);
66 static void hammer2_extend_file(hammer2_inode_t *ip, hammer2_key_t nsize);
67 static void hammer2_truncate_file(hammer2_inode_t *ip, hammer2_key_t nsize);
68
69 struct objcache *cache_xops;
70
71 static __inline
72 void
73 hammer2_knote(struct vnode *vp, int flags)
74 {
75         if (flags)
76                 KNOTE(&vp->v_pollinfo.vpi_kqinfo.ki_note, flags);
77 }
78
79 /*
80  * Last reference to a vnode is going away but it is still cached.
81  */
82 static
83 int
84 hammer2_vop_inactive(struct vop_inactive_args *ap)
85 {
86         hammer2_inode_t *ip;
87         struct vnode *vp;
88
89         LOCKSTART;
90         vp = ap->a_vp;
91         ip = VTOI(vp);
92
93         /*
94          * Degenerate case
95          */
96         if (ip == NULL) {
97                 vrecycle(vp);
98                 LOCKSTOP;
99                 return (0);
100         }
101
102         /*
103          * Check for deleted inodes and recycle immediately on the last
104          * release.  Be sure to destroy any left-over buffer cache buffers
105          * so we do not waste time trying to flush them.
106          *
107          * Note that deleting the file block chains under the inode chain
108          * would just be a waste of energy, so don't do it.
109          *
110          * WARNING: nvtruncbuf() can only be safely called without the inode
111          *          lock held due to the way our write thread works.
112          */
113         if (ip->flags & HAMMER2_INODE_ISUNLINKED) {
114                 hammer2_key_t lbase;
115                 int nblksize;
116
117                 /*
118                  * Detect updates to the embedded data which may be
119                  * synchronized by the strategy code.  Simply mark the
120                  * inode modified so it gets picked up by our normal flush.
121                  */
122                 nblksize = hammer2_calc_logical(ip, 0, &lbase, NULL);
123                 nvtruncbuf(vp, 0, nblksize, 0, 0);
124                 vrecycle(vp);
125         }
126         LOCKSTOP;
127         return (0);
128 }
129
130 /*
131  * Reclaim a vnode so that it can be reused; after the inode is
132  * disassociated, the filesystem must manage it alone.
133  */
134 static
135 int
136 hammer2_vop_reclaim(struct vop_reclaim_args *ap)
137 {
138         hammer2_inode_t *ip;
139         hammer2_pfs_t *pmp;
140         struct vnode *vp;
141
142         LOCKSTART;
143         vp = ap->a_vp;
144         ip = VTOI(vp);
145         if (ip == NULL) {
146                 LOCKSTOP;
147                 return(0);
148         }
149         pmp = ip->pmp;
150
151         /*
152          * The final close of a deleted file or directory marks it for
153          * destruction.  The DELETED flag allows the flusher to shortcut
154          * any modified blocks still unflushed (that is, just ignore them).
155          *
156          * HAMMER2 usually does not try to optimize the freemap by returning
157          * deleted blocks to it as it does not usually know how many snapshots
158          * might be referencing portions of the file/dir.
159          */
160         vp->v_data = NULL;
161         ip->vp = NULL;
162
163         /*
164          * NOTE! We do not attempt to flush chains here, flushing is
165          *       really fragile and could also deadlock.
166          */
167         vclrisdirty(vp);
168
169         /*
170          * An unlinked inode may have been relinked to the ihidden directory.
171          * This occurs if the inode was unlinked while open.  Reclamation of
172          * these inodes requires processing we cannot safely do here so add
173          * the inode to the sideq in that situation.
174          *
175          * A modified inode may require chain synchronization which will no
176          * longer be driven by a sync or fsync without the vnode, also use
177          * the sideq for that.
178          *
179          * A reclaim can occur at any time so we cannot safely start a
180          * transaction to handle reclamation of unlinked files.  Instead,
181          * the ip is left with a reference and placed on a linked list and
182          * handled later on.
183          */
184
185         if ((ip->flags & (HAMMER2_INODE_ISUNLINKED |
186                           HAMMER2_INODE_MODIFIED |
187                           HAMMER2_INODE_RESIZED)) &&
188             (ip->flags & HAMMER2_INODE_ISDELETED) == 0) {
189                 hammer2_inode_sideq_t *ipul;
190
191                 ipul = kmalloc(sizeof(*ipul), pmp->minode, M_WAITOK | M_ZERO);
192                 ipul->ip = ip;
193
194                 hammer2_spin_ex(&pmp->list_spin);
195                 if ((ip->flags & HAMMER2_INODE_ONSIDEQ) == 0) {
196                         /* ref -> sideq */
197                         atomic_set_int(&ip->flags, HAMMER2_INODE_ONSIDEQ);
198                         TAILQ_INSERT_TAIL(&pmp->sideq, ipul, entry);
199                         hammer2_spin_unex(&pmp->list_spin);
200                 } else {
201                         hammer2_spin_unex(&pmp->list_spin);
202                         kfree(ipul, pmp->minode);
203                         hammer2_inode_drop(ip);         /* vp ref */
204                 }
205                 /* retain ref from vp for ipul */
206         } else {
207                 hammer2_inode_drop(ip);                 /* vp ref */
208         }
209
210         /*
211          * XXX handle background sync when ip dirty, kernel will no longer
212          * notify us regarding this inode because there is no longer a
213          * vnode attached to it.
214          */
215
216         LOCKSTOP;
217         return (0);
218 }
219
220 static
221 int
222 hammer2_vop_fsync(struct vop_fsync_args *ap)
223 {
224         hammer2_inode_t *ip;
225         struct vnode *vp;
226
227         LOCKSTART;
228         vp = ap->a_vp;
229         ip = VTOI(vp);
230
231 #if 0
232         /* XXX can't do this yet */
233         hammer2_trans_init(ip->pmp, HAMMER2_TRANS_ISFLUSH);
234         vfsync(vp, ap->a_waitfor, 1, NULL, NULL);
235 #endif
236         hammer2_trans_init(ip->pmp, 0);
237         vfsync(vp, ap->a_waitfor, 1, NULL, NULL);
238
239         /*
240          * Calling chain_flush here creates a lot of duplicative
241          * COW operations due to non-optimal vnode ordering.
242          *
243          * Only do it for an actual fsync() syscall.  The other forms
244          * which call this function will eventually call chain_flush
245          * on the volume root as a catch-all, which is far more optimal.
246          */
247         hammer2_inode_lock(ip, 0);
248         if (ip->flags & HAMMER2_INODE_MODIFIED)
249                 hammer2_inode_chain_sync(ip);
250         hammer2_inode_unlock(ip);
251         hammer2_trans_done(ip->pmp);
252
253         LOCKSTOP;
254         return (0);
255 }
256
257 static
258 int
259 hammer2_vop_access(struct vop_access_args *ap)
260 {
261         hammer2_inode_t *ip = VTOI(ap->a_vp);
262         uid_t uid;
263         gid_t gid;
264         int error;
265
266         LOCKSTART;
267         hammer2_inode_lock(ip, HAMMER2_RESOLVE_SHARED);
268         uid = hammer2_to_unix_xid(&ip->meta.uid);
269         gid = hammer2_to_unix_xid(&ip->meta.gid);
270         error = vop_helper_access(ap, uid, gid, ip->meta.mode, ip->meta.uflags);
271         hammer2_inode_unlock(ip);
272
273         LOCKSTOP;
274         return (error);
275 }
276
277 static
278 int
279 hammer2_vop_getattr(struct vop_getattr_args *ap)
280 {
281         hammer2_pfs_t *pmp;
282         hammer2_inode_t *ip;
283         struct vnode *vp;
284         struct vattr *vap;
285         hammer2_chain_t *chain;
286         int i;
287
288         LOCKSTART;
289         vp = ap->a_vp;
290         vap = ap->a_vap;
291
292         ip = VTOI(vp);
293         pmp = ip->pmp;
294
295         hammer2_inode_lock(ip, HAMMER2_RESOLVE_SHARED);
296
297         vap->va_fsid = pmp->mp->mnt_stat.f_fsid.val[0];
298         vap->va_fileid = ip->meta.inum;
299         vap->va_mode = ip->meta.mode;
300         vap->va_nlink = ip->meta.nlinks;
301         vap->va_uid = hammer2_to_unix_xid(&ip->meta.uid);
302         vap->va_gid = hammer2_to_unix_xid(&ip->meta.gid);
303         vap->va_rmajor = 0;
304         vap->va_rminor = 0;
305         vap->va_size = ip->meta.size;   /* protected by shared lock */
306         vap->va_blocksize = HAMMER2_PBUFSIZE;
307         vap->va_flags = ip->meta.uflags;
308         hammer2_time_to_timespec(ip->meta.ctime, &vap->va_ctime);
309         hammer2_time_to_timespec(ip->meta.mtime, &vap->va_mtime);
310         hammer2_time_to_timespec(ip->meta.mtime, &vap->va_atime);
311         vap->va_gen = 1;
312         vap->va_bytes = 0;
313         if (ip->meta.type == HAMMER2_OBJTYPE_DIRECTORY) {
314                 /*
315                  * Can't really calculate directory use sans the files under
316                  * it, just assume one block for now.
317                  */
318                 vap->va_bytes += HAMMER2_INODE_BYTES;
319         } else {
320                 for (i = 0; i < ip->cluster.nchains; ++i) {
321                         if ((chain = ip->cluster.array[i].chain) != NULL) {
322                                 if (vap->va_bytes < chain->bref.data_count)
323                                         vap->va_bytes = chain->bref.data_count;
324                         }
325                 }
326         }
327         vap->va_type = hammer2_get_vtype(ip->meta.type);
328         vap->va_filerev = 0;
329         vap->va_uid_uuid = ip->meta.uid;
330         vap->va_gid_uuid = ip->meta.gid;
331         vap->va_vaflags = VA_UID_UUID_VALID | VA_GID_UUID_VALID |
332                           VA_FSID_UUID_VALID;
333
334         hammer2_inode_unlock(ip);
335
336         LOCKSTOP;
337         return (0);
338 }
339
340 static
341 int
342 hammer2_vop_setattr(struct vop_setattr_args *ap)
343 {
344         hammer2_inode_t *ip;
345         struct vnode *vp;
346         struct vattr *vap;
347         int error;
348         int kflags = 0;
349         uint64_t ctime;
350
351         LOCKSTART;
352         vp = ap->a_vp;
353         vap = ap->a_vap;
354         hammer2_update_time(&ctime);
355
356         ip = VTOI(vp);
357
358         if (ip->pmp->ronly) {
359                 LOCKSTOP;
360                 return(EROFS);
361         }
362
363         hammer2_pfs_memory_wait(ip->pmp);
364         hammer2_trans_init(ip->pmp, 0);
365         hammer2_inode_lock(ip, 0);
366         error = 0;
367
368         if (vap->va_flags != VNOVAL) {
369                 uint32_t flags;
370
371                 flags = ip->meta.uflags;
372                 error = vop_helper_setattr_flags(&flags, vap->va_flags,
373                                      hammer2_to_unix_xid(&ip->meta.uid),
374                                      ap->a_cred);
375                 if (error == 0) {
376                         if (ip->meta.uflags != flags) {
377                                 hammer2_inode_modify(ip);
378                                 ip->meta.uflags = flags;
379                                 ip->meta.ctime = ctime;
380                                 kflags |= NOTE_ATTRIB;
381                         }
382                         if (ip->meta.uflags & (IMMUTABLE | APPEND)) {
383                                 error = 0;
384                                 goto done;
385                         }
386                 }
387                 goto done;
388         }
389         if (ip->meta.uflags & (IMMUTABLE | APPEND)) {
390                 error = EPERM;
391                 goto done;
392         }
393         if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
394                 mode_t cur_mode = ip->meta.mode;
395                 uid_t cur_uid = hammer2_to_unix_xid(&ip->meta.uid);
396                 gid_t cur_gid = hammer2_to_unix_xid(&ip->meta.gid);
397                 uuid_t uuid_uid;
398                 uuid_t uuid_gid;
399
400                 error = vop_helper_chown(ap->a_vp, vap->va_uid, vap->va_gid,
401                                          ap->a_cred,
402                                          &cur_uid, &cur_gid, &cur_mode);
403                 if (error == 0) {
404                         hammer2_guid_to_uuid(&uuid_uid, cur_uid);
405                         hammer2_guid_to_uuid(&uuid_gid, cur_gid);
406                         if (bcmp(&uuid_uid, &ip->meta.uid, sizeof(uuid_uid)) ||
407                             bcmp(&uuid_gid, &ip->meta.gid, sizeof(uuid_gid)) ||
408                             ip->meta.mode != cur_mode
409                         ) {
410                                 hammer2_inode_modify(ip);
411                                 ip->meta.uid = uuid_uid;
412                                 ip->meta.gid = uuid_gid;
413                                 ip->meta.mode = cur_mode;
414                                 ip->meta.ctime = ctime;
415                         }
416                         kflags |= NOTE_ATTRIB;
417                 }
418         }
419
420         /*
421          * Resize the file
422          */
423         if (vap->va_size != VNOVAL && ip->meta.size != vap->va_size) {
424                 switch(vp->v_type) {
425                 case VREG:
426                         if (vap->va_size == ip->meta.size)
427                                 break;
428                         if (vap->va_size < ip->meta.size) {
429                                 hammer2_mtx_ex(&ip->truncate_lock);
430                                 hammer2_truncate_file(ip, vap->va_size);
431                                 hammer2_mtx_unlock(&ip->truncate_lock);
432                         } else {
433                                 hammer2_extend_file(ip, vap->va_size);
434                         }
435                         hammer2_inode_modify(ip);
436                         ip->meta.mtime = ctime;
437                         break;
438                 default:
439                         error = EINVAL;
440                         goto done;
441                 }
442         }
443 #if 0
444         /* atime not supported */
445         if (vap->va_atime.tv_sec != VNOVAL) {
446                 hammer2_inode_modify(ip);
447                 ip->meta.atime = hammer2_timespec_to_time(&vap->va_atime);
448                 kflags |= NOTE_ATTRIB;
449         }
450 #endif
451         if (vap->va_mode != (mode_t)VNOVAL) {
452                 mode_t cur_mode = ip->meta.mode;
453                 uid_t cur_uid = hammer2_to_unix_xid(&ip->meta.uid);
454                 gid_t cur_gid = hammer2_to_unix_xid(&ip->meta.gid);
455
456                 error = vop_helper_chmod(ap->a_vp, vap->va_mode, ap->a_cred,
457                                          cur_uid, cur_gid, &cur_mode);
458                 if (error == 0 && ip->meta.mode != cur_mode) {
459                         hammer2_inode_modify(ip);
460                         ip->meta.mode = cur_mode;
461                         ip->meta.ctime = ctime;
462                         kflags |= NOTE_ATTRIB;
463                 }
464         }
465
466         if (vap->va_mtime.tv_sec != VNOVAL) {
467                 hammer2_inode_modify(ip);
468                 ip->meta.mtime = hammer2_timespec_to_time(&vap->va_mtime);
469                 kflags |= NOTE_ATTRIB;
470         }
471
472 done:
473         /*
474          * If a truncation occurred we must call inode_fsync() now in order
475          * to trim the related data chains, otherwise a later expansion can
476          * cause havoc.
477          *
478          * If an extend occured that changed the DIRECTDATA state, we must
479          * call inode_fsync now in order to prepare the inode's indirect
480          * block table.
481          */
482         if (ip->flags & HAMMER2_INODE_RESIZED)
483                 hammer2_inode_chain_sync(ip);
484
485         /*
486          * Cleanup.
487          */
488         hammer2_inode_unlock(ip);
489         hammer2_trans_done(ip->pmp);
490         hammer2_knote(ip->vp, kflags);
491
492         LOCKSTOP;
493         return (error);
494 }
495
496 static
497 int
498 hammer2_vop_readdir(struct vop_readdir_args *ap)
499 {
500         hammer2_xop_readdir_t *xop;
501         hammer2_blockref_t bref;
502         hammer2_inode_t *ip;
503         hammer2_tid_t inum;
504         hammer2_key_t lkey;
505         struct uio *uio;
506         off_t *cookies;
507         off_t saveoff;
508         int cookie_index;
509         int ncookies;
510         int error;
511         int eofflag;
512         int dtype;
513         int r;
514
515         LOCKSTART;
516         ip = VTOI(ap->a_vp);
517         uio = ap->a_uio;
518         saveoff = uio->uio_offset;
519         eofflag = 0;
520         error = 0;
521
522         /*
523          * Setup cookies directory entry cookies if requested
524          */
525         if (ap->a_ncookies) {
526                 ncookies = uio->uio_resid / 16 + 1;
527                 if (ncookies > 1024)
528                         ncookies = 1024;
529                 cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
530         } else {
531                 ncookies = -1;
532                 cookies = NULL;
533         }
534         cookie_index = 0;
535
536         hammer2_inode_lock(ip, HAMMER2_RESOLVE_SHARED);
537
538         /*
539          * Handle artificial entries.  To ensure that only positive 64 bit
540          * quantities are returned to userland we always strip off bit 63.
541          * The hash code is designed such that codes 0x0000-0x7FFF are not
542          * used, allowing us to use these codes for articial entries.
543          *
544          * Entry 0 is used for '.' and entry 1 is used for '..'.  Do not
545          * allow '..' to cross the mount point into (e.g.) the super-root.
546          */
547         if (saveoff == 0) {
548                 inum = ip->meta.inum & HAMMER2_DIRHASH_USERMSK;
549                 r = vop_write_dirent(&error, uio, inum, DT_DIR, 1, ".");
550                 if (r)
551                         goto done;
552                 if (cookies)
553                         cookies[cookie_index] = saveoff;
554                 ++saveoff;
555                 ++cookie_index;
556                 if (cookie_index == ncookies)
557                         goto done;
558         }
559
560         if (saveoff == 1) {
561                 /*
562                  * Be careful with lockorder when accessing ".."
563                  *
564                  * (ip is the current dir. xip is the parent dir).
565                  */
566                 inum = ip->meta.inum & HAMMER2_DIRHASH_USERMSK;
567                 if (ip != ip->pmp->iroot)
568                         inum = ip->meta.iparent & HAMMER2_DIRHASH_USERMSK;
569                 r = vop_write_dirent(&error, uio, inum, DT_DIR, 2, "..");
570                 if (r)
571                         goto done;
572                 if (cookies)
573                         cookies[cookie_index] = saveoff;
574                 ++saveoff;
575                 ++cookie_index;
576                 if (cookie_index == ncookies)
577                         goto done;
578         }
579
580         lkey = saveoff | HAMMER2_DIRHASH_VISIBLE;
581         if (hammer2_debug & 0x0020)
582                 kprintf("readdir: lkey %016jx\n", lkey);
583         if (error)
584                 goto done;
585
586         /*
587          * Use XOP for cluster scan.
588          *
589          * parent is the inode cluster, already locked for us.  Don't
590          * double lock shared locks as this will screw up upgrades.
591          */
592         xop = hammer2_xop_alloc(ip, 0);
593         xop->lkey = lkey;
594         hammer2_xop_start(&xop->head, hammer2_xop_readdir);
595
596         for (;;) {
597                 const hammer2_inode_data_t *ripdata;
598
599                 error = hammer2_xop_collect(&xop->head, 0);
600                 if (error)
601                         break;
602                 if (cookie_index == ncookies)
603                         break;
604                 if (hammer2_debug & 0x0020)
605                 kprintf("cluster chain %p %p\n",
606                         xop->head.cluster.focus,
607                         (xop->head.cluster.focus ?
608                          xop->head.cluster.focus->data : (void *)-1));
609                 ripdata = &hammer2_cluster_rdata(&xop->head.cluster)->ipdata;
610                 hammer2_cluster_bref(&xop->head.cluster, &bref);
611                 if (bref.type == HAMMER2_BREF_TYPE_INODE) {
612                         dtype = hammer2_get_dtype(ripdata);
613                         saveoff = bref.key & HAMMER2_DIRHASH_USERMSK;
614                         r = vop_write_dirent(&error, uio,
615                                              ripdata->meta.inum &
616                                               HAMMER2_DIRHASH_USERMSK,
617                                              dtype,
618                                              ripdata->meta.name_len,
619                                              ripdata->filename);
620                         if (r)
621                                 break;
622                         if (cookies)
623                                 cookies[cookie_index] = saveoff;
624                         ++cookie_index;
625                 } else {
626                         /* XXX chain error */
627                         kprintf("bad chain type readdir %d\n", bref.type);
628                 }
629         }
630         hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
631         if (error == ENOENT) {
632                 error = 0;
633                 eofflag = 1;
634                 saveoff = (hammer2_key_t)-1;
635         } else {
636                 saveoff = bref.key & HAMMER2_DIRHASH_USERMSK;
637         }
638 done:
639         hammer2_inode_unlock(ip);
640         if (ap->a_eofflag)
641                 *ap->a_eofflag = eofflag;
642         if (hammer2_debug & 0x0020)
643                 kprintf("readdir: done at %016jx\n", saveoff);
644         uio->uio_offset = saveoff & ~HAMMER2_DIRHASH_VISIBLE;
645         if (error && cookie_index == 0) {
646                 if (cookies) {
647                         kfree(cookies, M_TEMP);
648                         *ap->a_ncookies = 0;
649                         *ap->a_cookies = NULL;
650                 }
651         } else {
652                 if (cookies) {
653                         *ap->a_ncookies = cookie_index;
654                         *ap->a_cookies = cookies;
655                 }
656         }
657         LOCKSTOP;
658         return (error);
659 }
660
661 /*
662  * hammer2_vop_readlink { vp, uio, cred }
663  */
664 static
665 int
666 hammer2_vop_readlink(struct vop_readlink_args *ap)
667 {
668         struct vnode *vp;
669         hammer2_inode_t *ip;
670         int error;
671
672         vp = ap->a_vp;
673         if (vp->v_type != VLNK)
674                 return (EINVAL);
675         ip = VTOI(vp);
676
677         error = hammer2_read_file(ip, ap->a_uio, 0);
678         return (error);
679 }
680
681 static
682 int
683 hammer2_vop_read(struct vop_read_args *ap)
684 {
685         struct vnode *vp;
686         hammer2_inode_t *ip;
687         struct uio *uio;
688         int error;
689         int seqcount;
690         int bigread;
691
692         /*
693          * Read operations supported on this vnode?
694          */
695         vp = ap->a_vp;
696         if (vp->v_type != VREG)
697                 return (EINVAL);
698
699         /*
700          * Misc
701          */
702         ip = VTOI(vp);
703         uio = ap->a_uio;
704         error = 0;
705
706         seqcount = ap->a_ioflag >> 16;
707         bigread = (uio->uio_resid > 100 * 1024 * 1024);
708
709         error = hammer2_read_file(ip, uio, seqcount);
710         return (error);
711 }
712
713 static
714 int
715 hammer2_vop_write(struct vop_write_args *ap)
716 {
717         hammer2_inode_t *ip;
718         thread_t td;
719         struct vnode *vp;
720         struct uio *uio;
721         int error;
722         int seqcount;
723
724         /*
725          * Read operations supported on this vnode?
726          */
727         vp = ap->a_vp;
728         if (vp->v_type != VREG)
729                 return (EINVAL);
730
731         /*
732          * Misc
733          */
734         ip = VTOI(vp);
735         uio = ap->a_uio;
736         error = 0;
737         if (ip->pmp->ronly) {
738                 return (EROFS);
739         }
740
741         seqcount = ap->a_ioflag >> 16;
742
743         /*
744          * Check resource limit
745          */
746         if (uio->uio_resid > 0 && (td = uio->uio_td) != NULL && td->td_proc &&
747             uio->uio_offset + uio->uio_resid >
748              td->td_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
749                 lwpsignal(td->td_proc, td->td_lwp, SIGXFSZ);
750                 return (EFBIG);
751         }
752
753         /*
754          * The transaction interlocks against flushes initiations
755          * (note: but will run concurrently with the actual flush).
756          */
757         hammer2_trans_init(ip->pmp, 0);
758         error = hammer2_write_file(ip, uio, ap->a_ioflag, seqcount);
759         hammer2_trans_done(ip->pmp);
760
761         return (error);
762 }
763
764 /*
765  * Perform read operations on a file or symlink given an UNLOCKED
766  * inode and uio.
767  *
768  * The passed ip is not locked.
769  */
770 static
771 int
772 hammer2_read_file(hammer2_inode_t *ip, struct uio *uio, int seqcount)
773 {
774         hammer2_off_t size;
775         struct buf *bp;
776         int error;
777
778         error = 0;
779
780         /*
781          * UIO read loop.
782          *
783          * WARNING! Assumes that the kernel interlocks size changes at the
784          *          vnode level.
785          */
786         hammer2_mtx_sh(&ip->lock);
787         hammer2_mtx_sh(&ip->truncate_lock);
788         size = ip->meta.size;
789         hammer2_mtx_unlock(&ip->lock);
790
791         while (uio->uio_resid > 0 && uio->uio_offset < size) {
792                 hammer2_key_t lbase;
793                 hammer2_key_t leof;
794                 int lblksize;
795                 int loff;
796                 int n;
797
798                 lblksize = hammer2_calc_logical(ip, uio->uio_offset,
799                                                 &lbase, &leof);
800
801 #if 1
802                 error = cluster_read(ip->vp, leof, lbase, lblksize,
803                                      uio->uio_resid, seqcount * BKVASIZE,
804                                      &bp);
805 #else
806                 if (uio->uio_segflg == UIO_NOCOPY) {
807                         bp = getblk(ip->vp, lbase, lblksize, GETBLK_BHEAVY, 0);
808                         if (bp->b_flags & B_CACHE) {
809                                 int i;
810                                 int j = 0;
811                                 if (bp->b_xio.xio_npages != 16)
812                                         kprintf("NPAGES BAD\n");
813                                 for (i = 0; i < bp->b_xio.xio_npages; ++i) {
814                                         vm_page_t m;
815                                         m = bp->b_xio.xio_pages[i];
816                                         if (m == NULL || m->valid == 0) {
817                                                 kprintf("bp %016jx %016jx pg %d inv",
818                                                         lbase, leof, i);
819                                                 if (m)
820                                                         kprintf("m->object %p/%p", m->object, ip->vp->v_object);
821                                                 kprintf("\n");
822                                                 j = 1;
823                                         }
824                                 }
825                                 if (j)
826                                         kprintf("b_flags %08x, b_error %d\n", bp->b_flags, bp->b_error);
827                         }
828                         bqrelse(bp);
829                 }
830                 error = bread(ip->vp, lbase, lblksize, &bp);
831 #endif
832                 if (error) {
833                         brelse(bp);
834                         break;
835                 }
836                 loff = (int)(uio->uio_offset - lbase);
837                 n = lblksize - loff;
838                 if (n > uio->uio_resid)
839                         n = uio->uio_resid;
840                 if (n > size - uio->uio_offset)
841                         n = (int)(size - uio->uio_offset);
842                 bp->b_flags |= B_AGE;
843                 uiomovebp(bp, (char *)bp->b_data + loff, n, uio);
844                 bqrelse(bp);
845         }
846         hammer2_mtx_unlock(&ip->truncate_lock);
847
848         return (error);
849 }
850
851 /*
852  * Write to the file represented by the inode via the logical buffer cache.
853  * The inode may represent a regular file or a symlink.
854  *
855  * The inode must not be locked.
856  */
857 static
858 int
859 hammer2_write_file(hammer2_inode_t *ip, struct uio *uio,
860                    int ioflag, int seqcount)
861 {
862         hammer2_key_t old_eof;
863         hammer2_key_t new_eof;
864         struct buf *bp;
865         int kflags;
866         int error;
867         int modified;
868
869         /*
870          * Setup if append
871          *
872          * WARNING! Assumes that the kernel interlocks size changes at the
873          *          vnode level.
874          */
875         hammer2_mtx_ex(&ip->lock);
876         hammer2_mtx_sh(&ip->truncate_lock);
877         if (ioflag & IO_APPEND)
878                 uio->uio_offset = ip->meta.size;
879         old_eof = ip->meta.size;
880
881         /*
882          * Extend the file if necessary.  If the write fails at some point
883          * we will truncate it back down to cover as much as we were able
884          * to write.
885          *
886          * Doing this now makes it easier to calculate buffer sizes in
887          * the loop.
888          */
889         kflags = 0;
890         error = 0;
891         modified = 0;
892
893         if (uio->uio_offset + uio->uio_resid > old_eof) {
894                 new_eof = uio->uio_offset + uio->uio_resid;
895                 modified = 1;
896                 hammer2_extend_file(ip, new_eof);
897                 kflags |= NOTE_EXTEND;
898         } else {
899                 new_eof = old_eof;
900         }
901         hammer2_mtx_unlock(&ip->lock);
902         
903         /*
904          * UIO write loop
905          */
906         while (uio->uio_resid > 0) {
907                 hammer2_key_t lbase;
908                 int trivial;
909                 int endofblk;
910                 int lblksize;
911                 int loff;
912                 int n;
913
914                 /*
915                  * Don't allow the buffer build to blow out the buffer
916                  * cache.
917                  */
918                 if ((ioflag & IO_RECURSE) == 0)
919                         bwillwrite(HAMMER2_PBUFSIZE);
920
921                 /*
922                  * This nominally tells us how much we can cluster and
923                  * what the logical buffer size needs to be.  Currently
924                  * we don't try to cluster the write and just handle one
925                  * block at a time.
926                  */
927                 lblksize = hammer2_calc_logical(ip, uio->uio_offset,
928                                                 &lbase, NULL);
929                 loff = (int)(uio->uio_offset - lbase);
930                 
931                 KKASSERT(lblksize <= 65536);
932
933                 /*
934                  * Calculate bytes to copy this transfer and whether the
935                  * copy completely covers the buffer or not.
936                  */
937                 trivial = 0;
938                 n = lblksize - loff;
939                 if (n > uio->uio_resid) {
940                         n = uio->uio_resid;
941                         if (loff == lbase && uio->uio_offset + n == new_eof)
942                                 trivial = 1;
943                         endofblk = 0;
944                 } else {
945                         if (loff == 0)
946                                 trivial = 1;
947                         endofblk = 1;
948                 }
949                 if (lbase >= new_eof)
950                         trivial = 1;
951
952                 /*
953                  * Get the buffer
954                  */
955                 if (uio->uio_segflg == UIO_NOCOPY) {
956                         /*
957                          * Issuing a write with the same data backing the
958                          * buffer.  Instantiate the buffer to collect the
959                          * backing vm pages, then read-in any missing bits.
960                          *
961                          * This case is used by vop_stdputpages().
962                          */
963                         bp = getblk(ip->vp, lbase, lblksize, GETBLK_BHEAVY, 0);
964                         if ((bp->b_flags & B_CACHE) == 0) {
965                                 bqrelse(bp);
966                                 error = bread(ip->vp, lbase, lblksize, &bp);
967                         }
968                 } else if (trivial) {
969                         /*
970                          * Even though we are entirely overwriting the buffer
971                          * we may still have to zero it out to avoid a
972                          * mmap/write visibility issue.
973                          */
974                         bp = getblk(ip->vp, lbase, lblksize, GETBLK_BHEAVY, 0);
975                         if ((bp->b_flags & B_CACHE) == 0)
976                                 vfs_bio_clrbuf(bp);
977                 } else {
978                         /*
979                          * Partial overwrite, read in any missing bits then
980                          * replace the portion being written.
981                          *
982                          * (The strategy code will detect zero-fill physical
983                          * blocks for this case).
984                          */
985                         error = bread(ip->vp, lbase, lblksize, &bp);
986                         if (error == 0)
987                                 bheavy(bp);
988                 }
989
990                 if (error) {
991                         brelse(bp);
992                         break;
993                 }
994
995                 /*
996                  * Ok, copy the data in
997                  */
998                 error = uiomovebp(bp, bp->b_data + loff, n, uio);
999                 kflags |= NOTE_WRITE;
1000                 modified = 1;
1001                 if (error) {
1002                         brelse(bp);
1003                         break;
1004                 }
1005
1006                 /*
1007                  * WARNING: Pageout daemon will issue UIO_NOCOPY writes
1008                  *          with IO_SYNC or IO_ASYNC set.  These writes
1009                  *          must be handled as the pageout daemon expects.
1010                  *
1011                  * NOTE!    H2 relies on cluster_write() here because it
1012                  *          cannot preallocate disk blocks at the logical
1013                  *          level due to not knowing what the compression
1014                  *          size will be at this time.
1015                  *
1016                  *          We must use cluster_write() here and we depend
1017                  *          on the write-behind feature to flush buffers
1018                  *          appropriately.  If we let the buffer daemons do
1019                  *          it the block allocations will be all over the
1020                  *          map.
1021                  */
1022                 if (ioflag & IO_SYNC) {
1023                         bwrite(bp);
1024                 } else if ((ioflag & IO_DIRECT) && endofblk) {
1025                         bawrite(bp);
1026                 } else if (ioflag & IO_ASYNC) {
1027                         bawrite(bp);
1028                 } else if (ip->vp->v_mount->mnt_flag & MNT_NOCLUSTERW) {
1029                         bdwrite(bp);
1030                 } else {
1031 #if 1
1032                         bp->b_flags |= B_CLUSTEROK;
1033                         cluster_write(bp, new_eof, lblksize, seqcount);
1034 #else
1035                         bp->b_flags |= B_CLUSTEROK;
1036                         bdwrite(bp);
1037 #endif
1038                 }
1039         }
1040
1041         /*
1042          * Cleanup.  If we extended the file EOF but failed to write through
1043          * the entire write is a failure and we have to back-up.
1044          */
1045         if (error && new_eof != old_eof) {
1046                 hammer2_mtx_unlock(&ip->truncate_lock);
1047                 hammer2_mtx_ex(&ip->lock);
1048                 hammer2_mtx_ex(&ip->truncate_lock);
1049                 hammer2_truncate_file(ip, old_eof);
1050                 if (ip->flags & HAMMER2_INODE_MODIFIED)
1051                         hammer2_inode_chain_sync(ip);
1052                 hammer2_mtx_unlock(&ip->lock);
1053         } else if (modified) {
1054                 hammer2_mtx_ex(&ip->lock);
1055                 hammer2_inode_modify(ip);
1056                 hammer2_update_time(&ip->meta.mtime);
1057                 if (ip->flags & HAMMER2_INODE_MODIFIED)
1058                         hammer2_inode_chain_sync(ip);
1059                 hammer2_mtx_unlock(&ip->lock);
1060                 hammer2_knote(ip->vp, kflags);
1061         }
1062         hammer2_trans_assert_strategy(ip->pmp);
1063         hammer2_mtx_unlock(&ip->truncate_lock);
1064
1065         return error;
1066 }
1067
1068 /*
1069  * Truncate the size of a file.  The inode must not be locked.
1070  *
1071  * We must unconditionally set HAMMER2_INODE_RESIZED to properly
1072  * ensure that any on-media data beyond the new file EOF has been destroyed.
1073  *
1074  * WARNING: nvtruncbuf() can only be safely called without the inode lock
1075  *          held due to the way our write thread works.  If the truncation
1076  *          occurs in the middle of a buffer, nvtruncbuf() is responsible
1077  *          for dirtying that buffer and zeroing out trailing bytes.
1078  *
1079  * WARNING! Assumes that the kernel interlocks size changes at the
1080  *          vnode level.
1081  *
1082  * WARNING! Caller assumes responsibility for removing dead blocks
1083  *          if INODE_RESIZED is set.
1084  */
1085 static
1086 void
1087 hammer2_truncate_file(hammer2_inode_t *ip, hammer2_key_t nsize)
1088 {
1089         hammer2_key_t lbase;
1090         int nblksize;
1091
1092         LOCKSTART;
1093         hammer2_mtx_unlock(&ip->lock);
1094         if (ip->vp) {
1095                 nblksize = hammer2_calc_logical(ip, nsize, &lbase, NULL);
1096                 nvtruncbuf(ip->vp, nsize,
1097                            nblksize, (int)nsize & (nblksize - 1),
1098                            0);
1099         }
1100         hammer2_mtx_ex(&ip->lock);
1101         KKASSERT((ip->flags & HAMMER2_INODE_RESIZED) == 0);
1102         ip->osize = ip->meta.size;
1103         ip->meta.size = nsize;
1104         atomic_set_int(&ip->flags, HAMMER2_INODE_RESIZED);
1105         hammer2_inode_modify(ip);
1106         LOCKSTOP;
1107 }
1108
1109 /*
1110  * Extend the size of a file.  The inode must not be locked.
1111  *
1112  * Even though the file size is changing, we do not have to set the
1113  * INODE_RESIZED bit unless the file size crosses the EMBEDDED_BYTES
1114  * boundary.  When this occurs a hammer2_inode_chain_sync() is required
1115  * to prepare the inode cluster's indirect block table, otherwise
1116  * async execution of the strategy code will implode on us.
1117  *
1118  * WARNING! Assumes that the kernel interlocks size changes at the
1119  *          vnode level.
1120  *
1121  * WARNING! Caller assumes responsibility for transitioning out
1122  *          of the inode DIRECTDATA mode if INODE_RESIZED is set.
1123  */
1124 static
1125 void
1126 hammer2_extend_file(hammer2_inode_t *ip, hammer2_key_t nsize)
1127 {
1128         hammer2_key_t lbase;
1129         hammer2_key_t osize;
1130         int oblksize;
1131         int nblksize;
1132
1133         LOCKSTART;
1134
1135         KKASSERT((ip->flags & HAMMER2_INODE_RESIZED) == 0);
1136         hammer2_inode_modify(ip);
1137         osize = ip->meta.size;
1138         ip->osize = osize;
1139         ip->meta.size = nsize;
1140
1141         if (osize <= HAMMER2_EMBEDDED_BYTES && nsize > HAMMER2_EMBEDDED_BYTES) {
1142                 atomic_set_int(&ip->flags, HAMMER2_INODE_RESIZED);
1143                 hammer2_inode_chain_sync(ip);
1144         }
1145
1146         hammer2_mtx_unlock(&ip->lock);
1147         if (ip->vp) {
1148                 oblksize = hammer2_calc_logical(ip, osize, &lbase, NULL);
1149                 nblksize = hammer2_calc_logical(ip, nsize, &lbase, NULL);
1150                 nvextendbuf(ip->vp,
1151                             osize, nsize,
1152                             oblksize, nblksize,
1153                             -1, -1, 0);
1154         }
1155         hammer2_mtx_ex(&ip->lock);
1156
1157         LOCKSTOP;
1158 }
1159
1160 static
1161 int
1162 hammer2_vop_nresolve(struct vop_nresolve_args *ap)
1163 {
1164         hammer2_xop_nresolve_t *xop;
1165         hammer2_inode_t *ip;
1166         hammer2_inode_t *dip;
1167         struct namecache *ncp;
1168         struct vnode *vp;
1169         int error;
1170
1171         LOCKSTART;
1172         dip = VTOI(ap->a_dvp);
1173         xop = hammer2_xop_alloc(dip, 0);
1174
1175         ncp = ap->a_nch->ncp;
1176         hammer2_xop_setname(&xop->head, ncp->nc_name, ncp->nc_nlen);
1177
1178         /*
1179          * Note: In DragonFly the kernel handles '.' and '..'.
1180          */
1181         hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);
1182         hammer2_xop_start(&xop->head, hammer2_xop_nresolve);
1183
1184         error = hammer2_xop_collect(&xop->head, 0);
1185         if (error) {
1186                 ip = NULL;
1187         } else {
1188                 ip = hammer2_inode_get(dip->pmp, dip, &xop->head.cluster, -1);
1189         }
1190         hammer2_inode_unlock(dip);
1191
1192         /*
1193          * Acquire the related vnode
1194          *
1195          * NOTE: For error processing, only ENOENT resolves the namecache
1196          *       entry to NULL, otherwise we just return the error and
1197          *       leave the namecache unresolved.
1198          *
1199          * NOTE: multiple hammer2_inode structures can be aliased to the
1200          *       same chain element, for example for hardlinks.  This
1201          *       use case does not 'reattach' inode associations that
1202          *       might already exist, but always allocates a new one.
1203          *
1204          * WARNING: inode structure is locked exclusively via inode_get
1205          *          but chain was locked shared.  inode_unlock()
1206          *          will handle it properly.
1207          */
1208         if (ip) {
1209                 vp = hammer2_igetv(ip, &error);
1210                 if (error == 0) {
1211                         vn_unlock(vp);
1212                         cache_setvp(ap->a_nch, vp);
1213                 } else if (error == ENOENT) {
1214                         cache_setvp(ap->a_nch, NULL);
1215                 }
1216                 hammer2_inode_unlock(ip);
1217
1218                 /*
1219                  * The vp should not be released until after we've disposed
1220                  * of our locks, because it might cause vop_inactive() to
1221                  * be called.
1222                  */
1223                 if (vp)
1224                         vrele(vp);
1225         } else {
1226                 error = ENOENT;
1227                 cache_setvp(ap->a_nch, NULL);
1228         }
1229         hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
1230         KASSERT(error || ap->a_nch->ncp->nc_vp != NULL,
1231                 ("resolve error %d/%p ap %p\n",
1232                  error, ap->a_nch->ncp->nc_vp, ap));
1233         LOCKSTOP;
1234
1235         return error;
1236 }
1237
1238 static
1239 int
1240 hammer2_vop_nlookupdotdot(struct vop_nlookupdotdot_args *ap)
1241 {
1242         hammer2_inode_t *dip;
1243         hammer2_tid_t inum;
1244         int error;
1245
1246         LOCKSTART;
1247         dip = VTOI(ap->a_dvp);
1248         inum = dip->meta.iparent;
1249         *ap->a_vpp = NULL;
1250
1251         if (inum) {
1252                 error = hammer2_vfs_vget(ap->a_dvp->v_mount, NULL,
1253                                          inum, ap->a_vpp);
1254         } else {
1255                 error = ENOENT;
1256         }
1257         LOCKSTOP;
1258         return error;
1259 }
1260
1261 static
1262 int
1263 hammer2_vop_nmkdir(struct vop_nmkdir_args *ap)
1264 {
1265         hammer2_inode_t *dip;
1266         hammer2_inode_t *nip;
1267         struct namecache *ncp;
1268         const uint8_t *name;
1269         size_t name_len;
1270         hammer2_tid_t inum;
1271         int error;
1272
1273         LOCKSTART;
1274         dip = VTOI(ap->a_dvp);
1275         if (dip->pmp->ronly) {
1276                 LOCKSTOP;
1277                 return (EROFS);
1278         }
1279
1280         ncp = ap->a_nch->ncp;
1281         name = ncp->nc_name;
1282         name_len = ncp->nc_nlen;
1283
1284         hammer2_pfs_memory_wait(dip->pmp);
1285         hammer2_trans_init(dip->pmp, 0);
1286
1287         inum = hammer2_trans_newinum(dip->pmp);
1288
1289         /*
1290          * Create the actual inode as a hidden file in the iroot, then
1291          * create the directory entry as a hardlink to it.  The creation
1292          * of the actual inode sets its nlinks to 1 which is the value
1293          * we desire.
1294          */
1295         nip = hammer2_inode_create(dip->pmp->iroot, dip, ap->a_vap, ap->a_cred,
1296                                    NULL, 0, inum,
1297                                    inum, 0, 0,
1298                                    0, &error);
1299         if (error == 0) {
1300                 hammer2_inode_create(dip, dip, NULL, NULL,
1301                                      name, name_len, 0,
1302                                      nip->meta.inum,
1303                                      HAMMER2_OBJTYPE_HARDLINK, nip->meta.type,
1304                                      0, &error);
1305         }
1306
1307         if (error) {
1308                 KKASSERT(nip == NULL);
1309                 *ap->a_vpp = NULL;
1310         } else {
1311                 *ap->a_vpp = hammer2_igetv(nip, &error);
1312                 hammer2_inode_unlock(nip);
1313         }
1314
1315         /*
1316          * Update dip's mtime
1317          */
1318         if (error == 0) {
1319                 uint64_t mtime;
1320
1321                 hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);
1322                 hammer2_update_time(&mtime);
1323                 hammer2_inode_modify(dip);
1324                 dip->meta.mtime = mtime;
1325                 hammer2_inode_unlock(dip);
1326         }
1327
1328         hammer2_trans_done(dip->pmp);
1329
1330         if (error == 0) {
1331                 cache_setunresolved(ap->a_nch);
1332                 cache_setvp(ap->a_nch, *ap->a_vpp);
1333         }
1334         LOCKSTOP;
1335         return error;
1336 }
1337
1338 static
1339 int
1340 hammer2_vop_open(struct vop_open_args *ap)
1341 {
1342         return vop_stdopen(ap);
1343 }
1344
1345 /*
1346  * hammer2_vop_advlock { vp, id, op, fl, flags }
1347  */
1348 static
1349 int
1350 hammer2_vop_advlock(struct vop_advlock_args *ap)
1351 {
1352         hammer2_inode_t *ip = VTOI(ap->a_vp);
1353         hammer2_off_t size;
1354
1355         size = ip->meta.size;
1356         return (lf_advlock(ap, &ip->advlock, size));
1357 }
1358
1359 static
1360 int
1361 hammer2_vop_close(struct vop_close_args *ap)
1362 {
1363         return vop_stdclose(ap);
1364 }
1365
1366 /*
1367  * hammer2_vop_nlink { nch, dvp, vp, cred }
1368  *
1369  * Create a hardlink from (vp) to {dvp, nch}.
1370  */
1371 static
1372 int
1373 hammer2_vop_nlink(struct vop_nlink_args *ap)
1374 {
1375         hammer2_inode_t *tdip;  /* target directory to create link in */
1376         hammer2_inode_t *ip;    /* inode we are hardlinking to */
1377         struct namecache *ncp;
1378         const uint8_t *name;
1379         size_t name_len;
1380         int error;
1381
1382         LOCKSTART;
1383         tdip = VTOI(ap->a_dvp);
1384         if (tdip->pmp->ronly) {
1385                 LOCKSTOP;
1386                 return (EROFS);
1387         }
1388
1389         ncp = ap->a_nch->ncp;
1390         name = ncp->nc_name;
1391         name_len = ncp->nc_nlen;
1392
1393         /*
1394          * ip represents the file being hardlinked.  The file could be a
1395          * normal file or a hardlink target if it has already been hardlinked.
1396          * (with the new semantics, it will almost always be a hardlink
1397          * target).
1398          *
1399          * Bump nlinks and potentially also create or move the hardlink
1400          * target in the parent directory common to (ip) and (tdip).  The
1401          * consolidation code can modify ip->cluster.  The returned cluster
1402          * is locked.
1403          */
1404         ip = VTOI(ap->a_vp);
1405         hammer2_pfs_memory_wait(ip->pmp);
1406         hammer2_trans_init(ip->pmp, 0);
1407
1408         /*
1409          * Target should be an indexed inode or there's no way we will ever
1410          * be able to find it!
1411          */
1412         KKASSERT((ip->meta.name_key & HAMMER2_DIRHASH_VISIBLE) == 0);
1413
1414         error = 0;
1415
1416         /*
1417          * Can return NULL and error == EXDEV if the common parent
1418          * crosses a directory with the xlink flag set.
1419          */
1420         hammer2_inode_lock(tdip, 0);
1421         hammer2_inode_lock(ip, 0);
1422
1423         /*
1424          * Create the hardlink target and bump nlinks.
1425          */
1426         if (error == 0) {
1427                 hammer2_inode_create(tdip, tdip, NULL, NULL,
1428                                      name, name_len, 0,
1429                                      ip->meta.inum,
1430                                      HAMMER2_OBJTYPE_HARDLINK, ip->meta.type,
1431                                      0, &error);
1432                 hammer2_inode_modify(ip);
1433                 ++ip->meta.nlinks;
1434         }
1435         if (error == 0) {
1436                 /*
1437                  * Update dip's mtime
1438                  */
1439                 uint64_t mtime;
1440
1441                 hammer2_update_time(&mtime);
1442                 hammer2_inode_modify(tdip);
1443                 tdip->meta.mtime = mtime;
1444
1445                 cache_setunresolved(ap->a_nch);
1446                 cache_setvp(ap->a_nch, ap->a_vp);
1447         }
1448         hammer2_inode_unlock(ip);
1449         hammer2_inode_unlock(tdip);
1450
1451         hammer2_trans_done(ip->pmp);
1452
1453         LOCKSTOP;
1454         return error;
1455 }
1456
1457 /*
1458  * hammer2_vop_ncreate { nch, dvp, vpp, cred, vap }
1459  *
1460  * The operating system has already ensured that the directory entry
1461  * does not exist and done all appropriate namespace locking.
1462  */
1463 static
1464 int
1465 hammer2_vop_ncreate(struct vop_ncreate_args *ap)
1466 {
1467         hammer2_inode_t *dip;
1468         hammer2_inode_t *nip;
1469         struct namecache *ncp;
1470         const uint8_t *name;
1471         size_t name_len;
1472         hammer2_tid_t inum;
1473         int error;
1474
1475         LOCKSTART;
1476         dip = VTOI(ap->a_dvp);
1477         if (dip->pmp->ronly) {
1478                 LOCKSTOP;
1479                 return (EROFS);
1480         }
1481
1482         ncp = ap->a_nch->ncp;
1483         name = ncp->nc_name;
1484         name_len = ncp->nc_nlen;
1485         hammer2_pfs_memory_wait(dip->pmp);
1486         hammer2_trans_init(dip->pmp, 0);
1487
1488         inum = hammer2_trans_newinum(dip->pmp);
1489
1490         /*
1491          * Create the actual inode as a hidden file in the iroot, then
1492          * create the directory entry as a hardlink to it.  The creation
1493          * of the actual inode sets its nlinks to 1 which is the value
1494          * we desire.
1495          */
1496         nip = hammer2_inode_create(dip->pmp->iroot, dip, ap->a_vap, ap->a_cred,
1497                                    NULL, 0, inum,
1498                                    inum, 0, 0,
1499                                    0, &error);
1500
1501         if (error == 0) {
1502                 hammer2_inode_create(dip, dip, NULL, NULL,
1503                                      name, name_len, 0,
1504                                      nip->meta.inum,
1505                                      HAMMER2_OBJTYPE_HARDLINK, nip->meta.type,
1506                                      0, &error);
1507         }
1508         if (error) {
1509                 KKASSERT(nip == NULL);
1510                 *ap->a_vpp = NULL;
1511         } else {
1512                 *ap->a_vpp = hammer2_igetv(nip, &error);
1513                 hammer2_inode_unlock(nip);
1514         }
1515
1516         /*
1517          * Update dip's mtime
1518          */
1519         if (error == 0) {
1520                 uint64_t mtime;
1521
1522                 hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);
1523                 hammer2_update_time(&mtime);
1524                 hammer2_inode_modify(dip);
1525                 dip->meta.mtime = mtime;
1526                 hammer2_inode_unlock(dip);
1527         }
1528
1529         hammer2_trans_done(dip->pmp);
1530
1531         if (error == 0) {
1532                 cache_setunresolved(ap->a_nch);
1533                 cache_setvp(ap->a_nch, *ap->a_vpp);
1534         }
1535         LOCKSTOP;
1536         return error;
1537 }
1538
1539 /*
1540  * Make a device node (typically a fifo)
1541  */
1542 static
1543 int
1544 hammer2_vop_nmknod(struct vop_nmknod_args *ap)
1545 {
1546         hammer2_inode_t *dip;
1547         hammer2_inode_t *nip;
1548         struct namecache *ncp;
1549         const uint8_t *name;
1550         size_t name_len;
1551         hammer2_tid_t inum;
1552         int error;
1553
1554         LOCKSTART;
1555         dip = VTOI(ap->a_dvp);
1556         if (dip->pmp->ronly) {
1557                 LOCKSTOP;
1558                 return (EROFS);
1559         }
1560
1561         ncp = ap->a_nch->ncp;
1562         name = ncp->nc_name;
1563         name_len = ncp->nc_nlen;
1564         hammer2_pfs_memory_wait(dip->pmp);
1565         hammer2_trans_init(dip->pmp, 0);
1566
1567         /*
1568          * The device node is entered as the directory entry itself and not
1569          * as a hardlink to an inode.  Since one cannot obtain a
1570          * file handle on the filesystem entry representing the device, we
1571          * do not have to worry about indexing its inode.
1572          */
1573         inum = hammer2_trans_newinum(dip->pmp);
1574         nip = hammer2_inode_create(dip->pmp->iroot, dip, ap->a_vap, ap->a_cred,
1575                                    NULL, 0, inum,
1576                                    inum, 0, 0,
1577                                    0, &error);
1578         if (error == 0) {
1579                 hammer2_inode_create(dip, dip, NULL, NULL,
1580                                      name, name_len, 0,
1581                                      nip->meta.inum,
1582                                      HAMMER2_OBJTYPE_HARDLINK, nip->meta.type,
1583                                      0, &error);
1584         }
1585
1586
1587         if (error) {
1588                 KKASSERT(nip == NULL);
1589                 *ap->a_vpp = NULL;
1590         } else {
1591                 *ap->a_vpp = hammer2_igetv(nip, &error);
1592                 hammer2_inode_unlock(nip);
1593         }
1594
1595         /*
1596          * Update dip's mtime
1597          */
1598         if (error == 0) {
1599                 uint64_t mtime;
1600
1601                 hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);
1602                 hammer2_update_time(&mtime);
1603                 hammer2_inode_modify(dip);
1604                 dip->meta.mtime = mtime;
1605                 hammer2_inode_unlock(dip);
1606         }
1607
1608         hammer2_trans_done(dip->pmp);
1609
1610         if (error == 0) {
1611                 cache_setunresolved(ap->a_nch);
1612                 cache_setvp(ap->a_nch, *ap->a_vpp);
1613         }
1614         LOCKSTOP;
1615         return error;
1616 }
1617
1618 /*
1619  * hammer2_vop_nsymlink { nch, dvp, vpp, cred, vap, target }
1620  */
1621 static
1622 int
1623 hammer2_vop_nsymlink(struct vop_nsymlink_args *ap)
1624 {
1625         hammer2_inode_t *dip;
1626         hammer2_inode_t *nip;
1627         struct namecache *ncp;
1628         const uint8_t *name;
1629         size_t name_len;
1630         hammer2_tid_t inum;
1631         int error;
1632         
1633         dip = VTOI(ap->a_dvp);
1634         if (dip->pmp->ronly)
1635                 return (EROFS);
1636
1637         ncp = ap->a_nch->ncp;
1638         name = ncp->nc_name;
1639         name_len = ncp->nc_nlen;
1640         hammer2_pfs_memory_wait(dip->pmp);
1641         hammer2_trans_init(dip->pmp, 0);
1642
1643         ap->a_vap->va_type = VLNK;      /* enforce type */
1644
1645         /*
1646          * The softlink is entered into the directory itself and not
1647          * as a hardlink to an inode.  Since one cannot obtain a
1648          * file handle on the softlink itself we do not have to worry
1649          * about indexing its inode.
1650          */
1651         inum = hammer2_trans_newinum(dip->pmp);
1652
1653         nip = hammer2_inode_create(dip->pmp->iroot, dip, ap->a_vap, ap->a_cred,
1654                                    NULL, 0, inum,
1655                                    inum, 0, 0,
1656                                    0, &error);
1657         if (error == 0) {
1658                 hammer2_inode_create(dip, dip, NULL, NULL,
1659                                      name, name_len, 0,
1660                                      nip->meta.inum,
1661                                      HAMMER2_OBJTYPE_HARDLINK, nip->meta.type,
1662                                      0, &error);
1663         }
1664
1665
1666         if (error) {
1667                 KKASSERT(nip == NULL);
1668                 *ap->a_vpp = NULL;
1669                 hammer2_trans_done(dip->pmp);
1670                 return error;
1671         }
1672         *ap->a_vpp = hammer2_igetv(nip, &error);
1673
1674         /*
1675          * Build the softlink (~like file data) and finalize the namecache.
1676          */
1677         if (error == 0) {
1678                 size_t bytes;
1679                 struct uio auio;
1680                 struct iovec aiov;
1681
1682                 bytes = strlen(ap->a_target);
1683
1684                 hammer2_inode_unlock(nip);
1685                 bzero(&auio, sizeof(auio));
1686                 bzero(&aiov, sizeof(aiov));
1687                 auio.uio_iov = &aiov;
1688                 auio.uio_segflg = UIO_SYSSPACE;
1689                 auio.uio_rw = UIO_WRITE;
1690                 auio.uio_resid = bytes;
1691                 auio.uio_iovcnt = 1;
1692                 auio.uio_td = curthread;
1693                 aiov.iov_base = ap->a_target;
1694                 aiov.iov_len = bytes;
1695                 error = hammer2_write_file(nip, &auio, IO_APPEND, 0);
1696                 /* XXX handle error */
1697                 error = 0;
1698         } else {
1699                 hammer2_inode_unlock(nip);
1700         }
1701
1702         /*
1703          * Update dip's mtime
1704          */
1705         if (error == 0) {
1706                 uint64_t mtime;
1707
1708                 hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);
1709                 hammer2_update_time(&mtime);
1710                 hammer2_inode_modify(dip);
1711                 dip->meta.mtime = mtime;
1712                 hammer2_inode_unlock(dip);
1713         }
1714
1715         hammer2_trans_done(dip->pmp);
1716
1717         /*
1718          * Finalize namecache
1719          */
1720         if (error == 0) {
1721                 cache_setunresolved(ap->a_nch);
1722                 cache_setvp(ap->a_nch, *ap->a_vpp);
1723                 /* hammer2_knote(ap->a_dvp, NOTE_WRITE); */
1724         }
1725         return error;
1726 }
1727
1728 /*
1729  * hammer2_vop_nremove { nch, dvp, cred }
1730  */
1731 static
1732 int
1733 hammer2_vop_nremove(struct vop_nremove_args *ap)
1734 {
1735         hammer2_xop_unlink_t *xop;
1736         hammer2_inode_t *dip;
1737         hammer2_inode_t *ip;
1738         struct namecache *ncp;
1739         int error;
1740         int isopen;
1741
1742         LOCKSTART;
1743         dip = VTOI(ap->a_dvp);
1744         if (dip->pmp->ronly) {
1745                 LOCKSTOP;
1746                 return(EROFS);
1747         }
1748
1749         ncp = ap->a_nch->ncp;
1750
1751         hammer2_pfs_memory_wait(dip->pmp);
1752         hammer2_trans_init(dip->pmp, 0);
1753         hammer2_inode_lock(dip, 0);
1754
1755         /*
1756          * The unlink XOP unlinks the path from the directory and
1757          * locates and returns the cluster associated with the real inode.
1758          * We have to handle nlinks here on the frontend.
1759          */
1760         xop = hammer2_xop_alloc(dip, HAMMER2_XOP_MODIFYING);
1761         hammer2_xop_setname(&xop->head, ncp->nc_name, ncp->nc_nlen);
1762         isopen = cache_isopen(ap->a_nch);
1763         xop->isdir = 0;
1764         xop->dopermanent = isopen ?  0 : HAMMER2_DELETE_PERMANENT;
1765         hammer2_xop_start(&xop->head, hammer2_xop_unlink);
1766
1767         /*
1768          * Collect the real inode and adjust nlinks, destroy the real
1769          * inode if nlinks transitions to 0 and it was the real inode
1770          * (else it has already been removed).
1771          */
1772         error = hammer2_xop_collect(&xop->head, 0);
1773         hammer2_inode_unlock(dip);
1774
1775         if (error == 0) {
1776                 ip = hammer2_inode_get(dip->pmp, dip, &xop->head.cluster, -1);
1777                 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
1778                 if (ip) {
1779                         hammer2_inode_unlink_finisher(ip, isopen);
1780                         hammer2_inode_unlock(ip);
1781                 }
1782         } else {
1783                 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
1784         }
1785
1786         /*
1787          * Update dip's mtime
1788          */
1789         if (error == 0) {
1790                 uint64_t mtime;
1791
1792                 hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);
1793                 hammer2_update_time(&mtime);
1794                 hammer2_inode_modify(dip);
1795                 dip->meta.mtime = mtime;
1796                 hammer2_inode_unlock(dip);
1797         }
1798
1799         hammer2_inode_run_sideq(dip->pmp);
1800         hammer2_trans_done(dip->pmp);
1801         if (error == 0)
1802                 cache_unlink(ap->a_nch);
1803         LOCKSTOP;
1804         return (error);
1805 }
1806
1807 /*
1808  * hammer2_vop_nrmdir { nch, dvp, cred }
1809  */
1810 static
1811 int
1812 hammer2_vop_nrmdir(struct vop_nrmdir_args *ap)
1813 {
1814         hammer2_xop_unlink_t *xop;
1815         hammer2_inode_t *dip;
1816         hammer2_inode_t *ip;
1817         struct namecache *ncp;
1818         int isopen;
1819         int error;
1820
1821         LOCKSTART;
1822         dip = VTOI(ap->a_dvp);
1823         if (dip->pmp->ronly) {
1824                 LOCKSTOP;
1825                 return(EROFS);
1826         }
1827
1828         hammer2_pfs_memory_wait(dip->pmp);
1829         hammer2_trans_init(dip->pmp, 0);
1830         hammer2_inode_lock(dip, 0);
1831
1832         xop = hammer2_xop_alloc(dip, HAMMER2_XOP_MODIFYING);
1833
1834         ncp = ap->a_nch->ncp;
1835         hammer2_xop_setname(&xop->head, ncp->nc_name, ncp->nc_nlen);
1836         isopen = cache_isopen(ap->a_nch);
1837         xop->isdir = 1;
1838         xop->dopermanent = isopen ?  0 : HAMMER2_DELETE_PERMANENT;
1839         hammer2_xop_start(&xop->head, hammer2_xop_unlink);
1840
1841         /*
1842          * Collect the real inode and adjust nlinks, destroy the real
1843          * inode if nlinks transitions to 0 and it was the real inode
1844          * (else it has already been removed).
1845          */
1846         error = hammer2_xop_collect(&xop->head, 0);
1847         hammer2_inode_unlock(dip);
1848
1849         if (error == 0) {
1850                 ip = hammer2_inode_get(dip->pmp, dip, &xop->head.cluster, -1);
1851                 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
1852                 if (ip) {
1853                         hammer2_inode_unlink_finisher(ip, isopen);
1854                         hammer2_inode_unlock(ip);
1855                 }
1856         } else {
1857                 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
1858         }
1859
1860         /*
1861          * Update dip's mtime
1862          */
1863         if (error == 0) {
1864                 uint64_t mtime;
1865
1866                 hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);
1867                 hammer2_update_time(&mtime);
1868                 hammer2_inode_modify(dip);
1869                 dip->meta.mtime = mtime;
1870                 hammer2_inode_unlock(dip);
1871         }
1872
1873         hammer2_inode_run_sideq(dip->pmp);
1874         hammer2_trans_done(dip->pmp);
1875         if (error == 0)
1876                 cache_unlink(ap->a_nch);
1877         LOCKSTOP;
1878         return (error);
1879 }
1880
1881 /*
1882  * hammer2_vop_nrename { fnch, tnch, fdvp, tdvp, cred }
1883  */
1884 static
1885 int
1886 hammer2_vop_nrename(struct vop_nrename_args *ap)
1887 {
1888         struct namecache *fncp;
1889         struct namecache *tncp;
1890         hammer2_inode_t *fdip;
1891         hammer2_inode_t *tdip;
1892         hammer2_inode_t *ip;
1893         const uint8_t *fname;
1894         size_t fname_len;
1895         const uint8_t *tname;
1896         size_t tname_len;
1897         int error;
1898         int tnch_error;
1899         int update_tdip;
1900         int update_fdip;
1901         hammer2_key_t tlhc;
1902
1903         if (ap->a_fdvp->v_mount != ap->a_tdvp->v_mount)
1904                 return(EXDEV);
1905         if (ap->a_fdvp->v_mount != ap->a_fnch->ncp->nc_vp->v_mount)
1906                 return(EXDEV);
1907
1908         fdip = VTOI(ap->a_fdvp);        /* source directory */
1909         tdip = VTOI(ap->a_tdvp);        /* target directory */
1910
1911         if (fdip->pmp->ronly)
1912                 return(EROFS);
1913
1914         LOCKSTART;
1915         fncp = ap->a_fnch->ncp;         /* entry name in source */
1916         fname = fncp->nc_name;
1917         fname_len = fncp->nc_nlen;
1918
1919         tncp = ap->a_tnch->ncp;         /* entry name in target */
1920         tname = tncp->nc_name;
1921         tname_len = tncp->nc_nlen;
1922
1923         hammer2_pfs_memory_wait(tdip->pmp);
1924         hammer2_trans_init(tdip->pmp, 0);
1925
1926         update_tdip = 0;
1927         update_fdip = 0;
1928
1929         /*
1930          * ip is the inode being renamed.  If this is a hardlink then
1931          * ip represents the actual file and not the hardlink marker.
1932          */
1933         ip = VTOI(fncp->nc_vp);
1934
1935         KKASSERT((ip->meta.name_key & HAMMER2_DIRHASH_VISIBLE) == 0);
1936
1937         /*
1938          * Can return NULL and error == EXDEV if the common parent
1939          * crosses a directory with the xlink flag set.
1940          */
1941         error = 0;
1942         hammer2_inode_lock(fdip, 0);
1943         hammer2_inode_lock(tdip, 0);
1944         hammer2_inode_ref(ip);          /* extra ref */
1945
1946         hammer2_inode_lock(ip, 0);
1947
1948         /*
1949          * Delete the target namespace.
1950          */
1951         {
1952                 hammer2_xop_unlink_t *xop2;
1953                 hammer2_inode_t *tip;
1954                 int isopen;
1955
1956                 /*
1957                  * The unlink XOP unlinks the path from the directory and
1958                  * locates and returns the cluster associated with the real
1959                  * inode.  We have to handle nlinks here on the frontend.
1960                  */
1961                 xop2 = hammer2_xop_alloc(tdip, HAMMER2_XOP_MODIFYING);
1962                 hammer2_xop_setname(&xop2->head, tname, tname_len);
1963                 isopen = cache_isopen(ap->a_tnch);
1964                 xop2->isdir = -1;
1965                 xop2->dopermanent = isopen ?  0 : HAMMER2_DELETE_PERMANENT;
1966                 hammer2_xop_start(&xop2->head, hammer2_xop_unlink);
1967
1968                 /*
1969                  * Collect the real inode and adjust nlinks, destroy the real
1970                  * inode if nlinks transitions to 0 and it was the real inode
1971                  * (else it has already been removed).
1972                  */
1973                 tnch_error = hammer2_xop_collect(&xop2->head, 0);
1974                 /* hammer2_inode_unlock(tdip); */
1975
1976                 if (tnch_error == 0) {
1977                         tip = hammer2_inode_get(tdip->pmp, NULL,
1978                                                 &xop2->head.cluster, -1);
1979                         hammer2_xop_retire(&xop2->head, HAMMER2_XOPMASK_VOP);
1980                         if (tip) {
1981                                 hammer2_inode_unlink_finisher(tip, isopen);
1982                                 hammer2_inode_unlock(tip);
1983                         }
1984                 } else {
1985                         hammer2_xop_retire(&xop2->head, HAMMER2_XOPMASK_VOP);
1986                 }
1987                 /* hammer2_inode_lock(tdip, 0); */
1988
1989                 if (tnch_error && tnch_error != ENOENT) {
1990                         error = tnch_error;
1991                         goto done2;
1992                 }
1993                 update_tdip = 1;
1994         }
1995
1996         /*
1997          * Resolve the collision space for (tdip, tname, tname_len)
1998          *
1999          * tdip must be held exclusively locked to prevent races.
2000          */
2001         {
2002                 hammer2_xop_scanlhc_t *sxop;
2003                 hammer2_tid_t lhcbase;
2004
2005                 tlhc = hammer2_dirhash(tname, tname_len);
2006                 lhcbase = tlhc;
2007                 sxop = hammer2_xop_alloc(tdip, HAMMER2_XOP_MODIFYING);
2008                 sxop->lhc = tlhc;
2009                 hammer2_xop_start(&sxop->head, hammer2_xop_scanlhc);
2010                 while ((error = hammer2_xop_collect(&sxop->head, 0)) == 0) {
2011                         if (tlhc != sxop->head.cluster.focus->bref.key)
2012                                 break;
2013                         ++tlhc;
2014                 }
2015                 hammer2_xop_retire(&sxop->head, HAMMER2_XOPMASK_VOP);
2016
2017                 if (error) {
2018                         if (error != ENOENT)
2019                                 goto done2;
2020                         ++tlhc;
2021                         error = 0;
2022                 }
2023                 if ((lhcbase ^ tlhc) & ~HAMMER2_DIRHASH_LOMASK) {
2024                         error = ENOSPC;
2025                         goto done2;
2026                 }
2027         }
2028
2029         /*
2030          * Everything is setup, do the rename.
2031          *
2032          * We have to synchronize ip->meta to the underlying operation.
2033          *
2034          * NOTE: To avoid deadlocks we cannot lock (ip) while we are
2035          *       unlinking elements from their directories.  Locking
2036          *       the nlinks field does not lock the whole inode.
2037          */
2038         /* hammer2_inode_lock(ip, 0); */
2039         if (error == 0) {
2040                 hammer2_xop_nrename_t *xop4;
2041
2042                 xop4 = hammer2_xop_alloc(fdip, HAMMER2_XOP_MODIFYING);
2043                 xop4->lhc = tlhc;
2044                 xop4->ip_key = ip->meta.name_key;
2045                 hammer2_xop_setip2(&xop4->head, ip);
2046                 hammer2_xop_setip3(&xop4->head, tdip);
2047                 hammer2_xop_setname(&xop4->head, fname, fname_len);
2048                 hammer2_xop_setname2(&xop4->head, tname, tname_len);
2049                 hammer2_xop_start(&xop4->head, hammer2_xop_nrename);
2050
2051                 error = hammer2_xop_collect(&xop4->head, 0);
2052                 hammer2_xop_retire(&xop4->head, HAMMER2_XOPMASK_VOP);
2053
2054                 if (error == ENOENT)
2055                         error = 0;
2056                 if (error == 0 &&
2057                     (ip->meta.name_key & HAMMER2_DIRHASH_VISIBLE)) {
2058                         hammer2_inode_modify(ip);
2059                         ip->meta.name_len = tname_len;
2060                         ip->meta.name_key = tlhc;
2061
2062                 }
2063                 update_fdip = 1;
2064                 update_fdip = 1;
2065         }
2066
2067 done2:
2068         /*
2069          * Update directory mtimes to represent the something changed.
2070          */
2071         if (update_fdip || update_tdip) {
2072                 uint64_t mtime;
2073
2074                 hammer2_update_time(&mtime);
2075                 if (update_fdip) {
2076                         hammer2_inode_modify(fdip);
2077                         fdip->meta.mtime = mtime;
2078                 }
2079                 if (update_tdip) {
2080                         hammer2_inode_modify(tdip);
2081                         tdip->meta.mtime = mtime;
2082                 }
2083         }
2084         hammer2_inode_unlock(ip);
2085         hammer2_inode_unlock(tdip);
2086         hammer2_inode_unlock(fdip);
2087         hammer2_inode_drop(ip);
2088         hammer2_inode_run_sideq(fdip->pmp);
2089
2090         hammer2_trans_done(tdip->pmp);
2091
2092         /*
2093          * Issue the namecache update after unlocking all the internal
2094          * hammer structures, otherwise we might deadlock.
2095          */
2096         if (tnch_error == 0) {
2097                 cache_unlink(ap->a_tnch);
2098                 cache_setunresolved(ap->a_tnch);
2099         }
2100         if (error == 0)
2101                 cache_rename(ap->a_fnch, ap->a_tnch);
2102
2103         LOCKSTOP;
2104         return (error);
2105 }
2106
2107 /*
2108  * hammer2_vop_ioctl { vp, command, data, fflag, cred }
2109  */
2110 static
2111 int
2112 hammer2_vop_ioctl(struct vop_ioctl_args *ap)
2113 {
2114         hammer2_inode_t *ip;
2115         int error;
2116
2117         LOCKSTART;
2118         ip = VTOI(ap->a_vp);
2119
2120         error = hammer2_ioctl(ip, ap->a_command, (void *)ap->a_data,
2121                               ap->a_fflag, ap->a_cred);
2122         LOCKSTOP;
2123         return (error);
2124 }
2125
2126 static
2127 int 
2128 hammer2_vop_mountctl(struct vop_mountctl_args *ap)
2129 {
2130         struct mount *mp;
2131         hammer2_pfs_t *pmp;
2132         int rc;
2133
2134         LOCKSTART;
2135         switch (ap->a_op) {
2136         case (MOUNTCTL_SET_EXPORT):
2137                 mp = ap->a_head.a_ops->head.vv_mount;
2138                 pmp = MPTOPMP(mp);
2139
2140                 if (ap->a_ctllen != sizeof(struct export_args))
2141                         rc = (EINVAL);
2142                 else
2143                         rc = vfs_export(mp, &pmp->export,
2144                                         (const struct export_args *)ap->a_ctl);
2145                 break;
2146         default:
2147                 rc = vop_stdmountctl(ap);
2148                 break;
2149         }
2150         LOCKSTOP;
2151         return (rc);
2152 }
2153
2154 /*
2155  * KQFILTER
2156  */
2157 static void filt_hammer2detach(struct knote *kn);
2158 static int filt_hammer2read(struct knote *kn, long hint);
2159 static int filt_hammer2write(struct knote *kn, long hint);
2160 static int filt_hammer2vnode(struct knote *kn, long hint);
2161
2162 static struct filterops hammer2read_filtops =
2163         { FILTEROP_ISFD | FILTEROP_MPSAFE,
2164           NULL, filt_hammer2detach, filt_hammer2read };
2165 static struct filterops hammer2write_filtops =
2166         { FILTEROP_ISFD | FILTEROP_MPSAFE,
2167           NULL, filt_hammer2detach, filt_hammer2write };
2168 static struct filterops hammer2vnode_filtops =
2169         { FILTEROP_ISFD | FILTEROP_MPSAFE,
2170           NULL, filt_hammer2detach, filt_hammer2vnode };
2171
2172 static
2173 int
2174 hammer2_vop_kqfilter(struct vop_kqfilter_args *ap)
2175 {
2176         struct vnode *vp = ap->a_vp;
2177         struct knote *kn = ap->a_kn;
2178
2179         switch (kn->kn_filter) {
2180         case EVFILT_READ:
2181                 kn->kn_fop = &hammer2read_filtops;
2182                 break;
2183         case EVFILT_WRITE:
2184                 kn->kn_fop = &hammer2write_filtops;
2185                 break;
2186         case EVFILT_VNODE:
2187                 kn->kn_fop = &hammer2vnode_filtops;
2188                 break;
2189         default:
2190                 return (EOPNOTSUPP);
2191         }
2192
2193         kn->kn_hook = (caddr_t)vp;
2194
2195         knote_insert(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
2196
2197         return(0);
2198 }
2199
2200 static void
2201 filt_hammer2detach(struct knote *kn)
2202 {
2203         struct vnode *vp = (void *)kn->kn_hook;
2204
2205         knote_remove(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
2206 }
2207
2208 static int
2209 filt_hammer2read(struct knote *kn, long hint)
2210 {
2211         struct vnode *vp = (void *)kn->kn_hook;
2212         hammer2_inode_t *ip = VTOI(vp);
2213         off_t off;
2214
2215         if (hint == NOTE_REVOKE) {
2216                 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
2217                 return(1);
2218         }
2219         off = ip->meta.size - kn->kn_fp->f_offset;
2220         kn->kn_data = (off < INTPTR_MAX) ? off : INTPTR_MAX;
2221         if (kn->kn_sfflags & NOTE_OLDAPI)
2222                 return(1);
2223         return (kn->kn_data != 0);
2224 }
2225
2226
2227 static int
2228 filt_hammer2write(struct knote *kn, long hint)
2229 {
2230         if (hint == NOTE_REVOKE)
2231                 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
2232         kn->kn_data = 0;
2233         return (1);
2234 }
2235
2236 static int
2237 filt_hammer2vnode(struct knote *kn, long hint)
2238 {
2239         if (kn->kn_sfflags & hint)
2240                 kn->kn_fflags |= hint;
2241         if (hint == NOTE_REVOKE) {
2242                 kn->kn_flags |= (EV_EOF | EV_NODATA);
2243                 return (1);
2244         }
2245         return (kn->kn_fflags != 0);
2246 }
2247
2248 /*
2249  * FIFO VOPS
2250  */
2251 static
2252 int
2253 hammer2_vop_markatime(struct vop_markatime_args *ap)
2254 {
2255         hammer2_inode_t *ip;
2256         struct vnode *vp;
2257
2258         vp = ap->a_vp;
2259         ip = VTOI(vp);
2260
2261         if (ip->pmp->ronly)
2262                 return(EROFS);
2263         return(0);
2264 }
2265
2266 static
2267 int
2268 hammer2_vop_fifokqfilter(struct vop_kqfilter_args *ap)
2269 {
2270         int error;
2271
2272         error = VOCALL(&fifo_vnode_vops, &ap->a_head);
2273         if (error)
2274                 error = hammer2_vop_kqfilter(ap);
2275         return(error);
2276 }
2277
2278 /*
2279  * VOPS vector
2280  */
2281 struct vop_ops hammer2_vnode_vops = {
2282         .vop_default    = vop_defaultop,
2283         .vop_fsync      = hammer2_vop_fsync,
2284         .vop_getpages   = vop_stdgetpages,
2285         .vop_putpages   = vop_stdputpages,
2286         .vop_access     = hammer2_vop_access,
2287         .vop_advlock    = hammer2_vop_advlock,
2288         .vop_close      = hammer2_vop_close,
2289         .vop_nlink      = hammer2_vop_nlink,
2290         .vop_ncreate    = hammer2_vop_ncreate,
2291         .vop_nsymlink   = hammer2_vop_nsymlink,
2292         .vop_nremove    = hammer2_vop_nremove,
2293         .vop_nrmdir     = hammer2_vop_nrmdir,
2294         .vop_nrename    = hammer2_vop_nrename,
2295         .vop_getattr    = hammer2_vop_getattr,
2296         .vop_setattr    = hammer2_vop_setattr,
2297         .vop_readdir    = hammer2_vop_readdir,
2298         .vop_readlink   = hammer2_vop_readlink,
2299         .vop_getpages   = vop_stdgetpages,
2300         .vop_putpages   = vop_stdputpages,
2301         .vop_read       = hammer2_vop_read,
2302         .vop_write      = hammer2_vop_write,
2303         .vop_open       = hammer2_vop_open,
2304         .vop_inactive   = hammer2_vop_inactive,
2305         .vop_reclaim    = hammer2_vop_reclaim,
2306         .vop_nresolve   = hammer2_vop_nresolve,
2307         .vop_nlookupdotdot = hammer2_vop_nlookupdotdot,
2308         .vop_nmkdir     = hammer2_vop_nmkdir,
2309         .vop_nmknod     = hammer2_vop_nmknod,
2310         .vop_ioctl      = hammer2_vop_ioctl,
2311         .vop_mountctl   = hammer2_vop_mountctl,
2312         .vop_bmap       = hammer2_vop_bmap,
2313         .vop_strategy   = hammer2_vop_strategy,
2314         .vop_kqfilter   = hammer2_vop_kqfilter
2315 };
2316
2317 struct vop_ops hammer2_spec_vops = {
2318         .vop_default =          vop_defaultop,
2319         .vop_fsync =            hammer2_vop_fsync,
2320         .vop_read =             vop_stdnoread,
2321         .vop_write =            vop_stdnowrite,
2322         .vop_access =           hammer2_vop_access,
2323         .vop_close =            hammer2_vop_close,
2324         .vop_markatime =        hammer2_vop_markatime,
2325         .vop_getattr =          hammer2_vop_getattr,
2326         .vop_inactive =         hammer2_vop_inactive,
2327         .vop_reclaim =          hammer2_vop_reclaim,
2328         .vop_setattr =          hammer2_vop_setattr
2329 };
2330
2331 struct vop_ops hammer2_fifo_vops = {
2332         .vop_default =          fifo_vnoperate,
2333         .vop_fsync =            hammer2_vop_fsync,
2334 #if 0
2335         .vop_read =             hammer2_vop_fiforead,
2336         .vop_write =            hammer2_vop_fifowrite,
2337 #endif
2338         .vop_access =           hammer2_vop_access,
2339 #if 0
2340         .vop_close =            hammer2_vop_fifoclose,
2341 #endif
2342         .vop_markatime =        hammer2_vop_markatime,
2343         .vop_getattr =          hammer2_vop_getattr,
2344         .vop_inactive =         hammer2_vop_inactive,
2345         .vop_reclaim =          hammer2_vop_reclaim,
2346         .vop_setattr =          hammer2_vop_setattr,
2347         .vop_kqfilter =         hammer2_vop_fifokqfilter
2348 };
2349