transaction.c 71 KB

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