Whamcloud - gitweb
71c2c3c5d4a8bafe73f1a4c6d441a75142ba7981
[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_data_store(const struct lu_env *env, struct mdd_device *mdd,
394                              enum changelog_rec_type type, int flags,
395                              struct mdd_object *mdd_obj,
396                              struct thandle *handle);
397 int mdd_changelog_ns_store(const struct lu_env *env, struct mdd_device *mdd,
398                            enum changelog_rec_type type, unsigned flags,
399                            struct mdd_object *target, struct mdd_object *parent,
400                            const struct lu_name *tname, struct thandle *handle);
401 int mdd_declare_object_create_internal(const struct lu_env *env,
402                                        struct mdd_object *p,
403                                        struct mdd_object *c,
404                                        struct lu_attr *attr,
405                                        struct thandle *handle,
406                                        const struct md_op_spec *spec);
407
408 /* mdd_trans.c */
409 int mdd_lov_destroy(const struct lu_env *env, struct mdd_device *mdd,
410                     struct mdd_object *obj, struct lu_attr *la);
411
412 void mdd_object_make_hint(const struct lu_env *env, struct mdd_object *parent,
413                           struct mdd_object *child, struct lu_attr *attr);
414
415 static inline void mdd_object_get(struct mdd_object *o)
416 {
417         lu_object_get(&o->mod_obj.mo_lu);
418 }
419
420 static inline void mdd_object_put(const struct lu_env *env,
421                                   struct mdd_object *o)
422 {
423         lu_object_put(env, &o->mod_obj.mo_lu);
424 }
425
426 struct thandle *mdd_trans_create(const struct lu_env *env,
427                                  struct mdd_device *mdd);
428 int mdd_trans_start(const struct lu_env *env, struct mdd_device *mdd,
429                     struct thandle *th);
430 void mdd_trans_stop(const struct lu_env *env, struct mdd_device *mdd,
431                     int rc, struct thandle *handle);
432 int mdd_txn_stop_cb(const struct lu_env *env, struct thandle *txn,
433                     void *cookie);
434 int mdd_txn_start_cb(const struct lu_env *env, struct thandle *,
435                      void *cookie);
436
437 /* mdd_lfsck.c */
438 int mdd_lfsck_set_speed(const struct lu_env *env, struct md_lfsck *lfsck,
439                         __u32 limit);
440 int mdd_lfsck_start(const struct lu_env *env, struct md_lfsck *lfsck,
441                     struct lfsck_start *start);
442 int mdd_lfsck_stop(const struct lu_env *env, struct md_lfsck *lfsck,
443                    bool pause);
444 int mdd_lfsck_setup(const struct lu_env *env, struct mdd_device *mdd);
445 void mdd_lfsck_cleanup(const struct lu_env *env, struct mdd_device *mdd);
446 int mdd_lfsck_dump(const struct lu_env *env, struct md_lfsck *lfsck,
447                    __u16 type, char *buf, int len);
448
449 /* mdd_device.c */
450 struct lu_object *mdd_object_alloc(const struct lu_env *env,
451                                    const struct lu_object_header *hdr,
452                                    struct lu_device *d);
453 int mdd_local_file_create(const struct lu_env *env, struct mdd_device *mdd,
454                           const struct lu_fid *pfid, const char *name,
455                           __u32 mode, struct lu_fid *fid);
456
457 int mdd_acl_chmod(const struct lu_env *env, struct mdd_object *o, __u32 mode,
458                   struct thandle *handle);
459 int __mdd_declare_acl_init(const struct lu_env *env, struct mdd_object *obj,
460                            int is_dir, struct thandle *handle);
461 int mdd_acl_set(const struct lu_env *env, struct mdd_object *obj,
462                 const struct lu_buf *buf, int fl);
463 int __mdd_fix_mode_acl(const struct lu_env *env, struct lu_buf *buf,
464                        __u32 *mode);
465 int __mdd_permission_internal(const struct lu_env *env, struct mdd_object *obj,
466                               struct lu_attr *la, int mask, int role);
467 int mdd_permission(const struct lu_env *env,
468                    struct md_object *pobj, struct md_object *cobj,
469                    struct md_attr *ma, int mask);
470 int mdd_capa_get(const struct lu_env *env, struct md_object *obj,
471                  struct lustre_capa *capa, int renewal);
472
473 /* mdd_prepare.c */
474 int mdd_compat_fixes(const struct lu_env *env, struct mdd_device *mdd);
475
476 static inline int lu_device_is_mdd(struct lu_device *d)
477 {
478         return ergo(d != NULL && d->ld_ops != NULL, d->ld_ops == &mdd_lu_ops);
479 }
480
481 static inline struct mdd_device* lu2mdd_dev(struct lu_device *d)
482 {
483         LASSERT(lu_device_is_mdd(d));
484         return container_of0(d, struct mdd_device, mdd_md_dev.md_lu_dev);
485 }
486
487 static inline struct lu_device *mdd2lu_dev(struct mdd_device *d)
488 {
489         return (&d->mdd_md_dev.md_lu_dev);
490 }
491
492 static inline struct mdd_object *lu2mdd_obj(struct lu_object *o)
493 {
494         LASSERT(ergo(o != NULL, lu_device_is_mdd(o->lo_dev)));
495         return container_of0(o, struct mdd_object, mod_obj.mo_lu);
496 }
497
498 static inline struct mdd_device* mdo2mdd(struct md_object *mdo)
499 {
500         return lu2mdd_dev(mdo->mo_lu.lo_dev);
501 }
502
503 static inline struct mdd_object* md2mdd_obj(struct md_object *mdo)
504 {
505         return container_of0(mdo, struct mdd_object, mod_obj);
506 }
507
508 static inline const struct dt_device_operations *
509 mdd_child_ops(struct mdd_device *d)
510 {
511         return d->mdd_child->dd_ops;
512 }
513
514 static inline struct lu_object *mdd2lu_obj(struct mdd_object *obj)
515 {
516         return &obj->mod_obj.mo_lu;
517 }
518
519 static inline struct dt_object* mdd_object_child(struct mdd_object *o)
520 {
521         return container_of0(lu_object_next(mdd2lu_obj(o)),
522                              struct dt_object, do_lu);
523 }
524
525 static inline struct obd_device *mdd2obd_dev(struct mdd_device *mdd)
526 {
527         return (mdd->mdd_md_dev.md_lu_dev.ld_obd);
528 }
529
530 static inline struct mdd_device *mdd_obj2mdd_dev(struct mdd_object *obj)
531 {
532         return mdo2mdd(&obj->mod_obj);
533 }
534
535 static inline const struct lu_fid *mdo2fid(const struct mdd_object *obj)
536 {
537         return lu_object_fid(&obj->mod_obj.mo_lu);
538 }
539
540 static inline int mdd_object_obf(const struct mdd_object *obj)
541 {
542         return lu_fid_eq(mdo2fid(obj), &LU_OBF_FID);
543 }
544
545 static inline cfs_umode_t mdd_object_type(const struct mdd_object *obj)
546 {
547         return lu_object_attr(&obj->mod_obj.mo_lu);
548 }
549
550 static inline int mdd_is_immutable(struct mdd_object *obj)
551 {
552         return obj->mod_flags & IMMUTE_OBJ;
553 }
554
555 static inline int mdd_is_dead_obj(struct mdd_object *obj)
556 {
557         return obj && obj->mod_flags & DEAD_OBJ;
558 }
559
560 static inline int mdd_is_append(struct mdd_object *obj)
561 {
562         return obj->mod_flags & APPEND_OBJ;
563 }
564
565 static inline int mdd_object_exists(struct mdd_object *obj)
566 {
567         return lu_object_exists(mdd2lu_obj(obj));
568 }
569
570 static inline int mdd_object_remote(struct mdd_object *obj)
571 {
572         return lu_object_remote(mdd2lu_obj(obj));
573 }
574
575 static inline const struct lu_fid *mdd_object_fid(struct mdd_object *obj)
576 {
577         return lu_object_fid(mdd2lu_obj(obj));
578 }
579
580 static inline struct seq_server_site *mdd_seq_site(struct mdd_device *mdd)
581 {
582         return mdd2lu_dev(mdd)->ld_site->ld_seq_site;
583 }
584
585 static inline struct lustre_capa *mdd_object_capa(const struct lu_env *env,
586                                                   const struct mdd_object *obj)
587 {
588         struct md_capainfo *ci = md_capainfo(env);
589         const struct lu_fid *fid = mdo2fid(obj);
590         int i;
591
592         /* NB: in mdt_init0 */
593         if (!ci)
594                 return BYPASS_CAPA;
595         for (i = 0; i < MD_CAPAINFO_MAX; i++)
596                 if (lu_fid_eq(&ci->mc_fid[i], fid))
597                         return ci->mc_capa[i];
598         return NULL;
599 }
600
601 static inline void mdd_set_capainfo(const struct lu_env *env, int offset,
602                                     const struct mdd_object *obj,
603                                     struct lustre_capa *capa)
604 {
605         struct md_capainfo *ci = md_capainfo(env);
606         const struct lu_fid *fid = mdo2fid(obj);
607
608         LASSERT(offset >= 0 && offset < MD_CAPAINFO_MAX);
609         /* NB: in mdt_init0 */
610         if (!ci)
611                 return;
612         ci->mc_fid[offset]  = *fid;
613         ci->mc_capa[offset] = capa;
614 }
615
616 static inline const char *mdd_obj_dev_name(const struct mdd_object *obj)
617 {
618         return lu_dev_name(obj->mod_obj.mo_lu.lo_dev);
619 }
620
621 #define MAX_ATIME_DIFF 60
622
623 enum {
624         LPROC_MDD_NR
625 };
626
627 static inline int mdd_permission_internal(const struct lu_env *env,
628                                           struct mdd_object *obj,
629                                           struct lu_attr *la, int mask)
630 {
631         return __mdd_permission_internal(env, obj, la, mask, -1);
632 }
633
634 static inline int mdd_permission_internal_locked(const struct lu_env *env,
635                                                  struct mdd_object *obj,
636                                                  struct lu_attr *la, int mask,
637                                                  enum mdd_object_role role)
638 {
639         return __mdd_permission_internal(env, obj, la, mask, role);
640 }
641
642 static inline int mdo_data_get(const struct lu_env *env,
643                                struct mdd_object *obj,
644                                void **data)
645 {
646         struct dt_object *next = mdd_object_child(obj);
647         next->do_ops->do_data_get(env, next, data);
648         return 0;
649 }
650
651 /* mdd inline func for calling osd_dt_object ops */
652 static inline int mdo_attr_get(const struct lu_env *env, struct mdd_object *obj,
653                                struct lu_attr *la, struct lustre_capa *capa)
654 {
655         struct dt_object *next = mdd_object_child(obj);
656         return next->do_ops->do_attr_get(env, next, la, capa);
657 }
658
659 static inline int mdo_declare_attr_set(const struct lu_env *env,
660                                        struct mdd_object *obj,
661                                        const struct lu_attr *la,
662                                        struct thandle *handle)
663 {
664         struct dt_object *next = mdd_object_child(obj);
665         return dt_declare_attr_set(env, next, la, handle);
666 }
667
668 static inline int mdo_attr_set(const struct lu_env *env,
669                                struct mdd_object *obj,
670                                const struct lu_attr *la,
671                                struct thandle *handle,
672                                struct lustre_capa *capa)
673 {
674         struct dt_object *next = mdd_object_child(obj);
675         if (mdd_object_exists(obj) == 0) {
676                 CERROR("%s: object "DFID" not found: rc = -2\n",
677                        mdd_obj_dev_name(obj), PFID(mdd_object_fid(obj)));
678                 return -ENOENT;
679         }
680         return next->do_ops->do_attr_set(env, next, la, handle, capa);
681 }
682
683 static inline int mdo_xattr_get(const struct lu_env *env,struct mdd_object *obj,
684                                 struct lu_buf *buf, const char *name,
685                                 struct lustre_capa *capa)
686 {
687         struct dt_object *next = mdd_object_child(obj);
688         return next->do_ops->do_xattr_get(env, next, buf, name, capa);
689 }
690
691 static inline int mdo_declare_xattr_set(const struct lu_env *env,
692                                         struct mdd_object *obj,
693                                         const struct lu_buf *buf,
694                                         const char *name,
695                                         int fl, struct thandle *handle)
696 {
697         struct dt_object *next = mdd_object_child(obj);
698         return dt_declare_xattr_set(env, next, buf, name, fl, handle);
699 }
700
701 static inline int mdo_xattr_set(const struct lu_env *env,struct mdd_object *obj,
702                                 const struct lu_buf *buf, const char *name,
703                                 int fl, struct thandle *handle,
704                                 struct lustre_capa *capa)
705 {
706         struct dt_object *next = mdd_object_child(obj);
707         if (mdd_object_exists(obj) == 0) {
708                 CERROR("%s: object "DFID" not found: rc = -2\n",
709                        mdd_obj_dev_name(obj), PFID(mdd_object_fid(obj)));
710                 return -ENOENT;
711         }
712         return next->do_ops->do_xattr_set(env, next, buf, name, fl, handle,
713                                           capa);
714 }
715
716 static inline int mdo_declare_xattr_del(const struct lu_env *env,
717                                         struct mdd_object *obj,
718                                         const char *name,
719                                         struct thandle *handle)
720 {
721         struct dt_object *next = mdd_object_child(obj);
722         return dt_declare_xattr_del(env, next, name, handle);
723 }
724
725 static inline int mdo_xattr_del(const struct lu_env *env,struct mdd_object *obj,
726                                 const char *name, struct thandle *handle,
727                                 struct lustre_capa *capa)
728 {
729         struct dt_object *next = mdd_object_child(obj);
730         if (mdd_object_exists(obj) == 0) {
731                 CERROR("%s: object "DFID" not found: rc = -2\n",
732                        mdd_obj_dev_name(obj), PFID(mdd_object_fid(obj)));
733                 return -ENOENT;
734         }
735         return next->do_ops->do_xattr_del(env, next, name, handle, capa);
736 }
737
738 static inline
739 int mdo_xattr_list(const struct lu_env *env, struct mdd_object *obj,
740                    struct lu_buf *buf, struct lustre_capa *capa)
741 {
742         struct dt_object *next = mdd_object_child(obj);
743         if (mdd_object_exists(obj) == 0) {
744                 CERROR("%s: object "DFID" not found: rc = -2\n",
745                        mdd_obj_dev_name(obj), PFID(mdd_object_fid(obj)));
746                 return -ENOENT;
747         }
748         return next->do_ops->do_xattr_list(env, next, buf, capa);
749 }
750
751 static inline
752 int mdo_index_try(const struct lu_env *env, struct mdd_object *obj,
753                                  const struct dt_index_features *feat)
754 {
755         struct dt_object *next = mdd_object_child(obj);
756         return next->do_ops->do_index_try(env, next, feat);
757 }
758
759 static inline
760 int mdo_declare_index_insert(const struct lu_env *env, struct mdd_object *obj,
761                              const struct lu_fid *fid, const char *name,
762                              struct thandle *handle)
763 {
764         struct dt_object *next = mdd_object_child(obj);
765         int              rc = 0;
766
767         /*
768          * if the object doesn't exist yet, then it's supposed to be created
769          * and declaration of the creation should be enough to insert ./..
770          */
771          /* FIXME: remote object should not be awared by MDD layer, but local
772           * creation does not declare insert ./.. (comments above), which
773           * is required by remote directory creation.
774           * This remote check should be removed when mdd_object_exists check is
775           * removed.
776           */
777          if (mdd_object_exists(obj) || mdd_object_remote(obj)) {
778                 rc = -ENOTDIR;
779                 if (dt_try_as_dir(env, next))
780                         rc = dt_declare_insert(env, next,
781                                                (struct dt_rec *)fid,
782                                                (const struct dt_key *)name,
783                                                handle);
784         }
785
786         return rc;
787 }
788
789 static inline
790 int mdo_declare_index_delete(const struct lu_env *env, struct mdd_object *obj,
791                              const char *name, struct thandle *handle)
792 {
793         struct dt_object *next = mdd_object_child(obj);
794
795         if (!dt_try_as_dir(env, next))
796                 return -ENOTDIR;
797
798         return dt_declare_delete(env, next, (const struct dt_key *)name,
799                                  handle);
800 }
801
802 static inline int mdo_declare_ref_add(const struct lu_env *env,
803                                       struct mdd_object *obj,
804                                       struct thandle *handle)
805 {
806         struct dt_object *next = mdd_object_child(obj);
807         return dt_declare_ref_add(env, next, handle);
808 }
809
810 static inline int mdo_ref_add(const struct lu_env *env, struct mdd_object *obj,
811                               struct thandle *handle)
812 {
813         struct dt_object *next = mdd_object_child(obj);
814         if (mdd_object_exists(obj) == 0) {
815                 CERROR("%s: object "DFID" not found: rc = -2\n",
816                        mdd_obj_dev_name(obj), PFID(mdd_object_fid(obj)));
817                 return -ENOENT;
818         }
819         return next->do_ops->do_ref_add(env, next, handle);
820 }
821
822 static inline int mdo_declare_ref_del(const struct lu_env *env,
823                                       struct mdd_object *obj,
824                                       struct thandle *handle)
825 {
826         struct dt_object *next = mdd_object_child(obj);
827         return dt_declare_ref_del(env, next, handle);
828 }
829
830 static inline int mdo_ref_del(const struct lu_env *env, struct mdd_object *obj,
831                               struct thandle *handle)
832 {
833         struct dt_object *next = mdd_object_child(obj);
834         if (mdd_object_exists(obj) == 0) {
835                 CERROR("%s: object "DFID" not found: rc = -2\n",
836                        mdd_obj_dev_name(obj), PFID(mdd_object_fid(obj)));
837                 return -ENOENT;
838         }
839         return next->do_ops->do_ref_del(env, next, handle);
840 }
841
842 static inline
843 int mdo_declare_create_obj(const struct lu_env *env, struct mdd_object *o,
844                            struct lu_attr *attr,
845                            struct dt_allocation_hint *hint,
846                            struct dt_object_format *dof,
847                            struct thandle *handle)
848 {
849         struct dt_object *next = mdd_object_child(o);
850         return next->do_ops->do_declare_create(env, next, attr, hint,
851                                                dof, handle);
852 }
853
854 static inline
855 int mdo_create_obj(const struct lu_env *env, struct mdd_object *o,
856                    struct lu_attr *attr,
857                    struct dt_allocation_hint *hint,
858                    struct dt_object_format *dof,
859                    struct thandle *handle)
860 {
861         struct dt_object *next = mdd_object_child(o);
862         int rc;
863
864         rc = next->do_ops->do_create(env, next, attr, hint, dof, handle);
865
866         return rc;
867 }
868
869 static inline
870 int mdo_declare_destroy(const struct lu_env *env, struct mdd_object *o,
871                         struct thandle *handle)
872 {
873         struct dt_object *next = mdd_object_child(o);
874         return dt_declare_destroy(env, next, handle);
875 }
876
877 static inline
878 int mdo_destroy(const struct lu_env *env, struct mdd_object *o,
879                 struct thandle *handle)
880 {
881         struct dt_object *next = mdd_object_child(o);
882         return dt_destroy(env, next, handle);
883 }
884
885 static inline struct obd_capa *mdo_capa_get(const struct lu_env *env,
886                                             struct mdd_object *obj,
887                                             struct lustre_capa *old,
888                                             __u64 opc)
889 {
890         struct dt_object *next = mdd_object_child(obj);
891         if (mdd_object_exists(obj) == 0) {
892                 CERROR("%s: object "DFID" not found: rc = -2\n",
893                        mdd_obj_dev_name(obj), PFID(mdd_object_fid(obj)));
894                 return ERR_PTR(-ENOENT);
895         }
896         return next->do_ops->do_capa_get(env, next, old, opc);
897 }
898
899 #endif