1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44#define DEBUG_SUBSYSTEM S_LLITE
45
46#include <linux/module.h>
47#include <linux/types.h>
48
49#include "../include/lustre_lite.h"
50#include "../include/lustre_ha.h"
51#include "../include/lustre_dlm.h"
52#include "../include/lprocfs_status.h"
53#include "../include/lustre_disk.h"
54#include "../include/lustre_param.h"
55#include "llite_internal.h"
56
57struct kmem_cache *ll_remote_perm_cachep;
58struct kmem_cache *ll_rmtperm_hash_cachep;
59
60static inline struct ll_remote_perm *alloc_ll_remote_perm(void)
61{
62 struct ll_remote_perm *lrp;
63
64 lrp = kmem_cache_zalloc(ll_remote_perm_cachep, GFP_KERNEL);
65 if (lrp)
66 INIT_HLIST_NODE(&lrp->lrp_list);
67 return lrp;
68}
69
70static inline void free_ll_remote_perm(struct ll_remote_perm *lrp)
71{
72 if (!lrp)
73 return;
74
75 if (!hlist_unhashed(&lrp->lrp_list))
76 hlist_del(&lrp->lrp_list);
77 kmem_cache_free(ll_remote_perm_cachep, lrp);
78}
79
80static struct hlist_head *alloc_rmtperm_hash(void)
81{
82 struct hlist_head *hash;
83 int i;
84
85 hash = kmem_cache_zalloc(ll_rmtperm_hash_cachep, GFP_NOFS);
86 if (!hash)
87 return NULL;
88
89 for (i = 0; i < REMOTE_PERM_HASHSIZE; i++)
90 INIT_HLIST_HEAD(hash + i);
91
92 return hash;
93}
94
95void free_rmtperm_hash(struct hlist_head *hash)
96{
97 int i;
98 struct ll_remote_perm *lrp;
99 struct hlist_node *next;
100
101 if (!hash)
102 return;
103
104 for (i = 0; i < REMOTE_PERM_HASHSIZE; i++)
105 hlist_for_each_entry_safe(lrp, next, hash + i, lrp_list)
106 free_ll_remote_perm(lrp);
107 kmem_cache_free(ll_rmtperm_hash_cachep, hash);
108}
109
110static inline int remote_perm_hashfunc(uid_t uid)
111{
112 return uid & (REMOTE_PERM_HASHSIZE - 1);
113}
114
115
116
117
118static int do_check_remote_perm(struct ll_inode_info *lli, int mask)
119{
120 struct hlist_head *head;
121 struct ll_remote_perm *lrp;
122 int found = 0, rc;
123
124 if (!lli->lli_remote_perms)
125 return -ENOENT;
126
127 head = lli->lli_remote_perms +
128 remote_perm_hashfunc(from_kuid(&init_user_ns, current_uid()));
129
130 spin_lock(&lli->lli_lock);
131 hlist_for_each_entry(lrp, head, lrp_list) {
132 if (lrp->lrp_uid != from_kuid(&init_user_ns, current_uid()))
133 continue;
134 if (lrp->lrp_gid != from_kgid(&init_user_ns, current_gid()))
135 continue;
136 if (lrp->lrp_fsuid != from_kuid(&init_user_ns, current_fsuid()))
137 continue;
138 if (lrp->lrp_fsgid != from_kgid(&init_user_ns, current_fsgid()))
139 continue;
140 found = 1;
141 break;
142 }
143
144 if (!found) {
145 rc = -ENOENT;
146 goto out;
147 }
148
149 CDEBUG(D_SEC, "found remote perm: %u/%u/%u/%u - %#x\n",
150 lrp->lrp_uid, lrp->lrp_gid, lrp->lrp_fsuid, lrp->lrp_fsgid,
151 lrp->lrp_access_perm);
152 rc = ((lrp->lrp_access_perm & mask) == mask) ? 0 : -EACCES;
153
154out:
155 spin_unlock(&lli->lli_lock);
156 return rc;
157}
158
159int ll_update_remote_perm(struct inode *inode, struct mdt_remote_perm *perm)
160{
161 struct ll_inode_info *lli = ll_i2info(inode);
162 struct ll_remote_perm *lrp = NULL, *tmp = NULL;
163 struct hlist_head *head, *perm_hash = NULL;
164
165 LASSERT(ll_i2sbi(inode)->ll_flags & LL_SBI_RMT_CLIENT);
166
167#if 0
168 if (perm->rp_uid != current->uid ||
169 perm->rp_gid != current->gid ||
170 perm->rp_fsuid != current->fsuid ||
171 perm->rp_fsgid != current->fsgid) {
172
173 CDEBUG(D_SEC,
174 "remote perm user %u/%u/%u/%u != current %u/%u/%u/%u\n",
175 perm->rp_uid, perm->rp_gid, perm->rp_fsuid,
176 perm->rp_fsgid, current->uid, current->gid,
177 current->fsuid, current->fsgid);
178 return -EAGAIN;
179 }
180#endif
181
182 if (!lli->lli_remote_perms) {
183 perm_hash = alloc_rmtperm_hash();
184 if (!perm_hash) {
185 CERROR("alloc lli_remote_perms failed!\n");
186 return -ENOMEM;
187 }
188 }
189
190 spin_lock(&lli->lli_lock);
191
192 if (!lli->lli_remote_perms)
193 lli->lli_remote_perms = perm_hash;
194 else
195 free_rmtperm_hash(perm_hash);
196
197 head = lli->lli_remote_perms + remote_perm_hashfunc(perm->rp_uid);
198
199again:
200 hlist_for_each_entry(tmp, head, lrp_list) {
201 if (tmp->lrp_uid != perm->rp_uid)
202 continue;
203 if (tmp->lrp_gid != perm->rp_gid)
204 continue;
205 if (tmp->lrp_fsuid != perm->rp_fsuid)
206 continue;
207 if (tmp->lrp_fsgid != perm->rp_fsgid)
208 continue;
209 free_ll_remote_perm(lrp);
210 lrp = tmp;
211 break;
212 }
213
214 if (!lrp) {
215 spin_unlock(&lli->lli_lock);
216 lrp = alloc_ll_remote_perm();
217 if (!lrp) {
218 CERROR("alloc memory for ll_remote_perm failed!\n");
219 return -ENOMEM;
220 }
221 spin_lock(&lli->lli_lock);
222 goto again;
223 }
224
225 lrp->lrp_access_perm = perm->rp_access_perm;
226 if (lrp != tmp) {
227 lrp->lrp_uid = perm->rp_uid;
228 lrp->lrp_gid = perm->rp_gid;
229 lrp->lrp_fsuid = perm->rp_fsuid;
230 lrp->lrp_fsgid = perm->rp_fsgid;
231 hlist_add_head(&lrp->lrp_list, head);
232 }
233 lli->lli_rmtperm_time = cfs_time_current();
234 spin_unlock(&lli->lli_lock);
235
236 CDEBUG(D_SEC, "new remote perm@%p: %u/%u/%u/%u - %#x\n",
237 lrp, lrp->lrp_uid, lrp->lrp_gid, lrp->lrp_fsuid, lrp->lrp_fsgid,
238 lrp->lrp_access_perm);
239
240 return 0;
241}
242
243int lustre_check_remote_perm(struct inode *inode, int mask)
244{
245 struct ll_inode_info *lli = ll_i2info(inode);
246 struct ll_sb_info *sbi = ll_i2sbi(inode);
247 struct ptlrpc_request *req = NULL;
248 struct mdt_remote_perm *perm;
249 unsigned long save;
250 int i = 0, rc;
251
252 do {
253 save = lli->lli_rmtperm_time;
254 rc = do_check_remote_perm(lli, mask);
255 if (!rc || (rc != -ENOENT && i))
256 break;
257
258 might_sleep();
259
260 mutex_lock(&lli->lli_rmtperm_mutex);
261
262 if (save != lli->lli_rmtperm_time) {
263 rc = do_check_remote_perm(lli, mask);
264 if (!rc || (rc != -ENOENT && i)) {
265 mutex_unlock(&lli->lli_rmtperm_mutex);
266 break;
267 }
268 }
269
270 if (i++ > 5) {
271 CERROR("check remote perm falls in dead loop!\n");
272 LBUG();
273 }
274
275 rc = md_get_remote_perm(sbi->ll_md_exp, ll_inode2fid(inode),
276 ll_i2suppgid(inode), &req);
277 if (rc) {
278 mutex_unlock(&lli->lli_rmtperm_mutex);
279 break;
280 }
281
282 perm = req_capsule_server_swab_get(&req->rq_pill, &RMF_ACL,
283 lustre_swab_mdt_remote_perm);
284 if (unlikely(!perm)) {
285 mutex_unlock(&lli->lli_rmtperm_mutex);
286 rc = -EPROTO;
287 break;
288 }
289
290 rc = ll_update_remote_perm(inode, perm);
291 mutex_unlock(&lli->lli_rmtperm_mutex);
292 if (rc == -ENOMEM)
293 break;
294
295 ptlrpc_req_finished(req);
296 req = NULL;
297 } while (1);
298 ptlrpc_req_finished(req);
299 return rc;
300}
301
302#if 0
303
304
305void ll_free_remote_perms(struct inode *inode)
306{
307 struct ll_inode_info *lli = ll_i2info(inode);
308 struct hlist_head *hash = lli->lli_remote_perms;
309 struct ll_remote_perm *lrp;
310 struct hlist_node *node, *next;
311 int i;
312
313 LASSERT(hash);
314
315 spin_lock(&lli->lli_lock);
316
317 for (i = 0; i < REMOTE_PERM_HASHSIZE; i++) {
318 hlist_for_each_entry_safe(lrp, node, next, hash + i, lrp_list)
319 free_ll_remote_perm(lrp);
320 }
321
322 spin_unlock(&lli->lli_lock);
323}
324#endif
325