Whamcloud - gitweb
f90de1fc93cc904ef97a2cc822b28616d08b0044
[fs/lustre-release.git] / lustre / llite / remote_perm.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/llite/remote_perm.c
37  *
38  * Lustre Permission Cache for Remote Client
39  *
40  * Author: Lai Siyao <lsy@clusterfs.com>
41  * Author: Fan Yong <fanyong@clusterfs.com>
42  */
43
44 #define DEBUG_SUBSYSTEM S_LLITE
45
46 #include <linux/module.h>
47 #include <linux/types.h>
48 #include <linux/version.h>
49
50 #include <lustre_lite.h>
51 #include <lustre_ha.h>
52 #include <lustre_dlm.h>
53 #include <lprocfs_status.h>
54 #include <lustre_disk.h>
55 #include <lustre_param.h>
56 #include "llite_internal.h"
57
58 cfs_mem_cache_t *ll_remote_perm_cachep = NULL;
59 cfs_mem_cache_t *ll_rmtperm_hash_cachep = NULL;
60
61 static inline struct ll_remote_perm *alloc_ll_remote_perm(void)
62 {
63         struct ll_remote_perm *lrp;
64
65         OBD_SLAB_ALLOC_PTR_GFP(lrp, ll_remote_perm_cachep, GFP_KERNEL);
66         if (lrp)
67                 CFS_INIT_HLIST_NODE(&lrp->lrp_list);
68         return lrp;
69 }
70
71 static inline void free_ll_remote_perm(struct ll_remote_perm *lrp)
72 {
73         if (!lrp)
74                 return;
75
76         if (!cfs_hlist_unhashed(&lrp->lrp_list))
77                 cfs_hlist_del(&lrp->lrp_list);
78         OBD_SLAB_FREE(lrp, ll_remote_perm_cachep, sizeof(*lrp));
79 }
80
81 cfs_hlist_head_t *alloc_rmtperm_hash(void)
82 {
83         cfs_hlist_head_t *hash;
84         int i;
85
86         OBD_SLAB_ALLOC(hash, ll_rmtperm_hash_cachep, GFP_KERNEL,
87                        REMOTE_PERM_HASHSIZE * sizeof(*hash));
88
89         if (!hash)
90                 return NULL;
91
92         for (i = 0; i < REMOTE_PERM_HASHSIZE; i++)
93                 CFS_INIT_HLIST_HEAD(hash + i);
94
95         return hash;
96 }
97
98 void free_rmtperm_hash(cfs_hlist_head_t *hash)
99 {
100         int i;
101         struct ll_remote_perm *lrp;
102         cfs_hlist_node_t *node, *next;
103
104         if(!hash)
105                 return;
106
107         for (i = 0; i < REMOTE_PERM_HASHSIZE; i++)
108                 cfs_hlist_for_each_entry_safe(lrp, node, next, hash + i,
109                                               lrp_list)
110                         free_ll_remote_perm(lrp);
111         OBD_SLAB_FREE(hash, ll_rmtperm_hash_cachep,
112                       REMOTE_PERM_HASHSIZE * sizeof(*hash));
113 }
114
115 static inline int remote_perm_hashfunc(uid_t uid)
116 {
117         return uid & (REMOTE_PERM_HASHSIZE - 1);
118 }
119
120 /* NB: setxid permission is not checked here, instead it's done on
121  * MDT when client get remote permission. */
122 static int do_check_remote_perm(struct ll_inode_info *lli, int mask)
123 {
124         cfs_hlist_head_t *head;
125         struct ll_remote_perm *lrp;
126         cfs_hlist_node_t *node;
127         int found = 0, rc;
128         ENTRY;
129
130         if (!lli->lli_remote_perms)
131                 RETURN(-ENOENT);
132
133         head = lli->lli_remote_perms + remote_perm_hashfunc(cfs_curproc_uid());
134
135         cfs_spin_lock(&lli->lli_lock);
136         cfs_hlist_for_each_entry(lrp, node, head, lrp_list) {
137                 if (lrp->lrp_uid != cfs_curproc_uid())
138                         continue;
139                 if (lrp->lrp_gid != cfs_curproc_gid())
140                         continue;
141                 if (lrp->lrp_fsuid != cfs_curproc_fsuid())
142                         continue;
143                 if (lrp->lrp_fsgid != cfs_curproc_fsgid())
144                         continue;
145                 found = 1;
146                 break;
147         }
148
149         if (!found)
150                 GOTO(out, rc = -ENOENT);
151
152         CDEBUG(D_SEC, "found remote perm: %u/%u/%u/%u - %#x\n",
153                lrp->lrp_uid, lrp->lrp_gid, lrp->lrp_fsuid, lrp->lrp_fsgid,
154                lrp->lrp_access_perm);
155         rc = ((lrp->lrp_access_perm & mask) == mask) ? 0 : -EACCES;
156
157 out:
158         cfs_spin_unlock(&lli->lli_lock);
159         return rc;
160 }
161
162 int ll_update_remote_perm(struct inode *inode, struct mdt_remote_perm *perm)
163 {
164         struct ll_inode_info *lli = ll_i2info(inode);
165         struct ll_remote_perm *lrp = NULL, *tmp = NULL;
166         cfs_hlist_head_t *head, *perm_hash = NULL;
167         cfs_hlist_node_t *node;
168         ENTRY;
169
170         LASSERT(ll_i2sbi(inode)->ll_flags & LL_SBI_RMT_CLIENT);
171
172 #if 0
173         if (perm->rp_uid != current->uid ||
174             perm->rp_gid != current->gid ||
175             perm->rp_fsuid != current->fsuid ||
176             perm->rp_fsgid != current->fsgid) {
177                 /* user might setxid in this small period */
178                 CDEBUG(D_SEC,
179                        "remote perm user %u/%u/%u/%u != current %u/%u/%u/%u\n",
180                        perm->rp_uid, perm->rp_gid, perm->rp_fsuid,
181                        perm->rp_fsgid, current->uid, current->gid,
182                        current->fsuid, current->fsgid);
183                 RETURN(-EAGAIN);
184         }
185 #endif
186
187         if (!lli->lli_remote_perms) {
188                 perm_hash = alloc_rmtperm_hash();
189                 if (perm_hash == NULL) {
190                         CERROR("alloc lli_remote_perms failed!\n");
191                         RETURN(-ENOMEM);
192                 }
193         }
194
195         cfs_spin_lock(&lli->lli_lock);
196
197         if (!lli->lli_remote_perms)
198                 lli->lli_remote_perms = perm_hash;
199         else if (perm_hash)
200                 free_rmtperm_hash(perm_hash);
201
202         head = lli->lli_remote_perms + remote_perm_hashfunc(perm->rp_uid);
203
204 again:
205         cfs_hlist_for_each_entry(tmp, node, head, lrp_list) {
206                 if (tmp->lrp_uid != perm->rp_uid)
207                         continue;
208                 if (tmp->lrp_gid != perm->rp_gid)
209                         continue;
210                 if (tmp->lrp_fsuid != perm->rp_fsuid)
211                         continue;
212                 if (tmp->lrp_fsgid != perm->rp_fsgid)
213                         continue;
214                 if (lrp)
215                         free_ll_remote_perm(lrp);
216                 lrp = tmp;
217                 break;
218         }
219
220         if (!lrp) {
221                 cfs_spin_unlock(&lli->lli_lock);
222                 lrp = alloc_ll_remote_perm();
223                 if (!lrp) {
224                         CERROR("alloc memory for ll_remote_perm failed!\n");
225                         RETURN(-ENOMEM);
226                 }
227                 cfs_spin_lock(&lli->lli_lock);
228                 goto again;
229         }
230
231         lrp->lrp_access_perm = perm->rp_access_perm;
232         if (lrp != tmp) {
233                 lrp->lrp_uid         = perm->rp_uid;
234                 lrp->lrp_gid         = perm->rp_gid;
235                 lrp->lrp_fsuid       = perm->rp_fsuid;
236                 lrp->lrp_fsgid       = perm->rp_fsgid;
237                 cfs_hlist_add_head(&lrp->lrp_list, head);
238         }
239         lli->lli_rmtperm_utime = jiffies;
240         cfs_spin_unlock(&lli->lli_lock);
241
242         CDEBUG(D_SEC, "new remote perm@%p: %u/%u/%u/%u - %#x\n",
243                lrp, lrp->lrp_uid, lrp->lrp_gid, lrp->lrp_fsuid, lrp->lrp_fsgid,
244                lrp->lrp_access_perm);
245
246         RETURN(0);
247 }
248
249 int lustre_check_remote_perm(struct inode *inode, int mask)
250 {
251         struct ll_inode_info *lli = ll_i2info(inode);
252         struct ll_sb_info *sbi = ll_i2sbi(inode);
253         struct ptlrpc_request *req = NULL;
254         struct mdt_remote_perm *perm;
255         struct obd_capa *oc;
256         unsigned long utime;
257         int i = 0, rc;
258         ENTRY;
259
260         do {
261                 utime = lli->lli_rmtperm_utime;
262                 rc = do_check_remote_perm(lli, mask);
263                 if (!rc || (rc != -ENOENT && i))
264                         break;
265
266                 cfs_might_sleep();
267
268                 cfs_down(&lli->lli_rmtperm_sem);
269                 /* check again */
270                 if (utime != lli->lli_rmtperm_utime) {
271                         rc = do_check_remote_perm(lli, mask);
272                         if (!rc || (rc != -ENOENT && i)) {
273                                 cfs_up(&lli->lli_rmtperm_sem);
274                                 break;
275                         }
276                 }
277
278                 if (i++ > 5) {
279                         CERROR("check remote perm falls in dead loop!\n");
280                         LBUG();
281                 }
282
283                 oc = ll_mdscapa_get(inode);
284                 rc = md_get_remote_perm(sbi->ll_md_exp, ll_inode2fid(inode), oc,
285                                         ll_i2suppgid(inode), &req);
286                 capa_put(oc);
287                 if (rc) {
288                         cfs_up(&lli->lli_rmtperm_sem);
289                         break;
290                 }
291
292                 perm = req_capsule_server_swab_get(&req->rq_pill, &RMF_ACL,
293                                                    lustre_swab_mdt_remote_perm);
294                 if (unlikely(perm == NULL)) {
295                         cfs_up(&lli->lli_rmtperm_sem);
296                         rc = -EPROTO;
297                         break;
298                 }
299
300                 rc = ll_update_remote_perm(inode, perm);
301                 cfs_up(&lli->lli_rmtperm_sem);
302                 if (rc == -ENOMEM)
303                         break;
304
305                 ptlrpc_req_finished(req);
306                 req = NULL;
307         } while (1);
308         ptlrpc_req_finished(req);
309         RETURN(rc);
310 }
311
312 #if 0  /* NB: remote perms can't be freed in ll_mdc_blocking_ast of UPDATE lock,
313         * because it will fail sanity test 48.
314         */
315 void ll_free_remote_perms(struct inode *inode)
316 {
317         struct ll_inode_info *lli = ll_i2info(inode);
318         cfs_hlist_head_t *hash = lli->lli_remote_perms;
319         struct ll_remote_perm *lrp;
320         cfs_hlist_node_t *node, *next;
321         int i;
322
323         LASSERT(hash);
324
325         cfs_spin_lock(&lli->lli_lock);
326
327         for (i = 0; i < REMOTE_PERM_HASHSIZE; i++) {
328                 cfs_hlist_for_each_entry_safe(lrp, node, next, hash + i,
329                                               lrp_list)
330                         free_ll_remote_perm(lrp);
331         }
332
333         cfs_spin_unlock(&lli->lli_lock);
334 }
335 #endif