glock.c 51 KB

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