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