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