ops_address.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
  4. *
  5. * This copyrighted material is made available to anyone wishing to use,
  6. * modify, copy, or redistribute it subject to the terms and conditions
  7. * of the GNU General Public License version 2.
  8. */
  9. #include <linux/sched.h>
  10. #include <linux/slab.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/completion.h>
  13. #include <linux/buffer_head.h>
  14. #include <linux/pagemap.h>
  15. #include <linux/pagevec.h>
  16. #include <linux/mpage.h>
  17. #include <linux/fs.h>
  18. #include <linux/writeback.h>
  19. #include <linux/swap.h>
  20. #include <linux/gfs2_ondisk.h>
  21. #include <linux/lm_interface.h>
  22. #include <linux/backing-dev.h>
  23. #include <linux/pagevec.h>
  24. #include "gfs2.h"
  25. #include "incore.h"
  26. #include "bmap.h"
  27. #include "glock.h"
  28. #include "inode.h"
  29. #include "log.h"
  30. #include "meta_io.h"
  31. #include "ops_address.h"
  32. #include "quota.h"
  33. #include "trans.h"
  34. #include "rgrp.h"
  35. #include "super.h"
  36. #include "util.h"
  37. #include "glops.h"
  38. static void gfs2_page_add_databufs(struct gfs2_inode *ip, struct page *page,
  39. unsigned int from, unsigned int to)
  40. {
  41. struct buffer_head *head = page_buffers(page);
  42. unsigned int bsize = head->b_size;
  43. struct buffer_head *bh;
  44. unsigned int start, end;
  45. for (bh = head, start = 0; bh != head || !start;
  46. bh = bh->b_this_page, start = end) {
  47. end = start + bsize;
  48. if (end <= from || start >= to)
  49. continue;
  50. if (gfs2_is_jdata(ip))
  51. set_buffer_uptodate(bh);
  52. gfs2_trans_add_bh(ip->i_gl, bh, 0);
  53. }
  54. }
  55. /**
  56. * gfs2_get_block_noalloc - Fills in a buffer head with details about a block
  57. * @inode: The inode
  58. * @lblock: The block number to look up
  59. * @bh_result: The buffer head to return the result in
  60. * @create: Non-zero if we may add block to the file
  61. *
  62. * Returns: errno
  63. */
  64. static int gfs2_get_block_noalloc(struct inode *inode, sector_t lblock,
  65. struct buffer_head *bh_result, int create)
  66. {
  67. int error;
  68. error = gfs2_block_map(inode, lblock, bh_result, 0);
  69. if (error)
  70. return error;
  71. if (!buffer_mapped(bh_result))
  72. return -EIO;
  73. return 0;
  74. }
  75. static int gfs2_get_block_direct(struct inode *inode, sector_t lblock,
  76. struct buffer_head *bh_result, int create)
  77. {
  78. return gfs2_block_map(inode, lblock, bh_result, 0);
  79. }
  80. /**
  81. * gfs2_writepage_common - Common bits of writepage
  82. * @page: The page to be written
  83. * @wbc: The writeback control
  84. *
  85. * Returns: 1 if writepage is ok, otherwise an error code or zero if no error.
  86. */
  87. static int gfs2_writepage_common(struct page *page,
  88. struct writeback_control *wbc)
  89. {
  90. struct inode *inode = page->mapping->host;
  91. struct gfs2_inode *ip = GFS2_I(inode);
  92. struct gfs2_sbd *sdp = GFS2_SB(inode);
  93. loff_t i_size = i_size_read(inode);
  94. pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
  95. unsigned offset;
  96. int ret = -EIO;
  97. if (gfs2_assert_withdraw(sdp, gfs2_glock_is_held_excl(ip->i_gl)))
  98. goto out;
  99. ret = 0;
  100. if (current->journal_info)
  101. goto redirty;
  102. /* Is the page fully outside i_size? (truncate in progress) */
  103. offset = i_size & (PAGE_CACHE_SIZE-1);
  104. if (page->index > end_index || (page->index == end_index && !offset)) {
  105. page->mapping->a_ops->invalidatepage(page, 0);
  106. goto out;
  107. }
  108. return 1;
  109. redirty:
  110. redirty_page_for_writepage(wbc, page);
  111. out:
  112. unlock_page(page);
  113. return 0;
  114. }
  115. /**
  116. * gfs2_writeback_writepage - Write page for writeback mappings
  117. * @page: The page
  118. * @wbc: The writeback control
  119. *
  120. */
  121. static int gfs2_writeback_writepage(struct page *page,
  122. struct writeback_control *wbc)
  123. {
  124. int ret;
  125. ret = gfs2_writepage_common(page, wbc);
  126. if (ret <= 0)
  127. return ret;
  128. ret = mpage_writepage(page, gfs2_get_block_noalloc, wbc);
  129. if (ret == -EAGAIN)
  130. ret = block_write_full_page(page, gfs2_get_block_noalloc, wbc);
  131. return ret;
  132. }
  133. /**
  134. * gfs2_ordered_writepage - Write page for ordered data files
  135. * @page: The page to write
  136. * @wbc: The writeback control
  137. *
  138. */
  139. static int gfs2_ordered_writepage(struct page *page,
  140. struct writeback_control *wbc)
  141. {
  142. struct inode *inode = page->mapping->host;
  143. struct gfs2_inode *ip = GFS2_I(inode);
  144. int ret;
  145. ret = gfs2_writepage_common(page, wbc);
  146. if (ret <= 0)
  147. return ret;
  148. if (!page_has_buffers(page)) {
  149. create_empty_buffers(page, inode->i_sb->s_blocksize,
  150. (1 << BH_Dirty)|(1 << BH_Uptodate));
  151. }
  152. gfs2_page_add_databufs(ip, page, 0, inode->i_sb->s_blocksize-1);
  153. return block_write_full_page(page, gfs2_get_block_noalloc, wbc);
  154. }
  155. /**
  156. * __gfs2_jdata_writepage - The core of jdata writepage
  157. * @page: The page to write
  158. * @wbc: The writeback control
  159. *
  160. * This is shared between writepage and writepages and implements the
  161. * core of the writepage operation. If a transaction is required then
  162. * PageChecked will have been set and the transaction will have
  163. * already been started before this is called.
  164. */
  165. static int __gfs2_jdata_writepage(struct page *page, struct writeback_control *wbc)
  166. {
  167. struct inode *inode = page->mapping->host;
  168. struct gfs2_inode *ip = GFS2_I(inode);
  169. struct gfs2_sbd *sdp = GFS2_SB(inode);
  170. if (PageChecked(page)) {
  171. ClearPageChecked(page);
  172. if (!page_has_buffers(page)) {
  173. create_empty_buffers(page, inode->i_sb->s_blocksize,
  174. (1 << BH_Dirty)|(1 << BH_Uptodate));
  175. }
  176. gfs2_page_add_databufs(ip, page, 0, sdp->sd_vfs->s_blocksize-1);
  177. }
  178. return block_write_full_page(page, gfs2_get_block_noalloc, wbc);
  179. }
  180. /**
  181. * gfs2_jdata_writepage - Write complete page
  182. * @page: Page to write
  183. *
  184. * Returns: errno
  185. *
  186. */
  187. static int gfs2_jdata_writepage(struct page *page, struct writeback_control *wbc)
  188. {
  189. struct inode *inode = page->mapping->host;
  190. struct gfs2_sbd *sdp = GFS2_SB(inode);
  191. int error;
  192. int done_trans = 0;
  193. error = gfs2_writepage_common(page, wbc);
  194. if (error <= 0)
  195. return error;
  196. if (PageChecked(page)) {
  197. if (wbc->sync_mode != WB_SYNC_ALL)
  198. goto out_ignore;
  199. error = gfs2_trans_begin(sdp, RES_DINODE + 1, 0);
  200. if (error)
  201. goto out_ignore;
  202. done_trans = 1;
  203. }
  204. error = __gfs2_jdata_writepage(page, wbc);
  205. if (done_trans)
  206. gfs2_trans_end(sdp);
  207. return error;
  208. out_ignore:
  209. redirty_page_for_writepage(wbc, page);
  210. unlock_page(page);
  211. return 0;
  212. }
  213. /**
  214. * gfs2_writeback_writepages - Write a bunch of dirty pages back to disk
  215. * @mapping: The mapping to write
  216. * @wbc: Write-back control
  217. *
  218. * For the data=writeback case we can already ignore buffer heads
  219. * and write whole extents at once. This is a big reduction in the
  220. * number of I/O requests we send and the bmap calls we make in this case.
  221. */
  222. static int gfs2_writeback_writepages(struct address_space *mapping,
  223. struct writeback_control *wbc)
  224. {
  225. return mpage_writepages(mapping, wbc, gfs2_get_block_noalloc);
  226. }
  227. /**
  228. * gfs2_write_jdata_pagevec - Write back a pagevec's worth of pages
  229. * @mapping: The mapping
  230. * @wbc: The writeback control
  231. * @writepage: The writepage function to call for each page
  232. * @pvec: The vector of pages
  233. * @nr_pages: The number of pages to write
  234. *
  235. * Returns: non-zero if loop should terminate, zero otherwise
  236. */
  237. static int gfs2_write_jdata_pagevec(struct address_space *mapping,
  238. struct writeback_control *wbc,
  239. struct pagevec *pvec,
  240. int nr_pages, pgoff_t end)
  241. {
  242. struct inode *inode = mapping->host;
  243. struct gfs2_sbd *sdp = GFS2_SB(inode);
  244. loff_t i_size = i_size_read(inode);
  245. pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
  246. unsigned offset = i_size & (PAGE_CACHE_SIZE-1);
  247. unsigned nrblocks = nr_pages * (PAGE_CACHE_SIZE/inode->i_sb->s_blocksize);
  248. struct backing_dev_info *bdi = mapping->backing_dev_info;
  249. int i;
  250. int ret;
  251. ret = gfs2_trans_begin(sdp, nrblocks, 0);
  252. if (ret < 0)
  253. return ret;
  254. for(i = 0; i < nr_pages; i++) {
  255. struct page *page = pvec->pages[i];
  256. lock_page(page);
  257. if (unlikely(page->mapping != mapping)) {
  258. unlock_page(page);
  259. continue;
  260. }
  261. if (!wbc->range_cyclic && page->index > end) {
  262. ret = 1;
  263. unlock_page(page);
  264. continue;
  265. }
  266. if (wbc->sync_mode != WB_SYNC_NONE)
  267. wait_on_page_writeback(page);
  268. if (PageWriteback(page) ||
  269. !clear_page_dirty_for_io(page)) {
  270. unlock_page(page);
  271. continue;
  272. }
  273. /* Is the page fully outside i_size? (truncate in progress) */
  274. if (page->index > end_index || (page->index == end_index && !offset)) {
  275. page->mapping->a_ops->invalidatepage(page, 0);
  276. unlock_page(page);
  277. continue;
  278. }
  279. ret = __gfs2_jdata_writepage(page, wbc);
  280. if (ret || (--(wbc->nr_to_write) <= 0))
  281. ret = 1;
  282. if (wbc->nonblocking && bdi_write_congested(bdi)) {
  283. wbc->encountered_congestion = 1;
  284. ret = 1;
  285. }
  286. }
  287. gfs2_trans_end(sdp);
  288. return ret;
  289. }
  290. /**
  291. * gfs2_write_cache_jdata - Like write_cache_pages but different
  292. * @mapping: The mapping to write
  293. * @wbc: The writeback control
  294. * @writepage: The writepage function to call
  295. * @data: The data to pass to writepage
  296. *
  297. * The reason that we use our own function here is that we need to
  298. * start transactions before we grab page locks. This allows us
  299. * to get the ordering right.
  300. */
  301. static int gfs2_write_cache_jdata(struct address_space *mapping,
  302. struct writeback_control *wbc)
  303. {
  304. struct backing_dev_info *bdi = mapping->backing_dev_info;
  305. int ret = 0;
  306. int done = 0;
  307. struct pagevec pvec;
  308. int nr_pages;
  309. pgoff_t index;
  310. pgoff_t end;
  311. int scanned = 0;
  312. int range_whole = 0;
  313. if (wbc->nonblocking && bdi_write_congested(bdi)) {
  314. wbc->encountered_congestion = 1;
  315. return 0;
  316. }
  317. pagevec_init(&pvec, 0);
  318. if (wbc->range_cyclic) {
  319. index = mapping->writeback_index; /* Start from prev offset */
  320. end = -1;
  321. } else {
  322. index = wbc->range_start >> PAGE_CACHE_SHIFT;
  323. end = wbc->range_end >> PAGE_CACHE_SHIFT;
  324. if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
  325. range_whole = 1;
  326. scanned = 1;
  327. }
  328. retry:
  329. while (!done && (index <= end) &&
  330. (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
  331. PAGECACHE_TAG_DIRTY,
  332. min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
  333. scanned = 1;
  334. ret = gfs2_write_jdata_pagevec(mapping, wbc, &pvec, nr_pages, end);
  335. if (ret)
  336. done = 1;
  337. if (ret > 0)
  338. ret = 0;
  339. pagevec_release(&pvec);
  340. cond_resched();
  341. }
  342. if (!scanned && !done) {
  343. /*
  344. * We hit the last page and there is more work to be done: wrap
  345. * back to the start of the file
  346. */
  347. scanned = 1;
  348. index = 0;
  349. goto retry;
  350. }
  351. if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
  352. mapping->writeback_index = index;
  353. return ret;
  354. }
  355. /**
  356. * gfs2_jdata_writepages - Write a bunch of dirty pages back to disk
  357. * @mapping: The mapping to write
  358. * @wbc: The writeback control
  359. *
  360. */
  361. static int gfs2_jdata_writepages(struct address_space *mapping,
  362. struct writeback_control *wbc)
  363. {
  364. struct gfs2_inode *ip = GFS2_I(mapping->host);
  365. struct gfs2_sbd *sdp = GFS2_SB(mapping->host);
  366. int ret;
  367. ret = gfs2_write_cache_jdata(mapping, wbc);
  368. if (ret == 0 && wbc->sync_mode == WB_SYNC_ALL) {
  369. gfs2_log_flush(sdp, ip->i_gl);
  370. ret = gfs2_write_cache_jdata(mapping, wbc);
  371. }
  372. return ret;
  373. }
  374. /**
  375. * stuffed_readpage - Fill in a Linux page with stuffed file data
  376. * @ip: the inode
  377. * @page: the page
  378. *
  379. * Returns: errno
  380. */
  381. static int stuffed_readpage(struct gfs2_inode *ip, struct page *page)
  382. {
  383. struct buffer_head *dibh;
  384. void *kaddr;
  385. int error;
  386. /*
  387. * Due to the order of unstuffing files and ->nopage(), we can be
  388. * asked for a zero page in the case of a stuffed file being extended,
  389. * so we need to supply one here. It doesn't happen often.
  390. */
  391. if (unlikely(page->index)) {
  392. zero_user_page(page, 0, PAGE_CACHE_SIZE, KM_USER0);
  393. return 0;
  394. }
  395. error = gfs2_meta_inode_buffer(ip, &dibh);
  396. if (error)
  397. return error;
  398. kaddr = kmap_atomic(page, KM_USER0);
  399. memcpy(kaddr, dibh->b_data + sizeof(struct gfs2_dinode),
  400. ip->i_di.di_size);
  401. memset(kaddr + ip->i_di.di_size, 0, PAGE_CACHE_SIZE - ip->i_di.di_size);
  402. kunmap_atomic(kaddr, KM_USER0);
  403. flush_dcache_page(page);
  404. brelse(dibh);
  405. SetPageUptodate(page);
  406. return 0;
  407. }
  408. /**
  409. * __gfs2_readpage - readpage
  410. * @file: The file to read a page for
  411. * @page: The page to read
  412. *
  413. * This is the core of gfs2's readpage. Its used by the internal file
  414. * reading code as in that case we already hold the glock. Also its
  415. * called by gfs2_readpage() once the required lock has been granted.
  416. *
  417. */
  418. static int __gfs2_readpage(void *file, struct page *page)
  419. {
  420. struct gfs2_inode *ip = GFS2_I(page->mapping->host);
  421. struct gfs2_sbd *sdp = GFS2_SB(page->mapping->host);
  422. int error;
  423. if (gfs2_is_stuffed(ip)) {
  424. error = stuffed_readpage(ip, page);
  425. unlock_page(page);
  426. } else {
  427. error = mpage_readpage(page, gfs2_block_map);
  428. }
  429. if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
  430. return -EIO;
  431. return error;
  432. }
  433. /**
  434. * gfs2_readpage - read a page of a file
  435. * @file: The file to read
  436. * @page: The page of the file
  437. *
  438. * This deals with the locking required. We use a trylock in order to
  439. * avoid the page lock / glock ordering problems returning AOP_TRUNCATED_PAGE
  440. * in the event that we are unable to get the lock.
  441. */
  442. static int gfs2_readpage(struct file *file, struct page *page)
  443. {
  444. struct gfs2_inode *ip = GFS2_I(page->mapping->host);
  445. struct gfs2_holder gh;
  446. int error;
  447. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME|LM_FLAG_TRY_1CB, &gh);
  448. error = gfs2_glock_nq_atime(&gh);
  449. if (unlikely(error)) {
  450. unlock_page(page);
  451. goto out;
  452. }
  453. error = __gfs2_readpage(file, page);
  454. gfs2_glock_dq(&gh);
  455. out:
  456. gfs2_holder_uninit(&gh);
  457. if (error == GLR_TRYFAILED) {
  458. yield();
  459. return AOP_TRUNCATED_PAGE;
  460. }
  461. return error;
  462. }
  463. /**
  464. * gfs2_internal_read - read an internal file
  465. * @ip: The gfs2 inode
  466. * @ra_state: The readahead state (or NULL for no readahead)
  467. * @buf: The buffer to fill
  468. * @pos: The file position
  469. * @size: The amount to read
  470. *
  471. */
  472. int gfs2_internal_read(struct gfs2_inode *ip, struct file_ra_state *ra_state,
  473. char *buf, loff_t *pos, unsigned size)
  474. {
  475. struct address_space *mapping = ip->i_inode.i_mapping;
  476. unsigned long index = *pos / PAGE_CACHE_SIZE;
  477. unsigned offset = *pos & (PAGE_CACHE_SIZE - 1);
  478. unsigned copied = 0;
  479. unsigned amt;
  480. struct page *page;
  481. void *p;
  482. do {
  483. amt = size - copied;
  484. if (offset + size > PAGE_CACHE_SIZE)
  485. amt = PAGE_CACHE_SIZE - offset;
  486. page = read_cache_page(mapping, index, __gfs2_readpage, NULL);
  487. if (IS_ERR(page))
  488. return PTR_ERR(page);
  489. p = kmap_atomic(page, KM_USER0);
  490. memcpy(buf + copied, p + offset, amt);
  491. kunmap_atomic(p, KM_USER0);
  492. mark_page_accessed(page);
  493. page_cache_release(page);
  494. copied += amt;
  495. index++;
  496. offset = 0;
  497. } while(copied < size);
  498. (*pos) += size;
  499. return size;
  500. }
  501. /**
  502. * gfs2_readpages - Read a bunch of pages at once
  503. *
  504. * Some notes:
  505. * 1. This is only for readahead, so we can simply ignore any things
  506. * which are slightly inconvenient (such as locking conflicts between
  507. * the page lock and the glock) and return having done no I/O. Its
  508. * obviously not something we'd want to do on too regular a basis.
  509. * Any I/O we ignore at this time will be done via readpage later.
  510. * 2. We don't handle stuffed files here we let readpage do the honours.
  511. * 3. mpage_readpages() does most of the heavy lifting in the common case.
  512. * 4. gfs2_block_map() is relied upon to set BH_Boundary in the right places.
  513. */
  514. static int gfs2_readpages(struct file *file, struct address_space *mapping,
  515. struct list_head *pages, unsigned nr_pages)
  516. {
  517. struct inode *inode = mapping->host;
  518. struct gfs2_inode *ip = GFS2_I(inode);
  519. struct gfs2_sbd *sdp = GFS2_SB(inode);
  520. struct gfs2_holder gh;
  521. int ret;
  522. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &gh);
  523. ret = gfs2_glock_nq_atime(&gh);
  524. if (unlikely(ret))
  525. goto out_uninit;
  526. if (!gfs2_is_stuffed(ip))
  527. ret = mpage_readpages(mapping, pages, nr_pages, gfs2_block_map);
  528. gfs2_glock_dq(&gh);
  529. out_uninit:
  530. gfs2_holder_uninit(&gh);
  531. if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
  532. ret = -EIO;
  533. return ret;
  534. }
  535. /**
  536. * gfs2_write_begin - Begin to write to a file
  537. * @file: The file to write to
  538. * @mapping: The mapping in which to write
  539. * @pos: The file offset at which to start writing
  540. * @len: Length of the write
  541. * @flags: Various flags
  542. * @pagep: Pointer to return the page
  543. * @fsdata: Pointer to return fs data (unused by GFS2)
  544. *
  545. * Returns: errno
  546. */
  547. static int gfs2_write_begin(struct file *file, struct address_space *mapping,
  548. loff_t pos, unsigned len, unsigned flags,
  549. struct page **pagep, void **fsdata)
  550. {
  551. struct gfs2_inode *ip = GFS2_I(mapping->host);
  552. struct gfs2_sbd *sdp = GFS2_SB(mapping->host);
  553. unsigned int data_blocks, ind_blocks, rblocks;
  554. int alloc_required;
  555. int error = 0;
  556. struct gfs2_alloc *al;
  557. pgoff_t index = pos >> PAGE_CACHE_SHIFT;
  558. unsigned from = pos & (PAGE_CACHE_SIZE - 1);
  559. unsigned to = from + len;
  560. struct page *page;
  561. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, GL_ATIME, &ip->i_gh);
  562. error = gfs2_glock_nq_atime(&ip->i_gh);
  563. if (unlikely(error))
  564. goto out_uninit;
  565. gfs2_write_calc_reserv(ip, len, &data_blocks, &ind_blocks);
  566. error = gfs2_write_alloc_required(ip, pos, len, &alloc_required);
  567. if (error)
  568. goto out_unlock;
  569. ip->i_alloc.al_requested = 0;
  570. if (alloc_required) {
  571. al = gfs2_alloc_get(ip);
  572. error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
  573. if (error)
  574. goto out_alloc_put;
  575. error = gfs2_quota_check(ip, ip->i_inode.i_uid, ip->i_inode.i_gid);
  576. if (error)
  577. goto out_qunlock;
  578. al->al_requested = data_blocks + ind_blocks;
  579. error = gfs2_inplace_reserve(ip);
  580. if (error)
  581. goto out_qunlock;
  582. }
  583. rblocks = RES_DINODE + ind_blocks;
  584. if (gfs2_is_jdata(ip))
  585. rblocks += data_blocks ? data_blocks : 1;
  586. if (ind_blocks || data_blocks)
  587. rblocks += RES_STATFS + RES_QUOTA;
  588. error = gfs2_trans_begin(sdp, rblocks,
  589. PAGE_CACHE_SIZE/sdp->sd_sb.sb_bsize);
  590. if (error)
  591. goto out_trans_fail;
  592. error = -ENOMEM;
  593. page = __grab_cache_page(mapping, index);
  594. *pagep = page;
  595. if (unlikely(!page))
  596. goto out_endtrans;
  597. if (gfs2_is_stuffed(ip)) {
  598. error = 0;
  599. if (pos + len > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) {
  600. error = gfs2_unstuff_dinode(ip, page);
  601. if (error == 0)
  602. goto prepare_write;
  603. } else if (!PageUptodate(page)) {
  604. error = stuffed_readpage(ip, page);
  605. }
  606. goto out;
  607. }
  608. prepare_write:
  609. error = block_prepare_write(page, from, to, gfs2_block_map);
  610. out:
  611. if (error == 0)
  612. return 0;
  613. page_cache_release(page);
  614. if (pos + len > ip->i_inode.i_size)
  615. vmtruncate(&ip->i_inode, ip->i_inode.i_size);
  616. out_endtrans:
  617. gfs2_trans_end(sdp);
  618. out_trans_fail:
  619. if (alloc_required) {
  620. gfs2_inplace_release(ip);
  621. out_qunlock:
  622. gfs2_quota_unlock(ip);
  623. out_alloc_put:
  624. gfs2_alloc_put(ip);
  625. }
  626. out_unlock:
  627. gfs2_glock_dq(&ip->i_gh);
  628. out_uninit:
  629. gfs2_holder_uninit(&ip->i_gh);
  630. return error;
  631. }
  632. /**
  633. * adjust_fs_space - Adjusts the free space available due to gfs2_grow
  634. * @inode: the rindex inode
  635. */
  636. static void adjust_fs_space(struct inode *inode)
  637. {
  638. struct gfs2_sbd *sdp = inode->i_sb->s_fs_info;
  639. struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
  640. struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
  641. u64 fs_total, new_free;
  642. /* Total up the file system space, according to the latest rindex. */
  643. fs_total = gfs2_ri_total(sdp);
  644. spin_lock(&sdp->sd_statfs_spin);
  645. if (fs_total > (m_sc->sc_total + l_sc->sc_total))
  646. new_free = fs_total - (m_sc->sc_total + l_sc->sc_total);
  647. else
  648. new_free = 0;
  649. spin_unlock(&sdp->sd_statfs_spin);
  650. fs_warn(sdp, "File system extended by %llu blocks.\n",
  651. (unsigned long long)new_free);
  652. gfs2_statfs_change(sdp, new_free, new_free, 0);
  653. }
  654. /**
  655. * gfs2_stuffed_write_end - Write end for stuffed files
  656. * @inode: The inode
  657. * @dibh: The buffer_head containing the on-disk inode
  658. * @pos: The file position
  659. * @len: The length of the write
  660. * @copied: How much was actually copied by the VFS
  661. * @page: The page
  662. *
  663. * This copies the data from the page into the inode block after
  664. * the inode data structure itself.
  665. *
  666. * Returns: errno
  667. */
  668. static int gfs2_stuffed_write_end(struct inode *inode, struct buffer_head *dibh,
  669. loff_t pos, unsigned len, unsigned copied,
  670. struct page *page)
  671. {
  672. struct gfs2_inode *ip = GFS2_I(inode);
  673. struct gfs2_sbd *sdp = GFS2_SB(inode);
  674. u64 to = pos + copied;
  675. void *kaddr;
  676. unsigned char *buf = dibh->b_data + sizeof(struct gfs2_dinode);
  677. struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
  678. BUG_ON((pos + len) > (dibh->b_size - sizeof(struct gfs2_dinode)));
  679. kaddr = kmap_atomic(page, KM_USER0);
  680. memcpy(buf + pos, kaddr + pos, copied);
  681. memset(kaddr + pos + copied, 0, len - copied);
  682. flush_dcache_page(page);
  683. kunmap_atomic(kaddr, KM_USER0);
  684. if (!PageUptodate(page))
  685. SetPageUptodate(page);
  686. unlock_page(page);
  687. page_cache_release(page);
  688. if (inode->i_size < to) {
  689. i_size_write(inode, to);
  690. ip->i_di.di_size = inode->i_size;
  691. di->di_size = cpu_to_be64(inode->i_size);
  692. mark_inode_dirty(inode);
  693. }
  694. if (inode == sdp->sd_rindex)
  695. adjust_fs_space(inode);
  696. brelse(dibh);
  697. gfs2_trans_end(sdp);
  698. gfs2_glock_dq(&ip->i_gh);
  699. gfs2_holder_uninit(&ip->i_gh);
  700. return copied;
  701. }
  702. /**
  703. * gfs2_write_end
  704. * @file: The file to write to
  705. * @mapping: The address space to write to
  706. * @pos: The file position
  707. * @len: The length of the data
  708. * @copied:
  709. * @page: The page that has been written
  710. * @fsdata: The fsdata (unused in GFS2)
  711. *
  712. * The main write_end function for GFS2. We have a separate one for
  713. * stuffed files as they are slightly different, otherwise we just
  714. * put our locking around the VFS provided functions.
  715. *
  716. * Returns: errno
  717. */
  718. static int gfs2_write_end(struct file *file, struct address_space *mapping,
  719. loff_t pos, unsigned len, unsigned copied,
  720. struct page *page, void *fsdata)
  721. {
  722. struct inode *inode = page->mapping->host;
  723. struct gfs2_inode *ip = GFS2_I(inode);
  724. struct gfs2_sbd *sdp = GFS2_SB(inode);
  725. struct buffer_head *dibh;
  726. struct gfs2_alloc *al = &ip->i_alloc;
  727. struct gfs2_dinode *di;
  728. unsigned int from = pos & (PAGE_CACHE_SIZE - 1);
  729. unsigned int to = from + len;
  730. int ret;
  731. BUG_ON(gfs2_glock_is_locked_by_me(ip->i_gl) == 0);
  732. ret = gfs2_meta_inode_buffer(ip, &dibh);
  733. if (unlikely(ret)) {
  734. unlock_page(page);
  735. page_cache_release(page);
  736. goto failed;
  737. }
  738. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  739. if (gfs2_is_stuffed(ip))
  740. return gfs2_stuffed_write_end(inode, dibh, pos, len, copied, page);
  741. if (!gfs2_is_writeback(ip))
  742. gfs2_page_add_databufs(ip, page, from, to);
  743. ret = generic_write_end(file, mapping, pos, len, copied, page, fsdata);
  744. if (likely(ret >= 0)) {
  745. copied = ret;
  746. if ((pos + copied) > inode->i_size) {
  747. di = (struct gfs2_dinode *)dibh->b_data;
  748. ip->i_di.di_size = inode->i_size;
  749. di->di_size = cpu_to_be64(inode->i_size);
  750. mark_inode_dirty(inode);
  751. }
  752. }
  753. if (inode == sdp->sd_rindex)
  754. adjust_fs_space(inode);
  755. brelse(dibh);
  756. gfs2_trans_end(sdp);
  757. failed:
  758. if (al->al_requested) {
  759. gfs2_inplace_release(ip);
  760. gfs2_quota_unlock(ip);
  761. gfs2_alloc_put(ip);
  762. }
  763. gfs2_glock_dq(&ip->i_gh);
  764. gfs2_holder_uninit(&ip->i_gh);
  765. return ret;
  766. }
  767. /**
  768. * gfs2_set_page_dirty - Page dirtying function
  769. * @page: The page to dirty
  770. *
  771. * Returns: 1 if it dirtyed the page, or 0 otherwise
  772. */
  773. static int gfs2_set_page_dirty(struct page *page)
  774. {
  775. SetPageChecked(page);
  776. return __set_page_dirty_buffers(page);
  777. }
  778. /**
  779. * gfs2_bmap - Block map function
  780. * @mapping: Address space info
  781. * @lblock: The block to map
  782. *
  783. * Returns: The disk address for the block or 0 on hole or error
  784. */
  785. static sector_t gfs2_bmap(struct address_space *mapping, sector_t lblock)
  786. {
  787. struct gfs2_inode *ip = GFS2_I(mapping->host);
  788. struct gfs2_holder i_gh;
  789. sector_t dblock = 0;
  790. int error;
  791. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
  792. if (error)
  793. return 0;
  794. if (!gfs2_is_stuffed(ip))
  795. dblock = generic_block_bmap(mapping, lblock, gfs2_block_map);
  796. gfs2_glock_dq_uninit(&i_gh);
  797. return dblock;
  798. }
  799. static void gfs2_discard(struct gfs2_sbd *sdp, struct buffer_head *bh)
  800. {
  801. struct gfs2_bufdata *bd;
  802. lock_buffer(bh);
  803. gfs2_log_lock(sdp);
  804. clear_buffer_dirty(bh);
  805. bd = bh->b_private;
  806. if (bd) {
  807. if (!list_empty(&bd->bd_le.le_list) && !buffer_pinned(bh))
  808. list_del_init(&bd->bd_le.le_list);
  809. else
  810. gfs2_remove_from_journal(bh, current->journal_info, 0);
  811. }
  812. bh->b_bdev = NULL;
  813. clear_buffer_mapped(bh);
  814. clear_buffer_req(bh);
  815. clear_buffer_new(bh);
  816. gfs2_log_unlock(sdp);
  817. unlock_buffer(bh);
  818. }
  819. static void gfs2_invalidatepage(struct page *page, unsigned long offset)
  820. {
  821. struct gfs2_sbd *sdp = GFS2_SB(page->mapping->host);
  822. struct buffer_head *bh, *head;
  823. unsigned long pos = 0;
  824. BUG_ON(!PageLocked(page));
  825. if (offset == 0)
  826. ClearPageChecked(page);
  827. if (!page_has_buffers(page))
  828. goto out;
  829. bh = head = page_buffers(page);
  830. do {
  831. if (offset <= pos)
  832. gfs2_discard(sdp, bh);
  833. pos += bh->b_size;
  834. bh = bh->b_this_page;
  835. } while (bh != head);
  836. out:
  837. if (offset == 0)
  838. try_to_release_page(page, 0);
  839. }
  840. /**
  841. * gfs2_ok_for_dio - check that dio is valid on this file
  842. * @ip: The inode
  843. * @rw: READ or WRITE
  844. * @offset: The offset at which we are reading or writing
  845. *
  846. * Returns: 0 (to ignore the i/o request and thus fall back to buffered i/o)
  847. * 1 (to accept the i/o request)
  848. */
  849. static int gfs2_ok_for_dio(struct gfs2_inode *ip, int rw, loff_t offset)
  850. {
  851. /*
  852. * Should we return an error here? I can't see that O_DIRECT for
  853. * a stuffed file makes any sense. For now we'll silently fall
  854. * back to buffered I/O
  855. */
  856. if (gfs2_is_stuffed(ip))
  857. return 0;
  858. if (offset > i_size_read(&ip->i_inode))
  859. return 0;
  860. return 1;
  861. }
  862. static ssize_t gfs2_direct_IO(int rw, struct kiocb *iocb,
  863. const struct iovec *iov, loff_t offset,
  864. unsigned long nr_segs)
  865. {
  866. struct file *file = iocb->ki_filp;
  867. struct inode *inode = file->f_mapping->host;
  868. struct gfs2_inode *ip = GFS2_I(inode);
  869. struct gfs2_holder gh;
  870. int rv;
  871. /*
  872. * Deferred lock, even if its a write, since we do no allocation
  873. * on this path. All we need change is atime, and this lock mode
  874. * ensures that other nodes have flushed their buffered read caches
  875. * (i.e. their page cache entries for this inode). We do not,
  876. * unfortunately have the option of only flushing a range like
  877. * the VFS does.
  878. */
  879. gfs2_holder_init(ip->i_gl, LM_ST_DEFERRED, GL_ATIME, &gh);
  880. rv = gfs2_glock_nq_atime(&gh);
  881. if (rv)
  882. return rv;
  883. rv = gfs2_ok_for_dio(ip, rw, offset);
  884. if (rv != 1)
  885. goto out; /* dio not valid, fall back to buffered i/o */
  886. rv = blockdev_direct_IO_no_locking(rw, iocb, inode, inode->i_sb->s_bdev,
  887. iov, offset, nr_segs,
  888. gfs2_get_block_direct, NULL);
  889. out:
  890. gfs2_glock_dq_m(1, &gh);
  891. gfs2_holder_uninit(&gh);
  892. return rv;
  893. }
  894. /**
  895. * gfs2_releasepage - free the metadata associated with a page
  896. * @page: the page that's being released
  897. * @gfp_mask: passed from Linux VFS, ignored by us
  898. *
  899. * Call try_to_free_buffers() if the buffers in this page can be
  900. * released.
  901. *
  902. * Returns: 0
  903. */
  904. int gfs2_releasepage(struct page *page, gfp_t gfp_mask)
  905. {
  906. struct inode *aspace = page->mapping->host;
  907. struct gfs2_sbd *sdp = aspace->i_sb->s_fs_info;
  908. struct buffer_head *bh, *head;
  909. struct gfs2_bufdata *bd;
  910. if (!page_has_buffers(page))
  911. return 0;
  912. gfs2_log_lock(sdp);
  913. head = bh = page_buffers(page);
  914. do {
  915. if (atomic_read(&bh->b_count))
  916. goto cannot_release;
  917. bd = bh->b_private;
  918. if (bd && bd->bd_ail)
  919. goto cannot_release;
  920. gfs2_assert_warn(sdp, !buffer_pinned(bh));
  921. gfs2_assert_warn(sdp, !buffer_dirty(bh));
  922. bh = bh->b_this_page;
  923. } while(bh != head);
  924. gfs2_log_unlock(sdp);
  925. head = bh = page_buffers(page);
  926. do {
  927. gfs2_log_lock(sdp);
  928. bd = bh->b_private;
  929. if (bd) {
  930. gfs2_assert_warn(sdp, bd->bd_bh == bh);
  931. gfs2_assert_warn(sdp, list_empty(&bd->bd_list_tr));
  932. if (!list_empty(&bd->bd_le.le_list)) {
  933. if (!buffer_pinned(bh))
  934. list_del_init(&bd->bd_le.le_list);
  935. else
  936. bd = NULL;
  937. }
  938. if (bd)
  939. bd->bd_bh = NULL;
  940. bh->b_private = NULL;
  941. }
  942. gfs2_log_unlock(sdp);
  943. if (bd)
  944. kmem_cache_free(gfs2_bufdata_cachep, bd);
  945. bh = bh->b_this_page;
  946. } while (bh != head);
  947. return try_to_free_buffers(page);
  948. cannot_release:
  949. gfs2_log_unlock(sdp);
  950. return 0;
  951. }
  952. static const struct address_space_operations gfs2_writeback_aops = {
  953. .writepage = gfs2_writeback_writepage,
  954. .writepages = gfs2_writeback_writepages,
  955. .readpage = gfs2_readpage,
  956. .readpages = gfs2_readpages,
  957. .sync_page = block_sync_page,
  958. .write_begin = gfs2_write_begin,
  959. .write_end = gfs2_write_end,
  960. .bmap = gfs2_bmap,
  961. .invalidatepage = gfs2_invalidatepage,
  962. .releasepage = gfs2_releasepage,
  963. .direct_IO = gfs2_direct_IO,
  964. .migratepage = buffer_migrate_page,
  965. };
  966. static const struct address_space_operations gfs2_ordered_aops = {
  967. .writepage = gfs2_ordered_writepage,
  968. .readpage = gfs2_readpage,
  969. .readpages = gfs2_readpages,
  970. .sync_page = block_sync_page,
  971. .write_begin = gfs2_write_begin,
  972. .write_end = gfs2_write_end,
  973. .set_page_dirty = gfs2_set_page_dirty,
  974. .bmap = gfs2_bmap,
  975. .invalidatepage = gfs2_invalidatepage,
  976. .releasepage = gfs2_releasepage,
  977. .direct_IO = gfs2_direct_IO,
  978. .migratepage = buffer_migrate_page,
  979. };
  980. static const struct address_space_operations gfs2_jdata_aops = {
  981. .writepage = gfs2_jdata_writepage,
  982. .writepages = gfs2_jdata_writepages,
  983. .readpage = gfs2_readpage,
  984. .readpages = gfs2_readpages,
  985. .sync_page = block_sync_page,
  986. .write_begin = gfs2_write_begin,
  987. .write_end = gfs2_write_end,
  988. .set_page_dirty = gfs2_set_page_dirty,
  989. .bmap = gfs2_bmap,
  990. .invalidatepage = gfs2_invalidatepage,
  991. .releasepage = gfs2_releasepage,
  992. };
  993. void gfs2_set_aops(struct inode *inode)
  994. {
  995. struct gfs2_inode *ip = GFS2_I(inode);
  996. if (gfs2_is_writeback(ip))
  997. inode->i_mapping->a_ops = &gfs2_writeback_aops;
  998. else if (gfs2_is_ordered(ip))
  999. inode->i_mapping->a_ops = &gfs2_ordered_aops;
  1000. else if (gfs2_is_jdata(ip))
  1001. inode->i_mapping->a_ops = &gfs2_jdata_aops;
  1002. else
  1003. BUG();
  1004. }