Fix panics which can occur when killing a threaded program. lwp_exit()
[dragonfly.git] / sys / vfs / hammer / hammer_inode.c
CommitLineData
427e5fc6
MD
1/*
2 * Copyright (c) 2007 The DragonFly Project. All rights reserved.
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 *
059819e3 34 * $DragonFly: src/sys/vfs/hammer/hammer_inode.c,v 1.34 2008/04/22 19:00:15 dillon Exp $
427e5fc6
MD
35 */
36
37#include "hammer.h"
38#include <sys/buf.h>
39#include <sys/buf2.h>
40
d113fda1
MD
41/*
42 * The kernel is not actively referencing this vnode but is still holding
43 * it cached.
44 */
427e5fc6
MD
45int
46hammer_vop_inactive(struct vop_inactive_args *ap)
47{
66325755 48 struct hammer_inode *ip = VTOI(ap->a_vp);
27ea2398 49
c0ade690
MD
50 /*
51 * Degenerate case
52 */
53 if (ip == NULL) {
66325755 54 vrecycle(ap->a_vp);
c0ade690
MD
55 return(0);
56 }
57
58 /*
59 * If the inode no longer has any references we recover its
60 * in-memory resources immediately.
61 */
d113fda1
MD
62 if (ip->ino_rec.ino_nlinks == 0)
63 vrecycle(ap->a_vp);
427e5fc6
MD
64 return(0);
65}
66
d113fda1
MD
67/*
68 * Release the vnode association. This is typically (but not always)
69 * the last reference on the inode and will flush the inode to the
70 * buffer cache.
71 *
72 * XXX Currently our sync code only runs through inodes with vnode
73 * associations, so we depend on hammer_rel_inode() to sync any inode
74 * record data to the block device prior to losing the association.
75 * Otherwise transactions that the user expected to be distinct by
76 * doing a manual sync may be merged.
77 */
427e5fc6
MD
78int
79hammer_vop_reclaim(struct vop_reclaim_args *ap)
80{
427e5fc6
MD
81 struct hammer_inode *ip;
82 struct vnode *vp;
83
84 vp = ap->a_vp;
c0ade690 85
a89aec1b
MD
86 if ((ip = vp->v_data) != NULL) {
87 vp->v_data = NULL;
88 ip->vp = NULL;
d113fda1 89 hammer_rel_inode(ip, 0);
a89aec1b 90 }
427e5fc6
MD
91 return(0);
92}
93
66325755
MD
94/*
95 * Return a locked vnode for the specified inode. The inode must be
96 * referenced but NOT LOCKED on entry and will remain referenced on
97 * return.
98 */
99int
100hammer_get_vnode(struct hammer_inode *ip, int lktype, struct vnode **vpp)
101{
102 struct vnode *vp;
103 int error = 0;
104
105 for (;;) {
106 if ((vp = ip->vp) == NULL) {
107 error = getnewvnode(VT_HAMMER, ip->hmp->mp, vpp, 0, 0);
108 if (error)
109 break;
8cd0a023
MD
110 hammer_lock_ex(&ip->lock);
111 if (ip->vp != NULL) {
112 hammer_unlock(&ip->lock);
113 vp->v_type = VBAD;
114 vx_put(vp);
115 continue;
66325755 116 }
8cd0a023
MD
117 hammer_ref(&ip->lock);
118 vp = *vpp;
119 ip->vp = vp;
120 vp->v_type = hammer_get_vnode_type(
121 ip->ino_rec.base.base.obj_type);
7a04d74f
MD
122
123 switch(ip->ino_rec.base.base.obj_type) {
124 case HAMMER_OBJTYPE_CDEV:
125 case HAMMER_OBJTYPE_BDEV:
126 vp->v_ops = &ip->hmp->mp->mnt_vn_spec_ops;
127 addaliasu(vp, ip->ino_data.rmajor,
128 ip->ino_data.rminor);
129 break;
130 case HAMMER_OBJTYPE_FIFO:
131 vp->v_ops = &ip->hmp->mp->mnt_vn_fifo_ops;
132 break;
133 default:
134 break;
135 }
42c7d26b
MD
136
137 /*
138 * Only mark as the root vnode if the ip is not
139 * historical, otherwise the VFS cache will get
140 * confused. The other half of the special handling
141 * is in hammer_vop_nlookupdotdot().
142 */
143 if (ip->obj_id == HAMMER_OBJID_ROOT &&
144 ip->obj_asof == ip->hmp->asof) {
7a04d74f 145 vp->v_flag |= VROOT;
42c7d26b 146 }
7a04d74f 147
8cd0a023
MD
148 vp->v_data = (void *)ip;
149 /* vnode locked by getnewvnode() */
150 /* make related vnode dirty if inode dirty? */
151 hammer_unlock(&ip->lock);
a89aec1b
MD
152 if (vp->v_type == VREG)
153 vinitvmio(vp, ip->ino_rec.ino_size);
8cd0a023
MD
154 break;
155 }
156
157 /*
158 * loop if the vget fails (aka races), or if the vp
159 * no longer matches ip->vp.
160 */
161 if (vget(vp, LK_EXCLUSIVE) == 0) {
162 if (vp == ip->vp)
163 break;
164 vput(vp);
66325755
MD
165 }
166 }
a89aec1b 167 *vpp = vp;
66325755
MD
168 return(error);
169}
170
171/*
8cd0a023
MD
172 * Acquire a HAMMER inode. The returned inode is not locked. These functions
173 * do not attach or detach the related vnode (use hammer_get_vnode() for
174 * that).
d113fda1
MD
175 *
176 * The flags argument is only applied for newly created inodes, and only
177 * certain flags are inherited.
66325755
MD
178 */
179struct hammer_inode *
36f82b23 180hammer_get_inode(hammer_transaction_t trans, struct hammer_node **cache,
61aeeb33 181 u_int64_t obj_id, hammer_tid_t asof, int flags, int *errorp)
66325755 182{
36f82b23 183 hammer_mount_t hmp = trans->hmp;
427e5fc6 184 struct hammer_inode_info iinfo;
8cd0a023 185 struct hammer_cursor cursor;
427e5fc6 186 struct hammer_inode *ip;
427e5fc6
MD
187
188 /*
189 * Determine if we already have an inode cached. If we do then
190 * we are golden.
191 */
66325755 192 iinfo.obj_id = obj_id;
7f7c1f84 193 iinfo.obj_asof = asof;
427e5fc6
MD
194loop:
195 ip = hammer_ino_rb_tree_RB_LOOKUP_INFO(&hmp->rb_inos_root, &iinfo);
196 if (ip) {
8cd0a023 197 hammer_ref(&ip->lock);
66325755
MD
198 *errorp = 0;
199 return(ip);
427e5fc6
MD
200 }
201
427e5fc6 202 ip = kmalloc(sizeof(*ip), M_HAMMER, M_WAITOK|M_ZERO);
b3deaf57 203 ++hammer_count_inodes;
66325755 204 ip->obj_id = obj_id;
27ea2398 205 ip->obj_asof = iinfo.obj_asof;
66325755 206 ip->hmp = hmp;
d113fda1
MD
207 ip->flags = flags & HAMMER_INODE_RO;
208 if (hmp->ronly)
209 ip->flags |= HAMMER_INODE_RO;
8cd0a023 210 RB_INIT(&ip->rec_tree);
059819e3 211 TAILQ_INIT(&ip->bio_list);
427e5fc6
MD
212
213 /*
8cd0a023 214 * Locate the on-disk inode.
427e5fc6 215 */
6a37e7e4 216retry:
36f82b23 217 hammer_init_cursor(trans, &cursor, cache);
8cd0a023
MD
218 cursor.key_beg.obj_id = ip->obj_id;
219 cursor.key_beg.key = 0;
d5530d22 220 cursor.key_beg.create_tid = 0;
8cd0a023
MD
221 cursor.key_beg.delete_tid = 0;
222 cursor.key_beg.rec_type = HAMMER_RECTYPE_INODE;
223 cursor.key_beg.obj_type = 0;
d5530d22
MD
224 cursor.asof = iinfo.obj_asof;
225 cursor.flags = HAMMER_CURSOR_GET_RECORD | HAMMER_CURSOR_GET_DATA |
226 HAMMER_CURSOR_ASOF;
8cd0a023
MD
227
228 *errorp = hammer_btree_lookup(&cursor);
6a37e7e4
MD
229 if (*errorp == EDEADLK) {
230 hammer_done_cursor(&cursor);
231 goto retry;
232 }
427e5fc6
MD
233
234 /*
235 * On success the B-Tree lookup will hold the appropriate
236 * buffer cache buffers and provide a pointer to the requested
d113fda1
MD
237 * information. Copy the information to the in-memory inode
238 * and cache the B-Tree node to improve future operations.
427e5fc6 239 */
66325755 240 if (*errorp == 0) {
8cd0a023 241 ip->ino_rec = cursor.record->inode;
40043e7f 242 ip->ino_data = cursor.data->inode;
855942b6 243 ip->sync_tid = ip->ino_rec.base.base.create_tid;
61aeeb33
MD
244 hammer_cache_node(cursor.node, &ip->cache[0]);
245 if (cache)
246 hammer_cache_node(cursor.node, cache);
427e5fc6 247 }
427e5fc6
MD
248
249 /*
250 * On success load the inode's record and data and insert the
251 * inode into the B-Tree. It is possible to race another lookup
252 * insertion of the same inode so deal with that condition too.
b3deaf57
MD
253 *
254 * The cursor's locked node interlocks against others creating and
255 * destroying ip while we were blocked.
427e5fc6 256 */
66325755 257 if (*errorp == 0) {
8cd0a023 258 hammer_ref(&ip->lock);
427e5fc6 259 if (RB_INSERT(hammer_ino_rb_tree, &hmp->rb_inos_root, ip)) {
61aeeb33
MD
260 hammer_uncache_node(&ip->cache[0]);
261 hammer_uncache_node(&ip->cache[1]);
8cd0a023 262 hammer_unref(&ip->lock);
b3deaf57 263 --hammer_count_inodes;
427e5fc6 264 kfree(ip, M_HAMMER);
b3deaf57 265 hammer_done_cursor(&cursor);
427e5fc6
MD
266 goto loop;
267 }
c0ade690 268 ip->flags |= HAMMER_INODE_ONDISK;
427e5fc6 269 } else {
b3deaf57 270 --hammer_count_inodes;
66325755
MD
271 kfree(ip, M_HAMMER);
272 ip = NULL;
427e5fc6 273 }
b3deaf57 274 hammer_done_cursor(&cursor);
66325755
MD
275 return (ip);
276}
277
8cd0a023
MD
278/*
279 * Create a new filesystem object, returning the inode in *ipp. The
280 * returned inode will be referenced but not locked.
281 *
282 * The inode is created in-memory and will be delay-synchronized to the
283 * disk.
284 */
285int
a89aec1b
MD
286hammer_create_inode(hammer_transaction_t trans, struct vattr *vap,
287 struct ucred *cred, hammer_inode_t dip,
8cd0a023 288 struct hammer_inode **ipp)
66325755 289{
a89aec1b
MD
290 hammer_mount_t hmp;
291 hammer_inode_t ip;
6b4f890b 292 uid_t xuid;
66325755 293
8cd0a023
MD
294 hmp = trans->hmp;
295 ip = kmalloc(sizeof(*ip), M_HAMMER, M_WAITOK|M_ZERO);
b3deaf57 296 ++hammer_count_inodes;
a89aec1b 297 ip->obj_id = hammer_alloc_tid(trans);
8cd0a023 298 KKASSERT(ip->obj_id != 0);
7f7c1f84 299 ip->obj_asof = hmp->asof;
8cd0a023
MD
300 ip->hmp = hmp;
301 ip->flags = HAMMER_INODE_DDIRTY | HAMMER_INODE_RDIRTY |
42c7d26b 302 HAMMER_INODE_ITIMES | HAMMER_INODE_TIDLOCKED;
8cd0a023
MD
303 ip->last_tid = trans->tid;
304
305 RB_INIT(&ip->rec_tree);
059819e3 306 TAILQ_INIT(&ip->bio_list);
8cd0a023
MD
307
308 ip->ino_rec.ino_atime = trans->tid;
309 ip->ino_rec.ino_mtime = trans->tid;
310 ip->ino_rec.ino_size = 0;
311 ip->ino_rec.ino_nlinks = 0;
312 /* XXX */
fe7678ee 313 ip->ino_rec.base.base.btype = HAMMER_BTREE_TYPE_RECORD;
8cd0a023
MD
314 ip->ino_rec.base.base.obj_id = ip->obj_id;
315 ip->ino_rec.base.base.key = 0;
316 ip->ino_rec.base.base.create_tid = trans->tid;
317 ip->ino_rec.base.base.delete_tid = 0;
318 ip->ino_rec.base.base.rec_type = HAMMER_RECTYPE_INODE;
319 ip->ino_rec.base.base.obj_type = hammer_get_obj_type(vap->va_type);
320
321 ip->ino_data.version = HAMMER_INODE_DATA_VERSION;
322 ip->ino_data.mode = vap->va_mode;
323 ip->ino_data.ctime = trans->tid;
324 ip->ino_data.parent_obj_id = (dip) ? dip->ino_rec.base.base.obj_id : 0;
6b4f890b 325
7a04d74f
MD
326 switch(ip->ino_rec.base.base.obj_type) {
327 case HAMMER_OBJTYPE_CDEV:
328 case HAMMER_OBJTYPE_BDEV:
329 ip->ino_data.rmajor = vap->va_rmajor;
330 ip->ino_data.rminor = vap->va_rminor;
331 break;
332 default:
333 break;
334 }
335
6b4f890b
MD
336 /*
337 * Calculate default uid/gid and overwrite with information from
338 * the vap.
339 */
340 xuid = hammer_to_unix_xid(&dip->ino_data.uid);
341 ip->ino_data.gid = dip->ino_data.gid;
342 xuid = vop_helper_create_uid(hmp->mp, dip->ino_data.mode, xuid, cred,
343 &vap->va_mode);
344 ip->ino_data.mode = vap->va_mode;
345
8cd0a023
MD
346 if (vap->va_vaflags & VA_UID_UUID_VALID)
347 ip->ino_data.uid = vap->va_uid_uuid;
6b4f890b
MD
348 else if (vap->va_uid != (uid_t)VNOVAL)
349 hammer_guid_to_uuid(&ip->ino_data.uid, xuid);
8cd0a023
MD
350 if (vap->va_vaflags & VA_GID_UUID_VALID)
351 ip->ino_data.gid = vap->va_gid_uuid;
6b4f890b 352 else if (vap->va_gid != (gid_t)VNOVAL)
8cd0a023
MD
353 hammer_guid_to_uuid(&ip->ino_data.gid, vap->va_gid);
354
355 hammer_ref(&ip->lock);
356 if (RB_INSERT(hammer_ino_rb_tree, &hmp->rb_inos_root, ip)) {
357 hammer_unref(&ip->lock);
a89aec1b 358 panic("hammer_create_inode: duplicate obj_id %llx", ip->obj_id);
8cd0a023
MD
359 }
360 *ipp = ip;
361 return(0);
66325755
MD
362}
363
d113fda1
MD
364/*
365 * Called by hammer_sync_inode().
366 */
367static int
36f82b23 368hammer_update_inode(hammer_transaction_t trans, hammer_inode_t ip)
c0ade690
MD
369{
370 struct hammer_cursor cursor;
371 hammer_record_t record;
372 int error;
373
374 /*
76376933 375 * Locate the record on-disk and mark it as deleted. Both the B-Tree
195c19a1
MD
376 * node and the record must be marked deleted. The record may or
377 * may not be physically deleted, depending on the retention policy.
76376933 378 *
195c19a1
MD
379 * If the inode has already been deleted on-disk we have nothing
380 * to do.
c0ade690
MD
381 *
382 * XXX Update the inode record and data in-place if the retention
383 * policy allows it.
384 */
d26d0ae9 385retry:
c0ade690
MD
386 error = 0;
387
76376933
MD
388 if ((ip->flags & (HAMMER_INODE_ONDISK|HAMMER_INODE_DELONDISK)) ==
389 HAMMER_INODE_ONDISK) {
36f82b23 390 hammer_init_cursor(trans, &cursor, &ip->cache[0]);
c0ade690
MD
391 cursor.key_beg.obj_id = ip->obj_id;
392 cursor.key_beg.key = 0;
d5530d22 393 cursor.key_beg.create_tid = 0;
c0ade690
MD
394 cursor.key_beg.delete_tid = 0;
395 cursor.key_beg.rec_type = HAMMER_RECTYPE_INODE;
396 cursor.key_beg.obj_type = 0;
d5530d22
MD
397 cursor.asof = ip->obj_asof;
398 cursor.flags |= HAMMER_CURSOR_GET_RECORD | HAMMER_CURSOR_ASOF;
c0ade690
MD
399
400 error = hammer_btree_lookup(&cursor);
401
402 if (error == 0) {
855942b6 403 error = hammer_ip_delete_record(&cursor, trans->tid);
195c19a1
MD
404 if (error == 0)
405 ip->flags |= HAMMER_INODE_DELONDISK;
6a37e7e4 406 hammer_cache_node(cursor.node, &ip->cache[0]);
c0ade690 407 }
c0ade690 408 hammer_done_cursor(&cursor);
6a37e7e4
MD
409 if (error == EDEADLK)
410 goto retry;
c0ade690
MD
411 }
412
413 /*
414 * Write out a new record if the in-memory inode is not marked
fbc6e32a
MD
415 * as having been deleted. Update our inode statistics if this
416 * is the first application of the inode on-disk.
76376933
MD
417 *
418 * If the inode has been deleted permanently, HAMMER_INODE_DELONDISK
419 * will remain set and prevent further updates.
c0ade690
MD
420 */
421 if (error == 0 && (ip->flags & HAMMER_INODE_DELETED) == 0) {
40043e7f 422 record = hammer_alloc_mem_record(ip);
c0ade690 423 record->rec.inode = ip->ino_rec;
855942b6 424 record->rec.inode.base.base.create_tid = trans->tid;
c0ade690
MD
425 record->rec.inode.base.data_len = sizeof(ip->ino_data);
426 record->data = (void *)&ip->ino_data;
36f82b23 427 error = hammer_ip_sync_record(trans, record);
b3deaf57
MD
428 record->flags |= HAMMER_RECF_DELETED;
429 hammer_rel_mem_record(record);
d26d0ae9 430 if (error == 0) {
d113fda1
MD
431 ip->flags &= ~(HAMMER_INODE_RDIRTY |
432 HAMMER_INODE_DDIRTY |
433 HAMMER_INODE_DELONDISK |
434 HAMMER_INODE_ITIMES);
d26d0ae9 435 if ((ip->flags & HAMMER_INODE_ONDISK) == 0) {
36f82b23
MD
436 hammer_modify_volume(trans, ip->hmp->rootvol,
437 NULL, 0);
0b075555 438 ++ip->hmp->rootvol->ondisk->vol0_stat_inodes;
d26d0ae9
MD
439 ip->flags |= HAMMER_INODE_ONDISK;
440 }
42c7d26b
MD
441
442 /*
443 * Unlock the sync TID if it was locked, now that
444 * we have written it out to disk.
445 */
446 ip->flags &= ~HAMMER_INODE_TIDLOCKED;
855942b6 447 ip->sync_tid = trans->tid;
fbc6e32a 448 }
c0ade690
MD
449 }
450 return(error);
451}
452
a89aec1b 453/*
d113fda1
MD
454 * Update only the itimes fields. This is done no-historically. The
455 * record is updated in-place on the disk.
456 */
457static int
36f82b23 458hammer_update_itimes(hammer_transaction_t trans, hammer_inode_t ip)
d113fda1
MD
459{
460 struct hammer_cursor cursor;
461 struct hammer_inode_record *rec;
462 int error;
463
6a37e7e4 464retry:
d113fda1
MD
465 error = 0;
466 if ((ip->flags & (HAMMER_INODE_ONDISK|HAMMER_INODE_DELONDISK)) ==
467 HAMMER_INODE_ONDISK) {
36f82b23 468 hammer_init_cursor(trans, &cursor, &ip->cache[0]);
d113fda1
MD
469 cursor.key_beg.obj_id = ip->obj_id;
470 cursor.key_beg.key = 0;
d5530d22 471 cursor.key_beg.create_tid = 0;
d113fda1
MD
472 cursor.key_beg.delete_tid = 0;
473 cursor.key_beg.rec_type = HAMMER_RECTYPE_INODE;
474 cursor.key_beg.obj_type = 0;
d5530d22
MD
475 cursor.asof = ip->obj_asof;
476 cursor.flags |= HAMMER_CURSOR_GET_RECORD | HAMMER_CURSOR_ASOF;
d113fda1
MD
477
478 error = hammer_btree_lookup(&cursor);
d113fda1
MD
479 if (error == 0) {
480 rec = &cursor.record->inode;
36f82b23
MD
481 hammer_modify_buffer(cursor.trans, cursor.record_buffer,
482 NULL, 0);
d113fda1
MD
483 rec->ino_atime = ip->ino_rec.ino_atime;
484 rec->ino_mtime = ip->ino_rec.ino_mtime;
d113fda1
MD
485 ip->flags &= ~HAMMER_INODE_ITIMES;
486 /* XXX recalculate crc */
6a37e7e4 487 hammer_cache_node(cursor.node, &ip->cache[0]);
d113fda1 488 }
d113fda1 489 hammer_done_cursor(&cursor);
6a37e7e4
MD
490 if (error == EDEADLK)
491 goto retry;
d113fda1
MD
492 }
493 return(error);
494}
495
496/*
497 * Release a reference on an inode. If asked to flush the last release
498 * will flush the inode.
a89aec1b 499 */
66325755 500void
a89aec1b 501hammer_rel_inode(struct hammer_inode *ip, int flush)
66325755
MD
502{
503 hammer_unref(&ip->lock);
d113fda1 504 if (flush)
a89aec1b 505 ip->flags |= HAMMER_INODE_FLUSH;
d113fda1 506 if (ip->lock.refs == 0) {
7aa3b8a6 507 KKASSERT(ip->cursor_ip_refs == 0);
d113fda1
MD
508 if (ip->flags & HAMMER_INODE_FLUSH)
509 hammer_unload_inode(ip, (void *)MNT_WAIT);
510 else
511 hammer_unload_inode(ip, (void *)MNT_NOWAIT);
512 }
427e5fc6
MD
513}
514
27ea2398 515/*
8cd0a023
MD
516 * Unload and destroy the specified inode.
517 *
b33e2cc0 518 * (typically called via RB_SCAN)
27ea2398
MD
519 */
520int
d113fda1 521hammer_unload_inode(struct hammer_inode *ip, void *data)
27ea2398 522{
c0ade690
MD
523 int error;
524
a89aec1b
MD
525 KASSERT(ip->lock.refs == 0,
526 ("hammer_unload_inode: %d refs\n", ip->lock.refs));
8cd0a023
MD
527 KKASSERT(ip->vp == NULL);
528 hammer_ref(&ip->lock);
6b4f890b 529
d113fda1 530 error = hammer_sync_inode(ip, (int)data, 1);
c0ade690
MD
531 if (error)
532 kprintf("hammer_sync_inode failed error %d\n", error);
d113fda1
MD
533 if (ip->lock.refs == 1) {
534 KKASSERT(RB_EMPTY(&ip->rec_tree));
059819e3 535 KKASSERT(TAILQ_EMPTY(&ip->bio_list));
d113fda1 536 RB_REMOVE(hammer_ino_rb_tree, &ip->hmp->rb_inos_root, ip);
8cd0a023 537
61aeeb33
MD
538 hammer_uncache_node(&ip->cache[0]);
539 hammer_uncache_node(&ip->cache[1]);
d113fda1
MD
540 --hammer_count_inodes;
541 kfree(ip, M_HAMMER);
542 } else {
543 hammer_unref(&ip->lock);
544 }
27ea2398
MD
545 return(0);
546}
547
427e5fc6 548/*
d113fda1
MD
549 * A transaction has modified an inode, requiring updates as specified by
550 * the passed flags.
7f7c1f84 551 *
d113fda1
MD
552 * HAMMER_INODE_RDIRTY: Inode record has been updated
553 * HAMMER_INODE_DDIRTY: Inode data has been updated
554 * HAMMER_INODE_DELETED: Inode record/data must be deleted
555 * HAMMER_INODE_ITIMES: mtime/atime has been updated
556 *
557 * last_tid is the TID to use to generate the correct TID when the inode
32c90105
MD
558 * is synced to disk. The first inode record laid out on disk must match
559 * the transaction id of the related directory entry so only update last_tid
560 * if that has already occured.
427e5fc6 561 */
66325755
MD
562void
563hammer_modify_inode(struct hammer_transaction *trans,
564 struct hammer_inode *ip, int flags)
427e5fc6 565{
d113fda1
MD
566 KKASSERT ((ip->flags & HAMMER_INODE_RO) == 0 ||
567 (HAMMER_INODE_RDIRTY|HAMMER_INODE_DDIRTY|
568 HAMMER_INODE_DELETED|HAMMER_INODE_ITIMES) == 0);
569
570 if (flags &
571 (HAMMER_INODE_RDIRTY|HAMMER_INODE_DDIRTY|HAMMER_INODE_DELETED)) {
572 if (hammer_debug_tid) {
573 kprintf("hammer_modify_inode: %016llx (%08x)\n",
574 trans->tid, (int)(trans->tid / 1000000000LL));
575 }
42c7d26b
MD
576
577 /*
578 * Update the inode sync transaction id unless it's locked
579 * due to some prior required synchroznization. Locking the
580 * tid in the new flags overrides this (used by rename).
581 */
582 if ((ip->flags & HAMMER_INODE_TIDLOCKED) == 0)
583 ip->last_tid = trans->tid;
584 else if (flags & HAMMER_INODE_TIDLOCKED)
32c90105 585 ip->last_tid = trans->tid;
7f7c1f84
MD
586 }
587 ip->flags |= flags;
c0ade690
MD
588}
589
590/*
591 * Sync any dirty buffers and records associated with an inode. The
592 * inode's last_tid field is used as the transaction id for the sync,
593 * overriding any intermediate TIDs that were used for records. Note
594 * that the dirty buffer cache buffers do not have any knowledge of
595 * the transaction id they were modified under.
596 */
597static int
36f82b23 598hammer_sync_inode_callback(hammer_record_t rec, void *data)
c0ade690 599{
36f82b23 600 hammer_transaction_t trans = data;
c0ade690
MD
601 int error;
602
d26d0ae9 603 hammer_ref(&rec->lock);
36f82b23 604 error = hammer_ip_sync_record(trans, rec);
b3deaf57 605 hammer_rel_mem_record(rec);
c0ade690
MD
606
607 if (error) {
b3deaf57
MD
608 error = -error;
609 if (error != -ENOSPC) {
610 kprintf("hammer_sync_inode_callback: sync failed rec "
611 "%p, error %d\n", rec, error);
612 }
c0ade690 613 }
b3deaf57 614 return(error);
c0ade690
MD
615}
616
617/*
618 * XXX error handling
619 */
620int
621hammer_sync_inode(hammer_inode_t ip, int waitfor, int handle_delete)
622{
623 struct hammer_transaction trans;
059819e3 624 struct bio *bio;
c0ade690 625 int error;
c0ade690 626
d113fda1
MD
627 if ((ip->flags & HAMMER_INODE_MODMASK) == 0) {
628 return(0);
629 }
630
c0ade690 631 hammer_lock_ex(&ip->lock);
d113fda1
MD
632
633 /*
855942b6
MD
634 * Use the transaction id of the last operation to sync. But we
635 * can't reuse a previous sync TID.
d113fda1 636 */
855942b6 637 if (ip->last_tid && ip->last_tid != ip->sync_tid)
d113fda1
MD
638 hammer_start_transaction_tid(&trans, ip->hmp, ip->last_tid);
639 else
640 hammer_start_transaction(&trans, ip->hmp);
c0ade690
MD
641
642 /*
643 * If the inode has been deleted (nlinks == 0), and the OS no longer
644 * has any references to it (handle_delete != 0), clean up in-memory
645 * data.
646 *
647 * NOTE: We do not set the RDIRTY flag when updating the delete_tid,
648 * setting HAMMER_INODE_DELETED takes care of it.
7f7c1f84
MD
649 *
650 * NOTE: Because we may sync records within this new transaction,
651 * force the inode update later on to use our transaction id or
652 * the delete_tid of the inode may be less then the create_tid of
653 * the inode update. XXX shouldn't happen but don't take the chance.
c0ade690 654 */
d113fda1
MD
655 if (ip->ino_rec.ino_nlinks == 0 && handle_delete &&
656 (ip->flags & HAMMER_INODE_GONE) == 0) {
657 ip->flags |= HAMMER_INODE_GONE;
c0ade690
MD
658 if (ip->vp)
659 vtruncbuf(ip->vp, 0, HAMMER_BUFSIZE);
7a04d74f 660 error = hammer_ip_delete_range_all(&trans, ip);
c0ade690
MD
661 KKASSERT(RB_EMPTY(&ip->rec_tree));
662 ip->ino_rec.base.base.delete_tid = trans.tid;
d113fda1 663 hammer_modify_inode(&trans, ip, HAMMER_INODE_DELETED);
36f82b23 664 hammer_modify_volume(&trans, ip->hmp->rootvol, NULL, 0);
0b075555 665 --ip->hmp->rootvol->ondisk->vol0_stat_inodes;
c0ade690
MD
666 }
667
668 /*
059819e3 669 * Sync the buffer cache. This will queue the BIOs
c0ade690 670 */
f3b0f382 671 if (ip->vp != NULL) {
059819e3 672 error = vfsync(ip->vp, MNT_NOWAIT, 1, NULL, NULL);
f3b0f382
MD
673 if (RB_ROOT(&ip->vp->v_rbdirty_tree) == NULL)
674 ip->flags &= ~HAMMER_INODE_BUFS;
675 } else {
c0ade690 676 error = 0;
f3b0f382
MD
677 }
678
059819e3
MD
679 /*
680 * Flush the queued BIOs
681 */
682 while ((bio = TAILQ_FIRST(&ip->bio_list)) != NULL) {
683 TAILQ_REMOVE(&ip->bio_list, bio, bio_act);
684 hammer_dowrite(&trans, ip, bio);
685 }
686
c0ade690
MD
687
688 /*
689 * Now sync related records
690 */
d26d0ae9
MD
691 for (;;) {
692 error = RB_SCAN(hammer_rec_rb_tree, &ip->rec_tree, NULL,
36f82b23 693 hammer_sync_inode_callback, &trans);
d26d0ae9
MD
694 KKASSERT(error <= 0);
695 if (error < 0)
696 error = -error;
d26d0ae9 697 break;
c0ade690 698 }
059819e3 699 if (RB_EMPTY(&ip->rec_tree) && TAILQ_EMPTY(&ip->bio_list))
d113fda1 700 ip->flags &= ~HAMMER_INODE_XDIRTY;
c0ade690
MD
701
702 /*
703 * Now update the inode's on-disk inode-data and/or on-disk record.
704 */
705 switch(ip->flags & (HAMMER_INODE_DELETED|HAMMER_INODE_ONDISK)) {
706 case HAMMER_INODE_DELETED|HAMMER_INODE_ONDISK:
707 /*
708 * If deleted and on-disk, don't set any additional flags.
709 * the delete flag takes care of things.
710 */
711 break;
712 case HAMMER_INODE_DELETED:
713 /*
714 * Take care of the case where a deleted inode was never
715 * flushed to the disk in the first place.
716 */
d113fda1
MD
717 ip->flags &= ~(HAMMER_INODE_RDIRTY|HAMMER_INODE_DDIRTY|
718 HAMMER_INODE_XDIRTY|HAMMER_INODE_ITIMES);
d26d0ae9
MD
719 while (RB_ROOT(&ip->rec_tree)) {
720 hammer_record_t rec = RB_ROOT(&ip->rec_tree);
721 hammer_ref(&rec->lock);
b3deaf57
MD
722 rec->flags |= HAMMER_RECF_DELETED;
723 hammer_rel_mem_record(rec);
d26d0ae9 724 }
c0ade690
MD
725 break;
726 case HAMMER_INODE_ONDISK:
727 /*
728 * If already on-disk, do not set any additional flags.
729 */
730 break;
731 default:
732 /*
733 * If not on-disk and not deleted, set both dirty flags
734 * to force an initial record to be written.
735 */
736 ip->flags |= HAMMER_INODE_RDIRTY | HAMMER_INODE_DDIRTY;
737 break;
738 }
739
740 /*
d113fda1
MD
741 * If RDIRTY or DDIRTY is set, write out a new record. If the inode
742 * is already on-disk the old record is marked as deleted.
743 *
744 * If DELETED is set hammer_update_inode() will delete the existing
745 * record without writing out a new one.
746 *
747 * If *ONLY* the ITIMES flag is set we can update the record in-place.
c0ade690 748 */
d113fda1
MD
749 if ((ip->flags & (HAMMER_INODE_RDIRTY | HAMMER_INODE_DDIRTY |
750 HAMMER_INODE_ITIMES | HAMMER_INODE_DELETED)) ==
751 HAMMER_INODE_ITIMES) {
36f82b23 752 error = hammer_update_itimes(&trans, ip);
d113fda1 753 } else
c0ade690 754 if (ip->flags & (HAMMER_INODE_RDIRTY | HAMMER_INODE_DDIRTY |
d113fda1 755 HAMMER_INODE_ITIMES | HAMMER_INODE_DELETED)) {
36f82b23 756 error = hammer_update_inode(&trans, ip);
c0ade690
MD
757 }
758 hammer_commit_transaction(&trans);
759 hammer_unlock(&ip->lock);
760 return(error);
8cd0a023
MD
761}
762