glock.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092
  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/buffer_head.h>
  13. #include <linux/delay.h>
  14. #include <linux/sort.h>
  15. #include <linux/jhash.h>
  16. #include <linux/kallsyms.h>
  17. #include <linux/gfs2_ondisk.h>
  18. #include <linux/list.h>
  19. #include <linux/wait.h>
  20. #include <linux/module.h>
  21. #include <asm/uaccess.h>
  22. #include <linux/seq_file.h>
  23. #include <linux/debugfs.h>
  24. #include <linux/kthread.h>
  25. #include <linux/freezer.h>
  26. #include <linux/workqueue.h>
  27. #include <linux/jiffies.h>
  28. #include <linux/rcupdate.h>
  29. #include <linux/rculist_bl.h>
  30. #include <linux/bit_spinlock.h>
  31. #include <linux/percpu.h>
  32. #include "gfs2.h"
  33. #include "incore.h"
  34. #include "glock.h"
  35. #include "glops.h"
  36. #include "inode.h"
  37. #include "lops.h"
  38. #include "meta_io.h"
  39. #include "quota.h"
  40. #include "super.h"
  41. #include "util.h"
  42. #include "bmap.h"
  43. #define CREATE_TRACE_POINTS
  44. #include "trace_gfs2.h"
  45. struct gfs2_glock_iter {
  46. int hash; /* hash bucket index */
  47. unsigned nhash; /* Index within current bucket */
  48. struct gfs2_sbd *sdp; /* incore superblock */
  49. struct gfs2_glock *gl; /* current glock struct */
  50. loff_t last_pos; /* last position */
  51. };
  52. typedef void (*glock_examiner) (struct gfs2_glock * gl);
  53. static int __dump_glock(struct seq_file *seq, const struct gfs2_glock *gl);
  54. #define GLOCK_BUG_ON(gl,x) do { if (unlikely(x)) { __dump_glock(NULL, gl); BUG(); } } while(0)
  55. static void do_xmote(struct gfs2_glock *gl, struct gfs2_holder *gh, unsigned int target);
  56. static struct dentry *gfs2_root;
  57. static struct workqueue_struct *glock_workqueue;
  58. struct workqueue_struct *gfs2_delete_workqueue;
  59. static LIST_HEAD(lru_list);
  60. static atomic_t lru_count = ATOMIC_INIT(0);
  61. static DEFINE_SPINLOCK(lru_lock);
  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 hlist_bl_head gl_hash_table[GFS2_GL_HASH_SIZE];
  66. static struct dentry *gfs2_root;
  67. /**
  68. * gl_hash() - Turn glock number into hash bucket number
  69. * @lock: The glock number
  70. *
  71. * Returns: The number of the corresponding hash bucket
  72. */
  73. static unsigned int gl_hash(const struct gfs2_sbd *sdp,
  74. const struct lm_lockname *name)
  75. {
  76. unsigned int h;
  77. h = jhash(&name->ln_number, sizeof(u64), 0);
  78. h = jhash(&name->ln_type, sizeof(unsigned int), h);
  79. h = jhash(&sdp, sizeof(struct gfs2_sbd *), h);
  80. h &= GFS2_GL_HASH_MASK;
  81. return h;
  82. }
  83. static inline void spin_lock_bucket(unsigned int hash)
  84. {
  85. hlist_bl_lock(&gl_hash_table[hash]);
  86. }
  87. static inline void spin_unlock_bucket(unsigned int hash)
  88. {
  89. hlist_bl_unlock(&gl_hash_table[hash]);
  90. }
  91. static void gfs2_glock_dealloc(struct rcu_head *rcu)
  92. {
  93. struct gfs2_glock *gl = container_of(rcu, struct gfs2_glock, gl_rcu);
  94. if (gl->gl_ops->go_flags & GLOF_ASPACE)
  95. kmem_cache_free(gfs2_glock_aspace_cachep, gl);
  96. else
  97. kmem_cache_free(gfs2_glock_cachep, gl);
  98. }
  99. void gfs2_glock_free(struct gfs2_glock *gl)
  100. {
  101. struct gfs2_sbd *sdp = gl->gl_sbd;
  102. call_rcu(&gl->gl_rcu, gfs2_glock_dealloc);
  103. if (atomic_dec_and_test(&sdp->sd_glock_disposal))
  104. wake_up(&sdp->sd_glock_wait);
  105. }
  106. /**
  107. * gfs2_glock_hold() - increment reference count on glock
  108. * @gl: The glock to hold
  109. *
  110. */
  111. void gfs2_glock_hold(struct gfs2_glock *gl)
  112. {
  113. GLOCK_BUG_ON(gl, atomic_read(&gl->gl_ref) == 0);
  114. atomic_inc(&gl->gl_ref);
  115. }
  116. /**
  117. * demote_ok - Check to see if it's ok to unlock a glock
  118. * @gl: the glock
  119. *
  120. * Returns: 1 if it's ok
  121. */
  122. static int demote_ok(const struct gfs2_glock *gl)
  123. {
  124. const struct gfs2_glock_operations *glops = gl->gl_ops;
  125. if (gl->gl_state == LM_ST_UNLOCKED)
  126. return 0;
  127. if (!list_empty(&gl->gl_holders))
  128. return 0;
  129. if (glops->go_demote_ok)
  130. return glops->go_demote_ok(gl);
  131. return 1;
  132. }
  133. void gfs2_glock_add_to_lru(struct gfs2_glock *gl)
  134. {
  135. spin_lock(&lru_lock);
  136. if (!list_empty(&gl->gl_lru))
  137. list_del_init(&gl->gl_lru);
  138. else
  139. atomic_inc(&lru_count);
  140. list_add_tail(&gl->gl_lru, &lru_list);
  141. set_bit(GLF_LRU, &gl->gl_flags);
  142. spin_unlock(&lru_lock);
  143. }
  144. static void __gfs2_glock_remove_from_lru(struct gfs2_glock *gl)
  145. {
  146. if (!list_empty(&gl->gl_lru)) {
  147. list_del_init(&gl->gl_lru);
  148. atomic_dec(&lru_count);
  149. clear_bit(GLF_LRU, &gl->gl_flags);
  150. }
  151. }
  152. static void gfs2_glock_remove_from_lru(struct gfs2_glock *gl)
  153. {
  154. spin_lock(&lru_lock);
  155. __gfs2_glock_remove_from_lru(gl);
  156. spin_unlock(&lru_lock);
  157. }
  158. /**
  159. * gfs2_glock_put_nolock() - Decrement reference count on glock
  160. * @gl: The glock to put
  161. *
  162. * This function should only be used if the caller has its own reference
  163. * to the glock, in addition to the one it is dropping.
  164. */
  165. void gfs2_glock_put_nolock(struct gfs2_glock *gl)
  166. {
  167. if (atomic_dec_and_test(&gl->gl_ref))
  168. GLOCK_BUG_ON(gl, 1);
  169. }
  170. /**
  171. * gfs2_glock_put() - Decrement reference count on glock
  172. * @gl: The glock to put
  173. *
  174. */
  175. void gfs2_glock_put(struct gfs2_glock *gl)
  176. {
  177. struct gfs2_sbd *sdp = gl->gl_sbd;
  178. struct address_space *mapping = gfs2_glock2aspace(gl);
  179. if (atomic_dec_and_lock(&gl->gl_ref, &lru_lock)) {
  180. __gfs2_glock_remove_from_lru(gl);
  181. spin_unlock(&lru_lock);
  182. spin_lock_bucket(gl->gl_hash);
  183. hlist_bl_del_rcu(&gl->gl_list);
  184. spin_unlock_bucket(gl->gl_hash);
  185. GLOCK_BUG_ON(gl, !list_empty(&gl->gl_holders));
  186. GLOCK_BUG_ON(gl, mapping && mapping->nrpages);
  187. trace_gfs2_glock_put(gl);
  188. sdp->sd_lockstruct.ls_ops->lm_put_lock(gl);
  189. }
  190. }
  191. /**
  192. * search_bucket() - Find struct gfs2_glock by lock number
  193. * @bucket: the bucket to search
  194. * @name: The lock name
  195. *
  196. * Returns: NULL, or the struct gfs2_glock with the requested number
  197. */
  198. static struct gfs2_glock *search_bucket(unsigned int hash,
  199. const struct gfs2_sbd *sdp,
  200. const struct lm_lockname *name)
  201. {
  202. struct gfs2_glock *gl;
  203. struct hlist_bl_node *h;
  204. hlist_bl_for_each_entry_rcu(gl, h, &gl_hash_table[hash], gl_list) {
  205. if (!lm_name_equal(&gl->gl_name, name))
  206. continue;
  207. if (gl->gl_sbd != sdp)
  208. continue;
  209. if (atomic_inc_not_zero(&gl->gl_ref))
  210. return gl;
  211. }
  212. return NULL;
  213. }
  214. /**
  215. * may_grant - check if its ok to grant a new lock
  216. * @gl: The glock
  217. * @gh: The lock request which we wish to grant
  218. *
  219. * Returns: true if its ok to grant the lock
  220. */
  221. static inline int may_grant(const struct gfs2_glock *gl, const struct gfs2_holder *gh)
  222. {
  223. const struct gfs2_holder *gh_head = list_entry(gl->gl_holders.next, const struct gfs2_holder, gh_list);
  224. if ((gh->gh_state == LM_ST_EXCLUSIVE ||
  225. gh_head->gh_state == LM_ST_EXCLUSIVE) && gh != gh_head)
  226. return 0;
  227. if (gl->gl_state == gh->gh_state)
  228. return 1;
  229. if (gh->gh_flags & GL_EXACT)
  230. return 0;
  231. if (gl->gl_state == LM_ST_EXCLUSIVE) {
  232. if (gh->gh_state == LM_ST_SHARED && gh_head->gh_state == LM_ST_SHARED)
  233. return 1;
  234. if (gh->gh_state == LM_ST_DEFERRED && gh_head->gh_state == LM_ST_DEFERRED)
  235. return 1;
  236. }
  237. if (gl->gl_state != LM_ST_UNLOCKED && (gh->gh_flags & LM_FLAG_ANY))
  238. return 1;
  239. return 0;
  240. }
  241. static void gfs2_holder_wake(struct gfs2_holder *gh)
  242. {
  243. clear_bit(HIF_WAIT, &gh->gh_iflags);
  244. smp_mb__after_clear_bit();
  245. wake_up_bit(&gh->gh_iflags, HIF_WAIT);
  246. }
  247. /**
  248. * do_error - Something unexpected has happened during a lock request
  249. *
  250. */
  251. static inline void do_error(struct gfs2_glock *gl, const int ret)
  252. {
  253. struct gfs2_holder *gh, *tmp;
  254. list_for_each_entry_safe(gh, tmp, &gl->gl_holders, gh_list) {
  255. if (test_bit(HIF_HOLDER, &gh->gh_iflags))
  256. continue;
  257. if (ret & LM_OUT_ERROR)
  258. gh->gh_error = -EIO;
  259. else if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB))
  260. gh->gh_error = GLR_TRYFAILED;
  261. else
  262. continue;
  263. list_del_init(&gh->gh_list);
  264. trace_gfs2_glock_queue(gh, 0);
  265. gfs2_holder_wake(gh);
  266. }
  267. }
  268. /**
  269. * do_promote - promote as many requests as possible on the current queue
  270. * @gl: The glock
  271. *
  272. * Returns: 1 if there is a blocked holder at the head of the list, or 2
  273. * if a type specific operation is underway.
  274. */
  275. static int do_promote(struct gfs2_glock *gl)
  276. __releases(&gl->gl_spin)
  277. __acquires(&gl->gl_spin)
  278. {
  279. const struct gfs2_glock_operations *glops = gl->gl_ops;
  280. struct gfs2_holder *gh, *tmp;
  281. int ret;
  282. restart:
  283. list_for_each_entry_safe(gh, tmp, &gl->gl_holders, gh_list) {
  284. if (test_bit(HIF_HOLDER, &gh->gh_iflags))
  285. continue;
  286. if (may_grant(gl, gh)) {
  287. if (gh->gh_list.prev == &gl->gl_holders &&
  288. glops->go_lock) {
  289. spin_unlock(&gl->gl_spin);
  290. /* FIXME: eliminate this eventually */
  291. ret = glops->go_lock(gh);
  292. spin_lock(&gl->gl_spin);
  293. if (ret) {
  294. if (ret == 1)
  295. return 2;
  296. gh->gh_error = ret;
  297. list_del_init(&gh->gh_list);
  298. trace_gfs2_glock_queue(gh, 0);
  299. gfs2_holder_wake(gh);
  300. goto restart;
  301. }
  302. set_bit(HIF_HOLDER, &gh->gh_iflags);
  303. trace_gfs2_promote(gh, 1);
  304. gfs2_holder_wake(gh);
  305. goto restart;
  306. }
  307. set_bit(HIF_HOLDER, &gh->gh_iflags);
  308. trace_gfs2_promote(gh, 0);
  309. gfs2_holder_wake(gh);
  310. continue;
  311. }
  312. if (gh->gh_list.prev == &gl->gl_holders)
  313. return 1;
  314. do_error(gl, 0);
  315. break;
  316. }
  317. return 0;
  318. }
  319. /**
  320. * find_first_waiter - find the first gh that's waiting for the glock
  321. * @gl: the glock
  322. */
  323. static inline struct gfs2_holder *find_first_waiter(const struct gfs2_glock *gl)
  324. {
  325. struct gfs2_holder *gh;
  326. list_for_each_entry(gh, &gl->gl_holders, gh_list) {
  327. if (!test_bit(HIF_HOLDER, &gh->gh_iflags))
  328. return gh;
  329. }
  330. return NULL;
  331. }
  332. /**
  333. * state_change - record that the glock is now in a different state
  334. * @gl: the glock
  335. * @new_state the new state
  336. *
  337. */
  338. static void state_change(struct gfs2_glock *gl, unsigned int new_state)
  339. {
  340. int held1, held2;
  341. held1 = (gl->gl_state != LM_ST_UNLOCKED);
  342. held2 = (new_state != LM_ST_UNLOCKED);
  343. if (held1 != held2) {
  344. if (held2)
  345. gfs2_glock_hold(gl);
  346. else
  347. gfs2_glock_put_nolock(gl);
  348. }
  349. if (held1 && held2 && list_empty(&gl->gl_holders))
  350. clear_bit(GLF_QUEUED, &gl->gl_flags);
  351. if (new_state != gl->gl_target)
  352. /* shorten our minimum hold time */
  353. gl->gl_hold_time = max(gl->gl_hold_time - GL_GLOCK_HOLD_DECR,
  354. GL_GLOCK_MIN_HOLD);
  355. gl->gl_state = new_state;
  356. gl->gl_tchange = jiffies;
  357. }
  358. static void gfs2_demote_wake(struct gfs2_glock *gl)
  359. {
  360. gl->gl_demote_state = LM_ST_EXCLUSIVE;
  361. clear_bit(GLF_DEMOTE, &gl->gl_flags);
  362. smp_mb__after_clear_bit();
  363. wake_up_bit(&gl->gl_flags, GLF_DEMOTE);
  364. }
  365. /**
  366. * finish_xmote - The DLM has replied to one of our lock requests
  367. * @gl: The glock
  368. * @ret: The status from the DLM
  369. *
  370. */
  371. static void finish_xmote(struct gfs2_glock *gl, unsigned int ret)
  372. {
  373. const struct gfs2_glock_operations *glops = gl->gl_ops;
  374. struct gfs2_holder *gh;
  375. unsigned state = ret & LM_OUT_ST_MASK;
  376. int rv;
  377. spin_lock(&gl->gl_spin);
  378. trace_gfs2_glock_state_change(gl, state);
  379. state_change(gl, state);
  380. gh = find_first_waiter(gl);
  381. /* Demote to UN request arrived during demote to SH or DF */
  382. if (test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags) &&
  383. state != LM_ST_UNLOCKED && gl->gl_demote_state == LM_ST_UNLOCKED)
  384. gl->gl_target = LM_ST_UNLOCKED;
  385. /* Check for state != intended state */
  386. if (unlikely(state != gl->gl_target)) {
  387. if (gh && !test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags)) {
  388. /* move to back of queue and try next entry */
  389. if (ret & LM_OUT_CANCELED) {
  390. if ((gh->gh_flags & LM_FLAG_PRIORITY) == 0)
  391. list_move_tail(&gh->gh_list, &gl->gl_holders);
  392. gh = find_first_waiter(gl);
  393. gl->gl_target = gh->gh_state;
  394. goto retry;
  395. }
  396. /* Some error or failed "try lock" - report it */
  397. if ((ret & LM_OUT_ERROR) ||
  398. (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB))) {
  399. gl->gl_target = gl->gl_state;
  400. do_error(gl, ret);
  401. goto out;
  402. }
  403. }
  404. switch(state) {
  405. /* Unlocked due to conversion deadlock, try again */
  406. case LM_ST_UNLOCKED:
  407. retry:
  408. do_xmote(gl, gh, gl->gl_target);
  409. break;
  410. /* Conversion fails, unlock and try again */
  411. case LM_ST_SHARED:
  412. case LM_ST_DEFERRED:
  413. do_xmote(gl, gh, LM_ST_UNLOCKED);
  414. break;
  415. default: /* Everything else */
  416. printk(KERN_ERR "GFS2: wanted %u got %u\n", gl->gl_target, state);
  417. GLOCK_BUG_ON(gl, 1);
  418. }
  419. spin_unlock(&gl->gl_spin);
  420. return;
  421. }
  422. /* Fast path - we got what we asked for */
  423. if (test_and_clear_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags))
  424. gfs2_demote_wake(gl);
  425. if (state != LM_ST_UNLOCKED) {
  426. if (glops->go_xmote_bh) {
  427. spin_unlock(&gl->gl_spin);
  428. rv = glops->go_xmote_bh(gl, gh);
  429. spin_lock(&gl->gl_spin);
  430. if (rv) {
  431. do_error(gl, rv);
  432. goto out;
  433. }
  434. }
  435. rv = do_promote(gl);
  436. if (rv == 2)
  437. goto out_locked;
  438. }
  439. out:
  440. clear_bit(GLF_LOCK, &gl->gl_flags);
  441. out_locked:
  442. spin_unlock(&gl->gl_spin);
  443. }
  444. /**
  445. * do_xmote - Calls the DLM to change the state of a lock
  446. * @gl: The lock state
  447. * @gh: The holder (only for promotes)
  448. * @target: The target lock state
  449. *
  450. */
  451. static void do_xmote(struct gfs2_glock *gl, struct gfs2_holder *gh, unsigned int target)
  452. __releases(&gl->gl_spin)
  453. __acquires(&gl->gl_spin)
  454. {
  455. const struct gfs2_glock_operations *glops = gl->gl_ops;
  456. struct gfs2_sbd *sdp = gl->gl_sbd;
  457. unsigned int lck_flags = gh ? gh->gh_flags : 0;
  458. int ret;
  459. lck_flags &= (LM_FLAG_TRY | LM_FLAG_TRY_1CB | LM_FLAG_NOEXP |
  460. LM_FLAG_PRIORITY);
  461. GLOCK_BUG_ON(gl, gl->gl_state == target);
  462. GLOCK_BUG_ON(gl, gl->gl_state == gl->gl_target);
  463. if ((target == LM_ST_UNLOCKED || target == LM_ST_DEFERRED) &&
  464. glops->go_inval) {
  465. set_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags);
  466. do_error(gl, 0); /* Fail queued try locks */
  467. }
  468. gl->gl_req = target;
  469. set_bit(GLF_BLOCKING, &gl->gl_flags);
  470. if ((gl->gl_req == LM_ST_UNLOCKED) ||
  471. (gl->gl_state == LM_ST_EXCLUSIVE) ||
  472. (lck_flags & (LM_FLAG_TRY|LM_FLAG_TRY_1CB)))
  473. clear_bit(GLF_BLOCKING, &gl->gl_flags);
  474. spin_unlock(&gl->gl_spin);
  475. if (glops->go_xmote_th)
  476. glops->go_xmote_th(gl);
  477. if (test_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags))
  478. glops->go_inval(gl, target == LM_ST_DEFERRED ? 0 : DIO_METADATA);
  479. clear_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags);
  480. gfs2_glock_hold(gl);
  481. if (sdp->sd_lockstruct.ls_ops->lm_lock) {
  482. /* lock_dlm */
  483. ret = sdp->sd_lockstruct.ls_ops->lm_lock(gl, target, lck_flags);
  484. GLOCK_BUG_ON(gl, ret);
  485. } else { /* lock_nolock */
  486. finish_xmote(gl, target);
  487. if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
  488. gfs2_glock_put(gl);
  489. }
  490. spin_lock(&gl->gl_spin);
  491. }
  492. /**
  493. * find_first_holder - find the first "holder" gh
  494. * @gl: the glock
  495. */
  496. static inline struct gfs2_holder *find_first_holder(const struct gfs2_glock *gl)
  497. {
  498. struct gfs2_holder *gh;
  499. if (!list_empty(&gl->gl_holders)) {
  500. gh = list_entry(gl->gl_holders.next, struct gfs2_holder, gh_list);
  501. if (test_bit(HIF_HOLDER, &gh->gh_iflags))
  502. return gh;
  503. }
  504. return NULL;
  505. }
  506. /**
  507. * run_queue - do all outstanding tasks related to a glock
  508. * @gl: The glock in question
  509. * @nonblock: True if we must not block in run_queue
  510. *
  511. */
  512. static void run_queue(struct gfs2_glock *gl, const int nonblock)
  513. __releases(&gl->gl_spin)
  514. __acquires(&gl->gl_spin)
  515. {
  516. struct gfs2_holder *gh = NULL;
  517. int ret;
  518. if (test_and_set_bit(GLF_LOCK, &gl->gl_flags))
  519. return;
  520. GLOCK_BUG_ON(gl, test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags));
  521. if (test_bit(GLF_DEMOTE, &gl->gl_flags) &&
  522. gl->gl_demote_state != gl->gl_state) {
  523. if (find_first_holder(gl))
  524. goto out_unlock;
  525. if (nonblock)
  526. goto out_sched;
  527. set_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags);
  528. GLOCK_BUG_ON(gl, gl->gl_demote_state == LM_ST_EXCLUSIVE);
  529. gl->gl_target = gl->gl_demote_state;
  530. } else {
  531. if (test_bit(GLF_DEMOTE, &gl->gl_flags))
  532. gfs2_demote_wake(gl);
  533. ret = do_promote(gl);
  534. if (ret == 0)
  535. goto out_unlock;
  536. if (ret == 2)
  537. goto out;
  538. gh = find_first_waiter(gl);
  539. gl->gl_target = gh->gh_state;
  540. if (!(gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)))
  541. do_error(gl, 0); /* Fail queued try locks */
  542. }
  543. do_xmote(gl, gh, gl->gl_target);
  544. out:
  545. return;
  546. out_sched:
  547. clear_bit(GLF_LOCK, &gl->gl_flags);
  548. smp_mb__after_clear_bit();
  549. gfs2_glock_hold(gl);
  550. if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
  551. gfs2_glock_put_nolock(gl);
  552. return;
  553. out_unlock:
  554. clear_bit(GLF_LOCK, &gl->gl_flags);
  555. smp_mb__after_clear_bit();
  556. return;
  557. }
  558. static void delete_work_func(struct work_struct *work)
  559. {
  560. struct gfs2_glock *gl = container_of(work, struct gfs2_glock, gl_delete);
  561. struct gfs2_sbd *sdp = gl->gl_sbd;
  562. struct gfs2_inode *ip;
  563. struct inode *inode;
  564. u64 no_addr = gl->gl_name.ln_number;
  565. ip = gl->gl_object;
  566. /* Note: Unsafe to dereference ip as we don't hold right refs/locks */
  567. if (ip)
  568. inode = gfs2_ilookup(sdp->sd_vfs, no_addr, 1);
  569. else
  570. inode = gfs2_lookup_by_inum(sdp, no_addr, NULL, GFS2_BLKST_UNLINKED);
  571. if (inode && !IS_ERR(inode)) {
  572. d_prune_aliases(inode);
  573. iput(inode);
  574. }
  575. gfs2_glock_put(gl);
  576. }
  577. static void glock_work_func(struct work_struct *work)
  578. {
  579. unsigned long delay = 0;
  580. struct gfs2_glock *gl = container_of(work, struct gfs2_glock, gl_work.work);
  581. int drop_ref = 0;
  582. if (test_and_clear_bit(GLF_REPLY_PENDING, &gl->gl_flags)) {
  583. finish_xmote(gl, gl->gl_reply);
  584. drop_ref = 1;
  585. }
  586. spin_lock(&gl->gl_spin);
  587. if (test_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) &&
  588. gl->gl_state != LM_ST_UNLOCKED &&
  589. gl->gl_demote_state != LM_ST_EXCLUSIVE) {
  590. unsigned long holdtime, now = jiffies;
  591. holdtime = gl->gl_tchange + gl->gl_hold_time;
  592. if (time_before(now, holdtime))
  593. delay = holdtime - now;
  594. if (!delay) {
  595. clear_bit(GLF_PENDING_DEMOTE, &gl->gl_flags);
  596. set_bit(GLF_DEMOTE, &gl->gl_flags);
  597. }
  598. }
  599. run_queue(gl, 0);
  600. spin_unlock(&gl->gl_spin);
  601. if (!delay)
  602. gfs2_glock_put(gl);
  603. else {
  604. if (gl->gl_name.ln_type != LM_TYPE_INODE)
  605. delay = 0;
  606. if (queue_delayed_work(glock_workqueue, &gl->gl_work, delay) == 0)
  607. gfs2_glock_put(gl);
  608. }
  609. if (drop_ref)
  610. gfs2_glock_put(gl);
  611. }
  612. /**
  613. * gfs2_glock_get() - Get a glock, or create one if one doesn't exist
  614. * @sdp: The GFS2 superblock
  615. * @number: the lock number
  616. * @glops: The glock_operations to use
  617. * @create: If 0, don't create the glock if it doesn't exist
  618. * @glp: the glock is returned here
  619. *
  620. * This does not lock a glock, just finds/creates structures for one.
  621. *
  622. * Returns: errno
  623. */
  624. int gfs2_glock_get(struct gfs2_sbd *sdp, u64 number,
  625. const struct gfs2_glock_operations *glops, int create,
  626. struct gfs2_glock **glp)
  627. {
  628. struct super_block *s = sdp->sd_vfs;
  629. struct lm_lockname name = { .ln_number = number, .ln_type = glops->go_type };
  630. struct gfs2_glock *gl, *tmp;
  631. unsigned int hash = gl_hash(sdp, &name);
  632. struct address_space *mapping;
  633. struct kmem_cache *cachep;
  634. rcu_read_lock();
  635. gl = search_bucket(hash, sdp, &name);
  636. rcu_read_unlock();
  637. *glp = gl;
  638. if (gl)
  639. return 0;
  640. if (!create)
  641. return -ENOENT;
  642. if (glops->go_flags & GLOF_ASPACE)
  643. cachep = gfs2_glock_aspace_cachep;
  644. else
  645. cachep = gfs2_glock_cachep;
  646. gl = kmem_cache_alloc(cachep, GFP_KERNEL);
  647. if (!gl)
  648. return -ENOMEM;
  649. atomic_inc(&sdp->sd_glock_disposal);
  650. gl->gl_sbd = sdp;
  651. gl->gl_flags = 0;
  652. gl->gl_name = name;
  653. atomic_set(&gl->gl_ref, 1);
  654. gl->gl_state = LM_ST_UNLOCKED;
  655. gl->gl_target = LM_ST_UNLOCKED;
  656. gl->gl_demote_state = LM_ST_EXCLUSIVE;
  657. gl->gl_hash = hash;
  658. gl->gl_ops = glops;
  659. gl->gl_dstamp = ktime_set(0, 0);
  660. preempt_disable();
  661. /* We use the global stats to estimate the initial per-glock stats */
  662. gl->gl_stats = this_cpu_ptr(sdp->sd_lkstats)->lkstats[glops->go_type];
  663. preempt_enable();
  664. gl->gl_stats.stats[GFS2_LKS_DCOUNT] = 0;
  665. gl->gl_stats.stats[GFS2_LKS_QCOUNT] = 0;
  666. memset(&gl->gl_lksb, 0, sizeof(struct dlm_lksb));
  667. memset(gl->gl_lvb, 0, 32 * sizeof(char));
  668. gl->gl_lksb.sb_lvbptr = gl->gl_lvb;
  669. gl->gl_tchange = jiffies;
  670. gl->gl_object = NULL;
  671. gl->gl_hold_time = GL_GLOCK_DFT_HOLD;
  672. INIT_DELAYED_WORK(&gl->gl_work, glock_work_func);
  673. INIT_WORK(&gl->gl_delete, delete_work_func);
  674. mapping = gfs2_glock2aspace(gl);
  675. if (mapping) {
  676. mapping->a_ops = &gfs2_meta_aops;
  677. mapping->host = s->s_bdev->bd_inode;
  678. mapping->flags = 0;
  679. mapping_set_gfp_mask(mapping, GFP_NOFS);
  680. mapping->assoc_mapping = NULL;
  681. mapping->backing_dev_info = s->s_bdi;
  682. mapping->writeback_index = 0;
  683. }
  684. spin_lock_bucket(hash);
  685. tmp = search_bucket(hash, sdp, &name);
  686. if (tmp) {
  687. spin_unlock_bucket(hash);
  688. kmem_cache_free(cachep, gl);
  689. atomic_dec(&sdp->sd_glock_disposal);
  690. gl = tmp;
  691. } else {
  692. hlist_bl_add_head_rcu(&gl->gl_list, &gl_hash_table[hash]);
  693. spin_unlock_bucket(hash);
  694. }
  695. *glp = gl;
  696. return 0;
  697. }
  698. /**
  699. * gfs2_holder_init - initialize a struct gfs2_holder in the default way
  700. * @gl: the glock
  701. * @state: the state we're requesting
  702. * @flags: the modifier flags
  703. * @gh: the holder structure
  704. *
  705. */
  706. void gfs2_holder_init(struct gfs2_glock *gl, unsigned int state, unsigned flags,
  707. struct gfs2_holder *gh)
  708. {
  709. INIT_LIST_HEAD(&gh->gh_list);
  710. gh->gh_gl = gl;
  711. gh->gh_ip = (unsigned long)__builtin_return_address(0);
  712. gh->gh_owner_pid = get_pid(task_pid(current));
  713. gh->gh_state = state;
  714. gh->gh_flags = flags;
  715. gh->gh_error = 0;
  716. gh->gh_iflags = 0;
  717. gfs2_glock_hold(gl);
  718. }
  719. /**
  720. * gfs2_holder_reinit - reinitialize a struct gfs2_holder so we can requeue it
  721. * @state: the state we're requesting
  722. * @flags: the modifier flags
  723. * @gh: the holder structure
  724. *
  725. * Don't mess with the glock.
  726. *
  727. */
  728. void gfs2_holder_reinit(unsigned int state, unsigned flags, struct gfs2_holder *gh)
  729. {
  730. gh->gh_state = state;
  731. gh->gh_flags = flags;
  732. gh->gh_iflags = 0;
  733. gh->gh_ip = (unsigned long)__builtin_return_address(0);
  734. if (gh->gh_owner_pid)
  735. put_pid(gh->gh_owner_pid);
  736. gh->gh_owner_pid = get_pid(task_pid(current));
  737. }
  738. /**
  739. * gfs2_holder_uninit - uninitialize a holder structure (drop glock reference)
  740. * @gh: the holder structure
  741. *
  742. */
  743. void gfs2_holder_uninit(struct gfs2_holder *gh)
  744. {
  745. put_pid(gh->gh_owner_pid);
  746. gfs2_glock_put(gh->gh_gl);
  747. gh->gh_gl = NULL;
  748. gh->gh_ip = 0;
  749. }
  750. /**
  751. * gfs2_glock_holder_wait
  752. * @word: unused
  753. *
  754. * This function and gfs2_glock_demote_wait both show up in the WCHAN
  755. * field. Thus I've separated these otherwise identical functions in
  756. * order to be more informative to the user.
  757. */
  758. static int gfs2_glock_holder_wait(void *word)
  759. {
  760. schedule();
  761. return 0;
  762. }
  763. static int gfs2_glock_demote_wait(void *word)
  764. {
  765. schedule();
  766. return 0;
  767. }
  768. /**
  769. * gfs2_glock_wait - wait on a glock acquisition
  770. * @gh: the glock holder
  771. *
  772. * Returns: 0 on success
  773. */
  774. int gfs2_glock_wait(struct gfs2_holder *gh)
  775. {
  776. unsigned long time1 = jiffies;
  777. might_sleep();
  778. wait_on_bit(&gh->gh_iflags, HIF_WAIT, gfs2_glock_holder_wait, TASK_UNINTERRUPTIBLE);
  779. if (time_after(jiffies, time1 + HZ)) /* have we waited > a second? */
  780. /* Lengthen the minimum hold time. */
  781. gh->gh_gl->gl_hold_time = min(gh->gh_gl->gl_hold_time +
  782. GL_GLOCK_HOLD_INCR,
  783. GL_GLOCK_MAX_HOLD);
  784. return gh->gh_error;
  785. }
  786. static void wait_on_demote(struct gfs2_glock *gl)
  787. {
  788. might_sleep();
  789. wait_on_bit(&gl->gl_flags, GLF_DEMOTE, gfs2_glock_demote_wait, TASK_UNINTERRUPTIBLE);
  790. }
  791. /**
  792. * handle_callback - process a demote request
  793. * @gl: the glock
  794. * @state: the state the caller wants us to change to
  795. *
  796. * There are only two requests that we are going to see in actual
  797. * practise: LM_ST_SHARED and LM_ST_UNLOCKED
  798. */
  799. static void handle_callback(struct gfs2_glock *gl, unsigned int state,
  800. unsigned long delay)
  801. {
  802. int bit = delay ? GLF_PENDING_DEMOTE : GLF_DEMOTE;
  803. set_bit(bit, &gl->gl_flags);
  804. if (gl->gl_demote_state == LM_ST_EXCLUSIVE) {
  805. gl->gl_demote_state = state;
  806. gl->gl_demote_time = jiffies;
  807. } else if (gl->gl_demote_state != LM_ST_UNLOCKED &&
  808. gl->gl_demote_state != state) {
  809. gl->gl_demote_state = LM_ST_UNLOCKED;
  810. }
  811. if (gl->gl_ops->go_callback)
  812. gl->gl_ops->go_callback(gl);
  813. trace_gfs2_demote_rq(gl);
  814. }
  815. void gfs2_print_dbg(struct seq_file *seq, const char *fmt, ...)
  816. {
  817. struct va_format vaf;
  818. va_list args;
  819. va_start(args, fmt);
  820. if (seq) {
  821. seq_vprintf(seq, fmt, args);
  822. } else {
  823. vaf.fmt = fmt;
  824. vaf.va = &args;
  825. printk(KERN_ERR " %pV", &vaf);
  826. }
  827. va_end(args);
  828. }
  829. /**
  830. * add_to_queue - Add a holder to the wait queue (but look for recursion)
  831. * @gh: the holder structure to add
  832. *
  833. * Eventually we should move the recursive locking trap to a
  834. * debugging option or something like that. This is the fast
  835. * path and needs to have the minimum number of distractions.
  836. *
  837. */
  838. static inline void add_to_queue(struct gfs2_holder *gh)
  839. __releases(&gl->gl_spin)
  840. __acquires(&gl->gl_spin)
  841. {
  842. struct gfs2_glock *gl = gh->gh_gl;
  843. struct gfs2_sbd *sdp = gl->gl_sbd;
  844. struct list_head *insert_pt = NULL;
  845. struct gfs2_holder *gh2;
  846. int try_lock = 0;
  847. BUG_ON(gh->gh_owner_pid == NULL);
  848. if (test_and_set_bit(HIF_WAIT, &gh->gh_iflags))
  849. BUG();
  850. if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) {
  851. if (test_bit(GLF_LOCK, &gl->gl_flags))
  852. try_lock = 1;
  853. if (test_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags))
  854. goto fail;
  855. }
  856. list_for_each_entry(gh2, &gl->gl_holders, gh_list) {
  857. if (unlikely(gh2->gh_owner_pid == gh->gh_owner_pid &&
  858. (gh->gh_gl->gl_ops->go_type != LM_TYPE_FLOCK)))
  859. goto trap_recursive;
  860. if (try_lock &&
  861. !(gh2->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) &&
  862. !may_grant(gl, gh)) {
  863. fail:
  864. gh->gh_error = GLR_TRYFAILED;
  865. gfs2_holder_wake(gh);
  866. return;
  867. }
  868. if (test_bit(HIF_HOLDER, &gh2->gh_iflags))
  869. continue;
  870. if (unlikely((gh->gh_flags & LM_FLAG_PRIORITY) && !insert_pt))
  871. insert_pt = &gh2->gh_list;
  872. }
  873. set_bit(GLF_QUEUED, &gl->gl_flags);
  874. trace_gfs2_glock_queue(gh, 1);
  875. gfs2_glstats_inc(gl, GFS2_LKS_QCOUNT);
  876. gfs2_sbstats_inc(gl, GFS2_LKS_QCOUNT);
  877. if (likely(insert_pt == NULL)) {
  878. list_add_tail(&gh->gh_list, &gl->gl_holders);
  879. if (unlikely(gh->gh_flags & LM_FLAG_PRIORITY))
  880. goto do_cancel;
  881. return;
  882. }
  883. list_add_tail(&gh->gh_list, insert_pt);
  884. do_cancel:
  885. gh = list_entry(gl->gl_holders.next, struct gfs2_holder, gh_list);
  886. if (!(gh->gh_flags & LM_FLAG_PRIORITY)) {
  887. spin_unlock(&gl->gl_spin);
  888. if (sdp->sd_lockstruct.ls_ops->lm_cancel)
  889. sdp->sd_lockstruct.ls_ops->lm_cancel(gl);
  890. spin_lock(&gl->gl_spin);
  891. }
  892. return;
  893. trap_recursive:
  894. print_symbol(KERN_ERR "original: %s\n", gh2->gh_ip);
  895. printk(KERN_ERR "pid: %d\n", pid_nr(gh2->gh_owner_pid));
  896. printk(KERN_ERR "lock type: %d req lock state : %d\n",
  897. gh2->gh_gl->gl_name.ln_type, gh2->gh_state);
  898. print_symbol(KERN_ERR "new: %s\n", gh->gh_ip);
  899. printk(KERN_ERR "pid: %d\n", pid_nr(gh->gh_owner_pid));
  900. printk(KERN_ERR "lock type: %d req lock state : %d\n",
  901. gh->gh_gl->gl_name.ln_type, gh->gh_state);
  902. __dump_glock(NULL, gl);
  903. BUG();
  904. }
  905. /**
  906. * gfs2_glock_nq - enqueue a struct gfs2_holder onto a glock (acquire a glock)
  907. * @gh: the holder structure
  908. *
  909. * if (gh->gh_flags & GL_ASYNC), this never returns an error
  910. *
  911. * Returns: 0, GLR_TRYFAILED, or errno on failure
  912. */
  913. int gfs2_glock_nq(struct gfs2_holder *gh)
  914. {
  915. struct gfs2_glock *gl = gh->gh_gl;
  916. struct gfs2_sbd *sdp = gl->gl_sbd;
  917. int error = 0;
  918. if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
  919. return -EIO;
  920. if (test_bit(GLF_LRU, &gl->gl_flags))
  921. gfs2_glock_remove_from_lru(gl);
  922. spin_lock(&gl->gl_spin);
  923. add_to_queue(gh);
  924. if ((LM_FLAG_NOEXP & gh->gh_flags) &&
  925. test_and_clear_bit(GLF_FROZEN, &gl->gl_flags))
  926. set_bit(GLF_REPLY_PENDING, &gl->gl_flags);
  927. run_queue(gl, 1);
  928. spin_unlock(&gl->gl_spin);
  929. if (!(gh->gh_flags & GL_ASYNC))
  930. error = gfs2_glock_wait(gh);
  931. return error;
  932. }
  933. /**
  934. * gfs2_glock_poll - poll to see if an async request has been completed
  935. * @gh: the holder
  936. *
  937. * Returns: 1 if the request is ready to be gfs2_glock_wait()ed on
  938. */
  939. int gfs2_glock_poll(struct gfs2_holder *gh)
  940. {
  941. return test_bit(HIF_WAIT, &gh->gh_iflags) ? 0 : 1;
  942. }
  943. /**
  944. * gfs2_glock_dq - dequeue a struct gfs2_holder from a glock (release a glock)
  945. * @gh: the glock holder
  946. *
  947. */
  948. void gfs2_glock_dq(struct gfs2_holder *gh)
  949. {
  950. struct gfs2_glock *gl = gh->gh_gl;
  951. const struct gfs2_glock_operations *glops = gl->gl_ops;
  952. unsigned delay = 0;
  953. int fast_path = 0;
  954. spin_lock(&gl->gl_spin);
  955. if (gh->gh_flags & GL_NOCACHE)
  956. handle_callback(gl, LM_ST_UNLOCKED, 0);
  957. list_del_init(&gh->gh_list);
  958. if (find_first_holder(gl) == NULL) {
  959. if (glops->go_unlock) {
  960. GLOCK_BUG_ON(gl, test_and_set_bit(GLF_LOCK, &gl->gl_flags));
  961. spin_unlock(&gl->gl_spin);
  962. glops->go_unlock(gh);
  963. spin_lock(&gl->gl_spin);
  964. clear_bit(GLF_LOCK, &gl->gl_flags);
  965. }
  966. if (list_empty(&gl->gl_holders) &&
  967. !test_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) &&
  968. !test_bit(GLF_DEMOTE, &gl->gl_flags))
  969. fast_path = 1;
  970. }
  971. if (!test_bit(GLF_LFLUSH, &gl->gl_flags) && demote_ok(gl))
  972. gfs2_glock_add_to_lru(gl);
  973. trace_gfs2_glock_queue(gh, 0);
  974. spin_unlock(&gl->gl_spin);
  975. if (likely(fast_path))
  976. return;
  977. gfs2_glock_hold(gl);
  978. if (test_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) &&
  979. !test_bit(GLF_DEMOTE, &gl->gl_flags) &&
  980. gl->gl_name.ln_type == LM_TYPE_INODE)
  981. delay = gl->gl_hold_time;
  982. if (queue_delayed_work(glock_workqueue, &gl->gl_work, delay) == 0)
  983. gfs2_glock_put(gl);
  984. }
  985. void gfs2_glock_dq_wait(struct gfs2_holder *gh)
  986. {
  987. struct gfs2_glock *gl = gh->gh_gl;
  988. gfs2_glock_dq(gh);
  989. wait_on_demote(gl);
  990. }
  991. /**
  992. * gfs2_glock_dq_uninit - dequeue a holder from a glock and initialize it
  993. * @gh: the holder structure
  994. *
  995. */
  996. void gfs2_glock_dq_uninit(struct gfs2_holder *gh)
  997. {
  998. gfs2_glock_dq(gh);
  999. gfs2_holder_uninit(gh);
  1000. }
  1001. /**
  1002. * gfs2_glock_nq_num - acquire a glock based on lock number
  1003. * @sdp: the filesystem
  1004. * @number: the lock number
  1005. * @glops: the glock operations for the type of glock
  1006. * @state: the state to acquire the glock in
  1007. * @flags: modifier flags for the acquisition
  1008. * @gh: the struct gfs2_holder
  1009. *
  1010. * Returns: errno
  1011. */
  1012. int gfs2_glock_nq_num(struct gfs2_sbd *sdp, u64 number,
  1013. const struct gfs2_glock_operations *glops,
  1014. unsigned int state, int flags, struct gfs2_holder *gh)
  1015. {
  1016. struct gfs2_glock *gl;
  1017. int error;
  1018. error = gfs2_glock_get(sdp, number, glops, CREATE, &gl);
  1019. if (!error) {
  1020. error = gfs2_glock_nq_init(gl, state, flags, gh);
  1021. gfs2_glock_put(gl);
  1022. }
  1023. return error;
  1024. }
  1025. /**
  1026. * glock_compare - Compare two struct gfs2_glock structures for sorting
  1027. * @arg_a: the first structure
  1028. * @arg_b: the second structure
  1029. *
  1030. */
  1031. static int glock_compare(const void *arg_a, const void *arg_b)
  1032. {
  1033. const struct gfs2_holder *gh_a = *(const struct gfs2_holder **)arg_a;
  1034. const struct gfs2_holder *gh_b = *(const struct gfs2_holder **)arg_b;
  1035. const struct lm_lockname *a = &gh_a->gh_gl->gl_name;
  1036. const struct lm_lockname *b = &gh_b->gh_gl->gl_name;
  1037. if (a->ln_number > b->ln_number)
  1038. return 1;
  1039. if (a->ln_number < b->ln_number)
  1040. return -1;
  1041. BUG_ON(gh_a->gh_gl->gl_ops->go_type == gh_b->gh_gl->gl_ops->go_type);
  1042. return 0;
  1043. }
  1044. /**
  1045. * nq_m_sync - synchonously acquire more than one glock in deadlock free order
  1046. * @num_gh: the number of structures
  1047. * @ghs: an array of struct gfs2_holder structures
  1048. *
  1049. * Returns: 0 on success (all glocks acquired),
  1050. * errno on failure (no glocks acquired)
  1051. */
  1052. static int nq_m_sync(unsigned int num_gh, struct gfs2_holder *ghs,
  1053. struct gfs2_holder **p)
  1054. {
  1055. unsigned int x;
  1056. int error = 0;
  1057. for (x = 0; x < num_gh; x++)
  1058. p[x] = &ghs[x];
  1059. sort(p, num_gh, sizeof(struct gfs2_holder *), glock_compare, NULL);
  1060. for (x = 0; x < num_gh; x++) {
  1061. p[x]->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
  1062. error = gfs2_glock_nq(p[x]);
  1063. if (error) {
  1064. while (x--)
  1065. gfs2_glock_dq(p[x]);
  1066. break;
  1067. }
  1068. }
  1069. return error;
  1070. }
  1071. /**
  1072. * gfs2_glock_nq_m - acquire multiple glocks
  1073. * @num_gh: the number of structures
  1074. * @ghs: an array of struct gfs2_holder structures
  1075. *
  1076. *
  1077. * Returns: 0 on success (all glocks acquired),
  1078. * errno on failure (no glocks acquired)
  1079. */
  1080. int gfs2_glock_nq_m(unsigned int num_gh, struct gfs2_holder *ghs)
  1081. {
  1082. struct gfs2_holder *tmp[4];
  1083. struct gfs2_holder **pph = tmp;
  1084. int error = 0;
  1085. switch(num_gh) {
  1086. case 0:
  1087. return 0;
  1088. case 1:
  1089. ghs->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
  1090. return gfs2_glock_nq(ghs);
  1091. default:
  1092. if (num_gh <= 4)
  1093. break;
  1094. pph = kmalloc(num_gh * sizeof(struct gfs2_holder *), GFP_NOFS);
  1095. if (!pph)
  1096. return -ENOMEM;
  1097. }
  1098. error = nq_m_sync(num_gh, ghs, pph);
  1099. if (pph != tmp)
  1100. kfree(pph);
  1101. return error;
  1102. }
  1103. /**
  1104. * gfs2_glock_dq_m - release multiple glocks
  1105. * @num_gh: the number of structures
  1106. * @ghs: an array of struct gfs2_holder structures
  1107. *
  1108. */
  1109. void gfs2_glock_dq_m(unsigned int num_gh, struct gfs2_holder *ghs)
  1110. {
  1111. while (num_gh--)
  1112. gfs2_glock_dq(&ghs[num_gh]);
  1113. }
  1114. /**
  1115. * gfs2_glock_dq_uninit_m - release multiple glocks
  1116. * @num_gh: the number of structures
  1117. * @ghs: an array of struct gfs2_holder structures
  1118. *
  1119. */
  1120. void gfs2_glock_dq_uninit_m(unsigned int num_gh, struct gfs2_holder *ghs)
  1121. {
  1122. while (num_gh--)
  1123. gfs2_glock_dq_uninit(&ghs[num_gh]);
  1124. }
  1125. void gfs2_glock_cb(struct gfs2_glock *gl, unsigned int state)
  1126. {
  1127. unsigned long delay = 0;
  1128. unsigned long holdtime;
  1129. unsigned long now = jiffies;
  1130. gfs2_glock_hold(gl);
  1131. holdtime = gl->gl_tchange + gl->gl_hold_time;
  1132. if (test_bit(GLF_QUEUED, &gl->gl_flags) &&
  1133. gl->gl_name.ln_type == LM_TYPE_INODE) {
  1134. if (time_before(now, holdtime))
  1135. delay = holdtime - now;
  1136. if (test_bit(GLF_REPLY_PENDING, &gl->gl_flags))
  1137. delay = gl->gl_hold_time;
  1138. }
  1139. spin_lock(&gl->gl_spin);
  1140. handle_callback(gl, state, delay);
  1141. spin_unlock(&gl->gl_spin);
  1142. if (queue_delayed_work(glock_workqueue, &gl->gl_work, delay) == 0)
  1143. gfs2_glock_put(gl);
  1144. }
  1145. /**
  1146. * gfs2_should_freeze - Figure out if glock should be frozen
  1147. * @gl: The glock in question
  1148. *
  1149. * Glocks are not frozen if (a) the result of the dlm operation is
  1150. * an error, (b) the locking operation was an unlock operation or
  1151. * (c) if there is a "noexp" flagged request anywhere in the queue
  1152. *
  1153. * Returns: 1 if freezing should occur, 0 otherwise
  1154. */
  1155. static int gfs2_should_freeze(const struct gfs2_glock *gl)
  1156. {
  1157. const struct gfs2_holder *gh;
  1158. if (gl->gl_reply & ~LM_OUT_ST_MASK)
  1159. return 0;
  1160. if (gl->gl_target == LM_ST_UNLOCKED)
  1161. return 0;
  1162. list_for_each_entry(gh, &gl->gl_holders, gh_list) {
  1163. if (test_bit(HIF_HOLDER, &gh->gh_iflags))
  1164. continue;
  1165. if (LM_FLAG_NOEXP & gh->gh_flags)
  1166. return 0;
  1167. }
  1168. return 1;
  1169. }
  1170. /**
  1171. * gfs2_glock_complete - Callback used by locking
  1172. * @gl: Pointer to the glock
  1173. * @ret: The return value from the dlm
  1174. *
  1175. * The gl_reply field is under the gl_spin lock so that it is ok
  1176. * to use a bitfield shared with other glock state fields.
  1177. */
  1178. void gfs2_glock_complete(struct gfs2_glock *gl, int ret)
  1179. {
  1180. struct lm_lockstruct *ls = &gl->gl_sbd->sd_lockstruct;
  1181. spin_lock(&gl->gl_spin);
  1182. gl->gl_reply = ret;
  1183. if (unlikely(test_bit(DFL_BLOCK_LOCKS, &ls->ls_recover_flags))) {
  1184. if (gfs2_should_freeze(gl)) {
  1185. set_bit(GLF_FROZEN, &gl->gl_flags);
  1186. spin_unlock(&gl->gl_spin);
  1187. return;
  1188. }
  1189. }
  1190. spin_unlock(&gl->gl_spin);
  1191. set_bit(GLF_REPLY_PENDING, &gl->gl_flags);
  1192. smp_wmb();
  1193. gfs2_glock_hold(gl);
  1194. if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
  1195. gfs2_glock_put(gl);
  1196. }
  1197. static int gfs2_shrink_glock_memory(struct shrinker *shrink,
  1198. struct shrink_control *sc)
  1199. {
  1200. struct gfs2_glock *gl;
  1201. int may_demote;
  1202. int nr_skipped = 0;
  1203. int nr = sc->nr_to_scan;
  1204. gfp_t gfp_mask = sc->gfp_mask;
  1205. LIST_HEAD(skipped);
  1206. if (nr == 0)
  1207. goto out;
  1208. if (!(gfp_mask & __GFP_FS))
  1209. return -1;
  1210. spin_lock(&lru_lock);
  1211. while(nr && !list_empty(&lru_list)) {
  1212. gl = list_entry(lru_list.next, struct gfs2_glock, gl_lru);
  1213. list_del_init(&gl->gl_lru);
  1214. clear_bit(GLF_LRU, &gl->gl_flags);
  1215. atomic_dec(&lru_count);
  1216. /* Test for being demotable */
  1217. if (!test_and_set_bit(GLF_LOCK, &gl->gl_flags)) {
  1218. gfs2_glock_hold(gl);
  1219. spin_unlock(&lru_lock);
  1220. spin_lock(&gl->gl_spin);
  1221. may_demote = demote_ok(gl);
  1222. if (may_demote) {
  1223. handle_callback(gl, LM_ST_UNLOCKED, 0);
  1224. nr--;
  1225. }
  1226. clear_bit(GLF_LOCK, &gl->gl_flags);
  1227. smp_mb__after_clear_bit();
  1228. if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
  1229. gfs2_glock_put_nolock(gl);
  1230. spin_unlock(&gl->gl_spin);
  1231. spin_lock(&lru_lock);
  1232. continue;
  1233. }
  1234. nr_skipped++;
  1235. list_add(&gl->gl_lru, &skipped);
  1236. set_bit(GLF_LRU, &gl->gl_flags);
  1237. }
  1238. list_splice(&skipped, &lru_list);
  1239. atomic_add(nr_skipped, &lru_count);
  1240. spin_unlock(&lru_lock);
  1241. out:
  1242. return (atomic_read(&lru_count) / 100) * sysctl_vfs_cache_pressure;
  1243. }
  1244. static struct shrinker glock_shrinker = {
  1245. .shrink = gfs2_shrink_glock_memory,
  1246. .seeks = DEFAULT_SEEKS,
  1247. };
  1248. /**
  1249. * examine_bucket - Call a function for glock in a hash bucket
  1250. * @examiner: the function
  1251. * @sdp: the filesystem
  1252. * @bucket: the bucket
  1253. *
  1254. */
  1255. static void examine_bucket(glock_examiner examiner, const struct gfs2_sbd *sdp,
  1256. unsigned int hash)
  1257. {
  1258. struct gfs2_glock *gl;
  1259. struct hlist_bl_head *head = &gl_hash_table[hash];
  1260. struct hlist_bl_node *pos;
  1261. rcu_read_lock();
  1262. hlist_bl_for_each_entry_rcu(gl, pos, head, gl_list) {
  1263. if ((gl->gl_sbd == sdp) && atomic_read(&gl->gl_ref))
  1264. examiner(gl);
  1265. }
  1266. rcu_read_unlock();
  1267. cond_resched();
  1268. }
  1269. static void glock_hash_walk(glock_examiner examiner, const struct gfs2_sbd *sdp)
  1270. {
  1271. unsigned x;
  1272. for (x = 0; x < GFS2_GL_HASH_SIZE; x++)
  1273. examine_bucket(examiner, sdp, x);
  1274. }
  1275. /**
  1276. * thaw_glock - thaw out a glock which has an unprocessed reply waiting
  1277. * @gl: The glock to thaw
  1278. *
  1279. * N.B. When we freeze a glock, we leave a ref to the glock outstanding,
  1280. * so this has to result in the ref count being dropped by one.
  1281. */
  1282. static void thaw_glock(struct gfs2_glock *gl)
  1283. {
  1284. if (!test_and_clear_bit(GLF_FROZEN, &gl->gl_flags))
  1285. return;
  1286. set_bit(GLF_REPLY_PENDING, &gl->gl_flags);
  1287. gfs2_glock_hold(gl);
  1288. if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
  1289. gfs2_glock_put(gl);
  1290. }
  1291. /**
  1292. * clear_glock - look at a glock and see if we can free it from glock cache
  1293. * @gl: the glock to look at
  1294. *
  1295. */
  1296. static void clear_glock(struct gfs2_glock *gl)
  1297. {
  1298. gfs2_glock_remove_from_lru(gl);
  1299. spin_lock(&gl->gl_spin);
  1300. if (gl->gl_state != LM_ST_UNLOCKED)
  1301. handle_callback(gl, LM_ST_UNLOCKED, 0);
  1302. spin_unlock(&gl->gl_spin);
  1303. gfs2_glock_hold(gl);
  1304. if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
  1305. gfs2_glock_put(gl);
  1306. }
  1307. /**
  1308. * gfs2_glock_thaw - Thaw any frozen glocks
  1309. * @sdp: The super block
  1310. *
  1311. */
  1312. void gfs2_glock_thaw(struct gfs2_sbd *sdp)
  1313. {
  1314. glock_hash_walk(thaw_glock, sdp);
  1315. }
  1316. static int dump_glock(struct seq_file *seq, struct gfs2_glock *gl)
  1317. {
  1318. int ret;
  1319. spin_lock(&gl->gl_spin);
  1320. ret = __dump_glock(seq, gl);
  1321. spin_unlock(&gl->gl_spin);
  1322. return ret;
  1323. }
  1324. static void dump_glock_func(struct gfs2_glock *gl)
  1325. {
  1326. dump_glock(NULL, gl);
  1327. }
  1328. /**
  1329. * gfs2_gl_hash_clear - Empty out the glock hash table
  1330. * @sdp: the filesystem
  1331. * @wait: wait until it's all gone
  1332. *
  1333. * Called when unmounting the filesystem.
  1334. */
  1335. void gfs2_gl_hash_clear(struct gfs2_sbd *sdp)
  1336. {
  1337. glock_hash_walk(clear_glock, sdp);
  1338. flush_workqueue(glock_workqueue);
  1339. wait_event(sdp->sd_glock_wait, atomic_read(&sdp->sd_glock_disposal) == 0);
  1340. glock_hash_walk(dump_glock_func, sdp);
  1341. }
  1342. void gfs2_glock_finish_truncate(struct gfs2_inode *ip)
  1343. {
  1344. struct gfs2_glock *gl = ip->i_gl;
  1345. int ret;
  1346. ret = gfs2_truncatei_resume(ip);
  1347. gfs2_assert_withdraw(gl->gl_sbd, ret == 0);
  1348. spin_lock(&gl->gl_spin);
  1349. clear_bit(GLF_LOCK, &gl->gl_flags);
  1350. run_queue(gl, 1);
  1351. spin_unlock(&gl->gl_spin);
  1352. }
  1353. static const char *state2str(unsigned state)
  1354. {
  1355. switch(state) {
  1356. case LM_ST_UNLOCKED:
  1357. return "UN";
  1358. case LM_ST_SHARED:
  1359. return "SH";
  1360. case LM_ST_DEFERRED:
  1361. return "DF";
  1362. case LM_ST_EXCLUSIVE:
  1363. return "EX";
  1364. }
  1365. return "??";
  1366. }
  1367. static const char *hflags2str(char *buf, unsigned flags, unsigned long iflags)
  1368. {
  1369. char *p = buf;
  1370. if (flags & LM_FLAG_TRY)
  1371. *p++ = 't';
  1372. if (flags & LM_FLAG_TRY_1CB)
  1373. *p++ = 'T';
  1374. if (flags & LM_FLAG_NOEXP)
  1375. *p++ = 'e';
  1376. if (flags & LM_FLAG_ANY)
  1377. *p++ = 'A';
  1378. if (flags & LM_FLAG_PRIORITY)
  1379. *p++ = 'p';
  1380. if (flags & GL_ASYNC)
  1381. *p++ = 'a';
  1382. if (flags & GL_EXACT)
  1383. *p++ = 'E';
  1384. if (flags & GL_NOCACHE)
  1385. *p++ = 'c';
  1386. if (test_bit(HIF_HOLDER, &iflags))
  1387. *p++ = 'H';
  1388. if (test_bit(HIF_WAIT, &iflags))
  1389. *p++ = 'W';
  1390. if (test_bit(HIF_FIRST, &iflags))
  1391. *p++ = 'F';
  1392. *p = 0;
  1393. return buf;
  1394. }
  1395. /**
  1396. * dump_holder - print information about a glock holder
  1397. * @seq: the seq_file struct
  1398. * @gh: the glock holder
  1399. *
  1400. * Returns: 0 on success, -ENOBUFS when we run out of space
  1401. */
  1402. static int dump_holder(struct seq_file *seq, const struct gfs2_holder *gh)
  1403. {
  1404. struct task_struct *gh_owner = NULL;
  1405. char flags_buf[32];
  1406. if (gh->gh_owner_pid)
  1407. gh_owner = pid_task(gh->gh_owner_pid, PIDTYPE_PID);
  1408. gfs2_print_dbg(seq, " H: s:%s f:%s e:%d p:%ld [%s] %pS\n",
  1409. state2str(gh->gh_state),
  1410. hflags2str(flags_buf, gh->gh_flags, gh->gh_iflags),
  1411. gh->gh_error,
  1412. gh->gh_owner_pid ? (long)pid_nr(gh->gh_owner_pid) : -1,
  1413. gh_owner ? gh_owner->comm : "(ended)",
  1414. (void *)gh->gh_ip);
  1415. return 0;
  1416. }
  1417. static const char *gflags2str(char *buf, const struct gfs2_glock *gl)
  1418. {
  1419. const unsigned long *gflags = &gl->gl_flags;
  1420. char *p = buf;
  1421. if (test_bit(GLF_LOCK, gflags))
  1422. *p++ = 'l';
  1423. if (test_bit(GLF_DEMOTE, gflags))
  1424. *p++ = 'D';
  1425. if (test_bit(GLF_PENDING_DEMOTE, gflags))
  1426. *p++ = 'd';
  1427. if (test_bit(GLF_DEMOTE_IN_PROGRESS, gflags))
  1428. *p++ = 'p';
  1429. if (test_bit(GLF_DIRTY, gflags))
  1430. *p++ = 'y';
  1431. if (test_bit(GLF_LFLUSH, gflags))
  1432. *p++ = 'f';
  1433. if (test_bit(GLF_INVALIDATE_IN_PROGRESS, gflags))
  1434. *p++ = 'i';
  1435. if (test_bit(GLF_REPLY_PENDING, gflags))
  1436. *p++ = 'r';
  1437. if (test_bit(GLF_INITIAL, gflags))
  1438. *p++ = 'I';
  1439. if (test_bit(GLF_FROZEN, gflags))
  1440. *p++ = 'F';
  1441. if (test_bit(GLF_QUEUED, gflags))
  1442. *p++ = 'q';
  1443. if (test_bit(GLF_LRU, gflags))
  1444. *p++ = 'L';
  1445. if (gl->gl_object)
  1446. *p++ = 'o';
  1447. if (test_bit(GLF_BLOCKING, gflags))
  1448. *p++ = 'b';
  1449. *p = 0;
  1450. return buf;
  1451. }
  1452. /**
  1453. * __dump_glock - print information about a glock
  1454. * @seq: The seq_file struct
  1455. * @gl: the glock
  1456. *
  1457. * The file format is as follows:
  1458. * One line per object, capital letters are used to indicate objects
  1459. * G = glock, I = Inode, R = rgrp, H = holder. Glocks are not indented,
  1460. * other objects are indented by a single space and follow the glock to
  1461. * which they are related. Fields are indicated by lower case letters
  1462. * followed by a colon and the field value, except for strings which are in
  1463. * [] so that its possible to see if they are composed of spaces for
  1464. * example. The field's are n = number (id of the object), f = flags,
  1465. * t = type, s = state, r = refcount, e = error, p = pid.
  1466. *
  1467. * Returns: 0 on success, -ENOBUFS when we run out of space
  1468. */
  1469. static int __dump_glock(struct seq_file *seq, const struct gfs2_glock *gl)
  1470. {
  1471. const struct gfs2_glock_operations *glops = gl->gl_ops;
  1472. unsigned long long dtime;
  1473. const struct gfs2_holder *gh;
  1474. char gflags_buf[32];
  1475. int error = 0;
  1476. dtime = jiffies - gl->gl_demote_time;
  1477. dtime *= 1000000/HZ; /* demote time in uSec */
  1478. if (!test_bit(GLF_DEMOTE, &gl->gl_flags))
  1479. dtime = 0;
  1480. gfs2_print_dbg(seq, "G: s:%s n:%u/%llx f:%s t:%s d:%s/%llu a:%d v:%d r:%d m:%ld\n",
  1481. state2str(gl->gl_state),
  1482. gl->gl_name.ln_type,
  1483. (unsigned long long)gl->gl_name.ln_number,
  1484. gflags2str(gflags_buf, gl),
  1485. state2str(gl->gl_target),
  1486. state2str(gl->gl_demote_state), dtime,
  1487. atomic_read(&gl->gl_ail_count),
  1488. atomic_read(&gl->gl_revokes),
  1489. atomic_read(&gl->gl_ref), gl->gl_hold_time);
  1490. list_for_each_entry(gh, &gl->gl_holders, gh_list) {
  1491. error = dump_holder(seq, gh);
  1492. if (error)
  1493. goto out;
  1494. }
  1495. if (gl->gl_state != LM_ST_UNLOCKED && glops->go_dump)
  1496. error = glops->go_dump(seq, gl);
  1497. out:
  1498. return error;
  1499. }
  1500. static int gfs2_glstats_seq_show(struct seq_file *seq, void *iter_ptr)
  1501. {
  1502. struct gfs2_glock *gl = iter_ptr;
  1503. seq_printf(seq, "G: n:%u/%llx rtt:%lld/%lld rttb:%lld/%lld irt:%lld/%lld dcnt: %lld qcnt: %lld\n",
  1504. gl->gl_name.ln_type,
  1505. (unsigned long long)gl->gl_name.ln_number,
  1506. (long long)gl->gl_stats.stats[GFS2_LKS_SRTT],
  1507. (long long)gl->gl_stats.stats[GFS2_LKS_SRTTVAR],
  1508. (long long)gl->gl_stats.stats[GFS2_LKS_SRTTB],
  1509. (long long)gl->gl_stats.stats[GFS2_LKS_SRTTVARB],
  1510. (long long)gl->gl_stats.stats[GFS2_LKS_SIRT],
  1511. (long long)gl->gl_stats.stats[GFS2_LKS_SIRTVAR],
  1512. (long long)gl->gl_stats.stats[GFS2_LKS_DCOUNT],
  1513. (long long)gl->gl_stats.stats[GFS2_LKS_QCOUNT]);
  1514. return 0;
  1515. }
  1516. static const char *gfs2_gltype[] = {
  1517. "type",
  1518. "reserved",
  1519. "nondisk",
  1520. "inode",
  1521. "rgrp",
  1522. "meta",
  1523. "iopen",
  1524. "flock",
  1525. "plock",
  1526. "quota",
  1527. "journal",
  1528. };
  1529. static const char *gfs2_stype[] = {
  1530. [GFS2_LKS_SRTT] = "srtt",
  1531. [GFS2_LKS_SRTTVAR] = "srttvar",
  1532. [GFS2_LKS_SRTTB] = "srttb",
  1533. [GFS2_LKS_SRTTVARB] = "srttvarb",
  1534. [GFS2_LKS_SIRT] = "sirt",
  1535. [GFS2_LKS_SIRTVAR] = "sirtvar",
  1536. [GFS2_LKS_DCOUNT] = "dlm",
  1537. [GFS2_LKS_QCOUNT] = "queue",
  1538. };
  1539. #define GFS2_NR_SBSTATS (ARRAY_SIZE(gfs2_gltype) * ARRAY_SIZE(gfs2_stype))
  1540. static int gfs2_sbstats_seq_show(struct seq_file *seq, void *iter_ptr)
  1541. {
  1542. struct gfs2_glock_iter *gi = seq->private;
  1543. struct gfs2_sbd *sdp = gi->sdp;
  1544. unsigned index = gi->hash >> 3;
  1545. unsigned subindex = gi->hash & 0x07;
  1546. s64 value;
  1547. int i;
  1548. if (index == 0 && subindex != 0)
  1549. return 0;
  1550. seq_printf(seq, "%-10s %8s:", gfs2_gltype[index],
  1551. (index == 0) ? "cpu": gfs2_stype[subindex]);
  1552. for_each_possible_cpu(i) {
  1553. const struct gfs2_pcpu_lkstats *lkstats = per_cpu_ptr(sdp->sd_lkstats, i);
  1554. if (index == 0) {
  1555. value = i;
  1556. } else {
  1557. value = lkstats->lkstats[index - 1].stats[subindex];
  1558. }
  1559. seq_printf(seq, " %15lld", (long long)value);
  1560. }
  1561. seq_putc(seq, '\n');
  1562. return 0;
  1563. }
  1564. int __init gfs2_glock_init(void)
  1565. {
  1566. unsigned i;
  1567. for(i = 0; i < GFS2_GL_HASH_SIZE; i++) {
  1568. INIT_HLIST_BL_HEAD(&gl_hash_table[i]);
  1569. }
  1570. glock_workqueue = alloc_workqueue("glock_workqueue", WQ_MEM_RECLAIM |
  1571. WQ_HIGHPRI | WQ_FREEZABLE, 0);
  1572. if (IS_ERR(glock_workqueue))
  1573. return PTR_ERR(glock_workqueue);
  1574. gfs2_delete_workqueue = alloc_workqueue("delete_workqueue",
  1575. WQ_MEM_RECLAIM | WQ_FREEZABLE,
  1576. 0);
  1577. if (IS_ERR(gfs2_delete_workqueue)) {
  1578. destroy_workqueue(glock_workqueue);
  1579. return PTR_ERR(gfs2_delete_workqueue);
  1580. }
  1581. register_shrinker(&glock_shrinker);
  1582. return 0;
  1583. }
  1584. void gfs2_glock_exit(void)
  1585. {
  1586. unregister_shrinker(&glock_shrinker);
  1587. destroy_workqueue(glock_workqueue);
  1588. destroy_workqueue(gfs2_delete_workqueue);
  1589. }
  1590. static inline struct gfs2_glock *glock_hash_chain(unsigned hash)
  1591. {
  1592. return hlist_bl_entry(hlist_bl_first_rcu(&gl_hash_table[hash]),
  1593. struct gfs2_glock, gl_list);
  1594. }
  1595. static inline struct gfs2_glock *glock_hash_next(struct gfs2_glock *gl)
  1596. {
  1597. return hlist_bl_entry(rcu_dereference(gl->gl_list.next),
  1598. struct gfs2_glock, gl_list);
  1599. }
  1600. static int gfs2_glock_iter_next(struct gfs2_glock_iter *gi)
  1601. {
  1602. struct gfs2_glock *gl;
  1603. do {
  1604. gl = gi->gl;
  1605. if (gl) {
  1606. gi->gl = glock_hash_next(gl);
  1607. gi->nhash++;
  1608. } else {
  1609. if (gi->hash >= GFS2_GL_HASH_SIZE) {
  1610. rcu_read_unlock();
  1611. return 1;
  1612. }
  1613. gi->gl = glock_hash_chain(gi->hash);
  1614. gi->nhash = 0;
  1615. }
  1616. while (gi->gl == NULL) {
  1617. gi->hash++;
  1618. if (gi->hash >= GFS2_GL_HASH_SIZE) {
  1619. rcu_read_unlock();
  1620. return 1;
  1621. }
  1622. gi->gl = glock_hash_chain(gi->hash);
  1623. gi->nhash = 0;
  1624. }
  1625. /* Skip entries for other sb and dead entries */
  1626. } while (gi->sdp != gi->gl->gl_sbd || atomic_read(&gi->gl->gl_ref) == 0);
  1627. return 0;
  1628. }
  1629. static void *gfs2_glock_seq_start(struct seq_file *seq, loff_t *pos)
  1630. {
  1631. struct gfs2_glock_iter *gi = seq->private;
  1632. loff_t n = *pos;
  1633. if (gi->last_pos <= *pos)
  1634. n = gi->nhash + (*pos - gi->last_pos);
  1635. else
  1636. gi->hash = 0;
  1637. gi->nhash = 0;
  1638. rcu_read_lock();
  1639. do {
  1640. if (gfs2_glock_iter_next(gi))
  1641. return NULL;
  1642. } while (n--);
  1643. gi->last_pos = *pos;
  1644. return gi->gl;
  1645. }
  1646. static void *gfs2_glock_seq_next(struct seq_file *seq, void *iter_ptr,
  1647. loff_t *pos)
  1648. {
  1649. struct gfs2_glock_iter *gi = seq->private;
  1650. (*pos)++;
  1651. gi->last_pos = *pos;
  1652. if (gfs2_glock_iter_next(gi))
  1653. return NULL;
  1654. return gi->gl;
  1655. }
  1656. static void gfs2_glock_seq_stop(struct seq_file *seq, void *iter_ptr)
  1657. {
  1658. struct gfs2_glock_iter *gi = seq->private;
  1659. if (gi->gl)
  1660. rcu_read_unlock();
  1661. gi->gl = NULL;
  1662. }
  1663. static int gfs2_glock_seq_show(struct seq_file *seq, void *iter_ptr)
  1664. {
  1665. return dump_glock(seq, iter_ptr);
  1666. }
  1667. static void *gfs2_sbstats_seq_start(struct seq_file *seq, loff_t *pos)
  1668. {
  1669. struct gfs2_glock_iter *gi = seq->private;
  1670. gi->hash = *pos;
  1671. if (*pos >= GFS2_NR_SBSTATS)
  1672. return NULL;
  1673. preempt_disable();
  1674. return SEQ_START_TOKEN;
  1675. }
  1676. static void *gfs2_sbstats_seq_next(struct seq_file *seq, void *iter_ptr,
  1677. loff_t *pos)
  1678. {
  1679. struct gfs2_glock_iter *gi = seq->private;
  1680. (*pos)++;
  1681. gi->hash++;
  1682. if (gi->hash >= GFS2_NR_SBSTATS) {
  1683. preempt_enable();
  1684. return NULL;
  1685. }
  1686. return SEQ_START_TOKEN;
  1687. }
  1688. static void gfs2_sbstats_seq_stop(struct seq_file *seq, void *iter_ptr)
  1689. {
  1690. preempt_enable();
  1691. }
  1692. static const struct seq_operations gfs2_glock_seq_ops = {
  1693. .start = gfs2_glock_seq_start,
  1694. .next = gfs2_glock_seq_next,
  1695. .stop = gfs2_glock_seq_stop,
  1696. .show = gfs2_glock_seq_show,
  1697. };
  1698. static const struct seq_operations gfs2_glstats_seq_ops = {
  1699. .start = gfs2_glock_seq_start,
  1700. .next = gfs2_glock_seq_next,
  1701. .stop = gfs2_glock_seq_stop,
  1702. .show = gfs2_glstats_seq_show,
  1703. };
  1704. static const struct seq_operations gfs2_sbstats_seq_ops = {
  1705. .start = gfs2_sbstats_seq_start,
  1706. .next = gfs2_sbstats_seq_next,
  1707. .stop = gfs2_sbstats_seq_stop,
  1708. .show = gfs2_sbstats_seq_show,
  1709. };
  1710. #define GFS2_SEQ_GOODSIZE min(PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER, 65536UL)
  1711. static int gfs2_glocks_open(struct inode *inode, struct file *file)
  1712. {
  1713. int ret = seq_open_private(file, &gfs2_glock_seq_ops,
  1714. sizeof(struct gfs2_glock_iter));
  1715. if (ret == 0) {
  1716. struct seq_file *seq = file->private_data;
  1717. struct gfs2_glock_iter *gi = seq->private;
  1718. gi->sdp = inode->i_private;
  1719. seq->buf = kmalloc(GFS2_SEQ_GOODSIZE, GFP_KERNEL | __GFP_NOWARN);
  1720. if (seq->buf)
  1721. seq->size = GFS2_SEQ_GOODSIZE;
  1722. }
  1723. return ret;
  1724. }
  1725. static int gfs2_glstats_open(struct inode *inode, struct file *file)
  1726. {
  1727. int ret = seq_open_private(file, &gfs2_glstats_seq_ops,
  1728. sizeof(struct gfs2_glock_iter));
  1729. if (ret == 0) {
  1730. struct seq_file *seq = file->private_data;
  1731. struct gfs2_glock_iter *gi = seq->private;
  1732. gi->sdp = inode->i_private;
  1733. seq->buf = kmalloc(GFS2_SEQ_GOODSIZE, GFP_KERNEL | __GFP_NOWARN);
  1734. if (seq->buf)
  1735. seq->size = GFS2_SEQ_GOODSIZE;
  1736. }
  1737. return ret;
  1738. }
  1739. static int gfs2_sbstats_open(struct inode *inode, struct file *file)
  1740. {
  1741. int ret = seq_open_private(file, &gfs2_sbstats_seq_ops,
  1742. sizeof(struct gfs2_glock_iter));
  1743. if (ret == 0) {
  1744. struct seq_file *seq = file->private_data;
  1745. struct gfs2_glock_iter *gi = seq->private;
  1746. gi->sdp = inode->i_private;
  1747. }
  1748. return ret;
  1749. }
  1750. static const struct file_operations gfs2_glocks_fops = {
  1751. .owner = THIS_MODULE,
  1752. .open = gfs2_glocks_open,
  1753. .read = seq_read,
  1754. .llseek = seq_lseek,
  1755. .release = seq_release_private,
  1756. };
  1757. static const struct file_operations gfs2_glstats_fops = {
  1758. .owner = THIS_MODULE,
  1759. .open = gfs2_glstats_open,
  1760. .read = seq_read,
  1761. .llseek = seq_lseek,
  1762. .release = seq_release_private,
  1763. };
  1764. static const struct file_operations gfs2_sbstats_fops = {
  1765. .owner = THIS_MODULE,
  1766. .open = gfs2_sbstats_open,
  1767. .read = seq_read,
  1768. .llseek = seq_lseek,
  1769. .release = seq_release_private,
  1770. };
  1771. int gfs2_create_debugfs_file(struct gfs2_sbd *sdp)
  1772. {
  1773. sdp->debugfs_dir = debugfs_create_dir(sdp->sd_table_name, gfs2_root);
  1774. if (!sdp->debugfs_dir)
  1775. return -ENOMEM;
  1776. sdp->debugfs_dentry_glocks = debugfs_create_file("glocks",
  1777. S_IFREG | S_IRUGO,
  1778. sdp->debugfs_dir, sdp,
  1779. &gfs2_glocks_fops);
  1780. if (!sdp->debugfs_dentry_glocks)
  1781. goto fail;
  1782. sdp->debugfs_dentry_glstats = debugfs_create_file("glstats",
  1783. S_IFREG | S_IRUGO,
  1784. sdp->debugfs_dir, sdp,
  1785. &gfs2_glstats_fops);
  1786. if (!sdp->debugfs_dentry_glstats)
  1787. goto fail;
  1788. sdp->debugfs_dentry_sbstats = debugfs_create_file("sbstats",
  1789. S_IFREG | S_IRUGO,
  1790. sdp->debugfs_dir, sdp,
  1791. &gfs2_sbstats_fops);
  1792. if (!sdp->debugfs_dentry_sbstats)
  1793. goto fail;
  1794. return 0;
  1795. fail:
  1796. gfs2_delete_debugfs_file(sdp);
  1797. return -ENOMEM;
  1798. }
  1799. void gfs2_delete_debugfs_file(struct gfs2_sbd *sdp)
  1800. {
  1801. if (sdp->debugfs_dir) {
  1802. if (sdp->debugfs_dentry_glocks) {
  1803. debugfs_remove(sdp->debugfs_dentry_glocks);
  1804. sdp->debugfs_dentry_glocks = NULL;
  1805. }
  1806. if (sdp->debugfs_dentry_glstats) {
  1807. debugfs_remove(sdp->debugfs_dentry_glstats);
  1808. sdp->debugfs_dentry_glstats = NULL;
  1809. }
  1810. if (sdp->debugfs_dentry_sbstats) {
  1811. debugfs_remove(sdp->debugfs_dentry_sbstats);
  1812. sdp->debugfs_dentry_sbstats = NULL;
  1813. }
  1814. debugfs_remove(sdp->debugfs_dir);
  1815. sdp->debugfs_dir = NULL;
  1816. }
  1817. }
  1818. int gfs2_register_debugfs(void)
  1819. {
  1820. gfs2_root = debugfs_create_dir("gfs2", NULL);
  1821. return gfs2_root ? 0 : -ENOMEM;
  1822. }
  1823. void gfs2_unregister_debugfs(void)
  1824. {
  1825. debugfs_remove(gfs2_root);
  1826. gfs2_root = NULL;
  1827. }