GNU Linux-libre 4.14.254-gnu1
[releases.git] / drivers / staging / lustre / 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, 2015, 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
44 #include "llite_internal.h"
45
46 const struct xattr_handler *get_xattr_type(const char *name)
47 {
48         int i = 0;
49
50         while (ll_xattr_handlers[i]) {
51                 size_t len = strlen(ll_xattr_handlers[i]->prefix);
52
53                 if (!strncmp(ll_xattr_handlers[i]->prefix, name, len))
54                         return ll_xattr_handlers[i];
55                 i++;
56         }
57         return NULL;
58 }
59
60 static int xattr_type_filter(struct ll_sb_info *sbi,
61                              const struct xattr_handler *handler)
62 {
63         /* No handler means XATTR_OTHER_T */
64         if (!handler)
65                 return -EOPNOTSUPP;
66
67         if ((handler->flags == XATTR_ACL_ACCESS_T ||
68              handler->flags == XATTR_ACL_DEFAULT_T) &&
69            !(sbi->ll_flags & LL_SBI_ACL))
70                 return -EOPNOTSUPP;
71
72         if (handler->flags == XATTR_USER_T &&
73             !(sbi->ll_flags & LL_SBI_USER_XATTR))
74                 return -EOPNOTSUPP;
75
76         if (handler->flags == XATTR_TRUSTED_T &&
77             !capable(CFS_CAP_SYS_ADMIN))
78                 return -EPERM;
79
80         return 0;
81 }
82
83 static int
84 ll_xattr_set_common(const struct xattr_handler *handler,
85                     struct dentry *dentry, struct inode *inode,
86                     const char *name, const void *value, size_t size,
87                     int flags)
88 {
89         char fullname[strlen(handler->prefix) + strlen(name) + 1];
90         struct ll_sb_info *sbi = ll_i2sbi(inode);
91         struct ptlrpc_request *req = NULL;
92         const char *pv = value;
93         __u64 valid;
94         int rc;
95
96         /* When setxattr() is called with a size of 0 the value is
97          * unconditionally replaced by "". When removexattr() is
98          * called we get a NULL value and XATTR_REPLACE for flags.
99          */
100         if (!value && flags == XATTR_REPLACE) {
101                 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_REMOVEXATTR, 1);
102                 valid = OBD_MD_FLXATTRRM;
103         } else {
104                 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_SETXATTR, 1);
105                 valid = OBD_MD_FLXATTR;
106         }
107
108         rc = xattr_type_filter(sbi, handler);
109         if (rc)
110                 return rc;
111
112         if ((handler->flags == XATTR_ACL_ACCESS_T ||
113              handler->flags == XATTR_ACL_DEFAULT_T) &&
114             !inode_owner_or_capable(inode))
115                 return -EPERM;
116
117         /* b10667: ignore lustre special xattr for now */
118         if (!strcmp(name, "hsm") ||
119             ((handler->flags == XATTR_TRUSTED_T && !strcmp(name, "lov")) ||
120              (handler->flags == XATTR_LUSTRE_T && !strcmp(name, "lov"))))
121                 return 0;
122
123         /* b15587: ignore security.capability xattr for now */
124         if ((handler->flags == XATTR_SECURITY_T &&
125              !strcmp(name, "capability")))
126                 return 0;
127
128         /* LU-549:  Disable security.selinux when selinux is disabled */
129         if (handler->flags == XATTR_SECURITY_T && !selinux_is_enabled() &&
130             strcmp(name, "selinux") == 0)
131                 return -EOPNOTSUPP;
132
133         /*FIXME: enable IMA when the conditions are ready */
134         if (handler->flags == XATTR_SECURITY_T &&
135             (!strcmp(name, "ima") || !strcmp(name, "evm")))
136                 return -EOPNOTSUPP;
137
138         /*
139          * In user.* namespace, only regular files and directories can have
140          * extended attributes.
141          */
142         if (handler->flags == XATTR_USER_T) {
143                 if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
144                         return -EPERM;
145         }
146
147         sprintf(fullname, "%s%s\n", handler->prefix, name);
148         rc = md_setxattr(sbi->ll_md_exp, ll_inode2fid(inode),
149                          valid, fullname, pv, size, 0, flags,
150                          ll_i2suppgid(inode), &req);
151         if (rc) {
152                 if (rc == -EOPNOTSUPP && handler->flags == XATTR_USER_T) {
153                         LCONSOLE_INFO("Disabling user_xattr feature because it is not supported on the server\n");
154                         sbi->ll_flags &= ~LL_SBI_USER_XATTR;
155                 }
156                 return rc;
157         }
158
159         ptlrpc_req_finished(req);
160         return 0;
161 }
162
163 static int get_hsm_state(struct inode *inode, u32 *hus_states)
164 {
165         struct md_op_data *op_data;
166         struct hsm_user_state *hus;
167         int rc;
168
169         hus = kzalloc(sizeof(*hus), GFP_NOFS);
170         if (!hus)
171                 return -ENOMEM;
172
173         op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
174                                      LUSTRE_OPC_ANY, hus);
175         if (!IS_ERR(op_data)) {
176                 rc = obd_iocontrol(LL_IOC_HSM_STATE_GET, ll_i2mdexp(inode),
177                                    sizeof(*op_data), op_data, NULL);
178                 if (!rc)
179                         *hus_states = hus->hus_states;
180                 else
181                         CDEBUG(D_VFSTRACE, "obd_iocontrol failed. rc = %d\n",
182                                rc);
183
184                 ll_finish_md_op_data(op_data);
185         } else {
186                 rc = PTR_ERR(op_data);
187                 CDEBUG(D_VFSTRACE, "Could not prepare the opdata. rc = %d\n",
188                        rc);
189         }
190         kfree(hus);
191         return rc;
192 }
193
194 static int ll_xattr_set(const struct xattr_handler *handler,
195                         struct dentry *dentry, struct inode *inode,
196                         const char *name, const void *value, size_t size,
197                         int flags)
198 {
199         LASSERT(inode);
200         LASSERT(name);
201
202         CDEBUG(D_VFSTRACE, "VFS Op:inode=" DFID "(%p), xattr %s\n",
203                PFID(ll_inode2fid(inode)), inode, name);
204
205         if (!strcmp(name, "lov")) {
206                 struct lov_user_md *lump = (struct lov_user_md *)value;
207                 int op_type = flags == XATTR_REPLACE ? LPROC_LL_REMOVEXATTR :
208                                                        LPROC_LL_SETXATTR;
209                 int rc = 0;
210
211                 ll_stats_ops_tally(ll_i2sbi(inode), op_type, 1);
212
213                 if (size != 0 && size < sizeof(struct lov_user_md))
214                         return -EINVAL;
215
216                 /*
217                  * It is possible to set an xattr to a "" value of zero size.
218                  * For this case we are going to treat it as a removal.
219                  */
220                 if (!size && lump)
221                         lump = NULL;
222
223                 /* Attributes that are saved via getxattr will always have
224                  * the stripe_offset as 0.  Instead, the MDS should be
225                  * allowed to pick the starting OST index.   b=17846
226                  */
227                 if (lump && lump->lmm_stripe_offset == 0)
228                         lump->lmm_stripe_offset = -1;
229
230                 /* Avoid anyone directly setting the RELEASED flag. */
231                 if (lump && (lump->lmm_pattern & LOV_PATTERN_F_RELEASED)) {
232                         /* Only if we have a released flag check if the file
233                          * was indeed archived.
234                          */
235                         u32 state = HS_NONE;
236
237                         rc = get_hsm_state(inode, &state);
238                         if (rc)
239                                 return rc;
240
241                         if (!(state & HS_ARCHIVED)) {
242                                 CDEBUG(D_VFSTRACE,
243                                        "hus_states state = %x, pattern = %x\n",
244                                 state, lump->lmm_pattern);
245                                 /*
246                                  * Here the state is: real file is not
247                                  * archived but user is requesting to set
248                                  * the RELEASED flag so we mask off the
249                                  * released flag from the request
250                                  */
251                                 lump->lmm_pattern ^= LOV_PATTERN_F_RELEASED;
252                         }
253                 }
254
255                 if (lump && S_ISREG(inode->i_mode)) {
256                         __u64 it_flags = FMODE_WRITE;
257                         int lum_size;
258
259                         lum_size = ll_lov_user_md_size(lump);
260                         if (lum_size < 0 || size < lum_size)
261                                 return 0; /* b=10667: ignore error */
262
263                         rc = ll_lov_setstripe_ea_info(inode, dentry, it_flags,
264                                                       lump, lum_size);
265                         /* b=10667: rc always be 0 here for now */
266                         rc = 0;
267                 } else if (S_ISDIR(inode->i_mode)) {
268                         rc = ll_dir_setstripe(inode, lump, 0);
269                 }
270
271                 return rc;
272
273         } else if (!strcmp(name, "lma") || !strcmp(name, "link")) {
274                 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_SETXATTR, 1);
275                 return 0;
276         }
277
278         return ll_xattr_set_common(handler, dentry, inode, name, value, size,
279                                    flags);
280 }
281
282 int
283 ll_xattr_list(struct inode *inode, const char *name, int type, void *buffer,
284               size_t size, __u64 valid)
285 {
286         struct ll_inode_info *lli = ll_i2info(inode);
287         struct ll_sb_info *sbi = ll_i2sbi(inode);
288         struct ptlrpc_request *req = NULL;
289         struct mdt_body *body;
290         void *xdata;
291         int rc;
292
293         if (sbi->ll_xattr_cache_enabled && type != XATTR_ACL_ACCESS_T &&
294             (type != XATTR_SECURITY_T || strcmp(name, "security.selinux"))) {
295                 rc = ll_xattr_cache_get(inode, name, buffer, size, valid);
296                 if (rc == -EAGAIN)
297                         goto getxattr_nocache;
298                 if (rc < 0)
299                         goto out_xattr;
300
301                 /* Add "system.posix_acl_access" to the list */
302                 if (lli->lli_posix_acl && valid & OBD_MD_FLXATTRLS) {
303                         if (size == 0) {
304                                 rc += sizeof(XATTR_NAME_ACL_ACCESS);
305                         } else if (size - rc >= sizeof(XATTR_NAME_ACL_ACCESS)) {
306                                 memcpy(buffer + rc, XATTR_NAME_ACL_ACCESS,
307                                        sizeof(XATTR_NAME_ACL_ACCESS));
308                                 rc += sizeof(XATTR_NAME_ACL_ACCESS);
309                         } else {
310                                 rc = -ERANGE;
311                                 goto out_xattr;
312                         }
313                 }
314         } else {
315 getxattr_nocache:
316                 rc = md_getxattr(sbi->ll_md_exp, ll_inode2fid(inode),
317                                  valid, name, NULL, 0, size, 0, &req);
318                 if (rc < 0)
319                         goto out_xattr;
320
321                 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
322                 LASSERT(body);
323
324                 /* only detect the xattr size */
325                 if (size == 0) {
326                         rc = body->mbo_eadatasize;
327                         goto out;
328                 }
329
330                 if (size < body->mbo_eadatasize) {
331                         CERROR("server bug: replied size %u > %u\n",
332                                body->mbo_eadatasize, (int)size);
333                         rc = -ERANGE;
334                         goto out;
335                 }
336
337                 if (body->mbo_eadatasize == 0) {
338                         rc = -ENODATA;
339                         goto out;
340                 }
341
342                 /* do not need swab xattr data */
343                 xdata = req_capsule_server_sized_get(&req->rq_pill, &RMF_EADATA,
344                                                      body->mbo_eadatasize);
345                 if (!xdata) {
346                         rc = -EFAULT;
347                         goto out;
348                 }
349
350                 memcpy(buffer, xdata, body->mbo_eadatasize);
351                 rc = body->mbo_eadatasize;
352         }
353
354 out_xattr:
355         if (rc == -EOPNOTSUPP && type == XATTR_USER_T) {
356                 LCONSOLE_INFO(
357                         "%s: disabling user_xattr feature because it is not supported on the server: rc = %d\n",
358                         ll_get_fsname(inode->i_sb, NULL, 0), rc);
359                 sbi->ll_flags &= ~LL_SBI_USER_XATTR;
360         }
361 out:
362         ptlrpc_req_finished(req);
363         return rc;
364 }
365
366 static int ll_xattr_get_common(const struct xattr_handler *handler,
367                                struct dentry *dentry, struct inode *inode,
368                                const char *name, void *buffer, size_t size)
369 {
370         char fullname[strlen(handler->prefix) + strlen(name) + 1];
371         struct ll_sb_info *sbi = ll_i2sbi(inode);
372 #ifdef CONFIG_FS_POSIX_ACL
373         struct ll_inode_info *lli = ll_i2info(inode);
374 #endif
375         int rc;
376
377         CDEBUG(D_VFSTRACE, "VFS Op:inode=" DFID "(%p)\n",
378                PFID(ll_inode2fid(inode)), inode);
379
380         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_GETXATTR, 1);
381
382         rc = xattr_type_filter(sbi, handler);
383         if (rc)
384                 return rc;
385
386         /* b15587: ignore security.capability xattr for now */
387         if ((handler->flags == XATTR_SECURITY_T && !strcmp(name, "capability")))
388                 return -ENODATA;
389
390         /* LU-549:  Disable security.selinux when selinux is disabled */
391         if (handler->flags == XATTR_SECURITY_T && !selinux_is_enabled() &&
392             !strcmp(name, "selinux"))
393                 return -EOPNOTSUPP;
394
395 #ifdef CONFIG_FS_POSIX_ACL
396         /* posix acl is under protection of LOOKUP lock. when calling to this,
397          * we just have path resolution to the target inode, so we have great
398          * chance that cached ACL is uptodate.
399          */
400         if (handler->flags == XATTR_ACL_ACCESS_T) {
401                 struct posix_acl *acl;
402
403                 spin_lock(&lli->lli_lock);
404                 acl = posix_acl_dup(lli->lli_posix_acl);
405                 spin_unlock(&lli->lli_lock);
406
407                 if (!acl)
408                         return -ENODATA;
409
410                 rc = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
411                 posix_acl_release(acl);
412                 return rc;
413         }
414         if (handler->flags == XATTR_ACL_DEFAULT_T && !S_ISDIR(inode->i_mode))
415                 return -ENODATA;
416 #endif
417         sprintf(fullname, "%s%s\n", handler->prefix, name);
418         return ll_xattr_list(inode, fullname, handler->flags, buffer, size,
419                              OBD_MD_FLXATTR);
420 }
421
422 static ssize_t ll_getxattr_lov(struct inode *inode, void *buf, size_t buf_size)
423 {
424         ssize_t rc;
425
426         if (S_ISREG(inode->i_mode)) {
427                 struct cl_object *obj = ll_i2info(inode)->lli_clob;
428                 struct cl_layout cl = {
429                         .cl_buf.lb_buf = buf,
430                         .cl_buf.lb_len = buf_size,
431                 };
432                 struct lu_env *env;
433                 u16 refcheck;
434
435                 if (!obj)
436                         return -ENODATA;
437
438                 env = cl_env_get(&refcheck);
439                 if (IS_ERR(env))
440                         return PTR_ERR(env);
441
442                 rc = cl_object_layout_get(env, obj, &cl);
443                 if (rc < 0)
444                         goto out_env;
445
446                 if (!cl.cl_size) {
447                         rc = -ENODATA;
448                         goto out_env;
449                 }
450
451                 rc = cl.cl_size;
452
453                 if (!buf_size)
454                         goto out_env;
455
456                 LASSERT(buf && rc <= buf_size);
457
458                 /*
459                  * Do not return layout gen for getxattr() since
460                  * otherwise it would confuse tar --xattr by
461                  * recognizing layout gen as stripe offset when the
462                  * file is restored. See LU-2809.
463                  */
464                 ((struct lov_mds_md *)buf)->lmm_layout_gen = 0;
465 out_env:
466                 cl_env_put(env, &refcheck);
467
468                 return rc;
469         } else if (S_ISDIR(inode->i_mode)) {
470                 struct ptlrpc_request *req = NULL;
471                 struct lov_mds_md *lmm = NULL;
472                 int lmm_size = 0;
473
474                 rc = ll_dir_getstripe(inode, (void **)&lmm, &lmm_size,
475                                       &req, 0);
476                 if (rc < 0)
477                         goto out_req;
478
479                 if (!buf_size) {
480                         rc = lmm_size;
481                         goto out_req;
482                 }
483
484                 if (buf_size < lmm_size) {
485                         rc = -ERANGE;
486                         goto out_req;
487                 }
488
489                 memcpy(buf, lmm, lmm_size);
490                 rc = lmm_size;
491 out_req:
492                 if (req)
493                         ptlrpc_req_finished(req);
494
495                 return rc;
496         } else {
497                 return -ENODATA;
498         }
499 }
500
501 static int ll_xattr_get(const struct xattr_handler *handler,
502                         struct dentry *dentry, struct inode *inode,
503                         const char *name, void *buffer, size_t size)
504 {
505         LASSERT(inode);
506         LASSERT(name);
507
508         CDEBUG(D_VFSTRACE, "VFS Op:inode=" DFID "(%p), xattr %s\n",
509                PFID(ll_inode2fid(inode)), inode, name);
510
511         if (!strcmp(name, "lov")) {
512                 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_GETXATTR, 1);
513
514                 return ll_getxattr_lov(inode, buffer, size);
515         }
516
517         return ll_xattr_get_common(handler, dentry, inode, name, buffer, size);
518 }
519
520 ssize_t ll_listxattr(struct dentry *dentry, char *buffer, size_t size)
521 {
522         struct inode *inode = d_inode(dentry);
523         struct ll_sb_info *sbi = ll_i2sbi(inode);
524         char *xattr_name;
525         ssize_t rc, rc2;
526         size_t len, rem;
527
528         LASSERT(inode);
529
530         CDEBUG(D_VFSTRACE, "VFS Op:inode=" DFID "(%p)\n",
531                PFID(ll_inode2fid(inode)), inode);
532
533         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_LISTXATTR, 1);
534
535         rc = ll_xattr_list(inode, NULL, XATTR_OTHER_T, buffer, size,
536                            OBD_MD_FLXATTRLS);
537         if (rc < 0)
538                 return rc;
539         /*
540          * If we're being called to get the size of the xattr list
541          * (buf_size == 0) then just assume that a lustre.lov xattr
542          * exists.
543          */
544         if (!size)
545                 return rc + sizeof(XATTR_LUSTRE_LOV);
546
547         xattr_name = buffer;
548         rem = rc;
549
550         while (rem > 0) {
551                 len = strnlen(xattr_name, rem - 1) + 1;
552                 rem -= len;
553                 if (!xattr_type_filter(sbi, get_xattr_type(xattr_name))) {
554                         /* Skip OK xattr type leave it in buffer */
555                         xattr_name += len;
556                         continue;
557                 }
558
559                 /*
560                  * Move up remaining xattrs in buffer
561                  * removing the xattr that is not OK
562                  */
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 (size < rc + sizeof(XATTR_LUSTRE_LOV))
575                 return -ERANGE;
576
577         memcpy(buffer + rc, XATTR_LUSTRE_LOV, sizeof(XATTR_LUSTRE_LOV));
578
579         return rc + sizeof(XATTR_LUSTRE_LOV);
580 }
581
582 static const struct xattr_handler ll_user_xattr_handler = {
583         .prefix = XATTR_USER_PREFIX,
584         .flags = XATTR_USER_T,
585         .get = ll_xattr_get_common,
586         .set = ll_xattr_set_common,
587 };
588
589 static const struct xattr_handler ll_trusted_xattr_handler = {
590         .prefix = XATTR_TRUSTED_PREFIX,
591         .flags = XATTR_TRUSTED_T,
592         .get = ll_xattr_get,
593         .set = ll_xattr_set,
594 };
595
596 static const struct xattr_handler ll_security_xattr_handler = {
597         .prefix = XATTR_SECURITY_PREFIX,
598         .flags = XATTR_SECURITY_T,
599         .get = ll_xattr_get_common,
600         .set = ll_xattr_set_common,
601 };
602
603 static const struct xattr_handler ll_acl_access_xattr_handler = {
604         .prefix = XATTR_NAME_POSIX_ACL_ACCESS,
605         .flags = XATTR_ACL_ACCESS_T,
606         .get = ll_xattr_get_common,
607         .set = ll_xattr_set_common,
608 };
609
610 static const struct xattr_handler ll_acl_default_xattr_handler = {
611         .prefix = XATTR_NAME_POSIX_ACL_DEFAULT,
612         .flags = XATTR_ACL_DEFAULT_T,
613         .get = ll_xattr_get_common,
614         .set = ll_xattr_set_common,
615 };
616
617 static const struct xattr_handler ll_lustre_xattr_handler = {
618         .prefix = XATTR_LUSTRE_PREFIX,
619         .flags = XATTR_LUSTRE_T,
620         .get = ll_xattr_get,
621         .set = ll_xattr_set,
622 };
623
624 const struct xattr_handler *ll_xattr_handlers[] = {
625         &ll_user_xattr_handler,
626         &ll_trusted_xattr_handler,
627         &ll_security_xattr_handler,
628 #ifdef CONFIG_FS_POSIX_ACL
629         &ll_acl_access_xattr_handler,
630         &ll_acl_default_xattr_handler,
631 #endif
632         &ll_lustre_xattr_handler,
633         NULL,
634 };