Whamcloud - gitweb
Branch: b_new_cmd
[fs/lustre-release.git] / lustre / include / md_object.h
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Extention of lu_object.h for metadata objects
5  *
6  *  Copyright (C) 2006 Cluster File Systems, Inc.
7  *
8  *   This file is part of Lustre, http://www.lustre.org.
9  *
10  *   Lustre is free software; you can redistribute it and/or
11  *   modify it under the terms of version 2 of the GNU General Public
12  *   License as published by the Free Software Foundation.
13  *
14  *   Lustre is distributed in the hope that it will be useful,
15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *   GNU General Public License for more details.
18  *
19  *   You should have received a copy of the GNU General Public License
20  *   along with Lustre; if not, write to the Free Software
21  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  *
23  */
24
25 #ifndef _LUSTRE_MD_OBJECT_H
26 #define _LUSTRE_MD_OBJECT_H
27
28 /*
29  * Sub-class of lu_object with methods common for "meta-data" objects in MDT
30  * stack.
31  *
32  * Meta-data objects implement namespace operations: you can link, unlink
33  * them, and treat them as directories.
34  *
35  * Examples: mdt, cmm, and mdt are implementations of md interface.
36  */
37
38
39 /*
40  * super-class definitions.
41  */
42 #include <lu_object.h>
43
44 struct md_device;
45 struct md_device_operations;
46 struct md_object;
47
48
49 /* metadata attributes */
50 enum ma_valid {
51         MA_INODE = (1 << 0),
52         MA_LOV   = (1 << 1),
53         MA_COOKIE = (1 << 2),
54         MA_FLAGS = (1 << 3),
55         MA_LMV   = (1 << 4)
56 };
57
58 struct md_attr {
59         __u64                   ma_valid;
60         __u64                   ma_need;
61         __u64                   ma_attr_flags;
62         struct lu_attr          ma_attr;
63         struct lov_mds_md      *ma_lmm;
64         int                     ma_lmm_size;
65         struct lmv_stripe_md   *ma_lmv;
66         int                     ma_lmv_size;
67         struct llog_cookie     *ma_cookie;
68         int                     ma_cookie_size;
69 };
70
71 /* additional parameters for create */
72 struct md_create_spec {
73         union {
74                 /* symlink target */
75                 const char               *sp_symname;
76                 /* parent FID for cross-ref mkdir */
77                 const struct lu_fid      *sp_pfid;
78                 /* eadata for regular files */
79                 struct md_spec_reg {
80                         /* lov objs exist already */
81                         const struct lu_fid   *fid;
82                         int no_lov_create;
83                         const void *eadata;
84                         int  eadatalen;
85                 } sp_ea;
86         } u;
87         /* create flag from client: such as MDS_OPEN_CREAT, and others */
88         __u32 sp_cr_flags;
89 };
90
91 /*
92  * Operations implemented for each md object (both directory and leaf).
93  */
94 struct md_object_operations {
95         int (*moo_attr_get)(const struct lu_context *ctxt, struct md_object *dt,
96                             struct md_attr *attr);
97
98         int (*moo_attr_set)(const struct lu_context *ctxt, struct md_object *dt,
99                             const struct md_attr *attr);
100
101         int (*moo_xattr_get)(const struct lu_context *ctxt,
102                              struct md_object *obj,
103                              void *buf, int buf_len, const char *name);
104
105         int (*moo_xattr_list)(const struct lu_context *ctxt,
106                               struct md_object *obj,
107                               void *buf, int buf_len);
108
109         int (*moo_xattr_set)(const struct lu_context *ctxt,
110                              struct md_object *obj, const void *buf,
111                              int buf_len, const char *name, int fl);
112         int (*moo_xattr_del)(const struct lu_context *ctxt,
113                              struct md_object *obj, const char *name);
114
115         int (*moo_readpage)(const struct lu_context *, struct md_object *,
116                             const struct lu_rdpg *);
117
118         int (*moo_readlink)(const struct lu_context *ctxt,
119                             struct md_object *obj,
120                             void *buf, int buf_len);
121
122         /* part of cross-ref operation */
123         int (*moo_object_create)(const struct lu_context *,
124                                  struct md_object *,
125                                  const struct md_create_spec *spec,
126                                  struct md_attr *);
127         int (*moo_ref_add)(const struct lu_context *, struct md_object *);
128         int (*moo_ref_del)(const struct lu_context *, struct md_object *,
129                            struct md_attr *);
130         int (*moo_open)(const struct lu_context *, struct md_object *, int flags);
131         int (*moo_close)(const struct lu_context *, struct md_object *,
132                          struct md_attr *);
133 };
134
135 /*
136  * Operations implemented for each directory object.
137  */
138 struct md_dir_operations {
139         int (*mdo_lookup)(const struct lu_context *, struct md_object *,
140                           const char *, struct lu_fid *);
141
142         int (*mdo_create)(const struct lu_context *, struct md_object *,
143                           const char *child_name, struct md_object *,
144                           const struct md_create_spec *spec,
145                           struct md_attr *);
146         /* This method is used for creating data object for this meta object*/
147         int (*mdo_create_data)(const struct lu_context *cx, struct md_object *p,
148                                struct md_object *o,
149                                const struct md_create_spec *spec,
150                                struct md_attr *ma);
151         int (*mdo_rename)(const struct lu_context *ctxt,
152                           struct md_object *spobj, struct md_object *tpobj,
153                           const struct lu_fid *lf, const char *sname,
154                           struct md_object *tobj, const char *tname,
155                           struct md_attr *);
156
157         int (*mdo_link)(const struct lu_context *, struct md_object *,
158                         struct md_object *, const char *, struct md_attr *);
159
160         int (*mdo_unlink)(const struct lu_context *, struct md_object *,
161                           struct md_object *, const char *, struct md_attr *);
162
163         /* partial ops for cross-ref case */
164         int (*mdo_name_insert)(const struct lu_context *, struct md_object *,
165                                const char *, const struct lu_fid *, int);
166         int (*mdo_name_remove)(const struct lu_context *, struct md_object *,
167                                const char *);
168         int (*mdo_rename_tgt)(const struct lu_context *, struct md_object *,
169                               struct md_object *, const struct lu_fid *,
170                               const char *, struct md_attr *);
171 };
172
173 struct md_device_operations {
174         /* meta-data device related handlers. */
175         int (*mdo_root_get)(const struct lu_context *ctx,
176                             struct md_device *m, struct lu_fid *f);
177         int (*mdo_maxsize_get)(const struct lu_context *ctx,
178                                struct md_device *m, int *md_size,
179                                int *cookie_size);
180         int (*mdo_statfs)(const struct lu_context *ctx,
181                           struct md_device *m, struct kstatfs *sfs);
182 };
183
184 enum md_upcall_event {
185         /*sync the md layer*/
186         MD_LOV_SYNC
187 };
188
189 struct md_upcall {
190         struct md_device            *mu_upcall_dev;
191         int (*mu_upcall)(const struct lu_context *ctxt, struct md_device *md,
192                          enum md_upcall_event ev);
193 };
194
195 struct md_device {
196         struct lu_device             md_lu_dev;
197         struct md_device_operations *md_ops;
198         struct md_upcall             md_upcall;
199 };
200
201 struct md_object {
202         struct lu_object             mo_lu;
203         struct md_object_operations *mo_ops;
204         struct md_dir_operations    *mo_dir_ops;
205 };
206
207 static inline int lu_device_is_md(const struct lu_device *d)
208 {
209         return ergo(d != NULL, d->ld_type->ldt_tags & LU_DEVICE_MD);
210 }
211
212 static inline struct md_device *lu2md_dev(const struct lu_device *d)
213 {
214         LASSERT(lu_device_is_md(d));
215         return container_of0(d, struct md_device, md_lu_dev);
216 }
217
218 static inline struct lu_device *md2lu_dev(struct md_device *d)
219 {
220         return &d->md_lu_dev;
221 }
222
223 static inline struct md_object *lu2md(const struct lu_object *o)
224 {
225         LASSERT(lu_device_is_md(o->lo_dev));
226         return container_of0(o, struct md_object, mo_lu);
227 }
228
229 static inline struct md_object *md_object_next(const struct md_object *obj)
230 {
231         return (obj ? lu2md(lu_object_next(&obj->mo_lu)) : NULL);
232 }
233
234 static inline struct md_device *md_obj2dev(const struct md_object *o)
235 {
236         LASSERT(lu_device_is_md(o->mo_lu.lo_dev));
237         return container_of0(o->mo_lu.lo_dev, struct md_device, md_lu_dev);
238 }
239
240 static inline int md_device_init(struct md_device *md, struct lu_device_type *t)
241 {
242         return lu_device_init(&md->md_lu_dev, t);
243 }
244
245 static inline void md_device_fini(struct md_device *md)
246 {
247         lu_device_fini(&md->md_lu_dev);
248 }
249
250 /* md operations */
251 static inline int mo_attr_get(const struct lu_context *cx, struct md_object *m,
252                               struct md_attr *at)
253 {
254         LASSERT(m->mo_ops->moo_attr_get);
255         return m->mo_ops->moo_attr_get(cx, m, at);
256 }
257
258 static inline int mo_readlink(const struct lu_context *cx, struct md_object *m,
259                               void *buf, int buf_len)
260 {
261         LASSERT(m->mo_ops->moo_attr_get);
262         return m->mo_ops->moo_readlink(cx, m, buf, buf_len);
263 }
264
265 static inline int mo_attr_set(const struct lu_context *cx, struct md_object *m,
266                               const struct md_attr *at)
267 {
268         LASSERT(m->mo_ops->moo_attr_set);
269         return m->mo_ops->moo_attr_set(cx, m, at);
270 }
271
272 static inline int mo_xattr_get(const struct lu_context *cx,
273                                struct md_object *m,
274                                void *buf, int buf_len, const char *name)
275 {
276         LASSERT(m->mo_ops->moo_xattr_get);
277         return m->mo_ops->moo_xattr_get(cx, m, buf, buf_len, name);
278 }
279
280 static inline int mo_xattr_del(const struct lu_context *cx,
281                                struct md_object *m,
282                                const char *name)
283 {
284         LASSERT(m->mo_ops->moo_xattr_set);
285         return m->mo_ops->moo_xattr_del(cx, m, name);
286 }
287
288 static inline int mo_xattr_set(const struct lu_context *cx,
289                                struct md_object *m, const void *buf,
290                                int buf_len, const char *name, int flags)
291 {
292         LASSERT(m->mo_ops->moo_xattr_set);
293         return m->mo_ops->moo_xattr_set(cx, m, buf, buf_len, name, flags);
294 }
295
296 static inline int mo_xattr_list(const struct lu_context *cx,
297                                struct md_object *m,
298                                void *buf, int buf_len)
299 {
300         LASSERT(m->mo_ops->moo_xattr_get);
301         return m->mo_ops->moo_xattr_list(cx, m, buf, buf_len);
302 }
303
304 static inline int mo_open(const struct lu_context *cx, struct md_object *m,
305                           int flags)
306 {
307         LASSERT(m->mo_ops->moo_open);
308         return m->mo_ops->moo_open(cx, m, flags);
309 }
310
311 static inline int mo_close(const struct lu_context *cx, struct md_object *m,
312                            struct md_attr *ma)
313 {
314         LASSERT(m->mo_ops->moo_close);
315         return m->mo_ops->moo_close(cx, m, ma);
316 }
317
318 static inline int mo_readpage(const struct lu_context *cx, struct md_object *m,
319                               const struct lu_rdpg *rdpg)
320 {
321         LASSERT(m->mo_ops->moo_readpage);
322         return m->mo_ops->moo_readpage(cx, m, rdpg);
323 }
324
325 static inline int mo_object_create(const struct lu_context *cx,
326                                    struct md_object *m,
327                                    const struct md_create_spec *spc,
328                                    struct md_attr *at)
329 {
330         LASSERT(m->mo_ops->moo_object_create);
331         return m->mo_ops->moo_object_create(cx, m, spc, at);
332 }
333
334 static inline int mo_ref_add(const struct lu_context *cx,
335                              struct md_object *m)
336 {
337         LASSERT(m->mo_ops->moo_ref_add);
338         return m->mo_ops->moo_ref_add(cx, m);
339 }
340
341 static inline int mo_ref_del(const struct lu_context *cx,
342                              struct md_object *m, struct md_attr *ma)
343 {
344         LASSERT(m->mo_ops->moo_ref_del);
345         return m->mo_ops->moo_ref_del(cx, m, ma);
346 }
347
348 static inline int mdo_lookup(const struct lu_context *cx, struct md_object *p,
349                              const char *name, struct lu_fid *f)
350 {
351         LASSERT(p->mo_dir_ops->mdo_lookup);
352         return p->mo_dir_ops->mdo_lookup(cx, p, name, f);
353 }
354
355 static inline int mdo_create(const struct lu_context *cx, struct md_object *p,
356                              const char *child_name, struct md_object *c,
357                              const struct md_create_spec *spc,
358                              struct md_attr *at)
359 {
360         LASSERT(c->mo_dir_ops->mdo_create);
361         return c->mo_dir_ops->mdo_create(cx, p, child_name, c, spc, at);
362 }
363 static inline int mdo_create_data(const struct lu_context *cx,
364                                   struct md_object *p, struct md_object *c,
365                                   const struct md_create_spec *spec,
366                                   struct md_attr *ma)
367 {
368         LASSERT(c->mo_dir_ops->mdo_create_data);
369         return c->mo_dir_ops->mdo_create_data(cx, p, c, spec, ma);
370 }
371
372 static inline int mdo_rename(const struct lu_context *cx,
373                              struct md_object *sp, struct md_object *tp,
374                              const struct lu_fid *lf, const char *sname,
375                              struct md_object *t, const char *tname,
376                              struct md_attr *ma)
377 {
378         LASSERT(tp->mo_dir_ops->mdo_rename);
379         return tp->mo_dir_ops->mdo_rename(cx, sp, tp, lf, sname, t, tname, ma);
380 }
381
382 static inline int mdo_link(const struct lu_context *cx, struct md_object *p,
383                            struct md_object *s, const char *name,
384                            struct md_attr *ma)
385 {
386         LASSERT(s->mo_dir_ops->mdo_link);
387         return s->mo_dir_ops->mdo_link(cx, p, s, name, ma);
388 }
389
390 static inline int mdo_unlink(const struct lu_context *cx, struct md_object *p,
391                              struct md_object *c, const char *name,
392                              struct md_attr *ma)
393 {
394         LASSERT(c->mo_dir_ops->mdo_unlink);
395         return c->mo_dir_ops->mdo_unlink(cx, p, c, name, ma);
396 }
397
398 static inline int mdo_name_insert(const struct lu_context *cx,
399                                   struct md_object *p, const char *name,
400                                   const struct lu_fid *f, int isdir)
401 {
402         LASSERT(p->mo_dir_ops->mdo_name_insert);
403         return p->mo_dir_ops->mdo_name_insert(cx, p, name, f, isdir);
404 }
405
406 static inline int mdo_name_remove(const struct lu_context *cx,
407                                   struct md_object *p,
408                                   const char *name)
409 {
410         LASSERT(p->mo_dir_ops->mdo_name_remove);
411         return p->mo_dir_ops->mdo_name_remove(cx, p, name);
412 }
413
414 static inline int mdo_rename_tgt(const struct lu_context *cx,
415                                  struct md_object *p, struct md_object *t,
416                                  const struct lu_fid *lf, const char *name,
417                                  struct md_attr *ma)
418 {
419         if (t) {
420                 LASSERT(t->mo_dir_ops->mdo_rename_tgt);
421                 return t->mo_dir_ops->mdo_rename_tgt(cx, p, t, lf, name, ma);
422         } else {
423                 LASSERT(p->mo_dir_ops->mdo_rename_tgt);
424                 return p->mo_dir_ops->mdo_rename_tgt(cx, p, t, lf, name, ma);
425         }
426 }
427
428 #endif /* _LINUX_MD_OBJECT_H */