balloc.c 22 KB

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