journal.c 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404
  1. /*
  2. * linux/fs/jbd2/journal.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 journal-writing code; part of the ext2fs
  13. * journaling system.
  14. *
  15. * This file manages journals: areas of disk reserved for logging
  16. * transactional updates. This includes the kernel journaling thread
  17. * which is responsible for scheduling updates to the log.
  18. *
  19. * We do not actually manage the physical storage of the journal in this
  20. * file: that is left to a per-journal policy function, which allows us
  21. * to store the journal within a filesystem-specified area for ext2
  22. * journaling (ext2 can use a reserved inode for storing the log).
  23. */
  24. #include <linux/module.h>
  25. #include <linux/time.h>
  26. #include <linux/fs.h>
  27. #include <linux/jbd2.h>
  28. #include <linux/errno.h>
  29. #include <linux/slab.h>
  30. #include <linux/init.h>
  31. #include <linux/mm.h>
  32. #include <linux/freezer.h>
  33. #include <linux/pagemap.h>
  34. #include <linux/kthread.h>
  35. #include <linux/poison.h>
  36. #include <linux/proc_fs.h>
  37. #include <linux/debugfs.h>
  38. #include <linux/seq_file.h>
  39. #include <linux/math64.h>
  40. #include <linux/hash.h>
  41. #include <linux/log2.h>
  42. #include <linux/vmalloc.h>
  43. #include <linux/backing-dev.h>
  44. #include <linux/bitops.h>
  45. #include <linux/ratelimit.h>
  46. #define CREATE_TRACE_POINTS
  47. #include <trace/events/jbd2.h>
  48. #include <asm/uaccess.h>
  49. #include <asm/page.h>
  50. #include <asm/system.h>
  51. EXPORT_SYMBOL(jbd2_journal_extend);
  52. EXPORT_SYMBOL(jbd2_journal_stop);
  53. EXPORT_SYMBOL(jbd2_journal_lock_updates);
  54. EXPORT_SYMBOL(jbd2_journal_unlock_updates);
  55. EXPORT_SYMBOL(jbd2_journal_get_write_access);
  56. EXPORT_SYMBOL(jbd2_journal_get_create_access);
  57. EXPORT_SYMBOL(jbd2_journal_get_undo_access);
  58. EXPORT_SYMBOL(jbd2_journal_set_triggers);
  59. EXPORT_SYMBOL(jbd2_journal_dirty_metadata);
  60. EXPORT_SYMBOL(jbd2_journal_release_buffer);
  61. EXPORT_SYMBOL(jbd2_journal_forget);
  62. #if 0
  63. EXPORT_SYMBOL(journal_sync_buffer);
  64. #endif
  65. EXPORT_SYMBOL(jbd2_journal_flush);
  66. EXPORT_SYMBOL(jbd2_journal_revoke);
  67. EXPORT_SYMBOL(jbd2_journal_init_dev);
  68. EXPORT_SYMBOL(jbd2_journal_init_inode);
  69. EXPORT_SYMBOL(jbd2_journal_update_format);
  70. EXPORT_SYMBOL(jbd2_journal_check_used_features);
  71. EXPORT_SYMBOL(jbd2_journal_check_available_features);
  72. EXPORT_SYMBOL(jbd2_journal_set_features);
  73. EXPORT_SYMBOL(jbd2_journal_load);
  74. EXPORT_SYMBOL(jbd2_journal_destroy);
  75. EXPORT_SYMBOL(jbd2_journal_abort);
  76. EXPORT_SYMBOL(jbd2_journal_errno);
  77. EXPORT_SYMBOL(jbd2_journal_ack_err);
  78. EXPORT_SYMBOL(jbd2_journal_clear_err);
  79. EXPORT_SYMBOL(jbd2_log_wait_commit);
  80. EXPORT_SYMBOL(jbd2_log_start_commit);
  81. EXPORT_SYMBOL(jbd2_journal_start_commit);
  82. EXPORT_SYMBOL(jbd2_journal_force_commit_nested);
  83. EXPORT_SYMBOL(jbd2_journal_wipe);
  84. EXPORT_SYMBOL(jbd2_journal_blocks_per_page);
  85. EXPORT_SYMBOL(jbd2_journal_invalidatepage);
  86. EXPORT_SYMBOL(jbd2_journal_try_to_free_buffers);
  87. EXPORT_SYMBOL(jbd2_journal_force_commit);
  88. EXPORT_SYMBOL(jbd2_journal_file_inode);
  89. EXPORT_SYMBOL(jbd2_journal_init_jbd_inode);
  90. EXPORT_SYMBOL(jbd2_journal_release_jbd_inode);
  91. EXPORT_SYMBOL(jbd2_journal_begin_ordered_truncate);
  92. EXPORT_SYMBOL(jbd2_inode_cache);
  93. static int journal_convert_superblock_v1(journal_t *, journal_superblock_t *);
  94. static void __journal_abort_soft (journal_t *journal, int errno);
  95. static int jbd2_journal_create_slab(size_t slab_size);
  96. /*
  97. * Helper function used to manage commit timeouts
  98. */
  99. static void commit_timeout(unsigned long __data)
  100. {
  101. struct task_struct * p = (struct task_struct *) __data;
  102. wake_up_process(p);
  103. }
  104. /*
  105. * kjournald2: The main thread function used to manage a logging device
  106. * journal.
  107. *
  108. * This kernel thread is responsible for two things:
  109. *
  110. * 1) COMMIT: Every so often we need to commit the current state of the
  111. * filesystem to disk. The journal thread is responsible for writing
  112. * all of the metadata buffers to disk.
  113. *
  114. * 2) CHECKPOINT: We cannot reuse a used section of the log file until all
  115. * of the data in that part of the log has been rewritten elsewhere on
  116. * the disk. Flushing these old buffers to reclaim space in the log is
  117. * known as checkpointing, and this thread is responsible for that job.
  118. */
  119. static int kjournald2(void *arg)
  120. {
  121. journal_t *journal = arg;
  122. transaction_t *transaction;
  123. /*
  124. * Set up an interval timer which can be used to trigger a commit wakeup
  125. * after the commit interval expires
  126. */
  127. setup_timer(&journal->j_commit_timer, commit_timeout,
  128. (unsigned long)current);
  129. /* Record that the journal thread is running */
  130. journal->j_task = current;
  131. wake_up(&journal->j_wait_done_commit);
  132. /*
  133. * And now, wait forever for commit wakeup events.
  134. */
  135. write_lock(&journal->j_state_lock);
  136. loop:
  137. if (journal->j_flags & JBD2_UNMOUNT)
  138. goto end_loop;
  139. jbd_debug(1, "commit_sequence=%d, commit_request=%d\n",
  140. journal->j_commit_sequence, journal->j_commit_request);
  141. if (journal->j_commit_sequence != journal->j_commit_request) {
  142. jbd_debug(1, "OK, requests differ\n");
  143. write_unlock(&journal->j_state_lock);
  144. del_timer_sync(&journal->j_commit_timer);
  145. jbd2_journal_commit_transaction(journal);
  146. write_lock(&journal->j_state_lock);
  147. goto loop;
  148. }
  149. wake_up(&journal->j_wait_done_commit);
  150. if (freezing(current)) {
  151. /*
  152. * The simpler the better. Flushing journal isn't a
  153. * good idea, because that depends on threads that may
  154. * be already stopped.
  155. */
  156. jbd_debug(1, "Now suspending kjournald2\n");
  157. write_unlock(&journal->j_state_lock);
  158. refrigerator();
  159. write_lock(&journal->j_state_lock);
  160. } else {
  161. /*
  162. * We assume on resume that commits are already there,
  163. * so we don't sleep
  164. */
  165. DEFINE_WAIT(wait);
  166. int should_sleep = 1;
  167. prepare_to_wait(&journal->j_wait_commit, &wait,
  168. TASK_INTERRUPTIBLE);
  169. if (journal->j_commit_sequence != journal->j_commit_request)
  170. should_sleep = 0;
  171. transaction = journal->j_running_transaction;
  172. if (transaction && time_after_eq(jiffies,
  173. transaction->t_expires))
  174. should_sleep = 0;
  175. if (journal->j_flags & JBD2_UNMOUNT)
  176. should_sleep = 0;
  177. if (should_sleep) {
  178. write_unlock(&journal->j_state_lock);
  179. schedule();
  180. write_lock(&journal->j_state_lock);
  181. }
  182. finish_wait(&journal->j_wait_commit, &wait);
  183. }
  184. jbd_debug(1, "kjournald2 wakes\n");
  185. /*
  186. * Were we woken up by a commit wakeup event?
  187. */
  188. transaction = journal->j_running_transaction;
  189. if (transaction && time_after_eq(jiffies, transaction->t_expires)) {
  190. journal->j_commit_request = transaction->t_tid;
  191. jbd_debug(1, "woke because of timeout\n");
  192. }
  193. goto loop;
  194. end_loop:
  195. write_unlock(&journal->j_state_lock);
  196. del_timer_sync(&journal->j_commit_timer);
  197. journal->j_task = NULL;
  198. wake_up(&journal->j_wait_done_commit);
  199. jbd_debug(1, "Journal thread exiting.\n");
  200. return 0;
  201. }
  202. static int jbd2_journal_start_thread(journal_t *journal)
  203. {
  204. struct task_struct *t;
  205. t = kthread_run(kjournald2, journal, "jbd2/%s",
  206. journal->j_devname);
  207. if (IS_ERR(t))
  208. return PTR_ERR(t);
  209. wait_event(journal->j_wait_done_commit, journal->j_task != NULL);
  210. return 0;
  211. }
  212. static void journal_kill_thread(journal_t *journal)
  213. {
  214. write_lock(&journal->j_state_lock);
  215. journal->j_flags |= JBD2_UNMOUNT;
  216. while (journal->j_task) {
  217. wake_up(&journal->j_wait_commit);
  218. write_unlock(&journal->j_state_lock);
  219. wait_event(journal->j_wait_done_commit, journal->j_task == NULL);
  220. write_lock(&journal->j_state_lock);
  221. }
  222. write_unlock(&journal->j_state_lock);
  223. }
  224. /*
  225. * jbd2_journal_write_metadata_buffer: write a metadata buffer to the journal.
  226. *
  227. * Writes a metadata buffer to a given disk block. The actual IO is not
  228. * performed but a new buffer_head is constructed which labels the data
  229. * to be written with the correct destination disk block.
  230. *
  231. * Any magic-number escaping which needs to be done will cause a
  232. * copy-out here. If the buffer happens to start with the
  233. * JBD2_MAGIC_NUMBER, then we can't write it to the log directly: the
  234. * magic number is only written to the log for descripter blocks. In
  235. * this case, we copy the data and replace the first word with 0, and we
  236. * return a result code which indicates that this buffer needs to be
  237. * marked as an escaped buffer in the corresponding log descriptor
  238. * block. The missing word can then be restored when the block is read
  239. * during recovery.
  240. *
  241. * If the source buffer has already been modified by a new transaction
  242. * since we took the last commit snapshot, we use the frozen copy of
  243. * that data for IO. If we end up using the existing buffer_head's data
  244. * for the write, then we *have* to lock the buffer to prevent anyone
  245. * else from using and possibly modifying it while the IO is in
  246. * progress.
  247. *
  248. * The function returns a pointer to the buffer_heads to be used for IO.
  249. *
  250. * We assume that the journal has already been locked in this function.
  251. *
  252. * Return value:
  253. * <0: Error
  254. * >=0: Finished OK
  255. *
  256. * On success:
  257. * Bit 0 set == escape performed on the data
  258. * Bit 1 set == buffer copy-out performed (kfree the data after IO)
  259. */
  260. int jbd2_journal_write_metadata_buffer(transaction_t *transaction,
  261. struct journal_head *jh_in,
  262. struct journal_head **jh_out,
  263. unsigned long long blocknr)
  264. {
  265. int need_copy_out = 0;
  266. int done_copy_out = 0;
  267. int do_escape = 0;
  268. char *mapped_data;
  269. struct buffer_head *new_bh;
  270. struct journal_head *new_jh;
  271. struct page *new_page;
  272. unsigned int new_offset;
  273. struct buffer_head *bh_in = jh2bh(jh_in);
  274. journal_t *journal = transaction->t_journal;
  275. /*
  276. * The buffer really shouldn't be locked: only the current committing
  277. * transaction is allowed to write it, so nobody else is allowed
  278. * to do any IO.
  279. *
  280. * akpm: except if we're journalling data, and write() output is
  281. * also part of a shared mapping, and another thread has
  282. * decided to launch a writepage() against this buffer.
  283. */
  284. J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in));
  285. retry_alloc:
  286. new_bh = alloc_buffer_head(GFP_NOFS);
  287. if (!new_bh) {
  288. /*
  289. * Failure is not an option, but __GFP_NOFAIL is going
  290. * away; so we retry ourselves here.
  291. */
  292. congestion_wait(BLK_RW_ASYNC, HZ/50);
  293. goto retry_alloc;
  294. }
  295. /* keep subsequent assertions sane */
  296. new_bh->b_state = 0;
  297. init_buffer(new_bh, NULL, NULL);
  298. atomic_set(&new_bh->b_count, 1);
  299. new_jh = jbd2_journal_add_journal_head(new_bh); /* This sleeps */
  300. /*
  301. * If a new transaction has already done a buffer copy-out, then
  302. * we use that version of the data for the commit.
  303. */
  304. jbd_lock_bh_state(bh_in);
  305. repeat:
  306. if (jh_in->b_frozen_data) {
  307. done_copy_out = 1;
  308. new_page = virt_to_page(jh_in->b_frozen_data);
  309. new_offset = offset_in_page(jh_in->b_frozen_data);
  310. } else {
  311. new_page = jh2bh(jh_in)->b_page;
  312. new_offset = offset_in_page(jh2bh(jh_in)->b_data);
  313. }
  314. mapped_data = kmap_atomic(new_page, KM_USER0);
  315. /*
  316. * Fire data frozen trigger if data already wasn't frozen. Do this
  317. * before checking for escaping, as the trigger may modify the magic
  318. * offset. If a copy-out happens afterwards, it will have the correct
  319. * data in the buffer.
  320. */
  321. if (!done_copy_out)
  322. jbd2_buffer_frozen_trigger(jh_in, mapped_data + new_offset,
  323. jh_in->b_triggers);
  324. /*
  325. * Check for escaping
  326. */
  327. if (*((__be32 *)(mapped_data + new_offset)) ==
  328. cpu_to_be32(JBD2_MAGIC_NUMBER)) {
  329. need_copy_out = 1;
  330. do_escape = 1;
  331. }
  332. kunmap_atomic(mapped_data, KM_USER0);
  333. /*
  334. * Do we need to do a data copy?
  335. */
  336. if (need_copy_out && !done_copy_out) {
  337. char *tmp;
  338. jbd_unlock_bh_state(bh_in);
  339. tmp = jbd2_alloc(bh_in->b_size, GFP_NOFS);
  340. if (!tmp) {
  341. jbd2_journal_put_journal_head(new_jh);
  342. return -ENOMEM;
  343. }
  344. jbd_lock_bh_state(bh_in);
  345. if (jh_in->b_frozen_data) {
  346. jbd2_free(tmp, bh_in->b_size);
  347. goto repeat;
  348. }
  349. jh_in->b_frozen_data = tmp;
  350. mapped_data = kmap_atomic(new_page, KM_USER0);
  351. memcpy(tmp, mapped_data + new_offset, jh2bh(jh_in)->b_size);
  352. kunmap_atomic(mapped_data, KM_USER0);
  353. new_page = virt_to_page(tmp);
  354. new_offset = offset_in_page(tmp);
  355. done_copy_out = 1;
  356. /*
  357. * This isn't strictly necessary, as we're using frozen
  358. * data for the escaping, but it keeps consistency with
  359. * b_frozen_data usage.
  360. */
  361. jh_in->b_frozen_triggers = jh_in->b_triggers;
  362. }
  363. /*
  364. * Did we need to do an escaping? Now we've done all the
  365. * copying, we can finally do so.
  366. */
  367. if (do_escape) {
  368. mapped_data = kmap_atomic(new_page, KM_USER0);
  369. *((unsigned int *)(mapped_data + new_offset)) = 0;
  370. kunmap_atomic(mapped_data, KM_USER0);
  371. }
  372. set_bh_page(new_bh, new_page, new_offset);
  373. new_jh->b_transaction = NULL;
  374. new_bh->b_size = jh2bh(jh_in)->b_size;
  375. new_bh->b_bdev = transaction->t_journal->j_dev;
  376. new_bh->b_blocknr = blocknr;
  377. set_buffer_mapped(new_bh);
  378. set_buffer_dirty(new_bh);
  379. *jh_out = new_jh;
  380. /*
  381. * The to-be-written buffer needs to get moved to the io queue,
  382. * and the original buffer whose contents we are shadowing or
  383. * copying is moved to the transaction's shadow queue.
  384. */
  385. JBUFFER_TRACE(jh_in, "file as BJ_Shadow");
  386. spin_lock(&journal->j_list_lock);
  387. __jbd2_journal_file_buffer(jh_in, transaction, BJ_Shadow);
  388. spin_unlock(&journal->j_list_lock);
  389. jbd_unlock_bh_state(bh_in);
  390. JBUFFER_TRACE(new_jh, "file as BJ_IO");
  391. jbd2_journal_file_buffer(new_jh, transaction, BJ_IO);
  392. return do_escape | (done_copy_out << 1);
  393. }
  394. /*
  395. * Allocation code for the journal file. Manage the space left in the
  396. * journal, so that we can begin checkpointing when appropriate.
  397. */
  398. /*
  399. * __jbd2_log_space_left: Return the number of free blocks left in the journal.
  400. *
  401. * Called with the journal already locked.
  402. *
  403. * Called under j_state_lock
  404. */
  405. int __jbd2_log_space_left(journal_t *journal)
  406. {
  407. int left = journal->j_free;
  408. /* assert_spin_locked(&journal->j_state_lock); */
  409. /*
  410. * Be pessimistic here about the number of those free blocks which
  411. * might be required for log descriptor control blocks.
  412. */
  413. #define MIN_LOG_RESERVED_BLOCKS 32 /* Allow for rounding errors */
  414. left -= MIN_LOG_RESERVED_BLOCKS;
  415. if (left <= 0)
  416. return 0;
  417. left -= (left >> 3);
  418. return left;
  419. }
  420. /*
  421. * Called with j_state_lock locked for writing.
  422. * Returns true if a transaction commit was started.
  423. */
  424. int __jbd2_log_start_commit(journal_t *journal, tid_t target)
  425. {
  426. /*
  427. * The only transaction we can possibly wait upon is the
  428. * currently running transaction (if it exists). Otherwise,
  429. * the target tid must be an old one.
  430. */
  431. if (journal->j_running_transaction &&
  432. journal->j_running_transaction->t_tid == target) {
  433. /*
  434. * We want a new commit: OK, mark the request and wakeup the
  435. * commit thread. We do _not_ do the commit ourselves.
  436. */
  437. journal->j_commit_request = target;
  438. jbd_debug(1, "JBD2: requesting commit %d/%d\n",
  439. journal->j_commit_request,
  440. journal->j_commit_sequence);
  441. wake_up(&journal->j_wait_commit);
  442. return 1;
  443. } else if (!tid_geq(journal->j_commit_request, target))
  444. /* This should never happen, but if it does, preserve
  445. the evidence before kjournald goes into a loop and
  446. increments j_commit_sequence beyond all recognition. */
  447. WARN_ONCE(1, "JBD2: bad log_start_commit: %u %u %u %u\n",
  448. journal->j_commit_request,
  449. journal->j_commit_sequence,
  450. target, journal->j_running_transaction ?
  451. journal->j_running_transaction->t_tid : 0);
  452. return 0;
  453. }
  454. int jbd2_log_start_commit(journal_t *journal, tid_t tid)
  455. {
  456. int ret;
  457. write_lock(&journal->j_state_lock);
  458. ret = __jbd2_log_start_commit(journal, tid);
  459. write_unlock(&journal->j_state_lock);
  460. return ret;
  461. }
  462. /*
  463. * Force and wait upon a commit if the calling process is not within
  464. * transaction. This is used for forcing out undo-protected data which contains
  465. * bitmaps, when the fs is running out of space.
  466. *
  467. * We can only force the running transaction if we don't have an active handle;
  468. * otherwise, we will deadlock.
  469. *
  470. * Returns true if a transaction was started.
  471. */
  472. int jbd2_journal_force_commit_nested(journal_t *journal)
  473. {
  474. transaction_t *transaction = NULL;
  475. tid_t tid;
  476. int need_to_start = 0;
  477. read_lock(&journal->j_state_lock);
  478. if (journal->j_running_transaction && !current->journal_info) {
  479. transaction = journal->j_running_transaction;
  480. if (!tid_geq(journal->j_commit_request, transaction->t_tid))
  481. need_to_start = 1;
  482. } else if (journal->j_committing_transaction)
  483. transaction = journal->j_committing_transaction;
  484. if (!transaction) {
  485. read_unlock(&journal->j_state_lock);
  486. return 0; /* Nothing to retry */
  487. }
  488. tid = transaction->t_tid;
  489. read_unlock(&journal->j_state_lock);
  490. if (need_to_start)
  491. jbd2_log_start_commit(journal, tid);
  492. jbd2_log_wait_commit(journal, tid);
  493. return 1;
  494. }
  495. /*
  496. * Start a commit of the current running transaction (if any). Returns true
  497. * if a transaction is going to be committed (or is currently already
  498. * committing), and fills its tid in at *ptid
  499. */
  500. int jbd2_journal_start_commit(journal_t *journal, tid_t *ptid)
  501. {
  502. int ret = 0;
  503. write_lock(&journal->j_state_lock);
  504. if (journal->j_running_transaction) {
  505. tid_t tid = journal->j_running_transaction->t_tid;
  506. __jbd2_log_start_commit(journal, tid);
  507. /* There's a running transaction and we've just made sure
  508. * it's commit has been scheduled. */
  509. if (ptid)
  510. *ptid = tid;
  511. ret = 1;
  512. } else if (journal->j_committing_transaction) {
  513. /*
  514. * If ext3_write_super() recently started a commit, then we
  515. * have to wait for completion of that transaction
  516. */
  517. if (ptid)
  518. *ptid = journal->j_committing_transaction->t_tid;
  519. ret = 1;
  520. }
  521. write_unlock(&journal->j_state_lock);
  522. return ret;
  523. }
  524. /*
  525. * Return 1 if a given transaction has not yet sent barrier request
  526. * connected with a transaction commit. If 0 is returned, transaction
  527. * may or may not have sent the barrier. Used to avoid sending barrier
  528. * twice in common cases.
  529. */
  530. int jbd2_trans_will_send_data_barrier(journal_t *journal, tid_t tid)
  531. {
  532. int ret = 0;
  533. transaction_t *commit_trans;
  534. if (!(journal->j_flags & JBD2_BARRIER))
  535. return 0;
  536. read_lock(&journal->j_state_lock);
  537. /* Transaction already committed? */
  538. if (tid_geq(journal->j_commit_sequence, tid))
  539. goto out;
  540. commit_trans = journal->j_committing_transaction;
  541. if (!commit_trans || commit_trans->t_tid != tid) {
  542. ret = 1;
  543. goto out;
  544. }
  545. /*
  546. * Transaction is being committed and we already proceeded to
  547. * submitting a flush to fs partition?
  548. */
  549. if (journal->j_fs_dev != journal->j_dev) {
  550. if (!commit_trans->t_need_data_flush ||
  551. commit_trans->t_state >= T_COMMIT_DFLUSH)
  552. goto out;
  553. } else {
  554. if (commit_trans->t_state >= T_COMMIT_JFLUSH)
  555. goto out;
  556. }
  557. ret = 1;
  558. out:
  559. read_unlock(&journal->j_state_lock);
  560. return ret;
  561. }
  562. EXPORT_SYMBOL(jbd2_trans_will_send_data_barrier);
  563. /*
  564. * Wait for a specified commit to complete.
  565. * The caller may not hold the journal lock.
  566. */
  567. int jbd2_log_wait_commit(journal_t *journal, tid_t tid)
  568. {
  569. int err = 0;
  570. read_lock(&journal->j_state_lock);
  571. #ifdef CONFIG_JBD2_DEBUG
  572. if (!tid_geq(journal->j_commit_request, tid)) {
  573. printk(KERN_EMERG
  574. "%s: error: j_commit_request=%d, tid=%d\n",
  575. __func__, journal->j_commit_request, tid);
  576. }
  577. #endif
  578. while (tid_gt(tid, journal->j_commit_sequence)) {
  579. jbd_debug(1, "JBD2: want %d, j_commit_sequence=%d\n",
  580. tid, journal->j_commit_sequence);
  581. wake_up(&journal->j_wait_commit);
  582. read_unlock(&journal->j_state_lock);
  583. wait_event(journal->j_wait_done_commit,
  584. !tid_gt(tid, journal->j_commit_sequence));
  585. read_lock(&journal->j_state_lock);
  586. }
  587. read_unlock(&journal->j_state_lock);
  588. if (unlikely(is_journal_aborted(journal))) {
  589. printk(KERN_EMERG "journal commit I/O error\n");
  590. err = -EIO;
  591. }
  592. return err;
  593. }
  594. /*
  595. * Log buffer allocation routines:
  596. */
  597. int jbd2_journal_next_log_block(journal_t *journal, unsigned long long *retp)
  598. {
  599. unsigned long blocknr;
  600. write_lock(&journal->j_state_lock);
  601. J_ASSERT(journal->j_free > 1);
  602. blocknr = journal->j_head;
  603. journal->j_head++;
  604. journal->j_free--;
  605. if (journal->j_head == journal->j_last)
  606. journal->j_head = journal->j_first;
  607. write_unlock(&journal->j_state_lock);
  608. return jbd2_journal_bmap(journal, blocknr, retp);
  609. }
  610. /*
  611. * Conversion of logical to physical block numbers for the journal
  612. *
  613. * On external journals the journal blocks are identity-mapped, so
  614. * this is a no-op. If needed, we can use j_blk_offset - everything is
  615. * ready.
  616. */
  617. int jbd2_journal_bmap(journal_t *journal, unsigned long blocknr,
  618. unsigned long long *retp)
  619. {
  620. int err = 0;
  621. unsigned long long ret;
  622. if (journal->j_inode) {
  623. ret = bmap(journal->j_inode, blocknr);
  624. if (ret)
  625. *retp = ret;
  626. else {
  627. printk(KERN_ALERT "%s: journal block not found "
  628. "at offset %lu on %s\n",
  629. __func__, blocknr, journal->j_devname);
  630. err = -EIO;
  631. __journal_abort_soft(journal, err);
  632. }
  633. } else {
  634. *retp = blocknr; /* +journal->j_blk_offset */
  635. }
  636. return err;
  637. }
  638. /*
  639. * We play buffer_head aliasing tricks to write data/metadata blocks to
  640. * the journal without copying their contents, but for journal
  641. * descriptor blocks we do need to generate bona fide buffers.
  642. *
  643. * After the caller of jbd2_journal_get_descriptor_buffer() has finished modifying
  644. * the buffer's contents they really should run flush_dcache_page(bh->b_page).
  645. * But we don't bother doing that, so there will be coherency problems with
  646. * mmaps of blockdevs which hold live JBD-controlled filesystems.
  647. */
  648. struct journal_head *jbd2_journal_get_descriptor_buffer(journal_t *journal)
  649. {
  650. struct buffer_head *bh;
  651. unsigned long long blocknr;
  652. int err;
  653. err = jbd2_journal_next_log_block(journal, &blocknr);
  654. if (err)
  655. return NULL;
  656. bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
  657. if (!bh)
  658. return NULL;
  659. lock_buffer(bh);
  660. memset(bh->b_data, 0, journal->j_blocksize);
  661. set_buffer_uptodate(bh);
  662. unlock_buffer(bh);
  663. BUFFER_TRACE(bh, "return this buffer");
  664. return jbd2_journal_add_journal_head(bh);
  665. }
  666. struct jbd2_stats_proc_session {
  667. journal_t *journal;
  668. struct transaction_stats_s *stats;
  669. int start;
  670. int max;
  671. };
  672. static void *jbd2_seq_info_start(struct seq_file *seq, loff_t *pos)
  673. {
  674. return *pos ? NULL : SEQ_START_TOKEN;
  675. }
  676. static void *jbd2_seq_info_next(struct seq_file *seq, void *v, loff_t *pos)
  677. {
  678. return NULL;
  679. }
  680. static int jbd2_seq_info_show(struct seq_file *seq, void *v)
  681. {
  682. struct jbd2_stats_proc_session *s = seq->private;
  683. if (v != SEQ_START_TOKEN)
  684. return 0;
  685. seq_printf(seq, "%lu transaction, each up to %u blocks\n",
  686. s->stats->ts_tid,
  687. s->journal->j_max_transaction_buffers);
  688. if (s->stats->ts_tid == 0)
  689. return 0;
  690. seq_printf(seq, "average: \n %ums waiting for transaction\n",
  691. jiffies_to_msecs(s->stats->run.rs_wait / s->stats->ts_tid));
  692. seq_printf(seq, " %ums running transaction\n",
  693. jiffies_to_msecs(s->stats->run.rs_running / s->stats->ts_tid));
  694. seq_printf(seq, " %ums transaction was being locked\n",
  695. jiffies_to_msecs(s->stats->run.rs_locked / s->stats->ts_tid));
  696. seq_printf(seq, " %ums flushing data (in ordered mode)\n",
  697. jiffies_to_msecs(s->stats->run.rs_flushing / s->stats->ts_tid));
  698. seq_printf(seq, " %ums logging transaction\n",
  699. jiffies_to_msecs(s->stats->run.rs_logging / s->stats->ts_tid));
  700. seq_printf(seq, " %lluus average transaction commit time\n",
  701. div_u64(s->journal->j_average_commit_time, 1000));
  702. seq_printf(seq, " %lu handles per transaction\n",
  703. s->stats->run.rs_handle_count / s->stats->ts_tid);
  704. seq_printf(seq, " %lu blocks per transaction\n",
  705. s->stats->run.rs_blocks / s->stats->ts_tid);
  706. seq_printf(seq, " %lu logged blocks per transaction\n",
  707. s->stats->run.rs_blocks_logged / s->stats->ts_tid);
  708. return 0;
  709. }
  710. static void jbd2_seq_info_stop(struct seq_file *seq, void *v)
  711. {
  712. }
  713. static const struct seq_operations jbd2_seq_info_ops = {
  714. .start = jbd2_seq_info_start,
  715. .next = jbd2_seq_info_next,
  716. .stop = jbd2_seq_info_stop,
  717. .show = jbd2_seq_info_show,
  718. };
  719. static int jbd2_seq_info_open(struct inode *inode, struct file *file)
  720. {
  721. journal_t *journal = PDE(inode)->data;
  722. struct jbd2_stats_proc_session *s;
  723. int rc, size;
  724. s = kmalloc(sizeof(*s), GFP_KERNEL);
  725. if (s == NULL)
  726. return -ENOMEM;
  727. size = sizeof(struct transaction_stats_s);
  728. s->stats = kmalloc(size, GFP_KERNEL);
  729. if (s->stats == NULL) {
  730. kfree(s);
  731. return -ENOMEM;
  732. }
  733. spin_lock(&journal->j_history_lock);
  734. memcpy(s->stats, &journal->j_stats, size);
  735. s->journal = journal;
  736. spin_unlock(&journal->j_history_lock);
  737. rc = seq_open(file, &jbd2_seq_info_ops);
  738. if (rc == 0) {
  739. struct seq_file *m = file->private_data;
  740. m->private = s;
  741. } else {
  742. kfree(s->stats);
  743. kfree(s);
  744. }
  745. return rc;
  746. }
  747. static int jbd2_seq_info_release(struct inode *inode, struct file *file)
  748. {
  749. struct seq_file *seq = file->private_data;
  750. struct jbd2_stats_proc_session *s = seq->private;
  751. kfree(s->stats);
  752. kfree(s);
  753. return seq_release(inode, file);
  754. }
  755. static const struct file_operations jbd2_seq_info_fops = {
  756. .owner = THIS_MODULE,
  757. .open = jbd2_seq_info_open,
  758. .read = seq_read,
  759. .llseek = seq_lseek,
  760. .release = jbd2_seq_info_release,
  761. };
  762. static struct proc_dir_entry *proc_jbd2_stats;
  763. static void jbd2_stats_proc_init(journal_t *journal)
  764. {
  765. journal->j_proc_entry = proc_mkdir(journal->j_devname, proc_jbd2_stats);
  766. if (journal->j_proc_entry) {
  767. proc_create_data("info", S_IRUGO, journal->j_proc_entry,
  768. &jbd2_seq_info_fops, journal);
  769. }
  770. }
  771. static void jbd2_stats_proc_exit(journal_t *journal)
  772. {
  773. remove_proc_entry("info", journal->j_proc_entry);
  774. remove_proc_entry(journal->j_devname, proc_jbd2_stats);
  775. }
  776. /*
  777. * Management for journal control blocks: functions to create and
  778. * destroy journal_t structures, and to initialise and read existing
  779. * journal blocks from disk. */
  780. /* First: create and setup a journal_t object in memory. We initialise
  781. * very few fields yet: that has to wait until we have created the
  782. * journal structures from from scratch, or loaded them from disk. */
  783. static journal_t * journal_init_common (void)
  784. {
  785. journal_t *journal;
  786. int err;
  787. journal = kzalloc(sizeof(*journal), GFP_KERNEL);
  788. if (!journal)
  789. return NULL;
  790. init_waitqueue_head(&journal->j_wait_transaction_locked);
  791. init_waitqueue_head(&journal->j_wait_logspace);
  792. init_waitqueue_head(&journal->j_wait_done_commit);
  793. init_waitqueue_head(&journal->j_wait_checkpoint);
  794. init_waitqueue_head(&journal->j_wait_commit);
  795. init_waitqueue_head(&journal->j_wait_updates);
  796. mutex_init(&journal->j_barrier);
  797. mutex_init(&journal->j_checkpoint_mutex);
  798. spin_lock_init(&journal->j_revoke_lock);
  799. spin_lock_init(&journal->j_list_lock);
  800. rwlock_init(&journal->j_state_lock);
  801. journal->j_commit_interval = (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE);
  802. journal->j_min_batch_time = 0;
  803. journal->j_max_batch_time = 15000; /* 15ms */
  804. /* The journal is marked for error until we succeed with recovery! */
  805. journal->j_flags = JBD2_ABORT;
  806. /* Set up a default-sized revoke table for the new mount. */
  807. err = jbd2_journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
  808. if (err) {
  809. kfree(journal);
  810. return NULL;
  811. }
  812. spin_lock_init(&journal->j_history_lock);
  813. return journal;
  814. }
  815. /* jbd2_journal_init_dev and jbd2_journal_init_inode:
  816. *
  817. * Create a journal structure assigned some fixed set of disk blocks to
  818. * the journal. We don't actually touch those disk blocks yet, but we
  819. * need to set up all of the mapping information to tell the journaling
  820. * system where the journal blocks are.
  821. *
  822. */
  823. /**
  824. * journal_t * jbd2_journal_init_dev() - creates and initialises a journal structure
  825. * @bdev: Block device on which to create the journal
  826. * @fs_dev: Device which hold journalled filesystem for this journal.
  827. * @start: Block nr Start of journal.
  828. * @len: Length of the journal in blocks.
  829. * @blocksize: blocksize of journalling device
  830. *
  831. * Returns: a newly created journal_t *
  832. *
  833. * jbd2_journal_init_dev creates a journal which maps a fixed contiguous
  834. * range of blocks on an arbitrary block device.
  835. *
  836. */
  837. journal_t * jbd2_journal_init_dev(struct block_device *bdev,
  838. struct block_device *fs_dev,
  839. unsigned long long start, int len, int blocksize)
  840. {
  841. journal_t *journal = journal_init_common();
  842. struct buffer_head *bh;
  843. char *p;
  844. int n;
  845. if (!journal)
  846. return NULL;
  847. /* journal descriptor can store up to n blocks -bzzz */
  848. journal->j_blocksize = blocksize;
  849. journal->j_dev = bdev;
  850. journal->j_fs_dev = fs_dev;
  851. journal->j_blk_offset = start;
  852. journal->j_maxlen = len;
  853. bdevname(journal->j_dev, journal->j_devname);
  854. p = journal->j_devname;
  855. while ((p = strchr(p, '/')))
  856. *p = '!';
  857. jbd2_stats_proc_init(journal);
  858. n = journal->j_blocksize / sizeof(journal_block_tag_t);
  859. journal->j_wbufsize = n;
  860. journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
  861. if (!journal->j_wbuf) {
  862. printk(KERN_ERR "%s: Can't allocate bhs for commit thread\n",
  863. __func__);
  864. goto out_err;
  865. }
  866. bh = __getblk(journal->j_dev, start, journal->j_blocksize);
  867. if (!bh) {
  868. printk(KERN_ERR
  869. "%s: Cannot get buffer for journal superblock\n",
  870. __func__);
  871. goto out_err;
  872. }
  873. journal->j_sb_buffer = bh;
  874. journal->j_superblock = (journal_superblock_t *)bh->b_data;
  875. return journal;
  876. out_err:
  877. kfree(journal->j_wbuf);
  878. jbd2_stats_proc_exit(journal);
  879. kfree(journal);
  880. return NULL;
  881. }
  882. /**
  883. * journal_t * jbd2_journal_init_inode () - creates a journal which maps to a inode.
  884. * @inode: An inode to create the journal in
  885. *
  886. * jbd2_journal_init_inode creates a journal which maps an on-disk inode as
  887. * the journal. The inode must exist already, must support bmap() and
  888. * must have all data blocks preallocated.
  889. */
  890. journal_t * jbd2_journal_init_inode (struct inode *inode)
  891. {
  892. struct buffer_head *bh;
  893. journal_t *journal = journal_init_common();
  894. char *p;
  895. int err;
  896. int n;
  897. unsigned long long blocknr;
  898. if (!journal)
  899. return NULL;
  900. journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev;
  901. journal->j_inode = inode;
  902. bdevname(journal->j_dev, journal->j_devname);
  903. p = journal->j_devname;
  904. while ((p = strchr(p, '/')))
  905. *p = '!';
  906. p = journal->j_devname + strlen(journal->j_devname);
  907. sprintf(p, "-%lu", journal->j_inode->i_ino);
  908. jbd_debug(1,
  909. "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
  910. journal, inode->i_sb->s_id, inode->i_ino,
  911. (long long) inode->i_size,
  912. inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
  913. journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits;
  914. journal->j_blocksize = inode->i_sb->s_blocksize;
  915. jbd2_stats_proc_init(journal);
  916. /* journal descriptor can store up to n blocks -bzzz */
  917. n = journal->j_blocksize / sizeof(journal_block_tag_t);
  918. journal->j_wbufsize = n;
  919. journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
  920. if (!journal->j_wbuf) {
  921. printk(KERN_ERR "%s: Can't allocate bhs for commit thread\n",
  922. __func__);
  923. goto out_err;
  924. }
  925. err = jbd2_journal_bmap(journal, 0, &blocknr);
  926. /* If that failed, give up */
  927. if (err) {
  928. printk(KERN_ERR "%s: Cannot locate journal superblock\n",
  929. __func__);
  930. goto out_err;
  931. }
  932. bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
  933. if (!bh) {
  934. printk(KERN_ERR
  935. "%s: Cannot get buffer for journal superblock\n",
  936. __func__);
  937. goto out_err;
  938. }
  939. journal->j_sb_buffer = bh;
  940. journal->j_superblock = (journal_superblock_t *)bh->b_data;
  941. return journal;
  942. out_err:
  943. kfree(journal->j_wbuf);
  944. jbd2_stats_proc_exit(journal);
  945. kfree(journal);
  946. return NULL;
  947. }
  948. /*
  949. * If the journal init or create aborts, we need to mark the journal
  950. * superblock as being NULL to prevent the journal destroy from writing
  951. * back a bogus superblock.
  952. */
  953. static void journal_fail_superblock (journal_t *journal)
  954. {
  955. struct buffer_head *bh = journal->j_sb_buffer;
  956. brelse(bh);
  957. journal->j_sb_buffer = NULL;
  958. }
  959. /*
  960. * Given a journal_t structure, initialise the various fields for
  961. * startup of a new journaling session. We use this both when creating
  962. * a journal, and after recovering an old journal to reset it for
  963. * subsequent use.
  964. */
  965. static int journal_reset(journal_t *journal)
  966. {
  967. journal_superblock_t *sb = journal->j_superblock;
  968. unsigned long long first, last;
  969. first = be32_to_cpu(sb->s_first);
  970. last = be32_to_cpu(sb->s_maxlen);
  971. if (first + JBD2_MIN_JOURNAL_BLOCKS > last + 1) {
  972. printk(KERN_ERR "JBD2: Journal too short (blocks %llu-%llu).\n",
  973. first, last);
  974. journal_fail_superblock(journal);
  975. return -EINVAL;
  976. }
  977. journal->j_first = first;
  978. journal->j_last = last;
  979. journal->j_head = first;
  980. journal->j_tail = first;
  981. journal->j_free = last - first;
  982. journal->j_tail_sequence = journal->j_transaction_sequence;
  983. journal->j_commit_sequence = journal->j_transaction_sequence - 1;
  984. journal->j_commit_request = journal->j_commit_sequence;
  985. journal->j_max_transaction_buffers = journal->j_maxlen / 4;
  986. /* Add the dynamic fields and write it to disk. */
  987. jbd2_journal_update_superblock(journal, 1);
  988. return jbd2_journal_start_thread(journal);
  989. }
  990. /**
  991. * void jbd2_journal_update_superblock() - Update journal sb on disk.
  992. * @journal: The journal to update.
  993. * @wait: Set to '0' if you don't want to wait for IO completion.
  994. *
  995. * Update a journal's dynamic superblock fields and write it to disk,
  996. * optionally waiting for the IO to complete.
  997. */
  998. void jbd2_journal_update_superblock(journal_t *journal, int wait)
  999. {
  1000. journal_superblock_t *sb = journal->j_superblock;
  1001. struct buffer_head *bh = journal->j_sb_buffer;
  1002. /*
  1003. * As a special case, if the on-disk copy is already marked as needing
  1004. * no recovery (s_start == 0) and there are no outstanding transactions
  1005. * in the filesystem, then we can safely defer the superblock update
  1006. * until the next commit by setting JBD2_FLUSHED. This avoids
  1007. * attempting a write to a potential-readonly device.
  1008. */
  1009. if (sb->s_start == 0 && journal->j_tail_sequence ==
  1010. journal->j_transaction_sequence) {
  1011. jbd_debug(1, "JBD2: Skipping superblock update on recovered sb "
  1012. "(start %ld, seq %d, errno %d)\n",
  1013. journal->j_tail, journal->j_tail_sequence,
  1014. journal->j_errno);
  1015. goto out;
  1016. }
  1017. if (buffer_write_io_error(bh)) {
  1018. /*
  1019. * Oh, dear. A previous attempt to write the journal
  1020. * superblock failed. This could happen because the
  1021. * USB device was yanked out. Or it could happen to
  1022. * be a transient write error and maybe the block will
  1023. * be remapped. Nothing we can do but to retry the
  1024. * write and hope for the best.
  1025. */
  1026. printk(KERN_ERR "JBD2: previous I/O error detected "
  1027. "for journal superblock update for %s.\n",
  1028. journal->j_devname);
  1029. clear_buffer_write_io_error(bh);
  1030. set_buffer_uptodate(bh);
  1031. }
  1032. read_lock(&journal->j_state_lock);
  1033. jbd_debug(1, "JBD2: updating superblock (start %ld, seq %d, errno %d)\n",
  1034. journal->j_tail, journal->j_tail_sequence, journal->j_errno);
  1035. sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
  1036. sb->s_start = cpu_to_be32(journal->j_tail);
  1037. sb->s_errno = cpu_to_be32(journal->j_errno);
  1038. read_unlock(&journal->j_state_lock);
  1039. BUFFER_TRACE(bh, "marking dirty");
  1040. mark_buffer_dirty(bh);
  1041. if (wait) {
  1042. sync_dirty_buffer(bh);
  1043. if (buffer_write_io_error(bh)) {
  1044. printk(KERN_ERR "JBD2: I/O error detected "
  1045. "when updating journal superblock for %s.\n",
  1046. journal->j_devname);
  1047. clear_buffer_write_io_error(bh);
  1048. set_buffer_uptodate(bh);
  1049. }
  1050. } else
  1051. write_dirty_buffer(bh, WRITE);
  1052. out:
  1053. /* If we have just flushed the log (by marking s_start==0), then
  1054. * any future commit will have to be careful to update the
  1055. * superblock again to re-record the true start of the log. */
  1056. write_lock(&journal->j_state_lock);
  1057. if (sb->s_start)
  1058. journal->j_flags &= ~JBD2_FLUSHED;
  1059. else
  1060. journal->j_flags |= JBD2_FLUSHED;
  1061. write_unlock(&journal->j_state_lock);
  1062. }
  1063. /*
  1064. * Read the superblock for a given journal, performing initial
  1065. * validation of the format.
  1066. */
  1067. static int journal_get_superblock(journal_t *journal)
  1068. {
  1069. struct buffer_head *bh;
  1070. journal_superblock_t *sb;
  1071. int err = -EIO;
  1072. bh = journal->j_sb_buffer;
  1073. J_ASSERT(bh != NULL);
  1074. if (!buffer_uptodate(bh)) {
  1075. ll_rw_block(READ, 1, &bh);
  1076. wait_on_buffer(bh);
  1077. if (!buffer_uptodate(bh)) {
  1078. printk(KERN_ERR
  1079. "JBD2: IO error reading journal superblock\n");
  1080. goto out;
  1081. }
  1082. }
  1083. sb = journal->j_superblock;
  1084. err = -EINVAL;
  1085. if (sb->s_header.h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER) ||
  1086. sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
  1087. printk(KERN_WARNING "JBD2: no valid journal superblock found\n");
  1088. goto out;
  1089. }
  1090. switch(be32_to_cpu(sb->s_header.h_blocktype)) {
  1091. case JBD2_SUPERBLOCK_V1:
  1092. journal->j_format_version = 1;
  1093. break;
  1094. case JBD2_SUPERBLOCK_V2:
  1095. journal->j_format_version = 2;
  1096. break;
  1097. default:
  1098. printk(KERN_WARNING "JBD2: unrecognised superblock format ID\n");
  1099. goto out;
  1100. }
  1101. if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen)
  1102. journal->j_maxlen = be32_to_cpu(sb->s_maxlen);
  1103. else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) {
  1104. printk(KERN_WARNING "JBD2: journal file too short\n");
  1105. goto out;
  1106. }
  1107. if (be32_to_cpu(sb->s_first) == 0 ||
  1108. be32_to_cpu(sb->s_first) >= journal->j_maxlen) {
  1109. printk(KERN_WARNING
  1110. "JBD2: Invalid start block of journal: %u\n",
  1111. be32_to_cpu(sb->s_first));
  1112. goto out;
  1113. }
  1114. return 0;
  1115. out:
  1116. journal_fail_superblock(journal);
  1117. return err;
  1118. }
  1119. /*
  1120. * Load the on-disk journal superblock and read the key fields into the
  1121. * journal_t.
  1122. */
  1123. static int load_superblock(journal_t *journal)
  1124. {
  1125. int err;
  1126. journal_superblock_t *sb;
  1127. err = journal_get_superblock(journal);
  1128. if (err)
  1129. return err;
  1130. sb = journal->j_superblock;
  1131. journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
  1132. journal->j_tail = be32_to_cpu(sb->s_start);
  1133. journal->j_first = be32_to_cpu(sb->s_first);
  1134. journal->j_last = be32_to_cpu(sb->s_maxlen);
  1135. journal->j_errno = be32_to_cpu(sb->s_errno);
  1136. return 0;
  1137. }
  1138. /**
  1139. * int jbd2_journal_load() - Read journal from disk.
  1140. * @journal: Journal to act on.
  1141. *
  1142. * Given a journal_t structure which tells us which disk blocks contain
  1143. * a journal, read the journal from disk to initialise the in-memory
  1144. * structures.
  1145. */
  1146. int jbd2_journal_load(journal_t *journal)
  1147. {
  1148. int err;
  1149. journal_superblock_t *sb;
  1150. err = load_superblock(journal);
  1151. if (err)
  1152. return err;
  1153. sb = journal->j_superblock;
  1154. /* If this is a V2 superblock, then we have to check the
  1155. * features flags on it. */
  1156. if (journal->j_format_version >= 2) {
  1157. if ((sb->s_feature_ro_compat &
  1158. ~cpu_to_be32(JBD2_KNOWN_ROCOMPAT_FEATURES)) ||
  1159. (sb->s_feature_incompat &
  1160. ~cpu_to_be32(JBD2_KNOWN_INCOMPAT_FEATURES))) {
  1161. printk(KERN_WARNING
  1162. "JBD2: Unrecognised features on journal\n");
  1163. return -EINVAL;
  1164. }
  1165. }
  1166. /*
  1167. * Create a slab for this blocksize
  1168. */
  1169. err = jbd2_journal_create_slab(be32_to_cpu(sb->s_blocksize));
  1170. if (err)
  1171. return err;
  1172. /* Let the recovery code check whether it needs to recover any
  1173. * data from the journal. */
  1174. if (jbd2_journal_recover(journal))
  1175. goto recovery_error;
  1176. if (journal->j_failed_commit) {
  1177. printk(KERN_ERR "JBD2: journal transaction %u on %s "
  1178. "is corrupt.\n", journal->j_failed_commit,
  1179. journal->j_devname);
  1180. return -EIO;
  1181. }
  1182. /* OK, we've finished with the dynamic journal bits:
  1183. * reinitialise the dynamic contents of the superblock in memory
  1184. * and reset them on disk. */
  1185. if (journal_reset(journal))
  1186. goto recovery_error;
  1187. journal->j_flags &= ~JBD2_ABORT;
  1188. journal->j_flags |= JBD2_LOADED;
  1189. return 0;
  1190. recovery_error:
  1191. printk(KERN_WARNING "JBD2: recovery failed\n");
  1192. return -EIO;
  1193. }
  1194. /**
  1195. * void jbd2_journal_destroy() - Release a journal_t structure.
  1196. * @journal: Journal to act on.
  1197. *
  1198. * Release a journal_t structure once it is no longer in use by the
  1199. * journaled object.
  1200. * Return <0 if we couldn't clean up the journal.
  1201. */
  1202. int jbd2_journal_destroy(journal_t *journal)
  1203. {
  1204. int err = 0;
  1205. /* Wait for the commit thread to wake up and die. */
  1206. journal_kill_thread(journal);
  1207. /* Force a final log commit */
  1208. if (journal->j_running_transaction)
  1209. jbd2_journal_commit_transaction(journal);
  1210. /* Force any old transactions to disk */
  1211. /* Totally anal locking here... */
  1212. spin_lock(&journal->j_list_lock);
  1213. while (journal->j_checkpoint_transactions != NULL) {
  1214. spin_unlock(&journal->j_list_lock);
  1215. mutex_lock(&journal->j_checkpoint_mutex);
  1216. jbd2_log_do_checkpoint(journal);
  1217. mutex_unlock(&journal->j_checkpoint_mutex);
  1218. spin_lock(&journal->j_list_lock);
  1219. }
  1220. J_ASSERT(journal->j_running_transaction == NULL);
  1221. J_ASSERT(journal->j_committing_transaction == NULL);
  1222. J_ASSERT(journal->j_checkpoint_transactions == NULL);
  1223. spin_unlock(&journal->j_list_lock);
  1224. if (journal->j_sb_buffer) {
  1225. if (!is_journal_aborted(journal)) {
  1226. /* We can now mark the journal as empty. */
  1227. journal->j_tail = 0;
  1228. journal->j_tail_sequence =
  1229. ++journal->j_transaction_sequence;
  1230. jbd2_journal_update_superblock(journal, 1);
  1231. } else {
  1232. err = -EIO;
  1233. }
  1234. brelse(journal->j_sb_buffer);
  1235. }
  1236. if (journal->j_proc_entry)
  1237. jbd2_stats_proc_exit(journal);
  1238. if (journal->j_inode)
  1239. iput(journal->j_inode);
  1240. if (journal->j_revoke)
  1241. jbd2_journal_destroy_revoke(journal);
  1242. kfree(journal->j_wbuf);
  1243. kfree(journal);
  1244. return err;
  1245. }
  1246. /**
  1247. *int jbd2_journal_check_used_features () - Check if features specified are used.
  1248. * @journal: Journal to check.
  1249. * @compat: bitmask of compatible features
  1250. * @ro: bitmask of features that force read-only mount
  1251. * @incompat: bitmask of incompatible features
  1252. *
  1253. * Check whether the journal uses all of a given set of
  1254. * features. Return true (non-zero) if it does.
  1255. **/
  1256. int jbd2_journal_check_used_features (journal_t *journal, unsigned long compat,
  1257. unsigned long ro, unsigned long incompat)
  1258. {
  1259. journal_superblock_t *sb;
  1260. if (!compat && !ro && !incompat)
  1261. return 1;
  1262. /* Load journal superblock if it is not loaded yet. */
  1263. if (journal->j_format_version == 0 &&
  1264. journal_get_superblock(journal) != 0)
  1265. return 0;
  1266. if (journal->j_format_version == 1)
  1267. return 0;
  1268. sb = journal->j_superblock;
  1269. if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
  1270. ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
  1271. ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
  1272. return 1;
  1273. return 0;
  1274. }
  1275. /**
  1276. * int jbd2_journal_check_available_features() - Check feature set in journalling layer
  1277. * @journal: Journal to check.
  1278. * @compat: bitmask of compatible features
  1279. * @ro: bitmask of features that force read-only mount
  1280. * @incompat: bitmask of incompatible features
  1281. *
  1282. * Check whether the journaling code supports the use of
  1283. * all of a given set of features on this journal. Return true
  1284. * (non-zero) if it can. */
  1285. int jbd2_journal_check_available_features (journal_t *journal, unsigned long compat,
  1286. unsigned long ro, unsigned long incompat)
  1287. {
  1288. if (!compat && !ro && !incompat)
  1289. return 1;
  1290. /* We can support any known requested features iff the
  1291. * superblock is in version 2. Otherwise we fail to support any
  1292. * extended sb features. */
  1293. if (journal->j_format_version != 2)
  1294. return 0;
  1295. if ((compat & JBD2_KNOWN_COMPAT_FEATURES) == compat &&
  1296. (ro & JBD2_KNOWN_ROCOMPAT_FEATURES) == ro &&
  1297. (incompat & JBD2_KNOWN_INCOMPAT_FEATURES) == incompat)
  1298. return 1;
  1299. return 0;
  1300. }
  1301. /**
  1302. * int jbd2_journal_set_features () - Mark a given journal feature in the superblock
  1303. * @journal: Journal to act on.
  1304. * @compat: bitmask of compatible features
  1305. * @ro: bitmask of features that force read-only mount
  1306. * @incompat: bitmask of incompatible features
  1307. *
  1308. * Mark a given journal feature as present on the
  1309. * superblock. Returns true if the requested features could be set.
  1310. *
  1311. */
  1312. int jbd2_journal_set_features (journal_t *journal, unsigned long compat,
  1313. unsigned long ro, unsigned long incompat)
  1314. {
  1315. journal_superblock_t *sb;
  1316. if (jbd2_journal_check_used_features(journal, compat, ro, incompat))
  1317. return 1;
  1318. if (!jbd2_journal_check_available_features(journal, compat, ro, incompat))
  1319. return 0;
  1320. jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
  1321. compat, ro, incompat);
  1322. sb = journal->j_superblock;
  1323. sb->s_feature_compat |= cpu_to_be32(compat);
  1324. sb->s_feature_ro_compat |= cpu_to_be32(ro);
  1325. sb->s_feature_incompat |= cpu_to_be32(incompat);
  1326. return 1;
  1327. }
  1328. /*
  1329. * jbd2_journal_clear_features () - Clear a given journal feature in the
  1330. * superblock
  1331. * @journal: Journal to act on.
  1332. * @compat: bitmask of compatible features
  1333. * @ro: bitmask of features that force read-only mount
  1334. * @incompat: bitmask of incompatible features
  1335. *
  1336. * Clear a given journal feature as present on the
  1337. * superblock.
  1338. */
  1339. void jbd2_journal_clear_features(journal_t *journal, unsigned long compat,
  1340. unsigned long ro, unsigned long incompat)
  1341. {
  1342. journal_superblock_t *sb;
  1343. jbd_debug(1, "Clear features 0x%lx/0x%lx/0x%lx\n",
  1344. compat, ro, incompat);
  1345. sb = journal->j_superblock;
  1346. sb->s_feature_compat &= ~cpu_to_be32(compat);
  1347. sb->s_feature_ro_compat &= ~cpu_to_be32(ro);
  1348. sb->s_feature_incompat &= ~cpu_to_be32(incompat);
  1349. }
  1350. EXPORT_SYMBOL(jbd2_journal_clear_features);
  1351. /**
  1352. * int jbd2_journal_update_format () - Update on-disk journal structure.
  1353. * @journal: Journal to act on.
  1354. *
  1355. * Given an initialised but unloaded journal struct, poke about in the
  1356. * on-disk structure to update it to the most recent supported version.
  1357. */
  1358. int jbd2_journal_update_format (journal_t *journal)
  1359. {
  1360. journal_superblock_t *sb;
  1361. int err;
  1362. err = journal_get_superblock(journal);
  1363. if (err)
  1364. return err;
  1365. sb = journal->j_superblock;
  1366. switch (be32_to_cpu(sb->s_header.h_blocktype)) {
  1367. case JBD2_SUPERBLOCK_V2:
  1368. return 0;
  1369. case JBD2_SUPERBLOCK_V1:
  1370. return journal_convert_superblock_v1(journal, sb);
  1371. default:
  1372. break;
  1373. }
  1374. return -EINVAL;
  1375. }
  1376. static int journal_convert_superblock_v1(journal_t *journal,
  1377. journal_superblock_t *sb)
  1378. {
  1379. int offset, blocksize;
  1380. struct buffer_head *bh;
  1381. printk(KERN_WARNING
  1382. "JBD2: Converting superblock from version 1 to 2.\n");
  1383. /* Pre-initialise new fields to zero */
  1384. offset = ((char *) &(sb->s_feature_compat)) - ((char *) sb);
  1385. blocksize = be32_to_cpu(sb->s_blocksize);
  1386. memset(&sb->s_feature_compat, 0, blocksize-offset);
  1387. sb->s_nr_users = cpu_to_be32(1);
  1388. sb->s_header.h_blocktype = cpu_to_be32(JBD2_SUPERBLOCK_V2);
  1389. journal->j_format_version = 2;
  1390. bh = journal->j_sb_buffer;
  1391. BUFFER_TRACE(bh, "marking dirty");
  1392. mark_buffer_dirty(bh);
  1393. sync_dirty_buffer(bh);
  1394. return 0;
  1395. }
  1396. /**
  1397. * int jbd2_journal_flush () - Flush journal
  1398. * @journal: Journal to act on.
  1399. *
  1400. * Flush all data for a given journal to disk and empty the journal.
  1401. * Filesystems can use this when remounting readonly to ensure that
  1402. * recovery does not need to happen on remount.
  1403. */
  1404. int jbd2_journal_flush(journal_t *journal)
  1405. {
  1406. int err = 0;
  1407. transaction_t *transaction = NULL;
  1408. unsigned long old_tail;
  1409. write_lock(&journal->j_state_lock);
  1410. /* Force everything buffered to the log... */
  1411. if (journal->j_running_transaction) {
  1412. transaction = journal->j_running_transaction;
  1413. __jbd2_log_start_commit(journal, transaction->t_tid);
  1414. } else if (journal->j_committing_transaction)
  1415. transaction = journal->j_committing_transaction;
  1416. /* Wait for the log commit to complete... */
  1417. if (transaction) {
  1418. tid_t tid = transaction->t_tid;
  1419. write_unlock(&journal->j_state_lock);
  1420. jbd2_log_wait_commit(journal, tid);
  1421. } else {
  1422. write_unlock(&journal->j_state_lock);
  1423. }
  1424. /* ...and flush everything in the log out to disk. */
  1425. spin_lock(&journal->j_list_lock);
  1426. while (!err && journal->j_checkpoint_transactions != NULL) {
  1427. spin_unlock(&journal->j_list_lock);
  1428. mutex_lock(&journal->j_checkpoint_mutex);
  1429. err = jbd2_log_do_checkpoint(journal);
  1430. mutex_unlock(&journal->j_checkpoint_mutex);
  1431. spin_lock(&journal->j_list_lock);
  1432. }
  1433. spin_unlock(&journal->j_list_lock);
  1434. if (is_journal_aborted(journal))
  1435. return -EIO;
  1436. jbd2_cleanup_journal_tail(journal);
  1437. /* Finally, mark the journal as really needing no recovery.
  1438. * This sets s_start==0 in the underlying superblock, which is
  1439. * the magic code for a fully-recovered superblock. Any future
  1440. * commits of data to the journal will restore the current
  1441. * s_start value. */
  1442. write_lock(&journal->j_state_lock);
  1443. old_tail = journal->j_tail;
  1444. journal->j_tail = 0;
  1445. write_unlock(&journal->j_state_lock);
  1446. jbd2_journal_update_superblock(journal, 1);
  1447. write_lock(&journal->j_state_lock);
  1448. journal->j_tail = old_tail;
  1449. J_ASSERT(!journal->j_running_transaction);
  1450. J_ASSERT(!journal->j_committing_transaction);
  1451. J_ASSERT(!journal->j_checkpoint_transactions);
  1452. J_ASSERT(journal->j_head == journal->j_tail);
  1453. J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
  1454. write_unlock(&journal->j_state_lock);
  1455. return 0;
  1456. }
  1457. /**
  1458. * int jbd2_journal_wipe() - Wipe journal contents
  1459. * @journal: Journal to act on.
  1460. * @write: flag (see below)
  1461. *
  1462. * Wipe out all of the contents of a journal, safely. This will produce
  1463. * a warning if the journal contains any valid recovery information.
  1464. * Must be called between journal_init_*() and jbd2_journal_load().
  1465. *
  1466. * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
  1467. * we merely suppress recovery.
  1468. */
  1469. int jbd2_journal_wipe(journal_t *journal, int write)
  1470. {
  1471. int err = 0;
  1472. J_ASSERT (!(journal->j_flags & JBD2_LOADED));
  1473. err = load_superblock(journal);
  1474. if (err)
  1475. return err;
  1476. if (!journal->j_tail)
  1477. goto no_recovery;
  1478. printk(KERN_WARNING "JBD2: %s recovery information on journal\n",
  1479. write ? "Clearing" : "Ignoring");
  1480. err = jbd2_journal_skip_recovery(journal);
  1481. if (write)
  1482. jbd2_journal_update_superblock(journal, 1);
  1483. no_recovery:
  1484. return err;
  1485. }
  1486. /*
  1487. * Journal abort has very specific semantics, which we describe
  1488. * for journal abort.
  1489. *
  1490. * Two internal functions, which provide abort to the jbd layer
  1491. * itself are here.
  1492. */
  1493. /*
  1494. * Quick version for internal journal use (doesn't lock the journal).
  1495. * Aborts hard --- we mark the abort as occurred, but do _nothing_ else,
  1496. * and don't attempt to make any other journal updates.
  1497. */
  1498. void __jbd2_journal_abort_hard(journal_t *journal)
  1499. {
  1500. transaction_t *transaction;
  1501. if (journal->j_flags & JBD2_ABORT)
  1502. return;
  1503. printk(KERN_ERR "Aborting journal on device %s.\n",
  1504. journal->j_devname);
  1505. write_lock(&journal->j_state_lock);
  1506. journal->j_flags |= JBD2_ABORT;
  1507. transaction = journal->j_running_transaction;
  1508. if (transaction)
  1509. __jbd2_log_start_commit(journal, transaction->t_tid);
  1510. write_unlock(&journal->j_state_lock);
  1511. }
  1512. /* Soft abort: record the abort error status in the journal superblock,
  1513. * but don't do any other IO. */
  1514. static void __journal_abort_soft (journal_t *journal, int errno)
  1515. {
  1516. if (journal->j_flags & JBD2_ABORT)
  1517. return;
  1518. if (!journal->j_errno)
  1519. journal->j_errno = errno;
  1520. __jbd2_journal_abort_hard(journal);
  1521. if (errno)
  1522. jbd2_journal_update_superblock(journal, 1);
  1523. }
  1524. /**
  1525. * void jbd2_journal_abort () - Shutdown the journal immediately.
  1526. * @journal: the journal to shutdown.
  1527. * @errno: an error number to record in the journal indicating
  1528. * the reason for the shutdown.
  1529. *
  1530. * Perform a complete, immediate shutdown of the ENTIRE
  1531. * journal (not of a single transaction). This operation cannot be
  1532. * undone without closing and reopening the journal.
  1533. *
  1534. * The jbd2_journal_abort function is intended to support higher level error
  1535. * recovery mechanisms such as the ext2/ext3 remount-readonly error
  1536. * mode.
  1537. *
  1538. * Journal abort has very specific semantics. Any existing dirty,
  1539. * unjournaled buffers in the main filesystem will still be written to
  1540. * disk by bdflush, but the journaling mechanism will be suspended
  1541. * immediately and no further transaction commits will be honoured.
  1542. *
  1543. * Any dirty, journaled buffers will be written back to disk without
  1544. * hitting the journal. Atomicity cannot be guaranteed on an aborted
  1545. * filesystem, but we _do_ attempt to leave as much data as possible
  1546. * behind for fsck to use for cleanup.
  1547. *
  1548. * Any attempt to get a new transaction handle on a journal which is in
  1549. * ABORT state will just result in an -EROFS error return. A
  1550. * jbd2_journal_stop on an existing handle will return -EIO if we have
  1551. * entered abort state during the update.
  1552. *
  1553. * Recursive transactions are not disturbed by journal abort until the
  1554. * final jbd2_journal_stop, which will receive the -EIO error.
  1555. *
  1556. * Finally, the jbd2_journal_abort call allows the caller to supply an errno
  1557. * which will be recorded (if possible) in the journal superblock. This
  1558. * allows a client to record failure conditions in the middle of a
  1559. * transaction without having to complete the transaction to record the
  1560. * failure to disk. ext3_error, for example, now uses this
  1561. * functionality.
  1562. *
  1563. * Errors which originate from within the journaling layer will NOT
  1564. * supply an errno; a null errno implies that absolutely no further
  1565. * writes are done to the journal (unless there are any already in
  1566. * progress).
  1567. *
  1568. */
  1569. void jbd2_journal_abort(journal_t *journal, int errno)
  1570. {
  1571. __journal_abort_soft(journal, errno);
  1572. }
  1573. /**
  1574. * int jbd2_journal_errno () - returns the journal's error state.
  1575. * @journal: journal to examine.
  1576. *
  1577. * This is the errno number set with jbd2_journal_abort(), the last
  1578. * time the journal was mounted - if the journal was stopped
  1579. * without calling abort this will be 0.
  1580. *
  1581. * If the journal has been aborted on this mount time -EROFS will
  1582. * be returned.
  1583. */
  1584. int jbd2_journal_errno(journal_t *journal)
  1585. {
  1586. int err;
  1587. read_lock(&journal->j_state_lock);
  1588. if (journal->j_flags & JBD2_ABORT)
  1589. err = -EROFS;
  1590. else
  1591. err = journal->j_errno;
  1592. read_unlock(&journal->j_state_lock);
  1593. return err;
  1594. }
  1595. /**
  1596. * int jbd2_journal_clear_err () - clears the journal's error state
  1597. * @journal: journal to act on.
  1598. *
  1599. * An error must be cleared or acked to take a FS out of readonly
  1600. * mode.
  1601. */
  1602. int jbd2_journal_clear_err(journal_t *journal)
  1603. {
  1604. int err = 0;
  1605. write_lock(&journal->j_state_lock);
  1606. if (journal->j_flags & JBD2_ABORT)
  1607. err = -EROFS;
  1608. else
  1609. journal->j_errno = 0;
  1610. write_unlock(&journal->j_state_lock);
  1611. return err;
  1612. }
  1613. /**
  1614. * void jbd2_journal_ack_err() - Ack journal err.
  1615. * @journal: journal to act on.
  1616. *
  1617. * An error must be cleared or acked to take a FS out of readonly
  1618. * mode.
  1619. */
  1620. void jbd2_journal_ack_err(journal_t *journal)
  1621. {
  1622. write_lock(&journal->j_state_lock);
  1623. if (journal->j_errno)
  1624. journal->j_flags |= JBD2_ACK_ERR;
  1625. write_unlock(&journal->j_state_lock);
  1626. }
  1627. int jbd2_journal_blocks_per_page(struct inode *inode)
  1628. {
  1629. return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
  1630. }
  1631. /*
  1632. * helper functions to deal with 32 or 64bit block numbers.
  1633. */
  1634. size_t journal_tag_bytes(journal_t *journal)
  1635. {
  1636. if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_64BIT))
  1637. return JBD2_TAG_SIZE64;
  1638. else
  1639. return JBD2_TAG_SIZE32;
  1640. }
  1641. /*
  1642. * JBD memory management
  1643. *
  1644. * These functions are used to allocate block-sized chunks of memory
  1645. * used for making copies of buffer_head data. Very often it will be
  1646. * page-sized chunks of data, but sometimes it will be in
  1647. * sub-page-size chunks. (For example, 16k pages on Power systems
  1648. * with a 4k block file system.) For blocks smaller than a page, we
  1649. * use a SLAB allocator. There are slab caches for each block size,
  1650. * which are allocated at mount time, if necessary, and we only free
  1651. * (all of) the slab caches when/if the jbd2 module is unloaded. For
  1652. * this reason we don't need to a mutex to protect access to
  1653. * jbd2_slab[] allocating or releasing memory; only in
  1654. * jbd2_journal_create_slab().
  1655. */
  1656. #define JBD2_MAX_SLABS 8
  1657. static struct kmem_cache *jbd2_slab[JBD2_MAX_SLABS];
  1658. static const char *jbd2_slab_names[JBD2_MAX_SLABS] = {
  1659. "jbd2_1k", "jbd2_2k", "jbd2_4k", "jbd2_8k",
  1660. "jbd2_16k", "jbd2_32k", "jbd2_64k", "jbd2_128k"
  1661. };
  1662. static void jbd2_journal_destroy_slabs(void)
  1663. {
  1664. int i;
  1665. for (i = 0; i < JBD2_MAX_SLABS; i++) {
  1666. if (jbd2_slab[i])
  1667. kmem_cache_destroy(jbd2_slab[i]);
  1668. jbd2_slab[i] = NULL;
  1669. }
  1670. }
  1671. static int jbd2_journal_create_slab(size_t size)
  1672. {
  1673. static DEFINE_MUTEX(jbd2_slab_create_mutex);
  1674. int i = order_base_2(size) - 10;
  1675. size_t slab_size;
  1676. if (size == PAGE_SIZE)
  1677. return 0;
  1678. if (i >= JBD2_MAX_SLABS)
  1679. return -EINVAL;
  1680. if (unlikely(i < 0))
  1681. i = 0;
  1682. mutex_lock(&jbd2_slab_create_mutex);
  1683. if (jbd2_slab[i]) {
  1684. mutex_unlock(&jbd2_slab_create_mutex);
  1685. return 0; /* Already created */
  1686. }
  1687. slab_size = 1 << (i+10);
  1688. jbd2_slab[i] = kmem_cache_create(jbd2_slab_names[i], slab_size,
  1689. slab_size, 0, NULL);
  1690. mutex_unlock(&jbd2_slab_create_mutex);
  1691. if (!jbd2_slab[i]) {
  1692. printk(KERN_EMERG "JBD2: no memory for jbd2_slab cache\n");
  1693. return -ENOMEM;
  1694. }
  1695. return 0;
  1696. }
  1697. static struct kmem_cache *get_slab(size_t size)
  1698. {
  1699. int i = order_base_2(size) - 10;
  1700. BUG_ON(i >= JBD2_MAX_SLABS);
  1701. if (unlikely(i < 0))
  1702. i = 0;
  1703. BUG_ON(jbd2_slab[i] == NULL);
  1704. return jbd2_slab[i];
  1705. }
  1706. void *jbd2_alloc(size_t size, gfp_t flags)
  1707. {
  1708. void *ptr;
  1709. BUG_ON(size & (size-1)); /* Must be a power of 2 */
  1710. flags |= __GFP_REPEAT;
  1711. if (size == PAGE_SIZE)
  1712. ptr = (void *)__get_free_pages(flags, 0);
  1713. else if (size > PAGE_SIZE) {
  1714. int order = get_order(size);
  1715. if (order < 3)
  1716. ptr = (void *)__get_free_pages(flags, order);
  1717. else
  1718. ptr = vmalloc(size);
  1719. } else
  1720. ptr = kmem_cache_alloc(get_slab(size), flags);
  1721. /* Check alignment; SLUB has gotten this wrong in the past,
  1722. * and this can lead to user data corruption! */
  1723. BUG_ON(((unsigned long) ptr) & (size-1));
  1724. return ptr;
  1725. }
  1726. void jbd2_free(void *ptr, size_t size)
  1727. {
  1728. if (size == PAGE_SIZE) {
  1729. free_pages((unsigned long)ptr, 0);
  1730. return;
  1731. }
  1732. if (size > PAGE_SIZE) {
  1733. int order = get_order(size);
  1734. if (order < 3)
  1735. free_pages((unsigned long)ptr, order);
  1736. else
  1737. vfree(ptr);
  1738. return;
  1739. }
  1740. kmem_cache_free(get_slab(size), ptr);
  1741. };
  1742. /*
  1743. * Journal_head storage management
  1744. */
  1745. static struct kmem_cache *jbd2_journal_head_cache;
  1746. #ifdef CONFIG_JBD2_DEBUG
  1747. static atomic_t nr_journal_heads = ATOMIC_INIT(0);
  1748. #endif
  1749. static int journal_init_jbd2_journal_head_cache(void)
  1750. {
  1751. int retval;
  1752. J_ASSERT(jbd2_journal_head_cache == NULL);
  1753. jbd2_journal_head_cache = kmem_cache_create("jbd2_journal_head",
  1754. sizeof(struct journal_head),
  1755. 0, /* offset */
  1756. SLAB_TEMPORARY, /* flags */
  1757. NULL); /* ctor */
  1758. retval = 0;
  1759. if (!jbd2_journal_head_cache) {
  1760. retval = -ENOMEM;
  1761. printk(KERN_EMERG "JBD2: no memory for journal_head cache\n");
  1762. }
  1763. return retval;
  1764. }
  1765. static void jbd2_journal_destroy_jbd2_journal_head_cache(void)
  1766. {
  1767. if (jbd2_journal_head_cache) {
  1768. kmem_cache_destroy(jbd2_journal_head_cache);
  1769. jbd2_journal_head_cache = NULL;
  1770. }
  1771. }
  1772. /*
  1773. * journal_head splicing and dicing
  1774. */
  1775. static struct journal_head *journal_alloc_journal_head(void)
  1776. {
  1777. struct journal_head *ret;
  1778. #ifdef CONFIG_JBD2_DEBUG
  1779. atomic_inc(&nr_journal_heads);
  1780. #endif
  1781. ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
  1782. if (!ret) {
  1783. jbd_debug(1, "out of memory for journal_head\n");
  1784. pr_notice_ratelimited("ENOMEM in %s, retrying.\n", __func__);
  1785. while (!ret) {
  1786. yield();
  1787. ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
  1788. }
  1789. }
  1790. return ret;
  1791. }
  1792. static void journal_free_journal_head(struct journal_head *jh)
  1793. {
  1794. #ifdef CONFIG_JBD2_DEBUG
  1795. atomic_dec(&nr_journal_heads);
  1796. memset(jh, JBD2_POISON_FREE, sizeof(*jh));
  1797. #endif
  1798. kmem_cache_free(jbd2_journal_head_cache, jh);
  1799. }
  1800. /*
  1801. * A journal_head is attached to a buffer_head whenever JBD has an
  1802. * interest in the buffer.
  1803. *
  1804. * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit
  1805. * is set. This bit is tested in core kernel code where we need to take
  1806. * JBD-specific actions. Testing the zeroness of ->b_private is not reliable
  1807. * there.
  1808. *
  1809. * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
  1810. *
  1811. * When a buffer has its BH_JBD bit set it is immune from being released by
  1812. * core kernel code, mainly via ->b_count.
  1813. *
  1814. * A journal_head is detached from its buffer_head when the journal_head's
  1815. * b_jcount reaches zero. Running transaction (b_transaction) and checkpoint
  1816. * transaction (b_cp_transaction) hold their references to b_jcount.
  1817. *
  1818. * Various places in the kernel want to attach a journal_head to a buffer_head
  1819. * _before_ attaching the journal_head to a transaction. To protect the
  1820. * journal_head in this situation, jbd2_journal_add_journal_head elevates the
  1821. * journal_head's b_jcount refcount by one. The caller must call
  1822. * jbd2_journal_put_journal_head() to undo this.
  1823. *
  1824. * So the typical usage would be:
  1825. *
  1826. * (Attach a journal_head if needed. Increments b_jcount)
  1827. * struct journal_head *jh = jbd2_journal_add_journal_head(bh);
  1828. * ...
  1829. * (Get another reference for transaction)
  1830. * jbd2_journal_grab_journal_head(bh);
  1831. * jh->b_transaction = xxx;
  1832. * (Put original reference)
  1833. * jbd2_journal_put_journal_head(jh);
  1834. */
  1835. /*
  1836. * Give a buffer_head a journal_head.
  1837. *
  1838. * May sleep.
  1839. */
  1840. struct journal_head *jbd2_journal_add_journal_head(struct buffer_head *bh)
  1841. {
  1842. struct journal_head *jh;
  1843. struct journal_head *new_jh = NULL;
  1844. repeat:
  1845. if (!buffer_jbd(bh)) {
  1846. new_jh = journal_alloc_journal_head();
  1847. memset(new_jh, 0, sizeof(*new_jh));
  1848. }
  1849. jbd_lock_bh_journal_head(bh);
  1850. if (buffer_jbd(bh)) {
  1851. jh = bh2jh(bh);
  1852. } else {
  1853. J_ASSERT_BH(bh,
  1854. (atomic_read(&bh->b_count) > 0) ||
  1855. (bh->b_page && bh->b_page->mapping));
  1856. if (!new_jh) {
  1857. jbd_unlock_bh_journal_head(bh);
  1858. goto repeat;
  1859. }
  1860. jh = new_jh;
  1861. new_jh = NULL; /* We consumed it */
  1862. set_buffer_jbd(bh);
  1863. bh->b_private = jh;
  1864. jh->b_bh = bh;
  1865. get_bh(bh);
  1866. BUFFER_TRACE(bh, "added journal_head");
  1867. }
  1868. jh->b_jcount++;
  1869. jbd_unlock_bh_journal_head(bh);
  1870. if (new_jh)
  1871. journal_free_journal_head(new_jh);
  1872. return bh->b_private;
  1873. }
  1874. /*
  1875. * Grab a ref against this buffer_head's journal_head. If it ended up not
  1876. * having a journal_head, return NULL
  1877. */
  1878. struct journal_head *jbd2_journal_grab_journal_head(struct buffer_head *bh)
  1879. {
  1880. struct journal_head *jh = NULL;
  1881. jbd_lock_bh_journal_head(bh);
  1882. if (buffer_jbd(bh)) {
  1883. jh = bh2jh(bh);
  1884. jh->b_jcount++;
  1885. }
  1886. jbd_unlock_bh_journal_head(bh);
  1887. return jh;
  1888. }
  1889. static void __journal_remove_journal_head(struct buffer_head *bh)
  1890. {
  1891. struct journal_head *jh = bh2jh(bh);
  1892. J_ASSERT_JH(jh, jh->b_jcount >= 0);
  1893. J_ASSERT_JH(jh, jh->b_transaction == NULL);
  1894. J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
  1895. J_ASSERT_JH(jh, jh->b_cp_transaction == NULL);
  1896. J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
  1897. J_ASSERT_BH(bh, buffer_jbd(bh));
  1898. J_ASSERT_BH(bh, jh2bh(jh) == bh);
  1899. BUFFER_TRACE(bh, "remove journal_head");
  1900. if (jh->b_frozen_data) {
  1901. printk(KERN_WARNING "%s: freeing b_frozen_data\n", __func__);
  1902. jbd2_free(jh->b_frozen_data, bh->b_size);
  1903. }
  1904. if (jh->b_committed_data) {
  1905. printk(KERN_WARNING "%s: freeing b_committed_data\n", __func__);
  1906. jbd2_free(jh->b_committed_data, bh->b_size);
  1907. }
  1908. bh->b_private = NULL;
  1909. jh->b_bh = NULL; /* debug, really */
  1910. clear_buffer_jbd(bh);
  1911. journal_free_journal_head(jh);
  1912. }
  1913. /*
  1914. * Drop a reference on the passed journal_head. If it fell to zero then
  1915. * release the journal_head from the buffer_head.
  1916. */
  1917. void jbd2_journal_put_journal_head(struct journal_head *jh)
  1918. {
  1919. struct buffer_head *bh = jh2bh(jh);
  1920. jbd_lock_bh_journal_head(bh);
  1921. J_ASSERT_JH(jh, jh->b_jcount > 0);
  1922. --jh->b_jcount;
  1923. if (!jh->b_jcount) {
  1924. __journal_remove_journal_head(bh);
  1925. jbd_unlock_bh_journal_head(bh);
  1926. __brelse(bh);
  1927. } else
  1928. jbd_unlock_bh_journal_head(bh);
  1929. }
  1930. /*
  1931. * Initialize jbd inode head
  1932. */
  1933. void jbd2_journal_init_jbd_inode(struct jbd2_inode *jinode, struct inode *inode)
  1934. {
  1935. jinode->i_transaction = NULL;
  1936. jinode->i_next_transaction = NULL;
  1937. jinode->i_vfs_inode = inode;
  1938. jinode->i_flags = 0;
  1939. INIT_LIST_HEAD(&jinode->i_list);
  1940. }
  1941. /*
  1942. * Function to be called before we start removing inode from memory (i.e.,
  1943. * clear_inode() is a fine place to be called from). It removes inode from
  1944. * transaction's lists.
  1945. */
  1946. void jbd2_journal_release_jbd_inode(journal_t *journal,
  1947. struct jbd2_inode *jinode)
  1948. {
  1949. if (!journal)
  1950. return;
  1951. restart:
  1952. spin_lock(&journal->j_list_lock);
  1953. /* Is commit writing out inode - we have to wait */
  1954. if (test_bit(__JI_COMMIT_RUNNING, &jinode->i_flags)) {
  1955. wait_queue_head_t *wq;
  1956. DEFINE_WAIT_BIT(wait, &jinode->i_flags, __JI_COMMIT_RUNNING);
  1957. wq = bit_waitqueue(&jinode->i_flags, __JI_COMMIT_RUNNING);
  1958. prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
  1959. spin_unlock(&journal->j_list_lock);
  1960. schedule();
  1961. finish_wait(wq, &wait.wait);
  1962. goto restart;
  1963. }
  1964. if (jinode->i_transaction) {
  1965. list_del(&jinode->i_list);
  1966. jinode->i_transaction = NULL;
  1967. }
  1968. spin_unlock(&journal->j_list_lock);
  1969. }
  1970. /*
  1971. * debugfs tunables
  1972. */
  1973. #ifdef CONFIG_JBD2_DEBUG
  1974. u8 jbd2_journal_enable_debug __read_mostly;
  1975. EXPORT_SYMBOL(jbd2_journal_enable_debug);
  1976. #define JBD2_DEBUG_NAME "jbd2-debug"
  1977. static struct dentry *jbd2_debugfs_dir;
  1978. static struct dentry *jbd2_debug;
  1979. static void __init jbd2_create_debugfs_entry(void)
  1980. {
  1981. jbd2_debugfs_dir = debugfs_create_dir("jbd2", NULL);
  1982. if (jbd2_debugfs_dir)
  1983. jbd2_debug = debugfs_create_u8(JBD2_DEBUG_NAME,
  1984. S_IRUGO | S_IWUSR,
  1985. jbd2_debugfs_dir,
  1986. &jbd2_journal_enable_debug);
  1987. }
  1988. static void __exit jbd2_remove_debugfs_entry(void)
  1989. {
  1990. debugfs_remove(jbd2_debug);
  1991. debugfs_remove(jbd2_debugfs_dir);
  1992. }
  1993. #else
  1994. static void __init jbd2_create_debugfs_entry(void)
  1995. {
  1996. }
  1997. static void __exit jbd2_remove_debugfs_entry(void)
  1998. {
  1999. }
  2000. #endif
  2001. #ifdef CONFIG_PROC_FS
  2002. #define JBD2_STATS_PROC_NAME "fs/jbd2"
  2003. static void __init jbd2_create_jbd_stats_proc_entry(void)
  2004. {
  2005. proc_jbd2_stats = proc_mkdir(JBD2_STATS_PROC_NAME, NULL);
  2006. }
  2007. static void __exit jbd2_remove_jbd_stats_proc_entry(void)
  2008. {
  2009. if (proc_jbd2_stats)
  2010. remove_proc_entry(JBD2_STATS_PROC_NAME, NULL);
  2011. }
  2012. #else
  2013. #define jbd2_create_jbd_stats_proc_entry() do {} while (0)
  2014. #define jbd2_remove_jbd_stats_proc_entry() do {} while (0)
  2015. #endif
  2016. struct kmem_cache *jbd2_handle_cache, *jbd2_inode_cache;
  2017. static int __init journal_init_handle_cache(void)
  2018. {
  2019. jbd2_handle_cache = KMEM_CACHE(jbd2_journal_handle, SLAB_TEMPORARY);
  2020. if (jbd2_handle_cache == NULL) {
  2021. printk(KERN_EMERG "JBD2: failed to create handle cache\n");
  2022. return -ENOMEM;
  2023. }
  2024. jbd2_inode_cache = KMEM_CACHE(jbd2_inode, 0);
  2025. if (jbd2_inode_cache == NULL) {
  2026. printk(KERN_EMERG "JBD2: failed to create inode cache\n");
  2027. kmem_cache_destroy(jbd2_handle_cache);
  2028. return -ENOMEM;
  2029. }
  2030. return 0;
  2031. }
  2032. static void jbd2_journal_destroy_handle_cache(void)
  2033. {
  2034. if (jbd2_handle_cache)
  2035. kmem_cache_destroy(jbd2_handle_cache);
  2036. if (jbd2_inode_cache)
  2037. kmem_cache_destroy(jbd2_inode_cache);
  2038. }
  2039. /*
  2040. * Module startup and shutdown
  2041. */
  2042. static int __init journal_init_caches(void)
  2043. {
  2044. int ret;
  2045. ret = jbd2_journal_init_revoke_caches();
  2046. if (ret == 0)
  2047. ret = journal_init_jbd2_journal_head_cache();
  2048. if (ret == 0)
  2049. ret = journal_init_handle_cache();
  2050. return ret;
  2051. }
  2052. static void jbd2_journal_destroy_caches(void)
  2053. {
  2054. jbd2_journal_destroy_revoke_caches();
  2055. jbd2_journal_destroy_jbd2_journal_head_cache();
  2056. jbd2_journal_destroy_handle_cache();
  2057. jbd2_journal_destroy_slabs();
  2058. }
  2059. static int __init journal_init(void)
  2060. {
  2061. int ret;
  2062. BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024);
  2063. ret = journal_init_caches();
  2064. if (ret == 0) {
  2065. jbd2_create_debugfs_entry();
  2066. jbd2_create_jbd_stats_proc_entry();
  2067. } else {
  2068. jbd2_journal_destroy_caches();
  2069. }
  2070. return ret;
  2071. }
  2072. static void __exit journal_exit(void)
  2073. {
  2074. #ifdef CONFIG_JBD2_DEBUG
  2075. int n = atomic_read(&nr_journal_heads);
  2076. if (n)
  2077. printk(KERN_EMERG "JBD2: leaked %d journal_heads!\n", n);
  2078. #endif
  2079. jbd2_remove_debugfs_entry();
  2080. jbd2_remove_jbd_stats_proc_entry();
  2081. jbd2_journal_destroy_caches();
  2082. }
  2083. MODULE_LICENSE("GPL");
  2084. module_init(journal_init);
  2085. module_exit(journal_exit);