glock.c 50 KB

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