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