balloc.c 28 KB

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