glock.c 49 KB

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