balloc.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  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. struct short_ad *sad = NULL;
  389. struct long_ad *lad = NULL;
  390. struct allocExtDesc *aed;
  391. eloc.logicalBlockNum = start;
  392. elen = EXT_RECORDED_ALLOCATED |
  393. (count << sb->s_blocksize_bits);
  394. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
  395. adsize = sizeof(struct short_ad);
  396. else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
  397. adsize = sizeof(struct long_ad);
  398. else {
  399. brelse(oepos.bh);
  400. brelse(epos.bh);
  401. goto error_return;
  402. }
  403. if (epos.offset + (2 * adsize) > sb->s_blocksize) {
  404. unsigned char *sptr, *dptr;
  405. int loffset;
  406. brelse(oepos.bh);
  407. oepos = epos;
  408. /* Steal a block from the extent being free'd */
  409. epos.block.logicalBlockNum = eloc.logicalBlockNum;
  410. eloc.logicalBlockNum++;
  411. elen -= sb->s_blocksize;
  412. epos.bh = udf_tread(sb,
  413. udf_get_lb_pblock(sb, &epos.block, 0));
  414. if (!epos.bh) {
  415. brelse(oepos.bh);
  416. goto error_return;
  417. }
  418. aed = (struct allocExtDesc *)(epos.bh->b_data);
  419. aed->previousAllocExtLocation =
  420. cpu_to_le32(oepos.block.logicalBlockNum);
  421. if (epos.offset + adsize > sb->s_blocksize) {
  422. loffset = epos.offset;
  423. aed->lengthAllocDescs = cpu_to_le32(adsize);
  424. sptr = iinfo->i_ext.i_data + epos.offset
  425. - adsize;
  426. dptr = epos.bh->b_data +
  427. sizeof(struct allocExtDesc);
  428. memcpy(dptr, sptr, adsize);
  429. epos.offset = sizeof(struct allocExtDesc) +
  430. adsize;
  431. } else {
  432. loffset = epos.offset + adsize;
  433. aed->lengthAllocDescs = cpu_to_le32(0);
  434. if (oepos.bh) {
  435. sptr = oepos.bh->b_data + epos.offset;
  436. aed = (struct allocExtDesc *)
  437. oepos.bh->b_data;
  438. le32_add_cpu(&aed->lengthAllocDescs,
  439. adsize);
  440. } else {
  441. sptr = iinfo->i_ext.i_data +
  442. epos.offset;
  443. iinfo->i_lenAlloc += adsize;
  444. mark_inode_dirty(table);
  445. }
  446. epos.offset = sizeof(struct allocExtDesc);
  447. }
  448. if (sbi->s_udfrev >= 0x0200)
  449. udf_new_tag(epos.bh->b_data, TAG_IDENT_AED,
  450. 3, 1, epos.block.logicalBlockNum,
  451. sizeof(struct tag));
  452. else
  453. udf_new_tag(epos.bh->b_data, TAG_IDENT_AED,
  454. 2, 1, epos.block.logicalBlockNum,
  455. sizeof(struct tag));
  456. switch (iinfo->i_alloc_type) {
  457. case ICBTAG_FLAG_AD_SHORT:
  458. sad = (struct short_ad *)sptr;
  459. sad->extLength = cpu_to_le32(
  460. EXT_NEXT_EXTENT_ALLOCDECS |
  461. sb->s_blocksize);
  462. sad->extPosition =
  463. cpu_to_le32(epos.block.logicalBlockNum);
  464. break;
  465. case ICBTAG_FLAG_AD_LONG:
  466. lad = (struct long_ad *)sptr;
  467. lad->extLength = cpu_to_le32(
  468. EXT_NEXT_EXTENT_ALLOCDECS |
  469. sb->s_blocksize);
  470. lad->extLocation =
  471. cpu_to_lelb(epos.block);
  472. break;
  473. }
  474. if (oepos.bh) {
  475. udf_update_tag(oepos.bh->b_data, loffset);
  476. mark_buffer_dirty(oepos.bh);
  477. } else {
  478. mark_inode_dirty(table);
  479. }
  480. }
  481. /* It's possible that stealing the block emptied the extent */
  482. if (elen) {
  483. udf_write_aext(table, &epos, &eloc, elen, 1);
  484. if (!epos.bh) {
  485. iinfo->i_lenAlloc += adsize;
  486. mark_inode_dirty(table);
  487. } else {
  488. aed = (struct allocExtDesc *)epos.bh->b_data;
  489. le32_add_cpu(&aed->lengthAllocDescs, adsize);
  490. udf_update_tag(epos.bh->b_data, epos.offset);
  491. mark_buffer_dirty(epos.bh);
  492. }
  493. }
  494. }
  495. brelse(epos.bh);
  496. brelse(oepos.bh);
  497. error_return:
  498. mutex_unlock(&sbi->s_alloc_mutex);
  499. return;
  500. }
  501. static int udf_table_prealloc_blocks(struct super_block *sb,
  502. struct inode *table, uint16_t partition,
  503. uint32_t first_block, uint32_t block_count)
  504. {
  505. struct udf_sb_info *sbi = UDF_SB(sb);
  506. int alloc_count = 0;
  507. uint32_t elen, adsize;
  508. struct kernel_lb_addr eloc;
  509. struct extent_position epos;
  510. int8_t etype = -1;
  511. struct udf_inode_info *iinfo;
  512. if (first_block >= sbi->s_partmaps[partition].s_partition_len)
  513. return 0;
  514. iinfo = UDF_I(table);
  515. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
  516. adsize = sizeof(struct short_ad);
  517. else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
  518. adsize = sizeof(struct long_ad);
  519. else
  520. return 0;
  521. mutex_lock(&sbi->s_alloc_mutex);
  522. epos.offset = sizeof(struct unallocSpaceEntry);
  523. epos.block = iinfo->i_location;
  524. epos.bh = NULL;
  525. eloc.logicalBlockNum = 0xFFFFFFFF;
  526. while (first_block != eloc.logicalBlockNum &&
  527. (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
  528. udf_debug("eloc=%d, elen=%d, first_block=%d\n",
  529. eloc.logicalBlockNum, elen, first_block);
  530. ; /* empty loop body */
  531. }
  532. if (first_block == eloc.logicalBlockNum) {
  533. epos.offset -= adsize;
  534. alloc_count = (elen >> sb->s_blocksize_bits);
  535. if (alloc_count > block_count) {
  536. alloc_count = block_count;
  537. eloc.logicalBlockNum += alloc_count;
  538. elen -= (alloc_count << sb->s_blocksize_bits);
  539. udf_write_aext(table, &epos, &eloc,
  540. (etype << 30) | elen, 1);
  541. } else
  542. udf_delete_aext(table, epos, eloc,
  543. (etype << 30) | elen);
  544. } else {
  545. alloc_count = 0;
  546. }
  547. brelse(epos.bh);
  548. if (alloc_count)
  549. udf_add_free_space(sb, partition, -alloc_count);
  550. mutex_unlock(&sbi->s_alloc_mutex);
  551. return alloc_count;
  552. }
  553. static int udf_table_new_block(struct super_block *sb,
  554. struct inode *table, uint16_t partition,
  555. uint32_t goal, int *err)
  556. {
  557. struct udf_sb_info *sbi = UDF_SB(sb);
  558. uint32_t spread = 0xFFFFFFFF, nspread = 0xFFFFFFFF;
  559. uint32_t newblock = 0, adsize;
  560. uint32_t elen, goal_elen = 0;
  561. struct kernel_lb_addr eloc, uninitialized_var(goal_eloc);
  562. struct extent_position epos, goal_epos;
  563. int8_t etype;
  564. struct udf_inode_info *iinfo = UDF_I(table);
  565. *err = -ENOSPC;
  566. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
  567. adsize = sizeof(struct short_ad);
  568. else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
  569. adsize = sizeof(struct long_ad);
  570. else
  571. return newblock;
  572. mutex_lock(&sbi->s_alloc_mutex);
  573. if (goal >= sbi->s_partmaps[partition].s_partition_len)
  574. goal = 0;
  575. /* We search for the closest matching block to goal. If we find
  576. a exact hit, we stop. Otherwise we keep going till we run out
  577. of extents. We store the buffer_head, bloc, and extoffset
  578. of the current closest match and use that when we are done.
  579. */
  580. epos.offset = sizeof(struct unallocSpaceEntry);
  581. epos.block = iinfo->i_location;
  582. epos.bh = goal_epos.bh = NULL;
  583. while (spread &&
  584. (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
  585. if (goal >= eloc.logicalBlockNum) {
  586. if (goal < eloc.logicalBlockNum +
  587. (elen >> sb->s_blocksize_bits))
  588. nspread = 0;
  589. else
  590. nspread = goal - eloc.logicalBlockNum -
  591. (elen >> sb->s_blocksize_bits);
  592. } else {
  593. nspread = eloc.logicalBlockNum - goal;
  594. }
  595. if (nspread < spread) {
  596. spread = nspread;
  597. if (goal_epos.bh != epos.bh) {
  598. brelse(goal_epos.bh);
  599. goal_epos.bh = epos.bh;
  600. get_bh(goal_epos.bh);
  601. }
  602. goal_epos.block = epos.block;
  603. goal_epos.offset = epos.offset - adsize;
  604. goal_eloc = eloc;
  605. goal_elen = (etype << 30) | elen;
  606. }
  607. }
  608. brelse(epos.bh);
  609. if (spread == 0xFFFFFFFF) {
  610. brelse(goal_epos.bh);
  611. mutex_unlock(&sbi->s_alloc_mutex);
  612. return 0;
  613. }
  614. /* Only allocate blocks from the beginning of the extent.
  615. That way, we only delete (empty) extents, never have to insert an
  616. extent because of splitting */
  617. /* This works, but very poorly.... */
  618. newblock = goal_eloc.logicalBlockNum;
  619. goal_eloc.logicalBlockNum++;
  620. goal_elen -= sb->s_blocksize;
  621. if (goal_elen)
  622. udf_write_aext(table, &goal_epos, &goal_eloc, goal_elen, 1);
  623. else
  624. udf_delete_aext(table, goal_epos, goal_eloc, goal_elen);
  625. brelse(goal_epos.bh);
  626. udf_add_free_space(sb, partition, -1);
  627. mutex_unlock(&sbi->s_alloc_mutex);
  628. *err = 0;
  629. return newblock;
  630. }
  631. void udf_free_blocks(struct super_block *sb, struct inode *inode,
  632. struct kernel_lb_addr *bloc, uint32_t offset,
  633. uint32_t count)
  634. {
  635. uint16_t partition = bloc->partitionReferenceNum;
  636. struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
  637. if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
  638. udf_bitmap_free_blocks(sb, map->s_uspace.s_bitmap,
  639. bloc, offset, count);
  640. } else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
  641. udf_table_free_blocks(sb, map->s_uspace.s_table,
  642. bloc, offset, count);
  643. } else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) {
  644. udf_bitmap_free_blocks(sb, map->s_fspace.s_bitmap,
  645. bloc, offset, count);
  646. } else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) {
  647. udf_table_free_blocks(sb, map->s_fspace.s_table,
  648. bloc, offset, count);
  649. }
  650. if (inode) {
  651. inode_sub_bytes(inode,
  652. ((sector_t)count) << sb->s_blocksize_bits);
  653. }
  654. }
  655. inline int udf_prealloc_blocks(struct super_block *sb,
  656. struct inode *inode,
  657. uint16_t partition, uint32_t first_block,
  658. uint32_t block_count)
  659. {
  660. struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
  661. int allocated;
  662. if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
  663. allocated = udf_bitmap_prealloc_blocks(sb,
  664. map->s_uspace.s_bitmap,
  665. partition, first_block,
  666. block_count);
  667. else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
  668. allocated = udf_table_prealloc_blocks(sb,
  669. map->s_uspace.s_table,
  670. partition, first_block,
  671. block_count);
  672. else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
  673. allocated = udf_bitmap_prealloc_blocks(sb,
  674. map->s_fspace.s_bitmap,
  675. partition, first_block,
  676. block_count);
  677. else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
  678. allocated = udf_table_prealloc_blocks(sb,
  679. map->s_fspace.s_table,
  680. partition, first_block,
  681. block_count);
  682. else
  683. return 0;
  684. if (inode && allocated > 0)
  685. inode_add_bytes(inode, allocated << sb->s_blocksize_bits);
  686. return allocated;
  687. }
  688. inline int udf_new_block(struct super_block *sb,
  689. struct inode *inode,
  690. uint16_t partition, uint32_t goal, int *err)
  691. {
  692. struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
  693. int block;
  694. if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
  695. block = udf_bitmap_new_block(sb,
  696. map->s_uspace.s_bitmap,
  697. partition, goal, err);
  698. else if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
  699. block = udf_table_new_block(sb,
  700. map->s_uspace.s_table,
  701. partition, goal, err);
  702. else if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
  703. block = udf_bitmap_new_block(sb,
  704. map->s_fspace.s_bitmap,
  705. partition, goal, err);
  706. else if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
  707. block = udf_table_new_block(sb,
  708. map->s_fspace.s_table,
  709. partition, goal, err);
  710. else {
  711. *err = -EIO;
  712. return 0;
  713. }
  714. if (inode && block)
  715. inode_add_bytes(inode, sb->s_blocksize);
  716. return block;
  717. }