transaction.c 61 KB

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