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