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