Commit 59e6b9c1 authored by Bryan Schumaker's avatar Bryan Schumaker Committed by Trond Myklebust

Created a function for setting timeouts on keys

The keyctl_set_timeout function isn't exported to other parts of the
kernel, but I want to use it for the NFS idmapper.  I already have the
key, but I wanted a generic way to set the timeout.
Signed-off-by: default avatarBryan Schumaker <bjschuma@netapp.com>
Acked-by: default avatarDavid Howells <dhowells@redhat.com>
Signed-off-by: default avatarTrond Myklebust <Trond.Myklebust@netapp.com>
parent 0cb3284b
...@@ -276,6 +276,8 @@ static inline key_serial_t key_serial(const struct key *key) ...@@ -276,6 +276,8 @@ static inline key_serial_t key_serial(const struct key *key)
return key ? key->serial : 0; return key ? key->serial : 0;
} }
extern void key_set_timeout(struct key *, unsigned);
/** /**
* key_is_instantiated - Determine if a key has been positively instantiated * key_is_instantiated - Determine if a key has been positively instantiated
* @key: The key to check. * @key: The key to check.
......
...@@ -671,6 +671,26 @@ struct key_type *key_type_lookup(const char *type) ...@@ -671,6 +671,26 @@ struct key_type *key_type_lookup(const char *type)
return ktype; return ktype;
} }
void key_set_timeout(struct key *key, unsigned timeout)
{
struct timespec now;
time_t expiry = 0;
/* make the changes with the locks held to prevent races */
down_write(&key->sem);
if (timeout > 0) {
now = current_kernel_time();
expiry = now.tv_sec + timeout;
}
key->expiry = expiry;
key_schedule_gc(key->expiry + key_gc_delay);
up_write(&key->sem);
}
EXPORT_SYMBOL_GPL(key_set_timeout);
/* /*
* Unlock a key type locked by key_type_lookup(). * Unlock a key type locked by key_type_lookup().
*/ */
......
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
#include <linux/sched.h> #include <linux/sched.h>
#include <linux/slab.h> #include <linux/slab.h>
#include <linux/syscalls.h> #include <linux/syscalls.h>
#include <linux/key.h>
#include <linux/keyctl.h> #include <linux/keyctl.h>
#include <linux/fs.h> #include <linux/fs.h>
#include <linux/capability.h> #include <linux/capability.h>
...@@ -1244,10 +1245,8 @@ long keyctl_set_reqkey_keyring(int reqkey_defl) ...@@ -1244,10 +1245,8 @@ long keyctl_set_reqkey_keyring(int reqkey_defl)
*/ */
long keyctl_set_timeout(key_serial_t id, unsigned timeout) long keyctl_set_timeout(key_serial_t id, unsigned timeout)
{ {
struct timespec now;
struct key *key, *instkey; struct key *key, *instkey;
key_ref_t key_ref; key_ref_t key_ref;
time_t expiry;
long ret; long ret;
key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL, key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
...@@ -1273,20 +1272,7 @@ long keyctl_set_timeout(key_serial_t id, unsigned timeout) ...@@ -1273,20 +1272,7 @@ long keyctl_set_timeout(key_serial_t id, unsigned timeout)
okay: okay:
key = key_ref_to_ptr(key_ref); key = key_ref_to_ptr(key_ref);
key_set_timeout(key, timeout);
/* make the changes with the locks held to prevent races */
down_write(&key->sem);
expiry = 0;
if (timeout > 0) {
now = current_kernel_time();
expiry = now.tv_sec + timeout;
}
key->expiry = expiry;
key_schedule_gc(key->expiry + key_gc_delay);
up_write(&key->sem);
key_put(key); key_put(key);
ret = 0; ret = 0;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment