1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
44#include <obd_support.h>
45#include <lustre_lite.h>
46#include <lustre_dlm.h>
47#include <lustre_ver.h>
48#include <lustre_eacl.h>
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{
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
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;
98 if (xattr_type == XATTR_TRUSTED_T && !cfs_capable(CFS_CAP_SYS_ADMIN))
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);
112 struct ptlrpc_request *req;
113 int xattr_type, rc;
114 struct obd_capa *oc;
115 posix_acl_xattr_header *new_value = NULL;
116 struct rmtacl_ctl_entry *rce = NULL;
117 ext_acl_xattr_header *acl = NULL;
118 const char *pv = value;
119 ENTRY;
120
121 xattr_type = get_xattr_type(name);
122 rc = xattr_type_filter(sbi, xattr_type);
123 if (rc)
124 RETURN(rc);
125
126
127 if ((xattr_type == XATTR_TRUSTED_T && strcmp(name, "trusted.lov") == 0) ||
128 (xattr_type == XATTR_LUSTRE_T && strcmp(name, "lustre.lov") == 0))
129 RETURN(0);
130
131
132 if ((xattr_type == XATTR_SECURITY_T &&
133 strcmp(name, "security.capability") == 0))
134 RETURN(0);
135
136
137 if (xattr_type == XATTR_SECURITY_T && !selinux_is_enabled() &&
138 strcmp(name, "security.selinux") == 0)
139 RETURN(-EOPNOTSUPP);
140
141#ifdef CONFIG_FS_POSIX_ACL
142 if (sbi->ll_flags & LL_SBI_RMT_CLIENT &&
143 (xattr_type == XATTR_ACL_ACCESS_T ||
144 xattr_type == XATTR_ACL_DEFAULT_T)) {
145 rce = rct_search(&sbi->ll_rct, current_pid());
146 if (rce == NULL ||
147 (rce->rce_ops != RMT_LSETFACL &&
148 rce->rce_ops != RMT_RSETFACL))
149 RETURN(-EOPNOTSUPP);
150
151 if (rce->rce_ops == RMT_LSETFACL) {
152 struct eacl_entry *ee;
153
154 ee = et_search_del(&sbi->ll_et, current_pid(),
155 ll_inode2fid(inode), xattr_type);
156 LASSERT(ee != NULL);
157 if (valid & OBD_MD_FLXATTR) {
158 acl = lustre_acl_xattr_merge2ext(
159 (posix_acl_xattr_header *)value,
160 size, ee->ee_acl);
161 if (IS_ERR(acl)) {
162 ee_free(ee);
163 RETURN(PTR_ERR(acl));
164 }
165 size = CFS_ACL_XATTR_SIZE(\
166 le32_to_cpu(acl->a_count), \
167 ext_acl_xattr);
168 pv = (const char *)acl;
169 }
170 ee_free(ee);
171 } else if (rce->rce_ops == RMT_RSETFACL) {
172 size = lustre_posix_acl_xattr_filter(
173 (posix_acl_xattr_header *)value,
174 size, &new_value);
175 if (unlikely(size < 0))
176 RETURN(size);
177
178 pv = (const char *)new_value;
179 } else
180 RETURN(-EOPNOTSUPP);
181
182 valid |= rce_ops2valid(rce->rce_ops);
183 }
184#endif
185 oc = ll_mdscapa_get(inode);
186 rc = md_setxattr(sbi->ll_md_exp, ll_inode2fid(inode), oc,
187 valid, name, pv, size, 0, flags, ll_i2suppgid(inode),
188 &req);
189 capa_put(oc);
190#ifdef CONFIG_FS_POSIX_ACL
191 if (new_value != NULL)
192 lustre_posix_acl_xattr_free(new_value, size);
193 if (acl != NULL)
194 lustre_ext_acl_xattr_free(acl);
195#endif
196 if (rc) {
197 if (rc == -EOPNOTSUPP && xattr_type == XATTR_USER_T) {
198 LCONSOLE_INFO("Disabling user_xattr feature because "
199 "it is not supported on the server\n");
200 sbi->ll_flags &= ~LL_SBI_USER_XATTR;
201 }
202 RETURN(rc);
203 }
204
205 ptlrpc_req_finished(req);
206 RETURN(0);
207}
208
209int ll_setxattr(struct dentry *dentry, const char *name,
210 const void *value, size_t size, int flags)
211{
212 struct inode *inode = dentry->d_inode;
213
214 LASSERT(inode);
215 LASSERT(name);
216
217 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), xattr %s\n",
218 inode->i_ino, inode->i_generation, inode, name);
219
220 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_SETXATTR, 1);
221
222 if ((strncmp(name, XATTR_TRUSTED_PREFIX,
223 sizeof(XATTR_TRUSTED_PREFIX) - 1) == 0 &&
224 strcmp(name + sizeof(XATTR_TRUSTED_PREFIX) - 1, "lov") == 0) ||
225 (strncmp(name, XATTR_LUSTRE_PREFIX,
226 sizeof(XATTR_LUSTRE_PREFIX) - 1) == 0 &&
227 strcmp(name + sizeof(XATTR_LUSTRE_PREFIX) - 1, "lov") == 0)) {
228 struct lov_user_md *lump = (struct lov_user_md *)value;
229 int rc = 0;
230
231
232
233
234 if (lump != NULL && lump->lmm_stripe_offset == 0)
235 lump->lmm_stripe_offset = -1;
236
237 if (lump != NULL && S_ISREG(inode->i_mode)) {
238 struct file f;
239 int flags = FMODE_WRITE;
240 int lum_size = (lump->lmm_magic == LOV_USER_MAGIC_V1) ?
241 sizeof(*lump) : sizeof(struct lov_user_md_v3);
242
243 f.f_dentry = dentry;
244 rc = ll_lov_setstripe_ea_info(inode, &f, flags, lump,
245 lum_size);
246
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
262int 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
277static
278int 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
294
295
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
308 if ((xattr_type == XATTR_SECURITY_T &&
309 strcmp(name, "security.capability") == 0))
310 RETURN(-ENODATA);
311
312
313 if (xattr_type == XATTR_SECURITY_T && !selinux_is_enabled() &&
314 strcmp(name, "security.selinux") == 0)
315 RETURN(-EOPNOTSUPP);
316
317#ifdef CONFIG_FS_POSIX_ACL
318 if (sbi->ll_flags & LL_SBI_RMT_CLIENT &&
319 (xattr_type == XATTR_ACL_ACCESS_T ||
320 xattr_type == XATTR_ACL_DEFAULT_T)) {
321 rce = rct_search(&sbi->ll_rct, current_pid());
322 if (rce == NULL ||
323 (rce->rce_ops != RMT_LSETFACL &&
324 rce->rce_ops != RMT_LGETFACL &&
325 rce->rce_ops != RMT_RSETFACL &&
326 rce->rce_ops != RMT_RGETFACL))
327 RETURN(-EOPNOTSUPP);
328 }
329
330
331
332
333
334 if (xattr_type == XATTR_ACL_ACCESS_T &&
335 !(sbi->ll_flags & LL_SBI_RMT_CLIENT)) {
336 struct ll_inode_info *lli = ll_i2info(inode);
337 struct posix_acl *acl;
338
339 spin_lock(&lli->lli_lock);
340 acl = posix_acl_dup(lli->lli_posix_acl);
341 spin_unlock(&lli->lli_lock);
342
343 if (!acl)
344 RETURN(-ENODATA);
345
346 rc = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
347 posix_acl_release(acl);
348 RETURN(rc);
349 }
350 if (xattr_type == XATTR_ACL_DEFAULT_T && !S_ISDIR(inode->i_mode))
351 RETURN(-ENODATA);
352#endif
353
354do_getxattr:
355 oc = ll_mdscapa_get(inode);
356 rc = md_getxattr(sbi->ll_md_exp, ll_inode2fid(inode), oc,
357 valid | (rce ? rce_ops2valid(rce->rce_ops) : 0),
358 name, NULL, 0, size, 0, &req);
359 capa_put(oc);
360 if (rc) {
361 if (rc == -EOPNOTSUPP && xattr_type == XATTR_USER_T) {
362 LCONSOLE_INFO("Disabling user_xattr feature because "
363 "it is not supported on the server\n");
364 sbi->ll_flags &= ~LL_SBI_USER_XATTR;
365 }
366 RETURN(rc);
367 }
368
369 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
370 LASSERT(body);
371
372
373 if (size == 0)
374 GOTO(out, rc = body->eadatasize);
375
376 if (size < body->eadatasize) {
377 CERROR("server bug: replied size %u > %u\n",
378 body->eadatasize, (int)size);
379 GOTO(out, rc = -ERANGE);
380 }
381
382 if (body->eadatasize == 0)
383 GOTO(out, rc = -ENODATA);
384
385
386 xdata = req_capsule_server_sized_get(&req->rq_pill, &RMF_EADATA,
387 body->eadatasize);
388 if (!xdata)
389 GOTO(out, rc = -EFAULT);
390
391#ifdef CONFIG_FS_POSIX_ACL
392 if (body->eadatasize >= 0 && rce && rce->rce_ops == RMT_LSETFACL) {
393 ext_acl_xattr_header *acl;
394
395 acl = lustre_posix_acl_xattr_2ext((posix_acl_xattr_header *)xdata,
396 body->eadatasize);
397 if (IS_ERR(acl))
398 GOTO(out, rc = PTR_ERR(acl));
399
400 rc = ee_add(&sbi->ll_et, current_pid(), ll_inode2fid(inode),
401 xattr_type, acl);
402 if (unlikely(rc < 0)) {
403 lustre_ext_acl_xattr_free(acl);
404 GOTO(out, rc);
405 }
406 }
407#endif
408
409 if (body->eadatasize == 0) {
410 rc = -ENODATA;
411 } else {
412 LASSERT(buffer);
413 memcpy(buffer, xdata, body->eadatasize);
414 rc = body->eadatasize;
415 }
416 EXIT;
417out:
418 ptlrpc_req_finished(req);
419 return rc;
420}
421
422ssize_t ll_getxattr(struct dentry *dentry, const char *name,
423 void *buffer, size_t size)
424{
425 struct inode *inode = dentry->d_inode;
426
427 LASSERT(inode);
428 LASSERT(name);
429
430 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), xattr %s\n",
431 inode->i_ino, inode->i_generation, inode, name);
432
433 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_GETXATTR, 1);
434
435 if ((strncmp(name, XATTR_TRUSTED_PREFIX,
436 sizeof(XATTR_TRUSTED_PREFIX) - 1) == 0 &&
437 strcmp(name + sizeof(XATTR_TRUSTED_PREFIX) - 1, "lov") == 0) ||
438 (strncmp(name, XATTR_LUSTRE_PREFIX,
439 sizeof(XATTR_LUSTRE_PREFIX) - 1) == 0 &&
440 strcmp(name + sizeof(XATTR_LUSTRE_PREFIX) - 1, "lov") == 0)) {
441 struct lov_stripe_md *lsm;
442 struct lov_user_md *lump;
443 struct lov_mds_md *lmm = NULL;
444 struct ptlrpc_request *request = NULL;
445 int rc = 0, lmmsize = 0;
446
447 if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
448 return -ENODATA;
449
450 if (size == 0 && S_ISDIR(inode->i_mode)) {
451
452
453 GOTO(out, rc = sizeof(struct lov_user_md));
454 }
455
456 lsm = ccc_inode_lsm_get(inode);
457 if (lsm == NULL) {
458 if (S_ISDIR(inode->i_mode)) {
459 rc = ll_dir_getstripe(inode, &lmm,
460 &lmmsize, &request);
461 } else {
462 rc = -ENODATA;
463 }
464 } else {
465
466
467 rc = obd_packmd(ll_i2dtexp(inode), &lmm, lsm);
468 lmmsize = rc;
469 }
470 ccc_inode_lsm_put(inode, lsm);
471
472 if (rc < 0)
473 GOTO(out, rc);
474
475 if (size == 0) {
476
477
478
479
480 rc = lmmsize;
481 GOTO(out, rc);
482 }
483
484 if (size < lmmsize) {
485 CERROR("server bug: replied size %d > %d for %s (%s)\n",
486 lmmsize, (int)size, dentry->d_name.name, name);
487 GOTO(out, rc = -ERANGE);
488 }
489
490 lump = (struct lov_user_md *)buffer;
491 memcpy(lump, lmm, lmmsize);
492
493
494
495 lump->lmm_layout_gen = 0;
496
497 rc = lmmsize;
498out:
499 if (request)
500 ptlrpc_req_finished(request);
501 else if (lmm)
502 obd_free_diskmd(ll_i2dtexp(inode), &lmm);
503 return(rc);
504 }
505
506 return ll_getxattr_common(inode, name, buffer, size, OBD_MD_FLXATTR);
507}
508
509ssize_t ll_listxattr(struct dentry *dentry, char *buffer, size_t size)
510{
511 struct inode *inode = dentry->d_inode;
512 int rc = 0, rc2 = 0;
513 struct lov_mds_md *lmm = NULL;
514 struct ptlrpc_request *request = NULL;
515 int lmmsize;
516
517 LASSERT(inode);
518
519 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n",
520 inode->i_ino, inode->i_generation, inode);
521
522 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_LISTXATTR, 1);
523
524 rc = ll_getxattr_common(inode, NULL, buffer, size, OBD_MD_FLXATTRLS);
525 if (rc < 0)
526 GOTO(out, rc);
527
528 if (buffer != NULL) {
529 struct ll_sb_info *sbi = ll_i2sbi(inode);
530 char *xattr_name = buffer;
531 int xlen, rem = rc;
532
533 while (rem > 0) {
534 xlen = strnlen(xattr_name, rem - 1) + 1;
535 rem -= xlen;
536 if (xattr_type_filter(sbi,
537 get_xattr_type(xattr_name)) == 0) {
538
539
540
541 xattr_name += xlen;
542 continue;
543 }
544
545
546
547 memmove(xattr_name, xattr_name + xlen, rem);
548 rc -= xlen;
549 }
550 }
551 if (S_ISREG(inode->i_mode)) {
552 if (!ll_i2info(inode)->lli_has_smd)
553 rc2 = -1;
554 } else if (S_ISDIR(inode->i_mode)) {
555 rc2 = ll_dir_getstripe(inode, &lmm, &lmmsize, &request);
556 }
557
558 if (rc2 < 0) {
559 GOTO(out, rc2 = 0);
560 } else if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)) {
561 const int prefix_len = sizeof(XATTR_LUSTRE_PREFIX) - 1;
562 const size_t name_len = sizeof("lov") - 1;
563 const size_t total_len = prefix_len + name_len + 1;
564
565 if (buffer && (rc + total_len) <= size) {
566 buffer += rc;
567 memcpy(buffer, XATTR_LUSTRE_PREFIX, prefix_len);
568 memcpy(buffer + prefix_len, "lov", name_len);
569 buffer[prefix_len + name_len] = '\0';
570 }
571 rc2 = total_len;
572 }
573out:
574 ptlrpc_req_finished(request);
575 rc = rc + rc2;
576
577 return rc;
578}
579