Whamcloud - gitweb
LU-5560 security: send file security context for creates
[fs/lustre-release.git] / lustre / include / md_object.h
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, 2015, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/include/md_object.h
37  *
38  * Extention of lu_object.h for metadata objects
39  */
40
41 #ifndef _LUSTRE_MD_OBJECT_H
42 #define _LUSTRE_MD_OBJECT_H
43
44 #ifndef HAVE_SERVER_SUPPORT
45 # error "client code should not depend on md_object.h"
46 #endif /* !HAVE_SERVER_SUPPORT */
47
48 /** \defgroup md md
49  * Sub-class of lu_object with methods common for "meta-data" objects in MDT
50  * stack.
51  *
52  * Meta-data objects implement namespace operations: you can link, unlink
53  * them, and treat them as directories.
54  *
55  * Examples: mdt, cmm, and mdt are implementations of md interface.
56  * @{
57  */
58
59
60 /*
61  * super-class definitions.
62  */
63 #include <dt_object.h>
64
65 struct md_device;
66 struct md_device_operations;
67 struct md_object;
68 struct obd_export;
69
70 /** metadata attributes */
71 enum ma_valid {
72         MA_INODE     = 1 << 0,
73         MA_LOV       = 1 << 1,
74         MA_FLAGS     = 1 << 2,
75         MA_LMV       = 1 << 3,
76         MA_ACL_DEF   = 1 << 4,
77         MA_LOV_DEF   = 1 << 5,
78         MA_HSM       = 1 << 6,
79         MA_PFID      = 1 << 7,
80         MA_LMV_DEF   = 1 << 8,
81 };
82
83 typedef enum {
84         MDL_MINMODE  = 0,
85         MDL_EX       = 1,
86         MDL_PW       = 2,
87         MDL_PR       = 4,
88         MDL_CW       = 8,
89         MDL_CR       = 16,
90         MDL_NL       = 32,
91         MDL_GROUP    = 64,
92         MDL_MAXMODE
93 } mdl_mode_t;
94
95 typedef enum {
96         MDT_NUL_LOCK = 0,
97         MDT_REG_LOCK = (1 << 0),
98         MDT_PDO_LOCK = (1 << 1)
99 } mdl_type_t;
100
101 /* lfs rgetfacl permission check */
102 #define MAY_RGETFACL    (1 << 14)
103
104 /* memory structure for hsm attributes
105  * for fields description see the on disk structure hsm_attrs
106  * which is defined in lustre_idl.h
107  */
108 struct md_hsm {
109         __u32   mh_compat;
110         __u32   mh_flags;
111         __u64   mh_arch_id;
112         __u64   mh_arch_ver;
113 };
114
115 struct md_attr {
116         __u64                   ma_valid;
117         __u64                   ma_need;
118         __u64                   ma_attr_flags;
119         struct lu_attr          ma_attr;
120         struct lu_fid           ma_pfid;
121         struct md_hsm           ma_hsm;
122         struct lov_mds_md      *ma_lmm;
123         union lmv_mds_md       *ma_lmv;
124         void                   *ma_acl;
125         int                     ma_lmm_size;
126         int                     ma_lmv_size;
127         int                     ma_acl_size;
128 };
129
130 /** Additional parameters for create */
131 struct md_op_spec {
132         union {
133                 /** symlink target */
134                 const char               *sp_symname;
135                 /** eadata for regular files */
136                 struct md_spec_reg {
137                         const void *eadata;
138                         int  eadatalen;
139                 } sp_ea;
140         } u;
141
142         /** Create flag from client: such as MDS_OPEN_CREAT, and others. */
143         __u64      sp_cr_flags;
144
145         /* File security context for creates. */
146         const char      *sp_cr_file_secctx_name; /* (security) xattr name */
147         void            *sp_cr_file_secctx; /* xattr value */
148         size_t           sp_cr_file_secctx_size; /* xattr value size */
149
150         /** don't create lov objects or llog cookie - this replay */
151         unsigned int no_create:1,
152                      sp_cr_lookup:1, /* do lookup sanity check or not. */
153                      sp_rm_entry:1,  /* only remove name entry */
154                      sp_permitted:1, /* do not check permission */
155                      sp_migrate_close:1; /* close the file during migrate */
156         /** Current lock mode for parent dir where create is performing. */
157         mdl_mode_t sp_cr_mode;
158
159         /** to create directory */
160         const struct dt_index_features *sp_feat;
161 };
162
163 union ldlm_policy_data;
164 /**
165  * Operations implemented for each md object (both directory and leaf).
166  */
167 struct md_object_operations {
168         int (*moo_permission)(const struct lu_env *env,
169                               struct md_object *pobj, struct md_object *cobj,
170                               struct md_attr *attr, int mask);
171
172         int (*moo_attr_get)(const struct lu_env *env, struct md_object *obj,
173                             struct md_attr *attr);
174
175         int (*moo_attr_set)(const struct lu_env *env, struct md_object *obj,
176                             const struct md_attr *attr);
177
178         int (*moo_xattr_get)(const struct lu_env *env, struct md_object *obj,
179                              struct lu_buf *buf, const char *name);
180
181         int (*moo_xattr_list)(const struct lu_env *env, struct md_object *obj,
182                               struct lu_buf *buf);
183
184         int (*moo_xattr_set)(const struct lu_env *env, struct md_object *obj,
185                              const struct lu_buf *buf, const char *name,
186                              int fl);
187
188         int (*moo_xattr_del)(const struct lu_env *env, struct md_object *obj,
189                              const char *name);
190
191         /** This method is used to swap the layouts between 2 objects */
192         int (*moo_swap_layouts)(const struct lu_env *env,
193                                struct md_object *obj1, struct md_object *obj2,
194                                __u64 flags);
195
196         /** \retval number of bytes actually read upon success */
197         int (*moo_readpage)(const struct lu_env *env, struct md_object *obj,
198                             const struct lu_rdpg *rdpg);
199
200         int (*moo_readlink)(const struct lu_env *env, struct md_object *obj,
201                             struct lu_buf *buf);
202         int (*moo_changelog)(const struct lu_env *env,
203                              enum changelog_rec_type type, int flags,
204                              struct md_object *obj);
205
206         int (*moo_open)(const struct lu_env *env,
207                         struct md_object *obj, int flag);
208
209         int (*moo_close)(const struct lu_env *env, struct md_object *obj,
210                          struct md_attr *ma, int mode);
211
212         int (*moo_object_sync)(const struct lu_env *, struct md_object *);
213
214         int (*moo_object_lock)(const struct lu_env *env, struct md_object *obj,
215                                struct lustre_handle *lh,
216                                struct ldlm_enqueue_info *einfo,
217                                union ldlm_policy_data *policy);
218         int (*moo_object_unlock)(const struct lu_env *env,
219                                  struct md_object *obj,
220                                  struct ldlm_enqueue_info *einfo,
221                                  union ldlm_policy_data *policy);
222
223         int (*moo_invalidate)(const struct lu_env *env, struct md_object *obj);
224 };
225
226 /**
227  * Operations implemented for each directory object.
228  */
229 struct md_dir_operations {
230         int (*mdo_is_subdir) (const struct lu_env *env, struct md_object *obj,
231                               const struct lu_fid *fid, struct lu_fid *sfid);
232
233         int (*mdo_lookup)(const struct lu_env *env, struct md_object *obj,
234                           const struct lu_name *lname, struct lu_fid *fid,
235                           struct md_op_spec *spec);
236
237         mdl_mode_t (*mdo_lock_mode)(const struct lu_env *env,
238                                     struct md_object *obj,
239                                     mdl_mode_t mode);
240
241         int (*mdo_create)(const struct lu_env *env, struct md_object *pobj,
242                           const struct lu_name *lname, struct md_object *child,
243                           struct md_op_spec *spec,
244                           struct md_attr *ma);
245
246         /** This method is used for creating data object for this meta object*/
247         int (*mdo_create_data)(const struct lu_env *env, struct md_object *p,
248                                struct md_object *o,
249                                const struct md_op_spec *spec,
250                                struct md_attr *ma);
251
252         int (*mdo_rename)(const struct lu_env *env, struct md_object *spobj,
253                           struct md_object *tpobj, const struct lu_fid *lf,
254                           const struct lu_name *lsname, struct md_object *tobj,
255                           const struct lu_name *ltname, struct md_attr *ma);
256
257         int (*mdo_link)(const struct lu_env *env, struct md_object *tgt_obj,
258                         struct md_object *src_obj, const struct lu_name *lname,
259                         struct md_attr *ma);
260
261         int (*mdo_unlink)(const struct lu_env *env, struct md_object *pobj,
262                           struct md_object *cobj, const struct lu_name *lname,
263                           struct md_attr *ma, int no_name);
264
265         int (*mdo_migrate)(const struct lu_env *env, struct md_object *pobj,
266                            struct md_object *sobj, const struct lu_name *lname,
267                            struct md_object *tobj, struct md_attr *ma);
268 };
269
270 struct md_device_operations {
271         /** meta-data device related handlers. */
272         int (*mdo_root_get)(const struct lu_env *env, struct md_device *m,
273                             struct lu_fid *f);
274
275         int (*mdo_maxeasize_get)(const struct lu_env *env, struct md_device *m,
276                                 int *easize);
277
278         int (*mdo_statfs)(const struct lu_env *env, struct md_device *m,
279                           struct obd_statfs *sfs);
280
281         int (*mdo_llog_ctxt_get)(const struct lu_env *env,
282                                  struct md_device *m, int idx, void **h);
283
284         int (*mdo_iocontrol)(const struct lu_env *env, struct md_device *m,
285                              unsigned int cmd, int len, void *data);
286 };
287
288 struct md_device {
289         struct lu_device                   md_lu_dev;
290         const struct md_device_operations *md_ops;
291 };
292
293 struct md_object {
294         struct lu_object                   mo_lu;
295         const struct md_object_operations *mo_ops;
296         const struct md_dir_operations    *mo_dir_ops;
297 };
298
299 static inline struct md_device *lu2md_dev(const struct lu_device *d)
300 {
301         LASSERT(IS_ERR(d) || lu_device_is_md(d));
302         return container_of0(d, struct md_device, md_lu_dev);
303 }
304
305 static inline struct lu_device *md2lu_dev(struct md_device *d)
306 {
307         return &d->md_lu_dev;
308 }
309
310 static inline struct md_object *lu2md(const struct lu_object *o)
311 {
312         LASSERT(o == NULL || IS_ERR(o) || lu_device_is_md(o->lo_dev));
313         return container_of0(o, struct md_object, mo_lu);
314 }
315
316 static inline int md_device_init(struct md_device *md, struct lu_device_type *t)
317 {
318         return lu_device_init(&md->md_lu_dev, t);
319 }
320
321 static inline void md_device_fini(struct md_device *md)
322 {
323         lu_device_fini(&md->md_lu_dev);
324 }
325
326 static inline struct md_object *md_object_find_slice(const struct lu_env *env,
327                                                      struct md_device *md,
328                                                      const struct lu_fid *f)
329 {
330         return lu2md(lu_object_find_slice(env, md2lu_dev(md), f, NULL));
331 }
332
333
334 /** md operations */
335 static inline int mo_permission(const struct lu_env *env,
336                                 struct md_object *p,
337                                 struct md_object *c,
338                                 struct md_attr *at,
339                                 int mask)
340 {
341         LASSERT(c->mo_ops->moo_permission);
342         return c->mo_ops->moo_permission(env, p, c, at, mask);
343 }
344
345 static inline int mo_attr_get(const struct lu_env *env,
346                               struct md_object *m,
347                               struct md_attr *at)
348 {
349         LASSERT(m->mo_ops->moo_attr_get);
350         return m->mo_ops->moo_attr_get(env, m, at);
351 }
352
353 static inline int mo_readlink(const struct lu_env *env,
354                               struct md_object *m,
355                               struct lu_buf *buf)
356 {
357         LASSERT(m->mo_ops->moo_readlink);
358         return m->mo_ops->moo_readlink(env, m, buf);
359 }
360
361 static inline int mo_changelog(const struct lu_env *env,
362                                enum changelog_rec_type type,
363                                int flags, struct md_object *m)
364 {
365         LASSERT(m->mo_ops->moo_changelog);
366         return m->mo_ops->moo_changelog(env, type, flags, m);
367 }
368
369 static inline int mo_attr_set(const struct lu_env *env,
370                               struct md_object *m,
371                               const struct md_attr *at)
372 {
373         LASSERT(m->mo_ops->moo_attr_set);
374         return m->mo_ops->moo_attr_set(env, m, at);
375 }
376
377 static inline int mo_xattr_get(const struct lu_env *env,
378                                struct md_object *m,
379                                struct lu_buf *buf,
380                                const char *name)
381 {
382         LASSERT(m->mo_ops->moo_xattr_get);
383         return m->mo_ops->moo_xattr_get(env, m, buf, name);
384 }
385
386 static inline int mo_xattr_del(const struct lu_env *env,
387                                struct md_object *m,
388                                const char *name)
389 {
390         LASSERT(m->mo_ops->moo_xattr_del);
391         return m->mo_ops->moo_xattr_del(env, m, name);
392 }
393
394 static inline int mo_xattr_set(const struct lu_env *env,
395                                struct md_object *m,
396                                const struct lu_buf *buf,
397                                const char *name,
398                                int flags)
399 {
400         LASSERT(m->mo_ops->moo_xattr_set);
401         return m->mo_ops->moo_xattr_set(env, m, buf, name, flags);
402 }
403
404 static inline int mo_xattr_list(const struct lu_env *env,
405                                 struct md_object *m,
406                                 struct lu_buf *buf)
407 {
408         LASSERT(m->mo_ops->moo_xattr_list);
409         return m->mo_ops->moo_xattr_list(env, m, buf);
410 }
411
412 static inline int mo_invalidate(const struct lu_env *env, struct md_object *m)
413 {
414         LASSERT(m->mo_ops->moo_invalidate);
415         return m->mo_ops->moo_invalidate(env, m);
416 }
417
418 static inline int mo_swap_layouts(const struct lu_env *env,
419                                   struct md_object *o1,
420                                   struct md_object *o2, __u64 flags)
421 {
422         LASSERT(o1->mo_ops->moo_swap_layouts);
423         LASSERT(o2->mo_ops->moo_swap_layouts);
424         if (o1->mo_ops->moo_swap_layouts != o2->mo_ops->moo_swap_layouts)
425                 return -EPERM;
426         return o1->mo_ops->moo_swap_layouts(env, o1, o2, flags);
427 }
428
429 static inline int mo_open(const struct lu_env *env,
430                           struct md_object *m,
431                           int flags)
432 {
433         LASSERT(m->mo_ops->moo_open);
434         return m->mo_ops->moo_open(env, m, flags);
435 }
436
437 static inline int mo_close(const struct lu_env *env,
438                            struct md_object *m,
439                            struct md_attr *ma,
440                            int mode)
441 {
442         LASSERT(m->mo_ops->moo_close);
443         return m->mo_ops->moo_close(env, m, ma, mode);
444 }
445
446 static inline int mo_readpage(const struct lu_env *env,
447                               struct md_object *m,
448                               const struct lu_rdpg *rdpg)
449 {
450         LASSERT(m->mo_ops->moo_readpage);
451         return m->mo_ops->moo_readpage(env, m, rdpg);
452 }
453
454 static inline int mo_object_sync(const struct lu_env *env, struct md_object *m)
455 {
456         LASSERT(m->mo_ops->moo_object_sync);
457         return m->mo_ops->moo_object_sync(env, m);
458 }
459
460 static inline int mo_object_lock(const struct lu_env *env,
461                                  struct md_object *m,
462                                  struct lustre_handle *lh,
463                                  struct ldlm_enqueue_info *einfo,
464                                  union ldlm_policy_data *policy)
465 {
466         LASSERT(m->mo_ops->moo_object_lock);
467         return m->mo_ops->moo_object_lock(env, m, lh, einfo, policy);
468 }
469
470 static inline int mo_object_unlock(const struct lu_env *env,
471                                    struct md_object *m,
472                                    struct ldlm_enqueue_info *einfo,
473                                    union ldlm_policy_data *policy)
474 {
475         LASSERT(m->mo_ops->moo_object_unlock);
476         return m->mo_ops->moo_object_unlock(env, m, einfo, policy);
477 }
478
479 static inline int mdo_lookup(const struct lu_env *env,
480                              struct md_object *p,
481                              const struct lu_name *lname,
482                              struct lu_fid *f,
483                              struct md_op_spec *spec)
484 {
485         LASSERT(p->mo_dir_ops->mdo_lookup);
486         return p->mo_dir_ops->mdo_lookup(env, p, lname, f, spec);
487 }
488
489 static inline mdl_mode_t mdo_lock_mode(const struct lu_env *env,
490                                        struct md_object *mo,
491                                        mdl_mode_t lm)
492 {
493         if (mo->mo_dir_ops->mdo_lock_mode == NULL)
494                 return MDL_MINMODE;
495         return mo->mo_dir_ops->mdo_lock_mode(env, mo, lm);
496 }
497
498 static inline int mdo_create(const struct lu_env *env,
499                              struct md_object *p,
500                              const struct lu_name *lchild_name,
501                              struct md_object *c,
502                              struct md_op_spec *spc,
503                              struct md_attr *at)
504 {
505         LASSERT(p->mo_dir_ops->mdo_create);
506         return p->mo_dir_ops->mdo_create(env, p, lchild_name, c, spc, at);
507 }
508
509 static inline int mdo_create_data(const struct lu_env *env,
510                                   struct md_object *p,
511                                   struct md_object *c,
512                                   const struct md_op_spec *spec,
513                                   struct md_attr *ma)
514 {
515         LASSERT(c->mo_dir_ops->mdo_create_data);
516         return c->mo_dir_ops->mdo_create_data(env, p, c, spec, ma);
517 }
518
519 static inline int mdo_rename(const struct lu_env *env,
520                              struct md_object *sp,
521                              struct md_object *tp,
522                              const struct lu_fid *lf,
523                              const struct lu_name *lsname,
524                              struct md_object *t,
525                              const struct lu_name *ltname,
526                              struct md_attr *ma)
527 {
528         LASSERT(tp->mo_dir_ops->mdo_rename);
529         return tp->mo_dir_ops->mdo_rename(env, sp, tp, lf, lsname, t, ltname,
530                                           ma);
531 }
532
533 static inline int mdo_migrate(const struct lu_env *env,
534                              struct md_object *pobj,
535                              struct md_object *sobj,
536                              const struct lu_name *lname,
537                              struct md_object *tobj,
538                              struct md_attr *ma)
539 {
540         LASSERT(pobj->mo_dir_ops->mdo_migrate);
541         return pobj->mo_dir_ops->mdo_migrate(env, pobj, sobj, lname, tobj, ma);
542 }
543
544 static inline int mdo_is_subdir(const struct lu_env *env,
545                                 struct md_object *mo,
546                                 const struct lu_fid *fid,
547                                 struct lu_fid *sfid)
548 {
549         LASSERT(mo->mo_dir_ops->mdo_is_subdir);
550         return mo->mo_dir_ops->mdo_is_subdir(env, mo, fid, sfid);
551 }
552
553 static inline int mdo_link(const struct lu_env *env,
554                            struct md_object *p,
555                            struct md_object *s,
556                            const struct lu_name *lname,
557                            struct md_attr *ma)
558 {
559         LASSERT(s->mo_dir_ops->mdo_link);
560         return s->mo_dir_ops->mdo_link(env, p, s, lname, ma);
561 }
562
563 static inline int mdo_unlink(const struct lu_env *env,
564                              struct md_object *p,
565                              struct md_object *c,
566                              const struct lu_name *lname,
567                              struct md_attr *ma, int no_name)
568 {
569         LASSERT(p->mo_dir_ops->mdo_unlink);
570         return p->mo_dir_ops->mdo_unlink(env, p, c, lname, ma, no_name);
571 }
572
573 /**
574  * Used in MDD/OUT layer for object lock rule
575  **/
576 enum mdd_object_role {
577         MOR_SRC_PARENT,
578         MOR_SRC_CHILD,
579         MOR_TGT_PARENT,
580         MOR_TGT_CHILD,
581         MOR_TGT_ORPHAN
582 };
583
584 struct dt_device;
585
586 int lustre_buf2hsm(void *buf, int rc, struct md_hsm *mh);
587 void lustre_hsm2buf(void *buf, const struct md_hsm *mh);
588
589 enum {
590         UCRED_INVALID   = -1,
591         UCRED_INIT      = 0,
592         UCRED_OLD       = 1,
593         UCRED_NEW       = 2,
594 };
595
596 struct lu_ucred {
597         __u32                    uc_valid;
598         __u32                    uc_o_uid;
599         __u32                    uc_o_gid;
600         __u32                    uc_o_fsuid;
601         __u32                    uc_o_fsgid;
602         __u32                    uc_uid;
603         __u32                    uc_gid;
604         __u32                    uc_fsuid;
605         __u32                    uc_fsgid;
606         __u32                    uc_suppgids[2];
607         cfs_cap_t                uc_cap;
608         __u32                    uc_umask;
609         struct group_info       *uc_ginfo;
610         struct md_identity      *uc_identity;
611         char                     uc_jobid[LUSTRE_JOBID_SIZE];
612 };
613
614 struct lu_ucred *lu_ucred(const struct lu_env *env);
615
616 struct lu_ucred *lu_ucred_check(const struct lu_env *env);
617
618 struct lu_ucred *lu_ucred_assert(const struct lu_env *env);
619
620 int lu_ucred_global_init(void);
621
622 void lu_ucred_global_fini(void);
623
624 #define md_cap_t(x) (x)
625
626 #define MD_CAP_TO_MASK(x) (1 << (x))
627
628 #define md_cap_raised(c, flag) (md_cap_t(c) & MD_CAP_TO_MASK(flag))
629
630 /* capable() is copied from linux kernel! */
631 static inline int md_capable(struct lu_ucred *uc, cfs_cap_t cap)
632 {
633         if (md_cap_raised(uc->uc_cap, cap))
634                 return 1;
635         return 0;
636 }
637
638 /** @} md */
639 #endif /* _LINUX_MD_OBJECT_H */