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