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