aops.c 30 KB

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