Line data Source code
1 : /* $OpenBSD: ext2fs_inode.c,v 1.59 2018/04/28 03:13:05 visa 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/buf.h>
42 : #include <sys/vnode.h>
43 : #include <sys/kernel.h>
44 : #include <sys/malloc.h>
45 : #include <sys/resourcevar.h>
46 :
47 : #include <ufs/ufs/quota.h>
48 : #include <ufs/ufs/inode.h>
49 : #include <ufs/ufs/ufsmount.h>
50 : #include <ufs/ufs/ufs_extern.h>
51 :
52 : #include <ufs/ext2fs/ext2fs.h>
53 : #include <ufs/ext2fs/ext2fs_extern.h>
54 :
55 : static int ext2fs_indirtrunc(struct inode *, int32_t, int32_t,
56 : int32_t, int, long *);
57 :
58 : /*
59 : * Get the size of an inode.
60 : */
61 : u_int64_t
62 0 : ext2fs_size(struct inode *ip)
63 : {
64 0 : u_int64_t size = ip->i_e2fs_size;
65 :
66 0 : if ((ip->i_e2fs_mode & IFMT) == IFREG)
67 0 : size |= (u_int64_t)ip->i_e2fs_size_hi << 32;
68 :
69 0 : return (size);
70 : }
71 :
72 : int
73 0 : ext2fs_setsize(struct inode *ip, u_int64_t size)
74 : {
75 0 : struct m_ext2fs *fs = ip->i_e2fs;
76 :
77 0 : if (size <= fs->e2fs_maxfilesize) {
78 : /* If HUGE_FILEs are off, e2fs_maxfilesize will protect us. */
79 0 : if ((ip->i_e2fs_mode & IFMT) == IFREG || ip->i_e2fs_mode == 0)
80 0 : ip->i_e2fs_size_hi = size >> 32;
81 :
82 0 : ip->i_e2fs_size = size;
83 0 : return (0);
84 : }
85 :
86 : /* Linux automagically upgrades to REV1 here! */
87 0 : if (fs->e2fs.e2fs_rev <= E2FS_REV0)
88 0 : return (EFBIG);
89 :
90 0 : if ((fs->e2fs.e2fs_features_rocompat & EXT2F_ROCOMPAT_LARGEFILE) == 0) {
91 0 : fs->e2fs.e2fs_features_rocompat |= EXT2F_ROCOMPAT_LARGEFILE;
92 0 : fs->e2fs_fmod = 1;
93 0 : }
94 0 : return (EFBIG);
95 0 : }
96 :
97 :
98 : /*
99 : * Last reference to an inode. If necessary, write or delete it.
100 : */
101 : int
102 0 : ext2fs_inactive(void *v)
103 : {
104 0 : struct vop_inactive_args *ap = v;
105 0 : struct vnode *vp = ap->a_vp;
106 0 : struct inode *ip = VTOI(vp);
107 0 : struct proc *p = ap->a_p;
108 0 : struct timespec ts;
109 : int error = 0;
110 : #ifdef DIAGNOSTIC
111 : extern int prtactive;
112 :
113 0 : if (prtactive && vp->v_usecount != 0)
114 0 : vprint("ext2fs_inactive: pushing active", vp);
115 : #endif
116 :
117 : /* Get rid of inodes related to stale file handles. */
118 0 : if (ip->i_e2din == NULL || ip->i_e2fs_mode == 0 || ip->i_e2fs_dtime)
119 : goto out;
120 :
121 : error = 0;
122 0 : if (ip->i_e2fs_nlink == 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
123 0 : if (ext2fs_size(ip) != 0) {
124 0 : error = ext2fs_truncate(ip, (off_t)0, 0, NOCRED);
125 0 : }
126 0 : getnanotime(&ts);
127 0 : ip->i_e2fs_dtime = ts.tv_sec;
128 0 : ip->i_flag |= IN_CHANGE | IN_UPDATE;
129 0 : ext2fs_inode_free(ip, ip->i_number, ip->i_e2fs_mode);
130 0 : }
131 0 : if (ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) {
132 0 : ext2fs_update(ip, 0);
133 0 : }
134 : out:
135 0 : VOP_UNLOCK(vp);
136 : /*
137 : * If we are done with the inode, reclaim it
138 : * so that it can be reused immediately.
139 : */
140 0 : if (ip->i_e2din == NULL || ip->i_e2fs_dtime != 0)
141 0 : vrecycle(vp, p);
142 0 : return (error);
143 0 : }
144 :
145 :
146 : /*
147 : * Update the access, modified, and inode change times as specified by the
148 : * IACCESS, IUPDATE, and ICHANGE flags respectively. The IMODIFIED flag is
149 : * used to specify that the inode needs to be updated but that the times have
150 : * already been set. The access and modified times are taken from the second
151 : * and third parameters; the inode change time is always taken from the current
152 : * time. If waitfor is set, then wait for the disk write of the inode to
153 : * complete.
154 : */
155 : int
156 0 : ext2fs_update(struct inode *ip, int waitfor)
157 : {
158 : struct m_ext2fs *fs;
159 0 : struct buf *bp;
160 : int error;
161 : caddr_t cp;
162 :
163 0 : if (ITOV(ip)->v_mount->mnt_flag & MNT_RDONLY)
164 0 : return (0);
165 0 : EXT2FS_ITIMES(ip);
166 0 : if ((ip->i_flag & IN_MODIFIED) == 0)
167 0 : return (0);
168 0 : ip->i_flag &= ~IN_MODIFIED;
169 0 : fs = ip->i_e2fs;
170 0 : error = bread(ip->i_devvp,
171 0 : fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
172 0 : (int)fs->e2fs_bsize, &bp);
173 0 : if (error) {
174 0 : brelse(bp);
175 0 : return (error);
176 : }
177 0 : ip->i_flag &= ~(IN_MODIFIED);
178 0 : cp = (caddr_t)bp->b_data +
179 0 : (ino_to_fsbo(fs, ip->i_number) * EXT2_DINODE_SIZE(fs));
180 :
181 : /*
182 : * See note about 16-bit UID/GID limitation in ext2fs_vget(). Now
183 : * that we are about to write the inode, construct the split UID and
184 : * GID fields out of the two 32-bit fields we kept in memory.
185 : */
186 0 : ip->i_e2fs_uid_low = (u_int16_t)ip->i_e2fs_uid;
187 0 : ip->i_e2fs_gid_low = (u_int16_t)ip->i_e2fs_gid;
188 0 : ip->i_e2fs_uid_high = ip->i_e2fs_uid >> 16;
189 0 : ip->i_e2fs_gid_high = ip->i_e2fs_gid >> 16;
190 :
191 0 : e2fs_isave(fs, ip->i_e2din, (struct ext2fs_dinode *)cp);
192 0 : if (waitfor)
193 0 : return (bwrite(bp));
194 : else {
195 0 : bdwrite(bp);
196 0 : return (0);
197 : }
198 0 : }
199 :
200 : #define SINGLE 0 /* index of single indirect block */
201 : #define DOUBLE 1 /* index of double indirect block */
202 : #define TRIPLE 2 /* index of triple indirect block */
203 : /*
204 : * Truncate the inode oip to at most length size, freeing the
205 : * disk blocks.
206 : */
207 : int
208 0 : ext2fs_truncate(struct inode *oip, off_t length, int flags, struct ucred *cred)
209 : {
210 0 : struct vnode *ovp = ITOV(oip);
211 : int32_t lastblock;
212 0 : int32_t bn, lbn, lastiblock[NIADDR], indir_lbn[NIADDR];
213 0 : int32_t oldblks[NDADDR + NIADDR], newblks[NDADDR + NIADDR];
214 : struct m_ext2fs *fs;
215 0 : struct buf *bp;
216 : int offset, size, level;
217 0 : long count, nblocks, vflags, blocksreleased = 0;
218 : int i;
219 : int aflags, error, allerror;
220 : off_t osize;
221 :
222 0 : if (length < 0)
223 0 : return (EINVAL);
224 :
225 0 : if (ovp->v_type != VREG &&
226 0 : ovp->v_type != VDIR &&
227 0 : ovp->v_type != VLNK)
228 0 : return (0);
229 :
230 0 : if (ovp->v_type == VLNK && ext2fs_size(oip) < EXT2_MAXSYMLINKLEN) {
231 : #ifdef DIAGNOSTIC
232 0 : if (length != 0)
233 0 : panic("ext2fs_truncate: partial truncate of symlink");
234 : #endif
235 0 : memset(&oip->i_e2din->e2di_shortlink, 0, ext2fs_size(oip));
236 0 : (void)ext2fs_setsize(oip, 0);
237 0 : oip->i_flag |= IN_CHANGE | IN_UPDATE;
238 0 : return (ext2fs_update(oip, 1));
239 : }
240 :
241 0 : if (ext2fs_size(oip) == length) {
242 0 : oip->i_flag |= IN_CHANGE | IN_UPDATE;
243 0 : return (ext2fs_update(oip, 0));
244 : }
245 0 : fs = oip->i_e2fs;
246 0 : osize = ext2fs_size(oip);
247 : /*
248 : * Lengthen the size of the file. We must ensure that the
249 : * last byte of the file is allocated. Since the smallest
250 : * value of osize is 0, length will be at least 1.
251 : */
252 0 : if (osize < length) {
253 : #if 0 /* XXX */
254 : if (length > fs->fs_maxfilesize)
255 : return (EFBIG);
256 : #endif
257 0 : offset = blkoff(fs, length - 1);
258 0 : lbn = lblkno(fs, length - 1);
259 : aflags = B_CLRBUF;
260 0 : if (flags & IO_SYNC)
261 0 : aflags |= B_SYNC;
262 0 : error = ext2fs_buf_alloc(oip, lbn, offset + 1, cred, &bp,
263 : aflags);
264 0 : if (error)
265 0 : return (error);
266 0 : (void)ext2fs_setsize(oip, length);
267 0 : uvm_vnp_setsize(ovp, length);
268 0 : uvm_vnp_uncache(ovp);
269 0 : if (aflags & B_SYNC)
270 0 : bwrite(bp);
271 : else
272 0 : bawrite(bp);
273 0 : oip->i_flag |= IN_CHANGE | IN_UPDATE;
274 0 : return (ext2fs_update(oip, 1));
275 : }
276 : /*
277 : * Shorten the size of the file. If the file is not being
278 : * truncated to a block boundry, the contents of the
279 : * partial block following the end of the file must be
280 : * zero'ed in case it ever become accessible again because
281 : * of subsequent file growth.
282 : */
283 0 : offset = blkoff(fs, length);
284 0 : if (offset == 0) {
285 0 : (void)ext2fs_setsize(oip, length);
286 0 : } else {
287 0 : lbn = lblkno(fs, length);
288 : aflags = B_CLRBUF;
289 0 : if (flags & IO_SYNC)
290 0 : aflags |= B_SYNC;
291 0 : error = ext2fs_buf_alloc(oip, lbn, offset, cred, &bp,
292 : aflags);
293 0 : if (error)
294 0 : return (error);
295 0 : (void)ext2fs_setsize(oip, length);
296 0 : size = fs->e2fs_bsize;
297 0 : uvm_vnp_setsize(ovp, length);
298 0 : uvm_vnp_uncache(ovp);
299 0 : memset(bp->b_data + offset, 0, size - offset);
300 0 : bp->b_bcount = size;
301 0 : if (aflags & B_SYNC)
302 0 : bwrite(bp);
303 : else
304 0 : bawrite(bp);
305 : }
306 : /*
307 : * Calculate index into inode's block list of
308 : * last direct and indirect blocks (if any)
309 : * which we want to keep. Lastblock is -1 when
310 : * the file is truncated to 0.
311 : */
312 0 : lastblock = lblkno(fs, length + fs->e2fs_bsize - 1) - 1;
313 0 : lastiblock[SINGLE] = lastblock - NDADDR;
314 0 : lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
315 0 : lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
316 0 : nblocks = btodb(fs->e2fs_bsize);
317 : /*
318 : * Update file and block pointers on disk before we start freeing
319 : * blocks. If we crash before free'ing blocks below, the blocks
320 : * will be returned to the free list. lastiblock values are also
321 : * normalized to -1 for calls to ext2fs_indirtrunc below.
322 : */
323 0 : memcpy(oldblks, &oip->i_e2fs_blocks[0], sizeof(oldblks));
324 0 : for (level = TRIPLE; level >= SINGLE; level--)
325 0 : if (lastiblock[level] < 0) {
326 0 : oip->i_e2fs_blocks[NDADDR + level] = 0;
327 0 : lastiblock[level] = -1;
328 0 : }
329 0 : for (i = NDADDR - 1; i > lastblock; i--)
330 0 : oip->i_e2fs_blocks[i] = 0;
331 0 : oip->i_flag |= IN_CHANGE | IN_UPDATE;
332 0 : if ((error = ext2fs_update(oip, 1)) != 0)
333 0 : allerror = error;
334 : /*
335 : * Having written the new inode to disk, save its new configuration
336 : * and put back the old block pointers long enough to process them.
337 : * Note that we save the new block configuration so we can check it
338 : * when we are done.
339 : */
340 0 : memcpy(newblks, &oip->i_e2fs_blocks[0], sizeof(newblks));
341 0 : memcpy(&oip->i_e2fs_blocks[0], oldblks, sizeof(oldblks));
342 0 : (void)ext2fs_setsize(oip, osize);
343 0 : vflags = ((length > 0) ? V_SAVE : 0) | V_SAVEMETA;
344 0 : allerror = vinvalbuf(ovp, vflags, cred, curproc, 0, 0);
345 :
346 : /*
347 : * Indirect blocks first.
348 : */
349 0 : indir_lbn[SINGLE] = -NDADDR;
350 0 : indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) -1;
351 0 : indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1;
352 0 : for (level = TRIPLE; level >= SINGLE; level--) {
353 0 : bn = letoh32(oip->i_e2fs_blocks[NDADDR + level]);
354 0 : if (bn != 0) {
355 0 : error = ext2fs_indirtrunc(oip, indir_lbn[level],
356 0 : fsbtodb(fs, bn), lastiblock[level], level, &count);
357 0 : if (error)
358 0 : allerror = error;
359 0 : blocksreleased += count;
360 0 : if (lastiblock[level] < 0) {
361 0 : oip->i_e2fs_blocks[NDADDR + level] = 0;
362 0 : ext2fs_blkfree(oip, bn);
363 0 : blocksreleased += nblocks;
364 0 : }
365 : }
366 0 : if (lastiblock[level] >= 0)
367 : goto done;
368 : }
369 :
370 : /*
371 : * All whole direct blocks or frags.
372 : */
373 0 : for (i = NDADDR - 1; i > lastblock; i--) {
374 0 : bn = letoh32(oip->i_e2fs_blocks[i]);
375 0 : if (bn == 0)
376 : continue;
377 0 : oip->i_e2fs_blocks[i] = 0;
378 0 : ext2fs_blkfree(oip, bn);
379 0 : blocksreleased += btodb(fs->e2fs_bsize);
380 0 : }
381 :
382 : done:
383 : #ifdef DIAGNOSTIC
384 0 : for (level = SINGLE; level <= TRIPLE; level++)
385 0 : if (newblks[NDADDR + level] !=
386 0 : oip->i_e2fs_blocks[NDADDR + level])
387 0 : panic("ext2fs_truncate1");
388 0 : for (i = 0; i < NDADDR; i++)
389 0 : if (newblks[i] != oip->i_e2fs_blocks[i])
390 0 : panic("ext2fs_truncate2");
391 0 : if (length == 0 &&
392 0 : (!LIST_EMPTY(&ovp->v_cleanblkhd) ||
393 0 : !LIST_EMPTY(&ovp->v_dirtyblkhd)))
394 0 : panic("ext2fs_truncate3");
395 : #endif /* DIAGNOSTIC */
396 : /*
397 : * Put back the real size.
398 : */
399 0 : (void)ext2fs_setsize(oip, length);
400 0 : if (blocksreleased >= oip->i_e2fs_nblock)
401 0 : oip->i_e2fs_nblock = 0;
402 : else
403 0 : oip->i_e2fs_nblock -= blocksreleased;
404 0 : oip->i_flag |= IN_CHANGE;
405 0 : return (allerror);
406 0 : }
407 :
408 : /*
409 : * Release blocks associated with the inode ip and stored in the indirect
410 : * block bn. Blocks are free'd in LIFO order up to (but not including)
411 : * lastbn. If level is greater than SINGLE, the block is an indirect block
412 : * and recursive calls to indirtrunc must be used to cleanse other indirect
413 : * blocks.
414 : *
415 : * NB: triple indirect blocks are untested.
416 : */
417 : static int
418 0 : ext2fs_indirtrunc(struct inode *ip, int32_t lbn, int32_t dbn, int32_t lastbn, int level, long *countp)
419 : {
420 : int i;
421 : struct buf *bp;
422 0 : struct m_ext2fs *fs = ip->i_e2fs;
423 : int32_t *bap;
424 : struct vnode *vp;
425 : int32_t *copy = NULL, nb, nlbn, last;
426 0 : long blkcount, factor;
427 : int nblocks, blocksreleased = 0;
428 : int error = 0, allerror = 0;
429 :
430 : /*
431 : * Calculate index in current block of last
432 : * block to be kept. -1 indicates the entire
433 : * block so we need not calculate the index.
434 : */
435 : factor = 1;
436 0 : for (i = SINGLE; i < level; i++)
437 0 : factor *= NINDIR(fs);
438 : last = lastbn;
439 0 : if (lastbn > 0)
440 0 : last /= factor;
441 0 : nblocks = btodb(fs->e2fs_bsize);
442 : /*
443 : * Get buffer of block pointers, zero those entries corresponding
444 : * to blocks to be free'd, and update on disk copy first. Since
445 : * double(triple) indirect before single(double) indirect, calls
446 : * to bmap on these blocks will fail. However, we already have
447 : * the on disk address, so we have to set the b_blkno field
448 : * explicitly instead of letting bread do everything for us.
449 : */
450 0 : vp = ITOV(ip);
451 0 : bp = getblk(vp, lbn, (int)fs->e2fs_bsize, 0, 0);
452 0 : if (!(bp->b_flags & (B_DONE | B_DELWRI))) {
453 0 : curproc->p_ru.ru_inblock++; /* pay for read */
454 0 : bcstats.pendingreads++;
455 0 : bcstats.numreads++;
456 0 : bp->b_flags |= B_READ;
457 0 : if (bp->b_bcount > bp->b_bufsize)
458 0 : panic("ext2fs_indirtrunc: bad buffer size");
459 0 : bp->b_blkno = dbn;
460 0 : VOP_STRATEGY(bp);
461 0 : error = biowait(bp);
462 0 : }
463 0 : if (error) {
464 0 : brelse(bp);
465 0 : *countp = 0;
466 0 : return (error);
467 : }
468 :
469 0 : bap = (int32_t *)bp->b_data;
470 0 : if (lastbn >= 0) {
471 0 : copy = malloc(fs->e2fs_bsize, M_TEMP, M_WAITOK);
472 0 : memcpy(copy, bap, fs->e2fs_bsize);
473 0 : memset(&bap[last + 1], 0,
474 : (NINDIR(fs) - (last + 1)) * sizeof(u_int32_t));
475 0 : error = bwrite(bp);
476 0 : if (error)
477 0 : allerror = error;
478 : bap = copy;
479 0 : }
480 :
481 : /*
482 : * Recursively free totally unused blocks.
483 : */
484 0 : for (i = NINDIR(fs) - 1,
485 0 : nlbn = lbn + 1 - i * factor; i > last;
486 0 : i--, nlbn += factor) {
487 0 : nb = letoh32(bap[i]);
488 0 : if (nb == 0)
489 : continue;
490 0 : if (level > SINGLE) {
491 0 : error = ext2fs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
492 0 : (int32_t)-1, level - 1,
493 : &blkcount);
494 0 : if (error)
495 0 : allerror = error;
496 0 : blocksreleased += blkcount;
497 0 : }
498 0 : ext2fs_blkfree(ip, nb);
499 0 : blocksreleased += nblocks;
500 0 : }
501 :
502 : /*
503 : * Recursively free last partial block.
504 : */
505 0 : if (level > SINGLE && lastbn >= 0) {
506 0 : last = lastbn % factor;
507 0 : nb = letoh32(bap[i]);
508 0 : if (nb != 0) {
509 0 : error = ext2fs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
510 0 : last, level - 1, &blkcount);
511 0 : if (error)
512 0 : allerror = error;
513 0 : blocksreleased += blkcount;
514 0 : }
515 : }
516 :
517 0 : if (copy != NULL) {
518 0 : free(copy, M_TEMP, fs->e2fs_bsize);
519 0 : } else {
520 0 : bp->b_flags |= B_INVAL;
521 0 : brelse(bp);
522 : }
523 :
524 0 : *countp = blocksreleased;
525 0 : return (allerror);
526 0 : }
|