balloc.c 28 KB

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