aops.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313
  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. /* this io's submitter should not have unlocked this before we could */
  453. BUG_ON(!ocfs2_iocb_is_rw_locked(iocb));
  454. ocfs2_iocb_clear_rw_locked(iocb);
  455. up_read(&inode->i_alloc_sem);
  456. ocfs2_rw_unlock(inode, 0);
  457. }
  458. /*
  459. * ocfs2_invalidatepage() and ocfs2_releasepage() are shamelessly stolen
  460. * from ext3. PageChecked() bits have been removed as OCFS2 does not
  461. * do journalled data.
  462. */
  463. static void ocfs2_invalidatepage(struct page *page, unsigned long offset)
  464. {
  465. journal_t *journal = OCFS2_SB(page->mapping->host->i_sb)->journal->j_journal;
  466. journal_invalidatepage(journal, page, offset);
  467. }
  468. static int ocfs2_releasepage(struct page *page, gfp_t wait)
  469. {
  470. journal_t *journal = OCFS2_SB(page->mapping->host->i_sb)->journal->j_journal;
  471. if (!page_has_buffers(page))
  472. return 0;
  473. return journal_try_to_free_buffers(journal, page, wait);
  474. }
  475. static ssize_t ocfs2_direct_IO(int rw,
  476. struct kiocb *iocb,
  477. const struct iovec *iov,
  478. loff_t offset,
  479. unsigned long nr_segs)
  480. {
  481. struct file *file = iocb->ki_filp;
  482. struct inode *inode = file->f_path.dentry->d_inode->i_mapping->host;
  483. int ret;
  484. mlog_entry_void();
  485. if (!ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb))) {
  486. /*
  487. * We get PR data locks even for O_DIRECT. This
  488. * allows concurrent O_DIRECT I/O but doesn't let
  489. * O_DIRECT with extending and buffered zeroing writes
  490. * race. If they did race then the buffered zeroing
  491. * could be written back after the O_DIRECT I/O. It's
  492. * one thing to tell people not to mix buffered and
  493. * O_DIRECT writes, but expecting them to understand
  494. * that file extension is also an implicit buffered
  495. * write is too much. By getting the PR we force
  496. * writeback of the buffered zeroing before
  497. * proceeding.
  498. */
  499. ret = ocfs2_data_lock(inode, 0);
  500. if (ret < 0) {
  501. mlog_errno(ret);
  502. goto out;
  503. }
  504. ocfs2_data_unlock(inode, 0);
  505. }
  506. ret = blockdev_direct_IO_no_locking(rw, iocb, inode,
  507. inode->i_sb->s_bdev, iov, offset,
  508. nr_segs,
  509. ocfs2_direct_IO_get_blocks,
  510. ocfs2_dio_end_io);
  511. out:
  512. mlog_exit(ret);
  513. return ret;
  514. }
  515. static void ocfs2_figure_cluster_boundaries(struct ocfs2_super *osb,
  516. u32 cpos,
  517. unsigned int *start,
  518. unsigned int *end)
  519. {
  520. unsigned int cluster_start = 0, cluster_end = PAGE_CACHE_SIZE;
  521. if (unlikely(PAGE_CACHE_SHIFT > osb->s_clustersize_bits)) {
  522. unsigned int cpp;
  523. cpp = 1 << (PAGE_CACHE_SHIFT - osb->s_clustersize_bits);
  524. cluster_start = cpos % cpp;
  525. cluster_start = cluster_start << osb->s_clustersize_bits;
  526. cluster_end = cluster_start + osb->s_clustersize;
  527. }
  528. BUG_ON(cluster_start > PAGE_SIZE);
  529. BUG_ON(cluster_end > PAGE_SIZE);
  530. if (start)
  531. *start = cluster_start;
  532. if (end)
  533. *end = cluster_end;
  534. }
  535. /*
  536. * 'from' and 'to' are the region in the page to avoid zeroing.
  537. *
  538. * If pagesize > clustersize, this function will avoid zeroing outside
  539. * of the cluster boundary.
  540. *
  541. * from == to == 0 is code for "zero the entire cluster region"
  542. */
  543. static void ocfs2_clear_page_regions(struct page *page,
  544. struct ocfs2_super *osb, u32 cpos,
  545. unsigned from, unsigned to)
  546. {
  547. void *kaddr;
  548. unsigned int cluster_start, cluster_end;
  549. ocfs2_figure_cluster_boundaries(osb, cpos, &cluster_start, &cluster_end);
  550. kaddr = kmap_atomic(page, KM_USER0);
  551. if (from || to) {
  552. if (from > cluster_start)
  553. memset(kaddr + cluster_start, 0, from - cluster_start);
  554. if (to < cluster_end)
  555. memset(kaddr + to, 0, cluster_end - to);
  556. } else {
  557. memset(kaddr + cluster_start, 0, cluster_end - cluster_start);
  558. }
  559. kunmap_atomic(kaddr, KM_USER0);
  560. }
  561. /*
  562. * Some of this taken from block_prepare_write(). We already have our
  563. * mapping by now though, and the entire write will be allocating or
  564. * it won't, so not much need to use BH_New.
  565. *
  566. * This will also skip zeroing, which is handled externally.
  567. */
  568. int ocfs2_map_page_blocks(struct page *page, u64 *p_blkno,
  569. struct inode *inode, unsigned int from,
  570. unsigned int to, int new)
  571. {
  572. int ret = 0;
  573. struct buffer_head *head, *bh, *wait[2], **wait_bh = wait;
  574. unsigned int block_end, block_start;
  575. unsigned int bsize = 1 << inode->i_blkbits;
  576. if (!page_has_buffers(page))
  577. create_empty_buffers(page, bsize, 0);
  578. head = page_buffers(page);
  579. for (bh = head, block_start = 0; bh != head || !block_start;
  580. bh = bh->b_this_page, block_start += bsize) {
  581. block_end = block_start + bsize;
  582. /*
  583. * Ignore blocks outside of our i/o range -
  584. * they may belong to unallocated clusters.
  585. */
  586. if (block_start >= to || block_end <= from) {
  587. if (PageUptodate(page))
  588. set_buffer_uptodate(bh);
  589. continue;
  590. }
  591. /*
  592. * For an allocating write with cluster size >= page
  593. * size, we always write the entire page.
  594. */
  595. if (buffer_new(bh))
  596. clear_buffer_new(bh);
  597. if (!buffer_mapped(bh)) {
  598. map_bh(bh, inode->i_sb, *p_blkno);
  599. unmap_underlying_metadata(bh->b_bdev, bh->b_blocknr);
  600. }
  601. if (PageUptodate(page)) {
  602. if (!buffer_uptodate(bh))
  603. set_buffer_uptodate(bh);
  604. } else if (!buffer_uptodate(bh) && !buffer_delay(bh) &&
  605. (block_start < from || block_end > to)) {
  606. ll_rw_block(READ, 1, &bh);
  607. *wait_bh++=bh;
  608. }
  609. *p_blkno = *p_blkno + 1;
  610. }
  611. /*
  612. * If we issued read requests - let them complete.
  613. */
  614. while(wait_bh > wait) {
  615. wait_on_buffer(*--wait_bh);
  616. if (!buffer_uptodate(*wait_bh))
  617. ret = -EIO;
  618. }
  619. if (ret == 0 || !new)
  620. return ret;
  621. /*
  622. * If we get -EIO above, zero out any newly allocated blocks
  623. * to avoid exposing stale data.
  624. */
  625. bh = head;
  626. block_start = 0;
  627. do {
  628. void *kaddr;
  629. block_end = block_start + bsize;
  630. if (block_end <= from)
  631. goto next_bh;
  632. if (block_start >= to)
  633. break;
  634. kaddr = kmap_atomic(page, KM_USER0);
  635. memset(kaddr+block_start, 0, bh->b_size);
  636. flush_dcache_page(page);
  637. kunmap_atomic(kaddr, KM_USER0);
  638. set_buffer_uptodate(bh);
  639. mark_buffer_dirty(bh);
  640. next_bh:
  641. block_start = block_end;
  642. bh = bh->b_this_page;
  643. } while (bh != head);
  644. return ret;
  645. }
  646. /*
  647. * This will copy user data from the buffer page in the splice
  648. * context.
  649. *
  650. * For now, we ignore SPLICE_F_MOVE as that would require some extra
  651. * communication out all the way to ocfs2_write().
  652. */
  653. int ocfs2_map_and_write_splice_data(struct inode *inode,
  654. struct ocfs2_write_ctxt *wc, u64 *p_blkno,
  655. unsigned int *ret_from, unsigned int *ret_to)
  656. {
  657. int ret;
  658. unsigned int to, from, cluster_start, cluster_end;
  659. char *src, *dst;
  660. struct ocfs2_splice_write_priv *sp = wc->w_private;
  661. struct pipe_buffer *buf = sp->s_buf;
  662. unsigned long bytes, src_from;
  663. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  664. ocfs2_figure_cluster_boundaries(osb, wc->w_cpos, &cluster_start,
  665. &cluster_end);
  666. from = sp->s_offset;
  667. src_from = sp->s_buf_offset;
  668. bytes = wc->w_count;
  669. if (wc->w_large_pages) {
  670. /*
  671. * For cluster size < page size, we have to
  672. * calculate pos within the cluster and obey
  673. * the rightmost boundary.
  674. */
  675. bytes = min(bytes, (unsigned long)(osb->s_clustersize
  676. - (wc->w_pos & (osb->s_clustersize - 1))));
  677. }
  678. to = from + bytes;
  679. if (wc->w_this_page_new)
  680. ret = ocfs2_map_page_blocks(wc->w_this_page, p_blkno, inode,
  681. cluster_start, cluster_end, 1);
  682. else
  683. ret = ocfs2_map_page_blocks(wc->w_this_page, p_blkno, inode,
  684. from, to, 0);
  685. if (ret) {
  686. mlog_errno(ret);
  687. goto out;
  688. }
  689. BUG_ON(from > PAGE_CACHE_SIZE);
  690. BUG_ON(to > PAGE_CACHE_SIZE);
  691. BUG_ON(from > osb->s_clustersize);
  692. BUG_ON(to > osb->s_clustersize);
  693. src = buf->ops->map(sp->s_pipe, buf, 1);
  694. dst = kmap_atomic(wc->w_this_page, KM_USER1);
  695. memcpy(dst + from, src + src_from, bytes);
  696. kunmap_atomic(wc->w_this_page, KM_USER1);
  697. buf->ops->unmap(sp->s_pipe, buf, src);
  698. wc->w_finished_copy = 1;
  699. *ret_from = from;
  700. *ret_to = to;
  701. out:
  702. return bytes ? (unsigned int)bytes : ret;
  703. }
  704. /*
  705. * This will copy user data from the iovec in the buffered write
  706. * context.
  707. */
  708. int ocfs2_map_and_write_user_data(struct inode *inode,
  709. struct ocfs2_write_ctxt *wc, u64 *p_blkno,
  710. unsigned int *ret_from, unsigned int *ret_to)
  711. {
  712. int ret;
  713. unsigned int to, from, cluster_start, cluster_end;
  714. unsigned long bytes, src_from;
  715. char *dst;
  716. struct ocfs2_buffered_write_priv *bp = wc->w_private;
  717. const struct iovec *cur_iov = bp->b_cur_iov;
  718. char __user *buf;
  719. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  720. ocfs2_figure_cluster_boundaries(osb, wc->w_cpos, &cluster_start,
  721. &cluster_end);
  722. buf = cur_iov->iov_base + bp->b_cur_off;
  723. src_from = (unsigned long)buf & ~PAGE_CACHE_MASK;
  724. from = wc->w_pos & (PAGE_CACHE_SIZE - 1);
  725. /*
  726. * This is a lot of comparisons, but it reads quite
  727. * easily, which is important here.
  728. */
  729. /* Stay within the src page */
  730. bytes = PAGE_SIZE - src_from;
  731. /* Stay within the vector */
  732. bytes = min(bytes,
  733. (unsigned long)(cur_iov->iov_len - bp->b_cur_off));
  734. /* Stay within count */
  735. bytes = min(bytes, (unsigned long)wc->w_count);
  736. /*
  737. * For clustersize > page size, just stay within
  738. * target page, otherwise we have to calculate pos
  739. * within the cluster and obey the rightmost
  740. * boundary.
  741. */
  742. if (wc->w_large_pages) {
  743. /*
  744. * For cluster size < page size, we have to
  745. * calculate pos within the cluster and obey
  746. * the rightmost boundary.
  747. */
  748. bytes = min(bytes, (unsigned long)(osb->s_clustersize
  749. - (wc->w_pos & (osb->s_clustersize - 1))));
  750. } else {
  751. /*
  752. * cluster size > page size is the most common
  753. * case - we just stay within the target page
  754. * boundary.
  755. */
  756. bytes = min(bytes, PAGE_CACHE_SIZE - from);
  757. }
  758. to = from + bytes;
  759. if (wc->w_this_page_new)
  760. ret = ocfs2_map_page_blocks(wc->w_this_page, p_blkno, inode,
  761. cluster_start, cluster_end, 1);
  762. else
  763. ret = ocfs2_map_page_blocks(wc->w_this_page, p_blkno, inode,
  764. from, to, 0);
  765. if (ret) {
  766. mlog_errno(ret);
  767. goto out;
  768. }
  769. BUG_ON(from > PAGE_CACHE_SIZE);
  770. BUG_ON(to > PAGE_CACHE_SIZE);
  771. BUG_ON(from > osb->s_clustersize);
  772. BUG_ON(to > osb->s_clustersize);
  773. dst = kmap(wc->w_this_page);
  774. memcpy(dst + from, bp->b_src_buf + src_from, bytes);
  775. kunmap(wc->w_this_page);
  776. /*
  777. * XXX: This is slow, but simple. The caller of
  778. * ocfs2_buffered_write_cluster() is responsible for
  779. * passing through the iovecs, so it's difficult to
  780. * predict what our next step is in here after our
  781. * initial write. A future version should be pushing
  782. * that iovec manipulation further down.
  783. *
  784. * By setting this, we indicate that a copy from user
  785. * data was done, and subsequent calls for this
  786. * cluster will skip copying more data.
  787. */
  788. wc->w_finished_copy = 1;
  789. *ret_from = from;
  790. *ret_to = to;
  791. out:
  792. return bytes ? (unsigned int)bytes : ret;
  793. }
  794. /*
  795. * Map, fill and write a page to disk.
  796. *
  797. * The work of copying data is done via callback. Newly allocated
  798. * pages which don't take user data will be zero'd (set 'new' to
  799. * indicate an allocating write)
  800. *
  801. * Returns a negative error code or the number of bytes copied into
  802. * the page.
  803. */
  804. int ocfs2_write_data_page(struct inode *inode, handle_t *handle,
  805. u64 *p_blkno, struct page *page,
  806. struct ocfs2_write_ctxt *wc, int new)
  807. {
  808. int ret, copied = 0;
  809. unsigned int from = 0, to = 0;
  810. unsigned int cluster_start, cluster_end;
  811. unsigned int zero_from = 0, zero_to = 0;
  812. ocfs2_figure_cluster_boundaries(OCFS2_SB(inode->i_sb), wc->w_cpos,
  813. &cluster_start, &cluster_end);
  814. if ((wc->w_pos >> PAGE_CACHE_SHIFT) == page->index
  815. && !wc->w_finished_copy) {
  816. wc->w_this_page = page;
  817. wc->w_this_page_new = new;
  818. ret = wc->w_write_data_page(inode, wc, p_blkno, &from, &to);
  819. if (ret < 0) {
  820. mlog_errno(ret);
  821. goto out;
  822. }
  823. copied = ret;
  824. zero_from = from;
  825. zero_to = to;
  826. if (new) {
  827. from = cluster_start;
  828. to = cluster_end;
  829. }
  830. } else {
  831. /*
  832. * If we haven't allocated the new page yet, we
  833. * shouldn't be writing it out without copying user
  834. * data. This is likely a math error from the caller.
  835. */
  836. BUG_ON(!new);
  837. from = cluster_start;
  838. to = cluster_end;
  839. ret = ocfs2_map_page_blocks(page, p_blkno, inode,
  840. cluster_start, cluster_end, 1);
  841. if (ret) {
  842. mlog_errno(ret);
  843. goto out;
  844. }
  845. }
  846. /*
  847. * Parts of newly allocated pages need to be zero'd.
  848. *
  849. * Above, we have also rewritten 'to' and 'from' - as far as
  850. * the rest of the function is concerned, the entire cluster
  851. * range inside of a page needs to be written.
  852. *
  853. * We can skip this if the page is up to date - it's already
  854. * been zero'd from being read in as a hole.
  855. */
  856. if (new && !PageUptodate(page))
  857. ocfs2_clear_page_regions(page, OCFS2_SB(inode->i_sb),
  858. wc->w_cpos, zero_from, zero_to);
  859. flush_dcache_page(page);
  860. if (ocfs2_should_order_data(inode)) {
  861. ret = walk_page_buffers(handle,
  862. page_buffers(page),
  863. from, to, NULL,
  864. ocfs2_journal_dirty_data);
  865. if (ret < 0)
  866. mlog_errno(ret);
  867. }
  868. /*
  869. * We don't use generic_commit_write() because we need to
  870. * handle our own i_size update.
  871. */
  872. ret = block_commit_write(page, from, to);
  873. if (ret)
  874. mlog_errno(ret);
  875. out:
  876. return copied ? copied : ret;
  877. }
  878. /*
  879. * Do the actual write of some data into an inode. Optionally allocate
  880. * in order to fulfill the write.
  881. *
  882. * cpos is the logical cluster offset within the file to write at
  883. *
  884. * 'phys' is the physical mapping of that offset. a 'phys' value of
  885. * zero indicates that allocation is required. In this case, data_ac
  886. * and meta_ac should be valid (meta_ac can be null if metadata
  887. * allocation isn't required).
  888. */
  889. static ssize_t ocfs2_write(struct file *file, u32 phys, handle_t *handle,
  890. struct buffer_head *di_bh,
  891. struct ocfs2_alloc_context *data_ac,
  892. struct ocfs2_alloc_context *meta_ac,
  893. struct ocfs2_write_ctxt *wc)
  894. {
  895. int ret, i, numpages = 1, new;
  896. unsigned int copied = 0;
  897. u32 tmp_pos;
  898. u64 v_blkno, p_blkno;
  899. struct address_space *mapping = file->f_mapping;
  900. struct inode *inode = mapping->host;
  901. unsigned long index, start;
  902. struct page **cpages;
  903. new = phys == 0 ? 1 : 0;
  904. /*
  905. * Figure out how many pages we'll be manipulating here. For
  906. * non allocating write, we just change the one
  907. * page. Otherwise, we'll need a whole clusters worth.
  908. */
  909. if (new)
  910. numpages = ocfs2_pages_per_cluster(inode->i_sb);
  911. cpages = kzalloc(sizeof(*cpages) * numpages, GFP_NOFS);
  912. if (!cpages) {
  913. ret = -ENOMEM;
  914. mlog_errno(ret);
  915. return ret;
  916. }
  917. /*
  918. * Fill our page array first. That way we've grabbed enough so
  919. * that we can zero and flush if we error after adding the
  920. * extent.
  921. */
  922. if (new) {
  923. start = ocfs2_align_clusters_to_page_index(inode->i_sb,
  924. wc->w_cpos);
  925. v_blkno = ocfs2_clusters_to_blocks(inode->i_sb, wc->w_cpos);
  926. } else {
  927. start = wc->w_pos >> PAGE_CACHE_SHIFT;
  928. v_blkno = wc->w_pos >> inode->i_sb->s_blocksize_bits;
  929. }
  930. for(i = 0; i < numpages; i++) {
  931. index = start + i;
  932. cpages[i] = grab_cache_page(mapping, index);
  933. if (!cpages[i]) {
  934. ret = -ENOMEM;
  935. mlog_errno(ret);
  936. goto out;
  937. }
  938. }
  939. if (new) {
  940. /*
  941. * This is safe to call with the page locks - it won't take
  942. * any additional semaphores or cluster locks.
  943. */
  944. tmp_pos = wc->w_cpos;
  945. ret = ocfs2_do_extend_allocation(OCFS2_SB(inode->i_sb), inode,
  946. &tmp_pos, 1, di_bh, handle,
  947. data_ac, meta_ac, NULL);
  948. /*
  949. * This shouldn't happen because we must have already
  950. * calculated the correct meta data allocation required. The
  951. * internal tree allocation code should know how to increase
  952. * transaction credits itself.
  953. *
  954. * If need be, we could handle -EAGAIN for a
  955. * RESTART_TRANS here.
  956. */
  957. mlog_bug_on_msg(ret == -EAGAIN,
  958. "Inode %llu: EAGAIN return during allocation.\n",
  959. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  960. if (ret < 0) {
  961. mlog_errno(ret);
  962. goto out;
  963. }
  964. }
  965. ret = ocfs2_extent_map_get_blocks(inode, v_blkno, &p_blkno, NULL,
  966. NULL);
  967. if (ret < 0) {
  968. /*
  969. * XXX: Should we go readonly here?
  970. */
  971. mlog_errno(ret);
  972. goto out;
  973. }
  974. BUG_ON(p_blkno == 0);
  975. for(i = 0; i < numpages; i++) {
  976. ret = ocfs2_write_data_page(inode, handle, &p_blkno, cpages[i],
  977. wc, new);
  978. if (ret < 0) {
  979. mlog_errno(ret);
  980. goto out;
  981. }
  982. copied += ret;
  983. }
  984. out:
  985. for(i = 0; i < numpages; i++) {
  986. unlock_page(cpages[i]);
  987. mark_page_accessed(cpages[i]);
  988. page_cache_release(cpages[i]);
  989. }
  990. kfree(cpages);
  991. return copied ? copied : ret;
  992. }
  993. static void ocfs2_write_ctxt_init(struct ocfs2_write_ctxt *wc,
  994. struct ocfs2_super *osb, loff_t pos,
  995. size_t count, ocfs2_page_writer *cb,
  996. void *cb_priv)
  997. {
  998. wc->w_count = count;
  999. wc->w_pos = pos;
  1000. wc->w_cpos = wc->w_pos >> osb->s_clustersize_bits;
  1001. wc->w_finished_copy = 0;
  1002. if (unlikely(PAGE_CACHE_SHIFT > osb->s_clustersize_bits))
  1003. wc->w_large_pages = 1;
  1004. else
  1005. wc->w_large_pages = 0;
  1006. wc->w_write_data_page = cb;
  1007. wc->w_private = cb_priv;
  1008. }
  1009. /*
  1010. * Write a cluster to an inode. The cluster may not be allocated yet,
  1011. * in which case it will be. This only exists for buffered writes -
  1012. * O_DIRECT takes a more "traditional" path through the kernel.
  1013. *
  1014. * The caller is responsible for incrementing pos, written counts, etc
  1015. *
  1016. * For file systems that don't support sparse files, pre-allocation
  1017. * and page zeroing up until cpos should be done prior to this
  1018. * function call.
  1019. *
  1020. * Callers should be holding i_sem, and the rw cluster lock.
  1021. *
  1022. * Returns the number of user bytes written, or less than zero for
  1023. * error.
  1024. */
  1025. ssize_t ocfs2_buffered_write_cluster(struct file *file, loff_t pos,
  1026. size_t count, ocfs2_page_writer *actor,
  1027. void *priv)
  1028. {
  1029. int ret, credits = OCFS2_INODE_UPDATE_CREDITS;
  1030. ssize_t written = 0;
  1031. u32 phys;
  1032. struct inode *inode = file->f_mapping->host;
  1033. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1034. struct buffer_head *di_bh = NULL;
  1035. struct ocfs2_dinode *di;
  1036. struct ocfs2_alloc_context *data_ac = NULL;
  1037. struct ocfs2_alloc_context *meta_ac = NULL;
  1038. handle_t *handle;
  1039. struct ocfs2_write_ctxt wc;
  1040. ocfs2_write_ctxt_init(&wc, osb, pos, count, actor, priv);
  1041. ret = ocfs2_meta_lock(inode, &di_bh, 1);
  1042. if (ret) {
  1043. mlog_errno(ret);
  1044. goto out;
  1045. }
  1046. di = (struct ocfs2_dinode *)di_bh->b_data;
  1047. /*
  1048. * Take alloc sem here to prevent concurrent lookups. That way
  1049. * the mapping, zeroing and tree manipulation within
  1050. * ocfs2_write() will be safe against ->readpage(). This
  1051. * should also serve to lock out allocation from a shared
  1052. * writeable region.
  1053. */
  1054. down_write(&OCFS2_I(inode)->ip_alloc_sem);
  1055. ret = ocfs2_get_clusters(inode, wc.w_cpos, &phys, NULL, NULL);
  1056. if (ret) {
  1057. mlog_errno(ret);
  1058. goto out_meta;
  1059. }
  1060. /* phys == 0 means that allocation is required. */
  1061. if (phys == 0) {
  1062. ret = ocfs2_lock_allocators(inode, di, 1, &data_ac, &meta_ac);
  1063. if (ret) {
  1064. mlog_errno(ret);
  1065. goto out_meta;
  1066. }
  1067. credits = ocfs2_calc_extend_credits(inode->i_sb, di, 1);
  1068. }
  1069. ret = ocfs2_data_lock(inode, 1);
  1070. if (ret) {
  1071. mlog_errno(ret);
  1072. goto out_meta;
  1073. }
  1074. handle = ocfs2_start_trans(osb, credits);
  1075. if (IS_ERR(handle)) {
  1076. ret = PTR_ERR(handle);
  1077. mlog_errno(ret);
  1078. goto out_data;
  1079. }
  1080. written = ocfs2_write(file, phys, handle, di_bh, data_ac,
  1081. meta_ac, &wc);
  1082. if (written < 0) {
  1083. ret = written;
  1084. mlog_errno(ret);
  1085. goto out_commit;
  1086. }
  1087. ret = ocfs2_journal_access(handle, inode, di_bh,
  1088. OCFS2_JOURNAL_ACCESS_WRITE);
  1089. if (ret) {
  1090. mlog_errno(ret);
  1091. goto out_commit;
  1092. }
  1093. pos += written;
  1094. if (pos > inode->i_size) {
  1095. i_size_write(inode, pos);
  1096. mark_inode_dirty(inode);
  1097. }
  1098. inode->i_blocks = ocfs2_inode_sector_count(inode);
  1099. di->i_size = cpu_to_le64((u64)i_size_read(inode));
  1100. inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  1101. di->i_mtime = di->i_ctime = cpu_to_le64(inode->i_mtime.tv_sec);
  1102. di->i_mtime_nsec = di->i_ctime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);
  1103. ret = ocfs2_journal_dirty(handle, di_bh);
  1104. if (ret)
  1105. mlog_errno(ret);
  1106. out_commit:
  1107. ocfs2_commit_trans(osb, handle);
  1108. out_data:
  1109. ocfs2_data_unlock(inode, 1);
  1110. out_meta:
  1111. up_write(&OCFS2_I(inode)->ip_alloc_sem);
  1112. ocfs2_meta_unlock(inode, 1);
  1113. out:
  1114. brelse(di_bh);
  1115. if (data_ac)
  1116. ocfs2_free_alloc_context(data_ac);
  1117. if (meta_ac)
  1118. ocfs2_free_alloc_context(meta_ac);
  1119. return written ? written : ret;
  1120. }
  1121. const struct address_space_operations ocfs2_aops = {
  1122. .readpage = ocfs2_readpage,
  1123. .writepage = ocfs2_writepage,
  1124. .bmap = ocfs2_bmap,
  1125. .sync_page = block_sync_page,
  1126. .direct_IO = ocfs2_direct_IO,
  1127. .invalidatepage = ocfs2_invalidatepage,
  1128. .releasepage = ocfs2_releasepage,
  1129. .migratepage = buffer_migrate_page,
  1130. };