glock.c 51 KB

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