ops_address.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  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 "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. * adjust_fs_space - Adjusts the free space available due to gfs2_grow
  400. * @inode: the rindex inode
  401. */
  402. static void adjust_fs_space(struct inode *inode)
  403. {
  404. struct gfs2_sbd *sdp = inode->i_sb->s_fs_info;
  405. struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
  406. struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
  407. u64 fs_total, new_free;
  408. /* Total up the file system space, according to the latest rindex. */
  409. fs_total = gfs2_ri_total(sdp);
  410. spin_lock(&sdp->sd_statfs_spin);
  411. if (fs_total > (m_sc->sc_total + l_sc->sc_total))
  412. new_free = fs_total - (m_sc->sc_total + l_sc->sc_total);
  413. else
  414. new_free = 0;
  415. spin_unlock(&sdp->sd_statfs_spin);
  416. fs_warn(sdp, "File system extended by %llu blocks.\n",
  417. (unsigned long long)new_free);
  418. gfs2_statfs_change(sdp, new_free, new_free, 0);
  419. }
  420. /**
  421. * gfs2_commit_write - Commit write to a file
  422. * @file: The file to write to
  423. * @page: The page containing the data
  424. * @from: From (byte range within page)
  425. * @to: To (byte range within page)
  426. *
  427. * Returns: errno
  428. */
  429. static int gfs2_commit_write(struct file *file, struct page *page,
  430. unsigned from, unsigned to)
  431. {
  432. struct inode *inode = page->mapping->host;
  433. struct gfs2_inode *ip = GFS2_I(inode);
  434. struct gfs2_sbd *sdp = GFS2_SB(inode);
  435. int error = -EOPNOTSUPP;
  436. struct buffer_head *dibh;
  437. struct gfs2_alloc *al = &ip->i_alloc;
  438. struct gfs2_dinode *di;
  439. if (gfs2_assert_withdraw(sdp, gfs2_glock_is_locked_by_me(ip->i_gl)))
  440. goto fail_nounlock;
  441. error = gfs2_meta_inode_buffer(ip, &dibh);
  442. if (error)
  443. goto fail_endtrans;
  444. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  445. di = (struct gfs2_dinode *)dibh->b_data;
  446. if (gfs2_is_stuffed(ip)) {
  447. u64 file_size;
  448. void *kaddr;
  449. file_size = ((u64)page->index << PAGE_CACHE_SHIFT) + to;
  450. kaddr = kmap_atomic(page, KM_USER0);
  451. memcpy(dibh->b_data + sizeof(struct gfs2_dinode) + from,
  452. kaddr + from, to - from);
  453. kunmap_atomic(kaddr, KM_USER0);
  454. SetPageUptodate(page);
  455. if (inode->i_size < file_size) {
  456. i_size_write(inode, file_size);
  457. mark_inode_dirty(inode);
  458. }
  459. } else {
  460. if (sdp->sd_args.ar_data == GFS2_DATA_ORDERED ||
  461. gfs2_is_jdata(ip))
  462. gfs2_page_add_databufs(ip, page, from, to);
  463. error = generic_commit_write(file, page, from, to);
  464. if (error)
  465. goto fail;
  466. }
  467. if (ip->i_di.di_size < inode->i_size) {
  468. ip->i_di.di_size = inode->i_size;
  469. di->di_size = cpu_to_be64(inode->i_size);
  470. }
  471. if (inode == sdp->sd_rindex)
  472. adjust_fs_space(inode);
  473. brelse(dibh);
  474. gfs2_trans_end(sdp);
  475. if (al->al_requested) {
  476. gfs2_inplace_release(ip);
  477. gfs2_quota_unlock(ip);
  478. gfs2_alloc_put(ip);
  479. }
  480. unlock_page(page);
  481. gfs2_glock_dq_m(1, &ip->i_gh);
  482. lock_page(page);
  483. gfs2_holder_uninit(&ip->i_gh);
  484. return 0;
  485. fail:
  486. brelse(dibh);
  487. fail_endtrans:
  488. gfs2_trans_end(sdp);
  489. if (al->al_requested) {
  490. gfs2_inplace_release(ip);
  491. gfs2_quota_unlock(ip);
  492. gfs2_alloc_put(ip);
  493. }
  494. unlock_page(page);
  495. gfs2_glock_dq_m(1, &ip->i_gh);
  496. lock_page(page);
  497. gfs2_holder_uninit(&ip->i_gh);
  498. fail_nounlock:
  499. ClearPageUptodate(page);
  500. return error;
  501. }
  502. /**
  503. * gfs2_bmap - Block map function
  504. * @mapping: Address space info
  505. * @lblock: The block to map
  506. *
  507. * Returns: The disk address for the block or 0 on hole or error
  508. */
  509. static sector_t gfs2_bmap(struct address_space *mapping, sector_t lblock)
  510. {
  511. struct gfs2_inode *ip = GFS2_I(mapping->host);
  512. struct gfs2_holder i_gh;
  513. sector_t dblock = 0;
  514. int error;
  515. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
  516. if (error)
  517. return 0;
  518. if (!gfs2_is_stuffed(ip))
  519. dblock = generic_block_bmap(mapping, lblock, gfs2_get_block);
  520. gfs2_glock_dq_uninit(&i_gh);
  521. return dblock;
  522. }
  523. static void discard_buffer(struct gfs2_sbd *sdp, struct buffer_head *bh)
  524. {
  525. struct gfs2_bufdata *bd;
  526. gfs2_log_lock(sdp);
  527. bd = bh->b_private;
  528. if (bd) {
  529. bd->bd_bh = NULL;
  530. bh->b_private = NULL;
  531. }
  532. gfs2_log_unlock(sdp);
  533. lock_buffer(bh);
  534. clear_buffer_dirty(bh);
  535. bh->b_bdev = NULL;
  536. clear_buffer_mapped(bh);
  537. clear_buffer_req(bh);
  538. clear_buffer_new(bh);
  539. clear_buffer_delay(bh);
  540. unlock_buffer(bh);
  541. }
  542. static void gfs2_invalidatepage(struct page *page, unsigned long offset)
  543. {
  544. struct gfs2_sbd *sdp = GFS2_SB(page->mapping->host);
  545. struct buffer_head *head, *bh, *next;
  546. unsigned int curr_off = 0;
  547. BUG_ON(!PageLocked(page));
  548. if (!page_has_buffers(page))
  549. return;
  550. bh = head = page_buffers(page);
  551. do {
  552. unsigned int next_off = curr_off + bh->b_size;
  553. next = bh->b_this_page;
  554. if (offset <= curr_off)
  555. discard_buffer(sdp, bh);
  556. curr_off = next_off;
  557. bh = next;
  558. } while (bh != head);
  559. if (!offset)
  560. try_to_release_page(page, 0);
  561. return;
  562. }
  563. /**
  564. * gfs2_ok_for_dio - check that dio is valid on this file
  565. * @ip: The inode
  566. * @rw: READ or WRITE
  567. * @offset: The offset at which we are reading or writing
  568. *
  569. * Returns: 0 (to ignore the i/o request and thus fall back to buffered i/o)
  570. * 1 (to accept the i/o request)
  571. */
  572. static int gfs2_ok_for_dio(struct gfs2_inode *ip, int rw, loff_t offset)
  573. {
  574. /*
  575. * Should we return an error here? I can't see that O_DIRECT for
  576. * a journaled file makes any sense. For now we'll silently fall
  577. * back to buffered I/O, likewise we do the same for stuffed
  578. * files since they are (a) small and (b) unaligned.
  579. */
  580. if (gfs2_is_jdata(ip))
  581. return 0;
  582. if (gfs2_is_stuffed(ip))
  583. return 0;
  584. if (offset > i_size_read(&ip->i_inode))
  585. return 0;
  586. return 1;
  587. }
  588. static ssize_t gfs2_direct_IO(int rw, struct kiocb *iocb,
  589. const struct iovec *iov, loff_t offset,
  590. unsigned long nr_segs)
  591. {
  592. struct file *file = iocb->ki_filp;
  593. struct inode *inode = file->f_mapping->host;
  594. struct gfs2_inode *ip = GFS2_I(inode);
  595. struct gfs2_holder gh;
  596. int rv;
  597. /*
  598. * Deferred lock, even if its a write, since we do no allocation
  599. * on this path. All we need change is atime, and this lock mode
  600. * ensures that other nodes have flushed their buffered read caches
  601. * (i.e. their page cache entries for this inode). We do not,
  602. * unfortunately have the option of only flushing a range like
  603. * the VFS does.
  604. */
  605. gfs2_holder_init(ip->i_gl, LM_ST_DEFERRED, GL_ATIME, &gh);
  606. rv = gfs2_glock_nq_atime(&gh);
  607. if (rv)
  608. return rv;
  609. rv = gfs2_ok_for_dio(ip, rw, offset);
  610. if (rv != 1)
  611. goto out; /* dio not valid, fall back to buffered i/o */
  612. rv = blockdev_direct_IO_no_locking(rw, iocb, inode, inode->i_sb->s_bdev,
  613. iov, offset, nr_segs,
  614. gfs2_get_block_direct, NULL);
  615. out:
  616. gfs2_glock_dq_m(1, &gh);
  617. gfs2_holder_uninit(&gh);
  618. return rv;
  619. }
  620. /**
  621. * stuck_releasepage - We're stuck in gfs2_releasepage(). Print stuff out.
  622. * @bh: the buffer we're stuck on
  623. *
  624. */
  625. static void stuck_releasepage(struct buffer_head *bh)
  626. {
  627. struct inode *inode = bh->b_page->mapping->host;
  628. struct gfs2_sbd *sdp = inode->i_sb->s_fs_info;
  629. struct gfs2_bufdata *bd = bh->b_private;
  630. struct gfs2_glock *gl;
  631. static unsigned limit = 0;
  632. if (limit > 3)
  633. return;
  634. limit++;
  635. fs_warn(sdp, "stuck in gfs2_releasepage() %p\n", inode);
  636. fs_warn(sdp, "blkno = %llu, bh->b_count = %d\n",
  637. (unsigned long long)bh->b_blocknr, atomic_read(&bh->b_count));
  638. fs_warn(sdp, "pinned = %u\n", buffer_pinned(bh));
  639. fs_warn(sdp, "bh->b_private = %s\n", (bd) ? "!NULL" : "NULL");
  640. if (!bd)
  641. return;
  642. gl = bd->bd_gl;
  643. fs_warn(sdp, "gl = (%u, %llu)\n",
  644. gl->gl_name.ln_type, (unsigned long long)gl->gl_name.ln_number);
  645. fs_warn(sdp, "bd_list_tr = %s, bd_le.le_list = %s\n",
  646. (list_empty(&bd->bd_list_tr)) ? "no" : "yes",
  647. (list_empty(&bd->bd_le.le_list)) ? "no" : "yes");
  648. if (gl->gl_ops == &gfs2_inode_glops) {
  649. struct gfs2_inode *ip = gl->gl_object;
  650. unsigned int x;
  651. if (!ip)
  652. return;
  653. fs_warn(sdp, "ip = %llu %llu\n",
  654. (unsigned long long)ip->i_num.no_formal_ino,
  655. (unsigned long long)ip->i_num.no_addr);
  656. for (x = 0; x < GFS2_MAX_META_HEIGHT; x++)
  657. fs_warn(sdp, "ip->i_cache[%u] = %s\n",
  658. x, (ip->i_cache[x]) ? "!NULL" : "NULL");
  659. }
  660. }
  661. /**
  662. * gfs2_releasepage - free the metadata associated with a page
  663. * @page: the page that's being released
  664. * @gfp_mask: passed from Linux VFS, ignored by us
  665. *
  666. * Call try_to_free_buffers() if the buffers in this page can be
  667. * released.
  668. *
  669. * Returns: 0
  670. */
  671. int gfs2_releasepage(struct page *page, gfp_t gfp_mask)
  672. {
  673. struct inode *aspace = page->mapping->host;
  674. struct gfs2_sbd *sdp = aspace->i_sb->s_fs_info;
  675. struct buffer_head *bh, *head;
  676. struct gfs2_bufdata *bd;
  677. unsigned long t = jiffies + gfs2_tune_get(sdp, gt_stall_secs) * HZ;
  678. if (!page_has_buffers(page))
  679. goto out;
  680. head = bh = page_buffers(page);
  681. do {
  682. while (atomic_read(&bh->b_count)) {
  683. if (!atomic_read(&aspace->i_writecount))
  684. return 0;
  685. if (!(gfp_mask & __GFP_WAIT))
  686. return 0;
  687. if (time_after_eq(jiffies, t)) {
  688. stuck_releasepage(bh);
  689. /* should we withdraw here? */
  690. return 0;
  691. }
  692. yield();
  693. }
  694. gfs2_assert_warn(sdp, !buffer_pinned(bh));
  695. gfs2_assert_warn(sdp, !buffer_dirty(bh));
  696. gfs2_log_lock(sdp);
  697. bd = bh->b_private;
  698. if (bd) {
  699. gfs2_assert_warn(sdp, bd->bd_bh == bh);
  700. gfs2_assert_warn(sdp, list_empty(&bd->bd_list_tr));
  701. gfs2_assert_warn(sdp, !bd->bd_ail);
  702. bd->bd_bh = NULL;
  703. if (!list_empty(&bd->bd_le.le_list))
  704. bd = NULL;
  705. bh->b_private = NULL;
  706. }
  707. gfs2_log_unlock(sdp);
  708. if (bd)
  709. kmem_cache_free(gfs2_bufdata_cachep, bd);
  710. bh = bh->b_this_page;
  711. } while (bh != head);
  712. out:
  713. return try_to_free_buffers(page);
  714. }
  715. const struct address_space_operations gfs2_file_aops = {
  716. .writepage = gfs2_writepage,
  717. .writepages = gfs2_writepages,
  718. .readpage = gfs2_readpage,
  719. .readpages = gfs2_readpages,
  720. .sync_page = block_sync_page,
  721. .prepare_write = gfs2_prepare_write,
  722. .commit_write = gfs2_commit_write,
  723. .bmap = gfs2_bmap,
  724. .invalidatepage = gfs2_invalidatepage,
  725. .releasepage = gfs2_releasepage,
  726. .direct_IO = gfs2_direct_IO,
  727. };