transaction.c 66 KB

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