ops_address.c 22 KB

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