truncate.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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,
  30. uint32_t nelen)
  31. {
  32. kernel_lb_addr neloc = { 0, 0 };
  33. int last_block =
  34. (elen + inode->i_sb->s_blocksize -
  35. 1) >> inode->i_sb->s_blocksize_bits;
  36. int first_block =
  37. (nelen + inode->i_sb->s_blocksize -
  38. 1) >> inode->i_sb->s_blocksize_bits;
  39. if (nelen) {
  40. if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
  41. udf_free_blocks(inode->i_sb, inode, eloc, 0,
  42. last_block);
  43. etype = (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30);
  44. } else
  45. neloc = eloc;
  46. nelen = (etype << 30) | nelen;
  47. }
  48. if (elen != nelen) {
  49. udf_write_aext(inode, epos, neloc, nelen, 0);
  50. if (last_block - first_block > 0) {
  51. if (etype == (EXT_RECORDED_ALLOCATED >> 30))
  52. mark_inode_dirty(inode);
  53. if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
  54. udf_free_blocks(inode->i_sb, inode, eloc,
  55. first_block,
  56. last_block - first_block);
  57. }
  58. }
  59. }
  60. /*
  61. * Truncate the last extent to match i_size. This function assumes
  62. * that preallocation extent is already truncated.
  63. */
  64. void udf_truncate_tail_extent(struct inode *inode)
  65. {
  66. struct extent_position epos = { NULL, 0, {0, 0} };
  67. kernel_lb_addr eloc;
  68. uint32_t elen, nelen;
  69. uint64_t lbcount = 0;
  70. int8_t etype = -1, netype;
  71. int adsize;
  72. if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_IN_ICB ||
  73. inode->i_size == UDF_I_LENEXTENTS(inode))
  74. return;
  75. /* Are we going to delete the file anyway? */
  76. if (inode->i_nlink == 0)
  77. return;
  78. if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_SHORT)
  79. adsize = sizeof(short_ad);
  80. else if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_LONG)
  81. adsize = sizeof(long_ad);
  82. else
  83. BUG();
  84. /* Find the last extent in the file */
  85. while ((netype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
  86. etype = netype;
  87. lbcount += elen;
  88. if (lbcount > inode->i_size) {
  89. if (lbcount - inode->i_size >= inode->i_sb->s_blocksize)
  90. printk(KERN_WARNING
  91. "udf_truncate_tail_extent(): Too long "
  92. "extent after EOF in inode %u: i_size: "
  93. "%Ld lbcount: %Ld extent %u+%u\n",
  94. (unsigned)inode->i_ino,
  95. (long long)inode->i_size,
  96. (long long)lbcount,
  97. (unsigned)eloc.logicalBlockNum,
  98. (unsigned)elen);
  99. nelen = elen - (lbcount - inode->i_size);
  100. epos.offset -= adsize;
  101. extent_trunc(inode, &epos, eloc, etype, elen, nelen);
  102. epos.offset += adsize;
  103. if (udf_next_aext(inode, &epos, &eloc, &elen, 1) != -1)
  104. printk(KERN_ERR "udf_truncate_tail_extent(): "
  105. "Extent after EOF in inode %u.\n",
  106. (unsigned)inode->i_ino);
  107. break;
  108. }
  109. }
  110. /* This inode entry is in-memory only and thus we don't have to mark
  111. * the inode dirty */
  112. UDF_I_LENEXTENTS(inode) = inode->i_size;
  113. brelse(epos.bh);
  114. }
  115. void udf_discard_prealloc(struct inode *inode)
  116. {
  117. struct extent_position epos = { NULL, 0, {0, 0} };
  118. kernel_lb_addr eloc;
  119. uint32_t elen;
  120. uint64_t lbcount = 0;
  121. int8_t etype = -1, netype;
  122. int adsize;
  123. if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_IN_ICB ||
  124. inode->i_size == UDF_I_LENEXTENTS(inode))
  125. return;
  126. if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_SHORT)
  127. adsize = sizeof(short_ad);
  128. else if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_LONG)
  129. adsize = sizeof(long_ad);
  130. else
  131. adsize = 0;
  132. epos.block = UDF_I_LOCATION(inode);
  133. /* Find the last extent in the file */
  134. while ((netype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
  135. etype = netype;
  136. lbcount += elen;
  137. }
  138. if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
  139. epos.offset -= adsize;
  140. lbcount -= elen;
  141. extent_trunc(inode, &epos, eloc, etype, elen, 0);
  142. if (!epos.bh) {
  143. UDF_I_LENALLOC(inode) =
  144. epos.offset - udf_file_entry_alloc_offset(inode);
  145. mark_inode_dirty(inode);
  146. } else {
  147. struct allocExtDesc *aed =
  148. (struct allocExtDesc *)(epos.bh->b_data);
  149. aed->lengthAllocDescs =
  150. cpu_to_le32(epos.offset -
  151. sizeof(struct allocExtDesc));
  152. if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT)
  153. || UDF_SB_UDFREV(inode->i_sb) >= 0x0201)
  154. udf_update_tag(epos.bh->b_data, epos.offset);
  155. else
  156. udf_update_tag(epos.bh->b_data,
  157. sizeof(struct allocExtDesc));
  158. mark_buffer_dirty_inode(epos.bh, inode);
  159. }
  160. }
  161. /* This inode entry is in-memory only and thus we don't have to mark
  162. * the inode dirty */
  163. UDF_I_LENEXTENTS(inode) = lbcount;
  164. brelse(epos.bh);
  165. }
  166. void udf_truncate_extents(struct inode *inode)
  167. {
  168. struct extent_position epos;
  169. kernel_lb_addr eloc, neloc = { 0, 0 };
  170. uint32_t elen, nelen = 0, indirect_ext_len = 0, lenalloc;
  171. int8_t etype;
  172. struct super_block *sb = inode->i_sb;
  173. sector_t first_block = inode->i_size >> sb->s_blocksize_bits, offset;
  174. loff_t byte_offset;
  175. int adsize;
  176. if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_SHORT)
  177. adsize = sizeof(short_ad);
  178. else if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_LONG)
  179. adsize = sizeof(long_ad);
  180. else
  181. BUG();
  182. etype = inode_bmap(inode, first_block, &epos, &eloc, &elen, &offset);
  183. byte_offset =
  184. (offset << sb->s_blocksize_bits) +
  185. (inode->i_size & (sb->s_blocksize - 1));
  186. if (etype != -1) {
  187. epos.offset -= adsize;
  188. extent_trunc(inode, &epos, eloc, etype, elen, byte_offset);
  189. epos.offset += adsize;
  190. if (byte_offset)
  191. lenalloc = epos.offset;
  192. else
  193. lenalloc = epos.offset - adsize;
  194. if (!epos.bh)
  195. lenalloc -= udf_file_entry_alloc_offset(inode);
  196. else
  197. lenalloc -= sizeof(struct allocExtDesc);
  198. while ((etype =
  199. udf_current_aext(inode, &epos, &eloc, &elen,
  200. 0)) != -1) {
  201. if (etype == (EXT_NEXT_EXTENT_ALLOCDECS >> 30)) {
  202. udf_write_aext(inode, &epos, neloc, nelen, 0);
  203. if (indirect_ext_len) {
  204. /* We managed to free all extents in the
  205. * indirect extent - free it too */
  206. if (!epos.bh)
  207. BUG();
  208. udf_free_blocks(sb, inode, epos.block,
  209. 0, indirect_ext_len);
  210. } else {
  211. if (!epos.bh) {
  212. UDF_I_LENALLOC(inode) =
  213. lenalloc;
  214. mark_inode_dirty(inode);
  215. } else {
  216. struct allocExtDesc *aed =
  217. (struct allocExtDesc
  218. *)(epos.bh->b_data);
  219. aed->lengthAllocDescs =
  220. cpu_to_le32(lenalloc);
  221. if (!UDF_QUERY_FLAG
  222. (sb, UDF_FLAG_STRICT)
  223. || UDF_SB_UDFREV(sb) >=
  224. 0x0201)
  225. udf_update_tag(epos.bh->
  226. b_data,
  227. lenalloc
  228. +
  229. sizeof
  230. (struct
  231. allocExtDesc));
  232. else
  233. udf_update_tag(epos.bh->
  234. b_data,
  235. sizeof
  236. (struct
  237. allocExtDesc));
  238. mark_buffer_dirty_inode(epos.bh,
  239. inode);
  240. }
  241. }
  242. brelse(epos.bh);
  243. epos.offset = sizeof(struct allocExtDesc);
  244. epos.block = eloc;
  245. epos.bh =
  246. udf_tread(sb,
  247. udf_get_lb_pblock(sb, eloc, 0));
  248. if (elen)
  249. indirect_ext_len = (elen +
  250. sb->s_blocksize -
  251. 1) >> sb->
  252. s_blocksize_bits;
  253. else
  254. indirect_ext_len = 1;
  255. } else {
  256. extent_trunc(inode, &epos, eloc, etype, elen,
  257. 0);
  258. epos.offset += adsize;
  259. }
  260. }
  261. if (indirect_ext_len) {
  262. if (!epos.bh)
  263. BUG();
  264. udf_free_blocks(sb, inode, epos.block, 0,
  265. indirect_ext_len);
  266. } else {
  267. if (!epos.bh) {
  268. UDF_I_LENALLOC(inode) = lenalloc;
  269. mark_inode_dirty(inode);
  270. } else {
  271. struct allocExtDesc *aed =
  272. (struct allocExtDesc *)(epos.bh->b_data);
  273. aed->lengthAllocDescs = cpu_to_le32(lenalloc);
  274. if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT)
  275. || UDF_SB_UDFREV(sb) >= 0x0201)
  276. udf_update_tag(epos.bh->b_data,
  277. lenalloc +
  278. sizeof(struct
  279. allocExtDesc));
  280. else
  281. udf_update_tag(epos.bh->b_data,
  282. sizeof(struct
  283. allocExtDesc));
  284. mark_buffer_dirty_inode(epos.bh, inode);
  285. }
  286. }
  287. } else if (inode->i_size) {
  288. if (byte_offset) {
  289. kernel_long_ad extent;
  290. /*
  291. * OK, there is not extent covering inode->i_size and
  292. * no extent above inode->i_size => truncate is
  293. * extending the file by 'offset' blocks.
  294. */
  295. if ((!epos.bh
  296. && epos.offset ==
  297. udf_file_entry_alloc_offset(inode)) || (epos.bh
  298. && epos.
  299. offset ==
  300. sizeof
  301. (struct
  302. allocExtDesc)))
  303. {
  304. /* File has no extents at all or has empty last
  305. * indirect extent! Create a fake extent... */
  306. extent.extLocation.logicalBlockNum = 0;
  307. extent.extLocation.partitionReferenceNum = 0;
  308. extent.extLength =
  309. EXT_NOT_RECORDED_NOT_ALLOCATED;
  310. } else {
  311. epos.offset -= adsize;
  312. etype = udf_next_aext(inode, &epos,
  313. &extent.extLocation,
  314. &extent.extLength, 0);
  315. extent.extLength |= etype << 30;
  316. }
  317. udf_extend_file(inode, &epos, &extent,
  318. offset +
  319. ((inode->
  320. i_size & (sb->s_blocksize - 1)) !=
  321. 0));
  322. }
  323. }
  324. UDF_I_LENEXTENTS(inode) = inode->i_size;
  325. brelse(epos.bh);
  326. }