aops.c 29 KB

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