balloc.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  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 (%u) > 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("%u < %d || %u + %u > %u\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 %lu already set\n", bit + i);
  128. udf_debug("byte=%2x\n",
  129. ((__u8 *)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 udf_pblk_t 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;
  190. udf_pblk_t block;
  191. int block_group, group_start;
  192. int end_goal, nr_groups, bitmap_nr, i;
  193. struct buffer_head *bh = NULL;
  194. char *ptr;
  195. udf_pblk_t newblock = 0;
  196. *err = -ENOSPC;
  197. mutex_lock(&sbi->s_alloc_mutex);
  198. repeat:
  199. if (goal >= sbi->s_partmaps[partition].s_partition_len)
  200. goal = 0;
  201. nr_groups = bitmap->s_nr_groups;
  202. block = goal + (sizeof(struct spaceBitmapDesc) << 3);
  203. block_group = block >> (sb->s_blocksize_bits + 3);
  204. group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc);
  205. bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
  206. if (bitmap_nr < 0)
  207. goto error_return;
  208. bh = bitmap->s_block_bitmap[bitmap_nr];
  209. ptr = memscan((char *)bh->b_data + group_start, 0xFF,
  210. sb->s_blocksize - group_start);
  211. if ((ptr - ((char *)bh->b_data)) < sb->s_blocksize) {
  212. bit = block % (sb->s_blocksize << 3);
  213. if (udf_test_bit(bit, bh->b_data))
  214. goto got_block;
  215. end_goal = (bit + 63) & ~63;
  216. bit = udf_find_next_one_bit(bh->b_data, end_goal, bit);
  217. if (bit < end_goal)
  218. goto got_block;
  219. ptr = memscan((char *)bh->b_data + (bit >> 3), 0xFF,
  220. sb->s_blocksize - ((bit + 7) >> 3));
  221. newbit = (ptr - ((char *)bh->b_data)) << 3;
  222. if (newbit < sb->s_blocksize << 3) {
  223. bit = newbit;
  224. goto search_back;
  225. }
  226. newbit = udf_find_next_one_bit(bh->b_data,
  227. sb->s_blocksize << 3, bit);
  228. if (newbit < sb->s_blocksize << 3) {
  229. bit = newbit;
  230. goto got_block;
  231. }
  232. }
  233. for (i = 0; i < (nr_groups * 2); i++) {
  234. block_group++;
  235. if (block_group >= nr_groups)
  236. block_group = 0;
  237. group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc);
  238. bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
  239. if (bitmap_nr < 0)
  240. goto error_return;
  241. bh = bitmap->s_block_bitmap[bitmap_nr];
  242. if (i < nr_groups) {
  243. ptr = memscan((char *)bh->b_data + group_start, 0xFF,
  244. sb->s_blocksize - group_start);
  245. if ((ptr - ((char *)bh->b_data)) < sb->s_blocksize) {
  246. bit = (ptr - ((char *)bh->b_data)) << 3;
  247. break;
  248. }
  249. } else {
  250. bit = udf_find_next_one_bit(bh->b_data,
  251. sb->s_blocksize << 3,
  252. group_start << 3);
  253. if (bit < sb->s_blocksize << 3)
  254. break;
  255. }
  256. }
  257. if (i >= (nr_groups * 2)) {
  258. mutex_unlock(&sbi->s_alloc_mutex);
  259. return newblock;
  260. }
  261. if (bit < sb->s_blocksize << 3)
  262. goto search_back;
  263. else
  264. bit = udf_find_next_one_bit(bh->b_data, sb->s_blocksize << 3,
  265. group_start << 3);
  266. if (bit >= sb->s_blocksize << 3) {
  267. mutex_unlock(&sbi->s_alloc_mutex);
  268. return 0;
  269. }
  270. search_back:
  271. i = 0;
  272. while (i < 7 && bit > (group_start << 3) &&
  273. udf_test_bit(bit - 1, bh->b_data)) {
  274. ++i;
  275. --bit;
  276. }
  277. got_block:
  278. newblock = bit + (block_group << (sb->s_blocksize_bits + 3)) -
  279. (sizeof(struct spaceBitmapDesc) << 3);
  280. if (!udf_clear_bit(bit, bh->b_data)) {
  281. udf_debug("bit already cleared for block %d\n", bit);
  282. goto repeat;
  283. }
  284. mark_buffer_dirty(bh);
  285. udf_add_free_space(sb, partition, -1);
  286. mutex_unlock(&sbi->s_alloc_mutex);
  287. *err = 0;
  288. return newblock;
  289. error_return:
  290. *err = -EIO;
  291. mutex_unlock(&sbi->s_alloc_mutex);
  292. return 0;
  293. }
  294. static void udf_table_free_blocks(struct super_block *sb,
  295. struct inode *table,
  296. struct kernel_lb_addr *bloc,
  297. uint32_t offset,
  298. uint32_t count)
  299. {
  300. struct udf_sb_info *sbi = UDF_SB(sb);
  301. struct udf_part_map *partmap;
  302. uint32_t start, end;
  303. uint32_t elen;
  304. struct kernel_lb_addr eloc;
  305. struct extent_position oepos, epos;
  306. int8_t etype;
  307. struct udf_inode_info *iinfo;
  308. mutex_lock(&sbi->s_alloc_mutex);
  309. partmap = &sbi->s_partmaps[bloc->partitionReferenceNum];
  310. if (bloc->logicalBlockNum + count < count ||
  311. (bloc->logicalBlockNum + count) > partmap->s_partition_len) {
  312. udf_debug("%u < %d || %u + %u > %u\n",
  313. bloc->logicalBlockNum, 0,
  314. bloc->logicalBlockNum, count,
  315. partmap->s_partition_len);
  316. goto error_return;
  317. }
  318. iinfo = UDF_I(table);
  319. udf_add_free_space(sb, sbi->s_partition, count);
  320. start = bloc->logicalBlockNum + offset;
  321. end = bloc->logicalBlockNum + offset + count - 1;
  322. epos.offset = oepos.offset = sizeof(struct unallocSpaceEntry);
  323. elen = 0;
  324. epos.block = oepos.block = iinfo->i_location;
  325. epos.bh = oepos.bh = NULL;
  326. while (count &&
  327. (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
  328. if (((eloc.logicalBlockNum +
  329. (elen >> sb->s_blocksize_bits)) == start)) {
  330. if ((0x3FFFFFFF - elen) <
  331. (count << sb->s_blocksize_bits)) {
  332. uint32_t tmp = ((0x3FFFFFFF - elen) >>
  333. sb->s_blocksize_bits);
  334. count -= tmp;
  335. start += tmp;
  336. elen = (etype << 30) |
  337. (0x40000000 - sb->s_blocksize);
  338. } else {
  339. elen = (etype << 30) |
  340. (elen +
  341. (count << sb->s_blocksize_bits));
  342. start += count;
  343. count = 0;
  344. }
  345. udf_write_aext(table, &oepos, &eloc, elen, 1);
  346. } else if (eloc.logicalBlockNum == (end + 1)) {
  347. if ((0x3FFFFFFF - elen) <
  348. (count << sb->s_blocksize_bits)) {
  349. uint32_t tmp = ((0x3FFFFFFF - elen) >>
  350. sb->s_blocksize_bits);
  351. count -= tmp;
  352. end -= tmp;
  353. eloc.logicalBlockNum -= tmp;
  354. elen = (etype << 30) |
  355. (0x40000000 - sb->s_blocksize);
  356. } else {
  357. eloc.logicalBlockNum = start;
  358. elen = (etype << 30) |
  359. (elen +
  360. (count << sb->s_blocksize_bits));
  361. end -= count;
  362. count = 0;
  363. }
  364. udf_write_aext(table, &oepos, &eloc, elen, 1);
  365. }
  366. if (epos.bh != oepos.bh) {
  367. oepos.block = epos.block;
  368. brelse(oepos.bh);
  369. get_bh(epos.bh);
  370. oepos.bh = epos.bh;
  371. oepos.offset = 0;
  372. } else {
  373. oepos.offset = epos.offset;
  374. }
  375. }
  376. if (count) {
  377. /*
  378. * NOTE: we CANNOT use udf_add_aext here, as it can try to
  379. * allocate a new block, and since we hold the super block
  380. * lock already very bad things would happen :)
  381. *
  382. * We copy the behavior of udf_add_aext, but instead of
  383. * trying to allocate a new block close to the existing one,
  384. * we just steal a block from the extent we are trying to add.
  385. *
  386. * It would be nice if the blocks were close together, but it
  387. * isn't required.
  388. */
  389. int adsize;
  390. eloc.logicalBlockNum = start;
  391. elen = EXT_RECORDED_ALLOCATED |
  392. (count << sb->s_blocksize_bits);
  393. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
  394. adsize = sizeof(struct short_ad);
  395. else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
  396. adsize = sizeof(struct long_ad);
  397. else {
  398. brelse(oepos.bh);
  399. brelse(epos.bh);
  400. goto error_return;
  401. }
  402. if (epos.offset + (2 * adsize) > sb->s_blocksize) {
  403. /* Steal a block from the extent being free'd */
  404. udf_setup_indirect_aext(table, eloc.logicalBlockNum,
  405. &epos);
  406. eloc.logicalBlockNum++;
  407. elen -= sb->s_blocksize;
  408. }
  409. /* It's possible that stealing the block emptied the extent */
  410. if (elen)
  411. __udf_add_aext(table, &epos, &eloc, elen, 1);
  412. }
  413. brelse(epos.bh);
  414. brelse(oepos.bh);
  415. error_return:
  416. mutex_unlock(&sbi->s_alloc_mutex);
  417. return;
  418. }
  419. static int udf_table_prealloc_blocks(struct super_block *sb,
  420. struct inode *table, uint16_t partition,
  421. uint32_t first_block, uint32_t block_count)
  422. {
  423. struct udf_sb_info *sbi = UDF_SB(sb);
  424. int alloc_count = 0;
  425. uint32_t elen, adsize;
  426. struct kernel_lb_addr eloc;
  427. struct extent_position epos;
  428. int8_t etype = -1;
  429. struct udf_inode_info *iinfo;
  430. if (first_block >= sbi->s_partmaps[partition].s_partition_len)
  431. return 0;
  432. iinfo = UDF_I(table);
  433. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
  434. adsize = sizeof(struct short_ad);
  435. else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
  436. adsize = sizeof(struct long_ad);
  437. else
  438. return 0;
  439. mutex_lock(&sbi->s_alloc_mutex);
  440. epos.offset = sizeof(struct unallocSpaceEntry);
  441. epos.block = iinfo->i_location;
  442. epos.bh = NULL;
  443. eloc.logicalBlockNum = 0xFFFFFFFF;
  444. while (first_block != eloc.logicalBlockNum &&
  445. (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
  446. udf_debug("eloc=%u, elen=%u, first_block=%u\n",
  447. eloc.logicalBlockNum, elen, first_block);
  448. ; /* empty loop body */
  449. }
  450. if (first_block == eloc.logicalBlockNum) {
  451. epos.offset -= adsize;
  452. alloc_count = (elen >> sb->s_blocksize_bits);
  453. if (alloc_count > block_count) {
  454. alloc_count = block_count;
  455. eloc.logicalBlockNum += alloc_count;
  456. elen -= (alloc_count << sb->s_blocksize_bits);
  457. udf_write_aext(table, &epos, &eloc,
  458. (etype << 30) | elen, 1);
  459. } else
  460. udf_delete_aext(table, epos);
  461. } else {
  462. alloc_count = 0;
  463. }
  464. brelse(epos.bh);
  465. if (alloc_count)
  466. udf_add_free_space(sb, partition, -alloc_count);
  467. mutex_unlock(&sbi->s_alloc_mutex);
  468. return alloc_count;
  469. }
  470. static udf_pblk_t udf_table_new_block(struct super_block *sb,
  471. struct inode *table, uint16_t partition,
  472. uint32_t goal, int *err)
  473. {
  474. struct udf_sb_info *sbi = UDF_SB(sb);
  475. uint32_t spread = 0xFFFFFFFF, nspread = 0xFFFFFFFF;
  476. udf_pblk_t newblock = 0;
  477. uint32_t adsize;
  478. uint32_t elen, goal_elen = 0;
  479. struct kernel_lb_addr eloc, uninitialized_var(goal_eloc);
  480. struct extent_position epos, goal_epos;
  481. int8_t etype;
  482. struct udf_inode_info *iinfo = UDF_I(table);
  483. *err = -ENOSPC;
  484. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
  485. adsize = sizeof(struct short_ad);
  486. else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
  487. adsize = sizeof(struct long_ad);
  488. else
  489. return newblock;
  490. mutex_lock(&sbi->s_alloc_mutex);
  491. if (goal >= sbi->s_partmaps[partition].s_partition_len)
  492. goal = 0;
  493. /* We search for the closest matching block to goal. If we find
  494. a exact hit, we stop. Otherwise we keep going till we run out
  495. of extents. We store the buffer_head, bloc, and extoffset
  496. of the current closest match and use that when we are done.
  497. */
  498. epos.offset = sizeof(struct unallocSpaceEntry);
  499. epos.block = iinfo->i_location;
  500. epos.bh = goal_epos.bh = NULL;
  501. while (spread &&
  502. (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
  503. if (goal >= eloc.logicalBlockNum) {
  504. if (goal < eloc.logicalBlockNum +
  505. (elen >> sb->s_blocksize_bits))
  506. nspread = 0;
  507. else
  508. nspread = goal - eloc.logicalBlockNum -
  509. (elen >> sb->s_blocksize_bits);
  510. } else {
  511. nspread = eloc.logicalBlockNum - goal;
  512. }
  513. if (nspread < spread) {
  514. spread = nspread;
  515. if (goal_epos.bh != epos.bh) {
  516. brelse(goal_epos.bh);
  517. goal_epos.bh = epos.bh;
  518. get_bh(goal_epos.bh);
  519. }
  520. goal_epos.block = epos.block;
  521. goal_epos.offset = epos.offset - adsize;
  522. goal_eloc = eloc;
  523. goal_elen = (etype << 30) | elen;
  524. }
  525. }
  526. brelse(epos.bh);
  527. if (spread == 0xFFFFFFFF) {
  528. brelse(goal_epos.bh);
  529. mutex_unlock(&sbi->s_alloc_mutex);
  530. return 0;
  531. }
  532. /* Only allocate blocks from the beginning of the extent.
  533. That way, we only delete (empty) extents, never have to insert an
  534. extent because of splitting */
  535. /* This works, but very poorly.... */
  536. newblock = goal_eloc.logicalBlockNum;
  537. goal_eloc.logicalBlockNum++;
  538. goal_elen -= sb->s_blocksize;
  539. if (goal_elen)
  540. udf_write_aext(table, &goal_epos, &goal_eloc, goal_elen, 1);
  541. else
  542. udf_delete_aext(table, goal_epos);
  543. brelse(goal_epos.bh);
  544. udf_add_free_space(sb, partition, -1);
  545. mutex_unlock(&sbi->s_alloc_mutex);
  546. *err = 0;
  547. return newblock;
  548. }
  549. void udf_free_blocks(struct super_block *sb, struct inode *inode,
  550. struct kernel_lb_addr *bloc, uint32_t offset,
  551. uint32_t count)
  552. {
  553. uint16_t partition = bloc->partitionReferenceNum;
  554. struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
  555. if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
  556. udf_bitmap_free_blocks(sb, map->s_uspace.s_bitmap,
  557. bloc, offset, count);
  558. } else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
  559. udf_table_free_blocks(sb, map->s_uspace.s_table,
  560. bloc, offset, count);
  561. } else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) {
  562. udf_bitmap_free_blocks(sb, map->s_fspace.s_bitmap,
  563. bloc, offset, count);
  564. } else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) {
  565. udf_table_free_blocks(sb, map->s_fspace.s_table,
  566. bloc, offset, count);
  567. }
  568. if (inode) {
  569. inode_sub_bytes(inode,
  570. ((sector_t)count) << sb->s_blocksize_bits);
  571. }
  572. }
  573. inline int udf_prealloc_blocks(struct super_block *sb,
  574. struct inode *inode,
  575. uint16_t partition, uint32_t first_block,
  576. uint32_t block_count)
  577. {
  578. struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
  579. int allocated;
  580. if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
  581. allocated = udf_bitmap_prealloc_blocks(sb,
  582. map->s_uspace.s_bitmap,
  583. partition, first_block,
  584. block_count);
  585. else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
  586. allocated = udf_table_prealloc_blocks(sb,
  587. map->s_uspace.s_table,
  588. partition, first_block,
  589. block_count);
  590. else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
  591. allocated = udf_bitmap_prealloc_blocks(sb,
  592. map->s_fspace.s_bitmap,
  593. partition, first_block,
  594. block_count);
  595. else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
  596. allocated = udf_table_prealloc_blocks(sb,
  597. map->s_fspace.s_table,
  598. partition, first_block,
  599. block_count);
  600. else
  601. return 0;
  602. if (inode && allocated > 0)
  603. inode_add_bytes(inode, allocated << sb->s_blocksize_bits);
  604. return allocated;
  605. }
  606. inline udf_pblk_t udf_new_block(struct super_block *sb,
  607. struct inode *inode,
  608. uint16_t partition, uint32_t goal, int *err)
  609. {
  610. struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
  611. udf_pblk_t block;
  612. if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
  613. block = udf_bitmap_new_block(sb,
  614. map->s_uspace.s_bitmap,
  615. partition, goal, err);
  616. else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
  617. block = udf_table_new_block(sb,
  618. map->s_uspace.s_table,
  619. partition, goal, err);
  620. else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
  621. block = udf_bitmap_new_block(sb,
  622. map->s_fspace.s_bitmap,
  623. partition, goal, err);
  624. else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
  625. block = udf_table_new_block(sb,
  626. map->s_fspace.s_table,
  627. partition, goal, err);
  628. else {
  629. *err = -EIO;
  630. return 0;
  631. }
  632. if (inode && block)
  633. inode_add_bytes(inode, sb->s_blocksize);
  634. return block;
  635. }