glock.c 52 KB

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