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