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