HAMMER 26/many: Misc features.
[dragonfly.git] / sys / vfs / hammer / hammer_vnops.c
1 /*
2  * Copyright (c) 2007 The DragonFly Project.  All rights reserved.
3  * 
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  * 
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  * 
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  * 
34  * $DragonFly: src/sys/vfs/hammer/hammer_vnops.c,v 1.28 2008/02/06 08:59:28 dillon Exp $
35  */
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/fcntl.h>
41 #include <sys/namecache.h>
42 #include <sys/vnode.h>
43 #include <sys/lockf.h>
44 #include <sys/event.h>
45 #include <sys/stat.h>
46 #include <sys/dirent.h>
47 #include <vm/vm_extern.h>
48 #include <vfs/fifofs/fifo.h>
49 #include "hammer.h"
50
51 /*
52  * USERFS VNOPS
53  */
54 /*static int hammer_vop_vnoperate(struct vop_generic_args *);*/
55 static int hammer_vop_fsync(struct vop_fsync_args *);
56 static int hammer_vop_read(struct vop_read_args *);
57 static int hammer_vop_write(struct vop_write_args *);
58 static int hammer_vop_access(struct vop_access_args *);
59 static int hammer_vop_advlock(struct vop_advlock_args *);
60 static int hammer_vop_close(struct vop_close_args *);
61 static int hammer_vop_ncreate(struct vop_ncreate_args *);
62 static int hammer_vop_getattr(struct vop_getattr_args *);
63 static int hammer_vop_nresolve(struct vop_nresolve_args *);
64 static int hammer_vop_nlookupdotdot(struct vop_nlookupdotdot_args *);
65 static int hammer_vop_nlink(struct vop_nlink_args *);
66 static int hammer_vop_nmkdir(struct vop_nmkdir_args *);
67 static int hammer_vop_nmknod(struct vop_nmknod_args *);
68 static int hammer_vop_open(struct vop_open_args *);
69 static int hammer_vop_pathconf(struct vop_pathconf_args *);
70 static int hammer_vop_print(struct vop_print_args *);
71 static int hammer_vop_readdir(struct vop_readdir_args *);
72 static int hammer_vop_readlink(struct vop_readlink_args *);
73 static int hammer_vop_nremove(struct vop_nremove_args *);
74 static int hammer_vop_nrename(struct vop_nrename_args *);
75 static int hammer_vop_nrmdir(struct vop_nrmdir_args *);
76 static int hammer_vop_setattr(struct vop_setattr_args *);
77 static int hammer_vop_strategy(struct vop_strategy_args *);
78 static int hammer_vop_nsymlink(struct vop_nsymlink_args *);
79 static int hammer_vop_nwhiteout(struct vop_nwhiteout_args *);
80 static int hammer_vop_ioctl(struct vop_ioctl_args *);
81 static int hammer_vop_mountctl(struct vop_mountctl_args *);
82
83 static int hammer_vop_fifoclose (struct vop_close_args *);
84 static int hammer_vop_fiforead (struct vop_read_args *);
85 static int hammer_vop_fifowrite (struct vop_write_args *);
86
87 static int hammer_vop_specclose (struct vop_close_args *);
88 static int hammer_vop_specread (struct vop_read_args *);
89 static int hammer_vop_specwrite (struct vop_write_args *);
90
91 struct vop_ops hammer_vnode_vops = {
92         .vop_default =          vop_defaultop,
93         .vop_fsync =            hammer_vop_fsync,
94         .vop_getpages =         vop_stdgetpages,
95         .vop_putpages =         vop_stdputpages,
96         .vop_read =             hammer_vop_read,
97         .vop_write =            hammer_vop_write,
98         .vop_access =           hammer_vop_access,
99         .vop_advlock =          hammer_vop_advlock,
100         .vop_close =            hammer_vop_close,
101         .vop_ncreate =          hammer_vop_ncreate,
102         .vop_getattr =          hammer_vop_getattr,
103         .vop_inactive =         hammer_vop_inactive,
104         .vop_reclaim =          hammer_vop_reclaim,
105         .vop_nresolve =         hammer_vop_nresolve,
106         .vop_nlookupdotdot =    hammer_vop_nlookupdotdot,
107         .vop_nlink =            hammer_vop_nlink,
108         .vop_nmkdir =           hammer_vop_nmkdir,
109         .vop_nmknod =           hammer_vop_nmknod,
110         .vop_open =             hammer_vop_open,
111         .vop_pathconf =         hammer_vop_pathconf,
112         .vop_print =            hammer_vop_print,
113         .vop_readdir =          hammer_vop_readdir,
114         .vop_readlink =         hammer_vop_readlink,
115         .vop_nremove =          hammer_vop_nremove,
116         .vop_nrename =          hammer_vop_nrename,
117         .vop_nrmdir =           hammer_vop_nrmdir,
118         .vop_setattr =          hammer_vop_setattr,
119         .vop_strategy =         hammer_vop_strategy,
120         .vop_nsymlink =         hammer_vop_nsymlink,
121         .vop_nwhiteout =        hammer_vop_nwhiteout,
122         .vop_ioctl =            hammer_vop_ioctl,
123         .vop_mountctl =         hammer_vop_mountctl
124 };
125
126 struct vop_ops hammer_spec_vops = {
127         .vop_default =          spec_vnoperate,
128         .vop_fsync =            hammer_vop_fsync,
129         .vop_read =             hammer_vop_specread,
130         .vop_write =            hammer_vop_specwrite,
131         .vop_access =           hammer_vop_access,
132         .vop_close =            hammer_vop_specclose,
133         .vop_getattr =          hammer_vop_getattr,
134         .vop_inactive =         hammer_vop_inactive,
135         .vop_reclaim =          hammer_vop_reclaim,
136         .vop_setattr =          hammer_vop_setattr
137 };
138
139 struct vop_ops hammer_fifo_vops = {
140         .vop_default =          fifo_vnoperate,
141         .vop_fsync =            hammer_vop_fsync,
142         .vop_read =             hammer_vop_fiforead,
143         .vop_write =            hammer_vop_fifowrite,
144         .vop_access =           hammer_vop_access,
145         .vop_close =            hammer_vop_fifoclose,
146         .vop_getattr =          hammer_vop_getattr,
147         .vop_inactive =         hammer_vop_inactive,
148         .vop_reclaim =          hammer_vop_reclaim,
149         .vop_setattr =          hammer_vop_setattr
150 };
151
152 static int hammer_dounlink(struct nchandle *nch, struct vnode *dvp,
153                            struct ucred *cred, int flags);
154 static int hammer_vop_strategy_read(struct vop_strategy_args *ap);
155 static int hammer_vop_strategy_write(struct vop_strategy_args *ap);
156
157 #if 0
158 static
159 int
160 hammer_vop_vnoperate(struct vop_generic_args *)
161 {
162         return (VOCALL(&hammer_vnode_vops, ap));
163 }
164 #endif
165
166 /*
167  * hammer_vop_fsync { vp, waitfor }
168  */
169 static
170 int
171 hammer_vop_fsync(struct vop_fsync_args *ap)
172 {
173         hammer_inode_t ip;
174         int error;
175
176         ip = VTOI(ap->a_vp);
177         error = hammer_sync_inode(ip, ap->a_waitfor, 0);
178         return (error);
179 }
180
181 /*
182  * hammer_vop_read { vp, uio, ioflag, cred }
183  */
184 static
185 int
186 hammer_vop_read(struct vop_read_args *ap)
187 {
188         struct hammer_transaction trans;
189         hammer_inode_t ip;
190         off_t offset;
191         struct buf *bp;
192         struct uio *uio;
193         int error;
194         int n;
195         int seqcount;
196
197         if (ap->a_vp->v_type != VREG)
198                 return (EINVAL);
199         ip = VTOI(ap->a_vp);
200         error = 0;
201         seqcount = ap->a_ioflag >> 16;
202
203         hammer_start_transaction(&trans, ip->hmp);
204
205         /*
206          * Access the data in HAMMER_BUFSIZE blocks via the buffer cache.
207          */
208         uio = ap->a_uio;
209         while (uio->uio_resid > 0 && uio->uio_offset < ip->ino_rec.ino_size) {
210                 offset = uio->uio_offset & HAMMER_BUFMASK;
211 #if 0
212                 error = cluster_read(ap->a_vp, ip->ino_rec.ino_size,
213                                      uio->uio_offset - offset, HAMMER_BUFSIZE,
214                                      MAXBSIZE, seqcount, &bp);
215 #endif
216                 error = bread(ap->a_vp, uio->uio_offset - offset,
217                               HAMMER_BUFSIZE, &bp);
218                 if (error) {
219                         brelse(bp);
220                         break;
221                 }
222                 /* bp->b_flags |= B_CLUSTEROK; temporarily disabled */
223                 n = HAMMER_BUFSIZE - offset;
224                 if (n > uio->uio_resid)
225                         n = uio->uio_resid;
226                 if (n > ip->ino_rec.ino_size - uio->uio_offset)
227                         n = (int)(ip->ino_rec.ino_size - uio->uio_offset);
228                 error = uiomove((char *)bp->b_data + offset, n, uio);
229                 if (error) {
230                         bqrelse(bp);
231                         break;
232                 }
233                 if ((ip->flags & HAMMER_INODE_RO) == 0 &&
234                     (ip->hmp->mp->mnt_flag & MNT_NOATIME) == 0) {
235                         ip->ino_rec.ino_atime = trans.tid;
236                         hammer_modify_inode(&trans, ip, HAMMER_INODE_ITIMES);
237                 }
238                 bqrelse(bp);
239         }
240         hammer_commit_transaction(&trans);
241         return (error);
242 }
243
244 /*
245  * hammer_vop_write { vp, uio, ioflag, cred }
246  */
247 static
248 int
249 hammer_vop_write(struct vop_write_args *ap)
250 {
251         struct hammer_transaction trans;
252         struct hammer_inode *ip;
253         struct uio *uio;
254         off_t offset;
255         struct buf *bp;
256         int error;
257         int n;
258         int flags;
259
260         if (ap->a_vp->v_type != VREG)
261                 return (EINVAL);
262         ip = VTOI(ap->a_vp);
263         error = 0;
264
265         if (ip->flags & HAMMER_INODE_RO)
266                 return (EROFS);
267
268         /*
269          * Create a transaction to cover the operations we perform.
270          */
271         hammer_start_transaction(&trans, ip->hmp);
272         uio = ap->a_uio;
273
274         /*
275          * Check append mode
276          */
277         if (ap->a_ioflag & IO_APPEND)
278                 uio->uio_offset = ip->ino_rec.ino_size;
279
280         /*
281          * Check for illegal write offsets.  Valid range is 0...2^63-1
282          */
283         if (uio->uio_offset < 0 || uio->uio_offset + uio->uio_resid <= 0) {
284                 hammer_commit_transaction(&trans);
285                 return (EFBIG);
286         }
287
288         /*
289          * Access the data in HAMMER_BUFSIZE blocks via the buffer cache.
290          */
291         while (uio->uio_resid > 0) {
292                 int fixsize = 0;
293
294                 offset = uio->uio_offset & HAMMER_BUFMASK;
295                 n = HAMMER_BUFSIZE - offset;
296                 if (n > uio->uio_resid)
297                         n = uio->uio_resid;
298                 if (uio->uio_offset + n > ip->ino_rec.ino_size) {
299                         vnode_pager_setsize(ap->a_vp, uio->uio_offset + n);
300                         fixsize = 1;
301                 }
302
303                 if (uio->uio_segflg == UIO_NOCOPY) {
304                         /*
305                          * Issuing a write with the same data backing the
306                          * buffer.  Instantiate the buffer to collect the
307                          * backing vm pages, then read-in any missing bits.
308                          *
309                          * This case is used by vop_stdputpages().
310                          */
311                         bp = getblk(ap->a_vp, uio->uio_offset - offset,
312                                     HAMMER_BUFSIZE, GETBLK_BHEAVY, 0);
313                         if ((bp->b_flags & B_CACHE) == 0) {
314                                 bqrelse(bp);
315                                 error = bread(ap->a_vp,
316                                               uio->uio_offset - offset,
317                                               HAMMER_BUFSIZE, &bp);
318                         }
319                 } else if (offset == 0 && uio->uio_resid >= HAMMER_BUFSIZE) {
320                         /*
321                          * entirely overwrite the buffer
322                          */
323                         bp = getblk(ap->a_vp, uio->uio_offset - offset,
324                                     HAMMER_BUFSIZE, GETBLK_BHEAVY, 0);
325                 } else if (offset == 0 && uio->uio_offset >= ip->ino_rec.ino_size) {
326                         /*
327                          * XXX
328                          */
329                         bp = getblk(ap->a_vp, uio->uio_offset - offset,
330                                     HAMMER_BUFSIZE, GETBLK_BHEAVY, 0);
331                         vfs_bio_clrbuf(bp);
332                 } else {
333                         /*
334                          * Partial overwrite, read in any missing bits then
335                          * replace the portion being written.
336                          */
337                         error = bread(ap->a_vp, uio->uio_offset - offset,
338                                       HAMMER_BUFSIZE, &bp);
339                         if (error == 0)
340                                 bheavy(bp);
341                 }
342                 if (error == 0)
343                         error = uiomove((char *)bp->b_data + offset, n, uio);
344
345                 /*
346                  * If we screwed up we have to undo any VM size changes we
347                  * made.
348                  */
349                 if (error) {
350                         brelse(bp);
351                         if (fixsize) {
352                                 vtruncbuf(ap->a_vp, ip->ino_rec.ino_size,
353                                           HAMMER_BUFSIZE);
354                         }
355                         break;
356                 }
357                 /* bp->b_flags |= B_CLUSTEROK; temporarily disabled */
358                 if (ip->ino_rec.ino_size < uio->uio_offset) {
359                         ip->ino_rec.ino_size = uio->uio_offset;
360                         flags = HAMMER_INODE_RDIRTY;
361                         vnode_pager_setsize(ap->a_vp, ip->ino_rec.ino_size);
362                 } else {
363                         flags = 0;
364                 }
365                 ip->ino_rec.ino_mtime = trans.tid;
366                 flags |= HAMMER_INODE_ITIMES | HAMMER_INODE_BUFS;
367                 hammer_modify_inode(&trans, ip, flags);
368
369                 /*
370                  * The file write must be tagged with the same TID as the
371                  * inode, for consistency in case the inode changed size.
372                  * This guarantees the on-disk data records will have a
373                  * TID <= the inode TID representing the size change.
374                  *
375                  * If a prior write has not yet flushed, retain its TID.
376                  */
377                 if (bp->b_tid == 0)
378                         bp->b_tid = ip->last_tid;
379
380                 if (ap->a_ioflag & IO_SYNC) {
381                         bwrite(bp);
382                 } else if (ap->a_ioflag & IO_DIRECT) {
383                         bawrite(bp);
384                 } else {
385                         bdwrite(bp);
386                 }
387         }
388         if (error)
389                 hammer_abort_transaction(&trans);
390         else
391                 hammer_commit_transaction(&trans);
392         return (error);
393 }
394
395 /*
396  * hammer_vop_access { vp, mode, cred }
397  */
398 static
399 int
400 hammer_vop_access(struct vop_access_args *ap)
401 {
402         struct hammer_inode *ip = VTOI(ap->a_vp);
403         uid_t uid;
404         gid_t gid;
405         int error;
406
407         uid = hammer_to_unix_xid(&ip->ino_data.uid);
408         gid = hammer_to_unix_xid(&ip->ino_data.gid);
409
410         error = vop_helper_access(ap, uid, gid, ip->ino_data.mode,
411                                   ip->ino_data.uflags);
412         return (error);
413 }
414
415 /*
416  * hammer_vop_advlock { vp, id, op, fl, flags }
417  */
418 static
419 int
420 hammer_vop_advlock(struct vop_advlock_args *ap)
421 {
422         struct hammer_inode *ip = VTOI(ap->a_vp);
423
424         return (lf_advlock(ap, &ip->advlock, ip->ino_rec.ino_size));
425 }
426
427 /*
428  * hammer_vop_close { vp, fflag }
429  */
430 static
431 int
432 hammer_vop_close(struct vop_close_args *ap)
433 {
434         return (vop_stdclose(ap));
435 }
436
437 /*
438  * hammer_vop_ncreate { nch, dvp, vpp, cred, vap }
439  *
440  * The operating system has already ensured that the directory entry
441  * does not exist and done all appropriate namespace locking.
442  */
443 static
444 int
445 hammer_vop_ncreate(struct vop_ncreate_args *ap)
446 {
447         struct hammer_transaction trans;
448         struct hammer_inode *dip;
449         struct hammer_inode *nip;
450         struct nchandle *nch;
451         int error;
452
453         nch = ap->a_nch;
454         dip = VTOI(ap->a_dvp);
455
456         if (dip->flags & HAMMER_INODE_RO)
457                 return (EROFS);
458
459         /*
460          * Create a transaction to cover the operations we perform.
461          */
462         hammer_start_transaction(&trans, dip->hmp);
463
464         /*
465          * Create a new filesystem object of the requested type.  The
466          * returned inode will be referenced but not locked.
467          */
468
469         error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred, dip, &nip);
470         if (error)
471                 kprintf("hammer_create_inode error %d\n", error);
472         if (error) {
473                 hammer_abort_transaction(&trans);
474                 *ap->a_vpp = NULL;
475                 return (error);
476         }
477
478         /*
479          * Add the new filesystem object to the directory.  This will also
480          * bump the inode's link count.
481          */
482         error = hammer_ip_add_directory(&trans, dip, nch->ncp, nip);
483         if (error)
484                 kprintf("hammer_ip_add_directory error %d\n", error);
485
486         /*
487          * Finish up.
488          */
489         if (error) {
490                 hammer_rel_inode(nip, 0);
491                 hammer_abort_transaction(&trans);
492                 *ap->a_vpp = NULL;
493         } else {
494                 hammer_commit_transaction(&trans);
495                 error = hammer_get_vnode(nip, LK_EXCLUSIVE, ap->a_vpp);
496                 hammer_rel_inode(nip, 0);
497                 if (error == 0) {
498                         cache_setunresolved(ap->a_nch);
499                         cache_setvp(ap->a_nch, *ap->a_vpp);
500                 }
501         }
502         return (error);
503 }
504
505 /*
506  * hammer_vop_getattr { vp, vap }
507  */
508 static
509 int
510 hammer_vop_getattr(struct vop_getattr_args *ap)
511 {
512         struct hammer_inode *ip = VTOI(ap->a_vp);
513         struct vattr *vap = ap->a_vap;
514
515 #if 0
516         if (cache_check_fsmid_vp(ap->a_vp, &ip->fsmid) &&
517             (vp->v_mount->mnt_flag & MNT_RDONLY) == 0 &&
518             ip->obj_asof == XXX
519         ) {
520                 /* LAZYMOD XXX */
521         }
522         hammer_itimes(ap->a_vp);
523 #endif
524
525         vap->va_fsid = ip->hmp->fsid_udev;
526         vap->va_fileid = ip->ino_rec.base.base.obj_id;
527         vap->va_mode = ip->ino_data.mode;
528         vap->va_nlink = ip->ino_rec.ino_nlinks;
529         vap->va_uid = hammer_to_unix_xid(&ip->ino_data.uid);
530         vap->va_gid = hammer_to_unix_xid(&ip->ino_data.gid);
531         vap->va_rmajor = 0;
532         vap->va_rminor = 0;
533         vap->va_size = ip->ino_rec.ino_size;
534         hammer_to_timespec(ip->ino_rec.ino_atime, &vap->va_atime);
535         hammer_to_timespec(ip->ino_rec.ino_mtime, &vap->va_mtime);
536         hammer_to_timespec(ip->ino_data.ctime, &vap->va_ctime);
537         vap->va_flags = ip->ino_data.uflags;
538         vap->va_gen = 1;        /* hammer inums are unique for all time */
539         vap->va_blocksize = 32768; /* XXX - extract from root volume */
540         vap->va_bytes = ip->ino_rec.ino_size;
541         vap->va_type = hammer_get_vnode_type(ip->ino_rec.base.base.obj_type);
542         vap->va_filerev = 0;    /* XXX */
543         /* mtime uniquely identifies any adjustments made to the file */
544         vap->va_fsmid = ip->ino_rec.ino_mtime;
545         vap->va_uid_uuid = ip->ino_data.uid;
546         vap->va_gid_uuid = ip->ino_data.gid;
547         vap->va_fsid_uuid = ip->hmp->fsid;
548         vap->va_vaflags = VA_UID_UUID_VALID | VA_GID_UUID_VALID |
549                           VA_FSID_UUID_VALID;
550
551         switch (ip->ino_rec.base.base.obj_type) {
552         case HAMMER_OBJTYPE_CDEV:
553         case HAMMER_OBJTYPE_BDEV:
554                 vap->va_rmajor = ip->ino_data.rmajor;
555                 vap->va_rminor = ip->ino_data.rminor;
556                 break;
557         default:
558                 break;
559         }
560
561         return(0);
562 }
563
564 /*
565  * hammer_vop_nresolve { nch, dvp, cred }
566  *
567  * Locate the requested directory entry.
568  */
569 static
570 int
571 hammer_vop_nresolve(struct vop_nresolve_args *ap)
572 {
573         struct namecache *ncp;
574         hammer_inode_t dip;
575         hammer_inode_t ip;
576         hammer_tid_t asof;
577         struct hammer_cursor cursor;
578         union hammer_record_ondisk *rec;
579         struct vnode *vp;
580         int64_t namekey;
581         int error;
582         int i;
583         int nlen;
584         int flags;
585         u_int64_t obj_id;
586
587         /*
588          * Misc initialization, plus handle as-of name extensions.  Look for
589          * the '@@' extension.  Note that as-of files and directories cannot
590          * be modified.
591          */
592         dip = VTOI(ap->a_dvp);
593         ncp = ap->a_nch->ncp;
594         asof = dip->obj_asof;
595         nlen = ncp->nc_nlen;
596         flags = dip->flags;
597
598         for (i = 0; i < nlen; ++i) {
599                 if (ncp->nc_name[i] == '@' && ncp->nc_name[i+1] == '@') {
600                         asof = hammer_str_to_tid(ncp->nc_name + i + 2);
601                         flags |= HAMMER_INODE_RO;
602                         break;
603                 }
604         }
605         nlen = i;
606
607         /*
608          * If there is no path component the time extension is relative to
609          * dip.
610          */
611         if (nlen == 0) {
612                 ip = hammer_get_inode(dip->hmp, &dip->cache[1], dip->obj_id,
613                                       asof, flags, &error);
614                 if (error == 0) {
615                         error = hammer_get_vnode(ip, LK_EXCLUSIVE, &vp);
616                         hammer_rel_inode(ip, 0);
617                 } else {
618                         vp = NULL;
619                 }
620                 if (error == 0) {
621                         vn_unlock(vp);
622                         cache_setvp(ap->a_nch, vp);
623                         vrele(vp);
624                 }
625                 return(error);
626         }
627
628         /*
629          * Calculate the namekey and setup the key range for the scan.  This
630          * works kinda like a chained hash table where the lower 32 bits
631          * of the namekey synthesize the chain.
632          *
633          * The key range is inclusive of both key_beg and key_end.
634          */
635         namekey = hammer_directory_namekey(ncp->nc_name, nlen);
636
637         error = hammer_init_cursor_hmp(&cursor, &dip->cache[0], dip->hmp);
638         cursor.key_beg.obj_id = dip->obj_id;
639         cursor.key_beg.key = namekey;
640         cursor.key_beg.create_tid = 0;
641         cursor.key_beg.delete_tid = 0;
642         cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
643         cursor.key_beg.obj_type = 0;
644
645         cursor.key_end = cursor.key_beg;
646         cursor.key_end.key |= 0xFFFFFFFFULL;
647         cursor.asof = asof;
648         cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
649
650         /*
651          * Scan all matching records (the chain), locate the one matching
652          * the requested path component.
653          *
654          * The hammer_ip_*() functions merge in-memory records with on-disk
655          * records for the purposes of the search.
656          */
657         if (error == 0)
658                 error = hammer_ip_first(&cursor, dip);
659
660         rec = NULL;
661         obj_id = 0;
662
663         while (error == 0) {
664                 error = hammer_ip_resolve_data(&cursor);
665                 if (error)
666                         break;
667                 rec = cursor.record;
668                 if (nlen == rec->entry.base.data_len &&
669                     bcmp(ncp->nc_name, cursor.data, nlen) == 0) {
670                         obj_id = rec->entry.obj_id;
671                         break;
672                 }
673                 error = hammer_ip_next(&cursor);
674         }
675         hammer_done_cursor(&cursor);
676         if (error == 0) {
677                 ip = hammer_get_inode(dip->hmp, &dip->cache[1],
678                                       obj_id, asof, flags, &error);
679                 if (error == 0) {
680                         error = hammer_get_vnode(ip, LK_EXCLUSIVE, &vp);
681                         hammer_rel_inode(ip, 0);
682                 } else {
683                         vp = NULL;
684                 }
685                 if (error == 0) {
686                         vn_unlock(vp);
687                         cache_setvp(ap->a_nch, vp);
688                         vrele(vp);
689                 }
690         } else if (error == ENOENT) {
691                 cache_setvp(ap->a_nch, NULL);
692         }
693         return (error);
694 }
695
696 /*
697  * hammer_vop_nlookupdotdot { dvp, vpp, cred }
698  *
699  * Locate the parent directory of a directory vnode.
700  *
701  * dvp is referenced but not locked.  *vpp must be returned referenced and
702  * locked.  A parent_obj_id of 0 does not necessarily indicate that we are
703  * at the root, instead it could indicate that the directory we were in was
704  * removed.
705  *
706  * NOTE: as-of sequences are not linked into the directory structure.  If
707  * we are at the root with a different asof then the mount point, reload
708  * the same directory with the mount point's asof.   I'm not sure what this
709  * will do to NFS.  We encode ASOF stamps in NFS file handles so it might not
710  * get confused, but it hasn't been tested.
711  */
712 static
713 int
714 hammer_vop_nlookupdotdot(struct vop_nlookupdotdot_args *ap)
715 {
716         struct hammer_inode *dip;
717         struct hammer_inode *ip;
718         int64_t parent_obj_id;
719         hammer_tid_t asof;
720         int error;
721
722         dip = VTOI(ap->a_dvp);
723         asof = dip->obj_asof;
724         parent_obj_id = dip->ino_data.parent_obj_id;
725
726         if (parent_obj_id == 0) {
727                 if (dip->obj_id == HAMMER_OBJID_ROOT &&
728                    asof != dip->hmp->asof) {
729                         parent_obj_id = dip->obj_id;
730                         asof = dip->hmp->asof;
731                         *ap->a_fakename = kmalloc(19, M_TEMP, M_WAITOK);
732                         ksnprintf(*ap->a_fakename, 19, "0x%016llx",
733                                    dip->obj_asof);
734                 } else {
735                         *ap->a_vpp = NULL;
736                         return ENOENT;
737                 }
738         }
739
740         ip = hammer_get_inode(dip->hmp, &dip->cache[1], parent_obj_id,
741                               asof, dip->flags, &error);
742         if (ip == NULL) {
743                 *ap->a_vpp = NULL;
744                 return(error);
745         }
746         error = hammer_get_vnode(ip, LK_EXCLUSIVE, ap->a_vpp);
747         hammer_rel_inode(ip, 0);
748         return (error);
749 }
750
751 /*
752  * hammer_vop_nlink { nch, dvp, vp, cred }
753  */
754 static
755 int
756 hammer_vop_nlink(struct vop_nlink_args *ap)
757 {
758         struct hammer_transaction trans;
759         struct hammer_inode *dip;
760         struct hammer_inode *ip;
761         struct nchandle *nch;
762         int error;
763
764         nch = ap->a_nch;
765         dip = VTOI(ap->a_dvp);
766         ip = VTOI(ap->a_vp);
767
768         if (dip->flags & HAMMER_INODE_RO)
769                 return (EROFS);
770         if (ip->flags & HAMMER_INODE_RO)
771                 return (EROFS);
772
773         /*
774          * Create a transaction to cover the operations we perform.
775          */
776         hammer_start_transaction(&trans, dip->hmp);
777
778         /*
779          * Add the filesystem object to the directory.  Note that neither
780          * dip nor ip are referenced or locked, but their vnodes are
781          * referenced.  This function will bump the inode's link count.
782          */
783         error = hammer_ip_add_directory(&trans, dip, nch->ncp, ip);
784
785         /*
786          * Finish up.
787          */
788         if (error) {
789                 hammer_abort_transaction(&trans);
790         } else {
791                 cache_setunresolved(nch);
792                 cache_setvp(nch, ap->a_vp);
793                 hammer_commit_transaction(&trans);
794         }
795         return (error);
796 }
797
798 /*
799  * hammer_vop_nmkdir { nch, dvp, vpp, cred, vap }
800  *
801  * The operating system has already ensured that the directory entry
802  * does not exist and done all appropriate namespace locking.
803  */
804 static
805 int
806 hammer_vop_nmkdir(struct vop_nmkdir_args *ap)
807 {
808         struct hammer_transaction trans;
809         struct hammer_inode *dip;
810         struct hammer_inode *nip;
811         struct nchandle *nch;
812         int error;
813
814         nch = ap->a_nch;
815         dip = VTOI(ap->a_dvp);
816
817         if (dip->flags & HAMMER_INODE_RO)
818                 return (EROFS);
819
820         /*
821          * Create a transaction to cover the operations we perform.
822          */
823         hammer_start_transaction(&trans, dip->hmp);
824
825         /*
826          * Create a new filesystem object of the requested type.  The
827          * returned inode will be referenced but not locked.
828          */
829         error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred, dip, &nip);
830         if (error)
831                 kprintf("hammer_mkdir error %d\n", error);
832         if (error) {
833                 hammer_abort_transaction(&trans);
834                 *ap->a_vpp = NULL;
835                 return (error);
836         }
837
838         /*
839          * Add the new filesystem object to the directory.  This will also
840          * bump the inode's link count.
841          */
842         error = hammer_ip_add_directory(&trans, dip, nch->ncp, nip);
843         if (error)
844                 kprintf("hammer_mkdir (add) error %d\n", error);
845
846         /*
847          * Finish up.
848          */
849         if (error) {
850                 hammer_rel_inode(nip, 0);
851                 hammer_abort_transaction(&trans);
852                 *ap->a_vpp = NULL;
853         } else {
854                 hammer_commit_transaction(&trans);
855                 error = hammer_get_vnode(nip, LK_EXCLUSIVE, ap->a_vpp);
856                 hammer_rel_inode(nip, 0);
857                 if (error == 0) {
858                         cache_setunresolved(ap->a_nch);
859                         cache_setvp(ap->a_nch, *ap->a_vpp);
860                 }
861         }
862         return (error);
863 }
864
865 /*
866  * hammer_vop_nmknod { nch, dvp, vpp, cred, vap }
867  *
868  * The operating system has already ensured that the directory entry
869  * does not exist and done all appropriate namespace locking.
870  */
871 static
872 int
873 hammer_vop_nmknod(struct vop_nmknod_args *ap)
874 {
875         struct hammer_transaction trans;
876         struct hammer_inode *dip;
877         struct hammer_inode *nip;
878         struct nchandle *nch;
879         int error;
880
881         nch = ap->a_nch;
882         dip = VTOI(ap->a_dvp);
883
884         if (dip->flags & HAMMER_INODE_RO)
885                 return (EROFS);
886
887         /*
888          * Create a transaction to cover the operations we perform.
889          */
890         hammer_start_transaction(&trans, dip->hmp);
891
892         /*
893          * Create a new filesystem object of the requested type.  The
894          * returned inode will be referenced but not locked.
895          */
896         error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred, dip, &nip);
897         if (error) {
898                 hammer_abort_transaction(&trans);
899                 *ap->a_vpp = NULL;
900                 return (error);
901         }
902
903         /*
904          * Add the new filesystem object to the directory.  This will also
905          * bump the inode's link count.
906          */
907         error = hammer_ip_add_directory(&trans, dip, nch->ncp, nip);
908
909         /*
910          * Finish up.
911          */
912         if (error) {
913                 hammer_rel_inode(nip, 0);
914                 hammer_abort_transaction(&trans);
915                 *ap->a_vpp = NULL;
916         } else {
917                 hammer_commit_transaction(&trans);
918                 error = hammer_get_vnode(nip, LK_EXCLUSIVE, ap->a_vpp);
919                 hammer_rel_inode(nip, 0);
920                 if (error == 0) {
921                         cache_setunresolved(ap->a_nch);
922                         cache_setvp(ap->a_nch, *ap->a_vpp);
923                 }
924         }
925         return (error);
926 }
927
928 /*
929  * hammer_vop_open { vp, mode, cred, fp }
930  */
931 static
932 int
933 hammer_vop_open(struct vop_open_args *ap)
934 {
935         if ((ap->a_mode & FWRITE) && (VTOI(ap->a_vp)->flags & HAMMER_INODE_RO))
936                 return (EROFS);
937
938         return(vop_stdopen(ap));
939 }
940
941 /*
942  * hammer_vop_pathconf { vp, name, retval }
943  */
944 static
945 int
946 hammer_vop_pathconf(struct vop_pathconf_args *ap)
947 {
948         return EOPNOTSUPP;
949 }
950
951 /*
952  * hammer_vop_print { vp }
953  */
954 static
955 int
956 hammer_vop_print(struct vop_print_args *ap)
957 {
958         return EOPNOTSUPP;
959 }
960
961 /*
962  * hammer_vop_readdir { vp, uio, cred, *eofflag, *ncookies, off_t **cookies }
963  */
964 static
965 int
966 hammer_vop_readdir(struct vop_readdir_args *ap)
967 {
968         struct hammer_cursor cursor;
969         struct hammer_inode *ip;
970         struct uio *uio;
971         hammer_record_ondisk_t rec;
972         hammer_base_elm_t base;
973         int error;
974         int cookie_index;
975         int ncookies;
976         off_t *cookies;
977         off_t saveoff;
978         int r;
979
980         ip = VTOI(ap->a_vp);
981         uio = ap->a_uio;
982         saveoff = uio->uio_offset;
983
984         if (ap->a_ncookies) {
985                 ncookies = uio->uio_resid / 16 + 1;
986                 if (ncookies > 1024)
987                         ncookies = 1024;
988                 cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
989                 cookie_index = 0;
990         } else {
991                 ncookies = -1;
992                 cookies = NULL;
993                 cookie_index = 0;
994         }
995
996         /*
997          * Handle artificial entries
998          */
999         error = 0;
1000         if (saveoff == 0) {
1001                 r = vop_write_dirent(&error, uio, ip->obj_id, DT_DIR, 1, ".");
1002                 if (r)
1003                         goto done;
1004                 if (cookies)
1005                         cookies[cookie_index] = saveoff;
1006                 ++saveoff;
1007                 ++cookie_index;
1008                 if (cookie_index == ncookies)
1009                         goto done;
1010         }
1011         if (saveoff == 1) {
1012                 if (ip->ino_data.parent_obj_id) {
1013                         r = vop_write_dirent(&error, uio,
1014                                              ip->ino_data.parent_obj_id,
1015                                              DT_DIR, 2, "..");
1016                 } else {
1017                         r = vop_write_dirent(&error, uio,
1018                                              ip->obj_id, DT_DIR, 2, "..");
1019                 }
1020                 if (r)
1021                         goto done;
1022                 if (cookies)
1023                         cookies[cookie_index] = saveoff;
1024                 ++saveoff;
1025                 ++cookie_index;
1026                 if (cookie_index == ncookies)
1027                         goto done;
1028         }
1029
1030         /*
1031          * Key range (begin and end inclusive) to scan.  Directory keys
1032          * directly translate to a 64 bit 'seek' position.
1033          */
1034         hammer_init_cursor_hmp(&cursor, &ip->cache[0], ip->hmp);
1035         cursor.key_beg.obj_id = ip->obj_id;
1036         cursor.key_beg.create_tid = 0;
1037         cursor.key_beg.delete_tid = 0;
1038         cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1039         cursor.key_beg.obj_type = 0;
1040         cursor.key_beg.key = saveoff;
1041
1042         cursor.key_end = cursor.key_beg;
1043         cursor.key_end.key = HAMMER_MAX_KEY;
1044         cursor.asof = ip->obj_asof;
1045         cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
1046
1047         error = hammer_ip_first(&cursor, ip);
1048
1049         while (error == 0) {
1050                 error = hammer_ip_resolve_data(&cursor);
1051                 if (error)
1052                         break;
1053                 rec = cursor.record;
1054                 base = &rec->base.base;
1055                 saveoff = base->key;
1056
1057                 if (base->obj_id != ip->obj_id)
1058                         panic("readdir: bad record at %p", cursor.node);
1059
1060                 r = vop_write_dirent(
1061                              &error, uio, rec->entry.obj_id,
1062                              hammer_get_dtype(rec->entry.base.base.obj_type),
1063                              rec->entry.base.data_len,
1064                              (void *)cursor.data);
1065                 if (r)
1066                         break;
1067                 ++saveoff;
1068                 if (cookies)
1069                         cookies[cookie_index] = base->key;
1070                 ++cookie_index;
1071                 if (cookie_index == ncookies)
1072                         break;
1073                 error = hammer_ip_next(&cursor);
1074         }
1075         hammer_done_cursor(&cursor);
1076
1077 done:
1078         if (ap->a_eofflag)
1079                 *ap->a_eofflag = (error == ENOENT);
1080         uio->uio_offset = saveoff;
1081         if (error && cookie_index == 0) {
1082                 if (error == ENOENT)
1083                         error = 0;
1084                 if (cookies) {
1085                         kfree(cookies, M_TEMP);
1086                         *ap->a_ncookies = 0;
1087                         *ap->a_cookies = NULL;
1088                 }
1089         } else {
1090                 if (error == ENOENT)
1091                         error = 0;
1092                 if (cookies) {
1093                         *ap->a_ncookies = cookie_index;
1094                         *ap->a_cookies = cookies;
1095                 }
1096         }
1097         return(error);
1098 }
1099
1100 /*
1101  * hammer_vop_readlink { vp, uio, cred }
1102  */
1103 static
1104 int
1105 hammer_vop_readlink(struct vop_readlink_args *ap)
1106 {
1107         struct hammer_cursor cursor;
1108         struct hammer_inode *ip;
1109         int error;
1110
1111         ip = VTOI(ap->a_vp);
1112         hammer_init_cursor_hmp(&cursor, &ip->cache[0], ip->hmp);
1113
1114         /*
1115          * Key range (begin and end inclusive) to scan.  Directory keys
1116          * directly translate to a 64 bit 'seek' position.
1117          */
1118         cursor.key_beg.obj_id = ip->obj_id;
1119         cursor.key_beg.create_tid = 0;
1120         cursor.key_beg.delete_tid = 0;
1121         cursor.key_beg.rec_type = HAMMER_RECTYPE_FIX;
1122         cursor.key_beg.obj_type = 0;
1123         cursor.key_beg.key = HAMMER_FIXKEY_SYMLINK;
1124         cursor.asof = ip->obj_asof;
1125         cursor.flags |= HAMMER_CURSOR_ASOF;
1126
1127         error = hammer_ip_lookup(&cursor, ip);
1128         if (error == 0) {
1129                 error = hammer_ip_resolve_data(&cursor);
1130                 if (error == 0) {
1131                         error = uiomove((char *)cursor.data,
1132                                         cursor.record->generic.base.data_len,
1133                                         ap->a_uio);
1134                 }
1135         }
1136         hammer_done_cursor(&cursor);
1137         return(error);
1138 }
1139
1140 /*
1141  * hammer_vop_nremove { nch, dvp, cred }
1142  */
1143 static
1144 int
1145 hammer_vop_nremove(struct vop_nremove_args *ap)
1146 {
1147         return(hammer_dounlink(ap->a_nch, ap->a_dvp, ap->a_cred, 0));
1148 }
1149
1150 /*
1151  * hammer_vop_nrename { fnch, tnch, fdvp, tdvp, cred }
1152  */
1153 static
1154 int
1155 hammer_vop_nrename(struct vop_nrename_args *ap)
1156 {
1157         struct hammer_transaction trans;
1158         struct namecache *fncp;
1159         struct namecache *tncp;
1160         struct hammer_inode *fdip;
1161         struct hammer_inode *tdip;
1162         struct hammer_inode *ip;
1163         struct hammer_cursor cursor;
1164         union hammer_record_ondisk *rec;
1165         int64_t namekey;
1166         int error;
1167
1168         fdip = VTOI(ap->a_fdvp);
1169         tdip = VTOI(ap->a_tdvp);
1170         fncp = ap->a_fnch->ncp;
1171         tncp = ap->a_tnch->ncp;
1172         ip = VTOI(fncp->nc_vp);
1173         KKASSERT(ip != NULL);
1174
1175         if (fdip->flags & HAMMER_INODE_RO)
1176                 return (EROFS);
1177         if (tdip->flags & HAMMER_INODE_RO)
1178                 return (EROFS);
1179         if (ip->flags & HAMMER_INODE_RO)
1180                 return (EROFS);
1181
1182         hammer_start_transaction(&trans, fdip->hmp);
1183
1184         /*
1185          * Remove tncp from the target directory and then link ip as
1186          * tncp. XXX pass trans to dounlink
1187          *
1188          * Force the inode sync-time to match the transaction so it is
1189          * in-sync with the creation of the target directory entry.
1190          */
1191         error = hammer_dounlink(ap->a_tnch, ap->a_tdvp, ap->a_cred, 0);
1192         if (error == 0 || error == ENOENT) {
1193                 error = hammer_ip_add_directory(&trans, tdip, tncp, ip);
1194                 if (error == 0) {
1195                         ip->ino_data.parent_obj_id = tdip->obj_id;
1196                         hammer_modify_inode(&trans, ip,
1197                                 HAMMER_INODE_DDIRTY | HAMMER_INODE_TIDLOCKED);
1198                 }
1199         }
1200         if (error)
1201                 goto failed; /* XXX */
1202
1203         /*
1204          * Locate the record in the originating directory and remove it.
1205          *
1206          * Calculate the namekey and setup the key range for the scan.  This
1207          * works kinda like a chained hash table where the lower 32 bits
1208          * of the namekey synthesize the chain.
1209          *
1210          * The key range is inclusive of both key_beg and key_end.
1211          */
1212         namekey = hammer_directory_namekey(fncp->nc_name, fncp->nc_nlen);
1213 retry:
1214         hammer_init_cursor_hmp(&cursor, &fdip->cache[0], fdip->hmp);
1215         cursor.key_beg.obj_id = fdip->obj_id;
1216         cursor.key_beg.key = namekey;
1217         cursor.key_beg.create_tid = 0;
1218         cursor.key_beg.delete_tid = 0;
1219         cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1220         cursor.key_beg.obj_type = 0;
1221
1222         cursor.key_end = cursor.key_beg;
1223         cursor.key_end.key |= 0xFFFFFFFFULL;
1224         cursor.asof = fdip->obj_asof;
1225         cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
1226
1227         /*
1228          * Scan all matching records (the chain), locate the one matching
1229          * the requested path component.
1230          *
1231          * The hammer_ip_*() functions merge in-memory records with on-disk
1232          * records for the purposes of the search.
1233          */
1234         error = hammer_ip_first(&cursor, fdip);
1235         while (error == 0) {
1236                 if (hammer_ip_resolve_data(&cursor) != 0)
1237                         break;
1238                 rec = cursor.record;
1239                 if (fncp->nc_nlen == rec->entry.base.data_len &&
1240                     bcmp(fncp->nc_name, cursor.data, fncp->nc_nlen) == 0) {
1241                         break;
1242                 }
1243                 error = hammer_ip_next(&cursor);
1244         }
1245
1246         /*
1247          * If all is ok we have to get the inode so we can adjust nlinks.
1248          *
1249          * WARNING: hammer_ip_del_directory() may have to terminate the
1250          * cursor to avoid a recursion.  It's ok to call hammer_done_cursor()
1251          * twice.
1252          */
1253         if (error == 0)
1254                 error = hammer_ip_del_directory(&trans, &cursor, fdip, ip);
1255         hammer_done_cursor(&cursor);
1256         if (error == 0)
1257                 cache_rename(ap->a_fnch, ap->a_tnch);
1258         if (error == EDEADLK)
1259                 goto retry;
1260 failed:
1261         if (error == 0) {
1262                 hammer_commit_transaction(&trans);
1263         } else {
1264                 hammer_abort_transaction(&trans);
1265         }
1266         return (error);
1267 }
1268
1269 /*
1270  * hammer_vop_nrmdir { nch, dvp, cred }
1271  */
1272 static
1273 int
1274 hammer_vop_nrmdir(struct vop_nrmdir_args *ap)
1275 {
1276         return(hammer_dounlink(ap->a_nch, ap->a_dvp, ap->a_cred, 0));
1277 }
1278
1279 /*
1280  * hammer_vop_setattr { vp, vap, cred }
1281  */
1282 static
1283 int
1284 hammer_vop_setattr(struct vop_setattr_args *ap)
1285 {
1286         struct hammer_transaction trans;
1287         struct hammer_cursor *spike = NULL;
1288         struct vattr *vap;
1289         struct hammer_inode *ip;
1290         int modflags;
1291         int error;
1292         int truncating;
1293         int64_t aligned_size;
1294         u_int32_t flags;
1295         uuid_t uuid;
1296
1297         vap = ap->a_vap;
1298         ip = ap->a_vp->v_data;
1299         modflags = 0;
1300
1301         if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
1302                 return(EROFS);
1303         if (ip->flags & HAMMER_INODE_RO)
1304                 return (EROFS);
1305
1306         hammer_start_transaction(&trans, ip->hmp);
1307         error = 0;
1308
1309         if (vap->va_flags != VNOVAL) {
1310                 flags = ip->ino_data.uflags;
1311                 error = vop_helper_setattr_flags(&flags, vap->va_flags,
1312                                          hammer_to_unix_xid(&ip->ino_data.uid),
1313                                          ap->a_cred);
1314                 if (error == 0) {
1315                         if (ip->ino_data.uflags != flags) {
1316                                 ip->ino_data.uflags = flags;
1317                                 modflags |= HAMMER_INODE_DDIRTY;
1318                         }
1319                         if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) {
1320                                 error = 0;
1321                                 goto done;
1322                         }
1323                 }
1324                 goto done;
1325         }
1326         if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) {
1327                 error = EPERM;
1328                 goto done;
1329         }
1330         if (vap->va_uid != (uid_t)VNOVAL) {
1331                 hammer_guid_to_uuid(&uuid, vap->va_uid);
1332                 if (bcmp(&uuid, &ip->ino_data.uid, sizeof(uuid)) != 0) {
1333                         ip->ino_data.uid = uuid;
1334                         modflags |= HAMMER_INODE_DDIRTY;
1335                 }
1336         }
1337         if (vap->va_gid != (uid_t)VNOVAL) {
1338                 hammer_guid_to_uuid(&uuid, vap->va_gid);
1339                 if (bcmp(&uuid, &ip->ino_data.gid, sizeof(uuid)) != 0) {
1340                         ip->ino_data.gid = uuid;
1341                         modflags |= HAMMER_INODE_DDIRTY;
1342                 }
1343         }
1344         while (vap->va_size != VNOVAL && ip->ino_rec.ino_size != vap->va_size) {
1345                 switch(ap->a_vp->v_type) {
1346                 case VREG:
1347                         if (vap->va_size == ip->ino_rec.ino_size)
1348                                 break;
1349                         if (vap->va_size < ip->ino_rec.ino_size) {
1350                                 vtruncbuf(ap->a_vp, vap->va_size,
1351                                           HAMMER_BUFSIZE);
1352                                 truncating = 1;
1353                         } else {
1354                                 vnode_pager_setsize(ap->a_vp, vap->va_size);
1355                                 truncating = 0;
1356                         }
1357                         ip->ino_rec.ino_size = vap->va_size;
1358                         modflags |= HAMMER_INODE_RDIRTY;
1359                         aligned_size = (vap->va_size + HAMMER_BUFMASK) &
1360                                         ~(int64_t)HAMMER_BUFMASK;
1361
1362                         if (truncating) {
1363                                 error = hammer_ip_delete_range(&trans, ip,
1364                                                     aligned_size,
1365                                                     0x7FFFFFFFFFFFFFFFLL,
1366                                                     &spike);
1367                         }
1368                         /*
1369                          * If truncating we have to clean out a portion of
1370                          * the last block on-disk.
1371                          */
1372                         if (truncating && error == 0 &&
1373                             vap->va_size < aligned_size) {
1374                                 struct buf *bp;
1375                                 int offset;
1376
1377                                 offset = vap->va_size & HAMMER_BUFMASK;
1378                                 error = bread(ap->a_vp,
1379                                               aligned_size - HAMMER_BUFSIZE,
1380                                               HAMMER_BUFSIZE, &bp);
1381                                 if (error == 0) {
1382                                         bzero(bp->b_data + offset,
1383                                               HAMMER_BUFSIZE - offset);
1384                                         bdwrite(bp);
1385                                 } else {
1386                                         brelse(bp);
1387                                 }
1388                         }
1389                         break;
1390                 case VDATABASE:
1391                         error = hammer_ip_delete_range(&trans, ip,
1392                                                     vap->va_size,
1393                                                     0x7FFFFFFFFFFFFFFFLL,
1394                                                     &spike);
1395                         ip->ino_rec.ino_size = vap->va_size;
1396                         modflags |= HAMMER_INODE_RDIRTY;
1397                         break;
1398                 default:
1399                         error = EINVAL;
1400                         goto done;
1401                 }
1402                 if (error == ENOSPC) {
1403                         error = hammer_spike(&spike);
1404                         if (error == 0)
1405                                 continue;
1406                 }
1407                 KKASSERT(spike == NULL);
1408                 break;
1409         }
1410         if (vap->va_atime.tv_sec != VNOVAL) {
1411                 ip->ino_rec.ino_atime =
1412                         hammer_timespec_to_transid(&vap->va_atime);
1413                 modflags |= HAMMER_INODE_ITIMES;
1414         }
1415         if (vap->va_mtime.tv_sec != VNOVAL) {
1416                 ip->ino_rec.ino_mtime =
1417                         hammer_timespec_to_transid(&vap->va_mtime);
1418                 modflags |= HAMMER_INODE_ITIMES;
1419         }
1420         if (vap->va_mode != (mode_t)VNOVAL) {
1421                 if (ip->ino_data.mode != vap->va_mode) {
1422                         ip->ino_data.mode = vap->va_mode;
1423                         modflags |= HAMMER_INODE_DDIRTY;
1424                 }
1425         }
1426 done:
1427         if (error) {
1428                 hammer_abort_transaction(&trans);
1429         } else {
1430                 hammer_modify_inode(&trans, ip, modflags);
1431                 hammer_commit_transaction(&trans);
1432         }
1433         return (error);
1434 }
1435
1436 /*
1437  * hammer_vop_nsymlink { nch, dvp, vpp, cred, vap, target }
1438  */
1439 static
1440 int
1441 hammer_vop_nsymlink(struct vop_nsymlink_args *ap)
1442 {
1443         struct hammer_transaction trans;
1444         struct hammer_inode *dip;
1445         struct hammer_inode *nip;
1446         struct nchandle *nch;
1447         hammer_record_t record;
1448         int error;
1449         int bytes;
1450
1451         ap->a_vap->va_type = VLNK;
1452
1453         nch = ap->a_nch;
1454         dip = VTOI(ap->a_dvp);
1455
1456         if (dip->flags & HAMMER_INODE_RO)
1457                 return (EROFS);
1458
1459         /*
1460          * Create a transaction to cover the operations we perform.
1461          */
1462         hammer_start_transaction(&trans, dip->hmp);
1463
1464         /*
1465          * Create a new filesystem object of the requested type.  The
1466          * returned inode will be referenced but not locked.
1467          */
1468
1469         error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred, dip, &nip);
1470         if (error) {
1471                 hammer_abort_transaction(&trans);
1472                 *ap->a_vpp = NULL;
1473                 return (error);
1474         }
1475
1476         /*
1477          * Add the new filesystem object to the directory.  This will also
1478          * bump the inode's link count.
1479          */
1480         error = hammer_ip_add_directory(&trans, dip, nch->ncp, nip);
1481
1482         /*
1483          * Add a record representing the symlink.  symlink stores the link
1484          * as pure data, not a string, and is no \0 terminated.
1485          */
1486         if (error == 0) {
1487                 record = hammer_alloc_mem_record(nip);
1488                 bytes = strlen(ap->a_target);
1489
1490                 record->rec.generic.base.base.key = HAMMER_FIXKEY_SYMLINK;
1491                 record->rec.generic.base.base.rec_type = HAMMER_RECTYPE_FIX;
1492                 record->rec.generic.base.data_len = bytes;
1493                 if (bytes <= sizeof(record->rec.generic.filler)) {
1494                         record->data = (void *)record->rec.generic.filler;
1495                         bcopy(ap->a_target, record->data, bytes);
1496                 } else {
1497                         record->data = (void *)ap->a_target;
1498                         /* will be reallocated by routine below */
1499                 }
1500                 error = hammer_ip_add_record(&trans, record);
1501
1502                 /*
1503                  * Set the file size to the length of the link.
1504                  */
1505                 if (error == 0) {
1506                         nip->ino_rec.ino_size = bytes;
1507                         hammer_modify_inode(&trans, nip, HAMMER_INODE_RDIRTY);
1508                 }
1509         }
1510
1511         /*
1512          * Finish up.
1513          */
1514         if (error) {
1515                 hammer_rel_inode(nip, 0);
1516                 hammer_abort_transaction(&trans);
1517                 *ap->a_vpp = NULL;
1518         } else {
1519                 hammer_commit_transaction(&trans);
1520                 error = hammer_get_vnode(nip, LK_EXCLUSIVE, ap->a_vpp);
1521                 hammer_rel_inode(nip, 0);
1522                 if (error == 0) {
1523                         cache_setunresolved(ap->a_nch);
1524                         cache_setvp(ap->a_nch, *ap->a_vpp);
1525                 }
1526         }
1527         return (error);
1528 }
1529
1530 /*
1531  * hammer_vop_nwhiteout { nch, dvp, cred, flags }
1532  */
1533 static
1534 int
1535 hammer_vop_nwhiteout(struct vop_nwhiteout_args *ap)
1536 {
1537         return(hammer_dounlink(ap->a_nch, ap->a_dvp, ap->a_cred, ap->a_flags));
1538 }
1539
1540 /*
1541  * hammer_vop_ioctl { vp, command, data, fflag, cred }
1542  */
1543 static
1544 int
1545 hammer_vop_ioctl(struct vop_ioctl_args *ap)
1546 {
1547         struct hammer_inode *ip = ap->a_vp->v_data;
1548
1549         return(hammer_ioctl(ip, ap->a_command, ap->a_data,
1550                             ap->a_fflag, ap->a_cred));
1551 }
1552
1553 static
1554 int
1555 hammer_vop_mountctl(struct vop_mountctl_args *ap)
1556 {
1557         struct mount *mp;
1558         int error;
1559
1560         mp = ap->a_head.a_ops->head.vv_mount;
1561
1562         switch(ap->a_op) {
1563         case MOUNTCTL_SET_EXPORT:
1564                 if (ap->a_ctllen != sizeof(struct export_args))
1565                         error = EINVAL;
1566                 error = hammer_vfs_export(mp, ap->a_op,
1567                                       (const struct export_args *)ap->a_ctl);
1568                 break;
1569         default:
1570                 error = journal_mountctl(ap);
1571                 break;
1572         }
1573         return(error);
1574 }
1575
1576 /*
1577  * hammer_vop_strategy { vp, bio }
1578  *
1579  * Strategy call, used for regular file read & write only.  Note that the
1580  * bp may represent a cluster.
1581  *
1582  * To simplify operation and allow better optimizations in the future,
1583  * this code does not make any assumptions with regards to buffer alignment
1584  * or size.
1585  */
1586 static
1587 int
1588 hammer_vop_strategy(struct vop_strategy_args *ap)
1589 {
1590         struct buf *bp;
1591         int error;
1592
1593         bp = ap->a_bio->bio_buf;
1594
1595         switch(bp->b_cmd) {
1596         case BUF_CMD_READ:
1597                 error = hammer_vop_strategy_read(ap);
1598                 break;
1599         case BUF_CMD_WRITE:
1600                 error = hammer_vop_strategy_write(ap);
1601                 break;
1602         default:
1603                 error = EINVAL;
1604                 break;
1605         }
1606         bp->b_error = error;
1607         if (error)
1608                 bp->b_flags |= B_ERROR;
1609         biodone(ap->a_bio);
1610         return (error);
1611 }
1612
1613 /*
1614  * Read from a regular file.  Iterate the related records and fill in the
1615  * BIO/BUF.  Gaps are zero-filled.
1616  *
1617  * The support code in hammer_object.c should be used to deal with mixed
1618  * in-memory and on-disk records.
1619  *
1620  * XXX atime update
1621  */
1622 static
1623 int
1624 hammer_vop_strategy_read(struct vop_strategy_args *ap)
1625 {
1626         struct hammer_inode *ip = ap->a_vp->v_data;
1627         struct hammer_cursor cursor;
1628         hammer_record_ondisk_t rec;
1629         hammer_base_elm_t base;
1630         struct bio *bio;
1631         struct buf *bp;
1632         int64_t rec_offset;
1633         int64_t ran_end;
1634         int64_t tmp64;
1635         int error;
1636         int boff;
1637         int roff;
1638         int n;
1639
1640         bio = ap->a_bio;
1641         bp = bio->bio_buf;
1642
1643         hammer_init_cursor_hmp(&cursor, &ip->cache[0], ip->hmp);
1644
1645         /*
1646          * Key range (begin and end inclusive) to scan.  Note that the key's
1647          * stored in the actual records represent BASE+LEN, not BASE.  The
1648          * first record containing bio_offset will have a key > bio_offset.
1649          */
1650         cursor.key_beg.obj_id = ip->obj_id;
1651         cursor.key_beg.create_tid = 0;
1652         cursor.key_beg.delete_tid = 0;
1653         cursor.key_beg.obj_type = 0;
1654         cursor.key_beg.key = bio->bio_offset + 1;
1655         cursor.asof = ip->obj_asof;
1656         cursor.flags |= HAMMER_CURSOR_ASOF;
1657
1658         cursor.key_end = cursor.key_beg;
1659         if (ip->ino_rec.base.base.obj_type == HAMMER_OBJTYPE_DBFILE) {
1660                 cursor.key_beg.rec_type = HAMMER_RECTYPE_DB;
1661                 cursor.key_end.rec_type = HAMMER_RECTYPE_DB;
1662                 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
1663         } else {
1664                 ran_end = bio->bio_offset + bp->b_bufsize;
1665                 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
1666                 cursor.key_end.rec_type = HAMMER_RECTYPE_DATA;
1667                 tmp64 = ran_end + MAXPHYS + 1;  /* work-around GCC-4 bug */
1668                 if (tmp64 < ran_end)
1669                         cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
1670                 else
1671                         cursor.key_end.key = ran_end + MAXPHYS + 1;
1672         }
1673         cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
1674
1675         error = hammer_ip_first(&cursor, ip);
1676         boff = 0;
1677
1678         while (error == 0) {
1679                 error = hammer_ip_resolve_data(&cursor);
1680                 if (error)
1681                         break;
1682                 rec = cursor.record;
1683                 base = &rec->base.base;
1684
1685                 rec_offset = base->key - rec->data.base.data_len;
1686
1687                 /*
1688                  * Calculate the gap, if any, and zero-fill it.
1689                  */
1690                 n = (int)(rec_offset - (bio->bio_offset + boff));
1691                 if (n > 0) {
1692                         if (n > bp->b_bufsize - boff)
1693                                 n = bp->b_bufsize - boff;
1694                         bzero((char *)bp->b_data + boff, n);
1695                         boff += n;
1696                         n = 0;
1697                 }
1698
1699                 /*
1700                  * Calculate the data offset in the record and the number
1701                  * of bytes we can copy.
1702                  *
1703                  * Note there is a degenerate case here where boff may
1704                  * already be at bp->b_bufsize.
1705                  */
1706                 roff = -n;
1707                 n = rec->data.base.data_len - roff;
1708                 KKASSERT(n > 0);
1709                 if (n > bp->b_bufsize - boff)
1710                         n = bp->b_bufsize - boff;
1711                 bcopy((char *)cursor.data + roff, (char *)bp->b_data + boff, n);
1712                 boff += n;
1713                 if (boff == bp->b_bufsize)
1714                         break;
1715                 error = hammer_ip_next(&cursor);
1716         }
1717         hammer_done_cursor(&cursor);
1718
1719         /*
1720          * There may have been a gap after the last record
1721          */
1722         if (error == ENOENT)
1723                 error = 0;
1724         if (error == 0 && boff != bp->b_bufsize) {
1725                 KKASSERT(boff < bp->b_bufsize);
1726                 bzero((char *)bp->b_data + boff, bp->b_bufsize - boff);
1727                 /* boff = bp->b_bufsize; */
1728         }
1729         bp->b_resid = 0;
1730         return(error);
1731 }
1732
1733 /*
1734  * Write to a regular file.  Iterate the related records and mark for
1735  * deletion.  If existing edge records (left and right side) overlap our
1736  * write they have to be marked deleted and new records created, usually
1737  * referencing a portion of the original data.  Then add a record to
1738  * represent the buffer.
1739  *
1740  * The support code in hammer_object.c should be used to deal with mixed
1741  * in-memory and on-disk records.
1742  */
1743 static
1744 int
1745 hammer_vop_strategy_write(struct vop_strategy_args *ap)
1746 {
1747         struct hammer_transaction trans;
1748         struct hammer_cursor *spike = NULL;
1749         hammer_inode_t ip;
1750         struct bio *bio;
1751         struct buf *bp;
1752         int error;
1753
1754         bio = ap->a_bio;
1755         bp = bio->bio_buf;
1756         ip = ap->a_vp->v_data;
1757
1758         if (ip->flags & HAMMER_INODE_RO)
1759                 return (EROFS);
1760
1761         /*
1762          * Start a transaction using the TID stored with the bp.
1763          */
1764         KKASSERT(bp->b_tid != 0);
1765         hammer_start_transaction_tid(&trans, ip->hmp, bp->b_tid);
1766
1767 retry:
1768         /*
1769          * Delete any records overlapping our range.  This function will
1770          * (eventually) properly truncate partial overlaps.
1771          */
1772         if (ip->ino_rec.base.base.obj_type == HAMMER_OBJTYPE_DBFILE) {
1773                 error = hammer_ip_delete_range(&trans, ip, bio->bio_offset,
1774                                                bio->bio_offset, &spike);
1775         } else {
1776                 error = hammer_ip_delete_range(&trans, ip, bio->bio_offset,
1777                                                bio->bio_offset +
1778                                                 bp->b_bufsize - 1,
1779                                                &spike);
1780         }
1781
1782         /*
1783          * Add a single record to cover the write
1784          */
1785         if (error == 0) {
1786                 error = hammer_ip_sync_data(&trans, ip, bio->bio_offset,
1787                                             bp->b_data, bp->b_bufsize,
1788                                             &spike);
1789         }
1790
1791         /*
1792          * If we ran out of space the spike structure will be filled in
1793          * and we must call hammer_spike with it, then retry.
1794          */
1795         if (error == ENOSPC) {
1796                 error = hammer_spike(&spike);
1797                 if (error == 0)
1798                         goto retry;
1799         }
1800         KKASSERT(spike == NULL);
1801
1802         /*
1803          * If an error occured abort the transaction
1804          */
1805         if (error) {
1806                 /* XXX undo deletion */
1807                 hammer_abort_transaction(&trans);
1808                 bp->b_resid = bp->b_bufsize;
1809         } else {
1810                 hammer_commit_transaction(&trans);
1811                 bp->b_resid = 0;
1812                 bp->b_tid = 0;
1813         }
1814         return(error);
1815 }
1816
1817 /*
1818  * dounlink - disconnect a directory entry
1819  *
1820  * XXX whiteout support not really in yet
1821  */
1822 static int
1823 hammer_dounlink(struct nchandle *nch, struct vnode *dvp, struct ucred *cred,
1824                 int flags)
1825 {
1826         struct hammer_transaction trans;
1827         struct namecache *ncp;
1828         hammer_inode_t dip;
1829         hammer_inode_t ip;
1830         hammer_record_ondisk_t rec;
1831         struct hammer_cursor cursor;
1832         int64_t namekey;
1833         int error;
1834
1835         /*
1836          * Calculate the namekey and setup the key range for the scan.  This
1837          * works kinda like a chained hash table where the lower 32 bits
1838          * of the namekey synthesize the chain.
1839          *
1840          * The key range is inclusive of both key_beg and key_end.
1841          */
1842         dip = VTOI(dvp);
1843         ncp = nch->ncp;
1844
1845         if (dip->flags & HAMMER_INODE_RO)
1846                 return (EROFS);
1847
1848         hammer_start_transaction(&trans, dip->hmp);
1849
1850         namekey = hammer_directory_namekey(ncp->nc_name, ncp->nc_nlen);
1851 retry:
1852         hammer_init_cursor_hmp(&cursor, &dip->cache[0], dip->hmp);
1853         cursor.key_beg.obj_id = dip->obj_id;
1854         cursor.key_beg.key = namekey;
1855         cursor.key_beg.create_tid = 0;
1856         cursor.key_beg.delete_tid = 0;
1857         cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1858         cursor.key_beg.obj_type = 0;
1859
1860         cursor.key_end = cursor.key_beg;
1861         cursor.key_end.key |= 0xFFFFFFFFULL;
1862         cursor.asof = dip->obj_asof;
1863         cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
1864
1865         /*
1866          * Scan all matching records (the chain), locate the one matching
1867          * the requested path component.  info->last_error contains the
1868          * error code on search termination and could be 0, ENOENT, or
1869          * something else.
1870          *
1871          * The hammer_ip_*() functions merge in-memory records with on-disk
1872          * records for the purposes of the search.
1873          */
1874         error = hammer_ip_first(&cursor, dip);
1875         while (error == 0) {
1876                 error = hammer_ip_resolve_data(&cursor);
1877                 if (error)
1878                         break;
1879                 rec = cursor.record;
1880                 if (ncp->nc_nlen == rec->entry.base.data_len &&
1881                     bcmp(ncp->nc_name, cursor.data, ncp->nc_nlen) == 0) {
1882                         break;
1883                 }
1884                 error = hammer_ip_next(&cursor);
1885         }
1886
1887         /*
1888          * If all is ok we have to get the inode so we can adjust nlinks.
1889          *
1890          * If the target is a directory, it must be empty.
1891          */
1892         if (error == 0) {
1893                 ip = hammer_get_inode(dip->hmp, &dip->cache[1],
1894                                       rec->entry.obj_id,
1895                                       dip->hmp->asof, 0, &error);
1896                 if (error == ENOENT) {
1897                         kprintf("obj_id %016llx\n", rec->entry.obj_id);
1898                         Debugger("ENOENT unlinking object that should exist, cont to sync");
1899                         hammer_sync_hmp(dip->hmp, MNT_NOWAIT);
1900                         Debugger("ENOENT - sync done");
1901                 }
1902                 if (error == 0 && ip->ino_rec.base.base.obj_type ==
1903                                   HAMMER_OBJTYPE_DIRECTORY) {
1904                         error = hammer_ip_check_directory_empty(&trans, ip);
1905                 }
1906                 /*
1907                  * WARNING: hammer_ip_del_directory() may have to terminate
1908                  * the cursor to avoid a lock recursion.  It's ok to call
1909                  * hammer_done_cursor() twice.
1910                  */
1911                 if (error == 0)
1912                         error = hammer_ip_del_directory(&trans, &cursor, dip, ip);
1913                 if (error == 0) {
1914                         cache_setunresolved(nch);
1915                         cache_setvp(nch, NULL);
1916                         /* XXX locking */
1917                         if (ip->vp)
1918                                 cache_inval_vp(ip->vp, CINV_DESTROY);
1919                 }
1920                 hammer_rel_inode(ip, 0);
1921         }
1922         hammer_done_cursor(&cursor);
1923         if (error == EDEADLK)
1924                 goto retry;
1925
1926         if (error == 0)
1927                 hammer_commit_transaction(&trans);
1928         else
1929                 hammer_abort_transaction(&trans);
1930         return (error);
1931 }
1932
1933 /************************************************************************
1934  *                          FIFO AND SPECFS OPS                         *
1935  ************************************************************************
1936  *
1937  */
1938
1939 static int
1940 hammer_vop_fifoclose (struct vop_close_args *ap)
1941 {
1942         /* XXX update itimes */
1943         return (VOCALL(&fifo_vnode_vops, &ap->a_head));
1944 }
1945
1946 static int
1947 hammer_vop_fiforead (struct vop_read_args *ap)
1948 {
1949         int error;
1950
1951         error = VOCALL(&fifo_vnode_vops, &ap->a_head);
1952         /* XXX update access time */
1953         return (error);
1954 }
1955
1956 static int
1957 hammer_vop_fifowrite (struct vop_write_args *ap)
1958 {
1959         int error;
1960
1961         error = VOCALL(&fifo_vnode_vops, &ap->a_head);
1962         /* XXX update access time */
1963         return (error);
1964 }
1965
1966 static int
1967 hammer_vop_specclose (struct vop_close_args *ap)
1968 {
1969         /* XXX update itimes */
1970         return (VOCALL(&spec_vnode_vops, &ap->a_head));
1971 }
1972
1973 static int
1974 hammer_vop_specread (struct vop_read_args *ap)
1975 {
1976         /* XXX update access time */
1977         return (VOCALL(&spec_vnode_vops, &ap->a_head));
1978 }
1979
1980 static int
1981 hammer_vop_specwrite (struct vop_write_args *ap)
1982 {
1983         /* XXX update last change time */
1984         return (VOCALL(&spec_vnode_vops, &ap->a_head));
1985 }
1986