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