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