lfs_inode.c revision 1.20 1 /* $NetBSD: lfs_inode.c,v 1.20 1999/03/25 21:39:18 perseant Exp $ */
2
3 /*-
4 * Copyright (c) 1999 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Konrad E. Schroder <perseant (at) hhhh.org>.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38 /*
39 * Copyright (c) 1986, 1989, 1991, 1993
40 * The Regents of the University of California. All rights reserved.
41 *
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
44 * are met:
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. All advertising materials mentioning features or use of this software
51 * must display the following acknowledgement:
52 * This product includes software developed by the University of
53 * California, Berkeley and its contributors.
54 * 4. Neither the name of the University nor the names of its contributors
55 * may be used to endorse or promote products derived from this software
56 * without specific prior written permission.
57 *
58 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 * SUCH DAMAGE.
69 *
70 * @(#)lfs_inode.c 8.9 (Berkeley) 5/8/95
71 */
72
73 #if defined(_KERNEL) && !defined(_LKM)
74 #include "opt_quota.h"
75 #endif
76
77 #include <sys/param.h>
78 #include <sys/systm.h>
79 #include <sys/mount.h>
80 #include <sys/proc.h>
81 #include <sys/file.h>
82 #include <sys/buf.h>
83 #include <sys/vnode.h>
84 #include <sys/kernel.h>
85 #include <sys/malloc.h>
86
87 #include <vm/vm.h>
88
89 #include <ufs/ufs/quota.h>
90 #include <ufs/ufs/inode.h>
91 #include <ufs/ufs/ufsmount.h>
92 #include <ufs/ufs/ufs_extern.h>
93
94 #include <ufs/lfs/lfs.h>
95 #include <ufs/lfs/lfs_extern.h>
96
97 /* Search a block for a specific dinode. */
98 struct dinode *
99 lfs_ifind(fs, ino, dip)
100 struct lfs *fs;
101 ino_t ino;
102 register struct dinode *dip;
103 {
104 register int cnt;
105 register struct dinode *ldip;
106
107 for (cnt = INOPB(fs), ldip = dip + (cnt - 1); cnt--; --ldip)
108 if (ldip->di_inumber == ino)
109 return (ldip);
110
111 panic("lfs_ifind: dinode %u not found", ino);
112 /* NOTREACHED */
113 }
114
115 int
116 lfs_update(v)
117 void *v;
118 {
119 struct vop_update_args /* {
120 struct vnode *a_vp;
121 struct timespec *a_access;
122 struct timespec *a_modify;
123 int a_waitfor;
124 } */ *ap = v;
125 struct inode *ip;
126 struct vnode *vp = ap->a_vp;
127 int mod, oflag;
128 struct timespec ts;
129
130 if (vp->v_mount->mnt_flag & MNT_RDONLY)
131 return (0);
132 ip = VTOI(vp);
133
134 /*
135 * If we are called from vinvalbuf, and the file's blocks have
136 * already been scheduled for writing, but the writes have not
137 * yet completed, lfs_vflush will not be called, and vinvalbuf
138 * will cause a panic. So, we must wait until any pending write
139 * for our inode completes, if we are called with LFS_SYNC set.
140 */
141 while((ap->a_waitfor & LFS_SYNC) && WRITEINPROG(vp)) {
142 tsleep(vp, (PRIBIO+1), "lfs_update", 0);
143 }
144 mod = ip->i_flag & IN_MODIFIED;
145 oflag = ip->i_flag;
146 TIMEVAL_TO_TIMESPEC(&time, &ts);
147 LFS_ITIMES(ip,
148 ap->a_access ? ap->a_access : &ts,
149 ap->a_modify ? ap->a_modify : &ts, &ts);
150 if (!mod && (ip->i_flag & IN_MODIFIED))
151 ip->i_lfs->lfs_uinodes++;
152 if ((ip->i_flag & (IN_MODIFIED|IN_CLEANING)) == 0) {
153 return (0);
154 }
155
156 /* If sync, push back the vnode and any dirty blocks it may have. */
157 return (ap->a_waitfor & LFS_SYNC ? lfs_vflush(vp) : 0);
158 }
159
160 /* Update segment usage information when removing a block. */
161 #define UPDATE_SEGUSE \
162 if (lastseg != -1) { \
163 LFS_SEGENTRY(sup, fs, lastseg, sup_bp); \
164 if (num > sup->su_nbytes) { \
165 panic("lfs_truncate: negative bytes in segment %d\n", \
166 lastseg); \
167 sup->su_nbytes = 0; \
168 } else \
169 sup->su_nbytes -= num; \
170 e1 = VOP_BWRITE(sup_bp); \
171 fragsreleased += numfrags(fs, num); \
172 }
173
174 #define SEGDEC(S) { \
175 if (daddr != 0) { \
176 if (lastseg != (seg = datosn(fs, daddr))) { \
177 UPDATE_SEGUSE; \
178 num = (S); \
179 lastseg = seg; \
180 } else \
181 num += (S); \
182 } \
183 }
184
185 /*
186 * Truncate the inode ip to at most length size. Update segment usage
187 * table information.
188 */
189 /* ARGSUSED */
190 int
191 lfs_truncate(v)
192 void *v;
193 {
194 struct vop_truncate_args /* {
195 struct vnode *a_vp;
196 off_t a_length;
197 int a_flags;
198 struct ucred *a_cred;
199 struct proc *a_p;
200 } */ *ap = v;
201 register struct indir *inp;
202 register int i;
203 register ufs_daddr_t *daddrp;
204 register struct vnode *vp = ap->a_vp;
205 off_t length = ap->a_length;
206 struct buf *bp, *sup_bp;
207 struct ifile *ifp;
208 struct inode *ip;
209 struct lfs *fs;
210 struct indir a[NIADDR + 2], a_end[NIADDR + 2];
211 SEGUSE *sup;
212 ufs_daddr_t daddr, lastblock, lbn, olastblock;
213 ufs_daddr_t oldsize_lastblock, oldsize_newlast, newsize;
214 long off, a_released, fragsreleased, i_released;
215 int e1, e2, depth, lastseg, num, offset, seg, freesize;
216
217 ip = VTOI(vp);
218
219 if (vp->v_type == VLNK && vp->v_mount->mnt_maxsymlinklen > 0) {
220 #ifdef DIAGNOSTIC
221 if (length != 0)
222 panic("lfs_truncate: partial truncate of symlink");
223 #endif
224 bzero((char *)&ip->i_ffs_shortlink, (u_int)ip->i_ffs_size);
225 ip->i_ffs_size = 0;
226 ip->i_flag |= IN_CHANGE | IN_UPDATE;
227 return (VOP_UPDATE(vp, NULL, NULL, 0));
228 }
229 uvm_vnp_setsize(vp, length);
230
231 fs = ip->i_lfs;
232 lfs_imtime(fs);
233
234 /* If length is larger than the file, just update the times. */
235 if (ip->i_ffs_size <= length) {
236 ip->i_flag |= IN_CHANGE | IN_UPDATE;
237 return (VOP_UPDATE(vp, NULL, NULL, 0));
238 }
239
240 /*
241 * Make sure no writes happen while we're truncating.
242 * Otherwise, blocks which are accounted for on the inode
243 * *and* which have been created for cleaning can coexist,
244 * and cause us to overcount, and panic below.
245 *
246 * XXX KS - too restrictive? Maybe only when cleaning?
247 */
248 while(fs->lfs_seglock) {
249 tsleep(&fs->lfs_seglock, (PRIBIO+1), "lfs_truncate", 0);
250 }
251
252 /*
253 * Calculate index into inode's block list of last direct and indirect
254 * blocks (if any) which we want to keep. Lastblock is 0 when the
255 * file is truncated to 0.
256 */
257 lastblock = lblkno(fs, length + fs->lfs_bsize - 1);
258 olastblock = lblkno(fs, ip->i_ffs_size + fs->lfs_bsize - 1) - 1;
259
260 /*
261 * Update the size of the file. If the file is not being truncated to
262 * a block boundry, the contents of the partial block following the end
263 * because of subsequent file growth. For this part of the code,
264 * oldsize_newlast refers to the old size of the new last block in the
265 * file.
266 */
267 offset = blkoff(fs, length);
268 lbn = lblkno(fs, length);
269 oldsize_newlast = blksize(fs, ip, lbn);
270
271 /* Now set oldsize to the current size of the current last block */
272 oldsize_lastblock = blksize(fs, ip, olastblock);
273 if (offset == 0)
274 ip->i_ffs_size = length;
275 else {
276 #ifdef QUOTA
277 if ((e1 = getinoquota(ip)) != 0)
278 return (e1);
279 #endif
280 if ((e1 = bread(vp, lbn, oldsize_newlast, NOCRED, &bp)) != 0) {
281 printf("lfs_truncate: bread: %d\n",e1);
282 brelse(bp);
283 return (e1);
284 }
285 ip->i_ffs_size = length;
286 (void)uvm_vnp_uncache(vp);
287 newsize = blksize(fs, ip, lbn);
288 bzero((char *)bp->b_data + offset, (u_int)(newsize - offset));
289 #ifdef DEBUG
290 if(bp->b_flags & B_CALL)
291 panic("Can't allocbuf malloced buffer!");
292 else
293 #endif
294 allocbuf(bp, newsize);
295 if(oldsize_newlast > newsize)
296 ip->i_ffs_blocks -= btodb(oldsize_newlast - newsize);
297 if ((e1 = VOP_BWRITE(bp)) != 0) {
298 printf("lfs_truncate: bwrite: %d\n",e1);
299 return (e1);
300 }
301 }
302 /*
303 * Modify sup->su_nbyte counters for each deleted block; keep track
304 * of number of blocks removed for ip->i_ffs_blocks.
305 */
306 fragsreleased = 0;
307 num = 0;
308 lastseg = -1;
309
310 for (lbn = olastblock; lbn >= lastblock;) {
311 /* XXX use run length from bmap array to make this faster */
312 ufs_bmaparray(vp, lbn, &daddr, a, &depth, NULL);
313 if (lbn == olastblock) {
314 for (i = NIADDR + 2; i--;)
315 a_end[i] = a[i];
316 freesize = oldsize_lastblock;
317 } else
318 freesize = fs->lfs_bsize;
319 switch (depth) {
320 case 0: /* Direct block. */
321 daddr = ip->i_ffs_db[lbn];
322 SEGDEC(freesize);
323 ip->i_ffs_db[lbn] = 0;
324 --lbn;
325 break;
326 #ifdef DIAGNOSTIC
327 case 1: /* An indirect block. */
328 panic("lfs_truncate: ufs_bmaparray returned depth 1");
329 /* NOTREACHED */
330 #endif
331 default: /* Chain of indirect blocks. */
332 inp = a + --depth;
333 if (inp->in_off > 0 && lbn != lastblock) {
334 lbn -= inp->in_off < lbn - lastblock ?
335 inp->in_off : lbn - lastblock;
336 break;
337 }
338 for (; depth && (inp->in_off == 0 || lbn == lastblock);
339 --inp, --depth) {
340 if (bread(vp,
341 inp->in_lbn, fs->lfs_bsize, NOCRED, &bp))
342 panic("lfs_truncate: bread bno %d",
343 inp->in_lbn);
344 daddrp = (ufs_daddr_t *)bp->b_data + inp->in_off;
345 for (i = inp->in_off;
346 i++ <= a_end[depth].in_off;) {
347 daddr = *daddrp++;
348 SEGDEC(freesize);
349 }
350 a_end[depth].in_off = NINDIR(fs) - 1;
351 if (inp->in_off == 0)
352 brelse (bp);
353 else {
354 bzero((ufs_daddr_t *)bp->b_data +
355 inp->in_off, fs->lfs_bsize -
356 inp->in_off * sizeof(ufs_daddr_t));
357 if ((e1 = VOP_BWRITE(bp)) != 0) {
358 printf("lfs_truncate: indir bwrite: %d\n",e1);
359 return (e1);
360 }
361 }
362 }
363 if (depth == 0 && a[1].in_off == 0) {
364 off = a[0].in_off;
365 daddr = ip->i_ffs_ib[off];
366 SEGDEC(freesize);
367 ip->i_ffs_ib[off] = 0;
368 }
369 if (lbn == lastblock || lbn <= NDADDR)
370 --lbn;
371 else {
372 lbn -= NINDIR(fs);
373 if (lbn < lastblock)
374 lbn = lastblock;
375 }
376 }
377 }
378 UPDATE_SEGUSE;
379
380 /* If truncating the file to 0, update the version number. */
381 if (length == 0) {
382 LFS_IENTRY(ifp, fs, ip->i_number, bp);
383 ++ifp->if_version;
384 (void) VOP_BWRITE(bp);
385 }
386 #ifdef DIAGNOSTIC
387 if (ip->i_ffs_blocks < fragstodb(fs, fragsreleased)) {
388 panic("lfs_truncate: frag count < 0 (%d<%ld), ino %d\n",
389 ip->i_ffs_blocks, fragstodb(fs, fragsreleased),
390 ip->i_number);
391 fragsreleased = dbtofrags(fs, ip->i_ffs_blocks);
392 }
393 #endif
394 ip->i_ffs_blocks -= fragstodb(fs, fragsreleased);
395 fs->lfs_bfree += fragstodb(fs, fragsreleased);
396 ip->i_flag |= IN_CHANGE | IN_UPDATE;
397 /*
398 * Traverse dirty block list counting number of dirty buffers
399 * that are being deleted out of the cache, so that the lfs_avail
400 * field can be updated.
401 */
402 a_released = 0;
403 i_released = 0;
404 for (bp = vp->v_dirtyblkhd.lh_first; bp; bp = bp->b_vnbufs.le_next) {
405 if (bp->b_flags & B_LOCKED) {
406 a_released += numfrags(fs, bp->b_bcount);
407 /*
408 * XXX
409 * When buffers are created in the cache, their block
410 * number is set equal to their logical block number.
411 * If that is still true, we are assuming that the
412 * blocks are new (not yet on disk) and weren't
413 * counted above. However, there is a slight chance
414 * that a block's disk address is equal to its logical
415 * block number in which case, we'll get an overcounting
416 * here.
417 */
418 if (bp->b_blkno == bp->b_lblkno) {
419 i_released += numfrags(fs, bp->b_bcount);
420 }
421 }
422 }
423 fragsreleased = i_released;
424 #ifdef DIAGNOSTIC
425 if (fragsreleased > dbtofrags(fs, ip->i_ffs_blocks)) {
426 printf("lfs_inode: %ld frags released > %d in inode %d\n",
427 fragsreleased, dbtofrags(fs, ip->i_ffs_blocks),
428 ip->i_number);
429 fragsreleased = dbtofrags(fs, ip->i_ffs_blocks);
430 }
431 #endif
432 fs->lfs_bfree += fragstodb(fs, fragsreleased);
433 ip->i_ffs_blocks -= fragstodb(fs, fragsreleased);
434 #ifdef DIAGNOSTIC
435 if (length == 0 && ip->i_ffs_blocks != 0) {
436 printf("lfs_inode: trunc to zero, but %d blocks left on inode %d\n",
437 ip->i_ffs_blocks, ip->i_number);
438 panic("lfs_inode\n");
439 }
440 #endif
441 fs->lfs_avail += fragstodb(fs, a_released);
442 e1 = vinvalbuf(vp, (length > 0) ? V_SAVE : 0, ap->a_cred, ap->a_p,
443 0, 0);
444 e2 = VOP_UPDATE(vp, NULL, NULL, 0);
445 if(e1)
446 printf("lfs_truncate: vinvalbuf: %d\n",e1);
447 if(e2)
448 printf("lfs_truncate: update: %d\n",e2);
449
450 return (e1 ? e1 : e2 ? e2 : 0);
451 }
452