10 #include <linux/sched.h>
11 #include <linux/slab.h>
19 #include <linux/list.h>
20 #include <linux/wait.h>
21 #include <linux/module.h>
22 #include <asm/uaccess.h>
45 #define CREATE_TRACE_POINTS
59 #define GLOCK_BUG_ON(gl,x) do { if (unlikely(x)) { __dump_glock(NULL, gl); BUG(); } } while(0)
62 static struct dentry *gfs2_root;
69 #define GFS2_GL_HASH_SHIFT 15
70 #define GFS2_GL_HASH_SIZE (1 << GFS2_GL_HASH_SHIFT)
71 #define GFS2_GL_HASH_MASK (GFS2_GL_HASH_SIZE - 1)
74 static struct dentry *gfs2_root;
83 static unsigned int gl_hash(
const struct gfs2_sbd *sdp,
89 h = jhash(&name->
ln_type,
sizeof(
unsigned int), h);
90 h = jhash(&sdp,
sizeof(
struct gfs2_sbd *), h);
96 static inline void spin_lock_bucket(
unsigned int hash)
98 hlist_bl_lock(&gl_hash_table[hash]);
101 static inline void spin_unlock_bucket(
unsigned int hash)
103 hlist_bl_unlock(&gl_hash_table[hash]);
106 static void gfs2_glock_dealloc(
struct rcu_head *rcu)
144 static int demote_ok(
const struct gfs2_glock *gl)
160 spin_lock(&lru_lock);
162 if (!list_empty(&gl->
gl_lru))
163 list_del_init(&gl->
gl_lru);
169 spin_unlock(&lru_lock);
172 static void __gfs2_glock_remove_from_lru(
struct gfs2_glock *gl)
174 if (!list_empty(&gl->
gl_lru)) {
175 list_del_init(&gl->
gl_lru);
181 static void gfs2_glock_remove_from_lru(
struct gfs2_glock *gl)
183 spin_lock(&lru_lock);
184 __gfs2_glock_remove_from_lru(gl);
185 spin_unlock(&lru_lock);
214 __gfs2_glock_remove_from_lru(gl);
215 spin_unlock(&lru_lock);
217 hlist_bl_del_rcu(&gl->
gl_list);
218 spin_unlock_bucket(gl->
gl_hash);
221 trace_gfs2_glock_put(gl);
282 static void gfs2_holder_wake(
struct gfs2_holder *gh)
308 trace_gfs2_glock_queue(gh, 0);
309 gfs2_holder_wake(gh);
333 if (may_grant(gl, gh)) {
345 trace_gfs2_glock_queue(gh, 0);
346 gfs2_holder_wake(gh);
350 trace_gfs2_promote(gh, 1);
351 gfs2_holder_wake(gh);
355 trace_gfs2_promote(gh, 0);
356 gfs2_holder_wake(gh);
397 if (held1 != held2) {
403 if (held1 && held2 && list_empty(&gl->
gl_holders))
414 static void gfs2_demote_wake(
struct gfs2_glock *gl)
429 static void finish_xmote(
struct gfs2_glock *gl,
unsigned int ret)
437 trace_gfs2_glock_state_change(gl, state);
438 state_change(gl, state);
439 gh = find_first_waiter(gl);
453 gh = find_first_waiter(gl);
486 gfs2_demote_wake(gl);
521 unsigned int lck_flags = gh ? gh->
gh_flags : 0;
584 static void run_queue(
struct gfs2_glock *gl,
const int nonblock)
598 if (find_first_holder(gl))
607 gfs2_demote_wake(gl);
608 ret = do_promote(gl);
613 gh = find_first_waiter(gl);
651 if (inode && !IS_ERR(inode)) {
658 static void glock_work_func(
struct work_struct *work)
660 unsigned long delay = 0;
672 unsigned long holdtime, now =
jiffies;
676 delay = holdtime - now;
717 unsigned int hash =
gl_hash(sdp, &name);
722 gl = search_bucket(hash, sdp, &name);
765 mapping = gfs2_glock2aspace(gl);
770 mapping_set_gfp_mask(mapping,
GFP_NOFS);
776 spin_lock_bucket(hash);
777 tmp = search_bucket(hash, sdp, &name);
779 spin_unlock_bucket(hash);
784 hlist_bl_add_head_rcu(&gl->
gl_list, &gl_hash_table[hash]);
785 spin_unlock_bucket(hash);
807 gh->
gh_ip = (
unsigned long)__builtin_return_address(0);
831 gh->
gh_ip = (
unsigned long)__builtin_return_address(0);
860 static int gfs2_glock_holder_wait(
void *
word)
866 static int gfs2_glock_demote_wait(
void *
word)
902 static void handle_callback(
struct gfs2_glock *gl,
unsigned int state,
915 if (gl->
gl_ops->go_callback)
916 gl->
gl_ops->go_callback(gl);
917 trace_gfs2_demote_rq(gl);
949 static inline void add_to_queue(
struct gfs2_holder *gh)
965 try_futile = !may_grant(gl, gh);
978 gfs2_holder_wake(gh);
987 trace_gfs2_glock_queue(gh, 1);
1016 __dump_glock(
NULL, gl);
1039 gfs2_glock_remove_from_lru(gl);
1085 if (find_first_holder(gl) ==
NULL) {
1101 trace_gfs2_glock_queue(gh, 0);
1156 error = gfs2_glock_nq_init(gl, state, flags, gh);
1170 static int glock_compare(
const void *arg_a,
const void *arg_b)
1194 static int nq_m_sync(
unsigned int num_gh,
struct gfs2_holder *ghs,
1200 for (x = 0; x < num_gh; x++)
1205 for (x = 0; x < num_gh; x++) {
1249 error = nq_m_sync(num_gh, ghs, pph);
1285 unsigned long delay = 0;
1286 unsigned long holdtime;
1294 delay = holdtime - now;
1300 handle_callback(gl, state, delay);
1317 static int gfs2_should_freeze(
const struct gfs2_glock *gl)
1321 if (gl->
gl_reply & ~LM_OUT_ST_MASK)
1353 if (gfs2_should_freeze(gl)) {
1385 spin_lock(&lru_lock);
1386 while(nr && !list_empty(&
lru_list)) {
1388 list_del_init(&gl->
gl_lru);
1395 spin_unlock(&lru_lock);
1397 may_demote = demote_ok(gl);
1407 spin_lock(&lru_lock);
1411 list_add(&gl->
gl_lru, &skipped);
1416 spin_unlock(&lru_lock);
1421 static struct shrinker glock_shrinker = {
1422 .shrink = gfs2_shrink_glock_memory,
1455 examine_bucket(examiner, sdp, x);
1467 static void thaw_glock(
struct gfs2_glock *gl)
1483 static void clear_glock(
struct gfs2_glock *gl)
1485 gfs2_glock_remove_from_lru(gl);
1504 glock_hash_walk(thaw_glock, sdp);
1511 ret = __dump_glock(seq, gl);
1516 static void dump_glock_func(
struct gfs2_glock *gl)
1518 dump_glock(
NULL, gl);
1531 glock_hash_walk(clear_glock, sdp);
1534 glock_hash_walk(dump_glock_func, sdp);
1551 static const char *state2str(
unsigned state)
1566 static const char *hflags2str(
char *
buf,
unsigned flags,
unsigned long iflags)
1615 gh_owner ? gh_owner->
comm :
"(ended)",
1620 static const char *gflags2str(
char *buf,
const struct gfs2_glock *gl)
1622 const unsigned long *gflags = &gl->
gl_flags;
1678 unsigned long long dtime;
1680 char gflags_buf[32];
1684 dtime *= 1000000/
HZ;
1687 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",
1690 (
unsigned long long)gl->
gl_name.ln_number,
1691 gflags2str(gflags_buf, gl),
1699 error = dump_holder(seq, gh);
1704 error = glops->
go_dump(seq, gl);
1709 static int gfs2_glstats_seq_show(
struct seq_file *seq,
void *iter_ptr)
1713 seq_printf(seq,
"G: n:%u/%llx rtt:%lld/%lld rttb:%lld/%lld irt:%lld/%lld dcnt: %lld qcnt: %lld\n",
1715 (
unsigned long long)gl->
gl_name.ln_number,
1727 static const char *gfs2_gltype[] = {
1741 static const char *gfs2_stype[] = {
1752 #define GFS2_NR_SBSTATS (ARRAY_SIZE(gfs2_gltype) * ARRAY_SIZE(gfs2_stype))
1754 static int gfs2_sbstats_seq_show(
struct seq_file *seq,
void *iter_ptr)
1759 unsigned subindex = gi->
hash & 0x07;
1763 if (index == 0 && subindex != 0)
1766 seq_printf(seq,
"%-10s %8s:", gfs2_gltype[index],
1767 (index == 0) ?
"cpu": gfs2_stype[subindex]);
1774 value = lkstats->
lkstats[index - 1].stats[subindex];
1776 seq_printf(seq,
" %15lld", (
long long)value);
1791 if (IS_ERR(glock_workqueue))
1792 return PTR_ERR(glock_workqueue);
1796 if (IS_ERR(gfs2_delete_workqueue)) {
1798 return PTR_ERR(gfs2_delete_workqueue);
1813 static inline struct gfs2_glock *glock_hash_chain(
unsigned hash)
1832 gi->
gl = glock_hash_next(gl);
1835 if (gi->
hash >= GFS2_GL_HASH_SIZE) {
1839 gi->
gl = glock_hash_chain(gi->
hash);
1844 if (gi->
hash >= GFS2_GL_HASH_SIZE) {
1848 gi->
gl = glock_hash_chain(gi->
hash);
1857 static void *gfs2_glock_seq_start(
struct seq_file *seq, loff_t *pos)
1871 if (gfs2_glock_iter_next(gi))
1879 static void *gfs2_glock_seq_next(
struct seq_file *seq,
void *iter_ptr,
1886 if (gfs2_glock_iter_next(gi))
1892 static void gfs2_glock_seq_stop(
struct seq_file *seq,
void *iter_ptr)
1901 static int gfs2_glock_seq_show(
struct seq_file *seq,
void *iter_ptr)
1903 return dump_glock(seq, iter_ptr);
1906 static void *gfs2_sbstats_seq_start(
struct seq_file *seq, loff_t *pos)
1917 static void *gfs2_sbstats_seq_next(
struct seq_file *seq,
void *iter_ptr,
1930 static void gfs2_sbstats_seq_stop(
struct seq_file *seq,
void *iter_ptr)
1936 .start = gfs2_glock_seq_start,
1937 .next = gfs2_glock_seq_next,
1938 .stop = gfs2_glock_seq_stop,
1939 .show = gfs2_glock_seq_show,
1943 .start = gfs2_glock_seq_start,
1944 .next = gfs2_glock_seq_next,
1945 .stop = gfs2_glock_seq_stop,
1946 .show = gfs2_glstats_seq_show,
1950 .start = gfs2_sbstats_seq_start,
1951 .next = gfs2_sbstats_seq_next,
1952 .stop = gfs2_sbstats_seq_stop,
1953 .show = gfs2_sbstats_seq_show,
1956 #define GFS2_SEQ_GOODSIZE min(PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER, 65536UL)
1958 static int gfs2_glocks_open(
struct inode *inode,
struct file *
file)
1973 static int gfs2_glstats_open(
struct inode *inode,
struct file *
file)
1988 static int gfs2_sbstats_open(
struct inode *inode,
struct file *file)
2002 .open = gfs2_glocks_open,
2010 .open = gfs2_glstats_open,
2018 .open = gfs2_sbstats_open,
2039 &gfs2_glstats_fops);
2046 &gfs2_sbstats_fops);
2079 return gfs2_root ? 0 : -
ENOMEM;