ops_address.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146
  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 "gfs2.h"
  23. #include "incore.h"
  24. #include "bmap.h"
  25. #include "glock.h"
  26. #include "inode.h"
  27. #include "log.h"
  28. #include "meta_io.h"
  29. #include "ops_address.h"
  30. #include "quota.h"
  31. #include "trans.h"
  32. #include "rgrp.h"
  33. #include "super.h"
  34. #include "util.h"
  35. #include "glops.h"
  36. static void gfs2_page_add_databufs(struct gfs2_inode *ip, struct page *page,
  37. unsigned int from, unsigned int to)
  38. {
  39. struct buffer_head *head = page_buffers(page);
  40. unsigned int bsize = head->b_size;
  41. struct buffer_head *bh;
  42. unsigned int start, end;
  43. for (bh = head, start = 0; bh != head || !start;
  44. bh = bh->b_this_page, start = end) {
  45. end = start + bsize;
  46. if (end <= from || start >= to)
  47. continue;
  48. if (gfs2_is_jdata(ip))
  49. set_buffer_uptodate(bh);
  50. gfs2_trans_add_bh(ip->i_gl, bh, 0);
  51. }
  52. }
  53. /**
  54. * gfs2_get_block_noalloc - Fills in a buffer head with details about a block
  55. * @inode: The inode
  56. * @lblock: The block number to look up
  57. * @bh_result: The buffer head to return the result in
  58. * @create: Non-zero if we may add block to the file
  59. *
  60. * Returns: errno
  61. */
  62. static int gfs2_get_block_noalloc(struct inode *inode, sector_t lblock,
  63. struct buffer_head *bh_result, int create)
  64. {
  65. int error;
  66. error = gfs2_block_map(inode, lblock, bh_result, 0);
  67. if (error)
  68. return error;
  69. if (!buffer_mapped(bh_result))
  70. return -EIO;
  71. return 0;
  72. }
  73. static int gfs2_get_block_direct(struct inode *inode, sector_t lblock,
  74. struct buffer_head *bh_result, int create)
  75. {
  76. return gfs2_block_map(inode, lblock, bh_result, 0);
  77. }
  78. /**
  79. * gfs2_writepage_common - Common bits of writepage
  80. * @page: The page to be written
  81. * @wbc: The writeback control
  82. *
  83. * Returns: 1 if writepage is ok, otherwise an error code or zero if no error.
  84. */
  85. static int gfs2_writepage_common(struct page *page,
  86. struct writeback_control *wbc)
  87. {
  88. struct inode *inode = page->mapping->host;
  89. struct gfs2_inode *ip = GFS2_I(inode);
  90. struct gfs2_sbd *sdp = GFS2_SB(inode);
  91. loff_t i_size = i_size_read(inode);
  92. pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
  93. unsigned offset;
  94. if (gfs2_assert_withdraw(sdp, gfs2_glock_is_held_excl(ip->i_gl)))
  95. goto out;
  96. if (current->journal_info)
  97. goto redirty;
  98. /* Is the page fully outside i_size? (truncate in progress) */
  99. offset = i_size & (PAGE_CACHE_SIZE-1);
  100. if (page->index > end_index || (page->index == end_index && !offset)) {
  101. page->mapping->a_ops->invalidatepage(page, 0);
  102. goto out;
  103. }
  104. return 1;
  105. redirty:
  106. redirty_page_for_writepage(wbc, page);
  107. out:
  108. unlock_page(page);
  109. return 0;
  110. }
  111. /**
  112. * gfs2_writeback_writepage - Write page for writeback mappings
  113. * @page: The page
  114. * @wbc: The writeback control
  115. *
  116. */
  117. static int gfs2_writeback_writepage(struct page *page,
  118. struct writeback_control *wbc)
  119. {
  120. int ret;
  121. ret = gfs2_writepage_common(page, wbc);
  122. if (ret <= 0)
  123. return ret;
  124. ret = mpage_writepage(page, gfs2_get_block_noalloc, wbc);
  125. if (ret == -EAGAIN)
  126. ret = block_write_full_page(page, gfs2_get_block_noalloc, wbc);
  127. return ret;
  128. }
  129. /**
  130. * gfs2_ordered_writepage - Write page for ordered data files
  131. * @page: The page to write
  132. * @wbc: The writeback control
  133. *
  134. */
  135. static int gfs2_ordered_writepage(struct page *page,
  136. struct writeback_control *wbc)
  137. {
  138. struct inode *inode = page->mapping->host;
  139. struct gfs2_inode *ip = GFS2_I(inode);
  140. int ret;
  141. ret = gfs2_writepage_common(page, wbc);
  142. if (ret <= 0)
  143. return ret;
  144. if (!page_has_buffers(page)) {
  145. create_empty_buffers(page, inode->i_sb->s_blocksize,
  146. (1 << BH_Dirty)|(1 << BH_Uptodate));
  147. }
  148. gfs2_page_add_databufs(ip, page, 0, inode->i_sb->s_blocksize-1);
  149. return block_write_full_page(page, gfs2_get_block_noalloc, wbc);
  150. }
  151. /**
  152. * __gfs2_jdata_writepage - The core of jdata writepage
  153. * @page: The page to write
  154. * @wbc: The writeback control
  155. *
  156. * This is shared between writepage and writepages and implements the
  157. * core of the writepage operation. If a transaction is required then
  158. * PageChecked will have been set and the transaction will have
  159. * already been started before this is called.
  160. */
  161. static int __gfs2_jdata_writepage(struct page *page, struct writeback_control *wbc)
  162. {
  163. struct inode *inode = page->mapping->host;
  164. struct gfs2_inode *ip = GFS2_I(inode);
  165. struct gfs2_sbd *sdp = GFS2_SB(inode);
  166. if (PageChecked(page)) {
  167. ClearPageChecked(page);
  168. if (!page_has_buffers(page)) {
  169. create_empty_buffers(page, inode->i_sb->s_blocksize,
  170. (1 << BH_Dirty)|(1 << BH_Uptodate));
  171. }
  172. gfs2_page_add_databufs(ip, page, 0, sdp->sd_vfs->s_blocksize-1);
  173. }
  174. return block_write_full_page(page, gfs2_get_block_noalloc, wbc);
  175. }
  176. /**
  177. * gfs2_jdata_writepage - Write complete page
  178. * @page: Page to write
  179. *
  180. * Returns: errno
  181. *
  182. */
  183. static int gfs2_jdata_writepage(struct page *page, struct writeback_control *wbc)
  184. {
  185. struct inode *inode = page->mapping->host;
  186. struct gfs2_sbd *sdp = GFS2_SB(inode);
  187. int ret;
  188. int done_trans = 0;
  189. if (PageChecked(page)) {
  190. if (wbc->sync_mode != WB_SYNC_ALL)
  191. goto out_ignore;
  192. ret = gfs2_trans_begin(sdp, RES_DINODE + 1, 0);
  193. if (ret)
  194. goto out_ignore;
  195. done_trans = 1;
  196. }
  197. ret = gfs2_writepage_common(page, wbc);
  198. if (ret > 0)
  199. ret = __gfs2_jdata_writepage(page, wbc);
  200. if (done_trans)
  201. gfs2_trans_end(sdp);
  202. return ret;
  203. out_ignore:
  204. redirty_page_for_writepage(wbc, page);
  205. unlock_page(page);
  206. return 0;
  207. }
  208. /**
  209. * gfs2_writeback_writepages - Write a bunch of dirty pages back to disk
  210. * @mapping: The mapping to write
  211. * @wbc: Write-back control
  212. *
  213. * For the data=writeback case we can already ignore buffer heads
  214. * and write whole extents at once. This is a big reduction in the
  215. * number of I/O requests we send and the bmap calls we make in this case.
  216. */
  217. static int gfs2_writeback_writepages(struct address_space *mapping,
  218. struct writeback_control *wbc)
  219. {
  220. return mpage_writepages(mapping, wbc, gfs2_get_block_noalloc);
  221. }
  222. /**
  223. * gfs2_write_jdata_pagevec - Write back a pagevec's worth of pages
  224. * @mapping: The mapping
  225. * @wbc: The writeback control
  226. * @writepage: The writepage function to call for each page
  227. * @pvec: The vector of pages
  228. * @nr_pages: The number of pages to write
  229. *
  230. * Returns: non-zero if loop should terminate, zero otherwise
  231. */
  232. static int gfs2_write_jdata_pagevec(struct address_space *mapping,
  233. struct writeback_control *wbc,
  234. struct pagevec *pvec,
  235. int nr_pages, pgoff_t end)
  236. {
  237. struct inode *inode = mapping->host;
  238. struct gfs2_sbd *sdp = GFS2_SB(inode);
  239. loff_t i_size = i_size_read(inode);
  240. pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
  241. unsigned offset = i_size & (PAGE_CACHE_SIZE-1);
  242. unsigned nrblocks = nr_pages * (PAGE_CACHE_SIZE/inode->i_sb->s_blocksize);
  243. struct backing_dev_info *bdi = mapping->backing_dev_info;
  244. int i;
  245. int ret;
  246. ret = gfs2_trans_begin(sdp, nrblocks, nrblocks);
  247. if (ret < 0)
  248. return ret;
  249. for(i = 0; i < nr_pages; i++) {
  250. struct page *page = pvec->pages[i];
  251. lock_page(page);
  252. if (unlikely(page->mapping != mapping)) {
  253. unlock_page(page);
  254. continue;
  255. }
  256. if (!wbc->range_cyclic && page->index > end) {
  257. ret = 1;
  258. unlock_page(page);
  259. continue;
  260. }
  261. if (wbc->sync_mode != WB_SYNC_NONE)
  262. wait_on_page_writeback(page);
  263. if (PageWriteback(page) ||
  264. !clear_page_dirty_for_io(page)) {
  265. unlock_page(page);
  266. continue;
  267. }
  268. /* Is the page fully outside i_size? (truncate in progress) */
  269. if (page->index > end_index || (page->index == end_index && !offset)) {
  270. page->mapping->a_ops->invalidatepage(page, 0);
  271. unlock_page(page);
  272. continue;
  273. }
  274. ret = __gfs2_jdata_writepage(page, wbc);
  275. if (ret || (--(wbc->nr_to_write) <= 0))
  276. ret = 1;
  277. if (wbc->nonblocking && bdi_write_congested(bdi)) {
  278. wbc->encountered_congestion = 1;
  279. ret = 1;
  280. }
  281. }
  282. gfs2_trans_end(sdp);
  283. return ret;
  284. }
  285. /**
  286. * gfs2_write_cache_jdata - Like write_cache_pages but different
  287. * @mapping: The mapping to write
  288. * @wbc: The writeback control
  289. * @writepage: The writepage function to call
  290. * @data: The data to pass to writepage
  291. *
  292. * The reason that we use our own function here is that we need to
  293. * start transactions before we grab page locks. This allows us
  294. * to get the ordering right.
  295. */
  296. static int gfs2_write_cache_jdata(struct address_space *mapping,
  297. struct writeback_control *wbc)
  298. {
  299. struct backing_dev_info *bdi = mapping->backing_dev_info;
  300. int ret = 0;
  301. int done = 0;
  302. struct pagevec pvec;
  303. int nr_pages;
  304. pgoff_t index;
  305. pgoff_t end;
  306. int scanned = 0;
  307. int range_whole = 0;
  308. if (wbc->nonblocking && bdi_write_congested(bdi)) {
  309. wbc->encountered_congestion = 1;
  310. return 0;
  311. }
  312. pagevec_init(&pvec, 0);
  313. if (wbc->range_cyclic) {
  314. index = mapping->writeback_index; /* Start from prev offset */
  315. end = -1;
  316. } else {
  317. index = wbc->range_start >> PAGE_CACHE_SHIFT;
  318. end = wbc->range_end >> PAGE_CACHE_SHIFT;
  319. if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
  320. range_whole = 1;
  321. scanned = 1;
  322. }
  323. retry:
  324. while (!done && (index <= end) &&
  325. (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
  326. PAGECACHE_TAG_DIRTY,
  327. min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
  328. scanned = 1;
  329. ret = gfs2_write_jdata_pagevec(mapping, wbc, &pvec, nr_pages, end);
  330. if (ret)
  331. done = 1;
  332. if (ret > 0)
  333. ret = 0;
  334. pagevec_release(&pvec);
  335. cond_resched();
  336. }
  337. if (!scanned && !done) {
  338. /*
  339. * We hit the last page and there is more work to be done: wrap
  340. * back to the start of the file
  341. */
  342. scanned = 1;
  343. index = 0;
  344. goto retry;
  345. }
  346. if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
  347. mapping->writeback_index = index;
  348. return ret;
  349. }
  350. /**
  351. * gfs2_jdata_writepages - Write a bunch of dirty pages back to disk
  352. * @mapping: The mapping to write
  353. * @wbc: The writeback control
  354. *
  355. */
  356. static int gfs2_jdata_writepages(struct address_space *mapping,
  357. struct writeback_control *wbc)
  358. {
  359. struct gfs2_inode *ip = GFS2_I(mapping->host);
  360. struct gfs2_sbd *sdp = GFS2_SB(mapping->host);
  361. int ret;
  362. ret = gfs2_write_cache_jdata(mapping, wbc);
  363. if (ret == 0 && wbc->sync_mode == WB_SYNC_ALL) {
  364. gfs2_log_flush(sdp, ip->i_gl);
  365. ret = gfs2_write_cache_jdata(mapping, wbc);
  366. }
  367. return ret;
  368. }
  369. /**
  370. * stuffed_readpage - Fill in a Linux page with stuffed file data
  371. * @ip: the inode
  372. * @page: the page
  373. *
  374. * Returns: errno
  375. */
  376. static int stuffed_readpage(struct gfs2_inode *ip, struct page *page)
  377. {
  378. struct buffer_head *dibh;
  379. void *kaddr;
  380. int error;
  381. /*
  382. * Due to the order of unstuffing files and ->fault(), we can be
  383. * asked for a zero page in the case of a stuffed file being extended,
  384. * so we need to supply one here. It doesn't happen often.
  385. */
  386. if (unlikely(page->index)) {
  387. zero_user(page, 0, PAGE_CACHE_SIZE);
  388. SetPageUptodate(page);
  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. .is_partially_uptodate = block_is_partially_uptodate,
  964. };
  965. static const struct address_space_operations gfs2_ordered_aops = {
  966. .writepage = gfs2_ordered_writepage,
  967. .readpage = gfs2_readpage,
  968. .readpages = gfs2_readpages,
  969. .sync_page = block_sync_page,
  970. .write_begin = gfs2_write_begin,
  971. .write_end = gfs2_write_end,
  972. .set_page_dirty = gfs2_set_page_dirty,
  973. .bmap = gfs2_bmap,
  974. .invalidatepage = gfs2_invalidatepage,
  975. .releasepage = gfs2_releasepage,
  976. .direct_IO = gfs2_direct_IO,
  977. .migratepage = buffer_migrate_page,
  978. .is_partially_uptodate = block_is_partially_uptodate,
  979. };
  980. static const struct address_space_operations gfs2_jdata_aops = {
  981. .writepage = gfs2_jdata_writepage,
  982. .writepages = gfs2_jdata_writepages,
  983. .readpage = gfs2_readpage,
  984. .readpages = gfs2_readpages,
  985. .sync_page = block_sync_page,
  986. .write_begin = gfs2_write_begin,
  987. .write_end = gfs2_write_end,
  988. .set_page_dirty = gfs2_set_page_dirty,
  989. .bmap = gfs2_bmap,
  990. .invalidatepage = gfs2_invalidatepage,
  991. .releasepage = gfs2_releasepage,
  992. .is_partially_uptodate = block_is_partially_uptodate,
  993. };
  994. void gfs2_set_aops(struct inode *inode)
  995. {
  996. struct gfs2_inode *ip = GFS2_I(inode);
  997. if (gfs2_is_writeback(ip))
  998. inode->i_mapping->a_ops = &gfs2_writeback_aops;
  999. else if (gfs2_is_ordered(ip))
  1000. inode->i_mapping->a_ops = &gfs2_ordered_aops;
  1001. else if (gfs2_is_jdata(ip))
  1002. inode->i_mapping->a_ops = &gfs2_jdata_aops;
  1003. else
  1004. BUG();
  1005. }