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