aops.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988
  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. #include <linux/mpage.h>
  29. #define MLOG_MASK_PREFIX ML_FILE_IO
  30. #include <cluster/masklog.h>
  31. #include "ocfs2.h"
  32. #include "alloc.h"
  33. #include "aops.h"
  34. #include "dlmglue.h"
  35. #include "extent_map.h"
  36. #include "file.h"
  37. #include "inode.h"
  38. #include "journal.h"
  39. #include "suballoc.h"
  40. #include "super.h"
  41. #include "symlink.h"
  42. #include "buffer_head_io.h"
  43. static int ocfs2_symlink_get_block(struct inode *inode, sector_t iblock,
  44. struct buffer_head *bh_result, int create)
  45. {
  46. int err = -EIO;
  47. int status;
  48. struct ocfs2_dinode *fe = NULL;
  49. struct buffer_head *bh = NULL;
  50. struct buffer_head *buffer_cache_bh = NULL;
  51. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  52. void *kaddr;
  53. mlog_entry("(0x%p, %llu, 0x%p, %d)\n", inode,
  54. (unsigned long long)iblock, bh_result, create);
  55. BUG_ON(ocfs2_inode_is_fast_symlink(inode));
  56. if ((iblock << inode->i_sb->s_blocksize_bits) > PATH_MAX + 1) {
  57. mlog(ML_ERROR, "block offset > PATH_MAX: %llu",
  58. (unsigned long long)iblock);
  59. goto bail;
  60. }
  61. status = ocfs2_read_block(OCFS2_SB(inode->i_sb),
  62. OCFS2_I(inode)->ip_blkno,
  63. &bh, OCFS2_BH_CACHED, inode);
  64. if (status < 0) {
  65. mlog_errno(status);
  66. goto bail;
  67. }
  68. fe = (struct ocfs2_dinode *) bh->b_data;
  69. if (!OCFS2_IS_VALID_DINODE(fe)) {
  70. mlog(ML_ERROR, "Invalid dinode #%llu: signature = %.*s\n",
  71. (unsigned long long)le64_to_cpu(fe->i_blkno), 7,
  72. fe->i_signature);
  73. goto bail;
  74. }
  75. if ((u64)iblock >= ocfs2_clusters_to_blocks(inode->i_sb,
  76. le32_to_cpu(fe->i_clusters))) {
  77. mlog(ML_ERROR, "block offset is outside the allocated size: "
  78. "%llu\n", (unsigned long long)iblock);
  79. goto bail;
  80. }
  81. /* We don't use the page cache to create symlink data, so if
  82. * need be, copy it over from the buffer cache. */
  83. if (!buffer_uptodate(bh_result) && ocfs2_inode_is_new(inode)) {
  84. u64 blkno = le64_to_cpu(fe->id2.i_list.l_recs[0].e_blkno) +
  85. iblock;
  86. buffer_cache_bh = sb_getblk(osb->sb, blkno);
  87. if (!buffer_cache_bh) {
  88. mlog(ML_ERROR, "couldn't getblock for symlink!\n");
  89. goto bail;
  90. }
  91. /* we haven't locked out transactions, so a commit
  92. * could've happened. Since we've got a reference on
  93. * the bh, even if it commits while we're doing the
  94. * copy, the data is still good. */
  95. if (buffer_jbd(buffer_cache_bh)
  96. && ocfs2_inode_is_new(inode)) {
  97. kaddr = kmap_atomic(bh_result->b_page, KM_USER0);
  98. if (!kaddr) {
  99. mlog(ML_ERROR, "couldn't kmap!\n");
  100. goto bail;
  101. }
  102. memcpy(kaddr + (bh_result->b_size * iblock),
  103. buffer_cache_bh->b_data,
  104. bh_result->b_size);
  105. kunmap_atomic(kaddr, KM_USER0);
  106. set_buffer_uptodate(bh_result);
  107. }
  108. brelse(buffer_cache_bh);
  109. }
  110. map_bh(bh_result, inode->i_sb,
  111. le64_to_cpu(fe->id2.i_list.l_recs[0].e_blkno) + iblock);
  112. err = 0;
  113. bail:
  114. brelse(bh);
  115. mlog_exit(err);
  116. return err;
  117. }
  118. static int ocfs2_get_block(struct inode *inode, sector_t iblock,
  119. struct buffer_head *bh_result, int create)
  120. {
  121. int err = 0;
  122. unsigned int ext_flags;
  123. u64 max_blocks = bh_result->b_size >> inode->i_blkbits;
  124. u64 p_blkno, count, past_eof;
  125. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  126. mlog_entry("(0x%p, %llu, 0x%p, %d)\n", inode,
  127. (unsigned long long)iblock, bh_result, create);
  128. if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_SYSTEM_FILE)
  129. mlog(ML_NOTICE, "get_block on system inode 0x%p (%lu)\n",
  130. inode, inode->i_ino);
  131. if (S_ISLNK(inode->i_mode)) {
  132. /* this always does I/O for some reason. */
  133. err = ocfs2_symlink_get_block(inode, iblock, bh_result, create);
  134. goto bail;
  135. }
  136. err = ocfs2_extent_map_get_blocks(inode, iblock, &p_blkno, &count,
  137. &ext_flags);
  138. if (err) {
  139. mlog(ML_ERROR, "Error %d from get_blocks(0x%p, %llu, 1, "
  140. "%llu, NULL)\n", err, inode, (unsigned long long)iblock,
  141. (unsigned long long)p_blkno);
  142. goto bail;
  143. }
  144. if (max_blocks < count)
  145. count = max_blocks;
  146. /*
  147. * ocfs2 never allocates in this function - the only time we
  148. * need to use BH_New is when we're extending i_size on a file
  149. * system which doesn't support holes, in which case BH_New
  150. * allows block_prepare_write() to zero.
  151. *
  152. * If we see this on a sparse file system, then a truncate has
  153. * raced us and removed the cluster. In this case, we clear
  154. * the buffers dirty and uptodate bits and let the buffer code
  155. * ignore it as a hole.
  156. */
  157. if (create && p_blkno == 0 && ocfs2_sparse_alloc(osb)) {
  158. clear_buffer_dirty(bh_result);
  159. clear_buffer_uptodate(bh_result);
  160. goto bail;
  161. }
  162. /* Treat the unwritten extent as a hole for zeroing purposes. */
  163. if (p_blkno && !(ext_flags & OCFS2_EXT_UNWRITTEN))
  164. map_bh(bh_result, inode->i_sb, p_blkno);
  165. bh_result->b_size = count << inode->i_blkbits;
  166. if (!ocfs2_sparse_alloc(osb)) {
  167. if (p_blkno == 0) {
  168. err = -EIO;
  169. mlog(ML_ERROR,
  170. "iblock = %llu p_blkno = %llu blkno=(%llu)\n",
  171. (unsigned long long)iblock,
  172. (unsigned long long)p_blkno,
  173. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  174. mlog(ML_ERROR, "Size %llu, clusters %u\n", (unsigned long long)i_size_read(inode), OCFS2_I(inode)->ip_clusters);
  175. dump_stack();
  176. }
  177. past_eof = ocfs2_blocks_for_bytes(inode->i_sb, i_size_read(inode));
  178. mlog(0, "Inode %lu, past_eof = %llu\n", inode->i_ino,
  179. (unsigned long long)past_eof);
  180. if (create && (iblock >= past_eof))
  181. set_buffer_new(bh_result);
  182. }
  183. bail:
  184. if (err < 0)
  185. err = -EIO;
  186. mlog_exit(err);
  187. return err;
  188. }
  189. int ocfs2_read_inline_data(struct inode *inode, struct page *page,
  190. struct buffer_head *di_bh)
  191. {
  192. void *kaddr;
  193. loff_t size;
  194. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  195. if (!(le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_DATA_FL)) {
  196. ocfs2_error(inode->i_sb, "Inode %llu lost inline data flag",
  197. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  198. return -EROFS;
  199. }
  200. size = i_size_read(inode);
  201. if (size > PAGE_CACHE_SIZE ||
  202. size > ocfs2_max_inline_data(inode->i_sb)) {
  203. ocfs2_error(inode->i_sb,
  204. "Inode %llu has with inline data has bad size: %Lu",
  205. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  206. (unsigned long long)size);
  207. return -EROFS;
  208. }
  209. kaddr = kmap_atomic(page, KM_USER0);
  210. if (size)
  211. memcpy(kaddr, di->id2.i_data.id_data, size);
  212. /* Clear the remaining part of the page */
  213. memset(kaddr + size, 0, PAGE_CACHE_SIZE - size);
  214. flush_dcache_page(page);
  215. kunmap_atomic(kaddr, KM_USER0);
  216. SetPageUptodate(page);
  217. return 0;
  218. }
  219. static int ocfs2_readpage_inline(struct inode *inode, struct page *page)
  220. {
  221. int ret;
  222. struct buffer_head *di_bh = NULL;
  223. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  224. BUG_ON(!PageLocked(page));
  225. BUG_ON(!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL));
  226. ret = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno, &di_bh,
  227. OCFS2_BH_CACHED, inode);
  228. if (ret) {
  229. mlog_errno(ret);
  230. goto out;
  231. }
  232. ret = ocfs2_read_inline_data(inode, page, di_bh);
  233. out:
  234. unlock_page(page);
  235. brelse(di_bh);
  236. return ret;
  237. }
  238. static int ocfs2_readpage(struct file *file, struct page *page)
  239. {
  240. struct inode *inode = page->mapping->host;
  241. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  242. loff_t start = (loff_t)page->index << PAGE_CACHE_SHIFT;
  243. int ret, unlock = 1;
  244. mlog_entry("(0x%p, %lu)\n", file, (page ? page->index : 0));
  245. ret = ocfs2_inode_lock_with_page(inode, NULL, 0, page);
  246. if (ret != 0) {
  247. if (ret == AOP_TRUNCATED_PAGE)
  248. unlock = 0;
  249. mlog_errno(ret);
  250. goto out;
  251. }
  252. if (down_read_trylock(&oi->ip_alloc_sem) == 0) {
  253. ret = AOP_TRUNCATED_PAGE;
  254. goto out_inode_unlock;
  255. }
  256. /*
  257. * i_size might have just been updated as we grabed the meta lock. We
  258. * might now be discovering a truncate that hit on another node.
  259. * block_read_full_page->get_block freaks out if it is asked to read
  260. * beyond the end of a file, so we check here. Callers
  261. * (generic_file_read, vm_ops->fault) are clever enough to check i_size
  262. * and notice that the page they just read isn't needed.
  263. *
  264. * XXX sys_readahead() seems to get that wrong?
  265. */
  266. if (start >= i_size_read(inode)) {
  267. zero_user(page, 0, PAGE_SIZE);
  268. SetPageUptodate(page);
  269. ret = 0;
  270. goto out_alloc;
  271. }
  272. if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL)
  273. ret = ocfs2_readpage_inline(inode, page);
  274. else
  275. ret = block_read_full_page(page, ocfs2_get_block);
  276. unlock = 0;
  277. out_alloc:
  278. up_read(&OCFS2_I(inode)->ip_alloc_sem);
  279. out_inode_unlock:
  280. ocfs2_inode_unlock(inode, 0);
  281. out:
  282. if (unlock)
  283. unlock_page(page);
  284. mlog_exit(ret);
  285. return ret;
  286. }
  287. /*
  288. * This is used only for read-ahead. Failures or difficult to handle
  289. * situations are safe to ignore.
  290. *
  291. * Right now, we don't bother with BH_Boundary - in-inode extent lists
  292. * are quite large (243 extents on 4k blocks), so most inodes don't
  293. * grow out to a tree. If need be, detecting boundary extents could
  294. * trivially be added in a future version of ocfs2_get_block().
  295. */
  296. static int ocfs2_readpages(struct file *filp, struct address_space *mapping,
  297. struct list_head *pages, unsigned nr_pages)
  298. {
  299. int ret, err = -EIO;
  300. struct inode *inode = mapping->host;
  301. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  302. loff_t start;
  303. struct page *last;
  304. /*
  305. * Use the nonblocking flag for the dlm code to avoid page
  306. * lock inversion, but don't bother with retrying.
  307. */
  308. ret = ocfs2_inode_lock_full(inode, NULL, 0, OCFS2_LOCK_NONBLOCK);
  309. if (ret)
  310. return err;
  311. if (down_read_trylock(&oi->ip_alloc_sem) == 0) {
  312. ocfs2_inode_unlock(inode, 0);
  313. return err;
  314. }
  315. /*
  316. * Don't bother with inline-data. There isn't anything
  317. * to read-ahead in that case anyway...
  318. */
  319. if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL)
  320. goto out_unlock;
  321. /*
  322. * Check whether a remote node truncated this file - we just
  323. * drop out in that case as it's not worth handling here.
  324. */
  325. last = list_entry(pages->prev, struct page, lru);
  326. start = (loff_t)last->index << PAGE_CACHE_SHIFT;
  327. if (start >= i_size_read(inode))
  328. goto out_unlock;
  329. err = mpage_readpages(mapping, pages, nr_pages, ocfs2_get_block);
  330. out_unlock:
  331. up_read(&oi->ip_alloc_sem);
  332. ocfs2_inode_unlock(inode, 0);
  333. return err;
  334. }
  335. /* Note: Because we don't support holes, our allocation has
  336. * already happened (allocation writes zeros to the file data)
  337. * so we don't have to worry about ordered writes in
  338. * ocfs2_writepage.
  339. *
  340. * ->writepage is called during the process of invalidating the page cache
  341. * during blocked lock processing. It can't block on any cluster locks
  342. * to during block mapping. It's relying on the fact that the block
  343. * mapping can't have disappeared under the dirty pages that it is
  344. * being asked to write back.
  345. */
  346. static int ocfs2_writepage(struct page *page, struct writeback_control *wbc)
  347. {
  348. int ret;
  349. mlog_entry("(0x%p)\n", page);
  350. ret = block_write_full_page(page, ocfs2_get_block, wbc);
  351. mlog_exit(ret);
  352. return ret;
  353. }
  354. /*
  355. * This is called from ocfs2_write_zero_page() which has handled it's
  356. * own cluster locking and has ensured allocation exists for those
  357. * blocks to be written.
  358. */
  359. int ocfs2_prepare_write_nolock(struct inode *inode, struct page *page,
  360. unsigned from, unsigned to)
  361. {
  362. int ret;
  363. ret = block_prepare_write(page, from, to, ocfs2_get_block);
  364. return ret;
  365. }
  366. /* Taken from ext3. We don't necessarily need the full blown
  367. * functionality yet, but IMHO it's better to cut and paste the whole
  368. * thing so we can avoid introducing our own bugs (and easily pick up
  369. * their fixes when they happen) --Mark */
  370. int walk_page_buffers( handle_t *handle,
  371. struct buffer_head *head,
  372. unsigned from,
  373. unsigned to,
  374. int *partial,
  375. int (*fn)( handle_t *handle,
  376. struct buffer_head *bh))
  377. {
  378. struct buffer_head *bh;
  379. unsigned block_start, block_end;
  380. unsigned blocksize = head->b_size;
  381. int err, ret = 0;
  382. struct buffer_head *next;
  383. for ( bh = head, block_start = 0;
  384. ret == 0 && (bh != head || !block_start);
  385. block_start = block_end, bh = next)
  386. {
  387. next = bh->b_this_page;
  388. block_end = block_start + blocksize;
  389. if (block_end <= from || block_start >= to) {
  390. if (partial && !buffer_uptodate(bh))
  391. *partial = 1;
  392. continue;
  393. }
  394. err = (*fn)(handle, bh);
  395. if (!ret)
  396. ret = err;
  397. }
  398. return ret;
  399. }
  400. handle_t *ocfs2_start_walk_page_trans(struct inode *inode,
  401. struct page *page,
  402. unsigned from,
  403. unsigned to)
  404. {
  405. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  406. handle_t *handle;
  407. int ret = 0;
  408. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  409. if (IS_ERR(handle)) {
  410. ret = -ENOMEM;
  411. mlog_errno(ret);
  412. goto out;
  413. }
  414. if (ocfs2_should_order_data(inode)) {
  415. ret = ocfs2_jbd2_file_inode(handle, inode);
  416. #ifdef CONFIG_OCFS2_COMPAT_JBD
  417. ret = walk_page_buffers(handle,
  418. page_buffers(page),
  419. from, to, NULL,
  420. ocfs2_journal_dirty_data);
  421. #endif
  422. if (ret < 0)
  423. mlog_errno(ret);
  424. }
  425. out:
  426. if (ret) {
  427. if (!IS_ERR(handle))
  428. ocfs2_commit_trans(osb, handle);
  429. handle = ERR_PTR(ret);
  430. }
  431. return handle;
  432. }
  433. static sector_t ocfs2_bmap(struct address_space *mapping, sector_t block)
  434. {
  435. sector_t status;
  436. u64 p_blkno = 0;
  437. int err = 0;
  438. struct inode *inode = mapping->host;
  439. mlog_entry("(block = %llu)\n", (unsigned long long)block);
  440. /* We don't need to lock journal system files, since they aren't
  441. * accessed concurrently from multiple nodes.
  442. */
  443. if (!INODE_JOURNAL(inode)) {
  444. err = ocfs2_inode_lock(inode, NULL, 0);
  445. if (err) {
  446. if (err != -ENOENT)
  447. mlog_errno(err);
  448. goto bail;
  449. }
  450. down_read(&OCFS2_I(inode)->ip_alloc_sem);
  451. }
  452. if (!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL))
  453. err = ocfs2_extent_map_get_blocks(inode, block, &p_blkno, NULL,
  454. NULL);
  455. if (!INODE_JOURNAL(inode)) {
  456. up_read(&OCFS2_I(inode)->ip_alloc_sem);
  457. ocfs2_inode_unlock(inode, 0);
  458. }
  459. if (err) {
  460. mlog(ML_ERROR, "get_blocks() failed, block = %llu\n",
  461. (unsigned long long)block);
  462. mlog_errno(err);
  463. goto bail;
  464. }
  465. bail:
  466. status = err ? 0 : p_blkno;
  467. mlog_exit((int)status);
  468. return status;
  469. }
  470. /*
  471. * TODO: Make this into a generic get_blocks function.
  472. *
  473. * From do_direct_io in direct-io.c:
  474. * "So what we do is to permit the ->get_blocks function to populate
  475. * bh.b_size with the size of IO which is permitted at this offset and
  476. * this i_blkbits."
  477. *
  478. * This function is called directly from get_more_blocks in direct-io.c.
  479. *
  480. * called like this: dio->get_blocks(dio->inode, fs_startblk,
  481. * fs_count, map_bh, dio->rw == WRITE);
  482. */
  483. static int ocfs2_direct_IO_get_blocks(struct inode *inode, sector_t iblock,
  484. struct buffer_head *bh_result, int create)
  485. {
  486. int ret;
  487. u64 p_blkno, inode_blocks, contig_blocks;
  488. unsigned int ext_flags;
  489. unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
  490. unsigned long max_blocks = bh_result->b_size >> inode->i_blkbits;
  491. /* This function won't even be called if the request isn't all
  492. * nicely aligned and of the right size, so there's no need
  493. * for us to check any of that. */
  494. inode_blocks = ocfs2_blocks_for_bytes(inode->i_sb, i_size_read(inode));
  495. /*
  496. * Any write past EOF is not allowed because we'd be extending.
  497. */
  498. if (create && (iblock + max_blocks) > inode_blocks) {
  499. ret = -EIO;
  500. goto bail;
  501. }
  502. /* This figures out the size of the next contiguous block, and
  503. * our logical offset */
  504. ret = ocfs2_extent_map_get_blocks(inode, iblock, &p_blkno,
  505. &contig_blocks, &ext_flags);
  506. if (ret) {
  507. mlog(ML_ERROR, "get_blocks() failed iblock=%llu\n",
  508. (unsigned long long)iblock);
  509. ret = -EIO;
  510. goto bail;
  511. }
  512. if (!ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb)) && !p_blkno && create) {
  513. ocfs2_error(inode->i_sb,
  514. "Inode %llu has a hole at block %llu\n",
  515. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  516. (unsigned long long)iblock);
  517. ret = -EROFS;
  518. goto bail;
  519. }
  520. /*
  521. * get_more_blocks() expects us to describe a hole by clearing
  522. * the mapped bit on bh_result().
  523. *
  524. * Consider an unwritten extent as a hole.
  525. */
  526. if (p_blkno && !(ext_flags & OCFS2_EXT_UNWRITTEN))
  527. map_bh(bh_result, inode->i_sb, p_blkno);
  528. else {
  529. /*
  530. * ocfs2_prepare_inode_for_write() should have caught
  531. * the case where we'd be filling a hole and triggered
  532. * a buffered write instead.
  533. */
  534. if (create) {
  535. ret = -EIO;
  536. mlog_errno(ret);
  537. goto bail;
  538. }
  539. clear_buffer_mapped(bh_result);
  540. }
  541. /* make sure we don't map more than max_blocks blocks here as
  542. that's all the kernel will handle at this point. */
  543. if (max_blocks < contig_blocks)
  544. contig_blocks = max_blocks;
  545. bh_result->b_size = contig_blocks << blocksize_bits;
  546. bail:
  547. return ret;
  548. }
  549. /*
  550. * ocfs2_dio_end_io is called by the dio core when a dio is finished. We're
  551. * particularly interested in the aio/dio case. Like the core uses
  552. * i_alloc_sem, we use the rw_lock DLM lock to protect io on one node from
  553. * truncation on another.
  554. */
  555. static void ocfs2_dio_end_io(struct kiocb *iocb,
  556. loff_t offset,
  557. ssize_t bytes,
  558. void *private)
  559. {
  560. struct inode *inode = iocb->ki_filp->f_path.dentry->d_inode;
  561. int level;
  562. /* this io's submitter should not have unlocked this before we could */
  563. BUG_ON(!ocfs2_iocb_is_rw_locked(iocb));
  564. ocfs2_iocb_clear_rw_locked(iocb);
  565. level = ocfs2_iocb_rw_locked_level(iocb);
  566. if (!level)
  567. up_read(&inode->i_alloc_sem);
  568. ocfs2_rw_unlock(inode, level);
  569. }
  570. /*
  571. * ocfs2_invalidatepage() and ocfs2_releasepage() are shamelessly stolen
  572. * from ext3. PageChecked() bits have been removed as OCFS2 does not
  573. * do journalled data.
  574. */
  575. static void ocfs2_invalidatepage(struct page *page, unsigned long offset)
  576. {
  577. journal_t *journal = OCFS2_SB(page->mapping->host->i_sb)->journal->j_journal;
  578. jbd2_journal_invalidatepage(journal, page, offset);
  579. }
  580. static int ocfs2_releasepage(struct page *page, gfp_t wait)
  581. {
  582. journal_t *journal = OCFS2_SB(page->mapping->host->i_sb)->journal->j_journal;
  583. if (!page_has_buffers(page))
  584. return 0;
  585. return jbd2_journal_try_to_free_buffers(journal, page, wait);
  586. }
  587. static ssize_t ocfs2_direct_IO(int rw,
  588. struct kiocb *iocb,
  589. const struct iovec *iov,
  590. loff_t offset,
  591. unsigned long nr_segs)
  592. {
  593. struct file *file = iocb->ki_filp;
  594. struct inode *inode = file->f_path.dentry->d_inode->i_mapping->host;
  595. int ret;
  596. mlog_entry_void();
  597. /*
  598. * Fallback to buffered I/O if we see an inode without
  599. * extents.
  600. */
  601. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
  602. return 0;
  603. ret = blockdev_direct_IO_no_locking(rw, iocb, inode,
  604. inode->i_sb->s_bdev, iov, offset,
  605. nr_segs,
  606. ocfs2_direct_IO_get_blocks,
  607. ocfs2_dio_end_io);
  608. mlog_exit(ret);
  609. return ret;
  610. }
  611. static void ocfs2_figure_cluster_boundaries(struct ocfs2_super *osb,
  612. u32 cpos,
  613. unsigned int *start,
  614. unsigned int *end)
  615. {
  616. unsigned int cluster_start = 0, cluster_end = PAGE_CACHE_SIZE;
  617. if (unlikely(PAGE_CACHE_SHIFT > osb->s_clustersize_bits)) {
  618. unsigned int cpp;
  619. cpp = 1 << (PAGE_CACHE_SHIFT - osb->s_clustersize_bits);
  620. cluster_start = cpos % cpp;
  621. cluster_start = cluster_start << osb->s_clustersize_bits;
  622. cluster_end = cluster_start + osb->s_clustersize;
  623. }
  624. BUG_ON(cluster_start > PAGE_SIZE);
  625. BUG_ON(cluster_end > PAGE_SIZE);
  626. if (start)
  627. *start = cluster_start;
  628. if (end)
  629. *end = cluster_end;
  630. }
  631. /*
  632. * 'from' and 'to' are the region in the page to avoid zeroing.
  633. *
  634. * If pagesize > clustersize, this function will avoid zeroing outside
  635. * of the cluster boundary.
  636. *
  637. * from == to == 0 is code for "zero the entire cluster region"
  638. */
  639. static void ocfs2_clear_page_regions(struct page *page,
  640. struct ocfs2_super *osb, u32 cpos,
  641. unsigned from, unsigned to)
  642. {
  643. void *kaddr;
  644. unsigned int cluster_start, cluster_end;
  645. ocfs2_figure_cluster_boundaries(osb, cpos, &cluster_start, &cluster_end);
  646. kaddr = kmap_atomic(page, KM_USER0);
  647. if (from || to) {
  648. if (from > cluster_start)
  649. memset(kaddr + cluster_start, 0, from - cluster_start);
  650. if (to < cluster_end)
  651. memset(kaddr + to, 0, cluster_end - to);
  652. } else {
  653. memset(kaddr + cluster_start, 0, cluster_end - cluster_start);
  654. }
  655. kunmap_atomic(kaddr, KM_USER0);
  656. }
  657. /*
  658. * Nonsparse file systems fully allocate before we get to the write
  659. * code. This prevents ocfs2_write() from tagging the write as an
  660. * allocating one, which means ocfs2_map_page_blocks() might try to
  661. * read-in the blocks at the tail of our file. Avoid reading them by
  662. * testing i_size against each block offset.
  663. */
  664. static int ocfs2_should_read_blk(struct inode *inode, struct page *page,
  665. unsigned int block_start)
  666. {
  667. u64 offset = page_offset(page) + block_start;
  668. if (ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb)))
  669. return 1;
  670. if (i_size_read(inode) > offset)
  671. return 1;
  672. return 0;
  673. }
  674. /*
  675. * Some of this taken from block_prepare_write(). We already have our
  676. * mapping by now though, and the entire write will be allocating or
  677. * it won't, so not much need to use BH_New.
  678. *
  679. * This will also skip zeroing, which is handled externally.
  680. */
  681. int ocfs2_map_page_blocks(struct page *page, u64 *p_blkno,
  682. struct inode *inode, unsigned int from,
  683. unsigned int to, int new)
  684. {
  685. int ret = 0;
  686. struct buffer_head *head, *bh, *wait[2], **wait_bh = wait;
  687. unsigned int block_end, block_start;
  688. unsigned int bsize = 1 << inode->i_blkbits;
  689. if (!page_has_buffers(page))
  690. create_empty_buffers(page, bsize, 0);
  691. head = page_buffers(page);
  692. for (bh = head, block_start = 0; bh != head || !block_start;
  693. bh = bh->b_this_page, block_start += bsize) {
  694. block_end = block_start + bsize;
  695. clear_buffer_new(bh);
  696. /*
  697. * Ignore blocks outside of our i/o range -
  698. * they may belong to unallocated clusters.
  699. */
  700. if (block_start >= to || block_end <= from) {
  701. if (PageUptodate(page))
  702. set_buffer_uptodate(bh);
  703. continue;
  704. }
  705. /*
  706. * For an allocating write with cluster size >= page
  707. * size, we always write the entire page.
  708. */
  709. if (new)
  710. set_buffer_new(bh);
  711. if (!buffer_mapped(bh)) {
  712. map_bh(bh, inode->i_sb, *p_blkno);
  713. unmap_underlying_metadata(bh->b_bdev, bh->b_blocknr);
  714. }
  715. if (PageUptodate(page)) {
  716. if (!buffer_uptodate(bh))
  717. set_buffer_uptodate(bh);
  718. } else if (!buffer_uptodate(bh) && !buffer_delay(bh) &&
  719. !buffer_new(bh) &&
  720. ocfs2_should_read_blk(inode, page, block_start) &&
  721. (block_start < from || block_end > to)) {
  722. ll_rw_block(READ, 1, &bh);
  723. *wait_bh++=bh;
  724. }
  725. *p_blkno = *p_blkno + 1;
  726. }
  727. /*
  728. * If we issued read requests - let them complete.
  729. */
  730. while(wait_bh > wait) {
  731. wait_on_buffer(*--wait_bh);
  732. if (!buffer_uptodate(*wait_bh))
  733. ret = -EIO;
  734. }
  735. if (ret == 0 || !new)
  736. return ret;
  737. /*
  738. * If we get -EIO above, zero out any newly allocated blocks
  739. * to avoid exposing stale data.
  740. */
  741. bh = head;
  742. block_start = 0;
  743. do {
  744. block_end = block_start + bsize;
  745. if (block_end <= from)
  746. goto next_bh;
  747. if (block_start >= to)
  748. break;
  749. zero_user(page, block_start, bh->b_size);
  750. set_buffer_uptodate(bh);
  751. mark_buffer_dirty(bh);
  752. next_bh:
  753. block_start = block_end;
  754. bh = bh->b_this_page;
  755. } while (bh != head);
  756. return ret;
  757. }
  758. #if (PAGE_CACHE_SIZE >= OCFS2_MAX_CLUSTERSIZE)
  759. #define OCFS2_MAX_CTXT_PAGES 1
  760. #else
  761. #define OCFS2_MAX_CTXT_PAGES (OCFS2_MAX_CLUSTERSIZE / PAGE_CACHE_SIZE)
  762. #endif
  763. #define OCFS2_MAX_CLUSTERS_PER_PAGE (PAGE_CACHE_SIZE / OCFS2_MIN_CLUSTERSIZE)
  764. /*
  765. * Describe the state of a single cluster to be written to.
  766. */
  767. struct ocfs2_write_cluster_desc {
  768. u32 c_cpos;
  769. u32 c_phys;
  770. /*
  771. * Give this a unique field because c_phys eventually gets
  772. * filled.
  773. */
  774. unsigned c_new;
  775. unsigned c_unwritten;
  776. };
  777. static inline int ocfs2_should_zero_cluster(struct ocfs2_write_cluster_desc *d)
  778. {
  779. return d->c_new || d->c_unwritten;
  780. }
  781. struct ocfs2_write_ctxt {
  782. /* Logical cluster position / len of write */
  783. u32 w_cpos;
  784. u32 w_clen;
  785. struct ocfs2_write_cluster_desc w_desc[OCFS2_MAX_CLUSTERS_PER_PAGE];
  786. /*
  787. * This is true if page_size > cluster_size.
  788. *
  789. * It triggers a set of special cases during write which might
  790. * have to deal with allocating writes to partial pages.
  791. */
  792. unsigned int w_large_pages;
  793. /*
  794. * Pages involved in this write.
  795. *
  796. * w_target_page is the page being written to by the user.
  797. *
  798. * w_pages is an array of pages which always contains
  799. * w_target_page, and in the case of an allocating write with
  800. * page_size < cluster size, it will contain zero'd and mapped
  801. * pages adjacent to w_target_page which need to be written
  802. * out in so that future reads from that region will get
  803. * zero's.
  804. */
  805. struct page *w_pages[OCFS2_MAX_CTXT_PAGES];
  806. unsigned int w_num_pages;
  807. struct page *w_target_page;
  808. /*
  809. * ocfs2_write_end() uses this to know what the real range to
  810. * write in the target should be.
  811. */
  812. unsigned int w_target_from;
  813. unsigned int w_target_to;
  814. /*
  815. * We could use journal_current_handle() but this is cleaner,
  816. * IMHO -Mark
  817. */
  818. handle_t *w_handle;
  819. struct buffer_head *w_di_bh;
  820. struct ocfs2_cached_dealloc_ctxt w_dealloc;
  821. };
  822. void ocfs2_unlock_and_free_pages(struct page **pages, int num_pages)
  823. {
  824. int i;
  825. for(i = 0; i < num_pages; i++) {
  826. if (pages[i]) {
  827. unlock_page(pages[i]);
  828. mark_page_accessed(pages[i]);
  829. page_cache_release(pages[i]);
  830. }
  831. }
  832. }
  833. static void ocfs2_free_write_ctxt(struct ocfs2_write_ctxt *wc)
  834. {
  835. ocfs2_unlock_and_free_pages(wc->w_pages, wc->w_num_pages);
  836. brelse(wc->w_di_bh);
  837. kfree(wc);
  838. }
  839. static int ocfs2_alloc_write_ctxt(struct ocfs2_write_ctxt **wcp,
  840. struct ocfs2_super *osb, loff_t pos,
  841. unsigned len, struct buffer_head *di_bh)
  842. {
  843. u32 cend;
  844. struct ocfs2_write_ctxt *wc;
  845. wc = kzalloc(sizeof(struct ocfs2_write_ctxt), GFP_NOFS);
  846. if (!wc)
  847. return -ENOMEM;
  848. wc->w_cpos = pos >> osb->s_clustersize_bits;
  849. cend = (pos + len - 1) >> osb->s_clustersize_bits;
  850. wc->w_clen = cend - wc->w_cpos + 1;
  851. get_bh(di_bh);
  852. wc->w_di_bh = di_bh;
  853. if (unlikely(PAGE_CACHE_SHIFT > osb->s_clustersize_bits))
  854. wc->w_large_pages = 1;
  855. else
  856. wc->w_large_pages = 0;
  857. ocfs2_init_dealloc_ctxt(&wc->w_dealloc);
  858. *wcp = wc;
  859. return 0;
  860. }
  861. /*
  862. * If a page has any new buffers, zero them out here, and mark them uptodate
  863. * and dirty so they'll be written out (in order to prevent uninitialised
  864. * block data from leaking). And clear the new bit.
  865. */
  866. static void ocfs2_zero_new_buffers(struct page *page, unsigned from, unsigned to)
  867. {
  868. unsigned int block_start, block_end;
  869. struct buffer_head *head, *bh;
  870. BUG_ON(!PageLocked(page));
  871. if (!page_has_buffers(page))
  872. return;
  873. bh = head = page_buffers(page);
  874. block_start = 0;
  875. do {
  876. block_end = block_start + bh->b_size;
  877. if (buffer_new(bh)) {
  878. if (block_end > from && block_start < to) {
  879. if (!PageUptodate(page)) {
  880. unsigned start, end;
  881. start = max(from, block_start);
  882. end = min(to, block_end);
  883. zero_user_segment(page, start, end);
  884. set_buffer_uptodate(bh);
  885. }
  886. clear_buffer_new(bh);
  887. mark_buffer_dirty(bh);
  888. }
  889. }
  890. block_start = block_end;
  891. bh = bh->b_this_page;
  892. } while (bh != head);
  893. }
  894. /*
  895. * Only called when we have a failure during allocating write to write
  896. * zero's to the newly allocated region.
  897. */
  898. static void ocfs2_write_failure(struct inode *inode,
  899. struct ocfs2_write_ctxt *wc,
  900. loff_t user_pos, unsigned user_len)
  901. {
  902. int i;
  903. unsigned from = user_pos & (PAGE_CACHE_SIZE - 1),
  904. to = user_pos + user_len;
  905. struct page *tmppage;
  906. ocfs2_zero_new_buffers(wc->w_target_page, from, to);
  907. for(i = 0; i < wc->w_num_pages; i++) {
  908. tmppage = wc->w_pages[i];
  909. if (page_has_buffers(tmppage)) {
  910. if (ocfs2_should_order_data(inode)) {
  911. ocfs2_jbd2_file_inode(wc->w_handle, inode);
  912. #ifdef CONFIG_OCFS2_COMPAT_JBD
  913. walk_page_buffers(wc->w_handle,
  914. page_buffers(tmppage),
  915. from, to, NULL,
  916. ocfs2_journal_dirty_data);
  917. #endif
  918. }
  919. block_commit_write(tmppage, from, to);
  920. }
  921. }
  922. }
  923. static int ocfs2_prepare_page_for_write(struct inode *inode, u64 *p_blkno,
  924. struct ocfs2_write_ctxt *wc,
  925. struct page *page, u32 cpos,
  926. loff_t user_pos, unsigned user_len,
  927. int new)
  928. {
  929. int ret;
  930. unsigned int map_from = 0, map_to = 0;
  931. unsigned int cluster_start, cluster_end;
  932. unsigned int user_data_from = 0, user_data_to = 0;
  933. ocfs2_figure_cluster_boundaries(OCFS2_SB(inode->i_sb), cpos,
  934. &cluster_start, &cluster_end);
  935. if (page == wc->w_target_page) {
  936. map_from = user_pos & (PAGE_CACHE_SIZE - 1);
  937. map_to = map_from + user_len;
  938. if (new)
  939. ret = ocfs2_map_page_blocks(page, p_blkno, inode,
  940. cluster_start, cluster_end,
  941. new);
  942. else
  943. ret = ocfs2_map_page_blocks(page, p_blkno, inode,
  944. map_from, map_to, new);
  945. if (ret) {
  946. mlog_errno(ret);
  947. goto out;
  948. }
  949. user_data_from = map_from;
  950. user_data_to = map_to;
  951. if (new) {
  952. map_from = cluster_start;
  953. map_to = cluster_end;
  954. }
  955. } else {
  956. /*
  957. * If we haven't allocated the new page yet, we
  958. * shouldn't be writing it out without copying user
  959. * data. This is likely a math error from the caller.
  960. */
  961. BUG_ON(!new);
  962. map_from = cluster_start;
  963. map_to = cluster_end;
  964. ret = ocfs2_map_page_blocks(page, p_blkno, inode,
  965. cluster_start, cluster_end, new);
  966. if (ret) {
  967. mlog_errno(ret);
  968. goto out;
  969. }
  970. }
  971. /*
  972. * Parts of newly allocated pages need to be zero'd.
  973. *
  974. * Above, we have also rewritten 'to' and 'from' - as far as
  975. * the rest of the function is concerned, the entire cluster
  976. * range inside of a page needs to be written.
  977. *
  978. * We can skip this if the page is up to date - it's already
  979. * been zero'd from being read in as a hole.
  980. */
  981. if (new && !PageUptodate(page))
  982. ocfs2_clear_page_regions(page, OCFS2_SB(inode->i_sb),
  983. cpos, user_data_from, user_data_to);
  984. flush_dcache_page(page);
  985. out:
  986. return ret;
  987. }
  988. /*
  989. * This function will only grab one clusters worth of pages.
  990. */
  991. static int ocfs2_grab_pages_for_write(struct address_space *mapping,
  992. struct ocfs2_write_ctxt *wc,
  993. u32 cpos, loff_t user_pos, int new,
  994. struct page *mmap_page)
  995. {
  996. int ret = 0, i;
  997. unsigned long start, target_index, index;
  998. struct inode *inode = mapping->host;
  999. target_index = user_pos >> PAGE_CACHE_SHIFT;
  1000. /*
  1001. * Figure out how many pages we'll be manipulating here. For
  1002. * non allocating write, we just change the one
  1003. * page. Otherwise, we'll need a whole clusters worth.
  1004. */
  1005. if (new) {
  1006. wc->w_num_pages = ocfs2_pages_per_cluster(inode->i_sb);
  1007. start = ocfs2_align_clusters_to_page_index(inode->i_sb, cpos);
  1008. } else {
  1009. wc->w_num_pages = 1;
  1010. start = target_index;
  1011. }
  1012. for(i = 0; i < wc->w_num_pages; i++) {
  1013. index = start + i;
  1014. if (index == target_index && mmap_page) {
  1015. /*
  1016. * ocfs2_pagemkwrite() is a little different
  1017. * and wants us to directly use the page
  1018. * passed in.
  1019. */
  1020. lock_page(mmap_page);
  1021. if (mmap_page->mapping != mapping) {
  1022. unlock_page(mmap_page);
  1023. /*
  1024. * Sanity check - the locking in
  1025. * ocfs2_pagemkwrite() should ensure
  1026. * that this code doesn't trigger.
  1027. */
  1028. ret = -EINVAL;
  1029. mlog_errno(ret);
  1030. goto out;
  1031. }
  1032. page_cache_get(mmap_page);
  1033. wc->w_pages[i] = mmap_page;
  1034. } else {
  1035. wc->w_pages[i] = find_or_create_page(mapping, index,
  1036. GFP_NOFS);
  1037. if (!wc->w_pages[i]) {
  1038. ret = -ENOMEM;
  1039. mlog_errno(ret);
  1040. goto out;
  1041. }
  1042. }
  1043. if (index == target_index)
  1044. wc->w_target_page = wc->w_pages[i];
  1045. }
  1046. out:
  1047. return ret;
  1048. }
  1049. /*
  1050. * Prepare a single cluster for write one cluster into the file.
  1051. */
  1052. static int ocfs2_write_cluster(struct address_space *mapping,
  1053. u32 phys, unsigned int unwritten,
  1054. struct ocfs2_alloc_context *data_ac,
  1055. struct ocfs2_alloc_context *meta_ac,
  1056. struct ocfs2_write_ctxt *wc, u32 cpos,
  1057. loff_t user_pos, unsigned user_len)
  1058. {
  1059. int ret, i, new, should_zero = 0;
  1060. u64 v_blkno, p_blkno;
  1061. struct inode *inode = mapping->host;
  1062. struct ocfs2_extent_tree et;
  1063. new = phys == 0 ? 1 : 0;
  1064. if (new || unwritten)
  1065. should_zero = 1;
  1066. if (new) {
  1067. u32 tmp_pos;
  1068. /*
  1069. * This is safe to call with the page locks - it won't take
  1070. * any additional semaphores or cluster locks.
  1071. */
  1072. tmp_pos = cpos;
  1073. ret = ocfs2_add_inode_data(OCFS2_SB(inode->i_sb), inode,
  1074. &tmp_pos, 1, 0, wc->w_di_bh,
  1075. wc->w_handle, data_ac,
  1076. meta_ac, NULL);
  1077. /*
  1078. * This shouldn't happen because we must have already
  1079. * calculated the correct meta data allocation required. The
  1080. * internal tree allocation code should know how to increase
  1081. * transaction credits itself.
  1082. *
  1083. * If need be, we could handle -EAGAIN for a
  1084. * RESTART_TRANS here.
  1085. */
  1086. mlog_bug_on_msg(ret == -EAGAIN,
  1087. "Inode %llu: EAGAIN return during allocation.\n",
  1088. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  1089. if (ret < 0) {
  1090. mlog_errno(ret);
  1091. goto out;
  1092. }
  1093. } else if (unwritten) {
  1094. ocfs2_init_dinode_extent_tree(&et, inode, wc->w_di_bh);
  1095. ret = ocfs2_mark_extent_written(inode, &et,
  1096. wc->w_handle, cpos, 1, phys,
  1097. meta_ac, &wc->w_dealloc);
  1098. if (ret < 0) {
  1099. mlog_errno(ret);
  1100. goto out;
  1101. }
  1102. }
  1103. if (should_zero)
  1104. v_blkno = ocfs2_clusters_to_blocks(inode->i_sb, cpos);
  1105. else
  1106. v_blkno = user_pos >> inode->i_sb->s_blocksize_bits;
  1107. /*
  1108. * The only reason this should fail is due to an inability to
  1109. * find the extent added.
  1110. */
  1111. ret = ocfs2_extent_map_get_blocks(inode, v_blkno, &p_blkno, NULL,
  1112. NULL);
  1113. if (ret < 0) {
  1114. ocfs2_error(inode->i_sb, "Corrupting extend for inode %llu, "
  1115. "at logical block %llu",
  1116. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  1117. (unsigned long long)v_blkno);
  1118. goto out;
  1119. }
  1120. BUG_ON(p_blkno == 0);
  1121. for(i = 0; i < wc->w_num_pages; i++) {
  1122. int tmpret;
  1123. tmpret = ocfs2_prepare_page_for_write(inode, &p_blkno, wc,
  1124. wc->w_pages[i], cpos,
  1125. user_pos, user_len,
  1126. should_zero);
  1127. if (tmpret) {
  1128. mlog_errno(tmpret);
  1129. if (ret == 0)
  1130. tmpret = ret;
  1131. }
  1132. }
  1133. /*
  1134. * We only have cleanup to do in case of allocating write.
  1135. */
  1136. if (ret && new)
  1137. ocfs2_write_failure(inode, wc, user_pos, user_len);
  1138. out:
  1139. return ret;
  1140. }
  1141. static int ocfs2_write_cluster_by_desc(struct address_space *mapping,
  1142. struct ocfs2_alloc_context *data_ac,
  1143. struct ocfs2_alloc_context *meta_ac,
  1144. struct ocfs2_write_ctxt *wc,
  1145. loff_t pos, unsigned len)
  1146. {
  1147. int ret, i;
  1148. loff_t cluster_off;
  1149. unsigned int local_len = len;
  1150. struct ocfs2_write_cluster_desc *desc;
  1151. struct ocfs2_super *osb = OCFS2_SB(mapping->host->i_sb);
  1152. for (i = 0; i < wc->w_clen; i++) {
  1153. desc = &wc->w_desc[i];
  1154. /*
  1155. * We have to make sure that the total write passed in
  1156. * doesn't extend past a single cluster.
  1157. */
  1158. local_len = len;
  1159. cluster_off = pos & (osb->s_clustersize - 1);
  1160. if ((cluster_off + local_len) > osb->s_clustersize)
  1161. local_len = osb->s_clustersize - cluster_off;
  1162. ret = ocfs2_write_cluster(mapping, desc->c_phys,
  1163. desc->c_unwritten, data_ac, meta_ac,
  1164. wc, desc->c_cpos, pos, local_len);
  1165. if (ret) {
  1166. mlog_errno(ret);
  1167. goto out;
  1168. }
  1169. len -= local_len;
  1170. pos += local_len;
  1171. }
  1172. ret = 0;
  1173. out:
  1174. return ret;
  1175. }
  1176. /*
  1177. * ocfs2_write_end() wants to know which parts of the target page it
  1178. * should complete the write on. It's easiest to compute them ahead of
  1179. * time when a more complete view of the write is available.
  1180. */
  1181. static void ocfs2_set_target_boundaries(struct ocfs2_super *osb,
  1182. struct ocfs2_write_ctxt *wc,
  1183. loff_t pos, unsigned len, int alloc)
  1184. {
  1185. struct ocfs2_write_cluster_desc *desc;
  1186. wc->w_target_from = pos & (PAGE_CACHE_SIZE - 1);
  1187. wc->w_target_to = wc->w_target_from + len;
  1188. if (alloc == 0)
  1189. return;
  1190. /*
  1191. * Allocating write - we may have different boundaries based
  1192. * on page size and cluster size.
  1193. *
  1194. * NOTE: We can no longer compute one value from the other as
  1195. * the actual write length and user provided length may be
  1196. * different.
  1197. */
  1198. if (wc->w_large_pages) {
  1199. /*
  1200. * We only care about the 1st and last cluster within
  1201. * our range and whether they should be zero'd or not. Either
  1202. * value may be extended out to the start/end of a
  1203. * newly allocated cluster.
  1204. */
  1205. desc = &wc->w_desc[0];
  1206. if (ocfs2_should_zero_cluster(desc))
  1207. ocfs2_figure_cluster_boundaries(osb,
  1208. desc->c_cpos,
  1209. &wc->w_target_from,
  1210. NULL);
  1211. desc = &wc->w_desc[wc->w_clen - 1];
  1212. if (ocfs2_should_zero_cluster(desc))
  1213. ocfs2_figure_cluster_boundaries(osb,
  1214. desc->c_cpos,
  1215. NULL,
  1216. &wc->w_target_to);
  1217. } else {
  1218. wc->w_target_from = 0;
  1219. wc->w_target_to = PAGE_CACHE_SIZE;
  1220. }
  1221. }
  1222. /*
  1223. * Populate each single-cluster write descriptor in the write context
  1224. * with information about the i/o to be done.
  1225. *
  1226. * Returns the number of clusters that will have to be allocated, as
  1227. * well as a worst case estimate of the number of extent records that
  1228. * would have to be created during a write to an unwritten region.
  1229. */
  1230. static int ocfs2_populate_write_desc(struct inode *inode,
  1231. struct ocfs2_write_ctxt *wc,
  1232. unsigned int *clusters_to_alloc,
  1233. unsigned int *extents_to_split)
  1234. {
  1235. int ret;
  1236. struct ocfs2_write_cluster_desc *desc;
  1237. unsigned int num_clusters = 0;
  1238. unsigned int ext_flags = 0;
  1239. u32 phys = 0;
  1240. int i;
  1241. *clusters_to_alloc = 0;
  1242. *extents_to_split = 0;
  1243. for (i = 0; i < wc->w_clen; i++) {
  1244. desc = &wc->w_desc[i];
  1245. desc->c_cpos = wc->w_cpos + i;
  1246. if (num_clusters == 0) {
  1247. /*
  1248. * Need to look up the next extent record.
  1249. */
  1250. ret = ocfs2_get_clusters(inode, desc->c_cpos, &phys,
  1251. &num_clusters, &ext_flags);
  1252. if (ret) {
  1253. mlog_errno(ret);
  1254. goto out;
  1255. }
  1256. /*
  1257. * Assume worst case - that we're writing in
  1258. * the middle of the extent.
  1259. *
  1260. * We can assume that the write proceeds from
  1261. * left to right, in which case the extent
  1262. * insert code is smart enough to coalesce the
  1263. * next splits into the previous records created.
  1264. */
  1265. if (ext_flags & OCFS2_EXT_UNWRITTEN)
  1266. *extents_to_split = *extents_to_split + 2;
  1267. } else if (phys) {
  1268. /*
  1269. * Only increment phys if it doesn't describe
  1270. * a hole.
  1271. */
  1272. phys++;
  1273. }
  1274. desc->c_phys = phys;
  1275. if (phys == 0) {
  1276. desc->c_new = 1;
  1277. *clusters_to_alloc = *clusters_to_alloc + 1;
  1278. }
  1279. if (ext_flags & OCFS2_EXT_UNWRITTEN)
  1280. desc->c_unwritten = 1;
  1281. num_clusters--;
  1282. }
  1283. ret = 0;
  1284. out:
  1285. return ret;
  1286. }
  1287. static int ocfs2_write_begin_inline(struct address_space *mapping,
  1288. struct inode *inode,
  1289. struct ocfs2_write_ctxt *wc)
  1290. {
  1291. int ret;
  1292. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1293. struct page *page;
  1294. handle_t *handle;
  1295. struct ocfs2_dinode *di = (struct ocfs2_dinode *)wc->w_di_bh->b_data;
  1296. page = find_or_create_page(mapping, 0, GFP_NOFS);
  1297. if (!page) {
  1298. ret = -ENOMEM;
  1299. mlog_errno(ret);
  1300. goto out;
  1301. }
  1302. /*
  1303. * If we don't set w_num_pages then this page won't get unlocked
  1304. * and freed on cleanup of the write context.
  1305. */
  1306. wc->w_pages[0] = wc->w_target_page = page;
  1307. wc->w_num_pages = 1;
  1308. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  1309. if (IS_ERR(handle)) {
  1310. ret = PTR_ERR(handle);
  1311. mlog_errno(ret);
  1312. goto out;
  1313. }
  1314. ret = ocfs2_journal_access(handle, inode, wc->w_di_bh,
  1315. OCFS2_JOURNAL_ACCESS_WRITE);
  1316. if (ret) {
  1317. ocfs2_commit_trans(osb, handle);
  1318. mlog_errno(ret);
  1319. goto out;
  1320. }
  1321. if (!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL))
  1322. ocfs2_set_inode_data_inline(inode, di);
  1323. if (!PageUptodate(page)) {
  1324. ret = ocfs2_read_inline_data(inode, page, wc->w_di_bh);
  1325. if (ret) {
  1326. ocfs2_commit_trans(osb, handle);
  1327. goto out;
  1328. }
  1329. }
  1330. wc->w_handle = handle;
  1331. out:
  1332. return ret;
  1333. }
  1334. int ocfs2_size_fits_inline_data(struct buffer_head *di_bh, u64 new_size)
  1335. {
  1336. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  1337. if (new_size <= le16_to_cpu(di->id2.i_data.id_count))
  1338. return 1;
  1339. return 0;
  1340. }
  1341. static int ocfs2_try_to_write_inline_data(struct address_space *mapping,
  1342. struct inode *inode, loff_t pos,
  1343. unsigned len, struct page *mmap_page,
  1344. struct ocfs2_write_ctxt *wc)
  1345. {
  1346. int ret, written = 0;
  1347. loff_t end = pos + len;
  1348. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  1349. mlog(0, "Inode %llu, write of %u bytes at off %llu. features: 0x%x\n",
  1350. (unsigned long long)oi->ip_blkno, len, (unsigned long long)pos,
  1351. oi->ip_dyn_features);
  1352. /*
  1353. * Handle inodes which already have inline data 1st.
  1354. */
  1355. if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  1356. if (mmap_page == NULL &&
  1357. ocfs2_size_fits_inline_data(wc->w_di_bh, end))
  1358. goto do_inline_write;
  1359. /*
  1360. * The write won't fit - we have to give this inode an
  1361. * inline extent list now.
  1362. */
  1363. ret = ocfs2_convert_inline_data_to_extents(inode, wc->w_di_bh);
  1364. if (ret)
  1365. mlog_errno(ret);
  1366. goto out;
  1367. }
  1368. /*
  1369. * Check whether the inode can accept inline data.
  1370. */
  1371. if (oi->ip_clusters != 0 || i_size_read(inode) != 0)
  1372. return 0;
  1373. /*
  1374. * Check whether the write can fit.
  1375. */
  1376. if (mmap_page || end > ocfs2_max_inline_data(inode->i_sb))
  1377. return 0;
  1378. do_inline_write:
  1379. ret = ocfs2_write_begin_inline(mapping, inode, wc);
  1380. if (ret) {
  1381. mlog_errno(ret);
  1382. goto out;
  1383. }
  1384. /*
  1385. * This signals to the caller that the data can be written
  1386. * inline.
  1387. */
  1388. written = 1;
  1389. out:
  1390. return written ? written : ret;
  1391. }
  1392. /*
  1393. * This function only does anything for file systems which can't
  1394. * handle sparse files.
  1395. *
  1396. * What we want to do here is fill in any hole between the current end
  1397. * of allocation and the end of our write. That way the rest of the
  1398. * write path can treat it as an non-allocating write, which has no
  1399. * special case code for sparse/nonsparse files.
  1400. */
  1401. static int ocfs2_expand_nonsparse_inode(struct inode *inode, loff_t pos,
  1402. unsigned len,
  1403. struct ocfs2_write_ctxt *wc)
  1404. {
  1405. int ret;
  1406. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1407. loff_t newsize = pos + len;
  1408. if (ocfs2_sparse_alloc(osb))
  1409. return 0;
  1410. if (newsize <= i_size_read(inode))
  1411. return 0;
  1412. ret = ocfs2_extend_no_holes(inode, newsize, newsize - len);
  1413. if (ret)
  1414. mlog_errno(ret);
  1415. return ret;
  1416. }
  1417. int ocfs2_write_begin_nolock(struct address_space *mapping,
  1418. loff_t pos, unsigned len, unsigned flags,
  1419. struct page **pagep, void **fsdata,
  1420. struct buffer_head *di_bh, struct page *mmap_page)
  1421. {
  1422. int ret, credits = OCFS2_INODE_UPDATE_CREDITS;
  1423. unsigned int clusters_to_alloc, extents_to_split;
  1424. struct ocfs2_write_ctxt *wc;
  1425. struct inode *inode = mapping->host;
  1426. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1427. struct ocfs2_dinode *di;
  1428. struct ocfs2_alloc_context *data_ac = NULL;
  1429. struct ocfs2_alloc_context *meta_ac = NULL;
  1430. handle_t *handle;
  1431. struct ocfs2_extent_tree et;
  1432. ret = ocfs2_alloc_write_ctxt(&wc, osb, pos, len, di_bh);
  1433. if (ret) {
  1434. mlog_errno(ret);
  1435. return ret;
  1436. }
  1437. if (ocfs2_supports_inline_data(osb)) {
  1438. ret = ocfs2_try_to_write_inline_data(mapping, inode, pos, len,
  1439. mmap_page, wc);
  1440. if (ret == 1) {
  1441. ret = 0;
  1442. goto success;
  1443. }
  1444. if (ret < 0) {
  1445. mlog_errno(ret);
  1446. goto out;
  1447. }
  1448. }
  1449. ret = ocfs2_expand_nonsparse_inode(inode, pos, len, wc);
  1450. if (ret) {
  1451. mlog_errno(ret);
  1452. goto out;
  1453. }
  1454. ret = ocfs2_populate_write_desc(inode, wc, &clusters_to_alloc,
  1455. &extents_to_split);
  1456. if (ret) {
  1457. mlog_errno(ret);
  1458. goto out;
  1459. }
  1460. di = (struct ocfs2_dinode *)wc->w_di_bh->b_data;
  1461. /*
  1462. * We set w_target_from, w_target_to here so that
  1463. * ocfs2_write_end() knows which range in the target page to
  1464. * write out. An allocation requires that we write the entire
  1465. * cluster range.
  1466. */
  1467. if (clusters_to_alloc || extents_to_split) {
  1468. /*
  1469. * XXX: We are stretching the limits of
  1470. * ocfs2_lock_allocators(). It greatly over-estimates
  1471. * the work to be done.
  1472. */
  1473. mlog(0, "extend inode %llu, i_size = %lld, di->i_clusters = %u,"
  1474. " clusters_to_add = %u, extents_to_split = %u\n",
  1475. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  1476. (long long)i_size_read(inode), le32_to_cpu(di->i_clusters),
  1477. clusters_to_alloc, extents_to_split);
  1478. ocfs2_init_dinode_extent_tree(&et, inode, wc->w_di_bh);
  1479. ret = ocfs2_lock_allocators(inode, &et,
  1480. clusters_to_alloc, extents_to_split,
  1481. &data_ac, &meta_ac);
  1482. if (ret) {
  1483. mlog_errno(ret);
  1484. goto out;
  1485. }
  1486. credits = ocfs2_calc_extend_credits(inode->i_sb,
  1487. &di->id2.i_list,
  1488. clusters_to_alloc);
  1489. }
  1490. ocfs2_set_target_boundaries(osb, wc, pos, len,
  1491. clusters_to_alloc + extents_to_split);
  1492. handle = ocfs2_start_trans(osb, credits);
  1493. if (IS_ERR(handle)) {
  1494. ret = PTR_ERR(handle);
  1495. mlog_errno(ret);
  1496. goto out;
  1497. }
  1498. wc->w_handle = handle;
  1499. /*
  1500. * We don't want this to fail in ocfs2_write_end(), so do it
  1501. * here.
  1502. */
  1503. ret = ocfs2_journal_access(handle, inode, wc->w_di_bh,
  1504. OCFS2_JOURNAL_ACCESS_WRITE);
  1505. if (ret) {
  1506. mlog_errno(ret);
  1507. goto out_commit;
  1508. }
  1509. /*
  1510. * Fill our page array first. That way we've grabbed enough so
  1511. * that we can zero and flush if we error after adding the
  1512. * extent.
  1513. */
  1514. ret = ocfs2_grab_pages_for_write(mapping, wc, wc->w_cpos, pos,
  1515. clusters_to_alloc + extents_to_split,
  1516. mmap_page);
  1517. if (ret) {
  1518. mlog_errno(ret);
  1519. goto out_commit;
  1520. }
  1521. ret = ocfs2_write_cluster_by_desc(mapping, data_ac, meta_ac, wc, pos,
  1522. len);
  1523. if (ret) {
  1524. mlog_errno(ret);
  1525. goto out_commit;
  1526. }
  1527. if (data_ac)
  1528. ocfs2_free_alloc_context(data_ac);
  1529. if (meta_ac)
  1530. ocfs2_free_alloc_context(meta_ac);
  1531. success:
  1532. *pagep = wc->w_target_page;
  1533. *fsdata = wc;
  1534. return 0;
  1535. out_commit:
  1536. ocfs2_commit_trans(osb, handle);
  1537. out:
  1538. ocfs2_free_write_ctxt(wc);
  1539. if (data_ac)
  1540. ocfs2_free_alloc_context(data_ac);
  1541. if (meta_ac)
  1542. ocfs2_free_alloc_context(meta_ac);
  1543. return ret;
  1544. }
  1545. static int ocfs2_write_begin(struct file *file, struct address_space *mapping,
  1546. loff_t pos, unsigned len, unsigned flags,
  1547. struct page **pagep, void **fsdata)
  1548. {
  1549. int ret;
  1550. struct buffer_head *di_bh = NULL;
  1551. struct inode *inode = mapping->host;
  1552. ret = ocfs2_inode_lock(inode, &di_bh, 1);
  1553. if (ret) {
  1554. mlog_errno(ret);
  1555. return ret;
  1556. }
  1557. /*
  1558. * Take alloc sem here to prevent concurrent lookups. That way
  1559. * the mapping, zeroing and tree manipulation within
  1560. * ocfs2_write() will be safe against ->readpage(). This
  1561. * should also serve to lock out allocation from a shared
  1562. * writeable region.
  1563. */
  1564. down_write(&OCFS2_I(inode)->ip_alloc_sem);
  1565. ret = ocfs2_write_begin_nolock(mapping, pos, len, flags, pagep,
  1566. fsdata, di_bh, NULL);
  1567. if (ret) {
  1568. mlog_errno(ret);
  1569. goto out_fail;
  1570. }
  1571. brelse(di_bh);
  1572. return 0;
  1573. out_fail:
  1574. up_write(&OCFS2_I(inode)->ip_alloc_sem);
  1575. brelse(di_bh);
  1576. ocfs2_inode_unlock(inode, 1);
  1577. return ret;
  1578. }
  1579. static void ocfs2_write_end_inline(struct inode *inode, loff_t pos,
  1580. unsigned len, unsigned *copied,
  1581. struct ocfs2_dinode *di,
  1582. struct ocfs2_write_ctxt *wc)
  1583. {
  1584. void *kaddr;
  1585. if (unlikely(*copied < len)) {
  1586. if (!PageUptodate(wc->w_target_page)) {
  1587. *copied = 0;
  1588. return;
  1589. }
  1590. }
  1591. kaddr = kmap_atomic(wc->w_target_page, KM_USER0);
  1592. memcpy(di->id2.i_data.id_data + pos, kaddr + pos, *copied);
  1593. kunmap_atomic(kaddr, KM_USER0);
  1594. mlog(0, "Data written to inode at offset %llu. "
  1595. "id_count = %u, copied = %u, i_dyn_features = 0x%x\n",
  1596. (unsigned long long)pos, *copied,
  1597. le16_to_cpu(di->id2.i_data.id_count),
  1598. le16_to_cpu(di->i_dyn_features));
  1599. }
  1600. int ocfs2_write_end_nolock(struct address_space *mapping,
  1601. loff_t pos, unsigned len, unsigned copied,
  1602. struct page *page, void *fsdata)
  1603. {
  1604. int i;
  1605. unsigned from, to, start = pos & (PAGE_CACHE_SIZE - 1);
  1606. struct inode *inode = mapping->host;
  1607. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1608. struct ocfs2_write_ctxt *wc = fsdata;
  1609. struct ocfs2_dinode *di = (struct ocfs2_dinode *)wc->w_di_bh->b_data;
  1610. handle_t *handle = wc->w_handle;
  1611. struct page *tmppage;
  1612. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  1613. ocfs2_write_end_inline(inode, pos, len, &copied, di, wc);
  1614. goto out_write_size;
  1615. }
  1616. if (unlikely(copied < len)) {
  1617. if (!PageUptodate(wc->w_target_page))
  1618. copied = 0;
  1619. ocfs2_zero_new_buffers(wc->w_target_page, start+copied,
  1620. start+len);
  1621. }
  1622. flush_dcache_page(wc->w_target_page);
  1623. for(i = 0; i < wc->w_num_pages; i++) {
  1624. tmppage = wc->w_pages[i];
  1625. if (tmppage == wc->w_target_page) {
  1626. from = wc->w_target_from;
  1627. to = wc->w_target_to;
  1628. BUG_ON(from > PAGE_CACHE_SIZE ||
  1629. to > PAGE_CACHE_SIZE ||
  1630. to < from);
  1631. } else {
  1632. /*
  1633. * Pages adjacent to the target (if any) imply
  1634. * a hole-filling write in which case we want
  1635. * to flush their entire range.
  1636. */
  1637. from = 0;
  1638. to = PAGE_CACHE_SIZE;
  1639. }
  1640. if (page_has_buffers(tmppage)) {
  1641. if (ocfs2_should_order_data(inode)) {
  1642. ocfs2_jbd2_file_inode(wc->w_handle, inode);
  1643. #ifdef CONFIG_OCFS2_COMPAT_JBD
  1644. walk_page_buffers(wc->w_handle,
  1645. page_buffers(tmppage),
  1646. from, to, NULL,
  1647. ocfs2_journal_dirty_data);
  1648. #endif
  1649. }
  1650. block_commit_write(tmppage, from, to);
  1651. }
  1652. }
  1653. out_write_size:
  1654. pos += copied;
  1655. if (pos > inode->i_size) {
  1656. i_size_write(inode, pos);
  1657. mark_inode_dirty(inode);
  1658. }
  1659. inode->i_blocks = ocfs2_inode_sector_count(inode);
  1660. di->i_size = cpu_to_le64((u64)i_size_read(inode));
  1661. inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  1662. di->i_mtime = di->i_ctime = cpu_to_le64(inode->i_mtime.tv_sec);
  1663. di->i_mtime_nsec = di->i_ctime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);
  1664. ocfs2_journal_dirty(handle, wc->w_di_bh);
  1665. ocfs2_commit_trans(osb, handle);
  1666. ocfs2_run_deallocs(osb, &wc->w_dealloc);
  1667. ocfs2_free_write_ctxt(wc);
  1668. return copied;
  1669. }
  1670. static int ocfs2_write_end(struct file *file, struct address_space *mapping,
  1671. loff_t pos, unsigned len, unsigned copied,
  1672. struct page *page, void *fsdata)
  1673. {
  1674. int ret;
  1675. struct inode *inode = mapping->host;
  1676. ret = ocfs2_write_end_nolock(mapping, pos, len, copied, page, fsdata);
  1677. up_write(&OCFS2_I(inode)->ip_alloc_sem);
  1678. ocfs2_inode_unlock(inode, 1);
  1679. return ret;
  1680. }
  1681. const struct address_space_operations ocfs2_aops = {
  1682. .readpage = ocfs2_readpage,
  1683. .readpages = ocfs2_readpages,
  1684. .writepage = ocfs2_writepage,
  1685. .write_begin = ocfs2_write_begin,
  1686. .write_end = ocfs2_write_end,
  1687. .bmap = ocfs2_bmap,
  1688. .sync_page = block_sync_page,
  1689. .direct_IO = ocfs2_direct_IO,
  1690. .invalidatepage = ocfs2_invalidatepage,
  1691. .releasepage = ocfs2_releasepage,
  1692. .migratepage = buffer_migrate_page,
  1693. };