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