balloc.c 24 KB

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