truncate.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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. if (ubh_max_bcount(ind_ubh) != 1) {
  208. retry = 1;
  209. }
  210. else {
  211. tmp = fs32_to_cpu(sb, *p);
  212. *p = 0;
  213. inode->i_blocks -= uspi->s_nspb;
  214. mark_inode_dirty(inode);
  215. ufs_free_blocks (inode, tmp, uspi->s_fpb);
  216. ubh_bforget(ind_ubh);
  217. ind_ubh = NULL;
  218. }
  219. }
  220. if (IS_SYNC(inode) && ind_ubh && ubh_buffer_dirty(ind_ubh)) {
  221. ubh_ll_rw_block (SWRITE, 1, &ind_ubh);
  222. ubh_wait_on_buffer (ind_ubh);
  223. }
  224. ubh_brelse (ind_ubh);
  225. UFSD(("EXIT\n"))
  226. return retry;
  227. }
  228. static int ufs_trunc_dindirect (struct inode *inode, unsigned offset, __fs32 *p)
  229. {
  230. struct super_block * sb;
  231. struct ufs_sb_private_info * uspi;
  232. struct ufs_buffer_head * dind_bh;
  233. unsigned i, tmp, dindirect_block;
  234. __fs32 * dind;
  235. int retry = 0;
  236. UFSD(("ENTER\n"))
  237. sb = inode->i_sb;
  238. uspi = UFS_SB(sb)->s_uspi;
  239. dindirect_block = (DIRECT_BLOCK > offset)
  240. ? ((DIRECT_BLOCK - offset) >> uspi->s_apbshift) : 0;
  241. retry = 0;
  242. tmp = fs32_to_cpu(sb, *p);
  243. if (!tmp)
  244. return 0;
  245. dind_bh = ubh_bread(sb, tmp, uspi->s_bsize);
  246. if (tmp != fs32_to_cpu(sb, *p)) {
  247. ubh_brelse (dind_bh);
  248. return 1;
  249. }
  250. if (!dind_bh) {
  251. *p = 0;
  252. return 0;
  253. }
  254. for (i = dindirect_block ; i < uspi->s_apb ; i++) {
  255. dind = ubh_get_addr32 (dind_bh, i);
  256. tmp = fs32_to_cpu(sb, *dind);
  257. if (!tmp)
  258. continue;
  259. retry |= ufs_trunc_indirect (inode, offset + (i << uspi->s_apbshift), dind);
  260. ubh_mark_buffer_dirty(dind_bh);
  261. }
  262. for (i = 0; i < uspi->s_apb; i++)
  263. if (*ubh_get_addr32 (dind_bh, i))
  264. break;
  265. if (i >= uspi->s_apb) {
  266. if (ubh_max_bcount(dind_bh) != 1)
  267. retry = 1;
  268. else {
  269. tmp = fs32_to_cpu(sb, *p);
  270. *p = 0;
  271. inode->i_blocks -= uspi->s_nspb;
  272. mark_inode_dirty(inode);
  273. ufs_free_blocks (inode, tmp, uspi->s_fpb);
  274. ubh_bforget(dind_bh);
  275. dind_bh = NULL;
  276. }
  277. }
  278. if (IS_SYNC(inode) && dind_bh && ubh_buffer_dirty(dind_bh)) {
  279. ubh_ll_rw_block (SWRITE, 1, &dind_bh);
  280. ubh_wait_on_buffer (dind_bh);
  281. }
  282. ubh_brelse (dind_bh);
  283. UFSD(("EXIT\n"))
  284. return retry;
  285. }
  286. static int ufs_trunc_tindirect (struct inode * inode)
  287. {
  288. struct ufs_inode_info *ufsi = UFS_I(inode);
  289. struct super_block * sb;
  290. struct ufs_sb_private_info * uspi;
  291. struct ufs_buffer_head * tind_bh;
  292. unsigned tindirect_block, tmp, i;
  293. __fs32 * tind, * p;
  294. int retry;
  295. UFSD(("ENTER\n"))
  296. sb = inode->i_sb;
  297. uspi = UFS_SB(sb)->s_uspi;
  298. retry = 0;
  299. tindirect_block = (DIRECT_BLOCK > (UFS_NDADDR + uspi->s_apb + uspi->s_2apb))
  300. ? ((DIRECT_BLOCK - UFS_NDADDR - uspi->s_apb - uspi->s_2apb) >> uspi->s_2apbshift) : 0;
  301. p = ufsi->i_u1.i_data + UFS_TIND_BLOCK;
  302. if (!(tmp = fs32_to_cpu(sb, *p)))
  303. return 0;
  304. tind_bh = ubh_bread (sb, tmp, uspi->s_bsize);
  305. if (tmp != fs32_to_cpu(sb, *p)) {
  306. ubh_brelse (tind_bh);
  307. return 1;
  308. }
  309. if (!tind_bh) {
  310. *p = 0;
  311. return 0;
  312. }
  313. for (i = tindirect_block ; i < uspi->s_apb ; i++) {
  314. tind = ubh_get_addr32 (tind_bh, i);
  315. retry |= ufs_trunc_dindirect(inode, UFS_NDADDR +
  316. uspi->s_apb + ((i + 1) << uspi->s_2apbshift), tind);
  317. ubh_mark_buffer_dirty(tind_bh);
  318. }
  319. for (i = 0; i < uspi->s_apb; i++)
  320. if (*ubh_get_addr32 (tind_bh, i))
  321. break;
  322. if (i >= uspi->s_apb) {
  323. if (ubh_max_bcount(tind_bh) != 1)
  324. retry = 1;
  325. else {
  326. tmp = fs32_to_cpu(sb, *p);
  327. *p = 0;
  328. inode->i_blocks -= uspi->s_nspb;
  329. mark_inode_dirty(inode);
  330. ufs_free_blocks (inode, tmp, uspi->s_fpb);
  331. ubh_bforget(tind_bh);
  332. tind_bh = NULL;
  333. }
  334. }
  335. if (IS_SYNC(inode) && tind_bh && ubh_buffer_dirty(tind_bh)) {
  336. ubh_ll_rw_block (SWRITE, 1, &tind_bh);
  337. ubh_wait_on_buffer (tind_bh);
  338. }
  339. ubh_brelse (tind_bh);
  340. UFSD(("EXIT\n"))
  341. return retry;
  342. }
  343. void ufs_truncate (struct inode * inode)
  344. {
  345. struct ufs_inode_info *ufsi = UFS_I(inode);
  346. struct super_block * sb;
  347. struct ufs_sb_private_info * uspi;
  348. int retry;
  349. UFSD(("ENTER\n"))
  350. sb = inode->i_sb;
  351. uspi = UFS_SB(sb)->s_uspi;
  352. if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)))
  353. return;
  354. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  355. return;
  356. block_truncate_page(inode->i_mapping, inode->i_size, ufs_getfrag_block);
  357. lock_kernel();
  358. while (1) {
  359. retry = ufs_trunc_direct(inode);
  360. retry |= ufs_trunc_indirect (inode, UFS_IND_BLOCK,
  361. (__fs32 *) &ufsi->i_u1.i_data[UFS_IND_BLOCK]);
  362. retry |= ufs_trunc_dindirect (inode, UFS_IND_BLOCK + uspi->s_apb,
  363. (__fs32 *) &ufsi->i_u1.i_data[UFS_DIND_BLOCK]);
  364. retry |= ufs_trunc_tindirect (inode);
  365. if (!retry)
  366. break;
  367. if (IS_SYNC(inode) && (inode->i_state & I_DIRTY))
  368. ufs_sync_inode (inode);
  369. blk_run_address_space(inode->i_mapping);
  370. yield();
  371. }
  372. inode->i_mtime = inode->i_ctime = CURRENT_TIME_SEC;
  373. ufsi->i_lastfrag = DIRECT_FRAGMENT;
  374. unlock_kernel();
  375. mark_inode_dirty(inode);
  376. UFSD(("EXIT\n"))
  377. }