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