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