Whamcloud - gitweb
landing b_cmobd_merge on HEAD
[fs/lustre-release.git] / lustre / mdc / mdc_reint.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Copyright (C) 2001-2003 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 #ifndef EXPORT_SYMTAB
23 # define EXPORT_SYMTAB
24 #endif
25 #define DEBUG_SUBSYSTEM S_MDC
26
27 #ifdef __KERNEL__
28 # include <linux/config.h>
29 # include <linux/module.h>
30 # include <linux/kernel.h>
31 #else
32 # include <liblustre.h>
33 #endif
34
35 #include <linux/obd_class.h>
36 #include <linux/lustre_mds.h>
37 #include "mdc_internal.h"
38
39 /* mdc_setattr does its own semaphore handling */
40 int mdc_reint(struct ptlrpc_request *request,
41               struct mdc_rpc_lock *rpc_lock, int level)
42 {
43         int rc;
44
45         request->rq_send_state = level;
46        
47         if (rpc_lock)
48                 mdc_get_rpc_lock(rpc_lock, NULL);
49        
50         rc = ptlrpc_queue_wait(request);
51         
52         if (rpc_lock)
53                 mdc_put_rpc_lock(rpc_lock, NULL);
54         if (rc)
55                 CDEBUG(D_INFO, "error in handling %d\n", rc);
56         else if (!lustre_swab_repbuf(request, 0, sizeof(struct mds_body),
57                                      lustre_swab_mds_body)) {
58                 CERROR ("Can't unpack mds_body\n");
59                 rc = -EPROTO;
60         }
61         return rc;
62 }
63 EXPORT_SYMBOL(mdc_reint);
64 /* If mdc_setattr is called with an 'iattr', then it is a normal RPC that
65  * should take the normal semaphore and go to the normal portal.
66  *
67  * If it is called with iattr->ia_valid & ATTR_FROM_OPEN, then it is a
68  * magic open-path setattr that should take the setattr semaphore and
69  * go to the setattr portal. */
70 int mdc_setattr(struct obd_export *exp, struct mdc_op_data *data,
71                 struct iattr *iattr, void *ea, int ealen, void *ea2, int ea2len,
72                 struct ptlrpc_request **request)
73 {
74         struct ptlrpc_request *req;
75         struct mds_rec_setattr *rec;
76         struct mdc_rpc_lock *rpc_lock;
77         struct obd_device *obd = exp->exp_obd;
78         int rc, bufcount = 1, size[3] = {sizeof(*rec), ealen, ea2len};
79         ENTRY;
80
81         LASSERT(iattr != NULL);
82
83         if (ealen > 0) {
84                 bufcount = 2;
85                 if (ea2len > 0)
86                         bufcount = 3;
87         }
88
89         req = ptlrpc_prep_req(class_exp2cliimp(exp), MDS_REINT, bufcount,
90                               size, NULL);
91         if (req == NULL)
92                 RETURN(-ENOMEM);
93
94         if (iattr->ia_valid & ATTR_FROM_OPEN) {
95                 req->rq_request_portal = MDS_SETATTR_PORTAL; //XXX FIXME bug 249
96                 rpc_lock = obd->u.cli.cl_setattr_lock;
97         } else {
98                 rpc_lock = obd->u.cli.cl_rpc_lock;
99         }
100
101         if (iattr->ia_valid & (ATTR_MTIME | ATTR_CTIME))
102                 CDEBUG(D_INODE, "setting mtime %lu, ctime %lu\n",
103                        LTIME_S(iattr->ia_mtime), LTIME_S(iattr->ia_ctime));
104         mdc_setattr_pack(req->rq_reqmsg, data, iattr, ea, ealen, ea2, ea2len);
105
106         size[0] = sizeof(struct mds_body);
107         req->rq_replen = lustre_msg_size(1, size);
108
109         rc = mdc_reint(req, rpc_lock, LUSTRE_IMP_FULL);
110         *request = req;
111         if (rc == -ERESTARTSYS)
112                 rc = 0;
113
114         RETURN(rc);
115 }
116
117 int mdc_create(struct obd_export *exp, struct mdc_op_data *op_data,
118                const void *data, int datalen, int mode, __u32 uid, __u32 gid,
119                __u64 rdev, struct ptlrpc_request **request)
120 {
121         struct obd_device *obd = exp->exp_obd;
122         struct ptlrpc_request *req;
123         int rc, size[3] = {sizeof(struct mds_rec_create), op_data->namelen + 1};
124         int level, bufcount = 2;
125         ENTRY;
126
127         if (data && datalen) {
128                 size[bufcount] = datalen;
129                 bufcount++;
130         }
131
132         req = ptlrpc_prep_req(class_exp2cliimp(exp), MDS_REINT, bufcount,
133                               size, NULL);
134         if (req == NULL)
135                 RETURN(-ENOMEM);
136
137         /* mdc_create_pack fills msg->bufs[1] with name
138          * and msg->bufs[2] with tgt, for symlinks or lov MD data */
139         mdc_create_pack(req->rq_reqmsg, 0, op_data, mode, rdev, data, datalen);
140
141         size[0] = sizeof(struct mds_body);
142         req->rq_replen = lustre_msg_size(1, size);
143
144         level = LUSTRE_IMP_FULL;
145  resend:
146         rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, level);
147         /* Resend if we were told to. */
148         if (rc == -ERESTARTSYS) {
149                 level = LUSTRE_IMP_RECOVER;
150                 goto resend;
151         }
152
153         if (!rc)
154                 mdc_store_inode_generation(exp, req, 0, 0);
155
156         *request = req;
157         RETURN(rc);
158 }
159
160 int mdc_unlink(struct obd_export *exp, struct mdc_op_data *data,
161                struct ptlrpc_request **request)
162 {
163         struct obd_device *obddev = class_exp2obd(exp);
164         struct ptlrpc_request *req = *request;
165         int rc, size[2] = {sizeof(struct mds_rec_unlink), data->namelen + 1};
166         ENTRY;
167
168         LASSERT(req == NULL);
169         req = ptlrpc_prep_req(class_exp2cliimp(exp), MDS_REINT, 2, size,
170                               NULL);
171         if (req == NULL)
172                 RETURN(-ENOMEM);
173         *request = req;
174
175         size[0] = sizeof(struct mds_body);
176         size[1] = obddev->u.cli.cl_max_mds_easize;
177         size[2] = obddev->u.cli.cl_max_mds_cookiesize;
178         req->rq_replen = lustre_msg_size(3, size);
179
180         mdc_unlink_pack(req->rq_reqmsg, 0, data);
181
182         rc = mdc_reint(req, obddev->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
183         if (rc == -ERESTARTSYS)
184                 rc = 0;
185         RETURN(rc);
186 }
187
188 int mdc_link(struct obd_export *exp, struct mdc_op_data *data,
189              struct ptlrpc_request **request)
190 {
191         struct obd_device *obd = exp->exp_obd;
192         struct ptlrpc_request *req;
193         int rc, size[2] = {sizeof(struct mds_rec_link), data->namelen + 1};
194         ENTRY;
195
196         req = ptlrpc_prep_req(class_exp2cliimp(exp), MDS_REINT, 2, size,
197                               NULL);
198         if (req == NULL)
199                 RETURN(-ENOMEM);
200
201         mdc_link_pack(req->rq_reqmsg, 0, data);
202
203         size[0] = sizeof(struct mds_body);
204         req->rq_replen = lustre_msg_size(1, size);
205
206         rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
207         *request = req;
208         if (rc == -ERESTARTSYS)
209                 rc = 0;
210
211         RETURN(rc);
212 }
213
214 int mdc_rename(struct obd_export *exp, struct mdc_op_data *data,
215                const char *old, int oldlen, const char *new, int newlen,
216                struct ptlrpc_request **request)
217 {
218         struct obd_device *obd = exp->exp_obd;
219         struct ptlrpc_request *req;
220         int rc, size[3] = {sizeof(struct mds_rec_rename), oldlen + 1,
221                            newlen + 1};
222         ENTRY;
223
224         req = ptlrpc_prep_req(class_exp2cliimp(exp), MDS_REINT, 3, size,
225                               NULL);
226         if (req == NULL)
227                 RETURN(-ENOMEM);
228
229         mdc_rename_pack(req->rq_reqmsg, 0, data, old, oldlen, new, newlen);
230
231         size[0] = sizeof(struct mds_body);
232         size[1] = obd->u.cli.cl_max_mds_easize;
233         req->rq_replen = lustre_msg_size(2, size);
234
235         rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
236         *request = req;
237         if (rc == -ERESTARTSYS)
238                 rc = 0;
239
240         RETURN(rc);
241 }