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