balloc.c 22 KB

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