balloc.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939
  1. /*
  2. * linux/fs/ufs/balloc.c
  3. *
  4. * Copyright (C) 1998
  5. * Daniel Pirkl <daniel.pirkl@email.cz>
  6. * Charles University, Faculty of Mathematics and Physics
  7. *
  8. * UFS2 write support Evgeniy Dushistov <dushistov@mail.ru>, 2007
  9. */
  10. #include <linux/fs.h>
  11. #include <linux/stat.h>
  12. #include <linux/time.h>
  13. #include <linux/string.h>
  14. #include <linux/buffer_head.h>
  15. #include <linux/capability.h>
  16. #include <linux/bitops.h>
  17. #include <asm/byteorder.h>
  18. #include "ufs_fs.h"
  19. #include "ufs.h"
  20. #include "swab.h"
  21. #include "util.h"
  22. #define INVBLOCK ((u64)-1L)
  23. static u64 ufs_add_fragments(struct inode *, u64, unsigned, unsigned);
  24. static u64 ufs_alloc_fragments(struct inode *, unsigned, u64, unsigned, int *);
  25. static u64 ufs_alloccg_block(struct inode *, struct ufs_cg_private_info *, u64, int *);
  26. static u64 ufs_bitmap_search (struct super_block *, struct ufs_cg_private_info *, u64, unsigned);
  27. static unsigned char ufs_fragtable_8fpb[], ufs_fragtable_other[];
  28. static void ufs_clusteracct(struct super_block *, struct ufs_cg_private_info *, unsigned, int);
  29. /*
  30. * Free 'count' fragments from fragment number 'fragment'
  31. */
  32. void ufs_free_fragments(struct inode *inode, u64 fragment, unsigned count)
  33. {
  34. struct super_block * sb;
  35. struct ufs_sb_private_info * uspi;
  36. struct ufs_cg_private_info * ucpi;
  37. struct ufs_cylinder_group * ucg;
  38. unsigned cgno, bit, end_bit, bbase, blkmap, i;
  39. u64 blkno;
  40. sb = inode->i_sb;
  41. uspi = UFS_SB(sb)->s_uspi;
  42. UFSD("ENTER, fragment %llu, count %u\n",
  43. (unsigned long long)fragment, count);
  44. if (ufs_fragnum(fragment) + count > uspi->s_fpg)
  45. ufs_error (sb, "ufs_free_fragments", "internal error");
  46. mutex_lock(&UFS_SB(sb)->s_lock);
  47. cgno = ufs_dtog(uspi, fragment);
  48. bit = ufs_dtogd(uspi, fragment);
  49. if (cgno >= uspi->s_ncg) {
  50. ufs_panic (sb, "ufs_free_fragments", "freeing blocks are outside device");
  51. goto failed;
  52. }
  53. ucpi = ufs_load_cylinder (sb, cgno);
  54. if (!ucpi)
  55. goto failed;
  56. ucg = ubh_get_ucg (UCPI_UBH(ucpi));
  57. if (!ufs_cg_chkmagic(sb, ucg)) {
  58. ufs_panic (sb, "ufs_free_fragments", "internal error, bad magic number on cg %u", cgno);
  59. goto failed;
  60. }
  61. end_bit = bit + count;
  62. bbase = ufs_blknum (bit);
  63. blkmap = ubh_blkmap (UCPI_UBH(ucpi), ucpi->c_freeoff, bbase);
  64. ufs_fragacct (sb, blkmap, ucg->cg_frsum, -1);
  65. for (i = bit; i < end_bit; i++) {
  66. if (ubh_isclr (UCPI_UBH(ucpi), ucpi->c_freeoff, i))
  67. ubh_setbit (UCPI_UBH(ucpi), ucpi->c_freeoff, i);
  68. else
  69. ufs_error (sb, "ufs_free_fragments",
  70. "bit already cleared for fragment %u", i);
  71. }
  72. fs32_add(sb, &ucg->cg_cs.cs_nffree, count);
  73. uspi->cs_total.cs_nffree += count;
  74. fs32_add(sb, &UFS_SB(sb)->fs_cs(cgno).cs_nffree, count);
  75. blkmap = ubh_blkmap (UCPI_UBH(ucpi), ucpi->c_freeoff, bbase);
  76. ufs_fragacct(sb, blkmap, ucg->cg_frsum, 1);
  77. /*
  78. * Trying to reassemble free fragments into block
  79. */
  80. blkno = ufs_fragstoblks (bbase);
  81. if (ubh_isblockset(UCPI_UBH(ucpi), ucpi->c_freeoff, blkno)) {
  82. fs32_sub(sb, &ucg->cg_cs.cs_nffree, uspi->s_fpb);
  83. uspi->cs_total.cs_nffree -= uspi->s_fpb;
  84. fs32_sub(sb, &UFS_SB(sb)->fs_cs(cgno).cs_nffree, uspi->s_fpb);
  85. if ((UFS_SB(sb)->s_flags & UFS_CG_MASK) == UFS_CG_44BSD)
  86. ufs_clusteracct (sb, ucpi, blkno, 1);
  87. fs32_add(sb, &ucg->cg_cs.cs_nbfree, 1);
  88. uspi->cs_total.cs_nbfree++;
  89. fs32_add(sb, &UFS_SB(sb)->fs_cs(cgno).cs_nbfree, 1);
  90. if (uspi->fs_magic != UFS2_MAGIC) {
  91. unsigned cylno = ufs_cbtocylno (bbase);
  92. fs16_add(sb, &ubh_cg_blks(ucpi, cylno,
  93. ufs_cbtorpos(bbase)), 1);
  94. fs32_add(sb, &ubh_cg_blktot(ucpi, cylno), 1);
  95. }
  96. }
  97. ubh_mark_buffer_dirty (USPI_UBH(uspi));
  98. ubh_mark_buffer_dirty (UCPI_UBH(ucpi));
  99. if (sb->s_flags & MS_SYNCHRONOUS)
  100. ubh_sync_block(UCPI_UBH(ucpi));
  101. ufs_mark_sb_dirty(sb);
  102. mutex_unlock(&UFS_SB(sb)->s_lock);
  103. UFSD("EXIT\n");
  104. return;
  105. failed:
  106. mutex_unlock(&UFS_SB(sb)->s_lock);
  107. UFSD("EXIT (FAILED)\n");
  108. return;
  109. }
  110. /*
  111. * Free 'count' fragments from fragment number 'fragment' (free whole blocks)
  112. */
  113. void ufs_free_blocks(struct inode *inode, u64 fragment, unsigned count)
  114. {
  115. struct super_block * sb;
  116. struct ufs_sb_private_info * uspi;
  117. struct ufs_cg_private_info * ucpi;
  118. struct ufs_cylinder_group * ucg;
  119. unsigned overflow, cgno, bit, end_bit, i;
  120. u64 blkno;
  121. sb = inode->i_sb;
  122. uspi = UFS_SB(sb)->s_uspi;
  123. UFSD("ENTER, fragment %llu, count %u\n",
  124. (unsigned long long)fragment, count);
  125. if ((fragment & uspi->s_fpbmask) || (count & uspi->s_fpbmask)) {
  126. ufs_error (sb, "ufs_free_blocks", "internal error, "
  127. "fragment %llu, count %u\n",
  128. (unsigned long long)fragment, count);
  129. goto failed;
  130. }
  131. mutex_lock(&UFS_SB(sb)->s_lock);
  132. do_more:
  133. overflow = 0;
  134. cgno = ufs_dtog(uspi, fragment);
  135. bit = ufs_dtogd(uspi, fragment);
  136. if (cgno >= uspi->s_ncg) {
  137. ufs_panic (sb, "ufs_free_blocks", "freeing blocks are outside device");
  138. goto failed_unlock;
  139. }
  140. end_bit = bit + count;
  141. if (end_bit > uspi->s_fpg) {
  142. overflow = bit + count - uspi->s_fpg;
  143. count -= overflow;
  144. end_bit -= overflow;
  145. }
  146. ucpi = ufs_load_cylinder (sb, cgno);
  147. if (!ucpi)
  148. goto failed_unlock;
  149. ucg = ubh_get_ucg (UCPI_UBH(ucpi));
  150. if (!ufs_cg_chkmagic(sb, ucg)) {
  151. ufs_panic (sb, "ufs_free_blocks", "internal error, bad magic number on cg %u", cgno);
  152. goto failed_unlock;
  153. }
  154. for (i = bit; i < end_bit; i += uspi->s_fpb) {
  155. blkno = ufs_fragstoblks(i);
  156. if (ubh_isblockset(UCPI_UBH(ucpi), ucpi->c_freeoff, blkno)) {
  157. ufs_error(sb, "ufs_free_blocks", "freeing free fragment");
  158. }
  159. ubh_setblock(UCPI_UBH(ucpi), ucpi->c_freeoff, blkno);
  160. if ((UFS_SB(sb)->s_flags & UFS_CG_MASK) == UFS_CG_44BSD)
  161. ufs_clusteracct (sb, ucpi, blkno, 1);
  162. fs32_add(sb, &ucg->cg_cs.cs_nbfree, 1);
  163. uspi->cs_total.cs_nbfree++;
  164. fs32_add(sb, &UFS_SB(sb)->fs_cs(cgno).cs_nbfree, 1);
  165. if (uspi->fs_magic != UFS2_MAGIC) {
  166. unsigned cylno = ufs_cbtocylno(i);
  167. fs16_add(sb, &ubh_cg_blks(ucpi, cylno,
  168. ufs_cbtorpos(i)), 1);
  169. fs32_add(sb, &ubh_cg_blktot(ucpi, cylno), 1);
  170. }
  171. }
  172. ubh_mark_buffer_dirty (USPI_UBH(uspi));
  173. ubh_mark_buffer_dirty (UCPI_UBH(ucpi));
  174. if (sb->s_flags & MS_SYNCHRONOUS)
  175. ubh_sync_block(UCPI_UBH(ucpi));
  176. if (overflow) {
  177. fragment += count;
  178. count = overflow;
  179. goto do_more;
  180. }
  181. ufs_mark_sb_dirty(sb);
  182. mutex_unlock(&UFS_SB(sb)->s_lock);
  183. UFSD("EXIT\n");
  184. return;
  185. failed_unlock:
  186. mutex_unlock(&UFS_SB(sb)->s_lock);
  187. failed:
  188. UFSD("EXIT (FAILED)\n");
  189. return;
  190. }
  191. /*
  192. * Modify inode page cache in such way:
  193. * have - blocks with b_blocknr equal to oldb...oldb+count-1
  194. * get - blocks with b_blocknr equal to newb...newb+count-1
  195. * also we suppose that oldb...oldb+count-1 blocks
  196. * situated at the end of file.
  197. *
  198. * We can come here from ufs_writepage or ufs_prepare_write,
  199. * locked_page is argument of these functions, so we already lock it.
  200. */
  201. static void ufs_change_blocknr(struct inode *inode, sector_t beg,
  202. unsigned int count, sector_t oldb,
  203. sector_t newb, struct page *locked_page)
  204. {
  205. const unsigned blks_per_page =
  206. 1 << (PAGE_CACHE_SHIFT - inode->i_blkbits);
  207. const unsigned mask = blks_per_page - 1;
  208. struct address_space * const mapping = inode->i_mapping;
  209. pgoff_t index, cur_index, last_index;
  210. unsigned pos, j, lblock;
  211. sector_t end, i;
  212. struct page *page;
  213. struct buffer_head *head, *bh;
  214. UFSD("ENTER, ino %lu, count %u, oldb %llu, newb %llu\n",
  215. inode->i_ino, count,
  216. (unsigned long long)oldb, (unsigned long long)newb);
  217. BUG_ON(!locked_page);
  218. BUG_ON(!PageLocked(locked_page));
  219. cur_index = locked_page->index;
  220. end = count + beg;
  221. last_index = end >> (PAGE_CACHE_SHIFT - inode->i_blkbits);
  222. for (i = beg; i < end; i = (i | mask) + 1) {
  223. index = i >> (PAGE_CACHE_SHIFT - inode->i_blkbits);
  224. if (likely(cur_index != index)) {
  225. page = ufs_get_locked_page(mapping, index);
  226. if (!page)/* it was truncated */
  227. continue;
  228. if (IS_ERR(page)) {/* or EIO */
  229. ufs_error(inode->i_sb, __func__,
  230. "read of page %llu failed\n",
  231. (unsigned long long)index);
  232. continue;
  233. }
  234. } else
  235. page = locked_page;
  236. head = page_buffers(page);
  237. bh = head;
  238. pos = i & mask;
  239. for (j = 0; j < pos; ++j)
  240. bh = bh->b_this_page;
  241. if (unlikely(index == last_index))
  242. lblock = end & mask;
  243. else
  244. lblock = blks_per_page;
  245. do {
  246. if (j >= lblock)
  247. break;
  248. pos = (i - beg) + j;
  249. if (!buffer_mapped(bh))
  250. map_bh(bh, inode->i_sb, oldb + pos);
  251. if (!buffer_uptodate(bh)) {
  252. ll_rw_block(READ, 1, &bh);
  253. wait_on_buffer(bh);
  254. if (!buffer_uptodate(bh)) {
  255. ufs_error(inode->i_sb, __func__,
  256. "read of block failed\n");
  257. break;
  258. }
  259. }
  260. UFSD(" change from %llu to %llu, pos %u\n",
  261. (unsigned long long)(pos + oldb),
  262. (unsigned long long)(pos + newb), pos);
  263. bh->b_blocknr = newb + pos;
  264. unmap_underlying_metadata(bh->b_bdev,
  265. bh->b_blocknr);
  266. mark_buffer_dirty(bh);
  267. ++j;
  268. bh = bh->b_this_page;
  269. } while (bh != head);
  270. if (likely(cur_index != index))
  271. ufs_put_locked_page(page);
  272. }
  273. UFSD("EXIT\n");
  274. }
  275. static void ufs_clear_frags(struct inode *inode, sector_t beg, unsigned int n,
  276. int sync)
  277. {
  278. struct buffer_head *bh;
  279. sector_t end = beg + n;
  280. for (; beg < end; ++beg) {
  281. bh = sb_getblk(inode->i_sb, beg);
  282. lock_buffer(bh);
  283. memset(bh->b_data, 0, inode->i_sb->s_blocksize);
  284. set_buffer_uptodate(bh);
  285. mark_buffer_dirty(bh);
  286. unlock_buffer(bh);
  287. if (IS_SYNC(inode) || sync)
  288. sync_dirty_buffer(bh);
  289. brelse(bh);
  290. }
  291. }
  292. u64 ufs_new_fragments(struct inode *inode, void *p, u64 fragment,
  293. u64 goal, unsigned count, int *err,
  294. struct page *locked_page)
  295. {
  296. struct super_block * sb;
  297. struct ufs_sb_private_info * uspi;
  298. struct ufs_super_block_first * usb1;
  299. unsigned cgno, oldcount, newcount;
  300. u64 tmp, request, result;
  301. UFSD("ENTER, ino %lu, fragment %llu, goal %llu, count %u\n",
  302. inode->i_ino, (unsigned long long)fragment,
  303. (unsigned long long)goal, count);
  304. sb = inode->i_sb;
  305. uspi = UFS_SB(sb)->s_uspi;
  306. usb1 = ubh_get_usb_first(uspi);
  307. *err = -ENOSPC;
  308. mutex_lock(&UFS_SB(sb)->s_lock);
  309. tmp = ufs_data_ptr_to_cpu(sb, p);
  310. if (count + ufs_fragnum(fragment) > uspi->s_fpb) {
  311. ufs_warning(sb, "ufs_new_fragments", "internal warning"
  312. " fragment %llu, count %u",
  313. (unsigned long long)fragment, count);
  314. count = uspi->s_fpb - ufs_fragnum(fragment);
  315. }
  316. oldcount = ufs_fragnum (fragment);
  317. newcount = oldcount + count;
  318. /*
  319. * Somebody else has just allocated our fragments
  320. */
  321. if (oldcount) {
  322. if (!tmp) {
  323. ufs_error(sb, "ufs_new_fragments", "internal error, "
  324. "fragment %llu, tmp %llu\n",
  325. (unsigned long long)fragment,
  326. (unsigned long long)tmp);
  327. mutex_unlock(&UFS_SB(sb)->s_lock);
  328. return INVBLOCK;
  329. }
  330. if (fragment < UFS_I(inode)->i_lastfrag) {
  331. UFSD("EXIT (ALREADY ALLOCATED)\n");
  332. mutex_unlock(&UFS_SB(sb)->s_lock);
  333. return 0;
  334. }
  335. }
  336. else {
  337. if (tmp) {
  338. UFSD("EXIT (ALREADY ALLOCATED)\n");
  339. mutex_unlock(&UFS_SB(sb)->s_lock);
  340. return 0;
  341. }
  342. }
  343. /*
  344. * There is not enough space for user on the device
  345. */
  346. if (!capable(CAP_SYS_RESOURCE) && ufs_freespace(uspi, UFS_MINFREE) <= 0) {
  347. mutex_unlock(&UFS_SB(sb)->s_lock);
  348. UFSD("EXIT (FAILED)\n");
  349. return 0;
  350. }
  351. if (goal >= uspi->s_size)
  352. goal = 0;
  353. if (goal == 0)
  354. cgno = ufs_inotocg (inode->i_ino);
  355. else
  356. cgno = ufs_dtog(uspi, goal);
  357. /*
  358. * allocate new fragment
  359. */
  360. if (oldcount == 0) {
  361. result = ufs_alloc_fragments (inode, cgno, goal, count, err);
  362. if (result) {
  363. ufs_cpu_to_data_ptr(sb, p, result);
  364. *err = 0;
  365. UFS_I(inode)->i_lastfrag =
  366. max(UFS_I(inode)->i_lastfrag, fragment + count);
  367. ufs_clear_frags(inode, result + oldcount,
  368. newcount - oldcount, locked_page != NULL);
  369. }
  370. mutex_unlock(&UFS_SB(sb)->s_lock);
  371. UFSD("EXIT, result %llu\n", (unsigned long long)result);
  372. return result;
  373. }
  374. /*
  375. * resize block
  376. */
  377. result = ufs_add_fragments(inode, tmp, oldcount, newcount);
  378. if (result) {
  379. *err = 0;
  380. UFS_I(inode)->i_lastfrag = max(UFS_I(inode)->i_lastfrag,
  381. fragment + count);
  382. ufs_clear_frags(inode, result + oldcount, newcount - oldcount,
  383. locked_page != NULL);
  384. mutex_unlock(&UFS_SB(sb)->s_lock);
  385. UFSD("EXIT, result %llu\n", (unsigned long long)result);
  386. return result;
  387. }
  388. /*
  389. * allocate new block and move data
  390. */
  391. switch (fs32_to_cpu(sb, usb1->fs_optim)) {
  392. case UFS_OPTSPACE:
  393. request = newcount;
  394. if (uspi->s_minfree < 5 || uspi->cs_total.cs_nffree
  395. > uspi->s_dsize * uspi->s_minfree / (2 * 100))
  396. break;
  397. usb1->fs_optim = cpu_to_fs32(sb, UFS_OPTTIME);
  398. break;
  399. default:
  400. usb1->fs_optim = cpu_to_fs32(sb, UFS_OPTTIME);
  401. case UFS_OPTTIME:
  402. request = uspi->s_fpb;
  403. if (uspi->cs_total.cs_nffree < uspi->s_dsize *
  404. (uspi->s_minfree - 2) / 100)
  405. break;
  406. usb1->fs_optim = cpu_to_fs32(sb, UFS_OPTTIME);
  407. break;
  408. }
  409. result = ufs_alloc_fragments (inode, cgno, goal, request, err);
  410. if (result) {
  411. ufs_clear_frags(inode, result + oldcount, newcount - oldcount,
  412. locked_page != NULL);
  413. ufs_change_blocknr(inode, fragment - oldcount, oldcount,
  414. uspi->s_sbbase + tmp,
  415. uspi->s_sbbase + result, locked_page);
  416. ufs_cpu_to_data_ptr(sb, p, result);
  417. *err = 0;
  418. UFS_I(inode)->i_lastfrag = max(UFS_I(inode)->i_lastfrag,
  419. fragment + count);
  420. mutex_unlock(&UFS_SB(sb)->s_lock);
  421. if (newcount < request)
  422. ufs_free_fragments (inode, result + newcount, request - newcount);
  423. ufs_free_fragments (inode, tmp, oldcount);
  424. UFSD("EXIT, result %llu\n", (unsigned long long)result);
  425. return result;
  426. }
  427. mutex_unlock(&UFS_SB(sb)->s_lock);
  428. UFSD("EXIT (FAILED)\n");
  429. return 0;
  430. }
  431. static u64 ufs_add_fragments(struct inode *inode, u64 fragment,
  432. unsigned oldcount, unsigned newcount)
  433. {
  434. struct super_block * sb;
  435. struct ufs_sb_private_info * uspi;
  436. struct ufs_cg_private_info * ucpi;
  437. struct ufs_cylinder_group * ucg;
  438. unsigned cgno, fragno, fragoff, count, fragsize, i;
  439. UFSD("ENTER, fragment %llu, oldcount %u, newcount %u\n",
  440. (unsigned long long)fragment, oldcount, newcount);
  441. sb = inode->i_sb;
  442. uspi = UFS_SB(sb)->s_uspi;
  443. count = newcount - oldcount;
  444. cgno = ufs_dtog(uspi, fragment);
  445. if (fs32_to_cpu(sb, UFS_SB(sb)->fs_cs(cgno).cs_nffree) < count)
  446. return 0;
  447. if ((ufs_fragnum (fragment) + newcount) > uspi->s_fpb)
  448. return 0;
  449. ucpi = ufs_load_cylinder (sb, cgno);
  450. if (!ucpi)
  451. return 0;
  452. ucg = ubh_get_ucg (UCPI_UBH(ucpi));
  453. if (!ufs_cg_chkmagic(sb, ucg)) {
  454. ufs_panic (sb, "ufs_add_fragments",
  455. "internal error, bad magic number on cg %u", cgno);
  456. return 0;
  457. }
  458. fragno = ufs_dtogd(uspi, fragment);
  459. fragoff = ufs_fragnum (fragno);
  460. for (i = oldcount; i < newcount; i++)
  461. if (ubh_isclr (UCPI_UBH(ucpi), ucpi->c_freeoff, fragno + i))
  462. return 0;
  463. /*
  464. * Block can be extended
  465. */
  466. ucg->cg_time = cpu_to_fs32(sb, get_seconds());
  467. for (i = newcount; i < (uspi->s_fpb - fragoff); i++)
  468. if (ubh_isclr (UCPI_UBH(ucpi), ucpi->c_freeoff, fragno + i))
  469. break;
  470. fragsize = i - oldcount;
  471. if (!fs32_to_cpu(sb, ucg->cg_frsum[fragsize]))
  472. ufs_panic (sb, "ufs_add_fragments",
  473. "internal error or corrupted bitmap on cg %u", cgno);
  474. fs32_sub(sb, &ucg->cg_frsum[fragsize], 1);
  475. if (fragsize != count)
  476. fs32_add(sb, &ucg->cg_frsum[fragsize - count], 1);
  477. for (i = oldcount; i < newcount; i++)
  478. ubh_clrbit (UCPI_UBH(ucpi), ucpi->c_freeoff, fragno + i);
  479. fs32_sub(sb, &ucg->cg_cs.cs_nffree, count);
  480. fs32_sub(sb, &UFS_SB(sb)->fs_cs(cgno).cs_nffree, count);
  481. uspi->cs_total.cs_nffree -= count;
  482. ubh_mark_buffer_dirty (USPI_UBH(uspi));
  483. ubh_mark_buffer_dirty (UCPI_UBH(ucpi));
  484. if (sb->s_flags & MS_SYNCHRONOUS)
  485. ubh_sync_block(UCPI_UBH(ucpi));
  486. ufs_mark_sb_dirty(sb);
  487. UFSD("EXIT, fragment %llu\n", (unsigned long long)fragment);
  488. return fragment;
  489. }
  490. #define UFS_TEST_FREE_SPACE_CG \
  491. ucg = (struct ufs_cylinder_group *) UFS_SB(sb)->s_ucg[cgno]->b_data; \
  492. if (fs32_to_cpu(sb, ucg->cg_cs.cs_nbfree)) \
  493. goto cg_found; \
  494. for (k = count; k < uspi->s_fpb; k++) \
  495. if (fs32_to_cpu(sb, ucg->cg_frsum[k])) \
  496. goto cg_found;
  497. static u64 ufs_alloc_fragments(struct inode *inode, unsigned cgno,
  498. u64 goal, unsigned count, int *err)
  499. {
  500. struct super_block * sb;
  501. struct ufs_sb_private_info * uspi;
  502. struct ufs_cg_private_info * ucpi;
  503. struct ufs_cylinder_group * ucg;
  504. unsigned oldcg, i, j, k, allocsize;
  505. u64 result;
  506. UFSD("ENTER, ino %lu, cgno %u, goal %llu, count %u\n",
  507. inode->i_ino, cgno, (unsigned long long)goal, count);
  508. sb = inode->i_sb;
  509. uspi = UFS_SB(sb)->s_uspi;
  510. oldcg = cgno;
  511. /*
  512. * 1. searching on preferred cylinder group
  513. */
  514. UFS_TEST_FREE_SPACE_CG
  515. /*
  516. * 2. quadratic rehash
  517. */
  518. for (j = 1; j < uspi->s_ncg; j *= 2) {
  519. cgno += j;
  520. if (cgno >= uspi->s_ncg)
  521. cgno -= uspi->s_ncg;
  522. UFS_TEST_FREE_SPACE_CG
  523. }
  524. /*
  525. * 3. brute force search
  526. * We start at i = 2 ( 0 is checked at 1.step, 1 at 2.step )
  527. */
  528. cgno = (oldcg + 1) % uspi->s_ncg;
  529. for (j = 2; j < uspi->s_ncg; j++) {
  530. cgno++;
  531. if (cgno >= uspi->s_ncg)
  532. cgno = 0;
  533. UFS_TEST_FREE_SPACE_CG
  534. }
  535. UFSD("EXIT (FAILED)\n");
  536. return 0;
  537. cg_found:
  538. ucpi = ufs_load_cylinder (sb, cgno);
  539. if (!ucpi)
  540. return 0;
  541. ucg = ubh_get_ucg (UCPI_UBH(ucpi));
  542. if (!ufs_cg_chkmagic(sb, ucg))
  543. ufs_panic (sb, "ufs_alloc_fragments",
  544. "internal error, bad magic number on cg %u", cgno);
  545. ucg->cg_time = cpu_to_fs32(sb, get_seconds());
  546. if (count == uspi->s_fpb) {
  547. result = ufs_alloccg_block (inode, ucpi, goal, err);
  548. if (result == INVBLOCK)
  549. return 0;
  550. goto succed;
  551. }
  552. for (allocsize = count; allocsize < uspi->s_fpb; allocsize++)
  553. if (fs32_to_cpu(sb, ucg->cg_frsum[allocsize]) != 0)
  554. break;
  555. if (allocsize == uspi->s_fpb) {
  556. result = ufs_alloccg_block (inode, ucpi, goal, err);
  557. if (result == INVBLOCK)
  558. return 0;
  559. goal = ufs_dtogd(uspi, result);
  560. for (i = count; i < uspi->s_fpb; i++)
  561. ubh_setbit (UCPI_UBH(ucpi), ucpi->c_freeoff, goal + i);
  562. i = uspi->s_fpb - count;
  563. fs32_add(sb, &ucg->cg_cs.cs_nffree, i);
  564. uspi->cs_total.cs_nffree += i;
  565. fs32_add(sb, &UFS_SB(sb)->fs_cs(cgno).cs_nffree, i);
  566. fs32_add(sb, &ucg->cg_frsum[i], 1);
  567. goto succed;
  568. }
  569. result = ufs_bitmap_search (sb, ucpi, goal, allocsize);
  570. if (result == INVBLOCK)
  571. return 0;
  572. for (i = 0; i < count; i++)
  573. ubh_clrbit (UCPI_UBH(ucpi), ucpi->c_freeoff, result + i);
  574. fs32_sub(sb, &ucg->cg_cs.cs_nffree, count);
  575. uspi->cs_total.cs_nffree -= count;
  576. fs32_sub(sb, &UFS_SB(sb)->fs_cs(cgno).cs_nffree, count);
  577. fs32_sub(sb, &ucg->cg_frsum[allocsize], 1);
  578. if (count != allocsize)
  579. fs32_add(sb, &ucg->cg_frsum[allocsize - count], 1);
  580. succed:
  581. ubh_mark_buffer_dirty (USPI_UBH(uspi));
  582. ubh_mark_buffer_dirty (UCPI_UBH(ucpi));
  583. if (sb->s_flags & MS_SYNCHRONOUS)
  584. ubh_sync_block(UCPI_UBH(ucpi));
  585. ufs_mark_sb_dirty(sb);
  586. result += cgno * uspi->s_fpg;
  587. UFSD("EXIT3, result %llu\n", (unsigned long long)result);
  588. return result;
  589. }
  590. static u64 ufs_alloccg_block(struct inode *inode,
  591. struct ufs_cg_private_info *ucpi,
  592. u64 goal, int *err)
  593. {
  594. struct super_block * sb;
  595. struct ufs_sb_private_info * uspi;
  596. struct ufs_cylinder_group * ucg;
  597. u64 result, blkno;
  598. UFSD("ENTER, goal %llu\n", (unsigned long long)goal);
  599. sb = inode->i_sb;
  600. uspi = UFS_SB(sb)->s_uspi;
  601. ucg = ubh_get_ucg(UCPI_UBH(ucpi));
  602. if (goal == 0) {
  603. goal = ucpi->c_rotor;
  604. goto norot;
  605. }
  606. goal = ufs_blknum (goal);
  607. goal = ufs_dtogd(uspi, goal);
  608. /*
  609. * If the requested block is available, use it.
  610. */
  611. if (ubh_isblockset(UCPI_UBH(ucpi), ucpi->c_freeoff, ufs_fragstoblks(goal))) {
  612. result = goal;
  613. goto gotit;
  614. }
  615. norot:
  616. result = ufs_bitmap_search (sb, ucpi, goal, uspi->s_fpb);
  617. if (result == INVBLOCK)
  618. return INVBLOCK;
  619. ucpi->c_rotor = result;
  620. gotit:
  621. blkno = ufs_fragstoblks(result);
  622. ubh_clrblock (UCPI_UBH(ucpi), ucpi->c_freeoff, blkno);
  623. if ((UFS_SB(sb)->s_flags & UFS_CG_MASK) == UFS_CG_44BSD)
  624. ufs_clusteracct (sb, ucpi, blkno, -1);
  625. fs32_sub(sb, &ucg->cg_cs.cs_nbfree, 1);
  626. uspi->cs_total.cs_nbfree--;
  627. fs32_sub(sb, &UFS_SB(sb)->fs_cs(ucpi->c_cgx).cs_nbfree, 1);
  628. if (uspi->fs_magic != UFS2_MAGIC) {
  629. unsigned cylno = ufs_cbtocylno((unsigned)result);
  630. fs16_sub(sb, &ubh_cg_blks(ucpi, cylno,
  631. ufs_cbtorpos((unsigned)result)), 1);
  632. fs32_sub(sb, &ubh_cg_blktot(ucpi, cylno), 1);
  633. }
  634. UFSD("EXIT, result %llu\n", (unsigned long long)result);
  635. return result;
  636. }
  637. static unsigned ubh_scanc(struct ufs_sb_private_info *uspi,
  638. struct ufs_buffer_head *ubh,
  639. unsigned begin, unsigned size,
  640. unsigned char *table, unsigned char mask)
  641. {
  642. unsigned rest, offset;
  643. unsigned char *cp;
  644. offset = begin & ~uspi->s_fmask;
  645. begin >>= uspi->s_fshift;
  646. for (;;) {
  647. if ((offset + size) < uspi->s_fsize)
  648. rest = size;
  649. else
  650. rest = uspi->s_fsize - offset;
  651. size -= rest;
  652. cp = ubh->bh[begin]->b_data + offset;
  653. while ((table[*cp++] & mask) == 0 && --rest)
  654. ;
  655. if (rest || !size)
  656. break;
  657. begin++;
  658. offset = 0;
  659. }
  660. return (size + rest);
  661. }
  662. /*
  663. * Find a block of the specified size in the specified cylinder group.
  664. * @sp: pointer to super block
  665. * @ucpi: pointer to cylinder group info
  666. * @goal: near which block we want find new one
  667. * @count: specified size
  668. */
  669. static u64 ufs_bitmap_search(struct super_block *sb,
  670. struct ufs_cg_private_info *ucpi,
  671. u64 goal, unsigned count)
  672. {
  673. /*
  674. * Bit patterns for identifying fragments in the block map
  675. * used as ((map & mask_arr) == want_arr)
  676. */
  677. static const int mask_arr[9] = {
  678. 0x3, 0x7, 0xf, 0x1f, 0x3f, 0x7f, 0xff, 0x1ff, 0x3ff
  679. };
  680. static const int want_arr[9] = {
  681. 0x0, 0x2, 0x6, 0xe, 0x1e, 0x3e, 0x7e, 0xfe, 0x1fe
  682. };
  683. struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
  684. unsigned start, length, loc;
  685. unsigned pos, want, blockmap, mask, end;
  686. u64 result;
  687. UFSD("ENTER, cg %u, goal %llu, count %u\n", ucpi->c_cgx,
  688. (unsigned long long)goal, count);
  689. if (goal)
  690. start = ufs_dtogd(uspi, goal) >> 3;
  691. else
  692. start = ucpi->c_frotor >> 3;
  693. length = ((uspi->s_fpg + 7) >> 3) - start;
  694. loc = ubh_scanc(uspi, UCPI_UBH(ucpi), ucpi->c_freeoff + start, length,
  695. (uspi->s_fpb == 8) ? ufs_fragtable_8fpb : ufs_fragtable_other,
  696. 1 << (count - 1 + (uspi->s_fpb & 7)));
  697. if (loc == 0) {
  698. length = start + 1;
  699. loc = ubh_scanc(uspi, UCPI_UBH(ucpi), ucpi->c_freeoff, length,
  700. (uspi->s_fpb == 8) ? ufs_fragtable_8fpb :
  701. ufs_fragtable_other,
  702. 1 << (count - 1 + (uspi->s_fpb & 7)));
  703. if (loc == 0) {
  704. ufs_error(sb, "ufs_bitmap_search",
  705. "bitmap corrupted on cg %u, start %u,"
  706. " length %u, count %u, freeoff %u\n",
  707. ucpi->c_cgx, start, length, count,
  708. ucpi->c_freeoff);
  709. return INVBLOCK;
  710. }
  711. start = 0;
  712. }
  713. result = (start + length - loc) << 3;
  714. ucpi->c_frotor = result;
  715. /*
  716. * found the byte in the map
  717. */
  718. for (end = result + 8; result < end; result += uspi->s_fpb) {
  719. blockmap = ubh_blkmap(UCPI_UBH(ucpi), ucpi->c_freeoff, result);
  720. blockmap <<= 1;
  721. mask = mask_arr[count];
  722. want = want_arr[count];
  723. for (pos = 0; pos <= uspi->s_fpb - count; pos++) {
  724. if ((blockmap & mask) == want) {
  725. UFSD("EXIT, result %llu\n",
  726. (unsigned long long)result);
  727. return result + pos;
  728. }
  729. mask <<= 1;
  730. want <<= 1;
  731. }
  732. }
  733. ufs_error(sb, "ufs_bitmap_search", "block not in map on cg %u\n",
  734. ucpi->c_cgx);
  735. UFSD("EXIT (FAILED)\n");
  736. return INVBLOCK;
  737. }
  738. static void ufs_clusteracct(struct super_block * sb,
  739. struct ufs_cg_private_info * ucpi, unsigned blkno, int cnt)
  740. {
  741. struct ufs_sb_private_info * uspi;
  742. int i, start, end, forw, back;
  743. uspi = UFS_SB(sb)->s_uspi;
  744. if (uspi->s_contigsumsize <= 0)
  745. return;
  746. if (cnt > 0)
  747. ubh_setbit(UCPI_UBH(ucpi), ucpi->c_clusteroff, blkno);
  748. else
  749. ubh_clrbit(UCPI_UBH(ucpi), ucpi->c_clusteroff, blkno);
  750. /*
  751. * Find the size of the cluster going forward.
  752. */
  753. start = blkno + 1;
  754. end = start + uspi->s_contigsumsize;
  755. if ( end >= ucpi->c_nclusterblks)
  756. end = ucpi->c_nclusterblks;
  757. i = ubh_find_next_zero_bit (UCPI_UBH(ucpi), ucpi->c_clusteroff, end, start);
  758. if (i > end)
  759. i = end;
  760. forw = i - start;
  761. /*
  762. * Find the size of the cluster going backward.
  763. */
  764. start = blkno - 1;
  765. end = start - uspi->s_contigsumsize;
  766. if (end < 0 )
  767. end = -1;
  768. i = ubh_find_last_zero_bit (UCPI_UBH(ucpi), ucpi->c_clusteroff, start, end);
  769. if ( i < end)
  770. i = end;
  771. back = start - i;
  772. /*
  773. * Account for old cluster and the possibly new forward and
  774. * back clusters.
  775. */
  776. i = back + forw + 1;
  777. if (i > uspi->s_contigsumsize)
  778. i = uspi->s_contigsumsize;
  779. fs32_add(sb, (__fs32*)ubh_get_addr(UCPI_UBH(ucpi), ucpi->c_clustersumoff + (i << 2)), cnt);
  780. if (back > 0)
  781. fs32_sub(sb, (__fs32*)ubh_get_addr(UCPI_UBH(ucpi), ucpi->c_clustersumoff + (back << 2)), cnt);
  782. if (forw > 0)
  783. fs32_sub(sb, (__fs32*)ubh_get_addr(UCPI_UBH(ucpi), ucpi->c_clustersumoff + (forw << 2)), cnt);
  784. }
  785. static unsigned char ufs_fragtable_8fpb[] = {
  786. 0x00, 0x01, 0x01, 0x02, 0x01, 0x01, 0x02, 0x04, 0x01, 0x01, 0x01, 0x03, 0x02, 0x03, 0x04, 0x08,
  787. 0x01, 0x01, 0x01, 0x03, 0x01, 0x01, 0x03, 0x05, 0x02, 0x03, 0x03, 0x02, 0x04, 0x05, 0x08, 0x10,
  788. 0x01, 0x01, 0x01, 0x03, 0x01, 0x01, 0x03, 0x05, 0x01, 0x01, 0x01, 0x03, 0x03, 0x03, 0x05, 0x09,
  789. 0x02, 0x03, 0x03, 0x02, 0x03, 0x03, 0x02, 0x06, 0x04, 0x05, 0x05, 0x06, 0x08, 0x09, 0x10, 0x20,
  790. 0x01, 0x01, 0x01, 0x03, 0x01, 0x01, 0x03, 0x05, 0x01, 0x01, 0x01, 0x03, 0x03, 0x03, 0x05, 0x09,
  791. 0x01, 0x01, 0x01, 0x03, 0x01, 0x01, 0x03, 0x05, 0x03, 0x03, 0x03, 0x03, 0x05, 0x05, 0x09, 0x11,
  792. 0x02, 0x03, 0x03, 0x02, 0x03, 0x03, 0x02, 0x06, 0x03, 0x03, 0x03, 0x03, 0x02, 0x03, 0x06, 0x0A,
  793. 0x04, 0x05, 0x05, 0x06, 0x05, 0x05, 0x06, 0x04, 0x08, 0x09, 0x09, 0x0A, 0x10, 0x11, 0x20, 0x40,
  794. 0x01, 0x01, 0x01, 0x03, 0x01, 0x01, 0x03, 0x05, 0x01, 0x01, 0x01, 0x03, 0x03, 0x03, 0x05, 0x09,
  795. 0x01, 0x01, 0x01, 0x03, 0x01, 0x01, 0x03, 0x05, 0x03, 0x03, 0x03, 0x03, 0x05, 0x05, 0x09, 0x11,
  796. 0x01, 0x01, 0x01, 0x03, 0x01, 0x01, 0x03, 0x05, 0x01, 0x01, 0x01, 0x03, 0x03, 0x03, 0x05, 0x09,
  797. 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x07, 0x05, 0x05, 0x05, 0x07, 0x09, 0x09, 0x11, 0x21,
  798. 0x02, 0x03, 0x03, 0x02, 0x03, 0x03, 0x02, 0x06, 0x03, 0x03, 0x03, 0x03, 0x02, 0x03, 0x06, 0x0A,
  799. 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x07, 0x02, 0x03, 0x03, 0x02, 0x06, 0x07, 0x0A, 0x12,
  800. 0x04, 0x05, 0x05, 0x06, 0x05, 0x05, 0x06, 0x04, 0x05, 0x05, 0x05, 0x07, 0x06, 0x07, 0x04, 0x0C,
  801. 0x08, 0x09, 0x09, 0x0A, 0x09, 0x09, 0x0A, 0x0C, 0x10, 0x11, 0x11, 0x12, 0x20, 0x21, 0x40, 0x80,
  802. };
  803. static unsigned char ufs_fragtable_other[] = {
  804. 0x00, 0x16, 0x16, 0x2A, 0x16, 0x16, 0x26, 0x4E, 0x16, 0x16, 0x16, 0x3E, 0x2A, 0x3E, 0x4E, 0x8A,
  805. 0x16, 0x16, 0x16, 0x3E, 0x16, 0x16, 0x36, 0x5E, 0x16, 0x16, 0x16, 0x3E, 0x3E, 0x3E, 0x5E, 0x9E,
  806. 0x16, 0x16, 0x16, 0x3E, 0x16, 0x16, 0x36, 0x5E, 0x16, 0x16, 0x16, 0x3E, 0x3E, 0x3E, 0x5E, 0x9E,
  807. 0x2A, 0x3E, 0x3E, 0x2A, 0x3E, 0x3E, 0x2E, 0x6E, 0x3E, 0x3E, 0x3E, 0x3E, 0x2A, 0x3E, 0x6E, 0xAA,
  808. 0x16, 0x16, 0x16, 0x3E, 0x16, 0x16, 0x36, 0x5E, 0x16, 0x16, 0x16, 0x3E, 0x3E, 0x3E, 0x5E, 0x9E,
  809. 0x16, 0x16, 0x16, 0x3E, 0x16, 0x16, 0x36, 0x5E, 0x16, 0x16, 0x16, 0x3E, 0x3E, 0x3E, 0x5E, 0x9E,
  810. 0x26, 0x36, 0x36, 0x2E, 0x36, 0x36, 0x26, 0x6E, 0x36, 0x36, 0x36, 0x3E, 0x2E, 0x3E, 0x6E, 0xAE,
  811. 0x4E, 0x5E, 0x5E, 0x6E, 0x5E, 0x5E, 0x6E, 0x4E, 0x5E, 0x5E, 0x5E, 0x7E, 0x6E, 0x7E, 0x4E, 0xCE,
  812. 0x16, 0x16, 0x16, 0x3E, 0x16, 0x16, 0x36, 0x5E, 0x16, 0x16, 0x16, 0x3E, 0x3E, 0x3E, 0x5E, 0x9E,
  813. 0x16, 0x16, 0x16, 0x3E, 0x16, 0x16, 0x36, 0x5E, 0x16, 0x16, 0x16, 0x3E, 0x3E, 0x3E, 0x5E, 0x9E,
  814. 0x16, 0x16, 0x16, 0x3E, 0x16, 0x16, 0x36, 0x5E, 0x16, 0x16, 0x16, 0x3E, 0x3E, 0x3E, 0x5E, 0x9E,
  815. 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x7E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x7E, 0xBE,
  816. 0x2A, 0x3E, 0x3E, 0x2A, 0x3E, 0x3E, 0x2E, 0x6E, 0x3E, 0x3E, 0x3E, 0x3E, 0x2A, 0x3E, 0x6E, 0xAA,
  817. 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x7E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x7E, 0xBE,
  818. 0x4E, 0x5E, 0x5E, 0x6E, 0x5E, 0x5E, 0x6E, 0x4E, 0x5E, 0x5E, 0x5E, 0x7E, 0x6E, 0x7E, 0x4E, 0xCE,
  819. 0x8A, 0x9E, 0x9E, 0xAA, 0x9E, 0x9E, 0xAE, 0xCE, 0x9E, 0x9E, 0x9E, 0xBE, 0xAA, 0xBE, 0xCE, 0x8A,
  820. };