transaction.c 63 KB

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