glock.c 46 KB

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