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