Whamcloud - gitweb
LU-549 llite: Improve statfs performance if selinux is disabled
[fs/lustre-release.git] / lustre / llite / xattr.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  *
32  * Copyright (c) 2011, 2012, Whamcloud, Inc.
33  */
34 /*
35  * This file is part of Lustre, http://www.lustre.org/
36  * Lustre is a trademark of Sun Microsystems, Inc.
37  */
38
39 #include <linux/fs.h>
40 #include <linux/sched.h>
41 #include <linux/mm.h>
42 #include <linux/smp_lock.h>
43 #ifdef HAVE_SELINUX_IS_ENABLED
44 #include <linux/selinux.h>
45 #endif
46
47 #define DEBUG_SUBSYSTEM S_LLITE
48
49 #include <obd_support.h>
50 #include <lustre_lite.h>
51 #include <lustre_dlm.h>
52 #include <lustre_ver.h>
53 #include <lustre_acl.h>
54
55 #include "llite_internal.h"
56
57 #define XATTR_USER_T            (1)
58 #define XATTR_TRUSTED_T         (2)
59 #define XATTR_SECURITY_T        (3)
60 #define XATTR_ACL_ACCESS_T      (4)
61 #define XATTR_ACL_DEFAULT_T     (5)
62 #define XATTR_LUSTRE_T          (6)
63 #define XATTR_OTHER_T           (7)
64
65 static
66 int get_xattr_type(const char *name)
67 {
68         if (!strcmp(name, POSIX_ACL_XATTR_ACCESS))
69                 return XATTR_ACL_ACCESS_T;
70
71         if (!strcmp(name, POSIX_ACL_XATTR_DEFAULT))
72                 return XATTR_ACL_DEFAULT_T;
73
74         if (!strncmp(name, XATTR_USER_PREFIX,
75                      sizeof(XATTR_USER_PREFIX) - 1))
76                 return XATTR_USER_T;
77
78         if (!strncmp(name, XATTR_TRUSTED_PREFIX,
79                      sizeof(XATTR_TRUSTED_PREFIX) - 1))
80                 return XATTR_TRUSTED_T;
81
82         if (!strncmp(name, XATTR_SECURITY_PREFIX,
83                      sizeof(XATTR_SECURITY_PREFIX) - 1))
84                 return XATTR_SECURITY_T;
85
86         if (!strncmp(name, XATTR_LUSTRE_PREFIX,
87                      sizeof(XATTR_LUSTRE_PREFIX) - 1))
88                 return XATTR_LUSTRE_T;
89
90         return XATTR_OTHER_T;
91 }
92
93 static
94 int xattr_type_filter(struct ll_sb_info *sbi, int xattr_type)
95 {
96         if ((xattr_type == XATTR_ACL_ACCESS_T ||
97              xattr_type == XATTR_ACL_DEFAULT_T) &&
98            !(sbi->ll_flags & LL_SBI_ACL))
99                 return -EOPNOTSUPP;
100
101         if (xattr_type == XATTR_SECURITY_T && !selinux_is_enabled())
102                 return -EOPNOTSUPP;
103         if (xattr_type == XATTR_USER_T && !(sbi->ll_flags & LL_SBI_USER_XATTR))
104                 return -EOPNOTSUPP;
105         if (xattr_type == XATTR_TRUSTED_T && !cfs_capable(CFS_CAP_SYS_ADMIN))
106                 return -EPERM;
107         if (xattr_type == XATTR_OTHER_T)
108                 return -EOPNOTSUPP;
109
110         return 0;
111 }
112
113 static
114 int ll_setxattr_common(struct inode *inode, const char *name,
115                        const void *value, size_t size,
116                        int flags, __u64 valid)
117 {
118         struct ll_sb_info *sbi = ll_i2sbi(inode);
119         struct ptlrpc_request *req;
120         int xattr_type, rc;
121         struct obd_capa *oc;
122         posix_acl_xattr_header *new_value = NULL;
123         struct rmtacl_ctl_entry *rce = NULL;
124         ext_acl_xattr_header *acl = NULL;
125         const char *pv = value;
126         ENTRY;
127
128         xattr_type = get_xattr_type(name);
129         rc = xattr_type_filter(sbi, xattr_type);
130         if (rc)
131                 RETURN(rc);
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 #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
243                         f.f_dentry = dentry;
244                         rc = ll_lov_setstripe_ea_info(inode, &f, flags,
245                                                       lump, sizeof(*lump));
246                         /* b10667: rc always be 0 here for now */
247                         rc = 0;
248                 } else if (S_ISDIR(inode->i_mode)) {
249                         rc = ll_dir_setstripe(inode, lump, 0);
250                 }
251
252                 return rc;
253
254         } else if (strcmp(name, XATTR_NAME_LMA) == 0 ||
255                    strcmp(name, XATTR_NAME_LINK) == 0)
256                 return 0;
257
258         return ll_setxattr_common(inode, name, value, size, flags,
259                                   OBD_MD_FLXATTR);
260 }
261
262 int ll_removexattr(struct dentry *dentry, const char *name)
263 {
264         struct inode *inode = dentry->d_inode;
265
266         LASSERT(inode);
267         LASSERT(name);
268
269         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), xattr %s\n",
270                inode->i_ino, inode->i_generation, inode, name);
271
272         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_REMOVEXATTR, 1);
273         return ll_setxattr_common(inode, name, NULL, 0, 0,
274                                   OBD_MD_FLXATTRRM);
275 }
276
277 static
278 int ll_getxattr_common(struct inode *inode, const char *name,
279                        void *buffer, size_t size, __u64 valid)
280 {
281         struct ll_sb_info *sbi = ll_i2sbi(inode);
282         struct ptlrpc_request *req = NULL;
283         struct mdt_body *body;
284         int xattr_type, rc;
285         void *xdata;
286         struct obd_capa *oc;
287         struct rmtacl_ctl_entry *rce = NULL;
288         ENTRY;
289
290         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n",
291                inode->i_ino, inode->i_generation, inode);
292
293         /* listxattr have slightly different behavior from of ext3:
294          * without 'user_xattr' ext3 will list all xattr names but
295          * filtered out "^user..*"; we list them all for simplicity.
296          */
297         if (!name) {
298                 xattr_type = XATTR_OTHER_T;
299                 goto do_getxattr;
300         }
301
302         xattr_type = get_xattr_type(name);
303         rc = xattr_type_filter(sbi, xattr_type);
304         if (rc)
305                 RETURN(rc);
306
307         /* b15587: ignore security.capability xattr for now */
308         if ((xattr_type == XATTR_SECURITY_T &&
309             strcmp(name, "security.capability") == 0))
310                 RETURN(-ENODATA);
311
312 #ifdef CONFIG_FS_POSIX_ACL
313         if (sbi->ll_flags & LL_SBI_RMT_CLIENT &&
314             (xattr_type == XATTR_ACL_ACCESS_T ||
315             xattr_type == XATTR_ACL_DEFAULT_T)) {
316                 rce = rct_search(&sbi->ll_rct, cfs_curproc_pid());
317                 if (rce == NULL ||
318                     (rce->rce_ops != RMT_LSETFACL &&
319                     rce->rce_ops != RMT_LGETFACL &&
320                     rce->rce_ops != RMT_RSETFACL &&
321                     rce->rce_ops != RMT_RGETFACL))
322                         RETURN(-EOPNOTSUPP);
323         }
324
325         /* posix acl is under protection of LOOKUP lock. when calling to this,
326          * we just have path resolution to the target inode, so we have great
327          * chance that cached ACL is uptodate.
328          */
329         if (xattr_type == XATTR_ACL_ACCESS_T &&
330             !(sbi->ll_flags & LL_SBI_RMT_CLIENT)) {
331                 struct ll_inode_info *lli = ll_i2info(inode);
332                 struct posix_acl *acl;
333
334                 cfs_spin_lock(&lli->lli_lock);
335                 acl = posix_acl_dup(lli->lli_posix_acl);
336                 cfs_spin_unlock(&lli->lli_lock);
337
338                 if (!acl)
339                         RETURN(-ENODATA);
340
341                 rc = posix_acl_to_xattr(acl, buffer, size);
342                 posix_acl_release(acl);
343                 RETURN(rc);
344         }
345         if (xattr_type == XATTR_ACL_DEFAULT_T && !S_ISDIR(inode->i_mode))
346                 RETURN(-ENODATA);
347 #endif
348
349 do_getxattr:
350         oc = ll_mdscapa_get(inode);
351         rc = md_getxattr(sbi->ll_md_exp, ll_inode2fid(inode), oc,
352                          valid | (rce ? rce_ops2valid(rce->rce_ops) : 0),
353                          name, NULL, 0, size, 0, &req);
354         capa_put(oc);
355         if (rc) {
356                 if (rc == -EOPNOTSUPP && xattr_type == XATTR_USER_T) {
357                         LCONSOLE_INFO("Disabling user_xattr feature because "
358                                       "it is not supported on the server\n");
359                         sbi->ll_flags &= ~LL_SBI_USER_XATTR;
360                 }
361                 RETURN(rc);
362         }
363
364         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
365         LASSERT(body);
366
367         /* only detect the xattr size */
368         if (size == 0)
369                 GOTO(out, rc = body->eadatasize);
370
371         if (size < body->eadatasize) {
372                 CERROR("server bug: replied size %u > %u\n",
373                        body->eadatasize, (int)size);
374                 GOTO(out, rc = -ERANGE);
375         }
376
377         if (body->eadatasize == 0)
378                 GOTO(out, rc = -ENODATA);
379
380         /* do not need swab xattr data */
381         xdata = req_capsule_server_sized_get(&req->rq_pill, &RMF_EADATA,
382                                              body->eadatasize);
383         if (!xdata)
384                 GOTO(out, rc = -EFAULT);
385
386 #ifdef CONFIG_FS_POSIX_ACL
387         if (body->eadatasize >= 0 && rce && rce->rce_ops == RMT_LSETFACL) {
388                 ext_acl_xattr_header *acl;
389
390                 acl = lustre_posix_acl_xattr_2ext((posix_acl_xattr_header *)xdata,
391                                                   body->eadatasize);
392                 if (IS_ERR(acl))
393                         GOTO(out, rc = PTR_ERR(acl));
394
395                 rc = ee_add(&sbi->ll_et, cfs_curproc_pid(), ll_inode2fid(inode),
396                             xattr_type, acl);
397                 if (unlikely(rc < 0)) {
398                         lustre_ext_acl_xattr_free(acl);
399                         GOTO(out, rc);
400                 }
401         }
402 #endif
403
404         if (body->eadatasize == 0) {
405                 rc = -ENODATA;
406         } else {
407                 LASSERT(buffer);
408                 memcpy(buffer, xdata, body->eadatasize);
409                 rc = body->eadatasize;
410         }
411         EXIT;
412 out:
413         ptlrpc_req_finished(req);
414         return rc;
415 }
416
417 ssize_t ll_getxattr(struct dentry *dentry, const char *name,
418                     void *buffer, size_t size)
419 {
420         struct inode *inode = dentry->d_inode;
421
422         LASSERT(inode);
423         LASSERT(name);
424
425         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), xattr %s\n",
426                inode->i_ino, inode->i_generation, inode, name);
427
428         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_GETXATTR, 1);
429
430         if ((strncmp(name, XATTR_TRUSTED_PREFIX,
431                      sizeof(XATTR_TRUSTED_PREFIX) - 1) == 0 &&
432              strcmp(name + sizeof(XATTR_TRUSTED_PREFIX) - 1, "lov") == 0) ||
433             (strncmp(name, XATTR_LUSTRE_PREFIX,
434                      sizeof(XATTR_LUSTRE_PREFIX) - 1) == 0 &&
435              strcmp(name + sizeof(XATTR_LUSTRE_PREFIX) - 1, "lov") == 0)) {
436                 struct lov_user_md *lump;
437                 struct lov_mds_md *lmm = NULL;
438                 struct ptlrpc_request *request = NULL;
439                 int rc = 0, lmmsize = 0;
440
441                 if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
442                         return -ENODATA;
443
444                 if (size == 0 && S_ISDIR(inode->i_mode)) {
445                         /* XXX directory EA is fix for now, optimize to save
446                          * RPC transfer */
447                         GOTO(out, rc = sizeof(struct lov_user_md));
448                 }
449
450                 if (!ll_i2info(inode)->lli_smd) {
451                         if (S_ISDIR(inode->i_mode)) {
452                                 rc = ll_dir_getstripe(inode, &lmm,
453                                                       &lmmsize, &request);
454                         } else {
455                                 rc = -ENODATA;
456                         }
457                 } else {
458                         /* LSM is present already after lookup/getattr call.
459                          * we need to grab layout lock once it is implemented */
460                         rc = obd_packmd(ll_i2dtexp(inode), &lmm,
461                                         ll_i2info(inode)->lli_smd);
462                         lmmsize = rc;
463                 }
464
465                 if (rc < 0)
466                        GOTO(out, rc);
467
468                 if (size == 0) {
469                         /* used to call ll_get_max_mdsize() forward to get
470                          * the maximum buffer size, while some apps (such as
471                          * rsync 3.0.x) care much about the exact xattr value
472                          * size */
473                         rc = lmmsize;
474                         GOTO(out, rc);
475                 }
476
477                 if (size < lmmsize) {
478                         CERROR("server bug: replied size %d > %d for %s (%s)\n",
479                                lmmsize, (int)size, dentry->d_name.name, name);
480                         GOTO(out, rc = -ERANGE);
481                 }
482
483                 lump = (struct lov_user_md *)buffer;
484                 memcpy(lump, lmm, lmmsize);
485
486                 rc = lmmsize;
487 out:
488                 if (request)
489                         ptlrpc_req_finished(request);
490                 else if (lmm)
491                         obd_free_diskmd(ll_i2dtexp(inode), &lmm);
492                 return(rc);
493         }
494
495         return ll_getxattr_common(inode, name, buffer, size, OBD_MD_FLXATTR);
496 }
497
498 ssize_t ll_listxattr(struct dentry *dentry, char *buffer, size_t size)
499 {
500         struct inode *inode = dentry->d_inode;
501         int rc = 0, rc2 = 0;
502         struct lov_mds_md *lmm = NULL;
503         struct ptlrpc_request *request = NULL;
504         int lmmsize;
505
506         LASSERT(inode);
507
508         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n",
509                inode->i_ino, inode->i_generation, inode);
510
511         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_LISTXATTR, 1);
512
513         rc = ll_getxattr_common(inode, NULL, buffer, size, OBD_MD_FLXATTRLS);
514         if (rc < 0)
515                 GOTO(out, rc);
516
517         if (S_ISREG(inode->i_mode)) {
518                 if (ll_i2info(inode)->lli_smd == NULL)
519                         rc2 = -1;
520         } else if (S_ISDIR(inode->i_mode)) {
521                 rc2 = ll_dir_getstripe(inode, &lmm, &lmmsize, &request);
522         }
523
524         if (rc2 < 0) {
525                 GOTO(out, rc2 = 0);
526         } else if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)) {
527                 const int prefix_len = sizeof(XATTR_LUSTRE_PREFIX) - 1;
528                 const size_t name_len   = sizeof("lov") - 1;
529                 const size_t total_len  = prefix_len + name_len + 1;
530
531                 if (buffer && (rc + total_len) <= size) {
532                         buffer += rc;
533                         memcpy(buffer,XATTR_LUSTRE_PREFIX, prefix_len);
534                         memcpy(buffer+prefix_len, "lov", name_len);
535                         buffer[prefix_len + name_len] = '\0';
536                 }
537                 rc2 = total_len;
538         }
539 out:
540         ptlrpc_req_finished(request);
541         rc = rc + rc2;
542
543         return rc;
544 }