truncate.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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/fcntl.h>
  37. #include <linux/time.h>
  38. #include <linux/stat.h>
  39. #include <linux/string.h>
  40. #include <linux/buffer_head.h>
  41. #include <linux/blkdev.h>
  42. #include <linux/sched.h>
  43. #include "ufs_fs.h"
  44. #include "ufs.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: ino %lu\n", inode->i_ino);
  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(u64, 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("ino %lu, frag1 %llu, frag2 %llu, block1 %llu, block2 %llu,"
  87. " frag3 %llu, frag4 %llu\n", inode->i_ino,
  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: ino %lu\n", inode->i_ino);
  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_sync_block(ind_ubh);
  212. ubh_brelse (ind_ubh);
  213. UFSD("EXIT: ino %lu\n", inode->i_ino);
  214. return retry;
  215. }
  216. static int ufs_trunc_dindirect(struct inode *inode, u64 offset, void *p)
  217. {
  218. struct super_block * sb;
  219. struct ufs_sb_private_info * uspi;
  220. struct ufs_buffer_head *dind_bh;
  221. u64 i, tmp, dindirect_block;
  222. void *dind;
  223. int retry = 0;
  224. UFSD("ENTER: ino %lu\n", inode->i_ino);
  225. sb = inode->i_sb;
  226. uspi = UFS_SB(sb)->s_uspi;
  227. dindirect_block = (DIRECT_BLOCK > offset)
  228. ? ((DIRECT_BLOCK - offset) >> uspi->s_apbshift) : 0;
  229. retry = 0;
  230. tmp = ufs_data_ptr_to_cpu(sb, p);
  231. if (!tmp)
  232. return 0;
  233. dind_bh = ubh_bread(sb, tmp, uspi->s_bsize);
  234. if (tmp != ufs_data_ptr_to_cpu(sb, p)) {
  235. ubh_brelse (dind_bh);
  236. return 1;
  237. }
  238. if (!dind_bh) {
  239. ufs_data_ptr_clear(uspi, p);
  240. return 0;
  241. }
  242. for (i = dindirect_block ; i < uspi->s_apb ; i++) {
  243. dind = ubh_get_data_ptr(uspi, dind_bh, i);
  244. tmp = ufs_data_ptr_to_cpu(sb, dind);
  245. if (!tmp)
  246. continue;
  247. retry |= ufs_trunc_indirect (inode, offset + (i << uspi->s_apbshift), dind);
  248. ubh_mark_buffer_dirty(dind_bh);
  249. }
  250. for (i = 0; i < uspi->s_apb; i++)
  251. if (!ufs_is_data_ptr_zero(uspi,
  252. ubh_get_data_ptr(uspi, dind_bh, i)))
  253. break;
  254. if (i >= uspi->s_apb) {
  255. tmp = ufs_data_ptr_to_cpu(sb, p);
  256. ufs_data_ptr_clear(uspi, p);
  257. ufs_free_blocks(inode, tmp, uspi->s_fpb);
  258. mark_inode_dirty(inode);
  259. ubh_bforget(dind_bh);
  260. dind_bh = NULL;
  261. }
  262. if (IS_SYNC(inode) && dind_bh && ubh_buffer_dirty(dind_bh))
  263. ubh_sync_block(dind_bh);
  264. ubh_brelse (dind_bh);
  265. UFSD("EXIT: ino %lu\n", inode->i_ino);
  266. return retry;
  267. }
  268. static int ufs_trunc_tindirect(struct inode *inode)
  269. {
  270. struct super_block *sb = inode->i_sb;
  271. struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
  272. struct ufs_inode_info *ufsi = UFS_I(inode);
  273. struct ufs_buffer_head * tind_bh;
  274. u64 tindirect_block, tmp, i;
  275. void *tind, *p;
  276. int retry;
  277. UFSD("ENTER: ino %lu\n", inode->i_ino);
  278. retry = 0;
  279. tindirect_block = (DIRECT_BLOCK > (UFS_NDADDR + uspi->s_apb + uspi->s_2apb))
  280. ? ((DIRECT_BLOCK - UFS_NDADDR - uspi->s_apb - uspi->s_2apb) >> uspi->s_2apbshift) : 0;
  281. p = ufs_get_direct_data_ptr(uspi, ufsi, UFS_TIND_BLOCK);
  282. if (!(tmp = ufs_data_ptr_to_cpu(sb, p)))
  283. return 0;
  284. tind_bh = ubh_bread (sb, tmp, uspi->s_bsize);
  285. if (tmp != ufs_data_ptr_to_cpu(sb, p)) {
  286. ubh_brelse (tind_bh);
  287. return 1;
  288. }
  289. if (!tind_bh) {
  290. ufs_data_ptr_clear(uspi, p);
  291. return 0;
  292. }
  293. for (i = tindirect_block ; i < uspi->s_apb ; i++) {
  294. tind = ubh_get_data_ptr(uspi, tind_bh, i);
  295. retry |= ufs_trunc_dindirect(inode, UFS_NDADDR +
  296. uspi->s_apb + ((i + 1) << uspi->s_2apbshift), tind);
  297. ubh_mark_buffer_dirty(tind_bh);
  298. }
  299. for (i = 0; i < uspi->s_apb; i++)
  300. if (!ufs_is_data_ptr_zero(uspi,
  301. ubh_get_data_ptr(uspi, tind_bh, i)))
  302. break;
  303. if (i >= uspi->s_apb) {
  304. tmp = ufs_data_ptr_to_cpu(sb, p);
  305. ufs_data_ptr_clear(uspi, p);
  306. ufs_free_blocks(inode, tmp, uspi->s_fpb);
  307. mark_inode_dirty(inode);
  308. ubh_bforget(tind_bh);
  309. tind_bh = NULL;
  310. }
  311. if (IS_SYNC(inode) && tind_bh && ubh_buffer_dirty(tind_bh))
  312. ubh_sync_block(tind_bh);
  313. ubh_brelse (tind_bh);
  314. UFSD("EXIT: ino %lu\n", inode->i_ino);
  315. return retry;
  316. }
  317. static int ufs_alloc_lastblock(struct inode *inode)
  318. {
  319. int err = 0;
  320. struct super_block *sb = inode->i_sb;
  321. struct address_space *mapping = inode->i_mapping;
  322. struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
  323. unsigned i, end;
  324. sector_t lastfrag;
  325. struct page *lastpage;
  326. struct buffer_head *bh;
  327. u64 phys64;
  328. lastfrag = (i_size_read(inode) + uspi->s_fsize - 1) >> uspi->s_fshift;
  329. if (!lastfrag)
  330. goto out;
  331. lastfrag--;
  332. lastpage = ufs_get_locked_page(mapping, lastfrag >>
  333. (PAGE_CACHE_SHIFT - inode->i_blkbits));
  334. if (IS_ERR(lastpage)) {
  335. err = -EIO;
  336. goto out;
  337. }
  338. end = lastfrag & ((1 << (PAGE_CACHE_SHIFT - inode->i_blkbits)) - 1);
  339. bh = page_buffers(lastpage);
  340. for (i = 0; i < end; ++i)
  341. bh = bh->b_this_page;
  342. err = ufs_getfrag_block(inode, lastfrag, bh, 1);
  343. if (unlikely(err))
  344. goto out_unlock;
  345. if (buffer_new(bh)) {
  346. clear_buffer_new(bh);
  347. unmap_underlying_metadata(bh->b_bdev,
  348. bh->b_blocknr);
  349. /*
  350. * we do not zeroize fragment, because of
  351. * if it maped to hole, it already contains zeroes
  352. */
  353. set_buffer_uptodate(bh);
  354. mark_buffer_dirty(bh);
  355. set_page_dirty(lastpage);
  356. }
  357. if (lastfrag >= UFS_IND_FRAGMENT) {
  358. end = uspi->s_fpb - ufs_fragnum(lastfrag) - 1;
  359. phys64 = bh->b_blocknr + 1;
  360. for (i = 0; i < end; ++i) {
  361. bh = sb_getblk(sb, i + phys64);
  362. lock_buffer(bh);
  363. memset(bh->b_data, 0, sb->s_blocksize);
  364. set_buffer_uptodate(bh);
  365. mark_buffer_dirty(bh);
  366. unlock_buffer(bh);
  367. sync_dirty_buffer(bh);
  368. brelse(bh);
  369. }
  370. }
  371. out_unlock:
  372. ufs_put_locked_page(lastpage);
  373. out:
  374. return err;
  375. }
  376. int ufs_truncate(struct inode *inode, loff_t old_i_size)
  377. {
  378. struct ufs_inode_info *ufsi = UFS_I(inode);
  379. struct super_block *sb = inode->i_sb;
  380. struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
  381. int retry, err = 0;
  382. UFSD("ENTER: ino %lu, i_size: %llu, old_i_size: %llu\n",
  383. inode->i_ino, (unsigned long long)i_size_read(inode),
  384. (unsigned long long)old_i_size);
  385. if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  386. S_ISLNK(inode->i_mode)))
  387. return -EINVAL;
  388. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  389. return -EPERM;
  390. err = ufs_alloc_lastblock(inode);
  391. if (err) {
  392. i_size_write(inode, old_i_size);
  393. goto out;
  394. }
  395. block_truncate_page(inode->i_mapping, inode->i_size, ufs_getfrag_block);
  396. while (1) {
  397. retry = ufs_trunc_direct(inode);
  398. retry |= ufs_trunc_indirect(inode, UFS_IND_BLOCK,
  399. ufs_get_direct_data_ptr(uspi, ufsi,
  400. UFS_IND_BLOCK));
  401. retry |= ufs_trunc_dindirect(inode, UFS_IND_BLOCK + uspi->s_apb,
  402. ufs_get_direct_data_ptr(uspi, ufsi,
  403. UFS_DIND_BLOCK));
  404. retry |= ufs_trunc_tindirect (inode);
  405. if (!retry)
  406. break;
  407. if (IS_SYNC(inode) && (inode->i_state & I_DIRTY))
  408. ufs_sync_inode (inode);
  409. yield();
  410. }
  411. inode->i_mtime = inode->i_ctime = CURRENT_TIME_SEC;
  412. ufsi->i_lastfrag = DIRECT_FRAGMENT;
  413. mark_inode_dirty(inode);
  414. out:
  415. UFSD("EXIT: err %d\n", err);
  416. return err;
  417. }
  418. int ufs_setattr(struct dentry *dentry, struct iattr *attr)
  419. {
  420. struct inode *inode = dentry->d_inode;
  421. unsigned int ia_valid = attr->ia_valid;
  422. int error;
  423. error = inode_change_ok(inode, attr);
  424. if (error)
  425. return error;
  426. if (ia_valid & ATTR_SIZE && attr->ia_size != inode->i_size) {
  427. loff_t old_i_size = inode->i_size;
  428. /* XXX(truncate): truncate_setsize should be called last */
  429. truncate_setsize(inode, attr->ia_size);
  430. lock_ufs(inode->i_sb);
  431. error = ufs_truncate(inode, old_i_size);
  432. unlock_ufs(inode->i_sb);
  433. if (error)
  434. return error;
  435. }
  436. setattr_copy(inode, attr);
  437. mark_inode_dirty(inode);
  438. return 0;
  439. }
  440. const struct inode_operations ufs_file_inode_operations = {
  441. .setattr = ufs_setattr,
  442. };