balloc.c 24 KB

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