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