glock.c 51 KB

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