aops.c 62 KB

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