fast_commit.c 65 KB

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