jfs_txnmgr.c 75 KB

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