glock.c 50 KB

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