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