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