jfs_txnmgr.c 76 KB

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