Whamcloud - gitweb
Make sure that we set last_xid and last_committed on replies from LDLM,
[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  *  lustre/mds/handler.c
5  *  Lustre Metadata Server (mds) request handler
6  *
7  *  Copyright (c) 2001, 2002 Cluster File Systems, Inc.
8  *   Author: Peter Braam <braam@clusterfs.com>
9  *   Author: Andreas Dilger <adilger@clusterfs.com>
10  *   Author: Phil Schwan <phil@clusterfs.com>
11  *
12  *   This file is part of Lustre, http://www.lustre.org.
13  *
14  *   Lustre is free software; you can redistribute it and/or
15  *   modify it under the terms of version 2 of the GNU General Public
16  *   License as published by the Free Software Foundation.
17  *
18  *   Lustre is distributed in the hope that it will be useful,
19  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
20  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  *   GNU General Public License for more details.
22  *
23  *   You should have received a copy of the GNU General Public License
24  *   along with Lustre; if not, write to the Free Software
25  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26  */
27
28 #define EXPORT_SYMTAB
29 #define DEBUG_SUBSYSTEM S_MDS
30
31 #include <linux/module.h>
32 #include <linux/lustre_mds.h>
33 #include <linux/lustre_dlm.h>
34 #include <linux/init.h>
35 #include <linux/obd_class.h>
36 #include <linux/random.h>
37 #include <linux/locks.h>
38 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
39 #include <linux/buffer_head.h>
40 #endif
41 #include <linux/lprocfs_status.h>
42
43 static kmem_cache_t *mds_file_cache;
44
45 extern int mds_get_lovtgts(struct mds_obd *obd, int tgt_count,
46                            obd_uuid_t *uuidarray);
47 extern int mds_get_lovdesc(struct mds_obd  *obd, struct lov_desc *desc);
48 extern int mds_update_last_rcvd(struct mds_obd *mds, void *handle,
49                                 struct ptlrpc_request *req);
50 static int mds_cleanup(struct obd_device * obddev);
51
52 extern struct lprocfs_vars status_var_nm_1[];
53 extern struct lprocfs_vars status_class_var[];
54
55 inline struct mds_obd *mds_req2mds(struct ptlrpc_request *req)
56 {
57         return &req->rq_export->exp_obd->u.mds;
58 }
59
60 static int mds_bulk_timeout(void *data)
61 {
62         struct ptlrpc_bulk_desc *desc = data;
63
64         ENTRY;
65         CERROR("(not yet) starting recovery of client %p\n", desc->bd_client);
66         RETURN(1);
67 }
68
69 /* Assumes caller has already pushed into the kernel filesystem context */
70 static int mds_sendpage(struct ptlrpc_request *req, struct file *file,
71                         __u64 offset)
72 {
73         int rc = 0;
74         struct mds_obd *mds = mds_req2mds(req);
75         struct ptlrpc_bulk_desc *desc;
76         struct ptlrpc_bulk_page *bulk;
77         struct l_wait_info lwi;
78         char *buf;
79         ENTRY;
80
81         desc = ptlrpc_prep_bulk(req->rq_connection);
82         if (desc == NULL)
83                 GOTO(out, rc = -ENOMEM);
84
85         bulk = ptlrpc_prep_bulk_page(desc);
86         if (bulk == NULL)
87                 GOTO(cleanup_bulk, rc = -ENOMEM);
88
89         OBD_ALLOC(buf, PAGE_SIZE);
90         if (buf == NULL)
91                 GOTO(cleanup_bulk, rc = -ENOMEM);
92
93         rc = mds_fs_readpage(mds, file, buf, PAGE_SIZE, (loff_t *)&offset);
94
95         if (rc != PAGE_SIZE)
96                 GOTO(cleanup_buf, rc = -EIO);
97
98         bulk->bp_xid = req->rq_xid;
99         bulk->bp_buf = buf;
100         bulk->bp_buflen = PAGE_SIZE;
101         desc->bd_portal = MDS_BULK_PORTAL;
102
103         rc = ptlrpc_send_bulk(desc);
104         if (rc)
105                 GOTO(cleanup_buf, rc);
106
107         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE)) {
108                 CERROR("obd_fail_loc=%x, fail operation rc=%d\n",
109                        OBD_FAIL_MDS_SENDPAGE, rc);
110                 ptlrpc_abort_bulk(desc);
111                 GOTO(cleanup_buf, rc);
112         }
113
114         lwi = LWI_TIMEOUT(obd_timeout * HZ, mds_bulk_timeout, desc);
115         rc = l_wait_event(desc->bd_waitq, desc->bd_flags & PTL_BULK_FL_SENT, &lwi);
116         if (rc) {
117                 if (rc != -ETIMEDOUT)
118                         LBUG();
119                 GOTO(cleanup_buf, rc);
120         }
121
122         EXIT;
123  cleanup_buf:
124         OBD_FREE(buf, PAGE_SIZE);
125  cleanup_bulk:
126         ptlrpc_free_bulk(desc);
127  out:
128         return rc;
129 }
130
131 /*
132  * Look up a named entry in a directory, and get an LDLM lock on it.
133  * 'dir' is a inode for which an LDLM lock has already been taken.
134  *
135  * If we do not need an exclusive or write lock on this entry (e.g.
136  * a read lock for attribute lookup only) then we do not hold the
137  * directory on return.  It is up to the caller to know what type
138  * of lock it is getting, and clean up appropriately.
139  */
140 struct dentry *mds_name2locked_dentry(struct obd_device *obd,
141                                       struct dentry *dir, struct vfsmount **mnt,
142                                       char *name, int namelen, int lock_mode,
143                                       struct lustre_handle *lockh,
144                                       int dir_lock_mode)
145 {
146         struct dentry *dchild;
147         int flags = 0, rc;
148         __u64 res_id[3] = {0};
149         ENTRY;
150
151         down(&dir->d_inode->i_sem);
152         dchild = lookup_one_len(name, dir, namelen);
153         if (IS_ERR(dchild)) {
154                 CERROR("child lookup error %ld\n", PTR_ERR(dchild));
155                 up(&dir->d_inode->i_sem);
156                 LBUG();
157                 RETURN(dchild);
158         }
159         if (dir_lock_mode != LCK_EX && dir_lock_mode != LCK_PW) {
160                 up(&dir->d_inode->i_sem);
161                 ldlm_lock_decref(lockh, dir_lock_mode);
162         }
163
164         if (lock_mode == 0 || !dchild->d_inode)
165                 RETURN(dchild);
166
167         res_id[0] = dchild->d_inode->i_ino;
168         res_id[1] = dchild->d_inode->i_generation;
169         rc = ldlm_match_or_enqueue(NULL, NULL, obd->obd_namespace, NULL,
170                                    res_id, LDLM_PLAIN, NULL, 0, lock_mode,
171                                    &flags, ldlm_completion_ast,
172                                    mds_blocking_ast, NULL, 0, lockh);
173         if (rc != ELDLM_OK) {
174                 l_dput(dchild);
175                 up(&dir->d_inode->i_sem);
176                 RETURN(ERR_PTR(-ENOLCK)); /* XXX translate ldlm code */
177         }
178
179         RETURN(dchild);
180 }
181
182 struct dentry *mds_fid2locked_dentry(struct obd_device *obd, struct ll_fid *fid,
183                                      struct vfsmount **mnt, int lock_mode,
184                                      struct lustre_handle *lockh)
185 {
186         struct mds_obd *mds = &obd->u.mds;
187         struct dentry *de = mds_fid2dentry(mds, fid, mnt), *retval = de;
188         int flags = 0, rc;
189         __u64 res_id[3] = {0};
190         ENTRY;
191
192         if (IS_ERR(de))
193                 RETURN(de);
194
195         res_id[0] = de->d_inode->i_ino;
196         res_id[1] = de->d_inode->i_generation;
197         rc = ldlm_match_or_enqueue(NULL, NULL, obd->obd_namespace, NULL,
198                                    res_id, LDLM_PLAIN, NULL, 0, lock_mode,
199                                    &flags, ldlm_completion_ast,
200                                    mds_blocking_ast, NULL, 0, lockh);
201         if (rc != ELDLM_OK) {
202                 l_dput(de);
203                 retval = ERR_PTR(-ENOLCK); /* XXX translate ldlm code */
204         }
205
206         RETURN(retval);
207 }
208
209 #ifndef DCACHE_DISCONNECTED
210 #define DCACHE_DISCONNECTED DCACHE_NFSD_DISCONNECTED
211 #endif
212
213 /* Look up an entry by inode number. */
214 struct dentry *mds_fid2dentry(struct mds_obd *mds, struct ll_fid *fid,
215                               struct vfsmount **mnt)
216 {
217         /* stolen from NFS */
218         struct super_block *sb = mds->mds_sb;
219         unsigned long ino = fid->id;
220         __u32 generation = fid->generation;
221         struct inode *inode;
222         struct list_head *lp;
223         struct dentry *result;
224
225         if (ino == 0)
226                 RETURN(ERR_PTR(-ESTALE));
227
228         inode = iget(sb, ino);
229         if (inode == NULL)
230                 RETURN(ERR_PTR(-ENOMEM));
231
232         CDEBUG(D_DENTRY, "--> mds_fid2dentry: sb %p\n", inode->i_sb);
233
234         if (is_bad_inode(inode) ||
235             (generation && inode->i_generation != generation)) {
236                 /* we didn't find the right inode.. */
237                 CERROR("bad inode %lu, link: %d ct: %d or version  %u/%u\n",
238                        inode->i_ino, inode->i_nlink,
239                        atomic_read(&inode->i_count), inode->i_generation,
240                        generation);
241                 LBUG();
242                 iput(inode);
243                 RETURN(ERR_PTR(-ESTALE));
244         }
245
246         /* now to find a dentry. If possible, get a well-connected one */
247         if (mnt)
248                 *mnt = mds->mds_vfsmnt;
249         spin_lock(&dcache_lock);
250         list_for_each(lp, &inode->i_dentry) {
251                 result = list_entry(lp, struct dentry, d_alias);
252                 if (!(result->d_flags & DCACHE_DISCONNECTED)) {
253                         dget_locked(result);
254                         result->d_vfs_flags |= DCACHE_REFERENCED;
255                         spin_unlock(&dcache_lock);
256                         iput(inode);
257                         if (mnt)
258                                 mntget(*mnt);
259                         return result;
260                 }
261         }
262         spin_unlock(&dcache_lock);
263         result = d_alloc_root(inode);
264         if (result == NULL) {
265                 iput(inode);
266                 return ERR_PTR(-ENOMEM);
267         }
268         if (mnt)
269                 mntget(*mnt);
270         result->d_flags |= DCACHE_DISCONNECTED;
271         return result;
272 }
273
274 /* Establish a connection to the MDS.
275  *
276  * This will set up an export structure for the client to hold state data
277  * about that client, like open files, the last operation number it did
278  * on the server, etc.
279  */
280 static int mds_connect(struct lustre_handle *conn, struct obd_device *obd,
281                        obd_uuid_t cluuid, struct recovd_obd *recovd,
282                        ptlrpc_recovery_cb_t recover)
283 {
284         struct obd_export *exp;
285         struct mds_export_data *med;
286         struct mds_client_data *mcd;
287         struct list_head *p;
288         int rc;
289         ENTRY;
290
291         if (!conn || !obd || !cluuid)
292                 RETURN(-EINVAL);
293
294         MOD_INC_USE_COUNT;
295
296         spin_lock(&obd->obd_dev_lock);
297         list_for_each(p, &obd->obd_exports) {
298                 exp = list_entry(p, struct obd_export, exp_obd_chain);
299                 mcd = exp->exp_mds_data.med_mcd;
300                 if (!memcmp(cluuid, mcd->mcd_uuid, sizeof(mcd->mcd_uuid))) {
301                         LASSERT(exp->exp_obd == obd);
302
303                         if (!list_empty(&exp->exp_conn_chain)) {
304                                 CERROR("existing uuid/export, list not empty!\n");
305                                 spin_unlock(&obd->obd_dev_lock);
306                                 MOD_DEC_USE_COUNT;
307                                 RETURN(-EALREADY);
308                         }
309                         conn->addr = (__u64) (unsigned long)exp;
310                         conn->cookie = exp->exp_cookie;
311                         spin_unlock(&obd->obd_dev_lock);
312                         CDEBUG(D_INFO, "existing export for UUID '%s' at %p\n",
313                                cluuid, exp);
314                         CDEBUG(D_IOCTL,"connect: addr %Lx cookie %Lx\n",
315                                (long long)conn->addr, (long long)conn->cookie);
316                         MOD_DEC_USE_COUNT;
317                         RETURN(0);
318                 }
319         }
320         spin_unlock(&obd->obd_dev_lock);
321         /* XXX There is a small race between checking the list and adding a
322          * new connection for the same UUID, but the real threat (list
323          * corruption when multiple different clients connect) is solved.
324          */
325         rc = class_connect(conn, obd, cluuid);
326         if (rc)
327                 GOTO(out_dec, rc);
328         exp = class_conn2export(conn);
329         LASSERT(exp);
330         med = &exp->exp_mds_data;
331
332         OBD_ALLOC(mcd, sizeof(*mcd));
333         if (!mcd) {
334                 CERROR("mds: out of memory for client data\n");
335                 GOTO(out_export, rc = -ENOMEM);
336         }
337
338         memcpy(mcd->mcd_uuid, cluuid, sizeof(mcd->mcd_uuid));
339         med->med_mcd = mcd;
340
341         INIT_LIST_HEAD(&med->med_open_head);
342         spin_lock_init(&med->med_open_lock);
343
344         rc = mds_client_add(med, -1);
345         if (rc)
346                 GOTO(out_mdc, rc);
347
348         RETURN(0);
349
350 out_mdc:
351         OBD_FREE(mcd, sizeof(*mcd));
352 out_export:
353         class_disconnect(conn);
354 out_dec:
355         MOD_DEC_USE_COUNT;
356
357         return rc;
358 }
359
360 /* Call with med->med_open_lock held, please. */
361 inline int mds_close_mfd(struct mds_file_data *mfd, struct mds_export_data *med)
362 {
363         struct file *file = mfd->mfd_file;
364         LASSERT(file->private_data == mfd);
365
366         list_del(&mfd->mfd_list);
367         mfd->mfd_servercookie = DEAD_HANDLE_MAGIC;
368         kmem_cache_free(mds_file_cache, mfd);
369
370         return filp_close(file, 0);
371 }
372
373 static int mds_disconnect(struct lustre_handle *conn)
374 {
375         struct obd_export *export = class_conn2export(conn);
376         struct list_head *tmp, *n;
377         struct mds_export_data *med = &export->exp_mds_data;
378         int rc;
379         ENTRY;
380
381         /*
382          * Close any open files.
383          */
384         spin_lock(&med->med_open_lock);
385         list_for_each_safe(tmp, n, &med->med_open_head) {
386                 struct mds_file_data *mfd =
387                         list_entry(tmp, struct mds_file_data, mfd_list);
388                 rc = mds_close_mfd(mfd, med);
389                 if (rc) {
390                         /* XXX better diagnostics, with file path and stuff */
391                         CDEBUG(D_INODE, "Error %d closing mfd %p\n", rc, mfd);
392                 }
393         }
394         spin_unlock(&med->med_open_lock);
395
396         ldlm_cancel_locks_for_export(export);
397         mds_client_free(export);
398
399         rc = class_disconnect(conn);
400         if (!rc)
401                 MOD_DEC_USE_COUNT;
402
403         RETURN(rc);
404 }
405
406 /*
407  * XXX This is NOT guaranteed to flush all transactions to disk (even though
408  *     it is equivalent to calling sync()) because it only _starts_ the flush
409  *     and does not wait for completion.  It's better than nothing though.
410  *     What we really want is a mild form of fsync_dev_lockfs(), but it is
411  *     non-standard, or enabling do_sync_supers in ext3, just for this call.
412  */
413 static void mds_fsync_super(struct super_block *sb)
414 {
415         lock_kernel();
416         lock_super(sb);
417         if (sb->s_dirt && sb->s_op && sb->s_op->write_super)
418                 sb->s_op->write_super(sb);
419         unlock_super(sb);
420         unlock_kernel();
421 }
422
423 static int mds_getstatus(struct ptlrpc_request *req)
424 {
425         struct mds_obd *mds = mds_req2mds(req);
426         struct mds_body *body;
427         int rc, size = sizeof(*body);
428         ENTRY;
429
430         rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
431         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_GETSTATUS_PACK)) {
432                 CERROR("mds: out of memory for message: size=%d\n", size);
433                 req->rq_status = -ENOMEM;
434                 RETURN(0);
435         }
436
437         /* Flush any outstanding transactions to disk so the client will
438          * get the latest last_committed value and can drop their local
439          * requests if they have any.  This would be fsync_super() if it
440          * was exported.
441          */
442         mds_fsync_super(mds->mds_sb);
443
444         body = lustre_msg_buf(req->rq_repmsg, 0);
445         memcpy(&body->fid1, &mds->mds_rootfid, sizeof(body->fid1));
446
447         /* the last_committed and last_xid fields are filled in for all
448          * replies already - no need to do so here also.
449          */
450         RETURN(0);
451 }
452
453 static int mds_getlovinfo(struct ptlrpc_request *req)
454 {
455         struct mds_obd *mds = mds_req2mds(req);
456         struct mds_status_req *streq;
457         struct lov_desc *desc;
458         int tgt_count;
459         int rc, size[2] = {sizeof(*desc)};
460         ENTRY;
461
462         streq = lustre_msg_buf(req->rq_reqmsg, 0);
463         streq->flags = NTOH__u32(streq->flags);
464         streq->repbuf = NTOH__u32(streq->repbuf);
465         size[1] = streq->repbuf;
466
467         rc = lustre_pack_msg(2, size, NULL, &req->rq_replen, &req->rq_repmsg);
468         if (rc) {
469                 CERROR("mds: out of memory for message: size=%d\n", size[1]);
470                 req->rq_status = -ENOMEM;
471                 RETURN(0);
472         }
473
474         desc = lustre_msg_buf(req->rq_repmsg, 0);
475         rc = mds_get_lovdesc(mds, desc);
476         if (rc) {
477                 CERROR("mds_get_lovdesc error %d", rc);
478                 req->rq_status = rc;
479                 RETURN(0);
480         }
481
482         tgt_count = le32_to_cpu(desc->ld_tgt_count);
483         if (tgt_count * sizeof(obd_uuid_t) > streq->repbuf) {
484                 CERROR("too many targets, enlarge client buffers\n");
485                 req->rq_status = -ENOSPC;
486                 RETURN(0);
487         }
488
489         mds->mds_max_mdsize = sizeof(struct lov_mds_md) +
490                 tgt_count * sizeof(struct lov_object_id);
491         rc = mds_get_lovtgts(mds, tgt_count,
492                              lustre_msg_buf(req->rq_repmsg, 1));
493         if (rc) {
494                 CERROR("get_lovtgts error %d\n", rc);
495                 req->rq_status = rc;
496                 RETURN(0);
497         }
498         RETURN(0);
499 }
500
501 int mds_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
502                      void *data, __u32 data_len, int flag)
503 {
504         int do_ast;
505         ENTRY;
506
507         if (flag == LDLM_CB_CANCELING) {
508                 /* Don't need to do anything here. */
509                 RETURN(0);
510         }
511
512         /* XXX layering violation!  -phil */
513         l_lock(&lock->l_resource->lr_namespace->ns_lock);
514         lock->l_flags |= LDLM_FL_CBPENDING;
515         do_ast = (!lock->l_readers && !lock->l_writers);
516         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
517
518         if (do_ast) {
519                 struct lustre_handle lockh;
520                 int rc;
521
522                 LDLM_DEBUG(lock, "already unused, calling ldlm_cli_cancel");
523                 ldlm_lock2handle(lock, &lockh);
524                 rc = ldlm_cli_cancel(&lockh);
525                 if (rc < 0)
526                         CERROR("ldlm_cli_cancel: %d\n", rc);
527         } else
528                 LDLM_DEBUG(lock, "Lock still has references, will be"
529                            "cancelled later");
530         RETURN(0);
531 }
532
533 static int mds_getattr_internal(struct mds_obd *mds, struct dentry *dentry,
534                                 struct ptlrpc_request *req,
535                                 struct mds_body *reqbody, int reply_off)
536 {
537         struct mds_body *body;
538         struct inode *inode = dentry->d_inode;
539         int rc;
540         ENTRY;
541
542         if (inode == NULL)
543                 RETURN(-ENOENT);
544
545         body = lustre_msg_buf(req->rq_repmsg, reply_off);
546
547         mds_pack_inode2fid(&body->fid1, inode);
548         mds_pack_inode2body(body, inode);
549
550         if (S_ISREG(inode->i_mode)) {
551                 struct lov_mds_md *lmm;
552
553                 lmm = lustre_msg_buf(req->rq_repmsg, reply_off + 1);
554                 lmm->lmm_easize = mds->mds_max_mdsize;
555                 rc = mds_fs_get_md(mds, inode, lmm);
556
557                 if (rc < 0) {
558                         if (rc == -ENODATA)
559                                 RETURN(0);
560                         CERROR("mds_fs_get_md failed: %d\n", rc);
561                         RETURN(rc);
562                 }
563                 body->valid |= OBD_MD_FLEASIZE;
564         } else if (S_ISLNK(inode->i_mode) && reqbody->valid & OBD_MD_LINKNAME) {
565                 char *symname = lustre_msg_buf(req->rq_repmsg, reply_off + 1);
566                 int len = req->rq_repmsg->buflens[reply_off + 1];
567
568                 rc = inode->i_op->readlink(dentry, symname, len);
569                 if (rc < 0) {
570                         CERROR("readlink failed: %d\n", rc);
571                         RETURN(rc);
572                 } else
573                         CDEBUG(D_INODE, "read symlink dest %s\n", symname);
574
575                 body->valid |= OBD_MD_LINKNAME;
576         }
577         RETURN(0);
578 }
579
580 static int mds_getattr_name(int offset, struct ptlrpc_request *req)
581 {
582         struct mds_obd *mds = mds_req2mds(req);
583         struct obd_device *obd = req->rq_export->exp_obd;
584         struct obd_run_ctxt saved;
585         struct mds_body *body;
586         struct dentry *de = NULL, *dchild = NULL;
587         struct inode *dir;
588         struct lustre_handle lockh;
589         char *name;
590         int namelen, flags = 0, lock_mode, rc = 0;
591         struct obd_ucred uc;
592         __u64 res_id[3] = {0, 0, 0};
593         ENTRY;
594
595         LASSERT(!strcmp(req->rq_export->exp_obd->obd_type->typ_name, "mds"));
596
597         if (req->rq_reqmsg->bufcount <= offset + 1) {
598                 LBUG();
599                 GOTO(out_pre_de, rc = -EINVAL);
600         }
601
602         body = lustre_msg_buf(req->rq_reqmsg, offset);
603         name = lustre_msg_buf(req->rq_reqmsg, offset + 1);
604         namelen = req->rq_reqmsg->buflens[offset + 1];
605         /* requests were at offset 2, replies go back at 1 */
606         if (offset)
607                 offset = 1;
608
609         uc.ouc_fsuid = body->fsuid;
610         uc.ouc_fsgid = body->fsgid;
611         uc.ouc_cap = body->capability;
612         push_ctxt(&saved, &mds->mds_ctxt, &uc);
613         de = mds_fid2dentry(mds, &body->fid1, NULL);
614         if (IS_ERR(de)) {
615                 LBUG();
616                 GOTO(out_pre_de, rc = -ESTALE);
617         }
618
619         dir = de->d_inode;
620         CDEBUG(D_INODE, "parent ino %ld, name %*s\n", dir->i_ino,namelen,name);
621
622         lock_mode = LCK_PR;
623         res_id[0] = dir->i_ino;
624         res_id[1] = dir->i_generation;
625
626         rc = ldlm_lock_match(obd->obd_namespace, res_id, LDLM_PLAIN,
627                              NULL, 0, lock_mode, &lockh);
628         if (rc == 0) {
629                 LDLM_DEBUG_NOLOCK("enqueue res "LPU64, res_id[0]);
630                 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, NULL,
631                                       res_id, LDLM_PLAIN, NULL, 0, lock_mode,
632                                       &flags, ldlm_completion_ast,
633                                       mds_blocking_ast, NULL, 0, &lockh);
634                 if (rc != ELDLM_OK) {
635                         CERROR("lock enqueue: err: %d\n", rc);
636                         GOTO(out_create_de, rc = -EIO);
637                 }
638         }
639         ldlm_lock_dump((void *)(unsigned long)lockh.addr);
640
641         down(&dir->i_sem);
642         dchild = lookup_one_len(name, de, namelen - 1);
643         if (IS_ERR(dchild)) {
644                 CDEBUG(D_INODE, "child lookup error %ld\n", PTR_ERR(dchild));
645                 up(&dir->i_sem);
646                 LBUG();
647                 GOTO(out_create_dchild, rc = -ESTALE);
648         }
649
650         rc = mds_getattr_internal(mds, dchild, req, body, offset);
651
652         EXIT;
653 out_create_dchild:
654         l_dput(dchild);
655         up(&dir->i_sem);
656         ldlm_lock_decref(&lockh, lock_mode);
657 out_create_de:
658         l_dput(de);
659 out_pre_de:
660         req->rq_status = rc;
661         pop_ctxt(&saved);
662         return 0;
663 }
664
665 static int mds_getattr(int offset, struct ptlrpc_request *req)
666 {
667         struct mds_obd *mds = mds_req2mds(req);
668         struct obd_run_ctxt saved;
669         struct dentry *de;
670         struct inode *inode;
671         struct mds_body *body;
672         struct obd_ucred uc;
673         int rc = 0, size[2] = {sizeof(*body)}, bufcount = 1;
674         ENTRY;
675
676         body = lustre_msg_buf(req->rq_reqmsg, offset);
677         uc.ouc_fsuid = body->fsuid;
678         uc.ouc_fsgid = body->fsgid;
679         uc.ouc_cap = body->capability;
680         push_ctxt(&saved, &mds->mds_ctxt, &uc);
681         de = mds_fid2dentry(mds, &body->fid1, NULL);
682         if (IS_ERR(de)) {
683                 rc = req->rq_status = -ENOENT;
684                 GOTO(out_pop, PTR_ERR(de));
685         }
686
687         inode = de->d_inode;
688         if (S_ISREG(body->fid1.f_type)) {
689                 bufcount = 2;
690                 size[1] = mds->mds_max_mdsize;
691         } else if (body->valid & OBD_MD_LINKNAME) {
692                 bufcount = 2;
693                 size[1] = MIN(inode->i_size + 1, body->size);
694                 CDEBUG(D_INODE, "symlink size: %d, reply space: %d\n",
695                        inode->i_size + 1, body->size);
696         }
697
698         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK)) {
699                 CERROR("failed GETATTR_PACK test\n");
700                 req->rq_status = -ENOMEM;
701                 GOTO(out, rc = -ENOMEM);
702         }
703
704         rc = lustre_pack_msg(bufcount, size, NULL, &req->rq_replen,
705                              &req->rq_repmsg);
706         if (rc) {
707                 CERROR("out of memory or FAIL_MDS_GETATTR_PACK\n");
708                 req->rq_status = rc;
709                 GOTO(out, rc);
710         }
711
712         req->rq_status = mds_getattr_internal(mds, de, req, body, 0);
713
714 out:
715         l_dput(de);
716 out_pop:
717         pop_ctxt(&saved);
718         RETURN(rc);
719 }
720
721 static int mds_statfs(struct ptlrpc_request *req)
722 {
723         struct mds_obd *mds = mds_req2mds(req);
724         struct obd_statfs *osfs;
725         struct statfs sfs;
726         int rc, size = sizeof(*osfs);
727         ENTRY;
728
729         rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen,
730                              &req->rq_repmsg);
731         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK)) {
732                 CERROR("mds: statfs lustre_pack_msg failed: rc = %d\n", rc);
733                 GOTO(out, rc);
734         }
735
736         rc = vfs_statfs(mds->mds_sb, &sfs);
737         if (rc) {
738                 CERROR("mds: statfs failed: rc %d\n", rc);
739                 GOTO(out, rc);
740         }
741         osfs = lustre_msg_buf(req->rq_repmsg, 0);
742         memset(osfs, 0, size);
743         statfs_pack(osfs, &sfs);
744         obd_statfs_pack(osfs, osfs);
745
746 out:
747         req->rq_status = rc;
748         RETURN(0);
749 }
750
751 static struct mds_file_data *mds_handle2mfd(struct lustre_handle *handle)
752 {
753         struct mds_file_data *mfd = NULL;
754
755         if (!handle || !handle->addr)
756                 RETURN(NULL);
757
758         mfd = (struct mds_file_data *)(unsigned long)(handle->addr);
759         if (!kmem_cache_validate(mds_file_cache, mfd))
760                 RETURN(NULL);
761
762         if (mfd->mfd_servercookie != handle->cookie)
763                 RETURN(NULL);
764
765         return mfd;
766 }
767
768 static int mds_store_ea(struct mds_obd *mds, struct ptlrpc_request *req,
769                         struct mds_body *body, struct dentry *de,
770                         struct lov_mds_md *lmm)
771 {
772         struct obd_run_ctxt saved;
773         struct obd_ucred uc;
774         void *handle;
775         int rc, rc2;
776
777         uc.ouc_fsuid = body->fsuid;
778         uc.ouc_fsgid = body->fsgid;
779         uc.ouc_cap = body->capability;
780        push_ctxt(&saved, &mds->mds_ctxt, &uc);
781         handle = mds_fs_start(mds, de->d_inode, MDS_FSOP_SETATTR);
782         if (!handle)
783                 GOTO(out_ea, rc = -ENOMEM);
784
785         rc = mds_fs_set_md(mds, de->d_inode, handle, lmm);
786         if (!rc)
787                 rc = mds_update_last_rcvd(mds, handle, req);
788
789         rc2 = mds_fs_commit(mds, de->d_inode, handle);
790         if (rc2 && !rc)
791                 rc = rc2;
792 out_ea:
793         pop_ctxt(&saved);
794
795         return rc;
796 }
797
798 static int mds_open(struct ptlrpc_request *req)
799 {
800         struct mds_obd *mds = mds_req2mds(req);
801         struct mds_body *body;
802         struct mds_export_data *med;
803         struct mds_file_data *mfd;
804         struct dentry *de;
805         struct file *file;
806         struct vfsmount *mnt;
807         __u32 flags;
808         struct list_head *tmp;
809         int rc, size = sizeof(*body);
810         ENTRY;
811
812         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OPEN_PACK)) {
813                 CERROR("test case OBD_FAIL_MDS_OPEN_PACK\n");
814                 req->rq_status = -ENOMEM;
815                 RETURN(-ENOMEM);
816         }
817
818         rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
819         if (rc) {
820                 CERROR("mds: pack error: rc = %d\n", rc);
821                 req->rq_status = rc;
822                 RETURN(rc);
823         }
824
825         body = lustre_msg_buf(req->rq_reqmsg, 0);
826
827         /* was this animal open already and the client lost the reply? */
828         /* XXX need some way to detect a reopen, to avoid locked list walks */
829         med = &req->rq_export->exp_mds_data;
830         spin_lock(&med->med_open_lock);
831         list_for_each(tmp, &med->med_open_head) {
832                 mfd = list_entry(tmp, typeof(*mfd), mfd_list);
833                 if (!memcmp(&mfd->mfd_clienthandle, &body->handle,
834                             sizeof(mfd->mfd_clienthandle)) &&
835                     body->fid1.id == mfd->mfd_file->f_dentry->d_inode->i_ino) {
836                         de = mfd->mfd_file->f_dentry;
837                         spin_unlock(&med->med_open_lock);
838                         CERROR("Re opening "LPD64"\n", body->fid1.id);
839                         GOTO(out_pack, rc = 0);
840                 }
841         }
842         spin_unlock(&med->med_open_lock);
843
844         mfd = kmem_cache_alloc(mds_file_cache, GFP_KERNEL);
845         if (!mfd) {
846                 CERROR("mds: out of memory\n");
847                 req->rq_status = -ENOMEM;
848                 RETURN(0);
849         }
850
851         de = mds_fid2dentry(mds, &body->fid1, &mnt);
852         if (IS_ERR(de))
853                 GOTO(out_free, rc = PTR_ERR(de));
854
855         /* check if this inode has seen a delayed object creation */
856         if (lustre_msg_get_op_flags(req->rq_reqmsg) & MDS_OPEN_HAS_EA) {
857                 struct lov_mds_md *lmm = lustre_msg_buf(req->rq_reqmsg, 1);
858
859                 rc = mds_store_ea(mds, req, body, de, lmm);
860                 if (rc) {
861                         l_dput(de);
862                         mntput(mnt);
863                         GOTO(out_free, rc);
864                 }
865         }
866
867         flags = body->flags;
868         /* dentry_open does a dput(de) and mntput(mnt) on error */
869         file = dentry_open(de, mnt, flags & ~O_DIRECT);
870         if (IS_ERR(file)) {
871                 rc = PTR_ERR(file);
872                 GOTO(out_free, 0);
873         }
874
875         file->private_data = mfd;
876         mfd->mfd_file = file;
877         memcpy(&mfd->mfd_clienthandle, &body->handle, sizeof(body->handle));
878         get_random_bytes(&mfd->mfd_servercookie, sizeof(mfd->mfd_servercookie));
879         spin_lock(&med->med_open_lock);
880         list_add(&mfd->mfd_list, &med->med_open_head);
881         spin_unlock(&med->med_open_lock);
882
883 out_pack:
884         body = lustre_msg_buf(req->rq_repmsg, 0);
885         mds_pack_inode2fid(&body->fid1, de->d_inode);
886         mds_pack_inode2body(body, de->d_inode);
887         body->handle.addr = (__u64)(unsigned long)mfd;
888         body->handle.cookie = mfd->mfd_servercookie;
889         CDEBUG(D_INODE, "llite file "LPX64": addr %p, cookie "LPX64"\n",
890                mfd->mfd_clienthandle.addr, mfd, mfd->mfd_servercookie);
891         RETURN(0);
892
893 out_free:
894         mfd->mfd_servercookie = DEAD_HANDLE_MAGIC;
895         kmem_cache_free(mds_file_cache, mfd);
896         req->rq_status = rc;
897         RETURN(0);
898 }
899
900 static int mds_close(struct ptlrpc_request *req)
901 {
902         struct mds_export_data *med = &req->rq_export->exp_mds_data;
903         struct mds_body *body;
904         struct mds_file_data *mfd;
905         int rc;
906         ENTRY;
907
908         body = lustre_msg_buf(req->rq_reqmsg, 0);
909
910         mfd = mds_handle2mfd(&body->handle);
911         if (!mfd) {
912                 CERROR("no handle for file close "LPD64
913                        ": addr "LPX64", cookie "LPX64"\n",
914                        body->fid1.id, body->handle.addr, body->handle.cookie);
915                 RETURN(-ESTALE);
916         }
917
918         spin_lock(&med->med_open_lock);
919         req->rq_status = mds_close_mfd(mfd, med);
920         spin_unlock(&med->med_open_lock);
921
922         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_CLOSE_PACK)) {
923                 CERROR("test case OBD_FAIL_MDS_CLOSE_PACK\n");
924                 req->rq_status = -ENOMEM;
925                 RETURN(-ENOMEM);
926         }
927
928         rc = lustre_pack_msg(0, NULL, NULL, &req->rq_replen, &req->rq_repmsg);
929         if (rc) {
930                 CERROR("mds: lustre_pack_msg: rc = %d\n", rc);
931                 req->rq_status = rc;
932         }
933
934         RETURN(0);
935 }
936
937 static int mds_readpage(struct ptlrpc_request *req)
938 {
939         struct mds_obd *mds = mds_req2mds(req);
940         struct vfsmount *mnt;
941         struct dentry *de;
942         struct file *file;
943         struct mds_body *body, *repbody;
944         struct obd_run_ctxt saved;
945         int rc, size = sizeof(*body);
946         struct obd_ucred uc;
947         ENTRY;
948
949         rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
950         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK)) {
951                 CERROR("mds: out of memory\n");
952                 GOTO(out, rc = -ENOMEM);
953         }
954
955         body = lustre_msg_buf(req->rq_reqmsg, 0);
956         uc.ouc_fsuid = body->fsuid;
957         uc.ouc_fsgid = body->fsgid;
958         uc.ouc_cap = body->capability;
959         push_ctxt(&saved, &mds->mds_ctxt, &uc);
960         de = mds_fid2dentry(mds, &body->fid1, &mnt);
961         if (IS_ERR(de))
962                 GOTO(out_pop, rc = PTR_ERR(de));
963
964         CDEBUG(D_INODE, "ino %ld\n", de->d_inode->i_ino);
965
966         file = dentry_open(de, mnt, O_RDONLY | O_LARGEFILE);
967         /* note: in case of an error, dentry_open puts dentry */
968         if (IS_ERR(file))
969                 GOTO(out_pop, rc = PTR_ERR(file));
970
971         repbody = lustre_msg_buf(req->rq_repmsg, 0);
972         repbody->size = file->f_dentry->d_inode->i_size;
973         repbody->valid = OBD_MD_FLSIZE;
974
975         /* to make this asynchronous make sure that the handling function
976            doesn't send a reply when this function completes. Instead a
977            callback function would send the reply */
978         /* note: in case of an error, dentry_open puts dentry */
979         rc = mds_sendpage(req, file, body->size);
980
981         filp_close(file, 0);
982 out_pop:
983         pop_ctxt(&saved);
984 out:
985         req->rq_status = rc;
986         RETURN(0);
987 }
988
989 int mds_reint(int offset, struct ptlrpc_request *req)
990 {
991         int rc;
992         struct mds_update_record rec;
993
994         rc = mds_update_unpack(req, offset, &rec);
995         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNPACK)) {
996                 CERROR("invalid record\n");
997                 req->rq_status = -EINVAL;
998                 RETURN(0);
999         }
1000         /* rc will be used to interrupt a for loop over multiple records */
1001         rc = mds_reint_rec(&rec, offset, req);
1002         return rc;
1003 }
1004
1005 int mds_handle(struct ptlrpc_request *req)
1006 {
1007         int rc;
1008         ENTRY;
1009
1010         rc = lustre_unpack_msg(req->rq_reqmsg, req->rq_reqlen);
1011         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_HANDLE_UNPACK)) {
1012                 CERROR("lustre_mds: Invalid request\n");
1013                 GOTO(out, rc);
1014         }
1015
1016         if (req->rq_reqmsg->opc != MDS_CONNECT && req->rq_export == NULL)
1017                 GOTO(out, rc = -ENOTCONN);
1018
1019         LASSERT(!strcmp(req->rq_obd->obd_type->typ_name, LUSTRE_MDT_NAME));
1020
1021         switch (req->rq_reqmsg->opc) {
1022         case MDS_CONNECT:
1023                 CDEBUG(D_INODE, "connect\n");
1024                 OBD_FAIL_RETURN(OBD_FAIL_MDS_CONNECT_NET, 0);
1025                 rc = target_handle_connect(req);
1026                 break;
1027
1028         case MDS_DISCONNECT:
1029                 CDEBUG(D_INODE, "disconnect\n");
1030                 OBD_FAIL_RETURN(OBD_FAIL_MDS_DISCONNECT_NET, 0);
1031                 rc = target_handle_disconnect(req);
1032                 goto out;
1033
1034         case MDS_GETSTATUS:
1035                 CDEBUG(D_INODE, "getstatus\n");
1036                 OBD_FAIL_RETURN(OBD_FAIL_MDS_GETSTATUS_NET, 0);
1037                 rc = mds_getstatus(req);
1038                 break;
1039
1040         case MDS_GETLOVINFO:
1041                 CDEBUG(D_INODE, "getlovinfo\n");
1042                 rc = mds_getlovinfo(req);
1043                 break;
1044
1045         case MDS_GETATTR:
1046                 CDEBUG(D_INODE, "getattr\n");
1047                 OBD_FAIL_RETURN(OBD_FAIL_MDS_GETATTR_NET, 0);
1048                 rc = mds_getattr(0, req);
1049                 break;
1050
1051         case MDS_STATFS:
1052                 CDEBUG(D_INODE, "statfs\n");
1053                 OBD_FAIL_RETURN(OBD_FAIL_MDS_STATFS_NET, 0);
1054                 rc = mds_statfs(req);
1055                 break;
1056
1057         case MDS_READPAGE:
1058                 CDEBUG(D_INODE, "readpage\n");
1059                 OBD_FAIL_RETURN(OBD_FAIL_MDS_READPAGE_NET, 0);
1060                 rc = mds_readpage(req);
1061
1062                 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
1063                         return 0;
1064                 break;
1065
1066         case MDS_REINT: {
1067                 int size = sizeof(struct mds_body);
1068                 CDEBUG(D_INODE, "reint\n");
1069                 OBD_FAIL_RETURN(OBD_FAIL_MDS_REINT_NET, 0);
1070
1071                 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen,
1072                                      &req->rq_repmsg);
1073                 if (rc) {
1074                         req->rq_status = rc;
1075                         break;
1076                 }
1077                 rc = mds_reint(0, req);
1078                 OBD_FAIL_RETURN(OBD_FAIL_MDS_REINT_NET_REP, 0);
1079                 break;
1080                 }
1081
1082         case MDS_OPEN:
1083                 CDEBUG(D_INODE, "open\n");
1084                 OBD_FAIL_RETURN(OBD_FAIL_MDS_OPEN_NET, 0);
1085                 rc = mds_open(req);
1086                 break;
1087
1088         case MDS_CLOSE:
1089                 CDEBUG(D_INODE, "close\n");
1090                 OBD_FAIL_RETURN(OBD_FAIL_MDS_CLOSE_NET, 0);
1091                 rc = mds_close(req);
1092                 break;
1093
1094         case LDLM_ENQUEUE:
1095                 CDEBUG(D_INODE, "enqueue\n");
1096                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_ENQUEUE, 0);
1097                 rc = ldlm_handle_enqueue(req);
1098                 break;
1099         case LDLM_CONVERT:
1100                 CDEBUG(D_INODE, "convert\n");
1101                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_CONVERT, 0);
1102                 rc = ldlm_handle_convert(req);
1103                 break;
1104         case LDLM_BL_CALLBACK:
1105         case LDLM_CP_CALLBACK:
1106                 CDEBUG(D_INODE, "callback\n");
1107                 CERROR("callbacks should not happen on MDS\n");
1108                 LBUG();
1109                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_BL_CALLBACK, 0);
1110                 break;
1111         default:
1112                 rc = ptlrpc_error(req->rq_svc, req);
1113                 RETURN(rc);
1114         }
1115
1116         EXIT;
1117
1118         if (!rc) {
1119                 struct mds_export_data *med = &req->rq_export->exp_mds_data;
1120                 struct mds_obd *mds = mds_req2mds(req);
1121
1122                 req->rq_repmsg->last_xid =
1123                         HTON__u64(le64_to_cpu(med->med_mcd->mcd_last_xid));
1124                 req->rq_repmsg->last_committed =
1125                         HTON__u64(mds->mds_last_committed);
1126                 CDEBUG(D_INFO, "last_rcvd ~%Lu, last_committed %Lu, xid %d\n",
1127                        (unsigned long long)mds->mds_last_rcvd,
1128                        (unsigned long long)mds->mds_last_committed,
1129                        cpu_to_le32(req->rq_xid));
1130         }
1131  out:
1132         if (rc) {
1133                 CERROR("mds: processing error (opcode %d): %d\n",
1134                        req->rq_reqmsg->opc, rc);
1135                 ptlrpc_error(req->rq_svc, req);
1136         } else {
1137                 CDEBUG(D_NET, "sending reply\n");
1138                 ptlrpc_reply(req->rq_svc, req);
1139         }
1140         return 0;
1141 }
1142
1143 /* Update the server data on disk.  This stores the new mount_count and
1144  * also the last_rcvd value to disk.  If we don't have a clean shutdown,
1145  * then the server last_rcvd value may be less than that of the clients.
1146  * This will alert us that we may need to do client recovery.
1147  *
1148  * Assumes we are already in the server filesystem context.
1149  *
1150  * Also assumes for mds_last_rcvd that we are not modifying it (no locking).
1151  */
1152 static
1153 int mds_update_server_data(struct mds_obd *mds)
1154 {
1155         struct mds_server_data *msd = mds->mds_server_data;
1156         struct file *filp = mds->mds_rcvd_filp;
1157         loff_t off = 0;
1158         int rc;
1159
1160         msd->msd_last_rcvd = cpu_to_le64(mds->mds_last_rcvd);
1161         msd->msd_mount_count = cpu_to_le64(mds->mds_mount_count);
1162
1163         CDEBUG(D_SUPER, "MDS mount_count is %Lu, last_rcvd is %Lu\n",
1164                (unsigned long long)mds->mds_mount_count,
1165                (unsigned long long)mds->mds_last_rcvd);
1166         rc = lustre_fwrite(filp, (char *)msd, sizeof(*msd), &off);
1167         if (rc != sizeof(*msd)) {
1168                 CERROR("error writing MDS server data: rc = %d\n", rc);
1169                 if (rc > 0)
1170                         RETURN(-EIO);
1171                 RETURN(rc);
1172         }
1173 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
1174         rc = fsync_dev(filp->f_dentry->d_inode->i_rdev);
1175 #else
1176         rc = file_fsync(filp,  filp->f_dentry, 1);
1177 #endif
1178         if (rc)
1179                 CERROR("error flushing MDS server data: rc = %d\n", rc);
1180
1181         return 0;
1182 }
1183
1184 /* Do recovery actions for the MDS */
1185 static int mds_recover(struct obd_device *obddev)
1186 {
1187         struct mds_obd *mds = &obddev->u.mds;
1188         struct obd_run_ctxt saved;
1189         int rc;
1190
1191         /* This happens at the end when recovery is complete */
1192         ++mds->mds_mount_count;
1193         push_ctxt(&saved, &mds->mds_ctxt, NULL);
1194         rc = mds_update_server_data(mds);
1195         pop_ctxt(&saved);
1196
1197         return rc;
1198 }
1199
1200 /* mount the file system (secretly) */
1201 static int mds_setup(struct obd_device *obddev, obd_count len, void *buf)
1202 {
1203         struct obd_ioctl_data* data = buf;
1204         struct mds_obd *mds = &obddev->u.mds;
1205         struct vfsmount *mnt;
1206         int rc = 0;
1207         ENTRY;
1208
1209         MOD_INC_USE_COUNT;
1210 #ifdef CONFIG_DEV_RDONLY
1211         dev_clear_rdonly(2);
1212 #endif
1213         if (!data->ioc_inlbuf1 || !data->ioc_inlbuf2)
1214                 GOTO(err_dec, rc = -EINVAL);
1215
1216         mds->mds_fstype = strdup(data->ioc_inlbuf2);
1217
1218         mnt = do_kern_mount(mds->mds_fstype, 0, data->ioc_inlbuf1, NULL);
1219         if (IS_ERR(mnt)) {
1220                 rc = PTR_ERR(mnt);
1221                 CERROR("do_kern_mount failed: rc = %d\n", rc);
1222                 GOTO(err_kfree, rc);
1223         }
1224
1225         CERROR("%s: mnt is %p\n", data->ioc_inlbuf1, mnt);
1226         mds->mds_sb = mnt->mnt_root->d_inode->i_sb;
1227         if (!mds->mds_sb)
1228                 GOTO(err_put, rc = -ENODEV);
1229
1230         spin_lock_init(&mds->mds_last_lock);
1231         mds->mds_max_mdsize = sizeof(struct lov_mds_md);
1232         rc = mds_fs_setup(obddev, mnt);
1233         if (rc) {
1234                 CERROR("MDS filesystem method init failed: rc = %d\n", rc);
1235                 GOTO(err_put, rc);
1236         }
1237
1238         obddev->obd_namespace =
1239                 ldlm_namespace_new("mds_server", LDLM_NAMESPACE_SERVER);
1240         if (obddev->obd_namespace == NULL) {
1241                 mds_cleanup(obddev);
1242                 GOTO(err_fs, rc = -ENOMEM);
1243         }
1244
1245
1246         rc = mds_recover(obddev);
1247         if (rc)
1248                 GOTO(err_fs, rc);
1249
1250         ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
1251                            "mds_ldlm_client", &obddev->obd_ldlm_client);
1252
1253         RETURN(0);
1254
1255 err_fs:
1256         mds_fs_cleanup(obddev);
1257 err_put:
1258         unlock_kernel();
1259         mntput(mds->mds_vfsmnt);
1260         mds->mds_sb = 0;
1261         lock_kernel();
1262 err_kfree:
1263         kfree(mds->mds_fstype);
1264 err_dec:
1265         MOD_DEC_USE_COUNT;
1266         RETURN(rc);
1267 }
1268
1269 static int mds_cleanup(struct obd_device *obddev)
1270 {
1271         struct super_block *sb;
1272         struct mds_obd *mds = &obddev->u.mds;
1273         struct obd_run_ctxt saved;
1274         ENTRY;
1275
1276         sb = mds->mds_sb;
1277         if (!mds->mds_sb)
1278                 RETURN(0);
1279
1280         push_ctxt(&saved, &mds->mds_ctxt, NULL);
1281         mds_update_server_data(mds);
1282
1283         if (mds->mds_rcvd_filp) {
1284                 int rc = filp_close(mds->mds_rcvd_filp, 0);
1285                 mds->mds_rcvd_filp = NULL;
1286
1287                 if (rc)
1288                         CERROR("last_rcvd file won't close, rc=%d\n", rc);
1289         }
1290         pop_ctxt(&saved);
1291
1292         unlock_kernel();
1293         mntput(mds->mds_vfsmnt);
1294         mds->mds_sb = 0;
1295         kfree(mds->mds_fstype);
1296
1297         ldlm_namespace_free(obddev->obd_namespace);
1298
1299         lock_kernel();
1300 #ifdef CONFIG_DEV_RDONLY
1301         dev_clear_rdonly(2);
1302 #endif
1303         mds_fs_cleanup(obddev);
1304
1305         MOD_DEC_USE_COUNT;
1306         RETURN(0);
1307 }
1308
1309 static int ldlm_intent_policy(struct ldlm_lock *lock, void *req_cookie,
1310                               ldlm_mode_t mode, int flags, void *data)
1311 {
1312         struct ptlrpc_request *req = req_cookie;
1313         int rc = 0;
1314         ENTRY;
1315
1316         if (!req_cookie)
1317                 RETURN(0);
1318
1319         if (req->rq_reqmsg->bufcount > 1) {
1320                 /* an intent needs to be considered */
1321                 struct ldlm_intent *it = lustre_msg_buf(req->rq_reqmsg, 1);
1322                 struct mds_obd *mds= &req->rq_export->exp_obd->u.mds;
1323                 struct mds_body *mds_rep;
1324                 struct ldlm_reply *rep;
1325                 __u64 new_resid[3] = {0, 0, 0}, old_res;
1326                 int rc, size[3] = {sizeof(struct ldlm_reply),
1327                                                   sizeof(struct mds_body),
1328                                                   mds->mds_max_mdsize};
1329
1330                 it->opc = NTOH__u64(it->opc);
1331
1332                 LDLM_DEBUG(lock, "intent policy, opc: %s",
1333                            ldlm_it2str(it->opc));
1334
1335                 rc = lustre_pack_msg(3, size, NULL, &req->rq_replen,
1336                                      &req->rq_repmsg);
1337                 if (rc) {
1338                         rc = req->rq_status = -ENOMEM;
1339                         RETURN(rc);
1340                 }
1341
1342                 rep = lustre_msg_buf(req->rq_repmsg, 0);
1343                 rep->lock_policy_res1 = 1;
1344
1345                 /* execute policy */
1346                 switch ((long)it->opc) {
1347                 case IT_CREAT|IT_OPEN:
1348                         rc = mds_reint(2, req);
1349                         if (rc || (req->rq_status != 0 &&
1350                                    req->rq_status != -EEXIST)) {
1351                                 rep->lock_policy_res2 = req->rq_status;
1352                                 RETURN(ELDLM_LOCK_ABORTED);
1353                         }
1354                         break;
1355                 case IT_CREAT:
1356                 case IT_MKDIR:
1357                 case IT_MKNOD:
1358                 case IT_RENAME2:
1359                 case IT_LINK2:
1360                 case IT_RMDIR:
1361                 case IT_SYMLINK:
1362                 case IT_UNLINK:
1363                         rc = mds_reint(2, req);
1364                         if (rc || (req->rq_status != 0 &&
1365                                    req->rq_status != -EISDIR &&
1366                                    req->rq_status != -ENOTDIR)) {
1367                                 rep->lock_policy_res2 = req->rq_status;
1368                                 RETURN(ELDLM_LOCK_ABORTED);
1369                         }
1370                         break;
1371                 case IT_GETATTR:
1372                 case IT_LOOKUP:
1373                 case IT_OPEN:
1374                 case IT_READDIR:
1375                 case IT_READLINK:
1376                 case IT_RENAME:
1377                 case IT_LINK:
1378                 case IT_SETATTR:
1379                         rc = mds_getattr_name(2, req);
1380                         /* FIXME: we need to sit down and decide on who should
1381                          * set req->rq_status, who should return negative and
1382                          * positive return values, and what they all mean. */
1383                         if (rc || req->rq_status != 0) {
1384                                 rep->lock_policy_res2 = req->rq_status;
1385                                 RETURN(ELDLM_LOCK_ABORTED);
1386                         }
1387                         break;
1388                 case IT_READDIR|IT_OPEN:
1389                         LBUG();
1390                         break;
1391                 default:
1392                         CERROR("Unhandled intent "LPD64"\n", it->opc);
1393                         LBUG();
1394                 }
1395
1396                 /* We don't bother returning a lock to the client for a file
1397                  * or directory we are removing.
1398                  *
1399                  * As for link and rename, there is no reason for the client
1400                  * to get a lock on the target at this point.  If they are
1401                  * going to modify the file/directory later they will get a
1402                  * lock at that time.
1403                  */
1404                 if (it->opc & (IT_UNLINK | IT_RMDIR | IT_LINK | IT_LINK2 |
1405                                IT_RENAME | IT_RENAME2))
1406                         RETURN(ELDLM_LOCK_ABORTED);
1407
1408                 rep->lock_policy_res2 = req->rq_status;
1409                 mds_rep = lustre_msg_buf(req->rq_repmsg, 1);
1410
1411                 /* If the client is about to open a file that doesn't have an MD
1412                  * stripe record, it's going to need a write lock. */
1413                 if (it->opc & IT_OPEN) {
1414                         struct lov_mds_md *lmm =
1415                                 lustre_msg_buf(req->rq_repmsg, 2);
1416                         if (lmm->lmm_easize == 0) {
1417                                 LDLM_DEBUG(lock, "open with no EA; returning PW"
1418                                            " lock");
1419                                 lock->l_req_mode = LCK_PW;
1420                         }
1421                 }
1422
1423                 if (flags & LDLM_FL_INTENT_ONLY) {
1424                         LDLM_DEBUG(lock, "INTENT_ONLY, aborting lock");
1425                         RETURN(ELDLM_LOCK_ABORTED);
1426                 }
1427                 /* Give the client a lock on the child object, instead of the
1428                  * parent that it requested. */
1429                 new_resid[0] = NTOH__u32(mds_rep->ino);
1430                 new_resid[1] = NTOH__u32(mds_rep->generation);
1431                 if (new_resid[0] == 0)
1432                         LBUG();
1433                 old_res = lock->l_resource->lr_name[0];
1434
1435                 ldlm_lock_change_resource(lock, new_resid);
1436                 if (lock->l_resource == NULL) {
1437                         LBUG();
1438                         RETURN(-ENOMEM);
1439                 }
1440                 LDLM_DEBUG(lock, "intent policy, old res %ld",
1441                            (long)old_res);
1442                 RETURN(ELDLM_LOCK_CHANGED);
1443         } else {
1444                 int size = sizeof(struct ldlm_reply);
1445                 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen,
1446                                      &req->rq_repmsg);
1447                 if (rc) {
1448                         LBUG();
1449                         RETURN(-ENOMEM);
1450                 }
1451         }
1452         RETURN(rc);
1453 }
1454
1455 int mds_attach(struct obd_device *dev,
1456                obd_count len, void *data)
1457 {
1458         int rc;
1459         rc = lprocfs_reg_obd(dev, (struct lprocfs_vars*)status_var_nm_1,
1460                              (void*)dev);
1461         return rc;
1462 }
1463
1464 int mds_detach(struct obd_device *dev)
1465 {
1466         int rc;
1467         rc = lprocfs_dereg_obd(dev);
1468         return rc;
1469
1470 }
1471
1472 #define MDT_NUM_THREADS 8
1473 static int mdt_setup(struct obd_device *obddev, obd_count len, void *buf)
1474 {
1475         int i;
1476         //        struct obd_ioctl_data* data = buf;
1477         struct mds_obd *mds = &obddev->u.mds;
1478         int rc = 0;
1479         ENTRY;
1480
1481         MOD_INC_USE_COUNT;
1482
1483         mds->mds_service = ptlrpc_init_svc(MDS_NEVENTS, MDS_NBUFS,
1484                                            MDS_BUFSIZE, MDS_MAXREQSIZE,
1485                                            MDS_REQUEST_PORTAL, MDC_REPLY_PORTAL,
1486                                            "self", mds_handle, "mds");
1487         if (!mds->mds_service) {
1488                 CERROR("failed to start service\n");
1489                 GOTO(err_dec, rc = -EINVAL);
1490         }
1491
1492         for (i = 0; i < MDT_NUM_THREADS; i++) {
1493                 char name[32];
1494                 sprintf(name, "lustre_MDT_%02d", i);
1495                 rc = ptlrpc_start_thread(obddev, mds->mds_service, name);
1496                 if (rc) {
1497                         CERROR("cannot start MDT thread #%d: rc %d\n", i, rc);
1498                         GOTO(err_thread, rc);
1499                 }
1500         }
1501
1502         RETURN(0);
1503
1504 err_thread:
1505         ptlrpc_stop_all_threads(mds->mds_service);
1506         ptlrpc_unregister_service(mds->mds_service);
1507 err_dec:
1508         MOD_DEC_USE_COUNT;
1509         RETURN(rc);
1510 }
1511
1512
1513 static int mdt_cleanup(struct obd_device *obddev)
1514 {
1515         struct mds_obd *mds = &obddev->u.mds;
1516         ENTRY;
1517
1518         ptlrpc_stop_all_threads(mds->mds_service);
1519         ptlrpc_unregister_service(mds->mds_service);
1520
1521         MOD_DEC_USE_COUNT;
1522         RETURN(0);
1523 }
1524
1525 extern int mds_iocontrol(long cmd, struct lustre_handle *conn,
1526                          int len, void *karg, void *uarg);
1527
1528 /* use obd ops to offer management infrastructure */
1529 static struct obd_ops mds_obd_ops = {
1530         o_attach:      mds_attach,
1531         o_detach:      mds_detach,
1532         o_connect:     mds_connect,
1533         o_disconnect:  mds_disconnect,
1534         o_setup:       mds_setup,
1535         o_cleanup:     mds_cleanup,
1536         o_iocontrol:   mds_iocontrol
1537 };
1538
1539 static struct obd_ops mdt_obd_ops = {
1540         o_setup:       mdt_setup,
1541         o_cleanup:     mdt_cleanup,
1542 };
1543
1544
1545 static int __init mds_init(void)
1546 {
1547
1548         mds_file_cache = kmem_cache_create("ll_mds_file_data",
1549                                            sizeof(struct mds_file_data),
1550                                            0, 0, NULL, NULL);
1551         if (mds_file_cache == NULL)
1552                 return -ENOMEM;
1553
1554         class_register_type(&mds_obd_ops,
1555                             (struct lprocfs_vars*)status_class_var,
1556                             LUSTRE_MDS_NAME);
1557         class_register_type(&mdt_obd_ops, 0, LUSTRE_MDT_NAME);
1558
1559         ldlm_register_intent(ldlm_intent_policy);
1560
1561         return 0;
1562
1563 }
1564
1565 static void __exit mds_exit(void)
1566 {
1567
1568
1569         ldlm_unregister_intent();
1570         class_unregister_type(LUSTRE_MDS_NAME);
1571         class_unregister_type(LUSTRE_MDT_NAME);
1572         if (kmem_cache_destroy(mds_file_cache))
1573                 CERROR("couldn't free MDS file cache\n");
1574
1575 }
1576
1577 MODULE_AUTHOR("Cluster File Systems <info@clusterfs.com>");
1578 MODULE_DESCRIPTION("Lustre Metadata Server (MDS) v0.01");
1579 MODULE_LICENSE("GPL");
1580
1581 module_init(mds_init);
1582 module_exit(mds_exit);