balloc.c 23 KB

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