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