ops_address.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2007 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/lm_interface.h>
  22. #include <linux/swap.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 "ops_address.h"
  31. #include "quota.h"
  32. #include "trans.h"
  33. #include "rgrp.h"
  34. #include "super.h"
  35. #include "util.h"
  36. #include "glops.h"
  37. static void gfs2_page_add_databufs(struct gfs2_inode *ip, struct page *page,
  38. unsigned int from, unsigned int to)
  39. {
  40. struct buffer_head *head = page_buffers(page);
  41. unsigned int bsize = head->b_size;
  42. struct buffer_head *bh;
  43. unsigned int start, end;
  44. for (bh = head, start = 0; bh != head || !start;
  45. bh = bh->b_this_page, start = end) {
  46. end = start + bsize;
  47. if (end <= from || start >= to)
  48. continue;
  49. if (gfs2_is_jdata(ip))
  50. set_buffer_uptodate(bh);
  51. gfs2_trans_add_bh(ip->i_gl, bh, 0);
  52. }
  53. }
  54. /**
  55. * gfs2_get_block - Fills in a buffer head with details about a block
  56. * @inode: The inode
  57. * @lblock: The block number to look up
  58. * @bh_result: The buffer head to return the result in
  59. * @create: Non-zero if we may add block to the file
  60. *
  61. * Returns: errno
  62. */
  63. int gfs2_get_block(struct inode *inode, sector_t lblock,
  64. struct buffer_head *bh_result, int create)
  65. {
  66. return gfs2_block_map(inode, lblock, create, bh_result);
  67. }
  68. /**
  69. * gfs2_get_block_noalloc - Fills in a buffer head with details about a block
  70. * @inode: The inode
  71. * @lblock: The block number to look up
  72. * @bh_result: The buffer head to return the result in
  73. * @create: Non-zero if we may add block to the file
  74. *
  75. * Returns: errno
  76. */
  77. static int gfs2_get_block_noalloc(struct inode *inode, sector_t lblock,
  78. struct buffer_head *bh_result, int create)
  79. {
  80. int error;
  81. error = gfs2_block_map(inode, lblock, 0, bh_result);
  82. if (error)
  83. return error;
  84. if (!buffer_mapped(bh_result))
  85. return -EIO;
  86. return 0;
  87. }
  88. static int gfs2_get_block_direct(struct inode *inode, sector_t lblock,
  89. struct buffer_head *bh_result, int create)
  90. {
  91. return gfs2_block_map(inode, lblock, 0, bh_result);
  92. }
  93. /**
  94. * gfs2_writepage_common - Common bits of writepage
  95. * @page: The page to be written
  96. * @wbc: The writeback control
  97. *
  98. * Returns: 1 if writepage is ok, otherwise an error code or zero if no error.
  99. */
  100. static int gfs2_writepage_common(struct page *page,
  101. struct writeback_control *wbc)
  102. {
  103. struct inode *inode = page->mapping->host;
  104. struct gfs2_inode *ip = GFS2_I(inode);
  105. struct gfs2_sbd *sdp = GFS2_SB(inode);
  106. loff_t i_size = i_size_read(inode);
  107. pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
  108. unsigned offset;
  109. int ret = -EIO;
  110. if (gfs2_assert_withdraw(sdp, gfs2_glock_is_held_excl(ip->i_gl)))
  111. goto out;
  112. ret = 0;
  113. if (current->journal_info)
  114. goto redirty;
  115. /* Is the page fully outside i_size? (truncate in progress) */
  116. offset = i_size & (PAGE_CACHE_SIZE-1);
  117. if (page->index > end_index || (page->index == end_index && !offset)) {
  118. page->mapping->a_ops->invalidatepage(page, 0);
  119. goto out;
  120. }
  121. return 1;
  122. redirty:
  123. redirty_page_for_writepage(wbc, page);
  124. out:
  125. unlock_page(page);
  126. return 0;
  127. }
  128. /**
  129. * gfs2_writeback_writepage - Write page for writeback mappings
  130. * @page: The page
  131. * @wbc: The writeback control
  132. *
  133. */
  134. static int gfs2_writeback_writepage(struct page *page,
  135. struct writeback_control *wbc)
  136. {
  137. int ret;
  138. ret = gfs2_writepage_common(page, wbc);
  139. if (ret <= 0)
  140. return ret;
  141. ret = mpage_writepage(page, gfs2_get_block_noalloc, wbc);
  142. if (ret == -EAGAIN)
  143. ret = block_write_full_page(page, gfs2_get_block_noalloc, wbc);
  144. return ret;
  145. }
  146. /**
  147. * gfs2_ordered_writepage - Write page for ordered data files
  148. * @page: The page to write
  149. * @wbc: The writeback control
  150. *
  151. */
  152. static int gfs2_ordered_writepage(struct page *page,
  153. struct writeback_control *wbc)
  154. {
  155. struct inode *inode = page->mapping->host;
  156. struct gfs2_inode *ip = GFS2_I(inode);
  157. int ret;
  158. ret = gfs2_writepage_common(page, wbc);
  159. if (ret <= 0)
  160. return ret;
  161. if (!page_has_buffers(page)) {
  162. create_empty_buffers(page, inode->i_sb->s_blocksize,
  163. (1 << BH_Dirty)|(1 << BH_Uptodate));
  164. }
  165. gfs2_page_add_databufs(ip, page, 0, inode->i_sb->s_blocksize-1);
  166. return block_write_full_page(page, gfs2_get_block_noalloc, wbc);
  167. }
  168. /**
  169. * gfs2_jdata_writepage - Write complete page
  170. * @page: Page to write
  171. *
  172. * Returns: errno
  173. *
  174. */
  175. static int gfs2_jdata_writepage(struct page *page, struct writeback_control *wbc)
  176. {
  177. struct inode *inode = page->mapping->host;
  178. struct gfs2_inode *ip = GFS2_I(inode);
  179. struct gfs2_sbd *sdp = GFS2_SB(inode);
  180. int error;
  181. int done_trans = 0;
  182. error = gfs2_writepage_common(page, wbc);
  183. if (error <= 0)
  184. return error;
  185. if (PageChecked(page)) {
  186. error = gfs2_trans_begin(sdp, RES_DINODE + 1, 0);
  187. if (error)
  188. goto out_ignore;
  189. ClearPageChecked(page);
  190. if (!page_has_buffers(page)) {
  191. create_empty_buffers(page, inode->i_sb->s_blocksize,
  192. (1 << BH_Dirty)|(1 << BH_Uptodate));
  193. }
  194. gfs2_page_add_databufs(ip, page, 0, sdp->sd_vfs->s_blocksize-1);
  195. done_trans = 1;
  196. }
  197. error = block_write_full_page(page, gfs2_get_block_noalloc, wbc);
  198. if (done_trans)
  199. gfs2_trans_end(sdp);
  200. return error;
  201. out_ignore:
  202. redirty_page_for_writepage(wbc, page);
  203. unlock_page(page);
  204. return 0;
  205. }
  206. /**
  207. * gfs2_writeback_writepages - Write a bunch of dirty pages back to disk
  208. * @mapping: The mapping to write
  209. * @wbc: Write-back control
  210. *
  211. * For the data=writeback case we can already ignore buffer heads
  212. * and write whole extents at once. This is a big reduction in the
  213. * number of I/O requests we send and the bmap calls we make in this case.
  214. */
  215. static int gfs2_writeback_writepages(struct address_space *mapping,
  216. struct writeback_control *wbc)
  217. {
  218. return mpage_writepages(mapping, wbc, gfs2_get_block_noalloc);
  219. }
  220. /**
  221. * stuffed_readpage - Fill in a Linux page with stuffed file data
  222. * @ip: the inode
  223. * @page: the page
  224. *
  225. * Returns: errno
  226. */
  227. static int stuffed_readpage(struct gfs2_inode *ip, struct page *page)
  228. {
  229. struct buffer_head *dibh;
  230. void *kaddr;
  231. int error;
  232. /*
  233. * Due to the order of unstuffing files and ->nopage(), we can be
  234. * asked for a zero page in the case of a stuffed file being extended,
  235. * so we need to supply one here. It doesn't happen often.
  236. */
  237. if (unlikely(page->index)) {
  238. zero_user_page(page, 0, PAGE_CACHE_SIZE, KM_USER0);
  239. return 0;
  240. }
  241. error = gfs2_meta_inode_buffer(ip, &dibh);
  242. if (error)
  243. return error;
  244. kaddr = kmap_atomic(page, KM_USER0);
  245. memcpy(kaddr, dibh->b_data + sizeof(struct gfs2_dinode),
  246. ip->i_di.di_size);
  247. memset(kaddr + ip->i_di.di_size, 0, PAGE_CACHE_SIZE - ip->i_di.di_size);
  248. kunmap_atomic(kaddr, KM_USER0);
  249. flush_dcache_page(page);
  250. brelse(dibh);
  251. SetPageUptodate(page);
  252. return 0;
  253. }
  254. /**
  255. * __gfs2_readpage - readpage
  256. * @file: The file to read a page for
  257. * @page: The page to read
  258. *
  259. * This is the core of gfs2's readpage. Its used by the internal file
  260. * reading code as in that case we already hold the glock. Also its
  261. * called by gfs2_readpage() once the required lock has been granted.
  262. *
  263. */
  264. static int __gfs2_readpage(void *file, struct page *page)
  265. {
  266. struct gfs2_inode *ip = GFS2_I(page->mapping->host);
  267. struct gfs2_sbd *sdp = GFS2_SB(page->mapping->host);
  268. int error;
  269. if (gfs2_is_stuffed(ip)) {
  270. error = stuffed_readpage(ip, page);
  271. unlock_page(page);
  272. } else {
  273. error = mpage_readpage(page, gfs2_get_block);
  274. }
  275. if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
  276. return -EIO;
  277. return error;
  278. }
  279. /**
  280. * gfs2_readpage - read a page of a file
  281. * @file: The file to read
  282. * @page: The page of the file
  283. *
  284. * This deals with the locking required. We use a trylock in order to
  285. * avoid the page lock / glock ordering problems returning AOP_TRUNCATED_PAGE
  286. * in the event that we are unable to get the lock.
  287. */
  288. static int gfs2_readpage(struct file *file, struct page *page)
  289. {
  290. struct gfs2_inode *ip = GFS2_I(page->mapping->host);
  291. struct gfs2_holder gh;
  292. int error;
  293. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME|LM_FLAG_TRY_1CB, &gh);
  294. error = gfs2_glock_nq_atime(&gh);
  295. if (unlikely(error)) {
  296. unlock_page(page);
  297. goto out;
  298. }
  299. error = __gfs2_readpage(file, page);
  300. gfs2_glock_dq(&gh);
  301. out:
  302. gfs2_holder_uninit(&gh);
  303. if (error == GLR_TRYFAILED) {
  304. yield();
  305. return AOP_TRUNCATED_PAGE;
  306. }
  307. return error;
  308. }
  309. /**
  310. * gfs2_internal_read - read an internal file
  311. * @ip: The gfs2 inode
  312. * @ra_state: The readahead state (or NULL for no readahead)
  313. * @buf: The buffer to fill
  314. * @pos: The file position
  315. * @size: The amount to read
  316. *
  317. */
  318. int gfs2_internal_read(struct gfs2_inode *ip, struct file_ra_state *ra_state,
  319. char *buf, loff_t *pos, unsigned size)
  320. {
  321. struct address_space *mapping = ip->i_inode.i_mapping;
  322. unsigned long index = *pos / PAGE_CACHE_SIZE;
  323. unsigned offset = *pos & (PAGE_CACHE_SIZE - 1);
  324. unsigned copied = 0;
  325. unsigned amt;
  326. struct page *page;
  327. void *p;
  328. do {
  329. amt = size - copied;
  330. if (offset + size > PAGE_CACHE_SIZE)
  331. amt = PAGE_CACHE_SIZE - offset;
  332. page = read_cache_page(mapping, index, __gfs2_readpage, NULL);
  333. if (IS_ERR(page))
  334. return PTR_ERR(page);
  335. p = kmap_atomic(page, KM_USER0);
  336. memcpy(buf + copied, p + offset, amt);
  337. kunmap_atomic(p, KM_USER0);
  338. mark_page_accessed(page);
  339. page_cache_release(page);
  340. copied += amt;
  341. index++;
  342. offset = 0;
  343. } while(copied < size);
  344. (*pos) += size;
  345. return size;
  346. }
  347. /**
  348. * gfs2_readpages - Read a bunch of pages at once
  349. *
  350. * Some notes:
  351. * 1. This is only for readahead, so we can simply ignore any things
  352. * which are slightly inconvenient (such as locking conflicts between
  353. * the page lock and the glock) and return having done no I/O. Its
  354. * obviously not something we'd want to do on too regular a basis.
  355. * Any I/O we ignore at this time will be done via readpage later.
  356. * 2. We don't handle stuffed files here we let readpage do the honours.
  357. * 3. mpage_readpages() does most of the heavy lifting in the common case.
  358. * 4. gfs2_get_block() is relied upon to set BH_Boundary in the right places.
  359. */
  360. static int gfs2_readpages(struct file *file, struct address_space *mapping,
  361. struct list_head *pages, unsigned nr_pages)
  362. {
  363. struct inode *inode = mapping->host;
  364. struct gfs2_inode *ip = GFS2_I(inode);
  365. struct gfs2_sbd *sdp = GFS2_SB(inode);
  366. struct gfs2_holder gh;
  367. int ret;
  368. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &gh);
  369. ret = gfs2_glock_nq_atime(&gh);
  370. if (unlikely(ret))
  371. goto out_uninit;
  372. if (!gfs2_is_stuffed(ip))
  373. ret = mpage_readpages(mapping, pages, nr_pages, gfs2_get_block);
  374. gfs2_glock_dq(&gh);
  375. out_uninit:
  376. gfs2_holder_uninit(&gh);
  377. if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
  378. ret = -EIO;
  379. return ret;
  380. }
  381. /**
  382. * gfs2_write_begin - Begin to write to a file
  383. * @file: The file to write to
  384. * @mapping: The mapping in which to write
  385. * @pos: The file offset at which to start writing
  386. * @len: Length of the write
  387. * @flags: Various flags
  388. * @pagep: Pointer to return the page
  389. * @fsdata: Pointer to return fs data (unused by GFS2)
  390. *
  391. * Returns: errno
  392. */
  393. static int gfs2_write_begin(struct file *file, struct address_space *mapping,
  394. loff_t pos, unsigned len, unsigned flags,
  395. struct page **pagep, void **fsdata)
  396. {
  397. struct gfs2_inode *ip = GFS2_I(mapping->host);
  398. struct gfs2_sbd *sdp = GFS2_SB(mapping->host);
  399. unsigned int data_blocks, ind_blocks, rblocks;
  400. int alloc_required;
  401. int error = 0;
  402. struct gfs2_alloc *al;
  403. pgoff_t index = pos >> PAGE_CACHE_SHIFT;
  404. unsigned from = pos & (PAGE_CACHE_SIZE - 1);
  405. unsigned to = from + len;
  406. struct page *page;
  407. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, GL_ATIME, &ip->i_gh);
  408. error = gfs2_glock_nq_atime(&ip->i_gh);
  409. if (unlikely(error))
  410. goto out_uninit;
  411. error = -ENOMEM;
  412. page = __grab_cache_page(mapping, index);
  413. *pagep = page;
  414. if (!page)
  415. goto out_unlock;
  416. gfs2_write_calc_reserv(ip, len, &data_blocks, &ind_blocks);
  417. error = gfs2_write_alloc_required(ip, pos, len, &alloc_required);
  418. if (error)
  419. goto out_putpage;
  420. ip->i_alloc.al_requested = 0;
  421. if (alloc_required) {
  422. al = gfs2_alloc_get(ip);
  423. error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
  424. if (error)
  425. goto out_alloc_put;
  426. error = gfs2_quota_check(ip, ip->i_inode.i_uid, ip->i_inode.i_gid);
  427. if (error)
  428. goto out_qunlock;
  429. al->al_requested = data_blocks + ind_blocks;
  430. error = gfs2_inplace_reserve(ip);
  431. if (error)
  432. goto out_qunlock;
  433. }
  434. rblocks = RES_DINODE + ind_blocks;
  435. if (gfs2_is_jdata(ip))
  436. rblocks += data_blocks ? data_blocks : 1;
  437. if (ind_blocks || data_blocks)
  438. rblocks += RES_STATFS + RES_QUOTA;
  439. error = gfs2_trans_begin(sdp, rblocks,
  440. PAGE_CACHE_SIZE/sdp->sd_sb.sb_bsize);
  441. if (error)
  442. goto out_trans_fail;
  443. if (gfs2_is_stuffed(ip)) {
  444. if (pos + len > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) {
  445. error = gfs2_unstuff_dinode(ip, page);
  446. if (error == 0)
  447. goto prepare_write;
  448. } else if (!PageUptodate(page))
  449. error = stuffed_readpage(ip, page);
  450. goto out;
  451. }
  452. prepare_write:
  453. error = block_prepare_write(page, from, to, gfs2_get_block);
  454. out:
  455. if (error) {
  456. gfs2_trans_end(sdp);
  457. out_trans_fail:
  458. if (alloc_required) {
  459. gfs2_inplace_release(ip);
  460. out_qunlock:
  461. gfs2_quota_unlock(ip);
  462. out_alloc_put:
  463. gfs2_alloc_put(ip);
  464. }
  465. out_putpage:
  466. page_cache_release(page);
  467. if (pos + len > ip->i_inode.i_size)
  468. vmtruncate(&ip->i_inode, ip->i_inode.i_size);
  469. out_unlock:
  470. gfs2_glock_dq_m(1, &ip->i_gh);
  471. out_uninit:
  472. gfs2_holder_uninit(&ip->i_gh);
  473. }
  474. return error;
  475. }
  476. /**
  477. * adjust_fs_space - Adjusts the free space available due to gfs2_grow
  478. * @inode: the rindex inode
  479. */
  480. static void adjust_fs_space(struct inode *inode)
  481. {
  482. struct gfs2_sbd *sdp = inode->i_sb->s_fs_info;
  483. struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
  484. struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
  485. u64 fs_total, new_free;
  486. /* Total up the file system space, according to the latest rindex. */
  487. fs_total = gfs2_ri_total(sdp);
  488. spin_lock(&sdp->sd_statfs_spin);
  489. if (fs_total > (m_sc->sc_total + l_sc->sc_total))
  490. new_free = fs_total - (m_sc->sc_total + l_sc->sc_total);
  491. else
  492. new_free = 0;
  493. spin_unlock(&sdp->sd_statfs_spin);
  494. fs_warn(sdp, "File system extended by %llu blocks.\n",
  495. (unsigned long long)new_free);
  496. gfs2_statfs_change(sdp, new_free, new_free, 0);
  497. }
  498. /**
  499. * gfs2_stuffed_write_end - Write end for stuffed files
  500. * @inode: The inode
  501. * @dibh: The buffer_head containing the on-disk inode
  502. * @pos: The file position
  503. * @len: The length of the write
  504. * @copied: How much was actually copied by the VFS
  505. * @page: The page
  506. *
  507. * This copies the data from the page into the inode block after
  508. * the inode data structure itself.
  509. *
  510. * Returns: errno
  511. */
  512. static int gfs2_stuffed_write_end(struct inode *inode, struct buffer_head *dibh,
  513. loff_t pos, unsigned len, unsigned copied,
  514. struct page *page)
  515. {
  516. struct gfs2_inode *ip = GFS2_I(inode);
  517. struct gfs2_sbd *sdp = GFS2_SB(inode);
  518. u64 to = pos + copied;
  519. void *kaddr;
  520. unsigned char *buf = dibh->b_data + sizeof(struct gfs2_dinode);
  521. struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
  522. BUG_ON((pos + len) > (dibh->b_size - sizeof(struct gfs2_dinode)));
  523. kaddr = kmap_atomic(page, KM_USER0);
  524. memcpy(buf + pos, kaddr + pos, copied);
  525. memset(kaddr + pos + copied, 0, len - copied);
  526. flush_dcache_page(page);
  527. kunmap_atomic(kaddr, KM_USER0);
  528. if (!PageUptodate(page))
  529. SetPageUptodate(page);
  530. unlock_page(page);
  531. page_cache_release(page);
  532. if (inode->i_size < to) {
  533. i_size_write(inode, to);
  534. ip->i_di.di_size = inode->i_size;
  535. di->di_size = cpu_to_be64(inode->i_size);
  536. mark_inode_dirty(inode);
  537. }
  538. if (inode == sdp->sd_rindex)
  539. adjust_fs_space(inode);
  540. brelse(dibh);
  541. gfs2_trans_end(sdp);
  542. gfs2_glock_dq(&ip->i_gh);
  543. gfs2_holder_uninit(&ip->i_gh);
  544. return copied;
  545. }
  546. /**
  547. * gfs2_write_end
  548. * @file: The file to write to
  549. * @mapping: The address space to write to
  550. * @pos: The file position
  551. * @len: The length of the data
  552. * @copied:
  553. * @page: The page that has been written
  554. * @fsdata: The fsdata (unused in GFS2)
  555. *
  556. * The main write_end function for GFS2. We have a separate one for
  557. * stuffed files as they are slightly different, otherwise we just
  558. * put our locking around the VFS provided functions.
  559. *
  560. * Returns: errno
  561. */
  562. static int gfs2_write_end(struct file *file, struct address_space *mapping,
  563. loff_t pos, unsigned len, unsigned copied,
  564. struct page *page, void *fsdata)
  565. {
  566. struct inode *inode = page->mapping->host;
  567. struct gfs2_inode *ip = GFS2_I(inode);
  568. struct gfs2_sbd *sdp = GFS2_SB(inode);
  569. struct buffer_head *dibh;
  570. struct gfs2_alloc *al = &ip->i_alloc;
  571. struct gfs2_dinode *di;
  572. unsigned int from = pos & (PAGE_CACHE_SIZE - 1);
  573. unsigned int to = from + len;
  574. int ret;
  575. BUG_ON(gfs2_glock_is_locked_by_me(ip->i_gl) == 0);
  576. ret = gfs2_meta_inode_buffer(ip, &dibh);
  577. if (unlikely(ret)) {
  578. unlock_page(page);
  579. page_cache_release(page);
  580. goto failed;
  581. }
  582. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  583. if (gfs2_is_stuffed(ip))
  584. return gfs2_stuffed_write_end(inode, dibh, pos, len, copied, page);
  585. if (!gfs2_is_writeback(ip))
  586. gfs2_page_add_databufs(ip, page, from, to);
  587. ret = generic_write_end(file, mapping, pos, len, copied, page, fsdata);
  588. if (likely(ret >= 0)) {
  589. copied = ret;
  590. if ((pos + copied) > inode->i_size) {
  591. di = (struct gfs2_dinode *)dibh->b_data;
  592. ip->i_di.di_size = inode->i_size;
  593. di->di_size = cpu_to_be64(inode->i_size);
  594. mark_inode_dirty(inode);
  595. }
  596. }
  597. if (inode == sdp->sd_rindex)
  598. adjust_fs_space(inode);
  599. brelse(dibh);
  600. gfs2_trans_end(sdp);
  601. failed:
  602. if (al->al_requested) {
  603. gfs2_inplace_release(ip);
  604. gfs2_quota_unlock(ip);
  605. gfs2_alloc_put(ip);
  606. }
  607. gfs2_glock_dq(&ip->i_gh);
  608. gfs2_holder_uninit(&ip->i_gh);
  609. return ret;
  610. }
  611. /**
  612. * gfs2_set_page_dirty - Page dirtying function
  613. * @page: The page to dirty
  614. *
  615. * Returns: 1 if it dirtyed the page, or 0 otherwise
  616. */
  617. static int gfs2_set_page_dirty(struct page *page)
  618. {
  619. SetPageChecked(page);
  620. return __set_page_dirty_buffers(page);
  621. }
  622. /**
  623. * gfs2_bmap - Block map function
  624. * @mapping: Address space info
  625. * @lblock: The block to map
  626. *
  627. * Returns: The disk address for the block or 0 on hole or error
  628. */
  629. static sector_t gfs2_bmap(struct address_space *mapping, sector_t lblock)
  630. {
  631. struct gfs2_inode *ip = GFS2_I(mapping->host);
  632. struct gfs2_holder i_gh;
  633. sector_t dblock = 0;
  634. int error;
  635. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
  636. if (error)
  637. return 0;
  638. if (!gfs2_is_stuffed(ip))
  639. dblock = generic_block_bmap(mapping, lblock, gfs2_get_block);
  640. gfs2_glock_dq_uninit(&i_gh);
  641. return dblock;
  642. }
  643. static void gfs2_discard(struct gfs2_sbd *sdp, struct buffer_head *bh)
  644. {
  645. struct gfs2_bufdata *bd;
  646. lock_buffer(bh);
  647. gfs2_log_lock(sdp);
  648. clear_buffer_dirty(bh);
  649. bd = bh->b_private;
  650. if (bd) {
  651. if (!list_empty(&bd->bd_le.le_list) && !buffer_pinned(bh))
  652. list_del_init(&bd->bd_le.le_list);
  653. else
  654. gfs2_remove_from_journal(bh, current->journal_info, 0);
  655. }
  656. bh->b_bdev = NULL;
  657. clear_buffer_mapped(bh);
  658. clear_buffer_req(bh);
  659. clear_buffer_new(bh);
  660. gfs2_log_unlock(sdp);
  661. unlock_buffer(bh);
  662. }
  663. static void gfs2_invalidatepage(struct page *page, unsigned long offset)
  664. {
  665. struct gfs2_sbd *sdp = GFS2_SB(page->mapping->host);
  666. struct buffer_head *bh, *head;
  667. unsigned long pos = 0;
  668. BUG_ON(!PageLocked(page));
  669. if (offset == 0)
  670. ClearPageChecked(page);
  671. if (!page_has_buffers(page))
  672. goto out;
  673. bh = head = page_buffers(page);
  674. do {
  675. if (offset <= pos)
  676. gfs2_discard(sdp, bh);
  677. pos += bh->b_size;
  678. bh = bh->b_this_page;
  679. } while (bh != head);
  680. out:
  681. if (offset == 0)
  682. try_to_release_page(page, 0);
  683. }
  684. /**
  685. * gfs2_ok_for_dio - check that dio is valid on this file
  686. * @ip: The inode
  687. * @rw: READ or WRITE
  688. * @offset: The offset at which we are reading or writing
  689. *
  690. * Returns: 0 (to ignore the i/o request and thus fall back to buffered i/o)
  691. * 1 (to accept the i/o request)
  692. */
  693. static int gfs2_ok_for_dio(struct gfs2_inode *ip, int rw, loff_t offset)
  694. {
  695. /*
  696. * Should we return an error here? I can't see that O_DIRECT for
  697. * a stuffed file makes any sense. For now we'll silently fall
  698. * back to buffered I/O
  699. */
  700. if (gfs2_is_stuffed(ip))
  701. return 0;
  702. if (offset > i_size_read(&ip->i_inode))
  703. return 0;
  704. return 1;
  705. }
  706. static ssize_t gfs2_direct_IO(int rw, struct kiocb *iocb,
  707. const struct iovec *iov, loff_t offset,
  708. unsigned long nr_segs)
  709. {
  710. struct file *file = iocb->ki_filp;
  711. struct inode *inode = file->f_mapping->host;
  712. struct gfs2_inode *ip = GFS2_I(inode);
  713. struct gfs2_holder gh;
  714. int rv;
  715. /*
  716. * Deferred lock, even if its a write, since we do no allocation
  717. * on this path. All we need change is atime, and this lock mode
  718. * ensures that other nodes have flushed their buffered read caches
  719. * (i.e. their page cache entries for this inode). We do not,
  720. * unfortunately have the option of only flushing a range like
  721. * the VFS does.
  722. */
  723. gfs2_holder_init(ip->i_gl, LM_ST_DEFERRED, GL_ATIME, &gh);
  724. rv = gfs2_glock_nq_atime(&gh);
  725. if (rv)
  726. return rv;
  727. rv = gfs2_ok_for_dio(ip, rw, offset);
  728. if (rv != 1)
  729. goto out; /* dio not valid, fall back to buffered i/o */
  730. rv = blockdev_direct_IO_no_locking(rw, iocb, inode, inode->i_sb->s_bdev,
  731. iov, offset, nr_segs,
  732. gfs2_get_block_direct, NULL);
  733. out:
  734. gfs2_glock_dq_m(1, &gh);
  735. gfs2_holder_uninit(&gh);
  736. return rv;
  737. }
  738. /**
  739. * gfs2_releasepage - free the metadata associated with a page
  740. * @page: the page that's being released
  741. * @gfp_mask: passed from Linux VFS, ignored by us
  742. *
  743. * Call try_to_free_buffers() if the buffers in this page can be
  744. * released.
  745. *
  746. * Returns: 0
  747. */
  748. int gfs2_releasepage(struct page *page, gfp_t gfp_mask)
  749. {
  750. struct inode *aspace = page->mapping->host;
  751. struct gfs2_sbd *sdp = aspace->i_sb->s_fs_info;
  752. struct buffer_head *bh, *head;
  753. struct gfs2_bufdata *bd;
  754. if (!page_has_buffers(page))
  755. return 0;
  756. gfs2_log_lock(sdp);
  757. head = bh = page_buffers(page);
  758. do {
  759. if (atomic_read(&bh->b_count))
  760. goto cannot_release;
  761. bd = bh->b_private;
  762. if (bd && bd->bd_ail)
  763. goto cannot_release;
  764. gfs2_assert_warn(sdp, !buffer_pinned(bh));
  765. gfs2_assert_warn(sdp, !buffer_dirty(bh));
  766. bh = bh->b_this_page;
  767. } while(bh != head);
  768. gfs2_log_unlock(sdp);
  769. head = bh = page_buffers(page);
  770. do {
  771. gfs2_log_lock(sdp);
  772. bd = bh->b_private;
  773. if (bd) {
  774. gfs2_assert_warn(sdp, bd->bd_bh == bh);
  775. gfs2_assert_warn(sdp, list_empty(&bd->bd_list_tr));
  776. if (!list_empty(&bd->bd_le.le_list)) {
  777. if (!buffer_pinned(bh))
  778. list_del_init(&bd->bd_le.le_list);
  779. else
  780. bd = NULL;
  781. }
  782. if (bd)
  783. bd->bd_bh = NULL;
  784. bh->b_private = NULL;
  785. }
  786. gfs2_log_unlock(sdp);
  787. if (bd)
  788. kmem_cache_free(gfs2_bufdata_cachep, bd);
  789. bh = bh->b_this_page;
  790. } while (bh != head);
  791. return try_to_free_buffers(page);
  792. cannot_release:
  793. gfs2_log_unlock(sdp);
  794. return 0;
  795. }
  796. static const struct address_space_operations gfs2_writeback_aops = {
  797. .writepage = gfs2_writeback_writepage,
  798. .writepages = gfs2_writeback_writepages,
  799. .readpage = gfs2_readpage,
  800. .readpages = gfs2_readpages,
  801. .sync_page = block_sync_page,
  802. .write_begin = gfs2_write_begin,
  803. .write_end = gfs2_write_end,
  804. .bmap = gfs2_bmap,
  805. .invalidatepage = gfs2_invalidatepage,
  806. .releasepage = gfs2_releasepage,
  807. .direct_IO = gfs2_direct_IO,
  808. };
  809. static const struct address_space_operations gfs2_ordered_aops = {
  810. .writepage = gfs2_ordered_writepage,
  811. .readpage = gfs2_readpage,
  812. .readpages = gfs2_readpages,
  813. .sync_page = block_sync_page,
  814. .write_begin = gfs2_write_begin,
  815. .write_end = gfs2_write_end,
  816. .set_page_dirty = gfs2_set_page_dirty,
  817. .bmap = gfs2_bmap,
  818. .invalidatepage = gfs2_invalidatepage,
  819. .releasepage = gfs2_releasepage,
  820. .direct_IO = gfs2_direct_IO,
  821. };
  822. static const struct address_space_operations gfs2_jdata_aops = {
  823. .writepage = gfs2_jdata_writepage,
  824. .readpage = gfs2_readpage,
  825. .readpages = gfs2_readpages,
  826. .sync_page = block_sync_page,
  827. .write_begin = gfs2_write_begin,
  828. .write_end = gfs2_write_end,
  829. .set_page_dirty = gfs2_set_page_dirty,
  830. .bmap = gfs2_bmap,
  831. .invalidatepage = gfs2_invalidatepage,
  832. .releasepage = gfs2_releasepage,
  833. };
  834. void gfs2_set_aops(struct inode *inode)
  835. {
  836. struct gfs2_inode *ip = GFS2_I(inode);
  837. if (gfs2_is_writeback(ip))
  838. inode->i_mapping->a_ops = &gfs2_writeback_aops;
  839. else if (gfs2_is_ordered(ip))
  840. inode->i_mapping->a_ops = &gfs2_ordered_aops;
  841. else if (gfs2_is_jdata(ip))
  842. inode->i_mapping->a_ops = &gfs2_jdata_aops;
  843. else
  844. BUG();
  845. }