ops_address.c 23 KB

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