glock.c 52 KB

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