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