Whamcloud - gitweb
Lproc-snmp code drop
[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 lprocfs_vars_t status_var_nm_1[];
53 extern lprocfs_vars_t 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                                 /* XXX should we MOD_DEC_USE_COUNT; here? */
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         push_ctxt(&saved, &mds->mds_ctxt, &uc);
612         de = mds_fid2dentry(mds, &body->fid1, NULL);
613         if (IS_ERR(de)) {
614                 LBUG();
615                 GOTO(out_pre_de, rc = -ESTALE);
616         }
617
618         dir = de->d_inode;
619         CDEBUG(D_INODE, "parent ino %ld, name %*s\n", dir->i_ino,namelen,name);
620
621         lock_mode = (req->rq_reqmsg->opc == MDS_REINT) ? LCK_CW : LCK_PW;
622         res_id[0] = dir->i_ino;
623         res_id[1] = dir->i_generation;
624
625         rc = ldlm_lock_match(obd->obd_namespace, res_id, LDLM_PLAIN,
626                              NULL, 0, lock_mode, &lockh);
627         if (rc == 0) {
628                 LDLM_DEBUG_NOLOCK("enqueue res "LPU64, res_id[0]);
629                 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, NULL,
630                                       res_id, LDLM_PLAIN, NULL, 0, lock_mode,
631                                       &flags, ldlm_completion_ast,
632                                       mds_blocking_ast, NULL, 0, &lockh);
633                 if (rc != ELDLM_OK) {
634                         CERROR("lock enqueue: err: %d\n", rc);
635                         GOTO(out_create_de, rc = -EIO);
636                 }
637         }
638         ldlm_lock_dump((void *)(unsigned long)lockh.addr);
639
640         down(&dir->i_sem);
641         dchild = lookup_one_len(name, de, namelen - 1);
642         if (IS_ERR(dchild)) {
643                 CDEBUG(D_INODE, "child lookup error %ld\n", PTR_ERR(dchild));
644                 up(&dir->i_sem);
645                 LBUG();
646                 GOTO(out_create_dchild, rc = -ESTALE);
647         }
648
649         rc = mds_getattr_internal(mds, dchild, req, body, offset);
650
651         EXIT;
652 out_create_dchild:
653         l_dput(dchild);
654         up(&dir->i_sem);
655         ldlm_lock_decref(&lockh, lock_mode);
656 out_create_de:
657         l_dput(de);
658 out_pre_de:
659         req->rq_status = rc;
660         pop_ctxt(&saved);
661         return 0;
662 }
663
664 static int mds_getattr(int offset, struct ptlrpc_request *req)
665 {
666         struct mds_obd *mds = mds_req2mds(req);
667         struct obd_run_ctxt saved;
668         struct dentry *de;
669         struct inode *inode;
670         struct mds_body *body;
671         struct obd_ucred uc;
672         int rc = 0, size[2] = {sizeof(*body)}, bufcount = 1;
673         ENTRY;
674
675         body = lustre_msg_buf(req->rq_reqmsg, offset);
676         uc.ouc_fsuid = body->fsuid;
677         uc.ouc_fsgid = body->fsgid;
678         push_ctxt(&saved, &mds->mds_ctxt, &uc);
679         de = mds_fid2dentry(mds, &body->fid1, NULL);
680         if (IS_ERR(de)) {
681                 rc = req->rq_status = -ENOENT;
682                 GOTO(out_pop, PTR_ERR(de));
683         }
684
685         inode = de->d_inode;
686         if (S_ISREG(body->fid1.f_type)) {
687                 bufcount = 2;
688                 size[1] = mds->mds_max_mdsize;
689         } else if (body->valid & OBD_MD_LINKNAME) {
690                 bufcount = 2;
691                 size[1] = MIN(inode->i_size + 1, body->size);
692                 CDEBUG(D_INODE, "symlink size: %d, reply space: %d\n",
693                        inode->i_size + 1, body->size);
694         }
695
696         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK)) {
697                 CERROR("failed GETATTR_PACK test\n");
698                 req->rq_status = -ENOMEM;
699                 GOTO(out, rc = -ENOMEM);
700         }
701
702         rc = lustre_pack_msg(bufcount, size, NULL, &req->rq_replen,
703                              &req->rq_repmsg);
704         if (rc) {
705                 CERROR("out of memory or FAIL_MDS_GETATTR_PACK\n");
706                 req->rq_status = rc;
707                 GOTO(out, rc);
708         }
709
710         req->rq_status = mds_getattr_internal(mds, de, req, body, 0);
711
712 out:
713         l_dput(de);
714 out_pop:
715         pop_ctxt(&saved);
716         RETURN(rc);
717 }
718
719 static int mds_statfs(struct ptlrpc_request *req)
720 {
721         struct mds_obd *mds = mds_req2mds(req);
722         struct obd_statfs *osfs;
723         struct statfs sfs;
724         int rc, size = sizeof(*osfs);
725         ENTRY;
726
727         rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen,
728                              &req->rq_repmsg);
729         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK)) {
730                 CERROR("mds: statfs lustre_pack_msg failed: rc = %d\n", rc);
731                 GOTO(out, rc);
732         }
733
734         rc = vfs_statfs(mds->mds_sb, &sfs);
735         if (rc) {
736                 CERROR("mds: statfs failed: rc %d\n", rc);
737                 GOTO(out, rc);
738         }
739         osfs = lustre_msg_buf(req->rq_repmsg, 0);
740         memset(osfs, 0, size);
741         statfs_pack(osfs, &sfs);
742         obd_statfs_pack(osfs, osfs);
743
744 out:
745         req->rq_status = rc;
746         RETURN(0);
747 }
748
749 static struct mds_file_data *mds_handle2mfd(struct lustre_handle *handle)
750 {
751         struct mds_file_data *mfd = NULL;
752
753         if (!handle || !handle->addr)
754                 RETURN(NULL);
755
756         mfd = (struct mds_file_data *)(unsigned long)(handle->addr);
757         if (!kmem_cache_validate(mds_file_cache, mfd))
758                 RETURN(NULL);
759
760         if (mfd->mfd_servercookie != handle->cookie)
761                 RETURN(NULL);
762
763         return mfd;
764 }
765
766 static int mds_store_ea(struct mds_obd *mds, struct ptlrpc_request *req,
767                         struct mds_body *body, struct dentry *de,
768                         struct lov_mds_md *lmm)
769 {
770         struct obd_run_ctxt saved;
771         struct obd_ucred uc;
772         void *handle;
773         int rc, rc2;
774
775         uc.ouc_fsuid = body->fsuid;
776         uc.ouc_fsgid = body->fsgid;
777         push_ctxt(&saved, &mds->mds_ctxt, &uc);
778         handle = mds_fs_start(mds, de->d_inode, MDS_FSOP_SETATTR);
779         if (!handle)
780                 GOTO(out_ea, rc = -ENOMEM);
781
782         rc = mds_fs_set_md(mds, de->d_inode, handle, lmm);
783         if (!rc)
784                 rc = mds_update_last_rcvd(mds, handle, req);
785
786         rc2 = mds_fs_commit(mds, de->d_inode, handle);
787         if (rc2 && !rc)
788                 rc = rc2;
789 out_ea:
790         pop_ctxt(&saved);
791
792         return rc;
793 }
794
795 static int mds_open(struct ptlrpc_request *req)
796 {
797         struct mds_obd *mds = mds_req2mds(req);
798         struct mds_body *body;
799         struct mds_export_data *med;
800         struct mds_file_data *mfd;
801         struct dentry *de;
802         struct file *file;
803         struct vfsmount *mnt;
804         __u32 flags;
805         struct list_head *tmp;
806         int rc, size = sizeof(*body);
807         ENTRY;
808
809         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OPEN_PACK)) {
810                 CERROR("test case OBD_FAIL_MDS_OPEN_PACK\n");
811                 req->rq_status = -ENOMEM;
812                 RETURN(-ENOMEM);
813         }
814
815         rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
816         if (rc) {
817                 CERROR("mds: pack error: rc = %d\n", rc);
818                 req->rq_status = rc;
819                 RETURN(rc);
820         }
821
822         body = lustre_msg_buf(req->rq_reqmsg, 0);
823
824         /* was this animal open already and the client lost the reply? */
825         /* XXX need some way to detect a reopen, to avoid locked list walks */
826         med = &req->rq_export->exp_mds_data;
827         spin_lock(&med->med_open_lock);
828         list_for_each(tmp, &med->med_open_head) {
829                 mfd = list_entry(tmp, typeof(*mfd), mfd_list);
830                 if (!memcmp(&mfd->mfd_clienthandle, &body->handle,
831                             sizeof(mfd->mfd_clienthandle)) &&
832                     body->fid1.id == mfd->mfd_file->f_dentry->d_inode->i_ino) {
833                         de = mfd->mfd_file->f_dentry;
834                         spin_unlock(&med->med_open_lock);
835                         CERROR("Re opening "LPD64"\n", body->fid1.id);
836                         GOTO(out_pack, rc = 0);
837                 }
838         }
839         spin_unlock(&med->med_open_lock);
840
841         mfd = kmem_cache_alloc(mds_file_cache, GFP_KERNEL);
842         if (!mfd) {
843                 CERROR("mds: out of memory\n");
844                 req->rq_status = -ENOMEM;
845                 RETURN(0);
846         }
847
848         de = mds_fid2dentry(mds, &body->fid1, &mnt);
849         if (IS_ERR(de))
850                 GOTO(out_free, rc = PTR_ERR(de));
851
852         /* check if this inode has seen a delayed object creation */
853         if (lustre_msg_get_op_flags(req->rq_reqmsg) & MDS_OPEN_HAS_EA) {
854                 struct lov_mds_md *lmm = lustre_msg_buf(req->rq_reqmsg, 1);
855
856                 rc = mds_store_ea(mds, req, body, de, lmm);
857                 if (rc) {
858                         l_dput(de);
859                         mntput(mnt);
860                         GOTO(out_free, rc);
861                 }
862         }
863
864         flags = body->flags;
865         /* dentry_open does a dput(de) and mntput(mnt) on error */
866         file = dentry_open(de, mnt, flags & ~O_DIRECT);
867         if (IS_ERR(file)) {
868                 rc = PTR_ERR(file);
869                 GOTO(out_free, 0);
870         }
871
872         file->private_data = mfd;
873         mfd->mfd_file = file;
874         memcpy(&mfd->mfd_clienthandle, &body->handle, sizeof(body->handle));
875         get_random_bytes(&mfd->mfd_servercookie, sizeof(mfd->mfd_servercookie));
876         spin_lock(&med->med_open_lock);
877         list_add(&mfd->mfd_list, &med->med_open_head);
878         spin_unlock(&med->med_open_lock);
879
880 out_pack:
881         body = lustre_msg_buf(req->rq_repmsg, 0);
882         mds_pack_inode2fid(&body->fid1, de->d_inode);
883         mds_pack_inode2body(body, de->d_inode);
884         body->handle.addr = (__u64)(unsigned long)mfd;
885         body->handle.cookie = mfd->mfd_servercookie;
886         CDEBUG(D_INODE, "llite file "LPX64": addr %p, cookie "LPX64"\n",
887                mfd->mfd_clienthandle.addr, mfd, mfd->mfd_servercookie);
888         RETURN(0);
889
890 out_free:
891         mfd->mfd_servercookie = DEAD_HANDLE_MAGIC;
892         kmem_cache_free(mds_file_cache, mfd);
893         req->rq_status = rc;
894         RETURN(0);
895 }
896
897 static int mds_close(struct ptlrpc_request *req)
898 {
899         struct mds_export_data *med = &req->rq_export->exp_mds_data;
900         struct mds_body *body;
901         struct mds_file_data *mfd;
902         int rc;
903         ENTRY;
904
905         body = lustre_msg_buf(req->rq_reqmsg, 0);
906
907         mfd = mds_handle2mfd(&body->handle);
908         if (!mfd) {
909                 CERROR("no handle for file close "LPD64
910                        ": addr "LPX64", cookie "LPX64"\n",
911                        body->fid1.id, body->handle.addr, body->handle.cookie);
912                 RETURN(-ESTALE);
913         }
914
915         spin_lock(&med->med_open_lock);
916         req->rq_status = mds_close_mfd(mfd, med);
917         spin_unlock(&med->med_open_lock);
918
919         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_CLOSE_PACK)) {
920                 CERROR("test case OBD_FAIL_MDS_CLOSE_PACK\n");
921                 req->rq_status = -ENOMEM;
922                 RETURN(-ENOMEM);
923         }
924
925         rc = lustre_pack_msg(0, NULL, NULL, &req->rq_replen, &req->rq_repmsg);
926         if (rc) {
927                 CERROR("mds: lustre_pack_msg: rc = %d\n", rc);
928                 req->rq_status = rc;
929         }
930
931         RETURN(0);
932 }
933
934 static int mds_readpage(struct ptlrpc_request *req)
935 {
936         struct mds_obd *mds = mds_req2mds(req);
937         struct vfsmount *mnt;
938         struct dentry *de;
939         struct file *file;
940         struct mds_body *body, *repbody;
941         struct obd_run_ctxt saved;
942         int rc, size = sizeof(*body);
943         struct obd_ucred uc;
944         ENTRY;
945
946         rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
947         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK)) {
948                 CERROR("mds: out of memory\n");
949                 GOTO(out, rc = -ENOMEM);
950         }
951
952         body = lustre_msg_buf(req->rq_reqmsg, 0);
953         uc.ouc_fsuid = body->fsuid;
954         uc.ouc_fsgid = body->fsgid;
955         push_ctxt(&saved, &mds->mds_ctxt, &uc);
956         de = mds_fid2dentry(mds, &body->fid1, &mnt);
957         if (IS_ERR(de))
958                 GOTO(out_pop, rc = PTR_ERR(de));
959
960         CDEBUG(D_INODE, "ino %ld\n", de->d_inode->i_ino);
961
962         file = dentry_open(de, mnt, O_RDONLY | O_LARGEFILE);
963         /* note: in case of an error, dentry_open puts dentry */
964         if (IS_ERR(file))
965                 GOTO(out_pop, rc = PTR_ERR(file));
966
967         repbody = lustre_msg_buf(req->rq_repmsg, 0);
968         repbody->size = file->f_dentry->d_inode->i_size;
969         repbody->valid = OBD_MD_FLSIZE;
970
971         /* to make this asynchronous make sure that the handling function
972            doesn't send a reply when this function completes. Instead a
973            callback function would send the reply */
974         /* note: in case of an error, dentry_open puts dentry */
975         rc = mds_sendpage(req, file, body->size);
976
977         filp_close(file, 0);
978 out_pop:
979         pop_ctxt(&saved);
980 out:
981         req->rq_status = rc;
982         RETURN(0);
983 }
984
985 int mds_reint(int offset, struct ptlrpc_request *req)
986 {
987         int rc;
988         struct mds_update_record rec;
989
990         rc = mds_update_unpack(req, offset, &rec);
991         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNPACK)) {
992                 CERROR("invalid record\n");
993                 req->rq_status = -EINVAL;
994                 RETURN(0);
995         }
996         /* rc will be used to interrupt a for loop over multiple records */
997         rc = mds_reint_rec(&rec, offset, req);
998         return rc;
999 }
1000
1001 int mds_handle(struct ptlrpc_request *req)
1002 {
1003         int rc;
1004         ENTRY;
1005
1006         rc = lustre_unpack_msg(req->rq_reqmsg, req->rq_reqlen);
1007         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_HANDLE_UNPACK)) {
1008                 CERROR("lustre_mds: Invalid request\n");
1009                 GOTO(out, rc);
1010         }
1011
1012         if (req->rq_reqmsg->opc != MDS_CONNECT && req->rq_export == NULL)
1013                 GOTO(out, rc = -ENOTCONN);
1014
1015         LASSERT(!strcmp(req->rq_obd->obd_type->typ_name, LUSTRE_MDT_NAME));
1016
1017         switch (req->rq_reqmsg->opc) {
1018         case MDS_CONNECT:
1019                 CDEBUG(D_INODE, "connect\n");
1020                 OBD_FAIL_RETURN(OBD_FAIL_MDS_CONNECT_NET, 0);
1021                 rc = target_handle_connect(req);
1022                 break;
1023
1024         case MDS_DISCONNECT:
1025                 CDEBUG(D_INODE, "disconnect\n");
1026                 OBD_FAIL_RETURN(OBD_FAIL_MDS_DISCONNECT_NET, 0);
1027                 rc = target_handle_disconnect(req);
1028                 goto out;
1029
1030         case MDS_GETSTATUS:
1031                 CDEBUG(D_INODE, "getstatus\n");
1032                 OBD_FAIL_RETURN(OBD_FAIL_MDS_GETSTATUS_NET, 0);
1033                 rc = mds_getstatus(req);
1034                 break;
1035
1036         case MDS_GETLOVINFO:
1037                 CDEBUG(D_INODE, "getlovinfo\n");
1038                 rc = mds_getlovinfo(req);
1039                 break;
1040
1041         case MDS_GETATTR:
1042                 CDEBUG(D_INODE, "getattr\n");
1043                 OBD_FAIL_RETURN(OBD_FAIL_MDS_GETATTR_NET, 0);
1044                 rc = mds_getattr(0, req);
1045                 break;
1046
1047         case MDS_STATFS:
1048                 CDEBUG(D_INODE, "statfs\n");
1049                 OBD_FAIL_RETURN(OBD_FAIL_MDS_STATFS_NET, 0);
1050                 rc = mds_statfs(req);
1051                 break;
1052
1053         case MDS_READPAGE:
1054                 CDEBUG(D_INODE, "readpage\n");
1055                 OBD_FAIL_RETURN(OBD_FAIL_MDS_READPAGE_NET, 0);
1056                 rc = mds_readpage(req);
1057
1058                 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
1059                         return 0;
1060                 break;
1061
1062         case MDS_REINT: {
1063                 int size = sizeof(struct mds_body);
1064                 CDEBUG(D_INODE, "reint\n");
1065                 OBD_FAIL_RETURN(OBD_FAIL_MDS_REINT_NET, 0);
1066
1067                 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen,
1068                                      &req->rq_repmsg);
1069                 if (rc) {
1070                         req->rq_status = rc;
1071                         break;
1072                 }
1073                 rc = mds_reint(0, req);
1074                 OBD_FAIL_RETURN(OBD_FAIL_MDS_REINT_NET_REP, 0);
1075                 break;
1076                 }
1077
1078         case MDS_OPEN:
1079                 CDEBUG(D_INODE, "open\n");
1080                 OBD_FAIL_RETURN(OBD_FAIL_MDS_OPEN_NET, 0);
1081                 rc = mds_open(req);
1082                 break;
1083
1084         case MDS_CLOSE:
1085                 CDEBUG(D_INODE, "close\n");
1086                 OBD_FAIL_RETURN(OBD_FAIL_MDS_CLOSE_NET, 0);
1087                 rc = mds_close(req);
1088                 break;
1089
1090         case LDLM_ENQUEUE:
1091                 CDEBUG(D_INODE, "enqueue\n");
1092                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_ENQUEUE, 0);
1093                 rc = ldlm_handle_enqueue(req);
1094                 if (rc)
1095                         break;
1096                 RETURN(0);
1097         case LDLM_CONVERT:
1098                 CDEBUG(D_INODE, "convert\n");
1099                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_CONVERT, 0);
1100                 rc = ldlm_handle_convert(req);
1101                 if (rc)
1102                         break;
1103                 RETURN(0);
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_REQUEST_PORTAL, LDLM_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\n");
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, (lprocfs_vars_t*)status_var_nm_1, (void*)dev);
1460         return rc; 
1461 }
1462
1463 int mds_detach(struct obd_device *dev)
1464 {
1465         int rc;
1466         rc = lprocfs_dereg_obd(dev);
1467         return rc;
1468
1469 }
1470
1471 #define MDT_NUM_THREADS 8
1472 static int mdt_setup(struct obd_device *obddev, obd_count len, void *buf)
1473 {
1474         int i;
1475         //        struct obd_ioctl_data* data = buf;
1476         struct mds_obd *mds = &obddev->u.mds;
1477         int rc = 0;
1478         ENTRY;
1479
1480         MOD_INC_USE_COUNT;
1481
1482         mds->mds_service = ptlrpc_init_svc(MDS_NEVENTS, MDS_NBUFS,
1483                                            MDS_BUFSIZE, MDS_MAXREQSIZE,
1484                                            MDS_REQUEST_PORTAL, MDC_REPLY_PORTAL,
1485                                            "self", mds_handle, "mds");
1486         if (!mds->mds_service) {
1487                 CERROR("failed to start service\n");
1488                 GOTO(err_dec, rc = -EINVAL);
1489         }
1490
1491         for (i = 0; i < MDT_NUM_THREADS; i++) {
1492                 char name[32];
1493                 sprintf(name, "lustre_MDT_%02d", i);
1494                 rc = ptlrpc_start_thread(obddev, mds->mds_service, name);
1495                 if (rc) {
1496                         CERROR("cannot start MDT thread #%d: rc %d\n", i, rc);
1497                         GOTO(err_thread, rc);
1498                 }
1499         }
1500
1501         RETURN(0);
1502
1503 err_thread:
1504         ptlrpc_stop_all_threads(mds->mds_service);
1505         ptlrpc_unregister_service(mds->mds_service);
1506 err_dec:
1507         MOD_DEC_USE_COUNT;
1508         RETURN(rc);
1509 }
1510
1511
1512 static int mdt_cleanup(struct obd_device *obddev)
1513 {
1514         struct mds_obd *mds = &obddev->u.mds;
1515         ENTRY;
1516
1517         ptlrpc_stop_all_threads(mds->mds_service);
1518         ptlrpc_unregister_service(mds->mds_service);
1519
1520         MOD_DEC_USE_COUNT;
1521         RETURN(0);
1522 }
1523
1524 extern int mds_iocontrol(long cmd, struct lustre_handle *conn,
1525                          int len, void *karg, void *uarg);
1526
1527 /* use obd ops to offer management infrastructure */
1528 static struct obd_ops mds_obd_ops = {
1529         o_attach:      mds_attach,
1530         o_detach:      mds_detach,
1531         o_connect:     mds_connect,
1532         o_disconnect:  mds_disconnect,
1533         o_setup:       mds_setup,
1534         o_cleanup:     mds_cleanup,
1535         o_iocontrol:   mds_iocontrol
1536 };
1537
1538 static struct obd_ops mdt_obd_ops = {
1539         o_setup:       mdt_setup,
1540         o_cleanup:     mdt_cleanup,
1541 };
1542
1543
1544 static int __init mds_init(void)
1545 {
1546      
1547         mds_file_cache = kmem_cache_create("ll_mds_file_data",
1548                                            sizeof(struct mds_file_data),
1549                                            0, 0, NULL, NULL);
1550         if (mds_file_cache == NULL)
1551                 return -ENOMEM;
1552
1553         class_register_type(&mds_obd_ops, (lprocfs_vars_t*)status_class_var, 
1554                             LUSTRE_MDS_NAME);
1555         class_register_type(&mdt_obd_ops, 0, LUSTRE_MDT_NAME);
1556
1557         ldlm_register_intent(ldlm_intent_policy);
1558         
1559         return 0;
1560         
1561 }
1562
1563 static void __exit mds_exit(void)
1564 {
1565         
1566         
1567         ldlm_unregister_intent();
1568         class_unregister_type(LUSTRE_MDS_NAME);
1569         class_unregister_type(LUSTRE_MDT_NAME);
1570         if (kmem_cache_destroy(mds_file_cache))
1571                 CERROR("couldn't free MDS file cache\n");
1572         
1573 }
1574
1575 MODULE_AUTHOR("Cluster File Systems <info@clusterfs.com>");
1576 MODULE_DESCRIPTION("Lustre Metadata Server (MDS) v0.01");
1577 MODULE_LICENSE("GPL");
1578
1579 module_init(mds_init);
1580 module_exit(mds_exit);