Whamcloud - gitweb
d123ba144504c0193f35854f14b8e5cb891ebf1b
[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  *  Copyright (c) 2004 - 2005 Cluster File Systems, Inc.
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
8  *   Lustre is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Lustre is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Lustre; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21
22 #include <linux/fs.h>
23 #include <linux/sched.h>
24 #include <linux/mm.h>
25 #include <linux/smp_lock.h>
26
27 #define DEBUG_SUBSYSTEM S_LLITE
28
29 #include <obd_support.h>
30 #include <lustre_lite.h>
31 #include <lustre_dlm.h>
32 #include <linux/lustre_version.h>
33
34 #ifndef POSIX_ACL_XATTR_ACCESS
35 #ifndef XATTR_NAME_ACL_ACCESS
36 #define XATTR_NAME_ACL_ACCESS   "system.posix_acl_access"
37 #endif
38 #define POSIX_ACL_XATTR_ACCESS XATTR_NAME_ACL_ACCESS
39 #endif
40 #ifndef POSIX_ACL_XATTR_DEFAULT
41 #ifndef XATTR_NAME_ACL_DEFAULT
42 #define XATTR_NAME_ACL_DEFAULT  "system.posix_acl_default"
43 #endif
44 #define POSIX_ACL_XATTR_DEFAULT XATTR_NAME_ACL_DEFAULT
45 #endif
46
47 #include "llite_internal.h"
48
49 #define XATTR_USER_PREFIX       "user."
50 #define XATTR_TRUSTED_PREFIX    "trusted."
51 #define XATTR_SECURITY_PREFIX   "security."
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_OTHER_T           (6)
59
60 static
61 int get_xattr_type(const char *name)
62 {
63         if (!strcmp(name, POSIX_ACL_XATTR_ACCESS))
64                 return XATTR_ACL_ACCESS_T;
65
66         if (!strcmp(name, POSIX_ACL_XATTR_DEFAULT))
67                 return XATTR_ACL_DEFAULT_T;
68
69         if (!strncmp(name, XATTR_USER_PREFIX,
70                      sizeof(XATTR_USER_PREFIX) - 1))
71                 return XATTR_USER_T;
72
73         if (!strncmp(name, XATTR_TRUSTED_PREFIX,
74                      sizeof(XATTR_TRUSTED_PREFIX) - 1))
75                 return XATTR_TRUSTED_T;
76
77         if (!strncmp(name, XATTR_SECURITY_PREFIX,
78                      sizeof(XATTR_SECURITY_PREFIX) - 1))
79                 return XATTR_SECURITY_T;
80
81         return XATTR_OTHER_T;
82 }
83
84 static
85 int 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;
94         if (xattr_type == XATTR_TRUSTED_T && !capable(CAP_SYS_ADMIN))
95                 return -EPERM;
96         if (xattr_type == XATTR_OTHER_T)
97                 return -EOPNOTSUPP;
98
99         return 0;
100 }
101
102 static
103 int 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);
108         struct ptlrpc_request *req;
109         struct ll_fid fid;
110         int xattr_type, rc;
111         ENTRY;
112
113
114         xattr_type = get_xattr_type(name);
115         rc = xattr_type_filter(sbi, xattr_type);
116         if (rc)
117                 RETURN(rc);
118
119         /* b10667: ignore lustre special xattr for now */
120         if (xattr_type == XATTR_TRUSTED_T && strcmp(name, "trusted.lov") == 0)
121                 RETURN(0);
122
123         ll_inode2fid(&fid, inode);
124         rc = mdc_setxattr(sbi->ll_mdc_exp, &fid, valid,
125                           name, value, size, 0, flags, &req);
126         if (rc) {
127                 if (rc == -EOPNOTSUPP && xattr_type == XATTR_USER_T) {
128                         LCONSOLE_INFO("Disabling user_xattr feature because "
129                                       "it is not supported on the server\n"); 
130                         sbi->ll_flags &= ~LL_SBI_USER_XATTR;
131                 }
132                 RETURN(rc);
133         }
134
135         ptlrpc_req_finished(req);
136         RETURN(0);
137 }
138
139 int ll_setxattr(struct dentry *dentry, const char *name,
140                 const void *value, size_t size, int flags)
141 {
142         struct inode *inode = dentry->d_inode;
143
144         LASSERT(inode);
145         LASSERT(name);
146
147         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), xattr %s\n",
148                inode->i_ino, inode->i_generation, inode, name);
149
150         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_SETXATTR, 1);
151
152         if (strncmp(name, XATTR_TRUSTED_PREFIX, 8) == 0 &&
153             strcmp(name + 8, "lov") == 0) {
154                 struct lov_user_md *lump = (struct lov_user_md *)value;
155                 int rc = 0;
156
157                 if (S_ISREG(inode->i_mode)) {
158                         struct file f;
159                         int flags = FMODE_WRITE;
160                         
161                         f.f_dentry = dentry;
162                         rc = ll_lov_setstripe_ea_info(inode, &f, flags, 
163                                                       lump, sizeof(*lump));
164                         /* b10667: rc always be 0 here for now */
165                         rc = 0;
166                 } else if (S_ISDIR(inode->i_mode)) {
167                         rc = ll_dir_setstripe(inode, lump);
168                 }
169                 
170                 return rc;
171         }
172
173         return ll_setxattr_common(inode, name, value, size, flags,
174                                   OBD_MD_FLXATTR);
175 }
176
177 int ll_removexattr(struct dentry *dentry, const char *name)
178 {
179         struct inode *inode = dentry->d_inode;
180
181         LASSERT(inode);
182         LASSERT(name);
183
184         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), xattr %s\n",
185                inode->i_ino, inode->i_generation, inode, name);
186
187         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_REMOVEXATTR, 1);
188         return ll_setxattr_common(inode, name, NULL, 0, 0,
189                                   OBD_MD_FLXATTRRM);
190 }
191
192 static
193 int ll_getxattr_common(struct inode *inode, const char *name,
194                        void *buffer, size_t size, __u64 valid)
195 {
196         struct ll_sb_info *sbi = ll_i2sbi(inode);
197         struct ptlrpc_request *req = NULL;
198         struct mds_body *body;
199         struct ll_fid fid;
200         void *xdata;
201         int xattr_type, rc;
202         ENTRY;
203
204         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n",
205                inode->i_ino, inode->i_generation, inode);
206
207
208         /* listxattr have slightly different behavior from of ext3:
209          * without 'user_xattr' ext3 will list all xattr names but
210          * filtered out "^user..*"; we list them all for simplicity.
211          */
212         if (!name) {
213                 xattr_type = XATTR_OTHER_T;
214                 goto do_getxattr;
215         }
216
217         xattr_type = get_xattr_type(name);
218         rc = xattr_type_filter(sbi, xattr_type);
219         if (rc)
220                 RETURN(rc);
221
222         /* posix acl is under protection of LOOKUP lock. when calling to this,
223          * we just have path resolution to the target inode, so we have great
224          * chance that cached ACL is uptodate.
225          */
226 #ifdef CONFIG_FS_POSIX_ACL
227         if (xattr_type == XATTR_ACL_ACCESS_T) {
228                 struct ll_inode_info *lli = ll_i2info(inode);
229                 struct posix_acl *acl;
230
231                 spin_lock(&lli->lli_lock);
232                 acl = posix_acl_dup(lli->lli_posix_acl);
233                 spin_unlock(&lli->lli_lock);
234
235                 if (!acl)
236                         RETURN(-ENODATA);
237
238                 rc = posix_acl_to_xattr(acl, buffer, size);
239                 posix_acl_release(acl);
240                 RETURN(rc);
241         }
242 #endif
243
244 do_getxattr:
245         ll_inode2fid(&fid, inode);
246         rc = mdc_getxattr(sbi->ll_mdc_exp, &fid, valid, name, NULL, 0, size,
247                           &req);
248         if (rc) {
249                 if (rc == -EOPNOTSUPP && xattr_type == XATTR_USER_T) {
250                         LCONSOLE_INFO("Disabling user_xattr feature because "
251                                       "it is not supported on the server\n"); 
252                         sbi->ll_flags &= ~LL_SBI_USER_XATTR;
253                 }
254                 RETURN(rc);
255         }
256
257         body = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF, sizeof(*body));
258         LASSERT(body);
259         LASSERT_REPSWABBED(req, REPLY_REC_OFF);
260
261         /* only detect the xattr size */
262         if (size == 0)
263                 GOTO(out, rc = body->eadatasize);
264
265         if (size < body->eadatasize) {
266                 CERROR("server bug: replied size %u > %u\n",
267                        body->eadatasize, (int)size);
268                 GOTO(out, rc = -ERANGE);
269         }
270
271         if (lustre_msg_bufcount(req->rq_repmsg) < 3) {
272                 CERROR("reply bufcount %u\n",
273                        lustre_msg_bufcount(req->rq_repmsg));
274                 GOTO(out, rc = -EFAULT);
275         }
276
277         /* do not need swab xattr data */
278         LASSERT_REPSWAB(req, REPLY_REC_OFF + 1);
279         xdata = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF + 1,
280                                body->eadatasize);
281         if (!xdata) {
282                 CERROR("can't extract: %u : %u\n", body->eadatasize,
283                        lustre_msg_buflen(req->rq_repmsg, REPLY_REC_OFF + 1));
284                 GOTO(out, rc = -EFAULT);
285         }
286
287         LASSERT(buffer);
288         memcpy(buffer, xdata, body->eadatasize);
289         rc = body->eadatasize;
290 out:
291         ptlrpc_req_finished(req);
292         RETURN(rc);
293 }
294
295 ssize_t ll_getxattr(struct dentry *dentry, const char *name,
296                     void *buffer, size_t size)
297 {
298         struct inode *inode = dentry->d_inode;
299
300         LASSERT(inode);
301         LASSERT(name);
302
303         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), xattr %s\n",
304                inode->i_ino, inode->i_generation, inode, name);
305
306         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_GETXATTR, 1);
307
308          if (strncmp(name, XATTR_TRUSTED_PREFIX, 8) == 0 &&
309              strcmp(name + 8, "lov") == 0) {
310                  struct lov_user_md *lump;
311                  struct lov_mds_md *lmm = NULL;
312                  struct ptlrpc_request *request = NULL;
313                  int rc = 0, lmmsize;
314
315                  if (S_ISREG(inode->i_mode)) {
316                          rc = ll_lov_getstripe_ea_info(dentry->d_parent->d_inode, 
317                                                        dentry->d_name.name, &lmm, 
318                                                        &lmmsize, &request);
319                  } else if (S_ISDIR(inode->i_mode)) {
320                          rc = ll_dir_getstripe(inode, &lmm, &lmmsize, &request);
321                  }
322
323                  if (rc < 0)
324                         GOTO(out, rc);
325                  if (size == 0)
326                         GOTO(out, rc = lmmsize);
327
328                  if (size < lmmsize) {
329                          CERROR("server bug: replied size %u > %u\n",
330                                 lmmsize, (int)size);
331                          GOTO(out, rc = -ERANGE);
332                  }
333
334                  lump = (struct lov_user_md *)buffer;
335                  memcpy(lump, lmm, lmmsize);
336
337                  rc = lmmsize;
338 out:
339                  ptlrpc_req_finished(request);
340                  return(rc);
341         }
342
343         return ll_getxattr_common(inode, name, buffer, size, OBD_MD_FLXATTR);
344 }
345
346 ssize_t ll_listxattr(struct dentry *dentry, char *buffer, size_t size)
347 {
348         struct inode *inode = dentry->d_inode;
349         int rc = 0, rc2 = 0;
350
351         LASSERT(inode);
352
353         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n",
354                inode->i_ino, inode->i_generation, inode);
355
356         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_LISTXATTR, 1);
357
358         rc = ll_getxattr_common(inode, NULL, buffer, size, OBD_MD_FLXATTRLS);
359
360         if (!capable(CAP_SYS_ADMIN)) {
361                 struct lov_mds_md *lmm = NULL;
362                 struct ptlrpc_request *request = NULL;
363                 int lmmsize;
364
365                 if (S_ISREG(inode->i_mode)) {
366                         struct ll_inode_info *lli = ll_i2info(inode);
367                         struct lov_stripe_md *lsm = NULL;
368                         lsm = lli->lli_smd;
369                         if (lsm == NULL)
370                                 rc2 = -1; 
371                 } else if (S_ISDIR(inode->i_mode)) {
372                         rc2 = ll_dir_getstripe(inode, &lmm, &lmmsize, &request);
373                 }
374
375                 if (rc2 < 0) {
376                         GOTO(out, rc2 = 0);
377                 } else {
378                         const int prefix_len = sizeof(XATTR_TRUSTED_PREFIX)-1;
379                         const size_t name_len   = sizeof("lov") - 1;
380                         const size_t total_len  = prefix_len + name_len + 1;
381
382                         if (buffer && (rc + total_len) <= size) {
383                                 buffer += rc;
384                                 memcpy(buffer,XATTR_TRUSTED_PREFIX, prefix_len);
385                                 memcpy(buffer+prefix_len, "lov", name_len);
386                                 buffer[prefix_len + name_len] = '\0';
387                         }
388                         rc2 = total_len;
389                 }
390 out:
391                 ptlrpc_req_finished(request);
392                 rc = rc + rc2;
393         }
394         
395         return rc;
396 }
397