ops_address.c 20 KB

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