aops.c 34 KB

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