glock.c 48 KB

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