Whamcloud - gitweb
Move cleanup of service struct into ptlrpc_unregister_service().
[fs/lustre-release.git] / lustre / mds / handler.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  linux/mds/handler.c
5  *
6  *  Lustre Metadata Server (mds) request handler
7  *
8  *  Copyright (C) 2001, 2002 Cluster File Systems, Inc.
9  *
10  *  This code is issued under the GNU General Public License.
11  *  See the file COPYING in this distribution
12  *
13  *  by Peter Braam <braam@clusterfs.com> &
14  *     Andreas Dilger <braam@clusterfs.com>
15  *
16  *  This server is single threaded at present (but can easily be multi threaded)
17  *
18  */
19
20 #define EXPORT_SYMTAB
21 #define DEBUG_SUBSYSTEM S_MDS
22
23 #include <linux/module.h>
24 #include <linux/lustre_mds.h>
25 #include <linux/lustre_dlm.h>
26 extern int mds_update_last_rcvd(struct mds_obd *mds, void *handle,
27                          struct ptlrpc_request *req);
28 static int mds_cleanup(struct obd_device * obddev);
29
30 /* Assumes caller has already pushed into the kernel filesystem context */
31 static int mds_sendpage(struct ptlrpc_request *req, struct file *file,
32                         __u64 offset)
33 {
34         int rc = 0;
35         struct mds_obd *mds = &req->rq_obd->u.mds;
36         struct ptlrpc_bulk_desc *desc;
37         struct ptlrpc_bulk_page *bulk;
38         char *buf;
39         ENTRY;
40
41         desc = ptlrpc_prep_bulk(req->rq_connection);
42         if (desc == NULL)
43                 GOTO(out, rc = -ENOMEM);
44
45         bulk = ptlrpc_prep_bulk_page(desc);
46         if (bulk == NULL)
47                 GOTO(cleanup_bulk, rc = -ENOMEM);
48
49         OBD_ALLOC(buf, PAGE_SIZE);
50         if (buf == NULL)
51                 GOTO(cleanup_bulk, rc = -ENOMEM);
52
53         rc = mds_fs_readpage(mds, file, buf, PAGE_SIZE, (loff_t *)&offset);
54
55         if (rc != PAGE_SIZE)
56                 GOTO(cleanup_buf, rc = -EIO);
57
58         bulk->b_xid = req->rq_xid;
59         bulk->b_buf = buf;
60         bulk->b_buflen = PAGE_SIZE;
61         desc->b_portal = MDS_BULK_PORTAL;
62
63         rc = ptlrpc_send_bulk(desc);
64         if (rc)
65                 GOTO(cleanup_buf, rc);
66
67         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE)) {
68                 CERROR("obd_fail_loc=%x, fail operation rc=%d\n",
69                        OBD_FAIL_MDS_SENDPAGE, rc);
70                 ptlrpc_abort_bulk(desc);
71                 GOTO(cleanup_buf, rc);
72         }
73
74         wait_event_interruptible(desc->b_waitq, ptlrpc_check_bulk_sent(desc));
75         if (desc->b_flags & PTL_RPC_FL_INTR)
76                 GOTO(cleanup_buf, rc = -EINTR);
77
78         EXIT;
79  cleanup_buf:
80         OBD_FREE(buf, PAGE_SIZE);
81  cleanup_bulk:
82         ptlrpc_free_bulk(desc);
83  out:
84         return rc;
85 }
86
87 struct dentry *mds_fid2dentry(struct mds_obd *mds, struct ll_fid *fid,
88                               struct vfsmount **mnt)
89 {
90         /* stolen from NFS */
91         struct super_block *sb = mds->mds_sb;
92         unsigned long ino = fid->id;
93         __u32 generation = fid->generation;
94         struct inode *inode;
95         struct list_head *lp;
96         struct dentry *result;
97
98         if (ino == 0)
99                 RETURN(ERR_PTR(-ESTALE));
100
101         inode = iget(sb, ino);
102         if (inode == NULL)
103                 RETURN(ERR_PTR(-ENOMEM));
104
105         CDEBUG(D_DENTRY, "--> mds_fid2dentry: sb %p\n", inode->i_sb);
106
107         if (is_bad_inode(inode) ||
108             (generation && inode->i_generation != generation)) {
109                 /* we didn't find the right inode.. */
110                 CERROR("bad inode %lu, link: %d ct: %d or version  %u/%u\n",
111                        inode->i_ino, inode->i_nlink,
112                        atomic_read(&inode->i_count), inode->i_generation,
113                        generation);
114                 LBUG();
115                 iput(inode);
116                 RETURN(ERR_PTR(-ESTALE));
117         }
118
119         /* now to find a dentry.
120          * If possible, get a well-connected one
121          */
122         if (mnt)
123                 *mnt = mds->mds_vfsmnt;
124         spin_lock(&dcache_lock);
125         for (lp = inode->i_dentry.next; lp != &inode->i_dentry ; lp=lp->next) {
126                 result = list_entry(lp,struct dentry, d_alias);
127                 if (! (result->d_flags & DCACHE_NFSD_DISCONNECTED)) {
128                         dget_locked(result);
129                         result->d_vfs_flags |= DCACHE_REFERENCED;
130                         spin_unlock(&dcache_lock);
131                         iput(inode);
132                         if (mnt)
133                                 mntget(*mnt);
134                         return result;
135                 }
136         }
137         spin_unlock(&dcache_lock);
138         result = d_alloc_root(inode);
139         if (result == NULL) {
140                 iput(inode);
141                 return ERR_PTR(-ENOMEM);
142         }
143         if (mnt)
144                 mntget(*mnt);
145         result->d_flags |= DCACHE_NFSD_DISCONNECTED;
146         return result;
147 }
148
149 static int mds_connect(struct ptlrpc_request *req, struct mds_obd **mdsp)
150 {
151         struct mds_obd *mds;
152         char *uuid;
153         int rc, i;
154         ENTRY;
155
156         uuid = lustre_msg_buf(req->rq_reqmsg, 0);
157         if (req->rq_reqmsg->buflens[0] > 37) {
158                 /* Invalid UUID */
159                 req->rq_status = -EINVAL;
160                 RETURN(-EINVAL);
161         }
162
163         i = obd_class_name2dev(uuid);
164         if (i == -1) {
165                 req->rq_status = -ENODEV;
166                 RETURN(-ENODEV);
167         }
168
169         *mdsp = mds = &(obd_dev[i].u.mds);
170         if (mds != &(req->rq_obd->u.mds)) {
171                 CERROR("device %d (%s) is not an mds\n", i, uuid);
172                 req->rq_status = -ENODEV;
173                 RETURN(-ENODEV);
174         }
175
176         CDEBUG(D_INFO, "MDS connect from UUID '%s'\n",
177                ptlrpc_req_to_uuid(req));
178         rc = lustre_pack_msg(0, NULL, NULL, &req->rq_replen, &req->rq_repmsg);
179         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_CONNECT_PACK)) {
180                 req->rq_status = -ENOMEM;
181                 RETURN(-ENOMEM);
182         }
183         req->rq_repmsg->target_id = i;
184
185         RETURN(0);
186 }
187
188 /* FIXME: the error cases need fixing to avoid leaks */
189 static int mds_getstatus(struct mds_obd *mds, struct ptlrpc_request *req)
190 {
191         struct mds_body *body;
192         struct mds_client_info *mci;
193         struct mds_client_data *mcd;
194         int rc, size = sizeof(*body);
195         ENTRY;
196
197         rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
198         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_GETSTATUS_PACK)) {
199                 CERROR("mds: out of memory for message: size=%d\n", size);
200                 req->rq_status = -ENOMEM;
201                 RETURN(0);
202         }
203
204         body = lustre_msg_buf(req->rq_reqmsg, 0);
205         mds_unpack_body(body);
206         /* Anything we need to do here with the client's trans no or so? */
207
208         body = lustre_msg_buf(req->rq_repmsg, 0);
209         memcpy(&body->fid1, &mds->mds_rootfid, sizeof(body->fid1));
210
211         mci = mds_uuid_to_mci(mds, ptlrpc_req_to_uuid(req));
212         if (!mci) {
213                 /* We don't have any old connection data for this client */
214                 int rc;
215
216                 CDEBUG(D_INFO, "allocating new client data for UUID '%s'",
217                        ptlrpc_req_to_uuid(req));
218
219                 OBD_ALLOC(mcd, sizeof(*mcd));
220                 if (!mcd) {
221                         CERROR("mds: out of memory for client data\n");
222                         req->rq_status = -ENOMEM;
223                         RETURN(0);
224                 }
225                 memcpy(mcd->mcd_uuid, ptlrpc_req_to_uuid(req),
226                        sizeof(mcd->mcd_uuid));
227                 rc = mds_client_add(mds, mcd, -1);
228                 if (rc) {
229                         req->rq_status = rc;
230                         RETURN(0);
231                 }
232         } else {
233                 /* We have old connection data for this client... */
234                 mcd = mci->mci_mcd;
235                 CDEBUG(D_INFO, "found existing data for UUID '%s' at #%d\n",
236                        mcd->mcd_uuid, mci->mci_off);
237         }
238         /* mcd_last_xid is is stored in little endian on the disk and
239            mds_pack_rep_body converts it to network order */
240         body->last_xid = le32_to_cpu(mcd->mcd_last_xid);
241         mds_pack_rep_body(req);
242         RETURN(0);
243 }
244
245 static int mds_disconnect(struct mds_obd *mds, struct ptlrpc_request *req)
246 {
247         struct mds_body *body;
248         int rc;
249         ENTRY;
250
251         body = lustre_msg_buf(req->rq_reqmsg, 0);
252
253         rc = lustre_pack_msg(0, NULL, NULL, &req->rq_replen, &req->rq_repmsg);
254         if (rc)
255                 RETURN(rc);
256
257         RETURN(0);
258 }
259
260 int mds_lock_callback(struct ldlm_lock *lock, struct ldlm_lock *new,
261                       void *data, int data_len, struct ptlrpc_request **reqp)
262 {
263         ENTRY;
264
265         if (new == NULL) {
266                 /* Completion AST.  Do nothing */
267                 RETURN(0);
268         }
269
270         if (ldlm_cli_cancel(lock->l_client, lock) < 0)
271                 LBUG();
272         RETURN(0);
273 }
274
275 static int mds_getattr_name(int offset, struct ptlrpc_request *req)
276 {
277         struct mds_obd *mds = &req->rq_obd->u.mds;
278         struct obd_run_ctxt saved;
279         struct mds_body *body;
280         struct dentry *de = NULL, *dchild = NULL;
281         struct inode *dir;
282         struct ldlm_lock *lock;
283         struct lustre_handle lockh;
284         char *name;
285         int namelen, flags, lock_mode, rc = 0;
286         __u64 res_id[3] = {0, 0, 0};
287         ENTRY;
288
289         if (strcmp(req->rq_obd->obd_type->typ_name, "mds") != 0)
290                 LBUG();
291
292         if (req->rq_reqmsg->bufcount <= offset + 1) {
293                 LBUG();
294                 GOTO(out_pre_de, rc = -EINVAL);
295         }
296
297         body = lustre_msg_buf(req->rq_reqmsg, offset);
298         name = lustre_msg_buf(req->rq_reqmsg, offset + 1);
299         namelen = req->rq_reqmsg->buflens[offset + 1];
300         /* requests were at offset 2, replies go back at 1 */
301         if (offset)
302                 offset = 1;
303
304         push_ctxt(&saved, &mds->mds_ctxt);
305         de = mds_fid2dentry(mds, &body->fid1, NULL);
306         if (IS_ERR(de)) {
307                 LBUG();
308                 GOTO(out_pre_de, rc = -ESTALE);
309         }
310
311         dir = de->d_inode;
312         CDEBUG(D_INODE, "parent ino %ld\n", dir->i_ino);
313
314         lock_mode = (req->rq_reqmsg->opc == MDS_REINT) ? LCK_CW : LCK_PW;
315         res_id[0] = dir->i_ino;
316
317         rc = ldlm_local_lock_match(mds->mds_local_namespace, res_id, LDLM_PLAIN,
318                                    NULL, 0, lock_mode, &lockh);
319         if (rc == 0) {
320                 LDLM_DEBUG_NOLOCK("enqueue res %Lu", res_id[0]);
321                 rc = ldlm_cli_enqueue(mds->mds_ldlm_client, mds->mds_ldlm_conn,
322                                       NULL, mds->mds_local_namespace, NULL,
323                                       res_id, LDLM_PLAIN, NULL, 0, lock_mode,
324                                       &flags, (void *)mds_lock_callback,
325                                       NULL, 0, &lockh);
326                 if (rc != ELDLM_OK) {
327                         CERROR("lock enqueue: err: %d\n", rc);
328                         GOTO(out_create_de, rc = -EIO);
329                 }
330         } else {
331                 lock = lustre_handle2object(&lockh);
332                 LDLM_DEBUG(lock, "matched");
333         }
334         ldlm_lock_dump((void *)(unsigned long)lockh.addr);
335
336         down(&dir->i_sem);
337         dchild = lookup_one_len(name, de, namelen - 1);
338         if (IS_ERR(dchild)) {
339                 CERROR("child lookup error %ld\n", PTR_ERR(dchild));
340                 up(&dir->i_sem);
341                 LBUG();
342                 GOTO(out_create_dchild, rc = -ESTALE);
343         }
344
345         if (dchild->d_inode) {
346                 struct mds_body *body;
347                 struct obdo *obdo;
348                 struct inode *inode = dchild->d_inode;
349                 CERROR("child exists (dir %ld, name %s, ino %ld)\n",
350                        dir->i_ino, name, dchild->d_inode->i_ino);
351
352                 body = lustre_msg_buf(req->rq_repmsg, offset);
353                 mds_pack_inode2fid(&body->fid1, inode);
354                 mds_pack_inode2body(body, inode);
355                 if (S_ISREG(inode->i_mode)) {
356                         obdo = lustre_msg_buf(req->rq_repmsg, offset + 1);
357                         mds_fs_get_obdo(mds, inode, obdo);
358                 }
359                 /* now a normal case for intent locking */
360                 rc = 0;
361         } else {
362                 rc = -ENOENT;
363         }
364
365         EXIT;
366 out_create_dchild:
367         l_dput(dchild);
368         up(&dir->i_sem);
369         lock = lustre_handle2object(&lockh);
370         ldlm_lock_decref(lock, lock_mode);
371 out_create_de:
372         l_dput(de);
373 out_pre_de:
374         req->rq_status = rc;
375         pop_ctxt(&saved);
376         return 0;
377 }
378
379
380 static int mds_getattr(int offset, struct ptlrpc_request *req)
381 {
382         struct mds_obd *mds = &req->rq_obd->u.mds;
383         struct obd_run_ctxt saved;
384         struct dentry *de;
385         struct inode *inode;
386         struct mds_body *body;
387         int rc, size[2] = {sizeof(*body)}, bufcount = 1;
388         ENTRY;
389
390         body = lustre_msg_buf(req->rq_reqmsg, offset);
391         push_ctxt(&saved, &mds->mds_ctxt);
392         de = mds_fid2dentry(mds, &body->fid1, NULL);
393         if (IS_ERR(de)) {
394                 GOTO(out_pop, rc = -ENOENT);
395         }
396
397         inode = de->d_inode;
398         if (S_ISREG(body->fid1.f_type)) {
399                 bufcount = 2;
400                 size[1] = sizeof(struct obdo);
401         } else if (body->valid & OBD_MD_LINKNAME) {
402                 bufcount = 2;
403                 size[1] = inode->i_size;
404         }
405
406         rc = lustre_pack_msg(bufcount, size, NULL, &req->rq_replen,
407                              &req->rq_repmsg);
408         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK)) {
409                 CERROR("mds: out of memory\n");
410                 GOTO(out, rc);
411         }
412
413         if (body->valid & OBD_MD_LINKNAME) {
414                 char *tmp = lustre_msg_buf(req->rq_repmsg, 1);
415
416                 rc = inode->i_op->readlink(de, tmp, size[1]);
417
418                 if (rc < 0) {
419                         CERROR("readlink failed: %d\n", rc);
420                         GOTO(out, rc);
421                 }
422         }
423
424         body = lustre_msg_buf(req->rq_repmsg, 0);
425         body->ino = inode->i_ino;
426         body->generation = inode->i_generation;
427         body->atime = inode->i_atime;
428         body->ctime = inode->i_ctime;
429         body->mtime = inode->i_mtime;
430         body->uid = inode->i_uid;
431         body->gid = inode->i_gid;
432         body->size = inode->i_size;
433         body->mode = inode->i_mode;
434         body->nlink = inode->i_nlink;
435         body->valid = ~0; /* FIXME: should be more selective */
436
437         if (S_ISREG(inode->i_mode)) {
438                 rc = mds_fs_get_obdo(mds, inode,
439                                      lustre_msg_buf(req->rq_repmsg, 1));
440                 if (rc < 0) {
441                         CERROR("mds_fs_get_obdo failed: %d\n", rc);
442                         GOTO(out, rc);
443                 }
444         }
445 out:
446         l_dput(de);
447 out_pop:
448         pop_ctxt(&saved);
449         req->rq_status = rc;
450         RETURN(0);
451 }
452
453 static int mds_statfs(struct ptlrpc_request *req)
454 {
455         struct mds_obd *mds = &req->rq_obd->u.mds;
456         struct obd_statfs *osfs;
457         struct statfs sfs;
458         int rc, size = sizeof(*osfs);
459         ENTRY;
460
461         rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen,
462                              &req->rq_repmsg);
463         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK)) {
464                 CERROR("mds: statfs lustre_pack_msg failed: rc = %d\n", rc);
465                 GOTO(out, rc);
466         }
467
468         rc = vfs_statfs(mds->mds_sb, &sfs);
469         if (rc) {
470                 CERROR("mds: statfs failed: rc %d\n", rc);
471                 GOTO(out, rc);
472         }
473         osfs = lustre_msg_buf(req->rq_repmsg, 0);
474         memset(osfs, 0, size);
475         /* FIXME: hack until the MDS gets the OST data, next month */
476         sfs.f_blocks = -1;
477         sfs.f_bfree = -1;
478         sfs.f_bavail = -1;
479         obd_statfs_pack(osfs, &sfs);
480
481 out:
482         req->rq_status = rc;
483         RETURN(0);
484 }
485
486 static int mds_open(struct ptlrpc_request *req)
487 {
488         struct dentry *de;
489         struct mds_body *body;
490         struct file *file;
491         struct vfsmount *mnt;
492         struct mds_obd *mds = &req->rq_obd->u.mds;
493         struct mds_client_info *mci;
494         __u32 flags;
495         struct list_head *tmp;
496         struct mds_file_data *mfd;
497         int rc, size = sizeof(*body);
498         ENTRY;
499
500         rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
501         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_OPEN_PACK)) {
502                 CERROR("mds: out of memory\n");
503                 req->rq_status = -ENOMEM;
504                 RETURN(0);
505         }
506
507         mci = mds_uuid_to_mci(mds, ptlrpc_req_to_uuid(req));
508         if (!mci) {
509                 CERROR("mds: no mci!\n");
510                 req->rq_status = -ENOTCONN;
511                 RETURN(0);
512         }
513
514         body = lustre_msg_buf(req->rq_reqmsg, 0);
515
516         /* was this animal open already? */
517         /* XXX we chould only check on re-open, or do a refcount... */
518         list_for_each(tmp, &mci->mci_open_head) {
519                 struct mds_file_data *fd;
520                 fd = list_entry(tmp, struct mds_file_data, mfd_list);
521                 if (body->extra == fd->mfd_clientfd &&
522                     body->fid1.id == fd->mfd_file->f_dentry->d_inode->i_ino) {
523                         CERROR("Re opening %Ld\n", body->fid1.id);
524                         RETURN(0);
525                 }
526         }
527
528         OBD_ALLOC(mfd, sizeof(*mfd));
529         if (!mfd) {
530                 CERROR("mds: out of memory\n");
531                 req->rq_status = -ENOMEM;
532                 RETURN(0);
533         }
534
535         de = mds_fid2dentry(mds, &body->fid1, &mnt);
536         if (IS_ERR(de)) {
537                 req->rq_status = -ENOENT;
538                 RETURN(0);
539         }
540
541         /* check if this inode has seen a delayed object creation */
542         if (req->rq_reqmsg->bufcount > 1) {
543                 void *handle;
544                 struct inode *inode = de->d_inode;
545                 //struct iattr iattr;
546                 struct obdo *obdo;
547                 int rc;
548
549                 obdo = lustre_msg_buf(req->rq_reqmsg, 1);
550                 //iattr.ia_valid = ATTR_MODE;
551                 //iattr.ia_mode = inode->i_mode;
552
553                 handle = mds_fs_start(mds, de->d_inode, MDS_FSOP_SETATTR);
554                 if (!handle) {
555                         req->rq_status = -ENOMEM;
556                         RETURN(0);
557                 }
558
559                 /* XXX error handling */
560                 rc = mds_fs_set_obdo(mds, inode, handle, obdo);
561                 //                rc = mds_fs_setattr(mds, de, handle, &iattr);
562                 if (!rc)
563                         rc = mds_update_last_rcvd(mds, handle, req);
564                 else {
565                         req->rq_status = rc;
566                         RETURN(0);
567                 }
568                 /* FIXME: need to return last_rcvd, last_committed */
569
570                 /* FIXME: keep rc intact */
571                 rc = mds_fs_commit(mds, de->d_inode, handle);
572                 if (rc) {
573                         req->rq_status = rc;
574                         RETURN(0);
575                 }
576         }
577
578         flags = body->flags;
579         file = dentry_open(de, mnt, flags & ~O_DIRECT);
580         if (!file || IS_ERR(file)) {
581                 req->rq_status = -EINVAL;
582                 OBD_FREE(mfd, sizeof(*mfd));
583                 RETURN(0);
584         }
585
586         file->private_data = mfd;
587         mfd->mfd_file = file;
588         mfd->mfd_clientfd = body->extra;
589         list_add(&mfd->mfd_list, &mci->mci_open_head);
590
591         body = lustre_msg_buf(req->rq_repmsg, 0);
592         body->extra = (__u64) (unsigned long)file;
593         RETURN(0);
594 }
595
596 static
597 int mds_close(struct ptlrpc_request *req)
598 {
599         struct dentry *de;
600         struct mds_body *body;
601         struct file *file;
602         struct mds_obd *mds = &req->rq_obd->u.mds;
603         struct vfsmount *mnt;
604         struct mds_file_data *mfd;
605         int rc;
606         ENTRY;
607
608         rc = lustre_pack_msg(0, NULL, NULL, &req->rq_replen, &req->rq_repmsg);
609         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_CLOSE_PACK)) {
610                 CERROR("mds: out of memory\n");
611                 req->rq_status = -ENOMEM;
612                 RETURN(0);
613         }
614
615         body = lustre_msg_buf(req->rq_reqmsg, 0);
616         de = mds_fid2dentry(mds, &body->fid1, &mnt);
617         if (IS_ERR(de)) {
618                 req->rq_status = -ENOENT;
619                 RETURN(0);
620         }
621
622         file = (struct file *)(unsigned long)body->extra;
623         if (!file->f_dentry)
624                 LBUG();
625         mfd = (struct mds_file_data *)file->private_data;
626         list_del(&mfd->mfd_list);
627         OBD_FREE(mfd, sizeof(*mfd));
628
629         req->rq_status = filp_close(file, 0);
630         l_dput(de);
631         mntput(mnt);
632
633         RETURN(0);
634 }
635
636 static int mds_readpage(struct mds_obd *mds, struct ptlrpc_request *req)
637 {
638         struct vfsmount *mnt;
639         struct dentry *de;
640         struct file *file;
641         struct mds_body *body;
642         struct obd_run_ctxt saved;
643         int rc, size = sizeof(*body);
644         ENTRY;
645
646         rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
647         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK)) {
648                 CERROR("mds: out of memory\n");
649                 GOTO(out, rc = -ENOMEM);
650         }
651
652         body = lustre_msg_buf(req->rq_reqmsg, 0);
653         push_ctxt(&saved, &mds->mds_ctxt);
654         de = mds_fid2dentry(mds, &body->fid1, &mnt);
655         if (IS_ERR(de))
656                 GOTO(out_pop, rc = PTR_ERR(de));
657
658         CDEBUG(D_INODE, "ino %ld\n", de->d_inode->i_ino);
659
660         file = dentry_open(de, mnt, O_RDONLY | O_LARGEFILE);
661         /* note: in case of an error, dentry_open puts dentry */
662         if (IS_ERR(file))
663                 GOTO(out_pop, rc = PTR_ERR(file));
664
665         /* to make this asynchronous make sure that the handling function
666            doesn't send a reply when this function completes. Instead a
667            callback function would send the reply */
668         rc = mds_sendpage(req, file, body->size);
669
670         filp_close(file, 0);
671 out_pop:
672         pop_ctxt(&saved);
673 out:
674         req->rq_status = rc;
675         RETURN(0);
676 }
677
678 int mds_reint(int offset, struct ptlrpc_request *req)
679 {
680         int rc;
681         struct mds_update_record rec;
682
683         rc = mds_update_unpack(req, offset, &rec);
684         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNPACK)) {
685                 CERROR("invalid record\n");
686                 req->rq_status = -EINVAL;
687                 RETURN(0);
688         }
689         /* rc will be used to interrupt a for loop over multiple records */
690         rc = mds_reint_rec(&rec, offset, req);
691         return rc;
692 }
693
694 int mds_handle(struct obd_device *dev, struct ptlrpc_service *svc,
695                struct ptlrpc_request *req)
696 {
697         struct mds_obd *mds = NULL;
698         int rc;
699         ENTRY;
700
701         rc = lustre_unpack_msg(req->rq_reqmsg, req->rq_reqlen);
702         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_HANDLE_UNPACK)) {
703                 CERROR("lustre_mds: Invalid request\n");
704                 GOTO(out, rc);
705         }
706
707         if (req->rq_reqmsg->type != PTL_RPC_MSG_REQUEST) {
708                 CERROR("lustre_mds: wrong packet type sent %d\n",
709                        req->rq_reqmsg->type);
710                 GOTO(out, rc = -EINVAL);
711         }
712
713         if (req->rq_reqmsg->opc != MDS_CONNECT) {
714                 int id = req->rq_reqmsg->target_id;
715                 struct obd_device *obddev;
716                 if (id < 0 || id > MAX_OBD_DEVICES)
717                         GOTO(out, rc = -ENODEV);
718                 obddev = &obd_dev[id];
719                 if (strcmp(obddev->obd_type->typ_name, "mds") != 0)
720                         GOTO(out, rc = -EINVAL);
721                 mds = &obddev->u.mds;
722                 req->rq_obd = obddev;
723         }
724
725         switch (req->rq_reqmsg->opc) {
726         case MDS_CONNECT:
727                 CDEBUG(D_INODE, "connect\n");
728                 OBD_FAIL_RETURN(OBD_FAIL_MDS_CONNECT_NET, 0);
729                 rc = mds_connect(req, &mds);
730                 break;
731
732         case MDS_DISCONNECT:
733                 CDEBUG(D_INODE, "disconnect\n");
734                 OBD_FAIL_RETURN(OBD_FAIL_MDS_DISCONNECT_NET, 0);
735                 rc = mds_disconnect(mds, req);
736                 break;
737
738         case MDS_GETSTATUS:
739                 CDEBUG(D_INODE, "getstatus\n");
740                 OBD_FAIL_RETURN(OBD_FAIL_MDS_GETSTATUS_NET, 0);
741                 rc = mds_getstatus(mds, req);
742                 break;
743
744         case MDS_GETATTR:
745                 CDEBUG(D_INODE, "getattr\n");
746                 OBD_FAIL_RETURN(OBD_FAIL_MDS_GETATTR_NET, 0);
747                 rc = mds_getattr(0, req);
748                 break;
749
750         case MDS_STATFS:
751                 CDEBUG(D_INODE, "statfs\n");
752                 OBD_FAIL_RETURN(OBD_FAIL_MDS_STATFS_NET, 0);
753                 rc = mds_statfs(req);
754                 break;
755
756         case MDS_READPAGE:
757                 CDEBUG(D_INODE, "readpage\n");
758                 OBD_FAIL_RETURN(OBD_FAIL_MDS_READPAGE_NET, 0);
759                 rc = mds_readpage(mds, req);
760
761                 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
762                         return 0;
763                 break;
764
765         case MDS_REINT: {
766                 int size = sizeof(struct mds_body);
767                 CDEBUG(D_INODE, "reint\n");
768                 OBD_FAIL_RETURN(OBD_FAIL_MDS_REINT_NET, 0);
769
770                 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen,
771                                      &req->rq_repmsg);
772                 if (rc) {
773                         rc = req->rq_status = -ENOMEM;
774                         break;
775                 }
776                 rc = mds_reint(0, req);
777                 OBD_FAIL_RETURN(OBD_FAIL_MDS_REINT_NET_REP, 0);
778                 break;
779         }
780
781         case MDS_OPEN:
782                 CDEBUG(D_INODE, "open\n");
783                 OBD_FAIL_RETURN(OBD_FAIL_MDS_OPEN_NET, 0);
784                 rc = mds_open(req);
785                 break;
786
787         case MDS_CLOSE:
788                 CDEBUG(D_INODE, "close\n");
789                 OBD_FAIL_RETURN(OBD_FAIL_MDS_CLOSE_NET, 0);
790                 rc = mds_close(req);
791                 break;
792
793         default:
794                 rc = ptlrpc_error(svc, req);
795                 RETURN(rc);
796         }
797
798         EXIT;
799 out:
800         /* Still not 100% sure whether we should reply with the server
801          * last_rcvd or that of this client.  I'm not sure it even makes
802          * a difference on a per-client basis, because last_rcvd is global
803          * and we are not supposed to allow transactions while in recovery.
804          */
805         if (rc) {
806                 ptlrpc_error(svc, req);
807         } else {
808                 req->rq_repmsg->last_rcvd = HTON__u64(mds->mds_last_rcvd);
809                 req->rq_repmsg->last_committed =
810                         HTON__u64(mds->mds_last_committed);
811                 CDEBUG(D_INFO, "last_rcvd %Lu, last_committed %Lu, xid %d\n",
812                        (unsigned long long)mds->mds_last_rcvd,
813                        (unsigned long long)mds->mds_last_committed,
814                        cpu_to_le32(req->rq_xid));
815                 CDEBUG(D_NET, "sending reply\n");
816                 ptlrpc_reply(svc, req);
817         }
818         return 0;
819 }
820
821 /* Update the server data on disk.  This stores the new mount_count and
822  * also the last_rcvd value to disk.  If we don't have a clean shutdown,
823  * then the server last_rcvd value may be less than that of the clients.
824  * This will alert us that we may need to do client recovery.
825  */
826 static
827 int mds_update_server_data(struct mds_obd *mds)
828 {
829         struct obd_run_ctxt saved;
830         struct mds_server_data *msd = mds->mds_server_data;
831         struct file *filp = mds->mds_rcvd_filp;
832         loff_t off = 0;
833         int rc;
834
835         msd->msd_last_rcvd = cpu_to_le64(mds->mds_last_rcvd);
836         msd->msd_mount_count = cpu_to_le64(mds->mds_mount_count);
837
838         CDEBUG(D_SUPER, "MDS mount_count is %Lu, last_rcvd is %Lu\n",
839                (unsigned long long)mds->mds_mount_count,
840                (unsigned long long)mds->mds_last_rcvd);
841         push_ctxt(&saved, &mds->mds_ctxt);
842         rc = lustre_fwrite(filp, (char *)msd, sizeof(*msd), &off);
843         if (rc != sizeof(*msd)) {
844                 CERROR("error writing MDS server data: rc = %d\n", rc);
845                 if (rc > 0)
846                         RETURN(-EIO);
847                 RETURN(rc);
848         }
849         rc = fsync_dev(filp->f_dentry->d_inode->i_rdev);
850         pop_ctxt(&saved);
851         if (rc)
852                 CERROR("error flushing MDS server data: rc = %d\n", rc);
853
854         return 0;
855 }
856
857 /* Do recovery actions for the MDS */
858 static int mds_recover(struct obd_device *obddev)
859 {
860         struct mds_obd *mds = &obddev->u.mds;
861         int rc;
862
863         /* This happens at the end when recovery is complete */
864         ++mds->mds_mount_count;
865         rc = mds_update_server_data(mds);
866
867         return rc;
868 }
869
870
871 /* mount the file system (secretly) */
872 static int mds_setup(struct obd_device *obddev, obd_count len, void *buf)
873 {
874         struct obd_ioctl_data* data = buf;
875         struct mds_obd *mds = &obddev->u.mds;
876         struct vfsmount *mnt;
877         int rc = 0;
878         ENTRY;
879
880         MOD_INC_USE_COUNT;
881 #ifdef CONFIG_DEV_RDONLY
882         dev_clear_rdonly(2);
883 #endif
884         if (!data->ioc_inlbuf1 || !data->ioc_inlbuf2)
885                 GOTO(err_dec, rc = -EINVAL);
886
887         mds->mds_fstype = strdup(data->ioc_inlbuf2);
888
889         mnt = do_kern_mount(mds->mds_fstype, 0, data->ioc_inlbuf1, NULL);
890         if (IS_ERR(mnt)) {
891                 rc = PTR_ERR(mnt);
892                 CERROR("do_kern_mount failed: rc = %d\n", rc);
893                 GOTO(err_kfree, rc);
894         }
895
896         mds->mds_sb = mnt->mnt_root->d_inode->i_sb;
897         if (!mds->mds_sb)
898                 GOTO(err_put, rc = -ENODEV);
899
900         rc = mds_fs_setup(mds, mnt);
901         if (rc) {
902                 CERROR("MDS filesystem method init failed: rc = %d\n", rc);
903                 GOTO(err_put, rc);
904         }
905
906         mds->mds_service = ptlrpc_init_svc(64 * 1024, MDS_REQUEST_PORTAL,
907                                            MDC_REPLY_PORTAL, "self",mds_handle);
908         if (!mds->mds_service) {
909                 CERROR("failed to start service\n");
910                 GOTO(err_fs, rc = -EINVAL);
911         }
912
913         rc = ptlrpc_start_thread(obddev, mds->mds_service, "lustre_mds");
914         if (rc) {
915                 CERROR("cannot start thread: rc = %d\n", rc);
916                 GOTO(err_svc, rc);
917         }
918
919         rc = -ENOENT;
920         mds->mds_ldlm_conn = ptlrpc_uuid_to_connection("self");
921         if (!mds->mds_ldlm_conn) {
922                 mds_cleanup(obddev);
923                 GOTO(err_thread, rc);
924         }
925
926         obddev->obd_namespace =
927                 ldlm_namespace_new("mds_server", LDLM_NAMESPACE_SERVER);
928         if (obddev->obd_namespace == NULL) {
929                 LBUG();
930                 mds_cleanup(obddev);
931                 GOTO(err_thread, rc);
932         }
933
934         mds->mds_local_namespace =
935                 ldlm_namespace_new("mds_client", LDLM_NAMESPACE_CLIENT);
936         if (mds->mds_local_namespace == NULL) {
937                 LBUG();
938                 mds_cleanup(obddev);
939                 GOTO(err_thread, rc);
940         }
941
942         OBD_ALLOC(mds->mds_ldlm_client, sizeof(*mds->mds_ldlm_client));
943         if (mds->mds_ldlm_client == NULL) {
944                 LBUG();
945                 mds_cleanup(obddev);
946                 GOTO(err_thread, rc);
947         }
948         ptlrpc_init_client(NULL, NULL, LDLM_REQUEST_PORTAL, LDLM_REPLY_PORTAL,
949                            mds->mds_ldlm_client);
950         mds->mds_ldlm_client->cli_target_devno = obddev->obd_minor;
951         mds->mds_ldlm_client->cli_name = "mds ldlm";
952
953         rc = mds_recover(obddev);
954         if (rc)
955                 GOTO(err_thread, rc);
956
957         RETURN(0);
958
959 err_thread:
960         ptlrpc_stop_all_threads(mds->mds_service);
961 err_svc:
962         ptlrpc_unregister_service(mds->mds_service);
963 err_fs:
964         mds_fs_cleanup(mds);
965 err_put:
966         unlock_kernel();
967         mntput(mds->mds_vfsmnt);
968         mds->mds_sb = 0;
969         lock_kernel();
970 err_kfree:
971         kfree(mds->mds_fstype);
972 err_dec:
973         MOD_DEC_USE_COUNT;
974         return rc;
975 }
976
977 static int mds_cleanup(struct obd_device * obddev)
978 {
979         struct super_block *sb;
980         struct mds_obd *mds = &obddev->u.mds;
981
982         ENTRY;
983
984         if ( !list_empty(&obddev->obd_gen_clients) ) {
985                 CERROR("still has clients!\n");
986                 RETURN(-EBUSY);
987         }
988
989         ptlrpc_stop_all_threads(mds->mds_service);
990         ptlrpc_unregister_service(mds->mds_service);
991
992         sb = mds->mds_sb;
993         if (!mds->mds_sb)
994                 RETURN(0);
995
996         mds_update_server_data(mds);
997
998         if (mds->mds_rcvd_filp) {
999                 int rc = filp_close(mds->mds_rcvd_filp, 0);
1000                 mds->mds_rcvd_filp = NULL;
1001
1002                 if (rc)
1003                         CERROR("last_rcvd file won't close, rc=%d\n", rc);
1004         }
1005
1006         unlock_kernel();
1007         mntput(mds->mds_vfsmnt);
1008         mds->mds_sb = 0;
1009         kfree(mds->mds_fstype);
1010
1011         ldlm_namespace_free(mds->mds_local_namespace);
1012         ldlm_namespace_free(obddev->obd_namespace);
1013
1014         if (mds->mds_ldlm_conn != NULL)
1015                 ptlrpc_put_connection(mds->mds_ldlm_conn);
1016
1017         OBD_FREE(mds->mds_ldlm_client, sizeof(*mds->mds_ldlm_client));
1018
1019         lock_kernel();
1020 #ifdef CONFIG_DEV_RDONLY
1021         dev_clear_rdonly(2);
1022 #endif
1023         mds_fs_cleanup(mds);
1024
1025         MOD_DEC_USE_COUNT;
1026         RETURN(0);
1027 }
1028
1029 /* use obd ops to offer management infrastructure */
1030 static struct obd_ops mds_obd_ops = {
1031         o_setup:       mds_setup,
1032         o_cleanup:     mds_cleanup,
1033 };
1034
1035 static int __init mds_init(void)
1036 {
1037         inter_module_register("mds_reint", THIS_MODULE, &mds_reint);
1038         inter_module_register("mds_getattr_name", THIS_MODULE,
1039                               &mds_getattr_name);
1040         obd_register_type(&mds_obd_ops, LUSTRE_MDS_NAME);
1041         return 0;
1042 }
1043
1044 static void __exit mds_exit(void)
1045 {
1046         inter_module_unregister("mds_reint");
1047         inter_module_unregister("mds_getattr_name");
1048         obd_unregister_type(LUSTRE_MDS_NAME);
1049 }
1050
1051 MODULE_AUTHOR("Cluster File Systems <info@clusterfs.com>");
1052 MODULE_DESCRIPTION("Lustre Metadata Server (MDS) v0.01");
1053 MODULE_LICENSE("GPL");
1054
1055 module_init(mds_init);
1056 module_exit(mds_exit);