glock.c 46 KB

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