glock.c 54 KB

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