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