balloc.c 23 KB

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