balloc.c 26 KB

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