glock.c 50 KB

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