transaction.c 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589
  1. /*
  2. * linux/fs/jbd2/transaction.c
  3. *
  4. * Written by Stephen C. Tweedie <sct@redhat.com>, 1998
  5. *
  6. * Copyright 1998 Red Hat corp --- All Rights Reserved
  7. *
  8. * This file is part of the Linux kernel and is made available under
  9. * the terms of the GNU General Public License, version 2, or at your
  10. * option, any later version, incorporated herein by reference.
  11. *
  12. * Generic filesystem transaction handling code; part of the ext2fs
  13. * journaling system.
  14. *
  15. * This file manages transactions (compound commits managed by the
  16. * journaling code) and handles (individual atomic operations by the
  17. * filesystem).
  18. */
  19. #include <linux/time.h>
  20. #include <linux/fs.h>
  21. #include <linux/jbd2.h>
  22. #include <linux/errno.h>
  23. #include <linux/slab.h>
  24. #include <linux/timer.h>
  25. #include <linux/mm.h>
  26. #include <linux/highmem.h>
  27. #include <linux/hrtimer.h>
  28. #include <linux/backing-dev.h>
  29. #include <linux/bug.h>
  30. #include <linux/module.h>
  31. #include <trace/events/jbd2.h>
  32. static void __jbd2_journal_temp_unlink_buffer(struct journal_head *jh);
  33. static void __jbd2_journal_unfile_buffer(struct journal_head *jh);
  34. static struct kmem_cache *transaction_cache;
  35. int __init jbd2_journal_init_transaction_cache(void)
  36. {
  37. J_ASSERT(!transaction_cache);
  38. transaction_cache = kmem_cache_create("jbd2_transaction_s",
  39. sizeof(transaction_t),
  40. 0,
  41. SLAB_HWCACHE_ALIGN|SLAB_TEMPORARY,
  42. NULL);
  43. if (transaction_cache)
  44. return 0;
  45. return -ENOMEM;
  46. }
  47. void jbd2_journal_destroy_transaction_cache(void)
  48. {
  49. if (transaction_cache) {
  50. kmem_cache_destroy(transaction_cache);
  51. transaction_cache = NULL;
  52. }
  53. }
  54. void jbd2_journal_free_transaction(transaction_t *transaction)
  55. {
  56. if (unlikely(ZERO_OR_NULL_PTR(transaction)))
  57. return;
  58. kmem_cache_free(transaction_cache, transaction);
  59. }
  60. /*
  61. * jbd2_get_transaction: obtain a new transaction_t object.
  62. *
  63. * Simply allocate and initialise a new transaction. Create it in
  64. * RUNNING state and add it to the current journal (which should not
  65. * have an existing running transaction: we only make a new transaction
  66. * once we have started to commit the old one).
  67. *
  68. * Preconditions:
  69. * The journal MUST be locked. We don't perform atomic mallocs on the
  70. * new transaction and we can't block without protecting against other
  71. * processes trying to touch the journal while it is in transition.
  72. *
  73. */
  74. static transaction_t *
  75. jbd2_get_transaction(journal_t *journal, transaction_t *transaction)
  76. {
  77. transaction->t_journal = journal;
  78. transaction->t_state = T_RUNNING;
  79. transaction->t_start_time = ktime_get();
  80. transaction->t_tid = journal->j_transaction_sequence++;
  81. transaction->t_expires = jiffies + journal->j_commit_interval;
  82. spin_lock_init(&transaction->t_handle_lock);
  83. atomic_set(&transaction->t_updates, 0);
  84. atomic_set(&transaction->t_outstanding_credits,
  85. atomic_read(&journal->j_reserved_credits));
  86. atomic_set(&transaction->t_handle_count, 0);
  87. INIT_LIST_HEAD(&transaction->t_inode_list);
  88. INIT_LIST_HEAD(&transaction->t_private_list);
  89. /* Set up the commit timer for the new transaction. */
  90. journal->j_commit_timer.expires = round_jiffies_up(transaction->t_expires);
  91. add_timer(&journal->j_commit_timer);
  92. J_ASSERT(journal->j_running_transaction == NULL);
  93. journal->j_running_transaction = transaction;
  94. transaction->t_max_wait = 0;
  95. transaction->t_start = jiffies;
  96. transaction->t_requested = 0;
  97. return transaction;
  98. }
  99. /*
  100. * Handle management.
  101. *
  102. * A handle_t is an object which represents a single atomic update to a
  103. * filesystem, and which tracks all of the modifications which form part
  104. * of that one update.
  105. */
  106. /*
  107. * Update transaction's maximum wait time, if debugging is enabled.
  108. *
  109. * In order for t_max_wait to be reliable, it must be protected by a
  110. * lock. But doing so will mean that start_this_handle() can not be
  111. * run in parallel on SMP systems, which limits our scalability. So
  112. * unless debugging is enabled, we no longer update t_max_wait, which
  113. * means that maximum wait time reported by the jbd2_run_stats
  114. * tracepoint will always be zero.
  115. */
  116. static inline void update_t_max_wait(transaction_t *transaction,
  117. unsigned long ts)
  118. {
  119. #ifdef CONFIG_JBD2_DEBUG
  120. if (jbd2_journal_enable_debug &&
  121. time_after(transaction->t_start, ts)) {
  122. ts = jbd2_time_diff(ts, transaction->t_start);
  123. spin_lock(&transaction->t_handle_lock);
  124. if (ts > transaction->t_max_wait)
  125. transaction->t_max_wait = ts;
  126. spin_unlock(&transaction->t_handle_lock);
  127. }
  128. #endif
  129. }
  130. /*
  131. * Wait until running transaction passes T_LOCKED state. Also starts the commit
  132. * if needed. The function expects running transaction to exist and releases
  133. * j_state_lock.
  134. */
  135. static void wait_transaction_locked(journal_t *journal)
  136. __releases(journal->j_state_lock)
  137. {
  138. DEFINE_WAIT(wait);
  139. int need_to_start;
  140. tid_t tid = journal->j_running_transaction->t_tid;
  141. prepare_to_wait(&journal->j_wait_transaction_locked, &wait,
  142. TASK_UNINTERRUPTIBLE);
  143. need_to_start = !tid_geq(journal->j_commit_request, tid);
  144. read_unlock(&journal->j_state_lock);
  145. if (need_to_start)
  146. jbd2_log_start_commit(journal, tid);
  147. jbd2_might_wait_for_commit(journal);
  148. schedule();
  149. finish_wait(&journal->j_wait_transaction_locked, &wait);
  150. }
  151. static void sub_reserved_credits(journal_t *journal, int blocks)
  152. {
  153. atomic_sub(blocks, &journal->j_reserved_credits);
  154. wake_up(&journal->j_wait_reserved);
  155. }
  156. /*
  157. * Wait until we can add credits for handle to the running transaction. Called
  158. * with j_state_lock held for reading. Returns 0 if handle joined the running
  159. * transaction. Returns 1 if we had to wait, j_state_lock is dropped, and
  160. * caller must retry.
  161. */
  162. static int add_transaction_credits(journal_t *journal, int blocks,
  163. int rsv_blocks)
  164. {
  165. transaction_t *t = journal->j_running_transaction;
  166. int needed;
  167. int total = blocks + rsv_blocks;
  168. /*
  169. * If the current transaction is locked down for commit, wait
  170. * for the lock to be released.
  171. */
  172. if (t->t_state == T_LOCKED) {
  173. wait_transaction_locked(journal);
  174. return 1;
  175. }
  176. /*
  177. * If there is not enough space left in the log to write all
  178. * potential buffers requested by this operation, we need to
  179. * stall pending a log checkpoint to free some more log space.
  180. */
  181. needed = atomic_add_return(total, &t->t_outstanding_credits);
  182. if (needed > journal->j_max_transaction_buffers) {
  183. /*
  184. * If the current transaction is already too large,
  185. * then start to commit it: we can then go back and
  186. * attach this handle to a new transaction.
  187. */
  188. atomic_sub(total, &t->t_outstanding_credits);
  189. /*
  190. * Is the number of reserved credits in the current transaction too
  191. * big to fit this handle? Wait until reserved credits are freed.
  192. */
  193. if (atomic_read(&journal->j_reserved_credits) + total >
  194. journal->j_max_transaction_buffers) {
  195. read_unlock(&journal->j_state_lock);
  196. jbd2_might_wait_for_commit(journal);
  197. wait_event(journal->j_wait_reserved,
  198. atomic_read(&journal->j_reserved_credits) + total <=
  199. journal->j_max_transaction_buffers);
  200. return 1;
  201. }
  202. wait_transaction_locked(journal);
  203. return 1;
  204. }
  205. /*
  206. * The commit code assumes that it can get enough log space
  207. * without forcing a checkpoint. This is *critical* for
  208. * correctness: a checkpoint of a buffer which is also
  209. * associated with a committing transaction creates a deadlock,
  210. * so commit simply cannot force through checkpoints.
  211. *
  212. * We must therefore ensure the necessary space in the journal
  213. * *before* starting to dirty potentially checkpointed buffers
  214. * in the new transaction.
  215. */
  216. if (jbd2_log_space_left(journal) < jbd2_space_needed(journal)) {
  217. atomic_sub(total, &t->t_outstanding_credits);
  218. read_unlock(&journal->j_state_lock);
  219. jbd2_might_wait_for_commit(journal);
  220. write_lock(&journal->j_state_lock);
  221. if (jbd2_log_space_left(journal) < jbd2_space_needed(journal))
  222. __jbd2_log_wait_for_space(journal);
  223. write_unlock(&journal->j_state_lock);
  224. return 1;
  225. }
  226. /* No reservation? We are done... */
  227. if (!rsv_blocks)
  228. return 0;
  229. needed = atomic_add_return(rsv_blocks, &journal->j_reserved_credits);
  230. /* We allow at most half of a transaction to be reserved */
  231. if (needed > journal->j_max_transaction_buffers / 2) {
  232. sub_reserved_credits(journal, rsv_blocks);
  233. atomic_sub(total, &t->t_outstanding_credits);
  234. read_unlock(&journal->j_state_lock);
  235. jbd2_might_wait_for_commit(journal);
  236. wait_event(journal->j_wait_reserved,
  237. atomic_read(&journal->j_reserved_credits) + rsv_blocks
  238. <= journal->j_max_transaction_buffers / 2);
  239. return 1;
  240. }
  241. return 0;
  242. }
  243. /*
  244. * start_this_handle: Given a handle, deal with any locking or stalling
  245. * needed to make sure that there is enough journal space for the handle
  246. * to begin. Attach the handle to a transaction and set up the
  247. * transaction's buffer credits.
  248. */
  249. static int start_this_handle(journal_t *journal, handle_t *handle,
  250. gfp_t gfp_mask)
  251. {
  252. transaction_t *transaction, *new_transaction = NULL;
  253. int blocks = handle->h_buffer_credits;
  254. int rsv_blocks = 0;
  255. unsigned long ts = jiffies;
  256. if (handle->h_rsv_handle)
  257. rsv_blocks = handle->h_rsv_handle->h_buffer_credits;
  258. /*
  259. * Limit the number of reserved credits to 1/2 of maximum transaction
  260. * size and limit the number of total credits to not exceed maximum
  261. * transaction size per operation.
  262. */
  263. if ((rsv_blocks > journal->j_max_transaction_buffers / 2) ||
  264. (rsv_blocks + blocks > journal->j_max_transaction_buffers)) {
  265. printk(KERN_ERR "JBD2: %s wants too many credits "
  266. "credits:%d rsv_credits:%d max:%d\n",
  267. current->comm, blocks, rsv_blocks,
  268. journal->j_max_transaction_buffers);
  269. WARN_ON(1);
  270. return -ENOSPC;
  271. }
  272. alloc_transaction:
  273. if (!journal->j_running_transaction) {
  274. /*
  275. * If __GFP_FS is not present, then we may be being called from
  276. * inside the fs writeback layer, so we MUST NOT fail.
  277. */
  278. if ((gfp_mask & __GFP_FS) == 0)
  279. gfp_mask |= __GFP_NOFAIL;
  280. new_transaction = kmem_cache_zalloc(transaction_cache,
  281. gfp_mask);
  282. if (!new_transaction)
  283. return -ENOMEM;
  284. }
  285. jbd_debug(3, "New handle %p going live.\n", handle);
  286. /*
  287. * We need to hold j_state_lock until t_updates has been incremented,
  288. * for proper journal barrier handling
  289. */
  290. repeat:
  291. read_lock(&journal->j_state_lock);
  292. BUG_ON(journal->j_flags & JBD2_UNMOUNT);
  293. if (is_journal_aborted(journal) ||
  294. (journal->j_errno != 0 && !(journal->j_flags & JBD2_ACK_ERR))) {
  295. read_unlock(&journal->j_state_lock);
  296. jbd2_journal_free_transaction(new_transaction);
  297. return -EROFS;
  298. }
  299. /*
  300. * Wait on the journal's transaction barrier if necessary. Specifically
  301. * we allow reserved handles to proceed because otherwise commit could
  302. * deadlock on page writeback not being able to complete.
  303. */
  304. if (!handle->h_reserved && journal->j_barrier_count) {
  305. read_unlock(&journal->j_state_lock);
  306. wait_event(journal->j_wait_transaction_locked,
  307. journal->j_barrier_count == 0);
  308. goto repeat;
  309. }
  310. if (!journal->j_running_transaction) {
  311. read_unlock(&journal->j_state_lock);
  312. if (!new_transaction)
  313. goto alloc_transaction;
  314. write_lock(&journal->j_state_lock);
  315. if (!journal->j_running_transaction &&
  316. (handle->h_reserved || !journal->j_barrier_count)) {
  317. jbd2_get_transaction(journal, new_transaction);
  318. new_transaction = NULL;
  319. }
  320. write_unlock(&journal->j_state_lock);
  321. goto repeat;
  322. }
  323. transaction = journal->j_running_transaction;
  324. if (!handle->h_reserved) {
  325. /* We may have dropped j_state_lock - restart in that case */
  326. if (add_transaction_credits(journal, blocks, rsv_blocks))
  327. goto repeat;
  328. } else {
  329. /*
  330. * We have handle reserved so we are allowed to join T_LOCKED
  331. * transaction and we don't have to check for transaction size
  332. * and journal space.
  333. */
  334. sub_reserved_credits(journal, blocks);
  335. handle->h_reserved = 0;
  336. }
  337. /* OK, account for the buffers that this operation expects to
  338. * use and add the handle to the running transaction.
  339. */
  340. update_t_max_wait(transaction, ts);
  341. handle->h_transaction = transaction;
  342. handle->h_requested_credits = blocks;
  343. handle->h_start_jiffies = jiffies;
  344. atomic_inc(&transaction->t_updates);
  345. atomic_inc(&transaction->t_handle_count);
  346. jbd_debug(4, "Handle %p given %d credits (total %d, free %lu)\n",
  347. handle, blocks,
  348. atomic_read(&transaction->t_outstanding_credits),
  349. jbd2_log_space_left(journal));
  350. read_unlock(&journal->j_state_lock);
  351. current->journal_info = handle;
  352. rwsem_acquire_read(&journal->j_trans_commit_map, 0, 0, _THIS_IP_);
  353. jbd2_journal_free_transaction(new_transaction);
  354. return 0;
  355. }
  356. /* Allocate a new handle. This should probably be in a slab... */
  357. static handle_t *new_handle(int nblocks)
  358. {
  359. handle_t *handle = jbd2_alloc_handle(GFP_NOFS);
  360. if (!handle)
  361. return NULL;
  362. handle->h_buffer_credits = nblocks;
  363. handle->h_ref = 1;
  364. return handle;
  365. }
  366. /**
  367. * handle_t *jbd2_journal_start() - Obtain a new handle.
  368. * @journal: Journal to start transaction on.
  369. * @nblocks: number of block buffer we might modify
  370. *
  371. * We make sure that the transaction can guarantee at least nblocks of
  372. * modified buffers in the log. We block until the log can guarantee
  373. * that much space. Additionally, if rsv_blocks > 0, we also create another
  374. * handle with rsv_blocks reserved blocks in the journal. This handle is
  375. * is stored in h_rsv_handle. It is not attached to any particular transaction
  376. * and thus doesn't block transaction commit. If the caller uses this reserved
  377. * handle, it has to set h_rsv_handle to NULL as otherwise jbd2_journal_stop()
  378. * on the parent handle will dispose the reserved one. Reserved handle has to
  379. * be converted to a normal handle using jbd2_journal_start_reserved() before
  380. * it can be used.
  381. *
  382. * Return a pointer to a newly allocated handle, or an ERR_PTR() value
  383. * on failure.
  384. */
  385. handle_t *jbd2__journal_start(journal_t *journal, int nblocks, int rsv_blocks,
  386. gfp_t gfp_mask, unsigned int type,
  387. unsigned int line_no)
  388. {
  389. handle_t *handle = journal_current_handle();
  390. int err;
  391. if (!journal)
  392. return ERR_PTR(-EROFS);
  393. if (handle) {
  394. J_ASSERT(handle->h_transaction->t_journal == journal);
  395. handle->h_ref++;
  396. return handle;
  397. }
  398. handle = new_handle(nblocks);
  399. if (!handle)
  400. return ERR_PTR(-ENOMEM);
  401. if (rsv_blocks) {
  402. handle_t *rsv_handle;
  403. rsv_handle = new_handle(rsv_blocks);
  404. if (!rsv_handle) {
  405. jbd2_free_handle(handle);
  406. return ERR_PTR(-ENOMEM);
  407. }
  408. rsv_handle->h_reserved = 1;
  409. rsv_handle->h_journal = journal;
  410. handle->h_rsv_handle = rsv_handle;
  411. }
  412. err = start_this_handle(journal, handle, gfp_mask);
  413. if (err < 0) {
  414. if (handle->h_rsv_handle)
  415. jbd2_free_handle(handle->h_rsv_handle);
  416. jbd2_free_handle(handle);
  417. return ERR_PTR(err);
  418. }
  419. handle->h_type = type;
  420. handle->h_line_no = line_no;
  421. trace_jbd2_handle_start(journal->j_fs_dev->bd_dev,
  422. handle->h_transaction->t_tid, type,
  423. line_no, nblocks);
  424. return handle;
  425. }
  426. EXPORT_SYMBOL(jbd2__journal_start);
  427. handle_t *jbd2_journal_start(journal_t *journal, int nblocks)
  428. {
  429. return jbd2__journal_start(journal, nblocks, 0, GFP_NOFS, 0, 0);
  430. }
  431. EXPORT_SYMBOL(jbd2_journal_start);
  432. void jbd2_journal_free_reserved(handle_t *handle)
  433. {
  434. journal_t *journal = handle->h_journal;
  435. WARN_ON(!handle->h_reserved);
  436. sub_reserved_credits(journal, handle->h_buffer_credits);
  437. jbd2_free_handle(handle);
  438. }
  439. EXPORT_SYMBOL(jbd2_journal_free_reserved);
  440. /**
  441. * int jbd2_journal_start_reserved(handle_t *handle) - start reserved handle
  442. * @handle: handle to start
  443. *
  444. * Start handle that has been previously reserved with jbd2_journal_reserve().
  445. * This attaches @handle to the running transaction (or creates one if there's
  446. * not transaction running). Unlike jbd2_journal_start() this function cannot
  447. * block on journal commit, checkpointing, or similar stuff. It can block on
  448. * memory allocation or frozen journal though.
  449. *
  450. * Return 0 on success, non-zero on error - handle is freed in that case.
  451. */
  452. int jbd2_journal_start_reserved(handle_t *handle, unsigned int type,
  453. unsigned int line_no)
  454. {
  455. journal_t *journal = handle->h_journal;
  456. int ret = -EIO;
  457. if (WARN_ON(!handle->h_reserved)) {
  458. /* Someone passed in normal handle? Just stop it. */
  459. jbd2_journal_stop(handle);
  460. return ret;
  461. }
  462. /*
  463. * Usefulness of mixing of reserved and unreserved handles is
  464. * questionable. So far nobody seems to need it so just error out.
  465. */
  466. if (WARN_ON(current->journal_info)) {
  467. jbd2_journal_free_reserved(handle);
  468. return ret;
  469. }
  470. handle->h_journal = NULL;
  471. /*
  472. * GFP_NOFS is here because callers are likely from writeback or
  473. * similarly constrained call sites
  474. */
  475. ret = start_this_handle(journal, handle, GFP_NOFS);
  476. if (ret < 0) {
  477. jbd2_journal_free_reserved(handle);
  478. return ret;
  479. }
  480. handle->h_type = type;
  481. handle->h_line_no = line_no;
  482. return 0;
  483. }
  484. EXPORT_SYMBOL(jbd2_journal_start_reserved);
  485. /**
  486. * int jbd2_journal_extend() - extend buffer credits.
  487. * @handle: handle to 'extend'
  488. * @nblocks: nr blocks to try to extend by.
  489. *
  490. * Some transactions, such as large extends and truncates, can be done
  491. * atomically all at once or in several stages. The operation requests
  492. * a credit for a number of buffer modifications in advance, but can
  493. * extend its credit if it needs more.
  494. *
  495. * jbd2_journal_extend tries to give the running handle more buffer credits.
  496. * It does not guarantee that allocation - this is a best-effort only.
  497. * The calling process MUST be able to deal cleanly with a failure to
  498. * extend here.
  499. *
  500. * Return 0 on success, non-zero on failure.
  501. *
  502. * return code < 0 implies an error
  503. * return code > 0 implies normal transaction-full status.
  504. */
  505. int jbd2_journal_extend(handle_t *handle, int nblocks)
  506. {
  507. transaction_t *transaction = handle->h_transaction;
  508. journal_t *journal;
  509. int result;
  510. int wanted;
  511. if (is_handle_aborted(handle))
  512. return -EROFS;
  513. journal = transaction->t_journal;
  514. result = 1;
  515. read_lock(&journal->j_state_lock);
  516. /* Don't extend a locked-down transaction! */
  517. if (transaction->t_state != T_RUNNING) {
  518. jbd_debug(3, "denied handle %p %d blocks: "
  519. "transaction not running\n", handle, nblocks);
  520. goto error_out;
  521. }
  522. spin_lock(&transaction->t_handle_lock);
  523. wanted = atomic_add_return(nblocks,
  524. &transaction->t_outstanding_credits);
  525. if (wanted > journal->j_max_transaction_buffers) {
  526. jbd_debug(3, "denied handle %p %d blocks: "
  527. "transaction too large\n", handle, nblocks);
  528. atomic_sub(nblocks, &transaction->t_outstanding_credits);
  529. goto unlock;
  530. }
  531. if (wanted + (wanted >> JBD2_CONTROL_BLOCKS_SHIFT) >
  532. jbd2_log_space_left(journal)) {
  533. jbd_debug(3, "denied handle %p %d blocks: "
  534. "insufficient log space\n", handle, nblocks);
  535. atomic_sub(nblocks, &transaction->t_outstanding_credits);
  536. goto unlock;
  537. }
  538. trace_jbd2_handle_extend(journal->j_fs_dev->bd_dev,
  539. transaction->t_tid,
  540. handle->h_type, handle->h_line_no,
  541. handle->h_buffer_credits,
  542. nblocks);
  543. handle->h_buffer_credits += nblocks;
  544. handle->h_requested_credits += nblocks;
  545. result = 0;
  546. jbd_debug(3, "extended handle %p by %d\n", handle, nblocks);
  547. unlock:
  548. spin_unlock(&transaction->t_handle_lock);
  549. error_out:
  550. read_unlock(&journal->j_state_lock);
  551. return result;
  552. }
  553. /**
  554. * int jbd2_journal_restart() - restart a handle .
  555. * @handle: handle to restart
  556. * @nblocks: nr credits requested
  557. *
  558. * Restart a handle for a multi-transaction filesystem
  559. * operation.
  560. *
  561. * If the jbd2_journal_extend() call above fails to grant new buffer credits
  562. * to a running handle, a call to jbd2_journal_restart will commit the
  563. * handle's transaction so far and reattach the handle to a new
  564. * transaction capable of guaranteeing the requested number of
  565. * credits. We preserve reserved handle if there's any attached to the
  566. * passed in handle.
  567. */
  568. int jbd2__journal_restart(handle_t *handle, int nblocks, gfp_t gfp_mask)
  569. {
  570. transaction_t *transaction = handle->h_transaction;
  571. journal_t *journal;
  572. tid_t tid;
  573. int need_to_start, ret;
  574. /* If we've had an abort of any type, don't even think about
  575. * actually doing the restart! */
  576. if (is_handle_aborted(handle))
  577. return 0;
  578. journal = transaction->t_journal;
  579. /*
  580. * First unlink the handle from its current transaction, and start the
  581. * commit on that.
  582. */
  583. J_ASSERT(atomic_read(&transaction->t_updates) > 0);
  584. J_ASSERT(journal_current_handle() == handle);
  585. read_lock(&journal->j_state_lock);
  586. spin_lock(&transaction->t_handle_lock);
  587. atomic_sub(handle->h_buffer_credits,
  588. &transaction->t_outstanding_credits);
  589. if (handle->h_rsv_handle) {
  590. sub_reserved_credits(journal,
  591. handle->h_rsv_handle->h_buffer_credits);
  592. }
  593. if (atomic_dec_and_test(&transaction->t_updates))
  594. wake_up(&journal->j_wait_updates);
  595. tid = transaction->t_tid;
  596. spin_unlock(&transaction->t_handle_lock);
  597. handle->h_transaction = NULL;
  598. current->journal_info = NULL;
  599. jbd_debug(2, "restarting handle %p\n", handle);
  600. need_to_start = !tid_geq(journal->j_commit_request, tid);
  601. read_unlock(&journal->j_state_lock);
  602. if (need_to_start)
  603. jbd2_log_start_commit(journal, tid);
  604. rwsem_release(&journal->j_trans_commit_map, 1, _THIS_IP_);
  605. handle->h_buffer_credits = nblocks;
  606. ret = start_this_handle(journal, handle, gfp_mask);
  607. return ret;
  608. }
  609. EXPORT_SYMBOL(jbd2__journal_restart);
  610. int jbd2_journal_restart(handle_t *handle, int nblocks)
  611. {
  612. return jbd2__journal_restart(handle, nblocks, GFP_NOFS);
  613. }
  614. EXPORT_SYMBOL(jbd2_journal_restart);
  615. /**
  616. * void jbd2_journal_lock_updates () - establish a transaction barrier.
  617. * @journal: Journal to establish a barrier on.
  618. *
  619. * This locks out any further updates from being started, and blocks
  620. * until all existing updates have completed, returning only once the
  621. * journal is in a quiescent state with no updates running.
  622. *
  623. * The journal lock should not be held on entry.
  624. */
  625. void jbd2_journal_lock_updates(journal_t *journal)
  626. {
  627. DEFINE_WAIT(wait);
  628. jbd2_might_wait_for_commit(journal);
  629. write_lock(&journal->j_state_lock);
  630. ++journal->j_barrier_count;
  631. /* Wait until there are no reserved handles */
  632. if (atomic_read(&journal->j_reserved_credits)) {
  633. write_unlock(&journal->j_state_lock);
  634. wait_event(journal->j_wait_reserved,
  635. atomic_read(&journal->j_reserved_credits) == 0);
  636. write_lock(&journal->j_state_lock);
  637. }
  638. /* Wait until there are no running updates */
  639. while (1) {
  640. transaction_t *transaction = journal->j_running_transaction;
  641. if (!transaction)
  642. break;
  643. spin_lock(&transaction->t_handle_lock);
  644. prepare_to_wait(&journal->j_wait_updates, &wait,
  645. TASK_UNINTERRUPTIBLE);
  646. if (!atomic_read(&transaction->t_updates)) {
  647. spin_unlock(&transaction->t_handle_lock);
  648. finish_wait(&journal->j_wait_updates, &wait);
  649. break;
  650. }
  651. spin_unlock(&transaction->t_handle_lock);
  652. write_unlock(&journal->j_state_lock);
  653. schedule();
  654. finish_wait(&journal->j_wait_updates, &wait);
  655. write_lock(&journal->j_state_lock);
  656. }
  657. write_unlock(&journal->j_state_lock);
  658. /*
  659. * We have now established a barrier against other normal updates, but
  660. * we also need to barrier against other jbd2_journal_lock_updates() calls
  661. * to make sure that we serialise special journal-locked operations
  662. * too.
  663. */
  664. mutex_lock(&journal->j_barrier);
  665. }
  666. /**
  667. * void jbd2_journal_unlock_updates (journal_t* journal) - release barrier
  668. * @journal: Journal to release the barrier on.
  669. *
  670. * Release a transaction barrier obtained with jbd2_journal_lock_updates().
  671. *
  672. * Should be called without the journal lock held.
  673. */
  674. void jbd2_journal_unlock_updates (journal_t *journal)
  675. {
  676. J_ASSERT(journal->j_barrier_count != 0);
  677. mutex_unlock(&journal->j_barrier);
  678. write_lock(&journal->j_state_lock);
  679. --journal->j_barrier_count;
  680. write_unlock(&journal->j_state_lock);
  681. wake_up(&journal->j_wait_transaction_locked);
  682. }
  683. static void warn_dirty_buffer(struct buffer_head *bh)
  684. {
  685. printk(KERN_WARNING
  686. "JBD2: Spotted dirty metadata buffer (dev = %pg, blocknr = %llu). "
  687. "There's a risk of filesystem corruption in case of system "
  688. "crash.\n",
  689. bh->b_bdev, (unsigned long long)bh->b_blocknr);
  690. }
  691. /* Call t_frozen trigger and copy buffer data into jh->b_frozen_data. */
  692. static void jbd2_freeze_jh_data(struct journal_head *jh)
  693. {
  694. struct page *page;
  695. int offset;
  696. char *source;
  697. struct buffer_head *bh = jh2bh(jh);
  698. J_EXPECT_JH(jh, buffer_uptodate(bh), "Possible IO failure.\n");
  699. page = bh->b_page;
  700. offset = offset_in_page(bh->b_data);
  701. source = kmap_atomic(page);
  702. /* Fire data frozen trigger just before we copy the data */
  703. jbd2_buffer_frozen_trigger(jh, source + offset, jh->b_triggers);
  704. memcpy(jh->b_frozen_data, source + offset, bh->b_size);
  705. kunmap_atomic(source);
  706. /*
  707. * Now that the frozen data is saved off, we need to store any matching
  708. * triggers.
  709. */
  710. jh->b_frozen_triggers = jh->b_triggers;
  711. }
  712. /*
  713. * If the buffer is already part of the current transaction, then there
  714. * is nothing we need to do. If it is already part of a prior
  715. * transaction which we are still committing to disk, then we need to
  716. * make sure that we do not overwrite the old copy: we do copy-out to
  717. * preserve the copy going to disk. We also account the buffer against
  718. * the handle's metadata buffer credits (unless the buffer is already
  719. * part of the transaction, that is).
  720. *
  721. */
  722. static int
  723. do_get_write_access(handle_t *handle, struct journal_head *jh,
  724. int force_copy)
  725. {
  726. struct buffer_head *bh;
  727. transaction_t *transaction = handle->h_transaction;
  728. journal_t *journal;
  729. int error;
  730. char *frozen_buffer = NULL;
  731. unsigned long start_lock, time_lock;
  732. if (is_handle_aborted(handle))
  733. return -EROFS;
  734. journal = transaction->t_journal;
  735. jbd_debug(5, "journal_head %p, force_copy %d\n", jh, force_copy);
  736. JBUFFER_TRACE(jh, "entry");
  737. repeat:
  738. bh = jh2bh(jh);
  739. /* @@@ Need to check for errors here at some point. */
  740. start_lock = jiffies;
  741. lock_buffer(bh);
  742. jbd_lock_bh_state(bh);
  743. /* If it takes too long to lock the buffer, trace it */
  744. time_lock = jbd2_time_diff(start_lock, jiffies);
  745. if (time_lock > HZ/10)
  746. trace_jbd2_lock_buffer_stall(bh->b_bdev->bd_dev,
  747. jiffies_to_msecs(time_lock));
  748. /* We now hold the buffer lock so it is safe to query the buffer
  749. * state. Is the buffer dirty?
  750. *
  751. * If so, there are two possibilities. The buffer may be
  752. * non-journaled, and undergoing a quite legitimate writeback.
  753. * Otherwise, it is journaled, and we don't expect dirty buffers
  754. * in that state (the buffers should be marked JBD_Dirty
  755. * instead.) So either the IO is being done under our own
  756. * control and this is a bug, or it's a third party IO such as
  757. * dump(8) (which may leave the buffer scheduled for read ---
  758. * ie. locked but not dirty) or tune2fs (which may actually have
  759. * the buffer dirtied, ugh.) */
  760. if (buffer_dirty(bh)) {
  761. /*
  762. * First question: is this buffer already part of the current
  763. * transaction or the existing committing transaction?
  764. */
  765. if (jh->b_transaction) {
  766. J_ASSERT_JH(jh,
  767. jh->b_transaction == transaction ||
  768. jh->b_transaction ==
  769. journal->j_committing_transaction);
  770. if (jh->b_next_transaction)
  771. J_ASSERT_JH(jh, jh->b_next_transaction ==
  772. transaction);
  773. warn_dirty_buffer(bh);
  774. }
  775. /*
  776. * In any case we need to clean the dirty flag and we must
  777. * do it under the buffer lock to be sure we don't race
  778. * with running write-out.
  779. */
  780. JBUFFER_TRACE(jh, "Journalling dirty buffer");
  781. clear_buffer_dirty(bh);
  782. set_buffer_jbddirty(bh);
  783. }
  784. unlock_buffer(bh);
  785. error = -EROFS;
  786. if (is_handle_aborted(handle)) {
  787. jbd_unlock_bh_state(bh);
  788. goto out;
  789. }
  790. error = 0;
  791. /*
  792. * The buffer is already part of this transaction if b_transaction or
  793. * b_next_transaction points to it
  794. */
  795. if (jh->b_transaction == transaction ||
  796. jh->b_next_transaction == transaction)
  797. goto done;
  798. /*
  799. * this is the first time this transaction is touching this buffer,
  800. * reset the modified flag
  801. */
  802. jh->b_modified = 0;
  803. /*
  804. * If the buffer is not journaled right now, we need to make sure it
  805. * doesn't get written to disk before the caller actually commits the
  806. * new data
  807. */
  808. if (!jh->b_transaction) {
  809. JBUFFER_TRACE(jh, "no transaction");
  810. J_ASSERT_JH(jh, !jh->b_next_transaction);
  811. JBUFFER_TRACE(jh, "file as BJ_Reserved");
  812. /*
  813. * Make sure all stores to jh (b_modified, b_frozen_data) are
  814. * visible before attaching it to the running transaction.
  815. * Paired with barrier in jbd2_write_access_granted()
  816. */
  817. smp_wmb();
  818. spin_lock(&journal->j_list_lock);
  819. __jbd2_journal_file_buffer(jh, transaction, BJ_Reserved);
  820. spin_unlock(&journal->j_list_lock);
  821. goto done;
  822. }
  823. /*
  824. * If there is already a copy-out version of this buffer, then we don't
  825. * need to make another one
  826. */
  827. if (jh->b_frozen_data) {
  828. JBUFFER_TRACE(jh, "has frozen data");
  829. J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
  830. goto attach_next;
  831. }
  832. JBUFFER_TRACE(jh, "owned by older transaction");
  833. J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
  834. J_ASSERT_JH(jh, jh->b_transaction == journal->j_committing_transaction);
  835. /*
  836. * There is one case we have to be very careful about. If the
  837. * committing transaction is currently writing this buffer out to disk
  838. * and has NOT made a copy-out, then we cannot modify the buffer
  839. * contents at all right now. The essence of copy-out is that it is
  840. * the extra copy, not the primary copy, which gets journaled. If the
  841. * primary copy is already going to disk then we cannot do copy-out
  842. * here.
  843. */
  844. if (buffer_shadow(bh)) {
  845. JBUFFER_TRACE(jh, "on shadow: sleep");
  846. jbd_unlock_bh_state(bh);
  847. wait_on_bit_io(&bh->b_state, BH_Shadow, TASK_UNINTERRUPTIBLE);
  848. goto repeat;
  849. }
  850. /*
  851. * Only do the copy if the currently-owning transaction still needs it.
  852. * If buffer isn't on BJ_Metadata list, the committing transaction is
  853. * past that stage (here we use the fact that BH_Shadow is set under
  854. * bh_state lock together with refiling to BJ_Shadow list and at this
  855. * point we know the buffer doesn't have BH_Shadow set).
  856. *
  857. * Subtle point, though: if this is a get_undo_access, then we will be
  858. * relying on the frozen_data to contain the new value of the
  859. * committed_data record after the transaction, so we HAVE to force the
  860. * frozen_data copy in that case.
  861. */
  862. if (jh->b_jlist == BJ_Metadata || force_copy) {
  863. JBUFFER_TRACE(jh, "generate frozen data");
  864. if (!frozen_buffer) {
  865. JBUFFER_TRACE(jh, "allocate memory for buffer");
  866. jbd_unlock_bh_state(bh);
  867. frozen_buffer = jbd2_alloc(jh2bh(jh)->b_size,
  868. GFP_NOFS | __GFP_NOFAIL);
  869. goto repeat;
  870. }
  871. jh->b_frozen_data = frozen_buffer;
  872. frozen_buffer = NULL;
  873. jbd2_freeze_jh_data(jh);
  874. }
  875. attach_next:
  876. /*
  877. * Make sure all stores to jh (b_modified, b_frozen_data) are visible
  878. * before attaching it to the running transaction. Paired with barrier
  879. * in jbd2_write_access_granted()
  880. */
  881. smp_wmb();
  882. jh->b_next_transaction = transaction;
  883. done:
  884. jbd_unlock_bh_state(bh);
  885. /*
  886. * If we are about to journal a buffer, then any revoke pending on it is
  887. * no longer valid
  888. */
  889. jbd2_journal_cancel_revoke(handle, jh);
  890. out:
  891. if (unlikely(frozen_buffer)) /* It's usually NULL */
  892. jbd2_free(frozen_buffer, bh->b_size);
  893. JBUFFER_TRACE(jh, "exit");
  894. return error;
  895. }
  896. /* Fast check whether buffer is already attached to the required transaction */
  897. static bool jbd2_write_access_granted(handle_t *handle, struct buffer_head *bh,
  898. bool undo)
  899. {
  900. struct journal_head *jh;
  901. bool ret = false;
  902. /* Dirty buffers require special handling... */
  903. if (buffer_dirty(bh))
  904. return false;
  905. /*
  906. * RCU protects us from dereferencing freed pages. So the checks we do
  907. * are guaranteed not to oops. However the jh slab object can get freed
  908. * & reallocated while we work with it. So we have to be careful. When
  909. * we see jh attached to the running transaction, we know it must stay
  910. * so until the transaction is committed. Thus jh won't be freed and
  911. * will be attached to the same bh while we run. However it can
  912. * happen jh gets freed, reallocated, and attached to the transaction
  913. * just after we get pointer to it from bh. So we have to be careful
  914. * and recheck jh still belongs to our bh before we return success.
  915. */
  916. rcu_read_lock();
  917. if (!buffer_jbd(bh))
  918. goto out;
  919. /* This should be bh2jh() but that doesn't work with inline functions */
  920. jh = READ_ONCE(bh->b_private);
  921. if (!jh)
  922. goto out;
  923. /* For undo access buffer must have data copied */
  924. if (undo && !jh->b_committed_data)
  925. goto out;
  926. if (jh->b_transaction != handle->h_transaction &&
  927. jh->b_next_transaction != handle->h_transaction)
  928. goto out;
  929. /*
  930. * There are two reasons for the barrier here:
  931. * 1) Make sure to fetch b_bh after we did previous checks so that we
  932. * detect when jh went through free, realloc, attach to transaction
  933. * while we were checking. Paired with implicit barrier in that path.
  934. * 2) So that access to bh done after jbd2_write_access_granted()
  935. * doesn't get reordered and see inconsistent state of concurrent
  936. * do_get_write_access().
  937. */
  938. smp_mb();
  939. if (unlikely(jh->b_bh != bh))
  940. goto out;
  941. ret = true;
  942. out:
  943. rcu_read_unlock();
  944. return ret;
  945. }
  946. /**
  947. * int jbd2_journal_get_write_access() - notify intent to modify a buffer for metadata (not data) update.
  948. * @handle: transaction to add buffer modifications to
  949. * @bh: bh to be used for metadata writes
  950. *
  951. * Returns an error code or 0 on success.
  952. *
  953. * In full data journalling mode the buffer may be of type BJ_AsyncData,
  954. * because we're write()ing a buffer which is also part of a shared mapping.
  955. */
  956. int jbd2_journal_get_write_access(handle_t *handle, struct buffer_head *bh)
  957. {
  958. struct journal_head *jh;
  959. int rc;
  960. if (jbd2_write_access_granted(handle, bh, false))
  961. return 0;
  962. jh = jbd2_journal_add_journal_head(bh);
  963. /* We do not want to get caught playing with fields which the
  964. * log thread also manipulates. Make sure that the buffer
  965. * completes any outstanding IO before proceeding. */
  966. rc = do_get_write_access(handle, jh, 0);
  967. jbd2_journal_put_journal_head(jh);
  968. return rc;
  969. }
  970. /*
  971. * When the user wants to journal a newly created buffer_head
  972. * (ie. getblk() returned a new buffer and we are going to populate it
  973. * manually rather than reading off disk), then we need to keep the
  974. * buffer_head locked until it has been completely filled with new
  975. * data. In this case, we should be able to make the assertion that
  976. * the bh is not already part of an existing transaction.
  977. *
  978. * The buffer should already be locked by the caller by this point.
  979. * There is no lock ranking violation: it was a newly created,
  980. * unlocked buffer beforehand. */
  981. /**
  982. * int jbd2_journal_get_create_access () - notify intent to use newly created bh
  983. * @handle: transaction to new buffer to
  984. * @bh: new buffer.
  985. *
  986. * Call this if you create a new bh.
  987. */
  988. int jbd2_journal_get_create_access(handle_t *handle, struct buffer_head *bh)
  989. {
  990. transaction_t *transaction = handle->h_transaction;
  991. journal_t *journal;
  992. struct journal_head *jh = jbd2_journal_add_journal_head(bh);
  993. int err;
  994. jbd_debug(5, "journal_head %p\n", jh);
  995. err = -EROFS;
  996. if (is_handle_aborted(handle))
  997. goto out;
  998. journal = transaction->t_journal;
  999. err = 0;
  1000. JBUFFER_TRACE(jh, "entry");
  1001. /*
  1002. * The buffer may already belong to this transaction due to pre-zeroing
  1003. * in the filesystem's new_block code. It may also be on the previous,
  1004. * committing transaction's lists, but it HAS to be in Forget state in
  1005. * that case: the transaction must have deleted the buffer for it to be
  1006. * reused here.
  1007. */
  1008. jbd_lock_bh_state(bh);
  1009. J_ASSERT_JH(jh, (jh->b_transaction == transaction ||
  1010. jh->b_transaction == NULL ||
  1011. (jh->b_transaction == journal->j_committing_transaction &&
  1012. jh->b_jlist == BJ_Forget)));
  1013. J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
  1014. J_ASSERT_JH(jh, buffer_locked(jh2bh(jh)));
  1015. if (jh->b_transaction == NULL) {
  1016. /*
  1017. * Previous jbd2_journal_forget() could have left the buffer
  1018. * with jbddirty bit set because it was being committed. When
  1019. * the commit finished, we've filed the buffer for
  1020. * checkpointing and marked it dirty. Now we are reallocating
  1021. * the buffer so the transaction freeing it must have
  1022. * committed and so it's safe to clear the dirty bit.
  1023. */
  1024. clear_buffer_dirty(jh2bh(jh));
  1025. /* first access by this transaction */
  1026. jh->b_modified = 0;
  1027. JBUFFER_TRACE(jh, "file as BJ_Reserved");
  1028. spin_lock(&journal->j_list_lock);
  1029. __jbd2_journal_file_buffer(jh, transaction, BJ_Reserved);
  1030. spin_unlock(&journal->j_list_lock);
  1031. } else if (jh->b_transaction == journal->j_committing_transaction) {
  1032. /* first access by this transaction */
  1033. jh->b_modified = 0;
  1034. JBUFFER_TRACE(jh, "set next transaction");
  1035. spin_lock(&journal->j_list_lock);
  1036. jh->b_next_transaction = transaction;
  1037. spin_unlock(&journal->j_list_lock);
  1038. }
  1039. jbd_unlock_bh_state(bh);
  1040. /*
  1041. * akpm: I added this. ext3_alloc_branch can pick up new indirect
  1042. * blocks which contain freed but then revoked metadata. We need
  1043. * to cancel the revoke in case we end up freeing it yet again
  1044. * and the reallocating as data - this would cause a second revoke,
  1045. * which hits an assertion error.
  1046. */
  1047. JBUFFER_TRACE(jh, "cancelling revoke");
  1048. jbd2_journal_cancel_revoke(handle, jh);
  1049. out:
  1050. jbd2_journal_put_journal_head(jh);
  1051. return err;
  1052. }
  1053. /**
  1054. * int jbd2_journal_get_undo_access() - Notify intent to modify metadata with
  1055. * non-rewindable consequences
  1056. * @handle: transaction
  1057. * @bh: buffer to undo
  1058. *
  1059. * Sometimes there is a need to distinguish between metadata which has
  1060. * been committed to disk and that which has not. The ext3fs code uses
  1061. * this for freeing and allocating space, we have to make sure that we
  1062. * do not reuse freed space until the deallocation has been committed,
  1063. * since if we overwrote that space we would make the delete
  1064. * un-rewindable in case of a crash.
  1065. *
  1066. * To deal with that, jbd2_journal_get_undo_access requests write access to a
  1067. * buffer for parts of non-rewindable operations such as delete
  1068. * operations on the bitmaps. The journaling code must keep a copy of
  1069. * the buffer's contents prior to the undo_access call until such time
  1070. * as we know that the buffer has definitely been committed to disk.
  1071. *
  1072. * We never need to know which transaction the committed data is part
  1073. * of, buffers touched here are guaranteed to be dirtied later and so
  1074. * will be committed to a new transaction in due course, at which point
  1075. * we can discard the old committed data pointer.
  1076. *
  1077. * Returns error number or 0 on success.
  1078. */
  1079. int jbd2_journal_get_undo_access(handle_t *handle, struct buffer_head *bh)
  1080. {
  1081. int err;
  1082. struct journal_head *jh;
  1083. char *committed_data = NULL;
  1084. JBUFFER_TRACE(jh, "entry");
  1085. if (jbd2_write_access_granted(handle, bh, true))
  1086. return 0;
  1087. jh = jbd2_journal_add_journal_head(bh);
  1088. /*
  1089. * Do this first --- it can drop the journal lock, so we want to
  1090. * make sure that obtaining the committed_data is done
  1091. * atomically wrt. completion of any outstanding commits.
  1092. */
  1093. err = do_get_write_access(handle, jh, 1);
  1094. if (err)
  1095. goto out;
  1096. repeat:
  1097. if (!jh->b_committed_data)
  1098. committed_data = jbd2_alloc(jh2bh(jh)->b_size,
  1099. GFP_NOFS|__GFP_NOFAIL);
  1100. jbd_lock_bh_state(bh);
  1101. if (!jh->b_committed_data) {
  1102. /* Copy out the current buffer contents into the
  1103. * preserved, committed copy. */
  1104. JBUFFER_TRACE(jh, "generate b_committed data");
  1105. if (!committed_data) {
  1106. jbd_unlock_bh_state(bh);
  1107. goto repeat;
  1108. }
  1109. jh->b_committed_data = committed_data;
  1110. committed_data = NULL;
  1111. memcpy(jh->b_committed_data, bh->b_data, bh->b_size);
  1112. }
  1113. jbd_unlock_bh_state(bh);
  1114. out:
  1115. jbd2_journal_put_journal_head(jh);
  1116. if (unlikely(committed_data))
  1117. jbd2_free(committed_data, bh->b_size);
  1118. return err;
  1119. }
  1120. /**
  1121. * void jbd2_journal_set_triggers() - Add triggers for commit writeout
  1122. * @bh: buffer to trigger on
  1123. * @type: struct jbd2_buffer_trigger_type containing the trigger(s).
  1124. *
  1125. * Set any triggers on this journal_head. This is always safe, because
  1126. * triggers for a committing buffer will be saved off, and triggers for
  1127. * a running transaction will match the buffer in that transaction.
  1128. *
  1129. * Call with NULL to clear the triggers.
  1130. */
  1131. void jbd2_journal_set_triggers(struct buffer_head *bh,
  1132. struct jbd2_buffer_trigger_type *type)
  1133. {
  1134. struct journal_head *jh = jbd2_journal_grab_journal_head(bh);
  1135. if (WARN_ON(!jh))
  1136. return;
  1137. jh->b_triggers = type;
  1138. jbd2_journal_put_journal_head(jh);
  1139. }
  1140. void jbd2_buffer_frozen_trigger(struct journal_head *jh, void *mapped_data,
  1141. struct jbd2_buffer_trigger_type *triggers)
  1142. {
  1143. struct buffer_head *bh = jh2bh(jh);
  1144. if (!triggers || !triggers->t_frozen)
  1145. return;
  1146. triggers->t_frozen(triggers, bh, mapped_data, bh->b_size);
  1147. }
  1148. void jbd2_buffer_abort_trigger(struct journal_head *jh,
  1149. struct jbd2_buffer_trigger_type *triggers)
  1150. {
  1151. if (!triggers || !triggers->t_abort)
  1152. return;
  1153. triggers->t_abort(triggers, jh2bh(jh));
  1154. }
  1155. /**
  1156. * int jbd2_journal_dirty_metadata() - mark a buffer as containing dirty metadata
  1157. * @handle: transaction to add buffer to.
  1158. * @bh: buffer to mark
  1159. *
  1160. * mark dirty metadata which needs to be journaled as part of the current
  1161. * transaction.
  1162. *
  1163. * The buffer must have previously had jbd2_journal_get_write_access()
  1164. * called so that it has a valid journal_head attached to the buffer
  1165. * head.
  1166. *
  1167. * The buffer is placed on the transaction's metadata list and is marked
  1168. * as belonging to the transaction.
  1169. *
  1170. * Returns error number or 0 on success.
  1171. *
  1172. * Special care needs to be taken if the buffer already belongs to the
  1173. * current committing transaction (in which case we should have frozen
  1174. * data present for that commit). In that case, we don't relink the
  1175. * buffer: that only gets done when the old transaction finally
  1176. * completes its commit.
  1177. */
  1178. int jbd2_journal_dirty_metadata(handle_t *handle, struct buffer_head *bh)
  1179. {
  1180. transaction_t *transaction = handle->h_transaction;
  1181. journal_t *journal;
  1182. struct journal_head *jh;
  1183. int ret = 0;
  1184. if (is_handle_aborted(handle))
  1185. return -EROFS;
  1186. if (!buffer_jbd(bh)) {
  1187. ret = -EUCLEAN;
  1188. goto out;
  1189. }
  1190. /*
  1191. * We don't grab jh reference here since the buffer must be part
  1192. * of the running transaction.
  1193. */
  1194. jh = bh2jh(bh);
  1195. /*
  1196. * This and the following assertions are unreliable since we may see jh
  1197. * in inconsistent state unless we grab bh_state lock. But this is
  1198. * crucial to catch bugs so let's do a reliable check until the
  1199. * lockless handling is fully proven.
  1200. */
  1201. if (jh->b_transaction != transaction &&
  1202. jh->b_next_transaction != transaction) {
  1203. jbd_lock_bh_state(bh);
  1204. J_ASSERT_JH(jh, jh->b_transaction == transaction ||
  1205. jh->b_next_transaction == transaction);
  1206. jbd_unlock_bh_state(bh);
  1207. }
  1208. if (jh->b_modified == 1) {
  1209. /* If it's in our transaction it must be in BJ_Metadata list. */
  1210. if (jh->b_transaction == transaction &&
  1211. jh->b_jlist != BJ_Metadata) {
  1212. jbd_lock_bh_state(bh);
  1213. J_ASSERT_JH(jh, jh->b_transaction != transaction ||
  1214. jh->b_jlist == BJ_Metadata);
  1215. jbd_unlock_bh_state(bh);
  1216. }
  1217. goto out;
  1218. }
  1219. journal = transaction->t_journal;
  1220. jbd_debug(5, "journal_head %p\n", jh);
  1221. JBUFFER_TRACE(jh, "entry");
  1222. jbd_lock_bh_state(bh);
  1223. if (jh->b_modified == 0) {
  1224. /*
  1225. * This buffer's got modified and becoming part
  1226. * of the transaction. This needs to be done
  1227. * once a transaction -bzzz
  1228. */
  1229. jh->b_modified = 1;
  1230. if (handle->h_buffer_credits <= 0) {
  1231. ret = -ENOSPC;
  1232. goto out_unlock_bh;
  1233. }
  1234. handle->h_buffer_credits--;
  1235. }
  1236. /*
  1237. * fastpath, to avoid expensive locking. If this buffer is already
  1238. * on the running transaction's metadata list there is nothing to do.
  1239. * Nobody can take it off again because there is a handle open.
  1240. * I _think_ we're OK here with SMP barriers - a mistaken decision will
  1241. * result in this test being false, so we go in and take the locks.
  1242. */
  1243. if (jh->b_transaction == transaction && jh->b_jlist == BJ_Metadata) {
  1244. JBUFFER_TRACE(jh, "fastpath");
  1245. if (unlikely(jh->b_transaction !=
  1246. journal->j_running_transaction)) {
  1247. printk(KERN_ERR "JBD2: %s: "
  1248. "jh->b_transaction (%llu, %p, %u) != "
  1249. "journal->j_running_transaction (%p, %u)\n",
  1250. journal->j_devname,
  1251. (unsigned long long) bh->b_blocknr,
  1252. jh->b_transaction,
  1253. jh->b_transaction ? jh->b_transaction->t_tid : 0,
  1254. journal->j_running_transaction,
  1255. journal->j_running_transaction ?
  1256. journal->j_running_transaction->t_tid : 0);
  1257. ret = -EINVAL;
  1258. }
  1259. goto out_unlock_bh;
  1260. }
  1261. set_buffer_jbddirty(bh);
  1262. /*
  1263. * Metadata already on the current transaction list doesn't
  1264. * need to be filed. Metadata on another transaction's list must
  1265. * be committing, and will be refiled once the commit completes:
  1266. * leave it alone for now.
  1267. */
  1268. if (jh->b_transaction != transaction) {
  1269. JBUFFER_TRACE(jh, "already on other transaction");
  1270. if (unlikely(((jh->b_transaction !=
  1271. journal->j_committing_transaction)) ||
  1272. (jh->b_next_transaction != transaction))) {
  1273. printk(KERN_ERR "jbd2_journal_dirty_metadata: %s: "
  1274. "bad jh for block %llu: "
  1275. "transaction (%p, %u), "
  1276. "jh->b_transaction (%p, %u), "
  1277. "jh->b_next_transaction (%p, %u), jlist %u\n",
  1278. journal->j_devname,
  1279. (unsigned long long) bh->b_blocknr,
  1280. transaction, transaction->t_tid,
  1281. jh->b_transaction,
  1282. jh->b_transaction ?
  1283. jh->b_transaction->t_tid : 0,
  1284. jh->b_next_transaction,
  1285. jh->b_next_transaction ?
  1286. jh->b_next_transaction->t_tid : 0,
  1287. jh->b_jlist);
  1288. WARN_ON(1);
  1289. ret = -EINVAL;
  1290. }
  1291. /* And this case is illegal: we can't reuse another
  1292. * transaction's data buffer, ever. */
  1293. goto out_unlock_bh;
  1294. }
  1295. /* That test should have eliminated the following case: */
  1296. J_ASSERT_JH(jh, jh->b_frozen_data == NULL);
  1297. JBUFFER_TRACE(jh, "file as BJ_Metadata");
  1298. spin_lock(&journal->j_list_lock);
  1299. __jbd2_journal_file_buffer(jh, transaction, BJ_Metadata);
  1300. spin_unlock(&journal->j_list_lock);
  1301. out_unlock_bh:
  1302. jbd_unlock_bh_state(bh);
  1303. out:
  1304. JBUFFER_TRACE(jh, "exit");
  1305. return ret;
  1306. }
  1307. /**
  1308. * void jbd2_journal_forget() - bforget() for potentially-journaled buffers.
  1309. * @handle: transaction handle
  1310. * @bh: bh to 'forget'
  1311. *
  1312. * We can only do the bforget if there are no commits pending against the
  1313. * buffer. If the buffer is dirty in the current running transaction we
  1314. * can safely unlink it.
  1315. *
  1316. * bh may not be a journalled buffer at all - it may be a non-JBD
  1317. * buffer which came off the hashtable. Check for this.
  1318. *
  1319. * Decrements bh->b_count by one.
  1320. *
  1321. * Allow this call even if the handle has aborted --- it may be part of
  1322. * the caller's cleanup after an abort.
  1323. */
  1324. int jbd2_journal_forget (handle_t *handle, struct buffer_head *bh)
  1325. {
  1326. transaction_t *transaction = handle->h_transaction;
  1327. journal_t *journal;
  1328. struct journal_head *jh;
  1329. int drop_reserve = 0;
  1330. int err = 0;
  1331. int was_modified = 0;
  1332. if (is_handle_aborted(handle))
  1333. return -EROFS;
  1334. journal = transaction->t_journal;
  1335. BUFFER_TRACE(bh, "entry");
  1336. jbd_lock_bh_state(bh);
  1337. if (!buffer_jbd(bh))
  1338. goto not_jbd;
  1339. jh = bh2jh(bh);
  1340. /* Critical error: attempting to delete a bitmap buffer, maybe?
  1341. * Don't do any jbd operations, and return an error. */
  1342. if (!J_EXPECT_JH(jh, !jh->b_committed_data,
  1343. "inconsistent data on disk")) {
  1344. err = -EIO;
  1345. goto not_jbd;
  1346. }
  1347. /* keep track of whether or not this transaction modified us */
  1348. was_modified = jh->b_modified;
  1349. /*
  1350. * The buffer's going from the transaction, we must drop
  1351. * all references -bzzz
  1352. */
  1353. jh->b_modified = 0;
  1354. if (jh->b_transaction == transaction) {
  1355. J_ASSERT_JH(jh, !jh->b_frozen_data);
  1356. /* If we are forgetting a buffer which is already part
  1357. * of this transaction, then we can just drop it from
  1358. * the transaction immediately. */
  1359. clear_buffer_dirty(bh);
  1360. clear_buffer_jbddirty(bh);
  1361. JBUFFER_TRACE(jh, "belongs to current transaction: unfile");
  1362. /*
  1363. * we only want to drop a reference if this transaction
  1364. * modified the buffer
  1365. */
  1366. if (was_modified)
  1367. drop_reserve = 1;
  1368. /*
  1369. * We are no longer going to journal this buffer.
  1370. * However, the commit of this transaction is still
  1371. * important to the buffer: the delete that we are now
  1372. * processing might obsolete an old log entry, so by
  1373. * committing, we can satisfy the buffer's checkpoint.
  1374. *
  1375. * So, if we have a checkpoint on the buffer, we should
  1376. * now refile the buffer on our BJ_Forget list so that
  1377. * we know to remove the checkpoint after we commit.
  1378. */
  1379. spin_lock(&journal->j_list_lock);
  1380. if (jh->b_cp_transaction) {
  1381. __jbd2_journal_temp_unlink_buffer(jh);
  1382. __jbd2_journal_file_buffer(jh, transaction, BJ_Forget);
  1383. } else {
  1384. __jbd2_journal_unfile_buffer(jh);
  1385. if (!buffer_jbd(bh)) {
  1386. spin_unlock(&journal->j_list_lock);
  1387. jbd_unlock_bh_state(bh);
  1388. __bforget(bh);
  1389. goto drop;
  1390. }
  1391. }
  1392. spin_unlock(&journal->j_list_lock);
  1393. } else if (jh->b_transaction) {
  1394. J_ASSERT_JH(jh, (jh->b_transaction ==
  1395. journal->j_committing_transaction));
  1396. /* However, if the buffer is still owned by a prior
  1397. * (committing) transaction, we can't drop it yet... */
  1398. JBUFFER_TRACE(jh, "belongs to older transaction");
  1399. /* ... but we CAN drop it from the new transaction if we
  1400. * have also modified it since the original commit. */
  1401. if (jh->b_next_transaction) {
  1402. J_ASSERT(jh->b_next_transaction == transaction);
  1403. spin_lock(&journal->j_list_lock);
  1404. jh->b_next_transaction = NULL;
  1405. spin_unlock(&journal->j_list_lock);
  1406. /*
  1407. * only drop a reference if this transaction modified
  1408. * the buffer
  1409. */
  1410. if (was_modified)
  1411. drop_reserve = 1;
  1412. }
  1413. }
  1414. not_jbd:
  1415. jbd_unlock_bh_state(bh);
  1416. __brelse(bh);
  1417. drop:
  1418. if (drop_reserve) {
  1419. /* no need to reserve log space for this block -bzzz */
  1420. handle->h_buffer_credits++;
  1421. }
  1422. return err;
  1423. }
  1424. /**
  1425. * int jbd2_journal_stop() - complete a transaction
  1426. * @handle: transaction to complete.
  1427. *
  1428. * All done for a particular handle.
  1429. *
  1430. * There is not much action needed here. We just return any remaining
  1431. * buffer credits to the transaction and remove the handle. The only
  1432. * complication is that we need to start a commit operation if the
  1433. * filesystem is marked for synchronous update.
  1434. *
  1435. * jbd2_journal_stop itself will not usually return an error, but it may
  1436. * do so in unusual circumstances. In particular, expect it to
  1437. * return -EIO if a jbd2_journal_abort has been executed since the
  1438. * transaction began.
  1439. */
  1440. int jbd2_journal_stop(handle_t *handle)
  1441. {
  1442. transaction_t *transaction = handle->h_transaction;
  1443. journal_t *journal;
  1444. int err = 0, wait_for_commit = 0;
  1445. tid_t tid;
  1446. pid_t pid;
  1447. if (!transaction) {
  1448. /*
  1449. * Handle is already detached from the transaction so
  1450. * there is nothing to do other than decrease a refcount,
  1451. * or free the handle if refcount drops to zero
  1452. */
  1453. if (--handle->h_ref > 0) {
  1454. jbd_debug(4, "h_ref %d -> %d\n", handle->h_ref + 1,
  1455. handle->h_ref);
  1456. return err;
  1457. } else {
  1458. if (handle->h_rsv_handle)
  1459. jbd2_free_handle(handle->h_rsv_handle);
  1460. goto free_and_exit;
  1461. }
  1462. }
  1463. journal = transaction->t_journal;
  1464. J_ASSERT(journal_current_handle() == handle);
  1465. if (is_handle_aborted(handle))
  1466. err = -EIO;
  1467. else
  1468. J_ASSERT(atomic_read(&transaction->t_updates) > 0);
  1469. if (--handle->h_ref > 0) {
  1470. jbd_debug(4, "h_ref %d -> %d\n", handle->h_ref + 1,
  1471. handle->h_ref);
  1472. return err;
  1473. }
  1474. jbd_debug(4, "Handle %p going down\n", handle);
  1475. trace_jbd2_handle_stats(journal->j_fs_dev->bd_dev,
  1476. transaction->t_tid,
  1477. handle->h_type, handle->h_line_no,
  1478. jiffies - handle->h_start_jiffies,
  1479. handle->h_sync, handle->h_requested_credits,
  1480. (handle->h_requested_credits -
  1481. handle->h_buffer_credits));
  1482. /*
  1483. * Implement synchronous transaction batching. If the handle
  1484. * was synchronous, don't force a commit immediately. Let's
  1485. * yield and let another thread piggyback onto this
  1486. * transaction. Keep doing that while new threads continue to
  1487. * arrive. It doesn't cost much - we're about to run a commit
  1488. * and sleep on IO anyway. Speeds up many-threaded, many-dir
  1489. * operations by 30x or more...
  1490. *
  1491. * We try and optimize the sleep time against what the
  1492. * underlying disk can do, instead of having a static sleep
  1493. * time. This is useful for the case where our storage is so
  1494. * fast that it is more optimal to go ahead and force a flush
  1495. * and wait for the transaction to be committed than it is to
  1496. * wait for an arbitrary amount of time for new writers to
  1497. * join the transaction. We achieve this by measuring how
  1498. * long it takes to commit a transaction, and compare it with
  1499. * how long this transaction has been running, and if run time
  1500. * < commit time then we sleep for the delta and commit. This
  1501. * greatly helps super fast disks that would see slowdowns as
  1502. * more threads started doing fsyncs.
  1503. *
  1504. * But don't do this if this process was the most recent one
  1505. * to perform a synchronous write. We do this to detect the
  1506. * case where a single process is doing a stream of sync
  1507. * writes. No point in waiting for joiners in that case.
  1508. *
  1509. * Setting max_batch_time to 0 disables this completely.
  1510. */
  1511. pid = current->pid;
  1512. if (handle->h_sync && journal->j_last_sync_writer != pid &&
  1513. journal->j_max_batch_time) {
  1514. u64 commit_time, trans_time;
  1515. journal->j_last_sync_writer = pid;
  1516. read_lock(&journal->j_state_lock);
  1517. commit_time = journal->j_average_commit_time;
  1518. read_unlock(&journal->j_state_lock);
  1519. trans_time = ktime_to_ns(ktime_sub(ktime_get(),
  1520. transaction->t_start_time));
  1521. commit_time = max_t(u64, commit_time,
  1522. 1000*journal->j_min_batch_time);
  1523. commit_time = min_t(u64, commit_time,
  1524. 1000*journal->j_max_batch_time);
  1525. if (trans_time < commit_time) {
  1526. ktime_t expires = ktime_add_ns(ktime_get(),
  1527. commit_time);
  1528. set_current_state(TASK_UNINTERRUPTIBLE);
  1529. schedule_hrtimeout(&expires, HRTIMER_MODE_ABS);
  1530. }
  1531. }
  1532. if (handle->h_sync)
  1533. transaction->t_synchronous_commit = 1;
  1534. current->journal_info = NULL;
  1535. atomic_sub(handle->h_buffer_credits,
  1536. &transaction->t_outstanding_credits);
  1537. /*
  1538. * If the handle is marked SYNC, we need to set another commit
  1539. * going! We also want to force a commit if the current
  1540. * transaction is occupying too much of the log, or if the
  1541. * transaction is too old now.
  1542. */
  1543. if (handle->h_sync ||
  1544. (atomic_read(&transaction->t_outstanding_credits) >
  1545. journal->j_max_transaction_buffers) ||
  1546. time_after_eq(jiffies, transaction->t_expires)) {
  1547. /* Do this even for aborted journals: an abort still
  1548. * completes the commit thread, it just doesn't write
  1549. * anything to disk. */
  1550. jbd_debug(2, "transaction too old, requesting commit for "
  1551. "handle %p\n", handle);
  1552. /* This is non-blocking */
  1553. jbd2_log_start_commit(journal, transaction->t_tid);
  1554. /*
  1555. * Special case: JBD2_SYNC synchronous updates require us
  1556. * to wait for the commit to complete.
  1557. */
  1558. if (handle->h_sync && !(current->flags & PF_MEMALLOC))
  1559. wait_for_commit = 1;
  1560. }
  1561. /*
  1562. * Once we drop t_updates, if it goes to zero the transaction
  1563. * could start committing on us and eventually disappear. So
  1564. * once we do this, we must not dereference transaction
  1565. * pointer again.
  1566. */
  1567. tid = transaction->t_tid;
  1568. if (atomic_dec_and_test(&transaction->t_updates)) {
  1569. wake_up(&journal->j_wait_updates);
  1570. if (journal->j_barrier_count)
  1571. wake_up(&journal->j_wait_transaction_locked);
  1572. }
  1573. rwsem_release(&journal->j_trans_commit_map, 1, _THIS_IP_);
  1574. if (wait_for_commit)
  1575. err = jbd2_log_wait_commit(journal, tid);
  1576. if (handle->h_rsv_handle)
  1577. jbd2_journal_free_reserved(handle->h_rsv_handle);
  1578. free_and_exit:
  1579. jbd2_free_handle(handle);
  1580. return err;
  1581. }
  1582. /*
  1583. *
  1584. * List management code snippets: various functions for manipulating the
  1585. * transaction buffer lists.
  1586. *
  1587. */
  1588. /*
  1589. * Append a buffer to a transaction list, given the transaction's list head
  1590. * pointer.
  1591. *
  1592. * j_list_lock is held.
  1593. *
  1594. * jbd_lock_bh_state(jh2bh(jh)) is held.
  1595. */
  1596. static inline void
  1597. __blist_add_buffer(struct journal_head **list, struct journal_head *jh)
  1598. {
  1599. if (!*list) {
  1600. jh->b_tnext = jh->b_tprev = jh;
  1601. *list = jh;
  1602. } else {
  1603. /* Insert at the tail of the list to preserve order */
  1604. struct journal_head *first = *list, *last = first->b_tprev;
  1605. jh->b_tprev = last;
  1606. jh->b_tnext = first;
  1607. last->b_tnext = first->b_tprev = jh;
  1608. }
  1609. }
  1610. /*
  1611. * Remove a buffer from a transaction list, given the transaction's list
  1612. * head pointer.
  1613. *
  1614. * Called with j_list_lock held, and the journal may not be locked.
  1615. *
  1616. * jbd_lock_bh_state(jh2bh(jh)) is held.
  1617. */
  1618. static inline void
  1619. __blist_del_buffer(struct journal_head **list, struct journal_head *jh)
  1620. {
  1621. if (*list == jh) {
  1622. *list = jh->b_tnext;
  1623. if (*list == jh)
  1624. *list = NULL;
  1625. }
  1626. jh->b_tprev->b_tnext = jh->b_tnext;
  1627. jh->b_tnext->b_tprev = jh->b_tprev;
  1628. }
  1629. /*
  1630. * Remove a buffer from the appropriate transaction list.
  1631. *
  1632. * Note that this function can *change* the value of
  1633. * bh->b_transaction->t_buffers, t_forget, t_shadow_list, t_log_list or
  1634. * t_reserved_list. If the caller is holding onto a copy of one of these
  1635. * pointers, it could go bad. Generally the caller needs to re-read the
  1636. * pointer from the transaction_t.
  1637. *
  1638. * Called under j_list_lock.
  1639. */
  1640. static void __jbd2_journal_temp_unlink_buffer(struct journal_head *jh)
  1641. {
  1642. struct journal_head **list = NULL;
  1643. transaction_t *transaction;
  1644. struct buffer_head *bh = jh2bh(jh);
  1645. J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
  1646. transaction = jh->b_transaction;
  1647. if (transaction)
  1648. assert_spin_locked(&transaction->t_journal->j_list_lock);
  1649. J_ASSERT_JH(jh, jh->b_jlist < BJ_Types);
  1650. if (jh->b_jlist != BJ_None)
  1651. J_ASSERT_JH(jh, transaction != NULL);
  1652. switch (jh->b_jlist) {
  1653. case BJ_None:
  1654. return;
  1655. case BJ_Metadata:
  1656. transaction->t_nr_buffers--;
  1657. J_ASSERT_JH(jh, transaction->t_nr_buffers >= 0);
  1658. list = &transaction->t_buffers;
  1659. break;
  1660. case BJ_Forget:
  1661. list = &transaction->t_forget;
  1662. break;
  1663. case BJ_Shadow:
  1664. list = &transaction->t_shadow_list;
  1665. break;
  1666. case BJ_Reserved:
  1667. list = &transaction->t_reserved_list;
  1668. break;
  1669. }
  1670. __blist_del_buffer(list, jh);
  1671. jh->b_jlist = BJ_None;
  1672. if (test_clear_buffer_jbddirty(bh))
  1673. mark_buffer_dirty(bh); /* Expose it to the VM */
  1674. }
  1675. /*
  1676. * Remove buffer from all transactions.
  1677. *
  1678. * Called with bh_state lock and j_list_lock
  1679. *
  1680. * jh and bh may be already freed when this function returns.
  1681. */
  1682. static void __jbd2_journal_unfile_buffer(struct journal_head *jh)
  1683. {
  1684. __jbd2_journal_temp_unlink_buffer(jh);
  1685. jh->b_transaction = NULL;
  1686. jbd2_journal_put_journal_head(jh);
  1687. }
  1688. void jbd2_journal_unfile_buffer(journal_t *journal, struct journal_head *jh)
  1689. {
  1690. struct buffer_head *bh = jh2bh(jh);
  1691. /* Get reference so that buffer cannot be freed before we unlock it */
  1692. get_bh(bh);
  1693. jbd_lock_bh_state(bh);
  1694. spin_lock(&journal->j_list_lock);
  1695. __jbd2_journal_unfile_buffer(jh);
  1696. spin_unlock(&journal->j_list_lock);
  1697. jbd_unlock_bh_state(bh);
  1698. __brelse(bh);
  1699. }
  1700. /*
  1701. * Called from jbd2_journal_try_to_free_buffers().
  1702. *
  1703. * Called under jbd_lock_bh_state(bh)
  1704. */
  1705. static void
  1706. __journal_try_to_free_buffer(journal_t *journal, struct buffer_head *bh)
  1707. {
  1708. struct journal_head *jh;
  1709. jh = bh2jh(bh);
  1710. if (buffer_locked(bh) || buffer_dirty(bh))
  1711. goto out;
  1712. if (jh->b_next_transaction != NULL || jh->b_transaction != NULL)
  1713. goto out;
  1714. spin_lock(&journal->j_list_lock);
  1715. if (jh->b_cp_transaction != NULL) {
  1716. /* written-back checkpointed metadata buffer */
  1717. JBUFFER_TRACE(jh, "remove from checkpoint list");
  1718. __jbd2_journal_remove_checkpoint(jh);
  1719. }
  1720. spin_unlock(&journal->j_list_lock);
  1721. out:
  1722. return;
  1723. }
  1724. /**
  1725. * int jbd2_journal_try_to_free_buffers() - try to free page buffers.
  1726. * @journal: journal for operation
  1727. * @page: to try and free
  1728. * @gfp_mask: we use the mask to detect how hard should we try to release
  1729. * buffers. If __GFP_DIRECT_RECLAIM and __GFP_FS is set, we wait for commit
  1730. * code to release the buffers.
  1731. *
  1732. *
  1733. * For all the buffers on this page,
  1734. * if they are fully written out ordered data, move them onto BUF_CLEAN
  1735. * so try_to_free_buffers() can reap them.
  1736. *
  1737. * This function returns non-zero if we wish try_to_free_buffers()
  1738. * to be called. We do this if the page is releasable by try_to_free_buffers().
  1739. * We also do it if the page has locked or dirty buffers and the caller wants
  1740. * us to perform sync or async writeout.
  1741. *
  1742. * This complicates JBD locking somewhat. We aren't protected by the
  1743. * BKL here. We wish to remove the buffer from its committing or
  1744. * running transaction's ->t_datalist via __jbd2_journal_unfile_buffer.
  1745. *
  1746. * This may *change* the value of transaction_t->t_datalist, so anyone
  1747. * who looks at t_datalist needs to lock against this function.
  1748. *
  1749. * Even worse, someone may be doing a jbd2_journal_dirty_data on this
  1750. * buffer. So we need to lock against that. jbd2_journal_dirty_data()
  1751. * will come out of the lock with the buffer dirty, which makes it
  1752. * ineligible for release here.
  1753. *
  1754. * Who else is affected by this? hmm... Really the only contender
  1755. * is do_get_write_access() - it could be looking at the buffer while
  1756. * journal_try_to_free_buffer() is changing its state. But that
  1757. * cannot happen because we never reallocate freed data as metadata
  1758. * while the data is part of a transaction. Yes?
  1759. *
  1760. * Return 0 on failure, 1 on success
  1761. */
  1762. int jbd2_journal_try_to_free_buffers(journal_t *journal,
  1763. struct page *page, gfp_t gfp_mask)
  1764. {
  1765. struct buffer_head *head;
  1766. struct buffer_head *bh;
  1767. int ret = 0;
  1768. J_ASSERT(PageLocked(page));
  1769. head = page_buffers(page);
  1770. bh = head;
  1771. do {
  1772. struct journal_head *jh;
  1773. /*
  1774. * We take our own ref against the journal_head here to avoid
  1775. * having to add tons of locking around each instance of
  1776. * jbd2_journal_put_journal_head().
  1777. */
  1778. jh = jbd2_journal_grab_journal_head(bh);
  1779. if (!jh)
  1780. continue;
  1781. jbd_lock_bh_state(bh);
  1782. __journal_try_to_free_buffer(journal, bh);
  1783. jbd2_journal_put_journal_head(jh);
  1784. jbd_unlock_bh_state(bh);
  1785. if (buffer_jbd(bh))
  1786. goto busy;
  1787. } while ((bh = bh->b_this_page) != head);
  1788. ret = try_to_free_buffers(page);
  1789. busy:
  1790. return ret;
  1791. }
  1792. /*
  1793. * This buffer is no longer needed. If it is on an older transaction's
  1794. * checkpoint list we need to record it on this transaction's forget list
  1795. * to pin this buffer (and hence its checkpointing transaction) down until
  1796. * this transaction commits. If the buffer isn't on a checkpoint list, we
  1797. * release it.
  1798. * Returns non-zero if JBD no longer has an interest in the buffer.
  1799. *
  1800. * Called under j_list_lock.
  1801. *
  1802. * Called under jbd_lock_bh_state(bh).
  1803. */
  1804. static int __dispose_buffer(struct journal_head *jh, transaction_t *transaction)
  1805. {
  1806. int may_free = 1;
  1807. struct buffer_head *bh = jh2bh(jh);
  1808. if (jh->b_cp_transaction) {
  1809. JBUFFER_TRACE(jh, "on running+cp transaction");
  1810. __jbd2_journal_temp_unlink_buffer(jh);
  1811. /*
  1812. * We don't want to write the buffer anymore, clear the
  1813. * bit so that we don't confuse checks in
  1814. * __journal_file_buffer
  1815. */
  1816. clear_buffer_dirty(bh);
  1817. __jbd2_journal_file_buffer(jh, transaction, BJ_Forget);
  1818. may_free = 0;
  1819. } else {
  1820. JBUFFER_TRACE(jh, "on running transaction");
  1821. __jbd2_journal_unfile_buffer(jh);
  1822. }
  1823. return may_free;
  1824. }
  1825. /*
  1826. * jbd2_journal_invalidatepage
  1827. *
  1828. * This code is tricky. It has a number of cases to deal with.
  1829. *
  1830. * There are two invariants which this code relies on:
  1831. *
  1832. * i_size must be updated on disk before we start calling invalidatepage on the
  1833. * data.
  1834. *
  1835. * This is done in ext3 by defining an ext3_setattr method which
  1836. * updates i_size before truncate gets going. By maintaining this
  1837. * invariant, we can be sure that it is safe to throw away any buffers
  1838. * attached to the current transaction: once the transaction commits,
  1839. * we know that the data will not be needed.
  1840. *
  1841. * Note however that we can *not* throw away data belonging to the
  1842. * previous, committing transaction!
  1843. *
  1844. * Any disk blocks which *are* part of the previous, committing
  1845. * transaction (and which therefore cannot be discarded immediately) are
  1846. * not going to be reused in the new running transaction
  1847. *
  1848. * The bitmap committed_data images guarantee this: any block which is
  1849. * allocated in one transaction and removed in the next will be marked
  1850. * as in-use in the committed_data bitmap, so cannot be reused until
  1851. * the next transaction to delete the block commits. This means that
  1852. * leaving committing buffers dirty is quite safe: the disk blocks
  1853. * cannot be reallocated to a different file and so buffer aliasing is
  1854. * not possible.
  1855. *
  1856. *
  1857. * The above applies mainly to ordered data mode. In writeback mode we
  1858. * don't make guarantees about the order in which data hits disk --- in
  1859. * particular we don't guarantee that new dirty data is flushed before
  1860. * transaction commit --- so it is always safe just to discard data
  1861. * immediately in that mode. --sct
  1862. */
  1863. /*
  1864. * The journal_unmap_buffer helper function returns zero if the buffer
  1865. * concerned remains pinned as an anonymous buffer belonging to an older
  1866. * transaction.
  1867. *
  1868. * We're outside-transaction here. Either or both of j_running_transaction
  1869. * and j_committing_transaction may be NULL.
  1870. */
  1871. static int journal_unmap_buffer(journal_t *journal, struct buffer_head *bh,
  1872. int partial_page)
  1873. {
  1874. transaction_t *transaction;
  1875. struct journal_head *jh;
  1876. int may_free = 1;
  1877. BUFFER_TRACE(bh, "entry");
  1878. /*
  1879. * It is safe to proceed here without the j_list_lock because the
  1880. * buffers cannot be stolen by try_to_free_buffers as long as we are
  1881. * holding the page lock. --sct
  1882. */
  1883. if (!buffer_jbd(bh))
  1884. goto zap_buffer_unlocked;
  1885. /* OK, we have data buffer in journaled mode */
  1886. write_lock(&journal->j_state_lock);
  1887. jbd_lock_bh_state(bh);
  1888. spin_lock(&journal->j_list_lock);
  1889. jh = jbd2_journal_grab_journal_head(bh);
  1890. if (!jh)
  1891. goto zap_buffer_no_jh;
  1892. /*
  1893. * We cannot remove the buffer from checkpoint lists until the
  1894. * transaction adding inode to orphan list (let's call it T)
  1895. * is committed. Otherwise if the transaction changing the
  1896. * buffer would be cleaned from the journal before T is
  1897. * committed, a crash will cause that the correct contents of
  1898. * the buffer will be lost. On the other hand we have to
  1899. * clear the buffer dirty bit at latest at the moment when the
  1900. * transaction marking the buffer as freed in the filesystem
  1901. * structures is committed because from that moment on the
  1902. * block can be reallocated and used by a different page.
  1903. * Since the block hasn't been freed yet but the inode has
  1904. * already been added to orphan list, it is safe for us to add
  1905. * the buffer to BJ_Forget list of the newest transaction.
  1906. *
  1907. * Also we have to clear buffer_mapped flag of a truncated buffer
  1908. * because the buffer_head may be attached to the page straddling
  1909. * i_size (can happen only when blocksize < pagesize) and thus the
  1910. * buffer_head can be reused when the file is extended again. So we end
  1911. * up keeping around invalidated buffers attached to transactions'
  1912. * BJ_Forget list just to stop checkpointing code from cleaning up
  1913. * the transaction this buffer was modified in.
  1914. */
  1915. transaction = jh->b_transaction;
  1916. if (transaction == NULL) {
  1917. /* First case: not on any transaction. If it
  1918. * has no checkpoint link, then we can zap it:
  1919. * it's a writeback-mode buffer so we don't care
  1920. * if it hits disk safely. */
  1921. if (!jh->b_cp_transaction) {
  1922. JBUFFER_TRACE(jh, "not on any transaction: zap");
  1923. goto zap_buffer;
  1924. }
  1925. if (!buffer_dirty(bh)) {
  1926. /* bdflush has written it. We can drop it now */
  1927. __jbd2_journal_remove_checkpoint(jh);
  1928. goto zap_buffer;
  1929. }
  1930. /* OK, it must be in the journal but still not
  1931. * written fully to disk: it's metadata or
  1932. * journaled data... */
  1933. if (journal->j_running_transaction) {
  1934. /* ... and once the current transaction has
  1935. * committed, the buffer won't be needed any
  1936. * longer. */
  1937. JBUFFER_TRACE(jh, "checkpointed: add to BJ_Forget");
  1938. may_free = __dispose_buffer(jh,
  1939. journal->j_running_transaction);
  1940. goto zap_buffer;
  1941. } else {
  1942. /* There is no currently-running transaction. So the
  1943. * orphan record which we wrote for this file must have
  1944. * passed into commit. We must attach this buffer to
  1945. * the committing transaction, if it exists. */
  1946. if (journal->j_committing_transaction) {
  1947. JBUFFER_TRACE(jh, "give to committing trans");
  1948. may_free = __dispose_buffer(jh,
  1949. journal->j_committing_transaction);
  1950. goto zap_buffer;
  1951. } else {
  1952. /* The orphan record's transaction has
  1953. * committed. We can cleanse this buffer */
  1954. clear_buffer_jbddirty(bh);
  1955. __jbd2_journal_remove_checkpoint(jh);
  1956. goto zap_buffer;
  1957. }
  1958. }
  1959. } else if (transaction == journal->j_committing_transaction) {
  1960. JBUFFER_TRACE(jh, "on committing transaction");
  1961. /*
  1962. * The buffer is committing, we simply cannot touch
  1963. * it. If the page is straddling i_size we have to wait
  1964. * for commit and try again.
  1965. */
  1966. if (partial_page) {
  1967. jbd2_journal_put_journal_head(jh);
  1968. spin_unlock(&journal->j_list_lock);
  1969. jbd_unlock_bh_state(bh);
  1970. write_unlock(&journal->j_state_lock);
  1971. return -EBUSY;
  1972. }
  1973. /*
  1974. * OK, buffer won't be reachable after truncate. We just set
  1975. * j_next_transaction to the running transaction (if there is
  1976. * one) and mark buffer as freed so that commit code knows it
  1977. * should clear dirty bits when it is done with the buffer.
  1978. */
  1979. set_buffer_freed(bh);
  1980. if (journal->j_running_transaction && buffer_jbddirty(bh))
  1981. jh->b_next_transaction = journal->j_running_transaction;
  1982. jbd2_journal_put_journal_head(jh);
  1983. spin_unlock(&journal->j_list_lock);
  1984. jbd_unlock_bh_state(bh);
  1985. write_unlock(&journal->j_state_lock);
  1986. return 0;
  1987. } else {
  1988. /* Good, the buffer belongs to the running transaction.
  1989. * We are writing our own transaction's data, not any
  1990. * previous one's, so it is safe to throw it away
  1991. * (remember that we expect the filesystem to have set
  1992. * i_size already for this truncate so recovery will not
  1993. * expose the disk blocks we are discarding here.) */
  1994. J_ASSERT_JH(jh, transaction == journal->j_running_transaction);
  1995. JBUFFER_TRACE(jh, "on running transaction");
  1996. may_free = __dispose_buffer(jh, transaction);
  1997. }
  1998. zap_buffer:
  1999. /*
  2000. * This is tricky. Although the buffer is truncated, it may be reused
  2001. * if blocksize < pagesize and it is attached to the page straddling
  2002. * EOF. Since the buffer might have been added to BJ_Forget list of the
  2003. * running transaction, journal_get_write_access() won't clear
  2004. * b_modified and credit accounting gets confused. So clear b_modified
  2005. * here.
  2006. */
  2007. jh->b_modified = 0;
  2008. jbd2_journal_put_journal_head(jh);
  2009. zap_buffer_no_jh:
  2010. spin_unlock(&journal->j_list_lock);
  2011. jbd_unlock_bh_state(bh);
  2012. write_unlock(&journal->j_state_lock);
  2013. zap_buffer_unlocked:
  2014. clear_buffer_dirty(bh);
  2015. J_ASSERT_BH(bh, !buffer_jbddirty(bh));
  2016. clear_buffer_mapped(bh);
  2017. clear_buffer_req(bh);
  2018. clear_buffer_new(bh);
  2019. clear_buffer_delay(bh);
  2020. clear_buffer_unwritten(bh);
  2021. bh->b_bdev = NULL;
  2022. return may_free;
  2023. }
  2024. /**
  2025. * void jbd2_journal_invalidatepage()
  2026. * @journal: journal to use for flush...
  2027. * @page: page to flush
  2028. * @offset: start of the range to invalidate
  2029. * @length: length of the range to invalidate
  2030. *
  2031. * Reap page buffers containing data after in the specified range in page.
  2032. * Can return -EBUSY if buffers are part of the committing transaction and
  2033. * the page is straddling i_size. Caller then has to wait for current commit
  2034. * and try again.
  2035. */
  2036. int jbd2_journal_invalidatepage(journal_t *journal,
  2037. struct page *page,
  2038. unsigned int offset,
  2039. unsigned int length)
  2040. {
  2041. struct buffer_head *head, *bh, *next;
  2042. unsigned int stop = offset + length;
  2043. unsigned int curr_off = 0;
  2044. int partial_page = (offset || length < PAGE_SIZE);
  2045. int may_free = 1;
  2046. int ret = 0;
  2047. if (!PageLocked(page))
  2048. BUG();
  2049. if (!page_has_buffers(page))
  2050. return 0;
  2051. BUG_ON(stop > PAGE_SIZE || stop < length);
  2052. /* We will potentially be playing with lists other than just the
  2053. * data lists (especially for journaled data mode), so be
  2054. * cautious in our locking. */
  2055. head = bh = page_buffers(page);
  2056. do {
  2057. unsigned int next_off = curr_off + bh->b_size;
  2058. next = bh->b_this_page;
  2059. if (next_off > stop)
  2060. return 0;
  2061. if (offset <= curr_off) {
  2062. /* This block is wholly outside the truncation point */
  2063. lock_buffer(bh);
  2064. ret = journal_unmap_buffer(journal, bh, partial_page);
  2065. unlock_buffer(bh);
  2066. if (ret < 0)
  2067. return ret;
  2068. may_free &= ret;
  2069. }
  2070. curr_off = next_off;
  2071. bh = next;
  2072. } while (bh != head);
  2073. if (!partial_page) {
  2074. if (may_free && try_to_free_buffers(page))
  2075. J_ASSERT(!page_has_buffers(page));
  2076. }
  2077. return 0;
  2078. }
  2079. /*
  2080. * File a buffer on the given transaction list.
  2081. */
  2082. void __jbd2_journal_file_buffer(struct journal_head *jh,
  2083. transaction_t *transaction, int jlist)
  2084. {
  2085. struct journal_head **list = NULL;
  2086. int was_dirty = 0;
  2087. struct buffer_head *bh = jh2bh(jh);
  2088. J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
  2089. assert_spin_locked(&transaction->t_journal->j_list_lock);
  2090. J_ASSERT_JH(jh, jh->b_jlist < BJ_Types);
  2091. J_ASSERT_JH(jh, jh->b_transaction == transaction ||
  2092. jh->b_transaction == NULL);
  2093. if (jh->b_transaction && jh->b_jlist == jlist)
  2094. return;
  2095. if (jlist == BJ_Metadata || jlist == BJ_Reserved ||
  2096. jlist == BJ_Shadow || jlist == BJ_Forget) {
  2097. /*
  2098. * For metadata buffers, we track dirty bit in buffer_jbddirty
  2099. * instead of buffer_dirty. We should not see a dirty bit set
  2100. * here because we clear it in do_get_write_access but e.g.
  2101. * tune2fs can modify the sb and set the dirty bit at any time
  2102. * so we try to gracefully handle that.
  2103. */
  2104. if (buffer_dirty(bh))
  2105. warn_dirty_buffer(bh);
  2106. if (test_clear_buffer_dirty(bh) ||
  2107. test_clear_buffer_jbddirty(bh))
  2108. was_dirty = 1;
  2109. }
  2110. if (jh->b_transaction)
  2111. __jbd2_journal_temp_unlink_buffer(jh);
  2112. else
  2113. jbd2_journal_grab_journal_head(bh);
  2114. jh->b_transaction = transaction;
  2115. switch (jlist) {
  2116. case BJ_None:
  2117. J_ASSERT_JH(jh, !jh->b_committed_data);
  2118. J_ASSERT_JH(jh, !jh->b_frozen_data);
  2119. return;
  2120. case BJ_Metadata:
  2121. transaction->t_nr_buffers++;
  2122. list = &transaction->t_buffers;
  2123. break;
  2124. case BJ_Forget:
  2125. list = &transaction->t_forget;
  2126. break;
  2127. case BJ_Shadow:
  2128. list = &transaction->t_shadow_list;
  2129. break;
  2130. case BJ_Reserved:
  2131. list = &transaction->t_reserved_list;
  2132. break;
  2133. }
  2134. __blist_add_buffer(list, jh);
  2135. jh->b_jlist = jlist;
  2136. if (was_dirty)
  2137. set_buffer_jbddirty(bh);
  2138. }
  2139. void jbd2_journal_file_buffer(struct journal_head *jh,
  2140. transaction_t *transaction, int jlist)
  2141. {
  2142. jbd_lock_bh_state(jh2bh(jh));
  2143. spin_lock(&transaction->t_journal->j_list_lock);
  2144. __jbd2_journal_file_buffer(jh, transaction, jlist);
  2145. spin_unlock(&transaction->t_journal->j_list_lock);
  2146. jbd_unlock_bh_state(jh2bh(jh));
  2147. }
  2148. /*
  2149. * Remove a buffer from its current buffer list in preparation for
  2150. * dropping it from its current transaction entirely. If the buffer has
  2151. * already started to be used by a subsequent transaction, refile the
  2152. * buffer on that transaction's metadata list.
  2153. *
  2154. * Called under j_list_lock
  2155. * Called under jbd_lock_bh_state(jh2bh(jh))
  2156. *
  2157. * jh and bh may be already free when this function returns
  2158. */
  2159. void __jbd2_journal_refile_buffer(struct journal_head *jh)
  2160. {
  2161. int was_dirty, jlist;
  2162. struct buffer_head *bh = jh2bh(jh);
  2163. J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
  2164. if (jh->b_transaction)
  2165. assert_spin_locked(&jh->b_transaction->t_journal->j_list_lock);
  2166. /* If the buffer is now unused, just drop it. */
  2167. if (jh->b_next_transaction == NULL) {
  2168. __jbd2_journal_unfile_buffer(jh);
  2169. return;
  2170. }
  2171. /*
  2172. * It has been modified by a later transaction: add it to the new
  2173. * transaction's metadata list.
  2174. */
  2175. was_dirty = test_clear_buffer_jbddirty(bh);
  2176. __jbd2_journal_temp_unlink_buffer(jh);
  2177. /*
  2178. * We set b_transaction here because b_next_transaction will inherit
  2179. * our jh reference and thus __jbd2_journal_file_buffer() must not
  2180. * take a new one.
  2181. */
  2182. jh->b_transaction = jh->b_next_transaction;
  2183. jh->b_next_transaction = NULL;
  2184. if (buffer_freed(bh))
  2185. jlist = BJ_Forget;
  2186. else if (jh->b_modified)
  2187. jlist = BJ_Metadata;
  2188. else
  2189. jlist = BJ_Reserved;
  2190. __jbd2_journal_file_buffer(jh, jh->b_transaction, jlist);
  2191. J_ASSERT_JH(jh, jh->b_transaction->t_state == T_RUNNING);
  2192. if (was_dirty)
  2193. set_buffer_jbddirty(bh);
  2194. }
  2195. /*
  2196. * __jbd2_journal_refile_buffer() with necessary locking added. We take our
  2197. * bh reference so that we can safely unlock bh.
  2198. *
  2199. * The jh and bh may be freed by this call.
  2200. */
  2201. void jbd2_journal_refile_buffer(journal_t *journal, struct journal_head *jh)
  2202. {
  2203. struct buffer_head *bh = jh2bh(jh);
  2204. /* Get reference so that buffer cannot be freed before we unlock it */
  2205. get_bh(bh);
  2206. jbd_lock_bh_state(bh);
  2207. spin_lock(&journal->j_list_lock);
  2208. __jbd2_journal_refile_buffer(jh);
  2209. jbd_unlock_bh_state(bh);
  2210. spin_unlock(&journal->j_list_lock);
  2211. __brelse(bh);
  2212. }
  2213. /*
  2214. * File inode in the inode list of the handle's transaction
  2215. */
  2216. static int jbd2_journal_file_inode(handle_t *handle, struct jbd2_inode *jinode,
  2217. unsigned long flags)
  2218. {
  2219. transaction_t *transaction = handle->h_transaction;
  2220. journal_t *journal;
  2221. if (is_handle_aborted(handle))
  2222. return -EROFS;
  2223. journal = transaction->t_journal;
  2224. jbd_debug(4, "Adding inode %lu, tid:%d\n", jinode->i_vfs_inode->i_ino,
  2225. transaction->t_tid);
  2226. /*
  2227. * First check whether inode isn't already on the transaction's
  2228. * lists without taking the lock. Note that this check is safe
  2229. * without the lock as we cannot race with somebody removing inode
  2230. * from the transaction. The reason is that we remove inode from the
  2231. * transaction only in journal_release_jbd_inode() and when we commit
  2232. * the transaction. We are guarded from the first case by holding
  2233. * a reference to the inode. We are safe against the second case
  2234. * because if jinode->i_transaction == transaction, commit code
  2235. * cannot touch the transaction because we hold reference to it,
  2236. * and if jinode->i_next_transaction == transaction, commit code
  2237. * will only file the inode where we want it.
  2238. */
  2239. if ((jinode->i_transaction == transaction ||
  2240. jinode->i_next_transaction == transaction) &&
  2241. (jinode->i_flags & flags) == flags)
  2242. return 0;
  2243. spin_lock(&journal->j_list_lock);
  2244. jinode->i_flags |= flags;
  2245. /* Is inode already attached where we need it? */
  2246. if (jinode->i_transaction == transaction ||
  2247. jinode->i_next_transaction == transaction)
  2248. goto done;
  2249. /*
  2250. * We only ever set this variable to 1 so the test is safe. Since
  2251. * t_need_data_flush is likely to be set, we do the test to save some
  2252. * cacheline bouncing
  2253. */
  2254. if (!transaction->t_need_data_flush)
  2255. transaction->t_need_data_flush = 1;
  2256. /* On some different transaction's list - should be
  2257. * the committing one */
  2258. if (jinode->i_transaction) {
  2259. J_ASSERT(jinode->i_next_transaction == NULL);
  2260. J_ASSERT(jinode->i_transaction ==
  2261. journal->j_committing_transaction);
  2262. jinode->i_next_transaction = transaction;
  2263. goto done;
  2264. }
  2265. /* Not on any transaction list... */
  2266. J_ASSERT(!jinode->i_next_transaction);
  2267. jinode->i_transaction = transaction;
  2268. list_add(&jinode->i_list, &transaction->t_inode_list);
  2269. done:
  2270. spin_unlock(&journal->j_list_lock);
  2271. return 0;
  2272. }
  2273. int jbd2_journal_inode_add_write(handle_t *handle, struct jbd2_inode *jinode)
  2274. {
  2275. return jbd2_journal_file_inode(handle, jinode,
  2276. JI_WRITE_DATA | JI_WAIT_DATA);
  2277. }
  2278. int jbd2_journal_inode_add_wait(handle_t *handle, struct jbd2_inode *jinode)
  2279. {
  2280. return jbd2_journal_file_inode(handle, jinode, JI_WAIT_DATA);
  2281. }
  2282. /*
  2283. * File truncate and transaction commit interact with each other in a
  2284. * non-trivial way. If a transaction writing data block A is
  2285. * committing, we cannot discard the data by truncate until we have
  2286. * written them. Otherwise if we crashed after the transaction with
  2287. * write has committed but before the transaction with truncate has
  2288. * committed, we could see stale data in block A. This function is a
  2289. * helper to solve this problem. It starts writeout of the truncated
  2290. * part in case it is in the committing transaction.
  2291. *
  2292. * Filesystem code must call this function when inode is journaled in
  2293. * ordered mode before truncation happens and after the inode has been
  2294. * placed on orphan list with the new inode size. The second condition
  2295. * avoids the race that someone writes new data and we start
  2296. * committing the transaction after this function has been called but
  2297. * before a transaction for truncate is started (and furthermore it
  2298. * allows us to optimize the case where the addition to orphan list
  2299. * happens in the same transaction as write --- we don't have to write
  2300. * any data in such case).
  2301. */
  2302. int jbd2_journal_begin_ordered_truncate(journal_t *journal,
  2303. struct jbd2_inode *jinode,
  2304. loff_t new_size)
  2305. {
  2306. transaction_t *inode_trans, *commit_trans;
  2307. int ret = 0;
  2308. /* This is a quick check to avoid locking if not necessary */
  2309. if (!jinode->i_transaction)
  2310. goto out;
  2311. /* Locks are here just to force reading of recent values, it is
  2312. * enough that the transaction was not committing before we started
  2313. * a transaction adding the inode to orphan list */
  2314. read_lock(&journal->j_state_lock);
  2315. commit_trans = journal->j_committing_transaction;
  2316. read_unlock(&journal->j_state_lock);
  2317. spin_lock(&journal->j_list_lock);
  2318. inode_trans = jinode->i_transaction;
  2319. spin_unlock(&journal->j_list_lock);
  2320. if (inode_trans == commit_trans) {
  2321. ret = filemap_fdatawrite_range(jinode->i_vfs_inode->i_mapping,
  2322. new_size, LLONG_MAX);
  2323. if (ret)
  2324. jbd2_journal_abort(journal, ret);
  2325. }
  2326. out:
  2327. return ret;
  2328. }