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