balloc.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  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_wait_on_buffer (UCPI_UBH);
  100. ubh_ll_rw_block (WRITE, 1, (struct ufs_buffer_head **)&ucpi);
  101. ubh_wait_on_buffer (UCPI_UBH);
  102. }
  103. sb->s_dirt = 1;
  104. unlock_super (sb);
  105. UFSD(("EXIT\n"))
  106. return;
  107. failed:
  108. unlock_super (sb);
  109. UFSD(("EXIT (FAILED)\n"))
  110. return;
  111. }
  112. /*
  113. * Free 'count' fragments from fragment number 'fragment' (free whole blocks)
  114. */
  115. void ufs_free_blocks (struct inode * inode, unsigned fragment, unsigned count) {
  116. struct super_block * sb;
  117. struct ufs_sb_private_info * uspi;
  118. struct ufs_super_block_first * usb1;
  119. struct ufs_cg_private_info * ucpi;
  120. struct ufs_cylinder_group * ucg;
  121. unsigned overflow, cgno, bit, end_bit, blkno, i, cylno;
  122. sb = inode->i_sb;
  123. uspi = UFS_SB(sb)->s_uspi;
  124. usb1 = ubh_get_usb_first(USPI_UBH);
  125. UFSD(("ENTER, fragment %u, count %u\n", fragment, count))
  126. if ((fragment & uspi->s_fpbmask) || (count & uspi->s_fpbmask)) {
  127. ufs_error (sb, "ufs_free_blocks", "internal error, "
  128. "fragment %u, count %u\n", fragment, count);
  129. goto failed;
  130. }
  131. lock_super(sb);
  132. do_more:
  133. overflow = 0;
  134. cgno = ufs_dtog (fragment);
  135. bit = ufs_dtogd (fragment);
  136. if (cgno >= uspi->s_ncg) {
  137. ufs_panic (sb, "ufs_free_blocks", "freeing blocks are outside device");
  138. goto failed;
  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;
  149. ucg = ubh_get_ucg (UCPI_UBH);
  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;
  153. }
  154. for (i = bit; i < end_bit; i += uspi->s_fpb) {
  155. blkno = ufs_fragstoblks(i);
  156. if (ubh_isblockset(UCPI_UBH, ucpi->c_freeoff, blkno)) {
  157. ufs_error(sb, "ufs_free_blocks", "freeing free fragment");
  158. }
  159. ubh_setblock(UCPI_UBH, 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. DQUOT_FREE_BLOCK(inode, uspi->s_fpb);
  163. fs32_add(sb, &ucg->cg_cs.cs_nbfree, 1);
  164. fs32_add(sb, &usb1->fs_cstotal.cs_nbfree, 1);
  165. fs32_add(sb, &UFS_SB(sb)->fs_cs(cgno).cs_nbfree, 1);
  166. cylno = ufs_cbtocylno(i);
  167. fs16_add(sb, &ubh_cg_blks(ucpi, cylno, ufs_cbtorpos(i)), 1);
  168. fs32_add(sb, &ubh_cg_blktot(ucpi, cylno), 1);
  169. }
  170. ubh_mark_buffer_dirty (USPI_UBH);
  171. ubh_mark_buffer_dirty (UCPI_UBH);
  172. if (sb->s_flags & MS_SYNCHRONOUS) {
  173. ubh_wait_on_buffer (UCPI_UBH);
  174. ubh_ll_rw_block (WRITE, 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_UBH);
  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_UBH);
  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_wait_on_buffer (UCPI_UBH);
  407. ubh_ll_rw_block (WRITE, 1, (struct ufs_buffer_head **)&ucpi);
  408. ubh_wait_on_buffer (UCPI_UBH);
  409. }
  410. sb->s_dirt = 1;
  411. UFSD(("EXIT, fragment %u\n", fragment))
  412. return fragment;
  413. }
  414. #define UFS_TEST_FREE_SPACE_CG \
  415. ucg = (struct ufs_cylinder_group *) UFS_SB(sb)->s_ucg[cgno]->b_data; \
  416. if (fs32_to_cpu(sb, ucg->cg_cs.cs_nbfree)) \
  417. goto cg_found; \
  418. for (k = count; k < uspi->s_fpb; k++) \
  419. if (fs32_to_cpu(sb, ucg->cg_frsum[k])) \
  420. goto cg_found;
  421. static unsigned ufs_alloc_fragments (struct inode * inode, unsigned cgno,
  422. unsigned goal, unsigned count, int * err)
  423. {
  424. struct super_block * sb;
  425. struct ufs_sb_private_info * uspi;
  426. struct ufs_super_block_first * usb1;
  427. struct ufs_cg_private_info * ucpi;
  428. struct ufs_cylinder_group * ucg;
  429. unsigned oldcg, i, j, k, result, allocsize;
  430. UFSD(("ENTER, ino %lu, cgno %u, goal %u, count %u\n", inode->i_ino, cgno, goal, count))
  431. sb = inode->i_sb;
  432. uspi = UFS_SB(sb)->s_uspi;
  433. usb1 = ubh_get_usb_first(USPI_UBH);
  434. oldcg = cgno;
  435. /*
  436. * 1. searching on preferred cylinder group
  437. */
  438. UFS_TEST_FREE_SPACE_CG
  439. /*
  440. * 2. quadratic rehash
  441. */
  442. for (j = 1; j < uspi->s_ncg; j *= 2) {
  443. cgno += j;
  444. if (cgno >= uspi->s_ncg)
  445. cgno -= uspi->s_ncg;
  446. UFS_TEST_FREE_SPACE_CG
  447. }
  448. /*
  449. * 3. brute force search
  450. * We start at i = 2 ( 0 is checked at 1.step, 1 at 2.step )
  451. */
  452. cgno = (oldcg + 1) % uspi->s_ncg;
  453. for (j = 2; j < uspi->s_ncg; j++) {
  454. cgno++;
  455. if (cgno >= uspi->s_ncg)
  456. cgno = 0;
  457. UFS_TEST_FREE_SPACE_CG
  458. }
  459. UFSD(("EXIT (FAILED)\n"))
  460. return 0;
  461. cg_found:
  462. ucpi = ufs_load_cylinder (sb, cgno);
  463. if (!ucpi)
  464. return 0;
  465. ucg = ubh_get_ucg (UCPI_UBH);
  466. if (!ufs_cg_chkmagic(sb, ucg))
  467. ufs_panic (sb, "ufs_alloc_fragments",
  468. "internal error, bad magic number on cg %u", cgno);
  469. ucg->cg_time = cpu_to_fs32(sb, get_seconds());
  470. if (count == uspi->s_fpb) {
  471. result = ufs_alloccg_block (inode, ucpi, goal, err);
  472. if (result == (unsigned)-1)
  473. return 0;
  474. goto succed;
  475. }
  476. for (allocsize = count; allocsize < uspi->s_fpb; allocsize++)
  477. if (fs32_to_cpu(sb, ucg->cg_frsum[allocsize]) != 0)
  478. break;
  479. if (allocsize == uspi->s_fpb) {
  480. result = ufs_alloccg_block (inode, ucpi, goal, err);
  481. if (result == (unsigned)-1)
  482. return 0;
  483. goal = ufs_dtogd (result);
  484. for (i = count; i < uspi->s_fpb; i++)
  485. ubh_setbit (UCPI_UBH, ucpi->c_freeoff, goal + i);
  486. i = uspi->s_fpb - count;
  487. DQUOT_FREE_BLOCK(inode, i);
  488. fs32_add(sb, &ucg->cg_cs.cs_nffree, i);
  489. fs32_add(sb, &usb1->fs_cstotal.cs_nffree, i);
  490. fs32_add(sb, &UFS_SB(sb)->fs_cs(cgno).cs_nffree, i);
  491. fs32_add(sb, &ucg->cg_frsum[i], 1);
  492. goto succed;
  493. }
  494. result = ufs_bitmap_search (sb, ucpi, goal, allocsize);
  495. if (result == (unsigned)-1)
  496. return 0;
  497. if(DQUOT_ALLOC_BLOCK(inode, count)) {
  498. *err = -EDQUOT;
  499. return 0;
  500. }
  501. for (i = 0; i < count; i++)
  502. ubh_clrbit (UCPI_UBH, ucpi->c_freeoff, result + i);
  503. fs32_sub(sb, &ucg->cg_cs.cs_nffree, count);
  504. fs32_sub(sb, &usb1->fs_cstotal.cs_nffree, count);
  505. fs32_sub(sb, &UFS_SB(sb)->fs_cs(cgno).cs_nffree, count);
  506. fs32_sub(sb, &ucg->cg_frsum[allocsize], 1);
  507. if (count != allocsize)
  508. fs32_add(sb, &ucg->cg_frsum[allocsize - count], 1);
  509. succed:
  510. ubh_mark_buffer_dirty (USPI_UBH);
  511. ubh_mark_buffer_dirty (UCPI_UBH);
  512. if (sb->s_flags & MS_SYNCHRONOUS) {
  513. ubh_wait_on_buffer (UCPI_UBH);
  514. ubh_ll_rw_block (WRITE, 1, (struct ufs_buffer_head **)&ucpi);
  515. ubh_wait_on_buffer (UCPI_UBH);
  516. }
  517. sb->s_dirt = 1;
  518. result += cgno * uspi->s_fpg;
  519. UFSD(("EXIT3, result %u\n", result))
  520. return result;
  521. }
  522. static unsigned ufs_alloccg_block (struct inode * inode,
  523. struct ufs_cg_private_info * ucpi, unsigned goal, int * err)
  524. {
  525. struct super_block * sb;
  526. struct ufs_sb_private_info * uspi;
  527. struct ufs_super_block_first * usb1;
  528. struct ufs_cylinder_group * ucg;
  529. unsigned result, cylno, blkno;
  530. UFSD(("ENTER, goal %u\n", goal))
  531. sb = inode->i_sb;
  532. uspi = UFS_SB(sb)->s_uspi;
  533. usb1 = ubh_get_usb_first(USPI_UBH);
  534. ucg = ubh_get_ucg(UCPI_UBH);
  535. if (goal == 0) {
  536. goal = ucpi->c_rotor;
  537. goto norot;
  538. }
  539. goal = ufs_blknum (goal);
  540. goal = ufs_dtogd (goal);
  541. /*
  542. * If the requested block is available, use it.
  543. */
  544. if (ubh_isblockset(UCPI_UBH, ucpi->c_freeoff, ufs_fragstoblks(goal))) {
  545. result = goal;
  546. goto gotit;
  547. }
  548. norot:
  549. result = ufs_bitmap_search (sb, ucpi, goal, uspi->s_fpb);
  550. if (result == (unsigned)-1)
  551. return (unsigned)-1;
  552. ucpi->c_rotor = result;
  553. gotit:
  554. blkno = ufs_fragstoblks(result);
  555. ubh_clrblock (UCPI_UBH, ucpi->c_freeoff, blkno);
  556. if ((UFS_SB(sb)->s_flags & UFS_CG_MASK) == UFS_CG_44BSD)
  557. ufs_clusteracct (sb, ucpi, blkno, -1);
  558. if(DQUOT_ALLOC_BLOCK(inode, uspi->s_fpb)) {
  559. *err = -EDQUOT;
  560. return (unsigned)-1;
  561. }
  562. fs32_sub(sb, &ucg->cg_cs.cs_nbfree, 1);
  563. fs32_sub(sb, &usb1->fs_cstotal.cs_nbfree, 1);
  564. fs32_sub(sb, &UFS_SB(sb)->fs_cs(ucpi->c_cgx).cs_nbfree, 1);
  565. cylno = ufs_cbtocylno(result);
  566. fs16_sub(sb, &ubh_cg_blks(ucpi, cylno, ufs_cbtorpos(result)), 1);
  567. fs32_sub(sb, &ubh_cg_blktot(ucpi, cylno), 1);
  568. UFSD(("EXIT, result %u\n", result))
  569. return result;
  570. }
  571. static unsigned ufs_bitmap_search (struct super_block * sb,
  572. struct ufs_cg_private_info * ucpi, unsigned goal, unsigned count)
  573. {
  574. struct ufs_sb_private_info * uspi;
  575. struct ufs_super_block_first * usb1;
  576. struct ufs_cylinder_group * ucg;
  577. unsigned start, length, location, result;
  578. unsigned possition, fragsize, blockmap, mask;
  579. UFSD(("ENTER, cg %u, goal %u, count %u\n", ucpi->c_cgx, goal, count))
  580. uspi = UFS_SB(sb)->s_uspi;
  581. usb1 = ubh_get_usb_first (USPI_UBH);
  582. ucg = ubh_get_ucg(UCPI_UBH);
  583. if (goal)
  584. start = ufs_dtogd(goal) >> 3;
  585. else
  586. start = ucpi->c_frotor >> 3;
  587. length = ((uspi->s_fpg + 7) >> 3) - start;
  588. location = ubh_scanc(UCPI_UBH, ucpi->c_freeoff + start, length,
  589. (uspi->s_fpb == 8) ? ufs_fragtable_8fpb : ufs_fragtable_other,
  590. 1 << (count - 1 + (uspi->s_fpb & 7)));
  591. if (location == 0) {
  592. length = start + 1;
  593. location = ubh_scanc(UCPI_UBH, ucpi->c_freeoff, length,
  594. (uspi->s_fpb == 8) ? ufs_fragtable_8fpb : ufs_fragtable_other,
  595. 1 << (count - 1 + (uspi->s_fpb & 7)));
  596. if (location == 0) {
  597. ufs_error (sb, "ufs_bitmap_search",
  598. "bitmap corrupted on cg %u, start %u, length %u, count %u, freeoff %u\n",
  599. ucpi->c_cgx, start, length, count, ucpi->c_freeoff);
  600. return (unsigned)-1;
  601. }
  602. start = 0;
  603. }
  604. result = (start + length - location) << 3;
  605. ucpi->c_frotor = result;
  606. /*
  607. * found the byte in the map
  608. */
  609. blockmap = ubh_blkmap(UCPI_UBH, ucpi->c_freeoff, result);
  610. fragsize = 0;
  611. for (possition = 0, mask = 1; possition < 8; possition++, mask <<= 1) {
  612. if (blockmap & mask) {
  613. if (!(possition & uspi->s_fpbmask))
  614. fragsize = 1;
  615. else
  616. fragsize++;
  617. }
  618. else {
  619. if (fragsize == count) {
  620. result += possition - count;
  621. UFSD(("EXIT, result %u\n", result))
  622. return result;
  623. }
  624. fragsize = 0;
  625. }
  626. }
  627. if (fragsize == count) {
  628. result += possition - count;
  629. UFSD(("EXIT, result %u\n", result))
  630. return result;
  631. }
  632. ufs_error (sb, "ufs_bitmap_search", "block not in map on cg %u\n", ucpi->c_cgx);
  633. UFSD(("EXIT (FAILED)\n"))
  634. return (unsigned)-1;
  635. }
  636. static void ufs_clusteracct(struct super_block * sb,
  637. struct ufs_cg_private_info * ucpi, unsigned blkno, int cnt)
  638. {
  639. struct ufs_sb_private_info * uspi;
  640. int i, start, end, forw, back;
  641. uspi = UFS_SB(sb)->s_uspi;
  642. if (uspi->s_contigsumsize <= 0)
  643. return;
  644. if (cnt > 0)
  645. ubh_setbit(UCPI_UBH, ucpi->c_clusteroff, blkno);
  646. else
  647. ubh_clrbit(UCPI_UBH, ucpi->c_clusteroff, blkno);
  648. /*
  649. * Find the size of the cluster going forward.
  650. */
  651. start = blkno + 1;
  652. end = start + uspi->s_contigsumsize;
  653. if ( end >= ucpi->c_nclusterblks)
  654. end = ucpi->c_nclusterblks;
  655. i = ubh_find_next_zero_bit (UCPI_UBH, ucpi->c_clusteroff, end, start);
  656. if (i > end)
  657. i = end;
  658. forw = i - start;
  659. /*
  660. * Find the size of the cluster going backward.
  661. */
  662. start = blkno - 1;
  663. end = start - uspi->s_contigsumsize;
  664. if (end < 0 )
  665. end = -1;
  666. i = ubh_find_last_zero_bit (UCPI_UBH, ucpi->c_clusteroff, start, end);
  667. if ( i < end)
  668. i = end;
  669. back = start - i;
  670. /*
  671. * Account for old cluster and the possibly new forward and
  672. * back clusters.
  673. */
  674. i = back + forw + 1;
  675. if (i > uspi->s_contigsumsize)
  676. i = uspi->s_contigsumsize;
  677. fs32_add(sb, (__fs32*)ubh_get_addr(UCPI_UBH, ucpi->c_clustersumoff + (i << 2)), cnt);
  678. if (back > 0)
  679. fs32_sub(sb, (__fs32*)ubh_get_addr(UCPI_UBH, ucpi->c_clustersumoff + (back << 2)), cnt);
  680. if (forw > 0)
  681. fs32_sub(sb, (__fs32*)ubh_get_addr(UCPI_UBH, ucpi->c_clustersumoff + (forw << 2)), cnt);
  682. }
  683. static unsigned char ufs_fragtable_8fpb[] = {
  684. 0x00, 0x01, 0x01, 0x02, 0x01, 0x01, 0x02, 0x04, 0x01, 0x01, 0x01, 0x03, 0x02, 0x03, 0x04, 0x08,
  685. 0x01, 0x01, 0x01, 0x03, 0x01, 0x01, 0x03, 0x05, 0x02, 0x03, 0x03, 0x02, 0x04, 0x05, 0x08, 0x10,
  686. 0x01, 0x01, 0x01, 0x03, 0x01, 0x01, 0x03, 0x05, 0x01, 0x01, 0x01, 0x03, 0x03, 0x03, 0x05, 0x09,
  687. 0x02, 0x03, 0x03, 0x02, 0x03, 0x03, 0x02, 0x06, 0x04, 0x05, 0x05, 0x06, 0x08, 0x09, 0x10, 0x20,
  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. 0x02, 0x03, 0x03, 0x02, 0x03, 0x03, 0x02, 0x06, 0x03, 0x03, 0x03, 0x03, 0x02, 0x03, 0x06, 0x0A,
  691. 0x04, 0x05, 0x05, 0x06, 0x05, 0x05, 0x06, 0x04, 0x08, 0x09, 0x09, 0x0A, 0x10, 0x11, 0x20, 0x40,
  692. 0x01, 0x01, 0x01, 0x03, 0x01, 0x01, 0x03, 0x05, 0x01, 0x01, 0x01, 0x03, 0x03, 0x03, 0x05, 0x09,
  693. 0x01, 0x01, 0x01, 0x03, 0x01, 0x01, 0x03, 0x05, 0x03, 0x03, 0x03, 0x03, 0x05, 0x05, 0x09, 0x11,
  694. 0x01, 0x01, 0x01, 0x03, 0x01, 0x01, 0x03, 0x05, 0x01, 0x01, 0x01, 0x03, 0x03, 0x03, 0x05, 0x09,
  695. 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x07, 0x05, 0x05, 0x05, 0x07, 0x09, 0x09, 0x11, 0x21,
  696. 0x02, 0x03, 0x03, 0x02, 0x03, 0x03, 0x02, 0x06, 0x03, 0x03, 0x03, 0x03, 0x02, 0x03, 0x06, 0x0A,
  697. 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x07, 0x02, 0x03, 0x03, 0x02, 0x06, 0x07, 0x0A, 0x12,
  698. 0x04, 0x05, 0x05, 0x06, 0x05, 0x05, 0x06, 0x04, 0x05, 0x05, 0x05, 0x07, 0x06, 0x07, 0x04, 0x0C,
  699. 0x08, 0x09, 0x09, 0x0A, 0x09, 0x09, 0x0A, 0x0C, 0x10, 0x11, 0x11, 0x12, 0x20, 0x21, 0x40, 0x80,
  700. };
  701. static unsigned char ufs_fragtable_other[] = {
  702. 0x00, 0x16, 0x16, 0x2A, 0x16, 0x16, 0x26, 0x4E, 0x16, 0x16, 0x16, 0x3E, 0x2A, 0x3E, 0x4E, 0x8A,
  703. 0x16, 0x16, 0x16, 0x3E, 0x16, 0x16, 0x36, 0x5E, 0x16, 0x16, 0x16, 0x3E, 0x3E, 0x3E, 0x5E, 0x9E,
  704. 0x16, 0x16, 0x16, 0x3E, 0x16, 0x16, 0x36, 0x5E, 0x16, 0x16, 0x16, 0x3E, 0x3E, 0x3E, 0x5E, 0x9E,
  705. 0x2A, 0x3E, 0x3E, 0x2A, 0x3E, 0x3E, 0x2E, 0x6E, 0x3E, 0x3E, 0x3E, 0x3E, 0x2A, 0x3E, 0x6E, 0xAA,
  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. 0x26, 0x36, 0x36, 0x2E, 0x36, 0x36, 0x26, 0x6E, 0x36, 0x36, 0x36, 0x3E, 0x2E, 0x3E, 0x6E, 0xAE,
  709. 0x4E, 0x5E, 0x5E, 0x6E, 0x5E, 0x5E, 0x6E, 0x4E, 0x5E, 0x5E, 0x5E, 0x7E, 0x6E, 0x7E, 0x4E, 0xCE,
  710. 0x16, 0x16, 0x16, 0x3E, 0x16, 0x16, 0x36, 0x5E, 0x16, 0x16, 0x16, 0x3E, 0x3E, 0x3E, 0x5E, 0x9E,
  711. 0x16, 0x16, 0x16, 0x3E, 0x16, 0x16, 0x36, 0x5E, 0x16, 0x16, 0x16, 0x3E, 0x3E, 0x3E, 0x5E, 0x9E,
  712. 0x16, 0x16, 0x16, 0x3E, 0x16, 0x16, 0x36, 0x5E, 0x16, 0x16, 0x16, 0x3E, 0x3E, 0x3E, 0x5E, 0x9E,
  713. 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x7E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x7E, 0xBE,
  714. 0x2A, 0x3E, 0x3E, 0x2A, 0x3E, 0x3E, 0x2E, 0x6E, 0x3E, 0x3E, 0x3E, 0x3E, 0x2A, 0x3E, 0x6E, 0xAA,
  715. 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x7E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x7E, 0xBE,
  716. 0x4E, 0x5E, 0x5E, 0x6E, 0x5E, 0x5E, 0x6E, 0x4E, 0x5E, 0x5E, 0x5E, 0x7E, 0x6E, 0x7E, 0x4E, 0xCE,
  717. 0x8A, 0x9E, 0x9E, 0xAA, 0x9E, 0x9E, 0xAE, 0xCE, 0x9E, 0x9E, 0x9E, 0xBE, 0xAA, 0xBE, 0xCE, 0x8A,
  718. };