Whamcloud - gitweb
LU-13055 mdd: per-user changelog names and mask
[fs/lustre-release.git] / lustre / mdd / mdd_internal.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, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  *
31  * lustre/mdd/mdd_internal.h
32  *
33  * Author: Wang Di <wangdi@clusterfs.com>
34  */
35
36 #ifndef _MDD_INTERNAL_H
37 #define _MDD_INTERNAL_H
38
39 #include <lustre_acl.h>
40 #include <lustre_compat.h>
41 #include <md_object.h>
42 #include <dt_object.h>
43 #include <lustre_lfsck.h>
44 #include <lustre_fid.h>
45 #include <lprocfs_status.h>
46 #include <lustre_log.h>
47 #include <lustre_linkea.h>
48
49 /* ChangeLog params for automatic purge mechanism */
50 /* max time allowed for a user to stay idle in seconds */
51 #define CHLOG_MAX_IDLE_TIME 2592000 /* = 30 days */
52 /* max gap allowed for a user to stay idle in number of ChangeLog records
53  * this is an evaluation, assuming that chunk-size is LLOG_MIN_CHUNK_SIZE, of
54  * the indexes gap for half full changelogs */
55 #define CHLOG_MAX_IDLE_INDEXES (((LLOG_MIN_CHUNK_SIZE - \
56                                   offsetof(struct llog_log_hdr, \
57                                            llh_bitmap[0]) - \
58                                   sizeof(struct llog_rec_tail)) * 4) * \
59                                 ((LLOG_MIN_CHUNK_SIZE - \
60                                   offsetof(struct llog_log_hdr, \
61                                            llh_bitmap[0]) - \
62                                   sizeof(struct llog_rec_tail)) * 8))
63 /* min time in seconds between two gc thread runs if none already started */
64 #define CHLOG_MIN_GC_INTERVAL 3600
65 /* minimum number of free ChangeLog catalog entries (ie, between cur and
66  * last indexes) before starting garbage collect */
67 #define CHLOG_MIN_FREE_CAT_ENTRIES 2
68
69 /* Changelog flags */
70 /** changelog is recording */
71 #define CLM_ON    0x00001
72 /** internal error prevented changelogs from starting */
73 #define CLM_ERR   0x00002
74 /* Marker flags */
75 /** changelogs turned on */
76 #define CLM_START 0x10000
77 /** changelogs turned off */
78 #define CLM_FINI  0x20000
79 /** some changelog records purged */
80 #define CLM_PURGE 0x40000
81 /** changelog cleanup done, to prevent double cleanup */
82 #define CLM_CLEANUP_DONE 0x80000
83
84 #define LLOG_CHANGELOG_HDR_SZ (sizeof(struct llog_changelog_rec) - \
85                                sizeof(struct changelog_rec))
86 /* mc_gc_task values */
87 /** no GC thread to be started **/
88 #define MDD_CHLG_GC_NONE NULL
89 /** a GC thread need to be started **/
90 #define MDD_CHLG_GC_NEED (struct task_struct *)(-1)
91 /** a GC thread will be started now **/
92 #define MDD_CHLG_GC_START (struct task_struct *)(-2)
93 /** else the started task_struct address when running **/
94
95 struct mdd_changelog {
96         spinlock_t              mc_lock;        /* for index */
97         int                     mc_flags;
98         __u32                   mc_proc_mask; /* per-server mask set via parameters */
99         __u32                   mc_current_mask; /* combined global+users */
100         __u64                   mc_index;
101         ktime_t                 mc_starttime;
102         spinlock_t              mc_user_lock;
103         int                     mc_lastuser;
104         int                     mc_users;      /* registered users number */
105         struct task_struct      *mc_gc_task;
106         time64_t                mc_gc_time;    /* last GC check or run time */
107         unsigned int            mc_deniednext; /* interval for recording denied
108                                                 * accesses
109                                                 */
110 };
111
112 static inline __u64 cl_time(void)
113 {
114         struct timespec64 time;
115
116         ktime_get_real_ts64(&time);
117         return (((__u64)time.tv_sec) << 30) + time.tv_nsec;
118 }
119
120 /** Objects in .lustre dir */
121 struct mdd_dot_lustre_objs {
122         struct mdd_object *mdd_obf;
123         struct mdd_object *mdd_lpf;
124 };
125
126 struct mdd_generic_thread {
127         struct completion       mgt_started;
128         struct completion       mgt_finished;
129         void                   *mgt_data;
130         bool                    mgt_abort;
131         bool                    mgt_init;
132 };
133
134 struct mdd_device {
135         struct md_device                 mdd_md_dev;
136         struct obd_export               *mdd_child_exp;
137         struct dt_device                *mdd_child;
138         struct dt_device                *mdd_bottom;
139         struct lu_fid                    mdd_root_fid; /* /ROOT */
140         struct lu_fid                    mdd_local_root_fid;
141         struct dt_device_param           mdd_dt_conf;
142         struct dt_object                *mdd_orphans; /* PENDING directory */
143         struct mdd_changelog             mdd_cl;
144         unsigned int                     mdd_changelog_gc;
145         time64_t                         mdd_changelog_max_idle_time;
146         unsigned long                    mdd_changelog_max_idle_indexes;
147         time64_t                         mdd_changelog_min_gc_interval;
148         unsigned int                     mdd_changelog_min_free_cat_entries;
149         time64_t                         mdd_atime_diff;
150         struct mdd_object               *mdd_dot_lustre;
151         struct mdd_dot_lustre_objs       mdd_dot_lustre_objs;
152         unsigned int                     mdd_sync_permission;
153         int                              mdd_connects;
154         int                              mdd_append_stripe_count;
155         char                             mdd_append_pool[LOV_MAXPOOLNAME + 1];
156         struct local_oid_storage        *mdd_los;
157         struct mdd_generic_thread        mdd_orphan_cleanup_thread;
158         struct kobject                   mdd_kobj;
159         struct kobj_type                 mdd_ktype;
160         struct completion                mdd_kobj_unregister;
161 };
162
163 enum mod_flags {
164         /* The dir object has been unlinked */
165         DEAD_OBJ        = BIT(0),
166         ORPHAN_OBJ      = BIT(1),
167         VOLATILE_OBJ    = BIT(4),
168 };
169
170 struct mdd_object {
171         struct md_object        mod_obj;
172         /* open count */
173         u32                     mod_count;
174         u32                     mod_valid;
175         ktime_t                 mod_cltime;
176         unsigned long           mod_flags;
177         struct list_head        mod_users;  /**< unique user opens */
178 };
179
180 #define MTI_KEEP_KEY    0x01
181
182 struct mdd_thread_info {
183         struct lu_fid             mti_fid;
184         struct lu_fid             mti_fid2; /* used for be & cpu converting */
185         /**
186         * only be used by MDD interfaces, can be passed into local MDD APIs.
187         */
188         struct lu_attr            mti_pattr;
189         struct lu_attr            mti_cattr;
190         struct lu_attr            mti_tpattr;
191         struct lu_attr            mti_tattr;
192         /** used to set c/mtime */
193         struct lu_attr            mti_la_for_fix;
194         /* Only used in mdd_object_start */
195         struct lu_attr            mti_la_for_start;
196         /* mti_ent and mti_key must be conjoint,
197         * then mti_ent::lde_name will be mti_key. */
198         struct lu_dirent          mti_ent;
199         char                      mti_key[NAME_MAX + 16];
200         int                       mti_flags;
201         char                      mti_name[NAME_MAX + 1];
202         struct lu_buf             mti_buf[4];
203         /* persistent buffers, must be handled with lu_buf_alloc/free */
204         struct lu_buf             mti_big_buf;
205         struct lu_buf             mti_chlg_buf;
206         struct lu_buf             mti_link_buf; /* buf for link ea */
207         struct lu_buf             mti_xattr_buf;
208         struct obdo               mti_oa;
209         struct dt_allocation_hint mti_hint;
210         struct dt_object_format   mti_dof;
211         struct linkea_data        mti_link_data;
212         struct md_op_spec         mti_spec;
213         struct dt_insert_rec      mti_dt_rec;
214         struct lfsck_req_local    mti_lrl;
215         struct lu_seq_range       mti_range;
216         union lmv_mds_md          mti_lmv;
217         struct md_layout_change   mti_mlc;
218 };
219
220 int mdd_la_get(const struct lu_env *env, struct mdd_object *obj,
221                struct lu_attr *la);
222 int mdd_attr_get(const struct lu_env *env, struct md_object *obj,
223                  struct md_attr *ma);
224 int mdd_attr_set(const struct lu_env *env, struct md_object *obj,
225                  const struct md_attr *ma);
226 int mdd_attr_set_internal(const struct lu_env *env,
227                           struct mdd_object *obj,
228                           const struct lu_attr *attr,
229                           struct thandle *handle,
230                           int needacl);
231 int mdd_update_time(const struct lu_env *env, struct mdd_object *obj,
232                     const struct lu_attr *oattr, struct lu_attr *attr,
233                     struct thandle *handle);
234 int mdd_create_object_internal(const struct lu_env *env, struct mdd_object *p,
235                                struct mdd_object *c, struct lu_attr *attr,
236                                struct thandle *handle,
237                                const struct md_op_spec *spec,
238                                struct dt_allocation_hint *hint);
239
240 /* mdd_lock.c */
241 void mdd_write_lock(const struct lu_env *env, struct mdd_object *obj,
242                     enum dt_object_role role);
243 void mdd_read_lock(const struct lu_env *env, struct mdd_object *obj,
244                    enum dt_object_role role);
245 void mdd_write_unlock(const struct lu_env *env, struct mdd_object *obj);
246 void mdd_read_unlock(const struct lu_env *env, struct mdd_object *obj);
247 int mdd_write_locked(const struct lu_env *env, struct mdd_object *obj);
248
249 /* mdd_dir.c */
250 int mdd_may_create(const struct lu_env *env, struct mdd_object *pobj,
251                    const struct lu_attr *pattr, struct mdd_object *cobj,
252                    bool check_perm);
253 int mdd_may_unlink(const struct lu_env *env, struct mdd_object *pobj,
254                    const struct lu_attr *pattr, const struct lu_attr *attr);
255 int mdd_may_delete(const struct lu_env *env, struct mdd_object *tpobj,
256                    const struct lu_attr *tpattr, struct mdd_object *tobj,
257                    const struct lu_attr *tattr, const struct lu_attr *cattr,
258                    int check_perm, int check_empty);
259 int mdd_unlink_sanity_check(const struct lu_env *env, struct mdd_object *pobj,
260                             const struct lu_attr *pattr,
261                             struct mdd_object *cobj,
262                             const struct lu_attr *cattr);
263 int mdd_finish_unlink(const struct lu_env *env, struct mdd_object *obj,
264                       struct md_attr *ma, struct mdd_object *pobj,
265                       const struct lu_name *lname, struct thandle *th);
266
267 int mdd_is_root(struct mdd_device *mdd, const struct lu_fid *fid);
268 int mdd_lookup(const struct lu_env *env,
269                struct md_object *pobj, const struct lu_name *lname,
270                struct lu_fid* fid, struct md_op_spec *spec);
271 int mdd_links_write(const struct lu_env *env, struct mdd_object *mdd_obj,
272                     struct linkea_data *ldata, struct thandle *handle);
273 int mdd_links_read(const struct lu_env *env,
274                    struct mdd_object *mdd_obj,
275                    struct linkea_data *ldata);
276 struct lu_buf *mdd_links_get(const struct lu_env *env,
277                              struct mdd_object *mdd_obj);
278 int mdd_links_rename(const struct lu_env *env,
279                      struct mdd_object *mdd_obj,
280                      const struct lu_fid *oldpfid,
281                      const struct lu_name *oldlname,
282                      const struct lu_fid *newpfid,
283                      const struct lu_name *newlname,
284                      struct thandle *handle,
285                      struct linkea_data *ldata,
286                      int first, int check);
287 int mdd_dir_layout_shrink(const struct lu_env *env,
288                           struct md_object *md_obj,
289                           struct md_layout_change *mlc);
290 int mdd_dir_layout_split(const struct lu_env *env, struct md_object *o,
291                          struct md_layout_change *mlc);
292
293 int mdd_changelog_write_rec(const struct lu_env *env,
294                             struct llog_handle *loghandle,
295                             struct llog_rec_hdr *rec,
296                             struct llog_cookie *cookie,
297                             int idx, struct thandle *th);
298
299 struct mdd_thread_info *mdd_env_info(const struct lu_env *env);
300
301 #define MDD_ENV_VAR(env, var) (&mdd_env_info(env)->mti_##var)
302
303 struct lu_buf *mdd_buf_get(const struct lu_env *env, void *area, ssize_t len);
304 const struct lu_buf *mdd_buf_get_const(const struct lu_env *env,
305                                        const void *area, ssize_t len);
306
307 int mdd_orphan_cleanup(const struct lu_env *env, struct mdd_device *d);
308 int mdd_orphan_insert(const struct lu_env *env, struct mdd_object *obj,
309                       struct thandle *thandle);
310 int mdd_orphan_delete(const struct lu_env *env, struct mdd_object *obj,
311                       struct thandle *thandle);
312 int mdd_orphan_index_init(const struct lu_env *env, struct mdd_device *mdd);
313 void mdd_orphan_index_fini(const struct lu_env *env, struct mdd_device *mdd);
314 int mdd_orphan_declare_insert(const struct lu_env *env, struct mdd_object *obj,
315                               umode_t mode, struct thandle *thandle);
316 int mdd_orphan_declare_delete(const struct lu_env *env, struct mdd_object *obj,
317                               struct thandle *thandle);
318 int mdd_dir_is_empty(const struct lu_env *env, struct mdd_object *dir);
319
320 /* mdd_lproc.c */
321 int mdd_procfs_init(struct mdd_device *mdd, const char *name);
322 void mdd_procfs_fini(struct mdd_device *mdd);
323
324 /* mdd_object.c */
325 extern struct kmem_cache *mdd_object_kmem;
326 extern const struct md_dir_operations    mdd_dir_ops;
327 extern const struct md_object_operations mdd_obj_ops;
328 int mdd_readlink(const struct lu_env *env, struct md_object *obj,
329                  struct lu_buf *buf);
330 extern struct lu_context_key mdd_thread_key;
331 extern const struct lu_device_operations mdd_lu_ops;
332
333 struct mdd_object *mdd_object_find(const struct lu_env *env,
334                                    struct mdd_device *d,
335                                    const struct lu_fid *f);
336 int mdd_readpage(const struct lu_env *env, struct md_object *obj,
337                  const struct lu_rdpg *rdpg);
338 int mdd_declare_changelog_store(const struct lu_env *env,
339                                 struct mdd_device *mdd,
340                                 enum changelog_rec_type type,
341                                 const struct lu_name *tname,
342                                 const struct lu_name *sname,
343                                 struct thandle *handle);
344 void mdd_changelog_rec_ext_jobid(struct changelog_rec *rec, const char *jobid);
345 void mdd_changelog_rec_ext_extra_flags(struct changelog_rec *rec, __u64 eflags);
346 void mdd_changelog_rec_extra_uidgid(struct changelog_rec *rec,
347                                     __u64 uid, __u64 gid);
348 void mdd_changelog_rec_extra_nid(struct changelog_rec *rec,
349                                  lnet_nid_t nid);
350 void mdd_changelog_rec_extra_omode(struct changelog_rec *rec, u32 flags);
351 void mdd_changelog_rec_extra_xattr(struct changelog_rec *rec,
352                                    const char *xattr_name);
353 int mdd_changelog_store(const struct lu_env *env, struct mdd_device *mdd,
354                         struct llog_changelog_rec *rec, struct thandle *th);
355 int mdd_changelog_data_store(const struct lu_env *env, struct mdd_device *mdd,
356                              enum changelog_rec_type type,
357                              enum changelog_rec_flags clf_flags,
358                              struct mdd_object *mdd_obj,
359                              struct thandle *handle,
360                              const struct lu_fid *pfid);
361 int mdd_changelog_ns_store(const struct lu_env *env, struct mdd_device *mdd,
362                            enum changelog_rec_type type,
363                            enum changelog_rec_flags clf_flags,
364                            struct mdd_object *target,
365                            const struct lu_fid *tpfid,
366                            const struct lu_fid *sfid,
367                            const struct lu_fid *spfid,
368                            const struct lu_name *tname,
369                            const struct lu_name *sname,
370                            struct thandle *handle);
371 int mdd_invalidate(const struct lu_env *env, struct md_object *obj);
372 int mdd_declare_create_object_internal(const struct lu_env *env,
373                                        struct mdd_object *p,
374                                        struct mdd_object *c,
375                                        struct lu_attr *attr,
376                                        struct thandle *handle,
377                                        const struct md_op_spec *spec,
378                                        struct dt_allocation_hint *hint);
379 int mdd_stripe_get(const struct lu_env *env, struct mdd_object *obj,
380                    struct lu_buf *lmm_buf, const char *name);
381 int mdd_changelog_data_store_xattr(const struct lu_env *env,
382                                    struct mdd_device *mdd,
383                                    enum changelog_rec_type type,
384                                    enum changelog_rec_flags clf_flags,
385                                    struct mdd_object *mdd_obj,
386                                    const char *xattr_name,
387                                    struct thandle *handle);
388
389 /* mdd_trans.c */
390 void mdd_object_make_hint(const struct lu_env *env, struct mdd_object *parent,
391                           struct mdd_object *child, const struct lu_attr *attr,
392                           const struct md_op_spec *spec,
393                           struct dt_allocation_hint *hint);
394
395 static inline void mdd_object_get(struct mdd_object *o)
396 {
397         lu_object_get(&o->mod_obj.mo_lu);
398 }
399
400 static inline void mdd_object_put(const struct lu_env *env,
401                                   struct mdd_object *o)
402 {
403         lu_object_put(env, &o->mod_obj.mo_lu);
404 }
405
406 struct thandle *mdd_trans_create(const struct lu_env *env,
407                                  struct mdd_device *mdd);
408 int mdd_trans_start(const struct lu_env *env, struct mdd_device *mdd,
409                     struct thandle *th);
410 int mdd_trans_stop(const struct lu_env *env, struct mdd_device *mdd,
411                    int rc, struct thandle *handle);
412
413 /* mdd_device.c */
414 struct lu_object *mdd_object_alloc(const struct lu_env *env,
415                                    const struct lu_object_header *hdr,
416                                    struct lu_device *d);
417 int mdd_local_file_create(const struct lu_env *env, struct mdd_device *mdd,
418                           const struct lu_fid *pfid, const char *name,
419                           __u32 mode, struct lu_fid *fid);
420
421 int mdd_acl_chmod(const struct lu_env *env, struct mdd_object *o, __u32 mode,
422                   struct thandle *handle);
423 int mdd_acl_set(const struct lu_env *env, struct mdd_object *obj,
424                 struct lu_attr *attr, const struct lu_buf *buf, int fl);
425 int __mdd_fix_mode_acl(const struct lu_env *env, struct lu_buf *buf,
426                        __u32 *mode);
427 int __mdd_permission_internal(const struct lu_env *env, struct mdd_object *obj,
428                               const struct lu_attr *la, unsigned int may_mask,
429                               int role);
430 int mdd_permission(const struct lu_env *env, struct md_object *pobj,
431                    struct md_object *cobj, struct md_attr *ma,
432                    unsigned int may_mask);
433 int mdd_generic_thread_start(struct mdd_generic_thread *thread,
434                              int (*func)(void *), void *data, char *name);
435 void mdd_generic_thread_stop(struct mdd_generic_thread *thread);
436 int mdd_changelog_user_purge(const struct lu_env *env, struct mdd_device *mdd,
437                              __u32 id);
438 char *mdd_chlg_username(struct llog_changelog_user_rec2 *rec, char *buf,
439                         size_t len);
440 __u32 mdd_chlg_usermask(struct llog_changelog_user_rec2 *rec);
441 int mdd_changelog_recalc_mask(const struct lu_env *env, struct mdd_device *mdd);
442
443 /* mdd_prepare.c */
444 int mdd_compat_fixes(const struct lu_env *env, struct mdd_device *mdd);
445
446 /* acl.c */
447 extern int lustre_posix_acl_permission(struct lu_ucred *mu,
448                                        const struct lu_attr *la,
449                                        unsigned int may_mask,
450                                        posix_acl_xattr_entry *entry,
451                                        int count);
452 extern int lustre_posix_acl_chmod_masq(posix_acl_xattr_entry *entry,
453                                        __u32 mode, int count);
454 extern int lustre_posix_acl_create_masq(posix_acl_xattr_entry *entry,
455                                         __u32 *pmode, int count);
456 extern int lustre_posix_acl_equiv_mode(posix_acl_xattr_entry *entry,
457                                        mode_t *mode_p, int count);
458
459 /* inline functions */
460 static inline int lu_device_is_mdd(struct lu_device *d)
461 {
462         return ergo(d != NULL && d->ld_ops != NULL, d->ld_ops == &mdd_lu_ops);
463 }
464
465 static inline struct mdd_device *lu2mdd_dev(struct lu_device *d)
466 {
467         LASSERT(lu_device_is_mdd(d));
468         return container_of_safe(d, struct mdd_device, mdd_md_dev.md_lu_dev);
469 }
470
471 static inline struct lu_device *mdd2lu_dev(struct mdd_device *mdd)
472 {
473         return &mdd->mdd_md_dev.md_lu_dev;
474 }
475
476 static inline struct mdd_object *lu2mdd_obj(struct lu_object *o)
477 {
478         LASSERT(ergo(o != NULL, lu_device_is_mdd(o->lo_dev)));
479         return container_of_safe(o, struct mdd_object,
480                                  mod_obj.mo_lu);
481 }
482
483 static inline struct mdd_device *mdo2mdd(struct md_object *mdo)
484 {
485         return lu2mdd_dev(mdo->mo_lu.lo_dev);
486 }
487
488 static inline struct mdd_object *md2mdd_obj(struct md_object *mdo)
489 {
490         return container_of_safe(mdo, struct mdd_object, mod_obj);
491 }
492
493 static inline const
494 struct dt_device_operations *mdd_child_ops(struct mdd_device *mdd)
495 {
496         return mdd->mdd_child->dd_ops;
497 }
498
499 static inline struct lu_object *mdd2lu_obj(struct mdd_object *obj)
500 {
501         return &obj->mod_obj.mo_lu;
502 }
503
504 static inline struct dt_object *mdd_object_child(struct mdd_object *obj)
505 {
506         return container_of(lu_object_next(mdd2lu_obj(obj)),
507                             struct dt_object, do_lu);
508 }
509
510 static inline struct obd_device *mdd2obd_dev(struct mdd_device *mdd)
511 {
512         return (mdd->mdd_md_dev.md_lu_dev.ld_obd);
513 }
514
515 static inline struct mdd_device *mdd_obj2mdd_dev(struct mdd_object *obj)
516 {
517         return mdo2mdd(&obj->mod_obj);
518 }
519
520 static inline umode_t mdd_object_type(const struct mdd_object *mdd_obj)
521 {
522         return lu_object_attr(&mdd_obj->mod_obj.mo_lu);
523 }
524
525 static inline int mdd_is_dead_obj(struct mdd_object *mdd_obj)
526 {
527         return mdd_obj && mdd_obj->mod_flags & DEAD_OBJ;
528 }
529
530 static inline bool mdd_is_volatile_obj(struct mdd_object *mdd_obj)
531 {
532         return mdd_obj->mod_flags & VOLATILE_OBJ;
533 }
534
535 static inline bool mdd_is_orphan_obj(struct mdd_object *mdd_obj)
536 {
537         return mdd_obj->mod_flags & ORPHAN_OBJ;
538 }
539
540 static inline int mdd_object_exists(struct mdd_object *mdd_obj)
541 {
542         return lu_object_exists(mdd2lu_obj(mdd_obj));
543 }
544
545 static inline int mdd_object_remote(struct mdd_object *mdd_obj)
546 {
547         return lu_object_remote(mdd2lu_obj(mdd_obj));
548 }
549
550 static inline const struct lu_fid *mdd_object_fid(struct mdd_object *mdd_obj)
551 {
552         return lu_object_fid(mdd2lu_obj(mdd_obj));
553 }
554
555 static inline struct seq_server_site *mdd_seq_site(struct mdd_device *mdd)
556 {
557         return mdd2lu_dev(mdd)->ld_site->ld_seq_site;
558 }
559
560 static inline const char *mdd_obj_dev_name(const struct mdd_object *mdd_obj)
561 {
562         return lu_dev_name(mdd_obj->mod_obj.mo_lu.lo_dev);
563 }
564
565 #define MAX_ATIME_DIFF 60
566
567 static inline int mdd_permission_internal(const struct lu_env *env,
568                                           struct mdd_object *obj,
569                                           const struct lu_attr *la,
570                                           unsigned int may_mask)
571 {
572         return __mdd_permission_internal(env, obj, la, may_mask, -1);
573 }
574
575 static inline int mdd_permission_internal_locked(const struct lu_env *env,
576                                                 struct mdd_object *obj,
577                                                 const struct lu_attr *la,
578                                                 unsigned int may_mask,
579                                                 enum dt_object_role role)
580 {
581         return __mdd_permission_internal(env, obj, la, may_mask, role);
582 }
583
584 /* mdd inline func for calling osd_dt_object ops */
585 static inline int mdo_attr_get(const struct lu_env *env, struct mdd_object *obj,
586                                struct lu_attr *la)
587 {
588         struct dt_object *next = mdd_object_child(obj);
589         return dt_attr_get(env, next, la);
590 }
591
592 static inline int mdo_declare_attr_set(const struct lu_env *env,
593                                        struct mdd_object *obj,
594                                        const struct lu_attr *la,
595                                        struct thandle *handle)
596 {
597         struct dt_object *next = mdd_object_child(obj);
598         return dt_declare_attr_set(env, next, la, handle);
599 }
600
601 static inline int mdo_attr_set(const struct lu_env *env,
602                                struct mdd_object *obj,
603                                const struct lu_attr *la,
604                                struct thandle *handle)
605 {
606         struct dt_object *next = mdd_object_child(obj);
607
608         if (!mdd_object_exists(obj))
609                 return -ENOENT;
610
611         return dt_attr_set(env, next, la, handle);
612 }
613
614 static inline int mdo_xattr_get(const struct lu_env *env,struct mdd_object *obj,
615                                 struct lu_buf *buf, const char *name)
616 {
617         struct dt_object *next = mdd_object_child(obj);
618         return dt_xattr_get(env, next, buf, name);
619 }
620
621 static inline int mdo_declare_xattr_set(const struct lu_env *env,
622                                         struct mdd_object *obj,
623                                         const struct lu_buf *buf,
624                                         const char *name,
625                                         int fl, struct thandle *handle)
626 {
627         struct dt_object *next = mdd_object_child(obj);
628         int rc;
629
630         rc = dt_declare_xattr_set(env, next, buf, name, fl, handle);
631         if (rc >= 0 && strcmp(name, LL_XATTR_NAME_ENCRYPTION_CONTEXT) == 0) {
632                 struct lu_attr la = { 0 };
633
634                 la.la_valid = LA_FLAGS;
635                 la.la_flags = LUSTRE_ENCRYPT_FL;
636                 rc = dt_declare_attr_set(env, next, &la, handle);
637         }
638         return rc;
639 }
640
641 static inline int mdo_xattr_set(const struct lu_env *env,struct mdd_object *obj,
642                                 const struct lu_buf *buf, const char *name,
643                                 int fl, struct thandle *handle)
644 {
645         struct dt_object *next = mdd_object_child(obj);
646         int rc = 0;
647
648         if (!mdd_object_exists(obj))
649                 return -ENOENT;
650
651         /* If we are about to set the LL_XATTR_NAME_ENCRYPTION_CONTEXT
652          * xattr, it means the file/dir is encrypted. In that case we want
653          * to set the LUSTRE_ENCRYPT_FL flag as well: it will be stored
654          * into the LMA, making it more efficient to recognise we are
655          * dealing with an encrypted file/dir, as LMA info is cached upon
656          * object init.
657          * However, marking a dir as encrypted is only possible if it is
658          * being created or migrated (LU_XATTR_CREATE flag not set), or
659          * if it is empty.
660          */
661         if ((strcmp(name, LL_XATTR_NAME_ENCRYPTION_CONTEXT) == 0) &&
662             (!S_ISDIR(mdd_object_type(obj)) ||
663              !(fl & LU_XATTR_CREATE) ||
664              (rc = mdd_dir_is_empty(env, obj)) == 0)) {
665                 struct lu_attr la = { 0 };
666
667                 la.la_valid = LA_FLAGS;
668                 la.la_flags = LUSTRE_ENCRYPT_FL;
669                 rc = dt_attr_set(env, next, &la, handle);
670         }
671         if (rc >= 0)
672                 rc = dt_xattr_set(env, next, buf, name, fl, handle);
673
674         return rc;
675 }
676
677 static inline int mdo_declare_xattr_del(const struct lu_env *env,
678                                         struct mdd_object *obj,
679                                         const char *name,
680                                         struct thandle *handle)
681 {
682         struct dt_object *next = mdd_object_child(obj);
683         return dt_declare_xattr_del(env, next, name, handle);
684 }
685
686 static inline int mdo_xattr_del(const struct lu_env *env,struct mdd_object *obj,
687                                 const char *name, struct thandle *handle)
688 {
689         struct dt_object *next = mdd_object_child(obj);
690
691         if (!mdd_object_exists(obj))
692                 return -ENOENT;
693
694         return dt_xattr_del(env, next, name, handle);
695 }
696
697 static inline int
698 mdo_xattr_list(const struct lu_env *env, struct mdd_object *obj,
699                struct lu_buf *buf)
700 {
701         struct dt_object *next = mdd_object_child(obj);
702
703         if (!mdd_object_exists(obj))
704                 return -ENOENT;
705
706         return dt_xattr_list(env, next, buf);
707 }
708
709 static inline int
710 mdo_invalidate(const struct lu_env *env, struct mdd_object *obj)
711 {
712         return dt_invalidate(env, mdd_object_child(obj));
713 }
714
715 static inline int
716 mdo_declare_layout_change(const struct lu_env *env, struct mdd_object *obj,
717                           struct md_layout_change *mlc, struct thandle *handle)
718 {
719         return dt_declare_layout_change(env, mdd_object_child(obj),
720                                         mlc, handle);
721 }
722
723 static inline int
724 mdo_layout_change(const struct lu_env *env, struct mdd_object *obj,
725                   struct md_layout_change *mlc, struct thandle *handle)
726 {
727         return dt_layout_change(env, mdd_object_child(obj), mlc, handle);
728 }
729
730 static inline
731 int mdo_declare_index_insert(const struct lu_env *env, struct mdd_object *obj,
732                              const struct lu_fid *fid, __u32 type,
733                              const char *name, struct thandle *handle)
734 {
735         struct dt_object *next  = mdd_object_child(obj);
736         int               rc;
737
738         /*
739          * if the object doesn't exist yet, then it's supposed to be created
740          * and declaration of the creation should be enough to insert ./..
741          */
742
743         rc = -ENOTDIR;
744         if (dt_try_as_dir(env, next)) {
745                 struct dt_insert_rec *rec = &mdd_env_info(env)->mti_dt_rec;
746
747                 rec->rec_fid = fid;
748                 rec->rec_type = type;
749                 rc = dt_declare_insert(env, next, (const struct dt_rec *)rec,
750                                        (const struct dt_key *)name, handle);
751         }
752
753          return rc;
754 }
755
756 static inline
757 int mdo_declare_index_delete(const struct lu_env *env, struct mdd_object *obj,
758                              const char *name, struct thandle *handle)
759 {
760         struct dt_object *next = mdd_object_child(obj);
761
762         if (!dt_try_as_dir(env, next))
763                 return -ENOTDIR;
764
765         return dt_declare_delete(env, next, (const struct dt_key *)name,
766                                  handle);
767 }
768
769 static inline int mdo_declare_ref_add(const struct lu_env *env,
770                                       struct mdd_object *obj,
771                                       struct thandle *handle)
772 {
773         struct dt_object *next = mdd_object_child(obj);
774         return dt_declare_ref_add(env, next, handle);
775 }
776
777 static inline int mdo_ref_add(const struct lu_env *env, struct mdd_object *obj,
778                               struct thandle *handle)
779 {
780         struct dt_object *next = mdd_object_child(obj);
781
782         if (!mdd_object_exists(obj))
783                 return -ENOENT;
784
785         return dt_ref_add(env, next, handle);
786 }
787
788 static inline int mdo_declare_ref_del(const struct lu_env *env,
789                                       struct mdd_object *obj,
790                                       struct thandle *handle)
791 {
792         struct dt_object *next = mdd_object_child(obj);
793         return dt_declare_ref_del(env, next, handle);
794 }
795
796 static inline int mdo_ref_del(const struct lu_env *env, struct mdd_object *obj,
797                               struct thandle *handle)
798 {
799         struct dt_object *next = mdd_object_child(obj);
800
801         if (!mdd_object_exists(obj))
802                 return -ENOENT;
803
804         return dt_ref_del(env, next, handle);
805 }
806
807 static inline int
808 mdo_declare_create_object(const struct lu_env *env, struct mdd_object *obj,
809                           struct lu_attr *attr, struct dt_allocation_hint *hint,
810                           struct dt_object_format *dof, struct thandle *handle)
811 {
812         struct dt_object *next = mdd_object_child(obj);
813         return dt_declare_create(env, next, attr, hint, dof, handle);
814 }
815
816 static inline int
817 mdo_create_object(const struct lu_env *env, struct mdd_object *obj,
818                   struct lu_attr *attr, struct dt_allocation_hint *hint,
819                   struct dt_object_format *dof, struct thandle *handle)
820 {
821         struct dt_object *next = mdd_object_child(obj);
822         return dt_create(env, next, attr, hint, dof, handle);
823 }
824
825 static inline
826 int mdo_declare_destroy(const struct lu_env *env, struct mdd_object *o,
827                         struct thandle *handle)
828 {
829         struct dt_object *next = mdd_object_child(o);
830         return dt_declare_destroy(env, next, handle);
831 }
832
833 static inline
834 int mdo_destroy(const struct lu_env *env, struct mdd_object *o,
835                 struct thandle *handle)
836 {
837         struct dt_object *next = mdd_object_child(o);
838         return dt_destroy(env, next, handle);
839 }
840
841 static inline bool mdd_changelog_enabled(const struct lu_env *env,
842                                         struct mdd_device *mdd,
843                                         enum changelog_rec_type type)
844 {
845         const struct lu_ucred *uc;
846
847         if ((mdd->mdd_cl.mc_flags & CLM_ON) &&
848             (mdd->mdd_cl.mc_current_mask & BIT(type))) {
849                 uc = lu_ucred_check(env);
850
851                 return uc != NULL ? uc->uc_enable_audit : true;
852         } else {
853                 return false;
854         }
855 }
856
857 #endif