ops_address.c 13 KB

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