glock.c 46 KB

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