struct list_head capa_list[3];
static int capa_count[3] = { 0 };
+static char *capa_type_name[] = { "client", "mds", "filter" };
+
/* TODO: mdc and llite all need this, so define it here.
* in the future it will be moved to ll_sb_info to support multi-
* mount point */
struct timer_list ll_capa_timer;
+atomic_t ll_capa_stat = ATOMIC_INIT(0);
EXPORT_SYMBOL(capa_lock);
EXPORT_SYMBOL(capa_hash);
EXPORT_SYMBOL(capa_list);
EXPORT_SYMBOL(ll_capa_timer);
+EXPORT_SYMBOL(ll_capa_stat);
static inline int const
-capa_hashfn(unsigned int uid, int capa_op, __u64 mdsid, unsigned long ino)
+capa_hashfn(unsigned int uid, __u64 mdsid, unsigned long ino)
{
- return (ino ^ uid) * (unsigned long)capa_op * (unsigned long)mdsid %
- NR_CAPAHASH;
+ return (ino ^ uid) * (unsigned long)(mdsid + 1) % NR_CAPAHASH;
}
int capa_op(int flags)
{
if (flags & (FMODE_WRITE|MDS_OPEN_TRUNC))
- return MAY_WRITE;
+ return CAPA_WRITE;
else if (flags & FMODE_READ)
- return MAY_READ;
+ return CAPA_READ;
LBUG(); /* should be either MAY_READ or MAY_WRITE */
return 0;
static struct obd_capa *
find_capa(struct hlist_head *head, uid_t uid, int capa_op, __u64 mdsid,
- unsigned long ino, int type)
+ unsigned long ino, __u32 igen, int type)
{
struct hlist_node *pos;
struct obd_capa *ocapa;
uid_t ouid;
- CDEBUG(D_CACHE, "find_capa uid %u op %u mdsid "LPU64" ino %lu "
- "type %d\n", uid, capa_op, mdsid, ino, type);
-
hlist_for_each_entry(ocapa, pos, head, c_hash) {
if (ocapa->c_capa.lc_ino != ino)
continue;
+ if (ocapa->c_capa.lc_igen != igen)
+ continue;
if (ocapa->c_capa.lc_mdsid != mdsid)
continue;
- if (ocapa->c_capa.lc_op != capa_op)
+ if ((ocapa->c_capa.lc_op & capa_op) != ocapa->c_capa.lc_op)
continue;
if (ocapa->c_type != type)
continue;
- if (ocapa->c_type == CLIENT_CAPA &&
- ocapa->c_capa.lc_flags & CAPA_FL_REMUID)
+ if (ocapa->c_type == CLIENT_CAPA)
ouid = ocapa->c_capa.lc_ruid;
else
ouid = ocapa->c_capa.lc_uid;
if (ouid != uid)
continue;
+ DEBUG_CAPA(D_INODE, &ocapa->c_capa, "found %s",
+ capa_type_name[ocapa->c_type]);
+
return ocapa;
}
return NULL;
}
+static struct obd_capa *
+filter_find_capa(struct hlist_head *head, struct lustre_capa *capa)
+{
+ struct hlist_node *pos;
+ struct obd_capa *ocapa;
+
+ hlist_for_each_entry(ocapa, pos, head, c_hash) {
+ if (ocapa->c_type != FILTER_CAPA)
+ continue;
+
+ if (!memcmp(&ocapa->c_capa, capa,
+ sizeof(struct lustre_capa_data))) {
+ DEBUG_CAPA(D_INODE, &ocapa->c_capa, "found %s",
+ capa_type_name[ocapa->c_type]);
+ return ocapa;
+ }
+ }
+
+ return NULL;
+}
+
inline void __capa_get(struct obd_capa *ocapa)
{
- atomic_inc(&ocapa->c_refc);
+ if (ocapa->c_type != CLIENT_CAPA)
+ atomic_inc(&ocapa->c_refc);
}
static struct obd_capa *
find_capa_locked(struct hlist_head *head, uid_t uid, int capa_op, __u64 mdsid,
- unsigned long ino, int type)
+ unsigned long ino, __u32 igen, int type)
{
struct obd_capa *ocapa;
- ENTRY;
spin_lock(&capa_lock);
- ocapa = find_capa(head, uid, capa_op, mdsid, ino, type);
+ ocapa = find_capa(head, uid, capa_op, mdsid, ino, igen, type);
if (ocapa)
__capa_get(ocapa);
spin_unlock(&capa_lock);
- RETURN(ocapa);
+ return ocapa;
}
static struct obd_capa *alloc_capa(void)
return ocapa;
}
+static void __capa_put(struct obd_capa *ocapa)
+{
+ hlist_del_init(&ocapa->c_hash);
+ list_del_init(&ocapa->c_list);
+ capa_count[ocapa->c_type]--;
+}
+
static void destroy_capa(struct obd_capa *ocapa)
{
OBD_SLAB_FREE(ocapa, capa_cachep, sizeof(*ocapa));
void capa_cache_cleanup(void)
{
- struct obd_capa *ocapa;
- struct hlist_node *pos, *n;
+ struct obd_capa *ocapa, *tmp;
+ int i;
- hlist_for_each_entry_safe(ocapa, pos, n, capa_hash, c_hash) {
- LASSERT(ocapa->c_type != CLIENT_CAPA);
- hlist_del(&ocapa->c_hash);
- list_del(&ocapa->c_list);
- OBD_FREE(ocapa, sizeof(*ocapa));
+ for (i = MDS_CAPA; i <= FILTER_CAPA; i++) {
+ list_for_each_entry_safe(ocapa, tmp, &capa_list[i], c_list) {
+ __capa_put(ocapa);
+ destroy_capa(ocapa);
+ }
}
OBD_FREE(capa_hash, PAGE_SIZE);
return;
}
}
+ list_add(&ocapa->c_list, head);
+ return;
}
list_add_tail(&ocapa->c_list, head);
}
static struct obd_capa *
-get_new_capa_locked(struct hlist_head *head, int type, struct lustre_capa *capa,
- struct inode *inode, struct lustre_handle *handle)
+get_new_capa_locked(struct hlist_head *head, int type, struct lustre_capa *capa)
{
uid_t uid = capa->lc_uid;
int capa_op = capa->lc_op;
__u64 mdsid = capa->lc_mdsid;
unsigned long ino = capa->lc_ino;
struct obd_capa *ocapa, *old;
- ENTRY;
ocapa = alloc_capa();
if (!ocapa)
- RETURN(NULL);
+ return NULL;
spin_lock(&capa_lock);
- old = find_capa(head, uid, capa_op, mdsid, ino, type);
+
+ if (type == FILTER_CAPA)
+ old = filter_find_capa(head, capa);
+ else
+ old = find_capa(head, uid, capa_op, mdsid, ino,
+ capa->lc_igen, type);
+
if (!old) {
do_update_capa(ocapa, capa);
ocapa->c_type = type;
-
- if (type == CLIENT_CAPA) {
- LASSERT(inode);
- LASSERT(handle);
-#ifdef __KERNEL__
- igrab(inode);
-#endif
- ocapa->c_inode = inode;
- memcpy(&ocapa->c_handle, handle, sizeof(*handle));
- }
-
list_add_capa(ocapa, &capa_list[type]);
- hlist_add_head(&ocapa->c_hash, capa_hash);
+ hlist_add_head(&ocapa->c_hash, head);
+ if (type == CLIENT_CAPA)
+ INIT_LIST_HEAD(&ocapa->c_lli_list);
+ __capa_get(ocapa);
+
capa_count[type]++;
- __capa_get(ocapa);
+ DEBUG_CAPA(D_INODE, &ocapa->c_capa, "new %s",
+ capa_type_name[type]);
if (type != CLIENT_CAPA && capa_count[type] > CAPA_CACHE_SIZE) {
struct list_head *node = capa_list[type].next;
/* free 12 unused capa from head */
while (node->next != &capa_list[type] && count < 12) {
- tcapa = list_entry(node, struct obd_capa, c_list);
+ tcapa = list_entry(node, struct obd_capa,
+ c_list);
node = node->next;
if (atomic_read(&tcapa->c_refc) > 0)
continue;
- list_del(&tcapa->c_list);
- hlist_del(&tcapa->c_hash);
+ DEBUG_CAPA(D_INODE, &tcapa->c_capa,
+ "free unused %s",
+ capa_type_name[type]);
+ __capa_put(tcapa);
destroy_capa(tcapa);
- capa_count[type]--;
count++;
}
}
spin_unlock(&capa_lock);
- RETURN(ocapa);
+ return ocapa;
}
-
- __capa_get(old);
spin_unlock(&capa_lock);
destroy_capa(ocapa);
- RETURN(old);
+ return old;
}
-static struct obd_capa *
-capa_get_locked(uid_t uid, int capa_op,__u64 mdsid, unsigned long ino,
- int type, struct lustre_capa *capa, struct inode *inode,
- struct lustre_handle *handle)
+struct obd_capa *
+capa_get(uid_t uid, int capa_op,__u64 mdsid, unsigned long ino,
+ __u32 igen, int type)
{
- struct hlist_head *head = capa_hash +
- capa_hashfn(uid, capa_op, mdsid, ino);
+ struct hlist_head *head = capa_hash + capa_hashfn(uid, mdsid, ino);
struct obd_capa *ocapa;
- ENTRY;
- ocapa = find_capa_locked(head, uid, capa_op, mdsid, ino, type);
- if (ocapa)
- RETURN(ocapa);
+ ocapa = find_capa_locked(head, uid, capa_op, mdsid, ino, igen, type);
- if (capa)
- ocapa = get_new_capa_locked(head, type, capa, inode, handle);
- RETURN(ocapa);
-}
+ if (type == CLIENT_CAPA && !ocapa && atomic_read(&ll_capa_stat)) {
+ CDEBUG(D_ERROR, "can't find capa for (uid %u, op %d, mdsid "
+ LPU64", ino %lu igen %u, type %d)\n",
+ (unsigned) uid, capa_op, mdsid, ino, igen, type);
+ atomic_set(&ll_capa_stat, 0);
+ }
-struct obd_capa *
-capa_get(uid_t uid, int capa_op, __u64 mdsid, unsigned long ino, int type,
- struct lustre_capa *capa, struct inode *inode,
- struct lustre_handle *handle)
-{
- return capa_get_locked(uid, capa_op, mdsid, ino, type, capa, inode,
- handle);
+ return ocapa;
}
-static void __capa_put(struct obd_capa *ocapa, int type)
+struct obd_capa * filter_capa_get(struct lustre_capa *capa)
{
- hlist_del_init(&ocapa->c_hash);
- list_del_init(&ocapa->c_list);
- capa_count[type]--;
+ struct hlist_head *head = capa_hash +
+ capa_hashfn(capa->lc_uid, capa->lc_mdsid, capa->lc_ino);
+ struct obd_capa *ocapa;
+
+ spin_lock(&capa_lock);
+ ocapa = filter_find_capa(head, capa);
+ if (ocapa)
+ __capa_get(ocapa);
+ spin_unlock(&capa_lock);
+ return ocapa;
}
-void capa_put(struct obd_capa *ocapa, int type)
+void capa_put_nolock(struct obd_capa *ocapa)
{
- ENTRY;
+ DEBUG_CAPA(D_INODE, &ocapa->c_capa, "put %s",
+ capa_type_name[ocapa->c_type]);
- if (ocapa) {
- if (atomic_dec_and_lock(&ocapa->c_refc, &capa_lock)) {
- if (type == CLIENT_CAPA) {
-#ifdef __KERNEL__
- iput(ocapa->c_inode);
-#endif
- __capa_put(ocapa, type);
- destroy_capa(ocapa);
- }
- spin_unlock(&capa_lock);
- }
+ if (ocapa->c_type == CLIENT_CAPA) {
+ list_del_init(&ocapa->c_lli_list);
+ __capa_put(ocapa);
+ destroy_capa(ocapa);
+ } else {
+ atomic_dec(&ocapa->c_refc);
}
+}
- EXIT;
+void capa_put(struct obd_capa *ocapa)
+{
+ if (!ocapa)
+ return;
+
+ spin_lock(&capa_lock);
+ capa_put_nolock(ocapa);
+ spin_unlock(&capa_lock);
}
-static int update_capa_locked(struct lustre_capa *capa, int type)
+struct obd_capa *capa_renew(struct lustre_capa *capa, int type)
{
uid_t uid = capa->lc_uid;
int capa_op = capa->lc_op;
__u64 mdsid = capa->lc_mdsid;
unsigned long ino = capa->lc_ino;
struct hlist_head *head = capa_hash +
- capa_hashfn(uid, capa_op, mdsid, ino);
+ capa_hashfn(uid, mdsid, ino);
struct obd_capa *ocapa;
- ENTRY;
spin_lock(&capa_lock);
- ocapa = find_capa(head, uid, capa_op, mdsid, ino, type);
- if (ocapa)
+
+ if (type == FILTER_CAPA)
+ ocapa = filter_find_capa(head, capa);
+ else
+ ocapa = find_capa(head, uid, capa_op, mdsid, ino,
+ capa->lc_igen, type);
+ if (ocapa) {
+ DEBUG_CAPA(D_INFO, capa, "renew %s", capa_type_name[type]);
do_update_capa(ocapa, capa);
+ }
+
spin_unlock(&capa_lock);
- if (ocapa == NULL && type == MDS_CAPA) {
- ocapa = get_new_capa_locked(head, type, capa, NULL, NULL);
- capa_put(ocapa, type);
- }
+ if (!ocapa)
+ ocapa = get_new_capa_locked(head, type, capa);
- RETURN(ocapa ? 0 : -ENOENT);
-}
+ if (type == CLIENT_CAPA)
+ atomic_set(&ll_capa_stat, 1);
-int capa_renew(struct lustre_capa *capa, int type)
-{
- return update_capa_locked(capa, type);
+ return ocapa;
}
-void capa_hmac(struct crypto_tfm *tfm, __u8 *key, struct lustre_capa *capa)
+void capa_hmac(__u8 *key, struct lustre_capa *capa)
{
+ struct crypto_tfm *tfm;
int keylen = CAPA_KEY_LEN;
struct scatterlist sl = {
.page = virt_to_page(capa),
.length = sizeof(struct lustre_capa_data),
};
+ tfm = crypto_alloc_tfm(CAPA_HMAC_ALG, 0);
LASSERT(tfm);
crypto_hmac(tfm, key, &keylen, &sl, 1, capa->lc_hmac);
+ crypto_free_tfm(tfm);
}
void capa_dup(void *dst, struct obd_capa *ocapa)
struct timeval tv;
do_gettimeofday(&tv);
- return (capa->lc_expiry < tv.tv_sec) ? 1 : 0;
+ return ((unsigned long )capa->lc_expiry <= tv.tv_sec) ? 1 : 0;
}
-int __capa_is_to_expire(struct obd_capa *ocapa)
+int __capa_is_to_expire(struct obd_capa *ocapa, struct timeval *tv)
{
- struct timeval tv;
int pre_expiry = capa_pre_expiry(&ocapa->c_capa);
- do_gettimeofday(&tv);
- return (ocapa->c_capa.lc_expiry - pre_expiry - 1 <= tv.tv_sec)? 1 : 0;
+ /* XXX: in case the clock is inaccurate, minus one more
+ * pre_expiry to make sure the expiry won't miss */
+ return ((unsigned long)ocapa->c_capa.lc_expiry -
+ 2 * pre_expiry <= tv->tv_sec)? 1 : 0;
}
int capa_is_to_expire(struct obd_capa *ocapa)
{
+ struct timeval tv;
int rc;
+ do_gettimeofday(&tv);
spin_lock(&capa_lock);
- rc = __capa_is_to_expire(ocapa);
+ rc = __capa_is_to_expire(ocapa, &tv);
spin_unlock(&capa_lock);
return rc;
}
+void dump_capa_hmac(char *buf, char *key)
+{
+ int i, n = 0;
+
+ for (i = 0; i < CAPA_DIGEST_SIZE; i++)
+ n += sprintf(buf + n, "%02x", (unsigned char) key[i]);
+}
+
+
EXPORT_SYMBOL(capa_op);
EXPORT_SYMBOL(capa_get);
+EXPORT_SYMBOL(__capa_get);
+EXPORT_SYMBOL(filter_capa_get);
EXPORT_SYMBOL(capa_put);
+EXPORT_SYMBOL(capa_put_nolock);
EXPORT_SYMBOL(capa_renew);
-EXPORT_SYMBOL(__capa_get);
EXPORT_SYMBOL(capa_hmac);
EXPORT_SYMBOL(capa_dup);
EXPORT_SYMBOL(capa_dup2);
EXPORT_SYMBOL(capa_expired);
EXPORT_SYMBOL(__capa_is_to_expire);
EXPORT_SYMBOL(capa_is_to_expire);
+EXPORT_SYMBOL(dump_capa_hmac);
+