glock.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868
  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 <linux/rwsem.h>
  22. #include <asm/uaccess.h>
  23. #include <linux/seq_file.h>
  24. #include <linux/debugfs.h>
  25. #include <linux/kthread.h>
  26. #include <linux/freezer.h>
  27. #include <linux/workqueue.h>
  28. #include <linux/jiffies.h>
  29. #include "gfs2.h"
  30. #include "incore.h"
  31. #include "glock.h"
  32. #include "glops.h"
  33. #include "inode.h"
  34. #include "lops.h"
  35. #include "meta_io.h"
  36. #include "quota.h"
  37. #include "super.h"
  38. #include "util.h"
  39. #include "bmap.h"
  40. struct gfs2_gl_hash_bucket {
  41. struct hlist_head hb_list;
  42. };
  43. struct gfs2_glock_iter {
  44. int hash; /* hash bucket index */
  45. struct gfs2_sbd *sdp; /* incore superblock */
  46. struct gfs2_glock *gl; /* current glock struct */
  47. char string[512]; /* scratch space */
  48. };
  49. typedef void (*glock_examiner) (struct gfs2_glock * gl);
  50. static int gfs2_dump_lockstate(struct gfs2_sbd *sdp);
  51. static int __dump_glock(struct seq_file *seq, const struct gfs2_glock *gl);
  52. #define GLOCK_BUG_ON(gl,x) do { if (unlikely(x)) { __dump_glock(NULL, gl); BUG(); } } while(0)
  53. static void do_xmote(struct gfs2_glock *gl, struct gfs2_holder *gh, unsigned int target);
  54. static DECLARE_RWSEM(gfs2_umount_flush_sem);
  55. static struct dentry *gfs2_root;
  56. static struct workqueue_struct *glock_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 gfs2_gl_hash_bucket gl_hash_table[GFS2_GL_HASH_SIZE];
  64. static struct dentry *gfs2_root;
  65. /*
  66. * Despite what you might think, the numbers below are not arbitrary :-)
  67. * They are taken from the ipv4 routing hash code, which is well tested
  68. * and thus should be nearly optimal. Later on we might tweek the numbers
  69. * but for now this should be fine.
  70. *
  71. * The reason for putting the locks in a separate array from the list heads
  72. * is that we can have fewer locks than list heads and save memory. We use
  73. * the same hash function for both, but with a different hash mask.
  74. */
  75. #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK) || \
  76. defined(CONFIG_PROVE_LOCKING)
  77. #ifdef CONFIG_LOCKDEP
  78. # define GL_HASH_LOCK_SZ 256
  79. #else
  80. # if NR_CPUS >= 32
  81. # define GL_HASH_LOCK_SZ 4096
  82. # elif NR_CPUS >= 16
  83. # define GL_HASH_LOCK_SZ 2048
  84. # elif NR_CPUS >= 8
  85. # define GL_HASH_LOCK_SZ 1024
  86. # elif NR_CPUS >= 4
  87. # define GL_HASH_LOCK_SZ 512
  88. # else
  89. # define GL_HASH_LOCK_SZ 256
  90. # endif
  91. #endif
  92. /* We never want more locks than chains */
  93. #if GFS2_GL_HASH_SIZE < GL_HASH_LOCK_SZ
  94. # undef GL_HASH_LOCK_SZ
  95. # define GL_HASH_LOCK_SZ GFS2_GL_HASH_SIZE
  96. #endif
  97. static rwlock_t gl_hash_locks[GL_HASH_LOCK_SZ];
  98. static inline rwlock_t *gl_lock_addr(unsigned int x)
  99. {
  100. return &gl_hash_locks[x & (GL_HASH_LOCK_SZ-1)];
  101. }
  102. #else /* not SMP, so no spinlocks required */
  103. static inline rwlock_t *gl_lock_addr(unsigned int x)
  104. {
  105. return NULL;
  106. }
  107. #endif
  108. /**
  109. * gl_hash() - Turn glock number into hash bucket number
  110. * @lock: The glock number
  111. *
  112. * Returns: The number of the corresponding hash bucket
  113. */
  114. static unsigned int gl_hash(const struct gfs2_sbd *sdp,
  115. const struct lm_lockname *name)
  116. {
  117. unsigned int h;
  118. h = jhash(&name->ln_number, sizeof(u64), 0);
  119. h = jhash(&name->ln_type, sizeof(unsigned int), h);
  120. h = jhash(&sdp, sizeof(struct gfs2_sbd *), h);
  121. h &= GFS2_GL_HASH_MASK;
  122. return h;
  123. }
  124. /**
  125. * glock_free() - Perform a few checks and then release struct gfs2_glock
  126. * @gl: The glock to release
  127. *
  128. * Also calls lock module to release its internal structure for this glock.
  129. *
  130. */
  131. static void glock_free(struct gfs2_glock *gl)
  132. {
  133. struct gfs2_sbd *sdp = gl->gl_sbd;
  134. struct inode *aspace = gl->gl_aspace;
  135. if (aspace)
  136. gfs2_aspace_put(aspace);
  137. sdp->sd_lockstruct.ls_ops->lm_put_lock(gfs2_glock_cachep, gl);
  138. }
  139. /**
  140. * gfs2_glock_hold() - increment reference count on glock
  141. * @gl: The glock to hold
  142. *
  143. */
  144. static void gfs2_glock_hold(struct gfs2_glock *gl)
  145. {
  146. GLOCK_BUG_ON(gl, atomic_read(&gl->gl_ref) == 0);
  147. atomic_inc(&gl->gl_ref);
  148. }
  149. /**
  150. * gfs2_glock_schedule_for_reclaim - Add a glock to the reclaim list
  151. * @gl: the glock
  152. *
  153. */
  154. static void gfs2_glock_schedule_for_reclaim(struct gfs2_glock *gl)
  155. {
  156. spin_lock(&lru_lock);
  157. if (list_empty(&gl->gl_lru) && gl->gl_state != LM_ST_UNLOCKED) {
  158. list_add_tail(&gl->gl_lru, &lru_list);
  159. atomic_inc(&lru_count);
  160. }
  161. spin_unlock(&lru_lock);
  162. }
  163. /**
  164. * gfs2_glock_put() - Decrement reference count on glock
  165. * @gl: The glock to put
  166. *
  167. */
  168. int gfs2_glock_put(struct gfs2_glock *gl)
  169. {
  170. int rv = 0;
  171. write_lock(gl_lock_addr(gl->gl_hash));
  172. if (atomic_dec_and_test(&gl->gl_ref)) {
  173. hlist_del(&gl->gl_list);
  174. write_unlock(gl_lock_addr(gl->gl_hash));
  175. spin_lock(&lru_lock);
  176. if (!list_empty(&gl->gl_lru)) {
  177. list_del_init(&gl->gl_lru);
  178. atomic_dec(&lru_count);
  179. }
  180. spin_unlock(&lru_lock);
  181. GLOCK_BUG_ON(gl, !list_empty(&gl->gl_holders));
  182. glock_free(gl);
  183. rv = 1;
  184. goto out;
  185. }
  186. /* 1 for being hashed, 1 for having state != LM_ST_UNLOCKED */
  187. if (atomic_read(&gl->gl_ref) == 2)
  188. gfs2_glock_schedule_for_reclaim(gl);
  189. write_unlock(gl_lock_addr(gl->gl_hash));
  190. out:
  191. return rv;
  192. }
  193. /**
  194. * search_bucket() - Find struct gfs2_glock by lock number
  195. * @bucket: the bucket to search
  196. * @name: The lock name
  197. *
  198. * Returns: NULL, or the struct gfs2_glock with the requested number
  199. */
  200. static struct gfs2_glock *search_bucket(unsigned int hash,
  201. const struct gfs2_sbd *sdp,
  202. const struct lm_lockname *name)
  203. {
  204. struct gfs2_glock *gl;
  205. struct hlist_node *h;
  206. hlist_for_each_entry(gl, h, &gl_hash_table[hash].hb_list, gl_list) {
  207. if (!lm_name_equal(&gl->gl_name, name))
  208. continue;
  209. if (gl->gl_sbd != sdp)
  210. continue;
  211. atomic_inc(&gl->gl_ref);
  212. return gl;
  213. }
  214. return NULL;
  215. }
  216. /**
  217. * may_grant - check if its ok to grant a new lock
  218. * @gl: The glock
  219. * @gh: The lock request which we wish to grant
  220. *
  221. * Returns: true if its ok to grant the lock
  222. */
  223. static inline int may_grant(const struct gfs2_glock *gl, const struct gfs2_holder *gh)
  224. {
  225. const struct gfs2_holder *gh_head = list_entry(gl->gl_holders.next, const struct gfs2_holder, gh_list);
  226. if ((gh->gh_state == LM_ST_EXCLUSIVE ||
  227. gh_head->gh_state == LM_ST_EXCLUSIVE) && gh != gh_head)
  228. return 0;
  229. if (gl->gl_state == gh->gh_state)
  230. return 1;
  231. if (gh->gh_flags & GL_EXACT)
  232. return 0;
  233. if (gl->gl_state == LM_ST_EXCLUSIVE) {
  234. if (gh->gh_state == LM_ST_SHARED && gh_head->gh_state == LM_ST_SHARED)
  235. return 1;
  236. if (gh->gh_state == LM_ST_DEFERRED && gh_head->gh_state == LM_ST_DEFERRED)
  237. return 1;
  238. }
  239. if (gl->gl_state != LM_ST_UNLOCKED && (gh->gh_flags & LM_FLAG_ANY))
  240. return 1;
  241. return 0;
  242. }
  243. static void gfs2_holder_wake(struct gfs2_holder *gh)
  244. {
  245. clear_bit(HIF_WAIT, &gh->gh_iflags);
  246. smp_mb__after_clear_bit();
  247. wake_up_bit(&gh->gh_iflags, HIF_WAIT);
  248. }
  249. /**
  250. * do_promote - promote as many requests as possible on the current queue
  251. * @gl: The glock
  252. *
  253. * Returns: 1 if there is a blocked holder at the head of the list, or 2
  254. * if a type specific operation is underway.
  255. */
  256. static int do_promote(struct gfs2_glock *gl)
  257. __releases(&gl->gl_spin)
  258. __acquires(&gl->gl_spin)
  259. {
  260. const struct gfs2_glock_operations *glops = gl->gl_ops;
  261. struct gfs2_holder *gh, *tmp;
  262. int ret;
  263. restart:
  264. list_for_each_entry_safe(gh, tmp, &gl->gl_holders, gh_list) {
  265. if (test_bit(HIF_HOLDER, &gh->gh_iflags))
  266. continue;
  267. if (may_grant(gl, gh)) {
  268. if (gh->gh_list.prev == &gl->gl_holders &&
  269. glops->go_lock) {
  270. spin_unlock(&gl->gl_spin);
  271. /* FIXME: eliminate this eventually */
  272. ret = glops->go_lock(gh);
  273. spin_lock(&gl->gl_spin);
  274. if (ret) {
  275. if (ret == 1)
  276. return 2;
  277. gh->gh_error = ret;
  278. list_del_init(&gh->gh_list);
  279. gfs2_holder_wake(gh);
  280. goto restart;
  281. }
  282. set_bit(HIF_HOLDER, &gh->gh_iflags);
  283. gfs2_holder_wake(gh);
  284. goto restart;
  285. }
  286. set_bit(HIF_HOLDER, &gh->gh_iflags);
  287. gfs2_holder_wake(gh);
  288. continue;
  289. }
  290. if (gh->gh_list.prev == &gl->gl_holders)
  291. return 1;
  292. break;
  293. }
  294. return 0;
  295. }
  296. /**
  297. * do_error - Something unexpected has happened during a lock request
  298. *
  299. */
  300. static inline void do_error(struct gfs2_glock *gl, const int ret)
  301. {
  302. struct gfs2_holder *gh, *tmp;
  303. list_for_each_entry_safe(gh, tmp, &gl->gl_holders, gh_list) {
  304. if (test_bit(HIF_HOLDER, &gh->gh_iflags))
  305. continue;
  306. if (ret & LM_OUT_ERROR)
  307. gh->gh_error = -EIO;
  308. else if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB))
  309. gh->gh_error = GLR_TRYFAILED;
  310. else
  311. continue;
  312. list_del_init(&gh->gh_list);
  313. gfs2_holder_wake(gh);
  314. }
  315. }
  316. /**
  317. * find_first_waiter - find the first gh that's waiting for the glock
  318. * @gl: the glock
  319. */
  320. static inline struct gfs2_holder *find_first_waiter(const struct gfs2_glock *gl)
  321. {
  322. struct gfs2_holder *gh;
  323. list_for_each_entry(gh, &gl->gl_holders, gh_list) {
  324. if (!test_bit(HIF_HOLDER, &gh->gh_iflags))
  325. return gh;
  326. }
  327. return NULL;
  328. }
  329. /**
  330. * state_change - record that the glock is now in a different state
  331. * @gl: the glock
  332. * @new_state the new state
  333. *
  334. */
  335. static void state_change(struct gfs2_glock *gl, unsigned int new_state)
  336. {
  337. int held1, held2;
  338. held1 = (gl->gl_state != LM_ST_UNLOCKED);
  339. held2 = (new_state != LM_ST_UNLOCKED);
  340. if (held1 != held2) {
  341. if (held2)
  342. gfs2_glock_hold(gl);
  343. else
  344. gfs2_glock_put(gl);
  345. }
  346. gl->gl_state = new_state;
  347. gl->gl_tchange = jiffies;
  348. }
  349. static void gfs2_demote_wake(struct gfs2_glock *gl)
  350. {
  351. gl->gl_demote_state = LM_ST_EXCLUSIVE;
  352. clear_bit(GLF_DEMOTE, &gl->gl_flags);
  353. smp_mb__after_clear_bit();
  354. wake_up_bit(&gl->gl_flags, GLF_DEMOTE);
  355. }
  356. /**
  357. * finish_xmote - The DLM has replied to one of our lock requests
  358. * @gl: The glock
  359. * @ret: The status from the DLM
  360. *
  361. */
  362. static void finish_xmote(struct gfs2_glock *gl, unsigned int ret)
  363. {
  364. const struct gfs2_glock_operations *glops = gl->gl_ops;
  365. struct gfs2_holder *gh;
  366. unsigned state = ret & LM_OUT_ST_MASK;
  367. int rv;
  368. spin_lock(&gl->gl_spin);
  369. state_change(gl, state);
  370. gh = find_first_waiter(gl);
  371. /* Demote to UN request arrived during demote to SH or DF */
  372. if (test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags) &&
  373. state != LM_ST_UNLOCKED && gl->gl_demote_state == LM_ST_UNLOCKED)
  374. gl->gl_target = LM_ST_UNLOCKED;
  375. /* Check for state != intended state */
  376. if (unlikely(state != gl->gl_target)) {
  377. if (gh && !test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags)) {
  378. /* move to back of queue and try next entry */
  379. if (ret & LM_OUT_CANCELED) {
  380. if ((gh->gh_flags & LM_FLAG_PRIORITY) == 0)
  381. list_move_tail(&gh->gh_list, &gl->gl_holders);
  382. gh = find_first_waiter(gl);
  383. gl->gl_target = gh->gh_state;
  384. goto retry;
  385. }
  386. /* Some error or failed "try lock" - report it */
  387. if ((ret & LM_OUT_ERROR) ||
  388. (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB))) {
  389. gl->gl_target = gl->gl_state;
  390. do_error(gl, ret);
  391. goto out;
  392. }
  393. }
  394. switch(state) {
  395. /* Unlocked due to conversion deadlock, try again */
  396. case LM_ST_UNLOCKED:
  397. retry:
  398. do_xmote(gl, gh, gl->gl_target);
  399. break;
  400. /* Conversion fails, unlock and try again */
  401. case LM_ST_SHARED:
  402. case LM_ST_DEFERRED:
  403. do_xmote(gl, gh, LM_ST_UNLOCKED);
  404. break;
  405. default: /* Everything else */
  406. printk(KERN_ERR "GFS2: wanted %u got %u\n", gl->gl_target, state);
  407. GLOCK_BUG_ON(gl, 1);
  408. }
  409. spin_unlock(&gl->gl_spin);
  410. gfs2_glock_put(gl);
  411. return;
  412. }
  413. /* Fast path - we got what we asked for */
  414. if (test_and_clear_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags))
  415. gfs2_demote_wake(gl);
  416. if (state != LM_ST_UNLOCKED) {
  417. if (glops->go_xmote_bh) {
  418. spin_unlock(&gl->gl_spin);
  419. rv = glops->go_xmote_bh(gl, gh);
  420. if (rv == -EAGAIN)
  421. return;
  422. spin_lock(&gl->gl_spin);
  423. if (rv) {
  424. do_error(gl, rv);
  425. goto out;
  426. }
  427. }
  428. rv = do_promote(gl);
  429. if (rv == 2)
  430. goto out_locked;
  431. }
  432. out:
  433. clear_bit(GLF_LOCK, &gl->gl_flags);
  434. out_locked:
  435. spin_unlock(&gl->gl_spin);
  436. gfs2_glock_put(gl);
  437. }
  438. static unsigned int gfs2_lm_lock(struct gfs2_sbd *sdp, void *lock,
  439. unsigned int req_state,
  440. unsigned int flags)
  441. {
  442. int ret = LM_OUT_ERROR;
  443. if (!sdp->sd_lockstruct.ls_ops->lm_lock)
  444. return req_state == LM_ST_UNLOCKED ? 0 : req_state;
  445. if (likely(!test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
  446. ret = sdp->sd_lockstruct.ls_ops->lm_lock(lock,
  447. req_state, flags);
  448. return ret;
  449. }
  450. /**
  451. * do_xmote - Calls the DLM to change the state of a lock
  452. * @gl: The lock state
  453. * @gh: The holder (only for promotes)
  454. * @target: The target lock state
  455. *
  456. */
  457. static void do_xmote(struct gfs2_glock *gl, struct gfs2_holder *gh, unsigned int target)
  458. __releases(&gl->gl_spin)
  459. __acquires(&gl->gl_spin)
  460. {
  461. const struct gfs2_glock_operations *glops = gl->gl_ops;
  462. struct gfs2_sbd *sdp = gl->gl_sbd;
  463. unsigned int lck_flags = gh ? gh->gh_flags : 0;
  464. int ret;
  465. lck_flags &= (LM_FLAG_TRY | LM_FLAG_TRY_1CB | LM_FLAG_NOEXP |
  466. LM_FLAG_PRIORITY);
  467. BUG_ON(gl->gl_state == target);
  468. BUG_ON(gl->gl_state == gl->gl_target);
  469. if ((target == LM_ST_UNLOCKED || target == LM_ST_DEFERRED) &&
  470. glops->go_inval) {
  471. set_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags);
  472. do_error(gl, 0); /* Fail queued try locks */
  473. }
  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 (target != LM_ST_UNLOCKED && (gl->gl_state == LM_ST_SHARED ||
  482. gl->gl_state == LM_ST_DEFERRED) &&
  483. !(lck_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)))
  484. lck_flags |= LM_FLAG_TRY_1CB;
  485. ret = gfs2_lm_lock(sdp, gl, target, lck_flags);
  486. if (!(ret & LM_OUT_ASYNC)) {
  487. finish_xmote(gl, ret);
  488. gfs2_glock_hold(gl);
  489. if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
  490. gfs2_glock_put(gl);
  491. } else {
  492. GLOCK_BUG_ON(gl, ret != LM_OUT_ASYNC);
  493. }
  494. spin_lock(&gl->gl_spin);
  495. }
  496. /**
  497. * find_first_holder - find the first "holder" gh
  498. * @gl: the glock
  499. */
  500. static inline struct gfs2_holder *find_first_holder(const struct gfs2_glock *gl)
  501. {
  502. struct gfs2_holder *gh;
  503. if (!list_empty(&gl->gl_holders)) {
  504. gh = list_entry(gl->gl_holders.next, struct gfs2_holder, gh_list);
  505. if (test_bit(HIF_HOLDER, &gh->gh_iflags))
  506. return gh;
  507. }
  508. return NULL;
  509. }
  510. /**
  511. * run_queue - do all outstanding tasks related to a glock
  512. * @gl: The glock in question
  513. * @nonblock: True if we must not block in run_queue
  514. *
  515. */
  516. static void run_queue(struct gfs2_glock *gl, const int nonblock)
  517. __releases(&gl->gl_spin)
  518. __acquires(&gl->gl_spin)
  519. {
  520. struct gfs2_holder *gh = NULL;
  521. int ret;
  522. if (test_and_set_bit(GLF_LOCK, &gl->gl_flags))
  523. return;
  524. GLOCK_BUG_ON(gl, test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags));
  525. if (test_bit(GLF_DEMOTE, &gl->gl_flags) &&
  526. gl->gl_demote_state != gl->gl_state) {
  527. if (find_first_holder(gl))
  528. goto out_unlock;
  529. if (nonblock)
  530. goto out_sched;
  531. set_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags);
  532. GLOCK_BUG_ON(gl, gl->gl_demote_state == LM_ST_EXCLUSIVE);
  533. gl->gl_target = gl->gl_demote_state;
  534. } else {
  535. if (test_bit(GLF_DEMOTE, &gl->gl_flags))
  536. gfs2_demote_wake(gl);
  537. ret = do_promote(gl);
  538. if (ret == 0)
  539. goto out_unlock;
  540. if (ret == 2)
  541. goto out;
  542. gh = find_first_waiter(gl);
  543. gl->gl_target = gh->gh_state;
  544. if (!(gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)))
  545. do_error(gl, 0); /* Fail queued try locks */
  546. }
  547. do_xmote(gl, gh, gl->gl_target);
  548. out:
  549. return;
  550. out_sched:
  551. gfs2_glock_hold(gl);
  552. if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
  553. gfs2_glock_put(gl);
  554. out_unlock:
  555. clear_bit(GLF_LOCK, &gl->gl_flags);
  556. goto out;
  557. }
  558. static void glock_work_func(struct work_struct *work)
  559. {
  560. unsigned long delay = 0;
  561. struct gfs2_glock *gl = container_of(work, struct gfs2_glock, gl_work.work);
  562. if (test_and_clear_bit(GLF_REPLY_PENDING, &gl->gl_flags))
  563. finish_xmote(gl, gl->gl_reply);
  564. down_read(&gfs2_umount_flush_sem);
  565. spin_lock(&gl->gl_spin);
  566. if (test_and_clear_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) &&
  567. gl->gl_state != LM_ST_UNLOCKED &&
  568. gl->gl_demote_state != LM_ST_EXCLUSIVE) {
  569. unsigned long holdtime, now = jiffies;
  570. holdtime = gl->gl_tchange + gl->gl_ops->go_min_hold_time;
  571. if (time_before(now, holdtime))
  572. delay = holdtime - now;
  573. set_bit(delay ? GLF_PENDING_DEMOTE : GLF_DEMOTE, &gl->gl_flags);
  574. }
  575. run_queue(gl, 0);
  576. spin_unlock(&gl->gl_spin);
  577. up_read(&gfs2_umount_flush_sem);
  578. if (!delay ||
  579. queue_delayed_work(glock_workqueue, &gl->gl_work, delay) == 0)
  580. gfs2_glock_put(gl);
  581. }
  582. /**
  583. * gfs2_glock_get() - Get a glock, or create one if one doesn't exist
  584. * @sdp: The GFS2 superblock
  585. * @number: the lock number
  586. * @glops: The glock_operations to use
  587. * @create: If 0, don't create the glock if it doesn't exist
  588. * @glp: the glock is returned here
  589. *
  590. * This does not lock a glock, just finds/creates structures for one.
  591. *
  592. * Returns: errno
  593. */
  594. int gfs2_glock_get(struct gfs2_sbd *sdp, u64 number,
  595. const struct gfs2_glock_operations *glops, int create,
  596. struct gfs2_glock **glp)
  597. {
  598. struct lm_lockname name = { .ln_number = number, .ln_type = glops->go_type };
  599. struct gfs2_glock *gl, *tmp;
  600. unsigned int hash = gl_hash(sdp, &name);
  601. int error;
  602. read_lock(gl_lock_addr(hash));
  603. gl = search_bucket(hash, sdp, &name);
  604. read_unlock(gl_lock_addr(hash));
  605. *glp = gl;
  606. if (gl)
  607. return 0;
  608. if (!create)
  609. return -ENOENT;
  610. gl = kmem_cache_alloc(gfs2_glock_cachep, GFP_KERNEL);
  611. if (!gl)
  612. return -ENOMEM;
  613. gl->gl_flags = 0;
  614. gl->gl_name = name;
  615. atomic_set(&gl->gl_ref, 1);
  616. gl->gl_state = LM_ST_UNLOCKED;
  617. gl->gl_target = LM_ST_UNLOCKED;
  618. gl->gl_demote_state = LM_ST_EXCLUSIVE;
  619. gl->gl_hash = hash;
  620. gl->gl_ops = glops;
  621. snprintf(gl->gl_strname, GDLM_STRNAME_BYTES, "%8x%16llx", name.ln_type, (unsigned long long)number);
  622. memset(&gl->gl_lksb, 0, sizeof(struct dlm_lksb));
  623. gl->gl_lksb.sb_lvbptr = gl->gl_lvb;
  624. gl->gl_tchange = jiffies;
  625. gl->gl_object = NULL;
  626. gl->gl_sbd = sdp;
  627. gl->gl_aspace = NULL;
  628. INIT_DELAYED_WORK(&gl->gl_work, glock_work_func);
  629. /* If this glock protects actual on-disk data or metadata blocks,
  630. create a VFS inode to manage the pages/buffers holding them. */
  631. if (glops == &gfs2_inode_glops || glops == &gfs2_rgrp_glops) {
  632. gl->gl_aspace = gfs2_aspace_get(sdp);
  633. if (!gl->gl_aspace) {
  634. error = -ENOMEM;
  635. goto fail;
  636. }
  637. }
  638. write_lock(gl_lock_addr(hash));
  639. tmp = search_bucket(hash, sdp, &name);
  640. if (tmp) {
  641. write_unlock(gl_lock_addr(hash));
  642. glock_free(gl);
  643. gl = tmp;
  644. } else {
  645. hlist_add_head(&gl->gl_list, &gl_hash_table[hash].hb_list);
  646. write_unlock(gl_lock_addr(hash));
  647. }
  648. *glp = gl;
  649. return 0;
  650. fail:
  651. kmem_cache_free(gfs2_glock_cachep, gl);
  652. return error;
  653. }
  654. /**
  655. * gfs2_holder_init - initialize a struct gfs2_holder in the default way
  656. * @gl: the glock
  657. * @state: the state we're requesting
  658. * @flags: the modifier flags
  659. * @gh: the holder structure
  660. *
  661. */
  662. void gfs2_holder_init(struct gfs2_glock *gl, unsigned int state, unsigned flags,
  663. struct gfs2_holder *gh)
  664. {
  665. INIT_LIST_HEAD(&gh->gh_list);
  666. gh->gh_gl = gl;
  667. gh->gh_ip = (unsigned long)__builtin_return_address(0);
  668. gh->gh_owner_pid = get_pid(task_pid(current));
  669. gh->gh_state = state;
  670. gh->gh_flags = flags;
  671. gh->gh_error = 0;
  672. gh->gh_iflags = 0;
  673. gfs2_glock_hold(gl);
  674. }
  675. /**
  676. * gfs2_holder_reinit - reinitialize a struct gfs2_holder so we can requeue it
  677. * @state: the state we're requesting
  678. * @flags: the modifier flags
  679. * @gh: the holder structure
  680. *
  681. * Don't mess with the glock.
  682. *
  683. */
  684. void gfs2_holder_reinit(unsigned int state, unsigned flags, struct gfs2_holder *gh)
  685. {
  686. gh->gh_state = state;
  687. gh->gh_flags = flags;
  688. gh->gh_iflags = 0;
  689. gh->gh_ip = (unsigned long)__builtin_return_address(0);
  690. }
  691. /**
  692. * gfs2_holder_uninit - uninitialize a holder structure (drop glock reference)
  693. * @gh: the holder structure
  694. *
  695. */
  696. void gfs2_holder_uninit(struct gfs2_holder *gh)
  697. {
  698. put_pid(gh->gh_owner_pid);
  699. gfs2_glock_put(gh->gh_gl);
  700. gh->gh_gl = NULL;
  701. gh->gh_ip = 0;
  702. }
  703. static int just_schedule(void *word)
  704. {
  705. schedule();
  706. return 0;
  707. }
  708. static void wait_on_holder(struct gfs2_holder *gh)
  709. {
  710. might_sleep();
  711. wait_on_bit(&gh->gh_iflags, HIF_WAIT, just_schedule, TASK_UNINTERRUPTIBLE);
  712. }
  713. static void wait_on_demote(struct gfs2_glock *gl)
  714. {
  715. might_sleep();
  716. wait_on_bit(&gl->gl_flags, GLF_DEMOTE, just_schedule, TASK_UNINTERRUPTIBLE);
  717. }
  718. /**
  719. * handle_callback - process a demote request
  720. * @gl: the glock
  721. * @state: the state the caller wants us to change to
  722. *
  723. * There are only two requests that we are going to see in actual
  724. * practise: LM_ST_SHARED and LM_ST_UNLOCKED
  725. */
  726. static void handle_callback(struct gfs2_glock *gl, unsigned int state,
  727. unsigned long delay)
  728. {
  729. int bit = delay ? GLF_PENDING_DEMOTE : GLF_DEMOTE;
  730. set_bit(bit, &gl->gl_flags);
  731. if (gl->gl_demote_state == LM_ST_EXCLUSIVE) {
  732. gl->gl_demote_state = state;
  733. gl->gl_demote_time = jiffies;
  734. } else if (gl->gl_demote_state != LM_ST_UNLOCKED &&
  735. gl->gl_demote_state != state) {
  736. gl->gl_demote_state = LM_ST_UNLOCKED;
  737. }
  738. }
  739. /**
  740. * gfs2_glock_wait - wait on a glock acquisition
  741. * @gh: the glock holder
  742. *
  743. * Returns: 0 on success
  744. */
  745. int gfs2_glock_wait(struct gfs2_holder *gh)
  746. {
  747. wait_on_holder(gh);
  748. return gh->gh_error;
  749. }
  750. void gfs2_print_dbg(struct seq_file *seq, const char *fmt, ...)
  751. {
  752. va_list args;
  753. va_start(args, fmt);
  754. if (seq) {
  755. struct gfs2_glock_iter *gi = seq->private;
  756. vsprintf(gi->string, fmt, args);
  757. seq_printf(seq, gi->string);
  758. } else {
  759. printk(KERN_ERR " ");
  760. vprintk(fmt, args);
  761. }
  762. va_end(args);
  763. }
  764. /**
  765. * add_to_queue - Add a holder to the wait queue (but look for recursion)
  766. * @gh: the holder structure to add
  767. *
  768. * Eventually we should move the recursive locking trap to a
  769. * debugging option or something like that. This is the fast
  770. * path and needs to have the minimum number of distractions.
  771. *
  772. */
  773. static inline void add_to_queue(struct gfs2_holder *gh)
  774. __releases(&gl->gl_spin)
  775. __acquires(&gl->gl_spin)
  776. {
  777. struct gfs2_glock *gl = gh->gh_gl;
  778. struct gfs2_sbd *sdp = gl->gl_sbd;
  779. struct list_head *insert_pt = NULL;
  780. struct gfs2_holder *gh2;
  781. int try_lock = 0;
  782. BUG_ON(gh->gh_owner_pid == NULL);
  783. if (test_and_set_bit(HIF_WAIT, &gh->gh_iflags))
  784. BUG();
  785. if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) {
  786. if (test_bit(GLF_LOCK, &gl->gl_flags))
  787. try_lock = 1;
  788. if (test_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags))
  789. goto fail;
  790. }
  791. list_for_each_entry(gh2, &gl->gl_holders, gh_list) {
  792. if (unlikely(gh2->gh_owner_pid == gh->gh_owner_pid &&
  793. (gh->gh_gl->gl_ops->go_type != LM_TYPE_FLOCK)))
  794. goto trap_recursive;
  795. if (try_lock &&
  796. !(gh2->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) &&
  797. !may_grant(gl, gh)) {
  798. fail:
  799. gh->gh_error = GLR_TRYFAILED;
  800. gfs2_holder_wake(gh);
  801. return;
  802. }
  803. if (test_bit(HIF_HOLDER, &gh2->gh_iflags))
  804. continue;
  805. if (unlikely((gh->gh_flags & LM_FLAG_PRIORITY) && !insert_pt))
  806. insert_pt = &gh2->gh_list;
  807. }
  808. if (likely(insert_pt == NULL)) {
  809. list_add_tail(&gh->gh_list, &gl->gl_holders);
  810. if (unlikely(gh->gh_flags & LM_FLAG_PRIORITY))
  811. goto do_cancel;
  812. return;
  813. }
  814. list_add_tail(&gh->gh_list, insert_pt);
  815. do_cancel:
  816. gh = list_entry(gl->gl_holders.next, struct gfs2_holder, gh_list);
  817. if (!(gh->gh_flags & LM_FLAG_PRIORITY)) {
  818. spin_unlock(&gl->gl_spin);
  819. if (sdp->sd_lockstruct.ls_ops->lm_cancel)
  820. sdp->sd_lockstruct.ls_ops->lm_cancel(gl);
  821. spin_lock(&gl->gl_spin);
  822. }
  823. return;
  824. trap_recursive:
  825. print_symbol(KERN_ERR "original: %s\n", gh2->gh_ip);
  826. printk(KERN_ERR "pid: %d\n", pid_nr(gh2->gh_owner_pid));
  827. printk(KERN_ERR "lock type: %d req lock state : %d\n",
  828. gh2->gh_gl->gl_name.ln_type, gh2->gh_state);
  829. print_symbol(KERN_ERR "new: %s\n", gh->gh_ip);
  830. printk(KERN_ERR "pid: %d\n", pid_nr(gh->gh_owner_pid));
  831. printk(KERN_ERR "lock type: %d req lock state : %d\n",
  832. gh->gh_gl->gl_name.ln_type, gh->gh_state);
  833. __dump_glock(NULL, gl);
  834. BUG();
  835. }
  836. /**
  837. * gfs2_glock_nq - enqueue a struct gfs2_holder onto a glock (acquire a glock)
  838. * @gh: the holder structure
  839. *
  840. * if (gh->gh_flags & GL_ASYNC), this never returns an error
  841. *
  842. * Returns: 0, GLR_TRYFAILED, or errno on failure
  843. */
  844. int gfs2_glock_nq(struct gfs2_holder *gh)
  845. {
  846. struct gfs2_glock *gl = gh->gh_gl;
  847. struct gfs2_sbd *sdp = gl->gl_sbd;
  848. int error = 0;
  849. if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
  850. return -EIO;
  851. spin_lock(&gl->gl_spin);
  852. add_to_queue(gh);
  853. run_queue(gl, 1);
  854. spin_unlock(&gl->gl_spin);
  855. if (!(gh->gh_flags & GL_ASYNC))
  856. error = gfs2_glock_wait(gh);
  857. return error;
  858. }
  859. /**
  860. * gfs2_glock_poll - poll to see if an async request has been completed
  861. * @gh: the holder
  862. *
  863. * Returns: 1 if the request is ready to be gfs2_glock_wait()ed on
  864. */
  865. int gfs2_glock_poll(struct gfs2_holder *gh)
  866. {
  867. return test_bit(HIF_WAIT, &gh->gh_iflags) ? 0 : 1;
  868. }
  869. /**
  870. * gfs2_glock_dq - dequeue a struct gfs2_holder from a glock (release a glock)
  871. * @gh: the glock holder
  872. *
  873. */
  874. void gfs2_glock_dq(struct gfs2_holder *gh)
  875. {
  876. struct gfs2_glock *gl = gh->gh_gl;
  877. const struct gfs2_glock_operations *glops = gl->gl_ops;
  878. unsigned delay = 0;
  879. int fast_path = 0;
  880. spin_lock(&gl->gl_spin);
  881. if (gh->gh_flags & GL_NOCACHE)
  882. handle_callback(gl, LM_ST_UNLOCKED, 0);
  883. list_del_init(&gh->gh_list);
  884. if (find_first_holder(gl) == NULL) {
  885. if (glops->go_unlock) {
  886. GLOCK_BUG_ON(gl, test_and_set_bit(GLF_LOCK, &gl->gl_flags));
  887. spin_unlock(&gl->gl_spin);
  888. glops->go_unlock(gh);
  889. spin_lock(&gl->gl_spin);
  890. clear_bit(GLF_LOCK, &gl->gl_flags);
  891. }
  892. if (list_empty(&gl->gl_holders) &&
  893. !test_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) &&
  894. !test_bit(GLF_DEMOTE, &gl->gl_flags))
  895. fast_path = 1;
  896. }
  897. spin_unlock(&gl->gl_spin);
  898. if (likely(fast_path))
  899. return;
  900. gfs2_glock_hold(gl);
  901. if (test_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) &&
  902. !test_bit(GLF_DEMOTE, &gl->gl_flags))
  903. delay = gl->gl_ops->go_min_hold_time;
  904. if (queue_delayed_work(glock_workqueue, &gl->gl_work, delay) == 0)
  905. gfs2_glock_put(gl);
  906. }
  907. void gfs2_glock_dq_wait(struct gfs2_holder *gh)
  908. {
  909. struct gfs2_glock *gl = gh->gh_gl;
  910. gfs2_glock_dq(gh);
  911. wait_on_demote(gl);
  912. }
  913. /**
  914. * gfs2_glock_dq_uninit - dequeue a holder from a glock and initialize it
  915. * @gh: the holder structure
  916. *
  917. */
  918. void gfs2_glock_dq_uninit(struct gfs2_holder *gh)
  919. {
  920. gfs2_glock_dq(gh);
  921. gfs2_holder_uninit(gh);
  922. }
  923. /**
  924. * gfs2_glock_nq_num - acquire a glock based on lock number
  925. * @sdp: the filesystem
  926. * @number: the lock number
  927. * @glops: the glock operations for the type of glock
  928. * @state: the state to acquire the glock in
  929. * @flags: modifier flags for the aquisition
  930. * @gh: the struct gfs2_holder
  931. *
  932. * Returns: errno
  933. */
  934. int gfs2_glock_nq_num(struct gfs2_sbd *sdp, u64 number,
  935. const struct gfs2_glock_operations *glops,
  936. unsigned int state, int flags, struct gfs2_holder *gh)
  937. {
  938. struct gfs2_glock *gl;
  939. int error;
  940. error = gfs2_glock_get(sdp, number, glops, CREATE, &gl);
  941. if (!error) {
  942. error = gfs2_glock_nq_init(gl, state, flags, gh);
  943. gfs2_glock_put(gl);
  944. }
  945. return error;
  946. }
  947. /**
  948. * glock_compare - Compare two struct gfs2_glock structures for sorting
  949. * @arg_a: the first structure
  950. * @arg_b: the second structure
  951. *
  952. */
  953. static int glock_compare(const void *arg_a, const void *arg_b)
  954. {
  955. const struct gfs2_holder *gh_a = *(const struct gfs2_holder **)arg_a;
  956. const struct gfs2_holder *gh_b = *(const struct gfs2_holder **)arg_b;
  957. const struct lm_lockname *a = &gh_a->gh_gl->gl_name;
  958. const struct lm_lockname *b = &gh_b->gh_gl->gl_name;
  959. if (a->ln_number > b->ln_number)
  960. return 1;
  961. if (a->ln_number < b->ln_number)
  962. return -1;
  963. BUG_ON(gh_a->gh_gl->gl_ops->go_type == gh_b->gh_gl->gl_ops->go_type);
  964. return 0;
  965. }
  966. /**
  967. * nq_m_sync - synchonously acquire more than one glock in deadlock free order
  968. * @num_gh: the number of structures
  969. * @ghs: an array of struct gfs2_holder structures
  970. *
  971. * Returns: 0 on success (all glocks acquired),
  972. * errno on failure (no glocks acquired)
  973. */
  974. static int nq_m_sync(unsigned int num_gh, struct gfs2_holder *ghs,
  975. struct gfs2_holder **p)
  976. {
  977. unsigned int x;
  978. int error = 0;
  979. for (x = 0; x < num_gh; x++)
  980. p[x] = &ghs[x];
  981. sort(p, num_gh, sizeof(struct gfs2_holder *), glock_compare, NULL);
  982. for (x = 0; x < num_gh; x++) {
  983. p[x]->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
  984. error = gfs2_glock_nq(p[x]);
  985. if (error) {
  986. while (x--)
  987. gfs2_glock_dq(p[x]);
  988. break;
  989. }
  990. }
  991. return error;
  992. }
  993. /**
  994. * gfs2_glock_nq_m - acquire multiple glocks
  995. * @num_gh: the number of structures
  996. * @ghs: an array of struct gfs2_holder structures
  997. *
  998. *
  999. * Returns: 0 on success (all glocks acquired),
  1000. * errno on failure (no glocks acquired)
  1001. */
  1002. int gfs2_glock_nq_m(unsigned int num_gh, struct gfs2_holder *ghs)
  1003. {
  1004. struct gfs2_holder *tmp[4];
  1005. struct gfs2_holder **pph = tmp;
  1006. int error = 0;
  1007. switch(num_gh) {
  1008. case 0:
  1009. return 0;
  1010. case 1:
  1011. ghs->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
  1012. return gfs2_glock_nq(ghs);
  1013. default:
  1014. if (num_gh <= 4)
  1015. break;
  1016. pph = kmalloc(num_gh * sizeof(struct gfs2_holder *), GFP_NOFS);
  1017. if (!pph)
  1018. return -ENOMEM;
  1019. }
  1020. error = nq_m_sync(num_gh, ghs, pph);
  1021. if (pph != tmp)
  1022. kfree(pph);
  1023. return error;
  1024. }
  1025. /**
  1026. * gfs2_glock_dq_m - release multiple glocks
  1027. * @num_gh: the number of structures
  1028. * @ghs: an array of struct gfs2_holder structures
  1029. *
  1030. */
  1031. void gfs2_glock_dq_m(unsigned int num_gh, struct gfs2_holder *ghs)
  1032. {
  1033. unsigned int x;
  1034. for (x = 0; x < num_gh; x++)
  1035. gfs2_glock_dq(&ghs[x]);
  1036. }
  1037. /**
  1038. * gfs2_glock_dq_uninit_m - release multiple glocks
  1039. * @num_gh: the number of structures
  1040. * @ghs: an array of struct gfs2_holder structures
  1041. *
  1042. */
  1043. void gfs2_glock_dq_uninit_m(unsigned int num_gh, struct gfs2_holder *ghs)
  1044. {
  1045. unsigned int x;
  1046. for (x = 0; x < num_gh; x++)
  1047. gfs2_glock_dq_uninit(&ghs[x]);
  1048. }
  1049. void gfs2_glock_cb(struct gfs2_glock *gl, unsigned int state)
  1050. {
  1051. unsigned long delay = 0;
  1052. unsigned long holdtime;
  1053. unsigned long now = jiffies;
  1054. gfs2_glock_hold(gl);
  1055. holdtime = gl->gl_tchange + gl->gl_ops->go_min_hold_time;
  1056. if (time_before(now, holdtime))
  1057. delay = holdtime - now;
  1058. if (test_bit(GLF_REPLY_PENDING, &gl->gl_flags))
  1059. delay = gl->gl_ops->go_min_hold_time;
  1060. spin_lock(&gl->gl_spin);
  1061. handle_callback(gl, state, delay);
  1062. spin_unlock(&gl->gl_spin);
  1063. if (queue_delayed_work(glock_workqueue, &gl->gl_work, delay) == 0)
  1064. gfs2_glock_put(gl);
  1065. }
  1066. /**
  1067. * gfs2_glock_complete - Callback used by locking
  1068. * @gl: Pointer to the glock
  1069. * @ret: The return value from the dlm
  1070. *
  1071. */
  1072. void gfs2_glock_complete(struct gfs2_glock *gl, int ret)
  1073. {
  1074. struct lm_lockstruct *ls = &gl->gl_sbd->sd_lockstruct;
  1075. gl->gl_reply = ret;
  1076. if (unlikely(test_bit(DFL_BLOCK_LOCKS, &ls->ls_flags))) {
  1077. struct gfs2_holder *gh;
  1078. spin_lock(&gl->gl_spin);
  1079. gh = find_first_waiter(gl);
  1080. if ((!(gh && (gh->gh_flags & LM_FLAG_NOEXP)) &&
  1081. (gl->gl_target != LM_ST_UNLOCKED)) ||
  1082. ((ret & ~LM_OUT_ST_MASK) != 0))
  1083. set_bit(GLF_FROZEN, &gl->gl_flags);
  1084. spin_unlock(&gl->gl_spin);
  1085. if (test_bit(GLF_FROZEN, &gl->gl_flags))
  1086. return;
  1087. }
  1088. set_bit(GLF_REPLY_PENDING, &gl->gl_flags);
  1089. gfs2_glock_hold(gl);
  1090. if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
  1091. gfs2_glock_put(gl);
  1092. }
  1093. /**
  1094. * demote_ok - Check to see if it's ok to unlock a glock
  1095. * @gl: the glock
  1096. *
  1097. * Returns: 1 if it's ok
  1098. */
  1099. static int demote_ok(const struct gfs2_glock *gl)
  1100. {
  1101. const struct gfs2_glock_operations *glops = gl->gl_ops;
  1102. if (gl->gl_state == LM_ST_UNLOCKED)
  1103. return 0;
  1104. if (!list_empty(&gl->gl_holders))
  1105. return 0;
  1106. if (glops->go_demote_ok)
  1107. return glops->go_demote_ok(gl);
  1108. return 1;
  1109. }
  1110. static int gfs2_shrink_glock_memory(int nr, gfp_t gfp_mask)
  1111. {
  1112. struct gfs2_glock *gl;
  1113. int may_demote;
  1114. int nr_skipped = 0;
  1115. int got_ref = 0;
  1116. LIST_HEAD(skipped);
  1117. if (nr == 0)
  1118. goto out;
  1119. if (!(gfp_mask & __GFP_FS))
  1120. return -1;
  1121. spin_lock(&lru_lock);
  1122. while(nr && !list_empty(&lru_list)) {
  1123. gl = list_entry(lru_list.next, struct gfs2_glock, gl_lru);
  1124. list_del_init(&gl->gl_lru);
  1125. atomic_dec(&lru_count);
  1126. /* Test for being demotable */
  1127. if (!test_and_set_bit(GLF_LOCK, &gl->gl_flags)) {
  1128. gfs2_glock_hold(gl);
  1129. got_ref = 1;
  1130. spin_unlock(&lru_lock);
  1131. spin_lock(&gl->gl_spin);
  1132. may_demote = demote_ok(gl);
  1133. spin_unlock(&gl->gl_spin);
  1134. clear_bit(GLF_LOCK, &gl->gl_flags);
  1135. if (may_demote) {
  1136. handle_callback(gl, LM_ST_UNLOCKED, 0);
  1137. nr--;
  1138. if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
  1139. gfs2_glock_put(gl);
  1140. got_ref = 0;
  1141. }
  1142. spin_lock(&lru_lock);
  1143. if (may_demote)
  1144. continue;
  1145. }
  1146. if (list_empty(&gl->gl_lru) &&
  1147. (atomic_read(&gl->gl_ref) <= (2 + got_ref))) {
  1148. nr_skipped++;
  1149. list_add(&gl->gl_lru, &skipped);
  1150. }
  1151. if (got_ref) {
  1152. spin_unlock(&lru_lock);
  1153. gfs2_glock_put(gl);
  1154. spin_lock(&lru_lock);
  1155. got_ref = 0;
  1156. }
  1157. }
  1158. list_splice(&skipped, &lru_list);
  1159. atomic_add(nr_skipped, &lru_count);
  1160. spin_unlock(&lru_lock);
  1161. out:
  1162. return (atomic_read(&lru_count) / 100) * sysctl_vfs_cache_pressure;
  1163. }
  1164. static struct shrinker glock_shrinker = {
  1165. .shrink = gfs2_shrink_glock_memory,
  1166. .seeks = DEFAULT_SEEKS,
  1167. };
  1168. /**
  1169. * examine_bucket - Call a function for glock in a hash bucket
  1170. * @examiner: the function
  1171. * @sdp: the filesystem
  1172. * @bucket: the bucket
  1173. *
  1174. * Returns: 1 if the bucket has entries
  1175. */
  1176. static int examine_bucket(glock_examiner examiner, struct gfs2_sbd *sdp,
  1177. unsigned int hash)
  1178. {
  1179. struct gfs2_glock *gl, *prev = NULL;
  1180. int has_entries = 0;
  1181. struct hlist_head *head = &gl_hash_table[hash].hb_list;
  1182. read_lock(gl_lock_addr(hash));
  1183. /* Can't use hlist_for_each_entry - don't want prefetch here */
  1184. if (hlist_empty(head))
  1185. goto out;
  1186. gl = list_entry(head->first, struct gfs2_glock, gl_list);
  1187. while(1) {
  1188. if (!sdp || gl->gl_sbd == sdp) {
  1189. gfs2_glock_hold(gl);
  1190. read_unlock(gl_lock_addr(hash));
  1191. if (prev)
  1192. gfs2_glock_put(prev);
  1193. prev = gl;
  1194. examiner(gl);
  1195. has_entries = 1;
  1196. read_lock(gl_lock_addr(hash));
  1197. }
  1198. if (gl->gl_list.next == NULL)
  1199. break;
  1200. gl = list_entry(gl->gl_list.next, struct gfs2_glock, gl_list);
  1201. }
  1202. out:
  1203. read_unlock(gl_lock_addr(hash));
  1204. if (prev)
  1205. gfs2_glock_put(prev);
  1206. cond_resched();
  1207. return has_entries;
  1208. }
  1209. /**
  1210. * thaw_glock - thaw out a glock which has an unprocessed reply waiting
  1211. * @gl: The glock to thaw
  1212. *
  1213. * N.B. When we freeze a glock, we leave a ref to the glock outstanding,
  1214. * so this has to result in the ref count being dropped by one.
  1215. */
  1216. static void thaw_glock(struct gfs2_glock *gl)
  1217. {
  1218. if (!test_and_clear_bit(GLF_FROZEN, &gl->gl_flags))
  1219. return;
  1220. set_bit(GLF_REPLY_PENDING, &gl->gl_flags);
  1221. gfs2_glock_hold(gl);
  1222. if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
  1223. gfs2_glock_put(gl);
  1224. }
  1225. /**
  1226. * clear_glock - look at a glock and see if we can free it from glock cache
  1227. * @gl: the glock to look at
  1228. *
  1229. */
  1230. static void clear_glock(struct gfs2_glock *gl)
  1231. {
  1232. spin_lock(&lru_lock);
  1233. if (!list_empty(&gl->gl_lru)) {
  1234. list_del_init(&gl->gl_lru);
  1235. atomic_dec(&lru_count);
  1236. }
  1237. spin_unlock(&lru_lock);
  1238. spin_lock(&gl->gl_spin);
  1239. if (find_first_holder(gl) == NULL && gl->gl_state != LM_ST_UNLOCKED)
  1240. handle_callback(gl, LM_ST_UNLOCKED, 0);
  1241. spin_unlock(&gl->gl_spin);
  1242. gfs2_glock_hold(gl);
  1243. if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
  1244. gfs2_glock_put(gl);
  1245. }
  1246. /**
  1247. * gfs2_glock_thaw - Thaw any frozen glocks
  1248. * @sdp: The super block
  1249. *
  1250. */
  1251. void gfs2_glock_thaw(struct gfs2_sbd *sdp)
  1252. {
  1253. unsigned x;
  1254. for (x = 0; x < GFS2_GL_HASH_SIZE; x++)
  1255. examine_bucket(thaw_glock, sdp, x);
  1256. }
  1257. /**
  1258. * gfs2_gl_hash_clear - Empty out the glock hash table
  1259. * @sdp: the filesystem
  1260. * @wait: wait until it's all gone
  1261. *
  1262. * Called when unmounting the filesystem.
  1263. */
  1264. void gfs2_gl_hash_clear(struct gfs2_sbd *sdp)
  1265. {
  1266. unsigned long t;
  1267. unsigned int x;
  1268. int cont;
  1269. t = jiffies;
  1270. for (;;) {
  1271. cont = 0;
  1272. for (x = 0; x < GFS2_GL_HASH_SIZE; x++) {
  1273. if (examine_bucket(clear_glock, sdp, x))
  1274. cont = 1;
  1275. }
  1276. if (!cont)
  1277. break;
  1278. if (time_after_eq(jiffies,
  1279. t + gfs2_tune_get(sdp, gt_stall_secs) * HZ)) {
  1280. fs_warn(sdp, "Unmount seems to be stalled. "
  1281. "Dumping lock state...\n");
  1282. gfs2_dump_lockstate(sdp);
  1283. t = jiffies;
  1284. }
  1285. down_write(&gfs2_umount_flush_sem);
  1286. invalidate_inodes(sdp->sd_vfs);
  1287. up_write(&gfs2_umount_flush_sem);
  1288. msleep(10);
  1289. }
  1290. }
  1291. void gfs2_glock_finish_truncate(struct gfs2_inode *ip)
  1292. {
  1293. struct gfs2_glock *gl = ip->i_gl;
  1294. int ret;
  1295. ret = gfs2_truncatei_resume(ip);
  1296. gfs2_assert_withdraw(gl->gl_sbd, ret == 0);
  1297. spin_lock(&gl->gl_spin);
  1298. clear_bit(GLF_LOCK, &gl->gl_flags);
  1299. run_queue(gl, 1);
  1300. spin_unlock(&gl->gl_spin);
  1301. }
  1302. static const char *state2str(unsigned state)
  1303. {
  1304. switch(state) {
  1305. case LM_ST_UNLOCKED:
  1306. return "UN";
  1307. case LM_ST_SHARED:
  1308. return "SH";
  1309. case LM_ST_DEFERRED:
  1310. return "DF";
  1311. case LM_ST_EXCLUSIVE:
  1312. return "EX";
  1313. }
  1314. return "??";
  1315. }
  1316. static const char *hflags2str(char *buf, unsigned flags, unsigned long iflags)
  1317. {
  1318. char *p = buf;
  1319. if (flags & LM_FLAG_TRY)
  1320. *p++ = 't';
  1321. if (flags & LM_FLAG_TRY_1CB)
  1322. *p++ = 'T';
  1323. if (flags & LM_FLAG_NOEXP)
  1324. *p++ = 'e';
  1325. if (flags & LM_FLAG_ANY)
  1326. *p++ = 'A';
  1327. if (flags & LM_FLAG_PRIORITY)
  1328. *p++ = 'p';
  1329. if (flags & GL_ASYNC)
  1330. *p++ = 'a';
  1331. if (flags & GL_EXACT)
  1332. *p++ = 'E';
  1333. if (flags & GL_NOCACHE)
  1334. *p++ = 'c';
  1335. if (test_bit(HIF_HOLDER, &iflags))
  1336. *p++ = 'H';
  1337. if (test_bit(HIF_WAIT, &iflags))
  1338. *p++ = 'W';
  1339. if (test_bit(HIF_FIRST, &iflags))
  1340. *p++ = 'F';
  1341. *p = 0;
  1342. return buf;
  1343. }
  1344. /**
  1345. * dump_holder - print information about a glock holder
  1346. * @seq: the seq_file struct
  1347. * @gh: the glock holder
  1348. *
  1349. * Returns: 0 on success, -ENOBUFS when we run out of space
  1350. */
  1351. static int dump_holder(struct seq_file *seq, const struct gfs2_holder *gh)
  1352. {
  1353. struct task_struct *gh_owner = NULL;
  1354. char buffer[KSYM_SYMBOL_LEN];
  1355. char flags_buf[32];
  1356. sprint_symbol(buffer, gh->gh_ip);
  1357. if (gh->gh_owner_pid)
  1358. gh_owner = pid_task(gh->gh_owner_pid, PIDTYPE_PID);
  1359. gfs2_print_dbg(seq, " H: s:%s f:%s e:%d p:%ld [%s] %s\n",
  1360. state2str(gh->gh_state),
  1361. hflags2str(flags_buf, gh->gh_flags, gh->gh_iflags),
  1362. gh->gh_error,
  1363. gh->gh_owner_pid ? (long)pid_nr(gh->gh_owner_pid) : -1,
  1364. gh_owner ? gh_owner->comm : "(ended)", buffer);
  1365. return 0;
  1366. }
  1367. static const char *gflags2str(char *buf, const unsigned long *gflags)
  1368. {
  1369. char *p = buf;
  1370. if (test_bit(GLF_LOCK, gflags))
  1371. *p++ = 'l';
  1372. if (test_bit(GLF_DEMOTE, gflags))
  1373. *p++ = 'D';
  1374. if (test_bit(GLF_PENDING_DEMOTE, gflags))
  1375. *p++ = 'd';
  1376. if (test_bit(GLF_DEMOTE_IN_PROGRESS, gflags))
  1377. *p++ = 'p';
  1378. if (test_bit(GLF_DIRTY, gflags))
  1379. *p++ = 'y';
  1380. if (test_bit(GLF_LFLUSH, gflags))
  1381. *p++ = 'f';
  1382. if (test_bit(GLF_INVALIDATE_IN_PROGRESS, gflags))
  1383. *p++ = 'i';
  1384. if (test_bit(GLF_REPLY_PENDING, gflags))
  1385. *p++ = 'r';
  1386. if (test_bit(GLF_INITIAL, gflags))
  1387. *p++ = 'I';
  1388. if (test_bit(GLF_FROZEN, gflags))
  1389. *p++ = 'F';
  1390. *p = 0;
  1391. return buf;
  1392. }
  1393. /**
  1394. * __dump_glock - print information about a glock
  1395. * @seq: The seq_file struct
  1396. * @gl: the glock
  1397. *
  1398. * The file format is as follows:
  1399. * One line per object, capital letters are used to indicate objects
  1400. * G = glock, I = Inode, R = rgrp, H = holder. Glocks are not indented,
  1401. * other objects are indented by a single space and follow the glock to
  1402. * which they are related. Fields are indicated by lower case letters
  1403. * followed by a colon and the field value, except for strings which are in
  1404. * [] so that its possible to see if they are composed of spaces for
  1405. * example. The field's are n = number (id of the object), f = flags,
  1406. * t = type, s = state, r = refcount, e = error, p = pid.
  1407. *
  1408. * Returns: 0 on success, -ENOBUFS when we run out of space
  1409. */
  1410. static int __dump_glock(struct seq_file *seq, const struct gfs2_glock *gl)
  1411. {
  1412. const struct gfs2_glock_operations *glops = gl->gl_ops;
  1413. unsigned long long dtime;
  1414. const struct gfs2_holder *gh;
  1415. char gflags_buf[32];
  1416. int error = 0;
  1417. dtime = jiffies - gl->gl_demote_time;
  1418. dtime *= 1000000/HZ; /* demote time in uSec */
  1419. if (!test_bit(GLF_DEMOTE, &gl->gl_flags))
  1420. dtime = 0;
  1421. gfs2_print_dbg(seq, "G: s:%s n:%u/%llu f:%s t:%s d:%s/%llu a:%d r:%d\n",
  1422. state2str(gl->gl_state),
  1423. gl->gl_name.ln_type,
  1424. (unsigned long long)gl->gl_name.ln_number,
  1425. gflags2str(gflags_buf, &gl->gl_flags),
  1426. state2str(gl->gl_target),
  1427. state2str(gl->gl_demote_state), dtime,
  1428. atomic_read(&gl->gl_ail_count),
  1429. atomic_read(&gl->gl_ref));
  1430. list_for_each_entry(gh, &gl->gl_holders, gh_list) {
  1431. error = dump_holder(seq, gh);
  1432. if (error)
  1433. goto out;
  1434. }
  1435. if (gl->gl_state != LM_ST_UNLOCKED && glops->go_dump)
  1436. error = glops->go_dump(seq, gl);
  1437. out:
  1438. return error;
  1439. }
  1440. static int dump_glock(struct seq_file *seq, struct gfs2_glock *gl)
  1441. {
  1442. int ret;
  1443. spin_lock(&gl->gl_spin);
  1444. ret = __dump_glock(seq, gl);
  1445. spin_unlock(&gl->gl_spin);
  1446. return ret;
  1447. }
  1448. /**
  1449. * gfs2_dump_lockstate - print out the current lockstate
  1450. * @sdp: the filesystem
  1451. * @ub: the buffer to copy the information into
  1452. *
  1453. * If @ub is NULL, dump the lockstate to the console.
  1454. *
  1455. */
  1456. static int gfs2_dump_lockstate(struct gfs2_sbd *sdp)
  1457. {
  1458. struct gfs2_glock *gl;
  1459. struct hlist_node *h;
  1460. unsigned int x;
  1461. int error = 0;
  1462. for (x = 0; x < GFS2_GL_HASH_SIZE; x++) {
  1463. read_lock(gl_lock_addr(x));
  1464. hlist_for_each_entry(gl, h, &gl_hash_table[x].hb_list, gl_list) {
  1465. if (gl->gl_sbd != sdp)
  1466. continue;
  1467. error = dump_glock(NULL, gl);
  1468. if (error)
  1469. break;
  1470. }
  1471. read_unlock(gl_lock_addr(x));
  1472. if (error)
  1473. break;
  1474. }
  1475. return error;
  1476. }
  1477. int __init gfs2_glock_init(void)
  1478. {
  1479. unsigned i;
  1480. for(i = 0; i < GFS2_GL_HASH_SIZE; i++) {
  1481. INIT_HLIST_HEAD(&gl_hash_table[i].hb_list);
  1482. }
  1483. #ifdef GL_HASH_LOCK_SZ
  1484. for(i = 0; i < GL_HASH_LOCK_SZ; i++) {
  1485. rwlock_init(&gl_hash_locks[i]);
  1486. }
  1487. #endif
  1488. glock_workqueue = create_workqueue("glock_workqueue");
  1489. if (IS_ERR(glock_workqueue))
  1490. return PTR_ERR(glock_workqueue);
  1491. register_shrinker(&glock_shrinker);
  1492. return 0;
  1493. }
  1494. void gfs2_glock_exit(void)
  1495. {
  1496. unregister_shrinker(&glock_shrinker);
  1497. destroy_workqueue(glock_workqueue);
  1498. }
  1499. static int gfs2_glock_iter_next(struct gfs2_glock_iter *gi)
  1500. {
  1501. struct gfs2_glock *gl;
  1502. restart:
  1503. read_lock(gl_lock_addr(gi->hash));
  1504. gl = gi->gl;
  1505. if (gl) {
  1506. gi->gl = hlist_entry(gl->gl_list.next,
  1507. struct gfs2_glock, gl_list);
  1508. } else {
  1509. gi->gl = hlist_entry(gl_hash_table[gi->hash].hb_list.first,
  1510. struct gfs2_glock, gl_list);
  1511. }
  1512. if (gi->gl)
  1513. gfs2_glock_hold(gi->gl);
  1514. read_unlock(gl_lock_addr(gi->hash));
  1515. if (gl)
  1516. gfs2_glock_put(gl);
  1517. while (gi->gl == NULL) {
  1518. gi->hash++;
  1519. if (gi->hash >= GFS2_GL_HASH_SIZE)
  1520. return 1;
  1521. read_lock(gl_lock_addr(gi->hash));
  1522. gi->gl = hlist_entry(gl_hash_table[gi->hash].hb_list.first,
  1523. struct gfs2_glock, gl_list);
  1524. if (gi->gl)
  1525. gfs2_glock_hold(gi->gl);
  1526. read_unlock(gl_lock_addr(gi->hash));
  1527. }
  1528. if (gi->sdp != gi->gl->gl_sbd)
  1529. goto restart;
  1530. return 0;
  1531. }
  1532. static void gfs2_glock_iter_free(struct gfs2_glock_iter *gi)
  1533. {
  1534. if (gi->gl)
  1535. gfs2_glock_put(gi->gl);
  1536. gi->gl = NULL;
  1537. }
  1538. static void *gfs2_glock_seq_start(struct seq_file *seq, loff_t *pos)
  1539. {
  1540. struct gfs2_glock_iter *gi = seq->private;
  1541. loff_t n = *pos;
  1542. gi->hash = 0;
  1543. do {
  1544. if (gfs2_glock_iter_next(gi)) {
  1545. gfs2_glock_iter_free(gi);
  1546. return NULL;
  1547. }
  1548. } while (n--);
  1549. return gi->gl;
  1550. }
  1551. static void *gfs2_glock_seq_next(struct seq_file *seq, void *iter_ptr,
  1552. loff_t *pos)
  1553. {
  1554. struct gfs2_glock_iter *gi = seq->private;
  1555. (*pos)++;
  1556. if (gfs2_glock_iter_next(gi)) {
  1557. gfs2_glock_iter_free(gi);
  1558. return NULL;
  1559. }
  1560. return gi->gl;
  1561. }
  1562. static void gfs2_glock_seq_stop(struct seq_file *seq, void *iter_ptr)
  1563. {
  1564. struct gfs2_glock_iter *gi = seq->private;
  1565. gfs2_glock_iter_free(gi);
  1566. }
  1567. static int gfs2_glock_seq_show(struct seq_file *seq, void *iter_ptr)
  1568. {
  1569. return dump_glock(seq, iter_ptr);
  1570. }
  1571. static const struct seq_operations gfs2_glock_seq_ops = {
  1572. .start = gfs2_glock_seq_start,
  1573. .next = gfs2_glock_seq_next,
  1574. .stop = gfs2_glock_seq_stop,
  1575. .show = gfs2_glock_seq_show,
  1576. };
  1577. static int gfs2_debugfs_open(struct inode *inode, struct file *file)
  1578. {
  1579. int ret = seq_open_private(file, &gfs2_glock_seq_ops,
  1580. sizeof(struct gfs2_glock_iter));
  1581. if (ret == 0) {
  1582. struct seq_file *seq = file->private_data;
  1583. struct gfs2_glock_iter *gi = seq->private;
  1584. gi->sdp = inode->i_private;
  1585. }
  1586. return ret;
  1587. }
  1588. static const struct file_operations gfs2_debug_fops = {
  1589. .owner = THIS_MODULE,
  1590. .open = gfs2_debugfs_open,
  1591. .read = seq_read,
  1592. .llseek = seq_lseek,
  1593. .release = seq_release_private,
  1594. };
  1595. int gfs2_create_debugfs_file(struct gfs2_sbd *sdp)
  1596. {
  1597. sdp->debugfs_dir = debugfs_create_dir(sdp->sd_table_name, gfs2_root);
  1598. if (!sdp->debugfs_dir)
  1599. return -ENOMEM;
  1600. sdp->debugfs_dentry_glocks = debugfs_create_file("glocks",
  1601. S_IFREG | S_IRUGO,
  1602. sdp->debugfs_dir, sdp,
  1603. &gfs2_debug_fops);
  1604. if (!sdp->debugfs_dentry_glocks)
  1605. return -ENOMEM;
  1606. return 0;
  1607. }
  1608. void gfs2_delete_debugfs_file(struct gfs2_sbd *sdp)
  1609. {
  1610. if (sdp && sdp->debugfs_dir) {
  1611. if (sdp->debugfs_dentry_glocks) {
  1612. debugfs_remove(sdp->debugfs_dentry_glocks);
  1613. sdp->debugfs_dentry_glocks = NULL;
  1614. }
  1615. debugfs_remove(sdp->debugfs_dir);
  1616. sdp->debugfs_dir = NULL;
  1617. }
  1618. }
  1619. int gfs2_register_debugfs(void)
  1620. {
  1621. gfs2_root = debugfs_create_dir("gfs2", NULL);
  1622. return gfs2_root ? 0 : -ENOMEM;
  1623. }
  1624. void gfs2_unregister_debugfs(void)
  1625. {
  1626. debugfs_remove(gfs2_root);
  1627. gfs2_root = NULL;
  1628. }