glock.c 51 KB

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