HAMMER 6/many - memory->disk flush, single-cluster sync to disk, more vnops.
[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.7 2007/11/26 05:03:11 dillon Exp $
35  */
36
37 #include "hammer.h"
38 #include <sys/buf.h>
39 #include <sys/buf2.h>
40
41 int
42 hammer_vop_inactive(struct vop_inactive_args *ap)
43 {
44         struct hammer_inode *ip = VTOI(ap->a_vp);
45
46         /*
47          * Degenerate case
48          */
49         if (ip == NULL) {
50                 vrecycle(ap->a_vp);
51                 return(0);
52         }
53
54         /*
55          * If the inode no longer has any references we recover its
56          * in-memory resources immediately.
57          */
58         if (ip->ino_rec.ino_nlinks == 0 &&
59             (ip->hmp->mp->mnt_flag & MNT_RDONLY) == 0) {
60                 hammer_sync_inode(ip, MNT_NOWAIT, 1);
61         }
62         return(0);
63 }
64
65 int
66 hammer_vop_reclaim(struct vop_reclaim_args *ap)
67 {
68         struct hammer_inode *ip;
69         struct vnode *vp;
70
71         vp = ap->a_vp;
72
73         /*
74          * Release the vnode association and ask that the inode be flushed.
75          */
76         if ((ip = vp->v_data) != NULL) {
77                 vp->v_data = NULL;
78                 ip->vp = NULL;
79                 hammer_rel_inode(ip, 1);
80         }
81         return(0);
82 }
83
84 /*
85  * Obtain a vnode for the specified inode number.  An exclusively locked
86  * vnode is returned.
87  */
88 int
89 hammer_vfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
90 {
91         struct hammer_mount *hmp = (void *)mp->mnt_data;
92         struct hammer_inode *ip;
93         int error;
94
95         /*
96          * Get/allocate the hammer_inode structure.  The structure must be
97          * unlocked while we manipulate the related vnode to avoid a
98          * deadlock.
99          */
100         ip = hammer_get_inode(hmp, ino, &error);
101         if (ip == NULL) {
102                 *vpp = NULL;
103                 return(error);
104         }
105         error = hammer_get_vnode(ip, LK_EXCLUSIVE, vpp);
106         hammer_rel_inode(ip, 0);
107         return (error);
108 }
109
110 /*
111  * Return a locked vnode for the specified inode.  The inode must be
112  * referenced but NOT LOCKED on entry and will remain referenced on
113  * return.
114  */
115 int
116 hammer_get_vnode(struct hammer_inode *ip, int lktype, struct vnode **vpp)
117 {
118         struct vnode *vp;
119         int error = 0;
120
121         for (;;) {
122                 if ((vp = ip->vp) == NULL) {
123                         error = getnewvnode(VT_HAMMER, ip->hmp->mp, vpp, 0, 0);
124                         if (error)
125                                 break;
126                         hammer_lock_ex(&ip->lock);
127                         if (ip->vp != NULL) {
128                                 hammer_unlock(&ip->lock);
129                                 vp->v_type = VBAD;
130                                 vx_put(vp);
131                                 continue;
132                         }
133                         hammer_ref(&ip->lock);
134                         vp = *vpp;
135                         ip->vp = vp;
136                         vp->v_type = hammer_get_vnode_type(
137                                             ip->ino_rec.base.base.obj_type);
138                         vp->v_data = (void *)ip;
139                         /* vnode locked by getnewvnode() */
140                         /* make related vnode dirty if inode dirty? */
141                         hammer_unlock(&ip->lock);
142                         if (vp->v_type == VREG)
143                                 vinitvmio(vp, ip->ino_rec.ino_size);
144                         break;
145                 }
146
147                 /*
148                  * loop if the vget fails (aka races), or if the vp
149                  * no longer matches ip->vp.
150                  */
151                 if (vget(vp, LK_EXCLUSIVE) == 0) {
152                         if (vp == ip->vp)
153                                 break;
154                         vput(vp);
155                 }
156         }
157         *vpp = vp;
158         return(error);
159 }
160
161 /*
162  * Acquire a HAMMER inode.  The returned inode is not locked.  These functions
163  * do not attach or detach the related vnode (use hammer_get_vnode() for
164  * that).
165  */
166 struct hammer_inode *
167 hammer_get_inode(struct hammer_mount *hmp, u_int64_t obj_id, int *errorp)
168 {
169         struct hammer_inode_info iinfo;
170         struct hammer_cursor cursor;
171         struct hammer_inode *ip;
172
173         /*
174          * Determine if we already have an inode cached.  If we do then
175          * we are golden.
176          */
177         iinfo.obj_id = obj_id;
178         iinfo.obj_asof = HAMMER_MAX_TID;        /* XXX */
179 loop:
180         ip = hammer_ino_rb_tree_RB_LOOKUP_INFO(&hmp->rb_inos_root, &iinfo);
181         if (ip) {
182                 hammer_ref(&ip->lock);
183                 *errorp = 0;
184                 return(ip);
185         }
186
187         ip = kmalloc(sizeof(*ip), M_HAMMER, M_WAITOK|M_ZERO);
188         ip->obj_id = obj_id;
189         ip->obj_asof = iinfo.obj_asof;
190         ip->hmp = hmp;
191         RB_INIT(&ip->rec_tree);
192
193         /*
194          * Locate the on-disk inode.
195          * If we do not have an inode cached search the HAMMER on-disk B-Tree
196          * for it.
197          */
198
199         hammer_init_cursor_hmp(&cursor, hmp);
200         cursor.key_beg.obj_id = ip->obj_id;
201         cursor.key_beg.key = 0;
202         cursor.key_beg.create_tid = iinfo.obj_asof;
203         cursor.key_beg.delete_tid = 0;
204         cursor.key_beg.rec_type = HAMMER_RECTYPE_INODE;
205         cursor.key_beg.obj_type = 0;
206         cursor.flags = HAMMER_CURSOR_GET_RECORD | HAMMER_CURSOR_GET_DATA;
207
208         *errorp = hammer_btree_lookup(&cursor);
209
210         /*
211          * On success the B-Tree lookup will hold the appropriate
212          * buffer cache buffers and provide a pointer to the requested
213          * information.  Copy the information to the in-memory inode.
214          */
215         if (*errorp == 0) {
216                 ip->ino_rec = cursor.record->inode;
217                 ip->ino_data = cursor.data->inode;
218         }
219         hammer_cache_node(cursor.node, &ip->cache);
220         hammer_done_cursor(&cursor);
221
222         /*
223          * On success load the inode's record and data and insert the
224          * inode into the B-Tree.  It is possible to race another lookup
225          * insertion of the same inode so deal with that condition too.
226          */
227         if (*errorp == 0) {
228                 hammer_ref(&ip->lock);
229                 if (RB_INSERT(hammer_ino_rb_tree, &hmp->rb_inos_root, ip)) {
230                         hammer_uncache_node(&ip->cache);
231                         hammer_unref(&ip->lock);
232                         kfree(ip, M_HAMMER);
233                         goto loop;
234                 }
235                 ip->flags |= HAMMER_INODE_ONDISK;
236         } else {
237                 kfree(ip, M_HAMMER);
238                 ip = NULL;
239         }
240         return (ip);
241 }
242
243 /*
244  * Create a new filesystem object, returning the inode in *ipp.  The
245  * returned inode will be referenced but not locked.
246  *
247  * The inode is created in-memory and will be delay-synchronized to the
248  * disk.
249  */
250 int
251 hammer_create_inode(hammer_transaction_t trans, struct vattr *vap,
252                     struct ucred *cred, hammer_inode_t dip,
253                     struct hammer_inode **ipp)
254 {
255         hammer_mount_t hmp;
256         hammer_inode_t ip;
257         uid_t xuid;
258
259         hmp = trans->hmp;
260         ip = kmalloc(sizeof(*ip), M_HAMMER, M_WAITOK|M_ZERO);
261         ip->obj_id = hammer_alloc_tid(trans);
262         KKASSERT(ip->obj_id != 0);
263         ip->obj_asof = HAMMER_MAX_TID;  /* XXX */
264         ip->hmp = hmp;
265         ip->flags = HAMMER_INODE_DDIRTY | HAMMER_INODE_RDIRTY |
266                     HAMMER_INODE_ITIMES;
267         ip->last_tid = trans->tid;
268
269         RB_INIT(&ip->rec_tree);
270
271         ip->ino_rec.ino_atime = trans->tid;
272         ip->ino_rec.ino_mtime = trans->tid;
273         ip->ino_rec.ino_size = 0;
274         ip->ino_rec.ino_nlinks = 0;
275         /* XXX */
276         kprintf("rootvol %p ondisk %p\n", hmp->rootvol, hmp->rootvol->ondisk);
277         ip->ino_rec.base.rec_id = hammer_alloc_recid(trans);
278         KKASSERT(ip->ino_rec.base.rec_id != 0);
279         ip->ino_rec.base.base.obj_id = ip->obj_id;
280         ip->ino_rec.base.base.key = 0;
281         ip->ino_rec.base.base.create_tid = trans->tid;
282         ip->ino_rec.base.base.delete_tid = 0;
283         ip->ino_rec.base.base.rec_type = HAMMER_RECTYPE_INODE;
284         ip->ino_rec.base.base.obj_type = hammer_get_obj_type(vap->va_type);
285
286         ip->ino_data.version = HAMMER_INODE_DATA_VERSION;
287         ip->ino_data.mode = vap->va_mode;
288         ip->ino_data.ctime = trans->tid;
289         ip->ino_data.parent_obj_id = (dip) ? dip->ino_rec.base.base.obj_id : 0;
290
291         /*
292          * Calculate default uid/gid and overwrite with information from
293          * the vap.
294          */
295         xuid = hammer_to_unix_xid(&dip->ino_data.uid);
296         ip->ino_data.gid = dip->ino_data.gid;
297         xuid = vop_helper_create_uid(hmp->mp, dip->ino_data.mode, xuid, cred,
298                                      &vap->va_mode);
299         ip->ino_data.mode = vap->va_mode;
300
301         if (vap->va_vaflags & VA_UID_UUID_VALID)
302                 ip->ino_data.uid = vap->va_uid_uuid;
303         else if (vap->va_uid != (uid_t)VNOVAL)
304                 hammer_guid_to_uuid(&ip->ino_data.uid, xuid);
305         if (vap->va_vaflags & VA_GID_UUID_VALID)
306                 ip->ino_data.gid = vap->va_gid_uuid;
307         else if (vap->va_gid != (gid_t)VNOVAL)
308                 hammer_guid_to_uuid(&ip->ino_data.gid, vap->va_gid);
309
310         hammer_ref(&ip->lock);
311         if (RB_INSERT(hammer_ino_rb_tree, &hmp->rb_inos_root, ip)) {
312                 hammer_unref(&ip->lock);
313                 panic("hammer_create_inode: duplicate obj_id %llx", ip->obj_id);
314         }
315         *ipp = ip;
316         return(0);
317 }
318
319 int
320 hammer_update_inode(hammer_transaction_t trans, hammer_inode_t ip)
321 {
322         struct hammer_cursor cursor;
323         hammer_record_t record;
324         int error;
325
326         /*
327          * Locate the record on-disk and mark it as deleted
328          *
329          * XXX Update the inode record and data in-place if the retention
330          * policy allows it.
331          */
332         error = 0;
333
334         if (ip->flags & HAMMER_INODE_ONDISK) {
335                 hammer_init_cursor_ip(&cursor, ip);
336                 cursor.key_beg.obj_id = ip->obj_id;
337                 cursor.key_beg.key = 0;
338                 cursor.key_beg.create_tid = ip->obj_asof;
339                 cursor.key_beg.delete_tid = 0;
340                 cursor.key_beg.rec_type = HAMMER_RECTYPE_INODE;
341                 cursor.key_beg.obj_type = 0;
342                 cursor.flags = HAMMER_CURSOR_GET_RECORD;
343
344                 error = hammer_btree_lookup(&cursor);
345
346                 if (error == 0) {
347                         cursor.record->base.base.delete_tid = trans->tid;
348                         hammer_modify_buffer(cursor.record_buffer);
349                 }
350                 hammer_cache_node(cursor.node, &ip->cache);
351                 hammer_done_cursor(&cursor);
352         }
353
354         /*
355          * Write out a new record if the in-memory inode is not marked
356          * as having been deleted.
357          */
358         if (error == 0 && (ip->flags & HAMMER_INODE_DELETED) == 0) { 
359                 record = hammer_alloc_mem_record(trans, ip);
360                 record->rec.inode = ip->ino_rec;
361                 record->rec.inode.base.base.create_tid = trans->tid;
362                 record->rec.inode.base.data_len = sizeof(ip->ino_data);
363                 record->data = (void *)&ip->ino_data;
364                 error = hammer_ip_sync_record(record);
365                 hammer_free_mem_record(record);
366                 ip->flags &= ~(HAMMER_INODE_RDIRTY|HAMMER_INODE_DDIRTY);
367                 ip->flags |= HAMMER_INODE_ONDISK;
368         }
369         return(error);
370 }
371
372 /*
373  * Release a reference on an inode and unload it if told to flush.
374  */
375 void
376 hammer_rel_inode(struct hammer_inode *ip, int flush)
377 {
378         hammer_unref(&ip->lock);
379         if (flush || ip->ino_rec.ino_nlinks == 0)
380                 ip->flags |= HAMMER_INODE_FLUSH;
381         if (ip->lock.refs == 0 && (ip->flags & HAMMER_INODE_FLUSH))
382                 hammer_unload_inode(ip, NULL);
383 }
384
385 /*
386  * Unload and destroy the specified inode.
387  *
388  * (called via RB_SCAN)
389  */
390 int
391 hammer_unload_inode(struct hammer_inode *ip, void *data __unused)
392 {
393         int error;
394
395         KASSERT(ip->lock.refs == 0,
396                 ("hammer_unload_inode: %d refs\n", ip->lock.refs));
397         KKASSERT(ip->vp == NULL);
398         hammer_ref(&ip->lock);
399
400         error = hammer_sync_inode(ip, MNT_WAIT, 1);
401         if (error)
402                 kprintf("hammer_sync_inode failed error %d\n", error);
403
404         RB_REMOVE(hammer_ino_rb_tree, &ip->hmp->rb_inos_root, ip);
405
406         hammer_uncache_node(&ip->cache);
407         kfree(ip, M_HAMMER);
408         return(0);
409 }
410
411 /*
412  * A transaction has modified an inode, requiring a new record and possibly
413  * also data to be written out.
414  */
415 void
416 hammer_modify_inode(struct hammer_transaction *trans,
417                     struct hammer_inode *ip, int flags)
418 {
419         ip->flags |= flags;
420         if (flags & HAMMER_INODE_TID)
421                 ip->last_tid = trans->tid;
422 }
423
424 /*
425  * Sync any dirty buffers and records associated with an inode.  The
426  * inode's last_tid field is used as the transaction id for the sync,
427  * overriding any intermediate TIDs that were used for records.  Note
428  * that the dirty buffer cache buffers do not have any knowledge of
429  * the transaction id they were modified under.
430  */
431 static int
432 hammer_sync_inode_callback(hammer_record_t rec, void *data __unused)
433 {
434         int error;
435
436         error = 0;
437         if ((rec->flags & HAMMER_RECF_DELETED) == 0)
438                 error = hammer_ip_sync_record(rec);
439
440         if (error) {
441                 kprintf("hammer_sync_inode_callback: sync failed rec %p\n",
442                         rec);
443                 return(-1);
444         }
445         hammer_free_mem_record(rec);
446         return(0);
447 }
448
449 /*
450  * XXX error handling
451  */
452 int
453 hammer_sync_inode(hammer_inode_t ip, int waitfor, int handle_delete)
454 {
455         struct hammer_transaction trans;
456         int error;
457         int r;
458
459         hammer_lock_ex(&ip->lock);
460         hammer_start_transaction(&trans, ip->hmp);
461
462         /*
463          * If the inode has been deleted (nlinks == 0), and the OS no longer
464          * has any references to it (handle_delete != 0), clean up in-memory
465          * data.
466          *
467          * NOTE: We do not set the RDIRTY flag when updating the delete_tid,
468          * setting HAMMER_INODE_DELETED takes care of it.
469          */
470         if (ip->ino_rec.ino_nlinks == 0 && handle_delete) {
471                 if (ip->vp)
472                         vtruncbuf(ip->vp, 0, HAMMER_BUFSIZE);
473                 error = hammer_ip_delete_range(&trans, ip,
474                                                HAMMER_MIN_KEY, HAMMER_MAX_KEY);
475                 KKASSERT(RB_EMPTY(&ip->rec_tree));
476                 ip->ino_rec.base.base.delete_tid = trans.tid;
477                 hammer_modify_inode(&trans, ip,
478                                     HAMMER_INODE_DELETED | HAMMER_INODE_TID);
479         }
480
481         /*
482          * Sync the buffer cache
483          */
484         if (ip->vp != NULL)
485                 error = vfsync(ip->vp, waitfor, 1, NULL, NULL);
486         else
487                 error = 0;
488
489         /*
490          * Now sync related records
491          */
492         if (error == 0) {
493                 r = RB_SCAN(hammer_rec_rb_tree, &ip->rec_tree, NULL,
494                             hammer_sync_inode_callback, NULL);
495                 if (r < 0)
496                         error = EIO;
497         }
498
499         /*
500          * Now update the inode's on-disk inode-data and/or on-disk record.
501          */
502         switch(ip->flags & (HAMMER_INODE_DELETED|HAMMER_INODE_ONDISK)) {
503         case HAMMER_INODE_DELETED|HAMMER_INODE_ONDISK:
504                 /*
505                  * If deleted and on-disk, don't set any additional flags.
506                  * the delete flag takes care of things.
507                  */
508                 break;
509         case HAMMER_INODE_DELETED:
510                 /*
511                  * Take care of the case where a deleted inode was never
512                  * flushed to the disk in the first place.
513                  */
514                 ip->flags &= ~(HAMMER_INODE_RDIRTY|HAMMER_INODE_DDIRTY);
515                 while (RB_ROOT(&ip->rec_tree))
516                         hammer_free_mem_record(RB_ROOT(&ip->rec_tree));
517                 break;
518         case HAMMER_INODE_ONDISK:
519                 /*
520                  * If already on-disk, do not set any additional flags.
521                  */
522                 break;
523         default:
524                 /*
525                  * If not on-disk and not deleted, set both dirty flags
526                  * to force an initial record to be written.
527                  */
528                 ip->flags |= HAMMER_INODE_RDIRTY | HAMMER_INODE_DDIRTY;
529                 break;
530         }
531
532         /*
533          * If RDIRTY or DDIRTY is set, write out a new record.  If the
534          * inode is already on-disk, the old record is marked as deleted.
535          */
536         if (ip->flags & (HAMMER_INODE_RDIRTY | HAMMER_INODE_DDIRTY |
537                          HAMMER_INODE_DELETED)) {
538                 error = hammer_update_inode(&trans, ip);
539         }
540         hammer_commit_transaction(&trans);
541         hammer_unlock(&ip->lock);
542         return(error);
543 }
544
545 /*
546  * Access the filesystem buffer containing the cluster-relative byte
547  * offset, validate the buffer type, load *bufferp and return a
548  * pointer to the requested data.  The buffer is reference and locked on
549  * return.
550  *
551  * If buf_type is 0 the buffer is assumed to be a pure-data buffer and
552  * no type or crc check is performed.
553  *
554  * If *bufferp is not NULL on entry it is assumed to contain a locked
555  * and referenced buffer which will then be replaced.
556  *
557  * If the caller is holding another unrelated buffer locked it must be
558  * passed in reorderbuf so we can properly order buffer locks.
559  *
560  * XXX add a flag for the buffer type and check the CRC here XXX
561  */
562 void *
563 hammer_bread(hammer_cluster_t cluster, int32_t cloff,
564              u_int64_t buf_type, int *errorp,
565              struct hammer_buffer **bufferp)
566 {
567         hammer_buffer_t buffer;
568         int32_t buf_no;
569         int32_t buf_off;
570
571         /*
572          * Load the correct filesystem buffer, replacing *bufferp.
573          */
574         buf_no = cloff / HAMMER_BUFSIZE;
575         buffer = *bufferp;
576         if (buffer == NULL || buffer->cluster != cluster ||
577             buffer->buf_no != buf_no) {
578                 if (buffer) {
579                         /*hammer_unlock(&buffer->io.lock);*/
580                         hammer_rel_buffer(buffer, 0);
581                 }
582                 buffer = hammer_get_buffer(cluster, buf_no, 0, errorp);
583                 *bufferp = buffer;
584                 if (buffer == NULL)
585                         return(NULL);
586                 /*hammer_lock_ex(&buffer->io.lock);*/
587         }
588
589         /*
590          * Validate the buffer type
591          */
592         buf_off = cloff & HAMMER_BUFMASK;
593         if (buf_type) {
594                 if (buf_type != buffer->ondisk->head.buf_type) {
595                         kprintf("BUFFER HEAD TYPE MISMATCH %llx %llx\n",
596                                 buf_type, buffer->ondisk->head.buf_type);
597                         *errorp = EIO;
598                         return(NULL);
599                 }
600                 if (buf_off < sizeof(buffer->ondisk->head)) {
601                         kprintf("BUFFER OFFSET TOO LOW %d\n", buf_off);
602                         *errorp = EIO;
603                         return(NULL);
604                 }
605         }
606
607         /*
608          * Return a pointer to the buffer data.
609          */
610         *errorp = 0;
611         return((char *)buffer->ondisk + buf_off);
612 }
613