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