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