Blame view

fs/nfs/idmap.c 19.4 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
  /*
   * fs/nfs/idmap.c
   *
   *  UID and GID to name mapping for clients.
   *
   *  Copyright (c) 2002 The Regents of the University of Michigan.
   *  All rights reserved.
   *
   *  Marius Aamodt Eriksen <marius@umich.edu>
   *
   *  Redistribution and use in source and binary forms, with or without
   *  modification, are permitted provided that the following conditions
   *  are met:
   *
   *  1. Redistributions of source code must retain the above copyright
   *     notice, this list of conditions and the following disclaimer.
   *  2. Redistributions in binary form must reproduce the above copyright
   *     notice, this list of conditions and the following disclaimer in the
   *     documentation and/or other materials provided with the distribution.
   *  3. Neither the name of the University nor the names of its
   *     contributors may be used to endorse or promote products derived
   *     from this software without specific prior written permission.
   *
   *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
   *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
   *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   *  DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
   *  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
   *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
   *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
   *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   */
5cf36cfdc   Trond Myklebust   NFSv4: If the ser...
36
  #include <linux/types.h>
57e62324e   Bryan Schumaker   NFS: Store the le...
37
38
  #include <linux/parser.h>
  #include <linux/fs.h>
e44ba033c   Vitaliy Ivanov   treewide: remove ...
39
  #include <linux/nfs_idmap.h>
57e62324e   Bryan Schumaker   NFS: Store the le...
40
41
  #include <net/net_namespace.h>
  #include <linux/sunrpc/rpc_pipe_fs.h>
6926afd19   Trond Myklebust   NFSv4: Save the o...
42
  #include <linux/nfs_fs.h>
3cd0f37a2   Bryan Schumaker   NFS: Keep idmappe...
43
  #include <linux/nfs_fs_sb.h>
57e62324e   Bryan Schumaker   NFS: Store the le...
44
  #include <linux/key.h>
3cd0f37a2   Bryan Schumaker   NFS: Keep idmappe...
45
46
  #include <linux/keyctl.h>
  #include <linux/key-type.h>
3cd0f37a2   Bryan Schumaker   NFS: Keep idmappe...
47
  #include <keys/user-type.h>
3cd0f37a2   Bryan Schumaker   NFS: Keep idmappe...
48
  #include <linux/module.h>
57e62324e   Bryan Schumaker   NFS: Store the le...
49

3cd0f37a2   Bryan Schumaker   NFS: Keep idmappe...
50
  #include "internal.h"
17347d03c   Stanislav Kinsbursky   NFS: build fixed ...
51
  #include "netns.h"
1f2d30b53   Trond Myklebust   NFSv4: Add tracep...
52
  #include "nfs4trace.h"
3cd0f37a2   Bryan Schumaker   NFS: Keep idmappe...
53
54
  
  #define NFS_UINT_MAXLEN 11
3cd0f37a2   Bryan Schumaker   NFS: Keep idmappe...
55

17280175c   Trond Myklebust   NFS: Fix a number...
56
57
  static const struct cred *id_resolver_cache;
  static struct key_type key_type_id_resolver_legacy;
3cd0f37a2   Bryan Schumaker   NFS: Keep idmappe...
58

c5066945b   Bryan Schumaker   NFS: Clear key co...
59
60
61
  struct idmap_legacy_upcalldata {
  	struct rpc_pipe_msg pipe_msg;
  	struct idmap_msg idmap_msg;
0cac12023   Trond Myklebust   NFSv4: Ensure tha...
62
  	struct key_construction	*key_cons;
c5066945b   Bryan Schumaker   NFS: Clear key co...
63
64
  	struct idmap *idmap;
  };
0cac12023   Trond Myklebust   NFSv4: Ensure tha...
65
  struct idmap {
2127d82af   Trond Myklebust   NFSv4: Convert id...
66
  	struct rpc_pipe_dir_object idmap_pdo;
0cac12023   Trond Myklebust   NFSv4: Ensure tha...
67
68
69
70
  	struct rpc_pipe		*idmap_pipe;
  	struct idmap_legacy_upcalldata *idmap_upcall_data;
  	struct mutex		idmap_mutex;
  };
6926afd19   Trond Myklebust   NFSv4: Save the o...
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
  /**
   * nfs_fattr_init_names - initialise the nfs_fattr owner_name/group_name fields
   * @fattr: fully initialised struct nfs_fattr
   * @owner_name: owner name string cache
   * @group_name: group name string cache
   */
  void nfs_fattr_init_names(struct nfs_fattr *fattr,
  		struct nfs4_string *owner_name,
  		struct nfs4_string *group_name)
  {
  	fattr->owner_name = owner_name;
  	fattr->group_name = group_name;
  }
  
  static void nfs_fattr_free_owner_name(struct nfs_fattr *fattr)
  {
  	fattr->valid &= ~NFS_ATTR_FATTR_OWNER_NAME;
  	kfree(fattr->owner_name->data);
  }
  
  static void nfs_fattr_free_group_name(struct nfs_fattr *fattr)
  {
  	fattr->valid &= ~NFS_ATTR_FATTR_GROUP_NAME;
  	kfree(fattr->group_name->data);
  }
  
  static bool nfs_fattr_map_owner_name(struct nfs_server *server, struct nfs_fattr *fattr)
  {
  	struct nfs4_string *owner = fattr->owner_name;
9f309c86c   Eric W. Biederman   nfs: Convert idma...
100
  	kuid_t uid;
6926afd19   Trond Myklebust   NFSv4: Save the o...
101
102
103
104
105
106
107
108
109
110
111
112
113
  
  	if (!(fattr->valid & NFS_ATTR_FATTR_OWNER_NAME))
  		return false;
  	if (nfs_map_name_to_uid(server, owner->data, owner->len, &uid) == 0) {
  		fattr->uid = uid;
  		fattr->valid |= NFS_ATTR_FATTR_OWNER;
  	}
  	return true;
  }
  
  static bool nfs_fattr_map_group_name(struct nfs_server *server, struct nfs_fattr *fattr)
  {
  	struct nfs4_string *group = fattr->group_name;
9f309c86c   Eric W. Biederman   nfs: Convert idma...
114
  	kgid_t gid;
6926afd19   Trond Myklebust   NFSv4: Save the o...
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
  
  	if (!(fattr->valid & NFS_ATTR_FATTR_GROUP_NAME))
  		return false;
  	if (nfs_map_group_to_gid(server, group->data, group->len, &gid) == 0) {
  		fattr->gid = gid;
  		fattr->valid |= NFS_ATTR_FATTR_GROUP;
  	}
  	return true;
  }
  
  /**
   * nfs_fattr_free_names - free up the NFSv4 owner and group strings
   * @fattr: a fully initialised nfs_fattr structure
   */
  void nfs_fattr_free_names(struct nfs_fattr *fattr)
  {
  	if (fattr->valid & NFS_ATTR_FATTR_OWNER_NAME)
  		nfs_fattr_free_owner_name(fattr);
  	if (fattr->valid & NFS_ATTR_FATTR_GROUP_NAME)
  		nfs_fattr_free_group_name(fattr);
  }
  
  /**
   * nfs_fattr_map_and_free_names - map owner/group strings into uid/gid and free
   * @server: pointer to the filesystem nfs_server structure
   * @fattr: a fully initialised nfs_fattr structure
   *
   * This helper maps the cached NFSv4 owner/group strings in fattr into
   * their numeric uid/gid equivalents, and then frees the cached strings.
   */
  void nfs_fattr_map_and_free_names(struct nfs_server *server, struct nfs_fattr *fattr)
  {
  	if (nfs_fattr_map_owner_name(server, fattr))
  		nfs_fattr_free_owner_name(fattr);
  	if (nfs_fattr_map_group_name(server, fattr))
  		nfs_fattr_free_group_name(fattr);
  }
