truncate.c 8.1 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/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 - inode->i_size < inode->i_sb->s_blocksize)
  81. {
  82. nelen = elen - (lbcount - inode->i_size);
  83. epos.offset -= adsize;
  84. extent_trunc(inode, &epos, eloc, etype, elen, nelen);
  85. epos.offset += adsize;
  86. lbcount = inode->i_size;
  87. }
  88. }
  89. if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
  90. epos.offset -= adsize;
  91. lbcount -= elen;
  92. extent_trunc(inode, &epos, eloc, etype, elen, 0);
  93. if (!epos.bh)
  94. {
  95. UDF_I_LENALLOC(inode) = epos.offset - udf_file_entry_alloc_offset(inode);
  96. mark_inode_dirty(inode);
  97. }
  98. else
  99. {
  100. struct allocExtDesc *aed = (struct allocExtDesc *)(epos.bh->b_data);
  101. aed->lengthAllocDescs = cpu_to_le32(epos.offset - sizeof(struct allocExtDesc));
  102. if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) || UDF_SB_UDFREV(inode->i_sb) >= 0x0201)
  103. udf_update_tag(epos.bh->b_data, epos.offset);
  104. else
  105. udf_update_tag(epos.bh->b_data, sizeof(struct allocExtDesc));
  106. mark_buffer_dirty_inode(epos.bh, inode);
  107. }
  108. }
  109. UDF_I_LENEXTENTS(inode) = lbcount;
  110. udf_release_data(epos.bh);
  111. }
  112. void udf_truncate_extents(struct inode * inode)
  113. {
  114. struct extent_position epos;
  115. kernel_lb_addr eloc, neloc = { 0, 0 };
  116. uint32_t elen, nelen = 0, indirect_ext_len = 0, lenalloc;
  117. int8_t etype;
  118. sector_t first_block = inode->i_size >> inode->i_sb->s_blocksize_bits, offset;
  119. loff_t byte_offset;
  120. int adsize;
  121. if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_SHORT)
  122. adsize = sizeof(short_ad);
  123. else if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_LONG)
  124. adsize = sizeof(long_ad);
  125. else
  126. BUG();
  127. etype = inode_bmap(inode, first_block, &epos, &eloc, &elen, &offset);
  128. byte_offset = (offset << inode->i_sb->s_blocksize_bits) + (inode->i_size & (inode->i_sb->s_blocksize-1));
  129. if (etype != -1)
  130. {
  131. epos.offset -= adsize;
  132. extent_trunc(inode, &epos, eloc, etype, elen, byte_offset);
  133. epos.offset += adsize;
  134. if (byte_offset)
  135. lenalloc = epos.offset;
  136. else
  137. lenalloc = epos.offset - adsize;
  138. if (!epos.bh)
  139. lenalloc -= udf_file_entry_alloc_offset(inode);
  140. else
  141. lenalloc -= sizeof(struct allocExtDesc);
  142. while ((etype = udf_current_aext(inode, &epos, &eloc, &elen, 0)) != -1)
  143. {
  144. if (etype == (EXT_NEXT_EXTENT_ALLOCDECS >> 30))
  145. {
  146. udf_write_aext(inode, &epos, neloc, nelen, 0);
  147. if (indirect_ext_len)
  148. {
  149. /* We managed to free all extents in the
  150. * indirect extent - free it too */
  151. if (!epos.bh)
  152. BUG();
  153. udf_free_blocks(inode->i_sb, inode, epos.block, 0, indirect_ext_len);
  154. }
  155. else
  156. {
  157. if (!epos.bh)
  158. {
  159. UDF_I_LENALLOC(inode) = lenalloc;
  160. mark_inode_dirty(inode);
  161. }
  162. else
  163. {
  164. struct allocExtDesc *aed = (struct allocExtDesc *)(epos.bh->b_data);
  165. aed->lengthAllocDescs = cpu_to_le32(lenalloc);
  166. if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) || UDF_SB_UDFREV(inode->i_sb) >= 0x0201)
  167. udf_update_tag(epos.bh->b_data, lenalloc +
  168. sizeof(struct allocExtDesc));
  169. else
  170. udf_update_tag(epos.bh->b_data, sizeof(struct allocExtDesc));
  171. mark_buffer_dirty_inode(epos.bh, inode);
  172. }
  173. }
  174. brelse(epos.bh);
  175. epos.offset = sizeof(struct allocExtDesc);
  176. epos.block = eloc;
  177. epos.bh = udf_tread(inode->i_sb, udf_get_lb_pblock(inode->i_sb, eloc, 0));
  178. if (elen)
  179. indirect_ext_len = (elen +
  180. inode->i_sb->s_blocksize - 1) >>
  181. inode->i_sb->s_blocksize_bits;
  182. else
  183. indirect_ext_len = 1;
  184. }
  185. else
  186. {
  187. extent_trunc(inode, &epos, eloc, etype, elen, 0);
  188. epos.offset += adsize;
  189. }
  190. }
  191. if (indirect_ext_len)
  192. {
  193. if (!epos.bh)
  194. BUG();
  195. udf_free_blocks(inode->i_sb, inode, epos.block, 0, indirect_ext_len);
  196. }
  197. else
  198. {
  199. if (!epos.bh)
  200. {
  201. UDF_I_LENALLOC(inode) = lenalloc;
  202. mark_inode_dirty(inode);
  203. }
  204. else
  205. {
  206. struct allocExtDesc *aed = (struct allocExtDesc *)(epos.bh->b_data);
  207. aed->lengthAllocDescs = cpu_to_le32(lenalloc);
  208. if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) || UDF_SB_UDFREV(inode->i_sb) >= 0x0201)
  209. udf_update_tag(epos.bh->b_data, lenalloc +
  210. sizeof(struct allocExtDesc));
  211. else
  212. udf_update_tag(epos.bh->b_data, sizeof(struct allocExtDesc));
  213. mark_buffer_dirty_inode(epos.bh, inode);
  214. }
  215. }
  216. }
  217. else if (inode->i_size)
  218. {
  219. if (byte_offset)
  220. {
  221. /*
  222. * OK, there is not extent covering inode->i_size and
  223. * no extent above inode->i_size => truncate is
  224. * extending the file by 'offset'.
  225. */
  226. if ((!epos.bh && epos.offset == udf_file_entry_alloc_offset(inode)) ||
  227. (epos.bh && epos.offset == sizeof(struct allocExtDesc))) {
  228. /* File has no extents at all! */
  229. memset(&eloc, 0x00, sizeof(kernel_lb_addr));
  230. elen = EXT_NOT_RECORDED_NOT_ALLOCATED | byte_offset;
  231. udf_add_aext(inode, &epos, eloc, elen, 1);
  232. }
  233. else {
  234. epos.offset -= adsize;
  235. etype = udf_next_aext(inode, &epos, &eloc, &elen, 1);
  236. if (etype == (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
  237. {
  238. epos.offset -= adsize;
  239. elen = EXT_NOT_RECORDED_NOT_ALLOCATED | (elen + byte_offset);
  240. udf_write_aext(inode, &epos, eloc, elen, 0);
  241. }
  242. else if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30))
  243. {
  244. kernel_lb_addr neloc = { 0, 0 };
  245. epos.offset -= adsize;
  246. nelen = EXT_NOT_RECORDED_NOT_ALLOCATED |
  247. ((elen + byte_offset + inode->i_sb->s_blocksize - 1) &
  248. ~(inode->i_sb->s_blocksize - 1));
  249. udf_write_aext(inode, &epos, neloc, nelen, 1);
  250. udf_add_aext(inode, &epos, eloc, (etype << 30) | elen, 1);
  251. }
  252. else
  253. {
  254. if (elen & (inode->i_sb->s_blocksize - 1))
  255. {
  256. epos.offset -= adsize;
  257. elen = EXT_RECORDED_ALLOCATED |
  258. ((elen + inode->i_sb->s_blocksize - 1) &
  259. ~(inode->i_sb->s_blocksize - 1));
  260. udf_write_aext(inode, &epos, eloc, elen, 1);
  261. }
  262. memset(&eloc, 0x00, sizeof(kernel_lb_addr));
  263. elen = EXT_NOT_RECORDED_NOT_ALLOCATED | byte_offset;
  264. udf_add_aext(inode, &epos, eloc, elen, 1);
  265. }
  266. }
  267. }
  268. }
  269. UDF_I_LENEXTENTS(inode) = inode->i_size;
  270. udf_release_data(epos.bh);
  271. }