jfs_txnmgr.c 76 KB

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