balloc.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * balloc.c
  4. *
  5. * PURPOSE
  6. * Block allocation handling routines for the OSTA-UDF(tm) filesystem.
  7. *
  8. * COPYRIGHT
  9. * (C) 1999-2001 Ben Fennema
  10. * (C) 1999 Stelias Computing Inc
  11. *
  12. * HISTORY
  13. *
  14. * 02/24/99 blf Created.
  15. *
  16. */
  17. #include "udfdecl.h"
  18. #include <linux/bitops.h>
  19. #include <linux/overflow.h>
  20. #include "udf_i.h"
  21. #include "udf_sb.h"
  22. #define udf_clear_bit __test_and_clear_bit_le
  23. #define udf_set_bit __test_and_set_bit_le
  24. #define udf_test_bit test_bit_le
  25. #define udf_find_next_one_bit find_next_bit_le
  26. static int read_block_bitmap(struct super_block *sb,
  27. struct udf_bitmap *bitmap, unsigned int block,
  28. unsigned long bitmap_nr)
  29. {
  30. struct buffer_head *bh = NULL;
  31. int i;
  32. int max_bits, off, count;
  33. struct kernel_lb_addr loc;
  34. loc.logicalBlockNum = bitmap->s_extPosition;
  35. loc.partitionReferenceNum = UDF_SB(sb)->s_partition;
  36. bh = sb_bread(sb, udf_get_lb_pblock(sb, &loc, block));
  37. bitmap->s_block_bitmap[bitmap_nr] = bh;
  38. if (!bh)
  39. return -EIO;
  40. /* Check consistency of Space Bitmap buffer. */
  41. max_bits = sb->s_blocksize * 8;
  42. if (!bitmap_nr) {
  43. off = sizeof(struct spaceBitmapDesc) << 3;
  44. count = min(max_bits - off, bitmap->s_nr_groups);
  45. } else {
  46. /*
  47. * Rough check if bitmap number is too big to have any bitmap
  48. * blocks reserved.
  49. */
  50. if (bitmap_nr >
  51. (bitmap->s_nr_groups >> (sb->s_blocksize_bits + 3)) + 2)
  52. return 0;
  53. off = 0;
  54. count = bitmap->s_nr_groups - bitmap_nr * max_bits +
  55. (sizeof(struct spaceBitmapDesc) << 3);
  56. count = min(count, max_bits);
  57. }
  58. for (i = 0; i < count; i++)
  59. if (udf_test_bit(i + off, bh->b_data)) {
  60. bitmap->s_block_bitmap[bitmap_nr] =
  61. ERR_PTR(-EFSCORRUPTED);
  62. brelse(bh);
  63. return -EFSCORRUPTED;
  64. }
  65. return 0;
  66. }
  67. static int load_block_bitmap(struct super_block *sb,
  68. struct udf_bitmap *bitmap,
  69. unsigned int block_group)
  70. {
  71. int retval = 0;
  72. int nr_groups = bitmap->s_nr_groups;
  73. if (block_group >= nr_groups) {
  74. udf_debug("block_group (%u) > nr_groups (%d)\n",
  75. block_group, nr_groups);
  76. }
  77. if (bitmap->s_block_bitmap[block_group]) {
  78. /*
  79. * The bitmap failed verification in the past. No point in
  80. * trying again.
  81. */
  82. if (IS_ERR(bitmap->s_block_bitmap[block_group]))
  83. return PTR_ERR(bitmap->s_block_bitmap[block_group]);
  84. return block_group;
  85. }
  86. retval = read_block_bitmap(sb, bitmap, block_group, block_group);
  87. if (retval < 0)
  88. return retval;
  89. return block_group;
  90. }
  91. static void udf_add_free_space(struct super_block *sb, u16 partition, u32 cnt)
  92. {
  93. struct udf_sb_info *sbi = UDF_SB(sb);
  94. struct logicalVolIntegrityDesc *lvid;
  95. if (!sbi->s_lvid_bh)
  96. return;
  97. lvid = (struct logicalVolIntegrityDesc *)sbi->s_lvid_bh->b_data;
  98. le32_add_cpu(&lvid->freeSpaceTable[partition], cnt);
  99. udf_updated_lvid(sb);
  100. }
  101. static void udf_bitmap_free_blocks(struct super_block *sb,
  102. struct udf_bitmap *bitmap,
  103. struct kernel_lb_addr *bloc,
  104. uint32_t offset,
  105. uint32_t count)
  106. {
  107. struct udf_sb_info *sbi = UDF_SB(sb);
  108. struct buffer_head *bh = NULL;
  109. unsigned long block;
  110. unsigned long block_group;
  111. unsigned long bit;
  112. unsigned long i;
  113. int bitmap_nr;
  114. unsigned long overflow;
  115. mutex_lock(&sbi->s_alloc_mutex);
  116. /* We make sure this cannot overflow when mounting the filesystem */
  117. block = bloc->logicalBlockNum + offset +
  118. (sizeof(struct spaceBitmapDesc) << 3);
  119. do {
  120. overflow = 0;
  121. block_group = block >> (sb->s_blocksize_bits + 3);
  122. bit = block % (sb->s_blocksize << 3);
  123. /*
  124. * Check to see if we are freeing blocks across a group boundary.
  125. */
  126. if (bit + count > (sb->s_blocksize << 3)) {
  127. overflow = bit + count - (sb->s_blocksize << 3);
  128. count -= overflow;
  129. }
  130. bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
  131. if (bitmap_nr < 0)
  132. goto error_return;
  133. bh = bitmap->s_block_bitmap[bitmap_nr];
  134. for (i = 0; i < count; i++) {
  135. if (udf_set_bit(bit + i, bh->b_data)) {
  136. udf_debug("bit %lu already set\n", bit + i);
  137. udf_debug("byte=%2x\n",
  138. ((__u8 *)bh->b_data)[(bit + i) >> 3]);
  139. }
  140. }
  141. udf_add_free_space(sb, sbi->s_partition, count);
  142. mark_buffer_dirty(bh);
  143. if (overflow) {
  144. block += count;
  145. count = overflow;
  146. }
  147. } while (overflow);
  148. error_return:
  149. mutex_unlock(&sbi->s_alloc_mutex);
  150. }
  151. static int udf_bitmap_prealloc_blocks(struct super_block *sb,
  152. struct udf_bitmap *bitmap,
  153. uint16_t partition, uint32_t first_block,
  154. uint32_t block_count)
  155. {
  156. struct udf_sb_info *sbi = UDF_SB(sb);
  157. int alloc_count = 0;
  158. int bit, block, block_group;
  159. int bitmap_nr;
  160. struct buffer_head *bh;
  161. __u32 part_len;
  162. mutex_lock(&sbi->s_alloc_mutex);
  163. part_len = sbi->s_partmaps[partition].s_partition_len;
  164. if (first_block >= part_len)
  165. goto out;
  166. if (first_block + block_count > part_len)
  167. block_count = part_len - first_block;
  168. do {
  169. block = first_block + (sizeof(struct spaceBitmapDesc) << 3);
  170. block_group = block >> (sb->s_blocksize_bits + 3);
  171. bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
  172. if (bitmap_nr < 0)
  173. goto out;
  174. bh = bitmap->s_block_bitmap[bitmap_nr];
  175. bit = block % (sb->s_blocksize << 3);
  176. while (bit < (sb->s_blocksize << 3) && block_count > 0) {
  177. if (!udf_clear_bit(bit, bh->b_data))
  178. goto out;
  179. block_count--;
  180. alloc_count++;
  181. bit++;
  182. block++;
  183. }
  184. mark_buffer_dirty(bh);
  185. } while (block_count > 0);
  186. out:
  187. udf_add_free_space(sb, partition, -alloc_count);
  188. mutex_unlock(&sbi->s_alloc_mutex);
  189. return alloc_count;
  190. }
  191. static udf_pblk_t udf_bitmap_new_block(struct super_block *sb,
  192. struct udf_bitmap *bitmap, uint16_t partition,
  193. uint32_t goal, int *err)
  194. {
  195. struct udf_sb_info *sbi = UDF_SB(sb);
  196. int newbit, bit = 0;
  197. udf_pblk_t block;
  198. int block_group, group_start;
  199. int end_goal, nr_groups, bitmap_nr, i;
  200. struct buffer_head *bh = NULL;
  201. char *ptr;
  202. udf_pblk_t newblock = 0;
  203. *err = -ENOSPC;
  204. mutex_lock(&sbi->s_alloc_mutex);
  205. repeat:
  206. if (goal >= sbi->s_partmaps[partition].s_partition_len)
  207. goal = 0;
  208. nr_groups = bitmap->s_nr_groups;
  209. block = goal + (sizeof(struct spaceBitmapDesc) << 3);
  210. block_group = block >> (sb->s_blocksize_bits + 3);
  211. group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc);
  212. bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
  213. if (bitmap_nr < 0)
  214. goto error_return;
  215. bh = bitmap->s_block_bitmap[bitmap_nr];
  216. ptr = memscan((char *)bh->b_data + group_start, 0xFF,
  217. sb->s_blocksize - group_start);
  218. if ((ptr - ((char *)bh->b_data)) < sb->s_blocksize) {
  219. bit = block % (sb->s_blocksize << 3);
  220. if (udf_test_bit(bit, bh->b_data))
  221. goto got_block;
  222. end_goal = (bit + 63) & ~63;
  223. bit = udf_find_next_one_bit(bh->b_data, end_goal, bit);
  224. if (bit < end_goal)
  225. goto got_block;
  226. ptr = memscan((char *)bh->b_data + (bit >> 3), 0xFF,
  227. sb->s_blocksize - ((bit + 7) >> 3));
  228. newbit = (ptr - ((char *)bh->b_data)) << 3;
  229. if (newbit < sb->s_blocksize << 3) {
  230. bit = newbit;
  231. goto search_back;
  232. }
  233. newbit = udf_find_next_one_bit(bh->b_data,
  234. sb->s_blocksize << 3, bit);
  235. if (newbit < sb->s_blocksize << 3) {
  236. bit = newbit;
  237. goto got_block;
  238. }
  239. }
  240. for (i = 0; i < (nr_groups * 2); i++) {
  241. block_group++;
  242. if (block_group >= nr_groups)
  243. block_group = 0;
  244. group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc);
  245. bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
  246. if (bitmap_nr < 0)
  247. goto error_return;
  248. bh = bitmap->s_block_bitmap[bitmap_nr];
  249. if (i < nr_groups) {
  250. ptr = memscan((char *)bh->b_data + group_start, 0xFF,
  251. sb->s_blocksize - group_start);
  252. if ((ptr - ((char *)bh->b_data)) < sb->s_blocksize) {
  253. bit = (ptr - ((char *)bh->b_data)) << 3;
  254. break;
  255. }
  256. } else {
  257. bit = udf_find_next_one_bit(bh->b_data,
  258. sb->s_blocksize << 3,
  259. group_start << 3);
  260. if (bit < sb->s_blocksize << 3)
  261. break;
  262. }
  263. }
  264. if (i >= (nr_groups * 2)) {
  265. mutex_unlock(&sbi->s_alloc_mutex);
  266. return newblock;
  267. }
  268. if (bit < sb->s_blocksize << 3)
  269. goto search_back;
  270. else
  271. bit = udf_find_next_one_bit(bh->b_data, sb->s_blocksize << 3,
  272. group_start << 3);
  273. if (bit >= sb->s_blocksize << 3) {
  274. mutex_unlock(&sbi->s_alloc_mutex);
  275. return 0;
  276. }
  277. search_back:
  278. i = 0;
  279. while (i < 7 && bit > (group_start << 3) &&
  280. udf_test_bit(bit - 1, bh->b_data)) {
  281. ++i;
  282. --bit;
  283. }
  284. got_block:
  285. newblock = bit + (block_group << (sb->s_blocksize_bits + 3)) -
  286. (sizeof(struct spaceBitmapDesc) << 3);
  287. if (newblock >= sbi->s_partmaps[partition].s_partition_len) {
  288. /*
  289. * Ran off the end of the bitmap, and bits following are
  290. * non-compliant (not all zero)
  291. */
  292. udf_err(sb, "bitmap for partition %d corrupted (block %u marked"
  293. " as free, partition length is %u)\n", partition,
  294. newblock, sbi->s_partmaps[partition].s_partition_len);
  295. goto error_return;
  296. }
  297. if (!udf_clear_bit(bit, bh->b_data)) {
  298. udf_debug("bit already cleared for block %d\n", bit);
  299. goto repeat;
  300. }
  301. mark_buffer_dirty(bh);
  302. udf_add_free_space(sb, partition, -1);
  303. mutex_unlock(&sbi->s_alloc_mutex);
  304. *err = 0;
  305. return newblock;
  306. error_return:
  307. *err = -EIO;
  308. mutex_unlock(&sbi->s_alloc_mutex);
  309. return 0;
  310. }
  311. static void udf_table_free_blocks(struct super_block *sb,
  312. struct inode *table,
  313. struct kernel_lb_addr *bloc,
  314. uint32_t offset,
  315. uint32_t count)
  316. {
  317. struct udf_sb_info *sbi = UDF_SB(sb);
  318. uint32_t start, end;
  319. uint32_t elen;
  320. struct kernel_lb_addr eloc;
  321. struct extent_position oepos, epos;
  322. int8_t etype;
  323. struct udf_inode_info *iinfo;
  324. int ret = 0;
  325. mutex_lock(&sbi->s_alloc_mutex);
  326. iinfo = UDF_I(table);
  327. udf_add_free_space(sb, sbi->s_partition, count);
  328. start = bloc->logicalBlockNum + offset;
  329. end = bloc->logicalBlockNum + offset + count - 1;
  330. epos.offset = oepos.offset = sizeof(struct unallocSpaceEntry);
  331. elen = 0;
  332. epos.block = oepos.block = iinfo->i_location;
  333. epos.bh = oepos.bh = NULL;
  334. while (count) {
  335. ret = udf_next_aext(table, &epos, &eloc, &elen, &etype, 1);
  336. if (ret < 0)
  337. goto error_return;
  338. if (ret == 0)
  339. break;
  340. if (((eloc.logicalBlockNum +
  341. (elen >> sb->s_blocksize_bits)) == start)) {
  342. if ((0x3FFFFFFF - elen) <
  343. (count << sb->s_blocksize_bits)) {
  344. uint32_t tmp = ((0x3FFFFFFF - elen) >>
  345. sb->s_blocksize_bits);
  346. count -= tmp;
  347. start += tmp;
  348. elen = (etype << 30) |
  349. (0x40000000 - sb->s_blocksize);
  350. } else {
  351. elen = (etype << 30) |
  352. (elen +
  353. (count << sb->s_blocksize_bits));
  354. start += count;
  355. count = 0;
  356. }
  357. udf_write_aext(table, &oepos, &eloc, elen, 1);
  358. } else if (eloc.logicalBlockNum == (end + 1)) {
  359. if ((0x3FFFFFFF - elen) <
  360. (count << sb->s_blocksize_bits)) {
  361. uint32_t tmp = ((0x3FFFFFFF - elen) >>
  362. sb->s_blocksize_bits);
  363. count -= tmp;
  364. end -= tmp;
  365. eloc.logicalBlockNum -= tmp;
  366. elen = (etype << 30) |
  367. (0x40000000 - sb->s_blocksize);
  368. } else {
  369. eloc.logicalBlockNum = start;
  370. elen = (etype << 30) |
  371. (elen +
  372. (count << sb->s_blocksize_bits));
  373. end -= count;
  374. count = 0;
  375. }
  376. udf_write_aext(table, &oepos, &eloc, elen, 1);
  377. }
  378. if (epos.bh != oepos.bh) {
  379. oepos.block = epos.block;
  380. brelse(oepos.bh);
  381. get_bh(epos.bh);
  382. oepos.bh = epos.bh;
  383. oepos.offset = 0;
  384. } else {
  385. oepos.offset = epos.offset;
  386. }
  387. }
  388. if (count) {
  389. /*
  390. * NOTE: we CANNOT use udf_add_aext here, as it can try to
  391. * allocate a new block, and since we hold the super block
  392. * lock already very bad things would happen :)
  393. *
  394. * We copy the behavior of udf_add_aext, but instead of
  395. * trying to allocate a new block close to the existing one,
  396. * we just steal a block from the extent we are trying to add.
  397. *
  398. * It would be nice if the blocks were close together, but it
  399. * isn't required.
  400. */
  401. int adsize;
  402. eloc.logicalBlockNum = start;
  403. elen = EXT_RECORDED_ALLOCATED |
  404. (count << sb->s_blocksize_bits);
  405. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
  406. adsize = sizeof(struct short_ad);
  407. else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
  408. adsize = sizeof(struct long_ad);
  409. else
  410. goto error_return;
  411. if (epos.offset + (2 * adsize) > sb->s_blocksize) {
  412. /* Steal a block from the extent being free'd */
  413. udf_setup_indirect_aext(table, eloc.logicalBlockNum,
  414. &epos);
  415. eloc.logicalBlockNum++;
  416. elen -= sb->s_blocksize;
  417. }
  418. /* It's possible that stealing the block emptied the extent */
  419. if (elen)
  420. __udf_add_aext(table, &epos, &eloc, elen, 1);
  421. }
  422. error_return:
  423. brelse(epos.bh);
  424. brelse(oepos.bh);
  425. mutex_unlock(&sbi->s_alloc_mutex);
  426. return;
  427. }
  428. static int udf_table_prealloc_blocks(struct super_block *sb,
  429. struct inode *table, uint16_t partition,
  430. uint32_t first_block, uint32_t block_count)
  431. {
  432. struct udf_sb_info *sbi = UDF_SB(sb);
  433. int alloc_count = 0;
  434. uint32_t elen, adsize;
  435. struct kernel_lb_addr eloc;
  436. struct extent_position epos;
  437. int8_t etype = -1;
  438. struct udf_inode_info *iinfo;
  439. int ret = 0;
  440. if (first_block >= sbi->s_partmaps[partition].s_partition_len)
  441. return 0;
  442. iinfo = UDF_I(table);
  443. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
  444. adsize = sizeof(struct short_ad);
  445. else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
  446. adsize = sizeof(struct long_ad);
  447. else
  448. return 0;
  449. mutex_lock(&sbi->s_alloc_mutex);
  450. epos.offset = sizeof(struct unallocSpaceEntry);
  451. epos.block = iinfo->i_location;
  452. epos.bh = NULL;
  453. eloc.logicalBlockNum = 0xFFFFFFFF;
  454. while (first_block != eloc.logicalBlockNum) {
  455. ret = udf_next_aext(table, &epos, &eloc, &elen, &etype, 1);
  456. if (ret < 0)
  457. goto err_out;
  458. if (ret == 0)
  459. break;
  460. udf_debug("eloc=%u, elen=%u, first_block=%u\n",
  461. eloc.logicalBlockNum, elen, first_block);
  462. }
  463. if (first_block == eloc.logicalBlockNum) {
  464. epos.offset -= adsize;
  465. alloc_count = (elen >> sb->s_blocksize_bits);
  466. if (alloc_count > block_count) {
  467. alloc_count = block_count;
  468. eloc.logicalBlockNum += alloc_count;
  469. elen -= (alloc_count << sb->s_blocksize_bits);
  470. udf_write_aext(table, &epos, &eloc,
  471. (etype << 30) | elen, 1);
  472. } else
  473. udf_delete_aext(table, epos);
  474. } else {
  475. alloc_count = 0;
  476. }
  477. err_out:
  478. brelse(epos.bh);
  479. if (alloc_count)
  480. udf_add_free_space(sb, partition, -alloc_count);
  481. mutex_unlock(&sbi->s_alloc_mutex);
  482. return alloc_count;
  483. }
  484. static udf_pblk_t udf_table_new_block(struct super_block *sb,
  485. struct inode *table, uint16_t partition,
  486. uint32_t goal, int *err)
  487. {
  488. struct udf_sb_info *sbi = UDF_SB(sb);
  489. uint32_t spread = 0xFFFFFFFF, nspread = 0xFFFFFFFF;
  490. udf_pblk_t newblock = 0;
  491. uint32_t adsize;
  492. uint32_t elen, goal_elen = 0;
  493. struct kernel_lb_addr eloc, goal_eloc;
  494. struct extent_position epos, goal_epos;
  495. int8_t etype;
  496. struct udf_inode_info *iinfo = UDF_I(table);
  497. int ret = 0;
  498. *err = -ENOSPC;
  499. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
  500. adsize = sizeof(struct short_ad);
  501. else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
  502. adsize = sizeof(struct long_ad);
  503. else
  504. return newblock;
  505. mutex_lock(&sbi->s_alloc_mutex);
  506. if (goal >= sbi->s_partmaps[partition].s_partition_len)
  507. goal = 0;
  508. /* We search for the closest matching block to goal. If we find
  509. a exact hit, we stop. Otherwise we keep going till we run out
  510. of extents. We store the buffer_head, bloc, and extoffset
  511. of the current closest match and use that when we are done.
  512. */
  513. epos.offset = sizeof(struct unallocSpaceEntry);
  514. epos.block = iinfo->i_location;
  515. epos.bh = goal_epos.bh = NULL;
  516. while (spread) {
  517. ret = udf_next_aext(table, &epos, &eloc, &elen, &etype, 1);
  518. if (ret <= 0)
  519. break;
  520. if (goal >= eloc.logicalBlockNum) {
  521. if (goal < eloc.logicalBlockNum +
  522. (elen >> sb->s_blocksize_bits))
  523. nspread = 0;
  524. else
  525. nspread = goal - eloc.logicalBlockNum -
  526. (elen >> sb->s_blocksize_bits);
  527. } else {
  528. nspread = eloc.logicalBlockNum - goal;
  529. }
  530. if (nspread < spread) {
  531. spread = nspread;
  532. if (goal_epos.bh != epos.bh) {
  533. brelse(goal_epos.bh);
  534. goal_epos.bh = epos.bh;
  535. get_bh(goal_epos.bh);
  536. }
  537. goal_epos.block = epos.block;
  538. goal_epos.offset = epos.offset - adsize;
  539. goal_eloc = eloc;
  540. goal_elen = (etype << 30) | elen;
  541. }
  542. }
  543. brelse(epos.bh);
  544. if (ret < 0 || spread == 0xFFFFFFFF) {
  545. brelse(goal_epos.bh);
  546. mutex_unlock(&sbi->s_alloc_mutex);
  547. if (ret < 0)
  548. *err = ret;
  549. return 0;
  550. }
  551. /* Only allocate blocks from the beginning of the extent.
  552. That way, we only delete (empty) extents, never have to insert an
  553. extent because of splitting */
  554. /* This works, but very poorly.... */
  555. newblock = goal_eloc.logicalBlockNum;
  556. goal_eloc.logicalBlockNum++;
  557. goal_elen -= sb->s_blocksize;
  558. if (goal_elen)
  559. udf_write_aext(table, &goal_epos, &goal_eloc, goal_elen, 1);
  560. else
  561. udf_delete_aext(table, goal_epos);
  562. brelse(goal_epos.bh);
  563. udf_add_free_space(sb, partition, -1);
  564. mutex_unlock(&sbi->s_alloc_mutex);
  565. *err = 0;
  566. return newblock;
  567. }
  568. void udf_free_blocks(struct super_block *sb, struct inode *inode,
  569. struct kernel_lb_addr *bloc, uint32_t offset,
  570. uint32_t count)
  571. {
  572. uint16_t partition = bloc->partitionReferenceNum;
  573. struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
  574. uint32_t blk;
  575. if (check_add_overflow(bloc->logicalBlockNum, offset, &blk) ||
  576. check_add_overflow(blk, count, &blk) ||
  577. bloc->logicalBlockNum + count > map->s_partition_len) {
  578. udf_debug("Invalid request to free blocks: (%d, %u), off %u, "
  579. "len %u, partition len %u\n",
  580. partition, bloc->logicalBlockNum, offset, count,
  581. map->s_partition_len);
  582. return;
  583. }
  584. if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
  585. udf_bitmap_free_blocks(sb, map->s_uspace.s_bitmap,
  586. bloc, offset, count);
  587. } else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
  588. udf_table_free_blocks(sb, map->s_uspace.s_table,
  589. bloc, offset, count);
  590. }
  591. if (inode) {
  592. inode_sub_bytes(inode,
  593. ((sector_t)count) << sb->s_blocksize_bits);
  594. }
  595. }
  596. inline int udf_prealloc_blocks(struct super_block *sb,
  597. struct inode *inode,
  598. uint16_t partition, uint32_t first_block,
  599. uint32_t block_count)
  600. {
  601. struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
  602. int allocated;
  603. if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
  604. allocated = udf_bitmap_prealloc_blocks(sb,
  605. map->s_uspace.s_bitmap,
  606. partition, first_block,
  607. block_count);
  608. else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
  609. allocated = udf_table_prealloc_blocks(sb,
  610. map->s_uspace.s_table,
  611. partition, first_block,
  612. block_count);
  613. else
  614. return 0;
  615. if (inode && allocated > 0)
  616. inode_add_bytes(inode, allocated << sb->s_blocksize_bits);
  617. return allocated;
  618. }
  619. inline udf_pblk_t udf_new_block(struct super_block *sb,
  620. struct inode *inode,
  621. uint16_t partition, uint32_t goal, int *err)
  622. {
  623. struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
  624. udf_pblk_t block;
  625. if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
  626. block = udf_bitmap_new_block(sb,
  627. map->s_uspace.s_bitmap,
  628. partition, goal, err);
  629. else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
  630. block = udf_table_new_block(sb,
  631. map->s_uspace.s_table,
  632. partition, goal, err);
  633. else {
  634. *err = -EIO;
  635. return 0;
  636. }
  637. if (inode && block)
  638. inode_add_bytes(inode, sb->s_blocksize);
  639. return block;
  640. }