Whamcloud - gitweb
b=13872
[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         MD_LOV_CONFIG = (1 << 2)
309 };
310
311 struct md_upcall {
312         /* this lock protects upcall using against its removal
313          * read lock is for usage the upcall, write - for init/fini */
314         struct rw_semaphore     mu_upcall_sem;
315         /* device to call, upper layer normally */
316         struct md_device       *mu_upcall_dev;
317         /* upcall function */
318         int (*mu_upcall)(const struct lu_env *env, struct md_device *md,
319                          enum md_upcall_event ev);
320 };
321
322 struct md_device {
323         struct lu_device             md_lu_dev;
324         struct md_device_operations *md_ops;
325         struct md_upcall             md_upcall;
326 };
327
328 static inline void md_upcall_init(struct md_device *m, void *upcl)
329 {
330         init_rwsem(&m->md_upcall.mu_upcall_sem);
331         m->md_upcall.mu_upcall_dev = NULL;
332         m->md_upcall.mu_upcall = upcl;
333 }
334
335 static inline void md_upcall_dev_set(struct md_device *m, struct md_device *up)
336 {
337         down_write(&m->md_upcall.mu_upcall_sem);
338         m->md_upcall.mu_upcall_dev = up;
339         up_write(&m->md_upcall.mu_upcall_sem);
340 }
341
342 static inline void md_upcall_fini(struct md_device *m)
343 {
344         down_write(&m->md_upcall.mu_upcall_sem);
345         m->md_upcall.mu_upcall_dev = NULL;
346         m->md_upcall.mu_upcall = NULL;
347         up_write(&m->md_upcall.mu_upcall_sem);
348 }
349
350 static inline int md_do_upcall(const struct lu_env *env, struct md_device *m,
351                                enum md_upcall_event ev)
352 {
353         int rc = 0;
354         down_read(&m->md_upcall.mu_upcall_sem);
355         if (m->md_upcall.mu_upcall_dev != NULL &&
356             m->md_upcall.mu_upcall_dev->md_upcall.mu_upcall != NULL) {
357                 rc = m->md_upcall.mu_upcall_dev->md_upcall.mu_upcall(env,
358                                               m->md_upcall.mu_upcall_dev, ev);
359         }
360         up_read(&m->md_upcall.mu_upcall_sem);
361         return rc;
362 }
363
364 struct md_object {
365         struct lu_object             mo_lu;
366         struct md_object_operations *mo_ops;
367         struct md_dir_operations    *mo_dir_ops;
368 };
369
370 static inline int lu_device_is_md(const struct lu_device *d)
371 {
372         return ergo(d != NULL, d->ld_type->ldt_tags & LU_DEVICE_MD);
373 }
374
375 static inline struct md_device *lu2md_dev(const struct lu_device *d)
376 {
377         LASSERT(lu_device_is_md(d));
378         return container_of0(d, struct md_device, md_lu_dev);
379 }
380
381 static inline struct lu_device *md2lu_dev(struct md_device *d)
382 {
383         return &d->md_lu_dev;
384 }
385
386 static inline struct md_object *lu2md(const struct lu_object *o)
387 {
388         LASSERT(lu_device_is_md(o->lo_dev));
389         return container_of0(o, struct md_object, mo_lu);
390 }
391
392 static inline struct md_object *md_object_next(const struct md_object *obj)
393 {
394         return (obj ? lu2md(lu_object_next(&obj->mo_lu)) : NULL);
395 }
396
397 static inline struct md_device *md_obj2dev(const struct md_object *o)
398 {
399         LASSERT(lu_device_is_md(o->mo_lu.lo_dev));
400         return container_of0(o->mo_lu.lo_dev, struct md_device, md_lu_dev);
401 }
402
403 static inline int md_device_init(struct md_device *md, struct lu_device_type *t)
404 {
405         return lu_device_init(&md->md_lu_dev, t);
406 }
407
408 static inline void md_device_fini(struct md_device *md)
409 {
410         lu_device_fini(&md->md_lu_dev);
411 }
412
413 /* md operations */
414 static inline int mo_permission(const struct lu_env *env,
415                                 struct md_object *p,
416                                 struct md_object *c,
417                                 struct md_attr *at,
418                                 int mask)
419 {
420         LASSERT(c->mo_ops->moo_permission);
421         return c->mo_ops->moo_permission(env, p, c, at, mask);
422 }
423
424 static inline int mo_attr_get(const struct lu_env *env,
425                               struct md_object *m,
426                               struct md_attr *at)
427 {
428         LASSERT(m->mo_ops->moo_attr_get);
429         return m->mo_ops->moo_attr_get(env, m, at);
430 }
431
432 static inline int mo_readlink(const struct lu_env *env,
433                               struct md_object *m,
434                               struct lu_buf *buf)
435 {
436         LASSERT(m->mo_ops->moo_readlink);
437         return m->mo_ops->moo_readlink(env, m, buf);
438 }
439
440 static inline int mo_attr_set(const struct lu_env *env,
441                               struct md_object *m,
442                               const struct md_attr *at)
443 {
444         LASSERT(m->mo_ops->moo_attr_set);
445         return m->mo_ops->moo_attr_set(env, m, at);
446 }
447
448 static inline int mo_xattr_get(const struct lu_env *env,
449                                struct md_object *m,
450                                struct lu_buf *buf,
451                                const char *name)
452 {
453         LASSERT(m->mo_ops->moo_xattr_get);
454         return m->mo_ops->moo_xattr_get(env, m, buf, name);
455 }
456
457 static inline int mo_xattr_del(const struct lu_env *env,
458                                struct md_object *m,
459                                const char *name)
460 {
461         LASSERT(m->mo_ops->moo_xattr_del);
462         return m->mo_ops->moo_xattr_del(env, m, name);
463 }
464
465 static inline int mo_xattr_set(const struct lu_env *env,
466                                struct md_object *m,
467                                const struct lu_buf *buf,
468                                const char *name,
469                                int flags)
470 {
471         LASSERT(m->mo_ops->moo_xattr_set);
472         return m->mo_ops->moo_xattr_set(env, m, buf, name, flags);
473 }
474
475 static inline int mo_xattr_list(const struct lu_env *env,
476                                 struct md_object *m,
477                                 struct lu_buf *buf)
478 {
479         LASSERT(m->mo_ops->moo_xattr_list);
480         return m->mo_ops->moo_xattr_list(env, m, buf);
481 }
482
483 static inline int mo_open(const struct lu_env *env,
484                           struct md_object *m,
485                           int flags)
486 {
487         LASSERT(m->mo_ops->moo_open);
488         return m->mo_ops->moo_open(env, m, flags);
489 }
490
491 static inline int mo_close(const struct lu_env *env,
492                            struct md_object *m,
493                            struct md_attr *ma)
494 {
495         LASSERT(m->mo_ops->moo_close);
496         return m->mo_ops->moo_close(env, m, ma);
497 }
498
499 static inline int mo_readpage(const struct lu_env *env,
500                               struct md_object *m,
501                               const struct lu_rdpg *rdpg)
502 {
503         LASSERT(m->mo_ops->moo_readpage);
504         return m->mo_ops->moo_readpage(env, m, rdpg);
505 }
506
507 static inline int mo_object_create(const struct lu_env *env,
508                                    struct md_object *m,
509                                    const struct md_op_spec *spc,
510                                    struct md_attr *at)
511 {
512         LASSERT(m->mo_ops->moo_object_create);
513         return m->mo_ops->moo_object_create(env, m, spc, at);
514 }
515
516 static inline int mo_ref_add(const struct lu_env *env,
517                              struct md_object *m,
518                              const struct md_attr *ma)
519 {
520         LASSERT(m->mo_ops->moo_ref_add);
521         return m->mo_ops->moo_ref_add(env, m, ma);
522 }
523
524 static inline int mo_ref_del(const struct lu_env *env,
525                              struct md_object *m,
526                              struct md_attr *ma)
527 {
528         LASSERT(m->mo_ops->moo_ref_del);
529         return m->mo_ops->moo_ref_del(env, m, ma);
530 }
531
532 static inline int mo_capa_get(const struct lu_env *env,
533                               struct md_object *m,
534                               struct lustre_capa *c,
535                               int renewal)
536 {
537         LASSERT(m->mo_ops->moo_capa_get);
538         return m->mo_ops->moo_capa_get(env, m, c, renewal);
539 }
540
541 static inline int mdo_lookup(const struct lu_env *env,
542                              struct md_object *p,
543                              const struct lu_name *lname,
544                              struct lu_fid *f,
545                              struct md_op_spec *spec)
546 {
547         LASSERT(p->mo_dir_ops->mdo_lookup);
548         return p->mo_dir_ops->mdo_lookup(env, p, lname, f, spec);
549 }
550
551 static inline mdl_mode_t mdo_lock_mode(const struct lu_env *env,
552                                        struct md_object *mo,
553                                        mdl_mode_t lm)
554 {
555         if (mo->mo_dir_ops->mdo_lock_mode == NULL)
556                 return MDL_MINMODE;
557         return mo->mo_dir_ops->mdo_lock_mode(env, mo, lm);
558 }
559
560 static inline int mdo_create(const struct lu_env *env,
561                              struct md_object *p,
562                              const struct lu_name *lchild_name,
563                              struct md_object *c,
564                              struct md_op_spec *spc,
565                              struct md_attr *at)
566 {
567         LASSERT(c->mo_dir_ops->mdo_create);
568         return c->mo_dir_ops->mdo_create(env, p, lchild_name, c, spc, at);
569 }
570
571 static inline int mdo_create_data(const struct lu_env *env,
572                                   struct md_object *p,
573                                   struct md_object *c,
574                                   const struct md_op_spec *spec,
575                                   struct md_attr *ma)
576 {
577         LASSERT(c->mo_dir_ops->mdo_create_data);
578         return c->mo_dir_ops->mdo_create_data(env, p, c, spec, ma);
579 }
580
581 static inline int mdo_rename(const struct lu_env *env,
582                              struct md_object *sp,
583                              struct md_object *tp,
584                              const struct lu_fid *lf,
585                              const struct lu_name *lsname,
586                              struct md_object *t,
587                              const struct lu_name *ltname,
588                              struct md_attr *ma)
589 {
590         LASSERT(tp->mo_dir_ops->mdo_rename);
591         return tp->mo_dir_ops->mdo_rename(env, sp, tp, lf, lsname, t, ltname,
592                                           ma);
593 }
594
595 static inline int mdo_is_subdir(const struct lu_env *env,
596                                 struct md_object *mo,
597                                 const struct lu_fid *fid,
598                                 struct lu_fid *sfid)
599 {
600         LASSERT(mo->mo_dir_ops->mdo_is_subdir);
601         return mo->mo_dir_ops->mdo_is_subdir(env, mo, fid, sfid);
602 }
603
604 static inline int mdo_link(const struct lu_env *env,
605                            struct md_object *p,
606                            struct md_object *s,
607                            const struct lu_name *lname,
608                            struct md_attr *ma)
609 {
610         LASSERT(s->mo_dir_ops->mdo_link);
611         return s->mo_dir_ops->mdo_link(env, p, s, lname, ma);
612 }
613
614 static inline int mdo_unlink(const struct lu_env *env,
615                              struct md_object *p,
616                              struct md_object *c,
617                              const struct lu_name *lname,
618                              struct md_attr *ma)
619 {
620         LASSERT(c->mo_dir_ops->mdo_unlink);
621         return c->mo_dir_ops->mdo_unlink(env, p, c, lname, ma);
622 }
623
624 static inline int mdo_name_insert(const struct lu_env *env,
625                                   struct md_object *p,
626                                   const struct lu_name *lname,
627                                   const struct lu_fid *f,
628                                   const struct md_attr *ma)
629 {
630         LASSERT(p->mo_dir_ops->mdo_name_insert);
631         return p->mo_dir_ops->mdo_name_insert(env, p, lname, f, ma);
632 }
633
634 static inline int mdo_name_remove(const struct lu_env *env,
635                                   struct md_object *p,
636                                   const struct lu_name *lname,
637                                   const struct md_attr *ma)
638 {
639         LASSERT(p->mo_dir_ops->mdo_name_remove);
640         return p->mo_dir_ops->mdo_name_remove(env, p, lname, ma);
641 }
642
643 static inline int mdo_rename_tgt(const struct lu_env *env,
644                                  struct md_object *p,
645                                  struct md_object *t,
646                                  const struct lu_fid *lf,
647                                  const struct lu_name *lname,
648                                  struct md_attr *ma)
649 {
650         if (t) {
651                 LASSERT(t->mo_dir_ops->mdo_rename_tgt);
652                 return t->mo_dir_ops->mdo_rename_tgt(env, p, t, lf, lname, ma);
653         } else {
654                 LASSERT(p->mo_dir_ops->mdo_rename_tgt);
655                 return p->mo_dir_ops->mdo_rename_tgt(env, p, t, lf, lname, ma);
656         }
657 }
658
659 #endif /* _LINUX_MD_OBJECT_H */