balloc.c 19 KB

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