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