glock.c 54 KB

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