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