jfs_txnmgr.c 76 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097
  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/freezer.h>
  48. #include <linux/module.h>
  49. #include <linux/moduleparam.h>
  50. #include <linux/kthread.h>
  51. #include "jfs_incore.h"
  52. #include "jfs_inode.h"
  53. #include "jfs_filsys.h"
  54. #include "jfs_metapage.h"
  55. #include "jfs_dinode.h"
  56. #include "jfs_imap.h"
  57. #include "jfs_dmap.h"
  58. #include "jfs_superblock.h"
  59. #include "jfs_debug.h"
  60. /*
  61. * transaction management structures
  62. */
  63. static struct {
  64. int freetid; /* index of a free tid structure */
  65. int freelock; /* index first free lock word */
  66. wait_queue_head_t freewait; /* eventlist of free tblock */
  67. wait_queue_head_t freelockwait; /* eventlist of free tlock */
  68. wait_queue_head_t lowlockwait; /* eventlist of ample tlocks */
  69. int tlocksInUse; /* Number of tlocks in use */
  70. spinlock_t LazyLock; /* synchronize sync_queue & unlock_queue */
  71. /* struct tblock *sync_queue; * Transactions waiting for data sync */
  72. struct list_head unlock_queue; /* Txns waiting to be released */
  73. struct list_head anon_list; /* inodes having anonymous txns */
  74. struct list_head anon_list2; /* inodes having anonymous txns
  75. that couldn't be sync'ed */
  76. } TxAnchor;
  77. int jfs_tlocks_low; /* Indicates low number of available tlocks */
  78. #ifdef CONFIG_JFS_STATISTICS
  79. static struct {
  80. uint txBegin;
  81. uint txBegin_barrier;
  82. uint txBegin_lockslow;
  83. uint txBegin_freetid;
  84. uint txBeginAnon;
  85. uint txBeginAnon_barrier;
  86. uint txBeginAnon_lockslow;
  87. uint txLockAlloc;
  88. uint txLockAlloc_freelock;
  89. } TxStat;
  90. #endif
  91. static int nTxBlock = -1; /* number of transaction blocks */
  92. module_param(nTxBlock, int, 0);
  93. MODULE_PARM_DESC(nTxBlock,
  94. "Number of transaction blocks (max:65536)");
  95. static int nTxLock = -1; /* number of transaction locks */
  96. module_param(nTxLock, int, 0);
  97. MODULE_PARM_DESC(nTxLock,
  98. "Number of transaction locks (max:65536)");
  99. struct tblock *TxBlock; /* transaction block table */
  100. static int TxLockLWM; /* Low water mark for number of txLocks used */
  101. static int TxLockHWM; /* High water mark for number of txLocks used */
  102. static int TxLockVHWM; /* Very High water mark */
  103. struct tlock *TxLock; /* transaction lock table */
  104. /*
  105. * transaction management lock
  106. */
  107. static DEFINE_SPINLOCK(jfsTxnLock);
  108. #define TXN_LOCK() spin_lock(&jfsTxnLock)
  109. #define TXN_UNLOCK() spin_unlock(&jfsTxnLock)
  110. #define LAZY_LOCK_INIT() spin_lock_init(&TxAnchor.LazyLock);
  111. #define LAZY_LOCK(flags) spin_lock_irqsave(&TxAnchor.LazyLock, flags)
  112. #define LAZY_UNLOCK(flags) spin_unlock_irqrestore(&TxAnchor.LazyLock, flags)
  113. static DECLARE_WAIT_QUEUE_HEAD(jfs_commit_thread_wait);
  114. static int jfs_commit_thread_waking;
  115. /*
  116. * Retry logic exist outside these macros to protect from spurrious wakeups.
  117. */
  118. static inline void TXN_SLEEP_DROP_LOCK(wait_queue_head_t * event)
  119. {
  120. DECLARE_WAITQUEUE(wait, current);
  121. add_wait_queue(event, &wait);
  122. set_current_state(TASK_UNINTERRUPTIBLE);
  123. TXN_UNLOCK();
  124. io_schedule();
  125. 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_process(jfsSyncThread);
  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 = 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 = 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; /* reacquire 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_write_and_wait(ip->i_mapping);
  1071. */
  1072. /*
  1073. * Mark inode as not dirty. It will still be on the dirty
  1074. * inode list, but we'll know not to commit it again unless
  1075. * it gets marked dirty again
  1076. */
  1077. clear_cflag(COMMIT_Dirty, ip);
  1078. /* inherit anonymous tlock(s) of inode */
  1079. if (jfs_ip->atlhead) {
  1080. lid_to_tlock(jfs_ip->atltail)->next = tblk->next;
  1081. tblk->next = jfs_ip->atlhead;
  1082. if (!tblk->last)
  1083. tblk->last = jfs_ip->atltail;
  1084. jfs_ip->atlhead = jfs_ip->atltail = 0;
  1085. TXN_LOCK();
  1086. list_del_init(&jfs_ip->anon_inode_list);
  1087. TXN_UNLOCK();
  1088. }
  1089. /*
  1090. * acquire transaction lock on on-disk inode page
  1091. * (become first tlock of the tblk's tlock list)
  1092. */
  1093. if (((rc = diWrite(tid, ip))))
  1094. goto out;
  1095. }
  1096. /*
  1097. * write log records from transaction locks
  1098. *
  1099. * txUpdateMap() resets XAD_NEW in XAD.
  1100. */
  1101. if ((rc = txLog(log, tblk, &cd)))
  1102. goto TheEnd;
  1103. /*
  1104. * Ensure that inode isn't reused before
  1105. * lazy commit thread finishes processing
  1106. */
  1107. if (tblk->xflag & COMMIT_DELETE) {
  1108. atomic_inc(&tblk->u.ip->i_count);
  1109. /*
  1110. * Avoid a rare deadlock
  1111. *
  1112. * If the inode is locked, we may be blocked in
  1113. * jfs_commit_inode. If so, we don't want the
  1114. * lazy_commit thread doing the last iput() on the inode
  1115. * since that may block on the locked inode. Instead,
  1116. * commit the transaction synchronously, so the last iput
  1117. * will be done by the calling thread (or later)
  1118. */
  1119. if (tblk->u.ip->i_state & I_LOCK)
  1120. tblk->xflag &= ~COMMIT_LAZY;
  1121. }
  1122. ASSERT((!(tblk->xflag & COMMIT_DELETE)) ||
  1123. ((tblk->u.ip->i_nlink == 0) &&
  1124. !test_cflag(COMMIT_Nolink, tblk->u.ip)));
  1125. /*
  1126. * write COMMIT log record
  1127. */
  1128. lrd->type = cpu_to_le16(LOG_COMMIT);
  1129. lrd->length = 0;
  1130. lsn = lmLog(log, tblk, lrd, NULL);
  1131. lmGroupCommit(log, tblk);
  1132. /*
  1133. * - transaction is now committed -
  1134. */
  1135. /*
  1136. * force pages in careful update
  1137. * (imap addressing structure update)
  1138. */
  1139. if (flag & COMMIT_FORCE)
  1140. txForce(tblk);
  1141. /*
  1142. * update allocation map.
  1143. *
  1144. * update inode allocation map and inode:
  1145. * free pager lock on memory object of inode if any.
  1146. * update block allocation map.
  1147. *
  1148. * txUpdateMap() resets XAD_NEW in XAD.
  1149. */
  1150. if (tblk->xflag & COMMIT_FORCE)
  1151. txUpdateMap(tblk);
  1152. /*
  1153. * free transaction locks and pageout/free pages
  1154. */
  1155. txRelease(tblk);
  1156. if ((tblk->flag & tblkGC_LAZY) == 0)
  1157. txUnlock(tblk);
  1158. /*
  1159. * reset in-memory object state
  1160. */
  1161. for (k = 0; k < cd.nip; k++) {
  1162. ip = cd.iplist[k];
  1163. jfs_ip = JFS_IP(ip);
  1164. /*
  1165. * reset in-memory inode state
  1166. */
  1167. jfs_ip->bxflag = 0;
  1168. jfs_ip->blid = 0;
  1169. }
  1170. out:
  1171. if (rc != 0)
  1172. txAbort(tid, 1);
  1173. TheEnd:
  1174. jfs_info("txCommit: tid = %d, returning %d", tid, rc);
  1175. return rc;
  1176. }
  1177. /*
  1178. * NAME: txLog()
  1179. *
  1180. * FUNCTION: Writes AFTER log records for all lines modified
  1181. * by tid for segments specified by inodes in comdata.
  1182. * Code assumes only WRITELOCKS are recorded in lockwords.
  1183. *
  1184. * PARAMETERS:
  1185. *
  1186. * RETURN :
  1187. */
  1188. static int txLog(struct jfs_log * log, struct tblock * tblk, struct commit * cd)
  1189. {
  1190. int rc = 0;
  1191. struct inode *ip;
  1192. lid_t lid;
  1193. struct tlock *tlck;
  1194. struct lrd *lrd = &cd->lrd;
  1195. /*
  1196. * write log record(s) for each tlock of transaction,
  1197. */
  1198. for (lid = tblk->next; lid; lid = tlck->next) {
  1199. tlck = lid_to_tlock(lid);
  1200. tlck->flag |= tlckLOG;
  1201. /* initialize lrd common */
  1202. ip = tlck->ip;
  1203. lrd->aggregate = cpu_to_le32(JFS_SBI(ip->i_sb)->aggregate);
  1204. lrd->log.redopage.fileset = cpu_to_le32(JFS_IP(ip)->fileset);
  1205. lrd->log.redopage.inode = cpu_to_le32(ip->i_ino);
  1206. /* write log record of page from the tlock */
  1207. switch (tlck->type & tlckTYPE) {
  1208. case tlckXTREE:
  1209. xtLog(log, tblk, lrd, tlck);
  1210. break;
  1211. case tlckDTREE:
  1212. dtLog(log, tblk, lrd, tlck);
  1213. break;
  1214. case tlckINODE:
  1215. diLog(log, tblk, lrd, tlck, cd);
  1216. break;
  1217. case tlckMAP:
  1218. mapLog(log, tblk, lrd, tlck);
  1219. break;
  1220. case tlckDATA:
  1221. dataLog(log, tblk, lrd, tlck);
  1222. break;
  1223. default:
  1224. jfs_err("UFO tlock:0x%p", tlck);
  1225. }
  1226. }
  1227. return rc;
  1228. }
  1229. /*
  1230. * diLog()
  1231. *
  1232. * function: log inode tlock and format maplock to update bmap;
  1233. */
  1234. static int diLog(struct jfs_log * log, struct tblock * tblk, struct lrd * lrd,
  1235. struct tlock * tlck, struct commit * cd)
  1236. {
  1237. int rc = 0;
  1238. struct metapage *mp;
  1239. pxd_t *pxd;
  1240. struct pxd_lock *pxdlock;
  1241. mp = tlck->mp;
  1242. /* initialize as REDOPAGE record format */
  1243. lrd->log.redopage.type = cpu_to_le16(LOG_INODE);
  1244. lrd->log.redopage.l2linesize = cpu_to_le16(L2INODESLOTSIZE);
  1245. pxd = &lrd->log.redopage.pxd;
  1246. /*
  1247. * inode after image
  1248. */
  1249. if (tlck->type & tlckENTRY) {
  1250. /* log after-image for logredo(): */
  1251. lrd->type = cpu_to_le16(LOG_REDOPAGE);
  1252. PXDaddress(pxd, mp->index);
  1253. PXDlength(pxd,
  1254. mp->logical_size >> tblk->sb->s_blocksize_bits);
  1255. lrd->backchain = cpu_to_le32(lmLog(log, tblk, lrd, tlck));
  1256. /* mark page as homeward bound */
  1257. tlck->flag |= tlckWRITEPAGE;
  1258. } else if (tlck->type & tlckFREE) {
  1259. /*
  1260. * free inode extent
  1261. *
  1262. * (pages of the freed inode extent have been invalidated and
  1263. * a maplock for free of the extent has been formatted at
  1264. * txLock() time);
  1265. *
  1266. * the tlock had been acquired on the inode allocation map page
  1267. * (iag) that specifies the freed extent, even though the map
  1268. * page is not itself logged, to prevent pageout of the map
  1269. * page before the log;
  1270. */
  1271. /* log LOG_NOREDOINOEXT of the freed inode extent for
  1272. * logredo() to start NoRedoPage filters, and to update
  1273. * imap and bmap for free of the extent;
  1274. */
  1275. lrd->type = cpu_to_le16(LOG_NOREDOINOEXT);
  1276. /*
  1277. * For the LOG_NOREDOINOEXT record, we need
  1278. * to pass the IAG number and inode extent
  1279. * index (within that IAG) from which the
  1280. * the extent being released. These have been
  1281. * passed to us in the iplist[1] and iplist[2].
  1282. */
  1283. lrd->log.noredoinoext.iagnum =
  1284. cpu_to_le32((u32) (size_t) cd->iplist[1]);
  1285. lrd->log.noredoinoext.inoext_idx =
  1286. cpu_to_le32((u32) (size_t) cd->iplist[2]);
  1287. pxdlock = (struct pxd_lock *) & tlck->lock;
  1288. *pxd = pxdlock->pxd;
  1289. lrd->backchain = cpu_to_le32(lmLog(log, tblk, lrd, NULL));
  1290. /* update bmap */
  1291. tlck->flag |= tlckUPDATEMAP;
  1292. /* mark page as homeward bound */
  1293. tlck->flag |= tlckWRITEPAGE;
  1294. } else
  1295. jfs_err("diLog: UFO type tlck:0x%p", tlck);
  1296. #ifdef _JFS_WIP
  1297. /*
  1298. * alloc/free external EA extent
  1299. *
  1300. * a maplock for txUpdateMap() to update bPWMAP for alloc/free
  1301. * of the extent has been formatted at txLock() time;
  1302. */
  1303. else {
  1304. assert(tlck->type & tlckEA);
  1305. /* log LOG_UPDATEMAP for logredo() to update bmap for
  1306. * alloc of new (and free of old) external EA extent;
  1307. */
  1308. lrd->type = cpu_to_le16(LOG_UPDATEMAP);
  1309. pxdlock = (struct pxd_lock *) & tlck->lock;
  1310. nlock = pxdlock->index;
  1311. for (i = 0; i < nlock; i++, pxdlock++) {
  1312. if (pxdlock->flag & mlckALLOCPXD)
  1313. lrd->log.updatemap.type =
  1314. cpu_to_le16(LOG_ALLOCPXD);
  1315. else
  1316. lrd->log.updatemap.type =
  1317. cpu_to_le16(LOG_FREEPXD);
  1318. lrd->log.updatemap.nxd = cpu_to_le16(1);
  1319. lrd->log.updatemap.pxd = pxdlock->pxd;
  1320. lrd->backchain =
  1321. cpu_to_le32(lmLog(log, tblk, lrd, NULL));
  1322. }
  1323. /* update bmap */
  1324. tlck->flag |= tlckUPDATEMAP;
  1325. }
  1326. #endif /* _JFS_WIP */
  1327. return rc;
  1328. }
  1329. /*
  1330. * dataLog()
  1331. *
  1332. * function: log data tlock
  1333. */
  1334. static int dataLog(struct jfs_log * log, struct tblock * tblk, struct lrd * lrd,
  1335. struct tlock * tlck)
  1336. {
  1337. struct metapage *mp;
  1338. pxd_t *pxd;
  1339. mp = tlck->mp;
  1340. /* initialize as REDOPAGE record format */
  1341. lrd->log.redopage.type = cpu_to_le16(LOG_DATA);
  1342. lrd->log.redopage.l2linesize = cpu_to_le16(L2DATASLOTSIZE);
  1343. pxd = &lrd->log.redopage.pxd;
  1344. /* log after-image for logredo(): */
  1345. lrd->type = cpu_to_le16(LOG_REDOPAGE);
  1346. if (jfs_dirtable_inline(tlck->ip)) {
  1347. /*
  1348. * The table has been truncated, we've must have deleted
  1349. * the last entry, so don't bother logging this
  1350. */
  1351. mp->lid = 0;
  1352. grab_metapage(mp);
  1353. metapage_homeok(mp);
  1354. discard_metapage(mp);
  1355. tlck->mp = NULL;
  1356. return 0;
  1357. }
  1358. PXDaddress(pxd, mp->index);
  1359. PXDlength(pxd, mp->logical_size >> tblk->sb->s_blocksize_bits);
  1360. lrd->backchain = cpu_to_le32(lmLog(log, tblk, lrd, tlck));
  1361. /* mark page as homeward bound */
  1362. tlck->flag |= tlckWRITEPAGE;
  1363. return 0;
  1364. }
  1365. /*
  1366. * dtLog()
  1367. *
  1368. * function: log dtree tlock and format maplock to update bmap;
  1369. */
  1370. static void dtLog(struct jfs_log * log, struct tblock * tblk, struct lrd * lrd,
  1371. struct tlock * tlck)
  1372. {
  1373. struct metapage *mp;
  1374. struct pxd_lock *pxdlock;
  1375. pxd_t *pxd;
  1376. mp = tlck->mp;
  1377. /* initialize as REDOPAGE/NOREDOPAGE record format */
  1378. lrd->log.redopage.type = cpu_to_le16(LOG_DTREE);
  1379. lrd->log.redopage.l2linesize = cpu_to_le16(L2DTSLOTSIZE);
  1380. pxd = &lrd->log.redopage.pxd;
  1381. if (tlck->type & tlckBTROOT)
  1382. lrd->log.redopage.type |= cpu_to_le16(LOG_BTROOT);
  1383. /*
  1384. * page extension via relocation: entry insertion;
  1385. * page extension in-place: entry insertion;
  1386. * new right page from page split, reinitialized in-line
  1387. * root from root page split: entry insertion;
  1388. */
  1389. if (tlck->type & (tlckNEW | tlckEXTEND)) {
  1390. /* log after-image of the new page for logredo():
  1391. * mark log (LOG_NEW) for logredo() to initialize
  1392. * freelist and update bmap for alloc of the new page;
  1393. */
  1394. lrd->type = cpu_to_le16(LOG_REDOPAGE);
  1395. if (tlck->type & tlckEXTEND)
  1396. lrd->log.redopage.type |= cpu_to_le16(LOG_EXTEND);
  1397. else
  1398. lrd->log.redopage.type |= cpu_to_le16(LOG_NEW);
  1399. PXDaddress(pxd, mp->index);
  1400. PXDlength(pxd,
  1401. mp->logical_size >> tblk->sb->s_blocksize_bits);
  1402. lrd->backchain = cpu_to_le32(lmLog(log, tblk, lrd, tlck));
  1403. /* format a maplock for txUpdateMap() to update bPMAP for
  1404. * alloc of the new page;
  1405. */
  1406. if (tlck->type & tlckBTROOT)
  1407. return;
  1408. tlck->flag |= tlckUPDATEMAP;
  1409. pxdlock = (struct pxd_lock *) & tlck->lock;
  1410. pxdlock->flag = mlckALLOCPXD;
  1411. pxdlock->pxd = *pxd;
  1412. pxdlock->index = 1;
  1413. /* mark page as homeward bound */
  1414. tlck->flag |= tlckWRITEPAGE;
  1415. return;
  1416. }
  1417. /*
  1418. * entry insertion/deletion,
  1419. * sibling page link update (old right page before split);
  1420. */
  1421. if (tlck->type & (tlckENTRY | tlckRELINK)) {
  1422. /* log after-image for logredo(): */
  1423. lrd->type = cpu_to_le16(LOG_REDOPAGE);
  1424. PXDaddress(pxd, mp->index);
  1425. PXDlength(pxd,
  1426. mp->logical_size >> tblk->sb->s_blocksize_bits);
  1427. lrd->backchain = cpu_to_le32(lmLog(log, tblk, lrd, tlck));
  1428. /* mark page as homeward bound */
  1429. tlck->flag |= tlckWRITEPAGE;
  1430. return;
  1431. }
  1432. /*
  1433. * page deletion: page has been invalidated
  1434. * page relocation: source extent
  1435. *
  1436. * a maplock for free of the page has been formatted
  1437. * at txLock() time);
  1438. */
  1439. if (tlck->type & (tlckFREE | tlckRELOCATE)) {
  1440. /* log LOG_NOREDOPAGE of the deleted page for logredo()
  1441. * to start NoRedoPage filter and to update bmap for free
  1442. * of the deletd page
  1443. */
  1444. lrd->type = cpu_to_le16(LOG_NOREDOPAGE);
  1445. pxdlock = (struct pxd_lock *) & tlck->lock;
  1446. *pxd = pxdlock->pxd;
  1447. lrd->backchain = cpu_to_le32(lmLog(log, tblk, lrd, NULL));
  1448. /* a maplock for txUpdateMap() for free of the page
  1449. * has been formatted at txLock() time;
  1450. */
  1451. tlck->flag |= tlckUPDATEMAP;
  1452. }
  1453. return;
  1454. }
  1455. /*
  1456. * xtLog()
  1457. *
  1458. * function: log xtree tlock and format maplock to update bmap;
  1459. */
  1460. static void xtLog(struct jfs_log * log, struct tblock * tblk, struct lrd * lrd,
  1461. struct tlock * tlck)
  1462. {
  1463. struct inode *ip;
  1464. struct metapage *mp;
  1465. xtpage_t *p;
  1466. struct xtlock *xtlck;
  1467. struct maplock *maplock;
  1468. struct xdlistlock *xadlock;
  1469. struct pxd_lock *pxdlock;
  1470. pxd_t *page_pxd;
  1471. int next, lwm, hwm;
  1472. ip = tlck->ip;
  1473. mp = tlck->mp;
  1474. /* initialize as REDOPAGE/NOREDOPAGE record format */
  1475. lrd->log.redopage.type = cpu_to_le16(LOG_XTREE);
  1476. lrd->log.redopage.l2linesize = cpu_to_le16(L2XTSLOTSIZE);
  1477. page_pxd = &lrd->log.redopage.pxd;
  1478. if (tlck->type & tlckBTROOT) {
  1479. lrd->log.redopage.type |= cpu_to_le16(LOG_BTROOT);
  1480. p = &JFS_IP(ip)->i_xtroot;
  1481. if (S_ISDIR(ip->i_mode))
  1482. lrd->log.redopage.type |=
  1483. cpu_to_le16(LOG_DIR_XTREE);
  1484. } else
  1485. p = (xtpage_t *) mp->data;
  1486. next = le16_to_cpu(p->header.nextindex);
  1487. xtlck = (struct xtlock *) & tlck->lock;
  1488. maplock = (struct maplock *) & tlck->lock;
  1489. xadlock = (struct xdlistlock *) maplock;
  1490. /*
  1491. * entry insertion/extension;
  1492. * sibling page link update (old right page before split);
  1493. */
  1494. if (tlck->type & (tlckNEW | tlckGROW | tlckRELINK)) {
  1495. /* log after-image for logredo():
  1496. * logredo() will update bmap for alloc of new/extended
  1497. * extents (XAD_NEW|XAD_EXTEND) of XAD[lwm:next) from
  1498. * after-image of XADlist;
  1499. * logredo() resets (XAD_NEW|XAD_EXTEND) flag when
  1500. * applying the after-image to the meta-data page.
  1501. */
  1502. lrd->type = cpu_to_le16(LOG_REDOPAGE);
  1503. PXDaddress(page_pxd, mp->index);
  1504. PXDlength(page_pxd,
  1505. mp->logical_size >> tblk->sb->s_blocksize_bits);
  1506. lrd->backchain = cpu_to_le32(lmLog(log, tblk, lrd, tlck));
  1507. /* format a maplock for txUpdateMap() to update bPMAP
  1508. * for alloc of new/extended extents of XAD[lwm:next)
  1509. * from the page itself;
  1510. * txUpdateMap() resets (XAD_NEW|XAD_EXTEND) flag.
  1511. */
  1512. lwm = xtlck->lwm.offset;
  1513. if (lwm == 0)
  1514. lwm = XTPAGEMAXSLOT;
  1515. if (lwm == next)
  1516. goto out;
  1517. if (lwm > next) {
  1518. jfs_err("xtLog: lwm > next\n");
  1519. goto out;
  1520. }
  1521. tlck->flag |= tlckUPDATEMAP;
  1522. xadlock->flag = mlckALLOCXADLIST;
  1523. xadlock->count = next - lwm;
  1524. if ((xadlock->count <= 4) && (tblk->xflag & COMMIT_LAZY)) {
  1525. int i;
  1526. pxd_t *pxd;
  1527. /*
  1528. * Lazy commit may allow xtree to be modified before
  1529. * txUpdateMap runs. Copy xad into linelock to
  1530. * preserve correct data.
  1531. *
  1532. * We can fit twice as may pxd's as xads in the lock
  1533. */
  1534. xadlock->flag = mlckALLOCPXDLIST;
  1535. pxd = xadlock->xdlist = &xtlck->pxdlock;
  1536. for (i = 0; i < xadlock->count; i++) {
  1537. PXDaddress(pxd, addressXAD(&p->xad[lwm + i]));
  1538. PXDlength(pxd, lengthXAD(&p->xad[lwm + i]));
  1539. p->xad[lwm + i].flag &=
  1540. ~(XAD_NEW | XAD_EXTENDED);
  1541. pxd++;
  1542. }
  1543. } else {
  1544. /*
  1545. * xdlist will point to into inode's xtree, ensure
  1546. * that transaction is not committed lazily.
  1547. */
  1548. xadlock->flag = mlckALLOCXADLIST;
  1549. xadlock->xdlist = &p->xad[lwm];
  1550. tblk->xflag &= ~COMMIT_LAZY;
  1551. }
  1552. jfs_info("xtLog: alloc ip:0x%p mp:0x%p tlck:0x%p lwm:%d "
  1553. "count:%d", tlck->ip, mp, tlck, lwm, xadlock->count);
  1554. maplock->index = 1;
  1555. out:
  1556. /* mark page as homeward bound */
  1557. tlck->flag |= tlckWRITEPAGE;
  1558. return;
  1559. }
  1560. /*
  1561. * page deletion: file deletion/truncation (ref. xtTruncate())
  1562. *
  1563. * (page will be invalidated after log is written and bmap
  1564. * is updated from the page);
  1565. */
  1566. if (tlck->type & tlckFREE) {
  1567. /* LOG_NOREDOPAGE log for NoRedoPage filter:
  1568. * if page free from file delete, NoRedoFile filter from
  1569. * inode image of zero link count will subsume NoRedoPage
  1570. * filters for each page;
  1571. * if page free from file truncattion, write NoRedoPage
  1572. * filter;
  1573. *
  1574. * upadte of block allocation map for the page itself:
  1575. * if page free from deletion and truncation, LOG_UPDATEMAP
  1576. * log for the page itself is generated from processing
  1577. * its parent page xad entries;
  1578. */
  1579. /* if page free from file truncation, log LOG_NOREDOPAGE
  1580. * of the deleted page for logredo() to start NoRedoPage
  1581. * filter for the page;
  1582. */
  1583. if (tblk->xflag & COMMIT_TRUNCATE) {
  1584. /* write NOREDOPAGE for the page */
  1585. lrd->type = cpu_to_le16(LOG_NOREDOPAGE);
  1586. PXDaddress(page_pxd, mp->index);
  1587. PXDlength(page_pxd,
  1588. mp->logical_size >> tblk->sb->
  1589. s_blocksize_bits);
  1590. lrd->backchain =
  1591. cpu_to_le32(lmLog(log, tblk, lrd, NULL));
  1592. if (tlck->type & tlckBTROOT) {
  1593. /* Empty xtree must be logged */
  1594. lrd->type = cpu_to_le16(LOG_REDOPAGE);
  1595. lrd->backchain =
  1596. cpu_to_le32(lmLog(log, tblk, lrd, tlck));
  1597. }
  1598. }
  1599. /* init LOG_UPDATEMAP of the freed extents
  1600. * XAD[XTENTRYSTART:hwm) from the deleted page itself
  1601. * for logredo() to update bmap;
  1602. */
  1603. lrd->type = cpu_to_le16(LOG_UPDATEMAP);
  1604. lrd->log.updatemap.type = cpu_to_le16(LOG_FREEXADLIST);
  1605. xtlck = (struct xtlock *) & tlck->lock;
  1606. hwm = xtlck->hwm.offset;
  1607. lrd->log.updatemap.nxd =
  1608. cpu_to_le16(hwm - XTENTRYSTART + 1);
  1609. /* reformat linelock for lmLog() */
  1610. xtlck->header.offset = XTENTRYSTART;
  1611. xtlck->header.length = hwm - XTENTRYSTART + 1;
  1612. xtlck->index = 1;
  1613. lrd->backchain = cpu_to_le32(lmLog(log, tblk, lrd, tlck));
  1614. /* format a maplock for txUpdateMap() to update bmap
  1615. * to free extents of XAD[XTENTRYSTART:hwm) from the
  1616. * deleted page itself;
  1617. */
  1618. tlck->flag |= tlckUPDATEMAP;
  1619. xadlock->count = hwm - XTENTRYSTART + 1;
  1620. if ((xadlock->count <= 4) && (tblk->xflag & COMMIT_LAZY)) {
  1621. int i;
  1622. pxd_t *pxd;
  1623. /*
  1624. * Lazy commit may allow xtree to be modified before
  1625. * txUpdateMap runs. Copy xad into linelock to
  1626. * preserve correct data.
  1627. *
  1628. * We can fit twice as may pxd's as xads in the lock
  1629. */
  1630. xadlock->flag = mlckFREEPXDLIST;
  1631. pxd = xadlock->xdlist = &xtlck->pxdlock;
  1632. for (i = 0; i < xadlock->count; i++) {
  1633. PXDaddress(pxd,
  1634. addressXAD(&p->xad[XTENTRYSTART + i]));
  1635. PXDlength(pxd,
  1636. lengthXAD(&p->xad[XTENTRYSTART + i]));
  1637. pxd++;
  1638. }
  1639. } else {
  1640. /*
  1641. * xdlist will point to into inode's xtree, ensure
  1642. * that transaction is not committed lazily.
  1643. */
  1644. xadlock->flag = mlckFREEXADLIST;
  1645. xadlock->xdlist = &p->xad[XTENTRYSTART];
  1646. tblk->xflag &= ~COMMIT_LAZY;
  1647. }
  1648. jfs_info("xtLog: free ip:0x%p mp:0x%p count:%d lwm:2",
  1649. tlck->ip, mp, xadlock->count);
  1650. maplock->index = 1;
  1651. /* mark page as invalid */
  1652. if (((tblk->xflag & COMMIT_PWMAP) || S_ISDIR(ip->i_mode))
  1653. && !(tlck->type & tlckBTROOT))
  1654. tlck->flag |= tlckFREEPAGE;
  1655. /*
  1656. else (tblk->xflag & COMMIT_PMAP)
  1657. ? release the page;
  1658. */
  1659. return;
  1660. }
  1661. /*
  1662. * page/entry truncation: file truncation (ref. xtTruncate())
  1663. *
  1664. * |----------+------+------+---------------|
  1665. * | | |
  1666. * | | hwm - hwm before truncation
  1667. * | next - truncation point
  1668. * lwm - lwm before truncation
  1669. * header ?
  1670. */
  1671. if (tlck->type & tlckTRUNCATE) {
  1672. /* This odd declaration suppresses a bogus gcc warning */
  1673. pxd_t pxd = pxd; /* truncated extent of xad */
  1674. int twm;
  1675. /*
  1676. * For truncation the entire linelock may be used, so it would
  1677. * be difficult to store xad list in linelock itself.
  1678. * Therefore, we'll just force transaction to be committed
  1679. * synchronously, so that xtree pages won't be changed before
  1680. * txUpdateMap runs.
  1681. */
  1682. tblk->xflag &= ~COMMIT_LAZY;
  1683. lwm = xtlck->lwm.offset;
  1684. if (lwm == 0)
  1685. lwm = XTPAGEMAXSLOT;
  1686. hwm = xtlck->hwm.offset;
  1687. twm = xtlck->twm.offset;
  1688. /*
  1689. * write log records
  1690. */
  1691. /* log after-image for logredo():
  1692. *
  1693. * logredo() will update bmap for alloc of new/extended
  1694. * extents (XAD_NEW|XAD_EXTEND) of XAD[lwm:next) from
  1695. * after-image of XADlist;
  1696. * logredo() resets (XAD_NEW|XAD_EXTEND) flag when
  1697. * applying the after-image to the meta-data page.
  1698. */
  1699. lrd->type = cpu_to_le16(LOG_REDOPAGE);
  1700. PXDaddress(page_pxd, mp->index);
  1701. PXDlength(page_pxd,
  1702. mp->logical_size >> tblk->sb->s_blocksize_bits);
  1703. lrd->backchain = cpu_to_le32(lmLog(log, tblk, lrd, tlck));
  1704. /*
  1705. * truncate entry XAD[twm == next - 1]:
  1706. */
  1707. if (twm == next - 1) {
  1708. /* init LOG_UPDATEMAP for logredo() to update bmap for
  1709. * free of truncated delta extent of the truncated
  1710. * entry XAD[next - 1]:
  1711. * (xtlck->pxdlock = truncated delta extent);
  1712. */
  1713. pxdlock = (struct pxd_lock *) & xtlck->pxdlock;
  1714. /* assert(pxdlock->type & tlckTRUNCATE); */
  1715. lrd->type = cpu_to_le16(LOG_UPDATEMAP);
  1716. lrd->log.updatemap.type = cpu_to_le16(LOG_FREEPXD);
  1717. lrd->log.updatemap.nxd = cpu_to_le16(1);
  1718. lrd->log.updatemap.pxd = pxdlock->pxd;
  1719. pxd = pxdlock->pxd; /* save to format maplock */
  1720. lrd->backchain =
  1721. cpu_to_le32(lmLog(log, tblk, lrd, NULL));
  1722. }
  1723. /*
  1724. * free entries XAD[next:hwm]:
  1725. */
  1726. if (hwm >= next) {
  1727. /* init LOG_UPDATEMAP of the freed extents
  1728. * XAD[next:hwm] from the deleted page itself
  1729. * for logredo() to update bmap;
  1730. */
  1731. lrd->type = cpu_to_le16(LOG_UPDATEMAP);
  1732. lrd->log.updatemap.type =
  1733. cpu_to_le16(LOG_FREEXADLIST);
  1734. xtlck = (struct xtlock *) & tlck->lock;
  1735. hwm = xtlck->hwm.offset;
  1736. lrd->log.updatemap.nxd =
  1737. cpu_to_le16(hwm - next + 1);
  1738. /* reformat linelock for lmLog() */
  1739. xtlck->header.offset = next;
  1740. xtlck->header.length = hwm - next + 1;
  1741. xtlck->index = 1;
  1742. lrd->backchain =
  1743. cpu_to_le32(lmLog(log, tblk, lrd, tlck));
  1744. }
  1745. /*
  1746. * format maplock(s) for txUpdateMap() to update bmap
  1747. */
  1748. maplock->index = 0;
  1749. /*
  1750. * allocate entries XAD[lwm:next):
  1751. */
  1752. if (lwm < next) {
  1753. /* format a maplock for txUpdateMap() to update bPMAP
  1754. * for alloc of new/extended extents of XAD[lwm:next)
  1755. * from the page itself;
  1756. * txUpdateMap() resets (XAD_NEW|XAD_EXTEND) flag.
  1757. */
  1758. tlck->flag |= tlckUPDATEMAP;
  1759. xadlock->flag = mlckALLOCXADLIST;
  1760. xadlock->count = next - lwm;
  1761. xadlock->xdlist = &p->xad[lwm];
  1762. jfs_info("xtLog: alloc ip:0x%p mp:0x%p count:%d "
  1763. "lwm:%d next:%d",
  1764. tlck->ip, mp, xadlock->count, lwm, next);
  1765. maplock->index++;
  1766. xadlock++;
  1767. }
  1768. /*
  1769. * truncate entry XAD[twm == next - 1]:
  1770. */
  1771. if (twm == next - 1) {
  1772. /* format a maplock for txUpdateMap() to update bmap
  1773. * to free truncated delta extent of the truncated
  1774. * entry XAD[next - 1];
  1775. * (xtlck->pxdlock = truncated delta extent);
  1776. */
  1777. tlck->flag |= tlckUPDATEMAP;
  1778. pxdlock = (struct pxd_lock *) xadlock;
  1779. pxdlock->flag = mlckFREEPXD;
  1780. pxdlock->count = 1;
  1781. pxdlock->pxd = pxd;
  1782. jfs_info("xtLog: truncate ip:0x%p mp:0x%p count:%d "
  1783. "hwm:%d", ip, mp, pxdlock->count, hwm);
  1784. maplock->index++;
  1785. xadlock++;
  1786. }
  1787. /*
  1788. * free entries XAD[next:hwm]:
  1789. */
  1790. if (hwm >= next) {
  1791. /* format a maplock for txUpdateMap() to update bmap
  1792. * to free extents of XAD[next:hwm] from thedeleted
  1793. * page itself;
  1794. */
  1795. tlck->flag |= tlckUPDATEMAP;
  1796. xadlock->flag = mlckFREEXADLIST;
  1797. xadlock->count = hwm - next + 1;
  1798. xadlock->xdlist = &p->xad[next];
  1799. jfs_info("xtLog: free ip:0x%p mp:0x%p count:%d "
  1800. "next:%d hwm:%d",
  1801. tlck->ip, mp, xadlock->count, next, hwm);
  1802. maplock->index++;
  1803. }
  1804. /* mark page as homeward bound */
  1805. tlck->flag |= tlckWRITEPAGE;
  1806. }
  1807. return;
  1808. }
  1809. /*
  1810. * mapLog()
  1811. *
  1812. * function: log from maplock of freed data extents;
  1813. */
  1814. static void mapLog(struct jfs_log * log, struct tblock * tblk, struct lrd * lrd,
  1815. struct tlock * tlck)
  1816. {
  1817. struct pxd_lock *pxdlock;
  1818. int i, nlock;
  1819. pxd_t *pxd;
  1820. /*
  1821. * page relocation: free the source page extent
  1822. *
  1823. * a maplock for txUpdateMap() for free of the page
  1824. * has been formatted at txLock() time saving the src
  1825. * relocated page address;
  1826. */
  1827. if (tlck->type & tlckRELOCATE) {
  1828. /* log LOG_NOREDOPAGE of the old relocated page
  1829. * for logredo() to start NoRedoPage filter;
  1830. */
  1831. lrd->type = cpu_to_le16(LOG_NOREDOPAGE);
  1832. pxdlock = (struct pxd_lock *) & tlck->lock;
  1833. pxd = &lrd->log.redopage.pxd;
  1834. *pxd = pxdlock->pxd;
  1835. lrd->backchain = cpu_to_le32(lmLog(log, tblk, lrd, NULL));
  1836. /* (N.B. currently, logredo() does NOT update bmap
  1837. * for free of the page itself for (LOG_XTREE|LOG_NOREDOPAGE);
  1838. * if page free from relocation, LOG_UPDATEMAP log is
  1839. * specifically generated now for logredo()
  1840. * to update bmap for free of src relocated page;
  1841. * (new flag LOG_RELOCATE may be introduced which will
  1842. * inform logredo() to start NORedoPage filter and also
  1843. * update block allocation map at the same time, thus
  1844. * avoiding an extra log write);
  1845. */
  1846. lrd->type = cpu_to_le16(LOG_UPDATEMAP);
  1847. lrd->log.updatemap.type = cpu_to_le16(LOG_FREEPXD);
  1848. lrd->log.updatemap.nxd = cpu_to_le16(1);
  1849. lrd->log.updatemap.pxd = pxdlock->pxd;
  1850. lrd->backchain = cpu_to_le32(lmLog(log, tblk, lrd, NULL));
  1851. /* a maplock for txUpdateMap() for free of the page
  1852. * has been formatted at txLock() time;
  1853. */
  1854. tlck->flag |= tlckUPDATEMAP;
  1855. return;
  1856. }
  1857. /*
  1858. * Otherwise it's not a relocate request
  1859. *
  1860. */
  1861. else {
  1862. /* log LOG_UPDATEMAP for logredo() to update bmap for
  1863. * free of truncated/relocated delta extent of the data;
  1864. * e.g.: external EA extent, relocated/truncated extent
  1865. * from xtTailgate();
  1866. */
  1867. lrd->type = cpu_to_le16(LOG_UPDATEMAP);
  1868. pxdlock = (struct pxd_lock *) & tlck->lock;
  1869. nlock = pxdlock->index;
  1870. for (i = 0; i < nlock; i++, pxdlock++) {
  1871. if (pxdlock->flag & mlckALLOCPXD)
  1872. lrd->log.updatemap.type =
  1873. cpu_to_le16(LOG_ALLOCPXD);
  1874. else
  1875. lrd->log.updatemap.type =
  1876. cpu_to_le16(LOG_FREEPXD);
  1877. lrd->log.updatemap.nxd = cpu_to_le16(1);
  1878. lrd->log.updatemap.pxd = pxdlock->pxd;
  1879. lrd->backchain =
  1880. cpu_to_le32(lmLog(log, tblk, lrd, NULL));
  1881. jfs_info("mapLog: xaddr:0x%lx xlen:0x%x",
  1882. (ulong) addressPXD(&pxdlock->pxd),
  1883. lengthPXD(&pxdlock->pxd));
  1884. }
  1885. /* update bmap */
  1886. tlck->flag |= tlckUPDATEMAP;
  1887. }
  1888. }
  1889. /*
  1890. * txEA()
  1891. *
  1892. * function: acquire maplock for EA/ACL extents or
  1893. * set COMMIT_INLINE flag;
  1894. */
  1895. void txEA(tid_t tid, struct inode *ip, dxd_t * oldea, dxd_t * newea)
  1896. {
  1897. struct tlock *tlck = NULL;
  1898. struct pxd_lock *maplock = NULL, *pxdlock = NULL;
  1899. /*
  1900. * format maplock for alloc of new EA extent
  1901. */
  1902. if (newea) {
  1903. /* Since the newea could be a completely zeroed entry we need to
  1904. * check for the two flags which indicate we should actually
  1905. * commit new EA data
  1906. */
  1907. if (newea->flag & DXD_EXTENT) {
  1908. tlck = txMaplock(tid, ip, tlckMAP);
  1909. maplock = (struct pxd_lock *) & tlck->lock;
  1910. pxdlock = (struct pxd_lock *) maplock;
  1911. pxdlock->flag = mlckALLOCPXD;
  1912. PXDaddress(&pxdlock->pxd, addressDXD(newea));
  1913. PXDlength(&pxdlock->pxd, lengthDXD(newea));
  1914. pxdlock++;
  1915. maplock->index = 1;
  1916. } else if (newea->flag & DXD_INLINE) {
  1917. tlck = NULL;
  1918. set_cflag(COMMIT_Inlineea, ip);
  1919. }
  1920. }
  1921. /*
  1922. * format maplock for free of old EA extent
  1923. */
  1924. if (!test_cflag(COMMIT_Nolink, ip) && oldea->flag & DXD_EXTENT) {
  1925. if (tlck == NULL) {
  1926. tlck = txMaplock(tid, ip, tlckMAP);
  1927. maplock = (struct pxd_lock *) & tlck->lock;
  1928. pxdlock = (struct pxd_lock *) maplock;
  1929. maplock->index = 0;
  1930. }
  1931. pxdlock->flag = mlckFREEPXD;
  1932. PXDaddress(&pxdlock->pxd, addressDXD(oldea));
  1933. PXDlength(&pxdlock->pxd, lengthDXD(oldea));
  1934. maplock->index++;
  1935. }
  1936. }
  1937. /*
  1938. * txForce()
  1939. *
  1940. * function: synchronously write pages locked by transaction
  1941. * after txLog() but before txUpdateMap();
  1942. */
  1943. static void txForce(struct tblock * tblk)
  1944. {
  1945. struct tlock *tlck;
  1946. lid_t lid, next;
  1947. struct metapage *mp;
  1948. /*
  1949. * reverse the order of transaction tlocks in
  1950. * careful update order of address index pages
  1951. * (right to left, bottom up)
  1952. */
  1953. tlck = lid_to_tlock(tblk->next);
  1954. lid = tlck->next;
  1955. tlck->next = 0;
  1956. while (lid) {
  1957. tlck = lid_to_tlock(lid);
  1958. next = tlck->next;
  1959. tlck->next = tblk->next;
  1960. tblk->next = lid;
  1961. lid = next;
  1962. }
  1963. /*
  1964. * synchronously write the page, and
  1965. * hold the page for txUpdateMap();
  1966. */
  1967. for (lid = tblk->next; lid; lid = next) {
  1968. tlck = lid_to_tlock(lid);
  1969. next = tlck->next;
  1970. if ((mp = tlck->mp) != NULL &&
  1971. (tlck->type & tlckBTROOT) == 0) {
  1972. assert(mp->xflag & COMMIT_PAGE);
  1973. if (tlck->flag & tlckWRITEPAGE) {
  1974. tlck->flag &= ~tlckWRITEPAGE;
  1975. /* do not release page to freelist */
  1976. force_metapage(mp);
  1977. #if 0
  1978. /*
  1979. * The "right" thing to do here is to
  1980. * synchronously write the metadata.
  1981. * With the current implementation this
  1982. * is hard since write_metapage requires
  1983. * us to kunmap & remap the page. If we
  1984. * have tlocks pointing into the metadata
  1985. * pages, we don't want to do this. I think
  1986. * we can get by with synchronously writing
  1987. * the pages when they are released.
  1988. */
  1989. assert(mp->nohomeok);
  1990. set_bit(META_dirty, &mp->flag);
  1991. set_bit(META_sync, &mp->flag);
  1992. #endif
  1993. }
  1994. }
  1995. }
  1996. }
  1997. /*
  1998. * txUpdateMap()
  1999. *
  2000. * function: update persistent allocation map (and working map
  2001. * if appropriate);
  2002. *
  2003. * parameter:
  2004. */
  2005. static void txUpdateMap(struct tblock * tblk)
  2006. {
  2007. struct inode *ip;
  2008. struct inode *ipimap;
  2009. lid_t lid;
  2010. struct tlock *tlck;
  2011. struct maplock *maplock;
  2012. struct pxd_lock pxdlock;
  2013. int maptype;
  2014. int k, nlock;
  2015. struct metapage *mp = NULL;
  2016. ipimap = JFS_SBI(tblk->sb)->ipimap;
  2017. maptype = (tblk->xflag & COMMIT_PMAP) ? COMMIT_PMAP : COMMIT_PWMAP;
  2018. /*
  2019. * update block allocation map
  2020. *
  2021. * update allocation state in pmap (and wmap) and
  2022. * update lsn of the pmap page;
  2023. */
  2024. /*
  2025. * scan each tlock/page of transaction for block allocation/free:
  2026. *
  2027. * for each tlock/page of transaction, update map.
  2028. * ? are there tlock for pmap and pwmap at the same time ?
  2029. */
  2030. for (lid = tblk->next; lid; lid = tlck->next) {
  2031. tlck = lid_to_tlock(lid);
  2032. if ((tlck->flag & tlckUPDATEMAP) == 0)
  2033. continue;
  2034. if (tlck->flag & tlckFREEPAGE) {
  2035. /*
  2036. * Another thread may attempt to reuse freed space
  2037. * immediately, so we want to get rid of the metapage
  2038. * before anyone else has a chance to get it.
  2039. * Lock metapage, update maps, then invalidate
  2040. * the metapage.
  2041. */
  2042. mp = tlck->mp;
  2043. ASSERT(mp->xflag & COMMIT_PAGE);
  2044. grab_metapage(mp);
  2045. }
  2046. /*
  2047. * extent list:
  2048. * . in-line PXD list:
  2049. * . out-of-line XAD list:
  2050. */
  2051. maplock = (struct maplock *) & tlck->lock;
  2052. nlock = maplock->index;
  2053. for (k = 0; k < nlock; k++, maplock++) {
  2054. /*
  2055. * allocate blocks in persistent map:
  2056. *
  2057. * blocks have been allocated from wmap at alloc time;
  2058. */
  2059. if (maplock->flag & mlckALLOC) {
  2060. txAllocPMap(ipimap, maplock, tblk);
  2061. }
  2062. /*
  2063. * free blocks in persistent and working map:
  2064. * blocks will be freed in pmap and then in wmap;
  2065. *
  2066. * ? tblock specifies the PMAP/PWMAP based upon
  2067. * transaction
  2068. *
  2069. * free blocks in persistent map:
  2070. * blocks will be freed from wmap at last reference
  2071. * release of the object for regular files;
  2072. *
  2073. * Alway free blocks from both persistent & working
  2074. * maps for directories
  2075. */
  2076. else { /* (maplock->flag & mlckFREE) */
  2077. if (tlck->flag & tlckDIRECTORY)
  2078. txFreeMap(ipimap, maplock,
  2079. tblk, COMMIT_PWMAP);
  2080. else
  2081. txFreeMap(ipimap, maplock,
  2082. tblk, maptype);
  2083. }
  2084. }
  2085. if (tlck->flag & tlckFREEPAGE) {
  2086. if (!(tblk->flag & tblkGC_LAZY)) {
  2087. /* This is equivalent to txRelease */
  2088. ASSERT(mp->lid == lid);
  2089. tlck->mp->lid = 0;
  2090. }
  2091. assert(mp->nohomeok == 1);
  2092. metapage_homeok(mp);
  2093. discard_metapage(mp);
  2094. tlck->mp = NULL;
  2095. }
  2096. }
  2097. /*
  2098. * update inode allocation map
  2099. *
  2100. * update allocation state in pmap and
  2101. * update lsn of the pmap page;
  2102. * update in-memory inode flag/state
  2103. *
  2104. * unlock mapper/write lock
  2105. */
  2106. if (tblk->xflag & COMMIT_CREATE) {
  2107. diUpdatePMap(ipimap, tblk->ino, false, tblk);
  2108. /* update persistent block allocation map
  2109. * for the allocation of inode extent;
  2110. */
  2111. pxdlock.flag = mlckALLOCPXD;
  2112. pxdlock.pxd = tblk->u.ixpxd;
  2113. pxdlock.index = 1;
  2114. txAllocPMap(ipimap, (struct maplock *) & pxdlock, tblk);
  2115. } else if (tblk->xflag & COMMIT_DELETE) {
  2116. ip = tblk->u.ip;
  2117. diUpdatePMap(ipimap, ip->i_ino, true, tblk);
  2118. iput(ip);
  2119. }
  2120. }
  2121. /*
  2122. * txAllocPMap()
  2123. *
  2124. * function: allocate from persistent map;
  2125. *
  2126. * parameter:
  2127. * ipbmap -
  2128. * malock -
  2129. * xad list:
  2130. * pxd:
  2131. *
  2132. * maptype -
  2133. * allocate from persistent map;
  2134. * free from persistent map;
  2135. * (e.g., tmp file - free from working map at releae
  2136. * of last reference);
  2137. * free from persistent and working map;
  2138. *
  2139. * lsn - log sequence number;
  2140. */
  2141. static void txAllocPMap(struct inode *ip, struct maplock * maplock,
  2142. struct tblock * tblk)
  2143. {
  2144. struct inode *ipbmap = JFS_SBI(ip->i_sb)->ipbmap;
  2145. struct xdlistlock *xadlistlock;
  2146. xad_t *xad;
  2147. s64 xaddr;
  2148. int xlen;
  2149. struct pxd_lock *pxdlock;
  2150. struct xdlistlock *pxdlistlock;
  2151. pxd_t *pxd;
  2152. int n;
  2153. /*
  2154. * allocate from persistent map;
  2155. */
  2156. if (maplock->flag & mlckALLOCXADLIST) {
  2157. xadlistlock = (struct xdlistlock *) maplock;
  2158. xad = xadlistlock->xdlist;
  2159. for (n = 0; n < xadlistlock->count; n++, xad++) {
  2160. if (xad->flag & (XAD_NEW | XAD_EXTENDED)) {
  2161. xaddr = addressXAD(xad);
  2162. xlen = lengthXAD(xad);
  2163. dbUpdatePMap(ipbmap, false, xaddr,
  2164. (s64) xlen, tblk);
  2165. xad->flag &= ~(XAD_NEW | XAD_EXTENDED);
  2166. jfs_info("allocPMap: xaddr:0x%lx xlen:%d",
  2167. (ulong) xaddr, xlen);
  2168. }
  2169. }
  2170. } else if (maplock->flag & mlckALLOCPXD) {
  2171. pxdlock = (struct pxd_lock *) maplock;
  2172. xaddr = addressPXD(&pxdlock->pxd);
  2173. xlen = lengthPXD(&pxdlock->pxd);
  2174. dbUpdatePMap(ipbmap, false, xaddr, (s64) xlen, tblk);
  2175. jfs_info("allocPMap: xaddr:0x%lx xlen:%d", (ulong) xaddr, xlen);
  2176. } else { /* (maplock->flag & mlckALLOCPXDLIST) */
  2177. pxdlistlock = (struct xdlistlock *) maplock;
  2178. pxd = pxdlistlock->xdlist;
  2179. for (n = 0; n < pxdlistlock->count; n++, pxd++) {
  2180. xaddr = addressPXD(pxd);
  2181. xlen = lengthPXD(pxd);
  2182. dbUpdatePMap(ipbmap, false, xaddr, (s64) xlen,
  2183. tblk);
  2184. jfs_info("allocPMap: xaddr:0x%lx xlen:%d",
  2185. (ulong) xaddr, xlen);
  2186. }
  2187. }
  2188. }
  2189. /*
  2190. * txFreeMap()
  2191. *
  2192. * function: free from persistent and/or working map;
  2193. *
  2194. * todo: optimization
  2195. */
  2196. void txFreeMap(struct inode *ip,
  2197. struct maplock * maplock, struct tblock * tblk, int maptype)
  2198. {
  2199. struct inode *ipbmap = JFS_SBI(ip->i_sb)->ipbmap;
  2200. struct xdlistlock *xadlistlock;
  2201. xad_t *xad;
  2202. s64 xaddr;
  2203. int xlen;
  2204. struct pxd_lock *pxdlock;
  2205. struct xdlistlock *pxdlistlock;
  2206. pxd_t *pxd;
  2207. int n;
  2208. jfs_info("txFreeMap: tblk:0x%p maplock:0x%p maptype:0x%x",
  2209. tblk, maplock, maptype);
  2210. /*
  2211. * free from persistent map;
  2212. */
  2213. if (maptype == COMMIT_PMAP || maptype == COMMIT_PWMAP) {
  2214. if (maplock->flag & mlckFREEXADLIST) {
  2215. xadlistlock = (struct xdlistlock *) maplock;
  2216. xad = xadlistlock->xdlist;
  2217. for (n = 0; n < xadlistlock->count; n++, xad++) {
  2218. if (!(xad->flag & XAD_NEW)) {
  2219. xaddr = addressXAD(xad);
  2220. xlen = lengthXAD(xad);
  2221. dbUpdatePMap(ipbmap, true, xaddr,
  2222. (s64) xlen, tblk);
  2223. jfs_info("freePMap: xaddr:0x%lx "
  2224. "xlen:%d",
  2225. (ulong) xaddr, xlen);
  2226. }
  2227. }
  2228. } else if (maplock->flag & mlckFREEPXD) {
  2229. pxdlock = (struct pxd_lock *) maplock;
  2230. xaddr = addressPXD(&pxdlock->pxd);
  2231. xlen = lengthPXD(&pxdlock->pxd);
  2232. dbUpdatePMap(ipbmap, true, xaddr, (s64) xlen,
  2233. tblk);
  2234. jfs_info("freePMap: xaddr:0x%lx xlen:%d",
  2235. (ulong) xaddr, xlen);
  2236. } else { /* (maplock->flag & mlckALLOCPXDLIST) */
  2237. pxdlistlock = (struct xdlistlock *) maplock;
  2238. pxd = pxdlistlock->xdlist;
  2239. for (n = 0; n < pxdlistlock->count; n++, pxd++) {
  2240. xaddr = addressPXD(pxd);
  2241. xlen = lengthPXD(pxd);
  2242. dbUpdatePMap(ipbmap, true, xaddr,
  2243. (s64) xlen, tblk);
  2244. jfs_info("freePMap: xaddr:0x%lx xlen:%d",
  2245. (ulong) xaddr, xlen);
  2246. }
  2247. }
  2248. }
  2249. /*
  2250. * free from working map;
  2251. */
  2252. if (maptype == COMMIT_PWMAP || maptype == COMMIT_WMAP) {
  2253. if (maplock->flag & mlckFREEXADLIST) {
  2254. xadlistlock = (struct xdlistlock *) maplock;
  2255. xad = xadlistlock->xdlist;
  2256. for (n = 0; n < xadlistlock->count; n++, xad++) {
  2257. xaddr = addressXAD(xad);
  2258. xlen = lengthXAD(xad);
  2259. dbFree(ip, xaddr, (s64) xlen);
  2260. xad->flag = 0;
  2261. jfs_info("freeWMap: xaddr:0x%lx xlen:%d",
  2262. (ulong) xaddr, xlen);
  2263. }
  2264. } else if (maplock->flag & mlckFREEPXD) {
  2265. pxdlock = (struct pxd_lock *) maplock;
  2266. xaddr = addressPXD(&pxdlock->pxd);
  2267. xlen = lengthPXD(&pxdlock->pxd);
  2268. dbFree(ip, xaddr, (s64) xlen);
  2269. jfs_info("freeWMap: xaddr:0x%lx xlen:%d",
  2270. (ulong) xaddr, xlen);
  2271. } else { /* (maplock->flag & mlckFREEPXDLIST) */
  2272. pxdlistlock = (struct xdlistlock *) maplock;
  2273. pxd = pxdlistlock->xdlist;
  2274. for (n = 0; n < pxdlistlock->count; n++, pxd++) {
  2275. xaddr = addressPXD(pxd);
  2276. xlen = lengthPXD(pxd);
  2277. dbFree(ip, xaddr, (s64) xlen);
  2278. jfs_info("freeWMap: xaddr:0x%lx xlen:%d",
  2279. (ulong) xaddr, xlen);
  2280. }
  2281. }
  2282. }
  2283. }
  2284. /*
  2285. * txFreelock()
  2286. *
  2287. * function: remove tlock from inode anonymous locklist
  2288. */
  2289. void txFreelock(struct inode *ip)
  2290. {
  2291. struct jfs_inode_info *jfs_ip = JFS_IP(ip);
  2292. struct tlock *xtlck, *tlck;
  2293. lid_t xlid = 0, lid;
  2294. if (!jfs_ip->atlhead)
  2295. return;
  2296. TXN_LOCK();
  2297. xtlck = (struct tlock *) &jfs_ip->atlhead;
  2298. while ((lid = xtlck->next) != 0) {
  2299. tlck = lid_to_tlock(lid);
  2300. if (tlck->flag & tlckFREELOCK) {
  2301. xtlck->next = tlck->next;
  2302. txLockFree(lid);
  2303. } else {
  2304. xtlck = tlck;
  2305. xlid = lid;
  2306. }
  2307. }
  2308. if (jfs_ip->atlhead)
  2309. jfs_ip->atltail = xlid;
  2310. else {
  2311. jfs_ip->atltail = 0;
  2312. /*
  2313. * If inode was on anon_list, remove it
  2314. */
  2315. list_del_init(&jfs_ip->anon_inode_list);
  2316. }
  2317. TXN_UNLOCK();
  2318. }
  2319. /*
  2320. * txAbort()
  2321. *
  2322. * function: abort tx before commit;
  2323. *
  2324. * frees line-locks and segment locks for all
  2325. * segments in comdata structure.
  2326. * Optionally sets state of file-system to FM_DIRTY in super-block.
  2327. * log age of page-frames in memory for which caller has
  2328. * are reset to 0 (to avoid logwarap).
  2329. */
  2330. void txAbort(tid_t tid, int dirty)
  2331. {
  2332. lid_t lid, next;
  2333. struct metapage *mp;
  2334. struct tblock *tblk = tid_to_tblock(tid);
  2335. struct tlock *tlck;
  2336. /*
  2337. * free tlocks of the transaction
  2338. */
  2339. for (lid = tblk->next; lid; lid = next) {
  2340. tlck = lid_to_tlock(lid);
  2341. next = tlck->next;
  2342. mp = tlck->mp;
  2343. JFS_IP(tlck->ip)->xtlid = 0;
  2344. if (mp) {
  2345. mp->lid = 0;
  2346. /*
  2347. * reset lsn of page to avoid logwarap:
  2348. *
  2349. * (page may have been previously committed by another
  2350. * transaction(s) but has not been paged, i.e.,
  2351. * it may be on logsync list even though it has not
  2352. * been logged for the current tx.)
  2353. */
  2354. if (mp->xflag & COMMIT_PAGE && mp->lsn)
  2355. LogSyncRelease(mp);
  2356. }
  2357. /* insert tlock at head of freelist */
  2358. TXN_LOCK();
  2359. txLockFree(lid);
  2360. TXN_UNLOCK();
  2361. }
  2362. /* caller will free the transaction block */
  2363. tblk->next = tblk->last = 0;
  2364. /*
  2365. * mark filesystem dirty
  2366. */
  2367. if (dirty)
  2368. jfs_error(tblk->sb, "txAbort");
  2369. return;
  2370. }
  2371. /*
  2372. * txLazyCommit(void)
  2373. *
  2374. * All transactions except those changing ipimap (COMMIT_FORCE) are
  2375. * processed by this routine. This insures that the inode and block
  2376. * allocation maps are updated in order. For synchronous transactions,
  2377. * let the user thread finish processing after txUpdateMap() is called.
  2378. */
  2379. static void txLazyCommit(struct tblock * tblk)
  2380. {
  2381. struct jfs_log *log;
  2382. while (((tblk->flag & tblkGC_READY) == 0) &&
  2383. ((tblk->flag & tblkGC_UNLOCKED) == 0)) {
  2384. /* We must have gotten ahead of the user thread
  2385. */
  2386. jfs_info("jfs_lazycommit: tblk 0x%p not unlocked", tblk);
  2387. yield();
  2388. }
  2389. jfs_info("txLazyCommit: processing tblk 0x%p", tblk);
  2390. txUpdateMap(tblk);
  2391. log = (struct jfs_log *) JFS_SBI(tblk->sb)->log;
  2392. spin_lock_irq(&log->gclock); // LOGGC_LOCK
  2393. tblk->flag |= tblkGC_COMMITTED;
  2394. if (tblk->flag & tblkGC_READY)
  2395. log->gcrtc--;
  2396. wake_up_all(&tblk->gcwait); // LOGGC_WAKEUP
  2397. /*
  2398. * Can't release log->gclock until we've tested tblk->flag
  2399. */
  2400. if (tblk->flag & tblkGC_LAZY) {
  2401. spin_unlock_irq(&log->gclock); // LOGGC_UNLOCK
  2402. txUnlock(tblk);
  2403. tblk->flag &= ~tblkGC_LAZY;
  2404. txEnd(tblk - TxBlock); /* Convert back to tid */
  2405. } else
  2406. spin_unlock_irq(&log->gclock); // LOGGC_UNLOCK
  2407. jfs_info("txLazyCommit: done: tblk = 0x%p", tblk);
  2408. }
  2409. /*
  2410. * jfs_lazycommit(void)
  2411. *
  2412. * To be run as a kernel daemon. If lbmIODone is called in an interrupt
  2413. * context, or where blocking is not wanted, this routine will process
  2414. * committed transactions from the unlock queue.
  2415. */
  2416. int jfs_lazycommit(void *arg)
  2417. {
  2418. int WorkDone;
  2419. struct tblock *tblk;
  2420. unsigned long flags;
  2421. struct jfs_sb_info *sbi;
  2422. do {
  2423. LAZY_LOCK(flags);
  2424. jfs_commit_thread_waking = 0; /* OK to wake another thread */
  2425. while (!list_empty(&TxAnchor.unlock_queue)) {
  2426. WorkDone = 0;
  2427. list_for_each_entry(tblk, &TxAnchor.unlock_queue,
  2428. cqueue) {
  2429. sbi = JFS_SBI(tblk->sb);
  2430. /*
  2431. * For each volume, the transactions must be
  2432. * handled in order. If another commit thread
  2433. * is handling a tblk for this superblock,
  2434. * skip it
  2435. */
  2436. if (sbi->commit_state & IN_LAZYCOMMIT)
  2437. continue;
  2438. sbi->commit_state |= IN_LAZYCOMMIT;
  2439. WorkDone = 1;
  2440. /*
  2441. * Remove transaction from queue
  2442. */
  2443. list_del(&tblk->cqueue);
  2444. LAZY_UNLOCK(flags);
  2445. txLazyCommit(tblk);
  2446. LAZY_LOCK(flags);
  2447. sbi->commit_state &= ~IN_LAZYCOMMIT;
  2448. /*
  2449. * Don't continue in the for loop. (We can't
  2450. * anyway, it's unsafe!) We want to go back to
  2451. * the beginning of the list.
  2452. */
  2453. break;
  2454. }
  2455. /* If there was nothing to do, don't continue */
  2456. if (!WorkDone)
  2457. break;
  2458. }
  2459. /* In case a wakeup came while all threads were active */
  2460. jfs_commit_thread_waking = 0;
  2461. if (freezing(current)) {
  2462. LAZY_UNLOCK(flags);
  2463. refrigerator();
  2464. } else {
  2465. DECLARE_WAITQUEUE(wq, current);
  2466. add_wait_queue(&jfs_commit_thread_wait, &wq);
  2467. set_current_state(TASK_INTERRUPTIBLE);
  2468. LAZY_UNLOCK(flags);
  2469. schedule();
  2470. current->state = TASK_RUNNING;
  2471. remove_wait_queue(&jfs_commit_thread_wait, &wq);
  2472. }
  2473. } while (!kthread_should_stop());
  2474. if (!list_empty(&TxAnchor.unlock_queue))
  2475. jfs_err("jfs_lazycommit being killed w/pending transactions!");
  2476. else
  2477. jfs_info("jfs_lazycommit being killed\n");
  2478. return 0;
  2479. }
  2480. void txLazyUnlock(struct tblock * tblk)
  2481. {
  2482. unsigned long flags;
  2483. LAZY_LOCK(flags);
  2484. list_add_tail(&tblk->cqueue, &TxAnchor.unlock_queue);
  2485. /*
  2486. * Don't wake up a commit thread if there is already one servicing
  2487. * this superblock, or if the last one we woke up hasn't started yet.
  2488. */
  2489. if (!(JFS_SBI(tblk->sb)->commit_state & IN_LAZYCOMMIT) &&
  2490. !jfs_commit_thread_waking) {
  2491. jfs_commit_thread_waking = 1;
  2492. wake_up(&jfs_commit_thread_wait);
  2493. }
  2494. LAZY_UNLOCK(flags);
  2495. }
  2496. static void LogSyncRelease(struct metapage * mp)
  2497. {
  2498. struct jfs_log *log = mp->log;
  2499. assert(mp->nohomeok);
  2500. assert(log);
  2501. metapage_homeok(mp);
  2502. }
  2503. /*
  2504. * txQuiesce
  2505. *
  2506. * Block all new transactions and push anonymous transactions to
  2507. * completion
  2508. *
  2509. * This does almost the same thing as jfs_sync below. We don't
  2510. * worry about deadlocking when jfs_tlocks_low is set, since we would
  2511. * expect jfs_sync to get us out of that jam.
  2512. */
  2513. void txQuiesce(struct super_block *sb)
  2514. {
  2515. struct inode *ip;
  2516. struct jfs_inode_info *jfs_ip;
  2517. struct jfs_log *log = JFS_SBI(sb)->log;
  2518. tid_t tid;
  2519. set_bit(log_QUIESCE, &log->flag);
  2520. TXN_LOCK();
  2521. restart:
  2522. while (!list_empty(&TxAnchor.anon_list)) {
  2523. jfs_ip = list_entry(TxAnchor.anon_list.next,
  2524. struct jfs_inode_info,
  2525. anon_inode_list);
  2526. ip = &jfs_ip->vfs_inode;
  2527. /*
  2528. * inode will be removed from anonymous list
  2529. * when it is committed
  2530. */
  2531. TXN_UNLOCK();
  2532. tid = txBegin(ip->i_sb, COMMIT_INODE | COMMIT_FORCE);
  2533. mutex_lock(&jfs_ip->commit_mutex);
  2534. txCommit(tid, 1, &ip, 0);
  2535. txEnd(tid);
  2536. mutex_unlock(&jfs_ip->commit_mutex);
  2537. /*
  2538. * Just to be safe. I don't know how
  2539. * long we can run without blocking
  2540. */
  2541. cond_resched();
  2542. TXN_LOCK();
  2543. }
  2544. /*
  2545. * If jfs_sync is running in parallel, there could be some inodes
  2546. * on anon_list2. Let's check.
  2547. */
  2548. if (!list_empty(&TxAnchor.anon_list2)) {
  2549. list_splice(&TxAnchor.anon_list2, &TxAnchor.anon_list);
  2550. INIT_LIST_HEAD(&TxAnchor.anon_list2);
  2551. goto restart;
  2552. }
  2553. TXN_UNLOCK();
  2554. /*
  2555. * We may need to kick off the group commit
  2556. */
  2557. jfs_flush_journal(log, 0);
  2558. }
  2559. /*
  2560. * txResume()
  2561. *
  2562. * Allows transactions to start again following txQuiesce
  2563. */
  2564. void txResume(struct super_block *sb)
  2565. {
  2566. struct jfs_log *log = JFS_SBI(sb)->log;
  2567. clear_bit(log_QUIESCE, &log->flag);
  2568. TXN_WAKEUP(&log->syncwait);
  2569. }
  2570. /*
  2571. * jfs_sync(void)
  2572. *
  2573. * To be run as a kernel daemon. This is awakened when tlocks run low.
  2574. * We write any inodes that have anonymous tlocks so they will become
  2575. * available.
  2576. */
  2577. int jfs_sync(void *arg)
  2578. {
  2579. struct inode *ip;
  2580. struct jfs_inode_info *jfs_ip;
  2581. int rc;
  2582. tid_t tid;
  2583. do {
  2584. /*
  2585. * write each inode on the anonymous inode list
  2586. */
  2587. TXN_LOCK();
  2588. while (jfs_tlocks_low && !list_empty(&TxAnchor.anon_list)) {
  2589. jfs_ip = list_entry(TxAnchor.anon_list.next,
  2590. struct jfs_inode_info,
  2591. anon_inode_list);
  2592. ip = &jfs_ip->vfs_inode;
  2593. if (! igrab(ip)) {
  2594. /*
  2595. * Inode is being freed
  2596. */
  2597. list_del_init(&jfs_ip->anon_inode_list);
  2598. } else if (mutex_trylock(&jfs_ip->commit_mutex)) {
  2599. /*
  2600. * inode will be removed from anonymous list
  2601. * when it is committed
  2602. */
  2603. TXN_UNLOCK();
  2604. tid = txBegin(ip->i_sb, COMMIT_INODE);
  2605. rc = txCommit(tid, 1, &ip, 0);
  2606. txEnd(tid);
  2607. mutex_unlock(&jfs_ip->commit_mutex);
  2608. iput(ip);
  2609. /*
  2610. * Just to be safe. I don't know how
  2611. * long we can run without blocking
  2612. */
  2613. cond_resched();
  2614. TXN_LOCK();
  2615. } else {
  2616. /* We can't get the commit mutex. It may
  2617. * be held by a thread waiting for tlock's
  2618. * so let's not block here. Save it to
  2619. * put back on the anon_list.
  2620. */
  2621. /* Take off anon_list */
  2622. list_del(&jfs_ip->anon_inode_list);
  2623. /* Put on anon_list2 */
  2624. list_add(&jfs_ip->anon_inode_list,
  2625. &TxAnchor.anon_list2);
  2626. TXN_UNLOCK();
  2627. iput(ip);
  2628. TXN_LOCK();
  2629. }
  2630. }
  2631. /* Add anon_list2 back to anon_list */
  2632. list_splice_init(&TxAnchor.anon_list2, &TxAnchor.anon_list);
  2633. if (freezing(current)) {
  2634. TXN_UNLOCK();
  2635. refrigerator();
  2636. } else {
  2637. set_current_state(TASK_INTERRUPTIBLE);
  2638. TXN_UNLOCK();
  2639. schedule();
  2640. current->state = TASK_RUNNING;
  2641. }
  2642. } while (!kthread_should_stop());
  2643. jfs_info("jfs_sync being killed");
  2644. return 0;
  2645. }
  2646. #if defined(CONFIG_PROC_FS) && defined(CONFIG_JFS_DEBUG)
  2647. int jfs_txanchor_read(char *buffer, char **start, off_t offset, int length,
  2648. int *eof, void *data)
  2649. {
  2650. int len = 0;
  2651. off_t begin;
  2652. char *freewait;
  2653. char *freelockwait;
  2654. char *lowlockwait;
  2655. freewait =
  2656. waitqueue_active(&TxAnchor.freewait) ? "active" : "empty";
  2657. freelockwait =
  2658. waitqueue_active(&TxAnchor.freelockwait) ? "active" : "empty";
  2659. lowlockwait =
  2660. waitqueue_active(&TxAnchor.lowlockwait) ? "active" : "empty";
  2661. len += sprintf(buffer,
  2662. "JFS TxAnchor\n"
  2663. "============\n"
  2664. "freetid = %d\n"
  2665. "freewait = %s\n"
  2666. "freelock = %d\n"
  2667. "freelockwait = %s\n"
  2668. "lowlockwait = %s\n"
  2669. "tlocksInUse = %d\n"
  2670. "jfs_tlocks_low = %d\n"
  2671. "unlock_queue is %sempty\n",
  2672. TxAnchor.freetid,
  2673. freewait,
  2674. TxAnchor.freelock,
  2675. freelockwait,
  2676. lowlockwait,
  2677. TxAnchor.tlocksInUse,
  2678. jfs_tlocks_low,
  2679. list_empty(&TxAnchor.unlock_queue) ? "" : "not ");
  2680. begin = offset;
  2681. *start = buffer + begin;
  2682. len -= begin;
  2683. if (len > length)
  2684. len = length;
  2685. else
  2686. *eof = 1;
  2687. if (len < 0)
  2688. len = 0;
  2689. return len;
  2690. }
  2691. #endif
  2692. #if defined(CONFIG_PROC_FS) && defined(CONFIG_JFS_STATISTICS)
  2693. int jfs_txstats_read(char *buffer, char **start, off_t offset, int length,
  2694. int *eof, void *data)
  2695. {
  2696. int len = 0;
  2697. off_t begin;
  2698. len += sprintf(buffer,
  2699. "JFS TxStats\n"
  2700. "===========\n"
  2701. "calls to txBegin = %d\n"
  2702. "txBegin blocked by sync barrier = %d\n"
  2703. "txBegin blocked by tlocks low = %d\n"
  2704. "txBegin blocked by no free tid = %d\n"
  2705. "calls to txBeginAnon = %d\n"
  2706. "txBeginAnon blocked by sync barrier = %d\n"
  2707. "txBeginAnon blocked by tlocks low = %d\n"
  2708. "calls to txLockAlloc = %d\n"
  2709. "tLockAlloc blocked by no free lock = %d\n",
  2710. TxStat.txBegin,
  2711. TxStat.txBegin_barrier,
  2712. TxStat.txBegin_lockslow,
  2713. TxStat.txBegin_freetid,
  2714. TxStat.txBeginAnon,
  2715. TxStat.txBeginAnon_barrier,
  2716. TxStat.txBeginAnon_lockslow,
  2717. TxStat.txLockAlloc,
  2718. TxStat.txLockAlloc_freelock);
  2719. begin = offset;
  2720. *start = buffer + begin;
  2721. len -= begin;
  2722. if (len > length)
  2723. len = length;
  2724. else
  2725. *eof = 1;
  2726. if (len < 0)
  2727. len = 0;
  2728. return len;
  2729. }
  2730. #endif