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