aops.c 34 KB

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