Whamcloud - gitweb
acl includes and time_t formats cleanup.
[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 && !capable(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
243         return ll_setxattr_common(inode, name, value, size, flags,
244                                   OBD_MD_FLXATTR);
245 }
246
247 int ll_removexattr(struct dentry *dentry, const char *name)
248 {
249         struct inode *inode = dentry->d_inode;
250
251         LASSERT(inode);
252         LASSERT(name);
253
254         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), xattr %s\n",
255                inode->i_ino, inode->i_generation, inode, name);
256
257         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_REMOVEXATTR, 1);
258         return ll_setxattr_common(inode, name, NULL, 0, 0,
259                                   OBD_MD_FLXATTRRM);
260 }
261
262 static
263 int ll_getxattr_common(struct inode *inode, const char *name,
264                        void *buffer, size_t size, __u64 valid)
265 {
266         struct ll_sb_info *sbi = ll_i2sbi(inode);
267         struct ptlrpc_request *req = NULL;
268         struct mdt_body *body;
269         int xattr_type, rc;
270         void *xdata;
271         struct obd_capa *oc;
272         struct rmtacl_ctl_entry *rce = NULL;
273         ENTRY;
274
275         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n",
276                inode->i_ino, inode->i_generation, inode);
277
278         /* listxattr have slightly different behavior from of ext3:
279          * without 'user_xattr' ext3 will list all xattr names but
280          * filtered out "^user..*"; we list them all for simplicity.
281          */
282         if (!name) {
283                 xattr_type = XATTR_OTHER_T;
284                 goto do_getxattr;
285         }
286
287         xattr_type = get_xattr_type(name);
288         rc = xattr_type_filter(sbi, xattr_type);
289         if (rc)
290                 RETURN(rc);
291
292 #ifdef CONFIG_FS_POSIX_ACL
293         if (sbi->ll_flags & LL_SBI_RMT_CLIENT &&
294             (xattr_type == XATTR_ACL_ACCESS_T ||
295             xattr_type == XATTR_ACL_DEFAULT_T)) {
296                 rce = rct_search(&sbi->ll_rct, cfs_curproc_pid());
297                 if (rce == NULL ||
298                     (rce->rce_ops != RMT_LSETFACL &&
299                     rce->rce_ops != RMT_LGETFACL &&
300                     rce->rce_ops != RMT_RSETFACL &&
301                     rce->rce_ops != RMT_RGETFACL))
302                         RETURN(-EOPNOTSUPP);
303         }
304 #endif
305
306         /* posix acl is under protection of LOOKUP lock. when calling to this,
307          * we just have path resolution to the target inode, so we have great
308          * chance that cached ACL is uptodate.
309          */
310 #ifdef CONFIG_FS_POSIX_ACL
311         if (xattr_type == XATTR_ACL_ACCESS_T &&
312             !(sbi->ll_flags & LL_SBI_RMT_CLIENT)) {
313                 struct ll_inode_info *lli = ll_i2info(inode);
314                 struct posix_acl *acl;
315
316                 spin_lock(&lli->lli_lock);
317                 acl = posix_acl_dup(lli->lli_posix_acl);
318                 spin_unlock(&lli->lli_lock);
319
320                 if (!acl)
321                         RETURN(-ENODATA);
322
323                 rc = posix_acl_to_xattr(acl, buffer, size);
324                 posix_acl_release(acl);
325                 RETURN(rc);
326         }
327         if (xattr_type == XATTR_ACL_DEFAULT_T && !S_ISDIR(inode->i_mode))
328                 RETURN(-ENODATA);
329 #endif
330
331 do_getxattr:
332         oc = ll_mdscapa_get(inode);
333         rc = md_getxattr(sbi->ll_md_exp, ll_inode2fid(inode), oc,
334                          valid | (rce ? rce_ops2valid(rce->rce_ops) : 0),
335                          name, NULL, 0, size, 0, &req);
336         capa_put(oc);
337         if (rc) {
338                 if (rc == -EOPNOTSUPP && xattr_type == XATTR_USER_T) {
339                         LCONSOLE_INFO("Disabling user_xattr feature because "
340                                       "it is not supported on the server\n"); 
341                         sbi->ll_flags &= ~LL_SBI_USER_XATTR;
342                 }
343                 RETURN(rc);
344         }
345
346         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
347         LASSERT(body);
348
349         /* only detect the xattr size */
350         if (size == 0)
351                 GOTO(out, rc = body->eadatasize);
352
353         if (size < body->eadatasize) {
354                 CERROR("server bug: replied size %u > %u\n",
355                        body->eadatasize, (int)size);
356                 GOTO(out, rc = -ERANGE);
357         }
358
359         /* do not need swab xattr data */
360         xdata = req_capsule_server_sized_get(&req->rq_pill, &RMF_EADATA,
361                                              body->eadatasize);
362         if (!xdata)
363                 GOTO(out, rc = -EFAULT);
364
365 #ifdef CONFIG_FS_POSIX_ACL
366         if (body->eadatasize >= 0 && rce && rce->rce_ops == RMT_LSETFACL) {
367                 ext_acl_xattr_header *acl;
368
369                 acl = lustre_posix_acl_xattr_2ext((posix_acl_xattr_header *)xdata,
370                                                   body->eadatasize);
371                 if (IS_ERR(acl))
372                         GOTO(out, rc = PTR_ERR(acl));
373
374                 rc = ee_add(&sbi->ll_et, cfs_curproc_pid(), ll_inode2fid(inode),
375                             xattr_type, acl);
376                 if (unlikely(rc < 0)) {
377                         lustre_ext_acl_xattr_free(acl);
378                         GOTO(out, rc);
379                 }
380         }
381
382         if (xattr_type == XATTR_ACL_ACCESS_T && !body->eadatasize)
383                 GOTO(out, rc = -ENODATA);
384 #endif
385         LASSERT(buffer);
386         memcpy(buffer, xdata, body->eadatasize);
387         rc = body->eadatasize;
388         EXIT;
389 out:
390         ptlrpc_req_finished(req);
391         return rc;
392 }
393
394 ssize_t ll_getxattr(struct dentry *dentry, const char *name,
395                     void *buffer, size_t size)
396 {
397         struct inode *inode = dentry->d_inode;
398
399         LASSERT(inode);
400         LASSERT(name);
401
402         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), xattr %s\n",
403                inode->i_ino, inode->i_generation, inode, name);
404
405         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_GETXATTR, 1);
406
407         if ((strncmp(name, XATTR_TRUSTED_PREFIX, 
408                      sizeof(XATTR_TRUSTED_PREFIX) - 1) == 0 &&
409              strcmp(name + sizeof(XATTR_TRUSTED_PREFIX) - 1, "lov") == 0) ||
410             (strncmp(name, XATTR_LUSTRE_PREFIX, 
411                      sizeof(XATTR_LUSTRE_PREFIX) - 1) == 0 &&
412              strcmp(name + sizeof(XATTR_LUSTRE_PREFIX) - 1, "lov") == 0)) {
413                 struct lov_user_md *lump;
414                 struct lov_mds_md *lmm = NULL;
415                 struct ptlrpc_request *request = NULL;
416                 int rc = 0, lmmsize;
417
418                 if (S_ISREG(inode->i_mode)) {
419                         rc = ll_lov_getstripe_ea_info(dentry->d_parent->d_inode, 
420                                                       dentry->d_name.name, &lmm, 
421                                                       &lmmsize, &request);
422                 } else if (S_ISDIR(inode->i_mode)) {
423                         rc = ll_dir_getstripe(inode, &lmm, &lmmsize, &request);
424                 }
425
426                 if (rc < 0)
427                        GOTO(out, rc);
428                 if (size == 0)
429                        GOTO(out, rc = lmmsize);
430
431                 if (size < lmmsize) {
432                         CERROR("server bug: replied size %u > %u\n",
433                                lmmsize, (int)size);
434                         GOTO(out, rc = -ERANGE);
435                 }
436
437                 lump = (struct lov_user_md *)buffer;
438                 memcpy(lump, lmm, lmmsize);
439
440                 rc = lmmsize;
441 out:
442                 ptlrpc_req_finished(request);
443                 return(rc);
444         }
445
446         return ll_getxattr_common(inode, name, buffer, size, OBD_MD_FLXATTR);
447 }
448
449 ssize_t ll_listxattr(struct dentry *dentry, char *buffer, size_t size)
450 {
451         struct inode *inode = dentry->d_inode;
452         int rc = 0, rc2 = 0;
453         struct lov_mds_md *lmm = NULL;
454         struct ptlrpc_request *request = NULL;
455         int lmmsize;
456
457         LASSERT(inode);
458
459         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n",
460                inode->i_ino, inode->i_generation, inode);
461
462         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_LISTXATTR, 1);
463
464         rc = ll_getxattr_common(inode, NULL, buffer, size, OBD_MD_FLXATTRLS);
465
466         if (S_ISREG(inode->i_mode)) {
467                 struct ll_inode_info *lli = ll_i2info(inode);
468                 struct lov_stripe_md *lsm = NULL;
469                 lsm = lli->lli_smd;
470                 if (lsm == NULL)
471                         rc2 = -1; 
472         } else if (S_ISDIR(inode->i_mode)) {
473                 rc2 = ll_dir_getstripe(inode, &lmm, &lmmsize, &request);
474         }
475
476         if (rc2 < 0) {
477                 GOTO(out, rc2 = 0);
478         } else {
479                 const int prefix_len = sizeof(XATTR_LUSTRE_PREFIX) - 1;
480                 const size_t name_len   = sizeof("lov") - 1;
481                 const size_t total_len  = prefix_len + name_len + 1;
482
483                 if (buffer && (rc + total_len) <= size) {
484                         buffer += rc;
485                         memcpy(buffer,XATTR_LUSTRE_PREFIX, prefix_len);
486                         memcpy(buffer+prefix_len, "lov", name_len);
487                         buffer[prefix_len + name_len] = '\0';
488                 }
489                 rc2 = total_len;
490         }
491 out:
492         ptlrpc_req_finished(request);
493         rc = rc + rc2;
494         
495         return rc;
496 }