glock.c 47 KB

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