glock.c 46 KB

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