glock.c 52 KB

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