Whamcloud - gitweb
b=24037 avoid conflicting DCACHE_LUSTRE_INVALID
[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 /*
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/smp_lock.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_mdc.h>
49 #include <lustre_acl.h>
50
51 #include "llite_internal.h"
52
53 #define XATTR_USER_T            (1)
54 #define XATTR_TRUSTED_T         (2)
55 #define XATTR_SECURITY_T        (3)
56 #define XATTR_ACL_ACCESS_T      (4)
57 #define XATTR_ACL_DEFAULT_T     (5)
58 #define XATTR_LUSTRE_T          (6)
59 #define XATTR_OTHER_T           (7)
60
61 static
62 int get_xattr_type(const char *name)
63 {
64         if (!strcmp(name, POSIX_ACL_XATTR_ACCESS))
65                 return XATTR_ACL_ACCESS_T;
66
67         if (!strcmp(name, POSIX_ACL_XATTR_DEFAULT))
68                 return XATTR_ACL_DEFAULT_T;
69
70         if (!strncmp(name, XATTR_USER_PREFIX,
71                      sizeof(XATTR_USER_PREFIX) - 1))
72                 return XATTR_USER_T;
73
74         if (!strncmp(name, XATTR_TRUSTED_PREFIX,
75                      sizeof(XATTR_TRUSTED_PREFIX) - 1))
76                 return XATTR_TRUSTED_T;
77
78         if (!strncmp(name, XATTR_SECURITY_PREFIX,
79                      sizeof(XATTR_SECURITY_PREFIX) - 1))
80                 return XATTR_SECURITY_T;
81
82         if (!strncmp(name, XATTR_LUSTRE_PREFIX,
83                      sizeof(XATTR_LUSTRE_PREFIX) - 1))
84                 return XATTR_LUSTRE_T;
85
86         return XATTR_OTHER_T;
87 }
88
89 static
90 int xattr_type_filter(struct ll_sb_info *sbi, int xattr_type)
91 {
92         if ((xattr_type == XATTR_ACL_ACCESS_T ||
93              xattr_type == XATTR_ACL_DEFAULT_T) &&
94            !(sbi->ll_flags & LL_SBI_ACL))
95                 return -EOPNOTSUPP;
96
97         if (xattr_type == XATTR_USER_T && !(sbi->ll_flags & LL_SBI_USER_XATTR))
98                 return -EOPNOTSUPP;
99         if (xattr_type == XATTR_TRUSTED_T && !cfs_capable(CFS_CAP_SYS_ADMIN))
100                 return -EPERM;
101         if (xattr_type == XATTR_OTHER_T)
102                 return -EOPNOTSUPP;
103
104         return 0;
105 }
106
107 static
108 int ll_setxattr_common(struct inode *inode, const char *name,
109                        const void *value, size_t size,
110                        int flags, __u64 valid)
111 {
112         struct ll_sb_info *sbi = ll_i2sbi(inode);
113         struct ptlrpc_request *req;
114         int xattr_type, rc;
115         struct obd_capa *oc;
116         posix_acl_xattr_header *new_value = NULL;
117         struct rmtacl_ctl_entry *rce = NULL;
118         ext_acl_xattr_header *acl = NULL;
119         const char *pv = value;
120         ENTRY;
121
122         xattr_type = get_xattr_type(name);
123         rc = xattr_type_filter(sbi, xattr_type);
124         if (rc)
125                 RETURN(rc);
126
127         /* b10667: ignore lustre special xattr for now */
128         if ((xattr_type == XATTR_TRUSTED_T && strcmp(name, "trusted.lov") == 0) ||
129             (xattr_type == XATTR_LUSTRE_T && strcmp(name, "lustre.lov") == 0))
130                 RETURN(0);
131
132         /* b15587: ignore security.capability xattr for now */
133         if ((xattr_type == XATTR_SECURITY_T &&
134             strcmp(name, "security.capability") == 0))
135                 RETURN(0);
136
137 #ifdef CONFIG_FS_POSIX_ACL
138         if (sbi->ll_flags & LL_SBI_RMT_CLIENT &&
139             (xattr_type == XATTR_ACL_ACCESS_T ||
140             xattr_type == XATTR_ACL_DEFAULT_T)) {
141                 rce = rct_search(&sbi->ll_rct, cfs_curproc_pid());
142                 if (rce == NULL ||
143                     (rce->rce_ops != RMT_LSETFACL &&
144                     rce->rce_ops != RMT_RSETFACL))
145                         RETURN(-EOPNOTSUPP);
146
147                 if (rce->rce_ops == RMT_LSETFACL) {
148                         struct eacl_entry *ee;
149
150                         ee = et_search_del(&sbi->ll_et, cfs_curproc_pid(),
151                                            ll_inode2fid(inode), xattr_type);
152                         LASSERT(ee != NULL);
153                         if (valid & OBD_MD_FLXATTR) {
154                                 acl = lustre_acl_xattr_merge2ext(
155                                                 (posix_acl_xattr_header *)value,
156                                                 size, ee->ee_acl);
157                                 if (IS_ERR(acl)) {
158                                         ee_free(ee);
159                                         RETURN(PTR_ERR(acl));
160                                 }
161                                 size =  CFS_ACL_XATTR_SIZE(\
162                                                 le32_to_cpu(acl->a_count), \
163                                                 ext_acl_xattr);
164                                 pv = (const char *)acl;
165                         }
166                         ee_free(ee);
167                 } else if (rce->rce_ops == RMT_RSETFACL) {
168                         size = lustre_posix_acl_xattr_filter(
169                                                 (posix_acl_xattr_header *)value,
170                                                 size, &new_value);
171                         if (unlikely(size < 0))
172                                 RETURN(size);
173
174                         pv = (const char *)new_value;
175                 } else
176                         RETURN(-EOPNOTSUPP);
177
178                 valid |= rce_ops2valid(rce->rce_ops);
179         }
180 #endif
181         oc = ll_mdscapa_get(inode);
182         rc = md_setxattr(sbi->ll_md_exp, ll_inode2fid(inode), oc,
183                          valid, name, pv, size, 0, flags, ll_i2suppgid(inode),
184                          &req);
185         capa_put(oc);
186 #ifdef CONFIG_FS_POSIX_ACL
187         if (new_value != NULL)
188                 lustre_posix_acl_xattr_free(new_value, size);
189         if (acl != NULL)
190                 lustre_ext_acl_xattr_free(acl);
191 #endif
192         if (rc) {
193                 if (rc == -EOPNOTSUPP && xattr_type == XATTR_USER_T) {
194                         LCONSOLE_INFO("Disabling user_xattr feature because "
195                                       "it is not supported on the server\n");
196                         sbi->ll_flags &= ~LL_SBI_USER_XATTR;
197                 }
198                 RETURN(rc);
199         }
200
201         ptlrpc_req_finished(req);
202         RETURN(0);
203 }
204
205 int ll_setxattr(struct dentry *dentry, const char *name,
206                 const void *value, size_t size, int flags)
207 {
208         struct inode *inode = dentry->d_inode;
209
210         LASSERT(inode);
211         LASSERT(name);
212
213         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), xattr %s\n",
214                inode->i_ino, inode->i_generation, inode, name);
215
216         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_SETXATTR, 1);
217
218         if ((strncmp(name, XATTR_TRUSTED_PREFIX,
219                      sizeof(XATTR_TRUSTED_PREFIX) - 1) == 0 &&
220              strcmp(name + sizeof(XATTR_TRUSTED_PREFIX) - 1, "lov") == 0) ||
221             (strncmp(name, XATTR_LUSTRE_PREFIX,
222                      sizeof(XATTR_LUSTRE_PREFIX) - 1) == 0 &&
223              strcmp(name + sizeof(XATTR_LUSTRE_PREFIX) - 1, "lov") == 0)) {
224                 struct lov_user_md *lump = (struct lov_user_md *)value;
225                 int rc = 0;
226
227                 /* Attributes that are saved via getxattr will always have
228                  * the stripe_offset as 0.  Instead, the MDS should be
229                  * allowed to pick the starting OST index.   b=17846 */
230                 if (lump != NULL && lump->lmm_stripe_offset == 0)
231                         lump->lmm_stripe_offset = -1;
232
233                 if (lump != NULL && S_ISREG(inode->i_mode)) {
234                         struct file f;
235                         int flags = FMODE_WRITE;
236
237                         f.f_dentry = dentry;
238                         rc = ll_lov_setstripe_ea_info(inode, &f, flags,
239                                                       lump, sizeof(*lump));
240                         /* b10667: rc always be 0 here for now */
241                         rc = 0;
242                 } else if (S_ISDIR(inode->i_mode)) {
243                         rc = ll_dir_setstripe(inode, lump, 0);
244                 }
245
246                 return rc;
247
248         } else if (strcmp(name, XATTR_NAME_LMA) == 0 ||
249                    strcmp(name, XATTR_NAME_LINK) == 0)
250                 return 0;
251
252         return ll_setxattr_common(inode, name, value, size, flags,
253                                   OBD_MD_FLXATTR);
254 }
255
256 int ll_removexattr(struct dentry *dentry, const char *name)
257 {
258         struct inode *inode = dentry->d_inode;
259
260         LASSERT(inode);
261         LASSERT(name);
262
263         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), xattr %s\n",
264                inode->i_ino, inode->i_generation, inode, name);
265
266         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_REMOVEXATTR, 1);
267         return ll_setxattr_common(inode, name, NULL, 0, 0,
268                                   OBD_MD_FLXATTRRM);
269 }
270
271 static
272 int ll_getxattr_common(struct inode *inode, const char *name,
273                        void *buffer, size_t size, __u64 valid)
274 {
275         struct ll_sb_info *sbi = ll_i2sbi(inode);
276         struct ptlrpc_request *req = NULL;
277         struct mdt_body *body;
278         int xattr_type, rc;
279         void *xdata;
280         struct obd_capa *oc;
281         struct rmtacl_ctl_entry *rce = NULL;
282         ENTRY;
283
284         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n",
285                inode->i_ino, inode->i_generation, inode);
286
287         /* listxattr have slightly different behavior from of ext3:
288          * without 'user_xattr' ext3 will list all xattr names but
289          * filtered out "^user..*"; we list them all for simplicity.
290          */
291         if (!name) {
292                 xattr_type = XATTR_OTHER_T;
293                 goto do_getxattr;
294         }
295
296         xattr_type = get_xattr_type(name);
297         rc = xattr_type_filter(sbi, xattr_type);
298         if (rc)
299                 RETURN(rc);
300
301         /* b15587: ignore security.capability xattr for now */
302         if ((xattr_type == XATTR_SECURITY_T &&
303             strcmp(name, "security.capability") == 0))
304                 RETURN(-ENODATA);
305
306 #ifdef CONFIG_FS_POSIX_ACL
307         if (sbi->ll_flags & LL_SBI_RMT_CLIENT &&
308             (xattr_type == XATTR_ACL_ACCESS_T ||
309             xattr_type == XATTR_ACL_DEFAULT_T)) {
310                 rce = rct_search(&sbi->ll_rct, cfs_curproc_pid());
311                 if (rce == NULL ||
312                     (rce->rce_ops != RMT_LSETFACL &&
313                     rce->rce_ops != RMT_LGETFACL &&
314                     rce->rce_ops != RMT_RSETFACL &&
315                     rce->rce_ops != RMT_RGETFACL))
316                         RETURN(-EOPNOTSUPP);
317         }
318
319         /* posix acl is under protection of LOOKUP lock. when calling to this,
320          * we just have path resolution to the target inode, so we have great
321          * chance that cached ACL is uptodate.
322          */
323         if (xattr_type == XATTR_ACL_ACCESS_T &&
324             !(sbi->ll_flags & LL_SBI_RMT_CLIENT)) {
325                 struct ll_inode_info *lli = ll_i2info(inode);
326                 struct posix_acl *acl;
327
328                 cfs_spin_lock(&lli->lli_lock);
329                 acl = posix_acl_dup(lli->lli_posix_acl);
330                 cfs_spin_unlock(&lli->lli_lock);
331
332                 if (!acl)
333                         RETURN(-ENODATA);
334
335                 rc = posix_acl_to_xattr(acl, buffer, size);
336                 posix_acl_release(acl);
337                 RETURN(rc);
338         }
339         if (xattr_type == XATTR_ACL_DEFAULT_T && !S_ISDIR(inode->i_mode))
340                 RETURN(-ENODATA);
341 #endif
342
343 do_getxattr:
344         oc = ll_mdscapa_get(inode);
345         rc = md_getxattr(sbi->ll_md_exp, ll_inode2fid(inode), oc,
346                          valid | (rce ? rce_ops2valid(rce->rce_ops) : 0),
347                          name, NULL, 0, size, 0, &req);
348         capa_put(oc);
349         if (rc) {
350                 if (rc == -EOPNOTSUPP && xattr_type == XATTR_USER_T) {
351                         LCONSOLE_INFO("Disabling user_xattr feature because "
352                                       "it is not supported on the server\n");
353                         sbi->ll_flags &= ~LL_SBI_USER_XATTR;
354                 }
355                 RETURN(rc);
356         }
357
358         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
359         LASSERT(body);
360
361         /* only detect the xattr size */
362         if (size == 0)
363                 GOTO(out, rc = body->eadatasize);
364
365         if (size < body->eadatasize) {
366                 CERROR("server bug: replied size %u > %u\n",
367                        body->eadatasize, (int)size);
368                 GOTO(out, rc = -ERANGE);
369         }
370
371         if (body->eadatasize == 0)
372                 GOTO(out, rc = -ENODATA);
373
374         /* do not need swab xattr data */
375         xdata = req_capsule_server_sized_get(&req->rq_pill, &RMF_EADATA,
376                                              body->eadatasize);
377         if (!xdata)
378                 GOTO(out, rc = -EFAULT);
379
380 #ifdef CONFIG_FS_POSIX_ACL
381         if (body->eadatasize >= 0 && rce && rce->rce_ops == RMT_LSETFACL) {
382                 ext_acl_xattr_header *acl;
383
384                 acl = lustre_posix_acl_xattr_2ext((posix_acl_xattr_header *)xdata,
385                                                   body->eadatasize);
386                 if (IS_ERR(acl))
387                         GOTO(out, rc = PTR_ERR(acl));
388
389                 rc = ee_add(&sbi->ll_et, cfs_curproc_pid(), ll_inode2fid(inode),
390                             xattr_type, acl);
391                 if (unlikely(rc < 0)) {
392                         lustre_ext_acl_xattr_free(acl);
393                         GOTO(out, rc);
394                 }
395         }
396 #endif
397
398         if (body->eadatasize == 0) {
399                 rc = -ENODATA;
400         } else {
401                 LASSERT(buffer);
402                 memcpy(buffer, xdata, body->eadatasize);
403                 rc = body->eadatasize;
404         }
405         EXIT;
406 out:
407         ptlrpc_req_finished(req);
408         return rc;
409 }
410
411 ssize_t ll_getxattr(struct dentry *dentry, const char *name,
412                     void *buffer, size_t size)
413 {
414         struct inode *inode = dentry->d_inode;
415
416         LASSERT(inode);
417         LASSERT(name);
418
419         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), xattr %s\n",
420                inode->i_ino, inode->i_generation, inode, name);
421
422         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_GETXATTR, 1);
423
424         if ((strncmp(name, XATTR_TRUSTED_PREFIX,
425                      sizeof(XATTR_TRUSTED_PREFIX) - 1) == 0 &&
426              strcmp(name + sizeof(XATTR_TRUSTED_PREFIX) - 1, "lov") == 0) ||
427             (strncmp(name, XATTR_LUSTRE_PREFIX,
428                      sizeof(XATTR_LUSTRE_PREFIX) - 1) == 0 &&
429              strcmp(name + sizeof(XATTR_LUSTRE_PREFIX) - 1, "lov") == 0)) {
430                 struct lov_user_md *lump;
431                 struct lov_mds_md *lmm = NULL;
432                 struct ptlrpc_request *request = NULL;
433                 int rc = 0, lmmsize = 0;
434
435                 if (S_ISREG(inode->i_mode)) {
436                         rc = ll_lov_getstripe_ea_info(dentry->d_parent->d_inode,
437                                                       dentry->d_name.name, &lmm,
438                                                       &lmmsize, &request);
439                 } else if (S_ISDIR(inode->i_mode)) {
440                         rc = ll_dir_getstripe(inode, &lmm, &lmmsize, &request);
441                 } else {
442                         rc = -ENODATA;
443                 }
444
445                 if (rc < 0)
446                        GOTO(out, rc);
447                 if (size == 0)
448                        GOTO(out, rc = lmmsize);
449
450                 if (size < lmmsize) {
451                         CERROR("server bug: replied size %d > %d for %s (%s)\n",
452                                lmmsize, (int)size, dentry->d_name.name, name);
453                         GOTO(out, rc = -ERANGE);
454                 }
455
456                 lump = (struct lov_user_md *)buffer;
457                 memcpy(lump, lmm, lmmsize);
458
459                 rc = lmmsize;
460 out:
461                 ptlrpc_req_finished(request);
462                 return(rc);
463         }
464
465         return ll_getxattr_common(inode, name, buffer, size, OBD_MD_FLXATTR);
466 }
467
468 ssize_t ll_listxattr(struct dentry *dentry, char *buffer, size_t size)
469 {
470         struct inode *inode = dentry->d_inode;
471         int rc = 0, rc2 = 0;
472         struct lov_mds_md *lmm = NULL;
473         struct ptlrpc_request *request = NULL;
474         int lmmsize;
475
476         LASSERT(inode);
477
478         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n",
479                inode->i_ino, inode->i_generation, inode);
480
481         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_LISTXATTR, 1);
482
483         rc = ll_getxattr_common(inode, NULL, buffer, size, OBD_MD_FLXATTRLS);
484         if (rc < 0)
485                 GOTO(out, rc);
486
487         if (S_ISREG(inode->i_mode)) {
488                 struct ll_inode_info *lli = ll_i2info(inode);
489                 struct lov_stripe_md *lsm = NULL;
490                 lsm = lli->lli_smd;
491                 if (lsm == NULL)
492                         rc2 = -1;
493         } else if (S_ISDIR(inode->i_mode)) {
494                 rc2 = ll_dir_getstripe(inode, &lmm, &lmmsize, &request);
495         }
496
497         if (rc2 < 0) {
498                 GOTO(out, rc2 = 0);
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
504                 if (buffer && (rc + total_len) <= size) {
505                         buffer += rc;
506                         memcpy(buffer,XATTR_LUSTRE_PREFIX, prefix_len);
507                         memcpy(buffer+prefix_len, "lov", name_len);
508                         buffer[prefix_len + name_len] = '\0';
509                 }
510                 rc2 = total_len;
511         }
512 out:
513         ptlrpc_req_finished(request);
514         rc = rc + rc2;
515
516         return rc;
517 }