Whamcloud - gitweb
44ef204ee575d3246d1269b372103e02b67da54e
[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-2005 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  *   Author: Mike Shaver <shaver@clusterfs.com>
12  *
13  *   This file is part of the Lustre file system, http://www.lustre.org
14  *   Lustre is a trademark of Cluster File Systems, Inc.
15  *
16  *   You may have signed or agreed to another license before downloading
17  *   this software.  If so, you are bound by the terms and conditions
18  *   of that agreement, and the following does not apply to you.  See the
19  *   LICENSE file included with this distribution for more information.
20  *
21  *   If you did not agree to a different license, then this copy of Lustre
22  *   is open source software; you can redistribute it and/or modify it
23  *   under the terms of version 2 of the GNU General Public License as
24  *   published by the Free Software Foundation.
25  *
26  *   In either case, Lustre is distributed in the hope that it will be
27  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
28  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29  *   license text for more details.
30  */
31
32 #ifndef EXPORT_SYMTAB
33 # define EXPORT_SYMTAB
34 #endif
35 #define DEBUG_SUBSYSTEM S_MDS
36
37 #include <lustre_mds.h>
38 #include <linux/module.h>
39 #include <linux/init.h>
40 #include <linux/random.h>
41 #include <linux/fs.h>
42 #include <linux/jbd.h>
43 #include <linux/smp_lock.h>
44 #include <linux/buffer_head.h>
45 #include <linux/workqueue.h>
46 #include <linux/mount.h>
47
48 #include <linux/lustre_acl.h>
49 #include <obd_class.h>
50 #include <lustre_dlm.h>
51 #include <obd_lov.h>
52 #include <lustre_fsfilt.h>
53 #include <lprocfs_status.h>
54 #include <lustre_commit_confd.h>
55 #include <lustre_quota.h>
56 #include <lustre_disk.h>
57 #include <lustre_param.h>
58
59 #include "mds_internal.h"
60
61 int mds_num_threads;
62 CFS_MODULE_PARM(mds_num_threads, "i", int, 0444,
63                 "number of MDS service threads to start");
64
65 __u32 mds_max_ost_index=0xFFFF;
66 CFS_MODULE_PARM(mds_max_ost_index, "i", int, 0444,
67                 "maximal OST index");
68
69 static int mds_intent_policy(struct ldlm_namespace *ns,
70                              struct ldlm_lock **lockp, void *req_cookie,
71                              ldlm_mode_t mode, int flags, void *data);
72 static int mds_postsetup(struct obd_device *obd);
73 static int mds_cleanup(struct obd_device *obd);
74
75 /* Assumes caller has already pushed into the kernel filesystem context */
76 static int mds_sendpage(struct ptlrpc_request *req, struct file *file,
77                         loff_t offset, int count)
78 {
79         struct ptlrpc_bulk_desc *desc;
80         struct l_wait_info lwi;
81         struct page **pages;
82         int rc = 0, npages, i, tmpcount, tmpsize = 0;
83         ENTRY;
84
85         LASSERT((offset & ~CFS_PAGE_MASK) == 0); /* I'm dubious about this */
86
87         npages = (count + CFS_PAGE_SIZE - 1) >> CFS_PAGE_SHIFT;
88         OBD_ALLOC(pages, sizeof(*pages) * npages);
89         if (!pages)
90                 GOTO(out, rc = -ENOMEM);
91
92         desc = ptlrpc_prep_bulk_exp(req, npages, BULK_PUT_SOURCE,
93                                     MDS_BULK_PORTAL);
94         if (desc == NULL)
95                 GOTO(out_free, rc = -ENOMEM);
96
97         for (i = 0, tmpcount = count; i < npages; i++, tmpcount -= tmpsize) {
98                 tmpsize = tmpcount > CFS_PAGE_SIZE ? CFS_PAGE_SIZE : tmpcount;
99
100                 OBD_PAGE_ALLOC(pages[i], CFS_ALLOC_STD);
101                 if (pages[i] == NULL)
102                         GOTO(cleanup_buf, rc = -ENOMEM);
103
104                 ptlrpc_prep_bulk_page(desc, pages[i], 0, tmpsize);
105         }
106
107         for (i = 0, tmpcount = count; i < npages; i++, tmpcount -= tmpsize) {
108                 tmpsize = tmpcount > CFS_PAGE_SIZE ? CFS_PAGE_SIZE : tmpcount;
109                 CDEBUG(D_EXT2, "reading %u@%llu from dir %lu (size %llu)\n",
110                        tmpsize, offset, file->f_dentry->d_inode->i_ino,
111                        i_size_read(file->f_dentry->d_inode));
112
113                 rc = fsfilt_readpage(req->rq_export->exp_obd, file,
114                                      kmap(pages[i]), tmpsize, &offset);
115                 kunmap(pages[i]);
116
117                 if (rc != tmpsize)
118                         GOTO(cleanup_buf, rc = -EIO);
119         }
120
121         LASSERT(desc->bd_nob == count);
122
123         rc = ptlrpc_start_bulk_transfer(desc);
124         if (rc)
125                 GOTO(cleanup_buf, rc);
126
127         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE)) {
128                 CERROR("obd_fail_loc=%x, fail operation rc=%d\n",
129                        OBD_FAIL_MDS_SENDPAGE, rc);
130                 GOTO(abort_bulk, rc);
131         }
132
133         lwi = LWI_TIMEOUT(obd_timeout * HZ / 4, NULL, NULL);
134         rc = l_wait_event(desc->bd_waitq, !ptlrpc_bulk_active(desc), &lwi);
135         LASSERT (rc == 0 || rc == -ETIMEDOUT);
136
137         if (rc == 0) {
138                 if (desc->bd_success &&
139                     desc->bd_nob_transferred == count)
140                         GOTO(cleanup_buf, rc);
141
142                 rc = -ETIMEDOUT; /* XXX should this be a different errno? */
143         }
144
145         DEBUG_REQ(D_ERROR, req, "bulk failed: %s %d(%d), evicting %s@%s",
146                   (rc == -ETIMEDOUT) ? "timeout" : "network error",
147                   desc->bd_nob_transferred, count,
148                   req->rq_export->exp_client_uuid.uuid,
149                   req->rq_export->exp_connection->c_remote_uuid.uuid);
150
151         class_fail_export(req->rq_export);
152
153         EXIT;
154  abort_bulk:
155         ptlrpc_abort_bulk (desc);
156  cleanup_buf:
157         for (i = 0; i < npages; i++)
158                 if (pages[i])
159                         OBD_PAGE_FREE(pages[i]);
160
161         ptlrpc_free_bulk(desc);
162  out_free:
163         OBD_FREE(pages, sizeof(*pages) * npages);
164  out:
165         return rc;
166 }
167
168 /* only valid locked dentries or errors should be returned */
169 struct dentry *mds_fid2locked_dentry(struct obd_device *obd, struct ll_fid *fid,
170                                      struct vfsmount **mnt, int lock_mode,
171                                      struct lustre_handle *lockh,
172                                      __u64 lockpart)
173 {
174         struct mds_obd *mds = &obd->u.mds;
175         struct dentry *de = mds_fid2dentry(mds, fid, mnt), *retval = de;
176         struct ldlm_res_id res_id = { .name = {0} };
177         int flags = LDLM_FL_ATOMIC_CB, rc;
178         ldlm_policy_data_t policy = { .l_inodebits = { lockpart} };
179         ENTRY;
180
181         if (IS_ERR(de))
182                 RETURN(de);
183
184         res_id.name[0] = de->d_inode->i_ino;
185         res_id.name[1] = de->d_inode->i_generation;
186         rc = ldlm_cli_enqueue_local(obd->obd_namespace, &res_id,
187                                     LDLM_IBITS, &policy, lock_mode, &flags,
188                                     ldlm_blocking_ast, ldlm_completion_ast,
189                                     NULL, NULL, 0, NULL, lockh);
190         if (rc != ELDLM_OK) {
191                 l_dput(de);
192                 retval = ERR_PTR(-EIO); /* XXX translate ldlm code */
193         }
194
195         RETURN(retval);
196 }
197
198 /* Look up an entry by inode number. */
199 /* this function ONLY returns valid dget'd dentries with an initialized inode
200    or errors */
201 struct dentry *mds_fid2dentry(struct mds_obd *mds, struct ll_fid *fid,
202                               struct vfsmount **mnt)
203 {
204         char fid_name[32];
205         unsigned long ino = fid->id;
206         __u32 generation = fid->generation;
207         struct inode *inode;
208         struct dentry *result;
209
210         if (ino == 0)
211                 RETURN(ERR_PTR(-ESTALE));
212
213         snprintf(fid_name, sizeof(fid_name), "0x%lx", ino);
214
215         CDEBUG(D_DENTRY, "--> mds_fid2dentry: ino/gen %lu/%u, sb %p\n",
216                ino, generation, mds->mds_obt.obt_sb);
217
218         /* under ext3 this is neither supposed to return bad inodes
219            nor NULL inodes. */
220         result = ll_lookup_one_len(fid_name, mds->mds_fid_de, strlen(fid_name));
221         if (IS_ERR(result))
222                 RETURN(result);
223
224         inode = result->d_inode;
225         if (!inode)
226                 RETURN(ERR_PTR(-ENOENT));
227
228         if (inode->i_generation == 0 || inode->i_nlink == 0) {
229                 LCONSOLE_WARN("Found inode with zero generation or link -- this"
230                               " may indicate disk corruption (inode: %lu/%u, "
231                               "link %lu, count %d)\n", inode->i_ino,
232                               inode->i_generation,(unsigned long)inode->i_nlink,
233                               atomic_read(&inode->i_count));
234                 dput(result);
235                 RETURN(ERR_PTR(-ENOENT));
236         }
237
238         if (generation && inode->i_generation != generation) {
239                 /* we didn't find the right inode.. */
240                 CDEBUG(D_INODE, "found wrong generation: inode %lu, link: %lu, "
241                        "count: %d, generation %u/%u\n", inode->i_ino,
242                        (unsigned long)inode->i_nlink,
243                        atomic_read(&inode->i_count), inode->i_generation,
244                        generation);
245                 dput(result);
246                 RETURN(ERR_PTR(-ENOENT));
247         }
248
249         if (mnt) {
250                 *mnt = mds->mds_vfsmnt;
251                 mntget(*mnt);
252         }
253
254         RETURN(result);
255 }
256
257 static int mds_connect_internal(struct obd_export *exp,
258                                 struct obd_connect_data *data)
259 {
260         struct obd_device *obd = exp->exp_obd;
261         if (data != NULL) {
262                 data->ocd_connect_flags &= MDT_CONNECT_SUPPORTED;
263                 data->ocd_ibits_known &= MDS_INODELOCK_FULL;
264
265                 /* If no known bits (which should not happen, probably,
266                    as everybody should support LOOKUP and UPDATE bits at least)
267                    revert to compat mode with plain locks. */
268                 if (!data->ocd_ibits_known &&
269                     data->ocd_connect_flags & OBD_CONNECT_IBITS)
270                         data->ocd_connect_flags &= ~OBD_CONNECT_IBITS;
271
272                 if (!obd->u.mds.mds_fl_acl)
273                         data->ocd_connect_flags &= ~OBD_CONNECT_ACL;
274
275                 if (!obd->u.mds.mds_fl_user_xattr)
276                         data->ocd_connect_flags &= ~OBD_CONNECT_XATTR;
277
278                 exp->exp_connect_flags = data->ocd_connect_flags;
279                 data->ocd_version = LUSTRE_VERSION_CODE;
280                 exp->exp_mds_data.med_ibits_known = data->ocd_ibits_known;
281         }
282
283         if (obd->u.mds.mds_fl_acl &&
284             ((exp->exp_connect_flags & OBD_CONNECT_ACL) == 0)) {
285                 CWARN("%s: MDS requires ACL support but client does not\n",
286                       obd->obd_name);
287                 return -EBADE;
288         }
289         return 0;
290 }
291
292 static int mds_reconnect(const struct lu_env *env,
293                          struct obd_export *exp, struct obd_device *obd,
294                          struct obd_uuid *cluuid,
295                          struct obd_connect_data *data)
296 {
297         int rc;
298         ENTRY;
299
300         if (exp == NULL || obd == NULL || cluuid == NULL)
301                 RETURN(-EINVAL);
302
303         rc = mds_connect_internal(exp, data);
304
305         RETURN(rc);
306 }
307
308 /* Establish a connection to the MDS.
309  *
310  * This will set up an export structure for the client to hold state data
311  * about that client, like open files, the last operation number it did
312  * on the server, etc.
313  */
314 static int mds_connect(const struct lu_env *env,
315                        struct lustre_handle *conn, struct obd_device *obd,
316                        struct obd_uuid *cluuid, struct obd_connect_data *data)
317 {
318         struct obd_export *exp;
319         struct mds_export_data *med;
320         struct mds_client_data *mcd = NULL;
321         int rc;
322         ENTRY;
323
324         if (!conn || !obd || !cluuid)
325                 RETURN(-EINVAL);
326
327         /* XXX There is a small race between checking the list and adding a
328          * new connection for the same UUID, but the real threat (list
329          * corruption when multiple different clients connect) is solved.
330          *
331          * There is a second race between adding the export to the list,
332          * and filling in the client data below.  Hence skipping the case
333          * of NULL mcd above.  We should already be controlling multiple
334          * connects at the client, and we can't hold the spinlock over
335          * memory allocations without risk of deadlocking.
336          */
337         rc = class_connect(conn, obd, cluuid);
338         if (rc)
339                 RETURN(rc);
340         exp = class_conn2export(conn);
341         LASSERT(exp);
342         med = &exp->exp_mds_data;
343
344         exp->exp_flvr.sf_rpc = SPTLRPC_FLVR_NULL;
345
346         rc = mds_connect_internal(exp, data);
347         if (rc)
348                 GOTO(out, rc);
349
350         OBD_ALLOC(mcd, sizeof(*mcd));
351         if (!mcd)
352                 GOTO(out, rc = -ENOMEM);
353
354         memcpy(mcd->mcd_uuid, cluuid, sizeof(mcd->mcd_uuid));
355         med->med_mcd = mcd;
356
357         rc = mds_client_add(obd, exp, -1);
358         GOTO(out, rc);
359
360 out:
361         if (rc) {
362                 if (mcd) {
363                         OBD_FREE(mcd, sizeof(*mcd));
364                         med->med_mcd = NULL;
365                 }
366                 class_disconnect(exp);
367         } else {
368                 class_export_put(exp);
369         }
370
371         RETURN(rc);
372 }
373
374 int mds_init_export(struct obd_export *exp)
375 {
376         struct mds_export_data *med = &exp->exp_mds_data;
377
378         INIT_LIST_HEAD(&med->med_open_head);
379         spin_lock_init(&med->med_open_lock);
380         
381         spin_lock(&exp->exp_lock);
382         exp->exp_connecting = 1;
383         spin_unlock(&exp->exp_lock);
384
385         RETURN(0);
386 }
387
388 static int mds_destroy_export(struct obd_export *export)
389 {
390         struct mds_export_data *med;
391         struct obd_device *obd = export->exp_obd;
392         struct mds_obd *mds = &obd->u.mds;
393         struct lvfs_run_ctxt saved;
394         struct lov_mds_md *lmm;
395         struct llog_cookie *logcookies;
396         int rc = 0;
397         ENTRY;
398
399         med = &export->exp_mds_data;
400         target_destroy_export(export);
401
402         if (obd_uuid_equals(&export->exp_client_uuid, &obd->obd_uuid))
403                 RETURN(0);
404
405         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
406         /* Close any open files (which may also cause orphan unlinking). */
407
408         OBD_ALLOC(lmm, mds->mds_max_mdsize);
409         if (lmm == NULL) {
410                 CWARN("%s: allocation failure during cleanup; can not force "
411                       "close file handles on this service.\n", obd->obd_name);
412                 GOTO(out, rc = -ENOMEM);
413         }
414
415         OBD_ALLOC(logcookies, mds->mds_max_cookiesize);
416         if (logcookies == NULL) {
417                 CWARN("%s: allocation failure during cleanup; can not force "
418                       "close file handles on this service.\n", obd->obd_name);
419                 OBD_FREE(lmm, mds->mds_max_mdsize);
420                 GOTO(out_lmm, rc = -ENOMEM);
421         }
422
423         spin_lock(&med->med_open_lock);
424         while (!list_empty(&med->med_open_head)) {
425                 struct list_head *tmp = med->med_open_head.next;
426                 struct mds_file_data *mfd =
427                         list_entry(tmp, struct mds_file_data, mfd_list);
428                 int lmm_size = mds->mds_max_mdsize;
429                 umode_t mode = mfd->mfd_dentry->d_inode->i_mode;
430                 __u64 valid = 0;
431
432                 /* Remove mfd handle so it can't be found again.
433                  * We are consuming the mfd_list reference here. */
434                 mds_mfd_unlink(mfd, 0);
435                 spin_unlock(&med->med_open_lock);
436
437                 /* If you change this message, be sure to update
438                  * replay_single:test_46 */
439                 CDEBUG(D_INODE|D_IOCTL, "%s: force closing file handle for "
440                        "%.*s (ino %lu)\n", obd->obd_name,
441                        mfd->mfd_dentry->d_name.len,mfd->mfd_dentry->d_name.name,
442                        mfd->mfd_dentry->d_inode->i_ino);
443
444                 rc = mds_get_md(obd, mfd->mfd_dentry->d_inode, lmm, &lmm_size, 1);
445                 if (rc < 0)
446                         CWARN("mds_get_md failure, rc=%d\n", rc);
447                 else
448                         valid |= OBD_MD_FLEASIZE;
449
450                 /* child orphan sem protects orphan_dec_test and
451                  * is_orphan race, mds_mfd_close drops it */
452                 MDS_DOWN_WRITE_ORPHAN_SEM(mfd->mfd_dentry->d_inode);
453                 rc = mds_mfd_close(NULL, REQ_REC_OFF, obd, mfd,
454                                    !(export->exp_flags & OBD_OPT_FAILOVER),
455                                    lmm, lmm_size, logcookies,
456                                    mds->mds_max_cookiesize,
457                                    &valid);
458
459                 if (rc)
460                         CDEBUG(D_INODE|D_IOCTL, "Error closing file: %d\n", rc);
461
462                 if (valid & OBD_MD_FLCOOKIE) {
463                         rc = mds_osc_destroy_orphan(obd, mode, lmm,
464                                                     lmm_size, logcookies, 1);
465                         if (rc < 0) {
466                                 CDEBUG(D_INODE, "%s: destroy of orphan failed,"
467                                        " rc = %d\n", obd->obd_name, rc);
468                                 rc = 0;
469                         }
470                         valid &= ~OBD_MD_FLCOOKIE;
471                 }
472
473                 spin_lock(&med->med_open_lock);
474         }
475         spin_unlock(&med->med_open_lock);
476
477         OBD_FREE(logcookies, mds->mds_max_cookiesize);
478 out_lmm:
479         OBD_FREE(lmm, mds->mds_max_mdsize);
480 out:
481         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
482         mds_client_free(export);
483         RETURN(rc);
484 }
485
486 static int mds_disconnect(struct obd_export *exp)
487 {
488         int rc;
489         ENTRY;
490
491         LASSERT(exp);
492         class_export_get(exp);
493
494         /* Disconnect early so that clients can't keep using export */
495         rc = class_disconnect(exp);
496         if (exp->exp_obd->obd_namespace != NULL)
497                 ldlm_cancel_locks_for_export(exp);
498
499         /* complete all outstanding replies */
500         spin_lock(&exp->exp_lock);
501         while (!list_empty(&exp->exp_outstanding_replies)) {
502                 struct ptlrpc_reply_state *rs =
503                         list_entry(exp->exp_outstanding_replies.next,
504                                    struct ptlrpc_reply_state, rs_exp_list);
505                 struct ptlrpc_service *svc = rs->rs_service;
506
507                 spin_lock(&svc->srv_lock);
508                 list_del_init(&rs->rs_exp_list);
509                 ptlrpc_schedule_difficult_reply(rs);
510                 spin_unlock(&svc->srv_lock);
511         }
512         spin_unlock(&exp->exp_lock);
513
514         class_export_put(exp);
515         RETURN(rc);
516 }
517
518 static int mds_getstatus(struct ptlrpc_request *req)
519 {
520         struct mds_obd *mds = mds_req2mds(req);
521         struct mds_body *body;
522         int rc, size[2] = { sizeof(struct ptlrpc_body), sizeof(*body) };
523         ENTRY;
524
525         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETSTATUS_PACK))
526                 RETURN(req->rq_status = -ENOMEM);
527         rc = lustre_pack_reply(req, 2, size, NULL);
528         if (rc)
529                 RETURN(req->rq_status = rc);
530
531         body = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF, sizeof(*body));
532         memcpy(&body->fid1, &mds->mds_rootfid, sizeof(body->fid1));
533
534         /* the last_committed and last_xid fields are filled in for all
535          * replies already - no need to do so here also.
536          */
537         RETURN(0);
538 }
539
540 /* get the LOV EA from @inode and store it into @md.  It can be at most
541  * @size bytes, and @size is updated with the actual EA size.
542  * The EA size is also returned on success, and -ve errno on failure.
543  * If there is no EA then 0 is returned. */
544 int mds_get_md(struct obd_device *obd, struct inode *inode, void *md,
545                int *size, int lock)
546 {
547         int rc = 0;
548         int lmm_size;
549
550         if (lock)
551                 LOCK_INODE_MUTEX(inode);
552         rc = fsfilt_get_md(obd, inode, md, *size, "lov");
553
554         if (rc < 0) {
555                 CERROR("Error %d reading eadata for ino %lu\n",
556                        rc, inode->i_ino);
557         } else if (rc > 0) {
558                 lmm_size = rc;
559                 rc = mds_convert_lov_ea(obd, inode, md, lmm_size);
560
561                 if (rc == 0) {
562                         *size = lmm_size;
563                         rc = lmm_size;
564                 } else if (rc > 0) {
565                         *size = rc;
566                 }
567         } else {
568                 *size = 0;
569         }
570         if (lock)
571                 UNLOCK_INODE_MUTEX(inode);
572
573         RETURN (rc);
574 }
575
576
577 /* Call with lock=1 if you want mds_pack_md to take the i_mutex.
578  * Call with lock=0 if the caller has already taken the i_mutex. */
579 int mds_pack_md(struct obd_device *obd, struct lustre_msg *msg, int offset,
580                 struct mds_body *body, struct inode *inode, int lock)
581 {
582         struct mds_obd *mds = &obd->u.mds;
583         void *lmm;
584         int lmm_size;
585         int rc;
586         ENTRY;
587
588         lmm = lustre_msg_buf(msg, offset, 0);
589         if (lmm == NULL) {
590                 /* Some problem with getting eadata when I sized the reply
591                  * buffer... */
592                 CDEBUG(D_INFO, "no space reserved for inode %lu MD\n",
593                        inode->i_ino);
594                 RETURN(0);
595         }
596         lmm_size = lustre_msg_buflen(msg, offset);
597
598         /* I don't really like this, but it is a sanity check on the client
599          * MD request.  However, if the client doesn't know how much space
600          * to reserve for the MD, it shouldn't be bad to have too much space.
601          */
602         if (lmm_size > mds->mds_max_mdsize) {
603                 CWARN("Reading MD for inode %lu of %d bytes > max %d\n",
604                        inode->i_ino, lmm_size, mds->mds_max_mdsize);
605                 // RETURN(-EINVAL);
606         }
607
608         rc = mds_get_md(obd, inode, lmm, &lmm_size, lock);
609         if (rc > 0) {
610                 if (S_ISDIR(inode->i_mode))
611                         body->valid |= OBD_MD_FLDIREA;
612                 else
613                         body->valid |= OBD_MD_FLEASIZE;
614                 body->eadatasize = lmm_size;
615                 rc = 0;
616         }
617
618         RETURN(rc);
619 }
620
621 #ifdef CONFIG_FS_POSIX_ACL
622 static
623 int mds_pack_posix_acl(struct inode *inode, struct lustre_msg *repmsg,
624                        struct mds_body *repbody, int repoff)
625 {
626         struct dentry de = { .d_inode = inode };
627         int buflen, rc;
628         ENTRY;
629
630         LASSERT(repbody->aclsize == 0);
631         LASSERT(lustre_msg_bufcount(repmsg) > repoff);
632
633         buflen = lustre_msg_buflen(repmsg, repoff);
634         if (!buflen)
635                 GOTO(out, 0);
636
637         if (!inode->i_op || !inode->i_op->getxattr)
638                 GOTO(out, 0);
639
640         rc = inode->i_op->getxattr(&de, MDS_XATTR_NAME_ACL_ACCESS,
641                                    lustre_msg_buf(repmsg, repoff, buflen),
642                                    buflen);
643
644         if (rc >= 0)
645                 repbody->aclsize = rc;
646         else if (rc != -ENODATA) {
647                 CERROR("buflen %d, get acl: %d\n", buflen, rc);
648                 RETURN(rc);
649         }
650         EXIT;
651 out:
652         repbody->valid |= OBD_MD_FLACL;
653         return 0;
654 }
655 #else
656 #define mds_pack_posix_acl(inode, repmsg, repbody, repoff) 0
657 #endif
658
659 int mds_pack_acl(struct mds_export_data *med, struct inode *inode,
660                  struct lustre_msg *repmsg, struct mds_body *repbody,
661                  int repoff)
662 {
663         return mds_pack_posix_acl(inode, repmsg, repbody, repoff);
664 }
665
666 static int mds_getattr_internal(struct obd_device *obd, struct dentry *dentry,
667                                 struct ptlrpc_request *req,
668                                 struct mds_body *reqbody, int reply_off)
669 {
670         struct mds_body *body;
671         struct inode *inode = dentry->d_inode;
672         int rc = 0;
673         ENTRY;
674
675         if (inode == NULL)
676                 RETURN(-ENOENT);
677
678         body = lustre_msg_buf(req->rq_repmsg, reply_off, sizeof(*body));
679         LASSERT(body != NULL);                 /* caller prepped reply */
680
681         mds_pack_inode2fid(&body->fid1, inode);
682         body->flags = reqbody->flags; /* copy MDS_BFLAG_EXT_FLAGS if present */
683         mds_pack_inode2body(body, inode);
684         reply_off++;
685
686         if ((S_ISREG(inode->i_mode) && (reqbody->valid & OBD_MD_FLEASIZE)) ||
687             (S_ISDIR(inode->i_mode) && (reqbody->valid & OBD_MD_FLDIREA))) {
688                 rc = mds_pack_md(obd, req->rq_repmsg, reply_off, body,
689                                  inode, 1);
690
691                 /* If we have LOV EA data, the OST holds size, atime, mtime */
692                 if (!(body->valid & OBD_MD_FLEASIZE) &&
693                     !(body->valid & OBD_MD_FLDIREA))
694                         body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
695                                         OBD_MD_FLATIME | OBD_MD_FLMTIME);
696
697                 lustre_shrink_reply(req, reply_off, body->eadatasize, 0);
698                 if (body->eadatasize)
699                         reply_off++;
700         } else if (S_ISLNK(inode->i_mode) &&
701                    (reqbody->valid & OBD_MD_LINKNAME) != 0) {
702                 char *symname = lustre_msg_buf(req->rq_repmsg, reply_off, 0);
703                 int len;
704
705                 LASSERT (symname != NULL);       /* caller prepped reply */
706                 len = lustre_msg_buflen(req->rq_repmsg, reply_off);
707
708                 rc = inode->i_op->readlink(dentry, symname, len);
709                 if (rc < 0) {
710                         CERROR("readlink failed: %d\n", rc);
711                 } else if (rc != len - 1) {
712                         CERROR ("Unexpected readlink rc %d: expecting %d\n",
713                                 rc, len - 1);
714                         rc = -EINVAL;
715                 } else {
716                         CDEBUG(D_INODE, "read symlink dest %s\n", symname);
717                         body->valid |= OBD_MD_LINKNAME;
718                         body->eadatasize = rc + 1;
719                         symname[rc] = 0;        /* NULL terminate */
720                         rc = 0;
721                 }
722                 reply_off++;
723         } else if (reqbody->valid == OBD_MD_FLFLAGS &&
724                    reqbody->flags & MDS_BFLAG_EXT_FLAGS) {
725                 int flags;
726
727                 /* We only return the full set of flags on ioctl, otherwise we
728                  * get enough flags from the inode in mds_pack_inode2body(). */
729                 rc = fsfilt_iocontrol(obd, inode, NULL, EXT3_IOC_GETFLAGS,
730                                       (long)&flags);
731                 if (rc == 0)
732                         body->flags = flags | MDS_BFLAG_EXT_FLAGS;
733         }
734
735         if (reqbody->valid & OBD_MD_FLMODEASIZE) {
736                 struct mds_obd *mds = mds_req2mds(req);
737                 body->max_cookiesize = mds->mds_max_cookiesize;
738                 body->max_mdsize = mds->mds_max_mdsize;
739                 body->valid |= OBD_MD_FLMODEASIZE;
740         }
741
742         if (rc)
743                 RETURN(rc);
744
745 #ifdef CONFIG_FS_POSIX_ACL
746         if ((req->rq_export->exp_connect_flags & OBD_CONNECT_ACL) &&
747             (reqbody->valid & OBD_MD_FLACL)) {
748                 rc = mds_pack_acl(&req->rq_export->exp_mds_data,
749                                   inode, req->rq_repmsg,
750                                   body, reply_off);
751
752                 lustre_shrink_reply(req, reply_off, body->aclsize, 0);
753                 if (body->aclsize)
754                         reply_off++;
755         }
756 #endif
757
758         RETURN(rc);
759 }
760
761 static int mds_getattr_pack_msg(struct ptlrpc_request *req, struct inode *inode,
762                                 int offset)
763 {
764         struct mds_obd *mds = mds_req2mds(req);
765         struct mds_body *body;
766         int rc, bufcount = 2;
767         int size[4] = { sizeof(struct ptlrpc_body), sizeof(*body) };
768         ENTRY;
769
770         LASSERT(offset == REQ_REC_OFF); /* non-intent */
771
772         body = lustre_msg_buf(req->rq_reqmsg, offset, sizeof(*body));
773         LASSERT(body != NULL);                    /* checked by caller */
774         LASSERT(lustre_req_swabbed(req, offset)); /* swabbed by caller */
775
776         if ((S_ISREG(inode->i_mode) && (body->valid & OBD_MD_FLEASIZE)) ||
777             (S_ISDIR(inode->i_mode) && (body->valid & OBD_MD_FLDIREA))) {
778                 LOCK_INODE_MUTEX(inode);
779                 rc = fsfilt_get_md(req->rq_export->exp_obd, inode, NULL, 0,
780                                    "lov");
781                 UNLOCK_INODE_MUTEX(inode);
782                 CDEBUG(D_INODE, "got %d bytes MD data for inode %lu\n",
783                        rc, inode->i_ino);
784                 if (rc < 0) {
785                         if (rc != -ENODATA) {
786                                 CERROR("error getting inode %lu MD: rc = %d\n",
787                                        inode->i_ino, rc);
788                                 RETURN(rc);
789                         }
790                         size[bufcount] = 0;
791                 } else if (rc > mds->mds_max_mdsize) {
792                         size[bufcount] = 0;
793                         CERROR("MD size %d larger than maximum possible %u\n",
794                                rc, mds->mds_max_mdsize);
795                 } else {
796                         size[bufcount] = rc;
797                 }
798                 bufcount++;
799         } else if (S_ISLNK(inode->i_mode) && (body->valid & OBD_MD_LINKNAME)) {
800                 if (i_size_read(inode) + 1 != body->eadatasize)
801                         CERROR("symlink size: %Lu, reply space: %d\n",
802                                i_size_read(inode) + 1, body->eadatasize);
803                 size[bufcount] = min_t(int, i_size_read(inode) + 1,
804                                        body->eadatasize);
805                 bufcount++;
806                 CDEBUG(D_INODE, "symlink size: %Lu, reply space: %d\n",
807                        i_size_read(inode) + 1, body->eadatasize);
808         }
809
810 #ifdef CONFIG_FS_POSIX_ACL
811         if ((req->rq_export->exp_connect_flags & OBD_CONNECT_ACL) &&
812             (body->valid & OBD_MD_FLACL)) {
813                 struct dentry de = { .d_inode = inode };
814
815                 size[bufcount] = 0;
816                 if (inode->i_op && inode->i_op->getxattr) {
817                         rc = inode->i_op->getxattr(&de, MDS_XATTR_NAME_ACL_ACCESS,
818                                                    NULL, 0);
819
820                         if (rc < 0) {
821                                 if (rc != -ENODATA) {
822                                         CERROR("got acl size: %d\n", rc);
823                                         RETURN(rc);
824                                 }
825                         } else
826                                 size[bufcount] = rc;
827                 }
828                 bufcount++;
829         }
830 #endif
831
832         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK)) {
833                 CERROR("failed MDS_GETATTR_PACK test\n");
834                 req->rq_status = -ENOMEM;
835                 RETURN(-ENOMEM);
836         }
837
838         rc = lustre_pack_reply(req, bufcount, size, NULL);
839         if (rc) {
840                 req->rq_status = rc;
841                 RETURN(rc);
842         }
843
844         RETURN(0);
845 }
846
847 static int mds_getattr_lock(struct ptlrpc_request *req, int offset,
848                             int child_part, struct lustre_handle *child_lockh)
849 {
850         struct obd_device *obd = req->rq_export->exp_obd;
851         struct mds_obd *mds = &obd->u.mds;
852         struct ldlm_reply *rep = NULL;
853         struct lvfs_run_ctxt saved;
854         struct mds_body *body;
855         struct dentry *dparent = NULL, *dchild = NULL;
856         struct lvfs_ucred uc = {0,};
857         struct lustre_handle parent_lockh;
858         int namesize;
859         int rc = 0, cleanup_phase = 0, resent_req = 0;
860         char *name;
861         ENTRY;
862
863         LASSERT(!strcmp(obd->obd_type->typ_name, LUSTRE_MDS_NAME));
864
865         /* Swab now, before anyone looks inside the request */
866         body = lustre_swab_reqbuf(req, offset, sizeof(*body),
867                                   lustre_swab_mds_body);
868         if (body == NULL) {
869                 CERROR("Can't swab mds_body\n");
870                 RETURN(-EFAULT);
871         }
872
873         lustre_set_req_swabbed(req, offset + 1);
874         name = lustre_msg_string(req->rq_reqmsg, offset + 1, 0);
875         if (name == NULL) {
876                 CERROR("Can't unpack name\n");
877                 RETURN(-EFAULT);
878         }
879         namesize = lustre_msg_buflen(req->rq_reqmsg, offset + 1);
880         /* namesize less than 2 means we have empty name, probably came from
881            revalidate by cfid, so no point in having name to be set */
882         if (namesize <= 1)
883                 name = NULL;
884
885         rc = mds_init_ucred(&uc, req, offset);
886         if (rc)
887                 GOTO(cleanup, rc);
888
889         LASSERT(offset == REQ_REC_OFF || offset == DLM_INTENT_REC_OFF);
890         /* if requests were at offset 2, the getattr reply goes back at 1 */
891         if (offset == DLM_INTENT_REC_OFF) {
892                 rep = lustre_msg_buf(req->rq_repmsg, DLM_LOCKREPLY_OFF,
893                                      sizeof(*rep));
894                 offset = DLM_REPLY_REC_OFF;
895         }
896
897         push_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
898         cleanup_phase = 1; /* kernel context */
899         intent_set_disposition(rep, DISP_LOOKUP_EXECD);
900
901         /* FIXME: handle raw lookup */
902 #if 0
903         if (body->valid == OBD_MD_FLID) {
904                 struct mds_body *mds_reply;
905                 int size = sizeof(*mds_reply);
906                 ino_t inum;
907                 // The user requested ONLY the inode number, so do a raw lookup
908                 rc = lustre_pack_reply(req, 1, &size, NULL);
909                 if (rc) {
910                         CERROR("out of memory\n");
911                         GOTO(cleanup, rc);
912                 }
913
914                 rc = dir->i_op->lookup_raw(dir, name, namesize - 1, &inum);
915
916                 mds_reply = lustre_msg_buf(req->rq_repmsg, offset,
917                                            sizeof(*mds_reply));
918                 mds_reply->fid1.id = inum;
919                 mds_reply->valid = OBD_MD_FLID;
920                 GOTO(cleanup, rc);
921         }
922 #endif
923
924         if (lustre_handle_is_used(child_lockh)) {
925                 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT);
926                 resent_req = 1;
927         }
928
929         if (resent_req == 0) {
930                 if (name) {
931                         OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RESEND, obd_timeout * 2);
932                         rc = mds_get_parent_child_locked(obd, &obd->u.mds,
933                                                          &body->fid1,
934                                                          &parent_lockh,
935                                                          &dparent, LCK_CR,
936                                                          MDS_INODELOCK_UPDATE,
937                                                          name, namesize,
938                                                          child_lockh, &dchild,
939                                                          LCK_CR, child_part);
940                 } else {
941                         /* For revalidate by fid we always take UPDATE lock */
942                         dchild = mds_fid2locked_dentry(obd, &body->fid2, NULL,
943                                                        LCK_CR, child_lockh,
944                                                        child_part);
945                         LASSERT(dchild);
946                         if (IS_ERR(dchild))
947                                 rc = PTR_ERR(dchild);
948                 }
949                 if (rc)
950                         GOTO(cleanup, rc);
951         } else {
952                 struct ldlm_lock *granted_lock;
953                 struct ll_fid child_fid;
954                 struct ldlm_resource *res;
955                 DEBUG_REQ(D_DLMTRACE, req, "resent, not enqueuing new locks");
956                 granted_lock = ldlm_handle2lock(child_lockh);
957                 LASSERTF(granted_lock != NULL, LPU64"/%u lockh "LPX64"\n",
958                          body->fid1.id, body->fid1.generation,
959                          child_lockh->cookie);
960
961
962                 res = granted_lock->l_resource;
963                 child_fid.id = res->lr_name.name[0];
964                 child_fid.generation = res->lr_name.name[1];
965                 dchild = mds_fid2dentry(&obd->u.mds, &child_fid, NULL);
966                 LASSERT(!IS_ERR(dchild));
967                 LDLM_LOCK_PUT(granted_lock);
968         }
969
970         cleanup_phase = 2; /* dchild, dparent, locks */
971
972         if (dchild->d_inode == NULL) {
973                 intent_set_disposition(rep, DISP_LOOKUP_NEG);
974                 /* in the intent case, the policy clears this error:
975                    the disposition is enough */
976                 GOTO(cleanup, rc = -ENOENT);
977         } else {
978                 intent_set_disposition(rep, DISP_LOOKUP_POS);
979         }
980
981         if (req->rq_repmsg == NULL) {
982                 rc = mds_getattr_pack_msg(req, dchild->d_inode, offset);
983                 if (rc != 0) {
984                         CERROR ("mds_getattr_pack_msg: %d\n", rc);
985                         GOTO (cleanup, rc);
986                 }
987         }
988
989         rc = mds_getattr_internal(obd, dchild, req, body, offset);
990         GOTO(cleanup, rc); /* returns the lock to the client */
991
992  cleanup:
993         switch (cleanup_phase) {
994         case 2:
995                 if (resent_req == 0) {
996                         if (rc && dchild->d_inode)
997                                 ldlm_lock_decref(child_lockh, LCK_CR);
998                         if (name) {
999                                 ldlm_lock_decref(&parent_lockh, LCK_CR);
1000                                 l_dput(dparent);
1001                         }
1002                 }
1003                 l_dput(dchild);
1004         case 1:
1005                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1006         default:
1007                 mds_exit_ucred(&uc, mds);
1008                 if (req->rq_reply_state == NULL) {
1009                         int rc2 = lustre_pack_reply(req, 1, NULL, NULL);
1010                         if (rc == 0)
1011                                 rc = rc2;
1012                         req->rq_status = rc;
1013                 }
1014         }
1015         return rc;
1016 }
1017
1018 static int mds_getattr(struct ptlrpc_request *req, int offset)
1019 {
1020         struct mds_obd *mds = mds_req2mds(req);
1021         struct obd_device *obd = req->rq_export->exp_obd;
1022         struct lvfs_run_ctxt saved;
1023         struct dentry *de;
1024         struct mds_body *body;
1025         struct lvfs_ucred uc = {0,};
1026         int rc = 0;
1027         ENTRY;
1028
1029         OBD_COUNTER_INCREMENT(obd, getattr);
1030
1031         body = lustre_swab_reqbuf(req, offset, sizeof(*body),
1032                                   lustre_swab_mds_body);
1033         if (body == NULL)
1034                 RETURN(-EFAULT);
1035
1036         rc = mds_init_ucred(&uc, req, offset);
1037         if (rc)
1038                 GOTO(out_ucred, rc);
1039
1040         push_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1041         de = mds_fid2dentry(mds, &body->fid1, NULL);
1042         if (IS_ERR(de)) {
1043                 rc = req->rq_status = PTR_ERR(de);
1044                 GOTO(out_pop, rc);
1045         }
1046
1047         rc = mds_getattr_pack_msg(req, de->d_inode, offset);
1048         if (rc != 0) {
1049                 CERROR("mds_getattr_pack_msg: %d\n", rc);
1050                 GOTO(out_pop, rc);
1051         }
1052
1053         req->rq_status = mds_getattr_internal(obd, de, req, body,
1054                                               REPLY_REC_OFF);
1055
1056         l_dput(de);
1057         GOTO(out_pop, rc);
1058 out_pop:
1059         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1060 out_ucred:
1061         if (req->rq_reply_state == NULL) {
1062                 int rc2 = lustre_pack_reply(req, 1, NULL, NULL);
1063                 if (rc == 0)
1064                         rc = rc2;
1065                 req->rq_status = rc;
1066         }
1067         mds_exit_ucred(&uc, mds);
1068         return rc;
1069 }
1070
1071 static int mds_obd_statfs(struct obd_device *obd, struct obd_statfs *osfs,
1072                           __u64 max_age)
1073 {
1074         int rc;
1075
1076         spin_lock(&obd->obd_osfs_lock);
1077         rc = fsfilt_statfs(obd, obd->u.obt.obt_sb, max_age);
1078         if (rc == 0)
1079                 memcpy(osfs, &obd->obd_osfs, sizeof(*osfs));
1080         spin_unlock(&obd->obd_osfs_lock);
1081
1082         return rc;
1083 }
1084
1085 static int mds_statfs(struct ptlrpc_request *req)
1086 {
1087         struct obd_device *obd = req->rq_export->exp_obd;
1088         int rc, size[2] = { sizeof(struct ptlrpc_body),
1089                             sizeof(struct obd_statfs) };
1090         ENTRY;
1091
1092         /* This will trigger a watchdog timeout */
1093         OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_STATFS_LCW_SLEEP,
1094                          (MDS_SERVICE_WATCHDOG_TIMEOUT / 1000) + 1);
1095         OBD_COUNTER_INCREMENT(obd, statfs);
1096
1097         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK))
1098                 GOTO(out, rc = -ENOMEM);
1099         rc = lustre_pack_reply(req, 2, size, NULL);
1100         if (rc)
1101                 GOTO(out, rc);
1102
1103         /* We call this so that we can cache a bit - 1 jiffie worth */
1104         rc = mds_obd_statfs(obd, lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
1105                                                 size[REPLY_REC_OFF]),
1106                             cfs_time_current_64() - HZ);
1107         if (rc) {
1108                 CERROR("mds_obd_statfs failed: rc %d\n", rc);
1109                 GOTO(out, rc);
1110         }
1111
1112         EXIT;
1113 out:
1114         req->rq_status = rc;
1115         return 0;
1116 }
1117
1118 static int mds_sync(struct ptlrpc_request *req, int offset)
1119 {
1120         struct obd_device *obd = req->rq_export->exp_obd;
1121         struct mds_obd *mds = &obd->u.mds;
1122         struct mds_body *body;
1123         int rc, size[2] = { sizeof(struct ptlrpc_body), sizeof(*body) };
1124         ENTRY;
1125
1126         body = lustre_swab_reqbuf(req, offset, sizeof(*body),
1127                                   lustre_swab_mds_body);
1128         if (body == NULL)
1129                 GOTO(out, rc = -EFAULT);
1130
1131         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
1132                 GOTO(out, rc = -ENOMEM);
1133         rc = lustre_pack_reply(req, 2, size, NULL);
1134         if (rc)
1135                 GOTO(out, rc);
1136
1137         if (body->fid1.id == 0) {
1138                 /* a fid of zero is taken to mean "sync whole filesystem" */
1139                 rc = fsfilt_sync(obd, obd->u.obt.obt_sb);
1140                 GOTO(out, rc);
1141         } else {
1142                 struct dentry *de;
1143
1144                 de = mds_fid2dentry(mds, &body->fid1, NULL);
1145                 if (IS_ERR(de))
1146                         GOTO(out, rc = PTR_ERR(de));
1147
1148                 /* The file parameter isn't used for anything */
1149                 if (de->d_inode->i_fop && de->d_inode->i_fop->fsync)
1150                         rc = de->d_inode->i_fop->fsync(NULL, de, 1);
1151                 if (rc == 0) {
1152                         body = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
1153                                               sizeof(*body));
1154                         mds_pack_inode2fid(&body->fid1, de->d_inode);
1155                         mds_pack_inode2body(body, de->d_inode);
1156                 }
1157
1158                 l_dput(de);
1159                 GOTO(out, rc);
1160         }
1161 out:
1162         req->rq_status = rc;
1163         return 0;
1164 }
1165
1166 /* mds_readpage does not take a DLM lock on the inode, because the client must
1167  * already have a PR lock.
1168  *
1169  * If we were to take another one here, a deadlock will result, if another
1170  * thread is already waiting for a PW lock. */
1171 static int mds_readpage(struct ptlrpc_request *req, int offset)
1172 {
1173         struct obd_device *obd = req->rq_export->exp_obd;
1174         struct mds_obd *mds = &obd->u.mds;
1175         struct vfsmount *mnt;
1176         struct dentry *de;
1177         struct file *file;
1178         struct mds_body *body, *repbody;
1179         struct lvfs_run_ctxt saved;
1180         int rc, size[2] = { sizeof(struct ptlrpc_body), sizeof(*repbody) };
1181         struct lvfs_ucred uc = {0,};
1182         ENTRY;
1183
1184         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK))
1185                 RETURN(-ENOMEM);
1186         rc = lustre_pack_reply(req, 2, size, NULL);
1187         if (rc)
1188                 GOTO(out, rc);
1189
1190         body = lustre_swab_reqbuf(req, offset, sizeof(*body),
1191                                   lustre_swab_mds_body);
1192         if (body == NULL)
1193                 GOTO (out, rc = -EFAULT);
1194
1195         rc = mds_init_ucred(&uc, req, offset);
1196         if (rc)
1197                 GOTO(out, rc);
1198
1199         push_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1200         de = mds_fid2dentry(&obd->u.mds, &body->fid1, &mnt);
1201         if (IS_ERR(de))
1202                 GOTO(out_pop, rc = PTR_ERR(de));
1203
1204         CDEBUG(D_INODE, "ino %lu\n", de->d_inode->i_ino);
1205
1206         file = dentry_open(de, mnt, O_RDONLY | O_LARGEFILE);
1207         /* note: in case of an error, dentry_open puts dentry */
1208         if (IS_ERR(file))
1209                 GOTO(out_pop, rc = PTR_ERR(file));
1210
1211         /* body->size is actually the offset -eeb */
1212         if ((body->size & (de->d_inode->i_sb->s_blocksize - 1)) != 0) {
1213                 CERROR("offset "LPU64" not on a block boundary of %lu\n",
1214                        body->size, de->d_inode->i_sb->s_blocksize);
1215                 GOTO(out_file, rc = -EFAULT);
1216         }
1217
1218         /* body->nlink is actually the #bytes to read -eeb */
1219         if (body->nlink & (de->d_inode->i_sb->s_blocksize - 1)) {
1220                 CERROR("size %u is not multiple of blocksize %lu\n",
1221                        body->nlink, de->d_inode->i_sb->s_blocksize);
1222                 GOTO(out_file, rc = -EFAULT);
1223         }
1224
1225         repbody = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
1226                                  sizeof(*repbody));
1227         repbody->size = i_size_read(file->f_dentry->d_inode);
1228         repbody->valid = OBD_MD_FLSIZE;
1229
1230         /* to make this asynchronous make sure that the handling function
1231            doesn't send a reply when this function completes. Instead a
1232            callback function would send the reply */
1233         /* body->size is actually the offset -eeb */
1234         rc = mds_sendpage(req, file, body->size, body->nlink);
1235
1236 out_file:
1237         filp_close(file, 0);
1238 out_pop:
1239         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1240 out:
1241         mds_exit_ucred(&uc, mds);
1242         req->rq_status = rc;
1243         RETURN(0);
1244 }
1245
1246 int mds_reint(struct ptlrpc_request *req, int offset,
1247               struct lustre_handle *lockh)
1248 {
1249         struct mds_update_record *rec; /* 116 bytes on the stack?  no sir! */
1250         int rc;
1251
1252         OBD_ALLOC(rec, sizeof(*rec));
1253         if (rec == NULL)
1254                 RETURN(-ENOMEM);
1255
1256         rc = mds_update_unpack(req, offset, rec);
1257         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNPACK)) {
1258                 CERROR("invalid record\n");
1259                 GOTO(out, req->rq_status = -EINVAL);
1260         }
1261
1262         /* rc will be used to interrupt a for loop over multiple records */
1263         rc = mds_reint_rec(rec, offset, req, lockh);
1264  out:
1265         OBD_FREE(rec, sizeof(*rec));
1266         return rc;
1267 }
1268
1269 int mds_filter_recovery_request(struct ptlrpc_request *req,
1270                                 struct obd_device *obd, int *process)
1271 {
1272         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1273         case MDS_CONNECT: /* This will never get here, but for completeness. */
1274         case OST_CONNECT: /* This will never get here, but for completeness. */
1275         case MDS_DISCONNECT:
1276         case OST_DISCONNECT:
1277                *process = 1;
1278                RETURN(0);
1279
1280         case MDS_CLOSE:
1281         case MDS_DONE_WRITING:
1282         case MDS_SYNC: /* used in unmounting */
1283         case OBD_PING:
1284         case MDS_REINT:
1285         case SEQ_QUERY:
1286         case FLD_QUERY:
1287         case LDLM_ENQUEUE:
1288                 *process = target_queue_recovery_request(req, obd);
1289                 RETURN(0);
1290
1291         default:
1292                 DEBUG_REQ(D_ERROR, req, "not permitted during recovery");
1293                 *process = -EAGAIN;
1294                 RETURN(0);
1295         }
1296 }
1297 EXPORT_SYMBOL(mds_filter_recovery_request);
1298
1299 static char *reint_names[] = {
1300         [REINT_SETATTR] "setattr",
1301         [REINT_CREATE]  "create",
1302         [REINT_LINK]    "link",
1303         [REINT_UNLINK]  "unlink",
1304         [REINT_RENAME]  "rename",
1305         [REINT_OPEN]    "open",
1306 };
1307
1308 static int mds_set_info_rpc(struct obd_export *exp, struct ptlrpc_request *req)
1309 {
1310         void *key, *val;
1311         int keylen, vallen, rc = 0;
1312         ENTRY;
1313
1314         key = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF, 1);
1315         if (key == NULL) {
1316                 DEBUG_REQ(D_HA, req, "no set_info key");
1317                 RETURN(-EFAULT);
1318         }
1319         keylen = lustre_msg_buflen(req->rq_reqmsg, REQ_REC_OFF);
1320
1321         val = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF + 1, 0);
1322         vallen = lustre_msg_buflen(req->rq_reqmsg, REQ_REC_OFF + 1);
1323
1324         rc = lustre_pack_reply(req, 1, NULL, NULL);
1325         if (rc)
1326                 RETURN(rc);
1327
1328         lustre_msg_set_status(req->rq_repmsg, 0);
1329
1330         if (KEY_IS("read-only")) {
1331                 if (val == NULL || vallen < sizeof(__u32)) {
1332                         DEBUG_REQ(D_HA, req, "no set_info val");
1333                         RETURN(-EFAULT);
1334                 }
1335
1336                 if (*(__u32 *)val)
1337                         exp->exp_connect_flags |= OBD_CONNECT_RDONLY;
1338                 else
1339                         exp->exp_connect_flags &= ~OBD_CONNECT_RDONLY;
1340         } else {
1341                 RETURN(-EINVAL);
1342         }
1343
1344         RETURN(0);
1345 }
1346
1347 static int mds_handle_quotacheck(struct ptlrpc_request *req)
1348 {
1349         struct obd_quotactl *oqctl;
1350         int rc;
1351         ENTRY;
1352
1353         oqctl = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*oqctl),
1354                                    lustre_swab_obd_quotactl);
1355         if (oqctl == NULL)
1356                 RETURN(-EPROTO);
1357
1358         rc = lustre_pack_reply(req, 1, NULL, NULL);
1359         if (rc)
1360                 RETURN(rc);
1361
1362         req->rq_status = obd_quotacheck(req->rq_export, oqctl);
1363         RETURN(0);
1364 }
1365
1366 static int mds_handle_quotactl(struct ptlrpc_request *req)
1367 {
1368         struct obd_quotactl *oqctl, *repoqc;
1369         int rc, size[2] = { sizeof(struct ptlrpc_body), sizeof(*repoqc) };
1370         ENTRY;
1371
1372         oqctl = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*oqctl),
1373                                    lustre_swab_obd_quotactl);
1374         if (oqctl == NULL)
1375                 RETURN(-EPROTO);
1376
1377         rc = lustre_pack_reply(req, 2, size, NULL);
1378         if (rc)
1379                 RETURN(rc);
1380
1381         repoqc = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF, sizeof(*repoqc));
1382
1383         req->rq_status = obd_quotactl(req->rq_export, oqctl);
1384         *repoqc = *oqctl;
1385         RETURN(0);
1386 }
1387
1388 int mds_msg_check_version(struct lustre_msg *msg)
1389 {
1390         int rc;
1391
1392         switch (lustre_msg_get_opc(msg)) {
1393         case MDS_CONNECT:
1394         case MDS_DISCONNECT:
1395         case OBD_PING:
1396         case SEC_CTX_INIT:
1397         case SEC_CTX_INIT_CONT:
1398         case SEC_CTX_FINI:
1399                 rc = lustre_msg_check_version(msg, LUSTRE_OBD_VERSION);
1400                 if (rc)
1401                         CERROR("bad opc %u version %08x, expecting %08x\n",
1402                                lustre_msg_get_opc(msg),
1403                                lustre_msg_get_version(msg),
1404                                LUSTRE_OBD_VERSION);
1405                 break;
1406         case MDS_GETSTATUS:
1407         case MDS_GETATTR:
1408         case MDS_GETATTR_NAME:
1409         case MDS_STATFS:
1410         case MDS_READPAGE:
1411         case MDS_WRITEPAGE:
1412         case MDS_IS_SUBDIR:
1413         case MDS_REINT:
1414         case MDS_CLOSE:
1415         case MDS_DONE_WRITING:
1416         case MDS_PIN:
1417         case MDS_SYNC:
1418         case MDS_GETXATTR:
1419         case MDS_SETXATTR:
1420         case MDS_SET_INFO:
1421         case MDS_QUOTACHECK:
1422         case MDS_QUOTACTL:
1423         case QUOTA_DQACQ:
1424         case QUOTA_DQREL:
1425         case SEQ_QUERY:
1426         case FLD_QUERY:
1427                 rc = lustre_msg_check_version(msg, LUSTRE_MDS_VERSION);
1428                 if (rc)
1429                         CERROR("bad opc %u version %08x, expecting %08x\n",
1430                                lustre_msg_get_opc(msg),
1431                                lustre_msg_get_version(msg),
1432                                LUSTRE_MDS_VERSION);
1433                 break;
1434         case LDLM_ENQUEUE:
1435         case LDLM_CONVERT:
1436         case LDLM_BL_CALLBACK:
1437         case LDLM_CP_CALLBACK:
1438                 rc = lustre_msg_check_version(msg, LUSTRE_DLM_VERSION);
1439                 if (rc)
1440                         CERROR("bad opc %u version %08x, expecting %08x\n",
1441                                lustre_msg_get_opc(msg),
1442                                lustre_msg_get_version(msg),
1443                                LUSTRE_DLM_VERSION);
1444                 break;
1445         case OBD_LOG_CANCEL:
1446         case LLOG_ORIGIN_HANDLE_CREATE:
1447         case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
1448         case LLOG_ORIGIN_HANDLE_READ_HEADER:
1449         case LLOG_ORIGIN_HANDLE_CLOSE:
1450         case LLOG_ORIGIN_HANDLE_DESTROY:
1451         case LLOG_ORIGIN_HANDLE_PREV_BLOCK:
1452         case LLOG_CATINFO:
1453                 rc = lustre_msg_check_version(msg, LUSTRE_LOG_VERSION);
1454                 if (rc)
1455                         CERROR("bad opc %u version %08x, expecting %08x\n",
1456                                lustre_msg_get_opc(msg),
1457                                lustre_msg_get_version(msg),
1458                                LUSTRE_LOG_VERSION);
1459                 break;
1460         default:
1461                 CERROR("MDS unknown opcode %d\n", lustre_msg_get_opc(msg));
1462                 rc = -ENOTSUPP;
1463         }
1464         return rc;
1465 }
1466 EXPORT_SYMBOL(mds_msg_check_version);
1467
1468 int mds_handle(struct ptlrpc_request *req)
1469 {
1470         int should_process, fail = OBD_FAIL_MDS_ALL_REPLY_NET;
1471         int rc;
1472         struct mds_obd *mds = NULL; /* quell gcc overwarning */
1473         struct obd_device *obd = NULL;
1474         ENTRY;
1475
1476         if (OBD_FAIL_CHECK_ORSET(OBD_FAIL_MDS_ALL_REQUEST_NET, OBD_FAIL_ONCE))
1477                 RETURN(0);
1478
1479         LASSERT(current->journal_info == NULL);
1480
1481         rc = mds_msg_check_version(req->rq_reqmsg);
1482         if (rc) {
1483                 CERROR("MDS drop mal-formed request\n");
1484                 RETURN(rc);
1485         }
1486
1487         /* XXX identical to OST */
1488         if (lustre_msg_get_opc(req->rq_reqmsg) != MDS_CONNECT) {
1489                 struct mds_export_data *med;
1490                 int recovering;
1491
1492                 if (req->rq_export == NULL) {
1493                         CERROR("operation %d on unconnected MDS from %s\n",
1494                                lustre_msg_get_opc(req->rq_reqmsg),
1495                                libcfs_id2str(req->rq_peer));
1496                         req->rq_status = -ENOTCONN;
1497                         GOTO(out, rc = -ENOTCONN);
1498                 }
1499
1500                 med = &req->rq_export->exp_mds_data;
1501                 obd = req->rq_export->exp_obd;
1502                 mds = mds_req2mds(req);
1503
1504                 /* sanity check: if the xid matches, the request must
1505                  * be marked as a resent or replayed */
1506                 if (req->rq_xid == le64_to_cpu(med->med_mcd->mcd_last_xid) ||
1507                    req->rq_xid == le64_to_cpu(med->med_mcd->mcd_last_close_xid))
1508                         if (!(lustre_msg_get_flags(req->rq_reqmsg) &
1509                                  (MSG_RESENT | MSG_REPLAY))) {
1510                                 CERROR("rq_xid "LPU64" matches last_xid, "
1511                                        "expected RESENT flag\n",
1512                                         req->rq_xid);
1513                                 req->rq_status = -ENOTCONN;
1514                                 GOTO(out, rc = -EFAULT);
1515                         }
1516                 /* else: note the opposite is not always true; a
1517                  * RESENT req after a failover will usually not match
1518                  * the last_xid, since it was likely never
1519                  * committed. A REPLAYed request will almost never
1520                  * match the last xid, however it could for a
1521                  * committed, but still retained, open. */
1522
1523                 /* Check for aborted recovery. */
1524                 spin_lock_bh(&obd->obd_processing_task_lock);
1525                 recovering = obd->obd_recovering;
1526                 spin_unlock_bh(&obd->obd_processing_task_lock);
1527                 if (recovering) {
1528                         rc = mds_filter_recovery_request(req, obd,
1529                                                          &should_process);
1530                         if (rc || !should_process)
1531                                 RETURN(rc);
1532                         else if (should_process < 0) {
1533                                 req->rq_status = should_process;
1534                                 rc = ptlrpc_error(req);
1535                                 RETURN(rc);
1536                         }
1537                 }
1538         }
1539
1540         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1541         case MDS_CONNECT:
1542                 DEBUG_REQ(D_INODE, req, "connect");
1543                 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_CONNECT_NET))
1544                         RETURN(0);
1545                 rc = target_handle_connect(req);
1546                 if (!rc) {
1547                         /* Now that we have an export, set mds. */
1548                         /*
1549                          * XXX nikita: these assignments are useless: mds is
1550                          * never used below, and obd is only used for
1551                          * MSG_LAST_REPLAY case, which never happens for
1552                          * MDS_CONNECT.
1553                          */
1554                         obd = req->rq_export->exp_obd;
1555                         mds = mds_req2mds(req);
1556                 }
1557                 break;
1558
1559         case MDS_DISCONNECT:
1560                 DEBUG_REQ(D_INODE, req, "disconnect");
1561                 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_DISCONNECT_NET))
1562                         RETURN(0);
1563                 rc = target_handle_disconnect(req);
1564                 req->rq_status = rc;            /* superfluous? */
1565                 break;
1566
1567         case MDS_GETSTATUS:
1568                 DEBUG_REQ(D_INODE, req, "getstatus");
1569                 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETSTATUS_NET))
1570                         RETURN(0);
1571                 rc = mds_getstatus(req);
1572                 break;
1573
1574         case MDS_GETATTR:
1575                 DEBUG_REQ(D_INODE, req, "getattr");
1576                 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_NET))
1577                         RETURN(0);
1578                 rc = mds_getattr(req, REQ_REC_OFF);
1579                 break;
1580
1581         case MDS_SETXATTR:
1582                 DEBUG_REQ(D_INODE, req, "setxattr");
1583                 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SETXATTR_NET))
1584                         RETURN(0);
1585                 rc = mds_setxattr(req);
1586                 break;
1587
1588         case MDS_GETXATTR:
1589                 DEBUG_REQ(D_INODE, req, "getxattr");
1590                 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETXATTR_NET))
1591                         RETURN(0);
1592                 rc = mds_getxattr(req);
1593                 break;
1594
1595         case MDS_GETATTR_NAME: {
1596                 struct lustre_handle lockh = { 0 };
1597                 DEBUG_REQ(D_INODE, req, "getattr_name");
1598                 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_NAME_NET))
1599                         RETURN(0);
1600
1601                 /* If this request gets a reconstructed reply, we won't be
1602                  * acquiring any new locks in mds_getattr_lock, so we don't
1603                  * want to cancel.
1604                  */
1605                 rc = mds_getattr_lock(req, REQ_REC_OFF, MDS_INODELOCK_UPDATE,
1606                                       &lockh);
1607                 /* this non-intent call (from an ioctl) is special */
1608                 req->rq_status = rc;
1609                 if (rc == 0 && lustre_handle_is_used(&lockh))
1610                         ldlm_lock_decref(&lockh, LCK_CR);
1611                 break;
1612         }
1613         case MDS_STATFS:
1614                 DEBUG_REQ(D_INODE, req, "statfs");
1615                 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_NET))
1616                         RETURN(0);
1617                 rc = mds_statfs(req);
1618                 break;
1619
1620         case MDS_READPAGE:
1621                 DEBUG_REQ(D_INODE, req, "readpage");
1622                 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_NET))
1623                         RETURN(0);
1624                 rc = mds_readpage(req, REQ_REC_OFF);
1625
1626                 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE)) {
1627                         RETURN(0);
1628                 }
1629
1630                 break;
1631
1632         case MDS_REINT: {
1633                 __u32 *opcp = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF,
1634                                              sizeof(*opcp));
1635                 __u32  opc;
1636                 int op = 0;
1637                 int size[4] = { sizeof(struct ptlrpc_body),
1638                                 sizeof(struct mds_body),
1639                                 mds->mds_max_mdsize,
1640                                 mds->mds_max_cookiesize };
1641                 int bufcount;
1642
1643                 /* NB only peek inside req now; mds_reint() will swab it */
1644                 if (opcp == NULL) {
1645                         CERROR ("Can't inspect opcode\n");
1646                         rc = -EINVAL;
1647                         break;
1648                 }
1649                 opc = *opcp;
1650                 if (lustre_msg_swabbed(req->rq_reqmsg))
1651                         __swab32s(&opc);
1652
1653                 DEBUG_REQ(D_INODE, req, "reint %d (%s)", opc,
1654                           (opc < sizeof(reint_names) / sizeof(reint_names[0]) ||
1655                            reint_names[opc] == NULL) ? reint_names[opc] :
1656                                                        "unknown opcode");
1657                 switch (opc) {
1658                 case REINT_CREATE:
1659                         op = PTLRPC_LAST_CNTR + MDS_REINT_CREATE;
1660                         break;
1661                 case REINT_LINK:
1662                         op = PTLRPC_LAST_CNTR + MDS_REINT_LINK;
1663                         break;
1664                 case REINT_OPEN:
1665                         op = PTLRPC_LAST_CNTR + MDS_REINT_OPEN;
1666                         break;
1667                 case REINT_SETATTR:
1668                         op = PTLRPC_LAST_CNTR + MDS_REINT_SETATTR;
1669                         break;
1670                 case REINT_RENAME:
1671                         op = PTLRPC_LAST_CNTR + MDS_REINT_RENAME;
1672                         break;
1673                 case REINT_UNLINK:
1674                         op = PTLRPC_LAST_CNTR + MDS_REINT_UNLINK;
1675                         break;
1676                 default:
1677                         op = 0;
1678                         break;
1679                 }
1680
1681                 if (op && req->rq_rqbd->rqbd_service->srv_stats)
1682                         lprocfs_counter_incr(
1683                                 req->rq_rqbd->rqbd_service->srv_stats, op);
1684
1685                 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_NET))
1686                         RETURN(0);
1687
1688                 if (opc == REINT_UNLINK || opc == REINT_RENAME)
1689                         bufcount = 4;
1690                 else if (opc == REINT_OPEN)
1691                         bufcount = 3;
1692                 else
1693                         bufcount = 2;
1694
1695                 rc = lustre_pack_reply(req, bufcount, size, NULL);
1696                 if (rc)
1697                         break;
1698
1699                 rc = mds_reint(req, REQ_REC_OFF, NULL);
1700                 fail = OBD_FAIL_MDS_REINT_NET_REP;
1701                 break;
1702         }
1703
1704         case MDS_CLOSE:
1705                 DEBUG_REQ(D_INODE, req, "close");
1706                 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_CLOSE_NET))
1707                         RETURN(0);
1708                 rc = mds_close(req, REQ_REC_OFF);
1709                 break;
1710
1711         case MDS_DONE_WRITING:
1712                 DEBUG_REQ(D_INODE, req, "done_writing");
1713                 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_DONE_WRITING_NET))
1714                         RETURN(0);
1715                 rc = mds_done_writing(req, REQ_REC_OFF);
1716                 break;
1717
1718         case MDS_PIN:
1719                 DEBUG_REQ(D_INODE, req, "pin");
1720                 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_PIN_NET))
1721                         RETURN(0);
1722                 rc = mds_pin(req, REQ_REC_OFF);
1723                 break;
1724
1725         case MDS_SYNC:
1726                 DEBUG_REQ(D_INODE, req, "sync");
1727                 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_NET))
1728                         RETURN(0);
1729                 rc = mds_sync(req, REQ_REC_OFF);
1730                 break;
1731
1732         case MDS_SET_INFO:
1733                 DEBUG_REQ(D_INODE, req, "set_info");
1734                 rc = mds_set_info_rpc(req->rq_export, req);
1735                 break;
1736
1737         case MDS_QUOTACHECK:
1738                 DEBUG_REQ(D_INODE, req, "quotacheck");
1739                 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_QUOTACHECK_NET))
1740                         RETURN(0);
1741                 rc = mds_handle_quotacheck(req);
1742                 break;
1743
1744         case MDS_QUOTACTL:
1745                 DEBUG_REQ(D_INODE, req, "quotactl");
1746                 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_QUOTACTL_NET))
1747                         RETURN(0);
1748                 rc = mds_handle_quotactl(req);
1749                 break;
1750
1751         case OBD_PING:
1752                 DEBUG_REQ(D_INODE, req, "ping");
1753                 rc = target_handle_ping(req);
1754                 break;
1755
1756         case OBD_LOG_CANCEL:
1757                 CDEBUG(D_INODE, "log cancel\n");
1758                 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOG_CANCEL_NET))
1759                         RETURN(0);
1760                 rc = -ENOTSUPP; /* la la la */
1761                 break;
1762
1763         case LDLM_ENQUEUE:
1764                 DEBUG_REQ(D_INODE, req, "enqueue");
1765                 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_ENQUEUE))
1766                         RETURN(0);
1767                 rc = ldlm_handle_enqueue(req, ldlm_server_completion_ast,
1768                                          ldlm_server_blocking_ast, NULL);
1769                 fail = OBD_FAIL_LDLM_REPLY;
1770                 break;
1771         case LDLM_CONVERT:
1772                 DEBUG_REQ(D_INODE, req, "convert");
1773                 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CONVERT))
1774                         RETURN(0);
1775                 rc = ldlm_handle_convert(req);
1776                 break;
1777         case LDLM_BL_CALLBACK:
1778         case LDLM_CP_CALLBACK:
1779                 DEBUG_REQ(D_INODE, req, "callback");
1780                 CERROR("callbacks should not happen on MDS\n");
1781                 LBUG();
1782                 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_BL_CALLBACK))
1783                         RETURN(0);
1784                 break;
1785         case LLOG_ORIGIN_HANDLE_CREATE:
1786                 DEBUG_REQ(D_INODE, req, "llog_init");
1787                 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
1788                         RETURN(0);
1789                 rc = llog_origin_handle_create(req);
1790                 break;
1791         case LLOG_ORIGIN_HANDLE_DESTROY:
1792                 DEBUG_REQ(D_INODE, req, "llog_init");
1793                 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
1794                         RETURN(0);
1795                 rc = llog_origin_handle_destroy(req);
1796                 break;
1797         case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
1798                 DEBUG_REQ(D_INODE, req, "llog next block");
1799                 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
1800                         RETURN(0);
1801                 rc = llog_origin_handle_next_block(req);
1802                 break;
1803         case LLOG_ORIGIN_HANDLE_PREV_BLOCK:
1804                 DEBUG_REQ(D_INODE, req, "llog prev block");
1805                 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
1806                         RETURN(0);
1807                 rc = llog_origin_handle_prev_block(req);
1808                 break;
1809         case LLOG_ORIGIN_HANDLE_READ_HEADER:
1810                 DEBUG_REQ(D_INODE, req, "llog read header");
1811                 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
1812                         RETURN(0);
1813                 rc = llog_origin_handle_read_header(req);
1814                 break;
1815         case LLOG_ORIGIN_HANDLE_CLOSE:
1816                 DEBUG_REQ(D_INODE, req, "llog close");
1817                 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
1818                         RETURN(0);
1819                 rc = llog_origin_handle_close(req);
1820                 break;
1821         case LLOG_CATINFO:
1822                 DEBUG_REQ(D_INODE, req, "llog catinfo");
1823                 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
1824                         RETURN(0);
1825                 rc = llog_catinfo(req);
1826                 break;
1827         default:
1828                 req->rq_status = -ENOTSUPP;
1829                 rc = ptlrpc_error(req);
1830                 RETURN(rc);
1831         }
1832
1833         LASSERT(current->journal_info == NULL);
1834
1835         /* If we're DISCONNECTing, the mds_export_data is already freed */
1836         if (!rc && lustre_msg_get_opc(req->rq_reqmsg) != MDS_DISCONNECT) {
1837                 struct mds_export_data *med = &req->rq_export->exp_mds_data;
1838
1839                 /* I don't think last_xid is used for anyway, so I'm not sure
1840                    if we need to care about last_close_xid here.*/
1841                 lustre_msg_set_last_xid(req->rq_repmsg,
1842                                        le64_to_cpu(med->med_mcd->mcd_last_xid));
1843
1844                 target_committed_to_req(req);
1845         }
1846
1847         EXIT;
1848  out:
1849
1850         target_send_reply(req, rc, fail);
1851         return 0;
1852 }
1853
1854 /* Update the server data on disk.  This stores the new mount_count and
1855  * also the last_rcvd value to disk.  If we don't have a clean shutdown,
1856  * then the server last_rcvd value may be less than that of the clients.
1857  * This will alert us that we may need to do client recovery.
1858  *
1859  * Also assumes for mds_last_transno that we are not modifying it (no locking).
1860  */
1861 int mds_update_server_data(struct obd_device *obd, int force_sync)
1862 {
1863         struct mds_obd *mds = &obd->u.mds;
1864         struct lr_server_data *lsd = mds->mds_server_data;
1865         struct file *filp = mds->mds_rcvd_filp;
1866         struct lvfs_run_ctxt saved;
1867         loff_t off = 0;
1868         int rc;
1869         ENTRY;
1870
1871         CDEBUG(D_SUPER, "MDS mount_count is "LPU64", last_transno is "LPU64"\n",
1872                mds->mds_mount_count, mds->mds_last_transno);
1873
1874         spin_lock(&mds->mds_transno_lock);
1875         lsd->lsd_last_transno = cpu_to_le64(mds->mds_last_transno);
1876         spin_unlock(&mds->mds_transno_lock);
1877
1878         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1879         rc = fsfilt_write_record(obd, filp, lsd, sizeof(*lsd), &off,force_sync);
1880         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1881         if (rc)
1882                 CERROR("error writing MDS server data: rc = %d\n", rc);
1883         RETURN(rc);
1884 }
1885
1886 static void fsoptions_to_mds_flags(struct mds_obd *mds, char *options)
1887 {
1888         char *p = options;
1889
1890         if (!options)
1891                 return;
1892
1893         while (*options) {
1894                 int len;
1895
1896                 while (*p && *p != ',')
1897                         p++;
1898
1899                 len = p - options;
1900                 if (len == sizeof("user_xattr") - 1 &&
1901                     memcmp(options, "user_xattr", len) == 0) {
1902                         mds->mds_fl_user_xattr = 1;
1903                         LCONSOLE_INFO("Enabling user_xattr\n");
1904                 } else if (len == sizeof("nouser_xattr") - 1 &&
1905                            memcmp(options, "nouser_xattr", len) == 0) {
1906                         mds->mds_fl_user_xattr = 0;
1907                         LCONSOLE_INFO("Disabling user_xattr\n");
1908                 } else if (len == sizeof("acl") - 1 &&
1909                            memcmp(options, "acl", len) == 0) {
1910 #ifdef CONFIG_FS_POSIX_ACL
1911                         mds->mds_fl_acl = 1;
1912                         LCONSOLE_INFO("Enabling ACL\n");
1913 #else
1914                         CWARN("ignoring unsupported acl mount option\n");
1915 #endif
1916                 } else if (len == sizeof("noacl") - 1 &&
1917                            memcmp(options, "noacl", len) == 0) {
1918 #ifdef CONFIG_FS_POSIX_ACL
1919                         mds->mds_fl_acl = 0;
1920                         LCONSOLE_INFO("Disabling ACL\n");
1921 #endif
1922                 }
1923
1924                 options = ++p;
1925         }
1926 }
1927 static int mds_lov_presetup (struct mds_obd *mds, struct lustre_cfg *lcfg)
1928 {
1929         int rc;
1930         ENTRY;
1931
1932         rc = llog_start_commit_thread();
1933         if (rc < 0)
1934                 RETURN(rc);
1935
1936         if (lcfg->lcfg_bufcount >= 4 && LUSTRE_CFG_BUFLEN(lcfg, 3) > 0) {
1937                 class_uuid_t uuid;
1938
1939                 ll_generate_random_uuid(uuid);
1940                 class_uuid_unparse(uuid, &mds->mds_lov_uuid);
1941
1942                 OBD_ALLOC(mds->mds_profile, LUSTRE_CFG_BUFLEN(lcfg, 3));
1943                 if (mds->mds_profile == NULL)
1944                         RETURN(-ENOMEM);
1945
1946                 strncpy(mds->mds_profile, lustre_cfg_string(lcfg, 3),
1947                         LUSTRE_CFG_BUFLEN(lcfg, 3));
1948         }
1949         RETURN(rc);
1950 }
1951
1952 /* mount the file system (secretly).  lustre_cfg parameters are:
1953  * 1 = device
1954  * 2 = fstype
1955  * 3 = config name
1956  * 4 = mount options
1957  */
1958 static int mds_setup(struct obd_device *obd, struct lustre_cfg* lcfg)
1959 {
1960         struct lprocfs_static_vars lvars;
1961         struct mds_obd *mds = &obd->u.mds;
1962         struct lustre_mount_info *lmi;
1963         struct vfsmount *mnt;
1964         struct lustre_sb_info *lsi;
1965         struct obd_uuid uuid;
1966         __u8 *uuid_ptr;
1967         char *str, *label;
1968         char ns_name[48];
1969         int rc = 0;
1970         ENTRY;
1971
1972         /* setup 1:/dev/loop/0 2:ext3 3:mdsA 4:errors=remount-ro,iopen_nopriv */
1973
1974         CLASSERT(offsetof(struct obd_device, u.obt) ==
1975                  offsetof(struct obd_device, u.mds.mds_obt));
1976
1977         if (lcfg->lcfg_bufcount < 3)
1978                 RETURN(-EINVAL);
1979
1980         if (LUSTRE_CFG_BUFLEN(lcfg, 1) == 0 || LUSTRE_CFG_BUFLEN(lcfg, 2) == 0)
1981                 RETURN(-EINVAL);
1982
1983         lmi = server_get_mount(obd->obd_name);
1984         if (!lmi) {
1985                 CERROR("Not mounted in lustre_fill_super?\n");
1986                 RETURN(-EINVAL);
1987         }
1988
1989         /* We mounted in lustre_fill_super.
1990            lcfg bufs 1, 2, 4 (device, fstype, mount opts) are ignored.*/
1991                 
1992         lsi = s2lsi(lmi->lmi_sb);
1993         fsoptions_to_mds_flags(mds, lsi->lsi_ldd->ldd_mount_opts);
1994         fsoptions_to_mds_flags(mds, lsi->lsi_lmd->lmd_opts);
1995         mnt = lmi->lmi_mnt;
1996         obd->obd_fsops = fsfilt_get_ops(MT_STR(lsi->lsi_ldd));
1997         if (IS_ERR(obd->obd_fsops))
1998                 GOTO(err_put, rc = PTR_ERR(obd->obd_fsops));
1999
2000         CDEBUG(D_SUPER, "%s: mnt = %p\n", lustre_cfg_string(lcfg, 1), mnt);
2001
2002         LASSERT(!lvfs_check_rdonly(lvfs_sbdev(mnt->mnt_sb)));
2003
2004         sema_init(&mds->mds_epoch_sem, 1);
2005         spin_lock_init(&mds->mds_transno_lock);
2006         mds->mds_max_mdsize = sizeof(struct lov_mds_md);
2007         mds->mds_max_cookiesize = sizeof(struct llog_cookie);
2008         mds->mds_atime_diff = MAX_ATIME_DIFF;
2009         mds->mds_evict_ost_nids = 1;
2010
2011         sprintf(ns_name, "mds-%s", obd->obd_uuid.uuid);
2012         obd->obd_namespace = ldlm_namespace_new(ns_name, LDLM_NAMESPACE_SERVER,
2013                                                 LDLM_NAMESPACE_GREEDY);
2014         if (obd->obd_namespace == NULL) {
2015                 mds_cleanup(obd);
2016                 GOTO(err_ops, rc = -ENOMEM);
2017         }
2018         ldlm_register_intent(obd->obd_namespace, mds_intent_policy);
2019
2020         lprocfs_mds_init_vars(&lvars);
2021         if (lprocfs_obd_setup(obd, lvars.obd_vars) == 0 &&
2022             lprocfs_alloc_obd_stats(obd, LPROC_MDS_LAST) == 0) {
2023                 /* Init private stats here */
2024                 mds_stats_counter_init(obd->obd_stats);
2025                 obd->obd_proc_exports = proc_mkdir("exports",
2026                                                    obd->obd_proc_entry);
2027         }
2028
2029         rc = mds_fs_setup(obd, mnt);
2030         if (rc) {
2031                 CERROR("%s: MDS filesystem method init failed: rc = %d\n",
2032                        obd->obd_name, rc);
2033                 GOTO(err_ns, rc);
2034         }
2035
2036         rc = mds_lov_presetup(mds, lcfg);
2037         if (rc < 0)
2038                 GOTO(err_fs, rc);
2039
2040         ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
2041                            "mds_ldlm_client", &obd->obd_ldlm_client);
2042         obd->obd_replayable = 1;
2043
2044         rc = lquota_setup(mds_quota_interface_ref, obd);
2045         if (rc)
2046                 GOTO(err_fs, rc);
2047
2048 #if 0
2049         mds->mds_group_hash = upcall_cache_init(obd->obd_name);
2050         if (IS_ERR(mds->mds_group_hash)) {
2051                 rc = PTR_ERR(mds->mds_group_hash);
2052                 mds->mds_group_hash = NULL;
2053                 GOTO(err_qctxt, rc);
2054         }
2055 #endif
2056
2057         /* Don't wait for mds_postrecov trying to clear orphans */
2058         obd->obd_async_recov = 1;
2059         rc = mds_postsetup(obd);
2060         /* Bug 11557 - allow async abort_recov start
2061            FIXME can remove most of this obd_async_recov plumbing
2062         obd->obd_async_recov = 0;
2063         */
2064         if (rc)
2065                 GOTO(err_qctxt, rc);
2066
2067         uuid_ptr = fsfilt_uuid(obd, obd->u.obt.obt_sb);
2068         if (uuid_ptr != NULL) {
2069                 class_uuid_unparse(uuid_ptr, &uuid);
2070                 str = uuid.uuid;
2071         } else {
2072                 str = "no UUID";
2073         }
2074
2075         label = fsfilt_get_label(obd, obd->u.obt.obt_sb);
2076         if (obd->obd_recovering) {
2077                 LCONSOLE_WARN("MDT %s now serving %s (%s%s%s), but will be in "
2078                               "recovery until %d %s reconnect, or if no clients"
2079                               " reconnect for %d:%.02d; during that time new "
2080                               "clients will not be allowed to connect. "
2081                               "Recovery progress can be monitored by watching "
2082                               "/proc/fs/lustre/mds/%s/recovery_status.\n",
2083                               obd->obd_name, lustre_cfg_string(lcfg, 1),
2084                               label ?: "", label ? "/" : "", str,
2085                               obd->obd_max_recoverable_clients,
2086                               (obd->obd_max_recoverable_clients == 1) ?
2087                               "client" : "clients",
2088                               (int)(OBD_RECOVERY_TIMEOUT) / 60,
2089                               (int)(OBD_RECOVERY_TIMEOUT) % 60,
2090                               obd->obd_name);
2091         } else {
2092                 LCONSOLE_INFO("MDT %s now serving %s (%s%s%s) with recovery "
2093                               "%s\n", obd->obd_name, lustre_cfg_string(lcfg, 1),
2094                               label ?: "", label ? "/" : "", str,
2095                               obd->obd_replayable ? "enabled" : "disabled");
2096         }
2097
2098         if (ldlm_timeout == LDLM_TIMEOUT_DEFAULT)
2099                 ldlm_timeout = 6;
2100
2101         RETURN(0);
2102
2103 err_qctxt:
2104         lquota_cleanup(mds_quota_interface_ref, obd);
2105 err_fs:
2106         /* No extra cleanup needed for llog_init_commit_thread() */
2107         mds_fs_cleanup(obd);
2108 #if 0
2109         upcall_cache_cleanup(mds->mds_group_hash);
2110         mds->mds_group_hash = NULL;
2111 #endif
2112 err_ns:
2113         lprocfs_obd_cleanup(obd);
2114         lprocfs_free_obd_stats(obd);
2115         ldlm_namespace_free(obd->obd_namespace, 0);
2116         obd->obd_namespace = NULL;
2117 err_ops:
2118         fsfilt_put_ops(obd->obd_fsops);
2119 err_put:
2120         server_put_mount(obd->obd_name, mnt);
2121         obd->u.obt.obt_sb = NULL;
2122         return rc;
2123 }
2124
2125 static int mds_lov_clean(struct obd_device *obd)
2126 {
2127         struct mds_obd *mds = &obd->u.mds;
2128         struct obd_device *osc = mds->mds_osc_obd;
2129         ENTRY;
2130
2131         if (mds->mds_profile) {
2132                 class_del_profile(mds->mds_profile);
2133                 OBD_FREE(mds->mds_profile, strlen(mds->mds_profile) + 1);
2134                 mds->mds_profile = NULL;
2135         }
2136
2137         /* There better be a lov */
2138         if (!osc)
2139                 RETURN(0);
2140         if (IS_ERR(osc))
2141                 RETURN(PTR_ERR(osc));
2142
2143         obd_register_observer(osc, NULL);
2144
2145         /* Give lov our same shutdown flags */
2146         osc->obd_force = obd->obd_force;
2147         osc->obd_fail = obd->obd_fail;
2148
2149         /* Cleanup the lov */
2150         obd_disconnect(mds->mds_osc_exp);
2151         class_manual_cleanup(osc);
2152         mds->mds_osc_exp = NULL;
2153
2154         RETURN(0);
2155 }
2156
2157 static int mds_postsetup(struct obd_device *obd)
2158 {
2159         struct mds_obd *mds = &obd->u.mds;
2160         int rc = 0;
2161         ENTRY;
2162
2163         rc = llog_setup(obd, NULL, LLOG_CONFIG_ORIG_CTXT, obd, 0, NULL,
2164                         &llog_lvfs_ops);
2165         if (rc)
2166                 RETURN(rc);
2167
2168         rc = llog_setup(obd, NULL, LLOG_LOVEA_ORIG_CTXT, obd, 0, NULL,
2169                         &llog_lvfs_ops);
2170         if (rc)
2171                 RETURN(rc);
2172
2173         if (mds->mds_profile) {
2174                 struct lustre_profile *lprof;
2175                 /* The profile defines which osc and mdc to connect to, for a
2176                    client.  We reuse that here to figure out the name of the
2177                    lov to use (and ignore lprof->lp_md).
2178                    The profile was set in the config log with
2179                    LCFG_MOUNTOPT profilenm oscnm mdcnm */
2180                 lprof = class_get_profile(mds->mds_profile);
2181                 if (lprof == NULL) {
2182                         CERROR("No profile found: %s\n", mds->mds_profile);
2183                         GOTO(err_cleanup, rc = -ENOENT);
2184                 }
2185                 rc = mds_lov_connect(obd, lprof->lp_dt);
2186                 if (rc)
2187                         GOTO(err_cleanup, rc);
2188         }
2189
2190         RETURN(rc);
2191
2192 err_cleanup:
2193         mds_lov_clean(obd);
2194         llog_cleanup(llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT));
2195         llog_cleanup(llog_get_context(obd, LLOG_LOVEA_ORIG_CTXT));
2196         RETURN(rc);
2197 }
2198
2199 int mds_postrecov(struct obd_device *obd)
2200 {
2201         int rc = 0;
2202         ENTRY;
2203
2204         if (obd->obd_fail)
2205                 RETURN(0);
2206
2207         LASSERT(!obd->obd_recovering);
2208         LASSERT(llog_get_context(obd, LLOG_MDS_OST_ORIG_CTXT) != NULL);
2209         /* clean PENDING dir */
2210         if (strncmp(obd->obd_name, MDD_OBD_NAME, strlen(MDD_OBD_NAME)))
2211                 rc = mds_cleanup_pending(obd);
2212                 if (rc < 0)
2213                         GOTO(out, rc);
2214
2215         /* FIXME Does target_finish_recovery really need this to block? */
2216         /* Notify the LOV, which will in turn call mds_notify for each tgt */
2217         /* This means that we have to hack obd_notify to think we're obd_set_up
2218            during mds_lov_connect. */
2219         obd_notify(obd->u.mds.mds_osc_obd, NULL,
2220                    obd->obd_async_recov ? OBD_NOTIFY_SYNC_NONBLOCK :
2221                    OBD_NOTIFY_SYNC, NULL);
2222
2223         /* quota recovery */
2224         lquota_recovery(mds_quota_interface_ref, obd);
2225
2226 out:
2227         RETURN(rc);
2228 }
2229
2230 /* We need to be able to stop an mds_lov_synchronize */
2231 static int mds_lov_early_clean(struct obd_device *obd)
2232 {
2233         struct mds_obd *mds = &obd->u.mds;
2234         struct obd_device *osc = mds->mds_osc_obd;
2235
2236         if (!osc || (!obd->obd_force && !obd->obd_fail))
2237                 return(0);
2238
2239         CDEBUG(D_HA, "abort inflight\n");
2240         return (obd_precleanup(osc, OBD_CLEANUP_EARLY));
2241 }
2242
2243 static int mds_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
2244 {
2245         int rc = 0;
2246         ENTRY;
2247
2248         switch (stage) {
2249         case OBD_CLEANUP_EARLY:
2250                 break;
2251         case OBD_CLEANUP_EXPORTS:
2252                 /*XXX Use this for mdd mds cleanup, so comment out
2253                  *this target_cleanup_recovery for this tmp MDD MDS
2254                  *Wangdi*/
2255                 if (strncmp(obd->obd_name, MDD_OBD_NAME, strlen(MDD_OBD_NAME)))
2256                         target_cleanup_recovery(obd);
2257                 mds_lov_early_clean(obd);
2258                 break;
2259         case OBD_CLEANUP_SELF_EXP:
2260                 mds_lov_disconnect(obd);
2261                 mds_lov_clean(obd);
2262                 llog_cleanup(llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT));
2263                 llog_cleanup(llog_get_context(obd, LLOG_LOVEA_ORIG_CTXT));
2264                 rc = obd_llog_finish(obd, 0);
2265                 break;
2266         case OBD_CLEANUP_OBD:
2267                 break;
2268         }
2269         RETURN(rc);
2270 }
2271
2272 static int mds_cleanup(struct obd_device *obd)
2273 {
2274         struct mds_obd *mds = &obd->u.mds;
2275         lvfs_sbdev_type save_dev;
2276         ENTRY;
2277
2278         if (obd->u.obt.obt_sb == NULL)
2279                 RETURN(0);
2280         save_dev = lvfs_sbdev(obd->u.obt.obt_sb);
2281
2282         if (mds->mds_osc_exp)
2283                 /* lov export was disconnected by mds_lov_clean;
2284                    we just need to drop our ref */
2285                 class_export_put(mds->mds_osc_exp);
2286
2287         lprocfs_obd_cleanup(obd);
2288         lprocfs_free_obd_stats(obd);
2289
2290         lquota_cleanup(mds_quota_interface_ref, obd);
2291
2292         mds_update_server_data(obd, 1);
2293         /* XXX
2294         mds_lov_destroy_objids(obd);
2295         */
2296         mds_fs_cleanup(obd);
2297
2298 #if 0
2299         upcall_cache_cleanup(mds->mds_group_hash);
2300         mds->mds_group_hash = NULL;
2301 #endif
2302
2303         server_put_mount(obd->obd_name, mds->mds_vfsmnt);
2304         obd->u.obt.obt_sb = NULL;
2305
2306         ldlm_namespace_free(obd->obd_namespace, obd->obd_force);
2307
2308         spin_lock_bh(&obd->obd_processing_task_lock);
2309         if (obd->obd_recovering) {
2310                 target_cancel_recovery_timer(obd);
2311                 obd->obd_recovering = 0;
2312         }
2313         spin_unlock_bh(&obd->obd_processing_task_lock);
2314
2315         fsfilt_put_ops(obd->obd_fsops);
2316
2317         LCONSOLE_INFO("MDT %s has stopped.\n", obd->obd_name);
2318
2319         RETURN(0);
2320 }
2321
2322 static void fixup_handle_for_resent_req(struct ptlrpc_request *req, int offset,
2323                                         struct ldlm_lock *new_lock,
2324                                         struct ldlm_lock **old_lock,
2325                                         struct lustre_handle *lockh)
2326 {
2327         struct obd_export *exp = req->rq_export;
2328         struct ldlm_request *dlmreq =
2329                 lustre_msg_buf(req->rq_reqmsg, offset, sizeof(*dlmreq));
2330         struct lustre_handle remote_hdl = dlmreq->lock_handle[0];
2331         struct list_head *iter;
2332
2333         if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
2334                 return;
2335
2336         spin_lock(&exp->exp_ldlm_data.led_lock);
2337         list_for_each(iter, &exp->exp_ldlm_data.led_held_locks) {
2338                 struct ldlm_lock *lock;
2339                 lock = list_entry(iter, struct ldlm_lock, l_export_chain);
2340                 if (lock == new_lock)
2341                         continue;
2342                 if (lock->l_remote_handle.cookie == remote_hdl.cookie) {
2343                         lockh->cookie = lock->l_handle.h_cookie;
2344                         LDLM_DEBUG(lock, "restoring lock cookie");
2345                         DEBUG_REQ(D_DLMTRACE, req,"restoring lock cookie "LPX64,
2346                                   lockh->cookie);
2347                         if (old_lock)
2348                                 *old_lock = LDLM_LOCK_GET(lock);
2349                         spin_unlock(&exp->exp_ldlm_data.led_lock);
2350                         return;
2351                 }
2352         }
2353         spin_unlock(&exp->exp_ldlm_data.led_lock);
2354
2355         /* If the xid matches, then we know this is a resent request,
2356          * and allow it. (It's probably an OPEN, for which we don't
2357          * send a lock */
2358         if (req->rq_xid ==
2359             le64_to_cpu(exp->exp_mds_data.med_mcd->mcd_last_xid))
2360                 return;
2361
2362         if (req->rq_xid ==
2363             le64_to_cpu(exp->exp_mds_data.med_mcd->mcd_last_close_xid))
2364                 return;
2365
2366         /* This remote handle isn't enqueued, so we never received or
2367          * processed this request.  Clear MSG_RESENT, because it can
2368          * be handled like any normal request now. */
2369
2370         lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
2371
2372         DEBUG_REQ(D_DLMTRACE, req, "no existing lock with rhandle "LPX64,
2373                   remote_hdl.cookie);
2374 }
2375
2376 int intent_disposition(struct ldlm_reply *rep, int flag)
2377 {
2378         if (!rep)
2379                 return 0;
2380         return (rep->lock_policy_res1 & flag);
2381 }
2382
2383 void intent_set_disposition(struct ldlm_reply *rep, int flag)
2384 {
2385         if (!rep)
2386                 return;
2387         rep->lock_policy_res1 |= flag;
2388 }
2389
2390 static int mds_intent_policy(struct ldlm_namespace *ns,
2391                              struct ldlm_lock **lockp, void *req_cookie,
2392                              ldlm_mode_t mode, int flags, void *data)
2393 {
2394         struct ptlrpc_request *req = req_cookie;
2395         struct ldlm_lock *lock = *lockp;
2396         struct ldlm_intent *it;
2397         struct mds_obd *mds = &req->rq_export->exp_obd->u.mds;
2398         struct ldlm_reply *rep;
2399         struct lustre_handle lockh = { 0 };
2400         struct ldlm_lock *new_lock = NULL;
2401         int getattr_part = MDS_INODELOCK_UPDATE;
2402         int repsize[5] = { [MSG_PTLRPC_BODY_OFF] = sizeof(struct ptlrpc_body),
2403                            [DLM_LOCKREPLY_OFF]   = sizeof(struct ldlm_reply),
2404                            [DLM_REPLY_REC_OFF]   = sizeof(struct mds_body),
2405                            [DLM_REPLY_REC_OFF+1] = mds->mds_max_mdsize };
2406         int repbufcnt = 4, rc;
2407         ENTRY;
2408
2409         LASSERT(req != NULL);
2410
2411         if (lustre_msg_bufcount(req->rq_reqmsg) <= DLM_INTENT_IT_OFF) {
2412                 /* No intent was provided */
2413                 rc = lustre_pack_reply(req, 2, repsize, NULL);
2414                 if (rc)
2415                         RETURN(rc);
2416                 RETURN(0);
2417         }
2418
2419         it = lustre_swab_reqbuf(req, DLM_INTENT_IT_OFF, sizeof(*it),
2420                                 lustre_swab_ldlm_intent);
2421         if (it == NULL) {
2422                 CERROR("Intent missing\n");
2423                 RETURN(req->rq_status = -EFAULT);
2424         }
2425
2426         LDLM_DEBUG(lock, "intent policy, opc: %s", ldlm_it2str(it->opc));
2427
2428         if ((req->rq_export->exp_connect_flags & OBD_CONNECT_ACL) &&
2429             (it->opc & (IT_OPEN | IT_GETATTR | IT_LOOKUP)))
2430                 /* we should never allow OBD_CONNECT_ACL if not configured */
2431                 repsize[repbufcnt++] = LUSTRE_POSIX_ACL_MAX_SIZE;
2432         else if (it->opc & IT_UNLINK)
2433                 repsize[repbufcnt++] = mds->mds_max_cookiesize;
2434
2435         rc = lustre_pack_reply(req, repbufcnt, repsize, NULL);
2436         if (rc)
2437                 RETURN(req->rq_status = rc);
2438
2439         rep = lustre_msg_buf(req->rq_repmsg, DLM_LOCKREPLY_OFF, sizeof(*rep));
2440         intent_set_disposition(rep, DISP_IT_EXECD);
2441
2442
2443         /* execute policy */
2444         switch ((long)it->opc) {
2445         case IT_OPEN:
2446         case IT_CREAT|IT_OPEN:
2447                 mds_counter_incr(req->rq_export, LPROC_MDS_OPEN);
2448                 fixup_handle_for_resent_req(req, DLM_LOCKREQ_OFF, lock, NULL,
2449                                             &lockh);
2450                 /* XXX swab here to assert that an mds_open reint
2451                  * packet is following */
2452                 rep->lock_policy_res2 = mds_reint(req, DLM_INTENT_REC_OFF,
2453                                                   &lockh);
2454 #if 0
2455                 /* We abort the lock if the lookup was negative and
2456                  * we did not make it to the OPEN portion */
2457                 if (!intent_disposition(rep, DISP_LOOKUP_EXECD))
2458                         RETURN(ELDLM_LOCK_ABORTED);
2459                 if (intent_disposition(rep, DISP_LOOKUP_NEG) &&
2460                     !intent_disposition(rep, DISP_OPEN_OPEN))
2461 #endif
2462
2463                 /* If there was an error of some sort or if we are not
2464                  * returning any locks */
2465                 if (rep->lock_policy_res2 ||
2466                     !intent_disposition(rep, DISP_OPEN_LOCK))
2467                         RETURN(ELDLM_LOCK_ABORTED);
2468                 break;
2469         case IT_LOOKUP:
2470                         getattr_part = MDS_INODELOCK_LOOKUP;
2471         case IT_GETATTR:
2472                         getattr_part |= MDS_INODELOCK_LOOKUP;
2473                         OBD_COUNTER_INCREMENT(req->rq_export->exp_obd, getattr);
2474         case IT_READDIR:
2475                 fixup_handle_for_resent_req(req, DLM_LOCKREQ_OFF, lock,
2476                                             &new_lock, &lockh);
2477
2478                 /* INODEBITS_INTEROP: if this lock was converted from a
2479                  * plain lock (client does not support inodebits), then
2480                  * child lock must be taken with both lookup and update
2481                  * bits set for all operations.
2482                  */
2483                 if (!(req->rq_export->exp_connect_flags & OBD_CONNECT_IBITS))
2484                         getattr_part = MDS_INODELOCK_LOOKUP |
2485                                        MDS_INODELOCK_UPDATE;
2486
2487                 rep->lock_policy_res2 = mds_getattr_lock(req,DLM_INTENT_REC_OFF,
2488                                                          getattr_part, &lockh);
2489                 /* FIXME: LDLM can set req->rq_status. MDS sets
2490                    policy_res{1,2} with disposition and status.
2491                    - replay: returns 0 & req->status is old status
2492                    - otherwise: returns req->status */
2493                 if (intent_disposition(rep, DISP_LOOKUP_NEG))
2494                         rep->lock_policy_res2 = 0;
2495                 if (!intent_disposition(rep, DISP_LOOKUP_POS) ||
2496                     rep->lock_policy_res2)
2497                         RETURN(ELDLM_LOCK_ABORTED);
2498                 if (req->rq_status != 0) {
2499                         LBUG();
2500                         rep->lock_policy_res2 = req->rq_status;
2501                         RETURN(ELDLM_LOCK_ABORTED);
2502                 }
2503                 break;
2504         default:
2505                 CERROR("Unhandled intent "LPD64"\n", it->opc);
2506                 RETURN(-EFAULT);
2507         }
2508
2509         /* By this point, whatever function we called above must have either
2510          * filled in 'lockh', been an intent replay, or returned an error.  We
2511          * want to allow replayed RPCs to not get a lock, since we would just
2512          * drop it below anyways because lock replay is done separately by the
2513          * client afterwards.  For regular RPCs we want to give the new lock to
2514          * the client instead of whatever lock it was about to get. */
2515         if (new_lock == NULL)
2516                 new_lock = ldlm_handle2lock(&lockh);
2517         if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY))
2518                 RETURN(0);
2519
2520         LASSERTF(new_lock != NULL, "op "LPX64" lockh "LPX64"\n",
2521                  it->opc, lockh.cookie);
2522
2523         /* If we've already given this lock to a client once, then we should
2524          * have no readers or writers.  Otherwise, we should have one reader
2525          * _or_ writer ref (which will be zeroed below) before returning the
2526          * lock to a client. */
2527         if (new_lock->l_export == req->rq_export) {
2528                 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
2529         } else {
2530                 LASSERT(new_lock->l_export == NULL);
2531                 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
2532         }
2533
2534         *lockp = new_lock;
2535
2536         if (new_lock->l_export == req->rq_export) {
2537                 /* Already gave this to the client, which means that we
2538                  * reconstructed a reply. */
2539                 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
2540                         MSG_RESENT);
2541                 RETURN(ELDLM_LOCK_REPLACED);
2542         }
2543
2544         /* Fixup the lock to be given to the client */
2545         lock_res_and_lock(new_lock);
2546         new_lock->l_readers = 0;
2547         new_lock->l_writers = 0;
2548
2549         new_lock->l_export = class_export_get(req->rq_export);
2550         spin_lock(&req->rq_export->exp_ldlm_data.led_lock);
2551         list_add(&new_lock->l_export_chain,
2552                  &new_lock->l_export->exp_ldlm_data.led_held_locks);
2553         spin_unlock(&req->rq_export->exp_ldlm_data.led_lock);
2554
2555         new_lock->l_blocking_ast = lock->l_blocking_ast;
2556         new_lock->l_completion_ast = lock->l_completion_ast;
2557
2558         memcpy(&new_lock->l_remote_handle, &lock->l_remote_handle,
2559                sizeof(lock->l_remote_handle));
2560
2561         new_lock->l_flags &= ~LDLM_FL_LOCAL;
2562
2563         unlock_res_and_lock(new_lock);
2564         LDLM_LOCK_PUT(new_lock);
2565
2566         RETURN(ELDLM_LOCK_REPLACED);
2567 }
2568
2569 static int mdt_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
2570 {
2571         struct mds_obd *mds = &obd->u.mds;
2572         struct lprocfs_static_vars lvars;
2573         int mds_min_threads;
2574         int mds_max_threads;
2575         int rc = 0;
2576         ENTRY;
2577
2578         lprocfs_mdt_init_vars(&lvars);
2579         lprocfs_obd_setup(obd, lvars.obd_vars);
2580
2581         sema_init(&mds->mds_health_sem, 1);
2582
2583         if (mds_num_threads) {
2584                 /* If mds_num_threads is set, it is the min and the max. */
2585                 if (mds_num_threads > MDS_THREADS_MAX)
2586                         mds_num_threads = MDS_THREADS_MAX;
2587                 if (mds_num_threads < MDS_THREADS_MIN)
2588                         mds_num_threads = MDS_THREADS_MIN;
2589                 mds_max_threads = mds_min_threads = mds_num_threads;
2590         } else {
2591                 /* Base min threads on memory and cpus */
2592                 mds_min_threads = num_possible_cpus() * num_physpages >> 
2593                         (27 - CFS_PAGE_SHIFT);
2594                 if (mds_min_threads < MDS_THREADS_MIN)
2595                         mds_min_threads = MDS_THREADS_MIN;
2596                 /* Largest auto threads start value */
2597                 if (mds_min_threads > 32) 
2598                         mds_min_threads = 32;
2599                 mds_max_threads = min(MDS_THREADS_MAX, mds_min_threads * 4);
2600         }
2601
2602         mds->mds_service =
2603                 ptlrpc_init_svc(MDS_NBUFS, MDS_BUFSIZE, MDS_MAXREQSIZE,
2604                                 MDS_MAXREPSIZE, MDS_REQUEST_PORTAL,
2605                                 MDC_REPLY_PORTAL, MDS_SERVICE_WATCHDOG_TIMEOUT,
2606                                 mds_handle, LUSTRE_MDS_NAME,
2607                                 obd->obd_proc_entry, NULL, 
2608                                 mds_min_threads, mds_max_threads, "ll_mdt", 0);
2609
2610         if (!mds->mds_service) {
2611                 CERROR("failed to start service\n");
2612                 GOTO(err_lprocfs, rc = -ENOMEM);
2613         }
2614
2615         rc = ptlrpc_start_threads(obd, mds->mds_service);
2616         if (rc)
2617                 GOTO(err_thread, rc);
2618
2619         mds->mds_setattr_service =
2620                 ptlrpc_init_svc(MDS_NBUFS, MDS_BUFSIZE, MDS_MAXREQSIZE,
2621                                 MDS_MAXREPSIZE, MDS_SETATTR_PORTAL,
2622                                 MDC_REPLY_PORTAL, MDS_SERVICE_WATCHDOG_TIMEOUT,
2623                                 mds_handle, "mds_setattr",
2624                                 obd->obd_proc_entry, NULL,
2625                                 mds_min_threads, mds_max_threads,
2626                                 "ll_mdt_attr", 0);
2627         if (!mds->mds_setattr_service) {
2628                 CERROR("failed to start getattr service\n");
2629                 GOTO(err_thread, rc = -ENOMEM);
2630         }
2631
2632         rc = ptlrpc_start_threads(obd, mds->mds_setattr_service);
2633         if (rc)
2634                 GOTO(err_thread2, rc);
2635
2636         mds->mds_readpage_service =
2637                 ptlrpc_init_svc(MDS_NBUFS, MDS_BUFSIZE, MDS_MAXREQSIZE,
2638                                 MDS_MAXREPSIZE, MDS_READPAGE_PORTAL,
2639                                 MDC_REPLY_PORTAL, MDS_SERVICE_WATCHDOG_TIMEOUT,
2640                                 mds_handle, "mds_readpage",
2641                                 obd->obd_proc_entry, NULL, 
2642                                 MDS_THREADS_MIN_READPAGE, mds_max_threads,
2643                                 "ll_mdt_rdpg", 0);
2644         if (!mds->mds_readpage_service) {
2645                 CERROR("failed to start readpage service\n");
2646                 GOTO(err_thread2, rc = -ENOMEM);
2647         }
2648
2649         rc = ptlrpc_start_threads(obd, mds->mds_readpage_service);
2650
2651         if (rc)
2652                 GOTO(err_thread3, rc);
2653
2654         ping_evictor_start();
2655         
2656         RETURN(0);
2657
2658 err_thread3:
2659         ptlrpc_unregister_service(mds->mds_readpage_service);
2660         mds->mds_readpage_service = NULL;
2661 err_thread2:
2662         ptlrpc_unregister_service(mds->mds_setattr_service);
2663         mds->mds_setattr_service = NULL;
2664 err_thread:
2665         ptlrpc_unregister_service(mds->mds_service);
2666         mds->mds_service = NULL;
2667 err_lprocfs:
2668         lprocfs_obd_cleanup(obd);
2669         return rc;
2670 }
2671
2672 static int mdt_cleanup(struct obd_device *obd)
2673 {
2674         struct mds_obd *mds = &obd->u.mds;
2675         ENTRY;
2676
2677         ping_evictor_stop();
2678
2679         down(&mds->mds_health_sem);
2680         ptlrpc_unregister_service(mds->mds_readpage_service);
2681         ptlrpc_unregister_service(mds->mds_setattr_service);
2682         ptlrpc_unregister_service(mds->mds_service);
2683         mds->mds_readpage_service = NULL;
2684         mds->mds_setattr_service = NULL;
2685         mds->mds_service = NULL;
2686         up(&mds->mds_health_sem);
2687
2688         lprocfs_obd_cleanup(obd);
2689
2690         RETURN(0);
2691 }
2692
2693 static int mdt_health_check(struct obd_device *obd)
2694 {
2695         struct mds_obd *mds = &obd->u.mds;
2696         int rc = 0;
2697
2698         down(&mds->mds_health_sem);
2699         rc |= ptlrpc_service_health_check(mds->mds_readpage_service);
2700         rc |= ptlrpc_service_health_check(mds->mds_setattr_service);
2701         rc |= ptlrpc_service_health_check(mds->mds_service);
2702         up(&mds->mds_health_sem);
2703
2704         /*
2705          * health_check to return 0 on healthy
2706          * and 1 on unhealthy.
2707          */
2708         if(rc != 0)
2709                 rc = 1;
2710
2711         return rc;
2712 }
2713
2714 static struct dentry *mds_lvfs_fid2dentry(__u64 id, __u32 gen, __u64 gr,
2715                                           void *data)
2716 {
2717         struct obd_device *obd = data;
2718         struct ll_fid fid;
2719         fid.id = id;
2720         fid.generation = gen;
2721         return mds_fid2dentry(&obd->u.mds, &fid, NULL);
2722 }
2723
2724 static int mds_health_check(struct obd_device *obd)
2725 {
2726         struct obd_device_target *odt = &obd->u.obt;
2727 #ifdef USE_HEALTH_CHECK_WRITE
2728         struct mds_obd *mds = &obd->u.mds;
2729 #endif
2730         int rc = 0;
2731
2732         if (odt->obt_sb->s_flags & MS_RDONLY)
2733                 rc = 1;
2734
2735 #ifdef USE_HEALTH_CHECK_WRITE
2736         LASSERT(mds->mds_health_check_filp != NULL);
2737         rc |= !!lvfs_check_io_health(obd, mds->mds_health_check_filp);
2738 #endif
2739         return rc;
2740 }
2741
2742 static int mds_process_config(struct obd_device *obd, obd_count len, void *buf)
2743 {
2744         struct lustre_cfg *lcfg = buf;
2745         struct lprocfs_static_vars lvars;
2746         int rc;
2747
2748         lprocfs_mds_init_vars(&lvars);
2749
2750         rc = class_process_proc_param(PARAM_MDT, lvars.obd_vars, lcfg, obd);
2751         return(rc);
2752 }
2753
2754 struct lvfs_callback_ops mds_lvfs_ops = {
2755         l_fid2dentry:     mds_lvfs_fid2dentry,
2756 };
2757
2758 /* use obd ops to offer management infrastructure */
2759 static struct obd_ops mds_obd_ops = {
2760         .o_owner           = THIS_MODULE,
2761         .o_connect         = mds_connect,
2762         .o_reconnect       = mds_reconnect,
2763         .o_init_export     = mds_init_export,
2764         .o_destroy_export  = mds_destroy_export,
2765         .o_disconnect      = mds_disconnect,
2766         .o_setup           = mds_setup,
2767         .o_precleanup      = mds_precleanup,
2768         .o_cleanup         = mds_cleanup,
2769         .o_postrecov       = mds_postrecov,
2770         .o_statfs          = mds_obd_statfs,
2771         .o_iocontrol       = mds_iocontrol,
2772         .o_create          = mds_obd_create,
2773         .o_destroy         = mds_obd_destroy,
2774         .o_llog_init       = mds_llog_init,
2775         .o_llog_finish     = mds_llog_finish,
2776         .o_notify          = mds_notify,
2777         .o_health_check    = mds_health_check,
2778         .o_process_config  = mds_process_config,
2779 };
2780
2781 static struct obd_ops mdt_obd_ops = {
2782         .o_owner           = THIS_MODULE,
2783         .o_setup           = mdt_setup,
2784         .o_cleanup         = mdt_cleanup,
2785         .o_health_check    = mdt_health_check,
2786 };
2787
2788 quota_interface_t *mds_quota_interface_ref;
2789 extern quota_interface_t mds_quota_interface;
2790
2791 static __attribute__((unused)) int __init mds_init(void)
2792 {
2793         int rc;
2794         struct lprocfs_static_vars lvars;
2795
2796         request_module("lquota");
2797         mds_quota_interface_ref = PORTAL_SYMBOL_GET(mds_quota_interface);
2798         rc = lquota_init(mds_quota_interface_ref);
2799         if (rc) {
2800                 if (mds_quota_interface_ref)
2801                         PORTAL_SYMBOL_PUT(mds_quota_interface);
2802                 return rc;
2803         }
2804         init_obd_quota_ops(mds_quota_interface_ref, &mds_obd_ops);
2805
2806         lprocfs_mds_init_vars(&lvars);
2807         class_register_type(&mds_obd_ops, NULL,
2808                             lvars.module_vars, LUSTRE_MDS_NAME, NULL);
2809         lprocfs_mds_init_vars(&lvars);
2810         mdt_obd_ops = mdt_obd_ops; //make compiler happy
2811 //        class_register_type(&mdt_obd_ops, NULL,
2812 //                            lvars.module_vars, LUSTRE_MDT_NAME, NULL);
2813
2814         return 0;
2815 }
2816
2817 static __attribute__((unused)) void /*__exit*/ mds_exit(void)
2818 {
2819         lquota_exit(mds_quota_interface_ref);
2820         if (mds_quota_interface_ref)
2821                 PORTAL_SYMBOL_PUT(mds_quota_interface);
2822
2823         class_unregister_type(LUSTRE_MDS_NAME);
2824 //        class_unregister_type(LUSTRE_MDT_NAME);
2825 }
2826 /*mds still need lov setup here*/
2827 static int mds_cmd_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
2828 {
2829         struct mds_obd *mds = &obd->u.mds;
2830         struct lvfs_run_ctxt saved;
2831         const char     *dev;
2832         struct vfsmount *mnt;
2833         struct lustre_sb_info *lsi;
2834         struct lustre_mount_info *lmi;
2835         struct dentry  *dentry;
2836         int rc = 0;
2837         ENTRY;
2838
2839         CDEBUG(D_INFO, "obd %s setup \n", obd->obd_name);
2840         if (strncmp(obd->obd_name, MDD_OBD_NAME, strlen(MDD_OBD_NAME)))
2841                 RETURN(0);
2842
2843         if (lcfg->lcfg_bufcount < 5) {
2844                 CERROR("invalid arg for setup %s\n", MDD_OBD_NAME);
2845                 RETURN(-EINVAL);
2846         }
2847         dev = lustre_cfg_string(lcfg, 4);
2848         lmi = server_get_mount(dev);
2849         LASSERT(lmi != NULL);
2850
2851         lsi = s2lsi(lmi->lmi_sb);
2852         mnt = lmi->lmi_mnt;
2853         /* FIXME: MDD LOV initialize objects.
2854          * we need only lmi here but not get mount
2855          * OSD did mount already, so put mount back
2856          */
2857         atomic_dec(&lsi->lsi_mounts);
2858         mntput(mnt);
2859
2860         obd->obd_fsops = fsfilt_get_ops(MT_STR(lsi->lsi_ldd));
2861         mds_init_ctxt(obd, mnt);
2862
2863         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
2864         dentry = simple_mkdir(current->fs->pwd, "OBJECTS", 0777, 1);
2865         if (IS_ERR(dentry)) {
2866                 rc = PTR_ERR(dentry);
2867                 CERROR("cannot create OBJECTS directory: rc = %d\n", rc);
2868                 GOTO(err_putfs, rc);
2869         }
2870         mds->mds_objects_dir = dentry;
2871
2872         dentry = lookup_one_len("__iopen__", current->fs->pwd,
2873                                 strlen("__iopen__"));
2874         if (IS_ERR(dentry)) {
2875                 rc = PTR_ERR(dentry);
2876                 CERROR("cannot lookup __iopen__ directory: rc = %d\n", rc);
2877                 GOTO(err_objects, rc);
2878         }
2879
2880         mds->mds_fid_de = dentry;
2881         if (!dentry->d_inode || is_bad_inode(dentry->d_inode)) {
2882                 rc = -ENOENT;
2883                 CERROR("__iopen__ directory has no inode? rc = %d\n", rc);
2884                 GOTO(err_fid, rc);
2885         }
2886         rc = mds_lov_init_objids(obd);
2887         if (rc != 0) {
2888                CERROR("cannot init lov objid rc = %d\n", rc);
2889                GOTO(err_fid, rc );
2890         }
2891
2892         rc = mds_lov_presetup(mds, lcfg);
2893         if (rc < 0)
2894                 GOTO(err_objects, rc);
2895
2896         /* Don't wait for mds_postrecov trying to clear orphans */
2897         obd->obd_async_recov = 1;
2898         rc = mds_postsetup(obd);
2899         /* Bug 11557 - allow async abort_recov start
2900            FIXME can remove most of this obd_async_recov plumbing
2901         obd->obd_async_recov = 0;
2902         */
2903
2904         if (rc)
2905                 GOTO(err_objects, rc);
2906
2907         mds->mds_max_mdsize = sizeof(struct lov_mds_md);
2908         mds->mds_max_cookiesize = sizeof(struct llog_cookie);
2909
2910 err_pop:
2911         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
2912         RETURN(rc);
2913 err_fid:
2914         dput(mds->mds_fid_de);
2915 err_objects:
2916         dput(mds->mds_objects_dir);
2917 err_putfs:
2918         fsfilt_put_ops(obd->obd_fsops);
2919         goto err_pop;
2920 }
2921
2922 static int mds_cmd_cleanup(struct obd_device *obd)
2923 {
2924         struct mds_obd *mds = &obd->u.mds;
2925         struct lvfs_run_ctxt saved;
2926         int rc = 0;
2927         ENTRY;
2928
2929         if (obd->obd_fail)
2930                 LCONSOLE_WARN("%s: shutting down for failover; client state "
2931                               "will be preserved.\n", obd->obd_name);
2932
2933         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
2934
2935         mds_lov_destroy_objids(obd);
2936
2937         if (mds->mds_objects_dir != NULL) {
2938                 l_dput(mds->mds_objects_dir);
2939                 mds->mds_objects_dir = NULL;
2940         }
2941
2942         shrink_dcache_parent(mds->mds_fid_de);
2943         dput(mds->mds_fid_de);
2944         LL_DQUOT_OFF(obd->u.obt.obt_sb);
2945         fsfilt_put_ops(obd->obd_fsops);
2946
2947         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
2948         RETURN(rc);
2949 }
2950
2951 #if 0
2952 static int mds_cmd_health_check(struct obd_device *obd)
2953 {
2954         return 0;
2955 }
2956 #endif
2957 static struct obd_ops mds_cmd_obd_ops = {
2958         .o_owner           = THIS_MODULE,
2959         .o_setup           = mds_cmd_setup,
2960         .o_cleanup         = mds_cmd_cleanup,
2961         .o_precleanup      = mds_precleanup,
2962         .o_create          = mds_obd_create,
2963         .o_destroy         = mds_obd_destroy,
2964         .o_llog_init       = mds_llog_init,
2965         .o_llog_finish     = mds_llog_finish,
2966         .o_notify          = mds_notify,
2967         .o_postrecov       = mds_postrecov,
2968         //   .o_health_check    = mds_cmd_health_check,
2969 };
2970
2971 static int __init mds_cmd_init(void)
2972 {
2973         struct lprocfs_static_vars lvars;
2974
2975         lprocfs_mds_init_vars(&lvars);
2976         class_register_type(&mds_cmd_obd_ops, NULL, lvars.module_vars,
2977                             LUSTRE_MDS_NAME, NULL);
2978
2979         return 0;
2980 }
2981
2982 static void /*__exit*/ mds_cmd_exit(void)
2983 {
2984         class_unregister_type(LUSTRE_MDS_NAME);
2985 }
2986
2987 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
2988 MODULE_DESCRIPTION("Lustre Metadata Server (MDS)");
2989 MODULE_LICENSE("GPL");
2990
2991 module_init(mds_cmd_init);
2992 module_exit(mds_cmd_exit);