jfs_logmgr.c 59 KB

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