jfs_txnmgr.c 75 KB

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