ops_address.c 13 KB

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