jfs_logmgr.c 59 KB

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