transaction.c 75 KB

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