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