aops.c 31 KB

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