ops_address.c 29 KB

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