staging: lustre: lnet: Remove old commented out code
[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
1e1f9ff4
JS
220static int
221ll_xattr_list(struct inode *inode, const char *name, int type, void *buffer,
222 size_t size, __u64 valid)
d7e09d03 223{
1e1f9ff4 224 struct ll_inode_info *lli = ll_i2info(inode);
d7e09d03
PT
225 struct ll_sb_info *sbi = ll_i2sbi(inode);
226 struct ptlrpc_request *req = NULL;
227 struct mdt_body *body;
d7e09d03 228 void *xdata;
1e1f9ff4 229 int rc;
d7e09d03 230
1e1f9ff4 231 if (sbi->ll_xattr_cache_enabled && type != XATTR_ACL_ACCESS_T) {
7fc1f831 232 rc = ll_xattr_cache_get(inode, name, buffer, size, valid);
e93a3082
AP
233 if (rc == -EAGAIN)
234 goto getxattr_nocache;
7fc1f831 235 if (rc < 0)
34e1f2bb 236 goto out_xattr;
e93a3082
AP
237
238 /* Add "system.posix_acl_access" to the list */
6e16818b 239 if (lli->lli_posix_acl && valid & OBD_MD_FLXATTRLS) {
e93a3082
AP
240 if (size == 0) {
241 rc += sizeof(XATTR_NAME_ACL_ACCESS);
242 } else if (size - rc >= sizeof(XATTR_NAME_ACL_ACCESS)) {
243 memcpy(buffer + rc, XATTR_NAME_ACL_ACCESS,
244 sizeof(XATTR_NAME_ACL_ACCESS));
245 rc += sizeof(XATTR_NAME_ACL_ACCESS);
246 } else {
34e1f2bb
JL
247 rc = -ERANGE;
248 goto out_xattr;
e93a3082
AP
249 }
250 }
7fc1f831 251 } else {
e93a3082 252getxattr_nocache:
ef2e0f55 253 rc = md_getxattr(sbi->ll_md_exp, ll_inode2fid(inode),
341f1f0a 254 valid, name, NULL, 0, size, 0, &req);
7fc1f831 255 if (rc < 0)
34e1f2bb 256 goto out_xattr;
7fc1f831
AP
257
258 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
259 LASSERT(body);
260
261 /* only detect the xattr size */
34e1f2bb
JL
262 if (size == 0) {
263 rc = body->eadatasize;
264 goto out;
265 }
7fc1f831
AP
266
267 if (size < body->eadatasize) {
268 CERROR("server bug: replied size %u > %u\n",
e15ba45d 269 body->eadatasize, (int)size);
34e1f2bb
JL
270 rc = -ERANGE;
271 goto out;
d7e09d03 272 }
d7e09d03 273
34e1f2bb
JL
274 if (body->eadatasize == 0) {
275 rc = -ENODATA;
276 goto out;
277 }
d7e09d03 278
7fc1f831
AP
279 /* do not need swab xattr data */
280 xdata = req_capsule_server_sized_get(&req->rq_pill, &RMF_EADATA,
e15ba45d 281 body->eadatasize);
34e1f2bb
JL
282 if (!xdata) {
283 rc = -EFAULT;
284 goto out;
285 }
d7e09d03 286
7fc1f831
AP
287 memcpy(buffer, xdata, body->eadatasize);
288 rc = body->eadatasize;
d7e09d03
PT
289 }
290
7fc1f831 291out_xattr:
1e1f9ff4 292 if (rc == -EOPNOTSUPP && type == XATTR_USER_T) {
7fc1f831
AP
293 LCONSOLE_INFO(
294 "%s: disabling user_xattr feature because it is not supported on the server: rc = %d\n",
295 ll_get_fsname(inode->i_sb, NULL, 0), rc);
296 sbi->ll_flags &= ~LL_SBI_USER_XATTR;
d7e09d03 297 }
d7e09d03
PT
298out:
299 ptlrpc_req_finished(req);
300 return rc;
301}
302
1e1f9ff4
JS
303static
304int ll_getxattr_common(struct inode *inode, const char *name,
305 void *buffer, size_t size)
306{
307 struct ll_sb_info *sbi = ll_i2sbi(inode);
308 int xattr_type, rc;
309 struct ll_inode_info *lli = ll_i2info(inode);
310
311 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p)\n",
312 PFID(ll_inode2fid(inode)), inode);
313
314 xattr_type = get_xattr_type(name);
315 rc = xattr_type_filter(sbi, xattr_type);
316 if (rc)
317 return rc;
318
319 /* b15587: ignore security.capability xattr for now */
320 if ((xattr_type == XATTR_SECURITY_T &&
321 strcmp(name, "security.capability") == 0))
322 return -ENODATA;
323
324 /* LU-549: Disable security.selinux when selinux is disabled */
325 if (xattr_type == XATTR_SECURITY_T && !selinux_is_enabled() &&
326 strcmp(name, "security.selinux") == 0)
327 return -EOPNOTSUPP;
328
329#ifdef CONFIG_FS_POSIX_ACL
330 /* posix acl is under protection of LOOKUP lock. when calling to this,
331 * we just have path resolution to the target inode, so we have great
332 * chance that cached ACL is uptodate.
333 */
334 if (xattr_type == XATTR_ACL_ACCESS_T) {
335 struct posix_acl *acl;
336
337 spin_lock(&lli->lli_lock);
338 acl = posix_acl_dup(lli->lli_posix_acl);
339 spin_unlock(&lli->lli_lock);
340
341 if (!acl)
342 return -ENODATA;
343
344 rc = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
345 posix_acl_release(acl);
346 return rc;
347 }
348 if (xattr_type == XATTR_ACL_DEFAULT_T && !S_ISDIR(inode->i_mode))
349 return -ENODATA;
350#endif
351 return ll_xattr_list(inode, name, xattr_type, buffer, size,
352 OBD_MD_FLXATTR);
353}
354
ce23e640
AV
355ssize_t ll_getxattr(struct dentry *dentry, struct inode *inode,
356 const char *name, void *buffer, size_t size)
d7e09d03 357{
d7e09d03
PT
358 LASSERT(inode);
359 LASSERT(name);
360
97a075cd
JN
361 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), xattr %s\n",
362 PFID(ll_inode2fid(inode)), inode, name);
d7e09d03
PT
363
364 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_GETXATTR, 1);
365
366 if ((strncmp(name, XATTR_TRUSTED_PREFIX,
367 sizeof(XATTR_TRUSTED_PREFIX) - 1) == 0 &&
368 strcmp(name + sizeof(XATTR_TRUSTED_PREFIX) - 1, "lov") == 0) ||
369 (strncmp(name, XATTR_LUSTRE_PREFIX,
370 sizeof(XATTR_LUSTRE_PREFIX) - 1) == 0 &&
371 strcmp(name + sizeof(XATTR_LUSTRE_PREFIX) - 1, "lov") == 0)) {
372 struct lov_stripe_md *lsm;
373 struct lov_user_md *lump;
374 struct lov_mds_md *lmm = NULL;
375 struct ptlrpc_request *request = NULL;
376 int rc = 0, lmmsize = 0;
377
378 if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
379 return -ENODATA;
380
381 if (size == 0 && S_ISDIR(inode->i_mode)) {
382 /* XXX directory EA is fix for now, optimize to save
c0894c6c
OD
383 * RPC transfer
384 */
34e1f2bb
JL
385 rc = sizeof(struct lov_user_md);
386 goto out;
d7e09d03
PT
387 }
388
389 lsm = ccc_inode_lsm_get(inode);
6e16818b 390 if (!lsm) {
d7e09d03
PT
391 if (S_ISDIR(inode->i_mode)) {
392 rc = ll_dir_getstripe(inode, &lmm,
393 &lmmsize, &request);
394 } else {
395 rc = -ENODATA;
396 }
397 } else {
398 /* LSM is present already after lookup/getattr call.
c0894c6c
OD
399 * we need to grab layout lock once it is implemented
400 */
d7e09d03
PT
401 rc = obd_packmd(ll_i2dtexp(inode), &lmm, lsm);
402 lmmsize = rc;
403 }
404 ccc_inode_lsm_put(inode, lsm);
405
406 if (rc < 0)
34e1f2bb 407 goto out;
d7e09d03
PT
408
409 if (size == 0) {
410 /* used to call ll_get_max_mdsize() forward to get
411 * the maximum buffer size, while some apps (such as
412 * rsync 3.0.x) care much about the exact xattr value
c0894c6c
OD
413 * size
414 */
d7e09d03 415 rc = lmmsize;
34e1f2bb 416 goto out;
d7e09d03
PT
417 }
418
419 if (size < lmmsize) {
09561a53
AV
420 CERROR("server bug: replied size %d > %d for %pd (%s)\n",
421 lmmsize, (int)size, dentry, name);
34e1f2bb
JL
422 rc = -ERANGE;
423 goto out;
d7e09d03
PT
424 }
425
634ffdd9 426 lump = buffer;
d7e09d03
PT
427 memcpy(lump, lmm, lmmsize);
428 /* do not return layout gen for getxattr otherwise it would
429 * confuse tar --xattr by recognizing layout gen as stripe
c0894c6c
OD
430 * offset when the file is restored. See LU-2809.
431 */
d7e09d03
PT
432 lump->lmm_layout_gen = 0;
433
434 rc = lmmsize;
435out:
436 if (request)
437 ptlrpc_req_finished(request);
438 else if (lmm)
439 obd_free_diskmd(ll_i2dtexp(inode), &lmm);
fbe7c6c7 440 return rc;
d7e09d03
PT
441 }
442
1e1f9ff4 443 return ll_getxattr_common(inode, name, buffer, size);
d7e09d03
PT
444}
445
446ssize_t ll_listxattr(struct dentry *dentry, char *buffer, size_t size)
447{
2b0143b5 448 struct inode *inode = d_inode(dentry);
d7e09d03
PT
449 int rc = 0, rc2 = 0;
450 struct lov_mds_md *lmm = NULL;
451 struct ptlrpc_request *request = NULL;
452 int lmmsize;
453
454 LASSERT(inode);
455
97a075cd
JN
456 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p)\n",
457 PFID(ll_inode2fid(inode)), inode);
d7e09d03
PT
458
459 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_LISTXATTR, 1);
460
1e1f9ff4
JS
461 rc = ll_xattr_list(inode, NULL, XATTR_OTHER_T, buffer, size,
462 OBD_MD_FLXATTRLS);
d7e09d03 463 if (rc < 0)
34e1f2bb 464 goto out;
d7e09d03 465
6e16818b 466 if (buffer) {
d7e09d03
PT
467 struct ll_sb_info *sbi = ll_i2sbi(inode);
468 char *xattr_name = buffer;
469 int xlen, rem = rc;
470
471 while (rem > 0) {
472 xlen = strnlen(xattr_name, rem - 1) + 1;
473 rem -= xlen;
474 if (xattr_type_filter(sbi,
475 get_xattr_type(xattr_name)) == 0) {
476 /* skip OK xattr type
477 * leave it in buffer
478 */
479 xattr_name += xlen;
480 continue;
481 }
482 /* move up remaining xattrs in buffer
483 * removing the xattr that is not OK
484 */
485 memmove(xattr_name, xattr_name + xlen, rem);
486 rc -= xlen;
487 }
488 }
489 if (S_ISREG(inode->i_mode)) {
490 if (!ll_i2info(inode)->lli_has_smd)
491 rc2 = -1;
492 } else if (S_ISDIR(inode->i_mode)) {
493 rc2 = ll_dir_getstripe(inode, &lmm, &lmmsize, &request);
494 }
495
496 if (rc2 < 0) {
34e1f2bb
JL
497 rc2 = 0;
498 goto out;
d7e09d03
PT
499 } else if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)) {
500 const int prefix_len = sizeof(XATTR_LUSTRE_PREFIX) - 1;
501 const size_t name_len = sizeof("lov") - 1;
502 const size_t total_len = prefix_len + name_len + 1;
503
6e16818b 504 if (((rc + total_len) > size) && buffer) {
cb6a2db6
KM
505 ptlrpc_req_finished(request);
506 return -ERANGE;
507 }
508
6e16818b 509 if (buffer) {
d7e09d03
PT
510 buffer += rc;
511 memcpy(buffer, XATTR_LUSTRE_PREFIX, prefix_len);
512 memcpy(buffer + prefix_len, "lov", name_len);
513 buffer[prefix_len + name_len] = '\0';
514 }
515 rc2 = total_len;
516 }
517out:
518 ptlrpc_req_finished(request);
519 rc = rc + rc2;
520
521 return rc;
522}