ops_address.c 19 KB

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