lfs_alloc.c revision 1.1.1.1 1 /*
2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * @(#)lfs_alloc.c 8.4 (Berkeley) 1/4/94
34 */
35
36 #include <sys/param.h>
37 #include <sys/kernel.h>
38 #include <sys/buf.h>
39 #include <sys/vnode.h>
40 #include <sys/syslog.h>
41 #include <sys/mount.h>
42 #include <sys/malloc.h>
43
44 #include <vm/vm.h>
45
46 #include <ufs/ufs/quota.h>
47 #include <ufs/ufs/inode.h>
48 #include <ufs/ufs/ufsmount.h>
49
50 #include <ufs/lfs/lfs.h>
51 #include <ufs/lfs/lfs_extern.h>
52
53 extern u_long nextgennumber;
54
55 /* Allocate a new inode. */
56 /* ARGSUSED */
57 int
58 lfs_valloc(ap)
59 struct vop_valloc_args /* {
60 struct vnode *a_pvp;
61 int a_mode;
62 struct ucred *a_cred;
63 struct vnode **a_vpp;
64 } */ *ap;
65 {
66 struct lfs *fs;
67 struct buf *bp;
68 struct ifile *ifp;
69 struct inode *ip;
70 struct vnode *vp;
71 daddr_t blkno;
72 ino_t new_ino;
73 u_long i, max;
74 int error;
75
76 /* Get the head of the freelist. */
77 fs = VTOI(ap->a_pvp)->i_lfs;
78 new_ino = fs->lfs_free;
79 #ifdef ALLOCPRINT
80 printf("lfs_ialloc: allocate inode %d\n", new_ino);
81 #endif
82
83 /*
84 * Remove the inode from the free list and write the new start
85 * of the free list into the superblock.
86 */
87 LFS_IENTRY(ifp, fs, new_ino, bp);
88 if (ifp->if_daddr != LFS_UNUSED_DADDR)
89 panic("lfs_ialloc: inuse inode on the free list");
90 fs->lfs_free = ifp->if_nextfree;
91 brelse(bp);
92
93 /* Extend IFILE so that the next lfs_valloc will succeed. */
94 if (fs->lfs_free == LFS_UNUSED_INUM) {
95 vp = fs->lfs_ivnode;
96 ip = VTOI(vp);
97 blkno = lblkno(fs, ip->i_size);
98 lfs_balloc(vp, fs->lfs_bsize, blkno, &bp);
99 ip->i_size += fs->lfs_bsize;
100 vnode_pager_setsize(vp, (u_long)ip->i_size);
101 vnode_pager_uncache(vp);
102
103 i = (blkno - fs->lfs_segtabsz - fs->lfs_cleansz) *
104 fs->lfs_ifpb;
105 fs->lfs_free = i;
106 max = i + fs->lfs_ifpb;
107 for (ifp = (struct ifile *)bp->b_data; i < max; ++ifp) {
108 ifp->if_version = 1;
109 ifp->if_daddr = LFS_UNUSED_DADDR;
110 ifp->if_nextfree = ++i;
111 }
112 ifp--;
113 ifp->if_nextfree = LFS_UNUSED_INUM;
114 if (error = VOP_BWRITE(bp))
115 return (error);
116 }
117
118 /* Create a vnode to associate with the inode. */
119 if (error = lfs_vcreate(ap->a_pvp->v_mount, new_ino, &vp))
120 return (error);
121
122
123 ip = VTOI(vp);
124 /* Zero out the direct and indirect block addresses. */
125 bzero(&ip->i_din, sizeof(struct dinode));
126 ip->i_din.di_inumber = new_ino;
127
128 /* Set a new generation number for this inode. */
129 if (++nextgennumber < (u_long)time.tv_sec)
130 nextgennumber = time.tv_sec;
131 ip->i_gen = nextgennumber;
132
133 /* Insert into the inode hash table. */
134 ufs_ihashins(ip);
135
136 if (error = ufs_vinit(vp->v_mount, lfs_specop_p, LFS_FIFOOPS, &vp)) {
137 vput(vp);
138 *ap->a_vpp = NULL;
139 return (error);
140 }
141
142 *ap->a_vpp = vp;
143 vp->v_flag |= VDIROP;
144 VREF(ip->i_devvp);
145
146 /* Set superblock modified bit and increment file count. */
147 fs->lfs_fmod = 1;
148 ++fs->lfs_nfiles;
149 return (0);
150 }
151
152 /* Create a new vnode/inode pair and initialize what fields we can. */
153 int
154 lfs_vcreate(mp, ino, vpp)
155 struct mount *mp;
156 ino_t ino;
157 struct vnode **vpp;
158 {
159 extern int (**lfs_vnodeop_p)();
160 struct inode *ip;
161 struct ufsmount *ump;
162 int error, i;
163
164 /* Create the vnode. */
165 if (error = getnewvnode(VT_LFS, mp, lfs_vnodeop_p, vpp)) {
166 *vpp = NULL;
167 return (error);
168 }
169
170 /* Get a pointer to the private mount structure. */
171 ump = VFSTOUFS(mp);
172
173 /* Initialize the inode. */
174 MALLOC(ip, struct inode *, sizeof(struct inode), M_LFSNODE, M_WAITOK);
175 (*vpp)->v_data = ip;
176 ip->i_vnode = *vpp;
177 ip->i_devvp = ump->um_devvp;
178 ip->i_flag = IN_MODIFIED;
179 ip->i_dev = ump->um_dev;
180 ip->i_number = ip->i_din.di_inumber = ino;
181 ip->i_din.di_spare[0] = 0xdeadbeef;
182 ip->i_din.di_spare[1] = 0xdeadbeef;
183 ip->i_lfs = ump->um_lfs;
184 #ifdef QUOTA
185 for (i = 0; i < MAXQUOTAS; i++)
186 ip->i_dquot[i] = NODQUOT;
187 #endif
188 ip->i_lockf = 0;
189 ip->i_diroff = 0;
190 ip->i_mode = 0;
191 ip->i_size = 0;
192 ip->i_blocks = 0;
193 ++ump->um_lfs->lfs_uinodes;
194 return (0);
195 }
196
197 /* Free an inode. */
198 /* ARGUSED */
199 int
200 lfs_vfree(ap)
201 struct vop_vfree_args /* {
202 struct vnode *a_pvp;
203 ino_t a_ino;
204 int a_mode;
205 } */ *ap;
206 {
207 SEGUSE *sup;
208 struct buf *bp;
209 struct ifile *ifp;
210 struct inode *ip;
211 struct lfs *fs;
212 daddr_t old_iaddr;
213 ino_t ino;
214
215 /* Get the inode number and file system. */
216 ip = VTOI(ap->a_pvp);
217 fs = ip->i_lfs;
218 ino = ip->i_number;
219 if (ip->i_flag & IN_MODIFIED) {
220 --fs->lfs_uinodes;
221 ip->i_flag &=
222 ~(IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE);
223 }
224 /*
225 * Set the ifile's inode entry to unused, increment its version number
226 * and link it into the free chain.
227 */
228 LFS_IENTRY(ifp, fs, ino, bp);
229 old_iaddr = ifp->if_daddr;
230 ifp->if_daddr = LFS_UNUSED_DADDR;
231 ++ifp->if_version;
232 ifp->if_nextfree = fs->lfs_free;
233 fs->lfs_free = ino;
234 (void) VOP_BWRITE(bp);
235
236 if (old_iaddr != LFS_UNUSED_DADDR) {
237 LFS_SEGENTRY(sup, fs, datosn(fs, old_iaddr), bp);
238 #ifdef DIAGNOSTIC
239 if (sup->su_nbytes < sizeof(struct dinode))
240 panic("lfs_vfree: negative byte count (segment %d)\n",
241 datosn(fs, old_iaddr));
242 #endif
243 sup->su_nbytes -= sizeof(struct dinode);
244 (void) VOP_BWRITE(bp);
245 }
246
247 /* Set superblock modified bit and decrement file count. */
248 fs->lfs_fmod = 1;
249 --fs->lfs_nfiles;
250 return (0);
251 }
252