ops_address.c 23 KB

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