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