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