/* * VOP_RECLAIM * * Reclaim should make an effort to returning errors other than EBUSY. */ static int emufs_reclaim(struct vnode *v) { struct emufs_vnode *ev = v->vn_data; struct emufs_fs *ef = v->vn_fs->fs_data; unsigned ix, i, num; int result; /* * Need both of these locks, e_lock to protect the device * and vfs_biglock to protect the fs-related material. */ vfs_biglock_acquire(); lock_acquire(ef->ef_emu->e_lock); if (ev->ev_v.vn_refcount != 1) { lock_release(ef->ef_emu->e_lock); vfs_biglock_release(); return EBUSY; } /* emu_close retries on I/O error */ result = emu_close(ev->ev_emu, ev->ev_handle); if (result) { lock_release(ef->ef_emu->e_lock); vfs_biglock_release(); return result; } num = vnodearray_num(ef->ef_vnodes); ix = num; for (i=0; i<num; i++) { struct vnode *vx; vx = vnodearray_get(ef->ef_vnodes, i); if (vx == v) { ix = i; break; } } if (ix == num) { panic("emu%d: reclaim vnode %u not in vnode pool\n", ef->ef_emu->e_unit, ev->ev_handle); } vnodearray_remove(ef->ef_vnodes, ix); VOP_CLEANUP(&ev->ev_v); lock_release(ef->ef_emu->e_lock); vfs_biglock_release(); kfree(ev); return 0; }
/* * Called when the vnode refcount (in-memory usage count) hits zero. * * This function should try to avoid returning errors other than EBUSY. */ static int sfs_reclaim(struct vnode *v) { struct sfs_vnode *sv = v->vn_data; struct sfs_fs *sfs = v->vn_fs->fs_data; unsigned ix, i, num; int result; vfs_biglock_acquire(); /* * Make sure someone else hasn't picked up the vnode since the * decision was made to reclaim it. (You must also synchronize * this with sfs_loadvnode.) */ if (v->vn_refcount != 1) { /* consume the reference VOP_DECREF gave us */ KASSERT(v->vn_refcount>1); v->vn_refcount--; vfs_biglock_release(); return EBUSY; } /* If there are no on-disk references to the file either, erase it. */ if (sv->sv_i.sfi_linkcount==0) { result = VOP_TRUNCATE(&sv->sv_v, 0); if (result) { vfs_biglock_release(); return result; } } /* Sync the inode to disk */ result = sfs_sync_inode(sv); if (result) { vfs_biglock_release(); return result; } /* If there are no on-disk references, discard the inode */ if (sv->sv_i.sfi_linkcount==0) { sfs_bfree(sfs, sv->sv_ino); } /* Remove the vnode structure from the table in the struct sfs_fs. */ num = vnodearray_num(sfs->sfs_vnodes); ix = num; for (i=0; i<num; i++) { struct vnode *v2 = vnodearray_get(sfs->sfs_vnodes, i); struct sfs_vnode *sv2 = v2->vn_data; if (sv2 == sv) { ix = i; break; } } if (ix == num) { panic("sfs: reclaim vnode %u not in vnode pool\n", sv->sv_ino); } vnodearray_remove(sfs->sfs_vnodes, ix); VOP_CLEANUP(&sv->sv_v); vfs_biglock_release(); /* Release the storage for the vnode structure itself. */ kfree(sv); /* Done */ return 0; }
/* * Function to load a inode into memory as a vnode, or dig up one * that's already resident. */ static int sfs_loadvnode(struct sfs_fs *sfs, uint32_t ino, int forcetype, struct sfs_vnode **ret) { struct vnode *v; struct sfs_vnode *sv; const struct vnode_ops *ops = NULL; unsigned i, num; int result; /* Look in the vnodes table */ num = vnodearray_num(sfs->sfs_vnodes); /* Linear search. Is this too slow? You decide. */ for (i=0; i<num; i++) { v = vnodearray_get(sfs->sfs_vnodes, i); sv = v->vn_data; /* Every inode in memory must be in an allocated block */ if (!sfs_bused(sfs, sv->sv_ino)) { panic("sfs: Found inode %u in unallocated block\n", sv->sv_ino); } if (sv->sv_ino==ino) { /* Found */ /* May only be set when creating new objects */ KASSERT(forcetype==SFS_TYPE_INVAL); VOP_INCREF(&sv->sv_v); *ret = sv; return 0; } } /* Didn't have it loaded; load it */ sv = kmalloc(sizeof(struct sfs_vnode)); if (sv==NULL) { return ENOMEM; } /* Must be in an allocated block */ if (!sfs_bused(sfs, ino)) { panic("sfs: Tried to load inode %u from unallocated block\n", ino); } /* Read the block the inode is in */ result = sfs_rblock(sfs, &sv->sv_i, ino); if (result) { kfree(sv); return result; } /* Not dirty yet */ sv->sv_dirty = false; /* * FORCETYPE is set if we're creating a new file, because the * block on disk will have been zeroed out and thus the type * recorded there will be SFS_TYPE_INVAL. */ if (forcetype != SFS_TYPE_INVAL) { KASSERT(sv->sv_i.sfi_type == SFS_TYPE_INVAL); sv->sv_i.sfi_type = forcetype; sv->sv_dirty = true; } /* * Choose the function table based on the object type. */ switch (sv->sv_i.sfi_type) { case SFS_TYPE_FILE: ops = &sfs_fileops; break; case SFS_TYPE_DIR: ops = &sfs_dirops; break; default: panic("sfs: loadvnode: Invalid inode type " "(inode %u, type %u)\n", ino, sv->sv_i.sfi_type); } /* Call the common vnode initializer */ result = VOP_INIT(&sv->sv_v, ops, &sfs->sfs_absfs, sv); if (result) { kfree(sv); return result; } /* Set the other fields in our vnode structure */ sv->sv_ino = ino; /* Add it to our table */ result = vnodearray_add(sfs->sfs_vnodes, &sv->sv_v, NULL); if (result) { VOP_CLEANUP(&sv->sv_v); kfree(sv); return result; } /* Hand it back */ *ret = sv; return 0; }
/* * Function to load a vnode into memory. */ static int emufs_loadvnode(struct emufs_fs *ef, uint32_t handle, int isdir, struct emufs_vnode **ret) { struct vnode *v; struct emufs_vnode *ev; unsigned i, num; int result; vfs_biglock_acquire(); lock_acquire(ef->ef_emu->e_lock); num = vnodearray_num(ef->ef_vnodes); for (i=0; i<num; i++) { v = vnodearray_get(ef->ef_vnodes, i); ev = v->vn_data; if (ev->ev_handle == handle) { /* Found */ VOP_INCREF(&ev->ev_v); lock_release(ef->ef_emu->e_lock); vfs_biglock_release(); *ret = ev; return 0; } } /* Didn't have one; create it */ ev = kmalloc(sizeof(struct emufs_vnode)); if (ev==NULL) { lock_release(ef->ef_emu->e_lock); return ENOMEM; } ev->ev_emu = ef->ef_emu; ev->ev_handle = handle; result = VOP_INIT(&ev->ev_v, isdir ? &emufs_dirops : &emufs_fileops, &ef->ef_fs, ev); if (result) { lock_release(ef->ef_emu->e_lock); vfs_biglock_release(); kfree(ev); return result; } result = vnodearray_add(ef->ef_vnodes, &ev->ev_v, NULL); if (result) { /* note: VOP_CLEANUP undoes VOP_INIT - it does not kfree */ VOP_CLEANUP(&ev->ev_v); lock_release(ef->ef_emu->e_lock); vfs_biglock_release(); kfree(ev); return result; } lock_release(ef->ef_emu->e_lock); vfs_biglock_release(); *ret = ev; return 0; }