jfs_logmgr.c 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) International Business Machines Corp., 2000-2004
  4. * Portions Copyright (C) Christoph Hellwig, 2001-2002
  5. */
  6. /*
  7. * jfs_logmgr.c: log manager
  8. *
  9. * for related information, see transaction manager (jfs_txnmgr.c), and
  10. * recovery manager (jfs_logredo.c).
  11. *
  12. * note: for detail, RTFS.
  13. *
  14. * log buffer manager:
  15. * special purpose buffer manager supporting log i/o requirements.
  16. * per log serial pageout of logpage
  17. * queuing i/o requests and redrive i/o at iodone
  18. * maintain current logpage buffer
  19. * no caching since append only
  20. * appropriate jfs buffer cache buffers as needed
  21. *
  22. * group commit:
  23. * transactions which wrote COMMIT records in the same in-memory
  24. * log page during the pageout of previous/current log page(s) are
  25. * committed together by the pageout of the page.
  26. *
  27. * TBD lazy commit:
  28. * transactions are committed asynchronously when the log page
  29. * containing it COMMIT is paged out when it becomes full;
  30. *
  31. * serialization:
  32. * . a per log lock serialize log write.
  33. * . a per log lock serialize group commit.
  34. * . a per log lock serialize log open/close;
  35. *
  36. * TBD log integrity:
  37. * careful-write (ping-pong) of last logpage to recover from crash
  38. * in overwrite.
  39. * detection of split (out-of-order) write of physical sectors
  40. * of last logpage via timestamp at end of each sector
  41. * with its mirror data array at trailer).
  42. *
  43. * alternatives:
  44. * lsn - 64-bit monotonically increasing integer vs
  45. * 32-bit lspn and page eor.
  46. */
  47. #include <linux/fs.h>
  48. #include <linux/blkdev.h>
  49. #include <linux/interrupt.h>
  50. #include <linux/completion.h>
  51. #include <linux/kthread.h>
  52. #include <linux/buffer_head.h> /* for sync_blockdev() */
  53. #include <linux/bio.h>
  54. #include <linux/freezer.h>
  55. #include <linux/export.h>
  56. #include <linux/delay.h>
  57. #include <linux/mutex.h>
  58. #include <linux/seq_file.h>
  59. #include <linux/slab.h>
  60. #include "jfs_incore.h"
  61. #include "jfs_filsys.h"
  62. #include "jfs_metapage.h"
  63. #include "jfs_superblock.h"
  64. #include "jfs_txnmgr.h"
  65. #include "jfs_debug.h"
  66. /*
  67. * lbuf's ready to be redriven. Protected by log_redrive_lock (jfsIO thread)
  68. */
  69. static struct lbuf *log_redrive_list;
  70. static DEFINE_SPINLOCK(log_redrive_lock);
  71. /*
  72. * log read/write serialization (per log)
  73. */
  74. #define LOG_LOCK_INIT(log) mutex_init(&(log)->loglock)
  75. #define LOG_LOCK(log) mutex_lock(&((log)->loglock))
  76. #define LOG_UNLOCK(log) mutex_unlock(&((log)->loglock))
  77. /*
  78. * log group commit serialization (per log)
  79. */
  80. #define LOGGC_LOCK_INIT(log) spin_lock_init(&(log)->gclock)
  81. #define LOGGC_LOCK(log) spin_lock_irq(&(log)->gclock)
  82. #define LOGGC_UNLOCK(log) spin_unlock_irq(&(log)->gclock)
  83. #define LOGGC_WAKEUP(tblk) wake_up_all(&(tblk)->gcwait)
  84. /*
  85. * log sync serialization (per log)
  86. */
  87. #define LOGSYNC_DELTA(logsize) min((logsize)/8, 128*LOGPSIZE)
  88. #define LOGSYNC_BARRIER(logsize) ((logsize)/4)
  89. /*
  90. #define LOGSYNC_DELTA(logsize) min((logsize)/4, 256*LOGPSIZE)
  91. #define LOGSYNC_BARRIER(logsize) ((logsize)/2)
  92. */
  93. /*
  94. * log buffer cache synchronization
  95. */
  96. static DEFINE_SPINLOCK(jfsLCacheLock);
  97. #define LCACHE_LOCK(flags) spin_lock_irqsave(&jfsLCacheLock, flags)
  98. #define LCACHE_UNLOCK(flags) spin_unlock_irqrestore(&jfsLCacheLock, flags)
  99. /*
  100. * See __SLEEP_COND in jfs_locks.h
  101. */
  102. #define LCACHE_SLEEP_COND(wq, cond, flags) \
  103. do { \
  104. if (cond) \
  105. break; \
  106. __SLEEP_COND(wq, cond, LCACHE_LOCK(flags), LCACHE_UNLOCK(flags)); \
  107. } while (0)
  108. #define LCACHE_WAKEUP(event) wake_up(event)
  109. /*
  110. * lbuf buffer cache (lCache) control
  111. */
  112. /* log buffer manager pageout control (cumulative, inclusive) */
  113. #define lbmREAD 0x0001
  114. #define lbmWRITE 0x0002 /* enqueue at tail of write queue;
  115. * init pageout if at head of queue;
  116. */
  117. #define lbmRELEASE 0x0004 /* remove from write queue
  118. * at completion of pageout;
  119. * do not free/recycle it yet:
  120. * caller will free it;
  121. */
  122. #define lbmSYNC 0x0008 /* do not return to freelist
  123. * when removed from write queue;
  124. */
  125. #define lbmFREE 0x0010 /* return to freelist
  126. * at completion of pageout;
  127. * the buffer may be recycled;
  128. */
  129. #define lbmDONE 0x0020
  130. #define lbmERROR 0x0040
  131. #define lbmGC 0x0080 /* lbmIODone to perform post-GC processing
  132. * of log page
  133. */
  134. #define lbmDIRECT 0x0100
  135. /*
  136. * Global list of active external journals
  137. */
  138. static LIST_HEAD(jfs_external_logs);
  139. static struct jfs_log *dummy_log;
  140. static DEFINE_MUTEX(jfs_log_mutex);
  141. /*
  142. * forward references
  143. */
  144. static int lmWriteRecord(struct jfs_log * log, struct tblock * tblk,
  145. struct lrd * lrd, struct tlock * tlck);
  146. static int lmNextPage(struct jfs_log * log);
  147. static int lmLogFileSystem(struct jfs_log * log, struct jfs_sb_info *sbi,
  148. int activate);
  149. static int open_inline_log(struct super_block *sb);
  150. static int open_dummy_log(struct super_block *sb);
  151. static int lbmLogInit(struct jfs_log * log);
  152. static void lbmLogShutdown(struct jfs_log * log);
  153. static struct lbuf *lbmAllocate(struct jfs_log * log, int);
  154. static void lbmFree(struct lbuf * bp);
  155. static void lbmfree(struct lbuf * bp);
  156. static int lbmRead(struct jfs_log * log, int pn, struct lbuf ** bpp);
  157. static void lbmWrite(struct jfs_log * log, struct lbuf * bp, int flag, int cant_block);
  158. static void lbmDirectWrite(struct jfs_log * log, struct lbuf * bp, int flag);
  159. static int lbmIOWait(struct lbuf * bp, int flag);
  160. static bio_end_io_t lbmIODone;
  161. static void lbmStartIO(struct lbuf * bp);
  162. static void lmGCwrite(struct jfs_log * log, int cant_block);
  163. static int lmLogSync(struct jfs_log * log, int hard_sync);
  164. /*
  165. * statistics
  166. */
  167. #ifdef CONFIG_JFS_STATISTICS
  168. static struct lmStat {
  169. uint commit; /* # of commit */
  170. uint pagedone; /* # of page written */
  171. uint submitted; /* # of pages submitted */
  172. uint full_page; /* # of full pages submitted */
  173. uint partial_page; /* # of partial pages submitted */
  174. } lmStat;
  175. #endif
  176. static void write_special_inodes(struct jfs_log *log,
  177. int (*writer)(struct address_space *))
  178. {
  179. struct jfs_sb_info *sbi;
  180. list_for_each_entry(sbi, &log->sb_list, log_list) {
  181. writer(sbi->ipbmap->i_mapping);
  182. writer(sbi->ipimap->i_mapping);
  183. writer(sbi->direct_inode->i_mapping);
  184. }
  185. }
  186. /*
  187. * NAME: lmLog()
  188. *
  189. * FUNCTION: write a log record;
  190. *
  191. * PARAMETER:
  192. *
  193. * RETURN: lsn - offset to the next log record to write (end-of-log);
  194. * -1 - error;
  195. *
  196. * note: todo: log error handler
  197. */
  198. int lmLog(struct jfs_log * log, struct tblock * tblk, struct lrd * lrd,
  199. struct tlock * tlck)
  200. {
  201. int lsn;
  202. int diffp, difft;
  203. struct metapage *mp = NULL;
  204. unsigned long flags;
  205. jfs_info("lmLog: log:0x%p tblk:0x%p, lrd:0x%p tlck:0x%p",
  206. log, tblk, lrd, tlck);
  207. LOG_LOCK(log);
  208. /* log by (out-of-transaction) JFS ? */
  209. if (tblk == NULL)
  210. goto writeRecord;
  211. /* log from page ? */
  212. if (tlck == NULL ||
  213. tlck->type & tlckBTROOT || (mp = tlck->mp) == NULL)
  214. goto writeRecord;
  215. /*
  216. * initialize/update page/transaction recovery lsn
  217. */
  218. lsn = log->lsn;
  219. LOGSYNC_LOCK(log, flags);
  220. /*
  221. * initialize page lsn if first log write of the page
  222. */
  223. if (mp->lsn == 0) {
  224. mp->log = log;
  225. mp->lsn = lsn;
  226. log->count++;
  227. /* insert page at tail of logsynclist */
  228. list_add_tail(&mp->synclist, &log->synclist);
  229. }
  230. /*
  231. * initialize/update lsn of tblock of the page
  232. *
  233. * transaction inherits oldest lsn of pages associated
  234. * with allocation/deallocation of resources (their
  235. * log records are used to reconstruct allocation map
  236. * at recovery time: inode for inode allocation map,
  237. * B+-tree index of extent descriptors for block
  238. * allocation map);
  239. * allocation map pages inherit transaction lsn at
  240. * commit time to allow forwarding log syncpt past log
  241. * records associated with allocation/deallocation of
  242. * resources only after persistent map of these map pages
  243. * have been updated and propagated to home.
  244. */
  245. /*
  246. * initialize transaction lsn:
  247. */
  248. if (tblk->lsn == 0) {
  249. /* inherit lsn of its first page logged */
  250. tblk->lsn = mp->lsn;
  251. log->count++;
  252. /* insert tblock after the page on logsynclist */
  253. list_add(&tblk->synclist, &mp->synclist);
  254. }
  255. /*
  256. * update transaction lsn:
  257. */
  258. else {
  259. /* inherit oldest/smallest lsn of page */
  260. logdiff(diffp, mp->lsn, log);
  261. logdiff(difft, tblk->lsn, log);
  262. if (diffp < difft) {
  263. /* update tblock lsn with page lsn */
  264. tblk->lsn = mp->lsn;
  265. /* move tblock after page on logsynclist */
  266. list_move(&tblk->synclist, &mp->synclist);
  267. }
  268. }
  269. LOGSYNC_UNLOCK(log, flags);
  270. /*
  271. * write the log record
  272. */
  273. writeRecord:
  274. lsn = lmWriteRecord(log, tblk, lrd, tlck);
  275. /*
  276. * forward log syncpt if log reached next syncpt trigger
  277. */
  278. logdiff(diffp, lsn, log);
  279. if (diffp >= log->nextsync)
  280. lsn = lmLogSync(log, 0);
  281. /* update end-of-log lsn */
  282. log->lsn = lsn;
  283. LOG_UNLOCK(log);
  284. /* return end-of-log address */
  285. return lsn;
  286. }
  287. /*
  288. * NAME: lmWriteRecord()
  289. *
  290. * FUNCTION: move the log record to current log page
  291. *
  292. * PARAMETER: cd - commit descriptor
  293. *
  294. * RETURN: end-of-log address
  295. *
  296. * serialization: LOG_LOCK() held on entry/exit
  297. */
  298. static int
  299. lmWriteRecord(struct jfs_log * log, struct tblock * tblk, struct lrd * lrd,
  300. struct tlock * tlck)
  301. {
  302. int lsn = 0; /* end-of-log address */
  303. struct lbuf *bp; /* dst log page buffer */
  304. struct logpage *lp; /* dst log page */
  305. caddr_t dst; /* destination address in log page */
  306. int dstoffset; /* end-of-log offset in log page */
  307. int freespace; /* free space in log page */
  308. caddr_t p; /* src meta-data page */
  309. caddr_t src;
  310. int srclen;
  311. int nbytes; /* number of bytes to move */
  312. int i;
  313. int len;
  314. struct linelock *linelock;
  315. struct lv *lv;
  316. struct lvd *lvd;
  317. int l2linesize;
  318. len = 0;
  319. /* retrieve destination log page to write */
  320. bp = (struct lbuf *) log->bp;
  321. lp = (struct logpage *) bp->l_ldata;
  322. dstoffset = log->eor;
  323. /* any log data to write ? */
  324. if (tlck == NULL)
  325. goto moveLrd;
  326. /*
  327. * move log record data
  328. */
  329. /* retrieve source meta-data page to log */
  330. if (tlck->flag & tlckPAGELOCK) {
  331. p = (caddr_t) (tlck->mp->data);
  332. linelock = (struct linelock *) & tlck->lock;
  333. }
  334. /* retrieve source in-memory inode to log */
  335. else if (tlck->flag & tlckINODELOCK) {
  336. if (tlck->type & tlckDTREE)
  337. p = (caddr_t) &JFS_IP(tlck->ip)->i_dtroot;
  338. else
  339. p = (caddr_t) &JFS_IP(tlck->ip)->i_xtroot;
  340. linelock = (struct linelock *) & tlck->lock;
  341. }
  342. else {
  343. jfs_err("lmWriteRecord: UFO tlck:0x%p", tlck);
  344. return 0; /* Probably should trap */
  345. }
  346. l2linesize = linelock->l2linesize;
  347. moveData:
  348. ASSERT(linelock->index <= linelock->maxcnt);
  349. lv = linelock->lv;
  350. for (i = 0; i < linelock->index; i++, lv++) {
  351. if (lv->length == 0)
  352. continue;
  353. /* is page full ? */
  354. if (dstoffset >= LOGPSIZE - LOGPTLRSIZE) {
  355. /* page become full: move on to next page */
  356. lmNextPage(log);
  357. bp = log->bp;
  358. lp = (struct logpage *) bp->l_ldata;
  359. dstoffset = LOGPHDRSIZE;
  360. }
  361. /*
  362. * move log vector data
  363. */
  364. src = (u8 *) p + (lv->offset << l2linesize);
  365. srclen = lv->length << l2linesize;
  366. len += srclen;
  367. while (srclen > 0) {
  368. freespace = (LOGPSIZE - LOGPTLRSIZE) - dstoffset;
  369. nbytes = min(freespace, srclen);
  370. dst = (caddr_t) lp + dstoffset;
  371. memcpy(dst, src, nbytes);
  372. dstoffset += nbytes;
  373. /* is page not full ? */
  374. if (dstoffset < LOGPSIZE - LOGPTLRSIZE)
  375. break;
  376. /* page become full: move on to next page */
  377. lmNextPage(log);
  378. bp = (struct lbuf *) log->bp;
  379. lp = (struct logpage *) bp->l_ldata;
  380. dstoffset = LOGPHDRSIZE;
  381. srclen -= nbytes;
  382. src += nbytes;
  383. }
  384. /*
  385. * move log vector descriptor
  386. */
  387. len += 4;
  388. lvd = (struct lvd *) ((caddr_t) lp + dstoffset);
  389. lvd->offset = cpu_to_le16(lv->offset);
  390. lvd->length = cpu_to_le16(lv->length);
  391. dstoffset += 4;
  392. jfs_info("lmWriteRecord: lv offset:%d length:%d",
  393. lv->offset, lv->length);
  394. }
  395. if ((i = linelock->next)) {
  396. linelock = (struct linelock *) lid_to_tlock(i);
  397. goto moveData;
  398. }
  399. /*
  400. * move log record descriptor
  401. */
  402. moveLrd:
  403. lrd->length = cpu_to_le16(len);
  404. src = (caddr_t) lrd;
  405. srclen = LOGRDSIZE;
  406. while (srclen > 0) {
  407. freespace = (LOGPSIZE - LOGPTLRSIZE) - dstoffset;
  408. nbytes = min(freespace, srclen);
  409. dst = (caddr_t) lp + dstoffset;
  410. memcpy(dst, src, nbytes);
  411. dstoffset += nbytes;
  412. srclen -= nbytes;
  413. /* are there more to move than freespace of page ? */
  414. if (srclen)
  415. goto pageFull;
  416. /*
  417. * end of log record descriptor
  418. */
  419. /* update last log record eor */
  420. log->eor = dstoffset;
  421. bp->l_eor = dstoffset;
  422. lsn = (log->page << L2LOGPSIZE) + dstoffset;
  423. if (lrd->type & cpu_to_le16(LOG_COMMIT)) {
  424. tblk->clsn = lsn;
  425. jfs_info("wr: tclsn:0x%x, beor:0x%x", tblk->clsn,
  426. bp->l_eor);
  427. INCREMENT(lmStat.commit); /* # of commit */
  428. /*
  429. * enqueue tblock for group commit:
  430. *
  431. * enqueue tblock of non-trivial/synchronous COMMIT
  432. * at tail of group commit queue
  433. * (trivial/asynchronous COMMITs are ignored by
  434. * group commit.)
  435. */
  436. LOGGC_LOCK(log);
  437. /* init tblock gc state */
  438. tblk->flag = tblkGC_QUEUE;
  439. tblk->bp = log->bp;
  440. tblk->pn = log->page;
  441. tblk->eor = log->eor;
  442. /* enqueue transaction to commit queue */
  443. list_add_tail(&tblk->cqueue, &log->cqueue);
  444. LOGGC_UNLOCK(log);
  445. }
  446. jfs_info("lmWriteRecord: lrd:0x%04x bp:0x%p pn:%d eor:0x%x",
  447. le16_to_cpu(lrd->type), log->bp, log->page, dstoffset);
  448. /* page not full ? */
  449. if (dstoffset < LOGPSIZE - LOGPTLRSIZE)
  450. return lsn;
  451. pageFull:
  452. /* page become full: move on to next page */
  453. lmNextPage(log);
  454. bp = (struct lbuf *) log->bp;
  455. lp = (struct logpage *) bp->l_ldata;
  456. dstoffset = LOGPHDRSIZE;
  457. src += nbytes;
  458. }
  459. return lsn;
  460. }
  461. /*
  462. * NAME: lmNextPage()
  463. *
  464. * FUNCTION: write current page and allocate next page.
  465. *
  466. * PARAMETER: log
  467. *
  468. * RETURN: 0
  469. *
  470. * serialization: LOG_LOCK() held on entry/exit
  471. */
  472. static int lmNextPage(struct jfs_log * log)
  473. {
  474. struct logpage *lp;
  475. int lspn; /* log sequence page number */
  476. int pn; /* current page number */
  477. struct lbuf *bp;
  478. struct lbuf *nextbp;
  479. struct tblock *tblk;
  480. /* get current log page number and log sequence page number */
  481. pn = log->page;
  482. bp = log->bp;
  483. lp = (struct logpage *) bp->l_ldata;
  484. lspn = le32_to_cpu(lp->h.page);
  485. LOGGC_LOCK(log);
  486. /*
  487. * write or queue the full page at the tail of write queue
  488. */
  489. /* get the tail tblk on commit queue */
  490. if (list_empty(&log->cqueue))
  491. tblk = NULL;
  492. else
  493. tblk = list_entry(log->cqueue.prev, struct tblock, cqueue);
  494. /* every tblk who has COMMIT record on the current page,
  495. * and has not been committed, must be on commit queue
  496. * since tblk is queued at commit queueu at the time
  497. * of writing its COMMIT record on the page before
  498. * page becomes full (even though the tblk thread
  499. * who wrote COMMIT record may have been suspended
  500. * currently);
  501. */
  502. /* is page bound with outstanding tail tblk ? */
  503. if (tblk && tblk->pn == pn) {
  504. /* mark tblk for end-of-page */
  505. tblk->flag |= tblkGC_EOP;
  506. if (log->cflag & logGC_PAGEOUT) {
  507. /* if page is not already on write queue,
  508. * just enqueue (no lbmWRITE to prevent redrive)
  509. * buffer to wqueue to ensure correct serial order
  510. * of the pages since log pages will be added
  511. * continuously
  512. */
  513. if (bp->l_wqnext == NULL)
  514. lbmWrite(log, bp, 0, 0);
  515. } else {
  516. /*
  517. * No current GC leader, initiate group commit
  518. */
  519. log->cflag |= logGC_PAGEOUT;
  520. lmGCwrite(log, 0);
  521. }
  522. }
  523. /* page is not bound with outstanding tblk:
  524. * init write or mark it to be redriven (lbmWRITE)
  525. */
  526. else {
  527. /* finalize the page */
  528. bp->l_ceor = bp->l_eor;
  529. lp->h.eor = lp->t.eor = cpu_to_le16(bp->l_ceor);
  530. lbmWrite(log, bp, lbmWRITE | lbmRELEASE | lbmFREE, 0);
  531. }
  532. LOGGC_UNLOCK(log);
  533. /*
  534. * allocate/initialize next page
  535. */
  536. /* if log wraps, the first data page of log is 2
  537. * (0 never used, 1 is superblock).
  538. */
  539. log->page = (pn == log->size - 1) ? 2 : pn + 1;
  540. log->eor = LOGPHDRSIZE; /* ? valid page empty/full at logRedo() */
  541. /* allocate/initialize next log page buffer */
  542. nextbp = lbmAllocate(log, log->page);
  543. nextbp->l_eor = log->eor;
  544. log->bp = nextbp;
  545. /* initialize next log page */
  546. lp = (struct logpage *) nextbp->l_ldata;
  547. lp->h.page = lp->t.page = cpu_to_le32(lspn + 1);
  548. lp->h.eor = lp->t.eor = cpu_to_le16(LOGPHDRSIZE);
  549. return 0;
  550. }
  551. /*
  552. * NAME: lmGroupCommit()
  553. *
  554. * FUNCTION: group commit
  555. * initiate pageout of the pages with COMMIT in the order of
  556. * page number - redrive pageout of the page at the head of
  557. * pageout queue until full page has been written.
  558. *
  559. * RETURN:
  560. *
  561. * NOTE:
  562. * LOGGC_LOCK serializes log group commit queue, and
  563. * transaction blocks on the commit queue.
  564. * N.B. LOG_LOCK is NOT held during lmGroupCommit().
  565. */
  566. int lmGroupCommit(struct jfs_log * log, struct tblock * tblk)
  567. {
  568. int rc = 0;
  569. LOGGC_LOCK(log);
  570. /* group committed already ? */
  571. if (tblk->flag & tblkGC_COMMITTED) {
  572. if (tblk->flag & tblkGC_ERROR)
  573. rc = -EIO;
  574. LOGGC_UNLOCK(log);
  575. return rc;
  576. }
  577. jfs_info("lmGroup Commit: tblk = 0x%p, gcrtc = %d", tblk, log->gcrtc);
  578. if (tblk->xflag & COMMIT_LAZY)
  579. tblk->flag |= tblkGC_LAZY;
  580. if ((!(log->cflag & logGC_PAGEOUT)) && (!list_empty(&log->cqueue)) &&
  581. (!(tblk->xflag & COMMIT_LAZY) || test_bit(log_FLUSH, &log->flag)
  582. || jfs_tlocks_low)) {
  583. /*
  584. * No pageout in progress
  585. *
  586. * start group commit as its group leader.
  587. */
  588. log->cflag |= logGC_PAGEOUT;
  589. lmGCwrite(log, 0);
  590. }
  591. if (tblk->xflag & COMMIT_LAZY) {
  592. /*
  593. * Lazy transactions can leave now
  594. */
  595. LOGGC_UNLOCK(log);
  596. return 0;
  597. }
  598. /* lmGCwrite gives up LOGGC_LOCK, check again */
  599. if (tblk->flag & tblkGC_COMMITTED) {
  600. if (tblk->flag & tblkGC_ERROR)
  601. rc = -EIO;
  602. LOGGC_UNLOCK(log);
  603. return rc;
  604. }
  605. /* upcount transaction waiting for completion
  606. */
  607. log->gcrtc++;
  608. tblk->flag |= tblkGC_READY;
  609. __SLEEP_COND(tblk->gcwait, (tblk->flag & tblkGC_COMMITTED),
  610. LOGGC_LOCK(log), LOGGC_UNLOCK(log));
  611. /* removed from commit queue */
  612. if (tblk->flag & tblkGC_ERROR)
  613. rc = -EIO;
  614. LOGGC_UNLOCK(log);
  615. return rc;
  616. }
  617. /*
  618. * NAME: lmGCwrite()
  619. *
  620. * FUNCTION: group commit write
  621. * initiate write of log page, building a group of all transactions
  622. * with commit records on that page.
  623. *
  624. * RETURN: None
  625. *
  626. * NOTE:
  627. * LOGGC_LOCK must be held by caller.
  628. * N.B. LOG_LOCK is NOT held during lmGroupCommit().
  629. */
  630. static void lmGCwrite(struct jfs_log * log, int cant_write)
  631. {
  632. struct lbuf *bp;
  633. struct logpage *lp;
  634. int gcpn; /* group commit page number */
  635. struct tblock *tblk;
  636. struct tblock *xtblk = NULL;
  637. /*
  638. * build the commit group of a log page
  639. *
  640. * scan commit queue and make a commit group of all
  641. * transactions with COMMIT records on the same log page.
  642. */
  643. /* get the head tblk on the commit queue */
  644. gcpn = list_entry(log->cqueue.next, struct tblock, cqueue)->pn;
  645. list_for_each_entry(tblk, &log->cqueue, cqueue) {
  646. if (tblk->pn != gcpn)
  647. break;
  648. xtblk = tblk;
  649. /* state transition: (QUEUE, READY) -> COMMIT */
  650. tblk->flag |= tblkGC_COMMIT;
  651. }
  652. tblk = xtblk; /* last tblk of the page */
  653. /*
  654. * pageout to commit transactions on the log page.
  655. */
  656. bp = (struct lbuf *) tblk->bp;
  657. lp = (struct logpage *) bp->l_ldata;
  658. /* is page already full ? */
  659. if (tblk->flag & tblkGC_EOP) {
  660. /* mark page to free at end of group commit of the page */
  661. tblk->flag &= ~tblkGC_EOP;
  662. tblk->flag |= tblkGC_FREE;
  663. bp->l_ceor = bp->l_eor;
  664. lp->h.eor = lp->t.eor = cpu_to_le16(bp->l_ceor);
  665. lbmWrite(log, bp, lbmWRITE | lbmRELEASE | lbmGC,
  666. cant_write);
  667. INCREMENT(lmStat.full_page);
  668. }
  669. /* page is not yet full */
  670. else {
  671. bp->l_ceor = tblk->eor; /* ? bp->l_ceor = bp->l_eor; */
  672. lp->h.eor = lp->t.eor = cpu_to_le16(bp->l_ceor);
  673. lbmWrite(log, bp, lbmWRITE | lbmGC, cant_write);
  674. INCREMENT(lmStat.partial_page);
  675. }
  676. }
  677. /*
  678. * NAME: lmPostGC()
  679. *
  680. * FUNCTION: group commit post-processing
  681. * Processes transactions after their commit records have been written
  682. * to disk, redriving log I/O if necessary.
  683. *
  684. * RETURN: None
  685. *
  686. * NOTE:
  687. * This routine is called a interrupt time by lbmIODone
  688. */
  689. static void lmPostGC(struct lbuf * bp)
  690. {
  691. unsigned long flags;
  692. struct jfs_log *log = bp->l_log;
  693. struct logpage *lp;
  694. struct tblock *tblk, *temp;
  695. //LOGGC_LOCK(log);
  696. spin_lock_irqsave(&log->gclock, flags);
  697. /*
  698. * current pageout of group commit completed.
  699. *
  700. * remove/wakeup transactions from commit queue who were
  701. * group committed with the current log page
  702. */
  703. list_for_each_entry_safe(tblk, temp, &log->cqueue, cqueue) {
  704. if (!(tblk->flag & tblkGC_COMMIT))
  705. break;
  706. /* if transaction was marked GC_COMMIT then
  707. * it has been shipped in the current pageout
  708. * and made it to disk - it is committed.
  709. */
  710. if (bp->l_flag & lbmERROR)
  711. tblk->flag |= tblkGC_ERROR;
  712. /* remove it from the commit queue */
  713. list_del(&tblk->cqueue);
  714. tblk->flag &= ~tblkGC_QUEUE;
  715. if (tblk == log->flush_tblk) {
  716. /* we can stop flushing the log now */
  717. clear_bit(log_FLUSH, &log->flag);
  718. log->flush_tblk = NULL;
  719. }
  720. jfs_info("lmPostGC: tblk = 0x%p, flag = 0x%x", tblk,
  721. tblk->flag);
  722. if (!(tblk->xflag & COMMIT_FORCE))
  723. /*
  724. * Hand tblk over to lazy commit thread
  725. */
  726. txLazyUnlock(tblk);
  727. else {
  728. /* state transition: COMMIT -> COMMITTED */
  729. tblk->flag |= tblkGC_COMMITTED;
  730. if (tblk->flag & tblkGC_READY)
  731. log->gcrtc--;
  732. LOGGC_WAKEUP(tblk);
  733. }
  734. /* was page full before pageout ?
  735. * (and this is the last tblk bound with the page)
  736. */
  737. if (tblk->flag & tblkGC_FREE)
  738. lbmFree(bp);
  739. /* did page become full after pageout ?
  740. * (and this is the last tblk bound with the page)
  741. */
  742. else if (tblk->flag & tblkGC_EOP) {
  743. /* finalize the page */
  744. lp = (struct logpage *) bp->l_ldata;
  745. bp->l_ceor = bp->l_eor;
  746. lp->h.eor = lp->t.eor = cpu_to_le16(bp->l_eor);
  747. jfs_info("lmPostGC: calling lbmWrite");
  748. lbmWrite(log, bp, lbmWRITE | lbmRELEASE | lbmFREE,
  749. 1);
  750. }
  751. }
  752. /* are there any transactions who have entered lnGroupCommit()
  753. * (whose COMMITs are after that of the last log page written.
  754. * They are waiting for new group commit (above at (SLEEP 1))
  755. * or lazy transactions are on a full (queued) log page,
  756. * select the latest ready transaction as new group leader and
  757. * wake her up to lead her group.
  758. */
  759. if ((!list_empty(&log->cqueue)) &&
  760. ((log->gcrtc > 0) || (tblk->bp->l_wqnext != NULL) ||
  761. test_bit(log_FLUSH, &log->flag) || jfs_tlocks_low))
  762. /*
  763. * Call lmGCwrite with new group leader
  764. */
  765. lmGCwrite(log, 1);
  766. /* no transaction are ready yet (transactions are only just
  767. * queued (GC_QUEUE) and not entered for group commit yet).
  768. * the first transaction entering group commit
  769. * will elect herself as new group leader.
  770. */
  771. else
  772. log->cflag &= ~logGC_PAGEOUT;
  773. //LOGGC_UNLOCK(log);
  774. spin_unlock_irqrestore(&log->gclock, flags);
  775. return;
  776. }
  777. /*
  778. * NAME: lmLogSync()
  779. *
  780. * FUNCTION: write log SYNCPT record for specified log
  781. * if new sync address is available
  782. * (normally the case if sync() is executed by back-ground
  783. * process).
  784. * calculate new value of i_nextsync which determines when
  785. * this code is called again.
  786. *
  787. * PARAMETERS: log - log structure
  788. * hard_sync - 1 to force all metadata to be written
  789. *
  790. * RETURN: 0
  791. *
  792. * serialization: LOG_LOCK() held on entry/exit
  793. */
  794. static int lmLogSync(struct jfs_log * log, int hard_sync)
  795. {
  796. int logsize;
  797. int written; /* written since last syncpt */
  798. int free; /* free space left available */
  799. int delta; /* additional delta to write normally */
  800. int more; /* additional write granted */
  801. struct lrd lrd;
  802. int lsn;
  803. struct logsyncblk *lp;
  804. unsigned long flags;
  805. /* push dirty metapages out to disk */
  806. if (hard_sync)
  807. write_special_inodes(log, filemap_fdatawrite);
  808. else
  809. write_special_inodes(log, filemap_flush);
  810. /*
  811. * forward syncpt
  812. */
  813. /* if last sync is same as last syncpt,
  814. * invoke sync point forward processing to update sync.
  815. */
  816. if (log->sync == log->syncpt) {
  817. LOGSYNC_LOCK(log, flags);
  818. if (list_empty(&log->synclist))
  819. log->sync = log->lsn;
  820. else {
  821. lp = list_entry(log->synclist.next,
  822. struct logsyncblk, synclist);
  823. log->sync = lp->lsn;
  824. }
  825. LOGSYNC_UNLOCK(log, flags);
  826. }
  827. /* if sync is different from last syncpt,
  828. * write a SYNCPT record with syncpt = sync.
  829. * reset syncpt = sync
  830. */
  831. if (log->sync != log->syncpt) {
  832. lrd.logtid = 0;
  833. lrd.backchain = 0;
  834. lrd.type = cpu_to_le16(LOG_SYNCPT);
  835. lrd.length = 0;
  836. lrd.log.syncpt.sync = cpu_to_le32(log->sync);
  837. lsn = lmWriteRecord(log, NULL, &lrd, NULL);
  838. log->syncpt = log->sync;
  839. } else
  840. lsn = log->lsn;
  841. /*
  842. * setup next syncpt trigger (SWAG)
  843. */
  844. logsize = log->logsize;
  845. logdiff(written, lsn, log);
  846. free = logsize - written;
  847. delta = LOGSYNC_DELTA(logsize);
  848. more = min(free / 2, delta);
  849. if (more < 2 * LOGPSIZE) {
  850. jfs_warn("\n ... Log Wrap ... Log Wrap ... Log Wrap ...\n");
  851. /*
  852. * log wrapping
  853. *
  854. * option 1 - panic ? No.!
  855. * option 2 - shutdown file systems
  856. * associated with log ?
  857. * option 3 - extend log ?
  858. * option 4 - second chance
  859. *
  860. * mark log wrapped, and continue.
  861. * when all active transactions are completed,
  862. * mark log valid for recovery.
  863. * if crashed during invalid state, log state
  864. * implies invalid log, forcing fsck().
  865. */
  866. /* mark log state log wrap in log superblock */
  867. /* log->state = LOGWRAP; */
  868. /* reset sync point computation */
  869. log->syncpt = log->sync = lsn;
  870. log->nextsync = delta;
  871. } else
  872. /* next syncpt trigger = written + more */
  873. log->nextsync = written + more;
  874. /* if number of bytes written from last sync point is more
  875. * than 1/4 of the log size, stop new transactions from
  876. * starting until all current transactions are completed
  877. * by setting syncbarrier flag.
  878. */
  879. if (!test_bit(log_SYNCBARRIER, &log->flag) &&
  880. (written > LOGSYNC_BARRIER(logsize)) && log->active) {
  881. set_bit(log_SYNCBARRIER, &log->flag);
  882. jfs_info("log barrier on: lsn=0x%x syncpt=0x%x", lsn,
  883. log->syncpt);
  884. /*
  885. * We may have to initiate group commit
  886. */
  887. jfs_flush_journal(log, 0);
  888. }
  889. return lsn;
  890. }
  891. /*
  892. * NAME: jfs_syncpt
  893. *
  894. * FUNCTION: write log SYNCPT record for specified log
  895. *
  896. * PARAMETERS: log - log structure
  897. * hard_sync - set to 1 to force metadata to be written
  898. */
  899. void jfs_syncpt(struct jfs_log *log, int hard_sync)
  900. { LOG_LOCK(log);
  901. if (!test_bit(log_QUIESCE, &log->flag))
  902. lmLogSync(log, hard_sync);
  903. LOG_UNLOCK(log);
  904. }
  905. /*
  906. * NAME: lmLogOpen()
  907. *
  908. * FUNCTION: open the log on first open;
  909. * insert filesystem in the active list of the log.
  910. *
  911. * PARAMETER: ipmnt - file system mount inode
  912. * iplog - log inode (out)
  913. *
  914. * RETURN:
  915. *
  916. * serialization:
  917. */
  918. int lmLogOpen(struct super_block *sb)
  919. {
  920. int rc;
  921. struct file *bdev_file;
  922. struct jfs_log *log;
  923. struct jfs_sb_info *sbi = JFS_SBI(sb);
  924. if (sbi->flag & JFS_NOINTEGRITY)
  925. return open_dummy_log(sb);
  926. if (sbi->mntflag & JFS_INLINELOG)
  927. return open_inline_log(sb);
  928. mutex_lock(&jfs_log_mutex);
  929. list_for_each_entry(log, &jfs_external_logs, journal_list) {
  930. if (file_bdev(log->bdev_file)->bd_dev == sbi->logdev) {
  931. if (!uuid_equal(&log->uuid, &sbi->loguuid)) {
  932. jfs_warn("wrong uuid on JFS journal");
  933. mutex_unlock(&jfs_log_mutex);
  934. return -EINVAL;
  935. }
  936. /*
  937. * add file system to log active file system list
  938. */
  939. if ((rc = lmLogFileSystem(log, sbi, 1))) {
  940. mutex_unlock(&jfs_log_mutex);
  941. return rc;
  942. }
  943. goto journal_found;
  944. }
  945. }
  946. if (!(log = kzalloc_obj(struct jfs_log))) {
  947. mutex_unlock(&jfs_log_mutex);
  948. return -ENOMEM;
  949. }
  950. INIT_LIST_HEAD(&log->sb_list);
  951. init_waitqueue_head(&log->syncwait);
  952. /*
  953. * external log as separate logical volume
  954. *
  955. * file systems to log may have n-to-1 relationship;
  956. */
  957. bdev_file = bdev_file_open_by_dev(sbi->logdev,
  958. BLK_OPEN_READ | BLK_OPEN_WRITE, log, NULL);
  959. if (IS_ERR(bdev_file)) {
  960. rc = PTR_ERR(bdev_file);
  961. goto free;
  962. }
  963. log->bdev_file = bdev_file;
  964. uuid_copy(&log->uuid, &sbi->loguuid);
  965. /*
  966. * initialize log:
  967. */
  968. if ((rc = lmLogInit(log)))
  969. goto close;
  970. list_add(&log->journal_list, &jfs_external_logs);
  971. /*
  972. * add file system to log active file system list
  973. */
  974. if ((rc = lmLogFileSystem(log, sbi, 1)))
  975. goto shutdown;
  976. journal_found:
  977. LOG_LOCK(log);
  978. list_add(&sbi->log_list, &log->sb_list);
  979. sbi->log = log;
  980. LOG_UNLOCK(log);
  981. mutex_unlock(&jfs_log_mutex);
  982. return 0;
  983. /*
  984. * unwind on error
  985. */
  986. shutdown: /* unwind lbmLogInit() */
  987. list_del(&log->journal_list);
  988. lbmLogShutdown(log);
  989. close: /* close external log device */
  990. bdev_fput(bdev_file);
  991. free: /* free log descriptor */
  992. mutex_unlock(&jfs_log_mutex);
  993. kfree(log);
  994. jfs_warn("lmLogOpen: exit(%d)", rc);
  995. return rc;
  996. }
  997. static int open_inline_log(struct super_block *sb)
  998. {
  999. struct jfs_log *log;
  1000. int rc;
  1001. if (!(log = kzalloc_obj(struct jfs_log)))
  1002. return -ENOMEM;
  1003. INIT_LIST_HEAD(&log->sb_list);
  1004. init_waitqueue_head(&log->syncwait);
  1005. set_bit(log_INLINELOG, &log->flag);
  1006. log->bdev_file = sb->s_bdev_file;
  1007. log->base = addressPXD(&JFS_SBI(sb)->logpxd);
  1008. log->size = lengthPXD(&JFS_SBI(sb)->logpxd) >>
  1009. (L2LOGPSIZE - sb->s_blocksize_bits);
  1010. log->l2bsize = sb->s_blocksize_bits;
  1011. ASSERT(L2LOGPSIZE >= sb->s_blocksize_bits);
  1012. /*
  1013. * initialize log.
  1014. */
  1015. if ((rc = lmLogInit(log))) {
  1016. kfree(log);
  1017. jfs_warn("lmLogOpen: exit(%d)", rc);
  1018. return rc;
  1019. }
  1020. list_add(&JFS_SBI(sb)->log_list, &log->sb_list);
  1021. JFS_SBI(sb)->log = log;
  1022. return rc;
  1023. }
  1024. static int open_dummy_log(struct super_block *sb)
  1025. {
  1026. int rc;
  1027. mutex_lock(&jfs_log_mutex);
  1028. if (!dummy_log) {
  1029. dummy_log = kzalloc_obj(struct jfs_log);
  1030. if (!dummy_log) {
  1031. mutex_unlock(&jfs_log_mutex);
  1032. return -ENOMEM;
  1033. }
  1034. INIT_LIST_HEAD(&dummy_log->sb_list);
  1035. init_waitqueue_head(&dummy_log->syncwait);
  1036. dummy_log->no_integrity = 1;
  1037. /* Make up some stuff */
  1038. dummy_log->size = 1024;
  1039. rc = lmLogInit(dummy_log);
  1040. if (rc) {
  1041. kfree(dummy_log);
  1042. dummy_log = NULL;
  1043. mutex_unlock(&jfs_log_mutex);
  1044. return rc;
  1045. }
  1046. }
  1047. LOG_LOCK(dummy_log);
  1048. list_add(&JFS_SBI(sb)->log_list, &dummy_log->sb_list);
  1049. JFS_SBI(sb)->log = dummy_log;
  1050. LOG_UNLOCK(dummy_log);
  1051. mutex_unlock(&jfs_log_mutex);
  1052. return 0;
  1053. }
  1054. /*
  1055. * NAME: lmLogInit()
  1056. *
  1057. * FUNCTION: log initialization at first log open.
  1058. *
  1059. * logredo() (or logformat()) should have been run previously.
  1060. * initialize the log from log superblock.
  1061. * set the log state in the superblock to LOGMOUNT and
  1062. * write SYNCPT log record.
  1063. *
  1064. * PARAMETER: log - log structure
  1065. *
  1066. * RETURN: 0 - if ok
  1067. * -EINVAL - bad log magic number or superblock dirty
  1068. * error returned from logwait()
  1069. *
  1070. * serialization: single first open thread
  1071. */
  1072. int lmLogInit(struct jfs_log * log)
  1073. {
  1074. int rc = 0;
  1075. struct lrd lrd;
  1076. struct logsuper *logsuper;
  1077. struct lbuf *bpsuper;
  1078. struct lbuf *bp;
  1079. struct logpage *lp;
  1080. int lsn = 0;
  1081. jfs_info("lmLogInit: log:0x%p", log);
  1082. /* initialize the group commit serialization lock */
  1083. LOGGC_LOCK_INIT(log);
  1084. /* allocate/initialize the log write serialization lock */
  1085. LOG_LOCK_INIT(log);
  1086. LOGSYNC_LOCK_INIT(log);
  1087. INIT_LIST_HEAD(&log->synclist);
  1088. INIT_LIST_HEAD(&log->cqueue);
  1089. log->flush_tblk = NULL;
  1090. log->count = 0;
  1091. /*
  1092. * initialize log i/o
  1093. */
  1094. if ((rc = lbmLogInit(log)))
  1095. return rc;
  1096. if (!test_bit(log_INLINELOG, &log->flag))
  1097. log->l2bsize = L2LOGPSIZE;
  1098. /* check for disabled journaling to disk */
  1099. if (log->no_integrity) {
  1100. /*
  1101. * Journal pages will still be filled. When the time comes
  1102. * to actually do the I/O, the write is not done, and the
  1103. * endio routine is called directly.
  1104. */
  1105. bp = lbmAllocate(log , 0);
  1106. log->bp = bp;
  1107. bp->l_pn = bp->l_eor = 0;
  1108. } else {
  1109. /*
  1110. * validate log superblock
  1111. */
  1112. if ((rc = lbmRead(log, 1, &bpsuper)))
  1113. goto errout10;
  1114. logsuper = (struct logsuper *) bpsuper->l_ldata;
  1115. if (logsuper->magic != cpu_to_le32(LOGMAGIC)) {
  1116. jfs_warn("*** Log Format Error ! ***");
  1117. rc = -EINVAL;
  1118. goto errout20;
  1119. }
  1120. /* logredo() should have been run successfully. */
  1121. if (logsuper->state != cpu_to_le32(LOGREDONE)) {
  1122. jfs_warn("*** Log Is Dirty ! ***");
  1123. rc = -EINVAL;
  1124. goto errout20;
  1125. }
  1126. /* initialize log from log superblock */
  1127. if (test_bit(log_INLINELOG,&log->flag)) {
  1128. if (log->size != le32_to_cpu(logsuper->size)) {
  1129. rc = -EINVAL;
  1130. goto errout20;
  1131. }
  1132. jfs_info("lmLogInit: inline log:0x%p base:0x%Lx size:0x%x",
  1133. log, (unsigned long long)log->base, log->size);
  1134. } else {
  1135. if (!uuid_equal(&logsuper->uuid, &log->uuid)) {
  1136. jfs_warn("wrong uuid on JFS log device");
  1137. rc = -EINVAL;
  1138. goto errout20;
  1139. }
  1140. log->size = le32_to_cpu(logsuper->size);
  1141. log->l2bsize = le32_to_cpu(logsuper->l2bsize);
  1142. jfs_info("lmLogInit: external log:0x%p base:0x%Lx size:0x%x",
  1143. log, (unsigned long long)log->base, log->size);
  1144. }
  1145. log->page = le32_to_cpu(logsuper->end) / LOGPSIZE;
  1146. log->eor = le32_to_cpu(logsuper->end) - (LOGPSIZE * log->page);
  1147. /*
  1148. * initialize for log append write mode
  1149. */
  1150. /* establish current/end-of-log page/buffer */
  1151. if ((rc = lbmRead(log, log->page, &bp)))
  1152. goto errout20;
  1153. lp = (struct logpage *) bp->l_ldata;
  1154. jfs_info("lmLogInit: lsn:0x%x page:%d eor:%d:%d",
  1155. le32_to_cpu(logsuper->end), log->page, log->eor,
  1156. le16_to_cpu(lp->h.eor));
  1157. log->bp = bp;
  1158. bp->l_pn = log->page;
  1159. bp->l_eor = log->eor;
  1160. /* if current page is full, move on to next page */
  1161. if (log->eor >= LOGPSIZE - LOGPTLRSIZE)
  1162. lmNextPage(log);
  1163. /*
  1164. * initialize log syncpoint
  1165. */
  1166. /*
  1167. * write the first SYNCPT record with syncpoint = 0
  1168. * (i.e., log redo up to HERE !);
  1169. * remove current page from lbm write queue at end of pageout
  1170. * (to write log superblock update), but do not release to
  1171. * freelist;
  1172. */
  1173. lrd.logtid = 0;
  1174. lrd.backchain = 0;
  1175. lrd.type = cpu_to_le16(LOG_SYNCPT);
  1176. lrd.length = 0;
  1177. lrd.log.syncpt.sync = 0;
  1178. lsn = lmWriteRecord(log, NULL, &lrd, NULL);
  1179. bp = log->bp;
  1180. bp->l_ceor = bp->l_eor;
  1181. lp = (struct logpage *) bp->l_ldata;
  1182. lp->h.eor = lp->t.eor = cpu_to_le16(bp->l_eor);
  1183. lbmWrite(log, bp, lbmWRITE | lbmSYNC, 0);
  1184. if ((rc = lbmIOWait(bp, 0)))
  1185. goto errout30;
  1186. /*
  1187. * update/write superblock
  1188. */
  1189. logsuper->state = cpu_to_le32(LOGMOUNT);
  1190. log->serial = le32_to_cpu(logsuper->serial) + 1;
  1191. logsuper->serial = cpu_to_le32(log->serial);
  1192. lbmDirectWrite(log, bpsuper, lbmWRITE | lbmRELEASE | lbmSYNC);
  1193. if ((rc = lbmIOWait(bpsuper, lbmFREE)))
  1194. goto errout30;
  1195. }
  1196. /* initialize logsync parameters */
  1197. log->logsize = (log->size - 2) << L2LOGPSIZE;
  1198. log->lsn = lsn;
  1199. log->syncpt = lsn;
  1200. log->sync = log->syncpt;
  1201. log->nextsync = LOGSYNC_DELTA(log->logsize);
  1202. jfs_info("lmLogInit: lsn:0x%x syncpt:0x%x sync:0x%x",
  1203. log->lsn, log->syncpt, log->sync);
  1204. /*
  1205. * initialize for lazy/group commit
  1206. */
  1207. log->clsn = lsn;
  1208. return 0;
  1209. /*
  1210. * unwind on error
  1211. */
  1212. errout30: /* release log page */
  1213. log->wqueue = NULL;
  1214. bp->l_wqnext = NULL;
  1215. lbmFree(bp);
  1216. errout20: /* release log superblock */
  1217. lbmFree(bpsuper);
  1218. errout10: /* unwind lbmLogInit() */
  1219. lbmLogShutdown(log);
  1220. jfs_warn("lmLogInit: exit(%d)", rc);
  1221. return rc;
  1222. }
  1223. /*
  1224. * NAME: lmLogClose()
  1225. *
  1226. * FUNCTION: remove file system <ipmnt> from active list of log <iplog>
  1227. * and close it on last close.
  1228. *
  1229. * PARAMETER: sb - superblock
  1230. *
  1231. * RETURN: errors from subroutines
  1232. *
  1233. * serialization:
  1234. */
  1235. int lmLogClose(struct super_block *sb)
  1236. {
  1237. struct jfs_sb_info *sbi = JFS_SBI(sb);
  1238. struct jfs_log *log = sbi->log;
  1239. struct file *bdev_file;
  1240. int rc = 0;
  1241. jfs_info("lmLogClose: log:0x%p", log);
  1242. mutex_lock(&jfs_log_mutex);
  1243. LOG_LOCK(log);
  1244. list_del(&sbi->log_list);
  1245. LOG_UNLOCK(log);
  1246. sbi->log = NULL;
  1247. /*
  1248. * We need to make sure all of the "written" metapages
  1249. * actually make it to disk
  1250. */
  1251. sync_blockdev(sb->s_bdev);
  1252. if (test_bit(log_INLINELOG, &log->flag)) {
  1253. /*
  1254. * in-line log in host file system
  1255. */
  1256. rc = lmLogShutdown(log);
  1257. kfree(log);
  1258. goto out;
  1259. }
  1260. if (!log->no_integrity)
  1261. lmLogFileSystem(log, sbi, 0);
  1262. if (!list_empty(&log->sb_list))
  1263. goto out;
  1264. /*
  1265. * TODO: ensure that the dummy_log is in a state to allow
  1266. * lbmLogShutdown to deallocate all the buffers and call
  1267. * kfree against dummy_log. For now, leave dummy_log & its
  1268. * buffers in memory, and resuse if another no-integrity mount
  1269. * is requested.
  1270. */
  1271. if (log->no_integrity)
  1272. goto out;
  1273. /*
  1274. * external log as separate logical volume
  1275. */
  1276. list_del(&log->journal_list);
  1277. bdev_file = log->bdev_file;
  1278. rc = lmLogShutdown(log);
  1279. bdev_fput(bdev_file);
  1280. kfree(log);
  1281. out:
  1282. mutex_unlock(&jfs_log_mutex);
  1283. jfs_info("lmLogClose: exit(%d)", rc);
  1284. return rc;
  1285. }
  1286. /*
  1287. * NAME: jfs_flush_journal()
  1288. *
  1289. * FUNCTION: initiate write of any outstanding transactions to the journal
  1290. * and optionally wait until they are all written to disk
  1291. *
  1292. * wait == 0 flush until latest txn is committed, don't wait
  1293. * wait == 1 flush until latest txn is committed, wait
  1294. * wait > 1 flush until all txn's are complete, wait
  1295. */
  1296. void jfs_flush_journal(struct jfs_log *log, int wait)
  1297. {
  1298. int i;
  1299. struct tblock *target = NULL;
  1300. /* jfs_write_inode may call us during read-only mount */
  1301. if (!log)
  1302. return;
  1303. jfs_info("jfs_flush_journal: log:0x%p wait=%d", log, wait);
  1304. LOGGC_LOCK(log);
  1305. if (!list_empty(&log->cqueue)) {
  1306. /*
  1307. * This ensures that we will keep writing to the journal as long
  1308. * as there are unwritten commit records
  1309. */
  1310. target = list_entry(log->cqueue.prev, struct tblock, cqueue);
  1311. if (test_bit(log_FLUSH, &log->flag)) {
  1312. /*
  1313. * We're already flushing.
  1314. * if flush_tblk is NULL, we are flushing everything,
  1315. * so leave it that way. Otherwise, update it to the
  1316. * latest transaction
  1317. */
  1318. if (log->flush_tblk)
  1319. log->flush_tblk = target;
  1320. } else {
  1321. /* Only flush until latest transaction is committed */
  1322. log->flush_tblk = target;
  1323. set_bit(log_FLUSH, &log->flag);
  1324. /*
  1325. * Initiate I/O on outstanding transactions
  1326. */
  1327. if (!(log->cflag & logGC_PAGEOUT)) {
  1328. log->cflag |= logGC_PAGEOUT;
  1329. lmGCwrite(log, 0);
  1330. }
  1331. }
  1332. }
  1333. if ((wait > 1) || test_bit(log_SYNCBARRIER, &log->flag)) {
  1334. /* Flush until all activity complete */
  1335. set_bit(log_FLUSH, &log->flag);
  1336. log->flush_tblk = NULL;
  1337. }
  1338. if (wait && target && !(target->flag & tblkGC_COMMITTED)) {
  1339. DECLARE_WAITQUEUE(__wait, current);
  1340. add_wait_queue(&target->gcwait, &__wait);
  1341. set_current_state(TASK_UNINTERRUPTIBLE);
  1342. LOGGC_UNLOCK(log);
  1343. schedule();
  1344. LOGGC_LOCK(log);
  1345. remove_wait_queue(&target->gcwait, &__wait);
  1346. }
  1347. LOGGC_UNLOCK(log);
  1348. if (wait < 2)
  1349. return;
  1350. write_special_inodes(log, filemap_fdatawrite);
  1351. /*
  1352. * If there was recent activity, we may need to wait
  1353. * for the lazycommit thread to catch up
  1354. */
  1355. if ((!list_empty(&log->cqueue)) || !list_empty(&log->synclist)) {
  1356. for (i = 0; i < 200; i++) { /* Too much? */
  1357. msleep(250);
  1358. write_special_inodes(log, filemap_fdatawrite);
  1359. if (list_empty(&log->cqueue) &&
  1360. list_empty(&log->synclist))
  1361. break;
  1362. }
  1363. }
  1364. assert(list_empty(&log->cqueue));
  1365. #ifdef CONFIG_JFS_DEBUG
  1366. if (!list_empty(&log->synclist)) {
  1367. struct logsyncblk *lp;
  1368. printk(KERN_ERR "jfs_flush_journal: synclist not empty\n");
  1369. list_for_each_entry(lp, &log->synclist, synclist) {
  1370. if (lp->xflag & COMMIT_PAGE) {
  1371. struct metapage *mp = (struct metapage *)lp;
  1372. print_hex_dump(KERN_ERR, "metapage: ",
  1373. DUMP_PREFIX_ADDRESS, 16, 4,
  1374. mp, sizeof(struct metapage), 0);
  1375. print_hex_dump(KERN_ERR, "page: ",
  1376. DUMP_PREFIX_ADDRESS, 16,
  1377. sizeof(long), mp->folio,
  1378. sizeof(struct page), 0);
  1379. } else
  1380. print_hex_dump(KERN_ERR, "tblock:",
  1381. DUMP_PREFIX_ADDRESS, 16, 4,
  1382. lp, sizeof(struct tblock), 0);
  1383. }
  1384. }
  1385. #else
  1386. WARN_ON(!list_empty(&log->synclist));
  1387. #endif
  1388. clear_bit(log_FLUSH, &log->flag);
  1389. }
  1390. /*
  1391. * NAME: lmLogShutdown()
  1392. *
  1393. * FUNCTION: log shutdown at last LogClose().
  1394. *
  1395. * write log syncpt record.
  1396. * update super block to set redone flag to 0.
  1397. *
  1398. * PARAMETER: log - log inode
  1399. *
  1400. * RETURN: 0 - success
  1401. *
  1402. * serialization: single last close thread
  1403. */
  1404. int lmLogShutdown(struct jfs_log * log)
  1405. {
  1406. int rc;
  1407. struct lrd lrd;
  1408. int lsn;
  1409. struct logsuper *logsuper;
  1410. struct lbuf *bpsuper;
  1411. struct lbuf *bp;
  1412. struct logpage *lp;
  1413. jfs_info("lmLogShutdown: log:0x%p", log);
  1414. jfs_flush_journal(log, 2);
  1415. /*
  1416. * write the last SYNCPT record with syncpoint = 0
  1417. * (i.e., log redo up to HERE !)
  1418. */
  1419. lrd.logtid = 0;
  1420. lrd.backchain = 0;
  1421. lrd.type = cpu_to_le16(LOG_SYNCPT);
  1422. lrd.length = 0;
  1423. lrd.log.syncpt.sync = 0;
  1424. lsn = lmWriteRecord(log, NULL, &lrd, NULL);
  1425. bp = log->bp;
  1426. lp = (struct logpage *) bp->l_ldata;
  1427. lp->h.eor = lp->t.eor = cpu_to_le16(bp->l_eor);
  1428. lbmWrite(log, log->bp, lbmWRITE | lbmRELEASE | lbmSYNC, 0);
  1429. lbmIOWait(log->bp, lbmFREE);
  1430. log->bp = NULL;
  1431. /*
  1432. * synchronous update log superblock
  1433. * mark log state as shutdown cleanly
  1434. * (i.e., Log does not need to be replayed).
  1435. */
  1436. if ((rc = lbmRead(log, 1, &bpsuper)))
  1437. goto out;
  1438. logsuper = (struct logsuper *) bpsuper->l_ldata;
  1439. logsuper->state = cpu_to_le32(LOGREDONE);
  1440. logsuper->end = cpu_to_le32(lsn);
  1441. lbmDirectWrite(log, bpsuper, lbmWRITE | lbmRELEASE | lbmSYNC);
  1442. rc = lbmIOWait(bpsuper, lbmFREE);
  1443. jfs_info("lmLogShutdown: lsn:0x%x page:%d eor:%d",
  1444. lsn, log->page, log->eor);
  1445. out:
  1446. /*
  1447. * shutdown per log i/o
  1448. */
  1449. lbmLogShutdown(log);
  1450. if (rc) {
  1451. jfs_warn("lmLogShutdown: exit(%d)", rc);
  1452. }
  1453. return rc;
  1454. }
  1455. /*
  1456. * NAME: lmLogFileSystem()
  1457. *
  1458. * FUNCTION: insert (<activate> = true)/remove (<activate> = false)
  1459. * file system into/from log active file system list.
  1460. *
  1461. * PARAMETE: log - pointer to logs inode.
  1462. * fsdev - kdev_t of filesystem.
  1463. * serial - pointer to returned log serial number
  1464. * activate - insert/remove device from active list.
  1465. *
  1466. * RETURN: 0 - success
  1467. * errors returned by vms_iowait().
  1468. */
  1469. static int lmLogFileSystem(struct jfs_log * log, struct jfs_sb_info *sbi,
  1470. int activate)
  1471. {
  1472. int rc = 0;
  1473. int i;
  1474. struct logsuper *logsuper;
  1475. struct lbuf *bpsuper;
  1476. uuid_t *uuid = &sbi->uuid;
  1477. /*
  1478. * insert/remove file system device to log active file system list.
  1479. */
  1480. if ((rc = lbmRead(log, 1, &bpsuper)))
  1481. return rc;
  1482. logsuper = (struct logsuper *) bpsuper->l_ldata;
  1483. if (activate) {
  1484. for (i = 0; i < MAX_ACTIVE; i++)
  1485. if (uuid_is_null(&logsuper->active[i].uuid)) {
  1486. uuid_copy(&logsuper->active[i].uuid, uuid);
  1487. sbi->aggregate = i;
  1488. break;
  1489. }
  1490. if (i == MAX_ACTIVE) {
  1491. jfs_warn("Too many file systems sharing journal!");
  1492. lbmFree(bpsuper);
  1493. return -EMFILE; /* Is there a better rc? */
  1494. }
  1495. } else {
  1496. for (i = 0; i < MAX_ACTIVE; i++)
  1497. if (uuid_equal(&logsuper->active[i].uuid, uuid)) {
  1498. uuid_copy(&logsuper->active[i].uuid,
  1499. &uuid_null);
  1500. break;
  1501. }
  1502. if (i == MAX_ACTIVE) {
  1503. jfs_warn("Somebody stomped on the journal!");
  1504. lbmFree(bpsuper);
  1505. return -EIO;
  1506. }
  1507. }
  1508. /*
  1509. * synchronous write log superblock:
  1510. *
  1511. * write sidestream bypassing write queue:
  1512. * at file system mount, log super block is updated for
  1513. * activation of the file system before any log record
  1514. * (MOUNT record) of the file system, and at file system
  1515. * unmount, all meta data for the file system has been
  1516. * flushed before log super block is updated for deactivation
  1517. * of the file system.
  1518. */
  1519. lbmDirectWrite(log, bpsuper, lbmWRITE | lbmRELEASE | lbmSYNC);
  1520. rc = lbmIOWait(bpsuper, lbmFREE);
  1521. return rc;
  1522. }
  1523. /*
  1524. * log buffer manager (lbm)
  1525. * ------------------------
  1526. *
  1527. * special purpose buffer manager supporting log i/o requirements.
  1528. *
  1529. * per log write queue:
  1530. * log pageout occurs in serial order by fifo write queue and
  1531. * restricting to a single i/o in pregress at any one time.
  1532. * a circular singly-linked list
  1533. * (log->wrqueue points to the tail, and buffers are linked via
  1534. * bp->wrqueue field), and
  1535. * maintains log page in pageout ot waiting for pageout in serial pageout.
  1536. */
  1537. /*
  1538. * lbmLogInit()
  1539. *
  1540. * initialize per log I/O setup at lmLogInit()
  1541. */
  1542. static int lbmLogInit(struct jfs_log * log)
  1543. { /* log inode */
  1544. int i;
  1545. struct lbuf *lbuf;
  1546. jfs_info("lbmLogInit: log:0x%p", log);
  1547. /* initialize current buffer cursor */
  1548. log->bp = NULL;
  1549. /* initialize log device write queue */
  1550. log->wqueue = NULL;
  1551. /*
  1552. * Each log has its own buffer pages allocated to it. These are
  1553. * not managed by the page cache. This ensures that a transaction
  1554. * writing to the log does not block trying to allocate a page from
  1555. * the page cache (for the log). This would be bad, since page
  1556. * allocation waits on the kswapd thread that may be committing inodes
  1557. * which would cause log activity. Was that clear? I'm trying to
  1558. * avoid deadlock here.
  1559. */
  1560. init_waitqueue_head(&log->free_wait);
  1561. log->lbuf_free = NULL;
  1562. for (i = 0; i < LOGPAGES;) {
  1563. char *buffer;
  1564. uint offset;
  1565. struct page *page = alloc_page(GFP_KERNEL | __GFP_ZERO);
  1566. if (!page)
  1567. goto error;
  1568. buffer = page_address(page);
  1569. for (offset = 0; offset < PAGE_SIZE; offset += LOGPSIZE) {
  1570. lbuf = kmalloc_obj(struct lbuf);
  1571. if (lbuf == NULL) {
  1572. if (offset == 0)
  1573. __free_page(page);
  1574. goto error;
  1575. }
  1576. if (offset) /* we already have one reference */
  1577. get_page(page);
  1578. lbuf->l_offset = offset;
  1579. lbuf->l_ldata = buffer + offset;
  1580. lbuf->l_page = page;
  1581. lbuf->l_log = log;
  1582. init_waitqueue_head(&lbuf->l_ioevent);
  1583. lbuf->l_freelist = log->lbuf_free;
  1584. log->lbuf_free = lbuf;
  1585. i++;
  1586. }
  1587. }
  1588. return (0);
  1589. error:
  1590. lbmLogShutdown(log);
  1591. return -ENOMEM;
  1592. }
  1593. /*
  1594. * lbmLogShutdown()
  1595. *
  1596. * finalize per log I/O setup at lmLogShutdown()
  1597. */
  1598. static void lbmLogShutdown(struct jfs_log * log)
  1599. {
  1600. struct lbuf *lbuf;
  1601. jfs_info("lbmLogShutdown: log:0x%p", log);
  1602. lbuf = log->lbuf_free;
  1603. while (lbuf) {
  1604. struct lbuf *next = lbuf->l_freelist;
  1605. __free_page(lbuf->l_page);
  1606. kfree(lbuf);
  1607. lbuf = next;
  1608. }
  1609. }
  1610. /*
  1611. * lbmAllocate()
  1612. *
  1613. * allocate an empty log buffer
  1614. */
  1615. static struct lbuf *lbmAllocate(struct jfs_log * log, int pn)
  1616. {
  1617. struct lbuf *bp;
  1618. unsigned long flags;
  1619. /*
  1620. * recycle from log buffer freelist if any
  1621. */
  1622. LCACHE_LOCK(flags);
  1623. LCACHE_SLEEP_COND(log->free_wait, (bp = log->lbuf_free), flags);
  1624. log->lbuf_free = bp->l_freelist;
  1625. LCACHE_UNLOCK(flags);
  1626. bp->l_flag = 0;
  1627. bp->l_wqnext = NULL;
  1628. bp->l_freelist = NULL;
  1629. bp->l_pn = pn;
  1630. bp->l_blkno = log->base + (pn << (L2LOGPSIZE - log->l2bsize));
  1631. bp->l_ceor = 0;
  1632. return bp;
  1633. }
  1634. /*
  1635. * lbmFree()
  1636. *
  1637. * release a log buffer to freelist
  1638. */
  1639. static void lbmFree(struct lbuf * bp)
  1640. {
  1641. unsigned long flags;
  1642. LCACHE_LOCK(flags);
  1643. lbmfree(bp);
  1644. LCACHE_UNLOCK(flags);
  1645. }
  1646. static void lbmfree(struct lbuf * bp)
  1647. {
  1648. struct jfs_log *log = bp->l_log;
  1649. assert(bp->l_wqnext == NULL);
  1650. /*
  1651. * return the buffer to head of freelist
  1652. */
  1653. bp->l_freelist = log->lbuf_free;
  1654. log->lbuf_free = bp;
  1655. wake_up(&log->free_wait);
  1656. return;
  1657. }
  1658. /*
  1659. * NAME: lbmRedrive
  1660. *
  1661. * FUNCTION: add a log buffer to the log redrive list
  1662. *
  1663. * PARAMETER:
  1664. * bp - log buffer
  1665. *
  1666. * NOTES:
  1667. * Takes log_redrive_lock.
  1668. */
  1669. static inline void lbmRedrive(struct lbuf *bp)
  1670. {
  1671. unsigned long flags;
  1672. spin_lock_irqsave(&log_redrive_lock, flags);
  1673. bp->l_redrive_next = log_redrive_list;
  1674. log_redrive_list = bp;
  1675. spin_unlock_irqrestore(&log_redrive_lock, flags);
  1676. wake_up_process(jfsIOthread);
  1677. }
  1678. /*
  1679. * lbmRead()
  1680. */
  1681. static int lbmRead(struct jfs_log * log, int pn, struct lbuf ** bpp)
  1682. {
  1683. struct bio *bio;
  1684. struct lbuf *bp;
  1685. /*
  1686. * allocate a log buffer
  1687. */
  1688. *bpp = bp = lbmAllocate(log, pn);
  1689. jfs_info("lbmRead: bp:0x%p pn:0x%x", bp, pn);
  1690. bp->l_flag |= lbmREAD;
  1691. bio = bio_alloc(file_bdev(log->bdev_file), 1, REQ_OP_READ, GFP_NOFS);
  1692. bio->bi_iter.bi_sector = bp->l_blkno << (log->l2bsize - 9);
  1693. __bio_add_page(bio, bp->l_page, LOGPSIZE, bp->l_offset);
  1694. BUG_ON(bio->bi_iter.bi_size != LOGPSIZE);
  1695. bio->bi_end_io = lbmIODone;
  1696. bio->bi_private = bp;
  1697. /*check if journaling to disk has been disabled*/
  1698. if (log->no_integrity) {
  1699. bio->bi_iter.bi_size = 0;
  1700. lbmIODone(bio);
  1701. } else {
  1702. submit_bio(bio);
  1703. }
  1704. wait_event(bp->l_ioevent, (bp->l_flag != lbmREAD));
  1705. return 0;
  1706. }
  1707. /*
  1708. * lbmWrite()
  1709. *
  1710. * buffer at head of pageout queue stays after completion of
  1711. * partial-page pageout and redriven by explicit initiation of
  1712. * pageout by caller until full-page pageout is completed and
  1713. * released.
  1714. *
  1715. * device driver i/o done redrives pageout of new buffer at
  1716. * head of pageout queue when current buffer at head of pageout
  1717. * queue is released at the completion of its full-page pageout.
  1718. *
  1719. * LOGGC_LOCK() serializes lbmWrite() by lmNextPage() and lmGroupCommit().
  1720. * LCACHE_LOCK() serializes xflag between lbmWrite() and lbmIODone()
  1721. */
  1722. static void lbmWrite(struct jfs_log * log, struct lbuf * bp, int flag,
  1723. int cant_block)
  1724. {
  1725. struct lbuf *tail;
  1726. unsigned long flags;
  1727. jfs_info("lbmWrite: bp:0x%p flag:0x%x pn:0x%x", bp, flag, bp->l_pn);
  1728. /* map the logical block address to physical block address */
  1729. bp->l_blkno =
  1730. log->base + (bp->l_pn << (L2LOGPSIZE - log->l2bsize));
  1731. LCACHE_LOCK(flags); /* disable+lock */
  1732. /*
  1733. * initialize buffer for device driver
  1734. */
  1735. bp->l_flag = flag;
  1736. /*
  1737. * insert bp at tail of write queue associated with log
  1738. *
  1739. * (request is either for bp already/currently at head of queue
  1740. * or new bp to be inserted at tail)
  1741. */
  1742. tail = log->wqueue;
  1743. /* is buffer not already on write queue ? */
  1744. if (bp->l_wqnext == NULL) {
  1745. /* insert at tail of wqueue */
  1746. if (tail == NULL) {
  1747. log->wqueue = bp;
  1748. bp->l_wqnext = bp;
  1749. } else {
  1750. log->wqueue = bp;
  1751. bp->l_wqnext = tail->l_wqnext;
  1752. tail->l_wqnext = bp;
  1753. }
  1754. tail = bp;
  1755. }
  1756. /* is buffer at head of wqueue and for write ? */
  1757. if ((bp != tail->l_wqnext) || !(flag & lbmWRITE)) {
  1758. LCACHE_UNLOCK(flags); /* unlock+enable */
  1759. return;
  1760. }
  1761. LCACHE_UNLOCK(flags); /* unlock+enable */
  1762. if (cant_block)
  1763. lbmRedrive(bp);
  1764. else if (flag & lbmSYNC)
  1765. lbmStartIO(bp);
  1766. else {
  1767. LOGGC_UNLOCK(log);
  1768. lbmStartIO(bp);
  1769. LOGGC_LOCK(log);
  1770. }
  1771. }
  1772. /*
  1773. * lbmDirectWrite()
  1774. *
  1775. * initiate pageout bypassing write queue for sidestream
  1776. * (e.g., log superblock) write;
  1777. */
  1778. static void lbmDirectWrite(struct jfs_log * log, struct lbuf * bp, int flag)
  1779. {
  1780. jfs_info("lbmDirectWrite: bp:0x%p flag:0x%x pn:0x%x",
  1781. bp, flag, bp->l_pn);
  1782. /*
  1783. * initialize buffer for device driver
  1784. */
  1785. bp->l_flag = flag | lbmDIRECT;
  1786. /* map the logical block address to physical block address */
  1787. bp->l_blkno =
  1788. log->base + (bp->l_pn << (L2LOGPSIZE - log->l2bsize));
  1789. /*
  1790. * initiate pageout of the page
  1791. */
  1792. lbmStartIO(bp);
  1793. }
  1794. /*
  1795. * NAME: lbmStartIO()
  1796. *
  1797. * FUNCTION: Interface to DD strategy routine
  1798. *
  1799. * RETURN: none
  1800. *
  1801. * serialization: LCACHE_LOCK() is NOT held during log i/o;
  1802. */
  1803. static void lbmStartIO(struct lbuf * bp)
  1804. {
  1805. struct bio *bio;
  1806. struct jfs_log *log = bp->l_log;
  1807. struct block_device *bdev = NULL;
  1808. jfs_info("lbmStartIO");
  1809. if (!log->no_integrity)
  1810. bdev = file_bdev(log->bdev_file);
  1811. bio = bio_alloc(bdev, 1, REQ_OP_WRITE | REQ_SYNC,
  1812. GFP_NOFS);
  1813. bio->bi_iter.bi_sector = bp->l_blkno << (log->l2bsize - 9);
  1814. __bio_add_page(bio, bp->l_page, LOGPSIZE, bp->l_offset);
  1815. BUG_ON(bio->bi_iter.bi_size != LOGPSIZE);
  1816. bio->bi_end_io = lbmIODone;
  1817. bio->bi_private = bp;
  1818. /* check if journaling to disk has been disabled */
  1819. if (log->no_integrity) {
  1820. bio->bi_iter.bi_size = 0;
  1821. lbmIODone(bio);
  1822. } else {
  1823. submit_bio(bio);
  1824. INCREMENT(lmStat.submitted);
  1825. }
  1826. }
  1827. /*
  1828. * lbmIOWait()
  1829. */
  1830. static int lbmIOWait(struct lbuf * bp, int flag)
  1831. {
  1832. unsigned long flags;
  1833. int rc = 0;
  1834. jfs_info("lbmIOWait1: bp:0x%p flag:0x%x:0x%x", bp, bp->l_flag, flag);
  1835. LCACHE_LOCK(flags); /* disable+lock */
  1836. LCACHE_SLEEP_COND(bp->l_ioevent, (bp->l_flag & lbmDONE), flags);
  1837. rc = (bp->l_flag & lbmERROR) ? -EIO : 0;
  1838. if (flag & lbmFREE)
  1839. lbmfree(bp);
  1840. LCACHE_UNLOCK(flags); /* unlock+enable */
  1841. jfs_info("lbmIOWait2: bp:0x%p flag:0x%x:0x%x", bp, bp->l_flag, flag);
  1842. return rc;
  1843. }
  1844. /*
  1845. * lbmIODone()
  1846. *
  1847. * executed at INTIODONE level
  1848. */
  1849. static void lbmIODone(struct bio *bio)
  1850. {
  1851. struct lbuf *bp = bio->bi_private;
  1852. struct lbuf *nextbp, *tail;
  1853. struct jfs_log *log;
  1854. unsigned long flags;
  1855. /*
  1856. * get back jfs buffer bound to the i/o buffer
  1857. */
  1858. jfs_info("lbmIODone: bp:0x%p flag:0x%x", bp, bp->l_flag);
  1859. LCACHE_LOCK(flags); /* disable+lock */
  1860. bp->l_flag |= lbmDONE;
  1861. if (bio->bi_status) {
  1862. bp->l_flag |= lbmERROR;
  1863. jfs_err("lbmIODone: I/O error in JFS log");
  1864. }
  1865. bio_put(bio);
  1866. /*
  1867. * pagein completion
  1868. */
  1869. if (bp->l_flag & lbmREAD) {
  1870. bp->l_flag &= ~lbmREAD;
  1871. LCACHE_UNLOCK(flags); /* unlock+enable */
  1872. /* wakeup I/O initiator */
  1873. LCACHE_WAKEUP(&bp->l_ioevent);
  1874. return;
  1875. }
  1876. /*
  1877. * pageout completion
  1878. *
  1879. * the bp at the head of write queue has completed pageout.
  1880. *
  1881. * if single-commit/full-page pageout, remove the current buffer
  1882. * from head of pageout queue, and redrive pageout with
  1883. * the new buffer at head of pageout queue;
  1884. * otherwise, the partial-page pageout buffer stays at
  1885. * the head of pageout queue to be redriven for pageout
  1886. * by lmGroupCommit() until full-page pageout is completed.
  1887. */
  1888. bp->l_flag &= ~lbmWRITE;
  1889. INCREMENT(lmStat.pagedone);
  1890. /* update committed lsn */
  1891. log = bp->l_log;
  1892. log->clsn = (bp->l_pn << L2LOGPSIZE) + bp->l_ceor;
  1893. if (bp->l_flag & lbmDIRECT) {
  1894. LCACHE_WAKEUP(&bp->l_ioevent);
  1895. LCACHE_UNLOCK(flags);
  1896. return;
  1897. }
  1898. tail = log->wqueue;
  1899. /* single element queue */
  1900. if (bp == tail) {
  1901. /* remove head buffer of full-page pageout
  1902. * from log device write queue
  1903. */
  1904. if (bp->l_flag & lbmRELEASE) {
  1905. log->wqueue = NULL;
  1906. bp->l_wqnext = NULL;
  1907. }
  1908. }
  1909. /* multi element queue */
  1910. else {
  1911. /* remove head buffer of full-page pageout
  1912. * from log device write queue
  1913. */
  1914. if (bp->l_flag & lbmRELEASE) {
  1915. nextbp = tail->l_wqnext = bp->l_wqnext;
  1916. bp->l_wqnext = NULL;
  1917. /*
  1918. * redrive pageout of next page at head of write queue:
  1919. * redrive next page without any bound tblk
  1920. * (i.e., page w/o any COMMIT records), or
  1921. * first page of new group commit which has been
  1922. * queued after current page (subsequent pageout
  1923. * is performed synchronously, except page without
  1924. * any COMMITs) by lmGroupCommit() as indicated
  1925. * by lbmWRITE flag;
  1926. */
  1927. if (nextbp->l_flag & lbmWRITE) {
  1928. /*
  1929. * We can't do the I/O at interrupt time.
  1930. * The jfsIO thread can do it
  1931. */
  1932. lbmRedrive(nextbp);
  1933. }
  1934. }
  1935. }
  1936. /*
  1937. * synchronous pageout:
  1938. *
  1939. * buffer has not necessarily been removed from write queue
  1940. * (e.g., synchronous write of partial-page with COMMIT):
  1941. * leave buffer for i/o initiator to dispose
  1942. */
  1943. if (bp->l_flag & lbmSYNC) {
  1944. LCACHE_UNLOCK(flags); /* unlock+enable */
  1945. /* wakeup I/O initiator */
  1946. LCACHE_WAKEUP(&bp->l_ioevent);
  1947. }
  1948. /*
  1949. * Group Commit pageout:
  1950. */
  1951. else if (bp->l_flag & lbmGC) {
  1952. LCACHE_UNLOCK(flags);
  1953. lmPostGC(bp);
  1954. }
  1955. /*
  1956. * asynchronous pageout:
  1957. *
  1958. * buffer must have been removed from write queue:
  1959. * insert buffer at head of freelist where it can be recycled
  1960. */
  1961. else {
  1962. assert(bp->l_flag & lbmRELEASE);
  1963. assert(bp->l_flag & lbmFREE);
  1964. lbmfree(bp);
  1965. LCACHE_UNLOCK(flags); /* unlock+enable */
  1966. }
  1967. }
  1968. int jfsIOWait(void *arg)
  1969. {
  1970. struct lbuf *bp;
  1971. set_freezable();
  1972. do {
  1973. spin_lock_irq(&log_redrive_lock);
  1974. while ((bp = log_redrive_list)) {
  1975. log_redrive_list = bp->l_redrive_next;
  1976. bp->l_redrive_next = NULL;
  1977. spin_unlock_irq(&log_redrive_lock);
  1978. lbmStartIO(bp);
  1979. spin_lock_irq(&log_redrive_lock);
  1980. }
  1981. if (freezing(current)) {
  1982. spin_unlock_irq(&log_redrive_lock);
  1983. try_to_freeze();
  1984. } else {
  1985. set_current_state(TASK_INTERRUPTIBLE);
  1986. spin_unlock_irq(&log_redrive_lock);
  1987. schedule();
  1988. }
  1989. } while (!kthread_should_stop());
  1990. jfs_info("jfsIOWait being killed!");
  1991. return 0;
  1992. }
  1993. /*
  1994. * NAME: lmLogFormat()/jfs_logform()
  1995. *
  1996. * FUNCTION: format file system log
  1997. *
  1998. * PARAMETERS:
  1999. * log - volume log
  2000. * logAddress - start address of log space in FS block
  2001. * logSize - length of log space in FS block;
  2002. *
  2003. * RETURN: 0 - success
  2004. * -EIO - i/o error
  2005. *
  2006. * XXX: We're synchronously writing one page at a time. This needs to
  2007. * be improved by writing multiple pages at once.
  2008. */
  2009. int lmLogFormat(struct jfs_log *log, s64 logAddress, int logSize)
  2010. {
  2011. int rc = -EIO;
  2012. struct jfs_sb_info *sbi;
  2013. struct logsuper *logsuper;
  2014. struct logpage *lp;
  2015. int lspn; /* log sequence page number */
  2016. struct lrd *lrd_ptr;
  2017. int npages = 0;
  2018. struct lbuf *bp;
  2019. jfs_info("lmLogFormat: logAddress:%Ld logSize:%d",
  2020. (long long)logAddress, logSize);
  2021. sbi = list_entry(log->sb_list.next, struct jfs_sb_info, log_list);
  2022. /* allocate a log buffer */
  2023. bp = lbmAllocate(log, 1);
  2024. npages = logSize >> sbi->l2nbperpage;
  2025. /*
  2026. * log space:
  2027. *
  2028. * page 0 - reserved;
  2029. * page 1 - log superblock;
  2030. * page 2 - log data page: A SYNC log record is written
  2031. * into this page at logform time;
  2032. * pages 3-N - log data page: set to empty log data pages;
  2033. */
  2034. /*
  2035. * init log superblock: log page 1
  2036. */
  2037. logsuper = (struct logsuper *) bp->l_ldata;
  2038. logsuper->magic = cpu_to_le32(LOGMAGIC);
  2039. logsuper->version = cpu_to_le32(LOGVERSION);
  2040. logsuper->state = cpu_to_le32(LOGREDONE);
  2041. logsuper->flag = cpu_to_le32(sbi->mntflag); /* ? */
  2042. logsuper->size = cpu_to_le32(npages);
  2043. logsuper->bsize = cpu_to_le32(sbi->bsize);
  2044. logsuper->l2bsize = cpu_to_le32(sbi->l2bsize);
  2045. logsuper->end = cpu_to_le32(2 * LOGPSIZE + LOGPHDRSIZE + LOGRDSIZE);
  2046. bp->l_flag = lbmWRITE | lbmSYNC | lbmDIRECT;
  2047. bp->l_blkno = logAddress + sbi->nbperpage;
  2048. lbmStartIO(bp);
  2049. if ((rc = lbmIOWait(bp, 0)))
  2050. goto exit;
  2051. /*
  2052. * init pages 2 to npages-1 as log data pages:
  2053. *
  2054. * log page sequence number (lpsn) initialization:
  2055. *
  2056. * pn: 0 1 2 3 n-1
  2057. * +-----+-----+=====+=====+===.....===+=====+
  2058. * lspn: N-1 0 1 N-2
  2059. * <--- N page circular file ---->
  2060. *
  2061. * the N (= npages-2) data pages of the log is maintained as
  2062. * a circular file for the log records;
  2063. * lpsn grows by 1 monotonically as each log page is written
  2064. * to the circular file of the log;
  2065. * and setLogpage() will not reset the page number even if
  2066. * the eor is equal to LOGPHDRSIZE. In order for binary search
  2067. * still work in find log end process, we have to simulate the
  2068. * log wrap situation at the log format time.
  2069. * The 1st log page written will have the highest lpsn. Then
  2070. * the succeeding log pages will have ascending order of
  2071. * the lspn starting from 0, ... (N-2)
  2072. */
  2073. lp = (struct logpage *) bp->l_ldata;
  2074. /*
  2075. * initialize 1st log page to be written: lpsn = N - 1,
  2076. * write a SYNCPT log record is written to this page
  2077. */
  2078. lp->h.page = lp->t.page = cpu_to_le32(npages - 3);
  2079. lp->h.eor = lp->t.eor = cpu_to_le16(LOGPHDRSIZE + LOGRDSIZE);
  2080. lrd_ptr = (struct lrd *) &lp->data;
  2081. lrd_ptr->logtid = 0;
  2082. lrd_ptr->backchain = 0;
  2083. lrd_ptr->type = cpu_to_le16(LOG_SYNCPT);
  2084. lrd_ptr->length = 0;
  2085. lrd_ptr->log.syncpt.sync = 0;
  2086. bp->l_blkno += sbi->nbperpage;
  2087. bp->l_flag = lbmWRITE | lbmSYNC | lbmDIRECT;
  2088. lbmStartIO(bp);
  2089. if ((rc = lbmIOWait(bp, 0)))
  2090. goto exit;
  2091. /*
  2092. * initialize succeeding log pages: lpsn = 0, 1, ..., (N-2)
  2093. */
  2094. for (lspn = 0; lspn < npages - 3; lspn++) {
  2095. lp->h.page = lp->t.page = cpu_to_le32(lspn);
  2096. lp->h.eor = lp->t.eor = cpu_to_le16(LOGPHDRSIZE);
  2097. bp->l_blkno += sbi->nbperpage;
  2098. bp->l_flag = lbmWRITE | lbmSYNC | lbmDIRECT;
  2099. lbmStartIO(bp);
  2100. if ((rc = lbmIOWait(bp, 0)))
  2101. goto exit;
  2102. }
  2103. rc = 0;
  2104. exit:
  2105. /*
  2106. * finalize log
  2107. */
  2108. /* release the buffer */
  2109. lbmFree(bp);
  2110. return rc;
  2111. }
  2112. #ifdef CONFIG_JFS_STATISTICS
  2113. int jfs_lmstats_proc_show(struct seq_file *m, void *v)
  2114. {
  2115. seq_printf(m,
  2116. "JFS Logmgr stats\n"
  2117. "================\n"
  2118. "commits = %d\n"
  2119. "writes submitted = %d\n"
  2120. "writes completed = %d\n"
  2121. "full pages submitted = %d\n"
  2122. "partial pages submitted = %d\n",
  2123. lmStat.commit,
  2124. lmStat.submitted,
  2125. lmStat.pagedone,
  2126. lmStat.full_page,
  2127. lmStat.partial_page);
  2128. return 0;
  2129. }
  2130. #endif /* CONFIG_JFS_STATISTICS */