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