1 /* $OpenBSD: ext2fs_inode.c,v 1.29 2005/10/06 17:43:14 pedro Exp $ */
2 /* $NetBSD: ext2fs_inode.c,v 1.24 2001/06/19 12:59:18 wiz Exp $ */
3
4 /*
5 * Copyright (c) 1997 Manuel Bouyer.
6 * Copyright (c) 1982, 1986, 1989, 1993
7 * The Regents of the University of California. All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * @(#)ffs_inode.c 8.8 (Berkeley) 10/19/94
34 * Modified for ext2fs by Manuel Bouyer.
35 */
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/mount.h>
40 #include <sys/proc.h>
41 #include <sys/file.h>
42 #include <sys/buf.h>
43 #include <sys/vnode.h>
44 #include <sys/kernel.h>
45 #include <sys/malloc.h>
46 #include <sys/resourcevar.h>
47
48 #include <uvm/uvm_extern.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/ext2fs/ext2fs.h>
56 #include <ufs/ext2fs/ext2fs_extern.h>
57
58 static int ext2fs_indirtrunc(struct inode *, ufs1_daddr_t, ufs1_daddr_t,
59 ufs1_daddr_t, int, long *);
60
61 /*
62 * Get the size of an inode.
63 */
64 u_int64_t
ext2fs_size(struct inode * ip)65 ext2fs_size(struct inode *ip)
66 {
67 u_int64_t size = ip->i_e2fs_size;
68
69 if ((ip->i_e2fs_mode & IFMT) == IFREG)
70 size |= (u_int64_t)ip->i_e2fs_dacl << 32;
71
72 return (size);
73 }
74
75 int
ext2fs_setsize(struct inode * ip,u_int64_t size)76 ext2fs_setsize(struct inode *ip, u_int64_t size)
77 {
78 if ((ip->i_e2fs_mode & IFMT) == IFREG ||
79 ip->i_e2fs_mode == 0) {
80 ip->i_e2fs_dacl = size >> 32;
81 if (size >= 0x80000000U) {
82 struct m_ext2fs *fs = ip->i_e2fs;
83
84 if (fs->e2fs.e2fs_rev <= E2FS_REV0) {
85 /* Linux automagically upgrades to REV1 here! */
86 return (EFBIG);
87 }
88 if (!(fs->e2fs.e2fs_features_rocompat
89 & EXT2F_ROCOMPAT_LARGEFILE)) {
90 fs->e2fs.e2fs_features_rocompat |=
91 EXT2F_ROCOMPAT_LARGEFILE;
92 fs->e2fs_fmod = 1;
93 }
94 }
95 } else if (size >= 0x80000000U)
96 return (EFBIG);
97
98 ip->i_e2fs_size = size;
99
100 return (0);
101 }
102
103
104 /*
105 * Last reference to an inode. If necessary, write or delete it.
106 */
107 int
ext2fs_inactive(v)108 ext2fs_inactive(v)
109 void *v;
110 {
111 struct vop_inactive_args /* {
112 struct vnode *a_vp;
113 struct proc *a_p;
114 } */ *ap = v;
115 struct vnode *vp = ap->a_vp;
116 struct inode *ip = VTOI(vp);
117 struct proc *p = ap->a_p;
118 struct timespec ts;
119 int error = 0;
120 #ifdef DIAGNOSTIC
121 extern int prtactive;
122
123 if (prtactive && vp->v_usecount != 0)
124 vprint("ext2fs_inactive: pushing active", vp);
125 #endif
126
127 /* Get rid of inodes related to stale file handles. */
128 if (ip->i_e2fs_mode == 0 || ip->i_e2fs_dtime)
129 goto out;
130
131 error = 0;
132 if (ip->i_e2fs_nlink == 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
133 if (ext2fs_size(ip) != 0) {
134 error = ext2fs_truncate(ip, (off_t)0, 0, NOCRED);
135 }
136 TIMEVAL_TO_TIMESPEC(&time, &ts);
137 ip->i_e2fs_dtime = ts.tv_sec;
138 ip->i_flag |= IN_CHANGE | IN_UPDATE;
139 ext2fs_inode_free(ip, ip->i_number, ip->i_e2fs_mode);
140 }
141 if (ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) {
142 ext2fs_update(ip, NULL, NULL, 0);
143 }
144 out:
145 VOP_UNLOCK(vp, 0, p);
146 /*
147 * If we are done with the inode, reclaim it
148 * so that it can be reused immediately.
149 */
150 if (ip->i_e2fs_dtime != 0)
151 vrecycle(vp, NULL, p);
152 return (error);
153 }
154
155
156 /*
157 * Update the access, modified, and inode change times as specified by the
158 * IACCESS, IUPDATE, and ICHANGE flags respectively. The IMODIFIED flag is
159 * used to specify that the inode needs to be updated but that the times have
160 * already been set. The access and modified times are taken from the second
161 * and third parameters; the inode change time is always taken from the current
162 * time. If waitfor is set, then wait for the disk write of the inode to
163 * complete.
164 */
165 int
ext2fs_update(struct inode * ip,struct timespec * atime,struct timespec * mtime,int waitfor)166 ext2fs_update(struct inode *ip, struct timespec *atime, struct timespec *mtime,
167 int waitfor)
168 {
169 struct m_ext2fs *fs;
170 struct buf *bp;
171 int error;
172 struct timespec ts;
173 caddr_t cp;
174
175 if (ITOV(ip)->v_mount->mnt_flag & MNT_RDONLY)
176 return (0);
177 TIMEVAL_TO_TIMESPEC(&time, &ts);
178 EXT2FS_ITIMES(ip,
179 atime ? atime : &ts,
180 mtime ? mtime : &ts);
181 if ((ip->i_flag & IN_MODIFIED) == 0)
182 return (0);
183 ip->i_flag &= ~IN_MODIFIED;
184 fs = ip->i_e2fs;
185 error = bread(ip->i_devvp,
186 fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
187 (int)fs->e2fs_bsize, NOCRED, &bp);
188 if (error) {
189 brelse(bp);
190 return (error);
191 }
192 ip->i_flag &= ~(IN_MODIFIED);
193 cp = (caddr_t)bp->b_data +
194 (ino_to_fsbo(fs, ip->i_number) * EXT2_DINODE_SIZE(fs));
195
196 /*
197 * See note about 16-bit UID/GID limitation in ext2fs_vget(). Now
198 * that we are about to write the inode, construct the split UID and
199 * GID fields out of the two 32-bit fields we kept in memory.
200 */
201 ip->i_e2fs_uid_low = (u_int16_t)ip->i_e2fs_uid;
202 ip->i_e2fs_gid_low = (u_int16_t)ip->i_e2fs_gid;
203 ip->i_e2fs_uid_high = ip->i_e2fs_uid >> 16;
204 ip->i_e2fs_gid_high = ip->i_e2fs_gid >> 16;
205
206 e2fs_isave(&ip->i_e2din, (struct ext2fs_dinode *)cp);
207 if (waitfor)
208 return (bwrite(bp));
209 else {
210 bdwrite(bp);
211 return (0);
212 }
213 }
214
215 #define SINGLE 0 /* index of single indirect block */
216 #define DOUBLE 1 /* index of double indirect block */
217 #define TRIPLE 2 /* index of triple indirect block */
218 /*
219 * Truncate the inode oip to at most length size, freeing the
220 * disk blocks.
221 */
222 int
ext2fs_truncate(struct inode * oip,off_t length,int flags,struct ucred * cred)223 ext2fs_truncate(struct inode *oip, off_t length, int flags, struct ucred *cred)
224 {
225 struct vnode *ovp = ITOV(oip);
226 ufs1_daddr_t lastblock;
227 ufs1_daddr_t bn, lbn, lastiblock[NIADDR], indir_lbn[NIADDR];
228 ufs1_daddr_t oldblks[NDADDR + NIADDR], newblks[NDADDR + NIADDR];
229 struct m_ext2fs *fs;
230 struct buf *bp;
231 int offset, size, level;
232 long count, nblocks, vflags, blocksreleased = 0;
233 int i;
234 int aflags, error, allerror;
235 off_t osize;
236
237 if (length < 0)
238 return (EINVAL);
239
240 if (ovp->v_type != VREG &&
241 ovp->v_type != VDIR &&
242 ovp->v_type != VLNK)
243 return (0);
244
245 if (ovp->v_type == VLNK &&
246 (ext2fs_size(oip) < ovp->v_mount->mnt_maxsymlinklen ||
247 (ovp->v_mount->mnt_maxsymlinklen == 0 &&
248 oip->i_e2fs_nblock == 0))) {
249 #ifdef DIAGNOSTIC
250 if (length != 0)
251 panic("ext2fs_truncate: partial truncate of symlink");
252 #endif
253 bzero((char *)&oip->i_e2din.e2di_shortlink,
254 (u_int)ext2fs_size(oip));
255 (void)ext2fs_setsize(oip, 0);
256 oip->i_flag |= IN_CHANGE | IN_UPDATE;
257 return (ext2fs_update(oip, NULL, NULL, 1));
258 }
259
260 if (ext2fs_size(oip) == length) {
261 oip->i_flag |= IN_CHANGE | IN_UPDATE;
262 return (ext2fs_update(oip, NULL, NULL, 0));
263 }
264 fs = oip->i_e2fs;
265 osize = ext2fs_size(oip);
266 /*
267 * Lengthen the size of the file. We must ensure that the
268 * last byte of the file is allocated. Since the smallest
269 * value of osize is 0, length will be at least 1.
270 */
271 if (osize < length) {
272 #if 0 /* XXX */
273 if (length > fs->fs_maxfilesize)
274 return (EFBIG);
275 #endif
276 offset = blkoff(fs, length - 1);
277 lbn = lblkno(fs, length - 1);
278 aflags = B_CLRBUF;
279 if (flags & IO_SYNC)
280 aflags |= B_SYNC;
281 error = ext2fs_buf_alloc(oip, lbn, offset + 1, cred, &bp,
282 aflags);
283 if (error)
284 return (error);
285 (void)ext2fs_setsize(oip, length);
286 uvm_vnp_setsize(ovp, length);
287 uvm_vnp_uncache(ovp);
288 if (aflags & B_SYNC)
289 bwrite(bp);
290 else
291 bawrite(bp);
292 oip->i_flag |= IN_CHANGE | IN_UPDATE;
293 return (ext2fs_update(oip, NULL, NULL, 1));
294 }
295 /*
296 * Shorten the size of the file. If the file is not being
297 * truncated to a block boundry, the contents of the
298 * partial block following the end of the file must be
299 * zero'ed in case it ever become accessible again because
300 * of subsequent file growth.
301 */
302 offset = blkoff(fs, length);
303 if (offset == 0) {
304 (void)ext2fs_setsize(oip, length);
305 } else {
306 lbn = lblkno(fs, length);
307 aflags = B_CLRBUF;
308 if (flags & IO_SYNC)
309 aflags |= B_SYNC;
310 error = ext2fs_buf_alloc(oip, lbn, offset, cred, &bp,
311 aflags);
312 if (error)
313 return (error);
314 (void)ext2fs_setsize(oip, length);
315 size = fs->e2fs_bsize;
316 uvm_vnp_setsize(ovp, length);
317 uvm_vnp_uncache(ovp);
318 bzero((char *)bp->b_data + offset, (u_int)(size - offset));
319 allocbuf(bp, size);
320 if (aflags & B_SYNC)
321 bwrite(bp);
322 else
323 bawrite(bp);
324 }
325 /*
326 * Calculate index into inode's block list of
327 * last direct and indirect blocks (if any)
328 * which we want to keep. Lastblock is -1 when
329 * the file is truncated to 0.
330 */
331 lastblock = lblkno(fs, length + fs->e2fs_bsize - 1) - 1;
332 lastiblock[SINGLE] = lastblock - NDADDR;
333 lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
334 lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
335 nblocks = btodb(fs->e2fs_bsize);
336 /*
337 * Update file and block pointers on disk before we start freeing
338 * blocks. If we crash before free'ing blocks below, the blocks
339 * will be returned to the free list. lastiblock values are also
340 * normalized to -1 for calls to ext2fs_indirtrunc below.
341 */
342 memcpy((caddr_t)oldblks, (caddr_t)&oip->i_e2fs_blocks[0], sizeof oldblks);
343 for (level = TRIPLE; level >= SINGLE; level--)
344 if (lastiblock[level] < 0) {
345 oip->i_e2fs_blocks[NDADDR + level] = 0;
346 lastiblock[level] = -1;
347 }
348 for (i = NDADDR - 1; i > lastblock; i--)
349 oip->i_e2fs_blocks[i] = 0;
350 oip->i_flag |= IN_CHANGE | IN_UPDATE;
351 if ((error = ext2fs_update(oip, NULL, NULL, 1)) != 0)
352 allerror = error;
353 /*
354 * Having written the new inode to disk, save its new configuration
355 * and put back the old block pointers long enough to process them.
356 * Note that we save the new block configuration so we can check it
357 * when we are done.
358 */
359 bcopy((caddr_t)&oip->i_e2fs_blocks[0], (caddr_t)newblks, sizeof newblks);
360 bcopy((caddr_t)oldblks, (caddr_t)&oip->i_e2fs_blocks[0], sizeof oldblks);
361 (void)ext2fs_setsize(oip, osize);
362 vflags = ((length > 0) ? V_SAVE : 0) | V_SAVEMETA;
363 allerror = vinvalbuf(ovp, vflags, cred, curproc, 0, 0);
364
365 /*
366 * Indirect blocks first.
367 */
368 indir_lbn[SINGLE] = -NDADDR;
369 indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) -1;
370 indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1;
371 for (level = TRIPLE; level >= SINGLE; level--) {
372 bn = fs2h32(oip->i_e2fs_blocks[NDADDR + level]);
373 if (bn != 0) {
374 error = ext2fs_indirtrunc(oip, indir_lbn[level],
375 fsbtodb(fs, bn), lastiblock[level], level, &count);
376 if (error)
377 allerror = error;
378 blocksreleased += count;
379 if (lastiblock[level] < 0) {
380 oip->i_e2fs_blocks[NDADDR + level] = 0;
381 ext2fs_blkfree(oip, bn);
382 blocksreleased += nblocks;
383 }
384 }
385 if (lastiblock[level] >= 0)
386 goto done;
387 }
388
389 /*
390 * All whole direct blocks or frags.
391 */
392 for (i = NDADDR - 1; i > lastblock; i--) {
393 bn = fs2h32(oip->i_e2fs_blocks[i]);
394 if (bn == 0)
395 continue;
396 oip->i_e2fs_blocks[i] = 0;
397 ext2fs_blkfree(oip, bn);
398 blocksreleased += btodb(fs->e2fs_bsize);
399 }
400
401 done:
402 #ifdef DIAGNOSTIC
403 for (level = SINGLE; level <= TRIPLE; level++)
404 if (newblks[NDADDR + level] !=
405 oip->i_e2fs_blocks[NDADDR + level])
406 panic("ext2fs_truncate1");
407 for (i = 0; i < NDADDR; i++)
408 if (newblks[i] != oip->i_e2fs_blocks[i])
409 panic("ext2fs_truncate2");
410 if (length == 0 &&
411 (!LIST_EMPTY(&ovp->v_cleanblkhd) ||
412 !LIST_EMPTY(&ovp->v_dirtyblkhd)))
413 panic("ext2fs_truncate3");
414 #endif /* DIAGNOSTIC */
415 /*
416 * Put back the real size.
417 */
418 (void)ext2fs_setsize(oip, length);
419 if (blocksreleased >= oip->i_e2fs_nblock)
420 oip->i_e2fs_nblock = 0;
421 else
422 oip->i_e2fs_nblock -= blocksreleased;
423 oip->i_flag |= IN_CHANGE;
424 return (allerror);
425 }
426
427 /*
428 * Release blocks associated with the inode ip and stored in the indirect
429 * block bn. Blocks are free'd in LIFO order up to (but not including)
430 * lastbn. If level is greater than SINGLE, the block is an indirect block
431 * and recursive calls to indirtrunc must be used to cleanse other indirect
432 * blocks.
433 *
434 * NB: triple indirect blocks are untested.
435 */
436 static int
ext2fs_indirtrunc(ip,lbn,dbn,lastbn,level,countp)437 ext2fs_indirtrunc(ip, lbn, dbn, lastbn, level, countp)
438 struct inode *ip;
439 ufs1_daddr_t lbn, lastbn;
440 ufs1_daddr_t dbn;
441 int level;
442 long *countp;
443 {
444 int i;
445 struct buf *bp;
446 struct m_ext2fs *fs = ip->i_e2fs;
447 ufs1_daddr_t *bap;
448 struct vnode *vp;
449 ufs1_daddr_t *copy = NULL, nb, nlbn, last;
450 long blkcount, factor;
451 int nblocks, blocksreleased = 0;
452 int error = 0, allerror = 0;
453
454 /*
455 * Calculate index in current block of last
456 * block to be kept. -1 indicates the entire
457 * block so we need not calculate the index.
458 */
459 factor = 1;
460 for (i = SINGLE; i < level; i++)
461 factor *= NINDIR(fs);
462 last = lastbn;
463 if (lastbn > 0)
464 last /= factor;
465 nblocks = btodb(fs->e2fs_bsize);
466 /*
467 * Get buffer of block pointers, zero those entries corresponding
468 * to blocks to be free'd, and update on disk copy first. Since
469 * double(triple) indirect before single(double) indirect, calls
470 * to bmap on these blocks will fail. However, we already have
471 * the on disk address, so we have to set the b_blkno field
472 * explicitly instead of letting bread do everything for us.
473 */
474 vp = ITOV(ip);
475 bp = getblk(vp, lbn, (int)fs->e2fs_bsize, 0, 0);
476 if (!(bp->b_flags & (B_DONE | B_DELWRI))) {
477 curproc->p_stats->p_ru.ru_inblock++; /* pay for read */
478 bp->b_flags |= B_READ;
479 if (bp->b_bcount > bp->b_bufsize)
480 panic("ext2fs_indirtrunc: bad buffer size");
481 bp->b_blkno = dbn;
482 VOP_STRATEGY(bp);
483 error = biowait(bp);
484 }
485 if (error) {
486 brelse(bp);
487 *countp = 0;
488 return (error);
489 }
490
491 bap = (ufs1_daddr_t *)bp->b_data;
492 if (lastbn >= 0) {
493 MALLOC(copy, ufs1_daddr_t *, fs->e2fs_bsize, M_TEMP, M_WAITOK);
494 memcpy((caddr_t)copy, (caddr_t)bap, (u_int)fs->e2fs_bsize);
495 memset((caddr_t)&bap[last + 1], 0,
496 (u_int)(NINDIR(fs) - (last + 1)) * sizeof (u_int32_t));
497 error = bwrite(bp);
498 if (error)
499 allerror = error;
500 bap = copy;
501 }
502
503 /*
504 * Recursively free totally unused blocks.
505 */
506 for (i = NINDIR(fs) - 1,
507 nlbn = lbn + 1 - i * factor; i > last;
508 i--, nlbn += factor) {
509 nb = fs2h32(bap[i]);
510 if (nb == 0)
511 continue;
512 if (level > SINGLE) {
513 error = ext2fs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
514 (ufs1_daddr_t)-1, level - 1,
515 &blkcount);
516 if (error)
517 allerror = error;
518 blocksreleased += blkcount;
519 }
520 ext2fs_blkfree(ip, nb);
521 blocksreleased += nblocks;
522 }
523
524 /*
525 * Recursively free last partial block.
526 */
527 if (level > SINGLE && lastbn >= 0) {
528 last = lastbn % factor;
529 nb = fs2h32(bap[i]);
530 if (nb != 0) {
531 error = ext2fs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
532 last, level - 1, &blkcount);
533 if (error)
534 allerror = error;
535 blocksreleased += blkcount;
536 }
537 }
538
539 if (copy != NULL) {
540 FREE(copy, M_TEMP);
541 } else {
542 bp->b_flags |= B_INVAL;
543 brelse(bp);
544 }
545
546 *countp = blocksreleased;
547 return (allerror);
548 }
549