Whamcloud - gitweb
Remove bogus LBUG and error return code. This was triggered by a non-root
[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, 2002 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  *
12  *   This file is part of Lustre, http://www.lustre.org.
13  *
14  *   Lustre is free software; you can redistribute it and/or
15  *   modify it under the terms of version 2 of the GNU General Public
16  *   License as published by the Free Software Foundation.
17  *
18  *   Lustre is distributed in the hope that it will be useful,
19  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
20  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  *   GNU General Public License for more details.
22  *
23  *   You should have received a copy of the GNU General Public License
24  *   along with Lustre; if not, write to the Free Software
25  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26  */
27
28 #define EXPORT_SYMTAB
29 #define DEBUG_SUBSYSTEM S_MDS
30
31 #include <linux/module.h>
32 #include <linux/lustre_mds.h>
33 #include <linux/lustre_dlm.h>
34 #include <linux/init.h>
35 #include <linux/obd_class.h>
36 #include <linux/random.h>
37 #include <linux/locks.h>
38 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
39 #include <linux/buffer_head.h>
40 #endif
41 #include <linux/obd_lov.h>
42 #include <linux/lprocfs_status.h>
43
44 static kmem_cache_t *mds_file_cache;
45
46 extern int mds_get_lovtgts(struct mds_obd *obd, int tgt_count,
47                            obd_uuid_t *uuidarray);
48 extern int mds_get_lovdesc(struct mds_obd  *obd, struct lov_desc *desc);
49 extern int mds_update_last_rcvd(struct mds_obd *mds, void *handle,
50                                 struct ptlrpc_request *req);
51 static int mds_cleanup(struct obd_device * obddev);
52
53 extern struct lprocfs_vars status_var_nm_1[];
54 extern struct lprocfs_vars status_class_var[];
55
56 inline struct mds_obd *mds_req2mds(struct ptlrpc_request *req)
57 {
58         return &req->rq_export->exp_obd->u.mds;
59 }
60
61 static int mds_bulk_timeout(void *data)
62 {
63         struct ptlrpc_bulk_desc *desc = data;
64
65         ENTRY;
66         CERROR("(not yet) starting recovery of client %p\n", desc->bd_client);
67         RETURN(1);
68 }
69
70 /* Assumes caller has already pushed into the kernel filesystem context */
71 static int mds_sendpage(struct ptlrpc_request *req, struct file *file,
72                         __u64 offset)
73 {
74         int rc = 0;
75         struct mds_obd *mds = mds_req2mds(req);
76         struct ptlrpc_bulk_desc *desc;
77         struct ptlrpc_bulk_page *bulk;
78         struct l_wait_info lwi;
79         char *buf;
80         ENTRY;
81
82         desc = ptlrpc_prep_bulk(req->rq_connection);
83         if (desc == NULL)
84                 GOTO(out, rc = -ENOMEM);
85
86         bulk = ptlrpc_prep_bulk_page(desc);
87         if (bulk == NULL)
88                 GOTO(cleanup_bulk, rc = -ENOMEM);
89
90         OBD_ALLOC(buf, PAGE_SIZE);
91         if (buf == NULL)
92                 GOTO(cleanup_bulk, rc = -ENOMEM);
93
94         rc = mds_fs_readpage(mds, file, buf, PAGE_SIZE, (loff_t *)&offset);
95
96         if (rc != PAGE_SIZE)
97                 GOTO(cleanup_buf, rc = -EIO);
98
99         bulk->bp_xid = req->rq_xid;
100         bulk->bp_buf = buf;
101         bulk->bp_buflen = PAGE_SIZE;
102         desc->bd_portal = MDS_BULK_PORTAL;
103
104         rc = ptlrpc_send_bulk(desc);
105         if (rc)
106                 GOTO(cleanup_buf, rc);
107
108         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE)) {
109                 CERROR("obd_fail_loc=%x, fail operation rc=%d\n",
110                        OBD_FAIL_MDS_SENDPAGE, rc);
111                 ptlrpc_abort_bulk(desc);
112                 GOTO(cleanup_buf, rc);
113         }
114
115         lwi = LWI_TIMEOUT(obd_timeout * HZ, mds_bulk_timeout, desc);
116         rc = l_wait_event(desc->bd_waitq, desc->bd_flags & PTL_BULK_FL_SENT, &lwi);
117         if (rc) {
118                 if (rc != -ETIMEDOUT)
119                         LBUG();
120                 GOTO(cleanup_buf, rc);
121         }
122
123         EXIT;
124  cleanup_buf:
125         OBD_FREE(buf, PAGE_SIZE);
126  cleanup_bulk:
127         ptlrpc_free_bulk(desc);
128  out:
129         return rc;
130 }
131
132 /*
133  * Look up a named entry in a directory, and get an LDLM lock on it.
134  * 'dir' is a inode for which an LDLM lock has already been taken.
135  *
136  * If we do not need an exclusive or write lock on this entry (e.g.
137  * a read lock for attribute lookup only) then we do not hold the
138  * directory on return.  It is up to the caller to know what type
139  * of lock it is getting, and clean up appropriately.
140  */
141 struct dentry *mds_name2locked_dentry(struct obd_device *obd,
142                                       struct dentry *dir, struct vfsmount **mnt,
143                                       char *name, int namelen, int lock_mode,
144                                       struct lustre_handle *lockh,
145                                       int dir_lock_mode)
146 {
147         struct dentry *dchild;
148         int flags = 0, rc;
149         __u64 res_id[3] = {0};
150         ENTRY;
151
152         down(&dir->d_inode->i_sem);
153         dchild = lookup_one_len(name, dir, namelen);
154         if (IS_ERR(dchild)) {
155                 CERROR("child lookup error %ld\n", PTR_ERR(dchild));
156                 up(&dir->d_inode->i_sem);
157                 LBUG();
158                 RETURN(dchild);
159         }
160         if (dir_lock_mode != LCK_EX && dir_lock_mode != LCK_PW) {
161                 up(&dir->d_inode->i_sem);
162                 ldlm_lock_decref(lockh, dir_lock_mode);
163         }
164
165         if (lock_mode == 0 || !dchild->d_inode)
166                 RETURN(dchild);
167
168         res_id[0] = dchild->d_inode->i_ino;
169         res_id[1] = dchild->d_inode->i_generation;
170         rc = ldlm_match_or_enqueue(NULL, NULL, obd->obd_namespace, NULL,
171                                    res_id, LDLM_PLAIN, NULL, 0, lock_mode,
172                                    &flags, ldlm_completion_ast,
173                                    mds_blocking_ast, NULL, 0, lockh);
174         if (rc != ELDLM_OK) {
175                 l_dput(dchild);
176                 up(&dir->d_inode->i_sem);
177                 RETURN(ERR_PTR(-ENOLCK)); /* XXX translate ldlm code */
178         }
179
180         RETURN(dchild);
181 }
182
183 struct dentry *mds_fid2locked_dentry(struct obd_device *obd, struct ll_fid *fid,
184                                      struct vfsmount **mnt, int lock_mode,
185                                      struct lustre_handle *lockh)
186 {
187         struct mds_obd *mds = &obd->u.mds;
188         struct dentry *de = mds_fid2dentry(mds, fid, mnt), *retval = de;
189         int flags = 0, rc;
190         __u64 res_id[3] = {0};
191         ENTRY;
192
193         if (IS_ERR(de))
194                 RETURN(de);
195
196         res_id[0] = de->d_inode->i_ino;
197         res_id[1] = de->d_inode->i_generation;
198         rc = ldlm_match_or_enqueue(NULL, NULL, obd->obd_namespace, NULL,
199                                    res_id, LDLM_PLAIN, NULL, 0, lock_mode,
200                                    &flags, ldlm_completion_ast,
201                                    mds_blocking_ast, NULL, 0, lockh);
202         if (rc != ELDLM_OK) {
203                 l_dput(de);
204                 retval = ERR_PTR(-ENOLCK); /* XXX translate ldlm code */
205         }
206
207         RETURN(retval);
208 }
209
210 #ifndef DCACHE_DISCONNECTED
211 #define DCACHE_DISCONNECTED DCACHE_NFSD_DISCONNECTED
212 #endif
213
214 /* Look up an entry by inode number. */
215 struct dentry *mds_fid2dentry(struct mds_obd *mds, struct ll_fid *fid,
216                               struct vfsmount **mnt)
217 {
218         /* stolen from NFS */
219         struct super_block *sb = mds->mds_sb;
220         unsigned long ino = fid->id;
221         __u32 generation = fid->generation;
222         struct inode *inode;
223         struct list_head *lp;
224         struct dentry *result;
225
226         if (ino == 0)
227                 RETURN(ERR_PTR(-ESTALE));
228
229         inode = iget(sb, ino);
230         if (inode == NULL)
231                 RETURN(ERR_PTR(-ENOMEM));
232
233         CDEBUG(D_DENTRY, "--> mds_fid2dentry: sb %p\n", inode->i_sb);
234
235         if (is_bad_inode(inode) ||
236             (generation && inode->i_generation != generation)) {
237                 /* we didn't find the right inode.. */
238                 CERROR("bad inode %lu, link: %d ct: %d or version  %u/%u\n",
239                        inode->i_ino, inode->i_nlink,
240                        atomic_read(&inode->i_count), inode->i_generation,
241                        generation);
242                 LBUG();
243                 iput(inode);
244                 RETURN(ERR_PTR(-ESTALE));
245         }
246
247         /* now to find a dentry. If possible, get a well-connected one */
248         if (mnt)
249                 *mnt = mds->mds_vfsmnt;
250         spin_lock(&dcache_lock);
251         list_for_each(lp, &inode->i_dentry) {
252                 result = list_entry(lp, struct dentry, d_alias);
253                 if (!(result->d_flags & DCACHE_DISCONNECTED)) {
254                         dget_locked(result);
255                         result->d_vfs_flags |= DCACHE_REFERENCED;
256                         spin_unlock(&dcache_lock);
257                         iput(inode);
258                         if (mnt)
259                                 mntget(*mnt);
260                         return result;
261                 }
262         }
263         spin_unlock(&dcache_lock);
264         result = d_alloc_root(inode);
265         if (result == NULL) {
266                 iput(inode);
267                 return ERR_PTR(-ENOMEM);
268         }
269         if (mnt)
270                 mntget(*mnt);
271         result->d_flags |= DCACHE_DISCONNECTED;
272         return result;
273 }
274
275 /* Establish a connection to the MDS.
276  *
277  * This will set up an export structure for the client to hold state data
278  * about that client, like open files, the last operation number it did
279  * on the server, etc.
280  */
281 static int mds_connect(struct lustre_handle *conn, struct obd_device *obd,
282                        obd_uuid_t cluuid, struct recovd_obd *recovd,
283                        ptlrpc_recovery_cb_t recover)
284 {
285         struct obd_export *exp;
286         struct mds_export_data *med;
287         struct mds_client_data *mcd;
288         struct list_head *p;
289         int rc;
290         ENTRY;
291
292         if (!conn || !obd || !cluuid)
293                 RETURN(-EINVAL);
294
295         MOD_INC_USE_COUNT;
296
297         spin_lock(&obd->obd_dev_lock);
298         list_for_each(p, &obd->obd_exports) {
299                 exp = list_entry(p, struct obd_export, exp_obd_chain);
300                 mcd = exp->exp_mds_data.med_mcd;
301                 if (!memcmp(cluuid, mcd->mcd_uuid, sizeof(mcd->mcd_uuid))) {
302                         LASSERT(exp->exp_obd == obd);
303
304                         if (!list_empty(&exp->exp_conn_chain)) {
305                                 CERROR("existing uuid/export, list not empty!\n");
306                                 spin_unlock(&obd->obd_dev_lock);
307                                 MOD_DEC_USE_COUNT;
308                                 RETURN(-EALREADY);
309                         }
310                         conn->addr = (__u64) (unsigned long)exp;
311                         conn->cookie = exp->exp_cookie;
312                         spin_unlock(&obd->obd_dev_lock);
313                         CDEBUG(D_INFO, "existing export for UUID '%s' at %p\n",
314                                cluuid, exp);
315                         CDEBUG(D_IOCTL,"connect: addr %Lx cookie %Lx\n",
316                                (long long)conn->addr, (long long)conn->cookie);
317                         MOD_DEC_USE_COUNT;
318                         RETURN(0);
319                 }
320         }
321         spin_unlock(&obd->obd_dev_lock);
322         /* XXX There is a small race between checking the list and adding a
323          * new connection for the same UUID, but the real threat (list
324          * corruption when multiple different clients connect) is solved.
325          */
326         rc = class_connect(conn, obd, cluuid);
327         if (rc)
328                 GOTO(out_dec, rc);
329         exp = class_conn2export(conn);
330         LASSERT(exp);
331         med = &exp->exp_mds_data;
332
333         OBD_ALLOC(mcd, sizeof(*mcd));
334         if (!mcd) {
335                 CERROR("mds: out of memory for client data\n");
336                 GOTO(out_export, rc = -ENOMEM);
337         }
338
339         memcpy(mcd->mcd_uuid, cluuid, sizeof(mcd->mcd_uuid));
340         med->med_mcd = mcd;
341
342         INIT_LIST_HEAD(&med->med_open_head);
343         spin_lock_init(&med->med_open_lock);
344
345         rc = mds_client_add(med, -1);
346         if (rc)
347                 GOTO(out_mdc, rc);
348
349         RETURN(0);
350
351 out_mdc:
352         OBD_FREE(mcd, sizeof(*mcd));
353 out_export:
354         class_disconnect(conn);
355 out_dec:
356         MOD_DEC_USE_COUNT;
357
358         return rc;
359 }
360
361 /* Call with med->med_open_lock held, please. */
362 inline int mds_close_mfd(struct mds_file_data *mfd, struct mds_export_data *med)
363 {
364         struct file *file = mfd->mfd_file;
365         LASSERT(file->private_data == mfd);
366
367         list_del(&mfd->mfd_list);
368         mfd->mfd_servercookie = DEAD_HANDLE_MAGIC;
369         kmem_cache_free(mds_file_cache, mfd);
370
371         return filp_close(file, 0);
372 }
373
374 static int mds_disconnect(struct lustre_handle *conn)
375 {
376         struct obd_export *export = class_conn2export(conn);
377         struct list_head *tmp, *n;
378         struct mds_export_data *med = &export->exp_mds_data;
379         int rc;
380         ENTRY;
381
382         /*
383          * Close any open files.
384          */
385         spin_lock(&med->med_open_lock);
386         list_for_each_safe(tmp, n, &med->med_open_head) {
387                 struct mds_file_data *mfd =
388                         list_entry(tmp, struct mds_file_data, mfd_list);
389                 rc = mds_close_mfd(mfd, med);
390                 if (rc) {
391                         /* XXX better diagnostics, with file path and stuff */
392                         CDEBUG(D_INODE, "Error %d closing mfd %p\n", rc, mfd);
393                 }
394         }
395         spin_unlock(&med->med_open_lock);
396
397         ldlm_cancel_locks_for_export(export);
398         mds_client_free(export);
399
400         rc = class_disconnect(conn);
401         if (!rc)
402                 MOD_DEC_USE_COUNT;
403
404         RETURN(rc);
405 }
406
407 /*
408  * XXX This is NOT guaranteed to flush all transactions to disk (even though
409  *     it is equivalent to calling sync()) because it only _starts_ the flush
410  *     and does not wait for completion.  It's better than nothing though.
411  *     What we really want is a mild form of fsync_dev_lockfs(), but it is
412  *     non-standard, or enabling do_sync_supers in ext3, just for this call.
413  */
414 static void mds_fsync_super(struct super_block *sb)
415 {
416         lock_kernel();
417         lock_super(sb);
418         if (sb->s_dirt && sb->s_op && sb->s_op->write_super)
419                 sb->s_op->write_super(sb);
420         unlock_super(sb);
421         unlock_kernel();
422 }
423
424 static int mds_getstatus(struct ptlrpc_request *req)
425 {
426         struct mds_obd *mds = mds_req2mds(req);
427         struct mds_body *body;
428         int rc, size = sizeof(*body);
429         ENTRY;
430
431         rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
432         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_GETSTATUS_PACK)) {
433                 CERROR("mds: out of memory for message: size=%d\n", size);
434                 req->rq_status = -ENOMEM;
435                 RETURN(0);
436         }
437
438         /* Flush any outstanding transactions to disk so the client will
439          * get the latest last_committed value and can drop their local
440          * requests if they have any.  This would be fsync_super() if it
441          * was exported.
442          */
443         mds_fsync_super(mds->mds_sb);
444
445         body = lustre_msg_buf(req->rq_repmsg, 0);
446         memcpy(&body->fid1, &mds->mds_rootfid, sizeof(body->fid1));
447
448         /* the last_committed and last_xid fields are filled in for all
449          * replies already - no need to do so here also.
450          */
451         RETURN(0);
452 }
453
454 static int mds_getlovinfo(struct ptlrpc_request *req)
455 {
456         struct mds_obd *mds = mds_req2mds(req);
457         struct mds_status_req *streq;
458         struct lov_desc *desc;
459         int tgt_count;
460         int rc, size[2] = {sizeof(*desc)};
461         ENTRY;
462
463         streq = lustre_msg_buf(req->rq_reqmsg, 0);
464         streq->flags = NTOH__u32(streq->flags);
465         streq->repbuf = NTOH__u32(streq->repbuf);
466         size[1] = streq->repbuf;
467
468         rc = lustre_pack_msg(2, size, NULL, &req->rq_replen, &req->rq_repmsg);
469         if (rc) {
470                 CERROR("mds: out of memory for message: size=%d\n", size[1]);
471                 req->rq_status = -ENOMEM;
472                 RETURN(0);
473         }
474
475         desc = lustre_msg_buf(req->rq_repmsg, 0);
476         rc = mds_get_lovdesc(mds, desc);
477         if (rc) {
478                 CERROR("mds_get_lovdesc error %d", rc);
479                 req->rq_status = rc;
480                 RETURN(0);
481         }
482
483         tgt_count = le32_to_cpu(desc->ld_tgt_count);
484         if (tgt_count * sizeof(obd_uuid_t) > streq->repbuf) {
485                 CERROR("too many targets, enlarge client buffers\n");
486                 req->rq_status = -ENOSPC;
487                 RETURN(0);
488         }
489
490         /* XXX the MDS should not really know about this */
491         mds->mds_max_mdsize = lov_mds_md_size(tgt_count);
492         rc = mds_get_lovtgts(mds, tgt_count,
493                              lustre_msg_buf(req->rq_repmsg, 1));
494         if (rc) {
495                 CERROR("get_lovtgts error %d\n", rc);
496                 req->rq_status = rc;
497                 RETURN(0);
498         }
499         RETURN(0);
500 }
501
502 int mds_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
503                      void *data, __u32 data_len, int flag)
504 {
505         int do_ast;
506         ENTRY;
507
508         if (flag == LDLM_CB_CANCELING) {
509                 /* Don't need to do anything here. */
510                 RETURN(0);
511         }
512
513         /* XXX layering violation!  -phil */
514         l_lock(&lock->l_resource->lr_namespace->ns_lock);
515         lock->l_flags |= LDLM_FL_CBPENDING;
516         do_ast = (!lock->l_readers && !lock->l_writers);
517         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
518
519         if (do_ast) {
520                 struct lustre_handle lockh;
521                 int rc;
522
523                 LDLM_DEBUG(lock, "already unused, calling ldlm_cli_cancel");
524                 ldlm_lock2handle(lock, &lockh);
525                 rc = ldlm_cli_cancel(&lockh);
526                 if (rc < 0)
527                         CERROR("ldlm_cli_cancel: %d\n", rc);
528         } else
529                 LDLM_DEBUG(lock, "Lock still has references, will be"
530                            "cancelled later");
531         RETURN(0);
532 }
533
534 int mds_pack_md(struct mds_obd *mds, struct ptlrpc_request *req,
535                 int offset, struct mds_body *body, struct inode *inode)
536 {
537         struct lov_mds_md *lmm;
538         int lmm_size = req->rq_repmsg->buflens[offset];
539         int rc;
540
541         if (lmm_size == 0) {
542                 CERROR("no space reserved for inode %u MD\n", inode->i_ino);
543                 RETURN(0);
544         }
545
546         lmm = lustre_msg_buf(req->rq_repmsg, offset);
547
548         /* I don't really like this, but it is a sanity check on the client
549          * MD request.  However, if the client doesn't know how much space
550          * to reserve for the MD, this shouldn't be fatal either...
551          */
552         if (lmm_size > mds->mds_max_mdsize) {
553                 CERROR("Reading MD for inode %u of %d bytes > max %d\n",
554                        inode->i_ino, lmm_size, mds->mds_max_mdsize);
555                 // RETURN(-EINVAL);
556         }
557
558         /* We don't need to store the reply size, because this buffer is
559          * discarded right after unpacking, and the LOV can figure out the
560          * size itself from the ost count.
561          */
562         if ((rc = mds_fs_get_md(mds, inode, lmm, lmm_size)) < 0) {
563                 CDEBUG(D_INFO, "No md for ino %u: rc = %d\n", inode->i_ino, rc);
564         } else {
565                 body->valid |= OBD_MD_FLEASIZE;
566                 rc = 0;
567         }
568
569         return rc;
570 }
571
572 static int mds_getattr_internal(struct mds_obd *mds, struct dentry *dentry,
573                                 struct ptlrpc_request *req,
574                                 struct mds_body *reqbody, int reply_off)
575 {
576         struct mds_body *body;
577         struct inode *inode = dentry->d_inode;
578         int rc = 0;
579         ENTRY;
580
581         if (inode == NULL)
582                 RETURN(-ENOENT);
583
584         body = lustre_msg_buf(req->rq_repmsg, reply_off);
585
586         mds_pack_inode2fid(&body->fid1, inode);
587         mds_pack_inode2body(body, inode);
588
589         if (S_ISREG(inode->i_mode)) {
590                 rc = mds_pack_md(mds, req, reply_off + 1, body, inode);
591         } else if (S_ISLNK(inode->i_mode) && reqbody->valid & OBD_MD_LINKNAME) {
592                 char *symname = lustre_msg_buf(req->rq_repmsg, reply_off + 1);
593                 int len = req->rq_repmsg->buflens[reply_off + 1];
594
595                 rc = inode->i_op->readlink(dentry, symname, len);
596                 if (rc < 0) {
597                         CERROR("readlink failed: %d\n", rc);
598                 } else {
599                         CDEBUG(D_INODE, "read symlink dest %s\n", symname);
600                         body->valid |= OBD_MD_LINKNAME;
601                 }
602         }
603         RETURN(rc);
604 }
605
606 static int mds_getattr_name(int offset, struct ptlrpc_request *req)
607 {
608         struct mds_obd *mds = mds_req2mds(req);
609         struct obd_device *obd = req->rq_export->exp_obd;
610         struct obd_run_ctxt saved;
611         struct mds_body *body;
612         struct dentry *de = NULL, *dchild = NULL;
613         struct inode *dir;
614         struct lustre_handle lockh;
615         char *name;
616         int namelen, flags = 0, lock_mode, rc = 0;
617         struct obd_ucred uc;
618         __u64 res_id[3] = {0, 0, 0};
619         ENTRY;
620
621         LASSERT(!strcmp(req->rq_export->exp_obd->obd_type->typ_name, "mds"));
622
623         if (req->rq_reqmsg->bufcount <= offset + 1) {
624                 LBUG();
625                 GOTO(out_pre_de, rc = -EINVAL);
626         }
627
628         body = lustre_msg_buf(req->rq_reqmsg, offset);
629         name = lustre_msg_buf(req->rq_reqmsg, offset + 1);
630         namelen = req->rq_reqmsg->buflens[offset + 1];
631         /* requests were at offset 2, replies go back at 1 */
632         if (offset)
633                 offset = 1;
634
635         uc.ouc_fsuid = body->fsuid;
636         uc.ouc_fsgid = body->fsgid;
637         uc.ouc_cap = body->capability;
638         push_ctxt(&saved, &mds->mds_ctxt, &uc);
639         de = mds_fid2dentry(mds, &body->fid1, NULL);
640         if (IS_ERR(de)) {
641                 LBUG();
642                 GOTO(out_pre_de, rc = -ESTALE);
643         }
644
645         dir = de->d_inode;
646         CDEBUG(D_INODE, "parent ino %ld, name %*s\n", dir->i_ino,namelen,name);
647
648         lock_mode = LCK_PR;
649         res_id[0] = dir->i_ino;
650         res_id[1] = dir->i_generation;
651
652         rc = ldlm_lock_match(obd->obd_namespace, res_id, LDLM_PLAIN,
653                              NULL, 0, lock_mode, &lockh);
654         if (rc == 0) {
655                 LDLM_DEBUG_NOLOCK("enqueue res "LPU64, res_id[0]);
656                 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, NULL,
657                                       res_id, LDLM_PLAIN, NULL, 0, lock_mode,
658                                       &flags, ldlm_completion_ast,
659                                       mds_blocking_ast, NULL, 0, &lockh);
660                 if (rc != ELDLM_OK) {
661                         CERROR("lock enqueue: err: %d\n", rc);
662                         GOTO(out_create_de, rc = -EIO);
663                 }
664         }
665         ldlm_lock_dump((void *)(unsigned long)lockh.addr);
666
667         down(&dir->i_sem);
668         dchild = lookup_one_len(name, de, namelen - 1);
669         if (IS_ERR(dchild)) {
670                 CDEBUG(D_INODE, "child lookup error %ld\n", PTR_ERR(dchild));
671                 up(&dir->i_sem);
672                 GOTO(out_create_dchild, rc = PTR_ERR(dchild));
673         }
674
675         rc = mds_getattr_internal(mds, dchild, req, body, offset);
676
677         EXIT;
678 out_create_dchild:
679         l_dput(dchild);
680         up(&dir->i_sem);
681         ldlm_lock_decref(&lockh, lock_mode);
682 out_create_de:
683         l_dput(de);
684 out_pre_de:
685         req->rq_status = rc;
686         pop_ctxt(&saved);
687         return 0;
688 }
689
690 static int mds_getattr(int offset, struct ptlrpc_request *req)
691 {
692         struct mds_obd *mds = mds_req2mds(req);
693         struct obd_run_ctxt saved;
694         struct dentry *de;
695         struct inode *inode;
696         struct mds_body *body;
697         struct obd_ucred uc;
698         int rc = 0, size[2] = {sizeof(*body)}, bufcount = 1;
699         ENTRY;
700
701         body = lustre_msg_buf(req->rq_reqmsg, offset);
702         uc.ouc_fsuid = body->fsuid;
703         uc.ouc_fsgid = body->fsgid;
704         uc.ouc_cap = body->capability;
705         push_ctxt(&saved, &mds->mds_ctxt, &uc);
706         de = mds_fid2dentry(mds, &body->fid1, NULL);
707         if (IS_ERR(de)) {
708                 rc = req->rq_status = -ENOENT;
709                 GOTO(out_pop, PTR_ERR(de));
710         }
711
712         inode = de->d_inode;
713         if (S_ISREG(body->fid1.f_type)) {
714                 int rc = mds_fs_get_md(mds, inode, NULL, 0);
715                 CDEBUG(D_INODE, "got %d bytes MD data for inode %u\n",
716                        rc, inode->i_ino);
717                 if (rc < 0) {
718                         if (rc != -ENODATA)
719                                 CERROR("error getting inode %u MD: rc = %d\n",
720                                        inode->i_ino, rc);
721                         size[bufcount] = 0;
722                 } else if (rc < mds->mds_max_mdsize) {
723                         size[bufcount] = 0;
724                         CERROR("MD size %d larger than maximum possible %u\n",
725                                rc, mds->mds_max_mdsize);
726                 } else
727                         size[bufcount] = rc;
728                 bufcount++;
729         } else if (body->valid & OBD_MD_LINKNAME) {
730                 size[bufcount] = MIN(inode->i_size + 1, body->size);
731                 bufcount++;
732                 CDEBUG(D_INODE, "symlink size: %d, reply space: %d\n",
733                        inode->i_size + 1, body->size);
734         }
735
736         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK)) {
737                 CERROR("failed MDS_GETATTR_PACK test\n");
738                 req->rq_status = -ENOMEM;
739                 GOTO(out, rc = -ENOMEM);
740         }
741
742         rc = lustre_pack_msg(bufcount, size, NULL, &req->rq_replen,
743                              &req->rq_repmsg);
744         if (rc) {
745                 CERROR("out of memoryK\n");
746                 req->rq_status = rc;
747                 GOTO(out, rc);
748         }
749
750         req->rq_status = mds_getattr_internal(mds, de, req, body, 0);
751
752 out:
753         l_dput(de);
754 out_pop:
755         pop_ctxt(&saved);
756         RETURN(rc);
757 }
758
759 static int mds_statfs(struct ptlrpc_request *req)
760 {
761         struct mds_obd *mds = mds_req2mds(req);
762         struct obd_statfs *osfs;
763         struct statfs sfs;
764         int rc, size = sizeof(*osfs);
765         ENTRY;
766
767         rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
768         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK)) {
769                 CERROR("mds: statfs lustre_pack_msg failed: rc = %d\n", rc);
770                 GOTO(out, rc);
771         }
772
773         rc = mds_fs_statfs(mds, &sfs);
774         if (rc) {
775                 CERROR("mds: statfs failed: rc %d\n", rc);
776                 GOTO(out, rc);
777         }
778         osfs = lustre_msg_buf(req->rq_repmsg, 0);
779         memset(osfs, 0, size);
780         statfs_pack(osfs, &sfs);
781         obd_statfs_pack(osfs, osfs);
782
783 out:
784         req->rq_status = rc;
785         RETURN(0);
786 }
787
788 static struct mds_file_data *mds_handle2mfd(struct lustre_handle *handle)
789 {
790         struct mds_file_data *mfd = NULL;
791
792         if (!handle || !handle->addr)
793                 RETURN(NULL);
794
795         mfd = (struct mds_file_data *)(unsigned long)(handle->addr);
796         if (!kmem_cache_validate(mds_file_cache, mfd))
797                 RETURN(NULL);
798
799         if (mfd->mfd_servercookie != handle->cookie)
800                 RETURN(NULL);
801
802         return mfd;
803 }
804
805 static int mds_store_md(struct mds_obd *mds, struct ptlrpc_request *req,
806                         int offset, struct mds_body *body, struct inode *inode)
807 {
808         struct lov_mds_md *lmm = lustre_msg_buf(req->rq_reqmsg, offset);
809         int lmm_size = req->rq_reqmsg->buflens[offset];
810         struct obd_run_ctxt saved;
811         struct obd_ucred uc;
812         void *handle;
813         int rc, rc2;
814         ENTRY;
815
816         /* I don't really like this, but it is a sanity check on the client
817          * MD request.
818          */
819         if (lmm_size > mds->mds_max_mdsize) {
820                 CERROR("Saving MD for inode %u of %d bytes > max %d\n",
821                        inode->i_ino, lmm_size, mds->mds_max_mdsize);
822                 //RETURN(-EINVAL);
823         }
824
825         CDEBUG(D_INODE, "storing %d bytes MD for inode %u\n",
826                lmm_size, inode->i_ino);
827         uc.ouc_fsuid = body->fsuid;
828         uc.ouc_fsgid = body->fsgid;
829         uc.ouc_cap = body->capability;
830         push_ctxt(&saved, &mds->mds_ctxt, &uc);
831         handle = mds_fs_start(mds, inode, MDS_FSOP_SETATTR);
832         if (!handle)
833                 GOTO(out_ea, rc = -ENOMEM);
834
835         rc = mds_fs_set_md(mds, inode, handle, lmm, lmm_size);
836         if (!rc)
837                 rc = mds_update_last_rcvd(mds, handle, req);
838
839         rc2 = mds_fs_commit(mds, inode, handle);
840         if (rc2 && !rc)
841                 rc = rc2;
842 out_ea:
843         pop_ctxt(&saved);
844
845         RETURN(rc);
846 }
847
848 static int mds_open(struct ptlrpc_request *req)
849 {
850         struct mds_obd *mds = mds_req2mds(req);
851         struct mds_body *body;
852         struct mds_export_data *med;
853         struct mds_file_data *mfd;
854         struct dentry *de;
855         struct file *file;
856         struct vfsmount *mnt;
857         __u32 flags;
858         struct list_head *tmp;
859         int rc, size = sizeof(*body);
860         ENTRY;
861
862         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OPEN_PACK)) {
863                 CERROR("test case OBD_FAIL_MDS_OPEN_PACK\n");
864                 req->rq_status = -ENOMEM;
865                 RETURN(-ENOMEM);
866         }
867
868         rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
869         if (rc) {
870                 CERROR("mds: pack error: rc = %d\n", rc);
871                 req->rq_status = rc;
872                 RETURN(rc);
873         }
874
875         body = lustre_msg_buf(req->rq_reqmsg, 0);
876
877         /* was this animal open already and the client lost the reply? */
878         /* XXX need some way to detect a reopen, to avoid locked list walks */
879         med = &req->rq_export->exp_mds_data;
880         spin_lock(&med->med_open_lock);
881         list_for_each(tmp, &med->med_open_head) {
882                 mfd = list_entry(tmp, typeof(*mfd), mfd_list);
883                 if (!memcmp(&mfd->mfd_clienthandle, &body->handle,
884                             sizeof(mfd->mfd_clienthandle)) &&
885                     body->fid1.id == mfd->mfd_file->f_dentry->d_inode->i_ino) {
886                         de = mfd->mfd_file->f_dentry;
887                         spin_unlock(&med->med_open_lock);
888                         CERROR("Re opening "LPD64"\n", body->fid1.id);
889                         GOTO(out_pack, rc = 0);
890                 }
891         }
892         spin_unlock(&med->med_open_lock);
893
894         mfd = kmem_cache_alloc(mds_file_cache, GFP_KERNEL);
895         if (!mfd) {
896                 CERROR("mds: out of memory\n");
897                 req->rq_status = -ENOMEM;
898                 RETURN(0);
899         }
900
901         de = mds_fid2dentry(mds, &body->fid1, &mnt);
902         if (IS_ERR(de))
903                 GOTO(out_free, rc = PTR_ERR(de));
904
905         /* check if this inode has seen a delayed object creation */
906         if (lustre_msg_get_op_flags(req->rq_reqmsg) & MDS_OPEN_HAS_EA) {
907                 rc = mds_store_md(mds, req, 1, body, de->d_inode);
908                 if (rc) {
909                         l_dput(de);
910                         mntput(mnt);
911                         GOTO(out_free, rc);
912                 }
913         }
914
915         flags = body->flags;
916         /* dentry_open does a dput(de) and mntput(mnt) on error */
917         file = dentry_open(de, mnt, flags & ~O_DIRECT);
918         if (IS_ERR(file)) {
919                 rc = PTR_ERR(file);
920                 GOTO(out_free, 0);
921         }
922
923         file->private_data = mfd;
924         mfd->mfd_file = file;
925         memcpy(&mfd->mfd_clienthandle, &body->handle, sizeof(body->handle));
926         get_random_bytes(&mfd->mfd_servercookie, sizeof(mfd->mfd_servercookie));
927         spin_lock(&med->med_open_lock);
928         list_add(&mfd->mfd_list, &med->med_open_head);
929         spin_unlock(&med->med_open_lock);
930
931 out_pack:
932         body = lustre_msg_buf(req->rq_repmsg, 0);
933         mds_pack_inode2fid(&body->fid1, de->d_inode);
934         mds_pack_inode2body(body, de->d_inode);
935         body->handle.addr = (__u64)(unsigned long)mfd;
936         body->handle.cookie = mfd->mfd_servercookie;
937         CDEBUG(D_INODE, "llite file "LPX64": addr %p, cookie "LPX64"\n",
938                mfd->mfd_clienthandle.addr, mfd, mfd->mfd_servercookie);
939         RETURN(0);
940
941 out_free:
942         mfd->mfd_servercookie = DEAD_HANDLE_MAGIC;
943         kmem_cache_free(mds_file_cache, mfd);
944         req->rq_status = rc;
945         RETURN(0);
946 }
947
948 static int mds_close(struct ptlrpc_request *req)
949 {
950         struct mds_export_data *med = &req->rq_export->exp_mds_data;
951         struct mds_body *body;
952         struct mds_file_data *mfd;
953         int rc;
954         ENTRY;
955
956         body = lustre_msg_buf(req->rq_reqmsg, 0);
957
958         mfd = mds_handle2mfd(&body->handle);
959         if (!mfd) {
960                 CERROR("no handle for file close "LPD64
961                        ": addr "LPX64", cookie "LPX64"\n",
962                        body->fid1.id, body->handle.addr, body->handle.cookie);
963                 RETURN(-ESTALE);
964         }
965
966         spin_lock(&med->med_open_lock);
967         req->rq_status = mds_close_mfd(mfd, med);
968         spin_unlock(&med->med_open_lock);
969
970         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_CLOSE_PACK)) {
971                 CERROR("test case OBD_FAIL_MDS_CLOSE_PACK\n");
972                 req->rq_status = -ENOMEM;
973                 RETURN(-ENOMEM);
974         }
975
976         rc = lustre_pack_msg(0, NULL, NULL, &req->rq_replen, &req->rq_repmsg);
977         if (rc) {
978                 CERROR("mds: lustre_pack_msg: rc = %d\n", rc);
979                 req->rq_status = rc;
980         }
981
982         RETURN(0);
983 }
984
985 static int mds_readpage(struct ptlrpc_request *req)
986 {
987         struct mds_obd *mds = mds_req2mds(req);
988         struct vfsmount *mnt;
989         struct dentry *de;
990         struct file *file;
991         struct mds_body *body, *repbody;
992         struct obd_run_ctxt saved;
993         int rc, size = sizeof(*body);
994         struct obd_ucred uc;
995         ENTRY;
996
997         rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
998         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK)) {
999                 CERROR("mds: out of memory\n");
1000                 GOTO(out, rc = -ENOMEM);
1001         }
1002
1003         body = lustre_msg_buf(req->rq_reqmsg, 0);
1004         uc.ouc_fsuid = body->fsuid;
1005         uc.ouc_fsgid = body->fsgid;
1006         uc.ouc_cap = body->capability;
1007         push_ctxt(&saved, &mds->mds_ctxt, &uc);
1008         de = mds_fid2dentry(mds, &body->fid1, &mnt);
1009         if (IS_ERR(de))
1010                 GOTO(out_pop, rc = PTR_ERR(de));
1011
1012         CDEBUG(D_INODE, "ino %ld\n", de->d_inode->i_ino);
1013
1014         file = dentry_open(de, mnt, O_RDONLY | O_LARGEFILE);
1015         /* note: in case of an error, dentry_open puts dentry */
1016         if (IS_ERR(file))
1017                 GOTO(out_pop, rc = PTR_ERR(file));
1018
1019         repbody = lustre_msg_buf(req->rq_repmsg, 0);
1020         repbody->size = file->f_dentry->d_inode->i_size;
1021         repbody->valid = OBD_MD_FLSIZE;
1022
1023         /* to make this asynchronous make sure that the handling function
1024            doesn't send a reply when this function completes. Instead a
1025            callback function would send the reply */
1026         /* note: in case of an error, dentry_open puts dentry */
1027         rc = mds_sendpage(req, file, body->size);
1028
1029         filp_close(file, 0);
1030 out_pop:
1031         pop_ctxt(&saved);
1032 out:
1033         req->rq_status = rc;
1034         RETURN(0);
1035 }
1036
1037 int mds_reint(struct ptlrpc_request *req, int offset)
1038 {
1039         int rc;
1040         struct mds_update_record rec;
1041
1042         rc = mds_update_unpack(req, offset, &rec);
1043         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNPACK)) {
1044                 CERROR("invalid record\n");
1045                 req->rq_status = -EINVAL;
1046                 RETURN(0);
1047         }
1048         /* rc will be used to interrupt a for loop over multiple records */
1049         rc = mds_reint_rec(&rec, offset, req);
1050         return rc;
1051 }
1052
1053 int mds_handle(struct ptlrpc_request *req)
1054 {
1055         int rc;
1056         ENTRY;
1057
1058         rc = lustre_unpack_msg(req->rq_reqmsg, req->rq_reqlen);
1059         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_HANDLE_UNPACK)) {
1060                 CERROR("lustre_mds: Invalid request\n");
1061                 GOTO(out, rc);
1062         }
1063
1064         if (req->rq_reqmsg->opc != MDS_CONNECT && req->rq_export == NULL)
1065                 GOTO(out, rc = -ENOTCONN);
1066
1067         LASSERT(!strcmp(req->rq_obd->obd_type->typ_name, LUSTRE_MDT_NAME));
1068
1069         switch (req->rq_reqmsg->opc) {
1070         case MDS_CONNECT:
1071                 CDEBUG(D_INODE, "connect\n");
1072                 OBD_FAIL_RETURN(OBD_FAIL_MDS_CONNECT_NET, 0);
1073                 rc = target_handle_connect(req);
1074                 break;
1075
1076         case MDS_DISCONNECT:
1077                 CDEBUG(D_INODE, "disconnect\n");
1078                 OBD_FAIL_RETURN(OBD_FAIL_MDS_DISCONNECT_NET, 0);
1079                 rc = target_handle_disconnect(req);
1080                 goto out;
1081
1082         case MDS_GETSTATUS:
1083                 CDEBUG(D_INODE, "getstatus\n");
1084                 OBD_FAIL_RETURN(OBD_FAIL_MDS_GETSTATUS_NET, 0);
1085                 rc = mds_getstatus(req);
1086                 break;
1087
1088         case MDS_GETLOVINFO:
1089                 CDEBUG(D_INODE, "getlovinfo\n");
1090                 rc = mds_getlovinfo(req);
1091                 break;
1092
1093         case MDS_GETATTR:
1094                 CDEBUG(D_INODE, "getattr\n");
1095                 OBD_FAIL_RETURN(OBD_FAIL_MDS_GETATTR_NET, 0);
1096                 rc = mds_getattr(0, req);
1097                 break;
1098
1099         case MDS_STATFS:
1100                 CDEBUG(D_INODE, "statfs\n");
1101                 OBD_FAIL_RETURN(OBD_FAIL_MDS_STATFS_NET, 0);
1102                 rc = mds_statfs(req);
1103                 break;
1104
1105         case MDS_READPAGE:
1106                 CDEBUG(D_INODE, "readpage\n");
1107                 OBD_FAIL_RETURN(OBD_FAIL_MDS_READPAGE_NET, 0);
1108                 rc = mds_readpage(req);
1109
1110                 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
1111                         return 0;
1112                 break;
1113
1114         case MDS_REINT: {
1115                 int size = sizeof(struct mds_body);
1116                 CDEBUG(D_INODE, "reint\n");
1117                 OBD_FAIL_RETURN(OBD_FAIL_MDS_REINT_NET, 0);
1118
1119                 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen,
1120                                      &req->rq_repmsg);
1121                 if (rc) {
1122                         req->rq_status = rc;
1123                         break;
1124                 }
1125                 rc = mds_reint(req, 0);
1126                 OBD_FAIL_RETURN(OBD_FAIL_MDS_REINT_NET_REP, 0);
1127                 break;
1128                 }
1129
1130         case MDS_OPEN:
1131                 CDEBUG(D_INODE, "open\n");
1132                 OBD_FAIL_RETURN(OBD_FAIL_MDS_OPEN_NET, 0);
1133                 rc = mds_open(req);
1134                 break;
1135
1136         case MDS_CLOSE:
1137                 CDEBUG(D_INODE, "close\n");
1138                 OBD_FAIL_RETURN(OBD_FAIL_MDS_CLOSE_NET, 0);
1139                 rc = mds_close(req);
1140                 break;
1141
1142         case LDLM_ENQUEUE:
1143                 CDEBUG(D_INODE, "enqueue\n");
1144                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_ENQUEUE, 0);
1145                 rc = ldlm_handle_enqueue(req);
1146                 break;
1147         case LDLM_CONVERT:
1148                 CDEBUG(D_INODE, "convert\n");
1149                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_CONVERT, 0);
1150                 rc = ldlm_handle_convert(req);
1151                 break;
1152         case LDLM_BL_CALLBACK:
1153         case LDLM_CP_CALLBACK:
1154                 CDEBUG(D_INODE, "callback\n");
1155                 CERROR("callbacks should not happen on MDS\n");
1156                 LBUG();
1157                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_BL_CALLBACK, 0);
1158                 break;
1159         default:
1160                 rc = ptlrpc_error(req->rq_svc, req);
1161                 RETURN(rc);
1162         }
1163
1164         EXIT;
1165
1166         if (!rc) {
1167                 struct mds_export_data *med = &req->rq_export->exp_mds_data;
1168                 struct mds_obd *mds = mds_req2mds(req);
1169
1170                 req->rq_repmsg->last_xid =
1171                         HTON__u64(le64_to_cpu(med->med_mcd->mcd_last_xid));
1172                 req->rq_repmsg->last_committed =
1173                         HTON__u64(mds->mds_last_committed);
1174                 CDEBUG(D_INFO, "last_rcvd ~%Lu, last_committed %Lu, xid %d\n",
1175                        (unsigned long long)mds->mds_last_rcvd,
1176                        (unsigned long long)mds->mds_last_committed,
1177                        cpu_to_le32(req->rq_xid));
1178         }
1179  out:
1180         if (rc) {
1181                 CERROR("mds: processing error (opcode %d): %d\n",
1182                        req->rq_reqmsg->opc, rc);
1183                 ptlrpc_error(req->rq_svc, req);
1184         } else {
1185                 CDEBUG(D_NET, "sending reply\n");
1186                 ptlrpc_reply(req->rq_svc, req);
1187         }
1188         return 0;
1189 }
1190
1191 /* Update the server data on disk.  This stores the new mount_count and
1192  * also the last_rcvd value to disk.  If we don't have a clean shutdown,
1193  * then the server last_rcvd value may be less than that of the clients.
1194  * This will alert us that we may need to do client recovery.
1195  *
1196  * Assumes we are already in the server filesystem context.
1197  *
1198  * Also assumes for mds_last_rcvd that we are not modifying it (no locking).
1199  */
1200 static
1201 int mds_update_server_data(struct mds_obd *mds)
1202 {
1203         struct mds_server_data *msd = mds->mds_server_data;
1204         struct file *filp = mds->mds_rcvd_filp;
1205         loff_t off = 0;
1206         int rc;
1207
1208         msd->msd_last_rcvd = cpu_to_le64(mds->mds_last_rcvd);
1209         msd->msd_mount_count = cpu_to_le64(mds->mds_mount_count);
1210
1211         CDEBUG(D_SUPER, "MDS mount_count is %Lu, last_rcvd is %Lu\n",
1212                (unsigned long long)mds->mds_mount_count,
1213                (unsigned long long)mds->mds_last_rcvd);
1214         rc = lustre_fwrite(filp, (char *)msd, sizeof(*msd), &off);
1215         if (rc != sizeof(*msd)) {
1216                 CERROR("error writing MDS server data: rc = %d\n", rc);
1217                 if (rc > 0)
1218                         RETURN(-EIO);
1219                 RETURN(rc);
1220         }
1221 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
1222         rc = fsync_dev(filp->f_dentry->d_inode->i_rdev);
1223 #else
1224         rc = file_fsync(filp,  filp->f_dentry, 1);
1225 #endif
1226         if (rc)
1227                 CERROR("error flushing MDS server data: rc = %d\n", rc);
1228
1229         return 0;
1230 }
1231
1232 /* Do recovery actions for the MDS */
1233 static int mds_recover(struct obd_device *obddev)
1234 {
1235         struct mds_obd *mds = &obddev->u.mds;
1236         struct obd_run_ctxt saved;
1237         int rc;
1238
1239         /* This happens at the end when recovery is complete */
1240         ++mds->mds_mount_count;
1241         push_ctxt(&saved, &mds->mds_ctxt, NULL);
1242         rc = mds_update_server_data(mds);
1243         pop_ctxt(&saved);
1244
1245         return rc;
1246 }
1247
1248 /* mount the file system (secretly) */
1249 static int mds_setup(struct obd_device *obddev, obd_count len, void *buf)
1250 {
1251         struct obd_ioctl_data* data = buf;
1252         struct mds_obd *mds = &obddev->u.mds;
1253         struct vfsmount *mnt;
1254         int rc = 0;
1255         ENTRY;
1256
1257         MOD_INC_USE_COUNT;
1258 #ifdef CONFIG_DEV_RDONLY
1259         dev_clear_rdonly(2);
1260 #endif
1261         if (!data->ioc_inlbuf1 || !data->ioc_inlbuf2)
1262                 GOTO(err_dec, rc = -EINVAL);
1263
1264         mds->mds_fstype = strdup(data->ioc_inlbuf2);
1265
1266         mnt = do_kern_mount(mds->mds_fstype, 0, data->ioc_inlbuf1, NULL);
1267         if (IS_ERR(mnt)) {
1268                 rc = PTR_ERR(mnt);
1269                 CERROR("do_kern_mount failed: rc = %d\n", rc);
1270                 GOTO(err_kfree, rc);
1271         }
1272
1273         CERROR("%s: mnt is %p\n", data->ioc_inlbuf1, mnt);
1274         mds->mds_sb = mnt->mnt_root->d_inode->i_sb;
1275         if (!mds->mds_sb)
1276                 GOTO(err_put, rc = -ENODEV);
1277
1278         spin_lock_init(&mds->mds_last_lock);
1279         mds->mds_max_mdsize = sizeof(struct lov_mds_md);
1280         rc = mds_fs_setup(obddev, mnt);
1281         if (rc) {
1282                 CERROR("MDS filesystem method init failed: rc = %d\n", rc);
1283                 GOTO(err_put, rc);
1284         }
1285
1286         obddev->obd_namespace =
1287                 ldlm_namespace_new("mds_server", LDLM_NAMESPACE_SERVER);
1288         if (obddev->obd_namespace == NULL) {
1289                 mds_cleanup(obddev);
1290                 GOTO(err_fs, rc = -ENOMEM);
1291         }
1292
1293
1294         rc = mds_recover(obddev);
1295         if (rc)
1296                 GOTO(err_fs, rc);
1297
1298         ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
1299                            "mds_ldlm_client", &obddev->obd_ldlm_client);
1300
1301         RETURN(0);
1302
1303 err_fs:
1304         mds_fs_cleanup(obddev);
1305 err_put:
1306         unlock_kernel();
1307         mntput(mds->mds_vfsmnt);
1308         mds->mds_sb = 0;
1309         lock_kernel();
1310 err_kfree:
1311         kfree(mds->mds_fstype);
1312 err_dec:
1313         MOD_DEC_USE_COUNT;
1314         RETURN(rc);
1315 }
1316
1317 static int mds_cleanup(struct obd_device *obddev)
1318 {
1319         struct super_block *sb;
1320         struct mds_obd *mds = &obddev->u.mds;
1321         struct obd_run_ctxt saved;
1322         ENTRY;
1323
1324         sb = mds->mds_sb;
1325         if (!mds->mds_sb)
1326                 RETURN(0);
1327
1328         push_ctxt(&saved, &mds->mds_ctxt, NULL);
1329         mds_update_server_data(mds);
1330
1331         if (mds->mds_rcvd_filp) {
1332                 int rc = filp_close(mds->mds_rcvd_filp, 0);
1333                 mds->mds_rcvd_filp = NULL;
1334
1335                 if (rc)
1336                         CERROR("last_rcvd file won't close, rc=%d\n", rc);
1337         }
1338         pop_ctxt(&saved);
1339
1340         unlock_kernel();
1341         mntput(mds->mds_vfsmnt);
1342         mds->mds_sb = 0;
1343         kfree(mds->mds_fstype);
1344
1345         ldlm_namespace_free(obddev->obd_namespace);
1346
1347         lock_kernel();
1348 #ifdef CONFIG_DEV_RDONLY
1349         dev_clear_rdonly(2);
1350 #endif
1351         mds_fs_cleanup(obddev);
1352
1353         MOD_DEC_USE_COUNT;
1354         RETURN(0);
1355 }
1356
1357 static int ldlm_intent_policy(struct ldlm_lock *lock, void *req_cookie,
1358                               ldlm_mode_t mode, int flags, void *data)
1359 {
1360         struct ptlrpc_request *req = req_cookie;
1361         int rc = 0;
1362         ENTRY;
1363
1364         if (!req_cookie)
1365                 RETURN(0);
1366
1367         if (req->rq_reqmsg->bufcount > 1) {
1368                 /* an intent needs to be considered */
1369                 struct ldlm_intent *it = lustre_msg_buf(req->rq_reqmsg, 1);
1370                 struct mds_obd *mds= &req->rq_export->exp_obd->u.mds;
1371                 struct mds_body *mds_rep;
1372                 struct ldlm_reply *rep;
1373                 __u64 new_resid[3] = {0, 0, 0}, old_res;
1374                 int rc, size[3] = {sizeof(struct ldlm_reply),
1375                                                   sizeof(struct mds_body),
1376                                                   mds->mds_max_mdsize};
1377
1378                 it->opc = NTOH__u64(it->opc);
1379
1380                 LDLM_DEBUG(lock, "intent policy, opc: %s",
1381                            ldlm_it2str(it->opc));
1382
1383                 rc = lustre_pack_msg(3, size, NULL, &req->rq_replen,
1384                                      &req->rq_repmsg);
1385                 if (rc) {
1386                         rc = req->rq_status = -ENOMEM;
1387                         RETURN(rc);
1388                 }
1389
1390                 rep = lustre_msg_buf(req->rq_repmsg, 0);
1391                 rep->lock_policy_res1 = 1;
1392
1393                 /* execute policy */
1394                 switch ((long)it->opc) {
1395                 case IT_CREAT|IT_OPEN:
1396                         rc = mds_reint(req, 2);
1397                         if (rc || (req->rq_status != 0 &&
1398                                    req->rq_status != -EEXIST)) {
1399                                 rep->lock_policy_res2 = req->rq_status;
1400                                 RETURN(ELDLM_LOCK_ABORTED);
1401                         }
1402                         break;
1403                 case IT_CREAT:
1404                 case IT_MKDIR:
1405                 case IT_MKNOD:
1406                 case IT_RENAME2:
1407                 case IT_LINK2:
1408                 case IT_RMDIR:
1409                 case IT_SYMLINK:
1410                 case IT_UNLINK:
1411                         rc = mds_reint(req, 2);
1412                         if (rc || (req->rq_status != 0 &&
1413                                    req->rq_status != -EISDIR &&
1414                                    req->rq_status != -ENOTDIR)) {
1415                                 rep->lock_policy_res2 = req->rq_status;
1416                                 RETURN(ELDLM_LOCK_ABORTED);
1417                         }
1418                         break;
1419                 case IT_GETATTR:
1420                 case IT_LOOKUP:
1421                 case IT_OPEN:
1422                 case IT_READDIR:
1423                 case IT_READLINK:
1424                 case IT_RENAME:
1425                 case IT_LINK:
1426                 case IT_SETATTR:
1427                         rc = mds_getattr_name(2, req);
1428                         /* FIXME: we need to sit down and decide on who should
1429                          * set req->rq_status, who should return negative and
1430                          * positive return values, and what they all mean. */
1431                         if (rc || req->rq_status != 0) {
1432                                 rep->lock_policy_res2 = req->rq_status;
1433                                 RETURN(ELDLM_LOCK_ABORTED);
1434                         }
1435                         break;
1436                 case IT_READDIR|IT_OPEN:
1437                         LBUG();
1438                         break;
1439                 default:
1440                         CERROR("Unhandled intent "LPD64"\n", it->opc);
1441                         LBUG();
1442                 }
1443
1444                 /* We don't bother returning a lock to the client for a file
1445                  * or directory we are removing.
1446                  *
1447                  * As for link and rename, there is no reason for the client
1448                  * to get a lock on the target at this point.  If they are
1449                  * going to modify the file/directory later they will get a
1450                  * lock at that time.
1451                  */
1452                 if (it->opc & (IT_UNLINK | IT_RMDIR | IT_LINK | IT_LINK2 |
1453                                IT_RENAME | IT_RENAME2))
1454                         RETURN(ELDLM_LOCK_ABORTED);
1455
1456                 rep->lock_policy_res2 = req->rq_status;
1457                 mds_rep = lustre_msg_buf(req->rq_repmsg, 1);
1458
1459                 /* If the client is about to open a file that doesn't have an MD
1460                  * stripe record, it's going to need a write lock. */
1461                 if (it->opc & IT_OPEN &&
1462                     !(lustre_msg_get_op_flags(req->rq_reqmsg)&MDS_OPEN_HAS_EA)){
1463                         LDLM_DEBUG(lock, "open with no EA; returning PW lock");
1464                         lock->l_req_mode = LCK_PW;
1465                 }
1466
1467                 if (flags & LDLM_FL_INTENT_ONLY) {
1468                         LDLM_DEBUG(lock, "INTENT_ONLY, aborting lock");
1469                         RETURN(ELDLM_LOCK_ABORTED);
1470                 }
1471                 /* Give the client a lock on the child object, instead of the
1472                  * parent that it requested. */
1473                 new_resid[0] = NTOH__u32(mds_rep->ino);
1474                 new_resid[1] = NTOH__u32(mds_rep->generation);
1475                 if (new_resid[0] == 0)
1476                         LBUG();
1477                 old_res = lock->l_resource->lr_name[0];
1478
1479                 ldlm_lock_change_resource(lock, new_resid);
1480                 if (lock->l_resource == NULL) {
1481                         LBUG();
1482                         RETURN(-ENOMEM);
1483                 }
1484                 LDLM_DEBUG(lock, "intent policy, old res %ld",
1485                            (long)old_res);
1486                 RETURN(ELDLM_LOCK_CHANGED);
1487         } else {
1488                 int size = sizeof(struct ldlm_reply);
1489                 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen,
1490                                      &req->rq_repmsg);
1491                 if (rc) {
1492                         LBUG();
1493                         RETURN(-ENOMEM);
1494                 }
1495         }
1496         RETURN(rc);
1497 }
1498
1499 int mds_attach(struct obd_device *dev, obd_count len, void *data)
1500 {
1501         return lprocfs_reg_obd(dev, status_var_nm_1, dev);
1502 }
1503
1504 int mds_detach(struct obd_device *dev)
1505 {
1506         return lprocfs_dereg_obd(dev);
1507 }
1508
1509 static int mdt_setup(struct obd_device *obddev, obd_count len, void *buf)
1510 {
1511         int i;
1512         //        struct obd_ioctl_data* data = buf;
1513         struct mds_obd *mds = &obddev->u.mds;
1514         int rc = 0;
1515         ENTRY;
1516
1517         MOD_INC_USE_COUNT;
1518
1519         mds->mds_service = ptlrpc_init_svc(MDS_NEVENTS, MDS_NBUFS,
1520                                            MDS_BUFSIZE, MDS_MAXREQSIZE,
1521                                            MDS_REQUEST_PORTAL, MDC_REPLY_PORTAL,
1522                                            "self", mds_handle, "mds");
1523         if (!mds->mds_service) {
1524                 CERROR("failed to start service\n");
1525                 GOTO(err_dec, rc = -EINVAL);
1526         }
1527
1528         for (i = 0; i < MDT_NUM_THREADS; i++) {
1529                 char name[32];
1530                 sprintf(name, "ll_mdt_%02d", i);
1531                 rc = ptlrpc_start_thread(obddev, mds->mds_service, name);
1532                 if (rc) {
1533                         CERROR("cannot start MDT thread #%d: rc %d\n", i, rc);
1534                         GOTO(err_thread, rc);
1535                 }
1536         }
1537
1538         RETURN(0);
1539
1540 err_thread:
1541         ptlrpc_stop_all_threads(mds->mds_service);
1542         ptlrpc_unregister_service(mds->mds_service);
1543 err_dec:
1544         MOD_DEC_USE_COUNT;
1545         RETURN(rc);
1546 }
1547
1548
1549 static int mdt_cleanup(struct obd_device *obddev)
1550 {
1551         struct mds_obd *mds = &obddev->u.mds;
1552         ENTRY;
1553
1554         ptlrpc_stop_all_threads(mds->mds_service);
1555         ptlrpc_unregister_service(mds->mds_service);
1556
1557         MOD_DEC_USE_COUNT;
1558         RETURN(0);
1559 }
1560
1561 extern int mds_iocontrol(long cmd, struct lustre_handle *conn,
1562                          int len, void *karg, void *uarg);
1563
1564 /* use obd ops to offer management infrastructure */
1565 static struct obd_ops mds_obd_ops = {
1566         o_attach:      mds_attach,
1567         o_detach:      mds_detach,
1568         o_connect:     mds_connect,
1569         o_disconnect:  mds_disconnect,
1570         o_setup:       mds_setup,
1571         o_cleanup:     mds_cleanup,
1572         o_iocontrol:   mds_iocontrol
1573 };
1574
1575 static struct obd_ops mdt_obd_ops = {
1576         o_setup:       mdt_setup,
1577         o_cleanup:     mdt_cleanup,
1578 };
1579
1580
1581 static int __init mds_init(void)
1582 {
1583
1584         mds_file_cache = kmem_cache_create("ll_mds_file_data",
1585                                            sizeof(struct mds_file_data),
1586                                            0, 0, NULL, NULL);
1587         if (mds_file_cache == NULL)
1588                 return -ENOMEM;
1589
1590         class_register_type(&mds_obd_ops, status_class_var, LUSTRE_MDS_NAME);
1591         class_register_type(&mdt_obd_ops, 0, LUSTRE_MDT_NAME);
1592         ldlm_register_intent(ldlm_intent_policy);
1593         return 0;
1594
1595 }
1596
1597 static void __exit mds_exit(void)
1598 {
1599
1600
1601         ldlm_unregister_intent();
1602         class_unregister_type(LUSTRE_MDS_NAME);
1603         class_unregister_type(LUSTRE_MDT_NAME);
1604         if (kmem_cache_destroy(mds_file_cache))
1605                 CERROR("couldn't free MDS file cache\n");
1606
1607 }
1608
1609 MODULE_AUTHOR("Cluster File Systems <info@clusterfs.com>");
1610 MODULE_DESCRIPTION("Lustre Metadata Server (MDS) v0.01");
1611 MODULE_LICENSE("GPL");
1612
1613 module_init(mds_init);
1614 module_exit(mds_exit);