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