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