HAMMER VFS - Remove the unused also_ip argument from the cursor API
[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 *
69ce4424 34 * $DragonFly: src/sys/vfs/hammer/hammer_inode.c,v 1.114 2008/09/24 00:53:51 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 42static int hammer_unload_inode(struct hammer_inode *ip);
5fa5c92f 43static void hammer_free_inode(hammer_inode_t ip);
7a61b85d
MD
44static void hammer_flush_inode_core(hammer_inode_t ip,
45 hammer_flush_group_t flg, int flags);
af209b0f 46static int hammer_setup_child_callback(hammer_record_t rec, void *data);
7a61b85d 47#if 0
525aad3a 48static int hammer_syncgrp_child_callback(hammer_record_t rec, void *data);
7a61b85d
MD
49#endif
50static int hammer_setup_parent_inodes(hammer_inode_t ip,
51 hammer_flush_group_t flg);
52static int hammer_setup_parent_inodes_helper(hammer_record_t record,
53 hammer_flush_group_t flg);
82010f9f 54static void hammer_inode_wakereclaims(hammer_inode_t ip, int dowake);
b84de5af 55
0832c9bb
MD
56#ifdef DEBUG_TRUNCATE
57extern struct hammer_inode *HammerTruncIp;
58#endif
59
43c665ae 60/*
5fa5c92f 61 * RB-Tree support for inode structures
43c665ae
MD
62 */
63int
64hammer_ino_rb_compare(hammer_inode_t ip1, hammer_inode_t ip2)
65{
66 if (ip1->obj_localization < ip2->obj_localization)
67 return(-1);
68 if (ip1->obj_localization > ip2->obj_localization)
69 return(1);
70 if (ip1->obj_id < ip2->obj_id)
71 return(-1);
72 if (ip1->obj_id > ip2->obj_id)
73 return(1);
74 if (ip1->obj_asof < ip2->obj_asof)
75 return(-1);
76 if (ip1->obj_asof > ip2->obj_asof)
77 return(1);
78 return(0);
79}
80
81/*
5fa5c92f 82 * RB-Tree support for inode structures / special LOOKUP_INFO
43c665ae
MD
83 */
84static int
85hammer_inode_info_cmp(hammer_inode_info_t info, hammer_inode_t ip)
86{
87 if (info->obj_localization < ip->obj_localization)
88 return(-1);
89 if (info->obj_localization > ip->obj_localization)
90 return(1);
91 if (info->obj_id < ip->obj_id)
92 return(-1);
93 if (info->obj_id > ip->obj_id)
94 return(1);
95 if (info->obj_asof < ip->obj_asof)
96 return(-1);
97 if (info->obj_asof > ip->obj_asof)
98 return(1);
99 return(0);
100}
101
102/*
103 * Used by hammer_scan_inode_snapshots() to locate all of an object's
104 * snapshots. Note that the asof field is not tested, which we can get
105 * away with because it is the lowest-priority field.
106 */
107static int
108hammer_inode_info_cmp_all_history(hammer_inode_t ip, void *data)
109{
110 hammer_inode_info_t info = data;
111
112 if (ip->obj_localization > info->obj_localization)
113 return(1);
114 if (ip->obj_localization < info->obj_localization)
115 return(-1);
116 if (ip->obj_id > info->obj_id)
117 return(1);
118 if (ip->obj_id < info->obj_id)
119 return(-1);
120 return(0);
121}
122
842e7a70
MD
123/*
124 * Used by hammer_unload_pseudofs() to locate all inodes associated with
125 * a particular PFS.
126 */
127static int
128hammer_inode_pfs_cmp(hammer_inode_t ip, void *data)
129{
130 u_int32_t localization = *(u_int32_t *)data;
131 if (ip->obj_localization > localization)
132 return(1);
133 if (ip->obj_localization < localization)
134 return(-1);
135 return(0);
136}
137
5fa5c92f
MD
138/*
139 * RB-Tree support for pseudofs structures
140 */
141static int
142hammer_pfs_rb_compare(hammer_pseudofs_inmem_t p1, hammer_pseudofs_inmem_t p2)
143{
144 if (p1->localization < p2->localization)
145 return(-1);
146 if (p1->localization > p2->localization)
147 return(1);
148 return(0);
149}
150
151
43c665ae
MD
152RB_GENERATE(hammer_ino_rb_tree, hammer_inode, rb_node, hammer_ino_rb_compare);
153RB_GENERATE_XLOOKUP(hammer_ino_rb_tree, INFO, hammer_inode, rb_node,
154 hammer_inode_info_cmp, hammer_inode_info_t);
5fa5c92f
MD
155RB_GENERATE2(hammer_pfs_rb_tree, hammer_pseudofs_inmem, rb_node,
156 hammer_pfs_rb_compare, u_int32_t, localization);
43c665ae 157
d113fda1
MD
158/*
159 * The kernel is not actively referencing this vnode but is still holding
160 * it cached.
b84de5af
MD
161 *
162 * This is called from the frontend.
d113fda1 163 */
427e5fc6
MD
164int
165hammer_vop_inactive(struct vop_inactive_args *ap)
166{
66325755 167 struct hammer_inode *ip = VTOI(ap->a_vp);
27ea2398 168
c0ade690
MD
169 /*
170 * Degenerate case
171 */
172 if (ip == NULL) {
66325755 173 vrecycle(ap->a_vp);
c0ade690
MD
174 return(0);
175 }
176
177 /*
4a2796f3
MD
178 * If the inode no longer has visibility in the filesystem try to
179 * recycle it immediately, even if the inode is dirty. Recycling
180 * it quickly allows the system to reclaim buffer cache and VM
181 * resources which can matter a lot in a heavily loaded system.
182 *
183 * This can deadlock in vfsync() if we aren't careful.
4e97774c
MD
184 *
185 * Do not queue the inode to the flusher if we still have visibility,
186 * otherwise namespace calls such as chmod will unnecessarily generate
187 * multiple inode updates.
c0ade690 188 */
e8599db1 189 hammer_inode_unloadable_check(ip, 0);
4e97774c
MD
190 if (ip->ino_data.nlinks == 0) {
191 if (ip->flags & HAMMER_INODE_MODMASK)
192 hammer_flush_inode(ip, 0);
4a2796f3 193 vrecycle(ap->a_vp);
4e97774c 194 }
427e5fc6
MD
195 return(0);
196}
197
d113fda1
MD
198/*
199 * Release the vnode association. This is typically (but not always)
1f07f686 200 * the last reference on the inode.
d113fda1 201 *
1f07f686
MD
202 * Once the association is lost we are on our own with regards to
203 * flushing the inode.
d113fda1 204 */
427e5fc6
MD
205int
206hammer_vop_reclaim(struct vop_reclaim_args *ap)
207{
427e5fc6 208 struct hammer_inode *ip;
7bc5b8c2 209 hammer_mount_t hmp;
427e5fc6
MD
210 struct vnode *vp;
211
212 vp = ap->a_vp;
c0ade690 213
a89aec1b 214 if ((ip = vp->v_data) != NULL) {
da2da375 215 hmp = ip->hmp;
a89aec1b
MD
216 vp->v_data = NULL;
217 ip->vp = NULL;
7bc5b8c2 218
4a2796f3 219 if ((ip->flags & HAMMER_INODE_RECLAIM) == 0) {
9f5097dc 220 ++hammer_count_reclaiming;
da2da375 221 ++hmp->inode_reclaims;
9f5097dc
MD
222 ip->flags |= HAMMER_INODE_RECLAIM;
223 }
ec4e8497 224 hammer_rel_inode(ip, 1);
a89aec1b 225 }
427e5fc6
MD
226 return(0);
227}
228
66325755
MD
229/*
230 * Return a locked vnode for the specified inode. The inode must be
231 * referenced but NOT LOCKED on entry and will remain referenced on
232 * return.
b84de5af
MD
233 *
234 * Called from the frontend.
66325755
MD
235 */
236int
e8599db1 237hammer_get_vnode(struct hammer_inode *ip, struct vnode **vpp)
66325755 238{
9f5097dc 239 hammer_mount_t hmp;
66325755
MD
240 struct vnode *vp;
241 int error = 0;
ea434b6f 242 u_int8_t obj_type;
66325755 243
9f5097dc
MD
244 hmp = ip->hmp;
245
66325755
MD
246 for (;;) {
247 if ((vp = ip->vp) == NULL) {
9f5097dc 248 error = getnewvnode(VT_HAMMER, hmp->mp, vpp, 0, 0);
66325755
MD
249 if (error)
250 break;
8cd0a023
MD
251 hammer_lock_ex(&ip->lock);
252 if (ip->vp != NULL) {
253 hammer_unlock(&ip->lock);
254 vp->v_type = VBAD;
255 vx_put(vp);
256 continue;
66325755 257 }
8cd0a023
MD
258 hammer_ref(&ip->lock);
259 vp = *vpp;
260 ip->vp = vp;
ea434b6f
MD
261
262 obj_type = ip->ino_data.obj_type;
263 vp->v_type = hammer_get_vnode_type(obj_type);
7a04d74f 264
82010f9f 265 hammer_inode_wakereclaims(ip, 0);
9f5097dc 266
11ad5ade 267 switch(ip->ino_data.obj_type) {
7a04d74f
MD
268 case HAMMER_OBJTYPE_CDEV:
269 case HAMMER_OBJTYPE_BDEV:
9f5097dc 270 vp->v_ops = &hmp->mp->mnt_vn_spec_ops;
7a04d74f
MD
271 addaliasu(vp, ip->ino_data.rmajor,
272 ip->ino_data.rminor);
273 break;
274 case HAMMER_OBJTYPE_FIFO:
9f5097dc 275 vp->v_ops = &hmp->mp->mnt_vn_fifo_ops;
7a04d74f
MD
276 break;
277 default:
278 break;
279 }
42c7d26b
MD
280
281 /*
282 * Only mark as the root vnode if the ip is not
283 * historical, otherwise the VFS cache will get
284 * confused. The other half of the special handling
285 * is in hammer_vop_nlookupdotdot().
ddfdf542 286 *
67863d04
MD
287 * Pseudo-filesystem roots can be accessed via
288 * non-root filesystem paths and setting VROOT may
289 * confuse the namecache. Set VPFSROOT instead.
42c7d26b
MD
290 */
291 if (ip->obj_id == HAMMER_OBJID_ROOT &&
67863d04
MD
292 ip->obj_asof == hmp->asof) {
293 if (ip->obj_localization == 0)
294 vp->v_flag |= VROOT;
295 else
296 vp->v_flag |= VPFSROOT;
42c7d26b 297 }
7a04d74f 298
8cd0a023
MD
299 vp->v_data = (void *)ip;
300 /* vnode locked by getnewvnode() */
301 /* make related vnode dirty if inode dirty? */
302 hammer_unlock(&ip->lock);
a89aec1b 303 if (vp->v_type == VREG)
11ad5ade 304 vinitvmio(vp, ip->ino_data.size);
8cd0a023
MD
305 break;
306 }
307
308 /*
309 * loop if the vget fails (aka races), or if the vp
310 * no longer matches ip->vp.
311 */
312 if (vget(vp, LK_EXCLUSIVE) == 0) {
313 if (vp == ip->vp)
314 break;
315 vput(vp);
66325755
MD
316 }
317 }
a89aec1b 318 *vpp = vp;
66325755
MD
319 return(error);
320}
321
43c665ae
MD
322/*
323 * Locate all copies of the inode for obj_id compatible with the specified
324 * asof, reference, and issue the related call-back. This routine is used
325 * for direct-io invalidation and does not create any new inodes.
326 */
327void
328hammer_scan_inode_snapshots(hammer_mount_t hmp, hammer_inode_info_t iinfo,
329 int (*callback)(hammer_inode_t ip, void *data),
330 void *data)
331{
332 hammer_ino_rb_tree_RB_SCAN(&hmp->rb_inos_root,
333 hammer_inode_info_cmp_all_history,
334 callback, iinfo);
335}
336
66325755 337/*
8cd0a023
MD
338 * Acquire a HAMMER inode. The returned inode is not locked. These functions
339 * do not attach or detach the related vnode (use hammer_get_vnode() for
340 * that).
d113fda1
MD
341 *
342 * The flags argument is only applied for newly created inodes, and only
343 * certain flags are inherited.
b84de5af
MD
344 *
345 * Called from the frontend.
66325755
MD
346 */
347struct hammer_inode *
bcac4bbb 348hammer_get_inode(hammer_transaction_t trans, hammer_inode_t dip,
adf01747 349 int64_t obj_id, hammer_tid_t asof, u_int32_t localization,
ddfdf542 350 int flags, int *errorp)
66325755 351{
36f82b23 352 hammer_mount_t hmp = trans->hmp;
427e5fc6 353 struct hammer_inode_info iinfo;
8cd0a023 354 struct hammer_cursor cursor;
427e5fc6 355 struct hammer_inode *ip;
427e5fc6 356
5fa5c92f 357
427e5fc6
MD
358 /*
359 * Determine if we already have an inode cached. If we do then
360 * we are golden.
82010f9f
MD
361 *
362 * If we find an inode with no vnode we have to mark the
363 * transaction such that hammer_inode_waitreclaims() is
364 * called later on to avoid building up an infinite number
365 * of inodes. Otherwise we can continue to * add new inodes
366 * faster then they can be disposed of, even with the tsleep
367 * delay.
427e5fc6 368 */
66325755 369 iinfo.obj_id = obj_id;
7f7c1f84 370 iinfo.obj_asof = asof;
ddfdf542 371 iinfo.obj_localization = localization;
427e5fc6
MD
372loop:
373 ip = hammer_ino_rb_tree_RB_LOOKUP_INFO(&hmp->rb_inos_root, &iinfo);
374 if (ip) {
82010f9f
MD
375#if 0
376 if (ip->vp == NULL)
377 trans->flags |= HAMMER_TRANSF_NEWINODE;
378#endif
8cd0a023 379 hammer_ref(&ip->lock);
66325755
MD
380 *errorp = 0;
381 return(ip);
427e5fc6
MD
382 }
383
3897d7e9
MD
384 /*
385 * Allocate a new inode structure and deal with races later.
386 */
bac808fe 387 ip = kmalloc(sizeof(*ip), hmp->m_inodes, M_WAITOK|M_ZERO);
b3deaf57 388 ++hammer_count_inodes;
9f5097dc 389 ++hmp->count_inodes;
66325755 390 ip->obj_id = obj_id;
27ea2398 391 ip->obj_asof = iinfo.obj_asof;
ddfdf542 392 ip->obj_localization = localization;
66325755 393 ip->hmp = hmp;
d113fda1 394 ip->flags = flags & HAMMER_INODE_RO;
bcac4bbb
MD
395 ip->cache[0].ip = ip;
396 ip->cache[1].ip = ip;
5fa5c92f 397 if (hmp->ronly)
d113fda1 398 ip->flags |= HAMMER_INODE_RO;
a9d52b76
MD
399 ip->sync_trunc_off = ip->trunc_off = ip->save_trunc_off =
400 0x7FFFFFFFFFFFFFFFLL;
8cd0a023 401 RB_INIT(&ip->rec_tree);
1f07f686 402 TAILQ_INIT(&ip->target_list);
5fa5c92f 403 hammer_ref(&ip->lock);
427e5fc6
MD
404
405 /*
ea434b6f
MD
406 * Locate the on-disk inode. If this is a PFS root we always
407 * access the current version of the root inode and (if it is not
408 * a master) always access information under it with a snapshot
409 * TID.
427e5fc6 410 */
6a37e7e4 411retry:
bcac4bbb 412 hammer_init_cursor(trans, &cursor, (dip ? &dip->cache[0] : NULL), NULL);
5a930e66 413 cursor.key_beg.localization = localization + HAMMER_LOCALIZE_INODE;
8cd0a023
MD
414 cursor.key_beg.obj_id = ip->obj_id;
415 cursor.key_beg.key = 0;
d5530d22 416 cursor.key_beg.create_tid = 0;
8cd0a023
MD
417 cursor.key_beg.delete_tid = 0;
418 cursor.key_beg.rec_type = HAMMER_RECTYPE_INODE;
419 cursor.key_beg.obj_type = 0;
ea434b6f 420
d5530d22 421 cursor.asof = iinfo.obj_asof;
11ad5ade 422 cursor.flags = HAMMER_CURSOR_GET_LEAF | HAMMER_CURSOR_GET_DATA |
d5530d22 423 HAMMER_CURSOR_ASOF;
8cd0a023
MD
424
425 *errorp = hammer_btree_lookup(&cursor);
6a37e7e4
MD
426 if (*errorp == EDEADLK) {
427 hammer_done_cursor(&cursor);
428 goto retry;
429 }
427e5fc6
MD
430
431 /*
432 * On success the B-Tree lookup will hold the appropriate
433 * buffer cache buffers and provide a pointer to the requested
d113fda1
MD
434 * information. Copy the information to the in-memory inode
435 * and cache the B-Tree node to improve future operations.
427e5fc6 436 */
66325755 437 if (*errorp == 0) {
11ad5ade 438 ip->ino_leaf = cursor.node->ondisk->elms[cursor.index].leaf;
40043e7f 439 ip->ino_data = cursor.data->inode;
bcac4bbb
MD
440
441 /*
442 * cache[0] tries to cache the location of the object inode.
443 * The assumption is that it is near the directory inode.
444 *
445 * cache[1] tries to cache the location of the object data.
446 * The assumption is that it is near the directory data.
447 */
448 hammer_cache_node(&ip->cache[0], cursor.node);
449 if (dip && dip->cache[1].node)
450 hammer_cache_node(&ip->cache[1], dip->cache[1].node);
cb51be26
MD
451
452 /*
453 * The file should not contain any data past the file size
a9d52b76 454 * stored in the inode. Setting save_trunc_off to the
cb51be26
MD
455 * file size instead of max reduces B-Tree lookup overheads
456 * on append by allowing the flusher to avoid checking for
457 * record overwrites.
458 */
a9d52b76 459 ip->save_trunc_off = ip->ino_data.size;
5fa5c92f
MD
460
461 /*
462 * Locate and assign the pseudofs management structure to
463 * the inode.
464 */
465 if (dip && dip->obj_localization == ip->obj_localization) {
466 ip->pfsm = dip->pfsm;
467 hammer_ref(&ip->pfsm->lock);
468 } else {
ea434b6f
MD
469 ip->pfsm = hammer_load_pseudofs(trans,
470 ip->obj_localization,
471 errorp);
472 *errorp = 0; /* ignore ENOENT */
5fa5c92f 473 }
427e5fc6 474 }
427e5fc6
MD
475
476 /*
cb51be26
MD
477 * The inode is placed on the red-black tree and will be synced to
478 * the media when flushed or by the filesystem sync. If this races
479 * another instantiation/lookup the insertion will fail.
427e5fc6 480 */
66325755 481 if (*errorp == 0) {
427e5fc6 482 if (RB_INSERT(hammer_ino_rb_tree, &hmp->rb_inos_root, ip)) {
5fa5c92f 483 hammer_free_inode(ip);
b3deaf57 484 hammer_done_cursor(&cursor);
427e5fc6
MD
485 goto loop;
486 }
c0ade690 487 ip->flags |= HAMMER_INODE_ONDISK;
427e5fc6 488 } else {
e63644f0
MD
489 if (ip->flags & HAMMER_INODE_RSV_INODES) {
490 ip->flags &= ~HAMMER_INODE_RSV_INODES; /* sanity */
9f5097dc 491 --hmp->rsv_inodes;
e63644f0 492 }
e63644f0 493
5fa5c92f 494 hammer_free_inode(ip);
66325755 495 ip = NULL;
427e5fc6 496 }
b3deaf57 497 hammer_done_cursor(&cursor);
21fde338 498 trans->flags |= HAMMER_TRANSF_NEWINODE;
66325755
MD
499 return (ip);
500}
501
8cd0a023
MD
502/*
503 * Create a new filesystem object, returning the inode in *ipp. The
ea434b6f 504 * returned inode will be referenced. The inode is created in-memory.
8cd0a023 505 *
ea434b6f
MD
506 * If pfsm is non-NULL the caller wishes to create the root inode for
507 * a master PFS.
8cd0a023
MD
508 */
509int
a89aec1b
MD
510hammer_create_inode(hammer_transaction_t trans, struct vattr *vap,
511 struct ucred *cred, hammer_inode_t dip,
ea434b6f 512 hammer_pseudofs_inmem_t pfsm, struct hammer_inode **ipp)
66325755 513{
a89aec1b
MD
514 hammer_mount_t hmp;
515 hammer_inode_t ip;
6b4f890b 516 uid_t xuid;
5a930e66 517 int error;
66325755 518
8cd0a023 519 hmp = trans->hmp;
5a930e66 520
bac808fe 521 ip = kmalloc(sizeof(*ip), hmp->m_inodes, M_WAITOK|M_ZERO);
b3deaf57 522 ++hammer_count_inodes;
9f5097dc 523 ++hmp->count_inodes;
82010f9f 524 trans->flags |= HAMMER_TRANSF_NEWINODE;
5a930e66 525
ea434b6f
MD
526 if (pfsm) {
527 KKASSERT(pfsm->localization != 0);
5a930e66 528 ip->obj_id = HAMMER_OBJID_ROOT;
ea434b6f
MD
529 ip->obj_localization = pfsm->localization;
530 } else {
531 KKASSERT(dip != NULL);
5de0c0e5 532 ip->obj_id = hammer_alloc_objid(hmp, dip);
ea434b6f
MD
533 ip->obj_localization = dip->obj_localization;
534 }
5a930e66 535
8cd0a023 536 KKASSERT(ip->obj_id != 0);
7f7c1f84 537 ip->obj_asof = hmp->asof;
8cd0a023 538 ip->hmp = hmp;
b84de5af 539 ip->flush_state = HAMMER_FST_IDLE;
ddfdf542
MD
540 ip->flags = HAMMER_INODE_DDIRTY |
541 HAMMER_INODE_ATIME | HAMMER_INODE_MTIME;
bcac4bbb
MD
542 ip->cache[0].ip = ip;
543 ip->cache[1].ip = ip;
8cd0a023 544
a5fddc16 545 ip->trunc_off = 0x7FFFFFFFFFFFFFFFLL;
a9d52b76 546 /* ip->save_trunc_off = 0; (already zero) */
8cd0a023 547 RB_INIT(&ip->rec_tree);
1f07f686 548 TAILQ_INIT(&ip->target_list);
8cd0a023 549
bcac4bbb 550 ip->ino_data.atime = trans->time;
11ad5ade
MD
551 ip->ino_data.mtime = trans->time;
552 ip->ino_data.size = 0;
553 ip->ino_data.nlinks = 0;
e63644f0
MD
554
555 /*
556 * A nohistory designator on the parent directory is inherited by
5a930e66
MD
557 * the child. We will do this even for pseudo-fs creation... the
558 * sysad can turn it off.
e63644f0 559 */
ea434b6f
MD
560 if (dip) {
561 ip->ino_data.uflags = dip->ino_data.uflags &
562 (SF_NOHISTORY|UF_NOHISTORY|UF_NODUMP);
563 }
e63644f0 564
11ad5ade 565 ip->ino_leaf.base.btype = HAMMER_BTREE_TYPE_RECORD;
5a930e66
MD
566 ip->ino_leaf.base.localization = ip->obj_localization +
567 HAMMER_LOCALIZE_INODE;
11ad5ade
MD
568 ip->ino_leaf.base.obj_id = ip->obj_id;
569 ip->ino_leaf.base.key = 0;
570 ip->ino_leaf.base.create_tid = 0;
571 ip->ino_leaf.base.delete_tid = 0;
572 ip->ino_leaf.base.rec_type = HAMMER_RECTYPE_INODE;
573 ip->ino_leaf.base.obj_type = hammer_get_obj_type(vap->va_type);
574
575 ip->ino_data.obj_type = ip->ino_leaf.base.obj_type;
8cd0a023
MD
576 ip->ino_data.version = HAMMER_INODE_DATA_VERSION;
577 ip->ino_data.mode = vap->va_mode;
b84de5af 578 ip->ino_data.ctime = trans->time;
5a930e66 579
5e435c92
MD
580 /*
581 * If we are running version 2 or greater we use dirhash algorithm #1
582 * which is semi-sorted. Algorithm #0 was just a pure crc.
583 */
584 if (trans->hmp->version >= HAMMER_VOL_VERSION_TWO) {
585 if (ip->ino_leaf.base.obj_type == HAMMER_OBJTYPE_DIRECTORY) {
586 ip->ino_data.cap_flags |= HAMMER_INODE_CAP_DIRHASH_ALG1;
587 }
588 }
589
5a930e66
MD
590 /*
591 * Setup the ".." pointer. This only needs to be done for directories
592 * but we do it for all objects as a recovery aid.
ea434b6f
MD
593 */
594 if (dip)
595 ip->ino_data.parent_obj_id = dip->ino_leaf.base.obj_id;
596#if 0
597 /*
5a930e66 598 * The parent_obj_localization field only applies to pseudo-fs roots.
ea434b6f
MD
599 * XXX this is no longer applicable, PFSs are no longer directly
600 * tied into the parent's directory structure.
5a930e66 601 */
5a930e66
MD
602 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_DIRECTORY &&
603 ip->obj_id == HAMMER_OBJID_ROOT) {
604 ip->ino_data.ext.obj.parent_obj_localization =
605 dip->obj_localization;
606 }
ea434b6f 607#endif
6b4f890b 608
11ad5ade 609 switch(ip->ino_leaf.base.obj_type) {
7a04d74f
MD
610 case HAMMER_OBJTYPE_CDEV:
611 case HAMMER_OBJTYPE_BDEV:
612 ip->ino_data.rmajor = vap->va_rmajor;
613 ip->ino_data.rminor = vap->va_rminor;
614 break;
615 default:
616 break;
617 }
618
6b4f890b
MD
619 /*
620 * Calculate default uid/gid and overwrite with information from
621 * the vap.
622 */
ea434b6f
MD
623 if (dip) {
624 xuid = hammer_to_unix_xid(&dip->ino_data.uid);
625 xuid = vop_helper_create_uid(hmp->mp, dip->ino_data.mode,
626 xuid, cred, &vap->va_mode);
627 } else {
628 xuid = 0;
629 }
6b4f890b
MD
630 ip->ino_data.mode = vap->va_mode;
631
8cd0a023
MD
632 if (vap->va_vaflags & VA_UID_UUID_VALID)
633 ip->ino_data.uid = vap->va_uid_uuid;
6b4f890b 634 else if (vap->va_uid != (uid_t)VNOVAL)
7538695e
MD
635 hammer_guid_to_uuid(&ip->ino_data.uid, vap->va_uid);
636 else
6b4f890b 637 hammer_guid_to_uuid(&ip->ino_data.uid, xuid);
7538695e 638
8cd0a023
MD
639 if (vap->va_vaflags & VA_GID_UUID_VALID)
640 ip->ino_data.gid = vap->va_gid_uuid;
6b4f890b 641 else if (vap->va_gid != (gid_t)VNOVAL)
8cd0a023 642 hammer_guid_to_uuid(&ip->ino_data.gid, vap->va_gid);
ea434b6f 643 else if (dip)
7538695e 644 ip->ino_data.gid = dip->ino_data.gid;
8cd0a023
MD
645
646 hammer_ref(&ip->lock);
5fa5c92f 647
ea434b6f
MD
648 if (pfsm) {
649 ip->pfsm = pfsm;
650 hammer_ref(&pfsm->lock);
651 error = 0;
652 } else if (dip->obj_localization == ip->obj_localization) {
5fa5c92f
MD
653 ip->pfsm = dip->pfsm;
654 hammer_ref(&ip->pfsm->lock);
655 error = 0;
656 } else {
ea434b6f
MD
657 ip->pfsm = hammer_load_pseudofs(trans,
658 ip->obj_localization,
659 &error);
660 error = 0; /* ignore ENOENT */
5fa5c92f
MD
661 }
662
663 if (error) {
664 hammer_free_inode(ip);
665 ip = NULL;
666 } else if (RB_INSERT(hammer_ino_rb_tree, &hmp->rb_inos_root, ip)) {
a89aec1b 667 panic("hammer_create_inode: duplicate obj_id %llx", ip->obj_id);
5fa5c92f
MD
668 /* not reached */
669 hammer_free_inode(ip);
8cd0a023
MD
670 }
671 *ipp = ip;
5fa5c92f
MD
672 return(error);
673}
674
675/*
676 * Final cleanup / freeing of an inode structure
677 */
678static void
679hammer_free_inode(hammer_inode_t ip)
680{
bac808fe
MD
681 struct hammer_mount *hmp;
682
683 hmp = ip->hmp;
5fa5c92f
MD
684 KKASSERT(ip->lock.refs == 1);
685 hammer_uncache_node(&ip->cache[0]);
686 hammer_uncache_node(&ip->cache[1]);
82010f9f 687 hammer_inode_wakereclaims(ip, 1);
5fa5c92f
MD
688 if (ip->objid_cache)
689 hammer_clear_objid(ip);
690 --hammer_count_inodes;
bac808fe 691 --hmp->count_inodes;
5fa5c92f 692 if (ip->pfsm) {
bac808fe 693 hammer_rel_pseudofs(hmp, ip->pfsm);
5fa5c92f
MD
694 ip->pfsm = NULL;
695 }
bac808fe 696 kfree(ip, hmp->m_inodes);
5fa5c92f
MD
697 ip = NULL;
698}
699
700/*
ea434b6f
MD
701 * Retrieve pseudo-fs data. NULL will never be returned.
702 *
703 * If an error occurs *errorp will be set and a default template is returned,
704 * otherwise *errorp is set to 0. Typically when an error occurs it will
705 * be ENOENT.
5fa5c92f 706 */
ea434b6f
MD
707hammer_pseudofs_inmem_t
708hammer_load_pseudofs(hammer_transaction_t trans,
709 u_int32_t localization, int *errorp)
5fa5c92f
MD
710{
711 hammer_mount_t hmp = trans->hmp;
ea434b6f 712 hammer_inode_t ip;
5fa5c92f
MD
713 hammer_pseudofs_inmem_t pfsm;
714 struct hammer_cursor cursor;
5fa5c92f
MD
715 int bytes;
716
717retry:
ea434b6f 718 pfsm = RB_LOOKUP(hammer_pfs_rb_tree, &hmp->rb_pfsm_root, localization);
5fa5c92f 719 if (pfsm) {
5fa5c92f 720 hammer_ref(&pfsm->lock);
ea434b6f
MD
721 *errorp = 0;
722 return(pfsm);
723 }
724
725 /*
726 * PFS records are stored in the root inode (not the PFS root inode,
727 * but the real root). Avoid an infinite recursion if loading
728 * the PFS for the real root.
729 */
730 if (localization) {
731 ip = hammer_get_inode(trans, NULL, HAMMER_OBJID_ROOT,
732 HAMMER_MAX_TID,
733 HAMMER_DEF_LOCALIZATION, 0, errorp);
734 } else {
735 ip = NULL;
5fa5c92f
MD
736 }
737
bac808fe 738 pfsm = kmalloc(sizeof(*pfsm), hmp->m_misc, M_WAITOK | M_ZERO);
ea434b6f 739 pfsm->localization = localization;
a56cb012
MD
740 pfsm->pfsd.unique_uuid = trans->rootvol->ondisk->vol_fsid;
741 pfsm->pfsd.shared_uuid = pfsm->pfsd.unique_uuid;
5fa5c92f 742
ea434b6f
MD
743 hammer_init_cursor(trans, &cursor, (ip ? &ip->cache[1] : NULL), ip);
744 cursor.key_beg.localization = HAMMER_DEF_LOCALIZATION +
5fa5c92f
MD
745 HAMMER_LOCALIZE_MISC;
746 cursor.key_beg.obj_id = HAMMER_OBJID_ROOT;
747 cursor.key_beg.create_tid = 0;
748 cursor.key_beg.delete_tid = 0;
ea434b6f 749 cursor.key_beg.rec_type = HAMMER_RECTYPE_PFS;
5fa5c92f 750 cursor.key_beg.obj_type = 0;
ea434b6f 751 cursor.key_beg.key = localization;
5fa5c92f
MD
752 cursor.asof = HAMMER_MAX_TID;
753 cursor.flags |= HAMMER_CURSOR_ASOF;
754
ea434b6f
MD
755 if (ip)
756 *errorp = hammer_ip_lookup(&cursor);
757 else
758 *errorp = hammer_btree_lookup(&cursor);
759 if (*errorp == 0) {
760 *errorp = hammer_ip_resolve_data(&cursor);
761 if (*errorp == 0) {
842e7a70
MD
762 if (cursor.data->pfsd.mirror_flags &
763 HAMMER_PFSD_DELETED) {
764 *errorp = ENOENT;
765 } else {
766 bytes = cursor.leaf->data_len;
767 if (bytes > sizeof(pfsm->pfsd))
768 bytes = sizeof(pfsm->pfsd);
769 bcopy(cursor.data, &pfsm->pfsd, bytes);
770 }
5fa5c92f 771 }
5fa5c92f 772 }
5fa5c92f
MD
773 hammer_done_cursor(&cursor);
774
ea434b6f
MD
775 pfsm->fsid_udev = hammer_fsid_to_udev(&pfsm->pfsd.shared_uuid);
776 hammer_ref(&pfsm->lock);
777 if (ip)
778 hammer_rel_inode(ip, 0);
779 if (RB_INSERT(hammer_pfs_rb_tree, &hmp->rb_pfsm_root, pfsm)) {
bac808fe 780 kfree(pfsm, hmp->m_misc);
ea434b6f 781 goto retry;
5fa5c92f 782 }
ea434b6f 783 return(pfsm);
5fa5c92f
MD
784}
785
786/*
787 * Store pseudo-fs data. The backend will automatically delete any prior
788 * on-disk pseudo-fs data but we have to delete in-memory versions.
789 */
790int
ea434b6f 791hammer_save_pseudofs(hammer_transaction_t trans, hammer_pseudofs_inmem_t pfsm)
5fa5c92f
MD
792{
793 struct hammer_cursor cursor;
5fa5c92f 794 hammer_record_t record;
ea434b6f 795 hammer_inode_t ip;
5fa5c92f
MD
796 int error;
797
ea434b6f
MD
798 ip = hammer_get_inode(trans, NULL, HAMMER_OBJID_ROOT, HAMMER_MAX_TID,
799 HAMMER_DEF_LOCALIZATION, 0, &error);
5fa5c92f 800retry:
a56cb012 801 pfsm->fsid_udev = hammer_fsid_to_udev(&pfsm->pfsd.shared_uuid);
5fa5c92f
MD
802 hammer_init_cursor(trans, &cursor, &ip->cache[1], ip);
803 cursor.key_beg.localization = ip->obj_localization +
804 HAMMER_LOCALIZE_MISC;
ea434b6f 805 cursor.key_beg.obj_id = HAMMER_OBJID_ROOT;
5fa5c92f
MD
806 cursor.key_beg.create_tid = 0;
807 cursor.key_beg.delete_tid = 0;
ea434b6f 808 cursor.key_beg.rec_type = HAMMER_RECTYPE_PFS;
5fa5c92f 809 cursor.key_beg.obj_type = 0;
ea434b6f 810 cursor.key_beg.key = pfsm->localization;
5fa5c92f
MD
811 cursor.asof = HAMMER_MAX_TID;
812 cursor.flags |= HAMMER_CURSOR_ASOF;
813
814 error = hammer_ip_lookup(&cursor);
815 if (error == 0 && hammer_cursor_inmem(&cursor)) {
816 record = cursor.iprec;
817 if (record->flags & HAMMER_RECF_INTERLOCK_BE) {
818 KKASSERT(cursor.deadlk_rec == NULL);
819 hammer_ref(&record->lock);
820 cursor.deadlk_rec = record;
821 error = EDEADLK;
822 } else {
823 record->flags |= HAMMER_RECF_DELETED_FE;
824 error = 0;
825 }
826 }
827 if (error == 0 || error == ENOENT) {
828 record = hammer_alloc_mem_record(ip, sizeof(pfsm->pfsd));
829 record->type = HAMMER_MEM_RECORD_GENERAL;
830
831 record->leaf.base.localization = ip->obj_localization +
832 HAMMER_LOCALIZE_MISC;
ea434b6f
MD
833 record->leaf.base.rec_type = HAMMER_RECTYPE_PFS;
834 record->leaf.base.key = pfsm->localization;
5fa5c92f
MD
835 record->leaf.data_len = sizeof(pfsm->pfsd);
836 bcopy(&pfsm->pfsd, record->data, sizeof(pfsm->pfsd));
837 error = hammer_ip_add_record(trans, record);
838 }
839 hammer_done_cursor(&cursor);
840 if (error == EDEADLK)
841 goto retry;
ea434b6f
MD
842 hammer_rel_inode(ip, 0);
843 return(error);
844}
845
846/*
847 * Create a root directory for a PFS if one does not alredy exist.
4c038e17
MD
848 *
849 * The PFS root stands alone so we must also bump the nlinks count
850 * to prevent it from being destroyed on release.
ea434b6f
MD
851 */
852int
853hammer_mkroot_pseudofs(hammer_transaction_t trans, struct ucred *cred,
854 hammer_pseudofs_inmem_t pfsm)
855{
856 hammer_inode_t ip;
857 struct vattr vap;
858 int error;
859
860 ip = hammer_get_inode(trans, NULL, HAMMER_OBJID_ROOT, HAMMER_MAX_TID,
861 pfsm->localization, 0, &error);
862 if (ip == NULL) {
863 vattr_null(&vap);
864 vap.va_mode = 0755;
865 vap.va_type = VDIR;
866 error = hammer_create_inode(trans, &vap, cred, NULL, pfsm, &ip);
4c038e17
MD
867 if (error == 0) {
868 ++ip->ino_data.nlinks;
869 hammer_modify_inode(ip, HAMMER_INODE_DDIRTY);
870 }
5fa5c92f 871 }
ea434b6f
MD
872 if (ip)
873 hammer_rel_inode(ip, 0);
5fa5c92f
MD
874 return(error);
875}
876
842e7a70
MD
877/*
878 * Unload any vnodes & inodes associated with a PFS, return ENOTEMPTY
879 * if we are unable to disassociate all the inodes.
880 */
881static
882int
883hammer_unload_pseudofs_callback(hammer_inode_t ip, void *data)
884{
885 int res;
886
887 hammer_ref(&ip->lock);
888 if (ip->lock.refs == 2 && ip->vp)
889 vclean_unlocked(ip->vp);
890 if (ip->lock.refs == 1 && ip->vp == NULL)
891 res = 0;
892 else
893 res = -1; /* stop, someone is using the inode */
894 hammer_rel_inode(ip, 0);
895 return(res);
896}
897
898int
899hammer_unload_pseudofs(hammer_transaction_t trans, u_int32_t localization)
900{
901 int res;
902 int try;
903
904 for (try = res = 0; try < 4; ++try) {
905 res = hammer_ino_rb_tree_RB_SCAN(&trans->hmp->rb_inos_root,
906 hammer_inode_pfs_cmp,
907 hammer_unload_pseudofs_callback,
908 &localization);
909 if (res == 0 && try > 1)
910 break;
911 hammer_flusher_sync(trans->hmp);
912 }
913 if (res != 0)
914 res = ENOTEMPTY;
915 return(res);
916}
917
918
ea434b6f
MD
919/*
920 * Release a reference on a PFS
921 */
5fa5c92f
MD
922void
923hammer_rel_pseudofs(hammer_mount_t hmp, hammer_pseudofs_inmem_t pfsm)
924{
925 hammer_unref(&pfsm->lock);
926 if (pfsm->lock.refs == 0) {
927 RB_REMOVE(hammer_pfs_rb_tree, &hmp->rb_pfsm_root, pfsm);
bac808fe 928 kfree(pfsm, hmp->m_misc);
5fa5c92f 929 }
66325755
MD
930}
931
d113fda1
MD
932/*
933 * Called by hammer_sync_inode().
934 */
935static int
4e17f465 936hammer_update_inode(hammer_cursor_t cursor, hammer_inode_t ip)
c0ade690 937{
4e17f465 938 hammer_transaction_t trans = cursor->trans;
c0ade690
MD
939 hammer_record_t record;
940 int error;
06ad81ff 941 int redirty;
c0ade690 942
d26d0ae9 943retry:
c0ade690
MD
944 error = 0;
945
869e8f55
MD
946 /*
947 * If the inode has a presence on-disk then locate it and mark
948 * it deleted, setting DELONDISK.
949 *
950 * The record may or may not be physically deleted, depending on
951 * the retention policy.
952 */
76376933
MD
953 if ((ip->flags & (HAMMER_INODE_ONDISK|HAMMER_INODE_DELONDISK)) ==
954 HAMMER_INODE_ONDISK) {
4e17f465 955 hammer_normalize_cursor(cursor);
5a930e66
MD
956 cursor->key_beg.localization = ip->obj_localization +
957 HAMMER_LOCALIZE_INODE;
4e17f465
MD
958 cursor->key_beg.obj_id = ip->obj_id;
959 cursor->key_beg.key = 0;
960 cursor->key_beg.create_tid = 0;
961 cursor->key_beg.delete_tid = 0;
962 cursor->key_beg.rec_type = HAMMER_RECTYPE_INODE;
963 cursor->key_beg.obj_type = 0;
964 cursor->asof = ip->obj_asof;
965 cursor->flags &= ~HAMMER_CURSOR_INITMASK;
11ad5ade 966 cursor->flags |= HAMMER_CURSOR_GET_LEAF | HAMMER_CURSOR_ASOF;
4e17f465
MD
967 cursor->flags |= HAMMER_CURSOR_BACKEND;
968
969 error = hammer_btree_lookup(cursor);
e8599db1
MD
970 if (hammer_debug_inode)
971 kprintf("IPDEL %p %08x %d", ip, ip->flags, error);
b84de5af 972
c0ade690 973 if (error == 0) {
e63644f0 974 error = hammer_ip_delete_record(cursor, ip, trans->tid);
e8599db1
MD
975 if (hammer_debug_inode)
976 kprintf(" error %d\n", error);
1f07f686 977 if (error == 0) {
195c19a1 978 ip->flags |= HAMMER_INODE_DELONDISK;
1f07f686 979 }
e8599db1 980 if (cursor->node)
bcac4bbb 981 hammer_cache_node(&ip->cache[0], cursor->node);
4e17f465
MD
982 }
983 if (error == EDEADLK) {
984 hammer_done_cursor(cursor);
985 error = hammer_init_cursor(trans, cursor,
986 &ip->cache[0], ip);
e8599db1
MD
987 if (hammer_debug_inode)
988 kprintf("IPDED %p %d\n", ip, error);
4e17f465
MD
989 if (error == 0)
990 goto retry;
c0ade690 991 }
c0ade690
MD
992 }
993
994 /*
869e8f55
MD
995 * Ok, write out the initial record or a new record (after deleting
996 * the old one), unless the DELETED flag is set. This routine will
997 * clear DELONDISK if it writes out a record.
76376933 998 *
869e8f55
MD
999 * Update our inode statistics if this is the first application of
1000 * the inode on-disk.
c0ade690 1001 */
869e8f55
MD
1002 if (error == 0 && (ip->flags & HAMMER_INODE_DELETED) == 0) {
1003 /*
7a61b85d
MD
1004 * Generate a record and write it to the media. We clean-up
1005 * the state before releasing so we do not have to set-up
1006 * a flush_group.
869e8f55 1007 */
11ad5ade 1008 record = hammer_alloc_mem_record(ip, 0);
930bf163 1009 record->type = HAMMER_MEM_RECORD_INODE;
1f07f686 1010 record->flush_state = HAMMER_FST_FLUSH;
11ad5ade
MD
1011 record->leaf = ip->sync_ino_leaf;
1012 record->leaf.base.create_tid = trans->tid;
1013 record->leaf.data_len = sizeof(ip->sync_ino_data);
dd94f1b1 1014 record->leaf.create_ts = trans->time32;
b84de5af 1015 record->data = (void *)&ip->sync_ino_data;
d36ec43b 1016 record->flags |= HAMMER_RECF_INTERLOCK_BE;
06ad81ff
MD
1017
1018 /*
1019 * If this flag is set we cannot sync the new file size
1020 * because we haven't finished related truncations. The
1021 * inode will be flushed in another flush group to finish
1022 * the job.
1023 */
1024 if ((ip->flags & HAMMER_INODE_WOULDBLOCK) &&
1025 ip->sync_ino_data.size != ip->ino_data.size) {
1026 redirty = 1;
1027 ip->sync_ino_data.size = ip->ino_data.size;
1028 } else {
1029 redirty = 0;
1030 }
1031
4e17f465
MD
1032 for (;;) {
1033 error = hammer_ip_sync_record_cursor(cursor, record);
e8599db1
MD
1034 if (hammer_debug_inode)
1035 kprintf("GENREC %p rec %08x %d\n",
1036 ip, record->flags, error);
4e17f465
MD
1037 if (error != EDEADLK)
1038 break;
1039 hammer_done_cursor(cursor);
1040 error = hammer_init_cursor(trans, cursor,
1041 &ip->cache[0], ip);
e8599db1
MD
1042 if (hammer_debug_inode)
1043 kprintf("GENREC reinit %d\n", error);
4e17f465
MD
1044 if (error)
1045 break;
1046 }
d36ec43b
MD
1047
1048 /*
1049 * The record isn't managed by the inode's record tree,
1050 * destroy it whether we succeed or fail.
1051 */
1052 record->flags &= ~HAMMER_RECF_INTERLOCK_BE;
1b0ab2c3 1053 record->flags |= HAMMER_RECF_DELETED_FE | HAMMER_RECF_COMMITTED;
1f07f686 1054 record->flush_state = HAMMER_FST_IDLE;
b3deaf57 1055 hammer_rel_mem_record(record);
d36ec43b 1056
869e8f55
MD
1057 /*
1058 * Finish up.
1059 */
d26d0ae9 1060 if (error == 0) {
e8599db1
MD
1061 if (hammer_debug_inode)
1062 kprintf("CLEANDELOND %p %08x\n", ip, ip->flags);
11ad5ade 1063 ip->sync_flags &= ~(HAMMER_INODE_DDIRTY |
ddfdf542
MD
1064 HAMMER_INODE_ATIME |
1065 HAMMER_INODE_MTIME);
b84de5af 1066 ip->flags &= ~HAMMER_INODE_DELONDISK;
06ad81ff
MD
1067 if (redirty)
1068 ip->sync_flags |= HAMMER_INODE_DDIRTY;
1f07f686
MD
1069
1070 /*
1071 * Root volume count of inodes
1072 */
98da6d8c 1073 hammer_sync_lock_sh(trans);
d26d0ae9 1074 if ((ip->flags & HAMMER_INODE_ONDISK) == 0) {
e8599db1
MD
1075 hammer_modify_volume_field(trans,
1076 trans->rootvol,
1077 vol0_stat_inodes);
0b075555 1078 ++ip->hmp->rootvol->ondisk->vol0_stat_inodes;
10a5d1ba 1079 hammer_modify_volume_done(trans->rootvol);
d26d0ae9 1080 ip->flags |= HAMMER_INODE_ONDISK;
e8599db1
MD
1081 if (hammer_debug_inode)
1082 kprintf("NOWONDISK %p\n", ip);
d26d0ae9 1083 }
98da6d8c 1084 hammer_sync_unlock(trans);
fbc6e32a 1085 }
c0ade690 1086 }
869e8f55
MD
1087
1088 /*
1089 * If the inode has been destroyed, clean out any left-over flags
1090 * that may have been set by the frontend.
1091 */
f90dde4c 1092 if (error == 0 && (ip->flags & HAMMER_INODE_DELETED)) {
11ad5ade 1093 ip->sync_flags &= ~(HAMMER_INODE_DDIRTY |
ddfdf542
MD
1094 HAMMER_INODE_ATIME |
1095 HAMMER_INODE_MTIME);
f90dde4c 1096 }
c0ade690
MD
1097 return(error);
1098}
1099
a89aec1b 1100/*
ddfdf542
MD
1101 * Update only the itimes fields.
1102 *
1103 * ATIME can be updated without generating any UNDO. MTIME is updated
1104 * with UNDO so it is guaranteed to be synchronized properly in case of
1105 * a crash.
1106 *
1107 * Neither field is included in the B-Tree leaf element's CRC, which is how
1108 * we can get away with updating ATIME the way we do.
d113fda1
MD
1109 */
1110static int
4e17f465 1111hammer_update_itimes(hammer_cursor_t cursor, hammer_inode_t ip)
d113fda1 1112{
4e17f465 1113 hammer_transaction_t trans = cursor->trans;
d113fda1
MD
1114 int error;
1115
6a37e7e4 1116retry:
ddfdf542 1117 if ((ip->flags & (HAMMER_INODE_ONDISK|HAMMER_INODE_DELONDISK)) !=
d113fda1 1118 HAMMER_INODE_ONDISK) {
ddfdf542
MD
1119 return(0);
1120 }
4e17f465 1121
ddfdf542 1122 hammer_normalize_cursor(cursor);
5a930e66
MD
1123 cursor->key_beg.localization = ip->obj_localization +
1124 HAMMER_LOCALIZE_INODE;
ddfdf542
MD
1125 cursor->key_beg.obj_id = ip->obj_id;
1126 cursor->key_beg.key = 0;
1127 cursor->key_beg.create_tid = 0;
1128 cursor->key_beg.delete_tid = 0;
1129 cursor->key_beg.rec_type = HAMMER_RECTYPE_INODE;
1130 cursor->key_beg.obj_type = 0;
1131 cursor->asof = ip->obj_asof;
1132 cursor->flags &= ~HAMMER_CURSOR_INITMASK;
1133 cursor->flags |= HAMMER_CURSOR_ASOF;
1134 cursor->flags |= HAMMER_CURSOR_GET_LEAF;
1135 cursor->flags |= HAMMER_CURSOR_GET_DATA;
1136 cursor->flags |= HAMMER_CURSOR_BACKEND;
1137
1138 error = hammer_btree_lookup(cursor);
ddfdf542
MD
1139 if (error == 0) {
1140 hammer_cache_node(&ip->cache[0], cursor->node);
1141 if (ip->sync_flags & HAMMER_INODE_MTIME) {
10a5d1ba 1142 /*
ddfdf542
MD
1143 * Updating MTIME requires an UNDO. Just cover
1144 * both atime and mtime.
10a5d1ba 1145 */
98da6d8c 1146 hammer_sync_lock_sh(trans);
bcac4bbb
MD
1147 hammer_modify_buffer(trans, cursor->data_buffer,
1148 HAMMER_ITIMES_BASE(&cursor->data->inode),
1149 HAMMER_ITIMES_BYTES);
1150 cursor->data->inode.atime = ip->sync_ino_data.atime;
1151 cursor->data->inode.mtime = ip->sync_ino_data.mtime;
1152 hammer_modify_buffer_done(cursor->data_buffer);
98da6d8c 1153 hammer_sync_unlock(trans);
ddfdf542
MD
1154 } else if (ip->sync_flags & HAMMER_INODE_ATIME) {
1155 /*
1156 * Updating atime only can be done in-place with
1157 * no UNDO.
1158 */
98da6d8c 1159 hammer_sync_lock_sh(trans);
ddfdf542
MD
1160 hammer_modify_buffer(trans, cursor->data_buffer,
1161 NULL, 0);
1162 cursor->data->inode.atime = ip->sync_ino_data.atime;
1163 hammer_modify_buffer_done(cursor->data_buffer);
98da6d8c 1164 hammer_sync_unlock(trans);
d113fda1 1165 }
ddfdf542
MD
1166 ip->sync_flags &= ~(HAMMER_INODE_ATIME | HAMMER_INODE_MTIME);
1167 }
1168 if (error == EDEADLK) {
1169 hammer_done_cursor(cursor);
1170 error = hammer_init_cursor(trans, cursor,
1171 &ip->cache[0], ip);
1172 if (error == 0)
1173 goto retry;
d113fda1
MD
1174 }
1175 return(error);
1176}
1177
1178/*
1f07f686 1179 * Release a reference on an inode, flush as requested.
b84de5af
MD
1180 *
1181 * On the last reference we queue the inode to the flusher for its final
1182 * disposition.
a89aec1b 1183 */
66325755 1184void
a89aec1b 1185hammer_rel_inode(struct hammer_inode *ip, int flush)
66325755 1186{
35a49944 1187 /*hammer_mount_t hmp = ip->hmp;*/
1f07f686 1188
f90dde4c
MD
1189 /*
1190 * Handle disposition when dropping the last ref.
1191 */
1f07f686
MD
1192 for (;;) {
1193 if (ip->lock.refs == 1) {
1194 /*
1195 * Determine whether on-disk action is needed for
1196 * the inode's final disposition.
1197 */
e8599db1
MD
1198 KKASSERT(ip->vp == NULL);
1199 hammer_inode_unloadable_check(ip, 0);
4e17f465 1200 if (ip->flags & HAMMER_INODE_MODMASK) {
35a49944 1201 hammer_flush_inode(ip, 0);
4e17f465 1202 } else if (ip->lock.refs == 1) {
1f07f686
MD
1203 hammer_unload_inode(ip);
1204 break;
1205 }
b84de5af 1206 } else {
4e17f465 1207 if (flush)
1f07f686 1208 hammer_flush_inode(ip, 0);
4e17f465 1209
1f07f686
MD
1210 /*
1211 * The inode still has multiple refs, try to drop
1212 * one ref.
1213 */
1214 KKASSERT(ip->lock.refs >= 1);
1215 if (ip->lock.refs > 1) {
1216 hammer_unref(&ip->lock);
1217 break;
1218 }
b84de5af 1219 }
f90dde4c 1220 }
427e5fc6
MD
1221}
1222
27ea2398 1223/*
b84de5af
MD
1224 * Unload and destroy the specified inode. Must be called with one remaining
1225 * reference. The reference is disposed of.
8cd0a023 1226 *
cdb6e4e6 1227 * The inode must be completely clean.
27ea2398 1228 */
b84de5af 1229static int
ec4e8497 1230hammer_unload_inode(struct hammer_inode *ip)
27ea2398 1231{
9f5097dc
MD
1232 hammer_mount_t hmp = ip->hmp;
1233
b84de5af 1234 KASSERT(ip->lock.refs == 1,
a89aec1b 1235 ("hammer_unload_inode: %d refs\n", ip->lock.refs));
8cd0a023 1236 KKASSERT(ip->vp == NULL);
f90dde4c
MD
1237 KKASSERT(ip->flush_state == HAMMER_FST_IDLE);
1238 KKASSERT(ip->cursor_ip_refs == 0);
45a014dc 1239 KKASSERT(ip->lock.lockcount == 0);
f90dde4c
MD
1240 KKASSERT((ip->flags & HAMMER_INODE_MODMASK) == 0);
1241
1242 KKASSERT(RB_EMPTY(&ip->rec_tree));
1f07f686 1243 KKASSERT(TAILQ_EMPTY(&ip->target_list));
f90dde4c 1244
9f5097dc 1245 RB_REMOVE(hammer_ino_rb_tree, &hmp->rb_inos_root, ip);
f90dde4c 1246
5fa5c92f 1247 hammer_free_inode(ip);
27ea2398
MD
1248 return(0);
1249}
1250
cdb6e4e6
MD
1251/*
1252 * Called during unmounting if a critical error occured. The in-memory
1253 * inode and all related structures are destroyed.
1254 *
1255 * If a critical error did not occur the unmount code calls the standard
1256 * release and asserts that the inode is gone.
1257 */
1258int
1259hammer_destroy_inode_callback(struct hammer_inode *ip, void *data __unused)
1260{
1261 hammer_record_t rec;
1262
1263 /*
1264 * Get rid of the inodes in-memory records, regardless of their
1265 * state, and clear the mod-mask.
1266 */
1267 while ((rec = TAILQ_FIRST(&ip->target_list)) != NULL) {
1268 TAILQ_REMOVE(&ip->target_list, rec, target_entry);
1269 rec->target_ip = NULL;
1270 if (rec->flush_state == HAMMER_FST_SETUP)
1271 rec->flush_state = HAMMER_FST_IDLE;
1272 }
1273 while ((rec = RB_ROOT(&ip->rec_tree)) != NULL) {
1274 if (rec->flush_state == HAMMER_FST_FLUSH)
1275 --rec->flush_group->refs;
1276 else
1277 hammer_ref(&rec->lock);
1278 KKASSERT(rec->lock.refs == 1);
1279 rec->flush_state = HAMMER_FST_IDLE;
1280 rec->flush_group = NULL;
1281 rec->flags |= HAMMER_RECF_DELETED_FE;
1282 rec->flags |= HAMMER_RECF_DELETED_BE;
1283 hammer_rel_mem_record(rec);
1284 }
1285 ip->flags &= ~HAMMER_INODE_MODMASK;
1286 ip->sync_flags &= ~HAMMER_INODE_MODMASK;
1287 KKASSERT(ip->vp == NULL);
1288
1289 /*
1290 * Remove the inode from any flush group, force it idle. FLUSH
1291 * and SETUP states have an inode ref.
1292 */
1293 switch(ip->flush_state) {
1294 case HAMMER_FST_FLUSH:
1295 TAILQ_REMOVE(&ip->flush_group->flush_list, ip, flush_entry);
1296 --ip->flush_group->refs;
1297 ip->flush_group = NULL;
1298 /* fall through */
1299 case HAMMER_FST_SETUP:
1300 hammer_unref(&ip->lock);
1301 ip->flush_state = HAMMER_FST_IDLE;
1302 /* fall through */
1303 case HAMMER_FST_IDLE:
1304 break;
1305 }
1306
1307 /*
1308 * There shouldn't be any associated vnode. The unload needs at
1309 * least one ref, if we do have a vp steal its ip ref.
1310 */
1311 if (ip->vp) {
1312 kprintf("hammer_destroy_inode_callback: Unexpected "
1313 "vnode association ip %p vp %p\n", ip, ip->vp);
1314 ip->vp->v_data = NULL;
1315 ip->vp = NULL;
1316 } else {
1317 hammer_ref(&ip->lock);
1318 }
1319 hammer_unload_inode(ip);
1320 return(0);
1321}
1322
51c35492
MD
1323/*
1324 * Called on mount -u when switching from RW to RO or vise-versa. Adjust
1325 * the read-only flag for cached inodes.
1326 *
1327 * This routine is called from a RB_SCAN().
1328 */
1329int
1330hammer_reload_inode(hammer_inode_t ip, void *arg __unused)
1331{
1332 hammer_mount_t hmp = ip->hmp;
1333
1334 if (hmp->ronly || hmp->asof != HAMMER_MAX_TID)
1335 ip->flags |= HAMMER_INODE_RO;
1336 else
1337 ip->flags &= ~HAMMER_INODE_RO;
1338 return(0);
1339}
1340
427e5fc6 1341/*
d113fda1
MD
1342 * A transaction has modified an inode, requiring updates as specified by
1343 * the passed flags.
7f7c1f84 1344 *
d113fda1 1345 * HAMMER_INODE_DDIRTY: Inode data has been updated
1f07f686 1346 * HAMMER_INODE_XDIRTY: Dirty in-memory records
4e17f465 1347 * HAMMER_INODE_BUFS: Dirty buffer cache buffers
d113fda1 1348 * HAMMER_INODE_DELETED: Inode record/data must be deleted
ddfdf542 1349 * HAMMER_INODE_ATIME/MTIME: mtime/atime has been updated
427e5fc6 1350 */
66325755 1351void
47637bff 1352hammer_modify_inode(hammer_inode_t ip, int flags)
427e5fc6 1353{
cdb6e4e6
MD
1354 /*
1355 * ronly of 0 or 2 does not trigger assertion.
1356 * 2 is a special error state
1357 */
1358 KKASSERT(ip->hmp->ronly != 1 ||
ddfdf542
MD
1359 (flags & (HAMMER_INODE_DDIRTY | HAMMER_INODE_XDIRTY |
1360 HAMMER_INODE_BUFS | HAMMER_INODE_DELETED |
1361 HAMMER_INODE_ATIME | HAMMER_INODE_MTIME)) == 0);
e63644f0
MD
1362 if ((ip->flags & HAMMER_INODE_RSV_INODES) == 0) {
1363 ip->flags |= HAMMER_INODE_RSV_INODES;
1364 ++ip->hmp->rsv_inodes;
1365 }
b84de5af
MD
1366
1367 ip->flags |= flags;
1368}
1369
1370/*
1f07f686 1371 * Request that an inode be flushed. This whole mess cannot block and may
7bc5b8c2
MD
1372 * recurse (if not synchronous). Once requested HAMMER will attempt to
1373 * actively flush the inode until the flush can be done.
b84de5af 1374 *
1f07f686
MD
1375 * The inode may already be flushing, or may be in a setup state. We can
1376 * place the inode in a flushing state if it is currently idle and flag it
1377 * to reflush if it is currently flushing.
7bc5b8c2 1378 *
4889cbd4
MD
1379 * Upon return if the inode could not be flushed due to a setup
1380 * dependancy, then it will be automatically flushed when the dependancy
1381 * is satisfied.
b84de5af
MD
1382 */
1383void
f90dde4c 1384hammer_flush_inode(hammer_inode_t ip, int flags)
b84de5af 1385{
7a61b85d
MD
1386 hammer_mount_t hmp;
1387 hammer_flush_group_t flg;
bf3b416b 1388 int good;
1f07f686
MD
1389
1390 /*
7b6ccb11
MD
1391 * next_flush_group is the first flush group we can place the inode
1392 * in. It may be NULL. If it becomes full we append a new flush
1393 * group and make that the next_flush_group.
7a61b85d
MD
1394 */
1395 hmp = ip->hmp;
7b6ccb11
MD
1396 while ((flg = hmp->next_flush_group) != NULL) {
1397 KKASSERT(flg->running == 0);
1398 if (flg->total_count + flg->refs <= ip->hmp->undo_rec_limit)
1399 break;
1400 hmp->next_flush_group = TAILQ_NEXT(flg, flush_entry);
1401 hammer_flusher_async(ip->hmp, flg);
7a61b85d
MD
1402 }
1403 if (flg == NULL) {
bac808fe 1404 flg = kmalloc(sizeof(*flg), hmp->m_misc, M_WAITOK|M_ZERO);
7b6ccb11 1405 hmp->next_flush_group = flg;
7a61b85d
MD
1406 TAILQ_INIT(&flg->flush_list);
1407 TAILQ_INSERT_TAIL(&hmp->flush_group_list, flg, flush_entry);
1408 }
1409
1410 /*
1411 * Trivial 'nothing to flush' case. If the inode is in a SETUP
1f07f686
MD
1412 * state we have to put it back into an IDLE state so we can
1413 * drop the extra ref.
7a61b85d
MD
1414 *
1415 * If we have a parent dependancy we must still fall through
1416 * so we can run it.
1f07f686 1417 */
4e17f465 1418 if ((ip->flags & HAMMER_INODE_MODMASK) == 0) {
7a61b85d
MD
1419 if (ip->flush_state == HAMMER_FST_SETUP &&
1420 TAILQ_EMPTY(&ip->target_list)) {
1f07f686
MD
1421 ip->flush_state = HAMMER_FST_IDLE;
1422 hammer_rel_inode(ip, 0);
ec4e8497 1423 }
7a61b85d
MD
1424 if (ip->flush_state == HAMMER_FST_IDLE)
1425 return;
b84de5af 1426 }
42c7d26b 1427
1f07f686
MD
1428 /*
1429 * Our flush action will depend on the current state.
1430 */
1431 switch(ip->flush_state) {
1432 case HAMMER_FST_IDLE:
1433 /*
1434 * We have no dependancies and can flush immediately. Some
1435 * our children may not be flushable so we have to re-test
1436 * with that additional knowledge.
1437 */
7a61b85d 1438 hammer_flush_inode_core(ip, flg, flags);
1f07f686
MD
1439 break;
1440 case HAMMER_FST_SETUP:
1441 /*
1442 * Recurse upwards through dependancies via target_list
1443 * and start their flusher actions going if possible.
1444 *
1445 * 'good' is our connectivity. -1 means we have none and
1446 * can't flush, 0 means there weren't any dependancies, and
1447 * 1 means we have good connectivity.
1448 */
7a61b85d 1449 good = hammer_setup_parent_inodes(ip, flg);
1f07f686 1450
1f07f686 1451 if (good >= 0) {
7b6ccb11
MD
1452 /*
1453 * We can continue if good >= 0. Determine how
1454 * many records under our inode can be flushed (and
1455 * mark them).
1456 */
7a61b85d 1457 hammer_flush_inode_core(ip, flg, flags);
1f07f686 1458 } else {
7b6ccb11 1459 /*
4889cbd4 1460 * Parent has no connectivity, tell it to flush
7b6ccb11 1461 * us as soon as it does.
4889cbd4
MD
1462 *
1463 * The REFLUSH flag is also needed to trigger
1464 * dependancy wakeups.
7b6ccb11 1465 */
4889cbd4
MD
1466 ip->flags |= HAMMER_INODE_CONN_DOWN |
1467 HAMMER_INODE_REFLUSH;
4e17f465
MD
1468 if (flags & HAMMER_FLUSH_SIGNAL) {
1469 ip->flags |= HAMMER_INODE_RESIGNAL;
7a61b85d 1470 hammer_flusher_async(ip->hmp, flg);
4e17f465 1471 }
1f07f686
MD
1472 }
1473 break;
7b6ccb11 1474 case HAMMER_FST_FLUSH:
1f07f686
MD
1475 /*
1476 * We are already flushing, flag the inode to reflush
1477 * if needed after it completes its current flush.
4889cbd4
MD
1478 *
1479 * The REFLUSH flag is also needed to trigger
1480 * dependancy wakeups.
1f07f686
MD
1481 */
1482 if ((ip->flags & HAMMER_INODE_REFLUSH) == 0)
1483 ip->flags |= HAMMER_INODE_REFLUSH;
4e17f465
MD
1484 if (flags & HAMMER_FLUSH_SIGNAL) {
1485 ip->flags |= HAMMER_INODE_RESIGNAL;
7a61b85d 1486 hammer_flusher_async(ip->hmp, flg);
4e17f465 1487 }
1f07f686
MD
1488 break;
1489 }
1490}
1491
1492/*
bf3b416b
MD
1493 * Scan ip->target_list, which is a list of records owned by PARENTS to our
1494 * ip which reference our ip.
1495 *
1496 * XXX This is a huge mess of recursive code, but not one bit of it blocks
1497 * so for now do not ref/deref the structures. Note that if we use the
1498 * ref/rel code later, the rel CAN block.
1499 */
1500static int
7a61b85d 1501hammer_setup_parent_inodes(hammer_inode_t ip, hammer_flush_group_t flg)
bf3b416b
MD
1502{
1503 hammer_record_t depend;
bf3b416b
MD
1504 int good;
1505 int r;
1506
1507 good = 0;
1508 TAILQ_FOREACH(depend, &ip->target_list, target_entry) {
7a61b85d 1509 r = hammer_setup_parent_inodes_helper(depend, flg);
bf3b416b 1510 KKASSERT(depend->target_ip == ip);
bf3b416b
MD
1511 if (r < 0 && good == 0)
1512 good = -1;
1513 if (r > 0)
1514 good = 1;
bf3b416b
MD
1515 }
1516 return(good);
bf3b416b
MD
1517}
1518
1519/*
1520 * This helper function takes a record representing the dependancy between
1521 * the parent inode and child inode.
1522 *
1523 * record->ip = parent inode
1524 * record->target_ip = child inode
1525 *
1f07f686 1526 * We are asked to recurse upwards and convert the record from SETUP
bf3b416b 1527 * to FLUSH if possible.
1f07f686
MD
1528 *
1529 * Return 1 if the record gives us connectivity
1530 *
1531 * Return 0 if the record is not relevant
1532 *
1533 * Return -1 if we can't resolve the dependancy and there is no connectivity.
1534 */
1535static int
7a61b85d
MD
1536hammer_setup_parent_inodes_helper(hammer_record_t record,
1537 hammer_flush_group_t flg)
1f07f686 1538{
bf3b416b
MD
1539 hammer_mount_t hmp;
1540 hammer_inode_t pip;
1541 int good;
1f07f686
MD
1542
1543 KKASSERT(record->flush_state != HAMMER_FST_IDLE);
bf3b416b
MD
1544 pip = record->ip;
1545 hmp = pip->hmp;
1f07f686
MD
1546
1547 /*
1548 * If the record is already flushing, is it in our flush group?
1549 *
e8599db1
MD
1550 * If it is in our flush group but it is a general record or a
1551 * delete-on-disk, it does not improve our connectivity (return 0),
1552 * and if the target inode is not trying to destroy itself we can't
1553 * allow the operation yet anyway (the second return -1).
1f07f686
MD
1554 */
1555 if (record->flush_state == HAMMER_FST_FLUSH) {
7b6ccb11
MD
1556 /*
1557 * If not in our flush group ask the parent to reflush
1558 * us as soon as possible.
1559 */
7a61b85d 1560 if (record->flush_group != flg) {
bf3b416b 1561 pip->flags |= HAMMER_INODE_REFLUSH;
7b6ccb11 1562 record->target_ip->flags |= HAMMER_INODE_CONN_DOWN;
1f07f686 1563 return(-1);
f90dde4c 1564 }
7b6ccb11
MD
1565
1566 /*
1567 * If in our flush group everything is already set up,
1568 * just return whether the record will improve our
1569 * visibility or not.
1570 */
1f07f686
MD
1571 if (record->type == HAMMER_MEM_RECORD_ADD)
1572 return(1);
1573 return(0);
1574 }
1575
1576 /*
1577 * It must be a setup record. Try to resolve the setup dependancies
1578 * by recursing upwards so we can place ip on the flush list.
1579 */
1580 KKASSERT(record->flush_state == HAMMER_FST_SETUP);
1581
7a61b85d 1582 good = hammer_setup_parent_inodes(pip, flg);
1f07f686
MD
1583
1584 /*
7b6ccb11
MD
1585 * If good < 0 the parent has no connectivity and we cannot safely
1586 * flush the directory entry, which also means we can't flush our
1587 * ip. Flag the parent and us for downward recursion once the
1588 * parent's connectivity is resolved.
1f07f686
MD
1589 */
1590 if (good < 0) {
7b6ccb11
MD
1591 /* pip->flags |= HAMMER_INODE_CONN_DOWN; set by recursion */
1592 record->target_ip->flags |= HAMMER_INODE_CONN_DOWN;
1f07f686
MD
1593 return(good);
1594 }
1595
1596 /*
1597 * We are go, place the parent inode in a flushing state so we can
1598 * place its record in a flushing state. Note that the parent
1599 * may already be flushing. The record must be in the same flush
1600 * group as the parent.
1601 */
bf3b416b 1602 if (pip->flush_state != HAMMER_FST_FLUSH)
7a61b85d 1603 hammer_flush_inode_core(pip, flg, HAMMER_FLUSH_RECURSION);
bf3b416b 1604 KKASSERT(pip->flush_state == HAMMER_FST_FLUSH);
1f07f686
MD
1605 KKASSERT(record->flush_state == HAMMER_FST_SETUP);
1606
1607#if 0
1608 if (record->type == HAMMER_MEM_RECORD_DEL &&
869e8f55 1609 (record->target_ip->flags & (HAMMER_INODE_DELETED|HAMMER_INODE_DELONDISK)) == 0) {
1f07f686
MD
1610 /*
1611 * Regardless of flushing state we cannot sync this path if the
1612 * record represents a delete-on-disk but the target inode
1613 * is not ready to sync its own deletion.
1614 *
1615 * XXX need to count effective nlinks to determine whether
1616 * the flush is ok, otherwise removing a hardlink will
1617 * just leave the DEL record to rot.
1618 */
1619 record->target_ip->flags |= HAMMER_INODE_REFLUSH;
1620 return(-1);
1621 } else
1622#endif
7a61b85d 1623 if (pip->flush_group == flg) {
1f07f686 1624 /*
d7e278bb
MD
1625 * Because we have not calculated nlinks yet we can just
1626 * set records to the flush state if the parent is in
1627 * the same flush group as we are.
1f07f686 1628 */
7b6ccb11
MD
1629 record->flush_state = HAMMER_FST_FLUSH;
1630 record->flush_group = flg;
1631 ++record->flush_group->refs;
1632 hammer_ref(&record->lock);
1f07f686
MD
1633
1634 /*
7b6ccb11
MD
1635 * A general directory-add contributes to our visibility.
1636 *
1637 * Otherwise it is probably a directory-delete or
1638 * delete-on-disk record and does not contribute to our
1639 * visbility (but we can still flush it).
1f07f686 1640 */
7b6ccb11
MD
1641 if (record->type == HAMMER_MEM_RECORD_ADD)
1642 return(1);
1f07f686
MD
1643 return(0);
1644 } else {
1645 /*
7b6ccb11
MD
1646 * If the parent is not in our flush group we cannot
1647 * flush this record yet, there is no visibility.
1648 * We tell the parent to reflush and mark ourselves
1649 * so the parent knows it should flush us too.
1f07f686 1650 */
bf3b416b 1651 pip->flags |= HAMMER_INODE_REFLUSH;
7b6ccb11 1652 record->target_ip->flags |= HAMMER_INODE_CONN_DOWN;
1f07f686 1653 return(-1);
7f7c1f84 1654 }
c0ade690
MD
1655}
1656
1657/*
1f07f686 1658 * This is the core routine placing an inode into the FST_FLUSH state.
c0ade690 1659 */
b84de5af 1660static void
7a61b85d 1661hammer_flush_inode_core(hammer_inode_t ip, hammer_flush_group_t flg, int flags)
b84de5af 1662{
1f07f686 1663 int go_count;
1f07f686 1664
4e17f465
MD
1665 /*
1666 * Set flush state and prevent the flusher from cycling into
1667 * the next flush group. Do not place the ip on the list yet.
1668 * Inodes not in the idle state get an extra reference.
1669 */
1f07f686
MD
1670 KKASSERT(ip->flush_state != HAMMER_FST_FLUSH);
1671 if (ip->flush_state == HAMMER_FST_IDLE)
1672 hammer_ref(&ip->lock);
1673 ip->flush_state = HAMMER_FST_FLUSH;
7a61b85d 1674 ip->flush_group = flg;
da2da375 1675 ++ip->hmp->flusher.group_lock;
af209b0f
MD
1676 ++ip->hmp->count_iqueued;
1677 ++hammer_count_iqueued;
7a61b85d 1678 ++flg->total_count;
b84de5af 1679
21fde338
MD
1680 /*
1681 * If the flush group reaches the autoflush limit we want to signal
1682 * the flusher. This is particularly important for remove()s.
1683 */
1684 if (flg->total_count == hammer_autoflush)
1685 flags |= HAMMER_FLUSH_SIGNAL;
1686
e8599db1
MD
1687 /*
1688 * We need to be able to vfsync/truncate from the backend.
1689 */
1690 KKASSERT((ip->flags & HAMMER_INODE_VHELD) == 0);
1691 if (ip->vp && (ip->vp->v_flag & VINACTIVE) == 0) {
1692 ip->flags |= HAMMER_INODE_VHELD;
1693 vref(ip->vp);
1694 }
1695
ec4e8497 1696 /*
1f07f686
MD
1697 * Figure out how many in-memory records we can actually flush
1698 * (not including inode meta-data, buffers, etc).
ec4e8497 1699 */
d7e278bb 1700 KKASSERT((ip->flags & HAMMER_INODE_WOULDBLOCK) == 0);
1f07f686 1701 if (flags & HAMMER_FLUSH_RECURSION) {
7a61b85d
MD
1702 /*
1703 * If this is a upwards recursion we do not want to
1704 * recurse down again!
1705 */
1f07f686 1706 go_count = 1;
d7e278bb 1707#if 0
312de84d 1708 } else if (ip->flags & HAMMER_INODE_WOULDBLOCK) {
7a61b85d
MD
1709 /*
1710 * No new records are added if we must complete a flush
1711 * from a previous cycle, but we do have to move the records
1712 * from the previous cycle to the current one.
1713 */
1714#if 0
525aad3a
MD
1715 go_count = RB_SCAN(hammer_rec_rb_tree, &ip->rec_tree, NULL,
1716 hammer_syncgrp_child_callback, NULL);
7a61b85d 1717#endif
312de84d 1718 go_count = 1;
d7e278bb 1719#endif
1f07f686 1720 } else {
7a61b85d
MD
1721 /*
1722 * Normal flush, scan records and bring them into the flush.
1723 * Directory adds and deletes are usually skipped (they are
1724 * grouped with the related inode rather then with the
1725 * directory).
1726 *
1727 * go_count can be negative, which means the scan aborted
1728 * due to the flush group being over-full and we should
1729 * flush what we have.
1730 */
1f07f686
MD
1731 go_count = RB_SCAN(hammer_rec_rb_tree, &ip->rec_tree, NULL,
1732 hammer_setup_child_callback, NULL);
1733 }
b84de5af
MD
1734
1735 /*
1f07f686
MD
1736 * This is a more involved test that includes go_count. If we
1737 * can't flush, flag the inode and return. If go_count is 0 we
1738 * were are unable to flush any records in our rec_tree and
1739 * must ignore the XDIRTY flag.
b84de5af 1740 */
1f07f686
MD
1741 if (go_count == 0) {
1742 if ((ip->flags & HAMMER_INODE_MODMASK_NOXDIRTY) == 0) {
af209b0f
MD
1743 --ip->hmp->count_iqueued;
1744 --hammer_count_iqueued;
1745
4889cbd4 1746 --flg->total_count;
1f07f686 1747 ip->flush_state = HAMMER_FST_SETUP;
7a61b85d 1748 ip->flush_group = NULL;
e8599db1
MD
1749 if (ip->flags & HAMMER_INODE_VHELD) {
1750 ip->flags &= ~HAMMER_INODE_VHELD;
1751 vrele(ip->vp);
1752 }
4889cbd4
MD
1753
1754 /*
1755 * REFLUSH is needed to trigger dependancy wakeups
1756 * when an inode is in SETUP.
1757 */
1758 ip->flags |= HAMMER_INODE_REFLUSH;
4e17f465
MD
1759 if (flags & HAMMER_FLUSH_SIGNAL) {
1760 ip->flags |= HAMMER_INODE_RESIGNAL;
7a61b85d 1761 hammer_flusher_async(ip->hmp, flg);
4e17f465 1762 }
da2da375
MD
1763 if (--ip->hmp->flusher.group_lock == 0)
1764 wakeup(&ip->hmp->flusher.group_lock);
1f07f686
MD
1765 return;
1766 }
1767 }
b84de5af 1768
b84de5af
MD
1769 /*
1770 * Snapshot the state of the inode for the backend flusher.
1771 *
a9d52b76 1772 * We continue to retain save_trunc_off even when all truncations
cb51be26
MD
1773 * have been resolved as an optimization to determine if we can
1774 * skip the B-Tree lookup for overwrite deletions.
1775 *
1f07f686
MD
1776 * NOTE: The DELETING flag is a mod flag, but it is also sticky,
1777 * and stays in ip->flags. Once set, it stays set until the
1778 * inode is destroyed.
b84de5af 1779 */
d7e278bb 1780 if (ip->flags & HAMMER_INODE_TRUNCATED) {
312de84d
MD
1781 KKASSERT((ip->sync_flags & HAMMER_INODE_TRUNCATED) == 0);
1782 ip->sync_trunc_off = ip->trunc_off;
1783 ip->trunc_off = 0x7FFFFFFFFFFFFFFFLL;
1784 ip->flags &= ~HAMMER_INODE_TRUNCATED;
1785 ip->sync_flags |= HAMMER_INODE_TRUNCATED;
a9d52b76
MD
1786
1787 /*
1788 * The save_trunc_off used to cache whether the B-Tree
1789 * holds any records past that point is not used until
1790 * after the truncation has succeeded, so we can safely
1791 * set it now.
1792 */
1793 if (ip->save_trunc_off > ip->sync_trunc_off)
1794 ip->save_trunc_off = ip->sync_trunc_off;
1795 }
312de84d
MD
1796 ip->sync_flags |= (ip->flags & HAMMER_INODE_MODMASK &
1797 ~HAMMER_INODE_TRUNCATED);
11ad5ade 1798 ip->sync_ino_leaf = ip->ino_leaf;
b84de5af 1799 ip->sync_ino_data = ip->ino_data;
312de84d 1800 ip->flags &= ~HAMMER_INODE_MODMASK | HAMMER_INODE_TRUNCATED;
0832c9bb
MD
1801#ifdef DEBUG_TRUNCATE
1802 if ((ip->sync_flags & HAMMER_INODE_TRUNCATED) && ip == HammerTruncIp)
1803 kprintf("truncateS %016llx\n", ip->sync_trunc_off);
1804#endif
b84de5af
MD
1805
1806 /*
4e17f465 1807 * The flusher list inherits our inode and reference.
b84de5af 1808 */
7a61b85d
MD
1809 KKASSERT(flg->running == 0);
1810 TAILQ_INSERT_TAIL(&flg->flush_list, ip, flush_entry);
da2da375
MD
1811 if (--ip->hmp->flusher.group_lock == 0)
1812 wakeup(&ip->hmp->flusher.group_lock);
1f07f686 1813
0832c9bb 1814 if (flags & HAMMER_FLUSH_SIGNAL) {
7a61b85d 1815 hammer_flusher_async(ip->hmp, flg);
0832c9bb 1816 }
b84de5af
MD
1817}
1818
ec4e8497 1819/*
1f07f686
MD
1820 * Callback for scan of ip->rec_tree. Try to include each record in our
1821 * flush. ip->flush_group has been set but the inode has not yet been
1822 * moved into a flushing state.
1823 *
1824 * If we get stuck on a record we have to set HAMMER_INODE_REFLUSH on
1825 * both inodes.
1826 *
1827 * We return 1 for any record placed or found in FST_FLUSH, which prevents
1828 * the caller from shortcutting the flush.
ec4e8497 1829 */
c0ade690 1830static int
1f07f686 1831hammer_setup_child_callback(hammer_record_t rec, void *data)
b84de5af 1832{
7a61b85d 1833 hammer_flush_group_t flg;
1f07f686
MD
1834 hammer_inode_t target_ip;
1835 hammer_inode_t ip;
1836 int r;
1837
1838 /*
7bc5b8c2
MD
1839 * Deleted records are ignored. Note that the flush detects deleted
1840 * front-end records at multiple points to deal with races. This is
1841 * just the first line of defense. The only time DELETED_FE cannot
1842 * be set is when HAMMER_RECF_INTERLOCK_BE is set.
1843 *
1844 * Don't get confused between record deletion and, say, directory
1845 * entry deletion. The deletion of a directory entry that is on
1846 * the media has nothing to do with the record deletion flags.
1f07f686 1847 */
ecca949a
MD
1848 if (rec->flags & (HAMMER_RECF_DELETED_FE|HAMMER_RECF_DELETED_BE)) {
1849 if (rec->flush_state == HAMMER_FST_FLUSH) {
7a61b85d 1850 KKASSERT(rec->flush_group == rec->ip->flush_group);
ecca949a
MD
1851 r = 1;
1852 } else {
1853 r = 0;
1854 }
1855 return(r);
1856 }
1f07f686
MD
1857
1858 /*
1859 * If the record is in an idle state it has no dependancies and
1860 * can be flushed.
1861 */
1862 ip = rec->ip;
7a61b85d 1863 flg = ip->flush_group;
1f07f686
MD
1864 r = 0;
1865
1866 switch(rec->flush_state) {
1867 case HAMMER_FST_IDLE:
1868 /*
7a61b85d 1869 * The record has no setup dependancy, we can flush it.
1f07f686
MD
1870 */
1871 KKASSERT(rec->target_ip == NULL);
1872 rec->flush_state = HAMMER_FST_FLUSH;
7a61b85d
MD
1873 rec->flush_group = flg;
1874 ++flg->refs;
b84de5af 1875 hammer_ref(&rec->lock);
1f07f686
MD
1876 r = 1;
1877 break;
1878 case HAMMER_FST_SETUP:
1879 /*
7a61b85d
MD
1880 * The record has a setup dependancy. These are typically
1881 * directory entry adds and deletes. Such entries will be
7b6ccb11
MD
1882 * flushed when their inodes are flushed so we do not
1883 * usually have to add them to the flush here. However,
1884 * if the target_ip has set HAMMER_INODE_CONN_DOWN then
1885 * it is asking us to flush this record (and it).
1f07f686
MD
1886 */
1887 target_ip = rec->target_ip;
1888 KKASSERT(target_ip != NULL);
1889 KKASSERT(target_ip->flush_state != HAMMER_FST_IDLE);
7a61b85d
MD
1890
1891 /*
1892 * If the target IP is already flushing in our group
5c8d05e2
MD
1893 * we could associate the record, but target_ip has
1894 * already synced ino_data to sync_ino_data and we
1895 * would also have to adjust nlinks. Plus there are
1896 * ordering issues for adds and deletes.
1897 *
1898 * Reflush downward if this is an ADD, and upward if
1899 * this is a DEL.
7a61b85d 1900 */
1f07f686 1901 if (target_ip->flush_state == HAMMER_FST_FLUSH) {
5c8d05e2
MD
1902 if (rec->flush_state == HAMMER_MEM_RECORD_ADD)
1903 ip->flags |= HAMMER_INODE_REFLUSH;
1904 else
1f07f686 1905 target_ip->flags |= HAMMER_INODE_REFLUSH;
7a61b85d
MD
1906 break;
1907 }
1908
1909 /*
1910 * Target IP is not yet flushing. This can get complex
1911 * because we have to be careful about the recursion.
7b6ccb11
MD
1912 *
1913 * Directories create an issue for us in that if a flush
1914 * of a directory is requested the expectation is to flush
1915 * any pending directory entries, but this will cause the
1916 * related inodes to recursively flush as well. We can't
1917 * really defer the operation so just get as many as we
1918 * can and
7a61b85d 1919 */
7b6ccb11 1920#if 0
7a61b85d 1921 if ((target_ip->flags & HAMMER_INODE_RECLAIM) == 0 &&
7b6ccb11 1922 (target_ip->flags & HAMMER_INODE_CONN_DOWN) == 0) {
7a61b85d 1923 /*
7b6ccb11
MD
1924 * We aren't reclaiming and the target ip was not
1925 * previously prevented from flushing due to this
1926 * record dependancy. Do not flush this record.
7a61b85d
MD
1927 */
1928 /*r = 0;*/
7b6ccb11
MD
1929 } else
1930#endif
1931 if (flg->total_count + flg->refs >
7a61b85d
MD
1932 ip->hmp->undo_rec_limit) {
1933 /*
1934 * Our flush group is over-full and we risk blowing
1935 * out the UNDO FIFO. Stop the scan, flush what we
1936 * have, then reflush the directory.
1937 *
1938 * The directory may be forced through multiple
1939 * flush groups before it can be completely
1940 * flushed.
1941 */
4889cbd4
MD
1942 ip->flags |= HAMMER_INODE_RESIGNAL |
1943 HAMMER_INODE_REFLUSH;
7a61b85d 1944 r = -1;
1f07f686
MD
1945 } else if (rec->type == HAMMER_MEM_RECORD_ADD) {
1946 /*
1947 * If the target IP is not flushing we can force
1948 * it to flush, even if it is unable to write out
1949 * any of its own records we have at least one in
1950 * hand that we CAN deal with.
1951 */
1952 rec->flush_state = HAMMER_FST_FLUSH;
7a61b85d
MD
1953 rec->flush_group = flg;
1954 ++flg->refs;
1f07f686 1955 hammer_ref(&rec->lock);
7a61b85d 1956 hammer_flush_inode_core(target_ip, flg,
1f07f686
MD
1957 HAMMER_FLUSH_RECURSION);
1958 r = 1;
1959 } else {
1960 /*
e8599db1
MD
1961 * General or delete-on-disk record.
1962 *
1963 * XXX this needs help. If a delete-on-disk we could
1964 * disconnect the target. If the target has its own
1965 * dependancies they really need to be flushed.
1f07f686
MD
1966 *
1967 * XXX
1968 */
1969 rec->flush_state = HAMMER_FST_FLUSH;
7a61b85d
MD
1970 rec->flush_group = flg;
1971 ++flg->refs;
1f07f686 1972 hammer_ref(&rec->lock);
7a61b85d 1973 hammer_flush_inode_core(target_ip, flg,
1f07f686
MD
1974 HAMMER_FLUSH_RECURSION);
1975 r = 1;
1976 }
1977 break;
1978 case HAMMER_FST_FLUSH:
1979 /*
d7e278bb 1980 * The flush_group should already match.
1f07f686 1981 */
7a61b85d 1982 KKASSERT(rec->flush_group == flg);
1f07f686
MD
1983 r = 1;
1984 break;
b84de5af 1985 }
1f07f686 1986 return(r);
b84de5af
MD
1987}
1988
7a61b85d 1989#if 0
525aad3a
MD
1990/*
1991 * This version just moves records already in a flush state to the new
1992 * flush group and that is it.
1993 */
1994static int
1995hammer_syncgrp_child_callback(hammer_record_t rec, void *data)
1996{
1997 hammer_inode_t ip = rec->ip;
1998
1999 switch(rec->flush_state) {
2000 case HAMMER_FST_FLUSH:
7a61b85d 2001 KKASSERT(rec->flush_group == ip->flush_group);
525aad3a
MD
2002 break;
2003 default:
2004 break;
2005 }
2006 return(0);
2007}
7a61b85d 2008#endif
525aad3a 2009
b84de5af 2010/*
7a61b85d 2011 * Wait for a previously queued flush to complete.
cdb6e4e6
MD
2012 *
2013 * If a critical error occured we don't try to wait.
b84de5af
MD
2014 */
2015void
2016hammer_wait_inode(hammer_inode_t ip)
2017{
7a61b85d 2018 hammer_flush_group_t flg;
ddfdf542 2019
7a61b85d 2020 flg = NULL;
cdb6e4e6 2021 if ((ip->hmp->flags & HAMMER_MOUNT_CRITICAL_ERROR) == 0) {
cdb6e4e6
MD
2022 while (ip->flush_state != HAMMER_FST_IDLE &&
2023 (ip->hmp->flags & HAMMER_MOUNT_CRITICAL_ERROR) == 0) {
f153644d
MD
2024 if (ip->flush_state == HAMMER_FST_SETUP)
2025 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
2026 if (ip->flush_state != HAMMER_FST_IDLE) {
2027 ip->flags |= HAMMER_INODE_FLUSHW;
2028 tsleep(&ip->flags, 0, "hmrwin", 0);
2029 }
cdb6e4e6 2030 }
b84de5af
MD
2031 }
2032}
2033
2034/*
2035 * Called by the backend code when a flush has been completed.
2036 * The inode has already been removed from the flush list.
2037 *
2038 * A pipelined flush can occur, in which case we must re-enter the
2039 * inode on the list and re-copy its fields.
2040 */
2041void
cdb6e4e6 2042hammer_flush_inode_done(hammer_inode_t ip, int error)
b84de5af 2043{
af209b0f
MD
2044 hammer_mount_t hmp;
2045 int dorel;
1955afa7 2046
b84de5af
MD
2047 KKASSERT(ip->flush_state == HAMMER_FST_FLUSH);
2048
af209b0f
MD
2049 hmp = ip->hmp;
2050
5c667a24
MD
2051 /*
2052 * Auto-reflush if the backend could not completely flush
2053 * the inode. This fixes a case where a deferred buffer flush
2054 * could cause fsync to return early.
2055 */
2056 if (ip->sync_flags & HAMMER_INODE_MODMASK)
2057 ip->flags |= HAMMER_INODE_REFLUSH;
2058
1f07f686
MD
2059 /*
2060 * Merge left-over flags back into the frontend and fix the state.
a9d52b76 2061 * Incomplete truncations are retained by the backend.
1f07f686 2062 */
cdb6e4e6 2063 ip->error = error;
a9d52b76
MD
2064 ip->flags |= ip->sync_flags & ~HAMMER_INODE_TRUNCATED;
2065 ip->sync_flags &= HAMMER_INODE_TRUNCATED;
1f07f686
MD
2066
2067 /*
2068 * The backend may have adjusted nlinks, so if the adjusted nlinks
2069 * does not match the fronttend set the frontend's RDIRTY flag again.
2070 */
11ad5ade
MD
2071 if (ip->ino_data.nlinks != ip->sync_ino_data.nlinks)
2072 ip->flags |= HAMMER_INODE_DDIRTY;
b84de5af 2073
4e17f465 2074 /*
a7e9bef1 2075 * Fix up the dirty buffer status.
4e17f465 2076 */
0832c9bb 2077 if (ip->vp && RB_ROOT(&ip->vp->v_rbdirty_tree)) {
1f07f686 2078 ip->flags |= HAMMER_INODE_BUFS;
1f07f686
MD
2079 }
2080
2081 /*
2082 * Re-set the XDIRTY flag if some of the inode's in-memory records
2083 * could not be flushed.
2084 */
0832c9bb
MD
2085 KKASSERT((RB_EMPTY(&ip->rec_tree) &&
2086 (ip->flags & HAMMER_INODE_XDIRTY) == 0) ||
2087 (!RB_EMPTY(&ip->rec_tree) &&
2088 (ip->flags & HAMMER_INODE_XDIRTY) != 0));
4e17f465
MD
2089
2090 /*
2091 * Do not lose track of inodes which no longer have vnode
2092 * assocations, otherwise they may never get flushed again.
35a49944
MD
2093 *
2094 * The reflush flag can be set superfluously, causing extra pain
2095 * for no reason. If the inode is no longer modified it no longer
2096 * needs to be flushed.
4e17f465 2097 */
35a49944
MD
2098 if (ip->flags & HAMMER_INODE_MODMASK) {
2099 if (ip->vp == NULL)
2100 ip->flags |= HAMMER_INODE_REFLUSH;
2101 } else {
2102 ip->flags &= ~HAMMER_INODE_REFLUSH;
2103 }
4e17f465 2104
a9d52b76 2105 /*
7a61b85d 2106 * Adjust the flush state.
4e17f465 2107 */
06ad81ff 2108 if (ip->flags & HAMMER_INODE_WOULDBLOCK) {
7a61b85d
MD
2109 /*
2110 * We were unable to flush out all our records, leave the
2111 * inode in a flush state and in the current flush group.
d7e278bb 2112 * The flush group will be re-run.
7a61b85d 2113 *
d7e278bb
MD
2114 * This occurs if the UNDO block gets too full or there is
2115 * too much dirty meta-data and allows the flusher to
2116 * finalize the UNDO block and then re-flush.
7a61b85d 2117 */
06ad81ff 2118 ip->flags &= ~HAMMER_INODE_WOULDBLOCK;
af209b0f 2119 dorel = 0;
7a61b85d
MD
2120 } else {
2121 /*
2122 * Remove from the flush_group
2123 */
2124 TAILQ_REMOVE(&ip->flush_group->flush_list, ip, flush_entry);
2125 ip->flush_group = NULL;
2126
2127 /*
2128 * Clean up the vnode ref and tracking counts.
2129 */
2130 if (ip->flags & HAMMER_INODE_VHELD) {
2131 ip->flags &= ~HAMMER_INODE_VHELD;
2132 vrele(ip->vp);
2133 }
2134 --hmp->count_iqueued;
2135 --hammer_count_iqueued;
2136
2137 /*
2138 * And adjust the state.
2139 */
2140 if (TAILQ_EMPTY(&ip->target_list) && RB_EMPTY(&ip->rec_tree)) {
2141 ip->flush_state = HAMMER_FST_IDLE;
2142 dorel = 1;
2143 } else {
2144 ip->flush_state = HAMMER_FST_SETUP;
2145 dorel = 0;
2146 }
b84de5af 2147
7a61b85d
MD
2148 /*
2149 * If the frontend is waiting for a flush to complete,
2150 * wake it up.
2151 */
2152 if (ip->flags & HAMMER_INODE_FLUSHW) {
2153 ip->flags &= ~HAMMER_INODE_FLUSHW;
2154 wakeup(&ip->flags);
2155 }
af209b0f 2156
d7e278bb
MD
2157 /*
2158 * If the frontend made more changes and requested another
2159 * flush, then try to get it running.
2160 *
2161 * Reflushes are aborted when the inode is errored out.
2162 */
2163 if (ip->flags & HAMMER_INODE_REFLUSH) {
2164 ip->flags &= ~HAMMER_INODE_REFLUSH;
2165 if (ip->flags & HAMMER_INODE_RESIGNAL) {
2166 ip->flags &= ~HAMMER_INODE_RESIGNAL;
2167 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
2168 } else {
2169 hammer_flush_inode(ip, 0);
2170 }
0729c8c8 2171 }
4e17f465
MD
2172 }
2173
7b6ccb11
MD
2174 /*
2175 * If we have no parent dependancies we can clear CONN_DOWN
2176 */
2177 if (TAILQ_EMPTY(&ip->target_list))
2178 ip->flags &= ~HAMMER_INODE_CONN_DOWN;
2179
e63644f0
MD
2180 /*
2181 * If the inode is now clean drop the space reservation.
2182 */
2183 if ((ip->flags & HAMMER_INODE_MODMASK) == 0 &&
2184 (ip->flags & HAMMER_INODE_RSV_INODES)) {
2185 ip->flags &= ~HAMMER_INODE_RSV_INODES;
af209b0f 2186 --hmp->rsv_inodes;
e63644f0
MD
2187 }
2188
1f07f686
MD
2189 if (dorel)
2190 hammer_rel_inode(ip, 0);
b84de5af
MD
2191}
2192
2193/*
2194 * Called from hammer_sync_inode() to synchronize in-memory records
2195 * to the media.
2196 */
2197static int
2198hammer_sync_record_callback(hammer_record_t record, void *data)
c0ade690 2199{
4e17f465
MD
2200 hammer_cursor_t cursor = data;
2201 hammer_transaction_t trans = cursor->trans;
6c1f89f4 2202 hammer_mount_t hmp = trans->hmp;
c0ade690
MD
2203 int error;
2204
b84de5af 2205 /*
1f07f686 2206 * Skip records that do not belong to the current flush.
b84de5af 2207 */
47637bff 2208 ++hammer_stats_record_iterations;
1f07f686 2209 if (record->flush_state != HAMMER_FST_FLUSH)
b84de5af 2210 return(0);
47637bff 2211
1f07f686
MD
2212#if 1
2213 if (record->flush_group != record->ip->flush_group) {
7a61b85d 2214 kprintf("sync_record %p ip %p bad flush group %p %p\n", record, record->ip, record->flush_group ,record->ip->flush_group);
1f07f686
MD
2215 Debugger("blah2");
2216 return(0);
2217 }
2218#endif
2219 KKASSERT(record->flush_group == record->ip->flush_group);
d36ec43b
MD
2220
2221 /*
2222 * Interlock the record using the BE flag. Once BE is set the
2223 * frontend cannot change the state of FE.
2224 *
2225 * NOTE: If FE is set prior to us setting BE we still sync the
2226 * record out, but the flush completion code converts it to
2227 * a delete-on-disk record instead of destroying it.
2228 */
4e17f465 2229 KKASSERT((record->flags & HAMMER_RECF_INTERLOCK_BE) == 0);
d36ec43b
MD
2230 record->flags |= HAMMER_RECF_INTERLOCK_BE;
2231
47637bff
MD
2232 /*
2233 * The backend may have already disposed of the record.
2234 */
2235 if (record->flags & HAMMER_RECF_DELETED_BE) {
2236 error = 0;
2237 goto done;
2238 }
2239
98f7132d
MD
2240 /*
2241 * If the whole inode is being deleting all on-disk records will
930bf163
MD
2242 * be deleted very soon, we can't sync any new records to disk
2243 * because they will be deleted in the same transaction they were
2244 * created in (delete_tid == create_tid), which will assert.
2245 *
2246 * XXX There may be a case with RECORD_ADD with DELETED_FE set
2247 * that we currently panic on.
98f7132d
MD
2248 */
2249 if (record->ip->sync_flags & HAMMER_INODE_DELETING) {
930bf163 2250 switch(record->type) {
47637bff
MD
2251 case HAMMER_MEM_RECORD_DATA:
2252 /*
2253 * We don't have to do anything, if the record was
2254 * committed the space will have been accounted for
2255 * in the blockmap.
2256 */
2257 /* fall through */
930bf163 2258 case HAMMER_MEM_RECORD_GENERAL:
98f7132d
MD
2259 record->flags |= HAMMER_RECF_DELETED_FE;
2260 record->flags |= HAMMER_RECF_DELETED_BE;
930bf163
MD
2261 error = 0;
2262 goto done;
2263 case HAMMER_MEM_RECORD_ADD:
2264 panic("hammer_sync_record_callback: illegal add "
2265 "during inode deletion record %p", record);
2266 break; /* NOT REACHED */
2267 case HAMMER_MEM_RECORD_INODE:
2268 panic("hammer_sync_record_callback: attempt to "
2269 "sync inode record %p?", record);
2270 break; /* NOT REACHED */
2271 case HAMMER_MEM_RECORD_DEL:
2272 /*
2273 * Follow through and issue the on-disk deletion
98f7132d 2274 */
930bf163 2275 break;
98f7132d 2276 }
98f7132d
MD
2277 }
2278
d36ec43b 2279 /*
7bc5b8c2
MD
2280 * If DELETED_FE is set special handling is needed for directory
2281 * entries. Dependant pieces related to the directory entry may
2282 * have already been synced to disk. If this occurs we have to
2283 * sync the directory entry and then change the in-memory record
2284 * from an ADD to a DELETE to cover the fact that it's been
2285 * deleted by the frontend.
2286 *
2287 * A directory delete covering record (MEM_RECORD_DEL) can never
2288 * be deleted by the frontend.
2289 *
2290 * Any other record type (aka DATA) can be deleted by the frontend.
2291 * XXX At the moment the flusher must skip it because there may
2292 * be another data record in the flush group for the same block,
2293 * meaning that some frontend data changes can leak into the backend's
2294 * synchronization point.
d36ec43b 2295 */
1f07f686 2296 if (record->flags & HAMMER_RECF_DELETED_FE) {
e8599db1
MD
2297 if (record->type == HAMMER_MEM_RECORD_ADD) {
2298 record->flags |= HAMMER_RECF_CONVERT_DELETE;
2299 } else {
2300 KKASSERT(record->type != HAMMER_MEM_RECORD_DEL);
7bc5b8c2
MD
2301 record->flags |= HAMMER_RECF_DELETED_BE;
2302 error = 0;
2303 goto done;
e8599db1 2304 }
1f07f686 2305 }
b84de5af
MD
2306
2307 /*
2308 * Assign the create_tid for new records. Deletions already
2309 * have the record's entire key properly set up.
2310 */
1f07f686 2311 if (record->type != HAMMER_MEM_RECORD_DEL)
11ad5ade 2312 record->leaf.base.create_tid = trans->tid;
dd94f1b1 2313 record->leaf.create_ts = trans->time32;
4e17f465
MD
2314 for (;;) {
2315 error = hammer_ip_sync_record_cursor(cursor, record);
2316 if (error != EDEADLK)
2317 break;
2318 hammer_done_cursor(cursor);
2319 error = hammer_init_cursor(trans, cursor, &record->ip->cache[0],
2320 record->ip);
2321 if (error)
2322 break;
2323 }
2324 record->flags &= ~HAMMER_RECF_CONVERT_DELETE;
c0ade690 2325
cdb6e4e6 2326 if (error)
b3deaf57 2327 error = -error;
98f7132d 2328done:
d36ec43b 2329 hammer_flush_record_done(record, error);
6c1f89f4
MD
2330
2331 /*
2332 * Do partial finalization if we have built up too many dirty
2333 * buffers. Otherwise a buffer cache deadlock can occur when
2334 * doing things like creating tens of thousands of tiny files.
2335 *
842e7a70
MD
2336 * We must release our cursor lock to avoid a 3-way deadlock
2337 * due to the exclusive sync lock the finalizer must get.
6c1f89f4 2338 */
842e7a70 2339 if (hammer_flusher_meta_limit(hmp)) {
982be4bf 2340 hammer_unlock_cursor(cursor);
6c1f89f4 2341 hammer_flusher_finalize(trans, 0);
982be4bf 2342 hammer_lock_cursor(cursor);
842e7a70 2343 }
6c1f89f4 2344
b3deaf57 2345 return(error);
c0ade690
MD
2346}
2347
2348/*
d7e278bb 2349 * Backend function called by the flusher to sync an inode to media.
c0ade690
MD
2350 */
2351int
02325004 2352hammer_sync_inode(hammer_transaction_t trans, hammer_inode_t ip)
c0ade690 2353{
4e17f465 2354 struct hammer_cursor cursor;
cb51be26 2355 hammer_node_t tmp_node;
1f07f686
MD
2356 hammer_record_t depend;
2357 hammer_record_t next;
ec4e8497 2358 int error, tmp_error;
1f07f686 2359 u_int64_t nlinks;
c0ade690 2360
1f07f686 2361 if ((ip->sync_flags & HAMMER_INODE_MODMASK) == 0)
d113fda1 2362 return(0);
d113fda1 2363
02325004 2364 error = hammer_init_cursor(trans, &cursor, &ip->cache[1], ip);
4e17f465
MD
2365 if (error)
2366 goto done;
c0ade690 2367
ec4e8497 2368 /*
1f07f686
MD
2369 * Any directory records referencing this inode which are not in
2370 * our current flush group must adjust our nlink count for the
2371 * purposes of synchronization to disk.
2372 *
2373 * Records which are in our flush group can be unlinked from our
c4bae5fd
MD
2374 * inode now, potentially allowing the inode to be physically
2375 * deleted.
bf3b416b
MD
2376 *
2377 * This cannot block.
ec4e8497 2378 */
11ad5ade 2379 nlinks = ip->ino_data.nlinks;
1f07f686
MD
2380 next = TAILQ_FIRST(&ip->target_list);
2381 while ((depend = next) != NULL) {
2382 next = TAILQ_NEXT(depend, target_entry);
2383 if (depend->flush_state == HAMMER_FST_FLUSH &&
7a61b85d 2384 depend->flush_group == ip->flush_group) {
c4bae5fd
MD
2385 /*
2386 * If this is an ADD that was deleted by the frontend
2387 * the frontend nlinks count will have already been
2388 * decremented, but the backend is going to sync its
2389 * directory entry and must account for it. The
2390 * record will be converted to a delete-on-disk when
2391 * it gets synced.
2392 *
2393 * If the ADD was not deleted by the frontend we
2394 * can remove the dependancy from our target_list.
2395 */
2396 if (depend->flags & HAMMER_RECF_DELETED_FE) {
2397 ++nlinks;
2398 } else {
2399 TAILQ_REMOVE(&ip->target_list, depend,
2400 target_entry);
2401 depend->target_ip = NULL;
2402 }
1f07f686 2403 } else if ((depend->flags & HAMMER_RECF_DELETED_FE) == 0) {
c4bae5fd
MD
2404 /*
2405 * Not part of our flush group
2406 */
2407 KKASSERT((depend->flags & HAMMER_RECF_DELETED_BE) == 0);
1f07f686
MD
2408 switch(depend->type) {
2409 case HAMMER_MEM_RECORD_ADD:
2410 --nlinks;
2411 break;
2412 case HAMMER_MEM_RECORD_DEL:
2413 ++nlinks;
2414 break;
e8599db1
MD
2415 default:
2416 break;
1f07f686 2417 }
ec4e8497 2418 }
ec4e8497
MD
2419 }
2420
c0ade690 2421 /*
1f07f686 2422 * Set dirty if we had to modify the link count.
c0ade690 2423 */
11ad5ade 2424 if (ip->sync_ino_data.nlinks != nlinks) {
1f07f686 2425 KKASSERT((int64_t)nlinks >= 0);
11ad5ade
MD
2426 ip->sync_ino_data.nlinks = nlinks;
2427 ip->sync_flags |= HAMMER_INODE_DDIRTY;
1f07f686 2428 }
b84de5af 2429
1f07f686 2430 /*
869e8f55
MD
2431 * If there is a trunction queued destroy any data past the (aligned)
2432 * truncation point. Userland will have dealt with the buffer
2433 * containing the truncation point for us.
2434 *
2435 * We don't flush pending frontend data buffers until after we've
cb51be26 2436 * dealt with the truncation.
1f07f686 2437 */
869e8f55 2438 if (ip->sync_flags & HAMMER_INODE_TRUNCATED) {
b84de5af
MD
2439 /*
2440 * Interlock trunc_off. The VOP front-end may continue to
2441 * make adjustments to it while we are blocked.
2442 */
2443 off_t trunc_off;
2444 off_t aligned_trunc_off;
4a2796f3 2445 int blkmask;
c0ade690 2446
b84de5af 2447 trunc_off = ip->sync_trunc_off;
4a2796f3
MD
2448 blkmask = hammer_blocksize(trunc_off) - 1;
2449 aligned_trunc_off = (trunc_off + blkmask) & ~(int64_t)blkmask;
b84de5af
MD
2450
2451 /*
2452 * Delete any whole blocks on-media. The front-end has
2453 * already cleaned out any partial block and made it
2454 * pending. The front-end may have updated trunc_off
47637bff 2455 * while we were blocked so we only use sync_trunc_off.
06ad81ff
MD
2456 *
2457 * This operation can blow out the buffer cache, EWOULDBLOCK
a9d52b76
MD
2458 * means we were unable to complete the deletion. The
2459 * deletion will update sync_trunc_off in that case.
b84de5af 2460 */
4e17f465 2461 error = hammer_ip_delete_range(&cursor, ip,
b84de5af 2462 aligned_trunc_off,
06ad81ff
MD
2463 0x7FFFFFFFFFFFFFFFLL, 2);
2464 if (error == EWOULDBLOCK) {
2465 ip->flags |= HAMMER_INODE_WOULDBLOCK;
2466 error = 0;
2467 goto defer_buffer_flush;
2468 }
2469
b84de5af 2470 if (error)
cdb6e4e6 2471 goto done;
47637bff
MD
2472
2473 /*
2474 * Clear the truncation flag on the backend after we have
2475 * complete the deletions. Backend data is now good again
2476 * (including new records we are about to sync, below).
cb51be26
MD
2477 *
2478 * Leave sync_trunc_off intact. As we write additional
2479 * records the backend will update sync_trunc_off. This
2480 * tells the backend whether it can skip the overwrite
2481 * test. This should work properly even when the backend
2482 * writes full blocks where the truncation point straddles
2483 * the block because the comparison is against the base
2484 * offset of the record.
47637bff 2485 */
b84de5af 2486 ip->sync_flags &= ~HAMMER_INODE_TRUNCATED;
cb51be26 2487 /* ip->sync_trunc_off = 0x7FFFFFFFFFFFFFFFLL; */
1f07f686
MD
2488 } else {
2489 error = 0;
f3b0f382
MD
2490 }
2491
1f07f686
MD
2492 /*
2493 * Now sync related records. These will typically be directory
6c1f89f4 2494 * entries, records tracking direct-writes, or delete-on-disk records.
1f07f686
MD
2495 */
2496 if (error == 0) {
2497 tmp_error = RB_SCAN(hammer_rec_rb_tree, &ip->rec_tree, NULL,
4e17f465 2498 hammer_sync_record_callback, &cursor);
1f07f686
MD
2499 if (tmp_error < 0)
2500 tmp_error = -error;
2501 if (tmp_error)
2502 error = tmp_error;
2503 }
bcac4bbb 2504 hammer_cache_node(&ip->cache[1], cursor.node);
cb51be26
MD
2505
2506 /*
43c665ae
MD
2507 * Re-seek for inode update, assuming our cache hasn't been ripped
2508 * out from under us.
cb51be26
MD
2509 */
2510 if (error == 0) {
2511 tmp_node = hammer_ref_node_safe(ip->hmp, &ip->cache[0], &error);
2512 if (tmp_node) {
5fa5c92f
MD
2513 hammer_cursor_downgrade(&cursor);
2514 hammer_lock_sh(&tmp_node->lock);
43c665ae
MD
2515 if ((tmp_node->flags & HAMMER_NODE_DELETED) == 0)
2516 hammer_cursor_seek(&cursor, tmp_node, 0);
5fa5c92f 2517 hammer_unlock(&tmp_node->lock);
cb51be26
MD
2518 hammer_rel_node(tmp_node);
2519 }
2520 error = 0;
2521 }
1f07f686
MD
2522
2523 /*
869e8f55
MD
2524 * If we are deleting the inode the frontend had better not have
2525 * any active references on elements making up the inode.
a9d52b76
MD
2526 *
2527 * The call to hammer_ip_delete_clean() cleans up auxillary records
2528 * but not DB or DATA records. Those must have already been deleted
2529 * by the normal truncation mechanic.
1f07f686 2530 */
11ad5ade 2531 if (error == 0 && ip->sync_ino_data.nlinks == 0 &&
869e8f55
MD
2532 RB_EMPTY(&ip->rec_tree) &&
2533 (ip->sync_flags & HAMMER_INODE_DELETING) &&
2534 (ip->flags & HAMMER_INODE_DELETED) == 0) {
2535 int count1 = 0;
1f07f686 2536
a9d52b76 2537 error = hammer_ip_delete_clean(&cursor, ip, &count1);
869e8f55 2538 if (error == 0) {
06ad81ff 2539 ip->flags |= HAMMER_INODE_DELETED;
869e8f55
MD
2540 ip->sync_flags &= ~HAMMER_INODE_DELETING;
2541 ip->sync_flags &= ~HAMMER_INODE_TRUNCATED;
2542 KKASSERT(RB_EMPTY(&ip->rec_tree));
1f07f686 2543
869e8f55
MD
2544 /*
2545 * Set delete_tid in both the frontend and backend
2546 * copy of the inode record. The DELETED flag handles
2547 * this, do not set RDIRTY.
2548 */
02325004
MD
2549 ip->ino_leaf.base.delete_tid = trans->tid;
2550 ip->sync_ino_leaf.base.delete_tid = trans->tid;
2551 ip->ino_leaf.delete_ts = trans->time32;
2552 ip->sync_ino_leaf.delete_ts = trans->time32;
dd94f1b1 2553
1f07f686 2554
869e8f55
MD
2555 /*
2556 * Adjust the inode count in the volume header
2557 */
02325004 2558 hammer_sync_lock_sh(trans);
f36a9737 2559 if (ip->flags & HAMMER_INODE_ONDISK) {
02325004
MD
2560 hammer_modify_volume_field(trans,
2561 trans->rootvol,
f36a9737
MD
2562 vol0_stat_inodes);
2563 --ip->hmp->rootvol->ondisk->vol0_stat_inodes;
02325004 2564 hammer_modify_volume_done(trans->rootvol);
f36a9737 2565 }
02325004 2566 hammer_sync_unlock(trans);
869e8f55 2567 }
1f07f686 2568 }
b84de5af 2569
b84de5af 2570 if (error)
cdb6e4e6
MD
2571 goto done;
2572 ip->sync_flags &= ~HAMMER_INODE_BUFS;
c0ade690 2573
06ad81ff 2574defer_buffer_flush:
c0ade690
MD
2575 /*
2576 * Now update the inode's on-disk inode-data and/or on-disk record.
b84de5af 2577 * DELETED and ONDISK are managed only in ip->flags.
06ad81ff
MD
2578 *
2579 * In the case of a defered buffer flush we still update the on-disk
2580 * inode to satisfy visibility requirements if there happen to be
2581 * directory dependancies.
c0ade690 2582 */
b84de5af 2583 switch(ip->flags & (HAMMER_INODE_DELETED | HAMMER_INODE_ONDISK)) {
c0ade690
MD
2584 case HAMMER_INODE_DELETED|HAMMER_INODE_ONDISK:
2585 /*
2586 * If deleted and on-disk, don't set any additional flags.
2587 * the delete flag takes care of things.
869e8f55
MD
2588 *
2589 * Clear flags which may have been set by the frontend.
c0ade690 2590 */
ddfdf542
MD
2591 ip->sync_flags &= ~(HAMMER_INODE_DDIRTY | HAMMER_INODE_XDIRTY |
2592 HAMMER_INODE_ATIME | HAMMER_INODE_MTIME |
869e8f55 2593 HAMMER_INODE_DELETING);
c0ade690
MD
2594 break;
2595 case HAMMER_INODE_DELETED:
2596 /*
2597 * Take care of the case where a deleted inode was never
2598 * flushed to the disk in the first place.
869e8f55
MD
2599 *
2600 * Clear flags which may have been set by the frontend.
c0ade690 2601 */
ddfdf542
MD
2602 ip->sync_flags &= ~(HAMMER_INODE_DDIRTY | HAMMER_INODE_XDIRTY |
2603 HAMMER_INODE_ATIME | HAMMER_INODE_MTIME |
869e8f55 2604 HAMMER_INODE_DELETING);
d26d0ae9 2605 while (RB_ROOT(&ip->rec_tree)) {
d36ec43b
MD
2606 hammer_record_t record = RB_ROOT(&ip->rec_tree);
2607 hammer_ref(&record->lock);
2608 KKASSERT(record->lock.refs == 1);
2609 record->flags |= HAMMER_RECF_DELETED_FE;
2610 record->flags |= HAMMER_RECF_DELETED_BE;
d36ec43b 2611 hammer_rel_mem_record(record);
d26d0ae9 2612 }
c0ade690
MD
2613 break;
2614 case HAMMER_INODE_ONDISK:
2615 /*
2616 * If already on-disk, do not set any additional flags.
2617 */
2618 break;
2619 default:
2620 /*
ddfdf542
MD
2621 * If not on-disk and not deleted, set DDIRTY to force
2622 * an initial record to be written.
b84de5af 2623 *
ddfdf542 2624 * Also set the create_tid in both the frontend and backend
b84de5af 2625 * copy of the inode record.
c0ade690 2626 */
02325004
MD
2627 ip->ino_leaf.base.create_tid = trans->tid;
2628 ip->ino_leaf.create_ts = trans->time32;
2629 ip->sync_ino_leaf.base.create_tid = trans->tid;
2630 ip->sync_ino_leaf.create_ts = trans->time32;
11ad5ade 2631 ip->sync_flags |= HAMMER_INODE_DDIRTY;
c0ade690
MD
2632 break;
2633 }
2634
2635 /*
d113fda1
MD
2636 * If RDIRTY or DDIRTY is set, write out a new record. If the inode
2637 * is already on-disk the old record is marked as deleted.
2638 *
2639 * If DELETED is set hammer_update_inode() will delete the existing
2640 * record without writing out a new one.
2641 *
2642 * If *ONLY* the ITIMES flag is set we can update the record in-place.
c0ade690 2643 */
b84de5af 2644 if (ip->flags & HAMMER_INODE_DELETED) {
4e17f465 2645 error = hammer_update_inode(&cursor, ip);
b84de5af 2646 } else
ddfdf542
MD
2647 if ((ip->sync_flags & HAMMER_INODE_DDIRTY) == 0 &&
2648 (ip->sync_flags & (HAMMER_INODE_ATIME | HAMMER_INODE_MTIME))) {
4e17f465 2649 error = hammer_update_itimes(&cursor, ip);
d113fda1 2650 } else
ddfdf542 2651 if (ip->sync_flags & (HAMMER_INODE_DDIRTY | HAMMER_INODE_ATIME | HAMMER_INODE_MTIME)) {
4e17f465 2652 error = hammer_update_inode(&cursor, ip);
c0ade690 2653 }
4e17f465 2654done:
cdb6e4e6
MD
2655 if (error) {
2656 hammer_critical_error(ip->hmp, ip, error,
2657 "while syncing inode");
2658 }
4e17f465 2659 hammer_done_cursor(&cursor);
c0ade690 2660 return(error);
8cd0a023
MD
2661}
2662
1f07f686
MD
2663/*
2664 * This routine is called when the OS is no longer actively referencing
2665 * the inode (but might still be keeping it cached), or when releasing
2666 * the last reference to an inode.
2667 *
2668 * At this point if the inode's nlinks count is zero we want to destroy
2669 * it, which may mean destroying it on-media too.
2670 */
3bf2d80a 2671void
e8599db1 2672hammer_inode_unloadable_check(hammer_inode_t ip, int getvp)
1f07f686 2673{
e8599db1
MD
2674 struct vnode *vp;
2675
1f07f686 2676 /*
c4bae5fd
MD
2677 * Set the DELETING flag when the link count drops to 0 and the
2678 * OS no longer has any opens on the inode.
2679 *
2680 * The backend will clear DELETING (a mod flag) and set DELETED
2681 * (a state flag) when it is actually able to perform the
2682 * operation.
35a49944
MD
2683 *
2684 * Don't reflag the deletion if the flusher is currently syncing
2685 * one that was already flagged. A previously set DELETING flag
2686 * may bounce around flags and sync_flags until the operation is
2687 * completely done.
1f07f686 2688 */
11ad5ade 2689 if (ip->ino_data.nlinks == 0 &&
35a49944 2690 ((ip->flags | ip->sync_flags) & (HAMMER_INODE_DELETING|HAMMER_INODE_DELETED)) == 0) {
e8599db1
MD
2691 ip->flags |= HAMMER_INODE_DELETING;
2692 ip->flags |= HAMMER_INODE_TRUNCATED;
2693 ip->trunc_off = 0;
2694 vp = NULL;
2695 if (getvp) {
2696 if (hammer_get_vnode(ip, &vp) != 0)
2697 return;
2698 }
29ce0677 2699
29ce0677
MD
2700 /*
2701 * Final cleanup
2702 */
869e8f55
MD
2703 if (ip->vp) {
2704 vtruncbuf(ip->vp, 0, HAMMER_BUFSIZE);
2705 vnode_pager_setsize(ip->vp, 0);
2706 }
e8599db1
MD
2707 if (getvp) {
2708 vput(vp);
2709 }
1f07f686 2710 }
1f07f686
MD
2711}
2712
3bf2d80a 2713/*
7b6ccb11
MD
2714 * After potentially resolving a dependancy the inode is tested
2715 * to determine whether it needs to be reflushed.
3bf2d80a 2716 */
1f07f686
MD
2717void
2718hammer_test_inode(hammer_inode_t ip)
2719{
2720 if (ip->flags & HAMMER_INODE_REFLUSH) {
2721 ip->flags &= ~HAMMER_INODE_REFLUSH;
2722 hammer_ref(&ip->lock);
3bf2d80a
MD
2723 if (ip->flags & HAMMER_INODE_RESIGNAL) {
2724 ip->flags &= ~HAMMER_INODE_RESIGNAL;
2725 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
2726 } else {
2727 hammer_flush_inode(ip, 0);
2728 }
1f07f686
MD
2729 hammer_rel_inode(ip, 0);
2730 }
2731}
2732
9f5097dc 2733/*
7bc5b8c2
MD
2734 * Clear the RECLAIM flag on an inode. This occurs when the inode is
2735 * reassociated with a vp or just before it gets freed.
af209b0f 2736 *
82010f9f
MD
2737 * Pipeline wakeups to threads blocked due to an excessive number of
2738 * detached inodes. The reclaim count generates a bit of negative
2739 * feedback.
9f5097dc 2740 */
7bc5b8c2 2741static void
82010f9f 2742hammer_inode_wakereclaims(hammer_inode_t ip, int dowake)
9f5097dc 2743{
7bc5b8c2 2744 struct hammer_reclaim *reclaim;
d99d6bf5 2745 hammer_mount_t hmp = ip->hmp;
d99d6bf5 2746
7bc5b8c2 2747 if ((ip->flags & HAMMER_INODE_RECLAIM) == 0)
d99d6bf5 2748 return;
3897d7e9 2749
7bc5b8c2
MD
2750 --hammer_count_reclaiming;
2751 --hmp->inode_reclaims;
2752 ip->flags &= ~HAMMER_INODE_RECLAIM;
9f5097dc 2753
82010f9f
MD
2754 if (hmp->inode_reclaims < HAMMER_RECLAIM_WAIT || dowake) {
2755 reclaim = TAILQ_FIRST(&hmp->reclaim_list);
2756 if (reclaim && reclaim->count > 0 && --reclaim->count == 0) {
2757 TAILQ_REMOVE(&hmp->reclaim_list, reclaim, entry);
2758 wakeup(reclaim);
2759 }
9f5097dc
MD
2760 }
2761}
2762
4a2796f3
MD
2763/*
2764 * Setup our reclaim pipeline. We only let so many detached (and dirty)
2765 * inodes build up before we start blocking.
2766 *
2767 * When we block we don't care *which* inode has finished reclaiming,
2768 * as lone as one does. This is somewhat heuristical... we also put a
2769 * cap on how long we are willing to wait.
2770 */
2771void
2772hammer_inode_waitreclaims(hammer_mount_t hmp)
2773{
2774 struct hammer_reclaim reclaim;
2775 int delay;
2776
82010f9f
MD
2777 if (hmp->inode_reclaims < HAMMER_RECLAIM_WAIT)
2778 return;
2779 delay = (hmp->inode_reclaims - HAMMER_RECLAIM_WAIT) * hz /
2780 (HAMMER_RECLAIM_WAIT * 3) + 1;
2781 if (delay > 0) {
2782 reclaim.count = 2;
2783 TAILQ_INSERT_TAIL(&hmp->reclaim_list, &reclaim, entry);
2784 tsleep(&reclaim, 0, "hmrrcm", delay);
2785 if (reclaim.count > 0)
2786 TAILQ_REMOVE(&hmp->reclaim_list, &reclaim, entry);
4a2796f3 2787 }
82010f9f 2788}
4a2796f3 2789
82010f9f
MD
2790/*
2791 * A larger then normal backlog of inodes is sitting in the flusher,
2792 * enforce a general slowdown to let it catch up. This routine is only
2793 * called on completion of a non-flusher-related transaction which
2794 * performed B-Tree node I/O.
2795 *
2796 * It is possible for the flusher to stall in a continuous load.
2797 * blogbench -i1000 -o seems to do a good job generating this sort of load.
2798 * If the flusher is unable to catch up the inode count can bloat until
2799 * we run out of kvm.
2800 *
2801 * This is a bit of a hack.
2802 */
2803void
2804hammer_inode_waithard(hammer_mount_t hmp)
2805{
2806 /*
2807 * Hysteresis.
2808 */
2809 if (hmp->flags & HAMMER_MOUNT_FLUSH_RECOVERY) {
2810 if (hmp->inode_reclaims < HAMMER_RECLAIM_WAIT / 2 &&
2811 hmp->count_iqueued < hmp->count_inodes / 20) {
2812 hmp->flags &= ~HAMMER_MOUNT_FLUSH_RECOVERY;
2813 return;
2814 }
2815 } else {
2816 if (hmp->inode_reclaims < HAMMER_RECLAIM_WAIT ||
2817 hmp->count_iqueued < hmp->count_inodes / 10) {
2818 return;
2819 }
2820 hmp->flags |= HAMMER_MOUNT_FLUSH_RECOVERY;
4a2796f3 2821 }
82010f9f
MD
2822
2823 /*
2824 * Block for one flush cycle.
2825 */
2826 hammer_flusher_wait_next(hmp);
4a2796f3
MD
2827}
2828