truncate.c 7.9 KB

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