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