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