jfs_logmgr.c 59 KB

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