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