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