glock.c 46 KB

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