Whamcloud - gitweb
LU-1856 build: fix 'out-of-bounds write' errors
[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 <md_object.h>
47 #include <dt_object.h>
48 #ifdef HAVE_QUOTA_SUPPORT
49 # include <lustre_quota.h>
50 #endif
51 #include <lustre_fsfilt.h>
52 #include <lustre/lustre_lfsck_user.h>
53 #include <lustre_fid.h>
54 #include <lustre_capa.h>
55 #include <lprocfs_status.h>
56 #include <lustre_log.h>
57
58 #ifdef HAVE_QUOTA_SUPPORT
59 /* quota stuff */
60 extern quota_interface_t *mds_quota_interface_ref;
61
62 static inline void mdd_quota_wrapper(struct lu_attr *la, unsigned int *qids)
63 {
64         qids[USRQUOTA] = la->la_uid;
65         qids[GRPQUOTA] = la->la_gid;
66 }
67 #endif
68
69 /* PDO lock is unnecessary for current MDT stack because operations
70  * are already protected by ldlm lock */
71 #define MDD_DISABLE_PDO_LOCK    1
72
73 /* Changelog flags */
74 /** changelog is recording */
75 #define CLM_ON    0x00001
76 /** internal error prevented changelogs from starting */
77 #define CLM_ERR   0x00002
78 /* Marker flags */
79 /** changelogs turned on */
80 #define CLM_START 0x10000
81 /** changelogs turned off */
82 #define CLM_FINI  0x20000
83 /** some changelog records purged */
84 #define CLM_PURGE 0x40000
85
86 struct mdd_changelog {
87         cfs_spinlock_t                   mc_lock;    /* for index */
88         int                              mc_flags;
89         int                              mc_mask;
90         __u64                            mc_index;
91         __u64                            mc_starttime;
92         cfs_spinlock_t                   mc_user_lock;
93         int                              mc_lastuser;
94 };
95
96 /** Objects in .lustre dir */
97 struct mdd_dot_lustre_objs {
98         struct mdd_object *mdd_obf;
99 };
100
101 extern const char lfsck_bookmark_name[];
102
103 struct md_lfsck {
104         cfs_mutex_t           ml_mutex;
105         cfs_spinlock_t        ml_lock;
106         struct ptlrpc_thread  ml_thread;
107         struct dt_object     *ml_bookmark_obj;
108         struct dt_object     *ml_it_obj;
109         __u32                 ml_new_scanned;
110         /* Arguments for low layer iteration. */
111         __u32                 ml_args;
112
113         /* Raw value for LFSCK speed limit. */
114         __u32                 ml_speed_limit;
115
116         /* Schedule for every N objects. */
117         __u32                 ml_sleep_rate;
118
119         /* Sleep N jiffies for each schedule. */
120         __u32                 ml_sleep_jif;
121         __u16                 ml_version;
122         unsigned int          ml_paused:1; /* The lfsck is paused. */
123 };
124
125 struct mdd_device {
126         struct md_device                 mdd_md_dev;
127         struct dt_device                *mdd_child;
128         struct obd_device               *mdd_obd_dev;
129         struct lu_fid                    mdd_root_fid;
130         struct dt_device_param           mdd_dt_conf;
131         struct dt_object                *mdd_orphans; /* PENDING directory */
132         struct dt_object                *mdd_capa;
133         struct dt_txn_callback           mdd_txn_cb;
134         cfs_proc_dir_entry_t            *mdd_proc_entry;
135         struct lprocfs_stats            *mdd_stats;
136         struct mdd_changelog             mdd_cl;
137         unsigned long                    mdd_atime_diff;
138         struct mdd_object               *mdd_dot_lustre;
139         struct mdd_dot_lustre_objs       mdd_dot_lustre_objs;
140         struct md_lfsck                  mdd_lfsck;
141         unsigned int                     mdd_sync_permission;
142 };
143
144 enum mod_flags {
145         /* The dir object has been unlinked */
146         DEAD_OBJ   = 1 << 0,
147         APPEND_OBJ = 1 << 1,
148         IMMUTE_OBJ = 1 << 2,
149         ORPHAN_OBJ = 1 << 3,
150 };
151
152 enum mdd_object_role {
153         MOR_SRC_PARENT,
154         MOR_SRC_CHILD,
155         MOR_TGT_PARENT,
156         MOR_TGT_CHILD,
157         MOR_TGT_ORPHAN
158 };
159
160 struct mdd_object {
161         struct md_object   mod_obj;
162         /* open count */
163         __u32             mod_count;
164         __u32             mod_valid;
165         __u64             mod_cltime;
166         unsigned long     mod_flags;
167 #ifndef MDD_DISABLE_PDO_LOCK
168         struct dynlock    mod_pdlock;
169 #endif
170 #ifdef CONFIG_LOCKDEP
171         /* "dep_map" name is assumed by lockdep.h macros. */
172         struct lockdep_map dep_map;
173 #endif
174 };
175
176 struct mdd_thread_info {
177         struct lu_fid             mti_fid;
178         struct lu_fid             mti_fid2; /* used for be & cpu converting */
179         struct lu_attr            mti_la;
180         struct lu_attr            mti_la_for_fix;
181         struct md_attr            mti_ma;
182         struct obd_info           mti_oi;
183         /* mti_orph_ent and mti_orph_key must be conjoint,
184          * then mti_orph_ent::lde_name will be mti_orph_key. */
185         struct lu_dirent          mti_orph_ent;
186         char                      mti_orph_key[NAME_MAX + 1];
187         struct obd_trans_info     mti_oti;
188         struct lu_buf             mti_buf;
189         struct lu_buf             mti_big_buf; /* biggish persistent buf */
190         struct lu_name            mti_name;
191         struct obdo               mti_oa;
192         char                      mti_xattr_buf[LUSTRE_POSIX_ACL_MAX_SIZE];
193         struct dt_allocation_hint mti_hint;
194         struct lov_mds_md        *mti_max_lmm;
195         int                       mti_max_lmm_size;
196         struct llog_cookie       *mti_max_cookie;
197         int                       mti_max_cookie_size;
198         struct dt_object_format   mti_dof;
199         struct obd_quotactl       mti_oqctl;
200 };
201
202 extern const char orph_index_name[];
203
204 extern const struct dt_index_features orph_index_features;
205
206 struct lov_mds_md *mdd_max_lmm_buffer(const struct lu_env *env, int size);
207 struct lov_mds_md *mdd_max_lmm_get(const struct lu_env *env,
208                                    struct mdd_device *mdd);
209
210 struct llog_cookie *mdd_max_cookie_get(const struct lu_env *env,
211                                        struct mdd_device *mdd);
212
213 int mdd_init_obd(const struct lu_env *env, struct mdd_device *mdd,
214                  struct lustre_cfg *cfg);
215 int mdd_fini_obd(const struct lu_env *env, struct mdd_device *mdd,
216                  struct lustre_cfg *lcfg);
217 int __mdd_xattr_set(const struct lu_env *env, struct mdd_object *obj,
218                     const struct lu_buf *buf, const char *name,
219                     int fl, struct thandle *handle);
220 int mdd_xattr_set_txn(const struct lu_env *env, struct mdd_object *obj,
221                       const struct lu_buf *buf, const char *name, int fl,
222                       struct thandle *txn);
223 int mdd_lsm_sanity_check(const struct lu_env *env, struct mdd_object *obj);
224 int mdd_lov_set_md(const struct lu_env *env, struct mdd_object *pobj,
225                    struct mdd_object *child, struct lov_mds_md *lmm,
226                    int lmm_size, struct thandle *handle, int set_stripe);
227 int mdd_lov_create(const struct lu_env *env, struct mdd_device *mdd,
228                    struct mdd_object *parent, struct mdd_object *child,
229                    struct lov_mds_md **lmm, int *lmm_size,
230                    const struct md_op_spec *spec, struct md_attr *ma);
231 int mdd_lov_objid_prepare(struct mdd_device *mdd, struct lov_mds_md *lmm);
232 int mdd_declare_lov_objid_update(const struct lu_env *, struct mdd_device *,
233                                  struct thandle *);
234 void mdd_lov_objid_update(struct mdd_device *mdd, struct lov_mds_md *lmm);
235 void mdd_lov_create_finish(const struct lu_env *env, struct mdd_device *mdd,
236                            struct lov_mds_md *lmm, int lmm_size,
237                            const struct md_op_spec *spec);
238 int mdd_file_lock(const struct lu_env *env, struct md_object *obj,
239                   struct lov_mds_md *lmm, struct ldlm_extent *extent,
240                   struct lustre_handle *lockh);
241 int mdd_file_unlock(const struct lu_env *env, struct md_object *obj,
242                     struct lov_mds_md *lmm, struct lustre_handle *lockh);
243 int mdd_lum_lmm_cmp(const struct lu_env *env, struct md_object *cobj,
244                     const struct md_op_spec *spec, struct md_attr *ma);
245 int mdd_get_md(const struct lu_env *env, struct mdd_object *obj,
246                void *md, int *md_size, const char *name);
247 int mdd_get_md_locked(const struct lu_env *env, struct mdd_object *obj,
248                       void *md, int *md_size, const char *name);
249 int mdd_data_get(const struct lu_env *env, struct mdd_object *obj, void **data);
250 int mdd_la_get(const struct lu_env *env, struct mdd_object *obj,
251                struct lu_attr *la, struct lustre_capa *capa);
252 int mdd_attr_get(const struct lu_env *env, struct md_object *obj,
253                  struct md_attr *ma);
254 int mdd_attr_set(const struct lu_env *env, struct md_object *obj,
255                  const struct md_attr *ma);
256 int mdd_attr_set_internal(const struct lu_env *env,
257                           struct mdd_object *obj,
258                           struct lu_attr *attr,
259                           struct thandle *handle,
260                           int needacl);
261 int mdd_attr_check_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_declare_object_kill(const struct lu_env *env, struct mdd_object *obj,
267                             struct md_attr *ma, struct thandle *handle);
268 int mdd_object_kill(const struct lu_env *env, struct mdd_object *obj,
269                     struct md_attr *ma, struct thandle *handle);
270 int mdd_iattr_get(const struct lu_env *env, struct mdd_object *mdd_obj,
271                   struct md_attr *ma);
272 int mdd_attr_get_internal(const struct lu_env *env, struct mdd_object *mdd_obj,
273                           struct md_attr *ma);
274 int mdd_attr_get_internal_locked(const struct lu_env *env,
275                                  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 md_attr *ma,
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_is_subdir(const struct lu_env *env, struct md_object *mo,
310                   const struct lu_fid *fid, struct lu_fid *sfid);
311 int mdd_may_create(const struct lu_env *env, struct mdd_object *pobj,
312                    struct mdd_object *cobj, int check_perm, int check_nlink);
313 int mdd_may_unlink(const struct lu_env *env, struct mdd_object *pobj,
314                    const struct md_attr *ma);
315 int mdd_may_delete(const struct lu_env *env, struct mdd_object *pobj,
316                    struct mdd_object *cobj, struct md_attr *ma,
317                    int check_perm, int check_empty);
318 int mdd_unlink_sanity_check(const struct lu_env *env, struct mdd_object *pobj,
319                             struct mdd_object *cobj, struct md_attr *ma);
320 int mdd_finish_unlink(const struct lu_env *env, struct mdd_object *obj,
321                       struct md_attr *ma, struct thandle *th);
322 int mdd_object_initialize(const struct lu_env *env, const struct lu_fid *pfid,
323                           const struct lu_name *lname, struct mdd_object *child,
324                           struct md_attr *ma, struct thandle *handle,
325                           const struct md_op_spec *spec);
326 int mdd_link_sanity_check(const struct lu_env *env, struct mdd_object *tgt_obj,
327                           const struct lu_name *lname, struct mdd_object *src_obj);
328 int mdd_is_root(struct mdd_device *mdd, const struct lu_fid *fid);
329 int mdd_lookup(const struct lu_env *env,
330                struct md_object *pobj, const struct lu_name *lname,
331                struct lu_fid* fid, struct md_op_spec *spec);
332 struct lu_buf *mdd_links_get(const struct lu_env *env,
333                              struct mdd_object *mdd_obj);
334 void mdd_lee_unpack(const struct link_ea_entry *lee, int *reclen,
335                     struct lu_name *lname, struct lu_fid *pfid);
336
337 /* mdd_lov.c */
338 int mdd_declare_unlink_log(const struct lu_env *env, struct mdd_object *obj,
339                            struct md_attr *ma, struct thandle *handle);
340 int mdd_unlink_log(const struct lu_env *env, struct mdd_device *mdd,
341                    struct mdd_object *mdd_cobj, struct md_attr *ma);
342
343 int mdd_setattr_log(const struct lu_env *env, struct mdd_device *mdd,
344                     const struct md_attr *ma,
345                     struct lov_mds_md *lmm, int lmm_size,
346                     struct llog_cookie *logcookies, int cookies_size);
347
348 int mdd_get_cookie_size(const struct lu_env *env, struct mdd_device *mdd,
349                         struct lov_mds_md *lmm);
350
351 int mdd_lov_setattr_async(const struct lu_env *env, struct mdd_object *obj,
352                           struct lov_mds_md *lmm, int lmm_size,
353                           struct llog_cookie *logcookies);
354
355 int mdd_lovobj_unlink(const struct lu_env *env, struct mdd_device *mdd,
356                       struct mdd_object *obj, struct lu_attr *la,
357                       struct md_attr *ma, int log_unlink);
358
359 struct mdd_thread_info *mdd_env_info(const struct lu_env *env);
360
361 struct lu_buf *mdd_buf_get(const struct lu_env *env, void *area, ssize_t len);
362 const struct lu_buf *mdd_buf_get_const(const struct lu_env *env,
363                                        const void *area, ssize_t len);
364
365 int __mdd_orphan_cleanup(const struct lu_env *env, struct mdd_device *d);
366 int __mdd_orphan_add(const struct lu_env *, struct mdd_object *,
367                      struct thandle *);
368 int __mdd_orphan_del(const struct lu_env *, struct mdd_object *,
369                      struct thandle *);
370 int orph_index_init(const struct lu_env *env, struct mdd_device *mdd);
371 void orph_index_fini(const struct lu_env *env, struct mdd_device *mdd);
372 int orph_declare_index_insert(const struct lu_env *, struct mdd_object *,
373                               struct thandle *);
374 int orph_declare_index_delete(const struct lu_env *, struct mdd_object *,
375                               struct thandle *);
376
377 /* mdd_lproc.c */
378 void lprocfs_mdd_init_vars(struct lprocfs_static_vars *lvars);
379 int mdd_procfs_init(struct mdd_device *mdd, const char *name);
380 int mdd_procfs_fini(struct mdd_device *mdd);
381 void mdd_lprocfs_time_start(const struct lu_env *env);
382 void mdd_lprocfs_time_end(const struct lu_env *env,
383                           struct mdd_device *mdd, int op);
384
385 /* mdd_object.c */
386 int mdd_get_flags(const struct lu_env *env, struct mdd_object *obj);
387 struct lu_buf *mdd_buf_alloc(const struct lu_env *env, ssize_t len);
388 int mdd_buf_grow(const struct lu_env *env, ssize_t len);
389 void mdd_buf_put(struct lu_buf *buf);
390
391 extern const struct md_dir_operations    mdd_dir_ops;
392 extern const struct md_object_operations mdd_obj_ops;
393
394 int accmode(const struct lu_env *env, struct lu_attr *la, int flags);
395 extern struct lu_context_key mdd_thread_key;
396 extern const struct lu_device_operations mdd_lu_ops;
397
398 struct mdd_object *mdd_object_find(const struct lu_env *env,
399                                    struct mdd_device *d,
400                                    const struct lu_fid *f);
401 int mdd_get_default_md(struct mdd_object *mdd_obj, struct lov_mds_md *lmm);
402 int mdd_readpage(const struct lu_env *env, struct md_object *obj,
403                  const struct lu_rdpg *rdpg);
404 int mdd_declare_llog_record(const struct lu_env *env, struct mdd_device *mdd,
405                             int reclen, struct thandle *handle);
406 int mdd_declare_changelog_store(const struct lu_env *env,
407                                 struct mdd_device *mdd,
408                                 const struct lu_name *fname,
409                                 struct thandle *handle);
410 int mdd_changelog(const struct lu_env *env, enum changelog_rec_type type,
411                   int flags, struct md_object *obj);
412 int mdd_declare_object_create_internal(const struct lu_env *env,
413                                        struct mdd_object *p,
414                                        struct mdd_object *c,
415                                        struct md_attr *ma,
416                                        struct thandle *handle,
417                                        const struct md_op_spec *spec);
418 /* mdd_quota.c*/
419 #ifdef HAVE_QUOTA_SUPPORT
420 int mdd_quota_notify(const struct lu_env *env, struct md_device *m);
421 int mdd_quota_setup(const struct lu_env *env, struct md_device *m,
422                     void *data);
423 int mdd_quota_cleanup(const struct lu_env *env, struct md_device *m);
424 int mdd_quota_recovery(const struct lu_env *env, struct md_device *m);
425 int mdd_quota_check(const struct lu_env *env, struct md_device *m,
426                     __u32 type);
427 int mdd_quota_on(const struct lu_env *env, struct md_device *m,
428                  __u32 type);
429 int mdd_quota_off(const struct lu_env *env, struct md_device *m,
430                   __u32 type);
431 int mdd_quota_setinfo(const struct lu_env *env, struct md_device *m,
432                       __u32 type, __u32 id, struct obd_dqinfo *dqinfo);
433 int mdd_quota_getinfo(const struct lu_env *env, const struct md_device *m,
434                       __u32 type, __u32 id, struct obd_dqinfo *dqinfo);
435 int mdd_quota_setquota(const struct lu_env *env, struct md_device *m,
436                        __u32 type, __u32 id, struct obd_dqblk *dqblk);
437 int mdd_quota_getquota(const struct lu_env *env, const struct md_device *m,
438                        __u32 type, __u32 id, struct obd_dqblk *dqblk);
439 int mdd_quota_getoinfo(const struct lu_env *env, const struct md_device *m,
440                        __u32 type, __u32 id, struct obd_dqinfo *dqinfo);
441 int mdd_quota_getoquota(const struct lu_env *env, const struct md_device *m,
442                         __u32 type, __u32 id, struct obd_dqblk *dqblk);
443 int mdd_quota_invalidate(const struct lu_env *env, struct md_device *m,
444                          __u32 type);
445 int mdd_quota_finvalidate(const struct lu_env *env, struct md_device *m,
446                           __u32 type);
447 #endif
448
449 /* mdd_trans.c */
450 int mdd_lov_destroy(const struct lu_env *env, struct mdd_device *mdd,
451                     struct mdd_object *obj, struct lu_attr *la);
452
453 static inline void mdd_object_put(const struct lu_env *env,
454                                   struct mdd_object *o)
455 {
456         lu_object_put(env, &o->mod_obj.mo_lu);
457 }
458
459 struct thandle *mdd_trans_create(const struct lu_env *env,
460                                  struct mdd_device *mdd);
461 int mdd_trans_start(const struct lu_env *env, struct mdd_device *mdd,
462                     struct thandle *th);
463 void mdd_trans_stop(const struct lu_env *env, struct mdd_device *mdd,
464                     int rc, struct thandle *handle);
465 int mdd_txn_stop_cb(const struct lu_env *env, struct thandle *txn,
466                     void *cookie);
467 int mdd_txn_start_cb(const struct lu_env *env, struct thandle *,
468                      void *cookie);
469
470 /* mdd_lfsck.c */
471 void mdd_lfsck_set_speed(struct md_lfsck *lfsck, __u32 limit);
472 int mdd_lfsck_start(const struct lu_env *env, struct md_lfsck *lfsck,
473                     struct lfsck_start *start);
474 int mdd_lfsck_stop(const struct lu_env *env, struct md_lfsck *lfsck);
475 int mdd_lfsck_setup(const struct lu_env *env, struct mdd_device *mdd);
476 void mdd_lfsck_cleanup(const struct lu_env *env, struct mdd_device *mdd);
477
478 /* mdd_device.c */
479 struct lu_object *mdd_object_alloc(const struct lu_env *env,
480                                    const struct lu_object_header *hdr,
481                                    struct lu_device *d);
482 struct llog_changelog_rec;
483 int mdd_changelog_llog_write(struct mdd_device         *mdd,
484                              struct llog_changelog_rec *rec,
485                              struct thandle            *handle);
486 int mdd_changelog_ext_llog_write(struct mdd_device *mdd,
487                                  struct llog_changelog_ext_rec *rec,
488                                  struct thandle *handle);
489 int mdd_changelog_llog_cancel(const struct lu_env *env, struct mdd_device *mdd,
490                               long long endrec);
491 int mdd_changelog_write_header(struct mdd_device *mdd, int markerflags);
492 int mdd_changelog_on(struct mdd_device *mdd, int on);
493
494 /* mdd_permission.c */
495 #define mdd_cap_t(x) (x)
496
497 #define MDD_CAP_TO_MASK(x) (1 << (x))
498
499 #define mdd_cap_raised(c, flag) (mdd_cap_t(c) & MDD_CAP_TO_MASK(flag))
500
501 /* capable() is copied from linux kernel! */
502 static inline int mdd_capable(struct md_ucred *uc, cfs_cap_t cap)
503 {
504         if (mdd_cap_raised(uc->mu_cap, cap))
505                 return 1;
506         return 0;
507 }
508
509 int mdd_def_acl_get(const struct lu_env *env, struct mdd_object *mdd_obj,
510                     struct md_attr *ma);
511 int mdd_acl_chmod(const struct lu_env *env, struct mdd_object *o, __u32 mode,
512                   struct thandle *handle);
513 int __mdd_declare_acl_init(const struct lu_env *env, struct mdd_object *obj,
514                            int is_dir, struct thandle *handle);
515 int __mdd_acl_init(const struct lu_env *env, struct mdd_object *obj,
516                    struct lu_buf *buf, __u32 *mode, struct thandle *handle);
517 int __mdd_permission_internal(const struct lu_env *env, struct mdd_object *obj,
518                               struct lu_attr *la, int mask, int role);
519 int mdd_permission(const struct lu_env *env,
520                    struct md_object *pobj, struct md_object *cobj,
521                    struct md_attr *ma, int mask);
522 int mdd_capa_get(const struct lu_env *env, struct md_object *obj,
523                  struct lustre_capa *capa, int renewal);
524
525 static inline int lu_device_is_mdd(struct lu_device *d)
526 {
527         return ergo(d != NULL && d->ld_ops != NULL, d->ld_ops == &mdd_lu_ops);
528 }
529
530 static inline struct mdd_device* lu2mdd_dev(struct lu_device *d)
531 {
532         LASSERT(lu_device_is_mdd(d));
533         return container_of0(d, struct mdd_device, mdd_md_dev.md_lu_dev);
534 }
535
536 static inline struct lu_device *mdd2lu_dev(struct mdd_device *d)
537 {
538         return (&d->mdd_md_dev.md_lu_dev);
539 }
540
541 static inline struct mdd_object *lu2mdd_obj(struct lu_object *o)
542 {
543         LASSERT(ergo(o != NULL, lu_device_is_mdd(o->lo_dev)));
544         return container_of0(o, struct mdd_object, mod_obj.mo_lu);
545 }
546
547 static inline struct mdd_device* mdo2mdd(struct md_object *mdo)
548 {
549         return lu2mdd_dev(mdo->mo_lu.lo_dev);
550 }
551
552 static inline struct mdd_object* md2mdd_obj(struct md_object *mdo)
553 {
554         return container_of0(mdo, struct mdd_object, mod_obj);
555 }
556
557 static inline const struct dt_device_operations *
558 mdd_child_ops(struct mdd_device *d)
559 {
560         return d->mdd_child->dd_ops;
561 }
562
563 static inline struct lu_object *mdd2lu_obj(struct mdd_object *obj)
564 {
565         return &obj->mod_obj.mo_lu;
566 }
567
568 static inline struct dt_object* mdd_object_child(struct mdd_object *o)
569 {
570         return container_of0(lu_object_next(mdd2lu_obj(o)),
571                              struct dt_object, do_lu);
572 }
573
574 static inline struct obd_device *mdd2obd_dev(struct mdd_device *mdd)
575 {
576         return mdd->mdd_obd_dev;
577 }
578
579 static inline struct mdd_device *mdd_obj2mdd_dev(struct mdd_object *obj)
580 {
581         return mdo2mdd(&obj->mod_obj);
582 }
583
584 static inline const struct lu_fid *mdo2fid(const struct mdd_object *obj)
585 {
586         return lu_object_fid(&obj->mod_obj.mo_lu);
587 }
588
589 static inline int mdd_object_obf(const struct mdd_object *obj)
590 {
591         return lu_fid_eq(mdo2fid(obj), &LU_OBF_FID);
592 }
593
594 static inline cfs_umode_t mdd_object_type(const struct mdd_object *obj)
595 {
596         return lu_object_attr(&obj->mod_obj.mo_lu);
597 }
598
599 static inline int mdd_lov_mdsize(const struct lu_env *env,
600                                  struct mdd_device *mdd)
601 {
602         struct obd_device *obd = mdd2obd_dev(mdd);
603         return obd->u.mds.mds_max_mdsize;
604 }
605
606 static inline int mdd_lov_cookiesize(const struct lu_env *env,
607                                      struct mdd_device *mdd)
608 {
609         struct obd_device *obd = mdd2obd_dev(mdd);
610         return obd->u.mds.mds_max_cookiesize;
611 }
612
613 static inline int mdd_is_immutable(struct mdd_object *obj)
614 {
615         return obj->mod_flags & IMMUTE_OBJ;
616 }
617
618 static inline int mdd_is_dead_obj(struct mdd_object *obj)
619 {
620         return obj && obj->mod_flags & DEAD_OBJ;
621 }
622
623 static inline int mdd_is_append(struct mdd_object *obj)
624 {
625         return obj->mod_flags & APPEND_OBJ;
626 }
627
628 static inline int mdd_object_exists(struct mdd_object *obj)
629 {
630         return lu_object_exists(mdd2lu_obj(obj));
631 }
632
633 static inline const struct lu_fid *mdd_object_fid(struct mdd_object *obj)
634 {
635         return lu_object_fid(mdd2lu_obj(obj));
636 }
637
638 static inline struct lustre_capa *mdd_object_capa(const struct lu_env *env,
639                                                   const struct mdd_object *obj)
640 {
641         struct md_capainfo *ci = md_capainfo(env);
642         const struct lu_fid *fid = mdo2fid(obj);
643         int i;
644
645         /* NB: in mdt_init0 */
646         if (!ci)
647                 return BYPASS_CAPA;
648         for (i = 0; i < MD_CAPAINFO_MAX; i++)
649                 if (lu_fid_eq(&ci->mc_fid[i], fid))
650                         return ci->mc_capa[i];
651         return NULL;
652 }
653
654 static inline void mdd_set_capainfo(const struct lu_env *env, int offset,
655                                     const struct mdd_object *obj,
656                                     struct lustre_capa *capa)
657 {
658         struct md_capainfo *ci = md_capainfo(env);
659         const struct lu_fid *fid = mdo2fid(obj);
660
661         LASSERT(offset >= 0 && offset < MD_CAPAINFO_MAX);
662         /* NB: in mdt_init0 */
663         if (!ci)
664                 return;
665         ci->mc_fid[offset]  = *fid;
666         ci->mc_capa[offset] = capa;
667 }
668
669 static inline const char *mdd_obj_dev_name(const struct mdd_object *obj)
670 {
671         return lu_dev_name(obj->mod_obj.mo_lu.lo_dev);
672 }
673
674 #define MAX_ATIME_DIFF 60
675
676 enum {
677         LPROC_MDD_NR
678 };
679
680 static inline int mdd_permission_internal(const struct lu_env *env,
681                                           struct mdd_object *obj,
682                                           struct lu_attr *la, int mask)
683 {
684         return __mdd_permission_internal(env, obj, la, mask, -1);
685 }
686
687 static inline int mdd_permission_internal_locked(const struct lu_env *env,
688                                                  struct mdd_object *obj,
689                                                  struct lu_attr *la, int mask,
690                                                  enum mdd_object_role role)
691 {
692         return __mdd_permission_internal(env, obj, la, mask, role);
693 }
694
695 static inline int mdo_data_get(const struct lu_env *env,
696                                struct mdd_object *obj,
697                                void **data)
698 {
699         struct dt_object *next = mdd_object_child(obj);
700         next->do_ops->do_data_get(env, next, data);
701         return 0;
702 }
703
704 /* mdd inline func for calling osd_dt_object ops */
705 static inline int mdo_attr_get(const struct lu_env *env, struct mdd_object *obj,
706                                struct lu_attr *la, struct lustre_capa *capa)
707 {
708         struct dt_object *next = mdd_object_child(obj);
709         return next->do_ops->do_attr_get(env, next, la, capa);
710 }
711
712 static inline int mdo_declare_attr_set(const struct lu_env *env,
713                                        struct mdd_object *obj,
714                                        const struct lu_attr *la,
715                                        struct thandle *handle)
716 {
717         struct dt_object *next = mdd_object_child(obj);
718         return dt_declare_attr_set(env, next, la, handle);
719 }
720
721 static inline int mdo_attr_set(const struct lu_env *env,
722                                struct mdd_object *obj,
723                                const struct lu_attr *la,
724                                struct thandle *handle,
725                                struct lustre_capa *capa)
726 {
727         struct dt_object *next = mdd_object_child(obj);
728         if (mdd_object_exists(obj) == 0) {
729                 CERROR("%s: object "DFID" not found: rc = -2\n",
730                        mdd_obj_dev_name(obj), PFID(mdd_object_fid(obj)));
731                 return -ENOENT;
732         }
733         return next->do_ops->do_attr_set(env, next, la, handle, capa);
734 }
735
736 static inline int mdo_xattr_get(const struct lu_env *env,struct mdd_object *obj,
737                                 struct lu_buf *buf, const char *name,
738                                 struct lustre_capa *capa)
739 {
740         struct dt_object *next = mdd_object_child(obj);
741         return next->do_ops->do_xattr_get(env, next, buf, name, capa);
742 }
743
744 static inline int mdo_declare_xattr_set(const struct lu_env *env,
745                                         struct mdd_object *obj,
746                                         const struct lu_buf *buf,
747                                         const char *name,
748                                         int fl, struct thandle *handle)
749 {
750         struct dt_object *next = mdd_object_child(obj);
751         return dt_declare_xattr_set(env, next, buf, name, fl, handle);
752 }
753
754 static inline int mdo_xattr_set(const struct lu_env *env,struct mdd_object *obj,
755                                 const struct lu_buf *buf, const char *name,
756                                 int fl, struct thandle *handle,
757                                 struct lustre_capa *capa)
758 {
759         struct dt_object *next = mdd_object_child(obj);
760         if (mdd_object_exists(obj) == 0) {
761                 CERROR("%s: object "DFID" not found: rc = -2\n",
762                        mdd_obj_dev_name(obj), PFID(mdd_object_fid(obj)));
763                 return -ENOENT;
764         }
765         return next->do_ops->do_xattr_set(env, next, buf, name, fl, handle,
766                                           capa);
767 }
768
769 static inline int mdo_declare_xattr_del(const struct lu_env *env,
770                                         struct mdd_object *obj,
771                                         const char *name,
772                                         struct thandle *handle)
773 {
774         struct dt_object *next = mdd_object_child(obj);
775         return dt_declare_xattr_del(env, next, name, handle);
776 }
777
778 static inline int mdo_xattr_del(const struct lu_env *env,struct mdd_object *obj,
779                                 const char *name, struct thandle *handle,
780                                 struct lustre_capa *capa)
781 {
782         struct dt_object *next = mdd_object_child(obj);
783         if (mdd_object_exists(obj) == 0) {
784                 CERROR("%s: object "DFID" not found: rc = -2\n",
785                        mdd_obj_dev_name(obj), PFID(mdd_object_fid(obj)));
786                 return -ENOENT;
787         }
788         return next->do_ops->do_xattr_del(env, next, name, handle, capa);
789 }
790
791 static inline
792 int mdo_xattr_list(const struct lu_env *env, struct mdd_object *obj,
793                    struct lu_buf *buf, struct lustre_capa *capa)
794 {
795         struct dt_object *next = mdd_object_child(obj);
796         if (mdd_object_exists(obj) == 0) {
797                 CERROR("%s: object "DFID" not found: rc = -2\n",
798                        mdd_obj_dev_name(obj), PFID(mdd_object_fid(obj)));
799                 return -ENOENT;
800         }
801         return next->do_ops->do_xattr_list(env, next, buf, capa);
802 }
803
804 static inline
805 int mdo_index_try(const struct lu_env *env, struct mdd_object *obj,
806                                  const struct dt_index_features *feat)
807 {
808         struct dt_object *next = mdd_object_child(obj);
809         return next->do_ops->do_index_try(env, next, feat);
810 }
811
812 static inline
813 int mdo_declare_index_insert(const struct lu_env *env, struct mdd_object *obj,
814                              const struct lu_fid *fid, const char *name,
815                              struct thandle *handle)
816 {
817         struct dt_object *next = mdd_object_child(obj);
818         int              rc = 0;
819
820         /*
821          * if the object doesn't exist yet, then it's supposed to be created
822          * and declaration of the creation should be enough to insert ./..
823          */
824         if (mdd_object_exists(obj)) {
825                 rc = -ENOTDIR;
826                 if (dt_try_as_dir(env, next))
827                         rc = dt_declare_insert(env, next,
828                                                (struct dt_rec *)fid,
829                                                (const struct dt_key *)name,
830                                                handle);
831         }
832
833         return rc;
834 }
835
836 static inline
837 int mdo_declare_index_delete(const struct lu_env *env, struct mdd_object *obj,
838                              const char *name, struct thandle *handle)
839 {
840         struct dt_object *next = mdd_object_child(obj);
841
842         if (!dt_try_as_dir(env, next))
843                 return -ENOTDIR;
844
845         return dt_declare_delete(env, next, (const struct dt_key *)name,
846                                  handle);
847 }
848
849 static inline int mdo_declare_ref_add(const struct lu_env *env,
850                                       struct mdd_object *obj,
851                                       struct thandle *handle)
852 {
853         struct dt_object *next = mdd_object_child(obj);
854         return dt_declare_ref_add(env, next, handle);
855 }
856
857 static inline int mdo_ref_add(const struct lu_env *env, struct mdd_object *obj,
858                               struct thandle *handle)
859 {
860         struct dt_object *next = mdd_object_child(obj);
861         if (mdd_object_exists(obj) == 0) {
862                 CERROR("%s: object "DFID" not found: rc = -2\n",
863                        mdd_obj_dev_name(obj), PFID(mdd_object_fid(obj)));
864                 return -ENOENT;
865         }
866         return next->do_ops->do_ref_add(env, next, handle);
867 }
868
869 static inline int mdo_declare_ref_del(const struct lu_env *env,
870                                       struct mdd_object *obj,
871                                       struct thandle *handle)
872 {
873         struct dt_object *next = mdd_object_child(obj);
874         return dt_declare_ref_del(env, next, handle);
875 }
876
877 static inline int mdo_ref_del(const struct lu_env *env, struct mdd_object *obj,
878                               struct thandle *handle)
879 {
880         struct dt_object *next = mdd_object_child(obj);
881         if (mdd_object_exists(obj) == 0) {
882                 CERROR("%s: object "DFID" not found: rc = -2\n",
883                        mdd_obj_dev_name(obj), PFID(mdd_object_fid(obj)));
884                 return -ENOENT;
885         }
886         return next->do_ops->do_ref_del(env, next, handle);
887 }
888
889 static inline
890 int mdo_declare_create_obj(const struct lu_env *env, struct mdd_object *o,
891                            struct lu_attr *attr,
892                            struct dt_allocation_hint *hint,
893                            struct dt_object_format *dof,
894                            struct thandle *handle)
895 {
896         struct dt_object *next = mdd_object_child(o);
897         return next->do_ops->do_declare_create(env, next, attr, hint,
898                                                dof, handle);
899 }
900
901 static inline
902 int mdo_create_obj(const struct lu_env *env, struct mdd_object *o,
903                    struct lu_attr *attr,
904                    struct dt_allocation_hint *hint,
905                    struct dt_object_format *dof,
906                    struct thandle *handle)
907 {
908         struct dt_object *next = mdd_object_child(o);
909         return next->do_ops->do_create(env, next, attr, hint, dof, handle);
910 }
911
912 static inline
913 int mdo_declare_destroy(const struct lu_env *env, struct mdd_object *o,
914                         struct thandle *handle)
915 {
916         struct dt_object *next = mdd_object_child(o);
917         return dt_declare_destroy(env, next, handle);
918 }
919
920 static inline
921 int mdo_destroy(const struct lu_env *env, struct mdd_object *o,
922                 struct thandle *handle)
923 {
924         struct dt_object *next = mdd_object_child(o);
925         return dt_destroy(env, next, handle);
926 }
927
928 static inline struct obd_capa *mdo_capa_get(const struct lu_env *env,
929                                             struct mdd_object *obj,
930                                             struct lustre_capa *old,
931                                             __u64 opc)
932 {
933         struct dt_object *next = mdd_object_child(obj);
934         if (mdd_object_exists(obj) == 0) {
935                 CERROR("%s: object "DFID" not found: rc = -2\n",
936                        mdd_obj_dev_name(obj), PFID(mdd_object_fid(obj)));
937                 return ERR_PTR(-ENOENT);
938         }
939         return next->do_ops->do_capa_get(env, next, old, opc);
940 }
941
942 #endif