glock.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174
  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();
  364. wake_up_bit(&gh->gh_iflags, HIF_WAIT);
  365. }
  366. static int holder_wait(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, holder_wait, TASK_UNINTERRUPTIBLE);
  375. }
  376. /**
  377. * rq_mutex - process a mutex request in the queue
  378. * @gh: the glock holder
  379. *
  380. * Returns: 1 if the queue is blocked
  381. */
  382. static int rq_mutex(struct gfs2_holder *gh)
  383. {
  384. struct gfs2_glock *gl = gh->gh_gl;
  385. list_del_init(&gh->gh_list);
  386. /* gh->gh_error never examined. */
  387. set_bit(GLF_LOCK, &gl->gl_flags);
  388. clear_bit(HIF_WAIT, &gh->gh_iflags);
  389. smp_mb();
  390. wake_up_bit(&gh->gh_iflags, HIF_WAIT);
  391. return 1;
  392. }
  393. /**
  394. * rq_promote - process a promote request in the queue
  395. * @gh: the glock holder
  396. *
  397. * Acquire a new inter-node lock, or change a lock state to more restrictive.
  398. *
  399. * Returns: 1 if the queue is blocked
  400. */
  401. static int rq_promote(struct gfs2_holder *gh)
  402. {
  403. struct gfs2_glock *gl = gh->gh_gl;
  404. struct gfs2_sbd *sdp = gl->gl_sbd;
  405. if (!relaxed_state_ok(gl->gl_state, gh->gh_state, gh->gh_flags)) {
  406. if (list_empty(&gl->gl_holders)) {
  407. gl->gl_req_gh = gh;
  408. set_bit(GLF_LOCK, &gl->gl_flags);
  409. spin_unlock(&gl->gl_spin);
  410. if (atomic_read(&sdp->sd_reclaim_count) >
  411. gfs2_tune_get(sdp, gt_reclaim_limit) &&
  412. !(gh->gh_flags & LM_FLAG_PRIORITY)) {
  413. gfs2_reclaim_glock(sdp);
  414. gfs2_reclaim_glock(sdp);
  415. }
  416. gfs2_glock_xmote_th(gh->gh_gl, gh);
  417. spin_lock(&gl->gl_spin);
  418. }
  419. return 1;
  420. }
  421. if (list_empty(&gl->gl_holders)) {
  422. set_bit(HIF_FIRST, &gh->gh_iflags);
  423. set_bit(GLF_LOCK, &gl->gl_flags);
  424. } else {
  425. struct gfs2_holder *next_gh;
  426. if (gh->gh_state == LM_ST_EXCLUSIVE)
  427. return 1;
  428. next_gh = list_entry(gl->gl_holders.next, struct gfs2_holder,
  429. gh_list);
  430. if (next_gh->gh_state == LM_ST_EXCLUSIVE)
  431. return 1;
  432. }
  433. list_move_tail(&gh->gh_list, &gl->gl_holders);
  434. gh->gh_error = 0;
  435. set_bit(HIF_HOLDER, &gh->gh_iflags);
  436. gfs2_holder_wake(gh);
  437. return 0;
  438. }
  439. /**
  440. * rq_demote - process a demote request in the queue
  441. * @gh: the glock holder
  442. *
  443. * Returns: 1 if the queue is blocked
  444. */
  445. static int rq_demote(struct gfs2_glock *gl)
  446. {
  447. if (!list_empty(&gl->gl_holders))
  448. return 1;
  449. if (gl->gl_state == gl->gl_demote_state ||
  450. gl->gl_state == LM_ST_UNLOCKED) {
  451. clear_bit(GLF_DEMOTE, &gl->gl_flags);
  452. return 0;
  453. }
  454. set_bit(GLF_LOCK, &gl->gl_flags);
  455. spin_unlock(&gl->gl_spin);
  456. if (gl->gl_demote_state == LM_ST_UNLOCKED ||
  457. gl->gl_state != LM_ST_EXCLUSIVE)
  458. gfs2_glock_drop_th(gl);
  459. else
  460. gfs2_glock_xmote_th(gl, NULL);
  461. spin_lock(&gl->gl_spin);
  462. return 0;
  463. }
  464. /**
  465. * run_queue - process holder structures on a glock
  466. * @gl: the glock
  467. *
  468. */
  469. static void run_queue(struct gfs2_glock *gl)
  470. {
  471. struct gfs2_holder *gh;
  472. int blocked = 1;
  473. for (;;) {
  474. if (test_bit(GLF_LOCK, &gl->gl_flags))
  475. break;
  476. if (!list_empty(&gl->gl_waiters1)) {
  477. gh = list_entry(gl->gl_waiters1.next,
  478. struct gfs2_holder, gh_list);
  479. if (test_bit(HIF_MUTEX, &gh->gh_iflags))
  480. blocked = rq_mutex(gh);
  481. else
  482. gfs2_assert_warn(gl->gl_sbd, 0);
  483. } else if (test_bit(GLF_DEMOTE, &gl->gl_flags)) {
  484. blocked = rq_demote(gl);
  485. } else if (!list_empty(&gl->gl_waiters3)) {
  486. gh = list_entry(gl->gl_waiters3.next,
  487. struct gfs2_holder, gh_list);
  488. if (test_bit(HIF_PROMOTE, &gh->gh_iflags))
  489. blocked = rq_promote(gh);
  490. else
  491. gfs2_assert_warn(gl->gl_sbd, 0);
  492. } else
  493. break;
  494. if (blocked)
  495. break;
  496. }
  497. }
  498. /**
  499. * gfs2_glmutex_lock - acquire a local lock on a glock
  500. * @gl: the glock
  501. *
  502. * Gives caller exclusive access to manipulate a glock structure.
  503. */
  504. static void gfs2_glmutex_lock(struct gfs2_glock *gl)
  505. {
  506. struct gfs2_holder gh;
  507. gfs2_holder_init(gl, 0, 0, &gh);
  508. set_bit(HIF_MUTEX, &gh.gh_iflags);
  509. if (test_and_set_bit(HIF_WAIT, &gh.gh_iflags))
  510. BUG();
  511. spin_lock(&gl->gl_spin);
  512. if (test_and_set_bit(GLF_LOCK, &gl->gl_flags)) {
  513. list_add_tail(&gh.gh_list, &gl->gl_waiters1);
  514. } else {
  515. gl->gl_owner_pid = current->pid;
  516. gl->gl_ip = (unsigned long)__builtin_return_address(0);
  517. clear_bit(HIF_WAIT, &gh.gh_iflags);
  518. smp_mb();
  519. wake_up_bit(&gh.gh_iflags, HIF_WAIT);
  520. }
  521. spin_unlock(&gl->gl_spin);
  522. wait_on_holder(&gh);
  523. gfs2_holder_uninit(&gh);
  524. }
  525. /**
  526. * gfs2_glmutex_trylock - try to acquire a local lock on a glock
  527. * @gl: the glock
  528. *
  529. * Returns: 1 if the glock is acquired
  530. */
  531. static int gfs2_glmutex_trylock(struct gfs2_glock *gl)
  532. {
  533. int acquired = 1;
  534. spin_lock(&gl->gl_spin);
  535. if (test_and_set_bit(GLF_LOCK, &gl->gl_flags)) {
  536. acquired = 0;
  537. } else {
  538. gl->gl_owner_pid = current->pid;
  539. gl->gl_ip = (unsigned long)__builtin_return_address(0);
  540. }
  541. spin_unlock(&gl->gl_spin);
  542. return acquired;
  543. }
  544. /**
  545. * gfs2_glmutex_unlock - release a local lock on a glock
  546. * @gl: the glock
  547. *
  548. */
  549. static void gfs2_glmutex_unlock(struct gfs2_glock *gl)
  550. {
  551. spin_lock(&gl->gl_spin);
  552. clear_bit(GLF_LOCK, &gl->gl_flags);
  553. gl->gl_owner_pid = 0;
  554. gl->gl_ip = 0;
  555. run_queue(gl);
  556. BUG_ON(!spin_is_locked(&gl->gl_spin));
  557. spin_unlock(&gl->gl_spin);
  558. }
  559. /**
  560. * handle_callback - process a demote request
  561. * @gl: the glock
  562. * @state: the state the caller wants us to change to
  563. *
  564. * There are only two requests that we are going to see in actual
  565. * practise: LM_ST_SHARED and LM_ST_UNLOCKED
  566. */
  567. static void handle_callback(struct gfs2_glock *gl, unsigned int state)
  568. {
  569. spin_lock(&gl->gl_spin);
  570. if (test_and_set_bit(GLF_DEMOTE, &gl->gl_flags) == 0) {
  571. gl->gl_demote_state = state;
  572. gl->gl_demote_time = jiffies;
  573. } else if (gl->gl_demote_state != LM_ST_UNLOCKED) {
  574. gl->gl_demote_state = state;
  575. }
  576. spin_unlock(&gl->gl_spin);
  577. }
  578. /**
  579. * state_change - record that the glock is now in a different state
  580. * @gl: the glock
  581. * @new_state the new state
  582. *
  583. */
  584. static void state_change(struct gfs2_glock *gl, unsigned int new_state)
  585. {
  586. int held1, held2;
  587. held1 = (gl->gl_state != LM_ST_UNLOCKED);
  588. held2 = (new_state != LM_ST_UNLOCKED);
  589. if (held1 != held2) {
  590. if (held2)
  591. gfs2_glock_hold(gl);
  592. else
  593. gfs2_glock_put(gl);
  594. }
  595. gl->gl_state = new_state;
  596. }
  597. /**
  598. * xmote_bh - Called after the lock module is done acquiring a lock
  599. * @gl: The glock in question
  600. * @ret: the int returned from the lock module
  601. *
  602. */
  603. static void xmote_bh(struct gfs2_glock *gl, unsigned int ret)
  604. {
  605. struct gfs2_sbd *sdp = gl->gl_sbd;
  606. const struct gfs2_glock_operations *glops = gl->gl_ops;
  607. struct gfs2_holder *gh = gl->gl_req_gh;
  608. int prev_state = gl->gl_state;
  609. int op_done = 1;
  610. gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
  611. gfs2_assert_warn(sdp, list_empty(&gl->gl_holders));
  612. gfs2_assert_warn(sdp, !(ret & LM_OUT_ASYNC));
  613. state_change(gl, ret & LM_OUT_ST_MASK);
  614. if (prev_state != LM_ST_UNLOCKED && !(ret & LM_OUT_CACHEABLE)) {
  615. if (glops->go_inval)
  616. glops->go_inval(gl, DIO_METADATA);
  617. } else if (gl->gl_state == LM_ST_DEFERRED) {
  618. /* We might not want to do this here.
  619. Look at moving to the inode glops. */
  620. if (glops->go_inval)
  621. glops->go_inval(gl, 0);
  622. }
  623. /* Deal with each possible exit condition */
  624. if (!gh) {
  625. gl->gl_stamp = jiffies;
  626. if (ret & LM_OUT_CANCELED)
  627. op_done = 0;
  628. else
  629. clear_bit(GLF_DEMOTE, &gl->gl_flags);
  630. } else {
  631. spin_lock(&gl->gl_spin);
  632. list_del_init(&gh->gh_list);
  633. gh->gh_error = -EIO;
  634. if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
  635. goto out;
  636. gh->gh_error = GLR_CANCELED;
  637. if (ret & LM_OUT_CANCELED)
  638. goto out;
  639. if (relaxed_state_ok(gl->gl_state, gh->gh_state, gh->gh_flags)) {
  640. list_add_tail(&gh->gh_list, &gl->gl_holders);
  641. gh->gh_error = 0;
  642. set_bit(HIF_HOLDER, &gh->gh_iflags);
  643. set_bit(HIF_FIRST, &gh->gh_iflags);
  644. op_done = 0;
  645. goto out;
  646. }
  647. gh->gh_error = GLR_TRYFAILED;
  648. if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB))
  649. goto out;
  650. gh->gh_error = -EINVAL;
  651. if (gfs2_assert_withdraw(sdp, 0) == -1)
  652. fs_err(sdp, "ret = 0x%.8X\n", ret);
  653. out:
  654. spin_unlock(&gl->gl_spin);
  655. }
  656. if (glops->go_xmote_bh)
  657. glops->go_xmote_bh(gl);
  658. if (op_done) {
  659. spin_lock(&gl->gl_spin);
  660. gl->gl_req_gh = NULL;
  661. gl->gl_req_bh = NULL;
  662. clear_bit(GLF_LOCK, &gl->gl_flags);
  663. run_queue(gl);
  664. spin_unlock(&gl->gl_spin);
  665. }
  666. gfs2_glock_put(gl);
  667. if (gh)
  668. gfs2_holder_wake(gh);
  669. }
  670. /**
  671. * gfs2_glock_xmote_th - Call into the lock module to acquire or change a glock
  672. * @gl: The glock in question
  673. * @state: the requested state
  674. * @flags: modifier flags to the lock call
  675. *
  676. */
  677. void gfs2_glock_xmote_th(struct gfs2_glock *gl, struct gfs2_holder *gh)
  678. {
  679. struct gfs2_sbd *sdp = gl->gl_sbd;
  680. int flags = gh ? gh->gh_flags : 0;
  681. unsigned state = gh ? gh->gh_state : gl->gl_demote_state;
  682. const struct gfs2_glock_operations *glops = gl->gl_ops;
  683. int lck_flags = flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB |
  684. LM_FLAG_NOEXP | LM_FLAG_ANY |
  685. LM_FLAG_PRIORITY);
  686. unsigned int lck_ret;
  687. if (glops->go_xmote_th)
  688. glops->go_xmote_th(gl);
  689. gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
  690. gfs2_assert_warn(sdp, list_empty(&gl->gl_holders));
  691. gfs2_assert_warn(sdp, state != LM_ST_UNLOCKED);
  692. gfs2_assert_warn(sdp, state != gl->gl_state);
  693. gfs2_glock_hold(gl);
  694. gl->gl_req_bh = xmote_bh;
  695. lck_ret = gfs2_lm_lock(sdp, gl->gl_lock, gl->gl_state, state, lck_flags);
  696. if (gfs2_assert_withdraw(sdp, !(lck_ret & LM_OUT_ERROR)))
  697. return;
  698. if (lck_ret & LM_OUT_ASYNC)
  699. gfs2_assert_warn(sdp, lck_ret == LM_OUT_ASYNC);
  700. else
  701. xmote_bh(gl, lck_ret);
  702. }
  703. /**
  704. * drop_bh - Called after a lock module unlock completes
  705. * @gl: the glock
  706. * @ret: the return status
  707. *
  708. * Doesn't wake up the process waiting on the struct gfs2_holder (if any)
  709. * Doesn't drop the reference on the glock the top half took out
  710. *
  711. */
  712. static void drop_bh(struct gfs2_glock *gl, unsigned int ret)
  713. {
  714. struct gfs2_sbd *sdp = gl->gl_sbd;
  715. const struct gfs2_glock_operations *glops = gl->gl_ops;
  716. struct gfs2_holder *gh = gl->gl_req_gh;
  717. gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
  718. gfs2_assert_warn(sdp, list_empty(&gl->gl_holders));
  719. gfs2_assert_warn(sdp, !ret);
  720. state_change(gl, LM_ST_UNLOCKED);
  721. clear_bit(GLF_DEMOTE, &gl->gl_flags);
  722. if (glops->go_inval)
  723. glops->go_inval(gl, DIO_METADATA);
  724. if (gh) {
  725. spin_lock(&gl->gl_spin);
  726. list_del_init(&gh->gh_list);
  727. gh->gh_error = 0;
  728. spin_unlock(&gl->gl_spin);
  729. }
  730. spin_lock(&gl->gl_spin);
  731. gl->gl_req_gh = NULL;
  732. gl->gl_req_bh = NULL;
  733. clear_bit(GLF_LOCK, &gl->gl_flags);
  734. run_queue(gl);
  735. spin_unlock(&gl->gl_spin);
  736. gfs2_glock_put(gl);
  737. if (gh)
  738. gfs2_holder_wake(gh);
  739. }
  740. /**
  741. * gfs2_glock_drop_th - call into the lock module to unlock a lock
  742. * @gl: the glock
  743. *
  744. */
  745. static void gfs2_glock_drop_th(struct gfs2_glock *gl)
  746. {
  747. struct gfs2_sbd *sdp = gl->gl_sbd;
  748. const struct gfs2_glock_operations *glops = gl->gl_ops;
  749. unsigned int ret;
  750. if (glops->go_drop_th)
  751. glops->go_drop_th(gl);
  752. gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
  753. gfs2_assert_warn(sdp, list_empty(&gl->gl_holders));
  754. gfs2_assert_warn(sdp, gl->gl_state != LM_ST_UNLOCKED);
  755. gfs2_glock_hold(gl);
  756. gl->gl_req_bh = drop_bh;
  757. ret = gfs2_lm_unlock(sdp, gl->gl_lock, gl->gl_state);
  758. if (gfs2_assert_withdraw(sdp, !(ret & LM_OUT_ERROR)))
  759. return;
  760. if (!ret)
  761. drop_bh(gl, ret);
  762. else
  763. gfs2_assert_warn(sdp, ret == LM_OUT_ASYNC);
  764. }
  765. /**
  766. * do_cancels - cancel requests for locks stuck waiting on an expire flag
  767. * @gh: the LM_FLAG_PRIORITY holder waiting to acquire the lock
  768. *
  769. * Don't cancel GL_NOCANCEL requests.
  770. */
  771. static void do_cancels(struct gfs2_holder *gh)
  772. {
  773. struct gfs2_glock *gl = gh->gh_gl;
  774. spin_lock(&gl->gl_spin);
  775. while (gl->gl_req_gh != gh &&
  776. !test_bit(HIF_HOLDER, &gh->gh_iflags) &&
  777. !list_empty(&gh->gh_list)) {
  778. if (gl->gl_req_bh && !(gl->gl_req_gh &&
  779. (gl->gl_req_gh->gh_flags & GL_NOCANCEL))) {
  780. spin_unlock(&gl->gl_spin);
  781. gfs2_lm_cancel(gl->gl_sbd, gl->gl_lock);
  782. msleep(100);
  783. spin_lock(&gl->gl_spin);
  784. } else {
  785. spin_unlock(&gl->gl_spin);
  786. msleep(100);
  787. spin_lock(&gl->gl_spin);
  788. }
  789. }
  790. spin_unlock(&gl->gl_spin);
  791. }
  792. /**
  793. * glock_wait_internal - wait on a glock acquisition
  794. * @gh: the glock holder
  795. *
  796. * Returns: 0 on success
  797. */
  798. static int glock_wait_internal(struct gfs2_holder *gh)
  799. {
  800. struct gfs2_glock *gl = gh->gh_gl;
  801. struct gfs2_sbd *sdp = gl->gl_sbd;
  802. const struct gfs2_glock_operations *glops = gl->gl_ops;
  803. if (test_bit(HIF_ABORTED, &gh->gh_iflags))
  804. return -EIO;
  805. if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) {
  806. spin_lock(&gl->gl_spin);
  807. if (gl->gl_req_gh != gh &&
  808. !test_bit(HIF_HOLDER, &gh->gh_iflags) &&
  809. !list_empty(&gh->gh_list)) {
  810. list_del_init(&gh->gh_list);
  811. gh->gh_error = GLR_TRYFAILED;
  812. run_queue(gl);
  813. spin_unlock(&gl->gl_spin);
  814. return gh->gh_error;
  815. }
  816. spin_unlock(&gl->gl_spin);
  817. }
  818. if (gh->gh_flags & LM_FLAG_PRIORITY)
  819. do_cancels(gh);
  820. wait_on_holder(gh);
  821. if (gh->gh_error)
  822. return gh->gh_error;
  823. gfs2_assert_withdraw(sdp, test_bit(HIF_HOLDER, &gh->gh_iflags));
  824. gfs2_assert_withdraw(sdp, relaxed_state_ok(gl->gl_state, gh->gh_state,
  825. gh->gh_flags));
  826. if (test_bit(HIF_FIRST, &gh->gh_iflags)) {
  827. gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
  828. if (glops->go_lock) {
  829. gh->gh_error = glops->go_lock(gh);
  830. if (gh->gh_error) {
  831. spin_lock(&gl->gl_spin);
  832. list_del_init(&gh->gh_list);
  833. spin_unlock(&gl->gl_spin);
  834. }
  835. }
  836. spin_lock(&gl->gl_spin);
  837. gl->gl_req_gh = NULL;
  838. gl->gl_req_bh = NULL;
  839. clear_bit(GLF_LOCK, &gl->gl_flags);
  840. run_queue(gl);
  841. spin_unlock(&gl->gl_spin);
  842. }
  843. return gh->gh_error;
  844. }
  845. static inline struct gfs2_holder *
  846. find_holder_by_owner(struct list_head *head, pid_t pid)
  847. {
  848. struct gfs2_holder *gh;
  849. list_for_each_entry(gh, head, gh_list) {
  850. if (gh->gh_owner_pid == pid)
  851. return gh;
  852. }
  853. return NULL;
  854. }
  855. static void print_dbg(struct glock_iter *gi, const char *fmt, ...)
  856. {
  857. va_list args;
  858. va_start(args, fmt);
  859. if (gi) {
  860. vsprintf(gi->string, fmt, args);
  861. seq_printf(gi->seq, gi->string);
  862. }
  863. else
  864. vprintk(fmt, args);
  865. va_end(args);
  866. }
  867. /**
  868. * add_to_queue - Add a holder to the wait queue (but look for recursion)
  869. * @gh: the holder structure to add
  870. *
  871. */
  872. static void add_to_queue(struct gfs2_holder *gh)
  873. {
  874. struct gfs2_glock *gl = gh->gh_gl;
  875. struct gfs2_holder *existing;
  876. BUG_ON(!gh->gh_owner_pid);
  877. if (test_and_set_bit(HIF_WAIT, &gh->gh_iflags))
  878. BUG();
  879. existing = find_holder_by_owner(&gl->gl_holders, gh->gh_owner_pid);
  880. if (existing) {
  881. print_symbol(KERN_WARNING "original: %s\n", existing->gh_ip);
  882. printk(KERN_INFO "pid : %d\n", existing->gh_owner_pid);
  883. printk(KERN_INFO "lock type : %d lock state : %d\n",
  884. existing->gh_gl->gl_name.ln_type, existing->gh_gl->gl_state);
  885. print_symbol(KERN_WARNING "new: %s\n", gh->gh_ip);
  886. printk(KERN_INFO "pid : %d\n", gh->gh_owner_pid);
  887. printk(KERN_INFO "lock type : %d lock state : %d\n",
  888. gl->gl_name.ln_type, gl->gl_state);
  889. BUG();
  890. }
  891. existing = find_holder_by_owner(&gl->gl_waiters3, gh->gh_owner_pid);
  892. if (existing) {
  893. print_symbol(KERN_WARNING "original: %s\n", existing->gh_ip);
  894. print_symbol(KERN_WARNING "new: %s\n", gh->gh_ip);
  895. BUG();
  896. }
  897. if (gh->gh_flags & LM_FLAG_PRIORITY)
  898. list_add(&gh->gh_list, &gl->gl_waiters3);
  899. else
  900. list_add_tail(&gh->gh_list, &gl->gl_waiters3);
  901. }
  902. /**
  903. * gfs2_glock_nq - enqueue a struct gfs2_holder onto a glock (acquire a glock)
  904. * @gh: the holder structure
  905. *
  906. * if (gh->gh_flags & GL_ASYNC), this never returns an error
  907. *
  908. * Returns: 0, GLR_TRYFAILED, or errno on failure
  909. */
  910. int gfs2_glock_nq(struct gfs2_holder *gh)
  911. {
  912. struct gfs2_glock *gl = gh->gh_gl;
  913. struct gfs2_sbd *sdp = gl->gl_sbd;
  914. int error = 0;
  915. restart:
  916. if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) {
  917. set_bit(HIF_ABORTED, &gh->gh_iflags);
  918. return -EIO;
  919. }
  920. set_bit(HIF_PROMOTE, &gh->gh_iflags);
  921. spin_lock(&gl->gl_spin);
  922. add_to_queue(gh);
  923. run_queue(gl);
  924. spin_unlock(&gl->gl_spin);
  925. if (!(gh->gh_flags & GL_ASYNC)) {
  926. error = glock_wait_internal(gh);
  927. if (error == GLR_CANCELED) {
  928. msleep(100);
  929. goto restart;
  930. }
  931. }
  932. return error;
  933. }
  934. /**
  935. * gfs2_glock_poll - poll to see if an async request has been completed
  936. * @gh: the holder
  937. *
  938. * Returns: 1 if the request is ready to be gfs2_glock_wait()ed on
  939. */
  940. int gfs2_glock_poll(struct gfs2_holder *gh)
  941. {
  942. struct gfs2_glock *gl = gh->gh_gl;
  943. int ready = 0;
  944. spin_lock(&gl->gl_spin);
  945. if (test_bit(HIF_HOLDER, &gh->gh_iflags))
  946. ready = 1;
  947. else if (list_empty(&gh->gh_list)) {
  948. if (gh->gh_error == GLR_CANCELED) {
  949. spin_unlock(&gl->gl_spin);
  950. msleep(100);
  951. if (gfs2_glock_nq(gh))
  952. return 1;
  953. return 0;
  954. } else
  955. ready = 1;
  956. }
  957. spin_unlock(&gl->gl_spin);
  958. return ready;
  959. }
  960. /**
  961. * gfs2_glock_wait - wait for a lock acquisition that ended in a GLR_ASYNC
  962. * @gh: the holder structure
  963. *
  964. * Returns: 0, GLR_TRYFAILED, or errno on failure
  965. */
  966. int gfs2_glock_wait(struct gfs2_holder *gh)
  967. {
  968. int error;
  969. error = glock_wait_internal(gh);
  970. if (error == GLR_CANCELED) {
  971. msleep(100);
  972. gh->gh_flags &= ~GL_ASYNC;
  973. error = gfs2_glock_nq(gh);
  974. }
  975. return error;
  976. }
  977. /**
  978. * gfs2_glock_dq - dequeue a struct gfs2_holder from a glock (release a glock)
  979. * @gh: the glock holder
  980. *
  981. */
  982. void gfs2_glock_dq(struct gfs2_holder *gh)
  983. {
  984. struct gfs2_glock *gl = gh->gh_gl;
  985. const struct gfs2_glock_operations *glops = gl->gl_ops;
  986. if (gh->gh_flags & GL_NOCACHE)
  987. handle_callback(gl, LM_ST_UNLOCKED);
  988. gfs2_glmutex_lock(gl);
  989. spin_lock(&gl->gl_spin);
  990. list_del_init(&gh->gh_list);
  991. if (list_empty(&gl->gl_holders)) {
  992. spin_unlock(&gl->gl_spin);
  993. if (glops->go_unlock)
  994. glops->go_unlock(gh);
  995. spin_lock(&gl->gl_spin);
  996. gl->gl_stamp = jiffies;
  997. }
  998. clear_bit(GLF_LOCK, &gl->gl_flags);
  999. run_queue(gl);
  1000. spin_unlock(&gl->gl_spin);
  1001. }
  1002. /**
  1003. * gfs2_glock_dq_uninit - dequeue a holder from a glock and initialize it
  1004. * @gh: the holder structure
  1005. *
  1006. */
  1007. void gfs2_glock_dq_uninit(struct gfs2_holder *gh)
  1008. {
  1009. gfs2_glock_dq(gh);
  1010. gfs2_holder_uninit(gh);
  1011. }
  1012. /**
  1013. * gfs2_glock_nq_num - acquire a glock based on lock number
  1014. * @sdp: the filesystem
  1015. * @number: the lock number
  1016. * @glops: the glock operations for the type of glock
  1017. * @state: the state to acquire the glock in
  1018. * @flags: modifier flags for the aquisition
  1019. * @gh: the struct gfs2_holder
  1020. *
  1021. * Returns: errno
  1022. */
  1023. int gfs2_glock_nq_num(struct gfs2_sbd *sdp, u64 number,
  1024. const struct gfs2_glock_operations *glops,
  1025. unsigned int state, int flags, struct gfs2_holder *gh)
  1026. {
  1027. struct gfs2_glock *gl;
  1028. int error;
  1029. error = gfs2_glock_get(sdp, number, glops, CREATE, &gl);
  1030. if (!error) {
  1031. error = gfs2_glock_nq_init(gl, state, flags, gh);
  1032. gfs2_glock_put(gl);
  1033. }
  1034. return error;
  1035. }
  1036. /**
  1037. * glock_compare - Compare two struct gfs2_glock structures for sorting
  1038. * @arg_a: the first structure
  1039. * @arg_b: the second structure
  1040. *
  1041. */
  1042. static int glock_compare(const void *arg_a, const void *arg_b)
  1043. {
  1044. const struct gfs2_holder *gh_a = *(const struct gfs2_holder **)arg_a;
  1045. const struct gfs2_holder *gh_b = *(const struct gfs2_holder **)arg_b;
  1046. const struct lm_lockname *a = &gh_a->gh_gl->gl_name;
  1047. const struct lm_lockname *b = &gh_b->gh_gl->gl_name;
  1048. if (a->ln_number > b->ln_number)
  1049. return 1;
  1050. if (a->ln_number < b->ln_number)
  1051. return -1;
  1052. BUG_ON(gh_a->gh_gl->gl_ops->go_type == gh_b->gh_gl->gl_ops->go_type);
  1053. return 0;
  1054. }
  1055. /**
  1056. * nq_m_sync - synchonously acquire more than one glock in deadlock free order
  1057. * @num_gh: the number of structures
  1058. * @ghs: an array of struct gfs2_holder structures
  1059. *
  1060. * Returns: 0 on success (all glocks acquired),
  1061. * errno on failure (no glocks acquired)
  1062. */
  1063. static int nq_m_sync(unsigned int num_gh, struct gfs2_holder *ghs,
  1064. struct gfs2_holder **p)
  1065. {
  1066. unsigned int x;
  1067. int error = 0;
  1068. for (x = 0; x < num_gh; x++)
  1069. p[x] = &ghs[x];
  1070. sort(p, num_gh, sizeof(struct gfs2_holder *), glock_compare, NULL);
  1071. for (x = 0; x < num_gh; x++) {
  1072. p[x]->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
  1073. error = gfs2_glock_nq(p[x]);
  1074. if (error) {
  1075. while (x--)
  1076. gfs2_glock_dq(p[x]);
  1077. break;
  1078. }
  1079. }
  1080. return error;
  1081. }
  1082. /**
  1083. * gfs2_glock_nq_m - acquire multiple glocks
  1084. * @num_gh: the number of structures
  1085. * @ghs: an array of struct gfs2_holder structures
  1086. *
  1087. * Figure out how big an impact this function has. Either:
  1088. * 1) Replace this code with code that calls gfs2_glock_prefetch()
  1089. * 2) Forget async stuff and just call nq_m_sync()
  1090. * 3) Leave it like it is
  1091. *
  1092. * Returns: 0 on success (all glocks acquired),
  1093. * errno on failure (no glocks acquired)
  1094. */
  1095. int gfs2_glock_nq_m(unsigned int num_gh, struct gfs2_holder *ghs)
  1096. {
  1097. int *e;
  1098. unsigned int x;
  1099. int borked = 0, serious = 0;
  1100. int error = 0;
  1101. if (!num_gh)
  1102. return 0;
  1103. if (num_gh == 1) {
  1104. ghs->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
  1105. return gfs2_glock_nq(ghs);
  1106. }
  1107. e = kcalloc(num_gh, sizeof(struct gfs2_holder *), GFP_KERNEL);
  1108. if (!e)
  1109. return -ENOMEM;
  1110. for (x = 0; x < num_gh; x++) {
  1111. ghs[x].gh_flags |= LM_FLAG_TRY | GL_ASYNC;
  1112. error = gfs2_glock_nq(&ghs[x]);
  1113. if (error) {
  1114. borked = 1;
  1115. serious = error;
  1116. num_gh = x;
  1117. break;
  1118. }
  1119. }
  1120. for (x = 0; x < num_gh; x++) {
  1121. error = e[x] = glock_wait_internal(&ghs[x]);
  1122. if (error) {
  1123. borked = 1;
  1124. if (error != GLR_TRYFAILED && error != GLR_CANCELED)
  1125. serious = error;
  1126. }
  1127. }
  1128. if (!borked) {
  1129. kfree(e);
  1130. return 0;
  1131. }
  1132. for (x = 0; x < num_gh; x++)
  1133. if (!e[x])
  1134. gfs2_glock_dq(&ghs[x]);
  1135. if (serious)
  1136. error = serious;
  1137. else {
  1138. for (x = 0; x < num_gh; x++)
  1139. gfs2_holder_reinit(ghs[x].gh_state, ghs[x].gh_flags,
  1140. &ghs[x]);
  1141. error = nq_m_sync(num_gh, ghs, (struct gfs2_holder **)e);
  1142. }
  1143. kfree(e);
  1144. return error;
  1145. }
  1146. /**
  1147. * gfs2_glock_dq_m - release multiple glocks
  1148. * @num_gh: the number of structures
  1149. * @ghs: an array of struct gfs2_holder structures
  1150. *
  1151. */
  1152. void gfs2_glock_dq_m(unsigned int num_gh, struct gfs2_holder *ghs)
  1153. {
  1154. unsigned int x;
  1155. for (x = 0; x < num_gh; x++)
  1156. gfs2_glock_dq(&ghs[x]);
  1157. }
  1158. /**
  1159. * gfs2_glock_dq_uninit_m - release multiple glocks
  1160. * @num_gh: the number of structures
  1161. * @ghs: an array of struct gfs2_holder structures
  1162. *
  1163. */
  1164. void gfs2_glock_dq_uninit_m(unsigned int num_gh, struct gfs2_holder *ghs)
  1165. {
  1166. unsigned int x;
  1167. for (x = 0; x < num_gh; x++)
  1168. gfs2_glock_dq_uninit(&ghs[x]);
  1169. }
  1170. /**
  1171. * gfs2_lvb_hold - attach a LVB from a glock
  1172. * @gl: The glock in question
  1173. *
  1174. */
  1175. int gfs2_lvb_hold(struct gfs2_glock *gl)
  1176. {
  1177. int error;
  1178. gfs2_glmutex_lock(gl);
  1179. if (!atomic_read(&gl->gl_lvb_count)) {
  1180. error = gfs2_lm_hold_lvb(gl->gl_sbd, gl->gl_lock, &gl->gl_lvb);
  1181. if (error) {
  1182. gfs2_glmutex_unlock(gl);
  1183. return error;
  1184. }
  1185. gfs2_glock_hold(gl);
  1186. }
  1187. atomic_inc(&gl->gl_lvb_count);
  1188. gfs2_glmutex_unlock(gl);
  1189. return 0;
  1190. }
  1191. /**
  1192. * gfs2_lvb_unhold - detach a LVB from a glock
  1193. * @gl: The glock in question
  1194. *
  1195. */
  1196. void gfs2_lvb_unhold(struct gfs2_glock *gl)
  1197. {
  1198. gfs2_glock_hold(gl);
  1199. gfs2_glmutex_lock(gl);
  1200. gfs2_assert(gl->gl_sbd, atomic_read(&gl->gl_lvb_count) > 0);
  1201. if (atomic_dec_and_test(&gl->gl_lvb_count)) {
  1202. gfs2_lm_unhold_lvb(gl->gl_sbd, gl->gl_lock, gl->gl_lvb);
  1203. gl->gl_lvb = NULL;
  1204. gfs2_glock_put(gl);
  1205. }
  1206. gfs2_glmutex_unlock(gl);
  1207. gfs2_glock_put(gl);
  1208. }
  1209. static void blocking_cb(struct gfs2_sbd *sdp, struct lm_lockname *name,
  1210. unsigned int state)
  1211. {
  1212. struct gfs2_glock *gl;
  1213. gl = gfs2_glock_find(sdp, name);
  1214. if (!gl)
  1215. return;
  1216. handle_callback(gl, state);
  1217. spin_lock(&gl->gl_spin);
  1218. run_queue(gl);
  1219. spin_unlock(&gl->gl_spin);
  1220. gfs2_glock_put(gl);
  1221. }
  1222. /**
  1223. * gfs2_glock_cb - Callback used by locking module
  1224. * @sdp: Pointer to the superblock
  1225. * @type: Type of callback
  1226. * @data: Type dependent data pointer
  1227. *
  1228. * Called by the locking module when it wants to tell us something.
  1229. * Either we need to drop a lock, one of our ASYNC requests completed, or
  1230. * a journal from another client needs to be recovered.
  1231. */
  1232. void gfs2_glock_cb(void *cb_data, unsigned int type, void *data)
  1233. {
  1234. struct gfs2_sbd *sdp = cb_data;
  1235. switch (type) {
  1236. case LM_CB_NEED_E:
  1237. blocking_cb(sdp, data, LM_ST_UNLOCKED);
  1238. return;
  1239. case LM_CB_NEED_D:
  1240. blocking_cb(sdp, data, LM_ST_DEFERRED);
  1241. return;
  1242. case LM_CB_NEED_S:
  1243. blocking_cb(sdp, data, LM_ST_SHARED);
  1244. return;
  1245. case LM_CB_ASYNC: {
  1246. struct lm_async_cb *async = data;
  1247. struct gfs2_glock *gl;
  1248. down_read(&gfs2_umount_flush_sem);
  1249. gl = gfs2_glock_find(sdp, &async->lc_name);
  1250. if (gfs2_assert_warn(sdp, gl))
  1251. return;
  1252. if (!gfs2_assert_warn(sdp, gl->gl_req_bh))
  1253. gl->gl_req_bh(gl, async->lc_ret);
  1254. gfs2_glock_put(gl);
  1255. up_read(&gfs2_umount_flush_sem);
  1256. return;
  1257. }
  1258. case LM_CB_NEED_RECOVERY:
  1259. gfs2_jdesc_make_dirty(sdp, *(unsigned int *)data);
  1260. if (sdp->sd_recoverd_process)
  1261. wake_up_process(sdp->sd_recoverd_process);
  1262. return;
  1263. case LM_CB_DROPLOCKS:
  1264. gfs2_gl_hash_clear(sdp, NO_WAIT);
  1265. gfs2_quota_scan(sdp);
  1266. return;
  1267. default:
  1268. gfs2_assert_warn(sdp, 0);
  1269. return;
  1270. }
  1271. }
  1272. /**
  1273. * demote_ok - Check to see if it's ok to unlock a glock
  1274. * @gl: the glock
  1275. *
  1276. * Returns: 1 if it's ok
  1277. */
  1278. static int demote_ok(struct gfs2_glock *gl)
  1279. {
  1280. const struct gfs2_glock_operations *glops = gl->gl_ops;
  1281. int demote = 1;
  1282. if (test_bit(GLF_STICKY, &gl->gl_flags))
  1283. demote = 0;
  1284. else if (glops->go_demote_ok)
  1285. demote = glops->go_demote_ok(gl);
  1286. return demote;
  1287. }
  1288. /**
  1289. * gfs2_glock_schedule_for_reclaim - Add a glock to the reclaim list
  1290. * @gl: the glock
  1291. *
  1292. */
  1293. void gfs2_glock_schedule_for_reclaim(struct gfs2_glock *gl)
  1294. {
  1295. struct gfs2_sbd *sdp = gl->gl_sbd;
  1296. spin_lock(&sdp->sd_reclaim_lock);
  1297. if (list_empty(&gl->gl_reclaim)) {
  1298. gfs2_glock_hold(gl);
  1299. list_add(&gl->gl_reclaim, &sdp->sd_reclaim_list);
  1300. atomic_inc(&sdp->sd_reclaim_count);
  1301. }
  1302. spin_unlock(&sdp->sd_reclaim_lock);
  1303. wake_up(&sdp->sd_reclaim_wq);
  1304. }
  1305. /**
  1306. * gfs2_reclaim_glock - process the next glock on the filesystem's reclaim list
  1307. * @sdp: the filesystem
  1308. *
  1309. * Called from gfs2_glockd() glock reclaim daemon, or when promoting a
  1310. * different glock and we notice that there are a lot of glocks in the
  1311. * reclaim list.
  1312. *
  1313. */
  1314. void gfs2_reclaim_glock(struct gfs2_sbd *sdp)
  1315. {
  1316. struct gfs2_glock *gl;
  1317. spin_lock(&sdp->sd_reclaim_lock);
  1318. if (list_empty(&sdp->sd_reclaim_list)) {
  1319. spin_unlock(&sdp->sd_reclaim_lock);
  1320. return;
  1321. }
  1322. gl = list_entry(sdp->sd_reclaim_list.next,
  1323. struct gfs2_glock, gl_reclaim);
  1324. list_del_init(&gl->gl_reclaim);
  1325. spin_unlock(&sdp->sd_reclaim_lock);
  1326. atomic_dec(&sdp->sd_reclaim_count);
  1327. atomic_inc(&sdp->sd_reclaimed);
  1328. if (gfs2_glmutex_trylock(gl)) {
  1329. if (list_empty(&gl->gl_holders) &&
  1330. gl->gl_state != LM_ST_UNLOCKED && demote_ok(gl))
  1331. handle_callback(gl, LM_ST_UNLOCKED);
  1332. gfs2_glmutex_unlock(gl);
  1333. }
  1334. gfs2_glock_put(gl);
  1335. }
  1336. /**
  1337. * examine_bucket - Call a function for glock in a hash bucket
  1338. * @examiner: the function
  1339. * @sdp: the filesystem
  1340. * @bucket: the bucket
  1341. *
  1342. * Returns: 1 if the bucket has entries
  1343. */
  1344. static int examine_bucket(glock_examiner examiner, struct gfs2_sbd *sdp,
  1345. unsigned int hash)
  1346. {
  1347. struct gfs2_glock *gl, *prev = NULL;
  1348. int has_entries = 0;
  1349. struct hlist_head *head = &gl_hash_table[hash].hb_list;
  1350. read_lock(gl_lock_addr(hash));
  1351. /* Can't use hlist_for_each_entry - don't want prefetch here */
  1352. if (hlist_empty(head))
  1353. goto out;
  1354. gl = list_entry(head->first, struct gfs2_glock, gl_list);
  1355. while(1) {
  1356. if (gl->gl_sbd == sdp) {
  1357. gfs2_glock_hold(gl);
  1358. read_unlock(gl_lock_addr(hash));
  1359. if (prev)
  1360. gfs2_glock_put(prev);
  1361. prev = gl;
  1362. examiner(gl);
  1363. has_entries = 1;
  1364. read_lock(gl_lock_addr(hash));
  1365. }
  1366. if (gl->gl_list.next == NULL)
  1367. break;
  1368. gl = list_entry(gl->gl_list.next, struct gfs2_glock, gl_list);
  1369. }
  1370. out:
  1371. read_unlock(gl_lock_addr(hash));
  1372. if (prev)
  1373. gfs2_glock_put(prev);
  1374. return has_entries;
  1375. }
  1376. /**
  1377. * scan_glock - look at a glock and see if we can reclaim it
  1378. * @gl: the glock to look at
  1379. *
  1380. */
  1381. static void scan_glock(struct gfs2_glock *gl)
  1382. {
  1383. if (gl->gl_ops == &gfs2_inode_glops && gl->gl_object)
  1384. return;
  1385. if (gfs2_glmutex_trylock(gl)) {
  1386. if (list_empty(&gl->gl_holders) &&
  1387. gl->gl_state != LM_ST_UNLOCKED && demote_ok(gl))
  1388. goto out_schedule;
  1389. gfs2_glmutex_unlock(gl);
  1390. }
  1391. return;
  1392. out_schedule:
  1393. gfs2_glmutex_unlock(gl);
  1394. gfs2_glock_schedule_for_reclaim(gl);
  1395. }
  1396. /**
  1397. * gfs2_scand_internal - Look for glocks and inodes to toss from memory
  1398. * @sdp: the filesystem
  1399. *
  1400. */
  1401. void gfs2_scand_internal(struct gfs2_sbd *sdp)
  1402. {
  1403. unsigned int x;
  1404. for (x = 0; x < GFS2_GL_HASH_SIZE; x++)
  1405. examine_bucket(scan_glock, sdp, x);
  1406. }
  1407. /**
  1408. * clear_glock - look at a glock and see if we can free it from glock cache
  1409. * @gl: the glock to look at
  1410. *
  1411. */
  1412. static void clear_glock(struct gfs2_glock *gl)
  1413. {
  1414. struct gfs2_sbd *sdp = gl->gl_sbd;
  1415. int released;
  1416. spin_lock(&sdp->sd_reclaim_lock);
  1417. if (!list_empty(&gl->gl_reclaim)) {
  1418. list_del_init(&gl->gl_reclaim);
  1419. atomic_dec(&sdp->sd_reclaim_count);
  1420. spin_unlock(&sdp->sd_reclaim_lock);
  1421. released = gfs2_glock_put(gl);
  1422. gfs2_assert(sdp, !released);
  1423. } else {
  1424. spin_unlock(&sdp->sd_reclaim_lock);
  1425. }
  1426. if (gfs2_glmutex_trylock(gl)) {
  1427. if (list_empty(&gl->gl_holders) &&
  1428. gl->gl_state != LM_ST_UNLOCKED)
  1429. handle_callback(gl, LM_ST_UNLOCKED);
  1430. gfs2_glmutex_unlock(gl);
  1431. }
  1432. }
  1433. /**
  1434. * gfs2_gl_hash_clear - Empty out the glock hash table
  1435. * @sdp: the filesystem
  1436. * @wait: wait until it's all gone
  1437. *
  1438. * Called when unmounting the filesystem, or when inter-node lock manager
  1439. * requests DROPLOCKS because it is running out of capacity.
  1440. */
  1441. void gfs2_gl_hash_clear(struct gfs2_sbd *sdp, int wait)
  1442. {
  1443. unsigned long t;
  1444. unsigned int x;
  1445. int cont;
  1446. t = jiffies;
  1447. for (;;) {
  1448. cont = 0;
  1449. for (x = 0; x < GFS2_GL_HASH_SIZE; x++) {
  1450. if (examine_bucket(clear_glock, sdp, x))
  1451. cont = 1;
  1452. }
  1453. if (!wait || !cont)
  1454. break;
  1455. if (time_after_eq(jiffies,
  1456. t + gfs2_tune_get(sdp, gt_stall_secs) * HZ)) {
  1457. fs_warn(sdp, "Unmount seems to be stalled. "
  1458. "Dumping lock state...\n");
  1459. gfs2_dump_lockstate(sdp);
  1460. t = jiffies;
  1461. }
  1462. down_write(&gfs2_umount_flush_sem);
  1463. invalidate_inodes(sdp->sd_vfs);
  1464. up_write(&gfs2_umount_flush_sem);
  1465. msleep(10);
  1466. }
  1467. }
  1468. /*
  1469. * Diagnostic routines to help debug distributed deadlock
  1470. */
  1471. static void gfs2_print_symbol(struct glock_iter *gi, const char *fmt,
  1472. unsigned long address)
  1473. {
  1474. /* when sprint_symbol becomes available in the new kernel, replace this */
  1475. /* function with:
  1476. char buffer[KSYM_SYMBOL_LEN];
  1477. sprint_symbol(buffer, address);
  1478. print_dbg(gi, fmt, buffer);
  1479. */
  1480. char buffer[256];
  1481. if (gi) {
  1482. memset(buffer, 0, sizeof(buffer));
  1483. sprintf(buffer, "%p", address);
  1484. print_dbg(gi, fmt, buffer);
  1485. }
  1486. else
  1487. print_symbol(fmt, address);
  1488. }
  1489. /**
  1490. * dump_holder - print information about a glock holder
  1491. * @str: a string naming the type of holder
  1492. * @gh: the glock holder
  1493. *
  1494. * Returns: 0 on success, -ENOBUFS when we run out of space
  1495. */
  1496. static int dump_holder(struct glock_iter *gi, char *str,
  1497. struct gfs2_holder *gh)
  1498. {
  1499. unsigned int x;
  1500. struct task_struct *gh_owner;
  1501. print_dbg(gi, " %s\n", str);
  1502. if (gh->gh_owner_pid) {
  1503. print_dbg(gi, " owner = %ld ", (long)gh->gh_owner_pid);
  1504. gh_owner = find_task_by_pid(gh->gh_owner_pid);
  1505. if (gh_owner)
  1506. print_dbg(gi, "(%s)\n", gh_owner->comm);
  1507. else
  1508. print_dbg(gi, "(ended)\n");
  1509. } else
  1510. print_dbg(gi, " owner = -1\n");
  1511. print_dbg(gi, " gh_state = %u\n", gh->gh_state);
  1512. print_dbg(gi, " gh_flags =");
  1513. for (x = 0; x < 32; x++)
  1514. if (gh->gh_flags & (1 << x))
  1515. print_dbg(gi, " %u", x);
  1516. print_dbg(gi, " \n");
  1517. print_dbg(gi, " error = %d\n", gh->gh_error);
  1518. print_dbg(gi, " gh_iflags =");
  1519. for (x = 0; x < 32; x++)
  1520. if (test_bit(x, &gh->gh_iflags))
  1521. print_dbg(gi, " %u", x);
  1522. print_dbg(gi, " \n");
  1523. gfs2_print_symbol(gi, " initialized at: %s\n", gh->gh_ip);
  1524. return 0;
  1525. }
  1526. /**
  1527. * dump_inode - print information about an inode
  1528. * @ip: the inode
  1529. *
  1530. * Returns: 0 on success, -ENOBUFS when we run out of space
  1531. */
  1532. static int dump_inode(struct glock_iter *gi, struct gfs2_inode *ip)
  1533. {
  1534. unsigned int x;
  1535. print_dbg(gi, " Inode:\n");
  1536. print_dbg(gi, " num = %llu/%llu\n",
  1537. ip->i_num.no_formal_ino, ip->i_num.no_addr);
  1538. print_dbg(gi, " type = %u\n", IF2DT(ip->i_inode.i_mode));
  1539. print_dbg(gi, " i_flags =");
  1540. for (x = 0; x < 32; x++)
  1541. if (test_bit(x, &ip->i_flags))
  1542. print_dbg(gi, " %u", x);
  1543. print_dbg(gi, " \n");
  1544. return 0;
  1545. }
  1546. /**
  1547. * dump_glock - print information about a glock
  1548. * @gl: the glock
  1549. * @count: where we are in the buffer
  1550. *
  1551. * Returns: 0 on success, -ENOBUFS when we run out of space
  1552. */
  1553. static int dump_glock(struct glock_iter *gi, struct gfs2_glock *gl)
  1554. {
  1555. struct gfs2_holder *gh;
  1556. unsigned int x;
  1557. int error = -ENOBUFS;
  1558. struct task_struct *gl_owner;
  1559. spin_lock(&gl->gl_spin);
  1560. print_dbg(gi, "Glock 0x%p (%u, %llu)\n", gl, gl->gl_name.ln_type,
  1561. (unsigned long long)gl->gl_name.ln_number);
  1562. print_dbg(gi, " gl_flags =");
  1563. for (x = 0; x < 32; x++) {
  1564. if (test_bit(x, &gl->gl_flags))
  1565. print_dbg(gi, " %u", x);
  1566. }
  1567. if (!test_bit(GLF_LOCK, &gl->gl_flags))
  1568. print_dbg(gi, " (unlocked)");
  1569. print_dbg(gi, " \n");
  1570. print_dbg(gi, " gl_ref = %d\n", atomic_read(&gl->gl_ref));
  1571. print_dbg(gi, " gl_state = %u\n", gl->gl_state);
  1572. if (gl->gl_owner_pid) {
  1573. gl_owner = find_task_by_pid(gl->gl_owner_pid);
  1574. if (gl_owner)
  1575. print_dbg(gi, " gl_owner = pid %d (%s)\n",
  1576. gl->gl_owner_pid, gl_owner->comm);
  1577. else
  1578. print_dbg(gi, " gl_owner = %d (ended)\n",
  1579. gl->gl_owner_pid);
  1580. } else
  1581. print_dbg(gi, " gl_owner = -1\n");
  1582. print_dbg(gi, " gl_ip = %lu\n", gl->gl_ip);
  1583. print_dbg(gi, " req_gh = %s\n", (gl->gl_req_gh) ? "yes" : "no");
  1584. print_dbg(gi, " req_bh = %s\n", (gl->gl_req_bh) ? "yes" : "no");
  1585. print_dbg(gi, " lvb_count = %d\n", atomic_read(&gl->gl_lvb_count));
  1586. print_dbg(gi, " object = %s\n", (gl->gl_object) ? "yes" : "no");
  1587. print_dbg(gi, " le = %s\n",
  1588. (list_empty(&gl->gl_le.le_list)) ? "no" : "yes");
  1589. print_dbg(gi, " reclaim = %s\n",
  1590. (list_empty(&gl->gl_reclaim)) ? "no" : "yes");
  1591. if (gl->gl_aspace)
  1592. print_dbg(gi, " aspace = 0x%p nrpages = %lu\n", gl->gl_aspace,
  1593. gl->gl_aspace->i_mapping->nrpages);
  1594. else
  1595. print_dbg(gi, " aspace = no\n");
  1596. print_dbg(gi, " ail = %d\n", atomic_read(&gl->gl_ail_count));
  1597. if (gl->gl_req_gh) {
  1598. error = dump_holder(gi, "Request", gl->gl_req_gh);
  1599. if (error)
  1600. goto out;
  1601. }
  1602. list_for_each_entry(gh, &gl->gl_holders, gh_list) {
  1603. error = dump_holder(gi, "Holder", gh);
  1604. if (error)
  1605. goto out;
  1606. }
  1607. list_for_each_entry(gh, &gl->gl_waiters1, gh_list) {
  1608. error = dump_holder(gi, "Waiter1", gh);
  1609. if (error)
  1610. goto out;
  1611. }
  1612. list_for_each_entry(gh, &gl->gl_waiters3, gh_list) {
  1613. error = dump_holder(gi, "Waiter3", gh);
  1614. if (error)
  1615. goto out;
  1616. }
  1617. if (test_bit(GLF_DEMOTE, &gl->gl_flags)) {
  1618. print_dbg(gi, " Demotion req to state %u (%llu uS ago)\n",
  1619. gl->gl_demote_state,
  1620. (u64)(jiffies - gl->gl_demote_time)*(1000000/HZ));
  1621. }
  1622. if (gl->gl_ops == &gfs2_inode_glops && gl->gl_object) {
  1623. if (!test_bit(GLF_LOCK, &gl->gl_flags) &&
  1624. list_empty(&gl->gl_holders)) {
  1625. error = dump_inode(gi, gl->gl_object);
  1626. if (error)
  1627. goto out;
  1628. } else {
  1629. error = -ENOBUFS;
  1630. print_dbg(gi, " Inode: busy\n");
  1631. }
  1632. }
  1633. error = 0;
  1634. out:
  1635. spin_unlock(&gl->gl_spin);
  1636. return error;
  1637. }
  1638. /**
  1639. * gfs2_dump_lockstate - print out the current lockstate
  1640. * @sdp: the filesystem
  1641. * @ub: the buffer to copy the information into
  1642. *
  1643. * If @ub is NULL, dump the lockstate to the console.
  1644. *
  1645. */
  1646. static int gfs2_dump_lockstate(struct gfs2_sbd *sdp)
  1647. {
  1648. struct gfs2_glock *gl;
  1649. struct hlist_node *h;
  1650. unsigned int x;
  1651. int error = 0;
  1652. for (x = 0; x < GFS2_GL_HASH_SIZE; x++) {
  1653. read_lock(gl_lock_addr(x));
  1654. hlist_for_each_entry(gl, h, &gl_hash_table[x].hb_list, gl_list) {
  1655. if (gl->gl_sbd != sdp)
  1656. continue;
  1657. error = dump_glock(NULL, gl);
  1658. if (error)
  1659. break;
  1660. }
  1661. read_unlock(gl_lock_addr(x));
  1662. if (error)
  1663. break;
  1664. }
  1665. return error;
  1666. }
  1667. int __init gfs2_glock_init(void)
  1668. {
  1669. unsigned i;
  1670. for(i = 0; i < GFS2_GL_HASH_SIZE; i++) {
  1671. INIT_HLIST_HEAD(&gl_hash_table[i].hb_list);
  1672. }
  1673. #ifdef GL_HASH_LOCK_SZ
  1674. for(i = 0; i < GL_HASH_LOCK_SZ; i++) {
  1675. rwlock_init(&gl_hash_locks[i]);
  1676. }
  1677. #endif
  1678. return 0;
  1679. }
  1680. static int gfs2_glock_iter_next(struct glock_iter *gi)
  1681. {
  1682. read_lock(gl_lock_addr(gi->hash));
  1683. while (1) {
  1684. if (!gi->hb_list) { /* If we don't have a hash bucket yet */
  1685. gi->hb_list = &gl_hash_table[gi->hash].hb_list;
  1686. if (hlist_empty(gi->hb_list)) {
  1687. read_unlock(gl_lock_addr(gi->hash));
  1688. gi->hash++;
  1689. read_lock(gl_lock_addr(gi->hash));
  1690. gi->hb_list = NULL;
  1691. if (gi->hash >= GFS2_GL_HASH_SIZE) {
  1692. read_unlock(gl_lock_addr(gi->hash));
  1693. return 1;
  1694. }
  1695. else
  1696. continue;
  1697. }
  1698. if (!hlist_empty(gi->hb_list)) {
  1699. gi->gl = list_entry(gi->hb_list->first,
  1700. struct gfs2_glock,
  1701. gl_list);
  1702. }
  1703. } else {
  1704. if (gi->gl->gl_list.next == NULL) {
  1705. read_unlock(gl_lock_addr(gi->hash));
  1706. gi->hash++;
  1707. read_lock(gl_lock_addr(gi->hash));
  1708. gi->hb_list = NULL;
  1709. continue;
  1710. }
  1711. gi->gl = list_entry(gi->gl->gl_list.next,
  1712. struct gfs2_glock, gl_list);
  1713. }
  1714. if (gi->gl)
  1715. break;
  1716. }
  1717. read_unlock(gl_lock_addr(gi->hash));
  1718. return 0;
  1719. }
  1720. static void gfs2_glock_iter_free(struct glock_iter *gi)
  1721. {
  1722. kfree(gi);
  1723. }
  1724. static struct glock_iter *gfs2_glock_iter_init(struct gfs2_sbd *sdp)
  1725. {
  1726. struct glock_iter *gi;
  1727. gi = kmalloc(sizeof (*gi), GFP_KERNEL);
  1728. if (!gi)
  1729. return NULL;
  1730. gi->sdp = sdp;
  1731. gi->hash = 0;
  1732. gi->gl = NULL;
  1733. gi->hb_list = NULL;
  1734. gi->seq = NULL;
  1735. memset(gi->string, 0, sizeof(gi->string));
  1736. if (gfs2_glock_iter_next(gi)) {
  1737. gfs2_glock_iter_free(gi);
  1738. return NULL;
  1739. }
  1740. return gi;
  1741. }
  1742. static void *gfs2_glock_seq_start(struct seq_file *file, loff_t *pos)
  1743. {
  1744. struct glock_iter *gi;
  1745. loff_t n = *pos;
  1746. gi = gfs2_glock_iter_init(file->private);
  1747. if (!gi)
  1748. return NULL;
  1749. while (n--) {
  1750. if (gfs2_glock_iter_next(gi)) {
  1751. gfs2_glock_iter_free(gi);
  1752. return NULL;
  1753. }
  1754. }
  1755. return gi;
  1756. }
  1757. static void *gfs2_glock_seq_next(struct seq_file *file, void *iter_ptr,
  1758. loff_t *pos)
  1759. {
  1760. struct glock_iter *gi = iter_ptr;
  1761. (*pos)++;
  1762. if (gfs2_glock_iter_next(gi)) {
  1763. gfs2_glock_iter_free(gi);
  1764. return NULL;
  1765. }
  1766. return gi;
  1767. }
  1768. static void gfs2_glock_seq_stop(struct seq_file *file, void *iter_ptr)
  1769. {
  1770. /* nothing for now */
  1771. }
  1772. static int gfs2_glock_seq_show(struct seq_file *file, void *iter_ptr)
  1773. {
  1774. struct glock_iter *gi = iter_ptr;
  1775. gi->seq = file;
  1776. dump_glock(gi, gi->gl);
  1777. return 0;
  1778. }
  1779. static struct seq_operations gfs2_glock_seq_ops = {
  1780. .start = gfs2_glock_seq_start,
  1781. .next = gfs2_glock_seq_next,
  1782. .stop = gfs2_glock_seq_stop,
  1783. .show = gfs2_glock_seq_show,
  1784. };
  1785. static int gfs2_debugfs_open(struct inode *inode, struct file *file)
  1786. {
  1787. struct seq_file *seq;
  1788. int ret;
  1789. ret = seq_open(file, &gfs2_glock_seq_ops);
  1790. if (ret)
  1791. return ret;
  1792. seq = file->private_data;
  1793. seq->private = inode->i_private;
  1794. return 0;
  1795. }
  1796. static const struct file_operations gfs2_debug_fops = {
  1797. .owner = THIS_MODULE,
  1798. .open = gfs2_debugfs_open,
  1799. .read = seq_read,
  1800. .llseek = seq_lseek,
  1801. .release = seq_release
  1802. };
  1803. int gfs2_create_debugfs_file(struct gfs2_sbd *sdp)
  1804. {
  1805. sdp->debugfs_dentry = debugfs_create_file(sdp->sd_table_name,
  1806. S_IFREG | S_IRUGO,
  1807. gfs2_root, sdp,
  1808. &gfs2_debug_fops);
  1809. if (!sdp->debugfs_dentry)
  1810. return -ENOMEM;
  1811. return 0;
  1812. }
  1813. void gfs2_delete_debugfs_file(struct gfs2_sbd *sdp)
  1814. {
  1815. if (sdp && sdp->debugfs_dentry)
  1816. debugfs_remove(sdp->debugfs_dentry);
  1817. }
  1818. int gfs2_register_debugfs(void)
  1819. {
  1820. gfs2_root = debugfs_create_dir("gfs2", NULL);
  1821. return gfs2_root ? 0 : -ENOMEM;
  1822. }
  1823. void gfs2_unregister_debugfs(void)
  1824. {
  1825. debugfs_remove(gfs2_root);
  1826. }