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