hammer2 - Fix *errorp, instrument strategy errors
[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->pip && ip != ip->pmp->iroot)
568                         inum = ip->pip->meta.inum & 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                         break;
834                 loff = (int)(uio->uio_offset - lbase);
835                 n = lblksize - loff;
836                 if (n > uio->uio_resid)
837                         n = uio->uio_resid;
838                 if (n > size - uio->uio_offset)
839                         n = (int)(size - uio->uio_offset);
840                 bp->b_flags |= B_AGE;
841                 uiomovebp(bp, (char *)bp->b_data + loff, n, uio);
842                 bqrelse(bp);
843         }
844         hammer2_mtx_unlock(&ip->truncate_lock);
845
846         return (error);
847 }
848
849 /*
850  * Write to the file represented by the inode via the logical buffer cache.
851  * The inode may represent a regular file or a symlink.
852  *
853  * The inode must not be locked.
854  */
855 static
856 int
857 hammer2_write_file(hammer2_inode_t *ip, struct uio *uio,
858                    int ioflag, int seqcount)
859 {
860         hammer2_key_t old_eof;
861         hammer2_key_t new_eof;
862         struct buf *bp;
863         int kflags;
864         int error;
865         int modified;
866
867         /*
868          * Setup if append
869          *
870          * WARNING! Assumes that the kernel interlocks size changes at the
871          *          vnode level.
872          */
873         hammer2_mtx_ex(&ip->lock);
874         hammer2_mtx_sh(&ip->truncate_lock);
875         if (ioflag & IO_APPEND)
876                 uio->uio_offset = ip->meta.size;
877         old_eof = ip->meta.size;
878
879         /*
880          * Extend the file if necessary.  If the write fails at some point
881          * we will truncate it back down to cover as much as we were able
882          * to write.
883          *
884          * Doing this now makes it easier to calculate buffer sizes in
885          * the loop.
886          */
887         kflags = 0;
888         error = 0;
889         modified = 0;
890
891         if (uio->uio_offset + uio->uio_resid > old_eof) {
892                 new_eof = uio->uio_offset + uio->uio_resid;
893                 modified = 1;
894                 hammer2_extend_file(ip, new_eof);
895                 kflags |= NOTE_EXTEND;
896         } else {
897                 new_eof = old_eof;
898         }
899         hammer2_mtx_unlock(&ip->lock);
900         
901         /*
902          * UIO write loop
903          */
904         while (uio->uio_resid > 0) {
905                 hammer2_key_t lbase;
906                 int trivial;
907                 int endofblk;
908                 int lblksize;
909                 int loff;
910                 int n;
911
912                 /*
913                  * Don't allow the buffer build to blow out the buffer
914                  * cache.
915                  */
916                 if ((ioflag & IO_RECURSE) == 0)
917                         bwillwrite(HAMMER2_PBUFSIZE);
918
919                 /*
920                  * This nominally tells us how much we can cluster and
921                  * what the logical buffer size needs to be.  Currently
922                  * we don't try to cluster the write and just handle one
923                  * block at a time.
924                  */
925                 lblksize = hammer2_calc_logical(ip, uio->uio_offset,
926                                                 &lbase, NULL);
927                 loff = (int)(uio->uio_offset - lbase);
928                 
929                 KKASSERT(lblksize <= 65536);
930
931                 /*
932                  * Calculate bytes to copy this transfer and whether the
933                  * copy completely covers the buffer or not.
934                  */
935                 trivial = 0;
936                 n = lblksize - loff;
937                 if (n > uio->uio_resid) {
938                         n = uio->uio_resid;
939                         if (loff == lbase && uio->uio_offset + n == new_eof)
940                                 trivial = 1;
941                         endofblk = 0;
942                 } else {
943                         if (loff == 0)
944                                 trivial = 1;
945                         endofblk = 1;
946                 }
947                 if (lbase >= new_eof)
948                         trivial = 1;
949
950                 /*
951                  * Get the buffer
952                  */
953                 if (uio->uio_segflg == UIO_NOCOPY) {
954                         /*
955                          * Issuing a write with the same data backing the
956                          * buffer.  Instantiate the buffer to collect the
957                          * backing vm pages, then read-in any missing bits.
958                          *
959                          * This case is used by vop_stdputpages().
960                          */
961                         bp = getblk(ip->vp, lbase, lblksize, GETBLK_BHEAVY, 0);
962                         if ((bp->b_flags & B_CACHE) == 0) {
963                                 bqrelse(bp);
964                                 error = bread(ip->vp, lbase, lblksize, &bp);
965                         }
966                 } else if (trivial) {
967                         /*
968                          * Even though we are entirely overwriting the buffer
969                          * we may still have to zero it out to avoid a
970                          * mmap/write visibility issue.
971                          */
972                         bp = getblk(ip->vp, lbase, lblksize, GETBLK_BHEAVY, 0);
973                         if ((bp->b_flags & B_CACHE) == 0)
974                                 vfs_bio_clrbuf(bp);
975                 } else {
976                         /*
977                          * Partial overwrite, read in any missing bits then
978                          * replace the portion being written.
979                          *
980                          * (The strategy code will detect zero-fill physical
981                          * blocks for this case).
982                          */
983                         error = bread(ip->vp, lbase, lblksize, &bp);
984                         if (error == 0)
985                                 bheavy(bp);
986                 }
987
988                 if (error) {
989                         brelse(bp);
990                         break;
991                 }
992
993                 /*
994                  * Ok, copy the data in
995                  */
996                 error = uiomovebp(bp, bp->b_data + loff, n, uio);
997                 kflags |= NOTE_WRITE;
998                 modified = 1;
999                 if (error) {
1000                         brelse(bp);
1001                         break;
1002                 }
1003
1004                 /*
1005                  * WARNING: Pageout daemon will issue UIO_NOCOPY writes
1006                  *          with IO_SYNC or IO_ASYNC set.  These writes
1007                  *          must be handled as the pageout daemon expects.
1008                  *
1009                  * NOTE!    H2 relies on cluster_write() here because it
1010                  *          cannot preallocate disk blocks at the logical
1011                  *          level due to not knowing what the compression
1012                  *          size will be at this time.
1013                  *
1014                  *          We must use cluster_write() here and we depend
1015                  *          on the write-behind feature to flush buffers
1016                  *          appropriately.  If we let the buffer daemons do
1017                  *          it the block allocations will be all over the
1018                  *          map.
1019                  */
1020                 if (ioflag & IO_SYNC) {
1021                         bwrite(bp);
1022                 } else if ((ioflag & IO_DIRECT) && endofblk) {
1023                         bawrite(bp);
1024                 } else if (ioflag & IO_ASYNC) {
1025                         bawrite(bp);
1026                 } else if (ip->vp->v_mount->mnt_flag & MNT_NOCLUSTERW) {
1027                         bdwrite(bp);
1028                 } else {
1029 #if 1
1030                         bp->b_flags |= B_CLUSTEROK;
1031                         cluster_write(bp, new_eof, lblksize, seqcount);
1032 #else
1033                         bdwrite(bp);
1034 #endif
1035                 }
1036         }
1037
1038         /*
1039          * Cleanup.  If we extended the file EOF but failed to write through
1040          * the entire write is a failure and we have to back-up.
1041          */
1042         if (error && new_eof != old_eof) {
1043                 hammer2_mtx_unlock(&ip->truncate_lock);
1044                 hammer2_mtx_ex(&ip->lock);
1045                 hammer2_mtx_ex(&ip->truncate_lock);
1046                 hammer2_truncate_file(ip, old_eof);
1047                 if (ip->flags & HAMMER2_INODE_MODIFIED)
1048                         hammer2_inode_chain_sync(ip);
1049                 hammer2_mtx_unlock(&ip->lock);
1050         } else if (modified) {
1051                 hammer2_mtx_ex(&ip->lock);
1052                 hammer2_inode_modify(ip);
1053                 hammer2_update_time(&ip->meta.mtime);
1054                 if (ip->flags & HAMMER2_INODE_MODIFIED)
1055                         hammer2_inode_chain_sync(ip);
1056                 hammer2_mtx_unlock(&ip->lock);
1057                 hammer2_knote(ip->vp, kflags);
1058         }
1059         hammer2_trans_assert_strategy(ip->pmp);
1060         hammer2_mtx_unlock(&ip->truncate_lock);
1061
1062         return error;
1063 }
1064
1065 /*
1066  * Truncate the size of a file.  The inode must not be locked.
1067  *
1068  * We must unconditionally set HAMMER2_INODE_RESIZED to properly
1069  * ensure that any on-media data beyond the new file EOF has been destroyed.
1070  *
1071  * WARNING: nvtruncbuf() can only be safely called without the inode lock
1072  *          held due to the way our write thread works.  If the truncation
1073  *          occurs in the middle of a buffer, nvtruncbuf() is responsible
1074  *          for dirtying that buffer and zeroing out trailing bytes.
1075  *
1076  * WARNING! Assumes that the kernel interlocks size changes at the
1077  *          vnode level.
1078  *
1079  * WARNING! Caller assumes responsibility for removing dead blocks
1080  *          if INODE_RESIZED is set.
1081  */
1082 static
1083 void
1084 hammer2_truncate_file(hammer2_inode_t *ip, hammer2_key_t nsize)
1085 {
1086         hammer2_key_t lbase;
1087         int nblksize;
1088
1089         LOCKSTART;
1090         hammer2_mtx_unlock(&ip->lock);
1091         if (ip->vp) {
1092                 nblksize = hammer2_calc_logical(ip, nsize, &lbase, NULL);
1093                 nvtruncbuf(ip->vp, nsize,
1094                            nblksize, (int)nsize & (nblksize - 1),
1095                            0);
1096         }
1097         hammer2_mtx_ex(&ip->lock);
1098         KKASSERT((ip->flags & HAMMER2_INODE_RESIZED) == 0);
1099         ip->osize = ip->meta.size;
1100         ip->meta.size = nsize;
1101         atomic_set_int(&ip->flags, HAMMER2_INODE_RESIZED);
1102         hammer2_inode_modify(ip);
1103         LOCKSTOP;
1104 }
1105
1106 /*
1107  * Extend the size of a file.  The inode must not be locked.
1108  *
1109  * Even though the file size is changing, we do not have to set the
1110  * INODE_RESIZED bit unless the file size crosses the EMBEDDED_BYTES
1111  * boundary.  When this occurs a hammer2_inode_chain_sync() is required
1112  * to prepare the inode cluster's indirect block table, otherwise
1113  * async execution of the strategy code will implode on us.
1114  *
1115  * WARNING! Assumes that the kernel interlocks size changes at the
1116  *          vnode level.
1117  *
1118  * WARNING! Caller assumes responsibility for transitioning out
1119  *          of the inode DIRECTDATA mode if INODE_RESIZED is set.
1120  */
1121 static
1122 void
1123 hammer2_extend_file(hammer2_inode_t *ip, hammer2_key_t nsize)
1124 {
1125         hammer2_key_t lbase;
1126         hammer2_key_t osize;
1127         int oblksize;
1128         int nblksize;
1129
1130         LOCKSTART;
1131
1132         KKASSERT((ip->flags & HAMMER2_INODE_RESIZED) == 0);
1133         hammer2_inode_modify(ip);
1134         osize = ip->meta.size;
1135         ip->osize = osize;
1136         ip->meta.size = nsize;
1137
1138         if (osize <= HAMMER2_EMBEDDED_BYTES && nsize > HAMMER2_EMBEDDED_BYTES) {
1139                 atomic_set_int(&ip->flags, HAMMER2_INODE_RESIZED);
1140                 hammer2_inode_chain_sync(ip);
1141         }
1142
1143         hammer2_mtx_unlock(&ip->lock);
1144         if (ip->vp) {
1145                 oblksize = hammer2_calc_logical(ip, osize, &lbase, NULL);
1146                 nblksize = hammer2_calc_logical(ip, nsize, &lbase, NULL);
1147                 nvextendbuf(ip->vp,
1148                             osize, nsize,
1149                             oblksize, nblksize,
1150                             -1, -1, 0);
1151         }
1152         hammer2_mtx_ex(&ip->lock);
1153
1154         LOCKSTOP;
1155 }
1156
1157 static
1158 int
1159 hammer2_vop_nresolve(struct vop_nresolve_args *ap)
1160 {
1161         hammer2_xop_nresolve_t *xop;
1162         hammer2_inode_t *ip;
1163         hammer2_inode_t *dip;
1164         struct namecache *ncp;
1165         struct vnode *vp;
1166         int error;
1167
1168         LOCKSTART;
1169         dip = VTOI(ap->a_dvp);
1170         xop = hammer2_xop_alloc(dip, 0);
1171
1172         ncp = ap->a_nch->ncp;
1173         hammer2_xop_setname(&xop->head, ncp->nc_name, ncp->nc_nlen);
1174
1175         /*
1176          * Note: In DragonFly the kernel handles '.' and '..'.
1177          */
1178         hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);
1179         hammer2_xop_start(&xop->head, hammer2_xop_nresolve);
1180
1181         error = hammer2_xop_collect(&xop->head, 0);
1182         if (error) {
1183                 ip = NULL;
1184         } else {
1185                 ip = hammer2_inode_get(dip->pmp, dip, &xop->head.cluster, -1);
1186         }
1187         hammer2_inode_unlock(dip);
1188
1189         /*
1190          * Acquire the related vnode
1191          *
1192          * NOTE: For error processing, only ENOENT resolves the namecache
1193          *       entry to NULL, otherwise we just return the error and
1194          *       leave the namecache unresolved.
1195          *
1196          * NOTE: multiple hammer2_inode structures can be aliased to the
1197          *       same chain element, for example for hardlinks.  This
1198          *       use case does not 'reattach' inode associations that
1199          *       might already exist, but always allocates a new one.
1200          *
1201          * WARNING: inode structure is locked exclusively via inode_get
1202          *          but chain was locked shared.  inode_unlock()
1203          *          will handle it properly.
1204          */
1205         if (ip) {
1206                 vp = hammer2_igetv(ip, &error);
1207                 if (error == 0) {
1208                         vn_unlock(vp);
1209                         cache_setvp(ap->a_nch, vp);
1210                 } else if (error == ENOENT) {
1211                         cache_setvp(ap->a_nch, NULL);
1212                 }
1213                 hammer2_inode_unlock(ip);
1214
1215                 /*
1216                  * The vp should not be released until after we've disposed
1217                  * of our locks, because it might cause vop_inactive() to
1218                  * be called.
1219                  */
1220                 if (vp)
1221                         vrele(vp);
1222         } else {
1223                 error = ENOENT;
1224                 cache_setvp(ap->a_nch, NULL);
1225         }
1226         hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
1227         KASSERT(error || ap->a_nch->ncp->nc_vp != NULL,
1228                 ("resolve error %d/%p ap %p\n",
1229                  error, ap->a_nch->ncp->nc_vp, ap));
1230         LOCKSTOP;
1231
1232         return error;
1233 }
1234
1235 static
1236 int
1237 hammer2_vop_nlookupdotdot(struct vop_nlookupdotdot_args *ap)
1238 {
1239         hammer2_inode_t *dip;
1240         hammer2_inode_t *ip;
1241         int error;
1242
1243         LOCKSTART;
1244         dip = VTOI(ap->a_dvp);
1245
1246         if ((ip = dip->pip) == NULL) {
1247                 *ap->a_vpp = NULL;
1248                 LOCKSTOP;
1249                 return ENOENT;
1250         }
1251         hammer2_inode_lock(ip, 0);
1252         *ap->a_vpp = hammer2_igetv(ip, &error);
1253         hammer2_inode_unlock(ip);
1254
1255         LOCKSTOP;
1256         return error;
1257 }
1258
1259 static
1260 int
1261 hammer2_vop_nmkdir(struct vop_nmkdir_args *ap)
1262 {
1263         hammer2_inode_t *dip;
1264         hammer2_inode_t *nip;
1265         struct namecache *ncp;
1266         const uint8_t *name;
1267         size_t name_len;
1268         int error;
1269
1270         LOCKSTART;
1271         dip = VTOI(ap->a_dvp);
1272         if (dip->pmp->ronly) {
1273                 LOCKSTOP;
1274                 return (EROFS);
1275         }
1276
1277         ncp = ap->a_nch->ncp;
1278         name = ncp->nc_name;
1279         name_len = ncp->nc_nlen;
1280
1281         hammer2_pfs_memory_wait(dip->pmp);
1282         hammer2_trans_init(dip->pmp, 0);
1283         nip = hammer2_inode_create(dip, ap->a_vap, ap->a_cred,
1284                                    name, name_len, 0,
1285                                    hammer2_trans_newinum(dip->pmp), 0, 0,
1286                                    0, &error);
1287         if (error) {
1288                 KKASSERT(nip == NULL);
1289                 *ap->a_vpp = NULL;
1290         } else {
1291                 *ap->a_vpp = hammer2_igetv(nip, &error);
1292                 hammer2_inode_unlock(nip);
1293         }
1294         hammer2_trans_done(dip->pmp);
1295
1296         if (error == 0) {
1297                 cache_setunresolved(ap->a_nch);
1298                 cache_setvp(ap->a_nch, *ap->a_vpp);
1299         }
1300         LOCKSTOP;
1301         return error;
1302 }
1303
1304 static
1305 int
1306 hammer2_vop_open(struct vop_open_args *ap)
1307 {
1308         return vop_stdopen(ap);
1309 }
1310
1311 /*
1312  * hammer2_vop_advlock { vp, id, op, fl, flags }
1313  */
1314 static
1315 int
1316 hammer2_vop_advlock(struct vop_advlock_args *ap)
1317 {
1318         hammer2_inode_t *ip = VTOI(ap->a_vp);
1319         hammer2_off_t size;
1320
1321         size = ip->meta.size;
1322         return (lf_advlock(ap, &ip->advlock, size));
1323 }
1324
1325 static
1326 int
1327 hammer2_vop_close(struct vop_close_args *ap)
1328 {
1329         return vop_stdclose(ap);
1330 }
1331
1332 /*
1333  * hammer2_vop_nlink { nch, dvp, vp, cred }
1334  *
1335  * Create a hardlink from (vp) to {dvp, nch}.
1336  */
1337 static
1338 int
1339 hammer2_vop_nlink(struct vop_nlink_args *ap)
1340 {
1341         hammer2_xop_nlink_t *xop1;
1342         hammer2_inode_t *fdip;  /* target directory to create link in */
1343         hammer2_inode_t *tdip;  /* target directory to create link in */
1344         hammer2_inode_t *cdip;  /* common parent directory */
1345         hammer2_inode_t *ip;    /* inode we are hardlinking to */
1346         struct namecache *ncp;
1347         const uint8_t *name;
1348         size_t name_len;
1349         int nlink_locked;
1350         int error;
1351
1352         LOCKSTART;
1353         tdip = VTOI(ap->a_dvp);
1354         if (tdip->pmp->ronly) {
1355                 LOCKSTOP;
1356                 return (EROFS);
1357         }
1358
1359         ncp = ap->a_nch->ncp;
1360         name = ncp->nc_name;
1361         name_len = ncp->nc_nlen;
1362
1363         /*
1364          * ip represents the file being hardlinked.  The file could be a
1365          * normal file or a hardlink target if it has already been hardlinked.
1366          * If ip is a hardlinked target then ip->pip represents the location
1367          * of the hardlinked target, NOT the location of the hardlink pointer.
1368          *
1369          * Bump nlinks and potentially also create or move the hardlink
1370          * target in the parent directory common to (ip) and (tdip).  The
1371          * consolidation code can modify ip->cluster and ip->pip.  The
1372          * returned cluster is locked.
1373          */
1374         ip = VTOI(ap->a_vp);
1375         hammer2_pfs_memory_wait(ip->pmp);
1376         hammer2_trans_init(ip->pmp, 0);
1377
1378         /*
1379          * The common parent directory must be locked first to avoid deadlocks.
1380          * Also note that fdip and/or tdip might match cdip.
1381          *
1382          * WARNING!  The kernel's namecache locks are insufficient for
1383          *           protecting us from hardlink shifts, since unrelated
1384          *           rename() or link() calls on parent directories might
1385          *           cause a shift.  A PFS-wide lock is required for this
1386          *           situation.
1387          */
1388         if (ip->meta.type == HAMMER2_OBJTYPE_DIRECTORY ||
1389             (ip->meta.name_key & HAMMER2_DIRHASH_VISIBLE) == 0) {
1390                 lockmgr(&ip->pmp->lock_nlink, LK_EXCLUSIVE);
1391                 nlink_locked = 1;
1392         } else {
1393                 nlink_locked = 0;
1394         }
1395         fdip = ip->pip;
1396         error = 0;
1397
1398         /*
1399          * Can return NULL and error == EXDEV if the common parent
1400          * crosses a directory with the xlink flag set.
1401          */
1402         cdip = hammer2_inode_common_parent(fdip, tdip, &error, 1);
1403         if (cdip)
1404                 hammer2_inode_lock(cdip, 0);
1405         hammer2_inode_lock(fdip, 0);
1406         hammer2_inode_lock(tdip, 0);
1407         hammer2_inode_lock(ip, 0);
1408
1409         /*
1410          * Dispatch xop_nlink unconditionally since we have to update nlinks.
1411          *
1412          * Otherwise we'd be able to avoid the XOP if the ip does not have
1413          * to be converted or moved.
1414          * If ip is not a hardlink target we must convert it to a hardlink.
1415          * If fdip != cdip we must shift the inode to cdip.
1416          *
1417          * XXX this and other nlink update usage should be passed top-down
1418          *     and not updated with a delta bottom-up.
1419          */
1420 #if 0
1421         if (fdip != cdip || (ip->meta.name_key & HAMMER2_DIRHASH_VISIBLE))
1422 #endif
1423         if (error == 0) {
1424                 xop1 = hammer2_xop_alloc(fdip, HAMMER2_XOP_MODIFYING);
1425                 hammer2_xop_setip2(&xop1->head, ip);
1426                 hammer2_xop_setip3(&xop1->head, cdip);
1427                 xop1->nlinks_delta = 1;
1428
1429                 hammer2_xop_start(&xop1->head, hammer2_xop_nlink);
1430                 error = hammer2_xop_collect(&xop1->head, 0);
1431                 hammer2_xop_retire(&xop1->head, HAMMER2_XOPMASK_VOP);
1432                 if (error == ENOENT)
1433                         error = 0;
1434         }
1435
1436         /*
1437          * Must synchronize original inode whos chains are now a hardlink
1438          * target.  We must match what the backend XOP did to the
1439          * chains.
1440          */
1441         if (error == 0 && (ip->meta.name_key & HAMMER2_DIRHASH_VISIBLE)) {
1442                 hammer2_inode_modify(ip);
1443                 ip->meta.name_key = ip->meta.inum;
1444                 ip->meta.name_len = 18; /* "0x%016jx" */
1445         }
1446
1447         /*
1448          * Create the hardlink target and bump nlinks.
1449          */
1450         if (error == 0) {
1451                 hammer2_inode_create(tdip, NULL, NULL,
1452                                      name, name_len, 0,
1453                                      ip->meta.inum,
1454                                      HAMMER2_OBJTYPE_HARDLINK, ip->meta.type,
1455                                      0, &error);
1456                 hammer2_inode_modify(ip);
1457                 ++ip->meta.nlinks;
1458         }
1459         if (error == 0) {
1460                 cache_setunresolved(ap->a_nch);
1461                 cache_setvp(ap->a_nch, ap->a_vp);
1462         }
1463         hammer2_inode_unlock(ip);
1464         hammer2_inode_unlock(tdip);
1465         hammer2_inode_unlock(fdip);
1466         if (cdip) {
1467                 hammer2_inode_unlock(cdip);
1468                 hammer2_inode_drop(cdip);
1469         }
1470
1471         if (nlink_locked)
1472                 lockmgr(&ip->pmp->lock_nlink, LK_RELEASE);
1473         hammer2_trans_done(ip->pmp);
1474
1475         LOCKSTOP;
1476         return error;
1477 }
1478
1479 /*
1480  * hammer2_vop_ncreate { nch, dvp, vpp, cred, vap }
1481  *
1482  * The operating system has already ensured that the directory entry
1483  * does not exist and done all appropriate namespace locking.
1484  */
1485 static
1486 int
1487 hammer2_vop_ncreate(struct vop_ncreate_args *ap)
1488 {
1489         hammer2_inode_t *dip;
1490         hammer2_inode_t *nip;
1491         struct namecache *ncp;
1492         const uint8_t *name;
1493         size_t name_len;
1494         int error;
1495
1496         LOCKSTART;
1497         dip = VTOI(ap->a_dvp);
1498         if (dip->pmp->ronly) {
1499                 LOCKSTOP;
1500                 return (EROFS);
1501         }
1502
1503         ncp = ap->a_nch->ncp;
1504         name = ncp->nc_name;
1505         name_len = ncp->nc_nlen;
1506         hammer2_pfs_memory_wait(dip->pmp);
1507         hammer2_trans_init(dip->pmp, 0);
1508
1509         nip = hammer2_inode_create(dip, ap->a_vap, ap->a_cred,
1510                                    name, name_len, 0,
1511                                    hammer2_trans_newinum(dip->pmp), 0, 0,
1512                                    0, &error);
1513         if (error) {
1514                 KKASSERT(nip == NULL);
1515                 *ap->a_vpp = NULL;
1516         } else {
1517                 *ap->a_vpp = hammer2_igetv(nip, &error);
1518                 hammer2_inode_unlock(nip);
1519         }
1520         hammer2_trans_done(dip->pmp);
1521
1522         if (error == 0) {
1523                 cache_setunresolved(ap->a_nch);
1524                 cache_setvp(ap->a_nch, *ap->a_vpp);
1525         }
1526         LOCKSTOP;
1527         return error;
1528 }
1529
1530 /*
1531  * Make a device node (typically a fifo)
1532  */
1533 static
1534 int
1535 hammer2_vop_nmknod(struct vop_nmknod_args *ap)
1536 {
1537         hammer2_inode_t *dip;
1538         hammer2_inode_t *nip;
1539         struct namecache *ncp;
1540         const uint8_t *name;
1541         size_t name_len;
1542         int error;
1543
1544         LOCKSTART;
1545         dip = VTOI(ap->a_dvp);
1546         if (dip->pmp->ronly) {
1547                 LOCKSTOP;
1548                 return (EROFS);
1549         }
1550
1551         ncp = ap->a_nch->ncp;
1552         name = ncp->nc_name;
1553         name_len = ncp->nc_nlen;
1554         hammer2_pfs_memory_wait(dip->pmp);
1555         hammer2_trans_init(dip->pmp, 0);
1556
1557         nip = hammer2_inode_create(dip, ap->a_vap, ap->a_cred,
1558                                    name, name_len, 0,
1559                                    hammer2_trans_newinum(dip->pmp), 0, 0,
1560                                    0, &error);
1561         if (error) {
1562                 KKASSERT(nip == NULL);
1563                 *ap->a_vpp = NULL;
1564         } else {
1565                 *ap->a_vpp = hammer2_igetv(nip, &error);
1566                 hammer2_inode_unlock(nip);
1567         }
1568         hammer2_trans_done(dip->pmp);
1569
1570         if (error == 0) {
1571                 cache_setunresolved(ap->a_nch);
1572                 cache_setvp(ap->a_nch, *ap->a_vpp);
1573         }
1574         LOCKSTOP;
1575         return error;
1576 }
1577
1578 /*
1579  * hammer2_vop_nsymlink { nch, dvp, vpp, cred, vap, target }
1580  */
1581 static
1582 int
1583 hammer2_vop_nsymlink(struct vop_nsymlink_args *ap)
1584 {
1585         hammer2_inode_t *dip;
1586         hammer2_inode_t *nip;
1587         struct namecache *ncp;
1588         const uint8_t *name;
1589         size_t name_len;
1590         int error;
1591         
1592         dip = VTOI(ap->a_dvp);
1593         if (dip->pmp->ronly)
1594                 return (EROFS);
1595
1596         ncp = ap->a_nch->ncp;
1597         name = ncp->nc_name;
1598         name_len = ncp->nc_nlen;
1599         hammer2_pfs_memory_wait(dip->pmp);
1600         hammer2_trans_init(dip->pmp, 0);
1601
1602         ap->a_vap->va_type = VLNK;      /* enforce type */
1603
1604         nip = hammer2_inode_create(dip, ap->a_vap, ap->a_cred,
1605                                    name, name_len, 0,
1606                                    hammer2_trans_newinum(dip->pmp), 0, 0,
1607                                    0, &error);
1608         if (error) {
1609                 KKASSERT(nip == NULL);
1610                 *ap->a_vpp = NULL;
1611                 hammer2_trans_done(dip->pmp);
1612                 return error;
1613         }
1614         *ap->a_vpp = hammer2_igetv(nip, &error);
1615
1616         /*
1617          * Build the softlink (~like file data) and finalize the namecache.
1618          */
1619         if (error == 0) {
1620                 size_t bytes;
1621                 struct uio auio;
1622                 struct iovec aiov;
1623
1624                 bytes = strlen(ap->a_target);
1625
1626                 hammer2_inode_unlock(nip);
1627                 bzero(&auio, sizeof(auio));
1628                 bzero(&aiov, sizeof(aiov));
1629                 auio.uio_iov = &aiov;
1630                 auio.uio_segflg = UIO_SYSSPACE;
1631                 auio.uio_rw = UIO_WRITE;
1632                 auio.uio_resid = bytes;
1633                 auio.uio_iovcnt = 1;
1634                 auio.uio_td = curthread;
1635                 aiov.iov_base = ap->a_target;
1636                 aiov.iov_len = bytes;
1637                 error = hammer2_write_file(nip, &auio, IO_APPEND, 0);
1638                 /* XXX handle error */
1639                 error = 0;
1640         } else {
1641                 hammer2_inode_unlock(nip);
1642         }
1643         hammer2_trans_done(dip->pmp);
1644
1645         /*
1646          * Finalize namecache
1647          */
1648         if (error == 0) {
1649                 cache_setunresolved(ap->a_nch);
1650                 cache_setvp(ap->a_nch, *ap->a_vpp);
1651                 /* hammer2_knote(ap->a_dvp, NOTE_WRITE); */
1652         }
1653         return error;
1654 }
1655
1656 /*
1657  * hammer2_vop_nremove { nch, dvp, cred }
1658  */
1659 static
1660 int
1661 hammer2_vop_nremove(struct vop_nremove_args *ap)
1662 {
1663         hammer2_xop_unlink_t *xop;
1664         hammer2_inode_t *dip;
1665         hammer2_inode_t *ip;
1666         struct namecache *ncp;
1667         int error;
1668         int isopen;
1669
1670         LOCKSTART;
1671         dip = VTOI(ap->a_dvp);
1672         if (dip->pmp->ronly) {
1673                 LOCKSTOP;
1674                 return(EROFS);
1675         }
1676
1677         ncp = ap->a_nch->ncp;
1678
1679         hammer2_pfs_memory_wait(dip->pmp);
1680         hammer2_trans_init(dip->pmp, 0);
1681         hammer2_inode_lock(dip, 0);
1682
1683         /*
1684          * The unlink XOP unlinks the path from the directory and
1685          * locates and returns the cluster associated with the real inode.
1686          * We have to handle nlinks here on the frontend.
1687          */
1688         xop = hammer2_xop_alloc(dip, HAMMER2_XOP_MODIFYING);
1689         hammer2_xop_setname(&xop->head, ncp->nc_name, ncp->nc_nlen);
1690         isopen = cache_isopen(ap->a_nch);
1691         xop->isdir = 0;
1692         xop->dopermanent = isopen ?  0 : HAMMER2_DELETE_PERMANENT;
1693         hammer2_xop_start(&xop->head, hammer2_xop_unlink);
1694
1695         /*
1696          * Collect the real inode and adjust nlinks, destroy the real
1697          * inode if nlinks transitions to 0 and it was the real inode
1698          * (else it has already been removed).
1699          */
1700         error = hammer2_xop_collect(&xop->head, 0);
1701         hammer2_inode_unlock(dip);
1702
1703         if (error == 0) {
1704                 ip = hammer2_inode_get(dip->pmp, dip, &xop->head.cluster, -1);
1705                 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
1706                 if (ip) {
1707                         hammer2_inode_unlink_finisher(ip, isopen);
1708                         hammer2_inode_unlock(ip);
1709                 }
1710         } else {
1711                 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
1712         }
1713
1714         hammer2_inode_run_sideq(dip->pmp);
1715         hammer2_trans_done(dip->pmp);
1716         if (error == 0)
1717                 cache_unlink(ap->a_nch);
1718         LOCKSTOP;
1719         return (error);
1720 }
1721
1722 /*
1723  * hammer2_vop_nrmdir { nch, dvp, cred }
1724  */
1725 static
1726 int
1727 hammer2_vop_nrmdir(struct vop_nrmdir_args *ap)
1728 {
1729         hammer2_xop_unlink_t *xop;
1730         hammer2_inode_t *dip;
1731         hammer2_inode_t *ip;
1732         struct namecache *ncp;
1733         int isopen;
1734         int error;
1735
1736         LOCKSTART;
1737         dip = VTOI(ap->a_dvp);
1738         if (dip->pmp->ronly) {
1739                 LOCKSTOP;
1740                 return(EROFS);
1741         }
1742
1743         hammer2_pfs_memory_wait(dip->pmp);
1744         hammer2_trans_init(dip->pmp, 0);
1745         hammer2_inode_lock(dip, 0);
1746
1747         xop = hammer2_xop_alloc(dip, HAMMER2_XOP_MODIFYING);
1748
1749         ncp = ap->a_nch->ncp;
1750         hammer2_xop_setname(&xop->head, ncp->nc_name, ncp->nc_nlen);
1751         isopen = cache_isopen(ap->a_nch);
1752         xop->isdir = 1;
1753         xop->dopermanent = isopen ?  0 : HAMMER2_DELETE_PERMANENT;
1754         hammer2_xop_start(&xop->head, hammer2_xop_unlink);
1755
1756         /*
1757          * Collect the real inode and adjust nlinks, destroy the real
1758          * inode if nlinks transitions to 0 and it was the real inode
1759          * (else it has already been removed).
1760          */
1761         error = hammer2_xop_collect(&xop->head, 0);
1762         hammer2_inode_unlock(dip);
1763
1764         if (error == 0) {
1765                 ip = hammer2_inode_get(dip->pmp, dip, &xop->head.cluster, -1);
1766                 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
1767                 if (ip) {
1768                         hammer2_inode_unlink_finisher(ip, isopen);
1769                         hammer2_inode_unlock(ip);
1770                 }
1771         } else {
1772                 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
1773         }
1774         hammer2_inode_run_sideq(dip->pmp);
1775         hammer2_trans_done(dip->pmp);
1776         if (error == 0)
1777                 cache_unlink(ap->a_nch);
1778         LOCKSTOP;
1779         return (error);
1780 }
1781
1782 /*
1783  * hammer2_vop_nrename { fnch, tnch, fdvp, tdvp, cred }
1784  */
1785 static
1786 int
1787 hammer2_vop_nrename(struct vop_nrename_args *ap)
1788 {
1789         struct namecache *fncp;
1790         struct namecache *tncp;
1791         hammer2_inode_t *cdip;
1792         hammer2_inode_t *fdip;
1793         hammer2_inode_t *tdip;
1794         hammer2_inode_t *ip;
1795         const uint8_t *fname;
1796         size_t fname_len;
1797         const uint8_t *tname;
1798         size_t tname_len;
1799         int error;
1800         int tnch_error;
1801         int nlink_locked;
1802         hammer2_key_t tlhc;
1803
1804         if (ap->a_fdvp->v_mount != ap->a_tdvp->v_mount)
1805                 return(EXDEV);
1806         if (ap->a_fdvp->v_mount != ap->a_fnch->ncp->nc_vp->v_mount)
1807                 return(EXDEV);
1808
1809         fdip = VTOI(ap->a_fdvp);        /* source directory */
1810         tdip = VTOI(ap->a_tdvp);        /* target directory */
1811
1812         if (fdip->pmp->ronly)
1813                 return(EROFS);
1814
1815         LOCKSTART;
1816         fncp = ap->a_fnch->ncp;         /* entry name in source */
1817         fname = fncp->nc_name;
1818         fname_len = fncp->nc_nlen;
1819
1820         tncp = ap->a_tnch->ncp;         /* entry name in target */
1821         tname = tncp->nc_name;
1822         tname_len = tncp->nc_nlen;
1823
1824         hammer2_pfs_memory_wait(tdip->pmp);
1825         hammer2_trans_init(tdip->pmp, 0);
1826
1827         /*
1828          * ip is the inode being renamed.  If this is a hardlink then
1829          * ip represents the actual file and not the hardlink marker.
1830          */
1831         ip = VTOI(fncp->nc_vp);
1832
1833         /*
1834          * The common parent directory must be locked first to avoid deadlocks.
1835          * Also note that fdip and/or tdip might match cdip.
1836          *
1837          * WARNING!  The kernel's namecache locks are insufficient for
1838          *           protecting us from hardlink shifts, since unrelated
1839          *           rename() or link() calls on parent directories might
1840          *           cause a shift.  A PFS-wide lock is required for this
1841          *           situation.
1842          */
1843         if (ip->meta.type == HAMMER2_OBJTYPE_DIRECTORY ||
1844             (ip->meta.name_key & HAMMER2_DIRHASH_VISIBLE) == 0) {
1845                 lockmgr(&ip->pmp->lock_nlink, LK_EXCLUSIVE);
1846                 nlink_locked = 1;
1847         } else {
1848                 nlink_locked = 0;
1849         }
1850
1851         /*
1852          * Can return NULL and error == EXDEV if the common parent
1853          * crosses a directory with the xlink flag set.
1854          */
1855         error = 0;
1856         cdip = hammer2_inode_common_parent(ip->pip, tdip, &error, 0);
1857         if (cdip == NULL) {
1858                 tnch_error = error;
1859                 goto done3;
1860         }
1861         hammer2_inode_lock(cdip, 0);
1862         hammer2_inode_lock(fdip, 0);
1863         hammer2_inode_lock(tdip, 0);
1864         hammer2_inode_ref(ip);          /* extra ref */
1865
1866         /*
1867          * If ip is a hardlink target and fdip != cdip we must shift the
1868          * inode to cdip.
1869          */
1870         hammer2_inode_lock(ip, 0);
1871
1872         if (fdip != cdip &&
1873             (ip->meta.name_key & HAMMER2_DIRHASH_VISIBLE) == 0) {
1874                 hammer2_xop_nlink_t *xop1;
1875
1876                 xop1 = hammer2_xop_alloc(fdip, HAMMER2_XOP_MODIFYING);
1877                 hammer2_xop_setip2(&xop1->head, ip);
1878                 hammer2_xop_setip3(&xop1->head, cdip);
1879                 xop1->nlinks_delta = 0;
1880
1881                 hammer2_xop_start(&xop1->head, hammer2_xop_nlink);
1882                 error = hammer2_xop_collect(&xop1->head, 0);
1883                 hammer2_xop_retire(&xop1->head, HAMMER2_XOPMASK_VOP);
1884         }
1885         /* hammer2_inode_unlock(ip); */
1886
1887         /*
1888          * Delete the target namespace.
1889          */
1890         {
1891                 hammer2_xop_unlink_t *xop2;
1892                 hammer2_inode_t *tip;
1893                 int isopen;
1894
1895                 /*
1896                  * The unlink XOP unlinks the path from the directory and
1897                  * locates and returns the cluster associated with the real
1898                  * inode.  We have to handle nlinks here on the frontend.
1899                  */
1900                 xop2 = hammer2_xop_alloc(tdip, HAMMER2_XOP_MODIFYING);
1901                 hammer2_xop_setname(&xop2->head, tname, tname_len);
1902                 isopen = cache_isopen(ap->a_tnch);
1903                 xop2->isdir = -1;
1904                 xop2->dopermanent = isopen ?  0 : HAMMER2_DELETE_PERMANENT;
1905                 hammer2_xop_start(&xop2->head, hammer2_xop_unlink);
1906
1907                 /*
1908                  * Collect the real inode and adjust nlinks, destroy the real
1909                  * inode if nlinks transitions to 0 and it was the real inode
1910                  * (else it has already been removed).
1911                  */
1912                 tnch_error = hammer2_xop_collect(&xop2->head, 0);
1913                 /* hammer2_inode_unlock(tdip); */
1914
1915                 if (tnch_error == 0) {
1916                         tip = hammer2_inode_get(tdip->pmp, NULL,
1917                                                 &xop2->head.cluster, -1);
1918                         hammer2_xop_retire(&xop2->head, HAMMER2_XOPMASK_VOP);
1919                         if (tip) {
1920                                 hammer2_inode_unlink_finisher(tip, isopen);
1921                                 hammer2_inode_unlock(tip);
1922                         }
1923                 } else {
1924                         hammer2_xop_retire(&xop2->head, HAMMER2_XOPMASK_VOP);
1925                 }
1926                 /* hammer2_inode_lock(tdip, 0); */
1927
1928                 if (tnch_error && tnch_error != ENOENT) {
1929                         error = tnch_error;
1930                         goto done2;
1931                 }
1932         }
1933
1934         /*
1935          * Resolve the collision space for (tdip, tname, tname_len)
1936          *
1937          * tdip must be held exclusively locked to prevent races.
1938          */
1939         {
1940                 hammer2_xop_scanlhc_t *sxop;
1941                 hammer2_tid_t lhcbase;
1942
1943                 tlhc = hammer2_dirhash(tname, tname_len);
1944                 lhcbase = tlhc;
1945                 sxop = hammer2_xop_alloc(tdip, HAMMER2_XOP_MODIFYING);
1946                 sxop->lhc = tlhc;
1947                 hammer2_xop_start(&sxop->head, hammer2_xop_scanlhc);
1948                 while ((error = hammer2_xop_collect(&sxop->head, 0)) == 0) {
1949                         if (tlhc != sxop->head.cluster.focus->bref.key)
1950                                 break;
1951                         ++tlhc;
1952                 }
1953                 hammer2_xop_retire(&sxop->head, HAMMER2_XOPMASK_VOP);
1954
1955                 if (error) {
1956                         if (error != ENOENT)
1957                                 goto done2;
1958                         ++tlhc;
1959                         error = 0;
1960                 }
1961                 if ((lhcbase ^ tlhc) & ~HAMMER2_DIRHASH_LOMASK) {
1962                         error = ENOSPC;
1963                         goto done2;
1964                 }
1965         }
1966
1967         /*
1968          * Everything is setup, do the rename.
1969          *
1970          * We have to synchronize ip->meta to the underlying operation.
1971          *
1972          * NOTE: To avoid deadlocks we cannot lock (ip) while we are
1973          *       unlinking elements from their directories.  Locking
1974          *       the nlinks field does not lock the whole inode.
1975          */
1976         /* hammer2_inode_lock(ip, 0); */
1977         if (error == 0) {
1978                 hammer2_xop_nrename_t *xop4;
1979
1980                 xop4 = hammer2_xop_alloc(fdip, HAMMER2_XOP_MODIFYING);
1981                 xop4->lhc = tlhc;
1982                 xop4->ip_key = ip->meta.name_key;
1983                 hammer2_xop_setip2(&xop4->head, ip);
1984                 hammer2_xop_setip3(&xop4->head, tdip);
1985                 hammer2_xop_setname(&xop4->head, fname, fname_len);
1986                 hammer2_xop_setname2(&xop4->head, tname, tname_len);
1987                 hammer2_xop_start(&xop4->head, hammer2_xop_nrename);
1988
1989                 error = hammer2_xop_collect(&xop4->head, 0);
1990                 hammer2_xop_retire(&xop4->head, HAMMER2_XOPMASK_VOP);
1991
1992                 if (error == ENOENT)
1993                         error = 0;
1994                 if (error == 0 &&
1995                     (ip->meta.name_key & HAMMER2_DIRHASH_VISIBLE)) {
1996                         hammer2_inode_modify(ip);
1997                         ip->meta.name_len = tname_len;
1998                         ip->meta.name_key = tlhc;
1999
2000                 }
2001         }
2002
2003         /*
2004          * Fixup ip->pip if we were renaming the actual file and not a
2005          * hardlink pointer.
2006          */
2007         if (error == 0 && (ip->meta.name_key & HAMMER2_DIRHASH_VISIBLE)) {
2008                 hammer2_inode_t *opip;
2009
2010                 if (ip->pip != tdip) {
2011                         hammer2_inode_ref(tdip);
2012                         opip = ip->pip;
2013                         ip->pip = tdip;
2014                         if (opip)
2015                                 hammer2_inode_drop(opip);
2016                 }
2017         }
2018 done2:
2019         hammer2_inode_unlock(ip);
2020         hammer2_inode_unlock(tdip);
2021         hammer2_inode_unlock(fdip);
2022         hammer2_inode_unlock(cdip);
2023         hammer2_inode_drop(ip);
2024         hammer2_inode_drop(cdip);
2025 done3:
2026         hammer2_inode_run_sideq(fdip->pmp);
2027
2028         if (nlink_locked)
2029                 lockmgr(&ip->pmp->lock_nlink, LK_RELEASE);
2030         hammer2_trans_done(tdip->pmp);
2031
2032         /*
2033          * Issue the namecache update after unlocking all the internal
2034          * hammer structures, otherwise we might deadlock.
2035          */
2036         if (tnch_error == 0) {
2037                 cache_unlink(ap->a_tnch);
2038                 cache_setunresolved(ap->a_tnch);
2039         }
2040         if (error == 0)
2041                 cache_rename(ap->a_fnch, ap->a_tnch);
2042
2043         LOCKSTOP;
2044         return (error);
2045 }
2046
2047 /*
2048  * hammer2_vop_ioctl { vp, command, data, fflag, cred }
2049  */
2050 static
2051 int
2052 hammer2_vop_ioctl(struct vop_ioctl_args *ap)
2053 {
2054         hammer2_inode_t *ip;
2055         int error;
2056
2057         LOCKSTART;
2058         ip = VTOI(ap->a_vp);
2059
2060         error = hammer2_ioctl(ip, ap->a_command, (void *)ap->a_data,
2061                               ap->a_fflag, ap->a_cred);
2062         LOCKSTOP;
2063         return (error);
2064 }
2065
2066 static
2067 int 
2068 hammer2_vop_mountctl(struct vop_mountctl_args *ap)
2069 {
2070         struct mount *mp;
2071         hammer2_pfs_t *pmp;
2072         int rc;
2073
2074         LOCKSTART;
2075         switch (ap->a_op) {
2076         case (MOUNTCTL_SET_EXPORT):
2077                 mp = ap->a_head.a_ops->head.vv_mount;
2078                 pmp = MPTOPMP(mp);
2079
2080                 if (ap->a_ctllen != sizeof(struct export_args))
2081                         rc = (EINVAL);
2082                 else
2083                         rc = vfs_export(mp, &pmp->export,
2084                                         (const struct export_args *)ap->a_ctl);
2085                 break;
2086         default:
2087                 rc = vop_stdmountctl(ap);
2088                 break;
2089         }
2090         LOCKSTOP;
2091         return (rc);
2092 }
2093
2094 /*
2095  * KQFILTER
2096  */
2097 static void filt_hammer2detach(struct knote *kn);
2098 static int filt_hammer2read(struct knote *kn, long hint);
2099 static int filt_hammer2write(struct knote *kn, long hint);
2100 static int filt_hammer2vnode(struct knote *kn, long hint);
2101
2102 static struct filterops hammer2read_filtops =
2103         { FILTEROP_ISFD | FILTEROP_MPSAFE,
2104           NULL, filt_hammer2detach, filt_hammer2read };
2105 static struct filterops hammer2write_filtops =
2106         { FILTEROP_ISFD | FILTEROP_MPSAFE,
2107           NULL, filt_hammer2detach, filt_hammer2write };
2108 static struct filterops hammer2vnode_filtops =
2109         { FILTEROP_ISFD | FILTEROP_MPSAFE,
2110           NULL, filt_hammer2detach, filt_hammer2vnode };
2111
2112 static
2113 int
2114 hammer2_vop_kqfilter(struct vop_kqfilter_args *ap)
2115 {
2116         struct vnode *vp = ap->a_vp;
2117         struct knote *kn = ap->a_kn;
2118
2119         switch (kn->kn_filter) {
2120         case EVFILT_READ:
2121                 kn->kn_fop = &hammer2read_filtops;
2122                 break;
2123         case EVFILT_WRITE:
2124                 kn->kn_fop = &hammer2write_filtops;
2125                 break;
2126         case EVFILT_VNODE:
2127                 kn->kn_fop = &hammer2vnode_filtops;
2128                 break;
2129         default:
2130                 return (EOPNOTSUPP);
2131         }
2132
2133         kn->kn_hook = (caddr_t)vp;
2134
2135         knote_insert(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
2136
2137         return(0);
2138 }
2139
2140 static void
2141 filt_hammer2detach(struct knote *kn)
2142 {
2143         struct vnode *vp = (void *)kn->kn_hook;
2144
2145         knote_remove(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
2146 }
2147
2148 static int
2149 filt_hammer2read(struct knote *kn, long hint)
2150 {
2151         struct vnode *vp = (void *)kn->kn_hook;
2152         hammer2_inode_t *ip = VTOI(vp);
2153         off_t off;
2154
2155         if (hint == NOTE_REVOKE) {
2156                 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
2157                 return(1);
2158         }
2159         off = ip->meta.size - kn->kn_fp->f_offset;
2160         kn->kn_data = (off < INTPTR_MAX) ? off : INTPTR_MAX;
2161         if (kn->kn_sfflags & NOTE_OLDAPI)
2162                 return(1);
2163         return (kn->kn_data != 0);
2164 }
2165
2166
2167 static int
2168 filt_hammer2write(struct knote *kn, long hint)
2169 {
2170         if (hint == NOTE_REVOKE)
2171                 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
2172         kn->kn_data = 0;
2173         return (1);
2174 }
2175
2176 static int
2177 filt_hammer2vnode(struct knote *kn, long hint)
2178 {
2179         if (kn->kn_sfflags & hint)
2180                 kn->kn_fflags |= hint;
2181         if (hint == NOTE_REVOKE) {
2182                 kn->kn_flags |= (EV_EOF | EV_NODATA);
2183                 return (1);
2184         }
2185         return (kn->kn_fflags != 0);
2186 }
2187
2188 /*
2189  * FIFO VOPS
2190  */
2191 static
2192 int
2193 hammer2_vop_markatime(struct vop_markatime_args *ap)
2194 {
2195         hammer2_inode_t *ip;
2196         struct vnode *vp;
2197
2198         vp = ap->a_vp;
2199         ip = VTOI(vp);
2200
2201         if (ip->pmp->ronly)
2202                 return(EROFS);
2203         return(0);
2204 }
2205
2206 static
2207 int
2208 hammer2_vop_fifokqfilter(struct vop_kqfilter_args *ap)
2209 {
2210         int error;
2211
2212         error = VOCALL(&fifo_vnode_vops, &ap->a_head);
2213         if (error)
2214                 error = hammer2_vop_kqfilter(ap);
2215         return(error);
2216 }
2217
2218 /*
2219  * VOPS vector
2220  */
2221 struct vop_ops hammer2_vnode_vops = {
2222         .vop_default    = vop_defaultop,
2223         .vop_fsync      = hammer2_vop_fsync,
2224         .vop_getpages   = vop_stdgetpages,
2225         .vop_putpages   = vop_stdputpages,
2226         .vop_access     = hammer2_vop_access,
2227         .vop_advlock    = hammer2_vop_advlock,
2228         .vop_close      = hammer2_vop_close,
2229         .vop_nlink      = hammer2_vop_nlink,
2230         .vop_ncreate    = hammer2_vop_ncreate,
2231         .vop_nsymlink   = hammer2_vop_nsymlink,
2232         .vop_nremove    = hammer2_vop_nremove,
2233         .vop_nrmdir     = hammer2_vop_nrmdir,
2234         .vop_nrename    = hammer2_vop_nrename,
2235         .vop_getattr    = hammer2_vop_getattr,
2236         .vop_setattr    = hammer2_vop_setattr,
2237         .vop_readdir    = hammer2_vop_readdir,
2238         .vop_readlink   = hammer2_vop_readlink,
2239         .vop_getpages   = vop_stdgetpages,
2240         .vop_putpages   = vop_stdputpages,
2241         .vop_read       = hammer2_vop_read,
2242         .vop_write      = hammer2_vop_write,
2243         .vop_open       = hammer2_vop_open,
2244         .vop_inactive   = hammer2_vop_inactive,
2245         .vop_reclaim    = hammer2_vop_reclaim,
2246         .vop_nresolve   = hammer2_vop_nresolve,
2247         .vop_nlookupdotdot = hammer2_vop_nlookupdotdot,
2248         .vop_nmkdir     = hammer2_vop_nmkdir,
2249         .vop_nmknod     = hammer2_vop_nmknod,
2250         .vop_ioctl      = hammer2_vop_ioctl,
2251         .vop_mountctl   = hammer2_vop_mountctl,
2252         .vop_bmap       = hammer2_vop_bmap,
2253         .vop_strategy   = hammer2_vop_strategy,
2254         .vop_kqfilter   = hammer2_vop_kqfilter
2255 };
2256
2257 struct vop_ops hammer2_spec_vops = {
2258         .vop_default =          vop_defaultop,
2259         .vop_fsync =            hammer2_vop_fsync,
2260         .vop_read =             vop_stdnoread,
2261         .vop_write =            vop_stdnowrite,
2262         .vop_access =           hammer2_vop_access,
2263         .vop_close =            hammer2_vop_close,
2264         .vop_markatime =        hammer2_vop_markatime,
2265         .vop_getattr =          hammer2_vop_getattr,
2266         .vop_inactive =         hammer2_vop_inactive,
2267         .vop_reclaim =          hammer2_vop_reclaim,
2268         .vop_setattr =          hammer2_vop_setattr
2269 };
2270
2271 struct vop_ops hammer2_fifo_vops = {
2272         .vop_default =          fifo_vnoperate,
2273         .vop_fsync =            hammer2_vop_fsync,
2274 #if 0
2275         .vop_read =             hammer2_vop_fiforead,
2276         .vop_write =            hammer2_vop_fifowrite,
2277 #endif
2278         .vop_access =           hammer2_vop_access,
2279 #if 0
2280         .vop_close =            hammer2_vop_fifoclose,
2281 #endif
2282         .vop_markatime =        hammer2_vop_markatime,
2283         .vop_getattr =          hammer2_vop_getattr,
2284         .vop_inactive =         hammer2_vop_inactive,
2285         .vop_reclaim =          hammer2_vop_reclaim,
2286         .vop_setattr =          hammer2_vop_setattr,
2287         .vop_kqfilter =         hammer2_vop_fifokqfilter
2288 };
2289