jfs_metapage.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  1. /*
  2. * Copyright (C) International Business Machines Corp., 2000-2005
  3. * Portions Copyright (C) Christoph Hellwig, 2001-2002
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  13. * the GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <linux/fs.h>
  20. #include <linux/mm.h>
  21. #include <linux/module.h>
  22. #include <linux/bio.h>
  23. #include <linux/slab.h>
  24. #include <linux/init.h>
  25. #include <linux/buffer_head.h>
  26. #include <linux/mempool.h>
  27. #include <linux/seq_file.h>
  28. #include "jfs_incore.h"
  29. #include "jfs_superblock.h"
  30. #include "jfs_filsys.h"
  31. #include "jfs_metapage.h"
  32. #include "jfs_txnmgr.h"
  33. #include "jfs_debug.h"
  34. #ifdef CONFIG_JFS_STATISTICS
  35. static struct {
  36. uint pagealloc; /* # of page allocations */
  37. uint pagefree; /* # of page frees */
  38. uint lockwait; /* # of sleeping lock_metapage() calls */
  39. } mpStat;
  40. #endif
  41. #define metapage_locked(mp) test_bit(META_locked, &(mp)->flag)
  42. #define trylock_metapage(mp) test_and_set_bit_lock(META_locked, &(mp)->flag)
  43. static inline void unlock_metapage(struct metapage *mp)
  44. {
  45. clear_bit_unlock(META_locked, &mp->flag);
  46. wake_up(&mp->wait);
  47. }
  48. static inline void __lock_metapage(struct metapage *mp)
  49. {
  50. DECLARE_WAITQUEUE(wait, current);
  51. INCREMENT(mpStat.lockwait);
  52. add_wait_queue_exclusive(&mp->wait, &wait);
  53. do {
  54. set_current_state(TASK_UNINTERRUPTIBLE);
  55. if (metapage_locked(mp)) {
  56. unlock_page(mp->page);
  57. io_schedule();
  58. lock_page(mp->page);
  59. }
  60. } while (trylock_metapage(mp));
  61. __set_current_state(TASK_RUNNING);
  62. remove_wait_queue(&mp->wait, &wait);
  63. }
  64. /*
  65. * Must have mp->page locked
  66. */
  67. static inline void lock_metapage(struct metapage *mp)
  68. {
  69. if (trylock_metapage(mp))
  70. __lock_metapage(mp);
  71. }
  72. #define METAPOOL_MIN_PAGES 32
  73. static struct kmem_cache *metapage_cache;
  74. static mempool_t *metapage_mempool;
  75. #define MPS_PER_PAGE (PAGE_CACHE_SIZE >> L2PSIZE)
  76. #if MPS_PER_PAGE > 1
  77. struct meta_anchor {
  78. int mp_count;
  79. atomic_t io_count;
  80. struct metapage *mp[MPS_PER_PAGE];
  81. };
  82. #define mp_anchor(page) ((struct meta_anchor *)page_private(page))
  83. static inline struct metapage *page_to_mp(struct page *page, int offset)
  84. {
  85. if (!PagePrivate(page))
  86. return NULL;
  87. return mp_anchor(page)->mp[offset >> L2PSIZE];
  88. }
  89. static inline int insert_metapage(struct page *page, struct metapage *mp)
  90. {
  91. struct meta_anchor *a;
  92. int index;
  93. int l2mp_blocks; /* log2 blocks per metapage */
  94. if (PagePrivate(page))
  95. a = mp_anchor(page);
  96. else {
  97. a = kzalloc(sizeof(struct meta_anchor), GFP_NOFS);
  98. if (!a)
  99. return -ENOMEM;
  100. set_page_private(page, (unsigned long)a);
  101. SetPagePrivate(page);
  102. kmap(page);
  103. }
  104. if (mp) {
  105. l2mp_blocks = L2PSIZE - page->mapping->host->i_blkbits;
  106. index = (mp->index >> l2mp_blocks) & (MPS_PER_PAGE - 1);
  107. a->mp_count++;
  108. a->mp[index] = mp;
  109. }
  110. return 0;
  111. }
  112. static inline void remove_metapage(struct page *page, struct metapage *mp)
  113. {
  114. struct meta_anchor *a = mp_anchor(page);
  115. int l2mp_blocks = L2PSIZE - page->mapping->host->i_blkbits;
  116. int index;
  117. index = (mp->index >> l2mp_blocks) & (MPS_PER_PAGE - 1);
  118. BUG_ON(a->mp[index] != mp);
  119. a->mp[index] = NULL;
  120. if (--a->mp_count == 0) {
  121. kfree(a);
  122. set_page_private(page, 0);
  123. ClearPagePrivate(page);
  124. kunmap(page);
  125. }
  126. }
  127. static inline void inc_io(struct page *page)
  128. {
  129. atomic_inc(&mp_anchor(page)->io_count);
  130. }
  131. static inline void dec_io(struct page *page, void (*handler) (struct page *))
  132. {
  133. if (atomic_dec_and_test(&mp_anchor(page)->io_count))
  134. handler(page);
  135. }
  136. #else
  137. static inline struct metapage *page_to_mp(struct page *page, int offset)
  138. {
  139. return PagePrivate(page) ? (struct metapage *)page_private(page) : NULL;
  140. }
  141. static inline int insert_metapage(struct page *page, struct metapage *mp)
  142. {
  143. if (mp) {
  144. set_page_private(page, (unsigned long)mp);
  145. SetPagePrivate(page);
  146. kmap(page);
  147. }
  148. return 0;
  149. }
  150. static inline void remove_metapage(struct page *page, struct metapage *mp)
  151. {
  152. set_page_private(page, 0);
  153. ClearPagePrivate(page);
  154. kunmap(page);
  155. }
  156. #define inc_io(page) do {} while(0)
  157. #define dec_io(page, handler) handler(page)
  158. #endif
  159. static void init_once(void *foo)
  160. {
  161. struct metapage *mp = (struct metapage *)foo;
  162. mp->lid = 0;
  163. mp->lsn = 0;
  164. mp->flag = 0;
  165. mp->data = NULL;
  166. mp->clsn = 0;
  167. mp->log = NULL;
  168. set_bit(META_free, &mp->flag);
  169. init_waitqueue_head(&mp->wait);
  170. }
  171. static inline struct metapage *alloc_metapage(gfp_t gfp_mask)
  172. {
  173. return mempool_alloc(metapage_mempool, gfp_mask);
  174. }
  175. static inline void free_metapage(struct metapage *mp)
  176. {
  177. mp->flag = 0;
  178. set_bit(META_free, &mp->flag);
  179. mempool_free(mp, metapage_mempool);
  180. }
  181. int __init metapage_init(void)
  182. {
  183. /*
  184. * Allocate the metapage structures
  185. */
  186. metapage_cache = kmem_cache_create("jfs_mp", sizeof(struct metapage),
  187. 0, 0, init_once);
  188. if (metapage_cache == NULL)
  189. return -ENOMEM;
  190. metapage_mempool = mempool_create_slab_pool(METAPOOL_MIN_PAGES,
  191. metapage_cache);
  192. if (metapage_mempool == NULL) {
  193. kmem_cache_destroy(metapage_cache);
  194. return -ENOMEM;
  195. }
  196. return 0;
  197. }
  198. void metapage_exit(void)
  199. {
  200. mempool_destroy(metapage_mempool);
  201. kmem_cache_destroy(metapage_cache);
  202. }
  203. static inline void drop_metapage(struct page *page, struct metapage *mp)
  204. {
  205. if (mp->count || mp->nohomeok || test_bit(META_dirty, &mp->flag) ||
  206. test_bit(META_io, &mp->flag))
  207. return;
  208. remove_metapage(page, mp);
  209. INCREMENT(mpStat.pagefree);
  210. free_metapage(mp);
  211. }
  212. /*
  213. * Metapage address space operations
  214. */
  215. static sector_t metapage_get_blocks(struct inode *inode, sector_t lblock,
  216. int *len)
  217. {
  218. int rc = 0;
  219. int xflag;
  220. s64 xaddr;
  221. sector_t file_blocks = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
  222. inode->i_blkbits;
  223. if (lblock >= file_blocks)
  224. return 0;
  225. if (lblock + *len > file_blocks)
  226. *len = file_blocks - lblock;
  227. if (inode->i_ino) {
  228. rc = xtLookup(inode, (s64)lblock, *len, &xflag, &xaddr, len, 0);
  229. if ((rc == 0) && *len)
  230. lblock = (sector_t)xaddr;
  231. else
  232. lblock = 0;
  233. } /* else no mapping */
  234. return lblock;
  235. }
  236. static void last_read_complete(struct page *page)
  237. {
  238. if (!PageError(page))
  239. SetPageUptodate(page);
  240. unlock_page(page);
  241. }
  242. static void metapage_read_end_io(struct bio *bio, int err)
  243. {
  244. struct page *page = bio->bi_private;
  245. if (!test_bit(BIO_UPTODATE, &bio->bi_flags)) {
  246. printk(KERN_ERR "metapage_read_end_io: I/O error\n");
  247. SetPageError(page);
  248. }
  249. dec_io(page, last_read_complete);
  250. bio_put(bio);
  251. }
  252. static void remove_from_logsync(struct metapage *mp)
  253. {
  254. struct jfs_log *log = mp->log;
  255. unsigned long flags;
  256. /*
  257. * This can race. Recheck that log hasn't been set to null, and after
  258. * acquiring logsync lock, recheck lsn
  259. */
  260. if (!log)
  261. return;
  262. LOGSYNC_LOCK(log, flags);
  263. if (mp->lsn) {
  264. mp->log = NULL;
  265. mp->lsn = 0;
  266. mp->clsn = 0;
  267. log->count--;
  268. list_del(&mp->synclist);
  269. }
  270. LOGSYNC_UNLOCK(log, flags);
  271. }
  272. static void last_write_complete(struct page *page)
  273. {
  274. struct metapage *mp;
  275. unsigned int offset;
  276. for (offset = 0; offset < PAGE_CACHE_SIZE; offset += PSIZE) {
  277. mp = page_to_mp(page, offset);
  278. if (mp && test_bit(META_io, &mp->flag)) {
  279. if (mp->lsn)
  280. remove_from_logsync(mp);
  281. clear_bit(META_io, &mp->flag);
  282. }
  283. /*
  284. * I'd like to call drop_metapage here, but I don't think it's
  285. * safe unless I have the page locked
  286. */
  287. }
  288. end_page_writeback(page);
  289. }
  290. static void metapage_write_end_io(struct bio *bio, int err)
  291. {
  292. struct page *page = bio->bi_private;
  293. BUG_ON(!PagePrivate(page));
  294. if (! test_bit(BIO_UPTODATE, &bio->bi_flags)) {
  295. printk(KERN_ERR "metapage_write_end_io: I/O error\n");
  296. SetPageError(page);
  297. }
  298. dec_io(page, last_write_complete);
  299. bio_put(bio);
  300. }
  301. static int metapage_writepage(struct page *page, struct writeback_control *wbc)
  302. {
  303. struct bio *bio = NULL;
  304. int block_offset; /* block offset of mp within page */
  305. struct inode *inode = page->mapping->host;
  306. int blocks_per_mp = JFS_SBI(inode->i_sb)->nbperpage;
  307. int len;
  308. int xlen;
  309. struct metapage *mp;
  310. int redirty = 0;
  311. sector_t lblock;
  312. int nr_underway = 0;
  313. sector_t pblock;
  314. sector_t next_block = 0;
  315. sector_t page_start;
  316. unsigned long bio_bytes = 0;
  317. unsigned long bio_offset = 0;
  318. int offset;
  319. int bad_blocks = 0;
  320. page_start = (sector_t)page->index <<
  321. (PAGE_CACHE_SHIFT - inode->i_blkbits);
  322. BUG_ON(!PageLocked(page));
  323. BUG_ON(PageWriteback(page));
  324. set_page_writeback(page);
  325. for (offset = 0; offset < PAGE_CACHE_SIZE; offset += PSIZE) {
  326. mp = page_to_mp(page, offset);
  327. if (!mp || !test_bit(META_dirty, &mp->flag))
  328. continue;
  329. if (mp->nohomeok && !test_bit(META_forcewrite, &mp->flag)) {
  330. redirty = 1;
  331. /*
  332. * Make sure this page isn't blocked indefinitely.
  333. * If the journal isn't undergoing I/O, push it
  334. */
  335. if (mp->log && !(mp->log->cflag & logGC_PAGEOUT))
  336. jfs_flush_journal(mp->log, 0);
  337. continue;
  338. }
  339. clear_bit(META_dirty, &mp->flag);
  340. set_bit(META_io, &mp->flag);
  341. block_offset = offset >> inode->i_blkbits;
  342. lblock = page_start + block_offset;
  343. if (bio) {
  344. if (xlen && lblock == next_block) {
  345. /* Contiguous, in memory & on disk */
  346. len = min(xlen, blocks_per_mp);
  347. xlen -= len;
  348. bio_bytes += len << inode->i_blkbits;
  349. continue;
  350. }
  351. /* Not contiguous */
  352. if (bio_add_page(bio, page, bio_bytes, bio_offset) <
  353. bio_bytes)
  354. goto add_failed;
  355. /*
  356. * Increment counter before submitting i/o to keep
  357. * count from hitting zero before we're through
  358. */
  359. inc_io(page);
  360. if (!bio->bi_size)
  361. goto dump_bio;
  362. submit_bio(WRITE, bio);
  363. nr_underway++;
  364. bio = NULL;
  365. } else
  366. inc_io(page);
  367. xlen = (PAGE_CACHE_SIZE - offset) >> inode->i_blkbits;
  368. pblock = metapage_get_blocks(inode, lblock, &xlen);
  369. if (!pblock) {
  370. printk(KERN_ERR "JFS: metapage_get_blocks failed\n");
  371. /*
  372. * We already called inc_io(), but can't cancel it
  373. * with dec_io() until we're done with the page
  374. */
  375. bad_blocks++;
  376. continue;
  377. }
  378. len = min(xlen, (int)JFS_SBI(inode->i_sb)->nbperpage);
  379. bio = bio_alloc(GFP_NOFS, 1);
  380. bio->bi_bdev = inode->i_sb->s_bdev;
  381. bio->bi_sector = pblock << (inode->i_blkbits - 9);
  382. bio->bi_end_io = metapage_write_end_io;
  383. bio->bi_private = page;
  384. /* Don't call bio_add_page yet, we may add to this vec */
  385. bio_offset = offset;
  386. bio_bytes = len << inode->i_blkbits;
  387. xlen -= len;
  388. next_block = lblock + len;
  389. }
  390. if (bio) {
  391. if (bio_add_page(bio, page, bio_bytes, bio_offset) < bio_bytes)
  392. goto add_failed;
  393. if (!bio->bi_size)
  394. goto dump_bio;
  395. submit_bio(WRITE, bio);
  396. nr_underway++;
  397. }
  398. if (redirty)
  399. redirty_page_for_writepage(wbc, page);
  400. unlock_page(page);
  401. if (bad_blocks)
  402. goto err_out;
  403. if (nr_underway == 0)
  404. end_page_writeback(page);
  405. return 0;
  406. add_failed:
  407. /* We should never reach here, since we're only adding one vec */
  408. printk(KERN_ERR "JFS: bio_add_page failed unexpectedly\n");
  409. goto skip;
  410. dump_bio:
  411. print_hex_dump(KERN_ERR, "JFS: dump of bio: ", DUMP_PREFIX_ADDRESS, 16,
  412. 4, bio, sizeof(*bio), 0);
  413. skip:
  414. bio_put(bio);
  415. unlock_page(page);
  416. dec_io(page, last_write_complete);
  417. err_out:
  418. while (bad_blocks--)
  419. dec_io(page, last_write_complete);
  420. return -EIO;
  421. }
  422. static int metapage_readpage(struct file *fp, struct page *page)
  423. {
  424. struct inode *inode = page->mapping->host;
  425. struct bio *bio = NULL;
  426. int block_offset;
  427. int blocks_per_page = PAGE_CACHE_SIZE >> inode->i_blkbits;
  428. sector_t page_start; /* address of page in fs blocks */
  429. sector_t pblock;
  430. int xlen;
  431. unsigned int len;
  432. int offset;
  433. BUG_ON(!PageLocked(page));
  434. page_start = (sector_t)page->index <<
  435. (PAGE_CACHE_SHIFT - inode->i_blkbits);
  436. block_offset = 0;
  437. while (block_offset < blocks_per_page) {
  438. xlen = blocks_per_page - block_offset;
  439. pblock = metapage_get_blocks(inode, page_start + block_offset,
  440. &xlen);
  441. if (pblock) {
  442. if (!PagePrivate(page))
  443. insert_metapage(page, NULL);
  444. inc_io(page);
  445. if (bio)
  446. submit_bio(READ, bio);
  447. bio = bio_alloc(GFP_NOFS, 1);
  448. bio->bi_bdev = inode->i_sb->s_bdev;
  449. bio->bi_sector = pblock << (inode->i_blkbits - 9);
  450. bio->bi_end_io = metapage_read_end_io;
  451. bio->bi_private = page;
  452. len = xlen << inode->i_blkbits;
  453. offset = block_offset << inode->i_blkbits;
  454. if (bio_add_page(bio, page, len, offset) < len)
  455. goto add_failed;
  456. block_offset += xlen;
  457. } else
  458. block_offset++;
  459. }
  460. if (bio)
  461. submit_bio(READ, bio);
  462. else
  463. unlock_page(page);
  464. return 0;
  465. add_failed:
  466. printk(KERN_ERR "JFS: bio_add_page failed unexpectedly\n");
  467. bio_put(bio);
  468. dec_io(page, last_read_complete);
  469. return -EIO;
  470. }
  471. static int metapage_releasepage(struct page *page, gfp_t gfp_mask)
  472. {
  473. struct metapage *mp;
  474. int ret = 1;
  475. int offset;
  476. for (offset = 0; offset < PAGE_CACHE_SIZE; offset += PSIZE) {
  477. mp = page_to_mp(page, offset);
  478. if (!mp)
  479. continue;
  480. jfs_info("metapage_releasepage: mp = 0x%p", mp);
  481. if (mp->count || mp->nohomeok ||
  482. test_bit(META_dirty, &mp->flag)) {
  483. jfs_info("count = %ld, nohomeok = %d", mp->count,
  484. mp->nohomeok);
  485. ret = 0;
  486. continue;
  487. }
  488. if (mp->lsn)
  489. remove_from_logsync(mp);
  490. remove_metapage(page, mp);
  491. INCREMENT(mpStat.pagefree);
  492. free_metapage(mp);
  493. }
  494. return ret;
  495. }
  496. static void metapage_invalidatepage(struct page *page, unsigned long offset)
  497. {
  498. BUG_ON(offset);
  499. BUG_ON(PageWriteback(page));
  500. metapage_releasepage(page, 0);
  501. }
  502. const struct address_space_operations jfs_metapage_aops = {
  503. .readpage = metapage_readpage,
  504. .writepage = metapage_writepage,
  505. .releasepage = metapage_releasepage,
  506. .invalidatepage = metapage_invalidatepage,
  507. .set_page_dirty = __set_page_dirty_nobuffers,
  508. };
  509. struct metapage *__get_metapage(struct inode *inode, unsigned long lblock,
  510. unsigned int size, int absolute,
  511. unsigned long new)
  512. {
  513. int l2BlocksPerPage;
  514. int l2bsize;
  515. struct address_space *mapping;
  516. struct metapage *mp = NULL;
  517. struct page *page;
  518. unsigned long page_index;
  519. unsigned long page_offset;
  520. jfs_info("__get_metapage: ino = %ld, lblock = 0x%lx, abs=%d",
  521. inode->i_ino, lblock, absolute);
  522. l2bsize = inode->i_blkbits;
  523. l2BlocksPerPage = PAGE_CACHE_SHIFT - l2bsize;
  524. page_index = lblock >> l2BlocksPerPage;
  525. page_offset = (lblock - (page_index << l2BlocksPerPage)) << l2bsize;
  526. if ((page_offset + size) > PAGE_CACHE_SIZE) {
  527. jfs_err("MetaData crosses page boundary!!");
  528. jfs_err("lblock = %lx, size = %d", lblock, size);
  529. dump_stack();
  530. return NULL;
  531. }
  532. if (absolute)
  533. mapping = JFS_SBI(inode->i_sb)->direct_inode->i_mapping;
  534. else {
  535. /*
  536. * If an nfs client tries to read an inode that is larger
  537. * than any existing inodes, we may try to read past the
  538. * end of the inode map
  539. */
  540. if ((lblock << inode->i_blkbits) >= inode->i_size)
  541. return NULL;
  542. mapping = inode->i_mapping;
  543. }
  544. if (new && (PSIZE == PAGE_CACHE_SIZE)) {
  545. page = grab_cache_page(mapping, page_index);
  546. if (!page) {
  547. jfs_err("grab_cache_page failed!");
  548. return NULL;
  549. }
  550. SetPageUptodate(page);
  551. } else {
  552. page = read_mapping_page(mapping, page_index, NULL);
  553. if (IS_ERR(page) || !PageUptodate(page)) {
  554. jfs_err("read_mapping_page failed!");
  555. return NULL;
  556. }
  557. lock_page(page);
  558. }
  559. mp = page_to_mp(page, page_offset);
  560. if (mp) {
  561. if (mp->logical_size != size) {
  562. jfs_error(inode->i_sb,
  563. "__get_metapage: mp->logical_size != size");
  564. jfs_err("logical_size = %d, size = %d",
  565. mp->logical_size, size);
  566. dump_stack();
  567. goto unlock;
  568. }
  569. mp->count++;
  570. lock_metapage(mp);
  571. if (test_bit(META_discard, &mp->flag)) {
  572. if (!new) {
  573. jfs_error(inode->i_sb,
  574. "__get_metapage: using a "
  575. "discarded metapage");
  576. discard_metapage(mp);
  577. goto unlock;
  578. }
  579. clear_bit(META_discard, &mp->flag);
  580. }
  581. } else {
  582. INCREMENT(mpStat.pagealloc);
  583. mp = alloc_metapage(GFP_NOFS);
  584. mp->page = page;
  585. mp->flag = 0;
  586. mp->xflag = COMMIT_PAGE;
  587. mp->count = 1;
  588. mp->nohomeok = 0;
  589. mp->logical_size = size;
  590. mp->data = page_address(page) + page_offset;
  591. mp->index = lblock;
  592. if (unlikely(insert_metapage(page, mp))) {
  593. free_metapage(mp);
  594. goto unlock;
  595. }
  596. lock_metapage(mp);
  597. }
  598. if (new) {
  599. jfs_info("zeroing mp = 0x%p", mp);
  600. memset(mp->data, 0, PSIZE);
  601. }
  602. unlock_page(page);
  603. jfs_info("__get_metapage: returning = 0x%p data = 0x%p", mp, mp->data);
  604. return mp;
  605. unlock:
  606. unlock_page(page);
  607. return NULL;
  608. }
  609. void grab_metapage(struct metapage * mp)
  610. {
  611. jfs_info("grab_metapage: mp = 0x%p", mp);
  612. page_cache_get(mp->page);
  613. lock_page(mp->page);
  614. mp->count++;
  615. lock_metapage(mp);
  616. unlock_page(mp->page);
  617. }
  618. void force_metapage(struct metapage *mp)
  619. {
  620. struct page *page = mp->page;
  621. jfs_info("force_metapage: mp = 0x%p", mp);
  622. set_bit(META_forcewrite, &mp->flag);
  623. clear_bit(META_sync, &mp->flag);
  624. page_cache_get(page);
  625. lock_page(page);
  626. set_page_dirty(page);
  627. write_one_page(page, 1);
  628. clear_bit(META_forcewrite, &mp->flag);
  629. page_cache_release(page);
  630. }
  631. void hold_metapage(struct metapage *mp)
  632. {
  633. lock_page(mp->page);
  634. }
  635. void put_metapage(struct metapage *mp)
  636. {
  637. if (mp->count || mp->nohomeok) {
  638. /* Someone else will release this */
  639. unlock_page(mp->page);
  640. return;
  641. }
  642. page_cache_get(mp->page);
  643. mp->count++;
  644. lock_metapage(mp);
  645. unlock_page(mp->page);
  646. release_metapage(mp);
  647. }
  648. void release_metapage(struct metapage * mp)
  649. {
  650. struct page *page = mp->page;
  651. jfs_info("release_metapage: mp = 0x%p, flag = 0x%lx", mp, mp->flag);
  652. BUG_ON(!page);
  653. lock_page(page);
  654. unlock_metapage(mp);
  655. assert(mp->count);
  656. if (--mp->count || mp->nohomeok) {
  657. unlock_page(page);
  658. page_cache_release(page);
  659. return;
  660. }
  661. if (test_bit(META_dirty, &mp->flag)) {
  662. set_page_dirty(page);
  663. if (test_bit(META_sync, &mp->flag)) {
  664. clear_bit(META_sync, &mp->flag);
  665. write_one_page(page, 1);
  666. lock_page(page); /* write_one_page unlocks the page */
  667. }
  668. } else if (mp->lsn) /* discard_metapage doesn't remove it */
  669. remove_from_logsync(mp);
  670. /* Try to keep metapages from using up too much memory */
  671. drop_metapage(page, mp);
  672. unlock_page(page);
  673. page_cache_release(page);
  674. }
  675. void __invalidate_metapages(struct inode *ip, s64 addr, int len)
  676. {
  677. sector_t lblock;
  678. int l2BlocksPerPage = PAGE_CACHE_SHIFT - ip->i_blkbits;
  679. int BlocksPerPage = 1 << l2BlocksPerPage;
  680. /* All callers are interested in block device's mapping */
  681. struct address_space *mapping =
  682. JFS_SBI(ip->i_sb)->direct_inode->i_mapping;
  683. struct metapage *mp;
  684. struct page *page;
  685. unsigned int offset;
  686. /*
  687. * Mark metapages to discard. They will eventually be
  688. * released, but should not be written.
  689. */
  690. for (lblock = addr & ~(BlocksPerPage - 1); lblock < addr + len;
  691. lblock += BlocksPerPage) {
  692. page = find_lock_page(mapping, lblock >> l2BlocksPerPage);
  693. if (!page)
  694. continue;
  695. for (offset = 0; offset < PAGE_CACHE_SIZE; offset += PSIZE) {
  696. mp = page_to_mp(page, offset);
  697. if (!mp)
  698. continue;
  699. if (mp->index < addr)
  700. continue;
  701. if (mp->index >= addr + len)
  702. break;
  703. clear_bit(META_dirty, &mp->flag);
  704. set_bit(META_discard, &mp->flag);
  705. if (mp->lsn)
  706. remove_from_logsync(mp);
  707. }
  708. unlock_page(page);
  709. page_cache_release(page);
  710. }
  711. }
  712. #ifdef CONFIG_JFS_STATISTICS
  713. static int jfs_mpstat_proc_show(struct seq_file *m, void *v)
  714. {
  715. seq_printf(m,
  716. "JFS Metapage statistics\n"
  717. "=======================\n"
  718. "page allocations = %d\n"
  719. "page frees = %d\n"
  720. "lock waits = %d\n",
  721. mpStat.pagealloc,
  722. mpStat.pagefree,
  723. mpStat.lockwait);
  724. return 0;
  725. }
  726. static int jfs_mpstat_proc_open(struct inode *inode, struct file *file)
  727. {
  728. return single_open(file, jfs_mpstat_proc_show, NULL);
  729. }
  730. const struct file_operations jfs_mpstat_proc_fops = {
  731. .owner = THIS_MODULE,
  732. .open = jfs_mpstat_proc_open,
  733. .read = seq_read,
  734. .llseek = seq_lseek,
  735. .release = single_release,
  736. };
  737. #endif