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