transaction.c 61 KB

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