glock.c 51 KB

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