HAMMER Utilities: Enhance mount_hammer
[dragonfly.git] / sys / vfs / hammer / hammer_inode.c
CommitLineData
427e5fc6 1/*
b84de5af 2 * Copyright (c) 2007-2008 The DragonFly Project. All rights reserved.
427e5fc6
MD
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 *
e63644f0 34 * $DragonFly: src/sys/vfs/hammer/hammer_inode.c,v 1.63 2008/06/02 20:19:03 dillon Exp $
427e5fc6
MD
35 */
36
37#include "hammer.h"
869e8f55 38#include <vm/vm_extern.h>
427e5fc6
MD
39#include <sys/buf.h>
40#include <sys/buf2.h>
41
ec4e8497 42static int hammer_unload_inode(struct hammer_inode *ip);
1f07f686
MD
43static void hammer_flush_inode_core(hammer_inode_t ip, int flags);
44static int hammer_setup_child_callback(hammer_record_t rec, void *data);
1f07f686 45static int hammer_setup_parent_inodes(hammer_record_t record);
b84de5af 46
d113fda1
MD
47/*
48 * The kernel is not actively referencing this vnode but is still holding
49 * it cached.
b84de5af
MD
50 *
51 * This is called from the frontend.
d113fda1 52 */
427e5fc6
MD
53int
54hammer_vop_inactive(struct vop_inactive_args *ap)
55{
66325755 56 struct hammer_inode *ip = VTOI(ap->a_vp);
27ea2398 57
c0ade690
MD
58 /*
59 * Degenerate case
60 */
61 if (ip == NULL) {
66325755 62 vrecycle(ap->a_vp);
c0ade690
MD
63 return(0);
64 }
65
66 /*
1f07f686
MD
67 * If the inode no longer has visibility in the filesystem and is
68 * fairly clean, try to recycle it immediately. This can deadlock
69 * in vfsync() if we aren't careful.
4e97774c
MD
70 *
71 * Do not queue the inode to the flusher if we still have visibility,
72 * otherwise namespace calls such as chmod will unnecessarily generate
73 * multiple inode updates.
c0ade690 74 */
e8599db1 75 hammer_inode_unloadable_check(ip, 0);
4e97774c
MD
76 if (ip->ino_data.nlinks == 0) {
77 if (ip->flags & HAMMER_INODE_MODMASK)
78 hammer_flush_inode(ip, 0);
79 else
80 vrecycle(ap->a_vp);
81 }
427e5fc6
MD
82 return(0);
83}
84
d113fda1
MD
85/*
86 * Release the vnode association. This is typically (but not always)
1f07f686 87 * the last reference on the inode.
d113fda1 88 *
1f07f686
MD
89 * Once the association is lost we are on our own with regards to
90 * flushing the inode.
d113fda1 91 */
427e5fc6
MD
92int
93hammer_vop_reclaim(struct vop_reclaim_args *ap)
94{
427e5fc6
MD
95 struct hammer_inode *ip;
96 struct vnode *vp;
97
98 vp = ap->a_vp;
c0ade690 99
a89aec1b
MD
100 if ((ip = vp->v_data) != NULL) {
101 vp->v_data = NULL;
102 ip->vp = NULL;
ec4e8497 103 hammer_rel_inode(ip, 1);
a89aec1b 104 }
427e5fc6
MD
105 return(0);
106}
107
66325755
MD
108/*
109 * Return a locked vnode for the specified inode. The inode must be
110 * referenced but NOT LOCKED on entry and will remain referenced on
111 * return.
b84de5af
MD
112 *
113 * Called from the frontend.
66325755
MD
114 */
115int
e8599db1 116hammer_get_vnode(struct hammer_inode *ip, struct vnode **vpp)
66325755
MD
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;
11ad5ade
MD
136 vp->v_type =
137 hammer_get_vnode_type(ip->ino_data.obj_type);
7a04d74f 138
11ad5ade 139 switch(ip->ino_data.obj_type) {
7a04d74f
MD
140 case HAMMER_OBJTYPE_CDEV:
141 case HAMMER_OBJTYPE_BDEV:
142 vp->v_ops = &ip->hmp->mp->mnt_vn_spec_ops;
143 addaliasu(vp, ip->ino_data.rmajor,
144 ip->ino_data.rminor);
145 break;
146 case HAMMER_OBJTYPE_FIFO:
147 vp->v_ops = &ip->hmp->mp->mnt_vn_fifo_ops;
148 break;
149 default:
150 break;
151 }
42c7d26b
MD
152
153 /*
154 * Only mark as the root vnode if the ip is not
155 * historical, otherwise the VFS cache will get
156 * confused. The other half of the special handling
157 * is in hammer_vop_nlookupdotdot().
158 */
159 if (ip->obj_id == HAMMER_OBJID_ROOT &&
160 ip->obj_asof == ip->hmp->asof) {
7a04d74f 161 vp->v_flag |= VROOT;
42c7d26b 162 }
7a04d74f 163
8cd0a023
MD
164 vp->v_data = (void *)ip;
165 /* vnode locked by getnewvnode() */
166 /* make related vnode dirty if inode dirty? */
167 hammer_unlock(&ip->lock);
a89aec1b 168 if (vp->v_type == VREG)
11ad5ade 169 vinitvmio(vp, ip->ino_data.size);
8cd0a023
MD
170 break;
171 }
172
173 /*
174 * loop if the vget fails (aka races), or if the vp
175 * no longer matches ip->vp.
176 */
177 if (vget(vp, LK_EXCLUSIVE) == 0) {
178 if (vp == ip->vp)
179 break;
180 vput(vp);
66325755
MD
181 }
182 }
a89aec1b 183 *vpp = vp;
66325755
MD
184 return(error);
185}
186
187/*
8cd0a023
MD
188 * Acquire a HAMMER inode. The returned inode is not locked. These functions
189 * do not attach or detach the related vnode (use hammer_get_vnode() for
190 * that).
d113fda1
MD
191 *
192 * The flags argument is only applied for newly created inodes, and only
193 * certain flags are inherited.
b84de5af
MD
194 *
195 * Called from the frontend.
66325755
MD
196 */
197struct hammer_inode *
36f82b23 198hammer_get_inode(hammer_transaction_t trans, struct hammer_node **cache,
61aeeb33 199 u_int64_t obj_id, hammer_tid_t asof, int flags, int *errorp)
66325755 200{
36f82b23 201 hammer_mount_t hmp = trans->hmp;
427e5fc6 202 struct hammer_inode_info iinfo;
8cd0a023 203 struct hammer_cursor cursor;
427e5fc6 204 struct hammer_inode *ip;
427e5fc6
MD
205
206 /*
207 * Determine if we already have an inode cached. If we do then
208 * we are golden.
209 */
66325755 210 iinfo.obj_id = obj_id;
7f7c1f84 211 iinfo.obj_asof = asof;
427e5fc6
MD
212loop:
213 ip = hammer_ino_rb_tree_RB_LOOKUP_INFO(&hmp->rb_inos_root, &iinfo);
214 if (ip) {
8cd0a023 215 hammer_ref(&ip->lock);
66325755
MD
216 *errorp = 0;
217 return(ip);
427e5fc6
MD
218 }
219
427e5fc6 220 ip = kmalloc(sizeof(*ip), M_HAMMER, M_WAITOK|M_ZERO);
b3deaf57 221 ++hammer_count_inodes;
66325755 222 ip->obj_id = obj_id;
27ea2398 223 ip->obj_asof = iinfo.obj_asof;
66325755 224 ip->hmp = hmp;
d113fda1
MD
225 ip->flags = flags & HAMMER_INODE_RO;
226 if (hmp->ronly)
227 ip->flags |= HAMMER_INODE_RO;
a5fddc16 228 ip->trunc_off = 0x7FFFFFFFFFFFFFFFLL;
8cd0a023 229 RB_INIT(&ip->rec_tree);
059819e3 230 TAILQ_INIT(&ip->bio_list);
b84de5af 231 TAILQ_INIT(&ip->bio_alt_list);
1f07f686 232 TAILQ_INIT(&ip->target_list);
427e5fc6
MD
233
234 /*
8cd0a023 235 * Locate the on-disk inode.
427e5fc6 236 */
6a37e7e4 237retry:
4e17f465 238 hammer_init_cursor(trans, &cursor, cache, NULL);
2f85fa4d 239 cursor.key_beg.localization = HAMMER_LOCALIZE_INODE;
8cd0a023
MD
240 cursor.key_beg.obj_id = ip->obj_id;
241 cursor.key_beg.key = 0;
d5530d22 242 cursor.key_beg.create_tid = 0;
8cd0a023
MD
243 cursor.key_beg.delete_tid = 0;
244 cursor.key_beg.rec_type = HAMMER_RECTYPE_INODE;
245 cursor.key_beg.obj_type = 0;
d5530d22 246 cursor.asof = iinfo.obj_asof;
11ad5ade 247 cursor.flags = HAMMER_CURSOR_GET_LEAF | HAMMER_CURSOR_GET_DATA |
d5530d22 248 HAMMER_CURSOR_ASOF;
8cd0a023
MD
249
250 *errorp = hammer_btree_lookup(&cursor);
6a37e7e4
MD
251 if (*errorp == EDEADLK) {
252 hammer_done_cursor(&cursor);
253 goto retry;
254 }
427e5fc6
MD
255
256 /*
257 * On success the B-Tree lookup will hold the appropriate
258 * buffer cache buffers and provide a pointer to the requested
d113fda1
MD
259 * information. Copy the information to the in-memory inode
260 * and cache the B-Tree node to improve future operations.
427e5fc6 261 */
66325755 262 if (*errorp == 0) {
11ad5ade 263 ip->ino_leaf = cursor.node->ondisk->elms[cursor.index].leaf;
40043e7f 264 ip->ino_data = cursor.data->inode;
61aeeb33
MD
265 hammer_cache_node(cursor.node, &ip->cache[0]);
266 if (cache)
267 hammer_cache_node(cursor.node, cache);
427e5fc6 268 }
427e5fc6
MD
269
270 /*
271 * On success load the inode's record and data and insert the
272 * inode into the B-Tree. It is possible to race another lookup
273 * insertion of the same inode so deal with that condition too.
b3deaf57
MD
274 *
275 * The cursor's locked node interlocks against others creating and
276 * destroying ip while we were blocked.
427e5fc6 277 */
66325755 278 if (*errorp == 0) {
8cd0a023 279 hammer_ref(&ip->lock);
427e5fc6 280 if (RB_INSERT(hammer_ino_rb_tree, &hmp->rb_inos_root, ip)) {
61aeeb33
MD
281 hammer_uncache_node(&ip->cache[0]);
282 hammer_uncache_node(&ip->cache[1]);
b84de5af 283 KKASSERT(ip->lock.refs == 1);
b3deaf57 284 --hammer_count_inodes;
427e5fc6 285 kfree(ip, M_HAMMER);
b3deaf57 286 hammer_done_cursor(&cursor);
427e5fc6
MD
287 goto loop;
288 }
c0ade690 289 ip->flags |= HAMMER_INODE_ONDISK;
427e5fc6 290 } else {
19619882
MD
291 /*
292 * Do not panic on read-only accesses which fail, particularly
293 * historical accesses where the snapshot might not have
294 * complete connectivity.
295 */
296 if ((flags & HAMMER_INODE_RO) == 0) {
297 kprintf("hammer_get_inode: failed ip %p obj_id %016llx cursor %p error %d\n",
298 ip, ip->obj_id, &cursor, *errorp);
77062c8a 299 Debugger("x");
19619882 300 }
e63644f0
MD
301 if (ip->flags & HAMMER_INODE_RSV_INODES) {
302 ip->flags &= ~HAMMER_INODE_RSV_INODES; /* sanity */
303 --ip->hmp->rsv_inodes;
304 }
305 ip->hmp->rsv_databufs -= ip->rsv_databufs;
306 ip->rsv_databufs = 0; /* sanity */
307
b3deaf57 308 --hammer_count_inodes;
66325755
MD
309 kfree(ip, M_HAMMER);
310 ip = NULL;
427e5fc6 311 }
b3deaf57 312 hammer_done_cursor(&cursor);
66325755
MD
313 return (ip);
314}
315
8cd0a023
MD
316/*
317 * Create a new filesystem object, returning the inode in *ipp. The
1f07f686 318 * returned inode will be referenced.
8cd0a023 319 *
b84de5af 320 * The inode is created in-memory.
8cd0a023
MD
321 */
322int
a89aec1b
MD
323hammer_create_inode(hammer_transaction_t trans, struct vattr *vap,
324 struct ucred *cred, hammer_inode_t dip,
8cd0a023 325 struct hammer_inode **ipp)
66325755 326{
a89aec1b
MD
327 hammer_mount_t hmp;
328 hammer_inode_t ip;
6b4f890b 329 uid_t xuid;
66325755 330
8cd0a023
MD
331 hmp = trans->hmp;
332 ip = kmalloc(sizeof(*ip), M_HAMMER, M_WAITOK|M_ZERO);
b3deaf57 333 ++hammer_count_inodes;
0729c8c8 334 ip->obj_id = hammer_alloc_objid(trans, dip);
8cd0a023 335 KKASSERT(ip->obj_id != 0);
7f7c1f84 336 ip->obj_asof = hmp->asof;
8cd0a023 337 ip->hmp = hmp;
b84de5af 338 ip->flush_state = HAMMER_FST_IDLE;
11ad5ade 339 ip->flags = HAMMER_INODE_DDIRTY | HAMMER_INODE_ITIMES;
8cd0a023 340
a5fddc16 341 ip->trunc_off = 0x7FFFFFFFFFFFFFFFLL;
8cd0a023 342 RB_INIT(&ip->rec_tree);
059819e3 343 TAILQ_INIT(&ip->bio_list);
b84de5af 344 TAILQ_INIT(&ip->bio_alt_list);
1f07f686 345 TAILQ_INIT(&ip->target_list);
8cd0a023 346
11ad5ade
MD
347 ip->ino_leaf.atime = trans->time;
348 ip->ino_data.mtime = trans->time;
349 ip->ino_data.size = 0;
350 ip->ino_data.nlinks = 0;
e63644f0
MD
351
352 /*
353 * A nohistory designator on the parent directory is inherited by
354 * the child.
355 */
356 ip->ino_data.uflags = dip->ino_data.uflags &
357 (SF_NOHISTORY|UF_NOHISTORY|UF_NODUMP);
358
11ad5ade 359 ip->ino_leaf.base.btype = HAMMER_BTREE_TYPE_RECORD;
2f85fa4d 360 ip->ino_leaf.base.localization = HAMMER_LOCALIZE_INODE;
11ad5ade
MD
361 ip->ino_leaf.base.obj_id = ip->obj_id;
362 ip->ino_leaf.base.key = 0;
363 ip->ino_leaf.base.create_tid = 0;
364 ip->ino_leaf.base.delete_tid = 0;
365 ip->ino_leaf.base.rec_type = HAMMER_RECTYPE_INODE;
366 ip->ino_leaf.base.obj_type = hammer_get_obj_type(vap->va_type);
367
368 ip->ino_data.obj_type = ip->ino_leaf.base.obj_type;
8cd0a023
MD
369 ip->ino_data.version = HAMMER_INODE_DATA_VERSION;
370 ip->ino_data.mode = vap->va_mode;
b84de5af 371 ip->ino_data.ctime = trans->time;
11ad5ade 372 ip->ino_data.parent_obj_id = (dip) ? dip->ino_leaf.base.obj_id : 0;
6b4f890b 373
11ad5ade 374 switch(ip->ino_leaf.base.obj_type) {
7a04d74f
MD
375 case HAMMER_OBJTYPE_CDEV:
376 case HAMMER_OBJTYPE_BDEV:
377 ip->ino_data.rmajor = vap->va_rmajor;
378 ip->ino_data.rminor = vap->va_rminor;
379 break;
380 default:
381 break;
382 }
383
6b4f890b
MD
384 /*
385 * Calculate default uid/gid and overwrite with information from
386 * the vap.
387 */
388 xuid = hammer_to_unix_xid(&dip->ino_data.uid);
6b4f890b
MD
389 xuid = vop_helper_create_uid(hmp->mp, dip->ino_data.mode, xuid, cred,
390 &vap->va_mode);
391 ip->ino_data.mode = vap->va_mode;
392
8cd0a023
MD
393 if (vap->va_vaflags & VA_UID_UUID_VALID)
394 ip->ino_data.uid = vap->va_uid_uuid;
6b4f890b 395 else if (vap->va_uid != (uid_t)VNOVAL)
7538695e
MD
396 hammer_guid_to_uuid(&ip->ino_data.uid, vap->va_uid);
397 else
6b4f890b 398 hammer_guid_to_uuid(&ip->ino_data.uid, xuid);
7538695e 399
8cd0a023
MD
400 if (vap->va_vaflags & VA_GID_UUID_VALID)
401 ip->ino_data.gid = vap->va_gid_uuid;
6b4f890b 402 else if (vap->va_gid != (gid_t)VNOVAL)
8cd0a023 403 hammer_guid_to_uuid(&ip->ino_data.gid, vap->va_gid);
7538695e
MD
404 else
405 ip->ino_data.gid = dip->ino_data.gid;
8cd0a023
MD
406
407 hammer_ref(&ip->lock);
408 if (RB_INSERT(hammer_ino_rb_tree, &hmp->rb_inos_root, ip)) {
409 hammer_unref(&ip->lock);
a89aec1b 410 panic("hammer_create_inode: duplicate obj_id %llx", ip->obj_id);
8cd0a023
MD
411 }
412 *ipp = ip;
413 return(0);
66325755
MD
414}
415
d113fda1
MD
416/*
417 * Called by hammer_sync_inode().
418 */
419static int
4e17f465 420hammer_update_inode(hammer_cursor_t cursor, hammer_inode_t ip)
c0ade690 421{
4e17f465 422 hammer_transaction_t trans = cursor->trans;
c0ade690
MD
423 hammer_record_t record;
424 int error;
425
d26d0ae9 426retry:
c0ade690
MD
427 error = 0;
428
869e8f55
MD
429 /*
430 * If the inode has a presence on-disk then locate it and mark
431 * it deleted, setting DELONDISK.
432 *
433 * The record may or may not be physically deleted, depending on
434 * the retention policy.
435 */
76376933
MD
436 if ((ip->flags & (HAMMER_INODE_ONDISK|HAMMER_INODE_DELONDISK)) ==
437 HAMMER_INODE_ONDISK) {
4e17f465 438 hammer_normalize_cursor(cursor);
2f85fa4d 439 cursor->key_beg.localization = HAMMER_LOCALIZE_INODE;
4e17f465
MD
440 cursor->key_beg.obj_id = ip->obj_id;
441 cursor->key_beg.key = 0;
442 cursor->key_beg.create_tid = 0;
443 cursor->key_beg.delete_tid = 0;
444 cursor->key_beg.rec_type = HAMMER_RECTYPE_INODE;
445 cursor->key_beg.obj_type = 0;
446 cursor->asof = ip->obj_asof;
447 cursor->flags &= ~HAMMER_CURSOR_INITMASK;
11ad5ade 448 cursor->flags |= HAMMER_CURSOR_GET_LEAF | HAMMER_CURSOR_ASOF;
4e17f465
MD
449 cursor->flags |= HAMMER_CURSOR_BACKEND;
450
451 error = hammer_btree_lookup(cursor);
e8599db1
MD
452 if (hammer_debug_inode)
453 kprintf("IPDEL %p %08x %d", ip, ip->flags, error);
b84de5af
MD
454 if (error) {
455 kprintf("error %d\n", error);
456 Debugger("hammer_update_inode");
457 }
458
c0ade690 459 if (error == 0) {
e63644f0 460 error = hammer_ip_delete_record(cursor, ip, trans->tid);
e8599db1
MD
461 if (hammer_debug_inode)
462 kprintf(" error %d\n", error);
f90dde4c 463 if (error && error != EDEADLK) {
b84de5af
MD
464 kprintf("error %d\n", error);
465 Debugger("hammer_update_inode2");
466 }
1f07f686 467 if (error == 0) {
195c19a1 468 ip->flags |= HAMMER_INODE_DELONDISK;
1f07f686 469 }
e8599db1
MD
470 if (cursor->node)
471 hammer_cache_node(cursor->node, &ip->cache[0]);
4e17f465
MD
472 }
473 if (error == EDEADLK) {
474 hammer_done_cursor(cursor);
475 error = hammer_init_cursor(trans, cursor,
476 &ip->cache[0], ip);
e8599db1
MD
477 if (hammer_debug_inode)
478 kprintf("IPDED %p %d\n", ip, error);
4e17f465
MD
479 if (error == 0)
480 goto retry;
c0ade690 481 }
c0ade690
MD
482 }
483
484 /*
869e8f55
MD
485 * Ok, write out the initial record or a new record (after deleting
486 * the old one), unless the DELETED flag is set. This routine will
487 * clear DELONDISK if it writes out a record.
76376933 488 *
869e8f55
MD
489 * Update our inode statistics if this is the first application of
490 * the inode on-disk.
c0ade690 491 */
869e8f55
MD
492 if (error == 0 && (ip->flags & HAMMER_INODE_DELETED) == 0) {
493 /*
494 * Generate a record and write it to the media
495 */
11ad5ade 496 record = hammer_alloc_mem_record(ip, 0);
930bf163 497 record->type = HAMMER_MEM_RECORD_INODE;
1f07f686 498 record->flush_state = HAMMER_FST_FLUSH;
11ad5ade
MD
499 record->leaf = ip->sync_ino_leaf;
500 record->leaf.base.create_tid = trans->tid;
501 record->leaf.data_len = sizeof(ip->sync_ino_data);
b84de5af 502 record->data = (void *)&ip->sync_ino_data;
d36ec43b 503 record->flags |= HAMMER_RECF_INTERLOCK_BE;
4e17f465
MD
504 for (;;) {
505 error = hammer_ip_sync_record_cursor(cursor, record);
e8599db1
MD
506 if (hammer_debug_inode)
507 kprintf("GENREC %p rec %08x %d\n",
508 ip, record->flags, error);
4e17f465
MD
509 if (error != EDEADLK)
510 break;
511 hammer_done_cursor(cursor);
512 error = hammer_init_cursor(trans, cursor,
513 &ip->cache[0], ip);
e8599db1
MD
514 if (hammer_debug_inode)
515 kprintf("GENREC reinit %d\n", error);
4e17f465
MD
516 if (error)
517 break;
518 }
b84de5af
MD
519 if (error) {
520 kprintf("error %d\n", error);
521 Debugger("hammer_update_inode3");
522 }
d36ec43b
MD
523
524 /*
525 * The record isn't managed by the inode's record tree,
526 * destroy it whether we succeed or fail.
527 */
528 record->flags &= ~HAMMER_RECF_INTERLOCK_BE;
529 record->flags |= HAMMER_RECF_DELETED_FE;
1f07f686 530 record->flush_state = HAMMER_FST_IDLE;
b3deaf57 531 hammer_rel_mem_record(record);
d36ec43b 532
869e8f55
MD
533 /*
534 * Finish up.
535 */
d26d0ae9 536 if (error == 0) {
e8599db1
MD
537 if (hammer_debug_inode)
538 kprintf("CLEANDELOND %p %08x\n", ip, ip->flags);
11ad5ade 539 ip->sync_flags &= ~(HAMMER_INODE_DDIRTY |
b84de5af
MD
540 HAMMER_INODE_ITIMES);
541 ip->flags &= ~HAMMER_INODE_DELONDISK;
1f07f686
MD
542
543 /*
544 * Root volume count of inodes
545 */
d26d0ae9 546 if ((ip->flags & HAMMER_INODE_ONDISK) == 0) {
e8599db1
MD
547 hammer_modify_volume_field(trans,
548 trans->rootvol,
549 vol0_stat_inodes);
0b075555 550 ++ip->hmp->rootvol->ondisk->vol0_stat_inodes;
10a5d1ba 551 hammer_modify_volume_done(trans->rootvol);
d26d0ae9 552 ip->flags |= HAMMER_INODE_ONDISK;
e8599db1
MD
553 if (hammer_debug_inode)
554 kprintf("NOWONDISK %p\n", ip);
d26d0ae9 555 }
fbc6e32a 556 }
c0ade690 557 }
869e8f55
MD
558
559 /*
560 * If the inode has been destroyed, clean out any left-over flags
561 * that may have been set by the frontend.
562 */
f90dde4c 563 if (error == 0 && (ip->flags & HAMMER_INODE_DELETED)) {
11ad5ade 564 ip->sync_flags &= ~(HAMMER_INODE_DDIRTY |
f90dde4c
MD
565 HAMMER_INODE_ITIMES);
566 }
c0ade690
MD
567 return(error);
568}
569
a89aec1b 570/*
d113fda1
MD
571 * Update only the itimes fields. This is done no-historically. The
572 * record is updated in-place on the disk.
573 */
574static int
4e17f465 575hammer_update_itimes(hammer_cursor_t cursor, hammer_inode_t ip)
d113fda1 576{
4e17f465 577 hammer_transaction_t trans = cursor->trans;
11ad5ade 578 struct hammer_btree_leaf_elm *leaf;
d113fda1
MD
579 int error;
580
6a37e7e4 581retry:
d113fda1
MD
582 error = 0;
583 if ((ip->flags & (HAMMER_INODE_ONDISK|HAMMER_INODE_DELONDISK)) ==
584 HAMMER_INODE_ONDISK) {
4e17f465 585 hammer_normalize_cursor(cursor);
2f85fa4d 586 cursor->key_beg.localization = HAMMER_LOCALIZE_INODE;
4e17f465
MD
587 cursor->key_beg.obj_id = ip->obj_id;
588 cursor->key_beg.key = 0;
589 cursor->key_beg.create_tid = 0;
590 cursor->key_beg.delete_tid = 0;
591 cursor->key_beg.rec_type = HAMMER_RECTYPE_INODE;
592 cursor->key_beg.obj_type = 0;
593 cursor->asof = ip->obj_asof;
594 cursor->flags &= ~HAMMER_CURSOR_INITMASK;
11ad5ade 595 cursor->flags |= HAMMER_CURSOR_GET_LEAF | HAMMER_CURSOR_ASOF;
4e17f465
MD
596 cursor->flags |= HAMMER_CURSOR_BACKEND;
597
598 error = hammer_btree_lookup(cursor);
b84de5af
MD
599 if (error) {
600 kprintf("error %d\n", error);
601 Debugger("hammer_update_itimes1");
602 }
d113fda1 603 if (error == 0) {
10a5d1ba 604 /*
11ad5ade 605 * Do not generate UNDO records for atime updates.
10a5d1ba 606 */
11ad5ade
MD
607 leaf = cursor->leaf;
608 hammer_modify_node(trans, cursor->node,
609 &leaf->atime, sizeof(leaf->atime));
610 leaf->atime = ip->sync_ino_leaf.atime;
611 hammer_modify_node_done(cursor->node);
612 /*rec->ino_mtime = ip->sync_ino_rec.ino_mtime;*/
b84de5af 613 ip->sync_flags &= ~HAMMER_INODE_ITIMES;
d113fda1 614 /* XXX recalculate crc */
4e17f465
MD
615 hammer_cache_node(cursor->node, &ip->cache[0]);
616 }
617 if (error == EDEADLK) {
618 hammer_done_cursor(cursor);
619 error = hammer_init_cursor(trans, cursor,
620 &ip->cache[0], ip);
621 if (error == 0)
622 goto retry;
d113fda1 623 }
d113fda1
MD
624 }
625 return(error);
626}
627
628/*
1f07f686 629 * Release a reference on an inode, flush as requested.
b84de5af
MD
630 *
631 * On the last reference we queue the inode to the flusher for its final
632 * disposition.
a89aec1b 633 */
66325755 634void
a89aec1b 635hammer_rel_inode(struct hammer_inode *ip, int flush)
66325755 636{
1f07f686
MD
637 hammer_mount_t hmp = ip->hmp;
638
f90dde4c
MD
639 /*
640 * Handle disposition when dropping the last ref.
641 */
1f07f686
MD
642 for (;;) {
643 if (ip->lock.refs == 1) {
644 /*
645 * Determine whether on-disk action is needed for
646 * the inode's final disposition.
647 */
e8599db1
MD
648 KKASSERT(ip->vp == NULL);
649 hammer_inode_unloadable_check(ip, 0);
4e17f465
MD
650 if (ip->flags & HAMMER_INODE_MODMASK) {
651 hammer_flush_inode(ip, 0);
652 } else if (ip->lock.refs == 1) {
1f07f686
MD
653 hammer_unload_inode(ip);
654 break;
655 }
b84de5af 656 } else {
4e17f465 657 if (flush)
1f07f686 658 hammer_flush_inode(ip, 0);
4e17f465 659
1f07f686
MD
660 /*
661 * The inode still has multiple refs, try to drop
662 * one ref.
663 */
664 KKASSERT(ip->lock.refs >= 1);
665 if (ip->lock.refs > 1) {
666 hammer_unref(&ip->lock);
667 break;
668 }
b84de5af 669 }
f90dde4c
MD
670 }
671
672 /*
1f07f686
MD
673 * XXX bad hack until I add code to track inodes in SETUP. We
674 * can queue a lot of inodes to the syncer but if we don't wake
675 * it up the undo sets will be too large or too many unflushed
676 * records will build up and blow our malloc limit.
f90dde4c 677 */
1f07f686
MD
678 if (++hmp->reclaim_count > 256) {
679 hmp->reclaim_count = 0;
680 hammer_flusher_async(hmp);
9480ff55 681 }
427e5fc6
MD
682}
683
27ea2398 684/*
b84de5af
MD
685 * Unload and destroy the specified inode. Must be called with one remaining
686 * reference. The reference is disposed of.
8cd0a023 687 *
b84de5af 688 * This can only be called in the context of the flusher.
27ea2398 689 */
b84de5af 690static int
ec4e8497 691hammer_unload_inode(struct hammer_inode *ip)
27ea2398 692{
b84de5af 693 KASSERT(ip->lock.refs == 1,
a89aec1b 694 ("hammer_unload_inode: %d refs\n", ip->lock.refs));
8cd0a023 695 KKASSERT(ip->vp == NULL);
f90dde4c
MD
696 KKASSERT(ip->flush_state == HAMMER_FST_IDLE);
697 KKASSERT(ip->cursor_ip_refs == 0);
45a014dc 698 KKASSERT(ip->lock.lockcount == 0);
f90dde4c
MD
699 KKASSERT((ip->flags & HAMMER_INODE_MODMASK) == 0);
700
701 KKASSERT(RB_EMPTY(&ip->rec_tree));
1f07f686 702 KKASSERT(TAILQ_EMPTY(&ip->target_list));
f90dde4c
MD
703 KKASSERT(TAILQ_EMPTY(&ip->bio_list));
704 KKASSERT(TAILQ_EMPTY(&ip->bio_alt_list));
705
706 RB_REMOVE(hammer_ino_rb_tree, &ip->hmp->rb_inos_root, ip);
707
708 hammer_uncache_node(&ip->cache[0]);
709 hammer_uncache_node(&ip->cache[1]);
0729c8c8
MD
710 if (ip->objid_cache)
711 hammer_clear_objid(ip);
f90dde4c
MD
712 --hammer_count_inodes;
713 kfree(ip, M_HAMMER);
6b4f890b 714
27ea2398
MD
715 return(0);
716}
717
427e5fc6 718/*
d113fda1
MD
719 * A transaction has modified an inode, requiring updates as specified by
720 * the passed flags.
7f7c1f84 721 *
d113fda1 722 * HAMMER_INODE_DDIRTY: Inode data has been updated
1f07f686 723 * HAMMER_INODE_XDIRTY: Dirty in-memory records
4e17f465 724 * HAMMER_INODE_BUFS: Dirty buffer cache buffers
d113fda1
MD
725 * HAMMER_INODE_DELETED: Inode record/data must be deleted
726 * HAMMER_INODE_ITIMES: mtime/atime has been updated
427e5fc6 727 */
66325755 728void
b84de5af 729hammer_modify_inode(hammer_transaction_t trans, hammer_inode_t ip, int flags)
427e5fc6 730{
d113fda1 731 KKASSERT ((ip->flags & HAMMER_INODE_RO) == 0 ||
11ad5ade
MD
732 (flags & (HAMMER_INODE_DDIRTY |
733 HAMMER_INODE_XDIRTY | HAMMER_INODE_BUFS |
734 HAMMER_INODE_DELETED | HAMMER_INODE_ITIMES)) == 0);
e63644f0
MD
735 if ((ip->flags & HAMMER_INODE_RSV_INODES) == 0) {
736 ip->flags |= HAMMER_INODE_RSV_INODES;
737 ++ip->hmp->rsv_inodes;
738 }
b84de5af
MD
739
740 ip->flags |= flags;
741}
742
743/*
1f07f686
MD
744 * Request that an inode be flushed. This whole mess cannot block and may
745 * recurse. Once requested HAMMER will attempt to actively flush it until
746 * the flush can be done.
b84de5af 747 *
1f07f686
MD
748 * The inode may already be flushing, or may be in a setup state. We can
749 * place the inode in a flushing state if it is currently idle and flag it
750 * to reflush if it is currently flushing.
b84de5af
MD
751 */
752void
f90dde4c 753hammer_flush_inode(hammer_inode_t ip, int flags)
b84de5af 754{
1f07f686
MD
755 hammer_record_t depend;
756 int r, good;
757
758 /*
759 * Trivial 'nothing to flush' case. If the inode is ina SETUP
760 * state we have to put it back into an IDLE state so we can
761 * drop the extra ref.
762 */
4e17f465 763 if ((ip->flags & HAMMER_INODE_MODMASK) == 0) {
1f07f686
MD
764 if (ip->flush_state == HAMMER_FST_SETUP) {
765 ip->flush_state = HAMMER_FST_IDLE;
766 hammer_rel_inode(ip, 0);
ec4e8497 767 }
b84de5af
MD
768 return;
769 }
42c7d26b 770
1f07f686
MD
771 /*
772 * Our flush action will depend on the current state.
773 */
774 switch(ip->flush_state) {
775 case HAMMER_FST_IDLE:
776 /*
777 * We have no dependancies and can flush immediately. Some
778 * our children may not be flushable so we have to re-test
779 * with that additional knowledge.
780 */
781 hammer_flush_inode_core(ip, flags);
782 break;
783 case HAMMER_FST_SETUP:
784 /*
785 * Recurse upwards through dependancies via target_list
786 * and start their flusher actions going if possible.
787 *
788 * 'good' is our connectivity. -1 means we have none and
789 * can't flush, 0 means there weren't any dependancies, and
790 * 1 means we have good connectivity.
791 */
792 good = 0;
793 TAILQ_FOREACH(depend, &ip->target_list, target_entry) {
794 r = hammer_setup_parent_inodes(depend);
795 if (r < 0 && good == 0)
796 good = -1;
797 if (r > 0)
798 good = 1;
799 }
800
801 /*
802 * We can continue if good >= 0. Determine how many records
803 * under our inode can be flushed (and mark them).
804 */
1f07f686
MD
805 if (good >= 0) {
806 hammer_flush_inode_core(ip, flags);
807 } else {
808 ip->flags |= HAMMER_INODE_REFLUSH;
4e17f465
MD
809 if (flags & HAMMER_FLUSH_SIGNAL) {
810 ip->flags |= HAMMER_INODE_RESIGNAL;
811 hammer_flusher_async(ip->hmp);
812 }
1f07f686
MD
813 }
814 break;
815 default:
816 /*
817 * We are already flushing, flag the inode to reflush
818 * if needed after it completes its current flush.
819 */
820 if ((ip->flags & HAMMER_INODE_REFLUSH) == 0)
821 ip->flags |= HAMMER_INODE_REFLUSH;
4e17f465
MD
822 if (flags & HAMMER_FLUSH_SIGNAL) {
823 ip->flags |= HAMMER_INODE_RESIGNAL;
824 hammer_flusher_async(ip->hmp);
825 }
1f07f686
MD
826 break;
827 }
828}
829
830/*
831 * We are asked to recurse upwards and convert the record from SETUP
832 * to FLUSH if possible. record->ip is a parent of the caller's inode,
833 * and record->target_ip is the caller's inode.
834 *
835 * Return 1 if the record gives us connectivity
836 *
837 * Return 0 if the record is not relevant
838 *
839 * Return -1 if we can't resolve the dependancy and there is no connectivity.
840 */
841static int
842hammer_setup_parent_inodes(hammer_record_t record)
843{
844 hammer_mount_t hmp = record->ip->hmp;
845 hammer_record_t depend;
846 hammer_inode_t ip;
847 int r, good;
848
849 KKASSERT(record->flush_state != HAMMER_FST_IDLE);
850 ip = record->ip;
851
852 /*
853 * If the record is already flushing, is it in our flush group?
854 *
e8599db1
MD
855 * If it is in our flush group but it is a general record or a
856 * delete-on-disk, it does not improve our connectivity (return 0),
857 * and if the target inode is not trying to destroy itself we can't
858 * allow the operation yet anyway (the second return -1).
1f07f686
MD
859 */
860 if (record->flush_state == HAMMER_FST_FLUSH) {
861 if (record->flush_group != hmp->flusher_next) {
862 ip->flags |= HAMMER_INODE_REFLUSH;
863 return(-1);
f90dde4c 864 }
1f07f686
MD
865 if (record->type == HAMMER_MEM_RECORD_ADD)
866 return(1);
e8599db1 867 /* GENERAL or DEL */
1f07f686
MD
868 return(0);
869 }
870
871 /*
872 * It must be a setup record. Try to resolve the setup dependancies
873 * by recursing upwards so we can place ip on the flush list.
874 */
875 KKASSERT(record->flush_state == HAMMER_FST_SETUP);
876
877 good = 0;
878 TAILQ_FOREACH(depend, &ip->target_list, target_entry) {
879 r = hammer_setup_parent_inodes(depend);
880 if (r < 0 && good == 0)
881 good = -1;
882 if (r > 0)
883 good = 1;
884 }
885
886 /*
887 * We can't flush ip because it has no connectivity (XXX also check
888 * nlinks for pre-existing connectivity!). Flag it so any resolution
889 * recurses back down.
890 */
891 if (good < 0) {
892 ip->flags |= HAMMER_INODE_REFLUSH;
893 return(good);
894 }
895
896 /*
897 * We are go, place the parent inode in a flushing state so we can
898 * place its record in a flushing state. Note that the parent
899 * may already be flushing. The record must be in the same flush
900 * group as the parent.
901 */
902 if (ip->flush_state != HAMMER_FST_FLUSH)
903 hammer_flush_inode_core(ip, HAMMER_FLUSH_RECURSION);
904 KKASSERT(ip->flush_state == HAMMER_FST_FLUSH);
905 KKASSERT(record->flush_state == HAMMER_FST_SETUP);
906
907#if 0
908 if (record->type == HAMMER_MEM_RECORD_DEL &&
869e8f55 909 (record->target_ip->flags & (HAMMER_INODE_DELETED|HAMMER_INODE_DELONDISK)) == 0) {
1f07f686
MD
910 /*
911 * Regardless of flushing state we cannot sync this path if the
912 * record represents a delete-on-disk but the target inode
913 * is not ready to sync its own deletion.
914 *
915 * XXX need to count effective nlinks to determine whether
916 * the flush is ok, otherwise removing a hardlink will
917 * just leave the DEL record to rot.
918 */
919 record->target_ip->flags |= HAMMER_INODE_REFLUSH;
920 return(-1);
921 } else
922#endif
923 if (ip->flush_group == ip->hmp->flusher_next) {
924 /*
925 * This is the record we wanted to synchronize.
926 */
927 record->flush_state = HAMMER_FST_FLUSH;
928 record->flush_group = ip->flush_group;
929 hammer_ref(&record->lock);
930 if (record->type == HAMMER_MEM_RECORD_ADD)
931 return(1);
932
933 /*
e8599db1
MD
934 * A general or delete-on-disk record does not contribute
935 * to our visibility. We can still flush it, however.
1f07f686
MD
936 */
937 return(0);
938 } else {
939 /*
940 * We couldn't resolve the dependancies, request that the
941 * inode be flushed when the dependancies can be resolved.
942 */
943 ip->flags |= HAMMER_INODE_REFLUSH;
944 return(-1);
7f7c1f84 945 }
c0ade690
MD
946}
947
948/*
1f07f686 949 * This is the core routine placing an inode into the FST_FLUSH state.
c0ade690 950 */
b84de5af 951static void
1f07f686 952hammer_flush_inode_core(hammer_inode_t ip, int flags)
b84de5af 953{
1f07f686 954 int go_count;
1f07f686 955
4e17f465
MD
956 /*
957 * Set flush state and prevent the flusher from cycling into
958 * the next flush group. Do not place the ip on the list yet.
959 * Inodes not in the idle state get an extra reference.
960 */
1f07f686
MD
961 KKASSERT(ip->flush_state != HAMMER_FST_FLUSH);
962 if (ip->flush_state == HAMMER_FST_IDLE)
963 hammer_ref(&ip->lock);
964 ip->flush_state = HAMMER_FST_FLUSH;
965 ip->flush_group = ip->hmp->flusher_next;
4e17f465 966 ++ip->hmp->flusher_lock;
b84de5af 967
e8599db1
MD
968 /*
969 * We need to be able to vfsync/truncate from the backend.
970 */
971 KKASSERT((ip->flags & HAMMER_INODE_VHELD) == 0);
972 if (ip->vp && (ip->vp->v_flag & VINACTIVE) == 0) {
973 ip->flags |= HAMMER_INODE_VHELD;
974 vref(ip->vp);
975 }
976
ec4e8497 977 /*
1f07f686
MD
978 * Figure out how many in-memory records we can actually flush
979 * (not including inode meta-data, buffers, etc).
ec4e8497 980 */
1f07f686
MD
981 if (flags & HAMMER_FLUSH_RECURSION) {
982 go_count = 1;
983 } else {
984 go_count = RB_SCAN(hammer_rec_rb_tree, &ip->rec_tree, NULL,
985 hammer_setup_child_callback, NULL);
986 }
b84de5af
MD
987
988 /*
1f07f686
MD
989 * This is a more involved test that includes go_count. If we
990 * can't flush, flag the inode and return. If go_count is 0 we
991 * were are unable to flush any records in our rec_tree and
992 * must ignore the XDIRTY flag.
b84de5af 993 */
1f07f686
MD
994 if (go_count == 0) {
995 if ((ip->flags & HAMMER_INODE_MODMASK_NOXDIRTY) == 0) {
996 ip->flags |= HAMMER_INODE_REFLUSH;
997 ip->flush_state = HAMMER_FST_SETUP;
e8599db1
MD
998 if (ip->flags & HAMMER_INODE_VHELD) {
999 ip->flags &= ~HAMMER_INODE_VHELD;
1000 vrele(ip->vp);
1001 }
4e17f465
MD
1002 if (flags & HAMMER_FLUSH_SIGNAL) {
1003 ip->flags |= HAMMER_INODE_RESIGNAL;
1004 hammer_flusher_async(ip->hmp);
1005 }
1006 if (--ip->hmp->flusher_lock == 0)
1007 wakeup(&ip->hmp->flusher_lock);
1f07f686
MD
1008 return;
1009 }
1010 }
b84de5af 1011
b84de5af
MD
1012 /*
1013 * Snapshot the state of the inode for the backend flusher.
1014 *
1015 * The truncation must be retained in the frontend until after
1016 * we've actually performed the record deletion.
1f07f686
MD
1017 *
1018 * NOTE: The DELETING flag is a mod flag, but it is also sticky,
1019 * and stays in ip->flags. Once set, it stays set until the
1020 * inode is destroyed.
b84de5af
MD
1021 */
1022 ip->sync_flags = (ip->flags & HAMMER_INODE_MODMASK);
1023 ip->sync_trunc_off = ip->trunc_off;
11ad5ade 1024 ip->sync_ino_leaf = ip->ino_leaf;
b84de5af 1025 ip->sync_ino_data = ip->ino_data;
4e17f465 1026 ip->flags &= ~HAMMER_INODE_MODMASK | HAMMER_INODE_TRUNCATED;
b84de5af
MD
1027
1028 /*
4e17f465 1029 * The flusher list inherits our inode and reference.
b84de5af 1030 */
1f07f686 1031 TAILQ_INSERT_TAIL(&ip->hmp->flush_list, ip, flush_entry);
4e17f465
MD
1032 if (--ip->hmp->flusher_lock == 0)
1033 wakeup(&ip->hmp->flusher_lock);
1f07f686
MD
1034
1035 if (flags & HAMMER_FLUSH_SIGNAL)
1036 hammer_flusher_async(ip->hmp);
b84de5af
MD
1037}
1038
ec4e8497 1039/*
1f07f686
MD
1040 * Callback for scan of ip->rec_tree. Try to include each record in our
1041 * flush. ip->flush_group has been set but the inode has not yet been
1042 * moved into a flushing state.
1043 *
1044 * If we get stuck on a record we have to set HAMMER_INODE_REFLUSH on
1045 * both inodes.
1046 *
1047 * We return 1 for any record placed or found in FST_FLUSH, which prevents
1048 * the caller from shortcutting the flush.
ec4e8497 1049 */
c0ade690 1050static int
1f07f686 1051hammer_setup_child_callback(hammer_record_t rec, void *data)
b84de5af 1052{
1f07f686
MD
1053 hammer_inode_t target_ip;
1054 hammer_inode_t ip;
1055 int r;
1056
1057 /*
1058 * If the record has been deleted by the backend (it's being held
1059 * by the frontend in a race), just ignore it.
1060 */
1061 if (rec->flags & HAMMER_RECF_DELETED_BE)
ec4e8497 1062 return(0);
1f07f686
MD
1063
1064 /*
1065 * If the record is in an idle state it has no dependancies and
1066 * can be flushed.
1067 */
1068 ip = rec->ip;
1069 r = 0;
1070
1071 switch(rec->flush_state) {
1072 case HAMMER_FST_IDLE:
1073 /*
1074 * Record has no setup dependancy, we can flush it.
1075 */
1076 KKASSERT(rec->target_ip == NULL);
1077 rec->flush_state = HAMMER_FST_FLUSH;
1078 rec->flush_group = ip->flush_group;
b84de5af 1079 hammer_ref(&rec->lock);
1f07f686
MD
1080 r = 1;
1081 break;
1082 case HAMMER_FST_SETUP:
1083 /*
1084 * Record has a setup dependancy. Try to include the
1085 * target ip in the flush.
1086 *
1087 * We have to be careful here, if we do not do the right
1088 * thing we can lose track of dirty inodes and the system
1089 * will lockup trying to allocate buffers.
1090 */
1091 target_ip = rec->target_ip;
1092 KKASSERT(target_ip != NULL);
1093 KKASSERT(target_ip->flush_state != HAMMER_FST_IDLE);
1094 if (target_ip->flush_state == HAMMER_FST_FLUSH) {
1095 /*
1096 * If the target IP is already flushing in our group
1097 * we are golden, otherwise make sure the target
1098 * reflushes.
1099 */
1100 if (target_ip->flush_group == ip->flush_group) {
1101 rec->flush_state = HAMMER_FST_FLUSH;
1102 rec->flush_group = ip->flush_group;
1103 hammer_ref(&rec->lock);
1104 r = 1;
1105 } else {
1106 target_ip->flags |= HAMMER_INODE_REFLUSH;
1107 }
1108 } else if (rec->type == HAMMER_MEM_RECORD_ADD) {
1109 /*
1110 * If the target IP is not flushing we can force
1111 * it to flush, even if it is unable to write out
1112 * any of its own records we have at least one in
1113 * hand that we CAN deal with.
1114 */
1115 rec->flush_state = HAMMER_FST_FLUSH;
1116 rec->flush_group = ip->flush_group;
1117 hammer_ref(&rec->lock);
1118 hammer_flush_inode_core(target_ip,
1119 HAMMER_FLUSH_RECURSION);
1120 r = 1;
1121 } else {
1122 /*
e8599db1
MD
1123 * General or delete-on-disk record.
1124 *
1125 * XXX this needs help. If a delete-on-disk we could
1126 * disconnect the target. If the target has its own
1127 * dependancies they really need to be flushed.
1f07f686
MD
1128 *
1129 * XXX
1130 */
1131 rec->flush_state = HAMMER_FST_FLUSH;
1132 rec->flush_group = ip->flush_group;
1133 hammer_ref(&rec->lock);
1134 hammer_flush_inode_core(target_ip,
1135 HAMMER_FLUSH_RECURSION);
1136 r = 1;
1137 }
1138 break;
1139 case HAMMER_FST_FLUSH:
1140 /*
1141 * Record already associated with a flush group. It had
1142 * better be ours.
1143 */
1144 KKASSERT(rec->flush_group == ip->flush_group);
1145 r = 1;
1146 break;
b84de5af 1147 }
1f07f686 1148 return(r);
b84de5af
MD
1149}
1150
b84de5af
MD
1151/*
1152 * Wait for a previously queued flush to complete
1153 */
1154void
1155hammer_wait_inode(hammer_inode_t ip)
1156{
e8599db1 1157 while (ip->flush_state != HAMMER_FST_IDLE) {
b84de5af
MD
1158 ip->flags |= HAMMER_INODE_FLUSHW;
1159 tsleep(&ip->flags, 0, "hmrwin", 0);
1160 }
1161}
1162
1163/*
1164 * Called by the backend code when a flush has been completed.
1165 * The inode has already been removed from the flush list.
1166 *
1167 * A pipelined flush can occur, in which case we must re-enter the
1168 * inode on the list and re-copy its fields.
1169 */
1170void
1171hammer_flush_inode_done(hammer_inode_t ip)
1172{
1955afa7 1173 struct bio *bio;
1f07f686 1174 int dorel = 0;
1955afa7 1175
b84de5af
MD
1176 KKASSERT(ip->flush_state == HAMMER_FST_FLUSH);
1177
1f07f686
MD
1178 /*
1179 * Allow BIOs to queue to the inode's primary bioq again.
1180 */
1181 ip->flags &= ~HAMMER_INODE_WRITE_ALT;
1182
1183 /*
1184 * Merge left-over flags back into the frontend and fix the state.
1185 */
b84de5af 1186 ip->flags |= ip->sync_flags;
1f07f686
MD
1187
1188 /*
1189 * The backend may have adjusted nlinks, so if the adjusted nlinks
1190 * does not match the fronttend set the frontend's RDIRTY flag again.
1191 */
11ad5ade
MD
1192 if (ip->ino_data.nlinks != ip->sync_ino_data.nlinks)
1193 ip->flags |= HAMMER_INODE_DDIRTY;
b84de5af 1194
b84de5af 1195 /*
1955afa7
MD
1196 * Reflush any BIOs that wound up in the alt list. Our inode will
1197 * also wind up at the end of the flusher's list.
b84de5af
MD
1198 */
1199 while ((bio = TAILQ_FIRST(&ip->bio_alt_list)) != NULL) {
1200 TAILQ_REMOVE(&ip->bio_alt_list, bio, bio_act);
1201 TAILQ_INSERT_TAIL(&ip->bio_list, bio, bio_act);
4e17f465
MD
1202 }
1203 /*
e63644f0
MD
1204 * Fix up the dirty buffer status. IO completions will also
1205 * try to clean up rsv_databufs.
4e17f465
MD
1206 */
1207 if (TAILQ_FIRST(&ip->bio_list) ||
1208 (ip->vp && RB_ROOT(&ip->vp->v_rbdirty_tree))) {
1f07f686 1209 ip->flags |= HAMMER_INODE_BUFS;
e63644f0
MD
1210 } else {
1211 ip->hmp->rsv_databufs -= ip->rsv_databufs;
1212 ip->rsv_databufs = 0;
1f07f686
MD
1213 }
1214
1215 /*
1216 * Re-set the XDIRTY flag if some of the inode's in-memory records
1217 * could not be flushed.
1218 */
4e17f465 1219 if (RB_ROOT(&ip->rec_tree))
f90dde4c 1220 ip->flags |= HAMMER_INODE_XDIRTY;
4e17f465
MD
1221
1222 /*
1223 * Do not lose track of inodes which no longer have vnode
1224 * assocations, otherwise they may never get flushed again.
1225 */
1226 if ((ip->flags & HAMMER_INODE_MODMASK) && ip->vp == NULL)
b84de5af 1227 ip->flags |= HAMMER_INODE_REFLUSH;
4e17f465
MD
1228
1229 /*
1230 * Adjust flush_state. The target state (idle or setup) shouldn't
1231 * be terribly important since we will reflush if we really need
1232 * to do anything. XXX
1233 */
1234 if (TAILQ_EMPTY(&ip->target_list) && RB_EMPTY(&ip->rec_tree)) {
1235 ip->flush_state = HAMMER_FST_IDLE;
1236 dorel = 1;
1237 } else {
1238 ip->flush_state = HAMMER_FST_SETUP;
b84de5af 1239 }
b84de5af 1240
e8599db1
MD
1241 /*
1242 * Clean up the vnode ref
1243 */
1244 if (ip->flags & HAMMER_INODE_VHELD) {
1245 ip->flags &= ~HAMMER_INODE_VHELD;
1246 vrele(ip->vp);
1247 }
1248
b84de5af
MD
1249 /*
1250 * If the frontend made more changes and requested another flush,
4e17f465 1251 * then try to get it running.
b84de5af
MD
1252 */
1253 if (ip->flags & HAMMER_INODE_REFLUSH) {
1254 ip->flags &= ~HAMMER_INODE_REFLUSH;
4e17f465
MD
1255 if (ip->flags & HAMMER_INODE_RESIGNAL) {
1256 ip->flags &= ~HAMMER_INODE_RESIGNAL;
1257 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
1258 } else {
1259 hammer_flush_inode(ip, 0);
0729c8c8 1260 }
4e17f465
MD
1261 }
1262
e63644f0
MD
1263 /*
1264 * If the inode is now clean drop the space reservation.
1265 */
1266 if ((ip->flags & HAMMER_INODE_MODMASK) == 0 &&
1267 (ip->flags & HAMMER_INODE_RSV_INODES)) {
1268 ip->flags &= ~HAMMER_INODE_RSV_INODES;
1269 --ip->hmp->rsv_inodes;
1270 }
1271
4e17f465
MD
1272 /*
1273 * Finally, if the frontend is waiting for a flush to complete,
1274 * wake it up.
1275 */
1276 if (ip->flush_state != HAMMER_FST_FLUSH) {
b84de5af
MD
1277 if (ip->flags & HAMMER_INODE_FLUSHW) {
1278 ip->flags &= ~HAMMER_INODE_FLUSHW;
1279 wakeup(&ip->flags);
1280 }
1281 }
1f07f686
MD
1282 if (dorel)
1283 hammer_rel_inode(ip, 0);
b84de5af
MD
1284}
1285
1286/*
1287 * Called from hammer_sync_inode() to synchronize in-memory records
1288 * to the media.
1289 */
1290static int
1291hammer_sync_record_callback(hammer_record_t record, void *data)
c0ade690 1292{
4e17f465
MD
1293 hammer_cursor_t cursor = data;
1294 hammer_transaction_t trans = cursor->trans;
c0ade690
MD
1295 int error;
1296
b84de5af 1297 /*
1f07f686 1298 * Skip records that do not belong to the current flush.
b84de5af 1299 */
1f07f686 1300 if (record->flush_state != HAMMER_FST_FLUSH)
b84de5af 1301 return(0);
1f07f686
MD
1302 KKASSERT((record->flags & HAMMER_RECF_DELETED_BE) == 0);
1303#if 1
1304 if (record->flush_group != record->ip->flush_group) {
1305 kprintf("sync_record %p ip %p bad flush group %d %d\n", record, record->ip, record->flush_group ,record->ip->flush_group);
1306 Debugger("blah2");
1307 return(0);
1308 }
1309#endif
1310 KKASSERT(record->flush_group == record->ip->flush_group);
d36ec43b
MD
1311
1312 /*
1313 * Interlock the record using the BE flag. Once BE is set the
1314 * frontend cannot change the state of FE.
1315 *
1316 * NOTE: If FE is set prior to us setting BE we still sync the
1317 * record out, but the flush completion code converts it to
1318 * a delete-on-disk record instead of destroying it.
1319 */
4e17f465 1320 KKASSERT((record->flags & HAMMER_RECF_INTERLOCK_BE) == 0);
d36ec43b
MD
1321 record->flags |= HAMMER_RECF_INTERLOCK_BE;
1322
98f7132d
MD
1323 /*
1324 * If the whole inode is being deleting all on-disk records will
930bf163
MD
1325 * be deleted very soon, we can't sync any new records to disk
1326 * because they will be deleted in the same transaction they were
1327 * created in (delete_tid == create_tid), which will assert.
1328 *
1329 * XXX There may be a case with RECORD_ADD with DELETED_FE set
1330 * that we currently panic on.
98f7132d
MD
1331 */
1332 if (record->ip->sync_flags & HAMMER_INODE_DELETING) {
930bf163
MD
1333 switch(record->type) {
1334 case HAMMER_MEM_RECORD_GENERAL:
98f7132d
MD
1335 record->flags |= HAMMER_RECF_DELETED_FE;
1336 record->flags |= HAMMER_RECF_DELETED_BE;
930bf163
MD
1337 error = 0;
1338 goto done;
1339 case HAMMER_MEM_RECORD_ADD:
1340 panic("hammer_sync_record_callback: illegal add "
1341 "during inode deletion record %p", record);
1342 break; /* NOT REACHED */
1343 case HAMMER_MEM_RECORD_INODE:
1344 panic("hammer_sync_record_callback: attempt to "
1345 "sync inode record %p?", record);
1346 break; /* NOT REACHED */
1347 case HAMMER_MEM_RECORD_DEL:
1348 /*
1349 * Follow through and issue the on-disk deletion
98f7132d 1350 */
930bf163 1351 break;
98f7132d 1352 }
98f7132d
MD
1353 }
1354
d36ec43b
MD
1355 /*
1356 * If DELETED_FE is set we may have already sent dependant pieces
1357 * to the disk and we must flush the record as if it hadn't been
1358 * deleted. This creates a bit of a mess because we have to
1f07f686 1359 * have ip_sync_record convert the record to MEM_RECORD_DEL before
d36ec43b
MD
1360 * it inserts the B-Tree record. Otherwise the media sync might
1361 * be visible to the frontend.
1362 */
1f07f686 1363 if (record->flags & HAMMER_RECF_DELETED_FE) {
e8599db1
MD
1364 if (record->type == HAMMER_MEM_RECORD_ADD) {
1365 record->flags |= HAMMER_RECF_CONVERT_DELETE;
1366 } else {
1367 KKASSERT(record->type != HAMMER_MEM_RECORD_DEL);
1368 return(0);
1369 }
1f07f686 1370 }
b84de5af
MD
1371
1372 /*
1373 * Assign the create_tid for new records. Deletions already
1374 * have the record's entire key properly set up.
1375 */
1f07f686 1376 if (record->type != HAMMER_MEM_RECORD_DEL)
11ad5ade 1377 record->leaf.base.create_tid = trans->tid;
4e17f465
MD
1378 for (;;) {
1379 error = hammer_ip_sync_record_cursor(cursor, record);
1380 if (error != EDEADLK)
1381 break;
1382 hammer_done_cursor(cursor);
1383 error = hammer_init_cursor(trans, cursor, &record->ip->cache[0],
1384 record->ip);
1385 if (error)
1386 break;
1387 }
1388 record->flags &= ~HAMMER_RECF_CONVERT_DELETE;
c0ade690
MD
1389
1390 if (error) {
b3deaf57
MD
1391 error = -error;
1392 if (error != -ENOSPC) {
b84de5af
MD
1393 kprintf("hammer_sync_record_callback: sync failed rec "
1394 "%p, error %d\n", record, error);
1395 Debugger("sync failed rec");
b3deaf57 1396 }
c0ade690 1397 }
98f7132d 1398done:
d36ec43b 1399 hammer_flush_record_done(record, error);
b3deaf57 1400 return(error);
c0ade690
MD
1401}
1402
1403/*
1404 * XXX error handling
1405 */
1406int
1f07f686 1407hammer_sync_inode(hammer_inode_t ip)
c0ade690
MD
1408{
1409 struct hammer_transaction trans;
4e17f465 1410 struct hammer_cursor cursor;
059819e3 1411 struct bio *bio;
1f07f686
MD
1412 hammer_record_t depend;
1413 hammer_record_t next;
ec4e8497 1414 int error, tmp_error;
1f07f686 1415 u_int64_t nlinks;
c0ade690 1416
1f07f686 1417 if ((ip->sync_flags & HAMMER_INODE_MODMASK) == 0)
d113fda1 1418 return(0);
d113fda1 1419
b84de5af 1420 hammer_start_transaction_fls(&trans, ip->hmp);
4e17f465
MD
1421 error = hammer_init_cursor(&trans, &cursor, &ip->cache[0], ip);
1422 if (error)
1423 goto done;
c0ade690 1424
ec4e8497 1425 /*
1f07f686
MD
1426 * Any directory records referencing this inode which are not in
1427 * our current flush group must adjust our nlink count for the
1428 * purposes of synchronization to disk.
1429 *
1430 * Records which are in our flush group can be unlinked from our
c4bae5fd
MD
1431 * inode now, potentially allowing the inode to be physically
1432 * deleted.
ec4e8497 1433 */
11ad5ade 1434 nlinks = ip->ino_data.nlinks;
1f07f686
MD
1435 next = TAILQ_FIRST(&ip->target_list);
1436 while ((depend = next) != NULL) {
1437 next = TAILQ_NEXT(depend, target_entry);
1438 if (depend->flush_state == HAMMER_FST_FLUSH &&
1439 depend->flush_group == ip->hmp->flusher_act) {
c4bae5fd
MD
1440 /*
1441 * If this is an ADD that was deleted by the frontend
1442 * the frontend nlinks count will have already been
1443 * decremented, but the backend is going to sync its
1444 * directory entry and must account for it. The
1445 * record will be converted to a delete-on-disk when
1446 * it gets synced.
1447 *
1448 * If the ADD was not deleted by the frontend we
1449 * can remove the dependancy from our target_list.
1450 */
1451 if (depend->flags & HAMMER_RECF_DELETED_FE) {
1452 ++nlinks;
1453 } else {
1454 TAILQ_REMOVE(&ip->target_list, depend,
1455 target_entry);
1456 depend->target_ip = NULL;
1457 }
1f07f686 1458 } else if ((depend->flags & HAMMER_RECF_DELETED_FE) == 0) {
c4bae5fd
MD
1459 /*
1460 * Not part of our flush group
1461 */
1462 KKASSERT((depend->flags & HAMMER_RECF_DELETED_BE) == 0);
1f07f686
MD
1463 switch(depend->type) {
1464 case HAMMER_MEM_RECORD_ADD:
1465 --nlinks;
1466 break;
1467 case HAMMER_MEM_RECORD_DEL:
1468 ++nlinks;
1469 break;
e8599db1
MD
1470 default:
1471 break;
1f07f686 1472 }
ec4e8497 1473 }
ec4e8497
MD
1474 }
1475
c0ade690 1476 /*
1f07f686 1477 * Set dirty if we had to modify the link count.
c0ade690 1478 */
11ad5ade 1479 if (ip->sync_ino_data.nlinks != nlinks) {
1f07f686 1480 KKASSERT((int64_t)nlinks >= 0);
11ad5ade
MD
1481 ip->sync_ino_data.nlinks = nlinks;
1482 ip->sync_flags |= HAMMER_INODE_DDIRTY;
1f07f686 1483 }
b84de5af 1484
4e17f465 1485 /*
e8599db1
MD
1486 * Queue up as many dirty buffers as we can then set a flag to
1487 * cause any further BIOs to go to the alternative queue.
4e17f465 1488 */
e8599db1 1489 if (ip->flags & HAMMER_INODE_VHELD)
4e17f465
MD
1490 error = vfsync(ip->vp, MNT_NOWAIT, 1, NULL, NULL);
1491 ip->flags |= HAMMER_INODE_WRITE_ALT;
1492
1493 /*
1494 * The buffer cache may contain dirty buffers beyond the inode
1495 * state we copied from the frontend to the backend. Because
1496 * we are syncing our buffer cache on the backend, resync
1497 * the truncation point and the file size so we don't wipe out
1498 * any data.
1499 *
1500 * Syncing the buffer cache on the frontend has serious problems
1501 * because it prevents us from passively queueing dirty inodes
1502 * to the backend (the BIO's could stall indefinitely).
1503 */
1504 if (ip->flags & HAMMER_INODE_TRUNCATED) {
1505 ip->sync_trunc_off = ip->trunc_off;
1506 ip->sync_flags |= HAMMER_INODE_TRUNCATED;
1507 }
11ad5ade
MD
1508 if (ip->sync_ino_data.size != ip->ino_data.size) {
1509 ip->sync_ino_data.size = ip->ino_data.size;
1510 ip->sync_flags |= HAMMER_INODE_DDIRTY;
4e17f465
MD
1511 }
1512
1f07f686 1513 /*
869e8f55
MD
1514 * If there is a trunction queued destroy any data past the (aligned)
1515 * truncation point. Userland will have dealt with the buffer
1516 * containing the truncation point for us.
1517 *
1518 * We don't flush pending frontend data buffers until after we've
1519 * dealth with the truncation.
1f07f686 1520 *
869e8f55 1521 * Don't bother if the inode is or has been deleted.
1f07f686 1522 */
869e8f55 1523 if (ip->sync_flags & HAMMER_INODE_TRUNCATED) {
b84de5af
MD
1524 /*
1525 * Interlock trunc_off. The VOP front-end may continue to
1526 * make adjustments to it while we are blocked.
1527 */
1528 off_t trunc_off;
1529 off_t aligned_trunc_off;
c0ade690 1530
b84de5af
MD
1531 trunc_off = ip->sync_trunc_off;
1532 aligned_trunc_off = (trunc_off + HAMMER_BUFMASK) &
1533 ~HAMMER_BUFMASK64;
1534
1535 /*
1536 * Delete any whole blocks on-media. The front-end has
1537 * already cleaned out any partial block and made it
1538 * pending. The front-end may have updated trunc_off
1539 * while we were blocked so do not just unconditionally
1540 * set it to the maximum offset.
1541 */
4e17f465 1542 error = hammer_ip_delete_range(&cursor, ip,
b84de5af
MD
1543 aligned_trunc_off,
1544 0x7FFFFFFFFFFFFFFFLL);
1545 if (error)
1546 Debugger("hammer_ip_delete_range errored");
1547 ip->sync_flags &= ~HAMMER_INODE_TRUNCATED;
1548 if (ip->trunc_off >= trunc_off) {
1549 ip->trunc_off = 0x7FFFFFFFFFFFFFFFLL;
1550 ip->flags &= ~HAMMER_INODE_TRUNCATED;
1551 }
1f07f686
MD
1552 } else {
1553 error = 0;
f3b0f382
MD
1554 }
1555
1f07f686
MD
1556 /*
1557 * Now sync related records. These will typically be directory
1558 * entries or delete-on-disk records.
869e8f55
MD
1559 *
1560 * Not all records will be flushed, but clear XDIRTY anyway. We
1561 * will set it again in the frontend hammer_flush_inode_done()
1562 * if records remain.
1f07f686
MD
1563 */
1564 if (error == 0) {
1565 tmp_error = RB_SCAN(hammer_rec_rb_tree, &ip->rec_tree, NULL,
4e17f465 1566 hammer_sync_record_callback, &cursor);
1f07f686
MD
1567 if (tmp_error < 0)
1568 tmp_error = -error;
1569 if (tmp_error)
1570 error = tmp_error;
930bf163 1571 if (RB_EMPTY(&ip->rec_tree))
869e8f55 1572 ip->sync_flags &= ~HAMMER_INODE_XDIRTY;
1f07f686
MD
1573 }
1574
1575 /*
869e8f55
MD
1576 * If we are deleting the inode the frontend had better not have
1577 * any active references on elements making up the inode.
1f07f686 1578 */
11ad5ade 1579 if (error == 0 && ip->sync_ino_data.nlinks == 0 &&
869e8f55
MD
1580 RB_EMPTY(&ip->rec_tree) &&
1581 (ip->sync_flags & HAMMER_INODE_DELETING) &&
1582 (ip->flags & HAMMER_INODE_DELETED) == 0) {
1583 int count1 = 0;
1f07f686 1584
77062c8a 1585 hkprintf("Y");
869e8f55 1586 ip->flags |= HAMMER_INODE_DELETED;
4e17f465 1587 error = hammer_ip_delete_range_all(&cursor, ip, &count1);
869e8f55
MD
1588 if (error == 0) {
1589 ip->sync_flags &= ~HAMMER_INODE_DELETING;
1590 ip->sync_flags &= ~HAMMER_INODE_TRUNCATED;
1591 KKASSERT(RB_EMPTY(&ip->rec_tree));
1f07f686 1592
869e8f55
MD
1593 /*
1594 * Set delete_tid in both the frontend and backend
1595 * copy of the inode record. The DELETED flag handles
1596 * this, do not set RDIRTY.
1597 */
11ad5ade
MD
1598 ip->ino_leaf.base.delete_tid = trans.tid;
1599 ip->sync_ino_leaf.base.delete_tid = trans.tid;
1f07f686 1600
869e8f55
MD
1601 /*
1602 * Adjust the inode count in the volume header
1603 */
f36a9737
MD
1604 if (ip->flags & HAMMER_INODE_ONDISK) {
1605 hammer_modify_volume_field(&trans,
1606 trans.rootvol,
1607 vol0_stat_inodes);
1608 --ip->hmp->rootvol->ondisk->vol0_stat_inodes;
1609 hammer_modify_volume_done(trans.rootvol);
1610 }
869e8f55
MD
1611 } else {
1612 ip->flags &= ~HAMMER_INODE_DELETED;
1613 Debugger("hammer_ip_delete_range_all errored");
1614 }
1f07f686 1615 }
b84de5af 1616
059819e3 1617 /*
869e8f55
MD
1618 * Flush any queued BIOs. These will just biodone() the IO's if
1619 * the inode has been deleted.
059819e3
MD
1620 */
1621 while ((bio = TAILQ_FIRST(&ip->bio_list)) != NULL) {
1622 TAILQ_REMOVE(&ip->bio_list, bio, bio_act);
4e17f465 1623 tmp_error = hammer_dowrite(&cursor, ip, bio);
ec4e8497
MD
1624 if (tmp_error)
1625 error = tmp_error;
059819e3 1626 }
b84de5af 1627 ip->sync_flags &= ~HAMMER_INODE_BUFS;
c0ade690 1628
b84de5af
MD
1629 if (error)
1630 Debugger("RB_SCAN errored");
c0ade690
MD
1631
1632 /*
1633 * Now update the inode's on-disk inode-data and/or on-disk record.
b84de5af 1634 * DELETED and ONDISK are managed only in ip->flags.
c0ade690 1635 */
b84de5af 1636 switch(ip->flags & (HAMMER_INODE_DELETED | HAMMER_INODE_ONDISK)) {
c0ade690
MD
1637 case HAMMER_INODE_DELETED|HAMMER_INODE_ONDISK:
1638 /*
1639 * If deleted and on-disk, don't set any additional flags.
1640 * the delete flag takes care of things.
869e8f55
MD
1641 *
1642 * Clear flags which may have been set by the frontend.
c0ade690 1643 */
11ad5ade 1644 ip->sync_flags &= ~(HAMMER_INODE_DDIRTY|
869e8f55
MD
1645 HAMMER_INODE_XDIRTY|HAMMER_INODE_ITIMES|
1646 HAMMER_INODE_DELETING);
c0ade690
MD
1647 break;
1648 case HAMMER_INODE_DELETED:
1649 /*
1650 * Take care of the case where a deleted inode was never
1651 * flushed to the disk in the first place.
869e8f55
MD
1652 *
1653 * Clear flags which may have been set by the frontend.
c0ade690 1654 */
11ad5ade 1655 ip->sync_flags &= ~(HAMMER_INODE_DDIRTY|
869e8f55
MD
1656 HAMMER_INODE_XDIRTY|HAMMER_INODE_ITIMES|
1657 HAMMER_INODE_DELETING);
d26d0ae9 1658 while (RB_ROOT(&ip->rec_tree)) {
d36ec43b
MD
1659 hammer_record_t record = RB_ROOT(&ip->rec_tree);
1660 hammer_ref(&record->lock);
1661 KKASSERT(record->lock.refs == 1);
1662 record->flags |= HAMMER_RECF_DELETED_FE;
1663 record->flags |= HAMMER_RECF_DELETED_BE;
d36ec43b 1664 hammer_rel_mem_record(record);
d26d0ae9 1665 }
c0ade690
MD
1666 break;
1667 case HAMMER_INODE_ONDISK:
1668 /*
1669 * If already on-disk, do not set any additional flags.
1670 */
1671 break;
1672 default:
1673 /*
1674 * If not on-disk and not deleted, set both dirty flags
b84de5af
MD
1675 * to force an initial record to be written. Also set
1676 * the create_tid for the inode.
1677 *
1678 * Set create_tid in both the frontend and backend
1679 * copy of the inode record.
c0ade690 1680 */
11ad5ade
MD
1681 ip->ino_leaf.base.create_tid = trans.tid;
1682 ip->sync_ino_leaf.base.create_tid = trans.tid;
1683 ip->sync_flags |= HAMMER_INODE_DDIRTY;
c0ade690
MD
1684 break;
1685 }
1686
1687 /*
d113fda1
MD
1688 * If RDIRTY or DDIRTY is set, write out a new record. If the inode
1689 * is already on-disk the old record is marked as deleted.
1690 *
1691 * If DELETED is set hammer_update_inode() will delete the existing
1692 * record without writing out a new one.
1693 *
1694 * If *ONLY* the ITIMES flag is set we can update the record in-place.
c0ade690 1695 */
b84de5af 1696 if (ip->flags & HAMMER_INODE_DELETED) {
4e17f465 1697 error = hammer_update_inode(&cursor, ip);
b84de5af 1698 } else
11ad5ade
MD
1699 if ((ip->sync_flags & (HAMMER_INODE_DDIRTY | HAMMER_INODE_ITIMES)) ==
1700 HAMMER_INODE_ITIMES) {
4e17f465 1701 error = hammer_update_itimes(&cursor, ip);
d113fda1 1702 } else
11ad5ade 1703 if (ip->sync_flags & (HAMMER_INODE_DDIRTY | HAMMER_INODE_ITIMES)) {
4e17f465 1704 error = hammer_update_inode(&cursor, ip);
c0ade690 1705 }
b84de5af
MD
1706 if (error)
1707 Debugger("hammer_update_itimes/inode errored");
4e17f465 1708done:
b84de5af
MD
1709 /*
1710 * Save the TID we used to sync the inode with to make sure we
1711 * do not improperly reuse it.
1712 */
4e17f465 1713 hammer_done_cursor(&cursor);
b84de5af 1714 hammer_done_transaction(&trans);
c0ade690 1715 return(error);
8cd0a023
MD
1716}
1717
1f07f686
MD
1718/*
1719 * This routine is called when the OS is no longer actively referencing
1720 * the inode (but might still be keeping it cached), or when releasing
1721 * the last reference to an inode.
1722 *
1723 * At this point if the inode's nlinks count is zero we want to destroy
1724 * it, which may mean destroying it on-media too.
1725 */
3bf2d80a 1726void
e8599db1 1727hammer_inode_unloadable_check(hammer_inode_t ip, int getvp)
1f07f686 1728{
e8599db1 1729 struct vnode *vp;
29ce0677 1730 struct bio *bio;
e8599db1 1731
1f07f686 1732 /*
c4bae5fd
MD
1733 * Set the DELETING flag when the link count drops to 0 and the
1734 * OS no longer has any opens on the inode.
1735 *
1736 * The backend will clear DELETING (a mod flag) and set DELETED
1737 * (a state flag) when it is actually able to perform the
1738 * operation.
1f07f686 1739 */
11ad5ade 1740 if (ip->ino_data.nlinks == 0 &&
869e8f55 1741 (ip->flags & (HAMMER_INODE_DELETING|HAMMER_INODE_DELETED)) == 0) {
e8599db1
MD
1742 ip->flags |= HAMMER_INODE_DELETING;
1743 ip->flags |= HAMMER_INODE_TRUNCATED;
1744 ip->trunc_off = 0;
1745 vp = NULL;
1746 if (getvp) {
1747 if (hammer_get_vnode(ip, &vp) != 0)
1748 return;
1749 }
29ce0677
MD
1750
1751 /*
1752 * biodone any buffers with pending IO. These buffers are
1753 * holding a BUF_KERNPROC() exclusive lock and our
1754 * vtruncbuf() call will deadlock if any remain.
1755 *
1756 * (interlocked against hammer_vop_strategy_write via
1757 * HAMMER_INODE_DELETING|HAMMER_INODE_DELETED).
1758 */
1759 while ((bio = TAILQ_FIRST(&ip->bio_list)) != NULL) {
1760 TAILQ_REMOVE(&ip->bio_list, bio, bio_act);
1761 bio->bio_buf->b_resid = 0;
1762 biodone(bio);
e63644f0
MD
1763 if (ip->rsv_databufs) {
1764 --ip->rsv_databufs;
1765 --ip->hmp->rsv_databufs;
1766 }
29ce0677
MD
1767 }
1768 while ((bio = TAILQ_FIRST(&ip->bio_alt_list)) != NULL) {
1769 TAILQ_REMOVE(&ip->bio_alt_list, bio, bio_act);
1770 bio->bio_buf->b_resid = 0;
1771 biodone(bio);
e63644f0
MD
1772 if (ip->rsv_databufs) {
1773 --ip->rsv_databufs;
1774 --ip->hmp->rsv_databufs;
1775 }
29ce0677
MD
1776 }
1777
1778 /*
1779 * Final cleanup
1780 */
869e8f55
MD
1781 if (ip->vp) {
1782 vtruncbuf(ip->vp, 0, HAMMER_BUFSIZE);
1783 vnode_pager_setsize(ip->vp, 0);
1784 }
e8599db1
MD
1785 if (getvp) {
1786 vput(vp);
1787 }
1f07f686 1788 }
1f07f686
MD
1789}
1790
3bf2d80a
MD
1791/*
1792 * Re-test an inode when a dependancy had gone away to see if we
1793 * can chain flush it.
1794 */
1f07f686
MD
1795void
1796hammer_test_inode(hammer_inode_t ip)
1797{
1798 if (ip->flags & HAMMER_INODE_REFLUSH) {
1799 ip->flags &= ~HAMMER_INODE_REFLUSH;
1800 hammer_ref(&ip->lock);
3bf2d80a
MD
1801 if (ip->flags & HAMMER_INODE_RESIGNAL) {
1802 ip->flags &= ~HAMMER_INODE_RESIGNAL;
1803 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
1804 } else {
1805 hammer_flush_inode(ip, 0);
1806 }
1f07f686
MD
1807 hammer_rel_inode(ip, 0);
1808 }
1809}
1810