transaction.c 68 KB

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