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