Whamcloud - gitweb
LU-5039 mdd: do orphan cleanup asynchronously
[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 };
89
90 struct mdd_generic_thread {
91         struct completion       mgt_started;
92         struct completion       mgt_finished;
93         void                   *mgt_data;
94         bool                    mgt_abort;
95         bool                    mgt_init;
96 };
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; /* /ROOT */
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         cfs_proc_dir_entry_t            *mdd_proc_entry;
108         struct mdd_changelog             mdd_cl;
109         unsigned long                    mdd_atime_diff;
110         struct mdd_object               *mdd_dot_lustre;
111         struct mdd_dot_lustre_objs       mdd_dot_lustre_objs;
112         unsigned int                     mdd_sync_permission;
113         int                              mdd_connects;
114         struct local_oid_storage        *mdd_los;
115         struct mdd_generic_thread        mdd_orph_cleanup_thread;
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 #ifdef CONFIG_LOCKDEP
134         /* "dep_map" name is assumed by lockdep.h macros. */
135         struct lockdep_map dep_map;
136 #endif
137 };
138
139 struct mdd_thread_info {
140         struct lu_fid             mti_fid;
141         struct lu_fid             mti_fid2; /* used for be & cpu converting */
142         struct lu_attr            mti_la;
143         struct lu_attr            mti_la_for_fix;
144         struct lu_attr            mti_pattr;
145         struct lu_attr            mti_cattr;
146         struct md_attr            mti_ma;
147         struct obd_info           mti_oi;
148         /* mti_ent and mti_key must be conjoint,
149          * then mti_ent::lde_name will be mti_key. */
150         struct lu_dirent          mti_ent;
151         char                      mti_key[NAME_MAX + 16];
152         struct obd_trans_info     mti_oti;
153         struct lu_buf             mti_buf[4];
154         struct lu_buf             mti_big_buf; /* biggish persistent buf */
155         struct lu_buf             mti_link_buf; /* buf for link ea */
156         struct lu_name            mti_name;
157         struct lu_name            mti_name2;
158         struct obdo               mti_oa;
159         char                      mti_xattr_buf[LUSTRE_POSIX_ACL_MAX_SIZE];
160         struct dt_allocation_hint mti_hint;
161         struct lov_mds_md        *mti_max_lmm;
162         int                       mti_max_lmm_size;
163         struct llog_cookie       *mti_max_cookie;
164         int                       mti_max_cookie_size;
165         struct dt_object_format   mti_dof;
166         struct obd_quotactl       mti_oqctl;
167         struct linkea_data        mti_link_data;
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 int mdd_attr_get(const struct lu_env *env, struct md_object *obj,
221                  struct md_attr *ma);
222 int mdd_attr_set(const struct lu_env *env, struct md_object *obj,
223                  const struct md_attr *ma);
224 int mdd_attr_set_internal(const struct lu_env *env,
225                           struct mdd_object *obj,
226                           const struct lu_attr *attr,
227                           struct thandle *handle,
228                           int needacl);
229 int mdd_attr_check_set_internal(const struct lu_env *env,
230                                 struct mdd_object *obj,
231                                 struct lu_attr *attr,
232                                 struct thandle *handle,
233                                 int needacl);
234 int mdd_declare_object_kill(const struct lu_env *env, struct mdd_object *obj,
235                             struct md_attr *ma, struct thandle *handle);
236 int mdd_object_kill(const struct lu_env *env, struct mdd_object *obj,
237                     struct md_attr *ma, struct thandle *handle);
238 int mdd_iattr_get(const struct lu_env *env, struct mdd_object *mdd_obj,
239                   struct md_attr *ma);
240 int mdd_object_create_internal(const struct lu_env *env, struct mdd_object *p,
241                                struct mdd_object *c, struct lu_attr *attr,
242                                struct thandle *handle,
243                                const struct md_op_spec *spec);
244 int mdd_attr_check_set_internal_locked(const struct lu_env *env,
245                                        struct mdd_object *obj,
246                                        struct lu_attr *attr,
247                                        struct thandle *handle,
248                                        int needacl);
249 int mdd_lmm_get_locked(const struct lu_env *env, struct mdd_object *mdd_obj,
250                        struct md_attr *ma);
251
252 /* mdd_lock.c */
253 void mdd_write_lock(const struct lu_env *env, struct mdd_object *obj,
254                     enum mdd_object_role role);
255 void mdd_read_lock(const struct lu_env *env, struct mdd_object *obj,
256                    enum mdd_object_role role);
257 void mdd_write_unlock(const struct lu_env *env, struct mdd_object *obj);
258 void mdd_read_unlock(const struct lu_env *env, struct mdd_object *obj);
259 int mdd_write_locked(const struct lu_env *env, struct mdd_object *obj);
260
261 unsigned long mdd_name2hash(const char *name);
262 void *mdd_pdo_write_lock(const struct lu_env *env, struct mdd_object *obj,
263                          const char *name, enum mdd_object_role role);
264 void mdd_pdo_write_unlock(const struct lu_env *env, struct mdd_object *obj,
265                           void *dlh);
266 /* mdd_dir.c */
267 int mdd_parent_fid(const struct lu_env *env, struct mdd_object *obj,
268                    struct lu_fid *fid);
269 int mdd_is_subdir(const struct lu_env *env, struct md_object *mo,
270                   const struct lu_fid *fid, struct lu_fid *sfid);
271 int mdd_may_create(const struct lu_env *env, struct mdd_object *pobj,
272                    struct mdd_object *cobj, int check_perm, int check_nlink);
273 int mdd_may_unlink(const struct lu_env *env, struct mdd_object *pobj,
274                    const struct lu_attr *attr);
275 int mdd_may_delete(const struct lu_env *env, struct mdd_object *pobj,
276                    struct mdd_object *cobj, struct lu_attr *cattr,
277                    struct lu_attr *src_attr, int check_perm, int check_empty);
278 int mdd_unlink_sanity_check(const struct lu_env *env, struct mdd_object *pobj,
279                             struct mdd_object *cobj, struct lu_attr *cattr);
280 int mdd_finish_unlink(const struct lu_env *env, struct mdd_object *obj,
281                       struct md_attr *ma, const struct mdd_object *pobj,
282                       const struct lu_name *lname, struct thandle *th);
283
284 int mdd_link_sanity_check(const struct lu_env *env, struct mdd_object *tgt_obj,
285                           const struct lu_name *lname, struct mdd_object *src_obj);
286 int mdd_is_root(struct mdd_device *mdd, const struct lu_fid *fid);
287 int mdd_lookup(const struct lu_env *env,
288                struct md_object *pobj, const struct lu_name *lname,
289                struct lu_fid* fid, struct md_op_spec *spec);
290 int mdd_links_read(const struct lu_env *env, struct mdd_object *mdd_obj,
291                    struct linkea_data *ldata);
292 int mdd_declare_links_add(const struct lu_env *env, struct mdd_object *mdd_obj,
293                           struct thandle *handle, struct linkea_data *ldata);
294 int mdd_links_write(const struct lu_env *env, struct mdd_object *mdd_obj,
295                     struct linkea_data *ldata, struct thandle *handle);
296 struct lu_buf *mdd_links_get(const struct lu_env *env,
297                              struct mdd_object *mdd_obj);
298 int mdd_links_rename(const struct lu_env *env,
299                      struct mdd_object *mdd_obj,
300                      const struct lu_fid *oldpfid,
301                      const struct lu_name *oldlname,
302                      const struct lu_fid *newpfid,
303                      const struct lu_name *newlname,
304                      struct thandle *handle,
305                      struct linkea_data *ldata,
306                      int first, int check);
307 int mdd_declare_links_add(const struct lu_env *env, struct mdd_object *mdd_obj,
308                           struct thandle *handle, struct linkea_data *ldata);
309
310 /* mdd_lov.c */
311 int mdd_declare_unlink_log(const struct lu_env *env, struct mdd_object *obj,
312                            struct md_attr *ma, struct thandle *handle);
313 int mdd_unlink_log(const struct lu_env *env, struct mdd_device *mdd,
314                    struct mdd_object *mdd_cobj, struct md_attr *ma);
315
316 int mdd_setattr_log(const struct lu_env *env, struct mdd_device *mdd,
317                     const struct md_attr *ma,
318                     struct lov_mds_md *lmm, int lmm_size,
319                     struct llog_cookie *logcookies, int cookies_size);
320
321 int mdd_get_cookie_size(const struct lu_env *env, struct mdd_device *mdd,
322                         struct lov_mds_md *lmm);
323
324 int mdd_lov_setattr_async(const struct lu_env *env, struct mdd_object *obj,
325                           struct lov_mds_md *lmm, int lmm_size,
326                           struct llog_cookie *logcookies);
327
328 int mdd_lovobj_unlink(const struct lu_env *env, struct mdd_device *mdd,
329                       struct mdd_object *obj, struct lu_attr *la,
330                       struct md_attr *ma, int log_unlink);
331
332 struct mdd_thread_info *mdd_env_info(const struct lu_env *env);
333
334 const struct lu_name *mdd_name_get_const(const struct lu_env *env,
335                                          const void *area, ssize_t len);
336 struct lu_buf *mdd_buf_get(const struct lu_env *env, void *area, ssize_t len);
337 const struct lu_buf *mdd_buf_get_const(const struct lu_env *env,
338                                        const void *area, ssize_t len);
339
340 int mdd_orphan_cleanup(const struct lu_env *env, struct mdd_device *d);
341 int __mdd_orphan_add(const struct lu_env *, struct mdd_object *,
342                      struct thandle *);
343 int __mdd_orphan_del(const struct lu_env *, struct mdd_object *,
344                      struct thandle *);
345 int orph_index_init(const struct lu_env *env, struct mdd_device *mdd);
346 void orph_index_fini(const struct lu_env *env, struct mdd_device *mdd);
347 int orph_declare_index_insert(const struct lu_env *, struct mdd_object *,
348                               umode_t mode, struct thandle *);
349 int orph_declare_index_delete(const struct lu_env *, struct mdd_object *,
350                               struct thandle *);
351
352 /* mdd_lproc.c */
353 void lprocfs_mdd_init_vars(struct lprocfs_static_vars *lvars);
354 int mdd_procfs_init(struct mdd_device *mdd, const char *name);
355 int mdd_procfs_fini(struct mdd_device *mdd);
356
357 /* mdd_object.c */
358 int mdd_get_flags(const struct lu_env *env, struct mdd_object *obj);
359 struct lu_buf *mdd_buf_alloc(const struct lu_env *env, ssize_t len);
360 int mdd_buf_grow(const struct lu_env *env, ssize_t len);
361 void mdd_buf_put(struct lu_buf *buf);
362
363 struct lu_buf *mdd_link_buf_alloc(const struct lu_env *env, ssize_t len);
364 int mdd_link_buf_grow(const struct lu_env *env, ssize_t len);
365 extern const struct md_dir_operations    mdd_dir_ops;
366 extern const struct md_object_operations mdd_obj_ops;
367
368 int accmode(const struct lu_env *env, struct lu_attr *la, int flags);
369 extern struct lu_context_key mdd_thread_key;
370 extern const struct lu_device_operations mdd_lu_ops;
371
372 struct mdd_object *mdd_object_find(const struct lu_env *env,
373                                    struct mdd_device *d,
374                                    const struct lu_fid *f);
375 int mdd_get_default_md(struct mdd_object *mdd_obj, struct lov_mds_md *lmm);
376 int mdd_readpage(const struct lu_env *env, struct md_object *obj,
377                  const struct lu_rdpg *rdpg);
378 int mdd_declare_changelog_store(const struct lu_env *env,
379                                 struct mdd_device *mdd,
380                                 const struct lu_name *fname,
381                                 struct thandle *handle);
382 int mdd_changelog_store(const struct lu_env *env, struct mdd_device *mdd,
383                         struct llog_changelog_rec *rec, struct thandle *th);
384 int mdd_changelog_data_store(const struct lu_env *env, struct mdd_device *mdd,
385                              enum changelog_rec_type type, int flags,
386                              struct mdd_object *mdd_obj,
387                              struct thandle *handle);
388 int mdd_changelog_ns_store(const struct lu_env *env, struct mdd_device *mdd,
389                            enum changelog_rec_type type, unsigned flags,
390                            struct mdd_object *target, struct mdd_object *parent,
391                            const struct lu_name *tname, struct thandle *handle);
392 int mdd_declare_object_create_internal(const struct lu_env *env,
393                                        struct mdd_object *p,
394                                        struct mdd_object *c,
395                                        struct lu_attr *attr,
396                                        struct thandle *handle,
397                                        const struct md_op_spec *spec);
398
399 /* mdd_trans.c */
400 int mdd_lov_destroy(const struct lu_env *env, struct mdd_device *mdd,
401                     struct mdd_object *obj, struct lu_attr *la);
402
403 void mdd_object_make_hint(const struct lu_env *env, struct mdd_object *parent,
404                           struct mdd_object *child, struct lu_attr *attr);
405
406 static inline void mdd_object_get(struct mdd_object *o)
407 {
408         lu_object_get(&o->mod_obj.mo_lu);
409 }
410
411 static inline void mdd_object_put(const struct lu_env *env,
412                                   struct mdd_object *o)
413 {
414         lu_object_put(env, &o->mod_obj.mo_lu);
415 }
416
417 struct thandle *mdd_trans_create(const struct lu_env *env,
418                                  struct mdd_device *mdd);
419 int mdd_trans_start(const struct lu_env *env, struct mdd_device *mdd,
420                     struct thandle *th);
421 void mdd_trans_stop(const struct lu_env *env, struct mdd_device *mdd,
422                     int rc, struct thandle *handle);
423 int mdd_txn_stop_cb(const struct lu_env *env, struct thandle *txn,
424                     void *cookie);
425 int mdd_txn_start_cb(const struct lu_env *env, struct thandle *,
426                      void *cookie);
427
428 /* mdd_device.c */
429 struct lu_object *mdd_object_alloc(const struct lu_env *env,
430                                    const struct lu_object_header *hdr,
431                                    struct lu_device *d);
432 int mdd_local_file_create(const struct lu_env *env, struct mdd_device *mdd,
433                           const struct lu_fid *pfid, const char *name,
434                           __u32 mode, struct lu_fid *fid);
435
436 int mdd_acl_chmod(const struct lu_env *env, struct mdd_object *o, __u32 mode,
437                   struct thandle *handle);
438 int __mdd_declare_acl_init(const struct lu_env *env, struct mdd_object *obj,
439                            int is_dir, struct thandle *handle);
440 int mdd_acl_set(const struct lu_env *env, struct mdd_object *obj,
441                 const struct lu_buf *buf, int fl);
442 int __mdd_fix_mode_acl(const struct lu_env *env, struct lu_buf *buf,
443                        __u32 *mode);
444 int __mdd_permission_internal(const struct lu_env *env, struct mdd_object *obj,
445                               struct lu_attr *la, int mask, int role);
446 int mdd_permission(const struct lu_env *env,
447                    struct md_object *pobj, struct md_object *cobj,
448                    struct md_attr *ma, int mask);
449 int mdd_capa_get(const struct lu_env *env, struct md_object *obj,
450                  struct lustre_capa *capa, int renewal);
451 int mdd_generic_thread_start(struct mdd_generic_thread *thread,
452                              int (*func)(void *), void *data, char *name);
453 void mdd_generic_thread_stop(struct mdd_generic_thread *thread);
454
455 /* mdd_prepare.c */
456 int mdd_compat_fixes(const struct lu_env *env, struct mdd_device *mdd);
457
458 /* inline functions */
459 static inline int lu_device_is_mdd(struct lu_device *d)
460 {
461         return ergo(d != NULL && d->ld_ops != NULL, d->ld_ops == &mdd_lu_ops);
462 }
463
464 static inline struct mdd_device* lu2mdd_dev(struct lu_device *d)
465 {
466         LASSERT(lu_device_is_mdd(d));
467         return container_of0(d, struct mdd_device, mdd_md_dev.md_lu_dev);
468 }
469
470 static inline struct lu_device *mdd2lu_dev(struct mdd_device *d)
471 {
472         return (&d->mdd_md_dev.md_lu_dev);
473 }
474
475 static inline struct mdd_object *lu2mdd_obj(struct lu_object *o)
476 {
477         LASSERT(ergo(o != NULL, lu_device_is_mdd(o->lo_dev)));
478         return container_of0(o, struct mdd_object, mod_obj.mo_lu);
479 }
480
481 static inline struct mdd_device* mdo2mdd(struct md_object *mdo)
482 {
483         return lu2mdd_dev(mdo->mo_lu.lo_dev);
484 }
485
486 static inline struct mdd_object* md2mdd_obj(struct md_object *mdo)
487 {
488         return container_of0(mdo, struct mdd_object, mod_obj);
489 }
490
491 static inline const struct dt_device_operations *
492 mdd_child_ops(struct mdd_device *d)
493 {
494         return d->mdd_child->dd_ops;
495 }
496
497 static inline struct lu_object *mdd2lu_obj(struct mdd_object *obj)
498 {
499         return &obj->mod_obj.mo_lu;
500 }
501
502 static inline struct dt_object* mdd_object_child(struct mdd_object *o)
503 {
504         return container_of0(lu_object_next(mdd2lu_obj(o)),
505                              struct dt_object, do_lu);
506 }
507
508 static inline struct obd_device *mdd2obd_dev(struct mdd_device *mdd)
509 {
510         return (mdd->mdd_md_dev.md_lu_dev.ld_obd);
511 }
512
513 static inline struct mdd_device *mdd_obj2mdd_dev(struct mdd_object *obj)
514 {
515         return mdo2mdd(&obj->mod_obj);
516 }
517
518 static inline const struct lu_fid *mdo2fid(const struct mdd_object *obj)
519 {
520         return lu_object_fid(&obj->mod_obj.mo_lu);
521 }
522
523 static inline int mdd_object_obf(const struct mdd_object *obj)
524 {
525         return lu_fid_eq(mdo2fid(obj), &LU_OBF_FID);
526 }
527
528 static inline umode_t mdd_object_type(const struct mdd_object *obj)
529 {
530         return lu_object_attr(&obj->mod_obj.mo_lu);
531 }
532
533 static inline int mdd_is_immutable(struct mdd_object *obj)
534 {
535         return obj->mod_flags & IMMUTE_OBJ;
536 }
537
538 static inline int mdd_is_dead_obj(struct mdd_object *obj)
539 {
540         return obj && obj->mod_flags & DEAD_OBJ;
541 }
542
543 static inline int mdd_is_append(struct mdd_object *obj)
544 {
545         return obj->mod_flags & APPEND_OBJ;
546 }
547
548 static inline int mdd_object_exists(struct mdd_object *obj)
549 {
550         return lu_object_exists(mdd2lu_obj(obj));
551 }
552
553 static inline int mdd_object_remote(struct mdd_object *obj)
554 {
555         return lu_object_remote(mdd2lu_obj(obj));
556 }
557
558 static inline const struct lu_fid *mdd_object_fid(struct mdd_object *obj)
559 {
560         return lu_object_fid(mdd2lu_obj(obj));
561 }
562
563 static inline struct seq_server_site *mdd_seq_site(struct mdd_device *mdd)
564 {
565         return mdd2lu_dev(mdd)->ld_site->ld_seq_site;
566 }
567
568 static inline struct lustre_capa *mdd_object_capa(const struct lu_env *env,
569                                                   const struct mdd_object *obj)
570 {
571         struct lu_capainfo *lci = lu_capainfo_get(env);
572         const struct lu_fid *fid = mdo2fid(obj);
573         int i;
574
575         /* NB: in mdt_init0 */
576         if (lci == NULL)
577                 return BYPASS_CAPA;
578
579         for (i = 0; i < LU_CAPAINFO_MAX; i++)
580                 if (lu_fid_eq(&lci->lci_fid[i], fid))
581                         return lci->lci_capa[i];
582         return NULL;
583 }
584
585 static inline void mdd_set_capainfo(const struct lu_env *env, int offset,
586                                     const struct mdd_object *obj,
587                                     struct lustre_capa *capa)
588 {
589         struct lu_capainfo *lci = lu_capainfo_get(env);
590         const struct lu_fid *fid = mdo2fid(obj);
591
592         LASSERT(offset >= 0 && offset < LU_CAPAINFO_MAX);
593         /* NB: in mdt_init0 */
594         if (lci == NULL)
595                 return;
596
597         lci->lci_fid[offset]  = *fid;
598         lci->lci_capa[offset] = capa;
599 }
600
601 static inline const char *mdd_obj_dev_name(const struct mdd_object *obj)
602 {
603         return lu_dev_name(obj->mod_obj.mo_lu.lo_dev);
604 }
605
606 #define MAX_ATIME_DIFF 60
607
608 enum {
609         LPROC_MDD_NR
610 };
611
612 static inline int mdd_permission_internal(const struct lu_env *env,
613                                           struct mdd_object *obj,
614                                           struct lu_attr *la, int mask)
615 {
616         return __mdd_permission_internal(env, obj, la, mask, -1);
617 }
618
619 static inline int mdd_permission_internal_locked(const struct lu_env *env,
620                                                  struct mdd_object *obj,
621                                                  struct lu_attr *la, int mask,
622                                                  enum mdd_object_role role)
623 {
624         return __mdd_permission_internal(env, obj, la, mask, role);
625 }
626
627 static inline int mdo_data_get(const struct lu_env *env,
628                                struct mdd_object *obj,
629                                void **data)
630 {
631         struct dt_object *next = mdd_object_child(obj);
632         next->do_ops->do_data_get(env, next, data);
633         return 0;
634 }
635
636 /* mdd inline func for calling osd_dt_object ops */
637 static inline int mdo_attr_get(const struct lu_env *env, struct mdd_object *obj,
638                                struct lu_attr *la, struct lustre_capa *capa)
639 {
640         struct dt_object *next = mdd_object_child(obj);
641         return next->do_ops->do_attr_get(env, next, la, capa);
642 }
643
644 static inline int mdo_declare_attr_set(const struct lu_env *env,
645                                        struct mdd_object *obj,
646                                        const struct lu_attr *la,
647                                        struct thandle *handle)
648 {
649         struct dt_object *next = mdd_object_child(obj);
650         return dt_declare_attr_set(env, next, la, handle);
651 }
652
653 static inline int mdo_attr_set(const struct lu_env *env,
654                                struct mdd_object *obj,
655                                const struct lu_attr *la,
656                                struct thandle *handle,
657                                struct lustre_capa *capa)
658 {
659         struct dt_object *next = mdd_object_child(obj);
660         if (mdd_object_exists(obj) == 0) {
661                 CERROR("%s: object "DFID" not found: rc = -2\n",
662                        mdd_obj_dev_name(obj), PFID(mdd_object_fid(obj)));
663                 return -ENOENT;
664         }
665         return next->do_ops->do_attr_set(env, next, la, handle, capa);
666 }
667
668 static inline int mdo_xattr_get(const struct lu_env *env,struct mdd_object *obj,
669                                 struct lu_buf *buf, const char *name,
670                                 struct lustre_capa *capa)
671 {
672         struct dt_object *next = mdd_object_child(obj);
673         return next->do_ops->do_xattr_get(env, next, buf, name, capa);
674 }
675
676 static inline int mdo_declare_xattr_set(const struct lu_env *env,
677                                         struct mdd_object *obj,
678                                         const struct lu_buf *buf,
679                                         const char *name,
680                                         int fl, struct thandle *handle)
681 {
682         struct dt_object *next = mdd_object_child(obj);
683         return dt_declare_xattr_set(env, next, buf, name, fl, handle);
684 }
685
686 static inline int mdo_xattr_set(const struct lu_env *env,struct mdd_object *obj,
687                                 const struct lu_buf *buf, const char *name,
688                                 int fl, struct thandle *handle,
689                                 struct lustre_capa *capa)
690 {
691         struct dt_object *next = mdd_object_child(obj);
692         if (mdd_object_exists(obj) == 0) {
693                 CERROR("%s: object "DFID" not found: rc = -2\n",
694                        mdd_obj_dev_name(obj), PFID(mdd_object_fid(obj)));
695                 return -ENOENT;
696         }
697         return next->do_ops->do_xattr_set(env, next, buf, name, fl, handle,
698                                           capa);
699 }
700
701 static inline int mdo_declare_xattr_del(const struct lu_env *env,
702                                         struct mdd_object *obj,
703                                         const char *name,
704                                         struct thandle *handle)
705 {
706         struct dt_object *next = mdd_object_child(obj);
707         return dt_declare_xattr_del(env, next, name, handle);
708 }
709
710 static inline int mdo_xattr_del(const struct lu_env *env,struct mdd_object *obj,
711                                 const char *name, struct thandle *handle,
712                                 struct lustre_capa *capa)
713 {
714         struct dt_object *next = mdd_object_child(obj);
715         if (mdd_object_exists(obj) == 0) {
716                 CERROR("%s: object "DFID" not found: rc = -2\n",
717                        mdd_obj_dev_name(obj), PFID(mdd_object_fid(obj)));
718                 return -ENOENT;
719         }
720         return next->do_ops->do_xattr_del(env, next, name, handle, capa);
721 }
722
723 static inline
724 int mdo_xattr_list(const struct lu_env *env, struct mdd_object *obj,
725                    struct lu_buf *buf, 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_list(env, next, buf, capa);
734 }
735
736 static inline
737 int mdo_index_try(const struct lu_env *env, struct mdd_object *obj,
738                                  const struct dt_index_features *feat)
739 {
740         struct dt_object *next = mdd_object_child(obj);
741         return next->do_ops->do_index_try(env, next, feat);
742 }
743
744 static inline
745 int mdo_declare_index_insert(const struct lu_env *env, struct mdd_object *obj,
746                              const struct lu_fid *fid, const char *name,
747                              struct thandle *handle)
748 {
749         struct dt_object *next = mdd_object_child(obj);
750         int              rc = 0;
751
752         /*
753          * if the object doesn't exist yet, then it's supposed to be created
754          * and declaration of the creation should be enough to insert ./..
755          */
756          /* FIXME: remote object should not be awared by MDD layer, but local
757           * creation does not declare insert ./.. (comments above), which
758           * is required by remote directory creation.
759           * This remote check should be removed when mdd_object_exists check is
760           * removed.
761           */
762          if (mdd_object_exists(obj) || mdd_object_remote(obj)) {
763                 rc = -ENOTDIR;
764                 if (dt_try_as_dir(env, next))
765                         rc = dt_declare_insert(env, next,
766                                                (struct dt_rec *)fid,
767                                                (const struct dt_key *)name,
768                                                handle);
769         }
770
771         return rc;
772 }
773
774 static inline
775 int mdo_declare_index_delete(const struct lu_env *env, struct mdd_object *obj,
776                              const char *name, struct thandle *handle)
777 {
778         struct dt_object *next = mdd_object_child(obj);
779
780         if (!dt_try_as_dir(env, next))
781                 return -ENOTDIR;
782
783         return dt_declare_delete(env, next, (const struct dt_key *)name,
784                                  handle);
785 }
786
787 static inline int mdo_declare_ref_add(const struct lu_env *env,
788                                       struct mdd_object *obj,
789                                       struct thandle *handle)
790 {
791         struct dt_object *next = mdd_object_child(obj);
792         return dt_declare_ref_add(env, next, handle);
793 }
794
795 static inline int mdo_ref_add(const struct lu_env *env, struct mdd_object *obj,
796                               struct thandle *handle)
797 {
798         struct dt_object *next = mdd_object_child(obj);
799         if (mdd_object_exists(obj) == 0) {
800                 CERROR("%s: object "DFID" not found: rc = -2\n",
801                        mdd_obj_dev_name(obj), PFID(mdd_object_fid(obj)));
802                 return -ENOENT;
803         }
804         return next->do_ops->do_ref_add(env, next, handle);
805 }
806
807 static inline int mdo_declare_ref_del(const struct lu_env *env,
808                                       struct mdd_object *obj,
809                                       struct thandle *handle)
810 {
811         struct dt_object *next = mdd_object_child(obj);
812         return dt_declare_ref_del(env, next, handle);
813 }
814
815 static inline int mdo_ref_del(const struct lu_env *env, struct mdd_object *obj,
816                               struct thandle *handle)
817 {
818         struct dt_object *next = mdd_object_child(obj);
819         if (mdd_object_exists(obj) == 0) {
820                 CERROR("%s: object "DFID" not found: rc = -2\n",
821                        mdd_obj_dev_name(obj), PFID(mdd_object_fid(obj)));
822                 return -ENOENT;
823         }
824         return next->do_ops->do_ref_del(env, next, handle);
825 }
826
827 static inline
828 int mdo_declare_create_obj(const struct lu_env *env, struct mdd_object *o,
829                            struct lu_attr *attr,
830                            struct dt_allocation_hint *hint,
831                            struct dt_object_format *dof,
832                            struct thandle *handle)
833 {
834         struct dt_object *next = mdd_object_child(o);
835         return next->do_ops->do_declare_create(env, next, attr, hint,
836                                                dof, handle);
837 }
838
839 static inline
840 int mdo_create_obj(const struct lu_env *env, struct mdd_object *o,
841                    struct lu_attr *attr,
842                    struct dt_allocation_hint *hint,
843                    struct dt_object_format *dof,
844                    struct thandle *handle)
845 {
846         struct dt_object *next = mdd_object_child(o);
847         int rc;
848
849         rc = next->do_ops->do_create(env, next, attr, hint, dof, handle);
850
851         return rc;
852 }
853
854 static inline
855 int mdo_declare_destroy(const struct lu_env *env, struct mdd_object *o,
856                         struct thandle *handle)
857 {
858         struct dt_object *next = mdd_object_child(o);
859         return dt_declare_destroy(env, next, handle);
860 }
861
862 static inline
863 int mdo_destroy(const struct lu_env *env, struct mdd_object *o,
864                 struct thandle *handle)
865 {
866         struct dt_object *next = mdd_object_child(o);
867         return dt_destroy(env, next, handle);
868 }
869
870 static inline struct obd_capa *mdo_capa_get(const struct lu_env *env,
871                                             struct mdd_object *obj,
872                                             struct lustre_capa *old,
873                                             __u64 opc)
874 {
875         struct dt_object *next = mdd_object_child(obj);
876         if (mdd_object_exists(obj) == 0) {
877                 CERROR("%s: object "DFID" not found: rc = -2\n",
878                        mdd_obj_dev_name(obj), PFID(mdd_object_fid(obj)));
879                 return ERR_PTR(-ENOENT);
880         }
881         return next->do_ops->do_capa_get(env, next, old, opc);
882 }
883
884 #endif