aops.c 29 KB

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