Whamcloud - gitweb
4d42956422205b85f641460d374abf40229990df
[fs/lustre-release.git] / lustre / llite / xattr.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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2016, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  */
32
33 #include <linux/fs.h>
34 #include <linux/sched.h>
35 #include <linux/mm.h>
36 #include <linux/xattr.h>
37 #include <linux/selinux.h>
38
39 #define DEBUG_SUBSYSTEM S_LLITE
40
41 #include <obd_support.h>
42 #include <lustre_dlm.h>
43 #include <lustre_ver.h>
44 #include <lustre_eacl.h>
45
46 #include "llite_internal.h"
47
48 /* xattr related to IMA(Integrity Measurement Architecture) */
49 #ifndef XATTR_NAME_IMA
50 #define XATTR_NAME_IMA          "security.ima"
51 #endif
52 #ifndef XATTR_NAME_EVM
53 #define XATTR_NAME_EVM          "security.evm"
54 #endif
55
56 #define XATTR_USER_T            (1)
57 #define XATTR_TRUSTED_T         (2)
58 #define XATTR_SECURITY_T        (3)
59 #define XATTR_ACL_ACCESS_T      (4)
60 #define XATTR_ACL_DEFAULT_T     (5)
61 #define XATTR_LUSTRE_T          (6)
62 #define XATTR_OTHER_T           (7)
63
64 static
65 int get_xattr_type(const char *name)
66 {
67         if (!strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS))
68                 return XATTR_ACL_ACCESS_T;
69
70         if (!strcmp(name, XATTR_NAME_POSIX_ACL_DEFAULT))
71                 return XATTR_ACL_DEFAULT_T;
72
73         if (!strncmp(name, XATTR_USER_PREFIX,
74                      sizeof(XATTR_USER_PREFIX) - 1))
75                 return XATTR_USER_T;
76
77         if (!strncmp(name, XATTR_TRUSTED_PREFIX,
78                      sizeof(XATTR_TRUSTED_PREFIX) - 1))
79                 return XATTR_TRUSTED_T;
80
81         if (!strncmp(name, XATTR_SECURITY_PREFIX,
82                      sizeof(XATTR_SECURITY_PREFIX) - 1))
83                 return XATTR_SECURITY_T;
84
85         if (!strncmp(name, XATTR_LUSTRE_PREFIX,
86                      sizeof(XATTR_LUSTRE_PREFIX) - 1))
87                 return XATTR_LUSTRE_T;
88
89         return XATTR_OTHER_T;
90 }
91
92 static
93 int xattr_type_filter(struct ll_sb_info *sbi, int xattr_type)
94 {
95         if ((xattr_type == XATTR_ACL_ACCESS_T ||
96              xattr_type == XATTR_ACL_DEFAULT_T) &&
97            !(sbi->ll_flags & LL_SBI_ACL))
98                 return -EOPNOTSUPP;
99
100         if (xattr_type == XATTR_USER_T && !(sbi->ll_flags & LL_SBI_USER_XATTR))
101                 return -EOPNOTSUPP;
102         if (xattr_type == XATTR_TRUSTED_T && !cfs_capable(CFS_CAP_SYS_ADMIN))
103                 return -EPERM;
104         if (xattr_type == XATTR_OTHER_T)
105                 return -EOPNOTSUPP;
106
107         return 0;
108 }
109
110 static
111 int ll_setxattr_common(struct inode *inode, const char *name,
112                        const void *value, size_t size,
113                        int flags, __u64 valid)
114 {
115         struct ll_sb_info *sbi = ll_i2sbi(inode);
116         struct ptlrpc_request *req = NULL;
117         int xattr_type, rc;
118         const char *pv = value;
119         ENTRY;
120
121         /*FIXME: enable IMA when the conditions are ready */
122         if (strncmp(name, XATTR_NAME_IMA,
123                     sizeof(XATTR_NAME_IMA)) == 0 ||
124             strncmp(name, XATTR_NAME_EVM,
125                     sizeof(XATTR_NAME_EVM)) == 0)
126                 return -EOPNOTSUPP;
127
128         xattr_type = get_xattr_type(name);
129         rc = xattr_type_filter(sbi, xattr_type);
130         if (rc)
131                 RETURN(rc);
132
133         if ((xattr_type == XATTR_ACL_ACCESS_T ||
134              xattr_type == XATTR_ACL_DEFAULT_T) &&
135 #ifdef HAVE_INODE_OWNER_OR_CAPABLE
136             !inode_owner_or_capable(inode))
137 #else
138             !is_owner_or_cap(inode))
139 #endif
140                 return -EPERM;
141
142         /* b10667: ignore lustre special xattr for now */
143         if (strcmp(name, XATTR_NAME_HSM) == 0 ||
144                 (xattr_type == XATTR_TRUSTED_T &&
145                 strcmp(name, XATTR_NAME_LOV) == 0) ||
146                 (xattr_type == XATTR_LUSTRE_T &&
147                  strcmp(name, "lustre.lov") == 0))
148                 RETURN(0);
149
150         /* b15587: ignore security.capability xattr for now */
151         if ((xattr_type == XATTR_SECURITY_T &&
152             strcmp(name, "security.capability") == 0))
153                 RETURN(0);
154
155         /* LU-549:  Disable security.selinux when selinux is disabled */
156         if (xattr_type == XATTR_SECURITY_T && !selinux_is_enabled() &&
157             strcmp(name, "security.selinux") == 0)
158                 RETURN(-EOPNOTSUPP);
159
160         /* In user.* namespace, only regular files and directories can have
161          * extended attributes. */
162         if (xattr_type == XATTR_USER_T) {
163                 if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
164                         RETURN(-EPERM);
165         }
166
167         rc = md_setxattr(sbi->ll_md_exp, ll_inode2fid(inode), valid, name, pv,
168                          size, 0, flags, ll_i2suppgid(inode), &req);
169         if (rc) {
170                 if (rc == -EOPNOTSUPP && xattr_type == XATTR_USER_T) {
171                         LCONSOLE_INFO("Disabling user_xattr feature because "
172                                       "it is not supported on the server\n");
173                         sbi->ll_flags &= ~LL_SBI_USER_XATTR;
174                 }
175                 RETURN(rc);
176         }
177
178         ptlrpc_req_finished(req);
179         RETURN(0);
180 }
181
182 static int get_hsm_state(struct inode *inode, __u32 *hus_states)
183 {
184         struct md_op_data *op_data;
185         struct hsm_user_state *hus;
186         int rc;
187
188         OBD_ALLOC_PTR(hus);
189         if (hus == NULL)
190                 return -ENOMEM;
191
192         op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
193                                      LUSTRE_OPC_ANY, hus);
194         if (!IS_ERR(op_data)) {
195                 rc = obd_iocontrol(LL_IOC_HSM_STATE_GET, ll_i2mdexp(inode),
196                                    sizeof(*op_data), op_data, NULL);
197                 if (rc == 0)
198                         *hus_states = hus->hus_states;
199                 else
200                         CDEBUG(D_VFSTRACE, "obd_iocontrol failed. rc = %d\n",
201                                rc);
202
203                 ll_finish_md_op_data(op_data);
204         } else {
205                 rc = PTR_ERR(op_data);
206                 CDEBUG(D_VFSTRACE, "Could not prepare the opdata. rc = %d\n",
207                        rc);
208         }
209         OBD_FREE_PTR(hus);
210         return rc;
211 }
212 int ll_setstripe_ea(struct dentry *dentry, struct lov_user_md *lump,
213                     size_t size)
214 {
215         struct inode *inode = dentry->d_inode;
216         int rc = 0;
217         bool return_err = false;
218
219         if (lump != NULL && lump->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
220                 return_err = true;
221                 goto setstripe;
222         }
223
224         /* Attributes that are saved via getxattr will always have
225          * the stripe_offset as 0.  Instead, the MDS should be
226          * allowed to pick the starting OST index.   b=17846 */
227         if (lump != NULL && lump->lmm_stripe_offset == 0)
228                 lump->lmm_stripe_offset = -1;
229         /* Avoid anyone directly setting the RELEASED flag. */
230         if (lump != NULL &&
231                 (lump->lmm_pattern & LOV_PATTERN_F_RELEASED)) {
232                 /* Only if we have a released flag check if the file
233                 * was indeed archived. */
234                 __u32 state = HS_NONE;
235                 rc = get_hsm_state(inode, &state);
236                 if (rc != 0)
237                         RETURN(rc);
238                 if (!(state & HS_ARCHIVED)) {
239                         CDEBUG(D_VFSTRACE,
240                                 "hus_states state = %x, pattern = %x\n",
241                                 state, lump->lmm_pattern);
242                         /* Here the state is: real file is not
243                          * archived but user is requesting to set
244                          * the RELEASED flag so we mask off the
245                          * released flag from the request */
246                         lump->lmm_pattern ^= LOV_PATTERN_F_RELEASED;
247                 }
248         }
249
250 setstripe:
251         if (lump != NULL && S_ISREG(inode->i_mode)) {
252                 __u64 it_flags = FMODE_WRITE;
253                 int lum_size;
254
255                 lum_size = ll_lov_user_md_size(lump);
256                 /**
257                  * b=10667: ignore error.
258                  * Silently eat error on setting strusted.lov attribute for
259                  * SuSE 9, it added default option to copy all attributes in
260                  * 'cp' command.
261                  */
262                 if (lum_size < 0 || size < lum_size)
263                         return return_err ? -ERANGE : 0;
264
265                 rc = ll_lov_setstripe_ea_info(inode, dentry, it_flags, lump,
266                                               lum_size);
267                 /* b=10667 */
268                 if (!return_err)
269                         rc = 0;
270         } else if (S_ISDIR(inode->i_mode)) {
271                 rc = ll_dir_setstripe(inode, lump, 0);
272         }
273
274         return rc;
275 }
276
277 int ll_setxattr(struct dentry *dentry, const char *name,
278                 const void *value, size_t size, int flags)
279 {
280         struct inode *inode = dentry->d_inode;
281
282         LASSERT(inode);
283         LASSERT(name);
284
285         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), xattr %s\n",
286                PFID(ll_inode2fid(inode)), inode, name);
287
288         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_SETXATTR, 1);
289
290         /* lustre/trusted.lov.xxx would be passed through xattr API */
291         if (strcmp(name, XATTR_NAME_LOV) == 0 ||
292             strcmp(name, XATTR_LUSTRE_LOV) == 0)
293                 return ll_setstripe_ea(dentry, (struct lov_user_md *)value,
294                                        size);
295         else if (strcmp(name, XATTR_NAME_LMA) == 0 ||
296                  strcmp(name, XATTR_NAME_LINK) == 0)
297                 return 0;
298
299         return ll_setxattr_common(inode, name, value, size, flags,
300                                   OBD_MD_FLXATTR);
301 }
302
303 int ll_removexattr(struct dentry *dentry, const char *name)
304 {
305         struct inode *inode = dentry->d_inode;
306
307         LASSERT(inode);
308         LASSERT(name);
309
310         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), xattr %s\n",
311                PFID(ll_inode2fid(inode)), inode, name);
312
313         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_REMOVEXATTR, 1);
314         return ll_setxattr_common(inode, name, NULL, 0, 0,
315                                   OBD_MD_FLXATTRRM);
316 }
317
318 int ll_getxattr_common(struct inode *inode, const char *name,
319                        void *buffer, size_t size, __u64 valid)
320 {
321         struct ll_sb_info *sbi = ll_i2sbi(inode);
322         struct ptlrpc_request *req = NULL;
323         struct mdt_body *body;
324         int xattr_type, rc;
325         void *xdata;
326         struct ll_inode_info *lli = ll_i2info(inode);
327         ENTRY;
328
329         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p)\n",
330                PFID(ll_inode2fid(inode)), inode);
331
332         /* listxattr have slightly different behavior from of ext3:
333          * without 'user_xattr' ext3 will list all xattr names but
334          * filtered out "^user..*"; we list them all for simplicity.
335          */
336         if (!name) {
337                 xattr_type = XATTR_OTHER_T;
338                 goto do_getxattr;
339         }
340
341         xattr_type = get_xattr_type(name);
342         rc = xattr_type_filter(sbi, xattr_type);
343         if (rc)
344                 RETURN(rc);
345
346         /* b15587: ignore security.capability xattr for now */
347         if ((xattr_type == XATTR_SECURITY_T &&
348             strcmp(name, "security.capability") == 0))
349                 RETURN(-ENODATA);
350
351         /* LU-549:  Disable security.selinux when selinux is disabled */
352         if (xattr_type == XATTR_SECURITY_T && !selinux_is_enabled() &&
353             strcmp(name, "security.selinux") == 0)
354                 RETURN(-EOPNOTSUPP);
355
356 #ifdef CONFIG_FS_POSIX_ACL
357         /* posix acl is under protection of LOOKUP lock. when calling to this,
358          * we just have path resolution to the target inode, so we have great
359          * chance that cached ACL is uptodate.
360          */
361         if (xattr_type == XATTR_ACL_ACCESS_T) {
362                 struct posix_acl *acl;
363
364                 spin_lock(&lli->lli_lock);
365                 acl = posix_acl_dup(lli->lli_posix_acl);
366                 spin_unlock(&lli->lli_lock);
367
368                 if (!acl)
369                         RETURN(-ENODATA);
370
371                 rc = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
372                 posix_acl_release(acl);
373                 RETURN(rc);
374         }
375         if (xattr_type == XATTR_ACL_DEFAULT_T && !S_ISDIR(inode->i_mode))
376                 RETURN(-ENODATA);
377 #endif
378
379 do_getxattr:
380         if (sbi->ll_xattr_cache_enabled &&
381             xattr_type != XATTR_ACL_ACCESS_T &&
382             (xattr_type != XATTR_SECURITY_T ||
383                 strcmp(name, "security.selinux") != 0)) {
384                 rc = ll_xattr_cache_get(inode, name, buffer, size, valid);
385                 if (rc == -EAGAIN)
386                         goto getxattr_nocache;
387                 if (rc < 0)
388                         GOTO(out_xattr, rc);
389
390                 /* Add "system.posix_acl_access" to the list */
391                 if (lli->lli_posix_acl != NULL && valid & OBD_MD_FLXATTRLS) {
392                         if (size == 0) {
393                                 rc += sizeof(XATTR_NAME_ACL_ACCESS);
394                         } else if (size - rc >= sizeof(XATTR_NAME_ACL_ACCESS)) {
395                                 memcpy(buffer + rc, XATTR_NAME_ACL_ACCESS,
396                                        sizeof(XATTR_NAME_ACL_ACCESS));
397                                 rc += sizeof(XATTR_NAME_ACL_ACCESS);
398                         } else {
399                                 GOTO(out_xattr, rc = -ERANGE);
400                         }
401                 }
402         } else {
403 getxattr_nocache:
404                 rc = md_getxattr(sbi->ll_md_exp, ll_inode2fid(inode),
405                                 valid, name, NULL, 0, size, 0, &req);
406
407                 if (rc < 0)
408                         GOTO(out_xattr, rc);
409
410                 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
411                 LASSERT(body);
412
413                 /* only detect the xattr size */
414                 if (size == 0)
415                         GOTO(out, rc = body->mbo_eadatasize);
416
417                 if (size < body->mbo_eadatasize) {
418                         CERROR("server bug: replied size %u > %u\n",
419                                 body->mbo_eadatasize, (int)size);
420                         GOTO(out, rc = -ERANGE);
421                 }
422
423                 if (body->mbo_eadatasize == 0)
424                         GOTO(out, rc = -ENODATA);
425
426                 /* do not need swab xattr data */
427                 xdata = req_capsule_server_sized_get(&req->rq_pill, &RMF_EADATA,
428                                                         body->mbo_eadatasize);
429                 if (!xdata)
430                         GOTO(out, rc = -EFAULT);
431
432                 memcpy(buffer, xdata, body->mbo_eadatasize);
433                 rc = body->mbo_eadatasize;
434         }
435
436         EXIT;
437
438 out_xattr:
439         if (rc == -EOPNOTSUPP && xattr_type == XATTR_USER_T) {
440                 LCONSOLE_INFO("%s: disabling user_xattr feature because "
441                                 "it is not supported on the server: rc = %d\n",
442                                 ll_get_fsname(inode->i_sb, NULL, 0), rc);
443                 sbi->ll_flags &= ~LL_SBI_USER_XATTR;
444         }
445 out:
446         ptlrpc_req_finished(req);
447         return rc;
448 }
449
450 static ssize_t ll_getxattr_lov(struct inode *inode, void *buf, size_t buf_size)
451 {
452         ssize_t rc;
453
454         if (S_ISREG(inode->i_mode)) {
455                 struct cl_object *obj = ll_i2info(inode)->lli_clob;
456                 struct lu_env *env;
457                 struct cl_layout cl = {
458                         .cl_buf.lb_buf = buf,
459                         .cl_buf.lb_len = buf_size,
460                 };
461                 __u16 refcheck;
462
463                 if (obj == NULL)
464                         RETURN(-ENODATA);
465
466                 env = cl_env_get(&refcheck);
467                 if (IS_ERR(env))
468                         RETURN(PTR_ERR(env));
469
470                 rc = cl_object_layout_get(env, obj, &cl);
471                 if (rc < 0)
472                         GOTO(out_env, rc);
473
474                 if (cl.cl_size == 0)
475                         GOTO(out_env, rc = -ENODATA);
476
477                 rc = cl.cl_size;
478
479                 if (buf_size == 0)
480                         GOTO(out_env, rc);
481
482                 LASSERT(buf != NULL && rc <= buf_size);
483
484                 /* Do not return layout gen for getxattr() since
485                  * otherwise it would confuse tar --xattr by
486                  * recognizing layout gen as stripe offset when the
487                  * file is restored. See LU-2809. */
488                 if (((struct lov_mds_md *)buf)->lmm_magic == LOV_MAGIC_COMP_V1)
489                         goto out_env;
490
491                 ((struct lov_mds_md *)buf)->lmm_layout_gen = 0;
492 out_env:
493                 cl_env_put(env, &refcheck);
494
495                 RETURN(rc);
496         } else if (S_ISDIR(inode->i_mode)) {
497                 struct lov_mds_md *lmm = NULL;
498                 int lmm_size = 0;
499                 struct ptlrpc_request *req = NULL;
500
501                 rc = ll_dir_getstripe(inode, (void **)&lmm, &lmm_size,
502                                       &req, 0);
503                 if (rc < 0)
504                         GOTO(out_req, rc);
505
506                 if (buf_size == 0)
507                         GOTO(out_req, rc = lmm_size);
508
509                 if (buf_size < lmm_size)
510                         GOTO(out_req, rc = -ERANGE);
511
512                 memcpy(buf, lmm, lmm_size);
513                 GOTO(out_req, rc = lmm_size);
514 out_req:
515                 if (req != NULL)
516                         ptlrpc_req_finished(req);
517
518                 return rc;
519         } else {
520                 RETURN(-ENODATA);
521         }
522 }
523
524 ssize_t ll_getxattr(struct dentry *dentry, const char *name, void *buf,
525                     size_t buf_size)
526 {
527         struct inode *inode = dentry->d_inode;
528
529         LASSERT(inode);
530         LASSERT(name);
531
532         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), xattr %s\n",
533                PFID(ll_inode2fid(inode)), inode, name);
534
535         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_GETXATTR, 1);
536
537         if (strcmp(name, XATTR_LUSTRE_LOV) == 0 ||
538             strcmp(name, XATTR_NAME_LOV) == 0)
539                 return ll_getxattr_lov(inode, buf, buf_size);
540         else
541                 return ll_getxattr_common(inode, name, buf, buf_size,
542                                           OBD_MD_FLXATTR);
543 }
544
545 ssize_t ll_listxattr(struct dentry *dentry, char *buf, size_t buf_size)
546 {
547         struct inode *inode = dentry->d_inode;
548         struct ll_sb_info *sbi = ll_i2sbi(inode);
549         char *xattr_name;
550         ssize_t rc, rc2;
551         size_t len, rem;
552
553         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p)\n",
554                PFID(ll_inode2fid(inode)), inode);
555
556         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_LISTXATTR, 1);
557
558         rc = ll_getxattr_common(inode, NULL, buf, buf_size, OBD_MD_FLXATTRLS);
559         if (rc < 0)
560                 RETURN(rc);
561
562         /* If we're being called to get the size of the xattr list
563          * (buf_size == 0) then just assume that a lustre.lov xattr
564          * exists. */
565         if (buf_size == 0)
566                 RETURN(rc + sizeof(XATTR_LUSTRE_LOV));
567
568         xattr_name = buf;
569         rem = rc;
570
571         while (rem > 0) {
572                 len = strnlen(xattr_name, rem - 1) + 1;
573                 rem -= len;
574                 if (xattr_type_filter(sbi, get_xattr_type(xattr_name)) == 0) {
575                         /* Skip OK xattr type, leave it in buffer. */
576                         xattr_name += len;
577                         continue;
578                 }
579
580                 /* Move up remaining xattrs in buffer removing the
581                  * xattr that is not OK. */
582                 memmove(xattr_name, xattr_name + len, rem);
583                 rc -= len;
584         }
585
586         rc2 = ll_getxattr_lov(inode, NULL, 0);
587         if (rc2 == -ENODATA)
588                 RETURN(rc);
589
590         if (rc2 < 0)
591                 RETURN(rc2);
592
593         if (buf_size < rc + sizeof(XATTR_LUSTRE_LOV))
594                 RETURN(-ERANGE);
595
596         memcpy(buf + rc, XATTR_LUSTRE_LOV, sizeof(XATTR_LUSTRE_LOV));
597
598         RETURN(rc + sizeof(XATTR_LUSTRE_LOV));
599 }