ops_address.c 19 KB

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