glock.c 55 KB

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