glock.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086
  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. /**
  787. * handle_callback - process a demote request
  788. * @gl: the glock
  789. * @state: the state the caller wants us to change to
  790. *
  791. * There are only two requests that we are going to see in actual
  792. * practise: LM_ST_SHARED and LM_ST_UNLOCKED
  793. */
  794. static void handle_callback(struct gfs2_glock *gl, unsigned int state,
  795. unsigned long delay)
  796. {
  797. int bit = delay ? GLF_PENDING_DEMOTE : GLF_DEMOTE;
  798. set_bit(bit, &gl->gl_flags);
  799. if (gl->gl_demote_state == LM_ST_EXCLUSIVE) {
  800. gl->gl_demote_state = state;
  801. gl->gl_demote_time = jiffies;
  802. } else if (gl->gl_demote_state != LM_ST_UNLOCKED &&
  803. gl->gl_demote_state != state) {
  804. gl->gl_demote_state = LM_ST_UNLOCKED;
  805. }
  806. if (gl->gl_ops->go_callback)
  807. gl->gl_ops->go_callback(gl);
  808. trace_gfs2_demote_rq(gl);
  809. }
  810. void gfs2_print_dbg(struct seq_file *seq, const char *fmt, ...)
  811. {
  812. struct va_format vaf;
  813. va_list args;
  814. va_start(args, fmt);
  815. if (seq) {
  816. seq_vprintf(seq, fmt, args);
  817. } else {
  818. vaf.fmt = fmt;
  819. vaf.va = &args;
  820. printk(KERN_ERR " %pV", &vaf);
  821. }
  822. va_end(args);
  823. }
  824. /**
  825. * add_to_queue - Add a holder to the wait queue (but look for recursion)
  826. * @gh: the holder structure to add
  827. *
  828. * Eventually we should move the recursive locking trap to a
  829. * debugging option or something like that. This is the fast
  830. * path and needs to have the minimum number of distractions.
  831. *
  832. */
  833. static inline void add_to_queue(struct gfs2_holder *gh)
  834. __releases(&gl->gl_spin)
  835. __acquires(&gl->gl_spin)
  836. {
  837. struct gfs2_glock *gl = gh->gh_gl;
  838. struct gfs2_sbd *sdp = gl->gl_sbd;
  839. struct list_head *insert_pt = NULL;
  840. struct gfs2_holder *gh2;
  841. int try_futile = 0;
  842. BUG_ON(gh->gh_owner_pid == NULL);
  843. if (test_and_set_bit(HIF_WAIT, &gh->gh_iflags))
  844. BUG();
  845. if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) {
  846. if (test_bit(GLF_LOCK, &gl->gl_flags))
  847. try_futile = !may_grant(gl, gh);
  848. if (test_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags))
  849. goto fail;
  850. }
  851. list_for_each_entry(gh2, &gl->gl_holders, gh_list) {
  852. if (unlikely(gh2->gh_owner_pid == gh->gh_owner_pid &&
  853. (gh->gh_gl->gl_ops->go_type != LM_TYPE_FLOCK)))
  854. goto trap_recursive;
  855. if (try_futile &&
  856. !(gh2->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB))) {
  857. fail:
  858. gh->gh_error = GLR_TRYFAILED;
  859. gfs2_holder_wake(gh);
  860. return;
  861. }
  862. if (test_bit(HIF_HOLDER, &gh2->gh_iflags))
  863. continue;
  864. if (unlikely((gh->gh_flags & LM_FLAG_PRIORITY) && !insert_pt))
  865. insert_pt = &gh2->gh_list;
  866. }
  867. set_bit(GLF_QUEUED, &gl->gl_flags);
  868. trace_gfs2_glock_queue(gh, 1);
  869. gfs2_glstats_inc(gl, GFS2_LKS_QCOUNT);
  870. gfs2_sbstats_inc(gl, GFS2_LKS_QCOUNT);
  871. if (likely(insert_pt == NULL)) {
  872. list_add_tail(&gh->gh_list, &gl->gl_holders);
  873. if (unlikely(gh->gh_flags & LM_FLAG_PRIORITY))
  874. goto do_cancel;
  875. return;
  876. }
  877. list_add_tail(&gh->gh_list, insert_pt);
  878. do_cancel:
  879. gh = list_entry(gl->gl_holders.next, struct gfs2_holder, gh_list);
  880. if (!(gh->gh_flags & LM_FLAG_PRIORITY)) {
  881. spin_unlock(&gl->gl_spin);
  882. if (sdp->sd_lockstruct.ls_ops->lm_cancel)
  883. sdp->sd_lockstruct.ls_ops->lm_cancel(gl);
  884. spin_lock(&gl->gl_spin);
  885. }
  886. return;
  887. trap_recursive:
  888. print_symbol(KERN_ERR "original: %s\n", gh2->gh_ip);
  889. printk(KERN_ERR "pid: %d\n", pid_nr(gh2->gh_owner_pid));
  890. printk(KERN_ERR "lock type: %d req lock state : %d\n",
  891. gh2->gh_gl->gl_name.ln_type, gh2->gh_state);
  892. print_symbol(KERN_ERR "new: %s\n", gh->gh_ip);
  893. printk(KERN_ERR "pid: %d\n", pid_nr(gh->gh_owner_pid));
  894. printk(KERN_ERR "lock type: %d req lock state : %d\n",
  895. gh->gh_gl->gl_name.ln_type, gh->gh_state);
  896. __dump_glock(NULL, gl);
  897. BUG();
  898. }
  899. /**
  900. * gfs2_glock_nq - enqueue a struct gfs2_holder onto a glock (acquire a glock)
  901. * @gh: the holder structure
  902. *
  903. * if (gh->gh_flags & GL_ASYNC), this never returns an error
  904. *
  905. * Returns: 0, GLR_TRYFAILED, or errno on failure
  906. */
  907. int gfs2_glock_nq(struct gfs2_holder *gh)
  908. {
  909. struct gfs2_glock *gl = gh->gh_gl;
  910. struct gfs2_sbd *sdp = gl->gl_sbd;
  911. int error = 0;
  912. if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
  913. return -EIO;
  914. if (test_bit(GLF_LRU, &gl->gl_flags))
  915. gfs2_glock_remove_from_lru(gl);
  916. spin_lock(&gl->gl_spin);
  917. add_to_queue(gh);
  918. if ((LM_FLAG_NOEXP & gh->gh_flags) &&
  919. test_and_clear_bit(GLF_FROZEN, &gl->gl_flags))
  920. set_bit(GLF_REPLY_PENDING, &gl->gl_flags);
  921. run_queue(gl, 1);
  922. spin_unlock(&gl->gl_spin);
  923. if (!(gh->gh_flags & GL_ASYNC))
  924. error = gfs2_glock_wait(gh);
  925. return error;
  926. }
  927. /**
  928. * gfs2_glock_poll - poll to see if an async request has been completed
  929. * @gh: the holder
  930. *
  931. * Returns: 1 if the request is ready to be gfs2_glock_wait()ed on
  932. */
  933. int gfs2_glock_poll(struct gfs2_holder *gh)
  934. {
  935. return test_bit(HIF_WAIT, &gh->gh_iflags) ? 0 : 1;
  936. }
  937. /**
  938. * gfs2_glock_dq - dequeue a struct gfs2_holder from a glock (release a glock)
  939. * @gh: the glock holder
  940. *
  941. */
  942. void gfs2_glock_dq(struct gfs2_holder *gh)
  943. {
  944. struct gfs2_glock *gl = gh->gh_gl;
  945. const struct gfs2_glock_operations *glops = gl->gl_ops;
  946. unsigned delay = 0;
  947. int fast_path = 0;
  948. spin_lock(&gl->gl_spin);
  949. if (gh->gh_flags & GL_NOCACHE)
  950. handle_callback(gl, LM_ST_UNLOCKED, 0);
  951. list_del_init(&gh->gh_list);
  952. if (find_first_holder(gl) == NULL) {
  953. if (glops->go_unlock) {
  954. GLOCK_BUG_ON(gl, test_and_set_bit(GLF_LOCK, &gl->gl_flags));
  955. spin_unlock(&gl->gl_spin);
  956. glops->go_unlock(gh);
  957. spin_lock(&gl->gl_spin);
  958. clear_bit(GLF_LOCK, &gl->gl_flags);
  959. }
  960. if (list_empty(&gl->gl_holders) &&
  961. !test_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) &&
  962. !test_bit(GLF_DEMOTE, &gl->gl_flags))
  963. fast_path = 1;
  964. }
  965. if (!test_bit(GLF_LFLUSH, &gl->gl_flags) && demote_ok(gl))
  966. gfs2_glock_add_to_lru(gl);
  967. trace_gfs2_glock_queue(gh, 0);
  968. spin_unlock(&gl->gl_spin);
  969. if (likely(fast_path))
  970. return;
  971. gfs2_glock_hold(gl);
  972. if (test_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) &&
  973. !test_bit(GLF_DEMOTE, &gl->gl_flags) &&
  974. gl->gl_name.ln_type == LM_TYPE_INODE)
  975. delay = gl->gl_hold_time;
  976. if (queue_delayed_work(glock_workqueue, &gl->gl_work, delay) == 0)
  977. gfs2_glock_put(gl);
  978. }
  979. void gfs2_glock_dq_wait(struct gfs2_holder *gh)
  980. {
  981. struct gfs2_glock *gl = gh->gh_gl;
  982. gfs2_glock_dq(gh);
  983. might_sleep();
  984. wait_on_bit(&gl->gl_flags, GLF_DEMOTE, gfs2_glock_demote_wait, TASK_UNINTERRUPTIBLE);
  985. }
  986. /**
  987. * gfs2_glock_dq_uninit - dequeue a holder from a glock and initialize it
  988. * @gh: the holder structure
  989. *
  990. */
  991. void gfs2_glock_dq_uninit(struct gfs2_holder *gh)
  992. {
  993. gfs2_glock_dq(gh);
  994. gfs2_holder_uninit(gh);
  995. }
  996. /**
  997. * gfs2_glock_nq_num - acquire a glock based on lock number
  998. * @sdp: the filesystem
  999. * @number: the lock number
  1000. * @glops: the glock operations for the type of glock
  1001. * @state: the state to acquire the glock in
  1002. * @flags: modifier flags for the acquisition
  1003. * @gh: the struct gfs2_holder
  1004. *
  1005. * Returns: errno
  1006. */
  1007. int gfs2_glock_nq_num(struct gfs2_sbd *sdp, u64 number,
  1008. const struct gfs2_glock_operations *glops,
  1009. unsigned int state, int flags, struct gfs2_holder *gh)
  1010. {
  1011. struct gfs2_glock *gl;
  1012. int error;
  1013. error = gfs2_glock_get(sdp, number, glops, CREATE, &gl);
  1014. if (!error) {
  1015. error = gfs2_glock_nq_init(gl, state, flags, gh);
  1016. gfs2_glock_put(gl);
  1017. }
  1018. return error;
  1019. }
  1020. /**
  1021. * glock_compare - Compare two struct gfs2_glock structures for sorting
  1022. * @arg_a: the first structure
  1023. * @arg_b: the second structure
  1024. *
  1025. */
  1026. static int glock_compare(const void *arg_a, const void *arg_b)
  1027. {
  1028. const struct gfs2_holder *gh_a = *(const struct gfs2_holder **)arg_a;
  1029. const struct gfs2_holder *gh_b = *(const struct gfs2_holder **)arg_b;
  1030. const struct lm_lockname *a = &gh_a->gh_gl->gl_name;
  1031. const struct lm_lockname *b = &gh_b->gh_gl->gl_name;
  1032. if (a->ln_number > b->ln_number)
  1033. return 1;
  1034. if (a->ln_number < b->ln_number)
  1035. return -1;
  1036. BUG_ON(gh_a->gh_gl->gl_ops->go_type == gh_b->gh_gl->gl_ops->go_type);
  1037. return 0;
  1038. }
  1039. /**
  1040. * nq_m_sync - synchonously acquire more than one glock in deadlock free order
  1041. * @num_gh: the number of structures
  1042. * @ghs: an array of struct gfs2_holder structures
  1043. *
  1044. * Returns: 0 on success (all glocks acquired),
  1045. * errno on failure (no glocks acquired)
  1046. */
  1047. static int nq_m_sync(unsigned int num_gh, struct gfs2_holder *ghs,
  1048. struct gfs2_holder **p)
  1049. {
  1050. unsigned int x;
  1051. int error = 0;
  1052. for (x = 0; x < num_gh; x++)
  1053. p[x] = &ghs[x];
  1054. sort(p, num_gh, sizeof(struct gfs2_holder *), glock_compare, NULL);
  1055. for (x = 0; x < num_gh; x++) {
  1056. p[x]->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
  1057. error = gfs2_glock_nq(p[x]);
  1058. if (error) {
  1059. while (x--)
  1060. gfs2_glock_dq(p[x]);
  1061. break;
  1062. }
  1063. }
  1064. return error;
  1065. }
  1066. /**
  1067. * gfs2_glock_nq_m - acquire multiple glocks
  1068. * @num_gh: the number of structures
  1069. * @ghs: an array of struct gfs2_holder structures
  1070. *
  1071. *
  1072. * Returns: 0 on success (all glocks acquired),
  1073. * errno on failure (no glocks acquired)
  1074. */
  1075. int gfs2_glock_nq_m(unsigned int num_gh, struct gfs2_holder *ghs)
  1076. {
  1077. struct gfs2_holder *tmp[4];
  1078. struct gfs2_holder **pph = tmp;
  1079. int error = 0;
  1080. switch(num_gh) {
  1081. case 0:
  1082. return 0;
  1083. case 1:
  1084. ghs->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
  1085. return gfs2_glock_nq(ghs);
  1086. default:
  1087. if (num_gh <= 4)
  1088. break;
  1089. pph = kmalloc(num_gh * sizeof(struct gfs2_holder *), GFP_NOFS);
  1090. if (!pph)
  1091. return -ENOMEM;
  1092. }
  1093. error = nq_m_sync(num_gh, ghs, pph);
  1094. if (pph != tmp)
  1095. kfree(pph);
  1096. return error;
  1097. }
  1098. /**
  1099. * gfs2_glock_dq_m - release multiple glocks
  1100. * @num_gh: the number of structures
  1101. * @ghs: an array of struct gfs2_holder structures
  1102. *
  1103. */
  1104. void gfs2_glock_dq_m(unsigned int num_gh, struct gfs2_holder *ghs)
  1105. {
  1106. while (num_gh--)
  1107. gfs2_glock_dq(&ghs[num_gh]);
  1108. }
  1109. /**
  1110. * gfs2_glock_dq_uninit_m - release multiple glocks
  1111. * @num_gh: the number of structures
  1112. * @ghs: an array of struct gfs2_holder structures
  1113. *
  1114. */
  1115. void gfs2_glock_dq_uninit_m(unsigned int num_gh, struct gfs2_holder *ghs)
  1116. {
  1117. while (num_gh--)
  1118. gfs2_glock_dq_uninit(&ghs[num_gh]);
  1119. }
  1120. void gfs2_glock_cb(struct gfs2_glock *gl, unsigned int state)
  1121. {
  1122. unsigned long delay = 0;
  1123. unsigned long holdtime;
  1124. unsigned long now = jiffies;
  1125. gfs2_glock_hold(gl);
  1126. holdtime = gl->gl_tchange + gl->gl_hold_time;
  1127. if (test_bit(GLF_QUEUED, &gl->gl_flags) &&
  1128. gl->gl_name.ln_type == LM_TYPE_INODE) {
  1129. if (time_before(now, holdtime))
  1130. delay = holdtime - now;
  1131. if (test_bit(GLF_REPLY_PENDING, &gl->gl_flags))
  1132. delay = gl->gl_hold_time;
  1133. }
  1134. spin_lock(&gl->gl_spin);
  1135. handle_callback(gl, state, delay);
  1136. spin_unlock(&gl->gl_spin);
  1137. if (queue_delayed_work(glock_workqueue, &gl->gl_work, delay) == 0)
  1138. gfs2_glock_put(gl);
  1139. }
  1140. /**
  1141. * gfs2_should_freeze - Figure out if glock should be frozen
  1142. * @gl: The glock in question
  1143. *
  1144. * Glocks are not frozen if (a) the result of the dlm operation is
  1145. * an error, (b) the locking operation was an unlock operation or
  1146. * (c) if there is a "noexp" flagged request anywhere in the queue
  1147. *
  1148. * Returns: 1 if freezing should occur, 0 otherwise
  1149. */
  1150. static int gfs2_should_freeze(const struct gfs2_glock *gl)
  1151. {
  1152. const struct gfs2_holder *gh;
  1153. if (gl->gl_reply & ~LM_OUT_ST_MASK)
  1154. return 0;
  1155. if (gl->gl_target == LM_ST_UNLOCKED)
  1156. return 0;
  1157. list_for_each_entry(gh, &gl->gl_holders, gh_list) {
  1158. if (test_bit(HIF_HOLDER, &gh->gh_iflags))
  1159. continue;
  1160. if (LM_FLAG_NOEXP & gh->gh_flags)
  1161. return 0;
  1162. }
  1163. return 1;
  1164. }
  1165. /**
  1166. * gfs2_glock_complete - Callback used by locking
  1167. * @gl: Pointer to the glock
  1168. * @ret: The return value from the dlm
  1169. *
  1170. * The gl_reply field is under the gl_spin lock so that it is ok
  1171. * to use a bitfield shared with other glock state fields.
  1172. */
  1173. void gfs2_glock_complete(struct gfs2_glock *gl, int ret)
  1174. {
  1175. struct lm_lockstruct *ls = &gl->gl_sbd->sd_lockstruct;
  1176. spin_lock(&gl->gl_spin);
  1177. gl->gl_reply = ret;
  1178. if (unlikely(test_bit(DFL_BLOCK_LOCKS, &ls->ls_recover_flags))) {
  1179. if (gfs2_should_freeze(gl)) {
  1180. set_bit(GLF_FROZEN, &gl->gl_flags);
  1181. spin_unlock(&gl->gl_spin);
  1182. return;
  1183. }
  1184. }
  1185. spin_unlock(&gl->gl_spin);
  1186. set_bit(GLF_REPLY_PENDING, &gl->gl_flags);
  1187. smp_wmb();
  1188. gfs2_glock_hold(gl);
  1189. if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
  1190. gfs2_glock_put(gl);
  1191. }
  1192. static int gfs2_shrink_glock_memory(struct shrinker *shrink,
  1193. struct shrink_control *sc)
  1194. {
  1195. struct gfs2_glock *gl;
  1196. int may_demote;
  1197. int nr_skipped = 0;
  1198. int nr = sc->nr_to_scan;
  1199. gfp_t gfp_mask = sc->gfp_mask;
  1200. LIST_HEAD(skipped);
  1201. if (nr == 0)
  1202. goto out;
  1203. if (!(gfp_mask & __GFP_FS))
  1204. return -1;
  1205. spin_lock(&lru_lock);
  1206. while(nr && !list_empty(&lru_list)) {
  1207. gl = list_entry(lru_list.next, struct gfs2_glock, gl_lru);
  1208. list_del_init(&gl->gl_lru);
  1209. clear_bit(GLF_LRU, &gl->gl_flags);
  1210. atomic_dec(&lru_count);
  1211. /* Test for being demotable */
  1212. if (!test_and_set_bit(GLF_LOCK, &gl->gl_flags)) {
  1213. gfs2_glock_hold(gl);
  1214. spin_unlock(&lru_lock);
  1215. spin_lock(&gl->gl_spin);
  1216. may_demote = demote_ok(gl);
  1217. if (may_demote) {
  1218. handle_callback(gl, LM_ST_UNLOCKED, 0);
  1219. nr--;
  1220. }
  1221. clear_bit(GLF_LOCK, &gl->gl_flags);
  1222. smp_mb__after_clear_bit();
  1223. if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
  1224. gfs2_glock_put_nolock(gl);
  1225. spin_unlock(&gl->gl_spin);
  1226. spin_lock(&lru_lock);
  1227. continue;
  1228. }
  1229. nr_skipped++;
  1230. list_add(&gl->gl_lru, &skipped);
  1231. set_bit(GLF_LRU, &gl->gl_flags);
  1232. }
  1233. list_splice(&skipped, &lru_list);
  1234. atomic_add(nr_skipped, &lru_count);
  1235. spin_unlock(&lru_lock);
  1236. out:
  1237. return (atomic_read(&lru_count) / 100) * sysctl_vfs_cache_pressure;
  1238. }
  1239. static struct shrinker glock_shrinker = {
  1240. .shrink = gfs2_shrink_glock_memory,
  1241. .seeks = DEFAULT_SEEKS,
  1242. };
  1243. /**
  1244. * examine_bucket - Call a function for glock in a hash bucket
  1245. * @examiner: the function
  1246. * @sdp: the filesystem
  1247. * @bucket: the bucket
  1248. *
  1249. */
  1250. static void examine_bucket(glock_examiner examiner, const struct gfs2_sbd *sdp,
  1251. unsigned int hash)
  1252. {
  1253. struct gfs2_glock *gl;
  1254. struct hlist_bl_head *head = &gl_hash_table[hash];
  1255. struct hlist_bl_node *pos;
  1256. rcu_read_lock();
  1257. hlist_bl_for_each_entry_rcu(gl, pos, head, gl_list) {
  1258. if ((gl->gl_sbd == sdp) && atomic_read(&gl->gl_ref))
  1259. examiner(gl);
  1260. }
  1261. rcu_read_unlock();
  1262. cond_resched();
  1263. }
  1264. static void glock_hash_walk(glock_examiner examiner, const struct gfs2_sbd *sdp)
  1265. {
  1266. unsigned x;
  1267. for (x = 0; x < GFS2_GL_HASH_SIZE; x++)
  1268. examine_bucket(examiner, sdp, x);
  1269. }
  1270. /**
  1271. * thaw_glock - thaw out a glock which has an unprocessed reply waiting
  1272. * @gl: The glock to thaw
  1273. *
  1274. * N.B. When we freeze a glock, we leave a ref to the glock outstanding,
  1275. * so this has to result in the ref count being dropped by one.
  1276. */
  1277. static void thaw_glock(struct gfs2_glock *gl)
  1278. {
  1279. if (!test_and_clear_bit(GLF_FROZEN, &gl->gl_flags))
  1280. return;
  1281. set_bit(GLF_REPLY_PENDING, &gl->gl_flags);
  1282. gfs2_glock_hold(gl);
  1283. if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
  1284. gfs2_glock_put(gl);
  1285. }
  1286. /**
  1287. * clear_glock - look at a glock and see if we can free it from glock cache
  1288. * @gl: the glock to look at
  1289. *
  1290. */
  1291. static void clear_glock(struct gfs2_glock *gl)
  1292. {
  1293. gfs2_glock_remove_from_lru(gl);
  1294. spin_lock(&gl->gl_spin);
  1295. if (gl->gl_state != LM_ST_UNLOCKED)
  1296. handle_callback(gl, LM_ST_UNLOCKED, 0);
  1297. spin_unlock(&gl->gl_spin);
  1298. gfs2_glock_hold(gl);
  1299. if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
  1300. gfs2_glock_put(gl);
  1301. }
  1302. /**
  1303. * gfs2_glock_thaw - Thaw any frozen glocks
  1304. * @sdp: The super block
  1305. *
  1306. */
  1307. void gfs2_glock_thaw(struct gfs2_sbd *sdp)
  1308. {
  1309. glock_hash_walk(thaw_glock, sdp);
  1310. }
  1311. static int dump_glock(struct seq_file *seq, struct gfs2_glock *gl)
  1312. {
  1313. int ret;
  1314. spin_lock(&gl->gl_spin);
  1315. ret = __dump_glock(seq, gl);
  1316. spin_unlock(&gl->gl_spin);
  1317. return ret;
  1318. }
  1319. static void dump_glock_func(struct gfs2_glock *gl)
  1320. {
  1321. dump_glock(NULL, gl);
  1322. }
  1323. /**
  1324. * gfs2_gl_hash_clear - Empty out the glock hash table
  1325. * @sdp: the filesystem
  1326. * @wait: wait until it's all gone
  1327. *
  1328. * Called when unmounting the filesystem.
  1329. */
  1330. void gfs2_gl_hash_clear(struct gfs2_sbd *sdp)
  1331. {
  1332. glock_hash_walk(clear_glock, sdp);
  1333. flush_workqueue(glock_workqueue);
  1334. wait_event(sdp->sd_glock_wait, atomic_read(&sdp->sd_glock_disposal) == 0);
  1335. glock_hash_walk(dump_glock_func, sdp);
  1336. }
  1337. void gfs2_glock_finish_truncate(struct gfs2_inode *ip)
  1338. {
  1339. struct gfs2_glock *gl = ip->i_gl;
  1340. int ret;
  1341. ret = gfs2_truncatei_resume(ip);
  1342. gfs2_assert_withdraw(gl->gl_sbd, ret == 0);
  1343. spin_lock(&gl->gl_spin);
  1344. clear_bit(GLF_LOCK, &gl->gl_flags);
  1345. run_queue(gl, 1);
  1346. spin_unlock(&gl->gl_spin);
  1347. }
  1348. static const char *state2str(unsigned state)
  1349. {
  1350. switch(state) {
  1351. case LM_ST_UNLOCKED:
  1352. return "UN";
  1353. case LM_ST_SHARED:
  1354. return "SH";
  1355. case LM_ST_DEFERRED:
  1356. return "DF";
  1357. case LM_ST_EXCLUSIVE:
  1358. return "EX";
  1359. }
  1360. return "??";
  1361. }
  1362. static const char *hflags2str(char *buf, unsigned flags, unsigned long iflags)
  1363. {
  1364. char *p = buf;
  1365. if (flags & LM_FLAG_TRY)
  1366. *p++ = 't';
  1367. if (flags & LM_FLAG_TRY_1CB)
  1368. *p++ = 'T';
  1369. if (flags & LM_FLAG_NOEXP)
  1370. *p++ = 'e';
  1371. if (flags & LM_FLAG_ANY)
  1372. *p++ = 'A';
  1373. if (flags & LM_FLAG_PRIORITY)
  1374. *p++ = 'p';
  1375. if (flags & GL_ASYNC)
  1376. *p++ = 'a';
  1377. if (flags & GL_EXACT)
  1378. *p++ = 'E';
  1379. if (flags & GL_NOCACHE)
  1380. *p++ = 'c';
  1381. if (test_bit(HIF_HOLDER, &iflags))
  1382. *p++ = 'H';
  1383. if (test_bit(HIF_WAIT, &iflags))
  1384. *p++ = 'W';
  1385. if (test_bit(HIF_FIRST, &iflags))
  1386. *p++ = 'F';
  1387. *p = 0;
  1388. return buf;
  1389. }
  1390. /**
  1391. * dump_holder - print information about a glock holder
  1392. * @seq: the seq_file struct
  1393. * @gh: the glock holder
  1394. *
  1395. * Returns: 0 on success, -ENOBUFS when we run out of space
  1396. */
  1397. static int dump_holder(struct seq_file *seq, const struct gfs2_holder *gh)
  1398. {
  1399. struct task_struct *gh_owner = NULL;
  1400. char flags_buf[32];
  1401. if (gh->gh_owner_pid)
  1402. gh_owner = pid_task(gh->gh_owner_pid, PIDTYPE_PID);
  1403. gfs2_print_dbg(seq, " H: s:%s f:%s e:%d p:%ld [%s] %pS\n",
  1404. state2str(gh->gh_state),
  1405. hflags2str(flags_buf, gh->gh_flags, gh->gh_iflags),
  1406. gh->gh_error,
  1407. gh->gh_owner_pid ? (long)pid_nr(gh->gh_owner_pid) : -1,
  1408. gh_owner ? gh_owner->comm : "(ended)",
  1409. (void *)gh->gh_ip);
  1410. return 0;
  1411. }
  1412. static const char *gflags2str(char *buf, const struct gfs2_glock *gl)
  1413. {
  1414. const unsigned long *gflags = &gl->gl_flags;
  1415. char *p = buf;
  1416. if (test_bit(GLF_LOCK, gflags))
  1417. *p++ = 'l';
  1418. if (test_bit(GLF_DEMOTE, gflags))
  1419. *p++ = 'D';
  1420. if (test_bit(GLF_PENDING_DEMOTE, gflags))
  1421. *p++ = 'd';
  1422. if (test_bit(GLF_DEMOTE_IN_PROGRESS, gflags))
  1423. *p++ = 'p';
  1424. if (test_bit(GLF_DIRTY, gflags))
  1425. *p++ = 'y';
  1426. if (test_bit(GLF_LFLUSH, gflags))
  1427. *p++ = 'f';
  1428. if (test_bit(GLF_INVALIDATE_IN_PROGRESS, gflags))
  1429. *p++ = 'i';
  1430. if (test_bit(GLF_REPLY_PENDING, gflags))
  1431. *p++ = 'r';
  1432. if (test_bit(GLF_INITIAL, gflags))
  1433. *p++ = 'I';
  1434. if (test_bit(GLF_FROZEN, gflags))
  1435. *p++ = 'F';
  1436. if (test_bit(GLF_QUEUED, gflags))
  1437. *p++ = 'q';
  1438. if (test_bit(GLF_LRU, gflags))
  1439. *p++ = 'L';
  1440. if (gl->gl_object)
  1441. *p++ = 'o';
  1442. if (test_bit(GLF_BLOCKING, gflags))
  1443. *p++ = 'b';
  1444. *p = 0;
  1445. return buf;
  1446. }
  1447. /**
  1448. * __dump_glock - print information about a glock
  1449. * @seq: The seq_file struct
  1450. * @gl: the glock
  1451. *
  1452. * The file format is as follows:
  1453. * One line per object, capital letters are used to indicate objects
  1454. * G = glock, I = Inode, R = rgrp, H = holder. Glocks are not indented,
  1455. * other objects are indented by a single space and follow the glock to
  1456. * which they are related. Fields are indicated by lower case letters
  1457. * followed by a colon and the field value, except for strings which are in
  1458. * [] so that its possible to see if they are composed of spaces for
  1459. * example. The field's are n = number (id of the object), f = flags,
  1460. * t = type, s = state, r = refcount, e = error, p = pid.
  1461. *
  1462. * Returns: 0 on success, -ENOBUFS when we run out of space
  1463. */
  1464. static int __dump_glock(struct seq_file *seq, const struct gfs2_glock *gl)
  1465. {
  1466. const struct gfs2_glock_operations *glops = gl->gl_ops;
  1467. unsigned long long dtime;
  1468. const struct gfs2_holder *gh;
  1469. char gflags_buf[32];
  1470. int error = 0;
  1471. dtime = jiffies - gl->gl_demote_time;
  1472. dtime *= 1000000/HZ; /* demote time in uSec */
  1473. if (!test_bit(GLF_DEMOTE, &gl->gl_flags))
  1474. dtime = 0;
  1475. 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",
  1476. state2str(gl->gl_state),
  1477. gl->gl_name.ln_type,
  1478. (unsigned long long)gl->gl_name.ln_number,
  1479. gflags2str(gflags_buf, gl),
  1480. state2str(gl->gl_target),
  1481. state2str(gl->gl_demote_state), dtime,
  1482. atomic_read(&gl->gl_ail_count),
  1483. atomic_read(&gl->gl_revokes),
  1484. atomic_read(&gl->gl_ref), gl->gl_hold_time);
  1485. list_for_each_entry(gh, &gl->gl_holders, gh_list) {
  1486. error = dump_holder(seq, gh);
  1487. if (error)
  1488. goto out;
  1489. }
  1490. if (gl->gl_state != LM_ST_UNLOCKED && glops->go_dump)
  1491. error = glops->go_dump(seq, gl);
  1492. out:
  1493. return error;
  1494. }
  1495. static int gfs2_glstats_seq_show(struct seq_file *seq, void *iter_ptr)
  1496. {
  1497. struct gfs2_glock *gl = iter_ptr;
  1498. seq_printf(seq, "G: n:%u/%llx rtt:%lld/%lld rttb:%lld/%lld irt:%lld/%lld dcnt: %lld qcnt: %lld\n",
  1499. gl->gl_name.ln_type,
  1500. (unsigned long long)gl->gl_name.ln_number,
  1501. (long long)gl->gl_stats.stats[GFS2_LKS_SRTT],
  1502. (long long)gl->gl_stats.stats[GFS2_LKS_SRTTVAR],
  1503. (long long)gl->gl_stats.stats[GFS2_LKS_SRTTB],
  1504. (long long)gl->gl_stats.stats[GFS2_LKS_SRTTVARB],
  1505. (long long)gl->gl_stats.stats[GFS2_LKS_SIRT],
  1506. (long long)gl->gl_stats.stats[GFS2_LKS_SIRTVAR],
  1507. (long long)gl->gl_stats.stats[GFS2_LKS_DCOUNT],
  1508. (long long)gl->gl_stats.stats[GFS2_LKS_QCOUNT]);
  1509. return 0;
  1510. }
  1511. static const char *gfs2_gltype[] = {
  1512. "type",
  1513. "reserved",
  1514. "nondisk",
  1515. "inode",
  1516. "rgrp",
  1517. "meta",
  1518. "iopen",
  1519. "flock",
  1520. "plock",
  1521. "quota",
  1522. "journal",
  1523. };
  1524. static const char *gfs2_stype[] = {
  1525. [GFS2_LKS_SRTT] = "srtt",
  1526. [GFS2_LKS_SRTTVAR] = "srttvar",
  1527. [GFS2_LKS_SRTTB] = "srttb",
  1528. [GFS2_LKS_SRTTVARB] = "srttvarb",
  1529. [GFS2_LKS_SIRT] = "sirt",
  1530. [GFS2_LKS_SIRTVAR] = "sirtvar",
  1531. [GFS2_LKS_DCOUNT] = "dlm",
  1532. [GFS2_LKS_QCOUNT] = "queue",
  1533. };
  1534. #define GFS2_NR_SBSTATS (ARRAY_SIZE(gfs2_gltype) * ARRAY_SIZE(gfs2_stype))
  1535. static int gfs2_sbstats_seq_show(struct seq_file *seq, void *iter_ptr)
  1536. {
  1537. struct gfs2_glock_iter *gi = seq->private;
  1538. struct gfs2_sbd *sdp = gi->sdp;
  1539. unsigned index = gi->hash >> 3;
  1540. unsigned subindex = gi->hash & 0x07;
  1541. s64 value;
  1542. int i;
  1543. if (index == 0 && subindex != 0)
  1544. return 0;
  1545. seq_printf(seq, "%-10s %8s:", gfs2_gltype[index],
  1546. (index == 0) ? "cpu": gfs2_stype[subindex]);
  1547. for_each_possible_cpu(i) {
  1548. const struct gfs2_pcpu_lkstats *lkstats = per_cpu_ptr(sdp->sd_lkstats, i);
  1549. if (index == 0) {
  1550. value = i;
  1551. } else {
  1552. value = lkstats->lkstats[index - 1].stats[subindex];
  1553. }
  1554. seq_printf(seq, " %15lld", (long long)value);
  1555. }
  1556. seq_putc(seq, '\n');
  1557. return 0;
  1558. }
  1559. int __init gfs2_glock_init(void)
  1560. {
  1561. unsigned i;
  1562. for(i = 0; i < GFS2_GL_HASH_SIZE; i++) {
  1563. INIT_HLIST_BL_HEAD(&gl_hash_table[i]);
  1564. }
  1565. glock_workqueue = alloc_workqueue("glock_workqueue", WQ_MEM_RECLAIM |
  1566. WQ_HIGHPRI | WQ_FREEZABLE, 0);
  1567. if (IS_ERR(glock_workqueue))
  1568. return PTR_ERR(glock_workqueue);
  1569. gfs2_delete_workqueue = alloc_workqueue("delete_workqueue",
  1570. WQ_MEM_RECLAIM | WQ_FREEZABLE,
  1571. 0);
  1572. if (IS_ERR(gfs2_delete_workqueue)) {
  1573. destroy_workqueue(glock_workqueue);
  1574. return PTR_ERR(gfs2_delete_workqueue);
  1575. }
  1576. register_shrinker(&glock_shrinker);
  1577. return 0;
  1578. }
  1579. void gfs2_glock_exit(void)
  1580. {
  1581. unregister_shrinker(&glock_shrinker);
  1582. destroy_workqueue(glock_workqueue);
  1583. destroy_workqueue(gfs2_delete_workqueue);
  1584. }
  1585. static inline struct gfs2_glock *glock_hash_chain(unsigned hash)
  1586. {
  1587. return hlist_bl_entry(hlist_bl_first_rcu(&gl_hash_table[hash]),
  1588. struct gfs2_glock, gl_list);
  1589. }
  1590. static inline struct gfs2_glock *glock_hash_next(struct gfs2_glock *gl)
  1591. {
  1592. return hlist_bl_entry(rcu_dereference(gl->gl_list.next),
  1593. struct gfs2_glock, gl_list);
  1594. }
  1595. static int gfs2_glock_iter_next(struct gfs2_glock_iter *gi)
  1596. {
  1597. struct gfs2_glock *gl;
  1598. do {
  1599. gl = gi->gl;
  1600. if (gl) {
  1601. gi->gl = glock_hash_next(gl);
  1602. gi->nhash++;
  1603. } else {
  1604. if (gi->hash >= GFS2_GL_HASH_SIZE) {
  1605. rcu_read_unlock();
  1606. return 1;
  1607. }
  1608. gi->gl = glock_hash_chain(gi->hash);
  1609. gi->nhash = 0;
  1610. }
  1611. while (gi->gl == NULL) {
  1612. gi->hash++;
  1613. if (gi->hash >= GFS2_GL_HASH_SIZE) {
  1614. rcu_read_unlock();
  1615. return 1;
  1616. }
  1617. gi->gl = glock_hash_chain(gi->hash);
  1618. gi->nhash = 0;
  1619. }
  1620. /* Skip entries for other sb and dead entries */
  1621. } while (gi->sdp != gi->gl->gl_sbd || atomic_read(&gi->gl->gl_ref) == 0);
  1622. return 0;
  1623. }
  1624. static void *gfs2_glock_seq_start(struct seq_file *seq, loff_t *pos)
  1625. {
  1626. struct gfs2_glock_iter *gi = seq->private;
  1627. loff_t n = *pos;
  1628. if (gi->last_pos <= *pos)
  1629. n = gi->nhash + (*pos - gi->last_pos);
  1630. else
  1631. gi->hash = 0;
  1632. gi->nhash = 0;
  1633. rcu_read_lock();
  1634. do {
  1635. if (gfs2_glock_iter_next(gi))
  1636. return NULL;
  1637. } while (n--);
  1638. gi->last_pos = *pos;
  1639. return gi->gl;
  1640. }
  1641. static void *gfs2_glock_seq_next(struct seq_file *seq, void *iter_ptr,
  1642. loff_t *pos)
  1643. {
  1644. struct gfs2_glock_iter *gi = seq->private;
  1645. (*pos)++;
  1646. gi->last_pos = *pos;
  1647. if (gfs2_glock_iter_next(gi))
  1648. return NULL;
  1649. return gi->gl;
  1650. }
  1651. static void gfs2_glock_seq_stop(struct seq_file *seq, void *iter_ptr)
  1652. {
  1653. struct gfs2_glock_iter *gi = seq->private;
  1654. if (gi->gl)
  1655. rcu_read_unlock();
  1656. gi->gl = NULL;
  1657. }
  1658. static int gfs2_glock_seq_show(struct seq_file *seq, void *iter_ptr)
  1659. {
  1660. return dump_glock(seq, iter_ptr);
  1661. }
  1662. static void *gfs2_sbstats_seq_start(struct seq_file *seq, loff_t *pos)
  1663. {
  1664. struct gfs2_glock_iter *gi = seq->private;
  1665. gi->hash = *pos;
  1666. if (*pos >= GFS2_NR_SBSTATS)
  1667. return NULL;
  1668. preempt_disable();
  1669. return SEQ_START_TOKEN;
  1670. }
  1671. static void *gfs2_sbstats_seq_next(struct seq_file *seq, void *iter_ptr,
  1672. loff_t *pos)
  1673. {
  1674. struct gfs2_glock_iter *gi = seq->private;
  1675. (*pos)++;
  1676. gi->hash++;
  1677. if (gi->hash >= GFS2_NR_SBSTATS) {
  1678. preempt_enable();
  1679. return NULL;
  1680. }
  1681. return SEQ_START_TOKEN;
  1682. }
  1683. static void gfs2_sbstats_seq_stop(struct seq_file *seq, void *iter_ptr)
  1684. {
  1685. preempt_enable();
  1686. }
  1687. static const struct seq_operations gfs2_glock_seq_ops = {
  1688. .start = gfs2_glock_seq_start,
  1689. .next = gfs2_glock_seq_next,
  1690. .stop = gfs2_glock_seq_stop,
  1691. .show = gfs2_glock_seq_show,
  1692. };
  1693. static const struct seq_operations gfs2_glstats_seq_ops = {
  1694. .start = gfs2_glock_seq_start,
  1695. .next = gfs2_glock_seq_next,
  1696. .stop = gfs2_glock_seq_stop,
  1697. .show = gfs2_glstats_seq_show,
  1698. };
  1699. static const struct seq_operations gfs2_sbstats_seq_ops = {
  1700. .start = gfs2_sbstats_seq_start,
  1701. .next = gfs2_sbstats_seq_next,
  1702. .stop = gfs2_sbstats_seq_stop,
  1703. .show = gfs2_sbstats_seq_show,
  1704. };
  1705. #define GFS2_SEQ_GOODSIZE min(PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER, 65536UL)
  1706. static int gfs2_glocks_open(struct inode *inode, struct file *file)
  1707. {
  1708. int ret = seq_open_private(file, &gfs2_glock_seq_ops,
  1709. sizeof(struct gfs2_glock_iter));
  1710. if (ret == 0) {
  1711. struct seq_file *seq = file->private_data;
  1712. struct gfs2_glock_iter *gi = seq->private;
  1713. gi->sdp = inode->i_private;
  1714. seq->buf = kmalloc(GFS2_SEQ_GOODSIZE, GFP_KERNEL | __GFP_NOWARN);
  1715. if (seq->buf)
  1716. seq->size = GFS2_SEQ_GOODSIZE;
  1717. }
  1718. return ret;
  1719. }
  1720. static int gfs2_glstats_open(struct inode *inode, struct file *file)
  1721. {
  1722. int ret = seq_open_private(file, &gfs2_glstats_seq_ops,
  1723. sizeof(struct gfs2_glock_iter));
  1724. if (ret == 0) {
  1725. struct seq_file *seq = file->private_data;
  1726. struct gfs2_glock_iter *gi = seq->private;
  1727. gi->sdp = inode->i_private;
  1728. seq->buf = kmalloc(GFS2_SEQ_GOODSIZE, GFP_KERNEL | __GFP_NOWARN);
  1729. if (seq->buf)
  1730. seq->size = GFS2_SEQ_GOODSIZE;
  1731. }
  1732. return ret;
  1733. }
  1734. static int gfs2_sbstats_open(struct inode *inode, struct file *file)
  1735. {
  1736. int ret = seq_open_private(file, &gfs2_sbstats_seq_ops,
  1737. sizeof(struct gfs2_glock_iter));
  1738. if (ret == 0) {
  1739. struct seq_file *seq = file->private_data;
  1740. struct gfs2_glock_iter *gi = seq->private;
  1741. gi->sdp = inode->i_private;
  1742. }
  1743. return ret;
  1744. }
  1745. static const struct file_operations gfs2_glocks_fops = {
  1746. .owner = THIS_MODULE,
  1747. .open = gfs2_glocks_open,
  1748. .read = seq_read,
  1749. .llseek = seq_lseek,
  1750. .release = seq_release_private,
  1751. };
  1752. static const struct file_operations gfs2_glstats_fops = {
  1753. .owner = THIS_MODULE,
  1754. .open = gfs2_glstats_open,
  1755. .read = seq_read,
  1756. .llseek = seq_lseek,
  1757. .release = seq_release_private,
  1758. };
  1759. static const struct file_operations gfs2_sbstats_fops = {
  1760. .owner = THIS_MODULE,
  1761. .open = gfs2_sbstats_open,
  1762. .read = seq_read,
  1763. .llseek = seq_lseek,
  1764. .release = seq_release_private,
  1765. };
  1766. int gfs2_create_debugfs_file(struct gfs2_sbd *sdp)
  1767. {
  1768. sdp->debugfs_dir = debugfs_create_dir(sdp->sd_table_name, gfs2_root);
  1769. if (!sdp->debugfs_dir)
  1770. return -ENOMEM;
  1771. sdp->debugfs_dentry_glocks = debugfs_create_file("glocks",
  1772. S_IFREG | S_IRUGO,
  1773. sdp->debugfs_dir, sdp,
  1774. &gfs2_glocks_fops);
  1775. if (!sdp->debugfs_dentry_glocks)
  1776. goto fail;
  1777. sdp->debugfs_dentry_glstats = debugfs_create_file("glstats",
  1778. S_IFREG | S_IRUGO,
  1779. sdp->debugfs_dir, sdp,
  1780. &gfs2_glstats_fops);
  1781. if (!sdp->debugfs_dentry_glstats)
  1782. goto fail;
  1783. sdp->debugfs_dentry_sbstats = debugfs_create_file("sbstats",
  1784. S_IFREG | S_IRUGO,
  1785. sdp->debugfs_dir, sdp,
  1786. &gfs2_sbstats_fops);
  1787. if (!sdp->debugfs_dentry_sbstats)
  1788. goto fail;
  1789. return 0;
  1790. fail:
  1791. gfs2_delete_debugfs_file(sdp);
  1792. return -ENOMEM;
  1793. }
  1794. void gfs2_delete_debugfs_file(struct gfs2_sbd *sdp)
  1795. {
  1796. if (sdp->debugfs_dir) {
  1797. if (sdp->debugfs_dentry_glocks) {
  1798. debugfs_remove(sdp->debugfs_dentry_glocks);
  1799. sdp->debugfs_dentry_glocks = NULL;
  1800. }
  1801. if (sdp->debugfs_dentry_glstats) {
  1802. debugfs_remove(sdp->debugfs_dentry_glstats);
  1803. sdp->debugfs_dentry_glstats = NULL;
  1804. }
  1805. if (sdp->debugfs_dentry_sbstats) {
  1806. debugfs_remove(sdp->debugfs_dentry_sbstats);
  1807. sdp->debugfs_dentry_sbstats = NULL;
  1808. }
  1809. debugfs_remove(sdp->debugfs_dir);
  1810. sdp->debugfs_dir = NULL;
  1811. }
  1812. }
  1813. int gfs2_register_debugfs(void)
  1814. {
  1815. gfs2_root = debugfs_create_dir("gfs2", NULL);
  1816. return gfs2_root ? 0 : -ENOMEM;
  1817. }
  1818. void gfs2_unregister_debugfs(void)
  1819. {
  1820. debugfs_remove(gfs2_root);
  1821. gfs2_root = NULL;
  1822. }