jfs_logmgr.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486
  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 block_device *bdev;
  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 (log->bdev->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(sizeof(struct jfs_log), GFP_KERNEL))) {
  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 = blkdev_get_by_dev(sbi->logdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
  958. log);
  959. if (IS_ERR(bdev)) {
  960. rc = PTR_ERR(bdev);
  961. goto free;
  962. }
  963. log->bdev = bdev;
  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. blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
  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(sizeof(struct jfs_log), GFP_KERNEL)))
  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 = sb->s_bdev;
  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(sizeof(struct jfs_log), GFP_KERNEL);
  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->base = 0;
  1039. dummy_log->size = 1024;
  1040. rc = lmLogInit(dummy_log);
  1041. if (rc) {
  1042. kfree(dummy_log);
  1043. dummy_log = NULL;
  1044. mutex_unlock(&jfs_log_mutex);
  1045. return rc;
  1046. }
  1047. }
  1048. LOG_LOCK(dummy_log);
  1049. list_add(&JFS_SBI(sb)->log_list, &dummy_log->sb_list);
  1050. JFS_SBI(sb)->log = dummy_log;
  1051. LOG_UNLOCK(dummy_log);
  1052. mutex_unlock(&jfs_log_mutex);
  1053. return 0;
  1054. }
  1055. /*
  1056. * NAME: lmLogInit()
  1057. *
  1058. * FUNCTION: log initialization at first log open.
  1059. *
  1060. * logredo() (or logformat()) should have been run previously.
  1061. * initialize the log from log superblock.
  1062. * set the log state in the superblock to LOGMOUNT and
  1063. * write SYNCPT log record.
  1064. *
  1065. * PARAMETER: log - log structure
  1066. *
  1067. * RETURN: 0 - if ok
  1068. * -EINVAL - bad log magic number or superblock dirty
  1069. * error returned from logwait()
  1070. *
  1071. * serialization: single first open thread
  1072. */
  1073. int lmLogInit(struct jfs_log * log)
  1074. {
  1075. int rc = 0;
  1076. struct lrd lrd;
  1077. struct logsuper *logsuper;
  1078. struct lbuf *bpsuper;
  1079. struct lbuf *bp;
  1080. struct logpage *lp;
  1081. int lsn = 0;
  1082. jfs_info("lmLogInit: log:0x%p", log);
  1083. /* initialize the group commit serialization lock */
  1084. LOGGC_LOCK_INIT(log);
  1085. /* allocate/initialize the log write serialization lock */
  1086. LOG_LOCK_INIT(log);
  1087. LOGSYNC_LOCK_INIT(log);
  1088. INIT_LIST_HEAD(&log->synclist);
  1089. INIT_LIST_HEAD(&log->cqueue);
  1090. log->flush_tblk = NULL;
  1091. log->count = 0;
  1092. /*
  1093. * initialize log i/o
  1094. */
  1095. if ((rc = lbmLogInit(log)))
  1096. return rc;
  1097. if (!test_bit(log_INLINELOG, &log->flag))
  1098. log->l2bsize = L2LOGPSIZE;
  1099. /* check for disabled journaling to disk */
  1100. if (log->no_integrity) {
  1101. /*
  1102. * Journal pages will still be filled. When the time comes
  1103. * to actually do the I/O, the write is not done, and the
  1104. * endio routine is called directly.
  1105. */
  1106. bp = lbmAllocate(log , 0);
  1107. log->bp = bp;
  1108. bp->l_pn = bp->l_eor = 0;
  1109. } else {
  1110. /*
  1111. * validate log superblock
  1112. */
  1113. if ((rc = lbmRead(log, 1, &bpsuper)))
  1114. goto errout10;
  1115. logsuper = (struct logsuper *) bpsuper->l_ldata;
  1116. if (logsuper->magic != cpu_to_le32(LOGMAGIC)) {
  1117. jfs_warn("*** Log Format Error ! ***");
  1118. rc = -EINVAL;
  1119. goto errout20;
  1120. }
  1121. /* logredo() should have been run successfully. */
  1122. if (logsuper->state != cpu_to_le32(LOGREDONE)) {
  1123. jfs_warn("*** Log Is Dirty ! ***");
  1124. rc = -EINVAL;
  1125. goto errout20;
  1126. }
  1127. /* initialize log from log superblock */
  1128. if (test_bit(log_INLINELOG,&log->flag)) {
  1129. if (log->size != le32_to_cpu(logsuper->size)) {
  1130. rc = -EINVAL;
  1131. goto errout20;
  1132. }
  1133. jfs_info("lmLogInit: inline log:0x%p base:0x%Lx size:0x%x",
  1134. log, (unsigned long long)log->base, log->size);
  1135. } else {
  1136. if (!uuid_equal(&logsuper->uuid, &log->uuid)) {
  1137. jfs_warn("wrong uuid on JFS log device");
  1138. rc = -EINVAL;
  1139. goto errout20;
  1140. }
  1141. log->size = le32_to_cpu(logsuper->size);
  1142. log->l2bsize = le32_to_cpu(logsuper->l2bsize);
  1143. jfs_info("lmLogInit: external log:0x%p base:0x%Lx size:0x%x",
  1144. log, (unsigned long long)log->base, log->size);
  1145. }
  1146. log->page = le32_to_cpu(logsuper->end) / LOGPSIZE;
  1147. log->eor = le32_to_cpu(logsuper->end) - (LOGPSIZE * log->page);
  1148. /*
  1149. * initialize for log append write mode
  1150. */
  1151. /* establish current/end-of-log page/buffer */
  1152. if ((rc = lbmRead(log, log->page, &bp)))
  1153. goto errout20;
  1154. lp = (struct logpage *) bp->l_ldata;
  1155. jfs_info("lmLogInit: lsn:0x%x page:%d eor:%d:%d",
  1156. le32_to_cpu(logsuper->end), log->page, log->eor,
  1157. le16_to_cpu(lp->h.eor));
  1158. log->bp = bp;
  1159. bp->l_pn = log->page;
  1160. bp->l_eor = log->eor;
  1161. /* if current page is full, move on to next page */
  1162. if (log->eor >= LOGPSIZE - LOGPTLRSIZE)
  1163. lmNextPage(log);
  1164. /*
  1165. * initialize log syncpoint
  1166. */
  1167. /*
  1168. * write the first SYNCPT record with syncpoint = 0
  1169. * (i.e., log redo up to HERE !);
  1170. * remove current page from lbm write queue at end of pageout
  1171. * (to write log superblock update), but do not release to
  1172. * freelist;
  1173. */
  1174. lrd.logtid = 0;
  1175. lrd.backchain = 0;
  1176. lrd.type = cpu_to_le16(LOG_SYNCPT);
  1177. lrd.length = 0;
  1178. lrd.log.syncpt.sync = 0;
  1179. lsn = lmWriteRecord(log, NULL, &lrd, NULL);
  1180. bp = log->bp;
  1181. bp->l_ceor = bp->l_eor;
  1182. lp = (struct logpage *) bp->l_ldata;
  1183. lp->h.eor = lp->t.eor = cpu_to_le16(bp->l_eor);
  1184. lbmWrite(log, bp, lbmWRITE | lbmSYNC, 0);
  1185. if ((rc = lbmIOWait(bp, 0)))
  1186. goto errout30;
  1187. /*
  1188. * update/write superblock
  1189. */
  1190. logsuper->state = cpu_to_le32(LOGMOUNT);
  1191. log->serial = le32_to_cpu(logsuper->serial) + 1;
  1192. logsuper->serial = cpu_to_le32(log->serial);
  1193. lbmDirectWrite(log, bpsuper, lbmWRITE | lbmRELEASE | lbmSYNC);
  1194. if ((rc = lbmIOWait(bpsuper, lbmFREE)))
  1195. goto errout30;
  1196. }
  1197. /* initialize logsync parameters */
  1198. log->logsize = (log->size - 2) << L2LOGPSIZE;
  1199. log->lsn = lsn;
  1200. log->syncpt = lsn;
  1201. log->sync = log->syncpt;
  1202. log->nextsync = LOGSYNC_DELTA(log->logsize);
  1203. jfs_info("lmLogInit: lsn:0x%x syncpt:0x%x sync:0x%x",
  1204. log->lsn, log->syncpt, log->sync);
  1205. /*
  1206. * initialize for lazy/group commit
  1207. */
  1208. log->clsn = lsn;
  1209. return 0;
  1210. /*
  1211. * unwind on error
  1212. */
  1213. errout30: /* release log page */
  1214. log->wqueue = NULL;
  1215. bp->l_wqnext = NULL;
  1216. lbmFree(bp);
  1217. errout20: /* release log superblock */
  1218. lbmFree(bpsuper);
  1219. errout10: /* unwind lbmLogInit() */
  1220. lbmLogShutdown(log);
  1221. jfs_warn("lmLogInit: exit(%d)", rc);
  1222. return rc;
  1223. }
  1224. /*
  1225. * NAME: lmLogClose()
  1226. *
  1227. * FUNCTION: remove file system <ipmnt> from active list of log <iplog>
  1228. * and close it on last close.
  1229. *
  1230. * PARAMETER: sb - superblock
  1231. *
  1232. * RETURN: errors from subroutines
  1233. *
  1234. * serialization:
  1235. */
  1236. int lmLogClose(struct super_block *sb)
  1237. {
  1238. struct jfs_sb_info *sbi = JFS_SBI(sb);
  1239. struct jfs_log *log = sbi->log;
  1240. struct block_device *bdev;
  1241. int rc = 0;
  1242. jfs_info("lmLogClose: log:0x%p", log);
  1243. mutex_lock(&jfs_log_mutex);
  1244. LOG_LOCK(log);
  1245. list_del(&sbi->log_list);
  1246. LOG_UNLOCK(log);
  1247. sbi->log = NULL;
  1248. /*
  1249. * We need to make sure all of the "written" metapages
  1250. * actually make it to disk
  1251. */
  1252. sync_blockdev(sb->s_bdev);
  1253. if (test_bit(log_INLINELOG, &log->flag)) {
  1254. /*
  1255. * in-line log in host file system
  1256. */
  1257. rc = lmLogShutdown(log);
  1258. kfree(log);
  1259. goto out;
  1260. }
  1261. if (!log->no_integrity)
  1262. lmLogFileSystem(log, sbi, 0);
  1263. if (!list_empty(&log->sb_list))
  1264. goto out;
  1265. /*
  1266. * TODO: ensure that the dummy_log is in a state to allow
  1267. * lbmLogShutdown to deallocate all the buffers and call
  1268. * kfree against dummy_log. For now, leave dummy_log & its
  1269. * buffers in memory, and resuse if another no-integrity mount
  1270. * is requested.
  1271. */
  1272. if (log->no_integrity)
  1273. goto out;
  1274. /*
  1275. * external log as separate logical volume
  1276. */
  1277. list_del(&log->journal_list);
  1278. bdev = log->bdev;
  1279. rc = lmLogShutdown(log);
  1280. blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
  1281. kfree(log);
  1282. out:
  1283. mutex_unlock(&jfs_log_mutex);
  1284. jfs_info("lmLogClose: exit(%d)", rc);
  1285. return rc;
  1286. }
  1287. /*
  1288. * NAME: jfs_flush_journal()
  1289. *
  1290. * FUNCTION: initiate write of any outstanding transactions to the journal
  1291. * and optionally wait until they are all written to disk
  1292. *
  1293. * wait == 0 flush until latest txn is committed, don't wait
  1294. * wait == 1 flush until latest txn is committed, wait
  1295. * wait > 1 flush until all txn's are complete, wait
  1296. */
  1297. void jfs_flush_journal(struct jfs_log *log, int wait)
  1298. {
  1299. int i;
  1300. struct tblock *target = NULL;
  1301. /* jfs_write_inode may call us during read-only mount */
  1302. if (!log)
  1303. return;
  1304. jfs_info("jfs_flush_journal: log:0x%p wait=%d", log, wait);
  1305. LOGGC_LOCK(log);
  1306. if (!list_empty(&log->cqueue)) {
  1307. /*
  1308. * This ensures that we will keep writing to the journal as long
  1309. * as there are unwritten commit records
  1310. */
  1311. target = list_entry(log->cqueue.prev, struct tblock, cqueue);
  1312. if (test_bit(log_FLUSH, &log->flag)) {
  1313. /*
  1314. * We're already flushing.
  1315. * if flush_tblk is NULL, we are flushing everything,
  1316. * so leave it that way. Otherwise, update it to the
  1317. * latest transaction
  1318. */
  1319. if (log->flush_tblk)
  1320. log->flush_tblk = target;
  1321. } else {
  1322. /* Only flush until latest transaction is committed */
  1323. log->flush_tblk = target;
  1324. set_bit(log_FLUSH, &log->flag);
  1325. /*
  1326. * Initiate I/O on outstanding transactions
  1327. */
  1328. if (!(log->cflag & logGC_PAGEOUT)) {
  1329. log->cflag |= logGC_PAGEOUT;
  1330. lmGCwrite(log, 0);
  1331. }
  1332. }
  1333. }
  1334. if ((wait > 1) || test_bit(log_SYNCBARRIER, &log->flag)) {
  1335. /* Flush until all activity complete */
  1336. set_bit(log_FLUSH, &log->flag);
  1337. log->flush_tblk = NULL;
  1338. }
  1339. if (wait && target && !(target->flag & tblkGC_COMMITTED)) {
  1340. DECLARE_WAITQUEUE(__wait, current);
  1341. add_wait_queue(&target->gcwait, &__wait);
  1342. set_current_state(TASK_UNINTERRUPTIBLE);
  1343. LOGGC_UNLOCK(log);
  1344. schedule();
  1345. LOGGC_LOCK(log);
  1346. remove_wait_queue(&target->gcwait, &__wait);
  1347. }
  1348. LOGGC_UNLOCK(log);
  1349. if (wait < 2)
  1350. return;
  1351. write_special_inodes(log, filemap_fdatawrite);
  1352. /*
  1353. * If there was recent activity, we may need to wait
  1354. * for the lazycommit thread to catch up
  1355. */
  1356. if ((!list_empty(&log->cqueue)) || !list_empty(&log->synclist)) {
  1357. for (i = 0; i < 200; i++) { /* Too much? */
  1358. msleep(250);
  1359. write_special_inodes(log, filemap_fdatawrite);
  1360. if (list_empty(&log->cqueue) &&
  1361. list_empty(&log->synclist))
  1362. break;
  1363. }
  1364. }
  1365. assert(list_empty(&log->cqueue));
  1366. #ifdef CONFIG_JFS_DEBUG
  1367. if (!list_empty(&log->synclist)) {
  1368. struct logsyncblk *lp;
  1369. printk(KERN_ERR "jfs_flush_journal: synclist not empty\n");
  1370. list_for_each_entry(lp, &log->synclist, synclist) {
  1371. if (lp->xflag & COMMIT_PAGE) {
  1372. struct metapage *mp = (struct metapage *)lp;
  1373. print_hex_dump(KERN_ERR, "metapage: ",
  1374. DUMP_PREFIX_ADDRESS, 16, 4,
  1375. mp, sizeof(struct metapage), 0);
  1376. print_hex_dump(KERN_ERR, "page: ",
  1377. DUMP_PREFIX_ADDRESS, 16,
  1378. sizeof(long), mp->page,
  1379. sizeof(struct page), 0);
  1380. } else
  1381. print_hex_dump(KERN_ERR, "tblock:",
  1382. DUMP_PREFIX_ADDRESS, 16, 4,
  1383. lp, sizeof(struct tblock), 0);
  1384. }
  1385. }
  1386. #else
  1387. WARN_ON(!list_empty(&log->synclist));
  1388. #endif
  1389. clear_bit(log_FLUSH, &log->flag);
  1390. }
  1391. /*
  1392. * NAME: lmLogShutdown()
  1393. *
  1394. * FUNCTION: log shutdown at last LogClose().
  1395. *
  1396. * write log syncpt record.
  1397. * update super block to set redone flag to 0.
  1398. *
  1399. * PARAMETER: log - log inode
  1400. *
  1401. * RETURN: 0 - success
  1402. *
  1403. * serialization: single last close thread
  1404. */
  1405. int lmLogShutdown(struct jfs_log * log)
  1406. {
  1407. int rc;
  1408. struct lrd lrd;
  1409. int lsn;
  1410. struct logsuper *logsuper;
  1411. struct lbuf *bpsuper;
  1412. struct lbuf *bp;
  1413. struct logpage *lp;
  1414. jfs_info("lmLogShutdown: log:0x%p", log);
  1415. jfs_flush_journal(log, 2);
  1416. /*
  1417. * write the last SYNCPT record with syncpoint = 0
  1418. * (i.e., log redo up to HERE !)
  1419. */
  1420. lrd.logtid = 0;
  1421. lrd.backchain = 0;
  1422. lrd.type = cpu_to_le16(LOG_SYNCPT);
  1423. lrd.length = 0;
  1424. lrd.log.syncpt.sync = 0;
  1425. lsn = lmWriteRecord(log, NULL, &lrd, NULL);
  1426. bp = log->bp;
  1427. lp = (struct logpage *) bp->l_ldata;
  1428. lp->h.eor = lp->t.eor = cpu_to_le16(bp->l_eor);
  1429. lbmWrite(log, log->bp, lbmWRITE | lbmRELEASE | lbmSYNC, 0);
  1430. lbmIOWait(log->bp, lbmFREE);
  1431. log->bp = NULL;
  1432. /*
  1433. * synchronous update log superblock
  1434. * mark log state as shutdown cleanly
  1435. * (i.e., Log does not need to be replayed).
  1436. */
  1437. if ((rc = lbmRead(log, 1, &bpsuper)))
  1438. goto out;
  1439. logsuper = (struct logsuper *) bpsuper->l_ldata;
  1440. logsuper->state = cpu_to_le32(LOGREDONE);
  1441. logsuper->end = cpu_to_le32(lsn);
  1442. lbmDirectWrite(log, bpsuper, lbmWRITE | lbmRELEASE | lbmSYNC);
  1443. rc = lbmIOWait(bpsuper, lbmFREE);
  1444. jfs_info("lmLogShutdown: lsn:0x%x page:%d eor:%d",
  1445. lsn, log->page, log->eor);
  1446. out:
  1447. /*
  1448. * shutdown per log i/o
  1449. */
  1450. lbmLogShutdown(log);
  1451. if (rc) {
  1452. jfs_warn("lmLogShutdown: exit(%d)", rc);
  1453. }
  1454. return rc;
  1455. }
  1456. /*
  1457. * NAME: lmLogFileSystem()
  1458. *
  1459. * FUNCTION: insert (<activate> = true)/remove (<activate> = false)
  1460. * file system into/from log active file system list.
  1461. *
  1462. * PARAMETE: log - pointer to logs inode.
  1463. * fsdev - kdev_t of filesystem.
  1464. * serial - pointer to returned log serial number
  1465. * activate - insert/remove device from active list.
  1466. *
  1467. * RETURN: 0 - success
  1468. * errors returned by vms_iowait().
  1469. */
  1470. static int lmLogFileSystem(struct jfs_log * log, struct jfs_sb_info *sbi,
  1471. int activate)
  1472. {
  1473. int rc = 0;
  1474. int i;
  1475. struct logsuper *logsuper;
  1476. struct lbuf *bpsuper;
  1477. uuid_t *uuid = &sbi->uuid;
  1478. /*
  1479. * insert/remove file system device to log active file system list.
  1480. */
  1481. if ((rc = lbmRead(log, 1, &bpsuper)))
  1482. return rc;
  1483. logsuper = (struct logsuper *) bpsuper->l_ldata;
  1484. if (activate) {
  1485. for (i = 0; i < MAX_ACTIVE; i++)
  1486. if (uuid_is_null(&logsuper->active[i].uuid)) {
  1487. uuid_copy(&logsuper->active[i].uuid, uuid);
  1488. sbi->aggregate = i;
  1489. break;
  1490. }
  1491. if (i == MAX_ACTIVE) {
  1492. jfs_warn("Too many file systems sharing journal!");
  1493. lbmFree(bpsuper);
  1494. return -EMFILE; /* Is there a better rc? */
  1495. }
  1496. } else {
  1497. for (i = 0; i < MAX_ACTIVE; i++)
  1498. if (uuid_equal(&logsuper->active[i].uuid, uuid)) {
  1499. uuid_copy(&logsuper->active[i].uuid,
  1500. &uuid_null);
  1501. break;
  1502. }
  1503. if (i == MAX_ACTIVE) {
  1504. jfs_warn("Somebody stomped on the journal!");
  1505. lbmFree(bpsuper);
  1506. return -EIO;
  1507. }
  1508. }
  1509. /*
  1510. * synchronous write log superblock:
  1511. *
  1512. * write sidestream bypassing write queue:
  1513. * at file system mount, log super block is updated for
  1514. * activation of the file system before any log record
  1515. * (MOUNT record) of the file system, and at file system
  1516. * unmount, all meta data for the file system has been
  1517. * flushed before log super block is updated for deactivation
  1518. * of the file system.
  1519. */
  1520. lbmDirectWrite(log, bpsuper, lbmWRITE | lbmRELEASE | lbmSYNC);
  1521. rc = lbmIOWait(bpsuper, lbmFREE);
  1522. return rc;
  1523. }
  1524. /*
  1525. * log buffer manager (lbm)
  1526. * ------------------------
  1527. *
  1528. * special purpose buffer manager supporting log i/o requirements.
  1529. *
  1530. * per log write queue:
  1531. * log pageout occurs in serial order by fifo write queue and
  1532. * restricting to a single i/o in pregress at any one time.
  1533. * a circular singly-linked list
  1534. * (log->wrqueue points to the tail, and buffers are linked via
  1535. * bp->wrqueue field), and
  1536. * maintains log page in pageout ot waiting for pageout in serial pageout.
  1537. */
  1538. /*
  1539. * lbmLogInit()
  1540. *
  1541. * initialize per log I/O setup at lmLogInit()
  1542. */
  1543. static int lbmLogInit(struct jfs_log * log)
  1544. { /* log inode */
  1545. int i;
  1546. struct lbuf *lbuf;
  1547. jfs_info("lbmLogInit: log:0x%p", log);
  1548. /* initialize current buffer cursor */
  1549. log->bp = NULL;
  1550. /* initialize log device write queue */
  1551. log->wqueue = NULL;
  1552. /*
  1553. * Each log has its own buffer pages allocated to it. These are
  1554. * not managed by the page cache. This ensures that a transaction
  1555. * writing to the log does not block trying to allocate a page from
  1556. * the page cache (for the log). This would be bad, since page
  1557. * allocation waits on the kswapd thread that may be committing inodes
  1558. * which would cause log activity. Was that clear? I'm trying to
  1559. * avoid deadlock here.
  1560. */
  1561. init_waitqueue_head(&log->free_wait);
  1562. log->lbuf_free = NULL;
  1563. for (i = 0; i < LOGPAGES;) {
  1564. char *buffer;
  1565. uint offset;
  1566. struct page *page = alloc_page(GFP_KERNEL | __GFP_ZERO);
  1567. if (!page)
  1568. goto error;
  1569. buffer = page_address(page);
  1570. for (offset = 0; offset < PAGE_SIZE; offset += LOGPSIZE) {
  1571. lbuf = kmalloc(sizeof(struct lbuf), GFP_KERNEL);
  1572. if (lbuf == NULL) {
  1573. if (offset == 0)
  1574. __free_page(page);
  1575. goto error;
  1576. }
  1577. if (offset) /* we already have one reference */
  1578. get_page(page);
  1579. lbuf->l_offset = offset;
  1580. lbuf->l_ldata = buffer + offset;
  1581. lbuf->l_page = page;
  1582. lbuf->l_log = log;
  1583. init_waitqueue_head(&lbuf->l_ioevent);
  1584. lbuf->l_freelist = log->lbuf_free;
  1585. log->lbuf_free = lbuf;
  1586. i++;
  1587. }
  1588. }
  1589. return (0);
  1590. error:
  1591. lbmLogShutdown(log);
  1592. return -ENOMEM;
  1593. }
  1594. /*
  1595. * lbmLogShutdown()
  1596. *
  1597. * finalize per log I/O setup at lmLogShutdown()
  1598. */
  1599. static void lbmLogShutdown(struct jfs_log * log)
  1600. {
  1601. struct lbuf *lbuf;
  1602. jfs_info("lbmLogShutdown: log:0x%p", log);
  1603. lbuf = log->lbuf_free;
  1604. while (lbuf) {
  1605. struct lbuf *next = lbuf->l_freelist;
  1606. __free_page(lbuf->l_page);
  1607. kfree(lbuf);
  1608. lbuf = next;
  1609. }
  1610. }
  1611. /*
  1612. * lbmAllocate()
  1613. *
  1614. * allocate an empty log buffer
  1615. */
  1616. static struct lbuf *lbmAllocate(struct jfs_log * log, int pn)
  1617. {
  1618. struct lbuf *bp;
  1619. unsigned long flags;
  1620. /*
  1621. * recycle from log buffer freelist if any
  1622. */
  1623. LCACHE_LOCK(flags);
  1624. LCACHE_SLEEP_COND(log->free_wait, (bp = log->lbuf_free), flags);
  1625. log->lbuf_free = bp->l_freelist;
  1626. LCACHE_UNLOCK(flags);
  1627. bp->l_flag = 0;
  1628. bp->l_wqnext = NULL;
  1629. bp->l_freelist = NULL;
  1630. bp->l_pn = pn;
  1631. bp->l_blkno = log->base + (pn << (L2LOGPSIZE - log->l2bsize));
  1632. bp->l_ceor = 0;
  1633. return bp;
  1634. }
  1635. /*
  1636. * lbmFree()
  1637. *
  1638. * release a log buffer to freelist
  1639. */
  1640. static void lbmFree(struct lbuf * bp)
  1641. {
  1642. unsigned long flags;
  1643. LCACHE_LOCK(flags);
  1644. lbmfree(bp);
  1645. LCACHE_UNLOCK(flags);
  1646. }
  1647. static void lbmfree(struct lbuf * bp)
  1648. {
  1649. struct jfs_log *log = bp->l_log;
  1650. assert(bp->l_wqnext == NULL);
  1651. /*
  1652. * return the buffer to head of freelist
  1653. */
  1654. bp->l_freelist = log->lbuf_free;
  1655. log->lbuf_free = bp;
  1656. wake_up(&log->free_wait);
  1657. return;
  1658. }
  1659. /*
  1660. * NAME: lbmRedrive
  1661. *
  1662. * FUNCTION: add a log buffer to the log redrive list
  1663. *
  1664. * PARAMETER:
  1665. * bp - log buffer
  1666. *
  1667. * NOTES:
  1668. * Takes log_redrive_lock.
  1669. */
  1670. static inline void lbmRedrive(struct lbuf *bp)
  1671. {
  1672. unsigned long flags;
  1673. spin_lock_irqsave(&log_redrive_lock, flags);
  1674. bp->l_redrive_next = log_redrive_list;
  1675. log_redrive_list = bp;
  1676. spin_unlock_irqrestore(&log_redrive_lock, flags);
  1677. wake_up_process(jfsIOthread);
  1678. }
  1679. /*
  1680. * lbmRead()
  1681. */
  1682. static int lbmRead(struct jfs_log * log, int pn, struct lbuf ** bpp)
  1683. {
  1684. struct bio *bio;
  1685. struct lbuf *bp;
  1686. /*
  1687. * allocate a log buffer
  1688. */
  1689. *bpp = bp = lbmAllocate(log, pn);
  1690. jfs_info("lbmRead: bp:0x%p pn:0x%x", bp, pn);
  1691. bp->l_flag |= lbmREAD;
  1692. bio = bio_alloc(log->bdev, 1, REQ_OP_READ, GFP_NOFS);
  1693. bio->bi_iter.bi_sector = bp->l_blkno << (log->l2bsize - 9);
  1694. bio_add_page(bio, bp->l_page, LOGPSIZE, bp->l_offset);
  1695. BUG_ON(bio->bi_iter.bi_size != LOGPSIZE);
  1696. bio->bi_end_io = lbmIODone;
  1697. bio->bi_private = bp;
  1698. /*check if journaling to disk has been disabled*/
  1699. if (log->no_integrity) {
  1700. bio->bi_iter.bi_size = 0;
  1701. lbmIODone(bio);
  1702. } else {
  1703. submit_bio(bio);
  1704. }
  1705. wait_event(bp->l_ioevent, (bp->l_flag != lbmREAD));
  1706. return 0;
  1707. }
  1708. /*
  1709. * lbmWrite()
  1710. *
  1711. * buffer at head of pageout queue stays after completion of
  1712. * partial-page pageout and redriven by explicit initiation of
  1713. * pageout by caller until full-page pageout is completed and
  1714. * released.
  1715. *
  1716. * device driver i/o done redrives pageout of new buffer at
  1717. * head of pageout queue when current buffer at head of pageout
  1718. * queue is released at the completion of its full-page pageout.
  1719. *
  1720. * LOGGC_LOCK() serializes lbmWrite() by lmNextPage() and lmGroupCommit().
  1721. * LCACHE_LOCK() serializes xflag between lbmWrite() and lbmIODone()
  1722. */
  1723. static void lbmWrite(struct jfs_log * log, struct lbuf * bp, int flag,
  1724. int cant_block)
  1725. {
  1726. struct lbuf *tail;
  1727. unsigned long flags;
  1728. jfs_info("lbmWrite: bp:0x%p flag:0x%x pn:0x%x", bp, flag, bp->l_pn);
  1729. /* map the logical block address to physical block address */
  1730. bp->l_blkno =
  1731. log->base + (bp->l_pn << (L2LOGPSIZE - log->l2bsize));
  1732. LCACHE_LOCK(flags); /* disable+lock */
  1733. /*
  1734. * initialize buffer for device driver
  1735. */
  1736. bp->l_flag = flag;
  1737. /*
  1738. * insert bp at tail of write queue associated with log
  1739. *
  1740. * (request is either for bp already/currently at head of queue
  1741. * or new bp to be inserted at tail)
  1742. */
  1743. tail = log->wqueue;
  1744. /* is buffer not already on write queue ? */
  1745. if (bp->l_wqnext == NULL) {
  1746. /* insert at tail of wqueue */
  1747. if (tail == NULL) {
  1748. log->wqueue = bp;
  1749. bp->l_wqnext = bp;
  1750. } else {
  1751. log->wqueue = bp;
  1752. bp->l_wqnext = tail->l_wqnext;
  1753. tail->l_wqnext = bp;
  1754. }
  1755. tail = bp;
  1756. }
  1757. /* is buffer at head of wqueue and for write ? */
  1758. if ((bp != tail->l_wqnext) || !(flag & lbmWRITE)) {
  1759. LCACHE_UNLOCK(flags); /* unlock+enable */
  1760. return;
  1761. }
  1762. LCACHE_UNLOCK(flags); /* unlock+enable */
  1763. if (cant_block)
  1764. lbmRedrive(bp);
  1765. else if (flag & lbmSYNC)
  1766. lbmStartIO(bp);
  1767. else {
  1768. LOGGC_UNLOCK(log);
  1769. lbmStartIO(bp);
  1770. LOGGC_LOCK(log);
  1771. }
  1772. }
  1773. /*
  1774. * lbmDirectWrite()
  1775. *
  1776. * initiate pageout bypassing write queue for sidestream
  1777. * (e.g., log superblock) write;
  1778. */
  1779. static void lbmDirectWrite(struct jfs_log * log, struct lbuf * bp, int flag)
  1780. {
  1781. jfs_info("lbmDirectWrite: bp:0x%p flag:0x%x pn:0x%x",
  1782. bp, flag, bp->l_pn);
  1783. /*
  1784. * initialize buffer for device driver
  1785. */
  1786. bp->l_flag = flag | lbmDIRECT;
  1787. /* map the logical block address to physical block address */
  1788. bp->l_blkno =
  1789. log->base + (bp->l_pn << (L2LOGPSIZE - log->l2bsize));
  1790. /*
  1791. * initiate pageout of the page
  1792. */
  1793. lbmStartIO(bp);
  1794. }
  1795. /*
  1796. * NAME: lbmStartIO()
  1797. *
  1798. * FUNCTION: Interface to DD strategy routine
  1799. *
  1800. * RETURN: none
  1801. *
  1802. * serialization: LCACHE_LOCK() is NOT held during log i/o;
  1803. */
  1804. static void lbmStartIO(struct lbuf * bp)
  1805. {
  1806. struct bio *bio;
  1807. struct jfs_log *log = bp->l_log;
  1808. jfs_info("lbmStartIO");
  1809. bio = bio_alloc(log->bdev, 1, REQ_OP_WRITE | REQ_SYNC, GFP_NOFS);
  1810. bio->bi_iter.bi_sector = bp->l_blkno << (log->l2bsize - 9);
  1811. bio_add_page(bio, bp->l_page, LOGPSIZE, bp->l_offset);
  1812. BUG_ON(bio->bi_iter.bi_size != LOGPSIZE);
  1813. bio->bi_end_io = lbmIODone;
  1814. bio->bi_private = bp;
  1815. /* check if journaling to disk has been disabled */
  1816. if (log->no_integrity) {
  1817. bio->bi_iter.bi_size = 0;
  1818. lbmIODone(bio);
  1819. } else {
  1820. submit_bio(bio);
  1821. INCREMENT(lmStat.submitted);
  1822. }
  1823. }
  1824. /*
  1825. * lbmIOWait()
  1826. */
  1827. static int lbmIOWait(struct lbuf * bp, int flag)
  1828. {
  1829. unsigned long flags;
  1830. int rc = 0;
  1831. jfs_info("lbmIOWait1: bp:0x%p flag:0x%x:0x%x", bp, bp->l_flag, flag);
  1832. LCACHE_LOCK(flags); /* disable+lock */
  1833. LCACHE_SLEEP_COND(bp->l_ioevent, (bp->l_flag & lbmDONE), flags);
  1834. rc = (bp->l_flag & lbmERROR) ? -EIO : 0;
  1835. if (flag & lbmFREE)
  1836. lbmfree(bp);
  1837. LCACHE_UNLOCK(flags); /* unlock+enable */
  1838. jfs_info("lbmIOWait2: bp:0x%p flag:0x%x:0x%x", bp, bp->l_flag, flag);
  1839. return rc;
  1840. }
  1841. /*
  1842. * lbmIODone()
  1843. *
  1844. * executed at INTIODONE level
  1845. */
  1846. static void lbmIODone(struct bio *bio)
  1847. {
  1848. struct lbuf *bp = bio->bi_private;
  1849. struct lbuf *nextbp, *tail;
  1850. struct jfs_log *log;
  1851. unsigned long flags;
  1852. /*
  1853. * get back jfs buffer bound to the i/o buffer
  1854. */
  1855. jfs_info("lbmIODone: bp:0x%p flag:0x%x", bp, bp->l_flag);
  1856. LCACHE_LOCK(flags); /* disable+lock */
  1857. bp->l_flag |= lbmDONE;
  1858. if (bio->bi_status) {
  1859. bp->l_flag |= lbmERROR;
  1860. jfs_err("lbmIODone: I/O error in JFS log");
  1861. }
  1862. bio_put(bio);
  1863. /*
  1864. * pagein completion
  1865. */
  1866. if (bp->l_flag & lbmREAD) {
  1867. bp->l_flag &= ~lbmREAD;
  1868. LCACHE_UNLOCK(flags); /* unlock+enable */
  1869. /* wakeup I/O initiator */
  1870. LCACHE_WAKEUP(&bp->l_ioevent);
  1871. return;
  1872. }
  1873. /*
  1874. * pageout completion
  1875. *
  1876. * the bp at the head of write queue has completed pageout.
  1877. *
  1878. * if single-commit/full-page pageout, remove the current buffer
  1879. * from head of pageout queue, and redrive pageout with
  1880. * the new buffer at head of pageout queue;
  1881. * otherwise, the partial-page pageout buffer stays at
  1882. * the head of pageout queue to be redriven for pageout
  1883. * by lmGroupCommit() until full-page pageout is completed.
  1884. */
  1885. bp->l_flag &= ~lbmWRITE;
  1886. INCREMENT(lmStat.pagedone);
  1887. /* update committed lsn */
  1888. log = bp->l_log;
  1889. log->clsn = (bp->l_pn << L2LOGPSIZE) + bp->l_ceor;
  1890. if (bp->l_flag & lbmDIRECT) {
  1891. LCACHE_WAKEUP(&bp->l_ioevent);
  1892. LCACHE_UNLOCK(flags);
  1893. return;
  1894. }
  1895. tail = log->wqueue;
  1896. /* single element queue */
  1897. if (bp == tail) {
  1898. /* remove head buffer of full-page pageout
  1899. * from log device write queue
  1900. */
  1901. if (bp->l_flag & lbmRELEASE) {
  1902. log->wqueue = NULL;
  1903. bp->l_wqnext = NULL;
  1904. }
  1905. }
  1906. /* multi element queue */
  1907. else {
  1908. /* remove head buffer of full-page pageout
  1909. * from log device write queue
  1910. */
  1911. if (bp->l_flag & lbmRELEASE) {
  1912. nextbp = tail->l_wqnext = bp->l_wqnext;
  1913. bp->l_wqnext = NULL;
  1914. /*
  1915. * redrive pageout of next page at head of write queue:
  1916. * redrive next page without any bound tblk
  1917. * (i.e., page w/o any COMMIT records), or
  1918. * first page of new group commit which has been
  1919. * queued after current page (subsequent pageout
  1920. * is performed synchronously, except page without
  1921. * any COMMITs) by lmGroupCommit() as indicated
  1922. * by lbmWRITE flag;
  1923. */
  1924. if (nextbp->l_flag & lbmWRITE) {
  1925. /*
  1926. * We can't do the I/O at interrupt time.
  1927. * The jfsIO thread can do it
  1928. */
  1929. lbmRedrive(nextbp);
  1930. }
  1931. }
  1932. }
  1933. /*
  1934. * synchronous pageout:
  1935. *
  1936. * buffer has not necessarily been removed from write queue
  1937. * (e.g., synchronous write of partial-page with COMMIT):
  1938. * leave buffer for i/o initiator to dispose
  1939. */
  1940. if (bp->l_flag & lbmSYNC) {
  1941. LCACHE_UNLOCK(flags); /* unlock+enable */
  1942. /* wakeup I/O initiator */
  1943. LCACHE_WAKEUP(&bp->l_ioevent);
  1944. }
  1945. /*
  1946. * Group Commit pageout:
  1947. */
  1948. else if (bp->l_flag & lbmGC) {
  1949. LCACHE_UNLOCK(flags);
  1950. lmPostGC(bp);
  1951. }
  1952. /*
  1953. * asynchronous pageout:
  1954. *
  1955. * buffer must have been removed from write queue:
  1956. * insert buffer at head of freelist where it can be recycled
  1957. */
  1958. else {
  1959. assert(bp->l_flag & lbmRELEASE);
  1960. assert(bp->l_flag & lbmFREE);
  1961. lbmfree(bp);
  1962. LCACHE_UNLOCK(flags); /* unlock+enable */
  1963. }
  1964. }
  1965. int jfsIOWait(void *arg)
  1966. {
  1967. struct lbuf *bp;
  1968. do {
  1969. spin_lock_irq(&log_redrive_lock);
  1970. while ((bp = log_redrive_list)) {
  1971. log_redrive_list = bp->l_redrive_next;
  1972. bp->l_redrive_next = NULL;
  1973. spin_unlock_irq(&log_redrive_lock);
  1974. lbmStartIO(bp);
  1975. spin_lock_irq(&log_redrive_lock);
  1976. }
  1977. if (freezing(current)) {
  1978. spin_unlock_irq(&log_redrive_lock);
  1979. try_to_freeze();
  1980. } else {
  1981. set_current_state(TASK_INTERRUPTIBLE);
  1982. spin_unlock_irq(&log_redrive_lock);
  1983. schedule();
  1984. }
  1985. } while (!kthread_should_stop());
  1986. jfs_info("jfsIOWait being killed!");
  1987. return 0;
  1988. }
  1989. /*
  1990. * NAME: lmLogFormat()/jfs_logform()
  1991. *
  1992. * FUNCTION: format file system log
  1993. *
  1994. * PARAMETERS:
  1995. * log - volume log
  1996. * logAddress - start address of log space in FS block
  1997. * logSize - length of log space in FS block;
  1998. *
  1999. * RETURN: 0 - success
  2000. * -EIO - i/o error
  2001. *
  2002. * XXX: We're synchronously writing one page at a time. This needs to
  2003. * be improved by writing multiple pages at once.
  2004. */
  2005. int lmLogFormat(struct jfs_log *log, s64 logAddress, int logSize)
  2006. {
  2007. int rc = -EIO;
  2008. struct jfs_sb_info *sbi;
  2009. struct logsuper *logsuper;
  2010. struct logpage *lp;
  2011. int lspn; /* log sequence page number */
  2012. struct lrd *lrd_ptr;
  2013. int npages = 0;
  2014. struct lbuf *bp;
  2015. jfs_info("lmLogFormat: logAddress:%Ld logSize:%d",
  2016. (long long)logAddress, logSize);
  2017. sbi = list_entry(log->sb_list.next, struct jfs_sb_info, log_list);
  2018. /* allocate a log buffer */
  2019. bp = lbmAllocate(log, 1);
  2020. npages = logSize >> sbi->l2nbperpage;
  2021. /*
  2022. * log space:
  2023. *
  2024. * page 0 - reserved;
  2025. * page 1 - log superblock;
  2026. * page 2 - log data page: A SYNC log record is written
  2027. * into this page at logform time;
  2028. * pages 3-N - log data page: set to empty log data pages;
  2029. */
  2030. /*
  2031. * init log superblock: log page 1
  2032. */
  2033. logsuper = (struct logsuper *) bp->l_ldata;
  2034. logsuper->magic = cpu_to_le32(LOGMAGIC);
  2035. logsuper->version = cpu_to_le32(LOGVERSION);
  2036. logsuper->state = cpu_to_le32(LOGREDONE);
  2037. logsuper->flag = cpu_to_le32(sbi->mntflag); /* ? */
  2038. logsuper->size = cpu_to_le32(npages);
  2039. logsuper->bsize = cpu_to_le32(sbi->bsize);
  2040. logsuper->l2bsize = cpu_to_le32(sbi->l2bsize);
  2041. logsuper->end = cpu_to_le32(2 * LOGPSIZE + LOGPHDRSIZE + LOGRDSIZE);
  2042. bp->l_flag = lbmWRITE | lbmSYNC | lbmDIRECT;
  2043. bp->l_blkno = logAddress + sbi->nbperpage;
  2044. lbmStartIO(bp);
  2045. if ((rc = lbmIOWait(bp, 0)))
  2046. goto exit;
  2047. /*
  2048. * init pages 2 to npages-1 as log data pages:
  2049. *
  2050. * log page sequence number (lpsn) initialization:
  2051. *
  2052. * pn: 0 1 2 3 n-1
  2053. * +-----+-----+=====+=====+===.....===+=====+
  2054. * lspn: N-1 0 1 N-2
  2055. * <--- N page circular file ---->
  2056. *
  2057. * the N (= npages-2) data pages of the log is maintained as
  2058. * a circular file for the log records;
  2059. * lpsn grows by 1 monotonically as each log page is written
  2060. * to the circular file of the log;
  2061. * and setLogpage() will not reset the page number even if
  2062. * the eor is equal to LOGPHDRSIZE. In order for binary search
  2063. * still work in find log end process, we have to simulate the
  2064. * log wrap situation at the log format time.
  2065. * The 1st log page written will have the highest lpsn. Then
  2066. * the succeeding log pages will have ascending order of
  2067. * the lspn starting from 0, ... (N-2)
  2068. */
  2069. lp = (struct logpage *) bp->l_ldata;
  2070. /*
  2071. * initialize 1st log page to be written: lpsn = N - 1,
  2072. * write a SYNCPT log record is written to this page
  2073. */
  2074. lp->h.page = lp->t.page = cpu_to_le32(npages - 3);
  2075. lp->h.eor = lp->t.eor = cpu_to_le16(LOGPHDRSIZE + LOGRDSIZE);
  2076. lrd_ptr = (struct lrd *) &lp->data;
  2077. lrd_ptr->logtid = 0;
  2078. lrd_ptr->backchain = 0;
  2079. lrd_ptr->type = cpu_to_le16(LOG_SYNCPT);
  2080. lrd_ptr->length = 0;
  2081. lrd_ptr->log.syncpt.sync = 0;
  2082. bp->l_blkno += sbi->nbperpage;
  2083. bp->l_flag = lbmWRITE | lbmSYNC | lbmDIRECT;
  2084. lbmStartIO(bp);
  2085. if ((rc = lbmIOWait(bp, 0)))
  2086. goto exit;
  2087. /*
  2088. * initialize succeeding log pages: lpsn = 0, 1, ..., (N-2)
  2089. */
  2090. for (lspn = 0; lspn < npages - 3; lspn++) {
  2091. lp->h.page = lp->t.page = cpu_to_le32(lspn);
  2092. lp->h.eor = lp->t.eor = cpu_to_le16(LOGPHDRSIZE);
  2093. bp->l_blkno += sbi->nbperpage;
  2094. bp->l_flag = lbmWRITE | lbmSYNC | lbmDIRECT;
  2095. lbmStartIO(bp);
  2096. if ((rc = lbmIOWait(bp, 0)))
  2097. goto exit;
  2098. }
  2099. rc = 0;
  2100. exit:
  2101. /*
  2102. * finalize log
  2103. */
  2104. /* release the buffer */
  2105. lbmFree(bp);
  2106. return rc;
  2107. }
  2108. #ifdef CONFIG_JFS_STATISTICS
  2109. int jfs_lmstats_proc_show(struct seq_file *m, void *v)
  2110. {
  2111. seq_printf(m,
  2112. "JFS Logmgr stats\n"
  2113. "================\n"
  2114. "commits = %d\n"
  2115. "writes submitted = %d\n"
  2116. "writes completed = %d\n"
  2117. "full pages submitted = %d\n"
  2118. "partial pages submitted = %d\n",
  2119. lmStat.commit,
  2120. lmStat.submitted,
  2121. lmStat.pagedone,
  2122. lmStat.full_page,
  2123. lmStat.partial_page);
  2124. return 0;
  2125. }
  2126. #endif /* CONFIG_JFS_STATISTICS */