Whamcloud - gitweb
land lustre part of b_hd_sec on HEAD.
[fs/lustre-release.git] / lustre / ptlrpc / pack_generic.c
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  *   Author: Peter J. Braam <braam@clusterfs.com>
6  *   Author: Phil Schwan <phil@clusterfs.com>
7  *   Author: Eric Barton <eeb@clusterfs.com>
8  *
9  *   This file is part of Lustre, http://www.lustre.org.
10  *
11  *   Lustre is free software; you can redistribute it and/or
12  *   modify it under the terms of version 2 of the GNU General Public
13  *   License as published by the Free Software Foundation.
14  *
15  *   Lustre is distributed in the hope that it will be useful,
16  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *   GNU General Public License for more details.
19  *
20  *   You should have received a copy of the GNU General Public License
21  *   along with Lustre; if not, write to the Free Software
22  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  *
24  * (Un)packing of OST requests
25  *
26  */
27
28 #define DEBUG_SUBSYSTEM S_RPC
29 #ifndef __KERNEL__
30 # include <liblustre.h>
31 #endif
32
33 #include <linux/obd_support.h>
34 #include <linux/obd_class.h>
35 #include <linux/lustre_net.h>
36 #include <linux/lustre_sec.h>
37 #include <linux/fcntl.h>
38
39
40 #define HDR_SIZE(count) \
41     size_round(offsetof (struct lustre_msg, buflens[(count)]))
42
43 int lustre_msg_swabbed(struct lustre_msg *msg)
44 {
45         return (msg->magic == __swab32(PTLRPC_MSG_MAGIC));
46 }
47
48 int lustre_msg_check_version(struct lustre_msg *msg, __u32 version)
49 {
50         if (!lustre_msg_swabbed(msg))
51                 return (msg->version & LUSTRE_VERSION_MASK) != version;
52
53         return (__swab32(msg->version) & LUSTRE_VERSION_MASK) != version;
54 }
55
56 void lustre_init_msg (struct lustre_msg *msg, int count, int *lens, char **bufs)
57 {
58         char *ptr;
59         int   i;
60
61         msg->magic = PTLRPC_MSG_MAGIC;
62         msg->version = PTLRPC_MSG_VERSION;
63         msg->bufcount = count;
64         for (i = 0; i < count; i++)
65                 msg->buflens[i] = lens[i];
66
67         if (bufs == NULL)
68                 return;
69
70         ptr = (char *)msg + HDR_SIZE(count);
71         for (i = 0; i < count; i++) {
72                 char *tmp = bufs[i];
73                 LOGL(tmp, lens[i], ptr);
74         }
75 }
76
77 int lustre_pack_request (struct ptlrpc_request *req,
78                          int count, int *lens, char **bufs)
79 {
80         int rc;
81         ENTRY;
82
83         req->rq_reqlen = lustre_msg_size(count, lens);
84         rc = ptlrpcs_cli_alloc_reqbuf(req, req->rq_reqlen);
85         if (rc)
86                 RETURN(rc);
87
88         lustre_init_msg(req->rq_reqmsg, count, lens, bufs);
89         RETURN (0);
90 }
91
92 #if RS_DEBUG
93 LIST_HEAD(ptlrpc_rs_debug_lru);
94 spinlock_t ptlrpc_rs_debug_lock = SPIN_LOCK_UNLOCKED;
95
96 #define PTLRPC_RS_DEBUG_LRU_ADD(rs)                                     \
97 do {                                                                    \
98         unsigned long __flags;                                          \
99                                                                         \
100         spin_lock_irqsave(&ptlrpc_rs_debug_lock, __flags);              \
101         list_add_tail(&(rs)->rs_debug_list, &ptlrpc_rs_debug_lru);      \
102         spin_unlock_irqrestore(&ptlrpc_rs_debug_lock, __flags);         \
103 } while (0)
104
105 #define PTLRPC_RS_DEBUG_LRU_DEL(rs)                                     \
106 do {                                                                    \
107         unsigned long __flags;                                          \
108                                                                         \
109         spin_lock_irqsave(&ptlrpc_rs_debug_lock, __flags);              \
110         list_del(&(rs)->rs_debug_list);                                 \
111         spin_unlock_irqrestore(&ptlrpc_rs_debug_lock, __flags);         \
112 } while (0)
113 #else
114 # define PTLRPC_RS_DEBUG_LRU_ADD(rs) do {} while(0)
115 # define PTLRPC_RS_DEBUG_LRU_DEL(rs) do {} while(0)
116 #endif
117
118 int lustre_pack_reply (struct ptlrpc_request *req,
119                        int count, int *lens, char **bufs)
120 {
121         struct ptlrpc_reply_state *rs;
122         int                        rc;
123         ENTRY;
124
125         LASSERT(req->rq_reply_state == NULL);
126         LASSERT(req->rq_svcsec);
127         LASSERT(req->rq_repmsg == NULL);
128
129         req->rq_replen = lustre_msg_size(count, lens);
130         rc = svcsec_alloc_repbuf(req->rq_svcsec, req, req->rq_replen);
131         if (rc)
132                 RETURN(rc);
133         LASSERT(req->rq_reply_state);
134         LASSERT(req->rq_repmsg == req->rq_reply_state->rs_msg);
135                                                                                                     
136         rs = req->rq_reply_state;
137         rs->rs_svcsec = svcsec_get(req->rq_svcsec);
138         rs->rs_cb_id.cbid_fn = reply_out_callback;
139         rs->rs_cb_id.cbid_arg = rs;
140         rs->rs_srv_ni = req->rq_rqbd->rqbd_srv_ni;
141         INIT_LIST_HEAD(&rs->rs_exp_list);
142         INIT_LIST_HEAD(&rs->rs_obd_list);
143
144         lustre_init_msg(rs->rs_msg, count, lens, bufs);
145
146         PTLRPC_RS_DEBUG_LRU_ADD(rs);
147
148         RETURN (0);
149 }
150
151 void lustre_free_reply_state (struct ptlrpc_reply_state *rs)
152 {
153         struct ptlrpc_svcsec *svcsec = rs->rs_svcsec;
154
155         PTLRPC_RS_DEBUG_LRU_DEL(rs);
156
157         LASSERT (!rs->rs_difficult || rs->rs_handled);
158         LASSERT (!rs->rs_on_net);
159         LASSERT (!rs->rs_scheduled);
160         LASSERT (rs->rs_export == NULL);
161         LASSERT (rs->rs_nlocks == 0);
162         LASSERT (list_empty(&rs->rs_exp_list));
163         LASSERT (list_empty(&rs->rs_obd_list));
164         LASSERT (svcsec);
165
166         if (svcsec->free_repbuf)
167                 svcsec->free_repbuf(svcsec, rs);
168         else
169                 svcsec_free_reply_state(rs);
170
171         svcsec_put(svcsec);
172 }
173
174 /* This returns the size of the buffer that is required to hold a lustre_msg
175  * with the given sub-buffer lengths. */
176 int lustre_msg_size(int count, int *lengths)
177 {
178         int size;
179         int i;
180
181         size = HDR_SIZE (count);
182         for (i = 0; i < count; i++)
183                 size += size_round(lengths[i]);
184
185         return size;
186 }
187
188 int lustre_unpack_msg(struct lustre_msg *m, int len)
189 {
190         int   flipped;
191         int   required_len;
192         int   i;
193         ENTRY;
194
195         /* We can provide a slightly better error log, if we check the
196          * message magic and version first.  In the future, struct
197          * lustre_msg may grow, and we'd like to log a version mismatch,
198          * rather than a short message.
199          *
200          */
201         required_len = MAX (offsetof (struct lustre_msg, version) +
202                             sizeof (m->version),
203                             offsetof (struct lustre_msg, magic) +
204                             sizeof (m->magic));
205         if (len < required_len) {
206                 /* can't even look inside the message */
207                 CERROR ("message length %d too small for magic/version check\n",
208                         len);
209                 RETURN (-EINVAL);
210         }
211
212         flipped = lustre_msg_swabbed(m);
213         if (flipped)
214                 __swab32s (&m->version);
215         else if (m->magic != PTLRPC_MSG_MAGIC) {
216                 CERROR("wrong lustre_msg magic %#08x\n", m->magic);
217                 RETURN (-EINVAL);
218         }
219
220         if ((m->version & ~LUSTRE_VERSION_MASK) != PTLRPC_MSG_VERSION) {
221                 CERROR("wrong lustre_msg version %#08x\n", m->version);
222                 RETURN (-EINVAL);
223         }
224
225         /* Now we know the sender speaks my language (but possibly flipped)...*/
226         required_len = HDR_SIZE(0);
227         if (len < required_len) {
228                 /* can't even look inside the message */
229                 CERROR ("message length %d too small for lustre_msg\n", len);
230                 RETURN (-EINVAL);
231         }
232
233         if (flipped) {
234                 __swab32s (&m->type);
235                 __swab32s (&m->opc);
236                 __swab64s (&m->last_xid);
237                 __swab64s (&m->last_committed);
238                 __swab64s (&m->transno);
239                 __swab32s (&m->status);
240                 __swab32s (&m->bufcount);
241                 __swab32s (&m->flags);
242         }
243
244         required_len = HDR_SIZE(m->bufcount);
245
246         if (len < required_len) {
247                 /* didn't receive all the buffer lengths */
248                 CERROR ("message length %d too small for %d buflens\n",
249                         len, m->bufcount);
250                 RETURN(-EINVAL);
251         }
252
253         for (i = 0; i < m->bufcount; i++) {
254                 if (flipped)
255                         __swab32s (&m->buflens[i]);
256                 required_len += size_round(m->buflens[i]);
257         }
258
259         if (len < required_len) {
260                 CERROR("len: %d, required_len %d\n", len, required_len);
261                 CERROR("bufcount: %d\n", m->bufcount);
262                 for (i = 0; i < m->bufcount; i++)
263                         CERROR("buffer %d length %d\n", i, m->buflens[i]);
264                 RETURN(-EINVAL);
265         }
266
267         RETURN(0);
268 }
269
270 void *lustre_msg_buf(struct lustre_msg *m, int n, int min_size)
271 {
272         int i;
273         int offset;
274         int buflen;
275         int bufcount;
276
277         LASSERT (m != NULL);
278         LASSERT (n >= 0);
279
280         bufcount = m->bufcount;
281         if (n >= bufcount) {
282                 CDEBUG(D_INFO, "msg %p buffer[%d] not present (count %d)\n",
283                        m, n, bufcount);
284                 return NULL;
285         }
286
287         buflen = m->buflens[n];
288         if (buflen < min_size) {
289                 CERROR("msg %p buffer[%d] size %d too small (required %d)\n",
290                        m, n, buflen, min_size);
291                 return NULL;
292         }
293
294         offset = HDR_SIZE(bufcount);
295         for (i = 0; i < n; i++)
296                 offset += size_round(m->buflens[i]);
297
298         return (char *)m + offset;
299 }
300
301 char *lustre_msg_string (struct lustre_msg *m, int index, int max_len)
302 {
303         /* max_len == 0 means the string should fill the buffer */
304         char *str = lustre_msg_buf (m, index, 0);
305         int   slen;
306         int   blen;
307
308         if (str == NULL) {
309                 CERROR ("can't unpack string in msg %p buffer[%d]\n", m, index);
310                 return (NULL);
311         }
312
313         blen = m->buflens[index];
314         slen = strnlen (str, blen);
315
316         if (slen == blen) {                     /* not NULL terminated */
317                 CERROR ("can't unpack non-NULL terminated string in "
318                         "msg %p buffer[%d] len %d\n", m, index, blen);
319                 return (NULL);
320         }
321
322         if (max_len == 0) {
323                 if (slen != blen - 1) {
324                         CERROR ("can't unpack short string in msg %p "
325                                 "buffer[%d] len %d: strlen %d\n",
326                                 m, index, blen, slen);
327                         return (NULL);
328                 }
329         } else if (slen > max_len) {
330                 CERROR ("can't unpack oversized string in msg %p "
331                         "buffer[%d] len %d strlen %d: max %d expected\n",
332                         m, index, blen, slen, max_len);
333                 return (NULL);
334         }
335
336         return (str);
337 }
338
339 /* Wrap up the normal fixed length cases */
340 void *lustre_swab_buf(struct lustre_msg *msg, int index, int min_size,
341                       void *swabber)
342 {
343         void *ptr;
344
345         ptr = lustre_msg_buf(msg, index, min_size);
346         if (ptr == NULL)
347                 return NULL;
348
349         if (swabber != NULL && lustre_msg_swabbed(msg))
350                 ((void (*)(void *))swabber)(ptr);
351
352         return ptr;
353 }
354
355 void *lustre_swab_reqbuf(struct ptlrpc_request *req, int index, int min_size,
356                          void *swabber)
357 {
358         LASSERT_REQSWAB(req, index);
359         return lustre_swab_buf(req->rq_reqmsg, index, min_size, swabber);
360 }
361
362 void *lustre_swab_repbuf(struct ptlrpc_request *req, int index, int min_size,
363                          void *swabber)
364 {
365         LASSERT_REPSWAB(req, index);
366         return lustre_swab_buf(req->rq_repmsg, index, min_size, swabber);
367 }
368
369 /* byte flipping routines for all wire types declared in
370  * lustre_idl.h implemented here.
371  */
372
373 void lustre_swab_obdo (struct obdo  *o)
374 {
375         __swab64s (&o->o_id);
376         __swab64s (&o->o_gr);
377         __swab64s (&o->o_atime);
378         __swab64s (&o->o_mtime);
379         __swab64s (&o->o_ctime);
380         __swab64s (&o->o_size);
381         __swab64s (&o->o_blocks);
382         __swab64s (&o->o_grant);
383         __swab32s (&o->o_blksize);
384         __swab32s (&o->o_mode);
385         __swab32s (&o->o_uid);
386         __swab32s (&o->o_gid);
387         __swab32s (&o->o_flags);
388         __swab32s (&o->o_nlink);
389         __swab32s (&o->o_generation);
390         __swab64s (&o->o_valid);
391         __swab32s (&o->o_misc);
392         __swab32s (&o->o_easize);
393         __swab32s (&o->o_mds);
394         __swab64s (&o->o_fid);
395         /* o_inline is opaque */
396 }
397
398 /* mdc pack methods used by mdc and smfs*/
399 void *mdc_create_pack(struct lustre_msg *msg, int offset,
400                       struct mdc_op_data *op_data, __u32 mode,
401                       __u64 rdev, const void *data, int datalen)
402 {
403         struct mds_rec_create *rec;
404         char *tmp;
405         rec = lustre_msg_buf(msg, offset, sizeof (*rec));
406
407         rec->cr_opcode = REINT_CREATE;
408         rec->cr_id = op_data->id1;
409         memset(&rec->cr_replayid, 0, sizeof(rec->cr_replayid));
410         rec->cr_mode = mode;
411         rec->cr_rdev = rdev;
412         rec->cr_time = op_data->mod_time;
413
414         tmp = lustre_msg_buf(msg, offset + 1, op_data->namelen + 1);
415         LOGL0(op_data->name, op_data->namelen, tmp);
416
417         if (data) {
418                 tmp = lustre_msg_buf(msg, offset + 2, datalen);
419                 memcpy (tmp, data, datalen);
420         }
421         return ((void*)tmp + size_round(datalen));
422 }
423
424 void *mdc_setattr_pack(struct lustre_msg *msg, int offset,
425                        struct mdc_op_data *data, struct iattr *iattr,
426                        void *ea, int ealen, void *ea2, int ea2len)
427 {
428         struct mds_rec_setattr *rec = lustre_msg_buf(msg, offset, sizeof(*rec));
429         char *tmp = NULL;
430
431         rec->sa_opcode = REINT_SETATTR;
432         rec->sa_id = data->id1;
433
434         if (iattr) {
435                 rec->sa_valid = iattr->ia_valid;
436                 rec->sa_mode = iattr->ia_mode;
437                 rec->sa_uid = iattr->ia_uid;
438                 rec->sa_gid = iattr->ia_gid;
439                 rec->sa_size = iattr->ia_size;
440                 rec->sa_atime = LTIME_S(iattr->ia_atime);
441                 rec->sa_mtime = LTIME_S(iattr->ia_mtime);
442                 rec->sa_ctime = LTIME_S(iattr->ia_ctime);
443                 rec->sa_attr_flags = iattr->ia_attr_flags;
444         }
445         tmp = (char*)rec + size_round(sizeof(*rec));
446                 
447         if (ealen == 0)
448                 return (void*)tmp;
449
450         memcpy(lustre_msg_buf(msg, offset + 1, ealen), ea, ealen);
451         tmp += size_round(ealen);
452
453         if (ea2len == 0)
454                 return (void*)tmp;
455
456         memcpy(lustre_msg_buf(msg, offset + 2, ea2len), ea2, ea2len);
457         tmp += size_round(ea2len);
458         return (void*)tmp;
459 }
460
461 void *mdc_unlink_pack(struct lustre_msg *msg, int offset,
462                       struct mdc_op_data *data)
463 {
464         struct mds_rec_unlink *rec;
465         char *tmp;
466
467         rec = lustre_msg_buf(msg, offset, sizeof (*rec));
468         LASSERT (rec != NULL);
469
470         rec->ul_opcode = REINT_UNLINK;
471         rec->ul_mode = data->create_mode;
472         rec->ul_id1 = data->id1;
473         rec->ul_id2 = data->id2;
474         rec->ul_time = data->mod_time;
475
476         tmp = lustre_msg_buf(msg, offset + 1, data->namelen + 1);
477         LASSERT (tmp != NULL);
478         LOGL0(data->name, data->namelen, tmp);
479         return (void*)tmp;        
480 }
481
482 void *mdc_link_pack(struct lustre_msg *msg, int offset,
483                     struct mdc_op_data *data)
484 {
485         struct mds_rec_link *rec;
486         char *tmp;
487
488         rec = lustre_msg_buf(msg, offset, sizeof (*rec));
489
490         rec->lk_opcode = REINT_LINK;
491         rec->lk_id1 = data->id1;
492         rec->lk_id2 = data->id2;
493         rec->lk_time = data->mod_time;
494
495         tmp = lustre_msg_buf(msg, offset + 1, data->namelen + 1);
496         LOGL0(data->name, data->namelen, tmp);
497         
498         return (void*)tmp; 
499 }
500
501 void *mdc_rename_pack(struct lustre_msg *msg, int offset,
502                       struct mdc_op_data *data,
503                       const char *old, int oldlen,
504                       const char *new, int newlen)
505 {
506         struct mds_rec_rename *rec;
507         char *tmp;
508
509         rec = lustre_msg_buf(msg, offset, sizeof (*rec));
510
511         /* XXX do something about time, uid, gid */
512         rec->rn_opcode = REINT_RENAME;
513         rec->rn_id1 = data->id1;
514         rec->rn_id2 = data->id2;
515         rec->rn_time = data->mod_time;
516
517         tmp = lustre_msg_buf(msg, offset + 1, oldlen + 1);
518         LOGL0(old, oldlen, tmp);
519
520         if (new) {
521                 tmp = lustre_msg_buf(msg, offset + 2, newlen + 1);
522                 LOGL0(new, newlen, tmp);
523         }
524         return (void*)tmp;
525 }
526
527 void lustre_swab_obd_statfs (struct obd_statfs *os)
528 {
529         __swab64s (&os->os_type);
530         __swab64s (&os->os_blocks);
531         __swab64s (&os->os_bfree);
532         __swab64s (&os->os_bavail);
533         __swab64s (&os->os_ffree);
534         /* no need to swap os_fsid */
535         __swab32s (&os->os_bsize);
536         __swab32s (&os->os_namelen);
537         /* no need to swap os_spare */
538 }
539
540 void lustre_swab_obd_ioobj (struct obd_ioobj *ioo)
541 {
542         __swab64s (&ioo->ioo_id);
543         __swab64s (&ioo->ioo_gr);
544         __swab32s (&ioo->ioo_type);
545         __swab32s (&ioo->ioo_bufcnt);
546 }
547
548 void lustre_swab_niobuf_remote (struct niobuf_remote *nbr)
549 {
550         __swab64s (&nbr->offset);
551         __swab32s (&nbr->len);
552         __swab32s (&nbr->flags);
553 }
554
555 void lustre_swab_ost_body (struct ost_body *b)
556 {
557         lustre_swab_obdo (&b->oa);
558 }
559
560 void lustre_swab_ost_last_id(obd_id *id)
561 {
562         __swab64s(id);
563 }
564
565 void lustre_swab_generic_32s(__u32 *val)
566 {
567         __swab32s(val);
568 }
569
570 void lustre_swab_ost_lvb(struct ost_lvb *lvb)
571 {
572         __swab64s(&lvb->lvb_size);
573         __swab64s(&lvb->lvb_mtime);
574         __swab64s(&lvb->lvb_atime);
575         __swab64s(&lvb->lvb_ctime);
576         __swab64s(&lvb->lvb_blocks);
577 }
578
579 void lustre_swab_lustre_stc (struct lustre_stc *stc)
580 {
581         __swab64s (&stc->u.e3s.l3s_ino);
582         __swab32s (&stc->u.e3s.l3s_gen);
583         __swab32s (&stc->u.e3s.l3s_type);
584 }
585
586 void lustre_swab_lustre_fid(struct lustre_fid *fid)
587 {
588         __swab64s (&fid->lf_id);
589         __swab64s (&fid->lf_group);
590         __swab32s (&fid->lf_version);
591 }
592
593 void lustre_swab_lustre_id (struct lustre_id *id)
594 {
595         lustre_swab_lustre_stc(&id->li_stc);
596         lustre_swab_lustre_fid(&id->li_fid);
597 }
598
599 void lustre_swab_mds_status_req (struct mds_status_req *r)
600 {
601         __swab32s (&r->flags);
602         __swab32s (&r->repbuf);
603 }
604
605 /* 
606  * because sec_desc is variable buffer, we must check it by hand
607  */
608 struct mds_req_sec_desc *lustre_swab_mds_secdesc(struct ptlrpc_request *req,
609                                                  int offset)
610 {
611         struct mds_req_sec_desc *rsd;
612         struct lustre_msg *m;
613         __u32 i;
614
615         LASSERT_REQSWAB(req, offset);
616
617         m = req->rq_reqmsg;
618         rsd = lustre_msg_buf(m, offset, sizeof(*rsd));
619         if (!rsd)
620                 return NULL;
621
622         if (lustre_msg_swabbed(m)) {
623                 __swab32s(&rsd->rsd_uid);
624                 __swab32s(&rsd->rsd_gid);
625                 __swab32s(&rsd->rsd_fsuid);
626                 __swab32s(&rsd->rsd_fsgid);
627                 __swab32s(&rsd->rsd_cap);
628                 __swab32s(&rsd->rsd_ngroups);
629         }
630
631         if (rsd->rsd_ngroups > LUSTRE_MAX_GROUPS) {
632                 CERROR("%u groups is not allowed\n", rsd->rsd_ngroups);
633                 return NULL;
634         }
635
636         if (m->buflens[offset] !=
637             sizeof(*rsd) + rsd->rsd_ngroups * sizeof(__u32)) {
638                 CERROR("bufflen %u while contains %u groups\n",
639                         m->buflens[offset], rsd->rsd_ngroups);
640                 return NULL;
641         }
642
643         if (lustre_msg_swabbed(m)) {
644                 for (i = 0; i < rsd->rsd_ngroups; i++)
645                         __swab32s(&rsd->rsd_groups[i]);
646         }
647
648         return rsd;
649 }
650
651 void lustre_swab_mds_body (struct mds_body *b)
652 {
653         lustre_swab_lustre_id (&b->id1);
654         lustre_swab_lustre_id (&b->id2);
655         /* handle is opaque */
656         __swab64s (&b->size);
657         __swab64s (&b->blocks);
658         __swab64s (&b->valid);
659         __swab32s (&b->mode);
660         __swab32s (&b->uid);
661         __swab32s (&b->gid);
662         __swab32s (&b->mtime);
663         __swab32s (&b->ctime);
664         __swab32s (&b->atime);
665         __swab32s (&b->flags);
666         __swab32s (&b->rdev);
667         __swab32s (&b->nlink);
668         __swab32s (&b->eadatasize);
669 }
670 void lustre_swab_mds_rec_setattr (struct mds_rec_setattr *sa)
671 {
672         __swab32s (&sa->sa_opcode);
673         __swab32s (&sa->sa_valid);
674         lustre_swab_lustre_id (&sa->sa_id);
675         __swab32s (&sa->sa_mode);
676         __swab32s (&sa->sa_uid);
677         __swab32s (&sa->sa_gid);
678         __swab32s (&sa->sa_attr_flags);
679         __swab64s (&sa->sa_size);
680         __swab64s (&sa->sa_atime);
681         __swab64s (&sa->sa_mtime);
682         __swab64s (&sa->sa_ctime);
683 }
684
685 void lustre_swab_mds_rec_create (struct mds_rec_create *cr)
686 {
687         __swab32s (&cr->cr_opcode);
688         __swab32s (&cr->cr_flags); /* for use with open */
689         __swab32s (&cr->cr_mode);
690         lustre_swab_lustre_id (&cr->cr_id);
691         lustre_swab_lustre_id (&cr->cr_replayid);
692         __swab64s (&cr->cr_time);
693         __swab64s (&cr->cr_rdev);
694 }
695
696 void lustre_swab_mds_rec_link (struct mds_rec_link *lk)
697 {
698         __swab32s (&lk->lk_opcode);
699         lustre_swab_lustre_id (&lk->lk_id1);
700         lustre_swab_lustre_id (&lk->lk_id2);
701 }
702
703 void lustre_swab_mds_rec_unlink (struct mds_rec_unlink *ul)
704 {
705         __swab32s (&ul->ul_opcode);
706         __swab32s (&ul->ul_mode);
707         lustre_swab_lustre_id (&ul->ul_id1);
708         lustre_swab_lustre_id (&ul->ul_id2);
709 }
710
711 void lustre_swab_mds_rec_rename (struct mds_rec_rename *rn)
712 {
713         __swab32s (&rn->rn_opcode);
714         lustre_swab_lustre_id (&rn->rn_id1);
715         lustre_swab_lustre_id (&rn->rn_id2);
716 }
717
718 void lustre_swab_lov_desc (struct lov_desc *ld)
719 {
720         __swab32s (&ld->ld_tgt_count);
721         __swab32s (&ld->ld_active_tgt_count);
722         __swab32s (&ld->ld_default_stripe_count);
723         __swab64s (&ld->ld_default_stripe_size);
724         __swab64s (&ld->ld_default_stripe_offset);
725         __swab32s (&ld->ld_pattern);
726         /* uuid endian insensitive */
727 }
728
729 void lustre_swab_ldlm_res_id (struct ldlm_res_id *id)
730 {
731         int  i;
732
733         for (i = 0; i < RES_NAME_SIZE; i++)
734                 __swab64s (&id->name[i]);
735 }
736
737 void lustre_swab_ldlm_policy_data (ldlm_policy_data_t *d)
738 {
739         /* the lock data is a union and the first three fields of both EXTENT
740          * and FLOCK types are __u64, so it's ok to swab them in the same way */
741         __swab64s (&d->l_flock.start);
742         __swab64s (&d->l_flock.end);
743         __swab64s (&d->l_flock.pid);
744         __swab64s (&d->l_flock.blocking_pid);
745 }
746
747 void lustre_swab_ldlm_intent (struct ldlm_intent *i)
748 {
749         __swab64s (&i->opc);
750 }
751
752 void lustre_swab_ldlm_resource_desc (struct ldlm_resource_desc *r)
753 {
754         __swab32s (&r->lr_type);
755         lustre_swab_ldlm_res_id (&r->lr_name);
756 }
757
758 void lustre_swab_ldlm_lock_desc (struct ldlm_lock_desc *l)
759 {
760         lustre_swab_ldlm_resource_desc (&l->l_resource);
761         __swab32s (&l->l_req_mode);
762         __swab32s (&l->l_granted_mode);
763         lustre_swab_ldlm_policy_data (&l->l_policy_data);
764 }
765
766 void lustre_swab_ldlm_request (struct ldlm_request *rq)
767 {
768         __swab32s (&rq->lock_flags);
769         lustre_swab_ldlm_lock_desc (&rq->lock_desc);
770         /* lock_handle1 opaque */
771         /* lock_handle2 opaque */
772 }
773
774 void lustre_swab_ldlm_reply (struct ldlm_reply *r)
775 {
776         __swab32s (&r->lock_flags);
777         lustre_swab_ldlm_lock_desc (&r->lock_desc);
778         /* lock_handle opaque */
779         __swab64s (&r->lock_policy_res1);
780         __swab64s (&r->lock_policy_res2);
781 }
782
783 void lustre_swab_ptlbd_op (struct ptlbd_op *op)
784 {
785         __swab16s (&op->op_cmd);
786         __swab16s (&op->op_lun);
787         __swab16s (&op->op_niob_cnt);
788         /* ignore op__padding */
789         __swab32s (&op->op_block_cnt);
790 }
791
792 void lustre_swab_ptlbd_niob (struct ptlbd_niob *n)
793 {
794         __swab64s (&n->n_xid);
795         __swab64s (&n->n_block_nr);
796         __swab32s (&n->n_offset);
797         __swab32s (&n->n_length);
798 }
799
800 void lustre_swab_ptlbd_rsp (struct ptlbd_rsp *r)
801 {
802         __swab16s (&r->r_status);
803         __swab16s (&r->r_error_cnt);
804 }
805
806 /* no one calls this */
807 int llog_log_swabbed(struct llog_log_hdr *hdr)
808 {
809         if (hdr->llh_hdr.lrh_type == __swab32(LLOG_HDR_MAGIC))
810                 return 1;
811         if (hdr->llh_hdr.lrh_type == LLOG_HDR_MAGIC)
812                 return 0;
813         return -1;
814 }
815
816 void lustre_swab_llogd_body (struct llogd_body *d)
817 {
818         __swab64s (&d->lgd_logid.lgl_oid);
819         __swab64s (&d->lgd_logid.lgl_ogr);
820         __swab32s (&d->lgd_logid.lgl_ogen);
821         __swab32s (&d->lgd_ctxt_idx);
822         __swab32s (&d->lgd_llh_flags);
823         __swab32s (&d->lgd_index);
824         __swab32s (&d->lgd_saved_index);
825         __swab32s (&d->lgd_len);
826         __swab64s (&d->lgd_cur_offset);
827 }
828
829 void lustre_swab_llog_hdr (struct llog_log_hdr *h)
830 {
831         __swab32s (&h->llh_hdr.lrh_index);
832         __swab32s (&h->llh_hdr.lrh_len);
833         __swab32s (&h->llh_hdr.lrh_type);
834         __swab64s (&h->llh_timestamp);
835         __swab32s (&h->llh_count);
836         __swab32s (&h->llh_bitmap_offset);
837         __swab32s (&h->llh_flags);
838         __swab32s (&h->llh_tail.lrt_index);
839         __swab32s (&h->llh_tail.lrt_len);
840 }
841
842 void lustre_swab_llogd_conn_body (struct llogd_conn_body *d)
843 {
844         __swab64s (&d->lgdc_gen.mnt_cnt);
845         __swab64s (&d->lgdc_gen.conn_cnt);
846         __swab64s (&d->lgdc_logid.lgl_oid);
847         __swab64s (&d->lgdc_logid.lgl_ogr);
848         __swab32s (&d->lgdc_logid.lgl_ogen);
849         __swab32s (&d->lgdc_ctxt_idx);
850 }
851
852 void lustre_assert_wire_constants(void)
853 {
854 }
855