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