glock.c 51 KB

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