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(nr, addr) ext2_clear_bit(nr, addr)
  27. #define udf_set_bit(nr, addr) ext2_set_bit(nr, addr)
  28. #define udf_test_bit(nr, addr) ext2_test_bit(nr, addr)
  29. #define udf_find_next_one_bit(addr, size, offset) \
  30. ext2_find_next_bit(addr, size, offset)
  31. static int read_block_bitmap(struct super_block *sb,
  32. struct udf_bitmap *bitmap, unsigned int block,
  33. unsigned long bitmap_nr)
  34. {
  35. struct buffer_head *bh = NULL;
  36. int retval = 0;
  37. struct kernel_lb_addr loc;
  38. loc.logicalBlockNum = bitmap->s_extPosition;
  39. loc.partitionReferenceNum = UDF_SB(sb)->s_partition;
  40. bh = udf_tread(sb, udf_get_lb_pblock(sb, &loc, block));
  41. if (!bh)
  42. retval = -EIO;
  43. bitmap->s_block_bitmap[bitmap_nr] = bh;
  44. return retval;
  45. }
  46. static int __load_block_bitmap(struct super_block *sb,
  47. struct udf_bitmap *bitmap,
  48. unsigned int block_group)
  49. {
  50. int retval = 0;
  51. int nr_groups = bitmap->s_nr_groups;
  52. if (block_group >= nr_groups) {
  53. udf_debug("block_group (%d) > nr_groups (%d)\n", block_group,
  54. nr_groups);
  55. }
  56. if (bitmap->s_block_bitmap[block_group]) {
  57. return block_group;
  58. } else {
  59. retval = read_block_bitmap(sb, bitmap, block_group,
  60. block_group);
  61. if (retval < 0)
  62. return retval;
  63. return block_group;
  64. }
  65. }
  66. static inline int load_block_bitmap(struct super_block *sb,
  67. struct udf_bitmap *bitmap,
  68. unsigned int block_group)
  69. {
  70. int slot;
  71. slot = __load_block_bitmap(sb, bitmap, block_group);
  72. if (slot < 0)
  73. return slot;
  74. if (!bitmap->s_block_bitmap[slot])
  75. return -EIO;
  76. return slot;
  77. }
  78. static void udf_add_free_space(struct super_block *sb, u16 partition, u32 cnt)
  79. {
  80. struct udf_sb_info *sbi = UDF_SB(sb);
  81. struct logicalVolIntegrityDesc *lvid;
  82. if (!sbi->s_lvid_bh)
  83. return;
  84. lvid = (struct logicalVolIntegrityDesc *)sbi->s_lvid_bh->b_data;
  85. le32_add_cpu(&lvid->freeSpaceTable[partition], cnt);
  86. udf_updated_lvid(sb);
  87. }
  88. static void udf_bitmap_free_blocks(struct super_block *sb,
  89. struct inode *inode,
  90. struct udf_bitmap *bitmap,
  91. struct kernel_lb_addr *bloc,
  92. uint32_t offset,
  93. uint32_t count)
  94. {
  95. struct udf_sb_info *sbi = UDF_SB(sb);
  96. struct buffer_head *bh = NULL;
  97. struct udf_part_map *partmap;
  98. unsigned long block;
  99. unsigned long block_group;
  100. unsigned long bit;
  101. unsigned long i;
  102. int bitmap_nr;
  103. unsigned long overflow;
  104. mutex_lock(&sbi->s_alloc_mutex);
  105. partmap = &sbi->s_partmaps[bloc->partitionReferenceNum];
  106. if (bloc->logicalBlockNum + count < count ||
  107. (bloc->logicalBlockNum + count) > partmap->s_partition_len) {
  108. udf_debug("%d < %d || %d + %d > %d\n",
  109. bloc->logicalBlockNum, 0, bloc->logicalBlockNum,
  110. count, 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. } else {
  136. udf_add_free_space(sb, sbi->s_partition, 1);
  137. }
  138. }
  139. mark_buffer_dirty(bh);
  140. if (overflow) {
  141. block += count;
  142. count = overflow;
  143. }
  144. } while (overflow);
  145. error_return:
  146. mutex_unlock(&sbi->s_alloc_mutex);
  147. }
  148. static int udf_bitmap_prealloc_blocks(struct super_block *sb,
  149. struct inode *inode,
  150. struct udf_bitmap *bitmap,
  151. uint16_t partition, uint32_t first_block,
  152. uint32_t block_count)
  153. {
  154. struct udf_sb_info *sbi = UDF_SB(sb);
  155. int alloc_count = 0;
  156. int bit, block, block_group, group_start;
  157. int nr_groups, bitmap_nr;
  158. struct buffer_head *bh;
  159. __u32 part_len;
  160. mutex_lock(&sbi->s_alloc_mutex);
  161. part_len = sbi->s_partmaps[partition].s_partition_len;
  162. if (first_block >= part_len)
  163. goto out;
  164. if (first_block + block_count > part_len)
  165. block_count = part_len - first_block;
  166. do {
  167. nr_groups = udf_compute_nr_groups(sb, partition);
  168. block = first_block + (sizeof(struct spaceBitmapDesc) << 3);
  169. block_group = block >> (sb->s_blocksize_bits + 3);
  170. group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc);
  171. bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
  172. if (bitmap_nr < 0)
  173. goto out;
  174. bh = bitmap->s_block_bitmap[bitmap_nr];
  175. bit = block % (sb->s_blocksize << 3);
  176. while (bit < (sb->s_blocksize << 3) && block_count > 0) {
  177. if (!udf_clear_bit(bit, bh->b_data))
  178. goto out;
  179. block_count--;
  180. alloc_count++;
  181. bit++;
  182. block++;
  183. }
  184. mark_buffer_dirty(bh);
  185. } while (block_count > 0);
  186. out:
  187. udf_add_free_space(sb, partition, -alloc_count);
  188. mutex_unlock(&sbi->s_alloc_mutex);
  189. return alloc_count;
  190. }
  191. static int udf_bitmap_new_block(struct super_block *sb,
  192. struct inode *inode,
  193. struct udf_bitmap *bitmap, uint16_t partition,
  194. uint32_t goal, int *err)
  195. {
  196. struct udf_sb_info *sbi = UDF_SB(sb);
  197. int newbit, bit = 0, block, block_group, group_start;
  198. int end_goal, nr_groups, bitmap_nr, i;
  199. struct buffer_head *bh = NULL;
  200. char *ptr;
  201. int newblock = 0;
  202. *err = -ENOSPC;
  203. mutex_lock(&sbi->s_alloc_mutex);
  204. repeat:
  205. if (goal >= sbi->s_partmaps[partition].s_partition_len)
  206. goal = 0;
  207. nr_groups = bitmap->s_nr_groups;
  208. block = goal + (sizeof(struct spaceBitmapDesc) << 3);
  209. block_group = block >> (sb->s_blocksize_bits + 3);
  210. group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc);
  211. bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
  212. if (bitmap_nr < 0)
  213. goto error_return;
  214. bh = bitmap->s_block_bitmap[bitmap_nr];
  215. ptr = memscan((char *)bh->b_data + group_start, 0xFF,
  216. sb->s_blocksize - group_start);
  217. if ((ptr - ((char *)bh->b_data)) < sb->s_blocksize) {
  218. bit = block % (sb->s_blocksize << 3);
  219. if (udf_test_bit(bit, bh->b_data))
  220. goto got_block;
  221. end_goal = (bit + 63) & ~63;
  222. bit = udf_find_next_one_bit(bh->b_data, end_goal, bit);
  223. if (bit < end_goal)
  224. goto got_block;
  225. ptr = memscan((char *)bh->b_data + (bit >> 3), 0xFF,
  226. sb->s_blocksize - ((bit + 7) >> 3));
  227. newbit = (ptr - ((char *)bh->b_data)) << 3;
  228. if (newbit < sb->s_blocksize << 3) {
  229. bit = newbit;
  230. goto search_back;
  231. }
  232. newbit = udf_find_next_one_bit(bh->b_data,
  233. sb->s_blocksize << 3, bit);
  234. if (newbit < sb->s_blocksize << 3) {
  235. bit = newbit;
  236. goto got_block;
  237. }
  238. }
  239. for (i = 0; i < (nr_groups * 2); i++) {
  240. block_group++;
  241. if (block_group >= nr_groups)
  242. block_group = 0;
  243. group_start = block_group ? 0 : sizeof(struct spaceBitmapDesc);
  244. bitmap_nr = load_block_bitmap(sb, bitmap, block_group);
  245. if (bitmap_nr < 0)
  246. goto error_return;
  247. bh = bitmap->s_block_bitmap[bitmap_nr];
  248. if (i < nr_groups) {
  249. ptr = memscan((char *)bh->b_data + group_start, 0xFF,
  250. sb->s_blocksize - group_start);
  251. if ((ptr - ((char *)bh->b_data)) < sb->s_blocksize) {
  252. bit = (ptr - ((char *)bh->b_data)) << 3;
  253. break;
  254. }
  255. } else {
  256. bit = udf_find_next_one_bit((char *)bh->b_data,
  257. sb->s_blocksize << 3,
  258. group_start << 3);
  259. if (bit < sb->s_blocksize << 3)
  260. break;
  261. }
  262. }
  263. if (i >= (nr_groups * 2)) {
  264. mutex_unlock(&sbi->s_alloc_mutex);
  265. return newblock;
  266. }
  267. if (bit < sb->s_blocksize << 3)
  268. goto search_back;
  269. else
  270. bit = udf_find_next_one_bit(bh->b_data, sb->s_blocksize << 3,
  271. group_start << 3);
  272. if (bit >= sb->s_blocksize << 3) {
  273. mutex_unlock(&sbi->s_alloc_mutex);
  274. return 0;
  275. }
  276. search_back:
  277. i = 0;
  278. while (i < 7 && bit > (group_start << 3) &&
  279. udf_test_bit(bit - 1, bh->b_data)) {
  280. ++i;
  281. --bit;
  282. }
  283. got_block:
  284. newblock = bit + (block_group << (sb->s_blocksize_bits + 3)) -
  285. (sizeof(struct spaceBitmapDesc) << 3);
  286. if (!udf_clear_bit(bit, bh->b_data)) {
  287. udf_debug("bit already cleared for block %d\n", bit);
  288. goto repeat;
  289. }
  290. mark_buffer_dirty(bh);
  291. udf_add_free_space(sb, partition, -1);
  292. mutex_unlock(&sbi->s_alloc_mutex);
  293. *err = 0;
  294. return newblock;
  295. error_return:
  296. *err = -EIO;
  297. mutex_unlock(&sbi->s_alloc_mutex);
  298. return 0;
  299. }
  300. static void udf_table_free_blocks(struct super_block *sb,
  301. struct inode *inode,
  302. struct inode *table,
  303. struct kernel_lb_addr *bloc,
  304. uint32_t offset,
  305. uint32_t count)
  306. {
  307. struct udf_sb_info *sbi = UDF_SB(sb);
  308. struct udf_part_map *partmap;
  309. uint32_t start, end;
  310. uint32_t elen;
  311. struct kernel_lb_addr eloc;
  312. struct extent_position oepos, epos;
  313. int8_t etype;
  314. int i;
  315. struct udf_inode_info *iinfo;
  316. mutex_lock(&sbi->s_alloc_mutex);
  317. partmap = &sbi->s_partmaps[bloc->partitionReferenceNum];
  318. if (bloc->logicalBlockNum + count < count ||
  319. (bloc->logicalBlockNum + count) > partmap->s_partition_len) {
  320. udf_debug("%d < %d || %d + %d > %d\n",
  321. bloc->logicalBlockNum, 0, 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. }