ops_address.c 19 KB

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