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