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