fast_commit.c 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * fs/ext4/fast_commit.c
  4. *
  5. * Written by Harshad Shirwadkar <harshadshirwadkar@gmail.com>
  6. *
  7. * Ext4 fast commits routines.
  8. */
  9. #include "ext4.h"
  10. #include "ext4_jbd2.h"
  11. #include "ext4_extents.h"
  12. #include "mballoc.h"
  13. #include <linux/lockdep.h>
  14. /*
  15. * Ext4 Fast Commits
  16. * -----------------
  17. *
  18. * Ext4 fast commits implement fine grained journalling for Ext4.
  19. *
  20. * Fast commits are organized as a log of tag-length-value (TLV) structs. (See
  21. * struct ext4_fc_tl). Each TLV contains some delta that is replayed TLV by
  22. * TLV during the recovery phase. For the scenarios for which we currently
  23. * don't have replay code, fast commit falls back to full commits.
  24. * Fast commits record delta in one of the following three categories.
  25. *
  26. * (A) Directory entry updates:
  27. *
  28. * - EXT4_FC_TAG_UNLINK - records directory entry unlink
  29. * - EXT4_FC_TAG_LINK - records directory entry link
  30. * - EXT4_FC_TAG_CREAT - records inode and directory entry creation
  31. *
  32. * (B) File specific data range updates:
  33. *
  34. * - EXT4_FC_TAG_ADD_RANGE - records addition of new blocks to an inode
  35. * - EXT4_FC_TAG_DEL_RANGE - records deletion of blocks from an inode
  36. *
  37. * (C) Inode metadata (mtime / ctime etc):
  38. *
  39. * - EXT4_FC_TAG_INODE - record the inode that should be replayed
  40. * during recovery. Note that iblocks field is
  41. * not replayed and instead derived during
  42. * replay.
  43. * Commit Operation
  44. * ----------------
  45. * With fast commits, we maintain all the directory entry operations in the
  46. * order in which they are issued in an in-memory queue. This queue is flushed
  47. * to disk during the commit operation. We also maintain a list of inodes
  48. * that need to be committed during a fast commit in another in memory queue of
  49. * inodes. During the commit operation, we commit in the following order:
  50. *
  51. * [1] Prepare all the inodes to write out their data by setting
  52. * "EXT4_STATE_FC_FLUSHING_DATA". This ensures that inode cannot be
  53. * deleted while it is being flushed.
  54. * [2] Flush data buffers to disk and clear "EXT4_STATE_FC_FLUSHING_DATA"
  55. * state.
  56. * [3] Lock the journal by calling jbd2_journal_lock_updates. This ensures that
  57. * all the exsiting handles finish and no new handles can start.
  58. * [4] Mark all the fast commit eligible inodes as undergoing fast commit
  59. * by setting "EXT4_STATE_FC_COMMITTING" state.
  60. * [5] Unlock the journal by calling jbd2_journal_unlock_updates. This allows
  61. * starting of new handles. If new handles try to start an update on
  62. * any of the inodes that are being committed, ext4_fc_track_inode()
  63. * will block until those inodes have finished the fast commit.
  64. * [6] Commit all the directory entry updates in the fast commit space.
  65. * [7] Commit all the changed inodes in the fast commit space and clear
  66. * "EXT4_STATE_FC_COMMITTING" for these inodes.
  67. * [8] Write tail tag (this tag ensures the atomicity, please read the following
  68. * section for more details).
  69. *
  70. * All the inode updates must be enclosed within jbd2_jounrnal_start()
  71. * and jbd2_journal_stop() similar to JBD2 journaling.
  72. *
  73. * Fast Commit Ineligibility
  74. * -------------------------
  75. *
  76. * Not all operations are supported by fast commits today (e.g extended
  77. * attributes). Fast commit ineligibility is marked by calling
  78. * ext4_fc_mark_ineligible(): This makes next fast commit operation to fall back
  79. * to full commit.
  80. *
  81. * Atomicity of commits
  82. * --------------------
  83. * In order to guarantee atomicity during the commit operation, fast commit
  84. * uses "EXT4_FC_TAG_TAIL" tag that marks a fast commit as complete. Tail
  85. * tag contains CRC of the contents and TID of the transaction after which
  86. * this fast commit should be applied. Recovery code replays fast commit
  87. * logs only if there's at least 1 valid tail present. For every fast commit
  88. * operation, there is 1 tail. This means, we may end up with multiple tails
  89. * in the fast commit space. Here's an example:
  90. *
  91. * - Create a new file A and remove existing file B
  92. * - fsync()
  93. * - Append contents to file A
  94. * - Truncate file A
  95. * - fsync()
  96. *
  97. * The fast commit space at the end of above operations would look like this:
  98. * [HEAD] [CREAT A] [UNLINK B] [TAIL] [ADD_RANGE A] [DEL_RANGE A] [TAIL]
  99. * |<--- Fast Commit 1 --->|<--- Fast Commit 2 ---->|
  100. *
  101. * Replay code should thus check for all the valid tails in the FC area.
  102. *
  103. * Fast Commit Replay Idempotence
  104. * ------------------------------
  105. *
  106. * Fast commits tags are idempotent in nature provided the recovery code follows
  107. * certain rules. The guiding principle that the commit path follows while
  108. * committing is that it stores the result of a particular operation instead of
  109. * storing the procedure.
  110. *
  111. * Let's consider this rename operation: 'mv /a /b'. Let's assume dirent '/a'
  112. * was associated with inode 10. During fast commit, instead of storing this
  113. * operation as a procedure "rename a to b", we store the resulting file system
  114. * state as a "series" of outcomes:
  115. *
  116. * - Link dirent b to inode 10
  117. * - Unlink dirent a
  118. * - Inode <10> with valid refcount
  119. *
  120. * Now when recovery code runs, it needs "enforce" this state on the file
  121. * system. This is what guarantees idempotence of fast commit replay.
  122. *
  123. * Let's take an example of a procedure that is not idempotent and see how fast
  124. * commits make it idempotent. Consider following sequence of operations:
  125. *
  126. * rm A; mv B A; read A
  127. * (x) (y) (z)
  128. *
  129. * (x), (y) and (z) are the points at which we can crash. If we store this
  130. * sequence of operations as is then the replay is not idempotent. Let's say
  131. * while in replay, we crash at (z). During the second replay, file A (which was
  132. * actually created as a result of "mv B A" operation) would get deleted. Thus,
  133. * file named A would be absent when we try to read A. So, this sequence of
  134. * operations is not idempotent. However, as mentioned above, instead of storing
  135. * the procedure fast commits store the outcome of each procedure. Thus the fast
  136. * commit log for above procedure would be as follows:
  137. *
  138. * (Let's assume dirent A was linked to inode 10 and dirent B was linked to
  139. * inode 11 before the replay)
  140. *
  141. * [Unlink A] [Link A to inode 11] [Unlink B] [Inode 11]
  142. * (w) (x) (y) (z)
  143. *
  144. * If we crash at (z), we will have file A linked to inode 11. During the second
  145. * replay, we will remove file A (inode 11). But we will create it back and make
  146. * it point to inode 11. We won't find B, so we'll just skip that step. At this
  147. * point, the refcount for inode 11 is not reliable, but that gets fixed by the
  148. * replay of last inode 11 tag. Crashes at points (w), (x) and (y) get handled
  149. * similarly. Thus, by converting a non-idempotent procedure into a series of
  150. * idempotent outcomes, fast commits ensured idempotence during the replay.
  151. *
  152. * Locking
  153. * -------
  154. * sbi->s_fc_lock protects the fast commit inodes queue and the fast commit
  155. * dentry queue. ei->i_fc_lock protects the fast commit related info in a given
  156. * inode. Most of the code avoids acquiring both the locks, but if one must do
  157. * that then sbi->s_fc_lock must be acquired before ei->i_fc_lock.
  158. *
  159. * TODOs
  160. * -----
  161. *
  162. * 0) Fast commit replay path hardening: Fast commit replay code should use
  163. * journal handles to make sure all the updates it does during the replay
  164. * path are atomic. With that if we crash during fast commit replay, after
  165. * trying to do recovery again, we will find a file system where fast commit
  166. * area is invalid (because new full commit would be found). In order to deal
  167. * with that, fast commit replay code should ensure that the "FC_REPLAY"
  168. * superblock state is persisted before starting the replay, so that after
  169. * the crash, fast commit recovery code can look at that flag and perform
  170. * fast commit recovery even if that area is invalidated by later full
  171. * commits.
  172. *
  173. * 1) Handle more ineligible cases.
  174. *
  175. * 2) Change ext4_fc_commit() to lookup logical to physical mapping using extent
  176. * status tree. This would get rid of the need to call ext4_fc_track_inode()
  177. * before acquiring i_data_sem. To do that we would need to ensure that
  178. * modified extents from the extent status tree are not evicted from memory.
  179. */
  180. #include <trace/events/ext4.h>
  181. static struct kmem_cache *ext4_fc_dentry_cachep;
  182. static void ext4_end_buffer_io_sync(struct buffer_head *bh, int uptodate)
  183. {
  184. BUFFER_TRACE(bh, "");
  185. if (uptodate) {
  186. ext4_debug("%s: Block %lld up-to-date",
  187. __func__, bh->b_blocknr);
  188. set_buffer_uptodate(bh);
  189. } else {
  190. ext4_debug("%s: Block %lld not up-to-date",
  191. __func__, bh->b_blocknr);
  192. clear_buffer_uptodate(bh);
  193. }
  194. unlock_buffer(bh);
  195. }
  196. static inline void ext4_fc_reset_inode(struct inode *inode)
  197. {
  198. struct ext4_inode_info *ei = EXT4_I(inode);
  199. ei->i_fc_lblk_start = 0;
  200. ei->i_fc_lblk_len = 0;
  201. }
  202. void ext4_fc_init_inode(struct inode *inode)
  203. {
  204. struct ext4_inode_info *ei = EXT4_I(inode);
  205. ext4_fc_reset_inode(inode);
  206. ext4_clear_inode_state(inode, EXT4_STATE_FC_COMMITTING);
  207. INIT_LIST_HEAD(&ei->i_fc_list);
  208. INIT_LIST_HEAD(&ei->i_fc_dilist);
  209. init_waitqueue_head(&ei->i_fc_wait);
  210. }
  211. static bool ext4_fc_disabled(struct super_block *sb)
  212. {
  213. return (!test_opt2(sb, JOURNAL_FAST_COMMIT) ||
  214. (EXT4_SB(sb)->s_mount_state & EXT4_FC_REPLAY));
  215. }
  216. /*
  217. * Remove inode from fast commit list. If the inode is being committed
  218. * we wait until inode commit is done.
  219. */
  220. void ext4_fc_del(struct inode *inode)
  221. {
  222. struct ext4_inode_info *ei = EXT4_I(inode);
  223. struct ext4_fc_dentry_update *fc_dentry;
  224. wait_queue_head_t *wq;
  225. int alloc_ctx;
  226. if (ext4_fc_disabled(inode->i_sb))
  227. return;
  228. alloc_ctx = ext4_fc_lock(inode->i_sb);
  229. if (list_empty(&ei->i_fc_list) && list_empty(&ei->i_fc_dilist)) {
  230. ext4_fc_unlock(inode->i_sb, alloc_ctx);
  231. return;
  232. }
  233. /*
  234. * Since ext4_fc_del is called from ext4_evict_inode while having a
  235. * handle open, there is no need for us to wait here even if a fast
  236. * commit is going on. That is because, if this inode is being
  237. * committed, ext4_mark_inode_dirty would have waited for inode commit
  238. * operation to finish before we come here. So, by the time we come
  239. * here, inode's EXT4_STATE_FC_COMMITTING would have been cleared. So,
  240. * we shouldn't see EXT4_STATE_FC_COMMITTING to be set on this inode
  241. * here.
  242. *
  243. * We may come here without any handles open in the "no_delete" case of
  244. * ext4_evict_inode as well. However, if that happens, we first mark the
  245. * file system as fast commit ineligible anyway. So, even in that case,
  246. * it is okay to remove the inode from the fc list.
  247. */
  248. WARN_ON(ext4_test_inode_state(inode, EXT4_STATE_FC_COMMITTING)
  249. && !ext4_test_mount_flag(inode->i_sb, EXT4_MF_FC_INELIGIBLE));
  250. while (ext4_test_inode_state(inode, EXT4_STATE_FC_FLUSHING_DATA)) {
  251. #if (BITS_PER_LONG < 64)
  252. DEFINE_WAIT_BIT(wait, &ei->i_state_flags,
  253. EXT4_STATE_FC_FLUSHING_DATA);
  254. wq = bit_waitqueue(&ei->i_state_flags,
  255. EXT4_STATE_FC_FLUSHING_DATA);
  256. #else
  257. DEFINE_WAIT_BIT(wait, &ei->i_flags,
  258. EXT4_STATE_FC_FLUSHING_DATA);
  259. wq = bit_waitqueue(&ei->i_flags,
  260. EXT4_STATE_FC_FLUSHING_DATA);
  261. #endif
  262. prepare_to_wait(wq, &wait.wq_entry, TASK_UNINTERRUPTIBLE);
  263. if (ext4_test_inode_state(inode, EXT4_STATE_FC_FLUSHING_DATA)) {
  264. ext4_fc_unlock(inode->i_sb, alloc_ctx);
  265. schedule();
  266. alloc_ctx = ext4_fc_lock(inode->i_sb);
  267. }
  268. finish_wait(wq, &wait.wq_entry);
  269. }
  270. list_del_init(&ei->i_fc_list);
  271. /*
  272. * Since this inode is getting removed, let's also remove all FC
  273. * dentry create references, since it is not needed to log it anyways.
  274. */
  275. if (list_empty(&ei->i_fc_dilist)) {
  276. ext4_fc_unlock(inode->i_sb, alloc_ctx);
  277. return;
  278. }
  279. fc_dentry = list_first_entry(&ei->i_fc_dilist, struct ext4_fc_dentry_update, fcd_dilist);
  280. WARN_ON(fc_dentry->fcd_op != EXT4_FC_TAG_CREAT);
  281. list_del_init(&fc_dentry->fcd_list);
  282. list_del_init(&fc_dentry->fcd_dilist);
  283. WARN_ON(!list_empty(&ei->i_fc_dilist));
  284. ext4_fc_unlock(inode->i_sb, alloc_ctx);
  285. release_dentry_name_snapshot(&fc_dentry->fcd_name);
  286. kmem_cache_free(ext4_fc_dentry_cachep, fc_dentry);
  287. }
  288. /*
  289. * Mark file system as fast commit ineligible, and record latest
  290. * ineligible transaction tid. This means until the recorded
  291. * transaction, commit operation would result in a full jbd2 commit.
  292. */
  293. void ext4_fc_mark_ineligible(struct super_block *sb, int reason, handle_t *handle)
  294. {
  295. struct ext4_sb_info *sbi = EXT4_SB(sb);
  296. tid_t tid;
  297. bool has_transaction = true;
  298. bool is_ineligible;
  299. int alloc_ctx;
  300. if (ext4_fc_disabled(sb))
  301. return;
  302. if (handle && !IS_ERR(handle))
  303. tid = handle->h_transaction->t_tid;
  304. else {
  305. read_lock(&sbi->s_journal->j_state_lock);
  306. if (sbi->s_journal->j_running_transaction)
  307. tid = sbi->s_journal->j_running_transaction->t_tid;
  308. else
  309. has_transaction = false;
  310. read_unlock(&sbi->s_journal->j_state_lock);
  311. }
  312. alloc_ctx = ext4_fc_lock(sb);
  313. is_ineligible = ext4_test_mount_flag(sb, EXT4_MF_FC_INELIGIBLE);
  314. if (has_transaction && (!is_ineligible || tid_gt(tid, sbi->s_fc_ineligible_tid)))
  315. sbi->s_fc_ineligible_tid = tid;
  316. ext4_set_mount_flag(sb, EXT4_MF_FC_INELIGIBLE);
  317. ext4_fc_unlock(sb, alloc_ctx);
  318. WARN_ON(reason >= EXT4_FC_REASON_MAX);
  319. sbi->s_fc_stats.fc_ineligible_reason_count[reason]++;
  320. }
  321. /*
  322. * Generic fast commit tracking function. If this is the first time this we are
  323. * called after a full commit, we initialize fast commit fields and then call
  324. * __fc_track_fn() with update = 0. If we have already been called after a full
  325. * commit, we pass update = 1. Based on that, the track function can determine
  326. * if it needs to track a field for the first time or if it needs to just
  327. * update the previously tracked value.
  328. *
  329. * If enqueue is set, this function enqueues the inode in fast commit list.
  330. */
  331. static int ext4_fc_track_template(
  332. handle_t *handle, struct inode *inode,
  333. int (*__fc_track_fn)(handle_t *handle, struct inode *, void *, bool),
  334. void *args, int enqueue)
  335. {
  336. bool update = false;
  337. struct ext4_inode_info *ei = EXT4_I(inode);
  338. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  339. tid_t tid = 0;
  340. int alloc_ctx;
  341. int ret;
  342. tid = handle->h_transaction->t_tid;
  343. spin_lock(&ei->i_fc_lock);
  344. if (tid == ei->i_sync_tid) {
  345. update = true;
  346. } else {
  347. ext4_fc_reset_inode(inode);
  348. ei->i_sync_tid = tid;
  349. }
  350. ret = __fc_track_fn(handle, inode, args, update);
  351. spin_unlock(&ei->i_fc_lock);
  352. if (!enqueue)
  353. return ret;
  354. alloc_ctx = ext4_fc_lock(inode->i_sb);
  355. if (list_empty(&EXT4_I(inode)->i_fc_list))
  356. list_add_tail(&EXT4_I(inode)->i_fc_list,
  357. (sbi->s_journal->j_flags & JBD2_FULL_COMMIT_ONGOING ||
  358. sbi->s_journal->j_flags & JBD2_FAST_COMMIT_ONGOING) ?
  359. &sbi->s_fc_q[FC_Q_STAGING] :
  360. &sbi->s_fc_q[FC_Q_MAIN]);
  361. ext4_fc_unlock(inode->i_sb, alloc_ctx);
  362. return ret;
  363. }
  364. struct __track_dentry_update_args {
  365. struct dentry *dentry;
  366. int op;
  367. };
  368. /* __track_fn for directory entry updates. Called with ei->i_fc_lock. */
  369. static int __track_dentry_update(handle_t *handle, struct inode *inode,
  370. void *arg, bool update)
  371. {
  372. struct ext4_fc_dentry_update *node;
  373. struct ext4_inode_info *ei = EXT4_I(inode);
  374. struct __track_dentry_update_args *dentry_update =
  375. (struct __track_dentry_update_args *)arg;
  376. struct dentry *dentry = dentry_update->dentry;
  377. struct inode *dir = dentry->d_parent->d_inode;
  378. struct super_block *sb = inode->i_sb;
  379. struct ext4_sb_info *sbi = EXT4_SB(sb);
  380. int alloc_ctx;
  381. spin_unlock(&ei->i_fc_lock);
  382. if (IS_ENCRYPTED(dir)) {
  383. ext4_fc_mark_ineligible(sb, EXT4_FC_REASON_ENCRYPTED_FILENAME,
  384. handle);
  385. spin_lock(&ei->i_fc_lock);
  386. return -EOPNOTSUPP;
  387. }
  388. node = kmem_cache_alloc(ext4_fc_dentry_cachep, GFP_NOFS);
  389. if (!node) {
  390. ext4_fc_mark_ineligible(sb, EXT4_FC_REASON_NOMEM, handle);
  391. spin_lock(&ei->i_fc_lock);
  392. return -ENOMEM;
  393. }
  394. node->fcd_op = dentry_update->op;
  395. node->fcd_parent = dir->i_ino;
  396. node->fcd_ino = inode->i_ino;
  397. take_dentry_name_snapshot(&node->fcd_name, dentry);
  398. INIT_LIST_HEAD(&node->fcd_dilist);
  399. INIT_LIST_HEAD(&node->fcd_list);
  400. alloc_ctx = ext4_fc_lock(sb);
  401. if (sbi->s_journal->j_flags & JBD2_FULL_COMMIT_ONGOING ||
  402. sbi->s_journal->j_flags & JBD2_FAST_COMMIT_ONGOING)
  403. list_add_tail(&node->fcd_list,
  404. &sbi->s_fc_dentry_q[FC_Q_STAGING]);
  405. else
  406. list_add_tail(&node->fcd_list, &sbi->s_fc_dentry_q[FC_Q_MAIN]);
  407. /*
  408. * This helps us keep a track of all fc_dentry updates which is part of
  409. * this ext4 inode. So in case the inode is getting unlinked, before
  410. * even we get a chance to fsync, we could remove all fc_dentry
  411. * references while evicting the inode in ext4_fc_del().
  412. * Also with this, we don't need to loop over all the inodes in
  413. * sbi->s_fc_q to get the corresponding inode in
  414. * ext4_fc_commit_dentry_updates().
  415. */
  416. if (dentry_update->op == EXT4_FC_TAG_CREAT) {
  417. WARN_ON(!list_empty(&ei->i_fc_dilist));
  418. list_add_tail(&node->fcd_dilist, &ei->i_fc_dilist);
  419. }
  420. ext4_fc_unlock(sb, alloc_ctx);
  421. spin_lock(&ei->i_fc_lock);
  422. return 0;
  423. }
  424. void __ext4_fc_track_unlink(handle_t *handle,
  425. struct inode *inode, struct dentry *dentry)
  426. {
  427. struct __track_dentry_update_args args;
  428. int ret;
  429. args.dentry = dentry;
  430. args.op = EXT4_FC_TAG_UNLINK;
  431. ret = ext4_fc_track_template(handle, inode, __track_dentry_update,
  432. (void *)&args, 0);
  433. trace_ext4_fc_track_unlink(handle, inode, dentry, ret);
  434. }
  435. void ext4_fc_track_unlink(handle_t *handle, struct dentry *dentry)
  436. {
  437. struct inode *inode = d_inode(dentry);
  438. if (ext4_fc_disabled(inode->i_sb))
  439. return;
  440. if (ext4_test_mount_flag(inode->i_sb, EXT4_MF_FC_INELIGIBLE))
  441. return;
  442. __ext4_fc_track_unlink(handle, inode, dentry);
  443. }
  444. void __ext4_fc_track_link(handle_t *handle,
  445. struct inode *inode, struct dentry *dentry)
  446. {
  447. struct __track_dentry_update_args args;
  448. int ret;
  449. args.dentry = dentry;
  450. args.op = EXT4_FC_TAG_LINK;
  451. ret = ext4_fc_track_template(handle, inode, __track_dentry_update,
  452. (void *)&args, 0);
  453. trace_ext4_fc_track_link(handle, inode, dentry, ret);
  454. }
  455. void ext4_fc_track_link(handle_t *handle, struct dentry *dentry)
  456. {
  457. struct inode *inode = d_inode(dentry);
  458. if (ext4_fc_disabled(inode->i_sb))
  459. return;
  460. if (ext4_test_mount_flag(inode->i_sb, EXT4_MF_FC_INELIGIBLE))
  461. return;
  462. __ext4_fc_track_link(handle, inode, dentry);
  463. }
  464. void __ext4_fc_track_create(handle_t *handle, struct inode *inode,
  465. struct dentry *dentry)
  466. {
  467. struct __track_dentry_update_args args;
  468. int ret;
  469. args.dentry = dentry;
  470. args.op = EXT4_FC_TAG_CREAT;
  471. ret = ext4_fc_track_template(handle, inode, __track_dentry_update,
  472. (void *)&args, 0);
  473. trace_ext4_fc_track_create(handle, inode, dentry, ret);
  474. }
  475. void ext4_fc_track_create(handle_t *handle, struct dentry *dentry)
  476. {
  477. struct inode *inode = d_inode(dentry);
  478. if (ext4_fc_disabled(inode->i_sb))
  479. return;
  480. if (ext4_test_mount_flag(inode->i_sb, EXT4_MF_FC_INELIGIBLE))
  481. return;
  482. __ext4_fc_track_create(handle, inode, dentry);
  483. }
  484. /* __track_fn for inode tracking */
  485. static int __track_inode(handle_t *handle, struct inode *inode, void *arg,
  486. bool update)
  487. {
  488. if (update)
  489. return -EEXIST;
  490. EXT4_I(inode)->i_fc_lblk_len = 0;
  491. return 0;
  492. }
  493. void ext4_fc_track_inode(handle_t *handle, struct inode *inode)
  494. {
  495. struct ext4_inode_info *ei = EXT4_I(inode);
  496. wait_queue_head_t *wq;
  497. int ret;
  498. if (S_ISDIR(inode->i_mode))
  499. return;
  500. if (ext4_fc_disabled(inode->i_sb))
  501. return;
  502. if (ext4_should_journal_data(inode)) {
  503. ext4_fc_mark_ineligible(inode->i_sb,
  504. EXT4_FC_REASON_INODE_JOURNAL_DATA, handle);
  505. return;
  506. }
  507. if (ext4_test_mount_flag(inode->i_sb, EXT4_MF_FC_INELIGIBLE))
  508. return;
  509. /*
  510. * If we come here, we may sleep while waiting for the inode to
  511. * commit. We shouldn't be holding i_data_sem when we go to sleep since
  512. * the commit path needs to grab the lock while committing the inode.
  513. */
  514. lockdep_assert_not_held(&ei->i_data_sem);
  515. while (ext4_test_inode_state(inode, EXT4_STATE_FC_COMMITTING)) {
  516. #if (BITS_PER_LONG < 64)
  517. DEFINE_WAIT_BIT(wait, &ei->i_state_flags,
  518. EXT4_STATE_FC_COMMITTING);
  519. wq = bit_waitqueue(&ei->i_state_flags,
  520. EXT4_STATE_FC_COMMITTING);
  521. #else
  522. DEFINE_WAIT_BIT(wait, &ei->i_flags,
  523. EXT4_STATE_FC_COMMITTING);
  524. wq = bit_waitqueue(&ei->i_flags,
  525. EXT4_STATE_FC_COMMITTING);
  526. #endif
  527. prepare_to_wait(wq, &wait.wq_entry, TASK_UNINTERRUPTIBLE);
  528. if (ext4_test_inode_state(inode, EXT4_STATE_FC_COMMITTING))
  529. schedule();
  530. finish_wait(wq, &wait.wq_entry);
  531. }
  532. /*
  533. * From this point on, this inode will not be committed either
  534. * by fast or full commit as long as the handle is open.
  535. */
  536. ret = ext4_fc_track_template(handle, inode, __track_inode, NULL, 1);
  537. trace_ext4_fc_track_inode(handle, inode, ret);
  538. }
  539. struct __track_range_args {
  540. ext4_lblk_t start, end;
  541. };
  542. /* __track_fn for tracking data updates */
  543. static int __track_range(handle_t *handle, struct inode *inode, void *arg,
  544. bool update)
  545. {
  546. struct ext4_inode_info *ei = EXT4_I(inode);
  547. ext4_lblk_t oldstart;
  548. struct __track_range_args *__arg =
  549. (struct __track_range_args *)arg;
  550. if (inode->i_ino < EXT4_FIRST_INO(inode->i_sb)) {
  551. ext4_debug("Special inode %ld being modified\n", inode->i_ino);
  552. return -ECANCELED;
  553. }
  554. oldstart = ei->i_fc_lblk_start;
  555. if (update && ei->i_fc_lblk_len > 0) {
  556. ei->i_fc_lblk_start = min(ei->i_fc_lblk_start, __arg->start);
  557. ei->i_fc_lblk_len =
  558. max(oldstart + ei->i_fc_lblk_len - 1, __arg->end) -
  559. ei->i_fc_lblk_start + 1;
  560. } else {
  561. ei->i_fc_lblk_start = __arg->start;
  562. ei->i_fc_lblk_len = __arg->end - __arg->start + 1;
  563. }
  564. return 0;
  565. }
  566. void ext4_fc_track_range(handle_t *handle, struct inode *inode, ext4_lblk_t start,
  567. ext4_lblk_t end)
  568. {
  569. struct __track_range_args args;
  570. int ret;
  571. if (S_ISDIR(inode->i_mode))
  572. return;
  573. if (ext4_fc_disabled(inode->i_sb))
  574. return;
  575. if (ext4_test_mount_flag(inode->i_sb, EXT4_MF_FC_INELIGIBLE))
  576. return;
  577. if (ext4_has_inline_data(inode)) {
  578. ext4_fc_mark_ineligible(inode->i_sb, EXT4_FC_REASON_XATTR,
  579. handle);
  580. return;
  581. }
  582. args.start = start;
  583. args.end = end;
  584. ret = ext4_fc_track_template(handle, inode, __track_range, &args, 1);
  585. trace_ext4_fc_track_range(handle, inode, start, end, ret);
  586. }
  587. static void ext4_fc_submit_bh(struct super_block *sb, bool is_tail)
  588. {
  589. blk_opf_t write_flags = JBD2_JOURNAL_REQ_FLAGS;
  590. struct buffer_head *bh = EXT4_SB(sb)->s_fc_bh;
  591. /* Add REQ_FUA | REQ_PREFLUSH only its tail */
  592. if (test_opt(sb, BARRIER) && is_tail)
  593. write_flags |= REQ_FUA | REQ_PREFLUSH;
  594. lock_buffer(bh);
  595. set_buffer_dirty(bh);
  596. set_buffer_uptodate(bh);
  597. bh->b_end_io = ext4_end_buffer_io_sync;
  598. submit_bh(REQ_OP_WRITE | write_flags, bh);
  599. EXT4_SB(sb)->s_fc_bh = NULL;
  600. }
  601. /* Ext4 commit path routines */
  602. /*
  603. * Allocate len bytes on a fast commit buffer.
  604. *
  605. * During the commit time this function is used to manage fast commit
  606. * block space. We don't split a fast commit log onto different
  607. * blocks. So this function makes sure that if there's not enough space
  608. * on the current block, the remaining space in the current block is
  609. * marked as unused by adding EXT4_FC_TAG_PAD tag. In that case,
  610. * new block is from jbd2 and CRC is updated to reflect the padding
  611. * we added.
  612. */
  613. static u8 *ext4_fc_reserve_space(struct super_block *sb, int len, u32 *crc)
  614. {
  615. struct ext4_fc_tl tl;
  616. struct ext4_sb_info *sbi = EXT4_SB(sb);
  617. struct buffer_head *bh;
  618. int bsize = sbi->s_journal->j_blocksize;
  619. int ret, off = sbi->s_fc_bytes % bsize;
  620. int remaining;
  621. u8 *dst;
  622. /*
  623. * If 'len' is too long to fit in any block alongside a PAD tlv, then we
  624. * cannot fulfill the request.
  625. */
  626. if (len > bsize - EXT4_FC_TAG_BASE_LEN)
  627. return NULL;
  628. if (!sbi->s_fc_bh) {
  629. ret = jbd2_fc_get_buf(EXT4_SB(sb)->s_journal, &bh);
  630. if (ret)
  631. return NULL;
  632. sbi->s_fc_bh = bh;
  633. }
  634. dst = sbi->s_fc_bh->b_data + off;
  635. /*
  636. * Allocate the bytes in the current block if we can do so while still
  637. * leaving enough space for a PAD tlv.
  638. */
  639. remaining = bsize - EXT4_FC_TAG_BASE_LEN - off;
  640. if (len <= remaining) {
  641. sbi->s_fc_bytes += len;
  642. return dst;
  643. }
  644. /*
  645. * Else, terminate the current block with a PAD tlv, then allocate a new
  646. * block and allocate the bytes at the start of that new block.
  647. */
  648. tl.fc_tag = cpu_to_le16(EXT4_FC_TAG_PAD);
  649. tl.fc_len = cpu_to_le16(remaining);
  650. memcpy(dst, &tl, EXT4_FC_TAG_BASE_LEN);
  651. memset(dst + EXT4_FC_TAG_BASE_LEN, 0, remaining);
  652. *crc = ext4_chksum(*crc, sbi->s_fc_bh->b_data, bsize);
  653. ext4_fc_submit_bh(sb, false);
  654. ret = jbd2_fc_get_buf(EXT4_SB(sb)->s_journal, &bh);
  655. if (ret)
  656. return NULL;
  657. sbi->s_fc_bh = bh;
  658. sbi->s_fc_bytes += bsize - off + len;
  659. return sbi->s_fc_bh->b_data;
  660. }
  661. /*
  662. * Complete a fast commit by writing tail tag.
  663. *
  664. * Writing tail tag marks the end of a fast commit. In order to guarantee
  665. * atomicity, after writing tail tag, even if there's space remaining
  666. * in the block, next commit shouldn't use it. That's why tail tag
  667. * has the length as that of the remaining space on the block.
  668. */
  669. static int ext4_fc_write_tail(struct super_block *sb, u32 crc)
  670. {
  671. struct ext4_sb_info *sbi = EXT4_SB(sb);
  672. struct ext4_fc_tl tl;
  673. struct ext4_fc_tail tail;
  674. int off, bsize = sbi->s_journal->j_blocksize;
  675. u8 *dst;
  676. /*
  677. * ext4_fc_reserve_space takes care of allocating an extra block if
  678. * there's no enough space on this block for accommodating this tail.
  679. */
  680. dst = ext4_fc_reserve_space(sb, EXT4_FC_TAG_BASE_LEN + sizeof(tail), &crc);
  681. if (!dst)
  682. return -ENOSPC;
  683. off = sbi->s_fc_bytes % bsize;
  684. tl.fc_tag = cpu_to_le16(EXT4_FC_TAG_TAIL);
  685. tl.fc_len = cpu_to_le16(bsize - off + sizeof(struct ext4_fc_tail));
  686. sbi->s_fc_bytes = round_up(sbi->s_fc_bytes, bsize);
  687. memcpy(dst, &tl, EXT4_FC_TAG_BASE_LEN);
  688. dst += EXT4_FC_TAG_BASE_LEN;
  689. tail.fc_tid = cpu_to_le32(sbi->s_journal->j_running_transaction->t_tid);
  690. memcpy(dst, &tail.fc_tid, sizeof(tail.fc_tid));
  691. dst += sizeof(tail.fc_tid);
  692. crc = ext4_chksum(crc, sbi->s_fc_bh->b_data,
  693. dst - (u8 *)sbi->s_fc_bh->b_data);
  694. tail.fc_crc = cpu_to_le32(crc);
  695. memcpy(dst, &tail.fc_crc, sizeof(tail.fc_crc));
  696. dst += sizeof(tail.fc_crc);
  697. memset(dst, 0, bsize - off); /* Don't leak uninitialized memory. */
  698. ext4_fc_submit_bh(sb, true);
  699. return 0;
  700. }
  701. /*
  702. * Adds tag, length, value and updates CRC. Returns true if tlv was added.
  703. * Returns false if there's not enough space.
  704. */
  705. static bool ext4_fc_add_tlv(struct super_block *sb, u16 tag, u16 len, u8 *val,
  706. u32 *crc)
  707. {
  708. struct ext4_fc_tl tl;
  709. u8 *dst;
  710. dst = ext4_fc_reserve_space(sb, EXT4_FC_TAG_BASE_LEN + len, crc);
  711. if (!dst)
  712. return false;
  713. tl.fc_tag = cpu_to_le16(tag);
  714. tl.fc_len = cpu_to_le16(len);
  715. memcpy(dst, &tl, EXT4_FC_TAG_BASE_LEN);
  716. memcpy(dst + EXT4_FC_TAG_BASE_LEN, val, len);
  717. return true;
  718. }
  719. /* Same as above, but adds dentry tlv. */
  720. static bool ext4_fc_add_dentry_tlv(struct super_block *sb, u32 *crc,
  721. struct ext4_fc_dentry_update *fc_dentry)
  722. {
  723. struct ext4_fc_dentry_info fcd;
  724. struct ext4_fc_tl tl;
  725. int dlen = fc_dentry->fcd_name.name.len;
  726. u8 *dst = ext4_fc_reserve_space(sb,
  727. EXT4_FC_TAG_BASE_LEN + sizeof(fcd) + dlen, crc);
  728. if (!dst)
  729. return false;
  730. fcd.fc_parent_ino = cpu_to_le32(fc_dentry->fcd_parent);
  731. fcd.fc_ino = cpu_to_le32(fc_dentry->fcd_ino);
  732. tl.fc_tag = cpu_to_le16(fc_dentry->fcd_op);
  733. tl.fc_len = cpu_to_le16(sizeof(fcd) + dlen);
  734. memcpy(dst, &tl, EXT4_FC_TAG_BASE_LEN);
  735. dst += EXT4_FC_TAG_BASE_LEN;
  736. memcpy(dst, &fcd, sizeof(fcd));
  737. dst += sizeof(fcd);
  738. memcpy(dst, fc_dentry->fcd_name.name.name, dlen);
  739. return true;
  740. }
  741. /*
  742. * Writes inode in the fast commit space under TLV with tag @tag.
  743. * Returns 0 on success, error on failure.
  744. */
  745. static int ext4_fc_write_inode(struct inode *inode, u32 *crc)
  746. {
  747. struct ext4_inode_info *ei = EXT4_I(inode);
  748. int inode_len = EXT4_GOOD_OLD_INODE_SIZE;
  749. int ret;
  750. struct ext4_iloc iloc;
  751. struct ext4_fc_inode fc_inode;
  752. struct ext4_fc_tl tl;
  753. u8 *dst;
  754. ret = ext4_get_inode_loc(inode, &iloc);
  755. if (ret)
  756. return ret;
  757. if (ext4_test_inode_flag(inode, EXT4_INODE_INLINE_DATA))
  758. inode_len = EXT4_INODE_SIZE(inode->i_sb);
  759. else if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE)
  760. inode_len += ei->i_extra_isize;
  761. fc_inode.fc_ino = cpu_to_le32(inode->i_ino);
  762. tl.fc_tag = cpu_to_le16(EXT4_FC_TAG_INODE);
  763. tl.fc_len = cpu_to_le16(inode_len + sizeof(fc_inode.fc_ino));
  764. ret = -ECANCELED;
  765. dst = ext4_fc_reserve_space(inode->i_sb,
  766. EXT4_FC_TAG_BASE_LEN + inode_len + sizeof(fc_inode.fc_ino), crc);
  767. if (!dst)
  768. goto err;
  769. memcpy(dst, &tl, EXT4_FC_TAG_BASE_LEN);
  770. dst += EXT4_FC_TAG_BASE_LEN;
  771. memcpy(dst, &fc_inode, sizeof(fc_inode));
  772. dst += sizeof(fc_inode);
  773. memcpy(dst, (u8 *)ext4_raw_inode(&iloc), inode_len);
  774. ret = 0;
  775. err:
  776. brelse(iloc.bh);
  777. return ret;
  778. }
  779. /*
  780. * Writes updated data ranges for the inode in question. Updates CRC.
  781. * Returns 0 on success, error otherwise.
  782. */
  783. static int ext4_fc_write_inode_data(struct inode *inode, u32 *crc)
  784. {
  785. ext4_lblk_t old_blk_size, cur_lblk_off, new_blk_size;
  786. struct ext4_inode_info *ei = EXT4_I(inode);
  787. struct ext4_map_blocks map;
  788. struct ext4_fc_add_range fc_ext;
  789. struct ext4_fc_del_range lrange;
  790. struct ext4_extent *ex;
  791. int ret;
  792. spin_lock(&ei->i_fc_lock);
  793. if (ei->i_fc_lblk_len == 0) {
  794. spin_unlock(&ei->i_fc_lock);
  795. return 0;
  796. }
  797. old_blk_size = ei->i_fc_lblk_start;
  798. new_blk_size = ei->i_fc_lblk_start + ei->i_fc_lblk_len - 1;
  799. ei->i_fc_lblk_len = 0;
  800. spin_unlock(&ei->i_fc_lock);
  801. cur_lblk_off = old_blk_size;
  802. ext4_debug("will try writing %d to %d for inode %ld\n",
  803. cur_lblk_off, new_blk_size, inode->i_ino);
  804. while (cur_lblk_off <= new_blk_size) {
  805. map.m_lblk = cur_lblk_off;
  806. map.m_len = new_blk_size - cur_lblk_off + 1;
  807. ret = ext4_map_blocks(NULL, inode, &map,
  808. EXT4_GET_BLOCKS_IO_SUBMIT |
  809. EXT4_EX_NOCACHE);
  810. if (ret < 0)
  811. return -ECANCELED;
  812. if (map.m_len == 0) {
  813. cur_lblk_off++;
  814. continue;
  815. }
  816. if (ret == 0) {
  817. lrange.fc_ino = cpu_to_le32(inode->i_ino);
  818. lrange.fc_lblk = cpu_to_le32(map.m_lblk);
  819. lrange.fc_len = cpu_to_le32(map.m_len);
  820. if (!ext4_fc_add_tlv(inode->i_sb, EXT4_FC_TAG_DEL_RANGE,
  821. sizeof(lrange), (u8 *)&lrange, crc))
  822. return -ENOSPC;
  823. } else {
  824. unsigned int max = (map.m_flags & EXT4_MAP_UNWRITTEN) ?
  825. EXT_UNWRITTEN_MAX_LEN : EXT_INIT_MAX_LEN;
  826. /* Limit the number of blocks in one extent */
  827. map.m_len = min(max, map.m_len);
  828. fc_ext.fc_ino = cpu_to_le32(inode->i_ino);
  829. ex = (struct ext4_extent *)&fc_ext.fc_ex;
  830. ex->ee_block = cpu_to_le32(map.m_lblk);
  831. ex->ee_len = cpu_to_le16(map.m_len);
  832. ext4_ext_store_pblock(ex, map.m_pblk);
  833. if (map.m_flags & EXT4_MAP_UNWRITTEN)
  834. ext4_ext_mark_unwritten(ex);
  835. else
  836. ext4_ext_mark_initialized(ex);
  837. if (!ext4_fc_add_tlv(inode->i_sb, EXT4_FC_TAG_ADD_RANGE,
  838. sizeof(fc_ext), (u8 *)&fc_ext, crc))
  839. return -ENOSPC;
  840. }
  841. cur_lblk_off += map.m_len;
  842. }
  843. return 0;
  844. }
  845. /* Flushes data of all the inodes in the commit queue. */
  846. static int ext4_fc_flush_data(journal_t *journal)
  847. {
  848. struct super_block *sb = journal->j_private;
  849. struct ext4_sb_info *sbi = EXT4_SB(sb);
  850. struct ext4_inode_info *ei;
  851. int ret = 0;
  852. list_for_each_entry(ei, &sbi->s_fc_q[FC_Q_MAIN], i_fc_list) {
  853. ret = jbd2_submit_inode_data(journal, READ_ONCE(ei->jinode));
  854. if (ret)
  855. return ret;
  856. }
  857. list_for_each_entry(ei, &sbi->s_fc_q[FC_Q_MAIN], i_fc_list) {
  858. ret = jbd2_wait_inode_data(journal, READ_ONCE(ei->jinode));
  859. if (ret)
  860. return ret;
  861. }
  862. return 0;
  863. }
  864. /* Commit all the directory entry updates */
  865. static int ext4_fc_commit_dentry_updates(journal_t *journal, u32 *crc)
  866. {
  867. struct super_block *sb = journal->j_private;
  868. struct ext4_sb_info *sbi = EXT4_SB(sb);
  869. struct ext4_fc_dentry_update *fc_dentry, *fc_dentry_n;
  870. struct inode *inode;
  871. struct ext4_inode_info *ei;
  872. int ret;
  873. if (list_empty(&sbi->s_fc_dentry_q[FC_Q_MAIN]))
  874. return 0;
  875. list_for_each_entry_safe(fc_dentry, fc_dentry_n,
  876. &sbi->s_fc_dentry_q[FC_Q_MAIN], fcd_list) {
  877. if (fc_dentry->fcd_op != EXT4_FC_TAG_CREAT) {
  878. if (!ext4_fc_add_dentry_tlv(sb, crc, fc_dentry))
  879. return -ENOSPC;
  880. continue;
  881. }
  882. /*
  883. * With fcd_dilist we need not loop in sbi->s_fc_q to get the
  884. * corresponding inode. Also, the corresponding inode could have been
  885. * deleted, in which case, we don't need to do anything.
  886. */
  887. if (list_empty(&fc_dentry->fcd_dilist))
  888. continue;
  889. ei = list_first_entry(&fc_dentry->fcd_dilist,
  890. struct ext4_inode_info, i_fc_dilist);
  891. inode = &ei->vfs_inode;
  892. WARN_ON(inode->i_ino != fc_dentry->fcd_ino);
  893. /*
  894. * We first write the inode and then the create dirent. This
  895. * allows the recovery code to create an unnamed inode first
  896. * and then link it to a directory entry. This allows us
  897. * to use namei.c routines almost as is and simplifies
  898. * the recovery code.
  899. */
  900. ret = ext4_fc_write_inode(inode, crc);
  901. if (ret)
  902. return ret;
  903. ret = ext4_fc_write_inode_data(inode, crc);
  904. if (ret)
  905. return ret;
  906. if (!ext4_fc_add_dentry_tlv(sb, crc, fc_dentry))
  907. return -ENOSPC;
  908. }
  909. return 0;
  910. }
  911. static int ext4_fc_perform_commit(journal_t *journal)
  912. {
  913. struct super_block *sb = journal->j_private;
  914. struct ext4_sb_info *sbi = EXT4_SB(sb);
  915. struct ext4_inode_info *iter;
  916. struct ext4_fc_head head;
  917. struct inode *inode;
  918. struct blk_plug plug;
  919. int ret = 0;
  920. u32 crc = 0;
  921. int alloc_ctx;
  922. /*
  923. * Step 1: Mark all inodes on s_fc_q[MAIN] with
  924. * EXT4_STATE_FC_FLUSHING_DATA. This prevents these inodes from being
  925. * freed until the data flush is over.
  926. */
  927. alloc_ctx = ext4_fc_lock(sb);
  928. list_for_each_entry(iter, &sbi->s_fc_q[FC_Q_MAIN], i_fc_list) {
  929. ext4_set_inode_state(&iter->vfs_inode,
  930. EXT4_STATE_FC_FLUSHING_DATA);
  931. }
  932. ext4_fc_unlock(sb, alloc_ctx);
  933. /* Step 2: Flush data for all the eligible inodes. */
  934. ret = ext4_fc_flush_data(journal);
  935. /*
  936. * Step 3: Clear EXT4_STATE_FC_FLUSHING_DATA flag, before returning
  937. * any error from step 2. This ensures that waiters waiting on
  938. * EXT4_STATE_FC_FLUSHING_DATA can resume.
  939. */
  940. alloc_ctx = ext4_fc_lock(sb);
  941. list_for_each_entry(iter, &sbi->s_fc_q[FC_Q_MAIN], i_fc_list) {
  942. ext4_clear_inode_state(&iter->vfs_inode,
  943. EXT4_STATE_FC_FLUSHING_DATA);
  944. #if (BITS_PER_LONG < 64)
  945. wake_up_bit(&iter->i_state_flags, EXT4_STATE_FC_FLUSHING_DATA);
  946. #else
  947. wake_up_bit(&iter->i_flags, EXT4_STATE_FC_FLUSHING_DATA);
  948. #endif
  949. }
  950. /*
  951. * Make sure clearing of EXT4_STATE_FC_FLUSHING_DATA is visible before
  952. * the waiter checks the bit. Pairs with implicit barrier in
  953. * prepare_to_wait() in ext4_fc_del().
  954. */
  955. smp_mb();
  956. ext4_fc_unlock(sb, alloc_ctx);
  957. /*
  958. * If we encountered error in Step 2, return it now after clearing
  959. * EXT4_STATE_FC_FLUSHING_DATA bit.
  960. */
  961. if (ret)
  962. return ret;
  963. /* Step 4: Mark all inodes as being committed. */
  964. jbd2_journal_lock_updates(journal);
  965. /*
  966. * The journal is now locked. No more handles can start and all the
  967. * previous handles are now drained. We now mark the inodes on the
  968. * commit queue as being committed.
  969. */
  970. alloc_ctx = ext4_fc_lock(sb);
  971. list_for_each_entry(iter, &sbi->s_fc_q[FC_Q_MAIN], i_fc_list) {
  972. ext4_set_inode_state(&iter->vfs_inode,
  973. EXT4_STATE_FC_COMMITTING);
  974. }
  975. ext4_fc_unlock(sb, alloc_ctx);
  976. jbd2_journal_unlock_updates(journal);
  977. /*
  978. * Step 5: If file system device is different from journal device,
  979. * issue a cache flush before we start writing fast commit blocks.
  980. */
  981. if (journal->j_fs_dev != journal->j_dev)
  982. blkdev_issue_flush(journal->j_fs_dev);
  983. blk_start_plug(&plug);
  984. alloc_ctx = ext4_fc_lock(sb);
  985. /* Step 6: Write fast commit blocks to disk. */
  986. if (sbi->s_fc_bytes == 0) {
  987. /*
  988. * Step 6.1: Add a head tag only if this is the first fast
  989. * commit in this TID.
  990. */
  991. head.fc_features = cpu_to_le32(EXT4_FC_SUPPORTED_FEATURES);
  992. head.fc_tid = cpu_to_le32(
  993. sbi->s_journal->j_running_transaction->t_tid);
  994. if (!ext4_fc_add_tlv(sb, EXT4_FC_TAG_HEAD, sizeof(head),
  995. (u8 *)&head, &crc)) {
  996. ret = -ENOSPC;
  997. goto out;
  998. }
  999. }
  1000. /* Step 6.2: Now write all the dentry updates. */
  1001. ret = ext4_fc_commit_dentry_updates(journal, &crc);
  1002. if (ret)
  1003. goto out;
  1004. /* Step 6.3: Now write all the changed inodes to disk. */
  1005. list_for_each_entry(iter, &sbi->s_fc_q[FC_Q_MAIN], i_fc_list) {
  1006. inode = &iter->vfs_inode;
  1007. if (!ext4_test_inode_state(inode, EXT4_STATE_FC_COMMITTING))
  1008. continue;
  1009. ret = ext4_fc_write_inode_data(inode, &crc);
  1010. if (ret)
  1011. goto out;
  1012. ret = ext4_fc_write_inode(inode, &crc);
  1013. if (ret)
  1014. goto out;
  1015. }
  1016. /* Step 6.4: Finally write tail tag to conclude this fast commit. */
  1017. ret = ext4_fc_write_tail(sb, crc);
  1018. out:
  1019. ext4_fc_unlock(sb, alloc_ctx);
  1020. blk_finish_plug(&plug);
  1021. return ret;
  1022. }
  1023. static void ext4_fc_update_stats(struct super_block *sb, int status,
  1024. u64 commit_time, int nblks, tid_t commit_tid)
  1025. {
  1026. struct ext4_fc_stats *stats = &EXT4_SB(sb)->s_fc_stats;
  1027. ext4_debug("Fast commit ended with status = %d for tid %u",
  1028. status, commit_tid);
  1029. if (status == EXT4_FC_STATUS_OK) {
  1030. stats->fc_num_commits++;
  1031. stats->fc_numblks += nblks;
  1032. if (likely(stats->s_fc_avg_commit_time))
  1033. stats->s_fc_avg_commit_time =
  1034. (commit_time +
  1035. stats->s_fc_avg_commit_time * 3) / 4;
  1036. else
  1037. stats->s_fc_avg_commit_time = commit_time;
  1038. } else if (status == EXT4_FC_STATUS_FAILED ||
  1039. status == EXT4_FC_STATUS_INELIGIBLE) {
  1040. if (status == EXT4_FC_STATUS_FAILED)
  1041. stats->fc_failed_commits++;
  1042. stats->fc_ineligible_commits++;
  1043. } else {
  1044. stats->fc_skipped_commits++;
  1045. }
  1046. trace_ext4_fc_commit_stop(sb, nblks, status, commit_tid);
  1047. }
  1048. /*
  1049. * The main commit entry point. Performs a fast commit for transaction
  1050. * commit_tid if needed. If it's not possible to perform a fast commit
  1051. * due to various reasons, we fall back to full commit. Returns 0
  1052. * on success, error otherwise.
  1053. */
  1054. int ext4_fc_commit(journal_t *journal, tid_t commit_tid)
  1055. {
  1056. struct super_block *sb = journal->j_private;
  1057. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1058. int nblks = 0, ret, bsize = journal->j_blocksize;
  1059. int subtid = atomic_read(&sbi->s_fc_subtid);
  1060. int status = EXT4_FC_STATUS_OK, fc_bufs_before = 0;
  1061. ktime_t start_time, commit_time;
  1062. int old_ioprio, journal_ioprio;
  1063. if (!test_opt2(sb, JOURNAL_FAST_COMMIT))
  1064. return jbd2_complete_transaction(journal, commit_tid);
  1065. trace_ext4_fc_commit_start(sb, commit_tid);
  1066. start_time = ktime_get();
  1067. old_ioprio = get_current_ioprio();
  1068. restart_fc:
  1069. ret = jbd2_fc_begin_commit(journal, commit_tid);
  1070. if (ret == -EALREADY) {
  1071. /* There was an ongoing commit, check if we need to restart */
  1072. if (atomic_read(&sbi->s_fc_subtid) <= subtid &&
  1073. tid_gt(commit_tid, journal->j_commit_sequence))
  1074. goto restart_fc;
  1075. ext4_fc_update_stats(sb, EXT4_FC_STATUS_SKIPPED, 0, 0,
  1076. commit_tid);
  1077. return 0;
  1078. } else if (ret) {
  1079. /*
  1080. * Commit couldn't start. Just update stats and perform a
  1081. * full commit.
  1082. */
  1083. ext4_fc_update_stats(sb, EXT4_FC_STATUS_FAILED, 0, 0,
  1084. commit_tid);
  1085. return jbd2_complete_transaction(journal, commit_tid);
  1086. }
  1087. /*
  1088. * After establishing journal barrier via jbd2_fc_begin_commit(), check
  1089. * if we are fast commit ineligible.
  1090. */
  1091. if (ext4_test_mount_flag(sb, EXT4_MF_FC_INELIGIBLE)) {
  1092. status = EXT4_FC_STATUS_INELIGIBLE;
  1093. goto fallback;
  1094. }
  1095. /*
  1096. * Now that we know that this thread is going to do a fast commit,
  1097. * elevate the priority to match that of the journal thread.
  1098. */
  1099. if (journal->j_task->io_context)
  1100. journal_ioprio = sbi->s_journal->j_task->io_context->ioprio;
  1101. else
  1102. journal_ioprio = EXT4_DEF_JOURNAL_IOPRIO;
  1103. set_task_ioprio(current, journal_ioprio);
  1104. fc_bufs_before = (sbi->s_fc_bytes + bsize - 1) / bsize;
  1105. ret = ext4_fc_perform_commit(journal);
  1106. if (ret < 0) {
  1107. status = EXT4_FC_STATUS_FAILED;
  1108. goto fallback;
  1109. }
  1110. nblks = (sbi->s_fc_bytes + bsize - 1) / bsize - fc_bufs_before;
  1111. ret = jbd2_fc_wait_bufs(journal, nblks);
  1112. if (ret < 0) {
  1113. status = EXT4_FC_STATUS_FAILED;
  1114. goto fallback;
  1115. }
  1116. atomic_inc(&sbi->s_fc_subtid);
  1117. ret = jbd2_fc_end_commit(journal);
  1118. set_task_ioprio(current, old_ioprio);
  1119. /*
  1120. * weight the commit time higher than the average time so we
  1121. * don't react too strongly to vast changes in the commit time
  1122. */
  1123. commit_time = ktime_to_ns(ktime_sub(ktime_get(), start_time));
  1124. ext4_fc_update_stats(sb, status, commit_time, nblks, commit_tid);
  1125. return ret;
  1126. fallback:
  1127. set_task_ioprio(current, old_ioprio);
  1128. ret = jbd2_fc_end_commit_fallback(journal);
  1129. ext4_fc_update_stats(sb, status, 0, 0, commit_tid);
  1130. return ret;
  1131. }
  1132. /*
  1133. * Fast commit cleanup routine. This is called after every fast commit and
  1134. * full commit. full is true if we are called after a full commit.
  1135. */
  1136. static void ext4_fc_cleanup(journal_t *journal, int full, tid_t tid)
  1137. {
  1138. struct super_block *sb = journal->j_private;
  1139. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1140. struct ext4_inode_info *ei;
  1141. struct ext4_fc_dentry_update *fc_dentry;
  1142. int alloc_ctx;
  1143. if (full && sbi->s_fc_bh)
  1144. sbi->s_fc_bh = NULL;
  1145. trace_ext4_fc_cleanup(journal, full, tid);
  1146. jbd2_fc_release_bufs(journal);
  1147. alloc_ctx = ext4_fc_lock(sb);
  1148. while (!list_empty(&sbi->s_fc_q[FC_Q_MAIN])) {
  1149. ei = list_first_entry(&sbi->s_fc_q[FC_Q_MAIN],
  1150. struct ext4_inode_info,
  1151. i_fc_list);
  1152. list_del_init(&ei->i_fc_list);
  1153. ext4_clear_inode_state(&ei->vfs_inode,
  1154. EXT4_STATE_FC_COMMITTING);
  1155. if (tid_geq(tid, ei->i_sync_tid)) {
  1156. ext4_fc_reset_inode(&ei->vfs_inode);
  1157. } else if (full) {
  1158. /*
  1159. * We are called after a full commit, inode has been
  1160. * modified while the commit was running. Re-enqueue
  1161. * the inode into STAGING, which will then be splice
  1162. * back into MAIN. This cannot happen during
  1163. * fastcommit because the journal is locked all the
  1164. * time in that case (and tid doesn't increase so
  1165. * tid check above isn't reliable).
  1166. */
  1167. list_add_tail(&ei->i_fc_list,
  1168. &sbi->s_fc_q[FC_Q_STAGING]);
  1169. }
  1170. /*
  1171. * Make sure clearing of EXT4_STATE_FC_COMMITTING is
  1172. * visible before we send the wakeup. Pairs with implicit
  1173. * barrier in prepare_to_wait() in ext4_fc_track_inode().
  1174. */
  1175. smp_mb();
  1176. #if (BITS_PER_LONG < 64)
  1177. wake_up_bit(&ei->i_state_flags, EXT4_STATE_FC_COMMITTING);
  1178. #else
  1179. wake_up_bit(&ei->i_flags, EXT4_STATE_FC_COMMITTING);
  1180. #endif
  1181. }
  1182. while (!list_empty(&sbi->s_fc_dentry_q[FC_Q_MAIN])) {
  1183. fc_dentry = list_first_entry(&sbi->s_fc_dentry_q[FC_Q_MAIN],
  1184. struct ext4_fc_dentry_update,
  1185. fcd_list);
  1186. list_del_init(&fc_dentry->fcd_list);
  1187. list_del_init(&fc_dentry->fcd_dilist);
  1188. release_dentry_name_snapshot(&fc_dentry->fcd_name);
  1189. kmem_cache_free(ext4_fc_dentry_cachep, fc_dentry);
  1190. }
  1191. list_splice_init(&sbi->s_fc_dentry_q[FC_Q_STAGING],
  1192. &sbi->s_fc_dentry_q[FC_Q_MAIN]);
  1193. list_splice_init(&sbi->s_fc_q[FC_Q_STAGING],
  1194. &sbi->s_fc_q[FC_Q_MAIN]);
  1195. if (tid_geq(tid, sbi->s_fc_ineligible_tid)) {
  1196. sbi->s_fc_ineligible_tid = 0;
  1197. ext4_clear_mount_flag(sb, EXT4_MF_FC_INELIGIBLE);
  1198. }
  1199. if (full)
  1200. sbi->s_fc_bytes = 0;
  1201. ext4_fc_unlock(sb, alloc_ctx);
  1202. trace_ext4_fc_stats(sb);
  1203. }
  1204. /* Ext4 Replay Path Routines */
  1205. /* Helper struct for dentry replay routines */
  1206. struct dentry_info_args {
  1207. int parent_ino, dname_len, ino, inode_len;
  1208. char *dname;
  1209. };
  1210. /* Same as struct ext4_fc_tl, but uses native endianness fields */
  1211. struct ext4_fc_tl_mem {
  1212. u16 fc_tag;
  1213. u16 fc_len;
  1214. };
  1215. static inline void tl_to_darg(struct dentry_info_args *darg,
  1216. struct ext4_fc_tl_mem *tl, u8 *val)
  1217. {
  1218. struct ext4_fc_dentry_info fcd;
  1219. memcpy(&fcd, val, sizeof(fcd));
  1220. darg->parent_ino = le32_to_cpu(fcd.fc_parent_ino);
  1221. darg->ino = le32_to_cpu(fcd.fc_ino);
  1222. darg->dname = val + offsetof(struct ext4_fc_dentry_info, fc_dname);
  1223. darg->dname_len = tl->fc_len - sizeof(struct ext4_fc_dentry_info);
  1224. }
  1225. static inline void ext4_fc_get_tl(struct ext4_fc_tl_mem *tl, u8 *val)
  1226. {
  1227. struct ext4_fc_tl tl_disk;
  1228. memcpy(&tl_disk, val, EXT4_FC_TAG_BASE_LEN);
  1229. tl->fc_len = le16_to_cpu(tl_disk.fc_len);
  1230. tl->fc_tag = le16_to_cpu(tl_disk.fc_tag);
  1231. }
  1232. /* Unlink replay function */
  1233. static int ext4_fc_replay_unlink(struct super_block *sb,
  1234. struct ext4_fc_tl_mem *tl, u8 *val)
  1235. {
  1236. struct inode *inode, *old_parent;
  1237. struct qstr entry;
  1238. struct dentry_info_args darg;
  1239. int ret = 0;
  1240. tl_to_darg(&darg, tl, val);
  1241. trace_ext4_fc_replay(sb, EXT4_FC_TAG_UNLINK, darg.ino,
  1242. darg.parent_ino, darg.dname_len);
  1243. entry.name = darg.dname;
  1244. entry.len = darg.dname_len;
  1245. inode = ext4_iget(sb, darg.ino, EXT4_IGET_NORMAL);
  1246. if (IS_ERR(inode)) {
  1247. ext4_debug("Inode %d not found", darg.ino);
  1248. return 0;
  1249. }
  1250. old_parent = ext4_iget(sb, darg.parent_ino,
  1251. EXT4_IGET_NORMAL);
  1252. if (IS_ERR(old_parent)) {
  1253. ext4_debug("Dir with inode %d not found", darg.parent_ino);
  1254. iput(inode);
  1255. return 0;
  1256. }
  1257. ret = __ext4_unlink(old_parent, &entry, inode, NULL);
  1258. /* -ENOENT ok coz it might not exist anymore. */
  1259. if (ret == -ENOENT)
  1260. ret = 0;
  1261. iput(old_parent);
  1262. iput(inode);
  1263. return ret;
  1264. }
  1265. static int ext4_fc_replay_link_internal(struct super_block *sb,
  1266. struct dentry_info_args *darg,
  1267. struct inode *inode)
  1268. {
  1269. struct inode *dir = NULL;
  1270. struct dentry *dentry_dir = NULL, *dentry_inode = NULL;
  1271. struct qstr qstr_dname = QSTR_INIT(darg->dname, darg->dname_len);
  1272. int ret = 0;
  1273. dir = ext4_iget(sb, darg->parent_ino, EXT4_IGET_NORMAL);
  1274. if (IS_ERR(dir)) {
  1275. ext4_debug("Dir with inode %d not found.", darg->parent_ino);
  1276. dir = NULL;
  1277. goto out;
  1278. }
  1279. dentry_dir = d_obtain_alias(dir);
  1280. if (IS_ERR(dentry_dir)) {
  1281. ext4_debug("Failed to obtain dentry");
  1282. dentry_dir = NULL;
  1283. goto out;
  1284. }
  1285. dentry_inode = d_alloc(dentry_dir, &qstr_dname);
  1286. if (!dentry_inode) {
  1287. ext4_debug("Inode dentry not created.");
  1288. ret = -ENOMEM;
  1289. goto out;
  1290. }
  1291. ret = __ext4_link(dir, inode, dentry_inode);
  1292. /*
  1293. * It's possible that link already existed since data blocks
  1294. * for the dir in question got persisted before we crashed OR
  1295. * we replayed this tag and crashed before the entire replay
  1296. * could complete.
  1297. */
  1298. if (ret && ret != -EEXIST) {
  1299. ext4_debug("Failed to link\n");
  1300. goto out;
  1301. }
  1302. ret = 0;
  1303. out:
  1304. if (dentry_dir) {
  1305. d_drop(dentry_dir);
  1306. dput(dentry_dir);
  1307. } else if (dir) {
  1308. iput(dir);
  1309. }
  1310. if (dentry_inode) {
  1311. d_drop(dentry_inode);
  1312. dput(dentry_inode);
  1313. }
  1314. return ret;
  1315. }
  1316. /* Link replay function */
  1317. static int ext4_fc_replay_link(struct super_block *sb,
  1318. struct ext4_fc_tl_mem *tl, u8 *val)
  1319. {
  1320. struct inode *inode;
  1321. struct dentry_info_args darg;
  1322. int ret = 0;
  1323. tl_to_darg(&darg, tl, val);
  1324. trace_ext4_fc_replay(sb, EXT4_FC_TAG_LINK, darg.ino,
  1325. darg.parent_ino, darg.dname_len);
  1326. inode = ext4_iget(sb, darg.ino, EXT4_IGET_NORMAL);
  1327. if (IS_ERR(inode)) {
  1328. ext4_debug("Inode not found.");
  1329. return 0;
  1330. }
  1331. ret = ext4_fc_replay_link_internal(sb, &darg, inode);
  1332. iput(inode);
  1333. return ret;
  1334. }
  1335. /*
  1336. * Record all the modified inodes during replay. We use this later to setup
  1337. * block bitmaps correctly.
  1338. */
  1339. static int ext4_fc_record_modified_inode(struct super_block *sb, int ino)
  1340. {
  1341. struct ext4_fc_replay_state *state;
  1342. int i;
  1343. state = &EXT4_SB(sb)->s_fc_replay_state;
  1344. for (i = 0; i < state->fc_modified_inodes_used; i++)
  1345. if (state->fc_modified_inodes[i] == ino)
  1346. return 0;
  1347. if (state->fc_modified_inodes_used == state->fc_modified_inodes_size) {
  1348. int *fc_modified_inodes;
  1349. fc_modified_inodes = krealloc(state->fc_modified_inodes,
  1350. sizeof(int) * (state->fc_modified_inodes_size +
  1351. EXT4_FC_REPLAY_REALLOC_INCREMENT),
  1352. GFP_KERNEL);
  1353. if (!fc_modified_inodes)
  1354. return -ENOMEM;
  1355. state->fc_modified_inodes = fc_modified_inodes;
  1356. state->fc_modified_inodes_size +=
  1357. EXT4_FC_REPLAY_REALLOC_INCREMENT;
  1358. }
  1359. state->fc_modified_inodes[state->fc_modified_inodes_used++] = ino;
  1360. return 0;
  1361. }
  1362. /*
  1363. * Inode replay function
  1364. */
  1365. static int ext4_fc_replay_inode(struct super_block *sb,
  1366. struct ext4_fc_tl_mem *tl, u8 *val)
  1367. {
  1368. struct ext4_fc_inode fc_inode;
  1369. struct ext4_inode *raw_inode;
  1370. struct ext4_inode *raw_fc_inode;
  1371. struct inode *inode = NULL;
  1372. struct ext4_iloc iloc;
  1373. int inode_len, ino, ret, tag = tl->fc_tag;
  1374. struct ext4_extent_header *eh;
  1375. size_t off_gen = offsetof(struct ext4_inode, i_generation);
  1376. memcpy(&fc_inode, val, sizeof(fc_inode));
  1377. ino = le32_to_cpu(fc_inode.fc_ino);
  1378. trace_ext4_fc_replay(sb, tag, ino, 0, 0);
  1379. inode = ext4_iget(sb, ino, EXT4_IGET_NORMAL);
  1380. if (!IS_ERR(inode)) {
  1381. ext4_ext_clear_bb(inode);
  1382. iput(inode);
  1383. }
  1384. inode = NULL;
  1385. ret = ext4_fc_record_modified_inode(sb, ino);
  1386. if (ret)
  1387. goto out;
  1388. raw_fc_inode = (struct ext4_inode *)
  1389. (val + offsetof(struct ext4_fc_inode, fc_raw_inode));
  1390. ret = ext4_get_fc_inode_loc(sb, ino, &iloc);
  1391. if (ret)
  1392. goto out;
  1393. inode_len = tl->fc_len - sizeof(struct ext4_fc_inode);
  1394. raw_inode = ext4_raw_inode(&iloc);
  1395. memcpy(raw_inode, raw_fc_inode, offsetof(struct ext4_inode, i_block));
  1396. memcpy((u8 *)raw_inode + off_gen, (u8 *)raw_fc_inode + off_gen,
  1397. inode_len - off_gen);
  1398. if (le32_to_cpu(raw_inode->i_flags) & EXT4_EXTENTS_FL) {
  1399. eh = (struct ext4_extent_header *)(&raw_inode->i_block[0]);
  1400. if (eh->eh_magic != EXT4_EXT_MAGIC) {
  1401. memset(eh, 0, sizeof(*eh));
  1402. eh->eh_magic = EXT4_EXT_MAGIC;
  1403. eh->eh_max = cpu_to_le16(
  1404. (sizeof(raw_inode->i_block) -
  1405. sizeof(struct ext4_extent_header))
  1406. / sizeof(struct ext4_extent));
  1407. }
  1408. } else if (le32_to_cpu(raw_inode->i_flags) & EXT4_INLINE_DATA_FL) {
  1409. memcpy(raw_inode->i_block, raw_fc_inode->i_block,
  1410. sizeof(raw_inode->i_block));
  1411. }
  1412. /* Immediately update the inode on disk. */
  1413. ret = ext4_handle_dirty_metadata(NULL, NULL, iloc.bh);
  1414. if (ret)
  1415. goto out_brelse;
  1416. ret = sync_dirty_buffer(iloc.bh);
  1417. if (ret)
  1418. goto out_brelse;
  1419. ret = ext4_mark_inode_used(sb, ino);
  1420. if (ret)
  1421. goto out_brelse;
  1422. /* Given that we just wrote the inode on disk, this SHOULD succeed. */
  1423. inode = ext4_iget(sb, ino, EXT4_IGET_NORMAL);
  1424. if (IS_ERR(inode)) {
  1425. ext4_debug("Inode not found.");
  1426. inode = NULL;
  1427. ret = -EFSCORRUPTED;
  1428. goto out_brelse;
  1429. }
  1430. /*
  1431. * Our allocator could have made different decisions than before
  1432. * crashing. This should be fixed but until then, we calculate
  1433. * the number of blocks the inode.
  1434. */
  1435. if (!ext4_test_inode_flag(inode, EXT4_INODE_INLINE_DATA))
  1436. ext4_ext_replay_set_iblocks(inode);
  1437. inode->i_generation = le32_to_cpu(ext4_raw_inode(&iloc)->i_generation);
  1438. ext4_reset_inode_seed(inode);
  1439. ext4_inode_csum_set(inode, ext4_raw_inode(&iloc), EXT4_I(inode));
  1440. ret = ext4_handle_dirty_metadata(NULL, NULL, iloc.bh);
  1441. sync_dirty_buffer(iloc.bh);
  1442. out_brelse:
  1443. brelse(iloc.bh);
  1444. out:
  1445. iput(inode);
  1446. if (!ret)
  1447. blkdev_issue_flush(sb->s_bdev);
  1448. return ret;
  1449. }
  1450. /*
  1451. * Dentry create replay function.
  1452. *
  1453. * EXT4_FC_TAG_CREAT is preceded by EXT4_FC_TAG_INODE_FULL. Which means, the
  1454. * inode for which we are trying to create a dentry here, should already have
  1455. * been replayed before we start here.
  1456. */
  1457. static int ext4_fc_replay_create(struct super_block *sb,
  1458. struct ext4_fc_tl_mem *tl, u8 *val)
  1459. {
  1460. int ret = 0;
  1461. struct inode *inode = NULL;
  1462. struct inode *dir = NULL;
  1463. struct dentry_info_args darg;
  1464. tl_to_darg(&darg, tl, val);
  1465. trace_ext4_fc_replay(sb, EXT4_FC_TAG_CREAT, darg.ino,
  1466. darg.parent_ino, darg.dname_len);
  1467. /* This takes care of update group descriptor and other metadata */
  1468. ret = ext4_mark_inode_used(sb, darg.ino);
  1469. if (ret)
  1470. goto out;
  1471. inode = ext4_iget(sb, darg.ino, EXT4_IGET_NORMAL);
  1472. if (IS_ERR(inode)) {
  1473. ext4_debug("inode %d not found.", darg.ino);
  1474. inode = NULL;
  1475. ret = -EINVAL;
  1476. goto out;
  1477. }
  1478. if (S_ISDIR(inode->i_mode)) {
  1479. /*
  1480. * If we are creating a directory, we need to make sure that the
  1481. * dot and dot dot dirents are setup properly.
  1482. */
  1483. dir = ext4_iget(sb, darg.parent_ino, EXT4_IGET_NORMAL);
  1484. if (IS_ERR(dir)) {
  1485. ext4_debug("Dir %d not found.", darg.ino);
  1486. goto out;
  1487. }
  1488. ret = ext4_init_new_dir(NULL, dir, inode);
  1489. iput(dir);
  1490. if (ret) {
  1491. ret = 0;
  1492. goto out;
  1493. }
  1494. }
  1495. ret = ext4_fc_replay_link_internal(sb, &darg, inode);
  1496. if (ret)
  1497. goto out;
  1498. set_nlink(inode, 1);
  1499. ext4_mark_inode_dirty(NULL, inode);
  1500. out:
  1501. iput(inode);
  1502. return ret;
  1503. }
  1504. /*
  1505. * Record physical disk regions which are in use as per fast commit area,
  1506. * and used by inodes during replay phase. Our simple replay phase
  1507. * allocator excludes these regions from allocation.
  1508. */
  1509. int ext4_fc_record_regions(struct super_block *sb, int ino,
  1510. ext4_lblk_t lblk, ext4_fsblk_t pblk, int len, int replay)
  1511. {
  1512. struct ext4_fc_replay_state *state;
  1513. struct ext4_fc_alloc_region *region;
  1514. state = &EXT4_SB(sb)->s_fc_replay_state;
  1515. /*
  1516. * during replay phase, the fc_regions_valid may not same as
  1517. * fc_regions_used, update it when do new additions.
  1518. */
  1519. if (replay && state->fc_regions_used != state->fc_regions_valid)
  1520. state->fc_regions_used = state->fc_regions_valid;
  1521. if (state->fc_regions_used == state->fc_regions_size) {
  1522. struct ext4_fc_alloc_region *fc_regions;
  1523. fc_regions = krealloc(state->fc_regions,
  1524. sizeof(struct ext4_fc_alloc_region) *
  1525. (state->fc_regions_size +
  1526. EXT4_FC_REPLAY_REALLOC_INCREMENT),
  1527. GFP_KERNEL);
  1528. if (!fc_regions)
  1529. return -ENOMEM;
  1530. state->fc_regions_size +=
  1531. EXT4_FC_REPLAY_REALLOC_INCREMENT;
  1532. state->fc_regions = fc_regions;
  1533. }
  1534. region = &state->fc_regions[state->fc_regions_used++];
  1535. region->ino = ino;
  1536. region->lblk = lblk;
  1537. region->pblk = pblk;
  1538. region->len = len;
  1539. if (replay)
  1540. state->fc_regions_valid++;
  1541. return 0;
  1542. }
  1543. /* Replay add range tag */
  1544. static int ext4_fc_replay_add_range(struct super_block *sb,
  1545. struct ext4_fc_tl_mem *tl, u8 *val)
  1546. {
  1547. struct ext4_fc_add_range fc_add_ex;
  1548. struct ext4_extent newex, *ex;
  1549. struct inode *inode;
  1550. ext4_lblk_t start, cur;
  1551. int remaining, len;
  1552. ext4_fsblk_t start_pblk;
  1553. struct ext4_map_blocks map;
  1554. struct ext4_ext_path *path = NULL;
  1555. int ret;
  1556. memcpy(&fc_add_ex, val, sizeof(fc_add_ex));
  1557. ex = (struct ext4_extent *)&fc_add_ex.fc_ex;
  1558. trace_ext4_fc_replay(sb, EXT4_FC_TAG_ADD_RANGE,
  1559. le32_to_cpu(fc_add_ex.fc_ino), le32_to_cpu(ex->ee_block),
  1560. ext4_ext_get_actual_len(ex));
  1561. inode = ext4_iget(sb, le32_to_cpu(fc_add_ex.fc_ino), EXT4_IGET_NORMAL);
  1562. if (IS_ERR(inode)) {
  1563. ext4_debug("Inode not found.");
  1564. return 0;
  1565. }
  1566. ret = ext4_fc_record_modified_inode(sb, inode->i_ino);
  1567. if (ret)
  1568. goto out;
  1569. start = le32_to_cpu(ex->ee_block);
  1570. start_pblk = ext4_ext_pblock(ex);
  1571. len = ext4_ext_get_actual_len(ex);
  1572. cur = start;
  1573. remaining = len;
  1574. ext4_debug("ADD_RANGE, lblk %d, pblk %lld, len %d, unwritten %d, inode %ld\n",
  1575. start, start_pblk, len, ext4_ext_is_unwritten(ex),
  1576. inode->i_ino);
  1577. while (remaining > 0) {
  1578. map.m_lblk = cur;
  1579. map.m_len = remaining;
  1580. map.m_pblk = 0;
  1581. ret = ext4_map_blocks(NULL, inode, &map, 0);
  1582. if (ret < 0)
  1583. goto out;
  1584. if (ret == 0) {
  1585. /* Range is not mapped */
  1586. path = ext4_find_extent(inode, cur, path, 0);
  1587. if (IS_ERR(path))
  1588. goto out;
  1589. memset(&newex, 0, sizeof(newex));
  1590. newex.ee_block = cpu_to_le32(cur);
  1591. ext4_ext_store_pblock(
  1592. &newex, start_pblk + cur - start);
  1593. newex.ee_len = cpu_to_le16(map.m_len);
  1594. if (ext4_ext_is_unwritten(ex))
  1595. ext4_ext_mark_unwritten(&newex);
  1596. down_write(&EXT4_I(inode)->i_data_sem);
  1597. path = ext4_ext_insert_extent(NULL, inode,
  1598. path, &newex, 0);
  1599. up_write((&EXT4_I(inode)->i_data_sem));
  1600. if (IS_ERR(path))
  1601. goto out;
  1602. goto next;
  1603. }
  1604. if (start_pblk + cur - start != map.m_pblk) {
  1605. /*
  1606. * Logical to physical mapping changed. This can happen
  1607. * if this range was removed and then reallocated to
  1608. * map to new physical blocks during a fast commit.
  1609. */
  1610. ret = ext4_ext_replay_update_ex(inode, cur, map.m_len,
  1611. ext4_ext_is_unwritten(ex),
  1612. start_pblk + cur - start);
  1613. if (ret)
  1614. goto out;
  1615. /*
  1616. * Mark the old blocks as free since they aren't used
  1617. * anymore. We maintain an array of all the modified
  1618. * inodes. In case these blocks are still used at either
  1619. * a different logical range in the same inode or in
  1620. * some different inode, we will mark them as allocated
  1621. * at the end of the FC replay using our array of
  1622. * modified inodes.
  1623. */
  1624. ext4_mb_mark_bb(inode->i_sb, map.m_pblk, map.m_len, false);
  1625. goto next;
  1626. }
  1627. /* Range is mapped and needs a state change */
  1628. ext4_debug("Converting from %ld to %d %lld",
  1629. map.m_flags & EXT4_MAP_UNWRITTEN,
  1630. ext4_ext_is_unwritten(ex), map.m_pblk);
  1631. ret = ext4_ext_replay_update_ex(inode, cur, map.m_len,
  1632. ext4_ext_is_unwritten(ex), map.m_pblk);
  1633. if (ret)
  1634. goto out;
  1635. /*
  1636. * We may have split the extent tree while toggling the state.
  1637. * Try to shrink the extent tree now.
  1638. */
  1639. ext4_ext_replay_shrink_inode(inode, start + len);
  1640. next:
  1641. cur += map.m_len;
  1642. remaining -= map.m_len;
  1643. }
  1644. ext4_ext_replay_shrink_inode(inode, i_size_read(inode) >>
  1645. sb->s_blocksize_bits);
  1646. out:
  1647. ext4_free_ext_path(path);
  1648. iput(inode);
  1649. return 0;
  1650. }
  1651. /* Replay DEL_RANGE tag */
  1652. static int
  1653. ext4_fc_replay_del_range(struct super_block *sb,
  1654. struct ext4_fc_tl_mem *tl, u8 *val)
  1655. {
  1656. struct inode *inode;
  1657. struct ext4_fc_del_range lrange;
  1658. struct ext4_map_blocks map;
  1659. ext4_lblk_t cur, remaining;
  1660. int ret;
  1661. memcpy(&lrange, val, sizeof(lrange));
  1662. cur = le32_to_cpu(lrange.fc_lblk);
  1663. remaining = le32_to_cpu(lrange.fc_len);
  1664. trace_ext4_fc_replay(sb, EXT4_FC_TAG_DEL_RANGE,
  1665. le32_to_cpu(lrange.fc_ino), cur, remaining);
  1666. inode = ext4_iget(sb, le32_to_cpu(lrange.fc_ino), EXT4_IGET_NORMAL);
  1667. if (IS_ERR(inode)) {
  1668. ext4_debug("Inode %d not found", le32_to_cpu(lrange.fc_ino));
  1669. return 0;
  1670. }
  1671. ret = ext4_fc_record_modified_inode(sb, inode->i_ino);
  1672. if (ret)
  1673. goto out;
  1674. ext4_debug("DEL_RANGE, inode %ld, lblk %d, len %d\n",
  1675. inode->i_ino, le32_to_cpu(lrange.fc_lblk),
  1676. le32_to_cpu(lrange.fc_len));
  1677. while (remaining > 0) {
  1678. map.m_lblk = cur;
  1679. map.m_len = remaining;
  1680. ret = ext4_map_blocks(NULL, inode, &map, 0);
  1681. if (ret < 0)
  1682. goto out;
  1683. if (ret > 0) {
  1684. remaining -= ret;
  1685. cur += ret;
  1686. ext4_mb_mark_bb(inode->i_sb, map.m_pblk, map.m_len, false);
  1687. } else {
  1688. remaining -= map.m_len;
  1689. cur += map.m_len;
  1690. }
  1691. }
  1692. down_write(&EXT4_I(inode)->i_data_sem);
  1693. ret = ext4_ext_remove_space(inode, le32_to_cpu(lrange.fc_lblk),
  1694. le32_to_cpu(lrange.fc_lblk) +
  1695. le32_to_cpu(lrange.fc_len) - 1);
  1696. up_write(&EXT4_I(inode)->i_data_sem);
  1697. if (ret)
  1698. goto out;
  1699. ext4_ext_replay_shrink_inode(inode,
  1700. i_size_read(inode) >> sb->s_blocksize_bits);
  1701. ext4_mark_inode_dirty(NULL, inode);
  1702. out:
  1703. iput(inode);
  1704. return 0;
  1705. }
  1706. static void ext4_fc_set_bitmaps_and_counters(struct super_block *sb)
  1707. {
  1708. struct ext4_fc_replay_state *state;
  1709. struct inode *inode;
  1710. struct ext4_ext_path *path = NULL;
  1711. struct ext4_map_blocks map;
  1712. int i, ret, j;
  1713. ext4_lblk_t cur, end;
  1714. state = &EXT4_SB(sb)->s_fc_replay_state;
  1715. for (i = 0; i < state->fc_modified_inodes_used; i++) {
  1716. inode = ext4_iget(sb, state->fc_modified_inodes[i],
  1717. EXT4_IGET_NORMAL);
  1718. if (IS_ERR(inode)) {
  1719. ext4_debug("Inode %d not found.",
  1720. state->fc_modified_inodes[i]);
  1721. continue;
  1722. }
  1723. cur = 0;
  1724. end = EXT_MAX_BLOCKS;
  1725. if (ext4_test_inode_flag(inode, EXT4_INODE_INLINE_DATA)) {
  1726. iput(inode);
  1727. continue;
  1728. }
  1729. while (cur < end) {
  1730. map.m_lblk = cur;
  1731. map.m_len = end - cur;
  1732. ret = ext4_map_blocks(NULL, inode, &map, 0);
  1733. if (ret < 0)
  1734. break;
  1735. if (ret > 0) {
  1736. path = ext4_find_extent(inode, map.m_lblk, path, 0);
  1737. if (!IS_ERR(path)) {
  1738. for (j = 0; j < path->p_depth; j++)
  1739. ext4_mb_mark_bb(inode->i_sb,
  1740. path[j].p_block, 1, true);
  1741. } else {
  1742. path = NULL;
  1743. }
  1744. cur += ret;
  1745. ext4_mb_mark_bb(inode->i_sb, map.m_pblk,
  1746. map.m_len, true);
  1747. } else {
  1748. cur = cur + (map.m_len ? map.m_len : 1);
  1749. }
  1750. }
  1751. iput(inode);
  1752. }
  1753. ext4_free_ext_path(path);
  1754. }
  1755. /*
  1756. * Check if block is in excluded regions for block allocation. The simple
  1757. * allocator that runs during replay phase is calls this function to see
  1758. * if it is okay to use a block.
  1759. */
  1760. bool ext4_fc_replay_check_excluded(struct super_block *sb, ext4_fsblk_t blk)
  1761. {
  1762. int i;
  1763. struct ext4_fc_replay_state *state;
  1764. state = &EXT4_SB(sb)->s_fc_replay_state;
  1765. for (i = 0; i < state->fc_regions_valid; i++) {
  1766. if (state->fc_regions[i].ino == 0 ||
  1767. state->fc_regions[i].len == 0)
  1768. continue;
  1769. if (in_range(blk, state->fc_regions[i].pblk,
  1770. state->fc_regions[i].len))
  1771. return true;
  1772. }
  1773. return false;
  1774. }
  1775. /* Cleanup function called after replay */
  1776. void ext4_fc_replay_cleanup(struct super_block *sb)
  1777. {
  1778. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1779. sbi->s_mount_state &= ~EXT4_FC_REPLAY;
  1780. kfree(sbi->s_fc_replay_state.fc_regions);
  1781. kfree(sbi->s_fc_replay_state.fc_modified_inodes);
  1782. }
  1783. static bool ext4_fc_value_len_isvalid(struct ext4_sb_info *sbi,
  1784. int tag, int len)
  1785. {
  1786. switch (tag) {
  1787. case EXT4_FC_TAG_ADD_RANGE:
  1788. return len == sizeof(struct ext4_fc_add_range);
  1789. case EXT4_FC_TAG_DEL_RANGE:
  1790. return len == sizeof(struct ext4_fc_del_range);
  1791. case EXT4_FC_TAG_CREAT:
  1792. case EXT4_FC_TAG_LINK:
  1793. case EXT4_FC_TAG_UNLINK:
  1794. len -= sizeof(struct ext4_fc_dentry_info);
  1795. return len >= 1 && len <= EXT4_NAME_LEN;
  1796. case EXT4_FC_TAG_INODE:
  1797. len -= sizeof(struct ext4_fc_inode);
  1798. return len >= EXT4_GOOD_OLD_INODE_SIZE &&
  1799. len <= sbi->s_inode_size;
  1800. case EXT4_FC_TAG_PAD:
  1801. return true; /* padding can have any length */
  1802. case EXT4_FC_TAG_TAIL:
  1803. return len >= sizeof(struct ext4_fc_tail);
  1804. case EXT4_FC_TAG_HEAD:
  1805. return len == sizeof(struct ext4_fc_head);
  1806. }
  1807. return false;
  1808. }
  1809. /*
  1810. * Recovery Scan phase handler
  1811. *
  1812. * This function is called during the scan phase and is responsible
  1813. * for doing following things:
  1814. * - Make sure the fast commit area has valid tags for replay
  1815. * - Count number of tags that need to be replayed by the replay handler
  1816. * - Verify CRC
  1817. * - Create a list of excluded blocks for allocation during replay phase
  1818. *
  1819. * This function returns JBD2_FC_REPLAY_CONTINUE to indicate that SCAN is
  1820. * incomplete and JBD2 should send more blocks. It returns JBD2_FC_REPLAY_STOP
  1821. * to indicate that scan has finished and JBD2 can now start replay phase.
  1822. * It returns a negative error to indicate that there was an error. At the end
  1823. * of a successful scan phase, sbi->s_fc_replay_state.fc_replay_num_tags is set
  1824. * to indicate the number of tags that need to replayed during the replay phase.
  1825. */
  1826. static int ext4_fc_replay_scan(journal_t *journal,
  1827. struct buffer_head *bh, int off,
  1828. tid_t expected_tid)
  1829. {
  1830. struct super_block *sb = journal->j_private;
  1831. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1832. struct ext4_fc_replay_state *state;
  1833. int ret = JBD2_FC_REPLAY_CONTINUE;
  1834. struct ext4_fc_add_range ext;
  1835. struct ext4_fc_tl_mem tl;
  1836. struct ext4_fc_tail tail;
  1837. __u8 *start, *end, *cur, *val;
  1838. struct ext4_fc_head head;
  1839. struct ext4_extent *ex;
  1840. state = &sbi->s_fc_replay_state;
  1841. start = (u8 *)bh->b_data;
  1842. end = start + journal->j_blocksize;
  1843. if (state->fc_replay_expected_off == 0) {
  1844. state->fc_cur_tag = 0;
  1845. state->fc_replay_num_tags = 0;
  1846. state->fc_crc = 0;
  1847. state->fc_regions = NULL;
  1848. state->fc_regions_valid = state->fc_regions_used =
  1849. state->fc_regions_size = 0;
  1850. /* Check if we can stop early */
  1851. if (le16_to_cpu(((struct ext4_fc_tl *)start)->fc_tag)
  1852. != EXT4_FC_TAG_HEAD)
  1853. return 0;
  1854. }
  1855. if (off != state->fc_replay_expected_off) {
  1856. ret = -EFSCORRUPTED;
  1857. goto out_err;
  1858. }
  1859. state->fc_replay_expected_off++;
  1860. for (cur = start; cur <= end - EXT4_FC_TAG_BASE_LEN;
  1861. cur = cur + EXT4_FC_TAG_BASE_LEN + tl.fc_len) {
  1862. ext4_fc_get_tl(&tl, cur);
  1863. val = cur + EXT4_FC_TAG_BASE_LEN;
  1864. if (tl.fc_len > end - val ||
  1865. !ext4_fc_value_len_isvalid(sbi, tl.fc_tag, tl.fc_len)) {
  1866. ret = state->fc_replay_num_tags ?
  1867. JBD2_FC_REPLAY_STOP : -ECANCELED;
  1868. goto out_err;
  1869. }
  1870. ext4_debug("Scan phase, tag:%s, blk %lld\n",
  1871. tag2str(tl.fc_tag), bh->b_blocknr);
  1872. switch (tl.fc_tag) {
  1873. case EXT4_FC_TAG_ADD_RANGE:
  1874. memcpy(&ext, val, sizeof(ext));
  1875. ex = (struct ext4_extent *)&ext.fc_ex;
  1876. ret = ext4_fc_record_regions(sb,
  1877. le32_to_cpu(ext.fc_ino),
  1878. le32_to_cpu(ex->ee_block), ext4_ext_pblock(ex),
  1879. ext4_ext_get_actual_len(ex), 0);
  1880. if (ret < 0)
  1881. break;
  1882. ret = JBD2_FC_REPLAY_CONTINUE;
  1883. fallthrough;
  1884. case EXT4_FC_TAG_DEL_RANGE:
  1885. case EXT4_FC_TAG_LINK:
  1886. case EXT4_FC_TAG_UNLINK:
  1887. case EXT4_FC_TAG_CREAT:
  1888. case EXT4_FC_TAG_INODE:
  1889. case EXT4_FC_TAG_PAD:
  1890. state->fc_cur_tag++;
  1891. state->fc_crc = ext4_chksum(state->fc_crc, cur,
  1892. EXT4_FC_TAG_BASE_LEN + tl.fc_len);
  1893. break;
  1894. case EXT4_FC_TAG_TAIL:
  1895. state->fc_cur_tag++;
  1896. memcpy(&tail, val, sizeof(tail));
  1897. state->fc_crc = ext4_chksum(state->fc_crc, cur,
  1898. EXT4_FC_TAG_BASE_LEN +
  1899. offsetof(struct ext4_fc_tail,
  1900. fc_crc));
  1901. if (le32_to_cpu(tail.fc_tid) == expected_tid &&
  1902. le32_to_cpu(tail.fc_crc) == state->fc_crc) {
  1903. state->fc_replay_num_tags = state->fc_cur_tag;
  1904. state->fc_regions_valid =
  1905. state->fc_regions_used;
  1906. } else {
  1907. ret = state->fc_replay_num_tags ?
  1908. JBD2_FC_REPLAY_STOP : -EFSBADCRC;
  1909. }
  1910. state->fc_crc = 0;
  1911. break;
  1912. case EXT4_FC_TAG_HEAD:
  1913. memcpy(&head, val, sizeof(head));
  1914. if (le32_to_cpu(head.fc_features) &
  1915. ~EXT4_FC_SUPPORTED_FEATURES) {
  1916. ret = -EOPNOTSUPP;
  1917. break;
  1918. }
  1919. if (le32_to_cpu(head.fc_tid) != expected_tid) {
  1920. ret = JBD2_FC_REPLAY_STOP;
  1921. break;
  1922. }
  1923. state->fc_cur_tag++;
  1924. state->fc_crc = ext4_chksum(state->fc_crc, cur,
  1925. EXT4_FC_TAG_BASE_LEN + tl.fc_len);
  1926. break;
  1927. default:
  1928. ret = state->fc_replay_num_tags ?
  1929. JBD2_FC_REPLAY_STOP : -ECANCELED;
  1930. }
  1931. if (ret < 0 || ret == JBD2_FC_REPLAY_STOP)
  1932. break;
  1933. }
  1934. out_err:
  1935. trace_ext4_fc_replay_scan(sb, ret, off);
  1936. return ret;
  1937. }
  1938. /*
  1939. * Main recovery path entry point.
  1940. * The meaning of return codes is similar as above.
  1941. */
  1942. static int ext4_fc_replay(journal_t *journal, struct buffer_head *bh,
  1943. enum passtype pass, int off, tid_t expected_tid)
  1944. {
  1945. struct super_block *sb = journal->j_private;
  1946. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1947. struct ext4_fc_tl_mem tl;
  1948. __u8 *start, *end, *cur, *val;
  1949. int ret = JBD2_FC_REPLAY_CONTINUE;
  1950. struct ext4_fc_replay_state *state = &sbi->s_fc_replay_state;
  1951. struct ext4_fc_tail tail;
  1952. if (pass == PASS_SCAN) {
  1953. state->fc_current_pass = PASS_SCAN;
  1954. return ext4_fc_replay_scan(journal, bh, off, expected_tid);
  1955. }
  1956. if (state->fc_current_pass != pass) {
  1957. state->fc_current_pass = pass;
  1958. sbi->s_mount_state |= EXT4_FC_REPLAY;
  1959. }
  1960. if (!sbi->s_fc_replay_state.fc_replay_num_tags) {
  1961. ext4_debug("Replay stops\n");
  1962. ext4_fc_set_bitmaps_and_counters(sb);
  1963. return 0;
  1964. }
  1965. #ifdef CONFIG_EXT4_DEBUG
  1966. if (sbi->s_fc_debug_max_replay && off >= sbi->s_fc_debug_max_replay) {
  1967. pr_warn("Dropping fc block %d because max_replay set\n", off);
  1968. return JBD2_FC_REPLAY_STOP;
  1969. }
  1970. #endif
  1971. start = (u8 *)bh->b_data;
  1972. end = start + journal->j_blocksize;
  1973. for (cur = start; cur <= end - EXT4_FC_TAG_BASE_LEN;
  1974. cur = cur + EXT4_FC_TAG_BASE_LEN + tl.fc_len) {
  1975. ext4_fc_get_tl(&tl, cur);
  1976. val = cur + EXT4_FC_TAG_BASE_LEN;
  1977. if (state->fc_replay_num_tags == 0) {
  1978. ret = JBD2_FC_REPLAY_STOP;
  1979. ext4_fc_set_bitmaps_and_counters(sb);
  1980. break;
  1981. }
  1982. ext4_debug("Replay phase, tag:%s\n", tag2str(tl.fc_tag));
  1983. state->fc_replay_num_tags--;
  1984. switch (tl.fc_tag) {
  1985. case EXT4_FC_TAG_LINK:
  1986. ret = ext4_fc_replay_link(sb, &tl, val);
  1987. break;
  1988. case EXT4_FC_TAG_UNLINK:
  1989. ret = ext4_fc_replay_unlink(sb, &tl, val);
  1990. break;
  1991. case EXT4_FC_TAG_ADD_RANGE:
  1992. ret = ext4_fc_replay_add_range(sb, &tl, val);
  1993. break;
  1994. case EXT4_FC_TAG_CREAT:
  1995. ret = ext4_fc_replay_create(sb, &tl, val);
  1996. break;
  1997. case EXT4_FC_TAG_DEL_RANGE:
  1998. ret = ext4_fc_replay_del_range(sb, &tl, val);
  1999. break;
  2000. case EXT4_FC_TAG_INODE:
  2001. ret = ext4_fc_replay_inode(sb, &tl, val);
  2002. break;
  2003. case EXT4_FC_TAG_PAD:
  2004. trace_ext4_fc_replay(sb, EXT4_FC_TAG_PAD, 0,
  2005. tl.fc_len, 0);
  2006. break;
  2007. case EXT4_FC_TAG_TAIL:
  2008. trace_ext4_fc_replay(sb, EXT4_FC_TAG_TAIL,
  2009. 0, tl.fc_len, 0);
  2010. memcpy(&tail, val, sizeof(tail));
  2011. WARN_ON(le32_to_cpu(tail.fc_tid) != expected_tid);
  2012. break;
  2013. case EXT4_FC_TAG_HEAD:
  2014. break;
  2015. default:
  2016. trace_ext4_fc_replay(sb, tl.fc_tag, 0, tl.fc_len, 0);
  2017. ret = -ECANCELED;
  2018. break;
  2019. }
  2020. if (ret < 0)
  2021. break;
  2022. ret = JBD2_FC_REPLAY_CONTINUE;
  2023. }
  2024. return ret;
  2025. }
  2026. void ext4_fc_init(struct super_block *sb, journal_t *journal)
  2027. {
  2028. /*
  2029. * We set replay callback even if fast commit disabled because we may
  2030. * could still have fast commit blocks that need to be replayed even if
  2031. * fast commit has now been turned off.
  2032. */
  2033. journal->j_fc_replay_callback = ext4_fc_replay;
  2034. if (!test_opt2(sb, JOURNAL_FAST_COMMIT))
  2035. return;
  2036. journal->j_fc_cleanup_callback = ext4_fc_cleanup;
  2037. }
  2038. static const char * const fc_ineligible_reasons[] = {
  2039. [EXT4_FC_REASON_XATTR] = "Extended attributes changed",
  2040. [EXT4_FC_REASON_CROSS_RENAME] = "Cross rename",
  2041. [EXT4_FC_REASON_JOURNAL_FLAG_CHANGE] = "Journal flag changed",
  2042. [EXT4_FC_REASON_NOMEM] = "Insufficient memory",
  2043. [EXT4_FC_REASON_SWAP_BOOT] = "Swap boot",
  2044. [EXT4_FC_REASON_RESIZE] = "Resize",
  2045. [EXT4_FC_REASON_RENAME_DIR] = "Dir renamed",
  2046. [EXT4_FC_REASON_FALLOC_RANGE] = "Falloc range op",
  2047. [EXT4_FC_REASON_INODE_JOURNAL_DATA] = "Data journalling",
  2048. [EXT4_FC_REASON_ENCRYPTED_FILENAME] = "Encrypted filename",
  2049. [EXT4_FC_REASON_MIGRATE] = "Inode format migration",
  2050. [EXT4_FC_REASON_VERITY] = "fs-verity enable",
  2051. [EXT4_FC_REASON_MOVE_EXT] = "Move extents",
  2052. };
  2053. int ext4_fc_info_show(struct seq_file *seq, void *v)
  2054. {
  2055. struct ext4_sb_info *sbi = EXT4_SB((struct super_block *)seq->private);
  2056. struct ext4_fc_stats *stats = &sbi->s_fc_stats;
  2057. int i;
  2058. if (v != SEQ_START_TOKEN)
  2059. return 0;
  2060. seq_printf(seq,
  2061. "fc stats:\n%ld commits\n%ld ineligible\n%ld numblks\n%lluus avg_commit_time\n",
  2062. stats->fc_num_commits, stats->fc_ineligible_commits,
  2063. stats->fc_numblks,
  2064. div_u64(stats->s_fc_avg_commit_time, 1000));
  2065. seq_puts(seq, "Ineligible reasons:\n");
  2066. for (i = 0; i < EXT4_FC_REASON_MAX; i++)
  2067. seq_printf(seq, "\"%s\":\t%d\n", fc_ineligible_reasons[i],
  2068. stats->fc_ineligible_reason_count[i]);
  2069. return 0;
  2070. }
  2071. int __init ext4_fc_init_dentry_cache(void)
  2072. {
  2073. ext4_fc_dentry_cachep = KMEM_CACHE(ext4_fc_dentry_update,
  2074. SLAB_RECLAIM_ACCOUNT);
  2075. if (ext4_fc_dentry_cachep == NULL)
  2076. return -ENOMEM;
  2077. return 0;
  2078. }
  2079. void ext4_fc_destroy_dentry_cache(void)
  2080. {
  2081. kmem_cache_destroy(ext4_fc_dentry_cachep);
  2082. }