ops_address.c 29 KB

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