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