Whamcloud - gitweb
Augment ->do_{read,write}_lock() prototypes with a `role' parameter indicating
[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 #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_def_acl_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 #endif
167
168 /*
169  * Hold read_lock for obj.
170  */
171 static int mdd_check_acl(const struct lu_env *env, struct mdd_object *obj,
172                          struct lu_attr *la, int mask)
173 {
174 #ifdef CONFIG_FS_POSIX_ACL
175         struct md_ucred  *uc  = md_ucred(env);
176         posix_acl_xattr_header *head;
177         posix_acl_xattr_entry *entry;
178         struct lu_buf   *buf;
179         int entry_count;
180         int rc;
181         ENTRY;
182
183         buf = mdd_buf_get(env, mdd_env_info(env)->mti_xattr_buf, 
184                           sizeof(mdd_env_info(env)->mti_xattr_buf));
185         rc = mdo_xattr_get(env, obj, buf, XATTR_NAME_ACL_ACCESS,
186                            mdd_object_capa(env, obj));
187         if (rc <= 0)
188                 RETURN(rc ? : -EACCES);
189
190         buf->lb_len = rc;
191         head = (posix_acl_xattr_header *)(buf->lb_buf);
192         entry = head->a_entries;
193         entry_count = (buf->lb_len - sizeof(head->a_version)) /
194                       sizeof(posix_acl_xattr_entry);
195
196         rc = lustre_posix_acl_permission(uc, la, mask, entry, entry_count);
197         RETURN(rc);
198 #else
199         ENTRY;
200         RETURN(-EAGAIN);
201 #endif
202 }
203
204 int __mdd_permission_internal(const struct lu_env *env, struct mdd_object *obj,
205                               struct lu_attr *la, int mask, int role)
206 {
207         struct md_ucred *uc = md_ucred(env);
208         __u32 mode;
209         int rc;
210         ENTRY;
211
212         if (mask == 0)
213                 RETURN(0);
214
215         /* These means unnecessary for permission check */
216         if ((uc == NULL) || (uc->mu_valid == UCRED_INIT))
217                 RETURN(0);
218
219         /* Invalid user credit */
220         if (uc->mu_valid == UCRED_INVALID)
221                 RETURN(-EACCES);
222
223         /*
224          * Nobody gets write access to an immutable file.
225          */
226         if ((mask & MAY_WRITE) && mdd_is_immutable(obj))
227                 RETURN(-EACCES);
228
229         if (la == NULL) {
230                 la = &mdd_env_info(env)->mti_la;
231                 rc = mdd_la_get(env, obj, la, BYPASS_CAPA);
232                 if (rc)
233                         RETURN(rc);
234         }
235
236         mode = la->la_mode;
237         if (uc->mu_fsuid == la->la_uid) {
238                 mode >>= 6;
239         } else {
240                 if (mode & S_IRWXG) {
241                         if (role != -1)
242                                 mdd_read_lock(env, obj, role);
243                         rc = mdd_check_acl(env, obj, la, mask);
244                         if (role != -1)
245                                 mdd_read_unlock(env, obj);
246                         if (rc == -EACCES)
247                                 goto check_capabilities;
248                         else if ((rc != -EAGAIN) && (rc != -EOPNOTSUPP) &&
249                                  (rc != -ENODATA))
250                                 RETURN(rc);
251                 }
252                 if (lustre_in_group_p(uc, la->la_gid))
253                         mode >>= 3;
254         }
255
256         if (((mode & mask & S_IRWXO) == mask))
257                 RETURN(0);
258
259 check_capabilities:
260         if (!(mask & MAY_EXEC) ||
261             (la->la_mode & S_IXUGO) || S_ISDIR(la->la_mode))
262                 if (mdd_capable(uc, CFS_CAP_DAC_OVERRIDE))
263                         RETURN(0);
264
265         if ((mask == MAY_READ) ||
266             (S_ISDIR(la->la_mode) && !(mask & MAY_WRITE)))
267                 if (mdd_capable(uc, CFS_CAP_DAC_READ_SEARCH))
268                         RETURN(0);
269
270         RETURN(-EACCES);
271 }
272
273 int mdd_permission(const struct lu_env *env, 
274                    struct md_object *pobj, struct md_object *cobj,
275                    struct md_attr *ma, int mask)
276 {
277         struct mdd_object *mdd_pobj, *mdd_cobj;
278         struct md_ucred *uc = NULL;
279         struct lu_attr *la = NULL;
280         int check_create, check_link;
281         int check_unlink;
282         int check_rename_src, check_rename_tar;
283         int check_vtx_part, check_vtx_full;
284         int check_rgetfacl;
285         int rc = 0;
286         ENTRY;
287
288         LASSERT(cobj);
289         mdd_cobj = md2mdd_obj(cobj);
290
291         /* For cross_open case, the "mask" is open flags,
292          * so convert it to permission mask first.
293          * XXX: MDS_OPEN_CROSS must be NOT equal to permission mask MAY_*. */
294         if (unlikely(mask & MDS_OPEN_CROSS)) {
295                 la = &mdd_env_info(env)->mti_la;
296                 rc = mdd_la_get(env, mdd_cobj, la, BYPASS_CAPA);
297                 if (rc)
298                         RETURN(rc);
299
300                 mask = accmode(env, la, mask & ~MDS_OPEN_CROSS);
301         }
302
303         check_create = mask & MAY_CREATE;
304         check_link = mask & MAY_LINK;
305         check_unlink = mask & MAY_UNLINK;
306         check_rename_src = mask & MAY_RENAME_SRC;
307         check_rename_tar = mask & MAY_RENAME_TAR;
308         check_vtx_part = mask & MAY_VTX_PART;
309         check_vtx_full = mask & MAY_VTX_FULL;
310         check_rgetfacl = mask & MAY_RGETFACL;
311
312         mask &= ~(MAY_CREATE | MAY_LINK |
313                 MAY_UNLINK |
314                 MAY_RENAME_SRC | MAY_RENAME_TAR |
315                 MAY_VTX_PART | MAY_VTX_FULL |
316                 MAY_RGETFACL);
317
318         rc = mdd_permission_internal_locked(env, mdd_cobj, NULL, mask,
319                                             MOR_TGT_CHILD);
320
321         if (!rc && (check_create || check_link))
322                 rc = mdd_may_create(env, mdd_cobj, NULL, 1, check_link);
323
324         if (!rc && check_unlink) {
325                 LASSERT(ma);
326                 rc = mdd_may_unlink(env, mdd_cobj, ma);
327         }
328
329         if (!rc && (check_rename_src || check_rename_tar)) {
330                 LASSERT(pobj);
331                 LASSERT(ma);
332                 mdd_pobj = md2mdd_obj(pobj);
333                 rc = mdd_may_delete(env, mdd_pobj, mdd_cobj, ma, 1,
334                                     check_rename_tar);
335         }
336
337         if (!rc && (check_vtx_part || check_vtx_full)) {
338                 uc = md_ucred(env);
339                 LASSERT(ma);
340                 if (likely(!la)) {
341                         la = &mdd_env_info(env)->mti_la;
342                         rc = mdd_la_get(env, mdd_cobj, la, BYPASS_CAPA);
343                         if (rc)
344                                 RETURN(rc);
345                 }
346
347                 if (!(la->la_mode & S_ISVTX) || (la->la_uid == uc->mu_fsuid) ||
348                     (check_vtx_full && (ma->ma_attr.la_valid & LA_UID) &&
349                     (ma->ma_attr.la_uid == uc->mu_fsuid))) {
350                         ma->ma_attr_flags |= MDS_VTX_BYPASS;
351                 } else {
352                         ma->ma_attr_flags &= ~MDS_VTX_BYPASS;
353                         if (check_vtx_full)
354                                 rc = -EPERM;
355                 }
356         }
357
358         if (unlikely(!rc && check_rgetfacl)) {
359                 if (likely(!uc))
360                         uc = md_ucred(env);
361
362                 if (likely(!la)) {
363                         la = &mdd_env_info(env)->mti_la;
364                         rc = mdd_la_get(env, mdd_cobj, la, BYPASS_CAPA);
365                         if (rc)
366                                 RETURN(rc);
367                 }
368
369                 if (la->la_uid != uc->mu_fsuid &&
370                     !mdd_capable(uc, CFS_CAP_FOWNER))
371                         rc = -EPERM;
372         }
373
374         RETURN(rc);
375 }
376
377 int mdd_capa_get(const struct lu_env *env, struct md_object *obj,
378                  struct lustre_capa *capa, int renewal)
379 {
380         struct mdd_object *mdd_obj = md2mdd_obj(obj);
381         struct obd_capa *oc;
382         int rc = 0;
383         ENTRY;
384
385         oc = mdo_capa_get(env, mdd_obj, renewal ? capa : NULL,
386                           capa->lc_opc);
387         if (IS_ERR(oc)) {
388                 rc = PTR_ERR(oc);
389         } else {
390                 capa_cpy(capa, oc);
391                 capa_put(oc);
392         }
393
394         RETURN(rc);
395 }