glock.c 47 KB

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