lfs_alloc.c revision 1.86.10.3 1 1.86.10.3 christos /* $NetBSD: lfs_alloc.c,v 1.86.10.3 2006/05/06 23:32:58 christos Exp $ */
2 1.2 cgd
3 1.17 perseant /*-
4 1.63 perseant * Copyright (c) 1999, 2000, 2001, 2002, 2003 The NetBSD Foundation, Inc.
5 1.17 perseant * All rights reserved.
6 1.17 perseant *
7 1.17 perseant * This code is derived from software contributed to The NetBSD Foundation
8 1.17 perseant * by Konrad E. Schroder <perseant (at) hhhh.org>.
9 1.17 perseant *
10 1.17 perseant * Redistribution and use in source and binary forms, with or without
11 1.17 perseant * modification, are permitted provided that the following conditions
12 1.17 perseant * are met:
13 1.17 perseant * 1. Redistributions of source code must retain the above copyright
14 1.17 perseant * notice, this list of conditions and the following disclaimer.
15 1.17 perseant * 2. Redistributions in binary form must reproduce the above copyright
16 1.17 perseant * notice, this list of conditions and the following disclaimer in the
17 1.17 perseant * documentation and/or other materials provided with the distribution.
18 1.17 perseant * 3. All advertising materials mentioning features or use of this software
19 1.17 perseant * must display the following acknowledgement:
20 1.64 perseant * This product includes software developed by the NetBSD
21 1.64 perseant * Foundation, Inc. and its contributors.
22 1.17 perseant * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.17 perseant * contributors may be used to endorse or promote products derived
24 1.17 perseant * from this software without specific prior written permission.
25 1.17 perseant *
26 1.17 perseant * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.17 perseant * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.17 perseant * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.17 perseant * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.17 perseant * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.17 perseant * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.17 perseant * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.17 perseant * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.17 perseant * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.17 perseant * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.17 perseant * POSSIBILITY OF SUCH DAMAGE.
37 1.17 perseant */
38 1.1 mycroft /*
39 1.1 mycroft * Copyright (c) 1991, 1993
40 1.1 mycroft * The Regents of the University of California. All rights reserved.
41 1.1 mycroft *
42 1.1 mycroft * Redistribution and use in source and binary forms, with or without
43 1.1 mycroft * modification, are permitted provided that the following conditions
44 1.1 mycroft * are met:
45 1.1 mycroft * 1. Redistributions of source code must retain the above copyright
46 1.1 mycroft * notice, this list of conditions and the following disclaimer.
47 1.1 mycroft * 2. Redistributions in binary form must reproduce the above copyright
48 1.1 mycroft * notice, this list of conditions and the following disclaimer in the
49 1.1 mycroft * documentation and/or other materials provided with the distribution.
50 1.71 agc * 3. Neither the name of the University nor the names of its contributors
51 1.1 mycroft * may be used to endorse or promote products derived from this software
52 1.1 mycroft * without specific prior written permission.
53 1.1 mycroft *
54 1.1 mycroft * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 1.1 mycroft * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 1.1 mycroft * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 1.1 mycroft * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 1.1 mycroft * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 1.1 mycroft * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 1.1 mycroft * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 1.1 mycroft * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 1.1 mycroft * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 1.1 mycroft * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 1.1 mycroft * SUCH DAMAGE.
65 1.1 mycroft *
66 1.2 cgd * @(#)lfs_alloc.c 8.4 (Berkeley) 1/4/94
67 1.1 mycroft */
68 1.52 lukem
69 1.52 lukem #include <sys/cdefs.h>
70 1.86.10.3 christos __KERNEL_RCSID(0, "$NetBSD: lfs_alloc.c,v 1.86.10.3 2006/05/06 23:32:58 christos Exp $");
71 1.12 scottr
72 1.47 mrg #if defined(_KERNEL_OPT)
73 1.12 scottr #include "opt_quota.h"
74 1.13 scottr #endif
75 1.1 mycroft
76 1.1 mycroft #include <sys/param.h>
77 1.3 christos #include <sys/systm.h>
78 1.1 mycroft #include <sys/kernel.h>
79 1.1 mycroft #include <sys/buf.h>
80 1.56 perseant #include <sys/lock.h>
81 1.1 mycroft #include <sys/vnode.h>
82 1.1 mycroft #include <sys/syslog.h>
83 1.1 mycroft #include <sys/mount.h>
84 1.86.10.2 elad #include <sys/malloc.h>
85 1.15 thorpej #include <sys/pool.h>
86 1.50 chs #include <sys/proc.h>
87 1.81 perseant #include <sys/tree.h>
88 1.86.10.3 christos #include <sys/kauth.h>
89 1.1 mycroft
90 1.1 mycroft #include <ufs/ufs/quota.h>
91 1.1 mycroft #include <ufs/ufs/inode.h>
92 1.1 mycroft #include <ufs/ufs/ufsmount.h>
93 1.3 christos #include <ufs/ufs/ufs_extern.h>
94 1.1 mycroft
95 1.1 mycroft #include <ufs/lfs/lfs.h>
96 1.1 mycroft #include <ufs/lfs/lfs_extern.h>
97 1.1 mycroft
98 1.17 perseant extern struct lock ufs_hashlock;
99 1.17 perseant
100 1.86.10.1 elad static int extend_ifile(struct lfs *, kauth_cred_t);
101 1.70 yamt static int lfs_ialloc(struct lfs *, struct vnode *, ino_t, int,
102 1.70 yamt struct vnode **);
103 1.44 perseant
104 1.86.10.2 elad /* Constants for inode free bitmap */
105 1.86.10.2 elad #define BMSHIFT 5 /* 2 ** 5 = 32 */
106 1.86.10.2 elad #define BMMASK ((1 << BMSHIFT) - 1)
107 1.86.10.2 elad #define SET_BITMAP_FREE(F, I) do { \
108 1.86.10.2 elad DLOG((DLOG_ALLOC, "lfs: ino %d wrd %d bit %d set\n", (int)(I), \
109 1.86.10.2 elad (int)((I) >> BMSHIFT), (int)((I) & BMMASK))); \
110 1.86.10.2 elad (F)->lfs_ino_bitmap[(I) >> BMSHIFT] |= (1 << ((I) & BMMASK)); \
111 1.86.10.2 elad } while (0)
112 1.86.10.2 elad #define CLR_BITMAP_FREE(F, I) do { \
113 1.86.10.2 elad DLOG((DLOG_ALLOC, "lfs: ino %d wrd %d bit %d clr\n", (int)(I), \
114 1.86.10.2 elad (int)((I) >> BMSHIFT), (int)((I) & BMMASK))); \
115 1.86.10.2 elad (F)->lfs_ino_bitmap[(I) >> BMSHIFT] &= ~(1 << ((I) & BMMASK)); \
116 1.86.10.2 elad } while(0)
117 1.86.10.2 elad
118 1.86.10.2 elad #define ISSET_BITMAP_FREE(F, I) \
119 1.86.10.2 elad ((F)->lfs_ino_bitmap[(I) >> BMSHIFT] & (1 << ((I) & BMMASK)))
120 1.86.10.2 elad
121 1.44 perseant /*
122 1.44 perseant * Allocate a particular inode with a particular version number, freeing
123 1.44 perseant * any previous versions of this inode that may have gone before.
124 1.44 perseant * Used by the roll-forward code.
125 1.44 perseant *
126 1.44 perseant * XXX this function does not have appropriate locking to be used on a live fs;
127 1.44 perseant * XXX but something similar could probably be used for an "undelete" call.
128 1.56 perseant *
129 1.56 perseant * Called with the Ifile inode locked.
130 1.44 perseant */
131 1.44 perseant int
132 1.86 christos lfs_rf_valloc(struct lfs *fs, ino_t ino, int vers, struct lwp *l,
133 1.44 perseant struct vnode **vpp)
134 1.44 perseant {
135 1.44 perseant IFILE *ifp;
136 1.48 perseant struct buf *bp, *cbp;
137 1.44 perseant struct vnode *vp;
138 1.44 perseant struct inode *ip;
139 1.44 perseant ino_t tino, oldnext;
140 1.44 perseant int error;
141 1.48 perseant CLEANERINFO *cip;
142 1.44 perseant
143 1.78 perseant ASSERT_SEGLOCK(fs); /* XXX it doesn't, really */
144 1.78 perseant
145 1.44 perseant /*
146 1.44 perseant * First, just try a vget. If the version number is the one we want,
147 1.44 perseant * we don't have to do anything else. If the version number is wrong,
148 1.44 perseant * take appropriate action.
149 1.44 perseant */
150 1.68 thorpej error = VFS_VGET(fs->lfs_ivnode->v_mount, ino, &vp);
151 1.44 perseant if (error == 0) {
152 1.76 perseant DLOG((DLOG_RF, "lfs_rf_valloc[1]: ino %d vp %p\n", ino, vp));
153 1.44 perseant
154 1.44 perseant *vpp = vp;
155 1.44 perseant ip = VTOI(vp);
156 1.83 christos if (ip->i_gen == vers)
157 1.44 perseant return 0;
158 1.83 christos else if (ip->i_gen < vers) {
159 1.86 christos lfs_truncate(vp, (off_t)0, 0, NOCRED, l);
160 1.83 christos ip->i_gen = ip->i_ffs1_gen = vers;
161 1.73 mycroft LFS_SET_UINO(ip, IN_CHANGE | IN_UPDATE);
162 1.44 perseant return 0;
163 1.44 perseant } else {
164 1.76 perseant DLOG((DLOG_RF, "ino %d: sought version %d, got %d\n",
165 1.83 christos ino, vers, ip->i_ffs1_gen));
166 1.44 perseant vput(vp);
167 1.44 perseant *vpp = NULLVP;
168 1.44 perseant return EEXIST;
169 1.44 perseant }
170 1.44 perseant }
171 1.44 perseant
172 1.44 perseant /*
173 1.44 perseant * The inode is not in use. Find it on the free list.
174 1.44 perseant */
175 1.44 perseant /* If the Ifile is too short to contain this inum, extend it */
176 1.75 perry while (VTOI(fs->lfs_ivnode)->i_size <= (ino /
177 1.75 perry fs->lfs_ifpb + fs->lfs_cleansz + fs->lfs_segtabsz)
178 1.48 perseant << fs->lfs_bshift) {
179 1.44 perseant extend_ifile(fs, NOCRED);
180 1.44 perseant }
181 1.44 perseant
182 1.44 perseant LFS_IENTRY(ifp, fs, ino, bp);
183 1.44 perseant oldnext = ifp->if_nextfree;
184 1.83 christos ifp->if_version = vers;
185 1.44 perseant brelse(bp);
186 1.44 perseant
187 1.48 perseant LFS_GET_HEADFREE(fs, cip, cbp, &ino);
188 1.48 perseant if (ino) {
189 1.48 perseant LFS_PUT_HEADFREE(fs, cip, cbp, oldnext);
190 1.44 perseant } else {
191 1.48 perseant tino = ino;
192 1.53 chs while (1) {
193 1.44 perseant LFS_IENTRY(ifp, fs, tino, bp);
194 1.44 perseant if (ifp->if_nextfree == ino ||
195 1.44 perseant ifp->if_nextfree == LFS_UNUSED_INUM)
196 1.44 perseant break;
197 1.44 perseant tino = ifp->if_nextfree;
198 1.44 perseant brelse(bp);
199 1.44 perseant }
200 1.44 perseant if (ifp->if_nextfree == LFS_UNUSED_INUM) {
201 1.44 perseant brelse(bp);
202 1.44 perseant return ENOENT;
203 1.44 perseant }
204 1.44 perseant ifp->if_nextfree = oldnext;
205 1.56 perseant LFS_BWRITE_LOG(bp);
206 1.44 perseant }
207 1.44 perseant
208 1.83 christos error = lfs_ialloc(fs, fs->lfs_ivnode, ino, vers, &vp);
209 1.44 perseant if (error == 0) {
210 1.44 perseant /*
211 1.44 perseant * Make it VREG so we can put blocks on it. We will change
212 1.44 perseant * this later if it turns out to be some other kind of file.
213 1.44 perseant */
214 1.44 perseant ip = VTOI(vp);
215 1.66 fvdl ip->i_mode = ip->i_ffs1_mode = IFREG;
216 1.66 fvdl ip->i_nlink = ip->i_ffs1_nlink = 1;
217 1.44 perseant ip->i_ffs_effnlink = 1;
218 1.44 perseant ufs_vinit(vp->v_mount, lfs_specop_p, lfs_fifoop_p, &vp);
219 1.44 perseant ip = VTOI(vp);
220 1.44 perseant
221 1.76 perseant DLOG((DLOG_RF, "lfs_rf_valloc: ino %d vp %p\n", ino, vp));
222 1.44 perseant
223 1.44 perseant /* The dirop-nature of this vnode is past */
224 1.72 yamt lfs_unmark_vnode(vp);
225 1.44 perseant (void)lfs_vunref(vp);
226 1.78 perseant vp->v_flag &= ~VDIROP;
227 1.78 perseant simple_lock(&fs->lfs_interlock);
228 1.78 perseant simple_lock(&lfs_subsys_lock);
229 1.44 perseant --lfs_dirvcount;
230 1.78 perseant simple_unlock(&lfs_subsys_lock);
231 1.63 perseant TAILQ_REMOVE(&fs->lfs_dchainhd, ip, i_lfs_dchain);
232 1.78 perseant simple_unlock(&fs->lfs_interlock);
233 1.44 perseant }
234 1.44 perseant *vpp = vp;
235 1.44 perseant return error;
236 1.44 perseant }
237 1.44 perseant
238 1.56 perseant /*
239 1.65 perseant * Add a new block to the Ifile, to accommodate future file creations.
240 1.65 perseant * Called with the segment lock held.
241 1.56 perseant */
242 1.44 perseant static int
243 1.86.10.1 elad extend_ifile(struct lfs *fs, kauth_cred_t cred)
244 1.44 perseant {
245 1.44 perseant struct vnode *vp;
246 1.44 perseant struct inode *ip;
247 1.44 perseant IFILE *ifp;
248 1.48 perseant IFILE_V1 *ifp_v1;
249 1.48 perseant struct buf *bp, *cbp;
250 1.44 perseant int error;
251 1.83 christos daddr_t i, blkno, xmax;
252 1.86.10.2 elad ino_t oldlast, maxino;
253 1.48 perseant CLEANERINFO *cip;
254 1.44 perseant
255 1.78 perseant ASSERT_SEGLOCK(fs);
256 1.78 perseant
257 1.44 perseant vp = fs->lfs_ivnode;
258 1.44 perseant ip = VTOI(vp);
259 1.66 fvdl blkno = lblkno(fs, ip->i_size);
260 1.85 yamt if ((error = lfs_balloc(vp, ip->i_size, fs->lfs_bsize, cred, 0,
261 1.44 perseant &bp)) != 0) {
262 1.44 perseant return (error);
263 1.44 perseant }
264 1.66 fvdl ip->i_size += fs->lfs_bsize;
265 1.66 fvdl ip->i_ffs1_size = ip->i_size;
266 1.66 fvdl uvm_vnp_setsize(vp, ip->i_size);
267 1.75 perry
268 1.86.10.2 elad maxino = ((ip->i_size >> fs->lfs_bshift) - fs->lfs_cleansz -
269 1.86.10.2 elad fs->lfs_segtabsz) * fs->lfs_ifpb;
270 1.86.10.2 elad fs->lfs_ino_bitmap = (lfs_bm_t *)
271 1.86.10.2 elad realloc(fs->lfs_ino_bitmap, ((maxino + BMMASK) >> BMSHIFT) *
272 1.86.10.2 elad sizeof(lfs_bm_t), M_SEGMENT, M_WAITOK);
273 1.86.10.2 elad KASSERT(fs->lfs_ino_bitmap != NULL);
274 1.86.10.2 elad
275 1.44 perseant i = (blkno - fs->lfs_segtabsz - fs->lfs_cleansz) *
276 1.44 perseant fs->lfs_ifpb;
277 1.86.10.2 elad
278 1.86.10.2 elad /*
279 1.86.10.2 elad * We insert the new inodes at the head of the free list.
280 1.86.10.2 elad * Under normal circumstances, the free list is empty here,
281 1.86.10.2 elad * so we are also incidentally placing them at the end (which
282 1.86.10.2 elad * we must do if we are to keep them in order).
283 1.86.10.2 elad */
284 1.48 perseant LFS_GET_HEADFREE(fs, cip, cbp, &oldlast);
285 1.48 perseant LFS_PUT_HEADFREE(fs, cip, cbp, i);
286 1.44 perseant #ifdef DIAGNOSTIC
287 1.63 perseant if (fs->lfs_freehd == LFS_UNUSED_INUM)
288 1.44 perseant panic("inode 0 allocated [2]");
289 1.44 perseant #endif /* DIAGNOSTIC */
290 1.83 christos xmax = i + fs->lfs_ifpb;
291 1.48 perseant
292 1.53 chs if (fs->lfs_version == 1) {
293 1.83 christos for (ifp_v1 = (IFILE_V1 *)bp->b_data; i < xmax; ++ifp_v1) {
294 1.48 perseant ifp_v1->if_version = 1;
295 1.48 perseant ifp_v1->if_daddr = LFS_UNUSED_DADDR;
296 1.48 perseant ifp_v1->if_nextfree = ++i;
297 1.86.10.2 elad SET_BITMAP_FREE(fs, i);
298 1.48 perseant }
299 1.48 perseant ifp_v1--;
300 1.48 perseant ifp_v1->if_nextfree = oldlast;
301 1.48 perseant } else {
302 1.83 christos for (ifp = (IFILE *)bp->b_data; i < xmax; ++ifp) {
303 1.48 perseant ifp->if_version = 1;
304 1.48 perseant ifp->if_daddr = LFS_UNUSED_DADDR;
305 1.48 perseant ifp->if_nextfree = ++i;
306 1.86.10.2 elad SET_BITMAP_FREE(fs, i);
307 1.48 perseant }
308 1.48 perseant ifp--;
309 1.48 perseant ifp->if_nextfree = oldlast;
310 1.44 perseant }
311 1.83 christos LFS_PUT_TAILFREE(fs, cip, cbp, xmax - 1);
312 1.48 perseant
313 1.56 perseant (void) LFS_BWRITE_LOG(bp); /* Ifile */
314 1.44 perseant
315 1.44 perseant return 0;
316 1.44 perseant }
317 1.44 perseant
318 1.1 mycroft /* Allocate a new inode. */
319 1.1 mycroft /* ARGSUSED */
320 1.43 perseant /* VOP_BWRITE 2i times */
321 1.1 mycroft int
322 1.86.10.1 elad lfs_valloc(struct vnode *pvp, int mode, kauth_cred_t cred, struct vnode **vpp)
323 1.3 christos {
324 1.1 mycroft struct lfs *fs;
325 1.48 perseant struct buf *bp, *cbp;
326 1.1 mycroft struct ifile *ifp;
327 1.1 mycroft ino_t new_ino;
328 1.1 mycroft int error;
329 1.36 perseant int new_gen;
330 1.48 perseant CLEANERINFO *cip;
331 1.1 mycroft
332 1.85 yamt fs = VTOI(pvp)->i_lfs;
333 1.38 perseant if (fs->lfs_ronly)
334 1.38 perseant return EROFS;
335 1.75 perry
336 1.78 perseant ASSERT_NO_SEGLOCK(fs);
337 1.78 perseant
338 1.56 perseant lfs_seglock(fs, SEGM_PROT);
339 1.78 perseant vn_lock(fs->lfs_ivnode, LK_EXCLUSIVE);
340 1.17 perseant
341 1.1 mycroft /* Get the head of the freelist. */
342 1.48 perseant LFS_GET_HEADFREE(fs, cip, cbp, &new_ino);
343 1.86.10.2 elad KASSERT(new_ino != LFS_UNUSED_INUM && new_ino != LFS_IFILE_INUM);
344 1.48 perseant
345 1.86.10.2 elad DLOG((DLOG_ALLOC, "lfs_valloc: allocate inode %lld\n",
346 1.86.10.2 elad (long long)new_ino));
347 1.75 perry
348 1.1 mycroft /*
349 1.1 mycroft * Remove the inode from the free list and write the new start
350 1.1 mycroft * of the free list into the superblock.
351 1.1 mycroft */
352 1.86.10.2 elad CLR_BITMAP_FREE(fs, new_ino);
353 1.1 mycroft LFS_IENTRY(ifp, fs, new_ino, bp);
354 1.1 mycroft if (ifp->if_daddr != LFS_UNUSED_DADDR)
355 1.84 christos panic("lfs_valloc: inuse inode %llu on the free list",
356 1.84 christos (unsigned long long)new_ino);
357 1.48 perseant LFS_PUT_HEADFREE(fs, cip, cbp, ifp->if_nextfree);
358 1.86.10.2 elad DLOG((DLOG_ALLOC, "lfs_valloc: headfree %lld -> %lld\n",
359 1.86.10.2 elad (long long)new_ino, (long long)ifp->if_nextfree));
360 1.48 perseant
361 1.37 perseant new_gen = ifp->if_version; /* version was updated by vfree */
362 1.28 perseant brelse(bp);
363 1.30 perseant
364 1.1 mycroft /* Extend IFILE so that the next lfs_valloc will succeed. */
365 1.63 perseant if (fs->lfs_freehd == LFS_UNUSED_INUM) {
366 1.85 yamt if ((error = extend_ifile(fs, cred)) != 0) {
367 1.48 perseant LFS_PUT_HEADFREE(fs, cip, cbp, new_ino);
368 1.78 perseant VOP_UNLOCK(fs->lfs_ivnode, 0);
369 1.56 perseant lfs_segunlock(fs);
370 1.44 perseant return error;
371 1.1 mycroft }
372 1.1 mycroft }
373 1.17 perseant #ifdef DIAGNOSTIC
374 1.63 perseant if (fs->lfs_freehd == LFS_UNUSED_INUM)
375 1.17 perseant panic("inode 0 allocated [3]");
376 1.17 perseant #endif /* DIAGNOSTIC */
377 1.48 perseant
378 1.78 perseant /* Set superblock modified bit and increment file count. */
379 1.78 perseant simple_lock(&fs->lfs_interlock);
380 1.78 perseant fs->lfs_fmod = 1;
381 1.78 perseant simple_unlock(&fs->lfs_interlock);
382 1.78 perseant ++fs->lfs_nfiles;
383 1.78 perseant
384 1.78 perseant VOP_UNLOCK(fs->lfs_ivnode, 0);
385 1.56 perseant lfs_segunlock(fs);
386 1.63 perseant
387 1.85 yamt return lfs_ialloc(fs, pvp, new_ino, new_gen, vpp);
388 1.44 perseant }
389 1.44 perseant
390 1.65 perseant /*
391 1.65 perseant * Finish allocating a new inode, given an inode and generation number.
392 1.65 perseant */
393 1.44 perseant static int
394 1.44 perseant lfs_ialloc(struct lfs *fs, struct vnode *pvp, ino_t new_ino, int new_gen,
395 1.44 perseant struct vnode **vpp)
396 1.44 perseant {
397 1.44 perseant struct inode *ip;
398 1.44 perseant struct vnode *vp;
399 1.40 fvdl
400 1.78 perseant ASSERT_NO_SEGLOCK(fs);
401 1.78 perseant
402 1.77 perseant vp = *vpp;
403 1.31 perseant lockmgr(&ufs_hashlock, LK_EXCLUSIVE, 0);
404 1.40 fvdl /* Create an inode to associate with the vnode. */
405 1.44 perseant lfs_vcreate(pvp->v_mount, new_ino, vp);
406 1.51 chs
407 1.1 mycroft ip = VTOI(vp);
408 1.73 mycroft LFS_SET_UINO(ip, IN_CHANGE);
409 1.66 fvdl /* on-disk structure has been zeroed out by lfs_vcreate */
410 1.66 fvdl ip->i_din.ffs1_din->di_inumber = new_ino;
411 1.51 chs
412 1.79 perseant /* Note no blocks yet */
413 1.79 perseant ip->i_lfs_hiblk = -1;
414 1.79 perseant
415 1.1 mycroft /* Set a new generation number for this inode. */
416 1.66 fvdl if (new_gen) {
417 1.66 fvdl ip->i_gen = new_gen;
418 1.66 fvdl ip->i_ffs1_gen = new_gen;
419 1.66 fvdl }
420 1.51 chs
421 1.1 mycroft /* Insert into the inode hash table. */
422 1.1 mycroft ufs_ihashins(ip);
423 1.26 perseant lockmgr(&ufs_hashlock, LK_RELEASE, 0);
424 1.44 perseant
425 1.77 perseant ufs_vinit(vp->v_mount, lfs_specop_p, lfs_fifoop_p, vpp);
426 1.77 perseant vp = *vpp;
427 1.44 perseant ip = VTOI(vp);
428 1.57 yamt
429 1.57 yamt memset(ip->i_lfs_fragsize, 0, NDADDR * sizeof(*ip->i_lfs_fragsize));
430 1.51 chs
431 1.51 chs uvm_vnp_setsize(vp, 0);
432 1.72 yamt lfs_mark_vnode(vp);
433 1.54 chs genfs_node_init(vp, &lfs_genfsops);
434 1.38 perseant VREF(ip->i_devvp);
435 1.1 mycroft return (0);
436 1.1 mycroft }
437 1.1 mycroft
438 1.1 mycroft /* Create a new vnode/inode pair and initialize what fields we can. */
439 1.40 fvdl void
440 1.48 perseant lfs_vcreate(struct mount *mp, ino_t ino, struct vnode *vp)
441 1.1 mycroft {
442 1.1 mycroft struct inode *ip;
443 1.66 fvdl struct ufs1_dinode *dp;
444 1.1 mycroft struct ufsmount *ump;
445 1.80 perseant #ifdef QUOTA
446 1.4 pk int i;
447 1.80 perseant #endif
448 1.75 perry
449 1.1 mycroft /* Get a pointer to the private mount structure. */
450 1.1 mycroft ump = VFSTOUFS(mp);
451 1.75 perry
452 1.78 perseant ASSERT_NO_SEGLOCK(ump->um_lfs);
453 1.78 perseant
454 1.1 mycroft /* Initialize the inode. */
455 1.15 thorpej ip = pool_get(&lfs_inode_pool, PR_WAITOK);
456 1.66 fvdl memset(ip, 0, sizeof(*ip));
457 1.66 fvdl dp = pool_get(&lfs_dinode_pool, PR_WAITOK);
458 1.66 fvdl memset(dp, 0, sizeof(*dp));
459 1.63 perseant ip->inode_ext.lfs = pool_get(&lfs_inoext_pool, PR_WAITOK);
460 1.74 perseant memset(ip->inode_ext.lfs, 0, sizeof(*ip->inode_ext.lfs));
461 1.40 fvdl vp->v_data = ip;
462 1.66 fvdl ip->i_din.ffs1_din = dp;
463 1.66 fvdl ip->i_ump = ump;
464 1.40 fvdl ip->i_vnode = vp;
465 1.1 mycroft ip->i_devvp = ump->um_devvp;
466 1.1 mycroft ip->i_dev = ump->um_dev;
467 1.66 fvdl ip->i_number = dp->di_inumber = ino;
468 1.1 mycroft ip->i_lfs = ump->um_lfs;
469 1.66 fvdl ip->i_lfs_effnblks = 0;
470 1.81 perseant SPLAY_INIT(&ip->i_lfs_lbtree);
471 1.82 perseant ip->i_lfs_nbtree = 0;
472 1.1 mycroft #ifdef QUOTA
473 1.1 mycroft for (i = 0; i < MAXQUOTAS; i++)
474 1.1 mycroft ip->i_dquot[i] = NODQUOT;
475 1.1 mycroft #endif
476 1.76 perseant #ifdef DEBUG
477 1.56 perseant if (ino == LFS_IFILE_INUM)
478 1.56 perseant vp->v_vnlock->lk_wmesg = "inlock";
479 1.56 perseant #endif
480 1.1 mycroft }
481 1.1 mycroft
482 1.86.10.2 elad #if 0
483 1.86.10.2 elad /*
484 1.86.10.2 elad * Find the highest-numbered allocated inode.
485 1.86.10.2 elad * This will be used to shrink the Ifile.
486 1.86.10.2 elad */
487 1.86.10.2 elad static inline ino_t
488 1.86.10.2 elad lfs_last_alloc_ino(struct lfs *fs)
489 1.86.10.2 elad {
490 1.86.10.2 elad ino_t ino, maxino;
491 1.86.10.2 elad
492 1.86.10.2 elad maxino = ((fs->lfs_ivnode->v_size >> fs->lfs_bshift) -
493 1.86.10.2 elad fs->lfs_cleansz - fs->lfs_segtabsz) * fs->lfs_ifpb;
494 1.86.10.2 elad for (ino = maxino - 1; ino > LFS_UNUSED_INUM; --ino) {
495 1.86.10.2 elad if (ISSET_BITMAP_FREE(fs, ino) == 0)
496 1.86.10.2 elad break;
497 1.86.10.2 elad }
498 1.86.10.2 elad return ino;
499 1.86.10.2 elad }
500 1.86.10.2 elad #endif
501 1.86.10.2 elad
502 1.86.10.2 elad /*
503 1.86.10.2 elad * Find the previous (next lowest numbered) free inode, if any.
504 1.86.10.2 elad * If there is none, return LFS_UNUSED_INUM.
505 1.86.10.2 elad */
506 1.86.10.2 elad static inline ino_t
507 1.86.10.2 elad lfs_freelist_prev(struct lfs *fs, ino_t ino)
508 1.86.10.2 elad {
509 1.86.10.2 elad ino_t tino, bound, bb, freehdbb;
510 1.86.10.2 elad
511 1.86.10.2 elad if (fs->lfs_freehd == LFS_UNUSED_INUM) /* No free inodes at all */
512 1.86.10.2 elad return LFS_UNUSED_INUM;
513 1.86.10.2 elad
514 1.86.10.2 elad /* Search our own word first */
515 1.86.10.2 elad bound = ino & ~BMMASK;
516 1.86.10.2 elad for (tino = ino - 1; tino > bound && tino > LFS_UNUSED_INUM; tino--)
517 1.86.10.2 elad if (ISSET_BITMAP_FREE(fs, tino))
518 1.86.10.2 elad return tino;
519 1.86.10.2 elad /* If there are no lower words to search, just return */
520 1.86.10.2 elad if (ino >> BMSHIFT == 0)
521 1.86.10.2 elad return LFS_UNUSED_INUM;
522 1.86.10.2 elad
523 1.86.10.2 elad /*
524 1.86.10.2 elad * Find a word with a free inode in it. We have to be a bit
525 1.86.10.2 elad * careful here since ino_t is unsigned.
526 1.86.10.2 elad */
527 1.86.10.2 elad freehdbb = (fs->lfs_freehd >> BMSHIFT);
528 1.86.10.2 elad for (bb = (ino >> BMSHIFT) - 1; bb >= freehdbb && bb > 0; --bb)
529 1.86.10.2 elad if (fs->lfs_ino_bitmap[bb])
530 1.86.10.2 elad break;
531 1.86.10.2 elad if (fs->lfs_ino_bitmap[bb] == 0)
532 1.86.10.2 elad return LFS_UNUSED_INUM;
533 1.86.10.2 elad
534 1.86.10.2 elad /* Search the word we found */
535 1.86.10.2 elad for (tino = (bb << BMSHIFT) | BMMASK; tino > LFS_UNUSED_INUM; tino--)
536 1.86.10.2 elad if (ISSET_BITMAP_FREE(fs, tino))
537 1.86.10.2 elad break;
538 1.86.10.2 elad
539 1.86.10.2 elad if (tino <= LFS_IFILE_INUM)
540 1.86.10.2 elad tino = LFS_UNUSED_INUM;
541 1.86.10.2 elad
542 1.86.10.2 elad return tino;
543 1.86.10.2 elad }
544 1.86.10.2 elad
545 1.1 mycroft /* Free an inode. */
546 1.1 mycroft /* ARGUSED */
547 1.43 perseant /* VOP_BWRITE 2i times */
548 1.1 mycroft int
549 1.85 yamt lfs_vfree(struct vnode *vp, ino_t ino, int mode)
550 1.3 christos {
551 1.1 mycroft SEGUSE *sup;
552 1.48 perseant CLEANERINFO *cip;
553 1.48 perseant struct buf *cbp, *bp;
554 1.1 mycroft struct ifile *ifp;
555 1.1 mycroft struct inode *ip;
556 1.1 mycroft struct lfs *fs;
557 1.59 fvdl daddr_t old_iaddr;
558 1.85 yamt ino_t otail;
559 1.56 perseant int s;
560 1.75 perry
561 1.1 mycroft /* Get the inode number and file system. */
562 1.30 perseant ip = VTOI(vp);
563 1.1 mycroft fs = ip->i_lfs;
564 1.1 mycroft ino = ip->i_number;
565 1.34 perseant
566 1.78 perseant ASSERT_NO_SEGLOCK(fs);
567 1.86.10.2 elad DLOG((DLOG_ALLOC, "lfs_vfree: free ino %lld\n", (long long)ino));
568 1.78 perseant
569 1.48 perseant /* Drain of pending writes */
570 1.78 perseant simple_lock(&vp->v_interlock);
571 1.56 perseant s = splbio();
572 1.48 perseant if (fs->lfs_version > 1 && WRITEINPROG(vp))
573 1.78 perseant ltsleep(vp, (PRIBIO+1), "lfs_vfree", 0, &vp->v_interlock);
574 1.56 perseant splx(s);
575 1.78 perseant simple_unlock(&vp->v_interlock);
576 1.48 perseant
577 1.63 perseant lfs_seglock(fs, SEGM_PROT);
578 1.78 perseant vn_lock(fs->lfs_ivnode, LK_EXCLUSIVE);
579 1.75 perry
580 1.72 yamt lfs_unmark_vnode(vp);
581 1.53 chs if (vp->v_flag & VDIROP) {
582 1.78 perseant vp->v_flag &= ~VDIROP;
583 1.78 perseant simple_lock(&fs->lfs_interlock);
584 1.78 perseant simple_lock(&lfs_subsys_lock);
585 1.30 perseant --lfs_dirvcount;
586 1.78 perseant simple_unlock(&lfs_subsys_lock);
587 1.63 perseant TAILQ_REMOVE(&fs->lfs_dchainhd, ip, i_lfs_dchain);
588 1.78 perseant simple_unlock(&fs->lfs_interlock);
589 1.30 perseant wakeup(&lfs_dirvcount);
590 1.30 perseant lfs_vunref(vp);
591 1.38 perseant }
592 1.30 perseant
593 1.42 perseant LFS_CLR_UINO(ip, IN_ACCESSED|IN_CLEANING|IN_MODIFIED);
594 1.33 perseant ip->i_flag &= ~IN_ALLMOD;
595 1.42 perseant
596 1.1 mycroft /*
597 1.1 mycroft * Set the ifile's inode entry to unused, increment its version number
598 1.48 perseant * and link it onto the free chain.
599 1.1 mycroft */
600 1.86.10.2 elad SET_BITMAP_FREE(fs, ino);
601 1.1 mycroft LFS_IENTRY(ifp, fs, ino, bp);
602 1.1 mycroft old_iaddr = ifp->if_daddr;
603 1.1 mycroft ifp->if_daddr = LFS_UNUSED_DADDR;
604 1.1 mycroft ++ifp->if_version;
605 1.48 perseant if (fs->lfs_version == 1) {
606 1.48 perseant LFS_GET_HEADFREE(fs, cip, cbp, &(ifp->if_nextfree));
607 1.48 perseant LFS_PUT_HEADFREE(fs, cip, cbp, ino);
608 1.56 perseant (void) LFS_BWRITE_LOG(bp); /* Ifile */
609 1.48 perseant } else {
610 1.86.10.2 elad ino_t tino, onf;
611 1.86.10.2 elad
612 1.48 perseant ifp->if_nextfree = LFS_UNUSED_INUM;
613 1.56 perseant (void) LFS_BWRITE_LOG(bp); /* Ifile */
614 1.86.10.2 elad
615 1.86.10.2 elad tino = lfs_freelist_prev(fs, ino);
616 1.86.10.2 elad if (tino == LFS_UNUSED_INUM) {
617 1.86.10.2 elad /* Nothing free below us, put us on the head */
618 1.86.10.2 elad LFS_IENTRY(ifp, fs, ino, bp);
619 1.86.10.2 elad LFS_GET_HEADFREE(fs, cip, cbp, &(ifp->if_nextfree));
620 1.86.10.2 elad LFS_PUT_HEADFREE(fs, cip, cbp, ino);
621 1.86.10.2 elad DLOG((DLOG_ALLOC, "lfs_vfree: headfree %lld -> %lld\n",
622 1.86.10.2 elad (long long)ifp->if_nextfree, (long long)ino));
623 1.86.10.2 elad LFS_BWRITE_LOG(bp); /* Ifile */
624 1.86.10.2 elad
625 1.86.10.2 elad /* If the list was empty, set tail too */
626 1.86.10.2 elad LFS_GET_TAILFREE(fs, cip, cbp, &otail);
627 1.86.10.2 elad if (otail == LFS_UNUSED_INUM) {
628 1.86.10.2 elad LFS_PUT_TAILFREE(fs, cip, cbp, ino);
629 1.86.10.2 elad DLOG((DLOG_ALLOC, "lfs_vfree: tailfree %lld "
630 1.86.10.2 elad "-> %lld\n", (long long)otail,
631 1.86.10.2 elad (long long)ino));
632 1.86.10.2 elad }
633 1.86.10.2 elad } else {
634 1.86.10.2 elad /*
635 1.86.10.2 elad * Insert this inode into the list after tino.
636 1.86.10.2 elad * We hold the segment lock so we don't have to
637 1.86.10.2 elad * worry about blocks being written out of order.
638 1.86.10.2 elad */
639 1.86.10.2 elad DLOG((DLOG_ALLOC, "lfs_vfree: insert ino %lld "
640 1.86.10.2 elad " after %lld\n", ino, tino));
641 1.86.10.2 elad
642 1.86.10.2 elad LFS_IENTRY(ifp, fs, tino, bp);
643 1.86.10.2 elad onf = ifp->if_nextfree;
644 1.86.10.2 elad ifp->if_nextfree = ino;
645 1.86.10.2 elad LFS_BWRITE_LOG(bp); /* Ifile */
646 1.86.10.2 elad
647 1.86.10.2 elad LFS_IENTRY(ifp, fs, ino, bp);
648 1.86.10.2 elad ifp->if_nextfree = onf;
649 1.86.10.2 elad LFS_BWRITE_LOG(bp); /* Ifile */
650 1.86.10.2 elad
651 1.86.10.2 elad /* If we're last, put us on the tail */
652 1.86.10.2 elad if (onf == LFS_UNUSED_INUM) {
653 1.86.10.2 elad LFS_GET_TAILFREE(fs, cip, cbp, &otail);
654 1.86.10.2 elad LFS_PUT_TAILFREE(fs, cip, cbp, ino);
655 1.86.10.2 elad DLOG((DLOG_ALLOC, "lfs_vfree: tailfree %lld "
656 1.86.10.2 elad "-> %lld\n", (long long)otail,
657 1.86.10.2 elad (long long)ino));
658 1.86.10.2 elad }
659 1.86.10.2 elad }
660 1.48 perseant }
661 1.17 perseant #ifdef DIAGNOSTIC
662 1.53 chs if (ino == LFS_UNUSED_INUM) {
663 1.17 perseant panic("inode 0 freed");
664 1.17 perseant }
665 1.17 perseant #endif /* DIAGNOSTIC */
666 1.1 mycroft if (old_iaddr != LFS_UNUSED_DADDR) {
667 1.48 perseant LFS_SEGENTRY(sup, fs, dtosn(fs, old_iaddr), bp);
668 1.22 perseant #ifdef DIAGNOSTIC
669 1.66 fvdl if (sup->su_nbytes < sizeof (struct ufs1_dinode)) {
670 1.43 perseant printf("lfs_vfree: negative byte count"
671 1.62 yamt " (segment %" PRIu32 " short by %d)\n",
672 1.48 perseant dtosn(fs, old_iaddr),
673 1.66 fvdl (int)sizeof (struct ufs1_dinode) -
674 1.66 fvdl sup->su_nbytes);
675 1.22 perseant panic("lfs_vfree: negative byte count");
676 1.66 fvdl sup->su_nbytes = sizeof (struct ufs1_dinode);
677 1.17 perseant }
678 1.22 perseant #endif
679 1.66 fvdl sup->su_nbytes -= sizeof (struct ufs1_dinode);
680 1.63 perseant LFS_WRITESEGENTRY(sup, fs, dtosn(fs, old_iaddr), bp); /* Ifile */
681 1.1 mycroft }
682 1.75 perry
683 1.1 mycroft /* Set superblock modified bit and decrement file count. */
684 1.78 perseant simple_lock(&fs->lfs_interlock);
685 1.1 mycroft fs->lfs_fmod = 1;
686 1.78 perseant simple_unlock(&fs->lfs_interlock);
687 1.1 mycroft --fs->lfs_nfiles;
688 1.75 perry
689 1.78 perseant VOP_UNLOCK(fs->lfs_ivnode, 0);
690 1.56 perseant lfs_segunlock(fs);
691 1.63 perseant
692 1.1 mycroft return (0);
693 1.1 mycroft }
694 1.86.10.2 elad
695 1.86.10.2 elad /*
696 1.86.10.2 elad * Sort the freelist and set up the free-inode bitmap.
697 1.86.10.2 elad * To be called by lfs_mountfs().
698 1.86.10.2 elad */
699 1.86.10.2 elad void
700 1.86.10.2 elad lfs_order_freelist(struct lfs *fs)
701 1.86.10.2 elad {
702 1.86.10.2 elad CLEANERINFO *cip;
703 1.86.10.2 elad IFILE *ifp = NULL;
704 1.86.10.2 elad struct buf *bp;
705 1.86.10.2 elad ino_t ino, firstino, lastino, maxino;
706 1.86.10.2 elad
707 1.86.10.2 elad maxino = ((fs->lfs_ivnode->v_size >> fs->lfs_bshift) -
708 1.86.10.2 elad fs->lfs_cleansz - fs->lfs_segtabsz) * fs->lfs_ifpb;
709 1.86.10.2 elad fs->lfs_ino_bitmap = (lfs_bm_t *)
710 1.86.10.2 elad malloc(((maxino + BMMASK) >> BMSHIFT) * sizeof(lfs_bm_t),
711 1.86.10.2 elad M_SEGMENT, M_WAITOK | M_ZERO);
712 1.86.10.2 elad KASSERT(fs->lfs_ino_bitmap != NULL);
713 1.86.10.2 elad
714 1.86.10.2 elad firstino = lastino = LFS_UNUSED_INUM;
715 1.86.10.2 elad for (ino = 0; ino < maxino; ino++) {
716 1.86.10.2 elad if (ino % fs->lfs_ifpb == 0)
717 1.86.10.2 elad LFS_IENTRY(ifp, fs, ino, bp);
718 1.86.10.2 elad else
719 1.86.10.2 elad ++ifp;
720 1.86.10.2 elad
721 1.86.10.2 elad /* Don't put zero or ifile on the free list */
722 1.86.10.2 elad if (ino == LFS_UNUSED_INUM || ino == LFS_IFILE_INUM)
723 1.86.10.2 elad continue;
724 1.86.10.2 elad
725 1.86.10.2 elad if (ifp->if_daddr == LFS_UNUSED_DADDR) {
726 1.86.10.2 elad if (firstino == LFS_UNUSED_INUM)
727 1.86.10.2 elad firstino = ino;
728 1.86.10.2 elad else {
729 1.86.10.2 elad brelse(bp);
730 1.86.10.2 elad
731 1.86.10.2 elad LFS_IENTRY(ifp, fs, lastino, bp);
732 1.86.10.2 elad ifp->if_nextfree = ino;
733 1.86.10.2 elad LFS_BWRITE_LOG(bp);
734 1.86.10.2 elad
735 1.86.10.2 elad LFS_IENTRY(ifp, fs, ino, bp);
736 1.86.10.2 elad }
737 1.86.10.2 elad lastino = ino;
738 1.86.10.2 elad
739 1.86.10.2 elad SET_BITMAP_FREE(fs, ino);
740 1.86.10.2 elad }
741 1.86.10.2 elad
742 1.86.10.2 elad if ((ino + 1) % fs->lfs_ifpb == 0)
743 1.86.10.2 elad brelse(bp);
744 1.86.10.2 elad }
745 1.86.10.2 elad
746 1.86.10.2 elad LFS_PUT_HEADFREE(fs, cip, bp, firstino);
747 1.86.10.2 elad LFS_PUT_TAILFREE(fs, cip, bp, lastino);
748 1.86.10.2 elad }
749