Remove bogus non-reentrant "temporary" implementation of gethostbyaddr_r()
[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 *
d26d0ae9 34 * $DragonFly: src/sys/vfs/hammer/hammer_inode.c,v 1.12 2007/12/29 09:01:27 dillon Exp $
427e5fc6
MD
35 */
36
37#include "hammer.h"
38#include <sys/buf.h>
39#include <sys/buf2.h>
40
427e5fc6
MD
41int
42hammer_vop_inactive(struct vop_inactive_args *ap)
43{
66325755 44 struct hammer_inode *ip = VTOI(ap->a_vp);
27ea2398 45
c0ade690
MD
46 /*
47 * Degenerate case
48 */
49 if (ip == NULL) {
66325755 50 vrecycle(ap->a_vp);
c0ade690
MD
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 }
427e5fc6
MD
62 return(0);
63}
64
65int
66hammer_vop_reclaim(struct vop_reclaim_args *ap)
67{
427e5fc6
MD
68 struct hammer_inode *ip;
69 struct vnode *vp;
70
71 vp = ap->a_vp;
c0ade690
MD
72
73 /*
74 * Release the vnode association and ask that the inode be flushed.
75 */
a89aec1b
MD
76 if ((ip = vp->v_data) != NULL) {
77 vp->v_data = NULL;
78 ip->vp = NULL;
79 hammer_rel_inode(ip, 1);
80 }
427e5fc6
MD
81 return(0);
82}
83
84/*
66325755
MD
85 * Obtain a vnode for the specified inode number. An exclusively locked
86 * vnode is returned.
427e5fc6
MD
87 */
88int
89hammer_vfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
90{
91 struct hammer_mount *hmp = (void *)mp->mnt_data;
66325755
MD
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 */
7f7c1f84 100 ip = hammer_get_inode(hmp, ino, hmp->asof, &error);
66325755
MD
101 if (ip == NULL) {
102 *vpp = NULL;
103 return(error);
104 }
66325755 105 error = hammer_get_vnode(ip, LK_EXCLUSIVE, vpp);
a89aec1b 106 hammer_rel_inode(ip, 0);
66325755
MD
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 */
115int
116hammer_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;
8cd0a023
MD
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;
66325755 132 }
8cd0a023
MD
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);
a89aec1b
MD
142 if (vp->v_type == VREG)
143 vinitvmio(vp, ip->ino_rec.ino_size);
8cd0a023
MD
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);
66325755
MD
155 }
156 }
a89aec1b 157 *vpp = vp;
66325755
MD
158 return(error);
159}
160
161/*
8cd0a023
MD
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).
66325755
MD
165 */
166struct hammer_inode *
7f7c1f84
MD
167hammer_get_inode(struct hammer_mount *hmp, u_int64_t obj_id, hammer_tid_t asof,
168 int *errorp)
66325755 169{
427e5fc6 170 struct hammer_inode_info iinfo;
8cd0a023 171 struct hammer_cursor cursor;
427e5fc6 172 struct hammer_inode *ip;
427e5fc6
MD
173
174 /*
175 * Determine if we already have an inode cached. If we do then
176 * we are golden.
177 */
66325755 178 iinfo.obj_id = obj_id;
7f7c1f84 179 iinfo.obj_asof = asof;
427e5fc6
MD
180loop:
181 ip = hammer_ino_rb_tree_RB_LOOKUP_INFO(&hmp->rb_inos_root, &iinfo);
182 if (ip) {
8cd0a023 183 hammer_ref(&ip->lock);
66325755
MD
184 *errorp = 0;
185 return(ip);
427e5fc6
MD
186 }
187
427e5fc6 188 ip = kmalloc(sizeof(*ip), M_HAMMER, M_WAITOK|M_ZERO);
66325755 189 ip->obj_id = obj_id;
27ea2398 190 ip->obj_asof = iinfo.obj_asof;
66325755 191 ip->hmp = hmp;
8cd0a023 192 RB_INIT(&ip->rec_tree);
427e5fc6
MD
193
194 /*
8cd0a023 195 * Locate the on-disk inode.
427e5fc6
MD
196 * If we do not have an inode cached search the HAMMER on-disk B-Tree
197 * for it.
198 */
427e5fc6 199
8cd0a023
MD
200 hammer_init_cursor_hmp(&cursor, hmp);
201 cursor.key_beg.obj_id = ip->obj_id;
202 cursor.key_beg.key = 0;
203 cursor.key_beg.create_tid = iinfo.obj_asof;
204 cursor.key_beg.delete_tid = 0;
205 cursor.key_beg.rec_type = HAMMER_RECTYPE_INODE;
206 cursor.key_beg.obj_type = 0;
a89aec1b 207 cursor.flags = HAMMER_CURSOR_GET_RECORD | HAMMER_CURSOR_GET_DATA;
8cd0a023
MD
208
209 *errorp = hammer_btree_lookup(&cursor);
427e5fc6
MD
210
211 /*
212 * On success the B-Tree lookup will hold the appropriate
213 * buffer cache buffers and provide a pointer to the requested
214 * information. Copy the information to the in-memory inode.
215 */
66325755 216 if (*errorp == 0) {
8cd0a023
MD
217 ip->ino_rec = cursor.record->inode;
218 ip->ino_data = cursor.data->inode;
427e5fc6 219 }
8cd0a023
MD
220 hammer_cache_node(cursor.node, &ip->cache);
221 hammer_done_cursor(&cursor);
427e5fc6
MD
222
223 /*
224 * On success load the inode's record and data and insert the
225 * inode into the B-Tree. It is possible to race another lookup
226 * insertion of the same inode so deal with that condition too.
227 */
66325755 228 if (*errorp == 0) {
8cd0a023 229 hammer_ref(&ip->lock);
427e5fc6 230 if (RB_INSERT(hammer_ino_rb_tree, &hmp->rb_inos_root, ip)) {
8cd0a023
MD
231 hammer_uncache_node(&ip->cache);
232 hammer_unref(&ip->lock);
427e5fc6
MD
233 kfree(ip, M_HAMMER);
234 goto loop;
235 }
c0ade690 236 ip->flags |= HAMMER_INODE_ONDISK;
427e5fc6 237 } else {
66325755
MD
238 kfree(ip, M_HAMMER);
239 ip = NULL;
427e5fc6 240 }
66325755
MD
241 return (ip);
242}
243
8cd0a023
MD
244/*
245 * Create a new filesystem object, returning the inode in *ipp. The
246 * returned inode will be referenced but not locked.
247 *
248 * The inode is created in-memory and will be delay-synchronized to the
249 * disk.
250 */
251int
a89aec1b
MD
252hammer_create_inode(hammer_transaction_t trans, struct vattr *vap,
253 struct ucred *cred, hammer_inode_t dip,
8cd0a023 254 struct hammer_inode **ipp)
66325755 255{
a89aec1b
MD
256 hammer_mount_t hmp;
257 hammer_inode_t ip;
6b4f890b 258 uid_t xuid;
66325755 259
8cd0a023
MD
260 hmp = trans->hmp;
261 ip = kmalloc(sizeof(*ip), M_HAMMER, M_WAITOK|M_ZERO);
a89aec1b 262 ip->obj_id = hammer_alloc_tid(trans);
8cd0a023 263 KKASSERT(ip->obj_id != 0);
7f7c1f84 264 ip->obj_asof = hmp->asof;
8cd0a023
MD
265 ip->hmp = hmp;
266 ip->flags = HAMMER_INODE_DDIRTY | HAMMER_INODE_RDIRTY |
267 HAMMER_INODE_ITIMES;
268 ip->last_tid = trans->tid;
269
270 RB_INIT(&ip->rec_tree);
271
272 ip->ino_rec.ino_atime = trans->tid;
273 ip->ino_rec.ino_mtime = trans->tid;
274 ip->ino_rec.ino_size = 0;
275 ip->ino_rec.ino_nlinks = 0;
276 /* XXX */
a89aec1b 277 ip->ino_rec.base.rec_id = hammer_alloc_recid(trans);
8cd0a023
MD
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;
6b4f890b
MD
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
8cd0a023
MD
301 if (vap->va_vaflags & VA_UID_UUID_VALID)
302 ip->ino_data.uid = vap->va_uid_uuid;
6b4f890b
MD
303 else if (vap->va_uid != (uid_t)VNOVAL)
304 hammer_guid_to_uuid(&ip->ino_data.uid, xuid);
8cd0a023
MD
305 if (vap->va_vaflags & VA_GID_UUID_VALID)
306 ip->ino_data.gid = vap->va_gid_uuid;
6b4f890b 307 else if (vap->va_gid != (gid_t)VNOVAL)
8cd0a023
MD
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);
a89aec1b 313 panic("hammer_create_inode: duplicate obj_id %llx", ip->obj_id);
8cd0a023
MD
314 }
315 *ipp = ip;
316 return(0);
66325755
MD
317}
318
c0ade690 319int
7f7c1f84 320hammer_update_inode(hammer_inode_t ip)
c0ade690
MD
321{
322 struct hammer_cursor cursor;
d26d0ae9 323 struct hammer_cursor *spike = NULL;
c0ade690
MD
324 hammer_record_t record;
325 int error;
326
327 /*
76376933 328 * Locate the record on-disk and mark it as deleted. Both the B-Tree
195c19a1
MD
329 * node and the record must be marked deleted. The record may or
330 * may not be physically deleted, depending on the retention policy.
76376933 331 *
195c19a1
MD
332 * If the inode has already been deleted on-disk we have nothing
333 * to do.
c0ade690
MD
334 *
335 * XXX Update the inode record and data in-place if the retention
336 * policy allows it.
337 */
d26d0ae9 338retry:
c0ade690
MD
339 error = 0;
340
76376933
MD
341 if ((ip->flags & (HAMMER_INODE_ONDISK|HAMMER_INODE_DELONDISK)) ==
342 HAMMER_INODE_ONDISK) {
c0ade690
MD
343 hammer_init_cursor_ip(&cursor, ip);
344 cursor.key_beg.obj_id = ip->obj_id;
345 cursor.key_beg.key = 0;
346 cursor.key_beg.create_tid = ip->obj_asof;
347 cursor.key_beg.delete_tid = 0;
348 cursor.key_beg.rec_type = HAMMER_RECTYPE_INODE;
349 cursor.key_beg.obj_type = 0;
350 cursor.flags = HAMMER_CURSOR_GET_RECORD;
351
352 error = hammer_btree_lookup(&cursor);
353
354 if (error == 0) {
195c19a1
MD
355 error = hammer_ip_delete_record(&cursor, ip->last_tid);
356 if (error == 0)
357 ip->flags |= HAMMER_INODE_DELONDISK;
c0ade690
MD
358 }
359 hammer_cache_node(cursor.node, &ip->cache);
360 hammer_done_cursor(&cursor);
361 }
362
363 /*
364 * Write out a new record if the in-memory inode is not marked
fbc6e32a
MD
365 * as having been deleted. Update our inode statistics if this
366 * is the first application of the inode on-disk.
76376933
MD
367 *
368 * If the inode has been deleted permanently, HAMMER_INODE_DELONDISK
369 * will remain set and prevent further updates.
c0ade690
MD
370 */
371 if (error == 0 && (ip->flags & HAMMER_INODE_DELETED) == 0) {
7f7c1f84 372 record = hammer_alloc_mem_record(ip);
c0ade690 373 record->rec.inode = ip->ino_rec;
7f7c1f84 374 record->rec.inode.base.base.create_tid = ip->last_tid;
c0ade690
MD
375 record->rec.inode.base.data_len = sizeof(ip->ino_data);
376 record->data = (void *)&ip->ino_data;
d26d0ae9
MD
377 error = hammer_ip_sync_record(record, &spike);
378 hammer_drop_mem_record(record, 1);
379 if (error == ENOSPC) {
380 error = hammer_spike(&spike);
381 if (error == 0)
382 goto retry;
383 }
384 KKASSERT(spike == NULL);
385 if (error == 0) {
386 ip->flags &= ~(HAMMER_INODE_RDIRTY|HAMMER_INODE_DDIRTY|
387 HAMMER_INODE_DELONDISK);
388 if ((ip->flags & HAMMER_INODE_ONDISK) == 0) {
389 ++ip->hmp->rootvol->ondisk->vol0_stat_inodes;
390 hammer_modify_volume(ip->hmp->rootvol);
391 ip->flags |= HAMMER_INODE_ONDISK;
392 }
fbc6e32a 393 }
c0ade690 394 }
d26d0ae9
MD
395 if (error == 0)
396 ip->flags &= ~HAMMER_INODE_TID;
c0ade690
MD
397 return(error);
398}
399
a89aec1b 400/*
6b4f890b 401 * Release a reference on an inode and unload it if told to flush.
a89aec1b 402 */
66325755 403void
a89aec1b 404hammer_rel_inode(struct hammer_inode *ip, int flush)
66325755
MD
405{
406 hammer_unref(&ip->lock);
6b4f890b 407 if (flush || ip->ino_rec.ino_nlinks == 0)
a89aec1b
MD
408 ip->flags |= HAMMER_INODE_FLUSH;
409 if (ip->lock.refs == 0 && (ip->flags & HAMMER_INODE_FLUSH))
410 hammer_unload_inode(ip, NULL);
427e5fc6
MD
411}
412
27ea2398 413/*
8cd0a023
MD
414 * Unload and destroy the specified inode.
415 *
27ea2398
MD
416 * (called via RB_SCAN)
417 */
418int
66325755 419hammer_unload_inode(struct hammer_inode *ip, void *data __unused)
27ea2398 420{
c0ade690
MD
421 int error;
422
a89aec1b
MD
423 KASSERT(ip->lock.refs == 0,
424 ("hammer_unload_inode: %d refs\n", ip->lock.refs));
8cd0a023
MD
425 KKASSERT(ip->vp == NULL);
426 hammer_ref(&ip->lock);
6b4f890b 427
c0ade690
MD
428 error = hammer_sync_inode(ip, MNT_WAIT, 1);
429 if (error)
430 kprintf("hammer_sync_inode failed error %d\n", error);
6b4f890b 431
66325755 432 RB_REMOVE(hammer_ino_rb_tree, &ip->hmp->rb_inos_root, ip);
8cd0a023
MD
433
434 hammer_uncache_node(&ip->cache);
27ea2398
MD
435 kfree(ip, M_HAMMER);
436 return(0);
437}
438
427e5fc6 439/*
66325755
MD
440 * A transaction has modified an inode, requiring a new record and possibly
441 * also data to be written out.
7f7c1f84
MD
442 *
443 * last_tid is the TID to use for the disk sync.
427e5fc6 444 */
66325755
MD
445void
446hammer_modify_inode(struct hammer_transaction *trans,
447 struct hammer_inode *ip, int flags)
427e5fc6 448{
7f7c1f84 449 if ((flags & HAMMER_INODE_TID) && (ip->flags & HAMMER_INODE_TID) == 0) {
c0ade690 450 ip->last_tid = trans->tid;
7f7c1f84
MD
451 }
452 ip->flags |= flags;
c0ade690
MD
453}
454
455/*
456 * Sync any dirty buffers and records associated with an inode. The
457 * inode's last_tid field is used as the transaction id for the sync,
458 * overriding any intermediate TIDs that were used for records. Note
459 * that the dirty buffer cache buffers do not have any knowledge of
460 * the transaction id they were modified under.
d26d0ae9
MD
461 *
462 * If we can't sync due to a cluster becoming full the spike structure
463 * will be filled in and ENOSPC returned. We must return -ENOSPC to
464 * terminate the RB_SCAN.
c0ade690
MD
465 */
466static int
d26d0ae9 467hammer_sync_inode_callback(hammer_record_t rec, void *data)
c0ade690 468{
d26d0ae9 469 struct hammer_cursor **spike = data;
c0ade690
MD
470 int error;
471
d26d0ae9
MD
472 hammer_ref(&rec->lock);
473 hammer_lock_ex(&rec->lock);
c0ade690 474 if ((rec->flags & HAMMER_RECF_DELETED) == 0)
d26d0ae9
MD
475 error = hammer_ip_sync_record(rec, spike);
476 else
477 error = 0;
478
479 if (error == ENOSPC) {
480 hammer_drop_mem_record(rec, 0);
481 return(-error);
482 }
c0ade690
MD
483
484 if (error) {
485 kprintf("hammer_sync_inode_callback: sync failed rec %p\n",
486 rec);
d26d0ae9
MD
487 hammer_drop_mem_record(rec, 0);
488 return(-error);
c0ade690 489 }
d26d0ae9 490 hammer_drop_mem_record(rec, 1); /* ref & lock eaten by call */
c0ade690
MD
491 return(0);
492}
493
494/*
495 * XXX error handling
496 */
497int
498hammer_sync_inode(hammer_inode_t ip, int waitfor, int handle_delete)
499{
500 struct hammer_transaction trans;
d26d0ae9 501 struct hammer_cursor *spike = NULL;
c0ade690 502 int error;
c0ade690
MD
503
504 hammer_lock_ex(&ip->lock);
505 hammer_start_transaction(&trans, ip->hmp);
506
507 /*
508 * If the inode has been deleted (nlinks == 0), and the OS no longer
509 * has any references to it (handle_delete != 0), clean up in-memory
510 * data.
511 *
512 * NOTE: We do not set the RDIRTY flag when updating the delete_tid,
513 * setting HAMMER_INODE_DELETED takes care of it.
7f7c1f84
MD
514 *
515 * NOTE: Because we may sync records within this new transaction,
516 * force the inode update later on to use our transaction id or
517 * the delete_tid of the inode may be less then the create_tid of
518 * the inode update. XXX shouldn't happen but don't take the chance.
d26d0ae9
MD
519 *
520 * NOTE: The call to hammer_ip_delete_range() cannot return ENOSPC
521 * so we can pass a NULL spike structure, because no partial data
522 * deletion can occur (yet).
c0ade690
MD
523 */
524 if (ip->ino_rec.ino_nlinks == 0 && handle_delete) {
525 if (ip->vp)
526 vtruncbuf(ip->vp, 0, HAMMER_BUFSIZE);
527 error = hammer_ip_delete_range(&trans, ip,
d26d0ae9
MD
528 HAMMER_MIN_KEY, HAMMER_MAX_KEY,
529 NULL);
c0ade690 530 KKASSERT(RB_EMPTY(&ip->rec_tree));
7f7c1f84 531 ip->flags &= ~HAMMER_INODE_TID;
c0ade690
MD
532 ip->ino_rec.base.base.delete_tid = trans.tid;
533 hammer_modify_inode(&trans, ip,
534 HAMMER_INODE_DELETED | HAMMER_INODE_TID);
fbc6e32a
MD
535 --ip->hmp->rootvol->ondisk->vol0_stat_inodes;
536 hammer_modify_volume(ip->hmp->rootvol);
c0ade690
MD
537 }
538
539 /*
540 * Sync the buffer cache
541 */
542 if (ip->vp != NULL)
543 error = vfsync(ip->vp, waitfor, 1, NULL, NULL);
544 else
545 error = 0;
546
547 /*
548 * Now sync related records
549 */
d26d0ae9
MD
550 for (;;) {
551 error = RB_SCAN(hammer_rec_rb_tree, &ip->rec_tree, NULL,
552 hammer_sync_inode_callback, &spike);
553 KKASSERT(error <= 0);
554 if (error < 0)
555 error = -error;
556 if (error == ENOSPC) {
557 error = hammer_spike(&spike);
558 if (error == 0)
559 continue;
560 }
561 break;
c0ade690
MD
562 }
563
564 /*
565 * Now update the inode's on-disk inode-data and/or on-disk record.
566 */
567 switch(ip->flags & (HAMMER_INODE_DELETED|HAMMER_INODE_ONDISK)) {
568 case HAMMER_INODE_DELETED|HAMMER_INODE_ONDISK:
569 /*
570 * If deleted and on-disk, don't set any additional flags.
571 * the delete flag takes care of things.
572 */
573 break;
574 case HAMMER_INODE_DELETED:
575 /*
576 * Take care of the case where a deleted inode was never
577 * flushed to the disk in the first place.
578 */
579 ip->flags &= ~(HAMMER_INODE_RDIRTY|HAMMER_INODE_DDIRTY);
d26d0ae9
MD
580 while (RB_ROOT(&ip->rec_tree)) {
581 hammer_record_t rec = RB_ROOT(&ip->rec_tree);
582 hammer_ref(&rec->lock);
583 hammer_lock_ex(&rec->lock);
584 hammer_drop_mem_record(rec, 1);
585 }
c0ade690
MD
586 break;
587 case HAMMER_INODE_ONDISK:
588 /*
589 * If already on-disk, do not set any additional flags.
590 */
591 break;
592 default:
593 /*
594 * If not on-disk and not deleted, set both dirty flags
595 * to force an initial record to be written.
596 */
597 ip->flags |= HAMMER_INODE_RDIRTY | HAMMER_INODE_DDIRTY;
598 break;
599 }
600
601 /*
602 * If RDIRTY or DDIRTY is set, write out a new record. If the
603 * inode is already on-disk, the old record is marked as deleted.
604 */
605 if (ip->flags & (HAMMER_INODE_RDIRTY | HAMMER_INODE_DDIRTY |
606 HAMMER_INODE_DELETED)) {
7f7c1f84 607 error = hammer_update_inode(ip);
c0ade690
MD
608 }
609 hammer_commit_transaction(&trans);
610 hammer_unlock(&ip->lock);
611 return(error);
8cd0a023
MD
612}
613
427e5fc6
MD
614/*
615 * Access the filesystem buffer containing the cluster-relative byte
616 * offset, validate the buffer type, load *bufferp and return a
8cd0a023
MD
617 * pointer to the requested data. The buffer is reference and locked on
618 * return.
427e5fc6
MD
619 *
620 * If buf_type is 0 the buffer is assumed to be a pure-data buffer and
621 * no type or crc check is performed.
622 *
8cd0a023
MD
623 * If *bufferp is not NULL on entry it is assumed to contain a locked
624 * and referenced buffer which will then be replaced.
625 *
626 * If the caller is holding another unrelated buffer locked it must be
627 * passed in reorderbuf so we can properly order buffer locks.
628 *
427e5fc6
MD
629 * XXX add a flag for the buffer type and check the CRC here XXX
630 */
631void *
8cd0a023
MD
632hammer_bread(hammer_cluster_t cluster, int32_t cloff,
633 u_int64_t buf_type, int *errorp,
634 struct hammer_buffer **bufferp)
427e5fc6 635{
8cd0a023 636 hammer_buffer_t buffer;
427e5fc6
MD
637 int32_t buf_no;
638 int32_t buf_off;
639
640 /*
641 * Load the correct filesystem buffer, replacing *bufferp.
642 */
643 buf_no = cloff / HAMMER_BUFSIZE;
644 buffer = *bufferp;
645 if (buffer == NULL || buffer->cluster != cluster ||
646 buffer->buf_no != buf_no) {
8cd0a023 647 if (buffer) {
c0ade690 648 /*hammer_unlock(&buffer->io.lock);*/
8cd0a023
MD
649 hammer_rel_buffer(buffer, 0);
650 }
427e5fc6
MD
651 buffer = hammer_get_buffer(cluster, buf_no, 0, errorp);
652 *bufferp = buffer;
653 if (buffer == NULL)
654 return(NULL);
c0ade690 655 /*hammer_lock_ex(&buffer->io.lock);*/
427e5fc6
MD
656 }
657
658 /*
8cd0a023 659 * Validate the buffer type
427e5fc6
MD
660 */
661 buf_off = cloff & HAMMER_BUFMASK;
662 if (buf_type) {
663 if (buf_type != buffer->ondisk->head.buf_type) {
27ea2398
MD
664 kprintf("BUFFER HEAD TYPE MISMATCH %llx %llx\n",
665 buf_type, buffer->ondisk->head.buf_type);
427e5fc6
MD
666 *errorp = EIO;
667 return(NULL);
668 }
669 if (buf_off < sizeof(buffer->ondisk->head)) {
27ea2398 670 kprintf("BUFFER OFFSET TOO LOW %d\n", buf_off);
427e5fc6
MD
671 *errorp = EIO;
672 return(NULL);
673 }
427e5fc6
MD
674 }
675
676 /*
677 * Return a pointer to the buffer data.
678 */
679 *errorp = 0;
680 return((char *)buffer->ondisk + buf_off);
681}
682