Whamcloud - gitweb
b=300
[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 mds_obd *obd, int tgt_count,
44                            obd_uuid_t *uuidarray);
45 extern int mds_get_lovdesc(struct mds_obd  *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 = 0, 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 = 0, 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 /* Call with med->med_open_lock held, please. */
354 inline int mds_close_mfd(struct mds_file_data *mfd, struct mds_export_data *med)
355 {
356         struct file *file = mfd->mfd_file;
357         LASSERT(file->private_data == mfd);
358
359         list_del(&mfd->mfd_list);
360         mfd->mfd_servercookie = DEAD_HANDLE_MAGIC;
361         kmem_cache_free(mds_file_cache, mfd);
362
363         return filp_close(file, 0);
364 }
365
366 static int mds_disconnect(struct lustre_handle *conn)
367 {
368         struct obd_export *export = class_conn2export(conn);
369         struct list_head *tmp, *n;
370         struct mds_export_data *med = &export->exp_mds_data;
371         int rc;
372         ENTRY;
373
374         /*
375          * Close any open files.
376          */
377         spin_lock(&med->med_open_lock);
378         list_for_each_safe(tmp, n, &med->med_open_head) {
379                 struct mds_file_data *mfd = 
380                         list_entry(tmp, struct mds_file_data, mfd_list);
381                 rc = mds_close_mfd(mfd, med);
382                 if (rc) {
383                         /* XXX better diagnostics, with file path and stuff */
384                         CDEBUG(D_INODE, "Error %d closing mfd %p\n", rc, mfd);
385                 }
386         }
387         spin_unlock(&med->med_open_lock);
388                                                        
389         ldlm_cancel_locks_for_export(export);
390         mds_client_free(export);
391
392         rc = class_disconnect(conn);
393         if (!rc)
394                 MOD_DEC_USE_COUNT;
395
396         RETURN(rc);
397 }
398
399 static int mds_getstatus(struct ptlrpc_request *req)
400 {
401         struct mds_obd *mds = mds_req2mds(req);
402         struct mds_body *body;
403         int rc, size = sizeof(*body);
404         ENTRY;
405
406         rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
407         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_GETSTATUS_PACK)) {
408                 CERROR("mds: out of memory for message: size=%d\n", size);
409                 req->rq_status = -ENOMEM;
410                 RETURN(0);
411         }
412
413         /* Anything we need to do here with the client's trans no or so? */
414         body = lustre_msg_buf(req->rq_repmsg, 0);
415         memcpy(&body->fid1, &mds->mds_rootfid, sizeof(body->fid1));
416
417         /* the last_committed and last_xid fields are filled in for all
418          * replies already - no need to do so here also.
419          */
420         RETURN(0);
421 }
422
423 static int mds_getlovinfo(struct ptlrpc_request *req)
424 {
425         struct mds_obd *mds = mds_req2mds(req);
426         struct mds_status_req *streq;
427         struct lov_desc *desc;
428         int tgt_count;
429         int rc, size[2] = {sizeof(*desc)};
430         ENTRY;
431
432         streq = lustre_msg_buf(req->rq_reqmsg, 0);
433         streq->flags = NTOH__u32(streq->flags);
434         streq->repbuf = NTOH__u32(streq->repbuf);
435         size[1] = streq->repbuf;
436
437         rc = lustre_pack_msg(2, size, NULL, &req->rq_replen, &req->rq_repmsg);
438         if (rc) {
439                 CERROR("mds: out of memory for message: size=%d\n", size[1]);
440                 req->rq_status = -ENOMEM;
441                 RETURN(0);
442         }
443
444         desc = lustre_msg_buf(req->rq_repmsg, 0);
445         rc = mds_get_lovdesc(mds, desc);
446         if (rc) {
447                 CERROR("mds_get_lovdesc error %d", rc);
448                 req->rq_status = rc;
449                 RETURN(0);
450         }
451
452         tgt_count = le32_to_cpu(desc->ld_tgt_count);
453         if (tgt_count * sizeof(obd_uuid_t) > streq->repbuf) {
454                 CERROR("too many targets, enlarge client buffers\n");
455                 req->rq_status = -ENOSPC;
456                 RETURN(0);
457         }
458
459         mds->mds_max_mdsize = sizeof(struct lov_mds_md) +
460                 tgt_count * sizeof(struct lov_object_id);
461         rc = mds_get_lovtgts(mds, tgt_count,
462                              lustre_msg_buf(req->rq_repmsg, 1));
463         if (rc) {
464                 CERROR("get_lovtgts error %d\n", rc);
465                 req->rq_status = rc;
466                 RETURN(0);
467         }
468         RETURN(0);
469 }
470
471 int mds_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
472                      void *data, __u32 data_len, int flag)
473 {
474         int do_ast;
475         ENTRY;
476
477         if (flag == LDLM_CB_CANCELING) {
478                 /* Don't need to do anything here. */
479                 RETURN(0);
480         }
481
482         /* XXX layering violation!  -phil */
483         l_lock(&lock->l_resource->lr_namespace->ns_lock);
484         lock->l_flags |= LDLM_FL_CBPENDING;
485         do_ast = (!lock->l_readers && !lock->l_writers);
486         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
487
488         if (do_ast) {
489                 struct lustre_handle lockh;
490                 int rc;
491
492                 LDLM_DEBUG(lock, "already unused, calling ldlm_cli_cancel");
493                 ldlm_lock2handle(lock, &lockh);
494                 rc = ldlm_cli_cancel(&lockh);
495                 if (rc < 0)
496                         CERROR("ldlm_cli_cancel: %d\n", rc);
497         } else
498                 LDLM_DEBUG(lock, "Lock still has references, will be"
499                            "cancelled later");
500         RETURN(0);
501 }
502
503 static int mds_getattr_internal(struct mds_obd *mds, struct dentry *dentry,
504                                 struct ptlrpc_request *req,
505                                 struct mds_body *reqbody, int reply_off)
506 {
507         struct mds_body *body;
508         struct inode *inode = dentry->d_inode;
509         int rc;
510         ENTRY;
511
512         if (inode == NULL)
513                 RETURN(-ENOENT);
514
515         body = lustre_msg_buf(req->rq_repmsg, reply_off);
516
517         mds_pack_inode2fid(&body->fid1, inode);
518         mds_pack_inode2body(body, inode);
519
520         if (S_ISREG(inode->i_mode)) {
521                 struct lov_mds_md *lmm;
522
523                 lmm = lustre_msg_buf(req->rq_repmsg, reply_off + 1);
524                 lmm->lmm_easize = mds->mds_max_mdsize;
525                 rc = mds_fs_get_md(mds, inode, lmm);
526
527                 if (rc < 0) {
528                         if (rc == -ENODATA)
529                                 RETURN(0);
530                         CERROR("mds_fs_get_md failed: %d\n", rc);
531                         RETURN(rc);
532                 }
533                 body->valid |= OBD_MD_FLEASIZE;
534         } else if (S_ISLNK(inode->i_mode) && reqbody->valid & OBD_MD_LINKNAME) {
535                 char *symname = lustre_msg_buf(req->rq_repmsg, reply_off + 1);
536                 int len = req->rq_repmsg->buflens[reply_off + 1];
537
538                 rc = inode->i_op->readlink(dentry, symname, len);
539                 if (rc < 0) {
540                         CERROR("readlink failed: %d\n", rc);
541                         RETURN(rc);
542                 } else
543                         CDEBUG(D_INODE, "read symlink dest %s\n", symname);
544
545                 body->valid |= OBD_MD_LINKNAME;
546         }
547         RETURN(0);
548 }
549
550 static int mds_getattr_name(int offset, struct ptlrpc_request *req)
551 {
552         struct mds_obd *mds = mds_req2mds(req);
553         struct obd_device *obd = req->rq_export->exp_obd;
554         struct obd_run_ctxt saved;
555         struct mds_body *body;
556         struct dentry *de = NULL, *dchild = NULL;
557         struct inode *dir;
558         struct lustre_handle lockh;
559         char *name;
560         int namelen, flags = 0, lock_mode, rc = 0;
561         struct obd_ucred uc;
562         __u64 res_id[3] = {0, 0, 0};
563         ENTRY;
564
565         LASSERT(!strcmp(req->rq_export->exp_obd->obd_type->typ_name, "mds"));
566
567         if (req->rq_reqmsg->bufcount <= offset + 1) {
568                 LBUG();
569                 GOTO(out_pre_de, rc = -EINVAL);
570         }
571
572         body = lustre_msg_buf(req->rq_reqmsg, offset);
573         name = lustre_msg_buf(req->rq_reqmsg, offset + 1);
574         namelen = req->rq_reqmsg->buflens[offset + 1];
575         /* requests were at offset 2, replies go back at 1 */
576         if (offset)
577                 offset = 1;
578
579         uc.ouc_fsuid = body->fsuid;
580         uc.ouc_fsgid = body->fsgid;
581         push_ctxt(&saved, &mds->mds_ctxt, &uc);
582         de = mds_fid2dentry(mds, &body->fid1, NULL);
583         if (IS_ERR(de)) {
584                 LBUG();
585                 GOTO(out_pre_de, rc = -ESTALE);
586         }
587
588         dir = de->d_inode;
589         CDEBUG(D_INODE, "parent ino %ld, name %*s\n", dir->i_ino,namelen,name);
590
591         lock_mode = (req->rq_reqmsg->opc == MDS_REINT) ? LCK_CW : LCK_PW;
592         res_id[0] = dir->i_ino;
593
594         rc = ldlm_lock_match(obd->obd_namespace, res_id, LDLM_PLAIN,
595                              NULL, 0, lock_mode, &lockh);
596         if (rc == 0) {
597                 LDLM_DEBUG_NOLOCK("enqueue res "LPU64, res_id[0]);
598                 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, NULL,
599                                       res_id, LDLM_PLAIN, NULL, 0, lock_mode,
600                                       &flags, ldlm_completion_ast,
601                                       mds_blocking_ast, NULL, 0, &lockh);
602                 if (rc != ELDLM_OK) {
603                         CERROR("lock enqueue: err: %d\n", rc);
604                         GOTO(out_create_de, rc = -EIO);
605                 }
606         }
607         ldlm_lock_dump((void *)(unsigned long)lockh.addr);
608
609         down(&dir->i_sem);
610         dchild = lookup_one_len(name, de, namelen - 1);
611         if (IS_ERR(dchild)) {
612                 CDEBUG(D_INODE, "child lookup error %ld\n", PTR_ERR(dchild));
613                 up(&dir->i_sem);
614                 LBUG();
615                 GOTO(out_create_dchild, rc = -ESTALE);
616         }
617
618         rc = mds_getattr_internal(mds, dchild, req, body, offset);
619
620         EXIT;
621 out_create_dchild:
622         l_dput(dchild);
623         up(&dir->i_sem);
624         ldlm_lock_decref(&lockh, lock_mode);
625 out_create_de:
626         l_dput(de);
627 out_pre_de:
628         req->rq_status = rc;
629         pop_ctxt(&saved);
630         return 0;
631 }
632
633 static int mds_getattr(int offset, struct ptlrpc_request *req)
634 {
635         struct mds_obd *mds = mds_req2mds(req);
636         struct obd_run_ctxt saved;
637         struct dentry *de;
638         struct inode *inode;
639         struct mds_body *body;
640         struct obd_ucred uc;
641         int rc = 0, size[2] = {sizeof(*body)}, bufcount = 1;
642         ENTRY;
643
644         body = lustre_msg_buf(req->rq_reqmsg, offset);
645         uc.ouc_fsuid = body->fsuid;
646         uc.ouc_fsgid = body->fsgid;
647         push_ctxt(&saved, &mds->mds_ctxt, &uc);
648         de = mds_fid2dentry(mds, &body->fid1, NULL);
649         if (IS_ERR(de)) {
650                 rc = req->rq_status = -ENOENT;
651                 GOTO(out_pop, PTR_ERR(de));
652         }
653
654         inode = de->d_inode;
655         if (S_ISREG(body->fid1.f_type)) {
656                 bufcount = 2;
657                 size[1] = mds->mds_max_mdsize;
658         } else if (body->valid & OBD_MD_LINKNAME) {
659                 bufcount = 2;
660                 size[1] = MIN(inode->i_size + 1, body->size);
661                 CDEBUG(D_INODE, "symlink size: %d, reply space: %d\n",
662                        inode->i_size + 1, body->size);
663         }
664
665         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK)) {
666                 CERROR("failed GETATTR_PACK test\n");
667                 req->rq_status = -ENOMEM;
668                 GOTO(out, rc = -ENOMEM);
669         }
670
671         rc = lustre_pack_msg(bufcount, size, NULL, &req->rq_replen,
672                              &req->rq_repmsg);
673         if (rc) {
674                 CERROR("out of memory or FAIL_MDS_GETATTR_PACK\n");
675                 req->rq_status = rc;
676                 GOTO(out, rc);
677         }
678
679         req->rq_status = mds_getattr_internal(mds, de, req, body, 0);
680
681 out:
682         l_dput(de);
683 out_pop:
684         pop_ctxt(&saved);
685         RETURN(rc);
686 }
687
688 static int mds_statfs(struct ptlrpc_request *req)
689 {
690         struct mds_obd *mds = mds_req2mds(req);
691         struct obd_statfs *osfs;
692         struct statfs sfs;
693         int rc, size = sizeof(*osfs);
694         ENTRY;
695
696         rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen,
697                              &req->rq_repmsg);
698         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK)) {
699                 CERROR("mds: statfs lustre_pack_msg failed: rc = %d\n", rc);
700                 GOTO(out, rc);
701         }
702
703         rc = vfs_statfs(mds->mds_sb, &sfs);
704         if (rc) {
705                 CERROR("mds: statfs failed: rc %d\n", rc);
706                 GOTO(out, rc);
707         }
708         osfs = lustre_msg_buf(req->rq_repmsg, 0);
709         memset(osfs, 0, size);
710         statfs_pack(osfs, &sfs);
711         obd_statfs_pack(osfs, osfs);
712
713 out:
714         req->rq_status = rc;
715         RETURN(0);
716 }
717
718 static struct mds_file_data *mds_handle2mfd(struct lustre_handle *handle)
719 {
720         struct mds_file_data *mfd = NULL;
721
722         if (!handle || !handle->addr)
723                 RETURN(NULL);
724
725         mfd = (struct mds_file_data *)(unsigned long)(handle->addr);
726         if (!kmem_cache_validate(mds_file_cache, mfd))
727                 RETURN(NULL);
728
729         if (mfd->mfd_servercookie != handle->cookie)
730                 RETURN(NULL);
731
732         return mfd;
733 }
734
735 static int mds_store_ea(struct mds_obd *mds, struct ptlrpc_request *req,
736                         struct mds_body *body, struct dentry *de,
737                         struct lov_mds_md *lmm)
738 {
739         struct obd_run_ctxt saved;
740         struct obd_ucred uc;
741         void *handle;
742         int rc, rc2;
743
744         uc.ouc_fsuid = body->fsuid;
745         uc.ouc_fsgid = body->fsgid;
746         push_ctxt(&saved, &mds->mds_ctxt, &uc);
747         handle = mds_fs_start(mds, de->d_inode, MDS_FSOP_SETATTR);
748         if (!handle)
749                 GOTO(out_ea, rc = -ENOMEM);
750
751         rc = mds_fs_set_md(mds, de->d_inode, handle, lmm);
752         if (!rc)
753                 rc = mds_update_last_rcvd(mds, handle, req);
754
755         rc2 = mds_fs_commit(mds, de->d_inode, handle);
756         if (rc2 && !rc)
757                 rc = rc2;
758 out_ea:
759         pop_ctxt(&saved);
760
761         return rc;
762 }
763
764 static int mds_open(struct ptlrpc_request *req)
765 {
766         struct mds_obd *mds = mds_req2mds(req);
767         struct mds_body *body;
768         struct mds_export_data *med;
769         struct mds_file_data *mfd;
770         struct dentry *de;
771         struct file *file;
772         struct vfsmount *mnt;
773         __u32 flags;
774         struct list_head *tmp;
775         int rc, size = sizeof(*body);
776         ENTRY;
777
778         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OPEN_PACK)) {
779                 CERROR("test case OBD_FAIL_MDS_OPEN_PACK\n");
780                 req->rq_status = -ENOMEM;
781                 RETURN(-ENOMEM);
782         }
783
784         rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
785         if (rc) {
786                 CERROR("mds: pack error: rc = %d\n", rc);
787                 req->rq_status = rc;
788                 RETURN(rc);
789         }
790
791         body = lustre_msg_buf(req->rq_reqmsg, 0);
792
793         /* was this animal open already and the client lost the reply? */
794         /* XXX need some way to detect a reopen, to avoid locked list walks */
795         med = &req->rq_export->exp_mds_data;
796         spin_lock(&med->med_open_lock);
797         list_for_each(tmp, &med->med_open_head) {
798                 mfd = list_entry(tmp, typeof(*mfd), mfd_list);
799                 if (!memcmp(&mfd->mfd_clienthandle, &body->handle,
800                             sizeof(mfd->mfd_clienthandle)) &&
801                     body->fid1.id == mfd->mfd_file->f_dentry->d_inode->i_ino) {
802                         de = mfd->mfd_file->f_dentry;
803                         spin_unlock(&med->med_open_lock);
804                         CERROR("Re opening "LPD64"\n", body->fid1.id);
805                         GOTO(out_pack, rc = 0);
806                 }
807         }
808         spin_unlock(&med->med_open_lock);
809
810         mfd = kmem_cache_alloc(mds_file_cache, GFP_KERNEL);
811         if (!mfd) {
812                 CERROR("mds: out of memory\n");
813                 req->rq_status = -ENOMEM;
814                 RETURN(0);
815         }
816
817         de = mds_fid2dentry(mds, &body->fid1, &mnt);
818         if (IS_ERR(de))
819                 GOTO(out_free, rc = PTR_ERR(de));
820
821         /* check if this inode has seen a delayed object creation */
822         if (lustre_msg_get_op_flags(req->rq_reqmsg) & MDS_OPEN_HAS_EA) {
823                 struct lov_mds_md *lmm = lustre_msg_buf(req->rq_reqmsg, 1);
824
825                 rc = mds_store_ea(mds, req, body, de, lmm);
826                 if (rc) {
827                         l_dput(de);
828                         mntput(mnt);
829                         GOTO(out_free, rc);
830                 }
831         }
832
833         flags = body->flags;
834         /* dentry_open does a dput(de) and mntput(mnt) on error */
835         file = dentry_open(de, mnt, flags & ~O_DIRECT);
836         if (IS_ERR(file)) {
837                 rc = PTR_ERR(file);
838                 GOTO(out_free, 0);
839         }
840
841         file->private_data = mfd;
842         mfd->mfd_file = file;
843         memcpy(&mfd->mfd_clienthandle, &body->handle, sizeof(body->handle));
844         get_random_bytes(&mfd->mfd_servercookie, sizeof(mfd->mfd_servercookie));
845         spin_lock(&med->med_open_lock);
846         list_add(&mfd->mfd_list, &med->med_open_head);
847         spin_unlock(&med->med_open_lock);
848
849 out_pack:
850         body = lustre_msg_buf(req->rq_repmsg, 0);
851         mds_pack_inode2fid(&body->fid1, de->d_inode);
852         mds_pack_inode2body(body, de->d_inode);
853         body->handle.addr = (__u64)(unsigned long)mfd;
854         body->handle.cookie = mfd->mfd_servercookie;
855         CDEBUG(D_INODE, "llite file "LPX64": addr %p, cookie "LPX64"\n",
856                mfd->mfd_clienthandle.addr, mfd, mfd->mfd_servercookie);
857         RETURN(0);
858
859 out_free:
860         mfd->mfd_servercookie = DEAD_HANDLE_MAGIC;
861         kmem_cache_free(mds_file_cache, mfd);
862         req->rq_status = rc;
863         RETURN(0);
864 }
865
866 static int mds_close(struct ptlrpc_request *req)
867 {
868         struct mds_export_data *med = &req->rq_export->exp_mds_data;
869         struct mds_body *body;
870         struct mds_file_data *mfd;
871         int rc;
872         ENTRY;
873
874         body = lustre_msg_buf(req->rq_reqmsg, 0);
875
876         mfd = mds_handle2mfd(&body->handle);
877         if (!mfd) {
878                 CERROR("no handle for file close "LPD64
879                        ": addr "LPX64", cookie "LPX64"\n",
880                        body->fid1.id, body->handle.addr, body->handle.cookie);
881                 RETURN(-ESTALE);
882         }
883
884         spin_lock(&med->med_open_lock);
885         req->rq_status = mds_close_mfd(mfd, med);
886         spin_unlock(&med->med_open_lock);
887
888         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_CLOSE_PACK)) {
889                 CERROR("test case OBD_FAIL_MDS_CLOSE_PACK\n");
890                 req->rq_status = -ENOMEM;
891                 RETURN(-ENOMEM);
892         }
893
894         rc = lustre_pack_msg(0, NULL, NULL, &req->rq_replen, &req->rq_repmsg);
895         if (rc) {
896                 CERROR("mds: lustre_pack_msg: rc = %d\n", rc);
897                 req->rq_status = rc;
898         }
899
900         RETURN(0);
901 }
902
903 static int mds_readpage(struct ptlrpc_request *req)
904 {
905         struct mds_obd *mds = mds_req2mds(req);
906         struct vfsmount *mnt;
907         struct dentry *de;
908         struct file *file;
909         struct mds_body *body, *repbody;
910         struct obd_run_ctxt saved;
911         int rc, size = sizeof(*body);
912         struct obd_ucred uc;
913         ENTRY;
914
915         rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
916         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK)) {
917                 CERROR("mds: out of memory\n");
918                 GOTO(out, rc = -ENOMEM);
919         }
920
921         body = lustre_msg_buf(req->rq_reqmsg, 0);
922         uc.ouc_fsuid = body->fsuid;
923         uc.ouc_fsgid = body->fsgid;
924         push_ctxt(&saved, &mds->mds_ctxt, &uc);
925         de = mds_fid2dentry(mds, &body->fid1, &mnt);
926         if (IS_ERR(de))
927                 GOTO(out_pop, rc = PTR_ERR(de));
928
929         CDEBUG(D_INODE, "ino %ld\n", de->d_inode->i_ino);
930
931         file = dentry_open(de, mnt, O_RDONLY | O_LARGEFILE);
932         /* note: in case of an error, dentry_open puts dentry */
933         if (IS_ERR(file))
934                 GOTO(out_pop, rc = PTR_ERR(file));
935
936         repbody = lustre_msg_buf(req->rq_repmsg, 0);
937         repbody->size = file->f_dentry->d_inode->i_size;
938         repbody->valid = OBD_MD_FLSIZE;
939
940         /* to make this asynchronous make sure that the handling function
941            doesn't send a reply when this function completes. Instead a
942            callback function would send the reply */
943         /* note: in case of an error, dentry_open puts dentry */
944         rc = mds_sendpage(req, file, body->size);
945
946         filp_close(file, 0);
947 out_pop:
948         pop_ctxt(&saved);
949 out:
950         req->rq_status = rc;
951         RETURN(0);
952 }
953
954 int mds_reint(int offset, struct ptlrpc_request *req)
955 {
956         int rc;
957         struct mds_update_record rec;
958
959         rc = mds_update_unpack(req, offset, &rec);
960         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNPACK)) {
961                 CERROR("invalid record\n");
962                 req->rq_status = -EINVAL;
963                 RETURN(0);
964         }
965         /* rc will be used to interrupt a for loop over multiple records */
966         rc = mds_reint_rec(&rec, offset, req);
967         return rc;
968 }
969
970 int mds_handle(struct ptlrpc_request *req)
971 {
972         int rc;
973         ENTRY;
974
975         rc = lustre_unpack_msg(req->rq_reqmsg, req->rq_reqlen);
976         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_HANDLE_UNPACK)) {
977                 CERROR("lustre_mds: Invalid request\n");
978                 GOTO(out, rc);
979         }
980
981         if (req->rq_reqmsg->opc != MDS_CONNECT && req->rq_export == NULL)
982                 GOTO(out, rc = -ENOTCONN);
983
984         LASSERT(!strcmp(req->rq_obd->obd_type->typ_name, LUSTRE_MDT_NAME));
985
986         switch (req->rq_reqmsg->opc) {
987         case MDS_CONNECT:
988                 CDEBUG(D_INODE, "connect\n");
989                 OBD_FAIL_RETURN(OBD_FAIL_MDS_CONNECT_NET, 0);
990                 rc = target_handle_connect(req);
991                 break;
992
993         case MDS_DISCONNECT:
994                 CDEBUG(D_INODE, "disconnect\n");
995                 OBD_FAIL_RETURN(OBD_FAIL_MDS_DISCONNECT_NET, 0);
996                 rc = target_handle_disconnect(req);
997                 goto out;
998
999         case MDS_GETSTATUS:
1000                 CDEBUG(D_INODE, "getstatus\n");
1001                 OBD_FAIL_RETURN(OBD_FAIL_MDS_GETSTATUS_NET, 0);
1002                 rc = mds_getstatus(req);
1003                 break;
1004
1005         case MDS_GETLOVINFO:
1006                 CDEBUG(D_INODE, "getlovinfo\n");
1007                 rc = mds_getlovinfo(req);
1008                 break;
1009
1010         case MDS_GETATTR:
1011                 CDEBUG(D_INODE, "getattr\n");
1012                 OBD_FAIL_RETURN(OBD_FAIL_MDS_GETATTR_NET, 0);
1013                 rc = mds_getattr(0, req);
1014                 break;
1015
1016         case MDS_STATFS:
1017                 CDEBUG(D_INODE, "statfs\n");
1018                 OBD_FAIL_RETURN(OBD_FAIL_MDS_STATFS_NET, 0);
1019                 rc = mds_statfs(req);
1020                 break;
1021
1022         case MDS_READPAGE:
1023                 CDEBUG(D_INODE, "readpage\n");
1024                 OBD_FAIL_RETURN(OBD_FAIL_MDS_READPAGE_NET, 0);
1025                 rc = mds_readpage(req);
1026
1027                 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
1028                         return 0;
1029                 break;
1030
1031         case MDS_REINT: {
1032                 int size = sizeof(struct mds_body);
1033                 CDEBUG(D_INODE, "reint\n");
1034                 OBD_FAIL_RETURN(OBD_FAIL_MDS_REINT_NET, 0);
1035
1036                 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen,
1037                                      &req->rq_repmsg);
1038                 if (rc) {
1039                         req->rq_status = rc;
1040                         break;
1041                 }
1042                 rc = mds_reint(0, req);
1043                 OBD_FAIL_RETURN(OBD_FAIL_MDS_REINT_NET_REP, 0);
1044                 break;
1045                 }
1046
1047         case MDS_OPEN:
1048                 CDEBUG(D_INODE, "open\n");
1049                 OBD_FAIL_RETURN(OBD_FAIL_MDS_OPEN_NET, 0);
1050                 rc = mds_open(req);
1051                 break;
1052
1053         case MDS_CLOSE:
1054                 CDEBUG(D_INODE, "close\n");
1055                 OBD_FAIL_RETURN(OBD_FAIL_MDS_CLOSE_NET, 0);
1056                 rc = mds_close(req);
1057                 break;
1058
1059         case LDLM_ENQUEUE:
1060                 CDEBUG(D_INODE, "enqueue\n");
1061                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_ENQUEUE, 0);
1062                 rc = ldlm_handle_enqueue(req);
1063                 if (rc)
1064                         break;
1065                 RETURN(0);
1066         case LDLM_CONVERT:
1067                 CDEBUG(D_INODE, "convert\n");
1068                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_CONVERT, 0);
1069                 rc = ldlm_handle_convert(req);
1070                 if (rc)
1071                         break;
1072                 RETURN(0);
1073         case LDLM_BL_CALLBACK:
1074         case LDLM_CP_CALLBACK:
1075                 CDEBUG(D_INODE, "callback\n");
1076                 CERROR("callbacks should not happen on MDS\n");
1077                 LBUG();
1078                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_BL_CALLBACK, 0);
1079                 break;
1080         default:
1081                 rc = ptlrpc_error(req->rq_svc, req);
1082                 RETURN(rc);
1083         }
1084
1085         EXIT;
1086
1087         if (!rc) {
1088                 struct mds_export_data *med = &req->rq_export->exp_mds_data;
1089                 struct mds_obd *mds = mds_req2mds(req);
1090
1091                 req->rq_repmsg->last_xid =
1092                         HTON__u64(le64_to_cpu(med->med_mcd->mcd_last_xid));
1093                 req->rq_repmsg->last_committed =
1094                         HTON__u64(mds->mds_last_committed);
1095                 CDEBUG(D_INFO, "last_rcvd ~%Lu, last_committed %Lu, xid %d\n",
1096                        (unsigned long long)mds->mds_last_rcvd,
1097                        (unsigned long long)mds->mds_last_committed,
1098                        cpu_to_le32(req->rq_xid));
1099         }
1100  out:
1101         if (rc) {
1102                 CERROR("mds: processing error (opcode %d): %d\n",
1103                        req->rq_reqmsg->opc, rc);
1104                 ptlrpc_error(req->rq_svc, req);
1105         } else {
1106                 CDEBUG(D_NET, "sending reply\n");
1107                 ptlrpc_reply(req->rq_svc, req);
1108         }
1109         return 0;
1110 }
1111
1112 /* Update the server data on disk.  This stores the new mount_count and
1113  * also the last_rcvd value to disk.  If we don't have a clean shutdown,
1114  * then the server last_rcvd value may be less than that of the clients.
1115  * This will alert us that we may need to do client recovery.
1116  *
1117  * Assumes we are already in the server filesystem context.
1118  *
1119  * Also assumes for mds_last_rcvd that we are not modifying it (no locking).
1120  */
1121 static
1122 int mds_update_server_data(struct mds_obd *mds)
1123 {
1124         struct mds_server_data *msd = mds->mds_server_data;
1125         struct file *filp = mds->mds_rcvd_filp;
1126         loff_t off = 0;
1127         int rc;
1128
1129         msd->msd_last_rcvd = cpu_to_le64(mds->mds_last_rcvd);
1130         msd->msd_mount_count = cpu_to_le64(mds->mds_mount_count);
1131
1132         CDEBUG(D_SUPER, "MDS mount_count is %Lu, last_rcvd is %Lu\n",
1133                (unsigned long long)mds->mds_mount_count,
1134                (unsigned long long)mds->mds_last_rcvd);
1135         rc = lustre_fwrite(filp, (char *)msd, sizeof(*msd), &off);
1136         if (rc != sizeof(*msd)) {
1137                 CERROR("error writing MDS server data: rc = %d\n", rc);
1138                 if (rc > 0)
1139                         RETURN(-EIO);
1140                 RETURN(rc);
1141         }
1142 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
1143         rc = fsync_dev(filp->f_dentry->d_inode->i_rdev);
1144 #else
1145         rc = file_fsync(filp,  filp->f_dentry, 1);
1146 #endif
1147         if (rc)
1148                 CERROR("error flushing MDS server data: rc = %d\n", rc);
1149
1150         return 0;
1151 }
1152
1153 /* Do recovery actions for the MDS */
1154 static int mds_recover(struct obd_device *obddev)
1155 {
1156         struct mds_obd *mds = &obddev->u.mds;
1157         struct obd_run_ctxt saved;
1158         int rc;
1159
1160         /* This happens at the end when recovery is complete */
1161         ++mds->mds_mount_count;
1162         push_ctxt(&saved, &mds->mds_ctxt, NULL);
1163         rc = mds_update_server_data(mds);
1164         pop_ctxt(&saved);
1165
1166         return rc;
1167 }
1168
1169 /* mount the file system (secretly) */
1170 static int mds_setup(struct obd_device *obddev, obd_count len, void *buf)
1171 {
1172         struct obd_ioctl_data* data = buf;
1173         struct mds_obd *mds = &obddev->u.mds;
1174         struct vfsmount *mnt;
1175         int rc = 0;
1176         ENTRY;
1177
1178         MOD_INC_USE_COUNT;
1179 #ifdef CONFIG_DEV_RDONLY
1180         dev_clear_rdonly(2);
1181 #endif
1182         if (!data->ioc_inlbuf1 || !data->ioc_inlbuf2)
1183                 GOTO(err_dec, rc = -EINVAL);
1184
1185         mds->mds_fstype = strdup(data->ioc_inlbuf2);
1186
1187         mnt = do_kern_mount(mds->mds_fstype, 0, data->ioc_inlbuf1, NULL);
1188         if (IS_ERR(mnt)) {
1189                 rc = PTR_ERR(mnt);
1190                 CERROR("do_kern_mount failed: rc = %d\n", rc);
1191                 GOTO(err_kfree, rc);
1192         }
1193
1194         CERROR("%s: mnt is %p\n", data->ioc_inlbuf1, mnt);
1195         mds->mds_sb = mnt->mnt_root->d_inode->i_sb;
1196         if (!mds->mds_sb)
1197                 GOTO(err_put, rc = -ENODEV);
1198
1199         spin_lock_init(&mds->mds_last_lock);
1200         mds->mds_max_mdsize = sizeof(struct lov_mds_md);
1201         rc = mds_fs_setup(obddev, mnt);
1202         if (rc) {
1203                 CERROR("MDS filesystem method init failed: rc = %d\n", rc);
1204                 GOTO(err_put, rc);
1205         }
1206
1207         obddev->obd_namespace =
1208                 ldlm_namespace_new("mds_server", LDLM_NAMESPACE_SERVER);
1209         if (obddev->obd_namespace == NULL) {
1210                 mds_cleanup(obddev);
1211                 GOTO(err_fs, rc = -ENOMEM);
1212         }
1213
1214
1215         rc = mds_recover(obddev);
1216         if (rc)
1217                 GOTO(err_fs, rc);
1218
1219         ptlrpc_init_client(LDLM_REQUEST_PORTAL, LDLM_REPLY_PORTAL,
1220                            "mds_ldlm_client", &obddev->obd_ldlm_client);
1221
1222         RETURN(0);
1223
1224 err_fs:
1225         mds_fs_cleanup(obddev);
1226 err_put:
1227         unlock_kernel();
1228         mntput(mds->mds_vfsmnt);
1229         mds->mds_sb = 0;
1230         lock_kernel();
1231 err_kfree:
1232         kfree(mds->mds_fstype);
1233 err_dec:
1234         MOD_DEC_USE_COUNT;
1235         RETURN(rc);
1236 }
1237
1238 static int mds_cleanup(struct obd_device *obddev)
1239 {
1240         struct super_block *sb;
1241         struct mds_obd *mds = &obddev->u.mds;
1242         struct obd_run_ctxt saved;
1243         ENTRY;
1244
1245         sb = mds->mds_sb;
1246         if (!mds->mds_sb)
1247                 RETURN(0);
1248
1249         push_ctxt(&saved, &mds->mds_ctxt, NULL);
1250         mds_update_server_data(mds);
1251
1252         if (mds->mds_rcvd_filp) {
1253                 int rc = filp_close(mds->mds_rcvd_filp, 0);
1254                 mds->mds_rcvd_filp = NULL;
1255
1256                 if (rc)
1257                         CERROR("last_rcvd file won't close, rc=%d\n", rc);
1258         }
1259         pop_ctxt(&saved);
1260
1261         unlock_kernel();
1262         mntput(mds->mds_vfsmnt);
1263         mds->mds_sb = 0;
1264         kfree(mds->mds_fstype);
1265
1266         ldlm_namespace_free(obddev->obd_namespace);
1267
1268         lock_kernel();
1269 #ifdef CONFIG_DEV_RDONLY
1270         dev_clear_rdonly(2);
1271 #endif
1272         mds_fs_cleanup(obddev);
1273
1274         MOD_DEC_USE_COUNT;
1275         RETURN(0);
1276 }
1277
1278 static int ldlm_intent_policy(struct ldlm_lock *lock, void *req_cookie,
1279                               ldlm_mode_t mode, int flags, void *data)
1280 {
1281         struct ptlrpc_request *req = req_cookie;
1282         int rc = 0;
1283         ENTRY;
1284
1285         if (!req_cookie)
1286                 RETURN(0);
1287
1288         if (req->rq_reqmsg->bufcount > 1) {
1289                 /* an intent needs to be considered */
1290                 struct ldlm_intent *it = lustre_msg_buf(req->rq_reqmsg, 1);
1291                 struct mds_obd *mds= &req->rq_export->exp_obd->u.mds;
1292                 struct mds_body *mds_rep;
1293                 struct ldlm_reply *rep;
1294                 __u64 new_resid[3] = {0, 0, 0}, old_res;
1295                 int rc, size[3] = {sizeof(struct ldlm_reply),
1296                                                   sizeof(struct mds_body),
1297                                                   mds->mds_max_mdsize};
1298
1299                 it->opc = NTOH__u64(it->opc);
1300
1301                 LDLM_DEBUG(lock, "intent policy, opc: %s",
1302                            ldlm_it2str(it->opc));
1303
1304                 rc = lustre_pack_msg(3, size, NULL, &req->rq_replen,
1305                                      &req->rq_repmsg);
1306                 if (rc) {
1307                         rc = req->rq_status = -ENOMEM;
1308                         RETURN(rc);
1309                 }
1310
1311                 rep = lustre_msg_buf(req->rq_repmsg, 0);
1312                 rep->lock_policy_res1 = 1;
1313
1314                 /* execute policy */
1315                 switch ((long)it->opc) {
1316                 case IT_CREAT|IT_OPEN:
1317                         rc = mds_reint(2, req);
1318                         if (rc || (req->rq_status != 0 &&
1319                                    req->rq_status != -EEXIST)) {
1320                                 rep->lock_policy_res2 = req->rq_status;
1321                                 RETURN(ELDLM_LOCK_ABORTED);
1322                         }
1323                         break;
1324                 case IT_CREAT:
1325                 case IT_MKDIR:
1326                 case IT_MKNOD:
1327                 case IT_RENAME2:
1328                 case IT_LINK2:
1329                 case IT_RMDIR:
1330                 case IT_SYMLINK:
1331                 case IT_UNLINK:
1332                         rc = mds_reint(2, req);
1333                         if (rc || (req->rq_status != 0 &&
1334                                    req->rq_status != -EISDIR &&
1335                                    req->rq_status != -ENOTDIR)) {
1336                                 rep->lock_policy_res2 = req->rq_status;
1337                                 RETURN(ELDLM_LOCK_ABORTED);
1338                         }
1339                         break;
1340                 case IT_GETATTR:
1341                 case IT_LOOKUP:
1342                 case IT_OPEN:
1343                 case IT_READDIR:
1344                 case IT_READLINK:
1345                 case IT_RENAME:
1346                 case IT_LINK:
1347                 case IT_SETATTR:
1348                         rc = mds_getattr_name(2, req);
1349                         /* FIXME: we need to sit down and decide on who should
1350                          * set req->rq_status, who should return negative and
1351                          * positive return values, and what they all mean. */
1352                         if (rc || req->rq_status != 0) {
1353                                 rep->lock_policy_res2 = req->rq_status;
1354                                 RETURN(ELDLM_LOCK_ABORTED);
1355                         }
1356                         break;
1357                 case IT_READDIR|IT_OPEN:
1358                         LBUG();
1359                         break;
1360                 default:
1361                         CERROR("Unhandled intent\n");
1362                         LBUG();
1363                 }
1364
1365                 /* We don't bother returning a lock to the client for a file
1366                  * or directory we are removing.
1367                  *
1368                  * As for link and rename, there is no reason for the client
1369                  * to get a lock on the target at this point.  If they are
1370                  * going to modify the file/directory later they will get a
1371                  * lock at that time.
1372                  */
1373                 if (it->opc & (IT_UNLINK | IT_RMDIR | IT_LINK | IT_LINK2 |
1374                                IT_RENAME | IT_RENAME2))
1375                         RETURN(ELDLM_LOCK_ABORTED);
1376
1377                 rep->lock_policy_res2 = req->rq_status;
1378                 mds_rep = lustre_msg_buf(req->rq_repmsg, 1);
1379
1380                 /* If the client is about to open a file that doesn't have an MD
1381                  * stripe record, it's going to need a write lock. */
1382                 if (it->opc & IT_OPEN) {
1383                         struct lov_mds_md *lmm =
1384                                 lustre_msg_buf(req->rq_repmsg, 2);
1385                         if (lmm->lmm_easize == 0) {
1386                                 LDLM_DEBUG(lock, "open with no EA; returning PW"
1387                                            " lock");
1388                                 lock->l_req_mode = LCK_PW;
1389                         }
1390                 }
1391
1392                 if (flags & LDLM_FL_INTENT_ONLY) {
1393                         LDLM_DEBUG(lock, "INTENT_ONLY, aborting lock");
1394                         RETURN(ELDLM_LOCK_ABORTED);
1395                 }
1396                 /* Give the client a lock on the child object, instead of the
1397                  * parent that it requested. */
1398                 new_resid[0] = NTOH__u32(mds_rep->ino);
1399                 new_resid[1] = NTOH__u32(mds_rep->generation);
1400                 if (new_resid[0] == 0)
1401                         LBUG();
1402                 old_res = lock->l_resource->lr_name[0];
1403
1404                 ldlm_lock_change_resource(lock, new_resid);
1405                 if (lock->l_resource == NULL) {
1406                         LBUG();
1407                         RETURN(-ENOMEM);
1408                 }
1409                 LDLM_DEBUG(lock, "intent policy, old res %ld",
1410                            (long)old_res);
1411                 RETURN(ELDLM_LOCK_CHANGED);
1412         } else {
1413                 int size = sizeof(struct ldlm_reply);
1414                 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen,
1415                                      &req->rq_repmsg);
1416                 if (rc) {
1417                         LBUG();
1418                         RETURN(-ENOMEM);
1419                 }
1420         }
1421         RETURN(rc);
1422 }
1423
1424
1425 #define MDT_NUM_THREADS 8
1426 static int mdt_setup(struct obd_device *obddev, obd_count len, void *buf)
1427 {
1428         int i;
1429         //        struct obd_ioctl_data* data = buf;
1430         struct mds_obd *mds = &obddev->u.mds;
1431         int rc = 0;
1432         ENTRY;
1433
1434         MOD_INC_USE_COUNT;
1435
1436         mds->mds_service = ptlrpc_init_svc(MDS_NEVENTS, MDS_NBUFS,
1437                                            MDS_BUFSIZE, MDS_MAXREQSIZE,
1438                                            MDS_REQUEST_PORTAL, MDC_REPLY_PORTAL,
1439                                            "self", mds_handle, "mds");
1440         if (!mds->mds_service) {
1441                 CERROR("failed to start service\n");
1442                 GOTO(err_dec, rc = -EINVAL);
1443         }
1444
1445         for (i = 0; i < MDT_NUM_THREADS; i++) {
1446                 char name[32];
1447                 sprintf(name, "lustre_MDT_%02d", i);
1448                 rc = ptlrpc_start_thread(obddev, mds->mds_service, name);
1449                 if (rc) {
1450                         CERROR("cannot start MDT thread #%d: rc %d\n", i, rc);
1451                         GOTO(err_thread, rc);
1452                 }
1453         }
1454
1455         RETURN(0);
1456
1457 err_thread:
1458         ptlrpc_stop_all_threads(mds->mds_service);
1459         ptlrpc_unregister_service(mds->mds_service);
1460 err_dec:
1461         MOD_DEC_USE_COUNT;
1462         RETURN(rc);
1463 }
1464
1465
1466 static int mdt_cleanup(struct obd_device *obddev)
1467 {
1468         struct mds_obd *mds = &obddev->u.mds;
1469         ENTRY;
1470
1471         ptlrpc_stop_all_threads(mds->mds_service);
1472         ptlrpc_unregister_service(mds->mds_service);
1473
1474         MOD_DEC_USE_COUNT;
1475         RETURN(0);
1476 }
1477
1478 extern int mds_iocontrol(long cmd, struct lustre_handle *conn,
1479                          int len, void *karg, void *uarg);
1480
1481 /* use obd ops to offer management infrastructure */
1482 static struct obd_ops mds_obd_ops = {
1483         o_connect:     mds_connect,
1484         o_disconnect:  mds_disconnect,
1485         o_setup:       mds_setup,
1486         o_cleanup:     mds_cleanup,
1487         o_iocontrol:   mds_iocontrol
1488 };
1489
1490 static struct obd_ops mdt_obd_ops = {
1491         o_setup:       mdt_setup,
1492         o_cleanup:     mdt_cleanup,
1493 };
1494
1495
1496 static int __init mds_init(void)
1497 {
1498         mds_file_cache = kmem_cache_create("ll_mds_file_data",
1499                                            sizeof(struct mds_file_data),
1500                                            0, 0, NULL, NULL);
1501         if (mds_file_cache == NULL)
1502                 return -ENOMEM;
1503
1504         class_register_type(&mds_obd_ops, LUSTRE_MDS_NAME);
1505         class_register_type(&mdt_obd_ops, LUSTRE_MDT_NAME);
1506         ldlm_register_intent(ldlm_intent_policy);
1507         return 0;
1508 }
1509
1510 static void __exit mds_exit(void)
1511 {
1512         ldlm_unregister_intent();
1513         class_unregister_type(LUSTRE_MDS_NAME);
1514         class_unregister_type(LUSTRE_MDT_NAME);
1515         if (kmem_cache_destroy(mds_file_cache))
1516                 CERROR("couldn't free MDS file cache\n");
1517 }
1518
1519 MODULE_AUTHOR("Cluster File Systems <info@clusterfs.com>");
1520 MODULE_DESCRIPTION("Lustre Metadata Server (MDS) v0.01");
1521 MODULE_LICENSE("GPL");
1522
1523 module_init(mds_init);
1524 module_exit(mds_exit);