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