jfs_logmgr.c 60 KB

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