glock.c 50 KB

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