Merge from vendor branch FILE:
[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.22 2008/01/18 07:02:41 dillon Exp $
35  */
36
37 #include "hammer.h"
38 #include <sys/buf.h>
39 #include <sys/buf2.h>
40
41 /*
42  * The kernel is not actively referencing this vnode but is still holding
43  * it cached.
44  */
45 int
46 hammer_vop_inactive(struct vop_inactive_args *ap)
47 {
48         struct hammer_inode *ip = VTOI(ap->a_vp);
49
50         /*
51          * Degenerate case
52          */
53         if (ip == NULL) {
54                 vrecycle(ap->a_vp);
55                 return(0);
56         }
57
58         /*
59          * If the inode no longer has any references we recover its
60          * in-memory resources immediately.
61          */
62         if (ip->ino_rec.ino_nlinks == 0)
63                 vrecycle(ap->a_vp);
64         return(0);
65 }
66
67 /*
68  * Release the vnode association.  This is typically (but not always)
69  * the last reference on the inode and will flush the inode to the
70  * buffer cache.
71  *
72  * XXX Currently our sync code only runs through inodes with vnode
73  * associations, so we depend on hammer_rel_inode() to sync any inode
74  * record data to the block device prior to losing the association.
75  * Otherwise transactions that the user expected to be distinct by
76  * doing a manual sync may be merged.
77  */
78 int
79 hammer_vop_reclaim(struct vop_reclaim_args *ap)
80 {
81         struct hammer_inode *ip;
82         struct vnode *vp;
83
84         vp = ap->a_vp;
85
86         if ((ip = vp->v_data) != NULL) {
87                 vp->v_data = NULL;
88                 ip->vp = NULL;
89                 hammer_rel_inode(ip, 0);
90         }
91         return(0);
92 }
93
94 /*
95  * Obtain a vnode for the specified inode number.  An exclusively locked
96  * vnode is returned.
97  */
98 int
99 hammer_vfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
100 {
101         struct hammer_mount *hmp = (void *)mp->mnt_data;
102         struct hammer_inode *ip;
103         int error;
104
105         /*
106          * Get/allocate the hammer_inode structure.  The structure must be
107          * unlocked while we manipulate the related vnode to avoid a
108          * deadlock.
109          */
110         ip = hammer_get_inode(hmp, NULL, ino, hmp->asof, 0, &error);
111         if (ip == NULL) {
112                 *vpp = NULL;
113                 return(error);
114         }
115         error = hammer_get_vnode(ip, LK_EXCLUSIVE, vpp);
116         hammer_rel_inode(ip, 0);
117         return (error);
118 }
119
120 /*
121  * Return a locked vnode for the specified inode.  The inode must be
122  * referenced but NOT LOCKED on entry and will remain referenced on
123  * return.
124  */
125 int
126 hammer_get_vnode(struct hammer_inode *ip, int lktype, struct vnode **vpp)
127 {
128         struct vnode *vp;
129         int error = 0;
130
131         for (;;) {
132                 if ((vp = ip->vp) == NULL) {
133                         error = getnewvnode(VT_HAMMER, ip->hmp->mp, vpp, 0, 0);
134                         if (error)
135                                 break;
136                         hammer_lock_ex(&ip->lock);
137                         if (ip->vp != NULL) {
138                                 hammer_unlock(&ip->lock);
139                                 vp->v_type = VBAD;
140                                 vx_put(vp);
141                                 continue;
142                         }
143                         hammer_ref(&ip->lock);
144                         vp = *vpp;
145                         ip->vp = vp;
146                         vp->v_type = hammer_get_vnode_type(
147                                             ip->ino_rec.base.base.obj_type);
148
149                         switch(ip->ino_rec.base.base.obj_type) {
150                         case HAMMER_OBJTYPE_CDEV:
151                         case HAMMER_OBJTYPE_BDEV:
152                                 vp->v_ops = &ip->hmp->mp->mnt_vn_spec_ops;
153                                 addaliasu(vp, ip->ino_data.rmajor,
154                                           ip->ino_data.rminor);
155                                 break;
156                         case HAMMER_OBJTYPE_FIFO:
157                                 vp->v_ops = &ip->hmp->mp->mnt_vn_fifo_ops;
158                                 break;
159                         default:
160                                 break;
161                         }
162                         if (ip->obj_id == HAMMER_OBJID_ROOT)
163                                 vp->v_flag |= VROOT;
164
165                         vp->v_data = (void *)ip;
166                         /* vnode locked by getnewvnode() */
167                         /* make related vnode dirty if inode dirty? */
168                         hammer_unlock(&ip->lock);
169                         if (vp->v_type == VREG)
170                                 vinitvmio(vp, ip->ino_rec.ino_size);
171                         break;
172                 }
173
174                 /*
175                  * loop if the vget fails (aka races), or if the vp
176                  * no longer matches ip->vp.
177                  */
178                 if (vget(vp, LK_EXCLUSIVE) == 0) {
179                         if (vp == ip->vp)
180                                 break;
181                         vput(vp);
182                 }
183         }
184         *vpp = vp;
185         return(error);
186 }
187
188 /*
189  * Acquire a HAMMER inode.  The returned inode is not locked.  These functions
190  * do not attach or detach the related vnode (use hammer_get_vnode() for
191  * that).
192  *
193  * The flags argument is only applied for newly created inodes, and only
194  * certain flags are inherited.
195  */
196 struct hammer_inode *
197 hammer_get_inode(struct hammer_mount *hmp, struct hammer_node **cache,
198                  u_int64_t obj_id, hammer_tid_t asof, int flags, int *errorp)
199 {
200         struct hammer_inode_info iinfo;
201         struct hammer_cursor cursor;
202         struct hammer_inode *ip;
203
204         /*
205          * Determine if we already have an inode cached.  If we do then
206          * we are golden.
207          */
208         iinfo.obj_id = obj_id;
209         iinfo.obj_asof = asof;
210 loop:
211         ip = hammer_ino_rb_tree_RB_LOOKUP_INFO(&hmp->rb_inos_root, &iinfo);
212         if (ip) {
213                 hammer_ref(&ip->lock);
214                 *errorp = 0;
215                 return(ip);
216         }
217
218         ip = kmalloc(sizeof(*ip), M_HAMMER, M_WAITOK|M_ZERO);
219         ++hammer_count_inodes;
220         ip->obj_id = obj_id;
221         ip->obj_asof = iinfo.obj_asof;
222         ip->hmp = hmp;
223         ip->flags = flags & HAMMER_INODE_RO;
224         if (hmp->ronly)
225                 ip->flags |= HAMMER_INODE_RO;
226         RB_INIT(&ip->rec_tree);
227
228         /*
229          * Locate the on-disk inode.
230          */
231 retry:
232         hammer_init_cursor_hmp(&cursor, cache, hmp);
233         cursor.key_beg.obj_id = ip->obj_id;
234         cursor.key_beg.key = 0;
235         cursor.key_beg.create_tid = 0;
236         cursor.key_beg.delete_tid = 0;
237         cursor.key_beg.rec_type = HAMMER_RECTYPE_INODE;
238         cursor.key_beg.obj_type = 0;
239         cursor.asof = iinfo.obj_asof;
240         cursor.flags = HAMMER_CURSOR_GET_RECORD | HAMMER_CURSOR_GET_DATA |
241                        HAMMER_CURSOR_ASOF;
242
243         *errorp = hammer_btree_lookup(&cursor);
244         if (*errorp == EDEADLK) {
245                 hammer_done_cursor(&cursor);
246                 goto retry;
247         }
248
249         /*
250          * On success the B-Tree lookup will hold the appropriate
251          * buffer cache buffers and provide a pointer to the requested
252          * information.  Copy the information to the in-memory inode
253          * and cache the B-Tree node to improve future operations.
254          */
255         if (*errorp == 0) {
256                 ip->ino_rec = cursor.record->inode;
257                 ip->ino_data = cursor.data->inode;
258                 hammer_cache_node(cursor.node, &ip->cache[0]);
259                 if (cache)
260                         hammer_cache_node(cursor.node, cache);
261         }
262
263         /*
264          * On success load the inode's record and data and insert the
265          * inode into the B-Tree.  It is possible to race another lookup
266          * insertion of the same inode so deal with that condition too.
267          *
268          * The cursor's locked node interlocks against others creating and
269          * destroying ip while we were blocked.
270          */
271         if (*errorp == 0) {
272                 hammer_ref(&ip->lock);
273                 if (RB_INSERT(hammer_ino_rb_tree, &hmp->rb_inos_root, ip)) {
274                         hammer_uncache_node(&ip->cache[0]);
275                         hammer_uncache_node(&ip->cache[1]);
276                         hammer_unref(&ip->lock);
277                         --hammer_count_inodes;
278                         kfree(ip, M_HAMMER);
279                         hammer_done_cursor(&cursor);
280                         goto loop;
281                 }
282                 ip->flags |= HAMMER_INODE_ONDISK;
283         } else {
284                 --hammer_count_inodes;
285                 kfree(ip, M_HAMMER);
286                 ip = NULL;
287         }
288         hammer_done_cursor(&cursor);
289         return (ip);
290 }
291
292 /*
293  * Create a new filesystem object, returning the inode in *ipp.  The
294  * returned inode will be referenced but not locked.
295  *
296  * The inode is created in-memory and will be delay-synchronized to the
297  * disk.
298  */
299 int
300 hammer_create_inode(hammer_transaction_t trans, struct vattr *vap,
301                     struct ucred *cred, hammer_inode_t dip,
302                     struct hammer_inode **ipp)
303 {
304         hammer_mount_t hmp;
305         hammer_inode_t ip;
306         uid_t xuid;
307
308         hmp = trans->hmp;
309         ip = kmalloc(sizeof(*ip), M_HAMMER, M_WAITOK|M_ZERO);
310         ++hammer_count_inodes;
311         ip->obj_id = hammer_alloc_tid(trans);
312         KKASSERT(ip->obj_id != 0);
313         ip->obj_asof = hmp->asof;
314         ip->hmp = hmp;
315         ip->flags = HAMMER_INODE_DDIRTY | HAMMER_INODE_RDIRTY |
316                     HAMMER_INODE_ITIMES;
317         ip->last_tid = trans->tid;
318
319         RB_INIT(&ip->rec_tree);
320
321         ip->ino_rec.ino_atime = trans->tid;
322         ip->ino_rec.ino_mtime = trans->tid;
323         ip->ino_rec.ino_size = 0;
324         ip->ino_rec.ino_nlinks = 0;
325         /* XXX */
326         ip->ino_rec.base.rec_id = hammer_alloc_recid(trans);
327         KKASSERT(ip->ino_rec.base.rec_id != 0);
328         ip->ino_rec.base.base.btype = HAMMER_BTREE_TYPE_RECORD;
329         ip->ino_rec.base.base.obj_id = ip->obj_id;
330         ip->ino_rec.base.base.key = 0;
331         ip->ino_rec.base.base.create_tid = trans->tid;
332         ip->ino_rec.base.base.delete_tid = 0;
333         ip->ino_rec.base.base.rec_type = HAMMER_RECTYPE_INODE;
334         ip->ino_rec.base.base.obj_type = hammer_get_obj_type(vap->va_type);
335
336         ip->ino_data.version = HAMMER_INODE_DATA_VERSION;
337         ip->ino_data.mode = vap->va_mode;
338         ip->ino_data.ctime = trans->tid;
339         ip->ino_data.parent_obj_id = (dip) ? dip->ino_rec.base.base.obj_id : 0;
340
341         switch(ip->ino_rec.base.base.obj_type) {
342         case HAMMER_OBJTYPE_CDEV:
343         case HAMMER_OBJTYPE_BDEV:
344                 ip->ino_data.rmajor = vap->va_rmajor;
345                 ip->ino_data.rminor = vap->va_rminor;
346                 break;
347         default:
348                 break;
349         }
350
351         /*
352          * Calculate default uid/gid and overwrite with information from
353          * the vap.
354          */
355         xuid = hammer_to_unix_xid(&dip->ino_data.uid);
356         ip->ino_data.gid = dip->ino_data.gid;
357         xuid = vop_helper_create_uid(hmp->mp, dip->ino_data.mode, xuid, cred,
358                                      &vap->va_mode);
359         ip->ino_data.mode = vap->va_mode;
360
361         if (vap->va_vaflags & VA_UID_UUID_VALID)
362                 ip->ino_data.uid = vap->va_uid_uuid;
363         else if (vap->va_uid != (uid_t)VNOVAL)
364                 hammer_guid_to_uuid(&ip->ino_data.uid, xuid);
365         if (vap->va_vaflags & VA_GID_UUID_VALID)
366                 ip->ino_data.gid = vap->va_gid_uuid;
367         else if (vap->va_gid != (gid_t)VNOVAL)
368                 hammer_guid_to_uuid(&ip->ino_data.gid, vap->va_gid);
369
370         hammer_ref(&ip->lock);
371         if (RB_INSERT(hammer_ino_rb_tree, &hmp->rb_inos_root, ip)) {
372                 hammer_unref(&ip->lock);
373                 panic("hammer_create_inode: duplicate obj_id %llx", ip->obj_id);
374         }
375         *ipp = ip;
376         return(0);
377 }
378
379 /*
380  * Called by hammer_sync_inode().
381  */
382 static int
383 hammer_update_inode(hammer_inode_t ip)
384 {
385         struct hammer_cursor cursor;
386         struct hammer_cursor *spike = NULL;
387         hammer_record_t record;
388         int error;
389         hammer_tid_t last_tid;
390
391         /*
392          * Locate the record on-disk and mark it as deleted.  Both the B-Tree
393          * node and the record must be marked deleted.  The record may or
394          * may not be physically deleted, depending on the retention policy.
395          *
396          * If the inode has already been deleted on-disk we have nothing
397          * to do.
398          *
399          * XXX Update the inode record and data in-place if the retention
400          * policy allows it.
401          */
402         last_tid = ip->last_tid;
403 retry:
404         error = 0;
405
406         if ((ip->flags & (HAMMER_INODE_ONDISK|HAMMER_INODE_DELONDISK)) ==
407             HAMMER_INODE_ONDISK) {
408                 hammer_init_cursor_hmp(&cursor, &ip->cache[0], ip->hmp);
409                 cursor.key_beg.obj_id = ip->obj_id;
410                 cursor.key_beg.key = 0;
411                 cursor.key_beg.create_tid = 0;
412                 cursor.key_beg.delete_tid = 0;
413                 cursor.key_beg.rec_type = HAMMER_RECTYPE_INODE;
414                 cursor.key_beg.obj_type = 0;
415                 cursor.asof = ip->obj_asof;
416                 cursor.flags |= HAMMER_CURSOR_GET_RECORD | HAMMER_CURSOR_ASOF;
417
418                 error = hammer_btree_lookup(&cursor);
419
420                 if (error == 0) {
421                         error = hammer_ip_delete_record(&cursor, last_tid);
422                         if (error == 0)
423                                 ip->flags |= HAMMER_INODE_DELONDISK;
424                         hammer_cache_node(cursor.node, &ip->cache[0]);
425                 }
426                 hammer_done_cursor(&cursor);
427                 if (error == EDEADLK)
428                         goto retry;
429         }
430
431         /*
432          * Write out a new record if the in-memory inode is not marked
433          * as having been deleted.  Update our inode statistics if this
434          * is the first application of the inode on-disk.
435          *
436          * If the inode has been deleted permanently, HAMMER_INODE_DELONDISK
437          * will remain set and prevent further updates.
438          */
439         if (error == 0 && (ip->flags & HAMMER_INODE_DELETED) == 0) { 
440                 record = hammer_alloc_mem_record(ip);
441                 record->rec.inode = ip->ino_rec;
442                 record->rec.inode.base.base.create_tid = last_tid;
443                 record->rec.inode.base.data_len = sizeof(ip->ino_data);
444                 record->data = (void *)&ip->ino_data;
445                 error = hammer_ip_sync_record(record, &spike);
446                 record->flags |= HAMMER_RECF_DELETED;
447                 hammer_rel_mem_record(record);
448                 if (error == ENOSPC) {
449                         error = hammer_spike(&spike);
450                         if (error == 0)
451                                 goto retry;
452                 }
453                 KKASSERT(spike == NULL);
454                 if (error == 0) {
455                         ip->flags &= ~(HAMMER_INODE_RDIRTY |
456                                        HAMMER_INODE_DDIRTY |
457                                        HAMMER_INODE_DELONDISK |
458                                        HAMMER_INODE_ITIMES);
459                         if ((ip->flags & HAMMER_INODE_ONDISK) == 0) {
460                                 hammer_modify_volume(ip->hmp->rootvol);
461                                 ++ip->hmp->rootvol->ondisk->vol0_stat_inodes;
462                                 ip->flags |= HAMMER_INODE_ONDISK;
463                         }
464                 }
465         }
466         return(error);
467 }
468
469 /*
470  * Update only the itimes fields.  This is done no-historically.  The
471  * record is updated in-place on the disk.
472  */
473 static int
474 hammer_update_itimes(hammer_inode_t ip)
475 {
476         struct hammer_cursor cursor;
477         struct hammer_inode_record *rec;
478         int error;
479
480 retry:
481         error = 0;
482         if ((ip->flags & (HAMMER_INODE_ONDISK|HAMMER_INODE_DELONDISK)) ==
483             HAMMER_INODE_ONDISK) {
484                 hammer_init_cursor_hmp(&cursor, &ip->cache[0], ip->hmp);
485                 cursor.key_beg.obj_id = ip->obj_id;
486                 cursor.key_beg.key = 0;
487                 cursor.key_beg.create_tid = 0;
488                 cursor.key_beg.delete_tid = 0;
489                 cursor.key_beg.rec_type = HAMMER_RECTYPE_INODE;
490                 cursor.key_beg.obj_type = 0;
491                 cursor.asof = ip->obj_asof;
492                 cursor.flags |= HAMMER_CURSOR_GET_RECORD | HAMMER_CURSOR_ASOF;
493
494                 error = hammer_btree_lookup(&cursor);
495                 if (error == 0) {
496                         rec = &cursor.record->inode;
497                         hammer_modify_buffer(cursor.record_buffer);
498                         rec->ino_atime = ip->ino_rec.ino_atime;
499                         rec->ino_mtime = ip->ino_rec.ino_mtime;
500                         ip->flags &= ~HAMMER_INODE_ITIMES;
501                         /* XXX recalculate crc */
502                         hammer_cache_node(cursor.node, &ip->cache[0]);
503                 }
504                 hammer_done_cursor(&cursor);
505                 if (error == EDEADLK)
506                         goto retry;
507         }
508         return(error);
509 }
510
511 /*
512  * Release a reference on an inode.  If asked to flush the last release
513  * will flush the inode.
514  */
515 void
516 hammer_rel_inode(struct hammer_inode *ip, int flush)
517 {
518         hammer_unref(&ip->lock);
519         if (flush)
520                 ip->flags |= HAMMER_INODE_FLUSH;
521         if (ip->lock.refs == 0) {
522                 if (ip->flags & HAMMER_INODE_FLUSH)
523                         hammer_unload_inode(ip, (void *)MNT_WAIT);
524                 else
525                         hammer_unload_inode(ip, (void *)MNT_NOWAIT);
526         }
527 }
528
529 /*
530  * Unload and destroy the specified inode.
531  *
532  * (called via RB_SCAN)
533  */
534 int
535 hammer_unload_inode(struct hammer_inode *ip, void *data)
536 {
537         int error;
538
539         KASSERT(ip->lock.refs == 0,
540                 ("hammer_unload_inode: %d refs\n", ip->lock.refs));
541         KKASSERT(ip->vp == NULL);
542         hammer_ref(&ip->lock);
543
544         error = hammer_sync_inode(ip, (int)data, 1);
545         if (error)
546                 kprintf("hammer_sync_inode failed error %d\n", error);
547         if (ip->lock.refs == 1) {
548                 KKASSERT(RB_EMPTY(&ip->rec_tree));
549                 RB_REMOVE(hammer_ino_rb_tree, &ip->hmp->rb_inos_root, ip);
550
551                 hammer_uncache_node(&ip->cache[0]);
552                 hammer_uncache_node(&ip->cache[1]);
553                 --hammer_count_inodes;
554                 kfree(ip, M_HAMMER);
555         } else {
556                 hammer_unref(&ip->lock);
557         }
558         return(0);
559 }
560
561 /*
562  * A transaction has modified an inode, requiring updates as specified by
563  * the passed flags.
564  *
565  * HAMMER_INODE_RDIRTY: Inode record has been updated
566  * HAMMER_INODE_DDIRTY: Inode data has been updated
567  * HAMMER_INODE_DELETED: Inode record/data must be deleted
568  * HAMMER_INODE_ITIMES: mtime/atime has been updated
569  *
570  * last_tid is the TID to use to generate the correct TID when the inode
571  * is synced to disk.
572  */
573 void
574 hammer_modify_inode(struct hammer_transaction *trans,
575                     struct hammer_inode *ip, int flags)
576 {
577         KKASSERT ((ip->flags & HAMMER_INODE_RO) == 0 ||
578                   (HAMMER_INODE_RDIRTY|HAMMER_INODE_DDIRTY|
579                    HAMMER_INODE_DELETED|HAMMER_INODE_ITIMES) == 0);
580
581         if (flags &
582             (HAMMER_INODE_RDIRTY|HAMMER_INODE_DDIRTY|HAMMER_INODE_DELETED)) {
583                 if (hammer_debug_tid) {
584                         kprintf("hammer_modify_inode: %016llx (%08x)\n", 
585                                 trans->tid, (int)(trans->tid / 1000000000LL));
586                 }
587                 ip->last_tid = trans->tid;
588         }
589         ip->flags |= flags;
590 }
591
592 /*
593  * Sync any dirty buffers and records associated with an inode.  The
594  * inode's last_tid field is used as the transaction id for the sync,
595  * overriding any intermediate TIDs that were used for records.  Note
596  * that the dirty buffer cache buffers do not have any knowledge of
597  * the transaction id they were modified under.
598  *
599  * If we can't sync due to a cluster becoming full the spike structure
600  * will be filled in and ENOSPC returned.  We must return -ENOSPC to
601  * terminate the RB_SCAN.
602  */
603 static int
604 hammer_sync_inode_callback(hammer_record_t rec, void *data)
605 {
606         struct hammer_cursor **spike = data;
607         int error;
608
609         hammer_ref(&rec->lock);
610         error = hammer_ip_sync_record(rec, spike);
611         hammer_rel_mem_record(rec);
612
613         if (error) {
614                 error = -error;
615                 if (error != -ENOSPC) {
616                         kprintf("hammer_sync_inode_callback: sync failed rec "
617                                 "%p, error %d\n", rec, error);
618                 }
619         }
620         return(error);
621 }
622
623 /*
624  * XXX error handling
625  */
626 int
627 hammer_sync_inode(hammer_inode_t ip, int waitfor, int handle_delete)
628 {
629         struct hammer_transaction trans;
630         struct hammer_cursor *spike = NULL;
631         int error;
632
633         if ((ip->flags & HAMMER_INODE_MODMASK) == 0) {
634                 return(0);
635         }
636
637         hammer_lock_ex(&ip->lock);
638
639         /*
640          * Use the transaction id of the last operation to sync.
641          */
642         if (ip->last_tid)
643                 hammer_start_transaction_tid(&trans, ip->hmp, ip->last_tid);
644         else
645                 hammer_start_transaction(&trans, ip->hmp);
646
647         /*
648          * If the inode has been deleted (nlinks == 0), and the OS no longer
649          * has any references to it (handle_delete != 0), clean up in-memory
650          * data.
651          *
652          * NOTE: We do not set the RDIRTY flag when updating the delete_tid,
653          * setting HAMMER_INODE_DELETED takes care of it.
654          *
655          * NOTE: Because we may sync records within this new transaction,
656          * force the inode update later on to use our transaction id or
657          * the delete_tid of the inode may be less then the create_tid of
658          * the inode update.  XXX shouldn't happen but don't take the chance.
659          *
660          * NOTE: The call to hammer_ip_delete_range() cannot return ENOSPC
661          * so we can pass a NULL spike structure, because no partial data
662          * deletion can occur (yet).
663          */
664         if (ip->ino_rec.ino_nlinks == 0 && handle_delete && 
665             (ip->flags & HAMMER_INODE_GONE) == 0) {
666                 ip->flags |= HAMMER_INODE_GONE;
667                 if (ip->vp)
668                         vtruncbuf(ip->vp, 0, HAMMER_BUFSIZE);
669                 error = hammer_ip_delete_range_all(&trans, ip);
670                 KKASSERT(RB_EMPTY(&ip->rec_tree));
671                 ip->ino_rec.base.base.delete_tid = trans.tid;
672                 hammer_modify_inode(&trans, ip, HAMMER_INODE_DELETED);
673                 hammer_modify_volume(ip->hmp->rootvol);
674                 --ip->hmp->rootvol->ondisk->vol0_stat_inodes;
675         }
676
677         /*
678          * Sync the buffer cache.
679          */
680         if (ip->vp != NULL) {
681                 error = vfsync(ip->vp, waitfor, 1, NULL, NULL);
682                 if (RB_ROOT(&ip->vp->v_rbdirty_tree) == NULL)
683                         ip->flags &= ~HAMMER_INODE_BUFS;
684         } else {
685                 error = 0;
686         }
687
688
689         /*
690          * Now sync related records
691          */
692         for (;;) {
693                 error = RB_SCAN(hammer_rec_rb_tree, &ip->rec_tree, NULL,
694                                 hammer_sync_inode_callback, &spike);
695                 KKASSERT(error <= 0);
696                 if (error < 0)
697                         error = -error;
698                 if (error == ENOSPC) {
699                         error = hammer_spike(&spike);
700                         if (error == 0)
701                                 continue;
702                 }
703                 break;
704         }
705         if (RB_EMPTY(&ip->rec_tree))
706                 ip->flags &= ~HAMMER_INODE_XDIRTY;
707
708         /*
709          * Now update the inode's on-disk inode-data and/or on-disk record.
710          */
711         switch(ip->flags & (HAMMER_INODE_DELETED|HAMMER_INODE_ONDISK)) {
712         case HAMMER_INODE_DELETED|HAMMER_INODE_ONDISK:
713                 /*
714                  * If deleted and on-disk, don't set any additional flags.
715                  * the delete flag takes care of things.
716                  */
717                 break;
718         case HAMMER_INODE_DELETED:
719                 /*
720                  * Take care of the case where a deleted inode was never
721                  * flushed to the disk in the first place.
722                  */
723                 ip->flags &= ~(HAMMER_INODE_RDIRTY|HAMMER_INODE_DDIRTY|
724                                HAMMER_INODE_XDIRTY|HAMMER_INODE_ITIMES);
725                 while (RB_ROOT(&ip->rec_tree)) {
726                         hammer_record_t rec = RB_ROOT(&ip->rec_tree);
727                         hammer_ref(&rec->lock);
728                         rec->flags |= HAMMER_RECF_DELETED;
729                         hammer_rel_mem_record(rec);
730                 }
731                 break;
732         case HAMMER_INODE_ONDISK:
733                 /*
734                  * If already on-disk, do not set any additional flags.
735                  */
736                 break;
737         default:
738                 /*
739                  * If not on-disk and not deleted, set both dirty flags
740                  * to force an initial record to be written.
741                  */
742                 ip->flags |= HAMMER_INODE_RDIRTY | HAMMER_INODE_DDIRTY;
743                 break;
744         }
745
746         /*
747          * If RDIRTY or DDIRTY is set, write out a new record.  If the inode
748          * is already on-disk the old record is marked as deleted.
749          *
750          * If DELETED is set hammer_update_inode() will delete the existing
751          * record without writing out a new one.
752          *
753          * If *ONLY* the ITIMES flag is set we can update the record in-place.
754          */
755         if ((ip->flags & (HAMMER_INODE_RDIRTY | HAMMER_INODE_DDIRTY |
756                          HAMMER_INODE_ITIMES | HAMMER_INODE_DELETED)) ==
757             HAMMER_INODE_ITIMES) {
758                 error = hammer_update_itimes(ip);
759         } else
760         if (ip->flags & (HAMMER_INODE_RDIRTY | HAMMER_INODE_DDIRTY |
761                          HAMMER_INODE_ITIMES | HAMMER_INODE_DELETED)) {
762                 error = hammer_update_inode(ip);
763         }
764         hammer_commit_transaction(&trans);
765         hammer_unlock(&ip->lock);
766         return(error);
767 }
768
769 /*
770  * Access the filesystem buffer containing the cluster-relative byte
771  * offset, validate the buffer type, load *bufferp and return a
772  * pointer to the requested data.  The buffer is reference and locked on
773  * return.
774  *
775  * If buf_type is 0 the buffer is assumed to be a pure-data buffer and
776  * no type or crc check is performed.
777  *
778  * If *bufferp is not NULL on entry it is assumed to contain a locked
779  * and referenced buffer which will then be replaced.
780  *
781  * If the caller is holding another unrelated buffer locked it must be
782  * passed in reorderbuf so we can properly order buffer locks.
783  *
784  * XXX add a flag for the buffer type and check the CRC here XXX
785  */
786 void *
787 hammer_bread(hammer_cluster_t cluster, int32_t cloff,
788              u_int64_t buf_type, int *errorp,
789              struct hammer_buffer **bufferp)
790 {
791         hammer_buffer_t buffer;
792         int32_t buf_no;
793         int32_t buf_off;
794
795         /*
796          * Load the correct filesystem buffer, replacing *bufferp.
797          */
798         buf_no = cloff / HAMMER_BUFSIZE;
799         buffer = *bufferp;
800         if (buffer == NULL || buffer->cluster != cluster ||
801             buffer->buf_no != buf_no) {
802                 if (buffer) {
803                         /*hammer_unlock(&buffer->io.lock);*/
804                         hammer_rel_buffer(buffer, 0);
805                 }
806                 buffer = hammer_get_buffer(cluster, buf_no, 0, errorp);
807                 *bufferp = buffer;
808                 if (buffer == NULL)
809                         return(NULL);
810                 /*hammer_lock_ex(&buffer->io.lock);*/
811         }
812
813         /*
814          * Validate the buffer type
815          */
816         buf_off = cloff & HAMMER_BUFMASK;
817         if (buf_type) {
818                 if (buf_type != buffer->ondisk->head.buf_type) {
819                         kprintf("BUFFER HEAD TYPE MISMATCH %llx %llx\n",
820                                 buf_type, buffer->ondisk->head.buf_type);
821                         *errorp = EIO;
822                         return(NULL);
823                 }
824                 if (buf_off < sizeof(buffer->ondisk->head)) {
825                         kprintf("BUFFER OFFSET TOO LOW %d\n", buf_off);
826                         *errorp = EIO;
827                         return(NULL);
828                 }
829         }
830
831         /*
832          * Return a pointer to the buffer data.
833          */
834         *errorp = 0;
835         return((char *)buffer->ondisk + buf_off);
836 }
837