lfs_alloc.c revision 1.1.1.2 1 /*
2 * Copyright (c) 1991, 1993, 1995
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.7 (Berkeley) 5/14/95
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 #include <ufs/ufs/ufs_extern.h>
50
51 #include <ufs/lfs/lfs.h>
52 #include <ufs/lfs/lfs_extern.h>
53
54 extern u_long nextgennumber;
55
56 /* Allocate a new inode. */
57 /* ARGSUSED */
58 int
59 lfs_valloc(ap)
60 struct vop_valloc_args /* {
61 struct vnode *a_pvp;
62 int a_mode;
63 struct ucred *a_cred;
64 struct vnode **a_vpp;
65 } */ *ap;
66 {
67 struct lfs *fs;
68 struct buf *bp;
69 struct ifile *ifp;
70 struct inode *ip;
71 struct vnode *vp;
72 ufs_daddr_t blkno;
73 ino_t new_ino;
74 u_long i, max;
75 int error;
76
77 /* Get the head of the freelist. */
78 fs = VTOI(ap->a_pvp)->i_lfs;
79 new_ino = fs->lfs_free;
80 #ifdef ALLOCPRINT
81 printf("lfs_ialloc: allocate inode %d\n", new_ino);
82 #endif
83
84 /*
85 * Remove the inode from the free list and write the new start
86 * of the free list into the superblock.
87 */
88 LFS_IENTRY(ifp, fs, new_ino, bp);
89 if (ifp->if_daddr != LFS_UNUSED_DADDR)
90 panic("lfs_ialloc: inuse inode on the free list");
91 fs->lfs_free = ifp->if_nextfree;
92 brelse(bp);
93
94 /* Extend IFILE so that the next lfs_valloc will succeed. */
95 if (fs->lfs_free == LFS_UNUSED_INUM) {
96 vp = fs->lfs_ivnode;
97 ip = VTOI(vp);
98 blkno = lblkno(fs, ip->i_size);
99 lfs_balloc(vp, 0, fs->lfs_bsize, blkno, &bp);
100 ip->i_size += fs->lfs_bsize;
101 vnode_pager_setsize(vp, (u_long)ip->i_size);
102 vnode_pager_uncache(vp);
103
104 i = (blkno - fs->lfs_segtabsz - fs->lfs_cleansz) *
105 fs->lfs_ifpb;
106 fs->lfs_free = i;
107 max = i + fs->lfs_ifpb;
108 for (ifp = (struct ifile *)bp->b_data; i < max; ++ifp) {
109 ifp->if_version = 1;
110 ifp->if_daddr = LFS_UNUSED_DADDR;
111 ifp->if_nextfree = ++i;
112 }
113 ifp--;
114 ifp->if_nextfree = LFS_UNUSED_INUM;
115 if (error = VOP_BWRITE(bp))
116 return (error);
117 }
118
119 /* Create a vnode to associate with the inode. */
120 if (error = lfs_vcreate(ap->a_pvp->v_mount, new_ino, &vp))
121 return (error);
122
123
124 ip = VTOI(vp);
125 /* Zero out the direct and indirect block addresses. */
126 bzero(&ip->i_din, sizeof(struct dinode));
127 ip->i_din.di_inumber = new_ino;
128
129 /* Set a new generation number for this inode. */
130 if (++nextgennumber < (u_long)time.tv_sec)
131 nextgennumber = time.tv_sec;
132 ip->i_gen = nextgennumber;
133
134 /* Insert into the inode hash table. */
135 ufs_ihashins(ip);
136
137 if (error = ufs_vinit(vp->v_mount, lfs_specop_p, LFS_FIFOOPS, &vp)) {
138 vput(vp);
139 *ap->a_vpp = NULL;
140 return (error);
141 }
142
143 *ap->a_vpp = vp;
144 vp->v_flag |= VDIROP;
145 VREF(ip->i_devvp);
146
147 /* Set superblock modified bit and increment file count. */
148 fs->lfs_fmod = 1;
149 ++fs->lfs_nfiles;
150 return (0);
151 }
152
153 /* Create a new vnode/inode pair and initialize what fields we can. */
154 int
155 lfs_vcreate(mp, ino, vpp)
156 struct mount *mp;
157 ino_t ino;
158 struct vnode **vpp;
159 {
160 extern int (**lfs_vnodeop_p)();
161 struct inode *ip;
162 struct ufsmount *ump;
163 int error, i;
164
165 /* Create the vnode. */
166 if (error = getnewvnode(VT_LFS, mp, lfs_vnodeop_p, vpp)) {
167 *vpp = NULL;
168 return (error);
169 }
170
171 /* Get a pointer to the private mount structure. */
172 ump = VFSTOUFS(mp);
173
174 /* Initialize the inode. */
175 MALLOC(ip, struct inode *, sizeof(struct inode), M_LFSNODE, M_WAITOK);
176 lockinit(&ip->i_lock, PINOD, "lfsinode", 0, 0);
177 (*vpp)->v_data = ip;
178 ip->i_vnode = *vpp;
179 ip->i_devvp = ump->um_devvp;
180 ip->i_flag = IN_MODIFIED;
181 ip->i_dev = ump->um_dev;
182 ip->i_number = ip->i_din.di_inumber = ino;
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 ufs_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