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