aops.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* -*- mode: c; c-basic-offset: 8; -*-
  3. * vim: noexpandtab sw=8 ts=8 sts=0:
  4. *
  5. * Copyright (C) 2002, 2004 Oracle. All rights reserved.
  6. */
  7. #include <linux/fs.h>
  8. #include <linux/slab.h>
  9. #include <linux/highmem.h>
  10. #include <linux/pagemap.h>
  11. #include <asm/byteorder.h>
  12. #include <linux/swap.h>
  13. #include <linux/mpage.h>
  14. #include <linux/quotaops.h>
  15. #include <linux/blkdev.h>
  16. #include <linux/uio.h>
  17. #include <linux/mm.h>
  18. #include <cluster/masklog.h>
  19. #include "ocfs2.h"
  20. #include "alloc.h"
  21. #include "aops.h"
  22. #include "dlmglue.h"
  23. #include "extent_map.h"
  24. #include "file.h"
  25. #include "inode.h"
  26. #include "journal.h"
  27. #include "suballoc.h"
  28. #include "super.h"
  29. #include "symlink.h"
  30. #include "refcounttree.h"
  31. #include "ocfs2_trace.h"
  32. #include "buffer_head_io.h"
  33. #include "dir.h"
  34. #include "namei.h"
  35. #include "sysfile.h"
  36. static int ocfs2_symlink_get_block(struct inode *inode, sector_t iblock,
  37. struct buffer_head *bh_result, int create)
  38. {
  39. int err = -EIO;
  40. int status;
  41. struct ocfs2_dinode *fe = NULL;
  42. struct buffer_head *bh = NULL;
  43. struct buffer_head *buffer_cache_bh = NULL;
  44. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  45. void *kaddr;
  46. trace_ocfs2_symlink_get_block(
  47. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  48. (unsigned long long)iblock, bh_result, create);
  49. BUG_ON(ocfs2_inode_is_fast_symlink(inode));
  50. if ((iblock << inode->i_sb->s_blocksize_bits) > PATH_MAX + 1) {
  51. mlog(ML_ERROR, "block offset > PATH_MAX: %llu",
  52. (unsigned long long)iblock);
  53. goto bail;
  54. }
  55. status = ocfs2_read_inode_block(inode, &bh);
  56. if (status < 0) {
  57. mlog_errno(status);
  58. goto bail;
  59. }
  60. fe = (struct ocfs2_dinode *) bh->b_data;
  61. if ((u64)iblock >= ocfs2_clusters_to_blocks(inode->i_sb,
  62. le32_to_cpu(fe->i_clusters))) {
  63. err = -ENOMEM;
  64. mlog(ML_ERROR, "block offset is outside the allocated size: "
  65. "%llu\n", (unsigned long long)iblock);
  66. goto bail;
  67. }
  68. /* We don't use the page cache to create symlink data, so if
  69. * need be, copy it over from the buffer cache. */
  70. if (!buffer_uptodate(bh_result) && ocfs2_inode_is_new(inode)) {
  71. u64 blkno = le64_to_cpu(fe->id2.i_list.l_recs[0].e_blkno) +
  72. iblock;
  73. buffer_cache_bh = sb_getblk(osb->sb, blkno);
  74. if (!buffer_cache_bh) {
  75. err = -ENOMEM;
  76. mlog(ML_ERROR, "couldn't getblock for symlink!\n");
  77. goto bail;
  78. }
  79. /* we haven't locked out transactions, so a commit
  80. * could've happened. Since we've got a reference on
  81. * the bh, even if it commits while we're doing the
  82. * copy, the data is still good. */
  83. if (buffer_jbd(buffer_cache_bh)
  84. && ocfs2_inode_is_new(inode)) {
  85. kaddr = kmap_atomic(bh_result->b_page);
  86. if (!kaddr) {
  87. mlog(ML_ERROR, "couldn't kmap!\n");
  88. goto bail;
  89. }
  90. memcpy(kaddr + (bh_result->b_size * iblock),
  91. buffer_cache_bh->b_data,
  92. bh_result->b_size);
  93. kunmap_atomic(kaddr);
  94. set_buffer_uptodate(bh_result);
  95. }
  96. brelse(buffer_cache_bh);
  97. }
  98. map_bh(bh_result, inode->i_sb,
  99. le64_to_cpu(fe->id2.i_list.l_recs[0].e_blkno) + iblock);
  100. err = 0;
  101. bail:
  102. brelse(bh);
  103. return err;
  104. }
  105. static int ocfs2_lock_get_block(struct inode *inode, sector_t iblock,
  106. struct buffer_head *bh_result, int create)
  107. {
  108. int ret = 0;
  109. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  110. down_read(&oi->ip_alloc_sem);
  111. ret = ocfs2_get_block(inode, iblock, bh_result, create);
  112. up_read(&oi->ip_alloc_sem);
  113. return ret;
  114. }
  115. int ocfs2_get_block(struct inode *inode, sector_t iblock,
  116. struct buffer_head *bh_result, int create)
  117. {
  118. int err = 0;
  119. unsigned int ext_flags;
  120. u64 max_blocks = bh_result->b_size >> inode->i_blkbits;
  121. u64 p_blkno, count, past_eof;
  122. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  123. trace_ocfs2_get_block((unsigned long long)OCFS2_I(inode)->ip_blkno,
  124. (unsigned long long)iblock, bh_result, create);
  125. if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_SYSTEM_FILE)
  126. mlog(ML_NOTICE, "get_block on system inode 0x%p (%lu)\n",
  127. inode, inode->i_ino);
  128. if (S_ISLNK(inode->i_mode)) {
  129. /* this always does I/O for some reason. */
  130. err = ocfs2_symlink_get_block(inode, iblock, bh_result, create);
  131. goto bail;
  132. }
  133. err = ocfs2_extent_map_get_blocks(inode, iblock, &p_blkno, &count,
  134. &ext_flags);
  135. if (err) {
  136. mlog(ML_ERROR, "Error %d from get_blocks(0x%p, %llu, 1, "
  137. "%llu, NULL)\n", err, inode, (unsigned long long)iblock,
  138. (unsigned long long)p_blkno);
  139. goto bail;
  140. }
  141. if (max_blocks < count)
  142. count = max_blocks;
  143. /*
  144. * ocfs2 never allocates in this function - the only time we
  145. * need to use BH_New is when we're extending i_size on a file
  146. * system which doesn't support holes, in which case BH_New
  147. * allows __block_write_begin() to zero.
  148. *
  149. * If we see this on a sparse file system, then a truncate has
  150. * raced us and removed the cluster. In this case, we clear
  151. * the buffers dirty and uptodate bits and let the buffer code
  152. * ignore it as a hole.
  153. */
  154. if (create && p_blkno == 0 && ocfs2_sparse_alloc(osb)) {
  155. clear_buffer_dirty(bh_result);
  156. clear_buffer_uptodate(bh_result);
  157. goto bail;
  158. }
  159. /* Treat the unwritten extent as a hole for zeroing purposes. */
  160. if (p_blkno && !(ext_flags & OCFS2_EXT_UNWRITTEN))
  161. map_bh(bh_result, inode->i_sb, p_blkno);
  162. bh_result->b_size = count << inode->i_blkbits;
  163. if (!ocfs2_sparse_alloc(osb)) {
  164. if (p_blkno == 0) {
  165. err = -EIO;
  166. mlog(ML_ERROR,
  167. "iblock = %llu p_blkno = %llu blkno=(%llu)\n",
  168. (unsigned long long)iblock,
  169. (unsigned long long)p_blkno,
  170. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  171. mlog(ML_ERROR, "Size %llu, clusters %u\n", (unsigned long long)i_size_read(inode), OCFS2_I(inode)->ip_clusters);
  172. dump_stack();
  173. goto bail;
  174. }
  175. }
  176. past_eof = ocfs2_blocks_for_bytes(inode->i_sb, i_size_read(inode));
  177. trace_ocfs2_get_block_end((unsigned long long)OCFS2_I(inode)->ip_blkno,
  178. (unsigned long long)past_eof);
  179. if (create && (iblock >= past_eof))
  180. set_buffer_new(bh_result);
  181. bail:
  182. if (err < 0)
  183. err = -EIO;
  184. return err;
  185. }
  186. int ocfs2_read_inline_data(struct inode *inode, struct page *page,
  187. struct buffer_head *di_bh)
  188. {
  189. void *kaddr;
  190. loff_t size;
  191. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  192. if (!(le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_DATA_FL)) {
  193. ocfs2_error(inode->i_sb, "Inode %llu lost inline data flag\n",
  194. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  195. return -EROFS;
  196. }
  197. size = i_size_read(inode);
  198. if (size > PAGE_SIZE ||
  199. size > ocfs2_max_inline_data_with_xattr(inode->i_sb, di)) {
  200. ocfs2_error(inode->i_sb,
  201. "Inode %llu has with inline data has bad size: %Lu\n",
  202. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  203. (unsigned long long)size);
  204. return -EROFS;
  205. }
  206. kaddr = kmap_atomic(page);
  207. if (size)
  208. memcpy(kaddr, di->id2.i_data.id_data, size);
  209. /* Clear the remaining part of the page */
  210. memset(kaddr + size, 0, PAGE_SIZE - size);
  211. flush_dcache_page(page);
  212. kunmap_atomic(kaddr);
  213. SetPageUptodate(page);
  214. return 0;
  215. }
  216. static int ocfs2_readpage_inline(struct inode *inode, struct page *page)
  217. {
  218. int ret;
  219. struct buffer_head *di_bh = NULL;
  220. BUG_ON(!PageLocked(page));
  221. BUG_ON(!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL));
  222. ret = ocfs2_read_inode_block(inode, &di_bh);
  223. if (ret) {
  224. mlog_errno(ret);
  225. goto out;
  226. }
  227. ret = ocfs2_read_inline_data(inode, page, di_bh);
  228. out:
  229. unlock_page(page);
  230. brelse(di_bh);
  231. return ret;
  232. }
  233. static int ocfs2_readpage(struct file *file, struct page *page)
  234. {
  235. struct inode *inode = page->mapping->host;
  236. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  237. loff_t start = (loff_t)page->index << PAGE_SHIFT;
  238. int ret, unlock = 1;
  239. trace_ocfs2_readpage((unsigned long long)oi->ip_blkno,
  240. (page ? page->index : 0));
  241. ret = ocfs2_inode_lock_with_page(inode, NULL, 0, page);
  242. if (ret != 0) {
  243. if (ret == AOP_TRUNCATED_PAGE)
  244. unlock = 0;
  245. mlog_errno(ret);
  246. goto out;
  247. }
  248. if (down_read_trylock(&oi->ip_alloc_sem) == 0) {
  249. /*
  250. * Unlock the page and cycle ip_alloc_sem so that we don't
  251. * busyloop waiting for ip_alloc_sem to unlock
  252. */
  253. ret = AOP_TRUNCATED_PAGE;
  254. unlock_page(page);
  255. unlock = 0;
  256. down_read(&oi->ip_alloc_sem);
  257. up_read(&oi->ip_alloc_sem);
  258. goto out_inode_unlock;
  259. }
  260. /*
  261. * i_size might have just been updated as we grabed the meta lock. We
  262. * might now be discovering a truncate that hit on another node.
  263. * block_read_full_page->get_block freaks out if it is asked to read
  264. * beyond the end of a file, so we check here. Callers
  265. * (generic_file_read, vm_ops->fault) are clever enough to check i_size
  266. * and notice that the page they just read isn't needed.
  267. *
  268. * XXX sys_readahead() seems to get that wrong?
  269. */
  270. if (start >= i_size_read(inode)) {
  271. zero_user(page, 0, PAGE_SIZE);
  272. SetPageUptodate(page);
  273. ret = 0;
  274. goto out_alloc;
  275. }
  276. if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL)
  277. ret = ocfs2_readpage_inline(inode, page);
  278. else
  279. ret = block_read_full_page(page, ocfs2_get_block);
  280. unlock = 0;
  281. out_alloc:
  282. up_read(&oi->ip_alloc_sem);
  283. out_inode_unlock:
  284. ocfs2_inode_unlock(inode, 0);
  285. out:
  286. if (unlock)
  287. unlock_page(page);
  288. return ret;
  289. }
  290. /*
  291. * This is used only for read-ahead. Failures or difficult to handle
  292. * situations are safe to ignore.
  293. *
  294. * Right now, we don't bother with BH_Boundary - in-inode extent lists
  295. * are quite large (243 extents on 4k blocks), so most inodes don't
  296. * grow out to a tree. If need be, detecting boundary extents could
  297. * trivially be added in a future version of ocfs2_get_block().
  298. */
  299. static void ocfs2_readahead(struct readahead_control *rac)
  300. {
  301. int ret;
  302. struct inode *inode = rac->mapping->host;
  303. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  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;
  311. if (down_read_trylock(&oi->ip_alloc_sem) == 0)
  312. goto out_unlock;
  313. /*
  314. * Don't bother with inline-data. There isn't anything
  315. * to read-ahead in that case anyway...
  316. */
  317. if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL)
  318. goto out_up;
  319. /*
  320. * Check whether a remote node truncated this file - we just
  321. * drop out in that case as it's not worth handling here.
  322. */
  323. if (readahead_pos(rac) >= i_size_read(inode))
  324. goto out_up;
  325. mpage_readahead(rac, ocfs2_get_block);
  326. out_up:
  327. up_read(&oi->ip_alloc_sem);
  328. out_unlock:
  329. ocfs2_inode_unlock(inode, 0);
  330. }
  331. /* Note: Because we don't support holes, our allocation has
  332. * already happened (allocation writes zeros to the file data)
  333. * so we don't have to worry about ordered writes in
  334. * ocfs2_writepage.
  335. *
  336. * ->writepage is called during the process of invalidating the page cache
  337. * during blocked lock processing. It can't block on any cluster locks
  338. * to during block mapping. It's relying on the fact that the block
  339. * mapping can't have disappeared under the dirty pages that it is
  340. * being asked to write back.
  341. */
  342. static int ocfs2_writepage(struct page *page, struct writeback_control *wbc)
  343. {
  344. trace_ocfs2_writepage(
  345. (unsigned long long)OCFS2_I(page->mapping->host)->ip_blkno,
  346. page->index);
  347. return block_write_full_page(page, ocfs2_get_block, wbc);
  348. }
  349. /* Taken from ext3. We don't necessarily need the full blown
  350. * functionality yet, but IMHO it's better to cut and paste the whole
  351. * thing so we can avoid introducing our own bugs (and easily pick up
  352. * their fixes when they happen) --Mark */
  353. int walk_page_buffers( handle_t *handle,
  354. struct buffer_head *head,
  355. unsigned from,
  356. unsigned to,
  357. int *partial,
  358. int (*fn)( handle_t *handle,
  359. struct buffer_head *bh))
  360. {
  361. struct buffer_head *bh;
  362. unsigned block_start, block_end;
  363. unsigned blocksize = head->b_size;
  364. int err, ret = 0;
  365. struct buffer_head *next;
  366. for ( bh = head, block_start = 0;
  367. ret == 0 && (bh != head || !block_start);
  368. block_start = block_end, bh = next)
  369. {
  370. next = bh->b_this_page;
  371. block_end = block_start + blocksize;
  372. if (block_end <= from || block_start >= to) {
  373. if (partial && !buffer_uptodate(bh))
  374. *partial = 1;
  375. continue;
  376. }
  377. err = (*fn)(handle, bh);
  378. if (!ret)
  379. ret = err;
  380. }
  381. return ret;
  382. }
  383. static sector_t ocfs2_bmap(struct address_space *mapping, sector_t block)
  384. {
  385. sector_t status;
  386. u64 p_blkno = 0;
  387. int err = 0;
  388. struct inode *inode = mapping->host;
  389. trace_ocfs2_bmap((unsigned long long)OCFS2_I(inode)->ip_blkno,
  390. (unsigned long long)block);
  391. /*
  392. * The swap code (ab-)uses ->bmap to get a block mapping and then
  393. * bypasseѕ the file system for actual I/O. We really can't allow
  394. * that on refcounted inodes, so we have to skip out here. And yes,
  395. * 0 is the magic code for a bmap error..
  396. */
  397. if (ocfs2_is_refcount_inode(inode))
  398. return 0;
  399. /* We don't need to lock journal system files, since they aren't
  400. * accessed concurrently from multiple nodes.
  401. */
  402. if (!INODE_JOURNAL(inode)) {
  403. err = ocfs2_inode_lock(inode, NULL, 0);
  404. if (err) {
  405. if (err != -ENOENT)
  406. mlog_errno(err);
  407. goto bail;
  408. }
  409. down_read(&OCFS2_I(inode)->ip_alloc_sem);
  410. }
  411. if (!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL))
  412. err = ocfs2_extent_map_get_blocks(inode, block, &p_blkno, NULL,
  413. NULL);
  414. if (!INODE_JOURNAL(inode)) {
  415. up_read(&OCFS2_I(inode)->ip_alloc_sem);
  416. ocfs2_inode_unlock(inode, 0);
  417. }
  418. if (err) {
  419. mlog(ML_ERROR, "get_blocks() failed, block = %llu\n",
  420. (unsigned long long)block);
  421. mlog_errno(err);
  422. goto bail;
  423. }
  424. bail:
  425. status = err ? 0 : p_blkno;
  426. return status;
  427. }
  428. static int ocfs2_releasepage(struct page *page, gfp_t wait)
  429. {
  430. if (!page_has_buffers(page))
  431. return 0;
  432. return try_to_free_buffers(page);
  433. }
  434. static void ocfs2_figure_cluster_boundaries(struct ocfs2_super *osb,
  435. u32 cpos,
  436. unsigned int *start,
  437. unsigned int *end)
  438. {
  439. unsigned int cluster_start = 0, cluster_end = PAGE_SIZE;
  440. if (unlikely(PAGE_SHIFT > osb->s_clustersize_bits)) {
  441. unsigned int cpp;
  442. cpp = 1 << (PAGE_SHIFT - osb->s_clustersize_bits);
  443. cluster_start = cpos % cpp;
  444. cluster_start = cluster_start << osb->s_clustersize_bits;
  445. cluster_end = cluster_start + osb->s_clustersize;
  446. }
  447. BUG_ON(cluster_start > PAGE_SIZE);
  448. BUG_ON(cluster_end > PAGE_SIZE);
  449. if (start)
  450. *start = cluster_start;
  451. if (end)
  452. *end = cluster_end;
  453. }
  454. /*
  455. * 'from' and 'to' are the region in the page to avoid zeroing.
  456. *
  457. * If pagesize > clustersize, this function will avoid zeroing outside
  458. * of the cluster boundary.
  459. *
  460. * from == to == 0 is code for "zero the entire cluster region"
  461. */
  462. static void ocfs2_clear_page_regions(struct page *page,
  463. struct ocfs2_super *osb, u32 cpos,
  464. unsigned from, unsigned to)
  465. {
  466. void *kaddr;
  467. unsigned int cluster_start, cluster_end;
  468. ocfs2_figure_cluster_boundaries(osb, cpos, &cluster_start, &cluster_end);
  469. kaddr = kmap_atomic(page);
  470. if (from || to) {
  471. if (from > cluster_start)
  472. memset(kaddr + cluster_start, 0, from - cluster_start);
  473. if (to < cluster_end)
  474. memset(kaddr + to, 0, cluster_end - to);
  475. } else {
  476. memset(kaddr + cluster_start, 0, cluster_end - cluster_start);
  477. }
  478. kunmap_atomic(kaddr);
  479. }
  480. /*
  481. * Nonsparse file systems fully allocate before we get to the write
  482. * code. This prevents ocfs2_write() from tagging the write as an
  483. * allocating one, which means ocfs2_map_page_blocks() might try to
  484. * read-in the blocks at the tail of our file. Avoid reading them by
  485. * testing i_size against each block offset.
  486. */
  487. static int ocfs2_should_read_blk(struct inode *inode, struct page *page,
  488. unsigned int block_start)
  489. {
  490. u64 offset = page_offset(page) + block_start;
  491. if (ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb)))
  492. return 1;
  493. if (i_size_read(inode) > offset)
  494. return 1;
  495. return 0;
  496. }
  497. /*
  498. * Some of this taken from __block_write_begin(). We already have our
  499. * mapping by now though, and the entire write will be allocating or
  500. * it won't, so not much need to use BH_New.
  501. *
  502. * This will also skip zeroing, which is handled externally.
  503. */
  504. int ocfs2_map_page_blocks(struct page *page, u64 *p_blkno,
  505. struct inode *inode, unsigned int from,
  506. unsigned int to, int new)
  507. {
  508. int ret = 0;
  509. struct buffer_head *head, *bh, *wait[2], **wait_bh = wait;
  510. unsigned int block_end, block_start;
  511. unsigned int bsize = i_blocksize(inode);
  512. if (!page_has_buffers(page))
  513. create_empty_buffers(page, bsize, 0);
  514. head = page_buffers(page);
  515. for (bh = head, block_start = 0; bh != head || !block_start;
  516. bh = bh->b_this_page, block_start += bsize) {
  517. block_end = block_start + bsize;
  518. clear_buffer_new(bh);
  519. /*
  520. * Ignore blocks outside of our i/o range -
  521. * they may belong to unallocated clusters.
  522. */
  523. if (block_start >= to || block_end <= from) {
  524. if (PageUptodate(page))
  525. set_buffer_uptodate(bh);
  526. continue;
  527. }
  528. /*
  529. * For an allocating write with cluster size >= page
  530. * size, we always write the entire page.
  531. */
  532. if (new)
  533. set_buffer_new(bh);
  534. if (!buffer_mapped(bh)) {
  535. map_bh(bh, inode->i_sb, *p_blkno);
  536. clean_bdev_bh_alias(bh);
  537. }
  538. if (PageUptodate(page)) {
  539. if (!buffer_uptodate(bh))
  540. set_buffer_uptodate(bh);
  541. } else if (!buffer_uptodate(bh) && !buffer_delay(bh) &&
  542. !buffer_new(bh) &&
  543. ocfs2_should_read_blk(inode, page, block_start) &&
  544. (block_start < from || block_end > to)) {
  545. ll_rw_block(REQ_OP_READ, 0, 1, &bh);
  546. *wait_bh++=bh;
  547. }
  548. *p_blkno = *p_blkno + 1;
  549. }
  550. /*
  551. * If we issued read requests - let them complete.
  552. */
  553. while(wait_bh > wait) {
  554. wait_on_buffer(*--wait_bh);
  555. if (!buffer_uptodate(*wait_bh))
  556. ret = -EIO;
  557. }
  558. if (ret == 0 || !new)
  559. return ret;
  560. /*
  561. * If we get -EIO above, zero out any newly allocated blocks
  562. * to avoid exposing stale data.
  563. */
  564. bh = head;
  565. block_start = 0;
  566. do {
  567. block_end = block_start + bsize;
  568. if (block_end <= from)
  569. goto next_bh;
  570. if (block_start >= to)
  571. break;
  572. zero_user(page, block_start, bh->b_size);
  573. set_buffer_uptodate(bh);
  574. mark_buffer_dirty(bh);
  575. next_bh:
  576. block_start = block_end;
  577. bh = bh->b_this_page;
  578. } while (bh != head);
  579. return ret;
  580. }
  581. #if (PAGE_SIZE >= OCFS2_MAX_CLUSTERSIZE)
  582. #define OCFS2_MAX_CTXT_PAGES 1
  583. #else
  584. #define OCFS2_MAX_CTXT_PAGES (OCFS2_MAX_CLUSTERSIZE / PAGE_SIZE)
  585. #endif
  586. #define OCFS2_MAX_CLUSTERS_PER_PAGE (PAGE_SIZE / OCFS2_MIN_CLUSTERSIZE)
  587. struct ocfs2_unwritten_extent {
  588. struct list_head ue_node;
  589. struct list_head ue_ip_node;
  590. u32 ue_cpos;
  591. u32 ue_phys;
  592. };
  593. /*
  594. * Describe the state of a single cluster to be written to.
  595. */
  596. struct ocfs2_write_cluster_desc {
  597. u32 c_cpos;
  598. u32 c_phys;
  599. /*
  600. * Give this a unique field because c_phys eventually gets
  601. * filled.
  602. */
  603. unsigned c_new;
  604. unsigned c_clear_unwritten;
  605. unsigned c_needs_zero;
  606. };
  607. struct ocfs2_write_ctxt {
  608. /* Logical cluster position / len of write */
  609. u32 w_cpos;
  610. u32 w_clen;
  611. /* First cluster allocated in a nonsparse extend */
  612. u32 w_first_new_cpos;
  613. /* Type of caller. Must be one of buffer, mmap, direct. */
  614. ocfs2_write_type_t w_type;
  615. struct ocfs2_write_cluster_desc w_desc[OCFS2_MAX_CLUSTERS_PER_PAGE];
  616. /*
  617. * This is true if page_size > cluster_size.
  618. *
  619. * It triggers a set of special cases during write which might
  620. * have to deal with allocating writes to partial pages.
  621. */
  622. unsigned int w_large_pages;
  623. /*
  624. * Pages involved in this write.
  625. *
  626. * w_target_page is the page being written to by the user.
  627. *
  628. * w_pages is an array of pages which always contains
  629. * w_target_page, and in the case of an allocating write with
  630. * page_size < cluster size, it will contain zero'd and mapped
  631. * pages adjacent to w_target_page which need to be written
  632. * out in so that future reads from that region will get
  633. * zero's.
  634. */
  635. unsigned int w_num_pages;
  636. struct page *w_pages[OCFS2_MAX_CTXT_PAGES];
  637. struct page *w_target_page;
  638. /*
  639. * w_target_locked is used for page_mkwrite path indicating no unlocking
  640. * against w_target_page in ocfs2_write_end_nolock.
  641. */
  642. unsigned int w_target_locked:1;
  643. /*
  644. * ocfs2_write_end() uses this to know what the real range to
  645. * write in the target should be.
  646. */
  647. unsigned int w_target_from;
  648. unsigned int w_target_to;
  649. /*
  650. * We could use journal_current_handle() but this is cleaner,
  651. * IMHO -Mark
  652. */
  653. handle_t *w_handle;
  654. struct buffer_head *w_di_bh;
  655. struct ocfs2_cached_dealloc_ctxt w_dealloc;
  656. struct list_head w_unwritten_list;
  657. unsigned int w_unwritten_count;
  658. };
  659. void ocfs2_unlock_and_free_pages(struct page **pages, int num_pages)
  660. {
  661. int i;
  662. for(i = 0; i < num_pages; i++) {
  663. if (pages[i]) {
  664. unlock_page(pages[i]);
  665. mark_page_accessed(pages[i]);
  666. put_page(pages[i]);
  667. }
  668. }
  669. }
  670. static void ocfs2_unlock_pages(struct ocfs2_write_ctxt *wc)
  671. {
  672. int i;
  673. /*
  674. * w_target_locked is only set to true in the page_mkwrite() case.
  675. * The intent is to allow us to lock the target page from write_begin()
  676. * to write_end(). The caller must hold a ref on w_target_page.
  677. */
  678. if (wc->w_target_locked) {
  679. BUG_ON(!wc->w_target_page);
  680. for (i = 0; i < wc->w_num_pages; i++) {
  681. if (wc->w_target_page == wc->w_pages[i]) {
  682. wc->w_pages[i] = NULL;
  683. break;
  684. }
  685. }
  686. mark_page_accessed(wc->w_target_page);
  687. put_page(wc->w_target_page);
  688. }
  689. ocfs2_unlock_and_free_pages(wc->w_pages, wc->w_num_pages);
  690. }
  691. static void ocfs2_free_unwritten_list(struct inode *inode,
  692. struct list_head *head)
  693. {
  694. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  695. struct ocfs2_unwritten_extent *ue = NULL, *tmp = NULL;
  696. list_for_each_entry_safe(ue, tmp, head, ue_node) {
  697. list_del(&ue->ue_node);
  698. spin_lock(&oi->ip_lock);
  699. list_del(&ue->ue_ip_node);
  700. spin_unlock(&oi->ip_lock);
  701. kfree(ue);
  702. }
  703. }
  704. static void ocfs2_free_write_ctxt(struct inode *inode,
  705. struct ocfs2_write_ctxt *wc)
  706. {
  707. ocfs2_free_unwritten_list(inode, &wc->w_unwritten_list);
  708. ocfs2_unlock_pages(wc);
  709. brelse(wc->w_di_bh);
  710. kfree(wc);
  711. }
  712. static int ocfs2_alloc_write_ctxt(struct ocfs2_write_ctxt **wcp,
  713. struct ocfs2_super *osb, loff_t pos,
  714. unsigned len, ocfs2_write_type_t type,
  715. struct buffer_head *di_bh)
  716. {
  717. u32 cend;
  718. struct ocfs2_write_ctxt *wc;
  719. wc = kzalloc(sizeof(struct ocfs2_write_ctxt), GFP_NOFS);
  720. if (!wc)
  721. return -ENOMEM;
  722. wc->w_cpos = pos >> osb->s_clustersize_bits;
  723. wc->w_first_new_cpos = UINT_MAX;
  724. cend = (pos + len - 1) >> osb->s_clustersize_bits;
  725. wc->w_clen = cend - wc->w_cpos + 1;
  726. get_bh(di_bh);
  727. wc->w_di_bh = di_bh;
  728. wc->w_type = type;
  729. if (unlikely(PAGE_SHIFT > osb->s_clustersize_bits))
  730. wc->w_large_pages = 1;
  731. else
  732. wc->w_large_pages = 0;
  733. ocfs2_init_dealloc_ctxt(&wc->w_dealloc);
  734. INIT_LIST_HEAD(&wc->w_unwritten_list);
  735. *wcp = wc;
  736. return 0;
  737. }
  738. /*
  739. * If a page has any new buffers, zero them out here, and mark them uptodate
  740. * and dirty so they'll be written out (in order to prevent uninitialised
  741. * block data from leaking). And clear the new bit.
  742. */
  743. static void ocfs2_zero_new_buffers(struct page *page, unsigned from, unsigned to)
  744. {
  745. unsigned int block_start, block_end;
  746. struct buffer_head *head, *bh;
  747. BUG_ON(!PageLocked(page));
  748. if (!page_has_buffers(page))
  749. return;
  750. bh = head = page_buffers(page);
  751. block_start = 0;
  752. do {
  753. block_end = block_start + bh->b_size;
  754. if (buffer_new(bh)) {
  755. if (block_end > from && block_start < to) {
  756. if (!PageUptodate(page)) {
  757. unsigned start, end;
  758. start = max(from, block_start);
  759. end = min(to, block_end);
  760. zero_user_segment(page, start, end);
  761. set_buffer_uptodate(bh);
  762. }
  763. clear_buffer_new(bh);
  764. mark_buffer_dirty(bh);
  765. }
  766. }
  767. block_start = block_end;
  768. bh = bh->b_this_page;
  769. } while (bh != head);
  770. }
  771. /*
  772. * Only called when we have a failure during allocating write to write
  773. * zero's to the newly allocated region.
  774. */
  775. static void ocfs2_write_failure(struct inode *inode,
  776. struct ocfs2_write_ctxt *wc,
  777. loff_t user_pos, unsigned user_len)
  778. {
  779. int i;
  780. unsigned from = user_pos & (PAGE_SIZE - 1),
  781. to = user_pos + user_len;
  782. struct page *tmppage;
  783. if (wc->w_target_page)
  784. ocfs2_zero_new_buffers(wc->w_target_page, from, to);
  785. for(i = 0; i < wc->w_num_pages; i++) {
  786. tmppage = wc->w_pages[i];
  787. if (tmppage && page_has_buffers(tmppage)) {
  788. if (ocfs2_should_order_data(inode))
  789. ocfs2_jbd2_inode_add_write(wc->w_handle, inode,
  790. user_pos, user_len);
  791. block_commit_write(tmppage, from, to);
  792. }
  793. }
  794. }
  795. static int ocfs2_prepare_page_for_write(struct inode *inode, u64 *p_blkno,
  796. struct ocfs2_write_ctxt *wc,
  797. struct page *page, u32 cpos,
  798. loff_t user_pos, unsigned user_len,
  799. int new)
  800. {
  801. int ret;
  802. unsigned int map_from = 0, map_to = 0;
  803. unsigned int cluster_start, cluster_end;
  804. unsigned int user_data_from = 0, user_data_to = 0;
  805. ocfs2_figure_cluster_boundaries(OCFS2_SB(inode->i_sb), cpos,
  806. &cluster_start, &cluster_end);
  807. /* treat the write as new if the a hole/lseek spanned across
  808. * the page boundary.
  809. */
  810. new = new | ((i_size_read(inode) <= page_offset(page)) &&
  811. (page_offset(page) <= user_pos));
  812. if (page == wc->w_target_page) {
  813. map_from = user_pos & (PAGE_SIZE - 1);
  814. map_to = map_from + user_len;
  815. if (new)
  816. ret = ocfs2_map_page_blocks(page, p_blkno, inode,
  817. cluster_start, cluster_end,
  818. new);
  819. else
  820. ret = ocfs2_map_page_blocks(page, p_blkno, inode,
  821. map_from, map_to, new);
  822. if (ret) {
  823. mlog_errno(ret);
  824. goto out;
  825. }
  826. user_data_from = map_from;
  827. user_data_to = map_to;
  828. if (new) {
  829. map_from = cluster_start;
  830. map_to = cluster_end;
  831. }
  832. } else {
  833. /*
  834. * If we haven't allocated the new page yet, we
  835. * shouldn't be writing it out without copying user
  836. * data. This is likely a math error from the caller.
  837. */
  838. BUG_ON(!new);
  839. map_from = cluster_start;
  840. map_to = cluster_end;
  841. ret = ocfs2_map_page_blocks(page, p_blkno, inode,
  842. cluster_start, cluster_end, new);
  843. if (ret) {
  844. mlog_errno(ret);
  845. goto out;
  846. }
  847. }
  848. /*
  849. * Parts of newly allocated pages need to be zero'd.
  850. *
  851. * Above, we have also rewritten 'to' and 'from' - as far as
  852. * the rest of the function is concerned, the entire cluster
  853. * range inside of a page needs to be written.
  854. *
  855. * We can skip this if the page is up to date - it's already
  856. * been zero'd from being read in as a hole.
  857. */
  858. if (new && !PageUptodate(page))
  859. ocfs2_clear_page_regions(page, OCFS2_SB(inode->i_sb),
  860. cpos, user_data_from, user_data_to);
  861. flush_dcache_page(page);
  862. out:
  863. return ret;
  864. }
  865. /*
  866. * This function will only grab one clusters worth of pages.
  867. */
  868. static int ocfs2_grab_pages_for_write(struct address_space *mapping,
  869. struct ocfs2_write_ctxt *wc,
  870. u32 cpos, loff_t user_pos,
  871. unsigned user_len, int new,
  872. struct page *mmap_page)
  873. {
  874. int ret = 0, i;
  875. unsigned long start, target_index, end_index, index;
  876. struct inode *inode = mapping->host;
  877. loff_t last_byte;
  878. target_index = user_pos >> PAGE_SHIFT;
  879. /*
  880. * Figure out how many pages we'll be manipulating here. For
  881. * non allocating write, we just change the one
  882. * page. Otherwise, we'll need a whole clusters worth. If we're
  883. * writing past i_size, we only need enough pages to cover the
  884. * last page of the write.
  885. */
  886. if (new) {
  887. wc->w_num_pages = ocfs2_pages_per_cluster(inode->i_sb);
  888. start = ocfs2_align_clusters_to_page_index(inode->i_sb, cpos);
  889. /*
  890. * We need the index *past* the last page we could possibly
  891. * touch. This is the page past the end of the write or
  892. * i_size, whichever is greater.
  893. */
  894. last_byte = max(user_pos + user_len, i_size_read(inode));
  895. BUG_ON(last_byte < 1);
  896. end_index = ((last_byte - 1) >> PAGE_SHIFT) + 1;
  897. if ((start + wc->w_num_pages) > end_index)
  898. wc->w_num_pages = end_index - start;
  899. } else {
  900. wc->w_num_pages = 1;
  901. start = target_index;
  902. }
  903. end_index = (user_pos + user_len - 1) >> PAGE_SHIFT;
  904. for(i = 0; i < wc->w_num_pages; i++) {
  905. index = start + i;
  906. if (index >= target_index && index <= end_index &&
  907. wc->w_type == OCFS2_WRITE_MMAP) {
  908. /*
  909. * ocfs2_pagemkwrite() is a little different
  910. * and wants us to directly use the page
  911. * passed in.
  912. */
  913. lock_page(mmap_page);
  914. /* Exit and let the caller retry */
  915. if (mmap_page->mapping != mapping) {
  916. WARN_ON(mmap_page->mapping);
  917. unlock_page(mmap_page);
  918. ret = -EAGAIN;
  919. goto out;
  920. }
  921. get_page(mmap_page);
  922. wc->w_pages[i] = mmap_page;
  923. wc->w_target_locked = true;
  924. } else if (index >= target_index && index <= end_index &&
  925. wc->w_type == OCFS2_WRITE_DIRECT) {
  926. /* Direct write has no mapping page. */
  927. wc->w_pages[i] = NULL;
  928. continue;
  929. } else {
  930. wc->w_pages[i] = find_or_create_page(mapping, index,
  931. GFP_NOFS);
  932. if (!wc->w_pages[i]) {
  933. ret = -ENOMEM;
  934. mlog_errno(ret);
  935. goto out;
  936. }
  937. }
  938. wait_for_stable_page(wc->w_pages[i]);
  939. if (index == target_index)
  940. wc->w_target_page = wc->w_pages[i];
  941. }
  942. out:
  943. if (ret)
  944. wc->w_target_locked = false;
  945. return ret;
  946. }
  947. /*
  948. * Prepare a single cluster for write one cluster into the file.
  949. */
  950. static int ocfs2_write_cluster(struct address_space *mapping,
  951. u32 *phys, unsigned int new,
  952. unsigned int clear_unwritten,
  953. unsigned int should_zero,
  954. struct ocfs2_alloc_context *data_ac,
  955. struct ocfs2_alloc_context *meta_ac,
  956. struct ocfs2_write_ctxt *wc, u32 cpos,
  957. loff_t user_pos, unsigned user_len)
  958. {
  959. int ret, i;
  960. u64 p_blkno;
  961. struct inode *inode = mapping->host;
  962. struct ocfs2_extent_tree et;
  963. int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
  964. if (new) {
  965. u32 tmp_pos;
  966. /*
  967. * This is safe to call with the page locks - it won't take
  968. * any additional semaphores or cluster locks.
  969. */
  970. tmp_pos = cpos;
  971. ret = ocfs2_add_inode_data(OCFS2_SB(inode->i_sb), inode,
  972. &tmp_pos, 1, !clear_unwritten,
  973. wc->w_di_bh, wc->w_handle,
  974. data_ac, meta_ac, NULL);
  975. /*
  976. * This shouldn't happen because we must have already
  977. * calculated the correct meta data allocation required. The
  978. * internal tree allocation code should know how to increase
  979. * transaction credits itself.
  980. *
  981. * If need be, we could handle -EAGAIN for a
  982. * RESTART_TRANS here.
  983. */
  984. mlog_bug_on_msg(ret == -EAGAIN,
  985. "Inode %llu: EAGAIN return during allocation.\n",
  986. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  987. if (ret < 0) {
  988. mlog_errno(ret);
  989. goto out;
  990. }
  991. } else if (clear_unwritten) {
  992. ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(inode),
  993. wc->w_di_bh);
  994. ret = ocfs2_mark_extent_written(inode, &et,
  995. wc->w_handle, cpos, 1, *phys,
  996. meta_ac, &wc->w_dealloc);
  997. if (ret < 0) {
  998. mlog_errno(ret);
  999. goto out;
  1000. }
  1001. }
  1002. /*
  1003. * The only reason this should fail is due to an inability to
  1004. * find the extent added.
  1005. */
  1006. ret = ocfs2_get_clusters(inode, cpos, phys, NULL, NULL);
  1007. if (ret < 0) {
  1008. mlog(ML_ERROR, "Get physical blkno failed for inode %llu, "
  1009. "at logical cluster %u",
  1010. (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos);
  1011. goto out;
  1012. }
  1013. BUG_ON(*phys == 0);
  1014. p_blkno = ocfs2_clusters_to_blocks(inode->i_sb, *phys);
  1015. if (!should_zero)
  1016. p_blkno += (user_pos >> inode->i_sb->s_blocksize_bits) & (u64)(bpc - 1);
  1017. for(i = 0; i < wc->w_num_pages; i++) {
  1018. int tmpret;
  1019. /* This is the direct io target page. */
  1020. if (wc->w_pages[i] == NULL) {
  1021. p_blkno++;
  1022. continue;
  1023. }
  1024. tmpret = ocfs2_prepare_page_for_write(inode, &p_blkno, wc,
  1025. wc->w_pages[i], cpos,
  1026. user_pos, user_len,
  1027. should_zero);
  1028. if (tmpret) {
  1029. mlog_errno(tmpret);
  1030. if (ret == 0)
  1031. ret = tmpret;
  1032. }
  1033. }
  1034. /*
  1035. * We only have cleanup to do in case of allocating write.
  1036. */
  1037. if (ret && new)
  1038. ocfs2_write_failure(inode, wc, user_pos, user_len);
  1039. out:
  1040. return ret;
  1041. }
  1042. static int ocfs2_write_cluster_by_desc(struct address_space *mapping,
  1043. struct ocfs2_alloc_context *data_ac,
  1044. struct ocfs2_alloc_context *meta_ac,
  1045. struct ocfs2_write_ctxt *wc,
  1046. loff_t pos, unsigned len)
  1047. {
  1048. int ret, i;
  1049. loff_t cluster_off;
  1050. unsigned int local_len = len;
  1051. struct ocfs2_write_cluster_desc *desc;
  1052. struct ocfs2_super *osb = OCFS2_SB(mapping->host->i_sb);
  1053. for (i = 0; i < wc->w_clen; i++) {
  1054. desc = &wc->w_desc[i];
  1055. /*
  1056. * We have to make sure that the total write passed in
  1057. * doesn't extend past a single cluster.
  1058. */
  1059. local_len = len;
  1060. cluster_off = pos & (osb->s_clustersize - 1);
  1061. if ((cluster_off + local_len) > osb->s_clustersize)
  1062. local_len = osb->s_clustersize - cluster_off;
  1063. ret = ocfs2_write_cluster(mapping, &desc->c_phys,
  1064. desc->c_new,
  1065. desc->c_clear_unwritten,
  1066. desc->c_needs_zero,
  1067. data_ac, meta_ac,
  1068. wc, desc->c_cpos, pos, local_len);
  1069. if (ret) {
  1070. mlog_errno(ret);
  1071. goto out;
  1072. }
  1073. len -= local_len;
  1074. pos += local_len;
  1075. }
  1076. ret = 0;
  1077. out:
  1078. return ret;
  1079. }
  1080. /*
  1081. * ocfs2_write_end() wants to know which parts of the target page it
  1082. * should complete the write on. It's easiest to compute them ahead of
  1083. * time when a more complete view of the write is available.
  1084. */
  1085. static void ocfs2_set_target_boundaries(struct ocfs2_super *osb,
  1086. struct ocfs2_write_ctxt *wc,
  1087. loff_t pos, unsigned len, int alloc)
  1088. {
  1089. struct ocfs2_write_cluster_desc *desc;
  1090. wc->w_target_from = pos & (PAGE_SIZE - 1);
  1091. wc->w_target_to = wc->w_target_from + len;
  1092. if (alloc == 0)
  1093. return;
  1094. /*
  1095. * Allocating write - we may have different boundaries based
  1096. * on page size and cluster size.
  1097. *
  1098. * NOTE: We can no longer compute one value from the other as
  1099. * the actual write length and user provided length may be
  1100. * different.
  1101. */
  1102. if (wc->w_large_pages) {
  1103. /*
  1104. * We only care about the 1st and last cluster within
  1105. * our range and whether they should be zero'd or not. Either
  1106. * value may be extended out to the start/end of a
  1107. * newly allocated cluster.
  1108. */
  1109. desc = &wc->w_desc[0];
  1110. if (desc->c_needs_zero)
  1111. ocfs2_figure_cluster_boundaries(osb,
  1112. desc->c_cpos,
  1113. &wc->w_target_from,
  1114. NULL);
  1115. desc = &wc->w_desc[wc->w_clen - 1];
  1116. if (desc->c_needs_zero)
  1117. ocfs2_figure_cluster_boundaries(osb,
  1118. desc->c_cpos,
  1119. NULL,
  1120. &wc->w_target_to);
  1121. } else {
  1122. wc->w_target_from = 0;
  1123. wc->w_target_to = PAGE_SIZE;
  1124. }
  1125. }
  1126. /*
  1127. * Check if this extent is marked UNWRITTEN by direct io. If so, we need not to
  1128. * do the zero work. And should not to clear UNWRITTEN since it will be cleared
  1129. * by the direct io procedure.
  1130. * If this is a new extent that allocated by direct io, we should mark it in
  1131. * the ip_unwritten_list.
  1132. */
  1133. static int ocfs2_unwritten_check(struct inode *inode,
  1134. struct ocfs2_write_ctxt *wc,
  1135. struct ocfs2_write_cluster_desc *desc)
  1136. {
  1137. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  1138. struct ocfs2_unwritten_extent *ue = NULL, *new = NULL;
  1139. int ret = 0;
  1140. if (!desc->c_needs_zero)
  1141. return 0;
  1142. retry:
  1143. spin_lock(&oi->ip_lock);
  1144. /* Needs not to zero no metter buffer or direct. The one who is zero
  1145. * the cluster is doing zero. And he will clear unwritten after all
  1146. * cluster io finished. */
  1147. list_for_each_entry(ue, &oi->ip_unwritten_list, ue_ip_node) {
  1148. if (desc->c_cpos == ue->ue_cpos) {
  1149. BUG_ON(desc->c_new);
  1150. desc->c_needs_zero = 0;
  1151. desc->c_clear_unwritten = 0;
  1152. goto unlock;
  1153. }
  1154. }
  1155. if (wc->w_type != OCFS2_WRITE_DIRECT)
  1156. goto unlock;
  1157. if (new == NULL) {
  1158. spin_unlock(&oi->ip_lock);
  1159. new = kmalloc(sizeof(struct ocfs2_unwritten_extent),
  1160. GFP_NOFS);
  1161. if (new == NULL) {
  1162. ret = -ENOMEM;
  1163. goto out;
  1164. }
  1165. goto retry;
  1166. }
  1167. /* This direct write will doing zero. */
  1168. new->ue_cpos = desc->c_cpos;
  1169. new->ue_phys = desc->c_phys;
  1170. desc->c_clear_unwritten = 0;
  1171. list_add_tail(&new->ue_ip_node, &oi->ip_unwritten_list);
  1172. list_add_tail(&new->ue_node, &wc->w_unwritten_list);
  1173. wc->w_unwritten_count++;
  1174. new = NULL;
  1175. unlock:
  1176. spin_unlock(&oi->ip_lock);
  1177. out:
  1178. kfree(new);
  1179. return ret;
  1180. }
  1181. /*
  1182. * Populate each single-cluster write descriptor in the write context
  1183. * with information about the i/o to be done.
  1184. *
  1185. * Returns the number of clusters that will have to be allocated, as
  1186. * well as a worst case estimate of the number of extent records that
  1187. * would have to be created during a write to an unwritten region.
  1188. */
  1189. static int ocfs2_populate_write_desc(struct inode *inode,
  1190. struct ocfs2_write_ctxt *wc,
  1191. unsigned int *clusters_to_alloc,
  1192. unsigned int *extents_to_split)
  1193. {
  1194. int ret;
  1195. struct ocfs2_write_cluster_desc *desc;
  1196. unsigned int num_clusters = 0;
  1197. unsigned int ext_flags = 0;
  1198. u32 phys = 0;
  1199. int i;
  1200. *clusters_to_alloc = 0;
  1201. *extents_to_split = 0;
  1202. for (i = 0; i < wc->w_clen; i++) {
  1203. desc = &wc->w_desc[i];
  1204. desc->c_cpos = wc->w_cpos + i;
  1205. if (num_clusters == 0) {
  1206. /*
  1207. * Need to look up the next extent record.
  1208. */
  1209. ret = ocfs2_get_clusters(inode, desc->c_cpos, &phys,
  1210. &num_clusters, &ext_flags);
  1211. if (ret) {
  1212. mlog_errno(ret);
  1213. goto out;
  1214. }
  1215. /* We should already CoW the refcountd extent. */
  1216. BUG_ON(ext_flags & OCFS2_EXT_REFCOUNTED);
  1217. /*
  1218. * Assume worst case - that we're writing in
  1219. * the middle of the extent.
  1220. *
  1221. * We can assume that the write proceeds from
  1222. * left to right, in which case the extent
  1223. * insert code is smart enough to coalesce the
  1224. * next splits into the previous records created.
  1225. */
  1226. if (ext_flags & OCFS2_EXT_UNWRITTEN)
  1227. *extents_to_split = *extents_to_split + 2;
  1228. } else if (phys) {
  1229. /*
  1230. * Only increment phys if it doesn't describe
  1231. * a hole.
  1232. */
  1233. phys++;
  1234. }
  1235. /*
  1236. * If w_first_new_cpos is < UINT_MAX, we have a non-sparse
  1237. * file that got extended. w_first_new_cpos tells us
  1238. * where the newly allocated clusters are so we can
  1239. * zero them.
  1240. */
  1241. if (desc->c_cpos >= wc->w_first_new_cpos) {
  1242. BUG_ON(phys == 0);
  1243. desc->c_needs_zero = 1;
  1244. }
  1245. desc->c_phys = phys;
  1246. if (phys == 0) {
  1247. desc->c_new = 1;
  1248. desc->c_needs_zero = 1;
  1249. desc->c_clear_unwritten = 1;
  1250. *clusters_to_alloc = *clusters_to_alloc + 1;
  1251. }
  1252. if (ext_flags & OCFS2_EXT_UNWRITTEN) {
  1253. desc->c_clear_unwritten = 1;
  1254. desc->c_needs_zero = 1;
  1255. }
  1256. ret = ocfs2_unwritten_check(inode, wc, desc);
  1257. if (ret) {
  1258. mlog_errno(ret);
  1259. goto out;
  1260. }
  1261. num_clusters--;
  1262. }
  1263. ret = 0;
  1264. out:
  1265. return ret;
  1266. }
  1267. static int ocfs2_write_begin_inline(struct address_space *mapping,
  1268. struct inode *inode,
  1269. struct ocfs2_write_ctxt *wc)
  1270. {
  1271. int ret;
  1272. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1273. struct page *page;
  1274. handle_t *handle;
  1275. struct ocfs2_dinode *di = (struct ocfs2_dinode *)wc->w_di_bh->b_data;
  1276. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  1277. if (IS_ERR(handle)) {
  1278. ret = PTR_ERR(handle);
  1279. mlog_errno(ret);
  1280. goto out;
  1281. }
  1282. page = find_or_create_page(mapping, 0, GFP_NOFS);
  1283. if (!page) {
  1284. ocfs2_commit_trans(osb, handle);
  1285. ret = -ENOMEM;
  1286. mlog_errno(ret);
  1287. goto out;
  1288. }
  1289. /*
  1290. * If we don't set w_num_pages then this page won't get unlocked
  1291. * and freed on cleanup of the write context.
  1292. */
  1293. wc->w_pages[0] = wc->w_target_page = page;
  1294. wc->w_num_pages = 1;
  1295. ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), wc->w_di_bh,
  1296. OCFS2_JOURNAL_ACCESS_WRITE);
  1297. if (ret) {
  1298. ocfs2_commit_trans(osb, handle);
  1299. mlog_errno(ret);
  1300. goto out;
  1301. }
  1302. if (!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL))
  1303. ocfs2_set_inode_data_inline(inode, di);
  1304. if (!PageUptodate(page)) {
  1305. ret = ocfs2_read_inline_data(inode, page, wc->w_di_bh);
  1306. if (ret) {
  1307. ocfs2_commit_trans(osb, handle);
  1308. goto out;
  1309. }
  1310. }
  1311. wc->w_handle = handle;
  1312. out:
  1313. return ret;
  1314. }
  1315. int ocfs2_size_fits_inline_data(struct buffer_head *di_bh, u64 new_size)
  1316. {
  1317. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  1318. if (new_size <= le16_to_cpu(di->id2.i_data.id_count))
  1319. return 1;
  1320. return 0;
  1321. }
  1322. static int ocfs2_try_to_write_inline_data(struct address_space *mapping,
  1323. struct inode *inode, loff_t pos,
  1324. unsigned len, struct page *mmap_page,
  1325. struct ocfs2_write_ctxt *wc)
  1326. {
  1327. int ret, written = 0;
  1328. loff_t end = pos + len;
  1329. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  1330. struct ocfs2_dinode *di = NULL;
  1331. trace_ocfs2_try_to_write_inline_data((unsigned long long)oi->ip_blkno,
  1332. len, (unsigned long long)pos,
  1333. oi->ip_dyn_features);
  1334. /*
  1335. * Handle inodes which already have inline data 1st.
  1336. */
  1337. if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  1338. if (mmap_page == NULL &&
  1339. ocfs2_size_fits_inline_data(wc->w_di_bh, end))
  1340. goto do_inline_write;
  1341. /*
  1342. * The write won't fit - we have to give this inode an
  1343. * inline extent list now.
  1344. */
  1345. ret = ocfs2_convert_inline_data_to_extents(inode, wc->w_di_bh);
  1346. if (ret)
  1347. mlog_errno(ret);
  1348. goto out;
  1349. }
  1350. /*
  1351. * Check whether the inode can accept inline data.
  1352. */
  1353. if (oi->ip_clusters != 0 || i_size_read(inode) != 0)
  1354. return 0;
  1355. /*
  1356. * Check whether the write can fit.
  1357. */
  1358. di = (struct ocfs2_dinode *)wc->w_di_bh->b_data;
  1359. if (mmap_page ||
  1360. end > ocfs2_max_inline_data_with_xattr(inode->i_sb, di))
  1361. return 0;
  1362. do_inline_write:
  1363. ret = ocfs2_write_begin_inline(mapping, inode, wc);
  1364. if (ret) {
  1365. mlog_errno(ret);
  1366. goto out;
  1367. }
  1368. /*
  1369. * This signals to the caller that the data can be written
  1370. * inline.
  1371. */
  1372. written = 1;
  1373. out:
  1374. return written ? written : ret;
  1375. }
  1376. /*
  1377. * This function only does anything for file systems which can't
  1378. * handle sparse files.
  1379. *
  1380. * What we want to do here is fill in any hole between the current end
  1381. * of allocation and the end of our write. That way the rest of the
  1382. * write path can treat it as an non-allocating write, which has no
  1383. * special case code for sparse/nonsparse files.
  1384. */
  1385. static int ocfs2_expand_nonsparse_inode(struct inode *inode,
  1386. struct buffer_head *di_bh,
  1387. loff_t pos, unsigned len,
  1388. struct ocfs2_write_ctxt *wc)
  1389. {
  1390. int ret;
  1391. loff_t newsize = pos + len;
  1392. BUG_ON(ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb)));
  1393. if (newsize <= i_size_read(inode))
  1394. return 0;
  1395. ret = ocfs2_extend_no_holes(inode, di_bh, newsize, pos);
  1396. if (ret)
  1397. mlog_errno(ret);
  1398. /* There is no wc if this is call from direct. */
  1399. if (wc)
  1400. wc->w_first_new_cpos =
  1401. ocfs2_clusters_for_bytes(inode->i_sb, i_size_read(inode));
  1402. return ret;
  1403. }
  1404. static int ocfs2_zero_tail(struct inode *inode, struct buffer_head *di_bh,
  1405. loff_t pos)
  1406. {
  1407. int ret = 0;
  1408. BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb)));
  1409. if (pos > i_size_read(inode))
  1410. ret = ocfs2_zero_extend(inode, di_bh, pos);
  1411. return ret;
  1412. }
  1413. int ocfs2_write_begin_nolock(struct address_space *mapping,
  1414. loff_t pos, unsigned len, ocfs2_write_type_t type,
  1415. struct page **pagep, void **fsdata,
  1416. struct buffer_head *di_bh, struct page *mmap_page)
  1417. {
  1418. int ret, cluster_of_pages, credits = OCFS2_INODE_UPDATE_CREDITS;
  1419. unsigned int clusters_to_alloc, extents_to_split, clusters_need = 0;
  1420. struct ocfs2_write_ctxt *wc;
  1421. struct inode *inode = mapping->host;
  1422. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1423. struct ocfs2_dinode *di;
  1424. struct ocfs2_alloc_context *data_ac = NULL;
  1425. struct ocfs2_alloc_context *meta_ac = NULL;
  1426. handle_t *handle;
  1427. struct ocfs2_extent_tree et;
  1428. int try_free = 1, ret1;
  1429. try_again:
  1430. ret = ocfs2_alloc_write_ctxt(&wc, osb, pos, len, type, di_bh);
  1431. if (ret) {
  1432. mlog_errno(ret);
  1433. return ret;
  1434. }
  1435. if (ocfs2_supports_inline_data(osb)) {
  1436. ret = ocfs2_try_to_write_inline_data(mapping, inode, pos, len,
  1437. mmap_page, wc);
  1438. if (ret == 1) {
  1439. ret = 0;
  1440. goto success;
  1441. }
  1442. if (ret < 0) {
  1443. mlog_errno(ret);
  1444. goto out;
  1445. }
  1446. }
  1447. /* Direct io change i_size late, should not zero tail here. */
  1448. if (type != OCFS2_WRITE_DIRECT) {
  1449. if (ocfs2_sparse_alloc(osb))
  1450. ret = ocfs2_zero_tail(inode, di_bh, pos);
  1451. else
  1452. ret = ocfs2_expand_nonsparse_inode(inode, di_bh, pos,
  1453. len, wc);
  1454. if (ret) {
  1455. mlog_errno(ret);
  1456. goto out;
  1457. }
  1458. }
  1459. ret = ocfs2_check_range_for_refcount(inode, pos, len);
  1460. if (ret < 0) {
  1461. mlog_errno(ret);
  1462. goto out;
  1463. } else if (ret == 1) {
  1464. clusters_need = wc->w_clen;
  1465. ret = ocfs2_refcount_cow(inode, di_bh,
  1466. wc->w_cpos, wc->w_clen, UINT_MAX);
  1467. if (ret) {
  1468. mlog_errno(ret);
  1469. goto out;
  1470. }
  1471. }
  1472. ret = ocfs2_populate_write_desc(inode, wc, &clusters_to_alloc,
  1473. &extents_to_split);
  1474. if (ret) {
  1475. mlog_errno(ret);
  1476. goto out;
  1477. }
  1478. clusters_need += clusters_to_alloc;
  1479. di = (struct ocfs2_dinode *)wc->w_di_bh->b_data;
  1480. trace_ocfs2_write_begin_nolock(
  1481. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  1482. (long long)i_size_read(inode),
  1483. le32_to_cpu(di->i_clusters),
  1484. pos, len, type, mmap_page,
  1485. clusters_to_alloc, extents_to_split);
  1486. /*
  1487. * We set w_target_from, w_target_to here so that
  1488. * ocfs2_write_end() knows which range in the target page to
  1489. * write out. An allocation requires that we write the entire
  1490. * cluster range.
  1491. */
  1492. if (clusters_to_alloc || extents_to_split) {
  1493. /*
  1494. * XXX: We are stretching the limits of
  1495. * ocfs2_lock_allocators(). It greatly over-estimates
  1496. * the work to be done.
  1497. */
  1498. ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(inode),
  1499. wc->w_di_bh);
  1500. ret = ocfs2_lock_allocators(inode, &et,
  1501. clusters_to_alloc, extents_to_split,
  1502. &data_ac, &meta_ac);
  1503. if (ret) {
  1504. mlog_errno(ret);
  1505. goto out;
  1506. }
  1507. if (data_ac)
  1508. data_ac->ac_resv = &OCFS2_I(inode)->ip_la_data_resv;
  1509. credits = ocfs2_calc_extend_credits(inode->i_sb,
  1510. &di->id2.i_list);
  1511. } else if (type == OCFS2_WRITE_DIRECT)
  1512. /* direct write needs not to start trans if no extents alloc. */
  1513. goto success;
  1514. /*
  1515. * We have to zero sparse allocated clusters, unwritten extent clusters,
  1516. * and non-sparse clusters we just extended. For non-sparse writes,
  1517. * we know zeros will only be needed in the first and/or last cluster.
  1518. */
  1519. if (wc->w_clen && (wc->w_desc[0].c_needs_zero ||
  1520. wc->w_desc[wc->w_clen - 1].c_needs_zero))
  1521. cluster_of_pages = 1;
  1522. else
  1523. cluster_of_pages = 0;
  1524. ocfs2_set_target_boundaries(osb, wc, pos, len, cluster_of_pages);
  1525. handle = ocfs2_start_trans(osb, credits);
  1526. if (IS_ERR(handle)) {
  1527. ret = PTR_ERR(handle);
  1528. mlog_errno(ret);
  1529. goto out;
  1530. }
  1531. wc->w_handle = handle;
  1532. if (clusters_to_alloc) {
  1533. ret = dquot_alloc_space_nodirty(inode,
  1534. ocfs2_clusters_to_bytes(osb->sb, clusters_to_alloc));
  1535. if (ret)
  1536. goto out_commit;
  1537. }
  1538. ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), wc->w_di_bh,
  1539. OCFS2_JOURNAL_ACCESS_WRITE);
  1540. if (ret) {
  1541. mlog_errno(ret);
  1542. goto out_quota;
  1543. }
  1544. /*
  1545. * Fill our page array first. That way we've grabbed enough so
  1546. * that we can zero and flush if we error after adding the
  1547. * extent.
  1548. */
  1549. ret = ocfs2_grab_pages_for_write(mapping, wc, wc->w_cpos, pos, len,
  1550. cluster_of_pages, mmap_page);
  1551. if (ret && ret != -EAGAIN) {
  1552. mlog_errno(ret);
  1553. goto out_quota;
  1554. }
  1555. /*
  1556. * ocfs2_grab_pages_for_write() returns -EAGAIN if it could not lock
  1557. * the target page. In this case, we exit with no error and no target
  1558. * page. This will trigger the caller, page_mkwrite(), to re-try
  1559. * the operation.
  1560. */
  1561. if (ret == -EAGAIN) {
  1562. BUG_ON(wc->w_target_page);
  1563. ret = 0;
  1564. goto out_quota;
  1565. }
  1566. ret = ocfs2_write_cluster_by_desc(mapping, data_ac, meta_ac, wc, pos,
  1567. len);
  1568. if (ret) {
  1569. mlog_errno(ret);
  1570. goto out_quota;
  1571. }
  1572. if (data_ac)
  1573. ocfs2_free_alloc_context(data_ac);
  1574. if (meta_ac)
  1575. ocfs2_free_alloc_context(meta_ac);
  1576. success:
  1577. if (pagep)
  1578. *pagep = wc->w_target_page;
  1579. *fsdata = wc;
  1580. return 0;
  1581. out_quota:
  1582. if (clusters_to_alloc)
  1583. dquot_free_space(inode,
  1584. ocfs2_clusters_to_bytes(osb->sb, clusters_to_alloc));
  1585. out_commit:
  1586. ocfs2_commit_trans(osb, handle);
  1587. out:
  1588. /*
  1589. * The mmapped page won't be unlocked in ocfs2_free_write_ctxt(),
  1590. * even in case of error here like ENOSPC and ENOMEM. So, we need
  1591. * to unlock the target page manually to prevent deadlocks when
  1592. * retrying again on ENOSPC, or when returning non-VM_FAULT_LOCKED
  1593. * to VM code.
  1594. */
  1595. if (wc->w_target_locked)
  1596. unlock_page(mmap_page);
  1597. ocfs2_free_write_ctxt(inode, wc);
  1598. if (data_ac) {
  1599. ocfs2_free_alloc_context(data_ac);
  1600. data_ac = NULL;
  1601. }
  1602. if (meta_ac) {
  1603. ocfs2_free_alloc_context(meta_ac);
  1604. meta_ac = NULL;
  1605. }
  1606. if (ret == -ENOSPC && try_free) {
  1607. /*
  1608. * Try to free some truncate log so that we can have enough
  1609. * clusters to allocate.
  1610. */
  1611. try_free = 0;
  1612. ret1 = ocfs2_try_to_free_truncate_log(osb, clusters_need);
  1613. if (ret1 == 1)
  1614. goto try_again;
  1615. if (ret1 < 0)
  1616. mlog_errno(ret1);
  1617. }
  1618. return ret;
  1619. }
  1620. static int ocfs2_write_begin(struct file *file, struct address_space *mapping,
  1621. loff_t pos, unsigned len, unsigned flags,
  1622. struct page **pagep, void **fsdata)
  1623. {
  1624. int ret;
  1625. struct buffer_head *di_bh = NULL;
  1626. struct inode *inode = mapping->host;
  1627. ret = ocfs2_inode_lock(inode, &di_bh, 1);
  1628. if (ret) {
  1629. mlog_errno(ret);
  1630. return ret;
  1631. }
  1632. /*
  1633. * Take alloc sem here to prevent concurrent lookups. That way
  1634. * the mapping, zeroing and tree manipulation within
  1635. * ocfs2_write() will be safe against ->readpage(). This
  1636. * should also serve to lock out allocation from a shared
  1637. * writeable region.
  1638. */
  1639. down_write(&OCFS2_I(inode)->ip_alloc_sem);
  1640. ret = ocfs2_write_begin_nolock(mapping, pos, len, OCFS2_WRITE_BUFFER,
  1641. pagep, fsdata, di_bh, NULL);
  1642. if (ret) {
  1643. mlog_errno(ret);
  1644. goto out_fail;
  1645. }
  1646. brelse(di_bh);
  1647. return 0;
  1648. out_fail:
  1649. up_write(&OCFS2_I(inode)->ip_alloc_sem);
  1650. brelse(di_bh);
  1651. ocfs2_inode_unlock(inode, 1);
  1652. return ret;
  1653. }
  1654. static void ocfs2_write_end_inline(struct inode *inode, loff_t pos,
  1655. unsigned len, unsigned *copied,
  1656. struct ocfs2_dinode *di,
  1657. struct ocfs2_write_ctxt *wc)
  1658. {
  1659. void *kaddr;
  1660. if (unlikely(*copied < len)) {
  1661. if (!PageUptodate(wc->w_target_page)) {
  1662. *copied = 0;
  1663. return;
  1664. }
  1665. }
  1666. kaddr = kmap_atomic(wc->w_target_page);
  1667. memcpy(di->id2.i_data.id_data + pos, kaddr + pos, *copied);
  1668. kunmap_atomic(kaddr);
  1669. trace_ocfs2_write_end_inline(
  1670. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  1671. (unsigned long long)pos, *copied,
  1672. le16_to_cpu(di->id2.i_data.id_count),
  1673. le16_to_cpu(di->i_dyn_features));
  1674. }
  1675. int ocfs2_write_end_nolock(struct address_space *mapping,
  1676. loff_t pos, unsigned len, unsigned copied, void *fsdata)
  1677. {
  1678. int i, ret;
  1679. unsigned from, to, start = pos & (PAGE_SIZE - 1);
  1680. struct inode *inode = mapping->host;
  1681. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1682. struct ocfs2_write_ctxt *wc = fsdata;
  1683. struct ocfs2_dinode *di = (struct ocfs2_dinode *)wc->w_di_bh->b_data;
  1684. handle_t *handle = wc->w_handle;
  1685. struct page *tmppage;
  1686. BUG_ON(!list_empty(&wc->w_unwritten_list));
  1687. if (handle) {
  1688. ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode),
  1689. wc->w_di_bh, OCFS2_JOURNAL_ACCESS_WRITE);
  1690. if (ret) {
  1691. copied = ret;
  1692. mlog_errno(ret);
  1693. goto out;
  1694. }
  1695. }
  1696. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  1697. ocfs2_write_end_inline(inode, pos, len, &copied, di, wc);
  1698. goto out_write_size;
  1699. }
  1700. if (unlikely(copied < len) && wc->w_target_page) {
  1701. if (!PageUptodate(wc->w_target_page))
  1702. copied = 0;
  1703. ocfs2_zero_new_buffers(wc->w_target_page, start+copied,
  1704. start+len);
  1705. }
  1706. if (wc->w_target_page)
  1707. flush_dcache_page(wc->w_target_page);
  1708. for(i = 0; i < wc->w_num_pages; i++) {
  1709. tmppage = wc->w_pages[i];
  1710. /* This is the direct io target page. */
  1711. if (tmppage == NULL)
  1712. continue;
  1713. if (tmppage == wc->w_target_page) {
  1714. from = wc->w_target_from;
  1715. to = wc->w_target_to;
  1716. BUG_ON(from > PAGE_SIZE ||
  1717. to > PAGE_SIZE ||
  1718. to < from);
  1719. } else {
  1720. /*
  1721. * Pages adjacent to the target (if any) imply
  1722. * a hole-filling write in which case we want
  1723. * to flush their entire range.
  1724. */
  1725. from = 0;
  1726. to = PAGE_SIZE;
  1727. }
  1728. if (page_has_buffers(tmppage)) {
  1729. if (handle && ocfs2_should_order_data(inode)) {
  1730. loff_t start_byte =
  1731. ((loff_t)tmppage->index << PAGE_SHIFT) +
  1732. from;
  1733. loff_t length = to - from;
  1734. ocfs2_jbd2_inode_add_write(handle, inode,
  1735. start_byte, length);
  1736. }
  1737. block_commit_write(tmppage, from, to);
  1738. }
  1739. }
  1740. out_write_size:
  1741. /* Direct io do not update i_size here. */
  1742. if (wc->w_type != OCFS2_WRITE_DIRECT) {
  1743. pos += copied;
  1744. if (pos > i_size_read(inode)) {
  1745. i_size_write(inode, pos);
  1746. mark_inode_dirty(inode);
  1747. }
  1748. inode->i_blocks = ocfs2_inode_sector_count(inode);
  1749. di->i_size = cpu_to_le64((u64)i_size_read(inode));
  1750. inode->i_mtime = inode->i_ctime = current_time(inode);
  1751. di->i_mtime = di->i_ctime = cpu_to_le64(inode->i_mtime.tv_sec);
  1752. di->i_mtime_nsec = di->i_ctime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);
  1753. if (handle)
  1754. ocfs2_update_inode_fsync_trans(handle, inode, 1);
  1755. }
  1756. if (handle)
  1757. ocfs2_journal_dirty(handle, wc->w_di_bh);
  1758. out:
  1759. /* unlock pages before dealloc since it needs acquiring j_trans_barrier
  1760. * lock, or it will cause a deadlock since journal commit threads holds
  1761. * this lock and will ask for the page lock when flushing the data.
  1762. * put it here to preserve the unlock order.
  1763. */
  1764. ocfs2_unlock_pages(wc);
  1765. if (handle)
  1766. ocfs2_commit_trans(osb, handle);
  1767. ocfs2_run_deallocs(osb, &wc->w_dealloc);
  1768. brelse(wc->w_di_bh);
  1769. kfree(wc);
  1770. return copied;
  1771. }
  1772. static int ocfs2_write_end(struct file *file, struct address_space *mapping,
  1773. loff_t pos, unsigned len, unsigned copied,
  1774. struct page *page, void *fsdata)
  1775. {
  1776. int ret;
  1777. struct inode *inode = mapping->host;
  1778. ret = ocfs2_write_end_nolock(mapping, pos, len, copied, fsdata);
  1779. up_write(&OCFS2_I(inode)->ip_alloc_sem);
  1780. ocfs2_inode_unlock(inode, 1);
  1781. return ret;
  1782. }
  1783. struct ocfs2_dio_write_ctxt {
  1784. struct list_head dw_zero_list;
  1785. unsigned dw_zero_count;
  1786. int dw_orphaned;
  1787. pid_t dw_writer_pid;
  1788. };
  1789. static struct ocfs2_dio_write_ctxt *
  1790. ocfs2_dio_alloc_write_ctx(struct buffer_head *bh, int *alloc)
  1791. {
  1792. struct ocfs2_dio_write_ctxt *dwc = NULL;
  1793. if (bh->b_private)
  1794. return bh->b_private;
  1795. dwc = kmalloc(sizeof(struct ocfs2_dio_write_ctxt), GFP_NOFS);
  1796. if (dwc == NULL)
  1797. return NULL;
  1798. INIT_LIST_HEAD(&dwc->dw_zero_list);
  1799. dwc->dw_zero_count = 0;
  1800. dwc->dw_orphaned = 0;
  1801. dwc->dw_writer_pid = task_pid_nr(current);
  1802. bh->b_private = dwc;
  1803. *alloc = 1;
  1804. return dwc;
  1805. }
  1806. static void ocfs2_dio_free_write_ctx(struct inode *inode,
  1807. struct ocfs2_dio_write_ctxt *dwc)
  1808. {
  1809. ocfs2_free_unwritten_list(inode, &dwc->dw_zero_list);
  1810. kfree(dwc);
  1811. }
  1812. /*
  1813. * TODO: Make this into a generic get_blocks function.
  1814. *
  1815. * From do_direct_io in direct-io.c:
  1816. * "So what we do is to permit the ->get_blocks function to populate
  1817. * bh.b_size with the size of IO which is permitted at this offset and
  1818. * this i_blkbits."
  1819. *
  1820. * This function is called directly from get_more_blocks in direct-io.c.
  1821. *
  1822. * called like this: dio->get_blocks(dio->inode, fs_startblk,
  1823. * fs_count, map_bh, dio->rw == WRITE);
  1824. */
  1825. static int ocfs2_dio_wr_get_block(struct inode *inode, sector_t iblock,
  1826. struct buffer_head *bh_result, int create)
  1827. {
  1828. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1829. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  1830. struct ocfs2_write_ctxt *wc;
  1831. struct ocfs2_write_cluster_desc *desc = NULL;
  1832. struct ocfs2_dio_write_ctxt *dwc = NULL;
  1833. struct buffer_head *di_bh = NULL;
  1834. u64 p_blkno;
  1835. unsigned int i_blkbits = inode->i_sb->s_blocksize_bits;
  1836. loff_t pos = iblock << i_blkbits;
  1837. sector_t endblk = (i_size_read(inode) - 1) >> i_blkbits;
  1838. unsigned len, total_len = bh_result->b_size;
  1839. int ret = 0, first_get_block = 0;
  1840. len = osb->s_clustersize - (pos & (osb->s_clustersize - 1));
  1841. len = min(total_len, len);
  1842. /*
  1843. * bh_result->b_size is count in get_more_blocks according to write
  1844. * "pos" and "end", we need map twice to return different buffer state:
  1845. * 1. area in file size, not set NEW;
  1846. * 2. area out file size, set NEW.
  1847. *
  1848. * iblock endblk
  1849. * |--------|---------|---------|---------
  1850. * |<-------area in file------->|
  1851. */
  1852. if ((iblock <= endblk) &&
  1853. ((iblock + ((len - 1) >> i_blkbits)) > endblk))
  1854. len = (endblk - iblock + 1) << i_blkbits;
  1855. mlog(0, "get block of %lu at %llu:%u req %u\n",
  1856. inode->i_ino, pos, len, total_len);
  1857. /*
  1858. * Because we need to change file size in ocfs2_dio_end_io_write(), or
  1859. * we may need to add it to orphan dir. So can not fall to fast path
  1860. * while file size will be changed.
  1861. */
  1862. if (pos + total_len <= i_size_read(inode)) {
  1863. /* This is the fast path for re-write. */
  1864. ret = ocfs2_lock_get_block(inode, iblock, bh_result, create);
  1865. if (buffer_mapped(bh_result) &&
  1866. !buffer_new(bh_result) &&
  1867. ret == 0)
  1868. goto out;
  1869. /* Clear state set by ocfs2_get_block. */
  1870. bh_result->b_state = 0;
  1871. }
  1872. dwc = ocfs2_dio_alloc_write_ctx(bh_result, &first_get_block);
  1873. if (unlikely(dwc == NULL)) {
  1874. ret = -ENOMEM;
  1875. mlog_errno(ret);
  1876. goto out;
  1877. }
  1878. if (ocfs2_clusters_for_bytes(inode->i_sb, pos + total_len) >
  1879. ocfs2_clusters_for_bytes(inode->i_sb, i_size_read(inode)) &&
  1880. !dwc->dw_orphaned) {
  1881. /*
  1882. * when we are going to alloc extents beyond file size, add the
  1883. * inode to orphan dir, so we can recall those spaces when
  1884. * system crashed during write.
  1885. */
  1886. ret = ocfs2_add_inode_to_orphan(osb, inode);
  1887. if (ret < 0) {
  1888. mlog_errno(ret);
  1889. goto out;
  1890. }
  1891. dwc->dw_orphaned = 1;
  1892. }
  1893. ret = ocfs2_inode_lock(inode, &di_bh, 1);
  1894. if (ret) {
  1895. mlog_errno(ret);
  1896. goto out;
  1897. }
  1898. down_write(&oi->ip_alloc_sem);
  1899. if (first_get_block) {
  1900. if (ocfs2_sparse_alloc(osb))
  1901. ret = ocfs2_zero_tail(inode, di_bh, pos);
  1902. else
  1903. ret = ocfs2_expand_nonsparse_inode(inode, di_bh, pos,
  1904. total_len, NULL);
  1905. if (ret < 0) {
  1906. mlog_errno(ret);
  1907. goto unlock;
  1908. }
  1909. }
  1910. ret = ocfs2_write_begin_nolock(inode->i_mapping, pos, len,
  1911. OCFS2_WRITE_DIRECT, NULL,
  1912. (void **)&wc, di_bh, NULL);
  1913. if (ret) {
  1914. mlog_errno(ret);
  1915. goto unlock;
  1916. }
  1917. desc = &wc->w_desc[0];
  1918. p_blkno = ocfs2_clusters_to_blocks(inode->i_sb, desc->c_phys);
  1919. BUG_ON(p_blkno == 0);
  1920. p_blkno += iblock & (u64)(ocfs2_clusters_to_blocks(inode->i_sb, 1) - 1);
  1921. map_bh(bh_result, inode->i_sb, p_blkno);
  1922. bh_result->b_size = len;
  1923. if (desc->c_needs_zero)
  1924. set_buffer_new(bh_result);
  1925. if (iblock > endblk)
  1926. set_buffer_new(bh_result);
  1927. /* May sleep in end_io. It should not happen in a irq context. So defer
  1928. * it to dio work queue. */
  1929. set_buffer_defer_completion(bh_result);
  1930. if (!list_empty(&wc->w_unwritten_list)) {
  1931. struct ocfs2_unwritten_extent *ue = NULL;
  1932. ue = list_first_entry(&wc->w_unwritten_list,
  1933. struct ocfs2_unwritten_extent,
  1934. ue_node);
  1935. BUG_ON(ue->ue_cpos != desc->c_cpos);
  1936. /* The physical address may be 0, fill it. */
  1937. ue->ue_phys = desc->c_phys;
  1938. list_splice_tail_init(&wc->w_unwritten_list, &dwc->dw_zero_list);
  1939. dwc->dw_zero_count += wc->w_unwritten_count;
  1940. }
  1941. ret = ocfs2_write_end_nolock(inode->i_mapping, pos, len, len, wc);
  1942. BUG_ON(ret != len);
  1943. ret = 0;
  1944. unlock:
  1945. up_write(&oi->ip_alloc_sem);
  1946. ocfs2_inode_unlock(inode, 1);
  1947. brelse(di_bh);
  1948. out:
  1949. if (ret < 0)
  1950. ret = -EIO;
  1951. return ret;
  1952. }
  1953. static int ocfs2_dio_end_io_write(struct inode *inode,
  1954. struct ocfs2_dio_write_ctxt *dwc,
  1955. loff_t offset,
  1956. ssize_t bytes)
  1957. {
  1958. struct ocfs2_cached_dealloc_ctxt dealloc;
  1959. struct ocfs2_extent_tree et;
  1960. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1961. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  1962. struct ocfs2_unwritten_extent *ue = NULL;
  1963. struct buffer_head *di_bh = NULL;
  1964. struct ocfs2_dinode *di;
  1965. struct ocfs2_alloc_context *data_ac = NULL;
  1966. struct ocfs2_alloc_context *meta_ac = NULL;
  1967. handle_t *handle = NULL;
  1968. loff_t end = offset + bytes;
  1969. int ret = 0, credits = 0;
  1970. ocfs2_init_dealloc_ctxt(&dealloc);
  1971. /* We do clear unwritten, delete orphan, change i_size here. If neither
  1972. * of these happen, we can skip all this. */
  1973. if (list_empty(&dwc->dw_zero_list) &&
  1974. end <= i_size_read(inode) &&
  1975. !dwc->dw_orphaned)
  1976. goto out;
  1977. ret = ocfs2_inode_lock(inode, &di_bh, 1);
  1978. if (ret < 0) {
  1979. mlog_errno(ret);
  1980. goto out;
  1981. }
  1982. down_write(&oi->ip_alloc_sem);
  1983. /* Delete orphan before acquire i_mutex. */
  1984. if (dwc->dw_orphaned) {
  1985. BUG_ON(dwc->dw_writer_pid != task_pid_nr(current));
  1986. end = end > i_size_read(inode) ? end : 0;
  1987. ret = ocfs2_del_inode_from_orphan(osb, inode, di_bh,
  1988. !!end, end);
  1989. if (ret < 0)
  1990. mlog_errno(ret);
  1991. }
  1992. di = (struct ocfs2_dinode *)di_bh->b_data;
  1993. ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(inode), di_bh);
  1994. /* Attach dealloc with extent tree in case that we may reuse extents
  1995. * which are already unlinked from current extent tree due to extent
  1996. * rotation and merging.
  1997. */
  1998. et.et_dealloc = &dealloc;
  1999. ret = ocfs2_lock_allocators(inode, &et, 0, dwc->dw_zero_count*2,
  2000. &data_ac, &meta_ac);
  2001. if (ret) {
  2002. mlog_errno(ret);
  2003. goto unlock;
  2004. }
  2005. credits = ocfs2_calc_extend_credits(inode->i_sb, &di->id2.i_list);
  2006. handle = ocfs2_start_trans(osb, credits);
  2007. if (IS_ERR(handle)) {
  2008. ret = PTR_ERR(handle);
  2009. mlog_errno(ret);
  2010. goto unlock;
  2011. }
  2012. ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
  2013. OCFS2_JOURNAL_ACCESS_WRITE);
  2014. if (ret) {
  2015. mlog_errno(ret);
  2016. goto commit;
  2017. }
  2018. list_for_each_entry(ue, &dwc->dw_zero_list, ue_node) {
  2019. ret = ocfs2_mark_extent_written(inode, &et, handle,
  2020. ue->ue_cpos, 1,
  2021. ue->ue_phys,
  2022. meta_ac, &dealloc);
  2023. if (ret < 0) {
  2024. mlog_errno(ret);
  2025. break;
  2026. }
  2027. }
  2028. if (end > i_size_read(inode)) {
  2029. ret = ocfs2_set_inode_size(handle, inode, di_bh, end);
  2030. if (ret < 0)
  2031. mlog_errno(ret);
  2032. }
  2033. commit:
  2034. ocfs2_commit_trans(osb, handle);
  2035. unlock:
  2036. up_write(&oi->ip_alloc_sem);
  2037. ocfs2_inode_unlock(inode, 1);
  2038. brelse(di_bh);
  2039. out:
  2040. if (data_ac)
  2041. ocfs2_free_alloc_context(data_ac);
  2042. if (meta_ac)
  2043. ocfs2_free_alloc_context(meta_ac);
  2044. ocfs2_run_deallocs(osb, &dealloc);
  2045. ocfs2_dio_free_write_ctx(inode, dwc);
  2046. return ret;
  2047. }
  2048. /*
  2049. * ocfs2_dio_end_io is called by the dio core when a dio is finished. We're
  2050. * particularly interested in the aio/dio case. We use the rw_lock DLM lock
  2051. * to protect io on one node from truncation on another.
  2052. */
  2053. static int ocfs2_dio_end_io(struct kiocb *iocb,
  2054. loff_t offset,
  2055. ssize_t bytes,
  2056. void *private)
  2057. {
  2058. struct inode *inode = file_inode(iocb->ki_filp);
  2059. int level;
  2060. int ret = 0;
  2061. /* this io's submitter should not have unlocked this before we could */
  2062. BUG_ON(!ocfs2_iocb_is_rw_locked(iocb));
  2063. if (bytes <= 0)
  2064. mlog_ratelimited(ML_ERROR, "Direct IO failed, bytes = %lld",
  2065. (long long)bytes);
  2066. if (private) {
  2067. if (bytes > 0)
  2068. ret = ocfs2_dio_end_io_write(inode, private, offset,
  2069. bytes);
  2070. else
  2071. ocfs2_dio_free_write_ctx(inode, private);
  2072. }
  2073. ocfs2_iocb_clear_rw_locked(iocb);
  2074. level = ocfs2_iocb_rw_locked_level(iocb);
  2075. ocfs2_rw_unlock(inode, level);
  2076. return ret;
  2077. }
  2078. static ssize_t ocfs2_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
  2079. {
  2080. struct file *file = iocb->ki_filp;
  2081. struct inode *inode = file->f_mapping->host;
  2082. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  2083. get_block_t *get_block;
  2084. /*
  2085. * Fallback to buffered I/O if we see an inode without
  2086. * extents.
  2087. */
  2088. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
  2089. return 0;
  2090. /* Fallback to buffered I/O if we do not support append dio. */
  2091. if (iocb->ki_pos + iter->count > i_size_read(inode) &&
  2092. !ocfs2_supports_append_dio(osb))
  2093. return 0;
  2094. if (iov_iter_rw(iter) == READ)
  2095. get_block = ocfs2_lock_get_block;
  2096. else
  2097. get_block = ocfs2_dio_wr_get_block;
  2098. return __blockdev_direct_IO(iocb, inode, inode->i_sb->s_bdev,
  2099. iter, get_block,
  2100. ocfs2_dio_end_io, NULL, 0);
  2101. }
  2102. const struct address_space_operations ocfs2_aops = {
  2103. .readpage = ocfs2_readpage,
  2104. .readahead = ocfs2_readahead,
  2105. .writepage = ocfs2_writepage,
  2106. .write_begin = ocfs2_write_begin,
  2107. .write_end = ocfs2_write_end,
  2108. .bmap = ocfs2_bmap,
  2109. .direct_IO = ocfs2_direct_IO,
  2110. .invalidatepage = block_invalidatepage,
  2111. .releasepage = ocfs2_releasepage,
  2112. .migratepage = buffer_migrate_page,
  2113. .is_partially_uptodate = block_is_partially_uptodate,
  2114. .error_remove_page = generic_error_remove_page,
  2115. };