balloc.c 28 KB

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