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