HAMMER 61F/Many: Stabilization w/ simultanious pruning and reblocking
[dragonfly.git] / sys / vfs / hammer / hammer_vnops.c
1 /*
2  * Copyright (c) 2007-2008 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.91 2008/07/14 03:20: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_bmap(struct vop_bmap_args *ap);
79 static int hammer_vop_nsymlink(struct vop_nsymlink_args *);
80 static int hammer_vop_nwhiteout(struct vop_nwhiteout_args *);
81 static int hammer_vop_ioctl(struct vop_ioctl_args *);
82 static int hammer_vop_mountctl(struct vop_mountctl_args *);
83
84 static int hammer_vop_fifoclose (struct vop_close_args *);
85 static int hammer_vop_fiforead (struct vop_read_args *);
86 static int hammer_vop_fifowrite (struct vop_write_args *);
87
88 static int hammer_vop_specclose (struct vop_close_args *);
89 static int hammer_vop_specread (struct vop_read_args *);
90 static int hammer_vop_specwrite (struct vop_write_args *);
91
92 struct vop_ops hammer_vnode_vops = {
93         .vop_default =          vop_defaultop,
94         .vop_fsync =            hammer_vop_fsync,
95         .vop_getpages =         vop_stdgetpages,
96         .vop_putpages =         vop_stdputpages,
97         .vop_read =             hammer_vop_read,
98         .vop_write =            hammer_vop_write,
99         .vop_access =           hammer_vop_access,
100         .vop_advlock =          hammer_vop_advlock,
101         .vop_close =            hammer_vop_close,
102         .vop_ncreate =          hammer_vop_ncreate,
103         .vop_getattr =          hammer_vop_getattr,
104         .vop_inactive =         hammer_vop_inactive,
105         .vop_reclaim =          hammer_vop_reclaim,
106         .vop_nresolve =         hammer_vop_nresolve,
107         .vop_nlookupdotdot =    hammer_vop_nlookupdotdot,
108         .vop_nlink =            hammer_vop_nlink,
109         .vop_nmkdir =           hammer_vop_nmkdir,
110         .vop_nmknod =           hammer_vop_nmknod,
111         .vop_open =             hammer_vop_open,
112         .vop_pathconf =         hammer_vop_pathconf,
113         .vop_print =            hammer_vop_print,
114         .vop_readdir =          hammer_vop_readdir,
115         .vop_readlink =         hammer_vop_readlink,
116         .vop_nremove =          hammer_vop_nremove,
117         .vop_nrename =          hammer_vop_nrename,
118         .vop_nrmdir =           hammer_vop_nrmdir,
119         .vop_setattr =          hammer_vop_setattr,
120         .vop_bmap =             hammer_vop_bmap,
121         .vop_strategy =         hammer_vop_strategy,
122         .vop_nsymlink =         hammer_vop_nsymlink,
123         .vop_nwhiteout =        hammer_vop_nwhiteout,
124         .vop_ioctl =            hammer_vop_ioctl,
125         .vop_mountctl =         hammer_vop_mountctl
126 };
127
128 struct vop_ops hammer_spec_vops = {
129         .vop_default =          spec_vnoperate,
130         .vop_fsync =            hammer_vop_fsync,
131         .vop_read =             hammer_vop_specread,
132         .vop_write =            hammer_vop_specwrite,
133         .vop_access =           hammer_vop_access,
134         .vop_close =            hammer_vop_specclose,
135         .vop_getattr =          hammer_vop_getattr,
136         .vop_inactive =         hammer_vop_inactive,
137         .vop_reclaim =          hammer_vop_reclaim,
138         .vop_setattr =          hammer_vop_setattr
139 };
140
141 struct vop_ops hammer_fifo_vops = {
142         .vop_default =          fifo_vnoperate,
143         .vop_fsync =            hammer_vop_fsync,
144         .vop_read =             hammer_vop_fiforead,
145         .vop_write =            hammer_vop_fifowrite,
146         .vop_access =           hammer_vop_access,
147         .vop_close =            hammer_vop_fifoclose,
148         .vop_getattr =          hammer_vop_getattr,
149         .vop_inactive =         hammer_vop_inactive,
150         .vop_reclaim =          hammer_vop_reclaim,
151         .vop_setattr =          hammer_vop_setattr
152 };
153
154 #ifdef DEBUG_TRUNCATE
155 struct hammer_inode *HammerTruncIp;
156 #endif
157
158 static int hammer_dounlink(hammer_transaction_t trans, struct nchandle *nch,
159                            struct vnode *dvp, struct ucred *cred, int flags);
160 static int hammer_vop_strategy_read(struct vop_strategy_args *ap);
161 static int hammer_vop_strategy_write(struct vop_strategy_args *ap);
162
163 #if 0
164 static
165 int
166 hammer_vop_vnoperate(struct vop_generic_args *)
167 {
168         return (VOCALL(&hammer_vnode_vops, ap));
169 }
170 #endif
171
172 /*
173  * hammer_vop_fsync { vp, waitfor }
174  *
175  * fsync() an inode to disk and wait for it to be completely committed
176  * such that the information would not be undone if a crash occured after
177  * return.
178  */
179 static
180 int
181 hammer_vop_fsync(struct vop_fsync_args *ap)
182 {
183         hammer_inode_t ip = VTOI(ap->a_vp);
184
185         ++hammer_count_fsyncs;
186         vfsync(ap->a_vp, ap->a_waitfor, 1, NULL, NULL);
187         hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
188         if (ap->a_waitfor == MNT_WAIT)
189                 hammer_wait_inode(ip);
190         return (ip->error);
191 }
192
193 /*
194  * hammer_vop_read { vp, uio, ioflag, cred }
195  */
196 static
197 int
198 hammer_vop_read(struct vop_read_args *ap)
199 {
200         struct hammer_transaction trans;
201         hammer_inode_t ip;
202         off_t offset;
203         struct buf *bp;
204         struct uio *uio;
205         int error;
206         int n;
207         int seqcount;
208         int ioseqcount;
209         int blksize;
210
211         if (ap->a_vp->v_type != VREG)
212                 return (EINVAL);
213         ip = VTOI(ap->a_vp);
214         error = 0;
215         uio = ap->a_uio;
216
217         /*
218          * Allow the UIO's size to override the sequential heuristic.
219          */
220         blksize = hammer_blocksize(uio->uio_offset);
221         seqcount = (uio->uio_resid + (blksize - 1)) / blksize;
222         ioseqcount = ap->a_ioflag >> 16;
223         if (seqcount < ioseqcount)
224                 seqcount = ioseqcount;
225
226         hammer_start_transaction(&trans, ip->hmp);
227
228         /*
229          * Access the data typically in HAMMER_BUFSIZE blocks via the
230          * buffer cache, but HAMMER may use a variable block size based
231          * on the offset.
232          */
233         while (uio->uio_resid > 0 && uio->uio_offset < ip->ino_data.size) {
234                 int64_t base_offset;
235                 int64_t file_limit;
236
237                 blksize = hammer_blocksize(uio->uio_offset);
238                 offset = (int)uio->uio_offset & (blksize - 1);
239                 base_offset = uio->uio_offset - offset;
240
241                 if (hammer_cluster_enable) {
242                         /*
243                          * Use file_limit to prevent cluster_read() from
244                          * creating buffers of the wrong block size past
245                          * the demarc.
246                          */
247                         file_limit = ip->ino_data.size;
248                         if (base_offset < HAMMER_XDEMARC &&
249                             file_limit > HAMMER_XDEMARC) {
250                                 file_limit = HAMMER_XDEMARC;
251                         }
252                         error = cluster_read(ap->a_vp,
253                                              file_limit, base_offset,
254                                              blksize, MAXPHYS,
255                                              seqcount, &bp);
256                 } else {
257                         error = bread(ap->a_vp, base_offset, blksize, &bp);
258                 }
259                 if (error) {
260                         kprintf("error %d\n", error);
261                         brelse(bp);
262                         break;
263                 }
264
265                 /* bp->b_flags |= B_CLUSTEROK; temporarily disabled */
266                 n = blksize - offset;
267                 if (n > uio->uio_resid)
268                         n = uio->uio_resid;
269                 if (n > ip->ino_data.size - uio->uio_offset)
270                         n = (int)(ip->ino_data.size - uio->uio_offset);
271                 error = uiomove((char *)bp->b_data + offset, n, uio);
272
273                 /* data has a lower priority then meta-data */
274                 bp->b_flags |= B_AGE;
275                 bqrelse(bp);
276                 if (error)
277                         break;
278         }
279         if ((ip->flags & HAMMER_INODE_RO) == 0 &&
280             (ip->hmp->mp->mnt_flag & MNT_NOATIME) == 0) {
281                 ip->ino_data.atime = trans.time;
282                 hammer_modify_inode(ip, HAMMER_INODE_ATIME);
283         }
284         hammer_done_transaction(&trans);
285         return (error);
286 }
287
288 /*
289  * hammer_vop_write { vp, uio, ioflag, cred }
290  */
291 static
292 int
293 hammer_vop_write(struct vop_write_args *ap)
294 {
295         struct hammer_transaction trans;
296         struct hammer_inode *ip;
297         hammer_mount_t hmp;
298         struct uio *uio;
299         int offset;
300         off_t base_offset;
301         struct buf *bp;
302         int error;
303         int n;
304         int flags;
305         int delta;
306         int seqcount;
307
308         if (ap->a_vp->v_type != VREG)
309                 return (EINVAL);
310         ip = VTOI(ap->a_vp);
311         hmp = ip->hmp;
312         error = 0;
313         seqcount = ap->a_ioflag >> 16;
314
315         if (ip->flags & HAMMER_INODE_RO)
316                 return (EROFS);
317
318         /*
319          * Create a transaction to cover the operations we perform.
320          */
321         hammer_start_transaction(&trans, hmp);
322         uio = ap->a_uio;
323
324         /*
325          * Check append mode
326          */
327         if (ap->a_ioflag & IO_APPEND)
328                 uio->uio_offset = ip->ino_data.size;
329
330         /*
331          * Check for illegal write offsets.  Valid range is 0...2^63-1.
332          *
333          * NOTE: the base_off assignment is required to work around what
334          * I consider to be a GCC-4 optimization bug.
335          */
336         if (uio->uio_offset < 0) {
337                 hammer_done_transaction(&trans);
338                 return (EFBIG);
339         }
340         base_offset = uio->uio_offset + uio->uio_resid; /* work around gcc-4 */
341         if (uio->uio_resid > 0 && base_offset <= 0) {
342                 hammer_done_transaction(&trans);
343                 return (EFBIG);
344         }
345
346         /*
347          * Access the data typically in HAMMER_BUFSIZE blocks via the
348          * buffer cache, but HAMMER may use a variable block size based
349          * on the offset.
350          */
351         while (uio->uio_resid > 0) {
352                 int fixsize = 0;
353                 int blksize;
354                 int blkmask;
355
356                 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_WRITE)) != 0)
357                         break;
358
359                 blksize = hammer_blocksize(uio->uio_offset);
360
361                 /*
362                  * Do not allow HAMMER to blow out the buffer cache.  Very
363                  * large UIOs can lockout other processes due to bwillwrite()
364                  * mechanics.
365                  *
366                  * The hammer inode is not locked during these operations.
367                  * The vnode is locked which can interfere with the pageout
368                  * daemon for non-UIO_NOCOPY writes but should not interfere
369                  * with the buffer cache.  Even so, we cannot afford to
370                  * allow the pageout daemon to build up too many dirty buffer
371                  * cache buffers.
372                  */
373                 /*if (((int)uio->uio_offset & (blksize - 1)) == 0)*/
374                 bwillwrite(blksize);
375
376                 /*
377                  * Do not allow HAMMER to blow out system memory by
378                  * accumulating too many records.   Records are so well
379                  * decoupled from the buffer cache that it is possible
380                  * for userland to push data out to the media via
381                  * direct-write, but build up the records queued to the
382                  * backend faster then the backend can flush them out.
383                  * HAMMER has hit its write limit but the frontend has
384                  * no pushback to slow it down.
385                  */
386                 if (hmp->rsv_recs > hammer_limit_recs / 2) {
387                         /*
388                          * Get the inode on the flush list
389                          */
390                         if (ip->rsv_recs >= 64)
391                                 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
392                         else if (ip->rsv_recs >= 16)
393                                 hammer_flush_inode(ip, 0);
394
395                         /*
396                          * Keep the flusher going if the system keeps
397                          * queueing records.
398                          */
399                         delta = hmp->count_newrecords -
400                                 hmp->last_newrecords;
401                         if (delta < 0 || delta > hammer_limit_recs / 2) {
402                                 hmp->last_newrecords = hmp->count_newrecords;
403                                 hammer_sync_hmp(hmp, MNT_NOWAIT);
404                         }
405
406                         /*
407                          * If we have gotten behind start slowing
408                          * down the writers.
409                          */
410                         delta = (hmp->rsv_recs - hammer_limit_recs) *
411                                 hz / hammer_limit_recs;
412                         if (delta > 0)
413                                 tsleep(&trans, 0, "hmrslo", delta);
414                 }
415
416                 /*
417                  * Calculate the blocksize at the current offset and figure
418                  * out how much we can actually write.
419                  */
420                 blkmask = blksize - 1;
421                 offset = (int)uio->uio_offset & blkmask;
422                 base_offset = uio->uio_offset & ~(int64_t)blkmask;
423                 n = blksize - offset;
424                 if (n > uio->uio_resid)
425                         n = uio->uio_resid;
426                 if (uio->uio_offset + n > ip->ino_data.size) {
427                         vnode_pager_setsize(ap->a_vp, uio->uio_offset + n);
428                         fixsize = 1;
429                 }
430
431                 if (uio->uio_segflg == UIO_NOCOPY) {
432                         /*
433                          * Issuing a write with the same data backing the
434                          * buffer.  Instantiate the buffer to collect the
435                          * backing vm pages, then read-in any missing bits.
436                          *
437                          * This case is used by vop_stdputpages().
438                          */
439                         bp = getblk(ap->a_vp, base_offset,
440                                     blksize, GETBLK_BHEAVY, 0);
441                         if ((bp->b_flags & B_CACHE) == 0) {
442                                 bqrelse(bp);
443                                 error = bread(ap->a_vp, base_offset,
444                                               blksize, &bp);
445                         }
446                 } else if (offset == 0 && uio->uio_resid >= blksize) {
447                         /*
448                          * Even though we are entirely overwriting the buffer
449                          * we may still have to zero it out to avoid a 
450                          * mmap/write visibility issue.
451                          */
452                         bp = getblk(ap->a_vp, base_offset, blksize, GETBLK_BHEAVY, 0);
453                         if ((bp->b_flags & B_CACHE) == 0)
454                                 vfs_bio_clrbuf(bp);
455                 } else if (base_offset >= ip->ino_data.size) {
456                         /*
457                          * If the base offset of the buffer is beyond the
458                          * file EOF, we don't have to issue a read.
459                          */
460                         bp = getblk(ap->a_vp, base_offset,
461                                     blksize, GETBLK_BHEAVY, 0);
462                         vfs_bio_clrbuf(bp);
463                 } else {
464                         /*
465                          * Partial overwrite, read in any missing bits then
466                          * replace the portion being written.
467                          */
468                         error = bread(ap->a_vp, base_offset, blksize, &bp);
469                         if (error == 0)
470                                 bheavy(bp);
471                 }
472                 if (error == 0) {
473                         error = uiomove((char *)bp->b_data + offset,
474                                         n, uio);
475                 }
476
477                 /*
478                  * If we screwed up we have to undo any VM size changes we
479                  * made.
480                  */
481                 if (error) {
482                         brelse(bp);
483                         if (fixsize) {
484                                 vtruncbuf(ap->a_vp, ip->ino_data.size,
485                                           hammer_blocksize(ip->ino_data.size));
486                         }
487                         break;
488                 }
489                 /* bp->b_flags |= B_CLUSTEROK; temporarily disabled */
490                 if (ip->ino_data.size < uio->uio_offset) {
491                         ip->ino_data.size = uio->uio_offset;
492                         flags = HAMMER_INODE_DDIRTY;
493                         vnode_pager_setsize(ap->a_vp, ip->ino_data.size);
494                 } else {
495                         flags = 0;
496                 }
497                 ip->ino_data.mtime = trans.time;
498                 flags |= HAMMER_INODE_MTIME | HAMMER_INODE_BUFS;
499                 hammer_modify_inode(ip, flags);
500
501                 /*
502                  * Once we dirty the buffer any cached zone-X offset
503                  * becomes invalid.  HAMMER NOTE: no-history mode cannot 
504                  * allow overwriting over the same data sector unless
505                  * we provide UNDOs for the old data, which we don't.
506                  */
507                 bp->b_bio2.bio_offset = NOOFFSET;
508
509                 /*
510                  * Final buffer disposition.
511                  */
512                 bp->b_flags |= B_AGE;
513                 if (ap->a_ioflag & IO_SYNC) {
514                         bwrite(bp);
515                 } else if (ap->a_ioflag & IO_DIRECT) {
516                         bawrite(bp);
517                 } else {
518                         bdwrite(bp);
519                 }
520         }
521         hammer_done_transaction(&trans);
522         return (error);
523 }
524
525 /*
526  * hammer_vop_access { vp, mode, cred }
527  */
528 static
529 int
530 hammer_vop_access(struct vop_access_args *ap)
531 {
532         struct hammer_inode *ip = VTOI(ap->a_vp);
533         uid_t uid;
534         gid_t gid;
535         int error;
536
537         uid = hammer_to_unix_xid(&ip->ino_data.uid);
538         gid = hammer_to_unix_xid(&ip->ino_data.gid);
539
540         error = vop_helper_access(ap, uid, gid, ip->ino_data.mode,
541                                   ip->ino_data.uflags);
542         return (error);
543 }
544
545 /*
546  * hammer_vop_advlock { vp, id, op, fl, flags }
547  */
548 static
549 int
550 hammer_vop_advlock(struct vop_advlock_args *ap)
551 {
552         hammer_inode_t ip = VTOI(ap->a_vp);
553
554         return (lf_advlock(ap, &ip->advlock, ip->ino_data.size));
555 }
556
557 /*
558  * hammer_vop_close { vp, fflag }
559  */
560 static
561 int
562 hammer_vop_close(struct vop_close_args *ap)
563 {
564         hammer_inode_t ip = VTOI(ap->a_vp);
565
566         if ((ip->flags | ip->sync_flags) & HAMMER_INODE_MODMASK)
567                 hammer_inode_waitreclaims(ip->hmp);
568         return (vop_stdclose(ap));
569 }
570
571 /*
572  * hammer_vop_ncreate { nch, dvp, vpp, cred, vap }
573  *
574  * The operating system has already ensured that the directory entry
575  * does not exist and done all appropriate namespace locking.
576  */
577 static
578 int
579 hammer_vop_ncreate(struct vop_ncreate_args *ap)
580 {
581         struct hammer_transaction trans;
582         struct hammer_inode *dip;
583         struct hammer_inode *nip;
584         struct nchandle *nch;
585         int error;
586
587         nch = ap->a_nch;
588         dip = VTOI(ap->a_dvp);
589
590         if (dip->flags & HAMMER_INODE_RO)
591                 return (EROFS);
592         if ((error = hammer_checkspace(dip->hmp, HAMMER_CHKSPC_CREATE)) != 0)
593                 return (error);
594
595         /*
596          * Create a transaction to cover the operations we perform.
597          */
598         hammer_start_transaction(&trans, dip->hmp);
599
600         /*
601          * Create a new filesystem object of the requested type.  The
602          * returned inode will be referenced and shared-locked to prevent
603          * it from being moved to the flusher.
604          */
605
606         error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
607                                     dip, NULL, &nip);
608         if (error) {
609                 hkprintf("hammer_create_inode error %d\n", error);
610                 hammer_done_transaction(&trans);
611                 *ap->a_vpp = NULL;
612                 return (error);
613         }
614
615         /*
616          * Add the new filesystem object to the directory.  This will also
617          * bump the inode's link count.
618          */
619         error = hammer_ip_add_directory(&trans, dip,
620                                         nch->ncp->nc_name, nch->ncp->nc_nlen,
621                                         nip);
622         if (error)
623                 hkprintf("hammer_ip_add_directory error %d\n", error);
624
625         /*
626          * Finish up.
627          */
628         if (error) {
629                 hammer_rel_inode(nip, 0);
630                 hammer_done_transaction(&trans);
631                 *ap->a_vpp = NULL;
632         } else {
633                 error = hammer_get_vnode(nip, ap->a_vpp);
634                 hammer_done_transaction(&trans);
635                 hammer_rel_inode(nip, 0);
636                 if (error == 0) {
637                         cache_setunresolved(ap->a_nch);
638                         cache_setvp(ap->a_nch, *ap->a_vpp);
639                 }
640         }
641         return (error);
642 }
643
644 /*
645  * hammer_vop_getattr { vp, vap }
646  *
647  * Retrieve an inode's attribute information.  When accessing inodes
648  * historically we fake the atime field to ensure consistent results.
649  * The atime field is stored in the B-Tree element and allowed to be
650  * updated without cycling the element.
651  */
652 static
653 int
654 hammer_vop_getattr(struct vop_getattr_args *ap)
655 {
656         struct hammer_inode *ip = VTOI(ap->a_vp);
657         struct vattr *vap = ap->a_vap;
658
659         /*
660          * We want the fsid to be different when accessing a filesystem
661          * with different as-of's so programs like diff don't think
662          * the files are the same.
663          *
664          * We also want the fsid to be the same when comparing snapshots,
665          * or when comparing mirrors (which might be backed by different
666          * physical devices).  HAMMER fsids are based on the PFS's
667          * shared_uuid field.
668          *
669          * XXX there is a chance of collision here.  The va_fsid reported
670          * by stat is different from the more involved fsid used in the
671          * mount structure.
672          */
673         vap->va_fsid = ip->pfsm->fsid_udev ^ (u_int32_t)ip->obj_asof ^
674                        (u_int32_t)(ip->obj_asof >> 32);
675
676         vap->va_fileid = ip->ino_leaf.base.obj_id;
677         vap->va_mode = ip->ino_data.mode;
678         vap->va_nlink = ip->ino_data.nlinks;
679         vap->va_uid = hammer_to_unix_xid(&ip->ino_data.uid);
680         vap->va_gid = hammer_to_unix_xid(&ip->ino_data.gid);
681         vap->va_rmajor = 0;
682         vap->va_rminor = 0;
683         vap->va_size = ip->ino_data.size;
684
685         /*
686          * We must provide a consistent atime and mtime for snapshots
687          * so people can do a 'tar cf - ... | md5' on them and get
688          * consistent results.
689          */
690         if (ip->flags & HAMMER_INODE_RO) {
691                 hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_atime);
692                 hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_mtime);
693         } else {
694                 hammer_time_to_timespec(ip->ino_data.atime, &vap->va_atime);
695                 hammer_time_to_timespec(ip->ino_data.mtime, &vap->va_mtime);
696         }
697         hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_ctime);
698         vap->va_flags = ip->ino_data.uflags;
699         vap->va_gen = 1;        /* hammer inums are unique for all time */
700         vap->va_blocksize = HAMMER_BUFSIZE;
701         if (ip->ino_data.size >= HAMMER_XDEMARC) {
702                 vap->va_bytes = (ip->ino_data.size + HAMMER_XBUFMASK64) &
703                                 ~HAMMER_XBUFMASK64;
704         } else if (ip->ino_data.size > HAMMER_BUFSIZE / 2) {
705                 vap->va_bytes = (ip->ino_data.size + HAMMER_BUFMASK64) &
706                                 ~HAMMER_BUFMASK64;
707         } else {
708                 vap->va_bytes = (ip->ino_data.size + 15) & ~15;
709         }
710         vap->va_type = hammer_get_vnode_type(ip->ino_data.obj_type);
711         vap->va_filerev = 0;    /* XXX */
712         /* mtime uniquely identifies any adjustments made to the file XXX */
713         vap->va_fsmid = ip->ino_data.mtime;
714         vap->va_uid_uuid = ip->ino_data.uid;
715         vap->va_gid_uuid = ip->ino_data.gid;
716         vap->va_fsid_uuid = ip->hmp->fsid;
717         vap->va_vaflags = VA_UID_UUID_VALID | VA_GID_UUID_VALID |
718                           VA_FSID_UUID_VALID;
719
720         switch (ip->ino_data.obj_type) {
721         case HAMMER_OBJTYPE_CDEV:
722         case HAMMER_OBJTYPE_BDEV:
723                 vap->va_rmajor = ip->ino_data.rmajor;
724                 vap->va_rminor = ip->ino_data.rminor;
725                 break;
726         default:
727                 break;
728         }
729         return(0);
730 }
731
732 /*
733  * hammer_vop_nresolve { nch, dvp, cred }
734  *
735  * Locate the requested directory entry.
736  */
737 static
738 int
739 hammer_vop_nresolve(struct vop_nresolve_args *ap)
740 {
741         struct hammer_transaction trans;
742         struct namecache *ncp;
743         hammer_inode_t dip;
744         hammer_inode_t ip;
745         hammer_tid_t asof;
746         struct hammer_cursor cursor;
747         struct vnode *vp;
748         int64_t namekey;
749         int error;
750         int i;
751         int nlen;
752         int flags;
753         int ispfs;
754         int64_t obj_id;
755         u_int32_t localization;
756
757         /*
758          * Misc initialization, plus handle as-of name extensions.  Look for
759          * the '@@' extension.  Note that as-of files and directories cannot
760          * be modified.
761          */
762         dip = VTOI(ap->a_dvp);
763         ncp = ap->a_nch->ncp;
764         asof = dip->obj_asof;
765         nlen = ncp->nc_nlen;
766         flags = dip->flags & HAMMER_INODE_RO;
767         ispfs = 0;
768
769         hammer_simple_transaction(&trans, dip->hmp);
770
771         for (i = 0; i < nlen; ++i) {
772                 if (ncp->nc_name[i] == '@' && ncp->nc_name[i+1] == '@') {
773                         asof = hammer_str_to_tid(ncp->nc_name + i + 2,
774                                                  &ispfs, &localization);
775                         if (asof != HAMMER_MAX_TID)
776                                 flags |= HAMMER_INODE_RO;
777                         break;
778                 }
779         }
780         nlen = i;
781
782         /*
783          * If this is a PFS softlink we dive into the PFS
784          */
785         if (ispfs && nlen == 0) {
786                 ip = hammer_get_inode(&trans, dip, HAMMER_OBJID_ROOT,
787                                       asof, localization,
788                                       flags, &error);
789                 if (error == 0) {
790                         error = hammer_get_vnode(ip, &vp);
791                         hammer_rel_inode(ip, 0);
792                 } else {
793                         vp = NULL;
794                 }
795                 if (error == 0) {
796                         vn_unlock(vp);
797                         cache_setvp(ap->a_nch, vp);
798                         vrele(vp);
799                 }
800                 goto done;
801         }
802
803         /*
804          * If there is no path component the time extension is relative to
805          * dip.
806          */
807         if (nlen == 0) {
808                 ip = hammer_get_inode(&trans, dip, dip->obj_id,
809                                       asof, dip->obj_localization,
810                                       flags, &error);
811                 if (error == 0) {
812                         error = hammer_get_vnode(ip, &vp);
813                         hammer_rel_inode(ip, 0);
814                 } else {
815                         vp = NULL;
816                 }
817                 if (error == 0) {
818                         vn_unlock(vp);
819                         cache_setvp(ap->a_nch, vp);
820                         vrele(vp);
821                 }
822                 goto done;
823         }
824
825         /*
826          * Calculate the namekey and setup the key range for the scan.  This
827          * works kinda like a chained hash table where the lower 32 bits
828          * of the namekey synthesize the chain.
829          *
830          * The key range is inclusive of both key_beg and key_end.
831          */
832         namekey = hammer_directory_namekey(ncp->nc_name, nlen);
833
834         error = hammer_init_cursor(&trans, &cursor, &dip->cache[1], dip);
835         cursor.key_beg.localization = dip->obj_localization +
836                                       HAMMER_LOCALIZE_MISC;
837         cursor.key_beg.obj_id = dip->obj_id;
838         cursor.key_beg.key = namekey;
839         cursor.key_beg.create_tid = 0;
840         cursor.key_beg.delete_tid = 0;
841         cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
842         cursor.key_beg.obj_type = 0;
843
844         cursor.key_end = cursor.key_beg;
845         cursor.key_end.key |= 0xFFFFFFFFULL;
846         cursor.asof = asof;
847         cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
848
849         /*
850          * Scan all matching records (the chain), locate the one matching
851          * the requested path component.
852          *
853          * The hammer_ip_*() functions merge in-memory records with on-disk
854          * records for the purposes of the search.
855          */
856         obj_id = 0;
857         localization = HAMMER_DEF_LOCALIZATION;
858
859         if (error == 0) {
860                 error = hammer_ip_first(&cursor);
861                 while (error == 0) {
862                         error = hammer_ip_resolve_data(&cursor);
863                         if (error)
864                                 break;
865                         if (nlen == cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF &&
866                             bcmp(ncp->nc_name, cursor.data->entry.name, nlen) == 0) {
867                                 obj_id = cursor.data->entry.obj_id;
868                                 localization = cursor.data->entry.localization;
869                                 break;
870                         }
871                         error = hammer_ip_next(&cursor);
872                 }
873         }
874         hammer_done_cursor(&cursor);
875         if (error == 0) {
876                 ip = hammer_get_inode(&trans, dip, obj_id,
877                                       asof, localization,
878                                       flags, &error);
879                 if (error == 0) {
880                         error = hammer_get_vnode(ip, &vp);
881                         hammer_rel_inode(ip, 0);
882                 } else {
883                         vp = NULL;
884                 }
885                 if (error == 0) {
886                         vn_unlock(vp);
887                         cache_setvp(ap->a_nch, vp);
888                         vrele(vp);
889                 }
890         } else if (error == ENOENT) {
891                 cache_setvp(ap->a_nch, NULL);
892         }
893 done:
894         hammer_done_transaction(&trans);
895         return (error);
896 }
897
898 /*
899  * hammer_vop_nlookupdotdot { dvp, vpp, cred }
900  *
901  * Locate the parent directory of a directory vnode.
902  *
903  * dvp is referenced but not locked.  *vpp must be returned referenced and
904  * locked.  A parent_obj_id of 0 does not necessarily indicate that we are
905  * at the root, instead it could indicate that the directory we were in was
906  * removed.
907  *
908  * NOTE: as-of sequences are not linked into the directory structure.  If
909  * we are at the root with a different asof then the mount point, reload
910  * the same directory with the mount point's asof.   I'm not sure what this
911  * will do to NFS.  We encode ASOF stamps in NFS file handles so it might not
912  * get confused, but it hasn't been tested.
913  */
914 static
915 int
916 hammer_vop_nlookupdotdot(struct vop_nlookupdotdot_args *ap)
917 {
918         struct hammer_transaction trans;
919         struct hammer_inode *dip;
920         struct hammer_inode *ip;
921         int64_t parent_obj_id;
922         u_int32_t parent_obj_localization;
923         hammer_tid_t asof;
924         int error;
925
926         dip = VTOI(ap->a_dvp);
927         asof = dip->obj_asof;
928
929         /*
930          * Whos are parent?  This could be the root of a pseudo-filesystem
931          * whos parent is in another localization domain.
932          */
933         parent_obj_id = dip->ino_data.parent_obj_id;
934         if (dip->obj_id == HAMMER_OBJID_ROOT)
935                 parent_obj_localization = dip->ino_data.ext.obj.parent_obj_localization;
936         else
937                 parent_obj_localization = dip->obj_localization;
938
939         if (parent_obj_id == 0) {
940                 if (dip->obj_id == HAMMER_OBJID_ROOT &&
941                    asof != dip->hmp->asof) {
942                         parent_obj_id = dip->obj_id;
943                         asof = dip->hmp->asof;
944                         *ap->a_fakename = kmalloc(19, M_TEMP, M_WAITOK);
945                         ksnprintf(*ap->a_fakename, 19, "0x%016llx",
946                                    dip->obj_asof);
947                 } else {
948                         *ap->a_vpp = NULL;
949                         return ENOENT;
950                 }
951         }
952
953         hammer_simple_transaction(&trans, dip->hmp);
954
955         ip = hammer_get_inode(&trans, dip, parent_obj_id,
956                               asof, parent_obj_localization,
957                               dip->flags, &error);
958         if (ip) {
959                 error = hammer_get_vnode(ip, ap->a_vpp);
960                 hammer_rel_inode(ip, 0);
961         } else {
962                 *ap->a_vpp = NULL;
963         }
964         hammer_done_transaction(&trans);
965         return (error);
966 }
967
968 /*
969  * hammer_vop_nlink { nch, dvp, vp, cred }
970  */
971 static
972 int
973 hammer_vop_nlink(struct vop_nlink_args *ap)
974 {
975         struct hammer_transaction trans;
976         struct hammer_inode *dip;
977         struct hammer_inode *ip;
978         struct nchandle *nch;
979         int error;
980
981         nch = ap->a_nch;
982         dip = VTOI(ap->a_dvp);
983         ip = VTOI(ap->a_vp);
984
985         if (dip->flags & HAMMER_INODE_RO)
986                 return (EROFS);
987         if (ip->flags & HAMMER_INODE_RO)
988                 return (EROFS);
989         if ((error = hammer_checkspace(dip->hmp, HAMMER_CHKSPC_CREATE)) != 0)
990                 return (error);
991
992         /*
993          * Create a transaction to cover the operations we perform.
994          */
995         hammer_start_transaction(&trans, dip->hmp);
996
997         /*
998          * Add the filesystem object to the directory.  Note that neither
999          * dip nor ip are referenced or locked, but their vnodes are
1000          * referenced.  This function will bump the inode's link count.
1001          */
1002         error = hammer_ip_add_directory(&trans, dip,
1003                                         nch->ncp->nc_name, nch->ncp->nc_nlen,
1004                                         ip);
1005
1006         /*
1007          * Finish up.
1008          */
1009         if (error == 0) {
1010                 cache_setunresolved(nch);
1011                 cache_setvp(nch, ap->a_vp);
1012         }
1013         hammer_done_transaction(&trans);
1014         return (error);
1015 }
1016
1017 /*
1018  * hammer_vop_nmkdir { nch, dvp, vpp, cred, vap }
1019  *
1020  * The operating system has already ensured that the directory entry
1021  * does not exist and done all appropriate namespace locking.
1022  */
1023 static
1024 int
1025 hammer_vop_nmkdir(struct vop_nmkdir_args *ap)
1026 {
1027         struct hammer_transaction trans;
1028         struct hammer_inode *dip;
1029         struct hammer_inode *nip;
1030         struct nchandle *nch;
1031         int error;
1032
1033         nch = ap->a_nch;
1034         dip = VTOI(ap->a_dvp);
1035
1036         if (dip->flags & HAMMER_INODE_RO)
1037                 return (EROFS);
1038         if ((error = hammer_checkspace(dip->hmp, HAMMER_CHKSPC_CREATE)) != 0)
1039                 return (error);
1040
1041         /*
1042          * Create a transaction to cover the operations we perform.
1043          */
1044         hammer_start_transaction(&trans, dip->hmp);
1045
1046         /*
1047          * Create a new filesystem object of the requested type.  The
1048          * returned inode will be referenced but not locked.
1049          */
1050         error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
1051                                     dip, NULL, &nip);
1052         if (error) {
1053                 hkprintf("hammer_mkdir error %d\n", error);
1054                 hammer_done_transaction(&trans);
1055                 *ap->a_vpp = NULL;
1056                 return (error);
1057         }
1058         /*
1059          * Add the new filesystem object to the directory.  This will also
1060          * bump the inode's link count.
1061          */
1062         error = hammer_ip_add_directory(&trans, dip,
1063                                         nch->ncp->nc_name, nch->ncp->nc_nlen,
1064                                         nip);
1065         if (error)
1066                 hkprintf("hammer_mkdir (add) error %d\n", error);
1067
1068         /*
1069          * Finish up.
1070          */
1071         if (error) {
1072                 hammer_rel_inode(nip, 0);
1073                 *ap->a_vpp = NULL;
1074         } else {
1075                 error = hammer_get_vnode(nip, ap->a_vpp);
1076                 hammer_rel_inode(nip, 0);
1077                 if (error == 0) {
1078                         cache_setunresolved(ap->a_nch);
1079                         cache_setvp(ap->a_nch, *ap->a_vpp);
1080                 }
1081         }
1082         hammer_done_transaction(&trans);
1083         return (error);
1084 }
1085
1086 /*
1087  * hammer_vop_nmknod { nch, dvp, vpp, cred, vap }
1088  *
1089  * The operating system has already ensured that the directory entry
1090  * does not exist and done all appropriate namespace locking.
1091  */
1092 static
1093 int
1094 hammer_vop_nmknod(struct vop_nmknod_args *ap)
1095 {
1096         struct hammer_transaction trans;
1097         struct hammer_inode *dip;
1098         struct hammer_inode *nip;
1099         struct nchandle *nch;
1100         int error;
1101
1102         nch = ap->a_nch;
1103         dip = VTOI(ap->a_dvp);
1104
1105         if (dip->flags & HAMMER_INODE_RO)
1106                 return (EROFS);
1107         if ((error = hammer_checkspace(dip->hmp, HAMMER_CHKSPC_CREATE)) != 0)
1108                 return (error);
1109
1110         /*
1111          * Create a transaction to cover the operations we perform.
1112          */
1113         hammer_start_transaction(&trans, dip->hmp);
1114
1115         /*
1116          * Create a new filesystem object of the requested type.  The
1117          * returned inode will be referenced but not locked.
1118          *
1119          * If mknod specifies a directory a pseudo-fs is created.
1120          */
1121         error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
1122                                     dip, NULL, &nip);
1123         if (error) {
1124                 hammer_done_transaction(&trans);
1125                 *ap->a_vpp = NULL;
1126                 return (error);
1127         }
1128
1129         /*
1130          * Add the new filesystem object to the directory.  This will also
1131          * bump the inode's link count.
1132          */
1133         error = hammer_ip_add_directory(&trans, dip,
1134                                         nch->ncp->nc_name, nch->ncp->nc_nlen,
1135                                         nip);
1136
1137         /*
1138          * Finish up.
1139          */
1140         if (error) {
1141                 hammer_rel_inode(nip, 0);
1142                 *ap->a_vpp = NULL;
1143         } else {
1144                 error = hammer_get_vnode(nip, ap->a_vpp);
1145                 hammer_rel_inode(nip, 0);
1146                 if (error == 0) {
1147                         cache_setunresolved(ap->a_nch);
1148                         cache_setvp(ap->a_nch, *ap->a_vpp);
1149                 }
1150         }
1151         hammer_done_transaction(&trans);
1152         return (error);
1153 }
1154
1155 /*
1156  * hammer_vop_open { vp, mode, cred, fp }
1157  */
1158 static
1159 int
1160 hammer_vop_open(struct vop_open_args *ap)
1161 {
1162         hammer_inode_t ip;
1163
1164         ip = VTOI(ap->a_vp);
1165
1166         if ((ap->a_mode & FWRITE) && (ip->flags & HAMMER_INODE_RO))
1167                 return (EROFS);
1168         return(vop_stdopen(ap));
1169 }
1170
1171 /*
1172  * hammer_vop_pathconf { vp, name, retval }
1173  */
1174 static
1175 int
1176 hammer_vop_pathconf(struct vop_pathconf_args *ap)
1177 {
1178         return EOPNOTSUPP;
1179 }
1180
1181 /*
1182  * hammer_vop_print { vp }
1183  */
1184 static
1185 int
1186 hammer_vop_print(struct vop_print_args *ap)
1187 {
1188         return EOPNOTSUPP;
1189 }
1190
1191 /*
1192  * hammer_vop_readdir { vp, uio, cred, *eofflag, *ncookies, off_t **cookies }
1193  */
1194 static
1195 int
1196 hammer_vop_readdir(struct vop_readdir_args *ap)
1197 {
1198         struct hammer_transaction trans;
1199         struct hammer_cursor cursor;
1200         struct hammer_inode *ip;
1201         struct uio *uio;
1202         hammer_base_elm_t base;
1203         int error;
1204         int cookie_index;
1205         int ncookies;
1206         off_t *cookies;
1207         off_t saveoff;
1208         int r;
1209         int dtype;
1210
1211         ip = VTOI(ap->a_vp);
1212         uio = ap->a_uio;
1213         saveoff = uio->uio_offset;
1214
1215         if (ap->a_ncookies) {
1216                 ncookies = uio->uio_resid / 16 + 1;
1217                 if (ncookies > 1024)
1218                         ncookies = 1024;
1219                 cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
1220                 cookie_index = 0;
1221         } else {
1222                 ncookies = -1;
1223                 cookies = NULL;
1224                 cookie_index = 0;
1225         }
1226
1227         hammer_simple_transaction(&trans, ip->hmp);
1228
1229         /*
1230          * Handle artificial entries
1231          */
1232         error = 0;
1233         if (saveoff == 0) {
1234                 r = vop_write_dirent(&error, uio, ip->obj_id, DT_DIR, 1, ".");
1235                 if (r)
1236                         goto done;
1237                 if (cookies)
1238                         cookies[cookie_index] = saveoff;
1239                 ++saveoff;
1240                 ++cookie_index;
1241                 if (cookie_index == ncookies)
1242                         goto done;
1243         }
1244         if (saveoff == 1) {
1245                 if (ip->ino_data.parent_obj_id) {
1246                         r = vop_write_dirent(&error, uio,
1247                                              ip->ino_data.parent_obj_id,
1248                                              DT_DIR, 2, "..");
1249                 } else {
1250                         r = vop_write_dirent(&error, uio,
1251                                              ip->obj_id, DT_DIR, 2, "..");
1252                 }
1253                 if (r)
1254                         goto done;
1255                 if (cookies)
1256                         cookies[cookie_index] = saveoff;
1257                 ++saveoff;
1258                 ++cookie_index;
1259                 if (cookie_index == ncookies)
1260                         goto done;
1261         }
1262
1263         /*
1264          * Key range (begin and end inclusive) to scan.  Directory keys
1265          * directly translate to a 64 bit 'seek' position.
1266          */
1267         hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
1268         cursor.key_beg.localization = ip->obj_localization +
1269                                       HAMMER_LOCALIZE_MISC;
1270         cursor.key_beg.obj_id = ip->obj_id;
1271         cursor.key_beg.create_tid = 0;
1272         cursor.key_beg.delete_tid = 0;
1273         cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1274         cursor.key_beg.obj_type = 0;
1275         cursor.key_beg.key = saveoff;
1276
1277         cursor.key_end = cursor.key_beg;
1278         cursor.key_end.key = HAMMER_MAX_KEY;
1279         cursor.asof = ip->obj_asof;
1280         cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
1281
1282         error = hammer_ip_first(&cursor);
1283
1284         while (error == 0) {
1285                 error = hammer_ip_resolve_data(&cursor);
1286                 if (error)
1287                         break;
1288                 base = &cursor.leaf->base;
1289                 saveoff = base->key;
1290                 KKASSERT(cursor.leaf->data_len > HAMMER_ENTRY_NAME_OFF);
1291
1292                 if (base->obj_id != ip->obj_id)
1293                         panic("readdir: bad record at %p", cursor.node);
1294
1295                 /*
1296                  * Convert pseudo-filesystems into softlinks
1297                  */
1298                 dtype = hammer_get_dtype(cursor.leaf->base.obj_type);
1299                 r = vop_write_dirent(
1300                              &error, uio, cursor.data->entry.obj_id,
1301                              dtype,
1302                              cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF ,
1303                              (void *)cursor.data->entry.name);
1304                 if (r)
1305                         break;
1306                 ++saveoff;
1307                 if (cookies)
1308                         cookies[cookie_index] = base->key;
1309                 ++cookie_index;
1310                 if (cookie_index == ncookies)
1311                         break;
1312                 error = hammer_ip_next(&cursor);
1313         }
1314         hammer_done_cursor(&cursor);
1315
1316 done:
1317         hammer_done_transaction(&trans);
1318
1319         if (ap->a_eofflag)
1320                 *ap->a_eofflag = (error == ENOENT);
1321         uio->uio_offset = saveoff;
1322         if (error && cookie_index == 0) {
1323                 if (error == ENOENT)
1324                         error = 0;
1325                 if (cookies) {
1326                         kfree(cookies, M_TEMP);
1327                         *ap->a_ncookies = 0;
1328                         *ap->a_cookies = NULL;
1329                 }
1330         } else {
1331                 if (error == ENOENT)
1332                         error = 0;
1333                 if (cookies) {
1334                         *ap->a_ncookies = cookie_index;
1335                         *ap->a_cookies = cookies;
1336                 }
1337         }
1338         return(error);
1339 }
1340
1341 /*
1342  * hammer_vop_readlink { vp, uio, cred }
1343  */
1344 static
1345 int
1346 hammer_vop_readlink(struct vop_readlink_args *ap)
1347 {
1348         struct hammer_transaction trans;
1349         struct hammer_cursor cursor;
1350         struct hammer_inode *ip;
1351         char buf[32];
1352         u_int32_t localization;
1353         hammer_pseudofs_inmem_t pfsm;
1354         int error;
1355
1356         ip = VTOI(ap->a_vp);
1357
1358         /*
1359          * Shortcut if the symlink data was stuffed into ino_data.
1360          *
1361          * Also expand special "@@PFS%05d" softlinks (expansion only
1362          * occurs for non-historical (current) accesses made from the
1363          * primary filesystem).
1364          */
1365         if (ip->ino_data.size <= HAMMER_INODE_BASESYMLEN) {
1366                 char *ptr;
1367                 int bytes;
1368
1369                 ptr = ip->ino_data.ext.symlink;
1370                 bytes = (int)ip->ino_data.size;
1371                 if (bytes == 10 &&
1372                     ip->obj_asof == HAMMER_MAX_TID &&
1373                     ip->obj_localization == 0 &&
1374                     strncmp(ptr, "@@PFS", 5) == 0) {
1375                         hammer_simple_transaction(&trans, ip->hmp);
1376                         bcopy(ptr + 5, buf, 5);
1377                         buf[5] = 0;
1378                         localization = strtoul(buf, NULL, 10) << 16;
1379                         pfsm = hammer_load_pseudofs(&trans, localization,
1380                                                     &error);
1381                         if (error == 0) {
1382                                 if (pfsm->pfsd.mirror_flags &
1383                                     HAMMER_PFSD_SLAVE) {
1384                                         ksnprintf(buf, sizeof(buf),
1385                                                   "@@0x%016llx:%05d",
1386                                                   pfsm->pfsd.sync_end_tid,
1387                                                   localization >> 16);
1388                                 } else {
1389                                         ksnprintf(buf, sizeof(buf),
1390                                                   "@@0x%016llx:%05d",
1391                                                   HAMMER_MAX_TID,
1392                                                   localization >> 16);
1393                                 }
1394                                 ptr = buf;
1395                                 bytes = strlen(buf);
1396                         }
1397                         if (pfsm)
1398                                 hammer_rel_pseudofs(trans.hmp, pfsm);
1399                         hammer_done_transaction(&trans);
1400                 }
1401                 error = uiomove(ptr, bytes, ap->a_uio);
1402                 return(error);
1403         }
1404
1405         /*
1406          * Long version
1407          */
1408         hammer_simple_transaction(&trans, ip->hmp);
1409         hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
1410
1411         /*
1412          * Key range (begin and end inclusive) to scan.  Directory keys
1413          * directly translate to a 64 bit 'seek' position.
1414          */
1415         cursor.key_beg.localization = ip->obj_localization +
1416                                       HAMMER_LOCALIZE_MISC;
1417         cursor.key_beg.obj_id = ip->obj_id;
1418         cursor.key_beg.create_tid = 0;
1419         cursor.key_beg.delete_tid = 0;
1420         cursor.key_beg.rec_type = HAMMER_RECTYPE_FIX;
1421         cursor.key_beg.obj_type = 0;
1422         cursor.key_beg.key = HAMMER_FIXKEY_SYMLINK;
1423         cursor.asof = ip->obj_asof;
1424         cursor.flags |= HAMMER_CURSOR_ASOF;
1425
1426         error = hammer_ip_lookup(&cursor);
1427         if (error == 0) {
1428                 error = hammer_ip_resolve_data(&cursor);
1429                 if (error == 0) {
1430                         KKASSERT(cursor.leaf->data_len >=
1431                                  HAMMER_SYMLINK_NAME_OFF);
1432                         error = uiomove(cursor.data->symlink.name,
1433                                         cursor.leaf->data_len -
1434                                                 HAMMER_SYMLINK_NAME_OFF,
1435                                         ap->a_uio);
1436                 }
1437         }
1438         hammer_done_cursor(&cursor);
1439         hammer_done_transaction(&trans);
1440         return(error);
1441 }
1442
1443 /*
1444  * hammer_vop_nremove { nch, dvp, cred }
1445  */
1446 static
1447 int
1448 hammer_vop_nremove(struct vop_nremove_args *ap)
1449 {
1450         struct hammer_transaction trans;
1451         struct hammer_inode *dip;
1452         int error;
1453
1454         dip = VTOI(ap->a_dvp);
1455
1456         if (hammer_nohistory(dip) == 0 &&
1457             (error = hammer_checkspace(dip->hmp, HAMMER_CHKSPC_REMOVE)) != 0) {
1458                 return (error);
1459         }
1460
1461         hammer_start_transaction(&trans, dip->hmp);
1462         error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp, ap->a_cred, 0);
1463         hammer_done_transaction(&trans);
1464
1465         return (error);
1466 }
1467
1468 /*
1469  * hammer_vop_nrename { fnch, tnch, fdvp, tdvp, cred }
1470  */
1471 static
1472 int
1473 hammer_vop_nrename(struct vop_nrename_args *ap)
1474 {
1475         struct hammer_transaction trans;
1476         struct namecache *fncp;
1477         struct namecache *tncp;
1478         struct hammer_inode *fdip;
1479         struct hammer_inode *tdip;
1480         struct hammer_inode *ip;
1481         struct hammer_cursor cursor;
1482         int64_t namekey;
1483         int nlen, error;
1484
1485         fdip = VTOI(ap->a_fdvp);
1486         tdip = VTOI(ap->a_tdvp);
1487         fncp = ap->a_fnch->ncp;
1488         tncp = ap->a_tnch->ncp;
1489         ip = VTOI(fncp->nc_vp);
1490         KKASSERT(ip != NULL);
1491
1492         if (fdip->flags & HAMMER_INODE_RO)
1493                 return (EROFS);
1494         if (tdip->flags & HAMMER_INODE_RO)
1495                 return (EROFS);
1496         if (ip->flags & HAMMER_INODE_RO)
1497                 return (EROFS);
1498         if ((error = hammer_checkspace(fdip->hmp, HAMMER_CHKSPC_CREATE)) != 0)
1499                 return (error);
1500
1501         hammer_start_transaction(&trans, fdip->hmp);
1502
1503         /*
1504          * Remove tncp from the target directory and then link ip as
1505          * tncp. XXX pass trans to dounlink
1506          *
1507          * Force the inode sync-time to match the transaction so it is
1508          * in-sync with the creation of the target directory entry.
1509          */
1510         error = hammer_dounlink(&trans, ap->a_tnch, ap->a_tdvp, ap->a_cred, 0);
1511         if (error == 0 || error == ENOENT) {
1512                 error = hammer_ip_add_directory(&trans, tdip,
1513                                                 tncp->nc_name, tncp->nc_nlen,
1514                                                 ip);
1515                 if (error == 0) {
1516                         ip->ino_data.parent_obj_id = tdip->obj_id;
1517                         hammer_modify_inode(ip, HAMMER_INODE_DDIRTY);
1518                 }
1519         }
1520         if (error)
1521                 goto failed; /* XXX */
1522
1523         /*
1524          * Locate the record in the originating directory and remove it.
1525          *
1526          * Calculate the namekey and setup the key range for the scan.  This
1527          * works kinda like a chained hash table where the lower 32 bits
1528          * of the namekey synthesize the chain.
1529          *
1530          * The key range is inclusive of both key_beg and key_end.
1531          */
1532         namekey = hammer_directory_namekey(fncp->nc_name, fncp->nc_nlen);
1533 retry:
1534         hammer_init_cursor(&trans, &cursor, &fdip->cache[1], fdip);
1535         cursor.key_beg.localization = fdip->obj_localization +
1536                                       HAMMER_LOCALIZE_MISC;
1537         cursor.key_beg.obj_id = fdip->obj_id;
1538         cursor.key_beg.key = namekey;
1539         cursor.key_beg.create_tid = 0;
1540         cursor.key_beg.delete_tid = 0;
1541         cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1542         cursor.key_beg.obj_type = 0;
1543
1544         cursor.key_end = cursor.key_beg;
1545         cursor.key_end.key |= 0xFFFFFFFFULL;
1546         cursor.asof = fdip->obj_asof;
1547         cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
1548
1549         /*
1550          * Scan all matching records (the chain), locate the one matching
1551          * the requested path component.
1552          *
1553          * The hammer_ip_*() functions merge in-memory records with on-disk
1554          * records for the purposes of the search.
1555          */
1556         error = hammer_ip_first(&cursor);
1557         while (error == 0) {
1558                 if (hammer_ip_resolve_data(&cursor) != 0)
1559                         break;
1560                 nlen = cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF;
1561                 KKASSERT(nlen > 0);
1562                 if (fncp->nc_nlen == nlen &&
1563                     bcmp(fncp->nc_name, cursor.data->entry.name, nlen) == 0) {
1564                         break;
1565                 }
1566                 error = hammer_ip_next(&cursor);
1567         }
1568
1569         /*
1570          * If all is ok we have to get the inode so we can adjust nlinks.
1571          *
1572          * WARNING: hammer_ip_del_directory() may have to terminate the
1573          * cursor to avoid a recursion.  It's ok to call hammer_done_cursor()
1574          * twice.
1575          */
1576         if (error == 0)
1577                 error = hammer_ip_del_directory(&trans, &cursor, fdip, ip);
1578
1579         /*
1580          * XXX A deadlock here will break rename's atomicy for the purposes
1581          * of crash recovery.
1582          */
1583         if (error == EDEADLK) {
1584                 hammer_done_cursor(&cursor);
1585                 goto retry;
1586         }
1587
1588         /*
1589          * Cleanup and tell the kernel that the rename succeeded.
1590          */
1591         hammer_done_cursor(&cursor);
1592         if (error == 0)
1593                 cache_rename(ap->a_fnch, ap->a_tnch);
1594
1595 failed:
1596         hammer_done_transaction(&trans);
1597         return (error);
1598 }
1599
1600 /*
1601  * hammer_vop_nrmdir { nch, dvp, cred }
1602  */
1603 static
1604 int
1605 hammer_vop_nrmdir(struct vop_nrmdir_args *ap)
1606 {
1607         struct hammer_transaction trans;
1608         struct hammer_inode *dip;
1609         int error;
1610
1611         dip = VTOI(ap->a_dvp);
1612
1613         if (hammer_nohistory(dip) == 0 &&
1614             (error = hammer_checkspace(dip->hmp, HAMMER_CHKSPC_REMOVE)) != 0) {
1615                 return (error);
1616         }
1617
1618         hammer_start_transaction(&trans, dip->hmp);
1619         error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp, ap->a_cred, 0);
1620         hammer_done_transaction(&trans);
1621
1622         return (error);
1623 }
1624
1625 /*
1626  * hammer_vop_setattr { vp, vap, cred }
1627  */
1628 static
1629 int
1630 hammer_vop_setattr(struct vop_setattr_args *ap)
1631 {
1632         struct hammer_transaction trans;
1633         struct vattr *vap;
1634         struct hammer_inode *ip;
1635         int modflags;
1636         int error;
1637         int truncating;
1638         int blksize;
1639         int64_t aligned_size;
1640         u_int32_t flags;
1641
1642         vap = ap->a_vap;
1643         ip = ap->a_vp->v_data;
1644         modflags = 0;
1645
1646         if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
1647                 return(EROFS);
1648         if (ip->flags & HAMMER_INODE_RO)
1649                 return (EROFS);
1650         if (hammer_nohistory(ip) == 0 &&
1651             (error = hammer_checkspace(ip->hmp, HAMMER_CHKSPC_REMOVE)) != 0) {
1652                 return (error);
1653         }
1654
1655         hammer_start_transaction(&trans, ip->hmp);
1656         error = 0;
1657
1658         if (vap->va_flags != VNOVAL) {
1659                 flags = ip->ino_data.uflags;
1660                 error = vop_helper_setattr_flags(&flags, vap->va_flags,
1661                                          hammer_to_unix_xid(&ip->ino_data.uid),
1662                                          ap->a_cred);
1663                 if (error == 0) {
1664                         if (ip->ino_data.uflags != flags) {
1665                                 ip->ino_data.uflags = flags;
1666                                 modflags |= HAMMER_INODE_DDIRTY;
1667                         }
1668                         if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) {
1669                                 error = 0;
1670                                 goto done;
1671                         }
1672                 }
1673                 goto done;
1674         }
1675         if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) {
1676                 error = EPERM;
1677                 goto done;
1678         }
1679         if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
1680                 mode_t cur_mode = ip->ino_data.mode;
1681                 uid_t cur_uid = hammer_to_unix_xid(&ip->ino_data.uid);
1682                 gid_t cur_gid = hammer_to_unix_xid(&ip->ino_data.gid);
1683                 uuid_t uuid_uid;
1684                 uuid_t uuid_gid;
1685
1686                 error = vop_helper_chown(ap->a_vp, vap->va_uid, vap->va_gid,
1687                                          ap->a_cred,
1688                                          &cur_uid, &cur_gid, &cur_mode);
1689                 if (error == 0) {
1690                         hammer_guid_to_uuid(&uuid_uid, cur_uid);
1691                         hammer_guid_to_uuid(&uuid_gid, cur_gid);
1692                         if (bcmp(&uuid_uid, &ip->ino_data.uid,
1693                                  sizeof(uuid_uid)) ||
1694                             bcmp(&uuid_gid, &ip->ino_data.gid,
1695                                  sizeof(uuid_gid)) ||
1696                             ip->ino_data.mode != cur_mode
1697                         ) {
1698                                 ip->ino_data.uid = uuid_uid;
1699                                 ip->ino_data.gid = uuid_gid;
1700                                 ip->ino_data.mode = cur_mode;
1701                         }
1702                         modflags |= HAMMER_INODE_DDIRTY;
1703                 }
1704         }
1705         while (vap->va_size != VNOVAL && ip->ino_data.size != vap->va_size) {
1706                 switch(ap->a_vp->v_type) {
1707                 case VREG:
1708                         if (vap->va_size == ip->ino_data.size)
1709                                 break;
1710                         /*
1711                          * XXX break atomicy, we can deadlock the backend
1712                          * if we do not release the lock.  Probably not a
1713                          * big deal here.
1714                          */
1715                         blksize = hammer_blocksize(vap->va_size);
1716                         if (vap->va_size < ip->ino_data.size) {
1717                                 vtruncbuf(ap->a_vp, vap->va_size, blksize);
1718                                 truncating = 1;
1719                         } else {
1720                                 vnode_pager_setsize(ap->a_vp, vap->va_size);
1721                                 truncating = 0;
1722                         }
1723                         ip->ino_data.size = vap->va_size;
1724                         modflags |= HAMMER_INODE_DDIRTY;
1725
1726                         /*
1727                          * on-media truncation is cached in the inode until
1728                          * the inode is synchronized.
1729                          */
1730                         if (truncating) {
1731                                 hammer_ip_frontend_trunc(ip, vap->va_size);
1732 #ifdef DEBUG_TRUNCATE
1733                                 if (HammerTruncIp == NULL)
1734                                         HammerTruncIp = ip;
1735 #endif
1736                                 if ((ip->flags & HAMMER_INODE_TRUNCATED) == 0) {
1737                                         ip->flags |= HAMMER_INODE_TRUNCATED;
1738                                         ip->trunc_off = vap->va_size;
1739 #ifdef DEBUG_TRUNCATE
1740                                         if (ip == HammerTruncIp)
1741                                         kprintf("truncate1 %016llx\n", ip->trunc_off);
1742 #endif
1743                                 } else if (ip->trunc_off > vap->va_size) {
1744                                         ip->trunc_off = vap->va_size;
1745 #ifdef DEBUG_TRUNCATE
1746                                         if (ip == HammerTruncIp)
1747                                         kprintf("truncate2 %016llx\n", ip->trunc_off);
1748 #endif
1749                                 } else {
1750 #ifdef DEBUG_TRUNCATE
1751                                         if (ip == HammerTruncIp)
1752                                         kprintf("truncate3 %016llx (ignored)\n", vap->va_size);
1753 #endif
1754                                 }
1755                         }
1756
1757                         /*
1758                          * If truncating we have to clean out a portion of
1759                          * the last block on-disk.  We do this in the
1760                          * front-end buffer cache.
1761                          */
1762                         aligned_size = (vap->va_size + (blksize - 1)) &
1763                                        ~(int64_t)(blksize - 1);
1764                         if (truncating && vap->va_size < aligned_size) {
1765                                 struct buf *bp;
1766                                 int offset;
1767
1768                                 aligned_size -= blksize;
1769
1770                                 offset = (int)vap->va_size & (blksize - 1);
1771                                 error = bread(ap->a_vp, aligned_size,
1772                                               blksize, &bp);
1773                                 hammer_ip_frontend_trunc(ip, aligned_size);
1774                                 if (error == 0) {
1775                                         bzero(bp->b_data + offset,
1776                                               blksize - offset);
1777                                         /* must de-cache direct-io offset */
1778                                         bp->b_bio2.bio_offset = NOOFFSET;
1779                                         bdwrite(bp);
1780                                 } else {
1781                                         kprintf("ERROR %d\n", error);
1782                                         brelse(bp);
1783                                 }
1784                         }
1785                         break;
1786                 case VDATABASE:
1787                         if ((ip->flags & HAMMER_INODE_TRUNCATED) == 0) {
1788                                 ip->flags |= HAMMER_INODE_TRUNCATED;
1789                                 ip->trunc_off = vap->va_size;
1790                         } else if (ip->trunc_off > vap->va_size) {
1791                                 ip->trunc_off = vap->va_size;
1792                         }
1793                         hammer_ip_frontend_trunc(ip, vap->va_size);
1794                         ip->ino_data.size = vap->va_size;
1795                         modflags |= HAMMER_INODE_DDIRTY;
1796                         break;
1797                 default:
1798                         error = EINVAL;
1799                         goto done;
1800                 }
1801                 break;
1802         }
1803         if (vap->va_atime.tv_sec != VNOVAL) {
1804                 ip->ino_data.atime =
1805                         hammer_timespec_to_time(&vap->va_atime);
1806                 modflags |= HAMMER_INODE_ATIME;
1807         }
1808         if (vap->va_mtime.tv_sec != VNOVAL) {
1809                 ip->ino_data.mtime =
1810                         hammer_timespec_to_time(&vap->va_mtime);
1811                 modflags |= HAMMER_INODE_MTIME;
1812         }
1813         if (vap->va_mode != (mode_t)VNOVAL) {
1814                 mode_t   cur_mode = ip->ino_data.mode;
1815                 uid_t cur_uid = hammer_to_unix_xid(&ip->ino_data.uid);
1816                 gid_t cur_gid = hammer_to_unix_xid(&ip->ino_data.gid);
1817
1818                 error = vop_helper_chmod(ap->a_vp, vap->va_mode, ap->a_cred,
1819                                          cur_uid, cur_gid, &cur_mode);
1820                 if (error == 0 && ip->ino_data.mode != cur_mode) {
1821                         ip->ino_data.mode = cur_mode;
1822                         modflags |= HAMMER_INODE_DDIRTY;
1823                 }
1824         }
1825 done:
1826         if (error == 0)
1827                 hammer_modify_inode(ip, modflags);
1828         hammer_done_transaction(&trans);
1829         return (error);
1830 }
1831
1832 /*
1833  * hammer_vop_nsymlink { nch, dvp, vpp, cred, vap, target }
1834  */
1835 static
1836 int
1837 hammer_vop_nsymlink(struct vop_nsymlink_args *ap)
1838 {
1839         struct hammer_transaction trans;
1840         struct hammer_inode *dip;
1841         struct hammer_inode *nip;
1842         struct nchandle *nch;
1843         hammer_record_t record;
1844         int error;
1845         int bytes;
1846
1847         ap->a_vap->va_type = VLNK;
1848
1849         nch = ap->a_nch;
1850         dip = VTOI(ap->a_dvp);
1851
1852         if (dip->flags & HAMMER_INODE_RO)
1853                 return (EROFS);
1854         if ((error = hammer_checkspace(dip->hmp, HAMMER_CHKSPC_CREATE)) != 0)
1855                 return (error);
1856
1857         /*
1858          * Create a transaction to cover the operations we perform.
1859          */
1860         hammer_start_transaction(&trans, dip->hmp);
1861
1862         /*
1863          * Create a new filesystem object of the requested type.  The
1864          * returned inode will be referenced but not locked.
1865          */
1866
1867         error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
1868                                     dip, NULL, &nip);
1869         if (error) {
1870                 hammer_done_transaction(&trans);
1871                 *ap->a_vpp = NULL;
1872                 return (error);
1873         }
1874
1875         /*
1876          * Add a record representing the symlink.  symlink stores the link
1877          * as pure data, not a string, and is no \0 terminated.
1878          */
1879         if (error == 0) {
1880                 bytes = strlen(ap->a_target);
1881
1882                 if (bytes <= HAMMER_INODE_BASESYMLEN) {
1883                         bcopy(ap->a_target, nip->ino_data.ext.symlink, bytes);
1884                 } else {
1885                         record = hammer_alloc_mem_record(nip, bytes);
1886                         record->type = HAMMER_MEM_RECORD_GENERAL;
1887
1888                         record->leaf.base.localization = nip->obj_localization +
1889                                                          HAMMER_LOCALIZE_MISC;
1890                         record->leaf.base.key = HAMMER_FIXKEY_SYMLINK;
1891                         record->leaf.base.rec_type = HAMMER_RECTYPE_FIX;
1892                         record->leaf.data_len = bytes;
1893                         KKASSERT(HAMMER_SYMLINK_NAME_OFF == 0);
1894                         bcopy(ap->a_target, record->data->symlink.name, bytes);
1895                         error = hammer_ip_add_record(&trans, record);
1896                 }
1897
1898                 /*
1899                  * Set the file size to the length of the link.
1900                  */
1901                 if (error == 0) {
1902                         nip->ino_data.size = bytes;
1903                         hammer_modify_inode(nip, HAMMER_INODE_DDIRTY);
1904                 }
1905         }
1906         if (error == 0)
1907                 error = hammer_ip_add_directory(&trans, dip, nch->ncp->nc_name,
1908                                                 nch->ncp->nc_nlen, nip);
1909
1910         /*
1911          * Finish up.
1912          */
1913         if (error) {
1914                 hammer_rel_inode(nip, 0);
1915                 *ap->a_vpp = NULL;
1916         } else {
1917                 error = hammer_get_vnode(nip, ap->a_vpp);
1918                 hammer_rel_inode(nip, 0);
1919                 if (error == 0) {
1920                         cache_setunresolved(ap->a_nch);
1921                         cache_setvp(ap->a_nch, *ap->a_vpp);
1922                 }
1923         }
1924         hammer_done_transaction(&trans);
1925         return (error);
1926 }
1927
1928 /*
1929  * hammer_vop_nwhiteout { nch, dvp, cred, flags }
1930  */
1931 static
1932 int
1933 hammer_vop_nwhiteout(struct vop_nwhiteout_args *ap)
1934 {
1935         struct hammer_transaction trans;
1936         struct hammer_inode *dip;
1937         int error;
1938
1939         dip = VTOI(ap->a_dvp);
1940
1941         if (hammer_nohistory(dip) == 0 &&
1942             (error = hammer_checkspace(dip->hmp, HAMMER_CHKSPC_CREATE)) != 0) {
1943                 return (error);
1944         }
1945
1946         hammer_start_transaction(&trans, dip->hmp);
1947         error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp,
1948                                 ap->a_cred, ap->a_flags);
1949         hammer_done_transaction(&trans);
1950
1951         return (error);
1952 }
1953
1954 /*
1955  * hammer_vop_ioctl { vp, command, data, fflag, cred }
1956  */
1957 static
1958 int
1959 hammer_vop_ioctl(struct vop_ioctl_args *ap)
1960 {
1961         struct hammer_inode *ip = ap->a_vp->v_data;
1962
1963         return(hammer_ioctl(ip, ap->a_command, ap->a_data,
1964                             ap->a_fflag, ap->a_cred));
1965 }
1966
1967 static
1968 int
1969 hammer_vop_mountctl(struct vop_mountctl_args *ap)
1970 {
1971         struct mount *mp;
1972         int error;
1973
1974         mp = ap->a_head.a_ops->head.vv_mount;
1975
1976         switch(ap->a_op) {
1977         case MOUNTCTL_SET_EXPORT:
1978                 if (ap->a_ctllen != sizeof(struct export_args))
1979                         error = EINVAL;
1980                 error = hammer_vfs_export(mp, ap->a_op,
1981                                       (const struct export_args *)ap->a_ctl);
1982                 break;
1983         default:
1984                 error = journal_mountctl(ap);
1985                 break;
1986         }
1987         return(error);
1988 }
1989
1990 /*
1991  * hammer_vop_strategy { vp, bio }
1992  *
1993  * Strategy call, used for regular file read & write only.  Note that the
1994  * bp may represent a cluster.
1995  *
1996  * To simplify operation and allow better optimizations in the future,
1997  * this code does not make any assumptions with regards to buffer alignment
1998  * or size.
1999  */
2000 static
2001 int
2002 hammer_vop_strategy(struct vop_strategy_args *ap)
2003 {
2004         struct buf *bp;
2005         int error;
2006
2007         bp = ap->a_bio->bio_buf;
2008
2009         switch(bp->b_cmd) {
2010         case BUF_CMD_READ:
2011                 error = hammer_vop_strategy_read(ap);
2012                 break;
2013         case BUF_CMD_WRITE:
2014                 error = hammer_vop_strategy_write(ap);
2015                 break;
2016         default:
2017                 bp->b_error = error = EINVAL;
2018                 bp->b_flags |= B_ERROR;
2019                 biodone(ap->a_bio);
2020                 break;
2021         }
2022         return (error);
2023 }
2024
2025 /*
2026  * Read from a regular file.  Iterate the related records and fill in the
2027  * BIO/BUF.  Gaps are zero-filled.
2028  *
2029  * The support code in hammer_object.c should be used to deal with mixed
2030  * in-memory and on-disk records.
2031  *
2032  * NOTE: Can be called from the cluster code with an oversized buf.
2033  *
2034  * XXX atime update
2035  */
2036 static
2037 int
2038 hammer_vop_strategy_read(struct vop_strategy_args *ap)
2039 {
2040         struct hammer_transaction trans;
2041         struct hammer_inode *ip;
2042         struct hammer_cursor cursor;
2043         hammer_base_elm_t base;
2044         hammer_off_t disk_offset;
2045         struct bio *bio;
2046         struct bio *nbio;
2047         struct buf *bp;
2048         int64_t rec_offset;
2049         int64_t ran_end;
2050         int64_t tmp64;
2051         int error;
2052         int boff;
2053         int roff;
2054         int n;
2055
2056         bio = ap->a_bio;
2057         bp = bio->bio_buf;
2058         ip = ap->a_vp->v_data;
2059
2060         /*
2061          * The zone-2 disk offset may have been set by the cluster code via
2062          * a BMAP operation, or else should be NOOFFSET.
2063          *
2064          * Checking the high bits for a match against zone-2 should suffice.
2065          */
2066         nbio = push_bio(bio);
2067         if ((nbio->bio_offset & HAMMER_OFF_ZONE_MASK) ==
2068             HAMMER_ZONE_LARGE_DATA) {
2069                 error = hammer_io_direct_read(ip->hmp, nbio, NULL);
2070                 return (error);
2071         }
2072
2073         /*
2074          * Well, that sucked.  Do it the hard way.  If all the stars are
2075          * aligned we may still be able to issue a direct-read.
2076          */
2077         hammer_simple_transaction(&trans, ip->hmp);
2078         hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
2079
2080         /*
2081          * Key range (begin and end inclusive) to scan.  Note that the key's
2082          * stored in the actual records represent BASE+LEN, not BASE.  The
2083          * first record containing bio_offset will have a key > bio_offset.
2084          */
2085         cursor.key_beg.localization = ip->obj_localization +
2086                                       HAMMER_LOCALIZE_MISC;
2087         cursor.key_beg.obj_id = ip->obj_id;
2088         cursor.key_beg.create_tid = 0;
2089         cursor.key_beg.delete_tid = 0;
2090         cursor.key_beg.obj_type = 0;
2091         cursor.key_beg.key = bio->bio_offset + 1;
2092         cursor.asof = ip->obj_asof;
2093         cursor.flags |= HAMMER_CURSOR_ASOF;
2094
2095         cursor.key_end = cursor.key_beg;
2096         KKASSERT(ip->ino_data.obj_type == HAMMER_OBJTYPE_REGFILE);
2097 #if 0
2098         if (ip->ino_data.obj_type == HAMMER_OBJTYPE_DBFILE) {
2099                 cursor.key_beg.rec_type = HAMMER_RECTYPE_DB;
2100                 cursor.key_end.rec_type = HAMMER_RECTYPE_DB;
2101                 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
2102         } else
2103 #endif
2104         {
2105                 ran_end = bio->bio_offset + bp->b_bufsize;
2106                 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
2107                 cursor.key_end.rec_type = HAMMER_RECTYPE_DATA;
2108                 tmp64 = ran_end + MAXPHYS + 1;  /* work-around GCC-4 bug */
2109                 if (tmp64 < ran_end)
2110                         cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
2111                 else
2112                         cursor.key_end.key = ran_end + MAXPHYS + 1;
2113         }
2114         cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
2115
2116         error = hammer_ip_first(&cursor);
2117         boff = 0;
2118
2119         while (error == 0) {
2120                 /*
2121                  * Get the base file offset of the record.  The key for
2122                  * data records is (base + bytes) rather then (base).
2123                  */
2124                 base = &cursor.leaf->base;
2125                 rec_offset = base->key - cursor.leaf->data_len;
2126
2127                 /*
2128                  * Calculate the gap, if any, and zero-fill it.
2129                  *
2130                  * n is the offset of the start of the record verses our
2131                  * current seek offset in the bio.
2132                  */
2133                 n = (int)(rec_offset - (bio->bio_offset + boff));
2134                 if (n > 0) {
2135                         if (n > bp->b_bufsize - boff)
2136                                 n = bp->b_bufsize - boff;
2137                         bzero((char *)bp->b_data + boff, n);
2138                         boff += n;
2139                         n = 0;
2140                 }
2141
2142                 /*
2143                  * Calculate the data offset in the record and the number
2144                  * of bytes we can copy.
2145                  *
2146                  * There are two degenerate cases.  First, boff may already
2147                  * be at bp->b_bufsize.  Secondly, the data offset within
2148                  * the record may exceed the record's size.
2149                  */
2150                 roff = -n;
2151                 rec_offset += roff;
2152                 n = cursor.leaf->data_len - roff;
2153                 if (n <= 0) {
2154                         kprintf("strategy_read: bad n=%d roff=%d\n", n, roff);
2155                         n = 0;
2156                 } else if (n > bp->b_bufsize - boff) {
2157                         n = bp->b_bufsize - boff;
2158                 }
2159
2160                 /*
2161                  * Deal with cached truncations.  This cool bit of code
2162                  * allows truncate()/ftruncate() to avoid having to sync
2163                  * the file.
2164                  *
2165                  * If the frontend is truncated then all backend records are
2166                  * subject to the frontend's truncation.
2167                  *
2168                  * If the backend is truncated then backend records on-disk
2169                  * (but not in-memory) are subject to the backend's
2170                  * truncation.  In-memory records owned by the backend
2171                  * represent data written after the truncation point on the
2172                  * backend and must not be truncated.
2173                  *
2174                  * Truncate operations deal with frontend buffer cache
2175                  * buffers and frontend-owned in-memory records synchronously.
2176                  */
2177                 if (ip->flags & HAMMER_INODE_TRUNCATED) {
2178                         if (hammer_cursor_ondisk(&cursor) ||
2179                             cursor.iprec->flush_state == HAMMER_FST_FLUSH) {
2180                                 if (ip->trunc_off <= rec_offset)
2181                                         n = 0;
2182                                 else if (ip->trunc_off < rec_offset + n)
2183                                         n = (int)(ip->trunc_off - rec_offset);
2184                         }
2185                 }
2186                 if (ip->sync_flags & HAMMER_INODE_TRUNCATED) {
2187                         if (hammer_cursor_ondisk(&cursor)) {
2188                                 if (ip->sync_trunc_off <= rec_offset)
2189                                         n = 0;
2190                                 else if (ip->sync_trunc_off < rec_offset + n)
2191                                         n = (int)(ip->sync_trunc_off - rec_offset);
2192                         }
2193                 }
2194
2195                 /*
2196                  * Try to issue a direct read into our bio if possible,
2197                  * otherwise resolve the element data into a hammer_buffer
2198                  * and copy.
2199                  *
2200                  * The buffer on-disk should be zerod past any real
2201                  * truncation point, but may not be for any synthesized
2202                  * truncation point from above.
2203                  */
2204                 disk_offset = cursor.leaf->data_offset + roff;
2205                 if (boff == 0 && n == bp->b_bufsize &&
2206                     hammer_cursor_ondisk(&cursor) &&
2207                     (disk_offset & HAMMER_BUFMASK) == 0) {
2208                         KKASSERT((disk_offset & HAMMER_OFF_ZONE_MASK) ==
2209                                  HAMMER_ZONE_LARGE_DATA);
2210                         nbio->bio_offset = disk_offset;
2211                         error = hammer_io_direct_read(trans.hmp, nbio,
2212                                                       cursor.leaf);
2213                         goto done;
2214                 } else if (n) {
2215                         error = hammer_ip_resolve_data(&cursor);
2216                         if (error == 0) {
2217                                 bcopy((char *)cursor.data + roff,
2218                                       (char *)bp->b_data + boff, n);
2219                         }
2220                 }
2221                 if (error)
2222                         break;
2223
2224                 /*
2225                  * Iterate until we have filled the request.
2226                  */
2227                 boff += n;
2228                 if (boff == bp->b_bufsize)
2229                         break;
2230                 error = hammer_ip_next(&cursor);
2231         }
2232
2233         /*
2234          * There may have been a gap after the last record
2235          */
2236         if (error == ENOENT)
2237                 error = 0;
2238         if (error == 0 && boff != bp->b_bufsize) {
2239                 KKASSERT(boff < bp->b_bufsize);
2240                 bzero((char *)bp->b_data + boff, bp->b_bufsize - boff);
2241                 /* boff = bp->b_bufsize; */
2242         }
2243         bp->b_resid = 0;
2244         bp->b_error = error;
2245         if (error)
2246                 bp->b_flags |= B_ERROR;
2247         biodone(ap->a_bio);
2248
2249 done:
2250         if (cursor.node)
2251                 hammer_cache_node(&ip->cache[1], cursor.node);
2252         hammer_done_cursor(&cursor);
2253         hammer_done_transaction(&trans);
2254         return(error);
2255 }
2256
2257 /*
2258  * BMAP operation - used to support cluster_read() only.
2259  *
2260  * (struct vnode *vp, off_t loffset, off_t *doffsetp, int *runp, int *runb)
2261  *
2262  * This routine may return EOPNOTSUPP if the opration is not supported for
2263  * the specified offset.  The contents of the pointer arguments do not
2264  * need to be initialized in that case. 
2265  *
2266  * If a disk address is available and properly aligned return 0 with 
2267  * *doffsetp set to the zone-2 address, and *runp / *runb set appropriately
2268  * to the run-length relative to that offset.  Callers may assume that
2269  * *doffsetp is valid if 0 is returned, even if *runp is not sufficiently
2270  * large, so return EOPNOTSUPP if it is not sufficiently large.
2271  */
2272 static
2273 int
2274 hammer_vop_bmap(struct vop_bmap_args *ap)
2275 {
2276         struct hammer_transaction trans;
2277         struct hammer_inode *ip;
2278         struct hammer_cursor cursor;
2279         hammer_base_elm_t base;
2280         int64_t rec_offset;
2281         int64_t ran_end;
2282         int64_t tmp64;
2283         int64_t base_offset;
2284         int64_t base_disk_offset;
2285         int64_t last_offset;
2286         hammer_off_t last_disk_offset;
2287         hammer_off_t disk_offset;
2288         int     rec_len;
2289         int     error;
2290         int     blksize;
2291
2292         ip = ap->a_vp->v_data;
2293
2294         /*
2295          * We can only BMAP regular files.  We can't BMAP database files,
2296          * directories, etc.
2297          */
2298         if (ip->ino_data.obj_type != HAMMER_OBJTYPE_REGFILE)
2299                 return(EOPNOTSUPP);
2300
2301         /*
2302          * bmap is typically called with runp/runb both NULL when used
2303          * for writing.  We do not support BMAP for writing atm.
2304          */
2305         if (ap->a_cmd != BUF_CMD_READ)
2306                 return(EOPNOTSUPP);
2307
2308         /*
2309          * Scan the B-Tree to acquire blockmap addresses, then translate
2310          * to raw addresses.
2311          */
2312         hammer_simple_transaction(&trans, ip->hmp);
2313 #if 0
2314         kprintf("bmap_beg %016llx ip->cache %p\n", ap->a_loffset, ip->cache[1]);
2315 #endif
2316         hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
2317
2318         /*
2319          * Key range (begin and end inclusive) to scan.  Note that the key's
2320          * stored in the actual records represent BASE+LEN, not BASE.  The
2321          * first record containing bio_offset will have a key > bio_offset.
2322          */
2323         cursor.key_beg.localization = ip->obj_localization +
2324                                       HAMMER_LOCALIZE_MISC;
2325         cursor.key_beg.obj_id = ip->obj_id;
2326         cursor.key_beg.create_tid = 0;
2327         cursor.key_beg.delete_tid = 0;
2328         cursor.key_beg.obj_type = 0;
2329         if (ap->a_runb)
2330                 cursor.key_beg.key = ap->a_loffset - MAXPHYS + 1;
2331         else
2332                 cursor.key_beg.key = ap->a_loffset + 1;
2333         if (cursor.key_beg.key < 0)
2334                 cursor.key_beg.key = 0;
2335         cursor.asof = ip->obj_asof;
2336         cursor.flags |= HAMMER_CURSOR_ASOF;
2337
2338         cursor.key_end = cursor.key_beg;
2339         KKASSERT(ip->ino_data.obj_type == HAMMER_OBJTYPE_REGFILE);
2340
2341         ran_end = ap->a_loffset + MAXPHYS;
2342         cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
2343         cursor.key_end.rec_type = HAMMER_RECTYPE_DATA;
2344         tmp64 = ran_end + MAXPHYS + 1;  /* work-around GCC-4 bug */
2345         if (tmp64 < ran_end)
2346                 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
2347         else
2348                 cursor.key_end.key = ran_end + MAXPHYS + 1;
2349
2350         cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
2351
2352         error = hammer_ip_first(&cursor);
2353         base_offset = last_offset = 0;
2354         base_disk_offset = last_disk_offset = 0;
2355
2356         while (error == 0) {
2357                 /*
2358                  * Get the base file offset of the record.  The key for
2359                  * data records is (base + bytes) rather then (base).
2360                  *
2361                  * NOTE: rec_offset + rec_len may exceed the end-of-file.
2362                  * The extra bytes should be zero on-disk and the BMAP op
2363                  * should still be ok.
2364                  */
2365                 base = &cursor.leaf->base;
2366                 rec_offset = base->key - cursor.leaf->data_len;
2367                 rec_len    = cursor.leaf->data_len;
2368
2369                 /*
2370                  * Incorporate any cached truncation.
2371                  *
2372                  * NOTE: Modifications to rec_len based on synthesized
2373                  * truncation points remove the guarantee that any extended
2374                  * data on disk is zero (since the truncations may not have
2375                  * taken place on-media yet).
2376                  */
2377                 if (ip->flags & HAMMER_INODE_TRUNCATED) {
2378                         if (hammer_cursor_ondisk(&cursor) ||
2379                             cursor.iprec->flush_state == HAMMER_FST_FLUSH) {
2380                                 if (ip->trunc_off <= rec_offset)
2381                                         rec_len = 0;
2382                                 else if (ip->trunc_off < rec_offset + rec_len)
2383                                         rec_len = (int)(ip->trunc_off - rec_offset);
2384                         }
2385                 }
2386                 if (ip->sync_flags & HAMMER_INODE_TRUNCATED) {
2387                         if (hammer_cursor_ondisk(&cursor)) {
2388                                 if (ip->sync_trunc_off <= rec_offset)
2389                                         rec_len = 0;
2390                                 else if (ip->sync_trunc_off < rec_offset + rec_len)
2391                                         rec_len = (int)(ip->sync_trunc_off - rec_offset);
2392                         }
2393                 }
2394
2395                 /*
2396                  * Accumulate information.  If we have hit a discontiguous
2397                  * block reset base_offset unless we are already beyond the
2398                  * requested offset.  If we are, that's it, we stop.
2399                  */
2400                 if (error)
2401                         break;
2402                 if (hammer_cursor_ondisk(&cursor)) {
2403                         disk_offset = cursor.leaf->data_offset;
2404                         if (rec_offset != last_offset ||
2405                             disk_offset != last_disk_offset) {
2406                                 if (rec_offset > ap->a_loffset)
2407                                         break;
2408                                 base_offset = rec_offset;
2409                                 base_disk_offset = disk_offset;
2410                         }
2411                         last_offset = rec_offset + rec_len;
2412                         last_disk_offset = disk_offset + rec_len;
2413                 }
2414                 error = hammer_ip_next(&cursor);
2415         }
2416
2417 #if 0
2418         kprintf("BMAP %016llx:  %016llx - %016llx\n",
2419                 ap->a_loffset, base_offset, last_offset);
2420         kprintf("BMAP %16s:  %016llx - %016llx\n",
2421                 "", base_disk_offset, last_disk_offset);
2422 #endif
2423
2424         if (cursor.node) {
2425                 hammer_cache_node(&ip->cache[1], cursor.node);
2426 #if 0
2427                 kprintf("bmap_end2 %016llx ip->cache %p\n", ap->a_loffset, ip->cache[1]);
2428 #endif
2429         }
2430         hammer_done_cursor(&cursor);
2431         hammer_done_transaction(&trans);
2432
2433         /*
2434          * If we couldn't find any records or the records we did find were
2435          * all behind the requested offset, return failure.  A forward
2436          * truncation can leave a hole w/ no on-disk records.
2437          */
2438         if (last_offset == 0 || last_offset < ap->a_loffset)
2439                 return (EOPNOTSUPP);
2440
2441         /*
2442          * Figure out the block size at the requested offset and adjust
2443          * our limits so the cluster_read() does not create inappropriately
2444          * sized buffer cache buffers.
2445          */
2446         blksize = hammer_blocksize(ap->a_loffset);
2447         if (hammer_blocksize(base_offset) != blksize) {
2448                 base_offset = hammer_blockdemarc(base_offset, ap->a_loffset);
2449         }
2450         if (last_offset != ap->a_loffset &&
2451             hammer_blocksize(last_offset - 1) != blksize) {
2452                 last_offset = hammer_blockdemarc(ap->a_loffset,
2453                                                  last_offset - 1);
2454         }
2455
2456         /*
2457          * Returning EOPNOTSUPP simply prevents the direct-IO optimization
2458          * from occuring.
2459          */
2460         disk_offset = base_disk_offset + (ap->a_loffset - base_offset);
2461
2462         if ((disk_offset & HAMMER_OFF_ZONE_MASK) != HAMMER_ZONE_LARGE_DATA) {
2463                 /*
2464                  * Only large-data zones can be direct-IOd
2465                  */
2466                 error = EOPNOTSUPP;
2467         } else if ((disk_offset & HAMMER_BUFMASK) ||
2468                    (last_offset - ap->a_loffset) < blksize) {
2469                 /*
2470                  * doffsetp is not aligned or the forward run size does
2471                  * not cover a whole buffer, disallow the direct I/O.
2472                  */
2473                 error = EOPNOTSUPP;
2474         } else {
2475                 /*
2476                  * We're good.
2477                  */
2478                 *ap->a_doffsetp = disk_offset;
2479                 if (ap->a_runb) {
2480                         *ap->a_runb = ap->a_loffset - base_offset;
2481                         KKASSERT(*ap->a_runb >= 0);
2482                 }
2483                 if (ap->a_runp) {
2484                         *ap->a_runp = last_offset - ap->a_loffset;
2485                         KKASSERT(*ap->a_runp >= 0);
2486                 }
2487                 error = 0;
2488         }
2489         return(error);
2490 }
2491
2492 /*
2493  * Write to a regular file.   Because this is a strategy call the OS is
2494  * trying to actually get data onto the media.
2495  */
2496 static
2497 int
2498 hammer_vop_strategy_write(struct vop_strategy_args *ap)
2499 {
2500         hammer_record_t record;
2501         hammer_mount_t hmp;
2502         hammer_inode_t ip;
2503         struct bio *bio;
2504         struct buf *bp;
2505         int blksize;
2506         int bytes;
2507         int error;
2508
2509         bio = ap->a_bio;
2510         bp = bio->bio_buf;
2511         ip = ap->a_vp->v_data;
2512         hmp = ip->hmp;
2513
2514         blksize = hammer_blocksize(bio->bio_offset);
2515         KKASSERT(bp->b_bufsize == blksize);
2516
2517         if (ip->flags & HAMMER_INODE_RO) {
2518                 bp->b_error = EROFS;
2519                 bp->b_flags |= B_ERROR;
2520                 biodone(ap->a_bio);
2521                 return(EROFS);
2522         }
2523
2524         /*
2525          * Interlock with inode destruction (no in-kernel or directory
2526          * topology visibility).  If we queue new IO while trying to
2527          * destroy the inode we can deadlock the vtrunc call in
2528          * hammer_inode_unloadable_check().
2529          */
2530         if (ip->flags & (HAMMER_INODE_DELETING|HAMMER_INODE_DELETED)) {
2531                 bp->b_resid = 0;
2532                 biodone(ap->a_bio);
2533                 return(0);
2534         }
2535
2536         /*
2537          * Reserve space and issue a direct-write from the front-end. 
2538          * NOTE: The direct_io code will hammer_bread/bcopy smaller
2539          * allocations.
2540          *
2541          * An in-memory record will be installed to reference the storage
2542          * until the flusher can get to it.
2543          *
2544          * Since we own the high level bio the front-end will not try to
2545          * do a direct-read until the write completes.
2546          *
2547          * NOTE: The only time we do not reserve a full-sized buffers
2548          * worth of data is if the file is small.  We do not try to
2549          * allocate a fragment (from the small-data zone) at the end of
2550          * an otherwise large file as this can lead to wildly separated
2551          * data.
2552          */
2553         KKASSERT((bio->bio_offset & HAMMER_BUFMASK) == 0);
2554         KKASSERT(bio->bio_offset < ip->ino_data.size);
2555         if (bio->bio_offset || ip->ino_data.size > HAMMER_BUFSIZE / 2)
2556                 bytes = bp->b_bufsize;
2557         else
2558                 bytes = ((int)ip->ino_data.size + 15) & ~15;
2559
2560         record = hammer_ip_add_bulk(ip, bio->bio_offset, bp->b_data,
2561                                     bytes, &error);
2562         if (record) {
2563                 hammer_io_direct_write(hmp, record, bio);
2564                 hammer_rel_mem_record(record);
2565                 if (ip->rsv_recs > 1 && hmp->rsv_recs > hammer_limit_recs)
2566                         hammer_flush_inode(ip, 0);
2567         } else {
2568                 bp->b_bio2.bio_offset = NOOFFSET;
2569                 bp->b_error = error;
2570                 bp->b_flags |= B_ERROR;
2571                 biodone(ap->a_bio);
2572         }
2573         return(error);
2574 }
2575
2576 /*
2577  * dounlink - disconnect a directory entry
2578  *
2579  * XXX whiteout support not really in yet
2580  */
2581 static int
2582 hammer_dounlink(hammer_transaction_t trans, struct nchandle *nch,
2583                 struct vnode *dvp, struct ucred *cred, int flags)
2584 {
2585         struct namecache *ncp;
2586         hammer_inode_t dip;
2587         hammer_inode_t ip;
2588         struct hammer_cursor cursor;
2589         int64_t namekey;
2590         int nlen, error;
2591
2592         /*
2593          * Calculate the namekey and setup the key range for the scan.  This
2594          * works kinda like a chained hash table where the lower 32 bits
2595          * of the namekey synthesize the chain.
2596          *
2597          * The key range is inclusive of both key_beg and key_end.
2598          */
2599         dip = VTOI(dvp);
2600         ncp = nch->ncp;
2601
2602         if (dip->flags & HAMMER_INODE_RO)
2603                 return (EROFS);
2604
2605         namekey = hammer_directory_namekey(ncp->nc_name, ncp->nc_nlen);
2606 retry:
2607         hammer_init_cursor(trans, &cursor, &dip->cache[1], dip);
2608         cursor.key_beg.localization = dip->obj_localization +
2609                                       HAMMER_LOCALIZE_MISC;
2610         cursor.key_beg.obj_id = dip->obj_id;
2611         cursor.key_beg.key = namekey;
2612         cursor.key_beg.create_tid = 0;
2613         cursor.key_beg.delete_tid = 0;
2614         cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
2615         cursor.key_beg.obj_type = 0;
2616
2617         cursor.key_end = cursor.key_beg;
2618         cursor.key_end.key |= 0xFFFFFFFFULL;
2619         cursor.asof = dip->obj_asof;
2620         cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
2621
2622         /*
2623          * Scan all matching records (the chain), locate the one matching
2624          * the requested path component.  info->last_error contains the
2625          * error code on search termination and could be 0, ENOENT, or
2626          * something else.
2627          *
2628          * The hammer_ip_*() functions merge in-memory records with on-disk
2629          * records for the purposes of the search.
2630          */
2631         error = hammer_ip_first(&cursor);
2632
2633         while (error == 0) {
2634                 error = hammer_ip_resolve_data(&cursor);
2635                 if (error)
2636                         break;
2637                 nlen = cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF;
2638                 KKASSERT(nlen > 0);
2639                 if (ncp->nc_nlen == nlen &&
2640                     bcmp(ncp->nc_name, cursor.data->entry.name, nlen) == 0) {
2641                         break;
2642                 }
2643                 error = hammer_ip_next(&cursor);
2644         }
2645
2646         /*
2647          * If all is ok we have to get the inode so we can adjust nlinks.
2648          * To avoid a deadlock with the flusher we must release the inode
2649          * lock on the directory when acquiring the inode for the entry.
2650          *
2651          * If the target is a directory, it must be empty.
2652          */
2653         if (error == 0) {
2654                 hammer_unlock(&cursor.ip->lock);
2655                 ip = hammer_get_inode(trans, dip, cursor.data->entry.obj_id,
2656                                       dip->hmp->asof,
2657                                       cursor.data->entry.localization,
2658                                       0, &error);
2659                 hammer_lock_sh(&cursor.ip->lock);
2660                 if (error == ENOENT) {
2661                         kprintf("obj_id %016llx\n", cursor.data->entry.obj_id);
2662                         Debugger("ENOENT unlinking object that should exist");
2663                 }
2664
2665                 /*
2666                  * If we are trying to remove a directory the directory must
2667                  * be empty.
2668                  *
2669                  * WARNING: hammer_ip_check_directory_empty() may have to
2670                  * terminate the cursor to avoid a deadlock.  It is ok to
2671                  * call hammer_done_cursor() twice.
2672                  */
2673                 if (error == 0 && ip->ino_data.obj_type ==
2674                                   HAMMER_OBJTYPE_DIRECTORY) {
2675                         error = hammer_ip_check_directory_empty(trans, ip);
2676                 }
2677
2678                 /*
2679                  * Delete the directory entry.
2680                  *
2681                  * WARNING: hammer_ip_del_directory() may have to terminate
2682                  * the cursor to avoid a deadlock.  It is ok to call
2683                  * hammer_done_cursor() twice.
2684                  */
2685                 if (error == 0) {
2686                         error = hammer_ip_del_directory(trans, &cursor,
2687                                                         dip, ip);
2688                 }
2689                 hammer_done_cursor(&cursor);
2690                 if (error == 0) {
2691                         cache_setunresolved(nch);
2692                         cache_setvp(nch, NULL);
2693                         /* XXX locking */
2694                         if (ip->vp)
2695                                 cache_inval_vp(ip->vp, CINV_DESTROY);
2696                 }
2697                 if (ip)
2698                         hammer_rel_inode(ip, 0);
2699         } else {
2700                 hammer_done_cursor(&cursor);
2701         }
2702         hammer_inode_waitreclaims(dip->hmp);
2703         if (error == EDEADLK)
2704                 goto retry;
2705
2706         return (error);
2707 }
2708
2709 /************************************************************************
2710  *                          FIFO AND SPECFS OPS                         *
2711  ************************************************************************
2712  *
2713  */
2714
2715 static int
2716 hammer_vop_fifoclose (struct vop_close_args *ap)
2717 {
2718         /* XXX update itimes */
2719         return (VOCALL(&fifo_vnode_vops, &ap->a_head));
2720 }
2721
2722 static int
2723 hammer_vop_fiforead (struct vop_read_args *ap)
2724 {
2725         int error;
2726
2727         error = VOCALL(&fifo_vnode_vops, &ap->a_head);
2728         /* XXX update access time */
2729         return (error);
2730 }
2731
2732 static int
2733 hammer_vop_fifowrite (struct vop_write_args *ap)
2734 {
2735         int error;
2736
2737         error = VOCALL(&fifo_vnode_vops, &ap->a_head);
2738         /* XXX update access time */
2739         return (error);
2740 }
2741
2742 static int
2743 hammer_vop_specclose (struct vop_close_args *ap)
2744 {
2745         /* XXX update itimes */
2746         return (VOCALL(&spec_vnode_vops, &ap->a_head));
2747 }
2748
2749 static int
2750 hammer_vop_specread (struct vop_read_args *ap)
2751 {
2752         /* XXX update access time */
2753         return (VOCALL(&spec_vnode_vops, &ap->a_head));
2754 }
2755
2756 static int
2757 hammer_vop_specwrite (struct vop_write_args *ap)
2758 {
2759         /* XXX update last change time */
2760         return (VOCALL(&spec_vnode_vops, &ap->a_head));
2761 }
2762