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