truncate.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. * truncate.c
  3. *
  4. * PURPOSE
  5. * Truncate handling routines for the OSTA-UDF(tm) filesystem.
  6. *
  7. * CONTACTS
  8. * E-mail regarding any portion of the Linux UDF file system should be
  9. * directed to the development team mailing list (run by majordomo):
  10. * linux_udf@hpesjro.fc.hp.com
  11. *
  12. * COPYRIGHT
  13. * This file is distributed under the terms of the GNU General Public
  14. * License (GPL). Copies of the GPL can be obtained from:
  15. * ftp://prep.ai.mit.edu/pub/gnu/GPL
  16. * Each contributing author retains all rights to their own work.
  17. *
  18. * (C) 1999-2004 Ben Fennema
  19. * (C) 1999 Stelias Computing Inc
  20. *
  21. * HISTORY
  22. *
  23. * 02/24/99 blf Created.
  24. *
  25. */
  26. #include "udfdecl.h"
  27. #include <linux/fs.h>
  28. #include <linux/mm.h>
  29. #include <linux/udf_fs.h>
  30. #include <linux/buffer_head.h>
  31. #include "udf_i.h"
  32. #include "udf_sb.h"
  33. static void extent_trunc(struct inode * inode, kernel_lb_addr bloc, int extoffset,
  34. kernel_lb_addr eloc, int8_t etype, uint32_t elen, struct buffer_head *bh, uint32_t nelen)
  35. {
  36. kernel_lb_addr neloc = { 0, 0 };
  37. int last_block = (elen + inode->i_sb->s_blocksize - 1) >> inode->i_sb->s_blocksize_bits;
  38. int first_block = (nelen + inode->i_sb->s_blocksize - 1) >> inode->i_sb->s_blocksize_bits;
  39. if (nelen)
  40. {
  41. if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30))
  42. {
  43. udf_free_blocks(inode->i_sb, inode, eloc, 0, last_block);
  44. etype = (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30);
  45. }
  46. else
  47. neloc = eloc;
  48. nelen = (etype << 30) | nelen;
  49. }
  50. if (elen != nelen)
  51. {
  52. udf_write_aext(inode, bloc, &extoffset, neloc, nelen, bh, 0);
  53. if (last_block - first_block > 0)
  54. {
  55. if (etype == (EXT_RECORDED_ALLOCATED >> 30))
  56. mark_inode_dirty(inode);
  57. if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
  58. udf_free_blocks(inode->i_sb, inode, eloc, first_block, last_block - first_block);
  59. }
  60. }
  61. }
  62. void udf_discard_prealloc(struct inode * inode)
  63. {
  64. kernel_lb_addr bloc, eloc;
  65. uint32_t extoffset = 0, elen, nelen;
  66. uint64_t lbcount = 0;
  67. int8_t etype = -1, netype;
  68. struct buffer_head *bh = NULL;
  69. int adsize;
  70. if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_IN_ICB ||
  71. inode->i_size == UDF_I_LENEXTENTS(inode))
  72. {
  73. return;
  74. }
  75. if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_SHORT)
  76. adsize = sizeof(short_ad);
  77. else if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_LONG)
  78. adsize = sizeof(long_ad);
  79. else
  80. adsize = 0;
  81. bloc = UDF_I_LOCATION(inode);
  82. while ((netype = udf_next_aext(inode, &bloc, &extoffset, &eloc, &elen, &bh, 1)) != -1)
  83. {
  84. etype = netype;
  85. lbcount += elen;
  86. if (lbcount > inode->i_size && lbcount - inode->i_size < inode->i_sb->s_blocksize)
  87. {
  88. nelen = elen - (lbcount - inode->i_size);
  89. extent_trunc(inode, bloc, extoffset-adsize, eloc, etype, elen, bh, nelen);
  90. lbcount = inode->i_size;
  91. }
  92. }
  93. if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30))
  94. {
  95. extoffset -= adsize;
  96. lbcount -= elen;
  97. extent_trunc(inode, bloc, extoffset, eloc, etype, elen, bh, 0);
  98. if (!bh)
  99. {
  100. UDF_I_LENALLOC(inode) = extoffset - udf_file_entry_alloc_offset(inode);
  101. mark_inode_dirty(inode);
  102. }
  103. else
  104. {
  105. struct allocExtDesc *aed = (struct allocExtDesc *)(bh->b_data);
  106. aed->lengthAllocDescs = cpu_to_le32(extoffset - sizeof(struct allocExtDesc));
  107. if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) || UDF_SB_UDFREV(inode->i_sb) >= 0x0201)
  108. udf_update_tag(bh->b_data, extoffset);
  109. else
  110. udf_update_tag(bh->b_data, sizeof(struct allocExtDesc));
  111. mark_buffer_dirty_inode(bh, inode);
  112. }
  113. }
  114. UDF_I_LENEXTENTS(inode) = lbcount;
  115. udf_release_data(bh);
  116. }
  117. void udf_truncate_extents(struct inode * inode)
  118. {
  119. kernel_lb_addr bloc, eloc, neloc = { 0, 0 };
  120. uint32_t extoffset, elen, offset, nelen = 0, lelen = 0, lenalloc;
  121. int8_t etype;
  122. int first_block = inode->i_size >> inode->i_sb->s_blocksize_bits;
  123. struct buffer_head *bh = NULL;
  124. int adsize;
  125. if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_SHORT)
  126. adsize = sizeof(short_ad);
  127. else if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_LONG)
  128. adsize = sizeof(long_ad);
  129. else
  130. adsize = 0;
  131. etype = inode_bmap(inode, first_block, &bloc, &extoffset, &eloc, &elen, &offset, &bh);
  132. offset += (inode->i_size & (inode->i_sb->s_blocksize - 1));
  133. if (etype != -1)
  134. {
  135. extoffset -= adsize;
  136. extent_trunc(inode, bloc, extoffset, eloc, etype, elen, bh, offset);
  137. extoffset += adsize;
  138. if (offset)
  139. lenalloc = extoffset;
  140. else
  141. lenalloc = extoffset - adsize;
  142. if (!bh)
  143. lenalloc -= udf_file_entry_alloc_offset(inode);
  144. else
  145. lenalloc -= sizeof(struct allocExtDesc);
  146. while ((etype = udf_current_aext(inode, &bloc, &extoffset, &eloc, &elen, &bh, 0)) != -1)
  147. {
  148. if (etype == (EXT_NEXT_EXTENT_ALLOCDECS >> 30))
  149. {
  150. udf_write_aext(inode, bloc, &extoffset, neloc, nelen, bh, 0);
  151. extoffset = 0;
  152. if (lelen)
  153. {
  154. if (!bh)
  155. BUG();
  156. else
  157. memset(bh->b_data, 0x00, sizeof(struct allocExtDesc));
  158. udf_free_blocks(inode->i_sb, inode, bloc, 0, lelen);
  159. }
  160. else
  161. {
  162. if (!bh)
  163. {
  164. UDF_I_LENALLOC(inode) = lenalloc;
  165. mark_inode_dirty(inode);
  166. }
  167. else
  168. {
  169. struct allocExtDesc *aed = (struct allocExtDesc *)(bh->b_data);
  170. aed->lengthAllocDescs = cpu_to_le32(lenalloc);
  171. if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) || UDF_SB_UDFREV(inode->i_sb) >= 0x0201)
  172. udf_update_tag(bh->b_data, lenalloc +
  173. sizeof(struct allocExtDesc));
  174. else
  175. udf_update_tag(bh->b_data, sizeof(struct allocExtDesc));
  176. mark_buffer_dirty_inode(bh, inode);
  177. }
  178. }
  179. udf_release_data(bh);
  180. extoffset = sizeof(struct allocExtDesc);
  181. bloc = eloc;
  182. bh = udf_tread(inode->i_sb, udf_get_lb_pblock(inode->i_sb, bloc, 0));
  183. if (elen)
  184. lelen = (elen + inode->i_sb->s_blocksize - 1) >>
  185. inode->i_sb->s_blocksize_bits;
  186. else
  187. lelen = 1;
  188. }
  189. else
  190. {
  191. extent_trunc(inode, bloc, extoffset, eloc, etype, elen, bh, 0);
  192. extoffset += adsize;
  193. }
  194. }
  195. if (lelen)
  196. {
  197. if (!bh)
  198. BUG();
  199. else
  200. memset(bh->b_data, 0x00, sizeof(struct allocExtDesc));
  201. udf_free_blocks(inode->i_sb, inode, bloc, 0, lelen);
  202. }
  203. else
  204. {
  205. if (!bh)
  206. {
  207. UDF_I_LENALLOC(inode) = lenalloc;
  208. mark_inode_dirty(inode);
  209. }
  210. else
  211. {
  212. struct allocExtDesc *aed = (struct allocExtDesc *)(bh->b_data);
  213. aed->lengthAllocDescs = cpu_to_le32(lenalloc);
  214. if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) || UDF_SB_UDFREV(inode->i_sb) >= 0x0201)
  215. udf_update_tag(bh->b_data, lenalloc +
  216. sizeof(struct allocExtDesc));
  217. else
  218. udf_update_tag(bh->b_data, sizeof(struct allocExtDesc));
  219. mark_buffer_dirty_inode(bh, inode);
  220. }
  221. }
  222. }
  223. else if (inode->i_size)
  224. {
  225. if (offset)
  226. {
  227. extoffset -= adsize;
  228. etype = udf_next_aext(inode, &bloc, &extoffset, &eloc, &elen, &bh, 1);
  229. if (etype == (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
  230. {
  231. extoffset -= adsize;
  232. elen = EXT_NOT_RECORDED_NOT_ALLOCATED | (elen + offset);
  233. udf_write_aext(inode, bloc, &extoffset, eloc, elen, bh, 0);
  234. }
  235. else if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30))
  236. {
  237. kernel_lb_addr neloc = { 0, 0 };
  238. extoffset -= adsize;
  239. nelen = EXT_NOT_RECORDED_NOT_ALLOCATED |
  240. ((elen + offset + inode->i_sb->s_blocksize - 1) &
  241. ~(inode->i_sb->s_blocksize - 1));
  242. udf_write_aext(inode, bloc, &extoffset, neloc, nelen, bh, 1);
  243. udf_add_aext(inode, &bloc, &extoffset, eloc, (etype << 30) | elen, &bh, 1);
  244. }
  245. else
  246. {
  247. if (elen & (inode->i_sb->s_blocksize - 1))
  248. {
  249. extoffset -= adsize;
  250. elen = EXT_RECORDED_ALLOCATED |
  251. ((elen + inode->i_sb->s_blocksize - 1) &
  252. ~(inode->i_sb->s_blocksize - 1));
  253. udf_write_aext(inode, bloc, &extoffset, eloc, elen, bh, 1);
  254. }
  255. memset(&eloc, 0x00, sizeof(kernel_lb_addr));
  256. elen = EXT_NOT_RECORDED_NOT_ALLOCATED | offset;
  257. udf_add_aext(inode, &bloc, &extoffset, eloc, elen, &bh, 1);
  258. }
  259. }
  260. }
  261. UDF_I_LENEXTENTS(inode) = inode->i_size;
  262. udf_release_data(bh);
  263. }