ops_address.c 21 KB

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