balloc.c 22 KB

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