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