Whamcloud - gitweb
- make HEAD from b_post_cmd3
[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 the Lustre file system, http://www.lustre.org
7  *   Lustre is a trademark of Cluster File Systems, Inc.
8  *
9  *   You may have signed or agreed to another license before downloading
10  *   this software.  If so, you are bound by the terms and conditions
11  *   of that agreement, and the following does not apply to you.  See the
12  *   LICENSE file included with this distribution for more information.
13  *
14  *   If you did not agree to a different license, then this copy of Lustre
15  *   is open source software; you can redistribute it and/or modify it
16  *   under the terms of version 2 of the GNU General Public License as
17  *   published by the Free Software Foundation.
18  *
19  *   In either case, Lustre is distributed in the hope that it will be
20  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
21  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  *   license text for more details.
23  */
24
25 #ifndef EXPORT_SYMTAB
26 # define EXPORT_SYMTAB
27 #endif
28 #define DEBUG_SUBSYSTEM S_MDC
29
30 #ifdef __KERNEL__
31 #ifdef HAVE_KERNEL_CONFIG_H
32 # include <linux/config.h>
33 #endif
34 # include <linux/module.h>
35 # include <linux/kernel.h>
36 #else
37 # include <liblustre.h>
38 #endif
39
40 #include <obd_class.h>
41 #include "mdc_internal.h"
42
43 /* mdc_setattr does its own semaphore handling */
44 static int mdc_reint(struct ptlrpc_request *request,
45                      struct mdc_rpc_lock *rpc_lock, int level)
46 {
47         int rc;
48
49         request->rq_send_state = level;
50
51         mdc_get_rpc_lock(rpc_lock, NULL);
52         rc = ptlrpc_queue_wait(request);
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, REPLY_REC_OFF,
57                                      sizeof(struct mdt_body),
58                                      lustre_swab_mdt_body)) {
59                 CERROR ("Can't unpack mdt_body\n");
60                 rc = -EPROTO;
61         }
62         return rc;
63 }
64
65 /* If mdc_setattr is called with an 'iattr', then it is a normal RPC that
66  * should take the normal semaphore and go to the normal portal.
67  *
68  * If it is called with iattr->ia_valid & ATTR_FROM_OPEN, then it is a
69  * magic open-path setattr that should take the setattr semaphore and
70  * go to the setattr portal. */
71 int mdc_setattr(struct obd_export *exp, struct md_op_data *op_data,
72                 void *ea, int ealen, void *ea2, int ea2len,
73                 struct ptlrpc_request **request)
74 {
75         struct ptlrpc_request *req;
76         struct mdt_rec_setattr *rec;
77         struct mdc_rpc_lock *rpc_lock;
78         struct obd_device *obd = exp->exp_obd;
79         int size[6] = { sizeof(struct ptlrpc_body),
80                         sizeof(*rec), 0, 0, ealen, ea2len };
81         int bufcount = 4, rc;
82         ENTRY;
83
84         LASSERT(op_data != NULL);
85
86         size[REQ_REC_OFF + 1] = op_data->op_capa1 ?
87                                         sizeof(struct lustre_capa) : 0;
88
89         if (op_data->op_flags & (MF_SOM_CHANGE | MF_EPOCH_OPEN))
90                 size[REQ_REC_OFF + 2] = sizeof(struct mdt_epoch);
91
92         if (ealen > 0) {
93                 bufcount++;
94                 if (ea2len > 0)
95                         bufcount++;
96         }
97
98         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
99                               MDS_REINT, bufcount, size, NULL);
100         if (req == NULL)
101                 RETURN(-ENOMEM);
102
103         if (op_data->op_attr.ia_valid & ATTR_FROM_OPEN) {
104                 req->rq_request_portal = MDS_SETATTR_PORTAL; //XXX FIXME bug 249
105                 rpc_lock = obd->u.cli.cl_setattr_lock;
106         } else {
107                 rpc_lock = obd->u.cli.cl_rpc_lock;
108         }
109
110         if (op_data->op_attr.ia_valid & (ATTR_MTIME | ATTR_CTIME))
111                 CDEBUG(D_INODE, "setting mtime %lu, ctime %lu\n",
112                        LTIME_S(op_data->op_attr.ia_mtime),
113                        LTIME_S(op_data->op_attr.ia_ctime));
114         mdc_setattr_pack(req, REQ_REC_OFF, op_data, ea, ealen, ea2, ea2len);
115
116         size[REPLY_REC_OFF] = sizeof(struct mdt_body);
117         size[REPLY_REC_OFF + 1] = sizeof(struct lustre_capa);
118         ptlrpc_req_set_repsize(req, 3, size);
119
120         rc = mdc_reint(req, rpc_lock, LUSTRE_IMP_FULL);
121         *request = req;
122         if (rc == -ERESTARTSYS)
123                 rc = 0;
124
125         RETURN(rc);
126 }
127
128 int mdc_create(struct obd_export *exp, struct md_op_data *op_data,
129                const void *data, int datalen, int mode, __u32 uid, __u32 gid,
130                __u32 cap_effective, __u64 rdev, struct ptlrpc_request **request)
131 {
132         int size[5] = { sizeof(struct ptlrpc_body),
133                         sizeof(struct mdt_rec_create),
134                         0, op_data->op_namelen + 1 };
135         struct obd_device *obd = exp->exp_obd;
136         int level, bufcount = 4, rc;
137         struct ptlrpc_request *req;
138         ENTRY;
139
140         /* For case if upper layer did not alloc fid, do it now. */
141         if (!fid_is_sane(&op_data->op_fid2)) {
142                 /*
143                  * mdc_fid_alloc() may return errno 1 in case of switch to new
144                  * sequence, handle this.
145                  */
146                 rc = mdc_fid_alloc(exp, &op_data->op_fid2, op_data);
147                 if (rc < 0) {
148                         CERROR("Can't alloc new fid, rc %d\n", rc);
149                         RETURN(rc);
150                 }
151         }
152
153         size[REQ_REC_OFF + 1] = op_data->op_capa1 ?
154                 sizeof(struct lustre_capa) : 0;
155         
156         if (data && datalen) {
157                 size[bufcount] = datalen;
158                 bufcount++;
159         }
160
161         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
162                               MDS_REINT, bufcount, size, NULL);
163         if (req == NULL)
164                 RETURN(-ENOMEM);
165
166         /*
167          * mdc_create_pack() fills msg->bufs[1] with name and msg->bufs[2] with
168          * tgt, for symlinks or lov MD data.
169          */
170         mdc_create_pack(req, REQ_REC_OFF, op_data, data, datalen, mode, uid,
171                         gid, cap_effective, rdev);
172
173         size[REPLY_REC_OFF] = sizeof(struct mdt_body);
174         size[REPLY_REC_OFF + 1] = sizeof(struct lustre_capa);
175         ptlrpc_req_set_repsize(req, 3, size);
176
177         level = LUSTRE_IMP_FULL;
178  resend:
179         rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, level);
180         
181         /* Resend if we were told to. */
182         if (rc == -ERESTARTSYS) {
183                 level = LUSTRE_IMP_RECOVER;
184                 goto resend;
185         } else if (rc == 0) {
186                 struct mdt_body *body;
187                 struct lustre_capa *capa;
188
189                 body = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
190                                       sizeof(*body));
191                 LASSERT(body);
192                 if (body->valid & OBD_MD_FLMDSCAPA) {
193                         capa = lustre_unpack_capa(req->rq_repmsg,
194                                                   REPLY_REC_OFF + 1);
195                         if (capa == NULL) {
196                                 CERROR("Missing/short MDS capability\n");
197                                 rc = -EPROTO;
198                         }
199                 }
200         }
201
202         *request = req;
203         RETURN(rc);
204 }
205
206 int mdc_unlink(struct obd_export *exp, struct md_op_data *op_data,
207                struct ptlrpc_request **request)
208 {
209         struct obd_device *obd = class_exp2obd(exp);
210         struct ptlrpc_request *req = *request;
211         int size[4] = { sizeof(struct ptlrpc_body),
212                         sizeof(struct mdt_rec_unlink),
213                         0, op_data->op_namelen + 1 };
214         int rc;
215         ENTRY;
216
217         LASSERT(req == NULL);
218
219         size[REQ_REC_OFF + 1] = op_data->op_capa1 ?
220                                         sizeof(struct lustre_capa) : 0;
221
222         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
223                               MDS_REINT, 4, size, NULL);
224         if (req == NULL)
225                 RETURN(-ENOMEM);
226         *request = req;
227
228         size[REPLY_REC_OFF] = sizeof(struct mdt_body);
229         size[REPLY_REC_OFF + 1] = obd->u.cli.cl_max_mds_easize;
230         size[REPLY_REC_OFF + 2] = obd->u.cli.cl_max_mds_cookiesize;
231         ptlrpc_req_set_repsize(req, 4, size);
232
233         mdc_unlink_pack(req, REQ_REC_OFF, op_data);
234
235         rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
236         if (rc == -ERESTARTSYS)
237                 rc = 0;
238         RETURN(rc);
239 }
240
241 int mdc_link(struct obd_export *exp, struct md_op_data *op_data,
242              struct ptlrpc_request **request)
243 {
244         struct obd_device *obd = exp->exp_obd;
245         struct ptlrpc_request *req;
246         int size[5] = { sizeof(struct ptlrpc_body),
247                         sizeof(struct mdt_rec_link),
248                         0, 0, op_data->op_namelen + 1 };
249         int rc;
250         ENTRY;
251
252         size[REQ_REC_OFF + 1] = op_data->op_capa1 ?
253                 sizeof(struct lustre_capa) : 0;
254         size[REQ_REC_OFF + 2] = op_data->op_capa2 ?
255                 sizeof(struct lustre_capa) : 0;
256
257         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
258                               MDS_REINT, 5, size, NULL);
259         if (req == NULL)
260                 RETURN(-ENOMEM);
261
262         mdc_link_pack(req, REQ_REC_OFF, op_data);
263
264         size[REPLY_REC_OFF] = sizeof(struct mdt_body);
265         ptlrpc_req_set_repsize(req, 2, size);
266
267         rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
268         *request = req;
269         if (rc == -ERESTARTSYS)
270                 rc = 0;
271
272         RETURN(rc);
273 }
274
275 int mdc_rename(struct obd_export *exp, struct md_op_data *op_data,
276                const char *old, int oldlen, const char *new, int newlen,
277                struct ptlrpc_request **request)
278 {
279         struct obd_device *obd = exp->exp_obd;
280         struct ptlrpc_request *req;
281         int size[6] = { sizeof(struct ptlrpc_body),
282                         sizeof(struct mdt_rec_rename),
283                         0, 0, oldlen + 1, newlen + 1 };
284         int rc;
285         ENTRY;
286
287         size[REQ_REC_OFF + 1] = op_data->op_capa1 ?
288                                         sizeof(struct lustre_capa) : 0;
289         size[REQ_REC_OFF + 2] = op_data->op_capa2 ?
290                                         sizeof(struct lustre_capa) : 0;
291
292         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
293                               MDS_REINT, 6, size, NULL);
294         if (req == NULL)
295                 RETURN(-ENOMEM);
296
297         mdc_rename_pack(req, REQ_REC_OFF, op_data, old, oldlen, new, newlen);
298
299         size[REPLY_REC_OFF] = sizeof(struct mdt_body);
300         size[REPLY_REC_OFF + 1] = obd->u.cli.cl_max_mds_easize;
301         size[REPLY_REC_OFF + 2] = obd->u.cli.cl_max_mds_cookiesize;
302         ptlrpc_req_set_repsize(req, 4, size);
303
304         rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
305         *request = req;
306         if (rc == -ERESTARTSYS)
307                 rc = 0;
308
309         RETURN(rc);
310 }