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