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