balloc.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  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/sched.h>
  19. #include <linux/bitops.h>
  20. #include <asm/byteorder.h>
  21. #include "swab.h"
  22. #include "util.h"
  23. #define INVBLOCK ((u64)-1L)
  24. static u64 ufs_add_fragments(struct inode *, u64, unsigned, unsigned, int *);
  25. static u64 ufs_alloc_fragments(struct inode *, unsigned, u64, unsigned, int *);
  26. static u64 ufs_alloccg_block(struct inode *, struct ufs_cg_private_info *, u64, int *);
  27. static u64 ufs_bitmap_search (struct super_block *, struct ufs_cg_private_info *, u64, unsigned);
  28. static unsigned char ufs_fragtable_8fpb[], ufs_fragtable_other[];
  29. static void ufs_clusteracct(struct super_block *, struct ufs_cg_private_info *, unsigned, int);
  30. /*
  31. * Free 'count' fragments from fragment number 'fragment'
  32. */
  33. void ufs_free_fragments(struct inode *inode, u64 fragment, unsigned count)
  34. {
  35. struct super_block * sb;
  36. struct ufs_sb_private_info * uspi;
  37. struct ufs_super_block_first * usb1;
  38. struct ufs_cg_private_info * ucpi;
  39. struct ufs_cylinder_group * ucg;
  40. unsigned cgno, bit, end_bit, bbase, blkmap, i;
  41. u64 blkno;
  42. sb = inode->i_sb;
  43. uspi = UFS_SB(sb)->s_uspi;
  44. usb1 = ubh_get_usb_first(uspi);
  45. UFSD("ENTER, fragment %llu, count %u\n",
  46. (unsigned long long)fragment, count);
  47. if (ufs_fragnum(fragment) + count > uspi->s_fpg)
  48. ufs_error (sb, "ufs_free_fragments", "internal error");
  49. lock_super(sb);
  50. cgno = ufs_dtog(uspi, fragment);
  51. bit = ufs_dtogd(uspi, fragment);
  52. if (cgno >= uspi->s_ncg) {
  53. ufs_panic (sb, "ufs_free_fragments", "freeing blocks are outside device");
  54. goto failed;
  55. }
  56. ucpi = ufs_load_cylinder (sb, cgno);
  57. if (!ucpi)
  58. goto failed;
  59. ucg = ubh_get_ucg (UCPI_UBH(ucpi));
  60. if (!ufs_cg_chkmagic(sb, ucg)) {
  61. ufs_panic (sb, "ufs_free_fragments", "internal error, bad magic number on cg %u", cgno);
  62. goto failed;
  63. }
  64. end_bit = bit + count;
  65. bbase = ufs_blknum (bit);
  66. blkmap = ubh_blkmap (UCPI_UBH(ucpi), ucpi->c_freeoff, bbase);
  67. ufs_fragacct (sb, blkmap, ucg->cg_frsum, -1);
  68. for (i = bit; i < end_bit; i++) {
  69. if (ubh_isclr (UCPI_UBH(ucpi), ucpi->c_freeoff, i))
  70. ubh_setbit (UCPI_UBH(ucpi), ucpi->c_freeoff, i);
  71. else
  72. ufs_error (sb, "ufs_free_fragments",
  73. "bit already cleared for fragment %u", i);
  74. }
  75. DQUOT_FREE_BLOCK (inode, count);
  76. fs32_add(sb, &ucg->cg_cs.cs_nffree, count);
  77. uspi->cs_total.cs_nffree += count;
  78. fs32_add(sb, &UFS_SB(sb)->fs_cs(cgno).cs_nffree, count);
  79. blkmap = ubh_blkmap (UCPI_UBH(ucpi), ucpi->c_freeoff, bbase);
  80. ufs_fragacct(sb, blkmap, ucg->cg_frsum, 1);
  81. /*
  82. * Trying to reassemble free fragments into block
  83. */
  84. blkno = ufs_fragstoblks (bbase);
  85. if (ubh_isblockset(UCPI_UBH(ucpi), ucpi->c_freeoff, blkno)) {
  86. fs32_sub(sb, &ucg->cg_cs.cs_nffree, uspi->s_fpb);
  87. uspi->cs_total.cs_nffree -= uspi->s_fpb;
  88. fs32_sub(sb, &UFS_SB(sb)->fs_cs(cgno).cs_nffree, uspi->s_fpb);
  89. if ((UFS_SB(sb)->s_flags & UFS_CG_MASK) == UFS_CG_44BSD)
  90. ufs_clusteracct (sb, ucpi, blkno, 1);
  91. fs32_add(sb, &ucg->cg_cs.cs_nbfree, 1);
  92. uspi->cs_total.cs_nbfree++;
  93. fs32_add(sb, &UFS_SB(sb)->fs_cs(cgno).cs_nbfree, 1);
  94. if (uspi->fs_magic != UFS2_MAGIC) {
  95. unsigned cylno = ufs_cbtocylno (bbase);
  96. fs16_add(sb, &ubh_cg_blks(ucpi, cylno,
  97. ufs_cbtorpos(bbase)), 1);
  98. fs32_add(sb, &ubh_cg_blktot(ucpi, cylno), 1);
  99. }
  100. }
  101. ubh_mark_buffer_dirty (USPI_UBH(uspi));
  102. ubh_mark_buffer_dirty (UCPI_UBH(ucpi));
  103. if (sb->s_flags & MS_SYNCHRONOUS) {
  104. ubh_ll_rw_block(SWRITE, UCPI_UBH(ucpi));
  105. ubh_wait_on_buffer (UCPI_UBH(ucpi));
  106. }
  107. sb->s_dirt = 1;
  108. unlock_super (sb);
  109. UFSD("EXIT\n");
  110. return;
  111. failed:
  112. unlock_super (sb);
  113. UFSD("EXIT (FAILED)\n");
  114. return;
  115. }
  116. /*
  117. * Free 'count' fragments from fragment number 'fragment' (free whole blocks)
  118. */
  119. void ufs_free_blocks(struct inode *inode, u64 fragment, unsigned count)
  120. {
  121. struct super_block * sb;
  122. struct ufs_sb_private_info * uspi;
  123. struct ufs_super_block_first * usb1;
  124. struct ufs_cg_private_info * ucpi;
  125. struct ufs_cylinder_group * ucg;
  126. unsigned overflow, cgno, bit, end_bit, i;
  127. u64 blkno;
  128. sb = inode->i_sb;
  129. uspi = UFS_SB(sb)->s_uspi;
  130. usb1 = ubh_get_usb_first(uspi);
  131. UFSD("ENTER, fragment %llu, count %u\n",
  132. (unsigned long long)fragment, count);
  133. if ((fragment & uspi->s_fpbmask) || (count & uspi->s_fpbmask)) {
  134. ufs_error (sb, "ufs_free_blocks", "internal error, "
  135. "fragment %llu, count %u\n",
  136. (unsigned long long)fragment, count);
  137. goto failed;
  138. }
  139. lock_super(sb);
  140. do_more:
  141. overflow = 0;
  142. cgno = ufs_dtog(uspi, fragment);
  143. bit = ufs_dtogd(uspi, fragment);
  144. if (cgno >= uspi->s_ncg) {
  145. ufs_panic (sb, "ufs_free_blocks", "freeing blocks are outside device");
  146. goto failed_unlock;
  147. }
  148. end_bit = bit + count;
  149. if (end_bit > uspi->s_fpg) {
  150. overflow = bit + count - uspi->s_fpg;
  151. count -= overflow;
  152. end_bit -= overflow;
  153. }
  154. ucpi = ufs_load_cylinder (sb, cgno);
  155. if (!ucpi)
  156. goto failed_unlock;
  157. ucg = ubh_get_ucg (UCPI_UBH(ucpi));
  158. if (!ufs_cg_chkmagic(sb, ucg)) {
  159. ufs_panic (sb, "ufs_free_blocks", "internal error, bad magic number on cg %u", cgno);
  160. goto failed_unlock;
  161. }
  162. for (i = bit; i < end_bit; i += uspi->s_fpb) {
  163. blkno = ufs_fragstoblks(i);
  164. if (ubh_isblockset(UCPI_UBH(ucpi), ucpi->c_freeoff, blkno)) {
  165. ufs_error(sb, "ufs_free_blocks", "freeing free fragment");
  166. }
  167. ubh_setblock(UCPI_UBH(ucpi), ucpi->c_freeoff, blkno);
  168. if ((UFS_SB(sb)->s_flags & UFS_CG_MASK) == UFS_CG_44BSD)
  169. ufs_clusteracct (sb, ucpi, blkno, 1);
  170. DQUOT_FREE_BLOCK(inode, uspi->s_fpb);
  171. fs32_add(sb, &ucg->cg_cs.cs_nbfree, 1);
  172. uspi->cs_total.cs_nbfree++;
  173. fs32_add(sb, &UFS_SB(sb)->fs_cs(cgno).cs_nbfree, 1);
  174. if (uspi->fs_magic != UFS2_MAGIC) {
  175. unsigned cylno = ufs_cbtocylno(i);
  176. fs16_add(sb, &ubh_cg_blks(ucpi, cylno,
  177. ufs_cbtorpos(i)), 1);
  178. fs32_add(sb, &ubh_cg_blktot(ucpi, cylno), 1);
  179. }
  180. }
  181. ubh_mark_buffer_dirty (USPI_UBH(uspi));
  182. ubh_mark_buffer_dirty (UCPI_UBH(ucpi));
  183. if (sb->s_flags & MS_SYNCHRONOUS) {
  184. ubh_ll_rw_block(SWRITE, UCPI_UBH(ucpi));
  185. ubh_wait_on_buffer (UCPI_UBH(ucpi));
  186. }
  187. if (overflow) {
  188. fragment += count;
  189. count = overflow;
  190. goto do_more;
  191. }
  192. sb->s_dirt = 1;
  193. unlock_super (sb);
  194. UFSD("EXIT\n");
  195. return;
  196. failed_unlock:
  197. unlock_super (sb);
  198. failed:
  199. UFSD("EXIT (FAILED)\n");
  200. return;
  201. }
  202. /*
  203. * Modify inode page cache in such way:
  204. * have - blocks with b_blocknr equal to oldb...oldb+count-1
  205. * get - blocks with b_blocknr equal to newb...newb+count-1
  206. * also we suppose that oldb...oldb+count-1 blocks
  207. * situated at the end of file.
  208. *
  209. * We can come here from ufs_writepage or ufs_prepare_write,
  210. * locked_page is argument of these functions, so we already lock it.
  211. */
  212. static void ufs_change_blocknr(struct inode *inode, unsigned int beg,
  213. unsigned int count, unsigned int oldb,
  214. unsigned int newb, struct page *locked_page)
  215. {
  216. const unsigned mask = (1 << (PAGE_CACHE_SHIFT - inode->i_blkbits)) - 1;
  217. struct address_space * const mapping = inode->i_mapping;
  218. pgoff_t index, cur_index;
  219. unsigned end, pos, j;
  220. struct page *page;
  221. struct buffer_head *head, *bh;
  222. UFSD("ENTER, ino %lu, count %u, oldb %u, newb %u\n",
  223. inode->i_ino, count, oldb, newb);
  224. BUG_ON(!locked_page);
  225. BUG_ON(!PageLocked(locked_page));
  226. cur_index = locked_page->index;
  227. for (end = count + beg; beg < end; beg = (beg | mask) + 1) {
  228. index = beg >> (PAGE_CACHE_SHIFT - inode->i_blkbits);
  229. if (likely(cur_index != index)) {
  230. page = ufs_get_locked_page(mapping, index);
  231. if (!page || IS_ERR(page)) /* it was truncated or EIO */
  232. continue;
  233. } else
  234. page = locked_page;
  235. head = page_buffers(page);
  236. bh = head;
  237. pos = beg & mask;
  238. for (j = 0; j < pos; ++j)
  239. bh = bh->b_this_page;
  240. j = 0;
  241. do {
  242. if (buffer_mapped(bh)) {
  243. pos = bh->b_blocknr - oldb;
  244. if (pos < count) {
  245. UFSD(" change from %llu to %llu\n",
  246. (unsigned long long)pos + oldb,
  247. (unsigned long long)pos + newb);
  248. bh->b_blocknr = newb + pos;
  249. unmap_underlying_metadata(bh->b_bdev,
  250. bh->b_blocknr);
  251. mark_buffer_dirty(bh);
  252. ++j;
  253. }
  254. }
  255. bh = bh->b_this_page;
  256. } while (bh != head);
  257. if (j)
  258. set_page_dirty(page);
  259. if (likely(cur_index != index))
  260. ufs_put_locked_page(page);
  261. }
  262. UFSD("EXIT\n");
  263. }
  264. static void ufs_clear_frags(struct inode *inode, sector_t beg, unsigned int n,
  265. int sync)
  266. {
  267. struct buffer_head *bh;
  268. sector_t end = beg + n;
  269. for (; beg < end; ++beg) {
  270. bh = sb_getblk(inode->i_sb, beg);
  271. lock_buffer(bh);
  272. memset(bh->b_data, 0, inode->i_sb->s_blocksize);
  273. set_buffer_uptodate(bh);
  274. mark_buffer_dirty(bh);
  275. unlock_buffer(bh);
  276. if (IS_SYNC(inode) || sync)
  277. sync_dirty_buffer(bh);
  278. brelse(bh);
  279. }
  280. }
  281. u64 ufs_new_fragments(struct inode *inode, void *p, u64 fragment,
  282. u64 goal, unsigned count, int *err,
  283. struct page *locked_page)
  284. {
  285. struct super_block * sb;
  286. struct ufs_sb_private_info * uspi;
  287. struct ufs_super_block_first * usb1;
  288. unsigned cgno, oldcount, newcount;
  289. u64 tmp, request, result;
  290. UFSD("ENTER, ino %lu, fragment %llu, goal %llu, count %u\n",
  291. inode->i_ino, (unsigned long long)fragment,
  292. (unsigned long long)goal, count);
  293. sb = inode->i_sb;
  294. uspi = UFS_SB(sb)->s_uspi;
  295. usb1 = ubh_get_usb_first(uspi);
  296. *err = -ENOSPC;
  297. lock_super (sb);
  298. tmp = ufs_data_ptr_to_cpu(sb, p);
  299. if (count + ufs_fragnum(fragment) > uspi->s_fpb) {
  300. ufs_warning(sb, "ufs_new_fragments", "internal warning"
  301. " fragment %llu, count %u",
  302. (unsigned long long)fragment, count);
  303. count = uspi->s_fpb - ufs_fragnum(fragment);
  304. }
  305. oldcount = ufs_fragnum (fragment);
  306. newcount = oldcount + count;
  307. /*
  308. * Somebody else has just allocated our fragments
  309. */
  310. if (oldcount) {
  311. if (!tmp) {
  312. ufs_error(sb, "ufs_new_fragments", "internal error, "
  313. "fragment %llu, tmp %llu\n",
  314. (unsigned long long)fragment,
  315. (unsigned long long)tmp);
  316. unlock_super(sb);
  317. return INVBLOCK;
  318. }
  319. if (fragment < UFS_I(inode)->i_lastfrag) {
  320. UFSD("EXIT (ALREADY ALLOCATED)\n");
  321. unlock_super (sb);
  322. return 0;
  323. }
  324. }
  325. else {
  326. if (tmp) {
  327. UFSD("EXIT (ALREADY ALLOCATED)\n");
  328. unlock_super(sb);
  329. return 0;
  330. }
  331. }
  332. /*
  333. * There is not enough space for user on the device
  334. */
  335. if (!capable(CAP_SYS_RESOURCE) && ufs_freespace(uspi, UFS_MINFREE) <= 0) {
  336. unlock_super (sb);
  337. UFSD("EXIT (FAILED)\n");
  338. return 0;
  339. }
  340. if (goal >= uspi->s_size)
  341. goal = 0;
  342. if (goal == 0)
  343. cgno = ufs_inotocg (inode->i_ino);
  344. else
  345. cgno = ufs_dtog(uspi, goal);
  346. /*
  347. * allocate new fragment
  348. */
  349. if (oldcount == 0) {
  350. result = ufs_alloc_fragments (inode, cgno, goal, count, err);
  351. if (result) {
  352. ufs_cpu_to_data_ptr(sb, p, result);
  353. *err = 0;
  354. UFS_I(inode)->i_lastfrag =
  355. max_t(u32, UFS_I(inode)->i_lastfrag,
  356. fragment + count);
  357. ufs_clear_frags(inode, result + oldcount,
  358. newcount - oldcount, locked_page != NULL);
  359. }
  360. unlock_super(sb);
  361. UFSD("EXIT, result %llu\n", (unsigned long long)result);
  362. return result;
  363. }
  364. /*
  365. * resize block
  366. */
  367. result = ufs_add_fragments (inode, tmp, oldcount, newcount, err);
  368. if (result) {
  369. *err = 0;
  370. UFS_I(inode)->i_lastfrag = max_t(u32, UFS_I(inode)->i_lastfrag, fragment + count);
  371. ufs_clear_frags(inode, result + oldcount, newcount - oldcount,
  372. locked_page != NULL);
  373. unlock_super(sb);
  374. UFSD("EXIT, result %llu\n", (unsigned long long)result);
  375. return result;
  376. }
  377. /*
  378. * allocate new block and move data
  379. */
  380. switch (fs32_to_cpu(sb, usb1->fs_optim)) {
  381. case UFS_OPTSPACE:
  382. request = newcount;
  383. if (uspi->s_minfree < 5 || uspi->cs_total.cs_nffree
  384. > uspi->s_dsize * uspi->s_minfree / (2 * 100))
  385. break;
  386. usb1->fs_optim = cpu_to_fs32(sb, UFS_OPTTIME);
  387. break;
  388. default:
  389. usb1->fs_optim = cpu_to_fs32(sb, UFS_OPTTIME);
  390. case UFS_OPTTIME:
  391. request = uspi->s_fpb;
  392. if (uspi->cs_total.cs_nffree < uspi->s_dsize *
  393. (uspi->s_minfree - 2) / 100)
  394. break;
  395. usb1->fs_optim = cpu_to_fs32(sb, UFS_OPTTIME);
  396. break;
  397. }
  398. result = ufs_alloc_fragments (inode, cgno, goal, request, err);
  399. if (result) {
  400. ufs_clear_frags(inode, result + oldcount, newcount - oldcount,
  401. locked_page != NULL);
  402. ufs_change_blocknr(inode, fragment - oldcount, oldcount, tmp,
  403. result, locked_page);
  404. ufs_cpu_to_data_ptr(sb, p, result);
  405. *err = 0;
  406. UFS_I(inode)->i_lastfrag = max_t(u32, UFS_I(inode)->i_lastfrag, fragment + count);
  407. unlock_super(sb);
  408. if (newcount < request)
  409. ufs_free_fragments (inode, result + newcount, request - newcount);
  410. ufs_free_fragments (inode, tmp, oldcount);
  411. UFSD("EXIT, result %llu\n", (unsigned long long)result);
  412. return result;
  413. }
  414. unlock_super(sb);
  415. UFSD("EXIT (FAILED)\n");
  416. return 0;
  417. }
  418. static u64 ufs_add_fragments(struct inode *inode, u64 fragment,
  419. unsigned oldcount, unsigned newcount, int *err)
  420. {
  421. struct super_block * sb;
  422. struct ufs_sb_private_info * uspi;
  423. struct ufs_super_block_first * usb1;
  424. struct ufs_cg_private_info * ucpi;
  425. struct ufs_cylinder_group * ucg;
  426. unsigned cgno, fragno, fragoff, count, fragsize, i;
  427. UFSD("ENTER, fragment %llu, oldcount %u, newcount %u\n",
  428. (unsigned long long)fragment, oldcount, newcount);
  429. sb = inode->i_sb;
  430. uspi = UFS_SB(sb)->s_uspi;
  431. usb1 = ubh_get_usb_first (uspi);
  432. count = newcount - oldcount;
  433. cgno = ufs_dtog(uspi, fragment);
  434. if (fs32_to_cpu(sb, UFS_SB(sb)->fs_cs(cgno).cs_nffree) < count)
  435. return 0;
  436. if ((ufs_fragnum (fragment) + newcount) > uspi->s_fpb)
  437. return 0;
  438. ucpi = ufs_load_cylinder (sb, cgno);
  439. if (!ucpi)
  440. return 0;
  441. ucg = ubh_get_ucg (UCPI_UBH(ucpi));
  442. if (!ufs_cg_chkmagic(sb, ucg)) {
  443. ufs_panic (sb, "ufs_add_fragments",
  444. "internal error, bad magic number on cg %u", cgno);
  445. return 0;
  446. }
  447. fragno = ufs_dtogd(uspi, fragment);
  448. fragoff = ufs_fragnum (fragno);
  449. for (i = oldcount; i < newcount; i++)
  450. if (ubh_isclr (UCPI_UBH(ucpi), ucpi->c_freeoff, fragno + i))
  451. return 0;
  452. /*
  453. * Block can be extended
  454. */
  455. ucg->cg_time = cpu_to_fs32(sb, get_seconds());
  456. for (i = newcount; i < (uspi->s_fpb - fragoff); i++)
  457. if (ubh_isclr (UCPI_UBH(ucpi), ucpi->c_freeoff, fragno + i))
  458. break;
  459. fragsize = i - oldcount;
  460. if (!fs32_to_cpu(sb, ucg->cg_frsum[fragsize]))
  461. ufs_panic (sb, "ufs_add_fragments",
  462. "internal error or corrupted bitmap on cg %u", cgno);
  463. fs32_sub(sb, &ucg->cg_frsum[fragsize], 1);
  464. if (fragsize != count)
  465. fs32_add(sb, &ucg->cg_frsum[fragsize - count], 1);
  466. for (i = oldcount; i < newcount; i++)
  467. ubh_clrbit (UCPI_UBH(ucpi), ucpi->c_freeoff, fragno + i);
  468. if(DQUOT_ALLOC_BLOCK(inode, count)) {
  469. *err = -EDQUOT;
  470. return 0;
  471. }
  472. fs32_sub(sb, &ucg->cg_cs.cs_nffree, count);
  473. fs32_sub(sb, &UFS_SB(sb)->fs_cs(cgno).cs_nffree, count);
  474. uspi->cs_total.cs_nffree -= count;
  475. ubh_mark_buffer_dirty (USPI_UBH(uspi));
  476. ubh_mark_buffer_dirty (UCPI_UBH(ucpi));
  477. if (sb->s_flags & MS_SYNCHRONOUS) {
  478. ubh_ll_rw_block(SWRITE, UCPI_UBH(ucpi));
  479. ubh_wait_on_buffer (UCPI_UBH(ucpi));
  480. }
  481. sb->s_dirt = 1;
  482. UFSD("EXIT, fragment %llu\n", (unsigned long long)fragment);
  483. return fragment;
  484. }
  485. #define UFS_TEST_FREE_SPACE_CG \
  486. ucg = (struct ufs_cylinder_group *) UFS_SB(sb)->s_ucg[cgno]->b_data; \
  487. if (fs32_to_cpu(sb, ucg->cg_cs.cs_nbfree)) \
  488. goto cg_found; \
  489. for (k = count; k < uspi->s_fpb; k++) \
  490. if (fs32_to_cpu(sb, ucg->cg_frsum[k])) \
  491. goto cg_found;
  492. static u64 ufs_alloc_fragments(struct inode *inode, unsigned cgno,
  493. u64 goal, unsigned count, int *err)
  494. {
  495. struct super_block * sb;
  496. struct ufs_sb_private_info * uspi;
  497. struct ufs_super_block_first * usb1;
  498. struct ufs_cg_private_info * ucpi;
  499. struct ufs_cylinder_group * ucg;
  500. unsigned oldcg, i, j, k, allocsize;
  501. u64 result;
  502. UFSD("ENTER, ino %lu, cgno %u, goal %llu, count %u\n",
  503. inode->i_ino, cgno, (unsigned long long)goal, count);
  504. sb = inode->i_sb;
  505. uspi = UFS_SB(sb)->s_uspi;
  506. usb1 = ubh_get_usb_first(uspi);
  507. oldcg = cgno;
  508. /*
  509. * 1. searching on preferred cylinder group
  510. */
  511. UFS_TEST_FREE_SPACE_CG
  512. /*
  513. * 2. quadratic rehash
  514. */
  515. for (j = 1; j < uspi->s_ncg; j *= 2) {
  516. cgno += j;
  517. if (cgno >= uspi->s_ncg)
  518. cgno -= uspi->s_ncg;
  519. UFS_TEST_FREE_SPACE_CG
  520. }
  521. /*
  522. * 3. brute force search
  523. * We start at i = 2 ( 0 is checked at 1.step, 1 at 2.step )
  524. */
  525. cgno = (oldcg + 1) % uspi->s_ncg;
  526. for (j = 2; j < uspi->s_ncg; j++) {
  527. cgno++;
  528. if (cgno >= uspi->s_ncg)
  529. cgno = 0;
  530. UFS_TEST_FREE_SPACE_CG
  531. }
  532. UFSD("EXIT (FAILED)\n");
  533. return 0;
  534. cg_found:
  535. ucpi = ufs_load_cylinder (sb, cgno);
  536. if (!ucpi)
  537. return 0;
  538. ucg = ubh_get_ucg (UCPI_UBH(ucpi));
  539. if (!ufs_cg_chkmagic(sb, ucg))
  540. ufs_panic (sb, "ufs_alloc_fragments",
  541. "internal error, bad magic number on cg %u", cgno);
  542. ucg->cg_time = cpu_to_fs32(sb, get_seconds());
  543. if (count == uspi->s_fpb) {
  544. result = ufs_alloccg_block (inode, ucpi, goal, err);
  545. if (result == INVBLOCK)
  546. return 0;
  547. goto succed;
  548. }
  549. for (allocsize = count; allocsize < uspi->s_fpb; allocsize++)
  550. if (fs32_to_cpu(sb, ucg->cg_frsum[allocsize]) != 0)
  551. break;
  552. if (allocsize == uspi->s_fpb) {
  553. result = ufs_alloccg_block (inode, ucpi, goal, err);
  554. if (result == INVBLOCK)
  555. return 0;
  556. goal = ufs_dtogd(uspi, result);
  557. for (i = count; i < uspi->s_fpb; i++)
  558. ubh_setbit (UCPI_UBH(ucpi), ucpi->c_freeoff, goal + i);
  559. i = uspi->s_fpb - count;
  560. DQUOT_FREE_BLOCK(inode, i);
  561. fs32_add(sb, &ucg->cg_cs.cs_nffree, i);
  562. uspi->cs_total.cs_nffree += i;
  563. fs32_add(sb, &UFS_SB(sb)->fs_cs(cgno).cs_nffree, i);
  564. fs32_add(sb, &ucg->cg_frsum[i], 1);
  565. goto succed;
  566. }
  567. result = ufs_bitmap_search (sb, ucpi, goal, allocsize);
  568. if (result == INVBLOCK)
  569. return 0;
  570. if(DQUOT_ALLOC_BLOCK(inode, count)) {
  571. *err = -EDQUOT;
  572. return 0;
  573. }
  574. for (i = 0; i < count; i++)
  575. ubh_clrbit (UCPI_UBH(ucpi), ucpi->c_freeoff, result + i);
  576. fs32_sub(sb, &ucg->cg_cs.cs_nffree, count);
  577. uspi->cs_total.cs_nffree -= count;
  578. fs32_sub(sb, &UFS_SB(sb)->fs_cs(cgno).cs_nffree, count);
  579. fs32_sub(sb, &ucg->cg_frsum[allocsize], 1);
  580. if (count != allocsize)
  581. fs32_add(sb, &ucg->cg_frsum[allocsize - count], 1);
  582. succed:
  583. ubh_mark_buffer_dirty (USPI_UBH(uspi));
  584. ubh_mark_buffer_dirty (UCPI_UBH(ucpi));
  585. if (sb->s_flags & MS_SYNCHRONOUS) {
  586. ubh_ll_rw_block(SWRITE, UCPI_UBH(ucpi));
  587. ubh_wait_on_buffer (UCPI_UBH(ucpi));
  588. }
  589. sb->s_dirt = 1;
  590. result += cgno * uspi->s_fpg;
  591. UFSD("EXIT3, result %llu\n", (unsigned long long)result);
  592. return result;
  593. }
  594. static u64 ufs_alloccg_block(struct inode *inode,
  595. struct ufs_cg_private_info *ucpi,
  596. u64 goal, int *err)
  597. {
  598. struct super_block * sb;
  599. struct ufs_sb_private_info * uspi;
  600. struct ufs_super_block_first * usb1;
  601. struct ufs_cylinder_group * ucg;
  602. u64 result, blkno;
  603. UFSD("ENTER, goal %llu\n", (unsigned long long)goal);
  604. sb = inode->i_sb;
  605. uspi = UFS_SB(sb)->s_uspi;
  606. usb1 = ubh_get_usb_first(uspi);
  607. ucg = ubh_get_ucg(UCPI_UBH(ucpi));
  608. if (goal == 0) {
  609. goal = ucpi->c_rotor;
  610. goto norot;
  611. }
  612. goal = ufs_blknum (goal);
  613. goal = ufs_dtogd(uspi, goal);
  614. /*
  615. * If the requested block is available, use it.
  616. */
  617. if (ubh_isblockset(UCPI_UBH(ucpi), ucpi->c_freeoff, ufs_fragstoblks(goal))) {
  618. result = goal;
  619. goto gotit;
  620. }
  621. norot:
  622. result = ufs_bitmap_search (sb, ucpi, goal, uspi->s_fpb);
  623. if (result == INVBLOCK)
  624. return INVBLOCK;
  625. ucpi->c_rotor = result;
  626. gotit:
  627. blkno = ufs_fragstoblks(result);
  628. ubh_clrblock (UCPI_UBH(ucpi), ucpi->c_freeoff, blkno);
  629. if ((UFS_SB(sb)->s_flags & UFS_CG_MASK) == UFS_CG_44BSD)
  630. ufs_clusteracct (sb, ucpi, blkno, -1);
  631. if(DQUOT_ALLOC_BLOCK(inode, uspi->s_fpb)) {
  632. *err = -EDQUOT;
  633. return INVBLOCK;
  634. }
  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. };