jfs_txnmgr.c 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) International Business Machines Corp., 2000-2005
  4. * Portions Copyright (C) Christoph Hellwig, 2001-2002
  5. */
  6. /*
  7. * jfs_txnmgr.c: transaction manager
  8. *
  9. * notes:
  10. * transaction starts with txBegin() and ends with txCommit()
  11. * or txAbort().
  12. *
  13. * tlock is acquired at the time of update;
  14. * (obviate scan at commit time for xtree and dtree)
  15. * tlock and mp points to each other;
  16. * (no hashlist for mp -> tlock).
  17. *
  18. * special cases:
  19. * tlock on in-memory inode:
  20. * in-place tlock in the in-memory inode itself;
  21. * converted to page lock by iWrite() at commit time.
  22. *
  23. * tlock during write()/mmap() under anonymous transaction (tid = 0):
  24. * transferred (?) to transaction at commit time.
  25. *
  26. * use the page itself to update allocation maps
  27. * (obviate intermediate replication of allocation/deallocation data)
  28. * hold on to mp+lock thru update of maps
  29. */
  30. #include <linux/fs.h>
  31. #include <linux/vmalloc.h>
  32. #include <linux/completion.h>
  33. #include <linux/freezer.h>
  34. #include <linux/module.h>
  35. #include <linux/moduleparam.h>
  36. #include <linux/kthread.h>
  37. #include <linux/seq_file.h>
  38. #include "jfs_incore.h"
  39. #include "jfs_inode.h"
  40. #include "jfs_filsys.h"
  41. #include "jfs_metapage.h"
  42. #include "jfs_dinode.h"
  43. #include "jfs_imap.h"
  44. #include "jfs_dmap.h"
  45. #include "jfs_superblock.h"
  46. #include "jfs_debug.h"
  47. /*
  48. * transaction management structures
  49. */
  50. static struct {
  51. int freetid; /* index of a free tid structure */
  52. int freelock; /* index first free lock word */
  53. wait_queue_head_t freewait; /* eventlist of free tblock */
  54. wait_queue_head_t freelockwait; /* eventlist of free tlock */
  55. wait_queue_head_t lowlockwait; /* eventlist of ample tlocks */
  56. int tlocksInUse; /* Number of tlocks in use */
  57. spinlock_t LazyLock; /* synchronize sync_queue & unlock_queue */
  58. /* struct tblock *sync_queue; * Transactions waiting for data sync */
  59. struct list_head unlock_queue; /* Txns waiting to be released */
  60. struct list_head anon_list; /* inodes having anonymous txns */
  61. struct list_head anon_list2; /* inodes having anonymous txns
  62. that couldn't be sync'ed */
  63. } TxAnchor;
  64. int jfs_tlocks_low; /* Indicates low number of available tlocks */
  65. #ifdef CONFIG_JFS_STATISTICS
  66. static struct {
  67. uint txBegin;
  68. uint txBegin_barrier;
  69. uint txBegin_lockslow;
  70. uint txBegin_freetid;
  71. uint txBeginAnon;
  72. uint txBeginAnon_barrier;
  73. uint txBeginAnon_lockslow;
  74. uint txLockAlloc;
  75. uint txLockAlloc_freelock;
  76. } TxStat;
  77. #endif
  78. static int nTxBlock = -1; /* number of transaction blocks */
  79. module_param(nTxBlock, int, 0);
  80. MODULE_PARM_DESC(nTxBlock,
  81. "Number of transaction blocks (max:65536)");
  82. static int nTxLock = -1; /* number of transaction locks */
  83. module_param(nTxLock, int, 0);
  84. MODULE_PARM_DESC(nTxLock,
  85. "Number of transaction locks (max:65536)");
  86. struct tblock *TxBlock; /* transaction block table */
  87. static int TxLockLWM; /* Low water mark for number of txLocks used */
  88. static int TxLockHWM; /* High water mark for number of txLocks used */
  89. static int TxLockVHWM; /* Very High water mark */
  90. struct tlock *TxLock; /* transaction lock table */
  91. /*
  92. * transaction management lock
  93. */
  94. static DEFINE_SPINLOCK(jfsTxnLock);
  95. #define TXN_LOCK() spin_lock(&jfsTxnLock)
  96. #define TXN_UNLOCK() spin_unlock(&jfsTxnLock)
  97. #define LAZY_LOCK_INIT() spin_lock_init(&TxAnchor.LazyLock)
  98. #define LAZY_LOCK(flags) spin_lock_irqsave(&TxAnchor.LazyLock, flags)
  99. #define LAZY_UNLOCK(flags) spin_unlock_irqrestore(&TxAnchor.LazyLock, flags)
  100. static DECLARE_WAIT_QUEUE_HEAD(jfs_commit_thread_wait);
  101. static int jfs_commit_thread_waking;
  102. /*
  103. * Retry logic exist outside these macros to protect from spurrious wakeups.
  104. */
  105. static inline void TXN_SLEEP_DROP_LOCK(wait_queue_head_t * event)
  106. {
  107. DECLARE_WAITQUEUE(wait, current);
  108. add_wait_queue(event, &wait);
  109. set_current_state(TASK_UNINTERRUPTIBLE);
  110. TXN_UNLOCK();
  111. io_schedule();
  112. remove_wait_queue(event, &wait);
  113. }
  114. #define TXN_SLEEP(event)\
  115. {\
  116. TXN_SLEEP_DROP_LOCK(event);\
  117. TXN_LOCK();\
  118. }
  119. #define TXN_WAKEUP(event) wake_up_all(event)
  120. /*
  121. * statistics
  122. */
  123. static struct {
  124. tid_t maxtid; /* 4: biggest tid ever used */
  125. lid_t maxlid; /* 4: biggest lid ever used */
  126. int ntid; /* 4: # of transactions performed */
  127. int nlid; /* 4: # of tlocks acquired */
  128. int waitlock; /* 4: # of tlock wait */
  129. } stattx;
  130. /*
  131. * forward references
  132. */
  133. static void diLog(struct jfs_log *log, struct tblock *tblk, struct lrd *lrd,
  134. struct tlock *tlck, struct commit *cd);
  135. static void dataLog(struct jfs_log *log, struct tblock *tblk, struct lrd *lrd,
  136. struct tlock *tlck);
  137. static void dtLog(struct jfs_log * log, struct tblock * tblk, struct lrd * lrd,
  138. struct tlock * tlck);
  139. static void mapLog(struct jfs_log * log, struct tblock * tblk, struct lrd * lrd,
  140. struct tlock * tlck);
  141. static void txAllocPMap(struct inode *ip, struct maplock * maplock,
  142. struct tblock * tblk);
  143. static void txForce(struct tblock * tblk);
  144. static void txLog(struct jfs_log *log, struct tblock *tblk,
  145. struct commit *cd);
  146. static void txUpdateMap(struct tblock * tblk);
  147. static void txRelease(struct tblock * tblk);
  148. static void xtLog(struct jfs_log * log, struct tblock * tblk, struct lrd * lrd,
  149. struct tlock * tlck);
  150. static void LogSyncRelease(struct metapage * mp);
  151. /*
  152. * transaction block/lock management
  153. * ---------------------------------
  154. */
  155. /*
  156. * Get a transaction lock from the free list. If the number in use is
  157. * greater than the high water mark, wake up the sync daemon. This should
  158. * free some anonymous transaction locks. (TXN_LOCK must be held.)
  159. */
  160. static lid_t txLockAlloc(void)
  161. {
  162. lid_t lid;
  163. INCREMENT(TxStat.txLockAlloc);
  164. if (!TxAnchor.freelock) {
  165. INCREMENT(TxStat.txLockAlloc_freelock);
  166. }
  167. while (!(lid = TxAnchor.freelock))
  168. TXN_SLEEP(&TxAnchor.freelockwait);
  169. TxAnchor.freelock = TxLock[lid].next;
  170. HIGHWATERMARK(stattx.maxlid, lid);
  171. if ((++TxAnchor.tlocksInUse > TxLockHWM) && (jfs_tlocks_low == 0)) {
  172. jfs_info("txLockAlloc tlocks low");
  173. jfs_tlocks_low = 1;
  174. wake_up_process(jfsSyncThread);
  175. }
  176. return lid;
  177. }
  178. static void txLockFree(lid_t lid)
  179. {
  180. TxLock[lid].tid = 0;
  181. TxLock[lid].next = TxAnchor.freelock;
  182. TxAnchor.freelock = lid;
  183. TxAnchor.tlocksInUse--;
  184. if (jfs_tlocks_low && (TxAnchor.tlocksInUse < TxLockLWM)) {
  185. jfs_info("txLockFree jfs_tlocks_low no more");
  186. jfs_tlocks_low = 0;
  187. TXN_WAKEUP(&TxAnchor.lowlockwait);
  188. }
  189. TXN_WAKEUP(&TxAnchor.freelockwait);
  190. }
  191. /*
  192. * NAME: txInit()
  193. *
  194. * FUNCTION: initialize transaction management structures
  195. *
  196. * RETURN:
  197. *
  198. * serialization: single thread at jfs_init()
  199. */
  200. int txInit(void)
  201. {
  202. int k, size;
  203. struct sysinfo si;
  204. /* Set defaults for nTxLock and nTxBlock if unset */
  205. if (nTxLock == -1) {
  206. if (nTxBlock == -1) {
  207. /* Base default on memory size */
  208. si_meminfo(&si);
  209. if (si.totalram > (256 * 1024)) /* 1 GB */
  210. nTxLock = 64 * 1024;
  211. else
  212. nTxLock = si.totalram >> 2;
  213. } else if (nTxBlock > (8 * 1024))
  214. nTxLock = 64 * 1024;
  215. else
  216. nTxLock = nTxBlock << 3;
  217. }
  218. if (nTxBlock == -1)
  219. nTxBlock = nTxLock >> 3;
  220. /* Verify tunable parameters */
  221. if (nTxBlock < 16)
  222. nTxBlock = 16; /* No one should set it this low */
  223. if (nTxBlock > 65536)
  224. nTxBlock = 65536;
  225. if (nTxLock < 256)
  226. nTxLock = 256; /* No one should set it this low */
  227. if (nTxLock > 65536)
  228. nTxLock = 65536;
  229. printk(KERN_INFO "JFS: nTxBlock = %d, nTxLock = %d\n",
  230. nTxBlock, nTxLock);
  231. /*
  232. * initialize transaction block (tblock) table
  233. *
  234. * transaction id (tid) = tblock index
  235. * tid = 0 is reserved.
  236. */
  237. TxLockLWM = (nTxLock * 4) / 10;
  238. TxLockHWM = (nTxLock * 7) / 10;
  239. TxLockVHWM = (nTxLock * 8) / 10;
  240. size = sizeof(struct tblock) * nTxBlock;
  241. TxBlock = vmalloc(size);
  242. if (TxBlock == NULL)
  243. return -ENOMEM;
  244. for (k = 1; k < nTxBlock - 1; k++) {
  245. TxBlock[k].next = k + 1;
  246. init_waitqueue_head(&TxBlock[k].gcwait);
  247. init_waitqueue_head(&TxBlock[k].waitor);
  248. }
  249. TxBlock[k].next = 0;
  250. init_waitqueue_head(&TxBlock[k].gcwait);
  251. init_waitqueue_head(&TxBlock[k].waitor);
  252. TxAnchor.freetid = 1;
  253. init_waitqueue_head(&TxAnchor.freewait);
  254. stattx.maxtid = 1; /* statistics */
  255. /*
  256. * initialize transaction lock (tlock) table
  257. *
  258. * transaction lock id = tlock index
  259. * tlock id = 0 is reserved.
  260. */
  261. size = sizeof(struct tlock) * nTxLock;
  262. TxLock = vmalloc(size);
  263. if (TxLock == NULL) {
  264. vfree(TxBlock);
  265. return -ENOMEM;
  266. }
  267. /* initialize tlock table */
  268. for (k = 1; k < nTxLock - 1; k++)
  269. TxLock[k].next = k + 1;
  270. TxLock[k].next = 0;
  271. init_waitqueue_head(&TxAnchor.freelockwait);
  272. init_waitqueue_head(&TxAnchor.lowlockwait);
  273. TxAnchor.freelock = 1;
  274. TxAnchor.tlocksInUse = 0;
  275. INIT_LIST_HEAD(&TxAnchor.anon_list);
  276. INIT_LIST_HEAD(&TxAnchor.anon_list2);
  277. LAZY_LOCK_INIT();
  278. INIT_LIST_HEAD(&TxAnchor.unlock_queue);
  279. stattx.maxlid = 1; /* statistics */
  280. return 0;
  281. }
  282. /*
  283. * NAME: txExit()
  284. *
  285. * FUNCTION: clean up when module is unloaded
  286. */
  287. void txExit(void)
  288. {
  289. vfree(TxLock);
  290. TxLock = NULL;
  291. vfree(TxBlock);
  292. TxBlock = NULL;
  293. }
  294. /*
  295. * NAME: txBegin()
  296. *
  297. * FUNCTION: start a transaction.
  298. *
  299. * PARAMETER: sb - superblock
  300. * flag - force for nested tx;
  301. *
  302. * RETURN: tid - transaction id
  303. *
  304. * note: flag force allows to start tx for nested tx
  305. * to prevent deadlock on logsync barrier;
  306. */
  307. tid_t txBegin(struct super_block *sb, int flag)
  308. {
  309. tid_t t;
  310. struct tblock *tblk;
  311. struct jfs_log *log;
  312. jfs_info("txBegin: flag = 0x%x", flag);
  313. log = JFS_SBI(sb)->log;
  314. if (!log) {
  315. jfs_error(sb, "read-only filesystem\n");
  316. return 0;
  317. }
  318. TXN_LOCK();
  319. INCREMENT(TxStat.txBegin);
  320. retry:
  321. if (!(flag & COMMIT_FORCE)) {
  322. /*
  323. * synchronize with logsync barrier
  324. */
  325. if (test_bit(log_SYNCBARRIER, &log->flag) ||
  326. test_bit(log_QUIESCE, &log->flag)) {
  327. INCREMENT(TxStat.txBegin_barrier);
  328. TXN_SLEEP(&log->syncwait);
  329. goto retry;
  330. }
  331. }
  332. if (flag == 0) {
  333. /*
  334. * Don't begin transaction if we're getting starved for tlocks
  335. * unless COMMIT_FORCE or COMMIT_INODE (which may ultimately
  336. * free tlocks)
  337. */
  338. if (TxAnchor.tlocksInUse > TxLockVHWM) {
  339. INCREMENT(TxStat.txBegin_lockslow);
  340. TXN_SLEEP(&TxAnchor.lowlockwait);
  341. goto retry;
  342. }
  343. }
  344. /*
  345. * allocate transaction id/block
  346. */
  347. if ((t = TxAnchor.freetid) == 0) {
  348. jfs_info("txBegin: waiting for free tid");
  349. INCREMENT(TxStat.txBegin_freetid);
  350. TXN_SLEEP(&TxAnchor.freewait);
  351. goto retry;
  352. }
  353. tblk = tid_to_tblock(t);
  354. if ((tblk->next == 0) && !(flag & COMMIT_FORCE)) {
  355. /* Don't let a non-forced transaction take the last tblk */
  356. jfs_info("txBegin: waiting for free tid");
  357. INCREMENT(TxStat.txBegin_freetid);
  358. TXN_SLEEP(&TxAnchor.freewait);
  359. goto retry;
  360. }
  361. TxAnchor.freetid = tblk->next;
  362. /*
  363. * initialize transaction
  364. */
  365. /*
  366. * We can't zero the whole thing or we screw up another thread being
  367. * awakened after sleeping on tblk->waitor
  368. *
  369. * memset(tblk, 0, sizeof(struct tblock));
  370. */
  371. tblk->next = tblk->last = tblk->xflag = tblk->flag = tblk->lsn = 0;
  372. tblk->sb = sb;
  373. ++log->logtid;
  374. tblk->logtid = log->logtid;
  375. ++log->active;
  376. HIGHWATERMARK(stattx.maxtid, t); /* statistics */
  377. INCREMENT(stattx.ntid); /* statistics */
  378. TXN_UNLOCK();
  379. jfs_info("txBegin: returning tid = %d", t);
  380. return t;
  381. }
  382. /*
  383. * NAME: txBeginAnon()
  384. *
  385. * FUNCTION: start an anonymous transaction.
  386. * Blocks if logsync or available tlocks are low to prevent
  387. * anonymous tlocks from depleting supply.
  388. *
  389. * PARAMETER: sb - superblock
  390. *
  391. * RETURN: none
  392. */
  393. void txBeginAnon(struct super_block *sb)
  394. {
  395. struct jfs_log *log;
  396. log = JFS_SBI(sb)->log;
  397. TXN_LOCK();
  398. INCREMENT(TxStat.txBeginAnon);
  399. retry:
  400. /*
  401. * synchronize with logsync barrier
  402. */
  403. if (test_bit(log_SYNCBARRIER, &log->flag) ||
  404. test_bit(log_QUIESCE, &log->flag)) {
  405. INCREMENT(TxStat.txBeginAnon_barrier);
  406. TXN_SLEEP(&log->syncwait);
  407. goto retry;
  408. }
  409. /*
  410. * Don't begin transaction if we're getting starved for tlocks
  411. */
  412. if (TxAnchor.tlocksInUse > TxLockVHWM) {
  413. INCREMENT(TxStat.txBeginAnon_lockslow);
  414. TXN_SLEEP(&TxAnchor.lowlockwait);
  415. goto retry;
  416. }
  417. TXN_UNLOCK();
  418. }
  419. /*
  420. * txEnd()
  421. *
  422. * function: free specified transaction block.
  423. *
  424. * logsync barrier processing:
  425. *
  426. * serialization:
  427. */
  428. void txEnd(tid_t tid)
  429. {
  430. struct tblock *tblk = tid_to_tblock(tid);
  431. struct jfs_log *log;
  432. jfs_info("txEnd: tid = %d", tid);
  433. TXN_LOCK();
  434. /*
  435. * wakeup transactions waiting on the page locked
  436. * by the current transaction
  437. */
  438. TXN_WAKEUP(&tblk->waitor);
  439. log = JFS_SBI(tblk->sb)->log;
  440. /*
  441. * Lazy commit thread can't free this guy until we mark it UNLOCKED,
  442. * otherwise, we would be left with a transaction that may have been
  443. * reused.
  444. *
  445. * Lazy commit thread will turn off tblkGC_LAZY before calling this
  446. * routine.
  447. */
  448. if (tblk->flag & tblkGC_LAZY) {
  449. jfs_info("txEnd called w/lazy tid: %d, tblk = 0x%p", tid, tblk);
  450. TXN_UNLOCK();
  451. spin_lock_irq(&log->gclock); // LOGGC_LOCK
  452. tblk->flag |= tblkGC_UNLOCKED;
  453. spin_unlock_irq(&log->gclock); // LOGGC_UNLOCK
  454. return;
  455. }
  456. jfs_info("txEnd: tid: %d, tblk = 0x%p", tid, tblk);
  457. assert(tblk->next == 0);
  458. /*
  459. * insert tblock back on freelist
  460. */
  461. tblk->next = TxAnchor.freetid;
  462. TxAnchor.freetid = tid;
  463. /*
  464. * mark the tblock not active
  465. */
  466. if (--log->active == 0) {
  467. clear_bit(log_FLUSH, &log->flag);
  468. /*
  469. * synchronize with logsync barrier
  470. */
  471. if (test_bit(log_SYNCBARRIER, &log->flag)) {
  472. TXN_UNLOCK();
  473. /* write dirty metadata & forward log syncpt */
  474. jfs_syncpt(log, 1);
  475. jfs_info("log barrier off: 0x%x", log->lsn);
  476. /* enable new transactions start */
  477. clear_bit(log_SYNCBARRIER, &log->flag);
  478. /* wakeup all waitors for logsync barrier */
  479. TXN_WAKEUP(&log->syncwait);
  480. goto wakeup;
  481. }
  482. }
  483. TXN_UNLOCK();
  484. wakeup:
  485. /*
  486. * wakeup all waitors for a free tblock
  487. */
  488. TXN_WAKEUP(&TxAnchor.freewait);
  489. }
  490. /*
  491. * txLock()
  492. *
  493. * function: acquire a transaction lock on the specified <mp>
  494. *
  495. * parameter:
  496. *
  497. * return: transaction lock id
  498. *
  499. * serialization:
  500. */
  501. struct tlock *txLock(tid_t tid, struct inode *ip, struct metapage * mp,
  502. int type)
  503. {
  504. struct jfs_inode_info *jfs_ip = JFS_IP(ip);
  505. int dir_xtree = 0;
  506. lid_t lid;
  507. tid_t xtid;
  508. struct tlock *tlck;
  509. struct xtlock *xtlck;
  510. struct linelock *linelock;
  511. xtpage_t *p;
  512. struct tblock *tblk;
  513. TXN_LOCK();
  514. if (S_ISDIR(ip->i_mode) && (type & tlckXTREE) &&
  515. !(mp->xflag & COMMIT_PAGE)) {
  516. /*
  517. * Directory inode is special. It can have both an xtree tlock
  518. * and a dtree tlock associated with it.
  519. */
  520. dir_xtree = 1;
  521. lid = jfs_ip->xtlid;
  522. } else
  523. lid = mp->lid;
  524. /* is page not locked by a transaction ? */
  525. if (lid == 0)
  526. goto allocateLock;
  527. jfs_info("txLock: tid:%d ip:0x%p mp:0x%p lid:%d", tid, ip, mp, lid);
  528. /* is page locked by the requester transaction ? */
  529. tlck = lid_to_tlock(lid);
  530. if ((xtid = tlck->tid) == tid) {
  531. TXN_UNLOCK();
  532. goto grantLock;
  533. }
  534. /*
  535. * is page locked by anonymous transaction/lock ?
  536. *
  537. * (page update without transaction (i.e., file write) is
  538. * locked under anonymous transaction tid = 0:
  539. * anonymous tlocks maintained on anonymous tlock list of
  540. * the inode of the page and available to all anonymous
  541. * transactions until txCommit() time at which point
  542. * they are transferred to the transaction tlock list of
  543. * the committing transaction of the inode)
  544. */
  545. if (xtid == 0) {
  546. tlck->tid = tid;
  547. TXN_UNLOCK();
  548. tblk = tid_to_tblock(tid);
  549. /*
  550. * The order of the tlocks in the transaction is important
  551. * (during truncate, child xtree pages must be freed before
  552. * parent's tlocks change the working map).
  553. * Take tlock off anonymous list and add to tail of
  554. * transaction list
  555. *
  556. * Note: We really need to get rid of the tid & lid and
  557. * use list_head's. This code is getting UGLY!
  558. */
  559. if (jfs_ip->atlhead == lid) {
  560. if (jfs_ip->atltail == lid) {
  561. /* only anonymous txn.
  562. * Remove from anon_list
  563. */
  564. TXN_LOCK();
  565. list_del_init(&jfs_ip->anon_inode_list);
  566. TXN_UNLOCK();
  567. }
  568. jfs_ip->atlhead = tlck->next;
  569. } else {
  570. lid_t last;
  571. for (last = jfs_ip->atlhead;
  572. lid_to_tlock(last)->next != lid;
  573. last = lid_to_tlock(last)->next) {
  574. assert(last);
  575. }
  576. lid_to_tlock(last)->next = tlck->next;
  577. if (jfs_ip->atltail == lid)
  578. jfs_ip->atltail = last;
  579. }
  580. /* insert the tlock at tail of transaction tlock list */
  581. if (tblk->next)
  582. lid_to_tlock(tblk->last)->next = lid;
  583. else
  584. tblk->next = lid;
  585. tlck->next = 0;
  586. tblk->last = lid;
  587. goto grantLock;
  588. }
  589. goto waitLock;
  590. /*
  591. * allocate a tlock
  592. */
  593. allocateLock:
  594. lid = txLockAlloc();
  595. tlck = lid_to_tlock(lid);
  596. /*
  597. * initialize tlock
  598. */
  599. tlck->tid = tid;
  600. TXN_UNLOCK();
  601. /* mark tlock for meta-data page */
  602. if (mp->xflag & COMMIT_PAGE) {
  603. tlck->flag = tlckPAGELOCK;
  604. /* mark the page dirty and nohomeok */
  605. metapage_nohomeok(mp);
  606. jfs_info("locking mp = 0x%p, nohomeok = %d tid = %d tlck = 0x%p",
  607. mp, mp->nohomeok, tid, tlck);
  608. /* if anonymous transaction, and buffer is on the group
  609. * commit synclist, mark inode to show this. This will
  610. * prevent the buffer from being marked nohomeok for too
  611. * long a time.
  612. */
  613. if ((tid == 0) && mp->lsn)
  614. set_cflag(COMMIT_Synclist, ip);
  615. }
  616. /* mark tlock for in-memory inode */
  617. else
  618. tlck->flag = tlckINODELOCK;
  619. if (S_ISDIR(ip->i_mode))
  620. tlck->flag |= tlckDIRECTORY;
  621. tlck->type = 0;
  622. /* bind the tlock and the page */
  623. tlck->ip = ip;
  624. tlck->mp = mp;
  625. if (dir_xtree)
  626. jfs_ip->xtlid = lid;
  627. else
  628. mp->lid = lid;
  629. /*
  630. * enqueue transaction lock to transaction/inode
  631. */
  632. /* insert the tlock at tail of transaction tlock list */
  633. if (tid) {
  634. tblk = tid_to_tblock(tid);
  635. if (tblk->next)
  636. lid_to_tlock(tblk->last)->next = lid;
  637. else
  638. tblk->next = lid;
  639. tlck->next = 0;
  640. tblk->last = lid;
  641. }
  642. /* anonymous transaction:
  643. * insert the tlock at head of inode anonymous tlock list
  644. */
  645. else {
  646. tlck->next = jfs_ip->atlhead;
  647. jfs_ip->atlhead = lid;
  648. if (tlck->next == 0) {
  649. /* This inode's first anonymous transaction */
  650. jfs_ip->atltail = lid;
  651. TXN_LOCK();
  652. list_add_tail(&jfs_ip->anon_inode_list,
  653. &TxAnchor.anon_list);
  654. TXN_UNLOCK();
  655. }
  656. }
  657. /* initialize type dependent area for linelock */
  658. linelock = (struct linelock *) & tlck->lock;
  659. linelock->next = 0;
  660. linelock->flag = tlckLINELOCK;
  661. linelock->maxcnt = TLOCKSHORT;
  662. linelock->index = 0;
  663. switch (type & tlckTYPE) {
  664. case tlckDTREE:
  665. linelock->l2linesize = L2DTSLOTSIZE;
  666. break;
  667. case tlckXTREE:
  668. linelock->l2linesize = L2XTSLOTSIZE;
  669. xtlck = (struct xtlock *) linelock;
  670. xtlck->header.offset = 0;
  671. xtlck->header.length = 2;
  672. if (type & tlckNEW) {
  673. xtlck->lwm.offset = XTENTRYSTART;
  674. } else {
  675. if (mp->xflag & COMMIT_PAGE)
  676. p = (xtpage_t *) mp->data;
  677. else
  678. p = &jfs_ip->i_xtroot;
  679. xtlck->lwm.offset =
  680. le16_to_cpu(p->header.nextindex);
  681. }
  682. xtlck->lwm.length = 0; /* ! */
  683. xtlck->twm.offset = 0;
  684. xtlck->hwm.offset = 0;
  685. xtlck->index = 2;
  686. break;
  687. case tlckINODE:
  688. linelock->l2linesize = L2INODESLOTSIZE;
  689. break;
  690. case tlckDATA:
  691. linelock->l2linesize = L2DATASLOTSIZE;
  692. break;
  693. default:
  694. jfs_err("UFO tlock:0x%p", tlck);
  695. }
  696. /*
  697. * update tlock vector
  698. */
  699. grantLock:
  700. tlck->type |= type;
  701. return tlck;
  702. /*
  703. * page is being locked by another transaction:
  704. */
  705. waitLock:
  706. /* Only locks on ipimap or ipaimap should reach here */
  707. /* assert(jfs_ip->fileset == AGGREGATE_I); */
  708. if (jfs_ip->fileset != AGGREGATE_I) {
  709. printk(KERN_ERR "txLock: trying to lock locked page!");
  710. print_hex_dump(KERN_ERR, "ip: ", DUMP_PREFIX_ADDRESS, 16, 4,
  711. ip, sizeof(*ip), 0);
  712. print_hex_dump(KERN_ERR, "mp: ", DUMP_PREFIX_ADDRESS, 16, 4,
  713. mp, sizeof(*mp), 0);
  714. print_hex_dump(KERN_ERR, "Locker's tblock: ",
  715. DUMP_PREFIX_ADDRESS, 16, 4, tid_to_tblock(tid),
  716. sizeof(struct tblock), 0);
  717. print_hex_dump(KERN_ERR, "Tlock: ", DUMP_PREFIX_ADDRESS, 16, 4,
  718. tlck, sizeof(*tlck), 0);
  719. BUG();
  720. }
  721. INCREMENT(stattx.waitlock); /* statistics */
  722. TXN_UNLOCK();
  723. release_metapage(mp);
  724. TXN_LOCK();
  725. xtid = tlck->tid; /* reacquire after dropping TXN_LOCK */
  726. jfs_info("txLock: in waitLock, tid = %d, xtid = %d, lid = %d",
  727. tid, xtid, lid);
  728. /* Recheck everything since dropping TXN_LOCK */
  729. if (xtid && (tlck->mp == mp) && (mp->lid == lid))
  730. TXN_SLEEP_DROP_LOCK(&tid_to_tblock(xtid)->waitor);
  731. else
  732. TXN_UNLOCK();
  733. jfs_info("txLock: awakened tid = %d, lid = %d", tid, lid);
  734. return NULL;
  735. }
  736. /*
  737. * NAME: txRelease()
  738. *
  739. * FUNCTION: Release buffers associated with transaction locks, but don't
  740. * mark homeok yet. The allows other transactions to modify
  741. * buffers, but won't let them go to disk until commit record
  742. * actually gets written.
  743. *
  744. * PARAMETER:
  745. * tblk -
  746. *
  747. * RETURN: Errors from subroutines.
  748. */
  749. static void txRelease(struct tblock * tblk)
  750. {
  751. struct metapage *mp;
  752. lid_t lid;
  753. struct tlock *tlck;
  754. TXN_LOCK();
  755. for (lid = tblk->next; lid; lid = tlck->next) {
  756. tlck = lid_to_tlock(lid);
  757. if ((mp = tlck->mp) != NULL &&
  758. (tlck->type & tlckBTROOT) == 0) {
  759. assert(mp->xflag & COMMIT_PAGE);
  760. mp->lid = 0;
  761. }
  762. }
  763. /*
  764. * wakeup transactions waiting on a page locked
  765. * by the current transaction
  766. */
  767. TXN_WAKEUP(&tblk->waitor);
  768. TXN_UNLOCK();
  769. }
  770. /*
  771. * NAME: txUnlock()
  772. *
  773. * FUNCTION: Initiates pageout of pages modified by tid in journalled
  774. * objects and frees their lockwords.
  775. */
  776. static void txUnlock(struct tblock * tblk)
  777. {
  778. struct tlock *tlck;
  779. struct linelock *linelock;
  780. lid_t lid, next, llid, k;
  781. struct metapage *mp;
  782. struct jfs_log *log;
  783. int difft, diffp;
  784. unsigned long flags;
  785. jfs_info("txUnlock: tblk = 0x%p", tblk);
  786. log = JFS_SBI(tblk->sb)->log;
  787. /*
  788. * mark page under tlock homeok (its log has been written):
  789. */
  790. for (lid = tblk->next; lid; lid = next) {
  791. tlck = lid_to_tlock(lid);
  792. next = tlck->next;
  793. jfs_info("unlocking lid = %d, tlck = 0x%p", lid, tlck);
  794. /* unbind page from tlock */
  795. if ((mp = tlck->mp) != NULL &&
  796. (tlck->type & tlckBTROOT) == 0) {
  797. assert(mp->xflag & COMMIT_PAGE);
  798. /* hold buffer
  799. */
  800. hold_metapage(mp);
  801. assert(mp->nohomeok > 0);
  802. _metapage_homeok(mp);
  803. /* inherit younger/larger clsn */
  804. LOGSYNC_LOCK(log, flags);
  805. if (mp->clsn) {
  806. logdiff(difft, tblk->clsn, log);
  807. logdiff(diffp, mp->clsn, log);
  808. if (difft > diffp)
  809. mp->clsn = tblk->clsn;
  810. } else
  811. mp->clsn = tblk->clsn;
  812. LOGSYNC_UNLOCK(log, flags);
  813. assert(!(tlck->flag & tlckFREEPAGE));
  814. put_metapage(mp);
  815. }
  816. /* insert tlock, and linelock(s) of the tlock if any,
  817. * at head of freelist
  818. */
  819. TXN_LOCK();
  820. llid = ((struct linelock *) & tlck->lock)->next;
  821. while (llid) {
  822. linelock = (struct linelock *) lid_to_tlock(llid);
  823. k = linelock->next;
  824. txLockFree(llid);
  825. llid = k;
  826. }
  827. txLockFree(lid);
  828. TXN_UNLOCK();
  829. }
  830. tblk->next = tblk->last = 0;
  831. /*
  832. * remove tblock from logsynclist
  833. * (allocation map pages inherited lsn of tblk and
  834. * has been inserted in logsync list at txUpdateMap())
  835. */
  836. if (tblk->lsn) {
  837. LOGSYNC_LOCK(log, flags);
  838. log->count--;
  839. list_del(&tblk->synclist);
  840. LOGSYNC_UNLOCK(log, flags);
  841. }
  842. }
  843. /*
  844. * txMaplock()
  845. *
  846. * function: allocate a transaction lock for freed page/entry;
  847. * for freed page, maplock is used as xtlock/dtlock type;
  848. */
  849. struct tlock *txMaplock(tid_t tid, struct inode *ip, int type)
  850. {
  851. struct jfs_inode_info *jfs_ip = JFS_IP(ip);
  852. lid_t lid;
  853. struct tblock *tblk;
  854. struct tlock *tlck;
  855. struct maplock *maplock;
  856. TXN_LOCK();
  857. /*
  858. * allocate a tlock
  859. */
  860. lid = txLockAlloc();
  861. tlck = lid_to_tlock(lid);
  862. /*
  863. * initialize tlock
  864. */
  865. tlck->tid = tid;
  866. /* bind the tlock and the object */
  867. tlck->flag = tlckINODELOCK;
  868. if (S_ISDIR(ip->i_mode))
  869. tlck->flag |= tlckDIRECTORY;
  870. tlck->ip = ip;
  871. tlck->mp = NULL;
  872. tlck->type = type;
  873. /*
  874. * enqueue transaction lock to transaction/inode
  875. */
  876. /* insert the tlock at tail of transaction tlock list */
  877. if (tid) {
  878. tblk = tid_to_tblock(tid);
  879. if (tblk->next)
  880. lid_to_tlock(tblk->last)->next = lid;
  881. else
  882. tblk->next = lid;
  883. tlck->next = 0;
  884. tblk->last = lid;
  885. }
  886. /* anonymous transaction:
  887. * insert the tlock at head of inode anonymous tlock list
  888. */
  889. else {
  890. tlck->next = jfs_ip->atlhead;
  891. jfs_ip->atlhead = lid;
  892. if (tlck->next == 0) {
  893. /* This inode's first anonymous transaction */
  894. jfs_ip->atltail = lid;
  895. list_add_tail(&jfs_ip->anon_inode_list,
  896. &TxAnchor.anon_list);
  897. }
  898. }
  899. TXN_UNLOCK();
  900. /* initialize type dependent area for maplock */
  901. maplock = (struct maplock *) & tlck->lock;
  902. maplock->next = 0;
  903. maplock->maxcnt = 0;
  904. maplock->index = 0;
  905. return tlck;
  906. }
  907. /*
  908. * txLinelock()
  909. *
  910. * function: allocate a transaction lock for log vector list
  911. */
  912. struct linelock *txLinelock(struct linelock * tlock)
  913. {
  914. lid_t lid;
  915. struct tlock *tlck;
  916. struct linelock *linelock;
  917. TXN_LOCK();
  918. /* allocate a TxLock structure */
  919. lid = txLockAlloc();
  920. tlck = lid_to_tlock(lid);
  921. TXN_UNLOCK();
  922. /* initialize linelock */
  923. linelock = (struct linelock *) tlck;
  924. linelock->next = 0;
  925. linelock->flag = tlckLINELOCK;
  926. linelock->maxcnt = TLOCKLONG;
  927. linelock->index = 0;
  928. if (tlck->flag & tlckDIRECTORY)
  929. linelock->flag |= tlckDIRECTORY;
  930. /* append linelock after tlock */
  931. linelock->next = tlock->next;
  932. tlock->next = lid;
  933. return linelock;
  934. }
  935. /*
  936. * transaction commit management
  937. * -----------------------------
  938. */
  939. /*
  940. * NAME: txCommit()
  941. *
  942. * FUNCTION: commit the changes to the objects specified in
  943. * clist. For journalled segments only the
  944. * changes of the caller are committed, ie by tid.
  945. * for non-journalled segments the data are flushed to
  946. * disk and then the change to the disk inode and indirect
  947. * blocks committed (so blocks newly allocated to the
  948. * segment will be made a part of the segment atomically).
  949. *
  950. * all of the segments specified in clist must be in
  951. * one file system. no more than 6 segments are needed
  952. * to handle all unix svcs.
  953. *
  954. * if the i_nlink field (i.e. disk inode link count)
  955. * is zero, and the type of inode is a regular file or
  956. * directory, or symbolic link , the inode is truncated
  957. * to zero length. the truncation is committed but the
  958. * VM resources are unaffected until it is closed (see
  959. * iput and iclose).
  960. *
  961. * PARAMETER:
  962. *
  963. * RETURN:
  964. *
  965. * serialization:
  966. * on entry the inode lock on each segment is assumed
  967. * to be held.
  968. *
  969. * i/o error:
  970. */
  971. int txCommit(tid_t tid, /* transaction identifier */
  972. int nip, /* number of inodes to commit */
  973. struct inode **iplist, /* list of inode to commit */
  974. int flag)
  975. {
  976. int rc = 0;
  977. struct commit cd;
  978. struct jfs_log *log;
  979. struct tblock *tblk;
  980. struct lrd *lrd;
  981. struct inode *ip;
  982. struct jfs_inode_info *jfs_ip;
  983. int k, n;
  984. ino_t top;
  985. struct super_block *sb;
  986. jfs_info("txCommit, tid = %d, flag = %d", tid, flag);
  987. /* is read-only file system ? */
  988. if (isReadOnly(iplist[0])) {
  989. rc = -EROFS;
  990. goto TheEnd;
  991. }
  992. sb = cd.sb = iplist[0]->i_sb;
  993. cd.tid = tid;
  994. if (tid == 0)
  995. tid = txBegin(sb, 0);
  996. tblk = tid_to_tblock(tid);
  997. /*
  998. * initialize commit structure
  999. */
  1000. log = JFS_SBI(sb)->log;
  1001. cd.log = log;
  1002. /* initialize log record descriptor in commit */
  1003. lrd = &cd.lrd;
  1004. lrd->logtid = cpu_to_le32(tblk->logtid);
  1005. lrd->backchain = 0;
  1006. tblk->xflag |= flag;
  1007. if ((flag & (COMMIT_FORCE | COMMIT_SYNC)) == 0)
  1008. tblk->xflag |= COMMIT_LAZY;
  1009. /*
  1010. * prepare non-journaled objects for commit
  1011. *
  1012. * flush data pages of non-journaled file
  1013. * to prevent the file getting non-initialized disk blocks
  1014. * in case of crash.
  1015. * (new blocks - )
  1016. */
  1017. cd.iplist = iplist;
  1018. cd.nip = nip;
  1019. /*
  1020. * acquire transaction lock on (on-disk) inodes
  1021. *
  1022. * update on-disk inode from in-memory inode
  1023. * acquiring transaction locks for AFTER records
  1024. * on the on-disk inode of file object
  1025. *
  1026. * sort the inodes array by inode number in descending order
  1027. * to prevent deadlock when acquiring transaction lock
  1028. * of on-disk inodes on multiple on-disk inode pages by
  1029. * multiple concurrent transactions
  1030. */
  1031. for (k = 0; k < cd.nip; k++) {
  1032. top = (cd.iplist[k])->i_ino;
  1033. for (n = k + 1; n < cd.nip; n++) {
  1034. ip = cd.iplist[n];
  1035. if (ip->i_ino > top) {
  1036. top = ip->i_ino;
  1037. cd.iplist[n] = cd.iplist[k];
  1038. cd.iplist[k] = ip;
  1039. }
  1040. }
  1041. ip = cd.iplist[k];
  1042. jfs_ip = JFS_IP(ip);
  1043. /*
  1044. * BUGBUG - This code has temporarily been removed. The
  1045. * intent is to ensure that any file data is written before
  1046. * the metadata is committed to the journal. This prevents
  1047. * uninitialized data from appearing in a file after the
  1048. * journal has been replayed. (The uninitialized data
  1049. * could be sensitive data removed by another user.)
  1050. *
  1051. * The problem now is that we are holding the IWRITELOCK
  1052. * on the inode, and calling filemap_fdatawrite on an
  1053. * unmapped page will cause a deadlock in jfs_get_block.
  1054. *
  1055. * The long term solution is to pare down the use of
  1056. * IWRITELOCK. We are currently holding it too long.
  1057. * We could also be smarter about which data pages need
  1058. * to be written before the transaction is committed and
  1059. * when we don't need to worry about it at all.
  1060. *
  1061. * if ((!S_ISDIR(ip->i_mode))
  1062. * && (tblk->flag & COMMIT_DELETE) == 0)
  1063. * filemap_write_and_wait(ip->i_mapping);
  1064. */
  1065. /*
  1066. * Mark inode as not dirty. It will still be on the dirty
  1067. * inode list, but we'll know not to commit it again unless
  1068. * it gets marked dirty again
  1069. */
  1070. clear_cflag(COMMIT_Dirty, ip);
  1071. /* inherit anonymous tlock(s) of inode */
  1072. if (jfs_ip->atlhead) {
  1073. lid_to_tlock(jfs_ip->atltail)->next = tblk->next;
  1074. tblk->next = jfs_ip->atlhead;
  1075. if (!tblk->last)
  1076. tblk->last = jfs_ip->atltail;
  1077. jfs_ip->atlhead = jfs_ip->atltail = 0;
  1078. TXN_LOCK();
  1079. list_del_init(&jfs_ip->anon_inode_list);
  1080. TXN_UNLOCK();
  1081. }
  1082. /*
  1083. * acquire transaction lock on on-disk inode page
  1084. * (become first tlock of the tblk's tlock list)
  1085. */
  1086. if (((rc = diWrite(tid, ip))))
  1087. goto out;
  1088. }
  1089. /*
  1090. * write log records from transaction locks
  1091. *
  1092. * txUpdateMap() resets XAD_NEW in XAD.
  1093. */
  1094. txLog(log, tblk, &cd);
  1095. /*
  1096. * Ensure that inode isn't reused before
  1097. * lazy commit thread finishes processing
  1098. */
  1099. if (tblk->xflag & COMMIT_DELETE) {
  1100. ihold(tblk->u.ip);
  1101. /*
  1102. * Avoid a rare deadlock
  1103. *
  1104. * If the inode is locked, we may be blocked in
  1105. * jfs_commit_inode. If so, we don't want the
  1106. * lazy_commit thread doing the last iput() on the inode
  1107. * since that may block on the locked inode. Instead,
  1108. * commit the transaction synchronously, so the last iput
  1109. * will be done by the calling thread (or later)
  1110. */
  1111. /*
  1112. * I believe this code is no longer needed. Splitting I_LOCK
  1113. * into two bits, I_NEW and I_SYNC should prevent this
  1114. * deadlock as well. But since I don't have a JFS testload
  1115. * to verify this, only a trivial s/I_LOCK/I_SYNC/ was done.
  1116. * Joern
  1117. */
  1118. if (tblk->u.ip->i_state & I_SYNC)
  1119. tblk->xflag &= ~COMMIT_LAZY;
  1120. }
  1121. ASSERT((!(tblk->xflag & COMMIT_DELETE)) ||
  1122. ((tblk->u.ip->i_nlink == 0) &&
  1123. !test_cflag(COMMIT_Nolink, tblk->u.ip)));
  1124. /*
  1125. * write COMMIT log record
  1126. */
  1127. lrd->type = cpu_to_le16(LOG_COMMIT);
  1128. lrd->length = 0;
  1129. lmLog(log, tblk, lrd, NULL);
  1130. lmGroupCommit(log, tblk);
  1131. /*
  1132. * - transaction is now committed -
  1133. */
  1134. /*
  1135. * force pages in careful update
  1136. * (imap addressing structure update)
  1137. */
  1138. if (flag & COMMIT_FORCE)
  1139. txForce(tblk);
  1140. /*
  1141. * update allocation map.
  1142. *
  1143. * update inode allocation map and inode:
  1144. * free pager lock on memory object of inode if any.
  1145. * update block allocation map.
  1146. *
  1147. * txUpdateMap() resets XAD_NEW in XAD.
  1148. */
  1149. if (tblk->xflag & COMMIT_FORCE)
  1150. txUpdateMap(tblk);
  1151. /*
  1152. * free transaction locks and pageout/free pages
  1153. */
  1154. txRelease(tblk);
  1155. if ((tblk->flag & tblkGC_LAZY) == 0)
  1156. txUnlock(tblk);
  1157. /*
  1158. * reset in-memory object state
  1159. */
  1160. for (k = 0; k < cd.nip; k++) {
  1161. ip = cd.iplist[k];
  1162. jfs_ip = JFS_IP(ip);
  1163. /*
  1164. * reset in-memory inode state
  1165. */
  1166. jfs_ip->bxflag = 0;
  1167. jfs_ip->blid = 0;
  1168. }
  1169. out:
  1170. if (rc != 0)
  1171. txAbort(tid, 1);
  1172. TheEnd:
  1173. jfs_info("txCommit: tid = %d, returning %d", tid, rc);
  1174. return rc;
  1175. }
  1176. /*
  1177. * NAME: txLog()
  1178. *
  1179. * FUNCTION: Writes AFTER log records for all lines modified
  1180. * by tid for segments specified by inodes in comdata.
  1181. * Code assumes only WRITELOCKS are recorded in lockwords.
  1182. *
  1183. * PARAMETERS:
  1184. *
  1185. * RETURN :
  1186. */
  1187. static void txLog(struct jfs_log *log, struct tblock *tblk, struct commit *cd)
  1188. {
  1189. struct inode *ip;
  1190. lid_t lid;
  1191. struct tlock *tlck;
  1192. struct lrd *lrd = &cd->lrd;
  1193. /*
  1194. * write log record(s) for each tlock of transaction,
  1195. */
  1196. for (lid = tblk->next; lid; lid = tlck->next) {
  1197. tlck = lid_to_tlock(lid);
  1198. tlck->flag |= tlckLOG;
  1199. /* initialize lrd common */
  1200. ip = tlck->ip;
  1201. lrd->aggregate = cpu_to_le32(JFS_SBI(ip->i_sb)->aggregate);
  1202. lrd->log.redopage.fileset = cpu_to_le32(JFS_IP(ip)->fileset);
  1203. lrd->log.redopage.inode = cpu_to_le32(ip->i_ino);
  1204. /* write log record of page from the tlock */
  1205. switch (tlck->type & tlckTYPE) {
  1206. case tlckXTREE:
  1207. xtLog(log, tblk, lrd, tlck);
  1208. break;
  1209. case tlckDTREE:
  1210. dtLog(log, tblk, lrd, tlck);
  1211. break;
  1212. case tlckINODE:
  1213. diLog(log, tblk, lrd, tlck, cd);
  1214. break;
  1215. case tlckMAP:
  1216. mapLog(log, tblk, lrd, tlck);
  1217. break;
  1218. case tlckDATA:
  1219. dataLog(log, tblk, lrd, tlck);
  1220. break;
  1221. default:
  1222. jfs_err("UFO tlock:0x%p", tlck);
  1223. }
  1224. }
  1225. return;
  1226. }
  1227. /*
  1228. * diLog()
  1229. *
  1230. * function: log inode tlock and format maplock to update bmap;
  1231. */
  1232. static void diLog(struct jfs_log *log, struct tblock *tblk, struct lrd *lrd,
  1233. struct tlock *tlck, struct commit *cd)
  1234. {
  1235. struct metapage *mp;
  1236. pxd_t *pxd;
  1237. struct pxd_lock *pxdlock;
  1238. mp = tlck->mp;
  1239. /* initialize as REDOPAGE record format */
  1240. lrd->log.redopage.type = cpu_to_le16(LOG_INODE);
  1241. lrd->log.redopage.l2linesize = cpu_to_le16(L2INODESLOTSIZE);
  1242. pxd = &lrd->log.redopage.pxd;
  1243. /*
  1244. * inode after image
  1245. */
  1246. if (tlck->type & tlckENTRY) {
  1247. /* log after-image for logredo(): */
  1248. lrd->type = cpu_to_le16(LOG_REDOPAGE);
  1249. PXDaddress(pxd, mp->index);
  1250. PXDlength(pxd,
  1251. mp->logical_size >> tblk->sb->s_blocksize_bits);
  1252. lrd->backchain = cpu_to_le32(lmLog(log, tblk, lrd, tlck));
  1253. /* mark page as homeward bound */
  1254. tlck->flag |= tlckWRITEPAGE;
  1255. } else if (tlck->type & tlckFREE) {
  1256. /*
  1257. * free inode extent
  1258. *
  1259. * (pages of the freed inode extent have been invalidated and
  1260. * a maplock for free of the extent has been formatted at
  1261. * txLock() time);
  1262. *
  1263. * the tlock had been acquired on the inode allocation map page
  1264. * (iag) that specifies the freed extent, even though the map
  1265. * page is not itself logged, to prevent pageout of the map
  1266. * page before the log;
  1267. */
  1268. /* log LOG_NOREDOINOEXT of the freed inode extent for
  1269. * logredo() to start NoRedoPage filters, and to update
  1270. * imap and bmap for free of the extent;
  1271. */
  1272. lrd->type = cpu_to_le16(LOG_NOREDOINOEXT);
  1273. /*
  1274. * For the LOG_NOREDOINOEXT record, we need
  1275. * to pass the IAG number and inode extent
  1276. * index (within that IAG) from which the
  1277. * extent is being released. These have been
  1278. * passed to us in the iplist[1] and iplist[2].
  1279. */
  1280. lrd->log.noredoinoext.iagnum =
  1281. cpu_to_le32((u32) (size_t) cd->iplist[1]);
  1282. lrd->log.noredoinoext.inoext_idx =
  1283. cpu_to_le32((u32) (size_t) cd->iplist[2]);
  1284. pxdlock = (struct pxd_lock *) & tlck->lock;
  1285. *pxd = pxdlock->pxd;
  1286. lrd->backchain = cpu_to_le32(lmLog(log, tblk, lrd, NULL));
  1287. /* update bmap */
  1288. tlck->flag |= tlckUPDATEMAP;
  1289. /* mark page as homeward bound */
  1290. tlck->flag |= tlckWRITEPAGE;
  1291. } else
  1292. jfs_err("diLog: UFO type tlck:0x%p", tlck);
  1293. return;
  1294. }
  1295. /*
  1296. * dataLog()
  1297. *
  1298. * function: log data tlock
  1299. */
  1300. static void dataLog(struct jfs_log *log, struct tblock *tblk, struct lrd *lrd,
  1301. struct tlock *tlck)
  1302. {
  1303. struct metapage *mp;
  1304. pxd_t *pxd;
  1305. mp = tlck->mp;
  1306. /* initialize as REDOPAGE record format */
  1307. lrd->log.redopage.type = cpu_to_le16(LOG_DATA);
  1308. lrd->log.redopage.l2linesize = cpu_to_le16(L2DATASLOTSIZE);
  1309. pxd = &lrd->log.redopage.pxd;
  1310. /* log after-image for logredo(): */
  1311. lrd->type = cpu_to_le16(LOG_REDOPAGE);
  1312. if (jfs_dirtable_inline(tlck->ip)) {
  1313. /*
  1314. * The table has been truncated, we've must have deleted
  1315. * the last entry, so don't bother logging this
  1316. */
  1317. mp->lid = 0;
  1318. grab_metapage(mp);
  1319. metapage_homeok(mp);
  1320. discard_metapage(mp);
  1321. tlck->mp = NULL;
  1322. return;
  1323. }
  1324. PXDaddress(pxd, mp->index);
  1325. PXDlength(pxd, mp->logical_size >> tblk->sb->s_blocksize_bits);
  1326. lrd->backchain = cpu_to_le32(lmLog(log, tblk, lrd, tlck));
  1327. /* mark page as homeward bound */
  1328. tlck->flag |= tlckWRITEPAGE;
  1329. return;
  1330. }
  1331. /*
  1332. * dtLog()
  1333. *
  1334. * function: log dtree tlock and format maplock to update bmap;
  1335. */
  1336. static void dtLog(struct jfs_log * log, struct tblock * tblk, struct lrd * lrd,
  1337. struct tlock * tlck)
  1338. {
  1339. struct metapage *mp;
  1340. struct pxd_lock *pxdlock;
  1341. pxd_t *pxd;
  1342. mp = tlck->mp;
  1343. /* initialize as REDOPAGE/NOREDOPAGE record format */
  1344. lrd->log.redopage.type = cpu_to_le16(LOG_DTREE);
  1345. lrd->log.redopage.l2linesize = cpu_to_le16(L2DTSLOTSIZE);
  1346. pxd = &lrd->log.redopage.pxd;
  1347. if (tlck->type & tlckBTROOT)
  1348. lrd->log.redopage.type |= cpu_to_le16(LOG_BTROOT);
  1349. /*
  1350. * page extension via relocation: entry insertion;
  1351. * page extension in-place: entry insertion;
  1352. * new right page from page split, reinitialized in-line
  1353. * root from root page split: entry insertion;
  1354. */
  1355. if (tlck->type & (tlckNEW | tlckEXTEND)) {
  1356. /* log after-image of the new page for logredo():
  1357. * mark log (LOG_NEW) for logredo() to initialize
  1358. * freelist and update bmap for alloc of the new page;
  1359. */
  1360. lrd->type = cpu_to_le16(LOG_REDOPAGE);
  1361. if (tlck->type & tlckEXTEND)
  1362. lrd->log.redopage.type |= cpu_to_le16(LOG_EXTEND);
  1363. else
  1364. lrd->log.redopage.type |= cpu_to_le16(LOG_NEW);
  1365. PXDaddress(pxd, mp->index);
  1366. PXDlength(pxd,
  1367. mp->logical_size >> tblk->sb->s_blocksize_bits);
  1368. lrd->backchain = cpu_to_le32(lmLog(log, tblk, lrd, tlck));
  1369. /* format a maplock for txUpdateMap() to update bPMAP for
  1370. * alloc of the new page;
  1371. */
  1372. if (tlck->type & tlckBTROOT)
  1373. return;
  1374. tlck->flag |= tlckUPDATEMAP;
  1375. pxdlock = (struct pxd_lock *) & tlck->lock;
  1376. pxdlock->flag = mlckALLOCPXD;
  1377. pxdlock->pxd = *pxd;
  1378. pxdlock->index = 1;
  1379. /* mark page as homeward bound */
  1380. tlck->flag |= tlckWRITEPAGE;
  1381. return;
  1382. }
  1383. /*
  1384. * entry insertion/deletion,
  1385. * sibling page link update (old right page before split);
  1386. */
  1387. if (tlck->type & (tlckENTRY | tlckRELINK)) {
  1388. /* log after-image for logredo(): */
  1389. lrd->type = cpu_to_le16(LOG_REDOPAGE);
  1390. PXDaddress(pxd, mp->index);
  1391. PXDlength(pxd,
  1392. mp->logical_size >> tblk->sb->s_blocksize_bits);
  1393. lrd->backchain = cpu_to_le32(lmLog(log, tblk, lrd, tlck));
  1394. /* mark page as homeward bound */
  1395. tlck->flag |= tlckWRITEPAGE;
  1396. return;
  1397. }
  1398. /*
  1399. * page deletion: page has been invalidated
  1400. * page relocation: source extent
  1401. *
  1402. * a maplock for free of the page has been formatted
  1403. * at txLock() time);
  1404. */
  1405. if (tlck->type & (tlckFREE | tlckRELOCATE)) {
  1406. /* log LOG_NOREDOPAGE of the deleted page for logredo()
  1407. * to start NoRedoPage filter and to update bmap for free
  1408. * of the deletd page
  1409. */
  1410. lrd->type = cpu_to_le16(LOG_NOREDOPAGE);
  1411. pxdlock = (struct pxd_lock *) & tlck->lock;
  1412. *pxd = pxdlock->pxd;
  1413. lrd->backchain = cpu_to_le32(lmLog(log, tblk, lrd, NULL));
  1414. /* a maplock for txUpdateMap() for free of the page
  1415. * has been formatted at txLock() time;
  1416. */
  1417. tlck->flag |= tlckUPDATEMAP;
  1418. }
  1419. return;
  1420. }
  1421. /*
  1422. * xtLog()
  1423. *
  1424. * function: log xtree tlock and format maplock to update bmap;
  1425. */
  1426. static void xtLog(struct jfs_log * log, struct tblock * tblk, struct lrd * lrd,
  1427. struct tlock * tlck)
  1428. {
  1429. struct inode *ip;
  1430. struct metapage *mp;
  1431. xtpage_t *p;
  1432. struct xtlock *xtlck;
  1433. struct maplock *maplock;
  1434. struct xdlistlock *xadlock;
  1435. struct pxd_lock *pxdlock;
  1436. pxd_t *page_pxd;
  1437. int next, lwm, hwm;
  1438. ip = tlck->ip;
  1439. mp = tlck->mp;
  1440. /* initialize as REDOPAGE/NOREDOPAGE record format */
  1441. lrd->log.redopage.type = cpu_to_le16(LOG_XTREE);
  1442. lrd->log.redopage.l2linesize = cpu_to_le16(L2XTSLOTSIZE);
  1443. page_pxd = &lrd->log.redopage.pxd;
  1444. if (tlck->type & tlckBTROOT) {
  1445. lrd->log.redopage.type |= cpu_to_le16(LOG_BTROOT);
  1446. p = &JFS_IP(ip)->i_xtroot;
  1447. if (S_ISDIR(ip->i_mode))
  1448. lrd->log.redopage.type |=
  1449. cpu_to_le16(LOG_DIR_XTREE);
  1450. } else
  1451. p = (xtpage_t *) mp->data;
  1452. next = le16_to_cpu(p->header.nextindex);
  1453. xtlck = (struct xtlock *) & tlck->lock;
  1454. maplock = (struct maplock *) & tlck->lock;
  1455. xadlock = (struct xdlistlock *) maplock;
  1456. /*
  1457. * entry insertion/extension;
  1458. * sibling page link update (old right page before split);
  1459. */
  1460. if (tlck->type & (tlckNEW | tlckGROW | tlckRELINK)) {
  1461. /* log after-image for logredo():
  1462. * logredo() will update bmap for alloc of new/extended
  1463. * extents (XAD_NEW|XAD_EXTEND) of XAD[lwm:next) from
  1464. * after-image of XADlist;
  1465. * logredo() resets (XAD_NEW|XAD_EXTEND) flag when
  1466. * applying the after-image to the meta-data page.
  1467. */
  1468. lrd->type = cpu_to_le16(LOG_REDOPAGE);
  1469. PXDaddress(page_pxd, mp->index);
  1470. PXDlength(page_pxd,
  1471. mp->logical_size >> tblk->sb->s_blocksize_bits);
  1472. lrd->backchain = cpu_to_le32(lmLog(log, tblk, lrd, tlck));
  1473. /* format a maplock for txUpdateMap() to update bPMAP
  1474. * for alloc of new/extended extents of XAD[lwm:next)
  1475. * from the page itself;
  1476. * txUpdateMap() resets (XAD_NEW|XAD_EXTEND) flag.
  1477. */
  1478. lwm = xtlck->lwm.offset;
  1479. if (lwm == 0)
  1480. lwm = XTPAGEMAXSLOT;
  1481. if (lwm == next)
  1482. goto out;
  1483. if (lwm > next) {
  1484. jfs_err("xtLog: lwm > next");
  1485. goto out;
  1486. }
  1487. tlck->flag |= tlckUPDATEMAP;
  1488. xadlock->flag = mlckALLOCXADLIST;
  1489. xadlock->count = next - lwm;
  1490. if ((xadlock->count <= 4) && (tblk->xflag & COMMIT_LAZY)) {
  1491. int i;
  1492. pxd_t *pxd;
  1493. /*
  1494. * Lazy commit may allow xtree to be modified before
  1495. * txUpdateMap runs. Copy xad into linelock to
  1496. * preserve correct data.
  1497. *
  1498. * We can fit twice as may pxd's as xads in the lock
  1499. */
  1500. xadlock->flag = mlckALLOCPXDLIST;
  1501. pxd = xadlock->xdlist = &xtlck->pxdlock;
  1502. for (i = 0; i < xadlock->count; i++) {
  1503. PXDaddress(pxd, addressXAD(&p->xad[lwm + i]));
  1504. PXDlength(pxd, lengthXAD(&p->xad[lwm + i]));
  1505. p->xad[lwm + i].flag &=
  1506. ~(XAD_NEW | XAD_EXTENDED);
  1507. pxd++;
  1508. }
  1509. } else {
  1510. /*
  1511. * xdlist will point to into inode's xtree, ensure
  1512. * that transaction is not committed lazily.
  1513. */
  1514. xadlock->flag = mlckALLOCXADLIST;
  1515. xadlock->xdlist = &p->xad[lwm];
  1516. tblk->xflag &= ~COMMIT_LAZY;
  1517. }
  1518. jfs_info("xtLog: alloc ip:0x%p mp:0x%p tlck:0x%p lwm:%d count:%d",
  1519. tlck->ip, mp, tlck, lwm, xadlock->count);
  1520. maplock->index = 1;
  1521. out:
  1522. /* mark page as homeward bound */
  1523. tlck->flag |= tlckWRITEPAGE;
  1524. return;
  1525. }
  1526. /*
  1527. * page deletion: file deletion/truncation (ref. xtTruncate())
  1528. *
  1529. * (page will be invalidated after log is written and bmap
  1530. * is updated from the page);
  1531. */
  1532. if (tlck->type & tlckFREE) {
  1533. /* LOG_NOREDOPAGE log for NoRedoPage filter:
  1534. * if page free from file delete, NoRedoFile filter from
  1535. * inode image of zero link count will subsume NoRedoPage
  1536. * filters for each page;
  1537. * if page free from file truncattion, write NoRedoPage
  1538. * filter;
  1539. *
  1540. * upadte of block allocation map for the page itself:
  1541. * if page free from deletion and truncation, LOG_UPDATEMAP
  1542. * log for the page itself is generated from processing
  1543. * its parent page xad entries;
  1544. */
  1545. /* if page free from file truncation, log LOG_NOREDOPAGE
  1546. * of the deleted page for logredo() to start NoRedoPage
  1547. * filter for the page;
  1548. */
  1549. if (tblk->xflag & COMMIT_TRUNCATE) {
  1550. /* write NOREDOPAGE for the page */
  1551. lrd->type = cpu_to_le16(LOG_NOREDOPAGE);
  1552. PXDaddress(page_pxd, mp->index);
  1553. PXDlength(page_pxd,
  1554. mp->logical_size >> tblk->sb->
  1555. s_blocksize_bits);
  1556. lrd->backchain =
  1557. cpu_to_le32(lmLog(log, tblk, lrd, NULL));
  1558. if (tlck->type & tlckBTROOT) {
  1559. /* Empty xtree must be logged */
  1560. lrd->type = cpu_to_le16(LOG_REDOPAGE);
  1561. lrd->backchain =
  1562. cpu_to_le32(lmLog(log, tblk, lrd, tlck));
  1563. }
  1564. }
  1565. /* init LOG_UPDATEMAP of the freed extents
  1566. * XAD[XTENTRYSTART:hwm) from the deleted page itself
  1567. * for logredo() to update bmap;
  1568. */
  1569. lrd->type = cpu_to_le16(LOG_UPDATEMAP);
  1570. lrd->log.updatemap.type = cpu_to_le16(LOG_FREEXADLIST);
  1571. xtlck = (struct xtlock *) & tlck->lock;
  1572. hwm = xtlck->hwm.offset;
  1573. lrd->log.updatemap.nxd =
  1574. cpu_to_le16(hwm - XTENTRYSTART + 1);
  1575. /* reformat linelock for lmLog() */
  1576. xtlck->header.offset = XTENTRYSTART;
  1577. xtlck->header.length = hwm - XTENTRYSTART + 1;
  1578. xtlck->index = 1;
  1579. lrd->backchain = cpu_to_le32(lmLog(log, tblk, lrd, tlck));
  1580. /* format a maplock for txUpdateMap() to update bmap
  1581. * to free extents of XAD[XTENTRYSTART:hwm) from the
  1582. * deleted page itself;
  1583. */
  1584. tlck->flag |= tlckUPDATEMAP;
  1585. xadlock->count = hwm - XTENTRYSTART + 1;
  1586. if ((xadlock->count <= 4) && (tblk->xflag & COMMIT_LAZY)) {
  1587. int i;
  1588. pxd_t *pxd;
  1589. /*
  1590. * Lazy commit may allow xtree to be modified before
  1591. * txUpdateMap runs. Copy xad into linelock to
  1592. * preserve correct data.
  1593. *
  1594. * We can fit twice as may pxd's as xads in the lock
  1595. */
  1596. xadlock->flag = mlckFREEPXDLIST;
  1597. pxd = xadlock->xdlist = &xtlck->pxdlock;
  1598. for (i = 0; i < xadlock->count; i++) {
  1599. PXDaddress(pxd,
  1600. addressXAD(&p->xad[XTENTRYSTART + i]));
  1601. PXDlength(pxd,
  1602. lengthXAD(&p->xad[XTENTRYSTART + i]));
  1603. pxd++;
  1604. }
  1605. } else {
  1606. /*
  1607. * xdlist will point to into inode's xtree, ensure
  1608. * that transaction is not committed lazily.
  1609. */
  1610. xadlock->flag = mlckFREEXADLIST;
  1611. xadlock->xdlist = &p->xad[XTENTRYSTART];
  1612. tblk->xflag &= ~COMMIT_LAZY;
  1613. }
  1614. jfs_info("xtLog: free ip:0x%p mp:0x%p count:%d lwm:2",
  1615. tlck->ip, mp, xadlock->count);
  1616. maplock->index = 1;
  1617. /* mark page as invalid */
  1618. if (((tblk->xflag & COMMIT_PWMAP) || S_ISDIR(ip->i_mode))
  1619. && !(tlck->type & tlckBTROOT))
  1620. tlck->flag |= tlckFREEPAGE;
  1621. /*
  1622. else (tblk->xflag & COMMIT_PMAP)
  1623. ? release the page;
  1624. */
  1625. return;
  1626. }
  1627. /*
  1628. * page/entry truncation: file truncation (ref. xtTruncate())
  1629. *
  1630. * |----------+------+------+---------------|
  1631. * | | |
  1632. * | | hwm - hwm before truncation
  1633. * | next - truncation point
  1634. * lwm - lwm before truncation
  1635. * header ?
  1636. */
  1637. if (tlck->type & tlckTRUNCATE) {
  1638. pxd_t pxd; /* truncated extent of xad */
  1639. int twm;
  1640. /*
  1641. * For truncation the entire linelock may be used, so it would
  1642. * be difficult to store xad list in linelock itself.
  1643. * Therefore, we'll just force transaction to be committed
  1644. * synchronously, so that xtree pages won't be changed before
  1645. * txUpdateMap runs.
  1646. */
  1647. tblk->xflag &= ~COMMIT_LAZY;
  1648. lwm = xtlck->lwm.offset;
  1649. if (lwm == 0)
  1650. lwm = XTPAGEMAXSLOT;
  1651. hwm = xtlck->hwm.offset;
  1652. twm = xtlck->twm.offset;
  1653. /*
  1654. * write log records
  1655. */
  1656. /* log after-image for logredo():
  1657. *
  1658. * logredo() will update bmap for alloc of new/extended
  1659. * extents (XAD_NEW|XAD_EXTEND) of XAD[lwm:next) from
  1660. * after-image of XADlist;
  1661. * logredo() resets (XAD_NEW|XAD_EXTEND) flag when
  1662. * applying the after-image to the meta-data page.
  1663. */
  1664. lrd->type = cpu_to_le16(LOG_REDOPAGE);
  1665. PXDaddress(page_pxd, mp->index);
  1666. PXDlength(page_pxd,
  1667. mp->logical_size >> tblk->sb->s_blocksize_bits);
  1668. lrd->backchain = cpu_to_le32(lmLog(log, tblk, lrd, tlck));
  1669. /*
  1670. * truncate entry XAD[twm == next - 1]:
  1671. */
  1672. if (twm == next - 1) {
  1673. /* init LOG_UPDATEMAP for logredo() to update bmap for
  1674. * free of truncated delta extent of the truncated
  1675. * entry XAD[next - 1]:
  1676. * (xtlck->pxdlock = truncated delta extent);
  1677. */
  1678. pxdlock = (struct pxd_lock *) & xtlck->pxdlock;
  1679. /* assert(pxdlock->type & tlckTRUNCATE); */
  1680. lrd->type = cpu_to_le16(LOG_UPDATEMAP);
  1681. lrd->log.updatemap.type = cpu_to_le16(LOG_FREEPXD);
  1682. lrd->log.updatemap.nxd = cpu_to_le16(1);
  1683. lrd->log.updatemap.pxd = pxdlock->pxd;
  1684. pxd = pxdlock->pxd; /* save to format maplock */
  1685. lrd->backchain =
  1686. cpu_to_le32(lmLog(log, tblk, lrd, NULL));
  1687. }
  1688. /*
  1689. * free entries XAD[next:hwm]:
  1690. */
  1691. if (hwm >= next) {
  1692. /* init LOG_UPDATEMAP of the freed extents
  1693. * XAD[next:hwm] from the deleted page itself
  1694. * for logredo() to update bmap;
  1695. */
  1696. lrd->type = cpu_to_le16(LOG_UPDATEMAP);
  1697. lrd->log.updatemap.type =
  1698. cpu_to_le16(LOG_FREEXADLIST);
  1699. xtlck = (struct xtlock *) & tlck->lock;
  1700. hwm = xtlck->hwm.offset;
  1701. lrd->log.updatemap.nxd =
  1702. cpu_to_le16(hwm - next + 1);
  1703. /* reformat linelock for lmLog() */
  1704. xtlck->header.offset = next;
  1705. xtlck->header.length = hwm - next + 1;
  1706. xtlck->index = 1;
  1707. lrd->backchain =
  1708. cpu_to_le32(lmLog(log, tblk, lrd, tlck));
  1709. }
  1710. /*
  1711. * format maplock(s) for txUpdateMap() to update bmap
  1712. */
  1713. maplock->index = 0;
  1714. /*
  1715. * allocate entries XAD[lwm:next):
  1716. */
  1717. if (lwm < next) {
  1718. /* format a maplock for txUpdateMap() to update bPMAP
  1719. * for alloc of new/extended extents of XAD[lwm:next)
  1720. * from the page itself;
  1721. * txUpdateMap() resets (XAD_NEW|XAD_EXTEND) flag.
  1722. */
  1723. tlck->flag |= tlckUPDATEMAP;
  1724. xadlock->flag = mlckALLOCXADLIST;
  1725. xadlock->count = next - lwm;
  1726. xadlock->xdlist = &p->xad[lwm];
  1727. jfs_info("xtLog: alloc ip:0x%p mp:0x%p count:%d lwm:%d next:%d",
  1728. tlck->ip, mp, xadlock->count, lwm, next);
  1729. maplock->index++;
  1730. xadlock++;
  1731. }
  1732. /*
  1733. * truncate entry XAD[twm == next - 1]:
  1734. */
  1735. if (twm == next - 1) {
  1736. /* format a maplock for txUpdateMap() to update bmap
  1737. * to free truncated delta extent of the truncated
  1738. * entry XAD[next - 1];
  1739. * (xtlck->pxdlock = truncated delta extent);
  1740. */
  1741. tlck->flag |= tlckUPDATEMAP;
  1742. pxdlock = (struct pxd_lock *) xadlock;
  1743. pxdlock->flag = mlckFREEPXD;
  1744. pxdlock->count = 1;
  1745. pxdlock->pxd = pxd;
  1746. jfs_info("xtLog: truncate ip:0x%p mp:0x%p count:%d hwm:%d",
  1747. ip, mp, pxdlock->count, hwm);
  1748. maplock->index++;
  1749. xadlock++;
  1750. }
  1751. /*
  1752. * free entries XAD[next:hwm]:
  1753. */
  1754. if (hwm >= next) {
  1755. /* format a maplock for txUpdateMap() to update bmap
  1756. * to free extents of XAD[next:hwm] from thedeleted
  1757. * page itself;
  1758. */
  1759. tlck->flag |= tlckUPDATEMAP;
  1760. xadlock->flag = mlckFREEXADLIST;
  1761. xadlock->count = hwm - next + 1;
  1762. xadlock->xdlist = &p->xad[next];
  1763. jfs_info("xtLog: free ip:0x%p mp:0x%p count:%d next:%d hwm:%d",
  1764. tlck->ip, mp, xadlock->count, next, hwm);
  1765. maplock->index++;
  1766. }
  1767. /* mark page as homeward bound */
  1768. tlck->flag |= tlckWRITEPAGE;
  1769. }
  1770. return;
  1771. }
  1772. /*
  1773. * mapLog()
  1774. *
  1775. * function: log from maplock of freed data extents;
  1776. */
  1777. static void mapLog(struct jfs_log * log, struct tblock * tblk, struct lrd * lrd,
  1778. struct tlock * tlck)
  1779. {
  1780. struct pxd_lock *pxdlock;
  1781. int i, nlock;
  1782. pxd_t *pxd;
  1783. /*
  1784. * page relocation: free the source page extent
  1785. *
  1786. * a maplock for txUpdateMap() for free of the page
  1787. * has been formatted at txLock() time saving the src
  1788. * relocated page address;
  1789. */
  1790. if (tlck->type & tlckRELOCATE) {
  1791. /* log LOG_NOREDOPAGE of the old relocated page
  1792. * for logredo() to start NoRedoPage filter;
  1793. */
  1794. lrd->type = cpu_to_le16(LOG_NOREDOPAGE);
  1795. pxdlock = (struct pxd_lock *) & tlck->lock;
  1796. pxd = &lrd->log.redopage.pxd;
  1797. *pxd = pxdlock->pxd;
  1798. lrd->backchain = cpu_to_le32(lmLog(log, tblk, lrd, NULL));
  1799. /* (N.B. currently, logredo() does NOT update bmap
  1800. * for free of the page itself for (LOG_XTREE|LOG_NOREDOPAGE);
  1801. * if page free from relocation, LOG_UPDATEMAP log is
  1802. * specifically generated now for logredo()
  1803. * to update bmap for free of src relocated page;
  1804. * (new flag LOG_RELOCATE may be introduced which will
  1805. * inform logredo() to start NORedoPage filter and also
  1806. * update block allocation map at the same time, thus
  1807. * avoiding an extra log write);
  1808. */
  1809. lrd->type = cpu_to_le16(LOG_UPDATEMAP);
  1810. lrd->log.updatemap.type = cpu_to_le16(LOG_FREEPXD);
  1811. lrd->log.updatemap.nxd = cpu_to_le16(1);
  1812. lrd->log.updatemap.pxd = pxdlock->pxd;
  1813. lrd->backchain = cpu_to_le32(lmLog(log, tblk, lrd, NULL));
  1814. /* a maplock for txUpdateMap() for free of the page
  1815. * has been formatted at txLock() time;
  1816. */
  1817. tlck->flag |= tlckUPDATEMAP;
  1818. return;
  1819. }
  1820. /*
  1821. * Otherwise it's not a relocate request
  1822. *
  1823. */
  1824. else {
  1825. /* log LOG_UPDATEMAP for logredo() to update bmap for
  1826. * free of truncated/relocated delta extent of the data;
  1827. * e.g.: external EA extent, relocated/truncated extent
  1828. * from xtTailgate();
  1829. */
  1830. lrd->type = cpu_to_le16(LOG_UPDATEMAP);
  1831. pxdlock = (struct pxd_lock *) & tlck->lock;
  1832. nlock = pxdlock->index;
  1833. for (i = 0; i < nlock; i++, pxdlock++) {
  1834. if (pxdlock->flag & mlckALLOCPXD)
  1835. lrd->log.updatemap.type =
  1836. cpu_to_le16(LOG_ALLOCPXD);
  1837. else
  1838. lrd->log.updatemap.type =
  1839. cpu_to_le16(LOG_FREEPXD);
  1840. lrd->log.updatemap.nxd = cpu_to_le16(1);
  1841. lrd->log.updatemap.pxd = pxdlock->pxd;
  1842. lrd->backchain =
  1843. cpu_to_le32(lmLog(log, tblk, lrd, NULL));
  1844. jfs_info("mapLog: xaddr:0x%lx xlen:0x%x",
  1845. (ulong) addressPXD(&pxdlock->pxd),
  1846. lengthPXD(&pxdlock->pxd));
  1847. }
  1848. /* update bmap */
  1849. tlck->flag |= tlckUPDATEMAP;
  1850. }
  1851. }
  1852. /*
  1853. * txEA()
  1854. *
  1855. * function: acquire maplock for EA/ACL extents or
  1856. * set COMMIT_INLINE flag;
  1857. */
  1858. void txEA(tid_t tid, struct inode *ip, dxd_t * oldea, dxd_t * newea)
  1859. {
  1860. struct tlock *tlck = NULL;
  1861. struct pxd_lock *maplock = NULL, *pxdlock = NULL;
  1862. /*
  1863. * format maplock for alloc of new EA extent
  1864. */
  1865. if (newea) {
  1866. /* Since the newea could be a completely zeroed entry we need to
  1867. * check for the two flags which indicate we should actually
  1868. * commit new EA data
  1869. */
  1870. if (newea->flag & DXD_EXTENT) {
  1871. tlck = txMaplock(tid, ip, tlckMAP);
  1872. maplock = (struct pxd_lock *) & tlck->lock;
  1873. pxdlock = (struct pxd_lock *) maplock;
  1874. pxdlock->flag = mlckALLOCPXD;
  1875. PXDaddress(&pxdlock->pxd, addressDXD(newea));
  1876. PXDlength(&pxdlock->pxd, lengthDXD(newea));
  1877. pxdlock++;
  1878. maplock->index = 1;
  1879. } else if (newea->flag & DXD_INLINE) {
  1880. tlck = NULL;
  1881. set_cflag(COMMIT_Inlineea, ip);
  1882. }
  1883. }
  1884. /*
  1885. * format maplock for free of old EA extent
  1886. */
  1887. if (!test_cflag(COMMIT_Nolink, ip) && oldea->flag & DXD_EXTENT) {
  1888. if (tlck == NULL) {
  1889. tlck = txMaplock(tid, ip, tlckMAP);
  1890. maplock = (struct pxd_lock *) & tlck->lock;
  1891. pxdlock = (struct pxd_lock *) maplock;
  1892. maplock->index = 0;
  1893. }
  1894. pxdlock->flag = mlckFREEPXD;
  1895. PXDaddress(&pxdlock->pxd, addressDXD(oldea));
  1896. PXDlength(&pxdlock->pxd, lengthDXD(oldea));
  1897. maplock->index++;
  1898. }
  1899. }
  1900. /*
  1901. * txForce()
  1902. *
  1903. * function: synchronously write pages locked by transaction
  1904. * after txLog() but before txUpdateMap();
  1905. */
  1906. static void txForce(struct tblock * tblk)
  1907. {
  1908. struct tlock *tlck;
  1909. lid_t lid, next;
  1910. struct metapage *mp;
  1911. /*
  1912. * reverse the order of transaction tlocks in
  1913. * careful update order of address index pages
  1914. * (right to left, bottom up)
  1915. */
  1916. tlck = lid_to_tlock(tblk->next);
  1917. lid = tlck->next;
  1918. tlck->next = 0;
  1919. while (lid) {
  1920. tlck = lid_to_tlock(lid);
  1921. next = tlck->next;
  1922. tlck->next = tblk->next;
  1923. tblk->next = lid;
  1924. lid = next;
  1925. }
  1926. /*
  1927. * synchronously write the page, and
  1928. * hold the page for txUpdateMap();
  1929. */
  1930. for (lid = tblk->next; lid; lid = next) {
  1931. tlck = lid_to_tlock(lid);
  1932. next = tlck->next;
  1933. if ((mp = tlck->mp) != NULL &&
  1934. (tlck->type & tlckBTROOT) == 0) {
  1935. assert(mp->xflag & COMMIT_PAGE);
  1936. if (tlck->flag & tlckWRITEPAGE) {
  1937. tlck->flag &= ~tlckWRITEPAGE;
  1938. /* do not release page to freelist */
  1939. force_metapage(mp);
  1940. #if 0
  1941. /*
  1942. * The "right" thing to do here is to
  1943. * synchronously write the metadata.
  1944. * With the current implementation this
  1945. * is hard since write_metapage requires
  1946. * us to kunmap & remap the page. If we
  1947. * have tlocks pointing into the metadata
  1948. * pages, we don't want to do this. I think
  1949. * we can get by with synchronously writing
  1950. * the pages when they are released.
  1951. */
  1952. assert(mp->nohomeok);
  1953. set_bit(META_dirty, &mp->flag);
  1954. set_bit(META_sync, &mp->flag);
  1955. #endif
  1956. }
  1957. }
  1958. }
  1959. }
  1960. /*
  1961. * txUpdateMap()
  1962. *
  1963. * function: update persistent allocation map (and working map
  1964. * if appropriate);
  1965. *
  1966. * parameter:
  1967. */
  1968. static void txUpdateMap(struct tblock * tblk)
  1969. {
  1970. struct inode *ip;
  1971. struct inode *ipimap;
  1972. lid_t lid;
  1973. struct tlock *tlck;
  1974. struct maplock *maplock;
  1975. struct pxd_lock pxdlock;
  1976. int maptype;
  1977. int k, nlock;
  1978. struct metapage *mp = NULL;
  1979. ipimap = JFS_SBI(tblk->sb)->ipimap;
  1980. maptype = (tblk->xflag & COMMIT_PMAP) ? COMMIT_PMAP : COMMIT_PWMAP;
  1981. /*
  1982. * update block allocation map
  1983. *
  1984. * update allocation state in pmap (and wmap) and
  1985. * update lsn of the pmap page;
  1986. */
  1987. /*
  1988. * scan each tlock/page of transaction for block allocation/free:
  1989. *
  1990. * for each tlock/page of transaction, update map.
  1991. * ? are there tlock for pmap and pwmap at the same time ?
  1992. */
  1993. for (lid = tblk->next; lid; lid = tlck->next) {
  1994. tlck = lid_to_tlock(lid);
  1995. if ((tlck->flag & tlckUPDATEMAP) == 0)
  1996. continue;
  1997. if (tlck->flag & tlckFREEPAGE) {
  1998. /*
  1999. * Another thread may attempt to reuse freed space
  2000. * immediately, so we want to get rid of the metapage
  2001. * before anyone else has a chance to get it.
  2002. * Lock metapage, update maps, then invalidate
  2003. * the metapage.
  2004. */
  2005. mp = tlck->mp;
  2006. ASSERT(mp->xflag & COMMIT_PAGE);
  2007. grab_metapage(mp);
  2008. }
  2009. /*
  2010. * extent list:
  2011. * . in-line PXD list:
  2012. * . out-of-line XAD list:
  2013. */
  2014. maplock = (struct maplock *) & tlck->lock;
  2015. nlock = maplock->index;
  2016. for (k = 0; k < nlock; k++, maplock++) {
  2017. /*
  2018. * allocate blocks in persistent map:
  2019. *
  2020. * blocks have been allocated from wmap at alloc time;
  2021. */
  2022. if (maplock->flag & mlckALLOC) {
  2023. txAllocPMap(ipimap, maplock, tblk);
  2024. }
  2025. /*
  2026. * free blocks in persistent and working map:
  2027. * blocks will be freed in pmap and then in wmap;
  2028. *
  2029. * ? tblock specifies the PMAP/PWMAP based upon
  2030. * transaction
  2031. *
  2032. * free blocks in persistent map:
  2033. * blocks will be freed from wmap at last reference
  2034. * release of the object for regular files;
  2035. *
  2036. * Alway free blocks from both persistent & working
  2037. * maps for directories
  2038. */
  2039. else { /* (maplock->flag & mlckFREE) */
  2040. if (tlck->flag & tlckDIRECTORY)
  2041. txFreeMap(ipimap, maplock,
  2042. tblk, COMMIT_PWMAP);
  2043. else
  2044. txFreeMap(ipimap, maplock,
  2045. tblk, maptype);
  2046. }
  2047. }
  2048. if (tlck->flag & tlckFREEPAGE) {
  2049. if (!(tblk->flag & tblkGC_LAZY)) {
  2050. /* This is equivalent to txRelease */
  2051. ASSERT(mp->lid == lid);
  2052. tlck->mp->lid = 0;
  2053. }
  2054. assert(mp->nohomeok == 1);
  2055. metapage_homeok(mp);
  2056. discard_metapage(mp);
  2057. tlck->mp = NULL;
  2058. }
  2059. }
  2060. /*
  2061. * update inode allocation map
  2062. *
  2063. * update allocation state in pmap and
  2064. * update lsn of the pmap page;
  2065. * update in-memory inode flag/state
  2066. *
  2067. * unlock mapper/write lock
  2068. */
  2069. if (tblk->xflag & COMMIT_CREATE) {
  2070. diUpdatePMap(ipimap, tblk->ino, false, tblk);
  2071. /* update persistent block allocation map
  2072. * for the allocation of inode extent;
  2073. */
  2074. pxdlock.flag = mlckALLOCPXD;
  2075. pxdlock.pxd = tblk->u.ixpxd;
  2076. pxdlock.index = 1;
  2077. txAllocPMap(ipimap, (struct maplock *) & pxdlock, tblk);
  2078. } else if (tblk->xflag & COMMIT_DELETE) {
  2079. ip = tblk->u.ip;
  2080. diUpdatePMap(ipimap, ip->i_ino, true, tblk);
  2081. iput(ip);
  2082. }
  2083. }
  2084. /*
  2085. * txAllocPMap()
  2086. *
  2087. * function: allocate from persistent map;
  2088. *
  2089. * parameter:
  2090. * ipbmap -
  2091. * malock -
  2092. * xad list:
  2093. * pxd:
  2094. *
  2095. * maptype -
  2096. * allocate from persistent map;
  2097. * free from persistent map;
  2098. * (e.g., tmp file - free from working map at releae
  2099. * of last reference);
  2100. * free from persistent and working map;
  2101. *
  2102. * lsn - log sequence number;
  2103. */
  2104. static void txAllocPMap(struct inode *ip, struct maplock * maplock,
  2105. struct tblock * tblk)
  2106. {
  2107. struct inode *ipbmap = JFS_SBI(ip->i_sb)->ipbmap;
  2108. struct xdlistlock *xadlistlock;
  2109. xad_t *xad;
  2110. s64 xaddr;
  2111. int xlen;
  2112. struct pxd_lock *pxdlock;
  2113. struct xdlistlock *pxdlistlock;
  2114. pxd_t *pxd;
  2115. int n;
  2116. /*
  2117. * allocate from persistent map;
  2118. */
  2119. if (maplock->flag & mlckALLOCXADLIST) {
  2120. xadlistlock = (struct xdlistlock *) maplock;
  2121. xad = xadlistlock->xdlist;
  2122. for (n = 0; n < xadlistlock->count; n++, xad++) {
  2123. if (xad->flag & (XAD_NEW | XAD_EXTENDED)) {
  2124. xaddr = addressXAD(xad);
  2125. xlen = lengthXAD(xad);
  2126. dbUpdatePMap(ipbmap, false, xaddr,
  2127. (s64) xlen, tblk);
  2128. xad->flag &= ~(XAD_NEW | XAD_EXTENDED);
  2129. jfs_info("allocPMap: xaddr:0x%lx xlen:%d",
  2130. (ulong) xaddr, xlen);
  2131. }
  2132. }
  2133. } else if (maplock->flag & mlckALLOCPXD) {
  2134. pxdlock = (struct pxd_lock *) maplock;
  2135. xaddr = addressPXD(&pxdlock->pxd);
  2136. xlen = lengthPXD(&pxdlock->pxd);
  2137. dbUpdatePMap(ipbmap, false, xaddr, (s64) xlen, tblk);
  2138. jfs_info("allocPMap: xaddr:0x%lx xlen:%d", (ulong) xaddr, xlen);
  2139. } else { /* (maplock->flag & mlckALLOCPXDLIST) */
  2140. pxdlistlock = (struct xdlistlock *) maplock;
  2141. pxd = pxdlistlock->xdlist;
  2142. for (n = 0; n < pxdlistlock->count; n++, pxd++) {
  2143. xaddr = addressPXD(pxd);
  2144. xlen = lengthPXD(pxd);
  2145. dbUpdatePMap(ipbmap, false, xaddr, (s64) xlen,
  2146. tblk);
  2147. jfs_info("allocPMap: xaddr:0x%lx xlen:%d",
  2148. (ulong) xaddr, xlen);
  2149. }
  2150. }
  2151. }
  2152. /*
  2153. * txFreeMap()
  2154. *
  2155. * function: free from persistent and/or working map;
  2156. *
  2157. * todo: optimization
  2158. */
  2159. void txFreeMap(struct inode *ip,
  2160. struct maplock * maplock, struct tblock * tblk, int maptype)
  2161. {
  2162. struct inode *ipbmap = JFS_SBI(ip->i_sb)->ipbmap;
  2163. struct xdlistlock *xadlistlock;
  2164. xad_t *xad;
  2165. s64 xaddr;
  2166. int xlen;
  2167. struct pxd_lock *pxdlock;
  2168. struct xdlistlock *pxdlistlock;
  2169. pxd_t *pxd;
  2170. int n;
  2171. jfs_info("txFreeMap: tblk:0x%p maplock:0x%p maptype:0x%x",
  2172. tblk, maplock, maptype);
  2173. /*
  2174. * free from persistent map;
  2175. */
  2176. if (maptype == COMMIT_PMAP || maptype == COMMIT_PWMAP) {
  2177. if (maplock->flag & mlckFREEXADLIST) {
  2178. xadlistlock = (struct xdlistlock *) maplock;
  2179. xad = xadlistlock->xdlist;
  2180. for (n = 0; n < xadlistlock->count; n++, xad++) {
  2181. if (!(xad->flag & XAD_NEW)) {
  2182. xaddr = addressXAD(xad);
  2183. xlen = lengthXAD(xad);
  2184. dbUpdatePMap(ipbmap, true, xaddr,
  2185. (s64) xlen, tblk);
  2186. jfs_info("freePMap: xaddr:0x%lx xlen:%d",
  2187. (ulong) xaddr, xlen);
  2188. }
  2189. }
  2190. } else if (maplock->flag & mlckFREEPXD) {
  2191. pxdlock = (struct pxd_lock *) maplock;
  2192. xaddr = addressPXD(&pxdlock->pxd);
  2193. xlen = lengthPXD(&pxdlock->pxd);
  2194. dbUpdatePMap(ipbmap, true, xaddr, (s64) xlen,
  2195. tblk);
  2196. jfs_info("freePMap: xaddr:0x%lx xlen:%d",
  2197. (ulong) xaddr, xlen);
  2198. } else { /* (maplock->flag & mlckALLOCPXDLIST) */
  2199. pxdlistlock = (struct xdlistlock *) maplock;
  2200. pxd = pxdlistlock->xdlist;
  2201. for (n = 0; n < pxdlistlock->count; n++, pxd++) {
  2202. xaddr = addressPXD(pxd);
  2203. xlen = lengthPXD(pxd);
  2204. dbUpdatePMap(ipbmap, true, xaddr,
  2205. (s64) xlen, tblk);
  2206. jfs_info("freePMap: xaddr:0x%lx xlen:%d",
  2207. (ulong) xaddr, xlen);
  2208. }
  2209. }
  2210. }
  2211. /*
  2212. * free from working map;
  2213. */
  2214. if (maptype == COMMIT_PWMAP || maptype == COMMIT_WMAP) {
  2215. if (maplock->flag & mlckFREEXADLIST) {
  2216. xadlistlock = (struct xdlistlock *) maplock;
  2217. xad = xadlistlock->xdlist;
  2218. for (n = 0; n < xadlistlock->count; n++, xad++) {
  2219. xaddr = addressXAD(xad);
  2220. xlen = lengthXAD(xad);
  2221. dbFree(ip, xaddr, (s64) xlen);
  2222. xad->flag = 0;
  2223. jfs_info("freeWMap: xaddr:0x%lx xlen:%d",
  2224. (ulong) xaddr, xlen);
  2225. }
  2226. } else if (maplock->flag & mlckFREEPXD) {
  2227. pxdlock = (struct pxd_lock *) maplock;
  2228. xaddr = addressPXD(&pxdlock->pxd);
  2229. xlen = lengthPXD(&pxdlock->pxd);
  2230. dbFree(ip, xaddr, (s64) xlen);
  2231. jfs_info("freeWMap: xaddr:0x%lx xlen:%d",
  2232. (ulong) xaddr, xlen);
  2233. } else { /* (maplock->flag & mlckFREEPXDLIST) */
  2234. pxdlistlock = (struct xdlistlock *) maplock;
  2235. pxd = pxdlistlock->xdlist;
  2236. for (n = 0; n < pxdlistlock->count; n++, pxd++) {
  2237. xaddr = addressPXD(pxd);
  2238. xlen = lengthPXD(pxd);
  2239. dbFree(ip, xaddr, (s64) xlen);
  2240. jfs_info("freeWMap: xaddr:0x%lx xlen:%d",
  2241. (ulong) xaddr, xlen);
  2242. }
  2243. }
  2244. }
  2245. }
  2246. /*
  2247. * txFreelock()
  2248. *
  2249. * function: remove tlock from inode anonymous locklist
  2250. */
  2251. void txFreelock(struct inode *ip)
  2252. {
  2253. struct jfs_inode_info *jfs_ip = JFS_IP(ip);
  2254. struct tlock *xtlck, *tlck;
  2255. lid_t xlid = 0, lid;
  2256. if (!jfs_ip->atlhead)
  2257. return;
  2258. TXN_LOCK();
  2259. xtlck = (struct tlock *) &jfs_ip->atlhead;
  2260. while ((lid = xtlck->next) != 0) {
  2261. tlck = lid_to_tlock(lid);
  2262. if (tlck->flag & tlckFREELOCK) {
  2263. xtlck->next = tlck->next;
  2264. txLockFree(lid);
  2265. } else {
  2266. xtlck = tlck;
  2267. xlid = lid;
  2268. }
  2269. }
  2270. if (jfs_ip->atlhead)
  2271. jfs_ip->atltail = xlid;
  2272. else {
  2273. jfs_ip->atltail = 0;
  2274. /*
  2275. * If inode was on anon_list, remove it
  2276. */
  2277. list_del_init(&jfs_ip->anon_inode_list);
  2278. }
  2279. TXN_UNLOCK();
  2280. }
  2281. /*
  2282. * txAbort()
  2283. *
  2284. * function: abort tx before commit;
  2285. *
  2286. * frees line-locks and segment locks for all
  2287. * segments in comdata structure.
  2288. * Optionally sets state of file-system to FM_DIRTY in super-block.
  2289. * log age of page-frames in memory for which caller has
  2290. * are reset to 0 (to avoid logwarap).
  2291. */
  2292. void txAbort(tid_t tid, int dirty)
  2293. {
  2294. lid_t lid, next;
  2295. struct metapage *mp;
  2296. struct tblock *tblk = tid_to_tblock(tid);
  2297. struct tlock *tlck;
  2298. /*
  2299. * free tlocks of the transaction
  2300. */
  2301. for (lid = tblk->next; lid; lid = next) {
  2302. tlck = lid_to_tlock(lid);
  2303. next = tlck->next;
  2304. mp = tlck->mp;
  2305. JFS_IP(tlck->ip)->xtlid = 0;
  2306. if (mp) {
  2307. mp->lid = 0;
  2308. /*
  2309. * reset lsn of page to avoid logwarap:
  2310. *
  2311. * (page may have been previously committed by another
  2312. * transaction(s) but has not been paged, i.e.,
  2313. * it may be on logsync list even though it has not
  2314. * been logged for the current tx.)
  2315. */
  2316. if (mp->xflag & COMMIT_PAGE && mp->lsn)
  2317. LogSyncRelease(mp);
  2318. }
  2319. /* insert tlock at head of freelist */
  2320. TXN_LOCK();
  2321. txLockFree(lid);
  2322. TXN_UNLOCK();
  2323. }
  2324. /* caller will free the transaction block */
  2325. tblk->next = tblk->last = 0;
  2326. /*
  2327. * mark filesystem dirty
  2328. */
  2329. if (dirty)
  2330. jfs_error(tblk->sb, "\n");
  2331. return;
  2332. }
  2333. /*
  2334. * txLazyCommit(void)
  2335. *
  2336. * All transactions except those changing ipimap (COMMIT_FORCE) are
  2337. * processed by this routine. This insures that the inode and block
  2338. * allocation maps are updated in order. For synchronous transactions,
  2339. * let the user thread finish processing after txUpdateMap() is called.
  2340. */
  2341. static void txLazyCommit(struct tblock * tblk)
  2342. {
  2343. struct jfs_log *log;
  2344. while (((tblk->flag & tblkGC_READY) == 0) &&
  2345. ((tblk->flag & tblkGC_UNLOCKED) == 0)) {
  2346. /* We must have gotten ahead of the user thread
  2347. */
  2348. jfs_info("jfs_lazycommit: tblk 0x%p not unlocked", tblk);
  2349. yield();
  2350. }
  2351. jfs_info("txLazyCommit: processing tblk 0x%p", tblk);
  2352. txUpdateMap(tblk);
  2353. log = (struct jfs_log *) JFS_SBI(tblk->sb)->log;
  2354. spin_lock_irq(&log->gclock); // LOGGC_LOCK
  2355. tblk->flag |= tblkGC_COMMITTED;
  2356. if (tblk->flag & tblkGC_READY)
  2357. log->gcrtc--;
  2358. wake_up_all(&tblk->gcwait); // LOGGC_WAKEUP
  2359. /*
  2360. * Can't release log->gclock until we've tested tblk->flag
  2361. */
  2362. if (tblk->flag & tblkGC_LAZY) {
  2363. spin_unlock_irq(&log->gclock); // LOGGC_UNLOCK
  2364. txUnlock(tblk);
  2365. tblk->flag &= ~tblkGC_LAZY;
  2366. txEnd(tblk - TxBlock); /* Convert back to tid */
  2367. } else
  2368. spin_unlock_irq(&log->gclock); // LOGGC_UNLOCK
  2369. jfs_info("txLazyCommit: done: tblk = 0x%p", tblk);
  2370. }
  2371. /*
  2372. * jfs_lazycommit(void)
  2373. *
  2374. * To be run as a kernel daemon. If lbmIODone is called in an interrupt
  2375. * context, or where blocking is not wanted, this routine will process
  2376. * committed transactions from the unlock queue.
  2377. */
  2378. int jfs_lazycommit(void *arg)
  2379. {
  2380. int WorkDone;
  2381. struct tblock *tblk;
  2382. unsigned long flags;
  2383. struct jfs_sb_info *sbi;
  2384. do {
  2385. LAZY_LOCK(flags);
  2386. jfs_commit_thread_waking = 0; /* OK to wake another thread */
  2387. while (!list_empty(&TxAnchor.unlock_queue)) {
  2388. WorkDone = 0;
  2389. list_for_each_entry(tblk, &TxAnchor.unlock_queue,
  2390. cqueue) {
  2391. sbi = JFS_SBI(tblk->sb);
  2392. /*
  2393. * For each volume, the transactions must be
  2394. * handled in order. If another commit thread
  2395. * is handling a tblk for this superblock,
  2396. * skip it
  2397. */
  2398. if (sbi->commit_state & IN_LAZYCOMMIT)
  2399. continue;
  2400. sbi->commit_state |= IN_LAZYCOMMIT;
  2401. WorkDone = 1;
  2402. /*
  2403. * Remove transaction from queue
  2404. */
  2405. list_del(&tblk->cqueue);
  2406. LAZY_UNLOCK(flags);
  2407. txLazyCommit(tblk);
  2408. LAZY_LOCK(flags);
  2409. sbi->commit_state &= ~IN_LAZYCOMMIT;
  2410. /*
  2411. * Don't continue in the for loop. (We can't
  2412. * anyway, it's unsafe!) We want to go back to
  2413. * the beginning of the list.
  2414. */
  2415. break;
  2416. }
  2417. /* If there was nothing to do, don't continue */
  2418. if (!WorkDone)
  2419. break;
  2420. }
  2421. /* In case a wakeup came while all threads were active */
  2422. jfs_commit_thread_waking = 0;
  2423. if (freezing(current)) {
  2424. LAZY_UNLOCK(flags);
  2425. try_to_freeze();
  2426. } else {
  2427. DECLARE_WAITQUEUE(wq, current);
  2428. add_wait_queue(&jfs_commit_thread_wait, &wq);
  2429. set_current_state(TASK_INTERRUPTIBLE);
  2430. LAZY_UNLOCK(flags);
  2431. schedule();
  2432. remove_wait_queue(&jfs_commit_thread_wait, &wq);
  2433. }
  2434. } while (!kthread_should_stop());
  2435. if (!list_empty(&TxAnchor.unlock_queue))
  2436. jfs_err("jfs_lazycommit being killed w/pending transactions!");
  2437. else
  2438. jfs_info("jfs_lazycommit being killed");
  2439. return 0;
  2440. }
  2441. void txLazyUnlock(struct tblock * tblk)
  2442. {
  2443. unsigned long flags;
  2444. LAZY_LOCK(flags);
  2445. list_add_tail(&tblk->cqueue, &TxAnchor.unlock_queue);
  2446. /*
  2447. * Don't wake up a commit thread if there is already one servicing
  2448. * this superblock, or if the last one we woke up hasn't started yet.
  2449. */
  2450. if (!(JFS_SBI(tblk->sb)->commit_state & IN_LAZYCOMMIT) &&
  2451. !jfs_commit_thread_waking) {
  2452. jfs_commit_thread_waking = 1;
  2453. wake_up(&jfs_commit_thread_wait);
  2454. }
  2455. LAZY_UNLOCK(flags);
  2456. }
  2457. static void LogSyncRelease(struct metapage * mp)
  2458. {
  2459. struct jfs_log *log = mp->log;
  2460. assert(mp->nohomeok);
  2461. assert(log);
  2462. metapage_homeok(mp);
  2463. }
  2464. /*
  2465. * txQuiesce
  2466. *
  2467. * Block all new transactions and push anonymous transactions to
  2468. * completion
  2469. *
  2470. * This does almost the same thing as jfs_sync below. We don't
  2471. * worry about deadlocking when jfs_tlocks_low is set, since we would
  2472. * expect jfs_sync to get us out of that jam.
  2473. */
  2474. void txQuiesce(struct super_block *sb)
  2475. {
  2476. struct inode *ip;
  2477. struct jfs_inode_info *jfs_ip;
  2478. struct jfs_log *log = JFS_SBI(sb)->log;
  2479. tid_t tid;
  2480. set_bit(log_QUIESCE, &log->flag);
  2481. TXN_LOCK();
  2482. restart:
  2483. while (!list_empty(&TxAnchor.anon_list)) {
  2484. jfs_ip = list_entry(TxAnchor.anon_list.next,
  2485. struct jfs_inode_info,
  2486. anon_inode_list);
  2487. ip = &jfs_ip->vfs_inode;
  2488. /*
  2489. * inode will be removed from anonymous list
  2490. * when it is committed
  2491. */
  2492. TXN_UNLOCK();
  2493. tid = txBegin(ip->i_sb, COMMIT_INODE | COMMIT_FORCE);
  2494. mutex_lock(&jfs_ip->commit_mutex);
  2495. txCommit(tid, 1, &ip, 0);
  2496. txEnd(tid);
  2497. mutex_unlock(&jfs_ip->commit_mutex);
  2498. /*
  2499. * Just to be safe. I don't know how
  2500. * long we can run without blocking
  2501. */
  2502. cond_resched();
  2503. TXN_LOCK();
  2504. }
  2505. /*
  2506. * If jfs_sync is running in parallel, there could be some inodes
  2507. * on anon_list2. Let's check.
  2508. */
  2509. if (!list_empty(&TxAnchor.anon_list2)) {
  2510. list_splice_init(&TxAnchor.anon_list2, &TxAnchor.anon_list);
  2511. goto restart;
  2512. }
  2513. TXN_UNLOCK();
  2514. /*
  2515. * We may need to kick off the group commit
  2516. */
  2517. jfs_flush_journal(log, 0);
  2518. }
  2519. /*
  2520. * txResume()
  2521. *
  2522. * Allows transactions to start again following txQuiesce
  2523. */
  2524. void txResume(struct super_block *sb)
  2525. {
  2526. struct jfs_log *log = JFS_SBI(sb)->log;
  2527. clear_bit(log_QUIESCE, &log->flag);
  2528. TXN_WAKEUP(&log->syncwait);
  2529. }
  2530. /*
  2531. * jfs_sync(void)
  2532. *
  2533. * To be run as a kernel daemon. This is awakened when tlocks run low.
  2534. * We write any inodes that have anonymous tlocks so they will become
  2535. * available.
  2536. */
  2537. int jfs_sync(void *arg)
  2538. {
  2539. struct inode *ip;
  2540. struct jfs_inode_info *jfs_ip;
  2541. tid_t tid;
  2542. do {
  2543. /*
  2544. * write each inode on the anonymous inode list
  2545. */
  2546. TXN_LOCK();
  2547. while (jfs_tlocks_low && !list_empty(&TxAnchor.anon_list)) {
  2548. jfs_ip = list_entry(TxAnchor.anon_list.next,
  2549. struct jfs_inode_info,
  2550. anon_inode_list);
  2551. ip = &jfs_ip->vfs_inode;
  2552. if (! igrab(ip)) {
  2553. /*
  2554. * Inode is being freed
  2555. */
  2556. list_del_init(&jfs_ip->anon_inode_list);
  2557. } else if (mutex_trylock(&jfs_ip->commit_mutex)) {
  2558. /*
  2559. * inode will be removed from anonymous list
  2560. * when it is committed
  2561. */
  2562. TXN_UNLOCK();
  2563. tid = txBegin(ip->i_sb, COMMIT_INODE);
  2564. txCommit(tid, 1, &ip, 0);
  2565. txEnd(tid);
  2566. mutex_unlock(&jfs_ip->commit_mutex);
  2567. iput(ip);
  2568. /*
  2569. * Just to be safe. I don't know how
  2570. * long we can run without blocking
  2571. */
  2572. cond_resched();
  2573. TXN_LOCK();
  2574. } else {
  2575. /* We can't get the commit mutex. It may
  2576. * be held by a thread waiting for tlock's
  2577. * so let's not block here. Save it to
  2578. * put back on the anon_list.
  2579. */
  2580. /* Move from anon_list to anon_list2 */
  2581. list_move(&jfs_ip->anon_inode_list,
  2582. &TxAnchor.anon_list2);
  2583. TXN_UNLOCK();
  2584. iput(ip);
  2585. TXN_LOCK();
  2586. }
  2587. }
  2588. /* Add anon_list2 back to anon_list */
  2589. list_splice_init(&TxAnchor.anon_list2, &TxAnchor.anon_list);
  2590. if (freezing(current)) {
  2591. TXN_UNLOCK();
  2592. try_to_freeze();
  2593. } else {
  2594. set_current_state(TASK_INTERRUPTIBLE);
  2595. TXN_UNLOCK();
  2596. schedule();
  2597. }
  2598. } while (!kthread_should_stop());
  2599. jfs_info("jfs_sync being killed");
  2600. return 0;
  2601. }
  2602. #if defined(CONFIG_PROC_FS) && defined(CONFIG_JFS_DEBUG)
  2603. int jfs_txanchor_proc_show(struct seq_file *m, void *v)
  2604. {
  2605. char *freewait;
  2606. char *freelockwait;
  2607. char *lowlockwait;
  2608. freewait =
  2609. waitqueue_active(&TxAnchor.freewait) ? "active" : "empty";
  2610. freelockwait =
  2611. waitqueue_active(&TxAnchor.freelockwait) ? "active" : "empty";
  2612. lowlockwait =
  2613. waitqueue_active(&TxAnchor.lowlockwait) ? "active" : "empty";
  2614. seq_printf(m,
  2615. "JFS TxAnchor\n"
  2616. "============\n"
  2617. "freetid = %d\n"
  2618. "freewait = %s\n"
  2619. "freelock = %d\n"
  2620. "freelockwait = %s\n"
  2621. "lowlockwait = %s\n"
  2622. "tlocksInUse = %d\n"
  2623. "jfs_tlocks_low = %d\n"
  2624. "unlock_queue is %sempty\n",
  2625. TxAnchor.freetid,
  2626. freewait,
  2627. TxAnchor.freelock,
  2628. freelockwait,
  2629. lowlockwait,
  2630. TxAnchor.tlocksInUse,
  2631. jfs_tlocks_low,
  2632. list_empty(&TxAnchor.unlock_queue) ? "" : "not ");
  2633. return 0;
  2634. }
  2635. #endif
  2636. #if defined(CONFIG_PROC_FS) && defined(CONFIG_JFS_STATISTICS)
  2637. int jfs_txstats_proc_show(struct seq_file *m, void *v)
  2638. {
  2639. seq_printf(m,
  2640. "JFS TxStats\n"
  2641. "===========\n"
  2642. "calls to txBegin = %d\n"
  2643. "txBegin blocked by sync barrier = %d\n"
  2644. "txBegin blocked by tlocks low = %d\n"
  2645. "txBegin blocked by no free tid = %d\n"
  2646. "calls to txBeginAnon = %d\n"
  2647. "txBeginAnon blocked by sync barrier = %d\n"
  2648. "txBeginAnon blocked by tlocks low = %d\n"
  2649. "calls to txLockAlloc = %d\n"
  2650. "tLockAlloc blocked by no free lock = %d\n",
  2651. TxStat.txBegin,
  2652. TxStat.txBegin_barrier,
  2653. TxStat.txBegin_lockslow,
  2654. TxStat.txBegin_freetid,
  2655. TxStat.txBeginAnon,
  2656. TxStat.txBeginAnon_barrier,
  2657. TxStat.txBeginAnon_lockslow,
  2658. TxStat.txLockAlloc,
  2659. TxStat.txLockAlloc_freelock);
  2660. return 0;
  2661. }
  2662. #endif