balloc.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  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. #include <linux/fs.h>
  9. #include <linux/ufs_fs.h>
  10. #include <linux/stat.h>
  11. #include <linux/time.h>
  12. #include <linux/string.h>
  13. #include <linux/quotaops.h>
  14. #include <linux/buffer_head.h>
  15. #include <linux/sched.h>
  16. #include <linux/bitops.h>
  17. #include <asm/byteorder.h>
  18. #include "swab.h"
  19. #include "util.h"
  20. #undef UFS_BALLOC_DEBUG
  21. #ifdef UFS_BALLOC_DEBUG
  22. #define UFSD(x) printk("(%s, %d), %s:", __FILE__, __LINE__, __FUNCTION__); printk x;
  23. #else
  24. #define UFSD(x)
  25. #endif
  26. static unsigned ufs_add_fragments (struct inode *, unsigned, unsigned, unsigned, int *);
  27. static unsigned ufs_alloc_fragments (struct inode *, unsigned, unsigned, unsigned, int *);
  28. static unsigned ufs_alloccg_block (struct inode *, struct ufs_cg_private_info *, unsigned, int *);
  29. static unsigned ufs_bitmap_search (struct super_block *, struct ufs_cg_private_info *, unsigned, unsigned);
  30. static unsigned char ufs_fragtable_8fpb[], ufs_fragtable_other[];
  31. static void ufs_clusteracct(struct super_block *, struct ufs_cg_private_info *, unsigned, int);
  32. /*
  33. * Free 'count' fragments from fragment number 'fragment'
  34. */
  35. void ufs_free_fragments (struct inode * inode, unsigned fragment, unsigned count) {
  36. struct super_block * sb;
  37. struct ufs_sb_private_info * uspi;
  38. struct ufs_super_block_first * usb1;
  39. struct ufs_cg_private_info * ucpi;
  40. struct ufs_cylinder_group * ucg;
  41. unsigned cgno, bit, end_bit, bbase, blkmap, i, blkno, cylno;
  42. sb = inode->i_sb;
  43. uspi = UFS_SB(sb)->s_uspi;
  44. usb1 = ubh_get_usb_first(USPI_UBH);
  45. UFSD(("ENTER, fragment %u, count %u\n", fragment, count))
  46. if (ufs_fragnum(fragment) + count > uspi->s_fpg)
  47. ufs_error (sb, "ufs_free_fragments", "internal error");
  48. lock_super(sb);
  49. cgno = ufs_dtog(fragment);
  50. bit = ufs_dtogd(fragment);
  51. if (cgno >= uspi->s_ncg) {
  52. ufs_panic (sb, "ufs_free_fragments", "freeing blocks are outside device");
  53. goto failed;
  54. }
  55. ucpi = ufs_load_cylinder (sb, cgno);
  56. if (!ucpi)
  57. goto failed;
  58. ucg = ubh_get_ucg (UCPI_UBH);
  59. if (!ufs_cg_chkmagic(sb, ucg)) {
  60. ufs_panic (sb, "ufs_free_fragments", "internal error, bad magic number on cg %u", cgno);
  61. goto failed;
  62. }
  63. end_bit = bit + count;
  64. bbase = ufs_blknum (bit);
  65. blkmap = ubh_blkmap (UCPI_UBH, ucpi->c_freeoff, bbase);
  66. ufs_fragacct (sb, blkmap, ucg->cg_frsum, -1);
  67. for (i = bit; i < end_bit; i++) {
  68. if (ubh_isclr (UCPI_UBH, ucpi->c_freeoff, i))
  69. ubh_setbit (UCPI_UBH, ucpi->c_freeoff, i);
  70. else ufs_error (sb, "ufs_free_fragments",
  71. "bit already cleared for fragment %u", i);
  72. }
  73. DQUOT_FREE_BLOCK (inode, count);
  74. fs32_add(sb, &ucg->cg_cs.cs_nffree, count);
  75. fs32_add(sb, &usb1->fs_cstotal.cs_nffree, count);
  76. fs32_add(sb, &UFS_SB(sb)->fs_cs(cgno).cs_nffree, count);
  77. blkmap = ubh_blkmap (UCPI_UBH, ucpi->c_freeoff, bbase);
  78. ufs_fragacct(sb, blkmap, ucg->cg_frsum, 1);
  79. /*
  80. * Trying to reassemble free fragments into block
  81. */
  82. blkno = ufs_fragstoblks (bbase);
  83. if (ubh_isblockset(UCPI_UBH, ucpi->c_freeoff, blkno)) {
  84. fs32_sub(sb, &ucg->cg_cs.cs_nffree, uspi->s_fpb);
  85. fs32_sub(sb, &usb1->fs_cstotal.cs_nffree, uspi->s_fpb);
  86. fs32_sub(sb, &UFS_SB(sb)->fs_cs(cgno).cs_nffree, uspi->s_fpb);
  87. if ((UFS_SB(sb)->s_flags & UFS_CG_MASK) == UFS_CG_44BSD)
  88. ufs_clusteracct (sb, ucpi, blkno, 1);
  89. fs32_add(sb, &ucg->cg_cs.cs_nbfree, 1);
  90. fs32_add(sb, &usb1->fs_cstotal.cs_nbfree, 1);
  91. fs32_add(sb, &UFS_SB(sb)->fs_cs(cgno).cs_nbfree, 1);
  92. cylno = ufs_cbtocylno (bbase);
  93. fs16_add(sb, &ubh_cg_blks(ucpi, cylno, ufs_cbtorpos(bbase)), 1);
  94. fs32_add(sb, &ubh_cg_blktot(ucpi, cylno), 1);
  95. }
  96. ubh_mark_buffer_dirty (USPI_UBH);
  97. ubh_mark_buffer_dirty (UCPI_UBH);
  98. if (sb->s_flags & MS_SYNCHRONOUS) {
  99. ubh_ll_rw_block (SWRITE, 1, (struct ufs_buffer_head **)&ucpi);
  100. ubh_wait_on_buffer (UCPI_UBH);
  101. }
  102. sb->s_dirt = 1;
  103. unlock_super (sb);
  104. UFSD(("EXIT\n"))
  105. return;
  106. failed:
  107. unlock_super (sb);
  108. UFSD(("EXIT (FAILED)\n"))
  109. return;
  110. }
  111. /*
  112. * Free 'count' fragments from fragment number 'fragment' (free whole blocks)
  113. */
  114. void ufs_free_blocks (struct inode * inode, unsigned fragment, unsigned count) {
  115. struct super_block * sb;
  116. struct ufs_sb_private_info * uspi;
  117. struct ufs_super_block_first * usb1;
  118. struct ufs_cg_private_info * ucpi;
  119. struct ufs_cylinder_group * ucg;
  120. unsigned overflow, cgno, bit, end_bit, blkno, i, cylno;
  121. sb = inode->i_sb;
  122. uspi = UFS_SB(sb)->s_uspi;
  123. usb1 = ubh_get_usb_first(USPI_UBH);
  124. UFSD(("ENTER, fragment %u, count %u\n", fragment, count))
  125. if ((fragment & uspi->s_fpbmask) || (count & uspi->s_fpbmask)) {
  126. ufs_error (sb, "ufs_free_blocks", "internal error, "
  127. "fragment %u, count %u\n", fragment, count);
  128. goto failed;
  129. }
  130. lock_super(sb);
  131. do_more:
  132. overflow = 0;
  133. cgno = ufs_dtog (fragment);
  134. bit = ufs_dtogd (fragment);
  135. if (cgno >= uspi->s_ncg) {
  136. ufs_panic (sb, "ufs_free_blocks", "freeing blocks are outside device");
  137. goto failed;
  138. }
  139. end_bit = bit + count;
  140. if (end_bit > uspi->s_fpg) {
  141. overflow = bit + count - uspi->s_fpg;
  142. count -= overflow;
  143. end_bit -= overflow;
  144. }
  145. ucpi = ufs_load_cylinder (sb, cgno);
  146. if (!ucpi)
  147. goto failed;
  148. ucg = ubh_get_ucg (UCPI_UBH);
  149. if (!ufs_cg_chkmagic(sb, ucg)) {
  150. ufs_panic (sb, "ufs_free_blocks", "internal error, bad magic number on cg %u", cgno);
  151. goto failed;
  152. }
  153. for (i = bit; i < end_bit; i += uspi->s_fpb) {
  154. blkno = ufs_fragstoblks(i);
  155. if (ubh_isblockset(UCPI_UBH, ucpi->c_freeoff, blkno)) {
  156. ufs_error(sb, "ufs_free_blocks", "freeing free fragment");
  157. }
  158. ubh_setblock(UCPI_UBH, ucpi->c_freeoff, blkno);
  159. if ((UFS_SB(sb)->s_flags & UFS_CG_MASK) == UFS_CG_44BSD)
  160. ufs_clusteracct (sb, ucpi, blkno, 1);
  161. DQUOT_FREE_BLOCK(inode, uspi->s_fpb);
  162. fs32_add(sb, &ucg->cg_cs.cs_nbfree, 1);
  163. fs32_add(sb, &usb1->fs_cstotal.cs_nbfree, 1);
  164. fs32_add(sb, &UFS_SB(sb)->fs_cs(cgno).cs_nbfree, 1);
  165. cylno = ufs_cbtocylno(i);
  166. fs16_add(sb, &ubh_cg_blks(ucpi, cylno, ufs_cbtorpos(i)), 1);
  167. fs32_add(sb, &ubh_cg_blktot(ucpi, cylno), 1);
  168. }
  169. ubh_mark_buffer_dirty (USPI_UBH);
  170. ubh_mark_buffer_dirty (UCPI_UBH);
  171. if (sb->s_flags & MS_SYNCHRONOUS) {
  172. ubh_ll_rw_block (SWRITE, 1, (struct ufs_buffer_head **)&ucpi);
  173. ubh_wait_on_buffer (UCPI_UBH);
  174. }
  175. if (overflow) {
  176. fragment += count;
  177. count = overflow;
  178. goto do_more;
  179. }
  180. sb->s_dirt = 1;
  181. unlock_super (sb);
  182. UFSD(("EXIT\n"))
  183. return;
  184. failed:
  185. unlock_super (sb);
  186. UFSD(("EXIT (FAILED)\n"))
  187. return;
  188. }
  189. #define NULLIFY_FRAGMENTS \
  190. for (i = oldcount; i < newcount; i++) { \
  191. bh = sb_getblk(sb, result + i); \
  192. memset (bh->b_data, 0, sb->s_blocksize); \
  193. set_buffer_uptodate(bh); \
  194. mark_buffer_dirty (bh); \
  195. if (IS_SYNC(inode)) \
  196. sync_dirty_buffer(bh); \
  197. brelse (bh); \
  198. }
  199. unsigned ufs_new_fragments (struct inode * inode, __fs32 * p, unsigned fragment,
  200. unsigned goal, unsigned count, int * err )
  201. {
  202. struct super_block * sb;
  203. struct ufs_sb_private_info * uspi;
  204. struct ufs_super_block_first * usb1;
  205. struct buffer_head * bh;
  206. unsigned cgno, oldcount, newcount, tmp, request, i, result;
  207. UFSD(("ENTER, ino %lu, fragment %u, goal %u, count %u\n", inode->i_ino, fragment, goal, count))
  208. sb = inode->i_sb;
  209. uspi = UFS_SB(sb)->s_uspi;
  210. usb1 = ubh_get_usb_first(USPI_UBH);
  211. *err = -ENOSPC;
  212. lock_super (sb);
  213. tmp = fs32_to_cpu(sb, *p);
  214. if (count + ufs_fragnum(fragment) > uspi->s_fpb) {
  215. ufs_warning (sb, "ufs_new_fragments", "internal warning"
  216. " fragment %u, count %u", fragment, count);
  217. count = uspi->s_fpb - ufs_fragnum(fragment);
  218. }
  219. oldcount = ufs_fragnum (fragment);
  220. newcount = oldcount + count;
  221. /*
  222. * Somebody else has just allocated our fragments
  223. */
  224. if (oldcount) {
  225. if (!tmp) {
  226. ufs_error (sb, "ufs_new_fragments", "internal error, "
  227. "fragment %u, tmp %u\n", fragment, tmp);
  228. unlock_super (sb);
  229. return (unsigned)-1;
  230. }
  231. if (fragment < UFS_I(inode)->i_lastfrag) {
  232. UFSD(("EXIT (ALREADY ALLOCATED)\n"))
  233. unlock_super (sb);
  234. return 0;
  235. }
  236. }
  237. else {
  238. if (tmp) {
  239. UFSD(("EXIT (ALREADY ALLOCATED)\n"))
  240. unlock_super(sb);
  241. return 0;
  242. }
  243. }
  244. /*
  245. * There is not enough space for user on the device
  246. */
  247. if (!capable(CAP_SYS_RESOURCE) && ufs_freespace(usb1, UFS_MINFREE) <= 0) {
  248. unlock_super (sb);
  249. UFSD(("EXIT (FAILED)\n"))
  250. return 0;
  251. }
  252. if (goal >= uspi->s_size)
  253. goal = 0;
  254. if (goal == 0)
  255. cgno = ufs_inotocg (inode->i_ino);
  256. else
  257. cgno = ufs_dtog (goal);
  258. /*
  259. * allocate new fragment
  260. */
  261. if (oldcount == 0) {
  262. result = ufs_alloc_fragments (inode, cgno, goal, count, err);
  263. if (result) {
  264. *p = cpu_to_fs32(sb, result);
  265. *err = 0;
  266. inode->i_blocks += count << uspi->s_nspfshift;
  267. UFS_I(inode)->i_lastfrag = max_t(u32, UFS_I(inode)->i_lastfrag, fragment + count);
  268. NULLIFY_FRAGMENTS
  269. }
  270. unlock_super(sb);
  271. UFSD(("EXIT, result %u\n", result))
  272. return result;
  273. }
  274. /*
  275. * resize block
  276. */
  277. result = ufs_add_fragments (inode, tmp, oldcount, newcount, err);
  278. if (result) {
  279. *err = 0;
  280. inode->i_blocks += count << uspi->s_nspfshift;
  281. UFS_I(inode)->i_lastfrag = max_t(u32, UFS_I(inode)->i_lastfrag, fragment + count);
  282. NULLIFY_FRAGMENTS
  283. unlock_super(sb);
  284. UFSD(("EXIT, result %u\n", result))
  285. return result;
  286. }
  287. /*
  288. * allocate new block and move data
  289. */
  290. switch (fs32_to_cpu(sb, usb1->fs_optim)) {
  291. case UFS_OPTSPACE:
  292. request = newcount;
  293. if (uspi->s_minfree < 5 || fs32_to_cpu(sb, usb1->fs_cstotal.cs_nffree)
  294. > uspi->s_dsize * uspi->s_minfree / (2 * 100) )
  295. break;
  296. usb1->fs_optim = cpu_to_fs32(sb, UFS_OPTTIME);
  297. break;
  298. default:
  299. usb1->fs_optim = cpu_to_fs32(sb, UFS_OPTTIME);
  300. case UFS_OPTTIME:
  301. request = uspi->s_fpb;
  302. if (fs32_to_cpu(sb, usb1->fs_cstotal.cs_nffree) < uspi->s_dsize *
  303. (uspi->s_minfree - 2) / 100)
  304. break;
  305. usb1->fs_optim = cpu_to_fs32(sb, UFS_OPTTIME);
  306. break;
  307. }
  308. result = ufs_alloc_fragments (inode, cgno, goal, request, err);
  309. if (result) {
  310. for (i = 0; i < oldcount; i++) {
  311. bh = sb_bread(sb, tmp + i);
  312. if(bh)
  313. {
  314. clear_buffer_dirty(bh);
  315. bh->b_blocknr = result + i;
  316. mark_buffer_dirty (bh);
  317. if (IS_SYNC(inode))
  318. sync_dirty_buffer(bh);
  319. brelse (bh);
  320. }
  321. else
  322. {
  323. printk(KERN_ERR "ufs_new_fragments: bread fail\n");
  324. unlock_super(sb);
  325. return 0;
  326. }
  327. }
  328. *p = cpu_to_fs32(sb, result);
  329. *err = 0;
  330. inode->i_blocks += count << uspi->s_nspfshift;
  331. UFS_I(inode)->i_lastfrag = max_t(u32, UFS_I(inode)->i_lastfrag, fragment + count);
  332. NULLIFY_FRAGMENTS
  333. unlock_super(sb);
  334. if (newcount < request)
  335. ufs_free_fragments (inode, result + newcount, request - newcount);
  336. ufs_free_fragments (inode, tmp, oldcount);
  337. UFSD(("EXIT, result %u\n", result))
  338. return result;
  339. }
  340. unlock_super(sb);
  341. UFSD(("EXIT (FAILED)\n"))
  342. return 0;
  343. }
  344. static unsigned
  345. ufs_add_fragments (struct inode * inode, unsigned fragment,
  346. unsigned oldcount, unsigned newcount, int * err)
  347. {
  348. struct super_block * sb;
  349. struct ufs_sb_private_info * uspi;
  350. struct ufs_super_block_first * usb1;
  351. struct ufs_cg_private_info * ucpi;
  352. struct ufs_cylinder_group * ucg;
  353. unsigned cgno, fragno, fragoff, count, fragsize, i;
  354. UFSD(("ENTER, fragment %u, oldcount %u, newcount %u\n", fragment, oldcount, newcount))
  355. sb = inode->i_sb;
  356. uspi = UFS_SB(sb)->s_uspi;
  357. usb1 = ubh_get_usb_first (USPI_UBH);
  358. count = newcount - oldcount;
  359. cgno = ufs_dtog(fragment);
  360. if (fs32_to_cpu(sb, UFS_SB(sb)->fs_cs(cgno).cs_nffree) < count)
  361. return 0;
  362. if ((ufs_fragnum (fragment) + newcount) > uspi->s_fpb)
  363. return 0;
  364. ucpi = ufs_load_cylinder (sb, cgno);
  365. if (!ucpi)
  366. return 0;
  367. ucg = ubh_get_ucg (UCPI_UBH);
  368. if (!ufs_cg_chkmagic(sb, ucg)) {
  369. ufs_panic (sb, "ufs_add_fragments",
  370. "internal error, bad magic number on cg %u", cgno);
  371. return 0;
  372. }
  373. fragno = ufs_dtogd (fragment);
  374. fragoff = ufs_fragnum (fragno);
  375. for (i = oldcount; i < newcount; i++)
  376. if (ubh_isclr (UCPI_UBH, ucpi->c_freeoff, fragno + i))
  377. return 0;
  378. /*
  379. * Block can be extended
  380. */
  381. ucg->cg_time = cpu_to_fs32(sb, get_seconds());
  382. for (i = newcount; i < (uspi->s_fpb - fragoff); i++)
  383. if (ubh_isclr (UCPI_UBH, ucpi->c_freeoff, fragno + i))
  384. break;
  385. fragsize = i - oldcount;
  386. if (!fs32_to_cpu(sb, ucg->cg_frsum[fragsize]))
  387. ufs_panic (sb, "ufs_add_fragments",
  388. "internal error or corrupted bitmap on cg %u", cgno);
  389. fs32_sub(sb, &ucg->cg_frsum[fragsize], 1);
  390. if (fragsize != count)
  391. fs32_add(sb, &ucg->cg_frsum[fragsize - count], 1);
  392. for (i = oldcount; i < newcount; i++)
  393. ubh_clrbit (UCPI_UBH, ucpi->c_freeoff, fragno + i);
  394. if(DQUOT_ALLOC_BLOCK(inode, count)) {
  395. *err = -EDQUOT;
  396. return 0;
  397. }
  398. fs32_sub(sb, &ucg->cg_cs.cs_nffree, count);
  399. fs32_sub(sb, &UFS_SB(sb)->fs_cs(cgno).cs_nffree, count);
  400. fs32_sub(sb, &usb1->fs_cstotal.cs_nffree, count);
  401. ubh_mark_buffer_dirty (USPI_UBH);
  402. ubh_mark_buffer_dirty (UCPI_UBH);
  403. if (sb->s_flags & MS_SYNCHRONOUS) {
  404. ubh_ll_rw_block (SWRITE, 1, (struct ufs_buffer_head **)&ucpi);
  405. ubh_wait_on_buffer (UCPI_UBH);
  406. }
  407. sb->s_dirt = 1;
  408. UFSD(("EXIT, fragment %u\n", fragment))
  409. return fragment;
  410. }
  411. #define UFS_TEST_FREE_SPACE_CG \
  412. ucg = (struct ufs_cylinder_group *) UFS_SB(sb)->s_ucg[cgno]->b_data; \
  413. if (fs32_to_cpu(sb, ucg->cg_cs.cs_nbfree)) \
  414. goto cg_found; \
  415. for (k = count; k < uspi->s_fpb; k++) \
  416. if (fs32_to_cpu(sb, ucg->cg_frsum[k])) \
  417. goto cg_found;
  418. static unsigned ufs_alloc_fragments (struct inode * inode, unsigned cgno,
  419. unsigned goal, unsigned count, int * err)
  420. {
  421. struct super_block * sb;
  422. struct ufs_sb_private_info * uspi;
  423. struct ufs_super_block_first * usb1;
  424. struct ufs_cg_private_info * ucpi;
  425. struct ufs_cylinder_group * ucg;
  426. unsigned oldcg, i, j, k, result, allocsize;
  427. UFSD(("ENTER, ino %lu, cgno %u, goal %u, count %u\n", inode->i_ino, cgno, goal, count))
  428. sb = inode->i_sb;
  429. uspi = UFS_SB(sb)->s_uspi;
  430. usb1 = ubh_get_usb_first(USPI_UBH);
  431. oldcg = cgno;
  432. /*
  433. * 1. searching on preferred cylinder group
  434. */
  435. UFS_TEST_FREE_SPACE_CG
  436. /*
  437. * 2. quadratic rehash
  438. */
  439. for (j = 1; j < uspi->s_ncg; j *= 2) {
  440. cgno += j;
  441. if (cgno >= uspi->s_ncg)
  442. cgno -= uspi->s_ncg;
  443. UFS_TEST_FREE_SPACE_CG
  444. }
  445. /*
  446. * 3. brute force search
  447. * We start at i = 2 ( 0 is checked at 1.step, 1 at 2.step )
  448. */
  449. cgno = (oldcg + 1) % uspi->s_ncg;
  450. for (j = 2; j < uspi->s_ncg; j++) {
  451. cgno++;
  452. if (cgno >= uspi->s_ncg)
  453. cgno = 0;
  454. UFS_TEST_FREE_SPACE_CG
  455. }
  456. UFSD(("EXIT (FAILED)\n"))
  457. return 0;
  458. cg_found:
  459. ucpi = ufs_load_cylinder (sb, cgno);
  460. if (!ucpi)
  461. return 0;
  462. ucg = ubh_get_ucg (UCPI_UBH);
  463. if (!ufs_cg_chkmagic(sb, ucg))
  464. ufs_panic (sb, "ufs_alloc_fragments",
  465. "internal error, bad magic number on cg %u", cgno);
  466. ucg->cg_time = cpu_to_fs32(sb, get_seconds());
  467. if (count == uspi->s_fpb) {
  468. result = ufs_alloccg_block (inode, ucpi, goal, err);
  469. if (result == (unsigned)-1)
  470. return 0;
  471. goto succed;
  472. }
  473. for (allocsize = count; allocsize < uspi->s_fpb; allocsize++)
  474. if (fs32_to_cpu(sb, ucg->cg_frsum[allocsize]) != 0)
  475. break;
  476. if (allocsize == uspi->s_fpb) {
  477. result = ufs_alloccg_block (inode, ucpi, goal, err);
  478. if (result == (unsigned)-1)
  479. return 0;
  480. goal = ufs_dtogd (result);
  481. for (i = count; i < uspi->s_fpb; i++)
  482. ubh_setbit (UCPI_UBH, ucpi->c_freeoff, goal + i);
  483. i = uspi->s_fpb - count;
  484. DQUOT_FREE_BLOCK(inode, i);
  485. fs32_add(sb, &ucg->cg_cs.cs_nffree, i);
  486. fs32_add(sb, &usb1->fs_cstotal.cs_nffree, i);
  487. fs32_add(sb, &UFS_SB(sb)->fs_cs(cgno).cs_nffree, i);
  488. fs32_add(sb, &ucg->cg_frsum[i], 1);
  489. goto succed;
  490. }
  491. result = ufs_bitmap_search (sb, ucpi, goal, allocsize);
  492. if (result == (unsigned)-1)
  493. return 0;
  494. if(DQUOT_ALLOC_BLOCK(inode, count)) {
  495. *err = -EDQUOT;
  496. return 0;
  497. }
  498. for (i = 0; i < count; i++)
  499. ubh_clrbit (UCPI_UBH, ucpi->c_freeoff, result + i);
  500. fs32_sub(sb, &ucg->cg_cs.cs_nffree, count);
  501. fs32_sub(sb, &usb1->fs_cstotal.cs_nffree, count);
  502. fs32_sub(sb, &UFS_SB(sb)->fs_cs(cgno).cs_nffree, count);
  503. fs32_sub(sb, &ucg->cg_frsum[allocsize], 1);
  504. if (count != allocsize)
  505. fs32_add(sb, &ucg->cg_frsum[allocsize - count], 1);
  506. succed:
  507. ubh_mark_buffer_dirty (USPI_UBH);
  508. ubh_mark_buffer_dirty (UCPI_UBH);
  509. if (sb->s_flags & MS_SYNCHRONOUS) {
  510. ubh_ll_rw_block (SWRITE, 1, (struct ufs_buffer_head **)&ucpi);
  511. ubh_wait_on_buffer (UCPI_UBH);
  512. }
  513. sb->s_dirt = 1;
  514. result += cgno * uspi->s_fpg;
  515. UFSD(("EXIT3, result %u\n", result))
  516. return result;
  517. }
  518. static unsigned ufs_alloccg_block (struct inode * inode,
  519. struct ufs_cg_private_info * ucpi, unsigned goal, int * err)
  520. {
  521. struct super_block * sb;
  522. struct ufs_sb_private_info * uspi;
  523. struct ufs_super_block_first * usb1;
  524. struct ufs_cylinder_group * ucg;
  525. unsigned result, cylno, blkno;
  526. UFSD(("ENTER, goal %u\n", goal))
  527. sb = inode->i_sb;
  528. uspi = UFS_SB(sb)->s_uspi;
  529. usb1 = ubh_get_usb_first(USPI_UBH);
  530. ucg = ubh_get_ucg(UCPI_UBH);
  531. if (goal == 0) {
  532. goal = ucpi->c_rotor;
  533. goto norot;
  534. }
  535. goal = ufs_blknum (goal);
  536. goal = ufs_dtogd (goal);
  537. /*
  538. * If the requested block is available, use it.
  539. */
  540. if (ubh_isblockset(UCPI_UBH, ucpi->c_freeoff, ufs_fragstoblks(goal))) {
  541. result = goal;
  542. goto gotit;
  543. }
  544. norot:
  545. result = ufs_bitmap_search (sb, ucpi, goal, uspi->s_fpb);
  546. if (result == (unsigned)-1)
  547. return (unsigned)-1;
  548. ucpi->c_rotor = result;
  549. gotit:
  550. blkno = ufs_fragstoblks(result);
  551. ubh_clrblock (UCPI_UBH, ucpi->c_freeoff, blkno);
  552. if ((UFS_SB(sb)->s_flags & UFS_CG_MASK) == UFS_CG_44BSD)
  553. ufs_clusteracct (sb, ucpi, blkno, -1);
  554. if(DQUOT_ALLOC_BLOCK(inode, uspi->s_fpb)) {
  555. *err = -EDQUOT;
  556. return (unsigned)-1;
  557. }
  558. fs32_sub(sb, &ucg->cg_cs.cs_nbfree, 1);
  559. fs32_sub(sb, &usb1->fs_cstotal.cs_nbfree, 1);
  560. fs32_sub(sb, &UFS_SB(sb)->fs_cs(ucpi->c_cgx).cs_nbfree, 1);
  561. cylno = ufs_cbtocylno(result);
  562. fs16_sub(sb, &ubh_cg_blks(ucpi, cylno, ufs_cbtorpos(result)), 1);
  563. fs32_sub(sb, &ubh_cg_blktot(ucpi, cylno), 1);
  564. UFSD(("EXIT, result %u\n", result))
  565. return result;
  566. }
  567. static unsigned ufs_bitmap_search (struct super_block * sb,
  568. struct ufs_cg_private_info * ucpi, unsigned goal, unsigned count)
  569. {
  570. struct ufs_sb_private_info * uspi;
  571. struct ufs_super_block_first * usb1;
  572. struct ufs_cylinder_group * ucg;
  573. unsigned start, length, location, result;
  574. unsigned possition, fragsize, blockmap, mask;
  575. UFSD(("ENTER, cg %u, goal %u, count %u\n", ucpi->c_cgx, goal, count))
  576. uspi = UFS_SB(sb)->s_uspi;
  577. usb1 = ubh_get_usb_first (USPI_UBH);
  578. ucg = ubh_get_ucg(UCPI_UBH);
  579. if (goal)
  580. start = ufs_dtogd(goal) >> 3;
  581. else
  582. start = ucpi->c_frotor >> 3;
  583. length = ((uspi->s_fpg + 7) >> 3) - start;
  584. location = ubh_scanc(UCPI_UBH, ucpi->c_freeoff + start, length,
  585. (uspi->s_fpb == 8) ? ufs_fragtable_8fpb : ufs_fragtable_other,
  586. 1 << (count - 1 + (uspi->s_fpb & 7)));
  587. if (location == 0) {
  588. length = start + 1;
  589. location = ubh_scanc(UCPI_UBH, ucpi->c_freeoff, length,
  590. (uspi->s_fpb == 8) ? ufs_fragtable_8fpb : ufs_fragtable_other,
  591. 1 << (count - 1 + (uspi->s_fpb & 7)));
  592. if (location == 0) {
  593. ufs_error (sb, "ufs_bitmap_search",
  594. "bitmap corrupted on cg %u, start %u, length %u, count %u, freeoff %u\n",
  595. ucpi->c_cgx, start, length, count, ucpi->c_freeoff);
  596. return (unsigned)-1;
  597. }
  598. start = 0;
  599. }
  600. result = (start + length - location) << 3;
  601. ucpi->c_frotor = result;
  602. /*
  603. * found the byte in the map
  604. */
  605. blockmap = ubh_blkmap(UCPI_UBH, ucpi->c_freeoff, result);
  606. fragsize = 0;
  607. for (possition = 0, mask = 1; possition < 8; possition++, mask <<= 1) {
  608. if (blockmap & mask) {
  609. if (!(possition & uspi->s_fpbmask))
  610. fragsize = 1;
  611. else
  612. fragsize++;
  613. }
  614. else {
  615. if (fragsize == count) {
  616. result += possition - count;
  617. UFSD(("EXIT, result %u\n", result))
  618. return result;
  619. }
  620. fragsize = 0;
  621. }
  622. }
  623. if (fragsize == count) {
  624. result += possition - count;
  625. UFSD(("EXIT, result %u\n", result))
  626. return result;
  627. }
  628. ufs_error (sb, "ufs_bitmap_search", "block not in map on cg %u\n", ucpi->c_cgx);
  629. UFSD(("EXIT (FAILED)\n"))
  630. return (unsigned)-1;
  631. }
  632. static void ufs_clusteracct(struct super_block * sb,
  633. struct ufs_cg_private_info * ucpi, unsigned blkno, int cnt)
  634. {
  635. struct ufs_sb_private_info * uspi;
  636. int i, start, end, forw, back;
  637. uspi = UFS_SB(sb)->s_uspi;
  638. if (uspi->s_contigsumsize <= 0)
  639. return;
  640. if (cnt > 0)
  641. ubh_setbit(UCPI_UBH, ucpi->c_clusteroff, blkno);
  642. else
  643. ubh_clrbit(UCPI_UBH, ucpi->c_clusteroff, blkno);
  644. /*
  645. * Find the size of the cluster going forward.
  646. */
  647. start = blkno + 1;
  648. end = start + uspi->s_contigsumsize;
  649. if ( end >= ucpi->c_nclusterblks)
  650. end = ucpi->c_nclusterblks;
  651. i = ubh_find_next_zero_bit (UCPI_UBH, ucpi->c_clusteroff, end, start);
  652. if (i > end)
  653. i = end;
  654. forw = i - start;
  655. /*
  656. * Find the size of the cluster going backward.
  657. */
  658. start = blkno - 1;
  659. end = start - uspi->s_contigsumsize;
  660. if (end < 0 )
  661. end = -1;
  662. i = ubh_find_last_zero_bit (UCPI_UBH, ucpi->c_clusteroff, start, end);
  663. if ( i < end)
  664. i = end;
  665. back = start - i;
  666. /*
  667. * Account for old cluster and the possibly new forward and
  668. * back clusters.
  669. */
  670. i = back + forw + 1;
  671. if (i > uspi->s_contigsumsize)
  672. i = uspi->s_contigsumsize;
  673. fs32_add(sb, (__fs32*)ubh_get_addr(UCPI_UBH, ucpi->c_clustersumoff + (i << 2)), cnt);
  674. if (back > 0)
  675. fs32_sub(sb, (__fs32*)ubh_get_addr(UCPI_UBH, ucpi->c_clustersumoff + (back << 2)), cnt);
  676. if (forw > 0)
  677. fs32_sub(sb, (__fs32*)ubh_get_addr(UCPI_UBH, ucpi->c_clustersumoff + (forw << 2)), cnt);
  678. }
  679. static unsigned char ufs_fragtable_8fpb[] = {
  680. 0x00, 0x01, 0x01, 0x02, 0x01, 0x01, 0x02, 0x04, 0x01, 0x01, 0x01, 0x03, 0x02, 0x03, 0x04, 0x08,
  681. 0x01, 0x01, 0x01, 0x03, 0x01, 0x01, 0x03, 0x05, 0x02, 0x03, 0x03, 0x02, 0x04, 0x05, 0x08, 0x10,
  682. 0x01, 0x01, 0x01, 0x03, 0x01, 0x01, 0x03, 0x05, 0x01, 0x01, 0x01, 0x03, 0x03, 0x03, 0x05, 0x09,
  683. 0x02, 0x03, 0x03, 0x02, 0x03, 0x03, 0x02, 0x06, 0x04, 0x05, 0x05, 0x06, 0x08, 0x09, 0x10, 0x20,
  684. 0x01, 0x01, 0x01, 0x03, 0x01, 0x01, 0x03, 0x05, 0x01, 0x01, 0x01, 0x03, 0x03, 0x03, 0x05, 0x09,
  685. 0x01, 0x01, 0x01, 0x03, 0x01, 0x01, 0x03, 0x05, 0x03, 0x03, 0x03, 0x03, 0x05, 0x05, 0x09, 0x11,
  686. 0x02, 0x03, 0x03, 0x02, 0x03, 0x03, 0x02, 0x06, 0x03, 0x03, 0x03, 0x03, 0x02, 0x03, 0x06, 0x0A,
  687. 0x04, 0x05, 0x05, 0x06, 0x05, 0x05, 0x06, 0x04, 0x08, 0x09, 0x09, 0x0A, 0x10, 0x11, 0x20, 0x40,
  688. 0x01, 0x01, 0x01, 0x03, 0x01, 0x01, 0x03, 0x05, 0x01, 0x01, 0x01, 0x03, 0x03, 0x03, 0x05, 0x09,
  689. 0x01, 0x01, 0x01, 0x03, 0x01, 0x01, 0x03, 0x05, 0x03, 0x03, 0x03, 0x03, 0x05, 0x05, 0x09, 0x11,
  690. 0x01, 0x01, 0x01, 0x03, 0x01, 0x01, 0x03, 0x05, 0x01, 0x01, 0x01, 0x03, 0x03, 0x03, 0x05, 0x09,
  691. 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x07, 0x05, 0x05, 0x05, 0x07, 0x09, 0x09, 0x11, 0x21,
  692. 0x02, 0x03, 0x03, 0x02, 0x03, 0x03, 0x02, 0x06, 0x03, 0x03, 0x03, 0x03, 0x02, 0x03, 0x06, 0x0A,
  693. 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x07, 0x02, 0x03, 0x03, 0x02, 0x06, 0x07, 0x0A, 0x12,
  694. 0x04, 0x05, 0x05, 0x06, 0x05, 0x05, 0x06, 0x04, 0x05, 0x05, 0x05, 0x07, 0x06, 0x07, 0x04, 0x0C,
  695. 0x08, 0x09, 0x09, 0x0A, 0x09, 0x09, 0x0A, 0x0C, 0x10, 0x11, 0x11, 0x12, 0x20, 0x21, 0x40, 0x80,
  696. };
  697. static unsigned char ufs_fragtable_other[] = {
  698. 0x00, 0x16, 0x16, 0x2A, 0x16, 0x16, 0x26, 0x4E, 0x16, 0x16, 0x16, 0x3E, 0x2A, 0x3E, 0x4E, 0x8A,
  699. 0x16, 0x16, 0x16, 0x3E, 0x16, 0x16, 0x36, 0x5E, 0x16, 0x16, 0x16, 0x3E, 0x3E, 0x3E, 0x5E, 0x9E,
  700. 0x16, 0x16, 0x16, 0x3E, 0x16, 0x16, 0x36, 0x5E, 0x16, 0x16, 0x16, 0x3E, 0x3E, 0x3E, 0x5E, 0x9E,
  701. 0x2A, 0x3E, 0x3E, 0x2A, 0x3E, 0x3E, 0x2E, 0x6E, 0x3E, 0x3E, 0x3E, 0x3E, 0x2A, 0x3E, 0x6E, 0xAA,
  702. 0x16, 0x16, 0x16, 0x3E, 0x16, 0x16, 0x36, 0x5E, 0x16, 0x16, 0x16, 0x3E, 0x3E, 0x3E, 0x5E, 0x9E,
  703. 0x16, 0x16, 0x16, 0x3E, 0x16, 0x16, 0x36, 0x5E, 0x16, 0x16, 0x16, 0x3E, 0x3E, 0x3E, 0x5E, 0x9E,
  704. 0x26, 0x36, 0x36, 0x2E, 0x36, 0x36, 0x26, 0x6E, 0x36, 0x36, 0x36, 0x3E, 0x2E, 0x3E, 0x6E, 0xAE,
  705. 0x4E, 0x5E, 0x5E, 0x6E, 0x5E, 0x5E, 0x6E, 0x4E, 0x5E, 0x5E, 0x5E, 0x7E, 0x6E, 0x7E, 0x4E, 0xCE,
  706. 0x16, 0x16, 0x16, 0x3E, 0x16, 0x16, 0x36, 0x5E, 0x16, 0x16, 0x16, 0x3E, 0x3E, 0x3E, 0x5E, 0x9E,
  707. 0x16, 0x16, 0x16, 0x3E, 0x16, 0x16, 0x36, 0x5E, 0x16, 0x16, 0x16, 0x3E, 0x3E, 0x3E, 0x5E, 0x9E,
  708. 0x16, 0x16, 0x16, 0x3E, 0x16, 0x16, 0x36, 0x5E, 0x16, 0x16, 0x16, 0x3E, 0x3E, 0x3E, 0x5E, 0x9E,
  709. 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x7E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x7E, 0xBE,
  710. 0x2A, 0x3E, 0x3E, 0x2A, 0x3E, 0x3E, 0x2E, 0x6E, 0x3E, 0x3E, 0x3E, 0x3E, 0x2A, 0x3E, 0x6E, 0xAA,
  711. 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x7E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x7E, 0xBE,
  712. 0x4E, 0x5E, 0x5E, 0x6E, 0x5E, 0x5E, 0x6E, 0x4E, 0x5E, 0x5E, 0x5E, 0x7E, 0x6E, 0x7E, 0x4E, 0xCE,
  713. 0x8A, 0x9E, 0x9E, 0xAA, 0x9E, 0x9E, 0xAE, 0xCE, 0x9E, 0x9E, 0x9E, 0xBE, 0xAA, 0xBE, 0xCE, 0x8A,
  714. };