jfs_txnmgr.c 76 KB

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