jfs_logmgr.c 59 KB

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