balloc.c 27 KB

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