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