Whamcloud - gitweb
35789c6ca130bc70dc6e03a472077716deafb2f1
[fs/lustre-release.git] / lustre / llite / xattr.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2014, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #include <linux/fs.h>
38 #include <linux/sched.h>
39 #include <linux/mm.h>
40 #include <linux/selinux.h>
41
42 #define DEBUG_SUBSYSTEM S_LLITE
43
44 #include <obd_support.h>
45 #include <lustre_dlm.h>
46 #include <lustre_ver.h>
47 #include <lustre_eacl.h>
48
49 #include "llite_internal.h"
50
51 #define XATTR_USER_T            (1)
52 #define XATTR_TRUSTED_T         (2)
53 #define XATTR_SECURITY_T        (3)
54 #define XATTR_ACL_ACCESS_T      (4)
55 #define XATTR_ACL_DEFAULT_T     (5)
56 #define XATTR_LUSTRE_T          (6)
57 #define XATTR_OTHER_T           (7)
58
59 static
60 int get_xattr_type(const char *name)
61 {
62         if (!strcmp(name, POSIX_ACL_XATTR_ACCESS))
63                 return XATTR_ACL_ACCESS_T;
64
65         if (!strcmp(name, POSIX_ACL_XATTR_DEFAULT))
66                 return XATTR_ACL_DEFAULT_T;
67
68         if (!strncmp(name, XATTR_USER_PREFIX,
69                      sizeof(XATTR_USER_PREFIX) - 1))
70                 return XATTR_USER_T;
71
72         if (!strncmp(name, XATTR_TRUSTED_PREFIX,
73                      sizeof(XATTR_TRUSTED_PREFIX) - 1))
74                 return XATTR_TRUSTED_T;
75
76         if (!strncmp(name, XATTR_SECURITY_PREFIX,
77                      sizeof(XATTR_SECURITY_PREFIX) - 1))
78                 return XATTR_SECURITY_T;
79
80         if (!strncmp(name, XATTR_LUSTRE_PREFIX,
81                      sizeof(XATTR_LUSTRE_PREFIX) - 1))
82                 return XATTR_LUSTRE_T;
83
84         return XATTR_OTHER_T;
85 }
86
87 static
88 int xattr_type_filter(struct ll_sb_info *sbi, int xattr_type)
89 {
90         if ((xattr_type == XATTR_ACL_ACCESS_T ||
91              xattr_type == XATTR_ACL_DEFAULT_T) &&
92            !(sbi->ll_flags & LL_SBI_ACL))
93                 return -EOPNOTSUPP;
94
95         if (xattr_type == XATTR_USER_T && !(sbi->ll_flags & LL_SBI_USER_XATTR))
96                 return -EOPNOTSUPP;
97         if (xattr_type == XATTR_TRUSTED_T && !cfs_capable(CFS_CAP_SYS_ADMIN))
98                 return -EPERM;
99         if (xattr_type == XATTR_OTHER_T)
100                 return -EOPNOTSUPP;
101
102         return 0;
103 }
104
105 static
106 int ll_setxattr_common(struct inode *inode, const char *name,
107                        const void *value, size_t size,
108                        int flags, __u64 valid)
109 {
110         struct ll_sb_info *sbi = ll_i2sbi(inode);
111         struct ptlrpc_request *req = NULL;
112         int xattr_type, rc;
113         posix_acl_xattr_header *new_value = NULL;
114         struct rmtacl_ctl_entry *rce = NULL;
115         ext_acl_xattr_header *acl = NULL;
116         const char *pv = value;
117         ENTRY;
118
119         xattr_type = get_xattr_type(name);
120         rc = xattr_type_filter(sbi, xattr_type);
121         if (rc)
122                 RETURN(rc);
123
124         if ((xattr_type == XATTR_ACL_ACCESS_T ||
125              xattr_type == XATTR_ACL_DEFAULT_T) &&
126 #ifdef HAVE_INODE_OWNER_OR_CAPABLE
127             !inode_owner_or_capable(inode))
128 #else
129             !is_owner_or_cap(inode))
130 #endif
131                 return -EPERM;
132
133         /* b10667: ignore lustre special xattr for now */
134         if ((xattr_type == XATTR_TRUSTED_T && strcmp(name, "trusted.lov") == 0) ||
135             (xattr_type == XATTR_LUSTRE_T && strcmp(name, "lustre.lov") == 0))
136                 RETURN(0);
137
138         /* b15587: ignore security.capability xattr for now */
139         if ((xattr_type == XATTR_SECURITY_T &&
140             strcmp(name, "security.capability") == 0))
141                 RETURN(0);
142
143         /* LU-549:  Disable security.selinux when selinux is disabled */
144         if (xattr_type == XATTR_SECURITY_T && !selinux_is_enabled() &&
145             strcmp(name, "security.selinux") == 0)
146                 RETURN(-EOPNOTSUPP);
147
148 #ifdef CONFIG_FS_POSIX_ACL
149         if (sbi->ll_flags & LL_SBI_RMT_CLIENT &&
150             (xattr_type == XATTR_ACL_ACCESS_T ||
151             xattr_type == XATTR_ACL_DEFAULT_T)) {
152                 rce = rct_search(&sbi->ll_rct, current_pid());
153                 if (rce == NULL ||
154                     (rce->rce_ops != RMT_LSETFACL &&
155                     rce->rce_ops != RMT_RSETFACL))
156                         RETURN(-EOPNOTSUPP);
157
158                 if (rce->rce_ops == RMT_LSETFACL) {
159                         struct eacl_entry *ee;
160
161                         ee = et_search_del(&sbi->ll_et, current_pid(),
162                                            ll_inode2fid(inode), xattr_type);
163                         LASSERT(ee != NULL);
164                         if (valid & OBD_MD_FLXATTR) {
165                                 acl = lustre_acl_xattr_merge2ext(
166                                                 (posix_acl_xattr_header *)value,
167                                                 size, ee->ee_acl);
168                                 if (IS_ERR(acl)) {
169                                         ee_free(ee);
170                                         RETURN(PTR_ERR(acl));
171                                 }
172                                 size =  CFS_ACL_XATTR_SIZE(\
173                                                 le32_to_cpu(acl->a_count), \
174                                                 ext_acl_xattr);
175                                 pv = (const char *)acl;
176                         }
177                         ee_free(ee);
178                 } else if (rce->rce_ops == RMT_RSETFACL) {
179                         int acl_size = lustre_posix_acl_xattr_filter(
180                                                 (posix_acl_xattr_header *)value,
181                                                 size, &new_value);
182                         if (unlikely(acl_size < 0))
183                                 RETURN(acl_size);
184                         size = acl_size;
185
186                         pv = (const char *)new_value;
187                 } else
188                         RETURN(-EOPNOTSUPP);
189
190                 valid |= rce_ops2valid(rce->rce_ops);
191         }
192 #endif
193         rc = md_setxattr(sbi->ll_md_exp, ll_inode2fid(inode), valid, name, pv,
194                          size, 0, flags, ll_i2suppgid(inode), &req);
195
196 #ifdef CONFIG_FS_POSIX_ACL
197         if (new_value != NULL)
198                 lustre_posix_acl_xattr_free(new_value, size);
199         if (acl != NULL)
200                 lustre_ext_acl_xattr_free(acl);
201 #endif
202         if (rc) {
203                 if (rc == -EOPNOTSUPP && xattr_type == XATTR_USER_T) {
204                         LCONSOLE_INFO("Disabling user_xattr feature because "
205                                       "it is not supported on the server\n");
206                         sbi->ll_flags &= ~LL_SBI_USER_XATTR;
207                 }
208                 RETURN(rc);
209         }
210
211         ptlrpc_req_finished(req);
212         RETURN(0);
213 }
214
215 int ll_setxattr(struct dentry *dentry, const char *name,
216                 const void *value, size_t size, int flags)
217 {
218         struct inode *inode = dentry->d_inode;
219
220         LASSERT(inode);
221         LASSERT(name);
222
223         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), xattr %s\n",
224                PFID(ll_inode2fid(inode)), inode, name);
225
226         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_SETXATTR, 1);
227
228         if ((strncmp(name, XATTR_TRUSTED_PREFIX,
229                      sizeof(XATTR_TRUSTED_PREFIX) - 1) == 0 &&
230              strcmp(name + sizeof(XATTR_TRUSTED_PREFIX) - 1, "lov") == 0) ||
231             (strncmp(name, XATTR_LUSTRE_PREFIX,
232                      sizeof(XATTR_LUSTRE_PREFIX) - 1) == 0 &&
233              strcmp(name + sizeof(XATTR_LUSTRE_PREFIX) - 1, "lov") == 0)) {
234                 struct lov_user_md *lump = (struct lov_user_md *)value;
235                 int                 rc = 0;
236
237                 /* Attributes that are saved via getxattr will always have
238                  * the stripe_offset as 0.  Instead, the MDS should be
239                  * allowed to pick the starting OST index.   b=17846 */
240                 if (lump != NULL && lump->lmm_stripe_offset == 0)
241                         lump->lmm_stripe_offset = -1;
242
243                 if (lump != NULL && S_ISREG(inode->i_mode)) {
244                         struct file     f;
245                         __u64           it_flags = FMODE_WRITE;
246                         int             lum_size;
247
248                         lum_size = ll_lov_user_md_size(lump);
249                         if (lum_size < 0 || size < lum_size)
250                                 return 0; /* b=10667: ignore error */
251
252                         memset(&f, 0, sizeof(f)); /* f.f_flags is used below */
253                         f.f_path.dentry = dentry;
254                         rc = ll_lov_setstripe_ea_info(inode, &f, it_flags, lump,
255                                                       lum_size);
256                         /* b=10667: rc always be 0 here for now */
257                         rc = 0;
258                 } else if (S_ISDIR(inode->i_mode)) {
259                         rc = ll_dir_setstripe(inode, lump, 0);
260                 }
261
262                 return rc;
263
264         } else if (strcmp(name, XATTR_NAME_LMA) == 0 ||
265                    strcmp(name, XATTR_NAME_LINK) == 0)
266                 return 0;
267
268         return ll_setxattr_common(inode, name, value, size, flags,
269                                   OBD_MD_FLXATTR);
270 }
271
272 int ll_removexattr(struct dentry *dentry, const char *name)
273 {
274         struct inode *inode = dentry->d_inode;
275
276         LASSERT(inode);
277         LASSERT(name);
278
279         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), xattr %s\n",
280                PFID(ll_inode2fid(inode)), inode, name);
281
282         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_REMOVEXATTR, 1);
283         return ll_setxattr_common(inode, name, NULL, 0, 0,
284                                   OBD_MD_FLXATTRRM);
285 }
286
287 int ll_getxattr_common(struct inode *inode, const char *name,
288                        void *buffer, size_t size, __u64 valid)
289 {
290         struct ll_sb_info *sbi = ll_i2sbi(inode);
291         struct ptlrpc_request *req = NULL;
292         struct mdt_body *body;
293         int xattr_type, rc;
294         void *xdata;
295         struct rmtacl_ctl_entry *rce = NULL;
296         struct ll_inode_info *lli = ll_i2info(inode);
297         ENTRY;
298
299         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p)\n",
300                PFID(ll_inode2fid(inode)), inode);
301
302         /* listxattr have slightly different behavior from of ext3:
303          * without 'user_xattr' ext3 will list all xattr names but
304          * filtered out "^user..*"; we list them all for simplicity.
305          */
306         if (!name) {
307                 xattr_type = XATTR_OTHER_T;
308                 goto do_getxattr;
309         }
310
311         xattr_type = get_xattr_type(name);
312         rc = xattr_type_filter(sbi, xattr_type);
313         if (rc)
314                 RETURN(rc);
315
316         /* b15587: ignore security.capability xattr for now */
317         if ((xattr_type == XATTR_SECURITY_T &&
318             strcmp(name, "security.capability") == 0))
319                 RETURN(-ENODATA);
320
321         /* LU-549:  Disable security.selinux when selinux is disabled */
322         if (xattr_type == XATTR_SECURITY_T && !selinux_is_enabled() &&
323             strcmp(name, "security.selinux") == 0)
324                 RETURN(-EOPNOTSUPP);
325
326 #ifdef CONFIG_FS_POSIX_ACL
327         if (sbi->ll_flags & LL_SBI_RMT_CLIENT &&
328             (xattr_type == XATTR_ACL_ACCESS_T ||
329             xattr_type == XATTR_ACL_DEFAULT_T)) {
330                 rce = rct_search(&sbi->ll_rct, current_pid());
331                 if (rce == NULL ||
332                     (rce->rce_ops != RMT_LSETFACL &&
333                     rce->rce_ops != RMT_LGETFACL &&
334                     rce->rce_ops != RMT_RSETFACL &&
335                     rce->rce_ops != RMT_RGETFACL))
336                         RETURN(-EOPNOTSUPP);
337         }
338
339         /* posix acl is under protection of LOOKUP lock. when calling to this,
340          * we just have path resolution to the target inode, so we have great
341          * chance that cached ACL is uptodate.
342          */
343         if (xattr_type == XATTR_ACL_ACCESS_T &&
344             !(sbi->ll_flags & LL_SBI_RMT_CLIENT)) {
345
346                 struct posix_acl *acl;
347
348                 spin_lock(&lli->lli_lock);
349                 acl = posix_acl_dup(lli->lli_posix_acl);
350                 spin_unlock(&lli->lli_lock);
351
352                 if (!acl)
353                         RETURN(-ENODATA);
354
355                 rc = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
356                 posix_acl_release(acl);
357                 RETURN(rc);
358         }
359         if (xattr_type == XATTR_ACL_DEFAULT_T && !S_ISDIR(inode->i_mode))
360                 RETURN(-ENODATA);
361 #endif
362
363 do_getxattr:
364         if (sbi->ll_xattr_cache_enabled && xattr_type != XATTR_ACL_ACCESS_T) {
365                 rc = ll_xattr_cache_get(inode, name, buffer, size, valid);
366                 if (rc == -EAGAIN)
367                         goto getxattr_nocache;
368                 if (rc < 0)
369                         GOTO(out_xattr, rc);
370
371                 /* Add "system.posix_acl_access" to the list */
372                 if (lli->lli_posix_acl != NULL && valid & OBD_MD_FLXATTRLS) {
373                         if (size == 0) {
374                                 rc += sizeof(XATTR_NAME_ACL_ACCESS);
375                         } else if (size - rc >= sizeof(XATTR_NAME_ACL_ACCESS)) {
376                                 memcpy(buffer + rc, XATTR_NAME_ACL_ACCESS,
377                                        sizeof(XATTR_NAME_ACL_ACCESS));
378                                 rc += sizeof(XATTR_NAME_ACL_ACCESS);
379                         } else {
380                                 GOTO(out_xattr, rc = -ERANGE);
381                         }
382                 }
383         } else {
384 getxattr_nocache:
385                 rc = md_getxattr(sbi->ll_md_exp, ll_inode2fid(inode),
386                                 valid | (rce ? rce_ops2valid(rce->rce_ops) : 0),
387                                 name, NULL, 0, size, 0, &req);
388
389                 if (rc < 0)
390                         GOTO(out_xattr, rc);
391
392                 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
393                 LASSERT(body);
394
395                 /* only detect the xattr size */
396                 if (size == 0)
397                         GOTO(out, rc = body->mbo_eadatasize);
398
399                 if (size < body->mbo_eadatasize) {
400                         CERROR("server bug: replied size %u > %u\n",
401                                 body->mbo_eadatasize, (int)size);
402                         GOTO(out, rc = -ERANGE);
403                 }
404
405                 if (body->mbo_eadatasize == 0)
406                         GOTO(out, rc = -ENODATA);
407
408                 /* do not need swab xattr data */
409                 xdata = req_capsule_server_sized_get(&req->rq_pill, &RMF_EADATA,
410                                                         body->mbo_eadatasize);
411                 if (!xdata)
412                         GOTO(out, rc = -EFAULT);
413
414                 memcpy(buffer, xdata, body->mbo_eadatasize);
415                 rc = body->mbo_eadatasize;
416         }
417
418 #ifdef CONFIG_FS_POSIX_ACL
419         if (rce != NULL && rce->rce_ops == RMT_LSETFACL) {
420                 ext_acl_xattr_header *acl;
421
422                 acl = lustre_posix_acl_xattr_2ext(buffer, rc);
423                 if (IS_ERR(acl))
424                         GOTO(out, rc = PTR_ERR(acl));
425
426                 rc = ee_add(&sbi->ll_et, current_pid(), ll_inode2fid(inode),
427                             xattr_type, acl);
428                 if (unlikely(rc < 0)) {
429                         lustre_ext_acl_xattr_free(acl);
430                         GOTO(out, rc);
431                 }
432         }
433 #endif
434         EXIT;
435
436 out_xattr:
437         if (rc == -EOPNOTSUPP && xattr_type == XATTR_USER_T) {
438                 LCONSOLE_INFO("%s: disabling user_xattr feature because "
439                                 "it is not supported on the server: rc = %d\n",
440                                 ll_get_fsname(inode->i_sb, NULL, 0), rc);
441                 sbi->ll_flags &= ~LL_SBI_USER_XATTR;
442         }
443 out:
444         ptlrpc_req_finished(req);
445         return rc;
446 }
447
448 static ssize_t ll_getxattr_lov(struct inode *inode, void *buf, size_t buf_size)
449 {
450         ssize_t rc;
451
452         if (S_ISREG(inode->i_mode)) {
453                 struct cl_object *obj = ll_i2info(inode)->lli_clob;
454                 struct lu_env *env;
455                 struct cl_layout cl = {
456                         .cl_buf.lb_buf = buf,
457                         .cl_buf.lb_len = buf_size,
458                 };
459                 int refcheck;
460
461                 if (obj == NULL)
462                         RETURN(-ENODATA);
463
464                 env = cl_env_get(&refcheck);
465                 if (IS_ERR(env))
466                         RETURN(PTR_ERR(env));
467
468                 rc = cl_object_layout_get(env, obj, &cl);
469                 if (rc < 0)
470                         GOTO(out_env, rc);
471
472                 if (cl.cl_size == 0)
473                         GOTO(out_env, rc = -ENODATA);
474
475                 rc = cl.cl_size;
476
477                 if (buf_size == 0)
478                         GOTO(out_env, rc);
479
480                 LASSERT(buf != NULL && rc <= buf_size);
481
482                 /* Do not return layout gen for getxattr() since
483                  * otherwise it would confuse tar --xattr by
484                  * recognizing layout gen as stripe offset when the
485                  * file is restored. See LU-2809. */
486                 ((struct lov_mds_md *)buf)->lmm_layout_gen = 0;
487 out_env:
488                 cl_env_put(env, &refcheck);
489
490                 RETURN(rc);
491         } else if (S_ISDIR(inode->i_mode)) {
492                 struct lov_mds_md *lmm = NULL;
493                 int lmm_size = 0;
494                 struct ptlrpc_request *req = NULL;
495
496                 rc = ll_dir_getstripe(inode, (void **)&lmm, &lmm_size,
497                                       &req, 0);
498                 if (rc < 0)
499                         GOTO(out_req, rc);
500
501                 if (buf_size == 0)
502                         GOTO(out_req, rc = lmm_size);
503
504                 if (buf_size < lmm_size)
505                         GOTO(out_req, rc = -ERANGE);
506
507                 memcpy(buf, lmm, lmm_size);
508                 GOTO(out_req, rc = lmm_size);
509 out_req:
510                 if (req != NULL)
511                         ptlrpc_req_finished(req);
512
513                 return rc;
514         } else {
515                 RETURN(-ENODATA);
516         }
517 }
518
519 ssize_t ll_getxattr(struct dentry *dentry, const char *name, void *buf,
520                     size_t buf_size)
521 {
522         struct inode *inode = dentry->d_inode;
523
524         LASSERT(inode);
525         LASSERT(name);
526
527         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), xattr %s\n",
528                PFID(ll_inode2fid(inode)), inode, name);
529
530         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_GETXATTR, 1);
531
532         if (strcmp(name, XATTR_LUSTRE_LOV) == 0 ||
533             strcmp(name, XATTR_NAME_LOV) == 0)
534                 return ll_getxattr_lov(inode, buf, buf_size);
535         else
536                 return ll_getxattr_common(inode, name, buf, buf_size,
537                                           OBD_MD_FLXATTR);
538 }
539
540 ssize_t ll_listxattr(struct dentry *dentry, char *buf, size_t buf_size)
541 {
542         struct inode *inode = dentry->d_inode;
543         struct ll_sb_info *sbi = ll_i2sbi(inode);
544         char *xattr_name;
545         ssize_t rc, rc2;
546         size_t len, rem;
547
548         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p)\n",
549                PFID(ll_inode2fid(inode)), inode);
550
551         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_LISTXATTR, 1);
552
553         rc = ll_getxattr_common(inode, NULL, buf, buf_size, OBD_MD_FLXATTRLS);
554         if (rc < 0)
555                 RETURN(rc);
556
557         /* If we're being called to get the size of the xattr list
558          * (buf_size == 0) then just assume that a lustre.lov xattr
559          * exists. */
560         if (buf_size == 0)
561                 RETURN(rc + sizeof(XATTR_LUSTRE_LOV));
562
563         xattr_name = buf;
564         rem = rc;
565
566         while (rem > 0) {
567                 len = strnlen(xattr_name, rem - 1) + 1;
568                 rem -= len;
569                 if (xattr_type_filter(sbi, get_xattr_type(xattr_name)) == 0) {
570                         /* Skip OK xattr type, leave it in buffer. */
571                         xattr_name += len;
572                         continue;
573                 }
574
575                 /* Move up remaining xattrs in buffer removing the
576                  * xattr that is not OK. */
577                 memmove(xattr_name, xattr_name + len, rem);
578                 rc -= len;
579         }
580
581         rc2 = ll_getxattr_lov(inode, NULL, 0);
582         if (rc2 == -ENODATA)
583                 RETURN(rc);
584
585         if (rc2 < 0)
586                 RETURN(rc2);
587
588         if (buf_size < rc + sizeof(XATTR_LUSTRE_LOV))
589                 RETURN(-ERANGE);
590
591         memcpy(buf + rc, XATTR_LUSTRE_LOV, sizeof(XATTR_LUSTRE_LOV));
592
593         RETURN(rc + sizeof(XATTR_LUSTRE_LOV));
594 }