truncate.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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/buffer_head.h>
  25. #include "udf_i.h"
  26. #include "udf_sb.h"
  27. static void extent_trunc(struct inode *inode, struct extent_position *epos,
  28. struct kernel_lb_addr *eloc, int8_t etype, uint32_t elen,
  29. uint32_t nelen)
  30. {
  31. struct kernel_lb_addr neloc = {};
  32. int last_block = (elen + inode->i_sb->s_blocksize - 1) >>
  33. inode->i_sb->s_blocksize_bits;
  34. int first_block = (nelen + inode->i_sb->s_blocksize - 1) >>
  35. inode->i_sb->s_blocksize_bits;
  36. if (nelen) {
  37. if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
  38. udf_free_blocks(inode->i_sb, inode, eloc, 0,
  39. last_block);
  40. etype = (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30);
  41. } else
  42. neloc = *eloc;
  43. nelen = (etype << 30) | nelen;
  44. }
  45. if (elen != nelen) {
  46. udf_write_aext(inode, epos, &neloc, nelen, 0);
  47. if (last_block - first_block > 0) {
  48. if (etype == (EXT_RECORDED_ALLOCATED >> 30))
  49. mark_inode_dirty(inode);
  50. if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
  51. udf_free_blocks(inode->i_sb, inode, eloc,
  52. first_block,
  53. last_block - first_block);
  54. }
  55. }
  56. }
  57. /*
  58. * Truncate the last extent to match i_size. This function assumes
  59. * that preallocation extent is already truncated.
  60. */
  61. void udf_truncate_tail_extent(struct inode *inode)
  62. {
  63. struct extent_position epos = {};
  64. struct kernel_lb_addr eloc;
  65. uint32_t elen, nelen;
  66. uint64_t lbcount = 0;
  67. int8_t etype = -1, netype;
  68. int adsize;
  69. struct udf_inode_info *iinfo = UDF_I(inode);
  70. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB ||
  71. inode->i_size == iinfo->i_lenExtents)
  72. return;
  73. /* Are we going to delete the file anyway? */
  74. if (inode->i_nlink == 0)
  75. return;
  76. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
  77. adsize = sizeof(struct short_ad);
  78. else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
  79. adsize = sizeof(struct long_ad);
  80. else
  81. BUG();
  82. /* Find the last extent in the file */
  83. while ((netype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
  84. etype = netype;
  85. lbcount += elen;
  86. if (lbcount > inode->i_size) {
  87. if (lbcount - inode->i_size >= inode->i_sb->s_blocksize)
  88. udf_warn(inode->i_sb,
  89. "Too long extent after EOF in inode %u: i_size: %lld lbcount: %lld extent %u+%u\n",
  90. (unsigned)inode->i_ino,
  91. (long long)inode->i_size,
  92. (long long)lbcount,
  93. (unsigned)eloc.logicalBlockNum,
  94. (unsigned)elen);
  95. nelen = elen - (lbcount - inode->i_size);
  96. epos.offset -= adsize;
  97. extent_trunc(inode, &epos, &eloc, etype, elen, nelen);
  98. epos.offset += adsize;
  99. if (udf_next_aext(inode, &epos, &eloc, &elen, 1) != -1)
  100. udf_err(inode->i_sb,
  101. "Extent after EOF in inode %u\n",
  102. (unsigned)inode->i_ino);
  103. break;
  104. }
  105. }
  106. /* This inode entry is in-memory only and thus we don't have to mark
  107. * the inode dirty */
  108. iinfo->i_lenExtents = inode->i_size;
  109. brelse(epos.bh);
  110. }
  111. void udf_discard_prealloc(struct inode *inode)
  112. {
  113. struct extent_position epos = { NULL, 0, {0, 0} };
  114. struct kernel_lb_addr eloc;
  115. uint32_t elen;
  116. uint64_t lbcount = 0;
  117. int8_t etype = -1, netype;
  118. int adsize;
  119. struct udf_inode_info *iinfo = UDF_I(inode);
  120. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB ||
  121. inode->i_size == iinfo->i_lenExtents)
  122. return;
  123. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
  124. adsize = sizeof(struct short_ad);
  125. else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
  126. adsize = sizeof(struct long_ad);
  127. else
  128. adsize = 0;
  129. epos.block = iinfo->i_location;
  130. /* Find the last extent in the file */
  131. while ((netype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
  132. etype = netype;
  133. lbcount += elen;
  134. }
  135. if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
  136. epos.offset -= adsize;
  137. lbcount -= elen;
  138. extent_trunc(inode, &epos, &eloc, etype, elen, 0);
  139. if (!epos.bh) {
  140. iinfo->i_lenAlloc =
  141. epos.offset -
  142. udf_file_entry_alloc_offset(inode);
  143. mark_inode_dirty(inode);
  144. } else {
  145. struct allocExtDesc *aed =
  146. (struct allocExtDesc *)(epos.bh->b_data);
  147. aed->lengthAllocDescs =
  148. cpu_to_le32(epos.offset -
  149. sizeof(struct allocExtDesc));
  150. if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
  151. UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
  152. udf_update_tag(epos.bh->b_data, epos.offset);
  153. else
  154. udf_update_tag(epos.bh->b_data,
  155. sizeof(struct allocExtDesc));
  156. mark_buffer_dirty_inode(epos.bh, inode);
  157. }
  158. }
  159. /* This inode entry is in-memory only and thus we don't have to mark
  160. * the inode dirty */
  161. iinfo->i_lenExtents = lbcount;
  162. brelse(epos.bh);
  163. }
  164. static void udf_update_alloc_ext_desc(struct inode *inode,
  165. struct extent_position *epos,
  166. u32 lenalloc)
  167. {
  168. struct super_block *sb = inode->i_sb;
  169. struct udf_sb_info *sbi = UDF_SB(sb);
  170. struct allocExtDesc *aed = (struct allocExtDesc *) (epos->bh->b_data);
  171. int len = sizeof(struct allocExtDesc);
  172. aed->lengthAllocDescs = cpu_to_le32(lenalloc);
  173. if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT) || sbi->s_udfrev >= 0x0201)
  174. len += lenalloc;
  175. udf_update_tag(epos->bh->b_data, len);
  176. mark_buffer_dirty_inode(epos->bh, inode);
  177. }
  178. /*
  179. * Truncate extents of inode to inode->i_size. This function can be used only
  180. * for making file shorter. For making file longer, udf_extend_file() has to
  181. * be used.
  182. */
  183. void udf_truncate_extents(struct inode *inode)
  184. {
  185. struct extent_position epos;
  186. struct kernel_lb_addr eloc, neloc = {};
  187. uint32_t elen, nelen = 0, indirect_ext_len = 0, lenalloc;
  188. int8_t etype;
  189. struct super_block *sb = inode->i_sb;
  190. sector_t first_block = inode->i_size >> sb->s_blocksize_bits, offset;
  191. loff_t byte_offset;
  192. int adsize;
  193. struct udf_inode_info *iinfo = UDF_I(inode);
  194. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
  195. adsize = sizeof(struct short_ad);
  196. else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
  197. adsize = sizeof(struct long_ad);
  198. else
  199. BUG();
  200. etype = inode_bmap(inode, first_block, &epos, &eloc, &elen, &offset);
  201. byte_offset = (offset << sb->s_blocksize_bits) +
  202. (inode->i_size & (sb->s_blocksize - 1));
  203. if (etype == -1) {
  204. /* We should extend the file? */
  205. WARN_ON(byte_offset);
  206. return;
  207. }
  208. epos.offset -= adsize;
  209. extent_trunc(inode, &epos, &eloc, etype, elen, byte_offset);
  210. epos.offset += adsize;
  211. if (byte_offset)
  212. lenalloc = epos.offset;
  213. else
  214. lenalloc = epos.offset - adsize;
  215. if (!epos.bh)
  216. lenalloc -= udf_file_entry_alloc_offset(inode);
  217. else
  218. lenalloc -= sizeof(struct allocExtDesc);
  219. while ((etype = udf_current_aext(inode, &epos, &eloc,
  220. &elen, 0)) != -1) {
  221. if (etype == (EXT_NEXT_EXTENT_ALLOCDECS >> 30)) {
  222. udf_write_aext(inode, &epos, &neloc, nelen, 0);
  223. if (indirect_ext_len) {
  224. /* We managed to free all extents in the
  225. * indirect extent - free it too */
  226. BUG_ON(!epos.bh);
  227. udf_free_blocks(sb, inode, &epos.block,
  228. 0, indirect_ext_len);
  229. } else if (!epos.bh) {
  230. iinfo->i_lenAlloc = lenalloc;
  231. mark_inode_dirty(inode);
  232. } else
  233. udf_update_alloc_ext_desc(inode,
  234. &epos, lenalloc);
  235. brelse(epos.bh);
  236. epos.offset = sizeof(struct allocExtDesc);
  237. epos.block = eloc;
  238. epos.bh = udf_tread(sb,
  239. udf_get_lb_pblock(sb, &eloc, 0));
  240. /* Error reading indirect block? */
  241. if (!epos.bh)
  242. return;
  243. if (elen)
  244. indirect_ext_len =
  245. (elen + sb->s_blocksize - 1) >>
  246. sb->s_blocksize_bits;
  247. else
  248. indirect_ext_len = 1;
  249. } else {
  250. extent_trunc(inode, &epos, &eloc, etype, elen, 0);
  251. epos.offset += adsize;
  252. }
  253. }
  254. if (indirect_ext_len) {
  255. BUG_ON(!epos.bh);
  256. udf_free_blocks(sb, inode, &epos.block, 0, indirect_ext_len);
  257. } else if (!epos.bh) {
  258. iinfo->i_lenAlloc = lenalloc;
  259. mark_inode_dirty(inode);
  260. } else
  261. udf_update_alloc_ext_desc(inode, &epos, lenalloc);
  262. iinfo->i_lenExtents = inode->i_size;
  263. brelse(epos.bh);
  264. }