balloc.c 24 KB

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