21e6106edae2b3f67d84c7ce8e313be657a47b67
[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.102 2008/10/16 17:24:16 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 <sys/file.h>
48 #include <vm/vm_extern.h>
49 #include <vfs/fifofs/fifo.h>
50
51 #include "hammer.h"
52
53 /*
54  * USERFS VNOPS
55  */
56 /*static int hammer_vop_vnoperate(struct vop_generic_args *);*/
57 static int hammer_vop_fsync(struct vop_fsync_args *);
58 static int hammer_vop_read(struct vop_read_args *);
59 static int hammer_vop_write(struct vop_write_args *);
60 static int hammer_vop_access(struct vop_access_args *);
61 static int hammer_vop_advlock(struct vop_advlock_args *);
62 static int hammer_vop_close(struct vop_close_args *);
63 static int hammer_vop_ncreate(struct vop_ncreate_args *);
64 static int hammer_vop_getattr(struct vop_getattr_args *);
65 static int hammer_vop_nresolve(struct vop_nresolve_args *);
66 static int hammer_vop_nlookupdotdot(struct vop_nlookupdotdot_args *);
67 static int hammer_vop_nlink(struct vop_nlink_args *);
68 static int hammer_vop_nmkdir(struct vop_nmkdir_args *);
69 static int hammer_vop_nmknod(struct vop_nmknod_args *);
70 static int hammer_vop_open(struct vop_open_args *);
71 static int hammer_vop_print(struct vop_print_args *);
72 static int hammer_vop_readdir(struct vop_readdir_args *);
73 static int hammer_vop_readlink(struct vop_readlink_args *);
74 static int hammer_vop_nremove(struct vop_nremove_args *);
75 static int hammer_vop_nrename(struct vop_nrename_args *);
76 static int hammer_vop_nrmdir(struct vop_nrmdir_args *);
77 static int hammer_vop_markatime(struct vop_markatime_args *);
78 static int hammer_vop_setattr(struct vop_setattr_args *);
79 static int hammer_vop_strategy(struct vop_strategy_args *);
80 static int hammer_vop_bmap(struct vop_bmap_args *ap);
81 static int hammer_vop_nsymlink(struct vop_nsymlink_args *);
82 static int hammer_vop_nwhiteout(struct vop_nwhiteout_args *);
83 static int hammer_vop_ioctl(struct vop_ioctl_args *);
84 static int hammer_vop_mountctl(struct vop_mountctl_args *);
85 static int hammer_vop_kqfilter (struct vop_kqfilter_args *);
86
87 static int hammer_vop_fifoclose (struct vop_close_args *);
88 static int hammer_vop_fiforead (struct vop_read_args *);
89 static int hammer_vop_fifowrite (struct vop_write_args *);
90 static int hammer_vop_fifokqfilter (struct vop_kqfilter_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 =         vop_stdpathconf,
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_markatime =        hammer_vop_markatime,
120         .vop_setattr =          hammer_vop_setattr,
121         .vop_bmap =             hammer_vop_bmap,
122         .vop_strategy =         hammer_vop_strategy,
123         .vop_nsymlink =         hammer_vop_nsymlink,
124         .vop_nwhiteout =        hammer_vop_nwhiteout,
125         .vop_ioctl =            hammer_vop_ioctl,
126         .vop_mountctl =         hammer_vop_mountctl,
127         .vop_kqfilter =         hammer_vop_kqfilter
128 };
129
130 struct vop_ops hammer_spec_vops = {
131         .vop_default =          vop_defaultop,
132         .vop_fsync =            hammer_vop_fsync,
133         .vop_read =             vop_stdnoread,
134         .vop_write =            vop_stdnowrite,
135         .vop_access =           hammer_vop_access,
136         .vop_close =            hammer_vop_close,
137         .vop_markatime =        hammer_vop_markatime,
138         .vop_getattr =          hammer_vop_getattr,
139         .vop_inactive =         hammer_vop_inactive,
140         .vop_reclaim =          hammer_vop_reclaim,
141         .vop_setattr =          hammer_vop_setattr
142 };
143
144 struct vop_ops hammer_fifo_vops = {
145         .vop_default =          fifo_vnoperate,
146         .vop_fsync =            hammer_vop_fsync,
147         .vop_read =             hammer_vop_fiforead,
148         .vop_write =            hammer_vop_fifowrite,
149         .vop_access =           hammer_vop_access,
150         .vop_close =            hammer_vop_fifoclose,
151         .vop_markatime =        hammer_vop_markatime,
152         .vop_getattr =          hammer_vop_getattr,
153         .vop_inactive =         hammer_vop_inactive,
154         .vop_reclaim =          hammer_vop_reclaim,
155         .vop_setattr =          hammer_vop_setattr,
156         .vop_kqfilter =         hammer_vop_fifokqfilter
157 };
158
159 static __inline
160 void
161 hammer_knote(struct vnode *vp, int flags)
162 {
163         if (flags)
164                 KNOTE(&vp->v_pollinfo.vpi_kqinfo.ki_note, flags);
165 }
166
167 #ifdef DEBUG_TRUNCATE
168 struct hammer_inode *HammerTruncIp;
169 #endif
170
171 static int hammer_dounlink(hammer_transaction_t trans, struct nchandle *nch,
172                            struct vnode *dvp, struct ucred *cred,
173                            int flags, int isdir);
174 static int hammer_vop_strategy_read(struct vop_strategy_args *ap);
175 static int hammer_vop_strategy_write(struct vop_strategy_args *ap);
176
177 #if 0
178 static
179 int
180 hammer_vop_vnoperate(struct vop_generic_args *)
181 {
182         return (VOCALL(&hammer_vnode_vops, ap));
183 }
184 #endif
185
186 /*
187  * hammer_vop_fsync { vp, waitfor }
188  *
189  * fsync() an inode to disk and wait for it to be completely committed
190  * such that the information would not be undone if a crash occured after
191  * return.
192  *
193  * NOTE: HAMMER's fsync()'s are going to remain expensive until we implement
194  *       a REDO log.  A sysctl is provided to relax HAMMER's fsync()
195  *       operation.
196  *
197  *       Ultimately the combination of a REDO log and use of fast storage
198  *       to front-end cluster caches will make fsync fast, but it aint
199  *       here yet.  And, in anycase, we need real transactional
200  *       all-or-nothing features which are not restricted to a single file.
201  */
202 static
203 int
204 hammer_vop_fsync(struct vop_fsync_args *ap)
205 {
206         hammer_inode_t ip = VTOI(ap->a_vp);
207         hammer_mount_t hmp = ip->hmp;
208         int waitfor = ap->a_waitfor;
209         int mode;
210
211         lwkt_gettoken(&hmp->fs_token);
212
213         /*
214          * Fsync rule relaxation (default is either full synchronous flush
215          * or REDO semantics with synchronous flush).
216          */
217         if (ap->a_flags & VOP_FSYNC_SYSCALL) {
218                 switch(hammer_fsync_mode) {
219                 case 0:
220 mode0:
221                         /* no REDO, full synchronous flush */
222                         goto skip;
223                 case 1:
224 mode1:
225                         /* no REDO, full asynchronous flush */
226                         if (waitfor == MNT_WAIT)
227                                 waitfor = MNT_NOWAIT;
228                         goto skip;
229                 case 2:
230                         /* REDO semantics, synchronous flush */
231                         if (hmp->version < HAMMER_VOL_VERSION_FOUR)
232                                 goto mode0;
233                         mode = HAMMER_FLUSH_UNDOS_AUTO;
234                         break;
235                 case 3:
236                         /* REDO semantics, relaxed asynchronous flush */
237                         if (hmp->version < HAMMER_VOL_VERSION_FOUR)
238                                 goto mode1;
239                         mode = HAMMER_FLUSH_UNDOS_RELAXED;
240                         if (waitfor == MNT_WAIT)
241                                 waitfor = MNT_NOWAIT;
242                         break;
243                 case 4:
244                         /* ignore the fsync() system call */
245                         lwkt_reltoken(&hmp->fs_token);
246                         return(0);
247                 default:
248                         /* we have to do something */
249                         mode = HAMMER_FLUSH_UNDOS_RELAXED;
250                         if (waitfor == MNT_WAIT)
251                                 waitfor = MNT_NOWAIT;
252                         break;
253                 }
254
255                 /*
256                  * Fast fsync only needs to flush the UNDO/REDO fifo if
257                  * HAMMER_INODE_REDO is non-zero and the only modifications
258                  * made to the file are write or write-extends.
259                  */
260                 if ((ip->flags & HAMMER_INODE_REDO) &&
261                     (ip->flags & HAMMER_INODE_MODMASK_NOREDO) == 0
262                 ) {
263                         ++hammer_count_fsyncs;
264                         hammer_flusher_flush_undos(hmp, mode);
265                         ip->redo_count = 0;
266                         lwkt_reltoken(&hmp->fs_token);
267                         return(0);
268                 }
269
270                 /*
271                  * REDO is enabled by fsync(), the idea being we really only
272                  * want to lay down REDO records when programs are using
273                  * fsync() heavily.  The first fsync() on the file starts
274                  * the gravy train going and later fsync()s keep it hot by
275                  * resetting the redo_count.
276                  *
277                  * We weren't running REDOs before now so we have to fall
278                  * through and do a full fsync of what we have.
279                  */
280                 if (hmp->version >= HAMMER_VOL_VERSION_FOUR &&
281                     (hmp->flags & HAMMER_MOUNT_REDO_RECOVERY_RUN) == 0) {
282                         ip->flags |= HAMMER_INODE_REDO;
283                         ip->redo_count = 0;
284                 }
285         }
286 skip:
287
288         /*
289          * Do a full flush sequence.
290          */
291         ++hammer_count_fsyncs;
292         vfsync(ap->a_vp, waitfor, 1, NULL, NULL);
293         hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
294         if (waitfor == MNT_WAIT) {
295                 vn_unlock(ap->a_vp);
296                 hammer_wait_inode(ip);
297                 vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY);
298         }
299         lwkt_reltoken(&hmp->fs_token);
300         return (ip->error);
301 }
302
303 /*
304  * hammer_vop_read { vp, uio, ioflag, cred }
305  *
306  * MPSAFE (for the cache safe does not require fs_token)
307  */
308 static
309 int
310 hammer_vop_read(struct vop_read_args *ap)
311 {
312         struct hammer_transaction trans;
313         hammer_inode_t ip;
314         hammer_mount_t hmp;
315         off_t offset;
316         struct buf *bp;
317         struct uio *uio;
318         int error;
319         int n;
320         int seqcount;
321         int ioseqcount;
322         int blksize;
323         int bigread;
324         int got_fstoken;
325
326         if (ap->a_vp->v_type != VREG)
327                 return (EINVAL);
328         ip = VTOI(ap->a_vp);
329         hmp = ip->hmp;
330         error = 0;
331         uio = ap->a_uio;
332
333         /*
334          * Allow the UIO's size to override the sequential heuristic.
335          */
336         blksize = hammer_blocksize(uio->uio_offset);
337         seqcount = (uio->uio_resid + (BKVASIZE - 1)) / BKVASIZE;
338         ioseqcount = (ap->a_ioflag >> 16);
339         if (seqcount < ioseqcount)
340                 seqcount = ioseqcount;
341
342         /*
343          * If reading or writing a huge amount of data we have to break
344          * atomicy and allow the operation to be interrupted by a signal
345          * or it can DOS the machine.
346          */
347         bigread = (uio->uio_resid > 100 * 1024 * 1024);
348         got_fstoken = 0;
349
350         /*
351          * Access the data typically in HAMMER_BUFSIZE blocks via the
352          * buffer cache, but HAMMER may use a variable block size based
353          * on the offset.
354          *
355          * XXX Temporary hack, delay the start transaction while we remain
356          *     MPSAFE.  NOTE: ino_data.size cannot change while vnode is
357          *     locked-shared.
358          */
359         while (uio->uio_resid > 0 && uio->uio_offset < ip->ino_data.size) {
360                 int64_t base_offset;
361                 int64_t file_limit;
362
363                 blksize = hammer_blocksize(uio->uio_offset);
364                 offset = (int)uio->uio_offset & (blksize - 1);
365                 base_offset = uio->uio_offset - offset;
366
367                 if (bigread && (error = hammer_signal_check(ip->hmp)) != 0)
368                         break;
369
370                 /*
371                  * MPSAFE
372                  */
373                 bp = getcacheblk(ap->a_vp, base_offset, blksize);
374                 if (bp) {
375                         error = 0;
376                         goto skip;
377                 }
378
379                 /*
380                  * MPUNSAFE
381                  */
382                 if (got_fstoken == 0) {
383                         lwkt_gettoken(&hmp->fs_token);
384                         got_fstoken = 1;
385                         hammer_start_transaction(&trans, ip->hmp);
386                 }
387
388                 if (hammer_cluster_enable) {
389                         /*
390                          * Use file_limit to prevent cluster_read() from
391                          * creating buffers of the wrong block size past
392                          * the demarc.
393                          */
394                         file_limit = ip->ino_data.size;
395                         if (base_offset < HAMMER_XDEMARC &&
396                             file_limit > HAMMER_XDEMARC) {
397                                 file_limit = HAMMER_XDEMARC;
398                         }
399                         error = cluster_read(ap->a_vp,
400                                              file_limit, base_offset,
401                                              blksize, uio->uio_resid,
402                                              seqcount * BKVASIZE, &bp);
403                 } else {
404                         error = bread(ap->a_vp, base_offset, blksize, &bp);
405                 }
406                 if (error) {
407                         brelse(bp);
408                         break;
409                 }
410 skip:
411                 if ((hammer_debug_io & 0x0001) && (bp->b_flags & B_IODEBUG)) {
412                         kprintf("doff %016jx read file %016jx@%016jx\n",
413                                 (intmax_t)bp->b_bio2.bio_offset,
414                                 (intmax_t)ip->obj_id,
415                                 (intmax_t)bp->b_loffset);
416                 }
417                 bp->b_flags &= ~B_IODEBUG;
418
419                 /* bp->b_flags |= B_CLUSTEROK; temporarily disabled */
420                 n = blksize - offset;
421                 if (n > uio->uio_resid)
422                         n = uio->uio_resid;
423                 if (n > ip->ino_data.size - uio->uio_offset)
424                         n = (int)(ip->ino_data.size - uio->uio_offset);
425                 if (got_fstoken)
426                         lwkt_reltoken(&hmp->fs_token);
427
428                 /*
429                  * Set B_AGE, data has a lower priority than meta-data.
430                  *
431                  * Use a hold/unlock/drop sequence to run the uiomove
432                  * with the buffer unlocked, avoiding deadlocks against
433                  * read()s on mmap()'d spaces.
434                  */
435                 bp->b_flags |= B_AGE;
436                 bqhold(bp);
437                 bqrelse(bp);
438                 error = uiomove((char *)bp->b_data + offset, n, uio);
439                 bqdrop(bp);
440
441                 if (got_fstoken)
442                         lwkt_gettoken(&hmp->fs_token);
443
444                 if (error)
445                         break;
446                 hammer_stats_file_read += n;
447         }
448
449         /*
450          * XXX only update the atime if we had to get the MP lock.
451          * XXX hack hack hack, fixme.
452          */
453         if (got_fstoken) {
454                 if ((ip->flags & HAMMER_INODE_RO) == 0 &&
455                     (ip->hmp->mp->mnt_flag & MNT_NOATIME) == 0) {
456                         ip->ino_data.atime = trans.time;
457                         hammer_modify_inode(&trans, ip, HAMMER_INODE_ATIME);
458                 }
459                 hammer_done_transaction(&trans);
460                 lwkt_reltoken(&hmp->fs_token);
461         }
462         return (error);
463 }
464
465 /*
466  * hammer_vop_write { vp, uio, ioflag, cred }
467  */
468 static
469 int
470 hammer_vop_write(struct vop_write_args *ap)
471 {
472         struct hammer_transaction trans;
473         struct hammer_inode *ip;
474         hammer_mount_t hmp;
475         struct uio *uio;
476         int offset;
477         off_t base_offset;
478         struct buf *bp;
479         int kflags;
480         int error;
481         int n;
482         int flags;
483         int seqcount;
484         int bigwrite;
485
486         if (ap->a_vp->v_type != VREG)
487                 return (EINVAL);
488         ip = VTOI(ap->a_vp);
489         hmp = ip->hmp;
490         error = 0;
491         kflags = 0;
492         seqcount = ap->a_ioflag >> 16;
493
494         if (ip->flags & HAMMER_INODE_RO)
495                 return (EROFS);
496
497         /*
498          * Create a transaction to cover the operations we perform.
499          */
500         lwkt_gettoken(&hmp->fs_token);
501         hammer_start_transaction(&trans, hmp);
502         uio = ap->a_uio;
503
504         /*
505          * Check append mode
506          */
507         if (ap->a_ioflag & IO_APPEND)
508                 uio->uio_offset = ip->ino_data.size;
509
510         /*
511          * Check for illegal write offsets.  Valid range is 0...2^63-1.
512          *
513          * NOTE: the base_off assignment is required to work around what
514          * I consider to be a GCC-4 optimization bug.
515          */
516         if (uio->uio_offset < 0) {
517                 hammer_done_transaction(&trans);
518                 lwkt_reltoken(&hmp->fs_token);
519                 return (EFBIG);
520         }
521         base_offset = uio->uio_offset + uio->uio_resid; /* work around gcc-4 */
522         if (uio->uio_resid > 0 && base_offset <= uio->uio_offset) {
523                 hammer_done_transaction(&trans);
524                 lwkt_reltoken(&hmp->fs_token);
525                 return (EFBIG);
526         }
527
528         /*
529          * If reading or writing a huge amount of data we have to break
530          * atomicy and allow the operation to be interrupted by a signal
531          * or it can DOS the machine.
532          *
533          * Preset redo_count so we stop generating REDOs earlier if the
534          * limit is exceeded.
535          */
536         bigwrite = (uio->uio_resid > 100 * 1024 * 1024);
537         if ((ip->flags & HAMMER_INODE_REDO) &&
538             ip->redo_count < hammer_limit_redo) {
539                 ip->redo_count += uio->uio_resid;
540         }
541
542         /*
543          * Access the data typically in HAMMER_BUFSIZE blocks via the
544          * buffer cache, but HAMMER may use a variable block size based
545          * on the offset.
546          */
547         while (uio->uio_resid > 0) {
548                 int fixsize = 0;
549                 int blksize;
550                 int blkmask;
551                 int trivial;
552                 int endofblk;
553                 off_t nsize;
554
555                 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_WRITE)) != 0)
556                         break;
557                 if (bigwrite && (error = hammer_signal_check(hmp)) != 0)
558                         break;
559
560                 blksize = hammer_blocksize(uio->uio_offset);
561
562                 /*
563                  * Do not allow HAMMER to blow out the buffer cache.  Very
564                  * large UIOs can lockout other processes due to bwillwrite()
565                  * mechanics.
566                  *
567                  * The hammer inode is not locked during these operations.
568                  * The vnode is locked which can interfere with the pageout
569                  * daemon for non-UIO_NOCOPY writes but should not interfere
570                  * with the buffer cache.  Even so, we cannot afford to
571                  * allow the pageout daemon to build up too many dirty buffer
572                  * cache buffers.
573                  *
574                  * Only call this if we aren't being recursively called from
575                  * a virtual disk device (vn), else we may deadlock.
576                  */
577                 if ((ap->a_ioflag & IO_RECURSE) == 0)
578                         bwillwrite(blksize);
579
580                 /*
581                  * Control the number of pending records associated with
582                  * this inode.  If too many have accumulated start a
583                  * flush.  Try to maintain a pipeline with the flusher.
584                  */
585                 if (ip->rsv_recs >= hammer_limit_inode_recs) {
586                         hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
587                 }
588                 if (ip->rsv_recs >= hammer_limit_inode_recs * 2) {
589                         while (ip->rsv_recs >= hammer_limit_inode_recs) {
590                                 tsleep(&ip->rsv_recs, 0, "hmrwww", hz);
591                         }
592                         hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
593                 }
594
595 #if 0
596                 /*
597                  * Do not allow HAMMER to blow out system memory by
598                  * accumulating too many records.   Records are so well
599                  * decoupled from the buffer cache that it is possible
600                  * for userland to push data out to the media via
601                  * direct-write, but build up the records queued to the
602                  * backend faster then the backend can flush them out.
603                  * HAMMER has hit its write limit but the frontend has
604                  * no pushback to slow it down.
605                  */
606                 if (hmp->rsv_recs > hammer_limit_recs / 2) {
607                         /*
608                          * Get the inode on the flush list
609                          */
610                         if (ip->rsv_recs >= 64)
611                                 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
612                         else if (ip->rsv_recs >= 16)
613                                 hammer_flush_inode(ip, 0);
614
615                         /*
616                          * Keep the flusher going if the system keeps
617                          * queueing records.
618                          */
619                         delta = hmp->count_newrecords -
620                                 hmp->last_newrecords;
621                         if (delta < 0 || delta > hammer_limit_recs / 2) {
622                                 hmp->last_newrecords = hmp->count_newrecords;
623                                 hammer_sync_hmp(hmp, MNT_NOWAIT);
624                         }
625
626                         /*
627                          * If we have gotten behind start slowing
628                          * down the writers.
629                          */
630                         delta = (hmp->rsv_recs - hammer_limit_recs) *
631                                 hz / hammer_limit_recs;
632                         if (delta > 0)
633                                 tsleep(&trans, 0, "hmrslo", delta);
634                 }
635 #endif
636
637                 /*
638                  * Calculate the blocksize at the current offset and figure
639                  * out how much we can actually write.
640                  */
641                 blkmask = blksize - 1;
642                 offset = (int)uio->uio_offset & blkmask;
643                 base_offset = uio->uio_offset & ~(int64_t)blkmask;
644                 n = blksize - offset;
645                 if (n > uio->uio_resid) {
646                         n = uio->uio_resid;
647                         endofblk = 0;
648                 } else {
649                         endofblk = 1;
650                 }
651                 nsize = uio->uio_offset + n;
652                 if (nsize > ip->ino_data.size) {
653                         if (uio->uio_offset > ip->ino_data.size)
654                                 trivial = 0;
655                         else
656                                 trivial = 1;
657                         nvextendbuf(ap->a_vp,
658                                     ip->ino_data.size,
659                                     nsize,
660                                     hammer_blocksize(ip->ino_data.size),
661                                     hammer_blocksize(nsize),
662                                     hammer_blockoff(ip->ino_data.size),
663                                     hammer_blockoff(nsize),
664                                     trivial);
665                         fixsize = 1;
666                         kflags |= NOTE_EXTEND;
667                 }
668
669                 if (uio->uio_segflg == UIO_NOCOPY) {
670                         /*
671                          * Issuing a write with the same data backing the
672                          * buffer.  Instantiate the buffer to collect the
673                          * backing vm pages, then read-in any missing bits.
674                          *
675                          * This case is used by vop_stdputpages().
676                          */
677                         bp = getblk(ap->a_vp, base_offset,
678                                     blksize, GETBLK_BHEAVY, 0);
679                         if ((bp->b_flags & B_CACHE) == 0) {
680                                 bqrelse(bp);
681                                 error = bread(ap->a_vp, base_offset,
682                                               blksize, &bp);
683                         }
684                 } else if (offset == 0 && uio->uio_resid >= blksize) {
685                         /*
686                          * Even though we are entirely overwriting the buffer
687                          * we may still have to zero it out to avoid a 
688                          * mmap/write visibility issue.
689                          */
690                         bp = getblk(ap->a_vp, base_offset, blksize, GETBLK_BHEAVY, 0);
691                         if ((bp->b_flags & B_CACHE) == 0)
692                                 vfs_bio_clrbuf(bp);
693                 } else if (base_offset >= ip->ino_data.size) {
694                         /*
695                          * If the base offset of the buffer is beyond the
696                          * file EOF, we don't have to issue a read.
697                          */
698                         bp = getblk(ap->a_vp, base_offset,
699                                     blksize, GETBLK_BHEAVY, 0);
700                         vfs_bio_clrbuf(bp);
701                 } else {
702                         /*
703                          * Partial overwrite, read in any missing bits then
704                          * replace the portion being written.
705                          */
706                         error = bread(ap->a_vp, base_offset, blksize, &bp);
707                         if (error == 0)
708                                 bheavy(bp);
709                 }
710                 if (error == 0) {
711                         lwkt_reltoken(&hmp->fs_token);
712                         error = uiomove(bp->b_data + offset, n, uio);
713                         lwkt_gettoken(&hmp->fs_token);
714                 }
715
716                 /*
717                  * Generate REDO records if enabled and redo_count will not
718                  * exceeded the limit.
719                  *
720                  * If redo_count exceeds the limit we stop generating records
721                  * and clear HAMMER_INODE_REDO.  This will cause the next
722                  * fsync() to do a full meta-data sync instead of just an
723                  * UNDO/REDO fifo update.
724                  *
725                  * When clearing HAMMER_INODE_REDO any pre-existing REDOs
726                  * will still be tracked.  The tracks will be terminated
727                  * when the related meta-data (including possible data
728                  * modifications which are not tracked via REDO) is
729                  * flushed.
730                  */
731                 if ((ip->flags & HAMMER_INODE_REDO) && error == 0) {
732                         if (ip->redo_count < hammer_limit_redo) {
733                                 bp->b_flags |= B_VFSFLAG1;
734                                 error = hammer_generate_redo(&trans, ip,
735                                                      base_offset + offset,
736                                                      HAMMER_REDO_WRITE,
737                                                      bp->b_data + offset,
738                                                      (size_t)n);
739                         } else {
740                                 ip->flags &= ~HAMMER_INODE_REDO;
741                         }
742                 }
743
744                 /*
745                  * If we screwed up we have to undo any VM size changes we
746                  * made.
747                  */
748                 if (error) {
749                         brelse(bp);
750                         if (fixsize) {
751                                 nvtruncbuf(ap->a_vp, ip->ino_data.size,
752                                           hammer_blocksize(ip->ino_data.size),
753                                           hammer_blockoff(ip->ino_data.size));
754                         }
755                         break;
756                 }
757                 kflags |= NOTE_WRITE;
758                 hammer_stats_file_write += n;
759                 /* bp->b_flags |= B_CLUSTEROK; temporarily disabled */
760                 if (ip->ino_data.size < uio->uio_offset) {
761                         ip->ino_data.size = uio->uio_offset;
762                         flags = HAMMER_INODE_SDIRTY;
763                 } else {
764                         flags = 0;
765                 }
766                 ip->ino_data.mtime = trans.time;
767                 flags |= HAMMER_INODE_MTIME | HAMMER_INODE_BUFS;
768                 hammer_modify_inode(&trans, ip, flags);
769
770                 /*
771                  * Once we dirty the buffer any cached zone-X offset
772                  * becomes invalid.  HAMMER NOTE: no-history mode cannot 
773                  * allow overwriting over the same data sector unless
774                  * we provide UNDOs for the old data, which we don't.
775                  */
776                 bp->b_bio2.bio_offset = NOOFFSET;
777
778                 /*
779                  * Final buffer disposition.
780                  *
781                  * Because meta-data updates are deferred, HAMMER is
782                  * especially sensitive to excessive bdwrite()s because
783                  * the I/O stream is not broken up by disk reads.  So the
784                  * buffer cache simply cannot keep up.
785                  *
786                  * WARNING!  blksize is variable.  cluster_write() is
787                  *           expected to not blow up if it encounters
788                  *           buffers that do not match the passed blksize.
789                  *
790                  * NOTE!  Hammer shouldn't need to bawrite()/cluster_write().
791                  *        The ip->rsv_recs check should burst-flush the data.
792                  *        If we queue it immediately the buf could be left
793                  *        locked on the device queue for a very long time.
794                  *
795                  * NOTE!  To avoid degenerate stalls due to mismatched block
796                  *        sizes we only honor IO_DIRECT on the write which
797                  *        abuts the end of the buffer.  However, we must
798                  *        honor IO_SYNC in case someone is silly enough to
799                  *        configure a HAMMER file as swap, or when HAMMER
800                  *        is serving NFS (for commits).  Ick ick.
801                  */
802                 bp->b_flags |= B_AGE;
803                 if (ap->a_ioflag & IO_SYNC) {
804                         bwrite(bp);
805                 } else if ((ap->a_ioflag & IO_DIRECT) && endofblk) {
806                         bawrite(bp);
807                 } else {
808 #if 0
809                 if (offset + n == blksize) {
810                         if (hammer_cluster_enable == 0 ||
811                             (ap->a_vp->v_mount->mnt_flag & MNT_NOCLUSTERW)) {
812                                 bawrite(bp);
813                         } else {
814                                 cluster_write(bp, ip->ino_data.size,
815                                               blksize, seqcount);
816                         }
817                 } else {
818 #endif
819                         bdwrite(bp);
820                 }
821         }
822         hammer_done_transaction(&trans);
823         hammer_knote(ap->a_vp, kflags);
824         lwkt_reltoken(&hmp->fs_token);
825         return (error);
826 }
827
828 /*
829  * hammer_vop_access { vp, mode, cred }
830  *
831  * MPSAFE - does not require fs_token
832  */
833 static
834 int
835 hammer_vop_access(struct vop_access_args *ap)
836 {
837         struct hammer_inode *ip = VTOI(ap->a_vp);
838         uid_t uid;
839         gid_t gid;
840         int error;
841
842         ++hammer_stats_file_iopsr;
843         uid = hammer_to_unix_xid(&ip->ino_data.uid);
844         gid = hammer_to_unix_xid(&ip->ino_data.gid);
845
846         error = vop_helper_access(ap, uid, gid, ip->ino_data.mode,
847                                   ip->ino_data.uflags);
848         return (error);
849 }
850
851 /*
852  * hammer_vop_advlock { vp, id, op, fl, flags }
853  *
854  * MPSAFE - does not require fs_token
855  */
856 static
857 int
858 hammer_vop_advlock(struct vop_advlock_args *ap)
859 {
860         hammer_inode_t ip = VTOI(ap->a_vp);
861
862         return (lf_advlock(ap, &ip->advlock, ip->ino_data.size));
863 }
864
865 /*
866  * hammer_vop_close { vp, fflag }
867  *
868  * We can only sync-on-close for normal closes.  XXX disabled for now.
869  */
870 static
871 int
872 hammer_vop_close(struct vop_close_args *ap)
873 {
874 #if 0
875         struct vnode *vp = ap->a_vp;
876         hammer_inode_t ip = VTOI(vp);
877         int waitfor;
878         if (ip->flags & (HAMMER_INODE_CLOSESYNC|HAMMER_INODE_CLOSEASYNC)) {
879                 if (vn_islocked(vp) == LK_EXCLUSIVE &&
880                     (vp->v_flag & (VINACTIVE|VRECLAIMED)) == 0) {
881                         if (ip->flags & HAMMER_INODE_CLOSESYNC)
882                                 waitfor = MNT_WAIT;
883                         else
884                                 waitfor = MNT_NOWAIT;
885                         ip->flags &= ~(HAMMER_INODE_CLOSESYNC |
886                                        HAMMER_INODE_CLOSEASYNC);
887                         VOP_FSYNC(vp, MNT_NOWAIT, waitfor);
888                 }
889         }
890 #endif
891         return (vop_stdclose(ap));
892 }
893
894 /*
895  * hammer_vop_ncreate { nch, dvp, vpp, cred, vap }
896  *
897  * The operating system has already ensured that the directory entry
898  * does not exist and done all appropriate namespace locking.
899  */
900 static
901 int
902 hammer_vop_ncreate(struct vop_ncreate_args *ap)
903 {
904         struct hammer_transaction trans;
905         struct hammer_inode *dip;
906         struct hammer_inode *nip;
907         struct nchandle *nch;
908         hammer_mount_t hmp;
909         int error;
910
911         nch = ap->a_nch;
912         dip = VTOI(ap->a_dvp);
913         hmp = dip->hmp;
914
915         if (dip->flags & HAMMER_INODE_RO)
916                 return (EROFS);
917         if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0)
918                 return (error);
919
920         /*
921          * Create a transaction to cover the operations we perform.
922          */
923         lwkt_gettoken(&hmp->fs_token);
924         hammer_start_transaction(&trans, hmp);
925         ++hammer_stats_file_iopsw;
926
927         /*
928          * Create a new filesystem object of the requested type.  The
929          * returned inode will be referenced and shared-locked to prevent
930          * it from being moved to the flusher.
931          */
932         error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
933                                     dip, nch->ncp->nc_name, nch->ncp->nc_nlen,
934                                     NULL, &nip);
935         if (error) {
936                 hkprintf("hammer_create_inode error %d\n", error);
937                 hammer_done_transaction(&trans);
938                 *ap->a_vpp = NULL;
939                 lwkt_reltoken(&hmp->fs_token);
940                 return (error);
941         }
942
943         /*
944          * Add the new filesystem object to the directory.  This will also
945          * bump the inode's link count.
946          */
947         error = hammer_ip_add_directory(&trans, dip,
948                                         nch->ncp->nc_name, nch->ncp->nc_nlen,
949                                         nip);
950         if (error)
951                 hkprintf("hammer_ip_add_directory error %d\n", error);
952
953         /*
954          * Finish up.
955          */
956         if (error) {
957                 hammer_rel_inode(nip, 0);
958                 hammer_done_transaction(&trans);
959                 *ap->a_vpp = NULL;
960         } else {
961                 error = hammer_get_vnode(nip, ap->a_vpp);
962                 hammer_done_transaction(&trans);
963                 hammer_rel_inode(nip, 0);
964                 if (error == 0) {
965                         cache_setunresolved(ap->a_nch);
966                         cache_setvp(ap->a_nch, *ap->a_vpp);
967                 }
968                 hammer_knote(ap->a_dvp, NOTE_WRITE);
969         }
970         lwkt_reltoken(&hmp->fs_token);
971         return (error);
972 }
973
974 /*
975  * hammer_vop_getattr { vp, vap }
976  *
977  * Retrieve an inode's attribute information.  When accessing inodes
978  * historically we fake the atime field to ensure consistent results.
979  * The atime field is stored in the B-Tree element and allowed to be
980  * updated without cycling the element.
981  *
982  * MPSAFE - does not require fs_token
983  */
984 static
985 int
986 hammer_vop_getattr(struct vop_getattr_args *ap)
987 {
988         struct hammer_inode *ip = VTOI(ap->a_vp);
989         struct vattr *vap = ap->a_vap;
990
991         /*
992          * We want the fsid to be different when accessing a filesystem
993          * with different as-of's so programs like diff don't think
994          * the files are the same.
995          *
996          * We also want the fsid to be the same when comparing snapshots,
997          * or when comparing mirrors (which might be backed by different
998          * physical devices).  HAMMER fsids are based on the PFS's
999          * shared_uuid field.
1000          *
1001          * XXX there is a chance of collision here.  The va_fsid reported
1002          * by stat is different from the more involved fsid used in the
1003          * mount structure.
1004          */
1005         ++hammer_stats_file_iopsr;
1006         hammer_lock_sh(&ip->lock);
1007         vap->va_fsid = ip->pfsm->fsid_udev ^ (u_int32_t)ip->obj_asof ^
1008                        (u_int32_t)(ip->obj_asof >> 32);
1009
1010         vap->va_fileid = ip->ino_leaf.base.obj_id;
1011         vap->va_mode = ip->ino_data.mode;
1012         vap->va_nlink = ip->ino_data.nlinks;
1013         vap->va_uid = hammer_to_unix_xid(&ip->ino_data.uid);
1014         vap->va_gid = hammer_to_unix_xid(&ip->ino_data.gid);
1015         vap->va_rmajor = 0;
1016         vap->va_rminor = 0;
1017         vap->va_size = ip->ino_data.size;
1018
1019         /*
1020          * Special case for @@PFS softlinks.  The actual size of the
1021          * expanded softlink is "@@0x%016llx:%05d" == 26 bytes.
1022          * or for MAX_TID is    "@@-1:%05d" == 10 bytes.
1023          */
1024         if (ip->ino_data.obj_type == HAMMER_OBJTYPE_SOFTLINK &&
1025             ip->ino_data.size == 10 &&
1026             ip->obj_asof == HAMMER_MAX_TID &&
1027             ip->obj_localization == 0 &&
1028             strncmp(ip->ino_data.ext.symlink, "@@PFS", 5) == 0) {
1029                     if (ip->pfsm->pfsd.mirror_flags & HAMMER_PFSD_SLAVE)
1030                             vap->va_size = 26;
1031                     else
1032                             vap->va_size = 10;
1033         }
1034
1035         /*
1036          * We must provide a consistent atime and mtime for snapshots
1037          * so people can do a 'tar cf - ... | md5' on them and get
1038          * consistent results.
1039          */
1040         if (ip->flags & HAMMER_INODE_RO) {
1041                 hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_atime);
1042                 hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_mtime);
1043         } else {
1044                 hammer_time_to_timespec(ip->ino_data.atime, &vap->va_atime);
1045                 hammer_time_to_timespec(ip->ino_data.mtime, &vap->va_mtime);
1046         }
1047         hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_ctime);
1048         vap->va_flags = ip->ino_data.uflags;
1049         vap->va_gen = 1;        /* hammer inums are unique for all time */
1050         vap->va_blocksize = HAMMER_BUFSIZE;
1051         if (ip->ino_data.size >= HAMMER_XDEMARC) {
1052                 vap->va_bytes = (ip->ino_data.size + HAMMER_XBUFMASK64) &
1053                                 ~HAMMER_XBUFMASK64;
1054         } else if (ip->ino_data.size > HAMMER_BUFSIZE / 2) {
1055                 vap->va_bytes = (ip->ino_data.size + HAMMER_BUFMASK64) &
1056                                 ~HAMMER_BUFMASK64;
1057         } else {
1058                 vap->va_bytes = (ip->ino_data.size + 15) & ~15;
1059         }
1060
1061         vap->va_type = hammer_get_vnode_type(ip->ino_data.obj_type);
1062         vap->va_filerev = 0;    /* XXX */
1063         vap->va_uid_uuid = ip->ino_data.uid;
1064         vap->va_gid_uuid = ip->ino_data.gid;
1065         vap->va_fsid_uuid = ip->hmp->fsid;
1066         vap->va_vaflags = VA_UID_UUID_VALID | VA_GID_UUID_VALID |
1067                           VA_FSID_UUID_VALID;
1068
1069         switch (ip->ino_data.obj_type) {
1070         case HAMMER_OBJTYPE_CDEV:
1071         case HAMMER_OBJTYPE_BDEV:
1072                 vap->va_rmajor = ip->ino_data.rmajor;
1073                 vap->va_rminor = ip->ino_data.rminor;
1074                 break;
1075         default:
1076                 break;
1077         }
1078         hammer_unlock(&ip->lock);
1079         return(0);
1080 }
1081
1082 /*
1083  * hammer_vop_nresolve { nch, dvp, cred }
1084  *
1085  * Locate the requested directory entry.
1086  */
1087 static
1088 int
1089 hammer_vop_nresolve(struct vop_nresolve_args *ap)
1090 {
1091         struct hammer_transaction trans;
1092         struct namecache *ncp;
1093         hammer_mount_t hmp;
1094         hammer_inode_t dip;
1095         hammer_inode_t ip;
1096         hammer_tid_t asof;
1097         struct hammer_cursor cursor;
1098         struct vnode *vp;
1099         int64_t namekey;
1100         int error;
1101         int i;
1102         int nlen;
1103         int flags;
1104         int ispfs;
1105         int64_t obj_id;
1106         u_int32_t localization;
1107         u_int32_t max_iterations;
1108
1109         /*
1110          * Misc initialization, plus handle as-of name extensions.  Look for
1111          * the '@@' extension.  Note that as-of files and directories cannot
1112          * be modified.
1113          */
1114         dip = VTOI(ap->a_dvp);
1115         ncp = ap->a_nch->ncp;
1116         asof = dip->obj_asof;
1117         localization = dip->obj_localization;   /* for code consistency */
1118         nlen = ncp->nc_nlen;
1119         flags = dip->flags & HAMMER_INODE_RO;
1120         ispfs = 0;
1121         hmp = dip->hmp;
1122
1123         lwkt_gettoken(&hmp->fs_token);
1124         hammer_simple_transaction(&trans, hmp);
1125         ++hammer_stats_file_iopsr;
1126
1127         for (i = 0; i < nlen; ++i) {
1128                 if (ncp->nc_name[i] == '@' && ncp->nc_name[i+1] == '@') {
1129                         error = hammer_str_to_tid(ncp->nc_name + i + 2,
1130                                                   &ispfs, &asof, &localization);
1131                         if (error != 0) {
1132                                 i = nlen;
1133                                 break;
1134                         }
1135                         if (asof != HAMMER_MAX_TID)
1136                                 flags |= HAMMER_INODE_RO;
1137                         break;
1138                 }
1139         }
1140         nlen = i;
1141
1142         /*
1143          * If this is a PFS softlink we dive into the PFS
1144          */
1145         if (ispfs && nlen == 0) {
1146                 ip = hammer_get_inode(&trans, dip, HAMMER_OBJID_ROOT,
1147                                       asof, localization,
1148                                       flags, &error);
1149                 if (error == 0) {
1150                         error = hammer_get_vnode(ip, &vp);
1151                         hammer_rel_inode(ip, 0);
1152                 } else {
1153                         vp = NULL;
1154                 }
1155                 if (error == 0) {
1156                         vn_unlock(vp);
1157                         cache_setvp(ap->a_nch, vp);
1158                         vrele(vp);
1159                 }
1160                 goto done;
1161         }
1162
1163         /*
1164          * If there is no path component the time extension is relative to dip.
1165          * e.g. "fubar/@@<snapshot>"
1166          *
1167          * "." is handled by the kernel, but ".@@<snapshot>" is not.
1168          * e.g. "fubar/.@@<snapshot>"
1169          *
1170          * ".." is handled by the kernel.  We do not currently handle
1171          * "..@<snapshot>".
1172          */
1173         if (nlen == 0 || (nlen == 1 && ncp->nc_name[0] == '.')) {
1174                 ip = hammer_get_inode(&trans, dip, dip->obj_id,
1175                                       asof, dip->obj_localization,
1176                                       flags, &error);
1177                 if (error == 0) {
1178                         error = hammer_get_vnode(ip, &vp);
1179                         hammer_rel_inode(ip, 0);
1180                 } else {
1181                         vp = NULL;
1182                 }
1183                 if (error == 0) {
1184                         vn_unlock(vp);
1185                         cache_setvp(ap->a_nch, vp);
1186                         vrele(vp);
1187                 }
1188                 goto done;
1189         }
1190
1191         /*
1192          * Calculate the namekey and setup the key range for the scan.  This
1193          * works kinda like a chained hash table where the lower 32 bits
1194          * of the namekey synthesize the chain.
1195          *
1196          * The key range is inclusive of both key_beg and key_end.
1197          */
1198         namekey = hammer_directory_namekey(dip, ncp->nc_name, nlen,
1199                                            &max_iterations);
1200
1201         error = hammer_init_cursor(&trans, &cursor, &dip->cache[1], dip);
1202         cursor.key_beg.localization = dip->obj_localization +
1203                                       hammer_dir_localization(dip);
1204         cursor.key_beg.obj_id = dip->obj_id;
1205         cursor.key_beg.key = namekey;
1206         cursor.key_beg.create_tid = 0;
1207         cursor.key_beg.delete_tid = 0;
1208         cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1209         cursor.key_beg.obj_type = 0;
1210
1211         cursor.key_end = cursor.key_beg;
1212         cursor.key_end.key += max_iterations;
1213         cursor.asof = asof;
1214         cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
1215
1216         /*
1217          * Scan all matching records (the chain), locate the one matching
1218          * the requested path component.
1219          *
1220          * The hammer_ip_*() functions merge in-memory records with on-disk
1221          * records for the purposes of the search.
1222          */
1223         obj_id = 0;
1224         localization = HAMMER_DEF_LOCALIZATION;
1225
1226         if (error == 0) {
1227                 error = hammer_ip_first(&cursor);
1228                 while (error == 0) {
1229                         error = hammer_ip_resolve_data(&cursor);
1230                         if (error)
1231                                 break;
1232                         if (nlen == cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF &&
1233                             bcmp(ncp->nc_name, cursor.data->entry.name, nlen) == 0) {
1234                                 obj_id = cursor.data->entry.obj_id;
1235                                 localization = cursor.data->entry.localization;
1236                                 break;
1237                         }
1238                         error = hammer_ip_next(&cursor);
1239                 }
1240         }
1241         hammer_done_cursor(&cursor);
1242
1243         /*
1244          * Lookup the obj_id.  This should always succeed.  If it does not
1245          * the filesystem may be damaged and we return a dummy inode.
1246          */
1247         if (error == 0) {
1248                 ip = hammer_get_inode(&trans, dip, obj_id,
1249                                       asof, localization,
1250                                       flags, &error);
1251                 if (error == ENOENT) {
1252                         kprintf("HAMMER: WARNING: Missing "
1253                                 "inode for dirent \"%s\"\n"
1254                                 "\tobj_id = %016llx, asof=%016llx, lo=%08x\n",
1255                                 ncp->nc_name,
1256                                 (long long)obj_id, (long long)asof,
1257                                 localization);
1258                         error = 0;
1259                         ip = hammer_get_dummy_inode(&trans, dip, obj_id,
1260                                                     asof, localization,
1261                                                     flags, &error);
1262                 }
1263                 if (error == 0) {
1264                         error = hammer_get_vnode(ip, &vp);
1265                         hammer_rel_inode(ip, 0);
1266                 } else {
1267                         vp = NULL;
1268                 }
1269                 if (error == 0) {
1270                         vn_unlock(vp);
1271                         cache_setvp(ap->a_nch, vp);
1272                         vrele(vp);
1273                 }
1274         } else if (error == ENOENT) {
1275                 cache_setvp(ap->a_nch, NULL);
1276         }
1277 done:
1278         hammer_done_transaction(&trans);
1279         lwkt_reltoken(&hmp->fs_token);
1280         return (error);
1281 }
1282
1283 /*
1284  * hammer_vop_nlookupdotdot { dvp, vpp, cred }
1285  *
1286  * Locate the parent directory of a directory vnode.
1287  *
1288  * dvp is referenced but not locked.  *vpp must be returned referenced and
1289  * locked.  A parent_obj_id of 0 does not necessarily indicate that we are
1290  * at the root, instead it could indicate that the directory we were in was
1291  * removed.
1292  *
1293  * NOTE: as-of sequences are not linked into the directory structure.  If
1294  * we are at the root with a different asof then the mount point, reload
1295  * the same directory with the mount point's asof.   I'm not sure what this
1296  * will do to NFS.  We encode ASOF stamps in NFS file handles so it might not
1297  * get confused, but it hasn't been tested.
1298  */
1299 static
1300 int
1301 hammer_vop_nlookupdotdot(struct vop_nlookupdotdot_args *ap)
1302 {
1303         struct hammer_transaction trans;
1304         struct hammer_inode *dip;
1305         struct hammer_inode *ip;
1306         hammer_mount_t hmp;
1307         int64_t parent_obj_id;
1308         u_int32_t parent_obj_localization;
1309         hammer_tid_t asof;
1310         int error;
1311
1312         dip = VTOI(ap->a_dvp);
1313         asof = dip->obj_asof;
1314         hmp = dip->hmp;
1315
1316         /*
1317          * Whos are parent?  This could be the root of a pseudo-filesystem
1318          * whos parent is in another localization domain.
1319          */
1320         lwkt_gettoken(&hmp->fs_token);
1321         parent_obj_id = dip->ino_data.parent_obj_id;
1322         if (dip->obj_id == HAMMER_OBJID_ROOT)
1323                 parent_obj_localization = dip->ino_data.ext.obj.parent_obj_localization;
1324         else
1325                 parent_obj_localization = dip->obj_localization;
1326
1327         if (parent_obj_id == 0) {
1328                 if (dip->obj_id == HAMMER_OBJID_ROOT &&
1329                    asof != hmp->asof) {
1330                         parent_obj_id = dip->obj_id;
1331                         asof = hmp->asof;
1332                         *ap->a_fakename = kmalloc(19, M_TEMP, M_WAITOK);
1333                         ksnprintf(*ap->a_fakename, 19, "0x%016llx",
1334                                   (long long)dip->obj_asof);
1335                 } else {
1336                         *ap->a_vpp = NULL;
1337                         lwkt_reltoken(&hmp->fs_token);
1338                         return ENOENT;
1339                 }
1340         }
1341
1342         hammer_simple_transaction(&trans, hmp);
1343         ++hammer_stats_file_iopsr;
1344
1345         ip = hammer_get_inode(&trans, dip, parent_obj_id,
1346                               asof, parent_obj_localization,
1347                               dip->flags, &error);
1348         if (ip) {
1349                 error = hammer_get_vnode(ip, ap->a_vpp);
1350                 hammer_rel_inode(ip, 0);
1351         } else {
1352                 *ap->a_vpp = NULL;
1353         }
1354         hammer_done_transaction(&trans);
1355         lwkt_reltoken(&hmp->fs_token);
1356         return (error);
1357 }
1358
1359 /*
1360  * hammer_vop_nlink { nch, dvp, vp, cred }
1361  */
1362 static
1363 int
1364 hammer_vop_nlink(struct vop_nlink_args *ap)
1365 {
1366         struct hammer_transaction trans;
1367         struct hammer_inode *dip;
1368         struct hammer_inode *ip;
1369         struct nchandle *nch;
1370         hammer_mount_t hmp;
1371         int error;
1372
1373         if (ap->a_dvp->v_mount != ap->a_vp->v_mount)    
1374                 return(EXDEV);
1375
1376         nch = ap->a_nch;
1377         dip = VTOI(ap->a_dvp);
1378         ip = VTOI(ap->a_vp);
1379         hmp = dip->hmp;
1380
1381         if (dip->obj_localization != ip->obj_localization)
1382                 return(EXDEV);
1383
1384         if (dip->flags & HAMMER_INODE_RO)
1385                 return (EROFS);
1386         if (ip->flags & HAMMER_INODE_RO)
1387                 return (EROFS);
1388         if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0)
1389                 return (error);
1390
1391         /*
1392          * Create a transaction to cover the operations we perform.
1393          */
1394         lwkt_gettoken(&hmp->fs_token);
1395         hammer_start_transaction(&trans, hmp);
1396         ++hammer_stats_file_iopsw;
1397
1398         /*
1399          * Add the filesystem object to the directory.  Note that neither
1400          * dip nor ip are referenced or locked, but their vnodes are
1401          * referenced.  This function will bump the inode's link count.
1402          */
1403         error = hammer_ip_add_directory(&trans, dip,
1404                                         nch->ncp->nc_name, nch->ncp->nc_nlen,
1405                                         ip);
1406
1407         /*
1408          * Finish up.
1409          */
1410         if (error == 0) {
1411                 cache_setunresolved(nch);
1412                 cache_setvp(nch, ap->a_vp);
1413         }
1414         hammer_done_transaction(&trans);
1415         hammer_knote(ap->a_vp, NOTE_LINK);
1416         hammer_knote(ap->a_dvp, NOTE_WRITE);
1417         lwkt_reltoken(&hmp->fs_token);
1418         return (error);
1419 }
1420
1421 /*
1422  * hammer_vop_nmkdir { nch, dvp, vpp, cred, vap }
1423  *
1424  * The operating system has already ensured that the directory entry
1425  * does not exist and done all appropriate namespace locking.
1426  */
1427 static
1428 int
1429 hammer_vop_nmkdir(struct vop_nmkdir_args *ap)
1430 {
1431         struct hammer_transaction trans;
1432         struct hammer_inode *dip;
1433         struct hammer_inode *nip;
1434         struct nchandle *nch;
1435         hammer_mount_t hmp;
1436         int error;
1437
1438         nch = ap->a_nch;
1439         dip = VTOI(ap->a_dvp);
1440         hmp = dip->hmp;
1441
1442         if (dip->flags & HAMMER_INODE_RO)
1443                 return (EROFS);
1444         if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0)
1445                 return (error);
1446
1447         /*
1448          * Create a transaction to cover the operations we perform.
1449          */
1450         lwkt_gettoken(&hmp->fs_token);
1451         hammer_start_transaction(&trans, hmp);
1452         ++hammer_stats_file_iopsw;
1453
1454         /*
1455          * Create a new filesystem object of the requested type.  The
1456          * returned inode will be referenced but not locked.
1457          */
1458         error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
1459                                     dip, nch->ncp->nc_name, nch->ncp->nc_nlen,
1460                                     NULL, &nip);
1461         if (error) {
1462                 hkprintf("hammer_mkdir error %d\n", error);
1463                 hammer_done_transaction(&trans);
1464                 *ap->a_vpp = NULL;
1465                 lwkt_reltoken(&hmp->fs_token);
1466                 return (error);
1467         }
1468         /*
1469          * Add the new filesystem object to the directory.  This will also
1470          * bump the inode's link count.
1471          */
1472         error = hammer_ip_add_directory(&trans, dip,
1473                                         nch->ncp->nc_name, nch->ncp->nc_nlen,
1474                                         nip);
1475         if (error)
1476                 hkprintf("hammer_mkdir (add) error %d\n", error);
1477
1478         /*
1479          * Finish up.
1480          */
1481         if (error) {
1482                 hammer_rel_inode(nip, 0);
1483                 *ap->a_vpp = NULL;
1484         } else {
1485                 error = hammer_get_vnode(nip, ap->a_vpp);
1486                 hammer_rel_inode(nip, 0);
1487                 if (error == 0) {
1488                         cache_setunresolved(ap->a_nch);
1489                         cache_setvp(ap->a_nch, *ap->a_vpp);
1490                 }
1491         }
1492         hammer_done_transaction(&trans);
1493         if (error == 0)
1494                 hammer_knote(ap->a_dvp, NOTE_WRITE | NOTE_LINK);
1495         lwkt_reltoken(&hmp->fs_token);
1496         return (error);
1497 }
1498
1499 /*
1500  * hammer_vop_nmknod { nch, dvp, vpp, cred, vap }
1501  *
1502  * The operating system has already ensured that the directory entry
1503  * does not exist and done all appropriate namespace locking.
1504  */
1505 static
1506 int
1507 hammer_vop_nmknod(struct vop_nmknod_args *ap)
1508 {
1509         struct hammer_transaction trans;
1510         struct hammer_inode *dip;
1511         struct hammer_inode *nip;
1512         struct nchandle *nch;
1513         hammer_mount_t hmp;
1514         int error;
1515
1516         nch = ap->a_nch;
1517         dip = VTOI(ap->a_dvp);
1518         hmp = dip->hmp;
1519
1520         if (dip->flags & HAMMER_INODE_RO)
1521                 return (EROFS);
1522         if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0)
1523                 return (error);
1524
1525         /*
1526          * Create a transaction to cover the operations we perform.
1527          */
1528         lwkt_gettoken(&hmp->fs_token);
1529         hammer_start_transaction(&trans, hmp);
1530         ++hammer_stats_file_iopsw;
1531
1532         /*
1533          * Create a new filesystem object of the requested type.  The
1534          * returned inode will be referenced but not locked.
1535          *
1536          * If mknod specifies a directory a pseudo-fs is created.
1537          */
1538         error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
1539                                     dip, nch->ncp->nc_name, nch->ncp->nc_nlen,
1540                                     NULL, &nip);
1541         if (error) {
1542                 hammer_done_transaction(&trans);
1543                 *ap->a_vpp = NULL;
1544                 lwkt_reltoken(&hmp->fs_token);
1545                 return (error);
1546         }
1547
1548         /*
1549          * Add the new filesystem object to the directory.  This will also
1550          * bump the inode's link count.
1551          */
1552         error = hammer_ip_add_directory(&trans, dip,
1553                                         nch->ncp->nc_name, nch->ncp->nc_nlen,
1554                                         nip);
1555
1556         /*
1557          * Finish up.
1558          */
1559         if (error) {
1560                 hammer_rel_inode(nip, 0);
1561                 *ap->a_vpp = NULL;
1562         } else {
1563                 error = hammer_get_vnode(nip, ap->a_vpp);
1564                 hammer_rel_inode(nip, 0);
1565                 if (error == 0) {
1566                         cache_setunresolved(ap->a_nch);
1567                         cache_setvp(ap->a_nch, *ap->a_vpp);
1568                 }
1569         }
1570         hammer_done_transaction(&trans);
1571         if (error == 0)
1572                 hammer_knote(ap->a_dvp, NOTE_WRITE);
1573         lwkt_reltoken(&hmp->fs_token);
1574         return (error);
1575 }
1576
1577 /*
1578  * hammer_vop_open { vp, mode, cred, fp }
1579  *
1580  * MPSAFE (does not require fs_token)
1581  */
1582 static
1583 int
1584 hammer_vop_open(struct vop_open_args *ap)
1585 {
1586         hammer_inode_t ip;
1587
1588         ++hammer_stats_file_iopsr;
1589         ip = VTOI(ap->a_vp);
1590
1591         if ((ap->a_mode & FWRITE) && (ip->flags & HAMMER_INODE_RO))
1592                 return (EROFS);
1593         return(vop_stdopen(ap));
1594 }
1595
1596 /*
1597  * hammer_vop_print { vp }
1598  */
1599 static
1600 int
1601 hammer_vop_print(struct vop_print_args *ap)
1602 {
1603         return EOPNOTSUPP;
1604 }
1605
1606 /*
1607  * hammer_vop_readdir { vp, uio, cred, *eofflag, *ncookies, off_t **cookies }
1608  */
1609 static
1610 int
1611 hammer_vop_readdir(struct vop_readdir_args *ap)
1612 {
1613         struct hammer_transaction trans;
1614         struct hammer_cursor cursor;
1615         struct hammer_inode *ip;
1616         hammer_mount_t hmp;
1617         struct uio *uio;
1618         hammer_base_elm_t base;
1619         int error;
1620         int cookie_index;
1621         int ncookies;
1622         off_t *cookies;
1623         off_t saveoff;
1624         int r;
1625         int dtype;
1626
1627         ++hammer_stats_file_iopsr;
1628         ip = VTOI(ap->a_vp);
1629         uio = ap->a_uio;
1630         saveoff = uio->uio_offset;
1631         hmp = ip->hmp;
1632
1633         if (ap->a_ncookies) {
1634                 ncookies = uio->uio_resid / 16 + 1;
1635                 if (ncookies > 1024)
1636                         ncookies = 1024;
1637                 cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
1638                 cookie_index = 0;
1639         } else {
1640                 ncookies = -1;
1641                 cookies = NULL;
1642                 cookie_index = 0;
1643         }
1644
1645         lwkt_gettoken(&hmp->fs_token);
1646         hammer_simple_transaction(&trans, hmp);
1647
1648         /*
1649          * Handle artificial entries
1650          *
1651          * It should be noted that the minimum value for a directory
1652          * hash key on-media is 0x0000000100000000, so we can use anything
1653          * less then that to represent our 'special' key space.
1654          */
1655         error = 0;
1656         if (saveoff == 0) {
1657                 r = vop_write_dirent(&error, uio, ip->obj_id, DT_DIR, 1, ".");
1658                 if (r)
1659                         goto done;
1660                 if (cookies)
1661                         cookies[cookie_index] = saveoff;
1662                 ++saveoff;
1663                 ++cookie_index;
1664                 if (cookie_index == ncookies)
1665                         goto done;
1666         }
1667         if (saveoff == 1) {
1668                 if (ip->ino_data.parent_obj_id) {
1669                         r = vop_write_dirent(&error, uio,
1670                                              ip->ino_data.parent_obj_id,
1671                                              DT_DIR, 2, "..");
1672                 } else {
1673                         r = vop_write_dirent(&error, uio,
1674                                              ip->obj_id, DT_DIR, 2, "..");
1675                 }
1676                 if (r)
1677                         goto done;
1678                 if (cookies)
1679                         cookies[cookie_index] = saveoff;
1680                 ++saveoff;
1681                 ++cookie_index;
1682                 if (cookie_index == ncookies)
1683                         goto done;
1684         }
1685
1686         /*
1687          * Key range (begin and end inclusive) to scan.  Directory keys
1688          * directly translate to a 64 bit 'seek' position.
1689          */
1690         hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
1691         cursor.key_beg.localization = ip->obj_localization +
1692                                       hammer_dir_localization(ip);
1693         cursor.key_beg.obj_id = ip->obj_id;
1694         cursor.key_beg.create_tid = 0;
1695         cursor.key_beg.delete_tid = 0;
1696         cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1697         cursor.key_beg.obj_type = 0;
1698         cursor.key_beg.key = saveoff;
1699
1700         cursor.key_end = cursor.key_beg;
1701         cursor.key_end.key = HAMMER_MAX_KEY;
1702         cursor.asof = ip->obj_asof;
1703         cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
1704
1705         error = hammer_ip_first(&cursor);
1706
1707         while (error == 0) {
1708                 error = hammer_ip_resolve_data(&cursor);
1709                 if (error)
1710                         break;
1711                 base = &cursor.leaf->base;
1712                 saveoff = base->key;
1713                 KKASSERT(cursor.leaf->data_len > HAMMER_ENTRY_NAME_OFF);
1714
1715                 if (base->obj_id != ip->obj_id)
1716                         panic("readdir: bad record at %p", cursor.node);
1717
1718                 /*
1719                  * Convert pseudo-filesystems into softlinks
1720                  */
1721                 dtype = hammer_get_dtype(cursor.leaf->base.obj_type);
1722                 r = vop_write_dirent(
1723                              &error, uio, cursor.data->entry.obj_id,
1724                              dtype,
1725                              cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF ,
1726                              (void *)cursor.data->entry.name);
1727                 if (r)
1728                         break;
1729                 ++saveoff;
1730                 if (cookies)
1731                         cookies[cookie_index] = base->key;
1732                 ++cookie_index;
1733                 if (cookie_index == ncookies)
1734                         break;
1735                 error = hammer_ip_next(&cursor);
1736         }
1737         hammer_done_cursor(&cursor);
1738
1739 done:
1740         hammer_done_transaction(&trans);
1741
1742         if (ap->a_eofflag)
1743                 *ap->a_eofflag = (error == ENOENT);
1744         uio->uio_offset = saveoff;
1745         if (error && cookie_index == 0) {
1746                 if (error == ENOENT)
1747                         error = 0;
1748                 if (cookies) {
1749                         kfree(cookies, M_TEMP);
1750                         *ap->a_ncookies = 0;
1751                         *ap->a_cookies = NULL;
1752                 }
1753         } else {
1754                 if (error == ENOENT)
1755                         error = 0;
1756                 if (cookies) {
1757                         *ap->a_ncookies = cookie_index;
1758                         *ap->a_cookies = cookies;
1759                 }
1760         }
1761         lwkt_reltoken(&hmp->fs_token);
1762         return(error);
1763 }
1764
1765 /*
1766  * hammer_vop_readlink { vp, uio, cred }
1767  */
1768 static
1769 int
1770 hammer_vop_readlink(struct vop_readlink_args *ap)
1771 {
1772         struct hammer_transaction trans;
1773         struct hammer_cursor cursor;
1774         struct hammer_inode *ip;
1775         hammer_mount_t hmp;
1776         char buf[32];
1777         u_int32_t localization;
1778         hammer_pseudofs_inmem_t pfsm;
1779         int error;
1780
1781         ip = VTOI(ap->a_vp);
1782         hmp = ip->hmp;
1783
1784         lwkt_gettoken(&hmp->fs_token);
1785
1786         /*
1787          * Shortcut if the symlink data was stuffed into ino_data.
1788          *
1789          * Also expand special "@@PFS%05d" softlinks (expansion only
1790          * occurs for non-historical (current) accesses made from the
1791          * primary filesystem).
1792          */
1793         if (ip->ino_data.size <= HAMMER_INODE_BASESYMLEN) {
1794                 char *ptr;
1795                 int bytes;
1796
1797                 ptr = ip->ino_data.ext.symlink;
1798                 bytes = (int)ip->ino_data.size;
1799                 if (bytes == 10 &&
1800                     ip->obj_asof == HAMMER_MAX_TID &&
1801                     ip->obj_localization == 0 &&
1802                     strncmp(ptr, "@@PFS", 5) == 0) {
1803                         hammer_simple_transaction(&trans, hmp);
1804                         bcopy(ptr + 5, buf, 5);
1805                         buf[5] = 0;
1806                         localization = strtoul(buf, NULL, 10) << 16;
1807                         pfsm = hammer_load_pseudofs(&trans, localization,
1808                                                     &error);
1809                         if (error == 0) {
1810                                 if (pfsm->pfsd.mirror_flags &
1811                                     HAMMER_PFSD_SLAVE) {
1812                                         /* vap->va_size == 26 */
1813                                         ksnprintf(buf, sizeof(buf),
1814                                                   "@@0x%016llx:%05d",
1815                                                   (long long)pfsm->pfsd.sync_end_tid,
1816                                                   localization >> 16);
1817                                 } else {
1818                                         /* vap->va_size == 10 */
1819                                         ksnprintf(buf, sizeof(buf),
1820                                                   "@@-1:%05d",
1821                                                   localization >> 16);
1822 #if 0
1823                                         ksnprintf(buf, sizeof(buf),
1824                                                   "@@0x%016llx:%05d",
1825                                                   (long long)HAMMER_MAX_TID,
1826                                                   localization >> 16);
1827 #endif
1828                                 }
1829                                 ptr = buf;
1830                                 bytes = strlen(buf);
1831                         }
1832                         if (pfsm)
1833                                 hammer_rel_pseudofs(hmp, pfsm);
1834                         hammer_done_transaction(&trans);
1835                 }
1836                 error = uiomove(ptr, bytes, ap->a_uio);
1837                 lwkt_reltoken(&hmp->fs_token);
1838                 return(error);
1839         }
1840
1841         /*
1842          * Long version
1843          */
1844         hammer_simple_transaction(&trans, hmp);
1845         ++hammer_stats_file_iopsr;
1846         hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
1847
1848         /*
1849          * Key range (begin and end inclusive) to scan.  Directory keys
1850          * directly translate to a 64 bit 'seek' position.
1851          */
1852         cursor.key_beg.localization = ip->obj_localization +
1853                                       HAMMER_LOCALIZE_MISC;
1854         cursor.key_beg.obj_id = ip->obj_id;
1855         cursor.key_beg.create_tid = 0;
1856         cursor.key_beg.delete_tid = 0;
1857         cursor.key_beg.rec_type = HAMMER_RECTYPE_FIX;
1858         cursor.key_beg.obj_type = 0;
1859         cursor.key_beg.key = HAMMER_FIXKEY_SYMLINK;
1860         cursor.asof = ip->obj_asof;
1861         cursor.flags |= HAMMER_CURSOR_ASOF;
1862
1863         error = hammer_ip_lookup(&cursor);
1864         if (error == 0) {
1865                 error = hammer_ip_resolve_data(&cursor);
1866                 if (error == 0) {
1867                         KKASSERT(cursor.leaf->data_len >=
1868                                  HAMMER_SYMLINK_NAME_OFF);
1869                         error = uiomove(cursor.data->symlink.name,
1870                                         cursor.leaf->data_len -
1871                                                 HAMMER_SYMLINK_NAME_OFF,
1872                                         ap->a_uio);
1873                 }
1874         }
1875         hammer_done_cursor(&cursor);
1876         hammer_done_transaction(&trans);
1877         lwkt_reltoken(&hmp->fs_token);
1878         return(error);
1879 }
1880
1881 /*
1882  * hammer_vop_nremove { nch, dvp, cred }
1883  */
1884 static
1885 int
1886 hammer_vop_nremove(struct vop_nremove_args *ap)
1887 {
1888         struct hammer_transaction trans;
1889         struct hammer_inode *dip;
1890         hammer_mount_t hmp;
1891         int error;
1892
1893         dip = VTOI(ap->a_dvp);
1894         hmp = dip->hmp;
1895
1896         if (hammer_nohistory(dip) == 0 &&
1897             (error = hammer_checkspace(hmp, HAMMER_CHKSPC_REMOVE)) != 0) {
1898                 return (error);
1899         }
1900
1901         lwkt_gettoken(&hmp->fs_token);
1902         hammer_start_transaction(&trans, hmp);
1903         ++hammer_stats_file_iopsw;
1904         error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp, ap->a_cred, 0, 0);
1905         hammer_done_transaction(&trans);
1906         if (error == 0)
1907                 hammer_knote(ap->a_dvp, NOTE_WRITE);
1908         lwkt_reltoken(&hmp->fs_token);
1909         return (error);
1910 }
1911
1912 /*
1913  * hammer_vop_nrename { fnch, tnch, fdvp, tdvp, cred }
1914  */
1915 static
1916 int
1917 hammer_vop_nrename(struct vop_nrename_args *ap)
1918 {
1919         struct hammer_transaction trans;
1920         struct namecache *fncp;
1921         struct namecache *tncp;
1922         struct hammer_inode *fdip;
1923         struct hammer_inode *tdip;
1924         struct hammer_inode *ip;
1925         hammer_mount_t hmp;
1926         struct hammer_cursor cursor;
1927         int64_t namekey;
1928         u_int32_t max_iterations;
1929         int nlen, error;
1930
1931         if (ap->a_fdvp->v_mount != ap->a_tdvp->v_mount) 
1932                 return(EXDEV);
1933         if (ap->a_fdvp->v_mount != ap->a_fnch->ncp->nc_vp->v_mount)
1934                 return(EXDEV);
1935
1936         fdip = VTOI(ap->a_fdvp);
1937         tdip = VTOI(ap->a_tdvp);
1938         fncp = ap->a_fnch->ncp;
1939         tncp = ap->a_tnch->ncp;
1940         ip = VTOI(fncp->nc_vp);
1941         KKASSERT(ip != NULL);
1942
1943         hmp = ip->hmp;
1944
1945         if (fdip->obj_localization != tdip->obj_localization)
1946                 return(EXDEV);
1947         if (fdip->obj_localization != ip->obj_localization)
1948                 return(EXDEV);
1949
1950         if (fdip->flags & HAMMER_INODE_RO)
1951                 return (EROFS);
1952         if (tdip->flags & HAMMER_INODE_RO)
1953                 return (EROFS);
1954         if (ip->flags & HAMMER_INODE_RO)
1955                 return (EROFS);
1956         if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0)
1957                 return (error);
1958
1959         lwkt_gettoken(&hmp->fs_token);
1960         hammer_start_transaction(&trans, hmp);
1961         ++hammer_stats_file_iopsw;
1962
1963         /*
1964          * Remove tncp from the target directory and then link ip as
1965          * tncp. XXX pass trans to dounlink
1966          *
1967          * Force the inode sync-time to match the transaction so it is
1968          * in-sync with the creation of the target directory entry.
1969          */
1970         error = hammer_dounlink(&trans, ap->a_tnch, ap->a_tdvp,
1971                                 ap->a_cred, 0, -1);
1972         if (error == 0 || error == ENOENT) {
1973                 error = hammer_ip_add_directory(&trans, tdip,
1974                                                 tncp->nc_name, tncp->nc_nlen,
1975                                                 ip);
1976                 if (error == 0) {
1977                         ip->ino_data.parent_obj_id = tdip->obj_id;
1978                         ip->ino_data.ctime = trans.time;
1979                         hammer_modify_inode(&trans, ip, HAMMER_INODE_DDIRTY);
1980                 }
1981         }
1982         if (error)
1983                 goto failed; /* XXX */
1984
1985         /*
1986          * Locate the record in the originating directory and remove it.
1987          *
1988          * Calculate the namekey and setup the key range for the scan.  This
1989          * works kinda like a chained hash table where the lower 32 bits
1990          * of the namekey synthesize the chain.
1991          *
1992          * The key range is inclusive of both key_beg and key_end.
1993          */
1994         namekey = hammer_directory_namekey(fdip, fncp->nc_name, fncp->nc_nlen,
1995                                            &max_iterations);
1996 retry:
1997         hammer_init_cursor(&trans, &cursor, &fdip->cache[1], fdip);
1998         cursor.key_beg.localization = fdip->obj_localization +
1999                                       hammer_dir_localization(fdip);
2000         cursor.key_beg.obj_id = fdip->obj_id;
2001         cursor.key_beg.key = namekey;
2002         cursor.key_beg.create_tid = 0;
2003         cursor.key_beg.delete_tid = 0;
2004         cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
2005         cursor.key_beg.obj_type = 0;
2006
2007         cursor.key_end = cursor.key_beg;
2008         cursor.key_end.key += max_iterations;
2009         cursor.asof = fdip->obj_asof;
2010         cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
2011
2012         /*
2013          * Scan all matching records (the chain), locate the one matching
2014          * the requested path component.
2015          *
2016          * The hammer_ip_*() functions merge in-memory records with on-disk
2017          * records for the purposes of the search.
2018          */
2019         error = hammer_ip_first(&cursor);
2020         while (error == 0) {
2021                 if (hammer_ip_resolve_data(&cursor) != 0)
2022                         break;
2023                 nlen = cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF;
2024                 KKASSERT(nlen > 0);
2025                 if (fncp->nc_nlen == nlen &&
2026                     bcmp(fncp->nc_name, cursor.data->entry.name, nlen) == 0) {
2027                         break;
2028                 }
2029                 error = hammer_ip_next(&cursor);
2030         }
2031
2032         /*
2033          * If all is ok we have to get the inode so we can adjust nlinks.
2034          *
2035          * WARNING: hammer_ip_del_directory() may have to terminate the
2036          * cursor to avoid a recursion.  It's ok to call hammer_done_cursor()
2037          * twice.
2038          */
2039         if (error == 0)
2040                 error = hammer_ip_del_directory(&trans, &cursor, fdip, ip);
2041
2042         /*
2043          * XXX A deadlock here will break rename's atomicy for the purposes
2044          * of crash recovery.
2045          */
2046         if (error == EDEADLK) {
2047                 hammer_done_cursor(&cursor);
2048                 goto retry;
2049         }
2050
2051         /*
2052          * Cleanup and tell the kernel that the rename succeeded.
2053          *
2054          * NOTE: ip->vp, if non-NULL, cannot be directly referenced
2055          *       without formally acquiring the vp since the vp might
2056          *       have zero refs on it, or in the middle of a reclaim,
2057          *       etc.
2058          */
2059         hammer_done_cursor(&cursor);
2060         if (error == 0) {
2061                 cache_rename(ap->a_fnch, ap->a_tnch);
2062                 hammer_knote(ap->a_fdvp, NOTE_WRITE);
2063                 hammer_knote(ap->a_tdvp, NOTE_WRITE);
2064                 while (ip->vp) {
2065                         struct vnode *vp;
2066
2067                         error = hammer_get_vnode(ip, &vp);
2068                         if (error == 0 && vp) {
2069                                 vn_unlock(vp);
2070                                 hammer_knote(ip->vp, NOTE_RENAME);
2071                                 vrele(vp);
2072                                 break;
2073                         }
2074                         kprintf("Debug: HAMMER ip/vp race2 avoided\n");
2075                 }
2076         }
2077
2078 failed:
2079         hammer_done_transaction(&trans);
2080         lwkt_reltoken(&hmp->fs_token);
2081         return (error);
2082 }
2083
2084 /*
2085  * hammer_vop_nrmdir { nch, dvp, cred }
2086  */
2087 static
2088 int
2089 hammer_vop_nrmdir(struct vop_nrmdir_args *ap)
2090 {
2091         struct hammer_transaction trans;
2092         struct hammer_inode *dip;
2093         hammer_mount_t hmp;
2094         int error;
2095
2096         dip = VTOI(ap->a_dvp);
2097         hmp = dip->hmp;
2098
2099         if (hammer_nohistory(dip) == 0 &&
2100             (error = hammer_checkspace(hmp, HAMMER_CHKSPC_REMOVE)) != 0) {
2101                 return (error);
2102         }
2103
2104         lwkt_gettoken(&hmp->fs_token);
2105         hammer_start_transaction(&trans, hmp);
2106         ++hammer_stats_file_iopsw;
2107         error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp, ap->a_cred, 0, 1);
2108         hammer_done_transaction(&trans);
2109         if (error == 0)
2110                 hammer_knote(ap->a_dvp, NOTE_WRITE | NOTE_LINK);
2111         lwkt_reltoken(&hmp->fs_token);
2112         return (error);
2113 }
2114
2115 /*
2116  * hammer_vop_markatime { vp, cred }
2117  */
2118 static
2119 int
2120 hammer_vop_markatime(struct vop_markatime_args *ap)
2121 {
2122         struct hammer_transaction trans;
2123         struct hammer_inode *ip;
2124         hammer_mount_t hmp;
2125
2126         ip = VTOI(ap->a_vp);
2127         if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
2128                 return (EROFS);
2129         if (ip->flags & HAMMER_INODE_RO)
2130                 return (EROFS);
2131         hmp = ip->hmp;
2132         if (hmp->mp->mnt_flag & MNT_NOATIME)
2133                 return (0);
2134         lwkt_gettoken(&hmp->fs_token);
2135         hammer_start_transaction(&trans, hmp);
2136         ++hammer_stats_file_iopsw;
2137
2138         ip->ino_data.atime = trans.time;
2139         hammer_modify_inode(&trans, ip, HAMMER_INODE_ATIME);
2140         hammer_done_transaction(&trans);
2141         hammer_knote(ap->a_vp, NOTE_ATTRIB);
2142         lwkt_reltoken(&hmp->fs_token);
2143         return (0);
2144 }
2145
2146 /*
2147  * hammer_vop_setattr { vp, vap, cred }
2148  */
2149 static
2150 int
2151 hammer_vop_setattr(struct vop_setattr_args *ap)
2152 {
2153         struct hammer_transaction trans;
2154         struct hammer_inode *ip;
2155         struct vattr *vap;
2156         hammer_mount_t hmp;
2157         int modflags;
2158         int error;
2159         int truncating;
2160         int blksize;
2161         int kflags;
2162 #if 0
2163         int64_t aligned_size;
2164 #endif
2165         u_int32_t flags;
2166
2167         vap = ap->a_vap;
2168         ip = ap->a_vp->v_data;
2169         modflags = 0;
2170         kflags = 0;
2171         hmp = ip->hmp;
2172
2173         if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
2174                 return(EROFS);
2175         if (ip->flags & HAMMER_INODE_RO)
2176                 return (EROFS);
2177         if (hammer_nohistory(ip) == 0 &&
2178             (error = hammer_checkspace(hmp, HAMMER_CHKSPC_REMOVE)) != 0) {
2179                 return (error);
2180         }
2181
2182         lwkt_gettoken(&hmp->fs_token);
2183         hammer_start_transaction(&trans, hmp);
2184         ++hammer_stats_file_iopsw;
2185         error = 0;
2186
2187         if (vap->va_flags != VNOVAL) {
2188                 flags = ip->ino_data.uflags;
2189                 error = vop_helper_setattr_flags(&flags, vap->va_flags,
2190                                          hammer_to_unix_xid(&ip->ino_data.uid),
2191                                          ap->a_cred);
2192                 if (error == 0) {
2193                         if (ip->ino_data.uflags != flags) {
2194                                 ip->ino_data.uflags = flags;
2195                                 ip->ino_data.ctime = trans.time;
2196                                 modflags |= HAMMER_INODE_DDIRTY;
2197                                 kflags |= NOTE_ATTRIB;
2198                         }
2199                         if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) {
2200                                 error = 0;
2201                                 goto done;
2202                         }
2203                 }
2204                 goto done;
2205         }
2206         if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) {
2207                 error = EPERM;
2208                 goto done;
2209         }
2210         if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
2211                 mode_t cur_mode = ip->ino_data.mode;
2212                 uid_t cur_uid = hammer_to_unix_xid(&ip->ino_data.uid);
2213                 gid_t cur_gid = hammer_to_unix_xid(&ip->ino_data.gid);
2214                 uuid_t uuid_uid;
2215                 uuid_t uuid_gid;
2216
2217                 error = vop_helper_chown(ap->a_vp, vap->va_uid, vap->va_gid,
2218                                          ap->a_cred,
2219                                          &cur_uid, &cur_gid, &cur_mode);
2220                 if (error == 0) {
2221                         hammer_guid_to_uuid(&uuid_uid, cur_uid);
2222                         hammer_guid_to_uuid(&uuid_gid, cur_gid);
2223                         if (bcmp(&uuid_uid, &ip->ino_data.uid,
2224                                  sizeof(uuid_uid)) ||
2225                             bcmp(&uuid_gid, &ip->ino_data.gid,
2226                                  sizeof(uuid_gid)) ||
2227                             ip->ino_data.mode != cur_mode
2228                         ) {
2229                                 ip->ino_data.uid = uuid_uid;
2230                                 ip->ino_data.gid = uuid_gid;
2231                                 ip->ino_data.mode = cur_mode;
2232                                 ip->ino_data.ctime = trans.time;
2233                                 modflags |= HAMMER_INODE_DDIRTY;
2234                         }
2235                         kflags |= NOTE_ATTRIB;
2236                 }
2237         }
2238         while (vap->va_size != VNOVAL && ip->ino_data.size != vap->va_size) {
2239                 switch(ap->a_vp->v_type) {
2240                 case VREG:
2241                         if (vap->va_size == ip->ino_data.size)
2242                                 break;
2243
2244                         /*
2245                          * Log the operation if in fast-fsync mode or if
2246                          * there are unterminated redo write records present.
2247                          *
2248                          * The second check is needed so the recovery code
2249                          * properly truncates write redos even if nominal
2250                          * REDO operations is turned off due to excessive
2251                          * writes, because the related records might be
2252                          * destroyed and never lay down a TERM_WRITE.
2253                          */
2254                         if ((ip->flags & HAMMER_INODE_REDO) ||
2255                             (ip->flags & HAMMER_INODE_RDIRTY)) {
2256                                 error = hammer_generate_redo(&trans, ip,
2257                                                              vap->va_size,
2258                                                              HAMMER_REDO_TRUNC,
2259                                                              NULL, 0);
2260                         }
2261                         blksize = hammer_blocksize(vap->va_size);
2262
2263                         /*
2264                          * XXX break atomicy, we can deadlock the backend
2265                          * if we do not release the lock.  Probably not a
2266                          * big deal here.
2267                          */
2268                         if (vap->va_size < ip->ino_data.size) {
2269                                 nvtruncbuf(ap->a_vp, vap->va_size,
2270                                            blksize,
2271                                            hammer_blockoff(vap->va_size));
2272                                 truncating = 1;
2273                                 kflags |= NOTE_WRITE;
2274                         } else {
2275                                 nvextendbuf(ap->a_vp,
2276                                             ip->ino_data.size,
2277                                             vap->va_size,
2278                                             hammer_blocksize(ip->ino_data.size),
2279                                             hammer_blocksize(vap->va_size),
2280                                             hammer_blockoff(ip->ino_data.size),
2281                                             hammer_blockoff(vap->va_size),
2282                                             0);
2283                                 truncating = 0;
2284                                 kflags |= NOTE_WRITE | NOTE_EXTEND;
2285                         }
2286                         ip->ino_data.size = vap->va_size;
2287                         ip->ino_data.mtime = trans.time;
2288                         /* XXX safe to use SDIRTY instead of DDIRTY here? */
2289                         modflags |= HAMMER_INODE_MTIME | HAMMER_INODE_DDIRTY;
2290
2291                         /*
2292                          * On-media truncation is cached in the inode until
2293                          * the inode is synchronized.  We must immediately
2294                          * handle any frontend records.
2295                          */
2296                         if (truncating) {
2297                                 hammer_ip_frontend_trunc(ip, vap->va_size);
2298 #ifdef DEBUG_TRUNCATE
2299                                 if (HammerTruncIp == NULL)
2300                                         HammerTruncIp = ip;
2301 #endif
2302                                 if ((ip->flags & HAMMER_INODE_TRUNCATED) == 0) {
2303                                         ip->flags |= HAMMER_INODE_TRUNCATED;
2304                                         ip->trunc_off = vap->va_size;
2305 #ifdef DEBUG_TRUNCATE
2306                                         if (ip == HammerTruncIp)
2307                                         kprintf("truncate1 %016llx\n",
2308                                                 (long long)ip->trunc_off);
2309 #endif
2310                                 } else if (ip->trunc_off > vap->va_size) {
2311                                         ip->trunc_off = vap->va_size;
2312 #ifdef DEBUG_TRUNCATE
2313                                         if (ip == HammerTruncIp)
2314                                         kprintf("truncate2 %016llx\n",
2315                                                 (long long)ip->trunc_off);
2316 #endif
2317                                 } else {
2318 #ifdef DEBUG_TRUNCATE
2319                                         if (ip == HammerTruncIp)
2320                                         kprintf("truncate3 %016llx (ignored)\n",
2321                                                 (long long)vap->va_size);
2322 #endif
2323                                 }
2324                         }
2325
2326 #if 0
2327                         /*
2328                          * When truncating, nvtruncbuf() may have cleaned out
2329                          * a portion of the last block on-disk in the buffer
2330                          * cache.  We must clean out any frontend records
2331                          * for blocks beyond the new last block.
2332                          */
2333                         aligned_size = (vap->va_size + (blksize - 1)) &
2334                                        ~(int64_t)(blksize - 1);
2335                         if (truncating && vap->va_size < aligned_size) {
2336                                 aligned_size -= blksize;
2337                                 hammer_ip_frontend_trunc(ip, aligned_size);
2338                         }
2339 #endif
2340                         break;
2341                 case VDATABASE:
2342                         if ((ip->flags & HAMMER_INODE_TRUNCATED) == 0) {
2343                                 ip->flags |= HAMMER_INODE_TRUNCATED;
2344                                 ip->trunc_off = vap->va_size;
2345                         } else if (ip->trunc_off > vap->va_size) {
2346                                 ip->trunc_off = vap->va_size;
2347                         }
2348                         hammer_ip_frontend_trunc(ip, vap->va_size);
2349                         ip->ino_data.size = vap->va_size;
2350                         ip->ino_data.mtime = trans.time;
2351                         modflags |= HAMMER_INODE_MTIME | HAMMER_INODE_DDIRTY;
2352                         kflags |= NOTE_ATTRIB;
2353                         break;
2354                 default:
2355                         error = EINVAL;
2356                         goto done;
2357                 }
2358                 break;
2359         }
2360         if (vap->va_atime.tv_sec != VNOVAL) {
2361                 ip->ino_data.atime = hammer_timespec_to_time(&vap->va_atime);
2362                 modflags |= HAMMER_INODE_ATIME;
2363                 kflags |= NOTE_ATTRIB;
2364         }
2365         if (vap->va_mtime.tv_sec != VNOVAL) {
2366                 ip->ino_data.mtime = hammer_timespec_to_time(&vap->va_mtime);
2367                 modflags |= HAMMER_INODE_MTIME;
2368                 kflags |= NOTE_ATTRIB;
2369         }
2370         if (vap->va_mode != (mode_t)VNOVAL) {
2371                 mode_t   cur_mode = ip->ino_data.mode;
2372                 uid_t cur_uid = hammer_to_unix_xid(&ip->ino_data.uid);
2373                 gid_t cur_gid = hammer_to_unix_xid(&ip->ino_data.gid);
2374
2375                 error = vop_helper_chmod(ap->a_vp, vap->va_mode, ap->a_cred,
2376                                          cur_uid, cur_gid, &cur_mode);
2377                 if (error == 0 && ip->ino_data.mode != cur_mode) {
2378                         ip->ino_data.mode = cur_mode;
2379                         ip->ino_data.ctime = trans.time;
2380                         modflags |= HAMMER_INODE_DDIRTY;
2381                         kflags |= NOTE_ATTRIB;
2382                 }
2383         }
2384 done:
2385         if (error == 0)
2386                 hammer_modify_inode(&trans, ip, modflags);
2387         hammer_done_transaction(&trans);
2388         hammer_knote(ap->a_vp, kflags);
2389         lwkt_reltoken(&hmp->fs_token);
2390         return (error);
2391 }
2392
2393 /*
2394  * hammer_vop_nsymlink { nch, dvp, vpp, cred, vap, target }
2395  */
2396 static
2397 int
2398 hammer_vop_nsymlink(struct vop_nsymlink_args *ap)
2399 {
2400         struct hammer_transaction trans;
2401         struct hammer_inode *dip;
2402         struct hammer_inode *nip;
2403         hammer_record_t record;
2404         struct nchandle *nch;
2405         hammer_mount_t hmp;
2406         int error;
2407         int bytes;
2408
2409         ap->a_vap->va_type = VLNK;
2410
2411         nch = ap->a_nch;
2412         dip = VTOI(ap->a_dvp);
2413         hmp = dip->hmp;
2414
2415         if (dip->flags & HAMMER_INODE_RO)
2416                 return (EROFS);
2417         if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0)
2418                 return (error);
2419
2420         /*
2421          * Create a transaction to cover the operations we perform.
2422          */
2423         lwkt_gettoken(&hmp->fs_token);
2424         hammer_start_transaction(&trans, hmp);
2425         ++hammer_stats_file_iopsw;
2426
2427         /*
2428          * Create a new filesystem object of the requested type.  The
2429          * returned inode will be referenced but not locked.
2430          */
2431
2432         error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
2433                                     dip, nch->ncp->nc_name, nch->ncp->nc_nlen,
2434                                     NULL, &nip);
2435         if (error) {
2436                 hammer_done_transaction(&trans);
2437                 *ap->a_vpp = NULL;
2438                 lwkt_reltoken(&hmp->fs_token);
2439                 return (error);
2440         }
2441
2442         /*
2443          * Add a record representing the symlink.  symlink stores the link
2444          * as pure data, not a string, and is no \0 terminated.
2445          */
2446         if (error == 0) {
2447                 bytes = strlen(ap->a_target);
2448
2449                 if (bytes <= HAMMER_INODE_BASESYMLEN) {
2450                         bcopy(ap->a_target, nip->ino_data.ext.symlink, bytes);
2451                 } else {
2452                         record = hammer_alloc_mem_record(nip, bytes);
2453                         record->type = HAMMER_MEM_RECORD_GENERAL;
2454
2455                         record->leaf.base.localization = nip->obj_localization +
2456                                                          HAMMER_LOCALIZE_MISC;
2457                         record->leaf.base.key = HAMMER_FIXKEY_SYMLINK;
2458                         record->leaf.base.rec_type = HAMMER_RECTYPE_FIX;
2459                         record->leaf.data_len = bytes;
2460                         KKASSERT(HAMMER_SYMLINK_NAME_OFF == 0);
2461                         bcopy(ap->a_target, record->data->symlink.name, bytes);
2462                         error = hammer_ip_add_record(&trans, record);
2463                 }
2464
2465                 /*
2466                  * Set the file size to the length of the link.
2467                  */
2468                 if (error == 0) {
2469                         nip->ino_data.size = bytes;
2470                         hammer_modify_inode(&trans, nip, HAMMER_INODE_DDIRTY);
2471                 }
2472         }
2473         if (error == 0)
2474                 error = hammer_ip_add_directory(&trans, dip, nch->ncp->nc_name,
2475                                                 nch->ncp->nc_nlen, nip);
2476
2477         /*
2478          * Finish up.
2479          */
2480         if (error) {
2481                 hammer_rel_inode(nip, 0);
2482                 *ap->a_vpp = NULL;
2483         } else {
2484                 error = hammer_get_vnode(nip, ap->a_vpp);
2485                 hammer_rel_inode(nip, 0);
2486                 if (error == 0) {
2487                         cache_setunresolved(ap->a_nch);
2488                         cache_setvp(ap->a_nch, *ap->a_vpp);
2489                         hammer_knote(ap->a_dvp, NOTE_WRITE);
2490                 }
2491         }
2492         hammer_done_transaction(&trans);
2493         lwkt_reltoken(&hmp->fs_token);
2494         return (error);
2495 }
2496
2497 /*
2498  * hammer_vop_nwhiteout { nch, dvp, cred, flags }
2499  */
2500 static
2501 int
2502 hammer_vop_nwhiteout(struct vop_nwhiteout_args *ap)
2503 {
2504         struct hammer_transaction trans;
2505         struct hammer_inode *dip;
2506         hammer_mount_t hmp;
2507         int error;
2508
2509         dip = VTOI(ap->a_dvp);
2510         hmp = dip->hmp;
2511
2512         if (hammer_nohistory(dip) == 0 &&
2513             (error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0) {
2514                 return (error);
2515         }
2516
2517         lwkt_gettoken(&hmp->fs_token);
2518         hammer_start_transaction(&trans, hmp);
2519         ++hammer_stats_file_iopsw;
2520         error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp,
2521                                 ap->a_cred, ap->a_flags, -1);
2522         hammer_done_transaction(&trans);
2523         lwkt_reltoken(&hmp->fs_token);
2524
2525         return (error);
2526 }
2527
2528 /*
2529  * hammer_vop_ioctl { vp, command, data, fflag, cred }
2530  */
2531 static
2532 int
2533 hammer_vop_ioctl(struct vop_ioctl_args *ap)
2534 {
2535         struct hammer_inode *ip = ap->a_vp->v_data;
2536         hammer_mount_t hmp = ip->hmp;
2537         int error;
2538
2539         ++hammer_stats_file_iopsr;
2540         lwkt_gettoken(&hmp->fs_token);
2541         error = hammer_ioctl(ip, ap->a_command, ap->a_data,
2542                              ap->a_fflag, ap->a_cred);
2543         lwkt_reltoken(&hmp->fs_token);
2544         return (error);
2545 }
2546
2547 static
2548 int
2549 hammer_vop_mountctl(struct vop_mountctl_args *ap)
2550 {
2551         static const struct mountctl_opt extraopt[] = {
2552                 { HMNT_NOHISTORY,       "nohistory" },
2553                 { HMNT_MASTERID,        "master" },
2554                 { 0, NULL}
2555
2556         };
2557         struct hammer_mount *hmp;
2558         struct mount *mp;
2559         int usedbytes;
2560         int error;
2561
2562         error = 0;
2563         usedbytes = 0;
2564         mp = ap->a_head.a_ops->head.vv_mount;
2565         KKASSERT(mp->mnt_data != NULL);
2566         hmp = (struct hammer_mount *)mp->mnt_data;
2567
2568         lwkt_gettoken(&hmp->fs_token);
2569
2570         switch(ap->a_op) {
2571         case MOUNTCTL_SET_EXPORT:
2572                 if (ap->a_ctllen != sizeof(struct export_args))
2573                         error = EINVAL;
2574                 else
2575                         error = hammer_vfs_export(mp, ap->a_op,
2576                                       (const struct export_args *)ap->a_ctl);
2577                 break;
2578         case MOUNTCTL_MOUNTFLAGS:
2579         {
2580                 /*
2581                  * Call standard mountctl VOP function
2582                  * so we get user mount flags.
2583                  */
2584                 error = vop_stdmountctl(ap);
2585                 if (error)
2586                         break;
2587
2588                 usedbytes = *ap->a_res;
2589
2590                 if (usedbytes > 0 && usedbytes < ap->a_buflen) {
2591                         usedbytes += vfs_flagstostr(hmp->hflags, extraopt,
2592                                                     ap->a_buf,
2593                                                     ap->a_buflen - usedbytes,
2594                                                     &error);
2595                 }
2596
2597                 *ap->a_res += usedbytes;
2598                 break;
2599         }
2600         default:
2601                 error = vop_stdmountctl(ap);
2602                 break;
2603         }
2604         lwkt_reltoken(&hmp->fs_token);
2605         return(error);
2606 }
2607
2608 /*
2609  * hammer_vop_strategy { vp, bio }
2610  *
2611  * Strategy call, used for regular file read & write only.  Note that the
2612  * bp may represent a cluster.
2613  *
2614  * To simplify operation and allow better optimizations in the future,
2615  * this code does not make any assumptions with regards to buffer alignment
2616  * or size.
2617  */
2618 static
2619 int
2620 hammer_vop_strategy(struct vop_strategy_args *ap)
2621 {
2622         struct buf *bp;
2623         int error;
2624
2625         bp = ap->a_bio->bio_buf;
2626
2627         switch(bp->b_cmd) {
2628         case BUF_CMD_READ:
2629                 error = hammer_vop_strategy_read(ap);
2630                 break;
2631         case BUF_CMD_WRITE:
2632                 error = hammer_vop_strategy_write(ap);
2633                 break;
2634         default:
2635                 bp->b_error = error = EINVAL;
2636                 bp->b_flags |= B_ERROR;
2637                 biodone(ap->a_bio);
2638                 break;
2639         }
2640
2641         /* hammer_dump_dedup_cache(((hammer_inode_t)ap->a_vp->v_data)->hmp); */
2642
2643         return (error);
2644 }
2645
2646 /*
2647  * Read from a regular file.  Iterate the related records and fill in the
2648  * BIO/BUF.  Gaps are zero-filled.
2649  *
2650  * The support code in hammer_object.c should be used to deal with mixed
2651  * in-memory and on-disk records.
2652  *
2653  * NOTE: Can be called from the cluster code with an oversized buf.
2654  *
2655  * XXX atime update
2656  */
2657 static
2658 int
2659 hammer_vop_strategy_read(struct vop_strategy_args *ap)
2660 {
2661         struct hammer_transaction trans;
2662         struct hammer_inode *ip;
2663         struct hammer_inode *dip;
2664         hammer_mount_t hmp;
2665         struct hammer_cursor cursor;
2666         hammer_base_elm_t base;
2667         hammer_off_t disk_offset;
2668         struct bio *bio;
2669         struct bio *nbio;
2670         struct buf *bp;
2671         int64_t rec_offset;
2672         int64_t ran_end;
2673         int64_t tmp64;
2674         int error;
2675         int boff;
2676         int roff;
2677         int n;
2678         int isdedupable;
2679
2680         bio = ap->a_bio;
2681         bp = bio->bio_buf;
2682         ip = ap->a_vp->v_data;
2683         hmp = ip->hmp;
2684
2685         /*
2686          * The zone-2 disk offset may have been set by the cluster code via
2687          * a BMAP operation, or else should be NOOFFSET.
2688          *
2689          * Checking the high bits for a match against zone-2 should suffice.
2690          *
2691          * In cases where a lot of data duplication is present it may be
2692          * more beneficial to drop through and doubule-buffer through the
2693          * device.
2694          */
2695         nbio = push_bio(bio);
2696         if (hammer_double_buffer == 0 &&
2697             (nbio->bio_offset & HAMMER_OFF_ZONE_MASK) ==
2698             HAMMER_ZONE_LARGE_DATA) {
2699                 lwkt_gettoken(&hmp->fs_token);
2700                 error = hammer_io_direct_read(hmp, nbio, NULL);
2701                 lwkt_reltoken(&hmp->fs_token);
2702                 return (error);
2703         }
2704
2705         /*
2706          * Well, that sucked.  Do it the hard way.  If all the stars are
2707          * aligned we may still be able to issue a direct-read.
2708          */
2709         lwkt_gettoken(&hmp->fs_token);
2710         hammer_simple_transaction(&trans, hmp);
2711         hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
2712
2713         /*
2714          * Key range (begin and end inclusive) to scan.  Note that the key's
2715          * stored in the actual records represent BASE+LEN, not BASE.  The
2716          * first record containing bio_offset will have a key > bio_offset.
2717          */
2718         cursor.key_beg.localization = ip->obj_localization +
2719                                       HAMMER_LOCALIZE_MISC;
2720         cursor.key_beg.obj_id = ip->obj_id;
2721         cursor.key_beg.create_tid = 0;
2722         cursor.key_beg.delete_tid = 0;
2723         cursor.key_beg.obj_type = 0;
2724         cursor.key_beg.key = bio->bio_offset + 1;
2725         cursor.asof = ip->obj_asof;
2726         cursor.flags |= HAMMER_CURSOR_ASOF;
2727
2728         cursor.key_end = cursor.key_beg;
2729         KKASSERT(ip->ino_data.obj_type == HAMMER_OBJTYPE_REGFILE);
2730 #if 0
2731         if (ip->ino_data.obj_type == HAMMER_OBJTYPE_DBFILE) {
2732                 cursor.key_beg.rec_type = HAMMER_RECTYPE_DB;
2733                 cursor.key_end.rec_type = HAMMER_RECTYPE_DB;
2734                 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
2735         } else
2736 #endif
2737         {
2738                 ran_end = bio->bio_offset + bp->b_bufsize;
2739                 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
2740                 cursor.key_end.rec_type = HAMMER_RECTYPE_DATA;
2741                 tmp64 = ran_end + MAXPHYS + 1;  /* work-around GCC-4 bug */
2742                 if (tmp64 < ran_end)
2743                         cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
2744                 else
2745                         cursor.key_end.key = ran_end + MAXPHYS + 1;
2746         }
2747         cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
2748
2749         error = hammer_ip_first(&cursor);
2750         boff = 0;
2751
2752         while (error == 0) {
2753                 /*
2754                  * Get the base file offset of the record.  The key for
2755                  * data records is (base + bytes) rather then (base).
2756                  */
2757                 base = &cursor.leaf->base;
2758                 rec_offset = base->key - cursor.leaf->data_len;
2759
2760                 /*
2761                  * Calculate the gap, if any, and zero-fill it.
2762                  *
2763                  * n is the offset of the start of the record verses our
2764                  * current seek offset in the bio.
2765                  */
2766                 n = (int)(rec_offset - (bio->bio_offset + boff));
2767                 if (n > 0) {
2768                         if (n > bp->b_bufsize - boff)
2769                                 n = bp->b_bufsize - boff;
2770                         bzero((char *)bp->b_data + boff, n);
2771                         boff += n;
2772                         n = 0;
2773                 }
2774
2775                 /*
2776                  * Calculate the data offset in the record and the number
2777                  * of bytes we can copy.
2778                  *
2779                  * There are two degenerate cases.  First, boff may already
2780                  * be at bp->b_bufsize.  Secondly, the data offset within
2781                  * the record may exceed the record's size.
2782                  */
2783                 roff = -n;
2784                 rec_offset += roff;
2785                 n = cursor.leaf->data_len - roff;
2786                 if (n <= 0) {
2787                         kprintf("strategy_read: bad n=%d roff=%d\n", n, roff);
2788                         n = 0;
2789                 } else if (n > bp->b_bufsize - boff) {
2790                         n = bp->b_bufsize - boff;
2791                 }
2792
2793                 /*
2794                  * Deal with cached truncations.  This cool bit of code
2795                  * allows truncate()/ftruncate() to avoid having to sync
2796                  * the file.
2797                  *
2798                  * If the frontend is truncated then all backend records are
2799                  * subject to the frontend's truncation.
2800                  *
2801                  * If the backend is truncated then backend records on-disk
2802                  * (but not in-memory) are subject to the backend's
2803                  * truncation.  In-memory records owned by the backend
2804                  * represent data written after the truncation point on the
2805                  * backend and must not be truncated.
2806                  *
2807                  * Truncate operations deal with frontend buffer cache
2808                  * buffers and frontend-owned in-memory records synchronously.
2809                  */
2810                 if (ip->flags & HAMMER_INODE_TRUNCATED) {
2811                         if (hammer_cursor_ondisk(&cursor)/* ||
2812                             cursor.iprec->flush_state == HAMMER_FST_FLUSH*/) {
2813                                 if (ip->trunc_off <= rec_offset)
2814                                         n = 0;
2815                                 else if (ip->trunc_off < rec_offset + n)
2816                                         n = (int)(ip->trunc_off - rec_offset);
2817                         }
2818                 }
2819                 if (ip->sync_flags & HAMMER_INODE_TRUNCATED) {
2820                         if (hammer_cursor_ondisk(&cursor)) {
2821                                 if (ip->sync_trunc_off <= rec_offset)
2822                                         n = 0;
2823                                 else if (ip->sync_trunc_off < rec_offset + n)
2824                                         n = (int)(ip->sync_trunc_off - rec_offset);
2825                         }
2826                 }
2827
2828                 /*
2829                  * Try to issue a direct read into our bio if possible,
2830                  * otherwise resolve the element data into a hammer_buffer
2831                  * and copy.
2832                  *
2833                  * The buffer on-disk should be zerod past any real
2834                  * truncation point, but may not be for any synthesized
2835                  * truncation point from above.
2836                  */
2837                 disk_offset = cursor.leaf->data_offset + roff;
2838                 isdedupable = (boff == 0 && n == bp->b_bufsize &&
2839                                hammer_cursor_ondisk(&cursor) &&
2840                                ((int)disk_offset & HAMMER_BUFMASK) == 0);
2841
2842                 if (isdedupable && hammer_double_buffer == 0) {
2843                         KKASSERT((disk_offset & HAMMER_OFF_ZONE_MASK) ==
2844                                  HAMMER_ZONE_LARGE_DATA);
2845                         nbio->bio_offset = disk_offset;
2846                         error = hammer_io_direct_read(hmp, nbio, cursor.leaf);
2847                         if (hammer_live_dedup && error == 0)
2848                                 hammer_dedup_cache_add(ip, cursor.leaf);
2849                         goto done;
2850                 } else if (n) {
2851                         error = hammer_ip_resolve_data(&cursor);
2852                         if (error == 0) {
2853                                 if (hammer_live_dedup && isdedupable)
2854                                         hammer_dedup_cache_add(ip, cursor.leaf);
2855                                 bcopy((char *)cursor.data + roff,
2856                                       (char *)bp->b_data + boff, n);
2857                         }
2858                 }
2859                 if (error)
2860                         break;
2861
2862                 /*
2863                  * We have to be sure that the only elements added to the
2864                  * dedup cache are those which are already on-media.
2865                  */
2866                 if (hammer_live_dedup && hammer_cursor_ondisk(&cursor))
2867                         hammer_dedup_cache_add(ip, cursor.leaf);
2868
2869                 /*
2870                  * Iterate until we have filled the request.
2871                  */
2872                 boff += n;
2873                 if (boff == bp->b_bufsize)
2874                         break;
2875                 error = hammer_ip_next(&cursor);
2876         }
2877
2878         /*
2879          * There may have been a gap after the last record
2880          */
2881         if (error == ENOENT)
2882                 error = 0;
2883         if (error == 0 && boff != bp->b_bufsize) {
2884                 KKASSERT(boff < bp->b_bufsize);
2885                 bzero((char *)bp->b_data + boff, bp->b_bufsize - boff);
2886                 /* boff = bp->b_bufsize; */
2887         }
2888         bp->b_resid = 0;
2889         bp->b_error = error;
2890         if (error)
2891                 bp->b_flags |= B_ERROR;
2892         biodone(ap->a_bio);
2893
2894 done:
2895         /*
2896          * Cache the b-tree node for the last data read in cache[1].
2897          *
2898          * If we hit the file EOF then also cache the node in the
2899          * governing director's cache[3], it will be used to initialize
2900          * the inode's cache[1] for any inodes looked up via the directory.
2901          *
2902          * This doesn't reduce disk accesses since the B-Tree chain is
2903          * likely cached, but it does reduce cpu overhead when looking
2904          * up file offsets for cpdup/tar/cpio style iterations.
2905          */
2906         if (cursor.node)
2907                 hammer_cache_node(&ip->cache[1], cursor.node);
2908         if (ran_end >= ip->ino_data.size) {
2909                 dip = hammer_find_inode(&trans, ip->ino_data.parent_obj_id,
2910                                         ip->obj_asof, ip->obj_localization);
2911                 if (dip) {
2912                         hammer_cache_node(&dip->cache[3], cursor.node);
2913                         hammer_rel_inode(dip, 0);
2914                 }
2915         }
2916         hammer_done_cursor(&cursor);
2917         hammer_done_transaction(&trans);
2918         lwkt_reltoken(&hmp->fs_token);
2919         return(error);
2920 }
2921
2922 /*
2923  * BMAP operation - used to support cluster_read() only.
2924  *
2925  * (struct vnode *vp, off_t loffset, off_t *doffsetp, int *runp, int *runb)
2926  *
2927  * This routine may return EOPNOTSUPP if the opration is not supported for
2928  * the specified offset.  The contents of the pointer arguments do not
2929  * need to be initialized in that case. 
2930  *
2931  * If a disk address is available and properly aligned return 0 with 
2932  * *doffsetp set to the zone-2 address, and *runp / *runb set appropriately
2933  * to the run-length relative to that offset.  Callers may assume that
2934  * *doffsetp is valid if 0 is returned, even if *runp is not sufficiently
2935  * large, so return EOPNOTSUPP if it is not sufficiently large.
2936  */
2937 static
2938 int
2939 hammer_vop_bmap(struct vop_bmap_args *ap)
2940 {
2941         struct hammer_transaction trans;
2942         struct hammer_inode *ip;
2943         hammer_mount_t hmp;
2944         struct hammer_cursor cursor;
2945         hammer_base_elm_t base;
2946         int64_t rec_offset;
2947         int64_t ran_end;
2948         int64_t tmp64;
2949         int64_t base_offset;
2950         int64_t base_disk_offset;
2951         int64_t last_offset;
2952         hammer_off_t last_disk_offset;
2953         hammer_off_t disk_offset;
2954         int     rec_len;
2955         int     error;
2956         int     blksize;
2957
2958         ++hammer_stats_file_iopsr;
2959         ip = ap->a_vp->v_data;
2960         hmp = ip->hmp;
2961
2962         /*
2963          * We can only BMAP regular files.  We can't BMAP database files,
2964          * directories, etc.
2965          */
2966         if (ip->ino_data.obj_type != HAMMER_OBJTYPE_REGFILE)
2967                 return(EOPNOTSUPP);
2968
2969         /*
2970          * bmap is typically called with runp/runb both NULL when used
2971          * for writing.  We do not support BMAP for writing atm.
2972          */
2973         if (ap->a_cmd != BUF_CMD_READ)
2974                 return(EOPNOTSUPP);
2975
2976         /*
2977          * Scan the B-Tree to acquire blockmap addresses, then translate
2978          * to raw addresses.
2979          */
2980         lwkt_gettoken(&hmp->fs_token);
2981         hammer_simple_transaction(&trans, hmp);
2982 #if 0
2983         kprintf("bmap_beg %016llx ip->cache %p\n",
2984                 (long long)ap->a_loffset, ip->cache[1]);
2985 #endif
2986         hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
2987
2988         /*
2989          * Key range (begin and end inclusive) to scan.  Note that the key's
2990          * stored in the actual records represent BASE+LEN, not BASE.  The
2991          * first record containing bio_offset will have a key > bio_offset.
2992          */
2993         cursor.key_beg.localization = ip->obj_localization +
2994                                       HAMMER_LOCALIZE_MISC;
2995         cursor.key_beg.obj_id = ip->obj_id;
2996         cursor.key_beg.create_tid = 0;
2997         cursor.key_beg.delete_tid = 0;
2998         cursor.key_beg.obj_type = 0;
2999         if (ap->a_runb)
3000                 cursor.key_beg.key = ap->a_loffset - MAXPHYS + 1;
3001         else
3002                 cursor.key_beg.key = ap->a_loffset + 1;
3003         if (cursor.key_beg.key < 0)
3004                 cursor.key_beg.key = 0;
3005         cursor.asof = ip->obj_asof;
3006         cursor.flags |= HAMMER_CURSOR_ASOF;
3007
3008         cursor.key_end = cursor.key_beg;
3009         KKASSERT(ip->ino_data.obj_type == HAMMER_OBJTYPE_REGFILE);
3010
3011         ran_end = ap->a_loffset + MAXPHYS;
3012         cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
3013         cursor.key_end.rec_type = HAMMER_RECTYPE_DATA;
3014         tmp64 = ran_end + MAXPHYS + 1;  /* work-around GCC-4 bug */
3015         if (tmp64 < ran_end)
3016                 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
3017         else
3018                 cursor.key_end.key = ran_end + MAXPHYS + 1;
3019
3020         cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
3021
3022         error = hammer_ip_first(&cursor);
3023         base_offset = last_offset = 0;
3024         base_disk_offset = last_disk_offset = 0;
3025
3026         while (error == 0) {
3027                 /*
3028                  * Get the base file offset of the record.  The key for
3029                  * data records is (base + bytes) rather then (base).
3030                  *
3031                  * NOTE: rec_offset + rec_len may exceed the end-of-file.
3032                  * The extra bytes should be zero on-disk and the BMAP op
3033                  * should still be ok.
3034                  */
3035                 base = &cursor.leaf->base;
3036                 rec_offset = base->key - cursor.leaf->data_len;
3037                 rec_len    = cursor.leaf->data_len;
3038
3039                 /*
3040                  * Incorporate any cached truncation.
3041                  *
3042                  * NOTE: Modifications to rec_len based on synthesized
3043                  * truncation points remove the guarantee that any extended
3044                  * data on disk is zero (since the truncations may not have
3045                  * taken place on-media yet).
3046                  */
3047                 if (ip->flags & HAMMER_INODE_TRUNCATED) {
3048                         if (hammer_cursor_ondisk(&cursor) ||
3049                             cursor.iprec->flush_state == HAMMER_FST_FLUSH) {
3050                                 if (ip->trunc_off <= rec_offset)
3051                                         rec_len = 0;
3052                                 else if (ip->trunc_off < rec_offset + rec_len)
3053                                         rec_len = (int)(ip->trunc_off - rec_offset);
3054                         }
3055                 }
3056                 if (ip->sync_flags & HAMMER_INODE_TRUNCATED) {
3057                         if (hammer_cursor_ondisk(&cursor)) {
3058                                 if (ip->sync_trunc_off <= rec_offset)
3059                                         rec_len = 0;
3060                                 else if (ip->sync_trunc_off < rec_offset + rec_len)
3061                                         rec_len = (int)(ip->sync_trunc_off - rec_offset);
3062                         }
3063                 }
3064
3065                 /*
3066                  * Accumulate information.  If we have hit a discontiguous
3067                  * block reset base_offset unless we are already beyond the
3068                  * requested offset.  If we are, that's it, we stop.
3069                  */
3070                 if (error)
3071                         break;
3072                 if (hammer_cursor_ondisk(&cursor)) {
3073                         disk_offset = cursor.leaf->data_offset;
3074                         if (rec_offset != last_offset ||
3075                             disk_offset != last_disk_offset) {
3076                                 if (rec_offset > ap->a_loffset)
3077                                         break;
3078                                 base_offset = rec_offset;
3079                                 base_disk_offset = disk_offset;
3080                         }
3081                         last_offset = rec_offset + rec_len;
3082                         last_disk_offset = disk_offset + rec_len;
3083
3084                         if (hammer_live_dedup)
3085                                 hammer_dedup_cache_add(ip, cursor.leaf);
3086                 }
3087                 
3088                 error = hammer_ip_next(&cursor);
3089         }
3090
3091 #if 0
3092         kprintf("BMAP %016llx:  %016llx - %016llx\n",
3093                 (long long)ap->a_loffset,
3094                 (long long)base_offset,
3095                 (long long)last_offset);
3096         kprintf("BMAP %16s:  %016llx - %016llx\n", "",
3097                 (long long)base_disk_offset,
3098                 (long long)last_disk_offset);
3099 #endif
3100
3101         if (cursor.node) {
3102                 hammer_cache_node(&ip->cache[1], cursor.node);
3103 #if 0
3104                 kprintf("bmap_end2 %016llx ip->cache %p\n",
3105                         (long long)ap->a_loffset, ip->cache[1]);
3106 #endif
3107         }
3108         hammer_done_cursor(&cursor);
3109         hammer_done_transaction(&trans);
3110         lwkt_reltoken(&hmp->fs_token);
3111
3112         /*
3113          * If we couldn't find any records or the records we did find were
3114          * all behind the requested offset, return failure.  A forward
3115          * truncation can leave a hole w/ no on-disk records.
3116          */
3117         if (last_offset == 0 || last_offset < ap->a_loffset)
3118                 return (EOPNOTSUPP);
3119
3120         /*
3121          * Figure out the block size at the requested offset and adjust
3122          * our limits so the cluster_read() does not create inappropriately
3123          * sized buffer cache buffers.
3124          */
3125         blksize = hammer_blocksize(ap->a_loffset);
3126         if (hammer_blocksize(base_offset) != blksize) {
3127                 base_offset = hammer_blockdemarc(base_offset, ap->a_loffset);
3128         }
3129         if (last_offset != ap->a_loffset &&
3130             hammer_blocksize(last_offset - 1) != blksize) {
3131                 last_offset = hammer_blockdemarc(ap->a_loffset,
3132                                                  last_offset - 1);
3133         }
3134
3135         /*
3136          * Returning EOPNOTSUPP simply prevents the direct-IO optimization
3137          * from occuring.
3138          */
3139         disk_offset = base_disk_offset + (ap->a_loffset - base_offset);
3140
3141         if ((disk_offset & HAMMER_OFF_ZONE_MASK) != HAMMER_ZONE_LARGE_DATA) {
3142                 /*
3143                  * Only large-data zones can be direct-IOd
3144                  */
3145                 error = EOPNOTSUPP;
3146         } else if ((disk_offset & HAMMER_BUFMASK) ||
3147                    (last_offset - ap->a_loffset) < blksize) {
3148                 /*
3149                  * doffsetp is not aligned or the forward run size does
3150                  * not cover a whole buffer, disallow the direct I/O.
3151                  */
3152                 error = EOPNOTSUPP;
3153         } else {
3154                 /*
3155                  * We're good.
3156                  */
3157                 *ap->a_doffsetp = disk_offset;
3158                 if (ap->a_runb) {
3159                         *ap->a_runb = ap->a_loffset - base_offset;
3160                         KKASSERT(*ap->a_runb >= 0);
3161                 }
3162                 if (ap->a_runp) {
3163                         *ap->a_runp = last_offset - ap->a_loffset;
3164                         KKASSERT(*ap->a_runp >= 0);
3165                 }
3166                 error = 0;
3167         }
3168         return(error);
3169 }
3170
3171 /*
3172  * Write to a regular file.   Because this is a strategy call the OS is
3173  * trying to actually get data onto the media.
3174  */
3175 static
3176 int
3177 hammer_vop_strategy_write(struct vop_strategy_args *ap)
3178 {
3179         hammer_record_t record;
3180         hammer_mount_t hmp;
3181         hammer_inode_t ip;
3182         struct bio *bio;
3183         struct buf *bp;
3184         int blksize;
3185         int bytes;
3186         int error;
3187
3188         bio = ap->a_bio;
3189         bp = bio->bio_buf;
3190         ip = ap->a_vp->v_data;
3191         hmp = ip->hmp;
3192
3193         blksize = hammer_blocksize(bio->bio_offset);
3194         KKASSERT(bp->b_bufsize == blksize);
3195
3196         if (ip->flags & HAMMER_INODE_RO) {
3197                 bp->b_error = EROFS;
3198                 bp->b_flags |= B_ERROR;
3199                 biodone(ap->a_bio);
3200                 return(EROFS);
3201         }
3202
3203         lwkt_gettoken(&hmp->fs_token);
3204
3205         /*
3206          * Interlock with inode destruction (no in-kernel or directory
3207          * topology visibility).  If we queue new IO while trying to
3208          * destroy the inode we can deadlock the vtrunc call in
3209          * hammer_inode_unloadable_check().
3210          *
3211          * Besides, there's no point flushing a bp associated with an
3212          * inode that is being destroyed on-media and has no kernel
3213          * references.
3214          */
3215         if ((ip->flags | ip->sync_flags) &
3216             (HAMMER_INODE_DELETING|HAMMER_INODE_DELETED)) {
3217                 bp->b_resid = 0;
3218                 biodone(ap->a_bio);
3219                 lwkt_reltoken(&hmp->fs_token);
3220                 return(0);
3221         }
3222
3223         /*
3224          * Reserve space and issue a direct-write from the front-end. 
3225          * NOTE: The direct_io code will hammer_bread/bcopy smaller
3226          * allocations.
3227          *
3228          * An in-memory record will be installed to reference the storage
3229          * until the flusher can get to it.
3230          *
3231          * Since we own the high level bio the front-end will not try to
3232          * do a direct-read until the write completes.
3233          *
3234          * NOTE: The only time we do not reserve a full-sized buffers
3235          * worth of data is if the file is small.  We do not try to
3236          * allocate a fragment (from the small-data zone) at the end of
3237          * an otherwise large file as this can lead to wildly separated
3238          * data.
3239          */
3240         KKASSERT((bio->bio_offset & HAMMER_BUFMASK) == 0);
3241         KKASSERT(bio->bio_offset < ip->ino_data.size);
3242         if (bio->bio_offset || ip->ino_data.size > HAMMER_BUFSIZE / 2)
3243                 bytes = bp->b_bufsize;
3244         else
3245                 bytes = ((int)ip->ino_data.size + 15) & ~15;
3246
3247         record = hammer_ip_add_bulk(ip, bio->bio_offset, bp->b_data,
3248                                     bytes, &error);
3249
3250         /*
3251          * B_VFSFLAG1 indicates that a REDO_WRITE entry was generated
3252          * in hammer_vop_write().  We must flag the record so the proper
3253          * REDO_TERM_WRITE entry is generated during the flush.
3254          */
3255         if (record) {
3256                 if (bp->b_flags & B_VFSFLAG1) {
3257                         record->flags |= HAMMER_RECF_REDO;
3258                         bp->b_flags &= ~B_VFSFLAG1;
3259                 }
3260                 if (record->flags & HAMMER_RECF_DEDUPED) {
3261                         bp->b_resid = 0;
3262                         hammer_ip_replace_bulk(hmp, record);
3263                         biodone(ap->a_bio);
3264                 } else {
3265                         hammer_io_direct_write(hmp, bio, record);
3266                 }
3267                 if (ip->rsv_recs > 1 && hmp->rsv_recs > hammer_limit_recs)
3268                         hammer_flush_inode(ip, 0);
3269         } else {
3270                 bp->b_bio2.bio_offset = NOOFFSET;
3271                 bp->b_error = error;
3272                 bp->b_flags |= B_ERROR;
3273                 biodone(ap->a_bio);
3274         }
3275         lwkt_reltoken(&hmp->fs_token);
3276         return(error);
3277 }
3278
3279 /*
3280  * dounlink - disconnect a directory entry
3281  *
3282  * XXX whiteout support not really in yet
3283  */
3284 static int
3285 hammer_dounlink(hammer_transaction_t trans, struct nchandle *nch,
3286                 struct vnode *dvp, struct ucred *cred, 
3287                 int flags, int isdir)
3288 {
3289         struct namecache *ncp;
3290         hammer_inode_t dip;
3291         hammer_inode_t ip;
3292         hammer_mount_t hmp;
3293         struct hammer_cursor cursor;
3294         int64_t namekey;
3295         u_int32_t max_iterations;
3296         int nlen, error;
3297
3298         /*
3299          * Calculate the namekey and setup the key range for the scan.  This
3300          * works kinda like a chained hash table where the lower 32 bits
3301          * of the namekey synthesize the chain.
3302          *
3303          * The key range is inclusive of both key_beg and key_end.
3304          */
3305         dip = VTOI(dvp);
3306         ncp = nch->ncp;
3307         hmp = dip->hmp;
3308
3309         if (dip->flags & HAMMER_INODE_RO)
3310                 return (EROFS);
3311
3312         namekey = hammer_directory_namekey(dip, ncp->nc_name, ncp->nc_nlen,
3313                                            &max_iterations);
3314 retry:
3315         hammer_init_cursor(trans, &cursor, &dip->cache[1], dip);
3316         cursor.key_beg.localization = dip->obj_localization +
3317                                       hammer_dir_localization(dip);
3318         cursor.key_beg.obj_id = dip->obj_id;
3319         cursor.key_beg.key = namekey;
3320         cursor.key_beg.create_tid = 0;
3321         cursor.key_beg.delete_tid = 0;
3322         cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
3323         cursor.key_beg.obj_type = 0;
3324
3325         cursor.key_end = cursor.key_beg;
3326         cursor.key_end.key += max_iterations;
3327         cursor.asof = dip->obj_asof;
3328         cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
3329
3330         /*
3331          * Scan all matching records (the chain), locate the one matching
3332          * the requested path component.  info->last_error contains the
3333          * error code on search termination and could be 0, ENOENT, or
3334          * something else.
3335          *
3336          * The hammer_ip_*() functions merge in-memory records with on-disk
3337          * records for the purposes of the search.
3338          */
3339         error = hammer_ip_first(&cursor);
3340
3341         while (error == 0) {
3342                 error = hammer_ip_resolve_data(&cursor);
3343                 if (error)
3344                         break;
3345                 nlen = cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF;
3346                 KKASSERT(nlen > 0);
3347                 if (ncp->nc_nlen == nlen &&
3348                     bcmp(ncp->nc_name, cursor.data->entry.name, nlen) == 0) {
3349                         break;
3350                 }
3351                 error = hammer_ip_next(&cursor);
3352         }
3353
3354         /*
3355          * If all is ok we have to get the inode so we can adjust nlinks.
3356          * To avoid a deadlock with the flusher we must release the inode
3357          * lock on the directory when acquiring the inode for the entry.
3358          *
3359          * If the target is a directory, it must be empty.
3360          */
3361         if (error == 0) {
3362                 hammer_unlock(&cursor.ip->lock);
3363                 ip = hammer_get_inode(trans, dip, cursor.data->entry.obj_id,
3364                                       hmp->asof,
3365                                       cursor.data->entry.localization,
3366                                       0, &error);
3367                 hammer_lock_sh(&cursor.ip->lock);
3368                 if (error == ENOENT) {
3369                         kprintf("HAMMER: WARNING: Removing "
3370                                 "dirent w/missing inode \"%s\"\n"
3371                                 "\tobj_id = %016llx\n",
3372                                 ncp->nc_name,
3373                                 (long long)cursor.data->entry.obj_id);
3374                         error = 0;
3375                 }
3376
3377                 /*
3378                  * If isdir >= 0 we validate that the entry is or is not a
3379                  * directory.  If isdir < 0 we don't care.
3380                  */
3381                 if (error == 0 && isdir >= 0 && ip) {
3382                         if (isdir &&
3383                             ip->ino_data.obj_type != HAMMER_OBJTYPE_DIRECTORY) {
3384                                 error = ENOTDIR;
3385                         } else if (isdir == 0 &&
3386                             ip->ino_data.obj_type == HAMMER_OBJTYPE_DIRECTORY) {
3387                                 error = EISDIR;
3388                         }
3389                 }
3390
3391                 /*
3392                  * If we are trying to remove a directory the directory must
3393                  * be empty.
3394                  *
3395                  * The check directory code can loop and deadlock/retry.  Our
3396                  * own cursor's node locks must be released to avoid a 3-way
3397                  * deadlock with the flusher if the check directory code
3398                  * blocks.
3399                  *
3400                  * If any changes whatsoever have been made to the cursor
3401                  * set EDEADLK and retry.
3402                  *
3403                  * WARNING: See warnings in hammer_unlock_cursor()
3404                  *          function.
3405                  */
3406                 if (error == 0 && ip && ip->ino_data.obj_type ==
3407                                         HAMMER_OBJTYPE_DIRECTORY) {
3408                         hammer_unlock_cursor(&cursor);
3409                         error = hammer_ip_check_directory_empty(trans, ip);
3410                         hammer_lock_cursor(&cursor);
3411                         if (cursor.flags & HAMMER_CURSOR_RETEST) {
3412                                 kprintf("HAMMER: Warning: avoided deadlock "
3413                                         "on rmdir '%s'\n",
3414                                         ncp->nc_name);
3415                                 error = EDEADLK;
3416                         }
3417                 }
3418
3419                 /*
3420                  * Delete the directory entry.
3421                  *
3422                  * WARNING: hammer_ip_del_directory() may have to terminate
3423                  * the cursor to avoid a deadlock.  It is ok to call
3424                  * hammer_done_cursor() twice.
3425                  */
3426                 if (error == 0) {
3427                         error = hammer_ip_del_directory(trans, &cursor,
3428                                                         dip, ip);
3429                 }
3430                 hammer_done_cursor(&cursor);
3431                 if (error == 0) {
3432                         cache_setunresolved(nch);
3433                         cache_setvp(nch, NULL);
3434
3435                         /*
3436                          * NOTE: ip->vp, if non-NULL, cannot be directly
3437                          *       referenced without formally acquiring the
3438                          *       vp since the vp might have zero refs on it,
3439                          *       or in the middle of a reclaim, etc.
3440                          *
3441                          * NOTE: The cache_setunresolved() can rip the vp
3442                          *       out from under us since the vp may not have
3443                          *       any refs, in which case ip->vp will be NULL
3444                          *       from the outset.
3445                          */
3446                         while (ip && ip->vp) {
3447                                 struct vnode *vp;
3448
3449                                 error = hammer_get_vnode(ip, &vp);
3450                                 if (error == 0 && vp) {
3451                                         vn_unlock(vp);
3452                                         hammer_knote(ip->vp, NOTE_DELETE);
3453                                         cache_inval_vp(ip->vp, CINV_DESTROY);
3454                                         vrele(vp);
3455                                         break;
3456                                 }
3457                                 kprintf("Debug: HAMMER ip/vp race1 avoided\n");
3458                         }
3459                 }
3460                 if (ip)
3461                         hammer_rel_inode(ip, 0);
3462         } else {
3463                 hammer_done_cursor(&cursor);
3464         }
3465         if (error == EDEADLK)
3466                 goto retry;
3467
3468         return (error);
3469 }
3470
3471 /************************************************************************
3472  *                          FIFO AND SPECFS OPS                         *
3473  ************************************************************************
3474  *
3475  */
3476 static int
3477 hammer_vop_fifoclose (struct vop_close_args *ap)
3478 {
3479         /* XXX update itimes */
3480         return (VOCALL(&fifo_vnode_vops, &ap->a_head));
3481 }
3482
3483 static int
3484 hammer_vop_fiforead (struct vop_read_args *ap)
3485 {
3486         int error;
3487
3488         error = VOCALL(&fifo_vnode_vops, &ap->a_head);
3489         /* XXX update access time */
3490         return (error);
3491 }
3492
3493 static int
3494 hammer_vop_fifowrite (struct vop_write_args *ap)
3495 {
3496         int error;
3497
3498         error = VOCALL(&fifo_vnode_vops, &ap->a_head);
3499         /* XXX update access time */
3500         return (error);
3501 }
3502
3503 static
3504 int
3505 hammer_vop_fifokqfilter(struct vop_kqfilter_args *ap)
3506 {
3507         int error;
3508
3509         error = VOCALL(&fifo_vnode_vops, &ap->a_head);
3510         if (error)
3511                 error = hammer_vop_kqfilter(ap);
3512         return(error);
3513 }
3514
3515 /************************************************************************
3516  *                          KQFILTER OPS                                *
3517  ************************************************************************
3518  *
3519  */
3520 static void filt_hammerdetach(struct knote *kn);
3521 static int filt_hammerread(struct knote *kn, long hint);
3522 static int filt_hammerwrite(struct knote *kn, long hint);
3523 static int filt_hammervnode(struct knote *kn, long hint);
3524
3525 static struct filterops hammerread_filtops =
3526         { FILTEROP_ISFD, NULL, filt_hammerdetach, filt_hammerread };
3527 static struct filterops hammerwrite_filtops =
3528         { FILTEROP_ISFD, NULL, filt_hammerdetach, filt_hammerwrite };
3529 static struct filterops hammervnode_filtops =
3530         { FILTEROP_ISFD, NULL, filt_hammerdetach, filt_hammervnode };
3531
3532 static
3533 int
3534 hammer_vop_kqfilter(struct vop_kqfilter_args *ap)
3535 {
3536         struct vnode *vp = ap->a_vp;
3537         struct knote *kn = ap->a_kn;
3538
3539         switch (kn->kn_filter) {
3540         case EVFILT_READ:
3541                 kn->kn_fop = &hammerread_filtops;
3542                 break;
3543         case EVFILT_WRITE:
3544                 kn->kn_fop = &hammerwrite_filtops;
3545                 break;
3546         case EVFILT_VNODE:
3547                 kn->kn_fop = &hammervnode_filtops;
3548                 break;
3549         default:
3550                 return (EOPNOTSUPP);
3551         }
3552
3553         kn->kn_hook = (caddr_t)vp;
3554
3555         knote_insert(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
3556
3557         return(0);
3558 }
3559
3560 static void
3561 filt_hammerdetach(struct knote *kn)
3562 {
3563         struct vnode *vp = (void *)kn->kn_hook;
3564
3565         knote_remove(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
3566 }
3567
3568 static int
3569 filt_hammerread(struct knote *kn, long hint)
3570 {
3571         struct vnode *vp = (void *)kn->kn_hook;
3572         hammer_inode_t ip = VTOI(vp);
3573         hammer_mount_t hmp = ip->hmp;
3574         off_t off;
3575
3576         if (hint == NOTE_REVOKE) {
3577                 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
3578                 return(1);
3579         }
3580         lwkt_gettoken(&hmp->fs_token);  /* XXX use per-ip-token */
3581         off = ip->ino_data.size - kn->kn_fp->f_offset;
3582         kn->kn_data = (off < INTPTR_MAX) ? off : INTPTR_MAX;
3583         lwkt_reltoken(&hmp->fs_token);
3584         if (kn->kn_sfflags & NOTE_OLDAPI)
3585                 return(1);
3586         return (kn->kn_data != 0);
3587 }
3588
3589 static int
3590 filt_hammerwrite(struct knote *kn, long hint)
3591 {
3592         if (hint == NOTE_REVOKE)
3593                 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
3594         kn->kn_data = 0;
3595         return (1);
3596 }
3597
3598 static int
3599 filt_hammervnode(struct knote *kn, long hint)
3600 {
3601         if (kn->kn_sfflags & hint)
3602                 kn->kn_fflags |= hint;
3603         if (hint == NOTE_REVOKE) {
3604                 kn->kn_flags |= EV_EOF;
3605                 return (1);
3606         }
3607         return (kn->kn_fflags != 0);
3608 }
3609