balloc.c 25 KB

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