balloc.c 24 KB

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