glock.c 44 KB

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