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