glock.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213
  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 delayed_work 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(unsigned int 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_NOFS);
  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. /**
  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. if (test_bit(HIF_DEALLOC, &gh->gh_iflags))
  762. gfs2_holder_put(gh);
  763. else
  764. complete(&gh->gh_wait);
  765. }
  766. }
  767. /**
  768. * gfs2_glock_xmote_th - Call into the lock module to acquire or change a glock
  769. * @gl: The glock in question
  770. * @state: the requested state
  771. * @flags: modifier flags to the lock call
  772. *
  773. */
  774. void gfs2_glock_xmote_th(struct gfs2_glock *gl, unsigned int state, int flags)
  775. {
  776. struct gfs2_sbd *sdp = gl->gl_sbd;
  777. const struct gfs2_glock_operations *glops = gl->gl_ops;
  778. int lck_flags = flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB |
  779. LM_FLAG_NOEXP | LM_FLAG_ANY |
  780. LM_FLAG_PRIORITY);
  781. unsigned int lck_ret;
  782. gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
  783. gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
  784. gfs2_assert_warn(sdp, state != LM_ST_UNLOCKED);
  785. gfs2_assert_warn(sdp, state != gl->gl_state);
  786. if (gl->gl_state == LM_ST_EXCLUSIVE && glops->go_sync)
  787. glops->go_sync(gl);
  788. gfs2_glock_hold(gl);
  789. gl->gl_req_bh = xmote_bh;
  790. lck_ret = gfs2_lm_lock(sdp, gl->gl_lock, gl->gl_state, state, lck_flags);
  791. if (gfs2_assert_withdraw(sdp, !(lck_ret & LM_OUT_ERROR)))
  792. return;
  793. if (lck_ret & LM_OUT_ASYNC)
  794. gfs2_assert_warn(sdp, lck_ret == LM_OUT_ASYNC);
  795. else
  796. xmote_bh(gl, lck_ret);
  797. }
  798. /**
  799. * drop_bh - Called after a lock module unlock completes
  800. * @gl: the glock
  801. * @ret: the return status
  802. *
  803. * Doesn't wake up the process waiting on the struct gfs2_holder (if any)
  804. * Doesn't drop the reference on the glock the top half took out
  805. *
  806. */
  807. static void drop_bh(struct gfs2_glock *gl, unsigned int ret)
  808. {
  809. struct gfs2_sbd *sdp = gl->gl_sbd;
  810. const struct gfs2_glock_operations *glops = gl->gl_ops;
  811. struct gfs2_holder *gh = gl->gl_req_gh;
  812. clear_bit(GLF_PREFETCH, &gl->gl_flags);
  813. gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
  814. gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
  815. gfs2_assert_warn(sdp, !ret);
  816. state_change(gl, LM_ST_UNLOCKED);
  817. if (glops->go_inval)
  818. glops->go_inval(gl, DIO_METADATA);
  819. if (gh) {
  820. spin_lock(&gl->gl_spin);
  821. list_del_init(&gh->gh_list);
  822. gh->gh_error = 0;
  823. spin_unlock(&gl->gl_spin);
  824. }
  825. if (glops->go_drop_bh)
  826. glops->go_drop_bh(gl);
  827. spin_lock(&gl->gl_spin);
  828. gl->gl_req_gh = NULL;
  829. gl->gl_req_bh = NULL;
  830. clear_bit(GLF_LOCK, &gl->gl_flags);
  831. run_queue(gl);
  832. spin_unlock(&gl->gl_spin);
  833. gfs2_glock_put(gl);
  834. if (gh) {
  835. if (test_bit(HIF_DEALLOC, &gh->gh_iflags))
  836. gfs2_holder_put(gh);
  837. else
  838. complete(&gh->gh_wait);
  839. }
  840. }
  841. /**
  842. * gfs2_glock_drop_th - call into the lock module to unlock a lock
  843. * @gl: the glock
  844. *
  845. */
  846. void gfs2_glock_drop_th(struct gfs2_glock *gl)
  847. {
  848. struct gfs2_sbd *sdp = gl->gl_sbd;
  849. const struct gfs2_glock_operations *glops = gl->gl_ops;
  850. unsigned int ret;
  851. gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
  852. gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
  853. gfs2_assert_warn(sdp, gl->gl_state != LM_ST_UNLOCKED);
  854. if (gl->gl_state == LM_ST_EXCLUSIVE && glops->go_sync)
  855. glops->go_sync(gl);
  856. gfs2_glock_hold(gl);
  857. gl->gl_req_bh = drop_bh;
  858. ret = gfs2_lm_unlock(sdp, gl->gl_lock, gl->gl_state);
  859. if (gfs2_assert_withdraw(sdp, !(ret & LM_OUT_ERROR)))
  860. return;
  861. if (!ret)
  862. drop_bh(gl, ret);
  863. else
  864. gfs2_assert_warn(sdp, ret == LM_OUT_ASYNC);
  865. }
  866. /**
  867. * do_cancels - cancel requests for locks stuck waiting on an expire flag
  868. * @gh: the LM_FLAG_PRIORITY holder waiting to acquire the lock
  869. *
  870. * Don't cancel GL_NOCANCEL requests.
  871. */
  872. static void do_cancels(struct gfs2_holder *gh)
  873. {
  874. struct gfs2_glock *gl = gh->gh_gl;
  875. spin_lock(&gl->gl_spin);
  876. while (gl->gl_req_gh != gh &&
  877. !test_bit(HIF_HOLDER, &gh->gh_iflags) &&
  878. !list_empty(&gh->gh_list)) {
  879. if (gl->gl_req_bh && !(gl->gl_req_gh &&
  880. (gl->gl_req_gh->gh_flags & GL_NOCANCEL))) {
  881. spin_unlock(&gl->gl_spin);
  882. gfs2_lm_cancel(gl->gl_sbd, gl->gl_lock);
  883. msleep(100);
  884. spin_lock(&gl->gl_spin);
  885. } else {
  886. spin_unlock(&gl->gl_spin);
  887. msleep(100);
  888. spin_lock(&gl->gl_spin);
  889. }
  890. }
  891. spin_unlock(&gl->gl_spin);
  892. }
  893. /**
  894. * glock_wait_internal - wait on a glock acquisition
  895. * @gh: the glock holder
  896. *
  897. * Returns: 0 on success
  898. */
  899. static int glock_wait_internal(struct gfs2_holder *gh)
  900. {
  901. struct gfs2_glock *gl = gh->gh_gl;
  902. struct gfs2_sbd *sdp = gl->gl_sbd;
  903. const struct gfs2_glock_operations *glops = gl->gl_ops;
  904. if (test_bit(HIF_ABORTED, &gh->gh_iflags))
  905. return -EIO;
  906. if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) {
  907. spin_lock(&gl->gl_spin);
  908. if (gl->gl_req_gh != gh &&
  909. !test_bit(HIF_HOLDER, &gh->gh_iflags) &&
  910. !list_empty(&gh->gh_list)) {
  911. list_del_init(&gh->gh_list);
  912. gh->gh_error = GLR_TRYFAILED;
  913. run_queue(gl);
  914. spin_unlock(&gl->gl_spin);
  915. return gh->gh_error;
  916. }
  917. spin_unlock(&gl->gl_spin);
  918. }
  919. if (gh->gh_flags & LM_FLAG_PRIORITY)
  920. do_cancels(gh);
  921. wait_for_completion(&gh->gh_wait);
  922. if (gh->gh_error)
  923. return gh->gh_error;
  924. gfs2_assert_withdraw(sdp, test_bit(HIF_HOLDER, &gh->gh_iflags));
  925. gfs2_assert_withdraw(sdp, relaxed_state_ok(gl->gl_state, gh->gh_state,
  926. gh->gh_flags));
  927. if (test_bit(HIF_FIRST, &gh->gh_iflags)) {
  928. gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
  929. if (glops->go_lock) {
  930. gh->gh_error = glops->go_lock(gh);
  931. if (gh->gh_error) {
  932. spin_lock(&gl->gl_spin);
  933. list_del_init(&gh->gh_list);
  934. spin_unlock(&gl->gl_spin);
  935. }
  936. }
  937. spin_lock(&gl->gl_spin);
  938. gl->gl_req_gh = NULL;
  939. gl->gl_req_bh = NULL;
  940. clear_bit(GLF_LOCK, &gl->gl_flags);
  941. run_queue(gl);
  942. spin_unlock(&gl->gl_spin);
  943. }
  944. return gh->gh_error;
  945. }
  946. static inline struct gfs2_holder *
  947. find_holder_by_owner(struct list_head *head, struct task_struct *owner)
  948. {
  949. struct gfs2_holder *gh;
  950. list_for_each_entry(gh, head, gh_list) {
  951. if (gh->gh_owner == owner)
  952. return gh;
  953. }
  954. return NULL;
  955. }
  956. /**
  957. * add_to_queue - Add a holder to the wait queue (but look for recursion)
  958. * @gh: the holder structure to add
  959. *
  960. */
  961. static void add_to_queue(struct gfs2_holder *gh)
  962. {
  963. struct gfs2_glock *gl = gh->gh_gl;
  964. struct gfs2_holder *existing;
  965. BUG_ON(!gh->gh_owner);
  966. existing = find_holder_by_owner(&gl->gl_holders, gh->gh_owner);
  967. if (existing) {
  968. print_symbol(KERN_WARNING "original: %s\n", existing->gh_ip);
  969. printk(KERN_INFO "pid : %d\n", existing->gh_owner->pid);
  970. printk(KERN_INFO "lock type : %d lock state : %d\n",
  971. existing->gh_gl->gl_name.ln_type, existing->gh_gl->gl_state);
  972. print_symbol(KERN_WARNING "new: %s\n", gh->gh_ip);
  973. printk(KERN_INFO "pid : %d\n", gh->gh_owner->pid);
  974. printk(KERN_INFO "lock type : %d lock state : %d\n",
  975. gl->gl_name.ln_type, gl->gl_state);
  976. BUG();
  977. }
  978. existing = find_holder_by_owner(&gl->gl_waiters3, gh->gh_owner);
  979. if (existing) {
  980. print_symbol(KERN_WARNING "original: %s\n", existing->gh_ip);
  981. print_symbol(KERN_WARNING "new: %s\n", gh->gh_ip);
  982. BUG();
  983. }
  984. if (gh->gh_flags & LM_FLAG_PRIORITY)
  985. list_add(&gh->gh_list, &gl->gl_waiters3);
  986. else
  987. list_add_tail(&gh->gh_list, &gl->gl_waiters3);
  988. }
  989. /**
  990. * gfs2_glock_nq - enqueue a struct gfs2_holder onto a glock (acquire a glock)
  991. * @gh: the holder structure
  992. *
  993. * if (gh->gh_flags & GL_ASYNC), this never returns an error
  994. *
  995. * Returns: 0, GLR_TRYFAILED, or errno on failure
  996. */
  997. int gfs2_glock_nq(struct gfs2_holder *gh)
  998. {
  999. struct gfs2_glock *gl = gh->gh_gl;
  1000. struct gfs2_sbd *sdp = gl->gl_sbd;
  1001. int error = 0;
  1002. restart:
  1003. if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) {
  1004. set_bit(HIF_ABORTED, &gh->gh_iflags);
  1005. return -EIO;
  1006. }
  1007. set_bit(HIF_PROMOTE, &gh->gh_iflags);
  1008. spin_lock(&gl->gl_spin);
  1009. add_to_queue(gh);
  1010. run_queue(gl);
  1011. spin_unlock(&gl->gl_spin);
  1012. if (!(gh->gh_flags & GL_ASYNC)) {
  1013. error = glock_wait_internal(gh);
  1014. if (error == GLR_CANCELED) {
  1015. msleep(100);
  1016. goto restart;
  1017. }
  1018. }
  1019. clear_bit(GLF_PREFETCH, &gl->gl_flags);
  1020. return error;
  1021. }
  1022. /**
  1023. * gfs2_glock_poll - poll to see if an async request has been completed
  1024. * @gh: the holder
  1025. *
  1026. * Returns: 1 if the request is ready to be gfs2_glock_wait()ed on
  1027. */
  1028. int gfs2_glock_poll(struct gfs2_holder *gh)
  1029. {
  1030. struct gfs2_glock *gl = gh->gh_gl;
  1031. int ready = 0;
  1032. spin_lock(&gl->gl_spin);
  1033. if (test_bit(HIF_HOLDER, &gh->gh_iflags))
  1034. ready = 1;
  1035. else if (list_empty(&gh->gh_list)) {
  1036. if (gh->gh_error == GLR_CANCELED) {
  1037. spin_unlock(&gl->gl_spin);
  1038. msleep(100);
  1039. if (gfs2_glock_nq(gh))
  1040. return 1;
  1041. return 0;
  1042. } else
  1043. ready = 1;
  1044. }
  1045. spin_unlock(&gl->gl_spin);
  1046. return ready;
  1047. }
  1048. /**
  1049. * gfs2_glock_wait - wait for a lock acquisition that ended in a GLR_ASYNC
  1050. * @gh: the holder structure
  1051. *
  1052. * Returns: 0, GLR_TRYFAILED, or errno on failure
  1053. */
  1054. int gfs2_glock_wait(struct gfs2_holder *gh)
  1055. {
  1056. int error;
  1057. error = glock_wait_internal(gh);
  1058. if (error == GLR_CANCELED) {
  1059. msleep(100);
  1060. gh->gh_flags &= ~GL_ASYNC;
  1061. error = gfs2_glock_nq(gh);
  1062. }
  1063. return error;
  1064. }
  1065. /**
  1066. * gfs2_glock_dq - dequeue a struct gfs2_holder from a glock (release a glock)
  1067. * @gh: the glock holder
  1068. *
  1069. */
  1070. void gfs2_glock_dq(struct gfs2_holder *gh)
  1071. {
  1072. struct gfs2_glock *gl = gh->gh_gl;
  1073. const struct gfs2_glock_operations *glops = gl->gl_ops;
  1074. if (gh->gh_flags & GL_NOCACHE)
  1075. handle_callback(gl, LM_ST_UNLOCKED);
  1076. gfs2_glmutex_lock(gl);
  1077. spin_lock(&gl->gl_spin);
  1078. list_del_init(&gh->gh_list);
  1079. if (list_empty(&gl->gl_holders)) {
  1080. spin_unlock(&gl->gl_spin);
  1081. if (glops->go_unlock)
  1082. glops->go_unlock(gh);
  1083. gl->gl_stamp = jiffies;
  1084. spin_lock(&gl->gl_spin);
  1085. }
  1086. clear_bit(GLF_LOCK, &gl->gl_flags);
  1087. run_queue(gl);
  1088. spin_unlock(&gl->gl_spin);
  1089. }
  1090. /**
  1091. * gfs2_glock_prefetch - Try to prefetch a glock
  1092. * @gl: the glock
  1093. * @state: the state to prefetch in
  1094. * @flags: flags passed to go_xmote_th()
  1095. *
  1096. */
  1097. static void gfs2_glock_prefetch(struct gfs2_glock *gl, unsigned int state,
  1098. int flags)
  1099. {
  1100. const struct gfs2_glock_operations *glops = gl->gl_ops;
  1101. spin_lock(&gl->gl_spin);
  1102. if (test_bit(GLF_LOCK, &gl->gl_flags) || !list_empty(&gl->gl_holders) ||
  1103. !list_empty(&gl->gl_waiters1) || !list_empty(&gl->gl_waiters2) ||
  1104. !list_empty(&gl->gl_waiters3) ||
  1105. relaxed_state_ok(gl->gl_state, state, flags)) {
  1106. spin_unlock(&gl->gl_spin);
  1107. return;
  1108. }
  1109. set_bit(GLF_PREFETCH, &gl->gl_flags);
  1110. set_bit(GLF_LOCK, &gl->gl_flags);
  1111. spin_unlock(&gl->gl_spin);
  1112. glops->go_xmote_th(gl, state, flags);
  1113. }
  1114. static void greedy_work(struct work_struct *work)
  1115. {
  1116. struct greedy *gr = container_of(work, struct greedy, gr_work.work);
  1117. struct gfs2_holder *gh = &gr->gr_gh;
  1118. struct gfs2_glock *gl = gh->gh_gl;
  1119. const struct gfs2_glock_operations *glops = gl->gl_ops;
  1120. clear_bit(GLF_SKIP_WAITERS2, &gl->gl_flags);
  1121. if (glops->go_greedy)
  1122. glops->go_greedy(gl);
  1123. spin_lock(&gl->gl_spin);
  1124. if (list_empty(&gl->gl_waiters2)) {
  1125. clear_bit(GLF_GREEDY, &gl->gl_flags);
  1126. spin_unlock(&gl->gl_spin);
  1127. gfs2_holder_uninit(gh);
  1128. kfree(gr);
  1129. } else {
  1130. gfs2_glock_hold(gl);
  1131. list_add_tail(&gh->gh_list, &gl->gl_waiters2);
  1132. run_queue(gl);
  1133. spin_unlock(&gl->gl_spin);
  1134. gfs2_glock_put(gl);
  1135. }
  1136. }
  1137. /**
  1138. * gfs2_glock_be_greedy -
  1139. * @gl:
  1140. * @time:
  1141. *
  1142. * Returns: 0 if go_greedy will be called, 1 otherwise
  1143. */
  1144. int gfs2_glock_be_greedy(struct gfs2_glock *gl, unsigned int time)
  1145. {
  1146. struct greedy *gr;
  1147. struct gfs2_holder *gh;
  1148. if (!time || gl->gl_sbd->sd_args.ar_localcaching ||
  1149. test_and_set_bit(GLF_GREEDY, &gl->gl_flags))
  1150. return 1;
  1151. gr = kmalloc(sizeof(struct greedy), GFP_KERNEL);
  1152. if (!gr) {
  1153. clear_bit(GLF_GREEDY, &gl->gl_flags);
  1154. return 1;
  1155. }
  1156. gh = &gr->gr_gh;
  1157. gfs2_holder_init(gl, 0, 0, gh);
  1158. set_bit(HIF_GREEDY, &gh->gh_iflags);
  1159. INIT_DELAYED_WORK(&gr->gr_work, greedy_work);
  1160. set_bit(GLF_SKIP_WAITERS2, &gl->gl_flags);
  1161. schedule_delayed_work(&gr->gr_work, time);
  1162. return 0;
  1163. }
  1164. /**
  1165. * gfs2_glock_dq_uninit - dequeue a holder from a glock and initialize it
  1166. * @gh: the holder structure
  1167. *
  1168. */
  1169. void gfs2_glock_dq_uninit(struct gfs2_holder *gh)
  1170. {
  1171. gfs2_glock_dq(gh);
  1172. gfs2_holder_uninit(gh);
  1173. }
  1174. /**
  1175. * gfs2_glock_nq_num - acquire a glock based on lock number
  1176. * @sdp: the filesystem
  1177. * @number: the lock number
  1178. * @glops: the glock operations for the type of glock
  1179. * @state: the state to acquire the glock in
  1180. * @flags: modifier flags for the aquisition
  1181. * @gh: the struct gfs2_holder
  1182. *
  1183. * Returns: errno
  1184. */
  1185. int gfs2_glock_nq_num(struct gfs2_sbd *sdp, u64 number,
  1186. const struct gfs2_glock_operations *glops,
  1187. unsigned int state, int flags, struct gfs2_holder *gh)
  1188. {
  1189. struct gfs2_glock *gl;
  1190. int error;
  1191. error = gfs2_glock_get(sdp, number, glops, CREATE, &gl);
  1192. if (!error) {
  1193. error = gfs2_glock_nq_init(gl, state, flags, gh);
  1194. gfs2_glock_put(gl);
  1195. }
  1196. return error;
  1197. }
  1198. /**
  1199. * glock_compare - Compare two struct gfs2_glock structures for sorting
  1200. * @arg_a: the first structure
  1201. * @arg_b: the second structure
  1202. *
  1203. */
  1204. static int glock_compare(const void *arg_a, const void *arg_b)
  1205. {
  1206. const struct gfs2_holder *gh_a = *(const struct gfs2_holder **)arg_a;
  1207. const struct gfs2_holder *gh_b = *(const struct gfs2_holder **)arg_b;
  1208. const struct lm_lockname *a = &gh_a->gh_gl->gl_name;
  1209. const struct lm_lockname *b = &gh_b->gh_gl->gl_name;
  1210. if (a->ln_number > b->ln_number)
  1211. return 1;
  1212. if (a->ln_number < b->ln_number)
  1213. return -1;
  1214. if (gh_a->gh_state == LM_ST_SHARED && gh_b->gh_state == LM_ST_EXCLUSIVE)
  1215. return 1;
  1216. if (!(gh_a->gh_flags & GL_LOCAL_EXCL) && (gh_b->gh_flags & GL_LOCAL_EXCL))
  1217. return 1;
  1218. return 0;
  1219. }
  1220. /**
  1221. * nq_m_sync - synchonously acquire more than one glock in deadlock free order
  1222. * @num_gh: the number of structures
  1223. * @ghs: an array of struct gfs2_holder structures
  1224. *
  1225. * Returns: 0 on success (all glocks acquired),
  1226. * errno on failure (no glocks acquired)
  1227. */
  1228. static int nq_m_sync(unsigned int num_gh, struct gfs2_holder *ghs,
  1229. struct gfs2_holder **p)
  1230. {
  1231. unsigned int x;
  1232. int error = 0;
  1233. for (x = 0; x < num_gh; x++)
  1234. p[x] = &ghs[x];
  1235. sort(p, num_gh, sizeof(struct gfs2_holder *), glock_compare, NULL);
  1236. for (x = 0; x < num_gh; x++) {
  1237. p[x]->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
  1238. error = gfs2_glock_nq(p[x]);
  1239. if (error) {
  1240. while (x--)
  1241. gfs2_glock_dq(p[x]);
  1242. break;
  1243. }
  1244. }
  1245. return error;
  1246. }
  1247. /**
  1248. * gfs2_glock_nq_m - acquire multiple glocks
  1249. * @num_gh: the number of structures
  1250. * @ghs: an array of struct gfs2_holder structures
  1251. *
  1252. * Figure out how big an impact this function has. Either:
  1253. * 1) Replace this code with code that calls gfs2_glock_prefetch()
  1254. * 2) Forget async stuff and just call nq_m_sync()
  1255. * 3) Leave it like it is
  1256. *
  1257. * Returns: 0 on success (all glocks acquired),
  1258. * errno on failure (no glocks acquired)
  1259. */
  1260. int gfs2_glock_nq_m(unsigned int num_gh, struct gfs2_holder *ghs)
  1261. {
  1262. int *e;
  1263. unsigned int x;
  1264. int borked = 0, serious = 0;
  1265. int error = 0;
  1266. if (!num_gh)
  1267. return 0;
  1268. if (num_gh == 1) {
  1269. ghs->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
  1270. return gfs2_glock_nq(ghs);
  1271. }
  1272. e = kcalloc(num_gh, sizeof(struct gfs2_holder *), GFP_KERNEL);
  1273. if (!e)
  1274. return -ENOMEM;
  1275. for (x = 0; x < num_gh; x++) {
  1276. ghs[x].gh_flags |= LM_FLAG_TRY | GL_ASYNC;
  1277. error = gfs2_glock_nq(&ghs[x]);
  1278. if (error) {
  1279. borked = 1;
  1280. serious = error;
  1281. num_gh = x;
  1282. break;
  1283. }
  1284. }
  1285. for (x = 0; x < num_gh; x++) {
  1286. error = e[x] = glock_wait_internal(&ghs[x]);
  1287. if (error) {
  1288. borked = 1;
  1289. if (error != GLR_TRYFAILED && error != GLR_CANCELED)
  1290. serious = error;
  1291. }
  1292. }
  1293. if (!borked) {
  1294. kfree(e);
  1295. return 0;
  1296. }
  1297. for (x = 0; x < num_gh; x++)
  1298. if (!e[x])
  1299. gfs2_glock_dq(&ghs[x]);
  1300. if (serious)
  1301. error = serious;
  1302. else {
  1303. for (x = 0; x < num_gh; x++)
  1304. gfs2_holder_reinit(ghs[x].gh_state, ghs[x].gh_flags,
  1305. &ghs[x]);
  1306. error = nq_m_sync(num_gh, ghs, (struct gfs2_holder **)e);
  1307. }
  1308. kfree(e);
  1309. return error;
  1310. }
  1311. /**
  1312. * gfs2_glock_dq_m - release multiple glocks
  1313. * @num_gh: the number of structures
  1314. * @ghs: an array of struct gfs2_holder structures
  1315. *
  1316. */
  1317. void gfs2_glock_dq_m(unsigned int num_gh, struct gfs2_holder *ghs)
  1318. {
  1319. unsigned int x;
  1320. for (x = 0; x < num_gh; x++)
  1321. gfs2_glock_dq(&ghs[x]);
  1322. }
  1323. /**
  1324. * gfs2_glock_dq_uninit_m - release multiple glocks
  1325. * @num_gh: the number of structures
  1326. * @ghs: an array of struct gfs2_holder structures
  1327. *
  1328. */
  1329. void gfs2_glock_dq_uninit_m(unsigned int num_gh, struct gfs2_holder *ghs)
  1330. {
  1331. unsigned int x;
  1332. for (x = 0; x < num_gh; x++)
  1333. gfs2_glock_dq_uninit(&ghs[x]);
  1334. }
  1335. /**
  1336. * gfs2_glock_prefetch_num - prefetch a glock based on lock number
  1337. * @sdp: the filesystem
  1338. * @number: the lock number
  1339. * @glops: the glock operations for the type of glock
  1340. * @state: the state to acquire the glock in
  1341. * @flags: modifier flags for the aquisition
  1342. *
  1343. * Returns: errno
  1344. */
  1345. void gfs2_glock_prefetch_num(struct gfs2_sbd *sdp, u64 number,
  1346. const struct gfs2_glock_operations *glops,
  1347. unsigned int state, int flags)
  1348. {
  1349. struct gfs2_glock *gl;
  1350. int error;
  1351. if (atomic_read(&sdp->sd_reclaim_count) <
  1352. gfs2_tune_get(sdp, gt_reclaim_limit)) {
  1353. error = gfs2_glock_get(sdp, number, glops, CREATE, &gl);
  1354. if (!error) {
  1355. gfs2_glock_prefetch(gl, state, flags);
  1356. gfs2_glock_put(gl);
  1357. }
  1358. }
  1359. }
  1360. /**
  1361. * gfs2_lvb_hold - attach a LVB from a glock
  1362. * @gl: The glock in question
  1363. *
  1364. */
  1365. int gfs2_lvb_hold(struct gfs2_glock *gl)
  1366. {
  1367. int error;
  1368. gfs2_glmutex_lock(gl);
  1369. if (!atomic_read(&gl->gl_lvb_count)) {
  1370. error = gfs2_lm_hold_lvb(gl->gl_sbd, gl->gl_lock, &gl->gl_lvb);
  1371. if (error) {
  1372. gfs2_glmutex_unlock(gl);
  1373. return error;
  1374. }
  1375. gfs2_glock_hold(gl);
  1376. }
  1377. atomic_inc(&gl->gl_lvb_count);
  1378. gfs2_glmutex_unlock(gl);
  1379. return 0;
  1380. }
  1381. /**
  1382. * gfs2_lvb_unhold - detach a LVB from a glock
  1383. * @gl: The glock in question
  1384. *
  1385. */
  1386. void gfs2_lvb_unhold(struct gfs2_glock *gl)
  1387. {
  1388. gfs2_glock_hold(gl);
  1389. gfs2_glmutex_lock(gl);
  1390. gfs2_assert(gl->gl_sbd, atomic_read(&gl->gl_lvb_count) > 0);
  1391. if (atomic_dec_and_test(&gl->gl_lvb_count)) {
  1392. gfs2_lm_unhold_lvb(gl->gl_sbd, gl->gl_lock, gl->gl_lvb);
  1393. gl->gl_lvb = NULL;
  1394. gfs2_glock_put(gl);
  1395. }
  1396. gfs2_glmutex_unlock(gl);
  1397. gfs2_glock_put(gl);
  1398. }
  1399. static void blocking_cb(struct gfs2_sbd *sdp, struct lm_lockname *name,
  1400. unsigned int state)
  1401. {
  1402. struct gfs2_glock *gl;
  1403. gl = gfs2_glock_find(sdp, name);
  1404. if (!gl)
  1405. return;
  1406. if (gl->gl_ops->go_callback)
  1407. gl->gl_ops->go_callback(gl, state);
  1408. handle_callback(gl, state);
  1409. spin_lock(&gl->gl_spin);
  1410. run_queue(gl);
  1411. spin_unlock(&gl->gl_spin);
  1412. gfs2_glock_put(gl);
  1413. }
  1414. /**
  1415. * gfs2_glock_cb - Callback used by locking module
  1416. * @sdp: Pointer to the superblock
  1417. * @type: Type of callback
  1418. * @data: Type dependent data pointer
  1419. *
  1420. * Called by the locking module when it wants to tell us something.
  1421. * Either we need to drop a lock, one of our ASYNC requests completed, or
  1422. * a journal from another client needs to be recovered.
  1423. */
  1424. void gfs2_glock_cb(void *cb_data, unsigned int type, void *data)
  1425. {
  1426. struct gfs2_sbd *sdp = cb_data;
  1427. switch (type) {
  1428. case LM_CB_NEED_E:
  1429. blocking_cb(sdp, data, LM_ST_UNLOCKED);
  1430. return;
  1431. case LM_CB_NEED_D:
  1432. blocking_cb(sdp, data, LM_ST_DEFERRED);
  1433. return;
  1434. case LM_CB_NEED_S:
  1435. blocking_cb(sdp, data, LM_ST_SHARED);
  1436. return;
  1437. case LM_CB_ASYNC: {
  1438. struct lm_async_cb *async = data;
  1439. struct gfs2_glock *gl;
  1440. gl = gfs2_glock_find(sdp, &async->lc_name);
  1441. if (gfs2_assert_warn(sdp, gl))
  1442. return;
  1443. if (!gfs2_assert_warn(sdp, gl->gl_req_bh))
  1444. gl->gl_req_bh(gl, async->lc_ret);
  1445. gfs2_glock_put(gl);
  1446. return;
  1447. }
  1448. case LM_CB_NEED_RECOVERY:
  1449. gfs2_jdesc_make_dirty(sdp, *(unsigned int *)data);
  1450. if (sdp->sd_recoverd_process)
  1451. wake_up_process(sdp->sd_recoverd_process);
  1452. return;
  1453. case LM_CB_DROPLOCKS:
  1454. gfs2_gl_hash_clear(sdp, NO_WAIT);
  1455. gfs2_quota_scan(sdp);
  1456. return;
  1457. default:
  1458. gfs2_assert_warn(sdp, 0);
  1459. return;
  1460. }
  1461. }
  1462. /**
  1463. * demote_ok - Check to see if it's ok to unlock a glock
  1464. * @gl: the glock
  1465. *
  1466. * Returns: 1 if it's ok
  1467. */
  1468. static int demote_ok(struct gfs2_glock *gl)
  1469. {
  1470. struct gfs2_sbd *sdp = gl->gl_sbd;
  1471. const struct gfs2_glock_operations *glops = gl->gl_ops;
  1472. int demote = 1;
  1473. if (test_bit(GLF_STICKY, &gl->gl_flags))
  1474. demote = 0;
  1475. else if (test_bit(GLF_PREFETCH, &gl->gl_flags))
  1476. demote = time_after_eq(jiffies, gl->gl_stamp +
  1477. gfs2_tune_get(sdp, gt_prefetch_secs) * HZ);
  1478. else if (glops->go_demote_ok)
  1479. demote = glops->go_demote_ok(gl);
  1480. return demote;
  1481. }
  1482. /**
  1483. * gfs2_glock_schedule_for_reclaim - Add a glock to the reclaim list
  1484. * @gl: the glock
  1485. *
  1486. */
  1487. void gfs2_glock_schedule_for_reclaim(struct gfs2_glock *gl)
  1488. {
  1489. struct gfs2_sbd *sdp = gl->gl_sbd;
  1490. spin_lock(&sdp->sd_reclaim_lock);
  1491. if (list_empty(&gl->gl_reclaim)) {
  1492. gfs2_glock_hold(gl);
  1493. list_add(&gl->gl_reclaim, &sdp->sd_reclaim_list);
  1494. atomic_inc(&sdp->sd_reclaim_count);
  1495. }
  1496. spin_unlock(&sdp->sd_reclaim_lock);
  1497. wake_up(&sdp->sd_reclaim_wq);
  1498. }
  1499. /**
  1500. * gfs2_reclaim_glock - process the next glock on the filesystem's reclaim list
  1501. * @sdp: the filesystem
  1502. *
  1503. * Called from gfs2_glockd() glock reclaim daemon, or when promoting a
  1504. * different glock and we notice that there are a lot of glocks in the
  1505. * reclaim list.
  1506. *
  1507. */
  1508. void gfs2_reclaim_glock(struct gfs2_sbd *sdp)
  1509. {
  1510. struct gfs2_glock *gl;
  1511. spin_lock(&sdp->sd_reclaim_lock);
  1512. if (list_empty(&sdp->sd_reclaim_list)) {
  1513. spin_unlock(&sdp->sd_reclaim_lock);
  1514. return;
  1515. }
  1516. gl = list_entry(sdp->sd_reclaim_list.next,
  1517. struct gfs2_glock, gl_reclaim);
  1518. list_del_init(&gl->gl_reclaim);
  1519. spin_unlock(&sdp->sd_reclaim_lock);
  1520. atomic_dec(&sdp->sd_reclaim_count);
  1521. atomic_inc(&sdp->sd_reclaimed);
  1522. if (gfs2_glmutex_trylock(gl)) {
  1523. if (queue_empty(gl, &gl->gl_holders) &&
  1524. gl->gl_state != LM_ST_UNLOCKED && demote_ok(gl))
  1525. handle_callback(gl, LM_ST_UNLOCKED);
  1526. gfs2_glmutex_unlock(gl);
  1527. }
  1528. gfs2_glock_put(gl);
  1529. }
  1530. /**
  1531. * examine_bucket - Call a function for glock in a hash bucket
  1532. * @examiner: the function
  1533. * @sdp: the filesystem
  1534. * @bucket: the bucket
  1535. *
  1536. * Returns: 1 if the bucket has entries
  1537. */
  1538. static int examine_bucket(glock_examiner examiner, struct gfs2_sbd *sdp,
  1539. unsigned int hash)
  1540. {
  1541. struct gfs2_glock *gl, *prev = NULL;
  1542. int has_entries = 0;
  1543. struct hlist_head *head = &gl_hash_table[hash].hb_list;
  1544. read_lock(gl_lock_addr(hash));
  1545. /* Can't use hlist_for_each_entry - don't want prefetch here */
  1546. if (hlist_empty(head))
  1547. goto out;
  1548. gl = list_entry(head->first, struct gfs2_glock, gl_list);
  1549. while(1) {
  1550. if (gl->gl_sbd == sdp) {
  1551. gfs2_glock_hold(gl);
  1552. read_unlock(gl_lock_addr(hash));
  1553. if (prev)
  1554. gfs2_glock_put(prev);
  1555. prev = gl;
  1556. examiner(gl);
  1557. has_entries = 1;
  1558. read_lock(gl_lock_addr(hash));
  1559. }
  1560. if (gl->gl_list.next == NULL)
  1561. break;
  1562. gl = list_entry(gl->gl_list.next, struct gfs2_glock, gl_list);
  1563. }
  1564. out:
  1565. read_unlock(gl_lock_addr(hash));
  1566. if (prev)
  1567. gfs2_glock_put(prev);
  1568. return has_entries;
  1569. }
  1570. /**
  1571. * scan_glock - look at a glock and see if we can reclaim it
  1572. * @gl: the glock to look at
  1573. *
  1574. */
  1575. static void scan_glock(struct gfs2_glock *gl)
  1576. {
  1577. if (gl->gl_ops == &gfs2_inode_glops && gl->gl_object)
  1578. return;
  1579. if (gfs2_glmutex_trylock(gl)) {
  1580. if (queue_empty(gl, &gl->gl_holders) &&
  1581. gl->gl_state != LM_ST_UNLOCKED && demote_ok(gl))
  1582. goto out_schedule;
  1583. gfs2_glmutex_unlock(gl);
  1584. }
  1585. return;
  1586. out_schedule:
  1587. gfs2_glmutex_unlock(gl);
  1588. gfs2_glock_schedule_for_reclaim(gl);
  1589. }
  1590. /**
  1591. * gfs2_scand_internal - Look for glocks and inodes to toss from memory
  1592. * @sdp: the filesystem
  1593. *
  1594. */
  1595. void gfs2_scand_internal(struct gfs2_sbd *sdp)
  1596. {
  1597. unsigned int x;
  1598. for (x = 0; x < GFS2_GL_HASH_SIZE; x++)
  1599. examine_bucket(scan_glock, sdp, x);
  1600. }
  1601. /**
  1602. * clear_glock - look at a glock and see if we can free it from glock cache
  1603. * @gl: the glock to look at
  1604. *
  1605. */
  1606. static void clear_glock(struct gfs2_glock *gl)
  1607. {
  1608. struct gfs2_sbd *sdp = gl->gl_sbd;
  1609. int released;
  1610. spin_lock(&sdp->sd_reclaim_lock);
  1611. if (!list_empty(&gl->gl_reclaim)) {
  1612. list_del_init(&gl->gl_reclaim);
  1613. atomic_dec(&sdp->sd_reclaim_count);
  1614. spin_unlock(&sdp->sd_reclaim_lock);
  1615. released = gfs2_glock_put(gl);
  1616. gfs2_assert(sdp, !released);
  1617. } else {
  1618. spin_unlock(&sdp->sd_reclaim_lock);
  1619. }
  1620. if (gfs2_glmutex_trylock(gl)) {
  1621. if (queue_empty(gl, &gl->gl_holders) &&
  1622. gl->gl_state != LM_ST_UNLOCKED)
  1623. handle_callback(gl, LM_ST_UNLOCKED);
  1624. gfs2_glmutex_unlock(gl);
  1625. }
  1626. }
  1627. /**
  1628. * gfs2_gl_hash_clear - Empty out the glock hash table
  1629. * @sdp: the filesystem
  1630. * @wait: wait until it's all gone
  1631. *
  1632. * Called when unmounting the filesystem, or when inter-node lock manager
  1633. * requests DROPLOCKS because it is running out of capacity.
  1634. */
  1635. void gfs2_gl_hash_clear(struct gfs2_sbd *sdp, int wait)
  1636. {
  1637. unsigned long t;
  1638. unsigned int x;
  1639. int cont;
  1640. t = jiffies;
  1641. for (;;) {
  1642. cont = 0;
  1643. for (x = 0; x < GFS2_GL_HASH_SIZE; x++) {
  1644. if (examine_bucket(clear_glock, sdp, x))
  1645. cont = 1;
  1646. }
  1647. if (!wait || !cont)
  1648. break;
  1649. if (time_after_eq(jiffies,
  1650. t + gfs2_tune_get(sdp, gt_stall_secs) * HZ)) {
  1651. fs_warn(sdp, "Unmount seems to be stalled. "
  1652. "Dumping lock state...\n");
  1653. gfs2_dump_lockstate(sdp);
  1654. t = jiffies;
  1655. }
  1656. invalidate_inodes(sdp->sd_vfs);
  1657. msleep(10);
  1658. }
  1659. }
  1660. /*
  1661. * Diagnostic routines to help debug distributed deadlock
  1662. */
  1663. /**
  1664. * dump_holder - print information about a glock holder
  1665. * @str: a string naming the type of holder
  1666. * @gh: the glock holder
  1667. *
  1668. * Returns: 0 on success, -ENOBUFS when we run out of space
  1669. */
  1670. static int dump_holder(char *str, struct gfs2_holder *gh)
  1671. {
  1672. unsigned int x;
  1673. int error = -ENOBUFS;
  1674. printk(KERN_INFO " %s\n", str);
  1675. printk(KERN_INFO " owner = %ld\n",
  1676. (gh->gh_owner) ? (long)gh->gh_owner->pid : -1);
  1677. printk(KERN_INFO " gh_state = %u\n", gh->gh_state);
  1678. printk(KERN_INFO " gh_flags =");
  1679. for (x = 0; x < 32; x++)
  1680. if (gh->gh_flags & (1 << x))
  1681. printk(" %u", x);
  1682. printk(" \n");
  1683. printk(KERN_INFO " error = %d\n", gh->gh_error);
  1684. printk(KERN_INFO " gh_iflags =");
  1685. for (x = 0; x < 32; x++)
  1686. if (test_bit(x, &gh->gh_iflags))
  1687. printk(" %u", x);
  1688. printk(" \n");
  1689. print_symbol(KERN_INFO " initialized at: %s\n", gh->gh_ip);
  1690. error = 0;
  1691. return error;
  1692. }
  1693. /**
  1694. * dump_inode - print information about an inode
  1695. * @ip: the inode
  1696. *
  1697. * Returns: 0 on success, -ENOBUFS when we run out of space
  1698. */
  1699. static int dump_inode(struct gfs2_inode *ip)
  1700. {
  1701. unsigned int x;
  1702. int error = -ENOBUFS;
  1703. printk(KERN_INFO " Inode:\n");
  1704. printk(KERN_INFO " num = %llu %llu\n",
  1705. (unsigned long long)ip->i_num.no_formal_ino,
  1706. (unsigned long long)ip->i_num.no_addr);
  1707. printk(KERN_INFO " type = %u\n", IF2DT(ip->i_inode.i_mode));
  1708. printk(KERN_INFO " i_flags =");
  1709. for (x = 0; x < 32; x++)
  1710. if (test_bit(x, &ip->i_flags))
  1711. printk(" %u", x);
  1712. printk(" \n");
  1713. error = 0;
  1714. return error;
  1715. }
  1716. /**
  1717. * dump_glock - print information about a glock
  1718. * @gl: the glock
  1719. * @count: where we are in the buffer
  1720. *
  1721. * Returns: 0 on success, -ENOBUFS when we run out of space
  1722. */
  1723. static int dump_glock(struct gfs2_glock *gl)
  1724. {
  1725. struct gfs2_holder *gh;
  1726. unsigned int x;
  1727. int error = -ENOBUFS;
  1728. spin_lock(&gl->gl_spin);
  1729. printk(KERN_INFO "Glock 0x%p (%u, %llu)\n", gl, gl->gl_name.ln_type,
  1730. (unsigned long long)gl->gl_name.ln_number);
  1731. printk(KERN_INFO " gl_flags =");
  1732. for (x = 0; x < 32; x++) {
  1733. if (test_bit(x, &gl->gl_flags))
  1734. printk(" %u", x);
  1735. }
  1736. printk(" \n");
  1737. printk(KERN_INFO " gl_ref = %d\n", atomic_read(&gl->gl_ref));
  1738. printk(KERN_INFO " gl_state = %u\n", gl->gl_state);
  1739. printk(KERN_INFO " gl_owner = %s\n", gl->gl_owner->comm);
  1740. print_symbol(KERN_INFO " gl_ip = %s\n", gl->gl_ip);
  1741. printk(KERN_INFO " req_gh = %s\n", (gl->gl_req_gh) ? "yes" : "no");
  1742. printk(KERN_INFO " req_bh = %s\n", (gl->gl_req_bh) ? "yes" : "no");
  1743. printk(KERN_INFO " lvb_count = %d\n", atomic_read(&gl->gl_lvb_count));
  1744. printk(KERN_INFO " object = %s\n", (gl->gl_object) ? "yes" : "no");
  1745. printk(KERN_INFO " le = %s\n",
  1746. (list_empty(&gl->gl_le.le_list)) ? "no" : "yes");
  1747. printk(KERN_INFO " reclaim = %s\n",
  1748. (list_empty(&gl->gl_reclaim)) ? "no" : "yes");
  1749. if (gl->gl_aspace)
  1750. printk(KERN_INFO " aspace = 0x%p nrpages = %lu\n", gl->gl_aspace,
  1751. gl->gl_aspace->i_mapping->nrpages);
  1752. else
  1753. printk(KERN_INFO " aspace = no\n");
  1754. printk(KERN_INFO " ail = %d\n", atomic_read(&gl->gl_ail_count));
  1755. if (gl->gl_req_gh) {
  1756. error = dump_holder("Request", gl->gl_req_gh);
  1757. if (error)
  1758. goto out;
  1759. }
  1760. list_for_each_entry(gh, &gl->gl_holders, gh_list) {
  1761. error = dump_holder("Holder", gh);
  1762. if (error)
  1763. goto out;
  1764. }
  1765. list_for_each_entry(gh, &gl->gl_waiters1, gh_list) {
  1766. error = dump_holder("Waiter1", gh);
  1767. if (error)
  1768. goto out;
  1769. }
  1770. list_for_each_entry(gh, &gl->gl_waiters2, gh_list) {
  1771. error = dump_holder("Waiter2", gh);
  1772. if (error)
  1773. goto out;
  1774. }
  1775. list_for_each_entry(gh, &gl->gl_waiters3, gh_list) {
  1776. error = dump_holder("Waiter3", gh);
  1777. if (error)
  1778. goto out;
  1779. }
  1780. if (gl->gl_ops == &gfs2_inode_glops && gl->gl_object) {
  1781. if (!test_bit(GLF_LOCK, &gl->gl_flags) &&
  1782. list_empty(&gl->gl_holders)) {
  1783. error = dump_inode(gl->gl_object);
  1784. if (error)
  1785. goto out;
  1786. } else {
  1787. error = -ENOBUFS;
  1788. printk(KERN_INFO " Inode: busy\n");
  1789. }
  1790. }
  1791. error = 0;
  1792. out:
  1793. spin_unlock(&gl->gl_spin);
  1794. return error;
  1795. }
  1796. /**
  1797. * gfs2_dump_lockstate - print out the current lockstate
  1798. * @sdp: the filesystem
  1799. * @ub: the buffer to copy the information into
  1800. *
  1801. * If @ub is NULL, dump the lockstate to the console.
  1802. *
  1803. */
  1804. static int gfs2_dump_lockstate(struct gfs2_sbd *sdp)
  1805. {
  1806. struct gfs2_glock *gl;
  1807. struct hlist_node *h;
  1808. unsigned int x;
  1809. int error = 0;
  1810. for (x = 0; x < GFS2_GL_HASH_SIZE; x++) {
  1811. read_lock(gl_lock_addr(x));
  1812. hlist_for_each_entry(gl, h, &gl_hash_table[x].hb_list, gl_list) {
  1813. if (gl->gl_sbd != sdp)
  1814. continue;
  1815. error = dump_glock(gl);
  1816. if (error)
  1817. break;
  1818. }
  1819. read_unlock(gl_lock_addr(x));
  1820. if (error)
  1821. break;
  1822. }
  1823. return error;
  1824. }
  1825. int __init gfs2_glock_init(void)
  1826. {
  1827. unsigned i;
  1828. for(i = 0; i < GFS2_GL_HASH_SIZE; i++) {
  1829. INIT_HLIST_HEAD(&gl_hash_table[i].hb_list);
  1830. }
  1831. #ifdef GL_HASH_LOCK_SZ
  1832. for(i = 0; i < GL_HASH_LOCK_SZ; i++) {
  1833. rwlock_init(&gl_hash_locks[i]);
  1834. }
  1835. #endif
  1836. return 0;
  1837. }