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