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