Skip to content

Commit 58df095

Browse files
Trond MyklebustTrond Myklebust
authored andcommitted
NFSv4: Allow entries in the idmap cache to expire
If someone changes the uid/gid mapping in userland, then we do eventually want those changes to be propagated to the kernel. Currently the kernel assumes that it may cache entries forever. Add an expiration time + garbage collector for idmap entries. Signed-off-by: Trond Myklebust <Trond.Myklebust@netapp.com>
1 parent 0065db3 commit 58df095

5 files changed

Lines changed: 39 additions & 0 deletions

File tree

‎Documentation/kernel-parameters.txt‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -914,6 +914,10 @@ running once the system is up.
914914
[NFS] set the TCP port on which the NFSv4 callback
915915
channel should listen.
916916

917+
nfs.idmap_cache_timeout=
918+
[NFS] set the maximum lifetime for idmapper cache
919+
entries.
920+
917921
nmi_watchdog= [KNL,BUGS=IA-32] Debugging features for SMP kernels
918922

919923
no387 [BUGS=IA-32] Tells the kernel to use the 387 maths

‎fs/nfs/idmap.c‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,11 @@
5454

5555
#define IDMAP_HASH_SZ 128
5656

57+
/* Default cache timeout is 10 minutes */
58+
unsigned int nfs_idmap_cache_timeout = 600 * HZ;
59+
5760
struct idmap_hashent {
61+
unsigned long ih_expires;
5862
__u32 ih_id;
5963
int ih_namelen;
6064
char ih_name[IDMAP_NAMESZ];
@@ -149,6 +153,8 @@ idmap_lookup_name(struct idmap_hashtable *h, const char *name, size_t len)
149153

150154
if (he->ih_namelen != len || memcmp(he->ih_name, name, len) != 0)
151155
return NULL;
156+
if (time_after(jiffies, he->ih_expires))
157+
return NULL;
152158
return he;
153159
}
154160

@@ -164,6 +170,8 @@ idmap_lookup_id(struct idmap_hashtable *h, __u32 id)
164170
struct idmap_hashent *he = idmap_id_hash(h, id);
165171
if (he->ih_id != id || he->ih_namelen == 0)
166172
return NULL;
173+
if (time_after(jiffies, he->ih_expires))
174+
return NULL;
167175
return he;
168176
}
169177

@@ -192,6 +200,7 @@ idmap_update_entry(struct idmap_hashent *he, const char *name,
192200
memcpy(he->ih_name, name, namelen);
193201
he->ih_name[namelen] = '\0';
194202
he->ih_namelen = namelen;
203+
he->ih_expires = jiffies + nfs_idmap_cache_timeout;
195204
}
196205

197206
/*

‎fs/nfs/inode.c‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2050,6 +2050,20 @@ static int param_set_port(const char *val, struct kernel_param *kp)
20502050
module_param_call(callback_tcpport, param_set_port, param_get_int,
20512051
&nfs_callback_set_tcpport, 0644);
20522052

2053+
static int param_set_idmap_timeout(const char *val, struct kernel_param *kp)
2054+
{
2055+
char *endp;
2056+
int num = simple_strtol(val, &endp, 0);
2057+
int jif = num * HZ;
2058+
if (endp == val || *endp || num < 0 || jif < num)
2059+
return -EINVAL;
2060+
*((int *)kp->arg) = jif;
2061+
return 0;
2062+
}
2063+
2064+
module_param_call(idmap_cache_timeout, param_set_idmap_timeout, param_get_int,
2065+
&nfs_idmap_cache_timeout, 0644);
2066+
20532067
#define nfs4_init_once(nfsi) \
20542068
do { \
20552069
INIT_LIST_HEAD(&(nfsi)->open_states); \

‎fs/nfs/sysctl.c‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <linux/sysctl.h>
1212
#include <linux/module.h>
1313
#include <linux/nfs4.h>
14+
#include <linux/nfs_idmap.h>
1415

1516
#include "callback.h"
1617

@@ -35,6 +36,15 @@ static ctl_table nfs_cb_sysctls[] = {
3536
.extra1 = (int *)&nfs_set_port_min,
3637
.extra2 = (int *)&nfs_set_port_max,
3738
},
39+
{
40+
.ctl_name = CTL_UNNUMBERED,
41+
.procname = "idmap_cache_timeout",
42+
.data = &nfs_idmap_cache_timeout,
43+
.maxlen = sizeof(int),
44+
.mode = 0644,
45+
.proc_handler = &proc_dointvec_jiffies,
46+
.strategy = &sysctl_jiffies,
47+
},
3848
#endif
3949
{ .ctl_name = 0 }
4050
};

‎include/linux/nfs_idmap.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ int nfs_map_name_to_uid(struct nfs4_client *, const char *, size_t, __u32 *);
7171
int nfs_map_group_to_gid(struct nfs4_client *, const char *, size_t, __u32 *);
7272
int nfs_map_uid_to_name(struct nfs4_client *, __u32, char *);
7373
int nfs_map_gid_to_group(struct nfs4_client *, __u32, char *);
74+
75+
extern unsigned int nfs_idmap_cache_timeout;
7476
#endif /* __KERNEL__ */
7577

7678
#endif /* NFS_IDMAP_H */

0 commit comments

Comments
 (0)