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