ops_address.c 15 KB

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