Whamcloud - gitweb
LU-1347 build: remove the vim/emacs modelines
[fs/lustre-release.git] / lustre / llite / xattr.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2012, Whamcloud, Inc.
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 #ifdef HAVE_SELINUX_IS_ENABLED
42 #include <linux/selinux.h>
43 #endif
44
45 #define DEBUG_SUBSYSTEM S_LLITE
46
47 #include <obd_support.h>
48 #include <lustre_lite.h>
49 #include <lustre_dlm.h>
50 #include <lustre_ver.h>
51 #include <lustre_acl.h>
52
53 #include "llite_internal.h"
54
55 #define XATTR_USER_T            (1)
56 #define XATTR_TRUSTED_T         (2)
57 #define XATTR_SECURITY_T        (3)
58 #define XATTR_ACL_ACCESS_T      (4)
59 #define XATTR_ACL_DEFAULT_T     (5)
60 #define XATTR_LUSTRE_T          (6)
61 #define XATTR_OTHER_T           (7)
62
63 static
64 int get_xattr_type(const char *name)
65 {
66         if (!strcmp(name, POSIX_ACL_XATTR_ACCESS))
67                 return XATTR_ACL_ACCESS_T;
68
69         if (!strcmp(name, POSIX_ACL_XATTR_DEFAULT))
70                 return XATTR_ACL_DEFAULT_T;
71
72         if (!strncmp(name, XATTR_USER_PREFIX,
73                      sizeof(XATTR_USER_PREFIX) - 1))
74                 return XATTR_USER_T;
75
76         if (!strncmp(name, XATTR_TRUSTED_PREFIX,
77                      sizeof(XATTR_TRUSTED_PREFIX) - 1))
78                 return XATTR_TRUSTED_T;
79
80         if (!strncmp(name, XATTR_SECURITY_PREFIX,
81                      sizeof(XATTR_SECURITY_PREFIX) - 1))
82                 return XATTR_SECURITY_T;
83
84         if (!strncmp(name, XATTR_LUSTRE_PREFIX,
85                      sizeof(XATTR_LUSTRE_PREFIX) - 1))
86                 return XATTR_LUSTRE_T;
87
88         return XATTR_OTHER_T;
89 }
90
91 static
92 int xattr_type_filter(struct ll_sb_info *sbi, int xattr_type)
93 {
94         if ((xattr_type == XATTR_ACL_ACCESS_T ||
95              xattr_type == XATTR_ACL_DEFAULT_T) &&
96            !(sbi->ll_flags & LL_SBI_ACL))
97                 return -EOPNOTSUPP;
98
99         if (xattr_type == XATTR_SECURITY_T && !selinux_is_enabled())
100                 return -EOPNOTSUPP;
101         if (xattr_type == XATTR_USER_T && !(sbi->ll_flags & LL_SBI_USER_XATTR))
102                 return -EOPNOTSUPP;
103         if (xattr_type == XATTR_TRUSTED_T && !cfs_capable(CFS_CAP_SYS_ADMIN))
104                 return -EPERM;
105         if (xattr_type == XATTR_OTHER_T)
106                 return -EOPNOTSUPP;
107
108         return 0;
109 }
110
111 static
112 int ll_setxattr_common(struct inode *inode, const char *name,
113                        const void *value, size_t size,
114                        int flags, __u64 valid)
115 {
116         struct ll_sb_info *sbi = ll_i2sbi(inode);
117         struct ptlrpc_request *req;
118         int xattr_type, rc;
119         struct obd_capa *oc;
120         posix_acl_xattr_header *new_value = NULL;
121         struct rmtacl_ctl_entry *rce = NULL;
122         ext_acl_xattr_header *acl = NULL;
123         const char *pv = value;
124         ENTRY;
125
126         xattr_type = get_xattr_type(name);
127         rc = xattr_type_filter(sbi, xattr_type);
128         if (rc)
129                 RETURN(rc);
130
131         /* b10667: ignore lustre special xattr for now */
132         if ((xattr_type == XATTR_TRUSTED_T && strcmp(name, "trusted.lov") == 0) ||
133             (xattr_type == XATTR_LUSTRE_T && strcmp(name, "lustre.lov") == 0))
134                 RETURN(0);
135
136         /* b15587: ignore security.capability xattr for now */
137         if ((xattr_type == XATTR_SECURITY_T &&
138             strcmp(name, "security.capability") == 0))
139                 RETURN(0);
140
141 #ifdef CONFIG_FS_POSIX_ACL
142         if (sbi->ll_flags & LL_SBI_RMT_CLIENT &&
143             (xattr_type == XATTR_ACL_ACCESS_T ||
144             xattr_type == XATTR_ACL_DEFAULT_T)) {
145                 rce = rct_search(&sbi->ll_rct, cfs_curproc_pid());
146                 if (rce == NULL ||
147                     (rce->rce_ops != RMT_LSETFACL &&
148                     rce->rce_ops != RMT_RSETFACL))
149                         RETURN(-EOPNOTSUPP);
150
151                 if (rce->rce_ops == RMT_LSETFACL) {
152                         struct eacl_entry *ee;
153
154                         ee = et_search_del(&sbi->ll_et, cfs_curproc_pid(),
155                                            ll_inode2fid(inode), xattr_type);
156                         LASSERT(ee != NULL);
157                         if (valid & OBD_MD_FLXATTR) {
158                                 acl = lustre_acl_xattr_merge2ext(
159                                                 (posix_acl_xattr_header *)value,
160                                                 size, ee->ee_acl);
161                                 if (IS_ERR(acl)) {
162                                         ee_free(ee);
163                                         RETURN(PTR_ERR(acl));
164                                 }
165                                 size =  CFS_ACL_XATTR_SIZE(\
166                                                 le32_to_cpu(acl->a_count), \
167                                                 ext_acl_xattr);
168                                 pv = (const char *)acl;
169                         }
170                         ee_free(ee);
171                 } else if (rce->rce_ops == RMT_RSETFACL) {
172                         size = lustre_posix_acl_xattr_filter(
173                                                 (posix_acl_xattr_header *)value,
174                                                 size, &new_value);
175                         if (unlikely(size < 0))
176                                 RETURN(size);
177
178                         pv = (const char *)new_value;
179                 } else
180                         RETURN(-EOPNOTSUPP);
181
182                 valid |= rce_ops2valid(rce->rce_ops);
183         }
184 #endif
185         oc = ll_mdscapa_get(inode);
186         rc = md_setxattr(sbi->ll_md_exp, ll_inode2fid(inode), oc,
187                          valid, name, pv, size, 0, flags, ll_i2suppgid(inode),
188                          &req);
189         capa_put(oc);
190 #ifdef CONFIG_FS_POSIX_ACL
191         if (new_value != NULL)
192                 lustre_posix_acl_xattr_free(new_value, size);
193         if (acl != NULL)
194                 lustre_ext_acl_xattr_free(acl);
195 #endif
196         if (rc) {
197                 if (rc == -EOPNOTSUPP && xattr_type == XATTR_USER_T) {
198                         LCONSOLE_INFO("Disabling user_xattr feature because "
199                                       "it is not supported on the server\n");
200                         sbi->ll_flags &= ~LL_SBI_USER_XATTR;
201                 }
202                 RETURN(rc);
203         }
204
205         ptlrpc_req_finished(req);
206         RETURN(0);
207 }
208
209 int ll_setxattr(struct dentry *dentry, const char *name,
210                 const void *value, size_t size, int flags)
211 {
212         struct inode *inode = dentry->d_inode;
213
214         LASSERT(inode);
215         LASSERT(name);
216
217         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), xattr %s\n",
218                inode->i_ino, inode->i_generation, inode, name);
219
220         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_SETXATTR, 1);
221
222         if ((strncmp(name, XATTR_TRUSTED_PREFIX,
223                      sizeof(XATTR_TRUSTED_PREFIX) - 1) == 0 &&
224              strcmp(name + sizeof(XATTR_TRUSTED_PREFIX) - 1, "lov") == 0) ||
225             (strncmp(name, XATTR_LUSTRE_PREFIX,
226                      sizeof(XATTR_LUSTRE_PREFIX) - 1) == 0 &&
227              strcmp(name + sizeof(XATTR_LUSTRE_PREFIX) - 1, "lov") == 0)) {
228                 struct lov_user_md *lump = (struct lov_user_md *)value;
229                 int rc = 0;
230
231                 /* Attributes that are saved via getxattr will always have
232                  * the stripe_offset as 0.  Instead, the MDS should be
233                  * allowed to pick the starting OST index.   b=17846 */
234                 if (lump != NULL && lump->lmm_stripe_offset == 0)
235                         lump->lmm_stripe_offset = -1;
236
237                 if (lump != NULL && S_ISREG(inode->i_mode)) {
238                         struct file f;
239                         int flags = FMODE_WRITE;
240
241                         f.f_dentry = dentry;
242                         rc = ll_lov_setstripe_ea_info(inode, &f, flags,
243                                                       lump, sizeof(*lump));
244                         /* b10667: rc always be 0 here for now */
245                         rc = 0;
246                 } else if (S_ISDIR(inode->i_mode)) {
247                         rc = ll_dir_setstripe(inode, lump, 0);
248                 }
249
250                 return rc;
251
252         } else if (strcmp(name, XATTR_NAME_LMA) == 0 ||
253                    strcmp(name, XATTR_NAME_LINK) == 0)
254                 return 0;
255
256         return ll_setxattr_common(inode, name, value, size, flags,
257                                   OBD_MD_FLXATTR);
258 }
259
260 int ll_removexattr(struct dentry *dentry, const char *name)
261 {
262         struct inode *inode = dentry->d_inode;
263
264         LASSERT(inode);
265         LASSERT(name);
266
267         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), xattr %s\n",
268                inode->i_ino, inode->i_generation, inode, name);
269
270         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_REMOVEXATTR, 1);
271         return ll_setxattr_common(inode, name, NULL, 0, 0,
272                                   OBD_MD_FLXATTRRM);
273 }
274
275 static
276 int ll_getxattr_common(struct inode *inode, const char *name,
277                        void *buffer, size_t size, __u64 valid)
278 {
279         struct ll_sb_info *sbi = ll_i2sbi(inode);
280         struct ptlrpc_request *req = NULL;
281         struct mdt_body *body;
282         int xattr_type, rc;
283         void *xdata;
284         struct obd_capa *oc;
285         struct rmtacl_ctl_entry *rce = NULL;
286         ENTRY;
287
288         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n",
289                inode->i_ino, inode->i_generation, inode);
290
291         /* listxattr have slightly different behavior from of ext3:
292          * without 'user_xattr' ext3 will list all xattr names but
293          * filtered out "^user..*"; we list them all for simplicity.
294          */
295         if (!name) {
296                 xattr_type = XATTR_OTHER_T;
297                 goto do_getxattr;
298         }
299
300         xattr_type = get_xattr_type(name);
301         rc = xattr_type_filter(sbi, xattr_type);
302         if (rc)
303                 RETURN(rc);
304
305         /* b15587: ignore security.capability xattr for now */
306         if ((xattr_type == XATTR_SECURITY_T &&
307             strcmp(name, "security.capability") == 0))
308                 RETURN(-ENODATA);
309
310 #ifdef CONFIG_FS_POSIX_ACL
311         if (sbi->ll_flags & LL_SBI_RMT_CLIENT &&
312             (xattr_type == XATTR_ACL_ACCESS_T ||
313             xattr_type == XATTR_ACL_DEFAULT_T)) {
314                 rce = rct_search(&sbi->ll_rct, cfs_curproc_pid());
315                 if (rce == NULL ||
316                     (rce->rce_ops != RMT_LSETFACL &&
317                     rce->rce_ops != RMT_LGETFACL &&
318                     rce->rce_ops != RMT_RSETFACL &&
319                     rce->rce_ops != RMT_RGETFACL))
320                         RETURN(-EOPNOTSUPP);
321         }
322
323         /* posix acl is under protection of LOOKUP lock. when calling to this,
324          * we just have path resolution to the target inode, so we have great
325          * chance that cached ACL is uptodate.
326          */
327         if (xattr_type == XATTR_ACL_ACCESS_T &&
328             !(sbi->ll_flags & LL_SBI_RMT_CLIENT)) {
329                 struct ll_inode_info *lli = ll_i2info(inode);
330                 struct posix_acl *acl;
331
332                 cfs_spin_lock(&lli->lli_lock);
333                 acl = posix_acl_dup(lli->lli_posix_acl);
334                 cfs_spin_unlock(&lli->lli_lock);
335
336                 if (!acl)
337                         RETURN(-ENODATA);
338
339                 rc = posix_acl_to_xattr(acl, buffer, size);
340                 posix_acl_release(acl);
341                 RETURN(rc);
342         }
343         if (xattr_type == XATTR_ACL_DEFAULT_T && !S_ISDIR(inode->i_mode))
344                 RETURN(-ENODATA);
345 #endif
346
347 do_getxattr:
348         oc = ll_mdscapa_get(inode);
349         rc = md_getxattr(sbi->ll_md_exp, ll_inode2fid(inode), oc,
350                          valid | (rce ? rce_ops2valid(rce->rce_ops) : 0),
351                          name, NULL, 0, size, 0, &req);
352         capa_put(oc);
353         if (rc) {
354                 if (rc == -EOPNOTSUPP && xattr_type == XATTR_USER_T) {
355                         LCONSOLE_INFO("Disabling user_xattr feature because "
356                                       "it is not supported on the server\n");
357                         sbi->ll_flags &= ~LL_SBI_USER_XATTR;
358                 }
359                 RETURN(rc);
360         }
361
362         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
363         LASSERT(body);
364
365         /* only detect the xattr size */
366         if (size == 0)
367                 GOTO(out, rc = body->eadatasize);
368
369         if (size < body->eadatasize) {
370                 CERROR("server bug: replied size %u > %u\n",
371                        body->eadatasize, (int)size);
372                 GOTO(out, rc = -ERANGE);
373         }
374
375         if (body->eadatasize == 0)
376                 GOTO(out, rc = -ENODATA);
377
378         /* do not need swab xattr data */
379         xdata = req_capsule_server_sized_get(&req->rq_pill, &RMF_EADATA,
380                                              body->eadatasize);
381         if (!xdata)
382                 GOTO(out, rc = -EFAULT);
383
384 #ifdef CONFIG_FS_POSIX_ACL
385         if (body->eadatasize >= 0 && rce && rce->rce_ops == RMT_LSETFACL) {
386                 ext_acl_xattr_header *acl;
387
388                 acl = lustre_posix_acl_xattr_2ext((posix_acl_xattr_header *)xdata,
389                                                   body->eadatasize);
390                 if (IS_ERR(acl))
391                         GOTO(out, rc = PTR_ERR(acl));
392
393                 rc = ee_add(&sbi->ll_et, cfs_curproc_pid(), ll_inode2fid(inode),
394                             xattr_type, acl);
395                 if (unlikely(rc < 0)) {
396                         lustre_ext_acl_xattr_free(acl);
397                         GOTO(out, rc);
398                 }
399         }
400 #endif
401
402         if (body->eadatasize == 0) {
403                 rc = -ENODATA;
404         } else {
405                 LASSERT(buffer);
406                 memcpy(buffer, xdata, body->eadatasize);
407                 rc = body->eadatasize;
408         }
409         EXIT;
410 out:
411         ptlrpc_req_finished(req);
412         return rc;
413 }
414
415 ssize_t ll_getxattr(struct dentry *dentry, const char *name,
416                     void *buffer, size_t size)
417 {
418         struct inode *inode = dentry->d_inode;
419
420         LASSERT(inode);
421         LASSERT(name);
422
423         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), xattr %s\n",
424                inode->i_ino, inode->i_generation, inode, name);
425
426         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_GETXATTR, 1);
427
428         if ((strncmp(name, XATTR_TRUSTED_PREFIX,
429                      sizeof(XATTR_TRUSTED_PREFIX) - 1) == 0 &&
430              strcmp(name + sizeof(XATTR_TRUSTED_PREFIX) - 1, "lov") == 0) ||
431             (strncmp(name, XATTR_LUSTRE_PREFIX,
432                      sizeof(XATTR_LUSTRE_PREFIX) - 1) == 0 &&
433              strcmp(name + sizeof(XATTR_LUSTRE_PREFIX) - 1, "lov") == 0)) {
434                 struct lov_user_md *lump;
435                 struct lov_mds_md *lmm = NULL;
436                 struct ptlrpc_request *request = NULL;
437                 int rc = 0, lmmsize = 0;
438
439                 if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
440                         return -ENODATA;
441
442                 if (size == 0 && S_ISDIR(inode->i_mode)) {
443                         /* XXX directory EA is fix for now, optimize to save
444                          * RPC transfer */
445                         GOTO(out, rc = sizeof(struct lov_user_md));
446                 }
447
448                 if (!ll_i2info(inode)->lli_smd) {
449                         if (S_ISDIR(inode->i_mode)) {
450                                 rc = ll_dir_getstripe(inode, &lmm,
451                                                       &lmmsize, &request);
452                         } else {
453                                 rc = -ENODATA;
454                         }
455                 } else {
456                         /* LSM is present already after lookup/getattr call.
457                          * we need to grab layout lock once it is implemented */
458                         rc = obd_packmd(ll_i2dtexp(inode), &lmm,
459                                         ll_i2info(inode)->lli_smd);
460                         lmmsize = rc;
461                 }
462
463                 if (rc < 0)
464                        GOTO(out, rc);
465
466                 if (size == 0) {
467                         /* used to call ll_get_max_mdsize() forward to get
468                          * the maximum buffer size, while some apps (such as
469                          * rsync 3.0.x) care much about the exact xattr value
470                          * size */
471                         rc = lmmsize;
472                         GOTO(out, rc);
473                 }
474
475                 if (size < lmmsize) {
476                         CERROR("server bug: replied size %d > %d for %s (%s)\n",
477                                lmmsize, (int)size, dentry->d_name.name, name);
478                         GOTO(out, rc = -ERANGE);
479                 }
480
481                 lump = (struct lov_user_md *)buffer;
482                 memcpy(lump, lmm, lmmsize);
483
484                 rc = lmmsize;
485 out:
486                 if (request)
487                         ptlrpc_req_finished(request);
488                 else if (lmm)
489                         obd_free_diskmd(ll_i2dtexp(inode), &lmm);
490                 return(rc);
491         }
492
493         return ll_getxattr_common(inode, name, buffer, size, OBD_MD_FLXATTR);
494 }
495
496 ssize_t ll_listxattr(struct dentry *dentry, char *buffer, size_t size)
497 {
498         struct inode *inode = dentry->d_inode;
499         int rc = 0, rc2 = 0;
500         struct lov_mds_md *lmm = NULL;
501         struct ptlrpc_request *request = NULL;
502         int lmmsize;
503
504         LASSERT(inode);
505
506         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n",
507                inode->i_ino, inode->i_generation, inode);
508
509         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_LISTXATTR, 1);
510
511         rc = ll_getxattr_common(inode, NULL, buffer, size, OBD_MD_FLXATTRLS);
512         if (rc < 0)
513                 GOTO(out, rc);
514
515         if (S_ISREG(inode->i_mode)) {
516                 if (ll_i2info(inode)->lli_smd == NULL)
517                         rc2 = -1;
518         } else if (S_ISDIR(inode->i_mode)) {
519                 rc2 = ll_dir_getstripe(inode, &lmm, &lmmsize, &request);
520         }
521
522         if (rc2 < 0) {
523                 GOTO(out, rc2 = 0);
524         } else if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)) {
525                 const int prefix_len = sizeof(XATTR_LUSTRE_PREFIX) - 1;
526                 const size_t name_len   = sizeof("lov") - 1;
527                 const size_t total_len  = prefix_len + name_len + 1;
528
529                 if (buffer && (rc + total_len) <= size) {
530                         buffer += rc;
531                         memcpy(buffer,XATTR_LUSTRE_PREFIX, prefix_len);
532                         memcpy(buffer+prefix_len, "lov", name_len);
533                         buffer[prefix_len + name_len] = '\0';
534                 }
535                 rc2 = total_len;
536         }
537 out:
538         ptlrpc_req_finished(request);
539         rc = rc + rc2;
540
541         return rc;
542 }