HAMMER 20B/many: New spike topology, simplify the B-Tree code.
[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.21 2008/01/17 05:06:09 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         hammer_init_cursor_hmp(&cursor, cache, hmp);
232         cursor.key_beg.obj_id = ip->obj_id;
233         cursor.key_beg.key = 0;
234         cursor.key_beg.create_tid = 0;
235         cursor.key_beg.delete_tid = 0;
236         cursor.key_beg.rec_type = HAMMER_RECTYPE_INODE;
237         cursor.key_beg.obj_type = 0;
238         cursor.asof = iinfo.obj_asof;
239         cursor.flags = HAMMER_CURSOR_GET_RECORD | HAMMER_CURSOR_GET_DATA |
240                        HAMMER_CURSOR_ASOF;
241
242         *errorp = hammer_btree_lookup(&cursor);
243
244         /*
245          * On success the B-Tree lookup will hold the appropriate
246          * buffer cache buffers and provide a pointer to the requested
247          * information.  Copy the information to the in-memory inode
248          * and cache the B-Tree node to improve future operations.
249          */
250         if (*errorp == 0) {
251                 ip->ino_rec = cursor.record->inode;
252                 ip->ino_data = cursor.data->inode;
253                 hammer_cache_node(cursor.node, &ip->cache[0]);
254                 if (cache)
255                         hammer_cache_node(cursor.node, cache);
256         }
257
258         /*
259          * On success load the inode's record and data and insert the
260          * inode into the B-Tree.  It is possible to race another lookup
261          * insertion of the same inode so deal with that condition too.
262          *
263          * The cursor's locked node interlocks against others creating and
264          * destroying ip while we were blocked.
265          */
266         if (*errorp == 0) {
267                 hammer_ref(&ip->lock);
268                 if (RB_INSERT(hammer_ino_rb_tree, &hmp->rb_inos_root, ip)) {
269                         hammer_uncache_node(&ip->cache[0]);
270                         hammer_uncache_node(&ip->cache[1]);
271                         hammer_unref(&ip->lock);
272                         --hammer_count_inodes;
273                         kfree(ip, M_HAMMER);
274                         hammer_done_cursor(&cursor);
275                         goto loop;
276                 }
277                 ip->flags |= HAMMER_INODE_ONDISK;
278         } else {
279                 --hammer_count_inodes;
280                 kfree(ip, M_HAMMER);
281                 ip = NULL;
282         }
283         hammer_done_cursor(&cursor);
284         return (ip);
285 }
286
287 /*
288  * Create a new filesystem object, returning the inode in *ipp.  The
289  * returned inode will be referenced but not locked.
290  *
291  * The inode is created in-memory and will be delay-synchronized to the
292  * disk.
293  */
294 int
295 hammer_create_inode(hammer_transaction_t trans, struct vattr *vap,
296                     struct ucred *cred, hammer_inode_t dip,
297                     struct hammer_inode **ipp)
298 {
299         hammer_mount_t hmp;
300         hammer_inode_t ip;
301         uid_t xuid;
302
303         hmp = trans->hmp;
304         ip = kmalloc(sizeof(*ip), M_HAMMER, M_WAITOK|M_ZERO);
305         ++hammer_count_inodes;
306         ip->obj_id = hammer_alloc_tid(trans);
307         KKASSERT(ip->obj_id != 0);
308         ip->obj_asof = hmp->asof;
309         ip->hmp = hmp;
310         ip->flags = HAMMER_INODE_DDIRTY | HAMMER_INODE_RDIRTY |
311                     HAMMER_INODE_ITIMES;
312         ip->last_tid = trans->tid;
313
314         RB_INIT(&ip->rec_tree);
315
316         ip->ino_rec.ino_atime = trans->tid;
317         ip->ino_rec.ino_mtime = trans->tid;
318         ip->ino_rec.ino_size = 0;
319         ip->ino_rec.ino_nlinks = 0;
320         /* XXX */
321         ip->ino_rec.base.rec_id = hammer_alloc_recid(trans);
322         KKASSERT(ip->ino_rec.base.rec_id != 0);
323         ip->ino_rec.base.base.btype = HAMMER_BTREE_TYPE_RECORD;
324         ip->ino_rec.base.base.obj_id = ip->obj_id;
325         ip->ino_rec.base.base.key = 0;
326         ip->ino_rec.base.base.create_tid = trans->tid;
327         ip->ino_rec.base.base.delete_tid = 0;
328         ip->ino_rec.base.base.rec_type = HAMMER_RECTYPE_INODE;
329         ip->ino_rec.base.base.obj_type = hammer_get_obj_type(vap->va_type);
330
331         ip->ino_data.version = HAMMER_INODE_DATA_VERSION;
332         ip->ino_data.mode = vap->va_mode;
333         ip->ino_data.ctime = trans->tid;
334         ip->ino_data.parent_obj_id = (dip) ? dip->ino_rec.base.base.obj_id : 0;
335
336         switch(ip->ino_rec.base.base.obj_type) {
337         case HAMMER_OBJTYPE_CDEV:
338         case HAMMER_OBJTYPE_BDEV:
339                 ip->ino_data.rmajor = vap->va_rmajor;
340                 ip->ino_data.rminor = vap->va_rminor;
341                 break;
342         default:
343                 break;
344         }
345
346         /*
347          * Calculate default uid/gid and overwrite with information from
348          * the vap.
349          */
350         xuid = hammer_to_unix_xid(&dip->ino_data.uid);
351         ip->ino_data.gid = dip->ino_data.gid;
352         xuid = vop_helper_create_uid(hmp->mp, dip->ino_data.mode, xuid, cred,
353                                      &vap->va_mode);
354         ip->ino_data.mode = vap->va_mode;
355
356         if (vap->va_vaflags & VA_UID_UUID_VALID)
357                 ip->ino_data.uid = vap->va_uid_uuid;
358         else if (vap->va_uid != (uid_t)VNOVAL)
359                 hammer_guid_to_uuid(&ip->ino_data.uid, xuid);
360         if (vap->va_vaflags & VA_GID_UUID_VALID)
361                 ip->ino_data.gid = vap->va_gid_uuid;
362         else if (vap->va_gid != (gid_t)VNOVAL)
363                 hammer_guid_to_uuid(&ip->ino_data.gid, vap->va_gid);
364
365         hammer_ref(&ip->lock);
366         if (RB_INSERT(hammer_ino_rb_tree, &hmp->rb_inos_root, ip)) {
367                 hammer_unref(&ip->lock);
368                 panic("hammer_create_inode: duplicate obj_id %llx", ip->obj_id);
369         }
370         *ipp = ip;
371         return(0);
372 }
373
374 /*
375  * Called by hammer_sync_inode().
376  */
377 static int
378 hammer_update_inode(hammer_inode_t ip)
379 {
380         struct hammer_cursor cursor;
381         struct hammer_cursor *spike = NULL;
382         hammer_record_t record;
383         int error;
384         hammer_tid_t last_tid;
385
386         /*
387          * Locate the record on-disk and mark it as deleted.  Both the B-Tree
388          * node and the record must be marked deleted.  The record may or
389          * may not be physically deleted, depending on the retention policy.
390          *
391          * If the inode has already been deleted on-disk we have nothing
392          * to do.
393          *
394          * XXX Update the inode record and data in-place if the retention
395          * policy allows it.
396          */
397         last_tid = ip->last_tid;
398 retry:
399         error = 0;
400
401         if ((ip->flags & (HAMMER_INODE_ONDISK|HAMMER_INODE_DELONDISK)) ==
402             HAMMER_INODE_ONDISK) {
403                 hammer_init_cursor_hmp(&cursor, &ip->cache[0], ip->hmp);
404                 cursor.key_beg.obj_id = ip->obj_id;
405                 cursor.key_beg.key = 0;
406                 cursor.key_beg.create_tid = 0;
407                 cursor.key_beg.delete_tid = 0;
408                 cursor.key_beg.rec_type = HAMMER_RECTYPE_INODE;
409                 cursor.key_beg.obj_type = 0;
410                 cursor.asof = ip->obj_asof;
411                 cursor.flags |= HAMMER_CURSOR_GET_RECORD | HAMMER_CURSOR_ASOF;
412
413                 error = hammer_btree_lookup(&cursor);
414
415                 if (error == 0) {
416                         error = hammer_ip_delete_record(&cursor, last_tid);
417                         if (error == 0)
418                                 ip->flags |= HAMMER_INODE_DELONDISK;
419                 }
420                 hammer_cache_node(cursor.node, &ip->cache[0]);
421                 hammer_done_cursor(&cursor);
422         }
423
424         /*
425          * Write out a new record if the in-memory inode is not marked
426          * as having been deleted.  Update our inode statistics if this
427          * is the first application of the inode on-disk.
428          *
429          * If the inode has been deleted permanently, HAMMER_INODE_DELONDISK
430          * will remain set and prevent further updates.
431          */
432         if (error == 0 && (ip->flags & HAMMER_INODE_DELETED) == 0) { 
433                 record = hammer_alloc_mem_record(ip);
434                 record->rec.inode = ip->ino_rec;
435                 record->rec.inode.base.base.create_tid = last_tid;
436                 record->rec.inode.base.data_len = sizeof(ip->ino_data);
437                 record->data = (void *)&ip->ino_data;
438                 error = hammer_ip_sync_record(record, &spike);
439                 record->flags |= HAMMER_RECF_DELETED;
440                 hammer_rel_mem_record(record);
441                 if (error == ENOSPC) {
442                         error = hammer_spike(&spike);
443                         if (error == 0)
444                                 goto retry;
445                 }
446                 KKASSERT(spike == NULL);
447                 if (error == 0) {
448                         ip->flags &= ~(HAMMER_INODE_RDIRTY |
449                                        HAMMER_INODE_DDIRTY |
450                                        HAMMER_INODE_DELONDISK |
451                                        HAMMER_INODE_ITIMES);
452                         if ((ip->flags & HAMMER_INODE_ONDISK) == 0) {
453                                 hammer_modify_volume(ip->hmp->rootvol);
454                                 ++ip->hmp->rootvol->ondisk->vol0_stat_inodes;
455                                 ip->flags |= HAMMER_INODE_ONDISK;
456                         }
457                 }
458         }
459         return(error);
460 }
461
462 /*
463  * Update only the itimes fields.  This is done no-historically.  The
464  * record is updated in-place on the disk.
465  */
466 static int
467 hammer_update_itimes(hammer_inode_t ip)
468 {
469         struct hammer_cursor cursor;
470         struct hammer_inode_record *rec;
471         int error;
472
473         error = 0;
474         if ((ip->flags & (HAMMER_INODE_ONDISK|HAMMER_INODE_DELONDISK)) ==
475             HAMMER_INODE_ONDISK) {
476                 hammer_init_cursor_hmp(&cursor, &ip->cache[0], ip->hmp);
477                 cursor.key_beg.obj_id = ip->obj_id;
478                 cursor.key_beg.key = 0;
479                 cursor.key_beg.create_tid = 0;
480                 cursor.key_beg.delete_tid = 0;
481                 cursor.key_beg.rec_type = HAMMER_RECTYPE_INODE;
482                 cursor.key_beg.obj_type = 0;
483                 cursor.asof = ip->obj_asof;
484                 cursor.flags |= HAMMER_CURSOR_GET_RECORD | HAMMER_CURSOR_ASOF;
485
486                 error = hammer_btree_lookup(&cursor);
487
488                 if (error == 0) {
489                         rec = &cursor.record->inode;
490                         hammer_modify_buffer(cursor.record_buffer);
491                         rec->ino_atime = ip->ino_rec.ino_atime;
492                         rec->ino_mtime = ip->ino_rec.ino_mtime;
493                         ip->flags &= ~HAMMER_INODE_ITIMES;
494                         /* XXX recalculate crc */
495                 }
496                 hammer_cache_node(cursor.node, &ip->cache[0]);
497                 hammer_done_cursor(&cursor);
498         }
499         return(error);
500 }
501
502 /*
503  * Release a reference on an inode.  If asked to flush the last release
504  * will flush the inode.
505  */
506 void
507 hammer_rel_inode(struct hammer_inode *ip, int flush)
508 {
509         hammer_unref(&ip->lock);
510         if (flush)
511                 ip->flags |= HAMMER_INODE_FLUSH;
512         if (ip->lock.refs == 0) {
513                 if (ip->flags & HAMMER_INODE_FLUSH)
514                         hammer_unload_inode(ip, (void *)MNT_WAIT);
515                 else
516                         hammer_unload_inode(ip, (void *)MNT_NOWAIT);
517         }
518 }
519
520 /*
521  * Unload and destroy the specified inode.
522  *
523  * (called via RB_SCAN)
524  */
525 int
526 hammer_unload_inode(struct hammer_inode *ip, void *data)
527 {
528         int error;
529
530         KASSERT(ip->lock.refs == 0,
531                 ("hammer_unload_inode: %d refs\n", ip->lock.refs));
532         KKASSERT(ip->vp == NULL);
533         hammer_ref(&ip->lock);
534
535         error = hammer_sync_inode(ip, (int)data, 1);
536         if (error)
537                 kprintf("hammer_sync_inode failed error %d\n", error);
538         if (ip->lock.refs == 1) {
539                 KKASSERT(RB_EMPTY(&ip->rec_tree));
540                 RB_REMOVE(hammer_ino_rb_tree, &ip->hmp->rb_inos_root, ip);
541
542                 hammer_uncache_node(&ip->cache[0]);
543                 hammer_uncache_node(&ip->cache[1]);
544                 --hammer_count_inodes;
545                 kfree(ip, M_HAMMER);
546         } else {
547                 hammer_unref(&ip->lock);
548         }
549         return(0);
550 }
551
552 /*
553  * A transaction has modified an inode, requiring updates as specified by
554  * the passed flags.
555  *
556  * HAMMER_INODE_RDIRTY: Inode record has been updated
557  * HAMMER_INODE_DDIRTY: Inode data has been updated
558  * HAMMER_INODE_DELETED: Inode record/data must be deleted
559  * HAMMER_INODE_ITIMES: mtime/atime has been updated
560  *
561  * last_tid is the TID to use to generate the correct TID when the inode
562  * is synced to disk.
563  */
564 void
565 hammer_modify_inode(struct hammer_transaction *trans,
566                     struct hammer_inode *ip, int flags)
567 {
568         KKASSERT ((ip->flags & HAMMER_INODE_RO) == 0 ||
569                   (HAMMER_INODE_RDIRTY|HAMMER_INODE_DDIRTY|
570                    HAMMER_INODE_DELETED|HAMMER_INODE_ITIMES) == 0);
571
572         if (flags &
573             (HAMMER_INODE_RDIRTY|HAMMER_INODE_DDIRTY|HAMMER_INODE_DELETED)) {
574                 if (hammer_debug_tid) {
575                         kprintf("hammer_modify_inode: %016llx (%08x)\n", 
576                                 trans->tid, (int)(trans->tid / 1000000000LL));
577                 }
578                 ip->last_tid = trans->tid;
579         }
580         ip->flags |= flags;
581 }
582
583 /*
584  * Sync any dirty buffers and records associated with an inode.  The
585  * inode's last_tid field is used as the transaction id for the sync,
586  * overriding any intermediate TIDs that were used for records.  Note
587  * that the dirty buffer cache buffers do not have any knowledge of
588  * the transaction id they were modified under.
589  *
590  * If we can't sync due to a cluster becoming full the spike structure
591  * will be filled in and ENOSPC returned.  We must return -ENOSPC to
592  * terminate the RB_SCAN.
593  */
594 static int
595 hammer_sync_inode_callback(hammer_record_t rec, void *data)
596 {
597         struct hammer_cursor **spike = data;
598         int error;
599
600         hammer_ref(&rec->lock);
601         error = hammer_ip_sync_record(rec, spike);
602         hammer_rel_mem_record(rec);
603
604         if (error) {
605                 error = -error;
606                 if (error != -ENOSPC) {
607                         kprintf("hammer_sync_inode_callback: sync failed rec "
608                                 "%p, error %d\n", rec, error);
609                 }
610         }
611         return(error);
612 }
613
614 /*
615  * XXX error handling
616  */
617 int
618 hammer_sync_inode(hammer_inode_t ip, int waitfor, int handle_delete)
619 {
620         struct hammer_transaction trans;
621         struct hammer_cursor *spike = NULL;
622         int error;
623
624         if ((ip->flags & HAMMER_INODE_MODMASK) == 0) {
625                 return(0);
626         }
627
628         hammer_lock_ex(&ip->lock);
629
630         /*
631          * Use the transaction id of the last operation to sync.
632          */
633         if (ip->last_tid)
634                 hammer_start_transaction_tid(&trans, ip->hmp, ip->last_tid);
635         else
636                 hammer_start_transaction(&trans, ip->hmp);
637
638         /*
639          * If the inode has been deleted (nlinks == 0), and the OS no longer
640          * has any references to it (handle_delete != 0), clean up in-memory
641          * data.
642          *
643          * NOTE: We do not set the RDIRTY flag when updating the delete_tid,
644          * setting HAMMER_INODE_DELETED takes care of it.
645          *
646          * NOTE: Because we may sync records within this new transaction,
647          * force the inode update later on to use our transaction id or
648          * the delete_tid of the inode may be less then the create_tid of
649          * the inode update.  XXX shouldn't happen but don't take the chance.
650          *
651          * NOTE: The call to hammer_ip_delete_range() cannot return ENOSPC
652          * so we can pass a NULL spike structure, because no partial data
653          * deletion can occur (yet).
654          */
655         if (ip->ino_rec.ino_nlinks == 0 && handle_delete && 
656             (ip->flags & HAMMER_INODE_GONE) == 0) {
657                 ip->flags |= HAMMER_INODE_GONE;
658                 if (ip->vp)
659                         vtruncbuf(ip->vp, 0, HAMMER_BUFSIZE);
660                 error = hammer_ip_delete_range_all(&trans, ip);
661                 KKASSERT(RB_EMPTY(&ip->rec_tree));
662                 ip->ino_rec.base.base.delete_tid = trans.tid;
663                 hammer_modify_inode(&trans, ip, HAMMER_INODE_DELETED);
664                 hammer_modify_volume(ip->hmp->rootvol);
665                 --ip->hmp->rootvol->ondisk->vol0_stat_inodes;
666         }
667
668         /*
669          * Sync the buffer cache.
670          */
671         if (ip->vp != NULL) {
672                 error = vfsync(ip->vp, waitfor, 1, NULL, NULL);
673                 if (RB_ROOT(&ip->vp->v_rbdirty_tree) == NULL)
674                         ip->flags &= ~HAMMER_INODE_BUFS;
675         } else {
676                 error = 0;
677         }
678
679
680         /*
681          * Now sync related records
682          */
683         for (;;) {
684                 error = RB_SCAN(hammer_rec_rb_tree, &ip->rec_tree, NULL,
685                                 hammer_sync_inode_callback, &spike);
686                 KKASSERT(error <= 0);
687                 if (error < 0)
688                         error = -error;
689                 if (error == ENOSPC) {
690                         error = hammer_spike(&spike);
691                         if (error == 0)
692                                 continue;
693                 }
694                 break;
695         }
696         if (RB_EMPTY(&ip->rec_tree))
697                 ip->flags &= ~HAMMER_INODE_XDIRTY;
698
699         /*
700          * Now update the inode's on-disk inode-data and/or on-disk record.
701          */
702         switch(ip->flags & (HAMMER_INODE_DELETED|HAMMER_INODE_ONDISK)) {
703         case HAMMER_INODE_DELETED|HAMMER_INODE_ONDISK:
704                 /*
705                  * If deleted and on-disk, don't set any additional flags.
706                  * the delete flag takes care of things.
707                  */
708                 break;
709         case HAMMER_INODE_DELETED:
710                 /*
711                  * Take care of the case where a deleted inode was never
712                  * flushed to the disk in the first place.
713                  */
714                 ip->flags &= ~(HAMMER_INODE_RDIRTY|HAMMER_INODE_DDIRTY|
715                                HAMMER_INODE_XDIRTY|HAMMER_INODE_ITIMES);
716                 while (RB_ROOT(&ip->rec_tree)) {
717                         hammer_record_t rec = RB_ROOT(&ip->rec_tree);
718                         hammer_ref(&rec->lock);
719                         rec->flags |= HAMMER_RECF_DELETED;
720                         hammer_rel_mem_record(rec);
721                 }
722                 break;
723         case HAMMER_INODE_ONDISK:
724                 /*
725                  * If already on-disk, do not set any additional flags.
726                  */
727                 break;
728         default:
729                 /*
730                  * If not on-disk and not deleted, set both dirty flags
731                  * to force an initial record to be written.
732                  */
733                 ip->flags |= HAMMER_INODE_RDIRTY | HAMMER_INODE_DDIRTY;
734                 break;
735         }
736
737         /*
738          * If RDIRTY or DDIRTY is set, write out a new record.  If the inode
739          * is already on-disk the old record is marked as deleted.
740          *
741          * If DELETED is set hammer_update_inode() will delete the existing
742          * record without writing out a new one.
743          *
744          * If *ONLY* the ITIMES flag is set we can update the record in-place.
745          */
746         if ((ip->flags & (HAMMER_INODE_RDIRTY | HAMMER_INODE_DDIRTY |
747                          HAMMER_INODE_ITIMES | HAMMER_INODE_DELETED)) ==
748             HAMMER_INODE_ITIMES) {
749                 error = hammer_update_itimes(ip);
750         } else
751         if (ip->flags & (HAMMER_INODE_RDIRTY | HAMMER_INODE_DDIRTY |
752                          HAMMER_INODE_ITIMES | HAMMER_INODE_DELETED)) {
753                 error = hammer_update_inode(ip);
754         }
755         hammer_commit_transaction(&trans);
756         hammer_unlock(&ip->lock);
757         return(error);
758 }
759
760 /*
761  * Access the filesystem buffer containing the cluster-relative byte
762  * offset, validate the buffer type, load *bufferp and return a
763  * pointer to the requested data.  The buffer is reference and locked on
764  * return.
765  *
766  * If buf_type is 0 the buffer is assumed to be a pure-data buffer and
767  * no type or crc check is performed.
768  *
769  * If *bufferp is not NULL on entry it is assumed to contain a locked
770  * and referenced buffer which will then be replaced.
771  *
772  * If the caller is holding another unrelated buffer locked it must be
773  * passed in reorderbuf so we can properly order buffer locks.
774  *
775  * XXX add a flag for the buffer type and check the CRC here XXX
776  */
777 void *
778 hammer_bread(hammer_cluster_t cluster, int32_t cloff,
779              u_int64_t buf_type, int *errorp,
780              struct hammer_buffer **bufferp)
781 {
782         hammer_buffer_t buffer;
783         int32_t buf_no;
784         int32_t buf_off;
785
786         /*
787          * Load the correct filesystem buffer, replacing *bufferp.
788          */
789         buf_no = cloff / HAMMER_BUFSIZE;
790         buffer = *bufferp;
791         if (buffer == NULL || buffer->cluster != cluster ||
792             buffer->buf_no != buf_no) {
793                 if (buffer) {
794                         /*hammer_unlock(&buffer->io.lock);*/
795                         hammer_rel_buffer(buffer, 0);
796                 }
797                 buffer = hammer_get_buffer(cluster, buf_no, 0, errorp);
798                 *bufferp = buffer;
799                 if (buffer == NULL)
800                         return(NULL);
801                 /*hammer_lock_ex(&buffer->io.lock);*/
802         }
803
804         /*
805          * Validate the buffer type
806          */
807         buf_off = cloff & HAMMER_BUFMASK;
808         if (buf_type) {
809                 if (buf_type != buffer->ondisk->head.buf_type) {
810                         kprintf("BUFFER HEAD TYPE MISMATCH %llx %llx\n",
811                                 buf_type, buffer->ondisk->head.buf_type);
812                         *errorp = EIO;
813                         return(NULL);
814                 }
815                 if (buf_off < sizeof(buffer->ondisk->head)) {
816                         kprintf("BUFFER OFFSET TOO LOW %d\n", buf_off);
817                         *errorp = EIO;
818                         return(NULL);
819                 }
820         }
821
822         /*
823          * Return a pointer to the buffer data.
824          */
825         *errorp = 0;
826         return((char *)buffer->ondisk + buf_off);
827 }
828