jfs_metapage.c 20 KB

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