jfs_txnmgr.c 76 KB

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