Whamcloud - gitweb
f7dcd86ee599cd27a42f9b06ff917f6423e8c17d
[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 #include "mds_internal.h"
59
60 static int mds_intent_policy(struct ldlm_namespace *ns,
61                              struct ldlm_lock **lockp, void *req_cookie,
62                              ldlm_mode_t mode, int flags, void *data);
63 static int mds_postsetup(struct obd_device *obd);
64 static int mds_cleanup(struct obd_device *obd, int flags);
65
66
67 /* Assumes caller has already pushed into the kernel filesystem context */
68 static int mds_sendpage(struct ptlrpc_request *req, struct file *file,
69                         loff_t offset, int count)
70 {
71         struct ptlrpc_bulk_desc *desc;
72         struct l_wait_info lwi;
73         struct page **pages;
74         int rc = 0, npages, i, tmpcount, tmpsize = 0;
75         ENTRY;
76
77         LASSERT((offset & (PAGE_SIZE - 1)) == 0); /* I'm dubious about this */
78
79         npages = (count + PAGE_SIZE - 1) >> PAGE_SHIFT;
80         OBD_ALLOC(pages, sizeof(*pages) * npages);
81         if (!pages)
82                 GOTO(out, rc = -ENOMEM);
83
84         desc = ptlrpc_prep_bulk_exp(req, npages, BULK_PUT_SOURCE,
85                                     MDS_BULK_PORTAL);
86         if (desc == NULL)
87                 GOTO(out_free, rc = -ENOMEM);
88
89         for (i = 0, tmpcount = count; i < npages; i++, tmpcount -= tmpsize) {
90                 tmpsize = tmpcount > PAGE_SIZE ? PAGE_SIZE : tmpcount;
91
92                 pages[i] = alloc_pages(GFP_KERNEL, 0);
93                 if (pages[i] == NULL)
94                         GOTO(cleanup_buf, rc = -ENOMEM);
95
96                 ptlrpc_prep_bulk_page(desc, pages[i], 0, tmpsize);
97         }
98
99         for (i = 0, tmpcount = count; i < npages; i++, tmpcount -= tmpsize) {
100                 tmpsize = tmpcount > PAGE_SIZE ? PAGE_SIZE : tmpcount;
101                 CDEBUG(D_EXT2, "reading %u@%llu from dir %lu (size %llu)\n",
102                        tmpsize, offset, file->f_dentry->d_inode->i_ino,
103                        file->f_dentry->d_inode->i_size);
104
105                 rc = fsfilt_readpage(req->rq_export->exp_obd, file,
106                                      kmap(pages[i]), tmpsize, &offset);
107                 kunmap(pages[i]);
108
109                 if (rc != tmpsize)
110                         GOTO(cleanup_buf, rc = -EIO);
111         }
112
113         LASSERT(desc->bd_nob == count);
114
115         rc = ptlrpc_start_bulk_transfer(desc);
116         if (rc)
117                 GOTO(cleanup_buf, rc);
118
119         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE)) {
120                 CERROR("obd_fail_loc=%x, fail operation rc=%d\n",
121                        OBD_FAIL_MDS_SENDPAGE, rc = -EIO);
122                 GOTO(abort_bulk, rc);
123         }
124
125         lwi = LWI_TIMEOUT(obd_timeout * HZ / 4, NULL, NULL);
126         rc = l_wait_event(desc->bd_waitq, !ptlrpc_bulk_active(desc), &lwi);
127         LASSERT (rc == 0 || rc == -ETIMEDOUT);
128
129         if (rc == 0) {
130                 if (desc->bd_success &&
131                     desc->bd_nob_transferred == count)
132                         GOTO(cleanup_buf, rc);
133
134                 rc = -ETIMEDOUT; /* XXX should this be a different errno? */
135         }
136
137         DEBUG_REQ(D_ERROR, req, "bulk failed: %s %d(%d), evicting %s@%s\n",
138                   (rc == -ETIMEDOUT) ? "timeout" : "network error",
139                   desc->bd_nob_transferred, count,
140                   req->rq_export->exp_client_uuid.uuid,
141                   req->rq_export->exp_connection->c_remote_uuid.uuid);
142
143         ptlrpc_fail_export(req->rq_export);
144
145         EXIT;
146  abort_bulk:
147         ptlrpc_abort_bulk (desc);
148  cleanup_buf:
149         for (i = 0; i < npages; i++)
150                 if (pages[i])
151                         __free_pages(pages[i], 0);
152
153         ptlrpc_free_bulk(desc);
154  out_free:
155         OBD_FREE(pages, sizeof(*pages) * npages);
156  out:
157         return rc;
158 }
159
160 int mds_lock_mode_for_dir(struct obd_device *obd,
161                           struct dentry *dentry, int mode)
162 {
163         int ret_mode, split;
164
165         /* any dir access needs couple locks:
166          * 1) on part of dir we gonna lookup/modify in
167          * 2) on a whole dir to protect it from concurrent splitting
168          *    and to flush client's cache for readdir()
169          * so, for a given mode and dentry this routine decides what
170          * lock mode to use for lock #2:
171          * 1) if caller's gonna lookup in dir then we need to protect
172          *    dir from being splitted only - LCK_CR
173          * 2) if caller's gonna modify dir then we need to protect
174          *    dir from being splitted and to flush cache - LCK_CW
175          * 3) if caller's gonna modify dir and that dir seems ready
176          *    for splitting then we need to protect it from any
177          *    type of access (lookup/modify/split) - LCK_EX -bzzz */
178
179         split = mds_splitting_expected(obd, dentry);
180         
181         /*
182          * it is important to check here only for MDS_NO_SPLITTABLE. The reason
183          * is that MDS_NO_SPLITTABLE means dir is not splittable in principle
184          * and another thread will not split it on the quiet. But if we have
185          * MDS_NO_SPLIT_EXPECTED, this means, that dir may be splitted anytime,
186          * but not now (for current thread) and we should consider that it can
187          * happen soon and go that branch which can yield LCK_EX to protect from
188          * possible splitting.
189          */
190         if (split == MDS_NO_SPLITTABLE) {
191                 /*
192                  * this inode won't be splitted. so we need not to protect from
193                  * just flush client's cache on modification.
194                  */
195                 ret_mode = 0;
196                 if (mode == LCK_PW)
197                         ret_mode = LCK_CW;
198         } else {
199                 if (mode == LCK_EX)
200                         return LCK_EX;
201                 
202                 if (mode == LCK_PR) {
203                         ret_mode = LCK_CR;
204                 } else if (mode == LCK_PW) {
205                         /*
206                          * caller gonna modify directory.we use concurrent write
207                          * lock here to retract client's cache for readdir.
208                          */
209                         ret_mode = LCK_CW;
210                         if (split == MDS_EXPECT_SPLIT) {
211                                 /*
212                                  * splitting possible. serialize any access the
213                                  * idea is that first one seen dir is splittable
214                                  * is given exclusive lock and split
215                                  * directory. caller passes lock mode to
216                                  * mds_try_to_split_dir() and splitting would be
217                                  * done with exclusive lock only -bzzz.
218                                  */
219                                 CDEBUG(D_OTHER, "%s: gonna split %lu/%lu\n",
220                                        obd->obd_name,
221                                        (unsigned long)dentry->d_inode->i_ino,
222                                        (unsigned long)dentry->d_inode->i_generation);
223                                 ret_mode = LCK_EX;
224                         }
225                 }
226         }
227         return ret_mode;
228 }
229
230 /* only valid locked dentries or errors should be returned */
231 struct dentry *mds_id2locked_dentry(struct obd_device *obd, struct lustre_id *id,
232                                     struct vfsmount **mnt, int lock_mode,
233                                     struct lustre_handle *lockh, int *mode,
234                                     char *name, int namelen, __u64 lockpart)
235 {
236         struct dentry *de = mds_id2dentry(obd, id, mnt), *retval = de;
237         ldlm_policy_data_t policy = { .l_inodebits = { lockpart } };
238         struct ldlm_res_id res_id = { .name = {0} };
239         int flags = 0, rc;
240         ENTRY;
241
242         if (IS_ERR(de))
243                 RETURN(de);
244
245         lockh[1].cookie = 0;
246         res_id.name[0] = id_fid(id);
247         res_id.name[1] = id_group(id);
248         
249 #ifdef S_PDIROPS
250         if (name && IS_PDIROPS(de->d_inode)) {
251                 ldlm_policy_data_t cpolicy =
252                         { .l_inodebits = { MDS_INODELOCK_UPDATE } };
253                 LASSERT(mode != NULL);
254                 *mode = mds_lock_mode_for_dir(obd, de, lock_mode);
255                 if (*mode) {
256                         rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
257                                               res_id, LDLM_IBITS,
258                                               &cpolicy, *mode, &flags,
259                                               mds_blocking_ast,
260                                               ldlm_completion_ast, NULL, NULL,
261                                               NULL, 0, NULL, lockh + 1);
262                         if (rc != ELDLM_OK) {
263                                 l_dput(de);
264                                 RETURN(ERR_PTR(-ENOLCK));
265                         }
266                 }
267                 flags = 0;
268
269                 res_id.name[2] = full_name_hash(name, namelen);
270
271                 CDEBUG(D_INFO, "take lock on "DLID4":"LPX64"\n",
272                        OLID4(id), res_id.name[2]);
273         }
274 #else
275 #warning "No PDIROPS support in the kernel"
276 #endif
277         rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, res_id,
278                               LDLM_IBITS, &policy, lock_mode, &flags,
279                               mds_blocking_ast, ldlm_completion_ast,
280                               NULL, NULL, NULL, 0, NULL, lockh);
281         if (rc != ELDLM_OK) {
282                 l_dput(de);
283                 retval = ERR_PTR(-EIO); /* XXX translate ldlm code */
284 #ifdef S_PDIROPS
285                 if (lockh[1].cookie)
286                         ldlm_lock_decref(lockh + 1, *mode);
287 #endif
288         }
289
290         RETURN(retval);
291 }
292
293 #ifndef DCACHE_DISCONNECTED
294 #define DCACHE_DISCONNECTED DCACHE_NFSD_DISCONNECTED
295 #endif
296
297
298 /* Look up an entry by inode number. This function ONLY returns valid dget'd
299  * dentries with an initialized inode or errors */
300 struct dentry *mds_id2dentry(struct obd_device *obd, struct lustre_id *id,
301                              struct vfsmount **mnt)
302 {
303         unsigned long ino = (unsigned long)id_ino(id);
304         __u32 generation = (__u32)id_gen(id);
305         struct mds_obd *mds = &obd->u.mds;
306         struct dentry *result;
307         struct inode *inode;
308         char idname[32];
309
310         if (ino == 0)
311                 RETURN(ERR_PTR(-ESTALE));
312
313         snprintf(idname, sizeof(idname), "0x%lx", ino);
314
315         CDEBUG(D_DENTRY, "--> mds_id2dentry: ino/gen %lu/%u, sb %p\n",
316                ino, generation, mds->mds_sb);
317
318         /* under ext3 this is neither supposed to return bad inodes nor NULL
319            inodes. */
320         result = ll_lookup_one_len(idname, mds->mds_id_de, 
321                                    strlen(idname));
322         if (IS_ERR(result))
323                 RETURN(result);
324
325         inode = result->d_inode;
326         if (!inode)
327                 RETURN(ERR_PTR(-ENOENT));
328
329         if (is_bad_inode(inode)) {
330                 CERROR("bad inode returned %lu/%u\n",
331                        inode->i_ino, inode->i_generation);
332                 dput(result);
333                 RETURN(ERR_PTR(-ENOENT));
334         }
335
336         /* here we disabled generation check, as root inode i_generation
337          * of cache mds and real mds are different. */
338         if (inode->i_ino != id_ino(&mds->mds_rootid) && generation &&
339             inode->i_generation != generation) {
340                 /* we didn't find the right inode.. */
341                 CERROR("bad inode %lu, link: %lu, ct: %d, generation %u/%u\n",
342                        inode->i_ino, (unsigned long)inode->i_nlink,
343                        atomic_read(&inode->i_count), inode->i_generation,
344                        generation);
345                 dput(result);
346                 RETURN(ERR_PTR(-ENOENT));
347         }
348
349         if (mnt) {
350                 *mnt = mds->mds_vfsmnt;
351                 mntget(*mnt);
352         }
353
354         RETURN(result);
355 }
356
357
358 /* Establish a connection to the MDS.
359  *
360  * This will set up an export structure for the client to hold state data about
361  * that client, like open files, the last operation number it did on the server,
362  * etc.
363  */
364 static int mds_connect(struct lustre_handle *conn, struct obd_device *obd,
365                        struct obd_uuid *cluuid, unsigned long flags)
366 {
367         struct mds_export_data *med;
368         struct mds_client_data *mcd;
369         struct obd_export *exp;
370         int rc;
371         ENTRY;
372
373         if (!conn || !obd || !cluuid)
374                 RETURN(-EINVAL);
375
376         /* XXX There is a small race between checking the list and adding a new
377          * connection for the same UUID, but the real threat (list corruption
378          * when multiple different clients connect) is solved.
379          *
380          * There is a second race between adding the export to the list, and
381          * filling in the client data below.  Hence skipping the case of NULL
382          * mcd above.  We should already be controlling multiple connects at the
383          * client, and we can't hold the spinlock over memory allocations
384          * without risk of deadlocking.
385          */
386         rc = class_connect(conn, obd, cluuid);
387         if (rc)
388                 RETURN(rc);
389         exp = class_conn2export(conn);
390         
391         LASSERT(exp != NULL);
392         med = &exp->exp_mds_data;
393
394         OBD_ALLOC(mcd, sizeof(*mcd));
395         if (!mcd) {
396                 CERROR("%s: out of memory for client data.\n",
397                         obd->obd_name);
398                 GOTO(out, rc = -ENOMEM);
399         }
400
401         memcpy(mcd->mcd_uuid, cluuid, sizeof(mcd->mcd_uuid));
402         med->med_mcd = mcd;
403
404         rc = mds_client_add(obd, &obd->u.mds, med, -1);
405         if (rc)
406                 GOTO(out, rc);
407        
408         EXIT;
409 out:
410         if (rc) {
411                 if (mcd)
412                         OBD_FREE(mcd, sizeof(*mcd));
413                 class_disconnect(exp, 0);
414         } else {
415                 class_export_put(exp);
416         }
417         return rc;
418 }
419
420 static int mds_connect_post(struct obd_export *exp, unsigned long flags)
421 {
422         struct obd_device *obd = exp->exp_obd;
423         struct mds_obd *mds = &obd->u.mds;
424         int rc = 0;
425         ENTRY;
426
427         if (!(flags & OBD_OPT_MDS_CONNECTION)) {
428                 if (!(exp->exp_flags & OBD_OPT_REAL_CLIENT)) {
429                         atomic_inc(&mds->mds_real_clients);
430                         CDEBUG(D_OTHER,"%s: peer from %s is real client (%d)\n",
431                                obd->obd_name, exp->exp_client_uuid.uuid,
432                                atomic_read(&mds->mds_real_clients));
433                         exp->exp_flags |= OBD_OPT_REAL_CLIENT;
434                 }
435                 if (mds->mds_md_name)
436                         rc = mds_md_connect(obd, mds->mds_md_name);
437         }
438         RETURN(rc);
439 }
440
441 static int mds_init_export(struct obd_export *exp)
442 {
443         struct mds_export_data *med = &exp->exp_mds_data;
444
445         INIT_LIST_HEAD(&med->med_open_head);
446         spin_lock_init(&med->med_open_lock);
447         return 0;
448 }
449
450 static int mds_destroy_export(struct obd_export *export)
451 {
452         struct mds_export_data *med;
453         struct obd_device *obd = export->exp_obd;
454         struct lvfs_run_ctxt saved;
455         int rc = 0;
456         ENTRY;
457
458         med = &export->exp_mds_data;
459         target_destroy_export(export);
460
461         if (obd_uuid_equals(&export->exp_client_uuid, &obd->obd_uuid))
462                 GOTO(out, 0);
463
464         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
465
466         /* Close any open files (which may also cause orphan unlinking). */
467         spin_lock(&med->med_open_lock);
468         while (!list_empty(&med->med_open_head)) {
469                 struct list_head *tmp = med->med_open_head.next;
470                 struct mds_file_data *mfd =
471                         list_entry(tmp, struct mds_file_data, mfd_list);
472                 BDEVNAME_DECLARE_STORAGE(btmp);
473
474                 /* bug 1579: fix force-closing for 2.5 */
475                 struct dentry *dentry = mfd->mfd_dentry;
476
477                 list_del(&mfd->mfd_list);
478                 spin_unlock(&med->med_open_lock);
479
480                 /* If you change this message, be sure to update
481                  * replay_single:test_46 */
482                 CERROR("force closing client file handle for %*s (%s:%lu)\n",
483                        dentry->d_name.len, dentry->d_name.name,
484                        ll_bdevname(dentry->d_inode->i_sb, btmp),
485                        dentry->d_inode->i_ino);
486                 /* child inode->i_alloc_sem protects orphan_dec_test and
487                  * is_orphan race, mds_mfd_close drops it */
488                 DOWN_WRITE_I_ALLOC_SEM(dentry->d_inode);
489                 rc = mds_mfd_close(NULL, 0, obd, mfd,
490                                    !(export->exp_flags & OBD_OPT_FAILOVER));
491                 if (rc)
492                         CDEBUG(D_INODE, "Error closing file: %d\n", rc);
493                 spin_lock(&med->med_open_lock);
494         }
495         spin_unlock(&med->med_open_lock);
496         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
497
498         EXIT;
499 out:
500         mds_client_free(export, !(export->exp_flags & OBD_OPT_FAILOVER));
501         return rc;
502 }
503
504 static int mds_disconnect(struct obd_export *exp, unsigned long flags)
505 {
506         unsigned long irqflags;
507         struct obd_device *obd;
508         struct mds_obd *mds;
509         int rc;
510         ENTRY;
511
512         LASSERT(exp != NULL);
513         obd = class_exp2obd(exp);
514         if (obd == NULL) {
515                 CDEBUG(D_IOCTL, "invalid client cookie "LPX64"\n",
516                        exp->exp_handle.h_cookie);
517                 RETURN(-EINVAL);
518         }
519         mds = &obd->u.mds;
520
521         /*
522          * suppress any inter-mds requests durring disconnecting lmv if this is
523          * detected --force mode. This is needed to avoid endless recovery.
524          */
525         if (atomic_read(&mds->mds_real_clients) > 0 &&
526             !(exp->exp_flags & OBD_OPT_REAL_CLIENT))
527                 flags |= OBD_OPT_FORCE;
528                                                                                               
529         if (!(exp->exp_flags & OBD_OPT_REAL_CLIENT)
530             && !atomic_read(&mds->mds_real_clients)) {
531                 /* there was no client at all */
532                 mds_md_disconnect(obd, flags);
533         }
534
535         if ((exp->exp_flags & OBD_OPT_REAL_CLIENT)
536             && atomic_dec_and_test(&mds->mds_real_clients)) {
537                 /* time to drop LMV connections */
538                 CDEBUG(D_OTHER, "%s: last real client %s disconnected.  "
539                        "Disconnnect from LMV now\n",
540                        obd->obd_name, exp->exp_client_uuid.uuid);
541                 mds_md_disconnect(obd, flags);
542         }
543
544         spin_lock_irqsave(&exp->exp_lock, irqflags);
545         exp->exp_flags = flags;
546         spin_unlock_irqrestore(&exp->exp_lock, irqflags);
547
548         /* disconnect early so that clients can't keep using export */
549         rc = class_disconnect(exp, flags);
550         ldlm_cancel_locks_for_export(exp);
551
552         /* complete all outstanding replies */
553         spin_lock_irqsave(&exp->exp_lock, irqflags);
554         while (!list_empty(&exp->exp_outstanding_replies)) {
555                 struct ptlrpc_reply_state *rs =
556                         list_entry(exp->exp_outstanding_replies.next,
557                                    struct ptlrpc_reply_state, rs_exp_list);
558                 struct ptlrpc_service *svc = rs->rs_srv_ni->sni_service;
559
560                 spin_lock(&svc->srv_lock);
561                 list_del_init(&rs->rs_exp_list);
562                 ptlrpc_schedule_difficult_reply(rs);
563                 spin_unlock(&svc->srv_lock);
564         }
565         spin_unlock_irqrestore(&exp->exp_lock, irqflags);
566         RETURN(rc);
567 }
568
569 static int mds_getstatus(struct ptlrpc_request *req)
570 {
571         struct mds_obd *mds = mds_req2mds(req);
572         struct mds_body *body;
573         int rc, size;
574         ENTRY;
575
576         size = sizeof(*body);
577         
578         rc = lustre_pack_reply(req, 1, &size, NULL);
579         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_GETSTATUS_PACK)) {
580                 CERROR("mds: out of memory for message: size=%d\n", size);
581                 req->rq_status = -ENOMEM;       /* superfluous? */
582                 RETURN(-ENOMEM);
583         }
584
585         body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
586         memcpy(&body->id1, &mds->mds_rootid, sizeof(body->id1));
587
588         /*
589          * the last_committed and last_xid fields are filled in for all replies
590          * already - no need to do so here also.
591          */
592         RETURN(0);
593 }
594
595 int mds_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
596                      void *data, int flag)
597 {
598         int do_ast;
599         ENTRY;
600
601         if (flag == LDLM_CB_CANCELING) {
602                 /* Don't need to do anything here. */
603                 RETURN(0);
604         }
605
606         /* XXX layering violation!  -phil */
607         l_lock(&lock->l_resource->lr_namespace->ns_lock);
608         
609         /*
610          * get this: if mds_blocking_ast is racing with mds_intent_policy, such
611          * that mds_blocking_ast is called just before l_i_p takes the ns_lock,
612          * then by the time we get the lock, we might not be the correct
613          * blocking function anymore.  So check, and return early, if so.
614          */
615         if (lock->l_blocking_ast != mds_blocking_ast) {
616                 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
617                 RETURN(0);
618         }
619
620         lock->l_flags |= LDLM_FL_CBPENDING;
621         do_ast = (!lock->l_readers && !lock->l_writers);
622         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
623
624         if (do_ast) {
625                 struct lustre_handle lockh;
626                 int rc;
627
628                 LDLM_DEBUG(lock, "already unused, calling ldlm_cli_cancel");
629                 ldlm_lock2handle(lock, &lockh);
630                 rc = ldlm_cli_cancel(&lockh);
631                 if (rc < 0)
632                         CERROR("ldlm_cli_cancel: %d\n", rc);
633         } else {
634                 LDLM_DEBUG(lock, "Lock still has references, will be "
635                            "cancelled later");
636         }
637         RETURN(0);
638 }
639
640 int mds_get_md(struct obd_device *obd, struct inode *inode, void *md,
641                int *size, int lock)
642 {
643         int lmm_size;
644         int rc = 0;
645         ENTRY;
646
647         if (lock)
648                 down(&inode->i_sem);
649         rc = fsfilt_get_md(obd, inode, md, *size);
650         if (lock)
651                 up(&inode->i_sem);
652
653         if (rc < 0) {
654                 CERROR("Error %d reading eadata for ino %lu\n",
655                        rc, inode->i_ino);
656         } else if (rc > 0) {
657                 lmm_size = rc;
658                 
659                 if (S_ISREG(inode->i_mode))
660                         rc = mds_convert_lov_ea(obd, inode, md, lmm_size);
661                 if (S_ISDIR(inode->i_mode))
662                         rc = mds_convert_mea_ea(obd, inode, md, lmm_size);
663
664                 if (rc == 0) {
665                         *size = lmm_size;
666                         rc = lmm_size;
667                 } else if (rc > 0) {
668                         *size = rc;
669                 }
670         }
671
672         RETURN (rc);
673 }
674
675
676 /* Call with lock=1 if you want mds_pack_md to take the i_sem.
677  * Call with lock=0 if the caller has already taken the i_sem. */
678 int mds_pack_md(struct obd_device *obd, struct lustre_msg *msg, int offset,
679                 struct mds_body *body, struct inode *inode, int lock)
680 {
681         struct mds_obd *mds = &obd->u.mds;
682         void *lmm;
683         int lmm_size;
684         int rc;
685         ENTRY;
686
687         lmm = lustre_msg_buf(msg, offset, 0);
688         if (lmm == NULL) {
689                 /* Some problem with getting eadata when I sized the reply
690                  * buffer... */
691                 CDEBUG(D_INFO, "no space reserved for inode %lu MD\n",
692                        inode->i_ino);
693                 RETURN(0);
694         }
695         lmm_size = msg->buflens[offset];
696
697         /* I don't really like this, but it is a sanity check on the client
698          * MD request.  However, if the client doesn't know how much space
699          * to reserve for the MD, it shouldn't be bad to have too much space.
700          */
701         if (lmm_size > mds->mds_max_mdsize) {
702                 CWARN("Reading MD for inode %lu of %d bytes > max %d\n",
703                        inode->i_ino, lmm_size, mds->mds_max_mdsize);
704                 // RETURN(-EINVAL);
705         }
706
707         rc = mds_get_md(obd, inode, lmm, &lmm_size, lock);
708         if (rc > 0) {
709                 if (S_ISDIR(inode->i_mode))
710                         body->valid |= OBD_MD_FLDIREA;
711                 else
712                         body->valid |= OBD_MD_FLEASIZE;
713                 body->eadatasize = lmm_size;
714                 rc = 0;
715         }
716
717         RETURN(rc);
718 }
719
720 void mds_squash_root(struct mds_obd *mds, struct mds_req_sec_desc *rsd,
721                      ptl_nid_t *peernid)
722 {
723         if (!mds->mds_squash_uid ||
724             (rsd->rsd_uid && rsd->rsd_fsuid))
725                 return;
726
727         if (*peernid == mds->mds_nosquash_nid)
728                 return;
729
730         CDEBUG(D_OTHER, "squash req from 0x%llx, (%d:%d/%x)=>(%d:%d/%x)\n",
731                 *peernid, rsd->rsd_fsuid, rsd->rsd_fsgid, rsd->rsd_cap,
732                 mds->mds_squash_uid, mds->mds_squash_gid,
733                 (rsd->rsd_cap & ~CAP_FS_MASK));
734
735         rsd->rsd_uid = mds->mds_squash_uid;
736         rsd->rsd_fsuid = mds->mds_squash_uid;
737         rsd->rsd_fsgid = mds->mds_squash_gid;
738
739         /* XXX should we remove all capabilities? */
740         rsd->rsd_cap &= ~CAP_FS_MASK;
741 }
742
743 static int mds_getattr_internal(struct obd_device *obd, struct dentry *dentry,
744                                 struct ptlrpc_request *req, struct mds_body *reqbody,
745                                 int reply_off)
746 {
747         struct inode *inode = dentry->d_inode;
748         struct mds_body *body;
749         int rc = 0;
750         ENTRY;
751
752         if (inode == NULL && !(dentry->d_flags & DCACHE_CROSS_REF))
753                 RETURN(-ENOENT);
754
755         body = lustre_msg_buf(req->rq_repmsg, reply_off, sizeof(*body));
756         LASSERT(body != NULL);                 /* caller prepped reply */
757
758         if (dentry->d_flags & DCACHE_CROSS_REF) {
759                 mds_pack_dentry2body(obd, body, dentry);
760                 CDEBUG(D_OTHER, "cross reference: "DLID4"\n",
761                        OLID4(&body->id1));
762                 RETURN(0);
763         }
764         
765         mds_pack_inode2body(obd, body, inode);
766
767         if ((S_ISREG(inode->i_mode) && (reqbody->valid & OBD_MD_FLEASIZE)) ||
768             (S_ISDIR(inode->i_mode) && (reqbody->valid & OBD_MD_FLDIREA))) {
769                 rc = mds_pack_md(obd, req->rq_repmsg, reply_off + 1, body,
770                                  inode, 1);
771
772                 /* if we have LOV EA data, the OST holds size, atime, mtime. */
773                 if (!(body->valid & OBD_MD_FLEASIZE) &&
774                     !(body->valid & OBD_MD_FLDIREA))
775                         body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
776                                         OBD_MD_FLATIME | OBD_MD_FLMTIME);
777         } else if (S_ISLNK(inode->i_mode) &&
778                    (reqbody->valid & OBD_MD_LINKNAME) != 0) {
779                 int len = req->rq_repmsg->buflens[reply_off + 1];
780                 char *symname = lustre_msg_buf(req->rq_repmsg, reply_off + 1, 0);
781
782                 LASSERT(symname != NULL);       /* caller prepped reply */
783
784                 if (!inode->i_op->readlink) {
785                         rc = -ENOSYS;
786                 } else {
787                         rc = inode->i_op->readlink(dentry, symname, len);
788                         if (rc < 0) {
789                                 CERROR("readlink failed: %d\n", rc);
790                         } else if (rc != len - 1) {
791                                 CERROR("Unexpected readlink rc %d: expecting %d\n",
792                                         rc, len - 1);
793                                 rc = -EINVAL;
794                         } else {
795                                 CDEBUG(D_INODE, "read symlink dest %s\n", symname);
796                                 body->valid |= OBD_MD_LINKNAME;
797                                 body->eadatasize = rc + 1;
798                                 symname[rc] = 0;
799                                 rc = 0;
800                         }
801                 }
802         }
803
804         RETURN(rc);
805 }
806
807 static int mds_getattr_pack_msg_cf(struct ptlrpc_request *req,
808                                    struct dentry *dentry,
809                                    int offset)
810 {
811         int rc = 0, size[1] = {sizeof(struct mds_body)};
812         ENTRY;
813
814         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK)) {
815                 CERROR("failed MDS_GETATTR_PACK test\n");
816                 req->rq_status = -ENOMEM;
817                 RETURN(-ENOMEM);
818         }
819
820         rc = lustre_pack_reply(req, 1, size, NULL);
821         if (rc) {
822                 CERROR("lustre_pack_reply failed: rc %d\n", rc);
823                 GOTO(out, req->rq_status = rc);
824         }
825
826         EXIT;
827 out:
828         return rc;
829 }
830
831 static int mds_getattr_pack_msg(struct ptlrpc_request *req, 
832                                 struct inode *inode,
833                                 int offset)
834 {
835         struct mds_obd *mds = mds_req2mds(req);
836         struct mds_body *body;
837         int rc = 0, size[2] = {sizeof(*body)}, bufcount = 1;
838         ENTRY;
839
840         body = lustre_msg_buf(req->rq_reqmsg, offset, sizeof (*body));
841         LASSERT(body != NULL);                 /* checked by caller */
842         LASSERT_REQSWABBED(req, offset);       /* swabbed by caller */
843
844         if ((S_ISREG(inode->i_mode) && (body->valid & OBD_MD_FLEASIZE)) ||
845             (S_ISDIR(inode->i_mode) && (body->valid & OBD_MD_FLDIREA))) {
846                 int rc;
847                 down(&inode->i_sem);
848                 rc = fsfilt_get_md(req->rq_export->exp_obd, inode, NULL, 0);
849                 up(&inode->i_sem);
850                 CDEBUG(D_INODE, "got %d bytes MD data for inode %lu\n",
851                        rc, inode->i_ino);
852                 if (rc < 0) {
853                         if (rc != -ENODATA)
854                                 CERROR("error getting inode %lu MD: rc = %d\n",
855                                        inode->i_ino, rc);
856                         size[bufcount] = 0;
857                 } else if (rc > mds->mds_max_mdsize) {
858                         size[bufcount] = 0;
859                         CERROR("MD size %d larger than maximum possible %u\n",
860                                rc, mds->mds_max_mdsize);
861                 } else {
862                         size[bufcount] = rc;
863                 }
864                 bufcount++;
865         } else if (S_ISLNK(inode->i_mode) && (body->valid & OBD_MD_LINKNAME)) {
866                 if (inode->i_size + 1 != body->eadatasize)
867                         CERROR("symlink size: %Lu, reply space: %d\n",
868                                inode->i_size + 1, body->eadatasize);
869                 size[bufcount] = min_t(int, inode->i_size+1, body->eadatasize);
870                 bufcount++;
871                 CDEBUG(D_INODE, "symlink size: %Lu, reply space: %d\n",
872                        inode->i_size + 1, body->eadatasize);
873         }
874
875         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK)) {
876                 CERROR("failed MDS_GETATTR_PACK test\n");
877                 req->rq_status = -ENOMEM;
878                 GOTO(out, rc = -ENOMEM);
879         }
880
881         rc = lustre_pack_reply(req, bufcount, size, NULL);
882         if (rc) {
883                 CERROR("out of memory\n");
884                 GOTO(out, req->rq_status = rc);
885         }
886
887         EXIT;
888  out:
889         return rc;
890 }
891
892 int mds_check_mds_num(struct obd_device *obd, struct inode *inode,
893                       char *name, int namelen)
894 {
895         struct mea *mea = NULL;
896         int mea_size, rc = 0;
897         ENTRY;
898         
899         rc = mds_md_get_attr(obd, inode, &mea, &mea_size);
900         if (rc)
901                 RETURN(rc);
902         if (mea != NULL) {
903                 /*
904                  * dir is already splitted, check if requested filename should
905                  * live at this MDS or at another one.
906                  */
907                 int i = mea_name2idx(mea, name, namelen - 1);
908                 if (mea->mea_master != id_group(&mea->mea_ids[i])) {
909                         CDEBUG(D_OTHER,
910                                "inapropriate MDS(%d) for %s. should be "
911                                "%lu(%d)\n", mea->mea_master, name, 
912                                (unsigned long)id_group(&mea->mea_ids[i]), i);
913                         rc = -ERESTART;
914                 }
915         }
916
917         if (mea)
918                 OBD_FREE(mea, mea_size);
919         RETURN(rc);
920 }
921
922 static int mds_getattr_lock(struct ptlrpc_request *req, int offset,
923                             struct lustre_handle *child_lockh, int child_part)
924 {
925         struct obd_device *obd = req->rq_export->exp_obd;
926         struct mds_obd *mds = &obd->u.mds;
927         struct ldlm_reply *rep = NULL;
928         struct lvfs_run_ctxt saved;
929         struct mds_req_sec_desc *rsd;
930         struct mds_body *body;
931         struct dentry *dparent = NULL, *dchild = NULL;
932         struct lvfs_ucred uc;
933         struct lustre_handle parent_lockh[2] = {{0}, {0}};
934         unsigned int namesize;
935         int rc = 0, cleanup_phase = 0, resent_req = 0, update_mode, reply_offset;
936         char *name = NULL;
937         ENTRY;
938
939         LASSERT(!strcmp(obd->obd_type->typ_name, LUSTRE_MDS_NAME));
940         MD_COUNTER_INCREMENT(obd, getattr_lock);
941
942         rsd = lustre_swab_mds_secdesc(req, MDS_REQ_SECDESC_OFF);
943         if (!rsd) {
944                 CERROR("Can't unpack security desc\n");
945                 RETURN(-EFAULT);
946         }
947         mds_squash_root(mds, rsd, &req->rq_peer.peer_id.nid); 
948
949         /* swab now, before anyone looks inside the request. */
950         body = lustre_swab_reqbuf(req, offset, sizeof(*body),
951                                   lustre_swab_mds_body);
952         if (body == NULL) {
953                 CERROR("Can't swab mds_body\n");
954                 GOTO(cleanup, rc = -EFAULT);
955         }
956
957         LASSERT_REQSWAB(req, offset + 1);
958         name = lustre_msg_string(req->rq_reqmsg, offset + 1, 0);
959         if (name == NULL) {
960                 CERROR("Can't unpack name\n");
961                 GOTO(cleanup, rc = -EFAULT);
962         }
963         namesize = req->rq_reqmsg->buflens[offset + 1];
964
965         /* namesize less than 2 means we have empty name, probably came from
966            revalidate by cfid, so no point in having name to be set */
967         if (namesize <= 1)
968                 name = NULL;
969
970         LASSERT (offset == 1 || offset == 3);
971         if (offset == 3) {
972                 rep = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*rep));
973                 reply_offset = 1;
974         } else {
975                 reply_offset = 0;
976         }
977
978         rc = mds_init_ucred(&uc, rsd);
979         if (rc) {
980                 CERROR("can't init ucred\n");
981                 GOTO(cleanup, rc);
982         }
983
984         push_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
985         cleanup_phase = 1; /* kernel context */
986         intent_set_disposition(rep, DISP_LOOKUP_EXECD);
987
988         LASSERT(namesize > 0);
989         if (child_lockh->cookie != 0) {
990                 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT);
991                 resent_req = 1;
992         }
993 #if 0        
994 #if HAVE_LOOKUP_RAW
995         if (body->valid == OBD_MD_FLID) {
996                 struct mds_body *mds_reply;
997                 int size = sizeof(*mds_reply);
998                 struct inode *dir;
999                 ino_t inum;
1000
1001                 dparent = mds_id2dentry(obd, &body->id1, NULL);
1002                 if (IS_ERR(dparent)) {
1003                         rc = PTR_ERR(dparent);
1004                         GOTO(cleanup, rc);
1005                 }
1006
1007                 /*
1008                  * the user requested ONLY the inode number, so do a raw lookup.
1009                  */
1010                 rc = lustre_pack_reply(req, 1, &size, NULL);
1011                 if (rc) {
1012                         CERROR("out of memory\n");
1013                         l_dput(dparent);
1014                         GOTO(cleanup, rc);
1015                 }
1016                 dir  = dparent->d_inode;
1017                 LASSERT(dir->i_op->lookup_raw != NULL);
1018                 rc = dir->i_op->lookup_raw(dir, name, namesize - 1, &inum);
1019                 l_dput(dparent);
1020                 mds_reply = lustre_msg_buf(req->rq_repmsg, 0,
1021                                            sizeof(*mds_reply));
1022
1023                 id_ino(&mds_reply->id1) = inum;
1024                 mds_reply->valid = OBD_MD_FLID;
1025                 GOTO(cleanup, rc);
1026         }
1027 #endif
1028 #endif
1029         if (resent_req == 0) {
1030                 LASSERT(id_fid(&body->id1) != 0);
1031                 if (name) {
1032                         rc = mds_get_parent_child_locked(obd, mds, &body->id1,
1033                                                          parent_lockh, &dparent,
1034                                                          LCK_PR, 
1035                                                          MDS_INODELOCK_UPDATE,
1036                                                          &update_mode, 
1037                                                          name, namesize,
1038                                                          child_lockh, &dchild, 
1039                                                          LCK_PR, child_part);
1040                         if (rc)
1041                                 GOTO(cleanup, rc);
1042                 
1043                         cleanup_phase = 2; /* dchild, dparent, locks */
1044                         
1045                         /*
1046                          * let's make sure this name should leave on this mds
1047                          * node.
1048                          */
1049                         rc = mds_check_mds_num(obd, dparent->d_inode, name, namesize);
1050                         if (rc)
1051                                 GOTO(cleanup, rc);
1052                 } else {
1053                         /* we have no dentry here, drop LOOKUP bit */
1054                         /* FIXME: we need MDS_INODELOCK_LOOKUP or not. */
1055                         child_part &= ~MDS_INODELOCK_LOOKUP;
1056                         CDEBUG(D_OTHER, "%s: retrieve attrs for "DLID4"\n",
1057                                obd->obd_name, OLID4(&body->id1));
1058
1059                         dchild = mds_id2locked_dentry(obd, &body->id1, NULL,
1060                                                       LCK_PR, parent_lockh,
1061                                                       &update_mode,
1062                                                       NULL, 0, 
1063                                                       MDS_INODELOCK_UPDATE);
1064                         if (IS_ERR(dchild)) {
1065                                 CERROR("can't find inode with id "DLID4", err = %d\n", 
1066                                        OLID4(&body->id1), (int)PTR_ERR(dchild));
1067                                 GOTO(cleanup, rc = PTR_ERR(dchild));
1068                         }
1069                         memcpy(child_lockh, parent_lockh, sizeof(parent_lockh[0]));
1070                 }
1071         } else {
1072                 struct ldlm_lock *granted_lock;
1073
1074                 DEBUG_REQ(D_DLMTRACE, req, "resent, not enqueuing new locks");
1075                 granted_lock = ldlm_handle2lock(child_lockh);
1076
1077                 LASSERTF(granted_lock != NULL, LPU64"/%lu lockh "LPX64"\n",
1078                          id_fid(&body->id1), (unsigned long)id_group(&body->id1),
1079                          child_lockh->cookie);
1080
1081                 dparent = mds_id2dentry(obd, &body->id1, NULL);
1082                 LASSERT(dparent);
1083
1084                 dchild = ll_lookup_one_len(name, dparent, namesize - 1);
1085                 LASSERT(dchild);
1086                 LDLM_LOCK_PUT(granted_lock);
1087         }
1088
1089         cleanup_phase = 2; /* dchild, dparent, locks */
1090
1091         if (!DENTRY_VALID(dchild)) {
1092                 intent_set_disposition(rep, DISP_LOOKUP_NEG);
1093                 /*
1094                  * in the intent case, the policy clears this error: the
1095                  * disposition is enough.
1096                  */
1097                 rc = -ENOENT;
1098                 GOTO(cleanup, rc);
1099         } else {
1100                 intent_set_disposition(rep, DISP_LOOKUP_POS);
1101         }
1102
1103         if (req->rq_repmsg == NULL) {
1104                 if (dchild->d_flags & DCACHE_CROSS_REF)
1105                         rc = mds_getattr_pack_msg_cf(req, dchild, offset);
1106                 else
1107                         rc = mds_getattr_pack_msg(req, dchild->d_inode, offset);
1108                 if (rc != 0) {
1109                         CERROR ("mds_getattr_pack_msg: %d\n", rc);
1110                         GOTO (cleanup, rc);
1111                 }
1112         }
1113
1114         rc = mds_getattr_internal(obd, dchild, req, body, reply_offset);
1115         GOTO(cleanup, rc); /* returns the lock to the client */
1116
1117  cleanup:
1118         switch (cleanup_phase) {
1119         case 2:
1120                 if (resent_req == 0) {
1121                         if (rc && DENTRY_VALID(dchild))
1122                                 ldlm_lock_decref(child_lockh, LCK_PR);
1123                         if (name)
1124                                 ldlm_lock_decref(parent_lockh, LCK_PR);
1125 #ifdef S_PDIROPS
1126                         if (parent_lockh[1].cookie != 0)
1127                                 ldlm_lock_decref(parent_lockh + 1, update_mode);
1128 #endif
1129                         if (dparent)
1130                                 l_dput(dparent);
1131                 }
1132                 l_dput(dchild);
1133         case 1:
1134                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1135                 mds_exit_ucred(&uc);
1136         }
1137         return rc;
1138 }
1139
1140 static int mds_getattr(struct ptlrpc_request *req, int offset)
1141 {
1142         struct obd_device *obd = req->rq_export->exp_obd;
1143         struct lvfs_run_ctxt saved;
1144         struct dentry *de;
1145         struct mds_req_sec_desc *rsd;
1146         struct mds_body *body;
1147         struct lvfs_ucred uc;
1148         int rc = 0;
1149         ENTRY;
1150
1151         rsd = lustre_swab_mds_secdesc(req, MDS_REQ_SECDESC_OFF);
1152         if (!rsd) {
1153                 CERROR("Can't unpack security desc\n");
1154                 RETURN(-EFAULT);
1155         }
1156
1157         body = lustre_swab_reqbuf(req, offset, sizeof(*body),
1158                                   lustre_swab_mds_body);
1159         if (body == NULL) {
1160                 CERROR ("Can't unpack body\n");
1161                 RETURN (-EFAULT);
1162         }
1163
1164         MD_COUNTER_INCREMENT(obd, getattr);
1165
1166         rc = mds_init_ucred(&uc, rsd);
1167         if (rc) {
1168                 CERROR("can't init ucred\n");
1169                 RETURN(rc);
1170         }
1171
1172         push_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1173         de = mds_id2dentry(obd, &body->id1, NULL);
1174         if (IS_ERR(de)) {
1175                 rc = req->rq_status = PTR_ERR(de);
1176                 GOTO(out_pop, rc);
1177         }
1178
1179         rc = mds_getattr_pack_msg(req, de->d_inode, offset);
1180         if (rc != 0) {
1181                 CERROR("mds_getattr_pack_msg: %d\n", rc);
1182                 GOTO(out_pop, rc);
1183         }
1184
1185         req->rq_status = mds_getattr_internal(obd, de, req, body, 0);
1186
1187         l_dput(de);
1188
1189         EXIT;
1190 out_pop:
1191         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1192         mds_exit_ucred(&uc);
1193         return rc;
1194 }
1195
1196 static int mds_obd_statfs(struct obd_device *obd, struct obd_statfs *osfs,
1197                           unsigned long max_age)
1198 {
1199         int rc;
1200         ENTRY;
1201
1202         spin_lock(&obd->obd_osfs_lock);
1203         rc = fsfilt_statfs(obd, obd->u.mds.mds_sb, max_age);
1204         if (rc == 0)
1205                 memcpy(osfs, &obd->obd_osfs, sizeof(*osfs));
1206         spin_unlock(&obd->obd_osfs_lock);
1207
1208         RETURN(rc);
1209 }
1210
1211 static int mds_statfs(struct ptlrpc_request *req)
1212 {
1213         struct obd_device *obd = req->rq_export->exp_obd;
1214         int rc, size = sizeof(struct obd_statfs);
1215         ENTRY;
1216
1217         rc = lustre_pack_reply(req, 1, &size, NULL);
1218         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK)) {
1219                 CERROR("mds: statfs lustre_pack_reply failed: rc = %d\n", rc);
1220                 GOTO(out, rc);
1221         }
1222
1223         OBD_COUNTER_INCREMENT(obd, statfs);
1224
1225         /* We call this so that we can cache a bit - 1 jiffie worth */
1226         rc = mds_obd_statfs(obd, lustre_msg_buf(req->rq_repmsg, 0, size),
1227                             jiffies - HZ);
1228         if (rc) {
1229                 CERROR("mds_obd_statfs failed: rc %d\n", rc);
1230                 GOTO(out, rc);
1231         }
1232
1233         EXIT;
1234 out:
1235         req->rq_status = rc;
1236         return 0;
1237 }
1238
1239 static int mds_sync(struct ptlrpc_request *req, int offset)
1240 {
1241         struct obd_device *obd = req->rq_export->exp_obd;
1242         struct mds_obd *mds = &obd->u.mds;
1243         struct mds_body *body;
1244         int rc, size = sizeof(*body);
1245         ENTRY;
1246
1247         body = lustre_msg_buf(req->rq_reqmsg, offset, sizeof(*body));
1248         if (body == NULL)
1249                 GOTO(out, rc = -EPROTO);
1250
1251         rc = lustre_pack_reply(req, 1, &size, NULL);
1252         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK)) {
1253                 CERROR("fsync lustre_pack_reply failed: rc = %d\n", rc);
1254                 GOTO(out, rc);
1255         }
1256
1257         if (id_ino(&body->id1) == 0) {
1258                 /* an id of zero is taken to mean "sync whole filesystem" */
1259                 rc = fsfilt_sync(obd, mds->mds_sb);
1260                 if (rc)
1261                         GOTO(out, rc);
1262         } else {
1263                 /* just any file to grab fsync method - "file" arg unused */
1264                 struct file *file = mds->mds_rcvd_filp;
1265                 struct dentry *de;
1266
1267                 de = mds_id2dentry(obd, &body->id1, NULL);
1268                 if (IS_ERR(de))
1269                         GOTO(out, rc = PTR_ERR(de));
1270
1271                 rc = file->f_op->fsync(NULL, de, 1);
1272                 if (rc)
1273                         GOTO(out, rc);
1274
1275                 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
1276                 mds_pack_inode2body(obd, body, de->d_inode);
1277                 l_dput(de);
1278         }
1279
1280         EXIT;
1281 out:
1282         req->rq_status = rc;
1283         return 0;
1284 }
1285
1286 /* mds_readpage does not take a DLM lock on the inode, because the client must
1287  * already have a PR lock.
1288  *
1289  * If we were to take another one here, a deadlock will result, if another
1290  * thread is already waiting for a PW lock. */
1291 static int mds_readpage(struct ptlrpc_request *req, int offset)
1292 {
1293         struct obd_device *obd = req->rq_export->exp_obd;
1294         struct mds_obd *mds = &obd->u.mds;
1295         struct vfsmount *mnt;
1296         struct dentry *de;
1297         struct file *file;
1298         struct mds_req_sec_desc *rsd;
1299         struct mds_body *body, *repbody;
1300         struct lvfs_run_ctxt saved;
1301         int rc, size = sizeof(*repbody);
1302         struct lvfs_ucred uc;
1303         ENTRY;
1304
1305         rc = lustre_pack_reply(req, 1, &size, NULL);
1306         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK)) {
1307                 CERROR("mds: out of memory\n");
1308                 GOTO(out, rc = -ENOMEM);
1309         }
1310
1311         rsd = lustre_swab_mds_secdesc(req, MDS_REQ_SECDESC_OFF);
1312         if (!rsd) {
1313                 CERROR("Can't unpack security desc\n");
1314                 GOTO (out, rc = -EFAULT);
1315         }
1316         mds_squash_root(mds, rsd, &req->rq_peer.peer_id.nid); 
1317
1318         body = lustre_swab_reqbuf(req, offset, sizeof(*body),
1319                                   lustre_swab_mds_body);
1320         if (body == NULL) {
1321                 CERROR("Can't unpack body\n");
1322                 GOTO (out, rc = -EFAULT);
1323         }
1324
1325         rc = mds_init_ucred(&uc, rsd);
1326         if (rc) {
1327                 CERROR("can't init ucred\n");
1328                 GOTO(out, rc);
1329         }
1330
1331         push_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1332         de = mds_id2dentry(obd, &body->id1, &mnt);
1333         if (IS_ERR(de))
1334                 GOTO(out_pop, rc = PTR_ERR(de));
1335
1336         CDEBUG(D_INODE, "ino %lu\n", de->d_inode->i_ino);
1337
1338         file = dentry_open(de, mnt, O_RDONLY | O_LARGEFILE);
1339         /* note: in case of an error, dentry_open puts dentry */
1340         if (IS_ERR(file))
1341                 GOTO(out_pop, rc = PTR_ERR(file));
1342
1343         /* body->size is actually the offset -eeb */
1344         if ((body->size & (de->d_inode->i_blksize - 1)) != 0) {
1345                 CERROR("offset "LPU64" not on a block boundary of %lu\n",
1346                        body->size, de->d_inode->i_blksize);
1347                 GOTO(out_file, rc = -EFAULT);
1348         }
1349
1350         /* body->nlink is actually the #bytes to read -eeb */
1351         if (body->nlink & (de->d_inode->i_blksize - 1)) {
1352                 CERROR("size %u is not multiple of blocksize %lu\n",
1353                        body->nlink, de->d_inode->i_blksize);
1354                 GOTO(out_file, rc = -EFAULT);
1355         }
1356
1357         repbody = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*repbody));
1358         repbody->size = file->f_dentry->d_inode->i_size;
1359         repbody->valid = OBD_MD_FLSIZE;
1360
1361         /* to make this asynchronous make sure that the handling function
1362            doesn't send a reply when this function completes. Instead a
1363            callback function would send the reply */
1364         /* body->size is actually the offset -eeb */
1365         rc = mds_sendpage(req, file, body->size, body->nlink);
1366
1367         EXIT;
1368 out_file:
1369         filp_close(file, 0);
1370 out_pop:
1371         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1372         mds_exit_ucred(&uc);
1373 out:
1374         req->rq_status = rc;
1375         return 0;
1376 }
1377
1378 /* update master MDS ID, which is stored in local inode EA. */
1379 int mds_update_mid(struct obd_device *obd, struct lustre_id *id,
1380                    void *data, int data_len)
1381 {
1382         struct mds_obd *mds = &obd->u.mds;
1383         struct dentry *dentry;
1384         void *handle;
1385         int rc = 0;
1386         ENTRY;
1387
1388         LASSERT(id);
1389         LASSERT(obd);
1390         
1391         dentry = mds_id2dentry(obd, id, NULL);
1392         if (IS_ERR(dentry))
1393                 GOTO(out, rc = PTR_ERR(dentry));
1394
1395         if (!dentry->d_inode) {
1396                 CERROR("Can't find object "DLID4".\n",
1397                        OLID4(id));
1398                 GOTO(out_dentry, rc = -EINVAL);
1399         }
1400
1401         handle = fsfilt_start(obd, dentry->d_inode,
1402                               FSFILT_OP_SETATTR, NULL);
1403         if (IS_ERR(handle))
1404                 GOTO(out_dentry, rc = PTR_ERR(handle));
1405
1406         rc = mds_update_inode_mid(obd, dentry->d_inode, handle,
1407                                   (struct lustre_id *)data);
1408         if (rc) {
1409                 CERROR("Can't update inode "DLID4" master id, "
1410                        "error = %d.\n", OLID4(id), rc);
1411                 GOTO(out_commit, rc);
1412         }
1413
1414         EXIT;
1415 out_commit:
1416         fsfilt_commit(obd, mds->mds_sb, dentry->d_inode,
1417                       handle, 0);
1418 out_dentry:
1419         l_dput(dentry);
1420 out:
1421         return rc;
1422 }
1423 EXPORT_SYMBOL(mds_update_mid);
1424
1425 /* read master MDS ID, which is stored in local inode EA. */
1426 int mds_read_mid(struct obd_device *obd, struct lustre_id *id,
1427                  void *data, int data_len)
1428 {
1429         struct dentry *dentry;
1430         int rc = 0;
1431         ENTRY;
1432
1433         LASSERT(id);
1434         LASSERT(obd);
1435         
1436         dentry = mds_id2dentry(obd, id, NULL);
1437         if (IS_ERR(dentry))
1438                 GOTO(out, rc = PTR_ERR(dentry));
1439
1440         if (!dentry->d_inode) {
1441                 CERROR("Can't find object "DLID4".\n",
1442                        OLID4(id));
1443                 GOTO(out_dentry, rc = -EINVAL);
1444         }
1445
1446         down(&dentry->d_inode->i_sem);
1447         rc = mds_read_inode_mid(obd, dentry->d_inode,
1448                                 (struct lustre_id *)data);
1449         up(&dentry->d_inode->i_sem);
1450         if (rc) {
1451                 CERROR("Can't read inode "DLID4" master id, "
1452                        "error = %d.\n", OLID4(id), rc);
1453                 GOTO(out_dentry, rc);
1454         }
1455
1456         EXIT;
1457 out_dentry:
1458         l_dput(dentry);
1459 out:
1460         return rc;
1461 }
1462 EXPORT_SYMBOL(mds_read_mid);
1463
1464 int mds_reint(struct ptlrpc_request *req, int offset,
1465               struct lustre_handle *lockh)
1466 {
1467         struct mds_obd *mds = &req->rq_export->exp_obd->u.mds;
1468         struct mds_update_record *rec;
1469         struct mds_req_sec_desc *rsd;
1470         int rc;
1471         ENTRY;
1472
1473         OBD_ALLOC(rec, sizeof(*rec));
1474         if (rec == NULL)
1475                 RETURN(-ENOMEM);
1476
1477         rsd = lustre_swab_mds_secdesc(req, MDS_REQ_SECDESC_OFF);
1478         if (!rsd) {
1479                 CERROR("Can't unpack security desc\n");
1480                 GOTO(out, rc = -EFAULT);
1481         }
1482         mds_squash_root(mds, rsd, &req->rq_peer.peer_id.nid); 
1483
1484         rc = mds_update_unpack(req, offset, rec);
1485         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNPACK)) {
1486                 CERROR("invalid record\n");
1487                 GOTO(out, req->rq_status = -EINVAL);
1488         }
1489
1490         rc = mds_init_ucred(&rec->ur_uc, rsd);
1491         if (rc) {
1492                 CERROR("can't init ucred\n");
1493                 GOTO(out, rc);
1494         }
1495
1496         /* rc will be used to interrupt a for loop over multiple records */
1497         rc = mds_reint_rec(rec, offset, req, lockh);
1498         mds_exit_ucred(&rec->ur_uc);
1499         EXIT;
1500  out:
1501         OBD_FREE(rec, sizeof(*rec));
1502         return rc;
1503 }
1504
1505 static int mds_filter_recovery_request(struct ptlrpc_request *req,
1506                                        struct obd_device *obd, int *process)
1507 {
1508         switch (req->rq_reqmsg->opc) {
1509         case MDS_CONNECT: /* This will never get here, but for completeness. */
1510         case OST_CONNECT: /* This will never get here, but for completeness. */
1511         case MDS_DISCONNECT:
1512         case OST_DISCONNECT:
1513                *process = 1;
1514                RETURN(0);
1515
1516         case MDS_CLOSE:
1517         case MDS_SYNC: /* used in unmounting */
1518         case OBD_PING:
1519         case MDS_REINT:
1520         case LDLM_ENQUEUE:
1521         case OST_CREATE:
1522                 *process = target_queue_recovery_request(req, obd);
1523                 RETURN(0);
1524
1525         default:
1526                 DEBUG_REQ(D_ERROR, req, "not permitted during recovery");
1527                 *process = 0;
1528                 /* XXX what should we set rq_status to here? */
1529                 req->rq_status = -EAGAIN;
1530                 RETURN(ptlrpc_error(req));
1531         }
1532 }
1533
1534 static char *reint_names[] = {
1535         [REINT_SETATTR] "setattr",
1536         [REINT_CREATE]  "create",
1537         [REINT_LINK]    "link",
1538         [REINT_UNLINK]  "unlink",
1539         [REINT_RENAME]  "rename",
1540         [REINT_OPEN]    "open",
1541 };
1542
1543 #define FILTER_VALID_FLAGS (OBD_MD_FLTYPE | OBD_MD_FLMODE | OBD_MD_FLGENER  | \
1544                             OBD_MD_FLSIZE | OBD_MD_FLBLOCKS | OBD_MD_FLBLKSZ| \
1545                             OBD_MD_FLATIME | OBD_MD_FLMTIME | OBD_MD_FLCTIME| \
1546                             OBD_MD_FLID) 
1547
1548 static void reconstruct_create(struct ptlrpc_request *req)
1549 {
1550         struct mds_export_data *med = &req->rq_export->exp_mds_data;
1551         struct mds_client_data *mcd = med->med_mcd;
1552         struct dentry *dentry;
1553         struct ost_body *body;
1554         struct lustre_id id;
1555         int rc;
1556         ENTRY;
1557
1558         /* copy rc, transno and disp; steal locks */
1559         mds_req_from_mcd(req, mcd);
1560         if (req->rq_status) {
1561                 EXIT;
1562                 return;
1563         }
1564
1565         id_gen(&id) = 0;
1566         id_group(&id) = 0;
1567
1568         id_ino(&id) = mcd->mcd_last_data;
1569         LASSERT(id_ino(&id) != 0);
1570
1571         dentry = mds_id2dentry(req2obd(req), &id, NULL);
1572         if (IS_ERR(dentry)) {
1573                 CERROR("can't find inode "LPU64"\n", id_ino(&id));
1574                 req->rq_status = PTR_ERR(dentry);
1575                 EXIT;
1576                 return;
1577         }
1578
1579         CWARN("reconstruct reply for x"LPU64" (remote ino) "LPU64" -> %lu/%u\n",
1580               req->rq_xid, id_ino(&id), dentry->d_inode->i_ino,
1581               dentry->d_inode->i_generation);
1582
1583         body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
1584         obdo_from_inode(&body->oa, dentry->d_inode, FILTER_VALID_FLAGS);
1585         body->oa.o_id = dentry->d_inode->i_ino;
1586         body->oa.o_generation = dentry->d_inode->i_generation;
1587         body->oa.o_valid |= OBD_MD_FLID | OBD_MD_FLGENER;
1588
1589         down(&dentry->d_inode->i_sem);
1590         rc = mds_read_inode_sid(req2obd(req), dentry->d_inode, &id);
1591         up(&dentry->d_inode->i_sem);
1592         if (rc) {
1593                 CERROR("Can't read inode self id, inode %lu, "
1594                        "rc %d\n", dentry->d_inode->i_ino, rc);
1595                 id_fid(&id) = 0;
1596         }
1597
1598         body->oa.o_fid = id_fid(&id);
1599         body->oa.o_mds = id_group(&id);
1600         l_dput(dentry);
1601
1602         EXIT;
1603 }
1604
1605 static int mdt_obj_create(struct ptlrpc_request *req)
1606 {
1607         struct obd_device *obd = req->rq_export->exp_obd;
1608         struct mds_obd *mds = &obd->u.mds;
1609         struct ost_body *body, *repbody;
1610         char idname[LL_ID_NAMELEN];
1611         int size = sizeof(*repbody);
1612         struct inode *parent_inode;
1613         struct lvfs_run_ctxt saved;
1614         int rc, cleanup_phase = 0;
1615         struct dentry *new = NULL;
1616         struct dentry_params dp;
1617         int mealen, flags = 0;
1618         struct lvfs_ucred uc;
1619         struct lustre_id id;
1620         struct mea *mea;
1621         void *handle = NULL;
1622         unsigned long cr_inum = 0;
1623         ENTRY;
1624        
1625         DEBUG_REQ(D_HA, req, "create remote object");
1626         parent_inode = mds->mds_unnamed_dir->d_inode;
1627
1628         body = lustre_swab_reqbuf(req, 0, sizeof(*body),
1629                                   lustre_swab_ost_body);
1630         if (body == NULL)
1631                 RETURN(-EFAULT);
1632
1633         rc = lustre_pack_reply(req, 1, &size, NULL);
1634         if (rc)
1635                 RETURN(rc);
1636
1637         MDS_CHECK_RESENT(req, reconstruct_create(req));
1638
1639         /*
1640          * this only serve to inter-mds request, don't need check group database
1641          * here. --ericm.
1642          */
1643         uc.luc_ghash = NULL;
1644         uc.luc_ginfo = NULL;
1645         uc.luc_uid = body->oa.o_uid;
1646         uc.luc_fsuid = body->oa.o_uid;
1647         uc.luc_fsgid = body->oa.o_gid;
1648
1649         push_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1650         repbody = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*repbody));
1651
1652         /* in REPLAY case inum should be given (client or other MDS fills it) */
1653         if (body->oa.o_id && ((body->oa.o_flags & OBD_FL_RECREATE_OBJS) ||
1654             (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY))) {
1655                 /*
1656                  * this is re-create request from MDS holding directory name.
1657                  * we have to lookup given ino/gen first. if it exists (good
1658                  * case) then there is nothing to do. if it does not then we
1659                  * have to recreate it.
1660                  */
1661                 id_ino(&id) = body->oa.o_id;
1662                 id_gen(&id) = body->oa.o_generation;
1663  
1664                 new = mds_id2dentry(obd, &id, NULL);
1665                 if (!IS_ERR(new) && new->d_inode) {
1666                         struct lustre_id sid;
1667                                 
1668                         CWARN("mkdir() repairing is on its way: %lu/%lu\n",
1669                               (unsigned long)id_ino(&id), (unsigned long)id_gen(&id));
1670                         
1671                         obdo_from_inode(&repbody->oa, new->d_inode,
1672                                         FILTER_VALID_FLAGS);
1673                         
1674                         repbody->oa.o_id = new->d_inode->i_ino;
1675                         repbody->oa.o_generation = new->d_inode->i_generation;
1676                         repbody->oa.o_valid |= OBD_MD_FLID | OBD_MD_FLGENER;
1677                         cleanup_phase = 1;
1678
1679                         down(&new->d_inode->i_sem);
1680                         rc = mds_read_inode_sid(obd, new->d_inode, &sid);
1681                         up(&new->d_inode->i_sem);
1682                         if (rc) {
1683                                 CERROR("Can't read inode self id "
1684                                        "inode %lu, rc %d.\n",
1685                                        new->d_inode->i_ino, rc);
1686                                 GOTO(cleanup, rc);
1687                         }
1688
1689                         repbody->oa.o_fid = id_fid(&sid);
1690                         repbody->oa.o_mds = id_group(&sid);
1691                         LASSERT(id_fid(&sid) != 0);
1692
1693                         /* 
1694                          * here we could use fid passed in body->oa.o_fid and
1695                          * thus avoid mds_read_inode_sid().
1696                          */
1697                         cr_inum = new->d_inode->i_ino;
1698                         GOTO(cleanup, rc = 0);
1699                 }
1700         }
1701         
1702         down(&parent_inode->i_sem);
1703         handle = fsfilt_start(obd, parent_inode, FSFILT_OP_MKDIR, NULL);
1704         if (IS_ERR(handle)) {
1705                 up(&parent_inode->i_sem);
1706                 CERROR("fsfilt_start() failed, rc = %d\n",
1707                        (int)PTR_ERR(handle));
1708                 GOTO(cleanup, rc = PTR_ERR(handle));
1709         }
1710         cleanup_phase = 1; /* transaction */
1711
1712 repeat:
1713         rc = sprintf(idname, "%u.%u", ll_insecure_random_int(), current->pid);
1714         new = lookup_one_len(idname, mds->mds_unnamed_dir, rc);
1715         if (IS_ERR(new)) {
1716                 CERROR("%s: can't lookup new inode (%s) for mkdir: %d\n",
1717                        obd->obd_name, idname, (int) PTR_ERR(new));
1718                 fsfilt_commit(obd, mds->mds_sb, new->d_inode, handle, 0);
1719                 up(&parent_inode->i_sem);
1720                 RETURN(PTR_ERR(new));
1721         } else if (new->d_inode) {
1722                 CERROR("%s: name exists. repeat\n", obd->obd_name);
1723                 goto repeat;
1724         }
1725
1726         new->d_fsdata = (void *)&dp;
1727         dp.p_inum = 0;
1728         dp.p_ptr = req;
1729
1730         if ((lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) ||
1731             (body->oa.o_flags & OBD_FL_RECREATE_OBJS)) {
1732                 LASSERT(body->oa.o_id != 0);
1733                 dp.p_inum = body->oa.o_id;
1734                 DEBUG_REQ(D_HA, req, "replay create obj %lu/%lu",
1735                           (unsigned long)body->oa.o_id,
1736                           (unsigned long)body->oa.o_generation);
1737         }
1738
1739         rc = vfs_mkdir(parent_inode, new, body->oa.o_mode);
1740         if (rc == 0) {
1741                 if ((body->oa.o_flags & OBD_FL_RECREATE_OBJS) ||
1742                     lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
1743                         new->d_inode->i_generation = body->oa.o_generation;
1744                         mark_inode_dirty(new->d_inode);
1745                         
1746                         /*
1747                          * avoiding asserts in cache flush case, as
1748                          * @body->oa.o_id should be zero.
1749                          */
1750                         if (body->oa.o_id) {
1751                                 LASSERTF(body->oa.o_id == new->d_inode->i_ino, 
1752                                          "BUG 3550: failed to recreate obj "
1753                                          LPU64" -> %lu\n", body->oa.o_id,
1754                                          new->d_inode->i_ino);
1755                                 
1756                                 LASSERTF(body->oa.o_generation == 
1757                                          new->d_inode->i_generation,
1758                                          "BUG 3550: failed to recreate obj/gen "
1759                                          LPU64"/%u -> %lu/%u\n", body->oa.o_id,
1760                                          body->oa.o_generation,
1761                                          new->d_inode->i_ino, 
1762                                          new->d_inode->i_generation);
1763                         }
1764                 }
1765                 
1766                 obdo_from_inode(&repbody->oa, new->d_inode, FILTER_VALID_FLAGS);
1767                 repbody->oa.o_id = new->d_inode->i_ino;
1768                 repbody->oa.o_generation = new->d_inode->i_generation;
1769                 repbody->oa.o_valid |= OBD_MD_FLID | OBD_MD_FLGENER;
1770
1771                 if ((body->oa.o_flags & OBD_FL_RECREATE_OBJS) ||
1772                     lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
1773                         id_group(&id) = mds->mds_num;
1774                 
1775                         LASSERT(body->oa.o_fid != 0);
1776                         id_fid(&id) = body->oa.o_fid;
1777
1778                         LASSERT(body->oa.o_id != 0);
1779                         id_ino(&id) = repbody->oa.o_id;
1780                         id_gen(&id) = repbody->oa.o_generation;
1781                 
1782                         down(&new->d_inode->i_sem);
1783                         rc = mds_update_inode_sid(obd, new->d_inode, handle, &id);
1784                         up(&new->d_inode->i_sem);
1785
1786                         /* 
1787                          * make sure, that fid is up-to-date.
1788                          */
1789                         mds_set_last_fid(obd, id_fid(&id));
1790                 } else {
1791                         /*
1792                          * allocate new sid, as object is created from scratch
1793                          * and this is not replay.
1794                          */
1795                         down(&new->d_inode->i_sem);
1796                         rc = mds_alloc_inode_sid(obd, new->d_inode, handle, &id);
1797                         up(&new->d_inode->i_sem);
1798                 }
1799                 if (rc) {
1800                         CERROR("Can't update lustre ID for inode %lu, "
1801                                "error = %d\n", new->d_inode->i_ino, rc);
1802                         GOTO(cleanup, rc);
1803                 }
1804
1805                 /* initializing o_fid after it is allocated. */
1806                 repbody->oa.o_fid = id_fid(&id);
1807                 repbody->oa.o_mds = id_group(&id);
1808
1809                 rc = fsfilt_del_dir_entry(obd, new);
1810                 up(&parent_inode->i_sem);
1811                 if (rc) {
1812                         CERROR("can't remove name for object: %d\n", rc);
1813                         GOTO(cleanup, rc);
1814                 }
1815                 
1816                 cleanup_phase = 2; /* created directory object */
1817
1818                 CDEBUG(D_OTHER, "created dirobj: %lu/%lu mode %o\n",
1819                        (unsigned long)new->d_inode->i_ino,
1820                        (unsigned long)new->d_inode->i_generation,
1821                        (unsigned)new->d_inode->i_mode);
1822                 cr_inum = new->d_inode->i_ino;
1823         } else {
1824                 up(&parent_inode->i_sem);
1825                 CERROR("%s: can't create dirobj: %d\n", obd->obd_name, rc);
1826                 GOTO(cleanup, rc);
1827         }
1828
1829         if (body->oa.o_valid & OBD_MD_FLID) {
1830                 /* this is new object for splitted dir. We have to prevent
1831                  * recursive splitting on it -bzzz */
1832                 mealen = obd_size_diskmd(mds->mds_md_exp, NULL);
1833
1834                 OBD_ALLOC(mea, mealen);
1835                 if (mea == NULL)
1836                         GOTO(cleanup, rc = -ENOMEM);
1837
1838                 mea->mea_magic = MEA_MAGIC_ALL_CHARS;
1839                 mea->mea_master = 0;
1840                 mea->mea_count = 0;
1841
1842                 down(&new->d_inode->i_sem);
1843                 rc = fsfilt_set_md(obd, new->d_inode, handle, mea, mealen);
1844                 up(&new->d_inode->i_sem);
1845                 if (rc)
1846                         CERROR("fsfilt_set_md() failed, rc = %d\n", rc);
1847
1848                 OBD_FREE(mea, mealen);
1849                 CDEBUG(D_OTHER, "%s: mark non-splittable %lu/%u - %d\n",
1850                        obd->obd_name, new->d_inode->i_ino,
1851                        new->d_inode->i_generation, flags);
1852         } else if (body->oa.o_easize) {
1853                 /* we pass LCK_EX to split routine to signal that we have
1854                  * exclusive access to the directory. simple because nobody
1855                  * knows it already exists -bzzz */
1856                 rc = mds_try_to_split_dir(obd, new, NULL,
1857                                           body->oa.o_easize, LCK_EX);
1858                 if (rc < 0) {
1859                         CERROR("Can't split directory %lu, error = %d.\n",
1860                                new->d_inode->i_ino, rc);
1861                 } else {
1862                         rc = 0;
1863                 }
1864         }
1865
1866         EXIT;
1867 cleanup:
1868         switch (cleanup_phase) {
1869         case 2: /* object has been created, but we'll may want to replay it later */
1870                 if (rc == 0)
1871                         ptlrpc_require_repack(req);
1872         case 1: /* transaction */
1873                 rc = mds_finish_transno(mds, parent_inode, handle,
1874                                         req, rc, cr_inum);
1875         }
1876
1877         l_dput(new);
1878         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1879         mds_put_group_entry(mds, uc.luc_ghash);
1880         return rc;
1881 }
1882
1883 static int mdt_get_info(struct ptlrpc_request *req)
1884 {
1885         struct obd_export *exp = req->rq_export;
1886         int keylen, rc = 0;
1887         char *key;
1888         ENTRY;
1889
1890         key = lustre_msg_buf(req->rq_reqmsg, 0, 1);
1891         if (key == NULL) {
1892                 DEBUG_REQ(D_HA, req, "no get_info key");
1893                 RETURN(-EFAULT);
1894         }
1895         keylen = req->rq_reqmsg->buflens[0];
1896
1897         if ((keylen < strlen("mdsize") || strcmp(key, "mdsize") != 0) &&
1898             (keylen < strlen("mdsnum") || strcmp(key, "mdsnum") != 0) &&
1899             (keylen < strlen("rootid") || strcmp(key, "rootid") != 0))
1900                 RETURN(-EPROTO);
1901
1902         if (keylen >= strlen("rootid") && !strcmp(key, "rootid")) {
1903                 struct lustre_id *reply;
1904                 int size = sizeof(*reply);
1905                 
1906                 rc = lustre_pack_reply(req, 1, &size, NULL);
1907                 if (rc)
1908                         RETURN(rc);
1909
1910                 reply = lustre_msg_buf(req->rq_repmsg, 0, size);
1911                 rc = obd_get_info(exp, keylen, key, &size, reply);
1912         } else {
1913                 obd_id *reply;
1914                 int size = sizeof(*reply);
1915                 
1916                 rc = lustre_pack_reply(req, 1, &size, NULL);
1917                 if (rc)
1918                         RETURN(rc);
1919
1920                 reply = lustre_msg_buf(req->rq_repmsg, 0, size);
1921                 rc = obd_get_info(exp, keylen, key, &size, reply);
1922         }
1923
1924         req->rq_repmsg->status = 0;
1925         RETURN(rc);
1926 }
1927
1928 static int mds_set_info(struct obd_export *exp, __u32 keylen,
1929                         void *key, __u32 vallen, void *val)
1930 {
1931         struct obd_device *obd;
1932         struct mds_obd *mds;
1933         int rc = 0;
1934         ENTRY;
1935
1936         obd = class_exp2obd(exp);
1937         if (obd == NULL) {
1938                 CDEBUG(D_IOCTL, "invalid client cookie "LPX64"\n",
1939                        exp->exp_handle.h_cookie);
1940                 RETURN(-EINVAL);
1941         }
1942
1943         mds = &obd->u.mds;
1944         if (keylen >= strlen("mds_type") &&
1945              memcmp(key, "mds_type", keylen) == 0) {
1946                 int valsize;
1947                 __u32 group;
1948                 
1949                 CDEBUG(D_IOCTL, "set mds type to %x\n", *(int*)val);
1950                 
1951                 mds->mds_obd_type = *(int*)val;
1952                 group = FILTER_GROUP_FIRST_MDS + mds->mds_obd_type;
1953                 valsize = sizeof(group);
1954                 
1955                 /* mds number has been changed, so the corresponding obdfilter
1956                  * exp need to be changed too. */
1957                 rc = obd_set_info(mds->mds_dt_exp, strlen("mds_conn"),
1958                                   "mds_conn", valsize, &group);
1959                 RETURN(rc);
1960         }
1961         CDEBUG(D_IOCTL, "invalid key\n");
1962         RETURN(-EINVAL);
1963 }
1964
1965 static int mdt_set_info(struct ptlrpc_request *req)
1966 {
1967         char *key, *val;
1968         struct obd_export *exp = req->rq_export;
1969         int keylen, rc = 0, vallen;
1970         ENTRY;
1971
1972         key = lustre_msg_buf(req->rq_reqmsg, 0, 1);
1973         if (key == NULL) {
1974                 DEBUG_REQ(D_HA, req, "no set_info key");
1975                 RETURN(-EFAULT);
1976         }
1977         keylen = req->rq_reqmsg->buflens[0];
1978
1979         if (keylen == strlen("mds_type") &&
1980             memcmp(key, "mds_type", keylen) == 0) {
1981                 rc = lustre_pack_reply(req, 0, NULL, NULL);
1982                 if (rc)
1983                         RETURN(rc);
1984                 
1985                 val = lustre_msg_buf(req->rq_reqmsg, 1, 0);
1986                 vallen = req->rq_reqmsg->buflens[1];
1987
1988                 rc = obd_set_info(exp, keylen, key, vallen, val);
1989                 req->rq_repmsg->status = 0;
1990                 RETURN(rc);
1991         }
1992         CDEBUG(D_IOCTL, "invalid key\n");
1993         RETURN(-EINVAL);
1994 }
1995
1996 static int mds_msg_check_version(struct lustre_msg *msg)
1997 {
1998         int rc;
1999
2000         switch (msg->opc) {
2001         case MDS_CONNECT:
2002         case MDS_DISCONNECT:
2003         case OBD_PING:
2004                 rc = lustre_msg_check_version(msg, LUSTRE_OBD_VERSION);
2005                 if (rc)
2006                         CERROR("bad opc %u version %08x, expecting %08x\n",
2007                                msg->opc, msg->version, LUSTRE_OBD_VERSION);
2008                 break;
2009         case MDS_STATFS:
2010         case MDS_GETSTATUS:
2011         case MDS_GETATTR:
2012         case MDS_GETATTR_LOCK:
2013         case MDS_READPAGE:
2014         case MDS_REINT:
2015         case MDS_CLOSE:
2016         case MDS_DONE_WRITING:
2017         case MDS_PIN:
2018         case MDS_SYNC:
2019                 rc = lustre_msg_check_version(msg, LUSTRE_MDS_VERSION);
2020                 if (rc)
2021                         CERROR("bad opc %u version %08x, expecting %08x\n",
2022                                msg->opc, msg->version, LUSTRE_MDS_VERSION);
2023                 break;
2024         case LDLM_ENQUEUE:
2025         case LDLM_CONVERT:
2026         case LDLM_BL_CALLBACK:
2027         case LDLM_CP_CALLBACK:
2028                 rc = lustre_msg_check_version(msg, LUSTRE_DLM_VERSION);
2029                 if (rc)
2030                         CERROR("bad opc %u version %08x, expecting %08x\n",
2031                                msg->opc, msg->version, LUSTRE_DLM_VERSION);
2032                 break;
2033         case OBD_LOG_CANCEL:
2034         case LLOG_ORIGIN_HANDLE_OPEN:
2035         case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
2036         case LLOG_ORIGIN_HANDLE_PREV_BLOCK:
2037         case LLOG_ORIGIN_HANDLE_READ_HEADER:
2038         case LLOG_ORIGIN_HANDLE_CLOSE:
2039         case LLOG_CATINFO:
2040                 rc = lustre_msg_check_version(msg, LUSTRE_LOG_VERSION);
2041                 if (rc)
2042                         CERROR("bad opc %u version %08x, expecting %08x\n",
2043                                msg->opc, msg->version, LUSTRE_LOG_VERSION);
2044                 break;
2045         case OST_CREATE:
2046         case OST_WRITE:
2047         case OST_GET_INFO:
2048         case OST_SET_INFO:
2049                 rc = lustre_msg_check_version(msg, LUSTRE_OBD_VERSION);
2050                 if (rc)
2051                         CERROR("bad opc %u version %08x, expecting %08x\n",
2052                                msg->opc, msg->version, LUSTRE_OBD_VERSION);
2053                 break;
2054         default:
2055                 CERROR("MDS unknown opcode %d\n", msg->opc);
2056                 rc = -ENOTSUPP;
2057                 break;
2058         }
2059
2060         return rc;
2061 }
2062
2063 static char str[PTL_NALFMT_SIZE];
2064
2065 int mds_handle(struct ptlrpc_request *req)
2066 {
2067         int should_process, fail = OBD_FAIL_MDS_ALL_REPLY_NET;
2068         struct obd_device *obd = NULL;
2069         struct mds_obd *mds = NULL; /* quell gcc overwarning */
2070         int rc = 0;
2071         ENTRY;
2072
2073         OBD_FAIL_RETURN(OBD_FAIL_MDS_ALL_REQUEST_NET | OBD_FAIL_ONCE, 0);
2074
2075         rc = mds_msg_check_version(req->rq_reqmsg);
2076         if (rc) {
2077                 CERROR("MDS drop mal-formed request\n");
2078                 RETURN(rc);
2079         }
2080
2081         LASSERT(current->journal_info == NULL);
2082         /* XXX identical to OST */
2083         if (req->rq_reqmsg->opc != MDS_CONNECT) {
2084                 struct mds_export_data *med;
2085                 int recovering;
2086
2087                 if (req->rq_export == NULL) {
2088                         CERROR("operation %d on unconnected MDS from NID %s\n",
2089                                req->rq_reqmsg->opc,
2090                                ptlrpc_peernid2str(&req->rq_peer, str));
2091                         req->rq_status = -ENOTCONN;
2092                         GOTO(out, rc = -ENOTCONN);
2093                 }
2094
2095                 med = &req->rq_export->exp_mds_data;
2096                 obd = req->rq_export->exp_obd;
2097                 mds = &obd->u.mds;
2098
2099                 /* sanity check: if the xid matches, the request must
2100                  * be marked as a resent or replayed */
2101                 if (req->rq_xid == med->med_mcd->mcd_last_xid) {
2102                         LASSERTF(lustre_msg_get_flags(req->rq_reqmsg) &
2103                                  (MSG_RESENT | MSG_REPLAY),
2104                                  "rq_xid "LPU64" matches last_xid, "
2105                                  "expected RESENT flag\n",
2106                                  req->rq_xid);
2107                 }
2108                 /* else: note the opposite is not always true; a
2109                  * RESENT req after a failover will usually not match
2110                  * the last_xid, since it was likely never
2111                  * committed. A REPLAYed request will almost never
2112                  * match the last xid, however it could for a
2113                  * committed, but still retained, open. */
2114
2115                 spin_lock_bh(&obd->obd_processing_task_lock);
2116                 recovering = obd->obd_recovering;
2117                 spin_unlock_bh(&obd->obd_processing_task_lock);
2118                 if (recovering) {
2119                         rc = mds_filter_recovery_request(req, obd,
2120                                                          &should_process);
2121                         if (rc || should_process == 0) {
2122                                 RETURN(rc);
2123                         } else if (should_process < 0) {
2124                                 req->rq_status = should_process;
2125                                 rc = ptlrpc_error(req);
2126                                 RETURN(rc);
2127                         }
2128                 }
2129         }
2130
2131         switch (req->rq_reqmsg->opc) {
2132         case MDS_CONNECT:
2133                 DEBUG_REQ(D_INODE, req, "connect");
2134                 OBD_FAIL_RETURN(OBD_FAIL_MDS_CONNECT_NET, 0);
2135                 rc = target_handle_connect(req);
2136                 if (!rc)
2137                         /* Now that we have an export, set mds. */
2138                         mds = mds_req2mds(req);
2139                 break;
2140
2141         case MDS_DISCONNECT:
2142                 DEBUG_REQ(D_INODE, req, "disconnect");
2143                 OBD_FAIL_RETURN(OBD_FAIL_MDS_DISCONNECT_NET, 0);
2144                 rc = target_handle_disconnect(req);
2145                 req->rq_status = rc;            /* superfluous? */
2146                 break;
2147
2148         case MDS_GETSTATUS:
2149                 DEBUG_REQ(D_INODE, req, "getstatus");
2150                 OBD_FAIL_RETURN(OBD_FAIL_MDS_GETSTATUS_NET, 0);
2151                 rc = mds_getstatus(req);
2152                 break;
2153
2154         case MDS_GETATTR:
2155                 DEBUG_REQ(D_INODE, req, "getattr");
2156                 OBD_FAIL_RETURN(OBD_FAIL_MDS_GETATTR_NET, 0);
2157                 rc = mds_getattr(req, MDS_REQ_REC_OFF);
2158                 break;
2159
2160         case MDS_GETATTR_LOCK: {
2161                 struct lustre_handle lockh;
2162                 DEBUG_REQ(D_INODE, req, "getattr_lock");
2163                 OBD_FAIL_RETURN(OBD_FAIL_MDS_GETATTR_LOCK_NET, 0);
2164
2165                 /* If this request gets a reconstructed reply, we won't be
2166                  * acquiring any new locks in mds_getattr_lock, so we don't
2167                  * want to cancel.
2168                  */
2169                 lockh.cookie = 0;
2170                 rc = mds_getattr_lock(req, MDS_REQ_REC_OFF, &lockh,
2171                                       MDS_INODELOCK_UPDATE);
2172                 /* this non-intent call (from an ioctl) is special */
2173                 req->rq_status = rc;
2174                 if (rc == 0 && lockh.cookie)
2175                         ldlm_lock_decref(&lockh, LCK_PR);
2176                 break;
2177         }
2178         case MDS_STATFS:
2179                 DEBUG_REQ(D_INODE, req, "statfs");
2180                 OBD_FAIL_RETURN(OBD_FAIL_MDS_STATFS_NET, 0);
2181                 rc = mds_statfs(req);
2182                 break;
2183
2184         case MDS_READPAGE:
2185                 DEBUG_REQ(D_INODE, req, "readpage");
2186                 OBD_FAIL_RETURN(OBD_FAIL_MDS_READPAGE_NET, 0);
2187                 rc = mds_readpage(req, MDS_REQ_REC_OFF);
2188
2189                 if (OBD_FAIL_CHECK_ONCE(OBD_FAIL_MDS_SENDPAGE)) {
2190                         if (req->rq_reply_state) {
2191                                 lustre_free_reply_state (req->rq_reply_state);
2192                                 req->rq_reply_state = NULL;
2193                         }
2194                         RETURN(0);
2195                 }
2196
2197                 break;
2198         case MDS_REINT: {
2199                 __u32 *opcp = lustre_msg_buf(req->rq_reqmsg, MDS_REQ_REC_OFF,
2200                                              sizeof (*opcp));
2201                 __u32  opc;
2202                 int size[3] = {sizeof(struct mds_body), mds->mds_max_mdsize,
2203                                mds->mds_max_cookiesize};
2204                 int bufcount;
2205
2206                 /* NB only peek inside req now; mds_reint() will swab it */
2207                 if (opcp == NULL) {
2208                         CERROR ("Can't inspect opcode\n");
2209                         rc = -EINVAL;
2210                         break;
2211                 }
2212                 opc = *opcp;
2213                 if (lustre_msg_swabbed (req->rq_reqmsg))
2214                         __swab32s(&opc);
2215
2216                 DEBUG_REQ(D_INODE, req, "reint %d (%s)", opc,
2217                           (opc < sizeof(reint_names) / sizeof(reint_names[0]) ||
2218                            reint_names[opc] == NULL) ? reint_names[opc] :
2219                                                        "unknown opcode");
2220
2221                 OBD_FAIL_RETURN(OBD_FAIL_MDS_REINT_NET, 0);
2222
2223                 if (opc == REINT_UNLINK || opc == REINT_RENAME)
2224                         bufcount = 3;
2225                 else if (opc == REINT_OPEN)
2226                         bufcount = 2;
2227                 else
2228                         bufcount = 1;
2229
2230                 rc = lustre_pack_reply(req, bufcount, size, NULL);
2231                 if (rc)
2232                         break;
2233
2234                 rc = mds_reint(req, MDS_REQ_REC_OFF, NULL);
2235                 fail = OBD_FAIL_MDS_REINT_NET_REP;
2236                 break;
2237         }
2238
2239         case MDS_CLOSE:
2240                 DEBUG_REQ(D_INODE, req, "close");
2241                 OBD_FAIL_RETURN(OBD_FAIL_MDS_CLOSE_NET, 0);
2242                 rc = mds_close(req, MDS_REQ_REC_OFF);
2243                 break;
2244
2245         case MDS_DONE_WRITING:
2246                 DEBUG_REQ(D_INODE, req, "done_writing");
2247                 OBD_FAIL_RETURN(OBD_FAIL_MDS_DONE_WRITING_NET, 0);
2248                 rc = mds_done_writing(req, MDS_REQ_REC_OFF);
2249                 break;
2250
2251         case MDS_PIN:
2252                 DEBUG_REQ(D_INODE, req, "pin");
2253                 OBD_FAIL_RETURN(OBD_FAIL_MDS_PIN_NET, 0);
2254                 rc = mds_pin(req, MDS_REQ_REC_OFF);
2255                 break;
2256
2257         case MDS_SYNC:
2258                 DEBUG_REQ(D_INODE, req, "sync");
2259                 OBD_FAIL_RETURN(OBD_FAIL_MDS_SYNC_NET, 0);
2260                 rc = mds_sync(req, MDS_REQ_REC_OFF);
2261                 break;
2262
2263         case OBD_PING:
2264                 DEBUG_REQ(D_INODE, req, "ping");
2265                 rc = target_handle_ping(req);
2266                 break;
2267
2268         case OBD_LOG_CANCEL:
2269                 CDEBUG(D_INODE, "log cancel\n");
2270                 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOG_CANCEL_NET, 0);
2271                 rc = -ENOTSUPP; /* la la la */
2272                 break;
2273
2274         case LDLM_ENQUEUE:
2275                 DEBUG_REQ(D_INODE, req, "enqueue");
2276                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_ENQUEUE, 0);
2277                 rc = ldlm_handle_enqueue(req, ldlm_server_completion_ast,
2278                                          ldlm_server_blocking_ast, NULL);
2279                 fail = OBD_FAIL_LDLM_REPLY;
2280                 break;
2281         case LDLM_CONVERT:
2282                 DEBUG_REQ(D_INODE, req, "convert");
2283                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_CONVERT, 0);
2284                 rc = ldlm_handle_convert(req);
2285                 break;
2286         case LDLM_BL_CALLBACK:
2287         case LDLM_CP_CALLBACK:
2288                 DEBUG_REQ(D_INODE, req, "callback");
2289                 CERROR("callbacks should not happen on MDS\n");
2290                 LBUG();
2291                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_BL_CALLBACK, 0);
2292                 break;
2293         case LLOG_ORIGIN_HANDLE_OPEN:
2294                 DEBUG_REQ(D_INODE, req, "llog_init");
2295                 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
2296                 rc = llog_origin_handle_open(req);
2297                 break;
2298         case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
2299                 DEBUG_REQ(D_INODE, req, "llog next block");
2300                 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
2301                 rc = llog_origin_handle_next_block(req);
2302                 break;
2303         case LLOG_ORIGIN_HANDLE_PREV_BLOCK:
2304                 DEBUG_REQ(D_INODE, req, "llog prev block");
2305                 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
2306                 rc = llog_origin_handle_prev_block(req);
2307                 break;
2308         case LLOG_ORIGIN_HANDLE_READ_HEADER:
2309                 DEBUG_REQ(D_INODE, req, "llog read header");
2310                 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
2311                 rc = llog_origin_handle_read_header(req);
2312                 break;
2313         case LLOG_ORIGIN_HANDLE_CLOSE:
2314                 DEBUG_REQ(D_INODE, req, "llog close");
2315                 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
2316                 rc = llog_origin_handle_close(req);
2317                 break;
2318         case OST_CREATE:
2319                 DEBUG_REQ(D_INODE, req, "ost_create");
2320                 rc = mdt_obj_create(req);
2321                 break;
2322         case OST_GET_INFO:
2323                 DEBUG_REQ(D_INODE, req, "get_info");
2324                 rc = mdt_get_info(req);
2325                 break;
2326         case OST_SET_INFO:
2327                 DEBUG_REQ(D_INODE, req, "set_info");
2328                 rc = mdt_set_info(req);
2329                 break;
2330         case OST_WRITE:
2331                 CDEBUG(D_INODE, "write\n");
2332                 OBD_FAIL_RETURN(OBD_FAIL_OST_BRW_NET, 0);
2333                 rc = ost_brw_write(req, NULL);
2334                 LASSERT(current->journal_info == NULL);
2335                 /* mdt_brw sends its own replies */
2336                 RETURN(rc);
2337                 break;
2338         case LLOG_CATINFO:
2339                 DEBUG_REQ(D_INODE, req, "llog catinfo");
2340                 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
2341                 rc = llog_catinfo(req);
2342                 break;
2343         default:
2344                 req->rq_status = -ENOTSUPP;
2345                 rc = ptlrpc_error(req);
2346                 RETURN(rc);
2347         }
2348
2349         LASSERT(current->journal_info == NULL);
2350
2351         EXIT;
2352
2353         /* If we're DISCONNECTing, the mds_export_data is already freed */
2354         if (!rc && req->rq_reqmsg->opc != MDS_DISCONNECT) {
2355                 struct mds_export_data *med = &req->rq_export->exp_mds_data;
2356                 struct obd_device *obd = list_entry(mds, struct obd_device,
2357                                                     u.mds);
2358                 req->rq_repmsg->last_xid =
2359                         le64_to_cpu(med->med_mcd->mcd_last_xid);
2360
2361                 if (!obd->obd_no_transno) {
2362                         req->rq_repmsg->last_committed =
2363                                 obd->obd_last_committed;
2364                 } else {
2365                         DEBUG_REQ(D_IOCTL, req,
2366                                   "not sending last_committed update");
2367                 }
2368                 CDEBUG(D_INFO, "last_transno "LPU64", last_committed "LPU64
2369                        ", xid "LPU64"\n",
2370                        mds->mds_last_transno, obd->obd_last_committed,
2371                        req->rq_xid);
2372         }
2373  out:
2374
2375         if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_LAST_REPLAY) {
2376                 if (obd && obd->obd_recovering) {
2377                         DEBUG_REQ(D_HA, req, "LAST_REPLAY, queuing reply");
2378                         return target_queue_final_reply(req, rc);
2379                 }
2380                 /* Lost a race with recovery; let the error path DTRT. */
2381                 rc = req->rq_status = -ENOTCONN;
2382         }
2383
2384         target_send_reply(req, rc, fail);
2385         return 0;
2386 }
2387
2388 /* Update the server data on disk.  This stores the new mount_count and also the
2389  * last_rcvd value to disk.  If we don't have a clean shutdown, then the server
2390  * last_rcvd value may be less than that of the clients.  This will alert us
2391  * that we may need to do client recovery.
2392  *
2393  * Also assumes for mds_last_transno that we are not modifying it (no locking).
2394  */
2395 int mds_update_server_data(struct obd_device *obd, int force_sync)
2396 {
2397         struct mds_obd *mds = &obd->u.mds;
2398         struct mds_server_data *msd = mds->mds_server_data;
2399         struct file *filp = mds->mds_rcvd_filp;
2400         struct lvfs_run_ctxt saved;
2401         loff_t off = 0;
2402         int rc;
2403         ENTRY;
2404
2405         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
2406         msd->msd_last_transno = cpu_to_le64(mds->mds_last_transno);
2407
2408         CDEBUG(D_SUPER, "MDS mount_count is "LPU64", last_transno is "LPU64"\n",
2409                mds->mds_mount_count, mds->mds_last_transno);
2410         rc = fsfilt_write_record(obd, filp, msd, sizeof(*msd), &off, force_sync);
2411         if (rc)
2412                 CERROR("error writing MDS server data: rc = %d\n", rc);
2413         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
2414
2415         RETURN(rc);
2416 }
2417
2418 /* saves last allocated fid counter to file. */
2419 int mds_update_last_fid(struct obd_device *obd, void *handle,
2420                         int force_sync)
2421 {
2422         struct mds_obd *mds = &obd->u.mds;
2423         struct file *filp = mds->mds_fid_filp;
2424         struct lvfs_run_ctxt saved;
2425         loff_t off = 0;
2426         __u64 last_fid;
2427         int rc = 0;
2428         ENTRY;
2429
2430         spin_lock(&mds->mds_last_fid_lock);
2431         last_fid = mds->mds_last_fid;
2432         spin_unlock(&mds->mds_last_fid_lock);
2433
2434         CDEBUG(D_SUPER, "MDS last_fid is #"LPU64"\n",
2435                last_fid);
2436
2437         if (handle) {
2438                 fsfilt_add_journal_cb(obd, mds->mds_sb, last_fid,
2439                                       handle, mds_commit_last_fid_cb,
2440                                       NULL);
2441         }
2442                 
2443         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
2444         rc = fsfilt_write_record(obd, filp, &last_fid, sizeof(last_fid),
2445                                  &off, force_sync);
2446         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
2447
2448         if (rc) {
2449                 CERROR("error writing MDS last_fid #"LPU64
2450                        ", err = %d\n", last_fid, rc);
2451                 RETURN(rc);
2452         }
2453                 
2454         CDEBUG(D_SUPER, "wrote fid #"LPU64" at idx "
2455                "%llu: err = %d\n", last_fid, off, rc);
2456
2457         RETURN(rc);
2458 }
2459
2460 void mds_set_last_fid(struct obd_device *obd, __u64 fid)
2461 {
2462         struct mds_obd *mds = &obd->u.mds;
2463
2464         spin_lock(&mds->mds_last_fid_lock);
2465         if (fid > mds->mds_last_fid)
2466                 mds->mds_last_fid = fid;
2467         spin_unlock(&mds->mds_last_fid_lock);
2468 }
2469
2470 void mds_commit_last_transno_cb(struct obd_device *obd,
2471                                 __u64 transno, void *data,
2472                                 int error)
2473 {
2474         obd_transno_commit_cb(obd, transno, error);
2475 }
2476
2477 void mds_commit_last_fid_cb(struct obd_device *obd,
2478                             __u64 fid, void *data,
2479                             int error)
2480 {
2481         if (error) {
2482                 CERROR("%s: fid "LPD64" commit error: %d\n",
2483                        obd->obd_name, fid, error);
2484                 return;
2485         }
2486         
2487         CDEBUG(D_HA, "%s: fid "LPD64" committed\n",
2488                obd->obd_name, fid);
2489 }
2490
2491 __u64 mds_alloc_fid(struct obd_device *obd)
2492 {
2493         struct mds_obd *mds = &obd->u.mds;
2494         __u64 fid;
2495         
2496         spin_lock(&mds->mds_last_fid_lock);
2497         fid = ++mds->mds_last_fid;
2498         spin_unlock(&mds->mds_last_fid_lock);
2499
2500         return fid;
2501 }
2502
2503 /*
2504  * allocates new lustre_id on passed @inode and saves it to inode EA.
2505  */
2506 int mds_alloc_inode_sid(struct obd_device *obd, struct inode *inode,
2507                         void *handle, struct lustre_id *id)
2508 {
2509         struct mds_obd *mds = &obd->u.mds;
2510         int alloc = 0, rc = 0;
2511         ENTRY;
2512
2513         LASSERT(obd != NULL);
2514         LASSERT(inode != NULL);
2515
2516         if (id == NULL) {
2517                 OBD_ALLOC(id, sizeof(*id));
2518                 if (id == NULL)
2519                         RETURN(-ENOMEM);
2520                 alloc = 1;
2521         }
2522
2523         id_group(id) = mds->mds_num;
2524         id_fid(id) = mds_alloc_fid(obd);
2525         id_ino(id) = inode->i_ino;
2526         id_gen(id) = inode->i_generation;
2527         id_type(id) = (S_IFMT & inode->i_mode);
2528
2529         rc = mds_update_inode_sid(obd, inode, handle, id);
2530         if (rc) {
2531                 CERROR("Can't update inode FID EA, "
2532                        "rc = %d\n", rc);
2533         }
2534
2535         if (alloc)
2536                 OBD_FREE(id, sizeof(*id));
2537         RETURN(rc);
2538 }
2539
2540 /*
2541  * reads inode self id from inode EA. Probably later this should be replaced by
2542  * caching inode self id to avoid raeding it every time it is needed.
2543  */
2544 int mds_read_inode_sid(struct obd_device *obd, struct inode *inode,
2545                        struct lustre_id *id)
2546 {
2547         int rc;
2548         ENTRY;
2549
2550         LASSERT(id != NULL);
2551         LASSERT(obd != NULL);
2552         LASSERT(inode != NULL);
2553
2554         rc = fsfilt_get_sid(obd, inode, &id->li_fid,
2555                             sizeof(id->li_fid));
2556         if (rc < 0) {
2557                 CERROR("fsfilt_get_sid() failed, "
2558                        "rc = %d\n", rc);
2559                 RETURN(rc);
2560         } else if (!rc) {
2561                 rc = -ENODATA;
2562                 RETURN(rc);
2563         } else {
2564                 rc = 0;
2565         }
2566
2567         RETURN(rc);
2568 }
2569
2570 /* updates inode self id in EA. */
2571 int mds_update_inode_sid(struct obd_device *obd, struct inode *inode,
2572                          void *handle, struct lustre_id *id)
2573 {
2574         int rc = 0;
2575         ENTRY;
2576
2577         LASSERT(id != NULL);
2578         LASSERT(obd != NULL);
2579         LASSERT(inode != NULL);
2580         
2581         rc = fsfilt_set_sid(obd, inode, handle, &id->li_fid,
2582                             sizeof(id->li_fid));
2583         if (rc) {
2584                 CERROR("fsfilt_set_sid() failed, rc = %d\n", rc);
2585                 RETURN(rc);
2586         }
2587
2588         RETURN(rc);
2589 }
2590
2591 /* 
2592  * reads inode id on master MDS. This is usualy done by CMOBD to update requests
2593  * to master MDS by correct store cookie, needed to find inode on master MDS
2594  * quickly.
2595  */
2596 int mds_read_inode_mid(struct obd_device *obd, struct inode *inode,
2597                        struct lustre_id *id)
2598 {
2599         int rc;
2600         ENTRY;
2601
2602         LASSERT(id != NULL);
2603         LASSERT(obd != NULL);
2604         LASSERT(inode != NULL);
2605
2606         rc = fsfilt_get_mid(obd, inode, id, sizeof(*id));
2607         if (rc < 0) {
2608                 CERROR("fsfilt_get_mid() failed, "
2609                        "rc = %d\n", rc);
2610                 RETURN(rc);
2611         } else if (!rc) {
2612                 rc = -ENODATA;
2613                 RETURN(rc);
2614         } else {
2615                 rc = 0;
2616         }
2617
2618         RETURN(rc);
2619 }
2620
2621 /*
2622  * updates master inode id. Usualy this is done by CMOBD after an inode is
2623  * created and relationship between cache MDS and master one should be
2624  * established.
2625  */
2626 int mds_update_inode_mid(struct obd_device *obd, struct inode *inode,
2627                          void *handle, struct lustre_id *id)
2628 {
2629         int rc = 0;
2630         ENTRY;
2631
2632         LASSERT(id != NULL);
2633         LASSERT(obd != NULL);
2634         LASSERT(inode != NULL);
2635         
2636         rc = fsfilt_set_mid(obd, inode, handle, id, sizeof(*id));
2637         if (rc) {
2638                 CERROR("fsfilt_set_mid() failed, rc = %d\n", rc);
2639                 RETURN(rc);
2640         }
2641
2642         RETURN(rc);
2643 }
2644
2645 /* mount the file system (secretly) */
2646 static int mds_setup(struct obd_device *obd, obd_count len, void *buf)
2647 {
2648         struct lustre_cfg* lcfg = buf;
2649         struct mds_obd *mds = &obd->u.mds;
2650         char *options = NULL;
2651         struct vfsmount *mnt;
2652         char ns_name[48];
2653         unsigned long page;
2654         int rc = 0;
2655         ENTRY;
2656
2657         dev_clear_rdonly(2);
2658
2659         if (!lcfg->lcfg_inlbuf1 || !lcfg->lcfg_inlbuf2)
2660                 RETURN(rc = -EINVAL);
2661
2662         obd->obd_fsops = fsfilt_get_ops(lcfg->lcfg_inlbuf2);
2663         if (IS_ERR(obd->obd_fsops))
2664                 RETURN(rc = PTR_ERR(obd->obd_fsops));
2665
2666         mds->mds_max_mdsize = sizeof(struct lov_mds_md);
2667
2668         page = __get_free_page(GFP_KERNEL);
2669         if (!page)
2670                 RETURN(-ENOMEM);
2671
2672         options = (char *)page;
2673         memset(options, 0, PAGE_SIZE);
2674
2675         /*
2676          * here we use "iopen_nopriv" hardcoded, because it affects MDS utility
2677          * and the rest of options are passed by mount options. Probably this
2678          * should be moved to somewhere else like startup scripts or lconf.
2679          */
2680         sprintf(options, "iopen_nopriv");
2681
2682         if (lcfg->lcfg_inllen4 > 0 && lcfg->lcfg_inlbuf4)
2683                 sprintf(options + strlen(options), ",%s",
2684                         lcfg->lcfg_inlbuf4);
2685
2686         /* we have to know mdsnum before touching underlying fs -bzzz */
2687         sema_init(&mds->mds_md_sem, 1);
2688         mds->mds_md_connected = 0;
2689         mds->mds_md_name = NULL;
2690         
2691         if (lcfg->lcfg_inllen5 > 0 && lcfg->lcfg_inlbuf5 && 
2692             strncmp(lcfg->lcfg_inlbuf5, "dumb", lcfg->lcfg_inllen5)) {
2693                 class_uuid_t uuid;
2694
2695                 CDEBUG(D_OTHER, "MDS: %s is master for %s\n",
2696                        obd->obd_name, lcfg->lcfg_inlbuf5);
2697
2698                 generate_random_uuid(uuid);
2699                 class_uuid_unparse(uuid, &mds->mds_md_uuid);
2700
2701                 OBD_ALLOC(mds->mds_md_name, lcfg->lcfg_inllen5);
2702                 if (mds->mds_md_name == NULL) 
2703                         RETURN(rc = -ENOMEM);
2704
2705                 memcpy(mds->mds_md_name, lcfg->lcfg_inlbuf5,
2706                        lcfg->lcfg_inllen5);
2707                 
2708                 rc = mds_md_connect(obd, mds->mds_md_name);
2709                 if (rc) {
2710                         OBD_FREE(mds->mds_md_name, lcfg->lcfg_inllen5);
2711                         GOTO(err_ops, rc);
2712                 }
2713         }
2714         
2715         mds->mds_obd_type = MDS_MASTER_OBD;
2716
2717         if (lcfg->lcfg_inllen6 > 0 && lcfg->lcfg_inlbuf6 &&
2718             strncmp(lcfg->lcfg_inlbuf6, "dumb", lcfg->lcfg_inllen6)) {
2719                 if (!memcmp(lcfg->lcfg_inlbuf6, "master", strlen("master"))) {
2720                         mds->mds_obd_type = MDS_MASTER_OBD;
2721                 } else if (!memcmp(lcfg->lcfg_inlbuf6, "cache", strlen("cache"))) {
2722                         mds->mds_obd_type = MDS_CACHE_OBD;
2723                 }     
2724         }
2725
2726         mnt = do_kern_mount(lcfg->lcfg_inlbuf2, 0, lcfg->lcfg_inlbuf1, options);
2727         free_page(page);
2728
2729         if (IS_ERR(mnt)) {
2730                 rc = PTR_ERR(mnt);
2731                 CERROR("do_kern_mount failed: rc = %d\n", rc);
2732                 GOTO(err_ops, rc);
2733         }
2734
2735         CDEBUG(D_SUPER, "%s: mnt = %p\n", lcfg->lcfg_inlbuf1, mnt);
2736
2737         sema_init(&mds->mds_epoch_sem, 1);
2738         atomic_set(&mds->mds_real_clients, 0);
2739         spin_lock_init(&mds->mds_transno_lock);
2740         spin_lock_init(&mds->mds_last_fid_lock);
2741         sema_init(&mds->mds_orphan_recovery_sem, 1);
2742         mds->mds_max_cookiesize = sizeof(struct llog_cookie);
2743
2744         sprintf(ns_name, "mds-%s", obd->obd_uuid.uuid);
2745         obd->obd_namespace = ldlm_namespace_new(ns_name, LDLM_NAMESPACE_SERVER);
2746
2747         if (obd->obd_namespace == NULL) {
2748                 mds_cleanup(obd, 0);
2749                 GOTO(err_put, rc = -ENOMEM);
2750         }
2751         ldlm_register_intent(obd->obd_namespace, mds_intent_policy);
2752
2753         rc = mds_fs_setup(obd, mnt);
2754         if (rc) {
2755                 CERROR("%s: MDS filesystem method init failed: rc = %d\n",
2756                        obd->obd_name, rc);
2757                 GOTO(err_ns, rc);
2758         }
2759         
2760         rc = llog_start_commit_thread();
2761         if (rc < 0)
2762                 GOTO(err_fs, rc);
2763
2764         if (lcfg->lcfg_inllen3 > 0 && lcfg->lcfg_inlbuf3 &&
2765             strncmp(lcfg->lcfg_inlbuf3, "dumb", lcfg->lcfg_inllen3)) {
2766                 class_uuid_t uuid;
2767
2768                 generate_random_uuid(uuid);
2769                 class_uuid_unparse(uuid, &mds->mds_dt_uuid);
2770
2771                 OBD_ALLOC(mds->mds_profile, lcfg->lcfg_inllen3);
2772                 if (mds->mds_profile == NULL)
2773                         GOTO(err_fs, rc = -ENOMEM);
2774
2775                 memcpy(mds->mds_profile, lcfg->lcfg_inlbuf3,
2776                        lcfg->lcfg_inllen3);
2777         }
2778
2779         /* 
2780          * setup root dir and files ID dir if lmv already connected, or there is
2781          * not lmv at all.
2782          */
2783         if (mds->mds_md_exp || (lcfg->lcfg_inllen3 > 0 && lcfg->lcfg_inlbuf3 &&
2784                                 strncmp(lcfg->lcfg_inlbuf3, "dumb", lcfg->lcfg_inllen3)))
2785         {
2786                 rc = mds_fs_setup_rootid(obd);
2787                 if (rc)
2788                         GOTO(err_fs, rc);
2789
2790                 rc = mds_fs_setup_virtid(obd);
2791                 if (rc)
2792                         GOTO(err_fs, rc);
2793         }
2794
2795         ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
2796                            "mds_ldlm_client", &obd->obd_ldlm_client);
2797         obd->obd_replayable = 1;
2798
2799         rc = mds_postsetup(obd);
2800         if (rc)
2801                 GOTO(err_fs, rc);
2802
2803         RETURN(0);
2804
2805 err_fs:
2806         /* No extra cleanup needed for llog_init_commit_thread() */
2807         mds_fs_cleanup(obd, 0);
2808 err_ns:
2809         ldlm_namespace_free(obd->obd_namespace, 0);
2810         obd->obd_namespace = NULL;
2811 err_put:
2812         unlock_kernel();
2813         mntput(mds->mds_vfsmnt);
2814         mds->mds_sb = 0;
2815         lock_kernel();
2816 err_ops:
2817         fsfilt_put_ops(obd->obd_fsops);
2818         return rc;
2819 }
2820
2821 static int mds_postsetup(struct obd_device *obd)
2822 {
2823         struct mds_obd *mds = &obd->u.mds;
2824         int rc = 0;
2825         ENTRY;
2826
2827         rc = obd_llog_setup(obd, &obd->obd_llogs, LLOG_CONFIG_ORIG_CTXT, 
2828                             obd, 0, NULL, &llog_lvfs_ops);
2829         if (rc)
2830                 RETURN(rc);
2831
2832         if (mds->mds_profile) {
2833                 struct llog_ctxt *lgctxt;
2834                 struct lvfs_run_ctxt saved;
2835                 struct lustre_profile *lprof;
2836                 struct config_llog_instance cfg;
2837
2838                 cfg.cfg_instance = NULL;
2839                 cfg.cfg_uuid = mds->mds_dt_uuid;
2840                 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
2841
2842                 lgctxt = llog_get_context(&obd->obd_llogs, LLOG_CONFIG_ORIG_CTXT);
2843                 if (!lgctxt) {
2844                         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
2845                         GOTO(err_llog, rc = -EINVAL);
2846                 }
2847                 
2848                 rc = class_config_process_llog(lgctxt, mds->mds_profile, &cfg);
2849                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
2850
2851                 if (rc)
2852                         GOTO(err_llog, rc);
2853
2854                 lprof = class_get_profile(mds->mds_profile);
2855                 if (lprof == NULL) {
2856                         CERROR("No profile found: %s\n", mds->mds_profile);
2857                         GOTO(err_cleanup, rc = -ENOENT);
2858                 }
2859                 rc = mds_dt_connect(obd, lprof->lp_lov);
2860                 if (rc)
2861                         GOTO(err_cleanup, rc);
2862
2863                 rc = mds_md_postsetup(obd);
2864                 if (rc)
2865                         GOTO(err_cleanup, rc);
2866         }
2867
2868         RETURN(rc);
2869 err_cleanup:
2870         mds_dt_clean(obd);
2871 err_llog:
2872         obd_llog_cleanup(llog_get_context(&obd->obd_llogs,
2873                                           LLOG_CONFIG_ORIG_CTXT));
2874         return rc;
2875 }
2876
2877 int mds_postrecov(struct obd_device *obd)
2878 {
2879         struct mds_obd *mds = &obd->u.mds;
2880         struct llog_ctxt *ctxt;
2881         int rc, item = 0, valsize;
2882          __u32 group;
2883         ENTRY;
2884
2885         LASSERT(!obd->obd_recovering);
2886         ctxt = llog_get_context(&obd->obd_llogs, LLOG_UNLINK_ORIG_CTXT);
2887         LASSERT(ctxt != NULL);
2888
2889         /* set nextid first, so we are sure it happens */
2890         rc = mds_dt_set_nextid(obd);
2891         if (rc) {
2892                 CERROR("%s: mds_dt_set_nextid() failed\n", obd->obd_name);
2893                 GOTO(out, rc);
2894         }
2895
2896         /* clean PENDING dir */
2897         rc = mds_cleanup_orphans(obd);
2898         if (rc < 0)
2899                 GOTO(out, rc);
2900         item = rc;
2901
2902         group = FILTER_GROUP_FIRST_MDS + mds->mds_num;
2903         valsize = sizeof(group);
2904         rc = obd_set_info(mds->mds_dt_exp, strlen("mds_conn"), "mds_conn",
2905                           valsize, &group);
2906         if (rc)
2907                 GOTO(out, rc);
2908
2909         rc = llog_connect(ctxt, obd->u.mds.mds_dt_desc.ld_tgt_count,
2910                           NULL, NULL, NULL);
2911         if (rc) {
2912                 CERROR("%s: failed at llog_origin_connect: %d\n", 
2913                        obd->obd_name, rc);
2914                 GOTO(out, rc);
2915         }
2916
2917         /* remove the orphaned precreated objects */
2918         rc = mds_dt_clearorphans(mds, NULL /* all OSTs */);
2919         if (rc)
2920                 GOTO(err_llog, rc);
2921
2922 out:
2923         RETURN(rc < 0 ? rc : item);
2924
2925 err_llog:
2926         /* cleanup all llogging subsystems */
2927         rc = obd_llog_finish(obd, &obd->obd_llogs,
2928                              mds->mds_dt_desc.ld_tgt_count);
2929         if (rc)
2930                 CERROR("%s: failed to cleanup llogging subsystems\n",
2931                         obd->obd_name);
2932         goto out;
2933 }
2934
2935 int mds_dt_clean(struct obd_device *obd)
2936 {
2937         struct mds_obd *mds = &obd->u.mds;
2938         ENTRY;
2939
2940         if (mds->mds_profile) {
2941                 char * cln_prof;
2942                 struct llog_ctxt *llctx;
2943                 struct lvfs_run_ctxt saved;
2944                 struct config_llog_instance cfg;
2945                 int len = strlen(mds->mds_profile) + sizeof("-clean") + 1;
2946
2947                 OBD_ALLOC(cln_prof, len);
2948                 sprintf(cln_prof, "%s-clean", mds->mds_profile);
2949
2950                 cfg.cfg_instance = NULL;
2951                 cfg.cfg_uuid = mds->mds_dt_uuid;
2952
2953                 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
2954                 llctx = llog_get_context(&obd->obd_llogs,
2955                                          LLOG_CONFIG_ORIG_CTXT);
2956                 class_config_process_llog(llctx, cln_prof, &cfg);
2957                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
2958
2959                 OBD_FREE(cln_prof, len);
2960                 OBD_FREE(mds->mds_profile, strlen(mds->mds_profile) + 1);
2961                 mds->mds_profile = NULL;
2962         }
2963         RETURN(0);
2964 }
2965
2966 int mds_md_clean(struct obd_device *obd)
2967 {
2968         struct mds_obd *mds = &obd->u.mds;
2969         ENTRY;
2970
2971         if (mds->mds_md_name) {
2972                 OBD_FREE(mds->mds_md_name, strlen(mds->mds_md_name) + 1);
2973                 mds->mds_md_name = NULL;
2974         }
2975         RETURN(0);
2976 }
2977
2978 static int mds_precleanup(struct obd_device *obd, int flags)
2979 {
2980         int rc = 0;
2981         ENTRY;
2982
2983         mds_md_clean(obd);
2984         mds_dt_disconnect(obd, flags);
2985         mds_dt_clean(obd);
2986         obd_llog_cleanup(llog_get_context(&obd->obd_llogs, LLOG_CONFIG_ORIG_CTXT));
2987         RETURN(rc);
2988 }
2989
2990 static int mds_cleanup(struct obd_device *obd, int flags)
2991 {
2992         struct mds_obd *mds = &obd->u.mds;
2993         ENTRY;
2994
2995         if (mds->mds_sb == NULL)
2996                 RETURN(0);
2997
2998         mds_update_server_data(obd, 1);
2999         mds_update_last_fid(obd, NULL, 1);
3000         
3001         if (mds->mds_dt_objids != NULL) {
3002                 int size = mds->mds_dt_desc.ld_tgt_count *
3003                         sizeof(obd_id);
3004                 OBD_FREE(mds->mds_dt_objids, size);
3005         }
3006         mds_fs_cleanup(obd, flags);
3007
3008         unlock_kernel();
3009
3010         /*
3011          * 2 seems normal on mds, (may_umount() also expects 2 fwiw), but we
3012          * only see 1 at this point in obdfilter.
3013          */
3014         if (atomic_read(&obd->u.mds.mds_vfsmnt->mnt_count) > 2)
3015                 CERROR("%s: mount busy, mnt_count %d != 2\n", obd->obd_name,
3016                        atomic_read(&obd->u.mds.mds_vfsmnt->mnt_count));
3017
3018         mntput(mds->mds_vfsmnt);
3019         mds->mds_sb = 0;
3020
3021         ldlm_namespace_free(obd->obd_namespace, flags & OBD_OPT_FORCE);
3022
3023         spin_lock_bh(&obd->obd_processing_task_lock);
3024         if (obd->obd_recovering) {
3025                 target_cancel_recovery_timer(obd);
3026                 obd->obd_recovering = 0;
3027         }
3028         spin_unlock_bh(&obd->obd_processing_task_lock);
3029
3030         lock_kernel();
3031         dev_clear_rdonly(2);
3032         fsfilt_put_ops(obd->obd_fsops);
3033
3034         RETURN(0);
3035 }
3036
3037 static void fixup_handle_for_resent_req(struct ptlrpc_request *req,
3038                                         int offset,
3039                                         struct ldlm_lock *new_lock,
3040                                         struct ldlm_lock **old_lock,
3041                                         struct lustre_handle *lockh)
3042 {
3043         struct obd_export *exp = req->rq_export;
3044         struct obd_device *obd = exp->exp_obd;
3045         struct ldlm_request *dlmreq =
3046                 lustre_msg_buf(req->rq_reqmsg, offset, sizeof (*dlmreq));
3047         struct lustre_handle remote_hdl = dlmreq->lock_handle1;
3048         struct list_head *iter;
3049
3050         if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
3051                 return;
3052
3053         l_lock(&obd->obd_namespace->ns_lock);
3054         list_for_each(iter, &exp->exp_ldlm_data.led_held_locks) {
3055                 struct ldlm_lock *lock;
3056                 lock = list_entry(iter, struct ldlm_lock, l_export_chain);
3057                 if (lock == new_lock)
3058                         continue;
3059                 if (lock->l_remote_handle.cookie == remote_hdl.cookie) {
3060                         lockh->cookie = lock->l_handle.h_cookie;
3061                         LDLM_DEBUG(lock, "restoring lock cookie");
3062                         DEBUG_REQ(D_HA, req, "restoring lock cookie "LPX64,
3063                                   lockh->cookie);
3064                         if (old_lock)
3065                                 *old_lock = LDLM_LOCK_GET(lock);
3066                         l_unlock(&obd->obd_namespace->ns_lock);
3067                         return;
3068                 }
3069         }
3070         l_unlock(&obd->obd_namespace->ns_lock);
3071
3072         /* If the xid matches, then we know this is a resent request,
3073          * and allow it. (It's probably an OPEN, for which we don't
3074          * send a lock */
3075         if (req->rq_xid == exp->exp_mds_data.med_mcd->mcd_last_xid)
3076                 return;
3077
3078         /* This remote handle isn't enqueued, so we never received or
3079          * processed this request.  Clear MSG_RESENT, because it can
3080          * be handled like any normal request now. */
3081
3082         lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
3083
3084         DEBUG_REQ(D_HA, req, "no existing lock with rhandle "LPX64,
3085                   remote_hdl.cookie);
3086 }
3087
3088 int intent_disposition(struct ldlm_reply *rep, int flag)
3089 {
3090         if (!rep)
3091                 return 0;
3092         return (rep->lock_policy_res1 & flag);
3093 }
3094
3095 void intent_set_disposition(struct ldlm_reply *rep, int flag)
3096 {
3097         if (!rep)
3098                 return;
3099         rep->lock_policy_res1 |= flag;
3100 }
3101
3102 static int mds_intent_policy(struct ldlm_namespace *ns,
3103                              struct ldlm_lock **lockp, void *req_cookie,
3104                              ldlm_mode_t mode, int flags, void *data)
3105 {
3106         struct ptlrpc_request *req = req_cookie;
3107         struct ldlm_lock *lock = *lockp;
3108         struct ldlm_intent *it;
3109         struct mds_obd *mds = &req->rq_export->exp_obd->u.mds;
3110         struct ldlm_reply *rep;
3111         struct lustre_handle lockh[2] = {{0}, {0}};
3112         struct ldlm_lock *new_lock = NULL;
3113         int getattr_part = MDS_INODELOCK_UPDATE;
3114         int rc, repsize[4] = { sizeof(struct ldlm_reply),
3115                                sizeof(struct mds_body),
3116                                mds->mds_max_mdsize,
3117                                mds->mds_max_cookiesize };
3118         int offset = MDS_REQ_INTENT_REC_OFF; 
3119         ENTRY;
3120
3121         LASSERT(req != NULL);
3122         MD_COUNTER_INCREMENT(req->rq_export->exp_obd, intent_lock);
3123
3124         if (req->rq_reqmsg->bufcount <= MDS_REQ_INTENT_IT_OFF) {
3125                 /* No intent was provided */
3126                 int size = sizeof(struct ldlm_reply);
3127                 rc = lustre_pack_reply(req, 1, &size, NULL);
3128                 LASSERT(rc == 0);
3129                 RETURN(0);
3130         }
3131
3132         it = lustre_swab_reqbuf(req, MDS_REQ_INTENT_IT_OFF, sizeof(*it),
3133                                 lustre_swab_ldlm_intent);
3134         if (it == NULL) {
3135                 CERROR("Intent missing\n");
3136                 RETURN(req->rq_status = -EFAULT);
3137         }
3138
3139         LDLM_DEBUG(lock, "intent policy, opc: %s", ldlm_it2str(it->opc));
3140
3141         rc = lustre_pack_reply(req, 3, repsize, NULL);
3142         if (rc)
3143                 RETURN(req->rq_status = rc);
3144
3145         rep = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*rep));
3146         LASSERT(rep != NULL);
3147
3148         intent_set_disposition(rep, DISP_IT_EXECD);
3149
3150         /* execute policy */
3151         switch ((long)it->opc) {
3152         case IT_OPEN:
3153         case IT_CREAT|IT_OPEN:
3154                 /* XXX swab here to assert that an mds_open reint
3155                  * packet is following */
3156                 fixup_handle_for_resent_req(req, MDS_REQ_INTENT_LOCKREQ_OFF, 
3157                                             lock, NULL, lockh);
3158                 rep->lock_policy_res2 = mds_reint(req, offset, lockh);
3159 #if 0
3160                 /* We abort the lock if the lookup was negative and
3161                  * we did not make it to the OPEN portion */
3162                 if (!intent_disposition(rep, DISP_LOOKUP_EXECD))
3163                         RETURN(ELDLM_LOCK_ABORTED);
3164                 if (intent_disposition(rep, DISP_LOOKUP_NEG) &&
3165                     !intent_disposition(rep, DISP_OPEN_OPEN))
3166 #endif
3167                 /* IT_OPEN may return lock on cross-node dentry
3168                  * that we want to hold during attr retrival -bzzz */
3169                 if (rc != 0 || lockh[0].cookie == 0)
3170                         RETURN(ELDLM_LOCK_ABORTED);
3171                 break;
3172         case IT_LOOKUP:
3173                 getattr_part = MDS_INODELOCK_LOOKUP;
3174         case IT_CHDIR:
3175         case IT_GETATTR:
3176                 getattr_part |= MDS_INODELOCK_LOOKUP;
3177         case IT_READDIR:
3178                 fixup_handle_for_resent_req(req, MDS_REQ_INTENT_LOCKREQ_OFF, 
3179                                             lock, &new_lock, lockh);
3180                 rep->lock_policy_res2 = mds_getattr_lock(req, offset, lockh,
3181                                                          getattr_part);
3182                 /* FIXME: LDLM can set req->rq_status. MDS sets
3183                    policy_res{1,2} with disposition and status.
3184                    - replay: returns 0 & req->status is old status
3185                    - otherwise: returns req->status */
3186                 if (intent_disposition(rep, DISP_LOOKUP_NEG))
3187                         rep->lock_policy_res2 = 0;
3188                 if (!intent_disposition(rep, DISP_LOOKUP_POS) ||
3189                     rep->lock_policy_res2)
3190                         RETURN(ELDLM_LOCK_ABORTED);
3191                 if (req->rq_status != 0) {
3192                         LBUG();
3193                         rep->lock_policy_res2 = req->rq_status;
3194                         RETURN(ELDLM_LOCK_ABORTED);
3195                 }
3196                 break;
3197         case IT_UNLINK:
3198                 rc = mds_lock_and_check_slave(offset, req, lockh);
3199                 if ((rep->lock_policy_res2 = rc)) {
3200                         if (rc == ENOLCK)
3201                                 rep->lock_policy_res2 = 0;
3202                         RETURN(ELDLM_LOCK_ABORTED);
3203                 }
3204                 break;
3205         default:
3206                 CERROR("Unhandled intent "LPD64"\n", it->opc);
3207                 LBUG();
3208         }
3209
3210         /* By this point, whatever function we called above must have either
3211          * filled in 'lockh', been an intent replay, or returned an error.  We
3212          * want to allow replayed RPCs to not get a lock, since we would just
3213          * drop it below anyways because lock replay is done separately by the
3214          * client afterwards.  For regular RPCs we want to give the new lock to
3215          * the client instead of whatever lock it was about to get. */
3216         if (new_lock == NULL)
3217                 new_lock = ldlm_handle2lock(&lockh[0]);
3218         if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY))
3219                 RETURN(0);
3220
3221         LASSERTF(new_lock != NULL, "op "LPX64" lockh "LPX64"\n",
3222                  it->opc, lockh[0].cookie);
3223  
3224         /* If we've already given this lock to a client once, then we should
3225          * have no readers or writers.  Otherwise, we should have one reader
3226          * _or_ writer ref (which will be zeroed below) before returning the
3227          * lock to a client. */
3228         if (new_lock->l_export == req->rq_export) {
3229                 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
3230         } else {
3231                 LASSERT(new_lock->l_export == NULL);
3232                 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
3233         }
3234
3235         *lockp = new_lock;
3236
3237         if (new_lock->l_export == req->rq_export) {
3238                 /* Already gave this to the client, which means that we
3239                  * reconstructed a reply. */
3240                 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
3241                         MSG_RESENT);
3242                 RETURN(ELDLM_LOCK_REPLACED);
3243         }
3244
3245         /* Fixup the lock to be given to the client */
3246         l_lock(&new_lock->l_resource->lr_namespace->ns_lock);
3247         new_lock->l_readers = 0;
3248         new_lock->l_writers = 0;
3249
3250         new_lock->l_export = class_export_get(req->rq_export);
3251         list_add(&new_lock->l_export_chain,
3252                  &new_lock->l_export->exp_ldlm_data.led_held_locks);
3253
3254         new_lock->l_blocking_ast = lock->l_blocking_ast;
3255         new_lock->l_completion_ast = lock->l_completion_ast;
3256
3257         memcpy(&new_lock->l_remote_handle, &lock->l_remote_handle,
3258                sizeof(lock->l_remote_handle));
3259
3260         new_lock->l_flags &= ~LDLM_FL_LOCAL;
3261
3262         LDLM_LOCK_PUT(new_lock);
3263         l_unlock(&new_lock->l_resource->lr_namespace->ns_lock);
3264
3265         RETURN(ELDLM_LOCK_REPLACED);
3266 }
3267
3268 int mds_attach(struct obd_device *dev, obd_count len, void *data)
3269 {
3270         struct lprocfs_static_vars lvars;
3271         int rc = 0;
3272
3273         lprocfs_init_multi_vars(0, &lvars);
3274
3275         rc = lprocfs_obd_attach(dev, lvars.obd_vars);
3276         if (rc)
3277                 return rc;
3278
3279         return lprocfs_alloc_md_stats(dev, 0);
3280 }
3281
3282 int mds_detach(struct obd_device *dev)
3283 {
3284         lprocfs_free_md_stats(dev);
3285         return lprocfs_obd_detach(dev);
3286 }
3287
3288 int mdt_attach(struct obd_device *dev, obd_count len, void *data)
3289 {
3290         struct lprocfs_static_vars lvars;
3291
3292         lprocfs_init_multi_vars(1, &lvars);
3293         return lprocfs_obd_attach(dev, lvars.obd_vars);
3294 }
3295
3296 int mdt_detach(struct obd_device *dev)
3297 {
3298         return lprocfs_obd_detach(dev);
3299 }
3300
3301 static int mdt_setup(struct obd_device *obd, obd_count len, void *buf)
3302 {
3303         struct mds_obd *mds = &obd->u.mds;
3304         int rc = 0;
3305         ENTRY;
3306
3307         mds->mds_service =
3308                 ptlrpc_init_svc(MDS_NBUFS, MDS_BUFSIZE, MDS_MAXREQSIZE,
3309                                 MDS_REQUEST_PORTAL, MDC_REPLY_PORTAL,
3310                                 mds_handle, "mds", obd->obd_proc_entry);
3311
3312         if (!mds->mds_service) {
3313                 CERROR("failed to start service\n");
3314                 RETURN(-ENOMEM);
3315         }
3316
3317         rc = ptlrpc_start_n_threads(obd, mds->mds_service, MDT_NUM_THREADS,
3318                                     "ll_mdt");
3319         if (rc)
3320                 GOTO(err_thread, rc);
3321
3322         mds->mds_setattr_service =
3323                 ptlrpc_init_svc(MDS_NBUFS, MDS_BUFSIZE, MDS_MAXREQSIZE,
3324                                 MDS_SETATTR_PORTAL, MDC_REPLY_PORTAL,
3325                                 mds_handle, "mds_setattr",
3326                                 obd->obd_proc_entry);
3327         if (!mds->mds_setattr_service) {
3328                 CERROR("failed to start getattr service\n");
3329                 GOTO(err_thread, rc = -ENOMEM);
3330         }
3331
3332         rc = ptlrpc_start_n_threads(obd, mds->mds_setattr_service,
3333                                     MDT_NUM_THREADS, "ll_mdt_attr");
3334         if (rc)
3335                 GOTO(err_thread2, rc);
3336
3337         mds->mds_readpage_service =
3338                 ptlrpc_init_svc(MDS_NBUFS, MDS_BUFSIZE, MDS_MAXREQSIZE,
3339                                 MDS_READPAGE_PORTAL, MDC_REPLY_PORTAL,
3340                                 mds_handle, "mds_readpage",
3341                                 obd->obd_proc_entry);
3342         if (!mds->mds_readpage_service) {
3343                 CERROR("failed to start readpage service\n");
3344                 GOTO(err_thread2, rc = -ENOMEM);
3345         }
3346
3347         rc = ptlrpc_start_n_threads(obd, mds->mds_readpage_service,
3348                                     MDT_NUM_THREADS, "ll_mdt_rdpg");
3349
3350         if (rc)
3351                 GOTO(err_thread3, rc);
3352
3353         RETURN(0);
3354
3355 err_thread3:
3356         ptlrpc_unregister_service(mds->mds_readpage_service);
3357 err_thread2:
3358         ptlrpc_unregister_service(mds->mds_setattr_service);
3359 err_thread:
3360         ptlrpc_unregister_service(mds->mds_service);
3361         return rc;
3362 }
3363
3364 static int mdt_cleanup(struct obd_device *obd, int flags)
3365 {
3366         struct mds_obd *mds = &obd->u.mds;
3367         ENTRY;
3368
3369         ptlrpc_stop_all_threads(mds->mds_readpage_service);
3370         ptlrpc_unregister_service(mds->mds_readpage_service);
3371
3372         ptlrpc_stop_all_threads(mds->mds_setattr_service);
3373         ptlrpc_unregister_service(mds->mds_setattr_service);
3374
3375         ptlrpc_stop_all_threads(mds->mds_service);
3376         ptlrpc_unregister_service(mds->mds_service);
3377
3378         RETURN(0);
3379 }
3380
3381 static struct dentry *mds_lvfs_id2dentry(__u64 ino, __u32 gen,
3382                                          __u64 gr, void *data)
3383 {
3384         struct lustre_id id;
3385         struct obd_device *obd = data;
3386         
3387         id_ino(&id) = ino;
3388         id_gen(&id) = gen;
3389         return mds_id2dentry(obd, &id, NULL);
3390 }
3391
3392 static int mds_get_info(struct obd_export *exp, __u32 keylen,
3393                         void *key, __u32 *valsize, void *val)
3394 {
3395         struct obd_device *obd;
3396         struct mds_obd *mds;
3397         ENTRY;
3398
3399         obd = class_exp2obd(exp);
3400         mds = &obd->u.mds;
3401         
3402         if (obd == NULL) {
3403                 CDEBUG(D_IOCTL, "invalid client cookie "LPX64"\n",
3404                        exp->exp_handle.h_cookie);
3405                 RETURN(-EINVAL);
3406         }
3407
3408         if (keylen >= strlen("reint_log") && memcmp(key, "reint_log", 9) == 0) {
3409                 /* get log_context handle. */
3410                 unsigned long *llh_handle = val;
3411                 *valsize = sizeof(unsigned long);
3412                 *llh_handle = (unsigned long)obd->obd_llog_ctxt[LLOG_REINT_ORIG_CTXT];
3413                 RETURN(0);
3414         }
3415         if (keylen >= strlen("cache_sb") && memcmp(key, "cache_sb", 8) == 0) {
3416                 /* get log_context handle. */
3417                 unsigned long *sb = val;
3418                 *valsize = sizeof(unsigned long);
3419                 *sb = (unsigned long)obd->u.mds.mds_sb;
3420                 RETURN(0);
3421         }
3422
3423         if (keylen >= strlen("mdsize") && memcmp(key, "mdsize", keylen) == 0) {
3424                 __u32 *mdsize = val;
3425                 *valsize = sizeof(*mdsize);
3426                 *mdsize = mds->mds_max_mdsize;
3427                 RETURN(0);
3428         }
3429
3430         if (keylen >= strlen("mdsnum") && strcmp(key, "mdsnum") == 0) {
3431                 __u32 *mdsnum = val;
3432                 *valsize = sizeof(*mdsnum);
3433                 *mdsnum = mds->mds_num;
3434                 RETURN(0);
3435         }
3436
3437         if (keylen >= strlen("rootid") && strcmp(key, "rootid") == 0) {
3438                 struct lustre_id *rootid = val;
3439                 *valsize = sizeof(struct lustre_id);
3440                 *rootid = mds->mds_rootid;
3441                 RETURN(0);
3442         }
3443
3444         CDEBUG(D_IOCTL, "invalid key\n");
3445         RETURN(-EINVAL);
3446
3447 }
3448 struct lvfs_callback_ops mds_lvfs_ops = {
3449         l_id2dentry:     mds_lvfs_id2dentry,
3450 };
3451
3452 int mds_preprw(int cmd, struct obd_export *exp, struct obdo *oa,
3453                 int objcount, struct obd_ioobj *obj,
3454                 int niocount, struct niobuf_remote *nb,
3455                 struct niobuf_local *res,
3456                 struct obd_trans_info *oti);
3457
3458 int mds_commitrw(int cmd, struct obd_export *exp, struct obdo *oa,
3459                  int objcount, struct obd_ioobj *obj, int niocount,
3460                  struct niobuf_local *res, struct obd_trans_info *oti,
3461                  int rc);
3462
3463 /* use obd ops to offer management infrastructure */
3464 static struct obd_ops mds_obd_ops = {
3465         .o_owner           = THIS_MODULE,
3466         .o_attach          = mds_attach,
3467         .o_detach          = mds_detach,
3468         .o_connect         = mds_connect,
3469         .o_connect_post    = mds_connect_post,
3470         .o_init_export     = mds_init_export,
3471         .o_destroy_export  = mds_destroy_export,
3472         .o_disconnect      = mds_disconnect,
3473         .o_setup           = mds_setup,
3474         .o_precleanup      = mds_precleanup,
3475         .o_cleanup         = mds_cleanup,
3476         .o_postrecov       = mds_postrecov,
3477         .o_statfs          = mds_obd_statfs,
3478         .o_iocontrol       = mds_iocontrol,
3479         .o_create          = mds_obd_create,
3480         .o_destroy         = mds_obd_destroy,
3481         .o_llog_init       = mds_llog_init,
3482         .o_llog_finish     = mds_llog_finish,
3483         .o_notify          = mds_notify,
3484         .o_get_info        = mds_get_info,
3485         .o_set_info        = mds_set_info,
3486         .o_preprw          = mds_preprw, 
3487         .o_commitrw        = mds_commitrw,
3488 };
3489
3490 static struct obd_ops mdt_obd_ops = {
3491         .o_owner           = THIS_MODULE,
3492         .o_attach          = mdt_attach,
3493         .o_detach          = mdt_detach,
3494         .o_setup           = mdt_setup,
3495         .o_cleanup         = mdt_cleanup,
3496 };
3497
3498 static int __init mds_init(void)
3499 {
3500         struct lprocfs_static_vars lvars;
3501
3502         mds_group_hash_init();
3503
3504         lprocfs_init_multi_vars(0, &lvars);
3505         class_register_type(&mds_obd_ops, NULL, lvars.module_vars,
3506                             LUSTRE_MDS_NAME);
3507         lprocfs_init_multi_vars(1, &lvars);
3508         class_register_type(&mdt_obd_ops, NULL, lvars.module_vars,
3509                             LUSTRE_MDT_NAME);
3510
3511         return 0;
3512 }
3513
3514 static void /*__exit*/ mds_exit(void)
3515 {
3516         mds_group_hash_cleanup();
3517
3518         class_unregister_type(LUSTRE_MDS_NAME);
3519         class_unregister_type(LUSTRE_MDT_NAME);
3520 }
3521
3522 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
3523 MODULE_DESCRIPTION("Lustre Metadata Server (MDS)");
3524 MODULE_LICENSE("GPL");
3525
3526 module_init(mds_init);
3527 module_exit(mds_exit);