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