Whamcloud - gitweb
add changelog section for 1.8.3
[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 <linux/lustre_version.h>
48
49 #ifndef POSIX_ACL_XATTR_ACCESS
50 #ifndef XATTR_NAME_ACL_ACCESS
51 #define XATTR_NAME_ACL_ACCESS   "system.posix_acl_access"
52 #endif
53 #define POSIX_ACL_XATTR_ACCESS XATTR_NAME_ACL_ACCESS
54 #endif
55 #ifndef POSIX_ACL_XATTR_DEFAULT
56 #ifndef XATTR_NAME_ACL_DEFAULT
57 #define XATTR_NAME_ACL_DEFAULT  "system.posix_acl_default"
58 #endif
59 #define POSIX_ACL_XATTR_DEFAULT XATTR_NAME_ACL_DEFAULT
60 #endif
61
62 #include "llite_internal.h"
63
64 #define XATTR_USER_PREFIX       "user."
65 #define XATTR_TRUSTED_PREFIX    "trusted."
66 #define XATTR_SECURITY_PREFIX   "security."
67 #define XATTR_LUSTRE_PREFIX     "lustre."
68
69 #define XATTR_USER_T            (1)
70 #define XATTR_TRUSTED_T         (2)
71 #define XATTR_SECURITY_T        (3)
72 #define XATTR_ACL_ACCESS_T      (4)
73 #define XATTR_ACL_DEFAULT_T     (5)
74 #define XATTR_LUSTRE_T          (6)
75 #define XATTR_OTHER_T           (7)
76
77 static
78 int get_xattr_type(const char *name)
79 {
80         if (!strcmp(name, POSIX_ACL_XATTR_ACCESS))
81                 return XATTR_ACL_ACCESS_T;
82
83         if (!strcmp(name, POSIX_ACL_XATTR_DEFAULT))
84                 return XATTR_ACL_DEFAULT_T;
85
86         if (!strncmp(name, XATTR_USER_PREFIX,
87                      sizeof(XATTR_USER_PREFIX) - 1))
88                 return XATTR_USER_T;
89
90         if (!strncmp(name, XATTR_TRUSTED_PREFIX,
91                      sizeof(XATTR_TRUSTED_PREFIX) - 1))
92                 return XATTR_TRUSTED_T;
93
94         if (!strncmp(name, XATTR_SECURITY_PREFIX,
95                      sizeof(XATTR_SECURITY_PREFIX) - 1))
96                 return XATTR_SECURITY_T;
97
98         if (!strncmp(name, XATTR_LUSTRE_PREFIX,
99                      sizeof(XATTR_LUSTRE_PREFIX) - 1))
100                 return XATTR_LUSTRE_T;
101
102         return XATTR_OTHER_T;
103 }
104
105 static
106 int xattr_type_filter(struct ll_sb_info *sbi, int xattr_type)
107 {
108         if ((xattr_type == XATTR_ACL_ACCESS_T ||
109              xattr_type == XATTR_ACL_DEFAULT_T) &&
110             !(sbi->ll_flags & LL_SBI_ACL))
111                 return -EOPNOTSUPP;
112
113         if (xattr_type == XATTR_USER_T && !(sbi->ll_flags & LL_SBI_USER_XATTR))
114                 return -EOPNOTSUPP;
115         if (xattr_type == XATTR_TRUSTED_T && !cfs_capable(CFS_CAP_SYS_ADMIN))
116                 return -EPERM;
117         if (xattr_type == XATTR_OTHER_T)
118                 return -EOPNOTSUPP;
119
120         return 0;
121 }
122
123 static
124 int ll_setxattr_common(struct inode *inode, const char *name,
125                        const void *value, size_t size,
126                        int flags, __u64 valid)
127 {
128         struct ll_sb_info *sbi = ll_i2sbi(inode);
129         struct ptlrpc_request *req;
130         struct ll_fid fid;
131         int xattr_type, rc;
132         ENTRY;
133
134
135         xattr_type = get_xattr_type(name);
136         rc = xattr_type_filter(sbi, xattr_type);
137         if (rc)
138                 RETURN(rc);
139
140         /* b10667: ignore lustre special xattr for now */
141         if ((xattr_type == XATTR_TRUSTED_T && strcmp(name, "trusted.lov") == 0) ||
142             (xattr_type == XATTR_LUSTRE_T && strcmp(name, "lustre.lov") == 0))
143                 RETURN(0);
144
145         ll_inode2fid(&fid, inode);
146         rc = mdc_setxattr(sbi->ll_mdc_exp, &fid, valid,
147                           name, value, size, 0, flags, &req);
148         if (rc) {
149                 if (rc == -EOPNOTSUPP && xattr_type == XATTR_USER_T) {
150                         LCONSOLE_INFO("Disabling user_xattr feature because "
151                                       "it is not supported on the server\n");
152                         sbi->ll_flags &= ~LL_SBI_USER_XATTR;
153                 }
154                 RETURN(rc);
155         }
156
157         ptlrpc_req_finished(req);
158         RETURN(0);
159 }
160
161 int ll_setxattr(struct dentry *dentry, const char *name,
162                 const void *value, size_t size, int flags)
163 {
164         struct inode *inode = dentry->d_inode;
165
166         LASSERT(inode);
167         LASSERT(name);
168
169         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), xattr %s\n",
170                inode->i_ino, inode->i_generation, inode, name);
171
172         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_SETXATTR, 1);
173
174         if ((strncmp(name, XATTR_TRUSTED_PREFIX,
175                     sizeof(XATTR_TRUSTED_PREFIX) - 1) == 0 &&
176              strcmp(name + sizeof(XATTR_TRUSTED_PREFIX) - 1, "lov") == 0) ||
177             (strncmp(name, XATTR_LUSTRE_PREFIX,
178                     sizeof(XATTR_LUSTRE_PREFIX) - 1) == 0 &&
179              strcmp(name + sizeof(XATTR_LUSTRE_PREFIX) - 1, "lov") == 0)) {
180                 struct lov_user_md *lump = (struct lov_user_md *)value;
181                 int rc = 0;
182
183                 /* Attributes that are saved via getxattr will always have
184                  * the stripe_offset as 0.  Instead, the MDS should be
185                  * allowed to pick the starting OST index.   b=17846 */
186                 if (lump->lmm_stripe_offset == 0)
187                         lump->lmm_stripe_offset = -1;
188
189                 if (S_ISREG(inode->i_mode)) {
190                         struct file f;
191                         int flags = FMODE_WRITE;
192
193                         f.f_dentry = dentry;
194                         rc = ll_lov_setstripe_ea_info(inode, &f, flags,
195                                                       lump, sizeof(*lump));
196                         /* b10667: rc always be 0 here for now */
197                         rc = 0;
198                 } else if (S_ISDIR(inode->i_mode)) {
199                         rc = ll_dir_setstripe(inode, lump, 0);
200                 }
201
202                 return rc;
203         } else if (strcmp(name, "trusted.lma") == 0 &&
204                    !OBD_FAIL_CHECK(OBD_FAIL_MDS_ALLOW_COMMON_EA_SETTING))
205                 return 0;
206
207
208         return ll_setxattr_common(inode, name, value, size, flags,
209                                   OBD_MD_FLXATTR);
210 }
211
212 int ll_removexattr(struct dentry *dentry, const char *name)
213 {
214         struct inode *inode = dentry->d_inode;
215
216         LASSERT(inode);
217         LASSERT(name);
218
219         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), xattr %s\n",
220                inode->i_ino, inode->i_generation, inode, name);
221
222         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_REMOVEXATTR, 1);
223         return ll_setxattr_common(inode, name, NULL, 0, 0,
224                                   OBD_MD_FLXATTRRM);
225 }
226
227 static
228 int ll_getxattr_common(struct inode *inode, const char *name,
229                        void *buffer, size_t size, __u64 valid)
230 {
231         struct ll_sb_info *sbi = ll_i2sbi(inode);
232         struct ptlrpc_request *req = NULL;
233         struct mds_body *body;
234         struct ll_fid fid;
235         void *xdata;
236         int xattr_type, rc;
237         ENTRY;
238
239         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n",
240                inode->i_ino, inode->i_generation, inode);
241
242
243         /* listxattr have slightly different behavior from of ext3:
244          * without 'user_xattr' ext3 will list all xattr names but
245          * filtered out "^user..*"; we list them all for simplicity.
246          */
247         if (!name) {
248                 xattr_type = XATTR_OTHER_T;
249                 goto do_getxattr;
250         }
251
252         xattr_type = get_xattr_type(name);
253         rc = xattr_type_filter(sbi, xattr_type);
254         if (rc)
255                 RETURN(rc);
256
257         /* posix acl is under protection of LOOKUP lock. when calling to this,
258          * we just have path resolution to the target inode, so we have great
259          * chance that cached ACL is uptodate.
260          */
261 #ifdef CONFIG_FS_POSIX_ACL
262         if (xattr_type == XATTR_ACL_ACCESS_T) {
263                 struct ll_inode_info *lli = ll_i2info(inode);
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)
271                         RETURN(-ENODATA);
272
273                 rc = posix_acl_to_xattr(acl, buffer, size);
274                 posix_acl_release(acl);
275                 RETURN(rc);
276         }
277         if (xattr_type == XATTR_ACL_DEFAULT_T && !S_ISDIR(inode->i_mode))
278                 RETURN(-ENODATA);
279 #endif
280
281 do_getxattr:
282         ll_inode2fid(&fid, inode);
283         rc = mdc_getxattr(sbi->ll_mdc_exp, &fid, valid, name, NULL, 0, size,
284                           &req);
285         if (rc) {
286                 if (rc == -EOPNOTSUPP && xattr_type == XATTR_USER_T) {
287                         LCONSOLE_INFO("Disabling user_xattr feature because "
288                                       "it is not supported on the server\n");
289                         sbi->ll_flags &= ~LL_SBI_USER_XATTR;
290                 }
291                 RETURN(rc);
292         }
293
294         body = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF, sizeof(*body));
295         LASSERT(body);
296         LASSERT(lustre_rep_swabbed(req, REPLY_REC_OFF));
297
298         /* only detect the xattr size */
299         if (size == 0)
300                 GOTO(out, rc = body->eadatasize);
301
302         if (size < body->eadatasize) {
303                 CERROR("server bug: replied size %u > %u\n",
304                        body->eadatasize, (int)size);
305                 GOTO(out, rc = -ERANGE);
306         }
307
308         if (lustre_msg_bufcount(req->rq_repmsg) < 3) {
309                 CERROR("reply bufcount %u\n",
310                        lustre_msg_bufcount(req->rq_repmsg));
311                 GOTO(out, rc = -EFAULT);
312         }
313
314         /* do not need swab xattr data */
315         lustre_set_rep_swabbed(req, REPLY_REC_OFF + 1);
316         xdata = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF + 1,
317                                body->eadatasize);
318         if (!xdata) {
319                 CERROR("can't extract: %u : %u\n", body->eadatasize,
320                        lustre_msg_buflen(req->rq_repmsg, REPLY_REC_OFF + 1));
321                 GOTO(out, rc = -EFAULT);
322         }
323
324         LASSERT(buffer);
325         memcpy(buffer, xdata, body->eadatasize);
326         rc = body->eadatasize;
327 out:
328         ptlrpc_req_finished(req);
329         RETURN(rc);
330 }
331
332 ssize_t ll_getxattr(struct dentry *dentry, const char *name,
333                     void *buffer, size_t size)
334 {
335         struct inode *inode = dentry->d_inode;
336
337         LASSERT(inode);
338         LASSERT(name);
339
340         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), xattr %s\n",
341                inode->i_ino, inode->i_generation, inode, name);
342
343         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_GETXATTR, 1);
344
345         if ((strncmp(name, XATTR_TRUSTED_PREFIX,
346                     sizeof(XATTR_TRUSTED_PREFIX) - 1) == 0 &&
347              strcmp(name + sizeof(XATTR_TRUSTED_PREFIX) - 1, "lov") == 0) ||
348             (strncmp(name, XATTR_LUSTRE_PREFIX,
349                     sizeof(XATTR_LUSTRE_PREFIX) - 1) == 0 &&
350              strcmp(name + sizeof(XATTR_LUSTRE_PREFIX) - 1, "lov") == 0)) {
351                 struct lov_user_md *lump;
352                 struct lov_mds_md *lmm = NULL;
353                 struct ptlrpc_request *request = NULL;
354                 int rc = 0, lmmsize = 0;
355
356                 if (S_ISREG(inode->i_mode)) {
357                         rc = ll_lov_getstripe_ea_info(dentry->d_parent->d_inode,
358                                                       dentry->d_name.name, &lmm,
359                                                       &lmmsize, &request);
360                 } else if (S_ISDIR(inode->i_mode)) {
361                         rc = ll_dir_getstripe(inode, &lmm, &lmmsize, &request);
362                 } else {
363                         rc = -ENODATA;
364                 }
365
366                 if (rc < 0)
367                        GOTO(out, rc);
368                 if (size == 0)
369                        GOTO(out, rc = lmmsize);
370
371                 if (size < lmmsize) {
372                         CERROR("server bug: replied size %d > %d for %s (%s)\n",
373                                lmmsize, (int)size, dentry->d_name.name, name);
374                         GOTO(out, rc = -ERANGE);
375                 }
376
377                 lump = (struct lov_user_md *)buffer;
378                 memcpy(lump, lmm, lmmsize);
379
380                 rc = lmmsize;
381 out:
382                 ptlrpc_req_finished(request);
383                 return(rc);
384         }
385
386         return ll_getxattr_common(inode, name, buffer, size, OBD_MD_FLXATTR);
387 }
388
389 ssize_t ll_listxattr(struct dentry *dentry, char *buffer, size_t size)
390 {
391         struct inode *inode = dentry->d_inode;
392         int rc = 0, rc2 = 0;
393         struct lov_mds_md *lmm = NULL;
394         struct ptlrpc_request *request = NULL;
395         int lmmsize;
396
397         LASSERT(inode);
398
399         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n",
400                inode->i_ino, inode->i_generation, inode);
401
402         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_LISTXATTR, 1);
403
404         rc = ll_getxattr_common(inode, NULL, buffer, size, OBD_MD_FLXATTRLS);
405
406         if (S_ISREG(inode->i_mode)) {
407                 struct ll_inode_info *lli = ll_i2info(inode);
408                 struct lov_stripe_md *lsm = NULL;
409                 lsm = lli->lli_smd;
410                 if (lsm == NULL)
411                         rc2 = -1;
412         } else if (S_ISDIR(inode->i_mode)) {
413                 rc2 = ll_dir_getstripe(inode, &lmm, &lmmsize, &request);
414         }
415
416         if (rc2 < 0) {
417                 GOTO(out, rc2 = 0);
418         } else {
419                 const int prefix_len = sizeof(XATTR_LUSTRE_PREFIX) - 1;
420                 const size_t name_len   = sizeof("lov") - 1;
421                 const size_t total_len  = prefix_len + name_len + 1;
422
423                 if (buffer && (rc + total_len) <= size) {
424                         buffer += rc;
425                         memcpy(buffer,XATTR_LUSTRE_PREFIX, prefix_len);
426                         memcpy(buffer+prefix_len, "lov", name_len);
427                         buffer[prefix_len + name_len] = '\0';
428                 }
429                 rc2 = total_len;
430         }
431 out:
432         ptlrpc_req_finished(request);
433         rc = rc + rc2;
434
435         return rc;
436 }