jfs_logmgr.c 59 KB

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