Whamcloud - gitweb
LU-2914 lfsck: split LFSCK code from mdd to lfsck
[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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2013, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/mdd/mdd_internal.h
37  *
38  * Author: Wang Di <wangdi@clusterfs.com>
39  */
40
41 #ifndef _MDD_INTERNAL_H
42 #define _MDD_INTERNAL_H
43
44 #include <lustre_acl.h>
45 #include <lustre_eacl.h>
46 #include <md_object.h>
47 #include <dt_object.h>
48 #include <lustre_fsfilt.h>
49 #include <lustre_lfsck.h>
50 #include <lustre_fid.h>
51 #include <lustre_capa.h>
52 #include <lprocfs_status.h>
53 #include <lustre_log.h>
54 #include <lustre_linkea.h>
55
56 /* PDO lock is unnecessary for current MDT stack because operations
57  * are already protected by ldlm lock */
58 #define MDD_DISABLE_PDO_LOCK    1
59
60 /* Changelog flags */
61 /** changelog is recording */
62 #define CLM_ON    0x00001
63 /** internal error prevented changelogs from starting */
64 #define CLM_ERR   0x00002
65 /* Marker flags */
66 /** changelogs turned on */
67 #define CLM_START 0x10000
68 /** changelogs turned off */
69 #define CLM_FINI  0x20000
70 /** some changelog records purged */
71 #define CLM_PURGE 0x40000
72
73 struct mdd_changelog {
74         spinlock_t              mc_lock;        /* for index */
75         int                     mc_flags;
76         int                     mc_mask;
77         __u64                   mc_index;
78         __u64                   mc_starttime;
79         spinlock_t              mc_user_lock;
80         int                     mc_lastuser;
81 };
82
83 static inline __u64 cl_time(void) {
84         cfs_fs_time_t time;
85
86         cfs_fs_time_current(&time);
87         return (((__u64)time.tv_sec) << 30) + time.tv_nsec;
88 }
89
90 /** Objects in .lustre dir */
91 struct mdd_dot_lustre_objs {
92         struct mdd_object *mdd_obf;
93 };
94
95 struct mdd_device {
96         struct md_device                 mdd_md_dev;
97         struct obd_export               *mdd_child_exp;
98         struct dt_device                *mdd_child;
99         struct dt_device                *mdd_bottom;
100         struct lu_fid                    mdd_root_fid; /* /ROOT */
101         struct lu_fid                    mdd_local_root_fid;
102         struct dt_device_param           mdd_dt_conf;
103         struct dt_object                *mdd_orphans; /* PENDING directory */
104         cfs_proc_dir_entry_t            *mdd_proc_entry;
105         struct mdd_changelog             mdd_cl;
106         unsigned long                    mdd_atime_diff;
107         struct mdd_object               *mdd_dot_lustre;
108         struct mdd_dot_lustre_objs       mdd_dot_lustre_objs;
109         unsigned int                     mdd_sync_permission;
110         int                              mdd_connects;
111         struct local_oid_storage        *mdd_los;
112 };
113
114 enum mod_flags {
115         /* The dir object has been unlinked */
116         DEAD_OBJ   = 1 << 0,
117         APPEND_OBJ = 1 << 1,
118         IMMUTE_OBJ = 1 << 2,
119         ORPHAN_OBJ = 1 << 3,
120 };
121
122 struct mdd_object {
123         struct md_object   mod_obj;
124         /* open count */
125         __u32             mod_count;
126         __u32             mod_valid;
127         __u64             mod_cltime;
128         unsigned long     mod_flags;
129 #ifndef MDD_DISABLE_PDO_LOCK
130         struct dynlock    mod_pdlock;
131 #endif
132 #ifdef CONFIG_LOCKDEP
133         /* "dep_map" name is assumed by lockdep.h macros. */
134         struct lockdep_map dep_map;
135 #endif
136 };
137
138 struct mdd_thread_info {
139         struct lu_fid             mti_fid;
140         struct lu_fid             mti_fid2; /* used for be & cpu converting */
141         struct lu_attr            mti_la;
142         struct lu_attr            mti_la_for_fix;
143         struct lu_attr            mti_pattr;
144         struct lu_attr            mti_cattr;
145         struct md_attr            mti_ma;
146         struct obd_info           mti_oi;
147         /* mti_ent and mti_key must be conjoint,
148          * then mti_ent::lde_name will be mti_key. */
149         struct lu_dirent          mti_ent;
150         char                      mti_key[NAME_MAX + 16];
151         struct obd_trans_info     mti_oti;
152         struct lu_buf             mti_buf;
153         struct lu_buf             mti_big_buf; /* biggish persistent buf */
154         struct lu_buf             mti_link_buf; /* buf for link ea */
155         struct lu_name            mti_name;
156         struct lu_name            mti_name2;
157         struct obdo               mti_oa;
158         char                      mti_xattr_buf[LUSTRE_POSIX_ACL_MAX_SIZE];
159         struct dt_allocation_hint mti_hint;
160         struct lov_mds_md        *mti_max_lmm;
161         int                       mti_max_lmm_size;
162         struct llog_cookie       *mti_max_cookie;
163         int                       mti_max_cookie_size;
164         struct dt_object_format   mti_dof;
165         struct obd_quotactl       mti_oqctl;
166         struct linkea_data        mti_link_data;
167 };
168
169 extern const char orph_index_name[];
170
171 extern const struct dt_index_features orph_index_features;
172
173 struct lov_mds_md *mdd_max_lmm_buffer(const struct lu_env *env, int size);
174 struct lov_mds_md *mdd_max_lmm_get(const struct lu_env *env,
175                                    struct mdd_device *mdd);
176
177 struct llog_cookie *mdd_max_cookie_get(const struct lu_env *env,
178                                        struct mdd_device *mdd);
179
180 int mdd_init_obd(const struct lu_env *env, struct mdd_device *mdd,
181                  struct lustre_cfg *cfg);
182 int mdd_fini_obd(const struct lu_env *env, struct mdd_device *mdd,
183                  struct lustre_cfg *lcfg);
184 int __mdd_xattr_set(const struct lu_env *env, struct mdd_object *obj,
185                     const struct lu_buf *buf, const char *name,
186                     int fl, struct thandle *handle);
187 int mdd_xattr_set_txn(const struct lu_env *env, struct mdd_object *obj,
188                       const struct lu_buf *buf, const char *name, int fl,
189                       struct thandle *txn);
190 int mdd_lsm_sanity_check(const struct lu_env *env, struct mdd_object *obj);
191 int mdd_lov_set_md(const struct lu_env *env, struct mdd_object *pobj,
192                    struct mdd_object *child, struct lov_mds_md *lmm,
193                    int lmm_size, struct thandle *handle, int set_stripe);
194 int mdd_lov_create(const struct lu_env *env, struct mdd_device *mdd,
195                    struct mdd_object *parent, struct mdd_object *child,
196                    struct lov_mds_md **lmm, int *lmm_size,
197                    const struct md_op_spec *spec, struct md_attr *ma);
198 int mdd_lov_objid_prepare(struct mdd_device *mdd, struct lov_mds_md *lmm);
199 int mdd_declare_lov_objid_update(const struct lu_env *, struct mdd_device *,
200                                  struct thandle *);
201 void mdd_lov_objid_update(struct mdd_device *mdd, struct lov_mds_md *lmm);
202 void mdd_lov_create_finish(const struct lu_env *env, struct mdd_device *mdd,
203                            struct lov_mds_md *lmm, int lmm_size,
204                            const struct md_op_spec *spec);
205 int mdd_file_lock(const struct lu_env *env, struct md_object *obj,
206                   struct lov_mds_md *lmm, struct ldlm_extent *extent,
207                   struct lustre_handle *lockh);
208 int mdd_file_unlock(const struct lu_env *env, struct md_object *obj,
209                     struct lov_mds_md *lmm, struct lustre_handle *lockh);
210 int mdd_lum_lmm_cmp(const struct lu_env *env, struct md_object *cobj,
211                     const struct md_op_spec *spec, struct md_attr *ma);
212 int mdd_get_md(const struct lu_env *env, struct mdd_object *obj,
213                void *md, int *md_size, const char *name);
214 int mdd_get_md_locked(const struct lu_env *env, struct mdd_object *obj,
215                       void *md, int *md_size, const char *name);
216 int mdd_data_get(const struct lu_env *env, struct mdd_object *obj, void **data);
217 int mdd_la_get(const struct lu_env *env, struct mdd_object *obj,
218                struct lu_attr *la, struct lustre_capa *capa);
219 int mdd_attr_get(const struct lu_env *env, struct md_object *obj,
220                  struct md_attr *ma);
221 int mdd_attr_set(const struct lu_env *env, struct md_object *obj,
222                  const struct md_attr *ma);
223 int mdd_attr_set_internal(const struct lu_env *env,
224                           struct mdd_object *obj,
225                           const struct lu_attr *attr,
226                           struct thandle *handle,
227                           int needacl);
228 int mdd_attr_check_set_internal(const struct lu_env *env,
229                                 struct mdd_object *obj,
230                                 struct lu_attr *attr,
231                                 struct thandle *handle,
232                                 int needacl);
233 int mdd_declare_object_kill(const struct lu_env *env, struct mdd_object *obj,
234                             struct md_attr *ma, struct thandle *handle);
235 int mdd_object_kill(const struct lu_env *env, struct mdd_object *obj,
236                     struct md_attr *ma, struct thandle *handle);
237 int mdd_iattr_get(const struct lu_env *env, struct mdd_object *mdd_obj,
238                   struct md_attr *ma);
239 int mdd_object_create_internal(const struct lu_env *env, struct mdd_object *p,
240                                struct mdd_object *c, struct lu_attr *attr,
241                                struct thandle *handle,
242                                const struct md_op_spec *spec);
243 int mdd_attr_check_set_internal_locked(const struct lu_env *env,
244                                        struct mdd_object *obj,
245                                        struct lu_attr *attr,
246                                        struct thandle *handle,
247                                        int needacl);
248 int mdd_lmm_get_locked(const struct lu_env *env, struct mdd_object *mdd_obj,
249                        struct md_attr *ma);
250
251 /* mdd_lock.c */
252 void mdd_write_lock(const struct lu_env *env, struct mdd_object *obj,
253                     enum mdd_object_role role);
254 void mdd_read_lock(const struct lu_env *env, struct mdd_object *obj,
255                    enum mdd_object_role role);
256 void mdd_write_unlock(const struct lu_env *env, struct mdd_object *obj);
257 void mdd_read_unlock(const struct lu_env *env, struct mdd_object *obj);
258 int mdd_write_locked(const struct lu_env *env, struct mdd_object *obj);
259
260 void mdd_pdlock_init(struct mdd_object *obj);
261 unsigned long mdd_name2hash(const char *name);
262 void *mdd_pdo_write_lock(const struct lu_env *env, struct mdd_object *obj,
263                          const char *name, enum mdd_object_role role);
264 void *mdd_pdo_read_lock(const struct lu_env *env, struct mdd_object *obj,
265                         const char *name, enum mdd_object_role role);
266 void mdd_pdo_write_unlock(const struct lu_env *env, struct mdd_object *obj,
267                           void *dlh);
268 void mdd_pdo_read_unlock(const struct lu_env *env, struct mdd_object *obj,
269                          void *dlh);
270 /* mdd_dir.c */
271 int mdd_parent_fid(const struct lu_env *env, struct mdd_object *obj,
272                    struct lu_fid *fid);
273 int mdd_is_subdir(const struct lu_env *env, struct md_object *mo,
274                   const struct lu_fid *fid, struct lu_fid *sfid);
275 int mdd_may_create(const struct lu_env *env, struct mdd_object *pobj,
276                    struct mdd_object *cobj, int check_perm, int check_nlink);
277 int mdd_may_unlink(const struct lu_env *env, struct mdd_object *pobj,
278                    const struct lu_attr *attr);
279 int mdd_may_delete(const struct lu_env *env, struct mdd_object *pobj,
280                    struct mdd_object *cobj, struct lu_attr *cattr,
281                    struct lu_attr *src_attr, int check_perm, int check_empty);
282 int mdd_unlink_sanity_check(const struct lu_env *env, struct mdd_object *pobj,
283                             struct mdd_object *cobj, struct lu_attr *cattr);
284 int mdd_finish_unlink(const struct lu_env *env, struct mdd_object *obj,
285                       struct md_attr *ma, struct thandle *th);
286
287 int mdd_link_sanity_check(const struct lu_env *env, struct mdd_object *tgt_obj,
288                           const struct lu_name *lname, struct mdd_object *src_obj);
289 int mdd_is_root(struct mdd_device *mdd, const struct lu_fid *fid);
290 int mdd_lookup(const struct lu_env *env,
291                struct md_object *pobj, const struct lu_name *lname,
292                struct lu_fid* fid, struct md_op_spec *spec);
293 int mdd_links_read(const struct lu_env *env, struct mdd_object *mdd_obj,
294                    struct linkea_data *ldata);
295 int mdd_declare_links_add(const struct lu_env *env, struct mdd_object *mdd_obj,
296                           struct thandle *handle, struct linkea_data *ldata);
297 int mdd_links_write(const struct lu_env *env, struct mdd_object *mdd_obj,
298                     struct linkea_data *ldata, struct thandle *handle);
299 struct lu_buf *mdd_links_get(const struct lu_env *env,
300                              struct mdd_object *mdd_obj);
301 int mdd_links_rename(const struct lu_env *env,
302                      struct mdd_object *mdd_obj,
303                      const struct lu_fid *oldpfid,
304                      const struct lu_name *oldlname,
305                      const struct lu_fid *newpfid,
306                      const struct lu_name *newlname,
307                      struct thandle *handle,
308                      struct linkea_data *ldata,
309                      int first, int check);
310 int mdd_declare_links_add(const struct lu_env *env, struct mdd_object *mdd_obj,
311                           struct thandle *handle, struct linkea_data *ldata);
312
313 /* mdd_lov.c */
314 int mdd_declare_unlink_log(const struct lu_env *env, struct mdd_object *obj,
315                            struct md_attr *ma, struct thandle *handle);
316 int mdd_unlink_log(const struct lu_env *env, struct mdd_device *mdd,
317                    struct mdd_object *mdd_cobj, struct md_attr *ma);
318
319 int mdd_setattr_log(const struct lu_env *env, struct mdd_device *mdd,
320                     const struct md_attr *ma,
321                     struct lov_mds_md *lmm, int lmm_size,
322                     struct llog_cookie *logcookies, int cookies_size);
323
324 int mdd_get_cookie_size(const struct lu_env *env, struct mdd_device *mdd,
325                         struct lov_mds_md *lmm);
326
327 int mdd_lov_setattr_async(const struct lu_env *env, struct mdd_object *obj,
328                           struct lov_mds_md *lmm, int lmm_size,
329                           struct llog_cookie *logcookies);
330
331 int mdd_lovobj_unlink(const struct lu_env *env, struct mdd_device *mdd,
332                       struct mdd_object *obj, struct lu_attr *la,
333                       struct md_attr *ma, int log_unlink);
334
335 struct mdd_thread_info *mdd_env_info(const struct lu_env *env);
336
337 const struct lu_name *mdd_name_get_const(const struct lu_env *env,
338                                          const void *area, ssize_t len);
339 struct lu_buf *mdd_buf_get(const struct lu_env *env, void *area, ssize_t len);
340 const struct lu_buf *mdd_buf_get_const(const struct lu_env *env,
341                                        const void *area, ssize_t len);
342
343 int __mdd_orphan_cleanup(const struct lu_env *env, struct mdd_device *d);
344 int __mdd_orphan_add(const struct lu_env *, struct mdd_object *,
345                      struct thandle *);
346 int __mdd_orphan_del(const struct lu_env *, struct mdd_object *,
347                      struct thandle *);
348 int orph_index_init(const struct lu_env *env, struct mdd_device *mdd);
349 void orph_index_fini(const struct lu_env *env, struct mdd_device *mdd);
350 int orph_declare_index_insert(const struct lu_env *, struct mdd_object *,
351                               cfs_umode_t mode, struct thandle *);
352 int orph_declare_index_delete(const struct lu_env *, struct mdd_object *,
353                               struct thandle *);
354
355 /* mdd_lproc.c */
356 void lprocfs_mdd_init_vars(struct lprocfs_static_vars *lvars);
357 int mdd_procfs_init(struct mdd_device *mdd, const char *name);
358 int mdd_procfs_fini(struct mdd_device *mdd);
359
360 /* mdd_object.c */
361 int mdd_get_flags(const struct lu_env *env, struct mdd_object *obj);
362 struct lu_buf *mdd_buf_alloc(const struct lu_env *env, ssize_t len);
363 int mdd_buf_grow(const struct lu_env *env, ssize_t len);
364 void mdd_buf_put(struct lu_buf *buf);
365
366 struct lu_buf *mdd_link_buf_alloc(const struct lu_env *env, ssize_t len);
367 int mdd_link_buf_grow(const struct lu_env *env, ssize_t len);
368 extern const struct md_dir_operations    mdd_dir_ops;
369 extern const struct md_object_operations mdd_obj_ops;
370
371 int accmode(const struct lu_env *env, struct lu_attr *la, int flags);
372 extern struct lu_context_key mdd_thread_key;
373 extern const struct lu_device_operations mdd_lu_ops;
374
375 struct mdd_object *mdd_object_find(const struct lu_env *env,
376                                    struct mdd_device *d,
377                                    const struct lu_fid *f);
378 int mdd_get_default_md(struct mdd_object *mdd_obj, struct lov_mds_md *lmm);
379 int mdd_readpage(const struct lu_env *env, struct md_object *obj,
380                  const struct lu_rdpg *rdpg);
381 int mdd_declare_changelog_store(const struct lu_env *env,
382                                 struct mdd_device *mdd,
383                                 const struct lu_name *fname,
384                                 struct thandle *handle);
385 int mdd_changelog_store(const struct lu_env *env, struct mdd_device *mdd,
386                         struct llog_changelog_rec *rec, struct thandle *th);
387 int mdd_changelog_data_store(const struct lu_env *env, struct mdd_device *mdd,
388                              enum changelog_rec_type type, int flags,
389                              struct mdd_object *mdd_obj,
390                              struct thandle *handle);
391 int mdd_changelog_ns_store(const struct lu_env *env, struct mdd_device *mdd,
392                            enum changelog_rec_type type, unsigned flags,
393                            struct mdd_object *target, struct mdd_object *parent,
394                            const struct lu_name *tname, struct thandle *handle);
395 int mdd_declare_object_create_internal(const struct lu_env *env,
396                                        struct mdd_object *p,
397                                        struct mdd_object *c,
398                                        struct lu_attr *attr,
399                                        struct thandle *handle,
400                                        const struct md_op_spec *spec);
401
402 /* mdd_trans.c */
403 int mdd_lov_destroy(const struct lu_env *env, struct mdd_device *mdd,
404                     struct mdd_object *obj, struct lu_attr *la);
405
406 void mdd_object_make_hint(const struct lu_env *env, struct mdd_object *parent,
407                           struct mdd_object *child, struct lu_attr *attr);
408
409 static inline void mdd_object_get(struct mdd_object *o)
410 {
411         lu_object_get(&o->mod_obj.mo_lu);
412 }
413
414 static inline void mdd_object_put(const struct lu_env *env,
415                                   struct mdd_object *o)
416 {
417         lu_object_put(env, &o->mod_obj.mo_lu);
418 }
419
420 struct thandle *mdd_trans_create(const struct lu_env *env,
421                                  struct mdd_device *mdd);
422 int mdd_trans_start(const struct lu_env *env, struct mdd_device *mdd,
423                     struct thandle *th);
424 void mdd_trans_stop(const struct lu_env *env, struct mdd_device *mdd,
425                     int rc, struct thandle *handle);
426 int mdd_txn_stop_cb(const struct lu_env *env, struct thandle *txn,
427                     void *cookie);
428 int mdd_txn_start_cb(const struct lu_env *env, struct thandle *,
429                      void *cookie);
430
431 /* mdd_device.c */
432 struct lu_object *mdd_object_alloc(const struct lu_env *env,
433                                    const struct lu_object_header *hdr,
434                                    struct lu_device *d);
435 int mdd_local_file_create(const struct lu_env *env, struct mdd_device *mdd,
436                           const struct lu_fid *pfid, const char *name,
437                           __u32 mode, struct lu_fid *fid);
438
439 int mdd_acl_chmod(const struct lu_env *env, struct mdd_object *o, __u32 mode,
440                   struct thandle *handle);
441 int __mdd_declare_acl_init(const struct lu_env *env, struct mdd_object *obj,
442                            int is_dir, struct thandle *handle);
443 int mdd_acl_set(const struct lu_env *env, struct mdd_object *obj,
444                 const struct lu_buf *buf, int fl);
445 int __mdd_fix_mode_acl(const struct lu_env *env, struct lu_buf *buf,
446                        __u32 *mode);
447 int __mdd_permission_internal(const struct lu_env *env, struct mdd_object *obj,
448                               struct lu_attr *la, int mask, int role);
449 int mdd_permission(const struct lu_env *env,
450                    struct md_object *pobj, struct md_object *cobj,
451                    struct md_attr *ma, int mask);
452 int mdd_capa_get(const struct lu_env *env, struct md_object *obj,
453                  struct lustre_capa *capa, int renewal);
454
455 /* mdd_prepare.c */
456 int mdd_compat_fixes(const struct lu_env *env, struct mdd_device *mdd);
457
458 static inline int lu_device_is_mdd(struct lu_device *d)
459 {
460         return ergo(d != NULL && d->ld_ops != NULL, d->ld_ops == &mdd_lu_ops);
461 }
462
463 static inline struct mdd_device* lu2mdd_dev(struct lu_device *d)
464 {
465         LASSERT(lu_device_is_mdd(d));
466         return container_of0(d, struct mdd_device, mdd_md_dev.md_lu_dev);
467 }
468
469 static inline struct lu_device *mdd2lu_dev(struct mdd_device *d)
470 {
471         return (&d->mdd_md_dev.md_lu_dev);
472 }
473
474 static inline struct mdd_object *lu2mdd_obj(struct lu_object *o)
475 {
476         LASSERT(ergo(o != NULL, lu_device_is_mdd(o->lo_dev)));
477         return container_of0(o, struct mdd_object, mod_obj.mo_lu);
478 }
479
480 static inline struct mdd_device* mdo2mdd(struct md_object *mdo)
481 {
482         return lu2mdd_dev(mdo->mo_lu.lo_dev);
483 }
484
485 static inline struct mdd_object* md2mdd_obj(struct md_object *mdo)
486 {
487         return container_of0(mdo, struct mdd_object, mod_obj);
488 }
489
490 static inline const struct dt_device_operations *
491 mdd_child_ops(struct mdd_device *d)
492 {
493         return d->mdd_child->dd_ops;
494 }
495
496 static inline struct lu_object *mdd2lu_obj(struct mdd_object *obj)
497 {
498         return &obj->mod_obj.mo_lu;
499 }
500
501 static inline struct dt_object* mdd_object_child(struct mdd_object *o)
502 {
503         return container_of0(lu_object_next(mdd2lu_obj(o)),
504                              struct dt_object, do_lu);
505 }
506
507 static inline struct obd_device *mdd2obd_dev(struct mdd_device *mdd)
508 {
509         return (mdd->mdd_md_dev.md_lu_dev.ld_obd);
510 }
511
512 static inline struct mdd_device *mdd_obj2mdd_dev(struct mdd_object *obj)
513 {
514         return mdo2mdd(&obj->mod_obj);
515 }
516
517 static inline const struct lu_fid *mdo2fid(const struct mdd_object *obj)
518 {
519         return lu_object_fid(&obj->mod_obj.mo_lu);
520 }
521
522 static inline int mdd_object_obf(const struct mdd_object *obj)
523 {
524         return lu_fid_eq(mdo2fid(obj), &LU_OBF_FID);
525 }
526
527 static inline cfs_umode_t mdd_object_type(const struct mdd_object *obj)
528 {
529         return lu_object_attr(&obj->mod_obj.mo_lu);
530 }
531
532 static inline int mdd_is_immutable(struct mdd_object *obj)
533 {
534         return obj->mod_flags & IMMUTE_OBJ;
535 }
536
537 static inline int mdd_is_dead_obj(struct mdd_object *obj)
538 {
539         return obj && obj->mod_flags & DEAD_OBJ;
540 }
541
542 static inline int mdd_is_append(struct mdd_object *obj)
543 {
544         return obj->mod_flags & APPEND_OBJ;
545 }
546
547 static inline int mdd_object_exists(struct mdd_object *obj)
548 {
549         return lu_object_exists(mdd2lu_obj(obj));
550 }
551
552 static inline int mdd_object_remote(struct mdd_object *obj)
553 {
554         return lu_object_remote(mdd2lu_obj(obj));
555 }
556
557 static inline const struct lu_fid *mdd_object_fid(struct mdd_object *obj)
558 {
559         return lu_object_fid(mdd2lu_obj(obj));
560 }
561
562 static inline struct seq_server_site *mdd_seq_site(struct mdd_device *mdd)
563 {
564         return mdd2lu_dev(mdd)->ld_site->ld_seq_site;
565 }
566
567 static inline struct lustre_capa *mdd_object_capa(const struct lu_env *env,
568                                                   const struct mdd_object *obj)
569 {
570         struct md_capainfo *ci = md_capainfo(env);
571         const struct lu_fid *fid = mdo2fid(obj);
572         int i;
573
574         /* NB: in mdt_init0 */
575         if (!ci)
576                 return BYPASS_CAPA;
577         for (i = 0; i < MD_CAPAINFO_MAX; i++)
578                 if (lu_fid_eq(&ci->mc_fid[i], fid))
579                         return ci->mc_capa[i];
580         return NULL;
581 }
582
583 static inline void mdd_set_capainfo(const struct lu_env *env, int offset,
584                                     const struct mdd_object *obj,
585                                     struct lustre_capa *capa)
586 {
587         struct md_capainfo *ci = md_capainfo(env);
588         const struct lu_fid *fid = mdo2fid(obj);
589
590         LASSERT(offset >= 0 && offset < MD_CAPAINFO_MAX);
591         /* NB: in mdt_init0 */
592         if (!ci)
593                 return;
594         ci->mc_fid[offset]  = *fid;
595         ci->mc_capa[offset] = capa;
596 }
597
598 static inline const char *mdd_obj_dev_name(const struct mdd_object *obj)
599 {
600         return lu_dev_name(obj->mod_obj.mo_lu.lo_dev);
601 }
602
603 #define MAX_ATIME_DIFF 60
604
605 enum {
606         LPROC_MDD_NR
607 };
608
609 static inline int mdd_permission_internal(const struct lu_env *env,
610                                           struct mdd_object *obj,
611                                           struct lu_attr *la, int mask)
612 {
613         return __mdd_permission_internal(env, obj, la, mask, -1);
614 }
615
616 static inline int mdd_permission_internal_locked(const struct lu_env *env,
617                                                  struct mdd_object *obj,
618                                                  struct lu_attr *la, int mask,
619                                                  enum mdd_object_role role)
620 {
621         return __mdd_permission_internal(env, obj, la, mask, role);
622 }
623
624 static inline int mdo_data_get(const struct lu_env *env,
625                                struct mdd_object *obj,
626                                void **data)
627 {
628         struct dt_object *next = mdd_object_child(obj);
629         next->do_ops->do_data_get(env, next, data);
630         return 0;
631 }
632
633 /* mdd inline func for calling osd_dt_object ops */
634 static inline int mdo_attr_get(const struct lu_env *env, struct mdd_object *obj,
635                                struct lu_attr *la, struct lustre_capa *capa)
636 {
637         struct dt_object *next = mdd_object_child(obj);
638         return next->do_ops->do_attr_get(env, next, la, capa);
639 }
640
641 static inline int mdo_declare_attr_set(const struct lu_env *env,
642                                        struct mdd_object *obj,
643                                        const struct lu_attr *la,
644                                        struct thandle *handle)
645 {
646         struct dt_object *next = mdd_object_child(obj);
647         return dt_declare_attr_set(env, next, la, handle);
648 }
649
650 static inline int mdo_attr_set(const struct lu_env *env,
651                                struct mdd_object *obj,
652                                const struct lu_attr *la,
653                                struct thandle *handle,
654                                struct lustre_capa *capa)
655 {
656         struct dt_object *next = mdd_object_child(obj);
657         if (mdd_object_exists(obj) == 0) {
658                 CERROR("%s: object "DFID" not found: rc = -2\n",
659                        mdd_obj_dev_name(obj), PFID(mdd_object_fid(obj)));
660                 return -ENOENT;
661         }
662         return next->do_ops->do_attr_set(env, next, la, handle, capa);
663 }
664
665 static inline int mdo_xattr_get(const struct lu_env *env,struct mdd_object *obj,
666                                 struct lu_buf *buf, const char *name,
667                                 struct lustre_capa *capa)
668 {
669         struct dt_object *next = mdd_object_child(obj);
670         return next->do_ops->do_xattr_get(env, next, buf, name, capa);
671 }
672
673 static inline int mdo_declare_xattr_set(const struct lu_env *env,
674                                         struct mdd_object *obj,
675                                         const struct lu_buf *buf,
676                                         const char *name,
677                                         int fl, struct thandle *handle)
678 {
679         struct dt_object *next = mdd_object_child(obj);
680         return dt_declare_xattr_set(env, next, buf, name, fl, handle);
681 }
682
683 static inline int mdo_xattr_set(const struct lu_env *env,struct mdd_object *obj,
684                                 const struct lu_buf *buf, const char *name,
685                                 int fl, struct thandle *handle,
686                                 struct lustre_capa *capa)
687 {
688         struct dt_object *next = mdd_object_child(obj);
689         if (mdd_object_exists(obj) == 0) {
690                 CERROR("%s: object "DFID" not found: rc = -2\n",
691                        mdd_obj_dev_name(obj), PFID(mdd_object_fid(obj)));
692                 return -ENOENT;
693         }
694         return next->do_ops->do_xattr_set(env, next, buf, name, fl, handle,
695                                           capa);
696 }
697
698 static inline int mdo_declare_xattr_del(const struct lu_env *env,
699                                         struct mdd_object *obj,
700                                         const char *name,
701                                         struct thandle *handle)
702 {
703         struct dt_object *next = mdd_object_child(obj);
704         return dt_declare_xattr_del(env, next, name, handle);
705 }
706
707 static inline int mdo_xattr_del(const struct lu_env *env,struct mdd_object *obj,
708                                 const char *name, struct thandle *handle,
709                                 struct lustre_capa *capa)
710 {
711         struct dt_object *next = mdd_object_child(obj);
712         if (mdd_object_exists(obj) == 0) {
713                 CERROR("%s: object "DFID" not found: rc = -2\n",
714                        mdd_obj_dev_name(obj), PFID(mdd_object_fid(obj)));
715                 return -ENOENT;
716         }
717         return next->do_ops->do_xattr_del(env, next, name, handle, capa);
718 }
719
720 static inline
721 int mdo_xattr_list(const struct lu_env *env, struct mdd_object *obj,
722                    struct lu_buf *buf, struct lustre_capa *capa)
723 {
724         struct dt_object *next = mdd_object_child(obj);
725         if (mdd_object_exists(obj) == 0) {
726                 CERROR("%s: object "DFID" not found: rc = -2\n",
727                        mdd_obj_dev_name(obj), PFID(mdd_object_fid(obj)));
728                 return -ENOENT;
729         }
730         return next->do_ops->do_xattr_list(env, next, buf, capa);
731 }
732
733 static inline
734 int mdo_index_try(const struct lu_env *env, struct mdd_object *obj,
735                                  const struct dt_index_features *feat)
736 {
737         struct dt_object *next = mdd_object_child(obj);
738         return next->do_ops->do_index_try(env, next, feat);
739 }
740
741 static inline
742 int mdo_declare_index_insert(const struct lu_env *env, struct mdd_object *obj,
743                              const struct lu_fid *fid, const char *name,
744                              struct thandle *handle)
745 {
746         struct dt_object *next = mdd_object_child(obj);
747         int              rc = 0;
748
749         /*
750          * if the object doesn't exist yet, then it's supposed to be created
751          * and declaration of the creation should be enough to insert ./..
752          */
753          /* FIXME: remote object should not be awared by MDD layer, but local
754           * creation does not declare insert ./.. (comments above), which
755           * is required by remote directory creation.
756           * This remote check should be removed when mdd_object_exists check is
757           * removed.
758           */
759          if (mdd_object_exists(obj) || mdd_object_remote(obj)) {
760                 rc = -ENOTDIR;
761                 if (dt_try_as_dir(env, next))
762                         rc = dt_declare_insert(env, next,
763                                                (struct dt_rec *)fid,
764                                                (const struct dt_key *)name,
765                                                handle);
766         }
767
768         return rc;
769 }
770
771 static inline
772 int mdo_declare_index_delete(const struct lu_env *env, struct mdd_object *obj,
773                              const char *name, struct thandle *handle)
774 {
775         struct dt_object *next = mdd_object_child(obj);
776
777         if (!dt_try_as_dir(env, next))
778                 return -ENOTDIR;
779
780         return dt_declare_delete(env, next, (const struct dt_key *)name,
781                                  handle);
782 }
783
784 static inline int mdo_declare_ref_add(const struct lu_env *env,
785                                       struct mdd_object *obj,
786                                       struct thandle *handle)
787 {
788         struct dt_object *next = mdd_object_child(obj);
789         return dt_declare_ref_add(env, next, handle);
790 }
791
792 static inline int mdo_ref_add(const struct lu_env *env, struct mdd_object *obj,
793                               struct thandle *handle)
794 {
795         struct dt_object *next = mdd_object_child(obj);
796         if (mdd_object_exists(obj) == 0) {
797                 CERROR("%s: object "DFID" not found: rc = -2\n",
798                        mdd_obj_dev_name(obj), PFID(mdd_object_fid(obj)));
799                 return -ENOENT;
800         }
801         return next->do_ops->do_ref_add(env, next, handle);
802 }
803
804 static inline int mdo_declare_ref_del(const struct lu_env *env,
805                                       struct mdd_object *obj,
806                                       struct thandle *handle)
807 {
808         struct dt_object *next = mdd_object_child(obj);
809         return dt_declare_ref_del(env, next, handle);
810 }
811
812 static inline int mdo_ref_del(const struct lu_env *env, struct mdd_object *obj,
813                               struct thandle *handle)
814 {
815         struct dt_object *next = mdd_object_child(obj);
816         if (mdd_object_exists(obj) == 0) {
817                 CERROR("%s: object "DFID" not found: rc = -2\n",
818                        mdd_obj_dev_name(obj), PFID(mdd_object_fid(obj)));
819                 return -ENOENT;
820         }
821         return next->do_ops->do_ref_del(env, next, handle);
822 }
823
824 static inline
825 int mdo_declare_create_obj(const struct lu_env *env, struct mdd_object *o,
826                            struct lu_attr *attr,
827                            struct dt_allocation_hint *hint,
828                            struct dt_object_format *dof,
829                            struct thandle *handle)
830 {
831         struct dt_object *next = mdd_object_child(o);
832         return next->do_ops->do_declare_create(env, next, attr, hint,
833                                                dof, handle);
834 }
835
836 static inline
837 int mdo_create_obj(const struct lu_env *env, struct mdd_object *o,
838                    struct lu_attr *attr,
839                    struct dt_allocation_hint *hint,
840                    struct dt_object_format *dof,
841                    struct thandle *handle)
842 {
843         struct dt_object *next = mdd_object_child(o);
844         int rc;
845
846         rc = next->do_ops->do_create(env, next, attr, hint, dof, handle);
847
848         return rc;
849 }
850
851 static inline
852 int mdo_declare_destroy(const struct lu_env *env, struct mdd_object *o,
853                         struct thandle *handle)
854 {
855         struct dt_object *next = mdd_object_child(o);
856         return dt_declare_destroy(env, next, handle);
857 }
858
859 static inline
860 int mdo_destroy(const struct lu_env *env, struct mdd_object *o,
861                 struct thandle *handle)
862 {
863         struct dt_object *next = mdd_object_child(o);
864         return dt_destroy(env, next, handle);
865 }
866
867 static inline struct obd_capa *mdo_capa_get(const struct lu_env *env,
868                                             struct mdd_object *obj,
869                                             struct lustre_capa *old,
870                                             __u64 opc)
871 {
872         struct dt_object *next = mdd_object_child(obj);
873         if (mdd_object_exists(obj) == 0) {
874                 CERROR("%s: object "DFID" not found: rc = -2\n",
875                        mdd_obj_dev_name(obj), PFID(mdd_object_fid(obj)));
876                 return ERR_PTR(-ENOENT);
877         }
878         return next->do_ops->do_capa_get(env, next, old, opc);
879 }
880
881 #endif