ops_address.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2005 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/mpage.h>
  16. #include <linux/fs.h>
  17. #include <linux/gfs2_ondisk.h>
  18. #include <asm/semaphore.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 "page.h"
  29. #include "quota.h"
  30. #include "trans.h"
  31. #include "rgrp.h"
  32. #include "ops_file.h"
  33. #include "util.h"
  34. /**
  35. * gfs2_get_block - Fills in a buffer head with details about a block
  36. * @inode: The inode
  37. * @lblock: The block number to look up
  38. * @bh_result: The buffer head to return the result in
  39. * @create: Non-zero if we may add block to the file
  40. *
  41. * Returns: errno
  42. */
  43. int gfs2_get_block(struct inode *inode, sector_t lblock,
  44. struct buffer_head *bh_result, int create)
  45. {
  46. struct gfs2_inode *ip = inode->u.generic_ip;
  47. int new = create;
  48. uint64_t dblock;
  49. int error;
  50. error = gfs2_block_map(ip, lblock, &new, &dblock, NULL);
  51. if (error)
  52. return error;
  53. if (!dblock)
  54. return 0;
  55. map_bh(bh_result, inode->i_sb, dblock);
  56. if (new)
  57. set_buffer_new(bh_result);
  58. return 0;
  59. }
  60. /**
  61. * get_block_noalloc - Fills in a buffer head with details about a block
  62. * @inode: The inode
  63. * @lblock: The block number to look up
  64. * @bh_result: The buffer head to return the result in
  65. * @create: Non-zero if we may add block to the file
  66. *
  67. * Returns: errno
  68. */
  69. static int get_block_noalloc(struct inode *inode, sector_t lblock,
  70. struct buffer_head *bh_result, int create)
  71. {
  72. struct gfs2_inode *ip = inode->u.generic_ip;
  73. int new = 0;
  74. uint64_t dblock;
  75. int error;
  76. error = gfs2_block_map(ip, lblock, &new, &dblock, NULL);
  77. if (error)
  78. return error;
  79. if (dblock)
  80. map_bh(bh_result, inode->i_sb, dblock);
  81. else if (gfs2_assert_withdraw(ip->i_sbd, !create))
  82. error = -EIO;
  83. return error;
  84. }
  85. static int get_blocks(struct inode *inode, sector_t lblock,
  86. unsigned long max_blocks, struct buffer_head *bh_result,
  87. int create)
  88. {
  89. struct gfs2_inode *ip = inode->u.generic_ip;
  90. int new = create;
  91. uint64_t dblock;
  92. uint32_t extlen;
  93. int error;
  94. error = gfs2_block_map(ip, lblock, &new, &dblock, &extlen);
  95. if (error)
  96. return error;
  97. if (!dblock)
  98. return 0;
  99. map_bh(bh_result, inode->i_sb, dblock);
  100. if (new)
  101. set_buffer_new(bh_result);
  102. if (extlen > max_blocks)
  103. extlen = max_blocks;
  104. bh_result->b_size = extlen << inode->i_blkbits;
  105. return 0;
  106. }
  107. static int get_blocks_noalloc(struct inode *inode, sector_t lblock,
  108. unsigned long max_blocks,
  109. struct buffer_head *bh_result, int create)
  110. {
  111. struct gfs2_inode *ip = inode->u.generic_ip;
  112. int new = 0;
  113. uint64_t dblock;
  114. uint32_t extlen;
  115. int error;
  116. error = gfs2_block_map(ip, lblock, &new, &dblock, &extlen);
  117. if (error)
  118. return error;
  119. if (dblock) {
  120. map_bh(bh_result, inode->i_sb, dblock);
  121. if (extlen > max_blocks)
  122. extlen = max_blocks;
  123. bh_result->b_size = extlen << inode->i_blkbits;
  124. } else if (gfs2_assert_withdraw(ip->i_sbd, !create))
  125. error = -EIO;
  126. return error;
  127. }
  128. /**
  129. * gfs2_writepage - Write complete page
  130. * @page: Page to write
  131. *
  132. * Returns: errno
  133. *
  134. * Some of this is copied from block_write_full_page() although we still
  135. * call it to do most of the work.
  136. */
  137. static int gfs2_writepage(struct page *page, struct writeback_control *wbc)
  138. {
  139. struct inode *inode = page->mapping->host;
  140. struct gfs2_inode *ip = page->mapping->host->u.generic_ip;
  141. struct gfs2_sbd *sdp = ip->i_sbd;
  142. loff_t i_size = i_size_read(inode);
  143. pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
  144. unsigned offset;
  145. int error;
  146. int done_trans = 0;
  147. if (gfs2_assert_withdraw(sdp, gfs2_glock_is_held_excl(ip->i_gl))) {
  148. unlock_page(page);
  149. return -EIO;
  150. }
  151. if (current->journal_info)
  152. goto out_ignore;
  153. /* Is the page fully outside i_size? (truncate in progress) */
  154. offset = i_size & (PAGE_CACHE_SIZE-1);
  155. if (page->index >= end_index+1 || !offset) {
  156. page->mapping->a_ops->invalidatepage(page, 0);
  157. unlock_page(page);
  158. return 0; /* don't care */
  159. }
  160. if (sdp->sd_args.ar_data == GFS2_DATA_ORDERED || gfs2_is_jdata(ip)) {
  161. error = gfs2_trans_begin(sdp, RES_DINODE + 1, 0);
  162. if (error)
  163. goto out_ignore;
  164. gfs2_page_add_databufs(ip, page, 0, sdp->sd_vfs->s_blocksize-1);
  165. done_trans = 1;
  166. }
  167. error = block_write_full_page(page, get_block_noalloc, wbc);
  168. if (done_trans)
  169. gfs2_trans_end(sdp);
  170. gfs2_meta_cache_flush(ip);
  171. return error;
  172. out_ignore:
  173. redirty_page_for_writepage(wbc, page);
  174. unlock_page(page);
  175. return 0;
  176. }
  177. /**
  178. * stuffed_readpage - Fill in a Linux page with stuffed file data
  179. * @ip: the inode
  180. * @page: the page
  181. *
  182. * Returns: errno
  183. */
  184. static int stuffed_readpage(struct gfs2_inode *ip, struct page *page)
  185. {
  186. struct buffer_head *dibh;
  187. void *kaddr;
  188. int error;
  189. error = gfs2_meta_inode_buffer(ip, &dibh);
  190. if (error)
  191. return error;
  192. kaddr = kmap_atomic(page, KM_USER0);
  193. memcpy((char *)kaddr,
  194. dibh->b_data + sizeof(struct gfs2_dinode),
  195. ip->i_di.di_size);
  196. memset((char *)kaddr + ip->i_di.di_size,
  197. 0,
  198. PAGE_CACHE_SIZE - ip->i_di.di_size);
  199. kunmap_atomic(page, KM_USER0);
  200. brelse(dibh);
  201. SetPageUptodate(page);
  202. return 0;
  203. }
  204. static int zero_readpage(struct page *page)
  205. {
  206. void *kaddr;
  207. kaddr = kmap_atomic(page, KM_USER0);
  208. memset(kaddr, 0, PAGE_CACHE_SIZE);
  209. kunmap_atomic(page, KM_USER0);
  210. SetPageUptodate(page);
  211. unlock_page(page);
  212. return 0;
  213. }
  214. /**
  215. * gfs2_readpage - readpage with locking
  216. * @file: The file to read a page for. N.B. This may be NULL if we are
  217. * reading an internal file.
  218. * @page: The page to read
  219. *
  220. * Returns: errno
  221. */
  222. static int gfs2_readpage(struct file *file, struct page *page)
  223. {
  224. struct gfs2_inode *ip = page->mapping->host->u.generic_ip;
  225. struct gfs2_sbd *sdp = ip->i_sbd;
  226. struct gfs2_holder gh;
  227. int error;
  228. if (file != &gfs2_internal_file_sentinal) {
  229. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &gh);
  230. error = gfs2_glock_nq_m_atime(1, &gh);
  231. if (error)
  232. goto out_unlock;
  233. }
  234. if (gfs2_is_stuffed(ip)) {
  235. if (!page->index) {
  236. error = stuffed_readpage(ip, page);
  237. unlock_page(page);
  238. } else
  239. error = zero_readpage(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. goto out;
  253. }
  254. /**
  255. * gfs2_prepare_write - Prepare to write a page to a file
  256. * @file: The file to write to
  257. * @page: The page which is to be prepared for writing
  258. * @from: From (byte range within page)
  259. * @to: To (byte range within page)
  260. *
  261. * Returns: errno
  262. */
  263. static int gfs2_prepare_write(struct file *file, struct page *page,
  264. unsigned from, unsigned to)
  265. {
  266. struct gfs2_inode *ip = page->mapping->host->u.generic_ip;
  267. struct gfs2_sbd *sdp = ip->i_sbd;
  268. unsigned int data_blocks, ind_blocks, rblocks;
  269. int alloc_required;
  270. int error = 0;
  271. loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + from;
  272. loff_t end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
  273. struct gfs2_alloc *al;
  274. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, GL_ATIME, &ip->i_gh);
  275. error = gfs2_glock_nq_m_atime(1, &ip->i_gh);
  276. if (error)
  277. goto out_uninit;
  278. gfs2_write_calc_reserv(ip, to - from, &data_blocks, &ind_blocks);
  279. error = gfs2_write_alloc_required(ip, pos, from - to, &alloc_required);
  280. if (error)
  281. goto out_unlock;
  282. if (alloc_required) {
  283. al = gfs2_alloc_get(ip);
  284. error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
  285. if (error)
  286. goto out_alloc_put;
  287. error = gfs2_quota_check(ip, ip->i_di.di_uid, ip->i_di.di_gid);
  288. if (error)
  289. goto out_qunlock;
  290. al->al_requested = data_blocks + ind_blocks;
  291. error = gfs2_inplace_reserve(ip);
  292. if (error)
  293. goto out_qunlock;
  294. }
  295. rblocks = RES_DINODE + ind_blocks;
  296. if (gfs2_is_jdata(ip))
  297. rblocks += data_blocks ? data_blocks : 1;
  298. if (ind_blocks || data_blocks)
  299. rblocks += RES_STATFS + RES_QUOTA;
  300. error = gfs2_trans_begin(sdp, rblocks, 0);
  301. if (error)
  302. goto out;
  303. if (gfs2_is_stuffed(ip)) {
  304. if (end > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) {
  305. error = gfs2_unstuff_dinode(ip, gfs2_unstuffer_page,
  306. page);
  307. if (error == 0)
  308. goto prepare_write;
  309. } else if (!PageUptodate(page))
  310. error = stuffed_readpage(ip, page);
  311. goto out;
  312. }
  313. prepare_write:
  314. error = block_prepare_write(page, from, to, gfs2_get_block);
  315. out:
  316. if (error) {
  317. gfs2_trans_end(sdp);
  318. if (alloc_required) {
  319. gfs2_inplace_release(ip);
  320. out_qunlock:
  321. gfs2_quota_unlock(ip);
  322. out_alloc_put:
  323. gfs2_alloc_put(ip);
  324. }
  325. out_unlock:
  326. gfs2_glock_dq_m(1, &ip->i_gh);
  327. out_uninit:
  328. gfs2_holder_uninit(&ip->i_gh);
  329. }
  330. return error;
  331. }
  332. /**
  333. * gfs2_commit_write - Commit write to a file
  334. * @file: The file to write to
  335. * @page: The page containing the data
  336. * @from: From (byte range within page)
  337. * @to: To (byte range within page)
  338. *
  339. * Returns: errno
  340. */
  341. static int gfs2_commit_write(struct file *file, struct page *page,
  342. unsigned from, unsigned to)
  343. {
  344. struct inode *inode = page->mapping->host;
  345. struct gfs2_inode *ip = inode->u.generic_ip;
  346. struct gfs2_sbd *sdp = ip->i_sbd;
  347. int error = -EOPNOTSUPP;
  348. struct buffer_head *dibh;
  349. struct gfs2_alloc *al = &ip->i_alloc;;
  350. if (gfs2_assert_withdraw(sdp, gfs2_glock_is_locked_by_me(ip->i_gl)))
  351. goto fail_nounlock;
  352. error = gfs2_meta_inode_buffer(ip, &dibh);
  353. if (error)
  354. goto fail_endtrans;
  355. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  356. if (gfs2_is_stuffed(ip)) {
  357. uint64_t file_size;
  358. void *kaddr;
  359. file_size = ((uint64_t)page->index << PAGE_CACHE_SHIFT) + to;
  360. kaddr = kmap_atomic(page, KM_USER0);
  361. memcpy(dibh->b_data + sizeof(struct gfs2_dinode) + from,
  362. (char *)kaddr + from, to - from);
  363. kunmap_atomic(page, KM_USER0);
  364. SetPageUptodate(page);
  365. if (inode->i_size < file_size)
  366. i_size_write(inode, file_size);
  367. } else {
  368. if (sdp->sd_args.ar_data == GFS2_DATA_ORDERED ||
  369. gfs2_is_jdata(ip))
  370. gfs2_page_add_databufs(ip, page, from, to);
  371. error = generic_commit_write(file, page, from, to);
  372. if (error)
  373. goto fail;
  374. }
  375. if (ip->i_di.di_size < inode->i_size)
  376. ip->i_di.di_size = inode->i_size;
  377. gfs2_dinode_out(&ip->i_di, dibh->b_data);
  378. brelse(dibh);
  379. gfs2_trans_end(sdp);
  380. if (al->al_requested) {
  381. gfs2_inplace_release(ip);
  382. gfs2_quota_unlock(ip);
  383. gfs2_alloc_put(ip);
  384. }
  385. gfs2_glock_dq_m(1, &ip->i_gh);
  386. gfs2_holder_uninit(&ip->i_gh);
  387. return 0;
  388. fail:
  389. brelse(dibh);
  390. fail_endtrans:
  391. gfs2_trans_end(sdp);
  392. if (al->al_requested) {
  393. gfs2_inplace_release(ip);
  394. gfs2_quota_unlock(ip);
  395. gfs2_alloc_put(ip);
  396. }
  397. gfs2_glock_dq_m(1, &ip->i_gh);
  398. gfs2_holder_uninit(&ip->i_gh);
  399. fail_nounlock:
  400. ClearPageUptodate(page);
  401. return error;
  402. }
  403. /**
  404. * gfs2_bmap - Block map function
  405. * @mapping: Address space info
  406. * @lblock: The block to map
  407. *
  408. * Returns: The disk address for the block or 0 on hole or error
  409. */
  410. static sector_t gfs2_bmap(struct address_space *mapping, sector_t lblock)
  411. {
  412. struct gfs2_inode *ip = mapping->host->u.generic_ip;
  413. struct gfs2_holder i_gh;
  414. sector_t dblock = 0;
  415. int error;
  416. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
  417. if (error)
  418. return 0;
  419. if (!gfs2_is_stuffed(ip))
  420. dblock = generic_block_bmap(mapping, lblock, gfs2_get_block);
  421. gfs2_glock_dq_uninit(&i_gh);
  422. return dblock;
  423. }
  424. static void discard_buffer(struct gfs2_sbd *sdp, struct buffer_head *bh)
  425. {
  426. struct gfs2_bufdata *bd;
  427. gfs2_log_lock(sdp);
  428. bd = bh->b_private;
  429. if (bd) {
  430. bd->bd_bh = NULL;
  431. bh->b_private = NULL;
  432. gfs2_log_unlock(sdp);
  433. brelse(bh);
  434. } else
  435. gfs2_log_unlock(sdp);
  436. lock_buffer(bh);
  437. clear_buffer_dirty(bh);
  438. bh->b_bdev = NULL;
  439. clear_buffer_mapped(bh);
  440. clear_buffer_req(bh);
  441. clear_buffer_new(bh);
  442. clear_buffer_delay(bh);
  443. unlock_buffer(bh);
  444. }
  445. static int gfs2_invalidatepage(struct page *page, unsigned long offset)
  446. {
  447. struct gfs2_sbd *sdp = page->mapping->host->i_sb->s_fs_info;
  448. struct buffer_head *head, *bh, *next;
  449. unsigned int curr_off = 0;
  450. int ret = 1;
  451. BUG_ON(!PageLocked(page));
  452. if (!page_has_buffers(page))
  453. return 1;
  454. bh = head = page_buffers(page);
  455. do {
  456. unsigned int next_off = curr_off + bh->b_size;
  457. next = bh->b_this_page;
  458. if (offset <= curr_off)
  459. discard_buffer(sdp, bh);
  460. curr_off = next_off;
  461. bh = next;
  462. } while (bh != head);
  463. if (!offset)
  464. ret = try_to_release_page(page, 0);
  465. return ret;
  466. }
  467. static ssize_t gfs2_direct_IO_write(struct kiocb *iocb, const struct iovec *iov,
  468. loff_t offset, unsigned long nr_segs)
  469. {
  470. struct file *file = iocb->ki_filp;
  471. struct inode *inode = file->f_mapping->host;
  472. struct gfs2_inode *ip = inode->u.generic_ip;
  473. struct gfs2_holder gh;
  474. int rv;
  475. /*
  476. * Shared lock, even though its write, since we do no allocation
  477. * on this path. All we need change is atime.
  478. */
  479. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &gh);
  480. rv = gfs2_glock_nq_m_atime(1, &gh);
  481. if (rv)
  482. goto out;
  483. /*
  484. * Should we return an error here? I can't see that O_DIRECT for
  485. * a journaled file makes any sense. For now we'll silently fall
  486. * back to buffered I/O, likewise we do the same for stuffed
  487. * files since they are (a) small and (b) unaligned.
  488. */
  489. if (gfs2_is_jdata(ip))
  490. goto out;
  491. if (gfs2_is_stuffed(ip))
  492. goto out;
  493. rv = __blockdev_direct_IO(WRITE, iocb, inode, inode->i_sb->s_bdev,
  494. iov, offset, nr_segs, get_blocks_noalloc,
  495. NULL, DIO_OWN_LOCKING);
  496. out:
  497. gfs2_glock_dq_m(1, &gh);
  498. gfs2_holder_uninit(&gh);
  499. return rv;
  500. }
  501. /**
  502. * gfs2_direct_IO
  503. *
  504. * This is called with a shared lock already held for the read path.
  505. * Currently, no locks are held when the write path is called.
  506. */
  507. static ssize_t gfs2_direct_IO(int rw, struct kiocb *iocb,
  508. const struct iovec *iov, loff_t offset,
  509. unsigned long nr_segs)
  510. {
  511. struct file *file = iocb->ki_filp;
  512. struct inode *inode = file->f_mapping->host;
  513. struct gfs2_inode *ip = inode->u.generic_ip;
  514. struct gfs2_sbd *sdp = ip->i_sbd;
  515. if (rw == WRITE)
  516. return gfs2_direct_IO_write(iocb, iov, offset, nr_segs);
  517. if (gfs2_assert_warn(sdp, gfs2_glock_is_locked_by_me(ip->i_gl)) ||
  518. gfs2_assert_warn(sdp, !gfs2_is_stuffed(ip)))
  519. return -EINVAL;
  520. return __blockdev_direct_IO(READ, iocb, inode, inode->i_sb->s_bdev, iov,
  521. offset, nr_segs, get_blocks, NULL,
  522. DIO_OWN_LOCKING);
  523. }
  524. struct address_space_operations gfs2_file_aops = {
  525. .writepage = gfs2_writepage,
  526. .readpage = gfs2_readpage,
  527. .sync_page = block_sync_page,
  528. .prepare_write = gfs2_prepare_write,
  529. .commit_write = gfs2_commit_write,
  530. .bmap = gfs2_bmap,
  531. .invalidatepage = gfs2_invalidatepage,
  532. .direct_IO = gfs2_direct_IO,
  533. };