balloc.c 19 KB

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