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