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