jfs_logmgr.c 59 KB

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