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