Whamcloud - gitweb
LU-1330 obdclass: splits server-side object stack from client
[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, 2012, 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 #ifdef HAVE_SELINUX_IS_ENABLED
41 #include <linux/selinux.h>
42 #endif
43
44 #define DEBUG_SUBSYSTEM S_LLITE
45
46 #include <obd_support.h>
47 #include <lustre_lite.h>
48 #include <lustre_dlm.h>
49 #include <lustre_ver.h>
50 #include <lustre_eacl.h>
51
52 #include "llite_internal.h"
53
54 #define XATTR_USER_T            (1)
55 #define XATTR_TRUSTED_T         (2)
56 #define XATTR_SECURITY_T        (3)
57 #define XATTR_ACL_ACCESS_T      (4)
58 #define XATTR_ACL_DEFAULT_T     (5)
59 #define XATTR_LUSTRE_T          (6)
60 #define XATTR_OTHER_T           (7)
61
62 static
63 int get_xattr_type(const char *name)
64 {
65         if (!strcmp(name, POSIX_ACL_XATTR_ACCESS))
66                 return XATTR_ACL_ACCESS_T;
67
68         if (!strcmp(name, POSIX_ACL_XATTR_DEFAULT))
69                 return XATTR_ACL_DEFAULT_T;
70
71         if (!strncmp(name, XATTR_USER_PREFIX,
72                      sizeof(XATTR_USER_PREFIX) - 1))
73                 return XATTR_USER_T;
74
75         if (!strncmp(name, XATTR_TRUSTED_PREFIX,
76                      sizeof(XATTR_TRUSTED_PREFIX) - 1))
77                 return XATTR_TRUSTED_T;
78
79         if (!strncmp(name, XATTR_SECURITY_PREFIX,
80                      sizeof(XATTR_SECURITY_PREFIX) - 1))
81                 return XATTR_SECURITY_T;
82
83         if (!strncmp(name, XATTR_LUSTRE_PREFIX,
84                      sizeof(XATTR_LUSTRE_PREFIX) - 1))
85                 return XATTR_LUSTRE_T;
86
87         return XATTR_OTHER_T;
88 }
89
90 static
91 int xattr_type_filter(struct ll_sb_info *sbi, int xattr_type)
92 {
93         if ((xattr_type == XATTR_ACL_ACCESS_T ||
94              xattr_type == XATTR_ACL_DEFAULT_T) &&
95            !(sbi->ll_flags & LL_SBI_ACL))
96                 return -EOPNOTSUPP;
97
98         if (xattr_type == XATTR_USER_T && !(sbi->ll_flags & LL_SBI_USER_XATTR))
99                 return -EOPNOTSUPP;
100         if (xattr_type == XATTR_TRUSTED_T && !cfs_capable(CFS_CAP_SYS_ADMIN))
101                 return -EPERM;
102         if (xattr_type == XATTR_OTHER_T)
103                 return -EOPNOTSUPP;
104
105         return 0;
106 }
107
108 static
109 int ll_setxattr_common(struct inode *inode, const char *name,
110                        const void *value, size_t size,
111                        int flags, __u64 valid)
112 {
113         struct ll_sb_info *sbi = ll_i2sbi(inode);
114         struct ptlrpc_request *req;
115         int xattr_type, rc;
116         struct obd_capa *oc;
117         posix_acl_xattr_header *new_value = NULL;
118         struct rmtacl_ctl_entry *rce = NULL;
119         ext_acl_xattr_header *acl = NULL;
120         const char *pv = value;
121         ENTRY;
122
123         xattr_type = get_xattr_type(name);
124         rc = xattr_type_filter(sbi, xattr_type);
125         if (rc)
126                 RETURN(rc);
127
128         /* b10667: ignore lustre special xattr for now */
129         if ((xattr_type == XATTR_TRUSTED_T && strcmp(name, "trusted.lov") == 0) ||
130             (xattr_type == XATTR_LUSTRE_T && strcmp(name, "lustre.lov") == 0))
131                 RETURN(0);
132
133         /* b15587: ignore security.capability xattr for now */
134         if ((xattr_type == XATTR_SECURITY_T &&
135             strcmp(name, "security.capability") == 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 #ifdef CONFIG_FS_POSIX_ACL
144         if (sbi->ll_flags & LL_SBI_RMT_CLIENT &&
145             (xattr_type == XATTR_ACL_ACCESS_T ||
146             xattr_type == XATTR_ACL_DEFAULT_T)) {
147                 rce = rct_search(&sbi->ll_rct, cfs_curproc_pid());
148                 if (rce == NULL ||
149                     (rce->rce_ops != RMT_LSETFACL &&
150                     rce->rce_ops != RMT_RSETFACL))
151                         RETURN(-EOPNOTSUPP);
152
153                 if (rce->rce_ops == RMT_LSETFACL) {
154                         struct eacl_entry *ee;
155
156                         ee = et_search_del(&sbi->ll_et, cfs_curproc_pid(),
157                                            ll_inode2fid(inode), xattr_type);
158                         LASSERT(ee != NULL);
159                         if (valid & OBD_MD_FLXATTR) {
160                                 acl = lustre_acl_xattr_merge2ext(
161                                                 (posix_acl_xattr_header *)value,
162                                                 size, ee->ee_acl);
163                                 if (IS_ERR(acl)) {
164                                         ee_free(ee);
165                                         RETURN(PTR_ERR(acl));
166                                 }
167                                 size =  CFS_ACL_XATTR_SIZE(\
168                                                 le32_to_cpu(acl->a_count), \
169                                                 ext_acl_xattr);
170                                 pv = (const char *)acl;
171                         }
172                         ee_free(ee);
173                 } else if (rce->rce_ops == RMT_RSETFACL) {
174                         size = lustre_posix_acl_xattr_filter(
175                                                 (posix_acl_xattr_header *)value,
176                                                 size, &new_value);
177                         if (unlikely(size < 0))
178                                 RETURN(size);
179
180                         pv = (const char *)new_value;
181                 } else
182                         RETURN(-EOPNOTSUPP);
183
184                 valid |= rce_ops2valid(rce->rce_ops);
185         }
186 #endif
187         oc = ll_mdscapa_get(inode);
188         rc = md_setxattr(sbi->ll_md_exp, ll_inode2fid(inode), oc,
189                          valid, name, pv, size, 0, flags, ll_i2suppgid(inode),
190                          &req);
191         capa_put(oc);
192 #ifdef CONFIG_FS_POSIX_ACL
193         if (new_value != NULL)
194                 lustre_posix_acl_xattr_free(new_value, size);
195         if (acl != NULL)
196                 lustre_ext_acl_xattr_free(acl);
197 #endif
198         if (rc) {
199                 if (rc == -EOPNOTSUPP && xattr_type == XATTR_USER_T) {
200                         LCONSOLE_INFO("Disabling user_xattr feature because "
201                                       "it is not supported on the server\n");
202                         sbi->ll_flags &= ~LL_SBI_USER_XATTR;
203                 }
204                 RETURN(rc);
205         }
206
207         ptlrpc_req_finished(req);
208         RETURN(0);
209 }
210
211 int ll_setxattr(struct dentry *dentry, const char *name,
212                 const void *value, size_t size, int flags)
213 {
214         struct inode *inode = dentry->d_inode;
215
216         LASSERT(inode);
217         LASSERT(name);
218
219         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), xattr %s\n",
220                inode->i_ino, inode->i_generation, inode, name);
221
222         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_SETXATTR, 1);
223
224         if ((strncmp(name, XATTR_TRUSTED_PREFIX,
225                      sizeof(XATTR_TRUSTED_PREFIX) - 1) == 0 &&
226              strcmp(name + sizeof(XATTR_TRUSTED_PREFIX) - 1, "lov") == 0) ||
227             (strncmp(name, XATTR_LUSTRE_PREFIX,
228                      sizeof(XATTR_LUSTRE_PREFIX) - 1) == 0 &&
229              strcmp(name + sizeof(XATTR_LUSTRE_PREFIX) - 1, "lov") == 0)) {
230                 struct lov_user_md *lump = (struct lov_user_md *)value;
231                 int rc = 0;
232
233                 /* Attributes that are saved via getxattr will always have
234                  * the stripe_offset as 0.  Instead, the MDS should be
235                  * allowed to pick the starting OST index.   b=17846 */
236                 if (lump != NULL && lump->lmm_stripe_offset == 0)
237                         lump->lmm_stripe_offset = -1;
238
239                 if (lump != NULL && S_ISREG(inode->i_mode)) {
240                         struct file f;
241                         int flags = FMODE_WRITE;
242                         int lum_size = (lump->lmm_magic == LOV_USER_MAGIC_V1) ?
243                                 sizeof(*lump) : sizeof(struct lov_user_md_v3);
244
245                         f.f_dentry = dentry;
246                         rc = ll_lov_setstripe_ea_info(inode, &f, flags, lump,
247                                                       lum_size);
248                         /* b10667: rc always be 0 here for now */
249                         rc = 0;
250                 } else if (S_ISDIR(inode->i_mode)) {
251                         rc = ll_dir_setstripe(inode, lump, 0);
252                 }
253
254                 return rc;
255
256         } else if (strcmp(name, XATTR_NAME_LMA) == 0 ||
257                    strcmp(name, XATTR_NAME_LINK) == 0)
258                 return 0;
259
260         return ll_setxattr_common(inode, name, value, size, flags,
261                                   OBD_MD_FLXATTR);
262 }
263
264 int ll_removexattr(struct dentry *dentry, const char *name)
265 {
266         struct inode *inode = dentry->d_inode;
267
268         LASSERT(inode);
269         LASSERT(name);
270
271         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), xattr %s\n",
272                inode->i_ino, inode->i_generation, inode, name);
273
274         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_REMOVEXATTR, 1);
275         return ll_setxattr_common(inode, name, NULL, 0, 0,
276                                   OBD_MD_FLXATTRRM);
277 }
278
279 static
280 int ll_getxattr_common(struct inode *inode, const char *name,
281                        void *buffer, size_t size, __u64 valid)
282 {
283         struct ll_sb_info *sbi = ll_i2sbi(inode);
284         struct ptlrpc_request *req = NULL;
285         struct mdt_body *body;
286         int xattr_type, rc;
287         void *xdata;
288         struct obd_capa *oc;
289         struct rmtacl_ctl_entry *rce = NULL;
290         ENTRY;
291
292         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n",
293                inode->i_ino, inode->i_generation, inode);
294
295         /* listxattr have slightly different behavior from of ext3:
296          * without 'user_xattr' ext3 will list all xattr names but
297          * filtered out "^user..*"; we list them all for simplicity.
298          */
299         if (!name) {
300                 xattr_type = XATTR_OTHER_T;
301                 goto do_getxattr;
302         }
303
304         xattr_type = get_xattr_type(name);
305         rc = xattr_type_filter(sbi, xattr_type);
306         if (rc)
307                 RETURN(rc);
308
309         /* b15587: ignore security.capability xattr for now */
310         if ((xattr_type == XATTR_SECURITY_T &&
311             strcmp(name, "security.capability") == 0))
312                 RETURN(-ENODATA);
313
314         /* LU-549:  Disable security.selinux when selinux is disabled */
315         if (xattr_type == XATTR_SECURITY_T && !selinux_is_enabled() &&
316             strcmp(name, "security.selinux") == 0)
317                 RETURN(-EOPNOTSUPP);
318
319 #ifdef CONFIG_FS_POSIX_ACL
320         if (sbi->ll_flags & LL_SBI_RMT_CLIENT &&
321             (xattr_type == XATTR_ACL_ACCESS_T ||
322             xattr_type == XATTR_ACL_DEFAULT_T)) {
323                 rce = rct_search(&sbi->ll_rct, cfs_curproc_pid());
324                 if (rce == NULL ||
325                     (rce->rce_ops != RMT_LSETFACL &&
326                     rce->rce_ops != RMT_LGETFACL &&
327                     rce->rce_ops != RMT_RSETFACL &&
328                     rce->rce_ops != RMT_RGETFACL))
329                         RETURN(-EOPNOTSUPP);
330         }
331
332         /* posix acl is under protection of LOOKUP lock. when calling to this,
333          * we just have path resolution to the target inode, so we have great
334          * chance that cached ACL is uptodate.
335          */
336         if (xattr_type == XATTR_ACL_ACCESS_T &&
337             !(sbi->ll_flags & LL_SBI_RMT_CLIENT)) {
338                 struct ll_inode_info *lli = ll_i2info(inode);
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(acl, buffer, size);
349                 posix_acl_release(acl);
350                 RETURN(rc);
351         }
352         if (xattr_type == XATTR_ACL_DEFAULT_T && !S_ISDIR(inode->i_mode))
353                 RETURN(-ENODATA);
354 #endif
355
356 do_getxattr:
357         oc = ll_mdscapa_get(inode);
358         rc = md_getxattr(sbi->ll_md_exp, ll_inode2fid(inode), oc,
359                          valid | (rce ? rce_ops2valid(rce->rce_ops) : 0),
360                          name, NULL, 0, size, 0, &req);
361         capa_put(oc);
362         if (rc) {
363                 if (rc == -EOPNOTSUPP && xattr_type == XATTR_USER_T) {
364                         LCONSOLE_INFO("Disabling user_xattr feature because "
365                                       "it is not supported on the server\n");
366                         sbi->ll_flags &= ~LL_SBI_USER_XATTR;
367                 }
368                 RETURN(rc);
369         }
370
371         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
372         LASSERT(body);
373
374         /* only detect the xattr size */
375         if (size == 0)
376                 GOTO(out, rc = body->eadatasize);
377
378         if (size < body->eadatasize) {
379                 CERROR("server bug: replied size %u > %u\n",
380                        body->eadatasize, (int)size);
381                 GOTO(out, rc = -ERANGE);
382         }
383
384         if (body->eadatasize == 0)
385                 GOTO(out, rc = -ENODATA);
386
387         /* do not need swab xattr data */
388         xdata = req_capsule_server_sized_get(&req->rq_pill, &RMF_EADATA,
389                                              body->eadatasize);
390         if (!xdata)
391                 GOTO(out, rc = -EFAULT);
392
393 #ifdef CONFIG_FS_POSIX_ACL
394         if (body->eadatasize >= 0 && rce && rce->rce_ops == RMT_LSETFACL) {
395                 ext_acl_xattr_header *acl;
396
397                 acl = lustre_posix_acl_xattr_2ext((posix_acl_xattr_header *)xdata,
398                                                   body->eadatasize);
399                 if (IS_ERR(acl))
400                         GOTO(out, rc = PTR_ERR(acl));
401
402                 rc = ee_add(&sbi->ll_et, cfs_curproc_pid(), ll_inode2fid(inode),
403                             xattr_type, acl);
404                 if (unlikely(rc < 0)) {
405                         lustre_ext_acl_xattr_free(acl);
406                         GOTO(out, rc);
407                 }
408         }
409 #endif
410
411         if (body->eadatasize == 0) {
412                 rc = -ENODATA;
413         } else {
414                 LASSERT(buffer);
415                 memcpy(buffer, xdata, body->eadatasize);
416                 rc = body->eadatasize;
417         }
418         EXIT;
419 out:
420         ptlrpc_req_finished(req);
421         return rc;
422 }
423
424 ssize_t ll_getxattr(struct dentry *dentry, const char *name,
425                     void *buffer, size_t size)
426 {
427         struct inode *inode = dentry->d_inode;
428
429         LASSERT(inode);
430         LASSERT(name);
431
432         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), xattr %s\n",
433                inode->i_ino, inode->i_generation, inode, name);
434
435         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_GETXATTR, 1);
436
437         if ((strncmp(name, XATTR_TRUSTED_PREFIX,
438                      sizeof(XATTR_TRUSTED_PREFIX) - 1) == 0 &&
439              strcmp(name + sizeof(XATTR_TRUSTED_PREFIX) - 1, "lov") == 0) ||
440             (strncmp(name, XATTR_LUSTRE_PREFIX,
441                      sizeof(XATTR_LUSTRE_PREFIX) - 1) == 0 &&
442              strcmp(name + sizeof(XATTR_LUSTRE_PREFIX) - 1, "lov") == 0)) {
443                 struct lov_stripe_md *lsm;
444                 struct lov_user_md *lump;
445                 struct lov_mds_md *lmm = NULL;
446                 struct ptlrpc_request *request = NULL;
447                 int rc = 0, lmmsize = 0;
448
449                 if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
450                         return -ENODATA;
451
452                 if (size == 0 && S_ISDIR(inode->i_mode)) {
453                         /* XXX directory EA is fix for now, optimize to save
454                          * RPC transfer */
455                         GOTO(out, rc = sizeof(struct lov_user_md));
456                 }
457
458                 lsm = ccc_inode_lsm_get(inode);
459                 if (lsm == NULL) {
460                         if (S_ISDIR(inode->i_mode)) {
461                                 rc = ll_dir_getstripe(inode, &lmm,
462                                                       &lmmsize, &request);
463                         } else {
464                                 rc = -ENODATA;
465                         }
466                 } else {
467                         /* LSM is present already after lookup/getattr call.
468                          * we need to grab layout lock once it is implemented */
469                         rc = obd_packmd(ll_i2dtexp(inode), &lmm, lsm);
470                         lmmsize = rc;
471                 }
472                 ccc_inode_lsm_put(inode, lsm);
473
474                 if (rc < 0)
475                        GOTO(out, rc);
476
477                 if (size == 0) {
478                         /* used to call ll_get_max_mdsize() forward to get
479                          * the maximum buffer size, while some apps (such as
480                          * rsync 3.0.x) care much about the exact xattr value
481                          * size */
482                         rc = lmmsize;
483                         GOTO(out, rc);
484                 }
485
486                 if (size < lmmsize) {
487                         CERROR("server bug: replied size %d > %d for %s (%s)\n",
488                                lmmsize, (int)size, dentry->d_name.name, name);
489                         GOTO(out, rc = -ERANGE);
490                 }
491
492                 lump = (struct lov_user_md *)buffer;
493                 memcpy(lump, lmm, lmmsize);
494                 /* do not return layout gen for getxattr otherwise it would
495                  * confuse tar --xattr by recognizing layout gen as stripe
496                  * offset when the file is restored. See LU-2809. */
497                 lump->lmm_layout_gen = 0;
498
499                 rc = lmmsize;
500 out:
501                 if (request)
502                         ptlrpc_req_finished(request);
503                 else if (lmm)
504                         obd_free_diskmd(ll_i2dtexp(inode), &lmm);
505                 return(rc);
506         }
507
508         return ll_getxattr_common(inode, name, buffer, size, OBD_MD_FLXATTR);
509 }
510
511 ssize_t ll_listxattr(struct dentry *dentry, char *buffer, size_t size)
512 {
513         struct inode *inode = dentry->d_inode;
514         int rc = 0, rc2 = 0;
515         struct lov_mds_md *lmm = NULL;
516         struct ptlrpc_request *request = NULL;
517         int lmmsize;
518
519         LASSERT(inode);
520
521         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n",
522                inode->i_ino, inode->i_generation, inode);
523
524         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_LISTXATTR, 1);
525
526         rc = ll_getxattr_common(inode, NULL, buffer, size, OBD_MD_FLXATTRLS);
527         if (rc < 0)
528                 GOTO(out, rc);
529
530         if (buffer != NULL) {
531                 struct ll_sb_info *sbi = ll_i2sbi(inode);
532                 char *xattr_name = buffer;
533                 int xlen, rem = rc;
534
535                 while (rem > 0) {
536                         xlen = strnlen(xattr_name, rem - 1) + 1;
537                         rem -= xlen;
538                         if (xattr_type_filter(sbi,
539                                         get_xattr_type(xattr_name)) == 0) {
540                                 /* skip OK xattr type
541                                  * leave it in buffer
542                                  */
543                                 xattr_name += xlen;
544                                 continue;
545                         }
546                         /* move up remaining xattrs in buffer
547                          * removing the xattr that is not OK
548                          */
549                         memmove(xattr_name, xattr_name + xlen, rem);
550                         rc -= xlen;
551                 }
552         }
553         if (S_ISREG(inode->i_mode)) {
554                 if (!ll_i2info(inode)->lli_has_smd)
555                         rc2 = -1;
556         } else if (S_ISDIR(inode->i_mode)) {
557                 rc2 = ll_dir_getstripe(inode, &lmm, &lmmsize, &request);
558         }
559
560         if (rc2 < 0) {
561                 GOTO(out, rc2 = 0);
562         } else if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)) {
563                 const int prefix_len = sizeof(XATTR_LUSTRE_PREFIX) - 1;
564                 const size_t name_len   = sizeof("lov") - 1;
565                 const size_t total_len  = prefix_len + name_len + 1;
566
567                 if (buffer && (rc + total_len) <= size) {
568                         buffer += rc;
569                         memcpy(buffer, XATTR_LUSTRE_PREFIX, prefix_len);
570                         memcpy(buffer + prefix_len, "lov", name_len);
571                         buffer[prefix_len + name_len] = '\0';
572                 }
573                 rc2 = total_len;
574         }
575 out:
576         ptlrpc_req_finished(request);
577         rc = rc + rc2;
578
579         return rc;
580 }