Whamcloud - gitweb
b=16098
[fs/lustre-release.git] / lustre / mdd / mdd_permission.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 [sun.com URL with a
20  * copy of GPLv2].
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/mdd/mdd_permission.c
37  *
38  * Lustre Metadata Server (mdd) routines
39  *
40  * Author: fangyong@clusterfs.com
41  * Author: lsy@clusterfs.com
42  */
43 #ifndef EXPORT_SYMTAB
44 # define EXPORT_SYMTAB
45 #endif
46 #define DEBUG_SUBSYSTEM S_MDS
47
48 #include <linux/module.h>
49 #include <linux/jbd.h>
50 #include <obd.h>
51 #include <obd_class.h>
52 #include <lustre_ver.h>
53 #include <obd_support.h>
54 #include <lprocfs_status.h>
55
56 #include <linux/ldiskfs_fs.h>
57 #include <lustre_mds.h>
58 #include <lustre/lustre_idl.h>
59
60 #include "mdd_internal.h"
61
62 #ifdef CONFIG_FS_POSIX_ACL
63
64 /*
65  * Get default acl EA only.
66  * Hold read_lock for mdd_obj.
67  */
68 int mdd_acl_def_get(const struct lu_env *env, struct mdd_object *mdd_obj, 
69                     struct md_attr *ma)
70 {
71         struct lu_buf *buf;
72         int rc;
73         ENTRY;
74
75         if (ma->ma_valid & MA_ACL_DEF)
76                 RETURN(0);
77         
78         buf = mdd_buf_get(env, ma->ma_acl, ma->ma_acl_size);
79         rc = mdo_xattr_get(env, mdd_obj, buf, XATTR_NAME_ACL_DEFAULT,
80                            BYPASS_CAPA);
81         if (rc > 0) {
82                 ma->ma_acl_size = rc;
83                 ma->ma_valid |= MA_ACL_DEF;
84                 rc = 0;
85         } else if ((rc == -EOPNOTSUPP) || (rc == -ENODATA)) {
86                 rc = 0;
87         }
88         RETURN(rc);
89 }
90
91 /*
92  * Hold write_lock for o.
93  */
94 int mdd_acl_chmod(const struct lu_env *env, struct mdd_object *o, __u32 mode, 
95                   struct thandle *handle)
96 {
97         struct lu_buf           *buf;
98         posix_acl_xattr_header  *head;
99         posix_acl_xattr_entry   *entry;
100         int                      entry_count;
101         int                      rc;
102
103         ENTRY;
104
105         buf = mdd_buf_get(env, mdd_env_info(env)->mti_xattr_buf, 
106                           sizeof(mdd_env_info(env)->mti_xattr_buf));
107         
108         rc = mdo_xattr_get(env, o, buf, XATTR_NAME_ACL_ACCESS, BYPASS_CAPA);
109         if ((rc == -EOPNOTSUPP) || (rc == -ENODATA))
110                 RETURN(0);
111         else if (rc <= 0)
112                 RETURN(rc);
113
114         buf->lb_len = rc;
115         head = (posix_acl_xattr_header *)(buf->lb_buf);
116         entry = head->a_entries;
117         entry_count = (buf->lb_len - sizeof(head->a_version)) /
118                       sizeof(posix_acl_xattr_entry);
119         if (entry_count <= 0)
120                 RETURN(0);
121        
122         rc = lustre_posix_acl_chmod_masq(entry, mode, entry_count);
123         if (rc)
124                 RETURN(rc);
125
126         rc = mdo_xattr_set(env, o, buf, XATTR_NAME_ACL_ACCESS,
127                            0, handle, BYPASS_CAPA);
128         RETURN(rc);
129 }
130
131 /*
132  * Hold write_lock for obj.
133  */
134 int __mdd_acl_init(const struct lu_env *env, struct mdd_object *obj,
135                    struct lu_buf *buf, __u32 *mode, struct thandle *handle)
136 {
137         posix_acl_xattr_header  *head;
138         posix_acl_xattr_entry   *entry;
139         int                      entry_count;
140         int                      rc;
141
142         ENTRY;
143
144         head = (posix_acl_xattr_header *)(buf->lb_buf);
145         entry = head->a_entries;
146         entry_count = (buf->lb_len - sizeof(head->a_version)) /
147                       sizeof(posix_acl_xattr_entry);
148         if (entry_count <= 0)
149                 RETURN(0);
150        
151         if (S_ISDIR(*mode)) {
152                 rc = mdo_xattr_set(env, obj, buf, XATTR_NAME_ACL_DEFAULT, 0, 
153                                    handle, BYPASS_CAPA);
154                 if (rc)
155                         RETURN(rc);
156         }
157
158         rc = lustre_posix_acl_create_masq(entry, mode, entry_count);
159         if (rc <= 0)
160                 RETURN(rc);
161
162         rc = mdo_xattr_set(env, obj, buf, XATTR_NAME_ACL_ACCESS, 0, handle,
163                            BYPASS_CAPA);
164         RETURN(rc);
165 }
166
167 /*
168  * Hold read_lock for pobj.
169  * Hold write_lock for cobj.
170  */
171 int mdd_acl_init(const struct lu_env *env, struct mdd_object *pobj,
172                  struct mdd_object *cobj, __u32 *mode, struct thandle *handle)
173 {
174         struct lu_buf   *buf;
175         int             rc;
176         ENTRY;
177
178         if (S_ISLNK(*mode))
179                 RETURN(0);
180
181         buf = mdd_buf_get(env, mdd_env_info(env)->mti_xattr_buf, 
182                           sizeof(mdd_env_info(env)->mti_xattr_buf));
183         rc = mdo_xattr_get(env, pobj, buf, XATTR_NAME_ACL_DEFAULT, BYPASS_CAPA);
184         if ((rc == -EOPNOTSUPP) || (rc == -ENODATA))
185                 RETURN(0);
186         else if (rc <= 0)
187                 RETURN(rc);
188
189         buf->lb_len = rc;
190         rc = __mdd_acl_init(env, cobj, buf, mode, handle);
191         RETURN(rc);
192 }
193 #endif
194
195 /*
196  * Hold read_lock for obj.
197  */
198 static int mdd_check_acl(const struct lu_env *env, struct mdd_object *obj,
199                          struct lu_attr *la, int mask)
200 {
201 #ifdef CONFIG_FS_POSIX_ACL
202         struct md_ucred  *uc  = md_ucred(env);
203         posix_acl_xattr_header *head;
204         posix_acl_xattr_entry *entry;
205         struct lu_buf   *buf;
206         int entry_count;
207         int rc;
208         ENTRY;
209
210         buf = mdd_buf_get(env, mdd_env_info(env)->mti_xattr_buf, 
211                           sizeof(mdd_env_info(env)->mti_xattr_buf));
212         rc = mdo_xattr_get(env, obj, buf, XATTR_NAME_ACL_ACCESS,
213                            mdd_object_capa(env, obj));
214         if (rc <= 0)
215                 RETURN(rc ? : -EACCES);
216
217         buf->lb_len = rc;
218         head = (posix_acl_xattr_header *)(buf->lb_buf);
219         entry = head->a_entries;
220         entry_count = (buf->lb_len - sizeof(head->a_version)) /
221                       sizeof(posix_acl_xattr_entry);
222
223         rc = lustre_posix_acl_permission(uc, la, mask, entry, entry_count);
224         RETURN(rc);
225 #else
226         ENTRY;
227         RETURN(-EAGAIN);
228 #endif
229 }
230
231 int __mdd_permission_internal(const struct lu_env *env, struct mdd_object *obj,
232                               struct lu_attr *la, int mask, int needlock)
233 {
234         struct md_ucred *uc = md_ucred(env);
235         __u32 mode;
236         int rc;
237         ENTRY;
238
239         if (mask == 0)
240                 RETURN(0);
241
242         /* These means unnecessary for permission check */
243         if ((uc == NULL) || (uc->mu_valid == UCRED_INIT))
244                 RETURN(0);
245
246         /* Invalid user credit */
247         if (uc->mu_valid == UCRED_INVALID)
248                 RETURN(-EACCES);
249
250         /*
251          * Nobody gets write access to an immutable file.
252          */
253         if ((mask & MAY_WRITE) && mdd_is_immutable(obj))
254                 RETURN(-EACCES);
255
256         if (la == NULL) {
257                 la = &mdd_env_info(env)->mti_la;
258                 rc = mdd_la_get(env, obj, la, BYPASS_CAPA);
259                 if (rc)
260                         RETURN(rc);
261         }
262
263         mode = la->la_mode;
264         if (uc->mu_fsuid == la->la_uid) {
265                 mode >>= 6;
266         } else {
267                 if (mode & S_IRWXG) {
268                         if (needlock)
269                                 mdd_read_lock(env, obj);
270                         rc = mdd_check_acl(env, obj, la, mask);
271                         if (needlock)
272                                 mdd_read_unlock(env, obj);
273                         if (rc == -EACCES)
274                                 goto check_capabilities;
275                         else if ((rc != -EAGAIN) && (rc != -EOPNOTSUPP) &&
276                                  (rc != -ENODATA))
277                                 RETURN(rc);
278                 }
279                 if (lustre_in_group_p(uc, la->la_gid))
280                         mode >>= 3;
281         }
282
283         if (((mode & mask & S_IRWXO) == mask))
284                 RETURN(0);
285
286 check_capabilities:
287         if (!(mask & MAY_EXEC) ||
288             (la->la_mode & S_IXUGO) || S_ISDIR(la->la_mode))
289                 if (mdd_capable(uc, CAP_DAC_OVERRIDE))
290                         RETURN(0);
291
292         if ((mask == MAY_READ) ||
293             (S_ISDIR(la->la_mode) && !(mask & MAY_WRITE)))
294                 if (mdd_capable(uc, CAP_DAC_READ_SEARCH))
295                         RETURN(0);
296
297         RETURN(-EACCES);
298 }
299
300 int mdd_permission(const struct lu_env *env, 
301                    struct md_object *pobj, struct md_object *cobj,
302                    struct md_attr *ma, int mask)
303 {
304         struct mdd_object *mdd_pobj, *mdd_cobj;
305         struct md_ucred *uc = NULL;
306         struct lu_attr *la = NULL;
307         int check_create, check_link;
308         int check_unlink;
309         int check_rename_src, check_rename_tar;
310         int check_vtx_part, check_vtx_full;
311         int check_rgetfacl;
312         int rc = 0;
313         ENTRY;
314
315         LASSERT(cobj);
316         mdd_cobj = md2mdd_obj(cobj);
317
318         /* For cross_open case, the "mask" is open flags,
319          * so convert it to permission mask first.
320          * XXX: MDS_OPEN_CROSS must be NOT equal to permission mask MAY_*. */
321         if (unlikely(mask & MDS_OPEN_CROSS)) {
322                 la = &mdd_env_info(env)->mti_la;
323                 rc = mdd_la_get(env, mdd_cobj, la, BYPASS_CAPA);
324                 if (rc)
325                         RETURN(rc);
326
327                 mask = accmode(env, la, mask & ~MDS_OPEN_CROSS);
328         }
329
330         check_create = mask & MAY_CREATE;
331         check_link = mask & MAY_LINK;
332         check_unlink = mask & MAY_UNLINK;
333         check_rename_src = mask & MAY_RENAME_SRC;
334         check_rename_tar = mask & MAY_RENAME_TAR;
335         check_vtx_part = mask & MAY_VTX_PART;
336         check_vtx_full = mask & MAY_VTX_FULL;
337         check_rgetfacl = mask & MAY_RGETFACL;
338
339         mask &= ~(MAY_CREATE | MAY_LINK |
340                 MAY_UNLINK |
341                 MAY_RENAME_SRC | MAY_RENAME_TAR |
342                 MAY_VTX_PART | MAY_VTX_FULL |
343                 MAY_RGETFACL);
344
345         rc = mdd_permission_internal_locked(env, mdd_cobj, NULL, mask);
346
347         if (!rc && (check_create || check_link))
348                 rc = mdd_may_create(env, mdd_cobj, NULL, 1, check_link);
349
350         if (!rc && check_unlink) {
351                 LASSERT(ma);
352                 rc = mdd_may_unlink(env, mdd_cobj, ma);
353         }
354
355         if (!rc && (check_rename_src || check_rename_tar)) {
356                 LASSERT(pobj);
357                 LASSERT(ma);
358                 mdd_pobj = md2mdd_obj(pobj);
359                 rc = mdd_may_delete(env, mdd_pobj, mdd_cobj, ma, 1,
360                                     check_rename_tar);
361         }
362
363         if (!rc && (check_vtx_part || check_vtx_full)) {
364                 uc = md_ucred(env);
365                 LASSERT(ma);
366                 if (likely(!la)) {
367                         la = &mdd_env_info(env)->mti_la;
368                         rc = mdd_la_get(env, mdd_cobj, la, BYPASS_CAPA);
369                         if (rc)
370                                 RETURN(rc);
371                 }
372
373                 if (!(la->la_mode & S_ISVTX) || (la->la_uid == uc->mu_fsuid) ||
374                     (check_vtx_full && (ma->ma_attr.la_valid & LA_UID) &&
375                     (ma->ma_attr.la_uid == uc->mu_fsuid))) {
376                         ma->ma_attr_flags |= MDS_VTX_BYPASS;
377                 } else {
378                         ma->ma_attr_flags &= ~MDS_VTX_BYPASS;
379                         if (check_vtx_full)
380                                 rc = -EPERM;
381                 }
382         }
383
384         if (unlikely(!rc && check_rgetfacl)) {
385                 if (likely(!uc))
386                         uc = md_ucred(env);
387
388                 if (likely(!la)) {
389                         la = &mdd_env_info(env)->mti_la;
390                         rc = mdd_la_get(env, mdd_cobj, la, BYPASS_CAPA);
391                         if (rc)
392                                 RETURN(rc);
393                 }
394
395                 if (la->la_uid != uc->mu_fsuid && !mdd_capable(uc, CAP_FOWNER))
396                         rc = -EPERM;
397         }
398
399         RETURN(rc);
400 }
401
402 int mdd_capa_get(const struct lu_env *env, struct md_object *obj,
403                  struct lustre_capa *capa, int renewal)
404 {
405         struct mdd_object *mdd_obj = md2mdd_obj(obj);
406         struct obd_capa *oc;
407         int rc = 0;
408         ENTRY;
409
410         oc = mdo_capa_get(env, mdd_obj, renewal ? capa : NULL,
411                           capa->lc_opc);
412         if (IS_ERR(oc)) {
413                 rc = PTR_ERR(oc);
414         } else {
415                 capa_cpy(capa, oc);
416                 capa_put(oc);
417         }
418
419         RETURN(rc);
420 }