glock.c 51 KB

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