truncate.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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. #include <linux/errno.h>
  31. #include <linux/fs.h>
  32. #include <linux/ufs_fs.h>
  33. #include <linux/fcntl.h>
  34. #include <linux/time.h>
  35. #include <linux/stat.h>
  36. #include <linux/string.h>
  37. #include <linux/smp_lock.h>
  38. #include <linux/buffer_head.h>
  39. #include <linux/blkdev.h>
  40. #include <linux/sched.h>
  41. #include "swab.h"
  42. #include "util.h"
  43. #undef UFS_TRUNCATE_DEBUG
  44. #ifdef UFS_TRUNCATE_DEBUG
  45. #define UFSD(x) printk("(%s, %d), %s: ", __FILE__, __LINE__, __FUNCTION__); printk x;
  46. #else
  47. #define UFSD(x)
  48. #endif
  49. /*
  50. * Secure deletion currently doesn't work. It interacts very badly
  51. * with buffers shared with memory mappings, and for that reason
  52. * can't be done in the truncate() routines. It should instead be
  53. * done separately in "release()" before calling the truncate routines
  54. * that will release the actual file blocks.
  55. *
  56. * Linus
  57. */
  58. #define DIRECT_BLOCK ((inode->i_size + uspi->s_bsize - 1) >> uspi->s_bshift)
  59. #define DIRECT_FRAGMENT ((inode->i_size + uspi->s_fsize - 1) >> uspi->s_fshift)
  60. #define DATA_BUFFER_USED(bh) \
  61. (atomic_read(&bh->b_count)>1 || buffer_locked(bh))
  62. static int ufs_trunc_direct (struct inode * inode)
  63. {
  64. struct ufs_inode_info *ufsi = UFS_I(inode);
  65. struct super_block * sb;
  66. struct ufs_sb_private_info * uspi;
  67. struct buffer_head * bh;
  68. __fs32 * p;
  69. unsigned frag1, frag2, frag3, frag4, block1, block2;
  70. unsigned frag_to_free, free_count;
  71. unsigned i, j, tmp;
  72. int retry;
  73. UFSD(("ENTER\n"))
  74. sb = inode->i_sb;
  75. uspi = UFS_SB(sb)->s_uspi;
  76. frag_to_free = 0;
  77. free_count = 0;
  78. retry = 0;
  79. frag1 = DIRECT_FRAGMENT;
  80. frag4 = min_t(u32, UFS_NDIR_FRAGMENT, ufsi->i_lastfrag);
  81. frag2 = ((frag1 & uspi->s_fpbmask) ? ((frag1 | uspi->s_fpbmask) + 1) : frag1);
  82. frag3 = frag4 & ~uspi->s_fpbmask;
  83. block1 = block2 = 0;
  84. if (frag2 > frag3) {
  85. frag2 = frag4;
  86. frag3 = frag4 = 0;
  87. }
  88. else if (frag2 < frag3) {
  89. block1 = ufs_fragstoblks (frag2);
  90. block2 = ufs_fragstoblks (frag3);
  91. }
  92. UFSD(("frag1 %u, frag2 %u, block1 %u, block2 %u, frag3 %u, frag4 %u\n", frag1, frag2, block1, block2, frag3, frag4))
  93. if (frag1 >= frag2)
  94. goto next1;
  95. /*
  96. * Free first free fragments
  97. */
  98. p = ufsi->i_u1.i_data + ufs_fragstoblks (frag1);
  99. tmp = fs32_to_cpu(sb, *p);
  100. if (!tmp )
  101. ufs_panic (sb, "ufs_trunc_direct", "internal error");
  102. frag1 = ufs_fragnum (frag1);
  103. frag2 = ufs_fragnum (frag2);
  104. for (j = frag1; j < frag2; j++) {
  105. bh = sb_find_get_block (sb, tmp + j);
  106. if ((bh && DATA_BUFFER_USED(bh)) || tmp != fs32_to_cpu(sb, *p)) {
  107. retry = 1;
  108. brelse (bh);
  109. goto next1;
  110. }
  111. bforget (bh);
  112. }
  113. inode->i_blocks -= (frag2-frag1) << uspi->s_nspfshift;
  114. mark_inode_dirty(inode);
  115. ufs_free_fragments (inode, tmp + frag1, frag2 - frag1);
  116. frag_to_free = tmp + frag1;
  117. next1:
  118. /*
  119. * Free whole blocks
  120. */
  121. for (i = block1 ; i < block2; i++) {
  122. p = ufsi->i_u1.i_data + i;
  123. tmp = fs32_to_cpu(sb, *p);
  124. if (!tmp)
  125. continue;
  126. for (j = 0; j < uspi->s_fpb; j++) {
  127. bh = sb_find_get_block(sb, tmp + j);
  128. if ((bh && DATA_BUFFER_USED(bh)) || tmp != fs32_to_cpu(sb, *p)) {
  129. retry = 1;
  130. brelse (bh);
  131. goto next2;
  132. }
  133. bforget (bh);
  134. }
  135. *p = 0;
  136. inode->i_blocks -= uspi->s_nspb;
  137. mark_inode_dirty(inode);
  138. if (free_count == 0) {
  139. frag_to_free = tmp;
  140. free_count = uspi->s_fpb;
  141. } else if (free_count > 0 && frag_to_free == tmp - free_count)
  142. free_count += uspi->s_fpb;
  143. else {
  144. ufs_free_blocks (inode, frag_to_free, free_count);
  145. frag_to_free = tmp;
  146. free_count = uspi->s_fpb;
  147. }
  148. next2:;
  149. }
  150. if (free_count > 0)
  151. ufs_free_blocks (inode, frag_to_free, free_count);
  152. if (frag3 >= frag4)
  153. goto next3;
  154. /*
  155. * Free last free fragments
  156. */
  157. p = ufsi->i_u1.i_data + ufs_fragstoblks (frag3);
  158. tmp = fs32_to_cpu(sb, *p);
  159. if (!tmp )
  160. ufs_panic(sb, "ufs_truncate_direct", "internal error");
  161. frag4 = ufs_fragnum (frag4);
  162. for (j = 0; j < frag4; j++) {
  163. bh = sb_find_get_block (sb, tmp + j);
  164. if ((bh && DATA_BUFFER_USED(bh)) || tmp != fs32_to_cpu(sb, *p)) {
  165. retry = 1;
  166. brelse (bh);
  167. goto next1;
  168. }
  169. bforget (bh);
  170. }
  171. *p = 0;
  172. inode->i_blocks -= frag4 << uspi->s_nspfshift;
  173. mark_inode_dirty(inode);
  174. ufs_free_fragments (inode, tmp, frag4);
  175. next3:
  176. UFSD(("EXIT\n"))
  177. return retry;
  178. }
  179. static int ufs_trunc_indirect (struct inode * inode, unsigned offset, __fs32 *p)
  180. {
  181. struct super_block * sb;
  182. struct ufs_sb_private_info * uspi;
  183. struct ufs_buffer_head * ind_ubh;
  184. struct buffer_head * bh;
  185. __fs32 * ind;
  186. unsigned indirect_block, i, j, tmp;
  187. unsigned frag_to_free, free_count;
  188. int retry;
  189. UFSD(("ENTER\n"))
  190. sb = inode->i_sb;
  191. uspi = UFS_SB(sb)->s_uspi;
  192. frag_to_free = 0;
  193. free_count = 0;
  194. retry = 0;
  195. tmp = fs32_to_cpu(sb, *p);
  196. if (!tmp)
  197. return 0;
  198. ind_ubh = ubh_bread(sb, tmp, uspi->s_bsize);
  199. if (tmp != fs32_to_cpu(sb, *p)) {
  200. ubh_brelse (ind_ubh);
  201. return 1;
  202. }
  203. if (!ind_ubh) {
  204. *p = 0;
  205. return 0;
  206. }
  207. indirect_block = (DIRECT_BLOCK > offset) ? (DIRECT_BLOCK - offset) : 0;
  208. for (i = indirect_block; i < uspi->s_apb; i++) {
  209. ind = ubh_get_addr32 (ind_ubh, i);
  210. tmp = fs32_to_cpu(sb, *ind);
  211. if (!tmp)
  212. continue;
  213. for (j = 0; j < uspi->s_fpb; j++) {
  214. bh = sb_find_get_block(sb, tmp + j);
  215. if ((bh && DATA_BUFFER_USED(bh)) || tmp != fs32_to_cpu(sb, *ind)) {
  216. retry = 1;
  217. brelse (bh);
  218. goto next;
  219. }
  220. bforget (bh);
  221. }
  222. *ind = 0;
  223. ubh_mark_buffer_dirty(ind_ubh);
  224. if (free_count == 0) {
  225. frag_to_free = tmp;
  226. free_count = uspi->s_fpb;
  227. } else if (free_count > 0 && frag_to_free == tmp - free_count)
  228. free_count += uspi->s_fpb;
  229. else {
  230. ufs_free_blocks (inode, frag_to_free, free_count);
  231. frag_to_free = tmp;
  232. free_count = uspi->s_fpb;
  233. }
  234. inode->i_blocks -= uspi->s_nspb;
  235. mark_inode_dirty(inode);
  236. next:;
  237. }
  238. if (free_count > 0) {
  239. ufs_free_blocks (inode, frag_to_free, free_count);
  240. }
  241. for (i = 0; i < uspi->s_apb; i++)
  242. if (*ubh_get_addr32(ind_ubh,i))
  243. break;
  244. if (i >= uspi->s_apb) {
  245. if (ubh_max_bcount(ind_ubh) != 1) {
  246. retry = 1;
  247. }
  248. else {
  249. tmp = fs32_to_cpu(sb, *p);
  250. *p = 0;
  251. inode->i_blocks -= uspi->s_nspb;
  252. mark_inode_dirty(inode);
  253. ufs_free_blocks (inode, tmp, uspi->s_fpb);
  254. ubh_bforget(ind_ubh);
  255. ind_ubh = NULL;
  256. }
  257. }
  258. if (IS_SYNC(inode) && ind_ubh && ubh_buffer_dirty(ind_ubh)) {
  259. ubh_ll_rw_block (SWRITE, 1, &ind_ubh);
  260. ubh_wait_on_buffer (ind_ubh);
  261. }
  262. ubh_brelse (ind_ubh);
  263. UFSD(("EXIT\n"))
  264. return retry;
  265. }
  266. static int ufs_trunc_dindirect (struct inode *inode, unsigned offset, __fs32 *p)
  267. {
  268. struct super_block * sb;
  269. struct ufs_sb_private_info * uspi;
  270. struct ufs_buffer_head * dind_bh;
  271. unsigned i, tmp, dindirect_block;
  272. __fs32 * dind;
  273. int retry = 0;
  274. UFSD(("ENTER\n"))
  275. sb = inode->i_sb;
  276. uspi = UFS_SB(sb)->s_uspi;
  277. dindirect_block = (DIRECT_BLOCK > offset)
  278. ? ((DIRECT_BLOCK - offset) >> uspi->s_apbshift) : 0;
  279. retry = 0;
  280. tmp = fs32_to_cpu(sb, *p);
  281. if (!tmp)
  282. return 0;
  283. dind_bh = ubh_bread(sb, tmp, uspi->s_bsize);
  284. if (tmp != fs32_to_cpu(sb, *p)) {
  285. ubh_brelse (dind_bh);
  286. return 1;
  287. }
  288. if (!dind_bh) {
  289. *p = 0;
  290. return 0;
  291. }
  292. for (i = dindirect_block ; i < uspi->s_apb ; i++) {
  293. dind = ubh_get_addr32 (dind_bh, i);
  294. tmp = fs32_to_cpu(sb, *dind);
  295. if (!tmp)
  296. continue;
  297. retry |= ufs_trunc_indirect (inode, offset + (i << uspi->s_apbshift), dind);
  298. ubh_mark_buffer_dirty(dind_bh);
  299. }
  300. for (i = 0; i < uspi->s_apb; i++)
  301. if (*ubh_get_addr32 (dind_bh, i))
  302. break;
  303. if (i >= uspi->s_apb) {
  304. if (ubh_max_bcount(dind_bh) != 1)
  305. retry = 1;
  306. else {
  307. tmp = fs32_to_cpu(sb, *p);
  308. *p = 0;
  309. inode->i_blocks -= uspi->s_nspb;
  310. mark_inode_dirty(inode);
  311. ufs_free_blocks (inode, tmp, uspi->s_fpb);
  312. ubh_bforget(dind_bh);
  313. dind_bh = NULL;
  314. }
  315. }
  316. if (IS_SYNC(inode) && dind_bh && ubh_buffer_dirty(dind_bh)) {
  317. ubh_ll_rw_block (SWRITE, 1, &dind_bh);
  318. ubh_wait_on_buffer (dind_bh);
  319. }
  320. ubh_brelse (dind_bh);
  321. UFSD(("EXIT\n"))
  322. return retry;
  323. }
  324. static int ufs_trunc_tindirect (struct inode * inode)
  325. {
  326. struct ufs_inode_info *ufsi = UFS_I(inode);
  327. struct super_block * sb;
  328. struct ufs_sb_private_info * uspi;
  329. struct ufs_buffer_head * tind_bh;
  330. unsigned tindirect_block, tmp, i;
  331. __fs32 * tind, * p;
  332. int retry;
  333. UFSD(("ENTER\n"))
  334. sb = inode->i_sb;
  335. uspi = UFS_SB(sb)->s_uspi;
  336. retry = 0;
  337. tindirect_block = (DIRECT_BLOCK > (UFS_NDADDR + uspi->s_apb + uspi->s_2apb))
  338. ? ((DIRECT_BLOCK - UFS_NDADDR - uspi->s_apb - uspi->s_2apb) >> uspi->s_2apbshift) : 0;
  339. p = ufsi->i_u1.i_data + UFS_TIND_BLOCK;
  340. if (!(tmp = fs32_to_cpu(sb, *p)))
  341. return 0;
  342. tind_bh = ubh_bread (sb, tmp, uspi->s_bsize);
  343. if (tmp != fs32_to_cpu(sb, *p)) {
  344. ubh_brelse (tind_bh);
  345. return 1;
  346. }
  347. if (!tind_bh) {
  348. *p = 0;
  349. return 0;
  350. }
  351. for (i = tindirect_block ; i < uspi->s_apb ; i++) {
  352. tind = ubh_get_addr32 (tind_bh, i);
  353. retry |= ufs_trunc_dindirect(inode, UFS_NDADDR +
  354. uspi->s_apb + ((i + 1) << uspi->s_2apbshift), tind);
  355. ubh_mark_buffer_dirty(tind_bh);
  356. }
  357. for (i = 0; i < uspi->s_apb; i++)
  358. if (*ubh_get_addr32 (tind_bh, i))
  359. break;
  360. if (i >= uspi->s_apb) {
  361. if (ubh_max_bcount(tind_bh) != 1)
  362. retry = 1;
  363. else {
  364. tmp = fs32_to_cpu(sb, *p);
  365. *p = 0;
  366. inode->i_blocks -= uspi->s_nspb;
  367. mark_inode_dirty(inode);
  368. ufs_free_blocks (inode, tmp, uspi->s_fpb);
  369. ubh_bforget(tind_bh);
  370. tind_bh = NULL;
  371. }
  372. }
  373. if (IS_SYNC(inode) && tind_bh && ubh_buffer_dirty(tind_bh)) {
  374. ubh_ll_rw_block (SWRITE, 1, &tind_bh);
  375. ubh_wait_on_buffer (tind_bh);
  376. }
  377. ubh_brelse (tind_bh);
  378. UFSD(("EXIT\n"))
  379. return retry;
  380. }
  381. void ufs_truncate (struct inode * inode)
  382. {
  383. struct ufs_inode_info *ufsi = UFS_I(inode);
  384. struct super_block * sb;
  385. struct ufs_sb_private_info * uspi;
  386. struct buffer_head * bh;
  387. unsigned offset;
  388. int err, retry;
  389. UFSD(("ENTER\n"))
  390. sb = inode->i_sb;
  391. uspi = UFS_SB(sb)->s_uspi;
  392. if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)))
  393. return;
  394. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  395. return;
  396. lock_kernel();
  397. while (1) {
  398. retry = ufs_trunc_direct(inode);
  399. retry |= ufs_trunc_indirect (inode, UFS_IND_BLOCK,
  400. (__fs32 *) &ufsi->i_u1.i_data[UFS_IND_BLOCK]);
  401. retry |= ufs_trunc_dindirect (inode, UFS_IND_BLOCK + uspi->s_apb,
  402. (__fs32 *) &ufsi->i_u1.i_data[UFS_DIND_BLOCK]);
  403. retry |= ufs_trunc_tindirect (inode);
  404. if (!retry)
  405. break;
  406. if (IS_SYNC(inode) && (inode->i_state & I_DIRTY))
  407. ufs_sync_inode (inode);
  408. blk_run_address_space(inode->i_mapping);
  409. yield();
  410. }
  411. offset = inode->i_size & uspi->s_fshift;
  412. if (offset) {
  413. bh = ufs_bread (inode, inode->i_size >> uspi->s_fshift, 0, &err);
  414. if (bh) {
  415. memset (bh->b_data + offset, 0, uspi->s_fsize - offset);
  416. mark_buffer_dirty (bh);
  417. brelse (bh);
  418. }
  419. }
  420. inode->i_mtime = inode->i_ctime = CURRENT_TIME_SEC;
  421. ufsi->i_lastfrag = DIRECT_FRAGMENT;
  422. unlock_kernel();
  423. mark_inode_dirty(inode);
  424. UFSD(("EXIT\n"))
  425. }