glock.c 47 KB

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