glock.c 50 KB

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