balloc.c 24 KB

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