truncate.c 8.1 KB

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