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