Whamcloud - gitweb
b=22310 temporary fix: align readahead window end to 1M rpc boundary.
[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  2008 Sun Microsystems, Inc. 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/random.h>
49 #include <linux/version.h>
50
51 #include <lustre_lite.h>
52 #include <lustre_ha.h>
53 #include <lustre_dlm.h>
54 #include <lprocfs_status.h>
55 #include <lustre_disk.h>
56 #include <lustre_param.h>
57 #include "llite_internal.h"
58
59 cfs_mem_cache_t *ll_remote_perm_cachep = NULL;
60 cfs_mem_cache_t *ll_rmtperm_hash_cachep = NULL;
61
62 static inline struct ll_remote_perm *alloc_ll_remote_perm(void)
63 {
64         struct ll_remote_perm *lrp;
65
66         OBD_SLAB_ALLOC_PTR_GFP(lrp, ll_remote_perm_cachep, GFP_KERNEL);
67         if (lrp)
68                 CFS_INIT_HLIST_NODE(&lrp->lrp_list);
69         return lrp;
70 }
71
72 static inline void free_ll_remote_perm(struct ll_remote_perm *lrp)
73 {
74         if (!lrp)
75                 return;
76
77         if (!cfs_hlist_unhashed(&lrp->lrp_list))
78                 cfs_hlist_del(&lrp->lrp_list);
79         OBD_SLAB_FREE(lrp, ll_remote_perm_cachep, sizeof(*lrp));
80 }
81
82 cfs_hlist_head_t *alloc_rmtperm_hash(void)
83 {
84         cfs_hlist_head_t *hash;
85         int i;
86
87         OBD_SLAB_ALLOC(hash, ll_rmtperm_hash_cachep, GFP_KERNEL,
88                        REMOTE_PERM_HASHSIZE * sizeof(*hash));
89
90         if (!hash)
91                 return NULL;
92
93         for (i = 0; i < REMOTE_PERM_HASHSIZE; i++)
94                 CFS_INIT_HLIST_HEAD(hash + i);
95
96         return hash;
97 }
98
99 void free_rmtperm_hash(cfs_hlist_head_t *hash)
100 {
101         int i;
102         struct ll_remote_perm *lrp;
103         cfs_hlist_node_t *node, *next;
104
105         if(!hash)
106                 return;
107
108         for (i = 0; i < REMOTE_PERM_HASHSIZE; i++)
109                 cfs_hlist_for_each_entry_safe(lrp, node, next, hash + i,
110                                               lrp_list)
111                         free_ll_remote_perm(lrp);
112         OBD_SLAB_FREE(hash, ll_rmtperm_hash_cachep,
113                       REMOTE_PERM_HASHSIZE * sizeof(*hash));
114 }
115
116 static inline int remote_perm_hashfunc(uid_t uid)
117 {
118         return uid & (REMOTE_PERM_HASHSIZE - 1);
119 }
120
121 /* NB: setxid permission is not checked here, instead it's done on
122  * MDT when client get remote permission. */
123 static int do_check_remote_perm(struct ll_inode_info *lli, int mask)
124 {
125         cfs_hlist_head_t *head;
126         struct ll_remote_perm *lrp;
127         cfs_hlist_node_t *node;
128         int found = 0, rc;
129         ENTRY;
130
131         if (!lli->lli_remote_perms)
132                 RETURN(-ENOENT);
133
134         head = lli->lli_remote_perms + remote_perm_hashfunc(cfs_curproc_uid());
135
136         cfs_spin_lock(&lli->lli_lock);
137         cfs_hlist_for_each_entry(lrp, node, head, lrp_list) {
138                 if (lrp->lrp_uid != cfs_curproc_uid())
139                         continue;
140                 if (lrp->lrp_gid != cfs_curproc_gid())
141                         continue;
142                 if (lrp->lrp_fsuid != cfs_curproc_fsuid())
143                         continue;
144                 if (lrp->lrp_fsgid != cfs_curproc_fsgid())
145                         continue;
146                 found = 1;
147                 break;
148         }
149
150         if (!found)
151                 GOTO(out, rc = -ENOENT);
152
153         CDEBUG(D_SEC, "found remote perm: %u/%u/%u/%u - %#x\n",
154                lrp->lrp_uid, lrp->lrp_gid, lrp->lrp_fsuid, lrp->lrp_fsgid,
155                lrp->lrp_access_perm);
156         rc = ((lrp->lrp_access_perm & mask) == mask) ? 0 : -EACCES;
157
158 out:
159         cfs_spin_unlock(&lli->lli_lock);
160         return rc;
161 }
162
163 int ll_update_remote_perm(struct inode *inode, struct mdt_remote_perm *perm)
164 {
165         struct ll_inode_info *lli = ll_i2info(inode);
166         struct ll_remote_perm *lrp = NULL, *tmp = NULL;
167         cfs_hlist_head_t *head, *perm_hash = NULL;
168         cfs_hlist_node_t *node;
169         ENTRY;
170
171         LASSERT(ll_i2sbi(inode)->ll_flags & LL_SBI_RMT_CLIENT);
172
173 #if 0
174         if (perm->rp_uid != current->uid ||
175             perm->rp_gid != current->gid ||
176             perm->rp_fsuid != current->fsuid ||
177             perm->rp_fsgid != current->fsgid) {
178                 /* user might setxid in this small period */
179                 CDEBUG(D_SEC,
180                        "remote perm user %u/%u/%u/%u != current %u/%u/%u/%u\n",
181                        perm->rp_uid, perm->rp_gid, perm->rp_fsuid,
182                        perm->rp_fsgid, current->uid, current->gid,
183                        current->fsuid, current->fsgid);
184                 RETURN(-EAGAIN);
185         }
186 #endif
187
188         if (!lli->lli_remote_perms) {
189                 perm_hash = alloc_rmtperm_hash();
190                 if (perm_hash == NULL) {
191                         CERROR("alloc lli_remote_perms failed!\n");
192                         RETURN(-ENOMEM);
193                 }
194         }
195
196         cfs_spin_lock(&lli->lli_lock);
197
198         if (!lli->lli_remote_perms)
199                 lli->lli_remote_perms = perm_hash;
200         else if (perm_hash)
201                 free_rmtperm_hash(perm_hash);
202
203         head = lli->lli_remote_perms + remote_perm_hashfunc(perm->rp_uid);
204
205 again:
206         cfs_hlist_for_each_entry(tmp, node, head, lrp_list) {
207                 if (tmp->lrp_uid != perm->rp_uid)
208                         continue;
209                 if (tmp->lrp_gid != perm->rp_gid)
210                         continue;
211                 if (tmp->lrp_fsuid != perm->rp_fsuid)
212                         continue;
213                 if (tmp->lrp_fsgid != perm->rp_fsgid)
214                         continue;
215                 if (lrp)
216                         free_ll_remote_perm(lrp);
217                 lrp = tmp;
218                 break;
219         }
220
221         if (!lrp) {
222                 cfs_spin_unlock(&lli->lli_lock);
223                 lrp = alloc_ll_remote_perm();
224                 if (!lrp) {
225                         CERROR("alloc memory for ll_remote_perm failed!\n");
226                         RETURN(-ENOMEM);
227                 }
228                 cfs_spin_lock(&lli->lli_lock);
229                 goto again;
230         }
231
232         lrp->lrp_access_perm = perm->rp_access_perm;
233         if (lrp != tmp) {
234                 lrp->lrp_uid         = perm->rp_uid;
235                 lrp->lrp_gid         = perm->rp_gid;
236                 lrp->lrp_fsuid       = perm->rp_fsuid;
237                 lrp->lrp_fsgid       = perm->rp_fsgid;
238                 cfs_hlist_add_head(&lrp->lrp_list, head);
239         }
240         lli->lli_rmtperm_utime = jiffies;
241         cfs_spin_unlock(&lli->lli_lock);
242
243         CDEBUG(D_SEC, "new remote perm@%p: %u/%u/%u/%u - %#x\n",
244                lrp, lrp->lrp_uid, lrp->lrp_gid, lrp->lrp_fsuid, lrp->lrp_fsgid,
245                lrp->lrp_access_perm);
246
247         RETURN(0);
248 }
249
250 int lustre_check_remote_perm(struct inode *inode, int mask)
251 {
252         struct ll_inode_info *lli = ll_i2info(inode);
253         struct ll_sb_info *sbi = ll_i2sbi(inode);
254         struct ptlrpc_request *req = NULL;
255         struct mdt_remote_perm *perm;
256         struct obd_capa *oc;
257         unsigned long utime;
258         int i = 0, rc;
259         ENTRY;
260
261         do {
262                 utime = lli->lli_rmtperm_utime;
263                 rc = do_check_remote_perm(lli, mask);
264                 if (!rc || (rc != -ENOENT && i))
265                         break;
266
267                 cfs_might_sleep();
268
269                 cfs_down(&lli->lli_rmtperm_sem);
270                 /* check again */
271                 if (utime != lli->lli_rmtperm_utime) {
272                         rc = do_check_remote_perm(lli, mask);
273                         if (!rc || (rc != -ENOENT && i)) {
274                                 cfs_up(&lli->lli_rmtperm_sem);
275                                 break;
276                         }
277                 }
278
279                 if (i++ > 5) {
280                         CERROR("check remote perm falls in dead loop!\n");
281                         LBUG();
282                 }
283
284                 oc = ll_mdscapa_get(inode);
285                 rc = md_get_remote_perm(sbi->ll_md_exp, ll_inode2fid(inode), oc,
286                                         ll_i2suppgid(inode), &req);
287                 capa_put(oc);
288                 if (rc) {
289                         cfs_up(&lli->lli_rmtperm_sem);
290                         break;
291                 }
292
293                 perm = req_capsule_server_swab_get(&req->rq_pill, &RMF_ACL,
294                                                    lustre_swab_mdt_remote_perm);
295                 if (unlikely(perm == NULL)) {
296                         cfs_up(&lli->lli_rmtperm_sem);
297                         rc = -EPROTO;
298                         break;
299                 }
300
301                 rc = ll_update_remote_perm(inode, perm);
302                 cfs_up(&lli->lli_rmtperm_sem);
303                 if (rc == -ENOMEM)
304                         break;
305
306                 ptlrpc_req_finished(req);
307                 req = NULL;
308         } while (1);
309         ptlrpc_req_finished(req);
310         RETURN(rc);
311 }
312
313 #if 0  /* NB: remote perms can't be freed in ll_mdc_blocking_ast of UPDATE lock,
314         * because it will fail sanity test 48.
315         */
316 void ll_free_remote_perms(struct inode *inode)
317 {
318         struct ll_inode_info *lli = ll_i2info(inode);
319         cfs_hlist_head_t *hash = lli->lli_remote_perms;
320         struct ll_remote_perm *lrp;
321         cfs_hlist_node_t *node, *next;
322         int i;
323
324         LASSERT(hash);
325
326         cfs_spin_lock(&lli->lli_lock);
327
328         for (i = 0; i < REMOTE_PERM_HASHSIZE; i++) {
329                 cfs_hlist_for_each_entry_safe(lrp, node, next, hash + i,
330                                               lrp_list)
331                         free_ll_remote_perm(lrp);
332         }
333
334         cfs_spin_unlock(&lli->lli_lock);
335 }
336 #endif