glock.c 52 KB

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