glock.c 54 KB

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