123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893 |
- /*
- * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
- * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
- *
- * This copyrighted material is made available to anyone wishing to use,
- * modify, copy, or redistribute it subject to the terms and conditions
- * of the GNU General Public License version 2.
- */
- #include <linux/sched.h>
- #include <linux/slab.h>
- #include <linux/spinlock.h>
- #include <linux/buffer_head.h>
- #include <linux/delay.h>
- #include <linux/sort.h>
- #include <linux/jhash.h>
- #include <linux/kallsyms.h>
- #include <linux/gfs2_ondisk.h>
- #include <linux/list.h>
- #include <linux/wait.h>
- #include <linux/module.h>
- #include <linux/rwsem.h>
- #include <asm/uaccess.h>
- #include <linux/seq_file.h>
- #include <linux/debugfs.h>
- #include <linux/kthread.h>
- #include <linux/freezer.h>
- #include <linux/workqueue.h>
- #include <linux/jiffies.h>
- #include "gfs2.h"
- #include "incore.h"
- #include "glock.h"
- #include "glops.h"
- #include "inode.h"
- #include "lops.h"
- #include "meta_io.h"
- #include "quota.h"
- #include "super.h"
- #include "util.h"
- #include "bmap.h"
- #define CREATE_TRACE_POINTS
- #include "trace_gfs2.h"
- struct gfs2_gl_hash_bucket {
- struct hlist_head hb_list;
- };
- struct gfs2_glock_iter {
- int hash; /* hash bucket index */
- struct gfs2_sbd *sdp; /* incore superblock */
- struct gfs2_glock *gl; /* current glock struct */
- char string[512]; /* scratch space */
- };
- typedef void (*glock_examiner) (struct gfs2_glock * gl);
- static int gfs2_dump_lockstate(struct gfs2_sbd *sdp);
- static int __dump_glock(struct seq_file *seq, const struct gfs2_glock *gl);
- #define GLOCK_BUG_ON(gl,x) do { if (unlikely(x)) { __dump_glock(NULL, gl); BUG(); } } while(0)
- static void do_xmote(struct gfs2_glock *gl, struct gfs2_holder *gh, unsigned int target);
- static DECLARE_RWSEM(gfs2_umount_flush_sem);
- static struct dentry *gfs2_root;
- static struct workqueue_struct *glock_workqueue;
- static LIST_HEAD(lru_list);
- static atomic_t lru_count = ATOMIC_INIT(0);
- static DEFINE_SPINLOCK(lru_lock);
- #define GFS2_GL_HASH_SHIFT 15
- #define GFS2_GL_HASH_SIZE (1 << GFS2_GL_HASH_SHIFT)
- #define GFS2_GL_HASH_MASK (GFS2_GL_HASH_SIZE - 1)
- static struct gfs2_gl_hash_bucket gl_hash_table[GFS2_GL_HASH_SIZE];
- static struct dentry *gfs2_root;
- /*
- * Despite what you might think, the numbers below are not arbitrary :-)
- * They are taken from the ipv4 routing hash code, which is well tested
- * and thus should be nearly optimal. Later on we might tweek the numbers
- * but for now this should be fine.
- *
- * The reason for putting the locks in a separate array from the list heads
- * is that we can have fewer locks than list heads and save memory. We use
- * the same hash function for both, but with a different hash mask.
- */
- #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK) || \
- defined(CONFIG_PROVE_LOCKING)
- #ifdef CONFIG_LOCKDEP
- # define GL_HASH_LOCK_SZ 256
- #else
- # if NR_CPUS >= 32
- # define GL_HASH_LOCK_SZ 4096
- # elif NR_CPUS >= 16
- # define GL_HASH_LOCK_SZ 2048
- # elif NR_CPUS >= 8
- # define GL_HASH_LOCK_SZ 1024
- # elif NR_CPUS >= 4
- # define GL_HASH_LOCK_SZ 512
- # else
- # define GL_HASH_LOCK_SZ 256
- # endif
- #endif
- /* We never want more locks than chains */
- #if GFS2_GL_HASH_SIZE < GL_HASH_LOCK_SZ
- # undef GL_HASH_LOCK_SZ
- # define GL_HASH_LOCK_SZ GFS2_GL_HASH_SIZE
- #endif
- static rwlock_t gl_hash_locks[GL_HASH_LOCK_SZ];
- static inline rwlock_t *gl_lock_addr(unsigned int x)
- {
- return &gl_hash_locks[x & (GL_HASH_LOCK_SZ-1)];
- }
- #else /* not SMP, so no spinlocks required */
- static inline rwlock_t *gl_lock_addr(unsigned int x)
- {
- return NULL;
- }
- #endif
- /**
- * gl_hash() - Turn glock number into hash bucket number
- * @lock: The glock number
- *
- * Returns: The number of the corresponding hash bucket
- */
- static unsigned int gl_hash(const struct gfs2_sbd *sdp,
- const struct lm_lockname *name)
- {
- unsigned int h;
- h = jhash(&name->ln_number, sizeof(u64), 0);
- h = jhash(&name->ln_type, sizeof(unsigned int), h);
- h = jhash(&sdp, sizeof(struct gfs2_sbd *), h);
- h &= GFS2_GL_HASH_MASK;
- return h;
- }
- /**
- * glock_free() - Perform a few checks and then release struct gfs2_glock
- * @gl: The glock to release
- *
- * Also calls lock module to release its internal structure for this glock.
- *
- */
- static void glock_free(struct gfs2_glock *gl)
- {
- struct gfs2_sbd *sdp = gl->gl_sbd;
- struct inode *aspace = gl->gl_aspace;
- if (aspace)
- gfs2_aspace_put(aspace);
- trace_gfs2_glock_put(gl);
- sdp->sd_lockstruct.ls_ops->lm_put_lock(gfs2_glock_cachep, gl);
- }
- /**
- * gfs2_glock_hold() - increment reference count on glock
- * @gl: The glock to hold
- *
- */
- static void gfs2_glock_hold(struct gfs2_glock *gl)
- {
- GLOCK_BUG_ON(gl, atomic_read(&gl->gl_ref) == 0);
- atomic_inc(&gl->gl_ref);
- }
- /**
- * gfs2_glock_schedule_for_reclaim - Add a glock to the reclaim list
- * @gl: the glock
- *
- */
- static void gfs2_glock_schedule_for_reclaim(struct gfs2_glock *gl)
- {
- spin_lock(&lru_lock);
- if (list_empty(&gl->gl_lru) && gl->gl_state != LM_ST_UNLOCKED) {
- list_add_tail(&gl->gl_lru, &lru_list);
- atomic_inc(&lru_count);
- }
- spin_unlock(&lru_lock);
- }
- /**
- * gfs2_glock_put() - Decrement reference count on glock
- * @gl: The glock to put
- *
- */
- int gfs2_glock_put(struct gfs2_glock *gl)
- {
- int rv = 0;
- write_lock(gl_lock_addr(gl->gl_hash));
- if (atomic_dec_and_test(&gl->gl_ref)) {
- hlist_del(&gl->gl_list);
- write_unlock(gl_lock_addr(gl->gl_hash));
- spin_lock(&lru_lock);
- if (!list_empty(&gl->gl_lru)) {
- list_del_init(&gl->gl_lru);
- atomic_dec(&lru_count);
- }
- spin_unlock(&lru_lock);
- GLOCK_BUG_ON(gl, !list_empty(&gl->gl_holders));
- glock_free(gl);
- rv = 1;
- goto out;
- }
- /* 1 for being hashed, 1 for having state != LM_ST_UNLOCKED */
- if (atomic_read(&gl->gl_ref) == 2)
- gfs2_glock_schedule_for_reclaim(gl);
- write_unlock(gl_lock_addr(gl->gl_hash));
- out:
- return rv;
- }
- /**
- * search_bucket() - Find struct gfs2_glock by lock number
- * @bucket: the bucket to search
- * @name: The lock name
- *
- * Returns: NULL, or the struct gfs2_glock with the requested number
- */
- static struct gfs2_glock *search_bucket(unsigned int hash,
- const struct gfs2_sbd *sdp,
- const struct lm_lockname *name)
- {
- struct gfs2_glock *gl;
- struct hlist_node *h;
- hlist_for_each_entry(gl, h, &gl_hash_table[hash].hb_list, gl_list) {
- if (!lm_name_equal(&gl->gl_name, name))
- continue;
- if (gl->gl_sbd != sdp)
- continue;
- atomic_inc(&gl->gl_ref);
- return gl;
- }
- return NULL;
- }
- /**
- * may_grant - check if its ok to grant a new lock
- * @gl: The glock
- * @gh: The lock request which we wish to grant
- *
- * Returns: true if its ok to grant the lock
- */
- static inline int may_grant(const struct gfs2_glock *gl, const struct gfs2_holder *gh)
- {
- const struct gfs2_holder *gh_head = list_entry(gl->gl_holders.next, const struct gfs2_holder, gh_list);
- if ((gh->gh_state == LM_ST_EXCLUSIVE ||
- gh_head->gh_state == LM_ST_EXCLUSIVE) && gh != gh_head)
- return 0;
- if (gl->gl_state == gh->gh_state)
- return 1;
- if (gh->gh_flags & GL_EXACT)
- return 0;
- if (gl->gl_state == LM_ST_EXCLUSIVE) {
- if (gh->gh_state == LM_ST_SHARED && gh_head->gh_state == LM_ST_SHARED)
- return 1;
- if (gh->gh_state == LM_ST_DEFERRED && gh_head->gh_state == LM_ST_DEFERRED)
- return 1;
- }
- if (gl->gl_state != LM_ST_UNLOCKED && (gh->gh_flags & LM_FLAG_ANY))
- return 1;
- return 0;
- }
- static void gfs2_holder_wake(struct gfs2_holder *gh)
- {
- clear_bit(HIF_WAIT, &gh->gh_iflags);
- smp_mb__after_clear_bit();
- wake_up_bit(&gh->gh_iflags, HIF_WAIT);
- }
- /**
- * do_promote - promote as many requests as possible on the current queue
- * @gl: The glock
- *
- * Returns: 1 if there is a blocked holder at the head of the list, or 2
- * if a type specific operation is underway.
- */
- static int do_promote(struct gfs2_glock *gl)
- __releases(&gl->gl_spin)
- __acquires(&gl->gl_spin)
- {
- const struct gfs2_glock_operations *glops = gl->gl_ops;
- struct gfs2_holder *gh, *tmp;
- int ret;
- restart:
- list_for_each_entry_safe(gh, tmp, &gl->gl_holders, gh_list) {
- if (test_bit(HIF_HOLDER, &gh->gh_iflags))
- continue;
- if (may_grant(gl, gh)) {
- if (gh->gh_list.prev == &gl->gl_holders &&
- glops->go_lock) {
- spin_unlock(&gl->gl_spin);
- /* FIXME: eliminate this eventually */
- ret = glops->go_lock(gh);
- spin_lock(&gl->gl_spin);
- if (ret) {
- if (ret == 1)
- return 2;
- gh->gh_error = ret;
- list_del_init(&gh->gh_list);
- trace_gfs2_glock_queue(gh, 0);
- gfs2_holder_wake(gh);
- goto restart;
- }
- set_bit(HIF_HOLDER, &gh->gh_iflags);
- trace_gfs2_promote(gh, 1);
- gfs2_holder_wake(gh);
- goto restart;
- }
- set_bit(HIF_HOLDER, &gh->gh_iflags);
- trace_gfs2_promote(gh, 0);
- gfs2_holder_wake(gh);
- continue;
- }
- if (gh->gh_list.prev == &gl->gl_holders)
- return 1;
- break;
- }
- return 0;
- }
- /**
- * do_error - Something unexpected has happened during a lock request
- *
- */
- static inline void do_error(struct gfs2_glock *gl, const int ret)
- {
- struct gfs2_holder *gh, *tmp;
- list_for_each_entry_safe(gh, tmp, &gl->gl_holders, gh_list) {
- if (test_bit(HIF_HOLDER, &gh->gh_iflags))
- continue;
- if (ret & LM_OUT_ERROR)
- gh->gh_error = -EIO;
- else if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB))
- gh->gh_error = GLR_TRYFAILED;
- else
- continue;
- list_del_init(&gh->gh_list);
- trace_gfs2_glock_queue(gh, 0);
- gfs2_holder_wake(gh);
- }
- }
- /**
- * find_first_waiter - find the first gh that's waiting for the glock
- * @gl: the glock
- */
- static inline struct gfs2_holder *find_first_waiter(const struct gfs2_glock *gl)
- {
- struct gfs2_holder *gh;
- list_for_each_entry(gh, &gl->gl_holders, gh_list) {
- if (!test_bit(HIF_HOLDER, &gh->gh_iflags))
- return gh;
- }
- return NULL;
- }
- /**
- * state_change - record that the glock is now in a different state
- * @gl: the glock
- * @new_state the new state
- *
- */
- static void state_change(struct gfs2_glock *gl, unsigned int new_state)
- {
- int held1, held2;
- held1 = (gl->gl_state != LM_ST_UNLOCKED);
- held2 = (new_state != LM_ST_UNLOCKED);
- if (held1 != held2) {
- if (held2)
- gfs2_glock_hold(gl);
- else
- gfs2_glock_put(gl);
- }
- gl->gl_state = new_state;
- gl->gl_tchange = jiffies;
- }
- static void gfs2_demote_wake(struct gfs2_glock *gl)
- {
- gl->gl_demote_state = LM_ST_EXCLUSIVE;
- clear_bit(GLF_DEMOTE, &gl->gl_flags);
- smp_mb__after_clear_bit();
- wake_up_bit(&gl->gl_flags, GLF_DEMOTE);
- }
- /**
- * finish_xmote - The DLM has replied to one of our lock requests
- * @gl: The glock
- * @ret: The status from the DLM
- *
- */
- static void finish_xmote(struct gfs2_glock *gl, unsigned int ret)
- {
- const struct gfs2_glock_operations *glops = gl->gl_ops;
- struct gfs2_holder *gh;
- unsigned state = ret & LM_OUT_ST_MASK;
- int rv;
- spin_lock(&gl->gl_spin);
- trace_gfs2_glock_state_change(gl, state);
- state_change(gl, state);
- gh = find_first_waiter(gl);
- /* Demote to UN request arrived during demote to SH or DF */
- if (test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags) &&
- state != LM_ST_UNLOCKED && gl->gl_demote_state == LM_ST_UNLOCKED)
- gl->gl_target = LM_ST_UNLOCKED;
- /* Check for state != intended state */
- if (unlikely(state != gl->gl_target)) {
- if (gh && !test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags)) {
- /* move to back of queue and try next entry */
- if (ret & LM_OUT_CANCELED) {
- if ((gh->gh_flags & LM_FLAG_PRIORITY) == 0)
- list_move_tail(&gh->gh_list, &gl->gl_holders);
- gh = find_first_waiter(gl);
- gl->gl_target = gh->gh_state;
- goto retry;
- }
- /* Some error or failed "try lock" - report it */
- if ((ret & LM_OUT_ERROR) ||
- (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB))) {
- gl->gl_target = gl->gl_state;
- do_error(gl, ret);
- goto out;
- }
- }
- switch(state) {
- /* Unlocked due to conversion deadlock, try again */
- case LM_ST_UNLOCKED:
- retry:
- do_xmote(gl, gh, gl->gl_target);
- break;
- /* Conversion fails, unlock and try again */
- case LM_ST_SHARED:
- case LM_ST_DEFERRED:
- do_xmote(gl, gh, LM_ST_UNLOCKED);
- break;
- default: /* Everything else */
- printk(KERN_ERR "GFS2: wanted %u got %u\n", gl->gl_target, state);
- GLOCK_BUG_ON(gl, 1);
- }
- spin_unlock(&gl->gl_spin);
- gfs2_glock_put(gl);
- return;
- }
- /* Fast path - we got what we asked for */
- if (test_and_clear_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags))
- gfs2_demote_wake(gl);
- if (state != LM_ST_UNLOCKED) {
- if (glops->go_xmote_bh) {
- spin_unlock(&gl->gl_spin);
- rv = glops->go_xmote_bh(gl, gh);
- if (rv == -EAGAIN)
- return;
- spin_lock(&gl->gl_spin);
- if (rv) {
- do_error(gl, rv);
- goto out;
- }
- }
- rv = do_promote(gl);
- if (rv == 2)
- goto out_locked;
- }
- out:
- clear_bit(GLF_LOCK, &gl->gl_flags);
- out_locked:
- spin_unlock(&gl->gl_spin);
- gfs2_glock_put(gl);
- }
- static unsigned int gfs2_lm_lock(struct gfs2_sbd *sdp, void *lock,
- unsigned int req_state,
- unsigned int flags)
- {
- int ret = LM_OUT_ERROR;
- if (!sdp->sd_lockstruct.ls_ops->lm_lock)
- return req_state == LM_ST_UNLOCKED ? 0 : req_state;
- if (likely(!test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
- ret = sdp->sd_lockstruct.ls_ops->lm_lock(lock,
- req_state, flags);
- return ret;
- }
- /**
- * do_xmote - Calls the DLM to change the state of a lock
- * @gl: The lock state
- * @gh: The holder (only for promotes)
- * @target: The target lock state
- *
- */
- static void do_xmote(struct gfs2_glock *gl, struct gfs2_holder *gh, unsigned int target)
- __releases(&gl->gl_spin)
- __acquires(&gl->gl_spin)
- {
- const struct gfs2_glock_operations *glops = gl->gl_ops;
- struct gfs2_sbd *sdp = gl->gl_sbd;
- unsigned int lck_flags = gh ? gh->gh_flags : 0;
- int ret;
- lck_flags &= (LM_FLAG_TRY | LM_FLAG_TRY_1CB | LM_FLAG_NOEXP |
- LM_FLAG_PRIORITY);
- BUG_ON(gl->gl_state == target);
- BUG_ON(gl->gl_state == gl->gl_target);
- if ((target == LM_ST_UNLOCKED || target == LM_ST_DEFERRED) &&
- glops->go_inval) {
- set_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags);
- do_error(gl, 0); /* Fail queued try locks */
- }
- spin_unlock(&gl->gl_spin);
- if (glops->go_xmote_th)
- glops->go_xmote_th(gl);
- if (test_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags))
- glops->go_inval(gl, target == LM_ST_DEFERRED ? 0 : DIO_METADATA);
- clear_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags);
- gfs2_glock_hold(gl);
- if (target != LM_ST_UNLOCKED && (gl->gl_state == LM_ST_SHARED ||
- gl->gl_state == LM_ST_DEFERRED) &&
- !(lck_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)))
- lck_flags |= LM_FLAG_TRY_1CB;
- ret = gfs2_lm_lock(sdp, gl, target, lck_flags);
- if (!(ret & LM_OUT_ASYNC)) {
- finish_xmote(gl, ret);
- gfs2_glock_hold(gl);
- if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
- gfs2_glock_put(gl);
- } else {
- GLOCK_BUG_ON(gl, ret != LM_OUT_ASYNC);
- }
- spin_lock(&gl->gl_spin);
- }
- /**
- * find_first_holder - find the first "holder" gh
- * @gl: the glock
- */
- static inline struct gfs2_holder *find_first_holder(const struct gfs2_glock *gl)
- {
- struct gfs2_holder *gh;
- if (!list_empty(&gl->gl_holders)) {
- gh = list_entry(gl->gl_holders.next, struct gfs2_holder, gh_list);
- if (test_bit(HIF_HOLDER, &gh->gh_iflags))
- return gh;
- }
- return NULL;
- }
- /**
- * run_queue - do all outstanding tasks related to a glock
- * @gl: The glock in question
- * @nonblock: True if we must not block in run_queue
- *
- */
- static void run_queue(struct gfs2_glock *gl, const int nonblock)
- __releases(&gl->gl_spin)
- __acquires(&gl->gl_spin)
- {
- struct gfs2_holder *gh = NULL;
- int ret;
- if (test_and_set_bit(GLF_LOCK, &gl->gl_flags))
- return;
- GLOCK_BUG_ON(gl, test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags));
- if (test_bit(GLF_DEMOTE, &gl->gl_flags) &&
- gl->gl_demote_state != gl->gl_state) {
- if (find_first_holder(gl))
- goto out_unlock;
- if (nonblock)
- goto out_sched;
- set_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags);
- GLOCK_BUG_ON(gl, gl->gl_demote_state == LM_ST_EXCLUSIVE);
- gl->gl_target = gl->gl_demote_state;
- } else {
- if (test_bit(GLF_DEMOTE, &gl->gl_flags))
- gfs2_demote_wake(gl);
- ret = do_promote(gl);
- if (ret == 0)
- goto out_unlock;
- if (ret == 2)
- goto out;
- gh = find_first_waiter(gl);
- gl->gl_target = gh->gh_state;
- if (!(gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)))
- do_error(gl, 0); /* Fail queued try locks */
- }
- do_xmote(gl, gh, gl->gl_target);
- out:
- return;
- out_sched:
- gfs2_glock_hold(gl);
- if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
- gfs2_glock_put(gl);
- out_unlock:
- clear_bit(GLF_LOCK, &gl->gl_flags);
- goto out;
- }
- static void glock_work_func(struct work_struct *work)
- {
- unsigned long delay = 0;
- struct gfs2_glock *gl = container_of(work, struct gfs2_glock, gl_work.work);
- if (test_and_clear_bit(GLF_REPLY_PENDING, &gl->gl_flags))
- finish_xmote(gl, gl->gl_reply);
- down_read(&gfs2_umount_flush_sem);
- spin_lock(&gl->gl_spin);
- if (test_and_clear_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) &&
- gl->gl_state != LM_ST_UNLOCKED &&
- gl->gl_demote_state != LM_ST_EXCLUSIVE) {
- unsigned long holdtime, now = jiffies;
- holdtime = gl->gl_tchange + gl->gl_ops->go_min_hold_time;
- if (time_before(now, holdtime))
- delay = holdtime - now;
- set_bit(delay ? GLF_PENDING_DEMOTE : GLF_DEMOTE, &gl->gl_flags);
- }
- run_queue(gl, 0);
- spin_unlock(&gl->gl_spin);
- up_read(&gfs2_umount_flush_sem);
- if (!delay ||
- queue_delayed_work(glock_workqueue, &gl->gl_work, delay) == 0)
- gfs2_glock_put(gl);
- }
- /**
- * gfs2_glock_get() - Get a glock, or create one if one doesn't exist
- * @sdp: The GFS2 superblock
- * @number: the lock number
- * @glops: The glock_operations to use
- * @create: If 0, don't create the glock if it doesn't exist
- * @glp: the glock is returned here
- *
- * This does not lock a glock, just finds/creates structures for one.
- *
- * Returns: errno
- */
- int gfs2_glock_get(struct gfs2_sbd *sdp, u64 number,
- const struct gfs2_glock_operations *glops, int create,
- struct gfs2_glock **glp)
- {
- struct lm_lockname name = { .ln_number = number, .ln_type = glops->go_type };
- struct gfs2_glock *gl, *tmp;
- unsigned int hash = gl_hash(sdp, &name);
- int error;
- read_lock(gl_lock_addr(hash));
- gl = search_bucket(hash, sdp, &name);
- read_unlock(gl_lock_addr(hash));
- *glp = gl;
- if (gl)
- return 0;
- if (!create)
- return -ENOENT;
- gl = kmem_cache_alloc(gfs2_glock_cachep, GFP_KERNEL);
- if (!gl)
- return -ENOMEM;
- gl->gl_flags = 0;
- gl->gl_name = name;
- atomic_set(&gl->gl_ref, 1);
- gl->gl_state = LM_ST_UNLOCKED;
- gl->gl_target = LM_ST_UNLOCKED;
- gl->gl_demote_state = LM_ST_EXCLUSIVE;
- gl->gl_hash = hash;
- gl->gl_ops = glops;
- snprintf(gl->gl_strname, GDLM_STRNAME_BYTES, "%8x%16llx", name.ln_type, (unsigned long long)number);
- memset(&gl->gl_lksb, 0, sizeof(struct dlm_lksb));
- gl->gl_lksb.sb_lvbptr = gl->gl_lvb;
- gl->gl_tchange = jiffies;
- gl->gl_object = NULL;
- gl->gl_sbd = sdp;
- gl->gl_aspace = NULL;
- INIT_DELAYED_WORK(&gl->gl_work, glock_work_func);
- /* If this glock protects actual on-disk data or metadata blocks,
- create a VFS inode to manage the pages/buffers holding them. */
- if (glops == &gfs2_inode_glops || glops == &gfs2_rgrp_glops) {
- gl->gl_aspace = gfs2_aspace_get(sdp);
- if (!gl->gl_aspace) {
- error = -ENOMEM;
- goto fail;
- }
- }
- write_lock(gl_lock_addr(hash));
- tmp = search_bucket(hash, sdp, &name);
- if (tmp) {
- write_unlock(gl_lock_addr(hash));
- glock_free(gl);
- gl = tmp;
- } else {
- hlist_add_head(&gl->gl_list, &gl_hash_table[hash].hb_list);
- write_unlock(gl_lock_addr(hash));
- }
- *glp = gl;
- return 0;
- fail:
- kmem_cache_free(gfs2_glock_cachep, gl);
- return error;
- }
- /**
- * gfs2_holder_init - initialize a struct gfs2_holder in the default way
- * @gl: the glock
- * @state: the state we're requesting
- * @flags: the modifier flags
- * @gh: the holder structure
- *
- */
- void gfs2_holder_init(struct gfs2_glock *gl, unsigned int state, unsigned flags,
- struct gfs2_holder *gh)
- {
- INIT_LIST_HEAD(&gh->gh_list);
- gh->gh_gl = gl;
- gh->gh_ip = (unsigned long)__builtin_return_address(0);
- gh->gh_owner_pid = get_pid(task_pid(current));
- gh->gh_state = state;
- gh->gh_flags = flags;
- gh->gh_error = 0;
- gh->gh_iflags = 0;
- gfs2_glock_hold(gl);
- }
- /**
- * gfs2_holder_reinit - reinitialize a struct gfs2_holder so we can requeue it
- * @state: the state we're requesting
- * @flags: the modifier flags
- * @gh: the holder structure
- *
- * Don't mess with the glock.
- *
- */
- void gfs2_holder_reinit(unsigned int state, unsigned flags, struct gfs2_holder *gh)
- {
- gh->gh_state = state;
- gh->gh_flags = flags;
- gh->gh_iflags = 0;
- gh->gh_ip = (unsigned long)__builtin_return_address(0);
- }
- /**
- * gfs2_holder_uninit - uninitialize a holder structure (drop glock reference)
- * @gh: the holder structure
- *
- */
- void gfs2_holder_uninit(struct gfs2_holder *gh)
- {
- put_pid(gh->gh_owner_pid);
- gfs2_glock_put(gh->gh_gl);
- gh->gh_gl = NULL;
- gh->gh_ip = 0;
- }
- /**
- * gfs2_glock_holder_wait
- * @word: unused
- *
- * This function and gfs2_glock_demote_wait both show up in the WCHAN
- * field. Thus I've separated these otherwise identical functions in
- * order to be more informative to the user.
- */
- static int gfs2_glock_holder_wait(void *word)
- {
- schedule();
- return 0;
- }
- static int gfs2_glock_demote_wait(void *word)
- {
- schedule();
- return 0;
- }
- static void wait_on_holder(struct gfs2_holder *gh)
- {
- might_sleep();
- wait_on_bit(&gh->gh_iflags, HIF_WAIT, gfs2_glock_holder_wait, TASK_UNINTERRUPTIBLE);
- }
- static void wait_on_demote(struct gfs2_glock *gl)
- {
- might_sleep();
- wait_on_bit(&gl->gl_flags, GLF_DEMOTE, gfs2_glock_demote_wait, TASK_UNINTERRUPTIBLE);
- }
- /**
- * handle_callback - process a demote request
- * @gl: the glock
- * @state: the state the caller wants us to change to
- *
- * There are only two requests that we are going to see in actual
- * practise: LM_ST_SHARED and LM_ST_UNLOCKED
- */
- static void handle_callback(struct gfs2_glock *gl, unsigned int state,
- unsigned long delay)
- {
- int bit = delay ? GLF_PENDING_DEMOTE : GLF_DEMOTE;
- set_bit(bit, &gl->gl_flags);
- if (gl->gl_demote_state == LM_ST_EXCLUSIVE) {
- gl->gl_demote_state = state;
- gl->gl_demote_time = jiffies;
- } else if (gl->gl_demote_state != LM_ST_UNLOCKED &&
- gl->gl_demote_state != state) {
- gl->gl_demote_state = LM_ST_UNLOCKED;
- }
- trace_gfs2_demote_rq(gl);
- }
- /**
- * gfs2_glock_wait - wait on a glock acquisition
- * @gh: the glock holder
- *
- * Returns: 0 on success
- */
- int gfs2_glock_wait(struct gfs2_holder *gh)
- {
- wait_on_holder(gh);
- return gh->gh_error;
- }
- void gfs2_print_dbg(struct seq_file *seq, const char *fmt, ...)
- {
- va_list args;
- va_start(args, fmt);
- if (seq) {
- struct gfs2_glock_iter *gi = seq->private;
- vsprintf(gi->string, fmt, args);
- seq_printf(seq, gi->string);
- } else {
- printk(KERN_ERR " ");
- vprintk(fmt, args);
- }
- va_end(args);
- }
- /**
- * add_to_queue - Add a holder to the wait queue (but look for recursion)
- * @gh: the holder structure to add
- *
- * Eventually we should move the recursive locking trap to a
- * debugging option or something like that. This is the fast
- * path and needs to have the minimum number of distractions.
- *
- */
- static inline void add_to_queue(struct gfs2_holder *gh)
- __releases(&gl->gl_spin)
- __acquires(&gl->gl_spin)
- {
- struct gfs2_glock *gl = gh->gh_gl;
- struct gfs2_sbd *sdp = gl->gl_sbd;
- struct list_head *insert_pt = NULL;
- struct gfs2_holder *gh2;
- int try_lock = 0;
- BUG_ON(gh->gh_owner_pid == NULL);
- if (test_and_set_bit(HIF_WAIT, &gh->gh_iflags))
- BUG();
- if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) {
- if (test_bit(GLF_LOCK, &gl->gl_flags))
- try_lock = 1;
- if (test_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags))
- goto fail;
- }
- list_for_each_entry(gh2, &gl->gl_holders, gh_list) {
- if (unlikely(gh2->gh_owner_pid == gh->gh_owner_pid &&
- (gh->gh_gl->gl_ops->go_type != LM_TYPE_FLOCK)))
- goto trap_recursive;
- if (try_lock &&
- !(gh2->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) &&
- !may_grant(gl, gh)) {
- fail:
- gh->gh_error = GLR_TRYFAILED;
- gfs2_holder_wake(gh);
- return;
- }
- if (test_bit(HIF_HOLDER, &gh2->gh_iflags))
- continue;
- if (unlikely((gh->gh_flags & LM_FLAG_PRIORITY) && !insert_pt))
- insert_pt = &gh2->gh_list;
- }
- if (likely(insert_pt == NULL)) {
- list_add_tail(&gh->gh_list, &gl->gl_holders);
- if (unlikely(gh->gh_flags & LM_FLAG_PRIORITY))
- goto do_cancel;
- return;
- }
- trace_gfs2_glock_queue(gh, 1);
- list_add_tail(&gh->gh_list, insert_pt);
- do_cancel:
- gh = list_entry(gl->gl_holders.next, struct gfs2_holder, gh_list);
- if (!(gh->gh_flags & LM_FLAG_PRIORITY)) {
- spin_unlock(&gl->gl_spin);
- if (sdp->sd_lockstruct.ls_ops->lm_cancel)
- sdp->sd_lockstruct.ls_ops->lm_cancel(gl);
- spin_lock(&gl->gl_spin);
- }
- return;
- trap_recursive:
- print_symbol(KERN_ERR "original: %s\n", gh2->gh_ip);
- printk(KERN_ERR "pid: %d\n", pid_nr(gh2->gh_owner_pid));
- printk(KERN_ERR "lock type: %d req lock state : %d\n",
- gh2->gh_gl->gl_name.ln_type, gh2->gh_state);
- print_symbol(KERN_ERR "new: %s\n", gh->gh_ip);
- printk(KERN_ERR "pid: %d\n", pid_nr(gh->gh_owner_pid));
- printk(KERN_ERR "lock type: %d req lock state : %d\n",
- gh->gh_gl->gl_name.ln_type, gh->gh_state);
- __dump_glock(NULL, gl);
- BUG();
- }
- /**
- * gfs2_glock_nq - enqueue a struct gfs2_holder onto a glock (acquire a glock)
- * @gh: the holder structure
- *
- * if (gh->gh_flags & GL_ASYNC), this never returns an error
- *
- * Returns: 0, GLR_TRYFAILED, or errno on failure
- */
- int gfs2_glock_nq(struct gfs2_holder *gh)
- {
- struct gfs2_glock *gl = gh->gh_gl;
- struct gfs2_sbd *sdp = gl->gl_sbd;
- int error = 0;
- if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
- return -EIO;
- spin_lock(&gl->gl_spin);
- add_to_queue(gh);
- run_queue(gl, 1);
- spin_unlock(&gl->gl_spin);
- if (!(gh->gh_flags & GL_ASYNC))
- error = gfs2_glock_wait(gh);
- return error;
- }
- /**
- * gfs2_glock_poll - poll to see if an async request has been completed
- * @gh: the holder
- *
- * Returns: 1 if the request is ready to be gfs2_glock_wait()ed on
- */
- int gfs2_glock_poll(struct gfs2_holder *gh)
- {
- return test_bit(HIF_WAIT, &gh->gh_iflags) ? 0 : 1;
- }
- /**
- * gfs2_glock_dq - dequeue a struct gfs2_holder from a glock (release a glock)
- * @gh: the glock holder
- *
- */
- void gfs2_glock_dq(struct gfs2_holder *gh)
- {
- struct gfs2_glock *gl = gh->gh_gl;
- const struct gfs2_glock_operations *glops = gl->gl_ops;
- unsigned delay = 0;
- int fast_path = 0;
- spin_lock(&gl->gl_spin);
- if (gh->gh_flags & GL_NOCACHE)
- handle_callback(gl, LM_ST_UNLOCKED, 0);
- list_del_init(&gh->gh_list);
- if (find_first_holder(gl) == NULL) {
- if (glops->go_unlock) {
- GLOCK_BUG_ON(gl, test_and_set_bit(GLF_LOCK, &gl->gl_flags));
- spin_unlock(&gl->gl_spin);
- glops->go_unlock(gh);
- spin_lock(&gl->gl_spin);
- clear_bit(GLF_LOCK, &gl->gl_flags);
- }
- if (list_empty(&gl->gl_holders) &&
- !test_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) &&
- !test_bit(GLF_DEMOTE, &gl->gl_flags))
- fast_path = 1;
- }
- trace_gfs2_glock_queue(gh, 0);
- spin_unlock(&gl->gl_spin);
- if (likely(fast_path))
- return;
- gfs2_glock_hold(gl);
- if (test_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) &&
- !test_bit(GLF_DEMOTE, &gl->gl_flags))
- delay = gl->gl_ops->go_min_hold_time;
- if (queue_delayed_work(glock_workqueue, &gl->gl_work, delay) == 0)
- gfs2_glock_put(gl);
- }
- void gfs2_glock_dq_wait(struct gfs2_holder *gh)
- {
- struct gfs2_glock *gl = gh->gh_gl;
- gfs2_glock_dq(gh);
- wait_on_demote(gl);
- }
- /**
- * gfs2_glock_dq_uninit - dequeue a holder from a glock and initialize it
- * @gh: the holder structure
- *
- */
- void gfs2_glock_dq_uninit(struct gfs2_holder *gh)
- {
- gfs2_glock_dq(gh);
- gfs2_holder_uninit(gh);
- }
- /**
- * gfs2_glock_nq_num - acquire a glock based on lock number
- * @sdp: the filesystem
- * @number: the lock number
- * @glops: the glock operations for the type of glock
- * @state: the state to acquire the glock in
- * @flags: modifier flags for the aquisition
- * @gh: the struct gfs2_holder
- *
- * Returns: errno
- */
- int gfs2_glock_nq_num(struct gfs2_sbd *sdp, u64 number,
- const struct gfs2_glock_operations *glops,
- unsigned int state, int flags, struct gfs2_holder *gh)
- {
- struct gfs2_glock *gl;
- int error;
- error = gfs2_glock_get(sdp, number, glops, CREATE, &gl);
- if (!error) {
- error = gfs2_glock_nq_init(gl, state, flags, gh);
- gfs2_glock_put(gl);
- }
- return error;
- }
- /**
- * glock_compare - Compare two struct gfs2_glock structures for sorting
- * @arg_a: the first structure
- * @arg_b: the second structure
- *
- */
- static int glock_compare(const void *arg_a, const void *arg_b)
- {
- const struct gfs2_holder *gh_a = *(const struct gfs2_holder **)arg_a;
- const struct gfs2_holder *gh_b = *(const struct gfs2_holder **)arg_b;
- const struct lm_lockname *a = &gh_a->gh_gl->gl_name;
- const struct lm_lockname *b = &gh_b->gh_gl->gl_name;
- if (a->ln_number > b->ln_number)
- return 1;
- if (a->ln_number < b->ln_number)
- return -1;
- BUG_ON(gh_a->gh_gl->gl_ops->go_type == gh_b->gh_gl->gl_ops->go_type);
- return 0;
- }
- /**
- * nq_m_sync - synchonously acquire more than one glock in deadlock free order
- * @num_gh: the number of structures
- * @ghs: an array of struct gfs2_holder structures
- *
- * Returns: 0 on success (all glocks acquired),
- * errno on failure (no glocks acquired)
- */
- static int nq_m_sync(unsigned int num_gh, struct gfs2_holder *ghs,
- struct gfs2_holder **p)
- {
- unsigned int x;
- int error = 0;
- for (x = 0; x < num_gh; x++)
- p[x] = &ghs[x];
- sort(p, num_gh, sizeof(struct gfs2_holder *), glock_compare, NULL);
- for (x = 0; x < num_gh; x++) {
- p[x]->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
- error = gfs2_glock_nq(p[x]);
- if (error) {
- while (x--)
- gfs2_glock_dq(p[x]);
- break;
- }
- }
- return error;
- }
- /**
- * gfs2_glock_nq_m - acquire multiple glocks
- * @num_gh: the number of structures
- * @ghs: an array of struct gfs2_holder structures
- *
- *
- * Returns: 0 on success (all glocks acquired),
- * errno on failure (no glocks acquired)
- */
- int gfs2_glock_nq_m(unsigned int num_gh, struct gfs2_holder *ghs)
- {
- struct gfs2_holder *tmp[4];
- struct gfs2_holder **pph = tmp;
- int error = 0;
- switch(num_gh) {
- case 0:
- return 0;
- case 1:
- ghs->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
- return gfs2_glock_nq(ghs);
- default:
- if (num_gh <= 4)
- break;
- pph = kmalloc(num_gh * sizeof(struct gfs2_holder *), GFP_NOFS);
- if (!pph)
- return -ENOMEM;
- }
- error = nq_m_sync(num_gh, ghs, pph);
- if (pph != tmp)
- kfree(pph);
- return error;
- }
- /**
- * gfs2_glock_dq_m - release multiple glocks
- * @num_gh: the number of structures
- * @ghs: an array of struct gfs2_holder structures
- *
- */
- void gfs2_glock_dq_m(unsigned int num_gh, struct gfs2_holder *ghs)
- {
- unsigned int x;
- for (x = 0; x < num_gh; x++)
- gfs2_glock_dq(&ghs[x]);
- }
- /**
- * gfs2_glock_dq_uninit_m - release multiple glocks
- * @num_gh: the number of structures
- * @ghs: an array of struct gfs2_holder structures
- *
- */
- void gfs2_glock_dq_uninit_m(unsigned int num_gh, struct gfs2_holder *ghs)
- {
- unsigned int x;
- for (x = 0; x < num_gh; x++)
- gfs2_glock_dq_uninit(&ghs[x]);
- }
- void gfs2_glock_cb(struct gfs2_glock *gl, unsigned int state)
- {
- unsigned long delay = 0;
- unsigned long holdtime;
- unsigned long now = jiffies;
- gfs2_glock_hold(gl);
- holdtime = gl->gl_tchange + gl->gl_ops->go_min_hold_time;
- if (time_before(now, holdtime))
- delay = holdtime - now;
- if (test_bit(GLF_REPLY_PENDING, &gl->gl_flags))
- delay = gl->gl_ops->go_min_hold_time;
- spin_lock(&gl->gl_spin);
- handle_callback(gl, state, delay);
- spin_unlock(&gl->gl_spin);
- if (queue_delayed_work(glock_workqueue, &gl->gl_work, delay) == 0)
- gfs2_glock_put(gl);
- }
- /**
- * gfs2_glock_complete - Callback used by locking
- * @gl: Pointer to the glock
- * @ret: The return value from the dlm
- *
- */
- void gfs2_glock_complete(struct gfs2_glock *gl, int ret)
- {
- struct lm_lockstruct *ls = &gl->gl_sbd->sd_lockstruct;
- gl->gl_reply = ret;
- if (unlikely(test_bit(DFL_BLOCK_LOCKS, &ls->ls_flags))) {
- struct gfs2_holder *gh;
- spin_lock(&gl->gl_spin);
- gh = find_first_waiter(gl);
- if ((!(gh && (gh->gh_flags & LM_FLAG_NOEXP)) &&
- (gl->gl_target != LM_ST_UNLOCKED)) ||
- ((ret & ~LM_OUT_ST_MASK) != 0))
- set_bit(GLF_FROZEN, &gl->gl_flags);
- spin_unlock(&gl->gl_spin);
- if (test_bit(GLF_FROZEN, &gl->gl_flags))
- return;
- }
- set_bit(GLF_REPLY_PENDING, &gl->gl_flags);
- gfs2_glock_hold(gl);
- if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
- gfs2_glock_put(gl);
- }
- /**
- * demote_ok - Check to see if it's ok to unlock a glock
- * @gl: the glock
- *
- * Returns: 1 if it's ok
- */
- static int demote_ok(const struct gfs2_glock *gl)
- {
- const struct gfs2_glock_operations *glops = gl->gl_ops;
- if (gl->gl_state == LM_ST_UNLOCKED)
- return 0;
- if (!list_empty(&gl->gl_holders))
- return 0;
- if (glops->go_demote_ok)
- return glops->go_demote_ok(gl);
- return 1;
- }
- static int gfs2_shrink_glock_memory(int nr, gfp_t gfp_mask)
- {
- struct gfs2_glock *gl;
- int may_demote;
- int nr_skipped = 0;
- int got_ref = 0;
- LIST_HEAD(skipped);
- if (nr == 0)
- goto out;
- if (!(gfp_mask & __GFP_FS))
- return -1;
- spin_lock(&lru_lock);
- while(nr && !list_empty(&lru_list)) {
- gl = list_entry(lru_list.next, struct gfs2_glock, gl_lru);
- list_del_init(&gl->gl_lru);
- atomic_dec(&lru_count);
- /* Test for being demotable */
- if (!test_and_set_bit(GLF_LOCK, &gl->gl_flags)) {
- gfs2_glock_hold(gl);
- got_ref = 1;
- spin_unlock(&lru_lock);
- spin_lock(&gl->gl_spin);
- may_demote = demote_ok(gl);
- spin_unlock(&gl->gl_spin);
- clear_bit(GLF_LOCK, &gl->gl_flags);
- if (may_demote) {
- handle_callback(gl, LM_ST_UNLOCKED, 0);
- nr--;
- if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
- gfs2_glock_put(gl);
- got_ref = 0;
- }
- spin_lock(&lru_lock);
- if (may_demote)
- continue;
- }
- if (list_empty(&gl->gl_lru) &&
- (atomic_read(&gl->gl_ref) <= (2 + got_ref))) {
- nr_skipped++;
- list_add(&gl->gl_lru, &skipped);
- }
- if (got_ref) {
- spin_unlock(&lru_lock);
- gfs2_glock_put(gl);
- spin_lock(&lru_lock);
- got_ref = 0;
- }
- }
- list_splice(&skipped, &lru_list);
- atomic_add(nr_skipped, &lru_count);
- spin_unlock(&lru_lock);
- out:
- return (atomic_read(&lru_count) / 100) * sysctl_vfs_cache_pressure;
- }
- static struct shrinker glock_shrinker = {
- .shrink = gfs2_shrink_glock_memory,
- .seeks = DEFAULT_SEEKS,
- };
- /**
- * examine_bucket - Call a function for glock in a hash bucket
- * @examiner: the function
- * @sdp: the filesystem
- * @bucket: the bucket
- *
- * Returns: 1 if the bucket has entries
- */
- static int examine_bucket(glock_examiner examiner, struct gfs2_sbd *sdp,
- unsigned int hash)
- {
- struct gfs2_glock *gl, *prev = NULL;
- int has_entries = 0;
- struct hlist_head *head = &gl_hash_table[hash].hb_list;
- read_lock(gl_lock_addr(hash));
- /* Can't use hlist_for_each_entry - don't want prefetch here */
- if (hlist_empty(head))
- goto out;
- gl = list_entry(head->first, struct gfs2_glock, gl_list);
- while(1) {
- if (!sdp || gl->gl_sbd == sdp) {
- gfs2_glock_hold(gl);
- read_unlock(gl_lock_addr(hash));
- if (prev)
- gfs2_glock_put(prev);
- prev = gl;
- examiner(gl);
- has_entries = 1;
- read_lock(gl_lock_addr(hash));
- }
- if (gl->gl_list.next == NULL)
- break;
- gl = list_entry(gl->gl_list.next, struct gfs2_glock, gl_list);
- }
- out:
- read_unlock(gl_lock_addr(hash));
- if (prev)
- gfs2_glock_put(prev);
- cond_resched();
- return has_entries;
- }
- /**
- * thaw_glock - thaw out a glock which has an unprocessed reply waiting
- * @gl: The glock to thaw
- *
- * N.B. When we freeze a glock, we leave a ref to the glock outstanding,
- * so this has to result in the ref count being dropped by one.
- */
- static void thaw_glock(struct gfs2_glock *gl)
- {
- if (!test_and_clear_bit(GLF_FROZEN, &gl->gl_flags))
- return;
- set_bit(GLF_REPLY_PENDING, &gl->gl_flags);
- gfs2_glock_hold(gl);
- if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
- gfs2_glock_put(gl);
- }
- /**
- * clear_glock - look at a glock and see if we can free it from glock cache
- * @gl: the glock to look at
- *
- */
- static void clear_glock(struct gfs2_glock *gl)
- {
- spin_lock(&lru_lock);
- if (!list_empty(&gl->gl_lru)) {
- list_del_init(&gl->gl_lru);
- atomic_dec(&lru_count);
- }
- spin_unlock(&lru_lock);
- spin_lock(&gl->gl_spin);
- if (find_first_holder(gl) == NULL && gl->gl_state != LM_ST_UNLOCKED)
- handle_callback(gl, LM_ST_UNLOCKED, 0);
- spin_unlock(&gl->gl_spin);
- gfs2_glock_hold(gl);
- if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
- gfs2_glock_put(gl);
- }
- /**
- * gfs2_glock_thaw - Thaw any frozen glocks
- * @sdp: The super block
- *
- */
- void gfs2_glock_thaw(struct gfs2_sbd *sdp)
- {
- unsigned x;
- for (x = 0; x < GFS2_GL_HASH_SIZE; x++)
- examine_bucket(thaw_glock, sdp, x);
- }
- /**
- * gfs2_gl_hash_clear - Empty out the glock hash table
- * @sdp: the filesystem
- * @wait: wait until it's all gone
- *
- * Called when unmounting the filesystem.
- */
- void gfs2_gl_hash_clear(struct gfs2_sbd *sdp)
- {
- unsigned long t;
- unsigned int x;
- int cont;
- t = jiffies;
- for (;;) {
- cont = 0;
- for (x = 0; x < GFS2_GL_HASH_SIZE; x++) {
- if (examine_bucket(clear_glock, sdp, x))
- cont = 1;
- }
- if (!cont)
- break;
- if (time_after_eq(jiffies,
- t + gfs2_tune_get(sdp, gt_stall_secs) * HZ)) {
- fs_warn(sdp, "Unmount seems to be stalled. "
- "Dumping lock state...\n");
- gfs2_dump_lockstate(sdp);
- t = jiffies;
- }
- down_write(&gfs2_umount_flush_sem);
- invalidate_inodes(sdp->sd_vfs);
- up_write(&gfs2_umount_flush_sem);
- msleep(10);
- }
- }
- void gfs2_glock_finish_truncate(struct gfs2_inode *ip)
- {
- struct gfs2_glock *gl = ip->i_gl;
- int ret;
- ret = gfs2_truncatei_resume(ip);
- gfs2_assert_withdraw(gl->gl_sbd, ret == 0);
- spin_lock(&gl->gl_spin);
- clear_bit(GLF_LOCK, &gl->gl_flags);
- run_queue(gl, 1);
- spin_unlock(&gl->gl_spin);
- }
- static const char *state2str(unsigned state)
- {
- switch(state) {
- case LM_ST_UNLOCKED:
- return "UN";
- case LM_ST_SHARED:
- return "SH";
- case LM_ST_DEFERRED:
- return "DF";
- case LM_ST_EXCLUSIVE:
- return "EX";
- }
- return "??";
- }
- static const char *hflags2str(char *buf, unsigned flags, unsigned long iflags)
- {
- char *p = buf;
- if (flags & LM_FLAG_TRY)
- *p++ = 't';
- if (flags & LM_FLAG_TRY_1CB)
- *p++ = 'T';
- if (flags & LM_FLAG_NOEXP)
- *p++ = 'e';
- if (flags & LM_FLAG_ANY)
- *p++ = 'A';
- if (flags & LM_FLAG_PRIORITY)
- *p++ = 'p';
- if (flags & GL_ASYNC)
- *p++ = 'a';
- if (flags & GL_EXACT)
- *p++ = 'E';
- if (flags & GL_NOCACHE)
- *p++ = 'c';
- if (test_bit(HIF_HOLDER, &iflags))
- *p++ = 'H';
- if (test_bit(HIF_WAIT, &iflags))
- *p++ = 'W';
- if (test_bit(HIF_FIRST, &iflags))
- *p++ = 'F';
- *p = 0;
- return buf;
- }
- /**
- * dump_holder - print information about a glock holder
- * @seq: the seq_file struct
- * @gh: the glock holder
- *
- * Returns: 0 on success, -ENOBUFS when we run out of space
- */
- static int dump_holder(struct seq_file *seq, const struct gfs2_holder *gh)
- {
- struct task_struct *gh_owner = NULL;
- char buffer[KSYM_SYMBOL_LEN];
- char flags_buf[32];
- sprint_symbol(buffer, gh->gh_ip);
- if (gh->gh_owner_pid)
- gh_owner = pid_task(gh->gh_owner_pid, PIDTYPE_PID);
- gfs2_print_dbg(seq, " H: s:%s f:%s e:%d p:%ld [%s] %s\n",
- state2str(gh->gh_state),
- hflags2str(flags_buf, gh->gh_flags, gh->gh_iflags),
- gh->gh_error,
- gh->gh_owner_pid ? (long)pid_nr(gh->gh_owner_pid) : -1,
- gh_owner ? gh_owner->comm : "(ended)", buffer);
- return 0;
- }
- static const char *gflags2str(char *buf, const unsigned long *gflags)
- {
- char *p = buf;
- if (test_bit(GLF_LOCK, gflags))
- *p++ = 'l';
- if (test_bit(GLF_DEMOTE, gflags))
- *p++ = 'D';
- if (test_bit(GLF_PENDING_DEMOTE, gflags))
- *p++ = 'd';
- if (test_bit(GLF_DEMOTE_IN_PROGRESS, gflags))
- *p++ = 'p';
- if (test_bit(GLF_DIRTY, gflags))
- *p++ = 'y';
- if (test_bit(GLF_LFLUSH, gflags))
- *p++ = 'f';
- if (test_bit(GLF_INVALIDATE_IN_PROGRESS, gflags))
- *p++ = 'i';
- if (test_bit(GLF_REPLY_PENDING, gflags))
- *p++ = 'r';
- if (test_bit(GLF_INITIAL, gflags))
- *p++ = 'I';
- if (test_bit(GLF_FROZEN, gflags))
- *p++ = 'F';
- *p = 0;
- return buf;
- }
- /**
- * __dump_glock - print information about a glock
- * @seq: The seq_file struct
- * @gl: the glock
- *
- * The file format is as follows:
- * One line per object, capital letters are used to indicate objects
- * G = glock, I = Inode, R = rgrp, H = holder. Glocks are not indented,
- * other objects are indented by a single space and follow the glock to
- * which they are related. Fields are indicated by lower case letters
- * followed by a colon and the field value, except for strings which are in
- * [] so that its possible to see if they are composed of spaces for
- * example. The field's are n = number (id of the object), f = flags,
- * t = type, s = state, r = refcount, e = error, p = pid.
- *
- * Returns: 0 on success, -ENOBUFS when we run out of space
- */
- static int __dump_glock(struct seq_file *seq, const struct gfs2_glock *gl)
- {
- const struct gfs2_glock_operations *glops = gl->gl_ops;
- unsigned long long dtime;
- const struct gfs2_holder *gh;
- char gflags_buf[32];
- int error = 0;
- dtime = jiffies - gl->gl_demote_time;
- dtime *= 1000000/HZ; /* demote time in uSec */
- if (!test_bit(GLF_DEMOTE, &gl->gl_flags))
- dtime = 0;
- gfs2_print_dbg(seq, "G: s:%s n:%u/%llu f:%s t:%s d:%s/%llu a:%d r:%d\n",
- state2str(gl->gl_state),
- gl->gl_name.ln_type,
- (unsigned long long)gl->gl_name.ln_number,
- gflags2str(gflags_buf, &gl->gl_flags),
- state2str(gl->gl_target),
- state2str(gl->gl_demote_state), dtime,
- atomic_read(&gl->gl_ail_count),
- atomic_read(&gl->gl_ref));
- list_for_each_entry(gh, &gl->gl_holders, gh_list) {
- error = dump_holder(seq, gh);
- if (error)
- goto out;
- }
- if (gl->gl_state != LM_ST_UNLOCKED && glops->go_dump)
- error = glops->go_dump(seq, gl);
- out:
- return error;
- }
- static int dump_glock(struct seq_file *seq, struct gfs2_glock *gl)
- {
- int ret;
- spin_lock(&gl->gl_spin);
- ret = __dump_glock(seq, gl);
- spin_unlock(&gl->gl_spin);
- return ret;
- }
- /**
- * gfs2_dump_lockstate - print out the current lockstate
- * @sdp: the filesystem
- * @ub: the buffer to copy the information into
- *
- * If @ub is NULL, dump the lockstate to the console.
- *
- */
- static int gfs2_dump_lockstate(struct gfs2_sbd *sdp)
- {
- struct gfs2_glock *gl;
- struct hlist_node *h;
- unsigned int x;
- int error = 0;
- for (x = 0; x < GFS2_GL_HASH_SIZE; x++) {
- read_lock(gl_lock_addr(x));
- hlist_for_each_entry(gl, h, &gl_hash_table[x].hb_list, gl_list) {
- if (gl->gl_sbd != sdp)
- continue;
- error = dump_glock(NULL, gl);
- if (error)
- break;
- }
- read_unlock(gl_lock_addr(x));
- if (error)
- break;
- }
- return error;
- }
- int __init gfs2_glock_init(void)
- {
- unsigned i;
- for(i = 0; i < GFS2_GL_HASH_SIZE; i++) {
- INIT_HLIST_HEAD(&gl_hash_table[i].hb_list);
- }
- #ifdef GL_HASH_LOCK_SZ
- for(i = 0; i < GL_HASH_LOCK_SZ; i++) {
- rwlock_init(&gl_hash_locks[i]);
- }
- #endif
- glock_workqueue = create_workqueue("glock_workqueue");
- if (IS_ERR(glock_workqueue))
- return PTR_ERR(glock_workqueue);
- register_shrinker(&glock_shrinker);
- return 0;
- }
- void gfs2_glock_exit(void)
- {
- unregister_shrinker(&glock_shrinker);
- destroy_workqueue(glock_workqueue);
- }
- static int gfs2_glock_iter_next(struct gfs2_glock_iter *gi)
- {
- struct gfs2_glock *gl;
- restart:
- read_lock(gl_lock_addr(gi->hash));
- gl = gi->gl;
- if (gl) {
- gi->gl = hlist_entry(gl->gl_list.next,
- struct gfs2_glock, gl_list);
- } else {
- gi->gl = hlist_entry(gl_hash_table[gi->hash].hb_list.first,
- struct gfs2_glock, gl_list);
- }
- if (gi->gl)
- gfs2_glock_hold(gi->gl);
- read_unlock(gl_lock_addr(gi->hash));
- if (gl)
- gfs2_glock_put(gl);
- while (gi->gl == NULL) {
- gi->hash++;
- if (gi->hash >= GFS2_GL_HASH_SIZE)
- return 1;
- read_lock(gl_lock_addr(gi->hash));
- gi->gl = hlist_entry(gl_hash_table[gi->hash].hb_list.first,
- struct gfs2_glock, gl_list);
- if (gi->gl)
- gfs2_glock_hold(gi->gl);
- read_unlock(gl_lock_addr(gi->hash));
- }
- if (gi->sdp != gi->gl->gl_sbd)
- goto restart;
- return 0;
- }
- static void gfs2_glock_iter_free(struct gfs2_glock_iter *gi)
- {
- if (gi->gl)
- gfs2_glock_put(gi->gl);
- gi->gl = NULL;
- }
- static void *gfs2_glock_seq_start(struct seq_file *seq, loff_t *pos)
- {
- struct gfs2_glock_iter *gi = seq->private;
- loff_t n = *pos;
- gi->hash = 0;
- do {
- if (gfs2_glock_iter_next(gi)) {
- gfs2_glock_iter_free(gi);
- return NULL;
- }
- } while (n--);
- return gi->gl;
- }
- static void *gfs2_glock_seq_next(struct seq_file *seq, void *iter_ptr,
- loff_t *pos)
- {
- struct gfs2_glock_iter *gi = seq->private;
- (*pos)++;
- if (gfs2_glock_iter_next(gi)) {
- gfs2_glock_iter_free(gi);
- return NULL;
- }
- return gi->gl;
- }
- static void gfs2_glock_seq_stop(struct seq_file *seq, void *iter_ptr)
- {
- struct gfs2_glock_iter *gi = seq->private;
- gfs2_glock_iter_free(gi);
- }
- static int gfs2_glock_seq_show(struct seq_file *seq, void *iter_ptr)
- {
- return dump_glock(seq, iter_ptr);
- }
- static const struct seq_operations gfs2_glock_seq_ops = {
- .start = gfs2_glock_seq_start,
- .next = gfs2_glock_seq_next,
- .stop = gfs2_glock_seq_stop,
- .show = gfs2_glock_seq_show,
- };
- static int gfs2_debugfs_open(struct inode *inode, struct file *file)
- {
- int ret = seq_open_private(file, &gfs2_glock_seq_ops,
- sizeof(struct gfs2_glock_iter));
- if (ret == 0) {
- struct seq_file *seq = file->private_data;
- struct gfs2_glock_iter *gi = seq->private;
- gi->sdp = inode->i_private;
- }
- return ret;
- }
- static const struct file_operations gfs2_debug_fops = {
- .owner = THIS_MODULE,
- .open = gfs2_debugfs_open,
- .read = seq_read,
- .llseek = seq_lseek,
- .release = seq_release_private,
- };
- int gfs2_create_debugfs_file(struct gfs2_sbd *sdp)
- {
- sdp->debugfs_dir = debugfs_create_dir(sdp->sd_table_name, gfs2_root);
- if (!sdp->debugfs_dir)
- return -ENOMEM;
- sdp->debugfs_dentry_glocks = debugfs_create_file("glocks",
- S_IFREG | S_IRUGO,
- sdp->debugfs_dir, sdp,
- &gfs2_debug_fops);
- if (!sdp->debugfs_dentry_glocks)
- return -ENOMEM;
- return 0;
- }
- void gfs2_delete_debugfs_file(struct gfs2_sbd *sdp)
- {
- if (sdp && sdp->debugfs_dir) {
- if (sdp->debugfs_dentry_glocks) {
- debugfs_remove(sdp->debugfs_dentry_glocks);
- sdp->debugfs_dentry_glocks = NULL;
- }
- debugfs_remove(sdp->debugfs_dir);
- sdp->debugfs_dir = NULL;
- }
- }
- int gfs2_register_debugfs(void)
- {
- gfs2_root = debugfs_create_dir("gfs2", NULL);
- return gfs2_root ? 0 : -ENOMEM;
- }
- void gfs2_unregister_debugfs(void)
- {
- debugfs_remove(gfs2_root);
- gfs2_root = NULL;
- }
|