HAMMER 40E/Many: Inode/link-count sequencer cleanup pass.
[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 *
3bf2d80a 34 * $DragonFly: src/sys/vfs/hammer/hammer_inode.c,v 1.48 2008/05/03 07:59:06 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
ec4e8497 42static int hammer_unload_inode(struct hammer_inode *ip);
1f07f686
MD
43static void hammer_flush_inode_core(hammer_inode_t ip, int flags);
44static int hammer_setup_child_callback(hammer_record_t rec, void *data);
1f07f686 45static int hammer_setup_parent_inodes(hammer_record_t record);
b84de5af 46
d113fda1
MD
47/*
48 * The kernel is not actively referencing this vnode but is still holding
49 * it cached.
b84de5af
MD
50 *
51 * This is called from the frontend.
d113fda1 52 */
427e5fc6
MD
53int
54hammer_vop_inactive(struct vop_inactive_args *ap)
55{
66325755 56 struct hammer_inode *ip = VTOI(ap->a_vp);
27ea2398 57
c0ade690
MD
58 /*
59 * Degenerate case
60 */
61 if (ip == NULL) {
66325755 62 vrecycle(ap->a_vp);
c0ade690
MD
63 return(0);
64 }
65
66 /*
1f07f686
MD
67 * If the inode no longer has visibility in the filesystem and is
68 * fairly clean, try to recycle it immediately. This can deadlock
69 * in vfsync() if we aren't careful.
c0ade690 70 */
4e17f465
MD
71 hammer_inode_unloadable_check(ip);
72 if (ip->flags & HAMMER_INODE_MODMASK)
73 hammer_flush_inode(ip, 0);
74 else if (ip->ino_rec.ino_nlinks == 0)
d113fda1 75 vrecycle(ap->a_vp);
427e5fc6
MD
76 return(0);
77}
78
d113fda1
MD
79/*
80 * Release the vnode association. This is typically (but not always)
1f07f686 81 * the last reference on the inode.
d113fda1 82 *
1f07f686
MD
83 * Once the association is lost we are on our own with regards to
84 * flushing the inode.
d113fda1 85 */
427e5fc6
MD
86int
87hammer_vop_reclaim(struct vop_reclaim_args *ap)
88{
427e5fc6
MD
89 struct hammer_inode *ip;
90 struct vnode *vp;
91
92 vp = ap->a_vp;
c0ade690 93
a89aec1b
MD
94 if ((ip = vp->v_data) != NULL) {
95 vp->v_data = NULL;
96 ip->vp = NULL;
ec4e8497 97 hammer_rel_inode(ip, 1);
a89aec1b 98 }
427e5fc6
MD
99 return(0);
100}
101
66325755
MD
102/*
103 * Return a locked vnode for the specified inode. The inode must be
104 * referenced but NOT LOCKED on entry and will remain referenced on
105 * return.
b84de5af
MD
106 *
107 * Called from the frontend.
66325755
MD
108 */
109int
110hammer_get_vnode(struct hammer_inode *ip, int lktype, struct vnode **vpp)
111{
112 struct vnode *vp;
113 int error = 0;
114
115 for (;;) {
116 if ((vp = ip->vp) == NULL) {
117 error = getnewvnode(VT_HAMMER, ip->hmp->mp, vpp, 0, 0);
118 if (error)
119 break;
8cd0a023
MD
120 hammer_lock_ex(&ip->lock);
121 if (ip->vp != NULL) {
122 hammer_unlock(&ip->lock);
123 vp->v_type = VBAD;
124 vx_put(vp);
125 continue;
66325755 126 }
8cd0a023
MD
127 hammer_ref(&ip->lock);
128 vp = *vpp;
129 ip->vp = vp;
130 vp->v_type = hammer_get_vnode_type(
131 ip->ino_rec.base.base.obj_type);
7a04d74f
MD
132
133 switch(ip->ino_rec.base.base.obj_type) {
134 case HAMMER_OBJTYPE_CDEV:
135 case HAMMER_OBJTYPE_BDEV:
136 vp->v_ops = &ip->hmp->mp->mnt_vn_spec_ops;
137 addaliasu(vp, ip->ino_data.rmajor,
138 ip->ino_data.rminor);
139 break;
140 case HAMMER_OBJTYPE_FIFO:
141 vp->v_ops = &ip->hmp->mp->mnt_vn_fifo_ops;
142 break;
143 default:
144 break;
145 }
42c7d26b
MD
146
147 /*
148 * Only mark as the root vnode if the ip is not
149 * historical, otherwise the VFS cache will get
150 * confused. The other half of the special handling
151 * is in hammer_vop_nlookupdotdot().
152 */
153 if (ip->obj_id == HAMMER_OBJID_ROOT &&
154 ip->obj_asof == ip->hmp->asof) {
7a04d74f 155 vp->v_flag |= VROOT;
42c7d26b 156 }
7a04d74f 157
8cd0a023
MD
158 vp->v_data = (void *)ip;
159 /* vnode locked by getnewvnode() */
160 /* make related vnode dirty if inode dirty? */
161 hammer_unlock(&ip->lock);
a89aec1b
MD
162 if (vp->v_type == VREG)
163 vinitvmio(vp, ip->ino_rec.ino_size);
8cd0a023
MD
164 break;
165 }
166
167 /*
168 * loop if the vget fails (aka races), or if the vp
169 * no longer matches ip->vp.
170 */
171 if (vget(vp, LK_EXCLUSIVE) == 0) {
172 if (vp == ip->vp)
173 break;
174 vput(vp);
66325755
MD
175 }
176 }
a89aec1b 177 *vpp = vp;
66325755
MD
178 return(error);
179}
180
181/*
8cd0a023
MD
182 * Acquire a HAMMER inode. The returned inode is not locked. These functions
183 * do not attach or detach the related vnode (use hammer_get_vnode() for
184 * that).
d113fda1
MD
185 *
186 * The flags argument is only applied for newly created inodes, and only
187 * certain flags are inherited.
b84de5af
MD
188 *
189 * Called from the frontend.
66325755
MD
190 */
191struct hammer_inode *
36f82b23 192hammer_get_inode(hammer_transaction_t trans, struct hammer_node **cache,
61aeeb33 193 u_int64_t obj_id, hammer_tid_t asof, int flags, int *errorp)
66325755 194{
36f82b23 195 hammer_mount_t hmp = trans->hmp;
427e5fc6 196 struct hammer_inode_info iinfo;
8cd0a023 197 struct hammer_cursor cursor;
427e5fc6 198 struct hammer_inode *ip;
427e5fc6
MD
199
200 /*
201 * Determine if we already have an inode cached. If we do then
202 * we are golden.
203 */
66325755 204 iinfo.obj_id = obj_id;
7f7c1f84 205 iinfo.obj_asof = asof;
427e5fc6
MD
206loop:
207 ip = hammer_ino_rb_tree_RB_LOOKUP_INFO(&hmp->rb_inos_root, &iinfo);
208 if (ip) {
8cd0a023 209 hammer_ref(&ip->lock);
66325755
MD
210 *errorp = 0;
211 return(ip);
427e5fc6
MD
212 }
213
427e5fc6 214 ip = kmalloc(sizeof(*ip), M_HAMMER, M_WAITOK|M_ZERO);
b3deaf57 215 ++hammer_count_inodes;
66325755 216 ip->obj_id = obj_id;
27ea2398 217 ip->obj_asof = iinfo.obj_asof;
66325755 218 ip->hmp = hmp;
d113fda1
MD
219 ip->flags = flags & HAMMER_INODE_RO;
220 if (hmp->ronly)
221 ip->flags |= HAMMER_INODE_RO;
a5fddc16 222 ip->trunc_off = 0x7FFFFFFFFFFFFFFFLL;
8cd0a023 223 RB_INIT(&ip->rec_tree);
059819e3 224 TAILQ_INIT(&ip->bio_list);
b84de5af 225 TAILQ_INIT(&ip->bio_alt_list);
1f07f686 226 TAILQ_INIT(&ip->target_list);
427e5fc6
MD
227
228 /*
8cd0a023 229 * Locate the on-disk inode.
427e5fc6 230 */
6a37e7e4 231retry:
4e17f465 232 hammer_init_cursor(trans, &cursor, cache, NULL);
8cd0a023
MD
233 cursor.key_beg.obj_id = ip->obj_id;
234 cursor.key_beg.key = 0;
d5530d22 235 cursor.key_beg.create_tid = 0;
8cd0a023
MD
236 cursor.key_beg.delete_tid = 0;
237 cursor.key_beg.rec_type = HAMMER_RECTYPE_INODE;
238 cursor.key_beg.obj_type = 0;
d5530d22
MD
239 cursor.asof = iinfo.obj_asof;
240 cursor.flags = HAMMER_CURSOR_GET_RECORD | HAMMER_CURSOR_GET_DATA |
241 HAMMER_CURSOR_ASOF;
8cd0a023
MD
242
243 *errorp = hammer_btree_lookup(&cursor);
6a37e7e4
MD
244 if (*errorp == EDEADLK) {
245 hammer_done_cursor(&cursor);
246 goto retry;
247 }
427e5fc6
MD
248
249 /*
250 * On success the B-Tree lookup will hold the appropriate
251 * buffer cache buffers and provide a pointer to the requested
d113fda1
MD
252 * information. Copy the information to the in-memory inode
253 * and cache the B-Tree node to improve future operations.
427e5fc6 254 */
66325755 255 if (*errorp == 0) {
8cd0a023 256 ip->ino_rec = cursor.record->inode;
40043e7f 257 ip->ino_data = cursor.data->inode;
61aeeb33
MD
258 hammer_cache_node(cursor.node, &ip->cache[0]);
259 if (cache)
260 hammer_cache_node(cursor.node, cache);
427e5fc6 261 }
427e5fc6
MD
262
263 /*
264 * On success load the inode's record and data and insert the
265 * inode into the B-Tree. It is possible to race another lookup
266 * insertion of the same inode so deal with that condition too.
b3deaf57
MD
267 *
268 * The cursor's locked node interlocks against others creating and
269 * destroying ip while we were blocked.
427e5fc6 270 */
66325755 271 if (*errorp == 0) {
8cd0a023 272 hammer_ref(&ip->lock);
427e5fc6 273 if (RB_INSERT(hammer_ino_rb_tree, &hmp->rb_inos_root, ip)) {
61aeeb33
MD
274 hammer_uncache_node(&ip->cache[0]);
275 hammer_uncache_node(&ip->cache[1]);
b84de5af 276 KKASSERT(ip->lock.refs == 1);
b3deaf57 277 --hammer_count_inodes;
427e5fc6 278 kfree(ip, M_HAMMER);
b3deaf57 279 hammer_done_cursor(&cursor);
427e5fc6
MD
280 goto loop;
281 }
c0ade690 282 ip->flags |= HAMMER_INODE_ONDISK;
427e5fc6 283 } else {
b3deaf57 284 --hammer_count_inodes;
66325755
MD
285 kfree(ip, M_HAMMER);
286 ip = NULL;
427e5fc6 287 }
b3deaf57 288 hammer_done_cursor(&cursor);
66325755
MD
289 return (ip);
290}
291
8cd0a023
MD
292/*
293 * Create a new filesystem object, returning the inode in *ipp. The
1f07f686 294 * returned inode will be referenced.
8cd0a023 295 *
b84de5af 296 * The inode is created in-memory.
8cd0a023
MD
297 */
298int
a89aec1b
MD
299hammer_create_inode(hammer_transaction_t trans, struct vattr *vap,
300 struct ucred *cred, hammer_inode_t dip,
8cd0a023 301 struct hammer_inode **ipp)
66325755 302{
a89aec1b
MD
303 hammer_mount_t hmp;
304 hammer_inode_t ip;
6b4f890b 305 uid_t xuid;
66325755 306
8cd0a023
MD
307 hmp = trans->hmp;
308 ip = kmalloc(sizeof(*ip), M_HAMMER, M_WAITOK|M_ZERO);
b3deaf57 309 ++hammer_count_inodes;
0729c8c8 310 ip->obj_id = hammer_alloc_objid(trans, dip);
8cd0a023 311 KKASSERT(ip->obj_id != 0);
7f7c1f84 312 ip->obj_asof = hmp->asof;
8cd0a023 313 ip->hmp = hmp;
b84de5af 314 ip->flush_state = HAMMER_FST_IDLE;
8cd0a023 315 ip->flags = HAMMER_INODE_DDIRTY | HAMMER_INODE_RDIRTY |
b84de5af 316 HAMMER_INODE_ITIMES;
8cd0a023 317
a5fddc16 318 ip->trunc_off = 0x7FFFFFFFFFFFFFFFLL;
8cd0a023 319 RB_INIT(&ip->rec_tree);
059819e3 320 TAILQ_INIT(&ip->bio_list);
b84de5af 321 TAILQ_INIT(&ip->bio_alt_list);
1f07f686 322 TAILQ_INIT(&ip->target_list);
8cd0a023 323
b84de5af
MD
324 ip->ino_rec.ino_atime = trans->time;
325 ip->ino_rec.ino_mtime = trans->time;
8cd0a023
MD
326 ip->ino_rec.ino_size = 0;
327 ip->ino_rec.ino_nlinks = 0;
328 /* XXX */
fe7678ee 329 ip->ino_rec.base.base.btype = HAMMER_BTREE_TYPE_RECORD;
8cd0a023
MD
330 ip->ino_rec.base.base.obj_id = ip->obj_id;
331 ip->ino_rec.base.base.key = 0;
b84de5af 332 ip->ino_rec.base.base.create_tid = 0;
8cd0a023
MD
333 ip->ino_rec.base.base.delete_tid = 0;
334 ip->ino_rec.base.base.rec_type = HAMMER_RECTYPE_INODE;
335 ip->ino_rec.base.base.obj_type = hammer_get_obj_type(vap->va_type);
336
337 ip->ino_data.version = HAMMER_INODE_DATA_VERSION;
338 ip->ino_data.mode = vap->va_mode;
b84de5af 339 ip->ino_data.ctime = trans->time;
8cd0a023 340 ip->ino_data.parent_obj_id = (dip) ? dip->ino_rec.base.base.obj_id : 0;
6b4f890b 341
7a04d74f
MD
342 switch(ip->ino_rec.base.base.obj_type) {
343 case HAMMER_OBJTYPE_CDEV:
344 case HAMMER_OBJTYPE_BDEV:
345 ip->ino_data.rmajor = vap->va_rmajor;
346 ip->ino_data.rminor = vap->va_rminor;
347 break;
348 default:
349 break;
350 }
351
6b4f890b
MD
352 /*
353 * Calculate default uid/gid and overwrite with information from
354 * the vap.
355 */
356 xuid = hammer_to_unix_xid(&dip->ino_data.uid);
357 ip->ino_data.gid = dip->ino_data.gid;
358 xuid = vop_helper_create_uid(hmp->mp, dip->ino_data.mode, xuid, cred,
359 &vap->va_mode);
360 ip->ino_data.mode = vap->va_mode;
361
8cd0a023
MD
362 if (vap->va_vaflags & VA_UID_UUID_VALID)
363 ip->ino_data.uid = vap->va_uid_uuid;
6b4f890b
MD
364 else if (vap->va_uid != (uid_t)VNOVAL)
365 hammer_guid_to_uuid(&ip->ino_data.uid, xuid);
8cd0a023
MD
366 if (vap->va_vaflags & VA_GID_UUID_VALID)
367 ip->ino_data.gid = vap->va_gid_uuid;
6b4f890b 368 else if (vap->va_gid != (gid_t)VNOVAL)
8cd0a023
MD
369 hammer_guid_to_uuid(&ip->ino_data.gid, vap->va_gid);
370
371 hammer_ref(&ip->lock);
372 if (RB_INSERT(hammer_ino_rb_tree, &hmp->rb_inos_root, ip)) {
373 hammer_unref(&ip->lock);
a89aec1b 374 panic("hammer_create_inode: duplicate obj_id %llx", ip->obj_id);
8cd0a023
MD
375 }
376 *ipp = ip;
377 return(0);
66325755
MD
378}
379
d113fda1
MD
380/*
381 * Called by hammer_sync_inode().
382 */
383static int
4e17f465 384hammer_update_inode(hammer_cursor_t cursor, hammer_inode_t ip)
c0ade690 385{
4e17f465 386 hammer_transaction_t trans = cursor->trans;
c0ade690
MD
387 hammer_record_t record;
388 int error;
389
d26d0ae9 390retry:
c0ade690
MD
391 error = 0;
392
869e8f55
MD
393 /*
394 * If the inode has a presence on-disk then locate it and mark
395 * it deleted, setting DELONDISK.
396 *
397 * The record may or may not be physically deleted, depending on
398 * the retention policy.
399 */
76376933
MD
400 if ((ip->flags & (HAMMER_INODE_ONDISK|HAMMER_INODE_DELONDISK)) ==
401 HAMMER_INODE_ONDISK) {
4e17f465
MD
402 hammer_normalize_cursor(cursor);
403 cursor->key_beg.obj_id = ip->obj_id;
404 cursor->key_beg.key = 0;
405 cursor->key_beg.create_tid = 0;
406 cursor->key_beg.delete_tid = 0;
407 cursor->key_beg.rec_type = HAMMER_RECTYPE_INODE;
408 cursor->key_beg.obj_type = 0;
409 cursor->asof = ip->obj_asof;
410 cursor->flags &= ~HAMMER_CURSOR_INITMASK;
411 cursor->flags |= HAMMER_CURSOR_GET_RECORD | HAMMER_CURSOR_ASOF;
412 cursor->flags |= HAMMER_CURSOR_BACKEND;
413
414 error = hammer_btree_lookup(cursor);
b84de5af
MD
415 if (error) {
416 kprintf("error %d\n", error);
417 Debugger("hammer_update_inode");
418 }
419
c0ade690 420 if (error == 0) {
4e17f465 421 error = hammer_ip_delete_record(cursor, trans->tid);
f90dde4c 422 if (error && error != EDEADLK) {
b84de5af
MD
423 kprintf("error %d\n", error);
424 Debugger("hammer_update_inode2");
425 }
1f07f686 426 if (error == 0) {
195c19a1 427 ip->flags |= HAMMER_INODE_DELONDISK;
1f07f686 428 }
4e17f465
MD
429 hammer_cache_node(cursor->node, &ip->cache[0]);
430 }
431 if (error == EDEADLK) {
432 hammer_done_cursor(cursor);
433 error = hammer_init_cursor(trans, cursor,
434 &ip->cache[0], ip);
435 if (error == 0)
436 goto retry;
c0ade690 437 }
c0ade690
MD
438 }
439
440 /*
869e8f55
MD
441 * Ok, write out the initial record or a new record (after deleting
442 * the old one), unless the DELETED flag is set. This routine will
443 * clear DELONDISK if it writes out a record.
76376933 444 *
869e8f55
MD
445 * Update our inode statistics if this is the first application of
446 * the inode on-disk.
c0ade690 447 */
869e8f55
MD
448 if (error == 0 && (ip->flags & HAMMER_INODE_DELETED) == 0) {
449 /*
450 * Generate a record and write it to the media
451 */
40043e7f 452 record = hammer_alloc_mem_record(ip);
1f07f686 453 record->flush_state = HAMMER_FST_FLUSH;
b84de5af 454 record->rec.inode = ip->sync_ino_rec;
855942b6 455 record->rec.inode.base.base.create_tid = trans->tid;
b84de5af
MD
456 record->rec.inode.base.data_len = sizeof(ip->sync_ino_data);
457 record->data = (void *)&ip->sync_ino_data;
d36ec43b 458 record->flags |= HAMMER_RECF_INTERLOCK_BE;
4e17f465
MD
459 for (;;) {
460 error = hammer_ip_sync_record_cursor(cursor, record);
461 if (error != EDEADLK)
462 break;
463 hammer_done_cursor(cursor);
464 error = hammer_init_cursor(trans, cursor,
465 &ip->cache[0], ip);
466 if (error)
467 break;
468 }
b84de5af
MD
469 if (error) {
470 kprintf("error %d\n", error);
471 Debugger("hammer_update_inode3");
472 }
d36ec43b
MD
473
474 /*
475 * The record isn't managed by the inode's record tree,
476 * destroy it whether we succeed or fail.
477 */
478 record->flags &= ~HAMMER_RECF_INTERLOCK_BE;
479 record->flags |= HAMMER_RECF_DELETED_FE;
1f07f686 480 record->flush_state = HAMMER_FST_IDLE;
b3deaf57 481 hammer_rel_mem_record(record);
d36ec43b 482
869e8f55
MD
483 /*
484 * Finish up.
485 */
d26d0ae9 486 if (error == 0) {
b84de5af
MD
487 ip->sync_flags &= ~(HAMMER_INODE_RDIRTY |
488 HAMMER_INODE_DDIRTY |
489 HAMMER_INODE_ITIMES);
490 ip->flags &= ~HAMMER_INODE_DELONDISK;
1f07f686
MD
491
492 /*
493 * Root volume count of inodes
494 */
d26d0ae9 495 if ((ip->flags & HAMMER_INODE_ONDISK) == 0) {
10a5d1ba 496 hammer_modify_volume(trans, trans->rootvol,
36f82b23 497 NULL, 0);
0b075555 498 ++ip->hmp->rootvol->ondisk->vol0_stat_inodes;
10a5d1ba 499 hammer_modify_volume_done(trans->rootvol);
d26d0ae9
MD
500 ip->flags |= HAMMER_INODE_ONDISK;
501 }
fbc6e32a 502 }
c0ade690 503 }
869e8f55
MD
504
505 /*
506 * If the inode has been destroyed, clean out any left-over flags
507 * that may have been set by the frontend.
508 */
f90dde4c 509 if (error == 0 && (ip->flags & HAMMER_INODE_DELETED)) {
f90dde4c
MD
510 ip->sync_flags &= ~(HAMMER_INODE_RDIRTY |
511 HAMMER_INODE_DDIRTY |
512 HAMMER_INODE_ITIMES);
513 }
c0ade690
MD
514 return(error);
515}
516
a89aec1b 517/*
d113fda1
MD
518 * Update only the itimes fields. This is done no-historically. The
519 * record is updated in-place on the disk.
520 */
521static int
4e17f465 522hammer_update_itimes(hammer_cursor_t cursor, hammer_inode_t ip)
d113fda1 523{
4e17f465 524 hammer_transaction_t trans = cursor->trans;
d113fda1
MD
525 struct hammer_inode_record *rec;
526 int error;
527
6a37e7e4 528retry:
d113fda1
MD
529 error = 0;
530 if ((ip->flags & (HAMMER_INODE_ONDISK|HAMMER_INODE_DELONDISK)) ==
531 HAMMER_INODE_ONDISK) {
4e17f465
MD
532 hammer_normalize_cursor(cursor);
533 cursor->key_beg.obj_id = ip->obj_id;
534 cursor->key_beg.key = 0;
535 cursor->key_beg.create_tid = 0;
536 cursor->key_beg.delete_tid = 0;
537 cursor->key_beg.rec_type = HAMMER_RECTYPE_INODE;
538 cursor->key_beg.obj_type = 0;
539 cursor->asof = ip->obj_asof;
540 cursor->flags &= ~HAMMER_CURSOR_INITMASK;
541 cursor->flags |= HAMMER_CURSOR_GET_RECORD | HAMMER_CURSOR_ASOF;
542 cursor->flags |= HAMMER_CURSOR_BACKEND;
543
544 error = hammer_btree_lookup(cursor);
b84de5af
MD
545 if (error) {
546 kprintf("error %d\n", error);
547 Debugger("hammer_update_itimes1");
548 }
d113fda1 549 if (error == 0) {
10a5d1ba
MD
550 /*
551 * Do not generate UNDO records for atime/mtime
552 * updates.
553 */
4e17f465
MD
554 rec = &cursor->record->inode;
555 hammer_modify_buffer(trans, cursor->record_buffer,
36f82b23 556 NULL, 0);
b84de5af
MD
557 rec->ino_atime = ip->sync_ino_rec.ino_atime;
558 rec->ino_mtime = ip->sync_ino_rec.ino_mtime;
4e17f465 559 hammer_modify_buffer_done(cursor->record_buffer);
b84de5af 560 ip->sync_flags &= ~HAMMER_INODE_ITIMES;
d113fda1 561 /* XXX recalculate crc */
4e17f465
MD
562 hammer_cache_node(cursor->node, &ip->cache[0]);
563 }
564 if (error == EDEADLK) {
565 hammer_done_cursor(cursor);
566 error = hammer_init_cursor(trans, cursor,
567 &ip->cache[0], ip);
568 if (error == 0)
569 goto retry;
d113fda1 570 }
d113fda1
MD
571 }
572 return(error);
573}
574
575/*
1f07f686 576 * Release a reference on an inode, flush as requested.
b84de5af
MD
577 *
578 * On the last reference we queue the inode to the flusher for its final
579 * disposition.
a89aec1b 580 */
66325755 581void
a89aec1b 582hammer_rel_inode(struct hammer_inode *ip, int flush)
66325755 583{
1f07f686
MD
584 hammer_mount_t hmp = ip->hmp;
585
f90dde4c
MD
586 /*
587 * Handle disposition when dropping the last ref.
588 */
1f07f686
MD
589 for (;;) {
590 if (ip->lock.refs == 1) {
591 /*
592 * Determine whether on-disk action is needed for
593 * the inode's final disposition.
594 */
4e17f465
MD
595 hammer_inode_unloadable_check(ip);
596 if (ip->flags & HAMMER_INODE_MODMASK) {
597 hammer_flush_inode(ip, 0);
598 } else if (ip->lock.refs == 1) {
1f07f686
MD
599 hammer_unload_inode(ip);
600 break;
601 }
b84de5af 602 } else {
4e17f465 603 if (flush)
1f07f686 604 hammer_flush_inode(ip, 0);
4e17f465 605
1f07f686
MD
606 /*
607 * The inode still has multiple refs, try to drop
608 * one ref.
609 */
610 KKASSERT(ip->lock.refs >= 1);
611 if (ip->lock.refs > 1) {
612 hammer_unref(&ip->lock);
613 break;
614 }
b84de5af 615 }
f90dde4c
MD
616 }
617
618 /*
1f07f686
MD
619 * XXX bad hack until I add code to track inodes in SETUP. We
620 * can queue a lot of inodes to the syncer but if we don't wake
621 * it up the undo sets will be too large or too many unflushed
622 * records will build up and blow our malloc limit.
f90dde4c 623 */
1f07f686
MD
624 if (++hmp->reclaim_count > 256) {
625 hmp->reclaim_count = 0;
626 hammer_flusher_async(hmp);
9480ff55 627 }
427e5fc6
MD
628}
629
27ea2398 630/*
b84de5af
MD
631 * Unload and destroy the specified inode. Must be called with one remaining
632 * reference. The reference is disposed of.
8cd0a023 633 *
b84de5af 634 * This can only be called in the context of the flusher.
27ea2398 635 */
b84de5af 636static int
ec4e8497 637hammer_unload_inode(struct hammer_inode *ip)
27ea2398 638{
b84de5af 639 KASSERT(ip->lock.refs == 1,
a89aec1b 640 ("hammer_unload_inode: %d refs\n", ip->lock.refs));
8cd0a023 641 KKASSERT(ip->vp == NULL);
f90dde4c
MD
642 KKASSERT(ip->flush_state == HAMMER_FST_IDLE);
643 KKASSERT(ip->cursor_ip_refs == 0);
644 KKASSERT((ip->flags & HAMMER_INODE_MODMASK) == 0);
645
646 KKASSERT(RB_EMPTY(&ip->rec_tree));
1f07f686 647 KKASSERT(TAILQ_EMPTY(&ip->target_list));
f90dde4c
MD
648 KKASSERT(TAILQ_EMPTY(&ip->bio_list));
649 KKASSERT(TAILQ_EMPTY(&ip->bio_alt_list));
650
651 RB_REMOVE(hammer_ino_rb_tree, &ip->hmp->rb_inos_root, ip);
652
653 hammer_uncache_node(&ip->cache[0]);
654 hammer_uncache_node(&ip->cache[1]);
0729c8c8
MD
655 if (ip->objid_cache)
656 hammer_clear_objid(ip);
f90dde4c
MD
657 --hammer_count_inodes;
658 kfree(ip, M_HAMMER);
6b4f890b 659
27ea2398
MD
660 return(0);
661}
662
427e5fc6 663/*
d113fda1
MD
664 * A transaction has modified an inode, requiring updates as specified by
665 * the passed flags.
7f7c1f84 666 *
d113fda1
MD
667 * HAMMER_INODE_RDIRTY: Inode record has been updated
668 * HAMMER_INODE_DDIRTY: Inode data has been updated
1f07f686 669 * HAMMER_INODE_XDIRTY: Dirty in-memory records
4e17f465 670 * HAMMER_INODE_BUFS: Dirty buffer cache buffers
d113fda1
MD
671 * HAMMER_INODE_DELETED: Inode record/data must be deleted
672 * HAMMER_INODE_ITIMES: mtime/atime has been updated
427e5fc6 673 */
66325755 674void
b84de5af 675hammer_modify_inode(hammer_transaction_t trans, hammer_inode_t ip, int flags)
427e5fc6 676{
d113fda1 677 KKASSERT ((ip->flags & HAMMER_INODE_RO) == 0 ||
b84de5af 678 (flags & (HAMMER_INODE_RDIRTY|HAMMER_INODE_DDIRTY|
1f07f686 679 HAMMER_INODE_XDIRTY|HAMMER_INODE_BUFS|
b84de5af
MD
680 HAMMER_INODE_DELETED|HAMMER_INODE_ITIMES)) == 0);
681
682 ip->flags |= flags;
683}
684
685/*
1f07f686
MD
686 * Request that an inode be flushed. This whole mess cannot block and may
687 * recurse. Once requested HAMMER will attempt to actively flush it until
688 * the flush can be done.
b84de5af 689 *
1f07f686
MD
690 * The inode may already be flushing, or may be in a setup state. We can
691 * place the inode in a flushing state if it is currently idle and flag it
692 * to reflush if it is currently flushing.
b84de5af
MD
693 */
694void
f90dde4c 695hammer_flush_inode(hammer_inode_t ip, int flags)
b84de5af 696{
1f07f686
MD
697 hammer_record_t depend;
698 int r, good;
699
700 /*
701 * Trivial 'nothing to flush' case. If the inode is ina SETUP
702 * state we have to put it back into an IDLE state so we can
703 * drop the extra ref.
704 */
4e17f465 705 if ((ip->flags & HAMMER_INODE_MODMASK) == 0) {
1f07f686
MD
706 if (ip->flush_state == HAMMER_FST_SETUP) {
707 ip->flush_state = HAMMER_FST_IDLE;
708 hammer_rel_inode(ip, 0);
ec4e8497 709 }
b84de5af
MD
710 return;
711 }
42c7d26b 712
1f07f686
MD
713 /*
714 * Our flush action will depend on the current state.
715 */
716 switch(ip->flush_state) {
717 case HAMMER_FST_IDLE:
718 /*
719 * We have no dependancies and can flush immediately. Some
720 * our children may not be flushable so we have to re-test
721 * with that additional knowledge.
722 */
723 hammer_flush_inode_core(ip, flags);
724 break;
725 case HAMMER_FST_SETUP:
726 /*
727 * Recurse upwards through dependancies via target_list
728 * and start their flusher actions going if possible.
729 *
730 * 'good' is our connectivity. -1 means we have none and
731 * can't flush, 0 means there weren't any dependancies, and
732 * 1 means we have good connectivity.
733 */
734 good = 0;
735 TAILQ_FOREACH(depend, &ip->target_list, target_entry) {
736 r = hammer_setup_parent_inodes(depend);
737 if (r < 0 && good == 0)
738 good = -1;
739 if (r > 0)
740 good = 1;
741 }
742
743 /*
744 * We can continue if good >= 0. Determine how many records
745 * under our inode can be flushed (and mark them).
746 */
1f07f686
MD
747 if (good >= 0) {
748 hammer_flush_inode_core(ip, flags);
749 } else {
750 ip->flags |= HAMMER_INODE_REFLUSH;
4e17f465
MD
751 if (flags & HAMMER_FLUSH_SIGNAL) {
752 ip->flags |= HAMMER_INODE_RESIGNAL;
753 hammer_flusher_async(ip->hmp);
754 }
1f07f686
MD
755 }
756 break;
757 default:
758 /*
759 * We are already flushing, flag the inode to reflush
760 * if needed after it completes its current flush.
761 */
762 if ((ip->flags & HAMMER_INODE_REFLUSH) == 0)
763 ip->flags |= HAMMER_INODE_REFLUSH;
4e17f465
MD
764 if (flags & HAMMER_FLUSH_SIGNAL) {
765 ip->flags |= HAMMER_INODE_RESIGNAL;
766 hammer_flusher_async(ip->hmp);
767 }
1f07f686
MD
768 break;
769 }
770}
771
772/*
773 * We are asked to recurse upwards and convert the record from SETUP
774 * to FLUSH if possible. record->ip is a parent of the caller's inode,
775 * and record->target_ip is the caller's inode.
776 *
777 * Return 1 if the record gives us connectivity
778 *
779 * Return 0 if the record is not relevant
780 *
781 * Return -1 if we can't resolve the dependancy and there is no connectivity.
782 */
783static int
784hammer_setup_parent_inodes(hammer_record_t record)
785{
786 hammer_mount_t hmp = record->ip->hmp;
787 hammer_record_t depend;
788 hammer_inode_t ip;
789 int r, good;
790
791 KKASSERT(record->flush_state != HAMMER_FST_IDLE);
792 ip = record->ip;
793
794 /*
795 * If the record is already flushing, is it in our flush group?
796 *
797 * If it is in our flush group but it is a delete-on-disk, it
798 * does not improve our connectivity (return 0), and if the
799 * target inode is not trying to destroy itself we can't allow
800 * the operation yet anyway (the second return -1).
801 */
802 if (record->flush_state == HAMMER_FST_FLUSH) {
803 if (record->flush_group != hmp->flusher_next) {
804 ip->flags |= HAMMER_INODE_REFLUSH;
805 return(-1);
f90dde4c 806 }
1f07f686
MD
807 if (record->type == HAMMER_MEM_RECORD_ADD)
808 return(1);
809 return(0);
810 }
811
812 /*
813 * It must be a setup record. Try to resolve the setup dependancies
814 * by recursing upwards so we can place ip on the flush list.
815 */
816 KKASSERT(record->flush_state == HAMMER_FST_SETUP);
817
818 good = 0;
819 TAILQ_FOREACH(depend, &ip->target_list, target_entry) {
820 r = hammer_setup_parent_inodes(depend);
821 if (r < 0 && good == 0)
822 good = -1;
823 if (r > 0)
824 good = 1;
825 }
826
827 /*
828 * We can't flush ip because it has no connectivity (XXX also check
829 * nlinks for pre-existing connectivity!). Flag it so any resolution
830 * recurses back down.
831 */
832 if (good < 0) {
833 ip->flags |= HAMMER_INODE_REFLUSH;
834 return(good);
835 }
836
837 /*
838 * We are go, place the parent inode in a flushing state so we can
839 * place its record in a flushing state. Note that the parent
840 * may already be flushing. The record must be in the same flush
841 * group as the parent.
842 */
843 if (ip->flush_state != HAMMER_FST_FLUSH)
844 hammer_flush_inode_core(ip, HAMMER_FLUSH_RECURSION);
845 KKASSERT(ip->flush_state == HAMMER_FST_FLUSH);
846 KKASSERT(record->flush_state == HAMMER_FST_SETUP);
847
848#if 0
849 if (record->type == HAMMER_MEM_RECORD_DEL &&
869e8f55 850 (record->target_ip->flags & (HAMMER_INODE_DELETED|HAMMER_INODE_DELONDISK)) == 0) {
1f07f686
MD
851 /*
852 * Regardless of flushing state we cannot sync this path if the
853 * record represents a delete-on-disk but the target inode
854 * is not ready to sync its own deletion.
855 *
856 * XXX need to count effective nlinks to determine whether
857 * the flush is ok, otherwise removing a hardlink will
858 * just leave the DEL record to rot.
859 */
860 record->target_ip->flags |= HAMMER_INODE_REFLUSH;
861 return(-1);
862 } else
863#endif
864 if (ip->flush_group == ip->hmp->flusher_next) {
865 /*
866 * This is the record we wanted to synchronize.
867 */
868 record->flush_state = HAMMER_FST_FLUSH;
869 record->flush_group = ip->flush_group;
870 hammer_ref(&record->lock);
871 if (record->type == HAMMER_MEM_RECORD_ADD)
872 return(1);
873
874 /*
875 * The record is a delete-n-disk. It does not contribute
876 * to our visibility. We can still flush it.
877 */
878 return(0);
879 } else {
880 /*
881 * We couldn't resolve the dependancies, request that the
882 * inode be flushed when the dependancies can be resolved.
883 */
884 ip->flags |= HAMMER_INODE_REFLUSH;
885 return(-1);
7f7c1f84 886 }
c0ade690
MD
887}
888
889/*
1f07f686 890 * This is the core routine placing an inode into the FST_FLUSH state.
c0ade690 891 */
b84de5af 892static void
1f07f686 893hammer_flush_inode_core(hammer_inode_t ip, int flags)
b84de5af 894{
1f07f686 895 int go_count;
1f07f686 896
4e17f465
MD
897 /*
898 * Set flush state and prevent the flusher from cycling into
899 * the next flush group. Do not place the ip on the list yet.
900 * Inodes not in the idle state get an extra reference.
901 */
1f07f686
MD
902 KKASSERT(ip->flush_state != HAMMER_FST_FLUSH);
903 if (ip->flush_state == HAMMER_FST_IDLE)
904 hammer_ref(&ip->lock);
905 ip->flush_state = HAMMER_FST_FLUSH;
906 ip->flush_group = ip->hmp->flusher_next;
4e17f465 907 ++ip->hmp->flusher_lock;
b84de5af 908
ec4e8497 909 /*
1f07f686
MD
910 * Figure out how many in-memory records we can actually flush
911 * (not including inode meta-data, buffers, etc).
ec4e8497 912 */
1f07f686
MD
913 if (flags & HAMMER_FLUSH_RECURSION) {
914 go_count = 1;
915 } else {
916 go_count = RB_SCAN(hammer_rec_rb_tree, &ip->rec_tree, NULL,
917 hammer_setup_child_callback, NULL);
918 }
b84de5af
MD
919
920 /*
1f07f686
MD
921 * This is a more involved test that includes go_count. If we
922 * can't flush, flag the inode and return. If go_count is 0 we
923 * were are unable to flush any records in our rec_tree and
924 * must ignore the XDIRTY flag.
b84de5af 925 */
1f07f686
MD
926 if (go_count == 0) {
927 if ((ip->flags & HAMMER_INODE_MODMASK_NOXDIRTY) == 0) {
928 ip->flags |= HAMMER_INODE_REFLUSH;
929 ip->flush_state = HAMMER_FST_SETUP;
4e17f465
MD
930 if (flags & HAMMER_FLUSH_SIGNAL) {
931 ip->flags |= HAMMER_INODE_RESIGNAL;
932 hammer_flusher_async(ip->hmp);
933 }
934 if (--ip->hmp->flusher_lock == 0)
935 wakeup(&ip->hmp->flusher_lock);
1f07f686
MD
936 return;
937 }
938 }
b84de5af 939
4e17f465 940#if 0
b84de5af 941 /*
4e17f465
MD
942 * XXX - don't sync the buffer cache on the frontend, the backend
943 * will do it and we do not want to prematurely activate the backend.
1f07f686 944 *
4e17f465
MD
945 * Sync the buffer cache if the caller wants to flush now, otherwise
946 * don't (any write bios will wake up the flusher).
b84de5af 947 */
4e17f465
MD
948 if ((flags & HAMMER_FLUSH_RECURSION) == 0 &&
949 (flags & HAMMER_FLUSH_SIGNAL)) {
1f07f686
MD
950 if (ip->vp != NULL)
951 error = vfsync(ip->vp, MNT_NOWAIT, 1, NULL, NULL);
952 else
953 error = 0;
954 }
955
956 /*
957 * Any further strategy calls will go into the inode's alternative
958 * bioq.
959 */
960 ip->flags |= HAMMER_INODE_WRITE_ALT;
4e17f465 961#endif
b84de5af
MD
962
963 /*
964 * Snapshot the state of the inode for the backend flusher.
965 *
966 * The truncation must be retained in the frontend until after
967 * we've actually performed the record deletion.
1f07f686
MD
968 *
969 * NOTE: The DELETING flag is a mod flag, but it is also sticky,
970 * and stays in ip->flags. Once set, it stays set until the
971 * inode is destroyed.
b84de5af
MD
972 */
973 ip->sync_flags = (ip->flags & HAMMER_INODE_MODMASK);
974 ip->sync_trunc_off = ip->trunc_off;
975 ip->sync_ino_rec = ip->ino_rec;
976 ip->sync_ino_data = ip->ino_data;
4e17f465 977 ip->flags &= ~HAMMER_INODE_MODMASK | HAMMER_INODE_TRUNCATED;
b84de5af
MD
978
979 /*
4e17f465 980 * The flusher list inherits our inode and reference.
b84de5af 981 */
1f07f686 982 TAILQ_INSERT_TAIL(&ip->hmp->flush_list, ip, flush_entry);
4e17f465
MD
983 if (--ip->hmp->flusher_lock == 0)
984 wakeup(&ip->hmp->flusher_lock);
1f07f686
MD
985
986 if (flags & HAMMER_FLUSH_SIGNAL)
987 hammer_flusher_async(ip->hmp);
b84de5af
MD
988}
989
ec4e8497 990/*
1f07f686
MD
991 * Callback for scan of ip->rec_tree. Try to include each record in our
992 * flush. ip->flush_group has been set but the inode has not yet been
993 * moved into a flushing state.
994 *
995 * If we get stuck on a record we have to set HAMMER_INODE_REFLUSH on
996 * both inodes.
997 *
998 * We return 1 for any record placed or found in FST_FLUSH, which prevents
999 * the caller from shortcutting the flush.
ec4e8497 1000 */
c0ade690 1001static int
1f07f686 1002hammer_setup_child_callback(hammer_record_t rec, void *data)
b84de5af 1003{
1f07f686
MD
1004 hammer_inode_t target_ip;
1005 hammer_inode_t ip;
1006 int r;
1007
1008 /*
1009 * If the record has been deleted by the backend (it's being held
1010 * by the frontend in a race), just ignore it.
1011 */
1012 if (rec->flags & HAMMER_RECF_DELETED_BE)
ec4e8497 1013 return(0);
1f07f686
MD
1014
1015 /*
1016 * If the record is in an idle state it has no dependancies and
1017 * can be flushed.
1018 */
1019 ip = rec->ip;
1020 r = 0;
1021
1022 switch(rec->flush_state) {
1023 case HAMMER_FST_IDLE:
1024 /*
1025 * Record has no setup dependancy, we can flush it.
1026 */
1027 KKASSERT(rec->target_ip == NULL);
1028 rec->flush_state = HAMMER_FST_FLUSH;
1029 rec->flush_group = ip->flush_group;
b84de5af 1030 hammer_ref(&rec->lock);
1f07f686
MD
1031 r = 1;
1032 break;
1033 case HAMMER_FST_SETUP:
1034 /*
1035 * Record has a setup dependancy. Try to include the
1036 * target ip in the flush.
1037 *
1038 * We have to be careful here, if we do not do the right
1039 * thing we can lose track of dirty inodes and the system
1040 * will lockup trying to allocate buffers.
1041 */
1042 target_ip = rec->target_ip;
1043 KKASSERT(target_ip != NULL);
1044 KKASSERT(target_ip->flush_state != HAMMER_FST_IDLE);
1045 if (target_ip->flush_state == HAMMER_FST_FLUSH) {
1046 /*
1047 * If the target IP is already flushing in our group
1048 * we are golden, otherwise make sure the target
1049 * reflushes.
1050 */
1051 if (target_ip->flush_group == ip->flush_group) {
1052 rec->flush_state = HAMMER_FST_FLUSH;
1053 rec->flush_group = ip->flush_group;
1054 hammer_ref(&rec->lock);
1055 r = 1;
1056 } else {
1057 target_ip->flags |= HAMMER_INODE_REFLUSH;
1058 }
1059 } else if (rec->type == HAMMER_MEM_RECORD_ADD) {
1060 /*
1061 * If the target IP is not flushing we can force
1062 * it to flush, even if it is unable to write out
1063 * any of its own records we have at least one in
1064 * hand that we CAN deal with.
1065 */
1066 rec->flush_state = HAMMER_FST_FLUSH;
1067 rec->flush_group = ip->flush_group;
1068 hammer_ref(&rec->lock);
1069 hammer_flush_inode_core(target_ip,
1070 HAMMER_FLUSH_RECURSION);
1071 r = 1;
1072 } else {
1073 /*
1074 * XXX this needs help. We have a delete-on-disk
1075 * which could disconnect the target. If the target
1076 * has its own dependancies they really need to
1077 * be flushed.
1078 *
1079 * XXX
1080 */
1081 rec->flush_state = HAMMER_FST_FLUSH;
1082 rec->flush_group = ip->flush_group;
1083 hammer_ref(&rec->lock);
1084 hammer_flush_inode_core(target_ip,
1085 HAMMER_FLUSH_RECURSION);
1086 r = 1;
1087 }
1088 break;
1089 case HAMMER_FST_FLUSH:
1090 /*
1091 * Record already associated with a flush group. It had
1092 * better be ours.
1093 */
1094 KKASSERT(rec->flush_group == ip->flush_group);
1095 r = 1;
1096 break;
b84de5af 1097 }
1f07f686 1098 return(r);
b84de5af
MD
1099}
1100
b84de5af
MD
1101/*
1102 * Wait for a previously queued flush to complete
1103 */
1104void
1105hammer_wait_inode(hammer_inode_t ip)
1106{
1107 while (ip->flush_state == HAMMER_FST_FLUSH) {
1108 ip->flags |= HAMMER_INODE_FLUSHW;
1109 tsleep(&ip->flags, 0, "hmrwin", 0);
1110 }
1111}
1112
1113/*
1114 * Called by the backend code when a flush has been completed.
1115 * The inode has already been removed from the flush list.
1116 *
1117 * A pipelined flush can occur, in which case we must re-enter the
1118 * inode on the list and re-copy its fields.
1119 */
1120void
1121hammer_flush_inode_done(hammer_inode_t ip)
1122{
1955afa7 1123 struct bio *bio;
1f07f686 1124 int dorel = 0;
1955afa7 1125
b84de5af
MD
1126 KKASSERT(ip->flush_state == HAMMER_FST_FLUSH);
1127
1f07f686
MD
1128 /*
1129 * Allow BIOs to queue to the inode's primary bioq again.
1130 */
1131 ip->flags &= ~HAMMER_INODE_WRITE_ALT;
1132
1133 /*
1134 * Merge left-over flags back into the frontend and fix the state.
1135 */
b84de5af 1136 ip->flags |= ip->sync_flags;
1f07f686
MD
1137
1138 /*
1139 * The backend may have adjusted nlinks, so if the adjusted nlinks
1140 * does not match the fronttend set the frontend's RDIRTY flag again.
1141 */
1142 if (ip->ino_rec.ino_nlinks != ip->sync_ino_rec.ino_nlinks)
1143 ip->flags |= HAMMER_INODE_RDIRTY;
b84de5af 1144
b84de5af 1145 /*
1955afa7
MD
1146 * Reflush any BIOs that wound up in the alt list. Our inode will
1147 * also wind up at the end of the flusher's list.
b84de5af
MD
1148 */
1149 while ((bio = TAILQ_FIRST(&ip->bio_alt_list)) != NULL) {
1150 TAILQ_REMOVE(&ip->bio_alt_list, bio, bio_act);
1151 TAILQ_INSERT_TAIL(&ip->bio_list, bio, bio_act);
4e17f465
MD
1152 }
1153 /*
1154 * Fix up the dirty buffer status.
1155 */
1156 if (TAILQ_FIRST(&ip->bio_list) ||
1157 (ip->vp && RB_ROOT(&ip->vp->v_rbdirty_tree))) {
1f07f686 1158 ip->flags |= HAMMER_INODE_BUFS;
1f07f686
MD
1159 }
1160
1161 /*
1162 * Re-set the XDIRTY flag if some of the inode's in-memory records
1163 * could not be flushed.
1164 */
4e17f465 1165 if (RB_ROOT(&ip->rec_tree))
f90dde4c 1166 ip->flags |= HAMMER_INODE_XDIRTY;
4e17f465
MD
1167
1168 /*
1169 * Do not lose track of inodes which no longer have vnode
1170 * assocations, otherwise they may never get flushed again.
1171 */
1172 if ((ip->flags & HAMMER_INODE_MODMASK) && ip->vp == NULL)
b84de5af 1173 ip->flags |= HAMMER_INODE_REFLUSH;
4e17f465
MD
1174
1175 /*
1176 * Adjust flush_state. The target state (idle or setup) shouldn't
1177 * be terribly important since we will reflush if we really need
1178 * to do anything. XXX
1179 */
1180 if (TAILQ_EMPTY(&ip->target_list) && RB_EMPTY(&ip->rec_tree)) {
1181 ip->flush_state = HAMMER_FST_IDLE;
1182 dorel = 1;
1183 } else {
1184 ip->flush_state = HAMMER_FST_SETUP;
b84de5af 1185 }
b84de5af
MD
1186
1187 /*
1188 * If the frontend made more changes and requested another flush,
4e17f465 1189 * then try to get it running.
b84de5af
MD
1190 */
1191 if (ip->flags & HAMMER_INODE_REFLUSH) {
1192 ip->flags &= ~HAMMER_INODE_REFLUSH;
4e17f465
MD
1193 if (ip->flags & HAMMER_INODE_RESIGNAL) {
1194 ip->flags &= ~HAMMER_INODE_RESIGNAL;
1195 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
1196 } else {
1197 hammer_flush_inode(ip, 0);
0729c8c8 1198 }
4e17f465
MD
1199 }
1200
1201 /*
1202 * Finally, if the frontend is waiting for a flush to complete,
1203 * wake it up.
1204 */
1205 if (ip->flush_state != HAMMER_FST_FLUSH) {
b84de5af
MD
1206 if (ip->flags & HAMMER_INODE_FLUSHW) {
1207 ip->flags &= ~HAMMER_INODE_FLUSHW;
1208 wakeup(&ip->flags);
1209 }
1210 }
1f07f686
MD
1211 if (dorel)
1212 hammer_rel_inode(ip, 0);
b84de5af
MD
1213}
1214
1215/*
1216 * Called from hammer_sync_inode() to synchronize in-memory records
1217 * to the media.
1218 */
1219static int
1220hammer_sync_record_callback(hammer_record_t record, void *data)
c0ade690 1221{
4e17f465
MD
1222 hammer_cursor_t cursor = data;
1223 hammer_transaction_t trans = cursor->trans;
c0ade690
MD
1224 int error;
1225
b84de5af 1226 /*
1f07f686 1227 * Skip records that do not belong to the current flush.
b84de5af 1228 */
1f07f686 1229 if (record->flush_state != HAMMER_FST_FLUSH)
b84de5af 1230 return(0);
1f07f686
MD
1231 KKASSERT((record->flags & HAMMER_RECF_DELETED_BE) == 0);
1232#if 1
1233 if (record->flush_group != record->ip->flush_group) {
1234 kprintf("sync_record %p ip %p bad flush group %d %d\n", record, record->ip, record->flush_group ,record->ip->flush_group);
1235 Debugger("blah2");
1236 return(0);
1237 }
1238#endif
1239 KKASSERT(record->flush_group == record->ip->flush_group);
d36ec43b
MD
1240
1241 /*
1242 * Interlock the record using the BE flag. Once BE is set the
1243 * frontend cannot change the state of FE.
1244 *
1245 * NOTE: If FE is set prior to us setting BE we still sync the
1246 * record out, but the flush completion code converts it to
1247 * a delete-on-disk record instead of destroying it.
1248 */
4e17f465 1249 KKASSERT((record->flags & HAMMER_RECF_INTERLOCK_BE) == 0);
d36ec43b
MD
1250 record->flags |= HAMMER_RECF_INTERLOCK_BE;
1251
1252 /*
1253 * If DELETED_FE is set we may have already sent dependant pieces
1254 * to the disk and we must flush the record as if it hadn't been
1255 * deleted. This creates a bit of a mess because we have to
1f07f686 1256 * have ip_sync_record convert the record to MEM_RECORD_DEL before
d36ec43b
MD
1257 * it inserts the B-Tree record. Otherwise the media sync might
1258 * be visible to the frontend.
1259 */
1f07f686
MD
1260 if (record->flags & HAMMER_RECF_DELETED_FE) {
1261 KKASSERT(record->type == HAMMER_MEM_RECORD_ADD);
1262 record->flags |= HAMMER_RECF_CONVERT_DELETE;
1263 }
b84de5af
MD
1264
1265 /*
1266 * Assign the create_tid for new records. Deletions already
1267 * have the record's entire key properly set up.
1268 */
1f07f686 1269 if (record->type != HAMMER_MEM_RECORD_DEL)
b84de5af 1270 record->rec.inode.base.base.create_tid = trans->tid;
4e17f465
MD
1271 for (;;) {
1272 error = hammer_ip_sync_record_cursor(cursor, record);
1273 if (error != EDEADLK)
1274 break;
1275 hammer_done_cursor(cursor);
1276 error = hammer_init_cursor(trans, cursor, &record->ip->cache[0],
1277 record->ip);
1278 if (error)
1279 break;
1280 }
1281 record->flags &= ~HAMMER_RECF_CONVERT_DELETE;
c0ade690
MD
1282
1283 if (error) {
b3deaf57
MD
1284 error = -error;
1285 if (error != -ENOSPC) {
b84de5af
MD
1286 kprintf("hammer_sync_record_callback: sync failed rec "
1287 "%p, error %d\n", record, error);
1288 Debugger("sync failed rec");
b3deaf57 1289 }
c0ade690 1290 }
d36ec43b 1291 hammer_flush_record_done(record, error);
b3deaf57 1292 return(error);
c0ade690
MD
1293}
1294
1295/*
1296 * XXX error handling
1297 */
1298int
1f07f686 1299hammer_sync_inode(hammer_inode_t ip)
c0ade690
MD
1300{
1301 struct hammer_transaction trans;
4e17f465 1302 struct hammer_cursor cursor;
059819e3 1303 struct bio *bio;
1f07f686
MD
1304 hammer_record_t depend;
1305 hammer_record_t next;
ec4e8497 1306 int error, tmp_error;
1f07f686 1307 u_int64_t nlinks;
c0ade690 1308
1f07f686 1309 if ((ip->sync_flags & HAMMER_INODE_MODMASK) == 0)
d113fda1 1310 return(0);
d113fda1 1311
b84de5af 1312 hammer_start_transaction_fls(&trans, ip->hmp);
4e17f465
MD
1313 error = hammer_init_cursor(&trans, &cursor, &ip->cache[0], ip);
1314 if (error)
1315 goto done;
c0ade690 1316
ec4e8497 1317 /*
1f07f686
MD
1318 * Any directory records referencing this inode which are not in
1319 * our current flush group must adjust our nlink count for the
1320 * purposes of synchronization to disk.
1321 *
1322 * Records which are in our flush group can be unlinked from our
1323 * inode now, allowing the inode to be physically deleted.
ec4e8497 1324 */
1f07f686
MD
1325 nlinks = ip->ino_rec.ino_nlinks;
1326 next = TAILQ_FIRST(&ip->target_list);
1327 while ((depend = next) != NULL) {
1328 next = TAILQ_NEXT(depend, target_entry);
1329 if (depend->flush_state == HAMMER_FST_FLUSH &&
1330 depend->flush_group == ip->hmp->flusher_act) {
1331 TAILQ_REMOVE(&ip->target_list, depend, target_entry);
1332 depend->target_ip = NULL;
1333 /* no need to signal target_ip, it is us */
1334 } else if ((depend->flags & HAMMER_RECF_DELETED_FE) == 0) {
1335 switch(depend->type) {
1336 case HAMMER_MEM_RECORD_ADD:
1337 --nlinks;
1338 break;
1339 case HAMMER_MEM_RECORD_DEL:
1340 ++nlinks;
1341 break;
1342 }
ec4e8497 1343 }
ec4e8497
MD
1344 }
1345
c0ade690 1346 /*
1f07f686 1347 * Set dirty if we had to modify the link count.
c0ade690 1348 */
1f07f686
MD
1349 if (ip->sync_ino_rec.ino_nlinks != nlinks) {
1350 KKASSERT((int64_t)nlinks >= 0);
1351 ip->sync_ino_rec.ino_nlinks = nlinks;
1352 ip->sync_flags |= HAMMER_INODE_RDIRTY;
1353 }
b84de5af 1354
4e17f465
MD
1355 /*
1356 * Queue up any pending dirty buffers then set a flag to cause
1357 * any further BIOs to go to the alternative queue.
1358 */
1359 if (ip->vp)
1360 error = vfsync(ip->vp, MNT_NOWAIT, 1, NULL, NULL);
1361 ip->flags |= HAMMER_INODE_WRITE_ALT;
1362
1363 /*
1364 * The buffer cache may contain dirty buffers beyond the inode
1365 * state we copied from the frontend to the backend. Because
1366 * we are syncing our buffer cache on the backend, resync
1367 * the truncation point and the file size so we don't wipe out
1368 * any data.
1369 *
1370 * Syncing the buffer cache on the frontend has serious problems
1371 * because it prevents us from passively queueing dirty inodes
1372 * to the backend (the BIO's could stall indefinitely).
1373 */
1374 if (ip->flags & HAMMER_INODE_TRUNCATED) {
1375 ip->sync_trunc_off = ip->trunc_off;
1376 ip->sync_flags |= HAMMER_INODE_TRUNCATED;
1377 }
1378 if (ip->sync_ino_rec.ino_size != ip->ino_rec.ino_size) {
1379 ip->sync_ino_rec.ino_size = ip->ino_rec.ino_size;
1380 ip->sync_flags |= HAMMER_INODE_RDIRTY;
1381 }
1382
1f07f686 1383 /*
869e8f55
MD
1384 * If there is a trunction queued destroy any data past the (aligned)
1385 * truncation point. Userland will have dealt with the buffer
1386 * containing the truncation point for us.
1387 *
1388 * We don't flush pending frontend data buffers until after we've
1389 * dealth with the truncation.
1f07f686 1390 *
869e8f55 1391 * Don't bother if the inode is or has been deleted.
1f07f686 1392 */
869e8f55 1393 if (ip->sync_flags & HAMMER_INODE_TRUNCATED) {
b84de5af
MD
1394 /*
1395 * Interlock trunc_off. The VOP front-end may continue to
1396 * make adjustments to it while we are blocked.
1397 */
1398 off_t trunc_off;
1399 off_t aligned_trunc_off;
c0ade690 1400
b84de5af
MD
1401 trunc_off = ip->sync_trunc_off;
1402 aligned_trunc_off = (trunc_off + HAMMER_BUFMASK) &
1403 ~HAMMER_BUFMASK64;
1404
1405 /*
1406 * Delete any whole blocks on-media. The front-end has
1407 * already cleaned out any partial block and made it
1408 * pending. The front-end may have updated trunc_off
1409 * while we were blocked so do not just unconditionally
1410 * set it to the maximum offset.
1411 */
4e17f465 1412 error = hammer_ip_delete_range(&cursor, ip,
b84de5af
MD
1413 aligned_trunc_off,
1414 0x7FFFFFFFFFFFFFFFLL);
1415 if (error)
1416 Debugger("hammer_ip_delete_range errored");
1417 ip->sync_flags &= ~HAMMER_INODE_TRUNCATED;
1418 if (ip->trunc_off >= trunc_off) {
1419 ip->trunc_off = 0x7FFFFFFFFFFFFFFFLL;
1420 ip->flags &= ~HAMMER_INODE_TRUNCATED;
1421 }
1f07f686
MD
1422 } else {
1423 error = 0;
f3b0f382
MD
1424 }
1425
1f07f686
MD
1426 /*
1427 * Now sync related records. These will typically be directory
1428 * entries or delete-on-disk records.
869e8f55
MD
1429 *
1430 * Not all records will be flushed, but clear XDIRTY anyway. We
1431 * will set it again in the frontend hammer_flush_inode_done()
1432 * if records remain.
1f07f686
MD
1433 */
1434 if (error == 0) {
1435 tmp_error = RB_SCAN(hammer_rec_rb_tree, &ip->rec_tree, NULL,
4e17f465 1436 hammer_sync_record_callback, &cursor);
1f07f686
MD
1437 if (tmp_error < 0)
1438 tmp_error = -error;
1439 if (tmp_error)
1440 error = tmp_error;
869e8f55
MD
1441 if (error == 0)
1442 ip->sync_flags &= ~HAMMER_INODE_XDIRTY;
1f07f686
MD
1443 }
1444
1445 /*
869e8f55
MD
1446 * If we are deleting the inode the frontend had better not have
1447 * any active references on elements making up the inode.
1f07f686 1448 */
869e8f55
MD
1449 if (error == 0 && ip->sync_ino_rec.ino_nlinks == 0 &&
1450 RB_EMPTY(&ip->rec_tree) &&
1451 (ip->sync_flags & HAMMER_INODE_DELETING) &&
1452 (ip->flags & HAMMER_INODE_DELETED) == 0) {
1453 int count1 = 0;
1f07f686 1454
869e8f55
MD
1455 kprintf("Y");
1456 ip->flags |= HAMMER_INODE_DELETED;
4e17f465 1457 error = hammer_ip_delete_range_all(&cursor, ip, &count1);
869e8f55
MD
1458 if (error == 0) {
1459 ip->sync_flags &= ~HAMMER_INODE_DELETING;
1460 ip->sync_flags &= ~HAMMER_INODE_TRUNCATED;
1461 KKASSERT(RB_EMPTY(&ip->rec_tree));
1f07f686 1462
869e8f55
MD
1463 /*
1464 * Set delete_tid in both the frontend and backend
1465 * copy of the inode record. The DELETED flag handles
1466 * this, do not set RDIRTY.
1467 */
1468 ip->ino_rec.base.base.delete_tid = trans.tid;
1469 ip->sync_ino_rec.base.base.delete_tid = trans.tid;
1f07f686 1470
869e8f55
MD
1471 /*
1472 * Adjust the inode count in the volume header
1473 */
1474 hammer_modify_volume(&trans, trans.rootvol, NULL, 0);
1475 --ip->hmp->rootvol->ondisk->vol0_stat_inodes;
1476 hammer_modify_volume_done(trans.rootvol);
1477 } else {
1478 ip->flags &= ~HAMMER_INODE_DELETED;
1479 Debugger("hammer_ip_delete_range_all errored");
1480 }
1f07f686 1481 }
b84de5af 1482
059819e3 1483 /*
869e8f55
MD
1484 * Flush any queued BIOs. These will just biodone() the IO's if
1485 * the inode has been deleted.
059819e3
MD
1486 */
1487 while ((bio = TAILQ_FIRST(&ip->bio_list)) != NULL) {
1488 TAILQ_REMOVE(&ip->bio_list, bio, bio_act);
4e17f465 1489 tmp_error = hammer_dowrite(&cursor, ip, bio);
ec4e8497
MD
1490 if (tmp_error)
1491 error = tmp_error;
059819e3 1492 }
b84de5af 1493 ip->sync_flags &= ~HAMMER_INODE_BUFS;
c0ade690 1494
b84de5af
MD
1495 if (error)
1496 Debugger("RB_SCAN errored");
c0ade690
MD
1497
1498 /*
1499 * Now update the inode's on-disk inode-data and/or on-disk record.
b84de5af 1500 * DELETED and ONDISK are managed only in ip->flags.
c0ade690 1501 */
b84de5af 1502 switch(ip->flags & (HAMMER_INODE_DELETED | HAMMER_INODE_ONDISK)) {
c0ade690
MD
1503 case HAMMER_INODE_DELETED|HAMMER_INODE_ONDISK:
1504 /*
1505 * If deleted and on-disk, don't set any additional flags.
1506 * the delete flag takes care of things.
869e8f55
MD
1507 *
1508 * Clear flags which may have been set by the frontend.
c0ade690 1509 */
869e8f55
MD
1510 ip->sync_flags &= ~(HAMMER_INODE_RDIRTY|HAMMER_INODE_DDIRTY|
1511 HAMMER_INODE_XDIRTY|HAMMER_INODE_ITIMES|
1512 HAMMER_INODE_DELETING);
c0ade690
MD
1513 break;
1514 case HAMMER_INODE_DELETED:
1515 /*
1516 * Take care of the case where a deleted inode was never
1517 * flushed to the disk in the first place.
869e8f55
MD
1518 *
1519 * Clear flags which may have been set by the frontend.
c0ade690 1520 */
b84de5af 1521 ip->sync_flags &= ~(HAMMER_INODE_RDIRTY|HAMMER_INODE_DDIRTY|
869e8f55
MD
1522 HAMMER_INODE_XDIRTY|HAMMER_INODE_ITIMES|
1523 HAMMER_INODE_DELETING);
d26d0ae9 1524 while (RB_ROOT(&ip->rec_tree)) {
d36ec43b
MD
1525 hammer_record_t record = RB_ROOT(&ip->rec_tree);
1526 hammer_ref(&record->lock);
1527 KKASSERT(record->lock.refs == 1);
1528 record->flags |= HAMMER_RECF_DELETED_FE;
1529 record->flags |= HAMMER_RECF_DELETED_BE;
d36ec43b 1530 hammer_rel_mem_record(record);
d26d0ae9 1531 }
c0ade690
MD
1532 break;
1533 case HAMMER_INODE_ONDISK:
1534 /*
1535 * If already on-disk, do not set any additional flags.
1536 */
1537 break;
1538 default:
1539 /*
1540 * If not on-disk and not deleted, set both dirty flags
b84de5af
MD
1541 * to force an initial record to be written. Also set
1542 * the create_tid for the inode.
1543 *
1544 * Set create_tid in both the frontend and backend
1545 * copy of the inode record.
c0ade690 1546 */
b84de5af
MD
1547 ip->ino_rec.base.base.create_tid = trans.tid;
1548 ip->sync_ino_rec.base.base.create_tid = trans.tid;
1549 ip->sync_flags |= HAMMER_INODE_RDIRTY | HAMMER_INODE_DDIRTY;
c0ade690
MD
1550 break;
1551 }
1552
1553 /*
d113fda1
MD
1554 * If RDIRTY or DDIRTY is set, write out a new record. If the inode
1555 * is already on-disk the old record is marked as deleted.
1556 *
1557 * If DELETED is set hammer_update_inode() will delete the existing
1558 * record without writing out a new one.
1559 *
1560 * If *ONLY* the ITIMES flag is set we can update the record in-place.
c0ade690 1561 */
b84de5af 1562 if (ip->flags & HAMMER_INODE_DELETED) {
4e17f465 1563 error = hammer_update_inode(&cursor, ip);
b84de5af
MD
1564 } else
1565 if ((ip->sync_flags & (HAMMER_INODE_RDIRTY | HAMMER_INODE_DDIRTY |
1566 HAMMER_INODE_ITIMES)) == HAMMER_INODE_ITIMES) {
4e17f465 1567 error = hammer_update_itimes(&cursor, ip);
d113fda1 1568 } else
b84de5af
MD
1569 if (ip->sync_flags & (HAMMER_INODE_RDIRTY | HAMMER_INODE_DDIRTY |
1570 HAMMER_INODE_ITIMES)) {
4e17f465 1571 error = hammer_update_inode(&cursor, ip);
c0ade690 1572 }
b84de5af
MD
1573 if (error)
1574 Debugger("hammer_update_itimes/inode errored");
4e17f465 1575done:
b84de5af
MD
1576 /*
1577 * Save the TID we used to sync the inode with to make sure we
1578 * do not improperly reuse it.
1579 */
4e17f465 1580 hammer_done_cursor(&cursor);
b84de5af 1581 hammer_done_transaction(&trans);
c0ade690 1582 return(error);
8cd0a023
MD
1583}
1584
1f07f686
MD
1585/*
1586 * This routine is called when the OS is no longer actively referencing
1587 * the inode (but might still be keeping it cached), or when releasing
1588 * the last reference to an inode.
1589 *
1590 * At this point if the inode's nlinks count is zero we want to destroy
1591 * it, which may mean destroying it on-media too.
1592 */
3bf2d80a 1593void
1f07f686
MD
1594hammer_inode_unloadable_check(hammer_inode_t ip)
1595{
1596 /*
1597 * If the inode is on-media and the link count is 0 we MUST delete
869e8f55 1598 * it on-media. DELETING is a mod flag, DELETED is a state flag.
1f07f686
MD
1599 */
1600 if (ip->ino_rec.ino_nlinks == 0 &&
869e8f55
MD
1601 (ip->flags & (HAMMER_INODE_DELETING|HAMMER_INODE_DELETED)) == 0) {
1602 if (ip->vp) {
1603 vtruncbuf(ip->vp, 0, HAMMER_BUFSIZE);
1604 vnode_pager_setsize(ip->vp, 0);
1605 }
1f07f686 1606 ip->flags |= HAMMER_INODE_DELETING;
869e8f55
MD
1607 ip->flags |= HAMMER_INODE_TRUNCATED;
1608 ip->trunc_off = 0;
1f07f686 1609 }
1f07f686
MD
1610}
1611
3bf2d80a
MD
1612/*
1613 * Re-test an inode when a dependancy had gone away to see if we
1614 * can chain flush it.
1615 */
1f07f686
MD
1616void
1617hammer_test_inode(hammer_inode_t ip)
1618{
1619 if (ip->flags & HAMMER_INODE_REFLUSH) {
1620 ip->flags &= ~HAMMER_INODE_REFLUSH;
1621 hammer_ref(&ip->lock);
3bf2d80a
MD
1622 if (ip->flags & HAMMER_INODE_RESIGNAL) {
1623 ip->flags &= ~HAMMER_INODE_RESIGNAL;
1624 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
1625 } else {
1626 hammer_flush_inode(ip, 0);
1627 }
1f07f686
MD
1628 hammer_rel_inode(ip, 0);
1629 }
1630}
1631