glock.c 51 KB

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