Whamcloud - gitweb
- removed hardcoded checking for ".mntinfo" on MDS to see if we are trying to open...
[fs/lustre-release.git] / lustre / mdc / mdc_request.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Copyright (C) 2001-2004 Cluster File Systems, Inc.
5  *
6  *   This file is part of Lustre, http://www.sf.net/projects/lustre/
7  *
8  *   Lustre is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Lustre is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Lustre; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21
22 #ifndef EXPORT_SYMTAB
23 # define EXPORT_SYMTAB
24 #endif
25 #define DEBUG_SUBSYSTEM S_MDC
26
27 #ifdef __KERNEL__
28 # include <linux/module.h>
29 # include <linux/pagemap.h>
30 # include <linux/miscdevice.h>
31 # include <linux/init.h>
32 #else
33 # include <liblustre.h>
34 #endif
35
36 #include <linux/obd_class.h>
37 #include <linux/lustre_mds.h>
38 #include <linux/lustre_dlm.h>
39 #include <linux/lustre_sec.h>
40 #include <linux/lprocfs_status.h>
41 #include <linux/lustre_acl.h>
42 #include "mdc_internal.h"
43
44 #define REQUEST_MINOR 244
45
46 static int mdc_cleanup(struct obd_device *obd, int flags);
47
48 int mdc_get_secdesc_size(void)
49 {
50 #ifdef __KERNEL__
51         int ngroups = current_ngroups;
52
53         if (ngroups > LUSTRE_MAX_GROUPS)
54                 ngroups = LUSTRE_MAX_GROUPS;
55
56         return sizeof(struct mds_req_sec_desc) +
57                 sizeof(__u32) * ngroups;
58 #else
59         return 0;
60 #endif
61 }
62
63 /*
64  * because group info might have changed since last time we call
65  * get_secdesc_size(), so here we did more sanity check to prevent garbage gids
66  */
67 void mdc_pack_secdesc(struct ptlrpc_request *req, int size)
68 {
69 #ifdef __KERNEL__
70         struct mds_req_sec_desc *rsd;
71         
72 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,4)
73         struct group_info *ginfo;
74 #endif
75
76         rsd = lustre_msg_buf(req->rq_reqmsg,
77                              MDS_REQ_SECDESC_OFF, size);
78         
79         rsd->rsd_uid = current->uid;
80         rsd->rsd_gid = current->gid;
81         rsd->rsd_fsuid = current->fsuid;
82         rsd->rsd_fsgid = current->fsgid;
83         rsd->rsd_cap = current->cap_effective;
84         rsd->rsd_ngroups = (size - sizeof(*rsd)) / sizeof(__u32);
85         LASSERT(rsd->rsd_ngroups <= LUSTRE_MAX_GROUPS);
86
87 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,4)
88         task_lock(current);
89         get_group_info(current->group_info);
90         ginfo = current->group_info;
91         task_unlock(current);
92         if (rsd->rsd_ngroups > ginfo->ngroups)
93                 rsd->rsd_ngroups = ginfo->ngroups;
94         memcpy(rsd->rsd_groups, ginfo->blocks[0],
95                rsd->rsd_ngroups * sizeof(__u32));
96 #else
97         LASSERT(rsd->rsd_ngroups <= NGROUPS);
98         if (rsd->rsd_ngroups > current->ngroups)
99                 rsd->rsd_ngroups = current->ngroups;
100         memcpy(rsd->rsd_groups, current->groups,
101                rsd->rsd_ngroups * sizeof(__u32));
102 #endif
103 #endif
104 }
105
106 extern int mds_queue_req(struct ptlrpc_request *);
107 /* Helper that implements most of mdc_getstatus and signal_completed_replay. */
108 /* XXX this should become mdc_get_info("key"), sending MDS_GET_INFO RPC */
109 static int send_getstatus(struct obd_import *imp, struct lustre_id *rootid,
110                           int level, int msg_flags)
111 {
112         struct ptlrpc_request *req;
113         struct mds_body *body;
114         int rc, size[2] = {0, sizeof(*body)};
115         ENTRY;
116
117         //size[0] = mdc_get_secdesc_size();
118
119         req = ptlrpc_prep_req(imp, LUSTRE_MDS_VERSION, MDS_GETSTATUS,
120                               2, size, NULL);
121         if (!req)
122                 GOTO(out, rc = -ENOMEM);
123
124         //mdc_pack_secdesc(req, size[0]);
125
126         body = lustre_msg_buf(req->rq_reqmsg, MDS_REQ_REC_OFF, sizeof (*body));
127         req->rq_send_state = level;
128         req->rq_replen = lustre_msg_size(1, &size[1]);
129
130         req->rq_reqmsg->flags |= msg_flags;
131         rc = ptlrpc_queue_wait(req);
132
133         if (!rc) {
134                 body = lustre_swab_repbuf (req, 0, sizeof (*body),
135                                            lustre_swab_mds_body);
136                 if (body == NULL) {
137                         CERROR ("Can't extract mds_body\n");
138                         GOTO (out, rc = -EPROTO);
139                 }
140
141                 memcpy(rootid, &body->id1, sizeof(*rootid));
142
143                 CDEBUG(D_NET, "root ino="LPU64", last_committed="LPU64
144                        ", last_xid="LPU64"\n", rootid->li_stc.u.e3s.l3s_ino,
145                        req->rq_repmsg->last_committed, req->rq_repmsg->last_xid);
146         }
147
148         EXIT;
149  out:
150         ptlrpc_req_finished(req);
151         return rc;
152 }
153
154 /* This should be mdc_get_info("rootid") */
155 int mdc_getstatus(struct obd_export *exp, struct lustre_id *rootid)
156 {
157         return send_getstatus(class_exp2cliimp(exp), rootid,
158                               LUSTRE_IMP_FULL, 0);
159 }
160
161 int mdc_getattr_common(struct obd_export *exp, unsigned int ea_size,
162                        struct ptlrpc_request *req)
163 {
164         struct mds_body *body, *reqbody;
165         void            *eadata;
166         int              rc;
167         int              repsize[4] = {sizeof(*body)};
168         int              bufcount = 1;
169         ENTRY;
170
171         /* request message already built */
172
173         if (ea_size != 0) {
174                 repsize[bufcount++] = ea_size;
175                 CDEBUG(D_INODE, "reserved %u bytes for MD/symlink in packet\n",
176                        ea_size);
177         }
178
179         reqbody = lustre_msg_buf(req->rq_reqmsg, 1, sizeof(*reqbody));
180
181         if (reqbody->valid & OBD_MD_FLACL_ACCESS) {
182                 repsize[bufcount++] = 4;
183                 repsize[bufcount++] = xattr_acl_size(LL_ACL_MAX_ENTRIES);
184         }
185
186         req->rq_replen = lustre_msg_size(bufcount, repsize);
187
188         mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
189         rc = ptlrpc_queue_wait(req);
190         mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
191         if (rc != 0)
192                 RETURN (rc);
193
194         body = lustre_swab_repbuf (req, 0, sizeof (*body),
195                                    lustre_swab_mds_body);
196         if (body == NULL) {
197                 CERROR ("Can't unpack mds_body\n");
198                 RETURN (-EPROTO);
199         }
200
201         CDEBUG(D_NET, "mode: %o\n", body->mode);
202
203         LASSERT_REPSWAB (req, 1);
204
205         /* Skip the check if getxattr/listxattr are called with no buffers */
206         if ((reqbody->valid & (OBD_MD_FLEA | OBD_MD_FLEALIST)) &&
207             (reqbody->eadatasize != 0)){
208                 if (body->eadatasize != 0) {
209                 /* reply indicates presence of eadata; check it's there... */
210                         eadata = lustre_msg_buf (req->rq_repmsg, 1,
211                                                  body->eadatasize);
212                         if (eadata == NULL) {
213                                 CERROR ("Missing/short eadata\n");
214                                 RETURN (-EPROTO);
215                         }
216                  }
217          }
218
219         RETURN (0);
220 }
221
222 int mdc_getattr(struct obd_export *exp, struct lustre_id *id,
223                 __u64 valid, const char *ea_name, int ea_namelen,
224                 unsigned int ea_size, struct ptlrpc_request **request)
225 {
226         struct ptlrpc_request *req;
227         struct mds_body *body;
228         int bufcount = 2;
229         int size[3] = {0, sizeof(*body)};
230         int rc;
231         ENTRY;
232
233         /* XXX do we need to make another request here?  We just did a getattr
234          *     to do the lookup in the first place.
235          */
236         size[0] = mdc_get_secdesc_size();
237
238         LASSERT((ea_name != NULL) == (ea_namelen != 0));
239         if (valid & (OBD_MD_FLEA | OBD_MD_FLEALIST)) {
240                 size[bufcount] = ea_namelen;
241                 bufcount++;
242         }
243
244         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
245                               MDS_GETATTR, bufcount, size, NULL);
246         if (!req)
247                 GOTO(out, rc = -ENOMEM);
248
249         mdc_pack_secdesc(req, size[0]);
250
251         body = lustre_msg_buf(req->rq_reqmsg, MDS_REQ_REC_OFF, sizeof (*body));
252         memcpy(&body->id1, id, sizeof(*id));
253         body->valid = valid;
254         body->eadatasize = ea_size;
255
256
257         if (valid & OBD_MD_FLEA) {
258                 LASSERT(strnlen(ea_name, ea_namelen) == (ea_namelen - 1));
259                 memcpy(lustre_msg_buf(req->rq_reqmsg, 2, ea_namelen),
260                        ea_name, ea_namelen);
261         }
262
263         rc = mdc_getattr_common(exp, ea_size, req);
264         if (rc != 0) {
265                 ptlrpc_req_finished (req);
266                 req = NULL;
267         }
268  out:
269         *request = req;
270         RETURN (rc);
271 }
272
273 int mdc_getattr_lock(struct obd_export *exp, struct lustre_id *id,
274                      char *filename, int namelen, __u64 valid,
275                      unsigned int ea_size, struct ptlrpc_request **request)
276 {
277         struct ptlrpc_request *req;
278         struct mds_body *body;
279         int rc, size[3] = {0, sizeof(*body), namelen};
280         ENTRY;
281
282         size[0] = mdc_get_secdesc_size();
283
284         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
285                               MDS_GETATTR_LOCK, 3, size, NULL);
286         if (!req)
287                 GOTO(out, rc = -ENOMEM);
288
289         mdc_pack_secdesc(req, size[0]);
290
291         body = lustre_msg_buf(req->rq_reqmsg, MDS_REQ_REC_OFF, sizeof (*body));
292         memcpy(&body->id1, id, sizeof(*id));
293         body->valid = valid;
294         body->eadatasize = ea_size;
295
296         if (filename != NULL) {
297                 LASSERT (strnlen (filename, namelen) == namelen - 1);
298                 memcpy(lustre_msg_buf(req->rq_reqmsg, 2, namelen),
299                        filename, namelen);
300         } else {
301                 LASSERT(namelen == 1);
302         }
303
304         rc = mdc_getattr_common(exp, ea_size, req);
305         if (rc != 0) {
306                 ptlrpc_req_finished (req);
307                 req = NULL;
308         }
309  out:
310         *request = req;
311         RETURN(rc);
312 }
313
314 /* This should be called with both the request and the reply still packed. */
315 int mdc_store_inode_generation(struct obd_export *exp,
316                                struct ptlrpc_request *req,
317                                int reqoff, int repoff)
318 {
319         struct mds_rec_create *rec =
320                 lustre_msg_buf(req->rq_reqmsg, reqoff, sizeof(*rec));
321         struct mds_body *body =
322                 lustre_msg_buf(req->rq_repmsg, repoff, sizeof(*body));
323
324         LASSERT (rec != NULL);
325         LASSERT (body != NULL);
326
327         memcpy(&rec->cr_replayid, &body->id1, sizeof(rec->cr_replayid));
328         DEBUG_REQ(D_HA, req, "storing generation for ino "DLID4,
329                   OLID4(&rec->cr_replayid));
330         return 0;
331 }
332
333 int mdc_req2lustre_md(struct obd_export *exp_lmv, struct ptlrpc_request *req, 
334                       unsigned int offset, struct obd_export *exp_lov, 
335                       struct lustre_md *md)
336 {
337         void *buf;
338         int rc = 0;
339         int size, acl_off;
340         struct posix_acl *acl;
341         struct lov_mds_md *lmm;
342         ENTRY;
343
344         LASSERT(md != NULL);
345         memset(md, 0, sizeof(*md));
346
347         md->body = lustre_msg_buf(req->rq_repmsg, offset,
348                                   sizeof(*md->body));
349         if (!md->body)
350                 RETURN(-ENOMEM);
351
352         LASSERT_REPSWABBED(req, offset);
353
354         if (!(md->body->valid & OBD_MD_FLEASIZE) && 
355             !(md->body->valid & OBD_MD_FLDIREA))
356                 RETURN(0);
357
358         if (S_ISREG(md->body->mode)) {
359                 if (md->body->eadatasize == 0) {
360                         CERROR("invalid EA size (0) is detected\n");
361                         RETURN(-EPROTO);
362                 }
363
364                 lmm = lustre_msg_buf(req->rq_repmsg, offset + 1,
365                                      md->body->eadatasize);
366                 if (!lmm)
367                         RETURN(-EINVAL);
368
369                 LASSERT(exp_lov != NULL);
370                 
371                 rc = obd_unpackmd(exp_lov, &md->lsm, lmm,
372                                   md->body->eadatasize);
373                 if (rc > 0) {
374                         LASSERT(rc >= sizeof(*md->lsm));
375                         rc = 0;
376                 }
377         } else if (S_ISDIR(md->body->mode)) {
378                 /* dir can be non-splitted */
379                 if (md->body->eadatasize == 0)
380                         RETURN(0);
381
382                 lmm = lustre_msg_buf(req->rq_repmsg, offset + 1,
383                                      md->body->eadatasize);
384                 if (!lmm)
385                         RETURN(-EINVAL);
386
387                 if (md->body->valid & OBD_MD_MEA) {
388                         LASSERT(exp_lmv != NULL);
389                 
390                         rc = obd_unpackmd(exp_lmv, (void *)&md->mea,
391                                           lmm, md->body->eadatasize);
392                         if (rc > 0) {
393                                 LASSERT(rc >= sizeof(*md->mea));
394                                 rc = 0;
395                         }
396                 }
397         } else {
398                 LASSERT(S_ISCHR(md->body->mode) ||
399                         S_ISBLK(md->body->mode) ||
400                         S_ISFIFO(md->body->mode)||
401                         S_ISLNK(md->body->mode) ||
402                         S_ISSOCK(md->body->mode));
403         }
404
405         acl_off = (md->body->valid & OBD_MD_FLEASIZE) ? (offset + 2) :
406                   (offset + 1);
407
408         if (md->body->valid & OBD_MD_FLACL_ACCESS) {
409                 size = le32_to_cpu(*(__u32 *) lustre_msg_buf(req->rq_repmsg, 
410                                    acl_off, 4));
411                 buf = lustre_msg_buf(req->rq_repmsg, acl_off + 1, size);
412
413                 acl = posix_acl_from_xattr(buf, size);
414                 if (IS_ERR(acl)) {
415                         rc = PTR_ERR(acl);
416                         CERROR("convert xattr to acl failed: %d\n", rc);
417                         RETURN(rc);
418                 } else if (acl) {
419                         rc = posix_acl_valid(acl);
420                         if (rc) {
421                                 CERROR("acl valid error: %d\n", rc);
422                                 posix_acl_release(acl);
423                                 RETURN(rc);
424                         }
425                 }
426
427                 md->acl_access = acl;
428         }
429         RETURN(rc);
430 }
431
432 static void mdc_commit_open(struct ptlrpc_request *req)
433 {
434         struct mdc_open_data *mod = req->rq_cb_data;
435         if (mod == NULL)
436                 return;
437
438         if (mod->mod_close_req != NULL)
439                 mod->mod_close_req->rq_cb_data = NULL;
440
441         if (mod->mod_och != NULL)
442                 mod->mod_och->och_mod = NULL;
443
444         OBD_FREE(mod, sizeof(*mod));
445         req->rq_cb_data = NULL;
446 }
447
448 static void mdc_replay_open(struct ptlrpc_request *req)
449 {
450         struct mdc_open_data *mod = req->rq_cb_data;
451         struct obd_client_handle *och;
452         struct ptlrpc_request *close_req;
453         struct lustre_handle old;
454         struct mds_body *body;
455         ENTRY;
456
457         body = lustre_swab_repbuf(req, 1, sizeof(*body), lustre_swab_mds_body);
458         LASSERT (body != NULL);
459
460         if (mod == NULL) {
461                 DEBUG_REQ(D_ERROR, req,
462                           "can't properly replay without open data");
463                 EXIT;
464                 return;
465         }
466
467         och = mod->mod_och;
468         if (och != NULL) {
469                 struct lustre_handle *file_fh;
470                 LASSERT(och->och_magic == OBD_CLIENT_HANDLE_MAGIC);
471                 file_fh = &och->och_fh;
472                 CDEBUG(D_HA, "updating handle from "LPX64" to "LPX64"\n",
473                        file_fh->cookie, body->handle.cookie);
474                 memcpy(&old, file_fh, sizeof(old));
475                 memcpy(file_fh, &body->handle, sizeof(*file_fh));
476         }
477
478         close_req = mod->mod_close_req;
479         if (close_req != NULL) {
480                 struct mds_body *close_body;
481                 LASSERT(close_req->rq_reqmsg->opc == MDS_CLOSE);
482                 close_body = lustre_msg_buf(close_req->rq_reqmsg,
483                                             MDS_REQ_REC_OFF,
484                                             sizeof(*close_body));
485                 if (och != NULL)
486                         LASSERT(!memcmp(&old, &close_body->handle, sizeof old));
487                 DEBUG_REQ(D_HA, close_req, "updating close body with new fh");
488                 memcpy(&close_body->handle, &body->handle,
489                        sizeof(close_body->handle));
490         }
491
492         EXIT;
493 }
494
495 int mdc_set_open_replay_data(struct obd_export *exp,
496                              struct obd_client_handle *och,
497                              struct ptlrpc_request *open_req)
498 {
499         struct mdc_open_data *mod;
500         struct mds_rec_create *rec;
501         struct mds_body *body;
502
503         rec = lustre_msg_buf(open_req->rq_reqmsg, MDS_REQ_INTENT_REC_OFF,
504                              sizeof(*rec));
505         body = lustre_msg_buf(open_req->rq_repmsg, 1, sizeof(*body));
506
507         LASSERT(rec != NULL);
508         /* outgoing messages always in my byte order */
509         LASSERT(body != NULL);
510         /* incoming message in my byte order (it's been swabbed) */
511         LASSERT_REPSWABBED(open_req, 1);
512
513         OBD_ALLOC(mod, sizeof(*mod));
514         if (mod == NULL) {
515                 DEBUG_REQ(D_ERROR, open_req, "can't allocate mdc_open_data");
516                 return 0;
517         }
518
519         och->och_mod = mod;
520         mod->mod_och = och;
521         mod->mod_open_req = open_req;
522
523         memcpy(&rec->cr_replayid, &body->id1, sizeof rec->cr_replayid);
524         open_req->rq_replay_cb = mdc_replay_open;
525         open_req->rq_commit_cb = mdc_commit_open;
526         open_req->rq_cb_data = mod;
527         DEBUG_REQ(D_HA, open_req, "set up replay data");
528         return 0;
529 }
530
531 int mdc_clear_open_replay_data(struct obd_export *exp,
532                                struct obd_client_handle *och)
533 {
534         struct mdc_open_data *mod = och->och_mod;
535
536         /* Don't free the structure now (it happens in mdc_commit_open, after
537          * we're sure we won't need to fix up the close request in the future),
538          * but make sure that replay doesn't poke at the och, which is about to
539          * be freed. */
540         LASSERT(mod != LP_POISON);
541         if (mod != NULL)
542                 mod->mod_och = NULL;
543         och->och_mod = NULL;
544         return 0;
545 }
546
547 static void mdc_commit_close(struct ptlrpc_request *req)
548 {
549         struct mdc_open_data *mod = req->rq_cb_data;
550         struct ptlrpc_request *open_req;
551         struct obd_import *imp = req->rq_import;
552
553         DEBUG_REQ(D_HA, req, "close req committed");
554         if (mod == NULL)
555                 return;
556
557         mod->mod_close_req = NULL;
558         req->rq_cb_data = NULL;
559         req->rq_commit_cb = NULL;
560
561         open_req = mod->mod_open_req;
562         LASSERT(open_req != NULL);
563         LASSERT(open_req != LP_POISON);
564         LASSERT(open_req->rq_type != LI_POISON);
565
566         DEBUG_REQ(D_HA, open_req, "open req balanced");
567         if (open_req->rq_transno == 0) {
568                 DEBUG_REQ(D_ERROR, open_req, "BUG 3892  open");
569                 DEBUG_REQ(D_ERROR, req, "BUG 3892 close");
570                 LASSERTF(open_req->rq_transno != 0, "BUG 3892");
571         }
572         LASSERT(open_req->rq_import == imp);
573
574         /* We no longer want to preserve this for transno-unconditional
575          * replay. */
576         spin_lock(&open_req->rq_lock);
577         open_req->rq_replay = 0;
578         spin_unlock(&open_req->rq_lock);
579 }
580
581 static int mdc_close_interpret(struct ptlrpc_request *req,
582                                void *data, int rc)
583 {
584         union ptlrpc_async_args *aa = data;
585         struct mdc_rpc_lock *close_lock;
586         struct obd_device *obd = aa->pointer_arg[1];
587         unsigned long flags;
588
589         spin_lock_irqsave(&req->rq_lock, flags);
590         close_lock = aa->pointer_arg[0];
591         aa->pointer_arg[0] = NULL;
592         spin_unlock_irqrestore (&req->rq_lock, flags);
593
594         if (close_lock == NULL) {
595                 CERROR("called with NULL close_lock\n");
596         } else {
597                 mdc_put_rpc_lock(close_lock, NULL);
598                 LASSERTF(close_lock == obd->u.cli.cl_close_lock, "%p != %p\n",
599                          close_lock, obd->u.cli.cl_close_lock);
600         }
601         wake_up(&req->rq_reply_waitq);
602         RETURN(rc);
603 }
604
605 /* We can't use ptlrpc_check_reply, because we don't want to wake up for
606  * anything but a reply or an error. */
607 static int mdc_close_check_reply(struct ptlrpc_request *req)
608 {
609         int rc = 0;
610         unsigned long flags;
611
612         spin_lock_irqsave(&req->rq_lock, flags);
613         if (req->rq_async_args.pointer_arg[0] == NULL)
614                 rc = 1;
615         spin_unlock_irqrestore (&req->rq_lock, flags);
616         return rc;
617 }
618
619 int mdc_close(struct obd_export *exp, struct obdo *oa,
620               struct obd_client_handle *och,
621               struct ptlrpc_request **request)
622 {
623         struct obd_device *obd = class_exp2obd(exp);
624         struct obd_import *imp = class_exp2cliimp(exp);
625         int reqsize[3] = {0, sizeof(struct mds_body),
626                           obd->u.cli.cl_max_mds_cookiesize};
627         int rc, repsize[3] = {sizeof(struct mds_body),
628                               obd->u.cli.cl_max_mds_easize,
629                               obd->u.cli.cl_max_mds_cookiesize};
630         struct ptlrpc_request *req;
631         struct mdc_open_data *mod;
632         struct l_wait_info lwi;
633         ENTRY;
634
635         if (imp->imp_connection == NULL) {
636                 CERROR("request on not connected import %s\n",
637                         imp->imp_obd->obd_name);
638                 RETURN(-EIO);
639         }
640
641         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
642                               MDS_CLOSE, 3, reqsize, NULL);
643         if (req == NULL)
644                 GOTO(out, rc = -ENOMEM);
645
646         //reqsize[0] = mdc_get_secdesc_size();
647         //mdc_pack_secdesc(req, reqsize[0]);
648
649         /* Ensure that this close's handle is fixed up during replay. */
650         LASSERT(och != NULL);
651         mod = och->och_mod;
652         if (likely(mod != NULL)) {
653                 mod->mod_close_req = req;
654                 LASSERT(mod->mod_open_req->rq_type != LI_POISON);
655                 DEBUG_REQ(D_HA, mod->mod_open_req, "matched open");
656         } else {
657                 CDEBUG(D_HA, "couldn't find open req; "
658                        "expecting close error\n");
659         }
660
661         mdc_close_pack(req, 1, oa, oa->o_valid, och);
662
663         req->rq_replen = lustre_msg_size(3, repsize);
664         req->rq_commit_cb = mdc_commit_close;
665         LASSERT(req->rq_cb_data == NULL);
666         req->rq_cb_data = mod;
667
668         /* We hand a ref to the rpcd here, so we need another one of our own. */
669         ptlrpc_request_addref(req);
670
671         mdc_get_rpc_lock(obd->u.cli.cl_close_lock, NULL);
672         req->rq_interpret_reply = mdc_close_interpret;
673         req->rq_async_args.pointer_arg[0] = obd->u.cli.cl_close_lock;
674         req->rq_async_args.pointer_arg[1] = obd;
675         ptlrpcd_add_req(req);
676
677         lwi = LWI_TIMEOUT_INTR(MAX(req->rq_timeout * HZ, 1), NULL, NULL, NULL);
678         rc = l_wait_event(req->rq_reply_waitq, mdc_close_check_reply(req),
679                           &lwi);
680         if (req->rq_repmsg == NULL) {
681                 CDEBUG(D_HA, "request failed to send: %p, %d\n", req,
682                        req->rq_status);
683                 if (rc == 0)
684                         rc = req->rq_status ? req->rq_status : -EIO;
685         } else if (rc == 0) {
686                 rc = req->rq_repmsg->status;
687                 if (req->rq_repmsg->type == PTL_RPC_MSG_ERR) {
688                         DEBUG_REQ(D_ERROR, req, "type == PTL_RPC_MSG_ERR, "
689                                   "err = %d", rc);
690                         if (rc > 0)
691                                 rc = -rc;
692                 } else {
693                         if (mod == NULL)
694                                 CERROR("Unexpected: can't find mdc_open_data, but "
695                                        "close succeeded. Please tell CFS.\n");
696                         if (!lustre_swab_repbuf(req, 0, sizeof(struct mds_body),
697                                                 lustre_swab_mds_body))
698                         {
699                                 CERROR("Error unpacking mds_body\n");
700                                 rc = -EPROTO;
701                         }
702                 }
703         }
704         if (req->rq_async_args.pointer_arg[0] != NULL) {
705                 CERROR("returned without dropping close lock: rc %d, "
706                        "dropping it now\n", rc);
707                 mdc_close_interpret(req, &req->rq_async_args, rc);
708         }
709
710         EXIT;
711  out:
712         *request = req;
713         return rc;
714 }
715
716 int mdc_done_writing(struct obd_export *exp, struct obdo *obdo)
717 {
718         struct ptlrpc_request *req;
719         struct mds_body *body;
720         int rc, size[2] = {0, sizeof(*body)};
721         ENTRY;
722
723         size[0] = mdc_get_secdesc_size();
724
725         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
726                               MDS_DONE_WRITING, 2, size, NULL);
727         if (req == NULL)
728                 RETURN(-ENOMEM);
729
730         mdc_pack_secdesc(req, size[0]);
731
732         body = lustre_msg_buf(req->rq_reqmsg, MDS_REQ_REC_OFF, 
733                               sizeof(*body));
734         
735         mdc_pack_id(&body->id1, obdo->o_id, 0, obdo->o_mode, 
736                     obdo->o_mds, obdo->o_fid);
737         
738         body->size = obdo->o_size;
739         body->blocks = obdo->o_blocks;
740         body->flags = obdo->o_flags;
741         body->valid = obdo->o_valid;
742
743         req->rq_replen = lustre_msg_size(1, &size[1]);
744
745         rc = ptlrpc_queue_wait(req);
746         ptlrpc_req_finished(req);
747         RETURN(rc);
748 }
749
750 int mdc_readpage(struct obd_export *exp,
751                  struct lustre_id *id,
752                  __u64 offset, struct page *page,
753                  struct ptlrpc_request **request)
754 {
755         struct obd_import *imp = class_exp2cliimp(exp);
756         struct ptlrpc_request *req = NULL;
757         struct ptlrpc_bulk_desc *desc = NULL;
758         struct mds_body *body;
759         int rc, size[2] = {0, sizeof(*body)};
760         ENTRY;
761
762         CDEBUG(D_INODE, "inode: %ld\n", (long)id->li_stc.u.e3s.l3s_ino);
763
764         size[0] = mdc_get_secdesc_size();
765
766         req = ptlrpc_prep_req(imp, LUSTRE_MDS_VERSION, MDS_READPAGE,
767                               2, size, NULL);
768         if (req == NULL)
769                 GOTO(out, rc = -ENOMEM);
770         /* XXX FIXME bug 249 */
771         req->rq_request_portal = MDS_READPAGE_PORTAL;
772
773         mdc_pack_secdesc(req, size[0]);
774
775         desc = ptlrpc_prep_bulk_imp(req, 1, BULK_PUT_SINK, MDS_BULK_PORTAL);
776         if (desc == NULL)
777                 GOTO(out, rc = -ENOMEM);
778         /* NB req now owns desc and will free it when it gets freed */
779
780         ptlrpc_prep_bulk_page(desc, page, 0, PAGE_CACHE_SIZE);
781         mdc_readdir_pack(req, 1, offset, PAGE_CACHE_SIZE, id);
782
783         req->rq_replen = lustre_msg_size(1, &size[1]);
784         rc = ptlrpc_queue_wait(req);
785
786         if (rc == 0) {
787                 body = lustre_swab_repbuf(req, 0, sizeof (*body),
788                                           lustre_swab_mds_body);
789                 if (body == NULL) {
790                         CERROR("Can't unpack mds_body\n");
791                         GOTO(out, rc = -EPROTO);
792                 }
793
794                 if (req->rq_bulk->bd_nob_transferred != PAGE_CACHE_SIZE) {
795                         CERROR ("Unexpected # bytes transferred: %d"
796                                 " (%ld expected)\n",
797                                 req->rq_bulk->bd_nob_transferred,
798                                 PAGE_CACHE_SIZE);
799                         GOTO (out, rc = -EPROTO);
800                 }
801         }
802
803         EXIT;
804  out:
805         *request = req;
806         return rc;
807 }
808
809 static int mdc_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
810                          void *karg, void *uarg)
811 {
812         struct obd_device *obd = exp->exp_obd;
813         struct obd_ioctl_data *data = karg;
814         struct obd_import *imp = obd->u.cli.cl_import;
815         struct llog_ctxt *ctxt;
816         int rc;
817         ENTRY;
818
819 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
820         MOD_INC_USE_COUNT;
821 #else
822         if (!try_module_get(THIS_MODULE)) {
823                 CERROR("Can't get module. Is it alive?");
824                 return -EINVAL;
825         }
826 #endif
827         switch (cmd) {
828         case OBD_IOC_CLIENT_RECOVER:
829                 rc = ptlrpc_recover_import(imp, data->ioc_inlbuf1);
830                 if (rc < 0)
831                         GOTO(out, rc);
832                 GOTO(out, rc = 0);
833         case IOC_OSC_SET_ACTIVE:
834                 rc = ptlrpc_set_import_active(imp, data->ioc_offset);
835                 GOTO(out, rc);
836         case OBD_IOC_PARSE: {
837                 ctxt = llog_get_context(&exp->exp_obd->obd_llogs,
838                                         LLOG_CONFIG_REPL_CTXT);
839                 rc = class_config_process_llog(ctxt, data->ioc_inlbuf1, NULL);
840                 GOTO(out, rc);
841         }
842 #ifdef __KERNEL__
843         case OBD_IOC_LLOG_INFO:
844         case OBD_IOC_LLOG_PRINT: {
845                 ctxt = llog_get_context(&obd->obd_llogs, LLOG_CONFIG_REPL_CTXT);
846                 rc = llog_ioctl(ctxt, cmd, data);
847
848                 GOTO(out, rc);
849         }
850 #endif
851         default:
852                 CERROR("mdc_ioctl(): unrecognised ioctl %#x\n", cmd);
853                 GOTO(out, rc = -ENOTTY);
854         }
855 out:
856 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
857         MOD_DEC_USE_COUNT;
858 #else
859         module_put(THIS_MODULE);
860 #endif
861
862         return rc;
863 }
864
865 int mdc_set_info(struct obd_export *exp, obd_count keylen,
866                  void *key, obd_count vallen, void *val)
867 {
868         int rc = -EINVAL;
869
870         if (keylen == strlen("initial_recov") &&
871             memcmp(key, "initial_recov", strlen("initial_recov")) == 0) {
872                 struct obd_import *imp = exp->exp_obd->u.cli.cl_import;
873                 if (vallen != sizeof(int))
874                         RETURN(-EINVAL);
875                 imp->imp_initial_recov = *(int *)val;
876                 CDEBUG(D_HA, "%s: set imp_no_init_recov = %d\n",
877                        exp->exp_obd->obd_name,
878                        imp->imp_initial_recov);
879                 RETURN(0);
880         } else if (keylen >= strlen("mds_type") && strcmp(key, "mds_type") == 0) {
881                 struct ptlrpc_request *req;
882                 char *bufs[2] = {key, val};
883                 int rc, size[2] = {keylen, vallen};
884
885                 req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_OBD_VERSION,
886                                       OST_SET_INFO, 2, size, bufs);
887                 if (req == NULL)
888                         RETURN(-ENOMEM);
889
890                 req->rq_replen = lustre_msg_size(0, NULL);
891                 rc = ptlrpc_queue_wait(req);
892                 ptlrpc_req_finished(req);
893                 RETURN(rc);
894         } else if (keylen >= strlen("inter_mds") && strcmp(key, "inter_mds") == 0) {
895                 struct obd_import *imp = class_exp2cliimp(exp);
896                 imp->imp_server_timeout = 1;
897                 CDEBUG(D_OTHER, "%s: timeout / 2\n", exp->exp_obd->obd_name);
898                 RETURN(0);
899         } else if (keylen == strlen("sec") && memcmp(key, "sec", keylen) == 0) {
900                 struct client_obd *cli = &exp->exp_obd->u.cli;
901
902                 if (vallen == strlen("null") &&
903                     memcmp(val, "null", vallen) == 0) {
904                         cli->cl_sec_flavor = PTLRPC_SEC_NULL;
905                         cli->cl_sec_subflavor = 0;
906                         RETURN(0);
907                 }
908                 if (vallen == strlen("krb5i") &&
909                     memcmp(val, "krb5i", vallen) == 0) {
910                         cli->cl_sec_flavor = PTLRPC_SEC_GSS;
911                         cli->cl_sec_subflavor = PTLRPC_SEC_GSS_KRB5I;
912                         RETURN(0);
913                 }
914                 if (vallen == strlen("krb5p") &&
915                     memcmp(val, "krb5p", vallen) == 0) {
916                         cli->cl_sec_flavor = PTLRPC_SEC_GSS;
917                         cli->cl_sec_subflavor = PTLRPC_SEC_GSS_KRB5P;
918                         RETURN(0);
919                 }
920                 CERROR("unrecognized security type %s\n", (char*) val);
921                 rc = -EINVAL;
922         } else if (keylen == strlen("nllu") && memcmp(key, "nllu", keylen) == 0) {
923                 struct client_obd *cli = &exp->exp_obd->u.cli;
924
925                 LASSERT(vallen == sizeof(__u32) * 2);
926                 cli->cl_nllu = ((__u32 *) val)[0];
927                 cli->cl_nllg = ((__u32 *) val)[1];
928                 RETURN(0);
929         } else if (keylen == strlen("async") && memcmp(key, "async", keylen) == 0) {
930                 struct client_obd *cl = &exp->exp_obd->u.cli;
931                 if (vallen != sizeof(int))
932                         RETURN(-EINVAL);
933                 cl->cl_async = *(int *)val;
934                 CDEBUG(D_HA, "%s: set async = %d\n",
935                        exp->exp_obd->obd_name, cl->cl_async);
936                 RETURN(0);
937         }
938
939         RETURN(rc);
940 }
941
942 static int mdc_statfs(struct obd_device *obd, struct obd_statfs *osfs,
943                       unsigned long max_age)
944 {
945         struct obd_statfs *msfs;
946         struct ptlrpc_request *req;
947         int rc, size = sizeof(*msfs);
948         ENTRY;
949
950         /* We could possibly pass max_age in the request (as an absolute
951          * timestamp or a "seconds.usec ago") so the target can avoid doing
952          * extra calls into the filesystem if that isn't necessary (e.g.
953          * during mount that would help a bit).  Having relative timestamps
954          * is not so great if request processing is slow, while absolute
955          * timestamps are not ideal because they need time synchronization. */
956         req = ptlrpc_prep_req(obd->u.cli.cl_import, LUSTRE_MDS_VERSION,
957                               MDS_STATFS, 0, NULL, NULL);
958         if (!req)
959                 RETURN(-ENOMEM);
960
961         req->rq_replen = lustre_msg_size(1, &size);
962
963         mdc_get_rpc_lock(obd->u.cli.cl_rpc_lock, NULL);
964         rc = ptlrpc_queue_wait(req);
965         mdc_put_rpc_lock(obd->u.cli.cl_rpc_lock, NULL);
966
967         if (rc) {
968                 /* this can be LMV fake import, whcih is not connected. */
969                 if (!req->rq_import->imp_connection)
970                         memset(osfs, 0, sizeof(*osfs));
971                 GOTO(out, rc);
972         }
973
974         msfs = lustre_swab_repbuf(req, 0, sizeof(*msfs),
975                                   lustre_swab_obd_statfs);
976         if (msfs == NULL) {
977                 CERROR("Can't unpack obd_statfs\n");
978                 GOTO(out, rc = -EPROTO);
979         }
980
981         memcpy(osfs, msfs, sizeof (*msfs));
982         EXIT;
983 out:
984         ptlrpc_req_finished(req);
985         return rc;
986 }
987
988 static int mdc_pin(struct obd_export *exp, obd_id ino, __u32 gen, int type,
989                    struct obd_client_handle *handle, int flag)
990 {
991         struct ptlrpc_request *req;
992         struct mds_body *body;
993         int rc, size[2] = {0, sizeof(*body)};
994         ENTRY;
995
996         //size[0] = mdc_get_secdesc_size();
997
998         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
999                               MDS_PIN, 2, size, NULL);
1000         if (req == NULL)
1001                 RETURN(-ENOMEM);
1002
1003         //mdc_pack_secdesc(req, size[0]);
1004
1005         body = lustre_msg_buf(req->rq_reqmsg, 
1006                               MDS_REQ_REC_OFF, sizeof(*body));
1007
1008         /* FIXME-UMKA: here should be also mdsnum and fid. */
1009         mdc_pack_id(&body->id1, ino, gen, type, 0, 0);
1010         body->flags = flag;
1011
1012         req->rq_replen = lustre_msg_size(1, &size[1]);
1013
1014         mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
1015         rc = ptlrpc_queue_wait(req);
1016         mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
1017         if (rc) {
1018                 CERROR("pin failed: %d\n", rc);
1019                 ptlrpc_req_finished(req);
1020                 RETURN(rc);
1021         }
1022
1023         body = lustre_swab_repbuf(req, 0, sizeof(*body), lustre_swab_mds_body);
1024         if (body == NULL) {
1025                 ptlrpc_req_finished(req);
1026                 RETURN(rc);
1027         }
1028
1029         memcpy(&handle->och_fh, &body->handle, sizeof(body->handle));
1030         handle->och_magic = OBD_CLIENT_HANDLE_MAGIC;
1031
1032         OBD_ALLOC(handle->och_mod, sizeof(*handle->och_mod));
1033         if (handle->och_mod == NULL) {
1034                 DEBUG_REQ(D_ERROR, req, "can't allocate mdc_open_data");
1035                 RETURN(-ENOMEM);
1036         }
1037         handle->och_mod->mod_open_req = req; /* will be dropped by unpin */
1038
1039         RETURN(rc);
1040 }
1041
1042 static int mdc_unpin(struct obd_export *exp,
1043                      struct obd_client_handle *handle, int flag)
1044 {
1045         struct ptlrpc_request *req;
1046         struct mds_body *body;
1047         int rc, size[2] = {0, sizeof(*body)};
1048         ENTRY;
1049
1050         if (handle->och_magic != OBD_CLIENT_HANDLE_MAGIC)
1051                 RETURN(0);
1052
1053         //size[0] = mdc_get_secdesc_size();
1054
1055         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
1056                               MDS_CLOSE, 2, size, NULL);
1057         if (req == NULL)
1058                 RETURN(-ENOMEM);
1059
1060         //mdc_pack_secdesc(req, size[0]);
1061
1062         body = lustre_msg_buf(req->rq_reqmsg, MDS_REQ_REC_OFF, sizeof(*body));
1063         memcpy(&body->handle, &handle->och_fh, sizeof(body->handle));
1064         body->flags = flag;
1065
1066         req->rq_replen = lustre_msg_size(0, NULL);
1067         mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
1068         rc = ptlrpc_queue_wait(req);
1069         mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
1070
1071         if (rc != 0)
1072                 CERROR("unpin failed: %d\n", rc);
1073
1074         ptlrpc_req_finished(req);
1075         ptlrpc_req_finished(handle->och_mod->mod_open_req);
1076         OBD_FREE(handle->och_mod, sizeof(*handle->och_mod));
1077         RETURN(rc);
1078 }
1079
1080 int mdc_sync(struct obd_export *exp, struct lustre_id *id,
1081              struct ptlrpc_request **request)
1082 {
1083         struct ptlrpc_request *req;
1084         struct mds_body *body;
1085         int size[2] = {0, sizeof(*body)};
1086         int rc;
1087         ENTRY;
1088
1089         //size[0] = mdc_get_secdesc_size();
1090
1091         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
1092                               MDS_SYNC, 2, size, NULL);
1093         if (!req)
1094                 RETURN(rc = -ENOMEM);
1095
1096         //mdc_pack_secdesc(req, size[0]);
1097
1098         if (id) {
1099                 body = lustre_msg_buf(req->rq_reqmsg, MDS_REQ_REC_OFF,
1100                                       sizeof (*body));
1101                 memcpy(&body->id1, id, sizeof(*id));
1102         }
1103
1104         req->rq_replen = lustre_msg_size(1, &size[1]);
1105
1106         rc = ptlrpc_queue_wait(req);
1107         if (rc || request == NULL)
1108                 ptlrpc_req_finished(req);
1109         else
1110                 *request = req;
1111
1112         RETURN(rc);
1113 }
1114
1115 static int mdc_import_event(struct obd_device *obd, struct obd_import *imp,
1116                             enum obd_import_event event)
1117 {
1118         int rc = 0;
1119
1120         LASSERT(imp->imp_obd == obd);
1121
1122         switch (event) {
1123         case IMP_EVENT_DISCON: {
1124                 break;
1125         }
1126         case IMP_EVENT_INACTIVE: {
1127                 if (obd->obd_observer)
1128                         rc = obd_notify(obd->obd_observer, obd, 0, 0);
1129                 break;
1130         }
1131         case IMP_EVENT_INVALIDATE: {
1132                 struct ldlm_namespace *ns = obd->obd_namespace;
1133
1134                 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
1135
1136                 break;
1137         }
1138         case IMP_EVENT_ACTIVE: {
1139                 if (obd->obd_observer)
1140                         rc = obd_notify(obd->obd_observer, obd, 1, 0);
1141                 break;
1142         }
1143         default:
1144                 CERROR("Unknown import event %d\n", event);
1145                 LBUG();
1146         }
1147         RETURN(rc);
1148 }
1149
1150 static int mdc_attach(struct obd_device *dev, obd_count len, void *data)
1151 {
1152         struct lprocfs_static_vars lvars;
1153
1154         lprocfs_init_vars(mdc, &lvars);
1155         return lprocfs_obd_attach(dev, lvars.obd_vars);
1156 }
1157
1158 static int mdc_detach(struct obd_device *dev)
1159 {
1160         return lprocfs_obd_detach(dev);
1161 }
1162
1163 static int mdc_setup(struct obd_device *obd, obd_count len, void *buf)
1164 {
1165         struct client_obd *cli = &obd->u.cli;
1166         int rc;
1167         ENTRY;
1168
1169         OBD_ALLOC(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
1170         if (!cli->cl_rpc_lock)
1171                 RETURN(-ENOMEM);
1172         mdc_init_rpc_lock(cli->cl_rpc_lock);
1173
1174         ptlrpcd_addref();
1175
1176         OBD_ALLOC(cli->cl_setattr_lock, sizeof (*cli->cl_setattr_lock));
1177         if (!cli->cl_setattr_lock)
1178                 GOTO(err_rpc_lock, rc = -ENOMEM);
1179         mdc_init_rpc_lock(cli->cl_setattr_lock);
1180
1181         OBD_ALLOC(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
1182         if (!cli->cl_close_lock)
1183                 GOTO(err_setattr_lock, rc = -ENOMEM);
1184         mdc_init_rpc_lock(cli->cl_close_lock);
1185
1186         rc = client_obd_setup(obd, len, buf);
1187         if (rc)
1188                 GOTO(err_close_lock, rc);
1189
1190         rc = obd_llog_init(obd, &obd->obd_llogs, obd, 0, NULL);
1191         if (rc) {
1192                 mdc_cleanup(obd, 0);
1193                 CERROR("failed to setup llogging subsystems\n");
1194         }
1195
1196         RETURN(rc);
1197
1198 err_close_lock:
1199         OBD_FREE(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
1200 err_setattr_lock:
1201         OBD_FREE(cli->cl_setattr_lock, sizeof (*cli->cl_setattr_lock));
1202 err_rpc_lock:
1203         OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
1204         ptlrpcd_decref();
1205         RETURN(rc);
1206 }
1207
1208 static int mdc_init_ea_size(struct obd_export *exp, int easize, int cookiesize)
1209 {
1210         struct obd_device *obd = exp->exp_obd;
1211         struct client_obd *cli = &obd->u.cli;
1212         ENTRY;
1213
1214         if (cli->cl_max_mds_easize < easize)
1215                 cli->cl_max_mds_easize = easize;
1216         if (cli->cl_max_mds_cookiesize < cookiesize)
1217                 cli->cl_max_mds_cookiesize = cookiesize;
1218         RETURN(0);
1219 }
1220
1221 static int mdc_precleanup(struct obd_device *obd, int flags)
1222 {
1223         int rc = 0;
1224         
1225         rc = obd_llog_finish(obd, &obd->obd_llogs, 0);
1226         if (rc != 0)
1227                 CERROR("failed to cleanup llogging subsystems\n");
1228
1229         RETURN(rc);
1230 }
1231
1232 static int mdc_cleanup(struct obd_device *obd, int flags)
1233 {
1234         struct client_obd *cli = &obd->u.cli;
1235
1236         OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
1237         OBD_FREE(cli->cl_setattr_lock, sizeof (*cli->cl_setattr_lock));
1238         OBD_FREE(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
1239
1240         ptlrpcd_decref();
1241
1242         return client_obd_cleanup(obd, flags);
1243 }
1244
1245
1246 static int mdc_llog_init(struct obd_device *obd, struct obd_llogs *llogs, 
1247                          struct obd_device *tgt, int count,
1248                          struct llog_catid *logid)
1249 {
1250         struct llog_ctxt *ctxt;
1251         int rc;
1252         ENTRY;
1253
1254         rc = obd_llog_setup(obd, llogs, LLOG_CONFIG_REPL_CTXT, tgt, 0, NULL,
1255                             &llog_client_ops);
1256         if (rc == 0) {
1257                 ctxt = llog_get_context(llogs, LLOG_CONFIG_REPL_CTXT);
1258                 ctxt->loc_imp = obd->u.cli.cl_import;
1259         }
1260
1261         RETURN(rc);
1262 }
1263
1264 static int mdc_llog_finish(struct obd_device *obd,
1265                            struct obd_llogs *llogs, int count)
1266 {
1267         int rc;
1268         ENTRY;
1269
1270         rc = obd_llog_cleanup(llog_get_context(llogs, LLOG_CONFIG_REPL_CTXT));
1271         RETURN(rc);
1272 }
1273 static struct obd_device *mdc_get_real_obd(struct obd_export *exp,
1274                                            struct lustre_id *id)
1275 {
1276        ENTRY;
1277        RETURN(exp->exp_obd);
1278 }
1279
1280 static int mdc_get_info(struct obd_export *exp, obd_count keylen,
1281                         void *key, __u32 *valsize, void *val)
1282 {
1283         struct ptlrpc_request *req;
1284         char *bufs[1] = {key};
1285         int rc = 0;
1286         ENTRY;
1287         
1288         if (!valsize || !val)
1289                 RETURN(-EFAULT);
1290
1291         if ((keylen < strlen("mdsize") || strcmp(key, "mdsize") != 0) &&
1292             (keylen < strlen("mdsnum") || strcmp(key, "mdsnum") != 0) &&
1293             (keylen < strlen("rootid") || strcmp(key, "rootid") != 0))
1294                 RETURN(-EPROTO);
1295                 
1296         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_OBD_VERSION,
1297                               OST_GET_INFO, 1, &keylen, bufs);
1298         if (req == NULL)
1299                 RETURN(-ENOMEM);
1300
1301         req->rq_replen = lustre_msg_size(1, valsize);
1302         rc = ptlrpc_queue_wait(req);
1303         if (rc)
1304                 GOTO(out_req, rc);
1305
1306         if (keylen >= strlen("rootid") && !strcmp(key, "rootid")) {
1307                 struct lustre_id *reply;
1308                 
1309                 reply = lustre_swab_repbuf(req, 0, sizeof(*reply),
1310                                            lustre_swab_lustre_id);
1311                 if (reply == NULL) {
1312                         CERROR("Can't unpack %s\n", (char *)key);
1313                         GOTO(out_req, rc = -EPROTO);
1314                 }
1315
1316                 *(struct lustre_id *)val = *reply;
1317         } else {
1318                 __u32 *reply;
1319                 
1320                 reply = lustre_swab_repbuf(req, 0, sizeof(*reply),
1321                                            lustre_swab_generic_32s);
1322                 if (reply == NULL) {
1323                         CERROR("Can't unpack %s\n", (char *)key);
1324                         GOTO(out_req, rc = -EPROTO);
1325                 }
1326                 *((__u32 *)val) = *reply;
1327         }
1328 out_req:
1329         ptlrpc_req_finished(req);
1330         RETURN(rc);
1331 }
1332
1333 int mdc_obj_create(struct obd_export *exp, struct obdo *oa,
1334                     struct lov_stripe_md **ea, struct obd_trans_info *oti)
1335 {
1336         struct ptlrpc_request *request;
1337         struct ost_body *body;
1338         int rc, size = sizeof(*body);
1339         ENTRY;
1340
1341         LASSERT(oa);
1342
1343         request = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_OBD_VERSION,
1344                                   OST_CREATE, 1, &size, NULL);
1345         if (!request)
1346                 GOTO(out_req, rc = -ENOMEM);
1347
1348         body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
1349         memcpy(&body->oa, oa, sizeof(body->oa));
1350
1351         request->rq_replen = lustre_msg_size(1, &size);
1352         rc = ptlrpc_queue_wait(request);
1353         if (rc)
1354                 GOTO(out_req, rc);
1355
1356         body = lustre_swab_repbuf(request, 0, sizeof(*body),
1357                                   lustre_swab_ost_body);
1358         if (body == NULL) {
1359                 CERROR ("can't unpack ost_body\n");
1360                 GOTO (out_req, rc = -EPROTO);
1361         }
1362
1363         memcpy(oa, &body->oa, sizeof(*oa));
1364
1365         /* store ino/generation for recovery */
1366         body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
1367         body->oa.o_id = oa->o_id;
1368         body->oa.o_generation = oa->o_generation;
1369         body->oa.o_fid = oa->o_fid;
1370         body->oa.o_mds = oa->o_mds;
1371
1372         CDEBUG(D_HA, "transno: "LPD64"\n", request->rq_repmsg->transno);
1373         EXIT;
1374 out_req:
1375         ptlrpc_req_finished(request);
1376         return rc;
1377 }
1378
1379 int mdc_brw(int rw, struct obd_export *exp, struct obdo *oa,
1380             struct lov_stripe_md *ea, obd_count oa_bufs,
1381             struct brw_page *pgarr, struct obd_trans_info *oti)
1382 {
1383         struct ptlrpc_bulk_desc *desc;
1384         struct niobuf_remote *niobuf;
1385         struct ptlrpc_request *req;
1386         struct obd_ioobj *ioobj;
1387         struct ost_body *body;
1388         int err, opc, i;
1389         int size[3];
1390
1391         opc = ((rw & OBD_BRW_WRITE) != 0) ? OST_WRITE : OST_READ;
1392         
1393         size[0] = sizeof(*body);
1394         size[1] = sizeof(*ioobj);
1395         size[2] = oa_bufs * sizeof(*niobuf);
1396
1397         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_OBD_VERSION, opc,
1398                               3, size, NULL);
1399         LASSERT(req != NULL);
1400
1401         if (opc == OST_WRITE)
1402                 desc = ptlrpc_prep_bulk_imp(req, oa_bufs, BULK_GET_SOURCE,
1403                                             OST_BULK_PORTAL);
1404         else
1405                 desc = ptlrpc_prep_bulk_imp(req, oa_bufs, BULK_PUT_SINK,
1406                                             OST_BULK_PORTAL);
1407         LASSERT(desc != NULL);
1408
1409         body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof(*body));
1410         ioobj = lustre_msg_buf(req->rq_reqmsg, 1, sizeof(*ioobj));
1411         niobuf = lustre_msg_buf(req->rq_reqmsg, 2, oa_bufs * sizeof(*niobuf));
1412
1413         memcpy(&body->oa, oa, sizeof(*oa));
1414         obdo_to_ioobj(oa, ioobj);
1415         ioobj->ioo_bufcnt = oa_bufs;
1416
1417         for (i = 0; i < oa_bufs; i++, niobuf++) {
1418                 struct brw_page *pg = &pgarr[i];
1419
1420                 LASSERT(pg->count > 0);
1421                 LASSERT((pg->disk_offset & ~PAGE_MASK) + pg->count <= PAGE_SIZE);
1422
1423                 ptlrpc_prep_bulk_page(desc, pg->pg, pg->disk_offset & ~PAGE_MASK,
1424                                       pg->count);
1425
1426                 niobuf->offset = pg->disk_offset;
1427                 niobuf->len = pg->count;
1428                 niobuf->flags = pg->flag;
1429         }
1430
1431         /* size[0] still sizeof (*body) */
1432         if (opc == OST_WRITE) {
1433                 /* 1 RC per niobuf */
1434                 size[1] = sizeof(__u32) * oa_bufs;
1435                 req->rq_replen = lustre_msg_size(2, size);
1436         } else {
1437                 /* 1 RC for the whole I/O */
1438                 req->rq_replen = lustre_msg_size(1, size);
1439         }
1440         err = ptlrpc_queue_wait(req);
1441         LASSERT(err == 0);
1442
1443         ptlrpc_req_finished(req);
1444         return 0;
1445 }
1446
1447 static int mdc_valid_attrs(struct obd_export *exp,
1448                            struct lustre_id *id)
1449 {
1450         struct ldlm_res_id res_id = { .name = {0} };
1451         struct obd_device *obd = exp->exp_obd;
1452         struct lustre_handle lockh;
1453         ldlm_policy_data_t policy;
1454         int flags;
1455         ENTRY;
1456
1457         res_id.name[0] = id_fid(id);
1458         res_id.name[1] = id_group(id);
1459         policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
1460
1461         CDEBUG(D_INFO, "trying to match res "LPU64"\n",
1462                res_id.name[0]);
1463
1464         /* FIXME use LDLM_FL_TEST_LOCK instead */
1465         flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_CBPENDING;
1466         if (ldlm_lock_match(obd->obd_namespace, flags, &res_id,
1467                             LDLM_IBITS, &policy, LCK_PR, &lockh)) {
1468                 ldlm_lock_decref(&lockh, LCK_PR);
1469                 RETURN(1);
1470         }
1471
1472         if (ldlm_lock_match(obd->obd_namespace, flags, &res_id,
1473                             LDLM_IBITS, &policy, LCK_PW, &lockh)) {
1474                 ldlm_lock_decref(&lockh, LCK_PW);
1475                 RETURN(1);
1476         }
1477         RETURN(0);
1478 }
1479
1480 static int mdc_change_cbdata_name(struct obd_export *exp,
1481                                   struct lustre_id *pid,
1482                                   char *name, int len,
1483                                   struct lustre_id *cid,
1484                                   ldlm_iterator_t it, void *data)
1485 {
1486         int rc;
1487         rc = mdc_change_cbdata(exp, cid, it, data);
1488         RETURN(rc);
1489 }
1490
1491 struct obd_ops mdc_obd_ops = {
1492         .o_owner         = THIS_MODULE,
1493         .o_attach        = mdc_attach,
1494         .o_detach        = mdc_detach,
1495         .o_setup         = mdc_setup,
1496         .o_precleanup    = mdc_precleanup,
1497         .o_cleanup       = mdc_cleanup,
1498         .o_add_conn      = client_import_add_conn,
1499         .o_del_conn      = client_import_del_conn,
1500         .o_connect       = client_connect_import,
1501         .o_disconnect    = client_disconnect_export,
1502         .o_iocontrol     = mdc_iocontrol,
1503         .o_packmd        = mdc_packmd,
1504         .o_unpackmd      = mdc_unpackmd,
1505         .o_statfs        = mdc_statfs,
1506         .o_pin           = mdc_pin,
1507         .o_unpin         = mdc_unpin,
1508         .o_import_event  = mdc_import_event,
1509         .o_llog_init     = mdc_llog_init,
1510         .o_llog_finish   = mdc_llog_finish,
1511         .o_create        = mdc_obj_create,
1512         .o_set_info      = mdc_set_info,
1513         .o_get_info      = mdc_get_info,
1514         .o_brw           = mdc_brw,
1515         .o_init_ea_size  = mdc_init_ea_size,
1516 };
1517
1518 struct md_ops mdc_md_ops = {
1519         .m_getstatus     = mdc_getstatus,
1520         .m_getattr       = mdc_getattr,
1521         .m_close         = mdc_close,
1522         .m_create        = mdc_create,
1523         .m_done_writing  = mdc_done_writing,
1524         .m_enqueue       = mdc_enqueue,
1525         .m_getattr_lock  = mdc_getattr_lock,
1526         .m_intent_lock   = mdc_intent_lock,
1527         .m_link          = mdc_link,
1528         .m_rename        = mdc_rename,
1529         .m_setattr       = mdc_setattr,
1530         .m_sync          = mdc_sync,
1531         .m_readpage      = mdc_readpage,
1532         .m_unlink        = mdc_unlink,
1533         .m_valid_attrs   = mdc_valid_attrs,
1534         .m_req2lustre_md = mdc_req2lustre_md,
1535         .m_set_open_replay_data   = mdc_set_open_replay_data,
1536         .m_clear_open_replay_data = mdc_clear_open_replay_data,
1537         .m_store_inode_generation = mdc_store_inode_generation,
1538         .m_set_lock_data = mdc_set_lock_data,
1539         .m_get_real_obd  = mdc_get_real_obd,
1540         .m_change_cbdata_name = mdc_change_cbdata_name,
1541         .m_change_cbdata = mdc_change_cbdata,
1542 };
1543
1544 int __init mdc_init(void)
1545 {
1546         struct lprocfs_static_vars lvars;
1547         lprocfs_init_vars(mdc, &lvars);
1548         return class_register_type(&mdc_obd_ops, &mdc_md_ops, lvars.module_vars,
1549                                    LUSTRE_MDC_NAME);
1550 }
1551
1552 #ifdef __KERNEL__
1553 static void /*__exit*/ mdc_exit(void)
1554 {
1555         class_unregister_type(LUSTRE_MDC_NAME);
1556 }
1557
1558 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
1559 MODULE_DESCRIPTION("Lustre Metadata Client");
1560 MODULE_LICENSE("GPL");
1561
1562 EXPORT_SYMBOL(mdc_req2lustre_md);
1563 EXPORT_SYMBOL(mdc_change_cbdata);
1564 EXPORT_SYMBOL(mdc_getstatus);
1565 EXPORT_SYMBOL(mdc_getattr);
1566 EXPORT_SYMBOL(mdc_getattr_lock);
1567 EXPORT_SYMBOL(mdc_create);
1568 EXPORT_SYMBOL(mdc_unlink);
1569 EXPORT_SYMBOL(mdc_rename);
1570 EXPORT_SYMBOL(mdc_link);
1571 EXPORT_SYMBOL(mdc_readpage);
1572 EXPORT_SYMBOL(mdc_setattr);
1573 EXPORT_SYMBOL(mdc_close);
1574 EXPORT_SYMBOL(mdc_done_writing);
1575 EXPORT_SYMBOL(mdc_sync);
1576 EXPORT_SYMBOL(mdc_set_open_replay_data);
1577 EXPORT_SYMBOL(mdc_clear_open_replay_data);
1578 EXPORT_SYMBOL(mdc_store_inode_generation);
1579
1580 module_init(mdc_init);
1581 module_exit(mdc_exit);
1582 #endif