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