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