balloc.c 24 KB

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