glock.c 51 KB

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