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