aops.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320
  1. /* -*- mode: c; c-basic-offset: 8; -*-
  2. * vim: noexpandtab sw=8 ts=8 sts=0:
  3. *
  4. * Copyright (C) 2002, 2004 Oracle. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public
  17. * License along with this program; if not, write to the
  18. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  19. * Boston, MA 021110-1307, USA.
  20. */
  21. #include <linux/fs.h>
  22. #include <linux/slab.h>
  23. #include <linux/highmem.h>
  24. #include <linux/pagemap.h>
  25. #include <asm/byteorder.h>
  26. #include <linux/swap.h>
  27. #include <linux/pipe_fs_i.h>
  28. #define MLOG_MASK_PREFIX ML_FILE_IO
  29. #include <cluster/masklog.h>
  30. #include "ocfs2.h"
  31. #include "alloc.h"
  32. #include "aops.h"
  33. #include "dlmglue.h"
  34. #include "extent_map.h"
  35. #include "file.h"
  36. #include "inode.h"
  37. #include "journal.h"
  38. #include "suballoc.h"
  39. #include "super.h"
  40. #include "symlink.h"
  41. #include "buffer_head_io.h"
  42. static int ocfs2_symlink_get_block(struct inode *inode, sector_t iblock,
  43. struct buffer_head *bh_result, int create)
  44. {
  45. int err = -EIO;
  46. int status;
  47. struct ocfs2_dinode *fe = NULL;
  48. struct buffer_head *bh = NULL;
  49. struct buffer_head *buffer_cache_bh = NULL;
  50. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  51. void *kaddr;
  52. mlog_entry("(0x%p, %llu, 0x%p, %d)\n", inode,
  53. (unsigned long long)iblock, bh_result, create);
  54. BUG_ON(ocfs2_inode_is_fast_symlink(inode));
  55. if ((iblock << inode->i_sb->s_blocksize_bits) > PATH_MAX + 1) {
  56. mlog(ML_ERROR, "block offset > PATH_MAX: %llu",
  57. (unsigned long long)iblock);
  58. goto bail;
  59. }
  60. status = ocfs2_read_block(OCFS2_SB(inode->i_sb),
  61. OCFS2_I(inode)->ip_blkno,
  62. &bh, OCFS2_BH_CACHED, inode);
  63. if (status < 0) {
  64. mlog_errno(status);
  65. goto bail;
  66. }
  67. fe = (struct ocfs2_dinode *) bh->b_data;
  68. if (!OCFS2_IS_VALID_DINODE(fe)) {
  69. mlog(ML_ERROR, "Invalid dinode #%llu: signature = %.*s\n",
  70. (unsigned long long)le64_to_cpu(fe->i_blkno), 7,
  71. fe->i_signature);
  72. goto bail;
  73. }
  74. if ((u64)iblock >= ocfs2_clusters_to_blocks(inode->i_sb,
  75. le32_to_cpu(fe->i_clusters))) {
  76. mlog(ML_ERROR, "block offset is outside the allocated size: "
  77. "%llu\n", (unsigned long long)iblock);
  78. goto bail;
  79. }
  80. /* We don't use the page cache to create symlink data, so if
  81. * need be, copy it over from the buffer cache. */
  82. if (!buffer_uptodate(bh_result) && ocfs2_inode_is_new(inode)) {
  83. u64 blkno = le64_to_cpu(fe->id2.i_list.l_recs[0].e_blkno) +
  84. iblock;
  85. buffer_cache_bh = sb_getblk(osb->sb, blkno);
  86. if (!buffer_cache_bh) {
  87. mlog(ML_ERROR, "couldn't getblock for symlink!\n");
  88. goto bail;
  89. }
  90. /* we haven't locked out transactions, so a commit
  91. * could've happened. Since we've got a reference on
  92. * the bh, even if it commits while we're doing the
  93. * copy, the data is still good. */
  94. if (buffer_jbd(buffer_cache_bh)
  95. && ocfs2_inode_is_new(inode)) {
  96. kaddr = kmap_atomic(bh_result->b_page, KM_USER0);
  97. if (!kaddr) {
  98. mlog(ML_ERROR, "couldn't kmap!\n");
  99. goto bail;
  100. }
  101. memcpy(kaddr + (bh_result->b_size * iblock),
  102. buffer_cache_bh->b_data,
  103. bh_result->b_size);
  104. kunmap_atomic(kaddr, KM_USER0);
  105. set_buffer_uptodate(bh_result);
  106. }
  107. brelse(buffer_cache_bh);
  108. }
  109. map_bh(bh_result, inode->i_sb,
  110. le64_to_cpu(fe->id2.i_list.l_recs[0].e_blkno) + iblock);
  111. err = 0;
  112. bail:
  113. if (bh)
  114. brelse(bh);
  115. mlog_exit(err);
  116. return err;
  117. }
  118. static int ocfs2_get_block(struct inode *inode, sector_t iblock,
  119. struct buffer_head *bh_result, int create)
  120. {
  121. int err = 0;
  122. unsigned int ext_flags;
  123. u64 p_blkno, past_eof;
  124. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  125. mlog_entry("(0x%p, %llu, 0x%p, %d)\n", inode,
  126. (unsigned long long)iblock, bh_result, create);
  127. if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_SYSTEM_FILE)
  128. mlog(ML_NOTICE, "get_block on system inode 0x%p (%lu)\n",
  129. inode, inode->i_ino);
  130. if (S_ISLNK(inode->i_mode)) {
  131. /* this always does I/O for some reason. */
  132. err = ocfs2_symlink_get_block(inode, iblock, bh_result, create);
  133. goto bail;
  134. }
  135. err = ocfs2_extent_map_get_blocks(inode, iblock, &p_blkno, NULL,
  136. &ext_flags);
  137. if (err) {
  138. mlog(ML_ERROR, "Error %d from get_blocks(0x%p, %llu, 1, "
  139. "%llu, NULL)\n", err, inode, (unsigned long long)iblock,
  140. (unsigned long long)p_blkno);
  141. goto bail;
  142. }
  143. /*
  144. * ocfs2 never allocates in this function - the only time we
  145. * need to use BH_New is when we're extending i_size on a file
  146. * system which doesn't support holes, in which case BH_New
  147. * allows block_prepare_write() to zero.
  148. */
  149. mlog_bug_on_msg(create && p_blkno == 0 && ocfs2_sparse_alloc(osb),
  150. "ino %lu, iblock %llu\n", inode->i_ino,
  151. (unsigned long long)iblock);
  152. /* Treat the unwritten extent as a hole for zeroing purposes. */
  153. if (p_blkno && !(ext_flags & OCFS2_EXT_UNWRITTEN))
  154. map_bh(bh_result, inode->i_sb, p_blkno);
  155. if (!ocfs2_sparse_alloc(osb)) {
  156. if (p_blkno == 0) {
  157. err = -EIO;
  158. mlog(ML_ERROR,
  159. "iblock = %llu p_blkno = %llu blkno=(%llu)\n",
  160. (unsigned long long)iblock,
  161. (unsigned long long)p_blkno,
  162. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  163. mlog(ML_ERROR, "Size %llu, clusters %u\n", (unsigned long long)i_size_read(inode), OCFS2_I(inode)->ip_clusters);
  164. dump_stack();
  165. }
  166. past_eof = ocfs2_blocks_for_bytes(inode->i_sb, i_size_read(inode));
  167. mlog(0, "Inode %lu, past_eof = %llu\n", inode->i_ino,
  168. (unsigned long long)past_eof);
  169. if (create && (iblock >= past_eof))
  170. set_buffer_new(bh_result);
  171. }
  172. bail:
  173. if (err < 0)
  174. err = -EIO;
  175. mlog_exit(err);
  176. return err;
  177. }
  178. static int ocfs2_readpage(struct file *file, struct page *page)
  179. {
  180. struct inode *inode = page->mapping->host;
  181. loff_t start = (loff_t)page->index << PAGE_CACHE_SHIFT;
  182. int ret, unlock = 1;
  183. mlog_entry("(0x%p, %lu)\n", file, (page ? page->index : 0));
  184. ret = ocfs2_meta_lock_with_page(inode, NULL, 0, page);
  185. if (ret != 0) {
  186. if (ret == AOP_TRUNCATED_PAGE)
  187. unlock = 0;
  188. mlog_errno(ret);
  189. goto out;
  190. }
  191. if (down_read_trylock(&OCFS2_I(inode)->ip_alloc_sem) == 0) {
  192. ret = AOP_TRUNCATED_PAGE;
  193. goto out_meta_unlock;
  194. }
  195. /*
  196. * i_size might have just been updated as we grabed the meta lock. We
  197. * might now be discovering a truncate that hit on another node.
  198. * block_read_full_page->get_block freaks out if it is asked to read
  199. * beyond the end of a file, so we check here. Callers
  200. * (generic_file_read, fault->nopage) are clever enough to check i_size
  201. * and notice that the page they just read isn't needed.
  202. *
  203. * XXX sys_readahead() seems to get that wrong?
  204. */
  205. if (start >= i_size_read(inode)) {
  206. zero_user_page(page, 0, PAGE_SIZE, KM_USER0);
  207. SetPageUptodate(page);
  208. ret = 0;
  209. goto out_alloc;
  210. }
  211. ret = ocfs2_data_lock_with_page(inode, 0, page);
  212. if (ret != 0) {
  213. if (ret == AOP_TRUNCATED_PAGE)
  214. unlock = 0;
  215. mlog_errno(ret);
  216. goto out_alloc;
  217. }
  218. ret = block_read_full_page(page, ocfs2_get_block);
  219. unlock = 0;
  220. ocfs2_data_unlock(inode, 0);
  221. out_alloc:
  222. up_read(&OCFS2_I(inode)->ip_alloc_sem);
  223. out_meta_unlock:
  224. ocfs2_meta_unlock(inode, 0);
  225. out:
  226. if (unlock)
  227. unlock_page(page);
  228. mlog_exit(ret);
  229. return ret;
  230. }
  231. /* Note: Because we don't support holes, our allocation has
  232. * already happened (allocation writes zeros to the file data)
  233. * so we don't have to worry about ordered writes in
  234. * ocfs2_writepage.
  235. *
  236. * ->writepage is called during the process of invalidating the page cache
  237. * during blocked lock processing. It can't block on any cluster locks
  238. * to during block mapping. It's relying on the fact that the block
  239. * mapping can't have disappeared under the dirty pages that it is
  240. * being asked to write back.
  241. */
  242. static int ocfs2_writepage(struct page *page, struct writeback_control *wbc)
  243. {
  244. int ret;
  245. mlog_entry("(0x%p)\n", page);
  246. ret = block_write_full_page(page, ocfs2_get_block, wbc);
  247. mlog_exit(ret);
  248. return ret;
  249. }
  250. /*
  251. * This is called from ocfs2_write_zero_page() which has handled it's
  252. * own cluster locking and has ensured allocation exists for those
  253. * blocks to be written.
  254. */
  255. int ocfs2_prepare_write_nolock(struct inode *inode, struct page *page,
  256. unsigned from, unsigned to)
  257. {
  258. int ret;
  259. down_read(&OCFS2_I(inode)->ip_alloc_sem);
  260. ret = block_prepare_write(page, from, to, ocfs2_get_block);
  261. up_read(&OCFS2_I(inode)->ip_alloc_sem);
  262. return ret;
  263. }
  264. /* Taken from ext3. We don't necessarily need the full blown
  265. * functionality yet, but IMHO it's better to cut and paste the whole
  266. * thing so we can avoid introducing our own bugs (and easily pick up
  267. * their fixes when they happen) --Mark */
  268. int walk_page_buffers( handle_t *handle,
  269. struct buffer_head *head,
  270. unsigned from,
  271. unsigned to,
  272. int *partial,
  273. int (*fn)( handle_t *handle,
  274. struct buffer_head *bh))
  275. {
  276. struct buffer_head *bh;
  277. unsigned block_start, block_end;
  278. unsigned blocksize = head->b_size;
  279. int err, ret = 0;
  280. struct buffer_head *next;
  281. for ( bh = head, block_start = 0;
  282. ret == 0 && (bh != head || !block_start);
  283. block_start = block_end, bh = next)
  284. {
  285. next = bh->b_this_page;
  286. block_end = block_start + blocksize;
  287. if (block_end <= from || block_start >= to) {
  288. if (partial && !buffer_uptodate(bh))
  289. *partial = 1;
  290. continue;
  291. }
  292. err = (*fn)(handle, bh);
  293. if (!ret)
  294. ret = err;
  295. }
  296. return ret;
  297. }
  298. handle_t *ocfs2_start_walk_page_trans(struct inode *inode,
  299. struct page *page,
  300. unsigned from,
  301. unsigned to)
  302. {
  303. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  304. handle_t *handle = NULL;
  305. int ret = 0;
  306. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  307. if (!handle) {
  308. ret = -ENOMEM;
  309. mlog_errno(ret);
  310. goto out;
  311. }
  312. if (ocfs2_should_order_data(inode)) {
  313. ret = walk_page_buffers(handle,
  314. page_buffers(page),
  315. from, to, NULL,
  316. ocfs2_journal_dirty_data);
  317. if (ret < 0)
  318. mlog_errno(ret);
  319. }
  320. out:
  321. if (ret) {
  322. if (handle)
  323. ocfs2_commit_trans(osb, handle);
  324. handle = ERR_PTR(ret);
  325. }
  326. return handle;
  327. }
  328. static sector_t ocfs2_bmap(struct address_space *mapping, sector_t block)
  329. {
  330. sector_t status;
  331. u64 p_blkno = 0;
  332. int err = 0;
  333. struct inode *inode = mapping->host;
  334. mlog_entry("(block = %llu)\n", (unsigned long long)block);
  335. /* We don't need to lock journal system files, since they aren't
  336. * accessed concurrently from multiple nodes.
  337. */
  338. if (!INODE_JOURNAL(inode)) {
  339. err = ocfs2_meta_lock(inode, NULL, 0);
  340. if (err) {
  341. if (err != -ENOENT)
  342. mlog_errno(err);
  343. goto bail;
  344. }
  345. down_read(&OCFS2_I(inode)->ip_alloc_sem);
  346. }
  347. err = ocfs2_extent_map_get_blocks(inode, block, &p_blkno, NULL, NULL);
  348. if (!INODE_JOURNAL(inode)) {
  349. up_read(&OCFS2_I(inode)->ip_alloc_sem);
  350. ocfs2_meta_unlock(inode, 0);
  351. }
  352. if (err) {
  353. mlog(ML_ERROR, "get_blocks() failed, block = %llu\n",
  354. (unsigned long long)block);
  355. mlog_errno(err);
  356. goto bail;
  357. }
  358. bail:
  359. status = err ? 0 : p_blkno;
  360. mlog_exit((int)status);
  361. return status;
  362. }
  363. /*
  364. * TODO: Make this into a generic get_blocks function.
  365. *
  366. * From do_direct_io in direct-io.c:
  367. * "So what we do is to permit the ->get_blocks function to populate
  368. * bh.b_size with the size of IO which is permitted at this offset and
  369. * this i_blkbits."
  370. *
  371. * This function is called directly from get_more_blocks in direct-io.c.
  372. *
  373. * called like this: dio->get_blocks(dio->inode, fs_startblk,
  374. * fs_count, map_bh, dio->rw == WRITE);
  375. */
  376. static int ocfs2_direct_IO_get_blocks(struct inode *inode, sector_t iblock,
  377. struct buffer_head *bh_result, int create)
  378. {
  379. int ret;
  380. u64 p_blkno, inode_blocks, contig_blocks;
  381. unsigned int ext_flags;
  382. unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
  383. unsigned long max_blocks = bh_result->b_size >> inode->i_blkbits;
  384. /* This function won't even be called if the request isn't all
  385. * nicely aligned and of the right size, so there's no need
  386. * for us to check any of that. */
  387. inode_blocks = ocfs2_blocks_for_bytes(inode->i_sb, i_size_read(inode));
  388. /*
  389. * Any write past EOF is not allowed because we'd be extending.
  390. */
  391. if (create && (iblock + max_blocks) > inode_blocks) {
  392. ret = -EIO;
  393. goto bail;
  394. }
  395. /* This figures out the size of the next contiguous block, and
  396. * our logical offset */
  397. ret = ocfs2_extent_map_get_blocks(inode, iblock, &p_blkno,
  398. &contig_blocks, &ext_flags);
  399. if (ret) {
  400. mlog(ML_ERROR, "get_blocks() failed iblock=%llu\n",
  401. (unsigned long long)iblock);
  402. ret = -EIO;
  403. goto bail;
  404. }
  405. if (!ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb)) && !p_blkno) {
  406. ocfs2_error(inode->i_sb,
  407. "Inode %llu has a hole at block %llu\n",
  408. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  409. (unsigned long long)iblock);
  410. ret = -EROFS;
  411. goto bail;
  412. }
  413. /*
  414. * get_more_blocks() expects us to describe a hole by clearing
  415. * the mapped bit on bh_result().
  416. *
  417. * Consider an unwritten extent as a hole.
  418. */
  419. if (p_blkno && !(ext_flags & OCFS2_EXT_UNWRITTEN))
  420. map_bh(bh_result, inode->i_sb, p_blkno);
  421. else {
  422. /*
  423. * ocfs2_prepare_inode_for_write() should have caught
  424. * the case where we'd be filling a hole and triggered
  425. * a buffered write instead.
  426. */
  427. if (create) {
  428. ret = -EIO;
  429. mlog_errno(ret);
  430. goto bail;
  431. }
  432. clear_buffer_mapped(bh_result);
  433. }
  434. /* make sure we don't map more than max_blocks blocks here as
  435. that's all the kernel will handle at this point. */
  436. if (max_blocks < contig_blocks)
  437. contig_blocks = max_blocks;
  438. bh_result->b_size = contig_blocks << blocksize_bits;
  439. bail:
  440. return ret;
  441. }
  442. /*
  443. * ocfs2_dio_end_io is called by the dio core when a dio is finished. We're
  444. * particularly interested in the aio/dio case. Like the core uses
  445. * i_alloc_sem, we use the rw_lock DLM lock to protect io on one node from
  446. * truncation on another.
  447. */
  448. static void ocfs2_dio_end_io(struct kiocb *iocb,
  449. loff_t offset,
  450. ssize_t bytes,
  451. void *private)
  452. {
  453. struct inode *inode = iocb->ki_filp->f_path.dentry->d_inode;
  454. int level;
  455. /* this io's submitter should not have unlocked this before we could */
  456. BUG_ON(!ocfs2_iocb_is_rw_locked(iocb));
  457. ocfs2_iocb_clear_rw_locked(iocb);
  458. level = ocfs2_iocb_rw_locked_level(iocb);
  459. if (!level)
  460. up_read(&inode->i_alloc_sem);
  461. ocfs2_rw_unlock(inode, level);
  462. }
  463. /*
  464. * ocfs2_invalidatepage() and ocfs2_releasepage() are shamelessly stolen
  465. * from ext3. PageChecked() bits have been removed as OCFS2 does not
  466. * do journalled data.
  467. */
  468. static void ocfs2_invalidatepage(struct page *page, unsigned long offset)
  469. {
  470. journal_t *journal = OCFS2_SB(page->mapping->host->i_sb)->journal->j_journal;
  471. journal_invalidatepage(journal, page, offset);
  472. }
  473. static int ocfs2_releasepage(struct page *page, gfp_t wait)
  474. {
  475. journal_t *journal = OCFS2_SB(page->mapping->host->i_sb)->journal->j_journal;
  476. if (!page_has_buffers(page))
  477. return 0;
  478. return journal_try_to_free_buffers(journal, page, wait);
  479. }
  480. static ssize_t ocfs2_direct_IO(int rw,
  481. struct kiocb *iocb,
  482. const struct iovec *iov,
  483. loff_t offset,
  484. unsigned long nr_segs)
  485. {
  486. struct file *file = iocb->ki_filp;
  487. struct inode *inode = file->f_path.dentry->d_inode->i_mapping->host;
  488. int ret;
  489. mlog_entry_void();
  490. if (!ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb))) {
  491. /*
  492. * We get PR data locks even for O_DIRECT. This
  493. * allows concurrent O_DIRECT I/O but doesn't let
  494. * O_DIRECT with extending and buffered zeroing writes
  495. * race. If they did race then the buffered zeroing
  496. * could be written back after the O_DIRECT I/O. It's
  497. * one thing to tell people not to mix buffered and
  498. * O_DIRECT writes, but expecting them to understand
  499. * that file extension is also an implicit buffered
  500. * write is too much. By getting the PR we force
  501. * writeback of the buffered zeroing before
  502. * proceeding.
  503. */
  504. ret = ocfs2_data_lock(inode, 0);
  505. if (ret < 0) {
  506. mlog_errno(ret);
  507. goto out;
  508. }
  509. ocfs2_data_unlock(inode, 0);
  510. }
  511. ret = blockdev_direct_IO_no_locking(rw, iocb, inode,
  512. inode->i_sb->s_bdev, iov, offset,
  513. nr_segs,
  514. ocfs2_direct_IO_get_blocks,
  515. ocfs2_dio_end_io);
  516. out:
  517. mlog_exit(ret);
  518. return ret;
  519. }
  520. static void ocfs2_figure_cluster_boundaries(struct ocfs2_super *osb,
  521. u32 cpos,
  522. unsigned int *start,
  523. unsigned int *end)
  524. {
  525. unsigned int cluster_start = 0, cluster_end = PAGE_CACHE_SIZE;
  526. if (unlikely(PAGE_CACHE_SHIFT > osb->s_clustersize_bits)) {
  527. unsigned int cpp;
  528. cpp = 1 << (PAGE_CACHE_SHIFT - osb->s_clustersize_bits);
  529. cluster_start = cpos % cpp;
  530. cluster_start = cluster_start << osb->s_clustersize_bits;
  531. cluster_end = cluster_start + osb->s_clustersize;
  532. }
  533. BUG_ON(cluster_start > PAGE_SIZE);
  534. BUG_ON(cluster_end > PAGE_SIZE);
  535. if (start)
  536. *start = cluster_start;
  537. if (end)
  538. *end = cluster_end;
  539. }
  540. /*
  541. * 'from' and 'to' are the region in the page to avoid zeroing.
  542. *
  543. * If pagesize > clustersize, this function will avoid zeroing outside
  544. * of the cluster boundary.
  545. *
  546. * from == to == 0 is code for "zero the entire cluster region"
  547. */
  548. static void ocfs2_clear_page_regions(struct page *page,
  549. struct ocfs2_super *osb, u32 cpos,
  550. unsigned from, unsigned to)
  551. {
  552. void *kaddr;
  553. unsigned int cluster_start, cluster_end;
  554. ocfs2_figure_cluster_boundaries(osb, cpos, &cluster_start, &cluster_end);
  555. kaddr = kmap_atomic(page, KM_USER0);
  556. if (from || to) {
  557. if (from > cluster_start)
  558. memset(kaddr + cluster_start, 0, from - cluster_start);
  559. if (to < cluster_end)
  560. memset(kaddr + to, 0, cluster_end - to);
  561. } else {
  562. memset(kaddr + cluster_start, 0, cluster_end - cluster_start);
  563. }
  564. kunmap_atomic(kaddr, KM_USER0);
  565. }
  566. /*
  567. * Some of this taken from block_prepare_write(). We already have our
  568. * mapping by now though, and the entire write will be allocating or
  569. * it won't, so not much need to use BH_New.
  570. *
  571. * This will also skip zeroing, which is handled externally.
  572. */
  573. int ocfs2_map_page_blocks(struct page *page, u64 *p_blkno,
  574. struct inode *inode, unsigned int from,
  575. unsigned int to, int new)
  576. {
  577. int ret = 0;
  578. struct buffer_head *head, *bh, *wait[2], **wait_bh = wait;
  579. unsigned int block_end, block_start;
  580. unsigned int bsize = 1 << inode->i_blkbits;
  581. if (!page_has_buffers(page))
  582. create_empty_buffers(page, bsize, 0);
  583. head = page_buffers(page);
  584. for (bh = head, block_start = 0; bh != head || !block_start;
  585. bh = bh->b_this_page, block_start += bsize) {
  586. block_end = block_start + bsize;
  587. /*
  588. * Ignore blocks outside of our i/o range -
  589. * they may belong to unallocated clusters.
  590. */
  591. if (block_start >= to || block_end <= from) {
  592. if (PageUptodate(page))
  593. set_buffer_uptodate(bh);
  594. continue;
  595. }
  596. /*
  597. * For an allocating write with cluster size >= page
  598. * size, we always write the entire page.
  599. */
  600. if (buffer_new(bh))
  601. clear_buffer_new(bh);
  602. if (!buffer_mapped(bh)) {
  603. map_bh(bh, inode->i_sb, *p_blkno);
  604. unmap_underlying_metadata(bh->b_bdev, bh->b_blocknr);
  605. }
  606. if (PageUptodate(page)) {
  607. if (!buffer_uptodate(bh))
  608. set_buffer_uptodate(bh);
  609. } else if (!buffer_uptodate(bh) && !buffer_delay(bh) &&
  610. (block_start < from || block_end > to)) {
  611. ll_rw_block(READ, 1, &bh);
  612. *wait_bh++=bh;
  613. }
  614. *p_blkno = *p_blkno + 1;
  615. }
  616. /*
  617. * If we issued read requests - let them complete.
  618. */
  619. while(wait_bh > wait) {
  620. wait_on_buffer(*--wait_bh);
  621. if (!buffer_uptodate(*wait_bh))
  622. ret = -EIO;
  623. }
  624. if (ret == 0 || !new)
  625. return ret;
  626. /*
  627. * If we get -EIO above, zero out any newly allocated blocks
  628. * to avoid exposing stale data.
  629. */
  630. bh = head;
  631. block_start = 0;
  632. do {
  633. void *kaddr;
  634. block_end = block_start + bsize;
  635. if (block_end <= from)
  636. goto next_bh;
  637. if (block_start >= to)
  638. break;
  639. kaddr = kmap_atomic(page, KM_USER0);
  640. memset(kaddr+block_start, 0, bh->b_size);
  641. flush_dcache_page(page);
  642. kunmap_atomic(kaddr, KM_USER0);
  643. set_buffer_uptodate(bh);
  644. mark_buffer_dirty(bh);
  645. next_bh:
  646. block_start = block_end;
  647. bh = bh->b_this_page;
  648. } while (bh != head);
  649. return ret;
  650. }
  651. /*
  652. * This will copy user data from the buffer page in the splice
  653. * context.
  654. *
  655. * For now, we ignore SPLICE_F_MOVE as that would require some extra
  656. * communication out all the way to ocfs2_write().
  657. */
  658. int ocfs2_map_and_write_splice_data(struct inode *inode,
  659. struct ocfs2_write_ctxt *wc, u64 *p_blkno,
  660. unsigned int *ret_from, unsigned int *ret_to)
  661. {
  662. int ret;
  663. unsigned int to, from, cluster_start, cluster_end;
  664. char *src, *dst;
  665. struct ocfs2_splice_write_priv *sp = wc->w_private;
  666. struct pipe_buffer *buf = sp->s_buf;
  667. unsigned long bytes, src_from;
  668. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  669. ocfs2_figure_cluster_boundaries(osb, wc->w_cpos, &cluster_start,
  670. &cluster_end);
  671. from = sp->s_offset;
  672. src_from = sp->s_buf_offset;
  673. bytes = wc->w_count;
  674. if (wc->w_large_pages) {
  675. /*
  676. * For cluster size < page size, we have to
  677. * calculate pos within the cluster and obey
  678. * the rightmost boundary.
  679. */
  680. bytes = min(bytes, (unsigned long)(osb->s_clustersize
  681. - (wc->w_pos & (osb->s_clustersize - 1))));
  682. }
  683. to = from + bytes;
  684. BUG_ON(from > PAGE_CACHE_SIZE);
  685. BUG_ON(to > PAGE_CACHE_SIZE);
  686. BUG_ON(from < cluster_start);
  687. BUG_ON(to > cluster_end);
  688. if (wc->w_this_page_new)
  689. ret = ocfs2_map_page_blocks(wc->w_this_page, p_blkno, inode,
  690. cluster_start, cluster_end, 1);
  691. else
  692. ret = ocfs2_map_page_blocks(wc->w_this_page, p_blkno, inode,
  693. from, to, 0);
  694. if (ret) {
  695. mlog_errno(ret);
  696. goto out;
  697. }
  698. src = buf->ops->map(sp->s_pipe, buf, 1);
  699. dst = kmap_atomic(wc->w_this_page, KM_USER1);
  700. memcpy(dst + from, src + src_from, bytes);
  701. kunmap_atomic(wc->w_this_page, KM_USER1);
  702. buf->ops->unmap(sp->s_pipe, buf, src);
  703. wc->w_finished_copy = 1;
  704. *ret_from = from;
  705. *ret_to = to;
  706. out:
  707. return bytes ? (unsigned int)bytes : ret;
  708. }
  709. /*
  710. * This will copy user data from the iovec in the buffered write
  711. * context.
  712. */
  713. int ocfs2_map_and_write_user_data(struct inode *inode,
  714. struct ocfs2_write_ctxt *wc, u64 *p_blkno,
  715. unsigned int *ret_from, unsigned int *ret_to)
  716. {
  717. int ret;
  718. unsigned int to, from, cluster_start, cluster_end;
  719. unsigned long bytes, src_from;
  720. char *dst;
  721. struct ocfs2_buffered_write_priv *bp = wc->w_private;
  722. const struct iovec *cur_iov = bp->b_cur_iov;
  723. char __user *buf;
  724. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  725. ocfs2_figure_cluster_boundaries(osb, wc->w_cpos, &cluster_start,
  726. &cluster_end);
  727. buf = cur_iov->iov_base + bp->b_cur_off;
  728. src_from = (unsigned long)buf & ~PAGE_CACHE_MASK;
  729. from = wc->w_pos & (PAGE_CACHE_SIZE - 1);
  730. /*
  731. * This is a lot of comparisons, but it reads quite
  732. * easily, which is important here.
  733. */
  734. /* Stay within the src page */
  735. bytes = PAGE_SIZE - src_from;
  736. /* Stay within the vector */
  737. bytes = min(bytes,
  738. (unsigned long)(cur_iov->iov_len - bp->b_cur_off));
  739. /* Stay within count */
  740. bytes = min(bytes, (unsigned long)wc->w_count);
  741. /*
  742. * For clustersize > page size, just stay within
  743. * target page, otherwise we have to calculate pos
  744. * within the cluster and obey the rightmost
  745. * boundary.
  746. */
  747. if (wc->w_large_pages) {
  748. /*
  749. * For cluster size < page size, we have to
  750. * calculate pos within the cluster and obey
  751. * the rightmost boundary.
  752. */
  753. bytes = min(bytes, (unsigned long)(osb->s_clustersize
  754. - (wc->w_pos & (osb->s_clustersize - 1))));
  755. } else {
  756. /*
  757. * cluster size > page size is the most common
  758. * case - we just stay within the target page
  759. * boundary.
  760. */
  761. bytes = min(bytes, PAGE_CACHE_SIZE - from);
  762. }
  763. to = from + bytes;
  764. BUG_ON(from > PAGE_CACHE_SIZE);
  765. BUG_ON(to > PAGE_CACHE_SIZE);
  766. BUG_ON(from < cluster_start);
  767. BUG_ON(to > cluster_end);
  768. if (wc->w_this_page_new)
  769. ret = ocfs2_map_page_blocks(wc->w_this_page, p_blkno, inode,
  770. cluster_start, cluster_end, 1);
  771. else
  772. ret = ocfs2_map_page_blocks(wc->w_this_page, p_blkno, inode,
  773. from, to, 0);
  774. if (ret) {
  775. mlog_errno(ret);
  776. goto out;
  777. }
  778. dst = kmap(wc->w_this_page);
  779. memcpy(dst + from, bp->b_src_buf + src_from, bytes);
  780. kunmap(wc->w_this_page);
  781. /*
  782. * XXX: This is slow, but simple. The caller of
  783. * ocfs2_buffered_write_cluster() is responsible for
  784. * passing through the iovecs, so it's difficult to
  785. * predict what our next step is in here after our
  786. * initial write. A future version should be pushing
  787. * that iovec manipulation further down.
  788. *
  789. * By setting this, we indicate that a copy from user
  790. * data was done, and subsequent calls for this
  791. * cluster will skip copying more data.
  792. */
  793. wc->w_finished_copy = 1;
  794. *ret_from = from;
  795. *ret_to = to;
  796. out:
  797. return bytes ? (unsigned int)bytes : ret;
  798. }
  799. /*
  800. * Map, fill and write a page to disk.
  801. *
  802. * The work of copying data is done via callback. Newly allocated
  803. * pages which don't take user data will be zero'd (set 'new' to
  804. * indicate an allocating write)
  805. *
  806. * Returns a negative error code or the number of bytes copied into
  807. * the page.
  808. */
  809. static int ocfs2_write_data_page(struct inode *inode, handle_t *handle,
  810. u64 *p_blkno, struct page *page,
  811. struct ocfs2_write_ctxt *wc, int new)
  812. {
  813. int ret, copied = 0;
  814. unsigned int from = 0, to = 0;
  815. unsigned int cluster_start, cluster_end;
  816. unsigned int zero_from = 0, zero_to = 0;
  817. ocfs2_figure_cluster_boundaries(OCFS2_SB(inode->i_sb), wc->w_cpos,
  818. &cluster_start, &cluster_end);
  819. if ((wc->w_pos >> PAGE_CACHE_SHIFT) == page->index
  820. && !wc->w_finished_copy) {
  821. wc->w_this_page = page;
  822. wc->w_this_page_new = new;
  823. ret = wc->w_write_data_page(inode, wc, p_blkno, &from, &to);
  824. if (ret < 0) {
  825. mlog_errno(ret);
  826. goto out;
  827. }
  828. copied = ret;
  829. zero_from = from;
  830. zero_to = to;
  831. if (new) {
  832. from = cluster_start;
  833. to = cluster_end;
  834. }
  835. } else {
  836. /*
  837. * If we haven't allocated the new page yet, we
  838. * shouldn't be writing it out without copying user
  839. * data. This is likely a math error from the caller.
  840. */
  841. BUG_ON(!new);
  842. from = cluster_start;
  843. to = cluster_end;
  844. ret = ocfs2_map_page_blocks(page, p_blkno, inode,
  845. cluster_start, cluster_end, 1);
  846. if (ret) {
  847. mlog_errno(ret);
  848. goto out;
  849. }
  850. }
  851. /*
  852. * Parts of newly allocated pages need to be zero'd.
  853. *
  854. * Above, we have also rewritten 'to' and 'from' - as far as
  855. * the rest of the function is concerned, the entire cluster
  856. * range inside of a page needs to be written.
  857. *
  858. * We can skip this if the page is up to date - it's already
  859. * been zero'd from being read in as a hole.
  860. */
  861. if (new && !PageUptodate(page))
  862. ocfs2_clear_page_regions(page, OCFS2_SB(inode->i_sb),
  863. wc->w_cpos, zero_from, zero_to);
  864. flush_dcache_page(page);
  865. if (ocfs2_should_order_data(inode)) {
  866. ret = walk_page_buffers(handle,
  867. page_buffers(page),
  868. from, to, NULL,
  869. ocfs2_journal_dirty_data);
  870. if (ret < 0)
  871. mlog_errno(ret);
  872. }
  873. /*
  874. * We don't use generic_commit_write() because we need to
  875. * handle our own i_size update.
  876. */
  877. ret = block_commit_write(page, from, to);
  878. if (ret)
  879. mlog_errno(ret);
  880. out:
  881. return copied ? copied : ret;
  882. }
  883. /*
  884. * Do the actual write of some data into an inode. Optionally allocate
  885. * in order to fulfill the write.
  886. *
  887. * cpos is the logical cluster offset within the file to write at
  888. *
  889. * 'phys' is the physical mapping of that offset. a 'phys' value of
  890. * zero indicates that allocation is required. In this case, data_ac
  891. * and meta_ac should be valid (meta_ac can be null if metadata
  892. * allocation isn't required).
  893. */
  894. static ssize_t ocfs2_write(struct file *file, u32 phys, handle_t *handle,
  895. struct buffer_head *di_bh,
  896. struct ocfs2_alloc_context *data_ac,
  897. struct ocfs2_alloc_context *meta_ac,
  898. struct ocfs2_write_ctxt *wc)
  899. {
  900. int ret, i, numpages = 1, new;
  901. unsigned int copied = 0;
  902. u32 tmp_pos;
  903. u64 v_blkno, p_blkno;
  904. struct address_space *mapping = file->f_mapping;
  905. struct inode *inode = mapping->host;
  906. unsigned long index, start;
  907. struct page **cpages;
  908. new = phys == 0 ? 1 : 0;
  909. /*
  910. * Figure out how many pages we'll be manipulating here. For
  911. * non allocating write, we just change the one
  912. * page. Otherwise, we'll need a whole clusters worth.
  913. */
  914. if (new)
  915. numpages = ocfs2_pages_per_cluster(inode->i_sb);
  916. cpages = kzalloc(sizeof(*cpages) * numpages, GFP_NOFS);
  917. if (!cpages) {
  918. ret = -ENOMEM;
  919. mlog_errno(ret);
  920. return ret;
  921. }
  922. /*
  923. * Fill our page array first. That way we've grabbed enough so
  924. * that we can zero and flush if we error after adding the
  925. * extent.
  926. */
  927. if (new) {
  928. start = ocfs2_align_clusters_to_page_index(inode->i_sb,
  929. wc->w_cpos);
  930. v_blkno = ocfs2_clusters_to_blocks(inode->i_sb, wc->w_cpos);
  931. } else {
  932. start = wc->w_pos >> PAGE_CACHE_SHIFT;
  933. v_blkno = wc->w_pos >> inode->i_sb->s_blocksize_bits;
  934. }
  935. for(i = 0; i < numpages; i++) {
  936. index = start + i;
  937. cpages[i] = find_or_create_page(mapping, index, GFP_NOFS);
  938. if (!cpages[i]) {
  939. ret = -ENOMEM;
  940. mlog_errno(ret);
  941. goto out;
  942. }
  943. }
  944. if (new) {
  945. /*
  946. * This is safe to call with the page locks - it won't take
  947. * any additional semaphores or cluster locks.
  948. */
  949. tmp_pos = wc->w_cpos;
  950. ret = ocfs2_do_extend_allocation(OCFS2_SB(inode->i_sb), inode,
  951. &tmp_pos, 1, di_bh, handle,
  952. data_ac, meta_ac, NULL);
  953. /*
  954. * This shouldn't happen because we must have already
  955. * calculated the correct meta data allocation required. The
  956. * internal tree allocation code should know how to increase
  957. * transaction credits itself.
  958. *
  959. * If need be, we could handle -EAGAIN for a
  960. * RESTART_TRANS here.
  961. */
  962. mlog_bug_on_msg(ret == -EAGAIN,
  963. "Inode %llu: EAGAIN return during allocation.\n",
  964. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  965. if (ret < 0) {
  966. mlog_errno(ret);
  967. goto out;
  968. }
  969. }
  970. ret = ocfs2_extent_map_get_blocks(inode, v_blkno, &p_blkno, NULL,
  971. NULL);
  972. if (ret < 0) {
  973. /*
  974. * XXX: Should we go readonly here?
  975. */
  976. mlog_errno(ret);
  977. goto out;
  978. }
  979. BUG_ON(p_blkno == 0);
  980. for(i = 0; i < numpages; i++) {
  981. ret = ocfs2_write_data_page(inode, handle, &p_blkno, cpages[i],
  982. wc, new);
  983. if (ret < 0) {
  984. mlog_errno(ret);
  985. goto out;
  986. }
  987. copied += ret;
  988. }
  989. out:
  990. for(i = 0; i < numpages; i++) {
  991. unlock_page(cpages[i]);
  992. mark_page_accessed(cpages[i]);
  993. page_cache_release(cpages[i]);
  994. }
  995. kfree(cpages);
  996. return copied ? copied : ret;
  997. }
  998. static void ocfs2_write_ctxt_init(struct ocfs2_write_ctxt *wc,
  999. struct ocfs2_super *osb, loff_t pos,
  1000. size_t count, ocfs2_page_writer *cb,
  1001. void *cb_priv)
  1002. {
  1003. wc->w_count = count;
  1004. wc->w_pos = pos;
  1005. wc->w_cpos = wc->w_pos >> osb->s_clustersize_bits;
  1006. wc->w_finished_copy = 0;
  1007. if (unlikely(PAGE_CACHE_SHIFT > osb->s_clustersize_bits))
  1008. wc->w_large_pages = 1;
  1009. else
  1010. wc->w_large_pages = 0;
  1011. wc->w_write_data_page = cb;
  1012. wc->w_private = cb_priv;
  1013. }
  1014. /*
  1015. * Write a cluster to an inode. The cluster may not be allocated yet,
  1016. * in which case it will be. This only exists for buffered writes -
  1017. * O_DIRECT takes a more "traditional" path through the kernel.
  1018. *
  1019. * The caller is responsible for incrementing pos, written counts, etc
  1020. *
  1021. * For file systems that don't support sparse files, pre-allocation
  1022. * and page zeroing up until cpos should be done prior to this
  1023. * function call.
  1024. *
  1025. * Callers should be holding i_sem, and the rw cluster lock.
  1026. *
  1027. * Returns the number of user bytes written, or less than zero for
  1028. * error.
  1029. */
  1030. ssize_t ocfs2_buffered_write_cluster(struct file *file, loff_t pos,
  1031. size_t count, ocfs2_page_writer *actor,
  1032. void *priv)
  1033. {
  1034. int ret, credits = OCFS2_INODE_UPDATE_CREDITS;
  1035. ssize_t written = 0;
  1036. u32 phys;
  1037. struct inode *inode = file->f_mapping->host;
  1038. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1039. struct buffer_head *di_bh = NULL;
  1040. struct ocfs2_dinode *di;
  1041. struct ocfs2_alloc_context *data_ac = NULL;
  1042. struct ocfs2_alloc_context *meta_ac = NULL;
  1043. handle_t *handle;
  1044. struct ocfs2_write_ctxt wc;
  1045. ocfs2_write_ctxt_init(&wc, osb, pos, count, actor, priv);
  1046. ret = ocfs2_meta_lock(inode, &di_bh, 1);
  1047. if (ret) {
  1048. mlog_errno(ret);
  1049. goto out;
  1050. }
  1051. di = (struct ocfs2_dinode *)di_bh->b_data;
  1052. /*
  1053. * Take alloc sem here to prevent concurrent lookups. That way
  1054. * the mapping, zeroing and tree manipulation within
  1055. * ocfs2_write() will be safe against ->readpage(). This
  1056. * should also serve to lock out allocation from a shared
  1057. * writeable region.
  1058. */
  1059. down_write(&OCFS2_I(inode)->ip_alloc_sem);
  1060. ret = ocfs2_get_clusters(inode, wc.w_cpos, &phys, NULL, NULL);
  1061. if (ret) {
  1062. mlog_errno(ret);
  1063. goto out_meta;
  1064. }
  1065. /* phys == 0 means that allocation is required. */
  1066. if (phys == 0) {
  1067. ret = ocfs2_lock_allocators(inode, di, 1, &data_ac, &meta_ac);
  1068. if (ret) {
  1069. mlog_errno(ret);
  1070. goto out_meta;
  1071. }
  1072. credits = ocfs2_calc_extend_credits(inode->i_sb, di, 1);
  1073. }
  1074. ret = ocfs2_data_lock(inode, 1);
  1075. if (ret) {
  1076. mlog_errno(ret);
  1077. goto out_meta;
  1078. }
  1079. handle = ocfs2_start_trans(osb, credits);
  1080. if (IS_ERR(handle)) {
  1081. ret = PTR_ERR(handle);
  1082. mlog_errno(ret);
  1083. goto out_data;
  1084. }
  1085. written = ocfs2_write(file, phys, handle, di_bh, data_ac,
  1086. meta_ac, &wc);
  1087. if (written < 0) {
  1088. ret = written;
  1089. mlog_errno(ret);
  1090. goto out_commit;
  1091. }
  1092. ret = ocfs2_journal_access(handle, inode, di_bh,
  1093. OCFS2_JOURNAL_ACCESS_WRITE);
  1094. if (ret) {
  1095. mlog_errno(ret);
  1096. goto out_commit;
  1097. }
  1098. pos += written;
  1099. if (pos > inode->i_size) {
  1100. i_size_write(inode, pos);
  1101. mark_inode_dirty(inode);
  1102. }
  1103. inode->i_blocks = ocfs2_inode_sector_count(inode);
  1104. di->i_size = cpu_to_le64((u64)i_size_read(inode));
  1105. inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  1106. di->i_mtime = di->i_ctime = cpu_to_le64(inode->i_mtime.tv_sec);
  1107. di->i_mtime_nsec = di->i_ctime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);
  1108. ret = ocfs2_journal_dirty(handle, di_bh);
  1109. if (ret)
  1110. mlog_errno(ret);
  1111. out_commit:
  1112. ocfs2_commit_trans(osb, handle);
  1113. out_data:
  1114. ocfs2_data_unlock(inode, 1);
  1115. out_meta:
  1116. up_write(&OCFS2_I(inode)->ip_alloc_sem);
  1117. ocfs2_meta_unlock(inode, 1);
  1118. out:
  1119. brelse(di_bh);
  1120. if (data_ac)
  1121. ocfs2_free_alloc_context(data_ac);
  1122. if (meta_ac)
  1123. ocfs2_free_alloc_context(meta_ac);
  1124. return written ? written : ret;
  1125. }
  1126. const struct address_space_operations ocfs2_aops = {
  1127. .readpage = ocfs2_readpage,
  1128. .writepage = ocfs2_writepage,
  1129. .bmap = ocfs2_bmap,
  1130. .sync_page = block_sync_page,
  1131. .direct_IO = ocfs2_direct_IO,
  1132. .invalidatepage = ocfs2_invalidatepage,
  1133. .releasepage = ocfs2_releasepage,
  1134. .migratepage = buffer_migrate_page,
  1135. };