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