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