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