glock.c 49 KB

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