jfs_logmgr.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514
  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");
  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 size:0x%x",
  1155. log, (unsigned long long)log->base, log->size);
  1156. } else {
  1157. if (memcmp(logsuper->uuid, log->uuid, 16)) {
  1158. jfs_warn("wrong uuid on JFS log device");
  1159. goto errout20;
  1160. }
  1161. log->size = le32_to_cpu(logsuper->size);
  1162. log->l2bsize = le32_to_cpu(logsuper->l2bsize);
  1163. jfs_info("lmLogInit: external log:0x%p base:0x%Lx size:0x%x",
  1164. log, (unsigned long long)log->base, log->size);
  1165. }
  1166. log->page = le32_to_cpu(logsuper->end) / LOGPSIZE;
  1167. log->eor = le32_to_cpu(logsuper->end) - (LOGPSIZE * log->page);
  1168. /*
  1169. * initialize for log append write mode
  1170. */
  1171. /* establish current/end-of-log page/buffer */
  1172. if ((rc = lbmRead(log, log->page, &bp)))
  1173. goto errout20;
  1174. lp = (struct logpage *) bp->l_ldata;
  1175. jfs_info("lmLogInit: lsn:0x%x page:%d eor:%d:%d",
  1176. le32_to_cpu(logsuper->end), log->page, log->eor,
  1177. le16_to_cpu(lp->h.eor));
  1178. log->bp = bp;
  1179. bp->l_pn = log->page;
  1180. bp->l_eor = log->eor;
  1181. /* if current page is full, move on to next page */
  1182. if (log->eor >= LOGPSIZE - LOGPTLRSIZE)
  1183. lmNextPage(log);
  1184. /*
  1185. * initialize log syncpoint
  1186. */
  1187. /*
  1188. * write the first SYNCPT record with syncpoint = 0
  1189. * (i.e., log redo up to HERE !);
  1190. * remove current page from lbm write queue at end of pageout
  1191. * (to write log superblock update), but do not release to
  1192. * freelist;
  1193. */
  1194. lrd.logtid = 0;
  1195. lrd.backchain = 0;
  1196. lrd.type = cpu_to_le16(LOG_SYNCPT);
  1197. lrd.length = 0;
  1198. lrd.log.syncpt.sync = 0;
  1199. lsn = lmWriteRecord(log, NULL, &lrd, NULL);
  1200. bp = log->bp;
  1201. bp->l_ceor = bp->l_eor;
  1202. lp = (struct logpage *) bp->l_ldata;
  1203. lp->h.eor = lp->t.eor = cpu_to_le16(bp->l_eor);
  1204. lbmWrite(log, bp, lbmWRITE | lbmSYNC, 0);
  1205. if ((rc = lbmIOWait(bp, 0)))
  1206. goto errout30;
  1207. /*
  1208. * update/write superblock
  1209. */
  1210. logsuper->state = cpu_to_le32(LOGMOUNT);
  1211. log->serial = le32_to_cpu(logsuper->serial) + 1;
  1212. logsuper->serial = cpu_to_le32(log->serial);
  1213. lbmDirectWrite(log, bpsuper, lbmWRITE | lbmRELEASE | lbmSYNC);
  1214. if ((rc = lbmIOWait(bpsuper, lbmFREE)))
  1215. goto errout30;
  1216. }
  1217. /* initialize logsync parameters */
  1218. log->logsize = (log->size - 2) << L2LOGPSIZE;
  1219. log->lsn = lsn;
  1220. log->syncpt = lsn;
  1221. log->sync = log->syncpt;
  1222. log->nextsync = LOGSYNC_DELTA(log->logsize);
  1223. jfs_info("lmLogInit: lsn:0x%x syncpt:0x%x sync:0x%x",
  1224. log->lsn, log->syncpt, log->sync);
  1225. /*
  1226. * initialize for lazy/group commit
  1227. */
  1228. log->clsn = lsn;
  1229. return 0;
  1230. /*
  1231. * unwind on error
  1232. */
  1233. errout30: /* release log page */
  1234. log->wqueue = NULL;
  1235. bp->l_wqnext = NULL;
  1236. lbmFree(bp);
  1237. errout20: /* release log superblock */
  1238. lbmFree(bpsuper);
  1239. errout10: /* unwind lbmLogInit() */
  1240. lbmLogShutdown(log);
  1241. jfs_warn("lmLogInit: exit(%d)", rc);
  1242. return rc;
  1243. }
  1244. /*
  1245. * NAME: lmLogClose()
  1246. *
  1247. * FUNCTION: remove file system <ipmnt> from active list of log <iplog>
  1248. * and close it on last close.
  1249. *
  1250. * PARAMETER: sb - superblock
  1251. *
  1252. * RETURN: errors from subroutines
  1253. *
  1254. * serialization:
  1255. */
  1256. int lmLogClose(struct super_block *sb)
  1257. {
  1258. struct jfs_sb_info *sbi = JFS_SBI(sb);
  1259. struct jfs_log *log = sbi->log;
  1260. struct block_device *bdev;
  1261. int rc = 0;
  1262. jfs_info("lmLogClose: log:0x%p", log);
  1263. mutex_lock(&jfs_log_mutex);
  1264. LOG_LOCK(log);
  1265. list_del(&sbi->log_list);
  1266. LOG_UNLOCK(log);
  1267. sbi->log = NULL;
  1268. /*
  1269. * We need to make sure all of the "written" metapages
  1270. * actually make it to disk
  1271. */
  1272. sync_blockdev(sb->s_bdev);
  1273. if (test_bit(log_INLINELOG, &log->flag)) {
  1274. /*
  1275. * in-line log in host file system
  1276. */
  1277. rc = lmLogShutdown(log);
  1278. kfree(log);
  1279. goto out;
  1280. }
  1281. if (!log->no_integrity)
  1282. lmLogFileSystem(log, sbi, 0);
  1283. if (!list_empty(&log->sb_list))
  1284. goto out;
  1285. /*
  1286. * TODO: ensure that the dummy_log is in a state to allow
  1287. * lbmLogShutdown to deallocate all the buffers and call
  1288. * kfree against dummy_log. For now, leave dummy_log & its
  1289. * buffers in memory, and resuse if another no-integrity mount
  1290. * is requested.
  1291. */
  1292. if (log->no_integrity)
  1293. goto out;
  1294. /*
  1295. * external log as separate logical volume
  1296. */
  1297. list_del(&log->journal_list);
  1298. bdev = log->bdev;
  1299. rc = lmLogShutdown(log);
  1300. blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
  1301. kfree(log);
  1302. out:
  1303. mutex_unlock(&jfs_log_mutex);
  1304. jfs_info("lmLogClose: exit(%d)", rc);
  1305. return rc;
  1306. }
  1307. /*
  1308. * NAME: jfs_flush_journal()
  1309. *
  1310. * FUNCTION: initiate write of any outstanding transactions to the journal
  1311. * and optionally wait until they are all written to disk
  1312. *
  1313. * wait == 0 flush until latest txn is committed, don't wait
  1314. * wait == 1 flush until latest txn is committed, wait
  1315. * wait > 1 flush until all txn's are complete, wait
  1316. */
  1317. void jfs_flush_journal(struct jfs_log *log, int wait)
  1318. {
  1319. int i;
  1320. struct tblock *target = NULL;
  1321. /* jfs_write_inode may call us during read-only mount */
  1322. if (!log)
  1323. return;
  1324. jfs_info("jfs_flush_journal: log:0x%p wait=%d", log, wait);
  1325. LOGGC_LOCK(log);
  1326. if (!list_empty(&log->cqueue)) {
  1327. /*
  1328. * This ensures that we will keep writing to the journal as long
  1329. * as there are unwritten commit records
  1330. */
  1331. target = list_entry(log->cqueue.prev, struct tblock, cqueue);
  1332. if (test_bit(log_FLUSH, &log->flag)) {
  1333. /*
  1334. * We're already flushing.
  1335. * if flush_tblk is NULL, we are flushing everything,
  1336. * so leave it that way. Otherwise, update it to the
  1337. * latest transaction
  1338. */
  1339. if (log->flush_tblk)
  1340. log->flush_tblk = target;
  1341. } else {
  1342. /* Only flush until latest transaction is committed */
  1343. log->flush_tblk = target;
  1344. set_bit(log_FLUSH, &log->flag);
  1345. /*
  1346. * Initiate I/O on outstanding transactions
  1347. */
  1348. if (!(log->cflag & logGC_PAGEOUT)) {
  1349. log->cflag |= logGC_PAGEOUT;
  1350. lmGCwrite(log, 0);
  1351. }
  1352. }
  1353. }
  1354. if ((wait > 1) || test_bit(log_SYNCBARRIER, &log->flag)) {
  1355. /* Flush until all activity complete */
  1356. set_bit(log_FLUSH, &log->flag);
  1357. log->flush_tblk = NULL;
  1358. }
  1359. if (wait && target && !(target->flag & tblkGC_COMMITTED)) {
  1360. DECLARE_WAITQUEUE(__wait, current);
  1361. add_wait_queue(&target->gcwait, &__wait);
  1362. set_current_state(TASK_UNINTERRUPTIBLE);
  1363. LOGGC_UNLOCK(log);
  1364. schedule();
  1365. LOGGC_LOCK(log);
  1366. remove_wait_queue(&target->gcwait, &__wait);
  1367. }
  1368. LOGGC_UNLOCK(log);
  1369. if (wait < 2)
  1370. return;
  1371. write_special_inodes(log, filemap_fdatawrite);
  1372. /*
  1373. * If there was recent activity, we may need to wait
  1374. * for the lazycommit thread to catch up
  1375. */
  1376. if ((!list_empty(&log->cqueue)) || !list_empty(&log->synclist)) {
  1377. for (i = 0; i < 200; i++) { /* Too much? */
  1378. msleep(250);
  1379. write_special_inodes(log, filemap_fdatawrite);
  1380. if (list_empty(&log->cqueue) &&
  1381. list_empty(&log->synclist))
  1382. break;
  1383. }
  1384. }
  1385. assert(list_empty(&log->cqueue));
  1386. #ifdef CONFIG_JFS_DEBUG
  1387. if (!list_empty(&log->synclist)) {
  1388. struct logsyncblk *lp;
  1389. printk(KERN_ERR "jfs_flush_journal: synclist not empty\n");
  1390. list_for_each_entry(lp, &log->synclist, synclist) {
  1391. if (lp->xflag & COMMIT_PAGE) {
  1392. struct metapage *mp = (struct metapage *)lp;
  1393. print_hex_dump(KERN_ERR, "metapage: ",
  1394. DUMP_PREFIX_ADDRESS, 16, 4,
  1395. mp, sizeof(struct metapage), 0);
  1396. print_hex_dump(KERN_ERR, "page: ",
  1397. DUMP_PREFIX_ADDRESS, 16,
  1398. sizeof(long), mp->page,
  1399. sizeof(struct page), 0);
  1400. } else
  1401. print_hex_dump(KERN_ERR, "tblock:",
  1402. DUMP_PREFIX_ADDRESS, 16, 4,
  1403. lp, sizeof(struct tblock), 0);
  1404. }
  1405. }
  1406. #else
  1407. WARN_ON(!list_empty(&log->synclist));
  1408. #endif
  1409. clear_bit(log_FLUSH, &log->flag);
  1410. }
  1411. /*
  1412. * NAME: lmLogShutdown()
  1413. *
  1414. * FUNCTION: log shutdown at last LogClose().
  1415. *
  1416. * write log syncpt record.
  1417. * update super block to set redone flag to 0.
  1418. *
  1419. * PARAMETER: log - log inode
  1420. *
  1421. * RETURN: 0 - success
  1422. *
  1423. * serialization: single last close thread
  1424. */
  1425. int lmLogShutdown(struct jfs_log * log)
  1426. {
  1427. int rc;
  1428. struct lrd lrd;
  1429. int lsn;
  1430. struct logsuper *logsuper;
  1431. struct lbuf *bpsuper;
  1432. struct lbuf *bp;
  1433. struct logpage *lp;
  1434. jfs_info("lmLogShutdown: log:0x%p", log);
  1435. jfs_flush_journal(log, 2);
  1436. /*
  1437. * write the last SYNCPT record with syncpoint = 0
  1438. * (i.e., log redo up to HERE !)
  1439. */
  1440. lrd.logtid = 0;
  1441. lrd.backchain = 0;
  1442. lrd.type = cpu_to_le16(LOG_SYNCPT);
  1443. lrd.length = 0;
  1444. lrd.log.syncpt.sync = 0;
  1445. lsn = lmWriteRecord(log, NULL, &lrd, NULL);
  1446. bp = log->bp;
  1447. lp = (struct logpage *) bp->l_ldata;
  1448. lp->h.eor = lp->t.eor = cpu_to_le16(bp->l_eor);
  1449. lbmWrite(log, log->bp, lbmWRITE | lbmRELEASE | lbmSYNC, 0);
  1450. lbmIOWait(log->bp, lbmFREE);
  1451. log->bp = NULL;
  1452. /*
  1453. * synchronous update log superblock
  1454. * mark log state as shutdown cleanly
  1455. * (i.e., Log does not need to be replayed).
  1456. */
  1457. if ((rc = lbmRead(log, 1, &bpsuper)))
  1458. goto out;
  1459. logsuper = (struct logsuper *) bpsuper->l_ldata;
  1460. logsuper->state = cpu_to_le32(LOGREDONE);
  1461. logsuper->end = cpu_to_le32(lsn);
  1462. lbmDirectWrite(log, bpsuper, lbmWRITE | lbmRELEASE | lbmSYNC);
  1463. rc = lbmIOWait(bpsuper, lbmFREE);
  1464. jfs_info("lmLogShutdown: lsn:0x%x page:%d eor:%d",
  1465. lsn, log->page, log->eor);
  1466. out:
  1467. /*
  1468. * shutdown per log i/o
  1469. */
  1470. lbmLogShutdown(log);
  1471. if (rc) {
  1472. jfs_warn("lmLogShutdown: exit(%d)", rc);
  1473. }
  1474. return rc;
  1475. }
  1476. /*
  1477. * NAME: lmLogFileSystem()
  1478. *
  1479. * FUNCTION: insert (<activate> = true)/remove (<activate> = false)
  1480. * file system into/from log active file system list.
  1481. *
  1482. * PARAMETE: log - pointer to logs inode.
  1483. * fsdev - kdev_t of filesystem.
  1484. * serial - pointer to returned log serial number
  1485. * activate - insert/remove device from active list.
  1486. *
  1487. * RETURN: 0 - success
  1488. * errors returned by vms_iowait().
  1489. */
  1490. static int lmLogFileSystem(struct jfs_log * log, struct jfs_sb_info *sbi,
  1491. int activate)
  1492. {
  1493. int rc = 0;
  1494. int i;
  1495. struct logsuper *logsuper;
  1496. struct lbuf *bpsuper;
  1497. char *uuid = sbi->uuid;
  1498. /*
  1499. * insert/remove file system device to log active file system list.
  1500. */
  1501. if ((rc = lbmRead(log, 1, &bpsuper)))
  1502. return rc;
  1503. logsuper = (struct logsuper *) bpsuper->l_ldata;
  1504. if (activate) {
  1505. for (i = 0; i < MAX_ACTIVE; i++)
  1506. if (!memcmp(logsuper->active[i].uuid, NULL_UUID, 16)) {
  1507. memcpy(logsuper->active[i].uuid, uuid, 16);
  1508. sbi->aggregate = i;
  1509. break;
  1510. }
  1511. if (i == MAX_ACTIVE) {
  1512. jfs_warn("Too many file systems sharing journal!");
  1513. lbmFree(bpsuper);
  1514. return -EMFILE; /* Is there a better rc? */
  1515. }
  1516. } else {
  1517. for (i = 0; i < MAX_ACTIVE; i++)
  1518. if (!memcmp(logsuper->active[i].uuid, uuid, 16)) {
  1519. memcpy(logsuper->active[i].uuid, NULL_UUID, 16);
  1520. break;
  1521. }
  1522. if (i == MAX_ACTIVE) {
  1523. jfs_warn("Somebody stomped on the journal!");
  1524. lbmFree(bpsuper);
  1525. return -EIO;
  1526. }
  1527. }
  1528. /*
  1529. * synchronous write log superblock:
  1530. *
  1531. * write sidestream bypassing write queue:
  1532. * at file system mount, log super block is updated for
  1533. * activation of the file system before any log record
  1534. * (MOUNT record) of the file system, and at file system
  1535. * unmount, all meta data for the file system has been
  1536. * flushed before log super block is updated for deactivation
  1537. * of the file system.
  1538. */
  1539. lbmDirectWrite(log, bpsuper, lbmWRITE | lbmRELEASE | lbmSYNC);
  1540. rc = lbmIOWait(bpsuper, lbmFREE);
  1541. return rc;
  1542. }
  1543. /*
  1544. * log buffer manager (lbm)
  1545. * ------------------------
  1546. *
  1547. * special purpose buffer manager supporting log i/o requirements.
  1548. *
  1549. * per log write queue:
  1550. * log pageout occurs in serial order by fifo write queue and
  1551. * restricting to a single i/o in pregress at any one time.
  1552. * a circular singly-linked list
  1553. * (log->wrqueue points to the tail, and buffers are linked via
  1554. * bp->wrqueue field), and
  1555. * maintains log page in pageout ot waiting for pageout in serial pageout.
  1556. */
  1557. /*
  1558. * lbmLogInit()
  1559. *
  1560. * initialize per log I/O setup at lmLogInit()
  1561. */
  1562. static int lbmLogInit(struct jfs_log * log)
  1563. { /* log inode */
  1564. int i;
  1565. struct lbuf *lbuf;
  1566. jfs_info("lbmLogInit: log:0x%p", log);
  1567. /* initialize current buffer cursor */
  1568. log->bp = NULL;
  1569. /* initialize log device write queue */
  1570. log->wqueue = NULL;
  1571. /*
  1572. * Each log has its own buffer pages allocated to it. These are
  1573. * not managed by the page cache. This ensures that a transaction
  1574. * writing to the log does not block trying to allocate a page from
  1575. * the page cache (for the log). This would be bad, since page
  1576. * allocation waits on the kswapd thread that may be committing inodes
  1577. * which would cause log activity. Was that clear? I'm trying to
  1578. * avoid deadlock here.
  1579. */
  1580. init_waitqueue_head(&log->free_wait);
  1581. log->lbuf_free = NULL;
  1582. for (i = 0; i < LOGPAGES;) {
  1583. char *buffer;
  1584. uint offset;
  1585. struct page *page = alloc_page(GFP_KERNEL | __GFP_ZERO);
  1586. if (!page)
  1587. goto error;
  1588. buffer = page_address(page);
  1589. for (offset = 0; offset < PAGE_SIZE; offset += LOGPSIZE) {
  1590. lbuf = kmalloc(sizeof(struct lbuf), GFP_KERNEL);
  1591. if (lbuf == NULL) {
  1592. if (offset == 0)
  1593. __free_page(page);
  1594. goto error;
  1595. }
  1596. if (offset) /* we already have one reference */
  1597. get_page(page);
  1598. lbuf->l_offset = offset;
  1599. lbuf->l_ldata = buffer + offset;
  1600. lbuf->l_page = page;
  1601. lbuf->l_log = log;
  1602. init_waitqueue_head(&lbuf->l_ioevent);
  1603. lbuf->l_freelist = log->lbuf_free;
  1604. log->lbuf_free = lbuf;
  1605. i++;
  1606. }
  1607. }
  1608. return (0);
  1609. error:
  1610. lbmLogShutdown(log);
  1611. return -ENOMEM;
  1612. }
  1613. /*
  1614. * lbmLogShutdown()
  1615. *
  1616. * finalize per log I/O setup at lmLogShutdown()
  1617. */
  1618. static void lbmLogShutdown(struct jfs_log * log)
  1619. {
  1620. struct lbuf *lbuf;
  1621. jfs_info("lbmLogShutdown: log:0x%p", log);
  1622. lbuf = log->lbuf_free;
  1623. while (lbuf) {
  1624. struct lbuf *next = lbuf->l_freelist;
  1625. __free_page(lbuf->l_page);
  1626. kfree(lbuf);
  1627. lbuf = next;
  1628. }
  1629. }
  1630. /*
  1631. * lbmAllocate()
  1632. *
  1633. * allocate an empty log buffer
  1634. */
  1635. static struct lbuf *lbmAllocate(struct jfs_log * log, int pn)
  1636. {
  1637. struct lbuf *bp;
  1638. unsigned long flags;
  1639. /*
  1640. * recycle from log buffer freelist if any
  1641. */
  1642. LCACHE_LOCK(flags);
  1643. LCACHE_SLEEP_COND(log->free_wait, (bp = log->lbuf_free), flags);
  1644. log->lbuf_free = bp->l_freelist;
  1645. LCACHE_UNLOCK(flags);
  1646. bp->l_flag = 0;
  1647. bp->l_wqnext = NULL;
  1648. bp->l_freelist = NULL;
  1649. bp->l_pn = pn;
  1650. bp->l_blkno = log->base + (pn << (L2LOGPSIZE - log->l2bsize));
  1651. bp->l_ceor = 0;
  1652. return bp;
  1653. }
  1654. /*
  1655. * lbmFree()
  1656. *
  1657. * release a log buffer to freelist
  1658. */
  1659. static void lbmFree(struct lbuf * bp)
  1660. {
  1661. unsigned long flags;
  1662. LCACHE_LOCK(flags);
  1663. lbmfree(bp);
  1664. LCACHE_UNLOCK(flags);
  1665. }
  1666. static void lbmfree(struct lbuf * bp)
  1667. {
  1668. struct jfs_log *log = bp->l_log;
  1669. assert(bp->l_wqnext == NULL);
  1670. /*
  1671. * return the buffer to head of freelist
  1672. */
  1673. bp->l_freelist = log->lbuf_free;
  1674. log->lbuf_free = bp;
  1675. wake_up(&log->free_wait);
  1676. return;
  1677. }
  1678. /*
  1679. * NAME: lbmRedrive
  1680. *
  1681. * FUNCTION: add a log buffer to the log redrive list
  1682. *
  1683. * PARAMETER:
  1684. * bp - log buffer
  1685. *
  1686. * NOTES:
  1687. * Takes log_redrive_lock.
  1688. */
  1689. static inline void lbmRedrive(struct lbuf *bp)
  1690. {
  1691. unsigned long flags;
  1692. spin_lock_irqsave(&log_redrive_lock, flags);
  1693. bp->l_redrive_next = log_redrive_list;
  1694. log_redrive_list = bp;
  1695. spin_unlock_irqrestore(&log_redrive_lock, flags);
  1696. wake_up_process(jfsIOthread);
  1697. }
  1698. /*
  1699. * lbmRead()
  1700. */
  1701. static int lbmRead(struct jfs_log * log, int pn, struct lbuf ** bpp)
  1702. {
  1703. struct bio *bio;
  1704. struct lbuf *bp;
  1705. /*
  1706. * allocate a log buffer
  1707. */
  1708. *bpp = bp = lbmAllocate(log, pn);
  1709. jfs_info("lbmRead: bp:0x%p pn:0x%x", bp, pn);
  1710. bp->l_flag |= lbmREAD;
  1711. bio = bio_alloc(GFP_NOFS, 1);
  1712. bio->bi_iter.bi_sector = bp->l_blkno << (log->l2bsize - 9);
  1713. bio_set_dev(bio, log->bdev);
  1714. bio_add_page(bio, bp->l_page, LOGPSIZE, bp->l_offset);
  1715. BUG_ON(bio->bi_iter.bi_size != LOGPSIZE);
  1716. bio->bi_end_io = lbmIODone;
  1717. bio->bi_private = bp;
  1718. bio->bi_opf = REQ_OP_READ;
  1719. /*check if journaling to disk has been disabled*/
  1720. if (log->no_integrity) {
  1721. bio->bi_iter.bi_size = 0;
  1722. lbmIODone(bio);
  1723. } else {
  1724. submit_bio(bio);
  1725. }
  1726. wait_event(bp->l_ioevent, (bp->l_flag != lbmREAD));
  1727. return 0;
  1728. }
  1729. /*
  1730. * lbmWrite()
  1731. *
  1732. * buffer at head of pageout queue stays after completion of
  1733. * partial-page pageout and redriven by explicit initiation of
  1734. * pageout by caller until full-page pageout is completed and
  1735. * released.
  1736. *
  1737. * device driver i/o done redrives pageout of new buffer at
  1738. * head of pageout queue when current buffer at head of pageout
  1739. * queue is released at the completion of its full-page pageout.
  1740. *
  1741. * LOGGC_LOCK() serializes lbmWrite() by lmNextPage() and lmGroupCommit().
  1742. * LCACHE_LOCK() serializes xflag between lbmWrite() and lbmIODone()
  1743. */
  1744. static void lbmWrite(struct jfs_log * log, struct lbuf * bp, int flag,
  1745. int cant_block)
  1746. {
  1747. struct lbuf *tail;
  1748. unsigned long flags;
  1749. jfs_info("lbmWrite: bp:0x%p flag:0x%x pn:0x%x", bp, flag, bp->l_pn);
  1750. /* map the logical block address to physical block address */
  1751. bp->l_blkno =
  1752. log->base + (bp->l_pn << (L2LOGPSIZE - log->l2bsize));
  1753. LCACHE_LOCK(flags); /* disable+lock */
  1754. /*
  1755. * initialize buffer for device driver
  1756. */
  1757. bp->l_flag = flag;
  1758. /*
  1759. * insert bp at tail of write queue associated with log
  1760. *
  1761. * (request is either for bp already/currently at head of queue
  1762. * or new bp to be inserted at tail)
  1763. */
  1764. tail = log->wqueue;
  1765. /* is buffer not already on write queue ? */
  1766. if (bp->l_wqnext == NULL) {
  1767. /* insert at tail of wqueue */
  1768. if (tail == NULL) {
  1769. log->wqueue = bp;
  1770. bp->l_wqnext = bp;
  1771. } else {
  1772. log->wqueue = bp;
  1773. bp->l_wqnext = tail->l_wqnext;
  1774. tail->l_wqnext = bp;
  1775. }
  1776. tail = bp;
  1777. }
  1778. /* is buffer at head of wqueue and for write ? */
  1779. if ((bp != tail->l_wqnext) || !(flag & lbmWRITE)) {
  1780. LCACHE_UNLOCK(flags); /* unlock+enable */
  1781. return;
  1782. }
  1783. LCACHE_UNLOCK(flags); /* unlock+enable */
  1784. if (cant_block)
  1785. lbmRedrive(bp);
  1786. else if (flag & lbmSYNC)
  1787. lbmStartIO(bp);
  1788. else {
  1789. LOGGC_UNLOCK(log);
  1790. lbmStartIO(bp);
  1791. LOGGC_LOCK(log);
  1792. }
  1793. }
  1794. /*
  1795. * lbmDirectWrite()
  1796. *
  1797. * initiate pageout bypassing write queue for sidestream
  1798. * (e.g., log superblock) write;
  1799. */
  1800. static void lbmDirectWrite(struct jfs_log * log, struct lbuf * bp, int flag)
  1801. {
  1802. jfs_info("lbmDirectWrite: bp:0x%p flag:0x%x pn:0x%x",
  1803. bp, flag, bp->l_pn);
  1804. /*
  1805. * initialize buffer for device driver
  1806. */
  1807. bp->l_flag = flag | lbmDIRECT;
  1808. /* map the logical block address to physical block address */
  1809. bp->l_blkno =
  1810. log->base + (bp->l_pn << (L2LOGPSIZE - log->l2bsize));
  1811. /*
  1812. * initiate pageout of the page
  1813. */
  1814. lbmStartIO(bp);
  1815. }
  1816. /*
  1817. * NAME: lbmStartIO()
  1818. *
  1819. * FUNCTION: Interface to DD strategy routine
  1820. *
  1821. * RETURN: none
  1822. *
  1823. * serialization: LCACHE_LOCK() is NOT held during log i/o;
  1824. */
  1825. static void lbmStartIO(struct lbuf * bp)
  1826. {
  1827. struct bio *bio;
  1828. struct jfs_log *log = bp->l_log;
  1829. jfs_info("lbmStartIO");
  1830. bio = bio_alloc(GFP_NOFS, 1);
  1831. bio->bi_iter.bi_sector = bp->l_blkno << (log->l2bsize - 9);
  1832. bio_set_dev(bio, log->bdev);
  1833. bio_add_page(bio, bp->l_page, LOGPSIZE, bp->l_offset);
  1834. BUG_ON(bio->bi_iter.bi_size != LOGPSIZE);
  1835. bio->bi_end_io = lbmIODone;
  1836. bio->bi_private = bp;
  1837. bio->bi_opf = REQ_OP_WRITE | REQ_SYNC;
  1838. /* check if journaling to disk has been disabled */
  1839. if (log->no_integrity) {
  1840. bio->bi_iter.bi_size = 0;
  1841. lbmIODone(bio);
  1842. } else {
  1843. submit_bio(bio);
  1844. INCREMENT(lmStat.submitted);
  1845. }
  1846. }
  1847. /*
  1848. * lbmIOWait()
  1849. */
  1850. static int lbmIOWait(struct lbuf * bp, int flag)
  1851. {
  1852. unsigned long flags;
  1853. int rc = 0;
  1854. jfs_info("lbmIOWait1: bp:0x%p flag:0x%x:0x%x", bp, bp->l_flag, flag);
  1855. LCACHE_LOCK(flags); /* disable+lock */
  1856. LCACHE_SLEEP_COND(bp->l_ioevent, (bp->l_flag & lbmDONE), flags);
  1857. rc = (bp->l_flag & lbmERROR) ? -EIO : 0;
  1858. if (flag & lbmFREE)
  1859. lbmfree(bp);
  1860. LCACHE_UNLOCK(flags); /* unlock+enable */
  1861. jfs_info("lbmIOWait2: bp:0x%p flag:0x%x:0x%x", bp, bp->l_flag, flag);
  1862. return rc;
  1863. }
  1864. /*
  1865. * lbmIODone()
  1866. *
  1867. * executed at INTIODONE level
  1868. */
  1869. static void lbmIODone(struct bio *bio)
  1870. {
  1871. struct lbuf *bp = bio->bi_private;
  1872. struct lbuf *nextbp, *tail;
  1873. struct jfs_log *log;
  1874. unsigned long flags;
  1875. /*
  1876. * get back jfs buffer bound to the i/o buffer
  1877. */
  1878. jfs_info("lbmIODone: bp:0x%p flag:0x%x", bp, bp->l_flag);
  1879. LCACHE_LOCK(flags); /* disable+lock */
  1880. bp->l_flag |= lbmDONE;
  1881. if (bio->bi_status) {
  1882. bp->l_flag |= lbmERROR;
  1883. jfs_err("lbmIODone: I/O error in JFS log");
  1884. }
  1885. bio_put(bio);
  1886. /*
  1887. * pagein completion
  1888. */
  1889. if (bp->l_flag & lbmREAD) {
  1890. bp->l_flag &= ~lbmREAD;
  1891. LCACHE_UNLOCK(flags); /* unlock+enable */
  1892. /* wakeup I/O initiator */
  1893. LCACHE_WAKEUP(&bp->l_ioevent);
  1894. return;
  1895. }
  1896. /*
  1897. * pageout completion
  1898. *
  1899. * the bp at the head of write queue has completed pageout.
  1900. *
  1901. * if single-commit/full-page pageout, remove the current buffer
  1902. * from head of pageout queue, and redrive pageout with
  1903. * the new buffer at head of pageout queue;
  1904. * otherwise, the partial-page pageout buffer stays at
  1905. * the head of pageout queue to be redriven for pageout
  1906. * by lmGroupCommit() until full-page pageout is completed.
  1907. */
  1908. bp->l_flag &= ~lbmWRITE;
  1909. INCREMENT(lmStat.pagedone);
  1910. /* update committed lsn */
  1911. log = bp->l_log;
  1912. log->clsn = (bp->l_pn << L2LOGPSIZE) + bp->l_ceor;
  1913. if (bp->l_flag & lbmDIRECT) {
  1914. LCACHE_WAKEUP(&bp->l_ioevent);
  1915. LCACHE_UNLOCK(flags);
  1916. return;
  1917. }
  1918. tail = log->wqueue;
  1919. /* single element queue */
  1920. if (bp == tail) {
  1921. /* remove head buffer of full-page pageout
  1922. * from log device write queue
  1923. */
  1924. if (bp->l_flag & lbmRELEASE) {
  1925. log->wqueue = NULL;
  1926. bp->l_wqnext = NULL;
  1927. }
  1928. }
  1929. /* multi element queue */
  1930. else {
  1931. /* remove head buffer of full-page pageout
  1932. * from log device write queue
  1933. */
  1934. if (bp->l_flag & lbmRELEASE) {
  1935. nextbp = tail->l_wqnext = bp->l_wqnext;
  1936. bp->l_wqnext = NULL;
  1937. /*
  1938. * redrive pageout of next page at head of write queue:
  1939. * redrive next page without any bound tblk
  1940. * (i.e., page w/o any COMMIT records), or
  1941. * first page of new group commit which has been
  1942. * queued after current page (subsequent pageout
  1943. * is performed synchronously, except page without
  1944. * any COMMITs) by lmGroupCommit() as indicated
  1945. * by lbmWRITE flag;
  1946. */
  1947. if (nextbp->l_flag & lbmWRITE) {
  1948. /*
  1949. * We can't do the I/O at interrupt time.
  1950. * The jfsIO thread can do it
  1951. */
  1952. lbmRedrive(nextbp);
  1953. }
  1954. }
  1955. }
  1956. /*
  1957. * synchronous pageout:
  1958. *
  1959. * buffer has not necessarily been removed from write queue
  1960. * (e.g., synchronous write of partial-page with COMMIT):
  1961. * leave buffer for i/o initiator to dispose
  1962. */
  1963. if (bp->l_flag & lbmSYNC) {
  1964. LCACHE_UNLOCK(flags); /* unlock+enable */
  1965. /* wakeup I/O initiator */
  1966. LCACHE_WAKEUP(&bp->l_ioevent);
  1967. }
  1968. /*
  1969. * Group Commit pageout:
  1970. */
  1971. else if (bp->l_flag & lbmGC) {
  1972. LCACHE_UNLOCK(flags);
  1973. lmPostGC(bp);
  1974. }
  1975. /*
  1976. * asynchronous pageout:
  1977. *
  1978. * buffer must have been removed from write queue:
  1979. * insert buffer at head of freelist where it can be recycled
  1980. */
  1981. else {
  1982. assert(bp->l_flag & lbmRELEASE);
  1983. assert(bp->l_flag & lbmFREE);
  1984. lbmfree(bp);
  1985. LCACHE_UNLOCK(flags); /* unlock+enable */
  1986. }
  1987. }
  1988. int jfsIOWait(void *arg)
  1989. {
  1990. struct lbuf *bp;
  1991. do {
  1992. spin_lock_irq(&log_redrive_lock);
  1993. while ((bp = log_redrive_list)) {
  1994. log_redrive_list = bp->l_redrive_next;
  1995. bp->l_redrive_next = NULL;
  1996. spin_unlock_irq(&log_redrive_lock);
  1997. lbmStartIO(bp);
  1998. spin_lock_irq(&log_redrive_lock);
  1999. }
  2000. if (freezing(current)) {
  2001. spin_unlock_irq(&log_redrive_lock);
  2002. try_to_freeze();
  2003. } else {
  2004. set_current_state(TASK_INTERRUPTIBLE);
  2005. spin_unlock_irq(&log_redrive_lock);
  2006. schedule();
  2007. }
  2008. } while (!kthread_should_stop());
  2009. jfs_info("jfsIOWait being killed!");
  2010. return 0;
  2011. }
  2012. /*
  2013. * NAME: lmLogFormat()/jfs_logform()
  2014. *
  2015. * FUNCTION: format file system log
  2016. *
  2017. * PARAMETERS:
  2018. * log - volume log
  2019. * logAddress - start address of log space in FS block
  2020. * logSize - length of log space in FS block;
  2021. *
  2022. * RETURN: 0 - success
  2023. * -EIO - i/o error
  2024. *
  2025. * XXX: We're synchronously writing one page at a time. This needs to
  2026. * be improved by writing multiple pages at once.
  2027. */
  2028. int lmLogFormat(struct jfs_log *log, s64 logAddress, int logSize)
  2029. {
  2030. int rc = -EIO;
  2031. struct jfs_sb_info *sbi;
  2032. struct logsuper *logsuper;
  2033. struct logpage *lp;
  2034. int lspn; /* log sequence page number */
  2035. struct lrd *lrd_ptr;
  2036. int npages = 0;
  2037. struct lbuf *bp;
  2038. jfs_info("lmLogFormat: logAddress:%Ld logSize:%d",
  2039. (long long)logAddress, logSize);
  2040. sbi = list_entry(log->sb_list.next, struct jfs_sb_info, log_list);
  2041. /* allocate a log buffer */
  2042. bp = lbmAllocate(log, 1);
  2043. npages = logSize >> sbi->l2nbperpage;
  2044. /*
  2045. * log space:
  2046. *
  2047. * page 0 - reserved;
  2048. * page 1 - log superblock;
  2049. * page 2 - log data page: A SYNC log record is written
  2050. * into this page at logform time;
  2051. * pages 3-N - log data page: set to empty log data pages;
  2052. */
  2053. /*
  2054. * init log superblock: log page 1
  2055. */
  2056. logsuper = (struct logsuper *) bp->l_ldata;
  2057. logsuper->magic = cpu_to_le32(LOGMAGIC);
  2058. logsuper->version = cpu_to_le32(LOGVERSION);
  2059. logsuper->state = cpu_to_le32(LOGREDONE);
  2060. logsuper->flag = cpu_to_le32(sbi->mntflag); /* ? */
  2061. logsuper->size = cpu_to_le32(npages);
  2062. logsuper->bsize = cpu_to_le32(sbi->bsize);
  2063. logsuper->l2bsize = cpu_to_le32(sbi->l2bsize);
  2064. logsuper->end = cpu_to_le32(2 * LOGPSIZE + LOGPHDRSIZE + LOGRDSIZE);
  2065. bp->l_flag = lbmWRITE | lbmSYNC | lbmDIRECT;
  2066. bp->l_blkno = logAddress + sbi->nbperpage;
  2067. lbmStartIO(bp);
  2068. if ((rc = lbmIOWait(bp, 0)))
  2069. goto exit;
  2070. /*
  2071. * init pages 2 to npages-1 as log data pages:
  2072. *
  2073. * log page sequence number (lpsn) initialization:
  2074. *
  2075. * pn: 0 1 2 3 n-1
  2076. * +-----+-----+=====+=====+===.....===+=====+
  2077. * lspn: N-1 0 1 N-2
  2078. * <--- N page circular file ---->
  2079. *
  2080. * the N (= npages-2) data pages of the log is maintained as
  2081. * a circular file for the log records;
  2082. * lpsn grows by 1 monotonically as each log page is written
  2083. * to the circular file of the log;
  2084. * and setLogpage() will not reset the page number even if
  2085. * the eor is equal to LOGPHDRSIZE. In order for binary search
  2086. * still work in find log end process, we have to simulate the
  2087. * log wrap situation at the log format time.
  2088. * The 1st log page written will have the highest lpsn. Then
  2089. * the succeeding log pages will have ascending order of
  2090. * the lspn starting from 0, ... (N-2)
  2091. */
  2092. lp = (struct logpage *) bp->l_ldata;
  2093. /*
  2094. * initialize 1st log page to be written: lpsn = N - 1,
  2095. * write a SYNCPT log record is written to this page
  2096. */
  2097. lp->h.page = lp->t.page = cpu_to_le32(npages - 3);
  2098. lp->h.eor = lp->t.eor = cpu_to_le16(LOGPHDRSIZE + LOGRDSIZE);
  2099. lrd_ptr = (struct lrd *) &lp->data;
  2100. lrd_ptr->logtid = 0;
  2101. lrd_ptr->backchain = 0;
  2102. lrd_ptr->type = cpu_to_le16(LOG_SYNCPT);
  2103. lrd_ptr->length = 0;
  2104. lrd_ptr->log.syncpt.sync = 0;
  2105. bp->l_blkno += sbi->nbperpage;
  2106. bp->l_flag = lbmWRITE | lbmSYNC | lbmDIRECT;
  2107. lbmStartIO(bp);
  2108. if ((rc = lbmIOWait(bp, 0)))
  2109. goto exit;
  2110. /*
  2111. * initialize succeeding log pages: lpsn = 0, 1, ..., (N-2)
  2112. */
  2113. for (lspn = 0; lspn < npages - 3; lspn++) {
  2114. lp->h.page = lp->t.page = cpu_to_le32(lspn);
  2115. lp->h.eor = lp->t.eor = cpu_to_le16(LOGPHDRSIZE);
  2116. bp->l_blkno += sbi->nbperpage;
  2117. bp->l_flag = lbmWRITE | lbmSYNC | lbmDIRECT;
  2118. lbmStartIO(bp);
  2119. if ((rc = lbmIOWait(bp, 0)))
  2120. goto exit;
  2121. }
  2122. rc = 0;
  2123. exit:
  2124. /*
  2125. * finalize log
  2126. */
  2127. /* release the buffer */
  2128. lbmFree(bp);
  2129. return rc;
  2130. }
  2131. #ifdef CONFIG_JFS_STATISTICS
  2132. int jfs_lmstats_proc_show(struct seq_file *m, void *v)
  2133. {
  2134. seq_printf(m,
  2135. "JFS Logmgr stats\n"
  2136. "================\n"
  2137. "commits = %d\n"
  2138. "writes submitted = %d\n"
  2139. "writes completed = %d\n"
  2140. "full pages submitted = %d\n"
  2141. "partial pages submitted = %d\n",
  2142. lmStat.commit,
  2143. lmStat.submitted,
  2144. lmStat.pagedone,
  2145. lmStat.full_page,
  2146. lmStat.partial_page);
  2147. return 0;
  2148. }
  2149. #endif /* CONFIG_JFS_STATISTICS */