balloc.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869
  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(inode) + epos.offset -
  478. udf_file_entry_alloc_offset(inode) +
  479. UDF_I_LENEATTR(inode) - adsize;
  480. dptr = epos.bh->b_data + sizeof(struct allocExtDesc);
  481. memcpy(dptr, sptr, adsize);
  482. epos.offset = sizeof(struct allocExtDesc) + adsize;
  483. } else {
  484. loffset = epos.offset + adsize;
  485. aed->lengthAllocDescs = cpu_to_le32(0);
  486. sptr = oepos.bh->b_data + epos.offset;
  487. epos.offset = sizeof(struct allocExtDesc);
  488. if (oepos.bh) {
  489. aed = (struct allocExtDesc *)oepos.bh->b_data;
  490. aed->lengthAllocDescs =
  491. cpu_to_le32(le32_to_cpu(aed->lengthAllocDescs) + adsize);
  492. } else {
  493. UDF_I_LENALLOC(table) += adsize;
  494. mark_inode_dirty(table);
  495. }
  496. }
  497. if (UDF_SB_UDFREV(sb) >= 0x0200)
  498. udf_new_tag(epos.bh->b_data, TAG_IDENT_AED, 3, 1,
  499. epos.block.logicalBlockNum, sizeof(tag));
  500. else
  501. udf_new_tag(epos.bh->b_data, TAG_IDENT_AED, 2, 1,
  502. epos.block.logicalBlockNum, sizeof(tag));
  503. switch (UDF_I_ALLOCTYPE(table)) {
  504. case ICBTAG_FLAG_AD_SHORT:
  505. sad = (short_ad *)sptr;
  506. sad->extLength = cpu_to_le32(
  507. EXT_NEXT_EXTENT_ALLOCDECS |
  508. sb->s_blocksize);
  509. sad->extPosition = cpu_to_le32(epos.block.logicalBlockNum);
  510. break;
  511. case ICBTAG_FLAG_AD_LONG:
  512. lad = (long_ad *)sptr;
  513. lad->extLength = cpu_to_le32(
  514. EXT_NEXT_EXTENT_ALLOCDECS |
  515. sb->s_blocksize);
  516. lad->extLocation = cpu_to_lelb(epos.block);
  517. break;
  518. }
  519. if (oepos.bh) {
  520. udf_update_tag(oepos.bh->b_data, loffset);
  521. mark_buffer_dirty(oepos.bh);
  522. } else {
  523. mark_inode_dirty(table);
  524. }
  525. }
  526. if (elen) { /* It's possible that stealing the block emptied the extent */
  527. udf_write_aext(table, &epos, eloc, elen, 1);
  528. if (!epos.bh) {
  529. UDF_I_LENALLOC(table) += adsize;
  530. mark_inode_dirty(table);
  531. } else {
  532. aed = (struct allocExtDesc *)epos.bh->b_data;
  533. aed->lengthAllocDescs =
  534. cpu_to_le32(le32_to_cpu(aed->lengthAllocDescs) + adsize);
  535. udf_update_tag(epos.bh->b_data, epos.offset);
  536. mark_buffer_dirty(epos.bh);
  537. }
  538. }
  539. }
  540. brelse(epos.bh);
  541. brelse(oepos.bh);
  542. error_return:
  543. sb->s_dirt = 1;
  544. mutex_unlock(&sbi->s_alloc_mutex);
  545. return;
  546. }
  547. static int udf_table_prealloc_blocks(struct super_block *sb,
  548. struct inode *inode,
  549. struct inode *table, uint16_t partition,
  550. uint32_t first_block, uint32_t block_count)
  551. {
  552. struct udf_sb_info *sbi = UDF_SB(sb);
  553. int alloc_count = 0;
  554. uint32_t elen, adsize;
  555. kernel_lb_addr eloc;
  556. struct extent_position epos;
  557. int8_t etype = -1;
  558. if (first_block < 0 || first_block >= UDF_SB_PARTLEN(sb, partition))
  559. return 0;
  560. if (UDF_I_ALLOCTYPE(table) == ICBTAG_FLAG_AD_SHORT)
  561. adsize = sizeof(short_ad);
  562. else if (UDF_I_ALLOCTYPE(table) == ICBTAG_FLAG_AD_LONG)
  563. adsize = sizeof(long_ad);
  564. else
  565. return 0;
  566. mutex_lock(&sbi->s_alloc_mutex);
  567. epos.offset = sizeof(struct unallocSpaceEntry);
  568. epos.block = UDF_I_LOCATION(table);
  569. epos.bh = NULL;
  570. eloc.logicalBlockNum = 0xFFFFFFFF;
  571. while (first_block != eloc.logicalBlockNum &&
  572. (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
  573. udf_debug("eloc=%d, elen=%d, first_block=%d\n",
  574. eloc.logicalBlockNum, elen, first_block);
  575. ; /* empty loop body */
  576. }
  577. if (first_block == eloc.logicalBlockNum) {
  578. epos.offset -= adsize;
  579. alloc_count = (elen >> sb->s_blocksize_bits);
  580. if (inode && DQUOT_PREALLOC_BLOCK(inode, alloc_count > block_count ? block_count : alloc_count)) {
  581. alloc_count = 0;
  582. } else if (alloc_count > block_count) {
  583. alloc_count = block_count;
  584. eloc.logicalBlockNum += alloc_count;
  585. elen -= (alloc_count << sb->s_blocksize_bits);
  586. udf_write_aext(table, &epos, eloc, (etype << 30) | elen, 1);
  587. } else {
  588. udf_delete_aext(table, epos, eloc, (etype << 30) | elen);
  589. }
  590. } else {
  591. alloc_count = 0;
  592. }
  593. brelse(epos.bh);
  594. if (alloc_count && UDF_SB_LVIDBH(sb)) {
  595. UDF_SB_LVID(sb)->freeSpaceTable[partition] =
  596. cpu_to_le32(le32_to_cpu(UDF_SB_LVID(sb)->freeSpaceTable[partition]) - alloc_count);
  597. mark_buffer_dirty(UDF_SB_LVIDBH(sb));
  598. sb->s_dirt = 1;
  599. }
  600. mutex_unlock(&sbi->s_alloc_mutex);
  601. return alloc_count;
  602. }
  603. static int udf_table_new_block(struct super_block *sb,
  604. struct inode *inode,
  605. struct inode *table, uint16_t partition,
  606. uint32_t goal, int *err)
  607. {
  608. struct udf_sb_info *sbi = UDF_SB(sb);
  609. uint32_t spread = 0xFFFFFFFF, nspread = 0xFFFFFFFF;
  610. uint32_t newblock = 0, adsize;
  611. uint32_t elen, goal_elen = 0;
  612. kernel_lb_addr eloc, goal_eloc;
  613. struct extent_position epos, goal_epos;
  614. int8_t etype;
  615. *err = -ENOSPC;
  616. if (UDF_I_ALLOCTYPE(table) == ICBTAG_FLAG_AD_SHORT)
  617. adsize = sizeof(short_ad);
  618. else if (UDF_I_ALLOCTYPE(table) == ICBTAG_FLAG_AD_LONG)
  619. adsize = sizeof(long_ad);
  620. else
  621. return newblock;
  622. mutex_lock(&sbi->s_alloc_mutex);
  623. if (goal < 0 || goal >= UDF_SB_PARTLEN(sb, partition))
  624. goal = 0;
  625. /* We search for the closest matching block to goal. If we find a exact hit,
  626. we stop. Otherwise we keep going till we run out of extents.
  627. We store the buffer_head, bloc, and extoffset of the current closest
  628. match and use that when we are done.
  629. */
  630. epos.offset = sizeof(struct unallocSpaceEntry);
  631. epos.block = UDF_I_LOCATION(table);
  632. epos.bh = goal_epos.bh = NULL;
  633. while (spread &&
  634. (etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
  635. if (goal >= eloc.logicalBlockNum) {
  636. if (goal < eloc.logicalBlockNum + (elen >> sb->s_blocksize_bits))
  637. nspread = 0;
  638. else
  639. nspread = goal - eloc.logicalBlockNum -
  640. (elen >> sb->s_blocksize_bits);
  641. } else {
  642. nspread = eloc.logicalBlockNum - goal;
  643. }
  644. if (nspread < spread) {
  645. spread = nspread;
  646. if (goal_epos.bh != epos.bh) {
  647. brelse(goal_epos.bh);
  648. goal_epos.bh = epos.bh;
  649. get_bh(goal_epos.bh);
  650. }
  651. goal_epos.block = epos.block;
  652. goal_epos.offset = epos.offset - adsize;
  653. goal_eloc = eloc;
  654. goal_elen = (etype << 30) | elen;
  655. }
  656. }
  657. brelse(epos.bh);
  658. if (spread == 0xFFFFFFFF) {
  659. brelse(goal_epos.bh);
  660. mutex_unlock(&sbi->s_alloc_mutex);
  661. return 0;
  662. }
  663. /* Only allocate blocks from the beginning of the extent.
  664. That way, we only delete (empty) extents, never have to insert an
  665. extent because of splitting */
  666. /* This works, but very poorly.... */
  667. newblock = goal_eloc.logicalBlockNum;
  668. goal_eloc.logicalBlockNum++;
  669. goal_elen -= sb->s_blocksize;
  670. if (inode && DQUOT_ALLOC_BLOCK(inode, 1)) {
  671. brelse(goal_epos.bh);
  672. mutex_unlock(&sbi->s_alloc_mutex);
  673. *err = -EDQUOT;
  674. return 0;
  675. }
  676. if (goal_elen)
  677. udf_write_aext(table, &goal_epos, goal_eloc, goal_elen, 1);
  678. else
  679. udf_delete_aext(table, goal_epos, goal_eloc, goal_elen);
  680. brelse(goal_epos.bh);
  681. if (UDF_SB_LVIDBH(sb)) {
  682. UDF_SB_LVID(sb)->freeSpaceTable[partition] =
  683. cpu_to_le32(le32_to_cpu(UDF_SB_LVID(sb)->freeSpaceTable[partition]) - 1);
  684. mark_buffer_dirty(UDF_SB_LVIDBH(sb));
  685. }
  686. sb->s_dirt = 1;
  687. mutex_unlock(&sbi->s_alloc_mutex);
  688. *err = 0;
  689. return newblock;
  690. }
  691. inline void udf_free_blocks(struct super_block *sb,
  692. struct inode *inode,
  693. kernel_lb_addr bloc, uint32_t offset,
  694. uint32_t count)
  695. {
  696. uint16_t partition = bloc.partitionReferenceNum;
  697. if (UDF_SB_PARTFLAGS(sb, partition) & UDF_PART_FLAG_UNALLOC_BITMAP) {
  698. return udf_bitmap_free_blocks(sb, inode,
  699. UDF_SB_PARTMAPS(sb)[partition].s_uspace.s_bitmap,
  700. bloc, offset, count);
  701. } else if (UDF_SB_PARTFLAGS(sb, partition) & UDF_PART_FLAG_UNALLOC_TABLE) {
  702. return udf_table_free_blocks(sb, inode,
  703. UDF_SB_PARTMAPS(sb)[partition].s_uspace.s_table,
  704. bloc, offset, count);
  705. } else if (UDF_SB_PARTFLAGS(sb, partition) & UDF_PART_FLAG_FREED_BITMAP) {
  706. return udf_bitmap_free_blocks(sb, inode,
  707. UDF_SB_PARTMAPS(sb)[partition].s_fspace.s_bitmap,
  708. bloc, offset, count);
  709. } else if (UDF_SB_PARTFLAGS(sb, partition) & UDF_PART_FLAG_FREED_TABLE) {
  710. return udf_table_free_blocks(sb, inode,
  711. UDF_SB_PARTMAPS(sb)[partition].s_fspace.s_table,
  712. bloc, offset, count);
  713. } else {
  714. return;
  715. }
  716. }
  717. inline int udf_prealloc_blocks(struct super_block *sb,
  718. struct inode *inode,
  719. uint16_t partition, uint32_t first_block,
  720. uint32_t block_count)
  721. {
  722. if (UDF_SB_PARTFLAGS(sb, partition) & UDF_PART_FLAG_UNALLOC_BITMAP) {
  723. return udf_bitmap_prealloc_blocks(sb, inode,
  724. UDF_SB_PARTMAPS(sb)[partition].s_uspace.s_bitmap,
  725. partition, first_block, block_count);
  726. } else if (UDF_SB_PARTFLAGS(sb, partition) & UDF_PART_FLAG_UNALLOC_TABLE) {
  727. return udf_table_prealloc_blocks(sb, inode,
  728. UDF_SB_PARTMAPS(sb)[partition].s_uspace.s_table,
  729. partition, first_block, block_count);
  730. } else if (UDF_SB_PARTFLAGS(sb, partition) & UDF_PART_FLAG_FREED_BITMAP) {
  731. return udf_bitmap_prealloc_blocks(sb, inode,
  732. UDF_SB_PARTMAPS(sb)[partition].s_fspace.s_bitmap,
  733. partition, first_block, block_count);
  734. } else if (UDF_SB_PARTFLAGS(sb, partition) & UDF_PART_FLAG_FREED_TABLE) {
  735. return udf_table_prealloc_blocks(sb, inode,
  736. UDF_SB_PARTMAPS(sb)[partition].s_fspace.s_table,
  737. partition, first_block, block_count);
  738. } else {
  739. return 0;
  740. }
  741. }
  742. inline int udf_new_block(struct super_block *sb,
  743. struct inode *inode,
  744. uint16_t partition, uint32_t goal, int *err)
  745. {
  746. int ret;
  747. if (UDF_SB_PARTFLAGS(sb, partition) & UDF_PART_FLAG_UNALLOC_BITMAP) {
  748. ret = udf_bitmap_new_block(sb, inode,
  749. UDF_SB_PARTMAPS(sb)[partition].s_uspace.s_bitmap,
  750. partition, goal, err);
  751. return ret;
  752. } else if (UDF_SB_PARTFLAGS(sb, partition) & UDF_PART_FLAG_UNALLOC_TABLE) {
  753. return udf_table_new_block(sb, inode,
  754. UDF_SB_PARTMAPS(sb)[partition].s_uspace.s_table,
  755. partition, goal, err);
  756. } else if (UDF_SB_PARTFLAGS(sb, partition) & UDF_PART_FLAG_FREED_BITMAP) {
  757. return udf_bitmap_new_block(sb, inode,
  758. UDF_SB_PARTMAPS(sb)[partition].s_fspace.s_bitmap,
  759. partition, goal, err);
  760. } else if (UDF_SB_PARTFLAGS(sb, partition) & UDF_PART_FLAG_FREED_TABLE) {
  761. return udf_table_new_block(sb, inode,
  762. UDF_SB_PARTMAPS(sb)[partition].s_fspace.s_table,
  763. partition, goal, err);
  764. } else {
  765. *err = -EIO;
  766. return 0;
  767. }
  768. }