ops_address.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  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 <linux/swap.h>
  23. #include "gfs2.h"
  24. #include "incore.h"
  25. #include "bmap.h"
  26. #include "glock.h"
  27. #include "inode.h"
  28. #include "log.h"
  29. #include "meta_io.h"
  30. #include "ops_address.h"
  31. #include "quota.h"
  32. #include "trans.h"
  33. #include "rgrp.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 (PageChecked(page)) {
  126. error = gfs2_trans_begin(sdp, RES_DINODE + 1, 0);
  127. if (error)
  128. goto out_ignore;
  129. ClearPageChecked(page);
  130. if (!page_has_buffers(page)) {
  131. create_empty_buffers(page, inode->i_sb->s_blocksize,
  132. (1 << BH_Dirty)|(1 << BH_Uptodate));
  133. }
  134. gfs2_page_add_databufs(ip, page, 0, sdp->sd_vfs->s_blocksize-1);
  135. done_trans = 1;
  136. }
  137. error = block_write_full_page(page, gfs2_get_block_noalloc, wbc);
  138. if (done_trans)
  139. gfs2_trans_end(sdp);
  140. return error;
  141. out_ignore:
  142. redirty_page_for_writepage(wbc, page);
  143. unlock_page(page);
  144. return 0;
  145. }
  146. /**
  147. * gfs2_writeback_writepages - Write a bunch of dirty pages back to disk
  148. * @mapping: The mapping to write
  149. * @wbc: Write-back control
  150. *
  151. * For the data=writeback case we can already ignore buffer heads
  152. * and write whole extents at once. This is a big reduction in the
  153. * number of I/O requests we send and the bmap calls we make in this case.
  154. */
  155. static int gfs2_writeback_writepages(struct address_space *mapping,
  156. struct writeback_control *wbc)
  157. {
  158. return mpage_writepages(mapping, wbc, gfs2_get_block_noalloc);
  159. }
  160. /**
  161. * stuffed_readpage - Fill in a Linux page with stuffed file data
  162. * @ip: the inode
  163. * @page: the page
  164. *
  165. * Returns: errno
  166. */
  167. static int stuffed_readpage(struct gfs2_inode *ip, struct page *page)
  168. {
  169. struct buffer_head *dibh;
  170. void *kaddr;
  171. int error;
  172. /*
  173. * Due to the order of unstuffing files and ->nopage(), we can be
  174. * asked for a zero page in the case of a stuffed file being extended,
  175. * so we need to supply one here. It doesn't happen often.
  176. */
  177. if (unlikely(page->index)) {
  178. zero_user_page(page, 0, PAGE_CACHE_SIZE, KM_USER0);
  179. return 0;
  180. }
  181. error = gfs2_meta_inode_buffer(ip, &dibh);
  182. if (error)
  183. return error;
  184. kaddr = kmap_atomic(page, KM_USER0);
  185. memcpy(kaddr, dibh->b_data + sizeof(struct gfs2_dinode),
  186. ip->i_di.di_size);
  187. memset(kaddr + ip->i_di.di_size, 0, PAGE_CACHE_SIZE - ip->i_di.di_size);
  188. kunmap_atomic(kaddr, KM_USER0);
  189. flush_dcache_page(page);
  190. brelse(dibh);
  191. SetPageUptodate(page);
  192. return 0;
  193. }
  194. /**
  195. * __gfs2_readpage - readpage
  196. * @file: The file to read a page for
  197. * @page: The page to read
  198. *
  199. * This is the core of gfs2's readpage. Its used by the internal file
  200. * reading code as in that case we already hold the glock. Also its
  201. * called by gfs2_readpage() once the required lock has been granted.
  202. *
  203. */
  204. static int __gfs2_readpage(void *file, struct page *page)
  205. {
  206. struct gfs2_inode *ip = GFS2_I(page->mapping->host);
  207. struct gfs2_sbd *sdp = GFS2_SB(page->mapping->host);
  208. int error;
  209. if (gfs2_is_stuffed(ip)) {
  210. error = stuffed_readpage(ip, page);
  211. unlock_page(page);
  212. } else {
  213. error = mpage_readpage(page, gfs2_get_block);
  214. }
  215. if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
  216. return -EIO;
  217. return error;
  218. }
  219. /**
  220. * gfs2_readpage - read a page of a file
  221. * @file: The file to read
  222. * @page: The page of the file
  223. *
  224. * This deals with the locking required. We use a trylock in order to
  225. * avoid the page lock / glock ordering problems returning AOP_TRUNCATED_PAGE
  226. * in the event that we are unable to get the lock.
  227. */
  228. static int gfs2_readpage(struct file *file, struct page *page)
  229. {
  230. struct gfs2_inode *ip = GFS2_I(page->mapping->host);
  231. struct gfs2_holder gh;
  232. int error;
  233. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME|LM_FLAG_TRY_1CB, &gh);
  234. error = gfs2_glock_nq_atime(&gh);
  235. if (unlikely(error)) {
  236. unlock_page(page);
  237. goto out;
  238. }
  239. error = __gfs2_readpage(file, page);
  240. gfs2_glock_dq(&gh);
  241. out:
  242. gfs2_holder_uninit(&gh);
  243. if (error == GLR_TRYFAILED) {
  244. yield();
  245. return AOP_TRUNCATED_PAGE;
  246. }
  247. return error;
  248. }
  249. /**
  250. * gfs2_internal_read - read an internal file
  251. * @ip: The gfs2 inode
  252. * @ra_state: The readahead state (or NULL for no readahead)
  253. * @buf: The buffer to fill
  254. * @pos: The file position
  255. * @size: The amount to read
  256. *
  257. */
  258. int gfs2_internal_read(struct gfs2_inode *ip, struct file_ra_state *ra_state,
  259. char *buf, loff_t *pos, unsigned size)
  260. {
  261. struct address_space *mapping = ip->i_inode.i_mapping;
  262. unsigned long index = *pos / PAGE_CACHE_SIZE;
  263. unsigned offset = *pos & (PAGE_CACHE_SIZE - 1);
  264. unsigned copied = 0;
  265. unsigned amt;
  266. struct page *page;
  267. void *p;
  268. do {
  269. amt = size - copied;
  270. if (offset + size > PAGE_CACHE_SIZE)
  271. amt = PAGE_CACHE_SIZE - offset;
  272. page = read_cache_page(mapping, index, __gfs2_readpage, NULL);
  273. if (IS_ERR(page))
  274. return PTR_ERR(page);
  275. p = kmap_atomic(page, KM_USER0);
  276. memcpy(buf + copied, p + offset, amt);
  277. kunmap_atomic(p, KM_USER0);
  278. mark_page_accessed(page);
  279. page_cache_release(page);
  280. copied += amt;
  281. index++;
  282. offset = 0;
  283. } while(copied < size);
  284. (*pos) += size;
  285. return size;
  286. }
  287. /**
  288. * gfs2_readpages - Read a bunch of pages at once
  289. *
  290. * Some notes:
  291. * 1. This is only for readahead, so we can simply ignore any things
  292. * which are slightly inconvenient (such as locking conflicts between
  293. * the page lock and the glock) and return having done no I/O. Its
  294. * obviously not something we'd want to do on too regular a basis.
  295. * Any I/O we ignore at this time will be done via readpage later.
  296. * 2. We don't handle stuffed files here we let readpage do the honours.
  297. * 3. mpage_readpages() does most of the heavy lifting in the common case.
  298. * 4. gfs2_get_block() is relied upon to set BH_Boundary in the right places.
  299. */
  300. static int gfs2_readpages(struct file *file, struct address_space *mapping,
  301. struct list_head *pages, unsigned nr_pages)
  302. {
  303. struct inode *inode = mapping->host;
  304. struct gfs2_inode *ip = GFS2_I(inode);
  305. struct gfs2_sbd *sdp = GFS2_SB(inode);
  306. struct gfs2_holder gh;
  307. int ret;
  308. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &gh);
  309. ret = gfs2_glock_nq_atime(&gh);
  310. if (unlikely(ret))
  311. goto out_uninit;
  312. if (!gfs2_is_stuffed(ip))
  313. ret = mpage_readpages(mapping, pages, nr_pages, gfs2_get_block);
  314. gfs2_glock_dq(&gh);
  315. out_uninit:
  316. gfs2_holder_uninit(&gh);
  317. if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
  318. ret = -EIO;
  319. return ret;
  320. }
  321. /**
  322. * gfs2_write_begin - Begin to write to a file
  323. * @file: The file to write to
  324. * @mapping: The mapping in which to write
  325. * @pos: The file offset at which to start writing
  326. * @len: Length of the write
  327. * @flags: Various flags
  328. * @pagep: Pointer to return the page
  329. * @fsdata: Pointer to return fs data (unused by GFS2)
  330. *
  331. * Returns: errno
  332. */
  333. static int gfs2_write_begin(struct file *file, struct address_space *mapping,
  334. loff_t pos, unsigned len, unsigned flags,
  335. struct page **pagep, void **fsdata)
  336. {
  337. struct gfs2_inode *ip = GFS2_I(mapping->host);
  338. struct gfs2_sbd *sdp = GFS2_SB(mapping->host);
  339. unsigned int data_blocks, ind_blocks, rblocks;
  340. int alloc_required;
  341. int error = 0;
  342. struct gfs2_alloc *al;
  343. pgoff_t index = pos >> PAGE_CACHE_SHIFT;
  344. unsigned from = pos & (PAGE_CACHE_SIZE - 1);
  345. unsigned to = from + len;
  346. struct page *page;
  347. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, GL_ATIME, &ip->i_gh);
  348. error = gfs2_glock_nq_atime(&ip->i_gh);
  349. if (unlikely(error))
  350. goto out_uninit;
  351. error = -ENOMEM;
  352. page = __grab_cache_page(mapping, index);
  353. *pagep = page;
  354. if (!page)
  355. goto out_unlock;
  356. gfs2_write_calc_reserv(ip, len, &data_blocks, &ind_blocks);
  357. error = gfs2_write_alloc_required(ip, pos, len, &alloc_required);
  358. if (error)
  359. goto out_putpage;
  360. ip->i_alloc.al_requested = 0;
  361. if (alloc_required) {
  362. al = gfs2_alloc_get(ip);
  363. error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
  364. if (error)
  365. goto out_alloc_put;
  366. error = gfs2_quota_check(ip, ip->i_inode.i_uid, ip->i_inode.i_gid);
  367. if (error)
  368. goto out_qunlock;
  369. al->al_requested = data_blocks + ind_blocks;
  370. error = gfs2_inplace_reserve(ip);
  371. if (error)
  372. goto out_qunlock;
  373. }
  374. rblocks = RES_DINODE + ind_blocks;
  375. if (gfs2_is_jdata(ip))
  376. rblocks += data_blocks ? data_blocks : 1;
  377. if (ind_blocks || data_blocks)
  378. rblocks += RES_STATFS + RES_QUOTA;
  379. error = gfs2_trans_begin(sdp, rblocks,
  380. PAGE_CACHE_SIZE/sdp->sd_sb.sb_bsize);
  381. if (error)
  382. goto out_trans_fail;
  383. if (gfs2_is_stuffed(ip)) {
  384. if (pos + len > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) {
  385. error = gfs2_unstuff_dinode(ip, page);
  386. if (error == 0)
  387. goto prepare_write;
  388. } else if (!PageUptodate(page))
  389. error = stuffed_readpage(ip, page);
  390. goto out;
  391. }
  392. prepare_write:
  393. error = block_prepare_write(page, from, to, gfs2_get_block);
  394. out:
  395. if (error) {
  396. gfs2_trans_end(sdp);
  397. out_trans_fail:
  398. if (alloc_required) {
  399. gfs2_inplace_release(ip);
  400. out_qunlock:
  401. gfs2_quota_unlock(ip);
  402. out_alloc_put:
  403. gfs2_alloc_put(ip);
  404. }
  405. out_putpage:
  406. page_cache_release(page);
  407. if (pos + len > ip->i_inode.i_size)
  408. vmtruncate(&ip->i_inode, ip->i_inode.i_size);
  409. out_unlock:
  410. gfs2_glock_dq_m(1, &ip->i_gh);
  411. out_uninit:
  412. gfs2_holder_uninit(&ip->i_gh);
  413. }
  414. return error;
  415. }
  416. /**
  417. * adjust_fs_space - Adjusts the free space available due to gfs2_grow
  418. * @inode: the rindex inode
  419. */
  420. static void adjust_fs_space(struct inode *inode)
  421. {
  422. struct gfs2_sbd *sdp = inode->i_sb->s_fs_info;
  423. struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
  424. struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
  425. u64 fs_total, new_free;
  426. /* Total up the file system space, according to the latest rindex. */
  427. fs_total = gfs2_ri_total(sdp);
  428. spin_lock(&sdp->sd_statfs_spin);
  429. if (fs_total > (m_sc->sc_total + l_sc->sc_total))
  430. new_free = fs_total - (m_sc->sc_total + l_sc->sc_total);
  431. else
  432. new_free = 0;
  433. spin_unlock(&sdp->sd_statfs_spin);
  434. fs_warn(sdp, "File system extended by %llu blocks.\n",
  435. (unsigned long long)new_free);
  436. gfs2_statfs_change(sdp, new_free, new_free, 0);
  437. }
  438. /**
  439. * gfs2_stuffed_write_end - Write end for stuffed files
  440. * @inode: The inode
  441. * @dibh: The buffer_head containing the on-disk inode
  442. * @pos: The file position
  443. * @len: The length of the write
  444. * @copied: How much was actually copied by the VFS
  445. * @page: The page
  446. *
  447. * This copies the data from the page into the inode block after
  448. * the inode data structure itself.
  449. *
  450. * Returns: errno
  451. */
  452. static int gfs2_stuffed_write_end(struct inode *inode, struct buffer_head *dibh,
  453. loff_t pos, unsigned len, unsigned copied,
  454. struct page *page)
  455. {
  456. struct gfs2_inode *ip = GFS2_I(inode);
  457. struct gfs2_sbd *sdp = GFS2_SB(inode);
  458. u64 to = pos + copied;
  459. void *kaddr;
  460. unsigned char *buf = dibh->b_data + sizeof(struct gfs2_dinode);
  461. struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
  462. BUG_ON((pos + len) > (dibh->b_size - sizeof(struct gfs2_dinode)));
  463. kaddr = kmap_atomic(page, KM_USER0);
  464. memcpy(buf + pos, kaddr + pos, copied);
  465. memset(kaddr + pos + copied, 0, len - copied);
  466. flush_dcache_page(page);
  467. kunmap_atomic(kaddr, KM_USER0);
  468. if (!PageUptodate(page))
  469. SetPageUptodate(page);
  470. unlock_page(page);
  471. page_cache_release(page);
  472. if (inode->i_size < to) {
  473. i_size_write(inode, to);
  474. ip->i_di.di_size = inode->i_size;
  475. di->di_size = cpu_to_be64(inode->i_size);
  476. mark_inode_dirty(inode);
  477. }
  478. if (inode == sdp->sd_rindex)
  479. adjust_fs_space(inode);
  480. brelse(dibh);
  481. gfs2_trans_end(sdp);
  482. gfs2_glock_dq(&ip->i_gh);
  483. gfs2_holder_uninit(&ip->i_gh);
  484. return copied;
  485. }
  486. /**
  487. * gfs2_write_end
  488. * @file: The file to write to
  489. * @mapping: The address space to write to
  490. * @pos: The file position
  491. * @len: The length of the data
  492. * @copied:
  493. * @page: The page that has been written
  494. * @fsdata: The fsdata (unused in GFS2)
  495. *
  496. * The main write_end function for GFS2. We have a separate one for
  497. * stuffed files as they are slightly different, otherwise we just
  498. * put our locking around the VFS provided functions.
  499. *
  500. * Returns: errno
  501. */
  502. static int gfs2_write_end(struct file *file, struct address_space *mapping,
  503. loff_t pos, unsigned len, unsigned copied,
  504. struct page *page, void *fsdata)
  505. {
  506. struct inode *inode = page->mapping->host;
  507. struct gfs2_inode *ip = GFS2_I(inode);
  508. struct gfs2_sbd *sdp = GFS2_SB(inode);
  509. struct buffer_head *dibh;
  510. struct gfs2_alloc *al = &ip->i_alloc;
  511. struct gfs2_dinode *di;
  512. unsigned int from = pos & (PAGE_CACHE_SIZE - 1);
  513. unsigned int to = from + len;
  514. int ret;
  515. BUG_ON(gfs2_glock_is_locked_by_me(ip->i_gl) == 0);
  516. ret = gfs2_meta_inode_buffer(ip, &dibh);
  517. if (unlikely(ret)) {
  518. unlock_page(page);
  519. page_cache_release(page);
  520. goto failed;
  521. }
  522. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  523. if (gfs2_is_stuffed(ip))
  524. return gfs2_stuffed_write_end(inode, dibh, pos, len, copied, page);
  525. if (!gfs2_is_writeback(ip))
  526. gfs2_page_add_databufs(ip, page, from, to);
  527. ret = generic_write_end(file, mapping, pos, len, copied, page, fsdata);
  528. if (likely(ret >= 0)) {
  529. copied = ret;
  530. if ((pos + copied) > inode->i_size) {
  531. di = (struct gfs2_dinode *)dibh->b_data;
  532. ip->i_di.di_size = inode->i_size;
  533. di->di_size = cpu_to_be64(inode->i_size);
  534. mark_inode_dirty(inode);
  535. }
  536. }
  537. if (inode == sdp->sd_rindex)
  538. adjust_fs_space(inode);
  539. brelse(dibh);
  540. gfs2_trans_end(sdp);
  541. failed:
  542. if (al->al_requested) {
  543. gfs2_inplace_release(ip);
  544. gfs2_quota_unlock(ip);
  545. gfs2_alloc_put(ip);
  546. }
  547. gfs2_glock_dq(&ip->i_gh);
  548. gfs2_holder_uninit(&ip->i_gh);
  549. return ret;
  550. }
  551. /**
  552. * gfs2_set_page_dirty - Page dirtying function
  553. * @page: The page to dirty
  554. *
  555. * Returns: 1 if it dirtyed the page, or 0 otherwise
  556. */
  557. static int gfs2_set_page_dirty(struct page *page)
  558. {
  559. SetPageChecked(page);
  560. return __set_page_dirty_buffers(page);
  561. }
  562. /**
  563. * gfs2_bmap - Block map function
  564. * @mapping: Address space info
  565. * @lblock: The block to map
  566. *
  567. * Returns: The disk address for the block or 0 on hole or error
  568. */
  569. static sector_t gfs2_bmap(struct address_space *mapping, sector_t lblock)
  570. {
  571. struct gfs2_inode *ip = GFS2_I(mapping->host);
  572. struct gfs2_holder i_gh;
  573. sector_t dblock = 0;
  574. int error;
  575. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
  576. if (error)
  577. return 0;
  578. if (!gfs2_is_stuffed(ip))
  579. dblock = generic_block_bmap(mapping, lblock, gfs2_get_block);
  580. gfs2_glock_dq_uninit(&i_gh);
  581. return dblock;
  582. }
  583. static void gfs2_discard(struct gfs2_sbd *sdp, struct buffer_head *bh)
  584. {
  585. struct gfs2_bufdata *bd;
  586. lock_buffer(bh);
  587. gfs2_log_lock(sdp);
  588. clear_buffer_dirty(bh);
  589. bd = bh->b_private;
  590. if (bd) {
  591. if (!list_empty(&bd->bd_le.le_list) && !buffer_pinned(bh))
  592. list_del_init(&bd->bd_le.le_list);
  593. else
  594. gfs2_remove_from_journal(bh, current->journal_info, 0);
  595. }
  596. bh->b_bdev = NULL;
  597. clear_buffer_mapped(bh);
  598. clear_buffer_req(bh);
  599. clear_buffer_new(bh);
  600. gfs2_log_unlock(sdp);
  601. unlock_buffer(bh);
  602. }
  603. static void gfs2_invalidatepage(struct page *page, unsigned long offset)
  604. {
  605. struct gfs2_sbd *sdp = GFS2_SB(page->mapping->host);
  606. struct buffer_head *bh, *head;
  607. unsigned long pos = 0;
  608. BUG_ON(!PageLocked(page));
  609. if (offset == 0)
  610. ClearPageChecked(page);
  611. if (!page_has_buffers(page))
  612. goto out;
  613. bh = head = page_buffers(page);
  614. do {
  615. if (offset <= pos)
  616. gfs2_discard(sdp, bh);
  617. pos += bh->b_size;
  618. bh = bh->b_this_page;
  619. } while (bh != head);
  620. out:
  621. if (offset == 0)
  622. try_to_release_page(page, 0);
  623. }
  624. /**
  625. * gfs2_ok_for_dio - check that dio is valid on this file
  626. * @ip: The inode
  627. * @rw: READ or WRITE
  628. * @offset: The offset at which we are reading or writing
  629. *
  630. * Returns: 0 (to ignore the i/o request and thus fall back to buffered i/o)
  631. * 1 (to accept the i/o request)
  632. */
  633. static int gfs2_ok_for_dio(struct gfs2_inode *ip, int rw, loff_t offset)
  634. {
  635. /*
  636. * Should we return an error here? I can't see that O_DIRECT for
  637. * a stuffed file makes any sense. For now we'll silently fall
  638. * back to buffered I/O
  639. */
  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. static const struct address_space_operations gfs2_writeback_aops = {
  737. .writepage = gfs2_writepage,
  738. .writepages = gfs2_writeback_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. .bmap = gfs2_bmap,
  745. .invalidatepage = gfs2_invalidatepage,
  746. .releasepage = gfs2_releasepage,
  747. .direct_IO = gfs2_direct_IO,
  748. };
  749. static const struct address_space_operations gfs2_ordered_aops = {
  750. .writepage = gfs2_writepage,
  751. .readpage = gfs2_readpage,
  752. .readpages = gfs2_readpages,
  753. .sync_page = block_sync_page,
  754. .write_begin = gfs2_write_begin,
  755. .write_end = gfs2_write_end,
  756. .set_page_dirty = gfs2_set_page_dirty,
  757. .bmap = gfs2_bmap,
  758. .invalidatepage = gfs2_invalidatepage,
  759. .releasepage = gfs2_releasepage,
  760. .direct_IO = gfs2_direct_IO,
  761. };
  762. static const struct address_space_operations gfs2_jdata_aops = {
  763. .writepage = gfs2_writepage,
  764. .readpage = gfs2_readpage,
  765. .readpages = gfs2_readpages,
  766. .sync_page = block_sync_page,
  767. .write_begin = gfs2_write_begin,
  768. .write_end = gfs2_write_end,
  769. .set_page_dirty = gfs2_set_page_dirty,
  770. .bmap = gfs2_bmap,
  771. .invalidatepage = gfs2_invalidatepage,
  772. .releasepage = gfs2_releasepage,
  773. };
  774. void gfs2_set_aops(struct inode *inode)
  775. {
  776. struct gfs2_inode *ip = GFS2_I(inode);
  777. if (gfs2_is_writeback(ip))
  778. inode->i_mapping->a_ops = &gfs2_writeback_aops;
  779. else if (gfs2_is_ordered(ip))
  780. inode->i_mapping->a_ops = &gfs2_ordered_aops;
  781. else if (gfs2_is_jdata(ip))
  782. inode->i_mapping->a_ops = &gfs2_jdata_aops;
  783. else
  784. BUG();
  785. }