jfs_logmgr.c 59 KB

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