glock.c 47 KB

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