lfs_alloc.c revision 1.94 1 1.94 elad /* $NetBSD: lfs_alloc.c,v 1.94 2006/05/14 21:32:45 elad 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.94 elad __KERNEL_RCSID(0, "$NetBSD: lfs_alloc.c,v 1.94 2006/05/14 21:32:45 elad 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.87 perseant #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.94 elad #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.94 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.87 perseant /* Constants for inode free bitmap */
105 1.88 perseant #define BMSHIFT 5 /* 2 ** 5 = 32 */
106 1.88 perseant #define BMMASK ((1 << BMSHIFT) - 1)
107 1.88 perseant #define SET_BITMAP_FREE(F, I) do { \
108 1.88 perseant DLOG((DLOG_ALLOC, "lfs: ino %d wrd %d bit %d set\n", (int)(I), \
109 1.88 perseant (int)((I) >> BMSHIFT), (int)((I) & BMMASK))); \
110 1.88 perseant (F)->lfs_ino_bitmap[(I) >> BMSHIFT] |= (1 << ((I) & BMMASK)); \
111 1.88 perseant } while (0)
112 1.88 perseant #define CLR_BITMAP_FREE(F, I) do { \
113 1.88 perseant DLOG((DLOG_ALLOC, "lfs: ino %d wrd %d bit %d clr\n", (int)(I), \
114 1.88 perseant (int)((I) >> BMSHIFT), (int)((I) & BMMASK))); \
115 1.88 perseant (F)->lfs_ino_bitmap[(I) >> BMSHIFT] &= ~(1 << ((I) & BMMASK)); \
116 1.88 perseant } while(0)
117 1.88 perseant
118 1.87 perseant #define ISSET_BITMAP_FREE(F, I) \
119 1.88 perseant ((F)->lfs_ino_bitmap[(I) >> BMSHIFT] & (1 << ((I) & BMMASK)))
120 1.87 perseant
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.92 perseant --fs->lfs_dirvcount;
232 1.63 perseant TAILQ_REMOVE(&fs->lfs_dchainhd, ip, i_lfs_dchain);
233 1.92 perseant wakeup(&lfs_dirvcount);
234 1.92 perseant wakeup(&fs->lfs_dirvcount);
235 1.78 perseant simple_unlock(&fs->lfs_interlock);
236 1.44 perseant }
237 1.44 perseant *vpp = vp;
238 1.44 perseant return error;
239 1.44 perseant }
240 1.44 perseant
241 1.56 perseant /*
242 1.65 perseant * Add a new block to the Ifile, to accommodate future file creations.
243 1.65 perseant * Called with the segment lock held.
244 1.56 perseant */
245 1.44 perseant static int
246 1.94 elad extend_ifile(struct lfs *fs, kauth_cred_t cred)
247 1.44 perseant {
248 1.44 perseant struct vnode *vp;
249 1.44 perseant struct inode *ip;
250 1.44 perseant IFILE *ifp;
251 1.48 perseant IFILE_V1 *ifp_v1;
252 1.48 perseant struct buf *bp, *cbp;
253 1.44 perseant int error;
254 1.83 christos daddr_t i, blkno, xmax;
255 1.87 perseant ino_t oldlast, maxino;
256 1.48 perseant CLEANERINFO *cip;
257 1.44 perseant
258 1.78 perseant ASSERT_SEGLOCK(fs);
259 1.78 perseant
260 1.44 perseant vp = fs->lfs_ivnode;
261 1.44 perseant ip = VTOI(vp);
262 1.66 fvdl blkno = lblkno(fs, ip->i_size);
263 1.85 yamt if ((error = lfs_balloc(vp, ip->i_size, fs->lfs_bsize, cred, 0,
264 1.44 perseant &bp)) != 0) {
265 1.44 perseant return (error);
266 1.44 perseant }
267 1.66 fvdl ip->i_size += fs->lfs_bsize;
268 1.66 fvdl ip->i_ffs1_size = ip->i_size;
269 1.66 fvdl uvm_vnp_setsize(vp, ip->i_size);
270 1.75 perry
271 1.88 perseant maxino = ((ip->i_size >> fs->lfs_bshift) - fs->lfs_cleansz -
272 1.88 perseant fs->lfs_segtabsz) * fs->lfs_ifpb;
273 1.88 perseant fs->lfs_ino_bitmap = (lfs_bm_t *)
274 1.88 perseant realloc(fs->lfs_ino_bitmap, ((maxino + BMMASK) >> BMSHIFT) *
275 1.88 perseant sizeof(lfs_bm_t), M_SEGMENT, M_WAITOK);
276 1.88 perseant KASSERT(fs->lfs_ino_bitmap != NULL);
277 1.87 perseant
278 1.44 perseant i = (blkno - fs->lfs_segtabsz - fs->lfs_cleansz) *
279 1.44 perseant fs->lfs_ifpb;
280 1.87 perseant
281 1.87 perseant /*
282 1.87 perseant * We insert the new inodes at the head of the free list.
283 1.87 perseant * Under normal circumstances, the free list is empty here,
284 1.87 perseant * so we are also incidentally placing them at the end (which
285 1.87 perseant * we must do if we are to keep them in order).
286 1.87 perseant */
287 1.48 perseant LFS_GET_HEADFREE(fs, cip, cbp, &oldlast);
288 1.48 perseant LFS_PUT_HEADFREE(fs, cip, cbp, i);
289 1.44 perseant #ifdef DIAGNOSTIC
290 1.63 perseant if (fs->lfs_freehd == LFS_UNUSED_INUM)
291 1.44 perseant panic("inode 0 allocated [2]");
292 1.44 perseant #endif /* DIAGNOSTIC */
293 1.83 christos xmax = i + fs->lfs_ifpb;
294 1.48 perseant
295 1.53 chs if (fs->lfs_version == 1) {
296 1.83 christos for (ifp_v1 = (IFILE_V1 *)bp->b_data; i < xmax; ++ifp_v1) {
297 1.89 perseant SET_BITMAP_FREE(fs, i);
298 1.48 perseant ifp_v1->if_version = 1;
299 1.48 perseant ifp_v1->if_daddr = LFS_UNUSED_DADDR;
300 1.48 perseant ifp_v1->if_nextfree = ++i;
301 1.48 perseant }
302 1.48 perseant ifp_v1--;
303 1.48 perseant ifp_v1->if_nextfree = oldlast;
304 1.48 perseant } else {
305 1.83 christos for (ifp = (IFILE *)bp->b_data; i < xmax; ++ifp) {
306 1.89 perseant SET_BITMAP_FREE(fs, i);
307 1.48 perseant ifp->if_version = 1;
308 1.48 perseant ifp->if_daddr = LFS_UNUSED_DADDR;
309 1.48 perseant ifp->if_nextfree = ++i;
310 1.48 perseant }
311 1.48 perseant ifp--;
312 1.48 perseant ifp->if_nextfree = oldlast;
313 1.44 perseant }
314 1.83 christos LFS_PUT_TAILFREE(fs, cip, cbp, xmax - 1);
315 1.48 perseant
316 1.56 perseant (void) LFS_BWRITE_LOG(bp); /* Ifile */
317 1.44 perseant
318 1.44 perseant return 0;
319 1.44 perseant }
320 1.44 perseant
321 1.1 mycroft /* Allocate a new inode. */
322 1.1 mycroft /* ARGSUSED */
323 1.43 perseant /* VOP_BWRITE 2i times */
324 1.1 mycroft int
325 1.94 elad lfs_valloc(struct vnode *pvp, int mode, kauth_cred_t cred, struct vnode **vpp)
326 1.3 christos {
327 1.1 mycroft struct lfs *fs;
328 1.48 perseant struct buf *bp, *cbp;
329 1.1 mycroft struct ifile *ifp;
330 1.1 mycroft ino_t new_ino;
331 1.1 mycroft int error;
332 1.36 perseant int new_gen;
333 1.48 perseant CLEANERINFO *cip;
334 1.1 mycroft
335 1.85 yamt fs = VTOI(pvp)->i_lfs;
336 1.38 perseant if (fs->lfs_ronly)
337 1.38 perseant return EROFS;
338 1.75 perry
339 1.78 perseant ASSERT_NO_SEGLOCK(fs);
340 1.78 perseant
341 1.56 perseant lfs_seglock(fs, SEGM_PROT);
342 1.78 perseant vn_lock(fs->lfs_ivnode, LK_EXCLUSIVE);
343 1.17 perseant
344 1.1 mycroft /* Get the head of the freelist. */
345 1.48 perseant LFS_GET_HEADFREE(fs, cip, cbp, &new_ino);
346 1.87 perseant KASSERT(new_ino != LFS_UNUSED_INUM && new_ino != LFS_IFILE_INUM);
347 1.48 perseant
348 1.87 perseant DLOG((DLOG_ALLOC, "lfs_valloc: allocate inode %lld\n",
349 1.87 perseant (long long)new_ino));
350 1.75 perry
351 1.1 mycroft /*
352 1.1 mycroft * Remove the inode from the free list and write the new start
353 1.1 mycroft * of the free list into the superblock.
354 1.1 mycroft */
355 1.87 perseant CLR_BITMAP_FREE(fs, new_ino);
356 1.1 mycroft LFS_IENTRY(ifp, fs, new_ino, bp);
357 1.1 mycroft if (ifp->if_daddr != LFS_UNUSED_DADDR)
358 1.84 christos panic("lfs_valloc: inuse inode %llu on the free list",
359 1.84 christos (unsigned long long)new_ino);
360 1.48 perseant LFS_PUT_HEADFREE(fs, cip, cbp, ifp->if_nextfree);
361 1.87 perseant DLOG((DLOG_ALLOC, "lfs_valloc: headfree %lld -> %lld\n",
362 1.87 perseant (long long)new_ino, (long long)ifp->if_nextfree));
363 1.48 perseant
364 1.37 perseant new_gen = ifp->if_version; /* version was updated by vfree */
365 1.28 perseant brelse(bp);
366 1.30 perseant
367 1.1 mycroft /* Extend IFILE so that the next lfs_valloc will succeed. */
368 1.63 perseant if (fs->lfs_freehd == LFS_UNUSED_INUM) {
369 1.85 yamt if ((error = extend_ifile(fs, cred)) != 0) {
370 1.48 perseant LFS_PUT_HEADFREE(fs, cip, cbp, new_ino);
371 1.78 perseant VOP_UNLOCK(fs->lfs_ivnode, 0);
372 1.56 perseant lfs_segunlock(fs);
373 1.44 perseant return error;
374 1.1 mycroft }
375 1.1 mycroft }
376 1.17 perseant #ifdef DIAGNOSTIC
377 1.63 perseant if (fs->lfs_freehd == LFS_UNUSED_INUM)
378 1.17 perseant panic("inode 0 allocated [3]");
379 1.17 perseant #endif /* DIAGNOSTIC */
380 1.48 perseant
381 1.78 perseant /* Set superblock modified bit and increment file count. */
382 1.78 perseant simple_lock(&fs->lfs_interlock);
383 1.78 perseant fs->lfs_fmod = 1;
384 1.78 perseant simple_unlock(&fs->lfs_interlock);
385 1.78 perseant ++fs->lfs_nfiles;
386 1.78 perseant
387 1.78 perseant VOP_UNLOCK(fs->lfs_ivnode, 0);
388 1.56 perseant lfs_segunlock(fs);
389 1.63 perseant
390 1.85 yamt return lfs_ialloc(fs, pvp, new_ino, new_gen, vpp);
391 1.44 perseant }
392 1.44 perseant
393 1.65 perseant /*
394 1.65 perseant * Finish allocating a new inode, given an inode and generation number.
395 1.65 perseant */
396 1.44 perseant static int
397 1.44 perseant lfs_ialloc(struct lfs *fs, struct vnode *pvp, ino_t new_ino, int new_gen,
398 1.44 perseant struct vnode **vpp)
399 1.44 perseant {
400 1.44 perseant struct inode *ip;
401 1.44 perseant struct vnode *vp;
402 1.40 fvdl
403 1.78 perseant ASSERT_NO_SEGLOCK(fs);
404 1.78 perseant
405 1.77 perseant vp = *vpp;
406 1.31 perseant lockmgr(&ufs_hashlock, LK_EXCLUSIVE, 0);
407 1.40 fvdl /* Create an inode to associate with the vnode. */
408 1.44 perseant lfs_vcreate(pvp->v_mount, new_ino, vp);
409 1.51 chs
410 1.1 mycroft ip = VTOI(vp);
411 1.73 mycroft LFS_SET_UINO(ip, IN_CHANGE);
412 1.66 fvdl /* on-disk structure has been zeroed out by lfs_vcreate */
413 1.66 fvdl ip->i_din.ffs1_din->di_inumber = new_ino;
414 1.51 chs
415 1.79 perseant /* Note no blocks yet */
416 1.79 perseant ip->i_lfs_hiblk = -1;
417 1.79 perseant
418 1.1 mycroft /* Set a new generation number for this inode. */
419 1.66 fvdl if (new_gen) {
420 1.66 fvdl ip->i_gen = new_gen;
421 1.66 fvdl ip->i_ffs1_gen = new_gen;
422 1.66 fvdl }
423 1.51 chs
424 1.1 mycroft /* Insert into the inode hash table. */
425 1.1 mycroft ufs_ihashins(ip);
426 1.26 perseant lockmgr(&ufs_hashlock, LK_RELEASE, 0);
427 1.44 perseant
428 1.77 perseant ufs_vinit(vp->v_mount, lfs_specop_p, lfs_fifoop_p, vpp);
429 1.77 perseant vp = *vpp;
430 1.44 perseant ip = VTOI(vp);
431 1.57 yamt
432 1.57 yamt memset(ip->i_lfs_fragsize, 0, NDADDR * sizeof(*ip->i_lfs_fragsize));
433 1.51 chs
434 1.51 chs uvm_vnp_setsize(vp, 0);
435 1.72 yamt lfs_mark_vnode(vp);
436 1.54 chs genfs_node_init(vp, &lfs_genfsops);
437 1.38 perseant VREF(ip->i_devvp);
438 1.1 mycroft return (0);
439 1.1 mycroft }
440 1.1 mycroft
441 1.1 mycroft /* Create a new vnode/inode pair and initialize what fields we can. */
442 1.40 fvdl void
443 1.48 perseant lfs_vcreate(struct mount *mp, ino_t ino, struct vnode *vp)
444 1.1 mycroft {
445 1.1 mycroft struct inode *ip;
446 1.66 fvdl struct ufs1_dinode *dp;
447 1.1 mycroft struct ufsmount *ump;
448 1.80 perseant #ifdef QUOTA
449 1.4 pk int i;
450 1.80 perseant #endif
451 1.75 perry
452 1.1 mycroft /* Get a pointer to the private mount structure. */
453 1.1 mycroft ump = VFSTOUFS(mp);
454 1.75 perry
455 1.78 perseant ASSERT_NO_SEGLOCK(ump->um_lfs);
456 1.78 perseant
457 1.1 mycroft /* Initialize the inode. */
458 1.15 thorpej ip = pool_get(&lfs_inode_pool, PR_WAITOK);
459 1.66 fvdl memset(ip, 0, sizeof(*ip));
460 1.66 fvdl dp = pool_get(&lfs_dinode_pool, PR_WAITOK);
461 1.66 fvdl memset(dp, 0, sizeof(*dp));
462 1.63 perseant ip->inode_ext.lfs = pool_get(&lfs_inoext_pool, PR_WAITOK);
463 1.74 perseant memset(ip->inode_ext.lfs, 0, sizeof(*ip->inode_ext.lfs));
464 1.40 fvdl vp->v_data = ip;
465 1.66 fvdl ip->i_din.ffs1_din = dp;
466 1.66 fvdl ip->i_ump = ump;
467 1.40 fvdl ip->i_vnode = vp;
468 1.1 mycroft ip->i_devvp = ump->um_devvp;
469 1.1 mycroft ip->i_dev = ump->um_dev;
470 1.66 fvdl ip->i_number = dp->di_inumber = ino;
471 1.1 mycroft ip->i_lfs = ump->um_lfs;
472 1.66 fvdl ip->i_lfs_effnblks = 0;
473 1.81 perseant SPLAY_INIT(&ip->i_lfs_lbtree);
474 1.82 perseant ip->i_lfs_nbtree = 0;
475 1.91 perseant LIST_INIT(&ip->i_lfs_segdhd);
476 1.1 mycroft #ifdef QUOTA
477 1.1 mycroft for (i = 0; i < MAXQUOTAS; i++)
478 1.1 mycroft ip->i_dquot[i] = NODQUOT;
479 1.1 mycroft #endif
480 1.76 perseant #ifdef DEBUG
481 1.56 perseant if (ino == LFS_IFILE_INUM)
482 1.56 perseant vp->v_vnlock->lk_wmesg = "inlock";
483 1.56 perseant #endif
484 1.1 mycroft }
485 1.1 mycroft
486 1.87 perseant #if 0
487 1.87 perseant /*
488 1.87 perseant * Find the highest-numbered allocated inode.
489 1.87 perseant * This will be used to shrink the Ifile.
490 1.87 perseant */
491 1.87 perseant static inline ino_t
492 1.87 perseant lfs_last_alloc_ino(struct lfs *fs)
493 1.87 perseant {
494 1.87 perseant ino_t ino, maxino;
495 1.87 perseant
496 1.87 perseant maxino = ((fs->lfs_ivnode->v_size >> fs->lfs_bshift) -
497 1.87 perseant fs->lfs_cleansz - fs->lfs_segtabsz) * fs->lfs_ifpb;
498 1.87 perseant for (ino = maxino - 1; ino > LFS_UNUSED_INUM; --ino) {
499 1.87 perseant if (ISSET_BITMAP_FREE(fs, ino) == 0)
500 1.87 perseant break;
501 1.87 perseant }
502 1.87 perseant return ino;
503 1.87 perseant }
504 1.87 perseant #endif
505 1.87 perseant
506 1.87 perseant /*
507 1.87 perseant * Find the previous (next lowest numbered) free inode, if any.
508 1.87 perseant * If there is none, return LFS_UNUSED_INUM.
509 1.87 perseant */
510 1.87 perseant static inline ino_t
511 1.87 perseant lfs_freelist_prev(struct lfs *fs, ino_t ino)
512 1.87 perseant {
513 1.88 perseant ino_t tino, bound, bb, freehdbb;
514 1.88 perseant
515 1.88 perseant if (fs->lfs_freehd == LFS_UNUSED_INUM) /* No free inodes at all */
516 1.88 perseant return LFS_UNUSED_INUM;
517 1.88 perseant
518 1.88 perseant /* Search our own word first */
519 1.88 perseant bound = ino & ~BMMASK;
520 1.89 perseant for (tino = ino - 1; tino >= bound && tino > LFS_UNUSED_INUM; tino--)
521 1.88 perseant if (ISSET_BITMAP_FREE(fs, tino))
522 1.88 perseant return tino;
523 1.88 perseant /* If there are no lower words to search, just return */
524 1.88 perseant if (ino >> BMSHIFT == 0)
525 1.88 perseant return LFS_UNUSED_INUM;
526 1.88 perseant
527 1.88 perseant /*
528 1.88 perseant * Find a word with a free inode in it. We have to be a bit
529 1.88 perseant * careful here since ino_t is unsigned.
530 1.88 perseant */
531 1.88 perseant freehdbb = (fs->lfs_freehd >> BMSHIFT);
532 1.88 perseant for (bb = (ino >> BMSHIFT) - 1; bb >= freehdbb && bb > 0; --bb)
533 1.88 perseant if (fs->lfs_ino_bitmap[bb])
534 1.88 perseant break;
535 1.88 perseant if (fs->lfs_ino_bitmap[bb] == 0)
536 1.88 perseant return LFS_UNUSED_INUM;
537 1.88 perseant
538 1.88 perseant /* Search the word we found */
539 1.89 perseant for (tino = (bb << BMSHIFT) | BMMASK; tino >= (bb << BMSHIFT) &&
540 1.89 perseant tino > LFS_UNUSED_INUM; tino--)
541 1.88 perseant if (ISSET_BITMAP_FREE(fs, tino))
542 1.88 perseant break;
543 1.87 perseant
544 1.87 perseant if (tino <= LFS_IFILE_INUM)
545 1.87 perseant tino = LFS_UNUSED_INUM;
546 1.87 perseant
547 1.87 perseant return tino;
548 1.87 perseant }
549 1.87 perseant
550 1.1 mycroft /* Free an inode. */
551 1.1 mycroft /* ARGUSED */
552 1.43 perseant /* VOP_BWRITE 2i times */
553 1.1 mycroft int
554 1.85 yamt lfs_vfree(struct vnode *vp, ino_t ino, int mode)
555 1.3 christos {
556 1.1 mycroft SEGUSE *sup;
557 1.48 perseant CLEANERINFO *cip;
558 1.48 perseant struct buf *cbp, *bp;
559 1.1 mycroft struct ifile *ifp;
560 1.1 mycroft struct inode *ip;
561 1.1 mycroft struct lfs *fs;
562 1.59 fvdl daddr_t old_iaddr;
563 1.85 yamt ino_t otail;
564 1.56 perseant int s;
565 1.75 perry
566 1.1 mycroft /* Get the inode number and file system. */
567 1.30 perseant ip = VTOI(vp);
568 1.1 mycroft fs = ip->i_lfs;
569 1.1 mycroft ino = ip->i_number;
570 1.34 perseant
571 1.78 perseant ASSERT_NO_SEGLOCK(fs);
572 1.88 perseant DLOG((DLOG_ALLOC, "lfs_vfree: free ino %lld\n", (long long)ino));
573 1.78 perseant
574 1.48 perseant /* Drain of pending writes */
575 1.78 perseant simple_lock(&vp->v_interlock);
576 1.56 perseant s = splbio();
577 1.48 perseant if (fs->lfs_version > 1 && WRITEINPROG(vp))
578 1.78 perseant ltsleep(vp, (PRIBIO+1), "lfs_vfree", 0, &vp->v_interlock);
579 1.56 perseant splx(s);
580 1.78 perseant simple_unlock(&vp->v_interlock);
581 1.48 perseant
582 1.63 perseant lfs_seglock(fs, SEGM_PROT);
583 1.78 perseant vn_lock(fs->lfs_ivnode, LK_EXCLUSIVE);
584 1.75 perry
585 1.72 yamt lfs_unmark_vnode(vp);
586 1.53 chs if (vp->v_flag & VDIROP) {
587 1.78 perseant vp->v_flag &= ~VDIROP;
588 1.78 perseant simple_lock(&fs->lfs_interlock);
589 1.78 perseant simple_lock(&lfs_subsys_lock);
590 1.30 perseant --lfs_dirvcount;
591 1.78 perseant simple_unlock(&lfs_subsys_lock);
592 1.92 perseant --fs->lfs_dirvcount;
593 1.63 perseant TAILQ_REMOVE(&fs->lfs_dchainhd, ip, i_lfs_dchain);
594 1.78 perseant simple_unlock(&fs->lfs_interlock);
595 1.92 perseant wakeup(&fs->lfs_dirvcount);
596 1.30 perseant wakeup(&lfs_dirvcount);
597 1.30 perseant lfs_vunref(vp);
598 1.90 perseant
599 1.90 perseant /*
600 1.90 perseant * If this inode is not going to be written any more, any
601 1.90 perseant * segment accounting left over from its truncation needs
602 1.90 perseant * to occur at the end of the next dirops flush. Attach
603 1.90 perseant * them to the fs-wide list for that purpose.
604 1.90 perseant */
605 1.90 perseant if (LIST_FIRST(&ip->i_lfs_segdhd) != NULL) {
606 1.90 perseant struct segdelta *sd;
607 1.90 perseant
608 1.90 perseant while((sd = LIST_FIRST(&ip->i_lfs_segdhd)) != NULL) {
609 1.90 perseant LIST_REMOVE(sd, list);
610 1.90 perseant LIST_INSERT_HEAD(&fs->lfs_segdhd, sd, list);
611 1.90 perseant }
612 1.90 perseant }
613 1.90 perseant } else {
614 1.90 perseant /*
615 1.90 perseant * If it's not a dirop, we can finalize right away.
616 1.90 perseant */
617 1.90 perseant lfs_finalize_ino_seguse(fs, ip);
618 1.38 perseant }
619 1.30 perseant
620 1.42 perseant LFS_CLR_UINO(ip, IN_ACCESSED|IN_CLEANING|IN_MODIFIED);
621 1.33 perseant ip->i_flag &= ~IN_ALLMOD;
622 1.93 perseant ip->i_lfs_iflags |= LFSI_DELETED;
623 1.93 perseant
624 1.1 mycroft /*
625 1.1 mycroft * Set the ifile's inode entry to unused, increment its version number
626 1.48 perseant * and link it onto the free chain.
627 1.1 mycroft */
628 1.87 perseant SET_BITMAP_FREE(fs, ino);
629 1.1 mycroft LFS_IENTRY(ifp, fs, ino, bp);
630 1.1 mycroft old_iaddr = ifp->if_daddr;
631 1.1 mycroft ifp->if_daddr = LFS_UNUSED_DADDR;
632 1.1 mycroft ++ifp->if_version;
633 1.48 perseant if (fs->lfs_version == 1) {
634 1.48 perseant LFS_GET_HEADFREE(fs, cip, cbp, &(ifp->if_nextfree));
635 1.48 perseant LFS_PUT_HEADFREE(fs, cip, cbp, ino);
636 1.56 perseant (void) LFS_BWRITE_LOG(bp); /* Ifile */
637 1.48 perseant } else {
638 1.87 perseant ino_t tino, onf;
639 1.87 perseant
640 1.48 perseant ifp->if_nextfree = LFS_UNUSED_INUM;
641 1.56 perseant (void) LFS_BWRITE_LOG(bp); /* Ifile */
642 1.87 perseant
643 1.87 perseant tino = lfs_freelist_prev(fs, ino);
644 1.87 perseant if (tino == LFS_UNUSED_INUM) {
645 1.87 perseant /* Nothing free below us, put us on the head */
646 1.87 perseant LFS_IENTRY(ifp, fs, ino, bp);
647 1.87 perseant LFS_GET_HEADFREE(fs, cip, cbp, &(ifp->if_nextfree));
648 1.87 perseant LFS_PUT_HEADFREE(fs, cip, cbp, ino);
649 1.88 perseant DLOG((DLOG_ALLOC, "lfs_vfree: headfree %lld -> %lld\n",
650 1.87 perseant (long long)ifp->if_nextfree, (long long)ino));
651 1.87 perseant LFS_BWRITE_LOG(bp); /* Ifile */
652 1.87 perseant
653 1.87 perseant /* If the list was empty, set tail too */
654 1.87 perseant LFS_GET_TAILFREE(fs, cip, cbp, &otail);
655 1.87 perseant if (otail == LFS_UNUSED_INUM) {
656 1.87 perseant LFS_PUT_TAILFREE(fs, cip, cbp, ino);
657 1.87 perseant DLOG((DLOG_ALLOC, "lfs_vfree: tailfree %lld "
658 1.87 perseant "-> %lld\n", (long long)otail,
659 1.87 perseant (long long)ino));
660 1.87 perseant }
661 1.87 perseant } else {
662 1.87 perseant /*
663 1.87 perseant * Insert this inode into the list after tino.
664 1.87 perseant * We hold the segment lock so we don't have to
665 1.87 perseant * worry about blocks being written out of order.
666 1.87 perseant */
667 1.87 perseant DLOG((DLOG_ALLOC, "lfs_vfree: insert ino %lld "
668 1.87 perseant " after %lld\n", ino, tino));
669 1.87 perseant
670 1.87 perseant LFS_IENTRY(ifp, fs, tino, bp);
671 1.87 perseant onf = ifp->if_nextfree;
672 1.87 perseant ifp->if_nextfree = ino;
673 1.87 perseant LFS_BWRITE_LOG(bp); /* Ifile */
674 1.87 perseant
675 1.87 perseant LFS_IENTRY(ifp, fs, ino, bp);
676 1.87 perseant ifp->if_nextfree = onf;
677 1.87 perseant LFS_BWRITE_LOG(bp); /* Ifile */
678 1.87 perseant
679 1.87 perseant /* If we're last, put us on the tail */
680 1.87 perseant if (onf == LFS_UNUSED_INUM) {
681 1.87 perseant LFS_GET_TAILFREE(fs, cip, cbp, &otail);
682 1.87 perseant LFS_PUT_TAILFREE(fs, cip, cbp, ino);
683 1.87 perseant DLOG((DLOG_ALLOC, "lfs_vfree: tailfree %lld "
684 1.87 perseant "-> %lld\n", (long long)otail,
685 1.87 perseant (long long)ino));
686 1.87 perseant }
687 1.87 perseant }
688 1.48 perseant }
689 1.17 perseant #ifdef DIAGNOSTIC
690 1.53 chs if (ino == LFS_UNUSED_INUM) {
691 1.17 perseant panic("inode 0 freed");
692 1.17 perseant }
693 1.17 perseant #endif /* DIAGNOSTIC */
694 1.1 mycroft if (old_iaddr != LFS_UNUSED_DADDR) {
695 1.48 perseant LFS_SEGENTRY(sup, fs, dtosn(fs, old_iaddr), bp);
696 1.22 perseant #ifdef DIAGNOSTIC
697 1.66 fvdl if (sup->su_nbytes < sizeof (struct ufs1_dinode)) {
698 1.43 perseant printf("lfs_vfree: negative byte count"
699 1.62 yamt " (segment %" PRIu32 " short by %d)\n",
700 1.48 perseant dtosn(fs, old_iaddr),
701 1.66 fvdl (int)sizeof (struct ufs1_dinode) -
702 1.66 fvdl sup->su_nbytes);
703 1.22 perseant panic("lfs_vfree: negative byte count");
704 1.66 fvdl sup->su_nbytes = sizeof (struct ufs1_dinode);
705 1.17 perseant }
706 1.22 perseant #endif
707 1.66 fvdl sup->su_nbytes -= sizeof (struct ufs1_dinode);
708 1.63 perseant LFS_WRITESEGENTRY(sup, fs, dtosn(fs, old_iaddr), bp); /* Ifile */
709 1.1 mycroft }
710 1.75 perry
711 1.1 mycroft /* Set superblock modified bit and decrement file count. */
712 1.78 perseant simple_lock(&fs->lfs_interlock);
713 1.1 mycroft fs->lfs_fmod = 1;
714 1.78 perseant simple_unlock(&fs->lfs_interlock);
715 1.1 mycroft --fs->lfs_nfiles;
716 1.75 perry
717 1.78 perseant VOP_UNLOCK(fs->lfs_ivnode, 0);
718 1.56 perseant lfs_segunlock(fs);
719 1.63 perseant
720 1.1 mycroft return (0);
721 1.1 mycroft }
722 1.87 perseant
723 1.87 perseant /*
724 1.87 perseant * Sort the freelist and set up the free-inode bitmap.
725 1.87 perseant * To be called by lfs_mountfs().
726 1.87 perseant */
727 1.87 perseant void
728 1.87 perseant lfs_order_freelist(struct lfs *fs)
729 1.87 perseant {
730 1.87 perseant CLEANERINFO *cip;
731 1.87 perseant IFILE *ifp = NULL;
732 1.87 perseant struct buf *bp;
733 1.87 perseant ino_t ino, firstino, lastino, maxino;
734 1.87 perseant
735 1.87 perseant maxino = ((fs->lfs_ivnode->v_size >> fs->lfs_bshift) -
736 1.87 perseant fs->lfs_cleansz - fs->lfs_segtabsz) * fs->lfs_ifpb;
737 1.88 perseant fs->lfs_ino_bitmap = (lfs_bm_t *)
738 1.88 perseant malloc(((maxino + BMMASK) >> BMSHIFT) * sizeof(lfs_bm_t),
739 1.88 perseant M_SEGMENT, M_WAITOK | M_ZERO);
740 1.88 perseant KASSERT(fs->lfs_ino_bitmap != NULL);
741 1.87 perseant
742 1.87 perseant firstino = lastino = LFS_UNUSED_INUM;
743 1.87 perseant for (ino = 0; ino < maxino; ino++) {
744 1.87 perseant if (ino % fs->lfs_ifpb == 0)
745 1.87 perseant LFS_IENTRY(ifp, fs, ino, bp);
746 1.87 perseant else
747 1.87 perseant ++ifp;
748 1.87 perseant
749 1.87 perseant /* Don't put zero or ifile on the free list */
750 1.87 perseant if (ino == LFS_UNUSED_INUM || ino == LFS_IFILE_INUM)
751 1.87 perseant continue;
752 1.87 perseant
753 1.87 perseant if (ifp->if_daddr == LFS_UNUSED_DADDR) {
754 1.87 perseant if (firstino == LFS_UNUSED_INUM)
755 1.87 perseant firstino = ino;
756 1.87 perseant else {
757 1.87 perseant brelse(bp);
758 1.87 perseant
759 1.87 perseant LFS_IENTRY(ifp, fs, lastino, bp);
760 1.87 perseant ifp->if_nextfree = ino;
761 1.87 perseant LFS_BWRITE_LOG(bp);
762 1.87 perseant
763 1.87 perseant LFS_IENTRY(ifp, fs, ino, bp);
764 1.87 perseant }
765 1.87 perseant lastino = ino;
766 1.87 perseant
767 1.87 perseant SET_BITMAP_FREE(fs, ino);
768 1.87 perseant }
769 1.87 perseant
770 1.87 perseant if ((ino + 1) % fs->lfs_ifpb == 0)
771 1.87 perseant brelse(bp);
772 1.87 perseant }
773 1.87 perseant
774 1.87 perseant LFS_PUT_HEADFREE(fs, cip, bp, firstino);
775 1.87 perseant LFS_PUT_TAILFREE(fs, cip, bp, lastino);
776 1.87 perseant }
777