jfs_txnmgr.c 76 KB

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