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