Whamcloud - gitweb
LU-9484 llite: eat -EEXIST on setting trunsted.lov
[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         /* LU-549:  Disable security.selinux when selinux is disabled */
151         if (xattr_type == XATTR_SECURITY_T && !selinux_is_enabled() &&
152             strcmp(name, "security.selinux") == 0)
153                 RETURN(-EOPNOTSUPP);
154
155         /* In user.* namespace, only regular files and directories can have
156          * extended attributes. */
157         if (xattr_type == XATTR_USER_T) {
158                 if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
159                         RETURN(-EPERM);
160         }
161
162         rc = md_setxattr(sbi->ll_md_exp, ll_inode2fid(inode), valid, name, pv,
163                          size, 0, flags, ll_i2suppgid(inode), &req);
164         if (rc) {
165                 if (rc == -EOPNOTSUPP && xattr_type == XATTR_USER_T) {
166                         LCONSOLE_INFO("Disabling user_xattr feature because "
167                                       "it is not supported on the server\n");
168                         sbi->ll_flags &= ~LL_SBI_USER_XATTR;
169                 }
170                 RETURN(rc);
171         }
172
173         ptlrpc_req_finished(req);
174         RETURN(0);
175 }
176
177 static int get_hsm_state(struct inode *inode, __u32 *hus_states)
178 {
179         struct md_op_data *op_data;
180         struct hsm_user_state *hus;
181         int rc;
182
183         OBD_ALLOC_PTR(hus);
184         if (hus == NULL)
185                 return -ENOMEM;
186
187         op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
188                                      LUSTRE_OPC_ANY, hus);
189         if (!IS_ERR(op_data)) {
190                 rc = obd_iocontrol(LL_IOC_HSM_STATE_GET, ll_i2mdexp(inode),
191                                    sizeof(*op_data), op_data, NULL);
192                 if (rc == 0)
193                         *hus_states = hus->hus_states;
194                 else
195                         CDEBUG(D_VFSTRACE, "obd_iocontrol failed. rc = %d\n",
196                                rc);
197
198                 ll_finish_md_op_data(op_data);
199         } else {
200                 rc = PTR_ERR(op_data);
201                 CDEBUG(D_VFSTRACE, "Could not prepare the opdata. rc = %d\n",
202                        rc);
203         }
204         OBD_FREE_PTR(hus);
205         return rc;
206 }
207
208 static int ll_adjust_lum(struct inode *inode, struct lov_user_md *lump)
209 {
210         struct lov_comp_md_v1 *comp_v1 = (struct lov_comp_md_v1 *)lump;
211         struct lov_user_md *v1 = lump;
212         bool release_checked = false;
213         bool need_clear_release = false;
214         __u16 entry_count = 1;
215         bool is_composite = false;
216         int rc = 0;
217         int i;
218
219         if (lump == NULL)
220                 return 0;
221
222         if (lump->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
223                 entry_count = comp_v1->lcm_entry_count;
224                 is_composite = true;
225         }
226
227         for (i = 0; i < entry_count; i++) {
228                 if (lump->lmm_magic == LOV_USER_MAGIC_COMP_V1)
229                         v1 = (struct lov_user_md *)((char *)comp_v1 +
230                                         comp_v1->lcm_entries[i].lcme_offset);
231
232                 /* Attributes that are saved via getxattr will always
233                  * have the stripe_offset as 0.  Instead, the MDS
234                  * should be allowed to pick the starting OST index.
235                  * b=17846 */
236                 if (!is_composite && v1->lmm_stripe_offset == 0)
237                         v1->lmm_stripe_offset = -1;
238
239                 /* Avoid anyone directly setting the RELEASED flag. */
240                 if (v1->lmm_pattern & LOV_PATTERN_F_RELEASED) {
241                         if (!release_checked) {
242                                 __u32 state = HS_NONE;
243                                 rc = get_hsm_state(inode, &state);
244                                 if (rc)
245                                         return rc;
246                                 if (!(state & HS_ARCHIVED))
247                                         need_clear_release = true;
248                                 release_checked = true;
249                         }
250                         if (need_clear_release)
251                                 v1->lmm_pattern ^= LOV_PATTERN_F_RELEASED;
252                 }
253         }
254
255         return rc;
256 }
257
258 int ll_setstripe_ea(struct dentry *dentry, struct lov_user_md *lump,
259                     size_t size)
260 {
261         struct inode *inode = dentry->d_inode;
262         int rc = 0;
263
264         rc = ll_adjust_lum(inode, lump);
265         if (rc)
266                 return rc;
267
268         if (lump != NULL && S_ISREG(inode->i_mode)) {
269                 __u64 it_flags = FMODE_WRITE;
270                 int lum_size;
271
272                 lum_size = ll_lov_user_md_size(lump);
273                 if (lum_size < 0 || size < lum_size)
274                         return -ERANGE;
275
276                 rc = ll_lov_setstripe_ea_info(inode, dentry, it_flags, lump,
277                                               lum_size);
278                 /**
279                  * b=10667: ignore -EEXIST.
280                  * Silently eat error on setting trusted.lov/lustre.lov
281                  * attribute for SuSE 9, it added default option to copy
282                  * all attributes in 'cp' command. rsync, tar --xattrs
283                  * also will try to set LOVEA for existing files.
284                  */
285                 if (rc == -EEXIST)
286                         rc = 0;
287         } else if (S_ISDIR(inode->i_mode)) {
288                 rc = ll_dir_setstripe(inode, lump, 0);
289         }
290
291         return rc;
292 }
293
294 int ll_setxattr(struct dentry *dentry, const char *name,
295                 const void *value, size_t size, int flags)
296 {
297         struct inode *inode = dentry->d_inode;
298
299         LASSERT(inode);
300         LASSERT(name);
301
302         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), xattr %s\n",
303                PFID(ll_inode2fid(inode)), inode, name);
304
305         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_SETXATTR, 1);
306
307         /* lustre/trusted.lov.xxx would be passed through xattr API */
308         if (strcmp(name, XATTR_NAME_LOV) == 0 ||
309             strcmp(name, XATTR_LUSTRE_LOV) == 0)
310                 return ll_setstripe_ea(dentry, (struct lov_user_md *)value,
311                                        size);
312         else if (strcmp(name, XATTR_NAME_LMA) == 0 ||
313                  strcmp(name, XATTR_NAME_LINK) == 0)
314                 return 0;
315
316         return ll_setxattr_common(inode, name, value, size, flags,
317                                   OBD_MD_FLXATTR);
318 }
319
320 int ll_removexattr(struct dentry *dentry, const char *name)
321 {
322         struct inode *inode = dentry->d_inode;
323
324         LASSERT(inode);
325         LASSERT(name);
326
327         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), xattr %s\n",
328                PFID(ll_inode2fid(inode)), inode, name);
329
330         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_REMOVEXATTR, 1);
331         return ll_setxattr_common(inode, name, NULL, 0, 0,
332                                   OBD_MD_FLXATTRRM);
333 }
334
335 int ll_getxattr_common(struct inode *inode, const char *name,
336                        void *buffer, size_t size, __u64 valid)
337 {
338         struct ll_sb_info *sbi = ll_i2sbi(inode);
339         struct ptlrpc_request *req = NULL;
340         struct mdt_body *body;
341         int xattr_type, rc;
342         void *xdata;
343         struct ll_inode_info *lli = ll_i2info(inode);
344         ENTRY;
345
346         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p)\n",
347                PFID(ll_inode2fid(inode)), inode);
348
349         /* listxattr have slightly different behavior from of ext3:
350          * without 'user_xattr' ext3 will list all xattr names but
351          * filtered out "^user..*"; we list them all for simplicity.
352          */
353         if (!name) {
354                 xattr_type = XATTR_OTHER_T;
355                 goto do_getxattr;
356         }
357
358         xattr_type = get_xattr_type(name);
359         rc = xattr_type_filter(sbi, xattr_type);
360         if (rc)
361                 RETURN(rc);
362
363         /* LU-549:  Disable security.selinux when selinux is disabled */
364         if (xattr_type == XATTR_SECURITY_T && !selinux_is_enabled() &&
365             strcmp(name, "security.selinux") == 0)
366                 RETURN(-EOPNOTSUPP);
367
368 #ifdef CONFIG_FS_POSIX_ACL
369         /* posix acl is under protection of LOOKUP lock. when calling to this,
370          * we just have path resolution to the target inode, so we have great
371          * chance that cached ACL is uptodate.
372          */
373         if (xattr_type == XATTR_ACL_ACCESS_T) {
374                 struct posix_acl *acl;
375
376                 spin_lock(&lli->lli_lock);
377                 acl = posix_acl_dup(lli->lli_posix_acl);
378                 spin_unlock(&lli->lli_lock);
379
380                 if (!acl)
381                         RETURN(-ENODATA);
382
383                 rc = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
384                 posix_acl_release(acl);
385                 RETURN(rc);
386         }
387         if (xattr_type == XATTR_ACL_DEFAULT_T && !S_ISDIR(inode->i_mode))
388                 RETURN(-ENODATA);
389 #endif
390
391 do_getxattr:
392         if (sbi->ll_xattr_cache_enabled &&
393             xattr_type != XATTR_ACL_ACCESS_T &&
394             (xattr_type != XATTR_SECURITY_T ||
395                 strcmp(name, "security.selinux") != 0)) {
396                 rc = ll_xattr_cache_get(inode, name, buffer, size, valid);
397                 if (rc == -EAGAIN)
398                         goto getxattr_nocache;
399                 if (rc < 0)
400                         GOTO(out_xattr, rc);
401
402                 /* Add "system.posix_acl_access" to the list */
403                 if (lli->lli_posix_acl != NULL && valid & OBD_MD_FLXATTRLS) {
404                         if (size == 0) {
405                                 rc += sizeof(XATTR_NAME_ACL_ACCESS);
406                         } else if (size - rc >= sizeof(XATTR_NAME_ACL_ACCESS)) {
407                                 memcpy(buffer + rc, XATTR_NAME_ACL_ACCESS,
408                                        sizeof(XATTR_NAME_ACL_ACCESS));
409                                 rc += sizeof(XATTR_NAME_ACL_ACCESS);
410                         } else {
411                                 GOTO(out_xattr, rc = -ERANGE);
412                         }
413                 }
414         } else {
415 getxattr_nocache:
416                 rc = md_getxattr(sbi->ll_md_exp, ll_inode2fid(inode),
417                                 valid, name, NULL, 0, size, 0, &req);
418
419                 if (rc < 0)
420                         GOTO(out_xattr, rc);
421
422                 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
423                 LASSERT(body);
424
425                 /* only detect the xattr size */
426                 if (size == 0)
427                         GOTO(out, rc = body->mbo_eadatasize);
428
429                 if (size < body->mbo_eadatasize) {
430                         CERROR("server bug: replied size %u > %u\n",
431                                 body->mbo_eadatasize, (int)size);
432                         GOTO(out, rc = -ERANGE);
433                 }
434
435                 if (body->mbo_eadatasize == 0)
436                         GOTO(out, rc = -ENODATA);
437
438                 /* do not need swab xattr data */
439                 xdata = req_capsule_server_sized_get(&req->rq_pill, &RMF_EADATA,
440                                                         body->mbo_eadatasize);
441                 if (!xdata)
442                         GOTO(out, rc = -EFAULT);
443
444                 memcpy(buffer, xdata, body->mbo_eadatasize);
445                 rc = body->mbo_eadatasize;
446         }
447
448         EXIT;
449
450 out_xattr:
451         if (rc == -EOPNOTSUPP && xattr_type == XATTR_USER_T) {
452                 LCONSOLE_INFO("%s: disabling user_xattr feature because "
453                                 "it is not supported on the server: rc = %d\n",
454                                 ll_get_fsname(inode->i_sb, NULL, 0), rc);
455                 sbi->ll_flags &= ~LL_SBI_USER_XATTR;
456         }
457 out:
458         ptlrpc_req_finished(req);
459         return rc;
460 }
461
462 static ssize_t ll_getxattr_lov(struct inode *inode, void *buf, size_t buf_size)
463 {
464         ssize_t rc;
465
466         if (S_ISREG(inode->i_mode)) {
467                 struct cl_object *obj = ll_i2info(inode)->lli_clob;
468                 struct lu_env *env;
469                 struct cl_layout cl = {
470                         .cl_buf.lb_buf = buf,
471                         .cl_buf.lb_len = buf_size,
472                 };
473                 __u16 refcheck;
474
475                 if (obj == NULL)
476                         RETURN(-ENODATA);
477
478                 env = cl_env_get(&refcheck);
479                 if (IS_ERR(env))
480                         RETURN(PTR_ERR(env));
481
482                 rc = cl_object_layout_get(env, obj, &cl);
483                 if (rc < 0)
484                         GOTO(out_env, rc);
485
486                 if (cl.cl_size == 0)
487                         GOTO(out_env, rc = -ENODATA);
488
489                 rc = cl.cl_size;
490
491                 if (buf_size == 0)
492                         GOTO(out_env, rc);
493
494                 LASSERT(buf != NULL && rc <= buf_size);
495
496                 /* Do not return layout gen for getxattr() since
497                  * otherwise it would confuse tar --xattr by
498                  * recognizing layout gen as stripe offset when the
499                  * file is restored. See LU-2809. */
500                 if (((struct lov_mds_md *)buf)->lmm_magic == LOV_MAGIC_COMP_V1)
501                         goto out_env;
502
503                 ((struct lov_mds_md *)buf)->lmm_layout_gen = 0;
504 out_env:
505                 cl_env_put(env, &refcheck);
506
507                 RETURN(rc);
508         } else if (S_ISDIR(inode->i_mode)) {
509                 struct lov_mds_md *lmm = NULL;
510                 int lmm_size = 0;
511                 struct ptlrpc_request *req = NULL;
512
513                 rc = ll_dir_getstripe(inode, (void **)&lmm, &lmm_size,
514                                       &req, 0);
515                 if (rc < 0)
516                         GOTO(out_req, rc);
517
518                 if (buf_size == 0)
519                         GOTO(out_req, rc = lmm_size);
520
521                 if (buf_size < lmm_size)
522                         GOTO(out_req, rc = -ERANGE);
523
524                 memcpy(buf, lmm, lmm_size);
525                 GOTO(out_req, rc = lmm_size);
526 out_req:
527                 if (req != NULL)
528                         ptlrpc_req_finished(req);
529
530                 return rc;
531         } else {
532                 RETURN(-ENODATA);
533         }
534 }
535
536 ssize_t ll_getxattr(struct dentry *dentry, const char *name, void *buf,
537                     size_t buf_size)
538 {
539         struct inode *inode = dentry->d_inode;
540
541         LASSERT(inode);
542         LASSERT(name);
543
544         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), xattr %s\n",
545                PFID(ll_inode2fid(inode)), inode, name);
546
547         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_GETXATTR, 1);
548
549         if (strcmp(name, XATTR_LUSTRE_LOV) == 0 ||
550             strcmp(name, XATTR_NAME_LOV) == 0)
551                 return ll_getxattr_lov(inode, buf, buf_size);
552         else
553                 return ll_getxattr_common(inode, name, buf, buf_size,
554                                           OBD_MD_FLXATTR);
555 }
556
557 ssize_t ll_listxattr(struct dentry *dentry, char *buf, size_t buf_size)
558 {
559         struct inode *inode = dentry->d_inode;
560         struct ll_sb_info *sbi = ll_i2sbi(inode);
561         char *xattr_name;
562         ssize_t rc, rc2;
563         size_t len, rem;
564
565         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p)\n",
566                PFID(ll_inode2fid(inode)), inode);
567
568         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_LISTXATTR, 1);
569
570         rc = ll_getxattr_common(inode, NULL, buf, buf_size, OBD_MD_FLXATTRLS);
571         if (rc < 0)
572                 RETURN(rc);
573
574         /* If we're being called to get the size of the xattr list
575          * (buf_size == 0) then just assume that a lustre.lov xattr
576          * exists. */
577         if (buf_size == 0)
578                 RETURN(rc + sizeof(XATTR_LUSTRE_LOV));
579
580         xattr_name = buf;
581         rem = rc;
582
583         while (rem > 0) {
584                 len = strnlen(xattr_name, rem - 1) + 1;
585                 rem -= len;
586                 if (xattr_type_filter(sbi, get_xattr_type(xattr_name)) == 0) {
587                         /* Skip OK xattr type, leave it in buffer. */
588                         xattr_name += len;
589                         continue;
590                 }
591
592                 /* Move up remaining xattrs in buffer removing the
593                  * xattr that is not OK. */
594                 memmove(xattr_name, xattr_name + len, rem);
595                 rc -= len;
596         }
597
598         rc2 = ll_getxattr_lov(inode, NULL, 0);
599         if (rc2 == -ENODATA)
600                 RETURN(rc);
601
602         if (rc2 < 0)
603                 RETURN(rc2);
604
605         if (buf_size < rc + sizeof(XATTR_LUSTRE_LOV))
606                 RETURN(-ERANGE);
607
608         memcpy(buf + rc, XATTR_LUSTRE_LOV, sizeof(XATTR_LUSTRE_LOV));
609
610         RETURN(rc + sizeof(XATTR_LUSTRE_LOV));
611 }