HAMMER: Mirroring work
[dragonfly.git] / sys / vfs / hammer / hammer_io.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_io.c,v 1.52 2008/07/31 22:30:33 dillon Exp $
35  */
36 /*
37  * IO Primitives and buffer cache management
38  *
39  * All major data-tracking structures in HAMMER contain a struct hammer_io
40  * which is used to manage their backing store.  We use filesystem buffers
41  * for backing store and we leave them passively associated with their
42  * HAMMER structures.
43  *
44  * If the kernel tries to destroy a passively associated buf which we cannot
45  * yet let go we set B_LOCKED in the buffer and then actively released it
46  * later when we can.
47  */
48
49 #include "hammer.h"
50 #include <sys/fcntl.h>
51 #include <sys/nlookup.h>
52 #include <sys/buf.h>
53 #include <sys/buf2.h>
54
55 static void hammer_io_modify(hammer_io_t io, int count);
56 static void hammer_io_deallocate(struct buf *bp);
57 #if 0
58 static void hammer_io_direct_read_complete(struct bio *nbio);
59 #endif
60 static void hammer_io_direct_write_complete(struct bio *nbio);
61 static int hammer_io_direct_uncache_callback(hammer_inode_t ip, void *data);
62 static void hammer_io_set_modlist(struct hammer_io *io);
63
64 /*
65  * Initialize a new, already-zero'd hammer_io structure, or reinitialize
66  * an existing hammer_io structure which may have switched to another type.
67  */
68 void
69 hammer_io_init(hammer_io_t io, hammer_mount_t hmp, enum hammer_io_type type)
70 {
71         io->hmp = hmp;
72         io->type = type;
73 }
74
75 /*
76  * Helper routine to disassociate a buffer cache buffer from an I/O
77  * structure.  The buffer is unlocked and marked appropriate for reclamation.
78  *
79  * The io may have 0 or 1 references depending on who called us.  The
80  * caller is responsible for dealing with the refs.
81  *
82  * This call can only be made when no action is required on the buffer.
83  *
84  * The caller must own the buffer and the IO must indicate that the
85  * structure no longer owns it (io.released != 0).
86  */
87 static void
88 hammer_io_disassociate(hammer_io_structure_t iou)
89 {
90         struct buf *bp = iou->io.bp;
91
92         KKASSERT(iou->io.released);
93         KKASSERT(iou->io.modified == 0);
94         KKASSERT(LIST_FIRST(&bp->b_dep) == (void *)iou);
95         buf_dep_init(bp);
96         iou->io.bp = NULL;
97
98         /*
99          * If the buffer was locked someone wanted to get rid of it.
100          */
101         if (bp->b_flags & B_LOCKED) {
102                 --hammer_count_io_locked;
103                 bp->b_flags &= ~B_LOCKED;
104         }
105         if (iou->io.reclaim) {
106                 bp->b_flags |= B_NOCACHE|B_RELBUF;
107                 iou->io.reclaim = 0;
108         }
109
110         switch(iou->io.type) {
111         case HAMMER_STRUCTURE_VOLUME:
112                 iou->volume.ondisk = NULL;
113                 break;
114         case HAMMER_STRUCTURE_DATA_BUFFER:
115         case HAMMER_STRUCTURE_META_BUFFER:
116         case HAMMER_STRUCTURE_UNDO_BUFFER:
117                 iou->buffer.ondisk = NULL;
118                 break;
119         }
120 }
121
122 /*
123  * Wait for any physical IO to complete
124  */
125 void
126 hammer_io_wait(hammer_io_t io)
127 {
128         if (io->running) {
129                 crit_enter();
130                 tsleep_interlock(io);
131                 io->waiting = 1;
132                 for (;;) {
133                         tsleep(io, 0, "hmrflw", 0);
134                         if (io->running == 0)
135                                 break;
136                         tsleep_interlock(io);
137                         io->waiting = 1;
138                         if (io->running == 0)
139                                 break;
140                 }
141                 crit_exit();
142         }
143 }
144
145 /*
146  * Wait for all hammer_io-initated write I/O's to complete.  This is not
147  * supposed to count direct I/O's but some can leak through (for
148  * non-full-sized direct I/Os).
149  */
150 void
151 hammer_io_wait_all(hammer_mount_t hmp, const char *ident)
152 {
153         crit_enter();
154         while (hmp->io_running_space)
155                 tsleep(&hmp->io_running_space, 0, ident, 0);
156         crit_exit();
157 }
158
159 #define HAMMER_MAXRA    4
160
161 /*
162  * Load bp for a HAMMER structure.  The io must be exclusively locked by
163  * the caller.
164  *
165  * This routine is mostly used on meta-data and small-data blocks.  Generally
166  * speaking HAMMER assumes some locality of reference and will cluster 
167  * a 64K read.
168  *
169  * Note that clustering occurs at the device layer, not the logical layer.
170  * If the buffers do not apply to the current operation they may apply to
171  * some other.
172  */
173 int
174 hammer_io_read(struct vnode *devvp, struct hammer_io *io, hammer_off_t limit)
175 {
176         struct buf *bp;
177         int   error;
178
179         if ((bp = io->bp) == NULL) {
180                 hammer_count_io_running_read += io->bytes;
181                 if (hammer_cluster_enable) {
182                         error = cluster_read(devvp, limit,
183                                              io->offset, io->bytes,
184                                              HAMMER_CLUSTER_SIZE,
185                                              HAMMER_CLUSTER_BUFS, &io->bp);
186                 } else {
187                         error = bread(devvp, io->offset, io->bytes, &io->bp);
188                 }
189                 hammer_stats_disk_read += io->bytes;
190                 hammer_count_io_running_read -= io->bytes;
191
192                 /*
193                  * The code generally assumes b_ops/b_dep has been set-up,
194                  * even if we error out here.
195                  */
196                 bp = io->bp;
197                 bp->b_ops = &hammer_bioops;
198                 KKASSERT(LIST_FIRST(&bp->b_dep) == NULL);
199                 LIST_INSERT_HEAD(&bp->b_dep, &io->worklist, node);
200                 BUF_KERNPROC(bp);
201                 KKASSERT(io->modified == 0);
202                 KKASSERT(io->running == 0);
203                 KKASSERT(io->waiting == 0);
204                 io->released = 0;       /* we hold an active lock on bp */
205         } else {
206                 error = 0;
207         }
208         return(error);
209 }
210
211 /*
212  * Similar to hammer_io_read() but returns a zero'd out buffer instead.
213  * Must be called with the IO exclusively locked.
214  *
215  * vfs_bio_clrbuf() is kinda nasty, enforce serialization against background
216  * I/O by forcing the buffer to not be in a released state before calling
217  * it.
218  *
219  * This function will also mark the IO as modified but it will not
220  * increment the modify_refs count.
221  */
222 int
223 hammer_io_new(struct vnode *devvp, struct hammer_io *io)
224 {
225         struct buf *bp;
226
227         if ((bp = io->bp) == NULL) {
228                 io->bp = getblk(devvp, io->offset, io->bytes, 0, 0);
229                 bp = io->bp;
230                 bp->b_ops = &hammer_bioops;
231                 KKASSERT(LIST_FIRST(&bp->b_dep) == NULL);
232                 LIST_INSERT_HEAD(&bp->b_dep, &io->worklist, node);
233                 io->released = 0;
234                 KKASSERT(io->running == 0);
235                 io->waiting = 0;
236                 BUF_KERNPROC(bp);
237         } else {
238                 if (io->released) {
239                         regetblk(bp);
240                         BUF_KERNPROC(bp);
241                         io->released = 0;
242                 }
243         }
244         hammer_io_modify(io, 0);
245         vfs_bio_clrbuf(bp);
246         return(0);
247 }
248
249 /*
250  * Remove potential device level aliases against buffers managed by high level
251  * vnodes.  Aliases can also be created due to mixed buffer sizes.
252  *
253  * This is nasty because the buffers are also VMIO-backed.  Even if a buffer
254  * does not exist its backing VM pages might, and we have to invalidate
255  * those as well or a getblk() will reinstate them.
256  */
257 void
258 hammer_io_inval(hammer_volume_t volume, hammer_off_t zone2_offset)
259 {
260         hammer_io_structure_t iou;
261         hammer_off_t phys_offset;
262         struct buf *bp;
263
264         phys_offset = volume->ondisk->vol_buf_beg +
265                       (zone2_offset & HAMMER_OFF_SHORT_MASK);
266         crit_enter();
267         if ((bp = findblk(volume->devvp, phys_offset)) != NULL)
268                 bp = getblk(volume->devvp, phys_offset, bp->b_bufsize, 0, 0);
269         else
270                 bp = getblk(volume->devvp, phys_offset, HAMMER_BUFSIZE, 0, 0);
271         if ((iou = (void *)LIST_FIRST(&bp->b_dep)) != NULL) {
272                 hammer_io_clear_modify(&iou->io, 1);
273                 bundirty(bp);
274                 iou->io.reclaim = 1;
275                 hammer_io_deallocate(bp);
276         } else {
277                 KKASSERT((bp->b_flags & B_LOCKED) == 0);
278                 bundirty(bp);
279                 bp->b_flags |= B_NOCACHE|B_RELBUF;
280         }
281         brelse(bp);
282         crit_exit();
283 }
284
285 /*
286  * This routine is called on the last reference to a hammer structure.
287  * The io is usually interlocked with io.loading and io.refs must be 1.
288  *
289  * This routine may return a non-NULL bp to the caller for dispoal.  Disposal
290  * simply means the caller finishes decrementing the ref-count on the 
291  * IO structure then brelse()'s the bp.  The bp may or may not still be
292  * passively associated with the IO.
293  * 
294  * The only requirement here is that modified meta-data and volume-header
295  * buffer may NOT be disassociated from the IO structure, and consequently
296  * we also leave such buffers actively associated with the IO if they already
297  * are (since the kernel can't do anything with them anyway).  Only the
298  * flusher is allowed to write such buffers out.  Modified pure-data and
299  * undo buffers are returned to the kernel but left passively associated
300  * so we can track when the kernel writes the bp out.
301  */
302 struct buf *
303 hammer_io_release(struct hammer_io *io, int flush)
304 {
305         union hammer_io_structure *iou = (void *)io;
306         struct buf *bp;
307
308         if ((bp = io->bp) == NULL)
309                 return(NULL);
310
311         /*
312          * Try to flush a dirty IO to disk if asked to by the
313          * caller or if the kernel tried to flush the buffer in the past.
314          *
315          * Kernel-initiated flushes are only allowed for pure-data buffers.
316          * meta-data and volume buffers can only be flushed explicitly
317          * by HAMMER.
318          */
319         if (io->modified) {
320                 if (flush) {
321                         hammer_io_flush(io);
322                 } else if (bp->b_flags & B_LOCKED) {
323                         switch(io->type) {
324                         case HAMMER_STRUCTURE_DATA_BUFFER:
325                         case HAMMER_STRUCTURE_UNDO_BUFFER:
326                                 hammer_io_flush(io);
327                                 break;
328                         default:
329                                 break;
330                         }
331                 } /* else no explicit request to flush the buffer */
332         }
333
334         /*
335          * Wait for the IO to complete if asked to.
336          */
337         if (io->waitdep && io->running) {
338                 hammer_io_wait(io);
339         }
340
341         /*
342          * Return control of the buffer to the kernel (with the provisio
343          * that our bioops can override kernel decisions with regards to
344          * the buffer).
345          */
346         if ((flush || io->reclaim) && io->modified == 0 && io->running == 0) {
347                 /*
348                  * Always disassociate the bp if an explicit flush
349                  * was requested and the IO completed with no error
350                  * (so unmount can really clean up the structure).
351                  */
352                 if (io->released) {
353                         regetblk(bp);
354                         BUF_KERNPROC(bp);
355                 } else {
356                         io->released = 1;
357                 }
358                 hammer_io_disassociate((hammer_io_structure_t)io);
359                 /* return the bp */
360         } else if (io->modified) {
361                 /*
362                  * Only certain IO types can be released to the kernel if
363                  * the buffer has been modified.
364                  *
365                  * volume and meta-data IO types may only be explicitly
366                  * flushed by HAMMER.
367                  */
368                 switch(io->type) {
369                 case HAMMER_STRUCTURE_DATA_BUFFER:
370                 case HAMMER_STRUCTURE_UNDO_BUFFER:
371                         if (io->released == 0) {
372                                 io->released = 1;
373                                 bdwrite(bp);
374                         }
375                         break;
376                 default:
377                         break;
378                 }
379                 bp = NULL;      /* bp left associated */
380         } else if (io->released == 0) {
381                 /*
382                  * Clean buffers can be generally released to the kernel.
383                  * We leave the bp passively associated with the HAMMER
384                  * structure and use bioops to disconnect it later on
385                  * if the kernel wants to discard the buffer.
386                  *
387                  * We can steal the structure's ownership of the bp.
388                  */
389                 io->released = 1;
390                 if (bp->b_flags & B_LOCKED) {
391                         hammer_io_disassociate(iou);
392                         /* return the bp */
393                 } else {
394                         if (io->reclaim) {
395                                 hammer_io_disassociate(iou);
396                                 /* return the bp */
397                         } else {
398                                 /* return the bp (bp passively associated) */
399                         }
400                 }
401         } else {
402                 /*
403                  * A released buffer is passively associate with our
404                  * hammer_io structure.  The kernel cannot destroy it
405                  * without making a bioops call.  If the kernel (B_LOCKED)
406                  * or we (reclaim) requested that the buffer be destroyed
407                  * we destroy it, otherwise we do a quick get/release to
408                  * reset its position in the kernel's LRU list.
409                  *
410                  * Leaving the buffer passively associated allows us to
411                  * use the kernel's LRU buffer flushing mechanisms rather
412                  * then rolling our own.
413                  *
414                  * XXX there are two ways of doing this.  We can re-acquire
415                  * and passively release to reset the LRU, or not.
416                  */
417                 if (io->running == 0) {
418                         regetblk(bp);
419                         if ((bp->b_flags & B_LOCKED) || io->reclaim) {
420                                 hammer_io_disassociate(iou);
421                                 /* return the bp */
422                         } else {
423                                 /* return the bp (bp passively associated) */
424                         }
425                 } else {
426                         /*
427                          * bp is left passively associated but we do not
428                          * try to reacquire it.  Interactions with the io
429                          * structure will occur on completion of the bp's
430                          * I/O.
431                          */
432                         bp = NULL;
433                 }
434         }
435         return(bp);
436 }
437
438 /*
439  * This routine is called with a locked IO when a flush is desired and
440  * no other references to the structure exists other then ours.  This
441  * routine is ONLY called when HAMMER believes it is safe to flush a
442  * potentially modified buffer out.
443  */
444 void
445 hammer_io_flush(struct hammer_io *io)
446 {
447         struct buf *bp;
448
449         /*
450          * Degenerate case - nothing to flush if nothing is dirty.
451          */
452         if (io->modified == 0) {
453                 return;
454         }
455
456         KKASSERT(io->bp);
457         KKASSERT(io->modify_refs <= 0);
458
459         /*
460          * Acquire ownership of the bp, particularly before we clear our
461          * modified flag.
462          *
463          * We are going to bawrite() this bp.  Don't leave a window where
464          * io->released is set, we actually own the bp rather then our
465          * buffer.
466          */
467         bp = io->bp;
468         if (io->released) {
469                 regetblk(bp);
470                 /* BUF_KERNPROC(io->bp); */
471                 /* io->released = 0; */
472                 KKASSERT(io->released);
473                 KKASSERT(io->bp == bp);
474         }
475         io->released = 1;
476
477         /*
478          * Acquire exclusive access to the bp and then clear the modified
479          * state of the buffer prior to issuing I/O to interlock any
480          * modifications made while the I/O is in progress.  This shouldn't
481          * happen anyway but losing data would be worse.  The modified bit
482          * will be rechecked after the IO completes.
483          *
484          * NOTE: This call also finalizes the buffer's content (inval == 0).
485          *
486          * This is only legal when lock.refs == 1 (otherwise we might clear
487          * the modified bit while there are still users of the cluster
488          * modifying the data).
489          *
490          * Do this before potentially blocking so any attempt to modify the
491          * ondisk while we are blocked blocks waiting for us.
492          */
493         hammer_io_clear_modify(io, 0);
494
495         /*
496          * Transfer ownership to the kernel and initiate I/O.
497          */
498         io->running = 1;
499         io->hmp->io_running_space += io->bytes;
500         hammer_count_io_running_write += io->bytes;
501         bawrite(bp);
502 }
503
504 /************************************************************************
505  *                              BUFFER DIRTYING                         *
506  ************************************************************************
507  *
508  * These routines deal with dependancies created when IO buffers get
509  * modified.  The caller must call hammer_modify_*() on a referenced
510  * HAMMER structure prior to modifying its on-disk data.
511  *
512  * Any intent to modify an IO buffer acquires the related bp and imposes
513  * various write ordering dependancies.
514  */
515
516 /*
517  * Mark a HAMMER structure as undergoing modification.  Meta-data buffers
518  * are locked until the flusher can deal with them, pure data buffers
519  * can be written out.
520  */
521 static
522 void
523 hammer_io_modify(hammer_io_t io, int count)
524 {
525         /*
526          * io->modify_refs must be >= 0
527          */
528         while (io->modify_refs < 0) {
529                 io->waitmod = 1;
530                 tsleep(io, 0, "hmrmod", 0);
531         }
532
533         /*
534          * Shortcut if nothing to do.
535          */
536         KKASSERT(io->lock.refs != 0 && io->bp != NULL);
537         io->modify_refs += count;
538         if (io->modified && io->released == 0)
539                 return;
540
541         hammer_lock_ex(&io->lock);
542         if (io->modified == 0) {
543                 hammer_io_set_modlist(io);
544                 io->modified = 1;
545         }
546         if (io->released) {
547                 regetblk(io->bp);
548                 BUF_KERNPROC(io->bp);
549                 io->released = 0;
550                 KKASSERT(io->modified != 0);
551         }
552         hammer_unlock(&io->lock);
553 }
554
555 static __inline
556 void
557 hammer_io_modify_done(hammer_io_t io)
558 {
559         KKASSERT(io->modify_refs > 0);
560         --io->modify_refs;
561         if (io->modify_refs == 0 && io->waitmod) {
562                 io->waitmod = 0;
563                 wakeup(io);
564         }
565 }
566
567 void
568 hammer_io_write_interlock(hammer_io_t io)
569 {
570         while (io->modify_refs != 0) {
571                 io->waitmod = 1;
572                 tsleep(io, 0, "hmrmod", 0);
573         }
574         io->modify_refs = -1;
575 }
576
577 void
578 hammer_io_done_interlock(hammer_io_t io)
579 {
580         KKASSERT(io->modify_refs == -1);
581         io->modify_refs = 0;
582         if (io->waitmod) {
583                 io->waitmod = 0;
584                 wakeup(io);
585         }
586 }
587
588 /*
589  * Caller intends to modify a volume's ondisk structure.
590  *
591  * This is only allowed if we are the flusher or we have a ref on the
592  * sync_lock.
593  */
594 void
595 hammer_modify_volume(hammer_transaction_t trans, hammer_volume_t volume,
596                      void *base, int len)
597 {
598         KKASSERT (trans == NULL || trans->sync_lock_refs > 0);
599
600         hammer_io_modify(&volume->io, 1);
601         if (len) {
602                 intptr_t rel_offset = (intptr_t)base - (intptr_t)volume->ondisk;
603                 KKASSERT((rel_offset & ~(intptr_t)HAMMER_BUFMASK) == 0);
604                 hammer_generate_undo(trans, &volume->io,
605                          HAMMER_ENCODE_RAW_VOLUME(volume->vol_no, rel_offset),
606                          base, len);
607         }
608 }
609
610 /*
611  * Caller intends to modify a buffer's ondisk structure.
612  *
613  * This is only allowed if we are the flusher or we have a ref on the
614  * sync_lock.
615  */
616 void
617 hammer_modify_buffer(hammer_transaction_t trans, hammer_buffer_t buffer,
618                      void *base, int len)
619 {
620         KKASSERT (trans == NULL || trans->sync_lock_refs > 0);
621
622         hammer_io_modify(&buffer->io, 1);
623         if (len) {
624                 intptr_t rel_offset = (intptr_t)base - (intptr_t)buffer->ondisk;
625                 KKASSERT((rel_offset & ~(intptr_t)HAMMER_BUFMASK) == 0);
626                 hammer_generate_undo(trans, &buffer->io,
627                                      buffer->zone2_offset + rel_offset,
628                                      base, len);
629         }
630 }
631
632 void
633 hammer_modify_volume_done(hammer_volume_t volume)
634 {
635         hammer_io_modify_done(&volume->io);
636 }
637
638 void
639 hammer_modify_buffer_done(hammer_buffer_t buffer)
640 {
641         hammer_io_modify_done(&buffer->io);
642 }
643
644 /*
645  * Mark an entity as not being dirty any more and finalize any
646  * delayed adjustments to the buffer.
647  *
648  * Delayed adjustments are an important performance enhancement, allowing
649  * us to avoid recalculating B-Tree node CRCs over and over again when
650  * making bulk-modifications to the B-Tree.
651  *
652  * If inval is non-zero delayed adjustments are ignored.
653  */
654 void
655 hammer_io_clear_modify(struct hammer_io *io, int inval)
656 {
657         if (io->modified == 0)
658                 return;
659
660         /*
661          * Take us off the mod-list and clear the modified bit.
662          */
663         KKASSERT(io->mod_list != NULL);
664         if (io->mod_list == &io->hmp->volu_list ||
665             io->mod_list == &io->hmp->meta_list) {
666                 io->hmp->locked_dirty_space -= io->bytes;
667                 hammer_count_dirtybufspace -= io->bytes;
668         }
669         TAILQ_REMOVE(io->mod_list, io, mod_entry);
670         io->mod_list = NULL;
671         io->modified = 0;
672
673         /*
674          * If this bit is not set there are no delayed adjustments.
675          */
676         if (io->gencrc == 0)
677                 return;
678         io->gencrc = 0;
679
680         /*
681          * Finalize requested CRCs.  The NEEDSCRC flag also holds a reference
682          * on the node (& underlying buffer).  Release the node after clearing
683          * the flag.
684          */
685         if (io->type == HAMMER_STRUCTURE_META_BUFFER) {
686                 hammer_buffer_t buffer = (void *)io;
687                 hammer_node_t node;
688
689 restart:
690                 TAILQ_FOREACH(node, &buffer->clist, entry) {
691                         if ((node->flags & HAMMER_NODE_NEEDSCRC) == 0)
692                                 continue;
693                         node->flags &= ~HAMMER_NODE_NEEDSCRC;
694                         KKASSERT(node->ondisk);
695                         if (inval == 0)
696                                 node->ondisk->crc = crc32(&node->ondisk->crc + 1, HAMMER_BTREE_CRCSIZE);
697                         hammer_rel_node(node);
698                         goto restart;
699                 }
700         }
701
702 }
703
704 /*
705  * Clear the IO's modify list.  Even though the IO is no longer modified
706  * it may still be on the lose_list.  This routine is called just before
707  * the governing hammer_buffer is destroyed.
708  */
709 void
710 hammer_io_clear_modlist(struct hammer_io *io)
711 {
712         KKASSERT(io->modified == 0);
713         if (io->mod_list) {
714                 crit_enter();   /* biodone race against list */
715                 KKASSERT(io->mod_list == &io->hmp->lose_list);
716                 TAILQ_REMOVE(io->mod_list, io, mod_entry);
717                 io->mod_list = NULL;
718                 crit_exit();
719         }
720 }
721
722 static void
723 hammer_io_set_modlist(struct hammer_io *io)
724 {
725         struct hammer_mount *hmp = io->hmp;
726
727         KKASSERT(io->mod_list == NULL);
728
729         switch(io->type) {
730         case HAMMER_STRUCTURE_VOLUME:
731                 io->mod_list = &hmp->volu_list;
732                 hmp->locked_dirty_space += io->bytes;
733                 hammer_count_dirtybufspace += io->bytes;
734                 break;
735         case HAMMER_STRUCTURE_META_BUFFER:
736                 io->mod_list = &hmp->meta_list;
737                 hmp->locked_dirty_space += io->bytes;
738                 hammer_count_dirtybufspace += io->bytes;
739                 break;
740         case HAMMER_STRUCTURE_UNDO_BUFFER:
741                 io->mod_list = &hmp->undo_list;
742                 break;
743         case HAMMER_STRUCTURE_DATA_BUFFER:
744                 io->mod_list = &hmp->data_list;
745                 break;
746         }
747         TAILQ_INSERT_TAIL(io->mod_list, io, mod_entry);
748 }
749
750 /************************************************************************
751  *                              HAMMER_BIOOPS                           *
752  ************************************************************************
753  *
754  */
755
756 /*
757  * Pre-IO initiation kernel callback - cluster build only
758  */
759 static void
760 hammer_io_start(struct buf *bp)
761 {
762 }
763
764 /*
765  * Post-IO completion kernel callback - MAY BE CALLED FROM INTERRUPT!
766  *
767  * NOTE: HAMMER may modify a buffer after initiating I/O.  The modified bit
768  * may also be set if we were marking a cluster header open.  Only remove
769  * our dependancy if the modified bit is clear.
770  */
771 static void
772 hammer_io_complete(struct buf *bp)
773 {
774         union hammer_io_structure *iou = (void *)LIST_FIRST(&bp->b_dep);
775
776         KKASSERT(iou->io.released == 1);
777
778         /*
779          * Deal with people waiting for I/O to drain
780          */
781         if (iou->io.running) {
782                 /*
783                  * Deal with critical write errors.  Once a critical error
784                  * has been flagged in hmp the UNDO FIFO will not be updated.
785                  * That way crash recover will give us a consistent
786                  * filesystem.
787                  *
788                  * Because of this we can throw away failed UNDO buffers.  If
789                  * we throw away META or DATA buffers we risk corrupting
790                  * the now read-only version of the filesystem visible to
791                  * the user.  Clear B_ERROR so the buffer is not re-dirtied
792                  * by the kernel and ref the io so it doesn't get thrown
793                  * away.
794                  */
795                 if (bp->b_flags & B_ERROR) {
796                         hammer_critical_error(iou->io.hmp, NULL, bp->b_error,
797                                               "while flushing meta-data");
798                         switch(iou->io.type) {
799                         case HAMMER_STRUCTURE_UNDO_BUFFER:
800                                 break;
801                         default:
802                                 if (iou->io.ioerror == 0) {
803                                         iou->io.ioerror = 1;
804                                         if (iou->io.lock.refs == 0)
805                                                 ++hammer_count_refedbufs;
806                                         hammer_ref(&iou->io.lock);
807                                 }
808                                 break;
809                         }
810                         bp->b_flags &= ~B_ERROR;
811                         bundirty(bp);
812 #if 0
813                         hammer_io_set_modlist(&iou->io);
814                         iou->io.modified = 1;
815 #endif
816                 }
817                 hammer_stats_disk_write += iou->io.bytes;
818                 hammer_count_io_running_write -= iou->io.bytes;
819                 iou->io.hmp->io_running_space -= iou->io.bytes;
820                 if (iou->io.hmp->io_running_space == 0)
821                         wakeup(&iou->io.hmp->io_running_space);
822                 KKASSERT(iou->io.hmp->io_running_space >= 0);
823                 iou->io.running = 0;
824         } else {
825                 hammer_stats_disk_read += iou->io.bytes;
826         }
827
828         if (iou->io.waiting) {
829                 iou->io.waiting = 0;
830                 wakeup(iou);
831         }
832
833         /*
834          * If B_LOCKED is set someone wanted to deallocate the bp at some
835          * point, do it now if refs has become zero.
836          */
837         if ((bp->b_flags & B_LOCKED) && iou->io.lock.refs == 0) {
838                 KKASSERT(iou->io.modified == 0);
839                 --hammer_count_io_locked;
840                 bp->b_flags &= ~B_LOCKED;
841                 hammer_io_deallocate(bp);
842                 /* structure may be dead now */
843         }
844 }
845
846 /*
847  * Callback from kernel when it wishes to deallocate a passively
848  * associated structure.  This mostly occurs with clean buffers
849  * but it may be possible for a holding structure to be marked dirty
850  * while its buffer is passively associated.  The caller owns the bp.
851  *
852  * If we cannot disassociate we set B_LOCKED to prevent the buffer
853  * from getting reused.
854  *
855  * WARNING: Because this can be called directly by getnewbuf we cannot
856  * recurse into the tree.  If a bp cannot be immediately disassociated
857  * our only recourse is to set B_LOCKED.
858  *
859  * WARNING: This may be called from an interrupt via hammer_io_complete()
860  */
861 static void
862 hammer_io_deallocate(struct buf *bp)
863 {
864         hammer_io_structure_t iou = (void *)LIST_FIRST(&bp->b_dep);
865
866         KKASSERT((bp->b_flags & B_LOCKED) == 0 && iou->io.running == 0);
867         if (iou->io.lock.refs > 0 || iou->io.modified) {
868                 /*
869                  * It is not legal to disassociate a modified buffer.  This
870                  * case really shouldn't ever occur.
871                  */
872                 bp->b_flags |= B_LOCKED;
873                 ++hammer_count_io_locked;
874         } else {
875                 /*
876                  * Disassociate the BP.  If the io has no refs left we
877                  * have to add it to the loose list.
878                  */
879                 hammer_io_disassociate(iou);
880                 if (iou->io.type != HAMMER_STRUCTURE_VOLUME) {
881                         KKASSERT(iou->io.bp == NULL);
882                         KKASSERT(iou->io.mod_list == NULL);
883                         crit_enter();   /* biodone race against list */
884                         iou->io.mod_list = &iou->io.hmp->lose_list;
885                         TAILQ_INSERT_TAIL(iou->io.mod_list, &iou->io, mod_entry);
886                         crit_exit();
887                 }
888         }
889 }
890
891 static int
892 hammer_io_fsync(struct vnode *vp)
893 {
894         return(0);
895 }
896
897 /*
898  * NOTE: will not be called unless we tell the kernel about the
899  * bioops.  Unused... we use the mount's VFS_SYNC instead.
900  */
901 static int
902 hammer_io_sync(struct mount *mp)
903 {
904         return(0);
905 }
906
907 static void
908 hammer_io_movedeps(struct buf *bp1, struct buf *bp2)
909 {
910 }
911
912 /*
913  * I/O pre-check for reading and writing.  HAMMER only uses this for
914  * B_CACHE buffers so checkread just shouldn't happen, but if it does
915  * allow it.
916  *
917  * Writing is a different case.  We don't want the kernel to try to write
918  * out a buffer that HAMMER may be modifying passively or which has a
919  * dependancy.  In addition, kernel-demanded writes can only proceed for
920  * certain types of buffers (i.e. UNDO and DATA types).  Other dirty
921  * buffer types can only be explicitly written by the flusher.
922  *
923  * checkwrite will only be called for bdwrite()n buffers.  If we return
924  * success the kernel is guaranteed to initiate the buffer write.
925  */
926 static int
927 hammer_io_checkread(struct buf *bp)
928 {
929         return(0);
930 }
931
932 static int
933 hammer_io_checkwrite(struct buf *bp)
934 {
935         hammer_io_t io = (void *)LIST_FIRST(&bp->b_dep);
936
937         /*
938          * This shouldn't happen under normal operation.
939          */
940         if (io->type == HAMMER_STRUCTURE_VOLUME ||
941             io->type == HAMMER_STRUCTURE_META_BUFFER) {
942                 if (!panicstr)
943                         panic("hammer_io_checkwrite: illegal buffer");
944                 if ((bp->b_flags & B_LOCKED) == 0) {
945                         bp->b_flags |= B_LOCKED;
946                         ++hammer_count_io_locked;
947                 }
948                 return(1);
949         }
950
951         /*
952          * We can only clear the modified bit if the IO is not currently
953          * undergoing modification.  Otherwise we may miss changes.
954          */
955         if (io->modify_refs == 0 && io->modified)
956                 hammer_io_clear_modify(io, 0);
957
958         /*
959          * The kernel is going to start the IO, set io->running.
960          */
961         KKASSERT(io->running == 0);
962         io->running = 1;
963         io->hmp->io_running_space += io->bytes;
964         hammer_count_io_running_write += io->bytes;
965         return(0);
966 }
967
968 /*
969  * Return non-zero if we wish to delay the kernel's attempt to flush
970  * this buffer to disk.
971  */
972 static int
973 hammer_io_countdeps(struct buf *bp, int n)
974 {
975         return(0);
976 }
977
978 struct bio_ops hammer_bioops = {
979         .io_start       = hammer_io_start,
980         .io_complete    = hammer_io_complete,
981         .io_deallocate  = hammer_io_deallocate,
982         .io_fsync       = hammer_io_fsync,
983         .io_sync        = hammer_io_sync,
984         .io_movedeps    = hammer_io_movedeps,
985         .io_countdeps   = hammer_io_countdeps,
986         .io_checkread   = hammer_io_checkread,
987         .io_checkwrite  = hammer_io_checkwrite,
988 };
989
990 /************************************************************************
991  *                              DIRECT IO OPS                           *
992  ************************************************************************
993  *
994  * These functions operate directly on the buffer cache buffer associated
995  * with a front-end vnode rather then a back-end device vnode.
996  */
997
998 /*
999  * Read a buffer associated with a front-end vnode directly from the
1000  * disk media.  The bio may be issued asynchronously.  If leaf is non-NULL
1001  * we validate the CRC.
1002  *
1003  * We must check for the presence of a HAMMER buffer to handle the case
1004  * where the reblocker has rewritten the data (which it does via the HAMMER
1005  * buffer system, not via the high-level vnode buffer cache), but not yet
1006  * committed the buffer to the media. 
1007  */
1008 int
1009 hammer_io_direct_read(hammer_mount_t hmp, struct bio *bio,
1010                       hammer_btree_leaf_elm_t leaf)
1011 {
1012         hammer_off_t buf_offset;
1013         hammer_off_t zone2_offset;
1014         hammer_volume_t volume;
1015         struct buf *bp;
1016         struct bio *nbio;
1017         int vol_no;
1018         int error;
1019
1020         buf_offset = bio->bio_offset;
1021         KKASSERT((buf_offset & HAMMER_OFF_ZONE_MASK) ==
1022                  HAMMER_ZONE_LARGE_DATA);
1023
1024         /*
1025          * The buffer cache may have an aliased buffer (the reblocker can
1026          * write them).  If it does we have to sync any dirty data before
1027          * we can build our direct-read.  This is a non-critical code path.
1028          */
1029         bp = bio->bio_buf;
1030         hammer_sync_buffers(hmp, buf_offset, bp->b_bufsize);
1031
1032         /*
1033          * Resolve to a zone-2 offset.  The conversion just requires
1034          * munging the top 4 bits but we want to abstract it anyway
1035          * so the blockmap code can verify the zone assignment.
1036          */
1037         zone2_offset = hammer_blockmap_lookup(hmp, buf_offset, &error);
1038         if (error)
1039                 goto done;
1040         KKASSERT((zone2_offset & HAMMER_OFF_ZONE_MASK) ==
1041                  HAMMER_ZONE_RAW_BUFFER);
1042
1043         /*
1044          * Resolve volume and raw-offset for 3rd level bio.  The
1045          * offset will be specific to the volume.
1046          */
1047         vol_no = HAMMER_VOL_DECODE(zone2_offset);
1048         volume = hammer_get_volume(hmp, vol_no, &error);
1049         if (error == 0 && zone2_offset >= volume->maxbuf_off)
1050                 error = EIO;
1051
1052         if (error == 0) {
1053                 /*
1054                  * 3rd level bio
1055                  */
1056                 nbio = push_bio(bio);
1057                 nbio->bio_offset = volume->ondisk->vol_buf_beg +
1058                                    (zone2_offset & HAMMER_OFF_SHORT_MASK);
1059 #if 0
1060                 /*
1061                  * XXX disabled - our CRC check doesn't work if the OS
1062                  * does bogus_page replacement on the direct-read.
1063                  */
1064                 if (leaf && hammer_verify_data) {
1065                         nbio->bio_done = hammer_io_direct_read_complete;
1066                         nbio->bio_caller_info1.uvalue32 = leaf->data_crc;
1067                 }
1068 #endif
1069                 hammer_stats_disk_read += bp->b_bufsize;
1070                 vn_strategy(volume->devvp, nbio);
1071         }
1072         hammer_rel_volume(volume, 0);
1073 done:
1074         if (error) {
1075                 kprintf("hammer_direct_read: failed @ %016llx\n",
1076                         zone2_offset);
1077                 bp->b_error = error;
1078                 bp->b_flags |= B_ERROR;
1079                 biodone(bio);
1080         }
1081         return(error);
1082 }
1083
1084 #if 0
1085 /*
1086  * On completion of the BIO this callback must check the data CRC
1087  * and chain to the previous bio.
1088  */
1089 static
1090 void
1091 hammer_io_direct_read_complete(struct bio *nbio)
1092 {
1093         struct bio *obio;
1094         struct buf *bp;
1095         u_int32_t rec_crc = nbio->bio_caller_info1.uvalue32;
1096
1097         bp = nbio->bio_buf;
1098         if (crc32(bp->b_data, bp->b_bufsize) != rec_crc) {
1099                 kprintf("HAMMER: data_crc error @%016llx/%d\n",
1100                         nbio->bio_offset, bp->b_bufsize);
1101                 if (hammer_debug_debug)
1102                         Debugger("");
1103                 bp->b_flags |= B_ERROR;
1104                 bp->b_error = EIO;
1105         }
1106         obio = pop_bio(nbio);
1107         biodone(obio);
1108 }
1109 #endif
1110
1111 /*
1112  * Write a buffer associated with a front-end vnode directly to the
1113  * disk media.  The bio may be issued asynchronously.
1114  *
1115  * The BIO is associated with the specified record and RECF_DIRECT_IO
1116  * is set.  The recorded is added to its object.
1117  */
1118 int
1119 hammer_io_direct_write(hammer_mount_t hmp, hammer_record_t record,
1120                        struct bio *bio)
1121 {
1122         hammer_btree_leaf_elm_t leaf = &record->leaf;
1123         hammer_off_t buf_offset;
1124         hammer_off_t zone2_offset;
1125         hammer_volume_t volume;
1126         hammer_buffer_t buffer;
1127         struct buf *bp;
1128         struct bio *nbio;
1129         char *ptr;
1130         int vol_no;
1131         int error;
1132
1133         buf_offset = leaf->data_offset;
1134
1135         KKASSERT(buf_offset > HAMMER_ZONE_BTREE);
1136         KKASSERT(bio->bio_buf->b_cmd == BUF_CMD_WRITE);
1137
1138         if ((buf_offset & HAMMER_BUFMASK) == 0 &&
1139             leaf->data_len >= HAMMER_BUFSIZE) {
1140                 /*
1141                  * We are using the vnode's bio to write directly to the
1142                  * media, any hammer_buffer at the same zone-X offset will
1143                  * now have stale data.
1144                  */
1145                 zone2_offset = hammer_blockmap_lookup(hmp, buf_offset, &error);
1146                 vol_no = HAMMER_VOL_DECODE(zone2_offset);
1147                 volume = hammer_get_volume(hmp, vol_no, &error);
1148
1149                 if (error == 0 && zone2_offset >= volume->maxbuf_off)
1150                         error = EIO;
1151                 if (error == 0) {
1152                         bp = bio->bio_buf;
1153                         KKASSERT((bp->b_bufsize & HAMMER_BUFMASK) == 0);
1154                         /*
1155                         hammer_del_buffers(hmp, buf_offset,
1156                                            zone2_offset, bp->b_bufsize);
1157                         */
1158
1159                         /*
1160                          * Second level bio - cached zone2 offset.
1161                          *
1162                          * (We can put our bio_done function in either the
1163                          *  2nd or 3rd level).
1164                          */
1165                         nbio = push_bio(bio);
1166                         nbio->bio_offset = zone2_offset;
1167                         nbio->bio_done = hammer_io_direct_write_complete;
1168                         nbio->bio_caller_info1.ptr = record;
1169                         record->zone2_offset = zone2_offset;
1170                         record->flags |= HAMMER_RECF_DIRECT_IO |
1171                                          HAMMER_RECF_DIRECT_INVAL;
1172
1173                         /*
1174                          * Third level bio - raw offset specific to the
1175                          * correct volume.
1176                          */
1177                         zone2_offset &= HAMMER_OFF_SHORT_MASK;
1178                         nbio = push_bio(nbio);
1179                         nbio->bio_offset = volume->ondisk->vol_buf_beg +
1180                                            zone2_offset;
1181                         hammer_stats_disk_write += bp->b_bufsize;
1182                         vn_strategy(volume->devvp, nbio);
1183                 }
1184                 hammer_rel_volume(volume, 0);
1185         } else {
1186                 /* 
1187                  * Must fit in a standard HAMMER buffer.  In this case all
1188                  * consumers use the HAMMER buffer system and RECF_DIRECT_IO
1189                  * does not need to be set-up.
1190                  */
1191                 KKASSERT(((buf_offset ^ (buf_offset + leaf->data_len - 1)) & ~HAMMER_BUFMASK64) == 0);
1192                 buffer = NULL;
1193                 ptr = hammer_bread(hmp, buf_offset, &error, &buffer);
1194                 if (error == 0) {
1195                         bp = bio->bio_buf;
1196                         bp->b_flags |= B_AGE;
1197                         hammer_io_modify(&buffer->io, 1);
1198                         bcopy(bp->b_data, ptr, leaf->data_len);
1199                         hammer_io_modify_done(&buffer->io);
1200                         hammer_rel_buffer(buffer, 0);
1201                         bp->b_resid = 0;
1202                         biodone(bio);
1203                 }
1204         }
1205         if (error == 0) {
1206                 /*
1207                  * The record is all setup now, add it.  Potential conflics
1208                  * have already been dealt with.
1209                  */
1210                 error = hammer_mem_add(record);
1211                 KKASSERT(error == 0);
1212         } else {
1213                 /*
1214                  * Major suckage occured.
1215                  */
1216                 kprintf("hammer_direct_write: failed @ %016llx\n",
1217                         leaf->data_offset);
1218                 bp = bio->bio_buf;
1219                 bp->b_resid = 0;
1220                 bp->b_error = EIO;
1221                 bp->b_flags |= B_ERROR;
1222                 biodone(bio);
1223                 record->flags |= HAMMER_RECF_DELETED_FE;
1224                 hammer_rel_mem_record(record);
1225         }
1226         return(error);
1227 }
1228
1229 /*
1230  * On completion of the BIO this callback must disconnect
1231  * it from the hammer_record and chain to the previous bio.
1232  *
1233  * An I/O error forces the mount to read-only.  Data buffers
1234  * are not B_LOCKED like meta-data buffers are, so we have to
1235  * throw the buffer away to prevent the kernel from retrying.
1236  */
1237 static
1238 void
1239 hammer_io_direct_write_complete(struct bio *nbio)
1240 {
1241         struct bio *obio;
1242         struct buf *bp;
1243         hammer_record_t record = nbio->bio_caller_info1.ptr;
1244
1245         bp = nbio->bio_buf;
1246         obio = pop_bio(nbio);
1247         if (bp->b_flags & B_ERROR) {
1248                 hammer_critical_error(record->ip->hmp, record->ip,
1249                                       bp->b_error,
1250                                       "while writing bulk data");
1251                 bp->b_flags |= B_INVAL;
1252         }
1253         biodone(obio);
1254
1255         KKASSERT(record != NULL);
1256         KKASSERT(record->flags & HAMMER_RECF_DIRECT_IO);
1257         record->flags &= ~HAMMER_RECF_DIRECT_IO;
1258         if (record->flags & HAMMER_RECF_DIRECT_WAIT) {
1259                 record->flags &= ~HAMMER_RECF_DIRECT_WAIT;
1260                 wakeup(&record->flags);
1261         }
1262 }
1263
1264
1265 /*
1266  * This is called before a record is either committed to the B-Tree
1267  * or destroyed, to resolve any associated direct-IO. 
1268  *
1269  * (1) We must wait for any direct-IO related to the record to complete.
1270  *
1271  * (2) We must remove any buffer cache aliases for data accessed via
1272  *     leaf->data_offset or zone2_offset so non-direct-IO consumers  
1273  *     (the mirroring and reblocking code) do not see stale data.
1274  */
1275 void
1276 hammer_io_direct_wait(hammer_record_t record)
1277 {
1278         /*
1279          * Wait for I/O to complete
1280          */
1281         if (record->flags & HAMMER_RECF_DIRECT_IO) {
1282                 crit_enter();
1283                 while (record->flags & HAMMER_RECF_DIRECT_IO) {
1284                         record->flags |= HAMMER_RECF_DIRECT_WAIT;
1285                         tsleep(&record->flags, 0, "hmdiow", 0);
1286                 }
1287                 crit_exit();
1288         }
1289
1290         /*
1291          * Invalidate any related buffer cache aliases.
1292          */
1293         if (record->flags & HAMMER_RECF_DIRECT_INVAL) {
1294                 KKASSERT(record->leaf.data_offset);
1295                 hammer_del_buffers(record->ip->hmp,
1296                                    record->leaf.data_offset,
1297                                    record->zone2_offset,
1298                                    record->leaf.data_len);
1299                 record->flags &= ~HAMMER_RECF_DIRECT_INVAL;
1300         }
1301 }
1302
1303 /*
1304  * This is called to remove the second-level cached zone-2 offset from
1305  * frontend buffer cache buffers, now stale due to a data relocation.
1306  * These offsets are generated by cluster_read() via VOP_BMAP, or directly
1307  * by hammer_vop_strategy_read().
1308  *
1309  * This is rather nasty because here we have something like the reblocker
1310  * scanning the raw B-Tree with no held references on anything, really,
1311  * other then a shared lock on the B-Tree node, and we have to access the
1312  * frontend's buffer cache to check for and clean out the association.
1313  * Specifically, if the reblocker is moving data on the disk, these cached
1314  * offsets will become invalid.
1315  *
1316  * Only data record types associated with the large-data zone are subject
1317  * to direct-io and need to be checked.
1318  *
1319  */
1320 void
1321 hammer_io_direct_uncache(hammer_mount_t hmp, hammer_btree_leaf_elm_t leaf)
1322 {
1323         struct hammer_inode_info iinfo;
1324         int zone;
1325
1326         if (leaf->base.rec_type != HAMMER_RECTYPE_DATA)
1327                 return;
1328         zone = HAMMER_ZONE_DECODE(leaf->data_offset);
1329         if (zone != HAMMER_ZONE_LARGE_DATA_INDEX)
1330                 return;
1331         iinfo.obj_id = leaf->base.obj_id;
1332         iinfo.obj_asof = 0;     /* unused */
1333         iinfo.obj_localization = leaf->base.localization &
1334                                  HAMMER_LOCALIZE_PSEUDOFS_MASK;
1335         iinfo.u.leaf = leaf;
1336         hammer_scan_inode_snapshots(hmp, &iinfo,
1337                                     hammer_io_direct_uncache_callback,
1338                                     leaf);
1339 }
1340
1341 static int
1342 hammer_io_direct_uncache_callback(hammer_inode_t ip, void *data)
1343 {
1344         hammer_inode_info_t iinfo = data;
1345         hammer_off_t data_offset;
1346         hammer_off_t file_offset;
1347         struct vnode *vp;
1348         struct buf *bp;
1349         int blksize;
1350
1351         if (ip->vp == NULL)
1352                 return(0);
1353         data_offset = iinfo->u.leaf->data_offset;
1354         file_offset = iinfo->u.leaf->base.key - iinfo->u.leaf->data_len;
1355         blksize = iinfo->u.leaf->data_len;
1356         KKASSERT((blksize & HAMMER_BUFMASK) == 0);
1357
1358         hammer_ref(&ip->lock);
1359         if (hammer_get_vnode(ip, &vp) == 0) {
1360                 if ((bp = findblk(ip->vp, file_offset)) != NULL &&
1361                     bp->b_bio2.bio_offset != NOOFFSET) {
1362                         bp = getblk(ip->vp, file_offset, blksize, 0, 0);
1363                         bp->b_bio2.bio_offset = NOOFFSET;
1364                         brelse(bp);
1365                 }
1366                 vput(vp);
1367         }
1368         hammer_rel_inode(ip, 0);
1369         return(0);
1370 }
1371