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