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