glock.c 50 KB

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