glock.c 46 KB

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