glock.c 51 KB

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