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