truncate.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. /*
  2. * linux/fs/ufs/truncate.c
  3. *
  4. * Copyright (C) 1998
  5. * Daniel Pirkl <daniel.pirkl@email.cz>
  6. * Charles University, Faculty of Mathematics and Physics
  7. *
  8. * from
  9. *
  10. * linux/fs/ext2/truncate.c
  11. *
  12. * Copyright (C) 1992, 1993, 1994, 1995
  13. * Remy Card (card@masi.ibp.fr)
  14. * Laboratoire MASI - Institut Blaise Pascal
  15. * Universite Pierre et Marie Curie (Paris VI)
  16. *
  17. * from
  18. *
  19. * linux/fs/minix/truncate.c
  20. *
  21. * Copyright (C) 1991, 1992 Linus Torvalds
  22. *
  23. * Big-endian to little-endian byte-swapping/bitmaps by
  24. * David S. Miller (davem@caip.rutgers.edu), 1995
  25. */
  26. /*
  27. * Real random numbers for secure rm added 94/02/18
  28. * Idea from Pierre del Perugia <delperug@gla.ecoledoc.ibp.fr>
  29. */
  30. /*
  31. * Adoptation to use page cache and UFS2 write support by
  32. * Evgeniy Dushistov <dushistov@mail.ru>, 2006-2007
  33. */
  34. #include <linux/errno.h>
  35. #include <linux/fs.h>
  36. #include <linux/ufs_fs.h>
  37. #include <linux/fcntl.h>
  38. #include <linux/time.h>
  39. #include <linux/stat.h>
  40. #include <linux/string.h>
  41. #include <linux/smp_lock.h>
  42. #include <linux/buffer_head.h>
  43. #include <linux/blkdev.h>
  44. #include <linux/sched.h>
  45. #include "swab.h"
  46. #include "util.h"
  47. /*
  48. * Secure deletion currently doesn't work. It interacts very badly
  49. * with buffers shared with memory mappings, and for that reason
  50. * can't be done in the truncate() routines. It should instead be
  51. * done separately in "release()" before calling the truncate routines
  52. * that will release the actual file blocks.
  53. *
  54. * Linus
  55. */
  56. #define DIRECT_BLOCK ((inode->i_size + uspi->s_bsize - 1) >> uspi->s_bshift)
  57. #define DIRECT_FRAGMENT ((inode->i_size + uspi->s_fsize - 1) >> uspi->s_fshift)
  58. static int ufs_trunc_direct(struct inode *inode)
  59. {
  60. struct ufs_inode_info *ufsi = UFS_I(inode);
  61. struct super_block * sb;
  62. struct ufs_sb_private_info * uspi;
  63. void *p;
  64. u64 frag1, frag2, frag3, frag4, block1, block2;
  65. unsigned frag_to_free, free_count;
  66. unsigned i, tmp;
  67. int retry;
  68. UFSD("ENTER\n");
  69. sb = inode->i_sb;
  70. uspi = UFS_SB(sb)->s_uspi;
  71. frag_to_free = 0;
  72. free_count = 0;
  73. retry = 0;
  74. frag1 = DIRECT_FRAGMENT;
  75. frag4 = min_t(u32, UFS_NDIR_FRAGMENT, ufsi->i_lastfrag);
  76. frag2 = ((frag1 & uspi->s_fpbmask) ? ((frag1 | uspi->s_fpbmask) + 1) : frag1);
  77. frag3 = frag4 & ~uspi->s_fpbmask;
  78. block1 = block2 = 0;
  79. if (frag2 > frag3) {
  80. frag2 = frag4;
  81. frag3 = frag4 = 0;
  82. } else if (frag2 < frag3) {
  83. block1 = ufs_fragstoblks (frag2);
  84. block2 = ufs_fragstoblks (frag3);
  85. }
  86. UFSD("frag1 %llu, frag2 %llu, block1 %llu, block2 %llu, frag3 %llu,"
  87. " frag4 %llu\n",
  88. (unsigned long long)frag1, (unsigned long long)frag2,
  89. (unsigned long long)block1, (unsigned long long)block2,
  90. (unsigned long long)frag3, (unsigned long long)frag4);
  91. if (frag1 >= frag2)
  92. goto next1;
  93. /*
  94. * Free first free fragments
  95. */
  96. p = ufs_get_direct_data_ptr(uspi, ufsi, ufs_fragstoblks(frag1));
  97. tmp = ufs_data_ptr_to_cpu(sb, p);
  98. if (!tmp )
  99. ufs_panic (sb, "ufs_trunc_direct", "internal error");
  100. frag2 -= frag1;
  101. frag1 = ufs_fragnum (frag1);
  102. ufs_free_fragments(inode, tmp + frag1, frag2);
  103. mark_inode_dirty(inode);
  104. frag_to_free = tmp + frag1;
  105. next1:
  106. /*
  107. * Free whole blocks
  108. */
  109. for (i = block1 ; i < block2; i++) {
  110. p = ufs_get_direct_data_ptr(uspi, ufsi, i);
  111. tmp = ufs_data_ptr_to_cpu(sb, p);
  112. if (!tmp)
  113. continue;
  114. ufs_data_ptr_clear(uspi, p);
  115. if (free_count == 0) {
  116. frag_to_free = tmp;
  117. free_count = uspi->s_fpb;
  118. } else if (free_count > 0 && frag_to_free == tmp - free_count)
  119. free_count += uspi->s_fpb;
  120. else {
  121. ufs_free_blocks (inode, frag_to_free, free_count);
  122. frag_to_free = tmp;
  123. free_count = uspi->s_fpb;
  124. }
  125. mark_inode_dirty(inode);
  126. }
  127. if (free_count > 0)
  128. ufs_free_blocks (inode, frag_to_free, free_count);
  129. if (frag3 >= frag4)
  130. goto next3;
  131. /*
  132. * Free last free fragments
  133. */
  134. p = ufs_get_direct_data_ptr(uspi, ufsi, ufs_fragstoblks(frag3));
  135. tmp = ufs_data_ptr_to_cpu(sb, p);
  136. if (!tmp )
  137. ufs_panic(sb, "ufs_truncate_direct", "internal error");
  138. frag4 = ufs_fragnum (frag4);
  139. ufs_data_ptr_clear(uspi, p);
  140. ufs_free_fragments (inode, tmp, frag4);
  141. mark_inode_dirty(inode);
  142. next3:
  143. UFSD("EXIT\n");
  144. return retry;
  145. }
  146. static int ufs_trunc_indirect(struct inode *inode, u64 offset, void *p)
  147. {
  148. struct super_block * sb;
  149. struct ufs_sb_private_info * uspi;
  150. struct ufs_buffer_head * ind_ubh;
  151. void *ind;
  152. u64 tmp, indirect_block, i, frag_to_free;
  153. unsigned free_count;
  154. int retry;
  155. UFSD("ENTER: ino %lu, offset %llu, p: %p\n",
  156. inode->i_ino, (unsigned long long)offset, p);
  157. BUG_ON(!p);
  158. sb = inode->i_sb;
  159. uspi = UFS_SB(sb)->s_uspi;
  160. frag_to_free = 0;
  161. free_count = 0;
  162. retry = 0;
  163. tmp = ufs_data_ptr_to_cpu(sb, p);
  164. if (!tmp)
  165. return 0;
  166. ind_ubh = ubh_bread(sb, tmp, uspi->s_bsize);
  167. if (tmp != ufs_data_ptr_to_cpu(sb, p)) {
  168. ubh_brelse (ind_ubh);
  169. return 1;
  170. }
  171. if (!ind_ubh) {
  172. ufs_data_ptr_clear(uspi, p);
  173. return 0;
  174. }
  175. indirect_block = (DIRECT_BLOCK > offset) ? (DIRECT_BLOCK - offset) : 0;
  176. for (i = indirect_block; i < uspi->s_apb; i++) {
  177. ind = ubh_get_data_ptr(uspi, ind_ubh, i);
  178. tmp = ufs_data_ptr_to_cpu(sb, ind);
  179. if (!tmp)
  180. continue;
  181. ufs_data_ptr_clear(uspi, ind);
  182. ubh_mark_buffer_dirty(ind_ubh);
  183. if (free_count == 0) {
  184. frag_to_free = tmp;
  185. free_count = uspi->s_fpb;
  186. } else if (free_count > 0 && frag_to_free == tmp - free_count)
  187. free_count += uspi->s_fpb;
  188. else {
  189. ufs_free_blocks (inode, frag_to_free, free_count);
  190. frag_to_free = tmp;
  191. free_count = uspi->s_fpb;
  192. }
  193. mark_inode_dirty(inode);
  194. }
  195. if (free_count > 0) {
  196. ufs_free_blocks (inode, frag_to_free, free_count);
  197. }
  198. for (i = 0; i < uspi->s_apb; i++)
  199. if (!ufs_is_data_ptr_zero(uspi,
  200. ubh_get_data_ptr(uspi, ind_ubh, i)))
  201. break;
  202. if (i >= uspi->s_apb) {
  203. tmp = ufs_data_ptr_to_cpu(sb, p);
  204. ufs_data_ptr_clear(uspi, p);
  205. ufs_free_blocks (inode, tmp, uspi->s_fpb);
  206. mark_inode_dirty(inode);
  207. ubh_bforget(ind_ubh);
  208. ind_ubh = NULL;
  209. }
  210. if (IS_SYNC(inode) && ind_ubh && ubh_buffer_dirty(ind_ubh)) {
  211. ubh_ll_rw_block(SWRITE, ind_ubh);
  212. ubh_wait_on_buffer (ind_ubh);
  213. }
  214. ubh_brelse (ind_ubh);
  215. UFSD("EXIT\n");
  216. return retry;
  217. }
  218. static int ufs_trunc_dindirect(struct inode *inode, u64 offset, void *p)
  219. {
  220. struct super_block * sb;
  221. struct ufs_sb_private_info * uspi;
  222. struct ufs_buffer_head *dind_bh;
  223. u64 i, tmp, dindirect_block;
  224. void *dind;
  225. int retry = 0;
  226. UFSD("ENTER\n");
  227. sb = inode->i_sb;
  228. uspi = UFS_SB(sb)->s_uspi;
  229. dindirect_block = (DIRECT_BLOCK > offset)
  230. ? ((DIRECT_BLOCK - offset) >> uspi->s_apbshift) : 0;
  231. retry = 0;
  232. tmp = ufs_data_ptr_to_cpu(sb, p);
  233. if (!tmp)
  234. return 0;
  235. dind_bh = ubh_bread(sb, tmp, uspi->s_bsize);
  236. if (tmp != ufs_data_ptr_to_cpu(sb, p)) {
  237. ubh_brelse (dind_bh);
  238. return 1;
  239. }
  240. if (!dind_bh) {
  241. ufs_data_ptr_clear(uspi, p);
  242. return 0;
  243. }
  244. for (i = dindirect_block ; i < uspi->s_apb ; i++) {
  245. dind = ubh_get_data_ptr(uspi, dind_bh, i);
  246. tmp = ufs_data_ptr_to_cpu(sb, dind);
  247. if (!tmp)
  248. continue;
  249. retry |= ufs_trunc_indirect (inode, offset + (i << uspi->s_apbshift), dind);
  250. ubh_mark_buffer_dirty(dind_bh);
  251. }
  252. for (i = 0; i < uspi->s_apb; i++)
  253. if (!ufs_is_data_ptr_zero(uspi,
  254. ubh_get_data_ptr(uspi, dind_bh, i)))
  255. break;
  256. if (i >= uspi->s_apb) {
  257. tmp = ufs_data_ptr_to_cpu(sb, p);
  258. ufs_data_ptr_clear(uspi, p);
  259. ufs_free_blocks(inode, tmp, uspi->s_fpb);
  260. mark_inode_dirty(inode);
  261. ubh_bforget(dind_bh);
  262. dind_bh = NULL;
  263. }
  264. if (IS_SYNC(inode) && dind_bh && ubh_buffer_dirty(dind_bh)) {
  265. ubh_ll_rw_block(SWRITE, dind_bh);
  266. ubh_wait_on_buffer (dind_bh);
  267. }
  268. ubh_brelse (dind_bh);
  269. UFSD("EXIT\n");
  270. return retry;
  271. }
  272. static int ufs_trunc_tindirect(struct inode *inode)
  273. {
  274. struct super_block *sb = inode->i_sb;
  275. struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
  276. struct ufs_inode_info *ufsi = UFS_I(inode);
  277. struct ufs_buffer_head * tind_bh;
  278. u64 tindirect_block, tmp, i;
  279. void *tind, *p;
  280. int retry;
  281. UFSD("ENTER\n");
  282. retry = 0;
  283. tindirect_block = (DIRECT_BLOCK > (UFS_NDADDR + uspi->s_apb + uspi->s_2apb))
  284. ? ((DIRECT_BLOCK - UFS_NDADDR - uspi->s_apb - uspi->s_2apb) >> uspi->s_2apbshift) : 0;
  285. p = ufs_get_direct_data_ptr(uspi, ufsi, UFS_TIND_BLOCK);
  286. if (!(tmp = ufs_data_ptr_to_cpu(sb, p)))
  287. return 0;
  288. tind_bh = ubh_bread (sb, tmp, uspi->s_bsize);
  289. if (tmp != ufs_data_ptr_to_cpu(sb, p)) {
  290. ubh_brelse (tind_bh);
  291. return 1;
  292. }
  293. if (!tind_bh) {
  294. ufs_data_ptr_clear(uspi, p);
  295. return 0;
  296. }
  297. for (i = tindirect_block ; i < uspi->s_apb ; i++) {
  298. tind = ubh_get_addr32 (tind_bh, i);
  299. retry |= ufs_trunc_dindirect(inode, UFS_NDADDR +
  300. uspi->s_apb + ((i + 1) << uspi->s_2apbshift), tind);
  301. ubh_mark_buffer_dirty(tind_bh);
  302. }
  303. for (i = 0; i < uspi->s_apb; i++)
  304. if (!ufs_is_data_ptr_zero(uspi,
  305. ubh_get_data_ptr(uspi, tind_bh, i)))
  306. break;
  307. if (i >= uspi->s_apb) {
  308. tmp = ufs_data_ptr_to_cpu(sb, p);
  309. ufs_data_ptr_clear(uspi, p);
  310. ufs_free_blocks(inode, tmp, uspi->s_fpb);
  311. mark_inode_dirty(inode);
  312. ubh_bforget(tind_bh);
  313. tind_bh = NULL;
  314. }
  315. if (IS_SYNC(inode) && tind_bh && ubh_buffer_dirty(tind_bh)) {
  316. ubh_ll_rw_block(SWRITE, tind_bh);
  317. ubh_wait_on_buffer (tind_bh);
  318. }
  319. ubh_brelse (tind_bh);
  320. UFSD("EXIT\n");
  321. return retry;
  322. }
  323. static int ufs_alloc_lastblock(struct inode *inode)
  324. {
  325. int err = 0;
  326. struct address_space *mapping = inode->i_mapping;
  327. struct ufs_sb_private_info *uspi = UFS_SB(inode->i_sb)->s_uspi;
  328. unsigned i, end;
  329. sector_t lastfrag;
  330. struct page *lastpage;
  331. struct buffer_head *bh;
  332. lastfrag = (i_size_read(inode) + uspi->s_fsize - 1) >> uspi->s_fshift;
  333. if (!lastfrag)
  334. goto out;
  335. lastfrag--;
  336. lastpage = ufs_get_locked_page(mapping, lastfrag >>
  337. (PAGE_CACHE_SHIFT - inode->i_blkbits));
  338. if (IS_ERR(lastpage)) {
  339. err = -EIO;
  340. goto out;
  341. }
  342. end = lastfrag & ((1 << (PAGE_CACHE_SHIFT - inode->i_blkbits)) - 1);
  343. bh = page_buffers(lastpage);
  344. for (i = 0; i < end; ++i)
  345. bh = bh->b_this_page;
  346. err = ufs_getfrag_block(inode, lastfrag, bh, 1);
  347. if (unlikely(err))
  348. goto out_unlock;
  349. if (buffer_new(bh)) {
  350. clear_buffer_new(bh);
  351. unmap_underlying_metadata(bh->b_bdev,
  352. bh->b_blocknr);
  353. /*
  354. * we do not zeroize fragment, because of
  355. * if it maped to hole, it already contains zeroes
  356. */
  357. set_buffer_uptodate(bh);
  358. mark_buffer_dirty(bh);
  359. set_page_dirty(lastpage);
  360. }
  361. out_unlock:
  362. ufs_put_locked_page(lastpage);
  363. out:
  364. return err;
  365. }
  366. int ufs_truncate(struct inode *inode, loff_t old_i_size)
  367. {
  368. struct ufs_inode_info *ufsi = UFS_I(inode);
  369. struct super_block *sb = inode->i_sb;
  370. struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
  371. int retry, err = 0;
  372. UFSD("ENTER: ino %lu, i_size: %llu, old_i_size: %llu\n",
  373. inode->i_ino, (unsigned long long)i_size_read(inode),
  374. (unsigned long long)old_i_size);
  375. if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  376. S_ISLNK(inode->i_mode)))
  377. return -EINVAL;
  378. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  379. return -EPERM;
  380. err = ufs_alloc_lastblock(inode);
  381. if (err) {
  382. i_size_write(inode, old_i_size);
  383. goto out;
  384. }
  385. block_truncate_page(inode->i_mapping, inode->i_size, ufs_getfrag_block);
  386. lock_kernel();
  387. while (1) {
  388. retry = ufs_trunc_direct(inode);
  389. retry |= ufs_trunc_indirect(inode, UFS_IND_BLOCK,
  390. ufs_get_direct_data_ptr(uspi, ufsi,
  391. UFS_IND_BLOCK));
  392. retry |= ufs_trunc_dindirect(inode, UFS_IND_BLOCK + uspi->s_apb,
  393. ufs_get_direct_data_ptr(uspi, ufsi,
  394. UFS_DIND_BLOCK));
  395. retry |= ufs_trunc_tindirect (inode);
  396. if (!retry)
  397. break;
  398. if (IS_SYNC(inode) && (inode->i_state & I_DIRTY))
  399. ufs_sync_inode (inode);
  400. blk_run_address_space(inode->i_mapping);
  401. yield();
  402. }
  403. inode->i_mtime = inode->i_ctime = CURRENT_TIME_SEC;
  404. ufsi->i_lastfrag = DIRECT_FRAGMENT;
  405. unlock_kernel();
  406. mark_inode_dirty(inode);
  407. out:
  408. UFSD("EXIT: err %d\n", err);
  409. return err;
  410. }
  411. /*
  412. * We don't define our `inode->i_op->truncate', and call it here,
  413. * because of:
  414. * - there is no way to know old size
  415. * - there is no way inform user about error, if it happens in `truncate'
  416. */
  417. static int ufs_setattr(struct dentry *dentry, struct iattr *attr)
  418. {
  419. struct inode *inode = dentry->d_inode;
  420. unsigned int ia_valid = attr->ia_valid;
  421. int error;
  422. error = inode_change_ok(inode, attr);
  423. if (error)
  424. return error;
  425. if (ia_valid & ATTR_SIZE &&
  426. attr->ia_size != i_size_read(inode)) {
  427. loff_t old_i_size = inode->i_size;
  428. error = vmtruncate(inode, attr->ia_size);
  429. if (error)
  430. return error;
  431. error = ufs_truncate(inode, old_i_size);
  432. if (error)
  433. return error;
  434. }
  435. return inode_setattr(inode, attr);
  436. }
  437. const struct inode_operations ufs_file_inode_operations = {
  438. .setattr = ufs_setattr,
  439. };