glock.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
  4. *
  5. * This copyrighted material is made available to anyone wishing to use,
  6. * modify, copy, or redistribute it subject to the terms and conditions
  7. * of the GNU General Public License version 2.
  8. */
  9. #include <linux/sched.h>
  10. #include <linux/slab.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/buffer_head.h>
  13. #include <linux/delay.h>
  14. #include <linux/sort.h>
  15. #include <linux/jhash.h>
  16. #include <linux/kallsyms.h>
  17. #include <linux/gfs2_ondisk.h>
  18. #include <linux/list.h>
  19. #include <linux/wait.h>
  20. #include <linux/module.h>
  21. #include <asm/uaccess.h>
  22. #include <linux/seq_file.h>
  23. #include <linux/debugfs.h>
  24. #include <linux/kthread.h>
  25. #include <linux/freezer.h>
  26. #include <linux/workqueue.h>
  27. #include <linux/jiffies.h>
  28. #include <linux/rcupdate.h>
  29. #include <linux/rculist_bl.h>
  30. #include <linux/bit_spinlock.h>
  31. #include "gfs2.h"
  32. #include "incore.h"
  33. #include "glock.h"
  34. #include "glops.h"
  35. #include "inode.h"
  36. #include "lops.h"
  37. #include "meta_io.h"
  38. #include "quota.h"
  39. #include "super.h"
  40. #include "util.h"
  41. #include "bmap.h"
  42. #define CREATE_TRACE_POINTS
  43. #include "trace_gfs2.h"
  44. struct gfs2_glock_iter {
  45. int hash; /* hash bucket index */
  46. struct gfs2_sbd *sdp; /* incore superblock */
  47. struct gfs2_glock *gl; /* current glock struct */
  48. char string[512]; /* scratch space */
  49. };
  50. typedef void (*glock_examiner) (struct gfs2_glock * gl);
  51. static int __dump_glock(struct seq_file *seq, const struct gfs2_glock *gl);
  52. #define GLOCK_BUG_ON(gl,x) do { if (unlikely(x)) { __dump_glock(NULL, gl); BUG(); } } while(0)
  53. static void do_xmote(struct gfs2_glock *gl, struct gfs2_holder *gh, unsigned int target);
  54. static struct dentry *gfs2_root;
  55. static struct workqueue_struct *glock_workqueue;
  56. struct workqueue_struct *gfs2_delete_workqueue;
  57. static LIST_HEAD(lru_list);
  58. static atomic_t lru_count = ATOMIC_INIT(0);
  59. static DEFINE_SPINLOCK(lru_lock);
  60. #define GFS2_GL_HASH_SHIFT 15
  61. #define GFS2_GL_HASH_SIZE (1 << GFS2_GL_HASH_SHIFT)
  62. #define GFS2_GL_HASH_MASK (GFS2_GL_HASH_SIZE - 1)
  63. static struct hlist_bl_head gl_hash_table[GFS2_GL_HASH_SIZE];
  64. static struct dentry *gfs2_root;
  65. /**
  66. * gl_hash() - Turn glock number into hash bucket number
  67. * @lock: The glock number
  68. *
  69. * Returns: The number of the corresponding hash bucket
  70. */
  71. static unsigned int gl_hash(const struct gfs2_sbd *sdp,
  72. const struct lm_lockname *name)
  73. {
  74. unsigned int h;
  75. h = jhash(&name->ln_number, sizeof(u64), 0);
  76. h = jhash(&name->ln_type, sizeof(unsigned int), h);
  77. h = jhash(&sdp, sizeof(struct gfs2_sbd *), h);
  78. h &= GFS2_GL_HASH_MASK;
  79. return h;
  80. }
  81. static inline void spin_lock_bucket(unsigned int hash)
  82. {
  83. struct hlist_bl_head *bl = &gl_hash_table[hash];
  84. bit_spin_lock(0, (unsigned long *)bl);
  85. }
  86. static inline void spin_unlock_bucket(unsigned int hash)
  87. {
  88. struct hlist_bl_head *bl = &gl_hash_table[hash];
  89. __bit_spin_unlock(0, (unsigned long *)bl);
  90. }
  91. static void gfs2_glock_dealloc(struct rcu_head *rcu)
  92. {
  93. struct gfs2_glock *gl = container_of(rcu, struct gfs2_glock, gl_rcu);
  94. if (gl->gl_ops->go_flags & GLOF_ASPACE)
  95. kmem_cache_free(gfs2_glock_aspace_cachep, gl);
  96. else
  97. kmem_cache_free(gfs2_glock_cachep, gl);
  98. }
  99. void gfs2_glock_free(struct gfs2_glock *gl)
  100. {
  101. struct gfs2_sbd *sdp = gl->gl_sbd;
  102. call_rcu(&gl->gl_rcu, gfs2_glock_dealloc);
  103. if (atomic_dec_and_test(&sdp->sd_glock_disposal))
  104. wake_up(&sdp->sd_glock_wait);
  105. }
  106. /**
  107. * gfs2_glock_hold() - increment reference count on glock
  108. * @gl: The glock to hold
  109. *
  110. */
  111. void gfs2_glock_hold(struct gfs2_glock *gl)
  112. {
  113. GLOCK_BUG_ON(gl, atomic_read(&gl->gl_ref) == 0);
  114. atomic_inc(&gl->gl_ref);
  115. }
  116. /**
  117. * demote_ok - Check to see if it's ok to unlock a glock
  118. * @gl: the glock
  119. *
  120. * Returns: 1 if it's ok
  121. */
  122. static int demote_ok(const struct gfs2_glock *gl)
  123. {
  124. const struct gfs2_glock_operations *glops = gl->gl_ops;
  125. if (gl->gl_state == LM_ST_UNLOCKED)
  126. return 0;
  127. if (!list_empty(&gl->gl_holders))
  128. return 0;
  129. if (glops->go_demote_ok)
  130. return glops->go_demote_ok(gl);
  131. return 1;
  132. }
  133. void gfs2_glock_add_to_lru(struct gfs2_glock *gl)
  134. {
  135. spin_lock(&lru_lock);
  136. if (!list_empty(&gl->gl_lru))
  137. list_del_init(&gl->gl_lru);
  138. else
  139. atomic_inc(&lru_count);
  140. list_add_tail(&gl->gl_lru, &lru_list);
  141. set_bit(GLF_LRU, &gl->gl_flags);
  142. spin_unlock(&lru_lock);
  143. }
  144. static void gfs2_glock_remove_from_lru(struct gfs2_glock *gl)
  145. {
  146. spin_lock(&lru_lock);
  147. if (!list_empty(&gl->gl_lru)) {
  148. list_del_init(&gl->gl_lru);
  149. atomic_dec(&lru_count);
  150. clear_bit(GLF_LRU, &gl->gl_flags);
  151. }
  152. spin_unlock(&lru_lock);
  153. }
  154. /**
  155. * __gfs2_glock_schedule_for_reclaim - Add a glock to the reclaim list
  156. * @gl: the glock
  157. *
  158. * If the glock is demotable, then we add it (or move it) to the end
  159. * of the glock LRU list.
  160. */
  161. static void __gfs2_glock_schedule_for_reclaim(struct gfs2_glock *gl)
  162. {
  163. if (demote_ok(gl))
  164. gfs2_glock_add_to_lru(gl);
  165. }
  166. /**
  167. * gfs2_glock_put_nolock() - Decrement reference count on glock
  168. * @gl: The glock to put
  169. *
  170. * This function should only be used if the caller has its own reference
  171. * to the glock, in addition to the one it is dropping.
  172. */
  173. void gfs2_glock_put_nolock(struct gfs2_glock *gl)
  174. {
  175. if (atomic_dec_and_test(&gl->gl_ref))
  176. GLOCK_BUG_ON(gl, 1);
  177. }
  178. /**
  179. * gfs2_glock_put() - Decrement reference count on glock
  180. * @gl: The glock to put
  181. *
  182. */
  183. void gfs2_glock_put(struct gfs2_glock *gl)
  184. {
  185. struct gfs2_sbd *sdp = gl->gl_sbd;
  186. struct address_space *mapping = gfs2_glock2aspace(gl);
  187. if (atomic_dec_and_test(&gl->gl_ref)) {
  188. spin_lock_bucket(gl->gl_hash);
  189. hlist_bl_del_rcu(&gl->gl_list);
  190. spin_unlock_bucket(gl->gl_hash);
  191. gfs2_glock_remove_from_lru(gl);
  192. GLOCK_BUG_ON(gl, !list_empty(&gl->gl_holders));
  193. GLOCK_BUG_ON(gl, mapping && mapping->nrpages);
  194. trace_gfs2_glock_put(gl);
  195. sdp->sd_lockstruct.ls_ops->lm_put_lock(gl);
  196. }
  197. }
  198. /**
  199. * search_bucket() - Find struct gfs2_glock by lock number
  200. * @bucket: the bucket to search
  201. * @name: The lock name
  202. *
  203. * Returns: NULL, or the struct gfs2_glock with the requested number
  204. */
  205. static struct gfs2_glock *search_bucket(unsigned int hash,
  206. const struct gfs2_sbd *sdp,
  207. const struct lm_lockname *name)
  208. {
  209. struct gfs2_glock *gl;
  210. struct hlist_bl_node *h;
  211. hlist_bl_for_each_entry_rcu(gl, h, &gl_hash_table[hash], gl_list) {
  212. if (!lm_name_equal(&gl->gl_name, name))
  213. continue;
  214. if (gl->gl_sbd != sdp)
  215. continue;
  216. if (atomic_inc_not_zero(&gl->gl_ref))
  217. return gl;
  218. }
  219. return NULL;
  220. }
  221. /**
  222. * may_grant - check if its ok to grant a new lock
  223. * @gl: The glock
  224. * @gh: The lock request which we wish to grant
  225. *
  226. * Returns: true if its ok to grant the lock
  227. */
  228. static inline int may_grant(const struct gfs2_glock *gl, const struct gfs2_holder *gh)
  229. {
  230. const struct gfs2_holder *gh_head = list_entry(gl->gl_holders.next, const struct gfs2_holder, gh_list);
  231. if ((gh->gh_state == LM_ST_EXCLUSIVE ||
  232. gh_head->gh_state == LM_ST_EXCLUSIVE) && gh != gh_head)
  233. return 0;
  234. if (gl->gl_state == gh->gh_state)
  235. return 1;
  236. if (gh->gh_flags & GL_EXACT)
  237. return 0;
  238. if (gl->gl_state == LM_ST_EXCLUSIVE) {
  239. if (gh->gh_state == LM_ST_SHARED && gh_head->gh_state == LM_ST_SHARED)
  240. return 1;
  241. if (gh->gh_state == LM_ST_DEFERRED && gh_head->gh_state == LM_ST_DEFERRED)
  242. return 1;
  243. }
  244. if (gl->gl_state != LM_ST_UNLOCKED && (gh->gh_flags & LM_FLAG_ANY))
  245. return 1;
  246. return 0;
  247. }
  248. static void gfs2_holder_wake(struct gfs2_holder *gh)
  249. {
  250. clear_bit(HIF_WAIT, &gh->gh_iflags);
  251. smp_mb__after_clear_bit();
  252. wake_up_bit(&gh->gh_iflags, HIF_WAIT);
  253. }
  254. /**
  255. * do_error - Something unexpected has happened during a lock request
  256. *
  257. */
  258. static inline void do_error(struct gfs2_glock *gl, const int ret)
  259. {
  260. struct gfs2_holder *gh, *tmp;
  261. list_for_each_entry_safe(gh, tmp, &gl->gl_holders, gh_list) {
  262. if (test_bit(HIF_HOLDER, &gh->gh_iflags))
  263. continue;
  264. if (ret & LM_OUT_ERROR)
  265. gh->gh_error = -EIO;
  266. else if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB))
  267. gh->gh_error = GLR_TRYFAILED;
  268. else
  269. continue;
  270. list_del_init(&gh->gh_list);
  271. trace_gfs2_glock_queue(gh, 0);
  272. gfs2_holder_wake(gh);
  273. }
  274. }
  275. /**
  276. * do_promote - promote as many requests as possible on the current queue
  277. * @gl: The glock
  278. *
  279. * Returns: 1 if there is a blocked holder at the head of the list, or 2
  280. * if a type specific operation is underway.
  281. */
  282. static int do_promote(struct gfs2_glock *gl)
  283. __releases(&gl->gl_spin)
  284. __acquires(&gl->gl_spin)
  285. {
  286. const struct gfs2_glock_operations *glops = gl->gl_ops;
  287. struct gfs2_holder *gh, *tmp;
  288. int ret;
  289. restart:
  290. list_for_each_entry_safe(gh, tmp, &gl->gl_holders, gh_list) {
  291. if (test_bit(HIF_HOLDER, &gh->gh_iflags))
  292. continue;
  293. if (may_grant(gl, gh)) {
  294. if (gh->gh_list.prev == &gl->gl_holders &&
  295. glops->go_lock) {
  296. spin_unlock(&gl->gl_spin);
  297. /* FIXME: eliminate this eventually */
  298. ret = glops->go_lock(gh);
  299. spin_lock(&gl->gl_spin);
  300. if (ret) {
  301. if (ret == 1)
  302. return 2;
  303. gh->gh_error = ret;
  304. list_del_init(&gh->gh_list);
  305. trace_gfs2_glock_queue(gh, 0);
  306. gfs2_holder_wake(gh);
  307. goto restart;
  308. }
  309. set_bit(HIF_HOLDER, &gh->gh_iflags);
  310. trace_gfs2_promote(gh, 1);
  311. gfs2_holder_wake(gh);
  312. goto restart;
  313. }
  314. set_bit(HIF_HOLDER, &gh->gh_iflags);
  315. trace_gfs2_promote(gh, 0);
  316. gfs2_holder_wake(gh);
  317. continue;
  318. }
  319. if (gh->gh_list.prev == &gl->gl_holders)
  320. return 1;
  321. do_error(gl, 0);
  322. break;
  323. }
  324. return 0;
  325. }
  326. /**
  327. * find_first_waiter - find the first gh that's waiting for the glock
  328. * @gl: the glock
  329. */
  330. static inline struct gfs2_holder *find_first_waiter(const struct gfs2_glock *gl)
  331. {
  332. struct gfs2_holder *gh;
  333. list_for_each_entry(gh, &gl->gl_holders, gh_list) {
  334. if (!test_bit(HIF_HOLDER, &gh->gh_iflags))
  335. return gh;
  336. }
  337. return NULL;
  338. }
  339. /**
  340. * state_change - record that the glock is now in a different state
  341. * @gl: the glock
  342. * @new_state the new state
  343. *
  344. */
  345. static void state_change(struct gfs2_glock *gl, unsigned int new_state)
  346. {
  347. int held1, held2;
  348. held1 = (gl->gl_state != LM_ST_UNLOCKED);
  349. held2 = (new_state != LM_ST_UNLOCKED);
  350. if (held1 != held2) {
  351. if (held2)
  352. gfs2_glock_hold(gl);
  353. else
  354. gfs2_glock_put_nolock(gl);
  355. }
  356. if (held1 && held2 && list_empty(&gl->gl_holders))
  357. clear_bit(GLF_QUEUED, &gl->gl_flags);
  358. gl->gl_state = new_state;
  359. gl->gl_tchange = jiffies;
  360. }
  361. static void gfs2_demote_wake(struct gfs2_glock *gl)
  362. {
  363. gl->gl_demote_state = LM_ST_EXCLUSIVE;
  364. clear_bit(GLF_DEMOTE, &gl->gl_flags);
  365. smp_mb__after_clear_bit();
  366. wake_up_bit(&gl->gl_flags, GLF_DEMOTE);
  367. }
  368. /**
  369. * finish_xmote - The DLM has replied to one of our lock requests
  370. * @gl: The glock
  371. * @ret: The status from the DLM
  372. *
  373. */
  374. static void finish_xmote(struct gfs2_glock *gl, unsigned int ret)
  375. {
  376. const struct gfs2_glock_operations *glops = gl->gl_ops;
  377. struct gfs2_holder *gh;
  378. unsigned state = ret & LM_OUT_ST_MASK;
  379. int rv;
  380. spin_lock(&gl->gl_spin);
  381. trace_gfs2_glock_state_change(gl, state);
  382. state_change(gl, state);
  383. gh = find_first_waiter(gl);
  384. /* Demote to UN request arrived during demote to SH or DF */
  385. if (test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags) &&
  386. state != LM_ST_UNLOCKED && gl->gl_demote_state == LM_ST_UNLOCKED)
  387. gl->gl_target = LM_ST_UNLOCKED;
  388. /* Check for state != intended state */
  389. if (unlikely(state != gl->gl_target)) {
  390. if (gh && !test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags)) {
  391. /* move to back of queue and try next entry */
  392. if (ret & LM_OUT_CANCELED) {
  393. if ((gh->gh_flags & LM_FLAG_PRIORITY) == 0)
  394. list_move_tail(&gh->gh_list, &gl->gl_holders);
  395. gh = find_first_waiter(gl);
  396. gl->gl_target = gh->gh_state;
  397. goto retry;
  398. }
  399. /* Some error or failed "try lock" - report it */
  400. if ((ret & LM_OUT_ERROR) ||
  401. (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB))) {
  402. gl->gl_target = gl->gl_state;
  403. do_error(gl, ret);
  404. goto out;
  405. }
  406. }
  407. switch(state) {
  408. /* Unlocked due to conversion deadlock, try again */
  409. case LM_ST_UNLOCKED:
  410. retry:
  411. do_xmote(gl, gh, gl->gl_target);
  412. break;
  413. /* Conversion fails, unlock and try again */
  414. case LM_ST_SHARED:
  415. case LM_ST_DEFERRED:
  416. do_xmote(gl, gh, LM_ST_UNLOCKED);
  417. break;
  418. default: /* Everything else */
  419. printk(KERN_ERR "GFS2: wanted %u got %u\n", gl->gl_target, state);
  420. GLOCK_BUG_ON(gl, 1);
  421. }
  422. spin_unlock(&gl->gl_spin);
  423. return;
  424. }
  425. /* Fast path - we got what we asked for */
  426. if (test_and_clear_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags))
  427. gfs2_demote_wake(gl);
  428. if (state != LM_ST_UNLOCKED) {
  429. if (glops->go_xmote_bh) {
  430. spin_unlock(&gl->gl_spin);
  431. rv = glops->go_xmote_bh(gl, gh);
  432. spin_lock(&gl->gl_spin);
  433. if (rv) {
  434. do_error(gl, rv);
  435. goto out;
  436. }
  437. }
  438. rv = do_promote(gl);
  439. if (rv == 2)
  440. goto out_locked;
  441. }
  442. out:
  443. clear_bit(GLF_LOCK, &gl->gl_flags);
  444. out_locked:
  445. spin_unlock(&gl->gl_spin);
  446. }
  447. /**
  448. * do_xmote - Calls the DLM to change the state of a lock
  449. * @gl: The lock state
  450. * @gh: The holder (only for promotes)
  451. * @target: The target lock state
  452. *
  453. */
  454. static void do_xmote(struct gfs2_glock *gl, struct gfs2_holder *gh, unsigned int target)
  455. __releases(&gl->gl_spin)
  456. __acquires(&gl->gl_spin)
  457. {
  458. const struct gfs2_glock_operations *glops = gl->gl_ops;
  459. struct gfs2_sbd *sdp = gl->gl_sbd;
  460. unsigned int lck_flags = gh ? gh->gh_flags : 0;
  461. int ret;
  462. lck_flags &= (LM_FLAG_TRY | LM_FLAG_TRY_1CB | LM_FLAG_NOEXP |
  463. LM_FLAG_PRIORITY);
  464. GLOCK_BUG_ON(gl, gl->gl_state == target);
  465. GLOCK_BUG_ON(gl, gl->gl_state == gl->gl_target);
  466. if ((target == LM_ST_UNLOCKED || target == LM_ST_DEFERRED) &&
  467. glops->go_inval) {
  468. set_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags);
  469. do_error(gl, 0); /* Fail queued try locks */
  470. }
  471. gl->gl_req = target;
  472. spin_unlock(&gl->gl_spin);
  473. if (glops->go_xmote_th)
  474. glops->go_xmote_th(gl);
  475. if (test_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags))
  476. glops->go_inval(gl, target == LM_ST_DEFERRED ? 0 : DIO_METADATA);
  477. clear_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags);
  478. gfs2_glock_hold(gl);
  479. if (sdp->sd_lockstruct.ls_ops->lm_lock) {
  480. /* lock_dlm */
  481. ret = sdp->sd_lockstruct.ls_ops->lm_lock(gl, target, lck_flags);
  482. GLOCK_BUG_ON(gl, ret);
  483. } else { /* lock_nolock */
  484. finish_xmote(gl, target);
  485. if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
  486. gfs2_glock_put(gl);
  487. }
  488. spin_lock(&gl->gl_spin);
  489. }
  490. /**
  491. * find_first_holder - find the first "holder" gh
  492. * @gl: the glock
  493. */
  494. static inline struct gfs2_holder *find_first_holder(const struct gfs2_glock *gl)
  495. {
  496. struct gfs2_holder *gh;
  497. if (!list_empty(&gl->gl_holders)) {
  498. gh = list_entry(gl->gl_holders.next, struct gfs2_holder, gh_list);
  499. if (test_bit(HIF_HOLDER, &gh->gh_iflags))
  500. return gh;
  501. }
  502. return NULL;
  503. }
  504. /**
  505. * run_queue - do all outstanding tasks related to a glock
  506. * @gl: The glock in question
  507. * @nonblock: True if we must not block in run_queue
  508. *
  509. */
  510. static void run_queue(struct gfs2_glock *gl, const int nonblock)
  511. __releases(&gl->gl_spin)
  512. __acquires(&gl->gl_spin)
  513. {
  514. struct gfs2_holder *gh = NULL;
  515. int ret;
  516. if (test_and_set_bit(GLF_LOCK, &gl->gl_flags))
  517. return;
  518. GLOCK_BUG_ON(gl, test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags));
  519. if (test_bit(GLF_DEMOTE, &gl->gl_flags) &&
  520. gl->gl_demote_state != gl->gl_state) {
  521. if (find_first_holder(gl))
  522. goto out_unlock;
  523. if (nonblock)
  524. goto out_sched;
  525. set_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags);
  526. GLOCK_BUG_ON(gl, gl->gl_demote_state == LM_ST_EXCLUSIVE);
  527. gl->gl_target = gl->gl_demote_state;
  528. } else {
  529. if (test_bit(GLF_DEMOTE, &gl->gl_flags))
  530. gfs2_demote_wake(gl);
  531. ret = do_promote(gl);
  532. if (ret == 0)
  533. goto out_unlock;
  534. if (ret == 2)
  535. goto out;
  536. gh = find_first_waiter(gl);
  537. gl->gl_target = gh->gh_state;
  538. if (!(gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)))
  539. do_error(gl, 0); /* Fail queued try locks */
  540. }
  541. do_xmote(gl, gh, gl->gl_target);
  542. out:
  543. return;
  544. out_sched:
  545. clear_bit(GLF_LOCK, &gl->gl_flags);
  546. smp_mb__after_clear_bit();
  547. gfs2_glock_hold(gl);
  548. if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
  549. gfs2_glock_put_nolock(gl);
  550. return;
  551. out_unlock:
  552. clear_bit(GLF_LOCK, &gl->gl_flags);
  553. smp_mb__after_clear_bit();
  554. return;
  555. }
  556. static void delete_work_func(struct work_struct *work)
  557. {
  558. struct gfs2_glock *gl = container_of(work, struct gfs2_glock, gl_delete);
  559. struct gfs2_sbd *sdp = gl->gl_sbd;
  560. struct gfs2_inode *ip;
  561. struct inode *inode;
  562. u64 no_addr = gl->gl_name.ln_number;
  563. ip = gl->gl_object;
  564. /* Note: Unsafe to dereference ip as we don't hold right refs/locks */
  565. if (ip)
  566. inode = gfs2_ilookup(sdp->sd_vfs, no_addr, 1);
  567. else
  568. inode = gfs2_lookup_by_inum(sdp, no_addr, NULL, GFS2_BLKST_UNLINKED);
  569. if (inode && !IS_ERR(inode)) {
  570. d_prune_aliases(inode);
  571. iput(inode);
  572. }
  573. gfs2_glock_put(gl);
  574. }
  575. static void glock_work_func(struct work_struct *work)
  576. {
  577. unsigned long delay = 0;
  578. struct gfs2_glock *gl = container_of(work, struct gfs2_glock, gl_work.work);
  579. int drop_ref = 0;
  580. if (test_and_clear_bit(GLF_REPLY_PENDING, &gl->gl_flags)) {
  581. finish_xmote(gl, gl->gl_reply);
  582. drop_ref = 1;
  583. }
  584. spin_lock(&gl->gl_spin);
  585. if (test_and_clear_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) &&
  586. gl->gl_state != LM_ST_UNLOCKED &&
  587. gl->gl_demote_state != LM_ST_EXCLUSIVE) {
  588. unsigned long holdtime, now = jiffies;
  589. holdtime = gl->gl_tchange + gl->gl_ops->go_min_hold_time;
  590. if (time_before(now, holdtime))
  591. delay = holdtime - now;
  592. set_bit(delay ? GLF_PENDING_DEMOTE : GLF_DEMOTE, &gl->gl_flags);
  593. }
  594. run_queue(gl, 0);
  595. spin_unlock(&gl->gl_spin);
  596. if (!delay ||
  597. queue_delayed_work(glock_workqueue, &gl->gl_work, delay) == 0)
  598. gfs2_glock_put(gl);
  599. if (drop_ref)
  600. gfs2_glock_put(gl);
  601. }
  602. /**
  603. * gfs2_glock_get() - Get a glock, or create one if one doesn't exist
  604. * @sdp: The GFS2 superblock
  605. * @number: the lock number
  606. * @glops: The glock_operations to use
  607. * @create: If 0, don't create the glock if it doesn't exist
  608. * @glp: the glock is returned here
  609. *
  610. * This does not lock a glock, just finds/creates structures for one.
  611. *
  612. * Returns: errno
  613. */
  614. int gfs2_glock_get(struct gfs2_sbd *sdp, u64 number,
  615. const struct gfs2_glock_operations *glops, int create,
  616. struct gfs2_glock **glp)
  617. {
  618. struct super_block *s = sdp->sd_vfs;
  619. struct lm_lockname name = { .ln_number = number, .ln_type = glops->go_type };
  620. struct gfs2_glock *gl, *tmp;
  621. unsigned int hash = gl_hash(sdp, &name);
  622. struct address_space *mapping;
  623. struct kmem_cache *cachep;
  624. rcu_read_lock();
  625. gl = search_bucket(hash, sdp, &name);
  626. rcu_read_unlock();
  627. *glp = gl;
  628. if (gl)
  629. return 0;
  630. if (!create)
  631. return -ENOENT;
  632. if (glops->go_flags & GLOF_ASPACE)
  633. cachep = gfs2_glock_aspace_cachep;
  634. else
  635. cachep = gfs2_glock_cachep;
  636. gl = kmem_cache_alloc(cachep, GFP_KERNEL);
  637. if (!gl)
  638. return -ENOMEM;
  639. atomic_inc(&sdp->sd_glock_disposal);
  640. gl->gl_flags = 0;
  641. gl->gl_name = name;
  642. atomic_set(&gl->gl_ref, 1);
  643. gl->gl_state = LM_ST_UNLOCKED;
  644. gl->gl_target = LM_ST_UNLOCKED;
  645. gl->gl_demote_state = LM_ST_EXCLUSIVE;
  646. gl->gl_hash = hash;
  647. gl->gl_ops = glops;
  648. snprintf(gl->gl_strname, GDLM_STRNAME_BYTES, "%8x%16llx", name.ln_type, (unsigned long long)number);
  649. memset(&gl->gl_lksb, 0, sizeof(struct dlm_lksb));
  650. gl->gl_lksb.sb_lvbptr = gl->gl_lvb;
  651. gl->gl_tchange = jiffies;
  652. gl->gl_object = NULL;
  653. gl->gl_sbd = sdp;
  654. INIT_DELAYED_WORK(&gl->gl_work, glock_work_func);
  655. INIT_WORK(&gl->gl_delete, delete_work_func);
  656. mapping = gfs2_glock2aspace(gl);
  657. if (mapping) {
  658. mapping->a_ops = &gfs2_meta_aops;
  659. mapping->host = s->s_bdev->bd_inode;
  660. mapping->flags = 0;
  661. mapping_set_gfp_mask(mapping, GFP_NOFS);
  662. mapping->assoc_mapping = NULL;
  663. mapping->backing_dev_info = s->s_bdi;
  664. mapping->writeback_index = 0;
  665. }
  666. spin_lock_bucket(hash);
  667. tmp = search_bucket(hash, sdp, &name);
  668. if (tmp) {
  669. spin_unlock_bucket(hash);
  670. kmem_cache_free(cachep, gl);
  671. atomic_dec(&sdp->sd_glock_disposal);
  672. gl = tmp;
  673. } else {
  674. hlist_bl_add_head_rcu(&gl->gl_list, &gl_hash_table[hash]);
  675. spin_unlock_bucket(hash);
  676. }
  677. *glp = gl;
  678. return 0;
  679. }
  680. /**
  681. * gfs2_holder_init - initialize a struct gfs2_holder in the default way
  682. * @gl: the glock
  683. * @state: the state we're requesting
  684. * @flags: the modifier flags
  685. * @gh: the holder structure
  686. *
  687. */
  688. void gfs2_holder_init(struct gfs2_glock *gl, unsigned int state, unsigned flags,
  689. struct gfs2_holder *gh)
  690. {
  691. INIT_LIST_HEAD(&gh->gh_list);
  692. gh->gh_gl = gl;
  693. gh->gh_ip = (unsigned long)__builtin_return_address(0);
  694. gh->gh_owner_pid = get_pid(task_pid(current));
  695. gh->gh_state = state;
  696. gh->gh_flags = flags;
  697. gh->gh_error = 0;
  698. gh->gh_iflags = 0;
  699. gfs2_glock_hold(gl);
  700. }
  701. /**
  702. * gfs2_holder_reinit - reinitialize a struct gfs2_holder so we can requeue it
  703. * @state: the state we're requesting
  704. * @flags: the modifier flags
  705. * @gh: the holder structure
  706. *
  707. * Don't mess with the glock.
  708. *
  709. */
  710. void gfs2_holder_reinit(unsigned int state, unsigned flags, struct gfs2_holder *gh)
  711. {
  712. gh->gh_state = state;
  713. gh->gh_flags = flags;
  714. gh->gh_iflags = 0;
  715. gh->gh_ip = (unsigned long)__builtin_return_address(0);
  716. if (gh->gh_owner_pid)
  717. put_pid(gh->gh_owner_pid);
  718. gh->gh_owner_pid = get_pid(task_pid(current));
  719. }
  720. /**
  721. * gfs2_holder_uninit - uninitialize a holder structure (drop glock reference)
  722. * @gh: the holder structure
  723. *
  724. */
  725. void gfs2_holder_uninit(struct gfs2_holder *gh)
  726. {
  727. put_pid(gh->gh_owner_pid);
  728. gfs2_glock_put(gh->gh_gl);
  729. gh->gh_gl = NULL;
  730. gh->gh_ip = 0;
  731. }
  732. /**
  733. * gfs2_glock_holder_wait
  734. * @word: unused
  735. *
  736. * This function and gfs2_glock_demote_wait both show up in the WCHAN
  737. * field. Thus I've separated these otherwise identical functions in
  738. * order to be more informative to the user.
  739. */
  740. static int gfs2_glock_holder_wait(void *word)
  741. {
  742. schedule();
  743. return 0;
  744. }
  745. static int gfs2_glock_demote_wait(void *word)
  746. {
  747. schedule();
  748. return 0;
  749. }
  750. static void wait_on_holder(struct gfs2_holder *gh)
  751. {
  752. might_sleep();
  753. wait_on_bit(&gh->gh_iflags, HIF_WAIT, gfs2_glock_holder_wait, TASK_UNINTERRUPTIBLE);
  754. }
  755. static void wait_on_demote(struct gfs2_glock *gl)
  756. {
  757. might_sleep();
  758. wait_on_bit(&gl->gl_flags, GLF_DEMOTE, gfs2_glock_demote_wait, TASK_UNINTERRUPTIBLE);
  759. }
  760. /**
  761. * handle_callback - process a demote request
  762. * @gl: the glock
  763. * @state: the state the caller wants us to change to
  764. *
  765. * There are only two requests that we are going to see in actual
  766. * practise: LM_ST_SHARED and LM_ST_UNLOCKED
  767. */
  768. static void handle_callback(struct gfs2_glock *gl, unsigned int state,
  769. unsigned long delay)
  770. {
  771. int bit = delay ? GLF_PENDING_DEMOTE : GLF_DEMOTE;
  772. set_bit(bit, &gl->gl_flags);
  773. if (gl->gl_demote_state == LM_ST_EXCLUSIVE) {
  774. gl->gl_demote_state = state;
  775. gl->gl_demote_time = jiffies;
  776. } else if (gl->gl_demote_state != LM_ST_UNLOCKED &&
  777. gl->gl_demote_state != state) {
  778. gl->gl_demote_state = LM_ST_UNLOCKED;
  779. }
  780. if (gl->gl_ops->go_callback)
  781. gl->gl_ops->go_callback(gl);
  782. trace_gfs2_demote_rq(gl);
  783. }
  784. /**
  785. * gfs2_glock_wait - wait on a glock acquisition
  786. * @gh: the glock holder
  787. *
  788. * Returns: 0 on success
  789. */
  790. int gfs2_glock_wait(struct gfs2_holder *gh)
  791. {
  792. wait_on_holder(gh);
  793. return gh->gh_error;
  794. }
  795. void gfs2_print_dbg(struct seq_file *seq, const char *fmt, ...)
  796. {
  797. struct va_format vaf;
  798. va_list args;
  799. va_start(args, fmt);
  800. if (seq) {
  801. struct gfs2_glock_iter *gi = seq->private;
  802. vsprintf(gi->string, fmt, args);
  803. seq_printf(seq, gi->string);
  804. } else {
  805. vaf.fmt = fmt;
  806. vaf.va = &args;
  807. printk(KERN_ERR " %pV", &vaf);
  808. }
  809. va_end(args);
  810. }
  811. /**
  812. * add_to_queue - Add a holder to the wait queue (but look for recursion)
  813. * @gh: the holder structure to add
  814. *
  815. * Eventually we should move the recursive locking trap to a
  816. * debugging option or something like that. This is the fast
  817. * path and needs to have the minimum number of distractions.
  818. *
  819. */
  820. static inline void add_to_queue(struct gfs2_holder *gh)
  821. __releases(&gl->gl_spin)
  822. __acquires(&gl->gl_spin)
  823. {
  824. struct gfs2_glock *gl = gh->gh_gl;
  825. struct gfs2_sbd *sdp = gl->gl_sbd;
  826. struct list_head *insert_pt = NULL;
  827. struct gfs2_holder *gh2;
  828. int try_lock = 0;
  829. BUG_ON(gh->gh_owner_pid == NULL);
  830. if (test_and_set_bit(HIF_WAIT, &gh->gh_iflags))
  831. BUG();
  832. if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) {
  833. if (test_bit(GLF_LOCK, &gl->gl_flags))
  834. try_lock = 1;
  835. if (test_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags))
  836. goto fail;
  837. }
  838. list_for_each_entry(gh2, &gl->gl_holders, gh_list) {
  839. if (unlikely(gh2->gh_owner_pid == gh->gh_owner_pid &&
  840. (gh->gh_gl->gl_ops->go_type != LM_TYPE_FLOCK)))
  841. goto trap_recursive;
  842. if (try_lock &&
  843. !(gh2->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) &&
  844. !may_grant(gl, gh)) {
  845. fail:
  846. gh->gh_error = GLR_TRYFAILED;
  847. gfs2_holder_wake(gh);
  848. return;
  849. }
  850. if (test_bit(HIF_HOLDER, &gh2->gh_iflags))
  851. continue;
  852. if (unlikely((gh->gh_flags & LM_FLAG_PRIORITY) && !insert_pt))
  853. insert_pt = &gh2->gh_list;
  854. }
  855. set_bit(GLF_QUEUED, &gl->gl_flags);
  856. trace_gfs2_glock_queue(gh, 1);
  857. if (likely(insert_pt == NULL)) {
  858. list_add_tail(&gh->gh_list, &gl->gl_holders);
  859. if (unlikely(gh->gh_flags & LM_FLAG_PRIORITY))
  860. goto do_cancel;
  861. return;
  862. }
  863. list_add_tail(&gh->gh_list, insert_pt);
  864. do_cancel:
  865. gh = list_entry(gl->gl_holders.next, struct gfs2_holder, gh_list);
  866. if (!(gh->gh_flags & LM_FLAG_PRIORITY)) {
  867. spin_unlock(&gl->gl_spin);
  868. if (sdp->sd_lockstruct.ls_ops->lm_cancel)
  869. sdp->sd_lockstruct.ls_ops->lm_cancel(gl);
  870. spin_lock(&gl->gl_spin);
  871. }
  872. return;
  873. trap_recursive:
  874. print_symbol(KERN_ERR "original: %s\n", gh2->gh_ip);
  875. printk(KERN_ERR "pid: %d\n", pid_nr(gh2->gh_owner_pid));
  876. printk(KERN_ERR "lock type: %d req lock state : %d\n",
  877. gh2->gh_gl->gl_name.ln_type, gh2->gh_state);
  878. print_symbol(KERN_ERR "new: %s\n", gh->gh_ip);
  879. printk(KERN_ERR "pid: %d\n", pid_nr(gh->gh_owner_pid));
  880. printk(KERN_ERR "lock type: %d req lock state : %d\n",
  881. gh->gh_gl->gl_name.ln_type, gh->gh_state);
  882. __dump_glock(NULL, gl);
  883. BUG();
  884. }
  885. /**
  886. * gfs2_glock_nq - enqueue a struct gfs2_holder onto a glock (acquire a glock)
  887. * @gh: the holder structure
  888. *
  889. * if (gh->gh_flags & GL_ASYNC), this never returns an error
  890. *
  891. * Returns: 0, GLR_TRYFAILED, or errno on failure
  892. */
  893. int gfs2_glock_nq(struct gfs2_holder *gh)
  894. {
  895. struct gfs2_glock *gl = gh->gh_gl;
  896. struct gfs2_sbd *sdp = gl->gl_sbd;
  897. int error = 0;
  898. if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
  899. return -EIO;
  900. if (test_bit(GLF_LRU, &gl->gl_flags))
  901. gfs2_glock_remove_from_lru(gl);
  902. spin_lock(&gl->gl_spin);
  903. add_to_queue(gh);
  904. if ((LM_FLAG_NOEXP & gh->gh_flags) &&
  905. test_and_clear_bit(GLF_FROZEN, &gl->gl_flags))
  906. set_bit(GLF_REPLY_PENDING, &gl->gl_flags);
  907. run_queue(gl, 1);
  908. spin_unlock(&gl->gl_spin);
  909. if (!(gh->gh_flags & GL_ASYNC))
  910. error = gfs2_glock_wait(gh);
  911. return error;
  912. }
  913. /**
  914. * gfs2_glock_poll - poll to see if an async request has been completed
  915. * @gh: the holder
  916. *
  917. * Returns: 1 if the request is ready to be gfs2_glock_wait()ed on
  918. */
  919. int gfs2_glock_poll(struct gfs2_holder *gh)
  920. {
  921. return test_bit(HIF_WAIT, &gh->gh_iflags) ? 0 : 1;
  922. }
  923. /**
  924. * gfs2_glock_dq - dequeue a struct gfs2_holder from a glock (release a glock)
  925. * @gh: the glock holder
  926. *
  927. */
  928. void gfs2_glock_dq(struct gfs2_holder *gh)
  929. {
  930. struct gfs2_glock *gl = gh->gh_gl;
  931. const struct gfs2_glock_operations *glops = gl->gl_ops;
  932. unsigned delay = 0;
  933. int fast_path = 0;
  934. spin_lock(&gl->gl_spin);
  935. if (gh->gh_flags & GL_NOCACHE)
  936. handle_callback(gl, LM_ST_UNLOCKED, 0);
  937. list_del_init(&gh->gh_list);
  938. if (find_first_holder(gl) == NULL) {
  939. if (glops->go_unlock) {
  940. GLOCK_BUG_ON(gl, test_and_set_bit(GLF_LOCK, &gl->gl_flags));
  941. spin_unlock(&gl->gl_spin);
  942. glops->go_unlock(gh);
  943. spin_lock(&gl->gl_spin);
  944. clear_bit(GLF_LOCK, &gl->gl_flags);
  945. }
  946. if (list_empty(&gl->gl_holders) &&
  947. !test_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) &&
  948. !test_bit(GLF_DEMOTE, &gl->gl_flags))
  949. fast_path = 1;
  950. }
  951. if (!test_bit(GLF_LFLUSH, &gl->gl_flags))
  952. __gfs2_glock_schedule_for_reclaim(gl);
  953. trace_gfs2_glock_queue(gh, 0);
  954. spin_unlock(&gl->gl_spin);
  955. if (likely(fast_path))
  956. return;
  957. gfs2_glock_hold(gl);
  958. if (test_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) &&
  959. !test_bit(GLF_DEMOTE, &gl->gl_flags))
  960. delay = gl->gl_ops->go_min_hold_time;
  961. if (queue_delayed_work(glock_workqueue, &gl->gl_work, delay) == 0)
  962. gfs2_glock_put(gl);
  963. }
  964. void gfs2_glock_dq_wait(struct gfs2_holder *gh)
  965. {
  966. struct gfs2_glock *gl = gh->gh_gl;
  967. gfs2_glock_dq(gh);
  968. wait_on_demote(gl);
  969. }
  970. /**
  971. * gfs2_glock_dq_uninit - dequeue a holder from a glock and initialize it
  972. * @gh: the holder structure
  973. *
  974. */
  975. void gfs2_glock_dq_uninit(struct gfs2_holder *gh)
  976. {
  977. gfs2_glock_dq(gh);
  978. gfs2_holder_uninit(gh);
  979. }
  980. /**
  981. * gfs2_glock_nq_num - acquire a glock based on lock number
  982. * @sdp: the filesystem
  983. * @number: the lock number
  984. * @glops: the glock operations for the type of glock
  985. * @state: the state to acquire the glock in
  986. * @flags: modifier flags for the acquisition
  987. * @gh: the struct gfs2_holder
  988. *
  989. * Returns: errno
  990. */
  991. int gfs2_glock_nq_num(struct gfs2_sbd *sdp, u64 number,
  992. const struct gfs2_glock_operations *glops,
  993. unsigned int state, int flags, struct gfs2_holder *gh)
  994. {
  995. struct gfs2_glock *gl;
  996. int error;
  997. error = gfs2_glock_get(sdp, number, glops, CREATE, &gl);
  998. if (!error) {
  999. error = gfs2_glock_nq_init(gl, state, flags, gh);
  1000. gfs2_glock_put(gl);
  1001. }
  1002. return error;
  1003. }
  1004. /**
  1005. * glock_compare - Compare two struct gfs2_glock structures for sorting
  1006. * @arg_a: the first structure
  1007. * @arg_b: the second structure
  1008. *
  1009. */
  1010. static int glock_compare(const void *arg_a, const void *arg_b)
  1011. {
  1012. const struct gfs2_holder *gh_a = *(const struct gfs2_holder **)arg_a;
  1013. const struct gfs2_holder *gh_b = *(const struct gfs2_holder **)arg_b;
  1014. const struct lm_lockname *a = &gh_a->gh_gl->gl_name;
  1015. const struct lm_lockname *b = &gh_b->gh_gl->gl_name;
  1016. if (a->ln_number > b->ln_number)
  1017. return 1;
  1018. if (a->ln_number < b->ln_number)
  1019. return -1;
  1020. BUG_ON(gh_a->gh_gl->gl_ops->go_type == gh_b->gh_gl->gl_ops->go_type);
  1021. return 0;
  1022. }
  1023. /**
  1024. * nq_m_sync - synchonously acquire more than one glock in deadlock free order
  1025. * @num_gh: the number of structures
  1026. * @ghs: an array of struct gfs2_holder structures
  1027. *
  1028. * Returns: 0 on success (all glocks acquired),
  1029. * errno on failure (no glocks acquired)
  1030. */
  1031. static int nq_m_sync(unsigned int num_gh, struct gfs2_holder *ghs,
  1032. struct gfs2_holder **p)
  1033. {
  1034. unsigned int x;
  1035. int error = 0;
  1036. for (x = 0; x < num_gh; x++)
  1037. p[x] = &ghs[x];
  1038. sort(p, num_gh, sizeof(struct gfs2_holder *), glock_compare, NULL);
  1039. for (x = 0; x < num_gh; x++) {
  1040. p[x]->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
  1041. error = gfs2_glock_nq(p[x]);
  1042. if (error) {
  1043. while (x--)
  1044. gfs2_glock_dq(p[x]);
  1045. break;
  1046. }
  1047. }
  1048. return error;
  1049. }
  1050. /**
  1051. * gfs2_glock_nq_m - acquire multiple glocks
  1052. * @num_gh: the number of structures
  1053. * @ghs: an array of struct gfs2_holder structures
  1054. *
  1055. *
  1056. * Returns: 0 on success (all glocks acquired),
  1057. * errno on failure (no glocks acquired)
  1058. */
  1059. int gfs2_glock_nq_m(unsigned int num_gh, struct gfs2_holder *ghs)
  1060. {
  1061. struct gfs2_holder *tmp[4];
  1062. struct gfs2_holder **pph = tmp;
  1063. int error = 0;
  1064. switch(num_gh) {
  1065. case 0:
  1066. return 0;
  1067. case 1:
  1068. ghs->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
  1069. return gfs2_glock_nq(ghs);
  1070. default:
  1071. if (num_gh <= 4)
  1072. break;
  1073. pph = kmalloc(num_gh * sizeof(struct gfs2_holder *), GFP_NOFS);
  1074. if (!pph)
  1075. return -ENOMEM;
  1076. }
  1077. error = nq_m_sync(num_gh, ghs, pph);
  1078. if (pph != tmp)
  1079. kfree(pph);
  1080. return error;
  1081. }
  1082. /**
  1083. * gfs2_glock_dq_m - release multiple glocks
  1084. * @num_gh: the number of structures
  1085. * @ghs: an array of struct gfs2_holder structures
  1086. *
  1087. */
  1088. void gfs2_glock_dq_m(unsigned int num_gh, struct gfs2_holder *ghs)
  1089. {
  1090. while (num_gh--)
  1091. gfs2_glock_dq(&ghs[num_gh]);
  1092. }
  1093. /**
  1094. * gfs2_glock_dq_uninit_m - release multiple glocks
  1095. * @num_gh: the number of structures
  1096. * @ghs: an array of struct gfs2_holder structures
  1097. *
  1098. */
  1099. void gfs2_glock_dq_uninit_m(unsigned int num_gh, struct gfs2_holder *ghs)
  1100. {
  1101. while (num_gh--)
  1102. gfs2_glock_dq_uninit(&ghs[num_gh]);
  1103. }
  1104. void gfs2_glock_cb(struct gfs2_glock *gl, unsigned int state)
  1105. {
  1106. unsigned long delay = 0;
  1107. unsigned long holdtime;
  1108. unsigned long now = jiffies;
  1109. gfs2_glock_hold(gl);
  1110. holdtime = gl->gl_tchange + gl->gl_ops->go_min_hold_time;
  1111. if (test_bit(GLF_QUEUED, &gl->gl_flags)) {
  1112. if (time_before(now, holdtime))
  1113. delay = holdtime - now;
  1114. if (test_bit(GLF_REPLY_PENDING, &gl->gl_flags))
  1115. delay = gl->gl_ops->go_min_hold_time;
  1116. }
  1117. spin_lock(&gl->gl_spin);
  1118. handle_callback(gl, state, delay);
  1119. spin_unlock(&gl->gl_spin);
  1120. if (queue_delayed_work(glock_workqueue, &gl->gl_work, delay) == 0)
  1121. gfs2_glock_put(gl);
  1122. }
  1123. /**
  1124. * gfs2_should_freeze - Figure out if glock should be frozen
  1125. * @gl: The glock in question
  1126. *
  1127. * Glocks are not frozen if (a) the result of the dlm operation is
  1128. * an error, (b) the locking operation was an unlock operation or
  1129. * (c) if there is a "noexp" flagged request anywhere in the queue
  1130. *
  1131. * Returns: 1 if freezing should occur, 0 otherwise
  1132. */
  1133. static int gfs2_should_freeze(const struct gfs2_glock *gl)
  1134. {
  1135. const struct gfs2_holder *gh;
  1136. if (gl->gl_reply & ~LM_OUT_ST_MASK)
  1137. return 0;
  1138. if (gl->gl_target == LM_ST_UNLOCKED)
  1139. return 0;
  1140. list_for_each_entry(gh, &gl->gl_holders, gh_list) {
  1141. if (test_bit(HIF_HOLDER, &gh->gh_iflags))
  1142. continue;
  1143. if (LM_FLAG_NOEXP & gh->gh_flags)
  1144. return 0;
  1145. }
  1146. return 1;
  1147. }
  1148. /**
  1149. * gfs2_glock_complete - Callback used by locking
  1150. * @gl: Pointer to the glock
  1151. * @ret: The return value from the dlm
  1152. *
  1153. * The gl_reply field is under the gl_spin lock so that it is ok
  1154. * to use a bitfield shared with other glock state fields.
  1155. */
  1156. void gfs2_glock_complete(struct gfs2_glock *gl, int ret)
  1157. {
  1158. struct lm_lockstruct *ls = &gl->gl_sbd->sd_lockstruct;
  1159. spin_lock(&gl->gl_spin);
  1160. gl->gl_reply = ret;
  1161. if (unlikely(test_bit(DFL_BLOCK_LOCKS, &ls->ls_flags))) {
  1162. if (gfs2_should_freeze(gl)) {
  1163. set_bit(GLF_FROZEN, &gl->gl_flags);
  1164. spin_unlock(&gl->gl_spin);
  1165. return;
  1166. }
  1167. }
  1168. spin_unlock(&gl->gl_spin);
  1169. set_bit(GLF_REPLY_PENDING, &gl->gl_flags);
  1170. smp_wmb();
  1171. gfs2_glock_hold(gl);
  1172. if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
  1173. gfs2_glock_put(gl);
  1174. }
  1175. static int gfs2_shrink_glock_memory(struct shrinker *shrink, int nr, gfp_t gfp_mask)
  1176. {
  1177. struct gfs2_glock *gl;
  1178. int may_demote;
  1179. int nr_skipped = 0;
  1180. LIST_HEAD(skipped);
  1181. if (nr == 0)
  1182. goto out;
  1183. if (!(gfp_mask & __GFP_FS))
  1184. return -1;
  1185. spin_lock(&lru_lock);
  1186. while(nr && !list_empty(&lru_list)) {
  1187. gl = list_entry(lru_list.next, struct gfs2_glock, gl_lru);
  1188. list_del_init(&gl->gl_lru);
  1189. clear_bit(GLF_LRU, &gl->gl_flags);
  1190. atomic_dec(&lru_count);
  1191. /* Test for being demotable */
  1192. if (!test_and_set_bit(GLF_LOCK, &gl->gl_flags)) {
  1193. gfs2_glock_hold(gl);
  1194. spin_unlock(&lru_lock);
  1195. spin_lock(&gl->gl_spin);
  1196. may_demote = demote_ok(gl);
  1197. if (may_demote) {
  1198. handle_callback(gl, LM_ST_UNLOCKED, 0);
  1199. nr--;
  1200. }
  1201. clear_bit(GLF_LOCK, &gl->gl_flags);
  1202. smp_mb__after_clear_bit();
  1203. if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
  1204. gfs2_glock_put_nolock(gl);
  1205. spin_unlock(&gl->gl_spin);
  1206. spin_lock(&lru_lock);
  1207. continue;
  1208. }
  1209. nr_skipped++;
  1210. list_add(&gl->gl_lru, &skipped);
  1211. set_bit(GLF_LRU, &gl->gl_flags);
  1212. }
  1213. list_splice(&skipped, &lru_list);
  1214. atomic_add(nr_skipped, &lru_count);
  1215. spin_unlock(&lru_lock);
  1216. out:
  1217. return (atomic_read(&lru_count) / 100) * sysctl_vfs_cache_pressure;
  1218. }
  1219. static struct shrinker glock_shrinker = {
  1220. .shrink = gfs2_shrink_glock_memory,
  1221. .seeks = DEFAULT_SEEKS,
  1222. };
  1223. /**
  1224. * examine_bucket - Call a function for glock in a hash bucket
  1225. * @examiner: the function
  1226. * @sdp: the filesystem
  1227. * @bucket: the bucket
  1228. *
  1229. */
  1230. static void examine_bucket(glock_examiner examiner, const struct gfs2_sbd *sdp,
  1231. unsigned int hash)
  1232. {
  1233. struct gfs2_glock *gl;
  1234. struct hlist_bl_head *head = &gl_hash_table[hash];
  1235. struct hlist_bl_node *pos;
  1236. rcu_read_lock();
  1237. hlist_bl_for_each_entry_rcu(gl, pos, head, gl_list) {
  1238. if ((gl->gl_sbd == sdp) && atomic_read(&gl->gl_ref))
  1239. examiner(gl);
  1240. }
  1241. rcu_read_unlock();
  1242. cond_resched();
  1243. }
  1244. static void glock_hash_walk(glock_examiner examiner, const struct gfs2_sbd *sdp)
  1245. {
  1246. unsigned x;
  1247. for (x = 0; x < GFS2_GL_HASH_SIZE; x++)
  1248. examine_bucket(examiner, sdp, x);
  1249. }
  1250. /**
  1251. * thaw_glock - thaw out a glock which has an unprocessed reply waiting
  1252. * @gl: The glock to thaw
  1253. *
  1254. * N.B. When we freeze a glock, we leave a ref to the glock outstanding,
  1255. * so this has to result in the ref count being dropped by one.
  1256. */
  1257. static void thaw_glock(struct gfs2_glock *gl)
  1258. {
  1259. if (!test_and_clear_bit(GLF_FROZEN, &gl->gl_flags))
  1260. return;
  1261. set_bit(GLF_REPLY_PENDING, &gl->gl_flags);
  1262. gfs2_glock_hold(gl);
  1263. if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
  1264. gfs2_glock_put(gl);
  1265. }
  1266. /**
  1267. * clear_glock - look at a glock and see if we can free it from glock cache
  1268. * @gl: the glock to look at
  1269. *
  1270. */
  1271. static void clear_glock(struct gfs2_glock *gl)
  1272. {
  1273. gfs2_glock_remove_from_lru(gl);
  1274. spin_lock(&gl->gl_spin);
  1275. if (gl->gl_state != LM_ST_UNLOCKED)
  1276. handle_callback(gl, LM_ST_UNLOCKED, 0);
  1277. spin_unlock(&gl->gl_spin);
  1278. gfs2_glock_hold(gl);
  1279. if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
  1280. gfs2_glock_put(gl);
  1281. }
  1282. /**
  1283. * gfs2_glock_thaw - Thaw any frozen glocks
  1284. * @sdp: The super block
  1285. *
  1286. */
  1287. void gfs2_glock_thaw(struct gfs2_sbd *sdp)
  1288. {
  1289. glock_hash_walk(thaw_glock, sdp);
  1290. }
  1291. static int dump_glock(struct seq_file *seq, struct gfs2_glock *gl)
  1292. {
  1293. int ret;
  1294. spin_lock(&gl->gl_spin);
  1295. ret = __dump_glock(seq, gl);
  1296. spin_unlock(&gl->gl_spin);
  1297. return ret;
  1298. }
  1299. static void dump_glock_func(struct gfs2_glock *gl)
  1300. {
  1301. dump_glock(NULL, gl);
  1302. }
  1303. /**
  1304. * gfs2_gl_hash_clear - Empty out the glock hash table
  1305. * @sdp: the filesystem
  1306. * @wait: wait until it's all gone
  1307. *
  1308. * Called when unmounting the filesystem.
  1309. */
  1310. void gfs2_gl_hash_clear(struct gfs2_sbd *sdp)
  1311. {
  1312. glock_hash_walk(clear_glock, sdp);
  1313. flush_workqueue(glock_workqueue);
  1314. wait_event(sdp->sd_glock_wait, atomic_read(&sdp->sd_glock_disposal) == 0);
  1315. glock_hash_walk(dump_glock_func, sdp);
  1316. }
  1317. void gfs2_glock_finish_truncate(struct gfs2_inode *ip)
  1318. {
  1319. struct gfs2_glock *gl = ip->i_gl;
  1320. int ret;
  1321. ret = gfs2_truncatei_resume(ip);
  1322. gfs2_assert_withdraw(gl->gl_sbd, ret == 0);
  1323. spin_lock(&gl->gl_spin);
  1324. clear_bit(GLF_LOCK, &gl->gl_flags);
  1325. run_queue(gl, 1);
  1326. spin_unlock(&gl->gl_spin);
  1327. }
  1328. static const char *state2str(unsigned state)
  1329. {
  1330. switch(state) {
  1331. case LM_ST_UNLOCKED:
  1332. return "UN";
  1333. case LM_ST_SHARED:
  1334. return "SH";
  1335. case LM_ST_DEFERRED:
  1336. return "DF";
  1337. case LM_ST_EXCLUSIVE:
  1338. return "EX";
  1339. }
  1340. return "??";
  1341. }
  1342. static const char *hflags2str(char *buf, unsigned flags, unsigned long iflags)
  1343. {
  1344. char *p = buf;
  1345. if (flags & LM_FLAG_TRY)
  1346. *p++ = 't';
  1347. if (flags & LM_FLAG_TRY_1CB)
  1348. *p++ = 'T';
  1349. if (flags & LM_FLAG_NOEXP)
  1350. *p++ = 'e';
  1351. if (flags & LM_FLAG_ANY)
  1352. *p++ = 'A';
  1353. if (flags & LM_FLAG_PRIORITY)
  1354. *p++ = 'p';
  1355. if (flags & GL_ASYNC)
  1356. *p++ = 'a';
  1357. if (flags & GL_EXACT)
  1358. *p++ = 'E';
  1359. if (flags & GL_NOCACHE)
  1360. *p++ = 'c';
  1361. if (test_bit(HIF_HOLDER, &iflags))
  1362. *p++ = 'H';
  1363. if (test_bit(HIF_WAIT, &iflags))
  1364. *p++ = 'W';
  1365. if (test_bit(HIF_FIRST, &iflags))
  1366. *p++ = 'F';
  1367. *p = 0;
  1368. return buf;
  1369. }
  1370. /**
  1371. * dump_holder - print information about a glock holder
  1372. * @seq: the seq_file struct
  1373. * @gh: the glock holder
  1374. *
  1375. * Returns: 0 on success, -ENOBUFS when we run out of space
  1376. */
  1377. static int dump_holder(struct seq_file *seq, const struct gfs2_holder *gh)
  1378. {
  1379. struct task_struct *gh_owner = NULL;
  1380. char flags_buf[32];
  1381. if (gh->gh_owner_pid)
  1382. gh_owner = pid_task(gh->gh_owner_pid, PIDTYPE_PID);
  1383. gfs2_print_dbg(seq, " H: s:%s f:%s e:%d p:%ld [%s] %pS\n",
  1384. state2str(gh->gh_state),
  1385. hflags2str(flags_buf, gh->gh_flags, gh->gh_iflags),
  1386. gh->gh_error,
  1387. gh->gh_owner_pid ? (long)pid_nr(gh->gh_owner_pid) : -1,
  1388. gh_owner ? gh_owner->comm : "(ended)",
  1389. (void *)gh->gh_ip);
  1390. return 0;
  1391. }
  1392. static const char *gflags2str(char *buf, const struct gfs2_glock *gl)
  1393. {
  1394. const unsigned long *gflags = &gl->gl_flags;
  1395. char *p = buf;
  1396. if (test_bit(GLF_LOCK, gflags))
  1397. *p++ = 'l';
  1398. if (test_bit(GLF_DEMOTE, gflags))
  1399. *p++ = 'D';
  1400. if (test_bit(GLF_PENDING_DEMOTE, gflags))
  1401. *p++ = 'd';
  1402. if (test_bit(GLF_DEMOTE_IN_PROGRESS, gflags))
  1403. *p++ = 'p';
  1404. if (test_bit(GLF_DIRTY, gflags))
  1405. *p++ = 'y';
  1406. if (test_bit(GLF_LFLUSH, gflags))
  1407. *p++ = 'f';
  1408. if (test_bit(GLF_INVALIDATE_IN_PROGRESS, gflags))
  1409. *p++ = 'i';
  1410. if (test_bit(GLF_REPLY_PENDING, gflags))
  1411. *p++ = 'r';
  1412. if (test_bit(GLF_INITIAL, gflags))
  1413. *p++ = 'I';
  1414. if (test_bit(GLF_FROZEN, gflags))
  1415. *p++ = 'F';
  1416. if (test_bit(GLF_QUEUED, gflags))
  1417. *p++ = 'q';
  1418. if (test_bit(GLF_LRU, gflags))
  1419. *p++ = 'L';
  1420. if (gl->gl_object)
  1421. *p++ = 'o';
  1422. *p = 0;
  1423. return buf;
  1424. }
  1425. /**
  1426. * __dump_glock - print information about a glock
  1427. * @seq: The seq_file struct
  1428. * @gl: the glock
  1429. *
  1430. * The file format is as follows:
  1431. * One line per object, capital letters are used to indicate objects
  1432. * G = glock, I = Inode, R = rgrp, H = holder. Glocks are not indented,
  1433. * other objects are indented by a single space and follow the glock to
  1434. * which they are related. Fields are indicated by lower case letters
  1435. * followed by a colon and the field value, except for strings which are in
  1436. * [] so that its possible to see if they are composed of spaces for
  1437. * example. The field's are n = number (id of the object), f = flags,
  1438. * t = type, s = state, r = refcount, e = error, p = pid.
  1439. *
  1440. * Returns: 0 on success, -ENOBUFS when we run out of space
  1441. */
  1442. static int __dump_glock(struct seq_file *seq, const struct gfs2_glock *gl)
  1443. {
  1444. const struct gfs2_glock_operations *glops = gl->gl_ops;
  1445. unsigned long long dtime;
  1446. const struct gfs2_holder *gh;
  1447. char gflags_buf[32];
  1448. int error = 0;
  1449. dtime = jiffies - gl->gl_demote_time;
  1450. dtime *= 1000000/HZ; /* demote time in uSec */
  1451. if (!test_bit(GLF_DEMOTE, &gl->gl_flags))
  1452. dtime = 0;
  1453. gfs2_print_dbg(seq, "G: s:%s n:%u/%llx f:%s t:%s d:%s/%llu a:%d v:%d r:%d\n",
  1454. state2str(gl->gl_state),
  1455. gl->gl_name.ln_type,
  1456. (unsigned long long)gl->gl_name.ln_number,
  1457. gflags2str(gflags_buf, gl),
  1458. state2str(gl->gl_target),
  1459. state2str(gl->gl_demote_state), dtime,
  1460. atomic_read(&gl->gl_ail_count),
  1461. atomic_read(&gl->gl_revokes),
  1462. atomic_read(&gl->gl_ref));
  1463. list_for_each_entry(gh, &gl->gl_holders, gh_list) {
  1464. error = dump_holder(seq, gh);
  1465. if (error)
  1466. goto out;
  1467. }
  1468. if (gl->gl_state != LM_ST_UNLOCKED && glops->go_dump)
  1469. error = glops->go_dump(seq, gl);
  1470. out:
  1471. return error;
  1472. }
  1473. int __init gfs2_glock_init(void)
  1474. {
  1475. unsigned i;
  1476. for(i = 0; i < GFS2_GL_HASH_SIZE; i++) {
  1477. INIT_HLIST_BL_HEAD(&gl_hash_table[i]);
  1478. }
  1479. glock_workqueue = alloc_workqueue("glock_workqueue", WQ_MEM_RECLAIM |
  1480. WQ_HIGHPRI | WQ_FREEZABLE, 0);
  1481. if (IS_ERR(glock_workqueue))
  1482. return PTR_ERR(glock_workqueue);
  1483. gfs2_delete_workqueue = alloc_workqueue("delete_workqueue",
  1484. WQ_MEM_RECLAIM | WQ_FREEZABLE,
  1485. 0);
  1486. if (IS_ERR(gfs2_delete_workqueue)) {
  1487. destroy_workqueue(glock_workqueue);
  1488. return PTR_ERR(gfs2_delete_workqueue);
  1489. }
  1490. register_shrinker(&glock_shrinker);
  1491. return 0;
  1492. }
  1493. void gfs2_glock_exit(void)
  1494. {
  1495. unregister_shrinker(&glock_shrinker);
  1496. destroy_workqueue(glock_workqueue);
  1497. destroy_workqueue(gfs2_delete_workqueue);
  1498. }
  1499. static inline struct gfs2_glock *glock_hash_chain(unsigned hash)
  1500. {
  1501. return hlist_bl_entry(hlist_bl_first_rcu(&gl_hash_table[hash]),
  1502. struct gfs2_glock, gl_list);
  1503. }
  1504. static inline struct gfs2_glock *glock_hash_next(struct gfs2_glock *gl)
  1505. {
  1506. return hlist_bl_entry(rcu_dereference(gl->gl_list.next),
  1507. struct gfs2_glock, gl_list);
  1508. }
  1509. static int gfs2_glock_iter_next(struct gfs2_glock_iter *gi)
  1510. {
  1511. struct gfs2_glock *gl;
  1512. do {
  1513. gl = gi->gl;
  1514. if (gl) {
  1515. gi->gl = glock_hash_next(gl);
  1516. } else {
  1517. gi->gl = glock_hash_chain(gi->hash);
  1518. }
  1519. while (gi->gl == NULL) {
  1520. gi->hash++;
  1521. if (gi->hash >= GFS2_GL_HASH_SIZE) {
  1522. rcu_read_unlock();
  1523. return 1;
  1524. }
  1525. gi->gl = glock_hash_chain(gi->hash);
  1526. }
  1527. /* Skip entries for other sb and dead entries */
  1528. } while (gi->sdp != gi->gl->gl_sbd || atomic_read(&gi->gl->gl_ref) == 0);
  1529. return 0;
  1530. }
  1531. static void *gfs2_glock_seq_start(struct seq_file *seq, loff_t *pos)
  1532. {
  1533. struct gfs2_glock_iter *gi = seq->private;
  1534. loff_t n = *pos;
  1535. gi->hash = 0;
  1536. rcu_read_lock();
  1537. do {
  1538. if (gfs2_glock_iter_next(gi))
  1539. return NULL;
  1540. } while (n--);
  1541. return gi->gl;
  1542. }
  1543. static void *gfs2_glock_seq_next(struct seq_file *seq, void *iter_ptr,
  1544. loff_t *pos)
  1545. {
  1546. struct gfs2_glock_iter *gi = seq->private;
  1547. (*pos)++;
  1548. if (gfs2_glock_iter_next(gi))
  1549. return NULL;
  1550. return gi->gl;
  1551. }
  1552. static void gfs2_glock_seq_stop(struct seq_file *seq, void *iter_ptr)
  1553. {
  1554. struct gfs2_glock_iter *gi = seq->private;
  1555. if (gi->gl)
  1556. rcu_read_unlock();
  1557. gi->gl = NULL;
  1558. }
  1559. static int gfs2_glock_seq_show(struct seq_file *seq, void *iter_ptr)
  1560. {
  1561. return dump_glock(seq, iter_ptr);
  1562. }
  1563. static const struct seq_operations gfs2_glock_seq_ops = {
  1564. .start = gfs2_glock_seq_start,
  1565. .next = gfs2_glock_seq_next,
  1566. .stop = gfs2_glock_seq_stop,
  1567. .show = gfs2_glock_seq_show,
  1568. };
  1569. static int gfs2_debugfs_open(struct inode *inode, struct file *file)
  1570. {
  1571. int ret = seq_open_private(file, &gfs2_glock_seq_ops,
  1572. sizeof(struct gfs2_glock_iter));
  1573. if (ret == 0) {
  1574. struct seq_file *seq = file->private_data;
  1575. struct gfs2_glock_iter *gi = seq->private;
  1576. gi->sdp = inode->i_private;
  1577. }
  1578. return ret;
  1579. }
  1580. static const struct file_operations gfs2_debug_fops = {
  1581. .owner = THIS_MODULE,
  1582. .open = gfs2_debugfs_open,
  1583. .read = seq_read,
  1584. .llseek = seq_lseek,
  1585. .release = seq_release_private,
  1586. };
  1587. int gfs2_create_debugfs_file(struct gfs2_sbd *sdp)
  1588. {
  1589. sdp->debugfs_dir = debugfs_create_dir(sdp->sd_table_name, gfs2_root);
  1590. if (!sdp->debugfs_dir)
  1591. return -ENOMEM;
  1592. sdp->debugfs_dentry_glocks = debugfs_create_file("glocks",
  1593. S_IFREG | S_IRUGO,
  1594. sdp->debugfs_dir, sdp,
  1595. &gfs2_debug_fops);
  1596. if (!sdp->debugfs_dentry_glocks)
  1597. return -ENOMEM;
  1598. return 0;
  1599. }
  1600. void gfs2_delete_debugfs_file(struct gfs2_sbd *sdp)
  1601. {
  1602. if (sdp && sdp->debugfs_dir) {
  1603. if (sdp->debugfs_dentry_glocks) {
  1604. debugfs_remove(sdp->debugfs_dentry_glocks);
  1605. sdp->debugfs_dentry_glocks = NULL;
  1606. }
  1607. debugfs_remove(sdp->debugfs_dir);
  1608. sdp->debugfs_dir = NULL;
  1609. }
  1610. }
  1611. int gfs2_register_debugfs(void)
  1612. {
  1613. gfs2_root = debugfs_create_dir("gfs2", NULL);
  1614. return gfs2_root ? 0 : -ENOMEM;
  1615. }
  1616. void gfs2_unregister_debugfs(void)
  1617. {
  1618. debugfs_remove(gfs2_root);
  1619. gfs2_root = NULL;
  1620. }