Whamcloud - gitweb
b=11694
[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 #include <lvfs.h>
44
45 struct md_device;
46 struct md_device_operations;
47 struct md_object;
48
49
50 typedef enum {
51         UCRED_INVALID   = -1,
52         UCRED_INIT      = 0,
53         UCRED_OLD       = 1,
54         UCRED_NEW       = 2,
55 } ucred_t;
56
57 #define SQUASH_NONE     0x00
58 #define SQUASH_UID      0x01
59 #define SQUASH_GID      0x02
60
61 struct md_ucred {
62         ucred_t                 mu_valid;
63         __u32                   mu_squash;
64         __u32                   mu_o_uid;
65         __u32                   mu_o_gid;
66         __u32                   mu_o_fsuid;
67         __u32                   mu_o_fsgid;
68         __u32                   mu_uid;
69         __u32                   mu_gid;
70         __u32                   mu_fsuid;
71         __u32                   mu_fsgid;
72         __u32                   mu_suppgids[2];
73         __u32                   mu_cap;
74         __u32                   mu_umask;
75         struct group_info      *mu_ginfo;
76         struct mdt_identity    *mu_identity;
77 };
78
79 #define MD_CAPAINFO_MAX 5
80
81 /* there are at most 5 fids in one operation, see rename, NOTE the last one
82  * is a temporary one used for is_subdir() */
83 struct md_capainfo {
84         const struct lu_fid    *mc_fid[MD_CAPAINFO_MAX];
85         struct lustre_capa     *mc_capa[MD_CAPAINFO_MAX];
86 };
87
88 /*
89  * Implemented in mdd/mdd_handler.c.
90  *
91  * XXX should be moved into separate .h/.c together with all md security
92  * related definitions.
93  */
94 struct md_ucred *md_ucred(const struct lu_env *env);
95 struct md_capainfo *md_capainfo(const struct lu_env *env);
96
97 /* metadata attributes */
98 enum ma_valid {
99         MA_INODE     = (1 << 0),
100         MA_LOV       = (1 << 1),
101         MA_COOKIE    = (1 << 2),
102         MA_FLAGS     = (1 << 3),
103         MA_LMV       = (1 << 4),
104         MA_ACL_DEF   = (1 << 5),
105         MA_LOV_DEF   = (1 << 6)
106 };
107
108 typedef enum {
109         MDL_MINMODE  = 0,
110         MDL_EX       = 1,
111         MDL_PW       = 2,
112         MDL_PR       = 4,
113         MDL_CW       = 8,
114         MDL_CR       = 16,
115         MDL_NL       = 32,
116         MDL_GROUP    = 64,
117         MDL_MAXMODE
118 } mdl_mode_t;
119
120 typedef enum {
121         MDT_NUL_LOCK = 0,
122         MDT_REG_LOCK = (1 << 0),
123         MDT_PDO_LOCK = (1 << 1)
124 } mdl_type_t;
125
126 struct md_attr {
127         __u64                   ma_valid;
128         __u64                   ma_need;
129         __u64                   ma_attr_flags;
130         struct lu_attr          ma_attr;
131         struct lov_mds_md      *ma_lmm;
132         int                     ma_lmm_size;
133         struct lmv_stripe_md   *ma_lmv;
134         int                     ma_lmv_size;
135         void                   *ma_acl;
136         int                     ma_acl_size;
137         struct llog_cookie     *ma_cookie;
138         int                     ma_cookie_size;
139         struct lustre_capa     *ma_capa;
140 };
141
142 /* Additional parameters for create */
143 struct md_op_spec {
144         union {
145                 /* symlink target */
146                 const char               *sp_symname;
147                 /* parent FID for cross-ref mkdir */
148                 const struct lu_fid      *sp_pfid;
149                 /* eadata for regular files */
150                 struct md_spec_reg {
151                         /* lov objs exist already */
152                         const struct lu_fid   *fid;
153                         int no_lov_create;
154                         const void *eadata;
155                         int  eadatalen;
156                 } sp_ea;
157         } u;
158         
159         /* Create flag from client: such as MDS_OPEN_CREAT, and others. */
160         __u32      sp_cr_flags;
161
162         /* Should mdd do lookup sanity check or not. */
163         int        sp_cr_lookup;
164
165         /* Current lock mode for parent dir where create is performing. */
166         mdl_mode_t sp_cr_mode;
167
168         /* Check for split */
169         int        sp_ck_split;
170 };
171
172 /*
173  * Operations implemented for each md object (both directory and leaf).
174  */
175 struct md_object_operations {
176         int (*moo_permission)(const struct lu_env *env,
177                               struct md_object *pobj, struct md_object *cobj,
178                               struct md_attr *attr, int mask);
179
180         int (*moo_attr_get)(const struct lu_env *env, struct md_object *obj,
181                             struct md_attr *attr);
182
183         int (*moo_attr_set)(const struct lu_env *env, struct md_object *obj,
184                             const struct md_attr *attr);
185
186         int (*moo_xattr_get)(const struct lu_env *env, struct md_object *obj,
187                              struct lu_buf *buf, const char *name);
188
189         int (*moo_xattr_list)(const struct lu_env *env, struct md_object *obj,
190                               struct lu_buf *buf);
191
192         int (*moo_xattr_set)(const struct lu_env *env, struct md_object *obj,
193                              const struct lu_buf *buf, const char *name,
194                              int fl);
195
196         int (*moo_xattr_del)(const struct lu_env *env, struct md_object *obj,
197                              const char *name);
198
199         int (*moo_readpage)(const struct lu_env *env, struct md_object *obj,
200                             const struct lu_rdpg *rdpg);
201
202         int (*moo_readlink)(const struct lu_env *env, struct md_object *obj,
203                             struct lu_buf *buf);
204
205         /* part of cross-ref operation */
206         int (*moo_object_create)(const struct lu_env *env,
207                                  struct md_object *obj,
208                                  const struct md_op_spec *spec,
209                                  struct md_attr *ma);
210
211         int (*moo_ref_add)(const struct lu_env *env,
212                            struct md_object *obj,
213                            const struct md_attr *ma);
214
215         int (*moo_ref_del)(const struct lu_env *env,
216                            struct md_object *obj,
217                            struct md_attr *ma);
218
219         int (*moo_open)(const struct lu_env *env,
220                         struct md_object *obj, int flag);
221
222         int (*moo_close)(const struct lu_env *env, struct md_object *obj,
223                          struct md_attr *ma);
224         
225         int (*moo_capa_get)(const struct lu_env *, struct md_object *,
226                             struct lustre_capa *, int renewal);
227 };
228
229 /*
230  * Operations implemented for each directory object.
231  */
232 struct md_dir_operations {
233         int (*mdo_is_subdir) (const struct lu_env *env, struct md_object *obj,
234                               const struct lu_fid *fid, struct lu_fid *sfid);
235
236         int (*mdo_lookup)(const struct lu_env *env, struct md_object *obj,
237                           const struct lu_name *lname, struct lu_fid *fid,
238                           struct md_op_spec *spec);
239
240         mdl_mode_t (*mdo_lock_mode)(const struct lu_env *env,
241                                     struct md_object *obj,
242                                     mdl_mode_t mode);
243
244         int (*mdo_create)(const struct lu_env *env, struct md_object *pobj,
245                           const struct lu_name *lname, struct md_object *child,
246                           struct md_op_spec *spec,
247                           struct md_attr *ma);
248
249         /* This method is used for creating data object for this meta object*/
250         int (*mdo_create_data)(const struct lu_env *env, struct md_object *p,
251                                struct md_object *o,
252                                const struct md_op_spec *spec,
253                                struct md_attr *ma);
254
255         int (*mdo_rename)(const struct lu_env *env, struct md_object *spobj,
256                           struct md_object *tpobj, const struct lu_fid *lf,
257                           const struct lu_name *lsname, struct md_object *tobj,
258                           const struct lu_name *ltname, struct md_attr *ma);
259
260         int (*mdo_link)(const struct lu_env *env, struct md_object *tgt_obj,
261                         struct md_object *src_obj, const struct lu_name *lname,
262                         struct md_attr *ma);
263
264         int (*mdo_unlink)(const struct lu_env *env, struct md_object *pobj,
265                           struct md_object *cobj, const struct lu_name *lname,
266                           struct md_attr *ma);
267
268         /* partial ops for cross-ref case */
269         int (*mdo_name_insert)(const struct lu_env *env,
270                                struct md_object *obj,
271                                const struct lu_name *lname,
272                                const struct lu_fid *fid,
273                                const struct md_attr *ma);
274
275         int (*mdo_name_remove)(const struct lu_env *env,
276                                struct md_object *obj,
277                                const struct lu_name *lname,
278                                const struct md_attr *ma);
279
280         int (*mdo_rename_tgt)(const struct lu_env *env, struct md_object *pobj,
281                               struct md_object *tobj, const struct lu_fid *fid,
282                               const struct lu_name *lname, struct md_attr *ma);
283 };
284
285 struct md_device_operations {
286         /* meta-data device related handlers. */
287         int (*mdo_root_get)(const struct lu_env *env, struct md_device *m,
288                             struct lu_fid *f);
289
290         int (*mdo_maxsize_get)(const struct lu_env *env, struct md_device *m,
291                                int *md_size, int *cookie_size);
292
293         int (*mdo_statfs)(const struct lu_env *env, struct md_device *m,
294                           struct kstatfs *sfs);
295
296         int (*mdo_init_capa_ctxt)(const struct lu_env *env, struct md_device *m,
297                                   int mode, unsigned long timeout, __u32 alg,
298                                   struct lustre_capa_key *keys);
299
300         int (*mdo_update_capa_key)(const struct lu_env *env,
301                                    struct md_device *m,
302                                    struct lustre_capa_key *key);
303 };
304
305 enum md_upcall_event {
306         /*sync the md layer*/
307         MD_LOV_SYNC = (1 << 0),
308         MD_NO_TRANS = (1 << 1), /* Just for split, no need trans, for replay */
309         MD_LOV_CONFIG = (1 << 2)
310 };
311
312 struct md_upcall {
313         /* this lock protects upcall using against its removal
314          * read lock is for usage the upcall, write - for init/fini */
315         struct rw_semaphore     mu_upcall_sem;
316         /* device to call, upper layer normally */
317         struct md_device       *mu_upcall_dev;
318         /* upcall function */
319         int (*mu_upcall)(const struct lu_env *env, struct md_device *md,
320                          enum md_upcall_event ev);
321 };
322
323 struct md_device {
324         struct lu_device             md_lu_dev;
325         struct md_device_operations *md_ops;
326         struct md_upcall             md_upcall;
327 };
328
329 static inline void md_upcall_init(struct md_device *m, void *upcl)
330 {
331         init_rwsem(&m->md_upcall.mu_upcall_sem);
332         m->md_upcall.mu_upcall_dev = NULL;
333         m->md_upcall.mu_upcall = upcl;
334 }
335
336 static inline void md_upcall_dev_set(struct md_device *m, struct md_device *up)
337 {
338         down_write(&m->md_upcall.mu_upcall_sem);
339         m->md_upcall.mu_upcall_dev = up;
340         up_write(&m->md_upcall.mu_upcall_sem);
341 }
342
343 static inline void md_upcall_fini(struct md_device *m)
344 {
345         down_write(&m->md_upcall.mu_upcall_sem);
346         m->md_upcall.mu_upcall_dev = NULL;
347         m->md_upcall.mu_upcall = NULL;
348         up_write(&m->md_upcall.mu_upcall_sem);
349 }
350
351 static inline int md_do_upcall(const struct lu_env *env, struct md_device *m,
352                                enum md_upcall_event ev)
353 {
354         int rc = 0;
355         down_read(&m->md_upcall.mu_upcall_sem);
356         if (m->md_upcall.mu_upcall_dev != NULL &&
357             m->md_upcall.mu_upcall_dev->md_upcall.mu_upcall != NULL) {
358                 rc = m->md_upcall.mu_upcall_dev->md_upcall.mu_upcall(env,
359                                               m->md_upcall.mu_upcall_dev, ev);
360         }
361         up_read(&m->md_upcall.mu_upcall_sem);
362         return rc;
363 }
364
365 struct md_object {
366         struct lu_object             mo_lu;
367         struct md_object_operations *mo_ops;
368         struct md_dir_operations    *mo_dir_ops;
369 };
370
371 static inline int lu_device_is_md(const struct lu_device *d)
372 {
373         return ergo(d != NULL, d->ld_type->ldt_tags & LU_DEVICE_MD);
374 }
375
376 static inline struct md_device *lu2md_dev(const struct lu_device *d)
377 {
378         LASSERT(lu_device_is_md(d));
379         return container_of0(d, struct md_device, md_lu_dev);
380 }
381
382 static inline struct lu_device *md2lu_dev(struct md_device *d)
383 {
384         return &d->md_lu_dev;
385 }
386
387 static inline struct md_object *lu2md(const struct lu_object *o)
388 {
389         LASSERT(lu_device_is_md(o->lo_dev));
390         return container_of0(o, struct md_object, mo_lu);
391 }
392
393 static inline struct md_object *md_object_next(const struct md_object *obj)
394 {
395         return (obj ? lu2md(lu_object_next(&obj->mo_lu)) : NULL);
396 }
397
398 static inline struct md_device *md_obj2dev(const struct md_object *o)
399 {
400         LASSERT(lu_device_is_md(o->mo_lu.lo_dev));
401         return container_of0(o->mo_lu.lo_dev, struct md_device, md_lu_dev);
402 }
403
404 static inline int md_device_init(struct md_device *md, struct lu_device_type *t)
405 {
406         return lu_device_init(&md->md_lu_dev, t);
407 }
408
409 static inline void md_device_fini(struct md_device *md)
410 {
411         lu_device_fini(&md->md_lu_dev);
412 }
413
414 /* md operations */
415 static inline int mo_permission(const struct lu_env *env,
416                                 struct md_object *p,
417                                 struct md_object *c,
418                                 struct md_attr *at,
419                                 int mask)
420 {
421         LASSERT(c->mo_ops->moo_permission);
422         return c->mo_ops->moo_permission(env, p, c, at, mask);
423 }
424
425 static inline int mo_attr_get(const struct lu_env *env,
426                               struct md_object *m,
427                               struct md_attr *at)
428 {
429         LASSERT(m->mo_ops->moo_attr_get);
430         return m->mo_ops->moo_attr_get(env, m, at);
431 }
432
433 static inline int mo_readlink(const struct lu_env *env,
434                               struct md_object *m,
435                               struct lu_buf *buf)
436 {
437         LASSERT(m->mo_ops->moo_readlink);
438         return m->mo_ops->moo_readlink(env, m, buf);
439 }
440
441 static inline int mo_attr_set(const struct lu_env *env,
442                               struct md_object *m,
443                               const struct md_attr *at)
444 {
445         LASSERT(m->mo_ops->moo_attr_set);
446         return m->mo_ops->moo_attr_set(env, m, at);
447 }
448
449 static inline int mo_xattr_get(const struct lu_env *env,
450                                struct md_object *m,
451                                struct lu_buf *buf,
452                                const char *name)
453 {
454         LASSERT(m->mo_ops->moo_xattr_get);
455         return m->mo_ops->moo_xattr_get(env, m, buf, name);
456 }
457
458 static inline int mo_xattr_del(const struct lu_env *env,
459                                struct md_object *m,
460                                const char *name)
461 {
462         LASSERT(m->mo_ops->moo_xattr_del);
463         return m->mo_ops->moo_xattr_del(env, m, name);
464 }
465
466 static inline int mo_xattr_set(const struct lu_env *env,
467                                struct md_object *m,
468                                const struct lu_buf *buf,
469                                const char *name,
470                                int flags)
471 {
472         LASSERT(m->mo_ops->moo_xattr_set);
473         return m->mo_ops->moo_xattr_set(env, m, buf, name, flags);
474 }
475
476 static inline int mo_xattr_list(const struct lu_env *env,
477                                 struct md_object *m,
478                                 struct lu_buf *buf)
479 {
480         LASSERT(m->mo_ops->moo_xattr_list);
481         return m->mo_ops->moo_xattr_list(env, m, buf);
482 }
483
484 static inline int mo_open(const struct lu_env *env,
485                           struct md_object *m,
486                           int flags)
487 {
488         LASSERT(m->mo_ops->moo_open);
489         return m->mo_ops->moo_open(env, m, flags);
490 }
491
492 static inline int mo_close(const struct lu_env *env,
493                            struct md_object *m,
494                            struct md_attr *ma)
495 {
496         LASSERT(m->mo_ops->moo_close);
497         return m->mo_ops->moo_close(env, m, ma);
498 }
499
500 static inline int mo_readpage(const struct lu_env *env,
501                               struct md_object *m,
502                               const struct lu_rdpg *rdpg)
503 {
504         LASSERT(m->mo_ops->moo_readpage);
505         return m->mo_ops->moo_readpage(env, m, rdpg);
506 }
507
508 static inline int mo_object_create(const struct lu_env *env,
509                                    struct md_object *m,
510                                    const struct md_op_spec *spc,
511                                    struct md_attr *at)
512 {
513         LASSERT(m->mo_ops->moo_object_create);
514         return m->mo_ops->moo_object_create(env, m, spc, at);
515 }
516
517 static inline int mo_ref_add(const struct lu_env *env,
518                              struct md_object *m,
519                              const struct md_attr *ma)
520 {
521         LASSERT(m->mo_ops->moo_ref_add);
522         return m->mo_ops->moo_ref_add(env, m, ma);
523 }
524
525 static inline int mo_ref_del(const struct lu_env *env,
526                              struct md_object *m,
527                              struct md_attr *ma)
528 {
529         LASSERT(m->mo_ops->moo_ref_del);
530         return m->mo_ops->moo_ref_del(env, m, ma);
531 }
532
533 static inline int mo_capa_get(const struct lu_env *env,
534                               struct md_object *m,
535                               struct lustre_capa *c,
536                               int renewal)
537 {
538         LASSERT(m->mo_ops->moo_capa_get);
539         return m->mo_ops->moo_capa_get(env, m, c, renewal);
540 }
541
542 static inline int mdo_lookup(const struct lu_env *env,
543                              struct md_object *p,
544                              const struct lu_name *lname,
545                              struct lu_fid *f,
546                              struct md_op_spec *spec)
547 {
548         LASSERT(p->mo_dir_ops->mdo_lookup);
549         return p->mo_dir_ops->mdo_lookup(env, p, lname, f, spec);
550 }
551
552 static inline mdl_mode_t mdo_lock_mode(const struct lu_env *env,
553                                        struct md_object *mo,
554                                        mdl_mode_t lm)
555 {
556         if (mo->mo_dir_ops->mdo_lock_mode == NULL)
557                 return MDL_MINMODE;
558         return mo->mo_dir_ops->mdo_lock_mode(env, mo, lm);
559 }
560
561 static inline int mdo_create(const struct lu_env *env,
562                              struct md_object *p,
563                              const struct lu_name *lchild_name,
564                              struct md_object *c,
565                              struct md_op_spec *spc,
566                              struct md_attr *at)
567 {
568         LASSERT(c->mo_dir_ops->mdo_create);
569         return c->mo_dir_ops->mdo_create(env, p, lchild_name, c, spc, at);
570 }
571
572 static inline int mdo_create_data(const struct lu_env *env,
573                                   struct md_object *p,
574                                   struct md_object *c,
575                                   const struct md_op_spec *spec,
576                                   struct md_attr *ma)
577 {
578         LASSERT(c->mo_dir_ops->mdo_create_data);
579         return c->mo_dir_ops->mdo_create_data(env, p, c, spec, ma);
580 }
581
582 static inline int mdo_rename(const struct lu_env *env,
583                              struct md_object *sp,
584                              struct md_object *tp,
585                              const struct lu_fid *lf,
586                              const struct lu_name *lsname,
587                              struct md_object *t,
588                              const struct lu_name *ltname,
589                              struct md_attr *ma)
590 {
591         LASSERT(tp->mo_dir_ops->mdo_rename);
592         return tp->mo_dir_ops->mdo_rename(env, sp, tp, lf, lsname, t, ltname,
593                                           ma);
594 }
595
596 static inline int mdo_is_subdir(const struct lu_env *env,
597                                 struct md_object *mo,
598                                 const struct lu_fid *fid,
599                                 struct lu_fid *sfid)
600 {
601         LASSERT(mo->mo_dir_ops->mdo_is_subdir);
602         return mo->mo_dir_ops->mdo_is_subdir(env, mo, fid, sfid);
603 }
604
605 static inline int mdo_link(const struct lu_env *env,
606                            struct md_object *p,
607                            struct md_object *s,
608                            const struct lu_name *lname,
609                            struct md_attr *ma)
610 {
611         LASSERT(s->mo_dir_ops->mdo_link);
612         return s->mo_dir_ops->mdo_link(env, p, s, lname, ma);
613 }
614
615 static inline int mdo_unlink(const struct lu_env *env,
616                              struct md_object *p,
617                              struct md_object *c,
618                              const struct lu_name *lname,
619                              struct md_attr *ma)
620 {
621         LASSERT(c->mo_dir_ops->mdo_unlink);
622         return c->mo_dir_ops->mdo_unlink(env, p, c, lname, ma);
623 }
624
625 static inline int mdo_name_insert(const struct lu_env *env,
626                                   struct md_object *p,
627                                   const struct lu_name *lname,
628                                   const struct lu_fid *f,
629                                   const struct md_attr *ma)
630 {
631         LASSERT(p->mo_dir_ops->mdo_name_insert);
632         return p->mo_dir_ops->mdo_name_insert(env, p, lname, f, ma);
633 }
634
635 static inline int mdo_name_remove(const struct lu_env *env,
636                                   struct md_object *p,
637                                   const struct lu_name *lname,
638                                   const struct md_attr *ma)
639 {
640         LASSERT(p->mo_dir_ops->mdo_name_remove);
641         return p->mo_dir_ops->mdo_name_remove(env, p, lname, ma);
642 }
643
644 static inline int mdo_rename_tgt(const struct lu_env *env,
645                                  struct md_object *p,
646                                  struct md_object *t,
647                                  const struct lu_fid *lf,
648                                  const struct lu_name *lname,
649                                  struct md_attr *ma)
650 {
651         if (t) {
652                 LASSERT(t->mo_dir_ops->mdo_rename_tgt);
653                 return t->mo_dir_ops->mdo_rename_tgt(env, p, t, lf, lname, ma);
654         } else {
655                 LASSERT(p->mo_dir_ops->mdo_rename_tgt);
656                 return p->mo_dir_ops->mdo_rename_tgt(env, p, t, lf, lname, ma);
657         }
658 }
659
660 #endif /* _LINUX_MD_OBJECT_H */