glock.c 47 KB

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