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