HAMMER 12/many - buffer cache sync, buffer cache interactions, misc fixes.
[dragonfly.git] / sys / vfs / hammer / hammer_inode.c
1 /*
2  * Copyright (c) 2007 The DragonFly Project.  All rights reserved.
3  * 
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  * 
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  * 
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  * 
34  * $DragonFly: src/sys/vfs/hammer/hammer_inode.c,v 1.14 2007/12/30 08:49:20 dillon Exp $
35  */
36
37 #include "hammer.h"
38 #include <sys/buf.h>
39 #include <sys/buf2.h>
40
41 int
42 hammer_vop_inactive(struct vop_inactive_args *ap)
43 {
44         struct hammer_inode *ip = VTOI(ap->a_vp);
45
46         /*
47          * Degenerate case
48          */
49         if (ip == NULL) {
50                 vrecycle(ap->a_vp);
51                 return(0);
52         }
53
54         /*
55          * If the inode no longer has any references we recover its
56          * in-memory resources immediately.
57          */
58         if (ip->ino_rec.ino_nlinks == 0 &&
59             (ip->hmp->mp->mnt_flag & MNT_RDONLY) == 0) {
60                 hammer_sync_inode(ip, MNT_NOWAIT, 1);
61         }
62         return(0);
63 }
64
65 int
66 hammer_vop_reclaim(struct vop_reclaim_args *ap)
67 {
68         struct hammer_inode *ip;
69         struct vnode *vp;
70
71         vp = ap->a_vp;
72
73         /*
74          * Release the vnode association and ask that the inode be flushed.
75          */
76         if ((ip = vp->v_data) != NULL) {
77                 vp->v_data = NULL;
78                 ip->vp = NULL;
79                 hammer_rel_inode(ip, 1);
80         }
81         return(0);
82 }
83
84 /*
85  * Obtain a vnode for the specified inode number.  An exclusively locked
86  * vnode is returned.
87  */
88 int
89 hammer_vfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
90 {
91         struct hammer_mount *hmp = (void *)mp->mnt_data;
92         struct hammer_inode *ip;
93         int error;
94
95         /*
96          * Get/allocate the hammer_inode structure.  The structure must be
97          * unlocked while we manipulate the related vnode to avoid a
98          * deadlock.
99          */
100         ip = hammer_get_inode(hmp, ino, hmp->asof, &error);
101         if (ip == NULL) {
102                 *vpp = NULL;
103                 return(error);
104         }
105         error = hammer_get_vnode(ip, LK_EXCLUSIVE, vpp);
106         hammer_rel_inode(ip, 0);
107         return (error);
108 }
109
110 /*
111  * Return a locked vnode for the specified inode.  The inode must be
112  * referenced but NOT LOCKED on entry and will remain referenced on
113  * return.
114  */
115 int
116 hammer_get_vnode(struct hammer_inode *ip, int lktype, struct vnode **vpp)
117 {
118         struct vnode *vp;
119         int error = 0;
120
121         for (;;) {
122                 if ((vp = ip->vp) == NULL) {
123                         error = getnewvnode(VT_HAMMER, ip->hmp->mp, vpp, 0, 0);
124                         if (error)
125                                 break;
126                         hammer_lock_ex(&ip->lock);
127                         if (ip->vp != NULL) {
128                                 hammer_unlock(&ip->lock);
129                                 vp->v_type = VBAD;
130                                 vx_put(vp);
131                                 continue;
132                         }
133                         hammer_ref(&ip->lock);
134                         vp = *vpp;
135                         ip->vp = vp;
136                         vp->v_type = hammer_get_vnode_type(
137                                             ip->ino_rec.base.base.obj_type);
138
139                         switch(ip->ino_rec.base.base.obj_type) {
140                         case HAMMER_OBJTYPE_CDEV:
141                         case HAMMER_OBJTYPE_BDEV:
142                                 vp->v_ops = &ip->hmp->mp->mnt_vn_spec_ops;
143                                 addaliasu(vp, ip->ino_data.rmajor,
144                                           ip->ino_data.rminor);
145                                 break;
146                         case HAMMER_OBJTYPE_FIFO:
147                                 vp->v_ops = &ip->hmp->mp->mnt_vn_fifo_ops;
148                                 break;
149                         default:
150                                 break;
151                         }
152                         if (ip->obj_id == HAMMER_OBJID_ROOT)
153                                 vp->v_flag |= VROOT;
154
155                         vp->v_data = (void *)ip;
156                         /* vnode locked by getnewvnode() */
157                         /* make related vnode dirty if inode dirty? */
158                         hammer_unlock(&ip->lock);
159                         if (vp->v_type == VREG)
160                                 vinitvmio(vp, ip->ino_rec.ino_size);
161                         break;
162                 }
163
164                 /*
165                  * loop if the vget fails (aka races), or if the vp
166                  * no longer matches ip->vp.
167                  */
168                 if (vget(vp, LK_EXCLUSIVE) == 0) {
169                         if (vp == ip->vp)
170                                 break;
171                         vput(vp);
172                 }
173         }
174         *vpp = vp;
175         return(error);
176 }
177
178 /*
179  * Acquire a HAMMER inode.  The returned inode is not locked.  These functions
180  * do not attach or detach the related vnode (use hammer_get_vnode() for
181  * that).
182  */
183 struct hammer_inode *
184 hammer_get_inode(struct hammer_mount *hmp, u_int64_t obj_id, hammer_tid_t asof,
185                  int *errorp)
186 {
187         struct hammer_inode_info iinfo;
188         struct hammer_cursor cursor;
189         struct hammer_inode *ip;
190
191         /*
192          * Determine if we already have an inode cached.  If we do then
193          * we are golden.
194          */
195         iinfo.obj_id = obj_id;
196         iinfo.obj_asof = asof;
197 loop:
198         ip = hammer_ino_rb_tree_RB_LOOKUP_INFO(&hmp->rb_inos_root, &iinfo);
199         if (ip) {
200                 hammer_ref(&ip->lock);
201                 *errorp = 0;
202                 return(ip);
203         }
204
205         ip = kmalloc(sizeof(*ip), M_HAMMER, M_WAITOK|M_ZERO);
206         ip->obj_id = obj_id;
207         ip->obj_asof = iinfo.obj_asof;
208         ip->hmp = hmp;
209         RB_INIT(&ip->rec_tree);
210
211         /*
212          * Locate the on-disk inode.
213          * If we do not have an inode cached search the HAMMER on-disk B-Tree
214          * for it.
215          */
216
217         hammer_init_cursor_hmp(&cursor, hmp);
218         cursor.key_beg.obj_id = ip->obj_id;
219         cursor.key_beg.key = 0;
220         cursor.key_beg.create_tid = iinfo.obj_asof;
221         cursor.key_beg.delete_tid = 0;
222         cursor.key_beg.rec_type = HAMMER_RECTYPE_INODE;
223         cursor.key_beg.obj_type = 0;
224         cursor.flags = HAMMER_CURSOR_GET_RECORD | HAMMER_CURSOR_GET_DATA;
225
226         *errorp = hammer_btree_lookup(&cursor);
227
228         /*
229          * On success the B-Tree lookup will hold the appropriate
230          * buffer cache buffers and provide a pointer to the requested
231          * information.  Copy the information to the in-memory inode.
232          */
233         if (*errorp == 0) {
234                 ip->ino_rec = cursor.record->inode;
235                 ip->ino_data = cursor.data->inode;
236         } else if (cursor.node) {
237                 hammer_cache_node(cursor.node, &ip->cache);
238         }
239         hammer_done_cursor(&cursor);
240
241         /*
242          * On success load the inode's record and data and insert the
243          * inode into the B-Tree.  It is possible to race another lookup
244          * insertion of the same inode so deal with that condition too.
245          */
246         if (*errorp == 0) {
247                 hammer_ref(&ip->lock);
248                 if (RB_INSERT(hammer_ino_rb_tree, &hmp->rb_inos_root, ip)) {
249                         hammer_uncache_node(&ip->cache);
250                         hammer_unref(&ip->lock);
251                         kfree(ip, M_HAMMER);
252                         goto loop;
253                 }
254                 ip->flags |= HAMMER_INODE_ONDISK;
255         } else {
256                 kfree(ip, M_HAMMER);
257                 ip = NULL;
258         }
259         return (ip);
260 }
261
262 /*
263  * Create a new filesystem object, returning the inode in *ipp.  The
264  * returned inode will be referenced but not locked.
265  *
266  * The inode is created in-memory and will be delay-synchronized to the
267  * disk.
268  */
269 int
270 hammer_create_inode(hammer_transaction_t trans, struct vattr *vap,
271                     struct ucred *cred, hammer_inode_t dip,
272                     struct hammer_inode **ipp)
273 {
274         hammer_mount_t hmp;
275         hammer_inode_t ip;
276         uid_t xuid;
277
278         hmp = trans->hmp;
279         ip = kmalloc(sizeof(*ip), M_HAMMER, M_WAITOK|M_ZERO);
280         ip->obj_id = hammer_alloc_tid(trans);
281         KKASSERT(ip->obj_id != 0);
282         ip->obj_asof = hmp->asof;
283         ip->hmp = hmp;
284         ip->flags = HAMMER_INODE_DDIRTY | HAMMER_INODE_RDIRTY |
285                     HAMMER_INODE_ITIMES;
286         ip->last_tid = trans->tid;
287
288         RB_INIT(&ip->rec_tree);
289
290         ip->ino_rec.ino_atime = trans->tid;
291         ip->ino_rec.ino_mtime = trans->tid;
292         ip->ino_rec.ino_size = 0;
293         ip->ino_rec.ino_nlinks = 0;
294         /* XXX */
295         ip->ino_rec.base.rec_id = hammer_alloc_recid(trans);
296         KKASSERT(ip->ino_rec.base.rec_id != 0);
297         ip->ino_rec.base.base.obj_id = ip->obj_id;
298         ip->ino_rec.base.base.key = 0;
299         ip->ino_rec.base.base.create_tid = trans->tid;
300         ip->ino_rec.base.base.delete_tid = 0;
301         ip->ino_rec.base.base.rec_type = HAMMER_RECTYPE_INODE;
302         ip->ino_rec.base.base.obj_type = hammer_get_obj_type(vap->va_type);
303
304         ip->ino_data.version = HAMMER_INODE_DATA_VERSION;
305         ip->ino_data.mode = vap->va_mode;
306         ip->ino_data.ctime = trans->tid;
307         ip->ino_data.parent_obj_id = (dip) ? dip->ino_rec.base.base.obj_id : 0;
308
309         switch(ip->ino_rec.base.base.obj_type) {
310         case HAMMER_OBJTYPE_CDEV:
311         case HAMMER_OBJTYPE_BDEV:
312                 ip->ino_data.rmajor = vap->va_rmajor;
313                 ip->ino_data.rminor = vap->va_rminor;
314                 break;
315         default:
316                 break;
317         }
318
319         /*
320          * Calculate default uid/gid and overwrite with information from
321          * the vap.
322          */
323         xuid = hammer_to_unix_xid(&dip->ino_data.uid);
324         ip->ino_data.gid = dip->ino_data.gid;
325         xuid = vop_helper_create_uid(hmp->mp, dip->ino_data.mode, xuid, cred,
326                                      &vap->va_mode);
327         ip->ino_data.mode = vap->va_mode;
328
329         if (vap->va_vaflags & VA_UID_UUID_VALID)
330                 ip->ino_data.uid = vap->va_uid_uuid;
331         else if (vap->va_uid != (uid_t)VNOVAL)
332                 hammer_guid_to_uuid(&ip->ino_data.uid, xuid);
333         if (vap->va_vaflags & VA_GID_UUID_VALID)
334                 ip->ino_data.gid = vap->va_gid_uuid;
335         else if (vap->va_gid != (gid_t)VNOVAL)
336                 hammer_guid_to_uuid(&ip->ino_data.gid, vap->va_gid);
337
338         hammer_ref(&ip->lock);
339         if (RB_INSERT(hammer_ino_rb_tree, &hmp->rb_inos_root, ip)) {
340                 hammer_unref(&ip->lock);
341                 panic("hammer_create_inode: duplicate obj_id %llx", ip->obj_id);
342         }
343         *ipp = ip;
344         return(0);
345 }
346
347 int
348 hammer_update_inode(hammer_inode_t ip)
349 {
350         struct hammer_cursor cursor;
351         struct hammer_cursor *spike = NULL;
352         hammer_record_t record;
353         int error;
354
355         /*
356          * Locate the record on-disk and mark it as deleted.  Both the B-Tree
357          * node and the record must be marked deleted.  The record may or
358          * may not be physically deleted, depending on the retention policy.
359          *
360          * If the inode has already been deleted on-disk we have nothing
361          * to do.
362          *
363          * XXX Update the inode record and data in-place if the retention
364          * policy allows it.
365          */
366 retry:
367         error = 0;
368
369         if ((ip->flags & (HAMMER_INODE_ONDISK|HAMMER_INODE_DELONDISK)) ==
370             HAMMER_INODE_ONDISK) {
371                 hammer_init_cursor_ip(&cursor, ip);
372                 cursor.key_beg.obj_id = ip->obj_id;
373                 cursor.key_beg.key = 0;
374                 cursor.key_beg.create_tid = ip->obj_asof;
375                 cursor.key_beg.delete_tid = 0;
376                 cursor.key_beg.rec_type = HAMMER_RECTYPE_INODE;
377                 cursor.key_beg.obj_type = 0;
378                 cursor.flags = HAMMER_CURSOR_GET_RECORD;
379
380                 error = hammer_btree_lookup(&cursor);
381
382                 if (error == 0) {
383                         error = hammer_ip_delete_record(&cursor, ip->last_tid);
384                         if (error == 0)
385                                 ip->flags |= HAMMER_INODE_DELONDISK;
386                 }
387                 hammer_cache_node(cursor.node, &ip->cache);
388                 hammer_done_cursor(&cursor);
389         }
390
391         /*
392          * Write out a new record if the in-memory inode is not marked
393          * as having been deleted.  Update our inode statistics if this
394          * is the first application of the inode on-disk.
395          *
396          * If the inode has been deleted permanently, HAMMER_INODE_DELONDISK
397          * will remain set and prevent further updates.
398          */
399         if (error == 0 && (ip->flags & HAMMER_INODE_DELETED) == 0) { 
400                 record = hammer_alloc_mem_record(ip);
401                 record->rec.inode = ip->ino_rec;
402                 record->rec.inode.base.base.create_tid = ip->last_tid;
403                 record->rec.inode.base.data_len = sizeof(ip->ino_data);
404                 record->data = (void *)&ip->ino_data;
405                 error = hammer_ip_sync_record(record, &spike);
406                 hammer_drop_mem_record(record, 1);
407                 if (error == ENOSPC) {
408                         error = hammer_spike(&spike);
409                         if (error == 0)
410                                 goto retry;
411                 }
412                 KKASSERT(spike == NULL);
413                 if (error == 0) {
414                         ip->flags &= ~(HAMMER_INODE_RDIRTY|HAMMER_INODE_DDIRTY|
415                                        HAMMER_INODE_DELONDISK);
416                         if ((ip->flags & HAMMER_INODE_ONDISK) == 0) {
417                                 hammer_modify_volume(ip->hmp->rootvol);
418                                 ++ip->hmp->rootvol->ondisk->vol0_stat_inodes;
419                                 hammer_modify_volume_done(ip->hmp->rootvol);
420                                 ip->flags |= HAMMER_INODE_ONDISK;
421                         }
422                 }
423         }
424         if (error == 0)
425                 ip->flags &= ~HAMMER_INODE_TID;
426         return(error);
427 }
428
429 /*
430  * Release a reference on an inode and unload it if told to flush.
431  */
432 void
433 hammer_rel_inode(struct hammer_inode *ip, int flush)
434 {
435         hammer_unref(&ip->lock);
436         if (flush || ip->ino_rec.ino_nlinks == 0)
437                 ip->flags |= HAMMER_INODE_FLUSH;
438         if (ip->lock.refs == 0 && (ip->flags & HAMMER_INODE_FLUSH))
439                 hammer_unload_inode(ip, NULL);
440 }
441
442 /*
443  * Unload and destroy the specified inode.
444  *
445  * (called via RB_SCAN)
446  */
447 int
448 hammer_unload_inode(struct hammer_inode *ip, void *data __unused)
449 {
450         int error;
451
452         KASSERT(ip->lock.refs == 0,
453                 ("hammer_unload_inode: %d refs\n", ip->lock.refs));
454         KKASSERT(ip->vp == NULL);
455         hammer_ref(&ip->lock);
456
457         error = hammer_sync_inode(ip, MNT_WAIT, 1);
458         if (error)
459                 kprintf("hammer_sync_inode failed error %d\n", error);
460
461         RB_REMOVE(hammer_ino_rb_tree, &ip->hmp->rb_inos_root, ip);
462
463         hammer_uncache_node(&ip->cache);
464         kfree(ip, M_HAMMER);
465         return(0);
466 }
467
468 /*
469  * A transaction has modified an inode, requiring a new record and possibly
470  * also data to be written out.
471  *
472  * last_tid is the TID to use for the disk sync.
473  */
474 void
475 hammer_modify_inode(struct hammer_transaction *trans,
476                     struct hammer_inode *ip, int flags)
477 {
478         if ((flags & HAMMER_INODE_TID) && (ip->flags & HAMMER_INODE_TID) == 0) {
479                 ip->last_tid = trans->tid;
480         }
481         ip->flags |= flags;
482 }
483
484 /*
485  * Sync any dirty buffers and records associated with an inode.  The
486  * inode's last_tid field is used as the transaction id for the sync,
487  * overriding any intermediate TIDs that were used for records.  Note
488  * that the dirty buffer cache buffers do not have any knowledge of
489  * the transaction id they were modified under.
490  *
491  * If we can't sync due to a cluster becoming full the spike structure
492  * will be filled in and ENOSPC returned.  We must return -ENOSPC to
493  * terminate the RB_SCAN.
494  */
495 static int
496 hammer_sync_inode_callback(hammer_record_t rec, void *data)
497 {
498         struct hammer_cursor **spike = data;
499         int error;
500
501         hammer_ref(&rec->lock);
502         hammer_lock_ex(&rec->lock);
503         if ((rec->flags & HAMMER_RECF_DELETED) == 0)
504                 error = hammer_ip_sync_record(rec, spike);
505         else
506                 error = 0;
507
508         if (error == ENOSPC) {
509                 hammer_drop_mem_record(rec, 0);
510                 return(-error);
511         }
512
513         if (error) {
514                 kprintf("hammer_sync_inode_callback: sync failed rec %p, error %d\n",
515                         rec, error);
516                 hammer_drop_mem_record(rec, 0);
517                 return(-error);
518         }
519         hammer_drop_mem_record(rec, 1); /* ref & lock eaten by call */
520         return(0);
521 }
522
523 /*
524  * XXX error handling
525  */
526 int
527 hammer_sync_inode(hammer_inode_t ip, int waitfor, int handle_delete)
528 {
529         struct hammer_transaction trans;
530         struct hammer_cursor *spike = NULL;
531         int error;
532
533         hammer_lock_ex(&ip->lock);
534         hammer_start_transaction(&trans, ip->hmp);
535
536         /*
537          * If the inode has been deleted (nlinks == 0), and the OS no longer
538          * has any references to it (handle_delete != 0), clean up in-memory
539          * data.
540          *
541          * NOTE: We do not set the RDIRTY flag when updating the delete_tid,
542          * setting HAMMER_INODE_DELETED takes care of it.
543          *
544          * NOTE: Because we may sync records within this new transaction,
545          * force the inode update later on to use our transaction id or
546          * the delete_tid of the inode may be less then the create_tid of
547          * the inode update.  XXX shouldn't happen but don't take the chance.
548          *
549          * NOTE: The call to hammer_ip_delete_range() cannot return ENOSPC
550          * so we can pass a NULL spike structure, because no partial data
551          * deletion can occur (yet).
552          */
553         if (ip->ino_rec.ino_nlinks == 0 && handle_delete) {
554                 if (ip->vp)
555                         vtruncbuf(ip->vp, 0, HAMMER_BUFSIZE);
556                 error = hammer_ip_delete_range_all(&trans, ip);
557                 KKASSERT(RB_EMPTY(&ip->rec_tree));
558                 ip->flags &= ~HAMMER_INODE_TID;
559                 ip->ino_rec.base.base.delete_tid = trans.tid;
560                 hammer_modify_inode(&trans, ip,
561                                     HAMMER_INODE_DELETED | HAMMER_INODE_TID);
562                 hammer_modify_volume(ip->hmp->rootvol);
563                 --ip->hmp->rootvol->ondisk->vol0_stat_inodes;
564                 hammer_modify_volume_done(ip->hmp->rootvol);
565         }
566
567         /*
568          * Sync the buffer cache
569          */
570         if (ip->vp != NULL)
571                 error = vfsync(ip->vp, waitfor, 1, NULL, NULL);
572         else
573                 error = 0;
574
575         /*
576          * Now sync related records
577          */
578         for (;;) {
579                 error = RB_SCAN(hammer_rec_rb_tree, &ip->rec_tree, NULL,
580                                 hammer_sync_inode_callback, &spike);
581                 KKASSERT(error <= 0);
582                 if (error < 0)
583                         error = -error;
584                 if (error == ENOSPC) {
585                         error = hammer_spike(&spike);
586                         if (error == 0)
587                                 continue;
588                 }
589                 break;
590         }
591
592         /*
593          * Now update the inode's on-disk inode-data and/or on-disk record.
594          */
595         switch(ip->flags & (HAMMER_INODE_DELETED|HAMMER_INODE_ONDISK)) {
596         case HAMMER_INODE_DELETED|HAMMER_INODE_ONDISK:
597                 /*
598                  * If deleted and on-disk, don't set any additional flags.
599                  * the delete flag takes care of things.
600                  */
601                 break;
602         case HAMMER_INODE_DELETED:
603                 /*
604                  * Take care of the case where a deleted inode was never
605                  * flushed to the disk in the first place.
606                  */
607                 ip->flags &= ~(HAMMER_INODE_RDIRTY|HAMMER_INODE_DDIRTY);
608                 while (RB_ROOT(&ip->rec_tree)) {
609                         hammer_record_t rec = RB_ROOT(&ip->rec_tree);
610                         hammer_ref(&rec->lock);
611                         hammer_lock_ex(&rec->lock);
612                         hammer_drop_mem_record(rec, 1);
613                 }
614                 break;
615         case HAMMER_INODE_ONDISK:
616                 /*
617                  * If already on-disk, do not set any additional flags.
618                  */
619                 break;
620         default:
621                 /*
622                  * If not on-disk and not deleted, set both dirty flags
623                  * to force an initial record to be written.
624                  */
625                 ip->flags |= HAMMER_INODE_RDIRTY | HAMMER_INODE_DDIRTY;
626                 break;
627         }
628
629         /*
630          * If RDIRTY or DDIRTY is set, write out a new record.  If the
631          * inode is already on-disk, the old record is marked as deleted.
632          */
633         if (ip->flags & (HAMMER_INODE_RDIRTY | HAMMER_INODE_DDIRTY |
634                          HAMMER_INODE_DELETED)) {
635                 error = hammer_update_inode(ip);
636         }
637         hammer_commit_transaction(&trans);
638         hammer_unlock(&ip->lock);
639         return(error);
640 }
641
642 /*
643  * Access the filesystem buffer containing the cluster-relative byte
644  * offset, validate the buffer type, load *bufferp and return a
645  * pointer to the requested data.  The buffer is reference and locked on
646  * return.
647  *
648  * If buf_type is 0 the buffer is assumed to be a pure-data buffer and
649  * no type or crc check is performed.
650  *
651  * If *bufferp is not NULL on entry it is assumed to contain a locked
652  * and referenced buffer which will then be replaced.
653  *
654  * If the caller is holding another unrelated buffer locked it must be
655  * passed in reorderbuf so we can properly order buffer locks.
656  *
657  * XXX add a flag for the buffer type and check the CRC here XXX
658  */
659 void *
660 hammer_bread(hammer_cluster_t cluster, int32_t cloff,
661              u_int64_t buf_type, int *errorp,
662              struct hammer_buffer **bufferp)
663 {
664         hammer_buffer_t buffer;
665         int32_t buf_no;
666         int32_t buf_off;
667
668         /*
669          * Load the correct filesystem buffer, replacing *bufferp.
670          */
671         buf_no = cloff / HAMMER_BUFSIZE;
672         buffer = *bufferp;
673         if (buffer == NULL || buffer->cluster != cluster ||
674             buffer->buf_no != buf_no) {
675                 if (buffer) {
676                         /*hammer_unlock(&buffer->io.lock);*/
677                         hammer_rel_buffer(buffer, 0);
678                 }
679                 buffer = hammer_get_buffer(cluster, buf_no, 0, errorp);
680                 *bufferp = buffer;
681                 if (buffer == NULL)
682                         return(NULL);
683                 /*hammer_lock_ex(&buffer->io.lock);*/
684         }
685
686         /*
687          * Validate the buffer type
688          */
689         buf_off = cloff & HAMMER_BUFMASK;
690         if (buf_type) {
691                 if (buf_type != buffer->ondisk->head.buf_type) {
692                         kprintf("BUFFER HEAD TYPE MISMATCH %llx %llx\n",
693                                 buf_type, buffer->ondisk->head.buf_type);
694                         *errorp = EIO;
695                         return(NULL);
696                 }
697                 if (buf_off < sizeof(buffer->ondisk->head)) {
698                         kprintf("BUFFER OFFSET TOO LOW %d\n", buf_off);
699                         *errorp = EIO;
700                         return(NULL);
701                 }
702         }
703
704         /*
705          * Return a pointer to the buffer data.
706          */
707         *errorp = 0;
708         return((char *)buffer->ondisk + buf_off);
709 }
710