glock.c 46 KB

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