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