ops_address.c 20 KB

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