aops.c 62 KB

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