transaction.c 62 KB

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