Whamcloud - gitweb
Merge b_md into HEAD
[fs/lustre-release.git] / lustre / include / linux / obd_class.h
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (C) 2001-2003 Cluster File Systems, Inc.
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
8  *   Lustre is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Lustre is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Lustre; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  */
22
23 #ifndef __LINUX_CLASS_OBD_H
24 #define __LINUX_CLASS_OBD_H
25
26 #ifndef __KERNEL__
27 # include <stdint.h>
28 # define __KERNEL__
29 # include <linux/list.h>
30 # undef __KERNEL__
31 #else
32 #include <asm/segment.h>
33 #include <asm/uaccess.h>
34 #include <linux/types.h>
35 #include <linux/fs.h>
36 #include <linux/time.h>
37
38 #include <linux/obd_support.h>
39 #include <linux/obd.h>
40 #include <linux/lustre_lib.h>
41 #include <linux/lustre_idl.h>
42 #include <linux/lustre_mds.h>
43 #include <linux/lustre_dlm.h>
44 #include <linux/lprocfs_status.h>
45 #endif
46
47 /* OBD Device Declarations */
48 #define MAX_OBD_DEVICES 128
49 extern struct obd_device obd_dev[MAX_OBD_DEVICES];
50
51 #define OBD_ATTACHED       0x01
52 #define OBD_SET_UP         0x02
53 #define OBD_RECOVERING     0x04
54 #define OBD_ABORT_RECOVERY 0x08
55 #define OBD_REPLAYABLE     0x10
56 #define OBD_NO_TRANSNO     0x20 /* XXX needs better name */
57
58 /* OBD Operations Declarations */
59
60 #ifdef __KERNEL__
61 static inline int obd_check_conn(struct lustre_handle *conn)
62 {
63         struct obd_device *obd;
64         if (!conn) {
65                 CERROR("NULL conn\n");
66                 RETURN(-ENOTCONN);
67         }
68         obd = class_conn2obd(conn);
69         if (!obd) {
70                 CERROR("NULL obd\n");
71                 RETURN(-ENODEV);
72         }
73
74         if (!obd->obd_flags & OBD_ATTACHED ) {
75                 CERROR("obd %d not attached\n", obd->obd_minor);
76                 RETURN(-ENODEV);
77         }
78
79         if (!obd->obd_flags & OBD_SET_UP) {
80                 CERROR("obd %d not setup\n", obd->obd_minor);
81                 RETURN(-ENODEV);
82         }
83
84         if (!obd->obd_type) {
85                 CERROR("obd %d not typed\n", obd->obd_minor);
86                 RETURN(-ENODEV);
87         }
88
89         if (!obd->obd_type->typ_ops) {
90                 CERROR("obd_check_conn: obd %d no operations\n",
91                        obd->obd_minor);
92                 RETURN(-EOPNOTSUPP);
93         }
94         return 0;
95 }
96
97
98 #define OBT(dev)        (dev)->obd_type
99 #define OBP(dev, op)    (dev)->obd_type->typ_ops->o_ ## op
100
101 #define OBD_CHECK_SETUP(conn, exp)                              \
102 do {                                                            \
103         if (!(conn)) {                                          \
104                 CERROR("NULL connection\n");                    \
105                 RETURN(-EINVAL);                                \
106         }                                                       \
107                                                                 \
108         exp = class_conn2export(conn);                          \
109         if (!(exp)) {                                           \
110                 CERROR("No export for conn "LPX64":"LPX64"\n",  \
111                        conn->addr, conn->cookie);               \
112                 RETURN(-EINVAL);                                \
113         }                                                       \
114                                                                 \
115         if (!((exp)->exp_obd->obd_flags & OBD_SET_UP)) {        \
116                 CERROR("Device %d not setup\n",                 \
117                        (exp)->exp_obd->obd_minor);              \
118                 RETURN(-EINVAL);                                \
119         }                                                       \
120 } while (0)
121
122 #define OBD_CHECK_DEVSETUP(obd)                                 \
123 do {                                                            \
124         if (!(obd)) {                                           \
125                 CERROR("NULL device\n");                        \
126                 RETURN(-EINVAL);                                \
127         }                                                       \
128                                                                 \
129         if (!((obd)->obd_flags & OBD_SET_UP)) {                 \
130                 CERROR("Device %d not setup\n",                 \
131                        (obd)->obd_minor);                       \
132                 RETURN(-EINVAL);                                \
133         }                                                       \
134 } while (0)
135
136 #define OBD_CHECK_OP(obd, op)                                   \
137 do {                                                            \
138         if (!OBP((obd), op)) {                                  \
139                 CERROR("obd_" #op ": dev %d no operation\n",    \
140                        obd->obd_minor);                         \
141                 RETURN(-EOPNOTSUPP);                            \
142         }                                                       \
143 } while (0)
144
145 static inline int obd_get_info(struct lustre_handle *conn, obd_count keylen,
146                                void *key, obd_count *vallen, void **val)
147 {
148         struct obd_export *exp;
149         int rc;
150         ENTRY;
151
152         OBD_CHECK_SETUP(conn, exp);
153         OBD_CHECK_OP(exp->exp_obd, get_info);
154
155         rc = OBP(exp->exp_obd, get_info)(conn, keylen, key, vallen, val);
156         RETURN(rc);
157 }
158
159 static inline int obd_set_info(struct lustre_handle *conn, obd_count keylen,
160                                void *key, obd_count vallen, void *val)
161 {
162         struct obd_export *exp;
163         int rc;
164         ENTRY;
165
166         OBD_CHECK_SETUP(conn, exp);
167         OBD_CHECK_OP(exp->exp_obd, set_info);
168
169         rc = OBP(exp->exp_obd, set_info)(conn, keylen, key, vallen, val);
170         RETURN(rc);
171 }
172
173 static inline int obd_setup(struct obd_device *obd, int datalen, void *data)
174 {
175         int rc;
176         ENTRY;
177
178         OBD_CHECK_OP(obd, setup);
179
180         rc = OBP(obd, setup)(obd, datalen, data);
181         RETURN(rc);
182 }
183
184 static inline int obd_cleanup(struct obd_device *obd)
185 {
186         int rc;
187         ENTRY;
188
189         OBD_CHECK_DEVSETUP(obd);
190         OBD_CHECK_OP(obd, cleanup);
191
192         rc = OBP(obd, cleanup)(obd);
193         RETURN(rc);
194 }
195
196 /* Pack an in-memory MD struct for sending to the MDS and/or disk.
197  * Returns +ve size of packed MD (0 for free), or -ve error.
198  *
199  * If @wire_tgt == NULL, MD size is returned (max size if @mem_src == NULL).
200  * If @*wire_tgt != NULL and @mem_src == NULL, @*wire_tgt will be freed.
201  * If @*wire_tgt == NULL, it will be allocated
202  */
203 static inline int obd_packmd(struct lustre_handle *conn,
204                              struct lov_mds_md **wire_tgt,
205                              struct lov_stripe_md *mem_src)
206 {
207         struct obd_export *exp;
208         ENTRY;
209
210         OBD_CHECK_SETUP(conn, exp);
211         OBD_CHECK_OP(exp->exp_obd, packmd);
212
213         RETURN(OBP(exp->exp_obd, packmd)(conn, wire_tgt, mem_src));
214 }
215
216 static inline int obd_size_wiremd(struct lustre_handle *conn,
217                                   struct lov_stripe_md *mem_src)
218 {
219         return obd_packmd(conn, NULL, mem_src);
220 }
221
222 /* helper functions */
223 static inline int obd_alloc_wiremd(struct lustre_handle *conn,
224                                    struct lov_mds_md **wire_tgt)
225 {
226         LASSERT(wire_tgt);
227         LASSERT(*wire_tgt == NULL);
228         return obd_packmd(conn, wire_tgt, NULL);
229 }
230
231 static inline int obd_free_wiremd(struct lustre_handle *conn,
232                                   struct lov_mds_md **wire_tgt)
233 {
234         LASSERT(wire_tgt);
235         LASSERT(*wire_tgt);
236         return obd_packmd(conn, wire_tgt, NULL);
237 }
238
239 /* Unpack an MD struct from the MDS and/or disk to in-memory format.
240  * Returns +ve size of unpacked MD (0 for free), or -ve error.
241  *
242  * If @mem_tgt == NULL, MD size is returned (max size if @wire_src == NULL).
243  * If @*mem_tgt != NULL and @wire_src == NULL, @*mem_tgt will be freed.
244  * If @*mem_tgt == NULL, it will be allocated
245  */
246 static inline int obd_unpackmd(struct lustre_handle *conn,
247                                struct lov_stripe_md **mem_tgt,
248                                struct lov_mds_md *wire_src)
249 {
250         struct obd_export *exp;
251         ENTRY;
252
253         OBD_CHECK_SETUP(conn, exp);
254         OBD_CHECK_OP(exp->exp_obd, unpackmd);
255
256         RETURN(OBP(exp->exp_obd, unpackmd)(conn, mem_tgt, wire_src));
257 }
258
259 static inline int obd_size_memmd(struct lustre_handle *conn,
260                                  struct lov_mds_md *wire_src)
261 {
262         return obd_unpackmd(conn, NULL, wire_src);
263 }
264
265 /* helper functions */
266 static inline int obd_alloc_memmd(struct lustre_handle *conn,
267                                   struct lov_stripe_md **mem_tgt)
268 {
269         LASSERT(mem_tgt);
270         LASSERT(*mem_tgt == NULL);
271         return obd_unpackmd(conn, mem_tgt, NULL);
272 }
273
274 static inline int obd_free_memmd(struct lustre_handle *conn,
275                                  struct lov_stripe_md **mem_tgt)
276 {
277         LASSERT(mem_tgt);
278         LASSERT(*mem_tgt);
279         return obd_unpackmd(conn, mem_tgt, NULL);
280 }
281
282 static inline int obd_create(struct lustre_handle *conn, struct obdo *obdo,
283                              struct lov_stripe_md **ea,
284                              struct obd_trans_info *oti)
285 {
286         struct obd_export *exp;
287         int rc;
288         ENTRY;
289
290         OBD_CHECK_SETUP(conn, exp);
291         OBD_CHECK_OP(exp->exp_obd, create);
292
293         rc = OBP(exp->exp_obd, create)(conn, obdo, ea, oti);
294         RETURN(rc);
295 }
296
297 static inline int obd_destroy(struct lustre_handle *conn, struct obdo *obdo,
298                               struct lov_stripe_md *ea,
299                               struct obd_trans_info *oti)
300 {
301         struct obd_export *exp;
302         int rc;
303         ENTRY;
304
305         OBD_CHECK_SETUP(conn, exp);
306         OBD_CHECK_OP(exp->exp_obd, destroy);
307
308         rc = OBP(exp->exp_obd, destroy)(conn, obdo, ea, oti);
309         RETURN(rc);
310 }
311
312 static inline int obd_getattr(struct lustre_handle *conn, struct obdo *obdo,
313                               struct lov_stripe_md *ea)
314 {
315         struct obd_export *exp;
316         int rc;
317         ENTRY;
318
319         OBD_CHECK_SETUP(conn, exp);
320         OBD_CHECK_OP(exp->exp_obd, getattr);
321
322         rc = OBP(exp->exp_obd, getattr)(conn, obdo, ea);
323         RETURN(rc);
324 }
325
326 static inline int obd_close(struct lustre_handle *conn, struct obdo *obdo,
327                             struct lov_stripe_md *ea,
328                             struct obd_trans_info *oti)
329 {
330         struct obd_export *exp;
331         int rc;
332         ENTRY;
333
334         OBD_CHECK_SETUP(conn, exp);
335         OBD_CHECK_OP(exp->exp_obd, close);
336
337         rc = OBP(exp->exp_obd, close)(conn, obdo, ea, oti);
338         RETURN(rc);
339 }
340
341 static inline int obd_open(struct lustre_handle *conn, struct obdo *obdo,
342                            struct lov_stripe_md *ea, struct obd_trans_info *oti)
343 {
344         struct obd_export *exp;
345         int rc;
346         ENTRY;
347
348         OBD_CHECK_SETUP(conn, exp);
349         OBD_CHECK_OP(exp->exp_obd, open);
350
351         rc = OBP(exp->exp_obd, open)(conn, obdo, ea, oti);
352         RETURN(rc);
353 }
354
355 static inline int obd_setattr(struct lustre_handle *conn, struct obdo *obdo,
356                               struct lov_stripe_md *ea,
357                               struct obd_trans_info *oti)
358 {
359         struct obd_export *exp;
360         int rc;
361         ENTRY;
362
363         OBD_CHECK_SETUP(conn, exp);
364         OBD_CHECK_OP(exp->exp_obd, setattr);
365
366         rc = OBP(exp->exp_obd, setattr)(conn, obdo, ea, oti);
367         RETURN(rc);
368 }
369
370 static inline int obd_connect(struct lustre_handle *conn,
371                               struct obd_device *obd, struct obd_uuid *cluuid,
372                               struct recovd_obd *recovd,
373                               ptlrpc_recovery_cb_t recover)
374 {
375         int rc;
376         ENTRY;
377
378         OBD_CHECK_DEVSETUP(obd);
379         OBD_CHECK_OP(obd, connect);
380
381         rc = OBP(obd, connect)(conn, obd, cluuid, recovd, recover);
382         RETURN(rc);
383 }
384
385 static inline int obd_disconnect(struct lustre_handle *conn)
386 {
387         struct obd_export *exp;
388         int rc;
389         ENTRY;
390
391         OBD_CHECK_SETUP(conn, exp);
392         OBD_CHECK_OP(exp->exp_obd, disconnect);
393
394         rc = OBP(exp->exp_obd, disconnect)(conn);
395         RETURN(rc);
396 }
397
398 static inline int obd_statfs(struct lustre_handle *conn,struct obd_statfs *osfs)
399 {
400         struct obd_export *exp;
401         int rc;
402         ENTRY;
403
404         OBD_CHECK_SETUP(conn, exp);
405         OBD_CHECK_OP(exp->exp_obd, statfs);
406
407         rc = OBP(exp->exp_obd, statfs)(conn, osfs);
408         RETURN(rc);
409 }
410
411 static inline int obd_punch(struct lustre_handle *conn, struct obdo *oa,
412                             struct lov_stripe_md *ea, obd_size start,
413                             obd_size end, struct obd_trans_info *oti)
414 {
415         struct obd_export *exp;
416         int rc;
417         ENTRY;
418
419         OBD_CHECK_SETUP(conn, exp);
420         OBD_CHECK_OP(exp->exp_obd, punch);
421
422         rc = OBP(exp->exp_obd, punch)(conn, oa, ea, start, end, oti);
423         RETURN(rc);
424 }
425
426 static inline int obd_brw(int cmd, struct lustre_handle *conn,
427                           struct lov_stripe_md *ea, obd_count oa_bufs,
428                           struct brw_page *pg, struct obd_brw_set *set,
429                           struct obd_trans_info *oti)
430 {
431         struct obd_export *exp;
432         int rc;
433         ENTRY;
434
435         OBD_CHECK_SETUP(conn, exp);
436         OBD_CHECK_OP(exp->exp_obd, brw);
437
438         if (!(cmd & OBD_BRW_RWMASK)) {
439                 CERROR("obd_brw: cmd must be OBD_BRW_READ or OBD_BRW_WRITE\n");
440                 LBUG();
441         }
442
443         rc = OBP(exp->exp_obd, brw)(cmd, conn, ea, oa_bufs, pg, set, oti);
444         RETURN(rc);
445 }
446
447 static inline int obd_preprw(int cmd, struct lustre_handle *conn,
448                              int objcount, struct obd_ioobj *obj,
449                              int niocount, struct niobuf_remote *remote,
450                              struct niobuf_local *local, void **desc_private,
451                              struct obd_trans_info *oti)
452 {
453         struct obd_export *exp;
454         int rc;
455         ENTRY;
456
457         OBD_CHECK_SETUP(conn, exp);
458         OBD_CHECK_OP(exp->exp_obd, preprw);
459
460         rc = OBP(exp->exp_obd, preprw)(cmd, conn, objcount, obj, niocount,
461                                        remote, local, desc_private, oti);
462         RETURN(rc);
463 }
464
465 static inline int obd_commitrw(int cmd, struct lustre_handle *conn,
466                                int objcount, struct obd_ioobj *obj,
467                                int niocount, struct niobuf_local *local,
468                                void *desc_private, struct obd_trans_info *oti)
469 {
470         struct obd_export *exp;
471         int rc;
472         ENTRY;
473
474         OBD_CHECK_SETUP(conn, exp);
475         OBD_CHECK_OP(exp->exp_obd, commitrw);
476
477         rc = OBP(exp->exp_obd, commitrw)(cmd, conn, objcount, obj, niocount,
478                                          local, desc_private, oti);
479         RETURN(rc);
480 }
481
482 static inline int obd_iocontrol(unsigned int cmd, struct lustre_handle *conn,
483                                 int len, void *karg, void *uarg)
484 {
485         struct obd_export *exp;
486         int rc;
487         ENTRY;
488
489         OBD_CHECK_SETUP(conn, exp);
490         OBD_CHECK_OP(exp->exp_obd, iocontrol);
491
492         rc = OBP(exp->exp_obd, iocontrol)(cmd, conn, len, karg, uarg);
493         RETURN(rc);
494 }
495
496 static inline int obd_enqueue(struct lustre_handle *conn,
497                               struct lov_stripe_md *ea,
498                               struct lustre_handle *parent_lock,
499                               __u32 type, void *cookie, int cookielen,
500                               __u32 mode, int *flags, void *cb, void *data,
501                               int datalen, struct lustre_handle *lockh)
502 {
503         struct obd_export *exp;
504         int rc;
505         ENTRY;
506
507         OBD_CHECK_SETUP(conn, exp);
508         OBD_CHECK_OP(exp->exp_obd, enqueue);
509
510         rc = OBP(exp->exp_obd, enqueue)(conn, ea, parent_lock, type,
511                                         cookie, cookielen, mode, flags, cb,
512                                         data, datalen, lockh);
513         RETURN(rc);
514 }
515
516 static inline int obd_cancel(struct lustre_handle *conn,
517                              struct lov_stripe_md *ea, __u32 mode,
518                              struct lustre_handle *lockh)
519 {
520         struct obd_export *exp;
521         int rc;
522         ENTRY;
523
524         OBD_CHECK_SETUP(conn, exp);
525         OBD_CHECK_OP(exp->exp_obd, cancel);
526
527         rc = OBP(exp->exp_obd, cancel)(conn, ea, mode, lockh);
528         RETURN(rc);
529 }
530
531 static inline int obd_cancel_unused(struct lustre_handle *conn,
532                                     struct lov_stripe_md *ea, int local)
533 {
534         struct obd_export *exp;
535         int rc;
536         ENTRY;
537
538         OBD_CHECK_SETUP(conn, exp);
539         OBD_CHECK_OP(exp->exp_obd, cancel_unused);
540
541         rc = OBP(exp->exp_obd, cancel_unused)(conn, ea, local);
542         RETURN(rc);
543 }
544
545 #endif
546
547 /* OBD Metadata Support */
548
549 extern int obd_init_caches(void);
550 extern void obd_cleanup_caches(void);
551
552 static inline struct lustre_handle *obdo_handle(struct obdo *oa)
553 {
554         return (struct lustre_handle *)&oa->o_inline;
555 }
556
557 static inline void obd_oa2handle(struct lustre_handle *handle, struct obdo *oa)
558 {
559         if (oa->o_valid |= OBD_MD_FLHANDLE) {
560                 struct lustre_handle *oa_handle = obdo_handle(oa);
561                 memcpy(handle, oa_handle, sizeof(*handle));
562         }
563 }
564
565 static inline void obd_handle2oa(struct obdo *oa, struct lustre_handle *handle)
566 {
567         if (handle && handle->addr) {
568                 struct lustre_handle *oa_handle = obdo_handle(oa);
569                 memcpy(oa_handle, handle, sizeof(*handle));
570                 oa->o_valid |= OBD_MD_FLHANDLE;
571         }
572 }
573
574 #ifdef __KERNEL__
575 /* support routines */
576 extern kmem_cache_t *obdo_cachep;
577 static inline struct obdo *obdo_alloc(void)
578 {
579         struct obdo *oa;
580
581         oa = kmem_cache_alloc(obdo_cachep, SLAB_KERNEL);
582         if (oa == NULL)
583                 LBUG();
584         memset(oa, 0, sizeof (*oa));
585
586         return oa;
587 }
588
589 static inline void obdo_free(struct obdo *oa)
590 {
591         if (!oa)
592                 return;
593         kmem_cache_free(obdo_cachep, oa);
594 }
595
596 static inline void obdo_from_iattr(struct obdo *oa, struct iattr *attr)
597 {
598         unsigned int ia_valid = attr->ia_valid;
599
600         if (ia_valid & ATTR_ATIME) {
601                 oa->o_atime = attr->ia_atime;
602                 oa->o_valid |= OBD_MD_FLATIME;
603         }
604         if (ia_valid & ATTR_MTIME) {
605                 oa->o_mtime = attr->ia_mtime;
606                 oa->o_valid |= OBD_MD_FLMTIME;
607         }
608         if (ia_valid & ATTR_CTIME) {
609                 oa->o_ctime = attr->ia_ctime;
610                 oa->o_valid |= OBD_MD_FLCTIME;
611         }
612         if (ia_valid & ATTR_SIZE) {
613                 oa->o_size = attr->ia_size;
614                 oa->o_valid |= OBD_MD_FLSIZE;
615         }
616         if (ia_valid & ATTR_MODE) {
617                 oa->o_mode = attr->ia_mode;
618                 oa->o_valid |= OBD_MD_FLTYPE | OBD_MD_FLMODE;
619                 if (!in_group_p(oa->o_gid) && !capable(CAP_FSETID))
620                         oa->o_mode &= ~S_ISGID;
621         }
622         if (ia_valid & ATTR_UID) {
623                 oa->o_uid = attr->ia_uid;
624                 oa->o_valid |= OBD_MD_FLUID;
625         }
626         if (ia_valid & ATTR_GID) {
627                 oa->o_gid = attr->ia_gid;
628                 oa->o_valid |= OBD_MD_FLGID;
629         }
630 }
631
632
633 static inline void iattr_from_obdo(struct iattr *attr, struct obdo *oa,
634                                    obd_flag valid)
635 {
636         memset(attr, 0, sizeof(*attr));
637         if (valid & OBD_MD_FLATIME) {
638                 attr->ia_atime = oa->o_atime;
639                 attr->ia_valid |= ATTR_ATIME;
640         }
641         if (valid & OBD_MD_FLMTIME) {
642                 attr->ia_mtime = oa->o_mtime;
643                 attr->ia_valid |= ATTR_MTIME;
644         }
645         if (valid & OBD_MD_FLCTIME) {
646                 attr->ia_ctime = oa->o_ctime;
647                 attr->ia_valid |= ATTR_CTIME;
648         }
649         if (valid & OBD_MD_FLSIZE) {
650                 attr->ia_size = oa->o_size;
651                 attr->ia_valid |= ATTR_SIZE;
652         }
653         if (valid & OBD_MD_FLTYPE) {
654                 attr->ia_mode = (attr->ia_mode & ~S_IFMT)|(oa->o_mode & S_IFMT);
655                 attr->ia_valid |= ATTR_MODE;
656         }
657         if (valid & OBD_MD_FLMODE) {
658                 attr->ia_mode = (attr->ia_mode & S_IFMT)|(oa->o_mode & ~S_IFMT);
659                 attr->ia_valid |= ATTR_MODE;
660                 if (!in_group_p(oa->o_gid) && !capable(CAP_FSETID))
661                         attr->ia_mode &= ~S_ISGID;
662         }
663         if (valid & OBD_MD_FLUID)
664         {
665                 attr->ia_uid = oa->o_uid;
666                 attr->ia_valid |= ATTR_UID;
667         }
668         if (valid & OBD_MD_FLGID) {
669                 attr->ia_gid = oa->o_gid;
670                 attr->ia_valid |= ATTR_GID;
671         }
672 }
673
674
675 /* WARNING: the file systems must take care not to tinker with
676    attributes they don't manage (such as blocks). */
677
678 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
679 #define to_kdev_t(dev) dev
680 #define kdev_t_to_nr(dev) dev
681 #endif
682
683 static inline void obdo_from_inode(struct obdo *dst, struct inode *src,
684                                    obd_flag valid)
685 {
686         if (valid & OBD_MD_FLATIME)
687                 dst->o_atime = src->i_atime;
688         if (valid & OBD_MD_FLMTIME)
689                 dst->o_mtime = src->i_mtime;
690         if (valid & OBD_MD_FLCTIME)
691                 dst->o_ctime = src->i_ctime;
692         if (valid & OBD_MD_FLSIZE)
693                 dst->o_size = src->i_size;
694         if (valid & OBD_MD_FLBLOCKS)   /* allocation of space */
695                 dst->o_blocks = src->i_blocks;
696         if (valid & OBD_MD_FLBLKSZ)
697                 dst->o_blksize = src->i_blksize;
698         if (valid & OBD_MD_FLTYPE)
699                 dst->o_mode = (dst->o_mode & ~S_IFMT) | (src->i_mode & S_IFMT);
700         if (valid & OBD_MD_FLMODE)
701                 dst->o_mode = (dst->o_mode & S_IFMT) | (src->i_mode & ~S_IFMT);
702         if (valid & OBD_MD_FLUID)
703                 dst->o_uid = src->i_uid;
704         if (valid & OBD_MD_FLGID)
705                 dst->o_gid = src->i_gid;
706         if (valid & OBD_MD_FLFLAGS)
707                 dst->o_flags = src->i_flags;
708         if (valid & OBD_MD_FLNLINK)
709                 dst->o_nlink = src->i_nlink;
710         if (valid & OBD_MD_FLGENER)
711                 dst->o_generation = src->i_generation;
712         if (valid & OBD_MD_FLRDEV)
713                 dst->o_rdev = (__u32)kdev_t_to_nr(src->i_rdev);
714
715         dst->o_valid |= (valid & ~OBD_MD_FLID);
716 }
717
718 static inline void obdo_to_inode(struct inode *dst, struct obdo *src,
719                                  obd_flag valid)
720 {
721         valid &= src->o_valid;
722
723         if (valid & OBD_MD_FLATIME)
724                 dst->i_atime = src->o_atime;
725         if (valid & OBD_MD_FLMTIME)
726                 dst->i_mtime = src->o_mtime;
727         if (valid & OBD_MD_FLCTIME && src->o_ctime > dst->i_ctime)
728                 dst->i_ctime = src->o_ctime;
729         if (valid & OBD_MD_FLSIZE)
730                 dst->i_size = src->o_size;
731         if (valid & OBD_MD_FLBLOCKS) /* allocation of space */
732                 dst->i_blocks = src->o_blocks;
733         if (valid & OBD_MD_FLBLKSZ)
734                 dst->i_blksize = src->o_blksize;
735         if (valid & OBD_MD_FLTYPE)
736                 dst->i_mode = (dst->i_mode & ~S_IFMT) | (src->o_mode & S_IFMT);
737         if (valid & OBD_MD_FLMODE)
738                 dst->i_mode = (dst->i_mode & S_IFMT) | (src->o_mode & ~S_IFMT);
739         if (valid & OBD_MD_FLUID)
740                 dst->i_uid = src->o_uid;
741         if (valid & OBD_MD_FLGID)
742                 dst->i_gid = src->o_gid;
743         if (valid & OBD_MD_FLFLAGS)
744                 dst->i_flags = src->o_flags;
745         if (valid & OBD_MD_FLNLINK)
746                 dst->i_nlink = src->o_nlink;
747         if (valid & OBD_MD_FLGENER)
748                 dst->i_generation = src->o_generation;
749         if (valid & OBD_MD_FLRDEV)
750                 dst->i_rdev = to_kdev_t(src->o_rdev);
751 }
752 #endif
753
754 static inline void obdo_cpy_md(struct obdo *dst, struct obdo *src,
755                                obd_flag valid)
756 {
757 #ifdef __KERNEL__
758         CDEBUG(D_INODE, "src obdo %Ld valid 0x%x, dst obdo %Ld\n",
759                (unsigned long long)src->o_id, src->o_valid,
760                (unsigned long long)dst->o_id);
761 #endif
762         if (valid & OBD_MD_FLATIME)
763                 dst->o_atime = src->o_atime;
764         if (valid & OBD_MD_FLMTIME)
765                 dst->o_mtime = src->o_mtime;
766         if (valid & OBD_MD_FLCTIME)
767                 dst->o_ctime = src->o_ctime;
768         if (valid & OBD_MD_FLSIZE)
769                 dst->o_size = src->o_size;
770         if (valid & OBD_MD_FLBLOCKS) /* allocation of space */
771                 dst->o_blocks = src->o_blocks;
772         if (valid & OBD_MD_FLBLKSZ)
773                 dst->o_blksize = src->o_blksize;
774         if (valid & OBD_MD_FLTYPE)
775                 dst->o_mode = (dst->o_mode & ~S_IFMT) | (src->o_mode & S_IFMT);
776         if (valid & OBD_MD_FLMODE)
777                 dst->o_mode = (dst->o_mode & S_IFMT) | (src->o_mode & ~S_IFMT);
778         if (valid & OBD_MD_FLUID)
779                 dst->o_uid = src->o_uid;
780         if (valid & OBD_MD_FLGID)
781                 dst->o_gid = src->o_gid;
782         if (valid & OBD_MD_FLFLAGS)
783                 dst->o_flags = src->o_flags;
784         /*
785         if (valid & OBD_MD_FLOBDFLG)
786                 dst->o_obdflags = src->o_obdflags;
787         */
788         if (valid & OBD_MD_FLNLINK)
789                 dst->o_nlink = src->o_nlink;
790         if (valid & OBD_MD_FLGENER)
791                 dst->o_generation = src->o_generation;
792         if (valid & OBD_MD_FLRDEV)
793                 dst->o_rdev = src->o_rdev;
794         if (valid & OBD_MD_FLINLINE &&
795              src->o_obdflags & OBD_FL_INLINEDATA) {
796                 memcpy(dst->o_inline, src->o_inline, sizeof(src->o_inline));
797                 dst->o_obdflags |= OBD_FL_INLINEDATA;
798         }
799
800         dst->o_valid |= valid;
801 }
802
803
804 /* returns FALSE if comparison (by flags) is same, TRUE if changed */
805 static inline int obdo_cmp_md(struct obdo *dst, struct obdo *src,
806                               obd_flag compare)
807 {
808         int res = 0;
809
810         if ( compare & OBD_MD_FLATIME )
811                 res = (res || (dst->o_atime != src->o_atime));
812         if ( compare & OBD_MD_FLMTIME )
813                 res = (res || (dst->o_mtime != src->o_mtime));
814         if ( compare & OBD_MD_FLCTIME )
815                 res = (res || (dst->o_ctime != src->o_ctime));
816         if ( compare & OBD_MD_FLSIZE )
817                 res = (res || (dst->o_size != src->o_size));
818         if ( compare & OBD_MD_FLBLOCKS ) /* allocation of space */
819                 res = (res || (dst->o_blocks != src->o_blocks));
820         if ( compare & OBD_MD_FLBLKSZ )
821                 res = (res || (dst->o_blksize != src->o_blksize));
822         if ( compare & OBD_MD_FLTYPE )
823                 res = (res || (((dst->o_mode ^ src->o_mode) & S_IFMT) != 0));
824         if ( compare & OBD_MD_FLMODE )
825                 res = (res || (((dst->o_mode ^ src->o_mode) & ~S_IFMT) != 0));
826         if ( compare & OBD_MD_FLUID )
827                 res = (res || (dst->o_uid != src->o_uid));
828         if ( compare & OBD_MD_FLGID )
829                 res = (res || (dst->o_gid != src->o_gid));
830         if ( compare & OBD_MD_FLFLAGS )
831                 res = (res || (dst->o_flags != src->o_flags));
832         if ( compare & OBD_MD_FLNLINK )
833                 res = (res || (dst->o_nlink != src->o_nlink));
834         if ( compare & OBD_MD_FLGENER )
835                 res = (res || (dst->o_generation != src->o_generation));
836         /* XXX Don't know if thses should be included here - wasn't previously
837         if ( compare & OBD_MD_FLINLINE )
838                 res = (res || memcmp(dst->o_inline, src->o_inline));
839         */
840         return res;
841 }
842
843
844 #ifdef __KERNEL__
845 /* I'm as embarrassed about this as you are.
846  *
847  * <shaver> // XXX do not look into _superhack with remaining eye
848  * <shaver> // XXX if this were any uglier, I'd get my own show on MTV */
849 extern int (*ptlrpc_put_connection_superhack)(struct ptlrpc_connection *c);
850 extern void (*ptlrpc_abort_inflight_superhack)(struct obd_import *imp,
851                                                int dying_import);
852
853 int class_register_type(struct obd_ops *ops, struct lprocfs_vars* vars,
854                         char *nm);
855 int class_unregister_type(char *nm);
856 int class_name2dev(char *name);
857 int class_uuid2dev(struct obd_uuid *uuid);
858 struct obd_device *class_uuid2obd(struct obd_uuid *uuid);
859 struct obd_export *class_new_export(struct obd_device *obddev);
860 struct obd_type *class_get_type(char *name);
861 void class_put_type(struct obd_type *type);
862 void class_destroy_export(struct obd_export *exp);
863 int class_connect(struct lustre_handle *conn, struct obd_device *obd,
864                   struct obd_uuid *cluuid);
865 int class_disconnect(struct lustre_handle *conn);
866 void class_disconnect_all(struct obd_device *obddev);
867
868 /* generic operations shared by various OBD types */
869 int class_multi_setup(struct obd_device *obddev, uint32_t len, void *data);
870 int class_multi_cleanup(struct obd_device *obddev);
871
872 extern void (*class_signal_connection_failure)(struct ptlrpc_connection *);
873
874 static inline struct ptlrpc_connection *class_rd2conn(struct recovd_data *rd)
875 {
876         /* reuse list_entry's member-pointer offset stuff */
877         return list_entry(rd, struct ptlrpc_connection, c_recovd_data);
878 }
879
880 struct obd_statfs;
881 struct statfs;
882 void statfs_pack(struct obd_statfs *osfs, struct statfs *sfs);
883 void statfs_unpack(struct statfs *sfs, struct obd_statfs *osfs);
884 void obd_statfs_pack(struct obd_statfs *tgt, struct obd_statfs *src);
885 void obd_statfs_unpack(struct obd_statfs *tgt, struct obd_statfs *src);
886
887
888 struct obd_class_user_state {
889         struct obd_device     *ocus_current_obd;
890         struct list_head       ocus_conns;
891 };
892
893 struct obd_class_user_conn {
894         struct list_head       ocuc_chain;
895         struct lustre_handle   ocuc_conn;
896 };
897
898 #endif
899
900 /* sysctl.c */
901 extern void obd_sysctl_init (void);
902 extern void obd_sysctl_clean (void);
903
904 /* uuid.c  */
905 typedef __u8 class_uuid_t[16];
906 //int class_uuid_parse(struct obd_uuid in, class_uuid_t out);
907 void class_uuid_unparse(class_uuid_t in, struct obd_uuid *out);
908 #endif /* __LINUX_OBD_CLASS_H */