Whamcloud - gitweb
LU-17744 ldiskfs: mballoc stats fixes
[fs/lustre-release.git] / lustre / obdclass / lu_ucred.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2013, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  *
31  * lustre/obdclass/lu_ucred.c
32  *
33  * Lustre user credentials context infrastructure.
34  *
35  *   Author: Nikita Danilov <nikita.danilov@sun.com>
36  *   Author: Fan Yong <fan.yong@intel.com>
37  *   Author: Vitaly Fertman <vitaly_fertman@xyratex.com>
38  */
39
40 #define DEBUG_SUBSYSTEM S_CLASS
41
42 #include <libcfs/libcfs.h>
43 #include <obd_support.h>
44 #include <lu_object.h>
45 #include <md_object.h>
46
47 /* context key constructor/destructor: lu_ucred_key_init, lu_ucred_key_fini */
48 LU_KEY_INIT_FINI(lu_ucred, struct lu_ucred);
49
50 static struct lu_context_key lu_ucred_key = {
51         .lct_tags = LCT_SERVER_SESSION,
52         .lct_init = lu_ucred_key_init,
53         .lct_fini = lu_ucred_key_fini
54 };
55
56 /**
57  * Get ucred key if session exists and ucred key is allocated on it.
58  * Return NULL otherwise.
59  */
60 struct lu_ucred *lu_ucred(const struct lu_env *env)
61 {
62         if (!env->le_ses)
63                 return NULL;
64         return lu_context_key_get(env->le_ses, &lu_ucred_key);
65 }
66 EXPORT_SYMBOL(lu_ucred);
67
68 /**
69  * Get ucred key and check if it is properly initialized.
70  * Return NULL otherwise.
71  */
72 struct lu_ucred *lu_ucred_check(const struct lu_env *env)
73 {
74         struct lu_ucred *uc = lu_ucred(env);
75         if (uc && uc->uc_valid != UCRED_OLD && uc->uc_valid != UCRED_NEW)
76                 return NULL;
77         return uc;
78 }
79 EXPORT_SYMBOL(lu_ucred_check);
80
81 /**
82  * Get ucred key, which must exist and must be properly initialized.
83  * Assert otherwise.
84  */
85 struct lu_ucred *lu_ucred_assert(const struct lu_env *env)
86 {
87         struct lu_ucred *uc = lu_ucred_check(env);
88         LASSERT(uc != NULL);
89         return uc;
90 }
91 EXPORT_SYMBOL(lu_ucred_assert);
92
93 int lu_ucred_global_init(void)
94 {
95         LU_CONTEXT_KEY_INIT(&lu_ucred_key);
96         return lu_context_key_register(&lu_ucred_key);
97 }
98
99 void lu_ucred_global_fini(void)
100 {
101         lu_context_key_degister(&lu_ucred_key);
102 }