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