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