aops.c 49 KB

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