GNU Linux-libre 4.9.309-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/selinux.h>
37
38 #define DEBUG_SUBSYSTEM S_LLITE
39
40 #include "../include/obd_support.h"
41 #include "../include/lustre_dlm.h"
42 #include "../include/lustre_ver.h"
43 #include "../include/lustre_eacl.h"
44
45 #include "llite_internal.h"
46
47 static
48 int get_xattr_type(const char *name)
49 {
50         if (!strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS))
51                 return XATTR_ACL_ACCESS_T;
52
53         if (!strcmp(name, XATTR_NAME_POSIX_ACL_DEFAULT))
54                 return XATTR_ACL_DEFAULT_T;
55
56         if (!strncmp(name, XATTR_USER_PREFIX,
57                      sizeof(XATTR_USER_PREFIX) - 1))
58                 return XATTR_USER_T;
59
60         if (!strncmp(name, XATTR_TRUSTED_PREFIX,
61                      sizeof(XATTR_TRUSTED_PREFIX) - 1))
62                 return XATTR_TRUSTED_T;
63
64         if (!strncmp(name, XATTR_SECURITY_PREFIX,
65                      sizeof(XATTR_SECURITY_PREFIX) - 1))
66                 return XATTR_SECURITY_T;
67
68         if (!strncmp(name, XATTR_LUSTRE_PREFIX,
69                      sizeof(XATTR_LUSTRE_PREFIX) - 1))
70                 return XATTR_LUSTRE_T;
71
72         return XATTR_OTHER_T;
73 }
74
75 static
76 int xattr_type_filter(struct ll_sb_info *sbi, int xattr_type)
77 {
78         if ((xattr_type == XATTR_ACL_ACCESS_T ||
79              xattr_type == XATTR_ACL_DEFAULT_T) &&
80            !(sbi->ll_flags & LL_SBI_ACL))
81                 return -EOPNOTSUPP;
82
83         if (xattr_type == XATTR_USER_T && !(sbi->ll_flags & LL_SBI_USER_XATTR))
84                 return -EOPNOTSUPP;
85         if (xattr_type == XATTR_TRUSTED_T && !capable(CFS_CAP_SYS_ADMIN))
86                 return -EPERM;
87         if (xattr_type == XATTR_OTHER_T)
88                 return -EOPNOTSUPP;
89
90         return 0;
91 }
92
93 static int
94 ll_xattr_set_common(const struct xattr_handler *handler,
95                     struct dentry *dentry, struct inode *inode,
96                     const char *name, const void *value, size_t size,
97                     int flags)
98 {
99         char fullname[strlen(handler->prefix) + strlen(name) + 1];
100         struct ll_sb_info *sbi = ll_i2sbi(inode);
101         struct ptlrpc_request *req = NULL;
102         const char *pv = value;
103         __u64 valid;
104         int rc;
105
106         /* When setxattr() is called with a size of 0 the value is
107          * unconditionally replaced by "". When removexattr() is
108          * called we get a NULL value and XATTR_REPLACE for flags.
109          */
110         if (!value && flags == XATTR_REPLACE) {
111                 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_REMOVEXATTR, 1);
112                 valid = OBD_MD_FLXATTRRM;
113         } else {
114                 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_SETXATTR, 1);
115                 valid = OBD_MD_FLXATTR;
116         }
117
118         rc = xattr_type_filter(sbi, handler->flags);
119         if (rc)
120                 return rc;
121
122         if ((handler->flags == XATTR_ACL_ACCESS_T ||
123              handler->flags == XATTR_ACL_DEFAULT_T) &&
124             !inode_owner_or_capable(inode))
125                 return -EPERM;
126
127         /* b10667: ignore lustre special xattr for now */
128         if ((handler->flags == XATTR_TRUSTED_T && !strcmp(name, "lov")) ||
129             (handler->flags == XATTR_LUSTRE_T && !strcmp(name, "lov")))
130                 return 0;
131
132         /* b15587: ignore security.capability xattr for now */
133         if ((handler->flags == XATTR_SECURITY_T &&
134              !strcmp(name, "capability")))
135                 return 0;
136
137         /* LU-549:  Disable security.selinux when selinux is disabled */
138         if (handler->flags == XATTR_SECURITY_T && !selinux_is_enabled() &&
139             strcmp(name, "selinux") == 0)
140                 return -EOPNOTSUPP;
141
142         sprintf(fullname, "%s%s\n", handler->prefix, name);
143         rc = md_setxattr(sbi->ll_md_exp, ll_inode2fid(inode),
144                          valid, fullname, pv, size, 0, flags,
145                          ll_i2suppgid(inode), &req);
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 ll_xattr_set(const struct xattr_handler *handler,
159                         struct dentry *dentry, struct inode *inode,
160                         const char *name, const void *value, size_t size,
161                         int flags)
162 {
163         LASSERT(inode);
164         LASSERT(name);
165
166         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), xattr %s\n",
167                PFID(ll_inode2fid(inode)), inode, name);
168
169         if (!strcmp(name, "lov")) {
170                 struct lov_user_md *lump = (struct lov_user_md *)value;
171                 int op_type = flags == XATTR_REPLACE ? LPROC_LL_REMOVEXATTR :
172                                                        LPROC_LL_SETXATTR;
173                 int rc = 0;
174
175                 ll_stats_ops_tally(ll_i2sbi(inode), op_type, 1);
176
177                 if (size != 0 && size < sizeof(struct lov_user_md))
178                         return -EINVAL;
179
180                 /*
181                  * It is possible to set an xattr to a "" value of zero size.
182                  * For this case we are going to treat it as a removal.
183                  */
184                 if (!size && lump)
185                         lump = NULL;
186
187                 /* Attributes that are saved via getxattr will always have
188                  * the stripe_offset as 0.  Instead, the MDS should be
189                  * allowed to pick the starting OST index.   b=17846
190                  */
191                 if (lump && lump->lmm_stripe_offset == 0)
192                         lump->lmm_stripe_offset = -1;
193
194                 if (lump && S_ISREG(inode->i_mode)) {
195                         __u64 it_flags = FMODE_WRITE;
196                         int lum_size;
197
198                         lum_size = ll_lov_user_md_size(lump);
199                         if (lum_size < 0 || size < lum_size)
200                                 return 0; /* b=10667: ignore error */
201
202                         rc = ll_lov_setstripe_ea_info(inode, dentry, it_flags,
203                                                       lump, lum_size);
204                         /* b=10667: rc always be 0 here for now */
205                         rc = 0;
206                 } else if (S_ISDIR(inode->i_mode)) {
207                         rc = ll_dir_setstripe(inode, lump, 0);
208                 }
209
210                 return rc;
211
212         } else if (!strcmp(name, "lma") || !strcmp(name, "link")) {
213                 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_SETXATTR, 1);
214                 return 0;
215         }
216
217         return ll_xattr_set_common(handler, dentry, inode, name, value, size,
218                                    flags);
219 }
220
221 int
222 ll_xattr_list(struct inode *inode, const char *name, int type, void *buffer,
223               size_t size, __u64 valid)
224 {
225         struct ll_inode_info *lli = ll_i2info(inode);
226         struct ll_sb_info *sbi = ll_i2sbi(inode);
227         struct ptlrpc_request *req = NULL;
228         struct mdt_body *body;
229         void *xdata;
230         int rc;
231
232         if (sbi->ll_xattr_cache_enabled && type != XATTR_ACL_ACCESS_T) {
233                 rc = ll_xattr_cache_get(inode, name, buffer, size, valid);
234                 if (rc == -EAGAIN)
235                         goto getxattr_nocache;
236                 if (rc < 0)
237                         goto out_xattr;
238
239                 /* Add "system.posix_acl_access" to the list */
240                 if (lli->lli_posix_acl && valid & OBD_MD_FLXATTRLS) {
241                         if (size == 0) {
242                                 rc += sizeof(XATTR_NAME_ACL_ACCESS);
243                         } else if (size - rc >= sizeof(XATTR_NAME_ACL_ACCESS)) {
244                                 memcpy(buffer + rc, XATTR_NAME_ACL_ACCESS,
245                                        sizeof(XATTR_NAME_ACL_ACCESS));
246                                 rc += sizeof(XATTR_NAME_ACL_ACCESS);
247                         } else {
248                                 rc = -ERANGE;
249                                 goto out_xattr;
250                         }
251                 }
252         } else {
253 getxattr_nocache:
254                 rc = md_getxattr(sbi->ll_md_exp, ll_inode2fid(inode),
255                                  valid, name, NULL, 0, size, 0, &req);
256                 if (rc < 0)
257                         goto out_xattr;
258
259                 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
260                 LASSERT(body);
261
262                 /* only detect the xattr size */
263                 if (size == 0) {
264                         rc = body->mbo_eadatasize;
265                         goto out;
266                 }
267
268                 if (size < body->mbo_eadatasize) {
269                         CERROR("server bug: replied size %u > %u\n",
270                                body->mbo_eadatasize, (int)size);
271                         rc = -ERANGE;
272                         goto out;
273                 }
274
275                 if (body->mbo_eadatasize == 0) {
276                         rc = -ENODATA;
277                         goto out;
278                 }
279
280                 /* do not need swab xattr data */
281                 xdata = req_capsule_server_sized_get(&req->rq_pill, &RMF_EADATA,
282                                                      body->mbo_eadatasize);
283                 if (!xdata) {
284                         rc = -EFAULT;
285                         goto out;
286                 }
287
288                 memcpy(buffer, xdata, body->mbo_eadatasize);
289                 rc = body->mbo_eadatasize;
290         }
291
292 out_xattr:
293         if (rc == -EOPNOTSUPP && type == XATTR_USER_T) {
294                 LCONSOLE_INFO(
295                         "%s: disabling user_xattr feature because it is not supported on the server: rc = %d\n",
296                         ll_get_fsname(inode->i_sb, NULL, 0), rc);
297                 sbi->ll_flags &= ~LL_SBI_USER_XATTR;
298         }
299 out:
300         ptlrpc_req_finished(req);
301         return rc;
302 }
303
304 static int ll_xattr_get_common(const struct xattr_handler *handler,
305                                struct dentry *dentry, struct inode *inode,
306                                const char *name, void *buffer, size_t size)
307 {
308         char fullname[strlen(handler->prefix) + strlen(name) + 1];
309         struct ll_sb_info *sbi = ll_i2sbi(inode);
310 #ifdef CONFIG_FS_POSIX_ACL
311         struct ll_inode_info *lli = ll_i2info(inode);
312 #endif
313         int rc;
314
315         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p)\n",
316                PFID(ll_inode2fid(inode)), inode);
317
318         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_GETXATTR, 1);
319
320         rc = xattr_type_filter(sbi, handler->flags);
321         if (rc)
322                 return rc;
323
324         /* b15587: ignore security.capability xattr for now */
325         if ((handler->flags == XATTR_SECURITY_T && !strcmp(name, "capability")))
326                 return -ENODATA;
327
328         /* LU-549:  Disable security.selinux when selinux is disabled */
329         if (handler->flags == XATTR_SECURITY_T && !selinux_is_enabled() &&
330             !strcmp(name, "selinux"))
331                 return -EOPNOTSUPP;
332
333 #ifdef CONFIG_FS_POSIX_ACL
334         /* posix acl is under protection of LOOKUP lock. when calling to this,
335          * we just have path resolution to the target inode, so we have great
336          * chance that cached ACL is uptodate.
337          */
338         if (handler->flags == XATTR_ACL_ACCESS_T) {
339                 struct posix_acl *acl;
340
341                 spin_lock(&lli->lli_lock);
342                 acl = posix_acl_dup(lli->lli_posix_acl);
343                 spin_unlock(&lli->lli_lock);
344
345                 if (!acl)
346                         return -ENODATA;
347
348                 rc = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
349                 posix_acl_release(acl);
350                 return rc;
351         }
352         if (handler->flags == XATTR_ACL_DEFAULT_T && !S_ISDIR(inode->i_mode))
353                 return -ENODATA;
354 #endif
355         sprintf(fullname, "%s%s\n", handler->prefix, name);
356         return ll_xattr_list(inode, fullname, handler->flags, buffer, size,
357                              OBD_MD_FLXATTR);
358 }
359
360 static int ll_xattr_get(const struct xattr_handler *handler,
361                         struct dentry *dentry, struct inode *inode,
362                         const char *name, void *buffer, size_t size)
363 {
364         LASSERT(inode);
365         LASSERT(name);
366
367         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), xattr %s\n",
368                PFID(ll_inode2fid(inode)), inode, name);
369
370         if (!strcmp(name, "lov")) {
371                 struct lov_stripe_md *lsm;
372                 struct lov_user_md *lump;
373                 struct lov_mds_md *lmm = NULL;
374                 struct ptlrpc_request *request = NULL;
375                 int rc = 0, lmmsize = 0;
376
377                 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_GETXATTR, 1);
378
379                 if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
380                         return -ENODATA;
381
382                 lsm = ccc_inode_lsm_get(inode);
383                 if (!lsm) {
384                         if (S_ISDIR(inode->i_mode)) {
385                                 rc = ll_dir_getstripe(inode, (void **)&lmm,
386                                                       &lmmsize, &request, 0);
387                         } else {
388                                 rc = -ENODATA;
389                         }
390                 } else {
391                         /* LSM is present already after lookup/getattr call.
392                          * we need to grab layout lock once it is implemented
393                          */
394                         rc = obd_packmd(ll_i2dtexp(inode), &lmm, lsm);
395                         lmmsize = rc;
396                 }
397                 ccc_inode_lsm_put(inode, lsm);
398
399                 if (rc < 0)
400                         goto out;
401
402                 if (size == 0) {
403                         /* used to call ll_get_max_mdsize() forward to get
404                          * the maximum buffer size, while some apps (such as
405                          * rsync 3.0.x) care much about the exact xattr value
406                          * size
407                          */
408                         rc = lmmsize;
409                         goto out;
410                 }
411
412                 if (size < lmmsize) {
413                         CERROR("server bug: replied size %d > %d for %pd (%s)\n",
414                                lmmsize, (int)size, dentry, name);
415                         rc = -ERANGE;
416                         goto out;
417                 }
418
419                 lump = buffer;
420                 memcpy(lump, lmm, lmmsize);
421                 /* do not return layout gen for getxattr otherwise it would
422                  * confuse tar --xattr by recognizing layout gen as stripe
423                  * offset when the file is restored. See LU-2809.
424                  */
425                 lump->lmm_layout_gen = 0;
426
427                 rc = lmmsize;
428 out:
429                 if (request)
430                         ptlrpc_req_finished(request);
431                 else if (lmm)
432                         obd_free_diskmd(ll_i2dtexp(inode), &lmm);
433                 return rc;
434         }
435
436         return ll_xattr_get_common(handler, dentry, inode, name, buffer, size);
437 }
438
439 ssize_t ll_listxattr(struct dentry *dentry, char *buffer, size_t size)
440 {
441         struct inode *inode = d_inode(dentry);
442         int rc = 0, rc2 = 0;
443         struct lov_mds_md *lmm = NULL;
444         struct ptlrpc_request *request = NULL;
445         int lmmsize;
446
447         LASSERT(inode);
448
449         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p)\n",
450                PFID(ll_inode2fid(inode)), inode);
451
452         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_LISTXATTR, 1);
453
454         rc = ll_xattr_list(inode, NULL, XATTR_OTHER_T, buffer, size,
455                            OBD_MD_FLXATTRLS);
456         if (rc < 0)
457                 goto out;
458
459         if (buffer) {
460                 struct ll_sb_info *sbi = ll_i2sbi(inode);
461                 char *xattr_name = buffer;
462                 int xlen, rem = rc;
463
464                 while (rem > 0) {
465                         xlen = strnlen(xattr_name, rem - 1) + 1;
466                         rem -= xlen;
467                         if (xattr_type_filter(sbi,
468                                         get_xattr_type(xattr_name)) == 0) {
469                                 /* skip OK xattr type
470                                  * leave it in buffer
471                                  */
472                                 xattr_name += xlen;
473                                 continue;
474                         }
475                         /* move up remaining xattrs in buffer
476                          * removing the xattr that is not OK
477                          */
478                         memmove(xattr_name, xattr_name + xlen, rem);
479                         rc -= xlen;
480                 }
481         }
482         if (S_ISREG(inode->i_mode)) {
483                 if (!ll_i2info(inode)->lli_has_smd)
484                         rc2 = -1;
485         } else if (S_ISDIR(inode->i_mode)) {
486                 rc2 = ll_dir_getstripe(inode, (void **)&lmm, &lmmsize,
487                                        &request, 0);
488         }
489
490         if (rc2 < 0) {
491                 rc2 = 0;
492                 goto out;
493         } else if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)) {
494                 const int prefix_len = sizeof(XATTR_LUSTRE_PREFIX) - 1;
495                 const size_t name_len   = sizeof("lov") - 1;
496                 const size_t total_len  = prefix_len + name_len + 1;
497
498                 if (((rc + total_len) > size) && buffer) {
499                         ptlrpc_req_finished(request);
500                         return -ERANGE;
501                 }
502
503                 if (buffer) {
504                         buffer += rc;
505                         memcpy(buffer, XATTR_LUSTRE_PREFIX, prefix_len);
506                         memcpy(buffer + prefix_len, "lov", name_len);
507                         buffer[prefix_len + name_len] = '\0';
508                 }
509                 rc2 = total_len;
510         }
511 out:
512         ptlrpc_req_finished(request);
513         rc = rc + rc2;
514
515         return rc;
516 }
517
518 static const struct xattr_handler ll_user_xattr_handler = {
519         .prefix = XATTR_USER_PREFIX,
520         .flags = XATTR_USER_T,
521         .get = ll_xattr_get_common,
522         .set = ll_xattr_set_common,
523 };
524
525 static const struct xattr_handler ll_trusted_xattr_handler = {
526         .prefix = XATTR_TRUSTED_PREFIX,
527         .flags = XATTR_TRUSTED_T,
528         .get = ll_xattr_get,
529         .set = ll_xattr_set,
530 };
531
532 static const struct xattr_handler ll_security_xattr_handler = {
533         .prefix = XATTR_SECURITY_PREFIX,
534         .flags = XATTR_SECURITY_T,
535         .get = ll_xattr_get_common,
536         .set = ll_xattr_set_common,
537 };
538
539 static const struct xattr_handler ll_acl_access_xattr_handler = {
540         .prefix = XATTR_NAME_POSIX_ACL_ACCESS,
541         .flags = XATTR_ACL_ACCESS_T,
542         .get = ll_xattr_get_common,
543         .set = ll_xattr_set_common,
544 };
545
546 static const struct xattr_handler ll_acl_default_xattr_handler = {
547         .prefix = XATTR_NAME_POSIX_ACL_DEFAULT,
548         .flags = XATTR_ACL_DEFAULT_T,
549         .get = ll_xattr_get_common,
550         .set = ll_xattr_set_common,
551 };
552
553 static const struct xattr_handler ll_lustre_xattr_handler = {
554         .prefix = XATTR_LUSTRE_PREFIX,
555         .flags = XATTR_LUSTRE_T,
556         .get = ll_xattr_get,
557         .set = ll_xattr_set,
558 };
559
560 const struct xattr_handler *ll_xattr_handlers[] = {
561         &ll_user_xattr_handler,
562         &ll_trusted_xattr_handler,
563         &ll_security_xattr_handler,
564 #ifdef CONFIG_FS_POSIX_ACL
565         &ll_acl_access_xattr_handler,
566         &ll_acl_default_xattr_handler,
567 #endif
568         &ll_lustre_xattr_handler,
569         NULL,
570 };