Whamcloud - gitweb
Change internal statfs calls to pass struct obd_statfs as a parameter
[fs/lustre-release.git] / lustre / mdc / mdc_request.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Copyright (C) 2001, 2002 Cluster File Systems, Inc.
5  *
6  *   This file is part of Lustre, http://www.sf.net/projects/lustre/
7  *
8  *   Lustre is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Lustre is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Lustre; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  */
22
23 #define EXPORT_SYMTAB
24 #define DEBUG_SUBSYSTEM S_MDC
25
26 #include <linux/module.h>
27 #include <linux/miscdevice.h>
28 #include <linux/lustre_mds.h>
29 #include <linux/lustre_lite.h>
30 #include <linux/lustre_dlm.h>
31 #include <linux/init.h>
32 #include <linux/obd_lov.h>
33
34 #define REQUEST_MINOR 244
35
36 extern int mds_queue_req(struct ptlrpc_request *);
37
38 int mdc_getstatus(struct lustre_handle *conn, struct ll_fid *rootfid,
39                   __u64 *last_committed, __u64 *last_xid,
40                   struct ptlrpc_request **request)
41 {
42         struct ptlrpc_request *req;
43         struct mds_body *body;
44         int rc, size = sizeof(*body);
45         ENTRY;
46
47         req = ptlrpc_prep_req(class_conn2cliimp(conn), MDS_GETSTATUS, 1, &size,
48                               NULL);
49         if (!req)
50                 GOTO(out, rc = -ENOMEM);
51
52         body = lustre_msg_buf(req->rq_reqmsg, 0);
53         req->rq_level = LUSTRE_CONN_CON;
54         req->rq_replen = lustre_msg_size(1, &size);
55
56         mds_pack_req_body(req);
57         rc = ptlrpc_queue_wait(req);
58         rc = ptlrpc_check_status(req, rc);
59
60         if (!rc) {
61                 body = lustre_msg_buf(req->rq_repmsg, 0);
62                 mds_unpack_body(body);
63                 memcpy(rootfid, &body->fid1, sizeof(*rootfid));
64                 *last_committed = req->rq_repmsg->last_committed;
65                 *last_xid = req->rq_repmsg->last_xid;
66
67                 CDEBUG(D_NET,"root ino=%ld, last_committed=%Lu, last_xid=%Ld\n",
68                        (unsigned long)rootfid->id,
69                        (unsigned long long)*last_committed,
70                        (unsigned long long)*last_xid);
71         }
72
73         EXIT;
74  out:
75         ptlrpc_free_req(req);
76         return rc;
77 }
78
79 int mdc_getlovinfo(struct obd_device *obd, struct lustre_handle *mdc_connh,
80                    struct ptlrpc_request **request)
81 {
82         struct ptlrpc_request *req;
83         struct mds_status_req *streq;
84         int rc, size[2] = {sizeof(*streq)};
85         ENTRY;
86
87         req = ptlrpc_prep_req(class_conn2cliimp(mdc_connh), MDS_GETLOVINFO, 1,
88                               size, NULL);
89         if (!req)
90                 GOTO(out, rc = -ENOMEM);
91
92         *request = req;
93         streq = lustre_msg_buf(req->rq_reqmsg, 0);
94         streq->flags = HTON__u32(MDS_STATUS_LOV);
95         streq->repbuf = HTON__u32(8192);
96
97         /* prepare for reply */
98         req->rq_level = LUSTRE_CONN_CON;
99         size[0] = 512;
100         size[1] = 8192;
101         req->rq_replen = lustre_msg_size(2, size);
102
103         rc = ptlrpc_queue_wait(req);
104         rc = ptlrpc_check_status(req, rc);
105
106  out:
107         RETURN(rc);
108 }
109
110
111 int mdc_getattr(struct lustre_handle *conn,
112                 obd_id ino, int type, unsigned long valid, size_t ea_size,
113                 struct ptlrpc_request **request)
114 {
115         struct ptlrpc_request *req;
116         struct mds_body *body;
117         int rc, size[2] = {sizeof(*body), 0}, bufcount = 1;
118         ENTRY;
119
120         req = ptlrpc_prep_req(class_conn2cliimp(conn), MDS_GETATTR, 1, size,
121                               NULL);
122         if (!req)
123                 GOTO(out, rc = -ENOMEM);
124
125         body = lustre_msg_buf(req->rq_reqmsg, 0);
126         ll_ino2fid(&body->fid1, ino, 0, type);
127         body->valid = valid;
128
129         if (S_ISREG(type)) {
130                 struct client_obd *mdc = &class_conn2obd(conn)->u.cli;
131                 bufcount = 2;
132                 size[1] = mdc->cl_max_mdsize;
133         } else if (valid & OBD_MD_LINKNAME) {
134                 bufcount = 2;
135                 size[1] = ea_size;
136         }
137         req->rq_replen = lustre_msg_size(bufcount, size);
138
139         rc = ptlrpc_queue_wait(req);
140         rc = ptlrpc_check_status(req, rc);
141
142         if (!rc) {
143                 body = lustre_msg_buf(req->rq_repmsg, 0);
144                 mds_unpack_body(body);
145                 CDEBUG(D_NET, "mode: %o\n", body->mode);
146         }
147
148         EXIT;
149  out:
150         *request = req;
151         return rc;
152 }
153
154 static int mdc_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
155                             void *data, __u32 data_len)
156 {
157         int rc;
158         struct inode *inode = data;
159         struct lustre_handle lockh;
160         ENTRY;
161
162         if (data_len != sizeof(*inode)) {
163                 CERROR("data_len should be %d, but is %d\n", sizeof(*inode),
164                        data_len);
165                 LBUG();
166                 RETURN(-EINVAL);
167         }
168
169         /* FIXME: do something better than throwing away everything */
170         if (inode == NULL)
171                 LBUG();
172         if (S_ISDIR(inode->i_mode)) {
173                 CDEBUG(D_INODE, "invalidating inode %ld\n", inode->i_ino);
174                 invalidate_inode_pages(inode);
175         }
176
177         ldlm_lock2handle(lock, &lockh);
178         rc = ldlm_cli_cancel(&lockh);
179         if (rc < 0) {
180                 CERROR("ldlm_cli_cancel: %d\n", rc);
181                 LBUG();
182         }
183         RETURN(0);
184 }
185
186 int mdc_enqueue(struct lustre_handle *conn, int lock_type,
187                 struct lookup_intent *it, int lock_mode, struct inode *dir,
188                 struct dentry *de, struct lustre_handle *lockh,
189                 char *tgt, int tgtlen, void *data, int datalen)
190 {
191         struct ptlrpc_request *req;
192         struct obd_device *obddev = class_conn2obd(conn);
193         __u64 res_id[RES_NAME_SIZE] = {dir->i_ino};
194         int size[5] = {sizeof(struct ldlm_request), sizeof(struct ldlm_intent)};
195         int rc, flags;
196         int repsize[3] = {sizeof(struct ldlm_reply),
197                           sizeof(struct mds_body),
198                           obddev->u.cli.cl_max_mdsize};
199         struct ldlm_reply *dlm_rep;
200         struct ldlm_intent *lit;
201         ENTRY;
202
203         LDLM_DEBUG_NOLOCK("mdsintent %s dir %ld", ldlm_it2str(it->it_op),
204                           dir->i_ino);
205
206         switch (it->it_op) {
207         case IT_MKDIR:
208                 it->it_mode = (it->it_mode | S_IFDIR) & ~current->fs->umask;
209                 break;
210         case (IT_CREAT|IT_OPEN):
211         case IT_CREAT:
212                 it->it_mode |= S_IFREG; /* no break */
213         case IT_MKNOD:
214                 it->it_mode &= ~current->fs->umask;
215                 break;
216         case IT_SYMLINK:
217                 it->it_mode = (it->it_mode | S_IFLNK) & ~current->fs->umask;
218                 break;
219         }
220
221         if (it->it_op & (IT_MKDIR | IT_CREAT | IT_SYMLINK | IT_MKNOD)) {
222                 size[2] = sizeof(struct mds_rec_create);
223                 size[3] = de->d_name.len + 1;
224                 size[4] = tgtlen + 1;
225                 req = ptlrpc_prep_req(class_conn2cliimp(conn), LDLM_ENQUEUE, 5,
226                                       size, NULL);
227                 if (!req)
228                         RETURN(-ENOMEM);
229
230                 /* pack the intent */
231                 lit = lustre_msg_buf(req->rq_reqmsg, 1);
232                 lit->opc = NTOH__u64((__u64)it->it_op);
233
234                 /* pack the intended request */
235                 mds_create_pack(req, 2, dir, it->it_mode, 0, current->fsuid,
236                                 current->fsgid, CURRENT_TIME, de->d_name.name,
237                                 de->d_name.len, tgt, tgtlen);
238                 req->rq_replen = lustre_msg_size(3, repsize);
239         } else if (it->it_op == IT_RENAME2) {
240                 struct dentry *old_de = it->it_data;
241
242                 size[2] = sizeof(struct mds_rec_rename);
243                 size[3] = old_de->d_name.len + 1;
244                 size[4] = de->d_name.len + 1;
245                 req = ptlrpc_prep_req(class_conn2cliimp(conn), LDLM_ENQUEUE, 5,
246                                       size, NULL);
247                 if (!req)
248                         RETURN(-ENOMEM);
249
250                 /* pack the intent */
251                 lit = lustre_msg_buf(req->rq_reqmsg, 1);
252                 lit->opc = NTOH__u64((__u64)it->it_op);
253
254                 /* pack the intended request */
255                 mds_rename_pack(req, 2, old_de->d_parent->d_inode, dir,
256                                 old_de->d_name.name, old_de->d_name.len,
257                                 de->d_name.name, de->d_name.len);
258                 req->rq_replen = lustre_msg_size(3, repsize);
259         } else if (it->it_op == IT_UNLINK || it->it_op == IT_RMDIR) {
260                 size[2] = sizeof(struct mds_rec_unlink);
261                 size[3] = de->d_name.len + 1;
262                 req = ptlrpc_prep_req(class_conn2cliimp(conn), LDLM_ENQUEUE, 4,
263                                       size, NULL);
264                 if (!req)
265                         RETURN(-ENOMEM);
266
267                 /* pack the intent */
268                 lit = lustre_msg_buf(req->rq_reqmsg, 1);
269                 lit->opc = NTOH__u64((__u64)it->it_op);
270
271                 /* pack the intended request */
272                 mds_unlink_pack(req, 2, dir, NULL,
273                                 it->it_op == IT_UNLINK ? S_IFREG : S_IFDIR,
274                                 de->d_name.name, de->d_name.len);
275
276                 req->rq_replen = lustre_msg_size(3, repsize);
277         } else if (it->it_op == IT_GETATTR || it->it_op == IT_RENAME ||
278                    it->it_op == IT_OPEN || it->it_op == IT_SETATTR ||
279                    it->it_op == IT_LOOKUP || it->it_op == IT_READLINK) {
280                 size[2] = sizeof(struct mds_body);
281                 size[3] = de->d_name.len + 1;
282
283                 req = ptlrpc_prep_req(class_conn2cliimp(conn), LDLM_ENQUEUE, 4,
284                                       size, NULL);
285                 if (!req)
286                         RETURN(-ENOMEM);
287
288                 /* pack the intent */
289                 lit = lustre_msg_buf(req->rq_reqmsg, 1);
290                 lit->opc = NTOH__u64((__u64)it->it_op);
291
292                 /* pack the intended request */
293                 mds_getattr_pack(req, 2, dir, de->d_name.name, de->d_name.len);
294
295                 /* we need to replay opens */
296                 if (it->it_op == IT_OPEN)
297                         req->rq_flags |= PTL_RPC_FL_REPLAY;
298
299                 /* get ready for the reply */
300                 req->rq_replen = lustre_msg_size(3, repsize);
301         } else if (it->it_op == IT_READDIR) {
302                 req = ptlrpc_prep_req(class_conn2cliimp(conn), LDLM_ENQUEUE, 1,
303                                       size, NULL);
304                 if (!req)
305                         RETURN(-ENOMEM);
306
307                 /* get ready for the reply */
308                 req->rq_replen = lustre_msg_size(1, repsize);
309         } else {
310                 LBUG();
311                 RETURN(-EINVAL);
312         }
313 #warning FIXME: the data here needs to be different if a lock was granted for a different inode
314         rc = ldlm_cli_enqueue(conn, req, obddev->obd_namespace, NULL, res_id,
315                               lock_type, NULL, 0, lock_mode, &flags,
316                               ldlm_completion_ast, mdc_blocking_ast, data,
317                               datalen, lockh);
318         if (rc == -ENOENT) {
319                 /* This can go when we're sure that this can never happen */
320                 LBUG();
321         }
322         if (rc == ELDLM_LOCK_ABORTED) {
323                 lock_mode = 0;
324                 memset(lockh, 0, sizeof(*lockh));
325                 /* rc = 0 */
326         } else if (rc != 0) {
327                 CERROR("ldlm_cli_enqueue: %d\n", rc);
328                 RETURN(rc);
329         }
330
331         dlm_rep = lustre_msg_buf(req->rq_repmsg, 0);
332         it->it_disposition = (int) dlm_rep->lock_policy_res1;
333         it->it_status = (int) dlm_rep->lock_policy_res2;
334         it->it_lock_mode = lock_mode;
335         it->it_data = req;
336
337         RETURN(0);
338 }
339
340 int mdc_open(struct lustre_handle *conn, obd_id ino, int type, int flags,
341              struct lov_stripe_md *smd, __u64 cookie, __u64 *fh,
342              struct ptlrpc_request **request)
343 {
344         struct mds_body *body;
345         int rc, size[2] = {sizeof(*body)}, bufcount = 1;
346         struct ptlrpc_request *req;
347         ENTRY;
348
349         if (smd != NULL) {
350                 bufcount = 2;
351                 size[1] = smd->lmd_easize;
352         }
353
354         req = ptlrpc_prep_req(class_conn2cliimp(conn), MDS_OPEN, bufcount, size,
355                               NULL);
356         if (!req)
357                 GOTO(out, rc = -ENOMEM);
358
359         req->rq_flags |= PTL_RPC_FL_REPLAY;
360         body = lustre_msg_buf(req->rq_reqmsg, 0);
361
362         ll_ino2fid(&body->fid1, ino, 0, type);
363         body->flags = HTON__u32(flags);
364         body->extra = cookie;
365
366         if (smd != NULL)
367                 lov_packmd(lustre_msg_buf(req->rq_reqmsg, 1), smd);
368
369         req->rq_replen = lustre_msg_size(1, size);
370
371         rc = ptlrpc_queue_wait(req);
372         rc = ptlrpc_check_status(req, rc);
373         if (!rc) {
374                 body = lustre_msg_buf(req->rq_repmsg, 0);
375                 mds_unpack_body(body);
376                 *fh = body->extra;
377         }
378
379         EXIT;
380  out:
381         *request = req;
382         return rc;
383 }
384
385 int mdc_close(struct lustre_handle *conn,
386               obd_id ino, int type, __u64 fh, struct ptlrpc_request **request)
387 {
388         struct mds_body *body;
389         int rc, size = sizeof(*body);
390         struct ptlrpc_request *req;
391
392         req = ptlrpc_prep_req(class_conn2cliimp(conn), MDS_CLOSE, 1, &size,
393                               NULL);
394         if (!req)
395                 GOTO(out, rc = -ENOMEM);
396
397         body = lustre_msg_buf(req->rq_reqmsg, 0);
398         ll_ino2fid(&body->fid1, ino, 0, type);
399         body->extra = fh;
400
401         req->rq_replen = lustre_msg_size(0, NULL);
402
403         rc = ptlrpc_queue_wait(req);
404         rc = ptlrpc_check_status(req, rc);
405
406         EXIT;
407  out:
408         *request = req;
409         return rc;
410 }
411
412 int mdc_readpage(struct lustre_handle *conn, obd_id ino, int type, __u64 offset,
413                  char *addr, struct ptlrpc_request **request)
414 {
415         struct ptlrpc_connection *connection = 
416                 client_conn2cli(conn)->cl_import.imp_connection;
417         struct ptlrpc_request *req = NULL;
418         struct ptlrpc_bulk_desc *desc = NULL;
419         struct ptlrpc_bulk_page *bulk = NULL;
420         struct mds_body *body;
421         int rc, size = sizeof(*body);
422         ENTRY;
423
424         CDEBUG(D_INODE, "inode: %ld\n", (long)ino);
425
426         desc = ptlrpc_prep_bulk(connection);
427         if (desc == NULL)
428                 GOTO(out, rc = -ENOMEM);
429
430         req = ptlrpc_prep_req(class_conn2cliimp(conn), MDS_READPAGE, 1, &size,
431                               NULL);
432         if (!req)
433                 GOTO(out2, rc = -ENOMEM);
434
435         bulk = ptlrpc_prep_bulk_page(desc);
436         bulk->bp_buflen = PAGE_SIZE;
437         bulk->bp_buf = addr;
438         bulk->bp_xid = req->rq_xid;
439         desc->bd_portal = MDS_BULK_PORTAL;
440
441         rc = ptlrpc_register_bulk(desc);
442         if (rc) {
443                 CERROR("couldn't setup bulk sink: error %d.\n", rc);
444                 GOTO(out2, rc);
445         }
446
447         body = lustre_msg_buf(req->rq_reqmsg, 0);
448         body->fid1.id = ino;
449         body->fid1.f_type = type;
450         body->size = offset;
451
452         req->rq_replen = lustre_msg_size(1, &size);
453         rc = ptlrpc_queue_wait(req);
454         rc = ptlrpc_check_status(req, rc);
455         if (rc) {
456                 ptlrpc_abort_bulk(desc);
457                 GOTO(out2, rc);
458         } else {
459                 body = lustre_msg_buf(req->rq_repmsg, 0);
460                 mds_unpack_body(body);
461         }
462
463         EXIT;
464  out2:
465         ptlrpc_free_bulk(desc);
466  out:
467         *request = req;
468         return rc;
469 }
470
471 int mdc_statfs(struct lustre_handle *conn, struct obd_statfs *osfs,
472                struct ptlrpc_request **request)
473 {
474         struct ptlrpc_request *req;
475         int rc, size = sizeof(*osfs);
476         ENTRY;
477
478         req = ptlrpc_prep_req(class_conn2cliimp(conn), MDS_STATFS, 0, NULL,
479                               NULL);
480         if (!req)
481                 GOTO(out, rc = -ENOMEM);
482         req->rq_replen = lustre_msg_size(1, &size);
483
484         rc = ptlrpc_queue_wait(req);
485         rc = ptlrpc_check_status(req, rc);
486
487         if (rc)
488                 GOTO(out, rc);
489
490         obd_statfs_unpack(osfs, lustre_msg_buf(req->rq_repmsg, 0));
491
492         EXIT;
493 out:
494         *request = req;
495
496         return rc;
497 }
498
499 struct obd_ops mdc_obd_ops = {
500         o_setup:   client_obd_setup,
501         o_cleanup: client_obd_cleanup,
502         o_connect: client_obd_connect,
503         o_disconnect: client_obd_disconnect,
504 };
505
506 static int __init ptlrpc_request_init(void)
507 {
508         return class_register_type(&mdc_obd_ops, LUSTRE_MDC_NAME);
509 }
510
511 static void __exit ptlrpc_request_exit(void)
512 {
513         class_unregister_type(LUSTRE_MDC_NAME);
514 }
515
516 MODULE_AUTHOR("Cluster File Systems <info@clusterfs.com>");
517 MODULE_DESCRIPTION("Lustre Metadata Client v1.0");
518 MODULE_LICENSE("GPL");
519
520 EXPORT_SYMBOL(mdc_getstatus);
521 EXPORT_SYMBOL(mdc_getlovinfo);
522 EXPORT_SYMBOL(mdc_enqueue);
523 EXPORT_SYMBOL(mdc_getattr);
524 EXPORT_SYMBOL(mdc_statfs);
525 EXPORT_SYMBOL(mdc_create);
526 EXPORT_SYMBOL(mdc_unlink);
527 EXPORT_SYMBOL(mdc_rename);
528 EXPORT_SYMBOL(mdc_link);
529 EXPORT_SYMBOL(mdc_readpage);
530 EXPORT_SYMBOL(mdc_setattr);
531 EXPORT_SYMBOL(mdc_close);
532 EXPORT_SYMBOL(mdc_open);
533
534 module_init(ptlrpc_request_init);
535 module_exit(ptlrpc_request_exit);