ops_address.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  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/swap.h>
  20. #include <linux/gfs2_ondisk.h>
  21. #include <linux/lm_interface.h>
  22. #include "gfs2.h"
  23. #include "incore.h"
  24. #include "bmap.h"
  25. #include "glock.h"
  26. #include "inode.h"
  27. #include "log.h"
  28. #include "meta_io.h"
  29. #include "ops_address.h"
  30. #include "quota.h"
  31. #include "trans.h"
  32. #include "rgrp.h"
  33. #include "ops_file.h"
  34. #include "super.h"
  35. #include "util.h"
  36. #include "glops.h"
  37. static void gfs2_page_add_databufs(struct gfs2_inode *ip, struct page *page,
  38. unsigned int from, unsigned int to)
  39. {
  40. struct buffer_head *head = page_buffers(page);
  41. unsigned int bsize = head->b_size;
  42. struct buffer_head *bh;
  43. unsigned int start, end;
  44. for (bh = head, start = 0; bh != head || !start;
  45. bh = bh->b_this_page, start = end) {
  46. end = start + bsize;
  47. if (end <= from || start >= to)
  48. continue;
  49. if (gfs2_is_jdata(ip))
  50. set_buffer_uptodate(bh);
  51. gfs2_trans_add_bh(ip->i_gl, bh, 0);
  52. }
  53. }
  54. /**
  55. * gfs2_get_block - Fills in a buffer head with details about a block
  56. * @inode: The inode
  57. * @lblock: The block number to look up
  58. * @bh_result: The buffer head to return the result in
  59. * @create: Non-zero if we may add block to the file
  60. *
  61. * Returns: errno
  62. */
  63. int gfs2_get_block(struct inode *inode, sector_t lblock,
  64. struct buffer_head *bh_result, int create)
  65. {
  66. return gfs2_block_map(inode, lblock, create, bh_result);
  67. }
  68. /**
  69. * gfs2_get_block_noalloc - Fills in a buffer head with details about a block
  70. * @inode: The inode
  71. * @lblock: The block number to look up
  72. * @bh_result: The buffer head to return the result in
  73. * @create: Non-zero if we may add block to the file
  74. *
  75. * Returns: errno
  76. */
  77. static int gfs2_get_block_noalloc(struct inode *inode, sector_t lblock,
  78. struct buffer_head *bh_result, int create)
  79. {
  80. int error;
  81. error = gfs2_block_map(inode, lblock, 0, bh_result);
  82. if (error)
  83. return error;
  84. if (!buffer_mapped(bh_result))
  85. return -EIO;
  86. return 0;
  87. }
  88. static int gfs2_get_block_direct(struct inode *inode, sector_t lblock,
  89. struct buffer_head *bh_result, int create)
  90. {
  91. return gfs2_block_map(inode, lblock, 0, bh_result);
  92. }
  93. /**
  94. * gfs2_writepage - Write complete page
  95. * @page: Page to write
  96. *
  97. * Returns: errno
  98. *
  99. * Some of this is copied from block_write_full_page() although we still
  100. * call it to do most of the work.
  101. */
  102. static int gfs2_writepage(struct page *page, struct writeback_control *wbc)
  103. {
  104. struct inode *inode = page->mapping->host;
  105. struct gfs2_inode *ip = GFS2_I(inode);
  106. struct gfs2_sbd *sdp = GFS2_SB(inode);
  107. loff_t i_size = i_size_read(inode);
  108. pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
  109. unsigned offset;
  110. int error;
  111. int done_trans = 0;
  112. if (gfs2_assert_withdraw(sdp, gfs2_glock_is_held_excl(ip->i_gl))) {
  113. unlock_page(page);
  114. return -EIO;
  115. }
  116. if (current->journal_info)
  117. goto out_ignore;
  118. /* Is the page fully outside i_size? (truncate in progress) */
  119. offset = i_size & (PAGE_CACHE_SIZE-1);
  120. if (page->index > end_index || (page->index == end_index && !offset)) {
  121. page->mapping->a_ops->invalidatepage(page, 0);
  122. unlock_page(page);
  123. return 0; /* don't care */
  124. }
  125. if ((sdp->sd_args.ar_data == GFS2_DATA_ORDERED || gfs2_is_jdata(ip)) &&
  126. PageChecked(page)) {
  127. ClearPageChecked(page);
  128. error = gfs2_trans_begin(sdp, RES_DINODE + 1, 0);
  129. if (error)
  130. goto out_ignore;
  131. if (!page_has_buffers(page)) {
  132. create_empty_buffers(page, inode->i_sb->s_blocksize,
  133. (1 << BH_Dirty)|(1 << BH_Uptodate));
  134. }
  135. gfs2_page_add_databufs(ip, page, 0, sdp->sd_vfs->s_blocksize-1);
  136. done_trans = 1;
  137. }
  138. error = block_write_full_page(page, gfs2_get_block_noalloc, wbc);
  139. if (done_trans)
  140. gfs2_trans_end(sdp);
  141. gfs2_meta_cache_flush(ip);
  142. return error;
  143. out_ignore:
  144. redirty_page_for_writepage(wbc, page);
  145. unlock_page(page);
  146. return 0;
  147. }
  148. /**
  149. * gfs2_writepages - Write a bunch of dirty pages back to disk
  150. * @mapping: The mapping to write
  151. * @wbc: Write-back control
  152. *
  153. * For journaled files and/or ordered writes this just falls back to the
  154. * kernel's default writepages path for now. We will probably want to change
  155. * that eventually (i.e. when we look at allocate on flush).
  156. *
  157. * For the data=writeback case though we can already ignore buffer heads
  158. * and write whole extents at once. This is a big reduction in the
  159. * number of I/O requests we send and the bmap calls we make in this case.
  160. */
  161. static int gfs2_writepages(struct address_space *mapping,
  162. struct writeback_control *wbc)
  163. {
  164. struct inode *inode = mapping->host;
  165. struct gfs2_inode *ip = GFS2_I(inode);
  166. struct gfs2_sbd *sdp = GFS2_SB(inode);
  167. if (sdp->sd_args.ar_data == GFS2_DATA_WRITEBACK && !gfs2_is_jdata(ip))
  168. return mpage_writepages(mapping, wbc, gfs2_get_block_noalloc);
  169. return generic_writepages(mapping, wbc);
  170. }
  171. /**
  172. * stuffed_readpage - Fill in a Linux page with stuffed file data
  173. * @ip: the inode
  174. * @page: the page
  175. *
  176. * Returns: errno
  177. */
  178. static int stuffed_readpage(struct gfs2_inode *ip, struct page *page)
  179. {
  180. struct buffer_head *dibh;
  181. void *kaddr;
  182. int error;
  183. /*
  184. * Due to the order of unstuffing files and ->nopage(), we can be
  185. * asked for a zero page in the case of a stuffed file being extended,
  186. * so we need to supply one here. It doesn't happen often.
  187. */
  188. if (unlikely(page->index)) {
  189. zero_user_page(page, 0, PAGE_CACHE_SIZE, KM_USER0);
  190. return 0;
  191. }
  192. error = gfs2_meta_inode_buffer(ip, &dibh);
  193. if (error)
  194. return error;
  195. kaddr = kmap_atomic(page, KM_USER0);
  196. memcpy(kaddr, dibh->b_data + sizeof(struct gfs2_dinode),
  197. ip->i_di.di_size);
  198. memset(kaddr + ip->i_di.di_size, 0, PAGE_CACHE_SIZE - ip->i_di.di_size);
  199. kunmap_atomic(kaddr, KM_USER0);
  200. flush_dcache_page(page);
  201. brelse(dibh);
  202. SetPageUptodate(page);
  203. return 0;
  204. }
  205. /**
  206. * gfs2_readpage - readpage with locking
  207. * @file: The file to read a page for. N.B. This may be NULL if we are
  208. * reading an internal file.
  209. * @page: The page to read
  210. *
  211. * Returns: errno
  212. */
  213. static int gfs2_readpage(struct file *file, struct page *page)
  214. {
  215. struct gfs2_inode *ip = GFS2_I(page->mapping->host);
  216. struct gfs2_sbd *sdp = GFS2_SB(page->mapping->host);
  217. struct gfs2_file *gf = NULL;
  218. struct gfs2_holder gh;
  219. int error;
  220. int do_unlock = 0;
  221. if (likely(file != &gfs2_internal_file_sentinel)) {
  222. if (file) {
  223. gf = file->private_data;
  224. if (test_bit(GFF_EXLOCK, &gf->f_flags))
  225. /* gfs2_sharewrite_fault has grabbed the ip->i_gl already */
  226. goto skip_lock;
  227. }
  228. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME|LM_FLAG_TRY_1CB, &gh);
  229. do_unlock = 1;
  230. error = gfs2_glock_nq_atime(&gh);
  231. if (unlikely(error))
  232. goto out_unlock;
  233. }
  234. skip_lock:
  235. if (gfs2_is_stuffed(ip)) {
  236. error = stuffed_readpage(ip, page);
  237. unlock_page(page);
  238. } else
  239. error = mpage_readpage(page, gfs2_get_block);
  240. if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
  241. error = -EIO;
  242. if (do_unlock) {
  243. gfs2_glock_dq_m(1, &gh);
  244. gfs2_holder_uninit(&gh);
  245. }
  246. out:
  247. return error;
  248. out_unlock:
  249. unlock_page(page);
  250. if (error == GLR_TRYFAILED) {
  251. error = AOP_TRUNCATED_PAGE;
  252. yield();
  253. }
  254. if (do_unlock)
  255. gfs2_holder_uninit(&gh);
  256. goto out;
  257. }
  258. /**
  259. * gfs2_readpages - Read a bunch of pages at once
  260. *
  261. * Some notes:
  262. * 1. This is only for readahead, so we can simply ignore any things
  263. * which are slightly inconvenient (such as locking conflicts between
  264. * the page lock and the glock) and return having done no I/O. Its
  265. * obviously not something we'd want to do on too regular a basis.
  266. * Any I/O we ignore at this time will be done via readpage later.
  267. * 2. We don't handle stuffed files here we let readpage do the honours.
  268. * 3. mpage_readpages() does most of the heavy lifting in the common case.
  269. * 4. gfs2_get_block() is relied upon to set BH_Boundary in the right places.
  270. * 5. We use LM_FLAG_TRY_1CB here, effectively we then have lock-ahead as
  271. * well as read-ahead.
  272. */
  273. static int gfs2_readpages(struct file *file, struct address_space *mapping,
  274. struct list_head *pages, unsigned nr_pages)
  275. {
  276. struct inode *inode = mapping->host;
  277. struct gfs2_inode *ip = GFS2_I(inode);
  278. struct gfs2_sbd *sdp = GFS2_SB(inode);
  279. struct gfs2_holder gh;
  280. int ret = 0;
  281. int do_unlock = 0;
  282. if (likely(file != &gfs2_internal_file_sentinel)) {
  283. if (file) {
  284. struct gfs2_file *gf = file->private_data;
  285. if (test_bit(GFF_EXLOCK, &gf->f_flags))
  286. goto skip_lock;
  287. }
  288. gfs2_holder_init(ip->i_gl, LM_ST_SHARED,
  289. LM_FLAG_TRY_1CB|GL_ATIME, &gh);
  290. do_unlock = 1;
  291. ret = gfs2_glock_nq_atime(&gh);
  292. if (ret == GLR_TRYFAILED)
  293. goto out_noerror;
  294. if (unlikely(ret))
  295. goto out_unlock;
  296. }
  297. skip_lock:
  298. if (!gfs2_is_stuffed(ip))
  299. ret = mpage_readpages(mapping, pages, nr_pages, gfs2_get_block);
  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. if (do_unlock)
  312. gfs2_holder_uninit(&gh);
  313. goto out;
  314. }
  315. /**
  316. * gfs2_write_begin - Begin to write to a file
  317. * @file: The file to write to
  318. * @mapping: The mapping in which to write
  319. * @pos: The file offset at which to start writing
  320. * @len: Length of the write
  321. * @flags: Various flags
  322. * @pagep: Pointer to return the page
  323. * @fsdata: Pointer to return fs data (unused by GFS2)
  324. *
  325. * Returns: errno
  326. */
  327. static int gfs2_write_begin(struct file *file, struct address_space *mapping,
  328. loff_t pos, unsigned len, unsigned flags,
  329. struct page **pagep, void **fsdata)
  330. {
  331. struct gfs2_inode *ip = GFS2_I(mapping->host);
  332. struct gfs2_sbd *sdp = GFS2_SB(mapping->host);
  333. unsigned int data_blocks, ind_blocks, rblocks;
  334. int alloc_required;
  335. int error = 0;
  336. struct gfs2_alloc *al;
  337. pgoff_t index = pos >> PAGE_CACHE_SHIFT;
  338. unsigned from = pos & (PAGE_CACHE_SIZE - 1);
  339. unsigned to = from + len;
  340. struct page *page;
  341. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, GL_ATIME, &ip->i_gh);
  342. error = gfs2_glock_nq_atime(&ip->i_gh);
  343. if (unlikely(error))
  344. goto out_uninit;
  345. error = -ENOMEM;
  346. page = __grab_cache_page(mapping, index);
  347. *pagep = page;
  348. if (!page)
  349. goto out_unlock;
  350. gfs2_write_calc_reserv(ip, len, &data_blocks, &ind_blocks);
  351. error = gfs2_write_alloc_required(ip, pos, len, &alloc_required);
  352. if (error)
  353. goto out_putpage;
  354. ip->i_alloc.al_requested = 0;
  355. if (alloc_required) {
  356. al = gfs2_alloc_get(ip);
  357. error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
  358. if (error)
  359. goto out_alloc_put;
  360. error = gfs2_quota_check(ip, ip->i_inode.i_uid, ip->i_inode.i_gid);
  361. if (error)
  362. goto out_qunlock;
  363. al->al_requested = data_blocks + ind_blocks;
  364. error = gfs2_inplace_reserve(ip);
  365. if (error)
  366. goto out_qunlock;
  367. }
  368. rblocks = RES_DINODE + ind_blocks;
  369. if (gfs2_is_jdata(ip))
  370. rblocks += data_blocks ? data_blocks : 1;
  371. if (ind_blocks || data_blocks)
  372. rblocks += RES_STATFS + RES_QUOTA;
  373. error = gfs2_trans_begin(sdp, rblocks,
  374. PAGE_CACHE_SIZE/sdp->sd_sb.sb_bsize);
  375. if (error)
  376. goto out_trans_fail;
  377. if (gfs2_is_stuffed(ip)) {
  378. if (pos + len > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) {
  379. error = gfs2_unstuff_dinode(ip, page);
  380. if (error == 0)
  381. goto prepare_write;
  382. } else if (!PageUptodate(page))
  383. error = stuffed_readpage(ip, page);
  384. goto out;
  385. }
  386. prepare_write:
  387. error = block_prepare_write(page, from, to, gfs2_get_block);
  388. out:
  389. if (error) {
  390. gfs2_trans_end(sdp);
  391. out_trans_fail:
  392. if (alloc_required) {
  393. gfs2_inplace_release(ip);
  394. out_qunlock:
  395. gfs2_quota_unlock(ip);
  396. out_alloc_put:
  397. gfs2_alloc_put(ip);
  398. }
  399. out_putpage:
  400. page_cache_release(page);
  401. if (pos + len > ip->i_inode.i_size)
  402. vmtruncate(&ip->i_inode, ip->i_inode.i_size);
  403. out_unlock:
  404. gfs2_glock_dq_m(1, &ip->i_gh);
  405. out_uninit:
  406. gfs2_holder_uninit(&ip->i_gh);
  407. }
  408. return error;
  409. }
  410. /**
  411. * adjust_fs_space - Adjusts the free space available due to gfs2_grow
  412. * @inode: the rindex inode
  413. */
  414. static void adjust_fs_space(struct inode *inode)
  415. {
  416. struct gfs2_sbd *sdp = inode->i_sb->s_fs_info;
  417. struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
  418. struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
  419. u64 fs_total, new_free;
  420. /* Total up the file system space, according to the latest rindex. */
  421. fs_total = gfs2_ri_total(sdp);
  422. spin_lock(&sdp->sd_statfs_spin);
  423. if (fs_total > (m_sc->sc_total + l_sc->sc_total))
  424. new_free = fs_total - (m_sc->sc_total + l_sc->sc_total);
  425. else
  426. new_free = 0;
  427. spin_unlock(&sdp->sd_statfs_spin);
  428. fs_warn(sdp, "File system extended by %llu blocks.\n",
  429. (unsigned long long)new_free);
  430. gfs2_statfs_change(sdp, new_free, new_free, 0);
  431. }
  432. /**
  433. * gfs2_stuffed_write_end - Write end for stuffed files
  434. * @inode: The inode
  435. * @dibh: The buffer_head containing the on-disk inode
  436. * @pos: The file position
  437. * @len: The length of the write
  438. * @copied: How much was actually copied by the VFS
  439. * @page: The page
  440. *
  441. * This copies the data from the page into the inode block after
  442. * the inode data structure itself.
  443. *
  444. * Returns: errno
  445. */
  446. static int gfs2_stuffed_write_end(struct inode *inode, struct buffer_head *dibh,
  447. loff_t pos, unsigned len, unsigned copied,
  448. struct page *page)
  449. {
  450. struct gfs2_inode *ip = GFS2_I(inode);
  451. struct gfs2_sbd *sdp = GFS2_SB(inode);
  452. u64 to = pos + copied;
  453. void *kaddr;
  454. unsigned char *buf = dibh->b_data + sizeof(struct gfs2_dinode);
  455. struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
  456. BUG_ON((pos + len) > (dibh->b_size - sizeof(struct gfs2_dinode)));
  457. kaddr = kmap_atomic(page, KM_USER0);
  458. memcpy(buf + pos, kaddr + pos, copied);
  459. memset(kaddr + pos + copied, 0, len - copied);
  460. flush_dcache_page(page);
  461. kunmap_atomic(kaddr, KM_USER0);
  462. if (!PageUptodate(page))
  463. SetPageUptodate(page);
  464. unlock_page(page);
  465. page_cache_release(page);
  466. if (inode->i_size < to) {
  467. i_size_write(inode, to);
  468. ip->i_di.di_size = inode->i_size;
  469. di->di_size = cpu_to_be64(inode->i_size);
  470. mark_inode_dirty(inode);
  471. }
  472. if (inode == sdp->sd_rindex)
  473. adjust_fs_space(inode);
  474. brelse(dibh);
  475. gfs2_trans_end(sdp);
  476. gfs2_glock_dq(&ip->i_gh);
  477. gfs2_holder_uninit(&ip->i_gh);
  478. return copied;
  479. }
  480. /**
  481. * gfs2_write_end
  482. * @file: The file to write to
  483. * @mapping: The address space to write to
  484. * @pos: The file position
  485. * @len: The length of the data
  486. * @copied:
  487. * @page: The page that has been written
  488. * @fsdata: The fsdata (unused in GFS2)
  489. *
  490. * The main write_end function for GFS2. We have a separate one for
  491. * stuffed files as they are slightly different, otherwise we just
  492. * put our locking around the VFS provided functions.
  493. *
  494. * Returns: errno
  495. */
  496. static int gfs2_write_end(struct file *file, struct address_space *mapping,
  497. loff_t pos, unsigned len, unsigned copied,
  498. struct page *page, void *fsdata)
  499. {
  500. struct inode *inode = page->mapping->host;
  501. struct gfs2_inode *ip = GFS2_I(inode);
  502. struct gfs2_sbd *sdp = GFS2_SB(inode);
  503. struct buffer_head *dibh;
  504. struct gfs2_alloc *al = &ip->i_alloc;
  505. struct gfs2_dinode *di;
  506. unsigned int from = pos & (PAGE_CACHE_SIZE - 1);
  507. unsigned int to = from + len;
  508. int ret;
  509. BUG_ON(gfs2_glock_is_locked_by_me(ip->i_gl) == 0);
  510. ret = gfs2_meta_inode_buffer(ip, &dibh);
  511. if (unlikely(ret)) {
  512. unlock_page(page);
  513. page_cache_release(page);
  514. goto failed;
  515. }
  516. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  517. if (gfs2_is_stuffed(ip))
  518. return gfs2_stuffed_write_end(inode, dibh, pos, len, copied, page);
  519. if (sdp->sd_args.ar_data == GFS2_DATA_ORDERED || gfs2_is_jdata(ip))
  520. gfs2_page_add_databufs(ip, page, from, to);
  521. ret = generic_write_end(file, mapping, pos, len, copied, page, fsdata);
  522. if (likely(ret >= 0)) {
  523. copied = ret;
  524. if ((pos + copied) > inode->i_size) {
  525. di = (struct gfs2_dinode *)dibh->b_data;
  526. ip->i_di.di_size = inode->i_size;
  527. di->di_size = cpu_to_be64(inode->i_size);
  528. mark_inode_dirty(inode);
  529. }
  530. }
  531. if (inode == sdp->sd_rindex)
  532. adjust_fs_space(inode);
  533. brelse(dibh);
  534. gfs2_trans_end(sdp);
  535. failed:
  536. if (al->al_requested) {
  537. gfs2_inplace_release(ip);
  538. gfs2_quota_unlock(ip);
  539. gfs2_alloc_put(ip);
  540. }
  541. gfs2_glock_dq(&ip->i_gh);
  542. gfs2_holder_uninit(&ip->i_gh);
  543. return ret;
  544. }
  545. /**
  546. * gfs2_set_page_dirty - Page dirtying function
  547. * @page: The page to dirty
  548. *
  549. * Returns: 1 if it dirtyed the page, or 0 otherwise
  550. */
  551. static int gfs2_set_page_dirty(struct page *page)
  552. {
  553. struct gfs2_inode *ip = GFS2_I(page->mapping->host);
  554. struct gfs2_sbd *sdp = GFS2_SB(page->mapping->host);
  555. if (sdp->sd_args.ar_data == GFS2_DATA_ORDERED || gfs2_is_jdata(ip))
  556. SetPageChecked(page);
  557. return __set_page_dirty_buffers(page);
  558. }
  559. /**
  560. * gfs2_bmap - Block map function
  561. * @mapping: Address space info
  562. * @lblock: The block to map
  563. *
  564. * Returns: The disk address for the block or 0 on hole or error
  565. */
  566. static sector_t gfs2_bmap(struct address_space *mapping, sector_t lblock)
  567. {
  568. struct gfs2_inode *ip = GFS2_I(mapping->host);
  569. struct gfs2_holder i_gh;
  570. sector_t dblock = 0;
  571. int error;
  572. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
  573. if (error)
  574. return 0;
  575. if (!gfs2_is_stuffed(ip))
  576. dblock = generic_block_bmap(mapping, lblock, gfs2_get_block);
  577. gfs2_glock_dq_uninit(&i_gh);
  578. return dblock;
  579. }
  580. static void gfs2_discard(struct gfs2_sbd *sdp, struct buffer_head *bh)
  581. {
  582. struct gfs2_bufdata *bd;
  583. lock_buffer(bh);
  584. gfs2_log_lock(sdp);
  585. clear_buffer_dirty(bh);
  586. bd = bh->b_private;
  587. if (bd) {
  588. if (!list_empty(&bd->bd_le.le_list) && !buffer_pinned(bh))
  589. list_del_init(&bd->bd_le.le_list);
  590. else
  591. gfs2_remove_from_journal(bh, current->journal_info, 0);
  592. }
  593. bh->b_bdev = NULL;
  594. clear_buffer_mapped(bh);
  595. clear_buffer_req(bh);
  596. clear_buffer_new(bh);
  597. gfs2_log_unlock(sdp);
  598. unlock_buffer(bh);
  599. }
  600. static void gfs2_invalidatepage(struct page *page, unsigned long offset)
  601. {
  602. struct gfs2_sbd *sdp = GFS2_SB(page->mapping->host);
  603. struct buffer_head *bh, *head;
  604. unsigned long pos = 0;
  605. BUG_ON(!PageLocked(page));
  606. if (offset == 0)
  607. ClearPageChecked(page);
  608. if (!page_has_buffers(page))
  609. goto out;
  610. bh = head = page_buffers(page);
  611. do {
  612. if (offset <= pos)
  613. gfs2_discard(sdp, bh);
  614. pos += bh->b_size;
  615. bh = bh->b_this_page;
  616. } while (bh != head);
  617. out:
  618. if (offset == 0)
  619. try_to_release_page(page, 0);
  620. }
  621. /**
  622. * gfs2_ok_for_dio - check that dio is valid on this file
  623. * @ip: The inode
  624. * @rw: READ or WRITE
  625. * @offset: The offset at which we are reading or writing
  626. *
  627. * Returns: 0 (to ignore the i/o request and thus fall back to buffered i/o)
  628. * 1 (to accept the i/o request)
  629. */
  630. static int gfs2_ok_for_dio(struct gfs2_inode *ip, int rw, loff_t offset)
  631. {
  632. /*
  633. * Should we return an error here? I can't see that O_DIRECT for
  634. * a journaled file makes any sense. For now we'll silently fall
  635. * back to buffered I/O, likewise we do the same for stuffed
  636. * files since they are (a) small and (b) unaligned.
  637. */
  638. if (gfs2_is_jdata(ip))
  639. return 0;
  640. if (gfs2_is_stuffed(ip))
  641. return 0;
  642. if (offset > i_size_read(&ip->i_inode))
  643. return 0;
  644. return 1;
  645. }
  646. static ssize_t gfs2_direct_IO(int rw, struct kiocb *iocb,
  647. const struct iovec *iov, loff_t offset,
  648. unsigned long nr_segs)
  649. {
  650. struct file *file = iocb->ki_filp;
  651. struct inode *inode = file->f_mapping->host;
  652. struct gfs2_inode *ip = GFS2_I(inode);
  653. struct gfs2_holder gh;
  654. int rv;
  655. /*
  656. * Deferred lock, even if its a write, since we do no allocation
  657. * on this path. All we need change is atime, and this lock mode
  658. * ensures that other nodes have flushed their buffered read caches
  659. * (i.e. their page cache entries for this inode). We do not,
  660. * unfortunately have the option of only flushing a range like
  661. * the VFS does.
  662. */
  663. gfs2_holder_init(ip->i_gl, LM_ST_DEFERRED, GL_ATIME, &gh);
  664. rv = gfs2_glock_nq_atime(&gh);
  665. if (rv)
  666. return rv;
  667. rv = gfs2_ok_for_dio(ip, rw, offset);
  668. if (rv != 1)
  669. goto out; /* dio not valid, fall back to buffered i/o */
  670. rv = blockdev_direct_IO_no_locking(rw, iocb, inode, inode->i_sb->s_bdev,
  671. iov, offset, nr_segs,
  672. gfs2_get_block_direct, NULL);
  673. out:
  674. gfs2_glock_dq_m(1, &gh);
  675. gfs2_holder_uninit(&gh);
  676. return rv;
  677. }
  678. /**
  679. * gfs2_releasepage - free the metadata associated with a page
  680. * @page: the page that's being released
  681. * @gfp_mask: passed from Linux VFS, ignored by us
  682. *
  683. * Call try_to_free_buffers() if the buffers in this page can be
  684. * released.
  685. *
  686. * Returns: 0
  687. */
  688. int gfs2_releasepage(struct page *page, gfp_t gfp_mask)
  689. {
  690. struct inode *aspace = page->mapping->host;
  691. struct gfs2_sbd *sdp = aspace->i_sb->s_fs_info;
  692. struct buffer_head *bh, *head;
  693. struct gfs2_bufdata *bd;
  694. if (!page_has_buffers(page))
  695. return 0;
  696. gfs2_log_lock(sdp);
  697. head = bh = page_buffers(page);
  698. do {
  699. if (atomic_read(&bh->b_count))
  700. goto cannot_release;
  701. bd = bh->b_private;
  702. if (bd && bd->bd_ail)
  703. goto cannot_release;
  704. gfs2_assert_warn(sdp, !buffer_pinned(bh));
  705. gfs2_assert_warn(sdp, !buffer_dirty(bh));
  706. bh = bh->b_this_page;
  707. } while(bh != head);
  708. gfs2_log_unlock(sdp);
  709. head = bh = page_buffers(page);
  710. do {
  711. gfs2_log_lock(sdp);
  712. bd = bh->b_private;
  713. if (bd) {
  714. gfs2_assert_warn(sdp, bd->bd_bh == bh);
  715. gfs2_assert_warn(sdp, list_empty(&bd->bd_list_tr));
  716. if (!list_empty(&bd->bd_le.le_list)) {
  717. if (!buffer_pinned(bh))
  718. list_del_init(&bd->bd_le.le_list);
  719. else
  720. bd = NULL;
  721. }
  722. if (bd)
  723. bd->bd_bh = NULL;
  724. bh->b_private = NULL;
  725. }
  726. gfs2_log_unlock(sdp);
  727. if (bd)
  728. kmem_cache_free(gfs2_bufdata_cachep, bd);
  729. bh = bh->b_this_page;
  730. } while (bh != head);
  731. return try_to_free_buffers(page);
  732. cannot_release:
  733. gfs2_log_unlock(sdp);
  734. return 0;
  735. }
  736. const struct address_space_operations gfs2_file_aops = {
  737. .writepage = gfs2_writepage,
  738. .writepages = gfs2_writepages,
  739. .readpage = gfs2_readpage,
  740. .readpages = gfs2_readpages,
  741. .sync_page = block_sync_page,
  742. .write_begin = gfs2_write_begin,
  743. .write_end = gfs2_write_end,
  744. .set_page_dirty = gfs2_set_page_dirty,
  745. .bmap = gfs2_bmap,
  746. .invalidatepage = gfs2_invalidatepage,
  747. .releasepage = gfs2_releasepage,
  748. .direct_IO = gfs2_direct_IO,
  749. };