journal.c 64 KB

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