glock.c 47 KB

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