Whamcloud - gitweb
- merge 0.7rc1 from b_devel to HEAD (20030612 merge point)
[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-2003 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 #define EXPORT_SYMTAB
23 #define DEBUG_SUBSYSTEM S_MDC
24
25 #ifdef __KERNEL__
26 # include <linux/module.h>
27 # include <linux/pagemap.h>
28 # include <linux/miscdevice.h>
29 # include <linux/init.h>
30 #else
31 # include <liblustre.h>
32 # include <linux/obd_class.h>
33 #endif
34
35 #include <linux/lustre_mds.h>
36 #include <linux/lustre_lite.h>
37 #include <linux/lustre_dlm.h>
38 #include <linux/lprocfs_status.h>
39 #include "mdc_internal.h"
40
41 #define REQUEST_MINOR 244
42
43 extern int mds_queue_req(struct ptlrpc_request *);
44 struct mdc_rpc_lock mdc_rpc_lock;
45 struct mdc_rpc_lock mdc_setattr_lock;
46 EXPORT_SYMBOL(mdc_rpc_lock);
47
48 /* Helper that implements most of mdc_getstatus and signal_completed_replay. */
49 static int send_getstatus(struct obd_import *imp, struct ll_fid *rootfid,
50                           int level, int msg_flags)
51 {
52         struct ptlrpc_request *req;
53         struct mds_body *body;
54         int rc, size = sizeof(*body);
55         ENTRY;
56
57         req = ptlrpc_prep_req(imp, MDS_GETSTATUS, 1, &size, NULL);
58         if (!req)
59                 GOTO(out, rc = -ENOMEM);
60
61         body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*body));
62         req->rq_level = level;
63         req->rq_replen = lustre_msg_size(1, &size);
64
65         mds_pack_req_body(req);
66         req->rq_reqmsg->flags |= msg_flags;
67         rc = ptlrpc_queue_wait(req);
68
69         if (!rc) {
70                 body = lustre_swab_repbuf (req, 0, sizeof (*body),
71                                            lustre_swab_mds_body);
72                 if (body == NULL) {
73                         CERROR ("Can't extract mds_body\n");
74                         GOTO (out, rc = -EPROTO);
75                 }
76
77                 memcpy(rootfid, &body->fid1, sizeof(*rootfid));
78
79                 CDEBUG(D_NET, "root ino="LPU64", last_committed="LPU64
80                        ", last_xid="LPU64"\n",
81                        rootfid->id, req->rq_repmsg->last_committed,
82                        req->rq_repmsg->last_xid);
83         }
84
85         EXIT;
86  out:
87         ptlrpc_req_finished(req);
88         return rc;
89 }
90
91 /* should become mdc_getinfo() */
92 int mdc_getstatus(struct lustre_handle *conn, struct ll_fid *rootfid)
93 {
94         return send_getstatus(class_conn2cliimp(conn), rootfid, LUSTRE_CONN_CON,
95                               0);
96 }
97
98 int mdc_getlovinfo(struct obd_device *obd, struct lustre_handle *mdc_connh,
99                    struct ptlrpc_request **request)
100 {
101         struct ptlrpc_request *req;
102         struct mds_status_req *streq;
103         struct lov_desc       *desc;
104         struct obd_uuid       *uuids;
105         int rc, size[2] = {sizeof(*streq)};
106         int i;
107         ENTRY;
108
109         req = ptlrpc_prep_req(class_conn2cliimp(mdc_connh), MDS_GETLOVINFO, 1,
110                               size, NULL);
111         if (!req)
112                 RETURN (-ENOMEM);
113
114         *request = req;
115         streq = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*streq));
116         streq->flags = MDS_STATUS_LOV;
117         streq->repbuf = LOV_MAX_UUID_BUFFER_SIZE;
118
119         /* prepare for reply */
120         req->rq_level = LUSTRE_CONN_CON;
121         size[0] = sizeof (*desc);
122         size[1] = LOV_MAX_UUID_BUFFER_SIZE;
123         req->rq_replen = lustre_msg_size(2, size);
124
125         mdc_get_rpc_lock(&mdc_rpc_lock, NULL);
126         rc = ptlrpc_queue_wait(req);
127         mdc_put_rpc_lock(&mdc_rpc_lock, NULL);
128
129         if (rc != 0) {
130                 CERROR ("rcp failed\n");
131                 GOTO (failed, rc);
132         }
133         
134         desc = lustre_swab_repbuf (req, 0, sizeof (*desc),
135                                    lustre_swab_lov_desc);
136         if (desc == NULL) {
137                 CERROR ("Can't unpack lov_desc\n");
138                 GOTO (failed, rc = -EPROTO);
139         }
140         
141         LASSERT_REPSWAB (req, 1);
142         /* array of uuids byte-sex insensitive; just verify they are all
143          * there and terminated */
144         uuids = lustre_msg_buf (req->rq_repmsg, 1,
145                                 desc->ld_tgt_count * sizeof (*uuids));
146         if (uuids == NULL) {
147                 CERROR ("Can't unpack %d uuids\n", desc->ld_tgt_count);
148                 GOTO (failed, rc = -EPROTO);
149         }
150
151         for (i = 0; i < desc->ld_tgt_count; i++) {
152                 int uid_len = strnlen (uuids[i].uuid, sizeof (uuids[i].uuid));
153                 
154                 if (uid_len == sizeof (uuids[i].uuid)) {
155                         CERROR ("Unterminated uuid %d:%*s\n",
156                                 i, (int)sizeof (uuids[i].uuid), uuids[i].uuid);
157                         GOTO (failed, rc = -EPROTO);
158                 }
159         }
160         RETURN(0);
161
162  failed:
163         ptlrpc_req_finished (req);
164         RETURN (rc);
165 }
166
167 int mdc_getattr_common (struct lustre_handle *conn,
168                         unsigned int ea_size, struct ptlrpc_request *req)
169 {
170         struct mds_body *body;
171         void            *eadata;
172         int              rc; 
173         int              size[2] = {sizeof(*body), 0};
174         int              bufcount = 1;
175         ENTRY;
176
177         /* request message already built */
178
179         if (ea_size != 0) {
180                 size[bufcount++] = ea_size;
181                 CDEBUG(D_INODE, "reserved %u bytes for MD/symlink in packet\n",
182                        ea_size);
183         }
184         req->rq_replen = lustre_msg_size(bufcount, size);
185
186         mdc_get_rpc_lock(&mdc_rpc_lock, NULL);
187         rc = ptlrpc_queue_wait(req);
188         mdc_put_rpc_lock(&mdc_rpc_lock, NULL);
189         if (rc != 0)
190                 RETURN (rc);
191         
192         body = lustre_swab_repbuf (req, 0, sizeof (*body),
193                                    lustre_swab_mds_body);
194         if (body == NULL) {
195                 CERROR ("Can't unpack mds_body\n");
196                 RETURN (-EPROTO);
197         }
198
199         CDEBUG(D_NET, "mode: %o\n", body->mode);
200
201         LASSERT_REPSWAB (req, 1);
202         if (body->eadatasize != 0) {
203                 /* reply indicates presence of eadata; check it's there... */
204                 eadata = lustre_msg_buf (req->rq_repmsg, 1, body->eadatasize);
205                 if (eadata == NULL) {
206                         CERROR ("Missing/short eadata\n");
207                         RETURN (-EPROTO);
208                 }
209         }
210
211         RETURN (0);
212 }
213                         
214 int mdc_getattr(struct lustre_handle *conn, struct ll_fid *fid,
215                 unsigned long valid, unsigned int ea_size,
216                 struct ptlrpc_request **request)
217 {
218         struct ptlrpc_request *req;
219         struct mds_body *body;
220         int size = sizeof(*body);
221         int rc;
222         ENTRY;
223
224         /* XXX do we need to make another request here?  We just did a getattr
225          *     to do the lookup in the first place.
226          */
227         req = ptlrpc_prep_req(class_conn2cliimp(conn), MDS_GETATTR, 1, &size,
228                               NULL);
229         if (!req)
230                 GOTO(out, rc = -ENOMEM);
231
232         body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*body));
233         memcpy(&body->fid1, fid, sizeof(*fid));
234         body->valid = valid;
235         body->eadatasize = ea_size;
236         mds_pack_req_body(req);
237
238         rc = mdc_getattr_common (conn, ea_size, req);
239         if (rc != 0) {
240                 ptlrpc_req_finished (req);
241                 req = NULL;
242         }
243  out:
244         *request = req;
245         RETURN (rc);
246 }
247
248 int mdc_getattr_name(struct lustre_handle *conn, struct ll_fid *fid,
249                      char *filename, int namelen, unsigned long valid,
250                      unsigned int ea_size, struct ptlrpc_request **request)
251 {
252         struct ptlrpc_request *req;
253         struct mds_body *body;
254         int rc, size[2] = {sizeof(*body), namelen};
255         ENTRY;
256
257         req = ptlrpc_prep_req(class_conn2cliimp(conn), MDS_GETATTR_NAME, 2,
258                               size, NULL);
259         if (!req)
260                 GOTO(out, rc = -ENOMEM);
261
262         body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*body));
263         memcpy(&body->fid1, fid, sizeof(*fid));
264         body->valid = valid;
265         body->eadatasize = ea_size;
266         mds_pack_req_body(req);
267
268         LASSERT (strnlen (filename, namelen) == namelen - 1);
269         memcpy(lustre_msg_buf(req->rq_reqmsg, 1, namelen), filename, namelen);
270
271         rc = mdc_getattr_common (conn, ea_size, req);
272         if (rc != 0) {
273                 ptlrpc_req_finished (req);
274                 req = NULL;
275         }
276  out:
277         *request = req;
278         return rc;
279 }
280
281 /* This should be called with both the request and the reply still packed. */
282 void mdc_store_inode_generation(struct ptlrpc_request *req, int reqoff,
283                                 int repoff)
284 {
285         struct mds_rec_create *rec =
286                 lustre_msg_buf(req->rq_reqmsg, reqoff, sizeof (*rec));
287         struct mds_body *body =
288                 lustre_msg_buf(req->rq_repmsg, repoff, sizeof (*body));
289
290         LASSERT (rec != NULL);
291         LASSERT (body != NULL);
292         
293         memcpy(&rec->cr_replayfid, &body->fid1, sizeof rec->cr_replayfid);
294         DEBUG_REQ(D_HA, req, "storing generation %x for ino "LPD64,
295                   rec->cr_replayfid.generation, rec->cr_replayfid.id);
296 }
297
298 /* We always reserve enough space in the reply packet for a stripe MD, because
299  * we don't know in advance the file type.
300  *
301  * XXX we could get that from ext2_dir_entry_2 file_type
302  */
303 int mdc_enqueue(struct lustre_handle *conn,
304                 int lock_type,
305                 struct lookup_intent *it,
306                 int lock_mode,
307                 struct mdc_op_data *data,
308                 struct lustre_handle *lockh,
309                 char *tgt,
310                 int tgtlen,
311                 ldlm_completion_callback cb_completion,
312                 ldlm_blocking_callback cb_blocking,
313                 void *cb_data)
314 {
315         struct ptlrpc_request *req;
316         struct obd_device *obddev = class_conn2obd(conn);
317         struct ldlm_res_id res_id =
318                 { .name = {data->ino1, data->gen1} };
319         int size[6] = {sizeof(struct ldlm_request), sizeof(struct ldlm_intent)};
320         int rc, flags = LDLM_FL_HAS_INTENT;
321         int repsize[3] = {sizeof(struct ldlm_reply),
322                           sizeof(struct mds_body),
323                           obddev->u.cli.cl_max_mds_easize};
324         struct ldlm_reply *dlm_rep;
325         struct ldlm_intent *lit;
326         struct ldlm_request *lockreq;
327         void *eadata;
328         unsigned long irqflags;
329         int   reply_buffers = 0;
330         ENTRY;
331
332 //        LDLM_DEBUG_NOLOCK("mdsintent=%s,name=%s,dir=%lu",
333 //                          ldlm_it2str(it->it_op), it_name, it_inode->i_ino);
334
335         if (it->it_op & IT_OPEN) {
336                 it->it_mode |= S_IFREG;
337                 it->it_mode &= ~current->fs->umask;
338
339                 size[2] = sizeof(struct mds_rec_create);
340                 size[3] = data->namelen + 1;
341                 req = ptlrpc_prep_req(class_conn2cliimp(conn), LDLM_ENQUEUE, 4,
342                                       size, NULL);
343                 if (!req)
344                         RETURN(-ENOMEM);
345
346                 spin_lock_irqsave (&req->rq_lock, irqflags);
347                 req->rq_replay = 1;
348                 spin_unlock_irqrestore (&req->rq_lock, irqflags);
349
350                 /* pack the intent */
351                 lit = lustre_msg_buf(req->rq_reqmsg, 1, sizeof (*lit));
352                 lit->opc = (__u64)it->it_op;
353
354                 /* pack the intended request */
355                 mds_open_pack(req, 2, data, it->it_mode, 0,
356                               current->fsuid, current->fsgid,
357                               LTIME_S(CURRENT_TIME), it->it_flags,
358                               tgt, tgtlen);
359                 /* get ready for the reply */
360                 reply_buffers = 3;
361                 req->rq_replen = lustre_msg_size(3, repsize);
362         } else if (it->it_op & IT_UNLINK) {
363                 size[2] = sizeof(struct mds_rec_unlink);
364                 size[3] = data->namelen + 1;
365                 req = ptlrpc_prep_req(class_conn2cliimp(conn), LDLM_ENQUEUE, 4,
366                                       size, NULL);
367                 if (!req)
368                         RETURN(-ENOMEM);
369
370                 /* pack the intent */
371                 lit = lustre_msg_buf(req->rq_reqmsg, 1, sizeof (*lit));
372                 lit->opc = (__u64)it->it_op;
373
374                 /* pack the intended request */
375                 mds_unlink_pack(req, 2, data);
376                 /* get ready for the reply */
377                 reply_buffers = 3;
378                 req->rq_replen = lustre_msg_size(3, repsize);
379         } else if (it->it_op & (IT_GETATTR | IT_LOOKUP)) {
380                 int valid = OBD_MD_FLNOTOBD | OBD_MD_FLEASIZE;
381                 size[2] = sizeof(struct mds_body);
382                 size[3] = data->namelen + 1;
383
384                 req = ptlrpc_prep_req(class_conn2cliimp(conn), LDLM_ENQUEUE, 4,
385                                       size, NULL);
386                 if (!req)
387                         RETURN(-ENOMEM);
388
389                 /* pack the intent */
390                 lit = lustre_msg_buf(req->rq_reqmsg, 1, sizeof (*lit));
391                 lit->opc = (__u64)it->it_op;
392
393                 /* pack the intended request */
394                 mds_getattr_pack(req, valid, 2, it->it_flags, data);
395                 /* get ready for the reply */
396                 reply_buffers = 3;
397                 req->rq_replen = lustre_msg_size(3, repsize);
398         } else if (it->it_op == IT_READDIR) {
399                 req = ptlrpc_prep_req(class_conn2cliimp(conn), LDLM_ENQUEUE, 1,
400                                       size, NULL);
401                 if (!req)
402                         RETURN(-ENOMEM);
403
404                 /* get ready for the reply */
405                 reply_buffers = 1;
406                 req->rq_replen = lustre_msg_size(1, repsize);
407         }  else {
408                 LBUG();
409                 RETURN(-EINVAL);
410         }
411
412         mdc_get_rpc_lock(&mdc_rpc_lock, it);
413         rc = ldlm_cli_enqueue(conn, req, obddev->obd_namespace, NULL, res_id,
414                               lock_type, NULL, 0, lock_mode, &flags,
415                               cb_completion, cb_blocking, cb_data, lockh);
416         mdc_put_rpc_lock(&mdc_rpc_lock, it);
417
418         /* Similarly, if we're going to replay this request, we don't want to
419          * actually get a lock, just perform the intent. */
420         if (req->rq_transno || req->rq_replay) {
421                 lockreq = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*lockreq));
422                 lockreq->lock_flags |= LDLM_FL_INTENT_ONLY;
423         }
424
425         /* This can go when we're sure that this can never happen */
426         LASSERT(rc != -ENOENT);
427         if (rc == ELDLM_LOCK_ABORTED) {
428                 lock_mode = 0;
429                 memset(lockh, 0, sizeof(*lockh));
430         } else if (rc != 0) {
431                 CERROR("ldlm_cli_enqueue: %d\n", rc);
432                 LASSERT (rc < 0);
433                 ptlrpc_req_finished(req);
434                 RETURN(rc);
435         } else { /* rc = 0 */
436                 struct ldlm_lock *lock = ldlm_handle2lock(lockh);
437                 struct lustre_handle lockh2;
438                 LASSERT(lock);
439
440                 /* If the server gave us back a different lock mode, we should
441                  * fix up our variables. */
442                 if (lock->l_req_mode != lock_mode) {
443                         ldlm_lock_addref(lockh, lock->l_req_mode);
444                         ldlm_lock_decref(lockh, lock_mode);
445                         lock_mode = lock->l_req_mode;
446                 }
447
448                 /* The server almost certainly gave us a lock other than the
449                  * one that we asked for.  If we already have a matching lock,
450                  * then cancel this one--we don't need two. */
451                 LDLM_DEBUG(lock, "matching against this");
452
453                 memcpy(&lockh2, lockh, sizeof(lockh2));
454                 if (ldlm_lock_match(NULL, LDLM_FL_BLOCK_GRANTED, NULL,
455                                     LDLM_PLAIN, NULL, 0, LCK_NL, &lockh2)) {
456                         /* We already have a lock; cancel the new one */
457                         ldlm_lock_decref_and_cancel(lockh, lock_mode);
458                         memcpy(lockh, &lockh2, sizeof(lockh2));
459                 }
460                 LDLM_LOCK_PUT(lock);
461         }
462
463         dlm_rep = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*dlm_rep));
464         LASSERT (dlm_rep != NULL);           /* checked by ldlm_cli_enqueue() */
465         LASSERT_REPSWABBED (req, 0);         /* swabbed by ldlm_cli_enqueue() */
466         
467         it->it_disposition = (int) dlm_rep->lock_policy_res1;
468         it->it_status = (int) dlm_rep->lock_policy_res2;
469         it->it_lock_mode = lock_mode;
470         it->it_data = req;
471
472         /* We know what to expect, so we do any byte flipping required here */
473         LASSERT (reply_buffers == 3 || reply_buffers == 1);
474         if (reply_buffers == 3) {
475                 struct mds_body *body;
476
477                 body = lustre_swab_repbuf (req, 1, sizeof (*body),
478                                            lustre_swab_mds_body);
479                 if (body == NULL) {
480                         CERROR ("Can't swab mds_body\n");
481                         RETURN (-EPROTO);
482                 }
483
484                 if ((body->valid & OBD_MD_FLEASIZE) != 0) {
485                         /* The eadata is opaque; just check that it is
486                          * there.  Eventually, obd_unpackmd() will check
487                          * the contents */
488                         eadata = lustre_swab_repbuf (req, 2, body->eadatasize, 
489                                                      NULL);
490                         if (eadata == NULL) {
491                                 CERROR ("Missing/short eadata\n");
492                                 RETURN (-EPROTO);
493                         }
494                 }
495         }
496         
497         RETURN(rc);
498 }
499
500 static void mdc_replay_open(struct ptlrpc_request *req)
501 {
502         struct obd_client_handle *och = req->rq_replay_data;
503         struct lustre_handle old, *file_fh = &och->och_fh;
504         struct list_head *tmp;
505         struct mds_body *body;
506
507         body = lustre_swab_repbuf (req, 1, sizeof (*body),
508                                    lustre_swab_mds_body);
509         LASSERT (body != NULL);
510         
511         memcpy(&old, file_fh, sizeof(old));
512         CDEBUG(D_HA, "updating handle from "LPD64" to "LPD64"\n",
513                file_fh->cookie, body->handle.cookie);
514         memcpy(file_fh, &body->handle, sizeof(body->handle));
515
516         /* A few frames up, ptlrpc_replay holds the lock, so this is safe. */
517         list_for_each(tmp, &req->rq_import->imp_sending_list) {
518                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
519                 if (req->rq_reqmsg->opc != MDS_CLOSE)
520                         continue;
521                 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*body));
522                 if (memcmp(&body->handle, &old, sizeof(old)))
523                         continue;
524
525                 DEBUG_REQ(D_HA, req, "updating close body with new fh");
526                 memcpy(&body->handle, file_fh, sizeof(*file_fh));
527         }
528 }
529
530 void mdc_set_open_replay_data(struct obd_client_handle *och)
531 {
532         struct ptlrpc_request *req = och->och_req;
533         struct mds_rec_create *rec =
534                 lustre_msg_buf(req->rq_reqmsg, 2, sizeof (*rec));
535         struct mds_body *body =
536                 lustre_msg_buf(req->rq_repmsg, 1, sizeof (*body));
537
538         LASSERT (rec != NULL);
539         /* outgoing messages always in my byte order */
540         LASSERT (body != NULL);
541         /* incoming message in my byte order (it's been swabbed) */
542         LASSERT_REPSWABBED (req, 1);
543
544         memcpy(&rec->cr_replayfid, &body->fid1, sizeof rec->cr_replayfid);
545         req->rq_replay_cb = mdc_replay_open;
546         req->rq_replay_data = och;
547 }
548
549 int mdc_close(struct lustre_handle *conn, obd_id ino, int type,
550               struct lustre_handle *fh, struct ptlrpc_request **request)
551 {
552         struct mds_body *body;
553         int rc, size = sizeof(*body);
554         struct ptlrpc_request *req;
555         ENTRY;
556
557         req = ptlrpc_prep_req(class_conn2cliimp(conn), MDS_CLOSE, 1, &size,
558                               NULL);
559         if (!req)
560                 GOTO(out, rc = -ENOMEM);
561
562         body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*body));
563         ll_ino2fid(&body->fid1, ino, 0, type);
564         memcpy(&body->handle, fh, sizeof(body->handle));
565
566         req->rq_replen = lustre_msg_size(0, NULL);
567
568         mdc_get_rpc_lock(&mdc_rpc_lock, NULL);
569         rc = ptlrpc_queue_wait(req);
570         mdc_put_rpc_lock(&mdc_rpc_lock, NULL);
571
572         EXIT;
573  out:
574         *request = req;
575         return rc;
576 }
577
578 int mdc_readpage(struct lustre_handle *conn, obd_id ino, int type, __u64 offset,
579                  struct page *page, struct ptlrpc_request **request)
580 {
581         struct obd_import *imp = class_conn2cliimp(conn);
582         struct ptlrpc_request *req = NULL;
583         struct ptlrpc_bulk_desc *desc = NULL;
584         struct mds_body *body;
585         int rc, size = sizeof(*body);
586         ENTRY;
587
588         CDEBUG(D_INODE, "inode: %ld\n", (long)ino);
589
590         req = ptlrpc_prep_req(imp, MDS_READPAGE, 1, &size, NULL);
591         if (!req)
592                 GOTO(out, rc = -ENOMEM);
593         /* XXX FIXME bug 249 */
594         req->rq_request_portal = MDS_READPAGE_PORTAL;
595
596         desc = ptlrpc_prep_bulk_imp (req, BULK_PUT_SINK, MDS_BULK_PORTAL);
597         if (desc == NULL) {
598                 GOTO(out, rc = -ENOMEM);
599         }
600         /* NB req now owns desc and will free it when it gets freed */
601
602         rc = ptlrpc_prep_bulk_page(desc, page, 0, PAGE_CACHE_SIZE);
603         if (rc != 0)
604                 GOTO(out, rc);
605
606         mds_readdir_pack(req, offset, PAGE_CACHE_SIZE, ino, type);
607
608         req->rq_replen = lustre_msg_size(1, &size);
609         rc = ptlrpc_queue_wait(req);
610
611         if (rc == 0) {
612                 LASSERT (desc->bd_page_count == 1);
613                 body = lustre_swab_repbuf (req, 0, sizeof (*body),
614                                            lustre_swab_mds_body);
615                 if (body == NULL) {
616                         CERROR ("Can't unpack mds_body\n");
617                         GOTO (out, rc = -EPROTO);
618                 }
619         }
620
621         EXIT;
622  out:
623         *request = req;
624         return rc;
625 }
626
627 static int mdc_iocontrol(unsigned int cmd, struct lustre_handle *conn, int len,
628                          void *karg, void *uarg)
629 {
630         struct obd_device *obddev = class_conn2obd(conn);
631         struct obd_ioctl_data *data = karg;
632         struct obd_import *imp = obddev->u.cli.cl_import;
633         ENTRY;
634
635         switch (cmd) {
636         case OBD_IOC_CLIENT_RECOVER:
637                 RETURN(ptlrpc_recover_import(imp, data->ioc_inlbuf1));
638         case IOC_OSC_SET_ACTIVE:
639                 if (data->ioc_offset) {
640                         CERROR("%s: can't reactivate MDC\n",
641                                obddev->obd_uuid.uuid);
642                         RETURN(-ENOTTY);
643                 }
644                 RETURN(ptlrpc_set_import_active(imp, 0));
645         default:
646                 CERROR("osc_ioctl(): unrecognised ioctl %#x\n", cmd);
647                 RETURN(-ENOTTY);
648         }
649 }
650
651 static int mdc_statfs(struct lustre_handle *conn, struct obd_statfs *osfs)
652 {
653         struct ptlrpc_request *req;
654         struct obd_statfs *msfs;
655         int rc, size = sizeof(*msfs);
656         ENTRY;
657
658         req = ptlrpc_prep_req(class_conn2cliimp(conn), MDS_STATFS, 0, NULL,
659                               NULL);
660         if (!req)
661                 RETURN(-ENOMEM);
662
663         req->rq_replen = lustre_msg_size(1, &size);
664
665         mdc_get_rpc_lock(&mdc_rpc_lock, NULL);
666         rc = ptlrpc_queue_wait(req);
667         mdc_put_rpc_lock(&mdc_rpc_lock, NULL);
668
669         if (rc)
670                 GOTO(out, rc);
671
672         msfs = lustre_swab_repbuf (req, 0, sizeof (*msfs),
673                                    lustre_swab_obd_statfs);
674         if (msfs == NULL) {
675                 CERROR ("Can't unpack obd_statfs\n");
676                 GOTO (out, rc = -EPROTO);
677         }
678         
679         memcpy (osfs, msfs, sizeof (*msfs));
680         EXIT;
681 out:
682         ptlrpc_req_finished(req);
683
684         return rc;
685 }
686
687 static int mdc_attach(struct obd_device *dev, obd_count len, void *data)
688 {
689         struct lprocfs_static_vars lvars;
690
691         lprocfs_init_vars(&lvars);
692         return lprocfs_obd_attach(dev, lvars.obd_vars);
693 }
694
695 static int mdc_detach(struct obd_device *dev)
696 {
697         return lprocfs_obd_detach(dev);
698 }
699
700 struct obd_ops mdc_obd_ops = {
701         o_owner:       THIS_MODULE,
702         o_attach:      mdc_attach,
703         o_detach:      mdc_detach,
704         o_setup:       client_obd_setup,
705         o_cleanup:     client_obd_cleanup,
706         o_connect:     client_import_connect,
707         o_disconnect:  client_import_disconnect,
708         o_iocontrol:   mdc_iocontrol,
709         o_statfs:      mdc_statfs
710 };
711
712 int __init mdc_init(void)
713 {
714         struct lprocfs_static_vars lvars;
715         mdc_init_rpc_lock(&mdc_rpc_lock);
716         mdc_init_rpc_lock(&mdc_setattr_lock);
717         lprocfs_init_vars(&lvars);
718         return class_register_type(&mdc_obd_ops, lvars.module_vars,
719                                    LUSTRE_MDC_NAME);
720 }
721
722 static void __exit mdc_exit(void)
723 {
724         class_unregister_type(LUSTRE_MDC_NAME);
725 }
726
727 #ifdef __KERNEL__
728 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
729 MODULE_DESCRIPTION("Lustre Metadata Client");
730 MODULE_LICENSE("GPL");
731
732 EXPORT_SYMBOL(mdc_getstatus);
733 EXPORT_SYMBOL(mdc_getlovinfo);
734 EXPORT_SYMBOL(mdc_enqueue);
735 EXPORT_SYMBOL(mdc_getattr);
736 EXPORT_SYMBOL(mdc_getattr_name);
737 EXPORT_SYMBOL(mdc_create);
738 EXPORT_SYMBOL(mdc_unlink);
739 EXPORT_SYMBOL(mdc_rename);
740 EXPORT_SYMBOL(mdc_link);
741 EXPORT_SYMBOL(mdc_readpage);
742 EXPORT_SYMBOL(mdc_setattr);
743 EXPORT_SYMBOL(mdc_close);
744 EXPORT_SYMBOL(mdc_set_open_replay_data);
745
746 EXPORT_SYMBOL(mdc_store_inode_generation);
747
748 module_init(mdc_init);
749 module_exit(mdc_exit);
750 #endif