balloc.c 24 KB

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