jfs_txnmgr.c 75 KB

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