Annotation of src/sys/ufs/ufs/ufs_inode.c, Revision 1.46
1.46 ! dbj 1: /* $NetBSD: ufs_inode.c,v 1.45 2004/10/08 18:43:50 dbj Exp $ */
1.3 cgd 2:
1.1 mycroft 3: /*
4: * Copyright (c) 1991, 1993
5: * The Regents of the University of California. All rights reserved.
6: * (c) UNIX System Laboratories, Inc.
7: * All or some portions of this file are derived from material licensed
8: * to the University of California by American Telephone and Telegraph
9: * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10: * the permission of UNIX System Laboratories, Inc.
11: *
12: * Redistribution and use in source and binary forms, with or without
13: * modification, are permitted provided that the following conditions
14: * are met:
15: * 1. Redistributions of source code must retain the above copyright
16: * notice, this list of conditions and the following disclaimer.
17: * 2. Redistributions in binary form must reproduce the above copyright
18: * notice, this list of conditions and the following disclaimer in the
19: * documentation and/or other materials provided with the distribution.
1.40 agc 20: * 3. Neither the name of the University nor the names of its contributors
1.1 mycroft 21: * may be used to endorse or promote products derived from this software
22: * without specific prior written permission.
23: *
24: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34: * SUCH DAMAGE.
35: *
1.10 fvdl 36: * @(#)ufs_inode.c 8.9 (Berkeley) 5/14/95
1.1 mycroft 37: */
1.28 lukem 38:
39: #include <sys/cdefs.h>
1.46 ! dbj 40: __KERNEL_RCSID(0, "$NetBSD: ufs_inode.c,v 1.45 2004/10/08 18:43:50 dbj Exp $");
1.11 scottr 41:
1.46 ! dbj 42: #if defined(_KERNEL_OPT)
1.11 scottr 43: #include "opt_quota.h"
1.46 ! dbj 44: #endif
1.1 mycroft 45:
46: #include <sys/param.h>
47: #include <sys/systm.h>
48: #include <sys/proc.h>
49: #include <sys/vnode.h>
50: #include <sys/mount.h>
51: #include <sys/kernel.h>
1.6 christos 52: #include <sys/namei.h>
1.1 mycroft 53:
54: #include <ufs/ufs/quota.h>
55: #include <ufs/ufs/inode.h>
56: #include <ufs/ufs/ufsmount.h>
57: #include <ufs/ufs/ufs_extern.h>
58:
1.16 chs 59: #include <uvm/uvm.h>
60:
1.19 tsutsui 61: extern int prtactive;
62:
1.1 mycroft 63: /*
64: * Last reference to an inode. If necessary, write or delete it.
65: */
66: int
1.6 christos 67: ufs_inactive(v)
68: void *v;
69: {
1.1 mycroft 70: struct vop_inactive_args /* {
71: struct vnode *a_vp;
1.39 fvdl 72: struct proc *a_p;
1.6 christos 73: } */ *ap = v;
1.10 fvdl 74: struct vnode *vp = ap->a_vp;
75: struct inode *ip = VTOI(vp);
1.41 hannken 76: struct mount *mp;
1.39 fvdl 77: struct proc *p = ap->a_p;
1.36 fvdl 78: mode_t mode;
79: int error = 0;
1.1 mycroft 80:
81: if (prtactive && vp->v_usecount != 0)
1.16 chs 82: vprint("ufs_inactive: pushing active", vp);
1.1 mycroft 83:
1.10 fvdl 84: /*
85: * Ignore inodes related to stale file handles.
86: */
1.36 fvdl 87: if (ip->i_mode == 0)
1.10 fvdl 88: goto out;
1.31 fvdl 89: if (ip->i_ffs_effnlink == 0 && DOINGSOFTDEP(vp))
90: softdep_releasefile(ip);
1.1 mycroft 91:
1.36 fvdl 92: if (ip->i_nlink <= 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
1.42 hannken 93: vn_start_write(vp, &mp, V_WAIT | V_LOWER);
1.1 mycroft 94: #ifdef QUOTA
95: if (!getinoquota(ip))
96: (void)chkiq(ip, -1, NOCRED, 0);
97: #endif
1.36 fvdl 98: if (ip->i_size != 0) {
1.39 fvdl 99: error = VOP_TRUNCATE(vp, (off_t)0, 0, NOCRED, p);
1.21 chs 100: }
1.32 fvdl 101: /*
102: * Setting the mode to zero needs to wait for the inode
103: * to be written just as does a change to the link count.
104: * So, rather than creating a new entry point to do the
105: * same thing, we just use softdep_change_linkcnt().
106: */
1.37 kristerw 107: DIP_ASSIGN(ip, rdev, 0);
1.36 fvdl 108: mode = ip->i_mode;
109: ip->i_mode = 0;
1.37 kristerw 110: DIP_ASSIGN(ip, mode, 0);
1.1 mycroft 111: ip->i_flag |= IN_CHANGE | IN_UPDATE;
1.32 fvdl 112: if (DOINGSOFTDEP(vp))
113: softdep_change_linkcnt(ip);
1.1 mycroft 114: VOP_VFREE(vp, ip->i_number, mode);
1.41 hannken 115: vn_finished_write(mp, V_LOWER);
1.1 mycroft 116: }
1.10 fvdl 117:
1.44 mycroft 118: if (ip->i_flag & (IN_CHANGE | IN_UPDATE | IN_MODIFIED)) {
1.42 hannken 119: vn_start_write(vp, &mp, V_WAIT | V_LOWER);
1.44 mycroft 120: VOP_UPDATE(vp, NULL, NULL, 0);
1.41 hannken 121: vn_finished_write(mp, V_LOWER);
122: }
1.10 fvdl 123: out:
124: VOP_UNLOCK(vp, 0);
1.1 mycroft 125: /*
126: * If we are done with the inode, reclaim it
127: * so that it can be reused immediately.
128: */
1.16 chs 129:
1.36 fvdl 130: if (ip->i_mode == 0)
1.39 fvdl 131: vrecycle(vp, NULL, p);
1.1 mycroft 132: return (error);
133: }
134:
135: /*
136: * Reclaim an inode so that it can be used for other purposes.
137: */
138: int
1.39 fvdl 139: ufs_reclaim(vp, p)
1.10 fvdl 140: struct vnode *vp;
1.39 fvdl 141: struct proc *p;
1.1 mycroft 142: {
1.44 mycroft 143: struct inode *ip = VTOI(vp);
144: struct mount *mp;
1.1 mycroft 145:
146: if (prtactive && vp->v_usecount != 0)
147: vprint("ufs_reclaim: pushing active", vp);
1.45 dbj 148:
149: vn_start_write(vp, &mp, V_WAIT | V_LOWER);
150: VOP_UPDATE(vp, NULL, NULL, UPDATE_CLOSE);
151: vn_finished_write(mp, V_LOWER);
152:
1.1 mycroft 153: /*
154: * Remove the inode from its hash chain.
155: */
156: ufs_ihashrem(ip);
157: /*
158: * Purge old data structures associated with the inode.
159: */
160: cache_purge(vp);
161: if (ip->i_devvp) {
162: vrele(ip->i_devvp);
163: ip->i_devvp = 0;
164: }
165: #ifdef QUOTA
1.6 christos 166: {
167: int i;
168: for (i = 0; i < MAXQUOTAS; i++) {
169: if (ip->i_dquot[i] != NODQUOT) {
170: dqrele(vp, ip->i_dquot[i]);
171: ip->i_dquot[i] = NODQUOT;
172: }
1.1 mycroft 173: }
174: }
175: #endif
176: return (0);
1.16 chs 177: }
178:
179: /*
180: * allocate a range of blocks in a file.
181: * after this function returns, any page entirely contained within the range
182: * will map to invalid data and thus must be overwritten before it is made
183: * accessible to others.
184: */
185:
186: int
187: ufs_balloc_range(vp, off, len, cred, flags)
188: struct vnode *vp;
189: off_t off, len;
190: struct ucred *cred;
191: int flags;
192: {
1.35 perseant 193: off_t oldeof, neweof, oldeob, oldeop, neweob, pagestart;
1.16 chs 194: struct uvm_object *uobj;
1.25 chs 195: struct genfs_node *gp = VTOG(vp);
1.29 chs 196: int i, delta, error, npages;
1.16 chs 197: int bshift = vp->v_mount->mnt_fs_bshift;
198: int bsize = 1 << bshift;
1.22 chs 199: int ppb = MAX(bsize >> PAGE_SHIFT, 1);
1.29 chs 200: struct vm_page *pgs[ppb];
1.16 chs 201: UVMHIST_FUNC("ufs_balloc_range"); UVMHIST_CALLED(ubchist);
202: UVMHIST_LOG(ubchist, "vp %p off 0x%x len 0x%x u_size 0x%x",
1.25 chs 203: vp, off, len, vp->v_size);
1.16 chs 204:
1.25 chs 205: oldeof = vp->v_size;
1.35 perseant 206: GOP_SIZE(vp, oldeof, &oldeop, GOP_SIZE_WRITE);
207: GOP_SIZE(vp, oldeof, &oldeob, GOP_SIZE_READ);
208:
209: /*
210: * If we need to map pages in the former last block,
211: * do so now.
212: */
213: if (oldeob != oldeop) {
214: uvm_vnp_zerorange(vp, oldeop, oldeob - oldeop);
215: }
1.16 chs 216:
1.25 chs 217: neweof = MAX(vp->v_size, off + len);
1.34 perseant 218: GOP_SIZE(vp, neweof, &neweob, GOP_SIZE_WRITE);
1.16 chs 219:
220: error = 0;
1.25 chs 221: uobj = &vp->v_uobj;
1.29 chs 222: pgs[0] = NULL;
1.16 chs 223:
224: /*
1.29 chs 225: * read or create pages covering the range of the allocation and
226: * keep them locked until the new block is allocated, so there
227: * will be no window where the old contents of the new block are
228: * visible to racing threads.
1.16 chs 229: */
230:
1.29 chs 231: pagestart = trunc_page(off) & ~(bsize - 1);
232: npages = MIN(ppb, (round_page(neweob) - pagestart) >> PAGE_SHIFT);
233: memset(pgs, 0, npages * sizeof(struct vm_page *));
234: simple_lock(&uobj->vmobjlock);
235: error = VOP_GETPAGES(vp, pagestart, pgs, &npages, 0,
236: VM_PROT_READ, 0, PGO_SYNCIO|PGO_PASTEOF);
237: if (error) {
1.33 chs 238: return error;
1.16 chs 239: }
1.29 chs 240: simple_lock(&uobj->vmobjlock);
241: uvm_lock_pageq();
242: for (i = 0; i < npages; i++) {
243: UVMHIST_LOG(ubchist, "got pgs[%d] %p", i, pgs[i],0,0);
244: KASSERT((pgs[i]->flags & PG_RELEASED) == 0);
245: pgs[i]->flags &= ~PG_CLEAN;
246: uvm_pageactivate(pgs[i]);
1.16 chs 247: }
1.29 chs 248: uvm_unlock_pageq();
249: simple_unlock(&uobj->vmobjlock);
1.16 chs 250:
251: /*
252: * adjust off to be block-aligned.
253: */
254:
255: delta = off & (bsize - 1);
256: off -= delta;
257: len += delta;
258:
259: /*
260: * now allocate the range.
261: */
262:
1.25 chs 263: lockmgr(&gp->g_glock, LK_EXCLUSIVE, NULL);
264: error = GOP_ALLOC(vp, off, len, flags, cred);
265: lockmgr(&gp->g_glock, LK_RELEASE, NULL);
1.16 chs 266:
267: /*
1.24 chs 268: * clear PG_RDONLY on any pages we are holding
269: * (since they now have backing store) and unbusy them.
1.16 chs 270: */
271:
1.18 chs 272: simple_lock(&uobj->vmobjlock);
1.29 chs 273: for (i = 0; i < npages; i++) {
274: pgs[i]->flags &= ~PG_RDONLY;
275: if (error) {
276: pgs[i]->flags |= PG_RELEASED;
1.24 chs 277: }
1.16 chs 278: }
1.30 chs 279: if (error) {
280: uvm_lock_pageq();
281: uvm_page_unbusy(pgs, npages);
282: uvm_unlock_pageq();
283: } else {
284: uvm_page_unbusy(pgs, npages);
285: }
1.16 chs 286: simple_unlock(&uobj->vmobjlock);
287: return error;
1.1 mycroft 288: }
CVSweb <webmaster@jp.NetBSD.org>