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