glock.c 51 KB

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