balloc.c 24 KB

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