balloc.c 25 KB

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