truncate.c 9.8 KB

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