Whamcloud - gitweb
Land b_head_interop_disk on HEAD (20081119_1314)
[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_PREFIX       "user."
54 #define XATTR_TRUSTED_PREFIX    "trusted."
55 #define XATTR_SECURITY_PREFIX   "security."
56 #define XATTR_LUSTRE_PREFIX     "lustre."
57
58 #define XATTR_USER_T            (1)
59 #define XATTR_TRUSTED_T         (2)
60 #define XATTR_SECURITY_T        (3)
61 #define XATTR_ACL_ACCESS_T      (4)
62 #define XATTR_ACL_DEFAULT_T     (5)
63 #define XATTR_LUSTRE_T          (6)
64 #define XATTR_OTHER_T           (7)
65
66 static
67 int get_xattr_type(const char *name)
68 {
69         if (!strcmp(name, POSIX_ACL_XATTR_ACCESS))
70                 return XATTR_ACL_ACCESS_T;
71
72         if (!strcmp(name, POSIX_ACL_XATTR_DEFAULT))
73                 return XATTR_ACL_DEFAULT_T;
74
75         if (!strncmp(name, XATTR_USER_PREFIX,
76                      sizeof(XATTR_USER_PREFIX) - 1))
77                 return XATTR_USER_T;
78
79         if (!strncmp(name, XATTR_TRUSTED_PREFIX,
80                      sizeof(XATTR_TRUSTED_PREFIX) - 1))
81                 return XATTR_TRUSTED_T;
82
83         if (!strncmp(name, XATTR_SECURITY_PREFIX,
84                      sizeof(XATTR_SECURITY_PREFIX) - 1))
85                 return XATTR_SECURITY_T;
86
87         if (!strncmp(name, XATTR_LUSTRE_PREFIX,
88                      sizeof(XATTR_LUSTRE_PREFIX) - 1))
89                 return XATTR_LUSTRE_T;
90
91         return XATTR_OTHER_T;
92 }
93
94 static
95 int xattr_type_filter(struct ll_sb_info *sbi, int xattr_type)
96 {
97         if ((xattr_type == XATTR_ACL_ACCESS_T ||
98              xattr_type == XATTR_ACL_DEFAULT_T) &&
99            !(sbi->ll_flags & LL_SBI_ACL))
100                 return -EOPNOTSUPP;
101
102         if (xattr_type == XATTR_USER_T && !(sbi->ll_flags & LL_SBI_USER_XATTR))
103                 return -EOPNOTSUPP;
104         if (xattr_type == XATTR_TRUSTED_T && !cfs_capable(CFS_CAP_SYS_ADMIN))
105                 return -EPERM;
106         if (xattr_type == XATTR_OTHER_T)
107                 return -EOPNOTSUPP;
108
109         return 0;
110 }
111
112 static
113 int ll_setxattr_common(struct inode *inode, const char *name,
114                        const void *value, size_t size,
115                        int flags, __u64 valid)
116 {
117         struct ll_sb_info *sbi = ll_i2sbi(inode);
118         struct ptlrpc_request *req;
119         int xattr_type, rc;
120         struct obd_capa *oc;
121         posix_acl_xattr_header *new_value = NULL;
122         struct rmtacl_ctl_entry *rce = NULL;
123         ext_acl_xattr_header *acl = NULL;
124         const char *pv = value;
125         ENTRY;
126
127         xattr_type = get_xattr_type(name);
128         rc = xattr_type_filter(sbi, xattr_type);
129         if (rc)
130                 RETURN(rc);
131
132         /* b10667: ignore lustre special xattr for now */
133         if ((xattr_type == XATTR_TRUSTED_T && strcmp(name, "trusted.lov") == 0) ||
134             (xattr_type == XATTR_LUSTRE_T && strcmp(name, "lustre.lov") == 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                 if (S_ISREG(inode->i_mode)) {
228                         struct file f;
229                         int flags = FMODE_WRITE;
230
231                         f.f_dentry = dentry;
232                         rc = ll_lov_setstripe_ea_info(inode, &f, flags,
233                                                       lump, sizeof(*lump));
234                         /* b10667: rc always be 0 here for now */
235                         rc = 0;
236                 } else if (S_ISDIR(inode->i_mode)) {
237                         rc = ll_dir_setstripe(inode, lump, 0);
238                 }
239
240                 return rc;
241
242         } else if (strcmp(name, "trusted.lma") == 0) /* b17288: ignore common_ea */
243                 return 0;
244
245         return ll_setxattr_common(inode, name, value, size, flags,
246                                   OBD_MD_FLXATTR);
247 }
248
249 int ll_removexattr(struct dentry *dentry, const char *name)
250 {
251         struct inode *inode = dentry->d_inode;
252
253         LASSERT(inode);
254         LASSERT(name);
255
256         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), xattr %s\n",
257                inode->i_ino, inode->i_generation, inode, name);
258
259         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_REMOVEXATTR, 1);
260         return ll_setxattr_common(inode, name, NULL, 0, 0,
261                                   OBD_MD_FLXATTRRM);
262 }
263
264 static
265 int ll_getxattr_common(struct inode *inode, const char *name,
266                        void *buffer, size_t size, __u64 valid)
267 {
268         struct ll_sb_info *sbi = ll_i2sbi(inode);
269         struct ptlrpc_request *req = NULL;
270         struct mdt_body *body;
271         int xattr_type, rc;
272         void *xdata;
273         struct obd_capa *oc;
274         struct rmtacl_ctl_entry *rce = NULL;
275         ENTRY;
276
277         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n",
278                inode->i_ino, inode->i_generation, inode);
279
280         /* listxattr have slightly different behavior from of ext3:
281          * without 'user_xattr' ext3 will list all xattr names but
282          * filtered out "^user..*"; we list them all for simplicity.
283          */
284         if (!name) {
285                 xattr_type = XATTR_OTHER_T;
286                 goto do_getxattr;
287         }
288
289         xattr_type = get_xattr_type(name);
290         rc = xattr_type_filter(sbi, xattr_type);
291         if (rc)
292                 RETURN(rc);
293
294 #ifdef CONFIG_FS_POSIX_ACL
295         if (sbi->ll_flags & LL_SBI_RMT_CLIENT &&
296             (xattr_type == XATTR_ACL_ACCESS_T ||
297             xattr_type == XATTR_ACL_DEFAULT_T)) {
298                 rce = rct_search(&sbi->ll_rct, cfs_curproc_pid());
299                 if (rce == NULL ||
300                     (rce->rce_ops != RMT_LSETFACL &&
301                     rce->rce_ops != RMT_LGETFACL &&
302                     rce->rce_ops != RMT_RSETFACL &&
303                     rce->rce_ops != RMT_RGETFACL))
304                         RETURN(-EOPNOTSUPP);
305         }
306 #endif
307
308         /* posix acl is under protection of LOOKUP lock. when calling to this,
309          * we just have path resolution to the target inode, so we have great
310          * chance that cached ACL is uptodate.
311          */
312 #ifdef CONFIG_FS_POSIX_ACL
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                 spin_lock(&lli->lli_lock);
319                 acl = posix_acl_dup(lli->lli_posix_acl);
320                 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         /* do not need swab xattr data */
362         xdata = req_capsule_server_sized_get(&req->rq_pill, &RMF_EADATA,
363                                              body->eadatasize);
364         if (!xdata)
365                 GOTO(out, rc = -EFAULT);
366
367 #ifdef CONFIG_FS_POSIX_ACL
368         if (body->eadatasize >= 0 && rce && rce->rce_ops == RMT_LSETFACL) {
369                 ext_acl_xattr_header *acl;
370
371                 acl = lustre_posix_acl_xattr_2ext((posix_acl_xattr_header *)xdata,
372                                                   body->eadatasize);
373                 if (IS_ERR(acl))
374                         GOTO(out, rc = PTR_ERR(acl));
375
376                 rc = ee_add(&sbi->ll_et, cfs_curproc_pid(), ll_inode2fid(inode),
377                             xattr_type, acl);
378                 if (unlikely(rc < 0)) {
379                         lustre_ext_acl_xattr_free(acl);
380                         GOTO(out, rc);
381                 }
382         }
383
384         if (xattr_type == XATTR_ACL_ACCESS_T && !body->eadatasize)
385                 GOTO(out, rc = -ENODATA);
386 #endif
387         LASSERT(buffer);
388         memcpy(buffer, xdata, body->eadatasize);
389         rc = body->eadatasize;
390         EXIT;
391 out:
392         ptlrpc_req_finished(req);
393         return rc;
394 }
395
396 ssize_t ll_getxattr(struct dentry *dentry, const char *name,
397                     void *buffer, size_t size)
398 {
399         struct inode *inode = dentry->d_inode;
400
401         LASSERT(inode);
402         LASSERT(name);
403
404         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), xattr %s\n",
405                inode->i_ino, inode->i_generation, inode, name);
406
407         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_GETXATTR, 1);
408
409         if ((strncmp(name, XATTR_TRUSTED_PREFIX,
410                      sizeof(XATTR_TRUSTED_PREFIX) - 1) == 0 &&
411              strcmp(name + sizeof(XATTR_TRUSTED_PREFIX) - 1, "lov") == 0) ||
412             (strncmp(name, XATTR_LUSTRE_PREFIX,
413                      sizeof(XATTR_LUSTRE_PREFIX) - 1) == 0 &&
414              strcmp(name + sizeof(XATTR_LUSTRE_PREFIX) - 1, "lov") == 0)) {
415                 struct lov_user_md *lump;
416                 struct lov_mds_md *lmm = NULL;
417                 struct ptlrpc_request *request = NULL;
418                 int rc = 0, lmmsize = 0;
419
420                 if (S_ISREG(inode->i_mode)) {
421                         rc = ll_lov_getstripe_ea_info(dentry->d_parent->d_inode,
422                                                       dentry->d_name.name, &lmm,
423                                                       &lmmsize, &request);
424                 } else if (S_ISDIR(inode->i_mode)) {
425                         rc = ll_dir_getstripe(inode, &lmm, &lmmsize, &request);
426                 } else {
427                         rc = -ENODATA;
428                 }
429
430                 if (rc < 0)
431                        GOTO(out, rc);
432                 if (size == 0)
433                        GOTO(out, rc = lmmsize);
434
435                 if (size < lmmsize) {
436                         CERROR("server bug: replied size %d > %d for %s (%s)\n",
437                                lmmsize, (int)size, dentry->d_name.name, name);
438                         GOTO(out, rc = -ERANGE);
439                 }
440
441                 lump = (struct lov_user_md *)buffer;
442                 memcpy(lump, lmm, lmmsize);
443
444                 rc = lmmsize;
445 out:
446                 ptlrpc_req_finished(request);
447                 return(rc);
448         }
449
450         return ll_getxattr_common(inode, name, buffer, size, OBD_MD_FLXATTR);
451 }
452
453 ssize_t ll_listxattr(struct dentry *dentry, char *buffer, size_t size)
454 {
455         struct inode *inode = dentry->d_inode;
456         int rc = 0, rc2 = 0;
457         struct lov_mds_md *lmm = NULL;
458         struct ptlrpc_request *request = NULL;
459         int lmmsize;
460
461         LASSERT(inode);
462
463         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n",
464                inode->i_ino, inode->i_generation, inode);
465
466         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_LISTXATTR, 1);
467
468         rc = ll_getxattr_common(inode, NULL, buffer, size, OBD_MD_FLXATTRLS);
469
470         if (S_ISREG(inode->i_mode)) {
471                 struct ll_inode_info *lli = ll_i2info(inode);
472                 struct lov_stripe_md *lsm = NULL;
473                 lsm = lli->lli_smd;
474                 if (lsm == NULL)
475                         rc2 = -1;
476         } else if (S_ISDIR(inode->i_mode)) {
477                 rc2 = ll_dir_getstripe(inode, &lmm, &lmmsize, &request);
478         }
479
480         if (rc2 < 0) {
481                 GOTO(out, rc2 = 0);
482         } else {
483                 const int prefix_len = sizeof(XATTR_LUSTRE_PREFIX) - 1;
484                 const size_t name_len   = sizeof("lov") - 1;
485                 const size_t total_len  = prefix_len + name_len + 1;
486
487                 if (buffer && (rc + total_len) <= size) {
488                         buffer += rc;
489                         memcpy(buffer,XATTR_LUSTRE_PREFIX, prefix_len);
490                         memcpy(buffer+prefix_len, "lov", name_len);
491                         buffer[prefix_len + name_len] = '\0';
492                 }
493                 rc2 = total_len;
494         }
495 out:
496         ptlrpc_req_finished(request);
497         rc = rc + rc2;
498
499         return rc;
500 }