Whamcloud - gitweb
b=7340
[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 <linux/lustre_acl.h>
38 #include "mdc_internal.h"
39
40 /* this function actually sends request to desired target. */
41 static int mdc_reint(struct ptlrpc_request *request,
42                      struct mdc_rpc_lock *rpc_lock,
43                      int level)
44 {
45         int rc;
46
47         request->rq_send_state = level;
48        
49         if (rpc_lock)
50                 mdc_get_rpc_lock(rpc_lock, NULL);
51        
52         rc = ptlrpc_queue_wait(request);
53         
54         if (rpc_lock)
55                 mdc_put_rpc_lock(rpc_lock, NULL);
56         if (rc)
57                 CDEBUG(D_INFO, "error in handling %d\n", rc);
58         else if (!lustre_swab_repbuf(request, 0, sizeof(struct mds_body),
59                                      lustre_swab_mds_body)) {
60                 CERROR ("Can't unpack mds_body\n");
61                 rc = -EPROTO;
62         }
63         return rc;
64 }
65
66 /* If mdc_setattr is called with an 'iattr', then it is a normal RPC that should
67  * take the normal semaphore and go to the normal portal.
68  *
69  * If it is called with iattr->ia_valid & ATTR_FROM_OPEN, then it is a magic
70  * open-path setattr that should take the setattr semaphore and go to the
71  * setattr portal. */
72 int mdc_setattr(struct obd_export *exp, struct mdc_op_data *data,
73                 struct iattr *iattr, void *ea, int ealen, void *ea2,
74                 int ea2len, void *ea3, int ea3len, 
75                 struct ptlrpc_request **request)
76 {
77         struct ptlrpc_request *req;
78         struct mds_rec_setattr *rec;
79         struct mdc_rpc_lock *rpc_lock;
80         struct obd_device *obd = exp->exp_obd;
81         int rc, bufcount = 2, size[5] = {0, sizeof(*rec), ealen, ea2len, 
82                                          ea3len};
83         ENTRY;
84
85         LASSERT(iattr != NULL);
86
87         size[0] = lustre_secdesc_size();
88         if (ealen > 0) {
89                 bufcount++;
90                 if (ea2len > 0)
91                         bufcount++;
92                 if (ea3len > 0)
93                         bufcount++;
94         }
95
96         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
97                               MDS_REINT, bufcount, size, NULL);
98         if (req == NULL)
99                 RETURN(-ENOMEM);
100
101         lustre_pack_secdesc(req, size[0]);
102
103         if (iattr->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 (iattr->ia_valid & (ATTR_MTIME | ATTR_CTIME))
111                 CDEBUG(D_INODE, "setting mtime %lu, ctime %lu\n",
112                        LTIME_S(iattr->ia_mtime), LTIME_S(iattr->ia_ctime));
113         mdc_setattr_pack(req->rq_reqmsg, 1, data, iattr, ea, ealen,
114                          ea2, ea2len, ea3, ea3len);
115
116         /* prepare the reply buffer
117          */
118         bufcount = 1;
119         size[0] = sizeof(struct mds_body);
120
121         /* This is a hack for setfacl remotely. XXX */
122         if (ealen == sizeof(XATTR_NAME_LUSTRE_ACL) &&
123             !strncmp((char *) ea, XATTR_NAME_LUSTRE_ACL, ealen)) {
124                 size[bufcount++] = LUSTRE_ACL_SIZE_MAX;
125         } else if (iattr->ia_valid & ATTR_SIZE) {
126                 size[bufcount++] = sizeof(struct lustre_capa);
127         }
128
129         req->rq_replen = lustre_msg_size(bufcount, size);
130
131         rc = mdc_reint(req, rpc_lock, LUSTRE_IMP_FULL);
132         *request = req;
133         if (rc == -ERESTARTSYS)
134                 rc = 0;
135
136         RETURN(rc);
137 }
138
139 int mdc_create(struct obd_export *exp, struct mdc_op_data *op_data,
140                const void *data, int datalen, int mode, __u32 uid,
141                __u32 gid, __u64 rdev, struct ptlrpc_request **request)
142 {
143         struct obd_device *obd = exp->exp_obd;
144         struct ptlrpc_request *req;
145         int rc, size[4] = {0, sizeof(struct mds_rec_create),
146                            op_data->namelen + 1};
147         int level, bufcount = 3;
148         ENTRY;
149
150         size[0] = lustre_secdesc_size();
151         if (data && datalen) {
152                 size[bufcount] = datalen;
153                 bufcount++;
154         }
155
156         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
157                               MDS_REINT, bufcount, size, NULL);
158         if (req == NULL)
159                 RETURN(-ENOMEM);
160
161         lustre_pack_secdesc(req, size[0]);
162
163         /*
164          * mdc_create_pack() fills msg->bufs[1] with name and msg->bufs[2] with
165          * tgt, for symlinks or lov MD data.
166          */
167         mdc_create_pack(req->rq_reqmsg, 1, op_data, mode, rdev, data, datalen);
168
169         size[0] = sizeof(struct mds_body);
170         req->rq_replen = lustre_msg_size(1, size);
171
172         level = LUSTRE_IMP_FULL;
173  resend:
174         rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, level);
175
176         /* resend if we were told to. */
177         if (rc == -ERESTARTSYS) {
178                 level = LUSTRE_IMP_RECOVER;
179                 goto resend;
180         }
181
182         if (!rc)
183                 mdc_store_inode_generation(exp, req, MDS_REQ_REC_OFF, 0);
184
185         *request = req;
186         RETURN(rc);
187 }
188
189 int mdc_unlink(struct obd_export *exp, struct mdc_op_data *data,
190                struct ptlrpc_request **request)
191 {
192         struct obd_device *obd = class_exp2obd(exp);
193         struct ptlrpc_request *req = *request;
194         int rc, size[4] = {0, sizeof(struct mds_rec_unlink),
195                            data->namelen + 1,
196                            obd->u.cli.cl_max_mds_cookiesize};
197         ENTRY;
198         LASSERT(req == NULL);
199
200         size[0] = lustre_secdesc_size();
201
202         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
203                               MDS_REINT, 4, size, NULL);
204         if (req == NULL)
205                 RETURN(-ENOMEM);
206
207         lustre_pack_secdesc(req, size[0]);
208         *request = req;
209
210         size[0] = sizeof(struct mds_body);
211         size[1] = obd->u.cli.cl_max_mds_easize;
212         size[2] = obd->u.cli.cl_max_mds_cookiesize;
213
214         req->rq_replen = lustre_msg_size(3, size);
215
216         mdc_unlink_pack(req->rq_reqmsg, 1, data);
217
218         rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
219         if (rc == -ERESTARTSYS)
220                 rc = 0;
221         RETURN(rc);
222 }
223
224 int mdc_link(struct obd_export *exp, struct mdc_op_data *data,
225              struct ptlrpc_request **request)
226 {
227         struct obd_device *obd = exp->exp_obd;
228         struct ptlrpc_request *req;
229         int rc, size[3] = {0, sizeof(struct mds_rec_link), data->namelen + 1};
230         ENTRY;
231
232         size[0] = lustre_secdesc_size();
233
234         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
235                               MDS_REINT, 3, size, NULL);
236         if (req == NULL)
237                 RETURN(-ENOMEM);
238
239         lustre_pack_secdesc(req, size[0]);
240
241         mdc_link_pack(req->rq_reqmsg, 1, data);
242
243         size[0] = sizeof(struct mds_body);
244         req->rq_replen = lustre_msg_size(1, size);
245
246         rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
247         *request = req;
248         if (rc == -ERESTARTSYS)
249                 rc = 0;
250
251         RETURN(rc);
252 }
253
254 int mdc_rename(struct obd_export *exp, struct mdc_op_data *data,
255                const char *old, int oldlen, const char *new, int newlen,
256                struct ptlrpc_request **request)
257 {
258         struct obd_device *obd = exp->exp_obd;
259         struct ptlrpc_request *req;
260         int rc, size[5] = {0, sizeof(struct mds_rec_rename), oldlen + 1,
261                            newlen + 1, obd->u.cli.cl_max_mds_cookiesize};
262         ENTRY;
263
264         size[0] = lustre_secdesc_size();
265
266         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
267                               MDS_REINT, 5, size, NULL);
268         if (req == NULL)
269                 RETURN(-ENOMEM);
270
271         lustre_pack_secdesc(req, size[0]);
272
273         mdc_rename_pack(req->rq_reqmsg, 1, data, old, oldlen, new, newlen);
274
275         size[0] = sizeof(struct mds_body);
276         size[1] = obd->u.cli.cl_max_mds_easize;
277         size[2] = obd->u.cli.cl_max_mds_cookiesize;
278         req->rq_replen = lustre_msg_size(3, size);
279
280         rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
281         *request = req;
282         if (rc == -ERESTARTSYS)
283                 rc = 0;
284
285         RETURN(rc);
286 }