Whamcloud - gitweb
Pack inode data into reply for mds_open().
[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         struct obd_export *export = class_conn2export(conn);
337
338         ldlm_cancel_locks_for_export(export);
339         mds_client_free(export);
340
341         rc = class_disconnect(conn);
342         if (!rc)
343                 MOD_DEC_USE_COUNT;
344
345         return rc;
346 }
347
348 static int mds_getstatus(struct ptlrpc_request *req)
349 {
350         struct mds_obd *mds = mds_req2mds(req);
351         struct mds_body *body;
352         int rc, size = sizeof(*body);
353         ENTRY;
354
355         rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
356         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_GETSTATUS_PACK)) {
357                 CERROR("mds: out of memory for message: size=%d\n", size);
358                 req->rq_status = -ENOMEM;
359                 RETURN(0);
360         }
361
362         /* Anything we need to do here with the client's trans no or so? */
363         body = lustre_msg_buf(req->rq_repmsg, 0);
364         memcpy(&body->fid1, &mds->mds_rootfid, sizeof(body->fid1));
365
366         /* the last_committed and last_xid fields are filled in for all
367          * replies already - no need to do so here also.
368          */
369         RETURN(0);
370 }
371
372 static int mds_getlovinfo(struct ptlrpc_request *req)
373 {
374         struct mds_obd *mds = mds_req2mds(req);
375         struct mds_status_req *streq;
376         struct lov_desc *desc;
377         int tgt_count;
378         int rc, size[2] = {sizeof(*desc)};
379         ENTRY;
380
381         streq = lustre_msg_buf(req->rq_reqmsg, 0);
382         streq->flags = NTOH__u32(streq->flags);
383         streq->repbuf = NTOH__u32(streq->repbuf);
384         size[1] = streq->repbuf;
385
386         rc = lustre_pack_msg(2, size, NULL, &req->rq_replen, &req->rq_repmsg);
387         if (rc) {
388                 CERROR("mds: out of memory for message: size=%d\n", size[1]);
389                 req->rq_status = -ENOMEM;
390                 RETURN(0);
391         }
392
393         desc = lustre_msg_buf(req->rq_repmsg, 0);
394         rc = mds_get_lovdesc(req->rq_obd, desc);
395         if (rc != 0 ) {
396                 CERROR("mds_get_lovdesc error %d", rc);
397                 req->rq_status = rc;
398                 RETURN(0);
399         }
400
401         tgt_count = le32_to_cpu(desc->ld_tgt_count);
402         if (tgt_count * sizeof(obd_uuid_t) > streq->repbuf) {
403                 CERROR("too many targets, enlarge client buffers\n");
404                 req->rq_status = -ENOSPC;
405                 RETURN(0);
406         }
407
408         mds->mds_max_mdsize = sizeof(struct lov_mds_md) +
409                 tgt_count * sizeof(struct lov_object_id);
410         rc = mds_get_lovtgts(req->rq_obd, tgt_count,
411                              lustre_msg_buf(req->rq_repmsg, 1));
412         if (rc) {
413                 CERROR("get_lovtgts error %d\n", rc);
414                 req->rq_status = rc;
415                 RETURN(0);
416         }
417         RETURN(0);
418 }
419
420 int mds_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
421                      void *data, __u32 data_len, int flag)
422 {
423         int do_ast;
424         ENTRY;
425
426         if (flag == LDLM_CB_CANCELING) {
427                 /* Don't need to do anything here. */
428                 RETURN(0);
429         }
430
431         l_lock(&lock->l_resource->lr_namespace->ns_lock);
432         lock->l_flags |= LDLM_FL_CBPENDING;
433         do_ast = (!lock->l_readers && !lock->l_writers);
434         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
435
436         if (do_ast) {
437                 struct lustre_handle lockh;
438                 int rc;
439
440                 LDLM_DEBUG(lock, "already unused, calling ldlm_cli_cancel");
441                 ldlm_lock2handle(lock, &lockh);
442                 rc = ldlm_cli_cancel(&lockh);
443                 if (rc < 0)
444                         CERROR("ldlm_cli_cancel: %d\n", rc);
445         } else
446                 LDLM_DEBUG(lock, "Lock still has references, will be"
447                            "cancelled later");
448         RETURN(0);
449 }
450
451 static int mds_getattr_internal(struct mds_obd *mds, struct dentry *dentry,
452                                 struct ptlrpc_request *req,
453                                 struct mds_body *reqbody, int reply_off)
454 {
455         struct mds_body *body;
456         struct inode *inode = dentry->d_inode;
457         int rc;
458         ENTRY;
459
460         if (inode == NULL)
461                 RETURN(-ENOENT);
462
463         body = lustre_msg_buf(req->rq_repmsg, reply_off);
464
465         mds_pack_inode2fid(&body->fid1, inode);
466         mds_pack_inode2body(body, inode);
467
468         if (S_ISREG(inode->i_mode)) {
469                 struct lov_mds_md *lmm;
470
471                 lmm = lustre_msg_buf(req->rq_repmsg, reply_off + 1);
472                 lmm->lmm_easize = mds->mds_max_mdsize;
473                 rc = mds_fs_get_md(mds, inode, lmm);
474
475                 if (rc < 0) {
476                         if (rc == -ENODATA)
477                                 RETURN(0);
478                         CERROR("mds_fs_get_md failed: %d\n", rc);
479                         RETURN(rc);
480                 }
481                 body->valid |= OBD_MD_FLEASIZE;
482         } else if (S_ISLNK(inode->i_mode) && reqbody->valid & OBD_MD_LINKNAME) {
483                 char *symname = lustre_msg_buf(req->rq_repmsg, reply_off + 1);
484                 int len = req->rq_repmsg->buflens[reply_off + 1];
485
486                 rc = inode->i_op->readlink(dentry, symname, len);
487                 if (rc < 0) {
488                         CERROR("readlink failed: %d\n", rc);
489                         RETURN(rc);
490                 } else
491                         CDEBUG(D_INODE, "read symlink dest %s\n", symname);
492
493                 body->valid |= OBD_MD_LINKNAME;
494         }
495         RETURN(0);
496 }
497
498 static int mds_getattr_name(int offset, struct ptlrpc_request *req)
499 {
500         struct mds_obd *mds = mds_req2mds(req);
501         struct obd_device *obd = req->rq_export->exp_obd;
502         struct obd_run_ctxt saved;
503         struct mds_body *body;
504         struct dentry *de = NULL, *dchild = NULL;
505         struct inode *dir;
506         struct lustre_handle lockh;
507         char *name;
508         int namelen, flags, lock_mode, rc = 0;
509         struct obd_ucred uc;
510         __u64 res_id[3] = {0, 0, 0};
511         ENTRY;
512
513         LASSERT(!strcmp(req->rq_export->exp_obd->obd_type->typ_name, "mds"));
514
515         if (req->rq_reqmsg->bufcount <= offset + 1) {
516                 LBUG();
517                 GOTO(out_pre_de, rc = -EINVAL);
518         }
519
520         body = lustre_msg_buf(req->rq_reqmsg, offset);
521         name = lustre_msg_buf(req->rq_reqmsg, offset + 1);
522         namelen = req->rq_reqmsg->buflens[offset + 1];
523         /* requests were at offset 2, replies go back at 1 */
524         if (offset)
525                 offset = 1;
526
527         uc.ouc_fsuid = body->fsuid;
528         uc.ouc_fsgid = body->fsgid;
529         push_ctxt(&saved, &mds->mds_ctxt, &uc);
530         de = mds_fid2dentry(mds, &body->fid1, NULL);
531         if (IS_ERR(de)) {
532                 LBUG();
533                 GOTO(out_pre_de, rc = -ESTALE);
534         }
535
536         dir = de->d_inode;
537         CDEBUG(D_INODE, "parent ino %ld, name %*s\n", dir->i_ino,namelen,name);
538
539         lock_mode = (req->rq_reqmsg->opc == MDS_REINT) ? LCK_CW : LCK_PW;
540         res_id[0] = dir->i_ino;
541
542         rc = ldlm_lock_match(obd->obd_namespace, res_id, LDLM_PLAIN,
543                              NULL, 0, lock_mode, &lockh);
544         if (rc == 0) {
545                 LDLM_DEBUG_NOLOCK("enqueue res "LPU64, res_id[0]);
546                 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, NULL,
547                                       res_id, LDLM_PLAIN, NULL, 0, lock_mode,
548                                       &flags, ldlm_completion_ast,
549                                       mds_blocking_ast, NULL, 0, &lockh);
550                 if (rc != ELDLM_OK) {
551                         CERROR("lock enqueue: err: %d\n", rc);
552                         GOTO(out_create_de, rc = -EIO);
553                 }
554         }
555         ldlm_lock_dump((void *)(unsigned long)lockh.addr);
556
557         down(&dir->i_sem);
558         dchild = lookup_one_len(name, de, namelen - 1);
559         if (IS_ERR(dchild)) {
560                 CDEBUG(D_INODE, "child lookup error %ld\n", PTR_ERR(dchild));
561                 up(&dir->i_sem);
562                 LBUG();
563                 GOTO(out_create_dchild, rc = -ESTALE);
564         }
565
566         rc = mds_getattr_internal(mds, dchild, req, body, offset);
567
568         EXIT;
569 out_create_dchild:
570         l_dput(dchild);
571         up(&dir->i_sem);
572         ldlm_lock_decref(&lockh, lock_mode);
573 out_create_de:
574         l_dput(de);
575 out_pre_de:
576         req->rq_status = rc;
577         pop_ctxt(&saved);
578         return 0;
579 }
580
581 static int mds_getattr(int offset, struct ptlrpc_request *req)
582 {
583         struct mds_obd *mds = mds_req2mds(req);
584         struct obd_run_ctxt saved;
585         struct dentry *de;
586         struct inode *inode;
587         struct mds_body *body;
588         struct obd_ucred uc;
589         int rc = 0, size[2] = {sizeof(*body)}, bufcount = 1;
590         ENTRY;
591
592         body = lustre_msg_buf(req->rq_reqmsg, offset);
593         uc.ouc_fsuid = body->fsuid;
594         uc.ouc_fsgid = body->fsgid;
595         push_ctxt(&saved, &mds->mds_ctxt, &uc);
596         de = mds_fid2dentry(mds, &body->fid1, NULL);
597         if (IS_ERR(de)) {
598                 req->rq_status = -ENOENT;
599                 rc = 0;
600                 GOTO(out_pop, PTR_ERR(de));
601         }
602
603         inode = de->d_inode;
604         if (S_ISREG(body->fid1.f_type)) {
605                 bufcount = 2;
606                 size[1] = mds->mds_max_mdsize;
607         } else if (body->valid & OBD_MD_LINKNAME) {
608                 bufcount = 2;
609                 size[1] = MIN(inode->i_size + 1, body->size);
610                 CDEBUG(D_INODE, "symlink size: %d, reply space: %d\n",
611                        inode->i_size + 1, body->size);
612         }
613
614         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK)) {
615                 CERROR("failed GETATTR_PACK test\n");
616                 req->rq_status = -ENOMEM;
617                 GOTO(out, rc = -ENOMEM);
618         }
619
620         rc = lustre_pack_msg(bufcount, size, NULL, &req->rq_replen,
621                              &req->rq_repmsg);
622         if (rc) {
623                 CERROR("out of memory or FAIL_MDS_GETATTR_PACK\n");
624                 req->rq_status = rc;
625                 GOTO(out, rc);
626         }
627
628         req->rq_status = mds_getattr_internal(mds, de, req, body, 0);
629
630 out:
631         l_dput(de);
632 out_pop:
633         pop_ctxt(&saved);
634         RETURN(rc);
635 }
636
637 static int mds_statfs(struct ptlrpc_request *req)
638 {
639         struct mds_obd *mds = mds_req2mds(req);
640         struct obd_statfs *osfs;
641         struct statfs sfs;
642         int rc, size = sizeof(*osfs);
643         ENTRY;
644
645         rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen,
646                              &req->rq_repmsg);
647         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK)) {
648                 CERROR("mds: statfs lustre_pack_msg failed: rc = %d\n", rc);
649                 GOTO(out, rc);
650         }
651
652         rc = vfs_statfs(mds->mds_sb, &sfs);
653         if (rc) {
654                 CERROR("mds: statfs failed: rc %d\n", rc);
655                 GOTO(out, rc);
656         }
657         osfs = lustre_msg_buf(req->rq_repmsg, 0);
658         memset(osfs, 0, size);
659         statfs_pack(osfs, &sfs);
660         obd_statfs_pack(osfs, osfs);
661
662 out:
663         req->rq_status = rc;
664         RETURN(0);
665 }
666
667 static int mds_open(struct ptlrpc_request *req)
668 {
669         struct dentry *de;
670         struct inode *inode;
671         struct mds_body *body;
672         struct file *file;
673         struct vfsmount *mnt;
674         struct mds_obd *mds = mds_req2mds(req);
675         struct mds_export_data *med;
676         __u32 flags;
677         struct list_head *tmp;
678         struct mds_file_data *mfd;
679         int rc, size = sizeof(*body);
680         ENTRY;
681
682         rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
683         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_OPEN_PACK)) {
684                 CERROR("mds: out of memory\n");
685                 req->rq_status = -ENOMEM;
686                 RETURN(0);
687         }
688
689         body = lustre_msg_buf(req->rq_reqmsg, 0);
690
691         /* was this animal open already? */
692         /* XXX we should only check on re-open, or do a refcount... */
693         med = &req->rq_export->exp_mds_data;
694         list_for_each(tmp, &med->med_open_head) {
695                 struct mds_file_data *fd;
696                 fd = list_entry(tmp, struct mds_file_data, mfd_list);
697                 if (body->extra == fd->mfd_clientfd &&
698                     body->fid1.id == fd->mfd_file->f_dentry->d_inode->i_ino) {
699                         CERROR("Re opening "LPD64"\n", body->fid1.id);
700                         RETURN(0);
701                 }
702         }
703
704         OBD_ALLOC(mfd, sizeof(*mfd));
705         if (!mfd) {
706                 CERROR("mds: out of memory\n");
707                 req->rq_status = -ENOMEM;
708                 RETURN(0);
709         }
710
711         de = mds_fid2dentry(mds, &body->fid1, &mnt);
712         if (IS_ERR(de)) {
713                 req->rq_status = -ENOENT;
714                 RETURN(0);
715         }
716
717         inode = de->d_inode;
718
719         /* check if this inode has seen a delayed object creation */
720         if (req->rq_reqmsg->bufcount > 1) {
721                 void *handle;
722                 struct lov_mds_md *lmm;
723                 struct obd_run_ctxt saved;
724                 struct obd_ucred uc;
725                 int rc, rc2;
726
727                 lmm = lustre_msg_buf(req->rq_reqmsg, 1);
728
729                 uc.ouc_fsuid = body->fsuid;
730                 uc.ouc_fsgid = body->fsgid;
731                 push_ctxt(&saved, &mds->mds_ctxt, &uc);
732                 handle = mds_fs_start(mds, de->d_inode, MDS_FSOP_SETATTR);
733                 if (!handle) {
734                         pop_ctxt(&saved);
735                         GOTO(out_md, rc = -ENOMEM);
736                 }
737
738                 rc = mds_fs_set_md(mds, inode, handle, lmm);
739                 if (!rc)
740                         rc = mds_update_last_rcvd(mds, handle, req);
741
742                 rc2 = mds_fs_commit(mds, inode, handle);
743                 if (rc2 && !rc)
744                         rc = rc2;
745                 pop_ctxt(&saved);
746                 if (rc) {
747 out_md:
748                         req->rq_status = rc;
749                         l_dput(de);
750                         mntput(mnt);
751                         RETURN(0);
752                 }
753         }
754
755         flags = body->flags;
756         file = dentry_open(de, mnt, flags & ~O_DIRECT);
757         if (IS_ERR(file)) {
758                 req->rq_status = PTR_ERR(file);
759                 OBD_FREE(mfd, sizeof(*mfd));
760                 RETURN(0);
761         }
762
763         file->private_data = mfd;
764         mfd->mfd_file = file;
765         mfd->mfd_clientfd = body->extra;
766         list_add(&mfd->mfd_list, &med->med_open_head);
767
768         body = lustre_msg_buf(req->rq_repmsg, 0);
769         mds_pack_inode2fid(&body->fid1, inode);
770         mds_pack_inode2body(body, inode);
771         /* FIXME: need to have cookies involved here */
772         body->extra = (__u64) (unsigned long)file;
773         RETURN(0);
774 }
775
776 static int mds_close(struct ptlrpc_request *req)
777 {
778         struct mds_body *body;
779         struct file *file;
780         struct mds_file_data *mfd;
781         int rc;
782         ENTRY;
783
784         body = lustre_msg_buf(req->rq_reqmsg, 0);
785
786         /* FIXME: need to have cookies involved here */
787         file = (struct file *)(unsigned long)body->extra;
788         if (!file->f_dentry)
789                 LBUG();
790         mfd = (struct mds_file_data *)file->private_data;
791         list_del(&mfd->mfd_list);
792         OBD_FREE(mfd, sizeof(*mfd));
793
794         req->rq_status = filp_close(file, 0);
795
796         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_CLOSE_PACK)) {
797                 CERROR("test case OBD_FAIL_MDS_CLOSE_PACK\n");
798                 req->rq_status = -ENOMEM;
799                 RETURN(-ENOMEM);
800         }
801
802         rc = lustre_pack_msg(0, NULL, NULL, &req->rq_replen, &req->rq_repmsg);
803         if (rc) {
804                 CERROR("mds: lustre_pack_msg: rc = %d\n", rc);
805                 req->rq_status = rc;
806         }
807
808         RETURN(0);
809 }
810
811 static int mds_readpage(struct ptlrpc_request *req)
812 {
813         struct mds_obd *mds = mds_req2mds(req);
814         struct vfsmount *mnt;
815         struct dentry *de;
816         struct file *file;
817         struct mds_body *body, *repbody;
818         struct obd_run_ctxt saved;
819         int rc, size = sizeof(*body);
820         struct obd_ucred uc;
821         ENTRY;
822
823         rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
824         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK)) {
825                 CERROR("mds: out of memory\n");
826                 GOTO(out, rc = -ENOMEM);
827         }
828
829         body = lustre_msg_buf(req->rq_reqmsg, 0);
830         uc.ouc_fsuid = body->fsuid;
831         uc.ouc_fsgid = body->fsgid;
832         push_ctxt(&saved, &mds->mds_ctxt, &uc);
833         de = mds_fid2dentry(mds, &body->fid1, &mnt);
834         if (IS_ERR(de))
835                 GOTO(out_pop, rc = PTR_ERR(de));
836
837         CDEBUG(D_INODE, "ino %ld\n", de->d_inode->i_ino);
838
839         file = dentry_open(de, mnt, O_RDONLY | O_LARGEFILE);
840         /* note: in case of an error, dentry_open puts dentry */
841         if (IS_ERR(file))
842                 GOTO(out_pop, rc = PTR_ERR(file));
843
844         repbody = lustre_msg_buf(req->rq_repmsg, 0);
845         repbody->size = file->f_dentry->d_inode->i_size;
846         repbody->valid = OBD_MD_FLSIZE;
847
848         /* to make this asynchronous make sure that the handling function
849            doesn't send a reply when this function completes. Instead a
850            callback function would send the reply */
851         /* note: in case of an error, dentry_open puts dentry */
852         rc = mds_sendpage(req, file, body->size);
853
854         filp_close(file, 0);
855 out_pop:
856         pop_ctxt(&saved);
857 out:
858         req->rq_status = rc;
859         RETURN(0);
860 }
861
862 int mds_reint(int offset, struct ptlrpc_request *req)
863 {
864         int rc;
865         struct mds_update_record rec;
866
867         rc = mds_update_unpack(req, offset, &rec);
868         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNPACK)) {
869                 CERROR("invalid record\n");
870                 req->rq_status = -EINVAL;
871                 RETURN(0);
872         }
873         /* rc will be used to interrupt a for loop over multiple records */
874         rc = mds_reint_rec(&rec, offset, req);
875         return rc;
876 }
877
878 int mds_handle(struct ptlrpc_request *req)
879 {
880         int rc;
881         ENTRY;
882
883         rc = lustre_unpack_msg(req->rq_reqmsg, req->rq_reqlen);
884         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_HANDLE_UNPACK)) {
885                 CERROR("lustre_mds: Invalid request\n");
886                 GOTO(out, rc);
887         }
888
889         if (req->rq_reqmsg->opc != MDS_CONNECT && req->rq_export == NULL)
890                 GOTO(out, rc = -ENOTCONN);
891
892         if (strcmp(req->rq_obd->obd_type->typ_name, "mds") != 0)
893                 GOTO(out, rc = -EINVAL);
894
895         switch (req->rq_reqmsg->opc) {
896         case MDS_CONNECT:
897                 CDEBUG(D_INODE, "connect\n");
898                 OBD_FAIL_RETURN(OBD_FAIL_MDS_CONNECT_NET, 0);
899                 rc = target_handle_connect(req);
900                 break;
901
902         case MDS_DISCONNECT:
903                 CDEBUG(D_INODE, "disconnect\n");
904                 OBD_FAIL_RETURN(OBD_FAIL_MDS_DISCONNECT_NET, 0);
905                 rc = target_handle_disconnect(req);
906                 goto out;
907
908         case MDS_GETSTATUS:
909                 CDEBUG(D_INODE, "getstatus\n");
910                 OBD_FAIL_RETURN(OBD_FAIL_MDS_GETSTATUS_NET, 0);
911                 rc = mds_getstatus(req);
912                 break;
913
914         case MDS_GETLOVINFO:
915                 CDEBUG(D_INODE, "getlovinfo\n");
916                 rc = mds_getlovinfo(req);
917                 break;
918
919         case MDS_GETATTR:
920                 CDEBUG(D_INODE, "getattr\n");
921                 OBD_FAIL_RETURN(OBD_FAIL_MDS_GETATTR_NET, 0);
922                 rc = mds_getattr(0, req);
923                 break;
924
925         case MDS_STATFS:
926                 CDEBUG(D_INODE, "statfs\n");
927                 OBD_FAIL_RETURN(OBD_FAIL_MDS_STATFS_NET, 0);
928                 rc = mds_statfs(req);
929                 break;
930
931         case MDS_READPAGE:
932                 CDEBUG(D_INODE, "readpage\n");
933                 OBD_FAIL_RETURN(OBD_FAIL_MDS_READPAGE_NET, 0);
934                 rc = mds_readpage(req);
935
936                 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
937                         return 0;
938                 break;
939
940         case MDS_REINT: {
941                 int size = sizeof(struct mds_body);
942                 CDEBUG(D_INODE, "reint\n");
943                 OBD_FAIL_RETURN(OBD_FAIL_MDS_REINT_NET, 0);
944
945                 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen,
946                                      &req->rq_repmsg);
947                 if (rc) {
948                         req->rq_status = rc;
949                         break;
950                 }
951                 rc = mds_reint(0, req);
952                 OBD_FAIL_RETURN(OBD_FAIL_MDS_REINT_NET_REP, 0);
953                 break;
954                 }
955
956         case MDS_OPEN:
957                 CDEBUG(D_INODE, "open\n");
958                 OBD_FAIL_RETURN(OBD_FAIL_MDS_OPEN_NET, 0);
959                 rc = mds_open(req);
960                 break;
961
962         case MDS_CLOSE:
963                 CDEBUG(D_INODE, "close\n");
964                 OBD_FAIL_RETURN(OBD_FAIL_MDS_CLOSE_NET, 0);
965                 rc = mds_close(req);
966                 break;
967
968         case LDLM_ENQUEUE:
969                 CDEBUG(D_INODE, "enqueue\n");
970                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_ENQUEUE, 0);
971                 rc = ldlm_handle_enqueue(req);
972                 if (rc)
973                         break;
974                 RETURN(0);
975
976         case LDLM_CONVERT:
977                 CDEBUG(D_INODE, "convert\n");
978                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_CONVERT, 0);
979                 rc = ldlm_handle_convert(req);
980                 if (rc)
981                         break;
982                 RETURN(0);
983
984         case LDLM_CANCEL:
985                 CDEBUG(D_INODE, "cancel\n");
986                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_CANCEL, 0);
987                 rc = ldlm_handle_cancel(req);
988                 if (rc)
989                         break;
990                 RETURN(0);
991         case LDLM_BL_CALLBACK:
992         case LDLM_CP_CALLBACK:
993                 CDEBUG(D_INODE, "callback\n");
994                 CERROR("callbacks should not happen on MDS\n");
995                 LBUG();
996                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_BL_CALLBACK, 0);
997                 break;
998         default:
999                 rc = ptlrpc_error(req->rq_svc, req);
1000                 RETURN(rc);
1001         }
1002
1003         EXIT;
1004
1005         if (!rc) {
1006                 struct mds_export_data *med = &req->rq_export->exp_mds_data;
1007                 struct mds_obd *mds = mds_req2mds(req);
1008
1009                 req->rq_repmsg->last_xid =
1010                         HTON__u64(le64_to_cpu(med->med_mcd->mcd_last_xid));
1011                 req->rq_repmsg->last_committed =
1012                         HTON__u64(mds->mds_last_committed);
1013                 CDEBUG(D_INFO, "last_rcvd ~%Lu, last_committed %Lu, xid %d\n",
1014                        (unsigned long long)mds->mds_last_rcvd,
1015                        (unsigned long long)mds->mds_last_committed,
1016                        cpu_to_le32(req->rq_xid));
1017         }
1018  out:
1019         if (rc) {
1020                 CERROR("mds: processing error (opcode %d): %d\n",
1021                        req->rq_reqmsg->opc, rc);
1022                 ptlrpc_error(req->rq_svc, req);
1023         } else {
1024                 CDEBUG(D_NET, "sending reply\n");
1025                 ptlrpc_reply(req->rq_svc, req);
1026         }
1027         return 0;
1028 }
1029
1030 /* Update the server data on disk.  This stores the new mount_count and
1031  * also the last_rcvd value to disk.  If we don't have a clean shutdown,
1032  * then the server last_rcvd value may be less than that of the clients.
1033  * This will alert us that we may need to do client recovery.
1034  *
1035  * Assumes we are already in the server filesystem context.
1036  *
1037  * Also assumes for mds_last_rcvd that we are not modifying it (no locking).
1038  */
1039 static
1040 int mds_update_server_data(struct mds_obd *mds)
1041 {
1042         struct mds_server_data *msd = mds->mds_server_data;
1043         struct file *filp = mds->mds_rcvd_filp;
1044         loff_t off = 0;
1045         int rc;
1046
1047         msd->msd_last_rcvd = cpu_to_le64(mds->mds_last_rcvd);
1048         msd->msd_mount_count = cpu_to_le64(mds->mds_mount_count);
1049
1050         CDEBUG(D_SUPER, "MDS mount_count is %Lu, last_rcvd is %Lu\n",
1051                (unsigned long long)mds->mds_mount_count,
1052                (unsigned long long)mds->mds_last_rcvd);
1053         rc = lustre_fwrite(filp, (char *)msd, sizeof(*msd), &off);
1054         if (rc != sizeof(*msd)) {
1055                 CERROR("error writing MDS server data: rc = %d\n", rc);
1056                 if (rc > 0)
1057                         RETURN(-EIO);
1058                 RETURN(rc);
1059         }
1060         rc = fsync_dev(filp->f_dentry->d_inode->i_rdev);
1061         if (rc)
1062                 CERROR("error flushing MDS server data: rc = %d\n", rc);
1063
1064         return 0;
1065 }
1066
1067 /* Do recovery actions for the MDS */
1068 static int mds_recover(struct obd_device *obddev)
1069 {
1070         struct mds_obd *mds = &obddev->u.mds;
1071         struct obd_run_ctxt saved;
1072         int rc;
1073
1074         /* This happens at the end when recovery is complete */
1075         ++mds->mds_mount_count;
1076         push_ctxt(&saved, &mds->mds_ctxt, NULL);
1077         rc = mds_update_server_data(mds);
1078         pop_ctxt(&saved);
1079
1080         return rc;
1081 }
1082
1083 #define MDS_NUM_THREADS 8
1084 /* mount the file system (secretly) */
1085 static int mds_setup(struct obd_device *obddev, obd_count len, void *buf)
1086 {
1087         int i;
1088         struct obd_ioctl_data* data = buf;
1089         struct mds_obd *mds = &obddev->u.mds;
1090         struct vfsmount *mnt;
1091         int rc = 0;
1092         ENTRY;
1093
1094         MOD_INC_USE_COUNT;
1095 #ifdef CONFIG_DEV_RDONLY
1096         dev_clear_rdonly(2);
1097 #endif
1098         if (!data->ioc_inlbuf1 || !data->ioc_inlbuf2)
1099                 GOTO(err_dec, rc = -EINVAL);
1100
1101         mds->mds_fstype = strdup(data->ioc_inlbuf2);
1102
1103         mnt = do_kern_mount(mds->mds_fstype, 0, data->ioc_inlbuf1, NULL);
1104         if (IS_ERR(mnt)) {
1105                 rc = PTR_ERR(mnt);
1106                 CERROR("do_kern_mount failed: rc = %d\n", rc);
1107                 GOTO(err_kfree, rc);
1108         }
1109
1110         mds->mds_sb = mnt->mnt_root->d_inode->i_sb;
1111         if (!mds->mds_sb)
1112                 GOTO(err_put, rc = -ENODEV);
1113
1114         spin_lock_init(&mds->mds_last_lock);
1115         mds->mds_max_mdsize = sizeof(struct lov_mds_md);
1116         rc = mds_fs_setup(obddev, mnt);
1117         if (rc) {
1118                 CERROR("MDS filesystem method init failed: rc = %d\n", rc);
1119                 GOTO(err_put, rc);
1120         }
1121
1122         mds->mds_service = ptlrpc_init_svc(MDS_NEVENTS, MDS_NBUFS,
1123                                            MDS_BUFSIZE, MDS_MAXREQSIZE,
1124                                            MDS_REQUEST_PORTAL, MDC_REPLY_PORTAL,
1125                                            "self", mds_handle, "mds");
1126         if (!mds->mds_service) {
1127                 CERROR("failed to start service\n");
1128                 GOTO(err_fs, rc = -EINVAL);
1129         }
1130
1131         obddev->obd_namespace =
1132                 ldlm_namespace_new("mds_server", LDLM_NAMESPACE_SERVER);
1133         if (obddev->obd_namespace == NULL) {
1134                 mds_cleanup(obddev);
1135                 GOTO(err_svc, rc = -ENOMEM);
1136         }
1137
1138         for (i = 0; i < MDS_NUM_THREADS; i++) {
1139                 char name[32];
1140                 sprintf(name, "lustre_MDS_%02d", i);
1141                 rc = ptlrpc_start_thread(obddev, mds->mds_service, name);
1142                 if (rc) {
1143                         CERROR("cannot start MDS thread #%d: rc %d\n", i, rc);
1144                         GOTO(err_thread, rc);
1145                 }
1146         }
1147
1148         rc = mds_recover(obddev);
1149         if (rc)
1150                 GOTO(err_thread, rc);
1151
1152         ptlrpc_init_client(LDLM_REQUEST_PORTAL, LDLM_REPLY_PORTAL,
1153                            "mds_ldlm_client", &obddev->obd_ldlm_client);
1154
1155         RETURN(0);
1156
1157 err_thread:
1158         ptlrpc_stop_all_threads(mds->mds_service);
1159 err_svc:
1160         ptlrpc_unregister_service(mds->mds_service);
1161 err_fs:
1162         mds_fs_cleanup(obddev);
1163 err_put:
1164         unlock_kernel();
1165         mntput(mds->mds_vfsmnt);
1166         mds->mds_sb = 0;
1167         lock_kernel();
1168 err_kfree:
1169         kfree(mds->mds_fstype);
1170 err_dec:
1171         MOD_DEC_USE_COUNT;
1172         return rc;
1173 }
1174
1175 static int mds_cleanup(struct obd_device *obddev)
1176 {
1177         struct super_block *sb;
1178         struct mds_obd *mds = &obddev->u.mds;
1179         struct obd_run_ctxt saved;
1180         ENTRY;
1181
1182         ptlrpc_stop_all_threads(mds->mds_service);
1183         ptlrpc_unregister_service(mds->mds_service);
1184
1185         sb = mds->mds_sb;
1186         if (!mds->mds_sb)
1187                 RETURN(0);
1188
1189         push_ctxt(&saved, &mds->mds_ctxt, NULL);
1190         mds_update_server_data(mds);
1191
1192         if (mds->mds_rcvd_filp) {
1193                 int rc = filp_close(mds->mds_rcvd_filp, 0);
1194                 mds->mds_rcvd_filp = NULL;
1195
1196                 if (rc)
1197                         CERROR("last_rcvd file won't close, rc=%d\n", rc);
1198         }
1199         pop_ctxt(&saved);
1200
1201         unlock_kernel();
1202         mntput(mds->mds_vfsmnt);
1203         mds->mds_sb = 0;
1204         kfree(mds->mds_fstype);
1205
1206         ldlm_namespace_free(obddev->obd_namespace);
1207
1208         lock_kernel();
1209 #ifdef CONFIG_DEV_RDONLY
1210         dev_clear_rdonly(2);
1211 #endif
1212         mds_fs_cleanup(obddev);
1213
1214         MOD_DEC_USE_COUNT;
1215         RETURN(0);
1216 }
1217
1218 static int ldlm_intent_policy(struct ldlm_lock *lock, void *req_cookie,
1219                               ldlm_mode_t mode, void *data)
1220 {
1221         struct ptlrpc_request *req = req_cookie;
1222         int rc = 0;
1223         ENTRY;
1224
1225         if (!req_cookie)
1226                 RETURN(0);
1227
1228         if (req->rq_reqmsg->bufcount > 1) {
1229                 /* an intent needs to be considered */
1230                 struct ldlm_intent *it = lustre_msg_buf(req->rq_reqmsg, 1);
1231                 struct mds_obd *mds= &req->rq_export->exp_obd->u.mds;
1232                 struct mds_body *mds_rep;
1233                 struct ldlm_reply *rep;
1234                 __u64 new_resid[3] = {0, 0, 0}, old_res;
1235                 int rc, size[3] = {sizeof(struct ldlm_reply),
1236                                                   sizeof(struct mds_body),
1237                                                   mds->mds_max_mdsize};
1238
1239                 it->opc = NTOH__u64(it->opc);
1240
1241                 LDLM_DEBUG(lock, "intent policy, opc: %s",
1242                            ldlm_it2str(it->opc));
1243
1244                 rc = lustre_pack_msg(3, size, NULL, &req->rq_replen,
1245                                      &req->rq_repmsg);
1246                 if (rc) {
1247                         rc = req->rq_status = -ENOMEM;
1248                         RETURN(rc);
1249                 }
1250
1251                 rep = lustre_msg_buf(req->rq_repmsg, 0);
1252                 rep->lock_policy_res1 = 1;
1253
1254                 /* execute policy */
1255                 switch ((long)it->opc) {
1256                 case IT_CREAT|IT_OPEN:
1257                         rc = mds_reint(2, req);
1258                         if (rc || (req->rq_status != 0 &&
1259                                    req->rq_status != -EEXIST)) {
1260                                 rep->lock_policy_res2 = req->rq_status;
1261                                 RETURN(ELDLM_LOCK_ABORTED);
1262                         }
1263                         break;
1264                 case IT_CREAT:
1265                 case IT_MKDIR:
1266                 case IT_MKNOD:
1267                 case IT_RENAME2:
1268                 case IT_LINK2:
1269                 case IT_RMDIR:
1270                 case IT_SYMLINK:
1271                 case IT_UNLINK:
1272                         rc = mds_reint(2, req);
1273                         if (rc || (req->rq_status != 0 &&
1274                                    req->rq_status != -EISDIR &&
1275                                    req->rq_status != -ENOTDIR)) {
1276                                 rep->lock_policy_res2 = req->rq_status;
1277                                 RETURN(ELDLM_LOCK_ABORTED);
1278                         }
1279                         break;
1280                 case IT_GETATTR:
1281                 case IT_LOOKUP:
1282                 case IT_OPEN:
1283                 case IT_READDIR:
1284                 case IT_READLINK:
1285                 case IT_RENAME:
1286                 case IT_LINK:
1287                 case IT_SETATTR:
1288                         rc = mds_getattr_name(2, req);
1289                         /* FIXME: we need to sit down and decide on who should
1290                          * set req->rq_status, who should return negative and
1291                          * positive return values, and what they all mean. */
1292                         if (rc || req->rq_status != 0) {
1293                                 rep->lock_policy_res2 = req->rq_status;
1294                                 RETURN(ELDLM_LOCK_ABORTED);
1295                         }
1296                         break;
1297                 case IT_READDIR|IT_OPEN:
1298                         LBUG();
1299                         break;
1300                 default:
1301                         CERROR("Unhandled intent\n");
1302                         LBUG();
1303                 }
1304
1305                 /* We don't bother returning a lock to the client for a file
1306                  * or directory we are removing.
1307                  *
1308                  * As for link and rename, there is no reason for the client
1309                  * to get a lock on the target at this point.  If they are
1310                  * going to modify the file/directory later they will get a
1311                  * lock at that time.
1312                  */
1313                 if (it->opc & (IT_UNLINK | IT_RMDIR | IT_LINK | IT_LINK2 |
1314                                IT_RENAME | IT_RENAME2))
1315                         RETURN(ELDLM_LOCK_ABORTED);
1316
1317                 rep->lock_policy_res2 = req->rq_status;
1318                 mds_rep = lustre_msg_buf(req->rq_repmsg, 1);
1319
1320                 /* If the client is about to open a file that doesn't have an MD
1321                  * stripe record, it's going to need a write lock. */
1322                 if (it->opc & IT_OPEN) {
1323                         struct lov_mds_md *lmm =
1324                                 lustre_msg_buf(req->rq_repmsg, 2);
1325                         if (lmm->lmm_easize == 0) {
1326                                 LDLM_DEBUG(lock, "open with no EA; returning PW"
1327                                            " lock");
1328                                 lock->l_req_mode = LCK_PW;
1329                         }
1330                 }
1331
1332                 /* Give the client a lock on the child object, instead of the
1333                  * parent that it requested. */
1334                 new_resid[0] = NTOH__u32(mds_rep->ino);
1335                 if (new_resid[0] == 0)
1336                         LBUG();
1337                 old_res = lock->l_resource->lr_name[0];
1338
1339                 ldlm_lock_change_resource(lock, new_resid);
1340                 if (lock->l_resource == NULL) {
1341                         LBUG();
1342                         RETURN(-ENOMEM);
1343                 }
1344                 LDLM_DEBUG(lock, "intent policy, old res %ld",
1345                            (long)old_res);
1346                 RETURN(ELDLM_LOCK_CHANGED);
1347         } else {
1348                 int size = sizeof(struct ldlm_reply);
1349                 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen,
1350                                      &req->rq_repmsg);
1351                 if (rc) {
1352                         LBUG();
1353                         RETURN(-ENOMEM);
1354                 }
1355         }
1356         RETURN(rc);
1357 }
1358
1359
1360 extern int mds_iocontrol(long cmd, struct lustre_handle *conn,
1361                          int len, void *karg, void *uarg);
1362
1363 /* use obd ops to offer management infrastructure */
1364 static struct obd_ops mds_obd_ops = {
1365         o_connect:     mds_connect,
1366         o_disconnect:  mds_disconnect,
1367         o_setup:       mds_setup,
1368         o_cleanup:     mds_cleanup,
1369         o_iocontrol:   mds_iocontrol
1370 };
1371
1372 static int __init mds_init(void)
1373 {
1374         class_register_type(&mds_obd_ops, LUSTRE_MDS_NAME);
1375         ldlm_register_intent(ldlm_intent_policy);
1376         return 0;
1377 }
1378
1379 static void __exit mds_exit(void)
1380 {
1381         ldlm_unregister_intent();
1382         class_unregister_type(LUSTRE_MDS_NAME);
1383 }
1384
1385 MODULE_AUTHOR("Cluster File Systems <info@clusterfs.com>");
1386 MODULE_DESCRIPTION("Lustre Metadata Server (MDS) v0.01");
1387 MODULE_LICENSE("GPL");
1388
1389 module_init(mds_init);
1390 module_exit(mds_exit);