Whamcloud - gitweb
LU-1146 build: batch update copyright messages
[fs/lustre-release.git] / lustre / llite / xattr.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  *
32  * Copyright (c) 2011, 2012, Whamcloud, Inc.
33  */
34 /*
35  * This file is part of Lustre, http://www.lustre.org/
36  * Lustre is a trademark of Sun Microsystems, Inc.
37  */
38
39 #include <linux/fs.h>
40 #include <linux/sched.h>
41 #include <linux/mm.h>
42 #include <linux/smp_lock.h>
43
44 #define DEBUG_SUBSYSTEM S_LLITE
45
46 #include <obd_support.h>
47 #include <lustre_lite.h>
48 #include <lustre_dlm.h>
49 #include <lustre_ver.h>
50 #include <lustre_acl.h>
51
52 #include "llite_internal.h"
53
54 #define XATTR_USER_T            (1)
55 #define XATTR_TRUSTED_T         (2)
56 #define XATTR_SECURITY_T        (3)
57 #define XATTR_ACL_ACCESS_T      (4)
58 #define XATTR_ACL_DEFAULT_T     (5)
59 #define XATTR_LUSTRE_T          (6)
60 #define XATTR_OTHER_T           (7)
61
62 static
63 int get_xattr_type(const char *name)
64 {
65         if (!strcmp(name, POSIX_ACL_XATTR_ACCESS))
66                 return XATTR_ACL_ACCESS_T;
67
68         if (!strcmp(name, POSIX_ACL_XATTR_DEFAULT))
69                 return XATTR_ACL_DEFAULT_T;
70
71         if (!strncmp(name, XATTR_USER_PREFIX,
72                      sizeof(XATTR_USER_PREFIX) - 1))
73                 return XATTR_USER_T;
74
75         if (!strncmp(name, XATTR_TRUSTED_PREFIX,
76                      sizeof(XATTR_TRUSTED_PREFIX) - 1))
77                 return XATTR_TRUSTED_T;
78
79         if (!strncmp(name, XATTR_SECURITY_PREFIX,
80                      sizeof(XATTR_SECURITY_PREFIX) - 1))
81                 return XATTR_SECURITY_T;
82
83         if (!strncmp(name, XATTR_LUSTRE_PREFIX,
84                      sizeof(XATTR_LUSTRE_PREFIX) - 1))
85                 return XATTR_LUSTRE_T;
86
87         return XATTR_OTHER_T;
88 }
89
90 static
91 int xattr_type_filter(struct ll_sb_info *sbi, int xattr_type)
92 {
93         if ((xattr_type == XATTR_ACL_ACCESS_T ||
94              xattr_type == XATTR_ACL_DEFAULT_T) &&
95            !(sbi->ll_flags & LL_SBI_ACL))
96                 return -EOPNOTSUPP;
97
98         if (xattr_type == XATTR_USER_T && !(sbi->ll_flags & LL_SBI_USER_XATTR))
99                 return -EOPNOTSUPP;
100         if (xattr_type == XATTR_TRUSTED_T && !cfs_capable(CFS_CAP_SYS_ADMIN))
101                 return -EPERM;
102         if (xattr_type == XATTR_OTHER_T)
103                 return -EOPNOTSUPP;
104
105         return 0;
106 }
107
108 static
109 int ll_setxattr_common(struct inode *inode, const char *name,
110                        const void *value, size_t size,
111                        int flags, __u64 valid)
112 {
113         struct ll_sb_info *sbi = ll_i2sbi(inode);
114         struct ptlrpc_request *req;
115         int xattr_type, rc;
116         struct obd_capa *oc;
117         posix_acl_xattr_header *new_value = NULL;
118         struct rmtacl_ctl_entry *rce = NULL;
119         ext_acl_xattr_header *acl = NULL;
120         const char *pv = value;
121         ENTRY;
122
123         xattr_type = get_xattr_type(name);
124         rc = xattr_type_filter(sbi, xattr_type);
125         if (rc)
126                 RETURN(rc);
127
128         /* b10667: ignore lustre special xattr for now */
129         if ((xattr_type == XATTR_TRUSTED_T && strcmp(name, "trusted.lov") == 0) ||
130             (xattr_type == XATTR_LUSTRE_T && strcmp(name, "lustre.lov") == 0))
131                 RETURN(0);
132
133         /* b15587: ignore security.capability xattr for now */
134         if ((xattr_type == XATTR_SECURITY_T &&
135             strcmp(name, "security.capability") == 0))
136                 RETURN(0);
137
138 #ifdef CONFIG_FS_POSIX_ACL
139         if (sbi->ll_flags & LL_SBI_RMT_CLIENT &&
140             (xattr_type == XATTR_ACL_ACCESS_T ||
141             xattr_type == XATTR_ACL_DEFAULT_T)) {
142                 rce = rct_search(&sbi->ll_rct, cfs_curproc_pid());
143                 if (rce == NULL ||
144                     (rce->rce_ops != RMT_LSETFACL &&
145                     rce->rce_ops != RMT_RSETFACL))
146                         RETURN(-EOPNOTSUPP);
147
148                 if (rce->rce_ops == RMT_LSETFACL) {
149                         struct eacl_entry *ee;
150
151                         ee = et_search_del(&sbi->ll_et, cfs_curproc_pid(),
152                                            ll_inode2fid(inode), xattr_type);
153                         LASSERT(ee != NULL);
154                         if (valid & OBD_MD_FLXATTR) {
155                                 acl = lustre_acl_xattr_merge2ext(
156                                                 (posix_acl_xattr_header *)value,
157                                                 size, ee->ee_acl);
158                                 if (IS_ERR(acl)) {
159                                         ee_free(ee);
160                                         RETURN(PTR_ERR(acl));
161                                 }
162                                 size =  CFS_ACL_XATTR_SIZE(\
163                                                 le32_to_cpu(acl->a_count), \
164                                                 ext_acl_xattr);
165                                 pv = (const char *)acl;
166                         }
167                         ee_free(ee);
168                 } else if (rce->rce_ops == RMT_RSETFACL) {
169                         size = lustre_posix_acl_xattr_filter(
170                                                 (posix_acl_xattr_header *)value,
171                                                 size, &new_value);
172                         if (unlikely(size < 0))
173                                 RETURN(size);
174
175                         pv = (const char *)new_value;
176                 } else
177                         RETURN(-EOPNOTSUPP);
178
179                 valid |= rce_ops2valid(rce->rce_ops);
180         }
181 #endif
182         oc = ll_mdscapa_get(inode);
183         rc = md_setxattr(sbi->ll_md_exp, ll_inode2fid(inode), oc,
184                          valid, name, pv, size, 0, flags, ll_i2suppgid(inode),
185                          &req);
186         capa_put(oc);
187 #ifdef CONFIG_FS_POSIX_ACL
188         if (new_value != NULL)
189                 lustre_posix_acl_xattr_free(new_value, size);
190         if (acl != NULL)
191                 lustre_ext_acl_xattr_free(acl);
192 #endif
193         if (rc) {
194                 if (rc == -EOPNOTSUPP && xattr_type == XATTR_USER_T) {
195                         LCONSOLE_INFO("Disabling user_xattr feature because "
196                                       "it is not supported on the server\n");
197                         sbi->ll_flags &= ~LL_SBI_USER_XATTR;
198                 }
199                 RETURN(rc);
200         }
201
202         ptlrpc_req_finished(req);
203         RETURN(0);
204 }
205
206 int ll_setxattr(struct dentry *dentry, const char *name,
207                 const void *value, size_t size, int flags)
208 {
209         struct inode *inode = dentry->d_inode;
210
211         LASSERT(inode);
212         LASSERT(name);
213
214         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), xattr %s\n",
215                inode->i_ino, inode->i_generation, inode, name);
216
217         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_SETXATTR, 1);
218
219         if ((strncmp(name, XATTR_TRUSTED_PREFIX,
220                      sizeof(XATTR_TRUSTED_PREFIX) - 1) == 0 &&
221              strcmp(name + sizeof(XATTR_TRUSTED_PREFIX) - 1, "lov") == 0) ||
222             (strncmp(name, XATTR_LUSTRE_PREFIX,
223                      sizeof(XATTR_LUSTRE_PREFIX) - 1) == 0 &&
224              strcmp(name + sizeof(XATTR_LUSTRE_PREFIX) - 1, "lov") == 0)) {
225                 struct lov_user_md *lump = (struct lov_user_md *)value;
226                 int rc = 0;
227
228                 /* Attributes that are saved via getxattr will always have
229                  * the stripe_offset as 0.  Instead, the MDS should be
230                  * allowed to pick the starting OST index.   b=17846 */
231                 if (lump != NULL && lump->lmm_stripe_offset == 0)
232                         lump->lmm_stripe_offset = -1;
233
234                 if (lump != NULL && S_ISREG(inode->i_mode)) {
235                         struct file f;
236                         int flags = FMODE_WRITE;
237
238                         f.f_dentry = dentry;
239                         rc = ll_lov_setstripe_ea_info(inode, &f, flags,
240                                                       lump, sizeof(*lump));
241                         /* b10667: rc always be 0 here for now */
242                         rc = 0;
243                 } else if (S_ISDIR(inode->i_mode)) {
244                         rc = ll_dir_setstripe(inode, lump, 0);
245                 }
246
247                 return rc;
248
249         } else if (strcmp(name, XATTR_NAME_LMA) == 0 ||
250                    strcmp(name, XATTR_NAME_LINK) == 0)
251                 return 0;
252
253         return ll_setxattr_common(inode, name, value, size, flags,
254                                   OBD_MD_FLXATTR);
255 }
256
257 int ll_removexattr(struct dentry *dentry, const char *name)
258 {
259         struct inode *inode = dentry->d_inode;
260
261         LASSERT(inode);
262         LASSERT(name);
263
264         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), xattr %s\n",
265                inode->i_ino, inode->i_generation, inode, name);
266
267         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_REMOVEXATTR, 1);
268         return ll_setxattr_common(inode, name, NULL, 0, 0,
269                                   OBD_MD_FLXATTRRM);
270 }
271
272 static
273 int ll_getxattr_common(struct inode *inode, const char *name,
274                        void *buffer, size_t size, __u64 valid)
275 {
276         struct ll_sb_info *sbi = ll_i2sbi(inode);
277         struct ptlrpc_request *req = NULL;
278         struct mdt_body *body;
279         int xattr_type, rc;
280         void *xdata;
281         struct obd_capa *oc;
282         struct rmtacl_ctl_entry *rce = NULL;
283         ENTRY;
284
285         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n",
286                inode->i_ino, inode->i_generation, inode);
287
288         /* listxattr have slightly different behavior from of ext3:
289          * without 'user_xattr' ext3 will list all xattr names but
290          * filtered out "^user..*"; we list them all for simplicity.
291          */
292         if (!name) {
293                 xattr_type = XATTR_OTHER_T;
294                 goto do_getxattr;
295         }
296
297         xattr_type = get_xattr_type(name);
298         rc = xattr_type_filter(sbi, xattr_type);
299         if (rc)
300                 RETURN(rc);
301
302         /* b15587: ignore security.capability xattr for now */
303         if ((xattr_type == XATTR_SECURITY_T &&
304             strcmp(name, "security.capability") == 0))
305                 RETURN(-ENODATA);
306
307 #ifdef CONFIG_FS_POSIX_ACL
308         if (sbi->ll_flags & LL_SBI_RMT_CLIENT &&
309             (xattr_type == XATTR_ACL_ACCESS_T ||
310             xattr_type == XATTR_ACL_DEFAULT_T)) {
311                 rce = rct_search(&sbi->ll_rct, cfs_curproc_pid());
312                 if (rce == NULL ||
313                     (rce->rce_ops != RMT_LSETFACL &&
314                     rce->rce_ops != RMT_LGETFACL &&
315                     rce->rce_ops != RMT_RSETFACL &&
316                     rce->rce_ops != RMT_RGETFACL))
317                         RETURN(-EOPNOTSUPP);
318         }
319
320         /* posix acl is under protection of LOOKUP lock. when calling to this,
321          * we just have path resolution to the target inode, so we have great
322          * chance that cached ACL is uptodate.
323          */
324         if (xattr_type == XATTR_ACL_ACCESS_T &&
325             !(sbi->ll_flags & LL_SBI_RMT_CLIENT)) {
326                 struct ll_inode_info *lli = ll_i2info(inode);
327                 struct posix_acl *acl;
328
329                 cfs_spin_lock(&lli->lli_lock);
330                 acl = posix_acl_dup(lli->lli_posix_acl);
331                 cfs_spin_unlock(&lli->lli_lock);
332
333                 if (!acl)
334                         RETURN(-ENODATA);
335
336                 rc = posix_acl_to_xattr(acl, buffer, size);
337                 posix_acl_release(acl);
338                 RETURN(rc);
339         }
340         if (xattr_type == XATTR_ACL_DEFAULT_T && !S_ISDIR(inode->i_mode))
341                 RETURN(-ENODATA);
342 #endif
343
344 do_getxattr:
345         oc = ll_mdscapa_get(inode);
346         rc = md_getxattr(sbi->ll_md_exp, ll_inode2fid(inode), oc,
347                          valid | (rce ? rce_ops2valid(rce->rce_ops) : 0),
348                          name, NULL, 0, size, 0, &req);
349         capa_put(oc);
350         if (rc) {
351                 if (rc == -EOPNOTSUPP && xattr_type == XATTR_USER_T) {
352                         LCONSOLE_INFO("Disabling user_xattr feature because "
353                                       "it is not supported on the server\n");
354                         sbi->ll_flags &= ~LL_SBI_USER_XATTR;
355                 }
356                 RETURN(rc);
357         }
358
359         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
360         LASSERT(body);
361
362         /* only detect the xattr size */
363         if (size == 0)
364                 GOTO(out, rc = body->eadatasize);
365
366         if (size < body->eadatasize) {
367                 CERROR("server bug: replied size %u > %u\n",
368                        body->eadatasize, (int)size);
369                 GOTO(out, rc = -ERANGE);
370         }
371
372         if (body->eadatasize == 0)
373                 GOTO(out, rc = -ENODATA);
374
375         /* do not need swab xattr data */
376         xdata = req_capsule_server_sized_get(&req->rq_pill, &RMF_EADATA,
377                                              body->eadatasize);
378         if (!xdata)
379                 GOTO(out, rc = -EFAULT);
380
381 #ifdef CONFIG_FS_POSIX_ACL
382         if (body->eadatasize >= 0 && rce && rce->rce_ops == RMT_LSETFACL) {
383                 ext_acl_xattr_header *acl;
384
385                 acl = lustre_posix_acl_xattr_2ext((posix_acl_xattr_header *)xdata,
386                                                   body->eadatasize);
387                 if (IS_ERR(acl))
388                         GOTO(out, rc = PTR_ERR(acl));
389
390                 rc = ee_add(&sbi->ll_et, cfs_curproc_pid(), ll_inode2fid(inode),
391                             xattr_type, acl);
392                 if (unlikely(rc < 0)) {
393                         lustre_ext_acl_xattr_free(acl);
394                         GOTO(out, rc);
395                 }
396         }
397 #endif
398
399         if (body->eadatasize == 0) {
400                 rc = -ENODATA;
401         } else {
402                 LASSERT(buffer);
403                 memcpy(buffer, xdata, body->eadatasize);
404                 rc = body->eadatasize;
405         }
406         EXIT;
407 out:
408         ptlrpc_req_finished(req);
409         return rc;
410 }
411
412 ssize_t ll_getxattr(struct dentry *dentry, const char *name,
413                     void *buffer, size_t size)
414 {
415         struct inode *inode = dentry->d_inode;
416
417         LASSERT(inode);
418         LASSERT(name);
419
420         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), xattr %s\n",
421                inode->i_ino, inode->i_generation, inode, name);
422
423         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_GETXATTR, 1);
424
425         if ((strncmp(name, XATTR_TRUSTED_PREFIX,
426                      sizeof(XATTR_TRUSTED_PREFIX) - 1) == 0 &&
427              strcmp(name + sizeof(XATTR_TRUSTED_PREFIX) - 1, "lov") == 0) ||
428             (strncmp(name, XATTR_LUSTRE_PREFIX,
429                      sizeof(XATTR_LUSTRE_PREFIX) - 1) == 0 &&
430              strcmp(name + sizeof(XATTR_LUSTRE_PREFIX) - 1, "lov") == 0)) {
431                 struct lov_user_md *lump;
432                 struct lov_mds_md *lmm = NULL;
433                 struct ptlrpc_request *request = NULL;
434                 int rc = 0, lmmsize = 0;
435
436                 if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
437                         return -ENODATA;
438
439                 if (size == 0 && S_ISDIR(inode->i_mode)) {
440                         /* XXX directory EA is fix for now, optimize to save
441                          * RPC transfer */
442                         GOTO(out, rc = sizeof(struct lov_user_md));
443                 }
444
445                 if (!ll_i2info(inode)->lli_smd) {
446                         if (S_ISDIR(inode->i_mode)) {
447                                 rc = ll_dir_getstripe(inode, &lmm,
448                                                       &lmmsize, &request);
449                         } else {
450                                 rc = -ENODATA;
451                         }
452                 } else {
453                         /* LSM is present already after lookup/getattr call.
454                          * we need to grab layout lock once it is implemented */
455                         rc = obd_packmd(ll_i2dtexp(inode), &lmm,
456                                         ll_i2info(inode)->lli_smd);
457                         lmmsize = rc;
458                 }
459
460                 if (rc < 0)
461                        GOTO(out, rc);
462
463                 if (size == 0) {
464                         /* used to call ll_get_max_mdsize() forward to get
465                          * the maximum buffer size, while some apps (such as
466                          * rsync 3.0.x) care much about the exact xattr value
467                          * size */
468                         rc = lmmsize;
469                         GOTO(out, rc);
470                 }
471
472                 if (size < lmmsize) {
473                         CERROR("server bug: replied size %d > %d for %s (%s)\n",
474                                lmmsize, (int)size, dentry->d_name.name, name);
475                         GOTO(out, rc = -ERANGE);
476                 }
477
478                 lump = (struct lov_user_md *)buffer;
479                 memcpy(lump, lmm, lmmsize);
480
481                 rc = lmmsize;
482 out:
483                 if (request)
484                         ptlrpc_req_finished(request);
485                 else if (lmm)
486                         obd_free_diskmd(ll_i2dtexp(inode), &lmm);
487                 return(rc);
488         }
489
490         return ll_getxattr_common(inode, name, buffer, size, OBD_MD_FLXATTR);
491 }
492
493 ssize_t ll_listxattr(struct dentry *dentry, char *buffer, size_t size)
494 {
495         struct inode *inode = dentry->d_inode;
496         int rc = 0, rc2 = 0;
497         struct lov_mds_md *lmm = NULL;
498         struct ptlrpc_request *request = NULL;
499         int lmmsize;
500
501         LASSERT(inode);
502
503         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n",
504                inode->i_ino, inode->i_generation, inode);
505
506         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_LISTXATTR, 1);
507
508         rc = ll_getxattr_common(inode, NULL, buffer, size, OBD_MD_FLXATTRLS);
509         if (rc < 0)
510                 GOTO(out, rc);
511
512         if (S_ISREG(inode->i_mode)) {
513                 if (ll_i2info(inode)->lli_smd == NULL)
514                         rc2 = -1;
515         } else if (S_ISDIR(inode->i_mode)) {
516                 rc2 = ll_dir_getstripe(inode, &lmm, &lmmsize, &request);
517         }
518
519         if (rc2 < 0) {
520                 GOTO(out, rc2 = 0);
521         } else if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)) {
522                 const int prefix_len = sizeof(XATTR_LUSTRE_PREFIX) - 1;
523                 const size_t name_len   = sizeof("lov") - 1;
524                 const size_t total_len  = prefix_len + name_len + 1;
525
526                 if (buffer && (rc + total_len) <= size) {
527                         buffer += rc;
528                         memcpy(buffer,XATTR_LUSTRE_PREFIX, prefix_len);
529                         memcpy(buffer+prefix_len, "lov", name_len);
530                         buffer[prefix_len + name_len] = '\0';
531                 }
532                 rc2 = total_len;
533         }
534 out:
535         ptlrpc_req_finished(request);
536         rc = rc + rc2;
537
538         return rc;
539 }