glock.c 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
  4. *
  5. * This copyrighted material is made available to anyone wishing to use,
  6. * modify, copy, or redistribute it subject to the terms and conditions
  7. * of the GNU General Public License version 2.
  8. */
  9. #include <linux/sched.h>
  10. #include <linux/slab.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/buffer_head.h>
  13. #include <linux/delay.h>
  14. #include <linux/sort.h>
  15. #include <linux/jhash.h>
  16. #include <linux/kallsyms.h>
  17. #include <linux/gfs2_ondisk.h>
  18. #include <linux/list.h>
  19. #include <linux/wait.h>
  20. #include <linux/module.h>
  21. #include <asm/uaccess.h>
  22. #include <linux/seq_file.h>
  23. #include <linux/debugfs.h>
  24. #include <linux/kthread.h>
  25. #include <linux/freezer.h>
  26. #include <linux/workqueue.h>
  27. #include <linux/jiffies.h>
  28. #include <linux/rcupdate.h>
  29. #include <linux/rculist_bl.h>
  30. #include <linux/bit_spinlock.h>
  31. #include <linux/percpu.h>
  32. #include "gfs2.h"
  33. #include "incore.h"
  34. #include "glock.h"
  35. #include "glops.h"
  36. #include "inode.h"
  37. #include "lops.h"
  38. #include "meta_io.h"
  39. #include "quota.h"
  40. #include "super.h"
  41. #include "util.h"
  42. #include "bmap.h"
  43. #define CREATE_TRACE_POINTS
  44. #include "trace_gfs2.h"
  45. struct gfs2_glock_iter {
  46. int hash; /* hash bucket index */
  47. struct gfs2_sbd *sdp; /* incore superblock */
  48. struct gfs2_glock *gl; /* current glock struct */
  49. char string[512]; /* scratch space */
  50. };
  51. typedef void (*glock_examiner) (struct gfs2_glock * gl);
  52. static int __dump_glock(struct seq_file *seq, const struct gfs2_glock *gl);
  53. #define GLOCK_BUG_ON(gl,x) do { if (unlikely(x)) { __dump_glock(NULL, gl); BUG(); } } while(0)
  54. static void do_xmote(struct gfs2_glock *gl, struct gfs2_holder *gh, unsigned int target);
  55. static struct dentry *gfs2_root;
  56. static struct workqueue_struct *glock_workqueue;
  57. struct workqueue_struct *gfs2_delete_workqueue;
  58. static LIST_HEAD(lru_list);
  59. static atomic_t lru_count = ATOMIC_INIT(0);
  60. static DEFINE_SPINLOCK(lru_lock);
  61. #define GFS2_GL_HASH_SHIFT 15
  62. #define GFS2_GL_HASH_SIZE (1 << GFS2_GL_HASH_SHIFT)
  63. #define GFS2_GL_HASH_MASK (GFS2_GL_HASH_SIZE - 1)
  64. static struct hlist_bl_head gl_hash_table[GFS2_GL_HASH_SIZE];
  65. static struct dentry *gfs2_root;
  66. /**
  67. * gl_hash() - Turn glock number into hash bucket number
  68. * @lock: The glock number
  69. *
  70. * Returns: The number of the corresponding hash bucket
  71. */
  72. static unsigned int gl_hash(const struct gfs2_sbd *sdp,
  73. const struct lm_lockname *name)
  74. {
  75. unsigned int h;
  76. h = jhash(&name->ln_number, sizeof(u64), 0);
  77. h = jhash(&name->ln_type, sizeof(unsigned int), h);
  78. h = jhash(&sdp, sizeof(struct gfs2_sbd *), h);
  79. h &= GFS2_GL_HASH_MASK;
  80. return h;
  81. }
  82. static inline void spin_lock_bucket(unsigned int hash)
  83. {
  84. hlist_bl_lock(&gl_hash_table[hash]);
  85. }
  86. static inline void spin_unlock_bucket(unsigned int hash)
  87. {
  88. hlist_bl_unlock(&gl_hash_table[hash]);
  89. }
  90. static void gfs2_glock_dealloc(struct rcu_head *rcu)
  91. {
  92. struct gfs2_glock *gl = container_of(rcu, struct gfs2_glock, gl_rcu);
  93. if (gl->gl_ops->go_flags & GLOF_ASPACE)
  94. kmem_cache_free(gfs2_glock_aspace_cachep, gl);
  95. else
  96. kmem_cache_free(gfs2_glock_cachep, gl);
  97. }
  98. void gfs2_glock_free(struct gfs2_glock *gl)
  99. {
  100. struct gfs2_sbd *sdp = gl->gl_sbd;
  101. call_rcu(&gl->gl_rcu, gfs2_glock_dealloc);
  102. if (atomic_dec_and_test(&sdp->sd_glock_disposal))
  103. wake_up(&sdp->sd_glock_wait);
  104. }
  105. /**
  106. * gfs2_glock_hold() - increment reference count on glock
  107. * @gl: The glock to hold
  108. *
  109. */
  110. void gfs2_glock_hold(struct gfs2_glock *gl)
  111. {
  112. GLOCK_BUG_ON(gl, atomic_read(&gl->gl_ref) == 0);
  113. atomic_inc(&gl->gl_ref);
  114. }
  115. /**
  116. * demote_ok - Check to see if it's ok to unlock a glock
  117. * @gl: the glock
  118. *
  119. * Returns: 1 if it's ok
  120. */
  121. static int demote_ok(const struct gfs2_glock *gl)
  122. {
  123. const struct gfs2_glock_operations *glops = gl->gl_ops;
  124. if (gl->gl_state == LM_ST_UNLOCKED)
  125. return 0;
  126. if (!list_empty(&gl->gl_holders))
  127. return 0;
  128. if (glops->go_demote_ok)
  129. return glops->go_demote_ok(gl);
  130. return 1;
  131. }
  132. void gfs2_glock_add_to_lru(struct gfs2_glock *gl)
  133. {
  134. spin_lock(&lru_lock);
  135. if (!list_empty(&gl->gl_lru))
  136. list_del_init(&gl->gl_lru);
  137. else
  138. atomic_inc(&lru_count);
  139. list_add_tail(&gl->gl_lru, &lru_list);
  140. set_bit(GLF_LRU, &gl->gl_flags);
  141. spin_unlock(&lru_lock);
  142. }
  143. static void __gfs2_glock_remove_from_lru(struct gfs2_glock *gl)
  144. {
  145. if (!list_empty(&gl->gl_lru)) {
  146. list_del_init(&gl->gl_lru);
  147. atomic_dec(&lru_count);
  148. clear_bit(GLF_LRU, &gl->gl_flags);
  149. }
  150. }
  151. static void gfs2_glock_remove_from_lru(struct gfs2_glock *gl)
  152. {
  153. spin_lock(&lru_lock);
  154. __gfs2_glock_remove_from_lru(gl);
  155. spin_unlock(&lru_lock);
  156. }
  157. /**
  158. * __gfs2_glock_schedule_for_reclaim - Add a glock to the reclaim list
  159. * @gl: the glock
  160. *
  161. * If the glock is demotable, then we add it (or move it) to the end
  162. * of the glock LRU list.
  163. */
  164. static void __gfs2_glock_schedule_for_reclaim(struct gfs2_glock *gl)
  165. {
  166. if (demote_ok(gl))
  167. gfs2_glock_add_to_lru(gl);
  168. }
  169. /**
  170. * gfs2_glock_put_nolock() - Decrement reference count on glock
  171. * @gl: The glock to put
  172. *
  173. * This function should only be used if the caller has its own reference
  174. * to the glock, in addition to the one it is dropping.
  175. */
  176. void gfs2_glock_put_nolock(struct gfs2_glock *gl)
  177. {
  178. if (atomic_dec_and_test(&gl->gl_ref))
  179. GLOCK_BUG_ON(gl, 1);
  180. }
  181. /**
  182. * gfs2_glock_put() - Decrement reference count on glock
  183. * @gl: The glock to put
  184. *
  185. */
  186. void gfs2_glock_put(struct gfs2_glock *gl)
  187. {
  188. struct gfs2_sbd *sdp = gl->gl_sbd;
  189. struct address_space *mapping = gfs2_glock2aspace(gl);
  190. if (atomic_dec_and_lock(&gl->gl_ref, &lru_lock)) {
  191. __gfs2_glock_remove_from_lru(gl);
  192. spin_unlock(&lru_lock);
  193. spin_lock_bucket(gl->gl_hash);
  194. hlist_bl_del_rcu(&gl->gl_list);
  195. spin_unlock_bucket(gl->gl_hash);
  196. GLOCK_BUG_ON(gl, !list_empty(&gl->gl_holders));
  197. GLOCK_BUG_ON(gl, mapping && mapping->nrpages);
  198. trace_gfs2_glock_put(gl);
  199. sdp->sd_lockstruct.ls_ops->lm_put_lock(gl);
  200. }
  201. }
  202. /**
  203. * search_bucket() - Find struct gfs2_glock by lock number
  204. * @bucket: the bucket to search
  205. * @name: The lock name
  206. *
  207. * Returns: NULL, or the struct gfs2_glock with the requested number
  208. */
  209. static struct gfs2_glock *search_bucket(unsigned int hash,
  210. const struct gfs2_sbd *sdp,
  211. const struct lm_lockname *name)
  212. {
  213. struct gfs2_glock *gl;
  214. struct hlist_bl_node *h;
  215. hlist_bl_for_each_entry_rcu(gl, h, &gl_hash_table[hash], gl_list) {
  216. if (!lm_name_equal(&gl->gl_name, name))
  217. continue;
  218. if (gl->gl_sbd != sdp)
  219. continue;
  220. if (atomic_inc_not_zero(&gl->gl_ref))
  221. return gl;
  222. }
  223. return NULL;
  224. }
  225. /**
  226. * may_grant - check if its ok to grant a new lock
  227. * @gl: The glock
  228. * @gh: The lock request which we wish to grant
  229. *
  230. * Returns: true if its ok to grant the lock
  231. */
  232. static inline int may_grant(const struct gfs2_glock *gl, const struct gfs2_holder *gh)
  233. {
  234. const struct gfs2_holder *gh_head = list_entry(gl->gl_holders.next, const struct gfs2_holder, gh_list);
  235. if ((gh->gh_state == LM_ST_EXCLUSIVE ||
  236. gh_head->gh_state == LM_ST_EXCLUSIVE) && gh != gh_head)
  237. return 0;
  238. if (gl->gl_state == gh->gh_state)
  239. return 1;
  240. if (gh->gh_flags & GL_EXACT)
  241. return 0;
  242. if (gl->gl_state == LM_ST_EXCLUSIVE) {
  243. if (gh->gh_state == LM_ST_SHARED && gh_head->gh_state == LM_ST_SHARED)
  244. return 1;
  245. if (gh->gh_state == LM_ST_DEFERRED && gh_head->gh_state == LM_ST_DEFERRED)
  246. return 1;
  247. }
  248. if (gl->gl_state != LM_ST_UNLOCKED && (gh->gh_flags & LM_FLAG_ANY))
  249. return 1;
  250. return 0;
  251. }
  252. static void gfs2_holder_wake(struct gfs2_holder *gh)
  253. {
  254. clear_bit(HIF_WAIT, &gh->gh_iflags);
  255. smp_mb__after_clear_bit();
  256. wake_up_bit(&gh->gh_iflags, HIF_WAIT);
  257. }
  258. /**
  259. * do_error - Something unexpected has happened during a lock request
  260. *
  261. */
  262. static inline void do_error(struct gfs2_glock *gl, const int ret)
  263. {
  264. struct gfs2_holder *gh, *tmp;
  265. list_for_each_entry_safe(gh, tmp, &gl->gl_holders, gh_list) {
  266. if (test_bit(HIF_HOLDER, &gh->gh_iflags))
  267. continue;
  268. if (ret & LM_OUT_ERROR)
  269. gh->gh_error = -EIO;
  270. else if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB))
  271. gh->gh_error = GLR_TRYFAILED;
  272. else
  273. continue;
  274. list_del_init(&gh->gh_list);
  275. trace_gfs2_glock_queue(gh, 0);
  276. gfs2_holder_wake(gh);
  277. }
  278. }
  279. /**
  280. * do_promote - promote as many requests as possible on the current queue
  281. * @gl: The glock
  282. *
  283. * Returns: 1 if there is a blocked holder at the head of the list, or 2
  284. * if a type specific operation is underway.
  285. */
  286. static int do_promote(struct gfs2_glock *gl)
  287. __releases(&gl->gl_spin)
  288. __acquires(&gl->gl_spin)
  289. {
  290. const struct gfs2_glock_operations *glops = gl->gl_ops;
  291. struct gfs2_holder *gh, *tmp;
  292. int ret;
  293. restart:
  294. list_for_each_entry_safe(gh, tmp, &gl->gl_holders, gh_list) {
  295. if (test_bit(HIF_HOLDER, &gh->gh_iflags))
  296. continue;
  297. if (may_grant(gl, gh)) {
  298. if (gh->gh_list.prev == &gl->gl_holders &&
  299. glops->go_lock) {
  300. spin_unlock(&gl->gl_spin);
  301. /* FIXME: eliminate this eventually */
  302. ret = glops->go_lock(gh);
  303. spin_lock(&gl->gl_spin);
  304. if (ret) {
  305. if (ret == 1)
  306. return 2;
  307. gh->gh_error = ret;
  308. list_del_init(&gh->gh_list);
  309. trace_gfs2_glock_queue(gh, 0);
  310. gfs2_holder_wake(gh);
  311. goto restart;
  312. }
  313. set_bit(HIF_HOLDER, &gh->gh_iflags);
  314. trace_gfs2_promote(gh, 1);
  315. gfs2_holder_wake(gh);
  316. goto restart;
  317. }
  318. set_bit(HIF_HOLDER, &gh->gh_iflags);
  319. trace_gfs2_promote(gh, 0);
  320. gfs2_holder_wake(gh);
  321. continue;
  322. }
  323. if (gh->gh_list.prev == &gl->gl_holders)
  324. return 1;
  325. do_error(gl, 0);
  326. break;
  327. }
  328. return 0;
  329. }
  330. /**
  331. * find_first_waiter - find the first gh that's waiting for the glock
  332. * @gl: the glock
  333. */
  334. static inline struct gfs2_holder *find_first_waiter(const struct gfs2_glock *gl)
  335. {
  336. struct gfs2_holder *gh;
  337. list_for_each_entry(gh, &gl->gl_holders, gh_list) {
  338. if (!test_bit(HIF_HOLDER, &gh->gh_iflags))
  339. return gh;
  340. }
  341. return NULL;
  342. }
  343. /**
  344. * state_change - record that the glock is now in a different state
  345. * @gl: the glock
  346. * @new_state the new state
  347. *
  348. */
  349. static void state_change(struct gfs2_glock *gl, unsigned int new_state)
  350. {
  351. int held1, held2;
  352. held1 = (gl->gl_state != LM_ST_UNLOCKED);
  353. held2 = (new_state != LM_ST_UNLOCKED);
  354. if (held1 != held2) {
  355. if (held2)
  356. gfs2_glock_hold(gl);
  357. else
  358. gfs2_glock_put_nolock(gl);
  359. }
  360. if (held1 && held2 && list_empty(&gl->gl_holders))
  361. clear_bit(GLF_QUEUED, &gl->gl_flags);
  362. if (new_state != gl->gl_target)
  363. /* shorten our minimum hold time */
  364. gl->gl_hold_time = max(gl->gl_hold_time - GL_GLOCK_HOLD_DECR,
  365. GL_GLOCK_MIN_HOLD);
  366. gl->gl_state = new_state;
  367. gl->gl_tchange = jiffies;
  368. }
  369. static void gfs2_demote_wake(struct gfs2_glock *gl)
  370. {
  371. gl->gl_demote_state = LM_ST_EXCLUSIVE;
  372. clear_bit(GLF_DEMOTE, &gl->gl_flags);
  373. smp_mb__after_clear_bit();
  374. wake_up_bit(&gl->gl_flags, GLF_DEMOTE);
  375. }
  376. /**
  377. * finish_xmote - The DLM has replied to one of our lock requests
  378. * @gl: The glock
  379. * @ret: The status from the DLM
  380. *
  381. */
  382. static void finish_xmote(struct gfs2_glock *gl, unsigned int ret)
  383. {
  384. const struct gfs2_glock_operations *glops = gl->gl_ops;
  385. struct gfs2_holder *gh;
  386. unsigned state = ret & LM_OUT_ST_MASK;
  387. int rv;
  388. spin_lock(&gl->gl_spin);
  389. trace_gfs2_glock_state_change(gl, state);
  390. state_change(gl, state);
  391. gh = find_first_waiter(gl);
  392. /* Demote to UN request arrived during demote to SH or DF */
  393. if (test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags) &&
  394. state != LM_ST_UNLOCKED && gl->gl_demote_state == LM_ST_UNLOCKED)
  395. gl->gl_target = LM_ST_UNLOCKED;
  396. /* Check for state != intended state */
  397. if (unlikely(state != gl->gl_target)) {
  398. if (gh && !test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags)) {
  399. /* move to back of queue and try next entry */
  400. if (ret & LM_OUT_CANCELED) {
  401. if ((gh->gh_flags & LM_FLAG_PRIORITY) == 0)
  402. list_move_tail(&gh->gh_list, &gl->gl_holders);
  403. gh = find_first_waiter(gl);
  404. gl->gl_target = gh->gh_state;
  405. goto retry;
  406. }
  407. /* Some error or failed "try lock" - report it */
  408. if ((ret & LM_OUT_ERROR) ||
  409. (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB))) {
  410. gl->gl_target = gl->gl_state;
  411. do_error(gl, ret);
  412. goto out;
  413. }
  414. }
  415. switch(state) {
  416. /* Unlocked due to conversion deadlock, try again */
  417. case LM_ST_UNLOCKED:
  418. retry:
  419. do_xmote(gl, gh, gl->gl_target);
  420. break;
  421. /* Conversion fails, unlock and try again */
  422. case LM_ST_SHARED:
  423. case LM_ST_DEFERRED:
  424. do_xmote(gl, gh, LM_ST_UNLOCKED);
  425. break;
  426. default: /* Everything else */
  427. printk(KERN_ERR "GFS2: wanted %u got %u\n", gl->gl_target, state);
  428. GLOCK_BUG_ON(gl, 1);
  429. }
  430. spin_unlock(&gl->gl_spin);
  431. return;
  432. }
  433. /* Fast path - we got what we asked for */
  434. if (test_and_clear_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags))
  435. gfs2_demote_wake(gl);
  436. if (state != LM_ST_UNLOCKED) {
  437. if (glops->go_xmote_bh) {
  438. spin_unlock(&gl->gl_spin);
  439. rv = glops->go_xmote_bh(gl, gh);
  440. spin_lock(&gl->gl_spin);
  441. if (rv) {
  442. do_error(gl, rv);
  443. goto out;
  444. }
  445. }
  446. rv = do_promote(gl);
  447. if (rv == 2)
  448. goto out_locked;
  449. }
  450. out:
  451. clear_bit(GLF_LOCK, &gl->gl_flags);
  452. out_locked:
  453. spin_unlock(&gl->gl_spin);
  454. }
  455. /**
  456. * do_xmote - Calls the DLM to change the state of a lock
  457. * @gl: The lock state
  458. * @gh: The holder (only for promotes)
  459. * @target: The target lock state
  460. *
  461. */
  462. static void do_xmote(struct gfs2_glock *gl, struct gfs2_holder *gh, unsigned int target)
  463. __releases(&gl->gl_spin)
  464. __acquires(&gl->gl_spin)
  465. {
  466. const struct gfs2_glock_operations *glops = gl->gl_ops;
  467. struct gfs2_sbd *sdp = gl->gl_sbd;
  468. unsigned int lck_flags = gh ? gh->gh_flags : 0;
  469. int ret;
  470. lck_flags &= (LM_FLAG_TRY | LM_FLAG_TRY_1CB | LM_FLAG_NOEXP |
  471. LM_FLAG_PRIORITY);
  472. GLOCK_BUG_ON(gl, gl->gl_state == target);
  473. GLOCK_BUG_ON(gl, gl->gl_state == gl->gl_target);
  474. if ((target == LM_ST_UNLOCKED || target == LM_ST_DEFERRED) &&
  475. glops->go_inval) {
  476. set_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags);
  477. do_error(gl, 0); /* Fail queued try locks */
  478. }
  479. gl->gl_req = target;
  480. set_bit(GLF_BLOCKING, &gl->gl_flags);
  481. if ((gl->gl_req == LM_ST_UNLOCKED) ||
  482. (gl->gl_state == LM_ST_EXCLUSIVE) ||
  483. (lck_flags & (LM_FLAG_TRY|LM_FLAG_TRY_1CB)))
  484. clear_bit(GLF_BLOCKING, &gl->gl_flags);
  485. spin_unlock(&gl->gl_spin);
  486. if (glops->go_xmote_th)
  487. glops->go_xmote_th(gl);
  488. if (test_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags))
  489. glops->go_inval(gl, target == LM_ST_DEFERRED ? 0 : DIO_METADATA);
  490. clear_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags);
  491. gfs2_glock_hold(gl);
  492. if (sdp->sd_lockstruct.ls_ops->lm_lock) {
  493. /* lock_dlm */
  494. ret = sdp->sd_lockstruct.ls_ops->lm_lock(gl, target, lck_flags);
  495. GLOCK_BUG_ON(gl, ret);
  496. } else { /* lock_nolock */
  497. finish_xmote(gl, target);
  498. if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
  499. gfs2_glock_put(gl);
  500. }
  501. spin_lock(&gl->gl_spin);
  502. }
  503. /**
  504. * find_first_holder - find the first "holder" gh
  505. * @gl: the glock
  506. */
  507. static inline struct gfs2_holder *find_first_holder(const struct gfs2_glock *gl)
  508. {
  509. struct gfs2_holder *gh;
  510. if (!list_empty(&gl->gl_holders)) {
  511. gh = list_entry(gl->gl_holders.next, struct gfs2_holder, gh_list);
  512. if (test_bit(HIF_HOLDER, &gh->gh_iflags))
  513. return gh;
  514. }
  515. return NULL;
  516. }
  517. /**
  518. * run_queue - do all outstanding tasks related to a glock
  519. * @gl: The glock in question
  520. * @nonblock: True if we must not block in run_queue
  521. *
  522. */
  523. static void run_queue(struct gfs2_glock *gl, const int nonblock)
  524. __releases(&gl->gl_spin)
  525. __acquires(&gl->gl_spin)
  526. {
  527. struct gfs2_holder *gh = NULL;
  528. int ret;
  529. if (test_and_set_bit(GLF_LOCK, &gl->gl_flags))
  530. return;
  531. GLOCK_BUG_ON(gl, test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags));
  532. if (test_bit(GLF_DEMOTE, &gl->gl_flags) &&
  533. gl->gl_demote_state != gl->gl_state) {
  534. if (find_first_holder(gl))
  535. goto out_unlock;
  536. if (nonblock)
  537. goto out_sched;
  538. set_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags);
  539. GLOCK_BUG_ON(gl, gl->gl_demote_state == LM_ST_EXCLUSIVE);
  540. gl->gl_target = gl->gl_demote_state;
  541. } else {
  542. if (test_bit(GLF_DEMOTE, &gl->gl_flags))
  543. gfs2_demote_wake(gl);
  544. ret = do_promote(gl);
  545. if (ret == 0)
  546. goto out_unlock;
  547. if (ret == 2)
  548. goto out;
  549. gh = find_first_waiter(gl);
  550. gl->gl_target = gh->gh_state;
  551. if (!(gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)))
  552. do_error(gl, 0); /* Fail queued try locks */
  553. }
  554. do_xmote(gl, gh, gl->gl_target);
  555. out:
  556. return;
  557. out_sched:
  558. clear_bit(GLF_LOCK, &gl->gl_flags);
  559. smp_mb__after_clear_bit();
  560. gfs2_glock_hold(gl);
  561. if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
  562. gfs2_glock_put_nolock(gl);
  563. return;
  564. out_unlock:
  565. clear_bit(GLF_LOCK, &gl->gl_flags);
  566. smp_mb__after_clear_bit();
  567. return;
  568. }
  569. static void delete_work_func(struct work_struct *work)
  570. {
  571. struct gfs2_glock *gl = container_of(work, struct gfs2_glock, gl_delete);
  572. struct gfs2_sbd *sdp = gl->gl_sbd;
  573. struct gfs2_inode *ip;
  574. struct inode *inode;
  575. u64 no_addr = gl->gl_name.ln_number;
  576. ip = gl->gl_object;
  577. /* Note: Unsafe to dereference ip as we don't hold right refs/locks */
  578. if (ip)
  579. inode = gfs2_ilookup(sdp->sd_vfs, no_addr, 1);
  580. else
  581. inode = gfs2_lookup_by_inum(sdp, no_addr, NULL, GFS2_BLKST_UNLINKED);
  582. if (inode && !IS_ERR(inode)) {
  583. d_prune_aliases(inode);
  584. iput(inode);
  585. }
  586. gfs2_glock_put(gl);
  587. }
  588. static void glock_work_func(struct work_struct *work)
  589. {
  590. unsigned long delay = 0;
  591. struct gfs2_glock *gl = container_of(work, struct gfs2_glock, gl_work.work);
  592. int drop_ref = 0;
  593. if (test_and_clear_bit(GLF_REPLY_PENDING, &gl->gl_flags)) {
  594. finish_xmote(gl, gl->gl_reply);
  595. drop_ref = 1;
  596. }
  597. spin_lock(&gl->gl_spin);
  598. if (test_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) &&
  599. gl->gl_state != LM_ST_UNLOCKED &&
  600. gl->gl_demote_state != LM_ST_EXCLUSIVE) {
  601. unsigned long holdtime, now = jiffies;
  602. holdtime = gl->gl_tchange + gl->gl_hold_time;
  603. if (time_before(now, holdtime))
  604. delay = holdtime - now;
  605. if (!delay) {
  606. clear_bit(GLF_PENDING_DEMOTE, &gl->gl_flags);
  607. set_bit(GLF_DEMOTE, &gl->gl_flags);
  608. }
  609. }
  610. run_queue(gl, 0);
  611. spin_unlock(&gl->gl_spin);
  612. if (!delay)
  613. gfs2_glock_put(gl);
  614. else {
  615. if (gl->gl_name.ln_type != LM_TYPE_INODE)
  616. delay = 0;
  617. if (queue_delayed_work(glock_workqueue, &gl->gl_work, delay) == 0)
  618. gfs2_glock_put(gl);
  619. }
  620. if (drop_ref)
  621. gfs2_glock_put(gl);
  622. }
  623. /**
  624. * gfs2_glock_get() - Get a glock, or create one if one doesn't exist
  625. * @sdp: The GFS2 superblock
  626. * @number: the lock number
  627. * @glops: The glock_operations to use
  628. * @create: If 0, don't create the glock if it doesn't exist
  629. * @glp: the glock is returned here
  630. *
  631. * This does not lock a glock, just finds/creates structures for one.
  632. *
  633. * Returns: errno
  634. */
  635. int gfs2_glock_get(struct gfs2_sbd *sdp, u64 number,
  636. const struct gfs2_glock_operations *glops, int create,
  637. struct gfs2_glock **glp)
  638. {
  639. struct super_block *s = sdp->sd_vfs;
  640. struct lm_lockname name = { .ln_number = number, .ln_type = glops->go_type };
  641. struct gfs2_glock *gl, *tmp;
  642. unsigned int hash = gl_hash(sdp, &name);
  643. struct address_space *mapping;
  644. struct kmem_cache *cachep;
  645. rcu_read_lock();
  646. gl = search_bucket(hash, sdp, &name);
  647. rcu_read_unlock();
  648. *glp = gl;
  649. if (gl)
  650. return 0;
  651. if (!create)
  652. return -ENOENT;
  653. if (glops->go_flags & GLOF_ASPACE)
  654. cachep = gfs2_glock_aspace_cachep;
  655. else
  656. cachep = gfs2_glock_cachep;
  657. gl = kmem_cache_alloc(cachep, GFP_KERNEL);
  658. if (!gl)
  659. return -ENOMEM;
  660. atomic_inc(&sdp->sd_glock_disposal);
  661. gl->gl_sbd = sdp;
  662. gl->gl_flags = 0;
  663. gl->gl_name = name;
  664. atomic_set(&gl->gl_ref, 1);
  665. gl->gl_state = LM_ST_UNLOCKED;
  666. gl->gl_target = LM_ST_UNLOCKED;
  667. gl->gl_demote_state = LM_ST_EXCLUSIVE;
  668. gl->gl_hash = hash;
  669. gl->gl_ops = glops;
  670. gl->gl_dstamp = ktime_set(0, 0);
  671. preempt_disable();
  672. /* We use the global stats to estimate the initial per-glock stats */
  673. gl->gl_stats = this_cpu_ptr(sdp->sd_lkstats)->lkstats[glops->go_type];
  674. preempt_enable();
  675. gl->gl_stats.stats[GFS2_LKS_DCOUNT] = 0;
  676. gl->gl_stats.stats[GFS2_LKS_QCOUNT] = 0;
  677. memset(&gl->gl_lksb, 0, sizeof(struct dlm_lksb));
  678. gl->gl_lksb.sb_lvbptr = gl->gl_lvb;
  679. gl->gl_tchange = jiffies;
  680. gl->gl_object = NULL;
  681. gl->gl_hold_time = GL_GLOCK_DFT_HOLD;
  682. INIT_DELAYED_WORK(&gl->gl_work, glock_work_func);
  683. INIT_WORK(&gl->gl_delete, delete_work_func);
  684. mapping = gfs2_glock2aspace(gl);
  685. if (mapping) {
  686. mapping->a_ops = &gfs2_meta_aops;
  687. mapping->host = s->s_bdev->bd_inode;
  688. mapping->flags = 0;
  689. mapping_set_gfp_mask(mapping, GFP_NOFS);
  690. mapping->assoc_mapping = NULL;
  691. mapping->backing_dev_info = s->s_bdi;
  692. mapping->writeback_index = 0;
  693. }
  694. spin_lock_bucket(hash);
  695. tmp = search_bucket(hash, sdp, &name);
  696. if (tmp) {
  697. spin_unlock_bucket(hash);
  698. kmem_cache_free(cachep, gl);
  699. atomic_dec(&sdp->sd_glock_disposal);
  700. gl = tmp;
  701. } else {
  702. hlist_bl_add_head_rcu(&gl->gl_list, &gl_hash_table[hash]);
  703. spin_unlock_bucket(hash);
  704. }
  705. *glp = gl;
  706. return 0;
  707. }
  708. /**
  709. * gfs2_holder_init - initialize a struct gfs2_holder in the default way
  710. * @gl: the glock
  711. * @state: the state we're requesting
  712. * @flags: the modifier flags
  713. * @gh: the holder structure
  714. *
  715. */
  716. void gfs2_holder_init(struct gfs2_glock *gl, unsigned int state, unsigned flags,
  717. struct gfs2_holder *gh)
  718. {
  719. INIT_LIST_HEAD(&gh->gh_list);
  720. gh->gh_gl = gl;
  721. gh->gh_ip = (unsigned long)__builtin_return_address(0);
  722. gh->gh_owner_pid = get_pid(task_pid(current));
  723. gh->gh_state = state;
  724. gh->gh_flags = flags;
  725. gh->gh_error = 0;
  726. gh->gh_iflags = 0;
  727. gfs2_glock_hold(gl);
  728. }
  729. /**
  730. * gfs2_holder_reinit - reinitialize a struct gfs2_holder so we can requeue it
  731. * @state: the state we're requesting
  732. * @flags: the modifier flags
  733. * @gh: the holder structure
  734. *
  735. * Don't mess with the glock.
  736. *
  737. */
  738. void gfs2_holder_reinit(unsigned int state, unsigned flags, struct gfs2_holder *gh)
  739. {
  740. gh->gh_state = state;
  741. gh->gh_flags = flags;
  742. gh->gh_iflags = 0;
  743. gh->gh_ip = (unsigned long)__builtin_return_address(0);
  744. if (gh->gh_owner_pid)
  745. put_pid(gh->gh_owner_pid);
  746. gh->gh_owner_pid = get_pid(task_pid(current));
  747. }
  748. /**
  749. * gfs2_holder_uninit - uninitialize a holder structure (drop glock reference)
  750. * @gh: the holder structure
  751. *
  752. */
  753. void gfs2_holder_uninit(struct gfs2_holder *gh)
  754. {
  755. put_pid(gh->gh_owner_pid);
  756. gfs2_glock_put(gh->gh_gl);
  757. gh->gh_gl = NULL;
  758. gh->gh_ip = 0;
  759. }
  760. /**
  761. * gfs2_glock_holder_wait
  762. * @word: unused
  763. *
  764. * This function and gfs2_glock_demote_wait both show up in the WCHAN
  765. * field. Thus I've separated these otherwise identical functions in
  766. * order to be more informative to the user.
  767. */
  768. static int gfs2_glock_holder_wait(void *word)
  769. {
  770. schedule();
  771. return 0;
  772. }
  773. static int gfs2_glock_demote_wait(void *word)
  774. {
  775. schedule();
  776. return 0;
  777. }
  778. static void wait_on_holder(struct gfs2_holder *gh)
  779. {
  780. unsigned long time1 = jiffies;
  781. might_sleep();
  782. wait_on_bit(&gh->gh_iflags, HIF_WAIT, gfs2_glock_holder_wait, TASK_UNINTERRUPTIBLE);
  783. if (time_after(jiffies, time1 + HZ)) /* have we waited > a second? */
  784. /* Lengthen the minimum hold time. */
  785. gh->gh_gl->gl_hold_time = min(gh->gh_gl->gl_hold_time +
  786. GL_GLOCK_HOLD_INCR,
  787. GL_GLOCK_MAX_HOLD);
  788. }
  789. static void wait_on_demote(struct gfs2_glock *gl)
  790. {
  791. might_sleep();
  792. wait_on_bit(&gl->gl_flags, GLF_DEMOTE, gfs2_glock_demote_wait, TASK_UNINTERRUPTIBLE);
  793. }
  794. /**
  795. * handle_callback - process a demote request
  796. * @gl: the glock
  797. * @state: the state the caller wants us to change to
  798. *
  799. * There are only two requests that we are going to see in actual
  800. * practise: LM_ST_SHARED and LM_ST_UNLOCKED
  801. */
  802. static void handle_callback(struct gfs2_glock *gl, unsigned int state,
  803. unsigned long delay)
  804. {
  805. int bit = delay ? GLF_PENDING_DEMOTE : GLF_DEMOTE;
  806. set_bit(bit, &gl->gl_flags);
  807. if (gl->gl_demote_state == LM_ST_EXCLUSIVE) {
  808. gl->gl_demote_state = state;
  809. gl->gl_demote_time = jiffies;
  810. } else if (gl->gl_demote_state != LM_ST_UNLOCKED &&
  811. gl->gl_demote_state != state) {
  812. gl->gl_demote_state = LM_ST_UNLOCKED;
  813. }
  814. if (gl->gl_ops->go_callback)
  815. gl->gl_ops->go_callback(gl);
  816. trace_gfs2_demote_rq(gl);
  817. }
  818. /**
  819. * gfs2_glock_wait - wait on a glock acquisition
  820. * @gh: the glock holder
  821. *
  822. * Returns: 0 on success
  823. */
  824. int gfs2_glock_wait(struct gfs2_holder *gh)
  825. {
  826. wait_on_holder(gh);
  827. return gh->gh_error;
  828. }
  829. void gfs2_print_dbg(struct seq_file *seq, const char *fmt, ...)
  830. {
  831. struct va_format vaf;
  832. va_list args;
  833. va_start(args, fmt);
  834. if (seq) {
  835. struct gfs2_glock_iter *gi = seq->private;
  836. vsprintf(gi->string, fmt, args);
  837. seq_printf(seq, gi->string);
  838. } else {
  839. vaf.fmt = fmt;
  840. vaf.va = &args;
  841. printk(KERN_ERR " %pV", &vaf);
  842. }
  843. va_end(args);
  844. }
  845. /**
  846. * add_to_queue - Add a holder to the wait queue (but look for recursion)
  847. * @gh: the holder structure to add
  848. *
  849. * Eventually we should move the recursive locking trap to a
  850. * debugging option or something like that. This is the fast
  851. * path and needs to have the minimum number of distractions.
  852. *
  853. */
  854. static inline void add_to_queue(struct gfs2_holder *gh)
  855. __releases(&gl->gl_spin)
  856. __acquires(&gl->gl_spin)
  857. {
  858. struct gfs2_glock *gl = gh->gh_gl;
  859. struct gfs2_sbd *sdp = gl->gl_sbd;
  860. struct list_head *insert_pt = NULL;
  861. struct gfs2_holder *gh2;
  862. int try_lock = 0;
  863. BUG_ON(gh->gh_owner_pid == NULL);
  864. if (test_and_set_bit(HIF_WAIT, &gh->gh_iflags))
  865. BUG();
  866. if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) {
  867. if (test_bit(GLF_LOCK, &gl->gl_flags))
  868. try_lock = 1;
  869. if (test_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags))
  870. goto fail;
  871. }
  872. list_for_each_entry(gh2, &gl->gl_holders, gh_list) {
  873. if (unlikely(gh2->gh_owner_pid == gh->gh_owner_pid &&
  874. (gh->gh_gl->gl_ops->go_type != LM_TYPE_FLOCK)))
  875. goto trap_recursive;
  876. if (try_lock &&
  877. !(gh2->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) &&
  878. !may_grant(gl, gh)) {
  879. fail:
  880. gh->gh_error = GLR_TRYFAILED;
  881. gfs2_holder_wake(gh);
  882. return;
  883. }
  884. if (test_bit(HIF_HOLDER, &gh2->gh_iflags))
  885. continue;
  886. if (unlikely((gh->gh_flags & LM_FLAG_PRIORITY) && !insert_pt))
  887. insert_pt = &gh2->gh_list;
  888. }
  889. set_bit(GLF_QUEUED, &gl->gl_flags);
  890. trace_gfs2_glock_queue(gh, 1);
  891. gfs2_glstats_inc(gl, GFS2_LKS_QCOUNT);
  892. gfs2_sbstats_inc(gl, GFS2_LKS_QCOUNT);
  893. if (likely(insert_pt == NULL)) {
  894. list_add_tail(&gh->gh_list, &gl->gl_holders);
  895. if (unlikely(gh->gh_flags & LM_FLAG_PRIORITY))
  896. goto do_cancel;
  897. return;
  898. }
  899. list_add_tail(&gh->gh_list, insert_pt);
  900. do_cancel:
  901. gh = list_entry(gl->gl_holders.next, struct gfs2_holder, gh_list);
  902. if (!(gh->gh_flags & LM_FLAG_PRIORITY)) {
  903. spin_unlock(&gl->gl_spin);
  904. if (sdp->sd_lockstruct.ls_ops->lm_cancel)
  905. sdp->sd_lockstruct.ls_ops->lm_cancel(gl);
  906. spin_lock(&gl->gl_spin);
  907. }
  908. return;
  909. trap_recursive:
  910. print_symbol(KERN_ERR "original: %s\n", gh2->gh_ip);
  911. printk(KERN_ERR "pid: %d\n", pid_nr(gh2->gh_owner_pid));
  912. printk(KERN_ERR "lock type: %d req lock state : %d\n",
  913. gh2->gh_gl->gl_name.ln_type, gh2->gh_state);
  914. print_symbol(KERN_ERR "new: %s\n", gh->gh_ip);
  915. printk(KERN_ERR "pid: %d\n", pid_nr(gh->gh_owner_pid));
  916. printk(KERN_ERR "lock type: %d req lock state : %d\n",
  917. gh->gh_gl->gl_name.ln_type, gh->gh_state);
  918. __dump_glock(NULL, gl);
  919. BUG();
  920. }
  921. /**
  922. * gfs2_glock_nq - enqueue a struct gfs2_holder onto a glock (acquire a glock)
  923. * @gh: the holder structure
  924. *
  925. * if (gh->gh_flags & GL_ASYNC), this never returns an error
  926. *
  927. * Returns: 0, GLR_TRYFAILED, or errno on failure
  928. */
  929. int gfs2_glock_nq(struct gfs2_holder *gh)
  930. {
  931. struct gfs2_glock *gl = gh->gh_gl;
  932. struct gfs2_sbd *sdp = gl->gl_sbd;
  933. int error = 0;
  934. if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
  935. return -EIO;
  936. if (test_bit(GLF_LRU, &gl->gl_flags))
  937. gfs2_glock_remove_from_lru(gl);
  938. spin_lock(&gl->gl_spin);
  939. add_to_queue(gh);
  940. if ((LM_FLAG_NOEXP & gh->gh_flags) &&
  941. test_and_clear_bit(GLF_FROZEN, &gl->gl_flags))
  942. set_bit(GLF_REPLY_PENDING, &gl->gl_flags);
  943. run_queue(gl, 1);
  944. spin_unlock(&gl->gl_spin);
  945. if (!(gh->gh_flags & GL_ASYNC))
  946. error = gfs2_glock_wait(gh);
  947. return error;
  948. }
  949. /**
  950. * gfs2_glock_poll - poll to see if an async request has been completed
  951. * @gh: the holder
  952. *
  953. * Returns: 1 if the request is ready to be gfs2_glock_wait()ed on
  954. */
  955. int gfs2_glock_poll(struct gfs2_holder *gh)
  956. {
  957. return test_bit(HIF_WAIT, &gh->gh_iflags) ? 0 : 1;
  958. }
  959. /**
  960. * gfs2_glock_dq - dequeue a struct gfs2_holder from a glock (release a glock)
  961. * @gh: the glock holder
  962. *
  963. */
  964. void gfs2_glock_dq(struct gfs2_holder *gh)
  965. {
  966. struct gfs2_glock *gl = gh->gh_gl;
  967. const struct gfs2_glock_operations *glops = gl->gl_ops;
  968. unsigned delay = 0;
  969. int fast_path = 0;
  970. spin_lock(&gl->gl_spin);
  971. if (gh->gh_flags & GL_NOCACHE)
  972. handle_callback(gl, LM_ST_UNLOCKED, 0);
  973. list_del_init(&gh->gh_list);
  974. if (find_first_holder(gl) == NULL) {
  975. if (glops->go_unlock) {
  976. GLOCK_BUG_ON(gl, test_and_set_bit(GLF_LOCK, &gl->gl_flags));
  977. spin_unlock(&gl->gl_spin);
  978. glops->go_unlock(gh);
  979. spin_lock(&gl->gl_spin);
  980. clear_bit(GLF_LOCK, &gl->gl_flags);
  981. }
  982. if (list_empty(&gl->gl_holders) &&
  983. !test_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) &&
  984. !test_bit(GLF_DEMOTE, &gl->gl_flags))
  985. fast_path = 1;
  986. }
  987. if (!test_bit(GLF_LFLUSH, &gl->gl_flags))
  988. __gfs2_glock_schedule_for_reclaim(gl);
  989. trace_gfs2_glock_queue(gh, 0);
  990. spin_unlock(&gl->gl_spin);
  991. if (likely(fast_path))
  992. return;
  993. gfs2_glock_hold(gl);
  994. if (test_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) &&
  995. !test_bit(GLF_DEMOTE, &gl->gl_flags) &&
  996. gl->gl_name.ln_type == LM_TYPE_INODE)
  997. delay = gl->gl_hold_time;
  998. if (queue_delayed_work(glock_workqueue, &gl->gl_work, delay) == 0)
  999. gfs2_glock_put(gl);
  1000. }
  1001. void gfs2_glock_dq_wait(struct gfs2_holder *gh)
  1002. {
  1003. struct gfs2_glock *gl = gh->gh_gl;
  1004. gfs2_glock_dq(gh);
  1005. wait_on_demote(gl);
  1006. }
  1007. /**
  1008. * gfs2_glock_dq_uninit - dequeue a holder from a glock and initialize it
  1009. * @gh: the holder structure
  1010. *
  1011. */
  1012. void gfs2_glock_dq_uninit(struct gfs2_holder *gh)
  1013. {
  1014. gfs2_glock_dq(gh);
  1015. gfs2_holder_uninit(gh);
  1016. }
  1017. /**
  1018. * gfs2_glock_nq_num - acquire a glock based on lock number
  1019. * @sdp: the filesystem
  1020. * @number: the lock number
  1021. * @glops: the glock operations for the type of glock
  1022. * @state: the state to acquire the glock in
  1023. * @flags: modifier flags for the acquisition
  1024. * @gh: the struct gfs2_holder
  1025. *
  1026. * Returns: errno
  1027. */
  1028. int gfs2_glock_nq_num(struct gfs2_sbd *sdp, u64 number,
  1029. const struct gfs2_glock_operations *glops,
  1030. unsigned int state, int flags, struct gfs2_holder *gh)
  1031. {
  1032. struct gfs2_glock *gl;
  1033. int error;
  1034. error = gfs2_glock_get(sdp, number, glops, CREATE, &gl);
  1035. if (!error) {
  1036. error = gfs2_glock_nq_init(gl, state, flags, gh);
  1037. gfs2_glock_put(gl);
  1038. }
  1039. return error;
  1040. }
  1041. /**
  1042. * glock_compare - Compare two struct gfs2_glock structures for sorting
  1043. * @arg_a: the first structure
  1044. * @arg_b: the second structure
  1045. *
  1046. */
  1047. static int glock_compare(const void *arg_a, const void *arg_b)
  1048. {
  1049. const struct gfs2_holder *gh_a = *(const struct gfs2_holder **)arg_a;
  1050. const struct gfs2_holder *gh_b = *(const struct gfs2_holder **)arg_b;
  1051. const struct lm_lockname *a = &gh_a->gh_gl->gl_name;
  1052. const struct lm_lockname *b = &gh_b->gh_gl->gl_name;
  1053. if (a->ln_number > b->ln_number)
  1054. return 1;
  1055. if (a->ln_number < b->ln_number)
  1056. return -1;
  1057. BUG_ON(gh_a->gh_gl->gl_ops->go_type == gh_b->gh_gl->gl_ops->go_type);
  1058. return 0;
  1059. }
  1060. /**
  1061. * nq_m_sync - synchonously acquire more than one glock in deadlock free order
  1062. * @num_gh: the number of structures
  1063. * @ghs: an array of struct gfs2_holder structures
  1064. *
  1065. * Returns: 0 on success (all glocks acquired),
  1066. * errno on failure (no glocks acquired)
  1067. */
  1068. static int nq_m_sync(unsigned int num_gh, struct gfs2_holder *ghs,
  1069. struct gfs2_holder **p)
  1070. {
  1071. unsigned int x;
  1072. int error = 0;
  1073. for (x = 0; x < num_gh; x++)
  1074. p[x] = &ghs[x];
  1075. sort(p, num_gh, sizeof(struct gfs2_holder *), glock_compare, NULL);
  1076. for (x = 0; x < num_gh; x++) {
  1077. p[x]->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
  1078. error = gfs2_glock_nq(p[x]);
  1079. if (error) {
  1080. while (x--)
  1081. gfs2_glock_dq(p[x]);
  1082. break;
  1083. }
  1084. }
  1085. return error;
  1086. }
  1087. /**
  1088. * gfs2_glock_nq_m - acquire multiple glocks
  1089. * @num_gh: the number of structures
  1090. * @ghs: an array of struct gfs2_holder structures
  1091. *
  1092. *
  1093. * Returns: 0 on success (all glocks acquired),
  1094. * errno on failure (no glocks acquired)
  1095. */
  1096. int gfs2_glock_nq_m(unsigned int num_gh, struct gfs2_holder *ghs)
  1097. {
  1098. struct gfs2_holder *tmp[4];
  1099. struct gfs2_holder **pph = tmp;
  1100. int error = 0;
  1101. switch(num_gh) {
  1102. case 0:
  1103. return 0;
  1104. case 1:
  1105. ghs->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
  1106. return gfs2_glock_nq(ghs);
  1107. default:
  1108. if (num_gh <= 4)
  1109. break;
  1110. pph = kmalloc(num_gh * sizeof(struct gfs2_holder *), GFP_NOFS);
  1111. if (!pph)
  1112. return -ENOMEM;
  1113. }
  1114. error = nq_m_sync(num_gh, ghs, pph);
  1115. if (pph != tmp)
  1116. kfree(pph);
  1117. return error;
  1118. }
  1119. /**
  1120. * gfs2_glock_dq_m - release multiple glocks
  1121. * @num_gh: the number of structures
  1122. * @ghs: an array of struct gfs2_holder structures
  1123. *
  1124. */
  1125. void gfs2_glock_dq_m(unsigned int num_gh, struct gfs2_holder *ghs)
  1126. {
  1127. while (num_gh--)
  1128. gfs2_glock_dq(&ghs[num_gh]);
  1129. }
  1130. /**
  1131. * gfs2_glock_dq_uninit_m - release multiple glocks
  1132. * @num_gh: the number of structures
  1133. * @ghs: an array of struct gfs2_holder structures
  1134. *
  1135. */
  1136. void gfs2_glock_dq_uninit_m(unsigned int num_gh, struct gfs2_holder *ghs)
  1137. {
  1138. while (num_gh--)
  1139. gfs2_glock_dq_uninit(&ghs[num_gh]);
  1140. }
  1141. void gfs2_glock_cb(struct gfs2_glock *gl, unsigned int state)
  1142. {
  1143. unsigned long delay = 0;
  1144. unsigned long holdtime;
  1145. unsigned long now = jiffies;
  1146. gfs2_glock_hold(gl);
  1147. holdtime = gl->gl_tchange + gl->gl_hold_time;
  1148. if (test_bit(GLF_QUEUED, &gl->gl_flags) &&
  1149. gl->gl_name.ln_type == LM_TYPE_INODE) {
  1150. if (time_before(now, holdtime))
  1151. delay = holdtime - now;
  1152. if (test_bit(GLF_REPLY_PENDING, &gl->gl_flags))
  1153. delay = gl->gl_hold_time;
  1154. }
  1155. spin_lock(&gl->gl_spin);
  1156. handle_callback(gl, state, delay);
  1157. spin_unlock(&gl->gl_spin);
  1158. if (queue_delayed_work(glock_workqueue, &gl->gl_work, delay) == 0)
  1159. gfs2_glock_put(gl);
  1160. }
  1161. /**
  1162. * gfs2_should_freeze - Figure out if glock should be frozen
  1163. * @gl: The glock in question
  1164. *
  1165. * Glocks are not frozen if (a) the result of the dlm operation is
  1166. * an error, (b) the locking operation was an unlock operation or
  1167. * (c) if there is a "noexp" flagged request anywhere in the queue
  1168. *
  1169. * Returns: 1 if freezing should occur, 0 otherwise
  1170. */
  1171. static int gfs2_should_freeze(const struct gfs2_glock *gl)
  1172. {
  1173. const struct gfs2_holder *gh;
  1174. if (gl->gl_reply & ~LM_OUT_ST_MASK)
  1175. return 0;
  1176. if (gl->gl_target == LM_ST_UNLOCKED)
  1177. return 0;
  1178. list_for_each_entry(gh, &gl->gl_holders, gh_list) {
  1179. if (test_bit(HIF_HOLDER, &gh->gh_iflags))
  1180. continue;
  1181. if (LM_FLAG_NOEXP & gh->gh_flags)
  1182. return 0;
  1183. }
  1184. return 1;
  1185. }
  1186. /**
  1187. * gfs2_glock_complete - Callback used by locking
  1188. * @gl: Pointer to the glock
  1189. * @ret: The return value from the dlm
  1190. *
  1191. * The gl_reply field is under the gl_spin lock so that it is ok
  1192. * to use a bitfield shared with other glock state fields.
  1193. */
  1194. void gfs2_glock_complete(struct gfs2_glock *gl, int ret)
  1195. {
  1196. struct lm_lockstruct *ls = &gl->gl_sbd->sd_lockstruct;
  1197. spin_lock(&gl->gl_spin);
  1198. gl->gl_reply = ret;
  1199. if (unlikely(test_bit(DFL_BLOCK_LOCKS, &ls->ls_recover_flags))) {
  1200. if (gfs2_should_freeze(gl)) {
  1201. set_bit(GLF_FROZEN, &gl->gl_flags);
  1202. spin_unlock(&gl->gl_spin);
  1203. return;
  1204. }
  1205. }
  1206. spin_unlock(&gl->gl_spin);
  1207. set_bit(GLF_REPLY_PENDING, &gl->gl_flags);
  1208. smp_wmb();
  1209. gfs2_glock_hold(gl);
  1210. if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
  1211. gfs2_glock_put(gl);
  1212. }
  1213. static int gfs2_shrink_glock_memory(struct shrinker *shrink,
  1214. struct shrink_control *sc)
  1215. {
  1216. struct gfs2_glock *gl;
  1217. int may_demote;
  1218. int nr_skipped = 0;
  1219. int nr = sc->nr_to_scan;
  1220. gfp_t gfp_mask = sc->gfp_mask;
  1221. LIST_HEAD(skipped);
  1222. if (nr == 0)
  1223. goto out;
  1224. if (!(gfp_mask & __GFP_FS))
  1225. return -1;
  1226. spin_lock(&lru_lock);
  1227. while(nr && !list_empty(&lru_list)) {
  1228. gl = list_entry(lru_list.next, struct gfs2_glock, gl_lru);
  1229. list_del_init(&gl->gl_lru);
  1230. clear_bit(GLF_LRU, &gl->gl_flags);
  1231. atomic_dec(&lru_count);
  1232. /* Test for being demotable */
  1233. if (!test_and_set_bit(GLF_LOCK, &gl->gl_flags)) {
  1234. gfs2_glock_hold(gl);
  1235. spin_unlock(&lru_lock);
  1236. spin_lock(&gl->gl_spin);
  1237. may_demote = demote_ok(gl);
  1238. if (may_demote) {
  1239. handle_callback(gl, LM_ST_UNLOCKED, 0);
  1240. nr--;
  1241. }
  1242. clear_bit(GLF_LOCK, &gl->gl_flags);
  1243. smp_mb__after_clear_bit();
  1244. if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
  1245. gfs2_glock_put_nolock(gl);
  1246. spin_unlock(&gl->gl_spin);
  1247. spin_lock(&lru_lock);
  1248. continue;
  1249. }
  1250. nr_skipped++;
  1251. list_add(&gl->gl_lru, &skipped);
  1252. set_bit(GLF_LRU, &gl->gl_flags);
  1253. }
  1254. list_splice(&skipped, &lru_list);
  1255. atomic_add(nr_skipped, &lru_count);
  1256. spin_unlock(&lru_lock);
  1257. out:
  1258. return vfs_pressure_ratio(atomic_read(&lru_count));
  1259. }
  1260. static struct shrinker glock_shrinker = {
  1261. .shrink = gfs2_shrink_glock_memory,
  1262. .seeks = DEFAULT_SEEKS,
  1263. };
  1264. /**
  1265. * examine_bucket - Call a function for glock in a hash bucket
  1266. * @examiner: the function
  1267. * @sdp: the filesystem
  1268. * @bucket: the bucket
  1269. *
  1270. */
  1271. static void examine_bucket(glock_examiner examiner, const struct gfs2_sbd *sdp,
  1272. unsigned int hash)
  1273. {
  1274. struct gfs2_glock *gl;
  1275. struct hlist_bl_head *head = &gl_hash_table[hash];
  1276. struct hlist_bl_node *pos;
  1277. rcu_read_lock();
  1278. hlist_bl_for_each_entry_rcu(gl, pos, head, gl_list) {
  1279. if ((gl->gl_sbd == sdp) && atomic_read(&gl->gl_ref))
  1280. examiner(gl);
  1281. }
  1282. rcu_read_unlock();
  1283. cond_resched();
  1284. }
  1285. static void glock_hash_walk(glock_examiner examiner, const struct gfs2_sbd *sdp)
  1286. {
  1287. unsigned x;
  1288. for (x = 0; x < GFS2_GL_HASH_SIZE; x++)
  1289. examine_bucket(examiner, sdp, x);
  1290. }
  1291. /**
  1292. * thaw_glock - thaw out a glock which has an unprocessed reply waiting
  1293. * @gl: The glock to thaw
  1294. *
  1295. * N.B. When we freeze a glock, we leave a ref to the glock outstanding,
  1296. * so this has to result in the ref count being dropped by one.
  1297. */
  1298. static void thaw_glock(struct gfs2_glock *gl)
  1299. {
  1300. if (!test_and_clear_bit(GLF_FROZEN, &gl->gl_flags))
  1301. return;
  1302. set_bit(GLF_REPLY_PENDING, &gl->gl_flags);
  1303. gfs2_glock_hold(gl);
  1304. if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
  1305. gfs2_glock_put(gl);
  1306. }
  1307. /**
  1308. * clear_glock - look at a glock and see if we can free it from glock cache
  1309. * @gl: the glock to look at
  1310. *
  1311. */
  1312. static void clear_glock(struct gfs2_glock *gl)
  1313. {
  1314. gfs2_glock_remove_from_lru(gl);
  1315. spin_lock(&gl->gl_spin);
  1316. if (gl->gl_state != LM_ST_UNLOCKED)
  1317. handle_callback(gl, LM_ST_UNLOCKED, 0);
  1318. spin_unlock(&gl->gl_spin);
  1319. gfs2_glock_hold(gl);
  1320. if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
  1321. gfs2_glock_put(gl);
  1322. }
  1323. /**
  1324. * gfs2_glock_thaw - Thaw any frozen glocks
  1325. * @sdp: The super block
  1326. *
  1327. */
  1328. void gfs2_glock_thaw(struct gfs2_sbd *sdp)
  1329. {
  1330. glock_hash_walk(thaw_glock, sdp);
  1331. }
  1332. static int dump_glock(struct seq_file *seq, struct gfs2_glock *gl)
  1333. {
  1334. int ret;
  1335. spin_lock(&gl->gl_spin);
  1336. ret = __dump_glock(seq, gl);
  1337. spin_unlock(&gl->gl_spin);
  1338. return ret;
  1339. }
  1340. static void dump_glock_func(struct gfs2_glock *gl)
  1341. {
  1342. dump_glock(NULL, gl);
  1343. }
  1344. /**
  1345. * gfs2_gl_hash_clear - Empty out the glock hash table
  1346. * @sdp: the filesystem
  1347. * @wait: wait until it's all gone
  1348. *
  1349. * Called when unmounting the filesystem.
  1350. */
  1351. void gfs2_gl_hash_clear(struct gfs2_sbd *sdp)
  1352. {
  1353. glock_hash_walk(clear_glock, sdp);
  1354. flush_workqueue(glock_workqueue);
  1355. wait_event(sdp->sd_glock_wait, atomic_read(&sdp->sd_glock_disposal) == 0);
  1356. glock_hash_walk(dump_glock_func, sdp);
  1357. }
  1358. void gfs2_glock_finish_truncate(struct gfs2_inode *ip)
  1359. {
  1360. struct gfs2_glock *gl = ip->i_gl;
  1361. int ret;
  1362. ret = gfs2_truncatei_resume(ip);
  1363. gfs2_assert_withdraw(gl->gl_sbd, ret == 0);
  1364. spin_lock(&gl->gl_spin);
  1365. clear_bit(GLF_LOCK, &gl->gl_flags);
  1366. run_queue(gl, 1);
  1367. spin_unlock(&gl->gl_spin);
  1368. }
  1369. static const char *state2str(unsigned state)
  1370. {
  1371. switch(state) {
  1372. case LM_ST_UNLOCKED:
  1373. return "UN";
  1374. case LM_ST_SHARED:
  1375. return "SH";
  1376. case LM_ST_DEFERRED:
  1377. return "DF";
  1378. case LM_ST_EXCLUSIVE:
  1379. return "EX";
  1380. }
  1381. return "??";
  1382. }
  1383. static const char *hflags2str(char *buf, unsigned flags, unsigned long iflags)
  1384. {
  1385. char *p = buf;
  1386. if (flags & LM_FLAG_TRY)
  1387. *p++ = 't';
  1388. if (flags & LM_FLAG_TRY_1CB)
  1389. *p++ = 'T';
  1390. if (flags & LM_FLAG_NOEXP)
  1391. *p++ = 'e';
  1392. if (flags & LM_FLAG_ANY)
  1393. *p++ = 'A';
  1394. if (flags & LM_FLAG_PRIORITY)
  1395. *p++ = 'p';
  1396. if (flags & GL_ASYNC)
  1397. *p++ = 'a';
  1398. if (flags & GL_EXACT)
  1399. *p++ = 'E';
  1400. if (flags & GL_NOCACHE)
  1401. *p++ = 'c';
  1402. if (test_bit(HIF_HOLDER, &iflags))
  1403. *p++ = 'H';
  1404. if (test_bit(HIF_WAIT, &iflags))
  1405. *p++ = 'W';
  1406. if (test_bit(HIF_FIRST, &iflags))
  1407. *p++ = 'F';
  1408. *p = 0;
  1409. return buf;
  1410. }
  1411. /**
  1412. * dump_holder - print information about a glock holder
  1413. * @seq: the seq_file struct
  1414. * @gh: the glock holder
  1415. *
  1416. * Returns: 0 on success, -ENOBUFS when we run out of space
  1417. */
  1418. static int dump_holder(struct seq_file *seq, const struct gfs2_holder *gh)
  1419. {
  1420. struct task_struct *gh_owner = NULL;
  1421. char flags_buf[32];
  1422. if (gh->gh_owner_pid)
  1423. gh_owner = pid_task(gh->gh_owner_pid, PIDTYPE_PID);
  1424. gfs2_print_dbg(seq, " H: s:%s f:%s e:%d p:%ld [%s] %pS\n",
  1425. state2str(gh->gh_state),
  1426. hflags2str(flags_buf, gh->gh_flags, gh->gh_iflags),
  1427. gh->gh_error,
  1428. gh->gh_owner_pid ? (long)pid_nr(gh->gh_owner_pid) : -1,
  1429. gh_owner ? gh_owner->comm : "(ended)",
  1430. (void *)gh->gh_ip);
  1431. return 0;
  1432. }
  1433. static const char *gflags2str(char *buf, const struct gfs2_glock *gl)
  1434. {
  1435. const unsigned long *gflags = &gl->gl_flags;
  1436. char *p = buf;
  1437. if (test_bit(GLF_LOCK, gflags))
  1438. *p++ = 'l';
  1439. if (test_bit(GLF_DEMOTE, gflags))
  1440. *p++ = 'D';
  1441. if (test_bit(GLF_PENDING_DEMOTE, gflags))
  1442. *p++ = 'd';
  1443. if (test_bit(GLF_DEMOTE_IN_PROGRESS, gflags))
  1444. *p++ = 'p';
  1445. if (test_bit(GLF_DIRTY, gflags))
  1446. *p++ = 'y';
  1447. if (test_bit(GLF_LFLUSH, gflags))
  1448. *p++ = 'f';
  1449. if (test_bit(GLF_INVALIDATE_IN_PROGRESS, gflags))
  1450. *p++ = 'i';
  1451. if (test_bit(GLF_REPLY_PENDING, gflags))
  1452. *p++ = 'r';
  1453. if (test_bit(GLF_INITIAL, gflags))
  1454. *p++ = 'I';
  1455. if (test_bit(GLF_FROZEN, gflags))
  1456. *p++ = 'F';
  1457. if (test_bit(GLF_QUEUED, gflags))
  1458. *p++ = 'q';
  1459. if (test_bit(GLF_LRU, gflags))
  1460. *p++ = 'L';
  1461. if (gl->gl_object)
  1462. *p++ = 'o';
  1463. if (test_bit(GLF_BLOCKING, gflags))
  1464. *p++ = 'b';
  1465. *p = 0;
  1466. return buf;
  1467. }
  1468. /**
  1469. * __dump_glock - print information about a glock
  1470. * @seq: The seq_file struct
  1471. * @gl: the glock
  1472. *
  1473. * The file format is as follows:
  1474. * One line per object, capital letters are used to indicate objects
  1475. * G = glock, I = Inode, R = rgrp, H = holder. Glocks are not indented,
  1476. * other objects are indented by a single space and follow the glock to
  1477. * which they are related. Fields are indicated by lower case letters
  1478. * followed by a colon and the field value, except for strings which are in
  1479. * [] so that its possible to see if they are composed of spaces for
  1480. * example. The field's are n = number (id of the object), f = flags,
  1481. * t = type, s = state, r = refcount, e = error, p = pid.
  1482. *
  1483. * Returns: 0 on success, -ENOBUFS when we run out of space
  1484. */
  1485. static int __dump_glock(struct seq_file *seq, const struct gfs2_glock *gl)
  1486. {
  1487. const struct gfs2_glock_operations *glops = gl->gl_ops;
  1488. unsigned long long dtime;
  1489. const struct gfs2_holder *gh;
  1490. char gflags_buf[32];
  1491. int error = 0;
  1492. dtime = jiffies - gl->gl_demote_time;
  1493. dtime *= 1000000/HZ; /* demote time in uSec */
  1494. if (!test_bit(GLF_DEMOTE, &gl->gl_flags))
  1495. dtime = 0;
  1496. gfs2_print_dbg(seq, "G: s:%s n:%u/%llx f:%s t:%s d:%s/%llu a:%d v:%d r:%d m:%ld\n",
  1497. state2str(gl->gl_state),
  1498. gl->gl_name.ln_type,
  1499. (unsigned long long)gl->gl_name.ln_number,
  1500. gflags2str(gflags_buf, gl),
  1501. state2str(gl->gl_target),
  1502. state2str(gl->gl_demote_state), dtime,
  1503. atomic_read(&gl->gl_ail_count),
  1504. atomic_read(&gl->gl_revokes),
  1505. atomic_read(&gl->gl_ref), gl->gl_hold_time);
  1506. list_for_each_entry(gh, &gl->gl_holders, gh_list) {
  1507. error = dump_holder(seq, gh);
  1508. if (error)
  1509. goto out;
  1510. }
  1511. if (gl->gl_state != LM_ST_UNLOCKED && glops->go_dump)
  1512. error = glops->go_dump(seq, gl);
  1513. out:
  1514. return error;
  1515. }
  1516. static int gfs2_glstats_seq_show(struct seq_file *seq, void *iter_ptr)
  1517. {
  1518. struct gfs2_glock *gl = iter_ptr;
  1519. seq_printf(seq, "G: n:%u/%llx rtt:%lld/%lld rttb:%lld/%lld irt:%lld/%lld dcnt: %lld qcnt: %lld\n",
  1520. gl->gl_name.ln_type,
  1521. (unsigned long long)gl->gl_name.ln_number,
  1522. (long long)gl->gl_stats.stats[GFS2_LKS_SRTT],
  1523. (long long)gl->gl_stats.stats[GFS2_LKS_SRTTVAR],
  1524. (long long)gl->gl_stats.stats[GFS2_LKS_SRTTB],
  1525. (long long)gl->gl_stats.stats[GFS2_LKS_SRTTVARB],
  1526. (long long)gl->gl_stats.stats[GFS2_LKS_SIRT],
  1527. (long long)gl->gl_stats.stats[GFS2_LKS_SIRTVAR],
  1528. (long long)gl->gl_stats.stats[GFS2_LKS_DCOUNT],
  1529. (long long)gl->gl_stats.stats[GFS2_LKS_QCOUNT]);
  1530. return 0;
  1531. }
  1532. static const char *gfs2_gltype[] = {
  1533. "type",
  1534. "reserved",
  1535. "nondisk",
  1536. "inode",
  1537. "rgrp",
  1538. "meta",
  1539. "iopen",
  1540. "flock",
  1541. "plock",
  1542. "quota",
  1543. "journal",
  1544. };
  1545. static const char *gfs2_stype[] = {
  1546. [GFS2_LKS_SRTT] = "srtt",
  1547. [GFS2_LKS_SRTTVAR] = "srttvar",
  1548. [GFS2_LKS_SRTTB] = "srttb",
  1549. [GFS2_LKS_SRTTVARB] = "srttvarb",
  1550. [GFS2_LKS_SIRT] = "sirt",
  1551. [GFS2_LKS_SIRTVAR] = "sirtvar",
  1552. [GFS2_LKS_DCOUNT] = "dlm",
  1553. [GFS2_LKS_QCOUNT] = "queue",
  1554. };
  1555. #define GFS2_NR_SBSTATS (ARRAY_SIZE(gfs2_gltype) * ARRAY_SIZE(gfs2_stype))
  1556. static int gfs2_sbstats_seq_show(struct seq_file *seq, void *iter_ptr)
  1557. {
  1558. struct gfs2_glock_iter *gi = seq->private;
  1559. struct gfs2_sbd *sdp = gi->sdp;
  1560. unsigned index = gi->hash >> 3;
  1561. unsigned subindex = gi->hash & 0x07;
  1562. s64 value;
  1563. int i;
  1564. if (index == 0 && subindex != 0)
  1565. return 0;
  1566. seq_printf(seq, "%-10s %8s:", gfs2_gltype[index],
  1567. (index == 0) ? "cpu": gfs2_stype[subindex]);
  1568. for_each_possible_cpu(i) {
  1569. const struct gfs2_pcpu_lkstats *lkstats = per_cpu_ptr(sdp->sd_lkstats, i);
  1570. if (index == 0) {
  1571. value = i;
  1572. } else {
  1573. value = lkstats->lkstats[index - 1].stats[subindex];
  1574. }
  1575. seq_printf(seq, " %15lld", (long long)value);
  1576. }
  1577. seq_putc(seq, '\n');
  1578. return 0;
  1579. }
  1580. int __init gfs2_glock_init(void)
  1581. {
  1582. unsigned i;
  1583. for(i = 0; i < GFS2_GL_HASH_SIZE; i++) {
  1584. INIT_HLIST_BL_HEAD(&gl_hash_table[i]);
  1585. }
  1586. glock_workqueue = alloc_workqueue("glock_workqueue", WQ_MEM_RECLAIM |
  1587. WQ_HIGHPRI | WQ_FREEZABLE, 0);
  1588. if (IS_ERR(glock_workqueue))
  1589. return PTR_ERR(glock_workqueue);
  1590. gfs2_delete_workqueue = alloc_workqueue("delete_workqueue",
  1591. WQ_MEM_RECLAIM | WQ_FREEZABLE,
  1592. 0);
  1593. if (IS_ERR(gfs2_delete_workqueue)) {
  1594. destroy_workqueue(glock_workqueue);
  1595. return PTR_ERR(gfs2_delete_workqueue);
  1596. }
  1597. register_shrinker(&glock_shrinker);
  1598. return 0;
  1599. }
  1600. void gfs2_glock_exit(void)
  1601. {
  1602. unregister_shrinker(&glock_shrinker);
  1603. destroy_workqueue(glock_workqueue);
  1604. destroy_workqueue(gfs2_delete_workqueue);
  1605. }
  1606. static inline struct gfs2_glock *glock_hash_chain(unsigned hash)
  1607. {
  1608. return hlist_bl_entry(hlist_bl_first_rcu(&gl_hash_table[hash]),
  1609. struct gfs2_glock, gl_list);
  1610. }
  1611. static inline struct gfs2_glock *glock_hash_next(struct gfs2_glock *gl)
  1612. {
  1613. return hlist_bl_entry(rcu_dereference(gl->gl_list.next),
  1614. struct gfs2_glock, gl_list);
  1615. }
  1616. static int gfs2_glock_iter_next(struct gfs2_glock_iter *gi)
  1617. {
  1618. struct gfs2_glock *gl;
  1619. do {
  1620. gl = gi->gl;
  1621. if (gl) {
  1622. gi->gl = glock_hash_next(gl);
  1623. } else {
  1624. gi->gl = glock_hash_chain(gi->hash);
  1625. }
  1626. while (gi->gl == NULL) {
  1627. gi->hash++;
  1628. if (gi->hash >= GFS2_GL_HASH_SIZE) {
  1629. rcu_read_unlock();
  1630. return 1;
  1631. }
  1632. gi->gl = glock_hash_chain(gi->hash);
  1633. }
  1634. /* Skip entries for other sb and dead entries */
  1635. } while (gi->sdp != gi->gl->gl_sbd || atomic_read(&gi->gl->gl_ref) == 0);
  1636. return 0;
  1637. }
  1638. static void *gfs2_glock_seq_start(struct seq_file *seq, loff_t *pos)
  1639. {
  1640. struct gfs2_glock_iter *gi = seq->private;
  1641. loff_t n = *pos;
  1642. gi->hash = 0;
  1643. rcu_read_lock();
  1644. do {
  1645. if (gfs2_glock_iter_next(gi))
  1646. return NULL;
  1647. } while (n--);
  1648. return gi->gl;
  1649. }
  1650. static void *gfs2_glock_seq_next(struct seq_file *seq, void *iter_ptr,
  1651. loff_t *pos)
  1652. {
  1653. struct gfs2_glock_iter *gi = seq->private;
  1654. (*pos)++;
  1655. if (gfs2_glock_iter_next(gi))
  1656. return NULL;
  1657. return gi->gl;
  1658. }
  1659. static void gfs2_glock_seq_stop(struct seq_file *seq, void *iter_ptr)
  1660. {
  1661. struct gfs2_glock_iter *gi = seq->private;
  1662. if (gi->gl)
  1663. rcu_read_unlock();
  1664. gi->gl = NULL;
  1665. }
  1666. static int gfs2_glock_seq_show(struct seq_file *seq, void *iter_ptr)
  1667. {
  1668. return dump_glock(seq, iter_ptr);
  1669. }
  1670. static void *gfs2_sbstats_seq_start(struct seq_file *seq, loff_t *pos)
  1671. {
  1672. struct gfs2_glock_iter *gi = seq->private;
  1673. gi->hash = *pos;
  1674. if (*pos >= GFS2_NR_SBSTATS)
  1675. return NULL;
  1676. preempt_disable();
  1677. return SEQ_START_TOKEN;
  1678. }
  1679. static void *gfs2_sbstats_seq_next(struct seq_file *seq, void *iter_ptr,
  1680. loff_t *pos)
  1681. {
  1682. struct gfs2_glock_iter *gi = seq->private;
  1683. (*pos)++;
  1684. gi->hash++;
  1685. if (gi->hash >= GFS2_NR_SBSTATS) {
  1686. preempt_enable();
  1687. return NULL;
  1688. }
  1689. return SEQ_START_TOKEN;
  1690. }
  1691. static void gfs2_sbstats_seq_stop(struct seq_file *seq, void *iter_ptr)
  1692. {
  1693. preempt_enable();
  1694. }
  1695. static const struct seq_operations gfs2_glock_seq_ops = {
  1696. .start = gfs2_glock_seq_start,
  1697. .next = gfs2_glock_seq_next,
  1698. .stop = gfs2_glock_seq_stop,
  1699. .show = gfs2_glock_seq_show,
  1700. };
  1701. static const struct seq_operations gfs2_glstats_seq_ops = {
  1702. .start = gfs2_glock_seq_start,
  1703. .next = gfs2_glock_seq_next,
  1704. .stop = gfs2_glock_seq_stop,
  1705. .show = gfs2_glstats_seq_show,
  1706. };
  1707. static const struct seq_operations gfs2_sbstats_seq_ops = {
  1708. .start = gfs2_sbstats_seq_start,
  1709. .next = gfs2_sbstats_seq_next,
  1710. .stop = gfs2_sbstats_seq_stop,
  1711. .show = gfs2_sbstats_seq_show,
  1712. };
  1713. static int gfs2_glocks_open(struct inode *inode, struct file *file)
  1714. {
  1715. int ret = seq_open_private(file, &gfs2_glock_seq_ops,
  1716. sizeof(struct gfs2_glock_iter));
  1717. if (ret == 0) {
  1718. struct seq_file *seq = file->private_data;
  1719. struct gfs2_glock_iter *gi = seq->private;
  1720. gi->sdp = inode->i_private;
  1721. }
  1722. return ret;
  1723. }
  1724. static int gfs2_glstats_open(struct inode *inode, struct file *file)
  1725. {
  1726. int ret = seq_open_private(file, &gfs2_glstats_seq_ops,
  1727. sizeof(struct gfs2_glock_iter));
  1728. if (ret == 0) {
  1729. struct seq_file *seq = file->private_data;
  1730. struct gfs2_glock_iter *gi = seq->private;
  1731. gi->sdp = inode->i_private;
  1732. }
  1733. return ret;
  1734. }
  1735. static int gfs2_sbstats_open(struct inode *inode, struct file *file)
  1736. {
  1737. int ret = seq_open_private(file, &gfs2_sbstats_seq_ops,
  1738. sizeof(struct gfs2_glock_iter));
  1739. if (ret == 0) {
  1740. struct seq_file *seq = file->private_data;
  1741. struct gfs2_glock_iter *gi = seq->private;
  1742. gi->sdp = inode->i_private;
  1743. }
  1744. return ret;
  1745. }
  1746. static const struct file_operations gfs2_glocks_fops = {
  1747. .owner = THIS_MODULE,
  1748. .open = gfs2_glocks_open,
  1749. .read = seq_read,
  1750. .llseek = seq_lseek,
  1751. .release = seq_release_private,
  1752. };
  1753. static const struct file_operations gfs2_glstats_fops = {
  1754. .owner = THIS_MODULE,
  1755. .open = gfs2_glstats_open,
  1756. .read = seq_read,
  1757. .llseek = seq_lseek,
  1758. .release = seq_release_private,
  1759. };
  1760. static const struct file_operations gfs2_sbstats_fops = {
  1761. .owner = THIS_MODULE,
  1762. .open = gfs2_sbstats_open,
  1763. .read = seq_read,
  1764. .llseek = seq_lseek,
  1765. .release = seq_release_private,
  1766. };
  1767. int gfs2_create_debugfs_file(struct gfs2_sbd *sdp)
  1768. {
  1769. sdp->debugfs_dir = debugfs_create_dir(sdp->sd_table_name, gfs2_root);
  1770. if (!sdp->debugfs_dir)
  1771. return -ENOMEM;
  1772. sdp->debugfs_dentry_glocks = debugfs_create_file("glocks",
  1773. S_IFREG | S_IRUGO,
  1774. sdp->debugfs_dir, sdp,
  1775. &gfs2_glocks_fops);
  1776. if (!sdp->debugfs_dentry_glocks)
  1777. goto fail;
  1778. sdp->debugfs_dentry_glstats = debugfs_create_file("glstats",
  1779. S_IFREG | S_IRUGO,
  1780. sdp->debugfs_dir, sdp,
  1781. &gfs2_glstats_fops);
  1782. if (!sdp->debugfs_dentry_glstats)
  1783. goto fail;
  1784. sdp->debugfs_dentry_sbstats = debugfs_create_file("sbstats",
  1785. S_IFREG | S_IRUGO,
  1786. sdp->debugfs_dir, sdp,
  1787. &gfs2_sbstats_fops);
  1788. if (!sdp->debugfs_dentry_sbstats)
  1789. goto fail;
  1790. return 0;
  1791. fail:
  1792. gfs2_delete_debugfs_file(sdp);
  1793. return -ENOMEM;
  1794. }
  1795. void gfs2_delete_debugfs_file(struct gfs2_sbd *sdp)
  1796. {
  1797. if (sdp->debugfs_dir) {
  1798. if (sdp->debugfs_dentry_glocks) {
  1799. debugfs_remove(sdp->debugfs_dentry_glocks);
  1800. sdp->debugfs_dentry_glocks = NULL;
  1801. }
  1802. if (sdp->debugfs_dentry_glstats) {
  1803. debugfs_remove(sdp->debugfs_dentry_glstats);
  1804. sdp->debugfs_dentry_glstats = NULL;
  1805. }
  1806. if (sdp->debugfs_dentry_sbstats) {
  1807. debugfs_remove(sdp->debugfs_dentry_sbstats);
  1808. sdp->debugfs_dentry_sbstats = NULL;
  1809. }
  1810. debugfs_remove(sdp->debugfs_dir);
  1811. sdp->debugfs_dir = NULL;
  1812. }
  1813. }
  1814. int gfs2_register_debugfs(void)
  1815. {
  1816. gfs2_root = debugfs_create_dir("gfs2", NULL);
  1817. return gfs2_root ? 0 : -ENOMEM;
  1818. }
  1819. void gfs2_unregister_debugfs(void)
  1820. {
  1821. debugfs_remove(gfs2_root);
  1822. gfs2_root = NULL;
  1823. }