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