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