glock.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2006 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/completion.h>
  13. #include <linux/buffer_head.h>
  14. #include <linux/delay.h>
  15. #include <linux/sort.h>
  16. #include <linux/jhash.h>
  17. #include <linux/kallsyms.h>
  18. #include <linux/gfs2_ondisk.h>
  19. #include <linux/list.h>
  20. #include <linux/lm_interface.h>
  21. #include <linux/wait.h>
  22. #include <linux/module.h>
  23. #include <linux/rwsem.h>
  24. #include <asm/uaccess.h>
  25. #include <linux/seq_file.h>
  26. #include <linux/debugfs.h>
  27. #include <linux/module.h>
  28. #include <linux/kallsyms.h>
  29. #include "gfs2.h"
  30. #include "incore.h"
  31. #include "glock.h"
  32. #include "glops.h"
  33. #include "inode.h"
  34. #include "lm.h"
  35. #include "lops.h"
  36. #include "meta_io.h"
  37. #include "quota.h"
  38. #include "super.h"
  39. #include "util.h"
  40. struct gfs2_gl_hash_bucket {
  41. struct hlist_head hb_list;
  42. };
  43. struct glock_iter {
  44. int hash; /* hash bucket index */
  45. struct gfs2_sbd *sdp; /* incore superblock */
  46. struct gfs2_glock *gl; /* current glock struct */
  47. struct hlist_head *hb_list; /* current hash bucket ptr */
  48. struct seq_file *seq; /* sequence file for debugfs */
  49. char string[512]; /* scratch space */
  50. };
  51. typedef void (*glock_examiner) (struct gfs2_glock * gl);
  52. static int gfs2_dump_lockstate(struct gfs2_sbd *sdp);
  53. static int dump_glock(struct glock_iter *gi, struct gfs2_glock *gl);
  54. static void gfs2_glock_xmote_th(struct gfs2_glock *gl, struct gfs2_holder *gh);
  55. static void gfs2_glock_drop_th(struct gfs2_glock *gl);
  56. static DECLARE_RWSEM(gfs2_umount_flush_sem);
  57. static struct dentry *gfs2_root;
  58. #define GFS2_GL_HASH_SHIFT 15
  59. #define GFS2_GL_HASH_SIZE (1 << GFS2_GL_HASH_SHIFT)
  60. #define GFS2_GL_HASH_MASK (GFS2_GL_HASH_SIZE - 1)
  61. static struct gfs2_gl_hash_bucket gl_hash_table[GFS2_GL_HASH_SIZE];
  62. static struct dentry *gfs2_root;
  63. /*
  64. * Despite what you might think, the numbers below are not arbitrary :-)
  65. * They are taken from the ipv4 routing hash code, which is well tested
  66. * and thus should be nearly optimal. Later on we might tweek the numbers
  67. * but for now this should be fine.
  68. *
  69. * The reason for putting the locks in a separate array from the list heads
  70. * is that we can have fewer locks than list heads and save memory. We use
  71. * the same hash function for both, but with a different hash mask.
  72. */
  73. #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK) || \
  74. defined(CONFIG_PROVE_LOCKING)
  75. #ifdef CONFIG_LOCKDEP
  76. # define GL_HASH_LOCK_SZ 256
  77. #else
  78. # if NR_CPUS >= 32
  79. # define GL_HASH_LOCK_SZ 4096
  80. # elif NR_CPUS >= 16
  81. # define GL_HASH_LOCK_SZ 2048
  82. # elif NR_CPUS >= 8
  83. # define GL_HASH_LOCK_SZ 1024
  84. # elif NR_CPUS >= 4
  85. # define GL_HASH_LOCK_SZ 512
  86. # else
  87. # define GL_HASH_LOCK_SZ 256
  88. # endif
  89. #endif
  90. /* We never want more locks than chains */
  91. #if GFS2_GL_HASH_SIZE < GL_HASH_LOCK_SZ
  92. # undef GL_HASH_LOCK_SZ
  93. # define GL_HASH_LOCK_SZ GFS2_GL_HASH_SIZE
  94. #endif
  95. static rwlock_t gl_hash_locks[GL_HASH_LOCK_SZ];
  96. static inline rwlock_t *gl_lock_addr(unsigned int x)
  97. {
  98. return &gl_hash_locks[x & (GL_HASH_LOCK_SZ-1)];
  99. }
  100. #else /* not SMP, so no spinlocks required */
  101. static inline rwlock_t *gl_lock_addr(unsigned int x)
  102. {
  103. return NULL;
  104. }
  105. #endif
  106. /**
  107. * relaxed_state_ok - is a requested lock compatible with the current lock mode?
  108. * @actual: the current state of the lock
  109. * @requested: the lock state that was requested by the caller
  110. * @flags: the modifier flags passed in by the caller
  111. *
  112. * Returns: 1 if the locks are compatible, 0 otherwise
  113. */
  114. static inline int relaxed_state_ok(unsigned int actual, unsigned requested,
  115. int flags)
  116. {
  117. if (actual == requested)
  118. return 1;
  119. if (flags & GL_EXACT)
  120. return 0;
  121. if (actual == LM_ST_EXCLUSIVE && requested == LM_ST_SHARED)
  122. return 1;
  123. if (actual != LM_ST_UNLOCKED && (flags & LM_FLAG_ANY))
  124. return 1;
  125. return 0;
  126. }
  127. /**
  128. * gl_hash() - Turn glock number into hash bucket number
  129. * @lock: The glock number
  130. *
  131. * Returns: The number of the corresponding hash bucket
  132. */
  133. static unsigned int gl_hash(const struct gfs2_sbd *sdp,
  134. const struct lm_lockname *name)
  135. {
  136. unsigned int h;
  137. h = jhash(&name->ln_number, sizeof(u64), 0);
  138. h = jhash(&name->ln_type, sizeof(unsigned int), h);
  139. h = jhash(&sdp, sizeof(struct gfs2_sbd *), h);
  140. h &= GFS2_GL_HASH_MASK;
  141. return h;
  142. }
  143. /**
  144. * glock_free() - Perform a few checks and then release struct gfs2_glock
  145. * @gl: The glock to release
  146. *
  147. * Also calls lock module to release its internal structure for this glock.
  148. *
  149. */
  150. static void glock_free(struct gfs2_glock *gl)
  151. {
  152. struct gfs2_sbd *sdp = gl->gl_sbd;
  153. struct inode *aspace = gl->gl_aspace;
  154. gfs2_lm_put_lock(sdp, gl->gl_lock);
  155. if (aspace)
  156. gfs2_aspace_put(aspace);
  157. kmem_cache_free(gfs2_glock_cachep, gl);
  158. }
  159. /**
  160. * gfs2_glock_hold() - increment reference count on glock
  161. * @gl: The glock to hold
  162. *
  163. */
  164. void gfs2_glock_hold(struct gfs2_glock *gl)
  165. {
  166. atomic_inc(&gl->gl_ref);
  167. }
  168. /**
  169. * gfs2_glock_put() - Decrement reference count on glock
  170. * @gl: The glock to put
  171. *
  172. */
  173. int gfs2_glock_put(struct gfs2_glock *gl)
  174. {
  175. int rv = 0;
  176. struct gfs2_sbd *sdp = gl->gl_sbd;
  177. write_lock(gl_lock_addr(gl->gl_hash));
  178. if (atomic_dec_and_test(&gl->gl_ref)) {
  179. hlist_del(&gl->gl_list);
  180. write_unlock(gl_lock_addr(gl->gl_hash));
  181. BUG_ON(spin_is_locked(&gl->gl_spin));
  182. gfs2_assert(sdp, gl->gl_state == LM_ST_UNLOCKED);
  183. gfs2_assert(sdp, list_empty(&gl->gl_reclaim));
  184. gfs2_assert(sdp, list_empty(&gl->gl_holders));
  185. gfs2_assert(sdp, list_empty(&gl->gl_waiters1));
  186. gfs2_assert(sdp, list_empty(&gl->gl_waiters3));
  187. glock_free(gl);
  188. rv = 1;
  189. goto out;
  190. }
  191. write_unlock(gl_lock_addr(gl->gl_hash));
  192. out:
  193. return rv;
  194. }
  195. /**
  196. * search_bucket() - Find struct gfs2_glock by lock number
  197. * @bucket: the bucket to search
  198. * @name: The lock name
  199. *
  200. * Returns: NULL, or the struct gfs2_glock with the requested number
  201. */
  202. static struct gfs2_glock *search_bucket(unsigned int hash,
  203. const struct gfs2_sbd *sdp,
  204. const struct lm_lockname *name)
  205. {
  206. struct gfs2_glock *gl;
  207. struct hlist_node *h;
  208. hlist_for_each_entry(gl, h, &gl_hash_table[hash].hb_list, gl_list) {
  209. if (!lm_name_equal(&gl->gl_name, name))
  210. continue;
  211. if (gl->gl_sbd != sdp)
  212. continue;
  213. atomic_inc(&gl->gl_ref);
  214. return gl;
  215. }
  216. return NULL;
  217. }
  218. /**
  219. * gfs2_glock_find() - Find glock by lock number
  220. * @sdp: The GFS2 superblock
  221. * @name: The lock name
  222. *
  223. * Returns: NULL, or the struct gfs2_glock with the requested number
  224. */
  225. static struct gfs2_glock *gfs2_glock_find(const struct gfs2_sbd *sdp,
  226. const struct lm_lockname *name)
  227. {
  228. unsigned int hash = gl_hash(sdp, name);
  229. struct gfs2_glock *gl;
  230. read_lock(gl_lock_addr(hash));
  231. gl = search_bucket(hash, sdp, name);
  232. read_unlock(gl_lock_addr(hash));
  233. return gl;
  234. }
  235. /**
  236. * gfs2_glock_get() - Get a glock, or create one if one doesn't exist
  237. * @sdp: The GFS2 superblock
  238. * @number: the lock number
  239. * @glops: The glock_operations to use
  240. * @create: If 0, don't create the glock if it doesn't exist
  241. * @glp: the glock is returned here
  242. *
  243. * This does not lock a glock, just finds/creates structures for one.
  244. *
  245. * Returns: errno
  246. */
  247. int gfs2_glock_get(struct gfs2_sbd *sdp, u64 number,
  248. const struct gfs2_glock_operations *glops, int create,
  249. struct gfs2_glock **glp)
  250. {
  251. struct lm_lockname name = { .ln_number = number, .ln_type = glops->go_type };
  252. struct gfs2_glock *gl, *tmp;
  253. unsigned int hash = gl_hash(sdp, &name);
  254. int error;
  255. read_lock(gl_lock_addr(hash));
  256. gl = search_bucket(hash, sdp, &name);
  257. read_unlock(gl_lock_addr(hash));
  258. if (gl || !create) {
  259. *glp = gl;
  260. return 0;
  261. }
  262. gl = kmem_cache_alloc(gfs2_glock_cachep, GFP_KERNEL);
  263. if (!gl)
  264. return -ENOMEM;
  265. gl->gl_flags = 0;
  266. gl->gl_name = name;
  267. atomic_set(&gl->gl_ref, 1);
  268. gl->gl_state = LM_ST_UNLOCKED;
  269. gl->gl_hash = hash;
  270. gl->gl_owner_pid = 0;
  271. gl->gl_ip = 0;
  272. gl->gl_ops = glops;
  273. gl->gl_req_gh = NULL;
  274. gl->gl_req_bh = NULL;
  275. gl->gl_vn = 0;
  276. gl->gl_stamp = jiffies;
  277. gl->gl_object = NULL;
  278. gl->gl_sbd = sdp;
  279. gl->gl_aspace = NULL;
  280. lops_init_le(&gl->gl_le, &gfs2_glock_lops);
  281. /* If this glock protects actual on-disk data or metadata blocks,
  282. create a VFS inode to manage the pages/buffers holding them. */
  283. if (glops == &gfs2_inode_glops || glops == &gfs2_rgrp_glops) {
  284. gl->gl_aspace = gfs2_aspace_get(sdp);
  285. if (!gl->gl_aspace) {
  286. error = -ENOMEM;
  287. goto fail;
  288. }
  289. }
  290. error = gfs2_lm_get_lock(sdp, &name, &gl->gl_lock);
  291. if (error)
  292. goto fail_aspace;
  293. write_lock(gl_lock_addr(hash));
  294. tmp = search_bucket(hash, sdp, &name);
  295. if (tmp) {
  296. write_unlock(gl_lock_addr(hash));
  297. glock_free(gl);
  298. gl = tmp;
  299. } else {
  300. hlist_add_head(&gl->gl_list, &gl_hash_table[hash].hb_list);
  301. write_unlock(gl_lock_addr(hash));
  302. }
  303. *glp = gl;
  304. return 0;
  305. fail_aspace:
  306. if (gl->gl_aspace)
  307. gfs2_aspace_put(gl->gl_aspace);
  308. fail:
  309. kmem_cache_free(gfs2_glock_cachep, gl);
  310. return error;
  311. }
  312. /**
  313. * gfs2_holder_init - initialize a struct gfs2_holder in the default way
  314. * @gl: the glock
  315. * @state: the state we're requesting
  316. * @flags: the modifier flags
  317. * @gh: the holder structure
  318. *
  319. */
  320. void gfs2_holder_init(struct gfs2_glock *gl, unsigned int state, unsigned flags,
  321. struct gfs2_holder *gh)
  322. {
  323. INIT_LIST_HEAD(&gh->gh_list);
  324. gh->gh_gl = gl;
  325. gh->gh_ip = (unsigned long)__builtin_return_address(0);
  326. gh->gh_owner_pid = current->pid;
  327. gh->gh_state = state;
  328. gh->gh_flags = flags;
  329. gh->gh_error = 0;
  330. gh->gh_iflags = 0;
  331. gfs2_glock_hold(gl);
  332. }
  333. /**
  334. * gfs2_holder_reinit - reinitialize a struct gfs2_holder so we can requeue it
  335. * @state: the state we're requesting
  336. * @flags: the modifier flags
  337. * @gh: the holder structure
  338. *
  339. * Don't mess with the glock.
  340. *
  341. */
  342. void gfs2_holder_reinit(unsigned int state, unsigned flags, struct gfs2_holder *gh)
  343. {
  344. gh->gh_state = state;
  345. gh->gh_flags = flags;
  346. gh->gh_iflags = 0;
  347. gh->gh_ip = (unsigned long)__builtin_return_address(0);
  348. }
  349. /**
  350. * gfs2_holder_uninit - uninitialize a holder structure (drop glock reference)
  351. * @gh: the holder structure
  352. *
  353. */
  354. void gfs2_holder_uninit(struct gfs2_holder *gh)
  355. {
  356. gfs2_glock_put(gh->gh_gl);
  357. gh->gh_gl = NULL;
  358. gh->gh_ip = 0;
  359. }
  360. static void gfs2_holder_wake(struct gfs2_holder *gh)
  361. {
  362. clear_bit(HIF_WAIT, &gh->gh_iflags);
  363. smp_mb();
  364. wake_up_bit(&gh->gh_iflags, HIF_WAIT);
  365. }
  366. static int holder_wait(void *word)
  367. {
  368. schedule();
  369. return 0;
  370. }
  371. static void wait_on_holder(struct gfs2_holder *gh)
  372. {
  373. might_sleep();
  374. wait_on_bit(&gh->gh_iflags, HIF_WAIT, holder_wait, TASK_UNINTERRUPTIBLE);
  375. }
  376. /**
  377. * rq_mutex - process a mutex request in the queue
  378. * @gh: the glock holder
  379. *
  380. * Returns: 1 if the queue is blocked
  381. */
  382. static int rq_mutex(struct gfs2_holder *gh)
  383. {
  384. struct gfs2_glock *gl = gh->gh_gl;
  385. list_del_init(&gh->gh_list);
  386. /* gh->gh_error never examined. */
  387. set_bit(GLF_LOCK, &gl->gl_flags);
  388. clear_bit(HIF_WAIT, &gh->gh_iflags);
  389. smp_mb();
  390. wake_up_bit(&gh->gh_iflags, HIF_WAIT);
  391. return 1;
  392. }
  393. /**
  394. * rq_promote - process a promote request in the queue
  395. * @gh: the glock holder
  396. *
  397. * Acquire a new inter-node lock, or change a lock state to more restrictive.
  398. *
  399. * Returns: 1 if the queue is blocked
  400. */
  401. static int rq_promote(struct gfs2_holder *gh)
  402. {
  403. struct gfs2_glock *gl = gh->gh_gl;
  404. struct gfs2_sbd *sdp = gl->gl_sbd;
  405. if (!relaxed_state_ok(gl->gl_state, gh->gh_state, gh->gh_flags)) {
  406. if (list_empty(&gl->gl_holders)) {
  407. gl->gl_req_gh = gh;
  408. set_bit(GLF_LOCK, &gl->gl_flags);
  409. spin_unlock(&gl->gl_spin);
  410. if (atomic_read(&sdp->sd_reclaim_count) >
  411. gfs2_tune_get(sdp, gt_reclaim_limit) &&
  412. !(gh->gh_flags & LM_FLAG_PRIORITY)) {
  413. gfs2_reclaim_glock(sdp);
  414. gfs2_reclaim_glock(sdp);
  415. }
  416. gfs2_glock_xmote_th(gh->gh_gl, gh);
  417. spin_lock(&gl->gl_spin);
  418. }
  419. return 1;
  420. }
  421. if (list_empty(&gl->gl_holders)) {
  422. set_bit(HIF_FIRST, &gh->gh_iflags);
  423. set_bit(GLF_LOCK, &gl->gl_flags);
  424. } else {
  425. struct gfs2_holder *next_gh;
  426. if (gh->gh_state == LM_ST_EXCLUSIVE)
  427. return 1;
  428. next_gh = list_entry(gl->gl_holders.next, struct gfs2_holder,
  429. gh_list);
  430. if (next_gh->gh_state == LM_ST_EXCLUSIVE)
  431. return 1;
  432. }
  433. list_move_tail(&gh->gh_list, &gl->gl_holders);
  434. gh->gh_error = 0;
  435. set_bit(HIF_HOLDER, &gh->gh_iflags);
  436. gfs2_holder_wake(gh);
  437. return 0;
  438. }
  439. /**
  440. * rq_demote - process a demote request in the queue
  441. * @gh: the glock holder
  442. *
  443. * Returns: 1 if the queue is blocked
  444. */
  445. static int rq_demote(struct gfs2_glock *gl)
  446. {
  447. if (!list_empty(&gl->gl_holders))
  448. return 1;
  449. if (gl->gl_state == gl->gl_demote_state ||
  450. gl->gl_state == LM_ST_UNLOCKED) {
  451. clear_bit(GLF_DEMOTE, &gl->gl_flags);
  452. return 0;
  453. }
  454. set_bit(GLF_LOCK, &gl->gl_flags);
  455. spin_unlock(&gl->gl_spin);
  456. if (gl->gl_demote_state == LM_ST_UNLOCKED ||
  457. gl->gl_state != LM_ST_EXCLUSIVE)
  458. gfs2_glock_drop_th(gl);
  459. else
  460. gfs2_glock_xmote_th(gl, NULL);
  461. spin_lock(&gl->gl_spin);
  462. return 0;
  463. }
  464. /**
  465. * run_queue - process holder structures on a glock
  466. * @gl: the glock
  467. *
  468. */
  469. static void run_queue(struct gfs2_glock *gl)
  470. {
  471. struct gfs2_holder *gh;
  472. int blocked = 1;
  473. for (;;) {
  474. if (test_bit(GLF_LOCK, &gl->gl_flags))
  475. break;
  476. if (!list_empty(&gl->gl_waiters1)) {
  477. gh = list_entry(gl->gl_waiters1.next,
  478. struct gfs2_holder, gh_list);
  479. if (test_bit(HIF_MUTEX, &gh->gh_iflags))
  480. blocked = rq_mutex(gh);
  481. else
  482. gfs2_assert_warn(gl->gl_sbd, 0);
  483. } else if (test_bit(GLF_DEMOTE, &gl->gl_flags)) {
  484. blocked = rq_demote(gl);
  485. } else if (!list_empty(&gl->gl_waiters3)) {
  486. gh = list_entry(gl->gl_waiters3.next,
  487. struct gfs2_holder, gh_list);
  488. if (test_bit(HIF_PROMOTE, &gh->gh_iflags))
  489. blocked = rq_promote(gh);
  490. else
  491. gfs2_assert_warn(gl->gl_sbd, 0);
  492. } else
  493. break;
  494. if (blocked)
  495. break;
  496. }
  497. }
  498. /**
  499. * gfs2_glmutex_lock - acquire a local lock on a glock
  500. * @gl: the glock
  501. *
  502. * Gives caller exclusive access to manipulate a glock structure.
  503. */
  504. static void gfs2_glmutex_lock(struct gfs2_glock *gl)
  505. {
  506. struct gfs2_holder gh;
  507. gfs2_holder_init(gl, 0, 0, &gh);
  508. set_bit(HIF_MUTEX, &gh.gh_iflags);
  509. if (test_and_set_bit(HIF_WAIT, &gh.gh_iflags))
  510. BUG();
  511. spin_lock(&gl->gl_spin);
  512. if (test_and_set_bit(GLF_LOCK, &gl->gl_flags)) {
  513. list_add_tail(&gh.gh_list, &gl->gl_waiters1);
  514. } else {
  515. gl->gl_owner_pid = current->pid;
  516. gl->gl_ip = (unsigned long)__builtin_return_address(0);
  517. clear_bit(HIF_WAIT, &gh.gh_iflags);
  518. smp_mb();
  519. wake_up_bit(&gh.gh_iflags, HIF_WAIT);
  520. }
  521. spin_unlock(&gl->gl_spin);
  522. wait_on_holder(&gh);
  523. gfs2_holder_uninit(&gh);
  524. }
  525. /**
  526. * gfs2_glmutex_trylock - try to acquire a local lock on a glock
  527. * @gl: the glock
  528. *
  529. * Returns: 1 if the glock is acquired
  530. */
  531. static int gfs2_glmutex_trylock(struct gfs2_glock *gl)
  532. {
  533. int acquired = 1;
  534. spin_lock(&gl->gl_spin);
  535. if (test_and_set_bit(GLF_LOCK, &gl->gl_flags)) {
  536. acquired = 0;
  537. } else {
  538. gl->gl_owner_pid = current->pid;
  539. gl->gl_ip = (unsigned long)__builtin_return_address(0);
  540. }
  541. spin_unlock(&gl->gl_spin);
  542. return acquired;
  543. }
  544. /**
  545. * gfs2_glmutex_unlock - release a local lock on a glock
  546. * @gl: the glock
  547. *
  548. */
  549. static void gfs2_glmutex_unlock(struct gfs2_glock *gl)
  550. {
  551. spin_lock(&gl->gl_spin);
  552. clear_bit(GLF_LOCK, &gl->gl_flags);
  553. gl->gl_owner_pid = 0;
  554. gl->gl_ip = 0;
  555. run_queue(gl);
  556. BUG_ON(!spin_is_locked(&gl->gl_spin));
  557. spin_unlock(&gl->gl_spin);
  558. }
  559. /**
  560. * handle_callback - process a demote request
  561. * @gl: the glock
  562. * @state: the state the caller wants us to change to
  563. *
  564. * There are only two requests that we are going to see in actual
  565. * practise: LM_ST_SHARED and LM_ST_UNLOCKED
  566. */
  567. static void handle_callback(struct gfs2_glock *gl, unsigned int state)
  568. {
  569. spin_lock(&gl->gl_spin);
  570. if (test_and_set_bit(GLF_DEMOTE, &gl->gl_flags) == 0) {
  571. gl->gl_demote_state = state;
  572. gl->gl_demote_time = jiffies;
  573. } else if (gl->gl_demote_state != LM_ST_UNLOCKED) {
  574. gl->gl_demote_state = state;
  575. }
  576. spin_unlock(&gl->gl_spin);
  577. }
  578. /**
  579. * state_change - record that the glock is now in a different state
  580. * @gl: the glock
  581. * @new_state the new state
  582. *
  583. */
  584. static void state_change(struct gfs2_glock *gl, unsigned int new_state)
  585. {
  586. int held1, held2;
  587. held1 = (gl->gl_state != LM_ST_UNLOCKED);
  588. held2 = (new_state != LM_ST_UNLOCKED);
  589. if (held1 != held2) {
  590. if (held2)
  591. gfs2_glock_hold(gl);
  592. else
  593. gfs2_glock_put(gl);
  594. }
  595. gl->gl_state = new_state;
  596. }
  597. /**
  598. * xmote_bh - Called after the lock module is done acquiring a lock
  599. * @gl: The glock in question
  600. * @ret: the int returned from the lock module
  601. *
  602. */
  603. static void xmote_bh(struct gfs2_glock *gl, unsigned int ret)
  604. {
  605. struct gfs2_sbd *sdp = gl->gl_sbd;
  606. const struct gfs2_glock_operations *glops = gl->gl_ops;
  607. struct gfs2_holder *gh = gl->gl_req_gh;
  608. int prev_state = gl->gl_state;
  609. int op_done = 1;
  610. gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
  611. gfs2_assert_warn(sdp, list_empty(&gl->gl_holders));
  612. gfs2_assert_warn(sdp, !(ret & LM_OUT_ASYNC));
  613. state_change(gl, ret & LM_OUT_ST_MASK);
  614. if (prev_state != LM_ST_UNLOCKED && !(ret & LM_OUT_CACHEABLE)) {
  615. if (glops->go_inval)
  616. glops->go_inval(gl, DIO_METADATA);
  617. } else if (gl->gl_state == LM_ST_DEFERRED) {
  618. /* We might not want to do this here.
  619. Look at moving to the inode glops. */
  620. if (glops->go_inval)
  621. glops->go_inval(gl, 0);
  622. }
  623. /* Deal with each possible exit condition */
  624. if (!gh) {
  625. gl->gl_stamp = jiffies;
  626. if (ret & LM_OUT_CANCELED)
  627. op_done = 0;
  628. else
  629. clear_bit(GLF_DEMOTE, &gl->gl_flags);
  630. } else {
  631. spin_lock(&gl->gl_spin);
  632. list_del_init(&gh->gh_list);
  633. gh->gh_error = -EIO;
  634. if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
  635. goto out;
  636. gh->gh_error = GLR_CANCELED;
  637. if (ret & LM_OUT_CANCELED)
  638. goto out;
  639. if (relaxed_state_ok(gl->gl_state, gh->gh_state, gh->gh_flags)) {
  640. list_add_tail(&gh->gh_list, &gl->gl_holders);
  641. gh->gh_error = 0;
  642. set_bit(HIF_HOLDER, &gh->gh_iflags);
  643. set_bit(HIF_FIRST, &gh->gh_iflags);
  644. op_done = 0;
  645. goto out;
  646. }
  647. gh->gh_error = GLR_TRYFAILED;
  648. if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB))
  649. goto out;
  650. gh->gh_error = -EINVAL;
  651. if (gfs2_assert_withdraw(sdp, 0) == -1)
  652. fs_err(sdp, "ret = 0x%.8X\n", ret);
  653. out:
  654. spin_unlock(&gl->gl_spin);
  655. }
  656. if (glops->go_xmote_bh)
  657. glops->go_xmote_bh(gl);
  658. if (op_done) {
  659. spin_lock(&gl->gl_spin);
  660. gl->gl_req_gh = NULL;
  661. gl->gl_req_bh = NULL;
  662. clear_bit(GLF_LOCK, &gl->gl_flags);
  663. run_queue(gl);
  664. spin_unlock(&gl->gl_spin);
  665. }
  666. gfs2_glock_put(gl);
  667. if (gh)
  668. gfs2_holder_wake(gh);
  669. }
  670. /**
  671. * gfs2_glock_xmote_th - Call into the lock module to acquire or change a glock
  672. * @gl: The glock in question
  673. * @state: the requested state
  674. * @flags: modifier flags to the lock call
  675. *
  676. */
  677. void gfs2_glock_xmote_th(struct gfs2_glock *gl, struct gfs2_holder *gh)
  678. {
  679. struct gfs2_sbd *sdp = gl->gl_sbd;
  680. int flags = gh ? gh->gh_flags : 0;
  681. unsigned state = gh ? gh->gh_state : gl->gl_demote_state;
  682. const struct gfs2_glock_operations *glops = gl->gl_ops;
  683. int lck_flags = flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB |
  684. LM_FLAG_NOEXP | LM_FLAG_ANY |
  685. LM_FLAG_PRIORITY);
  686. unsigned int lck_ret;
  687. if (glops->go_xmote_th)
  688. glops->go_xmote_th(gl);
  689. gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
  690. gfs2_assert_warn(sdp, list_empty(&gl->gl_holders));
  691. gfs2_assert_warn(sdp, state != LM_ST_UNLOCKED);
  692. gfs2_assert_warn(sdp, state != gl->gl_state);
  693. gfs2_glock_hold(gl);
  694. gl->gl_req_bh = xmote_bh;
  695. lck_ret = gfs2_lm_lock(sdp, gl->gl_lock, gl->gl_state, state, lck_flags);
  696. if (gfs2_assert_withdraw(sdp, !(lck_ret & LM_OUT_ERROR)))
  697. return;
  698. if (lck_ret & LM_OUT_ASYNC)
  699. gfs2_assert_warn(sdp, lck_ret == LM_OUT_ASYNC);
  700. else
  701. xmote_bh(gl, lck_ret);
  702. }
  703. /**
  704. * drop_bh - Called after a lock module unlock completes
  705. * @gl: the glock
  706. * @ret: the return status
  707. *
  708. * Doesn't wake up the process waiting on the struct gfs2_holder (if any)
  709. * Doesn't drop the reference on the glock the top half took out
  710. *
  711. */
  712. static void drop_bh(struct gfs2_glock *gl, unsigned int ret)
  713. {
  714. struct gfs2_sbd *sdp = gl->gl_sbd;
  715. const struct gfs2_glock_operations *glops = gl->gl_ops;
  716. struct gfs2_holder *gh = gl->gl_req_gh;
  717. gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
  718. gfs2_assert_warn(sdp, list_empty(&gl->gl_holders));
  719. gfs2_assert_warn(sdp, !ret);
  720. state_change(gl, LM_ST_UNLOCKED);
  721. clear_bit(GLF_DEMOTE, &gl->gl_flags);
  722. if (glops->go_inval)
  723. glops->go_inval(gl, DIO_METADATA);
  724. if (gh) {
  725. spin_lock(&gl->gl_spin);
  726. list_del_init(&gh->gh_list);
  727. gh->gh_error = 0;
  728. spin_unlock(&gl->gl_spin);
  729. }
  730. spin_lock(&gl->gl_spin);
  731. gl->gl_req_gh = NULL;
  732. gl->gl_req_bh = NULL;
  733. clear_bit(GLF_LOCK, &gl->gl_flags);
  734. run_queue(gl);
  735. spin_unlock(&gl->gl_spin);
  736. gfs2_glock_put(gl);
  737. if (gh)
  738. gfs2_holder_wake(gh);
  739. }
  740. /**
  741. * gfs2_glock_drop_th - call into the lock module to unlock a lock
  742. * @gl: the glock
  743. *
  744. */
  745. static void gfs2_glock_drop_th(struct gfs2_glock *gl)
  746. {
  747. struct gfs2_sbd *sdp = gl->gl_sbd;
  748. const struct gfs2_glock_operations *glops = gl->gl_ops;
  749. unsigned int ret;
  750. if (glops->go_drop_th)
  751. glops->go_drop_th(gl);
  752. gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
  753. gfs2_assert_warn(sdp, list_empty(&gl->gl_holders));
  754. gfs2_assert_warn(sdp, gl->gl_state != LM_ST_UNLOCKED);
  755. gfs2_glock_hold(gl);
  756. gl->gl_req_bh = drop_bh;
  757. ret = gfs2_lm_unlock(sdp, gl->gl_lock, gl->gl_state);
  758. if (gfs2_assert_withdraw(sdp, !(ret & LM_OUT_ERROR)))
  759. return;
  760. if (!ret)
  761. drop_bh(gl, ret);
  762. else
  763. gfs2_assert_warn(sdp, ret == LM_OUT_ASYNC);
  764. }
  765. /**
  766. * do_cancels - cancel requests for locks stuck waiting on an expire flag
  767. * @gh: the LM_FLAG_PRIORITY holder waiting to acquire the lock
  768. *
  769. * Don't cancel GL_NOCANCEL requests.
  770. */
  771. static void do_cancels(struct gfs2_holder *gh)
  772. {
  773. struct gfs2_glock *gl = gh->gh_gl;
  774. spin_lock(&gl->gl_spin);
  775. while (gl->gl_req_gh != gh &&
  776. !test_bit(HIF_HOLDER, &gh->gh_iflags) &&
  777. !list_empty(&gh->gh_list)) {
  778. if (gl->gl_req_bh && !(gl->gl_req_gh &&
  779. (gl->gl_req_gh->gh_flags & GL_NOCANCEL))) {
  780. spin_unlock(&gl->gl_spin);
  781. gfs2_lm_cancel(gl->gl_sbd, gl->gl_lock);
  782. msleep(100);
  783. spin_lock(&gl->gl_spin);
  784. } else {
  785. spin_unlock(&gl->gl_spin);
  786. msleep(100);
  787. spin_lock(&gl->gl_spin);
  788. }
  789. }
  790. spin_unlock(&gl->gl_spin);
  791. }
  792. /**
  793. * glock_wait_internal - wait on a glock acquisition
  794. * @gh: the glock holder
  795. *
  796. * Returns: 0 on success
  797. */
  798. static int glock_wait_internal(struct gfs2_holder *gh)
  799. {
  800. struct gfs2_glock *gl = gh->gh_gl;
  801. struct gfs2_sbd *sdp = gl->gl_sbd;
  802. const struct gfs2_glock_operations *glops = gl->gl_ops;
  803. if (test_bit(HIF_ABORTED, &gh->gh_iflags))
  804. return -EIO;
  805. if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) {
  806. spin_lock(&gl->gl_spin);
  807. if (gl->gl_req_gh != gh &&
  808. !test_bit(HIF_HOLDER, &gh->gh_iflags) &&
  809. !list_empty(&gh->gh_list)) {
  810. list_del_init(&gh->gh_list);
  811. gh->gh_error = GLR_TRYFAILED;
  812. run_queue(gl);
  813. spin_unlock(&gl->gl_spin);
  814. return gh->gh_error;
  815. }
  816. spin_unlock(&gl->gl_spin);
  817. }
  818. if (gh->gh_flags & LM_FLAG_PRIORITY)
  819. do_cancels(gh);
  820. wait_on_holder(gh);
  821. if (gh->gh_error)
  822. return gh->gh_error;
  823. gfs2_assert_withdraw(sdp, test_bit(HIF_HOLDER, &gh->gh_iflags));
  824. gfs2_assert_withdraw(sdp, relaxed_state_ok(gl->gl_state, gh->gh_state,
  825. gh->gh_flags));
  826. if (test_bit(HIF_FIRST, &gh->gh_iflags)) {
  827. gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
  828. if (glops->go_lock) {
  829. gh->gh_error = glops->go_lock(gh);
  830. if (gh->gh_error) {
  831. spin_lock(&gl->gl_spin);
  832. list_del_init(&gh->gh_list);
  833. spin_unlock(&gl->gl_spin);
  834. }
  835. }
  836. spin_lock(&gl->gl_spin);
  837. gl->gl_req_gh = NULL;
  838. gl->gl_req_bh = NULL;
  839. clear_bit(GLF_LOCK, &gl->gl_flags);
  840. run_queue(gl);
  841. spin_unlock(&gl->gl_spin);
  842. }
  843. return gh->gh_error;
  844. }
  845. static inline struct gfs2_holder *
  846. find_holder_by_owner(struct list_head *head, pid_t pid)
  847. {
  848. struct gfs2_holder *gh;
  849. list_for_each_entry(gh, head, gh_list) {
  850. if (gh->gh_owner_pid == pid)
  851. return gh;
  852. }
  853. return NULL;
  854. }
  855. static void print_dbg(struct glock_iter *gi, const char *fmt, ...)
  856. {
  857. va_list args;
  858. va_start(args, fmt);
  859. if (gi) {
  860. vsprintf(gi->string, fmt, args);
  861. seq_printf(gi->seq, gi->string);
  862. }
  863. else
  864. vprintk(fmt, args);
  865. va_end(args);
  866. }
  867. /**
  868. * add_to_queue - Add a holder to the wait queue (but look for recursion)
  869. * @gh: the holder structure to add
  870. *
  871. */
  872. static void add_to_queue(struct gfs2_holder *gh)
  873. {
  874. struct gfs2_glock *gl = gh->gh_gl;
  875. struct gfs2_holder *existing;
  876. BUG_ON(!gh->gh_owner_pid);
  877. if (test_and_set_bit(HIF_WAIT, &gh->gh_iflags))
  878. BUG();
  879. existing = find_holder_by_owner(&gl->gl_holders, gh->gh_owner_pid);
  880. if (existing) {
  881. print_symbol(KERN_WARNING "original: %s\n", existing->gh_ip);
  882. printk(KERN_INFO "pid : %d\n", existing->gh_owner_pid);
  883. printk(KERN_INFO "lock type : %d lock state : %d\n",
  884. existing->gh_gl->gl_name.ln_type, existing->gh_gl->gl_state);
  885. print_symbol(KERN_WARNING "new: %s\n", gh->gh_ip);
  886. printk(KERN_INFO "pid : %d\n", gh->gh_owner_pid);
  887. printk(KERN_INFO "lock type : %d lock state : %d\n",
  888. gl->gl_name.ln_type, gl->gl_state);
  889. BUG();
  890. }
  891. existing = find_holder_by_owner(&gl->gl_waiters3, gh->gh_owner_pid);
  892. if (existing) {
  893. print_symbol(KERN_WARNING "original: %s\n", existing->gh_ip);
  894. print_symbol(KERN_WARNING "new: %s\n", gh->gh_ip);
  895. BUG();
  896. }
  897. if (gh->gh_flags & LM_FLAG_PRIORITY)
  898. list_add(&gh->gh_list, &gl->gl_waiters3);
  899. else
  900. list_add_tail(&gh->gh_list, &gl->gl_waiters3);
  901. }
  902. /**
  903. * gfs2_glock_nq - enqueue a struct gfs2_holder onto a glock (acquire a glock)
  904. * @gh: the holder structure
  905. *
  906. * if (gh->gh_flags & GL_ASYNC), this never returns an error
  907. *
  908. * Returns: 0, GLR_TRYFAILED, or errno on failure
  909. */
  910. int gfs2_glock_nq(struct gfs2_holder *gh)
  911. {
  912. struct gfs2_glock *gl = gh->gh_gl;
  913. struct gfs2_sbd *sdp = gl->gl_sbd;
  914. int error = 0;
  915. restart:
  916. if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) {
  917. set_bit(HIF_ABORTED, &gh->gh_iflags);
  918. return -EIO;
  919. }
  920. set_bit(HIF_PROMOTE, &gh->gh_iflags);
  921. spin_lock(&gl->gl_spin);
  922. add_to_queue(gh);
  923. run_queue(gl);
  924. spin_unlock(&gl->gl_spin);
  925. if (!(gh->gh_flags & GL_ASYNC)) {
  926. error = glock_wait_internal(gh);
  927. if (error == GLR_CANCELED) {
  928. msleep(100);
  929. goto restart;
  930. }
  931. }
  932. return error;
  933. }
  934. /**
  935. * gfs2_glock_poll - poll to see if an async request has been completed
  936. * @gh: the holder
  937. *
  938. * Returns: 1 if the request is ready to be gfs2_glock_wait()ed on
  939. */
  940. int gfs2_glock_poll(struct gfs2_holder *gh)
  941. {
  942. struct gfs2_glock *gl = gh->gh_gl;
  943. int ready = 0;
  944. spin_lock(&gl->gl_spin);
  945. if (test_bit(HIF_HOLDER, &gh->gh_iflags))
  946. ready = 1;
  947. else if (list_empty(&gh->gh_list)) {
  948. if (gh->gh_error == GLR_CANCELED) {
  949. spin_unlock(&gl->gl_spin);
  950. msleep(100);
  951. if (gfs2_glock_nq(gh))
  952. return 1;
  953. return 0;
  954. } else
  955. ready = 1;
  956. }
  957. spin_unlock(&gl->gl_spin);
  958. return ready;
  959. }
  960. /**
  961. * gfs2_glock_wait - wait for a lock acquisition that ended in a GLR_ASYNC
  962. * @gh: the holder structure
  963. *
  964. * Returns: 0, GLR_TRYFAILED, or errno on failure
  965. */
  966. int gfs2_glock_wait(struct gfs2_holder *gh)
  967. {
  968. int error;
  969. error = glock_wait_internal(gh);
  970. if (error == GLR_CANCELED) {
  971. msleep(100);
  972. gh->gh_flags &= ~GL_ASYNC;
  973. error = gfs2_glock_nq(gh);
  974. }
  975. return error;
  976. }
  977. /**
  978. * gfs2_glock_dq - dequeue a struct gfs2_holder from a glock (release a glock)
  979. * @gh: the glock holder
  980. *
  981. */
  982. void gfs2_glock_dq(struct gfs2_holder *gh)
  983. {
  984. struct gfs2_glock *gl = gh->gh_gl;
  985. const struct gfs2_glock_operations *glops = gl->gl_ops;
  986. if (gh->gh_flags & GL_NOCACHE)
  987. handle_callback(gl, LM_ST_UNLOCKED);
  988. gfs2_glmutex_lock(gl);
  989. spin_lock(&gl->gl_spin);
  990. list_del_init(&gh->gh_list);
  991. if (list_empty(&gl->gl_holders)) {
  992. spin_unlock(&gl->gl_spin);
  993. if (glops->go_unlock)
  994. glops->go_unlock(gh);
  995. spin_lock(&gl->gl_spin);
  996. gl->gl_stamp = jiffies;
  997. }
  998. clear_bit(GLF_LOCK, &gl->gl_flags);
  999. run_queue(gl);
  1000. spin_unlock(&gl->gl_spin);
  1001. }
  1002. /**
  1003. * gfs2_glock_dq_uninit - dequeue a holder from a glock and initialize it
  1004. * @gh: the holder structure
  1005. *
  1006. */
  1007. void gfs2_glock_dq_uninit(struct gfs2_holder *gh)
  1008. {
  1009. gfs2_glock_dq(gh);
  1010. gfs2_holder_uninit(gh);
  1011. }
  1012. /**
  1013. * gfs2_glock_nq_num - acquire a glock based on lock number
  1014. * @sdp: the filesystem
  1015. * @number: the lock number
  1016. * @glops: the glock operations for the type of glock
  1017. * @state: the state to acquire the glock in
  1018. * @flags: modifier flags for the aquisition
  1019. * @gh: the struct gfs2_holder
  1020. *
  1021. * Returns: errno
  1022. */
  1023. int gfs2_glock_nq_num(struct gfs2_sbd *sdp, u64 number,
  1024. const struct gfs2_glock_operations *glops,
  1025. unsigned int state, int flags, struct gfs2_holder *gh)
  1026. {
  1027. struct gfs2_glock *gl;
  1028. int error;
  1029. error = gfs2_glock_get(sdp, number, glops, CREATE, &gl);
  1030. if (!error) {
  1031. error = gfs2_glock_nq_init(gl, state, flags, gh);
  1032. gfs2_glock_put(gl);
  1033. }
  1034. return error;
  1035. }
  1036. /**
  1037. * glock_compare - Compare two struct gfs2_glock structures for sorting
  1038. * @arg_a: the first structure
  1039. * @arg_b: the second structure
  1040. *
  1041. */
  1042. static int glock_compare(const void *arg_a, const void *arg_b)
  1043. {
  1044. const struct gfs2_holder *gh_a = *(const struct gfs2_holder **)arg_a;
  1045. const struct gfs2_holder *gh_b = *(const struct gfs2_holder **)arg_b;
  1046. const struct lm_lockname *a = &gh_a->gh_gl->gl_name;
  1047. const struct lm_lockname *b = &gh_b->gh_gl->gl_name;
  1048. if (a->ln_number > b->ln_number)
  1049. return 1;
  1050. if (a->ln_number < b->ln_number)
  1051. return -1;
  1052. BUG_ON(gh_a->gh_gl->gl_ops->go_type == gh_b->gh_gl->gl_ops->go_type);
  1053. return 0;
  1054. }
  1055. /**
  1056. * nq_m_sync - synchonously acquire more than one glock in deadlock free order
  1057. * @num_gh: the number of structures
  1058. * @ghs: an array of struct gfs2_holder structures
  1059. *
  1060. * Returns: 0 on success (all glocks acquired),
  1061. * errno on failure (no glocks acquired)
  1062. */
  1063. static int nq_m_sync(unsigned int num_gh, struct gfs2_holder *ghs,
  1064. struct gfs2_holder **p)
  1065. {
  1066. unsigned int x;
  1067. int error = 0;
  1068. for (x = 0; x < num_gh; x++)
  1069. p[x] = &ghs[x];
  1070. sort(p, num_gh, sizeof(struct gfs2_holder *), glock_compare, NULL);
  1071. for (x = 0; x < num_gh; x++) {
  1072. p[x]->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
  1073. error = gfs2_glock_nq(p[x]);
  1074. if (error) {
  1075. while (x--)
  1076. gfs2_glock_dq(p[x]);
  1077. break;
  1078. }
  1079. }
  1080. return error;
  1081. }
  1082. /**
  1083. * gfs2_glock_nq_m - acquire multiple glocks
  1084. * @num_gh: the number of structures
  1085. * @ghs: an array of struct gfs2_holder structures
  1086. *
  1087. * Figure out how big an impact this function has. Either:
  1088. * 1) Replace this code with code that calls gfs2_glock_prefetch()
  1089. * 2) Forget async stuff and just call nq_m_sync()
  1090. * 3) Leave it like it is
  1091. *
  1092. * Returns: 0 on success (all glocks acquired),
  1093. * errno on failure (no glocks acquired)
  1094. */
  1095. int gfs2_glock_nq_m(unsigned int num_gh, struct gfs2_holder *ghs)
  1096. {
  1097. int *e;
  1098. unsigned int x;
  1099. int borked = 0, serious = 0;
  1100. int error = 0;
  1101. if (!num_gh)
  1102. return 0;
  1103. if (num_gh == 1) {
  1104. ghs->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
  1105. return gfs2_glock_nq(ghs);
  1106. }
  1107. e = kcalloc(num_gh, sizeof(struct gfs2_holder *), GFP_KERNEL);
  1108. if (!e)
  1109. return -ENOMEM;
  1110. for (x = 0; x < num_gh; x++) {
  1111. ghs[x].gh_flags |= LM_FLAG_TRY | GL_ASYNC;
  1112. error = gfs2_glock_nq(&ghs[x]);
  1113. if (error) {
  1114. borked = 1;
  1115. serious = error;
  1116. num_gh = x;
  1117. break;
  1118. }
  1119. }
  1120. for (x = 0; x < num_gh; x++) {
  1121. error = e[x] = glock_wait_internal(&ghs[x]);
  1122. if (error) {
  1123. borked = 1;
  1124. if (error != GLR_TRYFAILED && error != GLR_CANCELED)
  1125. serious = error;
  1126. }
  1127. }
  1128. if (!borked) {
  1129. kfree(e);
  1130. return 0;
  1131. }
  1132. for (x = 0; x < num_gh; x++)
  1133. if (!e[x])
  1134. gfs2_glock_dq(&ghs[x]);
  1135. if (serious)
  1136. error = serious;
  1137. else {
  1138. for (x = 0; x < num_gh; x++)
  1139. gfs2_holder_reinit(ghs[x].gh_state, ghs[x].gh_flags,
  1140. &ghs[x]);
  1141. error = nq_m_sync(num_gh, ghs, (struct gfs2_holder **)e);
  1142. }
  1143. kfree(e);
  1144. return error;
  1145. }
  1146. /**
  1147. * gfs2_glock_dq_m - release multiple glocks
  1148. * @num_gh: the number of structures
  1149. * @ghs: an array of struct gfs2_holder structures
  1150. *
  1151. */
  1152. void gfs2_glock_dq_m(unsigned int num_gh, struct gfs2_holder *ghs)
  1153. {
  1154. unsigned int x;
  1155. for (x = 0; x < num_gh; x++)
  1156. gfs2_glock_dq(&ghs[x]);
  1157. }
  1158. /**
  1159. * gfs2_glock_dq_uninit_m - release multiple glocks
  1160. * @num_gh: the number of structures
  1161. * @ghs: an array of struct gfs2_holder structures
  1162. *
  1163. */
  1164. void gfs2_glock_dq_uninit_m(unsigned int num_gh, struct gfs2_holder *ghs)
  1165. {
  1166. unsigned int x;
  1167. for (x = 0; x < num_gh; x++)
  1168. gfs2_glock_dq_uninit(&ghs[x]);
  1169. }
  1170. /**
  1171. * gfs2_lvb_hold - attach a LVB from a glock
  1172. * @gl: The glock in question
  1173. *
  1174. */
  1175. int gfs2_lvb_hold(struct gfs2_glock *gl)
  1176. {
  1177. int error;
  1178. gfs2_glmutex_lock(gl);
  1179. if (!atomic_read(&gl->gl_lvb_count)) {
  1180. error = gfs2_lm_hold_lvb(gl->gl_sbd, gl->gl_lock, &gl->gl_lvb);
  1181. if (error) {
  1182. gfs2_glmutex_unlock(gl);
  1183. return error;
  1184. }
  1185. gfs2_glock_hold(gl);
  1186. }
  1187. atomic_inc(&gl->gl_lvb_count);
  1188. gfs2_glmutex_unlock(gl);
  1189. return 0;
  1190. }
  1191. /**
  1192. * gfs2_lvb_unhold - detach a LVB from a glock
  1193. * @gl: The glock in question
  1194. *
  1195. */
  1196. void gfs2_lvb_unhold(struct gfs2_glock *gl)
  1197. {
  1198. gfs2_glock_hold(gl);
  1199. gfs2_glmutex_lock(gl);
  1200. gfs2_assert(gl->gl_sbd, atomic_read(&gl->gl_lvb_count) > 0);
  1201. if (atomic_dec_and_test(&gl->gl_lvb_count)) {
  1202. gfs2_lm_unhold_lvb(gl->gl_sbd, gl->gl_lock, gl->gl_lvb);
  1203. gl->gl_lvb = NULL;
  1204. gfs2_glock_put(gl);
  1205. }
  1206. gfs2_glmutex_unlock(gl);
  1207. gfs2_glock_put(gl);
  1208. }
  1209. static void blocking_cb(struct gfs2_sbd *sdp, struct lm_lockname *name,
  1210. unsigned int state)
  1211. {
  1212. struct gfs2_glock *gl;
  1213. gl = gfs2_glock_find(sdp, name);
  1214. if (!gl)
  1215. return;
  1216. handle_callback(gl, state);
  1217. spin_lock(&gl->gl_spin);
  1218. run_queue(gl);
  1219. spin_unlock(&gl->gl_spin);
  1220. gfs2_glock_put(gl);
  1221. }
  1222. /**
  1223. * gfs2_glock_cb - Callback used by locking module
  1224. * @sdp: Pointer to the superblock
  1225. * @type: Type of callback
  1226. * @data: Type dependent data pointer
  1227. *
  1228. * Called by the locking module when it wants to tell us something.
  1229. * Either we need to drop a lock, one of our ASYNC requests completed, or
  1230. * a journal from another client needs to be recovered.
  1231. */
  1232. void gfs2_glock_cb(void *cb_data, unsigned int type, void *data)
  1233. {
  1234. struct gfs2_sbd *sdp = cb_data;
  1235. switch (type) {
  1236. case LM_CB_NEED_E:
  1237. blocking_cb(sdp, data, LM_ST_UNLOCKED);
  1238. return;
  1239. case LM_CB_NEED_D:
  1240. blocking_cb(sdp, data, LM_ST_DEFERRED);
  1241. return;
  1242. case LM_CB_NEED_S:
  1243. blocking_cb(sdp, data, LM_ST_SHARED);
  1244. return;
  1245. case LM_CB_ASYNC: {
  1246. struct lm_async_cb *async = data;
  1247. struct gfs2_glock *gl;
  1248. down_read(&gfs2_umount_flush_sem);
  1249. gl = gfs2_glock_find(sdp, &async->lc_name);
  1250. if (gfs2_assert_warn(sdp, gl))
  1251. return;
  1252. if (!gfs2_assert_warn(sdp, gl->gl_req_bh))
  1253. gl->gl_req_bh(gl, async->lc_ret);
  1254. gfs2_glock_put(gl);
  1255. up_read(&gfs2_umount_flush_sem);
  1256. return;
  1257. }
  1258. case LM_CB_NEED_RECOVERY:
  1259. gfs2_jdesc_make_dirty(sdp, *(unsigned int *)data);
  1260. if (sdp->sd_recoverd_process)
  1261. wake_up_process(sdp->sd_recoverd_process);
  1262. return;
  1263. case LM_CB_DROPLOCKS:
  1264. gfs2_gl_hash_clear(sdp, NO_WAIT);
  1265. gfs2_quota_scan(sdp);
  1266. return;
  1267. default:
  1268. gfs2_assert_warn(sdp, 0);
  1269. return;
  1270. }
  1271. }
  1272. /**
  1273. * demote_ok - Check to see if it's ok to unlock a glock
  1274. * @gl: the glock
  1275. *
  1276. * Returns: 1 if it's ok
  1277. */
  1278. static int demote_ok(struct gfs2_glock *gl)
  1279. {
  1280. const struct gfs2_glock_operations *glops = gl->gl_ops;
  1281. int demote = 1;
  1282. if (test_bit(GLF_STICKY, &gl->gl_flags))
  1283. demote = 0;
  1284. else if (glops->go_demote_ok)
  1285. demote = glops->go_demote_ok(gl);
  1286. return demote;
  1287. }
  1288. /**
  1289. * gfs2_glock_schedule_for_reclaim - Add a glock to the reclaim list
  1290. * @gl: the glock
  1291. *
  1292. */
  1293. void gfs2_glock_schedule_for_reclaim(struct gfs2_glock *gl)
  1294. {
  1295. struct gfs2_sbd *sdp = gl->gl_sbd;
  1296. spin_lock(&sdp->sd_reclaim_lock);
  1297. if (list_empty(&gl->gl_reclaim)) {
  1298. gfs2_glock_hold(gl);
  1299. list_add(&gl->gl_reclaim, &sdp->sd_reclaim_list);
  1300. atomic_inc(&sdp->sd_reclaim_count);
  1301. }
  1302. spin_unlock(&sdp->sd_reclaim_lock);
  1303. wake_up(&sdp->sd_reclaim_wq);
  1304. }
  1305. /**
  1306. * gfs2_reclaim_glock - process the next glock on the filesystem's reclaim list
  1307. * @sdp: the filesystem
  1308. *
  1309. * Called from gfs2_glockd() glock reclaim daemon, or when promoting a
  1310. * different glock and we notice that there are a lot of glocks in the
  1311. * reclaim list.
  1312. *
  1313. */
  1314. void gfs2_reclaim_glock(struct gfs2_sbd *sdp)
  1315. {
  1316. struct gfs2_glock *gl;
  1317. spin_lock(&sdp->sd_reclaim_lock);
  1318. if (list_empty(&sdp->sd_reclaim_list)) {
  1319. spin_unlock(&sdp->sd_reclaim_lock);
  1320. return;
  1321. }
  1322. gl = list_entry(sdp->sd_reclaim_list.next,
  1323. struct gfs2_glock, gl_reclaim);
  1324. list_del_init(&gl->gl_reclaim);
  1325. spin_unlock(&sdp->sd_reclaim_lock);
  1326. atomic_dec(&sdp->sd_reclaim_count);
  1327. atomic_inc(&sdp->sd_reclaimed);
  1328. if (gfs2_glmutex_trylock(gl)) {
  1329. if (list_empty(&gl->gl_holders) &&
  1330. gl->gl_state != LM_ST_UNLOCKED && demote_ok(gl))
  1331. handle_callback(gl, LM_ST_UNLOCKED);
  1332. gfs2_glmutex_unlock(gl);
  1333. }
  1334. gfs2_glock_put(gl);
  1335. }
  1336. /**
  1337. * examine_bucket - Call a function for glock in a hash bucket
  1338. * @examiner: the function
  1339. * @sdp: the filesystem
  1340. * @bucket: the bucket
  1341. *
  1342. * Returns: 1 if the bucket has entries
  1343. */
  1344. static int examine_bucket(glock_examiner examiner, struct gfs2_sbd *sdp,
  1345. unsigned int hash)
  1346. {
  1347. struct gfs2_glock *gl, *prev = NULL;
  1348. int has_entries = 0;
  1349. struct hlist_head *head = &gl_hash_table[hash].hb_list;
  1350. read_lock(gl_lock_addr(hash));
  1351. /* Can't use hlist_for_each_entry - don't want prefetch here */
  1352. if (hlist_empty(head))
  1353. goto out;
  1354. gl = list_entry(head->first, struct gfs2_glock, gl_list);
  1355. while(1) {
  1356. if (gl->gl_sbd == sdp) {
  1357. gfs2_glock_hold(gl);
  1358. read_unlock(gl_lock_addr(hash));
  1359. if (prev)
  1360. gfs2_glock_put(prev);
  1361. prev = gl;
  1362. examiner(gl);
  1363. has_entries = 1;
  1364. read_lock(gl_lock_addr(hash));
  1365. }
  1366. if (gl->gl_list.next == NULL)
  1367. break;
  1368. gl = list_entry(gl->gl_list.next, struct gfs2_glock, gl_list);
  1369. }
  1370. out:
  1371. read_unlock(gl_lock_addr(hash));
  1372. if (prev)
  1373. gfs2_glock_put(prev);
  1374. return has_entries;
  1375. }
  1376. /**
  1377. * scan_glock - look at a glock and see if we can reclaim it
  1378. * @gl: the glock to look at
  1379. *
  1380. */
  1381. static void scan_glock(struct gfs2_glock *gl)
  1382. {
  1383. if (gl->gl_ops == &gfs2_inode_glops && gl->gl_object)
  1384. return;
  1385. if (gfs2_glmutex_trylock(gl)) {
  1386. if (list_empty(&gl->gl_holders) &&
  1387. gl->gl_state != LM_ST_UNLOCKED && demote_ok(gl))
  1388. goto out_schedule;
  1389. gfs2_glmutex_unlock(gl);
  1390. }
  1391. return;
  1392. out_schedule:
  1393. gfs2_glmutex_unlock(gl);
  1394. gfs2_glock_schedule_for_reclaim(gl);
  1395. }
  1396. /**
  1397. * gfs2_scand_internal - Look for glocks and inodes to toss from memory
  1398. * @sdp: the filesystem
  1399. *
  1400. */
  1401. void gfs2_scand_internal(struct gfs2_sbd *sdp)
  1402. {
  1403. unsigned int x;
  1404. for (x = 0; x < GFS2_GL_HASH_SIZE; x++)
  1405. examine_bucket(scan_glock, sdp, x);
  1406. }
  1407. /**
  1408. * clear_glock - look at a glock and see if we can free it from glock cache
  1409. * @gl: the glock to look at
  1410. *
  1411. */
  1412. static void clear_glock(struct gfs2_glock *gl)
  1413. {
  1414. struct gfs2_sbd *sdp = gl->gl_sbd;
  1415. int released;
  1416. spin_lock(&sdp->sd_reclaim_lock);
  1417. if (!list_empty(&gl->gl_reclaim)) {
  1418. list_del_init(&gl->gl_reclaim);
  1419. atomic_dec(&sdp->sd_reclaim_count);
  1420. spin_unlock(&sdp->sd_reclaim_lock);
  1421. released = gfs2_glock_put(gl);
  1422. gfs2_assert(sdp, !released);
  1423. } else {
  1424. spin_unlock(&sdp->sd_reclaim_lock);
  1425. }
  1426. if (gfs2_glmutex_trylock(gl)) {
  1427. if (list_empty(&gl->gl_holders) &&
  1428. gl->gl_state != LM_ST_UNLOCKED)
  1429. handle_callback(gl, LM_ST_UNLOCKED);
  1430. gfs2_glmutex_unlock(gl);
  1431. }
  1432. }
  1433. /**
  1434. * gfs2_gl_hash_clear - Empty out the glock hash table
  1435. * @sdp: the filesystem
  1436. * @wait: wait until it's all gone
  1437. *
  1438. * Called when unmounting the filesystem, or when inter-node lock manager
  1439. * requests DROPLOCKS because it is running out of capacity.
  1440. */
  1441. void gfs2_gl_hash_clear(struct gfs2_sbd *sdp, int wait)
  1442. {
  1443. unsigned long t;
  1444. unsigned int x;
  1445. int cont;
  1446. t = jiffies;
  1447. for (;;) {
  1448. cont = 0;
  1449. for (x = 0; x < GFS2_GL_HASH_SIZE; x++) {
  1450. if (examine_bucket(clear_glock, sdp, x))
  1451. cont = 1;
  1452. }
  1453. if (!wait || !cont)
  1454. break;
  1455. if (time_after_eq(jiffies,
  1456. t + gfs2_tune_get(sdp, gt_stall_secs) * HZ)) {
  1457. fs_warn(sdp, "Unmount seems to be stalled. "
  1458. "Dumping lock state...\n");
  1459. gfs2_dump_lockstate(sdp);
  1460. t = jiffies;
  1461. }
  1462. down_write(&gfs2_umount_flush_sem);
  1463. invalidate_inodes(sdp->sd_vfs);
  1464. up_write(&gfs2_umount_flush_sem);
  1465. msleep(10);
  1466. }
  1467. }
  1468. /*
  1469. * Diagnostic routines to help debug distributed deadlock
  1470. */
  1471. static void gfs2_print_symbol(struct glock_iter *gi, const char *fmt,
  1472. unsigned long address)
  1473. {
  1474. char buffer[KSYM_SYMBOL_LEN];
  1475. sprint_symbol(buffer, address);
  1476. print_dbg(gi, fmt, buffer);
  1477. }
  1478. /**
  1479. * dump_holder - print information about a glock holder
  1480. * @str: a string naming the type of holder
  1481. * @gh: the glock holder
  1482. *
  1483. * Returns: 0 on success, -ENOBUFS when we run out of space
  1484. */
  1485. static int dump_holder(struct glock_iter *gi, char *str,
  1486. struct gfs2_holder *gh)
  1487. {
  1488. unsigned int x;
  1489. struct task_struct *gh_owner;
  1490. print_dbg(gi, " %s\n", str);
  1491. if (gh->gh_owner_pid) {
  1492. print_dbg(gi, " owner = %ld ", (long)gh->gh_owner_pid);
  1493. gh_owner = find_task_by_pid(gh->gh_owner_pid);
  1494. if (gh_owner)
  1495. print_dbg(gi, "(%s)\n", gh_owner->comm);
  1496. else
  1497. print_dbg(gi, "(ended)\n");
  1498. } else
  1499. print_dbg(gi, " owner = -1\n");
  1500. print_dbg(gi, " gh_state = %u\n", gh->gh_state);
  1501. print_dbg(gi, " gh_flags =");
  1502. for (x = 0; x < 32; x++)
  1503. if (gh->gh_flags & (1 << x))
  1504. print_dbg(gi, " %u", x);
  1505. print_dbg(gi, " \n");
  1506. print_dbg(gi, " error = %d\n", gh->gh_error);
  1507. print_dbg(gi, " gh_iflags =");
  1508. for (x = 0; x < 32; x++)
  1509. if (test_bit(x, &gh->gh_iflags))
  1510. print_dbg(gi, " %u", x);
  1511. print_dbg(gi, " \n");
  1512. gfs2_print_symbol(gi, " initialized at: %s\n", gh->gh_ip);
  1513. return 0;
  1514. }
  1515. /**
  1516. * dump_inode - print information about an inode
  1517. * @ip: the inode
  1518. *
  1519. * Returns: 0 on success, -ENOBUFS when we run out of space
  1520. */
  1521. static int dump_inode(struct glock_iter *gi, struct gfs2_inode *ip)
  1522. {
  1523. unsigned int x;
  1524. print_dbg(gi, " Inode:\n");
  1525. print_dbg(gi, " num = %llu/%llu\n",
  1526. ip->i_num.no_formal_ino, ip->i_num.no_addr);
  1527. print_dbg(gi, " type = %u\n", IF2DT(ip->i_inode.i_mode));
  1528. print_dbg(gi, " i_flags =");
  1529. for (x = 0; x < 32; x++)
  1530. if (test_bit(x, &ip->i_flags))
  1531. print_dbg(gi, " %u", x);
  1532. print_dbg(gi, " \n");
  1533. return 0;
  1534. }
  1535. /**
  1536. * dump_glock - print information about a glock
  1537. * @gl: the glock
  1538. * @count: where we are in the buffer
  1539. *
  1540. * Returns: 0 on success, -ENOBUFS when we run out of space
  1541. */
  1542. static int dump_glock(struct glock_iter *gi, struct gfs2_glock *gl)
  1543. {
  1544. struct gfs2_holder *gh;
  1545. unsigned int x;
  1546. int error = -ENOBUFS;
  1547. struct task_struct *gl_owner;
  1548. spin_lock(&gl->gl_spin);
  1549. print_dbg(gi, "Glock 0x%p (%u, %llu)\n", gl, gl->gl_name.ln_type,
  1550. (unsigned long long)gl->gl_name.ln_number);
  1551. print_dbg(gi, " gl_flags =");
  1552. for (x = 0; x < 32; x++) {
  1553. if (test_bit(x, &gl->gl_flags))
  1554. print_dbg(gi, " %u", x);
  1555. }
  1556. if (!test_bit(GLF_LOCK, &gl->gl_flags))
  1557. print_dbg(gi, " (unlocked)");
  1558. print_dbg(gi, " \n");
  1559. print_dbg(gi, " gl_ref = %d\n", atomic_read(&gl->gl_ref));
  1560. print_dbg(gi, " gl_state = %u\n", gl->gl_state);
  1561. if (gl->gl_owner_pid) {
  1562. gl_owner = find_task_by_pid(gl->gl_owner_pid);
  1563. if (gl_owner)
  1564. print_dbg(gi, " gl_owner = pid %d (%s)\n",
  1565. gl->gl_owner_pid, gl_owner->comm);
  1566. else
  1567. print_dbg(gi, " gl_owner = %d (ended)\n",
  1568. gl->gl_owner_pid);
  1569. } else
  1570. print_dbg(gi, " gl_owner = -1\n");
  1571. print_dbg(gi, " gl_ip = %lu\n", gl->gl_ip);
  1572. print_dbg(gi, " req_gh = %s\n", (gl->gl_req_gh) ? "yes" : "no");
  1573. print_dbg(gi, " req_bh = %s\n", (gl->gl_req_bh) ? "yes" : "no");
  1574. print_dbg(gi, " lvb_count = %d\n", atomic_read(&gl->gl_lvb_count));
  1575. print_dbg(gi, " object = %s\n", (gl->gl_object) ? "yes" : "no");
  1576. print_dbg(gi, " le = %s\n",
  1577. (list_empty(&gl->gl_le.le_list)) ? "no" : "yes");
  1578. print_dbg(gi, " reclaim = %s\n",
  1579. (list_empty(&gl->gl_reclaim)) ? "no" : "yes");
  1580. if (gl->gl_aspace)
  1581. print_dbg(gi, " aspace = 0x%p nrpages = %lu\n", gl->gl_aspace,
  1582. gl->gl_aspace->i_mapping->nrpages);
  1583. else
  1584. print_dbg(gi, " aspace = no\n");
  1585. print_dbg(gi, " ail = %d\n", atomic_read(&gl->gl_ail_count));
  1586. if (gl->gl_req_gh) {
  1587. error = dump_holder(gi, "Request", gl->gl_req_gh);
  1588. if (error)
  1589. goto out;
  1590. }
  1591. list_for_each_entry(gh, &gl->gl_holders, gh_list) {
  1592. error = dump_holder(gi, "Holder", gh);
  1593. if (error)
  1594. goto out;
  1595. }
  1596. list_for_each_entry(gh, &gl->gl_waiters1, gh_list) {
  1597. error = dump_holder(gi, "Waiter1", gh);
  1598. if (error)
  1599. goto out;
  1600. }
  1601. list_for_each_entry(gh, &gl->gl_waiters3, gh_list) {
  1602. error = dump_holder(gi, "Waiter3", gh);
  1603. if (error)
  1604. goto out;
  1605. }
  1606. if (test_bit(GLF_DEMOTE, &gl->gl_flags)) {
  1607. print_dbg(gi, " Demotion req to state %u (%llu uS ago)\n",
  1608. gl->gl_demote_state,
  1609. (u64)(jiffies - gl->gl_demote_time)*(1000000/HZ));
  1610. }
  1611. if (gl->gl_ops == &gfs2_inode_glops && gl->gl_object) {
  1612. if (!test_bit(GLF_LOCK, &gl->gl_flags) &&
  1613. list_empty(&gl->gl_holders)) {
  1614. error = dump_inode(gi, gl->gl_object);
  1615. if (error)
  1616. goto out;
  1617. } else {
  1618. error = -ENOBUFS;
  1619. print_dbg(gi, " Inode: busy\n");
  1620. }
  1621. }
  1622. error = 0;
  1623. out:
  1624. spin_unlock(&gl->gl_spin);
  1625. return error;
  1626. }
  1627. /**
  1628. * gfs2_dump_lockstate - print out the current lockstate
  1629. * @sdp: the filesystem
  1630. * @ub: the buffer to copy the information into
  1631. *
  1632. * If @ub is NULL, dump the lockstate to the console.
  1633. *
  1634. */
  1635. static int gfs2_dump_lockstate(struct gfs2_sbd *sdp)
  1636. {
  1637. struct gfs2_glock *gl;
  1638. struct hlist_node *h;
  1639. unsigned int x;
  1640. int error = 0;
  1641. for (x = 0; x < GFS2_GL_HASH_SIZE; x++) {
  1642. read_lock(gl_lock_addr(x));
  1643. hlist_for_each_entry(gl, h, &gl_hash_table[x].hb_list, gl_list) {
  1644. if (gl->gl_sbd != sdp)
  1645. continue;
  1646. error = dump_glock(NULL, gl);
  1647. if (error)
  1648. break;
  1649. }
  1650. read_unlock(gl_lock_addr(x));
  1651. if (error)
  1652. break;
  1653. }
  1654. return error;
  1655. }
  1656. int __init gfs2_glock_init(void)
  1657. {
  1658. unsigned i;
  1659. for(i = 0; i < GFS2_GL_HASH_SIZE; i++) {
  1660. INIT_HLIST_HEAD(&gl_hash_table[i].hb_list);
  1661. }
  1662. #ifdef GL_HASH_LOCK_SZ
  1663. for(i = 0; i < GL_HASH_LOCK_SZ; i++) {
  1664. rwlock_init(&gl_hash_locks[i]);
  1665. }
  1666. #endif
  1667. return 0;
  1668. }
  1669. static int gfs2_glock_iter_next(struct glock_iter *gi)
  1670. {
  1671. read_lock(gl_lock_addr(gi->hash));
  1672. while (1) {
  1673. if (!gi->hb_list) { /* If we don't have a hash bucket yet */
  1674. gi->hb_list = &gl_hash_table[gi->hash].hb_list;
  1675. if (hlist_empty(gi->hb_list)) {
  1676. read_unlock(gl_lock_addr(gi->hash));
  1677. gi->hash++;
  1678. read_lock(gl_lock_addr(gi->hash));
  1679. gi->hb_list = NULL;
  1680. if (gi->hash >= GFS2_GL_HASH_SIZE) {
  1681. read_unlock(gl_lock_addr(gi->hash));
  1682. return 1;
  1683. }
  1684. else
  1685. continue;
  1686. }
  1687. if (!hlist_empty(gi->hb_list)) {
  1688. gi->gl = list_entry(gi->hb_list->first,
  1689. struct gfs2_glock,
  1690. gl_list);
  1691. }
  1692. } else {
  1693. if (gi->gl->gl_list.next == NULL) {
  1694. read_unlock(gl_lock_addr(gi->hash));
  1695. gi->hash++;
  1696. read_lock(gl_lock_addr(gi->hash));
  1697. gi->hb_list = NULL;
  1698. continue;
  1699. }
  1700. gi->gl = list_entry(gi->gl->gl_list.next,
  1701. struct gfs2_glock, gl_list);
  1702. }
  1703. if (gi->gl)
  1704. break;
  1705. }
  1706. read_unlock(gl_lock_addr(gi->hash));
  1707. return 0;
  1708. }
  1709. static void gfs2_glock_iter_free(struct glock_iter *gi)
  1710. {
  1711. kfree(gi);
  1712. }
  1713. static struct glock_iter *gfs2_glock_iter_init(struct gfs2_sbd *sdp)
  1714. {
  1715. struct glock_iter *gi;
  1716. gi = kmalloc(sizeof (*gi), GFP_KERNEL);
  1717. if (!gi)
  1718. return NULL;
  1719. gi->sdp = sdp;
  1720. gi->hash = 0;
  1721. gi->gl = NULL;
  1722. gi->hb_list = NULL;
  1723. gi->seq = NULL;
  1724. memset(gi->string, 0, sizeof(gi->string));
  1725. if (gfs2_glock_iter_next(gi)) {
  1726. gfs2_glock_iter_free(gi);
  1727. return NULL;
  1728. }
  1729. return gi;
  1730. }
  1731. static void *gfs2_glock_seq_start(struct seq_file *file, loff_t *pos)
  1732. {
  1733. struct glock_iter *gi;
  1734. loff_t n = *pos;
  1735. gi = gfs2_glock_iter_init(file->private);
  1736. if (!gi)
  1737. return NULL;
  1738. while (n--) {
  1739. if (gfs2_glock_iter_next(gi)) {
  1740. gfs2_glock_iter_free(gi);
  1741. return NULL;
  1742. }
  1743. }
  1744. return gi;
  1745. }
  1746. static void *gfs2_glock_seq_next(struct seq_file *file, void *iter_ptr,
  1747. loff_t *pos)
  1748. {
  1749. struct glock_iter *gi = iter_ptr;
  1750. (*pos)++;
  1751. if (gfs2_glock_iter_next(gi)) {
  1752. gfs2_glock_iter_free(gi);
  1753. return NULL;
  1754. }
  1755. return gi;
  1756. }
  1757. static void gfs2_glock_seq_stop(struct seq_file *file, void *iter_ptr)
  1758. {
  1759. /* nothing for now */
  1760. }
  1761. static int gfs2_glock_seq_show(struct seq_file *file, void *iter_ptr)
  1762. {
  1763. struct glock_iter *gi = iter_ptr;
  1764. gi->seq = file;
  1765. dump_glock(gi, gi->gl);
  1766. return 0;
  1767. }
  1768. static struct seq_operations gfs2_glock_seq_ops = {
  1769. .start = gfs2_glock_seq_start,
  1770. .next = gfs2_glock_seq_next,
  1771. .stop = gfs2_glock_seq_stop,
  1772. .show = gfs2_glock_seq_show,
  1773. };
  1774. static int gfs2_debugfs_open(struct inode *inode, struct file *file)
  1775. {
  1776. struct seq_file *seq;
  1777. int ret;
  1778. ret = seq_open(file, &gfs2_glock_seq_ops);
  1779. if (ret)
  1780. return ret;
  1781. seq = file->private_data;
  1782. seq->private = inode->i_private;
  1783. return 0;
  1784. }
  1785. static const struct file_operations gfs2_debug_fops = {
  1786. .owner = THIS_MODULE,
  1787. .open = gfs2_debugfs_open,
  1788. .read = seq_read,
  1789. .llseek = seq_lseek,
  1790. .release = seq_release
  1791. };
  1792. int gfs2_create_debugfs_file(struct gfs2_sbd *sdp)
  1793. {
  1794. sdp->debugfs_dir = debugfs_create_dir(sdp->sd_table_name, gfs2_root);
  1795. if (!sdp->debugfs_dir)
  1796. return -ENOMEM;
  1797. sdp->debugfs_dentry_glocks = debugfs_create_file("glocks",
  1798. S_IFREG | S_IRUGO,
  1799. sdp->debugfs_dir, sdp,
  1800. &gfs2_debug_fops);
  1801. if (!sdp->debugfs_dentry_glocks)
  1802. return -ENOMEM;
  1803. return 0;
  1804. }
  1805. void gfs2_delete_debugfs_file(struct gfs2_sbd *sdp)
  1806. {
  1807. if (sdp && sdp->debugfs_dir) {
  1808. if (sdp->debugfs_dentry_glocks) {
  1809. debugfs_remove(sdp->debugfs_dentry_glocks);
  1810. sdp->debugfs_dentry_glocks = NULL;
  1811. }
  1812. debugfs_remove(sdp->debugfs_dir);
  1813. sdp->debugfs_dir = NULL;
  1814. }
  1815. }
  1816. int gfs2_register_debugfs(void)
  1817. {
  1818. gfs2_root = debugfs_create_dir("gfs2", NULL);
  1819. return gfs2_root ? 0 : -ENOMEM;
  1820. }
  1821. void gfs2_unregister_debugfs(void)
  1822. {
  1823. debugfs_remove(gfs2_root);
  1824. gfs2_root = NULL;
  1825. }