Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * fs/nfsd/nfs4idmap.c |
| 3 | * |
| 4 | * Mapping of UID/GIDs to name and vice versa. |
| 5 | * |
| 6 | * Copyright (c) 2002, 2003 The Regents of the University of |
| 7 | * Michigan. All rights reserved. |
| 8 | * |
| 9 | * Marius Aamodt Eriksen <marius@umich.edu> |
| 10 | * |
| 11 | * Redistribution and use in source and binary forms, with or without |
| 12 | * modification, are permitted provided that the following conditions |
| 13 | * are met: |
| 14 | * |
| 15 | * 1. Redistributions of source code must retain the above copyright |
| 16 | * notice, this list of conditions and the following disclaimer. |
| 17 | * 2. Redistributions in binary form must reproduce the above copyright |
| 18 | * notice, this list of conditions and the following disclaimer in the |
| 19 | * documentation and/or other materials provided with the distribution. |
| 20 | * 3. Neither the name of the University nor the names of its |
| 21 | * contributors may be used to endorse or promote products derived |
| 22 | * from this software without specific prior written permission. |
| 23 | * |
| 24 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 25 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 26 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 27 | * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 29 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 30 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 31 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 32 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 33 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 34 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 35 | */ |
| 36 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include <linux/module.h> |
| 38 | #include <linux/init.h> |
| 39 | |
| 40 | #include <linux/mm.h> |
| 41 | #include <linux/utsname.h> |
| 42 | #include <linux/errno.h> |
| 43 | #include <linux/string.h> |
| 44 | #include <linux/sunrpc/clnt.h> |
| 45 | #include <linux/nfs.h> |
| 46 | #include <linux/nfs4.h> |
| 47 | #include <linux/nfs_fs.h> |
| 48 | #include <linux/nfs_page.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | #include <linux/sunrpc/cache.h> |
| 50 | #include <linux/nfsd_idmap.h> |
| 51 | #include <linux/list.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | #include <linux/time.h> |
| 53 | #include <linux/seq_file.h> |
| 54 | #include <linux/sunrpc/svcauth.h> |
| 55 | |
| 56 | /* |
| 57 | * Cache entry |
| 58 | */ |
| 59 | |
| 60 | /* |
| 61 | * XXX we know that IDMAP_NAMESZ < PAGE_SIZE, but it's ugly to rely on |
| 62 | * that. |
| 63 | */ |
| 64 | |
| 65 | #define IDMAP_TYPE_USER 0 |
| 66 | #define IDMAP_TYPE_GROUP 1 |
| 67 | |
| 68 | struct ent { |
| 69 | struct cache_head h; |
| 70 | int type; /* User / Group */ |
| 71 | uid_t id; |
| 72 | char name[IDMAP_NAMESZ]; |
| 73 | char authname[IDMAP_NAMESZ]; |
| 74 | }; |
| 75 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | /* Common entry handling */ |
| 77 | |
| 78 | #define ENT_HASHBITS 8 |
| 79 | #define ENT_HASHMAX (1 << ENT_HASHBITS) |
| 80 | #define ENT_HASHMASK (ENT_HASHMAX - 1) |
| 81 | |
NeilBrown | f9ecc92 | 2006-03-27 01:15:06 -0800 | [diff] [blame] | 82 | static void |
| 83 | ent_init(struct cache_head *cnew, struct cache_head *citm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | { |
NeilBrown | f9ecc92 | 2006-03-27 01:15:06 -0800 | [diff] [blame] | 85 | struct ent *new = container_of(cnew, struct ent, h); |
| 86 | struct ent *itm = container_of(citm, struct ent, h); |
| 87 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | new->id = itm->id; |
| 89 | new->type = itm->type; |
| 90 | |
| 91 | strlcpy(new->name, itm->name, sizeof(new->name)); |
| 92 | strlcpy(new->authname, itm->authname, sizeof(new->name)); |
| 93 | } |
| 94 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 95 | static void |
NeilBrown | baab935 | 2006-03-27 01:15:09 -0800 | [diff] [blame] | 96 | ent_put(struct kref *ref) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | { |
NeilBrown | baab935 | 2006-03-27 01:15:09 -0800 | [diff] [blame] | 98 | struct ent *map = container_of(ref, struct ent, h.ref); |
| 99 | kfree(map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | } |
| 101 | |
NeilBrown | f9ecc92 | 2006-03-27 01:15:06 -0800 | [diff] [blame] | 102 | static struct cache_head * |
| 103 | ent_alloc(void) |
| 104 | { |
| 105 | struct ent *e = kmalloc(sizeof(*e), GFP_KERNEL); |
| 106 | if (e) |
| 107 | return &e->h; |
| 108 | else |
| 109 | return NULL; |
| 110 | } |
| 111 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | /* |
| 113 | * ID -> Name cache |
| 114 | */ |
| 115 | |
| 116 | static struct cache_head *idtoname_table[ENT_HASHMAX]; |
| 117 | |
| 118 | static uint32_t |
| 119 | idtoname_hash(struct ent *ent) |
| 120 | { |
| 121 | uint32_t hash; |
| 122 | |
| 123 | hash = hash_str(ent->authname, ENT_HASHBITS); |
| 124 | hash = hash_long(hash ^ ent->id, ENT_HASHBITS); |
| 125 | |
| 126 | /* Flip LSB for user/group */ |
| 127 | if (ent->type == IDMAP_TYPE_GROUP) |
| 128 | hash ^= 1; |
| 129 | |
| 130 | return hash; |
| 131 | } |
| 132 | |
| 133 | static void |
| 134 | idtoname_request(struct cache_detail *cd, struct cache_head *ch, char **bpp, |
| 135 | int *blen) |
| 136 | { |
| 137 | struct ent *ent = container_of(ch, struct ent, h); |
| 138 | char idstr[11]; |
| 139 | |
| 140 | qword_add(bpp, blen, ent->authname); |
J. Bruce Fields | 0a725fc | 2007-07-31 00:37:52 -0700 | [diff] [blame] | 141 | snprintf(idstr, sizeof(idstr), "%u", ent->id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | qword_add(bpp, blen, ent->type == IDMAP_TYPE_GROUP ? "group" : "user"); |
| 143 | qword_add(bpp, blen, idstr); |
| 144 | |
| 145 | (*bpp)[-1] = '\n'; |
| 146 | } |
| 147 | |
NeilBrown | f9ecc92 | 2006-03-27 01:15:06 -0800 | [diff] [blame] | 148 | static int |
| 149 | idtoname_match(struct cache_head *ca, struct cache_head *cb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | { |
NeilBrown | f9ecc92 | 2006-03-27 01:15:06 -0800 | [diff] [blame] | 151 | struct ent *a = container_of(ca, struct ent, h); |
| 152 | struct ent *b = container_of(cb, struct ent, h); |
| 153 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | return (a->id == b->id && a->type == b->type && |
| 155 | strcmp(a->authname, b->authname) == 0); |
| 156 | } |
| 157 | |
| 158 | static int |
| 159 | idtoname_show(struct seq_file *m, struct cache_detail *cd, struct cache_head *h) |
| 160 | { |
| 161 | struct ent *ent; |
| 162 | |
| 163 | if (h == NULL) { |
| 164 | seq_puts(m, "#domain type id [name]\n"); |
| 165 | return 0; |
| 166 | } |
| 167 | ent = container_of(h, struct ent, h); |
J. Bruce Fields | 0a725fc | 2007-07-31 00:37:52 -0700 | [diff] [blame] | 168 | seq_printf(m, "%s %s %u", ent->authname, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | ent->type == IDMAP_TYPE_GROUP ? "group" : "user", |
| 170 | ent->id); |
| 171 | if (test_bit(CACHE_VALID, &h->flags)) |
| 172 | seq_printf(m, " %s", ent->name); |
| 173 | seq_printf(m, "\n"); |
| 174 | return 0; |
| 175 | } |
| 176 | |
| 177 | static void |
| 178 | warn_no_idmapd(struct cache_detail *detail) |
| 179 | { |
| 180 | printk("nfsd: nfsv4 idmapping failing: has idmapd %s?\n", |
| 181 | detail->last_close? "died" : "not been started"); |
| 182 | } |
| 183 | |
| 184 | |
| 185 | static int idtoname_parse(struct cache_detail *, char *, int); |
NeilBrown | f9ecc92 | 2006-03-27 01:15:06 -0800 | [diff] [blame] | 186 | static struct ent *idtoname_lookup(struct ent *); |
| 187 | static struct ent *idtoname_update(struct ent *, struct ent *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 189 | static struct cache_detail idtoname_cache = { |
Bruce Allan | f35279d | 2005-09-06 15:17:08 -0700 | [diff] [blame] | 190 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | .hash_size = ENT_HASHMAX, |
| 192 | .hash_table = idtoname_table, |
| 193 | .name = "nfs4.idtoname", |
| 194 | .cache_put = ent_put, |
| 195 | .cache_request = idtoname_request, |
| 196 | .cache_parse = idtoname_parse, |
| 197 | .cache_show = idtoname_show, |
| 198 | .warn_no_listener = warn_no_idmapd, |
NeilBrown | f9ecc92 | 2006-03-27 01:15:06 -0800 | [diff] [blame] | 199 | .match = idtoname_match, |
| 200 | .init = ent_init, |
| 201 | .update = ent_init, |
| 202 | .alloc = ent_alloc, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | }; |
| 204 | |
| 205 | int |
| 206 | idtoname_parse(struct cache_detail *cd, char *buf, int buflen) |
| 207 | { |
| 208 | struct ent ent, *res; |
| 209 | char *buf1, *bp; |
J. Bruce Fields | c9b6cbe | 2007-07-27 16:36:45 -0400 | [diff] [blame] | 210 | int len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | int error = -EINVAL; |
| 212 | |
| 213 | if (buf[buflen - 1] != '\n') |
| 214 | return (-EINVAL); |
| 215 | buf[buflen - 1]= '\0'; |
| 216 | |
| 217 | buf1 = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 218 | if (buf1 == NULL) |
| 219 | return (-ENOMEM); |
| 220 | |
| 221 | memset(&ent, 0, sizeof(ent)); |
| 222 | |
| 223 | /* Authentication name */ |
| 224 | if (qword_get(&buf, buf1, PAGE_SIZE) <= 0) |
| 225 | goto out; |
| 226 | memcpy(ent.authname, buf1, sizeof(ent.authname)); |
| 227 | |
| 228 | /* Type */ |
| 229 | if (qword_get(&buf, buf1, PAGE_SIZE) <= 0) |
| 230 | goto out; |
| 231 | ent.type = strcmp(buf1, "user") == 0 ? |
| 232 | IDMAP_TYPE_USER : IDMAP_TYPE_GROUP; |
| 233 | |
| 234 | /* ID */ |
| 235 | if (qword_get(&buf, buf1, PAGE_SIZE) <= 0) |
| 236 | goto out; |
| 237 | ent.id = simple_strtoul(buf1, &bp, 10); |
| 238 | if (bp == buf1) |
| 239 | goto out; |
| 240 | |
| 241 | /* expiry */ |
| 242 | ent.h.expiry_time = get_expiry(&buf); |
| 243 | if (ent.h.expiry_time == 0) |
| 244 | goto out; |
| 245 | |
NeilBrown | f9ecc92 | 2006-03-27 01:15:06 -0800 | [diff] [blame] | 246 | error = -ENOMEM; |
| 247 | res = idtoname_lookup(&ent); |
| 248 | if (!res) |
| 249 | goto out; |
| 250 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | /* Name */ |
J. Bruce Fields | c9b6cbe | 2007-07-27 16:36:45 -0400 | [diff] [blame] | 252 | error = -EINVAL; |
| 253 | len = qword_get(&buf, buf1, PAGE_SIZE); |
| 254 | if (len < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | goto out; |
J. Bruce Fields | c9b6cbe | 2007-07-27 16:36:45 -0400 | [diff] [blame] | 256 | if (len == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | set_bit(CACHE_NEGATIVE, &ent.h.flags); |
J. Bruce Fields | d4395e0 | 2007-10-26 13:32:50 -0400 | [diff] [blame] | 258 | else if (len >= IDMAP_NAMESZ) |
| 259 | goto out; |
| 260 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | memcpy(ent.name, buf1, sizeof(ent.name)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | error = -ENOMEM; |
NeilBrown | f9ecc92 | 2006-03-27 01:15:06 -0800 | [diff] [blame] | 263 | res = idtoname_update(&ent, res); |
| 264 | if (res == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | goto out; |
| 266 | |
NeilBrown | baab935 | 2006-03-27 01:15:09 -0800 | [diff] [blame] | 267 | cache_put(&res->h, &idtoname_cache); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | |
| 269 | error = 0; |
| 270 | out: |
| 271 | kfree(buf1); |
| 272 | |
| 273 | return error; |
| 274 | } |
| 275 | |
NeilBrown | f9ecc92 | 2006-03-27 01:15:06 -0800 | [diff] [blame] | 276 | |
| 277 | static struct ent * |
| 278 | idtoname_lookup(struct ent *item) |
| 279 | { |
| 280 | struct cache_head *ch = sunrpc_cache_lookup(&idtoname_cache, |
| 281 | &item->h, |
| 282 | idtoname_hash(item)); |
| 283 | if (ch) |
| 284 | return container_of(ch, struct ent, h); |
| 285 | else |
| 286 | return NULL; |
| 287 | } |
| 288 | |
| 289 | static struct ent * |
| 290 | idtoname_update(struct ent *new, struct ent *old) |
| 291 | { |
| 292 | struct cache_head *ch = sunrpc_cache_update(&idtoname_cache, |
| 293 | &new->h, &old->h, |
| 294 | idtoname_hash(new)); |
| 295 | if (ch) |
| 296 | return container_of(ch, struct ent, h); |
| 297 | else |
| 298 | return NULL; |
| 299 | } |
| 300 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | |
| 302 | /* |
| 303 | * Name -> ID cache |
| 304 | */ |
| 305 | |
| 306 | static struct cache_head *nametoid_table[ENT_HASHMAX]; |
| 307 | |
| 308 | static inline int |
| 309 | nametoid_hash(struct ent *ent) |
| 310 | { |
| 311 | return hash_str(ent->name, ENT_HASHBITS); |
| 312 | } |
| 313 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 314 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | nametoid_request(struct cache_detail *cd, struct cache_head *ch, char **bpp, |
| 316 | int *blen) |
| 317 | { |
| 318 | struct ent *ent = container_of(ch, struct ent, h); |
| 319 | |
| 320 | qword_add(bpp, blen, ent->authname); |
| 321 | qword_add(bpp, blen, ent->type == IDMAP_TYPE_GROUP ? "group" : "user"); |
| 322 | qword_add(bpp, blen, ent->name); |
| 323 | |
| 324 | (*bpp)[-1] = '\n'; |
| 325 | } |
| 326 | |
NeilBrown | f9ecc92 | 2006-03-27 01:15:06 -0800 | [diff] [blame] | 327 | static int |
| 328 | nametoid_match(struct cache_head *ca, struct cache_head *cb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | { |
NeilBrown | f9ecc92 | 2006-03-27 01:15:06 -0800 | [diff] [blame] | 330 | struct ent *a = container_of(ca, struct ent, h); |
| 331 | struct ent *b = container_of(cb, struct ent, h); |
| 332 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | return (a->type == b->type && strcmp(a->name, b->name) == 0 && |
| 334 | strcmp(a->authname, b->authname) == 0); |
| 335 | } |
| 336 | |
| 337 | static int |
| 338 | nametoid_show(struct seq_file *m, struct cache_detail *cd, struct cache_head *h) |
| 339 | { |
| 340 | struct ent *ent; |
| 341 | |
| 342 | if (h == NULL) { |
| 343 | seq_puts(m, "#domain type name [id]\n"); |
| 344 | return 0; |
| 345 | } |
| 346 | ent = container_of(h, struct ent, h); |
| 347 | seq_printf(m, "%s %s %s", ent->authname, |
| 348 | ent->type == IDMAP_TYPE_GROUP ? "group" : "user", |
| 349 | ent->name); |
| 350 | if (test_bit(CACHE_VALID, &h->flags)) |
J. Bruce Fields | 0a725fc | 2007-07-31 00:37:52 -0700 | [diff] [blame] | 351 | seq_printf(m, " %u", ent->id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | seq_printf(m, "\n"); |
| 353 | return 0; |
| 354 | } |
| 355 | |
NeilBrown | f9ecc92 | 2006-03-27 01:15:06 -0800 | [diff] [blame] | 356 | static struct ent *nametoid_lookup(struct ent *); |
| 357 | static struct ent *nametoid_update(struct ent *, struct ent *); |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 358 | static int nametoid_parse(struct cache_detail *, char *, int); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 360 | static struct cache_detail nametoid_cache = { |
Bruce Allan | f35279d | 2005-09-06 15:17:08 -0700 | [diff] [blame] | 361 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | .hash_size = ENT_HASHMAX, |
| 363 | .hash_table = nametoid_table, |
| 364 | .name = "nfs4.nametoid", |
| 365 | .cache_put = ent_put, |
| 366 | .cache_request = nametoid_request, |
| 367 | .cache_parse = nametoid_parse, |
| 368 | .cache_show = nametoid_show, |
| 369 | .warn_no_listener = warn_no_idmapd, |
NeilBrown | f9ecc92 | 2006-03-27 01:15:06 -0800 | [diff] [blame] | 370 | .match = nametoid_match, |
| 371 | .init = ent_init, |
| 372 | .update = ent_init, |
| 373 | .alloc = ent_alloc, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | }; |
| 375 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 376 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | nametoid_parse(struct cache_detail *cd, char *buf, int buflen) |
| 378 | { |
| 379 | struct ent ent, *res; |
| 380 | char *buf1; |
| 381 | int error = -EINVAL; |
| 382 | |
| 383 | if (buf[buflen - 1] != '\n') |
| 384 | return (-EINVAL); |
| 385 | buf[buflen - 1]= '\0'; |
| 386 | |
| 387 | buf1 = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 388 | if (buf1 == NULL) |
| 389 | return (-ENOMEM); |
| 390 | |
| 391 | memset(&ent, 0, sizeof(ent)); |
| 392 | |
| 393 | /* Authentication name */ |
| 394 | if (qword_get(&buf, buf1, PAGE_SIZE) <= 0) |
| 395 | goto out; |
| 396 | memcpy(ent.authname, buf1, sizeof(ent.authname)); |
| 397 | |
| 398 | /* Type */ |
| 399 | if (qword_get(&buf, buf1, PAGE_SIZE) <= 0) |
| 400 | goto out; |
| 401 | ent.type = strcmp(buf1, "user") == 0 ? |
| 402 | IDMAP_TYPE_USER : IDMAP_TYPE_GROUP; |
| 403 | |
| 404 | /* Name */ |
| 405 | error = qword_get(&buf, buf1, PAGE_SIZE); |
| 406 | if (error <= 0 || error >= IDMAP_NAMESZ) |
| 407 | goto out; |
| 408 | memcpy(ent.name, buf1, sizeof(ent.name)); |
| 409 | |
| 410 | /* expiry */ |
| 411 | ent.h.expiry_time = get_expiry(&buf); |
| 412 | if (ent.h.expiry_time == 0) |
| 413 | goto out; |
| 414 | |
| 415 | /* ID */ |
| 416 | error = get_int(&buf, &ent.id); |
| 417 | if (error == -EINVAL) |
| 418 | goto out; |
| 419 | if (error == -ENOENT) |
| 420 | set_bit(CACHE_NEGATIVE, &ent.h.flags); |
| 421 | |
| 422 | error = -ENOMEM; |
NeilBrown | f9ecc92 | 2006-03-27 01:15:06 -0800 | [diff] [blame] | 423 | res = nametoid_lookup(&ent); |
| 424 | if (res == NULL) |
| 425 | goto out; |
| 426 | res = nametoid_update(&ent, res); |
| 427 | if (res == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | goto out; |
| 429 | |
NeilBrown | baab935 | 2006-03-27 01:15:09 -0800 | [diff] [blame] | 430 | cache_put(&res->h, &nametoid_cache); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | error = 0; |
| 432 | out: |
| 433 | kfree(buf1); |
| 434 | |
| 435 | return (error); |
| 436 | } |
| 437 | |
NeilBrown | f9ecc92 | 2006-03-27 01:15:06 -0800 | [diff] [blame] | 438 | |
| 439 | static struct ent * |
| 440 | nametoid_lookup(struct ent *item) |
| 441 | { |
| 442 | struct cache_head *ch = sunrpc_cache_lookup(&nametoid_cache, |
| 443 | &item->h, |
| 444 | nametoid_hash(item)); |
| 445 | if (ch) |
| 446 | return container_of(ch, struct ent, h); |
| 447 | else |
| 448 | return NULL; |
| 449 | } |
| 450 | |
| 451 | static struct ent * |
| 452 | nametoid_update(struct ent *new, struct ent *old) |
| 453 | { |
| 454 | struct cache_head *ch = sunrpc_cache_update(&nametoid_cache, |
| 455 | &new->h, &old->h, |
| 456 | nametoid_hash(new)); |
| 457 | if (ch) |
| 458 | return container_of(ch, struct ent, h); |
| 459 | else |
| 460 | return NULL; |
| 461 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | |
| 463 | /* |
| 464 | * Exported API |
| 465 | */ |
| 466 | |
| 467 | void |
| 468 | nfsd_idmap_init(void) |
| 469 | { |
| 470 | cache_register(&idtoname_cache); |
| 471 | cache_register(&nametoid_cache); |
| 472 | } |
| 473 | |
| 474 | void |
| 475 | nfsd_idmap_shutdown(void) |
| 476 | { |
J. Bruce Fields | df95a9d | 2007-11-08 16:09:59 -0500 | [diff] [blame^] | 477 | cache_unregister(&idtoname_cache); |
| 478 | cache_unregister(&nametoid_cache); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | /* |
| 482 | * Deferred request handling |
| 483 | */ |
| 484 | |
| 485 | struct idmap_defer_req { |
| 486 | struct cache_req req; |
| 487 | struct cache_deferred_req deferred_req; |
| 488 | wait_queue_head_t waitq; |
| 489 | atomic_t count; |
| 490 | }; |
| 491 | |
| 492 | static inline void |
| 493 | put_mdr(struct idmap_defer_req *mdr) |
| 494 | { |
| 495 | if (atomic_dec_and_test(&mdr->count)) |
| 496 | kfree(mdr); |
| 497 | } |
| 498 | |
| 499 | static inline void |
| 500 | get_mdr(struct idmap_defer_req *mdr) |
| 501 | { |
| 502 | atomic_inc(&mdr->count); |
| 503 | } |
| 504 | |
| 505 | static void |
| 506 | idmap_revisit(struct cache_deferred_req *dreq, int toomany) |
| 507 | { |
| 508 | struct idmap_defer_req *mdr = |
| 509 | container_of(dreq, struct idmap_defer_req, deferred_req); |
| 510 | |
| 511 | wake_up(&mdr->waitq); |
| 512 | put_mdr(mdr); |
| 513 | } |
| 514 | |
| 515 | static struct cache_deferred_req * |
| 516 | idmap_defer(struct cache_req *req) |
| 517 | { |
| 518 | struct idmap_defer_req *mdr = |
| 519 | container_of(req, struct idmap_defer_req, req); |
| 520 | |
| 521 | mdr->deferred_req.revisit = idmap_revisit; |
| 522 | get_mdr(mdr); |
| 523 | return (&mdr->deferred_req); |
| 524 | } |
| 525 | |
| 526 | static inline int |
NeilBrown | f9ecc92 | 2006-03-27 01:15:06 -0800 | [diff] [blame] | 527 | do_idmap_lookup(struct ent *(*lookup_fn)(struct ent *), struct ent *key, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | struct cache_detail *detail, struct ent **item, |
| 529 | struct idmap_defer_req *mdr) |
| 530 | { |
NeilBrown | f9ecc92 | 2006-03-27 01:15:06 -0800 | [diff] [blame] | 531 | *item = lookup_fn(key); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | if (!*item) |
| 533 | return -ENOMEM; |
| 534 | return cache_check(detail, &(*item)->h, &mdr->req); |
| 535 | } |
| 536 | |
| 537 | static inline int |
NeilBrown | f9ecc92 | 2006-03-27 01:15:06 -0800 | [diff] [blame] | 538 | do_idmap_lookup_nowait(struct ent *(*lookup_fn)(struct ent *), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | struct ent *key, struct cache_detail *detail, |
| 540 | struct ent **item) |
| 541 | { |
| 542 | int ret = -ENOMEM; |
| 543 | |
NeilBrown | f9ecc92 | 2006-03-27 01:15:06 -0800 | [diff] [blame] | 544 | *item = lookup_fn(key); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | if (!*item) |
| 546 | goto out_err; |
| 547 | ret = -ETIMEDOUT; |
| 548 | if (!test_bit(CACHE_VALID, &(*item)->h.flags) |
| 549 | || (*item)->h.expiry_time < get_seconds() |
| 550 | || detail->flush_time > (*item)->h.last_refresh) |
| 551 | goto out_put; |
| 552 | ret = -ENOENT; |
| 553 | if (test_bit(CACHE_NEGATIVE, &(*item)->h.flags)) |
| 554 | goto out_put; |
| 555 | return 0; |
| 556 | out_put: |
NeilBrown | baab935 | 2006-03-27 01:15:09 -0800 | [diff] [blame] | 557 | cache_put(&(*item)->h, detail); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | out_err: |
| 559 | *item = NULL; |
| 560 | return ret; |
| 561 | } |
| 562 | |
| 563 | static int |
| 564 | idmap_lookup(struct svc_rqst *rqstp, |
NeilBrown | f9ecc92 | 2006-03-27 01:15:06 -0800 | [diff] [blame] | 565 | struct ent *(*lookup_fn)(struct ent *), struct ent *key, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | struct cache_detail *detail, struct ent **item) |
| 567 | { |
| 568 | struct idmap_defer_req *mdr; |
| 569 | int ret; |
| 570 | |
Panagiotis Issaris | f8314dc | 2006-09-27 01:49:37 -0700 | [diff] [blame] | 571 | mdr = kzalloc(sizeof(*mdr), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | if (!mdr) |
| 573 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | atomic_set(&mdr->count, 1); |
| 575 | init_waitqueue_head(&mdr->waitq); |
| 576 | mdr->req.defer = idmap_defer; |
| 577 | ret = do_idmap_lookup(lookup_fn, key, detail, item, mdr); |
| 578 | if (ret == -EAGAIN) { |
| 579 | wait_event_interruptible_timeout(mdr->waitq, |
| 580 | test_bit(CACHE_VALID, &(*item)->h.flags), 1 * HZ); |
| 581 | ret = do_idmap_lookup_nowait(lookup_fn, key, detail, item); |
| 582 | } |
| 583 | put_mdr(mdr); |
| 584 | return ret; |
| 585 | } |
| 586 | |
J. Bruce Fields | 3ab4d8b | 2007-07-17 04:04:46 -0700 | [diff] [blame] | 587 | static char * |
| 588 | rqst_authname(struct svc_rqst *rqstp) |
| 589 | { |
| 590 | struct auth_domain *clp; |
| 591 | |
| 592 | clp = rqstp->rq_gssclient ? rqstp->rq_gssclient : rqstp->rq_client; |
| 593 | return clp->name; |
| 594 | } |
| 595 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | static int |
| 597 | idmap_name_to_id(struct svc_rqst *rqstp, int type, const char *name, u32 namelen, |
| 598 | uid_t *id) |
| 599 | { |
| 600 | struct ent *item, key = { |
| 601 | .type = type, |
| 602 | }; |
| 603 | int ret; |
| 604 | |
| 605 | if (namelen + 1 > sizeof(key.name)) |
| 606 | return -EINVAL; |
| 607 | memcpy(key.name, name, namelen); |
| 608 | key.name[namelen] = '\0'; |
J. Bruce Fields | 3ab4d8b | 2007-07-17 04:04:46 -0700 | [diff] [blame] | 609 | strlcpy(key.authname, rqst_authname(rqstp), sizeof(key.authname)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | ret = idmap_lookup(rqstp, nametoid_lookup, &key, &nametoid_cache, &item); |
| 611 | if (ret == -ENOENT) |
| 612 | ret = -ESRCH; /* nfserr_badname */ |
| 613 | if (ret) |
| 614 | return ret; |
| 615 | *id = item->id; |
NeilBrown | baab935 | 2006-03-27 01:15:09 -0800 | [diff] [blame] | 616 | cache_put(&item->h, &nametoid_cache); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | return 0; |
| 618 | } |
| 619 | |
| 620 | static int |
| 621 | idmap_id_to_name(struct svc_rqst *rqstp, int type, uid_t id, char *name) |
| 622 | { |
| 623 | struct ent *item, key = { |
| 624 | .id = id, |
| 625 | .type = type, |
| 626 | }; |
| 627 | int ret; |
| 628 | |
J. Bruce Fields | 3ab4d8b | 2007-07-17 04:04:46 -0700 | [diff] [blame] | 629 | strlcpy(key.authname, rqst_authname(rqstp), sizeof(key.authname)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | ret = idmap_lookup(rqstp, idtoname_lookup, &key, &idtoname_cache, &item); |
| 631 | if (ret == -ENOENT) |
| 632 | return sprintf(name, "%u", id); |
| 633 | if (ret) |
| 634 | return ret; |
| 635 | ret = strlen(item->name); |
| 636 | BUG_ON(ret > IDMAP_NAMESZ); |
| 637 | memcpy(name, item->name, ret); |
NeilBrown | baab935 | 2006-03-27 01:15:09 -0800 | [diff] [blame] | 638 | cache_put(&item->h, &idtoname_cache); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | return ret; |
| 640 | } |
| 641 | |
| 642 | int |
| 643 | nfsd_map_name_to_uid(struct svc_rqst *rqstp, const char *name, size_t namelen, |
| 644 | __u32 *id) |
| 645 | { |
| 646 | return idmap_name_to_id(rqstp, IDMAP_TYPE_USER, name, namelen, id); |
| 647 | } |
| 648 | |
| 649 | int |
| 650 | nfsd_map_name_to_gid(struct svc_rqst *rqstp, const char *name, size_t namelen, |
| 651 | __u32 *id) |
| 652 | { |
| 653 | return idmap_name_to_id(rqstp, IDMAP_TYPE_GROUP, name, namelen, id); |
| 654 | } |
| 655 | |
| 656 | int |
| 657 | nfsd_map_uid_to_name(struct svc_rqst *rqstp, __u32 id, char *name) |
| 658 | { |
| 659 | return idmap_id_to_name(rqstp, IDMAP_TYPE_USER, id, name); |
| 660 | } |
| 661 | |
| 662 | int |
| 663 | nfsd_map_gid_to_name(struct svc_rqst *rqstp, __u32 id, char *name) |
| 664 | { |
| 665 | return idmap_id_to_name(rqstp, IDMAP_TYPE_GROUP, id, name); |
| 666 | } |