glock.c 76 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  4. * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
  5. */
  6. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  7. #include <linux/sched.h>
  8. #include <linux/slab.h>
  9. #include <linux/spinlock.h>
  10. #include <linux/buffer_head.h>
  11. #include <linux/delay.h>
  12. #include <linux/sort.h>
  13. #include <linux/hash.h>
  14. #include <linux/jhash.h>
  15. #include <linux/kallsyms.h>
  16. #include <linux/gfs2_ondisk.h>
  17. #include <linux/list.h>
  18. #include <linux/wait.h>
  19. #include <linux/module.h>
  20. #include <linux/uaccess.h>
  21. #include <linux/seq_file.h>
  22. #include <linux/debugfs.h>
  23. #include <linux/kthread.h>
  24. #include <linux/freezer.h>
  25. #include <linux/workqueue.h>
  26. #include <linux/jiffies.h>
  27. #include <linux/rcupdate.h>
  28. #include <linux/rculist_bl.h>
  29. #include <linux/bit_spinlock.h>
  30. #include <linux/percpu.h>
  31. #include <linux/list_sort.h>
  32. #include <linux/lockref.h>
  33. #include <linux/rhashtable.h>
  34. #include <linux/pid_namespace.h>
  35. #include <linux/fdtable.h>
  36. #include <linux/file.h>
  37. #include "gfs2.h"
  38. #include "incore.h"
  39. #include "glock.h"
  40. #include "glops.h"
  41. #include "inode.h"
  42. #include "lops.h"
  43. #include "meta_io.h"
  44. #include "quota.h"
  45. #include "super.h"
  46. #include "util.h"
  47. #include "bmap.h"
  48. #define CREATE_TRACE_POINTS
  49. #include "trace_gfs2.h"
  50. struct gfs2_glock_iter {
  51. struct gfs2_sbd *sdp; /* incore superblock */
  52. struct rhashtable_iter hti; /* rhashtable iterator */
  53. struct gfs2_glock *gl; /* current glock struct */
  54. loff_t last_pos; /* last position */
  55. };
  56. typedef void (*glock_examiner) (struct gfs2_glock * gl);
  57. static void do_xmote(struct gfs2_glock *gl, struct gfs2_holder *gh, unsigned int target);
  58. static void __gfs2_glock_dq(struct gfs2_holder *gh);
  59. static void handle_callback(struct gfs2_glock *gl, unsigned int state,
  60. unsigned long delay, bool remote);
  61. static struct dentry *gfs2_root;
  62. static struct workqueue_struct *glock_workqueue;
  63. struct workqueue_struct *gfs2_delete_workqueue;
  64. static LIST_HEAD(lru_list);
  65. static atomic_t lru_count = ATOMIC_INIT(0);
  66. static DEFINE_SPINLOCK(lru_lock);
  67. #define GFS2_GL_HASH_SHIFT 15
  68. #define GFS2_GL_HASH_SIZE BIT(GFS2_GL_HASH_SHIFT)
  69. static const struct rhashtable_params ht_parms = {
  70. .nelem_hint = GFS2_GL_HASH_SIZE * 3 / 4,
  71. .key_len = offsetofend(struct lm_lockname, ln_type),
  72. .key_offset = offsetof(struct gfs2_glock, gl_name),
  73. .head_offset = offsetof(struct gfs2_glock, gl_node),
  74. };
  75. static struct rhashtable gl_hash_table;
  76. #define GLOCK_WAIT_TABLE_BITS 12
  77. #define GLOCK_WAIT_TABLE_SIZE (1 << GLOCK_WAIT_TABLE_BITS)
  78. static wait_queue_head_t glock_wait_table[GLOCK_WAIT_TABLE_SIZE] __cacheline_aligned;
  79. struct wait_glock_queue {
  80. struct lm_lockname *name;
  81. wait_queue_entry_t wait;
  82. };
  83. static int glock_wake_function(wait_queue_entry_t *wait, unsigned int mode,
  84. int sync, void *key)
  85. {
  86. struct wait_glock_queue *wait_glock =
  87. container_of(wait, struct wait_glock_queue, wait);
  88. struct lm_lockname *wait_name = wait_glock->name;
  89. struct lm_lockname *wake_name = key;
  90. if (wake_name->ln_sbd != wait_name->ln_sbd ||
  91. wake_name->ln_number != wait_name->ln_number ||
  92. wake_name->ln_type != wait_name->ln_type)
  93. return 0;
  94. return autoremove_wake_function(wait, mode, sync, key);
  95. }
  96. static wait_queue_head_t *glock_waitqueue(struct lm_lockname *name)
  97. {
  98. u32 hash = jhash2((u32 *)name, ht_parms.key_len / 4, 0);
  99. return glock_wait_table + hash_32(hash, GLOCK_WAIT_TABLE_BITS);
  100. }
  101. /**
  102. * wake_up_glock - Wake up waiters on a glock
  103. * @gl: the glock
  104. */
  105. static void wake_up_glock(struct gfs2_glock *gl)
  106. {
  107. wait_queue_head_t *wq = glock_waitqueue(&gl->gl_name);
  108. if (waitqueue_active(wq))
  109. __wake_up(wq, TASK_NORMAL, 1, &gl->gl_name);
  110. }
  111. static void gfs2_glock_dealloc(struct rcu_head *rcu)
  112. {
  113. struct gfs2_glock *gl = container_of(rcu, struct gfs2_glock, gl_rcu);
  114. kfree(gl->gl_lksb.sb_lvbptr);
  115. if (gl->gl_ops->go_flags & GLOF_ASPACE) {
  116. struct gfs2_glock_aspace *gla =
  117. container_of(gl, struct gfs2_glock_aspace, glock);
  118. kmem_cache_free(gfs2_glock_aspace_cachep, gla);
  119. } else
  120. kmem_cache_free(gfs2_glock_cachep, gl);
  121. }
  122. /**
  123. * glock_blocked_by_withdraw - determine if we can still use a glock
  124. * @gl: the glock
  125. *
  126. * We need to allow some glocks to be enqueued, dequeued, promoted, and demoted
  127. * when we're withdrawn. For example, to maintain metadata integrity, we should
  128. * disallow the use of inode and rgrp glocks when withdrawn. Other glocks, like
  129. * iopen or the transaction glocks may be safely used because none of their
  130. * metadata goes through the journal. So in general, we should disallow all
  131. * glocks that are journaled, and allow all the others. One exception is:
  132. * we need to allow our active journal to be promoted and demoted so others
  133. * may recover it and we can reacquire it when they're done.
  134. */
  135. static bool glock_blocked_by_withdraw(struct gfs2_glock *gl)
  136. {
  137. struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
  138. if (likely(!gfs2_withdrawn(sdp)))
  139. return false;
  140. if (gl->gl_ops->go_flags & GLOF_NONDISK)
  141. return false;
  142. if (!sdp->sd_jdesc ||
  143. gl->gl_name.ln_number == sdp->sd_jdesc->jd_no_addr)
  144. return false;
  145. return true;
  146. }
  147. void gfs2_glock_free(struct gfs2_glock *gl)
  148. {
  149. struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
  150. gfs2_glock_assert_withdraw(gl, atomic_read(&gl->gl_revokes) == 0);
  151. rhashtable_remove_fast(&gl_hash_table, &gl->gl_node, ht_parms);
  152. smp_mb();
  153. wake_up_glock(gl);
  154. call_rcu(&gl->gl_rcu, gfs2_glock_dealloc);
  155. if (atomic_dec_and_test(&sdp->sd_glock_disposal))
  156. wake_up(&sdp->sd_glock_wait);
  157. }
  158. /**
  159. * gfs2_glock_hold() - increment reference count on glock
  160. * @gl: The glock to hold
  161. *
  162. */
  163. void gfs2_glock_hold(struct gfs2_glock *gl)
  164. {
  165. GLOCK_BUG_ON(gl, __lockref_is_dead(&gl->gl_lockref));
  166. lockref_get(&gl->gl_lockref);
  167. }
  168. /**
  169. * demote_ok - Check to see if it's ok to unlock a glock
  170. * @gl: the glock
  171. *
  172. * Returns: 1 if it's ok
  173. */
  174. static int demote_ok(const struct gfs2_glock *gl)
  175. {
  176. const struct gfs2_glock_operations *glops = gl->gl_ops;
  177. if (gl->gl_state == LM_ST_UNLOCKED)
  178. return 0;
  179. /*
  180. * Note that demote_ok is used for the lru process of disposing of
  181. * glocks. For this purpose, we don't care if the glock's holders
  182. * have the HIF_MAY_DEMOTE flag set or not. If someone is using
  183. * them, don't demote.
  184. */
  185. if (!list_empty(&gl->gl_holders))
  186. return 0;
  187. if (glops->go_demote_ok)
  188. return glops->go_demote_ok(gl);
  189. return 1;
  190. }
  191. void gfs2_glock_add_to_lru(struct gfs2_glock *gl)
  192. {
  193. if (!(gl->gl_ops->go_flags & GLOF_LRU))
  194. return;
  195. spin_lock(&lru_lock);
  196. list_move_tail(&gl->gl_lru, &lru_list);
  197. if (!test_bit(GLF_LRU, &gl->gl_flags)) {
  198. set_bit(GLF_LRU, &gl->gl_flags);
  199. atomic_inc(&lru_count);
  200. }
  201. spin_unlock(&lru_lock);
  202. }
  203. static void gfs2_glock_remove_from_lru(struct gfs2_glock *gl)
  204. {
  205. if (!(gl->gl_ops->go_flags & GLOF_LRU))
  206. return;
  207. spin_lock(&lru_lock);
  208. if (test_bit(GLF_LRU, &gl->gl_flags)) {
  209. list_del_init(&gl->gl_lru);
  210. atomic_dec(&lru_count);
  211. clear_bit(GLF_LRU, &gl->gl_flags);
  212. }
  213. spin_unlock(&lru_lock);
  214. }
  215. /*
  216. * Enqueue the glock on the work queue. Passes one glock reference on to the
  217. * work queue.
  218. */
  219. static void __gfs2_glock_queue_work(struct gfs2_glock *gl, unsigned long delay) {
  220. if (!queue_delayed_work(glock_workqueue, &gl->gl_work, delay)) {
  221. /*
  222. * We are holding the lockref spinlock, and the work was still
  223. * queued above. The queued work (glock_work_func) takes that
  224. * spinlock before dropping its glock reference(s), so it
  225. * cannot have dropped them in the meantime.
  226. */
  227. GLOCK_BUG_ON(gl, gl->gl_lockref.count < 2);
  228. gl->gl_lockref.count--;
  229. }
  230. }
  231. static void gfs2_glock_queue_work(struct gfs2_glock *gl, unsigned long delay) {
  232. spin_lock(&gl->gl_lockref.lock);
  233. __gfs2_glock_queue_work(gl, delay);
  234. spin_unlock(&gl->gl_lockref.lock);
  235. }
  236. static void __gfs2_glock_put(struct gfs2_glock *gl)
  237. {
  238. struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
  239. struct address_space *mapping = gfs2_glock2aspace(gl);
  240. lockref_mark_dead(&gl->gl_lockref);
  241. gfs2_glock_remove_from_lru(gl);
  242. spin_unlock(&gl->gl_lockref.lock);
  243. GLOCK_BUG_ON(gl, !list_empty(&gl->gl_holders));
  244. if (mapping) {
  245. truncate_inode_pages_final(mapping);
  246. if (!gfs2_withdrawn(sdp))
  247. GLOCK_BUG_ON(gl, !mapping_empty(mapping));
  248. }
  249. trace_gfs2_glock_put(gl);
  250. sdp->sd_lockstruct.ls_ops->lm_put_lock(gl);
  251. }
  252. /*
  253. * Cause the glock to be put in work queue context.
  254. */
  255. void gfs2_glock_queue_put(struct gfs2_glock *gl)
  256. {
  257. gfs2_glock_queue_work(gl, 0);
  258. }
  259. /**
  260. * gfs2_glock_put() - Decrement reference count on glock
  261. * @gl: The glock to put
  262. *
  263. */
  264. void gfs2_glock_put(struct gfs2_glock *gl)
  265. {
  266. if (lockref_put_or_lock(&gl->gl_lockref))
  267. return;
  268. __gfs2_glock_put(gl);
  269. }
  270. /**
  271. * may_grant - check if it's ok to grant a new lock
  272. * @gl: The glock
  273. * @current_gh: One of the current holders of @gl
  274. * @gh: The lock request which we wish to grant
  275. *
  276. * With our current compatibility rules, if a glock has one or more active
  277. * holders (HIF_HOLDER flag set), any of those holders can be passed in as
  278. * @current_gh; they are all the same as far as compatibility with the new @gh
  279. * goes.
  280. *
  281. * Returns true if it's ok to grant the lock.
  282. */
  283. static inline bool may_grant(struct gfs2_glock *gl,
  284. struct gfs2_holder *current_gh,
  285. struct gfs2_holder *gh)
  286. {
  287. if (current_gh) {
  288. GLOCK_BUG_ON(gl, !test_bit(HIF_HOLDER, &current_gh->gh_iflags));
  289. switch(current_gh->gh_state) {
  290. case LM_ST_EXCLUSIVE:
  291. /*
  292. * Here we make a special exception to grant holders
  293. * who agree to share the EX lock with other holders
  294. * who also have the bit set. If the original holder
  295. * has the LM_FLAG_NODE_SCOPE bit set, we grant more
  296. * holders with the bit set.
  297. */
  298. return gh->gh_state == LM_ST_EXCLUSIVE &&
  299. (current_gh->gh_flags & LM_FLAG_NODE_SCOPE) &&
  300. (gh->gh_flags & LM_FLAG_NODE_SCOPE);
  301. case LM_ST_SHARED:
  302. case LM_ST_DEFERRED:
  303. return gh->gh_state == current_gh->gh_state;
  304. default:
  305. return false;
  306. }
  307. }
  308. if (gl->gl_state == gh->gh_state)
  309. return true;
  310. if (gh->gh_flags & GL_EXACT)
  311. return false;
  312. if (gl->gl_state == LM_ST_EXCLUSIVE) {
  313. return gh->gh_state == LM_ST_SHARED ||
  314. gh->gh_state == LM_ST_DEFERRED;
  315. }
  316. if (gh->gh_flags & LM_FLAG_ANY)
  317. return gl->gl_state != LM_ST_UNLOCKED;
  318. return false;
  319. }
  320. static void gfs2_holder_wake(struct gfs2_holder *gh)
  321. {
  322. clear_bit(HIF_WAIT, &gh->gh_iflags);
  323. smp_mb__after_atomic();
  324. wake_up_bit(&gh->gh_iflags, HIF_WAIT);
  325. if (gh->gh_flags & GL_ASYNC) {
  326. struct gfs2_sbd *sdp = gh->gh_gl->gl_name.ln_sbd;
  327. wake_up(&sdp->sd_async_glock_wait);
  328. }
  329. }
  330. /**
  331. * do_error - Something unexpected has happened during a lock request
  332. * @gl: The glock
  333. * @ret: The status from the DLM
  334. */
  335. static void do_error(struct gfs2_glock *gl, const int ret)
  336. {
  337. struct gfs2_holder *gh, *tmp;
  338. list_for_each_entry_safe(gh, tmp, &gl->gl_holders, gh_list) {
  339. if (!test_bit(HIF_WAIT, &gh->gh_iflags))
  340. continue;
  341. if (ret & LM_OUT_ERROR)
  342. gh->gh_error = -EIO;
  343. else if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB))
  344. gh->gh_error = GLR_TRYFAILED;
  345. else
  346. continue;
  347. list_del_init(&gh->gh_list);
  348. trace_gfs2_glock_queue(gh, 0);
  349. gfs2_holder_wake(gh);
  350. }
  351. }
  352. /**
  353. * demote_incompat_holders - demote incompatible demoteable holders
  354. * @gl: the glock we want to promote
  355. * @current_gh: the newly promoted holder
  356. *
  357. * We're passing the newly promoted holder in @current_gh, but actually, any of
  358. * the strong holders would do.
  359. */
  360. static void demote_incompat_holders(struct gfs2_glock *gl,
  361. struct gfs2_holder *current_gh)
  362. {
  363. struct gfs2_holder *gh, *tmp;
  364. /*
  365. * Demote incompatible holders before we make ourselves eligible.
  366. * (This holder may or may not allow auto-demoting, but we don't want
  367. * to demote the new holder before it's even granted.)
  368. */
  369. list_for_each_entry_safe(gh, tmp, &gl->gl_holders, gh_list) {
  370. /*
  371. * Since holders are at the front of the list, we stop when we
  372. * find the first non-holder.
  373. */
  374. if (!test_bit(HIF_HOLDER, &gh->gh_iflags))
  375. return;
  376. if (gh == current_gh)
  377. continue;
  378. if (test_bit(HIF_MAY_DEMOTE, &gh->gh_iflags) &&
  379. !may_grant(gl, current_gh, gh)) {
  380. /*
  381. * We should not recurse into do_promote because
  382. * __gfs2_glock_dq only calls handle_callback,
  383. * gfs2_glock_add_to_lru and __gfs2_glock_queue_work.
  384. */
  385. __gfs2_glock_dq(gh);
  386. }
  387. }
  388. }
  389. /**
  390. * find_first_holder - find the first "holder" gh
  391. * @gl: the glock
  392. */
  393. static inline struct gfs2_holder *find_first_holder(const struct gfs2_glock *gl)
  394. {
  395. struct gfs2_holder *gh;
  396. if (!list_empty(&gl->gl_holders)) {
  397. gh = list_first_entry(&gl->gl_holders, struct gfs2_holder,
  398. gh_list);
  399. if (test_bit(HIF_HOLDER, &gh->gh_iflags))
  400. return gh;
  401. }
  402. return NULL;
  403. }
  404. /**
  405. * find_first_strong_holder - find the first non-demoteable holder
  406. * @gl: the glock
  407. *
  408. * Find the first holder that doesn't have the HIF_MAY_DEMOTE flag set.
  409. */
  410. static inline struct gfs2_holder *
  411. find_first_strong_holder(struct gfs2_glock *gl)
  412. {
  413. struct gfs2_holder *gh;
  414. list_for_each_entry(gh, &gl->gl_holders, gh_list) {
  415. if (!test_bit(HIF_HOLDER, &gh->gh_iflags))
  416. return NULL;
  417. if (!test_bit(HIF_MAY_DEMOTE, &gh->gh_iflags))
  418. return gh;
  419. }
  420. return NULL;
  421. }
  422. /*
  423. * gfs2_instantiate - Call the glops instantiate function
  424. * @gh: The glock holder
  425. *
  426. * Returns: 0 if instantiate was successful, or error.
  427. */
  428. int gfs2_instantiate(struct gfs2_holder *gh)
  429. {
  430. struct gfs2_glock *gl = gh->gh_gl;
  431. const struct gfs2_glock_operations *glops = gl->gl_ops;
  432. int ret;
  433. again:
  434. if (!test_bit(GLF_INSTANTIATE_NEEDED, &gl->gl_flags))
  435. goto done;
  436. /*
  437. * Since we unlock the lockref lock, we set a flag to indicate
  438. * instantiate is in progress.
  439. */
  440. if (test_and_set_bit(GLF_INSTANTIATE_IN_PROG, &gl->gl_flags)) {
  441. wait_on_bit(&gl->gl_flags, GLF_INSTANTIATE_IN_PROG,
  442. TASK_UNINTERRUPTIBLE);
  443. /*
  444. * Here we just waited for a different instantiate to finish.
  445. * But that may not have been successful, as when a process
  446. * locks an inode glock _before_ it has an actual inode to
  447. * instantiate into. So we check again. This process might
  448. * have an inode to instantiate, so might be successful.
  449. */
  450. goto again;
  451. }
  452. ret = glops->go_instantiate(gl);
  453. if (!ret)
  454. clear_bit(GLF_INSTANTIATE_NEEDED, &gl->gl_flags);
  455. clear_and_wake_up_bit(GLF_INSTANTIATE_IN_PROG, &gl->gl_flags);
  456. if (ret)
  457. return ret;
  458. done:
  459. if (glops->go_held)
  460. return glops->go_held(gh);
  461. return 0;
  462. }
  463. /**
  464. * do_promote - promote as many requests as possible on the current queue
  465. * @gl: The glock
  466. *
  467. * Returns: 1 if there is a blocked holder at the head of the list
  468. */
  469. static int do_promote(struct gfs2_glock *gl)
  470. {
  471. struct gfs2_holder *gh, *current_gh;
  472. bool incompat_holders_demoted = false;
  473. current_gh = find_first_strong_holder(gl);
  474. list_for_each_entry(gh, &gl->gl_holders, gh_list) {
  475. if (test_bit(HIF_HOLDER, &gh->gh_iflags))
  476. continue;
  477. if (!may_grant(gl, current_gh, gh)) {
  478. /*
  479. * If we get here, it means we may not grant this
  480. * holder for some reason. If this holder is at the
  481. * head of the list, it means we have a blocked holder
  482. * at the head, so return 1.
  483. */
  484. if (list_is_first(&gh->gh_list, &gl->gl_holders))
  485. return 1;
  486. do_error(gl, 0);
  487. break;
  488. }
  489. set_bit(HIF_HOLDER, &gh->gh_iflags);
  490. trace_gfs2_promote(gh);
  491. gfs2_holder_wake(gh);
  492. if (!incompat_holders_demoted) {
  493. current_gh = gh;
  494. demote_incompat_holders(gl, current_gh);
  495. incompat_holders_demoted = true;
  496. }
  497. }
  498. return 0;
  499. }
  500. /**
  501. * find_first_waiter - find the first gh that's waiting for the glock
  502. * @gl: the glock
  503. */
  504. static inline struct gfs2_holder *find_first_waiter(const struct gfs2_glock *gl)
  505. {
  506. struct gfs2_holder *gh;
  507. list_for_each_entry(gh, &gl->gl_holders, gh_list) {
  508. if (!test_bit(HIF_HOLDER, &gh->gh_iflags))
  509. return gh;
  510. }
  511. return NULL;
  512. }
  513. /**
  514. * state_change - record that the glock is now in a different state
  515. * @gl: the glock
  516. * @new_state: the new state
  517. */
  518. static void state_change(struct gfs2_glock *gl, unsigned int new_state)
  519. {
  520. int held1, held2;
  521. held1 = (gl->gl_state != LM_ST_UNLOCKED);
  522. held2 = (new_state != LM_ST_UNLOCKED);
  523. if (held1 != held2) {
  524. GLOCK_BUG_ON(gl, __lockref_is_dead(&gl->gl_lockref));
  525. if (held2)
  526. gl->gl_lockref.count++;
  527. else
  528. gl->gl_lockref.count--;
  529. }
  530. if (new_state != gl->gl_target)
  531. /* shorten our minimum hold time */
  532. gl->gl_hold_time = max(gl->gl_hold_time - GL_GLOCK_HOLD_DECR,
  533. GL_GLOCK_MIN_HOLD);
  534. gl->gl_state = new_state;
  535. gl->gl_tchange = jiffies;
  536. }
  537. static void gfs2_set_demote(struct gfs2_glock *gl)
  538. {
  539. struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
  540. set_bit(GLF_DEMOTE, &gl->gl_flags);
  541. smp_mb();
  542. wake_up(&sdp->sd_async_glock_wait);
  543. }
  544. static void gfs2_demote_wake(struct gfs2_glock *gl)
  545. {
  546. gl->gl_demote_state = LM_ST_EXCLUSIVE;
  547. clear_bit(GLF_DEMOTE, &gl->gl_flags);
  548. smp_mb__after_atomic();
  549. wake_up_bit(&gl->gl_flags, GLF_DEMOTE);
  550. }
  551. /**
  552. * finish_xmote - The DLM has replied to one of our lock requests
  553. * @gl: The glock
  554. * @ret: The status from the DLM
  555. *
  556. */
  557. static void finish_xmote(struct gfs2_glock *gl, unsigned int ret)
  558. {
  559. const struct gfs2_glock_operations *glops = gl->gl_ops;
  560. struct gfs2_holder *gh;
  561. unsigned state = ret & LM_OUT_ST_MASK;
  562. spin_lock(&gl->gl_lockref.lock);
  563. trace_gfs2_glock_state_change(gl, state);
  564. state_change(gl, state);
  565. gh = find_first_waiter(gl);
  566. /* Demote to UN request arrived during demote to SH or DF */
  567. if (test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags) &&
  568. state != LM_ST_UNLOCKED && gl->gl_demote_state == LM_ST_UNLOCKED)
  569. gl->gl_target = LM_ST_UNLOCKED;
  570. /* Check for state != intended state */
  571. if (unlikely(state != gl->gl_target)) {
  572. if (gh && (ret & LM_OUT_CANCELED))
  573. gfs2_holder_wake(gh);
  574. if (gh && !test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags)) {
  575. /* move to back of queue and try next entry */
  576. if (ret & LM_OUT_CANCELED) {
  577. if ((gh->gh_flags & LM_FLAG_PRIORITY) == 0)
  578. list_move_tail(&gh->gh_list, &gl->gl_holders);
  579. gh = find_first_waiter(gl);
  580. gl->gl_target = gh->gh_state;
  581. goto retry;
  582. }
  583. /* Some error or failed "try lock" - report it */
  584. if ((ret & LM_OUT_ERROR) ||
  585. (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB))) {
  586. gl->gl_target = gl->gl_state;
  587. do_error(gl, ret);
  588. goto out;
  589. }
  590. }
  591. switch(state) {
  592. /* Unlocked due to conversion deadlock, try again */
  593. case LM_ST_UNLOCKED:
  594. retry:
  595. do_xmote(gl, gh, gl->gl_target);
  596. break;
  597. /* Conversion fails, unlock and try again */
  598. case LM_ST_SHARED:
  599. case LM_ST_DEFERRED:
  600. do_xmote(gl, gh, LM_ST_UNLOCKED);
  601. break;
  602. default: /* Everything else */
  603. fs_err(gl->gl_name.ln_sbd, "wanted %u got %u\n",
  604. gl->gl_target, state);
  605. GLOCK_BUG_ON(gl, 1);
  606. }
  607. spin_unlock(&gl->gl_lockref.lock);
  608. return;
  609. }
  610. /* Fast path - we got what we asked for */
  611. if (test_and_clear_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags))
  612. gfs2_demote_wake(gl);
  613. if (state != LM_ST_UNLOCKED) {
  614. if (glops->go_xmote_bh) {
  615. int rv;
  616. spin_unlock(&gl->gl_lockref.lock);
  617. rv = glops->go_xmote_bh(gl);
  618. spin_lock(&gl->gl_lockref.lock);
  619. if (rv) {
  620. do_error(gl, rv);
  621. goto out;
  622. }
  623. }
  624. do_promote(gl);
  625. }
  626. out:
  627. clear_bit(GLF_LOCK, &gl->gl_flags);
  628. spin_unlock(&gl->gl_lockref.lock);
  629. }
  630. static bool is_system_glock(struct gfs2_glock *gl)
  631. {
  632. struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
  633. struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode);
  634. if (gl == m_ip->i_gl)
  635. return true;
  636. return false;
  637. }
  638. /**
  639. * do_xmote - Calls the DLM to change the state of a lock
  640. * @gl: The lock state
  641. * @gh: The holder (only for promotes)
  642. * @target: The target lock state
  643. *
  644. */
  645. static void do_xmote(struct gfs2_glock *gl, struct gfs2_holder *gh,
  646. unsigned int target)
  647. __releases(&gl->gl_lockref.lock)
  648. __acquires(&gl->gl_lockref.lock)
  649. {
  650. const struct gfs2_glock_operations *glops = gl->gl_ops;
  651. struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
  652. unsigned int lck_flags = (unsigned int)(gh ? gh->gh_flags : 0);
  653. int ret;
  654. if (target != LM_ST_UNLOCKED && glock_blocked_by_withdraw(gl) &&
  655. gh && !(gh->gh_flags & LM_FLAG_NOEXP))
  656. goto skip_inval;
  657. lck_flags &= (LM_FLAG_TRY | LM_FLAG_TRY_1CB | LM_FLAG_NOEXP |
  658. LM_FLAG_PRIORITY);
  659. GLOCK_BUG_ON(gl, gl->gl_state == target);
  660. GLOCK_BUG_ON(gl, gl->gl_state == gl->gl_target);
  661. if ((target == LM_ST_UNLOCKED || target == LM_ST_DEFERRED) &&
  662. glops->go_inval) {
  663. /*
  664. * If another process is already doing the invalidate, let that
  665. * finish first. The glock state machine will get back to this
  666. * holder again later.
  667. */
  668. if (test_and_set_bit(GLF_INVALIDATE_IN_PROGRESS,
  669. &gl->gl_flags))
  670. return;
  671. do_error(gl, 0); /* Fail queued try locks */
  672. }
  673. gl->gl_req = target;
  674. set_bit(GLF_BLOCKING, &gl->gl_flags);
  675. if ((gl->gl_req == LM_ST_UNLOCKED) ||
  676. (gl->gl_state == LM_ST_EXCLUSIVE) ||
  677. (lck_flags & (LM_FLAG_TRY|LM_FLAG_TRY_1CB)))
  678. clear_bit(GLF_BLOCKING, &gl->gl_flags);
  679. spin_unlock(&gl->gl_lockref.lock);
  680. if (glops->go_sync) {
  681. ret = glops->go_sync(gl);
  682. /* If we had a problem syncing (due to io errors or whatever,
  683. * we should not invalidate the metadata or tell dlm to
  684. * release the glock to other nodes.
  685. */
  686. if (ret) {
  687. if (cmpxchg(&sdp->sd_log_error, 0, ret)) {
  688. fs_err(sdp, "Error %d syncing glock \n", ret);
  689. gfs2_dump_glock(NULL, gl, true);
  690. }
  691. goto skip_inval;
  692. }
  693. }
  694. if (test_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags)) {
  695. /*
  696. * The call to go_sync should have cleared out the ail list.
  697. * If there are still items, we have a problem. We ought to
  698. * withdraw, but we can't because the withdraw code also uses
  699. * glocks. Warn about the error, dump the glock, then fall
  700. * through and wait for logd to do the withdraw for us.
  701. */
  702. if ((atomic_read(&gl->gl_ail_count) != 0) &&
  703. (!cmpxchg(&sdp->sd_log_error, 0, -EIO))) {
  704. gfs2_glock_assert_warn(gl,
  705. !atomic_read(&gl->gl_ail_count));
  706. gfs2_dump_glock(NULL, gl, true);
  707. }
  708. glops->go_inval(gl, target == LM_ST_DEFERRED ? 0 : DIO_METADATA);
  709. clear_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags);
  710. }
  711. skip_inval:
  712. gfs2_glock_hold(gl);
  713. /*
  714. * Check for an error encountered since we called go_sync and go_inval.
  715. * If so, we can't withdraw from the glock code because the withdraw
  716. * code itself uses glocks (see function signal_our_withdraw) to
  717. * change the mount to read-only. Most importantly, we must not call
  718. * dlm to unlock the glock until the journal is in a known good state
  719. * (after journal replay) otherwise other nodes may use the object
  720. * (rgrp or dinode) and then later, journal replay will corrupt the
  721. * file system. The best we can do here is wait for the logd daemon
  722. * to see sd_log_error and withdraw, and in the meantime, requeue the
  723. * work for later.
  724. *
  725. * We make a special exception for some system glocks, such as the
  726. * system statfs inode glock, which needs to be granted before the
  727. * gfs2_quotad daemon can exit, and that exit needs to finish before
  728. * we can unmount the withdrawn file system.
  729. *
  730. * However, if we're just unlocking the lock (say, for unmount, when
  731. * gfs2_gl_hash_clear calls clear_glock) and recovery is complete
  732. * then it's okay to tell dlm to unlock it.
  733. */
  734. if (unlikely(sdp->sd_log_error && !gfs2_withdrawn(sdp)))
  735. gfs2_withdraw_delayed(sdp);
  736. if (glock_blocked_by_withdraw(gl) &&
  737. (target != LM_ST_UNLOCKED ||
  738. test_bit(SDF_WITHDRAW_RECOVERY, &sdp->sd_flags))) {
  739. if (!is_system_glock(gl)) {
  740. handle_callback(gl, LM_ST_UNLOCKED, 0, false); /* sets demote */
  741. /*
  742. * Ordinarily, we would call dlm and its callback would call
  743. * finish_xmote, which would call state_change() to the new state.
  744. * Since we withdrew, we won't call dlm, so call state_change
  745. * manually, but to the UNLOCKED state we desire.
  746. */
  747. state_change(gl, LM_ST_UNLOCKED);
  748. /*
  749. * We skip telling dlm to do the locking, so we won't get a
  750. * reply that would otherwise clear GLF_LOCK. So we clear it here.
  751. */
  752. clear_bit(GLF_LOCK, &gl->gl_flags);
  753. clear_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags);
  754. gfs2_glock_queue_work(gl, GL_GLOCK_DFT_HOLD);
  755. goto out;
  756. } else {
  757. clear_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags);
  758. }
  759. }
  760. if (sdp->sd_lockstruct.ls_ops->lm_lock) {
  761. /* lock_dlm */
  762. ret = sdp->sd_lockstruct.ls_ops->lm_lock(gl, target, lck_flags);
  763. if (ret == -EINVAL && gl->gl_target == LM_ST_UNLOCKED &&
  764. target == LM_ST_UNLOCKED &&
  765. test_bit(SDF_SKIP_DLM_UNLOCK, &sdp->sd_flags)) {
  766. finish_xmote(gl, target);
  767. gfs2_glock_queue_work(gl, 0);
  768. } else if (ret) {
  769. fs_err(sdp, "lm_lock ret %d\n", ret);
  770. GLOCK_BUG_ON(gl, !gfs2_withdrawn(sdp));
  771. }
  772. } else { /* lock_nolock */
  773. finish_xmote(gl, target);
  774. gfs2_glock_queue_work(gl, 0);
  775. }
  776. out:
  777. spin_lock(&gl->gl_lockref.lock);
  778. }
  779. /**
  780. * run_queue - do all outstanding tasks related to a glock
  781. * @gl: The glock in question
  782. * @nonblock: True if we must not block in run_queue
  783. *
  784. */
  785. static void run_queue(struct gfs2_glock *gl, const int nonblock)
  786. __releases(&gl->gl_lockref.lock)
  787. __acquires(&gl->gl_lockref.lock)
  788. {
  789. struct gfs2_holder *gh = NULL;
  790. if (test_and_set_bit(GLF_LOCK, &gl->gl_flags))
  791. return;
  792. GLOCK_BUG_ON(gl, test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags));
  793. if (test_bit(GLF_DEMOTE, &gl->gl_flags) &&
  794. gl->gl_demote_state != gl->gl_state) {
  795. if (find_first_holder(gl))
  796. goto out_unlock;
  797. if (nonblock)
  798. goto out_sched;
  799. set_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags);
  800. GLOCK_BUG_ON(gl, gl->gl_demote_state == LM_ST_EXCLUSIVE);
  801. gl->gl_target = gl->gl_demote_state;
  802. } else {
  803. if (test_bit(GLF_DEMOTE, &gl->gl_flags))
  804. gfs2_demote_wake(gl);
  805. if (do_promote(gl) == 0)
  806. goto out_unlock;
  807. gh = find_first_waiter(gl);
  808. gl->gl_target = gh->gh_state;
  809. if (!(gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)))
  810. do_error(gl, 0); /* Fail queued try locks */
  811. }
  812. do_xmote(gl, gh, gl->gl_target);
  813. return;
  814. out_sched:
  815. clear_bit(GLF_LOCK, &gl->gl_flags);
  816. smp_mb__after_atomic();
  817. gl->gl_lockref.count++;
  818. __gfs2_glock_queue_work(gl, 0);
  819. return;
  820. out_unlock:
  821. clear_bit(GLF_LOCK, &gl->gl_flags);
  822. smp_mb__after_atomic();
  823. return;
  824. }
  825. void gfs2_inode_remember_delete(struct gfs2_glock *gl, u64 generation)
  826. {
  827. struct gfs2_inode_lvb *ri = (void *)gl->gl_lksb.sb_lvbptr;
  828. if (ri->ri_magic == 0)
  829. ri->ri_magic = cpu_to_be32(GFS2_MAGIC);
  830. if (ri->ri_magic == cpu_to_be32(GFS2_MAGIC))
  831. ri->ri_generation_deleted = cpu_to_be64(generation);
  832. }
  833. bool gfs2_inode_already_deleted(struct gfs2_glock *gl, u64 generation)
  834. {
  835. struct gfs2_inode_lvb *ri = (void *)gl->gl_lksb.sb_lvbptr;
  836. if (ri->ri_magic != cpu_to_be32(GFS2_MAGIC))
  837. return false;
  838. return generation <= be64_to_cpu(ri->ri_generation_deleted);
  839. }
  840. static void gfs2_glock_poke(struct gfs2_glock *gl)
  841. {
  842. int flags = LM_FLAG_TRY_1CB | LM_FLAG_ANY | GL_SKIP;
  843. struct gfs2_holder gh;
  844. int error;
  845. __gfs2_holder_init(gl, LM_ST_SHARED, flags, &gh, _RET_IP_);
  846. error = gfs2_glock_nq(&gh);
  847. if (!error)
  848. gfs2_glock_dq(&gh);
  849. gfs2_holder_uninit(&gh);
  850. }
  851. static bool gfs2_try_evict(struct gfs2_glock *gl)
  852. {
  853. struct gfs2_inode *ip;
  854. bool evicted = false;
  855. /*
  856. * If there is contention on the iopen glock and we have an inode, try
  857. * to grab and release the inode so that it can be evicted. This will
  858. * allow the remote node to go ahead and delete the inode without us
  859. * having to do it, which will avoid rgrp glock thrashing.
  860. *
  861. * The remote node is likely still holding the corresponding inode
  862. * glock, so it will run before we get to verify that the delete has
  863. * happened below.
  864. */
  865. spin_lock(&gl->gl_lockref.lock);
  866. ip = gl->gl_object;
  867. if (ip && !igrab(&ip->i_inode))
  868. ip = NULL;
  869. spin_unlock(&gl->gl_lockref.lock);
  870. if (ip) {
  871. struct gfs2_glock *inode_gl = NULL;
  872. gl->gl_no_formal_ino = ip->i_no_formal_ino;
  873. set_bit(GIF_DEFERRED_DELETE, &ip->i_flags);
  874. d_prune_aliases(&ip->i_inode);
  875. iput(&ip->i_inode);
  876. /* If the inode was evicted, gl->gl_object will now be NULL. */
  877. spin_lock(&gl->gl_lockref.lock);
  878. ip = gl->gl_object;
  879. if (ip) {
  880. inode_gl = ip->i_gl;
  881. lockref_get(&inode_gl->gl_lockref);
  882. clear_bit(GIF_DEFERRED_DELETE, &ip->i_flags);
  883. }
  884. spin_unlock(&gl->gl_lockref.lock);
  885. if (inode_gl) {
  886. gfs2_glock_poke(inode_gl);
  887. gfs2_glock_put(inode_gl);
  888. }
  889. evicted = !ip;
  890. }
  891. return evicted;
  892. }
  893. static void delete_work_func(struct work_struct *work)
  894. {
  895. struct delayed_work *dwork = to_delayed_work(work);
  896. struct gfs2_glock *gl = container_of(dwork, struct gfs2_glock, gl_delete);
  897. struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
  898. struct inode *inode;
  899. u64 no_addr = gl->gl_name.ln_number;
  900. spin_lock(&gl->gl_lockref.lock);
  901. clear_bit(GLF_PENDING_DELETE, &gl->gl_flags);
  902. spin_unlock(&gl->gl_lockref.lock);
  903. if (test_bit(GLF_DEMOTE, &gl->gl_flags)) {
  904. /*
  905. * If we can evict the inode, give the remote node trying to
  906. * delete the inode some time before verifying that the delete
  907. * has happened. Otherwise, if we cause contention on the inode glock
  908. * immediately, the remote node will think that we still have
  909. * the inode in use, and so it will give up waiting.
  910. *
  911. * If we can't evict the inode, signal to the remote node that
  912. * the inode is still in use. We'll later try to delete the
  913. * inode locally in gfs2_evict_inode.
  914. *
  915. * FIXME: We only need to verify that the remote node has
  916. * deleted the inode because nodes before this remote delete
  917. * rework won't cooperate. At a later time, when we no longer
  918. * care about compatibility with such nodes, we can skip this
  919. * step entirely.
  920. */
  921. if (gfs2_try_evict(gl)) {
  922. if (gfs2_queue_delete_work(gl, 5 * HZ))
  923. return;
  924. }
  925. goto out;
  926. }
  927. inode = gfs2_lookup_by_inum(sdp, no_addr, gl->gl_no_formal_ino,
  928. GFS2_BLKST_UNLINKED);
  929. if (IS_ERR(inode)) {
  930. if (PTR_ERR(inode) == -EAGAIN &&
  931. (gfs2_queue_delete_work(gl, 5 * HZ)))
  932. return;
  933. } else {
  934. d_prune_aliases(inode);
  935. iput(inode);
  936. }
  937. out:
  938. gfs2_glock_put(gl);
  939. }
  940. static void glock_work_func(struct work_struct *work)
  941. {
  942. unsigned long delay = 0;
  943. struct gfs2_glock *gl = container_of(work, struct gfs2_glock, gl_work.work);
  944. unsigned int drop_refs = 1;
  945. if (test_and_clear_bit(GLF_REPLY_PENDING, &gl->gl_flags)) {
  946. finish_xmote(gl, gl->gl_reply);
  947. drop_refs++;
  948. }
  949. spin_lock(&gl->gl_lockref.lock);
  950. if (test_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) &&
  951. gl->gl_state != LM_ST_UNLOCKED &&
  952. gl->gl_demote_state != LM_ST_EXCLUSIVE) {
  953. unsigned long holdtime, now = jiffies;
  954. holdtime = gl->gl_tchange + gl->gl_hold_time;
  955. if (time_before(now, holdtime))
  956. delay = holdtime - now;
  957. if (!delay) {
  958. clear_bit(GLF_PENDING_DEMOTE, &gl->gl_flags);
  959. gfs2_set_demote(gl);
  960. }
  961. }
  962. run_queue(gl, 0);
  963. if (delay) {
  964. /* Keep one glock reference for the work we requeue. */
  965. drop_refs--;
  966. if (gl->gl_name.ln_type != LM_TYPE_INODE)
  967. delay = 0;
  968. __gfs2_glock_queue_work(gl, delay);
  969. }
  970. /*
  971. * Drop the remaining glock references manually here. (Mind that
  972. * __gfs2_glock_queue_work depends on the lockref spinlock begin held
  973. * here as well.)
  974. */
  975. gl->gl_lockref.count -= drop_refs;
  976. if (!gl->gl_lockref.count) {
  977. __gfs2_glock_put(gl);
  978. return;
  979. }
  980. spin_unlock(&gl->gl_lockref.lock);
  981. }
  982. static struct gfs2_glock *find_insert_glock(struct lm_lockname *name,
  983. struct gfs2_glock *new)
  984. {
  985. struct wait_glock_queue wait;
  986. wait_queue_head_t *wq = glock_waitqueue(name);
  987. struct gfs2_glock *gl;
  988. wait.name = name;
  989. init_wait(&wait.wait);
  990. wait.wait.func = glock_wake_function;
  991. again:
  992. prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
  993. rcu_read_lock();
  994. if (new) {
  995. gl = rhashtable_lookup_get_insert_fast(&gl_hash_table,
  996. &new->gl_node, ht_parms);
  997. if (IS_ERR(gl))
  998. goto out;
  999. } else {
  1000. gl = rhashtable_lookup_fast(&gl_hash_table,
  1001. name, ht_parms);
  1002. }
  1003. if (gl && !lockref_get_not_dead(&gl->gl_lockref)) {
  1004. rcu_read_unlock();
  1005. schedule();
  1006. goto again;
  1007. }
  1008. out:
  1009. rcu_read_unlock();
  1010. finish_wait(wq, &wait.wait);
  1011. return gl;
  1012. }
  1013. /**
  1014. * gfs2_glock_get() - Get a glock, or create one if one doesn't exist
  1015. * @sdp: The GFS2 superblock
  1016. * @number: the lock number
  1017. * @glops: The glock_operations to use
  1018. * @create: If 0, don't create the glock if it doesn't exist
  1019. * @glp: the glock is returned here
  1020. *
  1021. * This does not lock a glock, just finds/creates structures for one.
  1022. *
  1023. * Returns: errno
  1024. */
  1025. int gfs2_glock_get(struct gfs2_sbd *sdp, u64 number,
  1026. const struct gfs2_glock_operations *glops, int create,
  1027. struct gfs2_glock **glp)
  1028. {
  1029. struct super_block *s = sdp->sd_vfs;
  1030. struct lm_lockname name = { .ln_number = number,
  1031. .ln_type = glops->go_type,
  1032. .ln_sbd = sdp };
  1033. struct gfs2_glock *gl, *tmp;
  1034. struct address_space *mapping;
  1035. int ret = 0;
  1036. gl = find_insert_glock(&name, NULL);
  1037. if (gl) {
  1038. *glp = gl;
  1039. return 0;
  1040. }
  1041. if (!create)
  1042. return -ENOENT;
  1043. if (glops->go_flags & GLOF_ASPACE) {
  1044. struct gfs2_glock_aspace *gla =
  1045. kmem_cache_alloc(gfs2_glock_aspace_cachep, GFP_NOFS);
  1046. if (!gla)
  1047. return -ENOMEM;
  1048. gl = &gla->glock;
  1049. } else {
  1050. gl = kmem_cache_alloc(gfs2_glock_cachep, GFP_NOFS);
  1051. if (!gl)
  1052. return -ENOMEM;
  1053. }
  1054. memset(&gl->gl_lksb, 0, sizeof(struct dlm_lksb));
  1055. gl->gl_ops = glops;
  1056. if (glops->go_flags & GLOF_LVB) {
  1057. gl->gl_lksb.sb_lvbptr = kzalloc(GDLM_LVB_SIZE, GFP_NOFS);
  1058. if (!gl->gl_lksb.sb_lvbptr) {
  1059. gfs2_glock_dealloc(&gl->gl_rcu);
  1060. return -ENOMEM;
  1061. }
  1062. }
  1063. atomic_inc(&sdp->sd_glock_disposal);
  1064. gl->gl_node.next = NULL;
  1065. gl->gl_flags = glops->go_instantiate ? BIT(GLF_INSTANTIATE_NEEDED) : 0;
  1066. gl->gl_name = name;
  1067. lockdep_set_subclass(&gl->gl_lockref.lock, glops->go_subclass);
  1068. gl->gl_lockref.count = 1;
  1069. gl->gl_state = LM_ST_UNLOCKED;
  1070. gl->gl_target = LM_ST_UNLOCKED;
  1071. gl->gl_demote_state = LM_ST_EXCLUSIVE;
  1072. gl->gl_dstamp = 0;
  1073. preempt_disable();
  1074. /* We use the global stats to estimate the initial per-glock stats */
  1075. gl->gl_stats = this_cpu_ptr(sdp->sd_lkstats)->lkstats[glops->go_type];
  1076. preempt_enable();
  1077. gl->gl_stats.stats[GFS2_LKS_DCOUNT] = 0;
  1078. gl->gl_stats.stats[GFS2_LKS_QCOUNT] = 0;
  1079. gl->gl_tchange = jiffies;
  1080. gl->gl_object = NULL;
  1081. gl->gl_hold_time = GL_GLOCK_DFT_HOLD;
  1082. INIT_DELAYED_WORK(&gl->gl_work, glock_work_func);
  1083. if (gl->gl_name.ln_type == LM_TYPE_IOPEN)
  1084. INIT_DELAYED_WORK(&gl->gl_delete, delete_work_func);
  1085. mapping = gfs2_glock2aspace(gl);
  1086. if (mapping) {
  1087. mapping->a_ops = &gfs2_meta_aops;
  1088. mapping->host = s->s_bdev->bd_inode;
  1089. mapping->flags = 0;
  1090. mapping_set_gfp_mask(mapping, GFP_NOFS);
  1091. mapping->private_data = NULL;
  1092. mapping->writeback_index = 0;
  1093. }
  1094. tmp = find_insert_glock(&name, gl);
  1095. if (!tmp) {
  1096. *glp = gl;
  1097. goto out;
  1098. }
  1099. if (IS_ERR(tmp)) {
  1100. ret = PTR_ERR(tmp);
  1101. goto out_free;
  1102. }
  1103. *glp = tmp;
  1104. out_free:
  1105. gfs2_glock_dealloc(&gl->gl_rcu);
  1106. if (atomic_dec_and_test(&sdp->sd_glock_disposal))
  1107. wake_up(&sdp->sd_glock_wait);
  1108. out:
  1109. return ret;
  1110. }
  1111. /**
  1112. * __gfs2_holder_init - initialize a struct gfs2_holder in the default way
  1113. * @gl: the glock
  1114. * @state: the state we're requesting
  1115. * @flags: the modifier flags
  1116. * @gh: the holder structure
  1117. *
  1118. */
  1119. void __gfs2_holder_init(struct gfs2_glock *gl, unsigned int state, u16 flags,
  1120. struct gfs2_holder *gh, unsigned long ip)
  1121. {
  1122. INIT_LIST_HEAD(&gh->gh_list);
  1123. gh->gh_gl = gl;
  1124. gh->gh_ip = ip;
  1125. gh->gh_owner_pid = get_pid(task_pid(current));
  1126. gh->gh_state = state;
  1127. gh->gh_flags = flags;
  1128. gh->gh_iflags = 0;
  1129. gfs2_glock_hold(gl);
  1130. }
  1131. /**
  1132. * gfs2_holder_reinit - reinitialize a struct gfs2_holder so we can requeue it
  1133. * @state: the state we're requesting
  1134. * @flags: the modifier flags
  1135. * @gh: the holder structure
  1136. *
  1137. * Don't mess with the glock.
  1138. *
  1139. */
  1140. void gfs2_holder_reinit(unsigned int state, u16 flags, struct gfs2_holder *gh)
  1141. {
  1142. gh->gh_state = state;
  1143. gh->gh_flags = flags;
  1144. gh->gh_iflags = 0;
  1145. gh->gh_ip = _RET_IP_;
  1146. put_pid(gh->gh_owner_pid);
  1147. gh->gh_owner_pid = get_pid(task_pid(current));
  1148. }
  1149. /**
  1150. * gfs2_holder_uninit - uninitialize a holder structure (drop glock reference)
  1151. * @gh: the holder structure
  1152. *
  1153. */
  1154. void gfs2_holder_uninit(struct gfs2_holder *gh)
  1155. {
  1156. put_pid(gh->gh_owner_pid);
  1157. gfs2_glock_put(gh->gh_gl);
  1158. gfs2_holder_mark_uninitialized(gh);
  1159. gh->gh_ip = 0;
  1160. }
  1161. static void gfs2_glock_update_hold_time(struct gfs2_glock *gl,
  1162. unsigned long start_time)
  1163. {
  1164. /* Have we waited longer that a second? */
  1165. if (time_after(jiffies, start_time + HZ)) {
  1166. /* Lengthen the minimum hold time. */
  1167. gl->gl_hold_time = min(gl->gl_hold_time + GL_GLOCK_HOLD_INCR,
  1168. GL_GLOCK_MAX_HOLD);
  1169. }
  1170. }
  1171. /**
  1172. * gfs2_glock_holder_ready - holder is ready and its error code can be collected
  1173. * @gh: the glock holder
  1174. *
  1175. * Called when a glock holder no longer needs to be waited for because it is
  1176. * now either held (HIF_HOLDER set; gh_error == 0), or acquiring the lock has
  1177. * failed (gh_error != 0).
  1178. */
  1179. int gfs2_glock_holder_ready(struct gfs2_holder *gh)
  1180. {
  1181. if (gh->gh_error || (gh->gh_flags & GL_SKIP))
  1182. return gh->gh_error;
  1183. gh->gh_error = gfs2_instantiate(gh);
  1184. if (gh->gh_error)
  1185. gfs2_glock_dq(gh);
  1186. return gh->gh_error;
  1187. }
  1188. /**
  1189. * gfs2_glock_wait - wait on a glock acquisition
  1190. * @gh: the glock holder
  1191. *
  1192. * Returns: 0 on success
  1193. */
  1194. int gfs2_glock_wait(struct gfs2_holder *gh)
  1195. {
  1196. unsigned long start_time = jiffies;
  1197. might_sleep();
  1198. wait_on_bit(&gh->gh_iflags, HIF_WAIT, TASK_UNINTERRUPTIBLE);
  1199. gfs2_glock_update_hold_time(gh->gh_gl, start_time);
  1200. return gfs2_glock_holder_ready(gh);
  1201. }
  1202. static int glocks_pending(unsigned int num_gh, struct gfs2_holder *ghs)
  1203. {
  1204. int i;
  1205. for (i = 0; i < num_gh; i++)
  1206. if (test_bit(HIF_WAIT, &ghs[i].gh_iflags))
  1207. return 1;
  1208. return 0;
  1209. }
  1210. /**
  1211. * gfs2_glock_async_wait - wait on multiple asynchronous glock acquisitions
  1212. * @num_gh: the number of holders in the array
  1213. * @ghs: the glock holder array
  1214. *
  1215. * Returns: 0 on success, meaning all glocks have been granted and are held.
  1216. * -ESTALE if the request timed out, meaning all glocks were released,
  1217. * and the caller should retry the operation.
  1218. */
  1219. int gfs2_glock_async_wait(unsigned int num_gh, struct gfs2_holder *ghs)
  1220. {
  1221. struct gfs2_sbd *sdp = ghs[0].gh_gl->gl_name.ln_sbd;
  1222. int i, ret = 0, timeout = 0;
  1223. unsigned long start_time = jiffies;
  1224. might_sleep();
  1225. /*
  1226. * Total up the (minimum hold time * 2) of all glocks and use that to
  1227. * determine the max amount of time we should wait.
  1228. */
  1229. for (i = 0; i < num_gh; i++)
  1230. timeout += ghs[i].gh_gl->gl_hold_time << 1;
  1231. if (!wait_event_timeout(sdp->sd_async_glock_wait,
  1232. !glocks_pending(num_gh, ghs), timeout)) {
  1233. ret = -ESTALE; /* request timed out. */
  1234. goto out;
  1235. }
  1236. for (i = 0; i < num_gh; i++) {
  1237. struct gfs2_holder *gh = &ghs[i];
  1238. int ret2;
  1239. if (test_bit(HIF_HOLDER, &gh->gh_iflags)) {
  1240. gfs2_glock_update_hold_time(gh->gh_gl,
  1241. start_time);
  1242. }
  1243. ret2 = gfs2_glock_holder_ready(gh);
  1244. if (!ret)
  1245. ret = ret2;
  1246. }
  1247. out:
  1248. if (ret) {
  1249. for (i = 0; i < num_gh; i++) {
  1250. struct gfs2_holder *gh = &ghs[i];
  1251. gfs2_glock_dq(gh);
  1252. }
  1253. }
  1254. return ret;
  1255. }
  1256. /**
  1257. * handle_callback - process a demote request
  1258. * @gl: the glock
  1259. * @state: the state the caller wants us to change to
  1260. * @delay: zero to demote immediately; otherwise pending demote
  1261. * @remote: true if this came from a different cluster node
  1262. *
  1263. * There are only two requests that we are going to see in actual
  1264. * practise: LM_ST_SHARED and LM_ST_UNLOCKED
  1265. */
  1266. static void handle_callback(struct gfs2_glock *gl, unsigned int state,
  1267. unsigned long delay, bool remote)
  1268. {
  1269. if (delay)
  1270. set_bit(GLF_PENDING_DEMOTE, &gl->gl_flags);
  1271. else
  1272. gfs2_set_demote(gl);
  1273. if (gl->gl_demote_state == LM_ST_EXCLUSIVE) {
  1274. gl->gl_demote_state = state;
  1275. gl->gl_demote_time = jiffies;
  1276. } else if (gl->gl_demote_state != LM_ST_UNLOCKED &&
  1277. gl->gl_demote_state != state) {
  1278. gl->gl_demote_state = LM_ST_UNLOCKED;
  1279. }
  1280. if (gl->gl_ops->go_callback)
  1281. gl->gl_ops->go_callback(gl, remote);
  1282. trace_gfs2_demote_rq(gl, remote);
  1283. }
  1284. void gfs2_print_dbg(struct seq_file *seq, const char *fmt, ...)
  1285. {
  1286. struct va_format vaf;
  1287. va_list args;
  1288. va_start(args, fmt);
  1289. if (seq) {
  1290. seq_vprintf(seq, fmt, args);
  1291. } else {
  1292. vaf.fmt = fmt;
  1293. vaf.va = &args;
  1294. pr_err("%pV", &vaf);
  1295. }
  1296. va_end(args);
  1297. }
  1298. static inline bool pid_is_meaningful(const struct gfs2_holder *gh)
  1299. {
  1300. if (!(gh->gh_flags & GL_NOPID))
  1301. return true;
  1302. if (gh->gh_state == LM_ST_UNLOCKED)
  1303. return true;
  1304. return false;
  1305. }
  1306. /**
  1307. * add_to_queue - Add a holder to the wait queue (but look for recursion)
  1308. * @gh: the holder structure to add
  1309. *
  1310. * Eventually we should move the recursive locking trap to a
  1311. * debugging option or something like that. This is the fast
  1312. * path and needs to have the minimum number of distractions.
  1313. *
  1314. */
  1315. static inline void add_to_queue(struct gfs2_holder *gh)
  1316. __releases(&gl->gl_lockref.lock)
  1317. __acquires(&gl->gl_lockref.lock)
  1318. {
  1319. struct gfs2_glock *gl = gh->gh_gl;
  1320. struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
  1321. struct list_head *insert_pt = NULL;
  1322. struct gfs2_holder *gh2;
  1323. int try_futile = 0;
  1324. GLOCK_BUG_ON(gl, gh->gh_owner_pid == NULL);
  1325. if (test_and_set_bit(HIF_WAIT, &gh->gh_iflags))
  1326. GLOCK_BUG_ON(gl, true);
  1327. if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) {
  1328. if (test_bit(GLF_LOCK, &gl->gl_flags)) {
  1329. struct gfs2_holder *current_gh;
  1330. current_gh = find_first_strong_holder(gl);
  1331. try_futile = !may_grant(gl, current_gh, gh);
  1332. }
  1333. if (test_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags))
  1334. goto fail;
  1335. }
  1336. list_for_each_entry(gh2, &gl->gl_holders, gh_list) {
  1337. if (likely(gh2->gh_owner_pid != gh->gh_owner_pid))
  1338. continue;
  1339. if (gh->gh_gl->gl_ops->go_type == LM_TYPE_FLOCK)
  1340. continue;
  1341. if (test_bit(HIF_MAY_DEMOTE, &gh2->gh_iflags))
  1342. continue;
  1343. if (!pid_is_meaningful(gh2))
  1344. continue;
  1345. goto trap_recursive;
  1346. }
  1347. list_for_each_entry(gh2, &gl->gl_holders, gh_list) {
  1348. if (try_futile &&
  1349. !(gh2->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB))) {
  1350. fail:
  1351. gh->gh_error = GLR_TRYFAILED;
  1352. gfs2_holder_wake(gh);
  1353. return;
  1354. }
  1355. if (test_bit(HIF_HOLDER, &gh2->gh_iflags))
  1356. continue;
  1357. if (unlikely((gh->gh_flags & LM_FLAG_PRIORITY) && !insert_pt))
  1358. insert_pt = &gh2->gh_list;
  1359. }
  1360. trace_gfs2_glock_queue(gh, 1);
  1361. gfs2_glstats_inc(gl, GFS2_LKS_QCOUNT);
  1362. gfs2_sbstats_inc(gl, GFS2_LKS_QCOUNT);
  1363. if (likely(insert_pt == NULL)) {
  1364. list_add_tail(&gh->gh_list, &gl->gl_holders);
  1365. if (unlikely(gh->gh_flags & LM_FLAG_PRIORITY))
  1366. goto do_cancel;
  1367. return;
  1368. }
  1369. list_add_tail(&gh->gh_list, insert_pt);
  1370. do_cancel:
  1371. gh = list_first_entry(&gl->gl_holders, struct gfs2_holder, gh_list);
  1372. if (!(gh->gh_flags & LM_FLAG_PRIORITY)) {
  1373. spin_unlock(&gl->gl_lockref.lock);
  1374. if (sdp->sd_lockstruct.ls_ops->lm_cancel)
  1375. sdp->sd_lockstruct.ls_ops->lm_cancel(gl);
  1376. spin_lock(&gl->gl_lockref.lock);
  1377. }
  1378. return;
  1379. trap_recursive:
  1380. fs_err(sdp, "original: %pSR\n", (void *)gh2->gh_ip);
  1381. fs_err(sdp, "pid: %d\n", pid_nr(gh2->gh_owner_pid));
  1382. fs_err(sdp, "lock type: %d req lock state : %d\n",
  1383. gh2->gh_gl->gl_name.ln_type, gh2->gh_state);
  1384. fs_err(sdp, "new: %pSR\n", (void *)gh->gh_ip);
  1385. fs_err(sdp, "pid: %d\n", pid_nr(gh->gh_owner_pid));
  1386. fs_err(sdp, "lock type: %d req lock state : %d\n",
  1387. gh->gh_gl->gl_name.ln_type, gh->gh_state);
  1388. gfs2_dump_glock(NULL, gl, true);
  1389. BUG();
  1390. }
  1391. /**
  1392. * gfs2_glock_nq - enqueue a struct gfs2_holder onto a glock (acquire a glock)
  1393. * @gh: the holder structure
  1394. *
  1395. * if (gh->gh_flags & GL_ASYNC), this never returns an error
  1396. *
  1397. * Returns: 0, GLR_TRYFAILED, or errno on failure
  1398. */
  1399. int gfs2_glock_nq(struct gfs2_holder *gh)
  1400. {
  1401. struct gfs2_glock *gl = gh->gh_gl;
  1402. int error = 0;
  1403. if (glock_blocked_by_withdraw(gl) && !(gh->gh_flags & LM_FLAG_NOEXP))
  1404. return -EIO;
  1405. if (test_bit(GLF_LRU, &gl->gl_flags))
  1406. gfs2_glock_remove_from_lru(gl);
  1407. gh->gh_error = 0;
  1408. spin_lock(&gl->gl_lockref.lock);
  1409. add_to_queue(gh);
  1410. if (unlikely((LM_FLAG_NOEXP & gh->gh_flags) &&
  1411. test_and_clear_bit(GLF_FROZEN, &gl->gl_flags))) {
  1412. set_bit(GLF_REPLY_PENDING, &gl->gl_flags);
  1413. gl->gl_lockref.count++;
  1414. __gfs2_glock_queue_work(gl, 0);
  1415. }
  1416. run_queue(gl, 1);
  1417. spin_unlock(&gl->gl_lockref.lock);
  1418. if (!(gh->gh_flags & GL_ASYNC))
  1419. error = gfs2_glock_wait(gh);
  1420. return error;
  1421. }
  1422. /**
  1423. * gfs2_glock_poll - poll to see if an async request has been completed
  1424. * @gh: the holder
  1425. *
  1426. * Returns: 1 if the request is ready to be gfs2_glock_wait()ed on
  1427. */
  1428. int gfs2_glock_poll(struct gfs2_holder *gh)
  1429. {
  1430. return test_bit(HIF_WAIT, &gh->gh_iflags) ? 0 : 1;
  1431. }
  1432. static inline bool needs_demote(struct gfs2_glock *gl)
  1433. {
  1434. return (test_bit(GLF_DEMOTE, &gl->gl_flags) ||
  1435. test_bit(GLF_PENDING_DEMOTE, &gl->gl_flags));
  1436. }
  1437. static void __gfs2_glock_dq(struct gfs2_holder *gh)
  1438. {
  1439. struct gfs2_glock *gl = gh->gh_gl;
  1440. struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
  1441. unsigned delay = 0;
  1442. int fast_path = 0;
  1443. /*
  1444. * This while loop is similar to function demote_incompat_holders:
  1445. * If the glock is due to be demoted (which may be from another node
  1446. * or even if this holder is GL_NOCACHE), the weak holders are
  1447. * demoted as well, allowing the glock to be demoted.
  1448. */
  1449. while (gh) {
  1450. /*
  1451. * If we're in the process of file system withdraw, we cannot
  1452. * just dequeue any glocks until our journal is recovered, lest
  1453. * we introduce file system corruption. We need two exceptions
  1454. * to this rule: We need to allow unlocking of nondisk glocks
  1455. * and the glock for our own journal that needs recovery.
  1456. */
  1457. if (test_bit(SDF_WITHDRAW_RECOVERY, &sdp->sd_flags) &&
  1458. glock_blocked_by_withdraw(gl) &&
  1459. gh->gh_gl != sdp->sd_jinode_gl) {
  1460. sdp->sd_glock_dqs_held++;
  1461. spin_unlock(&gl->gl_lockref.lock);
  1462. might_sleep();
  1463. wait_on_bit(&sdp->sd_flags, SDF_WITHDRAW_RECOVERY,
  1464. TASK_UNINTERRUPTIBLE);
  1465. spin_lock(&gl->gl_lockref.lock);
  1466. }
  1467. /*
  1468. * This holder should not be cached, so mark it for demote.
  1469. * Note: this should be done before the check for needs_demote
  1470. * below.
  1471. */
  1472. if (gh->gh_flags & GL_NOCACHE)
  1473. handle_callback(gl, LM_ST_UNLOCKED, 0, false);
  1474. list_del_init(&gh->gh_list);
  1475. clear_bit(HIF_HOLDER, &gh->gh_iflags);
  1476. trace_gfs2_glock_queue(gh, 0);
  1477. /*
  1478. * If there hasn't been a demote request we are done.
  1479. * (Let the remaining holders, if any, keep holding it.)
  1480. */
  1481. if (!needs_demote(gl)) {
  1482. if (list_empty(&gl->gl_holders))
  1483. fast_path = 1;
  1484. break;
  1485. }
  1486. /*
  1487. * If we have another strong holder (we cannot auto-demote)
  1488. * we are done. It keeps holding it until it is done.
  1489. */
  1490. if (find_first_strong_holder(gl))
  1491. break;
  1492. /*
  1493. * If we have a weak holder at the head of the list, it
  1494. * (and all others like it) must be auto-demoted. If there
  1495. * are no more weak holders, we exit the while loop.
  1496. */
  1497. gh = find_first_holder(gl);
  1498. }
  1499. if (!test_bit(GLF_LFLUSH, &gl->gl_flags) && demote_ok(gl))
  1500. gfs2_glock_add_to_lru(gl);
  1501. if (unlikely(!fast_path)) {
  1502. gl->gl_lockref.count++;
  1503. if (test_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) &&
  1504. !test_bit(GLF_DEMOTE, &gl->gl_flags) &&
  1505. gl->gl_name.ln_type == LM_TYPE_INODE)
  1506. delay = gl->gl_hold_time;
  1507. __gfs2_glock_queue_work(gl, delay);
  1508. }
  1509. }
  1510. /**
  1511. * gfs2_glock_dq - dequeue a struct gfs2_holder from a glock (release a glock)
  1512. * @gh: the glock holder
  1513. *
  1514. */
  1515. void gfs2_glock_dq(struct gfs2_holder *gh)
  1516. {
  1517. struct gfs2_glock *gl = gh->gh_gl;
  1518. spin_lock(&gl->gl_lockref.lock);
  1519. if (list_is_first(&gh->gh_list, &gl->gl_holders) &&
  1520. !test_bit(HIF_HOLDER, &gh->gh_iflags)) {
  1521. spin_unlock(&gl->gl_lockref.lock);
  1522. gl->gl_name.ln_sbd->sd_lockstruct.ls_ops->lm_cancel(gl);
  1523. wait_on_bit(&gh->gh_iflags, HIF_WAIT, TASK_UNINTERRUPTIBLE);
  1524. spin_lock(&gl->gl_lockref.lock);
  1525. }
  1526. __gfs2_glock_dq(gh);
  1527. spin_unlock(&gl->gl_lockref.lock);
  1528. }
  1529. void gfs2_glock_dq_wait(struct gfs2_holder *gh)
  1530. {
  1531. struct gfs2_glock *gl = gh->gh_gl;
  1532. gfs2_glock_dq(gh);
  1533. might_sleep();
  1534. wait_on_bit(&gl->gl_flags, GLF_DEMOTE, TASK_UNINTERRUPTIBLE);
  1535. }
  1536. /**
  1537. * gfs2_glock_dq_uninit - dequeue a holder from a glock and initialize it
  1538. * @gh: the holder structure
  1539. *
  1540. */
  1541. void gfs2_glock_dq_uninit(struct gfs2_holder *gh)
  1542. {
  1543. gfs2_glock_dq(gh);
  1544. gfs2_holder_uninit(gh);
  1545. }
  1546. /**
  1547. * gfs2_glock_nq_num - acquire a glock based on lock number
  1548. * @sdp: the filesystem
  1549. * @number: the lock number
  1550. * @glops: the glock operations for the type of glock
  1551. * @state: the state to acquire the glock in
  1552. * @flags: modifier flags for the acquisition
  1553. * @gh: the struct gfs2_holder
  1554. *
  1555. * Returns: errno
  1556. */
  1557. int gfs2_glock_nq_num(struct gfs2_sbd *sdp, u64 number,
  1558. const struct gfs2_glock_operations *glops,
  1559. unsigned int state, u16 flags, struct gfs2_holder *gh)
  1560. {
  1561. struct gfs2_glock *gl;
  1562. int error;
  1563. error = gfs2_glock_get(sdp, number, glops, CREATE, &gl);
  1564. if (!error) {
  1565. error = gfs2_glock_nq_init(gl, state, flags, gh);
  1566. gfs2_glock_put(gl);
  1567. }
  1568. return error;
  1569. }
  1570. /**
  1571. * glock_compare - Compare two struct gfs2_glock structures for sorting
  1572. * @arg_a: the first structure
  1573. * @arg_b: the second structure
  1574. *
  1575. */
  1576. static int glock_compare(const void *arg_a, const void *arg_b)
  1577. {
  1578. const struct gfs2_holder *gh_a = *(const struct gfs2_holder **)arg_a;
  1579. const struct gfs2_holder *gh_b = *(const struct gfs2_holder **)arg_b;
  1580. const struct lm_lockname *a = &gh_a->gh_gl->gl_name;
  1581. const struct lm_lockname *b = &gh_b->gh_gl->gl_name;
  1582. if (a->ln_number > b->ln_number)
  1583. return 1;
  1584. if (a->ln_number < b->ln_number)
  1585. return -1;
  1586. BUG_ON(gh_a->gh_gl->gl_ops->go_type == gh_b->gh_gl->gl_ops->go_type);
  1587. return 0;
  1588. }
  1589. /**
  1590. * nq_m_sync - synchronously acquire more than one glock in deadlock free order
  1591. * @num_gh: the number of structures
  1592. * @ghs: an array of struct gfs2_holder structures
  1593. * @p: placeholder for the holder structure to pass back
  1594. *
  1595. * Returns: 0 on success (all glocks acquired),
  1596. * errno on failure (no glocks acquired)
  1597. */
  1598. static int nq_m_sync(unsigned int num_gh, struct gfs2_holder *ghs,
  1599. struct gfs2_holder **p)
  1600. {
  1601. unsigned int x;
  1602. int error = 0;
  1603. for (x = 0; x < num_gh; x++)
  1604. p[x] = &ghs[x];
  1605. sort(p, num_gh, sizeof(struct gfs2_holder *), glock_compare, NULL);
  1606. for (x = 0; x < num_gh; x++) {
  1607. error = gfs2_glock_nq(p[x]);
  1608. if (error) {
  1609. while (x--)
  1610. gfs2_glock_dq(p[x]);
  1611. break;
  1612. }
  1613. }
  1614. return error;
  1615. }
  1616. /**
  1617. * gfs2_glock_nq_m - acquire multiple glocks
  1618. * @num_gh: the number of structures
  1619. * @ghs: an array of struct gfs2_holder structures
  1620. *
  1621. * Returns: 0 on success (all glocks acquired),
  1622. * errno on failure (no glocks acquired)
  1623. */
  1624. int gfs2_glock_nq_m(unsigned int num_gh, struct gfs2_holder *ghs)
  1625. {
  1626. struct gfs2_holder *tmp[4];
  1627. struct gfs2_holder **pph = tmp;
  1628. int error = 0;
  1629. switch(num_gh) {
  1630. case 0:
  1631. return 0;
  1632. case 1:
  1633. return gfs2_glock_nq(ghs);
  1634. default:
  1635. if (num_gh <= 4)
  1636. break;
  1637. pph = kmalloc_array(num_gh, sizeof(struct gfs2_holder *),
  1638. GFP_NOFS);
  1639. if (!pph)
  1640. return -ENOMEM;
  1641. }
  1642. error = nq_m_sync(num_gh, ghs, pph);
  1643. if (pph != tmp)
  1644. kfree(pph);
  1645. return error;
  1646. }
  1647. /**
  1648. * gfs2_glock_dq_m - release multiple glocks
  1649. * @num_gh: the number of structures
  1650. * @ghs: an array of struct gfs2_holder structures
  1651. *
  1652. */
  1653. void gfs2_glock_dq_m(unsigned int num_gh, struct gfs2_holder *ghs)
  1654. {
  1655. while (num_gh--)
  1656. gfs2_glock_dq(&ghs[num_gh]);
  1657. }
  1658. void gfs2_glock_cb(struct gfs2_glock *gl, unsigned int state)
  1659. {
  1660. unsigned long delay = 0;
  1661. unsigned long holdtime;
  1662. unsigned long now = jiffies;
  1663. gfs2_glock_hold(gl);
  1664. spin_lock(&gl->gl_lockref.lock);
  1665. holdtime = gl->gl_tchange + gl->gl_hold_time;
  1666. if (!list_empty(&gl->gl_holders) &&
  1667. gl->gl_name.ln_type == LM_TYPE_INODE) {
  1668. if (time_before(now, holdtime))
  1669. delay = holdtime - now;
  1670. if (test_bit(GLF_REPLY_PENDING, &gl->gl_flags))
  1671. delay = gl->gl_hold_time;
  1672. }
  1673. /*
  1674. * Note 1: We cannot call demote_incompat_holders from handle_callback
  1675. * or gfs2_set_demote due to recursion problems like: gfs2_glock_dq ->
  1676. * handle_callback -> demote_incompat_holders -> gfs2_glock_dq
  1677. * Plus, we only want to demote the holders if the request comes from
  1678. * a remote cluster node because local holder conflicts are resolved
  1679. * elsewhere.
  1680. *
  1681. * Note 2: if a remote node wants this glock in EX mode, lock_dlm will
  1682. * request that we set our state to UNLOCKED. Here we mock up a holder
  1683. * to make it look like someone wants the lock EX locally. Any SH
  1684. * and DF requests should be able to share the lock without demoting.
  1685. *
  1686. * Note 3: We only want to demote the demoteable holders when there
  1687. * are no more strong holders. The demoteable holders might as well
  1688. * keep the glock until the last strong holder is done with it.
  1689. */
  1690. if (!find_first_strong_holder(gl)) {
  1691. struct gfs2_holder mock_gh = {
  1692. .gh_gl = gl,
  1693. .gh_state = (state == LM_ST_UNLOCKED) ?
  1694. LM_ST_EXCLUSIVE : state,
  1695. .gh_iflags = BIT(HIF_HOLDER)
  1696. };
  1697. demote_incompat_holders(gl, &mock_gh);
  1698. }
  1699. handle_callback(gl, state, delay, true);
  1700. __gfs2_glock_queue_work(gl, delay);
  1701. spin_unlock(&gl->gl_lockref.lock);
  1702. }
  1703. /**
  1704. * gfs2_should_freeze - Figure out if glock should be frozen
  1705. * @gl: The glock in question
  1706. *
  1707. * Glocks are not frozen if (a) the result of the dlm operation is
  1708. * an error, (b) the locking operation was an unlock operation or
  1709. * (c) if there is a "noexp" flagged request anywhere in the queue
  1710. *
  1711. * Returns: 1 if freezing should occur, 0 otherwise
  1712. */
  1713. static int gfs2_should_freeze(const struct gfs2_glock *gl)
  1714. {
  1715. const struct gfs2_holder *gh;
  1716. if (gl->gl_reply & ~LM_OUT_ST_MASK)
  1717. return 0;
  1718. if (gl->gl_target == LM_ST_UNLOCKED)
  1719. return 0;
  1720. list_for_each_entry(gh, &gl->gl_holders, gh_list) {
  1721. if (test_bit(HIF_HOLDER, &gh->gh_iflags))
  1722. continue;
  1723. if (LM_FLAG_NOEXP & gh->gh_flags)
  1724. return 0;
  1725. }
  1726. return 1;
  1727. }
  1728. /**
  1729. * gfs2_glock_complete - Callback used by locking
  1730. * @gl: Pointer to the glock
  1731. * @ret: The return value from the dlm
  1732. *
  1733. * The gl_reply field is under the gl_lockref.lock lock so that it is ok
  1734. * to use a bitfield shared with other glock state fields.
  1735. */
  1736. void gfs2_glock_complete(struct gfs2_glock *gl, int ret)
  1737. {
  1738. struct lm_lockstruct *ls = &gl->gl_name.ln_sbd->sd_lockstruct;
  1739. spin_lock(&gl->gl_lockref.lock);
  1740. gl->gl_reply = ret;
  1741. if (unlikely(test_bit(DFL_BLOCK_LOCKS, &ls->ls_recover_flags))) {
  1742. if (gfs2_should_freeze(gl)) {
  1743. set_bit(GLF_FROZEN, &gl->gl_flags);
  1744. spin_unlock(&gl->gl_lockref.lock);
  1745. return;
  1746. }
  1747. }
  1748. gl->gl_lockref.count++;
  1749. set_bit(GLF_REPLY_PENDING, &gl->gl_flags);
  1750. __gfs2_glock_queue_work(gl, 0);
  1751. spin_unlock(&gl->gl_lockref.lock);
  1752. }
  1753. static int glock_cmp(void *priv, const struct list_head *a,
  1754. const struct list_head *b)
  1755. {
  1756. struct gfs2_glock *gla, *glb;
  1757. gla = list_entry(a, struct gfs2_glock, gl_lru);
  1758. glb = list_entry(b, struct gfs2_glock, gl_lru);
  1759. if (gla->gl_name.ln_number > glb->gl_name.ln_number)
  1760. return 1;
  1761. if (gla->gl_name.ln_number < glb->gl_name.ln_number)
  1762. return -1;
  1763. return 0;
  1764. }
  1765. /**
  1766. * gfs2_dispose_glock_lru - Demote a list of glocks
  1767. * @list: The list to dispose of
  1768. *
  1769. * Disposing of glocks may involve disk accesses, so that here we sort
  1770. * the glocks by number (i.e. disk location of the inodes) so that if
  1771. * there are any such accesses, they'll be sent in order (mostly).
  1772. *
  1773. * Must be called under the lru_lock, but may drop and retake this
  1774. * lock. While the lru_lock is dropped, entries may vanish from the
  1775. * list, but no new entries will appear on the list (since it is
  1776. * private)
  1777. */
  1778. static void gfs2_dispose_glock_lru(struct list_head *list)
  1779. __releases(&lru_lock)
  1780. __acquires(&lru_lock)
  1781. {
  1782. struct gfs2_glock *gl;
  1783. list_sort(NULL, list, glock_cmp);
  1784. while(!list_empty(list)) {
  1785. gl = list_first_entry(list, struct gfs2_glock, gl_lru);
  1786. list_del_init(&gl->gl_lru);
  1787. clear_bit(GLF_LRU, &gl->gl_flags);
  1788. if (!spin_trylock(&gl->gl_lockref.lock)) {
  1789. add_back_to_lru:
  1790. list_add(&gl->gl_lru, &lru_list);
  1791. set_bit(GLF_LRU, &gl->gl_flags);
  1792. atomic_inc(&lru_count);
  1793. continue;
  1794. }
  1795. if (test_and_set_bit(GLF_LOCK, &gl->gl_flags)) {
  1796. spin_unlock(&gl->gl_lockref.lock);
  1797. goto add_back_to_lru;
  1798. }
  1799. gl->gl_lockref.count++;
  1800. if (demote_ok(gl))
  1801. handle_callback(gl, LM_ST_UNLOCKED, 0, false);
  1802. WARN_ON(!test_and_clear_bit(GLF_LOCK, &gl->gl_flags));
  1803. __gfs2_glock_queue_work(gl, 0);
  1804. spin_unlock(&gl->gl_lockref.lock);
  1805. cond_resched_lock(&lru_lock);
  1806. }
  1807. }
  1808. /**
  1809. * gfs2_scan_glock_lru - Scan the LRU looking for locks to demote
  1810. * @nr: The number of entries to scan
  1811. *
  1812. * This function selects the entries on the LRU which are able to
  1813. * be demoted, and then kicks off the process by calling
  1814. * gfs2_dispose_glock_lru() above.
  1815. */
  1816. static long gfs2_scan_glock_lru(int nr)
  1817. {
  1818. struct gfs2_glock *gl;
  1819. LIST_HEAD(skipped);
  1820. LIST_HEAD(dispose);
  1821. long freed = 0;
  1822. spin_lock(&lru_lock);
  1823. while ((nr-- >= 0) && !list_empty(&lru_list)) {
  1824. gl = list_first_entry(&lru_list, struct gfs2_glock, gl_lru);
  1825. /* Test for being demotable */
  1826. if (!test_bit(GLF_LOCK, &gl->gl_flags)) {
  1827. list_move(&gl->gl_lru, &dispose);
  1828. atomic_dec(&lru_count);
  1829. freed++;
  1830. continue;
  1831. }
  1832. list_move(&gl->gl_lru, &skipped);
  1833. }
  1834. list_splice(&skipped, &lru_list);
  1835. if (!list_empty(&dispose))
  1836. gfs2_dispose_glock_lru(&dispose);
  1837. spin_unlock(&lru_lock);
  1838. return freed;
  1839. }
  1840. static unsigned long gfs2_glock_shrink_scan(struct shrinker *shrink,
  1841. struct shrink_control *sc)
  1842. {
  1843. if (!(sc->gfp_mask & __GFP_FS))
  1844. return SHRINK_STOP;
  1845. return gfs2_scan_glock_lru(sc->nr_to_scan);
  1846. }
  1847. static unsigned long gfs2_glock_shrink_count(struct shrinker *shrink,
  1848. struct shrink_control *sc)
  1849. {
  1850. return vfs_pressure_ratio(atomic_read(&lru_count));
  1851. }
  1852. static struct shrinker glock_shrinker = {
  1853. .seeks = DEFAULT_SEEKS,
  1854. .count_objects = gfs2_glock_shrink_count,
  1855. .scan_objects = gfs2_glock_shrink_scan,
  1856. };
  1857. /**
  1858. * glock_hash_walk - Call a function for glock in a hash bucket
  1859. * @examiner: the function
  1860. * @sdp: the filesystem
  1861. *
  1862. * Note that the function can be called multiple times on the same
  1863. * object. So the user must ensure that the function can cope with
  1864. * that.
  1865. */
  1866. static void glock_hash_walk(glock_examiner examiner, const struct gfs2_sbd *sdp)
  1867. {
  1868. struct gfs2_glock *gl;
  1869. struct rhashtable_iter iter;
  1870. rhashtable_walk_enter(&gl_hash_table, &iter);
  1871. do {
  1872. rhashtable_walk_start(&iter);
  1873. while ((gl = rhashtable_walk_next(&iter)) && !IS_ERR(gl)) {
  1874. if (gl->gl_name.ln_sbd == sdp)
  1875. examiner(gl);
  1876. }
  1877. rhashtable_walk_stop(&iter);
  1878. } while (cond_resched(), gl == ERR_PTR(-EAGAIN));
  1879. rhashtable_walk_exit(&iter);
  1880. }
  1881. bool gfs2_queue_delete_work(struct gfs2_glock *gl, unsigned long delay)
  1882. {
  1883. bool queued;
  1884. spin_lock(&gl->gl_lockref.lock);
  1885. queued = queue_delayed_work(gfs2_delete_workqueue,
  1886. &gl->gl_delete, delay);
  1887. if (queued)
  1888. set_bit(GLF_PENDING_DELETE, &gl->gl_flags);
  1889. spin_unlock(&gl->gl_lockref.lock);
  1890. return queued;
  1891. }
  1892. void gfs2_cancel_delete_work(struct gfs2_glock *gl)
  1893. {
  1894. if (cancel_delayed_work(&gl->gl_delete)) {
  1895. clear_bit(GLF_PENDING_DELETE, &gl->gl_flags);
  1896. gfs2_glock_put(gl);
  1897. }
  1898. }
  1899. bool gfs2_delete_work_queued(const struct gfs2_glock *gl)
  1900. {
  1901. return test_bit(GLF_PENDING_DELETE, &gl->gl_flags);
  1902. }
  1903. static void flush_delete_work(struct gfs2_glock *gl)
  1904. {
  1905. if (gl->gl_name.ln_type == LM_TYPE_IOPEN) {
  1906. if (cancel_delayed_work(&gl->gl_delete)) {
  1907. queue_delayed_work(gfs2_delete_workqueue,
  1908. &gl->gl_delete, 0);
  1909. }
  1910. }
  1911. }
  1912. void gfs2_flush_delete_work(struct gfs2_sbd *sdp)
  1913. {
  1914. glock_hash_walk(flush_delete_work, sdp);
  1915. flush_workqueue(gfs2_delete_workqueue);
  1916. }
  1917. /**
  1918. * thaw_glock - thaw out a glock which has an unprocessed reply waiting
  1919. * @gl: The glock to thaw
  1920. *
  1921. */
  1922. static void thaw_glock(struct gfs2_glock *gl)
  1923. {
  1924. if (!test_and_clear_bit(GLF_FROZEN, &gl->gl_flags))
  1925. return;
  1926. if (!lockref_get_not_dead(&gl->gl_lockref))
  1927. return;
  1928. set_bit(GLF_REPLY_PENDING, &gl->gl_flags);
  1929. gfs2_glock_queue_work(gl, 0);
  1930. }
  1931. /**
  1932. * clear_glock - look at a glock and see if we can free it from glock cache
  1933. * @gl: the glock to look at
  1934. *
  1935. */
  1936. static void clear_glock(struct gfs2_glock *gl)
  1937. {
  1938. gfs2_glock_remove_from_lru(gl);
  1939. spin_lock(&gl->gl_lockref.lock);
  1940. if (!__lockref_is_dead(&gl->gl_lockref)) {
  1941. gl->gl_lockref.count++;
  1942. if (gl->gl_state != LM_ST_UNLOCKED)
  1943. handle_callback(gl, LM_ST_UNLOCKED, 0, false);
  1944. __gfs2_glock_queue_work(gl, 0);
  1945. }
  1946. spin_unlock(&gl->gl_lockref.lock);
  1947. }
  1948. /**
  1949. * gfs2_glock_thaw - Thaw any frozen glocks
  1950. * @sdp: The super block
  1951. *
  1952. */
  1953. void gfs2_glock_thaw(struct gfs2_sbd *sdp)
  1954. {
  1955. glock_hash_walk(thaw_glock, sdp);
  1956. }
  1957. static void dump_glock(struct seq_file *seq, struct gfs2_glock *gl, bool fsid)
  1958. {
  1959. spin_lock(&gl->gl_lockref.lock);
  1960. gfs2_dump_glock(seq, gl, fsid);
  1961. spin_unlock(&gl->gl_lockref.lock);
  1962. }
  1963. static void dump_glock_func(struct gfs2_glock *gl)
  1964. {
  1965. dump_glock(NULL, gl, true);
  1966. }
  1967. static void withdraw_dq(struct gfs2_glock *gl)
  1968. {
  1969. spin_lock(&gl->gl_lockref.lock);
  1970. if (!__lockref_is_dead(&gl->gl_lockref) &&
  1971. glock_blocked_by_withdraw(gl))
  1972. do_error(gl, LM_OUT_ERROR); /* remove pending waiters */
  1973. spin_unlock(&gl->gl_lockref.lock);
  1974. }
  1975. void gfs2_gl_dq_holders(struct gfs2_sbd *sdp)
  1976. {
  1977. glock_hash_walk(withdraw_dq, sdp);
  1978. }
  1979. /**
  1980. * gfs2_gl_hash_clear - Empty out the glock hash table
  1981. * @sdp: the filesystem
  1982. *
  1983. * Called when unmounting the filesystem.
  1984. */
  1985. void gfs2_gl_hash_clear(struct gfs2_sbd *sdp)
  1986. {
  1987. set_bit(SDF_SKIP_DLM_UNLOCK, &sdp->sd_flags);
  1988. flush_workqueue(glock_workqueue);
  1989. glock_hash_walk(clear_glock, sdp);
  1990. flush_workqueue(glock_workqueue);
  1991. wait_event_timeout(sdp->sd_glock_wait,
  1992. atomic_read(&sdp->sd_glock_disposal) == 0,
  1993. HZ * 600);
  1994. glock_hash_walk(dump_glock_func, sdp);
  1995. }
  1996. static const char *state2str(unsigned state)
  1997. {
  1998. switch(state) {
  1999. case LM_ST_UNLOCKED:
  2000. return "UN";
  2001. case LM_ST_SHARED:
  2002. return "SH";
  2003. case LM_ST_DEFERRED:
  2004. return "DF";
  2005. case LM_ST_EXCLUSIVE:
  2006. return "EX";
  2007. }
  2008. return "??";
  2009. }
  2010. static const char *hflags2str(char *buf, u16 flags, unsigned long iflags)
  2011. {
  2012. char *p = buf;
  2013. if (flags & LM_FLAG_TRY)
  2014. *p++ = 't';
  2015. if (flags & LM_FLAG_TRY_1CB)
  2016. *p++ = 'T';
  2017. if (flags & LM_FLAG_NOEXP)
  2018. *p++ = 'e';
  2019. if (flags & LM_FLAG_ANY)
  2020. *p++ = 'A';
  2021. if (flags & LM_FLAG_PRIORITY)
  2022. *p++ = 'p';
  2023. if (flags & LM_FLAG_NODE_SCOPE)
  2024. *p++ = 'n';
  2025. if (flags & GL_ASYNC)
  2026. *p++ = 'a';
  2027. if (flags & GL_EXACT)
  2028. *p++ = 'E';
  2029. if (flags & GL_NOCACHE)
  2030. *p++ = 'c';
  2031. if (test_bit(HIF_HOLDER, &iflags))
  2032. *p++ = 'H';
  2033. if (test_bit(HIF_WAIT, &iflags))
  2034. *p++ = 'W';
  2035. if (test_bit(HIF_MAY_DEMOTE, &iflags))
  2036. *p++ = 'D';
  2037. if (flags & GL_SKIP)
  2038. *p++ = 's';
  2039. *p = 0;
  2040. return buf;
  2041. }
  2042. /**
  2043. * dump_holder - print information about a glock holder
  2044. * @seq: the seq_file struct
  2045. * @gh: the glock holder
  2046. * @fs_id_buf: pointer to file system id (if requested)
  2047. *
  2048. */
  2049. static void dump_holder(struct seq_file *seq, const struct gfs2_holder *gh,
  2050. const char *fs_id_buf)
  2051. {
  2052. const char *comm = "(none)";
  2053. pid_t owner_pid = 0;
  2054. char flags_buf[32];
  2055. rcu_read_lock();
  2056. if (pid_is_meaningful(gh)) {
  2057. struct task_struct *gh_owner;
  2058. comm = "(ended)";
  2059. owner_pid = pid_nr(gh->gh_owner_pid);
  2060. gh_owner = pid_task(gh->gh_owner_pid, PIDTYPE_PID);
  2061. if (gh_owner)
  2062. comm = gh_owner->comm;
  2063. }
  2064. gfs2_print_dbg(seq, "%s H: s:%s f:%s e:%d p:%ld [%s] %pS\n",
  2065. fs_id_buf, state2str(gh->gh_state),
  2066. hflags2str(flags_buf, gh->gh_flags, gh->gh_iflags),
  2067. gh->gh_error, (long)owner_pid, comm, (void *)gh->gh_ip);
  2068. rcu_read_unlock();
  2069. }
  2070. static const char *gflags2str(char *buf, const struct gfs2_glock *gl)
  2071. {
  2072. const unsigned long *gflags = &gl->gl_flags;
  2073. char *p = buf;
  2074. if (test_bit(GLF_LOCK, gflags))
  2075. *p++ = 'l';
  2076. if (test_bit(GLF_DEMOTE, gflags))
  2077. *p++ = 'D';
  2078. if (test_bit(GLF_PENDING_DEMOTE, gflags))
  2079. *p++ = 'd';
  2080. if (test_bit(GLF_DEMOTE_IN_PROGRESS, gflags))
  2081. *p++ = 'p';
  2082. if (test_bit(GLF_DIRTY, gflags))
  2083. *p++ = 'y';
  2084. if (test_bit(GLF_LFLUSH, gflags))
  2085. *p++ = 'f';
  2086. if (test_bit(GLF_INVALIDATE_IN_PROGRESS, gflags))
  2087. *p++ = 'i';
  2088. if (test_bit(GLF_REPLY_PENDING, gflags))
  2089. *p++ = 'r';
  2090. if (test_bit(GLF_INITIAL, gflags))
  2091. *p++ = 'I';
  2092. if (test_bit(GLF_FROZEN, gflags))
  2093. *p++ = 'F';
  2094. if (!list_empty(&gl->gl_holders))
  2095. *p++ = 'q';
  2096. if (test_bit(GLF_LRU, gflags))
  2097. *p++ = 'L';
  2098. if (gl->gl_object)
  2099. *p++ = 'o';
  2100. if (test_bit(GLF_BLOCKING, gflags))
  2101. *p++ = 'b';
  2102. if (test_bit(GLF_PENDING_DELETE, gflags))
  2103. *p++ = 'P';
  2104. if (test_bit(GLF_FREEING, gflags))
  2105. *p++ = 'x';
  2106. if (test_bit(GLF_INSTANTIATE_NEEDED, gflags))
  2107. *p++ = 'n';
  2108. if (test_bit(GLF_INSTANTIATE_IN_PROG, gflags))
  2109. *p++ = 'N';
  2110. *p = 0;
  2111. return buf;
  2112. }
  2113. /**
  2114. * gfs2_dump_glock - print information about a glock
  2115. * @seq: The seq_file struct
  2116. * @gl: the glock
  2117. * @fsid: If true, also dump the file system id
  2118. *
  2119. * The file format is as follows:
  2120. * One line per object, capital letters are used to indicate objects
  2121. * G = glock, I = Inode, R = rgrp, H = holder. Glocks are not indented,
  2122. * other objects are indented by a single space and follow the glock to
  2123. * which they are related. Fields are indicated by lower case letters
  2124. * followed by a colon and the field value, except for strings which are in
  2125. * [] so that its possible to see if they are composed of spaces for
  2126. * example. The field's are n = number (id of the object), f = flags,
  2127. * t = type, s = state, r = refcount, e = error, p = pid.
  2128. *
  2129. */
  2130. void gfs2_dump_glock(struct seq_file *seq, struct gfs2_glock *gl, bool fsid)
  2131. {
  2132. const struct gfs2_glock_operations *glops = gl->gl_ops;
  2133. unsigned long long dtime;
  2134. const struct gfs2_holder *gh;
  2135. char gflags_buf[32];
  2136. struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
  2137. char fs_id_buf[sizeof(sdp->sd_fsname) + 7];
  2138. unsigned long nrpages = 0;
  2139. if (gl->gl_ops->go_flags & GLOF_ASPACE) {
  2140. struct address_space *mapping = gfs2_glock2aspace(gl);
  2141. nrpages = mapping->nrpages;
  2142. }
  2143. memset(fs_id_buf, 0, sizeof(fs_id_buf));
  2144. if (fsid && sdp) /* safety precaution */
  2145. sprintf(fs_id_buf, "fsid=%s: ", sdp->sd_fsname);
  2146. dtime = jiffies - gl->gl_demote_time;
  2147. dtime *= 1000000/HZ; /* demote time in uSec */
  2148. if (!test_bit(GLF_DEMOTE, &gl->gl_flags))
  2149. dtime = 0;
  2150. gfs2_print_dbg(seq, "%sG: s:%s n:%u/%llx f:%s t:%s d:%s/%llu a:%d "
  2151. "v:%d r:%d m:%ld p:%lu\n",
  2152. fs_id_buf, state2str(gl->gl_state),
  2153. gl->gl_name.ln_type,
  2154. (unsigned long long)gl->gl_name.ln_number,
  2155. gflags2str(gflags_buf, gl),
  2156. state2str(gl->gl_target),
  2157. state2str(gl->gl_demote_state), dtime,
  2158. atomic_read(&gl->gl_ail_count),
  2159. atomic_read(&gl->gl_revokes),
  2160. (int)gl->gl_lockref.count, gl->gl_hold_time, nrpages);
  2161. list_for_each_entry(gh, &gl->gl_holders, gh_list)
  2162. dump_holder(seq, gh, fs_id_buf);
  2163. if (gl->gl_state != LM_ST_UNLOCKED && glops->go_dump)
  2164. glops->go_dump(seq, gl, fs_id_buf);
  2165. }
  2166. static int gfs2_glstats_seq_show(struct seq_file *seq, void *iter_ptr)
  2167. {
  2168. struct gfs2_glock *gl = iter_ptr;
  2169. seq_printf(seq, "G: n:%u/%llx rtt:%llu/%llu rttb:%llu/%llu irt:%llu/%llu dcnt: %llu qcnt: %llu\n",
  2170. gl->gl_name.ln_type,
  2171. (unsigned long long)gl->gl_name.ln_number,
  2172. (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SRTT],
  2173. (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SRTTVAR],
  2174. (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SRTTB],
  2175. (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SRTTVARB],
  2176. (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SIRT],
  2177. (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SIRTVAR],
  2178. (unsigned long long)gl->gl_stats.stats[GFS2_LKS_DCOUNT],
  2179. (unsigned long long)gl->gl_stats.stats[GFS2_LKS_QCOUNT]);
  2180. return 0;
  2181. }
  2182. static const char *gfs2_gltype[] = {
  2183. "type",
  2184. "reserved",
  2185. "nondisk",
  2186. "inode",
  2187. "rgrp",
  2188. "meta",
  2189. "iopen",
  2190. "flock",
  2191. "plock",
  2192. "quota",
  2193. "journal",
  2194. };
  2195. static const char *gfs2_stype[] = {
  2196. [GFS2_LKS_SRTT] = "srtt",
  2197. [GFS2_LKS_SRTTVAR] = "srttvar",
  2198. [GFS2_LKS_SRTTB] = "srttb",
  2199. [GFS2_LKS_SRTTVARB] = "srttvarb",
  2200. [GFS2_LKS_SIRT] = "sirt",
  2201. [GFS2_LKS_SIRTVAR] = "sirtvar",
  2202. [GFS2_LKS_DCOUNT] = "dlm",
  2203. [GFS2_LKS_QCOUNT] = "queue",
  2204. };
  2205. #define GFS2_NR_SBSTATS (ARRAY_SIZE(gfs2_gltype) * ARRAY_SIZE(gfs2_stype))
  2206. static int gfs2_sbstats_seq_show(struct seq_file *seq, void *iter_ptr)
  2207. {
  2208. struct gfs2_sbd *sdp = seq->private;
  2209. loff_t pos = *(loff_t *)iter_ptr;
  2210. unsigned index = pos >> 3;
  2211. unsigned subindex = pos & 0x07;
  2212. int i;
  2213. if (index == 0 && subindex != 0)
  2214. return 0;
  2215. seq_printf(seq, "%-10s %8s:", gfs2_gltype[index],
  2216. (index == 0) ? "cpu": gfs2_stype[subindex]);
  2217. for_each_possible_cpu(i) {
  2218. const struct gfs2_pcpu_lkstats *lkstats = per_cpu_ptr(sdp->sd_lkstats, i);
  2219. if (index == 0)
  2220. seq_printf(seq, " %15u", i);
  2221. else
  2222. seq_printf(seq, " %15llu", (unsigned long long)lkstats->
  2223. lkstats[index - 1].stats[subindex]);
  2224. }
  2225. seq_putc(seq, '\n');
  2226. return 0;
  2227. }
  2228. int __init gfs2_glock_init(void)
  2229. {
  2230. int i, ret;
  2231. ret = rhashtable_init(&gl_hash_table, &ht_parms);
  2232. if (ret < 0)
  2233. return ret;
  2234. glock_workqueue = alloc_workqueue("glock_workqueue", WQ_MEM_RECLAIM |
  2235. WQ_HIGHPRI | WQ_FREEZABLE, 0);
  2236. if (!glock_workqueue) {
  2237. rhashtable_destroy(&gl_hash_table);
  2238. return -ENOMEM;
  2239. }
  2240. gfs2_delete_workqueue = alloc_workqueue("delete_workqueue",
  2241. WQ_MEM_RECLAIM | WQ_FREEZABLE,
  2242. 0);
  2243. if (!gfs2_delete_workqueue) {
  2244. destroy_workqueue(glock_workqueue);
  2245. rhashtable_destroy(&gl_hash_table);
  2246. return -ENOMEM;
  2247. }
  2248. ret = register_shrinker(&glock_shrinker, "gfs2-glock");
  2249. if (ret) {
  2250. destroy_workqueue(gfs2_delete_workqueue);
  2251. destroy_workqueue(glock_workqueue);
  2252. rhashtable_destroy(&gl_hash_table);
  2253. return ret;
  2254. }
  2255. for (i = 0; i < GLOCK_WAIT_TABLE_SIZE; i++)
  2256. init_waitqueue_head(glock_wait_table + i);
  2257. return 0;
  2258. }
  2259. void gfs2_glock_exit(void)
  2260. {
  2261. unregister_shrinker(&glock_shrinker);
  2262. rhashtable_destroy(&gl_hash_table);
  2263. destroy_workqueue(glock_workqueue);
  2264. destroy_workqueue(gfs2_delete_workqueue);
  2265. }
  2266. static void gfs2_glock_iter_next(struct gfs2_glock_iter *gi, loff_t n)
  2267. {
  2268. struct gfs2_glock *gl = gi->gl;
  2269. if (gl) {
  2270. if (n == 0)
  2271. return;
  2272. if (!lockref_put_not_zero(&gl->gl_lockref))
  2273. gfs2_glock_queue_put(gl);
  2274. }
  2275. for (;;) {
  2276. gl = rhashtable_walk_next(&gi->hti);
  2277. if (IS_ERR_OR_NULL(gl)) {
  2278. if (gl == ERR_PTR(-EAGAIN)) {
  2279. n = 1;
  2280. continue;
  2281. }
  2282. gl = NULL;
  2283. break;
  2284. }
  2285. if (gl->gl_name.ln_sbd != gi->sdp)
  2286. continue;
  2287. if (n <= 1) {
  2288. if (!lockref_get_not_dead(&gl->gl_lockref))
  2289. continue;
  2290. break;
  2291. } else {
  2292. if (__lockref_is_dead(&gl->gl_lockref))
  2293. continue;
  2294. n--;
  2295. }
  2296. }
  2297. gi->gl = gl;
  2298. }
  2299. static void *gfs2_glock_seq_start(struct seq_file *seq, loff_t *pos)
  2300. __acquires(RCU)
  2301. {
  2302. struct gfs2_glock_iter *gi = seq->private;
  2303. loff_t n;
  2304. /*
  2305. * We can either stay where we are, skip to the next hash table
  2306. * entry, or start from the beginning.
  2307. */
  2308. if (*pos < gi->last_pos) {
  2309. rhashtable_walk_exit(&gi->hti);
  2310. rhashtable_walk_enter(&gl_hash_table, &gi->hti);
  2311. n = *pos + 1;
  2312. } else {
  2313. n = *pos - gi->last_pos;
  2314. }
  2315. rhashtable_walk_start(&gi->hti);
  2316. gfs2_glock_iter_next(gi, n);
  2317. gi->last_pos = *pos;
  2318. return gi->gl;
  2319. }
  2320. static void *gfs2_glock_seq_next(struct seq_file *seq, void *iter_ptr,
  2321. loff_t *pos)
  2322. {
  2323. struct gfs2_glock_iter *gi = seq->private;
  2324. (*pos)++;
  2325. gi->last_pos = *pos;
  2326. gfs2_glock_iter_next(gi, 1);
  2327. return gi->gl;
  2328. }
  2329. static void gfs2_glock_seq_stop(struct seq_file *seq, void *iter_ptr)
  2330. __releases(RCU)
  2331. {
  2332. struct gfs2_glock_iter *gi = seq->private;
  2333. rhashtable_walk_stop(&gi->hti);
  2334. }
  2335. static int gfs2_glock_seq_show(struct seq_file *seq, void *iter_ptr)
  2336. {
  2337. dump_glock(seq, iter_ptr, false);
  2338. return 0;
  2339. }
  2340. static void *gfs2_sbstats_seq_start(struct seq_file *seq, loff_t *pos)
  2341. {
  2342. preempt_disable();
  2343. if (*pos >= GFS2_NR_SBSTATS)
  2344. return NULL;
  2345. return pos;
  2346. }
  2347. static void *gfs2_sbstats_seq_next(struct seq_file *seq, void *iter_ptr,
  2348. loff_t *pos)
  2349. {
  2350. (*pos)++;
  2351. if (*pos >= GFS2_NR_SBSTATS)
  2352. return NULL;
  2353. return pos;
  2354. }
  2355. static void gfs2_sbstats_seq_stop(struct seq_file *seq, void *iter_ptr)
  2356. {
  2357. preempt_enable();
  2358. }
  2359. static const struct seq_operations gfs2_glock_seq_ops = {
  2360. .start = gfs2_glock_seq_start,
  2361. .next = gfs2_glock_seq_next,
  2362. .stop = gfs2_glock_seq_stop,
  2363. .show = gfs2_glock_seq_show,
  2364. };
  2365. static const struct seq_operations gfs2_glstats_seq_ops = {
  2366. .start = gfs2_glock_seq_start,
  2367. .next = gfs2_glock_seq_next,
  2368. .stop = gfs2_glock_seq_stop,
  2369. .show = gfs2_glstats_seq_show,
  2370. };
  2371. static const struct seq_operations gfs2_sbstats_sops = {
  2372. .start = gfs2_sbstats_seq_start,
  2373. .next = gfs2_sbstats_seq_next,
  2374. .stop = gfs2_sbstats_seq_stop,
  2375. .show = gfs2_sbstats_seq_show,
  2376. };
  2377. #define GFS2_SEQ_GOODSIZE min(PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER, 65536UL)
  2378. static int __gfs2_glocks_open(struct inode *inode, struct file *file,
  2379. const struct seq_operations *ops)
  2380. {
  2381. int ret = seq_open_private(file, ops, sizeof(struct gfs2_glock_iter));
  2382. if (ret == 0) {
  2383. struct seq_file *seq = file->private_data;
  2384. struct gfs2_glock_iter *gi = seq->private;
  2385. gi->sdp = inode->i_private;
  2386. seq->buf = kmalloc(GFS2_SEQ_GOODSIZE, GFP_KERNEL | __GFP_NOWARN);
  2387. if (seq->buf)
  2388. seq->size = GFS2_SEQ_GOODSIZE;
  2389. /*
  2390. * Initially, we are "before" the first hash table entry; the
  2391. * first call to rhashtable_walk_next gets us the first entry.
  2392. */
  2393. gi->last_pos = -1;
  2394. gi->gl = NULL;
  2395. rhashtable_walk_enter(&gl_hash_table, &gi->hti);
  2396. }
  2397. return ret;
  2398. }
  2399. static int gfs2_glocks_open(struct inode *inode, struct file *file)
  2400. {
  2401. return __gfs2_glocks_open(inode, file, &gfs2_glock_seq_ops);
  2402. }
  2403. static int gfs2_glocks_release(struct inode *inode, struct file *file)
  2404. {
  2405. struct seq_file *seq = file->private_data;
  2406. struct gfs2_glock_iter *gi = seq->private;
  2407. if (gi->gl)
  2408. gfs2_glock_put(gi->gl);
  2409. rhashtable_walk_exit(&gi->hti);
  2410. return seq_release_private(inode, file);
  2411. }
  2412. static int gfs2_glstats_open(struct inode *inode, struct file *file)
  2413. {
  2414. return __gfs2_glocks_open(inode, file, &gfs2_glstats_seq_ops);
  2415. }
  2416. static const struct file_operations gfs2_glocks_fops = {
  2417. .owner = THIS_MODULE,
  2418. .open = gfs2_glocks_open,
  2419. .read = seq_read,
  2420. .llseek = seq_lseek,
  2421. .release = gfs2_glocks_release,
  2422. };
  2423. static const struct file_operations gfs2_glstats_fops = {
  2424. .owner = THIS_MODULE,
  2425. .open = gfs2_glstats_open,
  2426. .read = seq_read,
  2427. .llseek = seq_lseek,
  2428. .release = gfs2_glocks_release,
  2429. };
  2430. struct gfs2_glockfd_iter {
  2431. struct super_block *sb;
  2432. unsigned int tgid;
  2433. struct task_struct *task;
  2434. unsigned int fd;
  2435. struct file *file;
  2436. };
  2437. static struct task_struct *gfs2_glockfd_next_task(struct gfs2_glockfd_iter *i)
  2438. {
  2439. struct pid_namespace *ns = task_active_pid_ns(current);
  2440. struct pid *pid;
  2441. if (i->task)
  2442. put_task_struct(i->task);
  2443. rcu_read_lock();
  2444. retry:
  2445. i->task = NULL;
  2446. pid = find_ge_pid(i->tgid, ns);
  2447. if (pid) {
  2448. i->tgid = pid_nr_ns(pid, ns);
  2449. i->task = pid_task(pid, PIDTYPE_TGID);
  2450. if (!i->task) {
  2451. i->tgid++;
  2452. goto retry;
  2453. }
  2454. get_task_struct(i->task);
  2455. }
  2456. rcu_read_unlock();
  2457. return i->task;
  2458. }
  2459. static struct file *gfs2_glockfd_next_file(struct gfs2_glockfd_iter *i)
  2460. {
  2461. if (i->file) {
  2462. fput(i->file);
  2463. i->file = NULL;
  2464. }
  2465. rcu_read_lock();
  2466. for(;; i->fd++) {
  2467. struct inode *inode;
  2468. i->file = task_lookup_next_fd_rcu(i->task, &i->fd);
  2469. if (!i->file) {
  2470. i->fd = 0;
  2471. break;
  2472. }
  2473. inode = file_inode(i->file);
  2474. if (inode->i_sb != i->sb)
  2475. continue;
  2476. if (get_file_rcu(i->file))
  2477. break;
  2478. }
  2479. rcu_read_unlock();
  2480. return i->file;
  2481. }
  2482. static void *gfs2_glockfd_seq_start(struct seq_file *seq, loff_t *pos)
  2483. {
  2484. struct gfs2_glockfd_iter *i = seq->private;
  2485. if (*pos)
  2486. return NULL;
  2487. while (gfs2_glockfd_next_task(i)) {
  2488. if (gfs2_glockfd_next_file(i))
  2489. return i;
  2490. i->tgid++;
  2491. }
  2492. return NULL;
  2493. }
  2494. static void *gfs2_glockfd_seq_next(struct seq_file *seq, void *iter_ptr,
  2495. loff_t *pos)
  2496. {
  2497. struct gfs2_glockfd_iter *i = seq->private;
  2498. (*pos)++;
  2499. i->fd++;
  2500. do {
  2501. if (gfs2_glockfd_next_file(i))
  2502. return i;
  2503. i->tgid++;
  2504. } while (gfs2_glockfd_next_task(i));
  2505. return NULL;
  2506. }
  2507. static void gfs2_glockfd_seq_stop(struct seq_file *seq, void *iter_ptr)
  2508. {
  2509. struct gfs2_glockfd_iter *i = seq->private;
  2510. if (i->file)
  2511. fput(i->file);
  2512. if (i->task)
  2513. put_task_struct(i->task);
  2514. }
  2515. static void gfs2_glockfd_seq_show_flock(struct seq_file *seq,
  2516. struct gfs2_glockfd_iter *i)
  2517. {
  2518. struct gfs2_file *fp = i->file->private_data;
  2519. struct gfs2_holder *fl_gh = &fp->f_fl_gh;
  2520. struct lm_lockname gl_name = { .ln_type = LM_TYPE_RESERVED };
  2521. if (!READ_ONCE(fl_gh->gh_gl))
  2522. return;
  2523. spin_lock(&i->file->f_lock);
  2524. if (gfs2_holder_initialized(fl_gh))
  2525. gl_name = fl_gh->gh_gl->gl_name;
  2526. spin_unlock(&i->file->f_lock);
  2527. if (gl_name.ln_type != LM_TYPE_RESERVED) {
  2528. seq_printf(seq, "%d %u %u/%llx\n",
  2529. i->tgid, i->fd, gl_name.ln_type,
  2530. (unsigned long long)gl_name.ln_number);
  2531. }
  2532. }
  2533. static int gfs2_glockfd_seq_show(struct seq_file *seq, void *iter_ptr)
  2534. {
  2535. struct gfs2_glockfd_iter *i = seq->private;
  2536. struct inode *inode = file_inode(i->file);
  2537. struct gfs2_glock *gl;
  2538. inode_lock_shared(inode);
  2539. gl = GFS2_I(inode)->i_iopen_gh.gh_gl;
  2540. if (gl) {
  2541. seq_printf(seq, "%d %u %u/%llx\n",
  2542. i->tgid, i->fd, gl->gl_name.ln_type,
  2543. (unsigned long long)gl->gl_name.ln_number);
  2544. }
  2545. gfs2_glockfd_seq_show_flock(seq, i);
  2546. inode_unlock_shared(inode);
  2547. return 0;
  2548. }
  2549. static const struct seq_operations gfs2_glockfd_seq_ops = {
  2550. .start = gfs2_glockfd_seq_start,
  2551. .next = gfs2_glockfd_seq_next,
  2552. .stop = gfs2_glockfd_seq_stop,
  2553. .show = gfs2_glockfd_seq_show,
  2554. };
  2555. static int gfs2_glockfd_open(struct inode *inode, struct file *file)
  2556. {
  2557. struct gfs2_glockfd_iter *i;
  2558. struct gfs2_sbd *sdp = inode->i_private;
  2559. i = __seq_open_private(file, &gfs2_glockfd_seq_ops,
  2560. sizeof(struct gfs2_glockfd_iter));
  2561. if (!i)
  2562. return -ENOMEM;
  2563. i->sb = sdp->sd_vfs;
  2564. return 0;
  2565. }
  2566. static const struct file_operations gfs2_glockfd_fops = {
  2567. .owner = THIS_MODULE,
  2568. .open = gfs2_glockfd_open,
  2569. .read = seq_read,
  2570. .llseek = seq_lseek,
  2571. .release = seq_release_private,
  2572. };
  2573. DEFINE_SEQ_ATTRIBUTE(gfs2_sbstats);
  2574. void gfs2_create_debugfs_file(struct gfs2_sbd *sdp)
  2575. {
  2576. sdp->debugfs_dir = debugfs_create_dir(sdp->sd_table_name, gfs2_root);
  2577. debugfs_create_file("glocks", S_IFREG | S_IRUGO, sdp->debugfs_dir, sdp,
  2578. &gfs2_glocks_fops);
  2579. debugfs_create_file("glockfd", S_IFREG | S_IRUGO, sdp->debugfs_dir, sdp,
  2580. &gfs2_glockfd_fops);
  2581. debugfs_create_file("glstats", S_IFREG | S_IRUGO, sdp->debugfs_dir, sdp,
  2582. &gfs2_glstats_fops);
  2583. debugfs_create_file("sbstats", S_IFREG | S_IRUGO, sdp->debugfs_dir, sdp,
  2584. &gfs2_sbstats_fops);
  2585. }
  2586. void gfs2_delete_debugfs_file(struct gfs2_sbd *sdp)
  2587. {
  2588. debugfs_remove_recursive(sdp->debugfs_dir);
  2589. sdp->debugfs_dir = NULL;
  2590. }
  2591. void gfs2_register_debugfs(void)
  2592. {
  2593. gfs2_root = debugfs_create_dir("gfs2", NULL);
  2594. }
  2595. void gfs2_unregister_debugfs(void)
  2596. {
  2597. debugfs_remove(gfs2_root);
  2598. gfs2_root = NULL;
  2599. }