truncate.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /*
  2. * truncate.c
  3. *
  4. * PURPOSE
  5. * Truncate handling routines for the OSTA-UDF(tm) filesystem.
  6. *
  7. * COPYRIGHT
  8. * This file is distributed under the terms of the GNU General Public
  9. * License (GPL). Copies of the GPL can be obtained from:
  10. * ftp://prep.ai.mit.edu/pub/gnu/GPL
  11. * Each contributing author retains all rights to their own work.
  12. *
  13. * (C) 1999-2004 Ben Fennema
  14. * (C) 1999 Stelias Computing Inc
  15. *
  16. * HISTORY
  17. *
  18. * 02/24/99 blf Created.
  19. *
  20. */
  21. #include "udfdecl.h"
  22. #include <linux/fs.h>
  23. #include <linux/mm.h>
  24. #include <linux/udf_fs.h>
  25. #include <linux/buffer_head.h>
  26. #include "udf_i.h"
  27. #include "udf_sb.h"
  28. static void extent_trunc(struct inode *inode, struct extent_position *epos,
  29. kernel_lb_addr eloc, int8_t etype, uint32_t elen,
  30. uint32_t nelen)
  31. {
  32. kernel_lb_addr neloc = {};
  33. int last_block = (elen + inode->i_sb->s_blocksize - 1) >>
  34. inode->i_sb->s_blocksize_bits;
  35. int first_block = (nelen + inode->i_sb->s_blocksize - 1) >>
  36. inode->i_sb->s_blocksize_bits;
  37. if (nelen) {
  38. if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
  39. udf_free_blocks(inode->i_sb, inode, eloc, 0,
  40. last_block);
  41. etype = (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30);
  42. } else
  43. neloc = eloc;
  44. nelen = (etype << 30) | nelen;
  45. }
  46. if (elen != nelen) {
  47. udf_write_aext(inode, epos, neloc, nelen, 0);
  48. if (last_block - first_block > 0) {
  49. if (etype == (EXT_RECORDED_ALLOCATED >> 30))
  50. mark_inode_dirty(inode);
  51. if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
  52. udf_free_blocks(inode->i_sb, inode, eloc,
  53. first_block,
  54. last_block - first_block);
  55. }
  56. }
  57. }
  58. /*
  59. * Truncate the last extent to match i_size. This function assumes
  60. * that preallocation extent is already truncated.
  61. */
  62. void udf_truncate_tail_extent(struct inode *inode)
  63. {
  64. struct extent_position epos = {};
  65. kernel_lb_addr eloc;
  66. uint32_t elen, nelen;
  67. uint64_t lbcount = 0;
  68. int8_t etype = -1, netype;
  69. int adsize;
  70. struct udf_inode_info *iinfo = UDF_I(inode);
  71. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB ||
  72. inode->i_size == iinfo->i_lenExtents)
  73. return;
  74. /* Are we going to delete the file anyway? */
  75. if (inode->i_nlink == 0)
  76. return;
  77. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
  78. adsize = sizeof(short_ad);
  79. else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
  80. adsize = sizeof(long_ad);
  81. else
  82. BUG();
  83. /* Find the last extent in the file */
  84. while ((netype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
  85. etype = netype;
  86. lbcount += elen;
  87. if (lbcount > inode->i_size) {
  88. if (lbcount - inode->i_size >= inode->i_sb->s_blocksize)
  89. printk(KERN_WARNING
  90. "udf_truncate_tail_extent(): Too long "
  91. "extent after EOF in inode %u: i_size: "
  92. "%Ld lbcount: %Ld extent %u+%u\n",
  93. (unsigned)inode->i_ino,
  94. (long long)inode->i_size,
  95. (long long)lbcount,
  96. (unsigned)eloc.logicalBlockNum,
  97. (unsigned)elen);
  98. nelen = elen - (lbcount - inode->i_size);
  99. epos.offset -= adsize;
  100. extent_trunc(inode, &epos, eloc, etype, elen, nelen);
  101. epos.offset += adsize;
  102. if (udf_next_aext(inode, &epos, &eloc, &elen, 1) != -1)
  103. printk(KERN_ERR "udf_truncate_tail_extent(): "
  104. "Extent after EOF in inode %u.\n",
  105. (unsigned)inode->i_ino);
  106. break;
  107. }
  108. }
  109. /* This inode entry is in-memory only and thus we don't have to mark
  110. * the inode dirty */
  111. iinfo->i_lenExtents = inode->i_size;
  112. brelse(epos.bh);
  113. }
  114. void udf_discard_prealloc(struct inode *inode)
  115. {
  116. struct extent_position epos = { NULL, 0, {0, 0} };
  117. kernel_lb_addr eloc;
  118. uint32_t elen;
  119. uint64_t lbcount = 0;
  120. int8_t etype = -1, netype;
  121. int adsize;
  122. struct udf_inode_info *iinfo = UDF_I(inode);
  123. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB ||
  124. inode->i_size == iinfo->i_lenExtents)
  125. return;
  126. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
  127. adsize = sizeof(short_ad);
  128. else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
  129. adsize = sizeof(long_ad);
  130. else
  131. adsize = 0;
  132. epos.block = iinfo->i_location;
  133. /* Find the last extent in the file */
  134. while ((netype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
  135. etype = netype;
  136. lbcount += elen;
  137. }
  138. if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
  139. epos.offset -= adsize;
  140. lbcount -= elen;
  141. extent_trunc(inode, &epos, eloc, etype, elen, 0);
  142. if (!epos.bh) {
  143. iinfo->i_lenAlloc =
  144. epos.offset -
  145. udf_file_entry_alloc_offset(inode);
  146. mark_inode_dirty(inode);
  147. } else {
  148. struct allocExtDesc *aed =
  149. (struct allocExtDesc *)(epos.bh->b_data);
  150. aed->lengthAllocDescs =
  151. cpu_to_le32(epos.offset -
  152. sizeof(struct allocExtDesc));
  153. if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
  154. UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
  155. udf_update_tag(epos.bh->b_data, epos.offset);
  156. else
  157. udf_update_tag(epos.bh->b_data,
  158. sizeof(struct allocExtDesc));
  159. mark_buffer_dirty_inode(epos.bh, inode);
  160. }
  161. }
  162. /* This inode entry is in-memory only and thus we don't have to mark
  163. * the inode dirty */
  164. iinfo->i_lenExtents = lbcount;
  165. brelse(epos.bh);
  166. }
  167. void udf_truncate_extents(struct inode *inode)
  168. {
  169. struct extent_position epos;
  170. kernel_lb_addr eloc, neloc = {};
  171. uint32_t elen, nelen = 0, indirect_ext_len = 0, lenalloc;
  172. int8_t etype;
  173. struct super_block *sb = inode->i_sb;
  174. struct udf_sb_info *sbi = UDF_SB(sb);
  175. sector_t first_block = inode->i_size >> sb->s_blocksize_bits, offset;
  176. loff_t byte_offset;
  177. int adsize;
  178. struct udf_inode_info *iinfo = UDF_I(inode);
  179. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
  180. adsize = sizeof(short_ad);
  181. else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
  182. adsize = sizeof(long_ad);
  183. else
  184. BUG();
  185. etype = inode_bmap(inode, first_block, &epos, &eloc, &elen, &offset);
  186. byte_offset = (offset << sb->s_blocksize_bits) +
  187. (inode->i_size & (sb->s_blocksize - 1));
  188. if (etype != -1) {
  189. epos.offset -= adsize;
  190. extent_trunc(inode, &epos, eloc, etype, elen, byte_offset);
  191. epos.offset += adsize;
  192. if (byte_offset)
  193. lenalloc = epos.offset;
  194. else
  195. lenalloc = epos.offset - adsize;
  196. if (!epos.bh)
  197. lenalloc -= udf_file_entry_alloc_offset(inode);
  198. else
  199. lenalloc -= sizeof(struct allocExtDesc);
  200. while ((etype = udf_current_aext(inode, &epos, &eloc,
  201. &elen, 0)) != -1) {
  202. if (etype == (EXT_NEXT_EXTENT_ALLOCDECS >> 30)) {
  203. udf_write_aext(inode, &epos, neloc, nelen, 0);
  204. if (indirect_ext_len) {
  205. /* We managed to free all extents in the
  206. * indirect extent - free it too */
  207. if (!epos.bh)
  208. BUG();
  209. udf_free_blocks(sb, inode, epos.block,
  210. 0, indirect_ext_len);
  211. } else {
  212. if (!epos.bh) {
  213. iinfo->i_lenAlloc =
  214. lenalloc;
  215. mark_inode_dirty(inode);
  216. } else {
  217. struct allocExtDesc *aed =
  218. (struct allocExtDesc *)
  219. (epos.bh->b_data);
  220. int len =
  221. sizeof(struct allocExtDesc);
  222. aed->lengthAllocDescs =
  223. cpu_to_le32(lenalloc);
  224. if (!UDF_QUERY_FLAG(sb,
  225. UDF_FLAG_STRICT) ||
  226. sbi->s_udfrev >= 0x0201)
  227. len += lenalloc;
  228. udf_update_tag(epos.bh->b_data,
  229. len);
  230. mark_buffer_dirty_inode(
  231. epos.bh, inode);
  232. }
  233. }
  234. brelse(epos.bh);
  235. epos.offset = sizeof(struct allocExtDesc);
  236. epos.block = eloc;
  237. epos.bh = udf_tread(sb,
  238. udf_get_lb_pblock(sb, eloc, 0));
  239. if (elen)
  240. indirect_ext_len =
  241. (elen + sb->s_blocksize - 1) >>
  242. sb->s_blocksize_bits;
  243. else
  244. indirect_ext_len = 1;
  245. } else {
  246. extent_trunc(inode, &epos, eloc, etype,
  247. elen, 0);
  248. epos.offset += adsize;
  249. }
  250. }
  251. if (indirect_ext_len) {
  252. if (!epos.bh)
  253. BUG();
  254. udf_free_blocks(sb, inode, epos.block, 0,
  255. indirect_ext_len);
  256. } else {
  257. if (!epos.bh) {
  258. iinfo->i_lenAlloc = lenalloc;
  259. mark_inode_dirty(inode);
  260. } else {
  261. struct allocExtDesc *aed =
  262. (struct allocExtDesc *)(epos.bh->b_data);
  263. aed->lengthAllocDescs = cpu_to_le32(lenalloc);
  264. if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT) ||
  265. sbi->s_udfrev >= 0x0201)
  266. udf_update_tag(epos.bh->b_data,
  267. lenalloc +
  268. sizeof(struct allocExtDesc));
  269. else
  270. udf_update_tag(epos.bh->b_data,
  271. sizeof(struct allocExtDesc));
  272. mark_buffer_dirty_inode(epos.bh, inode);
  273. }
  274. }
  275. } else if (inode->i_size) {
  276. if (byte_offset) {
  277. kernel_long_ad extent;
  278. /*
  279. * OK, there is not extent covering inode->i_size and
  280. * no extent above inode->i_size => truncate is
  281. * extending the file by 'offset' blocks.
  282. */
  283. if ((!epos.bh &&
  284. epos.offset ==
  285. udf_file_entry_alloc_offset(inode)) ||
  286. (epos.bh && epos.offset ==
  287. sizeof(struct allocExtDesc))) {
  288. /* File has no extents at all or has empty last
  289. * indirect extent! Create a fake extent... */
  290. extent.extLocation.logicalBlockNum = 0;
  291. extent.extLocation.partitionReferenceNum = 0;
  292. extent.extLength =
  293. EXT_NOT_RECORDED_NOT_ALLOCATED;
  294. } else {
  295. epos.offset -= adsize;
  296. etype = udf_next_aext(inode, &epos,
  297. &extent.extLocation,
  298. &extent.extLength, 0);
  299. extent.extLength |= etype << 30;
  300. }
  301. udf_extend_file(inode, &epos, &extent,
  302. offset +
  303. ((inode->i_size &
  304. (sb->s_blocksize - 1)) != 0));
  305. }
  306. }
  307. iinfo->i_lenExtents = inode->i_size;
  308. brelse(epos.bh);
  309. }