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