Whamcloud - gitweb
Branch: b_new_cmd
[fs/lustre-release.git] / lustre / mdd / mdd_internal.h
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *  mdd/mdd_internel.c
4  *
5  *  Copyright (C) 2006 Cluster File Systems, Inc.
6  *   Author: Wang Di <wangdi@clusterfs.com>
7  *
8  *   This file is part of the Lustre file system, http://www.lustre.org
9  *   Lustre is a trademark of Cluster File Systems, Inc.
10  *
11  *   You may have signed or agreed to another license before downloading
12  *   this software.  If so, you are bound by the terms and conditions
13  *   of that agreement, and the following does not apply to you.  See the
14  *   LICENSE file included with this distribution for more information.
15  *
16  *   If you did not agree to a different license, then this copy of Lustre
17  *   is open source software; you can redistribute it and/or modify it
18  *   under the terms of version 2 of the GNU General Public License as
19  *   published by the Free Software Foundation.
20  *
21  *   In either case, Lustre is distributed in the hope that it will be
22  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
23  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  *   license text for more details.
25  */
26
27 #ifndef _MDD_INTERNAL_H
28 #define _MDD_INTERNAL_H
29
30 #include <asm/semaphore.h>
31
32 #include <linux/lustre_acl.h>
33 #include <md_object.h>
34 #include <dt_object.h>
35 #include <linux/sched.h>
36 #include <linux/capability.h>
37
38 enum mdd_txn_op {
39         MDD_TXN_OBJECT_DESTROY_OP = 0,
40         MDD_TXN_OBJECT_CREATE_OP,
41         MDD_TXN_ATTR_SET_OP,
42         MDD_TXN_XATTR_SET_OP,
43         MDD_TXN_INDEX_INSERT_OP,
44         MDD_TXN_INDEX_DELETE_OP,
45         MDD_TXN_LINK_OP,
46         MDD_TXN_UNLINK_OP,
47         MDD_TXN_RENAME_OP,
48         MDD_TXN_RENAME_TGT_OP,
49         MDD_TXN_CREATE_DATA_OP,
50         MDD_TXN_MKDIR_OP,
51         MDD_TXN_LAST_OP
52 };
53
54 struct mdd_txn_op_descr {
55         enum mdd_txn_op mod_op;
56         unsigned int    mod_credits;
57 };
58
59 struct mdd_device {
60         struct md_device                 mdd_md_dev;
61         struct dt_device                *mdd_child;
62         struct obd_device               *mdd_obd_dev;
63         struct lu_fid                    mdd_root_fid;
64         struct dt_device_param           mdd_dt_conf;
65         struct dt_object                *mdd_orphans;
66         struct dt_txn_callback           mdd_txn_cb;
67         cfs_proc_dir_entry_t            *mdd_proc_entry;
68         struct lprocfs_stats            *mdd_stats;
69         struct mdd_txn_op_descr          mdd_tod[MDD_TXN_LAST_OP];
70 };
71
72 enum mod_flags {
73         /*The dir object has been unlinked*/
74         DEAD_OBJ = 1 << 0,
75         APPEND_OBJ = 1 << 1,
76         IMMUTE_OBJ = 1 << 2
77 };
78
79 #define LUSTRE_APPEND_FL LDISKFS_APPEND_FL
80 #define LUSTRE_IMMUTABLE_FL LDISKFS_IMMUTABLE_FL
81
82 struct mdd_object {
83         struct md_object  mod_obj;
84         /* open count */
85         __u32             mod_count;
86         __u32             mod_valid;
87         unsigned long     mod_flags;
88 };
89
90 struct orph_key {
91         /* fid of the object*/
92         struct lu_fid ok_fid;
93         /* type of operation: unlink, truncate */
94         __u32         ok_op;
95 };
96
97 struct mdd_thread_info {
98         struct txn_param      mti_param;
99         struct lu_fid         mti_fid;
100         struct lu_attr        mti_la;
101         struct md_attr        mti_ma;
102         struct lu_attr        mti_la_for_fix;
103         struct lov_mds_md     mti_lmm;
104         struct obd_info       mti_oi;
105         struct orph_key       mti_orph_key;
106         struct obd_trans_info mti_oti;
107         struct lu_buf         mti_buf;
108         char                  mti_xattr_buf[LUSTRE_POSIX_ACL_MAX_SIZE];
109 };
110
111 int mdd_init_obd(const struct lu_env *env, struct mdd_device *mdd,
112                  struct lustre_cfg *cfg);
113 int mdd_fini_obd(const struct lu_env *, struct mdd_device *);
114 int mdd_xattr_set_txn(const struct lu_env *env, struct mdd_object *obj,
115                       const struct lu_buf *buf, const char *name, int fl,
116                       struct thandle *txn);
117 int mdd_lov_set_md(const struct lu_env *env, struct mdd_object *pobj,
118                    struct mdd_object *child, struct lov_mds_md *lmm,
119                    int lmm_size, struct thandle *handle, int set_stripe);
120 int mdd_lov_create(const struct lu_env *env, struct mdd_device *mdd,
121                    struct mdd_object *parent, struct mdd_object *child,
122                    struct lov_mds_md **lmm, int *lmm_size,
123                    const struct md_create_spec *spec, struct lu_attr *la);
124 void mdd_lov_create_finish(const struct lu_env *env,
125                            struct mdd_device *mdd, int rc);
126 int mdd_get_md(const struct lu_env *env, struct mdd_object *obj,
127                void *md, int *md_size, const char *name);
128 int mdd_get_md_locked(const struct lu_env *env, struct mdd_object *obj,
129                       void *md, int *md_size, const char *name);
130 int mdd_la_get(const struct lu_env *env, struct mdd_object *obj,
131                struct lu_attr *la, struct lustre_capa *capa);
132 int mdd_attr_set_internal(const struct lu_env *env, struct mdd_object *o,
133                           const struct lu_attr *attr, struct thandle *handle,
134                           const int needacl);
135 int mdd_object_kill(const struct lu_env *env, struct mdd_object *obj,
136                     struct md_attr *ma);
137 int mdd_iattr_get(const struct lu_env *env, struct mdd_object *mdd_obj, 
138                   struct md_attr *ma);
139 int mdd_attr_get_internal_locked(const struct lu_env *env,
140                                  struct mdd_object *mdd_obj, 
141                                  struct md_attr *ma);
142 int mdd_object_create_internal(const struct lu_env *env,
143                                struct mdd_object *obj, struct md_attr *ma,
144                                struct thandle *handle);
145 int mdd_attr_set_internal_locked(const struct lu_env *env,
146                                  struct mdd_object *o,
147                                  const struct lu_attr *attr,
148                                  struct thandle *handle);
149 int mdd_lmm_get_locked(const struct lu_env *env, struct mdd_object *mdd_obj,
150                        struct md_attr *ma);
151 /* mdd_dir.c */
152 int mdd_unlink_sanity_check(const struct lu_env *env, struct mdd_object *pobj,
153                             struct mdd_object *cobj, struct md_attr *ma);
154 int mdd_finish_unlink(const struct lu_env *env, struct mdd_object *obj, 
155                       struct md_attr *ma, struct thandle *th);
156 int mdd_object_initialize(const struct lu_env *env, const struct lu_fid *pfid,
157                           struct mdd_object *child, struct md_attr *ma, 
158                           struct thandle *handle);
159 int mdd_link_sanity_check(const struct lu_env *env, struct mdd_object *tgt_obj,
160                           struct mdd_object *src_obj);
161 void mdd_ref_add_internal(const struct lu_env *env, struct mdd_object *obj,
162                           struct thandle *handle);
163 void mdd_ref_del_internal(const struct lu_env *env, struct mdd_object *obj,
164                           struct thandle *handle);
165 /* mdd_lov.c */
166 int mdd_unlink_log(const struct lu_env *env, struct mdd_device *mdd,
167                    struct mdd_object *mdd_cobj, struct md_attr *ma);
168
169 int mdd_get_cookie_size(const struct lu_env *env, struct mdd_device *mdd,
170                         struct lov_mds_md *lmm);
171
172 int mdd_lov_setattr_async(const struct lu_env *env, struct mdd_object *obj,
173                           struct lov_mds_md *lmm, int lmm_size);
174
175 struct mdd_thread_info *mdd_env_info(const struct lu_env *env);
176
177 struct lu_buf *mdd_buf_get(const struct lu_env *env, void *area, ssize_t len);
178 const struct lu_buf *mdd_buf_get_const(const struct lu_env *env,
179                                        const void *area, ssize_t len);
180
181 void mdd_read_lock(const struct lu_env *env, struct mdd_object *obj);
182 void mdd_read_unlock(const struct lu_env *env, struct mdd_object *obj);
183 void mdd_write_lock(const struct lu_env *env, struct mdd_object *obj);
184 void mdd_write_unlock(const struct lu_env *env, struct mdd_object *obj);
185
186 int __mdd_orphan_cleanup(const struct lu_env *env, struct mdd_device *d);
187 int __mdd_orphan_add(const struct lu_env *, struct mdd_object *,
188                      struct thandle *);
189 int __mdd_orphan_del(const struct lu_env *, struct mdd_object *,
190                      struct thandle *);
191 int orph_index_init(const struct lu_env *env, struct mdd_device *mdd);
192 void orph_index_fini(const struct lu_env *env, struct mdd_device *mdd);
193 int mdd_txn_init_credits(const struct lu_env *env, struct mdd_device *mdd);
194
195 int mdd_procfs_init(struct mdd_device *mdd);
196 int mdd_procfs_fini(struct mdd_device *mdd);
197 void mdd_lproc_time_start(struct mdd_device *mdd, struct timeval *start, 
198                           int op);
199 void mdd_lproc_time_end(struct mdd_device *mdd, struct timeval *start, int op);
200
201 int mdd_get_flags(const struct lu_env *env, struct mdd_object *obj);
202
203 extern struct md_dir_operations    mdd_dir_ops;
204 extern struct md_object_operations mdd_obj_ops;
205
206 /* mdd_trans.c */
207 void mdd_txn_param_build(const struct lu_env *env, struct mdd_device *mdd,
208                          enum mdd_txn_op);
209 int mdd_log_txn_param_build(const struct lu_env *env, struct mdd_object *obj,
210                             struct md_attr *ma, enum mdd_txn_op);
211
212 static inline void mdd_object_put(const struct lu_env *env,
213                                   struct mdd_object *o)
214 {
215         lu_object_put(env, &o->mod_obj.mo_lu);
216 }
217
218 struct thandle* mdd_trans_start(const struct lu_env *env,
219                                        struct mdd_device *);
220
221 void mdd_trans_stop(const struct lu_env *env, struct mdd_device *mdd,
222                     int rc, struct thandle *handle);
223
224 int mdd_txn_start_cb(const struct lu_env *env, struct txn_param *param,
225                      void *cookie);
226
227 int mdd_txn_stop_cb(const struct lu_env *env, struct thandle *txn,
228                     void *cookie);
229
230 int mdd_txn_commit_cb(const struct lu_env *env, struct thandle *txn,
231                       void *cookie);
232 /* mdd_device.c */
233 struct lu_object *mdd_object_alloc(const struct lu_env *env,
234                                    const struct lu_object_header *hdr,
235                                    struct lu_device *d);
236
237 /* mdd_object.c */
238 extern struct lu_context_key mdd_thread_key;
239 extern struct lu_device_operations mdd_lu_ops;
240
241 struct mdd_object *mdd_object_find(const struct lu_env *env,
242                                    struct mdd_device *d,
243                                    const struct lu_fid *f);
244 /* mdd_permission.c */
245 #define mdd_cap_t(x) (x)
246
247 #define MDD_CAP_TO_MASK(x) (1 << (x))
248
249 #define mdd_cap_raised(c, flag) (mdd_cap_t(c) & MDD_CAP_TO_MASK(flag))
250
251 /* capable() is copied from linux kernel! */
252 static inline int mdd_capable(struct md_ucred *uc, int cap)
253 {
254         if (mdd_cap_raised(uc->mu_cap, cap))
255                 return 1;
256         return 0;
257 }
258
259 int mdd_in_group_p(struct md_ucred *uc, gid_t grp);
260 int mdd_acl_def_get(const struct lu_env *env, struct mdd_object *mdd_obj, 
261                     struct md_attr *ma);
262 int mdd_acl_chmod(const struct lu_env *env, struct mdd_object *o, __u32 mode, 
263                   struct thandle *handle);
264 int __mdd_acl_init(const struct lu_env *env, struct mdd_object *obj,
265                    struct lu_buf *buf, __u32 *mode, struct thandle *handle);
266 int mdd_acl_init(const struct lu_env *env, struct mdd_object *pobj,
267                  struct mdd_object *cobj, __u32 *mode, struct thandle *handle);
268 int __mdd_permission_internal(const struct lu_env *env, struct mdd_object *obj,
269                               int mask, int getattr);
270 int mdd_permission_internal(const struct lu_env *env, struct mdd_object *obj, 
271                             int mask);
272 int mdd_permission(const struct lu_env *env, struct md_object *obj, int mask);
273 int mdd_capa_get(const struct lu_env *env, struct md_object *obj,
274                  struct lustre_capa *capa, int renewal);
275
276 static inline int lu_device_is_mdd(struct lu_device *d)
277 {
278         return ergo(d != NULL && d->ld_ops != NULL, d->ld_ops == &mdd_lu_ops);
279 }
280
281 static inline struct mdd_device* lu2mdd_dev(struct lu_device *d)
282 {
283         LASSERT(lu_device_is_mdd(d));
284         return container_of0(d, struct mdd_device, mdd_md_dev.md_lu_dev);
285 }
286
287 static inline struct lu_device *mdd2lu_dev(struct mdd_device *d)
288 {
289         return (&d->mdd_md_dev.md_lu_dev);
290 }
291
292 static inline struct mdd_object *lu2mdd_obj(struct lu_object *o)
293 {
294         LASSERT(ergo(o != NULL, lu_device_is_mdd(o->lo_dev)));
295         return container_of0(o, struct mdd_object, mod_obj.mo_lu);
296 }
297
298 static inline struct mdd_device* mdo2mdd(struct md_object *mdo)
299 {
300         return lu2mdd_dev(mdo->mo_lu.lo_dev);
301 }
302
303 static inline struct mdd_object* md2mdd_obj(struct md_object *mdo)
304 {
305         return container_of0(mdo, struct mdd_object, mod_obj);
306 }
307
308 static inline struct dt_device_operations *mdd_child_ops(struct mdd_device *d)
309 {
310         return d->mdd_child->dd_ops;
311 }
312
313 static inline struct lu_object *mdd2lu_obj(struct mdd_object *obj)
314 {
315         return &obj->mod_obj.mo_lu;
316 }
317
318 static inline struct dt_object* mdd_object_child(struct mdd_object *o)
319 {
320         return container_of0(lu_object_next(mdd2lu_obj(o)),
321                              struct dt_object, do_lu);
322 }
323 static inline struct obd_device *mdd2obd_dev(struct mdd_device *mdd)
324 {
325         return mdd->mdd_obd_dev;
326 }
327
328 static inline const struct lu_fid *mdo2fid(const struct mdd_object *obj)
329 {
330         return lu_object_fid(&obj->mod_obj.mo_lu);
331 }
332
333 static inline umode_t mdd_object_type(const struct mdd_object *obj)
334 {
335         return lu_object_attr(&obj->mod_obj.mo_lu);
336 }
337
338 static inline int mdd_lov_mdsize(const struct lu_env *env,
339                                  struct mdd_device *mdd)
340 {
341         struct obd_device *obd = mdd2obd_dev(mdd);
342         return obd->u.mds.mds_max_mdsize;
343 }
344
345 static inline int mdd_lov_cookiesize(const struct lu_env *env,
346                                      struct mdd_device *mdd)
347 {
348         struct obd_device *obd = mdd2obd_dev(mdd);
349         return obd->u.mds.mds_max_cookiesize;
350 }
351
352 static inline int mdd_is_immutable(struct mdd_object *obj)
353 {
354         return obj->mod_flags & IMMUTE_OBJ;
355 }
356
357 static inline int mdd_is_dead_obj(struct mdd_object *obj)
358 {
359         return obj && obj->mod_flags & DEAD_OBJ;
360 }
361
362 static inline int mdd_is_append(struct mdd_object *obj)
363 {
364         return obj->mod_flags & APPEND_OBJ;
365 }
366
367 static inline struct lustre_capa *mdd_object_capa(const struct lu_env *env,
368                                                   const struct mdd_object *obj)
369 {
370         struct md_capainfo *ci = md_capainfo(env);
371         const struct lu_fid *fid = mdo2fid(obj);
372         int i;
373
374         /* NB: in mdt_init0 */
375         if (!ci)
376                 return BYPASS_CAPA;
377         for (i = 0; i < 4; i++)
378                 if (ci->mc_fid[i] && lu_fid_eq(ci->mc_fid[i], fid))
379                         return ci->mc_capa[i];
380         return NULL;
381 }
382
383 enum {
384         LPROC_MDD_OPEN = 0,
385         LPROC_MDD_CREATE,
386         LPROC_MDD_LAST
387 };
388 #endif