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