glock.c 51 KB

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