ops_address.c 22 KB

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