5cf36cfdc   Trond Myklebust   NFSv4: If the ser...
152
153
154
155
156
157
158
159
160
161
  
  static int nfs_map_string_to_numeric(const char *name, size_t namelen, __u32 *res)
  {
  	unsigned long val;
  	char buf[16];
  
  	if (memchr(name, '@', namelen) != NULL || namelen >= sizeof(buf))
  		return 0;
  	memcpy(buf, name, namelen);
  	buf[namelen] = '\0';
7297cb682   Daniel Walter   nfs: replace stri...
162
  	if (kstrtoul(buf, 0, &val) != 0)
5cf36cfdc   Trond Myklebust   NFSv4: If the ser...
163
164
165
166
  		return 0;
  	*res = val;
  	return 1;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
167

f0b851689   Trond Myklebust   NFSv4: Send unmap...
168
169
170
171
  static int nfs_map_numeric_to_string(__u32 id, char *buf, size_t buflen)
  {
  	return snprintf(buf, buflen, "%u", id);
  }
17280175c   Trond Myklebust   NFS: Fix a number...
172
  static struct key_type key_type_id_resolver = {
955a857e0   Bryan Schumaker   NFS: new idmapper
173
  	.name		= "id_resolver",
f9167789d   David Howells   KEYS: user: Use k...
174
175
176
  	.preparse	= user_preparse,
  	.free_preparse	= user_free_preparse,
  	.instantiate	= generic_key_instantiate,
955a857e0   Bryan Schumaker   NFS: new idmapper
177
178
179
180
181
  	.revoke		= user_revoke,
  	.destroy	= user_destroy,
  	.describe	= user_describe,
  	.read		= user_read,
  };
e6499c6f4   Bryan Schumaker   NFS: Fall back on...
182
  static int nfs_idmap_init_keyring(void)
955a857e0   Bryan Schumaker   NFS: new idmapper
183
184
185
186
  {
  	struct cred *cred;
  	struct key *keyring;
  	int ret = 0;
f9fd2d9c1   Weston Andros Adamson   NFS: printks in f...
187
188
189
  	printk(KERN_NOTICE "NFS: Registering the %s key type
  ",
  		key_type_id_resolver.name);
955a857e0   Bryan Schumaker   NFS: new idmapper
190
191
192
193
  
  	cred = prepare_kernel_cred(NULL);
  	if (!cred)
  		return -ENOMEM;
4e963d4f3   Eric W. Biederman   nfs: Pass GLOBAL_...
194
195
  	keyring = keyring_alloc(".id_resolver",
  				GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, cred,
f8aa23a55   David Howells   KEYS: Use keyring...
196
197
198
  				(KEY_POS_ALL & ~KEY_POS_SETATTR) |
  				KEY_USR_VIEW | KEY_USR_READ,
  				KEY_ALLOC_NOT_IN_QUOTA, NULL);
955a857e0   Bryan Schumaker   NFS: new idmapper
199
200
201
202
  	if (IS_ERR(keyring)) {
  		ret = PTR_ERR(keyring);
  		goto failed_put_cred;
  	}
955a857e0   Bryan Schumaker   NFS: new idmapper
203
204
205
  	ret = register_key_type(&key_type_id_resolver);
  	if (ret < 0)
  		goto failed_put_key;
a427b9ec4   David Howells   NFS: Fix a number...
206
207
208
  	ret = register_key_type(&key_type_id_resolver_legacy);
  	if (ret < 0)
  		goto failed_reg_legacy;
700920eb5   David Howells   KEYS: Allow speci...
209
  	set_bit(KEY_FLAG_ROOT_CAN_CLEAR, &keyring->flags);
955a857e0   Bryan Schumaker   NFS: new idmapper
210
211
212
213
  	cred->thread_keyring = keyring;
  	cred->jit_keyring = KEY_REQKEY_DEFL_THREAD_KEYRING;
  	id_resolver_cache = cred;
  	return 0;
a427b9ec4   David Howells   NFS: Fix a number...
214
215
  failed_reg_legacy:
  	unregister_key_type(&key_type_id_resolver);
955a857e0   Bryan Schumaker   NFS: new idmapper
216
217
218
219
220
221
  failed_put_key:
  	key_put(keyring);
  failed_put_cred:
  	put_cred(cred);
  	return ret;
  }
e6499c6f4   Bryan Schumaker   NFS: Fall back on...
222
  static void nfs_idmap_quit_keyring(void)
955a857e0   Bryan Schumaker   NFS: new idmapper
223
224
225
  {
  	key_revoke(id_resolver_cache->thread_keyring);
  	unregister_key_type(&key_type_id_resolver);
a427b9ec4   David Howells   NFS: Fix a number...
226
  	unregister_key_type(&key_type_id_resolver_legacy);
955a857e0   Bryan Schumaker   NFS: new idmapper
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
  	put_cred(id_resolver_cache);
  }
  
  /*
   * Assemble the description to pass to request_key()
   * This function will allocate a new string and update dest to point
   * at it.  The caller is responsible for freeing dest.
   *
   * On error 0 is returned.  Otherwise, the length of dest is returned.
   */
  static ssize_t nfs_idmap_get_desc(const char *name, size_t namelen,
  				const char *type, size_t typelen, char **desc)
  {
  	char *cp;
  	size_t desclen = typelen + namelen + 2;
  
  	*desc = kmalloc(desclen, GFP_KERNEL);
8f0d97b41   Dan Carpenter   nfs: testing the ...
244
  	if (!*desc)
955a857e0   Bryan Schumaker   NFS: new idmapper
245
246
247
248
249
250
251
252
253
254
255
256
  		return -ENOMEM;
  
  	cp = *desc;
  	memcpy(cp, type, typelen);
  	cp += typelen;
  	*cp++ = ':';
  
  	memcpy(cp, name, namelen);
  	cp += namelen;
  	*cp = '\0';
  	return desclen;
  }
ffa57b9e5   Bryan Schumaker   NFS: Improve lega...
257
258
  static struct key *nfs_idmap_request_key(const char *name, size_t namelen,
  					 const char *type, struct idmap *idmap)
955a857e0   Bryan Schumaker   NFS: new idmapper
259
  {
955a857e0   Bryan Schumaker   NFS: new idmapper
260
  	char *desc;
ffa57b9e5   Bryan Schumaker   NFS: Improve lega...
261
  	struct key *rkey;
955a857e0   Bryan Schumaker   NFS: new idmapper
262
263
264
265
  	ssize_t ret;
  
  	ret = nfs_idmap_get_desc(name, namelen, type, strlen(type), &desc);
  	if (ret <= 0)
ffa57b9e5   Bryan Schumaker   NFS: Improve lega...
266
267
268
269
270
271
272
273
274
  		return ERR_PTR(ret);
  
  	rkey = request_key(&key_type_id_resolver, desc, "");
  	if (IS_ERR(rkey)) {
  		mutex_lock(&idmap->idmap_mutex);
  		rkey = request_key_with_auxdata(&key_type_id_resolver_legacy,
  						desc, "", 0, idmap);
  		mutex_unlock(&idmap->idmap_mutex);
  	}
0c7774abb   David Howells   KEYS: Allow speci...
275
276
  	if (!IS_ERR(rkey))
  		set_bit(KEY_FLAG_ROOT_CAN_INVAL, &rkey->flags);
ffa57b9e5   Bryan Schumaker   NFS: Improve lega...
277
278
279
280
281
282
283
284
285
286
287
288
289
  
  	kfree(desc);
  	return rkey;
  }
  
  static ssize_t nfs_idmap_get_key(const char *name, size_t namelen,
  				 const char *type, void *data,
  				 size_t data_size, struct idmap *idmap)
  {
  	const struct cred *saved_cred;
  	struct key *rkey;
  	struct user_key_payload *payload;
  	ssize_t ret;
955a857e0   Bryan Schumaker   NFS: new idmapper
290
291
  
  	saved_cred = override_creds(id_resolver_cache);
ffa57b9e5   Bryan Schumaker   NFS: Improve lega...
292
  	rkey = nfs_idmap_request_key(name, namelen, type, idmap);
955a857e0   Bryan Schumaker   NFS: new idmapper
293
  	revert_creds(saved_cred);
57e62324e   Bryan Schumaker   NFS: Store the le...
294

955a857e0   Bryan Schumaker   NFS: new idmapper
295
296
297
298
299
300
301
302
303
304
305
  	if (IS_ERR(rkey)) {
  		ret = PTR_ERR(rkey);
  		goto out;
  	}
  
  	rcu_read_lock();
  	rkey->perm |= KEY_USR_VIEW;
  
  	ret = key_validate(rkey);
  	if (ret < 0)
  		goto out_up;
393faffe6   Trond Myklebust   NFSv4: Deal with ...
306
  	payload = rcu_dereference(rkey->payload.rcudata);
955a857e0   Bryan Schumaker   NFS: new idmapper
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
  	if (IS_ERR_OR_NULL(payload)) {
  		ret = PTR_ERR(payload);
  		goto out_up;
  	}
  
  	ret = payload->datalen;
  	if (ret > 0 && ret <= data_size)
  		memcpy(data, payload->data, ret);
  	else
  		ret = -EINVAL;
  
  out_up:
  	rcu_read_unlock();
  	key_put(rkey);
  out:
  	return ret;
  }
955a857e0   Bryan Schumaker   NFS: new idmapper
324
  /* ID -> Name */
57e62324e   Bryan Schumaker   NFS: Store the le...
325
326
  static ssize_t nfs_idmap_lookup_name(__u32 id, const char *type, char *buf,
  				     size_t buflen, struct idmap *idmap)
955a857e0   Bryan Schumaker   NFS: new idmapper
327
328
329
330
331
332
  {
  	char id_str[NFS_UINT_MAXLEN];
  	int id_len;
  	ssize_t ret;
  
  	id_len = snprintf(id_str, sizeof(id_str), "%u", id);
57e62324e   Bryan Schumaker   NFS: Store the le...
333
  	ret = nfs_idmap_get_key(id_str, id_len, type, buf, buflen, idmap);
955a857e0   Bryan Schumaker   NFS: new idmapper
334
335
336
337
338
339
  	if (ret < 0)
  		return -EINVAL;
  	return ret;
  }
  
  /* Name -> ID */
57e62324e   Bryan Schumaker   NFS: Store the le...
340
341
  static int nfs_idmap_lookup_id(const char *name, size_t namelen, const char *type,
  			       __u32 *id, struct idmap *idmap)
955a857e0   Bryan Schumaker   NFS: new idmapper
342
343
344
345
346
  {
  	char id_str[NFS_UINT_MAXLEN];
  	long id_long;
  	ssize_t data_size;
  	int ret = 0;
57e62324e   Bryan Schumaker   NFS: Store the le...
347
  	data_size = nfs_idmap_get_key(name, namelen, type, id_str, NFS_UINT_MAXLEN, idmap);
955a857e0   Bryan Schumaker   NFS: new idmapper
348
349
350
  	if (data_size <= 0) {
  		ret = -EINVAL;
  	} else {
7297cb682   Daniel Walter   nfs: replace stri...
351
  		ret = kstrtol(id_str, 10, &id_long);
955a857e0   Bryan Schumaker   NFS: new idmapper
352
353
354
355
  		*id = (__u32)id_long;
  	}
  	return ret;
  }
e6499c6f4   Bryan Schumaker   NFS: Fall back on...
356
  /* idmap classic begins here */
f0b851689   Trond Myklebust   NFSv4: Send unmap...
357

57e62324e   Bryan Schumaker   NFS: Store the le...
358
359
  enum {
  	Opt_find_uid, Opt_find_gid, Opt_find_user, Opt_find_group, Opt_find_err
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
360
  };
57e62324e   Bryan Schumaker   NFS: Store the le...
361
362
363
364
365
366
  static const match_table_t nfs_idmap_tokens = {
  	{ Opt_find_uid, "uid:%s" },
  	{ Opt_find_gid, "gid:%s" },
  	{ Opt_find_user, "user:%s" },
  	{ Opt_find_group, "group:%s" },
  	{ Opt_find_err, NULL }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
367
  };
57e62324e   Bryan Schumaker   NFS: Store the le...
368
  static int nfs_idmap_legacy_upcall(struct key_construction *, const char *, void *);
369af0f11   Chuck Lever   NFS: Clean up fs/...
369
370
  static ssize_t idmap_pipe_downcall(struct file *, const char __user *,
  				   size_t);
c5066945b   Bryan Schumaker   NFS: Clear key co...
371
  static void idmap_release_pipe(struct inode *);
369af0f11   Chuck Lever   NFS: Clean up fs/...
372
  static void idmap_pipe_destroy_msg(struct rpc_pipe_msg *);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
373

b693ba4a3   Trond Myklebust   SUNRPC: Constify ...
374
  static const struct rpc_pipe_ops idmap_upcall_ops = {
c1225158a   Peng Tao   SUNRPC/NFS: make ...
375
  	.upcall		= rpc_pipe_generic_upcall,
369af0f11   Chuck Lever   NFS: Clean up fs/...
376
  	.downcall	= idmap_pipe_downcall,
c5066945b   Bryan Schumaker   NFS: Clear key co...
377
  	.release_pipe	= idmap_release_pipe,
369af0f11   Chuck Lever   NFS: Clean up fs/...
378
  	.destroy_msg	= idmap_pipe_destroy_msg,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
379
  };
17280175c   Trond Myklebust   NFS: Fix a number...
380
  static struct key_type key_type_id_resolver_legacy = {
a427b9ec4   David Howells   NFS: Fix a number...
381
  	.name		= "id_legacy",
f9167789d   David Howells   KEYS: user: Use k...
382
383
384
  	.preparse	= user_preparse,
  	.free_preparse	= user_free_preparse,
  	.instantiate	= generic_key_instantiate,
57e62324e   Bryan Schumaker   NFS: Store the le...
385
386
387
388
389
390
  	.revoke		= user_revoke,
  	.destroy	= user_destroy,
  	.describe	= user_describe,
  	.read		= user_read,
  	.request_key	= nfs_idmap_legacy_upcall,
  };
2127d82af   Trond Myklebust   NFSv4: Convert id...
391
392
  static void nfs_idmap_pipe_destroy(struct dentry *dir,
  		struct rpc_pipe_dir_object *pdo)
4929d1d33   Stanislav Kinsbursky   NFS: handle NFS i...
393
  {
2127d82af   Trond Myklebust   NFSv4: Convert id...
394
395
  	struct idmap *idmap = pdo->pdo_data;
  	struct rpc_pipe *pipe = idmap->idmap_pipe;
d7631250b   Trond Myklebust   NFSv4: Fix a pote...
396
  	if (pipe->dentry) {
4929d1d33   Stanislav Kinsbursky   NFS: handle NFS i...
397
  		rpc_unlink(pipe->dentry);
d7631250b   Trond Myklebust   NFSv4: Fix a pote...
398
399
  		pipe->dentry = NULL;
  	}
4929d1d33   Stanislav Kinsbursky   NFS: handle NFS i...
400
  }
2127d82af   Trond Myklebust   NFSv4: Convert id...
401
402
  static int nfs_idmap_pipe_create(struct dentry *dir,
  		struct rpc_pipe_dir_object *pdo)
4929d1d33   Stanislav Kinsbursky   NFS: handle NFS i...
403
  {
2127d82af   Trond Myklebust   NFSv4: Convert id...
404
405
  	struct idmap *idmap = pdo->pdo_data;
  	struct rpc_pipe *pipe = idmap->idmap_pipe;
4929d1d33   Stanislav Kinsbursky   NFS: handle NFS i...
406
407
408
409
410
411
412
413
  	struct dentry *dentry;
  
  	dentry = rpc_mkpipe_dentry(dir, "idmap", idmap, pipe);
  	if (IS_ERR(dentry))
  		return PTR_ERR(dentry);
  	pipe->dentry = dentry;
  	return 0;
  }
2127d82af   Trond Myklebust   NFSv4: Convert id...
414
415
416
417
  static const struct rpc_pipe_dir_object_ops nfs_idmap_pipe_dir_object_ops = {
  	.create = nfs_idmap_pipe_create,
  	.destroy = nfs_idmap_pipe_destroy,
  };
4929d1d33   Stanislav Kinsbursky   NFS: handle NFS i...
418

b7162792b   David Howells   NFS: Return an er...
419
  int
adfa6f980   David Howells   NFS: Rename struc...
420
  nfs_idmap_new(struct nfs_client *clp)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
421
422
  {
  	struct idmap *idmap;
c239d83b9   Stanislav Kinsbursky   SUNRPC: split SUN...
423
  	struct rpc_pipe *pipe;
b7162792b   David Howells   NFS: Return an er...
424
  	int error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
425

369af0f11   Chuck Lever   NFS: Clean up fs/...
426
427
428
  	idmap = kzalloc(sizeof(*idmap), GFP_KERNEL);
  	if (idmap == NULL)
  		return -ENOMEM;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
429

2127d82af   Trond Myklebust   NFSv4: Convert id...
430
431
432
  	rpc_init_pipe_dir_object(&idmap->idmap_pdo,
  			&nfs_idmap_pipe_dir_object_ops,
  			idmap);
c239d83b9   Stanislav Kinsbursky   SUNRPC: split SUN...
433
434
435
  	pipe = rpc_mkpipe_data(&idmap_upcall_ops, 0);
  	if (IS_ERR(pipe)) {
  		error = PTR_ERR(pipe);
2127d82af   Trond Myklebust   NFSv4: Convert id...
436
  		goto err;
c239d83b9   Stanislav Kinsbursky   SUNRPC: split SUN...
437
438
  	}
  	idmap->idmap_pipe = pipe;
b1027439d   Bryan Schumaker   NFS: Force the le...
439
  	mutex_init(&idmap->idmap_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
440

2127d82af   Trond Myklebust   NFSv4: Convert id...
441
442
443
444
445
  	error = rpc_add_pipe_dir_object(clp->cl_net,
  			&clp->cl_rpcclient->cl_pipedir_objects,
  			&idmap->idmap_pdo);
  	if (error)
  		goto err_destroy_pipe;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
446
  	clp->cl_idmap = idmap;
b7162792b   David Howells   NFS: Return an er...
447
  	return 0;
2127d82af   Trond Myklebust   NFSv4: Convert id...
448
449
450
451
452
  err_destroy_pipe:
  	rpc_destroy_pipe_data(idmap->idmap_pipe);
  err:
  	kfree(idmap);
  	return error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
453
454
455
  }
  
  void
adfa6f980   David Howells   NFS: Rename struc...
456
  nfs_idmap_delete(struct nfs_client *clp)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
457
458
459
460
461
  {
  	struct idmap *idmap = clp->cl_idmap;
  
  	if (!idmap)
  		return;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
462
  	clp->cl_idmap = NULL;
2127d82af   Trond Myklebust   NFSv4: Convert id...
463
464
465
466
  	rpc_remove_pipe_dir_object(clp->cl_net,
  			&clp->cl_rpcclient->cl_pipedir_objects,
  			&idmap->idmap_pdo);
  	rpc_destroy_pipe_data(idmap->idmap_pipe);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
467
468
  	kfree(idmap);
  }
eee17325f   Stanislav Kinsbursky   NFS: idmap PipeFS...
469
  int nfs_idmap_init(void)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
470
  {
e6499c6f4   Bryan Schumaker   NFS: Fall back on...
471
472
473
474
  	int ret;
  	ret = nfs_idmap_init_keyring();
  	if (ret != 0)
  		goto out;
e6499c6f4   Bryan Schumaker   NFS: Fall back on...
475
476
  out:
  	return ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
477
  }
eee17325f   Stanislav Kinsbursky   NFS: idmap PipeFS...
478
  void nfs_idmap_quit(void)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
479
  {
e6499c6f4   Bryan Schumaker   NFS: Fall back on...
480
  	nfs_idmap_quit_keyring();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
481
  }
c5066945b   Bryan Schumaker   NFS: Clear key co...
482
483
  static int nfs_idmap_prepare_message(char *desc, struct idmap *idmap,
  				     struct idmap_msg *im,
57e62324e   Bryan Schumaker   NFS: Store the le...
484
  				     struct rpc_pipe_msg *msg)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
485
  {
57e62324e   Bryan Schumaker   NFS: Store the le...
486
487
  	substring_t substr;
  	int token, ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
488

57e62324e   Bryan Schumaker   NFS: Store the le...
489
490
  	im->im_type = IDMAP_TYPE_GROUP;
  	token = match_token(desc, nfs_idmap_tokens, &substr);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
491

57e62324e   Bryan Schumaker   NFS: Store the le...
492
493
494
495
496
497
498
  	switch (token) {
  	case Opt_find_uid:
  		im->im_type = IDMAP_TYPE_USER;
  	case Opt_find_gid:
  		im->im_conv = IDMAP_CONV_NAMETOID;
  		ret = match_strlcpy(im->im_name, &substr, IDMAP_NAMESZ);
  		break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
499

57e62324e   Bryan Schumaker   NFS: Store the le...
500
501
502
503
504
505
  	case Opt_find_user:
  		im->im_type = IDMAP_TYPE_USER;
  	case Opt_find_group:
  		im->im_conv = IDMAP_CONV_IDTONAME;
  		ret = match_int(&substr, &im->im_id);
  		break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
506

57e62324e   Bryan Schumaker   NFS: Store the le...
507
508
  	default:
  		ret = -EINVAL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
509
510
  		goto out;
  	}
57e62324e   Bryan Schumaker   NFS: Store the le...
511
512
  	msg->data = im;
  	msg->len  = sizeof(struct idmap_msg);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
513

57e62324e   Bryan Schumaker   NFS: Store the le...
514
  out:
369af0f11   Chuck Lever   NFS: Clean up fs/...
515
  	return ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
516
  }
e9ab41b62   Trond Myklebust   NFSv4: Clean up t...
517
518
  static bool
  nfs_idmap_prepare_pipe_upcall(struct idmap *idmap,
0cac12023   Trond Myklebust   NFSv4: Ensure tha...
519
  		struct idmap_legacy_upcalldata *data)
e9ab41b62   Trond Myklebust   NFSv4: Clean up t...
520
  {
0cac12023   Trond Myklebust   NFSv4: Ensure tha...
521
  	if (idmap->idmap_upcall_data != NULL) {
e9ab41b62   Trond Myklebust   NFSv4: Clean up t...
522
523
524
  		WARN_ON_ONCE(1);
  		return false;
  	}
0cac12023   Trond Myklebust   NFSv4: Ensure tha...
525
  	idmap->idmap_upcall_data = data;
e9ab41b62   Trond Myklebust   NFSv4: Clean up t...
526
527
528
529
530
531
  	return true;
  }
  
  static void
  nfs_idmap_complete_pipe_upcall_locked(struct idmap *idmap, int ret)
  {
0cac12023   Trond Myklebust   NFSv4: Ensure tha...
532
  	struct key_construction *cons = idmap->idmap_upcall_data->key_cons;
e9ab41b62   Trond Myklebust   NFSv4: Clean up t...
533

0cac12023   Trond Myklebust   NFSv4: Ensure tha...
534
535
  	kfree(idmap->idmap_upcall_data);
  	idmap->idmap_upcall_data = NULL;
e9ab41b62   Trond Myklebust   NFSv4: Clean up t...
536
537
538
539
540
541
  	complete_request_key(cons, ret);
  }
  
  static void
  nfs_idmap_abort_pipe_upcall(struct idmap *idmap, int ret)
  {
0cac12023   Trond Myklebust   NFSv4: Ensure tha...
542
  	if (idmap->idmap_upcall_data != NULL)
e9ab41b62   Trond Myklebust   NFSv4: Clean up t...
543
544
  		nfs_idmap_complete_pipe_upcall_locked(idmap, ret);
  }
57e62324e   Bryan Schumaker   NFS: Store the le...
545
546
547
  static int nfs_idmap_legacy_upcall(struct key_construction *cons,
  				   const char *op,
  				   void *aux)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
548
  {
c5066945b   Bryan Schumaker   NFS: Clear key co...
549
  	struct idmap_legacy_upcalldata *data;
57e62324e   Bryan Schumaker   NFS: Store the le...
550
  	struct rpc_pipe_msg *msg;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
551
  	struct idmap_msg *im;
57e62324e   Bryan Schumaker   NFS: Store the le...
552
553
  	struct idmap *idmap = (struct idmap *)aux;
  	struct key *key = cons->key;
5abc03cd9   Dan Carpenter   NFS: kmalloc() do...
554
  	int ret = -ENOMEM;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
555

57e62324e   Bryan Schumaker   NFS: Store the le...
556
  	/* msg and im are freed in idmap_pipe_destroy_msg */
57a51048d   Bryan Schumaker   NFS: Use kzalloc(...
557
  	data = kzalloc(sizeof(*data), GFP_KERNEL);
c5066945b   Bryan Schumaker   NFS: Clear key co...
558
  	if (!data)
57e62324e   Bryan Schumaker   NFS: Store the le...
559
  		goto out1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
560

c5066945b   Bryan Schumaker   NFS: Clear key co...
561
562
563
  	msg = &data->pipe_msg;
  	im = &data->idmap_msg;
  	data->idmap = idmap;
ddfc4e171   Bryan Schumaker   NFS: Set key cons...
564
  	data->key_cons = cons;
c5066945b   Bryan Schumaker   NFS: Clear key co...
565
566
  
  	ret = nfs_idmap_prepare_message(key->description, idmap, im, msg);
57e62324e   Bryan Schumaker   NFS: Store the le...
567
568
  	if (ret < 0)
  		goto out2;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
569

e9ab41b62   Trond Myklebust   NFSv4: Clean up t...
570
  	ret = -EAGAIN;
0cac12023   Trond Myklebust   NFSv4: Ensure tha...
571
  	if (!nfs_idmap_prepare_pipe_upcall(idmap, data))
0e24d849c   Trond Myklebust   NFSv4: Remove BUG...
572
  		goto out2;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
573

11588f493   Bryan Schumaker   NFS: Check return...
574
575
  	ret = rpc_queue_upcall(idmap->idmap_pipe, msg);
  	if (ret < 0)
e9ab41b62   Trond Myklebust   NFSv4: Clean up t...
576
  		nfs_idmap_abort_pipe_upcall(idmap, ret);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
577

11588f493   Bryan Schumaker   NFS: Check return...
578
  	return ret;
57e62324e   Bryan Schumaker   NFS: Store the le...
579
  out2:
c5066945b   Bryan Schumaker   NFS: Clear key co...
580
  	kfree(data);
57e62324e   Bryan Schumaker   NFS: Store the le...
581
  out1:
a427b9ec4   David Howells   NFS: Fix a number...
582
  	complete_request_key(cons, ret);
369af0f11   Chuck Lever   NFS: Clean up fs/...
583
  	return ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
584
  }
cf4ab538f   Trond Myklebust   NFSv4: Fix the st...
585
  static int nfs_idmap_instantiate(struct key *key, struct key *authkey, char *data, size_t datalen)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
586
  {
cf4ab538f   Trond Myklebust   NFSv4: Fix the st...
587
  	return key_instantiate_and_link(key, data, datalen,
57e62324e   Bryan Schumaker   NFS: Store the le...
588
589
590
  					id_resolver_cache->thread_keyring,
  					authkey);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
591

0cac12023   Trond Myklebust   NFSv4: Ensure tha...
592
593
594
  static int nfs_idmap_read_and_verify_message(struct idmap_msg *im,
  		struct idmap_msg *upcall,
  		struct key *key, struct key *authkey)
57e62324e   Bryan Schumaker   NFS: Store the le...
595
596
  {
  	char id_str[NFS_UINT_MAXLEN];
cf4ab538f   Trond Myklebust   NFSv4: Fix the st...
597
  	size_t len;
0cac12023   Trond Myklebust   NFSv4: Ensure tha...
598
  	int ret = -ENOKEY;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
599

0cac12023   Trond Myklebust   NFSv4: Ensure tha...
600
601
602
  	/* ret = -ENOKEY */
  	if (upcall->im_type != im->im_type || upcall->im_conv != im->im_conv)
  		goto out;
57e62324e   Bryan Schumaker   NFS: Store the le...
603
604
  	switch (im->im_conv) {
  	case IDMAP_CONV_NAMETOID:
0cac12023   Trond Myklebust   NFSv4: Ensure tha...
605
606
  		if (strcmp(upcall->im_name, im->im_name) != 0)
  			break;
cf4ab538f   Trond Myklebust   NFSv4: Fix the st...
607
608
609
  		/* Note: here we store the NUL terminator too */
  		len = sprintf(id_str, "%d", im->im_id) + 1;
  		ret = nfs_idmap_instantiate(key, authkey, id_str, len);
57e62324e   Bryan Schumaker   NFS: Store the le...
610
611
  		break;
  	case IDMAP_CONV_IDTONAME:
0cac12023   Trond Myklebust   NFSv4: Ensure tha...
612
613
  		if (upcall->im_id != im->im_id)
  			break;
cf4ab538f   Trond Myklebust   NFSv4: Fix the st...
614
615
  		len = strlen(im->im_name);
  		ret = nfs_idmap_instantiate(key, authkey, im->im_name, len);
57e62324e   Bryan Schumaker   NFS: Store the le...
616
  		break;
0cac12023   Trond Myklebust   NFSv4: Ensure tha...
617
618
  	default:
  		ret = -EINVAL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
619
  	}
0cac12023   Trond Myklebust   NFSv4: Ensure tha...
620
  out:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
621
622
  	return ret;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
623
624
625
  static ssize_t
  idmap_pipe_downcall(struct file *filp, const char __user *src, size_t mlen)
  {
496ad9aa8   Al Viro   new helper: file_...
626
  	struct rpc_inode *rpci = RPC_I(file_inode(filp));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
627
  	struct idmap *idmap = (struct idmap *)rpci->private;
a427b9ec4   David Howells   NFS: Fix a number...
628
  	struct key_construction *cons;
57e62324e   Bryan Schumaker   NFS: Store the le...
629
  	struct idmap_msg im;
d24aae41b   Chuck Lever   NFS: Use size_t f...
630
  	size_t namelen_in;
e9ab41b62   Trond Myklebust   NFSv4: Clean up t...
631
  	int ret = -ENOKEY;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
632

a427b9ec4   David Howells   NFS: Fix a number...
633
634
635
636
  	/* If instantiation is successful, anyone waiting for key construction
  	 * will have been woken up and someone else may now have used
  	 * idmap_key_cons - so after this point we may no longer touch it.
  	 */
0cac12023   Trond Myklebust   NFSv4: Ensure tha...
637
  	if (idmap->idmap_upcall_data == NULL)
e9ab41b62   Trond Myklebust   NFSv4: Clean up t...
638
  		goto out_noupcall;
a427b9ec4   David Howells   NFS: Fix a number...
639

0cac12023   Trond Myklebust   NFSv4: Ensure tha...
640
  	cons = idmap->idmap_upcall_data->key_cons;
a427b9ec4   David Howells   NFS: Fix a number...
641

57e62324e   Bryan Schumaker   NFS: Store the le...
642
643
  	if (mlen != sizeof(im)) {
  		ret = -ENOSPC;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
644
645
  		goto out;
  	}
57e62324e   Bryan Schumaker   NFS: Store the le...
646
647
  	if (copy_from_user(&im, src, mlen) != 0) {
  		ret = -EFAULT;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
648
  		goto out;
57e62324e   Bryan Schumaker   NFS: Store the le...
649
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
650

57e62324e   Bryan Schumaker   NFS: Store the le...
651
  	if (!(im.im_status & IDMAP_STATUS_SUCCESS)) {
12dfd0805   Bryan Schumaker   NFS: return -ENOK...
652
653
  		ret = -ENOKEY;
  		goto out;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
654
  	}
57e62324e   Bryan Schumaker   NFS: Store the le...
655
656
657
  	namelen_in = strnlen(im.im_name, IDMAP_NAMESZ);
  	if (namelen_in == 0 || namelen_in == IDMAP_NAMESZ) {
  		ret = -EINVAL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
658
  		goto out;
0cac12023   Trond Myklebust   NFSv4: Ensure tha...
659
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
660

0cac12023   Trond Myklebust   NFSv4: Ensure tha...
661
662
663
  	ret = nfs_idmap_read_and_verify_message(&im,
  			&idmap->idmap_upcall_data->idmap_msg,
  			cons->key, cons->authkey);
57e62324e   Bryan Schumaker   NFS: Store the le...
664
665
666
667
  	if (ret >= 0) {
  		key_set_timeout(cons->key, nfs_idmap_cache_timeout);
  		ret = mlen;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
668
  out:
e9ab41b62   Trond Myklebust   NFSv4: Clean up t...
669
670
  	nfs_idmap_complete_pipe_upcall_locked(idmap, ret);
  out_noupcall:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
671
672
  	return ret;
  }
75c96f858   Adrian Bunk   [PATCH] make some...
673
  static void
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
674
675
  idmap_pipe_destroy_msg(struct rpc_pipe_msg *msg)
  {
c5066945b   Bryan Schumaker   NFS: Clear key co...
676
677
678
679
  	struct idmap_legacy_upcalldata *data = container_of(msg,
  			struct idmap_legacy_upcalldata,
  			pipe_msg);
  	struct idmap *idmap = data->idmap;
e9ab41b62   Trond Myklebust   NFSv4: Clean up t...
680
681
682
  
  	if (msg->errno)
  		nfs_idmap_abort_pipe_upcall(idmap, msg->errno);
c5066945b   Bryan Schumaker   NFS: Clear key co...
683
684
685
686
687
688
689
  }
  
  static void
  idmap_release_pipe(struct inode *inode)
  {
  	struct rpc_inode *rpci = RPC_I(inode);
  	struct idmap *idmap = (struct idmap *)rpci->private;
e9ab41b62   Trond Myklebust   NFSv4: Clean up t...
690
691
  
  	nfs_idmap_abort_pipe_upcall(idmap, -EPIPE);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
692
  }
9f309c86c   Eric W. Biederman   nfs: Convert idma...
693
  int nfs_map_name_to_uid(const struct nfs_server *server, const char *name, size_t namelen, kuid_t *uid)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
694
  {
e4fd72a17   Trond Myklebust   NFSv4: cleanup id...
695
  	struct idmap *idmap = server->nfs_client->cl_idmap;
9f309c86c   Eric W. Biederman   nfs: Convert idma...
696
697
  	__u32 id = -1;
  	int ret = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
698

9f309c86c   Eric W. Biederman   nfs: Convert idma...
699
700
701
702
703
704
705
  	if (!nfs_map_string_to_numeric(name, namelen, &id))
  		ret = nfs_idmap_lookup_id(name, namelen, "uid", &id, idmap);
  	if (ret == 0) {
  		*uid = make_kuid(&init_user_ns, id);
  		if (!uid_valid(*uid))
  			ret = -ERANGE;
  	}
1f2d30b53   Trond Myklebust   NFSv4: Add tracep...
706
  	trace_nfs4_map_name_to_uid(name, namelen, id, ret);
9f309c86c   Eric W. Biederman   nfs: Convert idma...
707
  	return ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
708
  }
9f309c86c   Eric W. Biederman   nfs: Convert idma...
709
  int nfs_map_group_to_gid(const struct nfs_server *server, const char *name, size_t namelen, kgid_t *gid)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
710
  {
e4fd72a17   Trond Myklebust   NFSv4: cleanup id...
711
  	struct idmap *idmap = server->nfs_client->cl_idmap;
9f309c86c   Eric W. Biederman   nfs: Convert idma...
712
713
  	__u32 id = -1;
  	int ret = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
714

9f309c86c   Eric W. Biederman   nfs: Convert idma...
715
716
717
718
719
720
721
  	if (!nfs_map_string_to_numeric(name, namelen, &id))
  		ret = nfs_idmap_lookup_id(name, namelen, "gid", &id, idmap);
  	if (ret == 0) {
  		*gid = make_kgid(&init_user_ns, id);
  		if (!gid_valid(*gid))
  			ret = -ERANGE;
  	}
1f2d30b53   Trond Myklebust   NFSv4: Add tracep...
722
  	trace_nfs4_map_group_to_gid(name, namelen, id, ret);
9f309c86c   Eric W. Biederman   nfs: Convert idma...
723
  	return ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
724
  }
9f309c86c   Eric W. Biederman   nfs: Convert idma...
725
  int nfs_map_uid_to_name(const struct nfs_server *server, kuid_t uid, char *buf, size_t buflen)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
726
  {
e4fd72a17   Trond Myklebust   NFSv4: cleanup id...
727
  	struct idmap *idmap = server->nfs_client->cl_idmap;
b064eca2c   Trond Myklebust   NFSv4: Send unmap...
728
  	int ret = -EINVAL;
9f309c86c   Eric W. Biederman   nfs: Convert idma...
729
  	__u32 id;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
730

9f309c86c   Eric W. Biederman   nfs: Convert idma...
731
  	id = from_kuid(&init_user_ns, uid);
b064eca2c   Trond Myklebust   NFSv4: Send unmap...
732
  	if (!(server->caps & NFS_CAP_UIDGID_NOMAP))
9f309c86c   Eric W. Biederman   nfs: Convert idma...
733
  		ret = nfs_idmap_lookup_name(id, "user", buf, buflen, idmap);
f0b851689   Trond Myklebust   NFSv4: Send unmap...
734
  	if (ret < 0)
9f309c86c   Eric W. Biederman   nfs: Convert idma...
735
  		ret = nfs_map_numeric_to_string(id, buf, buflen);
1f2d30b53   Trond Myklebust   NFSv4: Add tracep...
736
  	trace_nfs4_map_uid_to_name(buf, ret, id, ret);
f0b851689   Trond Myklebust   NFSv4: Send unmap...
737
  	return ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
738
  }
9f309c86c   Eric W. Biederman   nfs: Convert idma...
739
  int nfs_map_gid_to_group(const struct nfs_server *server, kgid_t gid, char *buf, size_t buflen)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
740
  {
e4fd72a17   Trond Myklebust   NFSv4: cleanup id...
741
  	struct idmap *idmap = server->nfs_client->cl_idmap;
b064eca2c   Trond Myklebust   NFSv4: Send unmap...
742
  	int ret = -EINVAL;
9f309c86c   Eric W. Biederman   nfs: Convert idma...
743
  	__u32 id;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
744

9f309c86c   Eric W. Biederman   nfs: Convert idma...
745
  	id = from_kgid(&init_user_ns, gid);
b064eca2c   Trond Myklebust   NFSv4: Send unmap...
746
  	if (!(server->caps & NFS_CAP_UIDGID_NOMAP))
9f309c86c   Eric W. Biederman   nfs: Convert idma...
747
  		ret = nfs_idmap_lookup_name(id, "group", buf, buflen, idmap);
f0b851689   Trond Myklebust   NFSv4: Send unmap...
748
  	if (ret < 0)
9f309c86c   Eric W. Biederman   nfs: Convert idma...
749
  		ret = nfs_map_numeric_to_string(id, buf, buflen);
1f2d30b53   Trond Myklebust   NFSv4: Add tracep...
750
  	trace_nfs4_map_gid_to_group(buf, ret, id, ret);
f0b851689   Trond Myklebust   NFSv4: Send unmap...
751
  	return ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
752
  }