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