glock.c 54 KB

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