ext2fs_inode.c 15 KB

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