Whamcloud - gitweb
e8e00f32e420b4c992b35af9cabef5cc8c42dab7
[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         __u32                   mc_mintime; /* the oldest changelog user time */
101         __u64                   mc_minrec; /* last known minimal used index */
102         __u64                   mc_index;
103         ktime_t                 mc_starttime;
104         spinlock_t              mc_user_lock;
105         int                     mc_lastuser;
106         int                     mc_users;      /* registered users number */
107         struct task_struct      *mc_gc_task;
108         time64_t                mc_gc_time;    /* last GC check or run time */
109         unsigned int            mc_deniednext; /* interval for recording denied
110                                                 * accesses
111                                                 */
112         unsigned char           mc_enable_shard_pfid; /* master or shard pFID
113                                                        * for striped dirs
114                                                        */
115 };
116
117 static inline __u64 cl_time(void)
118 {
119         struct timespec64 time;
120
121         ktime_get_real_ts64(&time);
122         return (((__u64)time.tv_sec) << 30) + time.tv_nsec;
123 }
124
125 /** Objects in .lustre dir */
126 struct mdd_dot_lustre_objs {
127         struct mdd_object *mdd_obf;
128         struct mdd_object *mdd_lpf;
129 };
130
131 struct mdd_generic_thread {
132         struct completion       mgt_started;
133         struct completion       mgt_finished;
134         void                   *mgt_data;
135         bool                    mgt_abort;
136         bool                    mgt_init;
137 };
138
139 struct mdd_device {
140         struct md_device                 mdd_md_dev;
141         struct obd_export               *mdd_child_exp;
142         struct dt_device                *mdd_child;
143         struct dt_device                *mdd_bottom;
144         struct lu_fid                    mdd_root_fid; /* /ROOT */
145         struct lu_fid                    mdd_local_root_fid;
146         struct dt_device_param           mdd_dt_conf;
147         struct dt_object                *mdd_orphans; /* PENDING directory */
148         struct mdd_changelog             mdd_cl;
149         unsigned int                     mdd_changelog_gc;
150                                          /* emrg GC is in progress */
151         bool                             mdd_changelog_emrg_gc;
152                                          /* don't use GC by free space */
153         bool                             mdd_changelog_free_space_gc;
154         time64_t                         mdd_changelog_max_idle_time;
155         unsigned long                    mdd_changelog_max_idle_indexes;
156         time64_t                         mdd_changelog_min_gc_interval;
157         unsigned int                     mdd_changelog_min_free_cat_entries;
158         time64_t                         mdd_atime_diff;
159         struct mdd_object               *mdd_dot_lustre;
160         struct mdd_dot_lustre_objs       mdd_dot_lustre_objs;
161         unsigned int                     mdd_sync_permission;
162         int                              mdd_connects;
163         int                              mdd_append_stripe_count;
164         char                             mdd_append_pool[LOV_MAXPOOLNAME + 1];
165         struct local_oid_storage        *mdd_los;
166         struct mdd_generic_thread        mdd_orphan_cleanup_thread;
167         struct kobject                   mdd_kobj;
168         struct kobj_type                 mdd_ktype;
169         struct completion                mdd_kobj_unregister;
170 };
171
172 enum mod_flags {
173         /* The dir object has been unlinked */
174         DEAD_OBJ        = BIT(0),
175         ORPHAN_OBJ      = BIT(1),
176         VOLATILE_OBJ    = BIT(4),
177 };
178
179 struct mdd_object {
180         struct md_object        mod_obj;
181         struct lu_fid           mod_striped_pfid; /* master dir parent FID, in
182                                                    * case this is a striped dir
183                                                    */
184         /* open count */
185         u32                     mod_count;
186         u32                     mod_valid;
187         ktime_t                 mod_cltime;
188         unsigned long           mod_flags;
189         struct list_head        mod_users;  /**< unique user opens */
190 };
191
192 #define MDI_KEEP_KEY    0x01
193
194 struct mdd_thread_info {
195         struct lu_fid             mdi_fid;
196         struct lu_fid             mdi_fid2; /* used for be & cpu converting */
197         /**
198         * only be used by MDD interfaces, can be passed into local MDD APIs.
199         */
200         struct lu_attr            mdi_pattr;
201         struct lu_attr            mdi_cattr;
202         struct lu_attr            mdi_tpattr;
203         struct lu_attr            mdi_tattr;
204         /** used to set ctime/mtime */
205         struct lu_attr            mdi_la_for_fix;
206         /* Only used in mdd_object_start */
207         struct lu_attr            mdi_la_for_start;
208         /* mdi_ent/mdi_key must be together so mdi_ent::lde_name is mdi_key */
209         struct lu_dirent          mdi_ent;
210         char                      mdi_key[NAME_MAX + 16];
211         int                       mdi_flags;
212         char                      mdi_name[NAME_MAX + 1];
213         struct lu_buf             mdi_buf[4];
214         /* persistent buffers, must be handled with lu_buf_alloc/free */
215         struct lu_buf             mdi_big_buf;
216         struct lu_buf             mdi_chlg_buf;
217         struct lu_buf             mdi_link_buf; /* buf for link ea */
218         struct lu_buf             mdi_xattr_buf;
219         struct obdo               mdi_oa;
220         struct dt_allocation_hint mdi_hint;
221         struct dt_object_format   mdi_dof;
222         struct linkea_data        mdi_link_data;
223         struct md_op_spec         mdi_spec;
224         struct dt_insert_rec      mdi_dt_rec;
225         struct lu_seq_range       mdi_range;
226         struct md_layout_change   mdi_mlc;
227 };
228
229 int mdd_la_get(const struct lu_env *env, struct mdd_object *obj,
230                struct lu_attr *la);
231 int mdd_attr_get(const struct lu_env *env, struct md_object *obj,
232                  struct md_attr *ma);
233 int mdd_attr_set(const struct lu_env *env, struct md_object *obj,
234                  const struct md_attr *ma);
235 int mdd_attr_set_internal(const struct lu_env *env,
236                           struct mdd_object *obj,
237                           const struct lu_attr *attr,
238                           struct thandle *handle,
239                           int needacl);
240 int mdd_update_time(const struct lu_env *env, struct mdd_object *obj,
241                     const struct lu_attr *oattr, struct lu_attr *attr,
242                     struct thandle *handle);
243 int mdd_create(const struct lu_env *env, struct md_object *pobj,
244                       const struct lu_name *lname, struct md_object *child,
245                       struct md_op_spec *spec, struct md_attr *ma);
246 int mdd_create_object_internal(const struct lu_env *env, struct mdd_object *p,
247                                struct mdd_object *c, struct lu_attr *attr,
248                                struct thandle *handle,
249                                const struct md_op_spec *spec,
250                                struct dt_allocation_hint *hint);
251
252 /* mdd_lock.c */
253 void mdd_write_lock(const struct lu_env *env, struct mdd_object *obj,
254                     enum dt_object_role role);
255 void mdd_read_lock(const struct lu_env *env, struct mdd_object *obj,
256                    enum dt_object_role role);
257 void mdd_write_unlock(const struct lu_env *env, struct mdd_object *obj);
258 void mdd_read_unlock(const struct lu_env *env, struct mdd_object *obj);
259 int mdd_write_locked(const struct lu_env *env, struct mdd_object *obj);
260
261 /* mdd_dir.c */
262 int mdd_may_create(const struct lu_env *env, struct mdd_object *pobj,
263                    const struct lu_attr *pattr, struct mdd_object *cobj,
264                    bool check_perm);
265 int mdd_may_unlink(const struct lu_env *env, struct mdd_object *pobj,
266                    const struct lu_attr *pattr, const struct lu_attr *attr);
267 int mdd_may_delete(const struct lu_env *env, struct mdd_object *tpobj,
268                    const struct lu_attr *tpattr, struct mdd_object *tobj,
269                    const struct lu_attr *tattr, const struct lu_attr *cattr,
270                    int check_perm, int check_empty);
271 int mdd_unlink_sanity_check(const struct lu_env *env, struct mdd_object *pobj,
272                             const struct lu_attr *pattr,
273                             struct mdd_object *cobj,
274                             const struct lu_attr *cattr);
275 int mdd_finish_unlink(const struct lu_env *env, struct mdd_object *obj,
276                       struct md_attr *ma, struct mdd_object *pobj,
277                       const struct lu_name *lname, struct thandle *th);
278
279 int mdd_is_root(struct mdd_device *mdd, const struct lu_fid *fid);
280 int mdd_lookup(const struct lu_env *env,
281                struct md_object *pobj, const struct lu_name *lname,
282                struct lu_fid* fid, struct md_op_spec *spec);
283 int mdd_links_write(const struct lu_env *env, struct mdd_object *mdd_obj,
284                     struct linkea_data *ldata, struct thandle *handle);
285 int mdd_links_read(const struct lu_env *env,
286                    struct mdd_object *mdd_obj,
287                    struct linkea_data *ldata);
288 struct lu_buf *mdd_links_get(const struct lu_env *env,
289                              struct mdd_object *mdd_obj);
290 int mdd_links_rename(const struct lu_env *env,
291                      struct mdd_object *mdd_obj,
292                      const struct lu_fid *oldpfid,
293                      const struct lu_name *oldlname,
294                      const struct lu_fid *newpfid,
295                      const struct lu_name *newlname,
296                      struct thandle *handle,
297                      struct linkea_data *ldata,
298                      int first, int check);
299 int mdd_dir_layout_shrink(const struct lu_env *env,
300                           struct md_object *md_obj,
301                           struct md_layout_change *mlc);
302 int mdd_dir_layout_split(const struct lu_env *env, struct md_object *o,
303                          struct md_layout_change *mlc);
304
305 int mdd_changelog_write_rec(const struct lu_env *env,
306                             struct llog_handle *loghandle,
307                             struct llog_rec_hdr *rec,
308                             struct llog_cookie *cookie,
309                             int idx, struct thandle *th);
310
311 struct mdd_thread_info *mdd_env_info(const struct lu_env *env);
312
313 #define MDD_ENV_VAR(env, var) (&mdd_env_info(env)->mdi_##var)
314
315 struct lu_buf *mdd_buf_get(const struct lu_env *env, void *area, ssize_t len);
316 const struct lu_buf *mdd_buf_get_const(const struct lu_env *env,
317                                        const void *area, ssize_t len);
318
319 int mdd_orphan_cleanup(const struct lu_env *env, struct mdd_device *d);
320 int mdd_orphan_insert(const struct lu_env *env, struct mdd_object *obj,
321                       struct thandle *thandle);
322 int mdd_orphan_delete(const struct lu_env *env, struct mdd_object *obj,
323                       struct thandle *thandle);
324 int mdd_orphan_index_init(const struct lu_env *env, struct mdd_device *mdd);
325 void mdd_orphan_index_fini(const struct lu_env *env, struct mdd_device *mdd);
326 int mdd_orphan_declare_insert(const struct lu_env *env, struct mdd_object *obj,
327                               umode_t mode, struct thandle *thandle);
328 int mdd_orphan_declare_delete(const struct lu_env *env, struct mdd_object *obj,
329                               struct thandle *thandle);
330 int mdd_dir_is_empty(const struct lu_env *env, struct mdd_object *dir);
331
332 /* mdd_lproc.c */
333 int mdd_procfs_init(struct mdd_device *mdd, const char *name);
334 void mdd_procfs_fini(struct mdd_device *mdd);
335
336 /* mdd_object.c */
337 extern struct kmem_cache *mdd_object_kmem;
338 extern const struct md_dir_operations    mdd_dir_ops;
339 extern const struct md_object_operations mdd_obj_ops;
340 int mdd_readlink(const struct lu_env *env, struct md_object *obj,
341                  struct lu_buf *buf);
342 extern struct lu_context_key mdd_thread_key;
343 extern const struct lu_device_operations mdd_lu_ops;
344
345 struct mdd_object *mdd_object_find(const struct lu_env *env,
346                                    struct mdd_device *d,
347                                    const struct lu_fid *f);
348 int mdd_readpage(const struct lu_env *env, struct md_object *obj,
349                  const struct lu_rdpg *rdpg);
350 int mdd_declare_changelog_store(const struct lu_env *env,
351                                 struct mdd_device *mdd,
352                                 enum changelog_rec_type type,
353                                 const struct lu_name *tname,
354                                 const struct lu_name *sname,
355                                 struct thandle *handle);
356 void mdd_changelog_rec_ext_jobid(struct changelog_rec *rec, const char *jobid);
357 void mdd_changelog_rec_ext_extra_flags(struct changelog_rec *rec, __u64 eflags);
358 void mdd_changelog_rec_extra_uidgid(struct changelog_rec *rec,
359                                     __u64 uid, __u64 gid);
360 void mdd_changelog_rec_extra_nid(struct changelog_rec *rec,
361                                  lnet_nid_t nid);
362 void mdd_changelog_rec_extra_omode(struct changelog_rec *rec, u32 flags);
363 void mdd_changelog_rec_extra_xattr(struct changelog_rec *rec,
364                                    const char *xattr_name);
365 int mdd_changelog_store(const struct lu_env *env, struct mdd_device *mdd,
366                         struct llog_changelog_rec *rec, struct thandle *th);
367 int mdd_changelog_data_store(const struct lu_env *env, struct mdd_device *mdd,
368                              enum changelog_rec_type type,
369                              enum changelog_rec_flags clf_flags,
370                              struct mdd_object *mdd_obj,
371                              struct thandle *handle,
372                              const struct lu_fid *pfid);
373 int mdd_changelog_ns_store(const struct lu_env *env, struct mdd_device *mdd,
374                            enum changelog_rec_type type,
375                            enum changelog_rec_flags clf_flags,
376                            struct mdd_object *target,
377                            struct mdd_object *parent,
378                            const struct lu_attr *pattr,
379                            const struct lu_fid *sfid,
380                            struct mdd_object *src_parent,
381                            const struct lu_attr *src_pattr,
382                            const struct lu_name *tname,
383                            const struct lu_name *sname,
384                            struct thandle *handle);
385 int mdd_invalidate(const struct lu_env *env, struct md_object *obj);
386 int mdd_declare_create_object_internal(const struct lu_env *env,
387                                        struct mdd_object *p,
388                                        struct mdd_object *c,
389                                        struct lu_attr *attr,
390                                        struct thandle *handle,
391                                        const struct md_op_spec *spec,
392                                        struct dt_allocation_hint *hint);
393 int mdd_stripe_get(const struct lu_env *env, struct mdd_object *obj,
394                    struct lu_buf *lmm_buf, const char *name);
395 int mdd_changelog_data_store_xattr(const struct lu_env *env,
396                                    struct mdd_device *mdd,
397                                    enum changelog_rec_type type,
398                                    enum changelog_rec_flags clf_flags,
399                                    struct mdd_object *mdd_obj,
400                                    const char *xattr_name,
401                                    struct thandle *handle);
402 int mdd_dom_fixup(const struct lu_env *env, struct mdd_device *mdd,
403                   struct mdd_object *mo, struct mdd_object *vo);
404
405 /* mdd_trans.c */
406 void mdd_object_make_hint(const struct lu_env *env, struct mdd_object *parent,
407                           struct mdd_object *child, const struct lu_attr *attr,
408                           const struct md_op_spec *spec,
409                           struct dt_allocation_hint *hint);
410
411 static inline void mdd_object_get(struct mdd_object *o)
412 {
413         lu_object_get(&o->mod_obj.mo_lu);
414 }
415
416 static inline void mdd_object_put(const struct lu_env *env,
417                                   struct mdd_object *o)
418 {
419         lu_object_put(env, &o->mod_obj.mo_lu);
420 }
421
422 struct thandle *mdd_trans_create(const struct lu_env *env,
423                                  struct mdd_device *mdd);
424 int mdd_trans_start(const struct lu_env *env, struct mdd_device *mdd,
425                     struct thandle *th);
426 int mdd_trans_stop(const struct lu_env *env, struct mdd_device *mdd,
427                    int rc, struct thandle *handle);
428
429 /* mdd_device.c */
430 struct lu_object *mdd_object_alloc(const struct lu_env *env,
431                                    const struct lu_object_header *hdr,
432                                    struct lu_device *d);
433 int mdd_local_file_create(const struct lu_env *env, struct mdd_device *mdd,
434                           const struct lu_fid *pfid, const char *name,
435                           __u32 mode, struct lu_fid *fid);
436
437 int mdd_acl_chmod(const struct lu_env *env, struct mdd_object *o, __u32 mode,
438                   struct thandle *handle);
439 int mdd_acl_set(const struct lu_env *env, struct mdd_object *obj,
440                 struct lu_attr *attr, const struct lu_buf *buf, int fl);
441 int __mdd_fix_mode_acl(const struct lu_env *env, struct lu_buf *buf,
442                        __u32 *mode);
443 int __mdd_permission_internal(const struct lu_env *env, struct mdd_object *obj,
444                               const struct lu_attr *la, unsigned int may_mask,
445                               int role);
446 int mdd_permission(const struct lu_env *env, struct md_object *pobj,
447                    struct md_object *cobj, struct md_attr *ma,
448                    unsigned int may_mask);
449 int mdd_generic_thread_start(struct mdd_generic_thread *thread,
450                              int (*func)(void *), void *data, char *name);
451 void mdd_generic_thread_stop(struct mdd_generic_thread *thread);
452 int mdd_changelog_user_purge(const struct lu_env *env, struct mdd_device *mdd,
453                              __u32 id);
454 char *mdd_chlg_username(struct llog_changelog_user_rec2 *rec, char *buf,
455                         size_t len);
456 __u32 mdd_chlg_usermask(struct llog_changelog_user_rec2 *rec);
457 int mdd_changelog_recalc_mask(const struct lu_env *env, struct mdd_device *mdd);
458
459 /* mdd_prepare.c */
460 int mdd_compat_fixes(const struct lu_env *env, struct mdd_device *mdd);
461
462 /* acl.c */
463 extern int lustre_posix_acl_permission(struct lu_ucred *mu,
464                                        const struct lu_attr *la,
465                                        unsigned int may_mask,
466                                        posix_acl_xattr_entry *entry,
467                                        int count);
468 extern int lustre_posix_acl_chmod_masq(posix_acl_xattr_entry *entry,
469                                        __u32 mode, int count);
470 extern int lustre_posix_acl_create_masq(posix_acl_xattr_entry *entry,
471                                         __u32 *pmode, int count);
472 extern int lustre_posix_acl_equiv_mode(posix_acl_xattr_entry *entry,
473                                        mode_t *mode_p, int count);
474
475 /* inline functions */
476 static inline int lu_device_is_mdd(struct lu_device *d)
477 {
478         return ergo(d != NULL && d->ld_ops != NULL, d->ld_ops == &mdd_lu_ops);
479 }
480
481 static inline struct mdd_device *lu2mdd_dev(struct lu_device *d)
482 {
483         LASSERT(lu_device_is_mdd(d));
484         return container_of_safe(d, struct mdd_device, mdd_md_dev.md_lu_dev);
485 }
486
487 static inline struct lu_device *mdd2lu_dev(struct mdd_device *mdd)
488 {
489         return &mdd->mdd_md_dev.md_lu_dev;
490 }
491
492 static inline struct mdd_object *lu2mdd_obj(struct lu_object *o)
493 {
494         LASSERT(ergo(o != NULL, lu_device_is_mdd(o->lo_dev)));
495         return container_of_safe(o, struct mdd_object,
496                                  mod_obj.mo_lu);
497 }
498
499 static inline struct mdd_device *mdo2mdd(struct md_object *mdo)
500 {
501         return lu2mdd_dev(mdo->mo_lu.lo_dev);
502 }
503
504 static inline struct mdd_object *md2mdd_obj(struct md_object *mdo)
505 {
506         return container_of_safe(mdo, struct mdd_object, mod_obj);
507 }
508
509 static inline const
510 struct dt_device_operations *mdd_child_ops(struct mdd_device *mdd)
511 {
512         return mdd->mdd_child->dd_ops;
513 }
514
515 static inline struct lu_object *mdd2lu_obj(struct mdd_object *obj)
516 {
517         return &obj->mod_obj.mo_lu;
518 }
519
520 static inline struct dt_object *mdd_object_child(struct mdd_object *obj)
521 {
522         return container_of(lu_object_next(mdd2lu_obj(obj)),
523                             struct dt_object, do_lu);
524 }
525
526 static inline struct obd_device *mdd2obd_dev(struct mdd_device *mdd)
527 {
528         return (mdd->mdd_md_dev.md_lu_dev.ld_obd);
529 }
530
531 static inline struct mdd_device *mdd_obj2mdd_dev(struct mdd_object *obj)
532 {
533         return mdo2mdd(&obj->mod_obj);
534 }
535
536 static inline umode_t mdd_object_type(const struct mdd_object *mdd_obj)
537 {
538         return lu_object_attr(&mdd_obj->mod_obj.mo_lu);
539 }
540
541 static inline int mdd_is_dead_obj(struct mdd_object *mdd_obj)
542 {
543         return mdd_obj && mdd_obj->mod_flags & DEAD_OBJ;
544 }
545
546 static inline bool mdd_is_volatile_obj(struct mdd_object *mdd_obj)
547 {
548         return mdd_obj->mod_flags & VOLATILE_OBJ;
549 }
550
551 static inline bool mdd_is_orphan_obj(struct mdd_object *mdd_obj)
552 {
553         return mdd_obj->mod_flags & ORPHAN_OBJ;
554 }
555
556 static inline int mdd_object_exists(struct mdd_object *mdd_obj)
557 {
558         return lu_object_exists(mdd2lu_obj(mdd_obj));
559 }
560
561 static inline int mdd_object_remote(struct mdd_object *mdd_obj)
562 {
563         return lu_object_remote(mdd2lu_obj(mdd_obj));
564 }
565
566 static inline const struct lu_fid *mdd_object_fid(struct mdd_object *mdd_obj)
567 {
568         return lu_object_fid(mdd2lu_obj(mdd_obj));
569 }
570
571 static inline struct seq_server_site *mdd_seq_site(struct mdd_device *mdd)
572 {
573         return mdd2lu_dev(mdd)->ld_site->ld_seq_site;
574 }
575
576 static inline const char *mdd_obj_dev_name(const struct mdd_object *mdd_obj)
577 {
578         return lu_dev_name(mdd_obj->mod_obj.mo_lu.lo_dev);
579 }
580
581 #define MAX_ATIME_DIFF 60
582
583 static inline int mdd_permission_internal(const struct lu_env *env,
584                                           struct mdd_object *obj,
585                                           const struct lu_attr *la,
586                                           unsigned int may_mask)
587 {
588         return __mdd_permission_internal(env, obj, la, may_mask, -1);
589 }
590
591 static inline int mdd_permission_internal_locked(const struct lu_env *env,
592                                                 struct mdd_object *obj,
593                                                 const struct lu_attr *la,
594                                                 unsigned int may_mask,
595                                                 enum dt_object_role role)
596 {
597         return __mdd_permission_internal(env, obj, la, may_mask, role);
598 }
599
600 /* mdd inline func for calling osd_dt_object ops */
601 static inline int mdo_attr_get(const struct lu_env *env, struct mdd_object *obj,
602                                struct lu_attr *la)
603 {
604         struct dt_object *next = mdd_object_child(obj);
605         return dt_attr_get(env, next, la);
606 }
607
608 static inline int mdo_declare_attr_set(const struct lu_env *env,
609                                        struct mdd_object *obj,
610                                        const struct lu_attr *la,
611                                        struct thandle *handle)
612 {
613         struct dt_object *next = mdd_object_child(obj);
614         return dt_declare_attr_set(env, next, la, handle);
615 }
616
617 static inline int mdo_attr_set(const struct lu_env *env,
618                                struct mdd_object *obj,
619                                const struct lu_attr *la,
620                                struct thandle *handle)
621 {
622         struct dt_object *next = mdd_object_child(obj);
623
624         if (!mdd_object_exists(obj))
625                 return -ENOENT;
626
627         return dt_attr_set(env, next, la, handle);
628 }
629
630 static inline int mdo_xattr_get(const struct lu_env *env,struct mdd_object *obj,
631                                 struct lu_buf *buf, const char *name)
632 {
633         struct dt_object *next = mdd_object_child(obj);
634         return dt_xattr_get(env, next, buf, name);
635 }
636
637 static inline int mdo_declare_xattr_set(const struct lu_env *env,
638                                         struct mdd_object *obj,
639                                         const struct lu_buf *buf,
640                                         const char *name,
641                                         int fl, struct thandle *handle)
642 {
643         struct dt_object *next = mdd_object_child(obj);
644         int rc;
645
646         rc = dt_declare_xattr_set(env, next, buf, name, fl, handle);
647         if (rc >= 0 &&
648             (strcmp(name, LL_XATTR_NAME_ENCRYPTION_CONTEXT) == 0 ||
649              strcmp(name, LL_XATTR_NAME_ENCRYPTION_CONTEXT_OLD) == 0)) {
650                 struct lu_attr la = { 0 };
651
652                 la.la_valid = LA_FLAGS;
653                 la.la_flags = LUSTRE_ENCRYPT_FL;
654                 rc = dt_declare_attr_set(env, next, &la, handle);
655         }
656         return rc;
657 }
658
659 static inline int mdo_xattr_set(const struct lu_env *env,struct mdd_object *obj,
660                                 const struct lu_buf *buf, const char *name,
661                                 int fl, struct thandle *handle)
662 {
663         struct dt_object *next = mdd_object_child(obj);
664         int rc = 0;
665
666         if (!mdd_object_exists(obj))
667                 return -ENOENT;
668
669         /* If we are about to set the LL_XATTR_NAME_ENCRYPTION_CONTEXT
670          * xattr, it means the file/dir is encrypted. In that case we want
671          * to set the LUSTRE_ENCRYPT_FL flag as well: it will be stored
672          * into the LMA, making it more efficient to recognise we are
673          * dealing with an encrypted file/dir, as LMA info is cached upon
674          * object init.
675          * However, marking a dir as encrypted is only possible if it is
676          * being created or migrated (LU_XATTR_CREATE flag not set), or
677          * if it is empty.
678          */
679         if ((strcmp(name, LL_XATTR_NAME_ENCRYPTION_CONTEXT) == 0 ||
680              strcmp(name, LL_XATTR_NAME_ENCRYPTION_CONTEXT_OLD) == 0) &&
681             (!S_ISDIR(mdd_object_type(obj)) ||
682              !(fl & LU_XATTR_CREATE) ||
683              (rc = mdd_dir_is_empty(env, obj)) == 0)) {
684                 struct lu_attr la = { 0 };
685
686                 la.la_valid = LA_FLAGS;
687                 la.la_flags = LUSTRE_ENCRYPT_FL;
688                 /* if this is an old client using the old enc xattr name,
689                  * switch to the new name for consistency
690                  */
691                 if (strcmp(name, LL_XATTR_NAME_ENCRYPTION_CONTEXT_OLD) == 0)
692                         name = LL_XATTR_NAME_ENCRYPTION_CONTEXT;
693                 rc = dt_attr_set(env, next, &la, handle);
694         }
695         if (rc >= 0)
696                 rc = dt_xattr_set(env, next, buf, name, fl, handle);
697
698         return rc;
699 }
700
701 static inline int mdo_declare_xattr_del(const struct lu_env *env,
702                                         struct mdd_object *obj,
703                                         const char *name,
704                                         struct thandle *handle)
705 {
706         struct dt_object *next = mdd_object_child(obj);
707         return dt_declare_xattr_del(env, next, name, handle);
708 }
709
710 static inline int mdo_xattr_del(const struct lu_env *env,struct mdd_object *obj,
711                                 const char *name, struct thandle *handle)
712 {
713         struct dt_object *next = mdd_object_child(obj);
714
715         if (!mdd_object_exists(obj))
716                 return -ENOENT;
717
718         return dt_xattr_del(env, next, name, handle);
719 }
720
721 static inline int
722 mdo_xattr_list(const struct lu_env *env, struct mdd_object *obj,
723                struct lu_buf *buf)
724 {
725         struct dt_object *next = mdd_object_child(obj);
726
727         if (!mdd_object_exists(obj))
728                 return -ENOENT;
729
730         return dt_xattr_list(env, next, buf);
731 }
732
733 static inline int
734 mdo_invalidate(const struct lu_env *env, struct mdd_object *obj)
735 {
736         return dt_invalidate(env, mdd_object_child(obj));
737 }
738
739 static inline int
740 mdo_declare_layout_change(const struct lu_env *env, struct mdd_object *obj,
741                           struct md_layout_change *mlc, struct thandle *handle)
742 {
743         return dt_declare_layout_change(env, mdd_object_child(obj),
744                                         mlc, handle);
745 }
746
747 static inline int
748 mdo_layout_change(const struct lu_env *env, struct mdd_object *obj,
749                   struct md_layout_change *mlc, struct thandle *handle)
750 {
751         return dt_layout_change(env, mdd_object_child(obj), mlc, handle);
752 }
753
754 static inline
755 int mdo_declare_index_insert(const struct lu_env *env, struct mdd_object *obj,
756                              const struct lu_fid *fid, __u32 type,
757                              const char *name, struct thandle *handle)
758 {
759         struct dt_object *next  = mdd_object_child(obj);
760         int               rc;
761
762         /*
763          * if the object doesn't exist yet, then it's supposed to be created
764          * and declaration of the creation should be enough to insert ./..
765          */
766
767         rc = -ENOTDIR;
768         if (dt_try_as_dir(env, next, false)) {
769                 struct dt_insert_rec *rec = &mdd_env_info(env)->mdi_dt_rec;
770
771                 rec->rec_fid = fid;
772                 rec->rec_type = type;
773                 rc = dt_declare_insert(env, next, (const struct dt_rec *)rec,
774                                        (const struct dt_key *)name, handle);
775         }
776
777          return rc;
778 }
779
780 static inline
781 int mdo_declare_index_delete(const struct lu_env *env, struct mdd_object *obj,
782                              const char *name, struct thandle *handle)
783 {
784         struct dt_object *next = mdd_object_child(obj);
785
786         if (!dt_try_as_dir(env, next, true))
787                 return -ENOTDIR;
788
789         return dt_declare_delete(env, next, (const struct dt_key *)name,
790                                  handle);
791 }
792
793 static inline int mdo_declare_ref_add(const struct lu_env *env,
794                                       struct mdd_object *obj,
795                                       struct thandle *handle)
796 {
797         struct dt_object *next = mdd_object_child(obj);
798         return dt_declare_ref_add(env, next, handle);
799 }
800
801 static inline int mdo_ref_add(const struct lu_env *env, struct mdd_object *obj,
802                               struct thandle *handle)
803 {
804         struct dt_object *next = mdd_object_child(obj);
805
806         if (!mdd_object_exists(obj))
807                 return -ENOENT;
808
809         return dt_ref_add(env, next, handle);
810 }
811
812 static inline int mdo_declare_ref_del(const struct lu_env *env,
813                                       struct mdd_object *obj,
814                                       struct thandle *handle)
815 {
816         struct dt_object *next = mdd_object_child(obj);
817         return dt_declare_ref_del(env, next, handle);
818 }
819
820 static inline int mdo_ref_del(const struct lu_env *env, struct mdd_object *obj,
821                               struct thandle *handle)
822 {
823         struct dt_object *next = mdd_object_child(obj);
824
825         if (!mdd_object_exists(obj))
826                 return -ENOENT;
827
828         return dt_ref_del(env, next, handle);
829 }
830
831 static inline int
832 mdo_declare_create_object(const struct lu_env *env, struct mdd_object *obj,
833                           struct lu_attr *attr, struct dt_allocation_hint *hint,
834                           struct dt_object_format *dof, struct thandle *handle)
835 {
836         struct dt_object *next = mdd_object_child(obj);
837         return dt_declare_create(env, next, attr, hint, dof, handle);
838 }
839
840 static inline int
841 mdo_create_object(const struct lu_env *env, struct mdd_object *obj,
842                   struct lu_attr *attr, struct dt_allocation_hint *hint,
843                   struct dt_object_format *dof, struct thandle *handle)
844 {
845         struct dt_object *next = mdd_object_child(obj);
846         return dt_create(env, next, attr, hint, dof, handle);
847 }
848
849 static inline
850 int mdo_declare_destroy(const struct lu_env *env, struct mdd_object *o,
851                         struct thandle *handle)
852 {
853         struct dt_object *next = mdd_object_child(o);
854         return dt_declare_destroy(env, next, handle);
855 }
856
857 static inline
858 int mdo_destroy(const struct lu_env *env, struct mdd_object *o,
859                 struct thandle *handle)
860 {
861         struct dt_object *next = mdd_object_child(o);
862         return dt_destroy(env, next, handle);
863 }
864
865 static inline bool mdd_changelog_enabled(const struct lu_env *env,
866                                         struct mdd_device *mdd,
867                                         enum changelog_rec_type type)
868 {
869         const struct lu_ucred *uc;
870
871         if ((mdd->mdd_cl.mc_flags & CLM_ON) &&
872             (mdd->mdd_cl.mc_current_mask & BIT(type))) {
873                 uc = lu_ucred_check(env);
874
875                 return uc != NULL ? uc->uc_enable_audit : true;
876         } else {
877                 return false;
878         }
879 }
880
881 static inline bool mdd_changelog_is_too_idle(struct mdd_device *mdd,
882                                              __u64 cl_rec, __u32 cl_time)
883 {
884         __u64 idle_indexes = mdd->mdd_cl.mc_index - cl_rec;
885         __u32 idle_time = (__u32)ktime_get_real_seconds() - cl_time;
886
887         return (idle_indexes > mdd->mdd_changelog_max_idle_indexes ||
888                 idle_time > mdd->mdd_changelog_max_idle_time ||
889                 idle_time * idle_indexes > (24 * 3600ULL << 32));
890 }
891
892 bool mdd_changelog_is_space_safe(const struct lu_env *env,
893                                  struct mdd_device *mdd,
894                                  struct llog_handle *lgh,
895                                  bool estimate);
896
897 #define MAX_TRANS_RETRIED 20
898
899 static inline int
900 mdd_write_lock_two_objects(const struct lu_env *env,
901                            struct mdd_object *obj1,
902                            struct mdd_object *obj2)
903 {
904         int order;
905
906         /* there shouldn't be callers with obj1 == NULL */
907         LASSERT(obj1 != NULL);
908         if (!obj2)
909                 goto out_lock1;
910
911         order = lu_fid_cmp(mdd_object_fid(obj1), mdd_object_fid(obj2));
912         if (unlikely(order == 0))
913                 return -EPERM;
914
915         if (order > 0) {
916                 mdd_write_lock(env, obj1, DT_TGT_CHILD);
917                 mdd_write_lock(env, obj2, DT_TGT_CHILD);
918         } else {
919                 mdd_write_lock(env, obj2, DT_TGT_CHILD);
920 out_lock1:
921                 mdd_write_lock(env, obj1, DT_TGT_CHILD);
922         }
923
924         return 0;
925 }
926
927 static inline void
928 mdd_write_unlock_two_objects(const struct lu_env *env,
929                              struct mdd_object *obj1,
930                              struct mdd_object *obj2)
931 {
932         mdd_write_unlock(env, obj1);
933         if (obj2)
934                 mdd_write_unlock(env, obj2);
935 }
936
937
938 #endif