Whamcloud - gitweb
6abacbcef537ecaa44fe83c75ec59fd24ded1b2f
[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/xattr.h>
41 #include <linux/selinux.h>
42
43 #define DEBUG_SUBSYSTEM S_LLITE
44
45 #include <obd_support.h>
46 #include <lustre_dlm.h>
47 #include <lustre_ver.h>
48 #include <lustre_eacl.h>
49
50 #include "llite_internal.h"
51
52 /* xattr related to IMA(Integrity Measurement Architecture) */
53 #ifndef XATTR_NAME_IMA
54 #define XATTR_NAME_IMA          "security.ima"
55 #endif
56 #ifndef XATTR_NAME_EVM
57 #define XATTR_NAME_EVM          "security.evm"
58 #endif
59
60 #define XATTR_USER_T            (1)
61 #define XATTR_TRUSTED_T         (2)
62 #define XATTR_SECURITY_T        (3)
63 #define XATTR_ACL_ACCESS_T      (4)
64 #define XATTR_ACL_DEFAULT_T     (5)
65 #define XATTR_LUSTRE_T          (6)
66 #define XATTR_OTHER_T           (7)
67
68 static
69 int get_xattr_type(const char *name)
70 {
71         if (!strcmp(name, POSIX_ACL_XATTR_ACCESS))
72                 return XATTR_ACL_ACCESS_T;
73
74         if (!strcmp(name, POSIX_ACL_XATTR_DEFAULT))
75                 return XATTR_ACL_DEFAULT_T;
76
77         if (!strncmp(name, XATTR_USER_PREFIX,
78                      sizeof(XATTR_USER_PREFIX) - 1))
79                 return XATTR_USER_T;
80
81         if (!strncmp(name, XATTR_TRUSTED_PREFIX,
82                      sizeof(XATTR_TRUSTED_PREFIX) - 1))
83                 return XATTR_TRUSTED_T;
84
85         if (!strncmp(name, XATTR_SECURITY_PREFIX,
86                      sizeof(XATTR_SECURITY_PREFIX) - 1))
87                 return XATTR_SECURITY_T;
88
89         if (!strncmp(name, XATTR_LUSTRE_PREFIX,
90                      sizeof(XATTR_LUSTRE_PREFIX) - 1))
91                 return XATTR_LUSTRE_T;
92
93         return XATTR_OTHER_T;
94 }
95
96 static
97 int xattr_type_filter(struct ll_sb_info *sbi, int xattr_type)
98 {
99         if ((xattr_type == XATTR_ACL_ACCESS_T ||
100              xattr_type == XATTR_ACL_DEFAULT_T) &&
101            !(sbi->ll_flags & LL_SBI_ACL))
102                 return -EOPNOTSUPP;
103
104         if (xattr_type == XATTR_USER_T && !(sbi->ll_flags & LL_SBI_USER_XATTR))
105                 return -EOPNOTSUPP;
106         if (xattr_type == XATTR_TRUSTED_T && !cfs_capable(CFS_CAP_SYS_ADMIN))
107                 return -EPERM;
108         if (xattr_type == XATTR_OTHER_T)
109                 return -EOPNOTSUPP;
110
111         return 0;
112 }
113
114 static
115 int ll_setxattr_common(struct inode *inode, const char *name,
116                        const void *value, size_t size,
117                        int flags, __u64 valid)
118 {
119         struct ll_sb_info *sbi = ll_i2sbi(inode);
120         struct ptlrpc_request *req = NULL;
121         int xattr_type, rc;
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         /*FIXME: enable IMA when the conditions are ready */
129         if (strncmp(name, XATTR_NAME_IMA,
130                     sizeof(XATTR_NAME_IMA)) == 0 ||
131             strncmp(name, XATTR_NAME_EVM,
132                     sizeof(XATTR_NAME_EVM)) == 0)
133                 return -EOPNOTSUPP;
134
135         xattr_type = get_xattr_type(name);
136         rc = xattr_type_filter(sbi, xattr_type);
137         if (rc)
138                 RETURN(rc);
139
140         if ((xattr_type == XATTR_ACL_ACCESS_T ||
141              xattr_type == XATTR_ACL_DEFAULT_T) &&
142 #ifdef HAVE_INODE_OWNER_OR_CAPABLE
143             !inode_owner_or_capable(inode))
144 #else
145             !is_owner_or_cap(inode))
146 #endif
147                 return -EPERM;
148
149         /* b10667: ignore lustre special xattr for now */
150         if (strcmp(name, XATTR_NAME_HSM) == 0 ||
151                 (xattr_type == XATTR_TRUSTED_T &&
152                 strcmp(name, XATTR_NAME_LOV) == 0) ||
153                 (xattr_type == XATTR_LUSTRE_T &&
154                  strcmp(name, "lustre.lov") == 0))
155                 RETURN(0);
156
157         /* b15587: ignore security.capability xattr for now */
158         if ((xattr_type == XATTR_SECURITY_T &&
159             strcmp(name, "security.capability") == 0))
160                 RETURN(0);
161
162         /* LU-549:  Disable security.selinux when selinux is disabled */
163         if (xattr_type == XATTR_SECURITY_T && !selinux_is_enabled() &&
164             strcmp(name, "security.selinux") == 0)
165                 RETURN(-EOPNOTSUPP);
166
167 #ifdef CONFIG_FS_POSIX_ACL
168         if (sbi->ll_flags & LL_SBI_RMT_CLIENT &&
169             (xattr_type == XATTR_ACL_ACCESS_T ||
170             xattr_type == XATTR_ACL_DEFAULT_T)) {
171                 rce = rct_search(&sbi->ll_rct, current_pid());
172                 if (rce == NULL ||
173                     (rce->rce_ops != RMT_LSETFACL &&
174                     rce->rce_ops != RMT_RSETFACL))
175                         RETURN(-EOPNOTSUPP);
176
177                 if (rce->rce_ops == RMT_LSETFACL) {
178                         struct eacl_entry *ee;
179
180                         ee = et_search_del(&sbi->ll_et, current_pid(),
181                                            ll_inode2fid(inode), xattr_type);
182                         LASSERT(ee != NULL);
183                         if (valid & OBD_MD_FLXATTR) {
184                                 acl = lustre_acl_xattr_merge2ext(
185                                                 (posix_acl_xattr_header *)value,
186                                                 size, ee->ee_acl);
187                                 if (IS_ERR(acl)) {
188                                         ee_free(ee);
189                                         RETURN(PTR_ERR(acl));
190                                 }
191                                 size =  CFS_ACL_XATTR_SIZE(\
192                                                 le32_to_cpu(acl->a_count), \
193                                                 ext_acl_xattr);
194                                 pv = (const char *)acl;
195                         }
196                         ee_free(ee);
197                 } else if (rce->rce_ops == RMT_RSETFACL) {
198                         int acl_size = lustre_posix_acl_xattr_filter(
199                                                 (posix_acl_xattr_header *)value,
200                                                 size, &new_value);
201                         if (unlikely(acl_size < 0))
202                                 RETURN(acl_size);
203                         size = acl_size;
204
205                         pv = (const char *)new_value;
206                 } else
207                         RETURN(-EOPNOTSUPP);
208
209                 valid |= rce_ops2valid(rce->rce_ops);
210         }
211 #endif
212         rc = md_setxattr(sbi->ll_md_exp, ll_inode2fid(inode), valid, name, pv,
213                          size, 0, flags, ll_i2suppgid(inode), &req);
214
215 #ifdef CONFIG_FS_POSIX_ACL
216         if (new_value != NULL)
217                 lustre_posix_acl_xattr_free(new_value, size);
218         if (acl != NULL)
219                 lustre_ext_acl_xattr_free(acl);
220 #endif
221         if (rc) {
222                 if (rc == -EOPNOTSUPP && xattr_type == XATTR_USER_T) {
223                         LCONSOLE_INFO("Disabling user_xattr feature because "
224                                       "it is not supported on the server\n");
225                         sbi->ll_flags &= ~LL_SBI_USER_XATTR;
226                 }
227                 RETURN(rc);
228         }
229
230         ptlrpc_req_finished(req);
231         RETURN(0);
232 }
233
234 static int get_hsm_state(struct inode *inode, __u32 *hus_states)
235 {
236         struct md_op_data *op_data;
237         struct hsm_user_state *hus;
238         int rc;
239
240         OBD_ALLOC_PTR(hus);
241         if (hus == NULL)
242                 return -ENOMEM;
243
244         op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
245                                      LUSTRE_OPC_ANY, hus);
246         if (!IS_ERR(op_data)) {
247                 rc = obd_iocontrol(LL_IOC_HSM_STATE_GET, ll_i2mdexp(inode),
248                                    sizeof(*op_data), op_data, NULL);
249                 if (rc == 0)
250                         *hus_states = hus->hus_states;
251                 else
252                         CDEBUG(D_VFSTRACE, "obd_iocontrol failed. rc = %d\n",
253                                rc);
254
255                 ll_finish_md_op_data(op_data);
256         } else {
257                 rc = PTR_ERR(op_data);
258                 CDEBUG(D_VFSTRACE, "Could not prepare the opdata. rc = %d\n",
259                        rc);
260         }
261         OBD_FREE_PTR(hus);
262         return rc;
263 }
264
265 int ll_setxattr(struct dentry *dentry, const char *name,
266                 const void *value, size_t size, int flags)
267 {
268         struct inode *inode = dentry->d_inode;
269
270         LASSERT(inode);
271         LASSERT(name);
272
273         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), xattr %s\n",
274                PFID(ll_inode2fid(inode)), inode, name);
275
276         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_SETXATTR, 1);
277
278         if ((strncmp(name, XATTR_TRUSTED_PREFIX,
279                      sizeof(XATTR_TRUSTED_PREFIX) - 1) == 0 &&
280              strcmp(name + sizeof(XATTR_TRUSTED_PREFIX) - 1, "lov") == 0) ||
281             (strncmp(name, XATTR_LUSTRE_PREFIX,
282                      sizeof(XATTR_LUSTRE_PREFIX) - 1) == 0 &&
283              strcmp(name + sizeof(XATTR_LUSTRE_PREFIX) - 1, "lov") == 0)) {
284                 struct lov_user_md *lump = (struct lov_user_md *)value;
285                 int rc = 0;
286
287                 /* Attributes that are saved via getxattr will always have
288                  * the stripe_offset as 0.  Instead, the MDS should be
289                  * allowed to pick the starting OST index.   b=17846 */
290                 if (lump != NULL && lump->lmm_stripe_offset == 0)
291                         lump->lmm_stripe_offset = -1;
292                 /* Avoid anyone directly setting the RELEASED flag. */
293                 if (lump != NULL &&
294                         (lump->lmm_pattern & LOV_PATTERN_F_RELEASED)) {
295                         /* Only if we have a released flag check if the file
296                         * was indeed archived. */
297                         __u32 state = HS_NONE;
298                         rc = get_hsm_state(inode, &state);
299                         if (rc != 0)
300                                 RETURN(rc);
301                         if (!(state & HS_ARCHIVED)) {
302                                 CDEBUG(D_VFSTRACE,
303                                         "hus_states state = %x, pattern = %x\n",
304                                         state, lump->lmm_pattern);
305                                 /* Here the state is: real file is not
306                                  * archived but user is requesting to set
307                                  * the RELEASED flag so we mask off the
308                                  * released flag from the request */
309                                 lump->lmm_pattern ^= LOV_PATTERN_F_RELEASED;
310                         }
311                 }
312
313                 if (lump != NULL && S_ISREG(inode->i_mode)) {
314                         struct file     f;
315                         __u64           it_flags = FMODE_WRITE;
316                         int             lum_size;
317
318                         lum_size = ll_lov_user_md_size(lump);
319                         if (lum_size < 0 || size < lum_size)
320                                 return 0; /* b=10667: ignore error */
321
322                         memset(&f, 0, sizeof(f)); /* f.f_flags is used below */
323                         f.f_path.dentry = dentry;
324                         rc = ll_lov_setstripe_ea_info(inode, &f, it_flags, lump,
325                                                       lum_size);
326                         /* b=10667: rc always be 0 here for now */
327                         rc = 0;
328                 } else if (S_ISDIR(inode->i_mode)) {
329                         rc = ll_dir_setstripe(inode, lump, 0);
330                 }
331
332                 return rc;
333
334         } else if (strcmp(name, XATTR_NAME_LMA) == 0 ||
335                    strcmp(name, XATTR_NAME_LINK) == 0)
336                 return 0;
337
338         return ll_setxattr_common(inode, name, value, size, flags,
339                                   OBD_MD_FLXATTR);
340 }
341
342 int ll_removexattr(struct dentry *dentry, const char *name)
343 {
344         struct inode *inode = dentry->d_inode;
345
346         LASSERT(inode);
347         LASSERT(name);
348
349         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), xattr %s\n",
350                PFID(ll_inode2fid(inode)), inode, name);
351
352         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_REMOVEXATTR, 1);
353         return ll_setxattr_common(inode, name, NULL, 0, 0,
354                                   OBD_MD_FLXATTRRM);
355 }
356
357 int ll_getxattr_common(struct inode *inode, const char *name,
358                        void *buffer, size_t size, __u64 valid)
359 {
360         struct ll_sb_info *sbi = ll_i2sbi(inode);
361         struct ptlrpc_request *req = NULL;
362         struct mdt_body *body;
363         int xattr_type, rc;
364         void *xdata;
365         struct rmtacl_ctl_entry *rce = NULL;
366         struct ll_inode_info *lli = ll_i2info(inode);
367         ENTRY;
368
369         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p)\n",
370                PFID(ll_inode2fid(inode)), inode);
371
372         /* listxattr have slightly different behavior from of ext3:
373          * without 'user_xattr' ext3 will list all xattr names but
374          * filtered out "^user..*"; we list them all for simplicity.
375          */
376         if (!name) {
377                 xattr_type = XATTR_OTHER_T;
378                 goto do_getxattr;
379         }
380
381         xattr_type = get_xattr_type(name);
382         rc = xattr_type_filter(sbi, xattr_type);
383         if (rc)
384                 RETURN(rc);
385
386         /* b15587: ignore security.capability xattr for now */
387         if ((xattr_type == XATTR_SECURITY_T &&
388             strcmp(name, "security.capability") == 0))
389                 RETURN(-ENODATA);
390
391         /* LU-549:  Disable security.selinux when selinux is disabled */
392         if (xattr_type == XATTR_SECURITY_T && !selinux_is_enabled() &&
393             strcmp(name, "security.selinux") == 0)
394                 RETURN(-EOPNOTSUPP);
395
396 #ifdef CONFIG_FS_POSIX_ACL
397         if (sbi->ll_flags & LL_SBI_RMT_CLIENT &&
398             (xattr_type == XATTR_ACL_ACCESS_T ||
399             xattr_type == XATTR_ACL_DEFAULT_T)) {
400                 rce = rct_search(&sbi->ll_rct, current_pid());
401                 if (rce == NULL ||
402                     (rce->rce_ops != RMT_LSETFACL &&
403                     rce->rce_ops != RMT_LGETFACL &&
404                     rce->rce_ops != RMT_RSETFACL &&
405                     rce->rce_ops != RMT_RGETFACL))
406                         RETURN(-EOPNOTSUPP);
407         }
408
409         /* posix acl is under protection of LOOKUP lock. when calling to this,
410          * we just have path resolution to the target inode, so we have great
411          * chance that cached ACL is uptodate.
412          */
413         if (xattr_type == XATTR_ACL_ACCESS_T &&
414             !(sbi->ll_flags & LL_SBI_RMT_CLIENT)) {
415
416                 struct posix_acl *acl;
417
418                 spin_lock(&lli->lli_lock);
419                 acl = posix_acl_dup(lli->lli_posix_acl);
420                 spin_unlock(&lli->lli_lock);
421
422                 if (!acl)
423                         RETURN(-ENODATA);
424
425                 rc = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
426                 posix_acl_release(acl);
427                 RETURN(rc);
428         }
429         if (xattr_type == XATTR_ACL_DEFAULT_T && !S_ISDIR(inode->i_mode))
430                 RETURN(-ENODATA);
431 #endif
432
433 do_getxattr:
434         if (sbi->ll_xattr_cache_enabled &&
435             xattr_type != XATTR_ACL_ACCESS_T &&
436             (xattr_type != XATTR_SECURITY_T ||
437                 strcmp(name, "security.selinux") != 0)) {
438                 rc = ll_xattr_cache_get(inode, name, buffer, size, valid);
439                 if (rc == -EAGAIN)
440                         goto getxattr_nocache;
441                 if (rc < 0)
442                         GOTO(out_xattr, rc);
443
444                 /* Add "system.posix_acl_access" to the list */
445                 if (lli->lli_posix_acl != NULL && valid & OBD_MD_FLXATTRLS) {
446                         if (size == 0) {
447                                 rc += sizeof(XATTR_NAME_ACL_ACCESS);
448                         } else if (size - rc >= sizeof(XATTR_NAME_ACL_ACCESS)) {
449                                 memcpy(buffer + rc, XATTR_NAME_ACL_ACCESS,
450                                        sizeof(XATTR_NAME_ACL_ACCESS));
451                                 rc += sizeof(XATTR_NAME_ACL_ACCESS);
452                         } else {
453                                 GOTO(out_xattr, rc = -ERANGE);
454                         }
455                 }
456         } else {
457 getxattr_nocache:
458                 rc = md_getxattr(sbi->ll_md_exp, ll_inode2fid(inode),
459                                 valid | (rce ? rce_ops2valid(rce->rce_ops) : 0),
460                                 name, NULL, 0, size, 0, &req);
461
462                 if (rc < 0)
463                         GOTO(out_xattr, rc);
464
465                 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
466                 LASSERT(body);
467
468                 /* only detect the xattr size */
469                 if (size == 0)
470                         GOTO(out, rc = body->mbo_eadatasize);
471
472                 if (size < body->mbo_eadatasize) {
473                         CERROR("server bug: replied size %u > %u\n",
474                                 body->mbo_eadatasize, (int)size);
475                         GOTO(out, rc = -ERANGE);
476                 }
477
478                 if (body->mbo_eadatasize == 0)
479                         GOTO(out, rc = -ENODATA);
480
481                 /* do not need swab xattr data */
482                 xdata = req_capsule_server_sized_get(&req->rq_pill, &RMF_EADATA,
483                                                         body->mbo_eadatasize);
484                 if (!xdata)
485                         GOTO(out, rc = -EFAULT);
486
487                 memcpy(buffer, xdata, body->mbo_eadatasize);
488                 rc = body->mbo_eadatasize;
489         }
490
491 #ifdef CONFIG_FS_POSIX_ACL
492         if (rce != NULL && rce->rce_ops == RMT_LSETFACL) {
493                 ext_acl_xattr_header *acl;
494
495                 acl = lustre_posix_acl_xattr_2ext(buffer, rc);
496                 if (IS_ERR(acl))
497                         GOTO(out, rc = PTR_ERR(acl));
498
499                 rc = ee_add(&sbi->ll_et, current_pid(), ll_inode2fid(inode),
500                             xattr_type, acl);
501                 if (unlikely(rc < 0)) {
502                         lustre_ext_acl_xattr_free(acl);
503                         GOTO(out, rc);
504                 }
505         }
506 #endif
507         EXIT;
508
509 out_xattr:
510         if (rc == -EOPNOTSUPP && xattr_type == XATTR_USER_T) {
511                 LCONSOLE_INFO("%s: disabling user_xattr feature because "
512                                 "it is not supported on the server: rc = %d\n",
513                                 ll_get_fsname(inode->i_sb, NULL, 0), rc);
514                 sbi->ll_flags &= ~LL_SBI_USER_XATTR;
515         }
516 out:
517         ptlrpc_req_finished(req);
518         return rc;
519 }
520
521 static ssize_t ll_getxattr_lov(struct inode *inode, void *buf, size_t buf_size)
522 {
523         ssize_t rc;
524
525         if (S_ISREG(inode->i_mode)) {
526                 struct cl_object *obj = ll_i2info(inode)->lli_clob;
527                 struct lu_env *env;
528                 struct cl_layout cl = {
529                         .cl_buf.lb_buf = buf,
530                         .cl_buf.lb_len = buf_size,
531                 };
532                 int refcheck;
533
534                 if (obj == NULL)
535                         RETURN(-ENODATA);
536
537                 env = cl_env_get(&refcheck);
538                 if (IS_ERR(env))
539                         RETURN(PTR_ERR(env));
540
541                 rc = cl_object_layout_get(env, obj, &cl);
542                 if (rc < 0)
543                         GOTO(out_env, rc);
544
545                 if (cl.cl_size == 0)
546                         GOTO(out_env, rc = -ENODATA);
547
548                 rc = cl.cl_size;
549
550                 if (buf_size == 0)
551                         GOTO(out_env, rc);
552
553                 LASSERT(buf != NULL && rc <= buf_size);
554
555                 /* Do not return layout gen for getxattr() since
556                  * otherwise it would confuse tar --xattr by
557                  * recognizing layout gen as stripe offset when the
558                  * file is restored. See LU-2809. */
559                 ((struct lov_mds_md *)buf)->lmm_layout_gen = 0;
560 out_env:
561                 cl_env_put(env, &refcheck);
562
563                 RETURN(rc);
564         } else if (S_ISDIR(inode->i_mode)) {
565                 struct lov_mds_md *lmm = NULL;
566                 int lmm_size = 0;
567                 struct ptlrpc_request *req = NULL;
568
569                 rc = ll_dir_getstripe(inode, (void **)&lmm, &lmm_size,
570                                       &req, 0);
571                 if (rc < 0)
572                         GOTO(out_req, rc);
573
574                 if (buf_size == 0)
575                         GOTO(out_req, rc = lmm_size);
576
577                 if (buf_size < lmm_size)
578                         GOTO(out_req, rc = -ERANGE);
579
580                 memcpy(buf, lmm, lmm_size);
581                 GOTO(out_req, rc = lmm_size);
582 out_req:
583                 if (req != NULL)
584                         ptlrpc_req_finished(req);
585
586                 return rc;
587         } else {
588                 RETURN(-ENODATA);
589         }
590 }
591
592 ssize_t ll_getxattr(struct dentry *dentry, const char *name, void *buf,
593                     size_t buf_size)
594 {
595         struct inode *inode = dentry->d_inode;
596
597         LASSERT(inode);
598         LASSERT(name);
599
600         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), xattr %s\n",
601                PFID(ll_inode2fid(inode)), inode, name);
602
603         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_GETXATTR, 1);
604
605         if (strcmp(name, XATTR_LUSTRE_LOV) == 0 ||
606             strcmp(name, XATTR_NAME_LOV) == 0)
607                 return ll_getxattr_lov(inode, buf, buf_size);
608         else
609                 return ll_getxattr_common(inode, name, buf, buf_size,
610                                           OBD_MD_FLXATTR);
611 }
612
613 ssize_t ll_listxattr(struct dentry *dentry, char *buf, size_t buf_size)
614 {
615         struct inode *inode = dentry->d_inode;
616         struct ll_sb_info *sbi = ll_i2sbi(inode);
617         char *xattr_name;
618         ssize_t rc, rc2;
619         size_t len, rem;
620
621         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p)\n",
622                PFID(ll_inode2fid(inode)), inode);
623
624         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_LISTXATTR, 1);
625
626         rc = ll_getxattr_common(inode, NULL, buf, buf_size, OBD_MD_FLXATTRLS);
627         if (rc < 0)
628                 RETURN(rc);
629
630         /* If we're being called to get the size of the xattr list
631          * (buf_size == 0) then just assume that a lustre.lov xattr
632          * exists. */
633         if (buf_size == 0)
634                 RETURN(rc + sizeof(XATTR_LUSTRE_LOV));
635
636         xattr_name = buf;
637         rem = rc;
638
639         while (rem > 0) {
640                 len = strnlen(xattr_name, rem - 1) + 1;
641                 rem -= len;
642                 if (xattr_type_filter(sbi, get_xattr_type(xattr_name)) == 0) {
643                         /* Skip OK xattr type, leave it in buffer. */
644                         xattr_name += len;
645                         continue;
646                 }
647
648                 /* Move up remaining xattrs in buffer removing the
649                  * xattr that is not OK. */
650                 memmove(xattr_name, xattr_name + len, rem);
651                 rc -= len;
652         }
653
654         rc2 = ll_getxattr_lov(inode, NULL, 0);
655         if (rc2 == -ENODATA)
656                 RETURN(rc);
657
658         if (rc2 < 0)
659                 RETURN(rc2);
660
661         if (buf_size < rc + sizeof(XATTR_LUSTRE_LOV))
662                 RETURN(-ERANGE);
663
664         memcpy(buf + rc, XATTR_LUSTRE_LOV, sizeof(XATTR_LUSTRE_LOV));
665
666         RETURN(rc + sizeof(XATTR_LUSTRE_LOV));
667 }