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