glock.c 47 KB

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