jfs_txnmgr.c 76 KB

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