glock.c 75 KB

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