jfs_txnmgr.c 75 KB

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