balloc.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915
  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 beg,
  199. unsigned int count, unsigned int oldb,
  200. unsigned int newb, struct page *locked_page)
  201. {
  202. const unsigned mask = (1 << (PAGE_CACHE_SHIFT - inode->i_blkbits)) - 1;
  203. struct address_space * const mapping = inode->i_mapping;
  204. pgoff_t index, cur_index;
  205. unsigned end, pos, 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(!locked_page);
  211. BUG_ON(!PageLocked(locked_page));
  212. cur_index = locked_page->index;
  213. for (end = count + beg; beg < end; beg = (beg | mask) + 1) {
  214. index = beg >> (PAGE_CACHE_SHIFT - inode->i_blkbits);
  215. if (likely(cur_index != index)) {
  216. page = ufs_get_locked_page(mapping, index);
  217. if (!page || IS_ERR(page)) /* it was truncated or EIO */
  218. continue;
  219. } else
  220. page = locked_page;
  221. head = page_buffers(page);
  222. bh = head;
  223. pos = beg & mask;
  224. for (j = 0; j < pos; ++j)
  225. bh = bh->b_this_page;
  226. j = 0;
  227. do {
  228. if (buffer_mapped(bh)) {
  229. pos = bh->b_blocknr - oldb;
  230. if (pos < count) {
  231. UFSD(" change from %llu to %llu\n",
  232. (unsigned long long)pos + oldb,
  233. (unsigned long long)pos + newb);
  234. bh->b_blocknr = newb + pos;
  235. unmap_underlying_metadata(bh->b_bdev,
  236. bh->b_blocknr);
  237. mark_buffer_dirty(bh);
  238. ++j;
  239. }
  240. }
  241. bh = bh->b_this_page;
  242. } while (bh != head);
  243. if (j)
  244. set_page_dirty(page);
  245. if (likely(cur_index != index))
  246. ufs_put_locked_page(page);
  247. }
  248. UFSD("EXIT\n");
  249. }
  250. static void ufs_clear_frags(struct inode *inode, sector_t beg, unsigned int n,
  251. int sync)
  252. {
  253. struct buffer_head *bh;
  254. sector_t end = beg + n;
  255. for (; beg < end; ++beg) {
  256. bh = sb_getblk(inode->i_sb, beg);
  257. lock_buffer(bh);
  258. memset(bh->b_data, 0, inode->i_sb->s_blocksize);
  259. set_buffer_uptodate(bh);
  260. mark_buffer_dirty(bh);
  261. unlock_buffer(bh);
  262. if (IS_SYNC(inode) || sync)
  263. sync_dirty_buffer(bh);
  264. brelse(bh);
  265. }
  266. }
  267. unsigned ufs_new_fragments(struct inode * inode, __fs32 * p, unsigned fragment,
  268. unsigned goal, unsigned count, int * err, struct page *locked_page)
  269. {
  270. struct super_block * sb;
  271. struct ufs_sb_private_info * uspi;
  272. struct ufs_super_block_first * usb1;
  273. unsigned cgno, oldcount, newcount, tmp, request, result;
  274. UFSD("ENTER, ino %lu, fragment %u, goal %u, count %u\n", inode->i_ino, fragment, goal, count);
  275. sb = inode->i_sb;
  276. uspi = UFS_SB(sb)->s_uspi;
  277. usb1 = ubh_get_usb_first(uspi);
  278. *err = -ENOSPC;
  279. lock_super (sb);
  280. tmp = fs32_to_cpu(sb, *p);
  281. if (count + ufs_fragnum(fragment) > uspi->s_fpb) {
  282. ufs_warning (sb, "ufs_new_fragments", "internal warning"
  283. " fragment %u, count %u", fragment, count);
  284. count = uspi->s_fpb - ufs_fragnum(fragment);
  285. }
  286. oldcount = ufs_fragnum (fragment);
  287. newcount = oldcount + count;
  288. /*
  289. * Somebody else has just allocated our fragments
  290. */
  291. if (oldcount) {
  292. if (!tmp) {
  293. ufs_error (sb, "ufs_new_fragments", "internal error, "
  294. "fragment %u, tmp %u\n", fragment, tmp);
  295. unlock_super (sb);
  296. return (unsigned)-1;
  297. }
  298. if (fragment < UFS_I(inode)->i_lastfrag) {
  299. UFSD("EXIT (ALREADY ALLOCATED)\n");
  300. unlock_super (sb);
  301. return 0;
  302. }
  303. }
  304. else {
  305. if (tmp) {
  306. UFSD("EXIT (ALREADY ALLOCATED)\n");
  307. unlock_super(sb);
  308. return 0;
  309. }
  310. }
  311. /*
  312. * There is not enough space for user on the device
  313. */
  314. if (!capable(CAP_SYS_RESOURCE) && ufs_freespace(uspi, UFS_MINFREE) <= 0) {
  315. unlock_super (sb);
  316. UFSD("EXIT (FAILED)\n");
  317. return 0;
  318. }
  319. if (goal >= uspi->s_size)
  320. goal = 0;
  321. if (goal == 0)
  322. cgno = ufs_inotocg (inode->i_ino);
  323. else
  324. cgno = ufs_dtog (goal);
  325. /*
  326. * allocate new fragment
  327. */
  328. if (oldcount == 0) {
  329. result = ufs_alloc_fragments (inode, cgno, goal, count, err);
  330. if (result) {
  331. *p = cpu_to_fs32(sb, result);
  332. *err = 0;
  333. UFS_I(inode)->i_lastfrag = max_t(u32, UFS_I(inode)->i_lastfrag, fragment + count);
  334. ufs_clear_frags(inode, result + oldcount, newcount - oldcount,
  335. locked_page != NULL);
  336. }
  337. unlock_super(sb);
  338. UFSD("EXIT, result %u\n", result);
  339. return result;
  340. }
  341. /*
  342. * resize block
  343. */
  344. result = ufs_add_fragments (inode, tmp, oldcount, newcount, err);
  345. if (result) {
  346. *err = 0;
  347. UFS_I(inode)->i_lastfrag = max_t(u32, UFS_I(inode)->i_lastfrag, fragment + count);
  348. ufs_clear_frags(inode, result + oldcount, newcount - oldcount,
  349. locked_page != NULL);
  350. unlock_super(sb);
  351. UFSD("EXIT, result %u\n", result);
  352. return result;
  353. }
  354. /*
  355. * allocate new block and move data
  356. */
  357. switch (fs32_to_cpu(sb, usb1->fs_optim)) {
  358. case UFS_OPTSPACE:
  359. request = newcount;
  360. if (uspi->s_minfree < 5 || uspi->cs_total.cs_nffree
  361. > uspi->s_dsize * uspi->s_minfree / (2 * 100))
  362. break;
  363. usb1->fs_optim = cpu_to_fs32(sb, UFS_OPTTIME);
  364. break;
  365. default:
  366. usb1->fs_optim = cpu_to_fs32(sb, UFS_OPTTIME);
  367. case UFS_OPTTIME:
  368. request = uspi->s_fpb;
  369. if (uspi->cs_total.cs_nffree < uspi->s_dsize *
  370. (uspi->s_minfree - 2) / 100)
  371. break;
  372. usb1->fs_optim = cpu_to_fs32(sb, UFS_OPTTIME);
  373. break;
  374. }
  375. result = ufs_alloc_fragments (inode, cgno, goal, request, err);
  376. if (result) {
  377. ufs_clear_frags(inode, result + oldcount, newcount - oldcount,
  378. locked_page != NULL);
  379. ufs_change_blocknr(inode, fragment - oldcount, oldcount, tmp,
  380. result, locked_page);
  381. *p = cpu_to_fs32(sb, result);
  382. *err = 0;
  383. UFS_I(inode)->i_lastfrag = max_t(u32, UFS_I(inode)->i_lastfrag, fragment + count);
  384. unlock_super(sb);
  385. if (newcount < request)
  386. ufs_free_fragments (inode, result + newcount, request - newcount);
  387. ufs_free_fragments (inode, tmp, oldcount);
  388. UFSD("EXIT, result %u\n", result);
  389. return result;
  390. }
  391. unlock_super(sb);
  392. UFSD("EXIT (FAILED)\n");
  393. return 0;
  394. }
  395. static unsigned
  396. ufs_add_fragments (struct inode * inode, unsigned fragment,
  397. unsigned oldcount, unsigned newcount, int * err)
  398. {
  399. struct super_block * sb;
  400. struct ufs_sb_private_info * uspi;
  401. struct ufs_super_block_first * usb1;
  402. struct ufs_cg_private_info * ucpi;
  403. struct ufs_cylinder_group * ucg;
  404. unsigned cgno, fragno, fragoff, count, fragsize, i;
  405. UFSD("ENTER, fragment %u, oldcount %u, newcount %u\n", fragment, oldcount, newcount);
  406. sb = inode->i_sb;
  407. uspi = UFS_SB(sb)->s_uspi;
  408. usb1 = ubh_get_usb_first (uspi);
  409. count = newcount - oldcount;
  410. cgno = ufs_dtog(fragment);
  411. if (fs32_to_cpu(sb, UFS_SB(sb)->fs_cs(cgno).cs_nffree) < count)
  412. return 0;
  413. if ((ufs_fragnum (fragment) + newcount) > uspi->s_fpb)
  414. return 0;
  415. ucpi = ufs_load_cylinder (sb, cgno);
  416. if (!ucpi)
  417. return 0;
  418. ucg = ubh_get_ucg (UCPI_UBH(ucpi));
  419. if (!ufs_cg_chkmagic(sb, ucg)) {
  420. ufs_panic (sb, "ufs_add_fragments",
  421. "internal error, bad magic number on cg %u", cgno);
  422. return 0;
  423. }
  424. fragno = ufs_dtogd (fragment);
  425. fragoff = ufs_fragnum (fragno);
  426. for (i = oldcount; i < newcount; i++)
  427. if (ubh_isclr (UCPI_UBH(ucpi), ucpi->c_freeoff, fragno + i))
  428. return 0;
  429. /*
  430. * Block can be extended
  431. */
  432. ucg->cg_time = cpu_to_fs32(sb, get_seconds());
  433. for (i = newcount; i < (uspi->s_fpb - fragoff); i++)
  434. if (ubh_isclr (UCPI_UBH(ucpi), ucpi->c_freeoff, fragno + i))
  435. break;
  436. fragsize = i - oldcount;
  437. if (!fs32_to_cpu(sb, ucg->cg_frsum[fragsize]))
  438. ufs_panic (sb, "ufs_add_fragments",
  439. "internal error or corrupted bitmap on cg %u", cgno);
  440. fs32_sub(sb, &ucg->cg_frsum[fragsize], 1);
  441. if (fragsize != count)
  442. fs32_add(sb, &ucg->cg_frsum[fragsize - count], 1);
  443. for (i = oldcount; i < newcount; i++)
  444. ubh_clrbit (UCPI_UBH(ucpi), ucpi->c_freeoff, fragno + i);
  445. if(DQUOT_ALLOC_BLOCK(inode, count)) {
  446. *err = -EDQUOT;
  447. return 0;
  448. }
  449. fs32_sub(sb, &ucg->cg_cs.cs_nffree, count);
  450. fs32_sub(sb, &UFS_SB(sb)->fs_cs(cgno).cs_nffree, count);
  451. uspi->cs_total.cs_nffree -= count;
  452. ubh_mark_buffer_dirty (USPI_UBH(uspi));
  453. ubh_mark_buffer_dirty (UCPI_UBH(ucpi));
  454. if (sb->s_flags & MS_SYNCHRONOUS) {
  455. ubh_ll_rw_block(SWRITE, UCPI_UBH(ucpi));
  456. ubh_wait_on_buffer (UCPI_UBH(ucpi));
  457. }
  458. sb->s_dirt = 1;
  459. UFSD("EXIT, fragment %u\n", fragment);
  460. return fragment;
  461. }
  462. #define UFS_TEST_FREE_SPACE_CG \
  463. ucg = (struct ufs_cylinder_group *) UFS_SB(sb)->s_ucg[cgno]->b_data; \
  464. if (fs32_to_cpu(sb, ucg->cg_cs.cs_nbfree)) \
  465. goto cg_found; \
  466. for (k = count; k < uspi->s_fpb; k++) \
  467. if (fs32_to_cpu(sb, ucg->cg_frsum[k])) \
  468. goto cg_found;
  469. static unsigned ufs_alloc_fragments (struct inode * inode, unsigned cgno,
  470. unsigned goal, unsigned count, int * err)
  471. {
  472. struct super_block * sb;
  473. struct ufs_sb_private_info * uspi;
  474. struct ufs_super_block_first * usb1;
  475. struct ufs_cg_private_info * ucpi;
  476. struct ufs_cylinder_group * ucg;
  477. unsigned oldcg, i, j, k, result, allocsize;
  478. UFSD("ENTER, ino %lu, cgno %u, goal %u, count %u\n", inode->i_ino, cgno, goal, count);
  479. sb = inode->i_sb;
  480. uspi = UFS_SB(sb)->s_uspi;
  481. usb1 = ubh_get_usb_first(uspi);
  482. oldcg = cgno;
  483. /*
  484. * 1. searching on preferred cylinder group
  485. */
  486. UFS_TEST_FREE_SPACE_CG
  487. /*
  488. * 2. quadratic rehash
  489. */
  490. for (j = 1; j < uspi->s_ncg; j *= 2) {
  491. cgno += j;
  492. if (cgno >= uspi->s_ncg)
  493. cgno -= uspi->s_ncg;
  494. UFS_TEST_FREE_SPACE_CG
  495. }
  496. /*
  497. * 3. brute force search
  498. * We start at i = 2 ( 0 is checked at 1.step, 1 at 2.step )
  499. */
  500. cgno = (oldcg + 1) % uspi->s_ncg;
  501. for (j = 2; j < uspi->s_ncg; j++) {
  502. cgno++;
  503. if (cgno >= uspi->s_ncg)
  504. cgno = 0;
  505. UFS_TEST_FREE_SPACE_CG
  506. }
  507. UFSD("EXIT (FAILED)\n");
  508. return 0;
  509. cg_found:
  510. ucpi = ufs_load_cylinder (sb, cgno);
  511. if (!ucpi)
  512. return 0;
  513. ucg = ubh_get_ucg (UCPI_UBH(ucpi));
  514. if (!ufs_cg_chkmagic(sb, ucg))
  515. ufs_panic (sb, "ufs_alloc_fragments",
  516. "internal error, bad magic number on cg %u", cgno);
  517. ucg->cg_time = cpu_to_fs32(sb, get_seconds());
  518. if (count == uspi->s_fpb) {
  519. result = ufs_alloccg_block (inode, ucpi, goal, err);
  520. if (result == (unsigned)-1)
  521. return 0;
  522. goto succed;
  523. }
  524. for (allocsize = count; allocsize < uspi->s_fpb; allocsize++)
  525. if (fs32_to_cpu(sb, ucg->cg_frsum[allocsize]) != 0)
  526. break;
  527. if (allocsize == uspi->s_fpb) {
  528. result = ufs_alloccg_block (inode, ucpi, goal, err);
  529. if (result == (unsigned)-1)
  530. return 0;
  531. goal = ufs_dtogd (result);
  532. for (i = count; i < uspi->s_fpb; i++)
  533. ubh_setbit (UCPI_UBH(ucpi), ucpi->c_freeoff, goal + i);
  534. i = uspi->s_fpb - count;
  535. DQUOT_FREE_BLOCK(inode, i);
  536. fs32_add(sb, &ucg->cg_cs.cs_nffree, i);
  537. uspi->cs_total.cs_nffree += i;
  538. fs32_add(sb, &UFS_SB(sb)->fs_cs(cgno).cs_nffree, i);
  539. fs32_add(sb, &ucg->cg_frsum[i], 1);
  540. goto succed;
  541. }
  542. result = ufs_bitmap_search (sb, ucpi, goal, allocsize);
  543. if (result == (unsigned)-1)
  544. return 0;
  545. if(DQUOT_ALLOC_BLOCK(inode, count)) {
  546. *err = -EDQUOT;
  547. return 0;
  548. }
  549. for (i = 0; i < count; i++)
  550. ubh_clrbit (UCPI_UBH(ucpi), ucpi->c_freeoff, result + i);
  551. fs32_sub(sb, &ucg->cg_cs.cs_nffree, count);
  552. uspi->cs_total.cs_nffree -= count;
  553. fs32_sub(sb, &UFS_SB(sb)->fs_cs(cgno).cs_nffree, count);
  554. fs32_sub(sb, &ucg->cg_frsum[allocsize], 1);
  555. if (count != allocsize)
  556. fs32_add(sb, &ucg->cg_frsum[allocsize - count], 1);
  557. succed:
  558. ubh_mark_buffer_dirty (USPI_UBH(uspi));
  559. ubh_mark_buffer_dirty (UCPI_UBH(ucpi));
  560. if (sb->s_flags & MS_SYNCHRONOUS) {
  561. ubh_ll_rw_block(SWRITE, UCPI_UBH(ucpi));
  562. ubh_wait_on_buffer (UCPI_UBH(ucpi));
  563. }
  564. sb->s_dirt = 1;
  565. result += cgno * uspi->s_fpg;
  566. UFSD("EXIT3, result %u\n", result);
  567. return result;
  568. }
  569. static unsigned ufs_alloccg_block (struct inode * inode,
  570. struct ufs_cg_private_info * ucpi, unsigned goal, int * err)
  571. {
  572. struct super_block * sb;
  573. struct ufs_sb_private_info * uspi;
  574. struct ufs_super_block_first * usb1;
  575. struct ufs_cylinder_group * ucg;
  576. unsigned result, cylno, blkno;
  577. UFSD("ENTER, goal %u\n", goal);
  578. sb = inode->i_sb;
  579. uspi = UFS_SB(sb)->s_uspi;
  580. usb1 = ubh_get_usb_first(uspi);
  581. ucg = ubh_get_ucg(UCPI_UBH(ucpi));
  582. if (goal == 0) {
  583. goal = ucpi->c_rotor;
  584. goto norot;
  585. }
  586. goal = ufs_blknum (goal);
  587. goal = ufs_dtogd (goal);
  588. /*
  589. * If the requested block is available, use it.
  590. */
  591. if (ubh_isblockset(UCPI_UBH(ucpi), ucpi->c_freeoff, ufs_fragstoblks(goal))) {
  592. result = goal;
  593. goto gotit;
  594. }
  595. norot:
  596. result = ufs_bitmap_search (sb, ucpi, goal, uspi->s_fpb);
  597. if (result == (unsigned)-1)
  598. return (unsigned)-1;
  599. ucpi->c_rotor = result;
  600. gotit:
  601. blkno = ufs_fragstoblks(result);
  602. ubh_clrblock (UCPI_UBH(ucpi), ucpi->c_freeoff, blkno);
  603. if ((UFS_SB(sb)->s_flags & UFS_CG_MASK) == UFS_CG_44BSD)
  604. ufs_clusteracct (sb, ucpi, blkno, -1);
  605. if(DQUOT_ALLOC_BLOCK(inode, uspi->s_fpb)) {
  606. *err = -EDQUOT;
  607. return (unsigned)-1;
  608. }
  609. fs32_sub(sb, &ucg->cg_cs.cs_nbfree, 1);
  610. uspi->cs_total.cs_nbfree--;
  611. fs32_sub(sb, &UFS_SB(sb)->fs_cs(ucpi->c_cgx).cs_nbfree, 1);
  612. cylno = ufs_cbtocylno(result);
  613. fs16_sub(sb, &ubh_cg_blks(ucpi, cylno, ufs_cbtorpos(result)), 1);
  614. fs32_sub(sb, &ubh_cg_blktot(ucpi, cylno), 1);
  615. UFSD("EXIT, result %u\n", result);
  616. return result;
  617. }
  618. static unsigned ubh_scanc(struct ufs_sb_private_info *uspi,
  619. struct ufs_buffer_head *ubh,
  620. unsigned begin, unsigned size,
  621. unsigned char *table, unsigned char mask)
  622. {
  623. unsigned rest, offset;
  624. unsigned char *cp;
  625. offset = begin & ~uspi->s_fmask;
  626. begin >>= uspi->s_fshift;
  627. for (;;) {
  628. if ((offset + size) < uspi->s_fsize)
  629. rest = size;
  630. else
  631. rest = uspi->s_fsize - offset;
  632. size -= rest;
  633. cp = ubh->bh[begin]->b_data + offset;
  634. while ((table[*cp++] & mask) == 0 && --rest)
  635. ;
  636. if (rest || !size)
  637. break;
  638. begin++;
  639. offset = 0;
  640. }
  641. return (size + rest);
  642. }
  643. /*
  644. * Find a block of the specified size in the specified cylinder group.
  645. * @sp: pointer to super block
  646. * @ucpi: pointer to cylinder group info
  647. * @goal: near which block we want find new one
  648. * @count: specified size
  649. */
  650. static unsigned ufs_bitmap_search(struct super_block *sb,
  651. struct ufs_cg_private_info *ucpi,
  652. unsigned goal, unsigned count)
  653. {
  654. /*
  655. * Bit patterns for identifying fragments in the block map
  656. * used as ((map & mask_arr) == want_arr)
  657. */
  658. static const int mask_arr[9] = {
  659. 0x3, 0x7, 0xf, 0x1f, 0x3f, 0x7f, 0xff, 0x1ff, 0x3ff
  660. };
  661. static const int want_arr[9] = {
  662. 0x0, 0x2, 0x6, 0xe, 0x1e, 0x3e, 0x7e, 0xfe, 0x1fe
  663. };
  664. struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
  665. struct ufs_super_block_first *usb1;
  666. struct ufs_cylinder_group *ucg;
  667. unsigned start, length, loc, result;
  668. unsigned pos, want, blockmap, mask, end;
  669. UFSD("ENTER, cg %u, goal %u, count %u\n", ucpi->c_cgx, goal, count);
  670. usb1 = ubh_get_usb_first (uspi);
  671. ucg = ubh_get_ucg(UCPI_UBH(ucpi));
  672. if (goal)
  673. start = ufs_dtogd(goal) >> 3;
  674. else
  675. start = ucpi->c_frotor >> 3;
  676. length = ((uspi->s_fpg + 7) >> 3) - start;
  677. loc = ubh_scanc(uspi, UCPI_UBH(ucpi), ucpi->c_freeoff + start, length,
  678. (uspi->s_fpb == 8) ? ufs_fragtable_8fpb : ufs_fragtable_other,
  679. 1 << (count - 1 + (uspi->s_fpb & 7)));
  680. if (loc == 0) {
  681. length = start + 1;
  682. loc = ubh_scanc(uspi, UCPI_UBH(ucpi), ucpi->c_freeoff, length,
  683. (uspi->s_fpb == 8) ? ufs_fragtable_8fpb :
  684. ufs_fragtable_other,
  685. 1 << (count - 1 + (uspi->s_fpb & 7)));
  686. if (loc == 0) {
  687. ufs_error(sb, "ufs_bitmap_search",
  688. "bitmap corrupted on cg %u, start %u,"
  689. " length %u, count %u, freeoff %u\n",
  690. ucpi->c_cgx, start, length, count,
  691. ucpi->c_freeoff);
  692. return (unsigned)-1;
  693. }
  694. start = 0;
  695. }
  696. result = (start + length - loc) << 3;
  697. ucpi->c_frotor = result;
  698. /*
  699. * found the byte in the map
  700. */
  701. for (end = result + 8; result < end; result += uspi->s_fpb) {
  702. blockmap = ubh_blkmap(UCPI_UBH(ucpi), ucpi->c_freeoff, result);
  703. blockmap <<= 1;
  704. mask = mask_arr[count];
  705. want = want_arr[count];
  706. for (pos = 0; pos <= uspi->s_fpb - count; pos++) {
  707. if ((blockmap & mask) == want) {
  708. UFSD("EXIT, result %u\n", result);
  709. return result + pos;
  710. }
  711. mask <<= 1;
  712. want <<= 1;
  713. }
  714. }
  715. ufs_error(sb, "ufs_bitmap_search", "block not in map on cg %u\n",
  716. ucpi->c_cgx);
  717. UFSD("EXIT (FAILED)\n");
  718. return (unsigned)-1;
  719. }
  720. static void ufs_clusteracct(struct super_block * sb,
  721. struct ufs_cg_private_info * ucpi, unsigned blkno, int cnt)
  722. {
  723. struct ufs_sb_private_info * uspi;
  724. int i, start, end, forw, back;
  725. uspi = UFS_SB(sb)->s_uspi;
  726. if (uspi->s_contigsumsize <= 0)
  727. return;
  728. if (cnt > 0)
  729. ubh_setbit(UCPI_UBH(ucpi), ucpi->c_clusteroff, blkno);
  730. else
  731. ubh_clrbit(UCPI_UBH(ucpi), ucpi->c_clusteroff, blkno);
  732. /*
  733. * Find the size of the cluster going forward.
  734. */
  735. start = blkno + 1;
  736. end = start + uspi->s_contigsumsize;
  737. if ( end >= ucpi->c_nclusterblks)
  738. end = ucpi->c_nclusterblks;
  739. i = ubh_find_next_zero_bit (UCPI_UBH(ucpi), ucpi->c_clusteroff, end, start);
  740. if (i > end)
  741. i = end;
  742. forw = i - start;
  743. /*
  744. * Find the size of the cluster going backward.
  745. */
  746. start = blkno - 1;
  747. end = start - uspi->s_contigsumsize;
  748. if (end < 0 )
  749. end = -1;
  750. i = ubh_find_last_zero_bit (UCPI_UBH(ucpi), ucpi->c_clusteroff, start, end);
  751. if ( i < end)
  752. i = end;
  753. back = start - i;
  754. /*
  755. * Account for old cluster and the possibly new forward and
  756. * back clusters.
  757. */
  758. i = back + forw + 1;
  759. if (i > uspi->s_contigsumsize)
  760. i = uspi->s_contigsumsize;
  761. fs32_add(sb, (__fs32*)ubh_get_addr(UCPI_UBH(ucpi), ucpi->c_clustersumoff + (i << 2)), cnt);
  762. if (back > 0)
  763. fs32_sub(sb, (__fs32*)ubh_get_addr(UCPI_UBH(ucpi), ucpi->c_clustersumoff + (back << 2)), cnt);
  764. if (forw > 0)
  765. fs32_sub(sb, (__fs32*)ubh_get_addr(UCPI_UBH(ucpi), ucpi->c_clustersumoff + (forw << 2)), cnt);
  766. }
  767. static unsigned char ufs_fragtable_8fpb[] = {
  768. 0x00, 0x01, 0x01, 0x02, 0x01, 0x01, 0x02, 0x04, 0x01, 0x01, 0x01, 0x03, 0x02, 0x03, 0x04, 0x08,
  769. 0x01, 0x01, 0x01, 0x03, 0x01, 0x01, 0x03, 0x05, 0x02, 0x03, 0x03, 0x02, 0x04, 0x05, 0x08, 0x10,
  770. 0x01, 0x01, 0x01, 0x03, 0x01, 0x01, 0x03, 0x05, 0x01, 0x01, 0x01, 0x03, 0x03, 0x03, 0x05, 0x09,
  771. 0x02, 0x03, 0x03, 0x02, 0x03, 0x03, 0x02, 0x06, 0x04, 0x05, 0x05, 0x06, 0x08, 0x09, 0x10, 0x20,
  772. 0x01, 0x01, 0x01, 0x03, 0x01, 0x01, 0x03, 0x05, 0x01, 0x01, 0x01, 0x03, 0x03, 0x03, 0x05, 0x09,
  773. 0x01, 0x01, 0x01, 0x03, 0x01, 0x01, 0x03, 0x05, 0x03, 0x03, 0x03, 0x03, 0x05, 0x05, 0x09, 0x11,
  774. 0x02, 0x03, 0x03, 0x02, 0x03, 0x03, 0x02, 0x06, 0x03, 0x03, 0x03, 0x03, 0x02, 0x03, 0x06, 0x0A,
  775. 0x04, 0x05, 0x05, 0x06, 0x05, 0x05, 0x06, 0x04, 0x08, 0x09, 0x09, 0x0A, 0x10, 0x11, 0x20, 0x40,
  776. 0x01, 0x01, 0x01, 0x03, 0x01, 0x01, 0x03, 0x05, 0x01, 0x01, 0x01, 0x03, 0x03, 0x03, 0x05, 0x09,
  777. 0x01, 0x01, 0x01, 0x03, 0x01, 0x01, 0x03, 0x05, 0x03, 0x03, 0x03, 0x03, 0x05, 0x05, 0x09, 0x11,
  778. 0x01, 0x01, 0x01, 0x03, 0x01, 0x01, 0x03, 0x05, 0x01, 0x01, 0x01, 0x03, 0x03, 0x03, 0x05, 0x09,
  779. 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x07, 0x05, 0x05, 0x05, 0x07, 0x09, 0x09, 0x11, 0x21,
  780. 0x02, 0x03, 0x03, 0x02, 0x03, 0x03, 0x02, 0x06, 0x03, 0x03, 0x03, 0x03, 0x02, 0x03, 0x06, 0x0A,
  781. 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x07, 0x02, 0x03, 0x03, 0x02, 0x06, 0x07, 0x0A, 0x12,
  782. 0x04, 0x05, 0x05, 0x06, 0x05, 0x05, 0x06, 0x04, 0x05, 0x05, 0x05, 0x07, 0x06, 0x07, 0x04, 0x0C,
  783. 0x08, 0x09, 0x09, 0x0A, 0x09, 0x09, 0x0A, 0x0C, 0x10, 0x11, 0x11, 0x12, 0x20, 0x21, 0x40, 0x80,
  784. };
  785. static unsigned char ufs_fragtable_other[] = {
  786. 0x00, 0x16, 0x16, 0x2A, 0x16, 0x16, 0x26, 0x4E, 0x16, 0x16, 0x16, 0x3E, 0x2A, 0x3E, 0x4E, 0x8A,
  787. 0x16, 0x16, 0x16, 0x3E, 0x16, 0x16, 0x36, 0x5E, 0x16, 0x16, 0x16, 0x3E, 0x3E, 0x3E, 0x5E, 0x9E,
  788. 0x16, 0x16, 0x16, 0x3E, 0x16, 0x16, 0x36, 0x5E, 0x16, 0x16, 0x16, 0x3E, 0x3E, 0x3E, 0x5E, 0x9E,
  789. 0x2A, 0x3E, 0x3E, 0x2A, 0x3E, 0x3E, 0x2E, 0x6E, 0x3E, 0x3E, 0x3E, 0x3E, 0x2A, 0x3E, 0x6E, 0xAA,
  790. 0x16, 0x16, 0x16, 0x3E, 0x16, 0x16, 0x36, 0x5E, 0x16, 0x16, 0x16, 0x3E, 0x3E, 0x3E, 0x5E, 0x9E,
  791. 0x16, 0x16, 0x16, 0x3E, 0x16, 0x16, 0x36, 0x5E, 0x16, 0x16, 0x16, 0x3E, 0x3E, 0x3E, 0x5E, 0x9E,
  792. 0x26, 0x36, 0x36, 0x2E, 0x36, 0x36, 0x26, 0x6E, 0x36, 0x36, 0x36, 0x3E, 0x2E, 0x3E, 0x6E, 0xAE,
  793. 0x4E, 0x5E, 0x5E, 0x6E, 0x5E, 0x5E, 0x6E, 0x4E, 0x5E, 0x5E, 0x5E, 0x7E, 0x6E, 0x7E, 0x4E, 0xCE,
  794. 0x16, 0x16, 0x16, 0x3E, 0x16, 0x16, 0x36, 0x5E, 0x16, 0x16, 0x16, 0x3E, 0x3E, 0x3E, 0x5E, 0x9E,
  795. 0x16, 0x16, 0x16, 0x3E, 0x16, 0x16, 0x36, 0x5E, 0x16, 0x16, 0x16, 0x3E, 0x3E, 0x3E, 0x5E, 0x9E,
  796. 0x16, 0x16, 0x16, 0x3E, 0x16, 0x16, 0x36, 0x5E, 0x16, 0x16, 0x16, 0x3E, 0x3E, 0x3E, 0x5E, 0x9E,
  797. 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x7E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x7E, 0xBE,
  798. 0x2A, 0x3E, 0x3E, 0x2A, 0x3E, 0x3E, 0x2E, 0x6E, 0x3E, 0x3E, 0x3E, 0x3E, 0x2A, 0x3E, 0x6E, 0xAA,
  799. 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x7E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x7E, 0xBE,
  800. 0x4E, 0x5E, 0x5E, 0x6E, 0x5E, 0x5E, 0x6E, 0x4E, 0x5E, 0x5E, 0x5E, 0x7E, 0x6E, 0x7E, 0x4E, 0xCE,
  801. 0x8A, 0x9E, 0x9E, 0xAA, 0x9E, 0x9E, 0xAE, 0xCE, 0x9E, 0x9E, 0x9E, 0xBE, 0xAA, 0xBE, 0xCE, 0x8A,
  802. };