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