lfs_inode.c revision 1.1.1.1 1 /*
2 * Copyright (c) 1986, 1989, 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_inode.c 8.5 (Berkeley) 12/30/93
34 */
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/mount.h>
39 #include <sys/proc.h>
40 #include <sys/file.h>
41 #include <sys/buf.h>
42 #include <sys/vnode.h>
43 #include <sys/kernel.h>
44 #include <sys/malloc.h>
45
46 #include <vm/vm.h>
47
48 #include <ufs/ufs/quota.h>
49 #include <ufs/ufs/inode.h>
50 #include <ufs/ufs/ufsmount.h>
51 #include <ufs/ufs/ufs_extern.h>
52
53 #include <ufs/lfs/lfs.h>
54 #include <ufs/lfs/lfs_extern.h>
55
56 int
57 lfs_init()
58 {
59 return (ufs_init());
60 }
61
62 /* Search a block for a specific dinode. */
63 struct dinode *
64 lfs_ifind(fs, ino, dip)
65 struct lfs *fs;
66 ino_t ino;
67 register struct dinode *dip;
68 {
69 register int cnt;
70 register struct dinode *ldip;
71
72 for (cnt = INOPB(fs), ldip = dip + (cnt - 1); cnt--; --ldip)
73 if (ldip->di_inumber == ino)
74 return (ldip);
75
76 panic("lfs_ifind: dinode %u not found", ino);
77 /* NOTREACHED */
78 }
79
80 int
81 lfs_update(ap)
82 struct vop_update_args /* {
83 struct vnode *a_vp;
84 struct timeval *a_access;
85 struct timeval *a_modify;
86 int a_waitfor;
87 } */ *ap;
88 {
89 struct vnode *vp = ap->a_vp;
90 struct inode *ip;
91
92 if (vp->v_mount->mnt_flag & MNT_RDONLY)
93 return (0);
94 ip = VTOI(vp);
95 if ((ip->i_flag &
96 (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0)
97 return (0);
98 if (ip->i_flag & IN_ACCESS)
99 ip->i_atime.ts_sec = ap->a_access->tv_sec;
100 if (ip->i_flag & IN_UPDATE) {
101 ip->i_mtime.ts_sec = ap->a_modify->tv_sec;
102 (ip)->i_modrev++;
103 }
104 if (ip->i_flag & IN_CHANGE)
105 ip->i_ctime.ts_sec = time.tv_sec;
106 ip->i_flag &= ~(IN_ACCESS | IN_CHANGE | IN_UPDATE);
107
108 if (!(ip->i_flag & IN_MODIFIED))
109 ++(VFSTOUFS(vp->v_mount)->um_lfs->lfs_uinodes);
110 ip->i_flag |= IN_MODIFIED;
111
112 /* If sync, push back the vnode and any dirty blocks it may have. */
113 return (ap->a_waitfor & LFS_SYNC ? lfs_vflush(vp) : 0);
114 }
115
116 /* Update segment usage information when removing a block. */
117 #define UPDATE_SEGUSE \
118 if (lastseg != -1) { \
119 LFS_SEGENTRY(sup, fs, lastseg, sup_bp); \
120 if ((num << fs->lfs_bshift) > sup->su_nbytes) \
121 panic("lfs_truncate: negative bytes in segment %d\n", \
122 lastseg); \
123 sup->su_nbytes -= num << fs->lfs_bshift; \
124 e1 = VOP_BWRITE(sup_bp); \
125 blocksreleased += num; \
126 }
127
128 #define SEGDEC { \
129 if (daddr != 0) { \
130 if (lastseg != (seg = datosn(fs, daddr))) { \
131 UPDATE_SEGUSE; \
132 num = 1; \
133 lastseg = seg; \
134 } else \
135 ++num; \
136 } \
137 }
138
139 /*
140 * Truncate the inode ip to at most length size. Update segment usage
141 * table information.
142 */
143 /* ARGSUSED */
144 int
145 lfs_truncate(ap)
146 struct vop_truncate_args /* {
147 struct vnode *a_vp;
148 off_t a_length;
149 int a_flags;
150 struct ucred *a_cred;
151 struct proc *a_p;
152 } */ *ap;
153 {
154 register struct indir *inp;
155 register int i;
156 register daddr_t *daddrp;
157 register struct vnode *vp = ap->a_vp;
158 off_t length = ap->a_length;
159 struct buf *bp, *sup_bp;
160 struct timeval tv;
161 struct ifile *ifp;
162 struct inode *ip;
163 struct lfs *fs;
164 struct indir a[NIADDR + 2], a_end[NIADDR + 2];
165 SEGUSE *sup;
166 daddr_t daddr, lastblock, lbn, olastblock;
167 long off, a_released, blocksreleased, i_released;
168 int e1, e2, depth, lastseg, num, offset, seg, size;
169
170 ip = VTOI(vp);
171 tv = time;
172 if (vp->v_type == VLNK && vp->v_mount->mnt_maxsymlinklen > 0) {
173 #ifdef DIAGNOSTIC
174 if (length != 0)
175 panic("lfs_truncate: partial truncate of symlink");
176 #endif
177 bzero((char *)&ip->i_shortlink, (u_int)ip->i_size);
178 ip->i_size = 0;
179 ip->i_flag |= IN_CHANGE | IN_UPDATE;
180 return (VOP_UPDATE(vp, &tv, &tv, 0));
181 }
182 vnode_pager_setsize(vp, (u_long)length);
183
184 fs = ip->i_lfs;
185
186 /* If length is larger than the file, just update the times. */
187 if (ip->i_size <= length) {
188 ip->i_flag |= IN_CHANGE | IN_UPDATE;
189 return (VOP_UPDATE(vp, &tv, &tv, 0));
190 }
191
192 /*
193 * Calculate index into inode's block list of last direct and indirect
194 * blocks (if any) which we want to keep. Lastblock is 0 when the
195 * file is truncated to 0.
196 */
197 lastblock = lblkno(fs, length + fs->lfs_bsize - 1);
198 olastblock = lblkno(fs, ip->i_size + fs->lfs_bsize - 1) - 1;
199
200 /*
201 * Update the size of the file. If the file is not being truncated to
202 * a block boundry, the contents of the partial block following the end
203 * of the file must be zero'ed in case it ever become accessable again
204 * because of subsequent file growth.
205 */
206 offset = blkoff(fs, length);
207 if (offset == 0)
208 ip->i_size = length;
209 else {
210 lbn = lblkno(fs, length);
211 #ifdef QUOTA
212 if (e1 = getinoquota(ip))
213 return (e1);
214 #endif
215 if (e1 = bread(vp, lbn, fs->lfs_bsize, NOCRED, &bp))
216 return (e1);
217 ip->i_size = length;
218 size = blksize(fs);
219 (void)vnode_pager_uncache(vp);
220 bzero((char *)bp->b_data + offset, (u_int)(size - offset));
221 allocbuf(bp, size);
222 if (e1 = VOP_BWRITE(bp))
223 return (e1);
224 }
225 /*
226 * Modify sup->su_nbyte counters for each deleted block; keep track
227 * of number of blocks removed for ip->i_blocks.
228 */
229 blocksreleased = 0;
230 num = 0;
231 lastseg = -1;
232
233 for (lbn = olastblock; lbn >= lastblock;) {
234 /* XXX use run length from bmap array to make this faster */
235 ufs_bmaparray(vp, lbn, &daddr, a, &depth, NULL);
236 if (lbn == olastblock)
237 for (i = NIADDR + 2; i--;)
238 a_end[i] = a[i];
239 switch (depth) {
240 case 0: /* Direct block. */
241 daddr = ip->i_db[lbn];
242 SEGDEC;
243 ip->i_db[lbn] = 0;
244 --lbn;
245 break;
246 #ifdef DIAGNOSTIC
247 case 1: /* An indirect block. */
248 panic("lfs_truncate: ufs_bmaparray returned depth 1");
249 /* NOTREACHED */
250 #endif
251 default: /* Chain of indirect blocks. */
252 inp = a + --depth;
253 if (inp->in_off > 0 && lbn != lastblock) {
254 lbn -= inp->in_off < lbn - lastblock ?
255 inp->in_off : lbn - lastblock;
256 break;
257 }
258 for (; depth && (inp->in_off == 0 || lbn == lastblock);
259 --inp, --depth) {
260 if (bread(vp,
261 inp->in_lbn, fs->lfs_bsize, NOCRED, &bp))
262 panic("lfs_truncate: bread bno %d",
263 inp->in_lbn);
264 daddrp = (daddr_t *)bp->b_data + inp->in_off;
265 for (i = inp->in_off;
266 i++ <= a_end[depth].in_off;) {
267 daddr = *daddrp++;
268 SEGDEC;
269 }
270 a_end[depth].in_off = NINDIR(fs) - 1;
271 if (inp->in_off == 0)
272 brelse (bp);
273 else {
274 bzero((daddr_t *)bp->b_data +
275 inp->in_off, fs->lfs_bsize -
276 inp->in_off * sizeof(daddr_t));
277 if (e1 = VOP_BWRITE(bp))
278 return (e1);
279 }
280 }
281 if (depth == 0 && a[1].in_off == 0) {
282 off = a[0].in_off;
283 daddr = ip->i_ib[off];
284 SEGDEC;
285 ip->i_ib[off] = 0;
286 }
287 if (lbn == lastblock || lbn <= NDADDR)
288 --lbn;
289 else {
290 lbn -= NINDIR(fs);
291 if (lbn < lastblock)
292 lbn = lastblock;
293 }
294 }
295 }
296 UPDATE_SEGUSE;
297
298 /* If truncating the file to 0, update the version number. */
299 if (length == 0) {
300 LFS_IENTRY(ifp, fs, ip->i_number, bp);
301 ++ifp->if_version;
302 (void) VOP_BWRITE(bp);
303 }
304
305 #ifdef DIAGNOSTIC
306 if (ip->i_blocks < fsbtodb(fs, blocksreleased)) {
307 printf("lfs_truncate: block count < 0\n");
308 blocksreleased = ip->i_blocks;
309 }
310 #endif
311 ip->i_blocks -= fsbtodb(fs, blocksreleased);
312 fs->lfs_bfree += fsbtodb(fs, blocksreleased);
313 ip->i_flag |= IN_CHANGE | IN_UPDATE;
314 /*
315 * Traverse dirty block list counting number of dirty buffers
316 * that are being deleted out of the cache, so that the lfs_avail
317 * field can be updated.
318 */
319 a_released = 0;
320 i_released = 0;
321 for (bp = vp->v_dirtyblkhd.lh_first; bp; bp = bp->b_vnbufs.le_next)
322 if (bp->b_flags & B_LOCKED) {
323 ++a_released;
324 /*
325 * XXX
326 * When buffers are created in the cache, their block
327 * number is set equal to their logical block number.
328 * If that is still true, we are assuming that the
329 * blocks are new (not yet on disk) and weren't
330 * counted above. However, there is a slight chance
331 * that a block's disk address is equal to its logical
332 * block number in which case, we'll get an overcounting
333 * here.
334 */
335 if (bp->b_blkno == bp->b_lblkno)
336 ++i_released;
337 }
338 blocksreleased = fsbtodb(fs, i_released);
339 #ifdef DIAGNOSTIC
340 if (blocksreleased > ip->i_blocks) {
341 printf("lfs_inode: Warning! %s\n",
342 "more blocks released from inode than are in inode");
343 blocksreleased = ip->i_blocks;
344 }
345 #endif
346 fs->lfs_bfree += blocksreleased;
347 ip->i_blocks -= blocksreleased;
348 #ifdef DIAGNOSTIC
349 if (length == 0 && ip->i_blocks != 0)
350 printf("lfs_inode: Warning! %s%d%s\n",
351 "Truncation to zero, but ", ip->i_blocks,
352 " blocks left on inode");
353 #endif
354 fs->lfs_avail += fsbtodb(fs, a_released);
355 e1 = vinvalbuf(vp, (length > 0) ? V_SAVE : 0, ap->a_cred, ap->a_p,
356 0, 0);
357 e2 = VOP_UPDATE(vp, &tv, &tv, 0);
358 return (e1 ? e1 : e2 ? e2 : 0);
359 }
360