balloc.c 28 KB

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