balloc.c 22 KB

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