jfs_logmgr.c 59 KB

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