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