jfs_logmgr.c 59 KB

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