glock.c 54 KB

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