truncate.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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, offset, nelen = 0, lelen = 0, lenalloc;
  116. int8_t etype;
  117. int first_block = inode->i_size >> inode->i_sb->s_blocksize_bits;
  118. struct buffer_head *bh = NULL;
  119. int adsize;
  120. if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_SHORT)
  121. adsize = sizeof(short_ad);
  122. else if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_LONG)
  123. adsize = sizeof(long_ad);
  124. else
  125. adsize = 0;
  126. etype = inode_bmap(inode, first_block, &bloc, &extoffset, &eloc, &elen, &offset, &bh);
  127. offset += (inode->i_size & (inode->i_sb->s_blocksize - 1));
  128. if (etype != -1)
  129. {
  130. extoffset -= adsize;
  131. extent_trunc(inode, bloc, extoffset, eloc, etype, elen, bh, offset);
  132. extoffset += adsize;
  133. if (offset)
  134. lenalloc = extoffset;
  135. else
  136. lenalloc = extoffset - adsize;
  137. if (!bh)
  138. lenalloc -= udf_file_entry_alloc_offset(inode);
  139. else
  140. lenalloc -= sizeof(struct allocExtDesc);
  141. while ((etype = udf_current_aext(inode, &bloc, &extoffset, &eloc, &elen, &bh, 0)) != -1)
  142. {
  143. if (etype == (EXT_NEXT_EXTENT_ALLOCDECS >> 30))
  144. {
  145. udf_write_aext(inode, bloc, &extoffset, neloc, nelen, bh, 0);
  146. extoffset = 0;
  147. if (lelen)
  148. {
  149. if (!bh)
  150. BUG();
  151. else
  152. memset(bh->b_data, 0x00, sizeof(struct allocExtDesc));
  153. udf_free_blocks(inode->i_sb, inode, bloc, 0, lelen);
  154. }
  155. else
  156. {
  157. if (!bh)
  158. {
  159. UDF_I_LENALLOC(inode) = lenalloc;
  160. mark_inode_dirty(inode);
  161. }
  162. else
  163. {
  164. struct allocExtDesc *aed = (struct allocExtDesc *)(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(bh->b_data, lenalloc +
  168. sizeof(struct allocExtDesc));
  169. else
  170. udf_update_tag(bh->b_data, sizeof(struct allocExtDesc));
  171. mark_buffer_dirty_inode(bh, inode);
  172. }
  173. }
  174. udf_release_data(bh);
  175. extoffset = sizeof(struct allocExtDesc);
  176. bloc = eloc;
  177. bh = udf_tread(inode->i_sb, udf_get_lb_pblock(inode->i_sb, bloc, 0));
  178. if (elen)
  179. lelen = (elen + inode->i_sb->s_blocksize - 1) >>
  180. inode->i_sb->s_blocksize_bits;
  181. else
  182. lelen = 1;
  183. }
  184. else
  185. {
  186. extent_trunc(inode, bloc, extoffset, eloc, etype, elen, bh, 0);
  187. extoffset += adsize;
  188. }
  189. }
  190. if (lelen)
  191. {
  192. if (!bh)
  193. BUG();
  194. else
  195. memset(bh->b_data, 0x00, sizeof(struct allocExtDesc));
  196. udf_free_blocks(inode->i_sb, inode, bloc, 0, lelen);
  197. }
  198. else
  199. {
  200. if (!bh)
  201. {
  202. UDF_I_LENALLOC(inode) = lenalloc;
  203. mark_inode_dirty(inode);
  204. }
  205. else
  206. {
  207. struct allocExtDesc *aed = (struct allocExtDesc *)(bh->b_data);
  208. aed->lengthAllocDescs = cpu_to_le32(lenalloc);
  209. if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) || UDF_SB_UDFREV(inode->i_sb) >= 0x0201)
  210. udf_update_tag(bh->b_data, lenalloc +
  211. sizeof(struct allocExtDesc));
  212. else
  213. udf_update_tag(bh->b_data, sizeof(struct allocExtDesc));
  214. mark_buffer_dirty_inode(bh, inode);
  215. }
  216. }
  217. }
  218. else if (inode->i_size)
  219. {
  220. if (offset)
  221. {
  222. /*
  223. * OK, there is not extent covering inode->i_size and
  224. * no extent above inode->i_size => truncate is
  225. * extending the file by 'offset'.
  226. */
  227. if ((!bh && extoffset == udf_file_entry_alloc_offset(inode)) ||
  228. (bh && extoffset == sizeof(struct allocExtDesc))) {
  229. /* File has no extents at all! */
  230. memset(&eloc, 0x00, sizeof(kernel_lb_addr));
  231. elen = EXT_NOT_RECORDED_NOT_ALLOCATED | offset;
  232. udf_add_aext(inode, &bloc, &extoffset, eloc, elen, &bh, 1);
  233. }
  234. else {
  235. extoffset -= adsize;
  236. etype = udf_next_aext(inode, &bloc, &extoffset, &eloc, &elen, &bh, 1);
  237. if (etype == (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
  238. {
  239. extoffset -= adsize;
  240. elen = EXT_NOT_RECORDED_NOT_ALLOCATED | (elen + offset);
  241. udf_write_aext(inode, bloc, &extoffset, eloc, elen, bh, 0);
  242. }
  243. else if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30))
  244. {
  245. kernel_lb_addr neloc = { 0, 0 };
  246. extoffset -= adsize;
  247. nelen = EXT_NOT_RECORDED_NOT_ALLOCATED |
  248. ((elen + offset + inode->i_sb->s_blocksize - 1) &
  249. ~(inode->i_sb->s_blocksize - 1));
  250. udf_write_aext(inode, bloc, &extoffset, neloc, nelen, bh, 1);
  251. udf_add_aext(inode, &bloc, &extoffset, eloc, (etype << 30) | elen, &bh, 1);
  252. }
  253. else
  254. {
  255. if (elen & (inode->i_sb->s_blocksize - 1))
  256. {
  257. extoffset -= adsize;
  258. elen = EXT_RECORDED_ALLOCATED |
  259. ((elen + inode->i_sb->s_blocksize - 1) &
  260. ~(inode->i_sb->s_blocksize - 1));
  261. udf_write_aext(inode, bloc, &extoffset, eloc, elen, bh, 1);
  262. }
  263. memset(&eloc, 0x00, sizeof(kernel_lb_addr));
  264. elen = EXT_NOT_RECORDED_NOT_ALLOCATED | offset;
  265. udf_add_aext(inode, &bloc, &extoffset, eloc, elen, &bh, 1);
  266. }
  267. }
  268. }
  269. }
  270. UDF_I_LENEXTENTS(inode) = inode->i_size;
  271. udf_release_data(bh);
  272. }