glock.c 45 KB

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