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/uio.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. * @wbc: The writeback control
  155. *
  156. * Returns: errno
  157. *
  158. */
  159. static int gfs2_jdata_writepage(struct page *page, struct writeback_control *wbc)
  160. {
  161. struct inode *inode = page->mapping->host;
  162. struct gfs2_sbd *sdp = GFS2_SB(inode);
  163. int ret;
  164. int done_trans = 0;
  165. if (PageChecked(page)) {
  166. if (wbc->sync_mode != WB_SYNC_ALL)
  167. goto out_ignore;
  168. ret = gfs2_trans_begin(sdp, RES_DINODE + 1, 0);
  169. if (ret)
  170. goto out_ignore;
  171. done_trans = 1;
  172. }
  173. ret = gfs2_writepage_common(page, wbc);
  174. if (ret > 0)
  175. ret = __gfs2_jdata_writepage(page, wbc);
  176. if (done_trans)
  177. gfs2_trans_end(sdp);
  178. return ret;
  179. out_ignore:
  180. redirty_page_for_writepage(wbc, page);
  181. unlock_page(page);
  182. return 0;
  183. }
  184. /**
  185. * gfs2_writepages - Write a bunch of dirty pages back to disk
  186. * @mapping: The mapping to write
  187. * @wbc: Write-back control
  188. *
  189. * Used for both ordered and writeback modes.
  190. */
  191. static int gfs2_writepages(struct address_space *mapping,
  192. struct writeback_control *wbc)
  193. {
  194. return mpage_writepages(mapping, wbc, gfs2_get_block_noalloc);
  195. }
  196. /**
  197. * gfs2_write_jdata_pagevec - Write back a pagevec's worth of pages
  198. * @mapping: The mapping
  199. * @wbc: The writeback control
  200. * @pvec: The vector of pages
  201. * @nr_pages: The number of pages to write
  202. * @end: End position
  203. * @done_index: Page index
  204. *
  205. * Returns: non-zero if loop should terminate, zero otherwise
  206. */
  207. static int gfs2_write_jdata_pagevec(struct address_space *mapping,
  208. struct writeback_control *wbc,
  209. struct pagevec *pvec,
  210. int nr_pages, pgoff_t end,
  211. pgoff_t *done_index)
  212. {
  213. struct inode *inode = mapping->host;
  214. struct gfs2_sbd *sdp = GFS2_SB(inode);
  215. unsigned nrblocks = nr_pages * (PAGE_CACHE_SIZE/inode->i_sb->s_blocksize);
  216. int i;
  217. int ret;
  218. ret = gfs2_trans_begin(sdp, nrblocks, nrblocks);
  219. if (ret < 0)
  220. return ret;
  221. for(i = 0; i < nr_pages; i++) {
  222. struct page *page = pvec->pages[i];
  223. /*
  224. * At this point, the page may be truncated or
  225. * invalidated (changing page->mapping to NULL), or
  226. * even swizzled back from swapper_space to tmpfs file
  227. * mapping. However, page->index will not change
  228. * because we have a reference on the page.
  229. */
  230. if (page->index > end) {
  231. /*
  232. * can't be range_cyclic (1st pass) because
  233. * end == -1 in that case.
  234. */
  235. ret = 1;
  236. break;
  237. }
  238. *done_index = page->index;
  239. lock_page(page);
  240. if (unlikely(page->mapping != mapping)) {
  241. continue_unlock:
  242. unlock_page(page);
  243. continue;
  244. }
  245. if (!PageDirty(page)) {
  246. /* someone wrote it for us */
  247. goto continue_unlock;
  248. }
  249. if (PageWriteback(page)) {
  250. if (wbc->sync_mode != WB_SYNC_NONE)
  251. wait_on_page_writeback(page);
  252. else
  253. goto continue_unlock;
  254. }
  255. BUG_ON(PageWriteback(page));
  256. if (!clear_page_dirty_for_io(page))
  257. goto continue_unlock;
  258. trace_wbc_writepage(wbc, inode_to_bdi(inode));
  259. ret = __gfs2_jdata_writepage(page, wbc);
  260. if (unlikely(ret)) {
  261. if (ret == AOP_WRITEPAGE_ACTIVATE) {
  262. unlock_page(page);
  263. ret = 0;
  264. } else {
  265. /*
  266. * done_index is set past this page,
  267. * so media errors will not choke
  268. * background writeout for the entire
  269. * file. This has consequences for
  270. * range_cyclic semantics (ie. it may
  271. * not be suitable for data integrity
  272. * writeout).
  273. */
  274. *done_index = page->index + 1;
  275. ret = 1;
  276. break;
  277. }
  278. }
  279. /*
  280. * We stop writing back only if we are not doing
  281. * integrity sync. In case of integrity sync we have to
  282. * keep going until we have written all the pages
  283. * we tagged for writeback prior to entering this loop.
  284. */
  285. if (--wbc->nr_to_write <= 0 && wbc->sync_mode == WB_SYNC_NONE) {
  286. ret = 1;
  287. break;
  288. }
  289. }
  290. gfs2_trans_end(sdp);
  291. return ret;
  292. }
  293. /**
  294. * gfs2_write_cache_jdata - Like write_cache_pages but different
  295. * @mapping: The mapping to write
  296. * @wbc: The writeback control
  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, NORMAL_FLUSH);
  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. page_cache_release(page);
  511. copied += amt;
  512. index++;
  513. offset = 0;
  514. } while(copied < size);
  515. (*pos) += size;
  516. return size;
  517. }
  518. /**
  519. * gfs2_readpages - Read a bunch of pages at once
  520. * @file: The file to read from
  521. * @mapping: Address space info
  522. * @pages: List of pages to read
  523. * @nr_pages: Number of pages to read
  524. *
  525. * Some notes:
  526. * 1. This is only for readahead, so we can simply ignore any things
  527. * which are slightly inconvenient (such as locking conflicts between
  528. * the page lock and the glock) and return having done no I/O. Its
  529. * obviously not something we'd want to do on too regular a basis.
  530. * Any I/O we ignore at this time will be done via readpage later.
  531. * 2. We don't handle stuffed files here we let readpage do the honours.
  532. * 3. mpage_readpages() does most of the heavy lifting in the common case.
  533. * 4. gfs2_block_map() is relied upon to set BH_Boundary in the right places.
  534. */
  535. static int gfs2_readpages(struct file *file, struct address_space *mapping,
  536. struct list_head *pages, unsigned nr_pages)
  537. {
  538. struct inode *inode = mapping->host;
  539. struct gfs2_inode *ip = GFS2_I(inode);
  540. struct gfs2_sbd *sdp = GFS2_SB(inode);
  541. struct gfs2_holder gh;
  542. int ret;
  543. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
  544. ret = gfs2_glock_nq(&gh);
  545. if (unlikely(ret))
  546. goto out_uninit;
  547. if (!gfs2_is_stuffed(ip))
  548. ret = mpage_readpages(mapping, pages, nr_pages, gfs2_block_map);
  549. gfs2_glock_dq(&gh);
  550. out_uninit:
  551. gfs2_holder_uninit(&gh);
  552. if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
  553. ret = -EIO;
  554. return ret;
  555. }
  556. /**
  557. * gfs2_write_begin - Begin to write to a file
  558. * @file: The file to write to
  559. * @mapping: The mapping in which to write
  560. * @pos: The file offset at which to start writing
  561. * @len: Length of the write
  562. * @flags: Various flags
  563. * @pagep: Pointer to return the page
  564. * @fsdata: Pointer to return fs data (unused by GFS2)
  565. *
  566. * Returns: errno
  567. */
  568. static int gfs2_write_begin(struct file *file, struct address_space *mapping,
  569. loff_t pos, unsigned len, unsigned flags,
  570. struct page **pagep, void **fsdata)
  571. {
  572. struct gfs2_inode *ip = GFS2_I(mapping->host);
  573. struct gfs2_sbd *sdp = GFS2_SB(mapping->host);
  574. struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode);
  575. unsigned int data_blocks = 0, ind_blocks = 0, rblocks;
  576. unsigned requested = 0;
  577. int alloc_required;
  578. int error = 0;
  579. pgoff_t index = pos >> PAGE_CACHE_SHIFT;
  580. unsigned from = pos & (PAGE_CACHE_SIZE - 1);
  581. struct page *page;
  582. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &ip->i_gh);
  583. error = gfs2_glock_nq(&ip->i_gh);
  584. if (unlikely(error))
  585. goto out_uninit;
  586. if (&ip->i_inode == sdp->sd_rindex) {
  587. error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE,
  588. GL_NOCACHE, &m_ip->i_gh);
  589. if (unlikely(error)) {
  590. gfs2_glock_dq(&ip->i_gh);
  591. goto out_uninit;
  592. }
  593. }
  594. alloc_required = gfs2_write_alloc_required(ip, pos, len);
  595. if (alloc_required || gfs2_is_jdata(ip))
  596. gfs2_write_calc_reserv(ip, len, &data_blocks, &ind_blocks);
  597. if (alloc_required) {
  598. struct gfs2_alloc_parms ap = { .aflags = 0, };
  599. requested = data_blocks + ind_blocks;
  600. ap.target = requested;
  601. error = gfs2_quota_lock_check(ip, &ap);
  602. if (error)
  603. goto out_unlock;
  604. error = gfs2_inplace_reserve(ip, &ap);
  605. if (error)
  606. goto out_qunlock;
  607. }
  608. rblocks = RES_DINODE + ind_blocks;
  609. if (gfs2_is_jdata(ip))
  610. rblocks += data_blocks ? data_blocks : 1;
  611. if (ind_blocks || data_blocks)
  612. rblocks += RES_STATFS + RES_QUOTA;
  613. if (&ip->i_inode == sdp->sd_rindex)
  614. rblocks += 2 * RES_STATFS;
  615. if (alloc_required)
  616. rblocks += gfs2_rg_blocks(ip, requested);
  617. error = gfs2_trans_begin(sdp, rblocks,
  618. PAGE_CACHE_SIZE/sdp->sd_sb.sb_bsize);
  619. if (error)
  620. goto out_trans_fail;
  621. error = -ENOMEM;
  622. flags |= AOP_FLAG_NOFS;
  623. page = grab_cache_page_write_begin(mapping, index, flags);
  624. *pagep = page;
  625. if (unlikely(!page))
  626. goto out_endtrans;
  627. if (gfs2_is_stuffed(ip)) {
  628. error = 0;
  629. if (pos + len > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) {
  630. error = gfs2_unstuff_dinode(ip, page);
  631. if (error == 0)
  632. goto prepare_write;
  633. } else if (!PageUptodate(page)) {
  634. error = stuffed_readpage(ip, page);
  635. }
  636. goto out;
  637. }
  638. prepare_write:
  639. error = __block_write_begin(page, from, len, gfs2_block_map);
  640. out:
  641. if (error == 0)
  642. return 0;
  643. unlock_page(page);
  644. page_cache_release(page);
  645. gfs2_trans_end(sdp);
  646. if (pos + len > ip->i_inode.i_size)
  647. gfs2_trim_blocks(&ip->i_inode);
  648. goto out_trans_fail;
  649. out_endtrans:
  650. gfs2_trans_end(sdp);
  651. out_trans_fail:
  652. if (alloc_required) {
  653. gfs2_inplace_release(ip);
  654. out_qunlock:
  655. gfs2_quota_unlock(ip);
  656. }
  657. out_unlock:
  658. if (&ip->i_inode == sdp->sd_rindex) {
  659. gfs2_glock_dq(&m_ip->i_gh);
  660. gfs2_holder_uninit(&m_ip->i_gh);
  661. }
  662. gfs2_glock_dq(&ip->i_gh);
  663. out_uninit:
  664. gfs2_holder_uninit(&ip->i_gh);
  665. return error;
  666. }
  667. /**
  668. * adjust_fs_space - Adjusts the free space available due to gfs2_grow
  669. * @inode: the rindex inode
  670. */
  671. static void adjust_fs_space(struct inode *inode)
  672. {
  673. struct gfs2_sbd *sdp = inode->i_sb->s_fs_info;
  674. struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode);
  675. struct gfs2_inode *l_ip = GFS2_I(sdp->sd_sc_inode);
  676. struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
  677. struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
  678. struct buffer_head *m_bh, *l_bh;
  679. u64 fs_total, new_free;
  680. /* Total up the file system space, according to the latest rindex. */
  681. fs_total = gfs2_ri_total(sdp);
  682. if (gfs2_meta_inode_buffer(m_ip, &m_bh) != 0)
  683. return;
  684. spin_lock(&sdp->sd_statfs_spin);
  685. gfs2_statfs_change_in(m_sc, m_bh->b_data +
  686. sizeof(struct gfs2_dinode));
  687. if (fs_total > (m_sc->sc_total + l_sc->sc_total))
  688. new_free = fs_total - (m_sc->sc_total + l_sc->sc_total);
  689. else
  690. new_free = 0;
  691. spin_unlock(&sdp->sd_statfs_spin);
  692. fs_warn(sdp, "File system extended by %llu blocks.\n",
  693. (unsigned long long)new_free);
  694. gfs2_statfs_change(sdp, new_free, new_free, 0);
  695. if (gfs2_meta_inode_buffer(l_ip, &l_bh) != 0)
  696. goto out;
  697. update_statfs(sdp, m_bh, l_bh);
  698. brelse(l_bh);
  699. out:
  700. brelse(m_bh);
  701. }
  702. /**
  703. * gfs2_stuffed_write_end - Write end for stuffed files
  704. * @inode: The inode
  705. * @dibh: The buffer_head containing the on-disk inode
  706. * @pos: The file position
  707. * @len: The length of the write
  708. * @copied: How much was actually copied by the VFS
  709. * @page: The page
  710. *
  711. * This copies the data from the page into the inode block after
  712. * the inode data structure itself.
  713. *
  714. * Returns: errno
  715. */
  716. static int gfs2_stuffed_write_end(struct inode *inode, struct buffer_head *dibh,
  717. loff_t pos, unsigned len, unsigned copied,
  718. struct page *page)
  719. {
  720. struct gfs2_inode *ip = GFS2_I(inode);
  721. struct gfs2_sbd *sdp = GFS2_SB(inode);
  722. struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode);
  723. u64 to = pos + copied;
  724. void *kaddr;
  725. unsigned char *buf = dibh->b_data + sizeof(struct gfs2_dinode);
  726. BUG_ON((pos + len) > (dibh->b_size - sizeof(struct gfs2_dinode)));
  727. kaddr = kmap_atomic(page);
  728. memcpy(buf + pos, kaddr + pos, copied);
  729. memset(kaddr + pos + copied, 0, len - copied);
  730. flush_dcache_page(page);
  731. kunmap_atomic(kaddr);
  732. if (!PageUptodate(page))
  733. SetPageUptodate(page);
  734. unlock_page(page);
  735. page_cache_release(page);
  736. if (copied) {
  737. if (inode->i_size < to)
  738. i_size_write(inode, to);
  739. mark_inode_dirty(inode);
  740. }
  741. if (inode == sdp->sd_rindex) {
  742. adjust_fs_space(inode);
  743. sdp->sd_rindex_uptodate = 0;
  744. }
  745. brelse(dibh);
  746. gfs2_trans_end(sdp);
  747. if (inode == sdp->sd_rindex) {
  748. gfs2_glock_dq(&m_ip->i_gh);
  749. gfs2_holder_uninit(&m_ip->i_gh);
  750. }
  751. gfs2_glock_dq(&ip->i_gh);
  752. gfs2_holder_uninit(&ip->i_gh);
  753. return copied;
  754. }
  755. /**
  756. * gfs2_write_end
  757. * @file: The file to write to
  758. * @mapping: The address space to write to
  759. * @pos: The file position
  760. * @len: The length of the data
  761. * @copied: How much was actually copied by the VFS
  762. * @page: The page that has been written
  763. * @fsdata: The fsdata (unused in GFS2)
  764. *
  765. * The main write_end function for GFS2. We have a separate one for
  766. * stuffed files as they are slightly different, otherwise we just
  767. * put our locking around the VFS provided functions.
  768. *
  769. * Returns: errno
  770. */
  771. static int gfs2_write_end(struct file *file, struct address_space *mapping,
  772. loff_t pos, unsigned len, unsigned copied,
  773. struct page *page, void *fsdata)
  774. {
  775. struct inode *inode = page->mapping->host;
  776. struct gfs2_inode *ip = GFS2_I(inode);
  777. struct gfs2_sbd *sdp = GFS2_SB(inode);
  778. struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode);
  779. struct buffer_head *dibh;
  780. unsigned int from = pos & (PAGE_CACHE_SIZE - 1);
  781. unsigned int to = from + len;
  782. int ret;
  783. struct gfs2_trans *tr = current->journal_info;
  784. BUG_ON(!tr);
  785. BUG_ON(gfs2_glock_is_locked_by_me(ip->i_gl) == NULL);
  786. ret = gfs2_meta_inode_buffer(ip, &dibh);
  787. if (unlikely(ret)) {
  788. unlock_page(page);
  789. page_cache_release(page);
  790. goto failed;
  791. }
  792. if (gfs2_is_stuffed(ip))
  793. return gfs2_stuffed_write_end(inode, dibh, pos, len, copied, page);
  794. if (!gfs2_is_writeback(ip))
  795. gfs2_page_add_databufs(ip, page, from, to);
  796. ret = generic_write_end(file, mapping, pos, len, copied, page, fsdata);
  797. if (tr->tr_num_buf_new)
  798. __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
  799. else
  800. gfs2_trans_add_meta(ip->i_gl, dibh);
  801. if (inode == sdp->sd_rindex) {
  802. adjust_fs_space(inode);
  803. sdp->sd_rindex_uptodate = 0;
  804. }
  805. brelse(dibh);
  806. failed:
  807. gfs2_trans_end(sdp);
  808. gfs2_inplace_release(ip);
  809. if (ip->i_res->rs_qa_qd_num)
  810. gfs2_quota_unlock(ip);
  811. if (inode == sdp->sd_rindex) {
  812. gfs2_glock_dq(&m_ip->i_gh);
  813. gfs2_holder_uninit(&m_ip->i_gh);
  814. }
  815. gfs2_glock_dq(&ip->i_gh);
  816. gfs2_holder_uninit(&ip->i_gh);
  817. return ret;
  818. }
  819. /**
  820. * gfs2_set_page_dirty - Page dirtying function
  821. * @page: The page to dirty
  822. *
  823. * Returns: 1 if it dirtyed the page, or 0 otherwise
  824. */
  825. static int gfs2_set_page_dirty(struct page *page)
  826. {
  827. SetPageChecked(page);
  828. return __set_page_dirty_buffers(page);
  829. }
  830. /**
  831. * gfs2_bmap - Block map function
  832. * @mapping: Address space info
  833. * @lblock: The block to map
  834. *
  835. * Returns: The disk address for the block or 0 on hole or error
  836. */
  837. static sector_t gfs2_bmap(struct address_space *mapping, sector_t lblock)
  838. {
  839. struct gfs2_inode *ip = GFS2_I(mapping->host);
  840. struct gfs2_holder i_gh;
  841. sector_t dblock = 0;
  842. int error;
  843. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
  844. if (error)
  845. return 0;
  846. if (!gfs2_is_stuffed(ip))
  847. dblock = generic_block_bmap(mapping, lblock, gfs2_block_map);
  848. gfs2_glock_dq_uninit(&i_gh);
  849. return dblock;
  850. }
  851. static void gfs2_discard(struct gfs2_sbd *sdp, struct buffer_head *bh)
  852. {
  853. struct gfs2_bufdata *bd;
  854. lock_buffer(bh);
  855. gfs2_log_lock(sdp);
  856. clear_buffer_dirty(bh);
  857. bd = bh->b_private;
  858. if (bd) {
  859. if (!list_empty(&bd->bd_list) && !buffer_pinned(bh))
  860. list_del_init(&bd->bd_list);
  861. else
  862. gfs2_remove_from_journal(bh, current->journal_info, 0);
  863. }
  864. bh->b_bdev = NULL;
  865. clear_buffer_mapped(bh);
  866. clear_buffer_req(bh);
  867. clear_buffer_new(bh);
  868. gfs2_log_unlock(sdp);
  869. unlock_buffer(bh);
  870. }
  871. static void gfs2_invalidatepage(struct page *page, unsigned int offset,
  872. unsigned int length)
  873. {
  874. struct gfs2_sbd *sdp = GFS2_SB(page->mapping->host);
  875. unsigned int stop = offset + length;
  876. int partial_page = (offset || length < PAGE_CACHE_SIZE);
  877. struct buffer_head *bh, *head;
  878. unsigned long pos = 0;
  879. BUG_ON(!PageLocked(page));
  880. if (!partial_page)
  881. ClearPageChecked(page);
  882. if (!page_has_buffers(page))
  883. goto out;
  884. bh = head = page_buffers(page);
  885. do {
  886. if (pos + bh->b_size > stop)
  887. return;
  888. if (offset <= pos)
  889. gfs2_discard(sdp, bh);
  890. pos += bh->b_size;
  891. bh = bh->b_this_page;
  892. } while (bh != head);
  893. out:
  894. if (!partial_page)
  895. try_to_release_page(page, 0);
  896. }
  897. /**
  898. * gfs2_ok_for_dio - check that dio is valid on this file
  899. * @ip: The inode
  900. * @offset: The offset at which we are reading or writing
  901. *
  902. * Returns: 0 (to ignore the i/o request and thus fall back to buffered i/o)
  903. * 1 (to accept the i/o request)
  904. */
  905. static int gfs2_ok_for_dio(struct gfs2_inode *ip, loff_t offset)
  906. {
  907. /*
  908. * Should we return an error here? I can't see that O_DIRECT for
  909. * a stuffed file makes any sense. For now we'll silently fall
  910. * back to buffered I/O
  911. */
  912. if (gfs2_is_stuffed(ip))
  913. return 0;
  914. if (offset >= i_size_read(&ip->i_inode))
  915. return 0;
  916. return 1;
  917. }
  918. static ssize_t gfs2_direct_IO(struct kiocb *iocb, struct iov_iter *iter,
  919. loff_t offset)
  920. {
  921. struct file *file = iocb->ki_filp;
  922. struct inode *inode = file->f_mapping->host;
  923. struct address_space *mapping = inode->i_mapping;
  924. struct gfs2_inode *ip = GFS2_I(inode);
  925. struct gfs2_holder gh;
  926. int rv;
  927. /*
  928. * Deferred lock, even if its a write, since we do no allocation
  929. * on this path. All we need change is atime, and this lock mode
  930. * ensures that other nodes have flushed their buffered read caches
  931. * (i.e. their page cache entries for this inode). We do not,
  932. * unfortunately have the option of only flushing a range like
  933. * the VFS does.
  934. */
  935. gfs2_holder_init(ip->i_gl, LM_ST_DEFERRED, 0, &gh);
  936. rv = gfs2_glock_nq(&gh);
  937. if (rv)
  938. return rv;
  939. rv = gfs2_ok_for_dio(ip, offset);
  940. if (rv != 1)
  941. goto out; /* dio not valid, fall back to buffered i/o */
  942. /*
  943. * Now since we are holding a deferred (CW) lock at this point, you
  944. * might be wondering why this is ever needed. There is a case however
  945. * where we've granted a deferred local lock against a cached exclusive
  946. * glock. That is ok provided all granted local locks are deferred, but
  947. * it also means that it is possible to encounter pages which are
  948. * cached and possibly also mapped. So here we check for that and sort
  949. * them out ahead of the dio. The glock state machine will take care of
  950. * everything else.
  951. *
  952. * If in fact the cached glock state (gl->gl_state) is deferred (CW) in
  953. * the first place, mapping->nr_pages will always be zero.
  954. */
  955. if (mapping->nrpages) {
  956. loff_t lstart = offset & (PAGE_CACHE_SIZE - 1);
  957. loff_t len = iov_iter_count(iter);
  958. loff_t end = PAGE_ALIGN(offset + len) - 1;
  959. rv = 0;
  960. if (len == 0)
  961. goto out;
  962. if (test_and_clear_bit(GIF_SW_PAGED, &ip->i_flags))
  963. unmap_shared_mapping_range(ip->i_inode.i_mapping, offset, len);
  964. rv = filemap_write_and_wait_range(mapping, lstart, end);
  965. if (rv)
  966. goto out;
  967. if (iov_iter_rw(iter) == WRITE)
  968. truncate_inode_pages_range(mapping, lstart, end);
  969. }
  970. rv = __blockdev_direct_IO(iocb, inode, inode->i_sb->s_bdev, iter,
  971. offset, gfs2_get_block_direct, 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. }