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