truncate.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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, uint32_t nelen)
  30. {
  31. kernel_lb_addr neloc = { 0, 0 };
  32. int last_block = (elen + inode->i_sb->s_blocksize - 1) >> inode->i_sb->s_blocksize_bits;
  33. int first_block = (nelen + inode->i_sb->s_blocksize - 1) >> inode->i_sb->s_blocksize_bits;
  34. if (nelen)
  35. {
  36. if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30))
  37. {
  38. udf_free_blocks(inode->i_sb, inode, eloc, 0, last_block);
  39. etype = (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30);
  40. }
  41. else
  42. neloc = eloc;
  43. nelen = (etype << 30) | nelen;
  44. }
  45. if (elen != nelen)
  46. {
  47. udf_write_aext(inode, epos, neloc, nelen, 0);
  48. if (last_block - first_block > 0)
  49. {
  50. if (etype == (EXT_RECORDED_ALLOCATED >> 30))
  51. mark_inode_dirty(inode);
  52. if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
  53. udf_free_blocks(inode->i_sb, inode, eloc, first_block, last_block - first_block);
  54. }
  55. }
  56. }
  57. void udf_discard_prealloc(struct inode * inode)
  58. {
  59. struct extent_position epos = { NULL, 0, {0, 0}};
  60. kernel_lb_addr eloc;
  61. uint32_t elen, nelen;
  62. uint64_t lbcount = 0;
  63. int8_t etype = -1, netype;
  64. int adsize;
  65. if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_IN_ICB ||
  66. inode->i_size == UDF_I_LENEXTENTS(inode))
  67. return;
  68. if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_SHORT)
  69. adsize = sizeof(short_ad);
  70. else if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_LONG)
  71. adsize = sizeof(long_ad);
  72. else
  73. adsize = 0;
  74. epos.block = UDF_I_LOCATION(inode);
  75. /* Find the last extent in the file */
  76. while ((netype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1)
  77. {
  78. etype = netype;
  79. lbcount += elen;
  80. if (lbcount > inode->i_size && lbcount - elen < inode->i_size)
  81. {
  82. WARN_ON(lbcount - inode->i_size >= inode->i_sb->s_blocksize);
  83. nelen = elen - (lbcount - inode->i_size);
  84. epos.offset -= adsize;
  85. extent_trunc(inode, &epos, eloc, etype, elen, nelen);
  86. epos.offset += adsize;
  87. lbcount = inode->i_size;
  88. }
  89. }
  90. if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
  91. epos.offset -= adsize;
  92. lbcount -= elen;
  93. extent_trunc(inode, &epos, eloc, etype, elen, 0);
  94. if (!epos.bh)
  95. {
  96. UDF_I_LENALLOC(inode) = epos.offset - udf_file_entry_alloc_offset(inode);
  97. mark_inode_dirty(inode);
  98. }
  99. else
  100. {
  101. struct allocExtDesc *aed = (struct allocExtDesc *)(epos.bh->b_data);
  102. aed->lengthAllocDescs = cpu_to_le32(epos.offset - sizeof(struct allocExtDesc));
  103. if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) || UDF_SB_UDFREV(inode->i_sb) >= 0x0201)
  104. udf_update_tag(epos.bh->b_data, epos.offset);
  105. else
  106. udf_update_tag(epos.bh->b_data, sizeof(struct allocExtDesc));
  107. mark_buffer_dirty_inode(epos.bh, inode);
  108. }
  109. }
  110. UDF_I_LENEXTENTS(inode) = lbcount;
  111. WARN_ON(lbcount != inode->i_size);
  112. brelse(epos.bh);
  113. }
  114. void udf_truncate_extents(struct inode * inode)
  115. {
  116. struct extent_position epos;
  117. kernel_lb_addr eloc, neloc = { 0, 0 };
  118. uint32_t elen, nelen = 0, indirect_ext_len = 0, lenalloc;
  119. int8_t etype;
  120. struct super_block *sb = inode->i_sb;
  121. sector_t first_block = inode->i_size >> sb->s_blocksize_bits, offset;
  122. loff_t byte_offset;
  123. int adsize;
  124. if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_SHORT)
  125. adsize = sizeof(short_ad);
  126. else if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_LONG)
  127. adsize = sizeof(long_ad);
  128. else
  129. BUG();
  130. etype = inode_bmap(inode, first_block, &epos, &eloc, &elen, &offset);
  131. byte_offset = (offset << sb->s_blocksize_bits) + (inode->i_size & (sb->s_blocksize-1));
  132. if (etype != -1)
  133. {
  134. epos.offset -= adsize;
  135. extent_trunc(inode, &epos, eloc, etype, elen, byte_offset);
  136. epos.offset += adsize;
  137. if (byte_offset)
  138. lenalloc = epos.offset;
  139. else
  140. lenalloc = epos.offset - adsize;
  141. if (!epos.bh)
  142. lenalloc -= udf_file_entry_alloc_offset(inode);
  143. else
  144. lenalloc -= sizeof(struct allocExtDesc);
  145. while ((etype = udf_current_aext(inode, &epos, &eloc, &elen, 0)) != -1)
  146. {
  147. if (etype == (EXT_NEXT_EXTENT_ALLOCDECS >> 30))
  148. {
  149. udf_write_aext(inode, &epos, neloc, nelen, 0);
  150. if (indirect_ext_len)
  151. {
  152. /* We managed to free all extents in the
  153. * indirect extent - free it too */
  154. if (!epos.bh)
  155. BUG();
  156. udf_free_blocks(sb, inode, epos.block, 0, indirect_ext_len);
  157. }
  158. else
  159. {
  160. if (!epos.bh)
  161. {
  162. UDF_I_LENALLOC(inode) = lenalloc;
  163. mark_inode_dirty(inode);
  164. }
  165. else
  166. {
  167. struct allocExtDesc *aed = (struct allocExtDesc *)(epos.bh->b_data);
  168. aed->lengthAllocDescs = cpu_to_le32(lenalloc);
  169. if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT) || UDF_SB_UDFREV(sb) >= 0x0201)
  170. udf_update_tag(epos.bh->b_data, lenalloc +
  171. sizeof(struct allocExtDesc));
  172. else
  173. udf_update_tag(epos.bh->b_data, sizeof(struct allocExtDesc));
  174. mark_buffer_dirty_inode(epos.bh, inode);
  175. }
  176. }
  177. brelse(epos.bh);
  178. epos.offset = sizeof(struct allocExtDesc);
  179. epos.block = eloc;
  180. epos.bh = udf_tread(sb, udf_get_lb_pblock(sb, eloc, 0));
  181. if (elen)
  182. indirect_ext_len = (elen +
  183. sb->s_blocksize - 1) >>
  184. sb->s_blocksize_bits;
  185. else
  186. indirect_ext_len = 1;
  187. }
  188. else
  189. {
  190. extent_trunc(inode, &epos, eloc, etype, elen, 0);
  191. epos.offset += adsize;
  192. }
  193. }
  194. if (indirect_ext_len)
  195. {
  196. if (!epos.bh)
  197. BUG();
  198. udf_free_blocks(sb, inode, epos.block, 0, indirect_ext_len);
  199. }
  200. else
  201. {
  202. if (!epos.bh)
  203. {
  204. UDF_I_LENALLOC(inode) = lenalloc;
  205. mark_inode_dirty(inode);
  206. }
  207. else
  208. {
  209. struct allocExtDesc *aed = (struct allocExtDesc *)(epos.bh->b_data);
  210. aed->lengthAllocDescs = cpu_to_le32(lenalloc);
  211. if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT) || UDF_SB_UDFREV(sb) >= 0x0201)
  212. udf_update_tag(epos.bh->b_data, lenalloc +
  213. sizeof(struct allocExtDesc));
  214. else
  215. udf_update_tag(epos.bh->b_data, sizeof(struct allocExtDesc));
  216. mark_buffer_dirty_inode(epos.bh, inode);
  217. }
  218. }
  219. }
  220. else if (inode->i_size)
  221. {
  222. if (byte_offset)
  223. {
  224. kernel_long_ad extent;
  225. /*
  226. * OK, there is not extent covering inode->i_size and
  227. * no extent above inode->i_size => truncate is
  228. * extending the file by 'offset' blocks.
  229. */
  230. if ((!epos.bh && epos.offset == udf_file_entry_alloc_offset(inode)) ||
  231. (epos.bh && epos.offset == sizeof(struct allocExtDesc))) {
  232. /* File has no extents at all or has empty last
  233. * indirect extent! Create a fake extent... */
  234. extent.extLocation.logicalBlockNum = 0;
  235. extent.extLocation.partitionReferenceNum = 0;
  236. extent.extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
  237. }
  238. else {
  239. epos.offset -= adsize;
  240. etype = udf_next_aext(inode, &epos,
  241. &extent.extLocation, &extent.extLength, 0);
  242. extent.extLength |= etype << 30;
  243. }
  244. udf_extend_file(inode, &epos, &extent, offset+((inode->i_size & (sb->s_blocksize-1)) != 0));
  245. }
  246. }
  247. UDF_I_LENEXTENTS(inode) = inode->i_size;
  248. brelse(epos.bh);
  249. }