glock.c 48 KB

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