balloc.c 22 KB

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