Merge from vendor branch BZIP:
[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.31 2008/03/19 20:18:17 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  * Return a locked vnode for the specified inode.  The inode must be
96  * referenced but NOT LOCKED on entry and will remain referenced on
97  * return.
98  */
99 int
100 hammer_get_vnode(struct hammer_inode *ip, int lktype, struct vnode **vpp)
101 {
102         struct vnode *vp;
103         int error = 0;
104
105         for (;;) {
106                 if ((vp = ip->vp) == NULL) {
107                         error = getnewvnode(VT_HAMMER, ip->hmp->mp, vpp, 0, 0);
108                         if (error)
109                                 break;
110                         hammer_lock_ex(&ip->lock);
111                         if (ip->vp != NULL) {
112                                 hammer_unlock(&ip->lock);
113                                 vp->v_type = VBAD;
114                                 vx_put(vp);
115                                 continue;
116                         }
117                         hammer_ref(&ip->lock);
118                         vp = *vpp;
119                         ip->vp = vp;
120                         vp->v_type = hammer_get_vnode_type(
121                                             ip->ino_rec.base.base.obj_type);
122
123                         switch(ip->ino_rec.base.base.obj_type) {
124                         case HAMMER_OBJTYPE_CDEV:
125                         case HAMMER_OBJTYPE_BDEV:
126                                 vp->v_ops = &ip->hmp->mp->mnt_vn_spec_ops;
127                                 addaliasu(vp, ip->ino_data.rmajor,
128                                           ip->ino_data.rminor);
129                                 break;
130                         case HAMMER_OBJTYPE_FIFO:
131                                 vp->v_ops = &ip->hmp->mp->mnt_vn_fifo_ops;
132                                 break;
133                         default:
134                                 break;
135                         }
136
137                         /*
138                          * Only mark as the root vnode if the ip is not
139                          * historical, otherwise the VFS cache will get
140                          * confused.  The other half of the special handling
141                          * is in hammer_vop_nlookupdotdot().
142                          */
143                         if (ip->obj_id == HAMMER_OBJID_ROOT &&
144                             ip->obj_asof == ip->hmp->asof) {
145                                 vp->v_flag |= VROOT;
146                         }
147
148                         vp->v_data = (void *)ip;
149                         /* vnode locked by getnewvnode() */
150                         /* make related vnode dirty if inode dirty? */
151                         hammer_unlock(&ip->lock);
152                         if (vp->v_type == VREG)
153                                 vinitvmio(vp, ip->ino_rec.ino_size);
154                         break;
155                 }
156
157                 /*
158                  * loop if the vget fails (aka races), or if the vp
159                  * no longer matches ip->vp.
160                  */
161                 if (vget(vp, LK_EXCLUSIVE) == 0) {
162                         if (vp == ip->vp)
163                                 break;
164                         vput(vp);
165                 }
166         }
167         *vpp = vp;
168         return(error);
169 }
170
171 /*
172  * Acquire a HAMMER inode.  The returned inode is not locked.  These functions
173  * do not attach or detach the related vnode (use hammer_get_vnode() for
174  * that).
175  *
176  * The flags argument is only applied for newly created inodes, and only
177  * certain flags are inherited.
178  */
179 struct hammer_inode *
180 hammer_get_inode(hammer_transaction_t trans, struct hammer_node **cache,
181                  u_int64_t obj_id, hammer_tid_t asof, int flags, int *errorp)
182 {
183         hammer_mount_t hmp = trans->hmp;
184         struct hammer_inode_info iinfo;
185         struct hammer_cursor cursor;
186         struct hammer_inode *ip;
187
188         /*
189          * Determine if we already have an inode cached.  If we do then
190          * we are golden.
191          */
192         iinfo.obj_id = obj_id;
193         iinfo.obj_asof = asof;
194 loop:
195         ip = hammer_ino_rb_tree_RB_LOOKUP_INFO(&hmp->rb_inos_root, &iinfo);
196         if (ip) {
197                 hammer_ref(&ip->lock);
198                 *errorp = 0;
199                 return(ip);
200         }
201
202         ip = kmalloc(sizeof(*ip), M_HAMMER, M_WAITOK|M_ZERO);
203         ++hammer_count_inodes;
204         ip->obj_id = obj_id;
205         ip->obj_asof = iinfo.obj_asof;
206         ip->hmp = hmp;
207         ip->flags = flags & HAMMER_INODE_RO;
208         if (hmp->ronly)
209                 ip->flags |= HAMMER_INODE_RO;
210         RB_INIT(&ip->rec_tree);
211
212         /*
213          * Locate the on-disk inode.
214          */
215 retry:
216         hammer_init_cursor(trans, &cursor, cache);
217         cursor.key_beg.obj_id = ip->obj_id;
218         cursor.key_beg.key = 0;
219         cursor.key_beg.create_tid = 0;
220         cursor.key_beg.delete_tid = 0;
221         cursor.key_beg.rec_type = HAMMER_RECTYPE_INODE;
222         cursor.key_beg.obj_type = 0;
223         cursor.asof = iinfo.obj_asof;
224         cursor.flags = HAMMER_CURSOR_GET_RECORD | HAMMER_CURSOR_GET_DATA |
225                        HAMMER_CURSOR_ASOF;
226
227         *errorp = hammer_btree_lookup(&cursor);
228         if (*errorp == EDEADLK) {
229                 hammer_done_cursor(&cursor);
230                 goto retry;
231         }
232
233         /*
234          * On success the B-Tree lookup will hold the appropriate
235          * buffer cache buffers and provide a pointer to the requested
236          * information.  Copy the information to the in-memory inode
237          * and cache the B-Tree node to improve future operations.
238          */
239         if (*errorp == 0) {
240                 ip->ino_rec = cursor.record->inode;
241                 ip->ino_data = cursor.data->inode;
242                 hammer_cache_node(cursor.node, &ip->cache[0]);
243                 if (cache)
244                         hammer_cache_node(cursor.node, cache);
245         }
246
247         /*
248          * On success load the inode's record and data and insert the
249          * inode into the B-Tree.  It is possible to race another lookup
250          * insertion of the same inode so deal with that condition too.
251          *
252          * The cursor's locked node interlocks against others creating and
253          * destroying ip while we were blocked.
254          */
255         if (*errorp == 0) {
256                 hammer_ref(&ip->lock);
257                 if (RB_INSERT(hammer_ino_rb_tree, &hmp->rb_inos_root, ip)) {
258                         hammer_uncache_node(&ip->cache[0]);
259                         hammer_uncache_node(&ip->cache[1]);
260                         hammer_unref(&ip->lock);
261                         --hammer_count_inodes;
262                         kfree(ip, M_HAMMER);
263                         hammer_done_cursor(&cursor);
264                         goto loop;
265                 }
266                 ip->flags |= HAMMER_INODE_ONDISK;
267         } else {
268                 --hammer_count_inodes;
269                 kfree(ip, M_HAMMER);
270                 ip = NULL;
271         }
272         hammer_done_cursor(&cursor);
273         return (ip);
274 }
275
276 /*
277  * Create a new filesystem object, returning the inode in *ipp.  The
278  * returned inode will be referenced but not locked.
279  *
280  * The inode is created in-memory and will be delay-synchronized to the
281  * disk.
282  */
283 int
284 hammer_create_inode(hammer_transaction_t trans, struct vattr *vap,
285                     struct ucred *cred, hammer_inode_t dip,
286                     struct hammer_inode **ipp)
287 {
288         hammer_mount_t hmp;
289         hammer_inode_t ip;
290         uid_t xuid;
291
292         hmp = trans->hmp;
293         ip = kmalloc(sizeof(*ip), M_HAMMER, M_WAITOK|M_ZERO);
294         ++hammer_count_inodes;
295         ip->obj_id = hammer_alloc_tid(trans);
296         KKASSERT(ip->obj_id != 0);
297         ip->obj_asof = hmp->asof;
298         ip->hmp = hmp;
299         ip->flags = HAMMER_INODE_DDIRTY | HAMMER_INODE_RDIRTY |
300                     HAMMER_INODE_ITIMES | HAMMER_INODE_TIDLOCKED;
301         ip->last_tid = trans->tid;
302
303         RB_INIT(&ip->rec_tree);
304
305         ip->ino_rec.ino_atime = trans->tid;
306         ip->ino_rec.ino_mtime = trans->tid;
307         ip->ino_rec.ino_size = 0;
308         ip->ino_rec.ino_nlinks = 0;
309         /* XXX */
310         ip->ino_rec.base.base.btype = HAMMER_BTREE_TYPE_RECORD;
311         ip->ino_rec.base.base.obj_id = ip->obj_id;
312         ip->ino_rec.base.base.key = 0;
313         ip->ino_rec.base.base.create_tid = trans->tid;
314         ip->ino_rec.base.base.delete_tid = 0;
315         ip->ino_rec.base.base.rec_type = HAMMER_RECTYPE_INODE;
316         ip->ino_rec.base.base.obj_type = hammer_get_obj_type(vap->va_type);
317
318         ip->ino_data.version = HAMMER_INODE_DATA_VERSION;
319         ip->ino_data.mode = vap->va_mode;
320         ip->ino_data.ctime = trans->tid;
321         ip->ino_data.parent_obj_id = (dip) ? dip->ino_rec.base.base.obj_id : 0;
322
323         switch(ip->ino_rec.base.base.obj_type) {
324         case HAMMER_OBJTYPE_CDEV:
325         case HAMMER_OBJTYPE_BDEV:
326                 ip->ino_data.rmajor = vap->va_rmajor;
327                 ip->ino_data.rminor = vap->va_rminor;
328                 break;
329         default:
330                 break;
331         }
332
333         /*
334          * Calculate default uid/gid and overwrite with information from
335          * the vap.
336          */
337         xuid = hammer_to_unix_xid(&dip->ino_data.uid);
338         ip->ino_data.gid = dip->ino_data.gid;
339         xuid = vop_helper_create_uid(hmp->mp, dip->ino_data.mode, xuid, cred,
340                                      &vap->va_mode);
341         ip->ino_data.mode = vap->va_mode;
342
343         if (vap->va_vaflags & VA_UID_UUID_VALID)
344                 ip->ino_data.uid = vap->va_uid_uuid;
345         else if (vap->va_uid != (uid_t)VNOVAL)
346                 hammer_guid_to_uuid(&ip->ino_data.uid, xuid);
347         if (vap->va_vaflags & VA_GID_UUID_VALID)
348                 ip->ino_data.gid = vap->va_gid_uuid;
349         else if (vap->va_gid != (gid_t)VNOVAL)
350                 hammer_guid_to_uuid(&ip->ino_data.gid, vap->va_gid);
351
352         hammer_ref(&ip->lock);
353         if (RB_INSERT(hammer_ino_rb_tree, &hmp->rb_inos_root, ip)) {
354                 hammer_unref(&ip->lock);
355                 panic("hammer_create_inode: duplicate obj_id %llx", ip->obj_id);
356         }
357         *ipp = ip;
358         return(0);
359 }
360
361 /*
362  * Called by hammer_sync_inode().
363  */
364 static int
365 hammer_update_inode(hammer_transaction_t trans, hammer_inode_t ip)
366 {
367         struct hammer_cursor cursor;
368         hammer_record_t record;
369         int error;
370         hammer_tid_t last_tid;
371
372         /*
373          * Locate the record on-disk and mark it as deleted.  Both the B-Tree
374          * node and the record must be marked deleted.  The record may or
375          * may not be physically deleted, depending on the retention policy.
376          *
377          * If the inode has already been deleted on-disk we have nothing
378          * to do.
379          *
380          * XXX Update the inode record and data in-place if the retention
381          * policy allows it.
382          */
383         last_tid = ip->last_tid;
384 retry:
385         error = 0;
386
387         if ((ip->flags & (HAMMER_INODE_ONDISK|HAMMER_INODE_DELONDISK)) ==
388             HAMMER_INODE_ONDISK) {
389                 hammer_init_cursor(trans, &cursor, &ip->cache[0]);
390                 cursor.key_beg.obj_id = ip->obj_id;
391                 cursor.key_beg.key = 0;
392                 cursor.key_beg.create_tid = 0;
393                 cursor.key_beg.delete_tid = 0;
394                 cursor.key_beg.rec_type = HAMMER_RECTYPE_INODE;
395                 cursor.key_beg.obj_type = 0;
396                 cursor.asof = ip->obj_asof;
397                 cursor.flags |= HAMMER_CURSOR_GET_RECORD | HAMMER_CURSOR_ASOF;
398
399                 error = hammer_btree_lookup(&cursor);
400
401                 if (error == 0) {
402                         error = hammer_ip_delete_record(&cursor, last_tid);
403                         if (error == 0)
404                                 ip->flags |= HAMMER_INODE_DELONDISK;
405                         hammer_cache_node(cursor.node, &ip->cache[0]);
406                 }
407                 hammer_done_cursor(&cursor);
408                 if (error == EDEADLK)
409                         goto retry;
410         }
411
412         /*
413          * Write out a new record if the in-memory inode is not marked
414          * as having been deleted.  Update our inode statistics if this
415          * is the first application of the inode on-disk.
416          *
417          * If the inode has been deleted permanently, HAMMER_INODE_DELONDISK
418          * will remain set and prevent further updates.
419          */
420         if (error == 0 && (ip->flags & HAMMER_INODE_DELETED) == 0) { 
421                 record = hammer_alloc_mem_record(ip);
422                 record->rec.inode = ip->ino_rec;
423                 record->rec.inode.base.base.create_tid = last_tid;
424                 record->rec.inode.base.data_len = sizeof(ip->ino_data);
425                 record->data = (void *)&ip->ino_data;
426                 error = hammer_ip_sync_record(trans, record);
427                 record->flags |= HAMMER_RECF_DELETED;
428                 hammer_rel_mem_record(record);
429                 if (error == 0) {
430                         ip->flags &= ~(HAMMER_INODE_RDIRTY |
431                                        HAMMER_INODE_DDIRTY |
432                                        HAMMER_INODE_DELONDISK |
433                                        HAMMER_INODE_ITIMES);
434                         if ((ip->flags & HAMMER_INODE_ONDISK) == 0) {
435                                 hammer_modify_volume(trans, ip->hmp->rootvol,
436                                                      NULL, 0);
437                                 ++ip->hmp->rootvol->ondisk->vol0_stat_inodes;
438                                 ip->flags |= HAMMER_INODE_ONDISK;
439                         }
440
441                         /*
442                          * Unlock the sync TID if it was locked, now that
443                          * we have written it out to disk.
444                          */
445                         ip->flags &= ~HAMMER_INODE_TIDLOCKED;
446                 }
447         }
448         return(error);
449 }
450
451 /*
452  * Update only the itimes fields.  This is done no-historically.  The
453  * record is updated in-place on the disk.
454  */
455 static int
456 hammer_update_itimes(hammer_transaction_t trans, hammer_inode_t ip)
457 {
458         struct hammer_cursor cursor;
459         struct hammer_inode_record *rec;
460         int error;
461
462 retry:
463         error = 0;
464         if ((ip->flags & (HAMMER_INODE_ONDISK|HAMMER_INODE_DELONDISK)) ==
465             HAMMER_INODE_ONDISK) {
466                 hammer_init_cursor(trans, &cursor, &ip->cache[0]);
467                 cursor.key_beg.obj_id = ip->obj_id;
468                 cursor.key_beg.key = 0;
469                 cursor.key_beg.create_tid = 0;
470                 cursor.key_beg.delete_tid = 0;
471                 cursor.key_beg.rec_type = HAMMER_RECTYPE_INODE;
472                 cursor.key_beg.obj_type = 0;
473                 cursor.asof = ip->obj_asof;
474                 cursor.flags |= HAMMER_CURSOR_GET_RECORD | HAMMER_CURSOR_ASOF;
475
476                 error = hammer_btree_lookup(&cursor);
477                 if (error == 0) {
478                         rec = &cursor.record->inode;
479                         hammer_modify_buffer(cursor.trans, cursor.record_buffer,
480                                              NULL, 0);
481                         rec->ino_atime = ip->ino_rec.ino_atime;
482                         rec->ino_mtime = ip->ino_rec.ino_mtime;
483                         ip->flags &= ~HAMMER_INODE_ITIMES;
484                         /* XXX recalculate crc */
485                         hammer_cache_node(cursor.node, &ip->cache[0]);
486                 }
487                 hammer_done_cursor(&cursor);
488                 if (error == EDEADLK)
489                         goto retry;
490         }
491         return(error);
492 }
493
494 /*
495  * Release a reference on an inode.  If asked to flush the last release
496  * will flush the inode.
497  */
498 void
499 hammer_rel_inode(struct hammer_inode *ip, int flush)
500 {
501         hammer_unref(&ip->lock);
502         if (flush)
503                 ip->flags |= HAMMER_INODE_FLUSH;
504         if (ip->lock.refs == 0) {
505                 if (ip->flags & HAMMER_INODE_FLUSH)
506                         hammer_unload_inode(ip, (void *)MNT_WAIT);
507                 else
508                         hammer_unload_inode(ip, (void *)MNT_NOWAIT);
509         }
510 }
511
512 /*
513  * Unload and destroy the specified inode.
514  *
515  * (typically called via RB_SCAN)
516  */
517 int
518 hammer_unload_inode(struct hammer_inode *ip, void *data)
519 {
520         int error;
521
522         KASSERT(ip->lock.refs == 0,
523                 ("hammer_unload_inode: %d refs\n", ip->lock.refs));
524         KKASSERT(ip->vp == NULL);
525         hammer_ref(&ip->lock);
526
527         error = hammer_sync_inode(ip, (int)data, 1);
528         if (error)
529                 kprintf("hammer_sync_inode failed error %d\n", error);
530         if (ip->lock.refs == 1) {
531                 KKASSERT(RB_EMPTY(&ip->rec_tree));
532                 RB_REMOVE(hammer_ino_rb_tree, &ip->hmp->rb_inos_root, ip);
533
534                 hammer_uncache_node(&ip->cache[0]);
535                 hammer_uncache_node(&ip->cache[1]);
536                 --hammer_count_inodes;
537                 kfree(ip, M_HAMMER);
538         } else {
539                 hammer_unref(&ip->lock);
540         }
541         return(0);
542 }
543
544 /*
545  * A transaction has modified an inode, requiring updates as specified by
546  * the passed flags.
547  *
548  * HAMMER_INODE_RDIRTY: Inode record has been updated
549  * HAMMER_INODE_DDIRTY: Inode data has been updated
550  * HAMMER_INODE_DELETED: Inode record/data must be deleted
551  * HAMMER_INODE_ITIMES: mtime/atime has been updated
552  *
553  * last_tid is the TID to use to generate the correct TID when the inode
554  * is synced to disk.  The first inode record laid out on disk must match
555  * the transaction id of the related directory entry so only update last_tid
556  * if that has already occured.
557  */
558 void
559 hammer_modify_inode(struct hammer_transaction *trans,
560                     struct hammer_inode *ip, int flags)
561 {
562         KKASSERT ((ip->flags & HAMMER_INODE_RO) == 0 ||
563                   (HAMMER_INODE_RDIRTY|HAMMER_INODE_DDIRTY|
564                    HAMMER_INODE_DELETED|HAMMER_INODE_ITIMES) == 0);
565
566         if (flags &
567             (HAMMER_INODE_RDIRTY|HAMMER_INODE_DDIRTY|HAMMER_INODE_DELETED)) {
568                 if (hammer_debug_tid) {
569                         kprintf("hammer_modify_inode: %016llx (%08x)\n", 
570                                 trans->tid, (int)(trans->tid / 1000000000LL));
571                 }
572
573                 /*
574                  * Update the inode sync transaction id unless it's locked
575                  * due to some prior required synchroznization.  Locking the
576                  * tid in the new flags overrides this (used by rename).
577                  */
578                 if ((ip->flags & HAMMER_INODE_TIDLOCKED) == 0)
579                         ip->last_tid = trans->tid;
580                 else if (flags & HAMMER_INODE_TIDLOCKED)
581                         ip->last_tid = trans->tid;
582         }
583         ip->flags |= flags;
584 }
585
586 /*
587  * Sync any dirty buffers and records associated with an inode.  The
588  * inode's last_tid field is used as the transaction id for the sync,
589  * overriding any intermediate TIDs that were used for records.  Note
590  * that the dirty buffer cache buffers do not have any knowledge of
591  * the transaction id they were modified under.
592  */
593 static int
594 hammer_sync_inode_callback(hammer_record_t rec, void *data)
595 {
596         hammer_transaction_t trans = data;
597         int error;
598
599         hammer_ref(&rec->lock);
600         error = hammer_ip_sync_record(trans, rec);
601         hammer_rel_mem_record(rec);
602
603         if (error) {
604                 error = -error;
605                 if (error != -ENOSPC) {
606                         kprintf("hammer_sync_inode_callback: sync failed rec "
607                                 "%p, error %d\n", rec, error);
608                 }
609         }
610         return(error);
611 }
612
613 /*
614  * XXX error handling
615  */
616 int
617 hammer_sync_inode(hammer_inode_t ip, int waitfor, int handle_delete)
618 {
619         struct hammer_transaction trans;
620         int error;
621
622         if ((ip->flags & HAMMER_INODE_MODMASK) == 0) {
623                 return(0);
624         }
625
626         hammer_lock_ex(&ip->lock);
627
628         /*
629          * Use the transaction id of the last operation to sync.
630          */
631         if (ip->last_tid)
632                 hammer_start_transaction_tid(&trans, ip->hmp, ip->last_tid);
633         else
634                 hammer_start_transaction(&trans, ip->hmp);
635
636         /*
637          * If the inode has been deleted (nlinks == 0), and the OS no longer
638          * has any references to it (handle_delete != 0), clean up in-memory
639          * data.
640          *
641          * NOTE: We do not set the RDIRTY flag when updating the delete_tid,
642          * setting HAMMER_INODE_DELETED takes care of it.
643          *
644          * NOTE: Because we may sync records within this new transaction,
645          * force the inode update later on to use our transaction id or
646          * the delete_tid of the inode may be less then the create_tid of
647          * the inode update.  XXX shouldn't happen but don't take the chance.
648          */
649         if (ip->ino_rec.ino_nlinks == 0 && handle_delete && 
650             (ip->flags & HAMMER_INODE_GONE) == 0) {
651                 ip->flags |= HAMMER_INODE_GONE;
652                 if (ip->vp)
653                         vtruncbuf(ip->vp, 0, HAMMER_BUFSIZE);
654                 error = hammer_ip_delete_range_all(&trans, ip);
655                 KKASSERT(RB_EMPTY(&ip->rec_tree));
656                 ip->ino_rec.base.base.delete_tid = trans.tid;
657                 hammer_modify_inode(&trans, ip, HAMMER_INODE_DELETED);
658                 hammer_modify_volume(&trans, ip->hmp->rootvol, NULL, 0);
659                 --ip->hmp->rootvol->ondisk->vol0_stat_inodes;
660         }
661
662         /*
663          * Sync the buffer cache.
664          */
665         if (ip->vp != NULL) {
666                 error = vfsync(ip->vp, waitfor, 1, NULL, NULL);
667                 if (RB_ROOT(&ip->vp->v_rbdirty_tree) == NULL)
668                         ip->flags &= ~HAMMER_INODE_BUFS;
669         } else {
670                 error = 0;
671         }
672
673
674         /*
675          * Now sync related records
676          */
677         for (;;) {
678                 error = RB_SCAN(hammer_rec_rb_tree, &ip->rec_tree, NULL,
679                                 hammer_sync_inode_callback, &trans);
680                 KKASSERT(error <= 0);
681                 if (error < 0)
682                         error = -error;
683                 break;
684         }
685         if (RB_EMPTY(&ip->rec_tree))
686                 ip->flags &= ~HAMMER_INODE_XDIRTY;
687
688         /*
689          * Now update the inode's on-disk inode-data and/or on-disk record.
690          */
691         switch(ip->flags & (HAMMER_INODE_DELETED|HAMMER_INODE_ONDISK)) {
692         case HAMMER_INODE_DELETED|HAMMER_INODE_ONDISK:
693                 /*
694                  * If deleted and on-disk, don't set any additional flags.
695                  * the delete flag takes care of things.
696                  */
697                 break;
698         case HAMMER_INODE_DELETED:
699                 /*
700                  * Take care of the case where a deleted inode was never
701                  * flushed to the disk in the first place.
702                  */
703                 ip->flags &= ~(HAMMER_INODE_RDIRTY|HAMMER_INODE_DDIRTY|
704                                HAMMER_INODE_XDIRTY|HAMMER_INODE_ITIMES);
705                 while (RB_ROOT(&ip->rec_tree)) {
706                         hammer_record_t rec = RB_ROOT(&ip->rec_tree);
707                         hammer_ref(&rec->lock);
708                         rec->flags |= HAMMER_RECF_DELETED;
709                         hammer_rel_mem_record(rec);
710                 }
711                 break;
712         case HAMMER_INODE_ONDISK:
713                 /*
714                  * If already on-disk, do not set any additional flags.
715                  */
716                 break;
717         default:
718                 /*
719                  * If not on-disk and not deleted, set both dirty flags
720                  * to force an initial record to be written.
721                  */
722                 ip->flags |= HAMMER_INODE_RDIRTY | HAMMER_INODE_DDIRTY;
723                 break;
724         }
725
726         /*
727          * If RDIRTY or DDIRTY is set, write out a new record.  If the inode
728          * is already on-disk the old record is marked as deleted.
729          *
730          * If DELETED is set hammer_update_inode() will delete the existing
731          * record without writing out a new one.
732          *
733          * If *ONLY* the ITIMES flag is set we can update the record in-place.
734          */
735         if ((ip->flags & (HAMMER_INODE_RDIRTY | HAMMER_INODE_DDIRTY |
736                          HAMMER_INODE_ITIMES | HAMMER_INODE_DELETED)) ==
737             HAMMER_INODE_ITIMES) {
738                 error = hammer_update_itimes(&trans, ip);
739         } else
740         if (ip->flags & (HAMMER_INODE_RDIRTY | HAMMER_INODE_DDIRTY |
741                          HAMMER_INODE_ITIMES | HAMMER_INODE_DELETED)) {
742                 error = hammer_update_inode(&trans, ip);
743         }
744         hammer_commit_transaction(&trans);
745         hammer_unlock(&ip->lock);
746         return(error);
747 }
748