📄 nfs4idmap.c
字号:
strcmp(a->authname, b->authname) == 0);}static intnametoid_show(struct seq_file *m, struct cache_detail *cd, struct cache_head *h){ struct ent *ent; if (h == NULL) { seq_puts(m, "#domain type name [id]\n"); return 0; } ent = container_of(h, struct ent, h); seq_printf(m, "%s %s %s", ent->authname, ent->type == IDMAP_TYPE_GROUP ? "group" : "user", ent->name); if (test_bit(CACHE_VALID, &h->flags)) seq_printf(m, " %u", ent->id); seq_printf(m, "\n"); return 0;}static struct ent *nametoid_lookup(struct ent *);static struct ent *nametoid_update(struct ent *, struct ent *);static int nametoid_parse(struct cache_detail *, char *, int);static struct cache_detail nametoid_cache = { .owner = THIS_MODULE, .hash_size = ENT_HASHMAX, .hash_table = nametoid_table, .name = "nfs4.nametoid", .cache_put = ent_put, .cache_request = nametoid_request, .cache_parse = nametoid_parse, .cache_show = nametoid_show, .warn_no_listener = warn_no_idmapd, .match = nametoid_match, .init = ent_init, .update = ent_init, .alloc = ent_alloc,};static intnametoid_parse(struct cache_detail *cd, char *buf, int buflen){ struct ent ent, *res; char *buf1; int error = -EINVAL; if (buf[buflen - 1] != '\n') return (-EINVAL); buf[buflen - 1]= '\0'; buf1 = kmalloc(PAGE_SIZE, GFP_KERNEL); if (buf1 == NULL) return (-ENOMEM); memset(&ent, 0, sizeof(ent)); /* Authentication name */ if (qword_get(&buf, buf1, PAGE_SIZE) <= 0) goto out; memcpy(ent.authname, buf1, sizeof(ent.authname)); /* Type */ if (qword_get(&buf, buf1, PAGE_SIZE) <= 0) goto out; ent.type = strcmp(buf1, "user") == 0 ? IDMAP_TYPE_USER : IDMAP_TYPE_GROUP; /* Name */ error = qword_get(&buf, buf1, PAGE_SIZE); if (error <= 0 || error >= IDMAP_NAMESZ) goto out; memcpy(ent.name, buf1, sizeof(ent.name)); /* expiry */ ent.h.expiry_time = get_expiry(&buf); if (ent.h.expiry_time == 0) goto out; /* ID */ error = get_int(&buf, &ent.id); if (error == -EINVAL) goto out; if (error == -ENOENT) set_bit(CACHE_NEGATIVE, &ent.h.flags); error = -ENOMEM; res = nametoid_lookup(&ent); if (res == NULL) goto out; res = nametoid_update(&ent, res); if (res == NULL) goto out; cache_put(&res->h, &nametoid_cache); error = 0;out: kfree(buf1); return (error);}static struct ent *nametoid_lookup(struct ent *item){ struct cache_head *ch = sunrpc_cache_lookup(&nametoid_cache, &item->h, nametoid_hash(item)); if (ch) return container_of(ch, struct ent, h); else return NULL;}static struct ent *nametoid_update(struct ent *new, struct ent *old){ struct cache_head *ch = sunrpc_cache_update(&nametoid_cache, &new->h, &old->h, nametoid_hash(new)); if (ch) return container_of(ch, struct ent, h); else return NULL;}/* * Exported API */voidnfsd_idmap_init(void){ cache_register(&idtoname_cache); cache_register(&nametoid_cache);}voidnfsd_idmap_shutdown(void){ if (cache_unregister(&idtoname_cache)) printk(KERN_ERR "nfsd: failed to unregister idtoname cache\n"); if (cache_unregister(&nametoid_cache)) printk(KERN_ERR "nfsd: failed to unregister nametoid cache\n");}/* * Deferred request handling */struct idmap_defer_req { struct cache_req req; struct cache_deferred_req deferred_req; wait_queue_head_t waitq; atomic_t count;};static inline voidput_mdr(struct idmap_defer_req *mdr){ if (atomic_dec_and_test(&mdr->count)) kfree(mdr);}static inline voidget_mdr(struct idmap_defer_req *mdr){ atomic_inc(&mdr->count);}static voididmap_revisit(struct cache_deferred_req *dreq, int toomany){ struct idmap_defer_req *mdr = container_of(dreq, struct idmap_defer_req, deferred_req); wake_up(&mdr->waitq); put_mdr(mdr);}static struct cache_deferred_req *idmap_defer(struct cache_req *req){ struct idmap_defer_req *mdr = container_of(req, struct idmap_defer_req, req); mdr->deferred_req.revisit = idmap_revisit; get_mdr(mdr); return (&mdr->deferred_req);}static inline intdo_idmap_lookup(struct ent *(*lookup_fn)(struct ent *), struct ent *key, struct cache_detail *detail, struct ent **item, struct idmap_defer_req *mdr){ *item = lookup_fn(key); if (!*item) return -ENOMEM; return cache_check(detail, &(*item)->h, &mdr->req);}static inline intdo_idmap_lookup_nowait(struct ent *(*lookup_fn)(struct ent *), struct ent *key, struct cache_detail *detail, struct ent **item){ int ret = -ENOMEM; *item = lookup_fn(key); if (!*item) goto out_err; ret = -ETIMEDOUT; if (!test_bit(CACHE_VALID, &(*item)->h.flags) || (*item)->h.expiry_time < get_seconds() || detail->flush_time > (*item)->h.last_refresh) goto out_put; ret = -ENOENT; if (test_bit(CACHE_NEGATIVE, &(*item)->h.flags)) goto out_put; return 0;out_put: cache_put(&(*item)->h, detail);out_err: *item = NULL; return ret;}static intidmap_lookup(struct svc_rqst *rqstp, struct ent *(*lookup_fn)(struct ent *), struct ent *key, struct cache_detail *detail, struct ent **item){ struct idmap_defer_req *mdr; int ret; mdr = kzalloc(sizeof(*mdr), GFP_KERNEL); if (!mdr) return -ENOMEM; atomic_set(&mdr->count, 1); init_waitqueue_head(&mdr->waitq); mdr->req.defer = idmap_defer; ret = do_idmap_lookup(lookup_fn, key, detail, item, mdr); if (ret == -EAGAIN) { wait_event_interruptible_timeout(mdr->waitq, test_bit(CACHE_VALID, &(*item)->h.flags), 1 * HZ); ret = do_idmap_lookup_nowait(lookup_fn, key, detail, item); } put_mdr(mdr); return ret;}static char *rqst_authname(struct svc_rqst *rqstp){ struct auth_domain *clp; clp = rqstp->rq_gssclient ? rqstp->rq_gssclient : rqstp->rq_client; return clp->name;}static intidmap_name_to_id(struct svc_rqst *rqstp, int type, const char *name, u32 namelen, uid_t *id){ struct ent *item, key = { .type = type, }; int ret; if (namelen + 1 > sizeof(key.name)) return -EINVAL; memcpy(key.name, name, namelen); key.name[namelen] = '\0'; strlcpy(key.authname, rqst_authname(rqstp), sizeof(key.authname)); ret = idmap_lookup(rqstp, nametoid_lookup, &key, &nametoid_cache, &item); if (ret == -ENOENT) ret = -ESRCH; /* nfserr_badname */ if (ret) return ret; *id = item->id; cache_put(&item->h, &nametoid_cache); return 0;}static intidmap_id_to_name(struct svc_rqst *rqstp, int type, uid_t id, char *name){ struct ent *item, key = { .id = id, .type = type, }; int ret; strlcpy(key.authname, rqst_authname(rqstp), sizeof(key.authname)); ret = idmap_lookup(rqstp, idtoname_lookup, &key, &idtoname_cache, &item); if (ret == -ENOENT) return sprintf(name, "%u", id); if (ret) return ret; ret = strlen(item->name); BUG_ON(ret > IDMAP_NAMESZ); memcpy(name, item->name, ret); cache_put(&item->h, &idtoname_cache); return ret;}intnfsd_map_name_to_uid(struct svc_rqst *rqstp, const char *name, size_t namelen, __u32 *id){ return idmap_name_to_id(rqstp, IDMAP_TYPE_USER, name, namelen, id);}intnfsd_map_name_to_gid(struct svc_rqst *rqstp, const char *name, size_t namelen, __u32 *id){ return idmap_name_to_id(rqstp, IDMAP_TYPE_GROUP, name, namelen, id);}intnfsd_map_uid_to_name(struct svc_rqst *rqstp, __u32 id, char *name){ return idmap_id_to_name(rqstp, IDMAP_TYPE_USER, id, name);}intnfsd_map_gid_to_name(struct svc_rqst *rqstp, __u32 id, char *name){ return idmap_id_to_name(rqstp, IDMAP_TYPE_GROUP, id, name);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -