glock.c 45 KB

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