Whamcloud - gitweb
Land b_hd_capa onto HEAD (20050809_1942)
[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[4] = {0, sizeof(*rec), ealen, ea2len};
82         ENTRY;
83
84         LASSERT(iattr != NULL);
85
86         size[0] = lustre_secdesc_size();
87         if (ealen > 0) {
88                 bufcount++;
89                 if (ea2len > 0)
90                         bufcount++;
91                 if (ea3len > 0)
92                         bufcount++;
93         }
94
95         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
96                               MDS_REINT, bufcount, size, NULL);
97         if (req == NULL)
98                 RETURN(-ENOMEM);
99
100         lustre_pack_secdesc(req, size[0]);
101
102         if (iattr->ia_valid & ATTR_FROM_OPEN) {
103                 req->rq_request_portal = MDS_SETATTR_PORTAL; //XXX FIXME bug 249
104                 rpc_lock = obd->u.cli.cl_setattr_lock;
105         } else {
106                 rpc_lock = obd->u.cli.cl_rpc_lock;
107         }
108
109         if (iattr->ia_valid & (ATTR_MTIME | ATTR_CTIME))
110                 CDEBUG(D_INODE, "setting mtime %lu, ctime %lu\n",
111                        LTIME_S(iattr->ia_mtime), LTIME_S(iattr->ia_ctime));
112         mdc_setattr_pack(req->rq_reqmsg, 1, data, iattr, ea, ealen,
113                          ea2, ea2len, ea3, ea3len);
114
115         /* prepare the reply buffer
116          */
117         bufcount = 1;
118         size[0] = sizeof(struct mds_body);
119
120         /* This is a hack for setfacl remotely. XXX */
121         if (ealen == sizeof(XATTR_NAME_LUSTRE_ACL) &&
122             !strncmp((char *) ea, XATTR_NAME_LUSTRE_ACL, ealen)) {
123                 size[bufcount++] = LUSTRE_ACL_SIZE_MAX;
124         } else if (iattr->ia_valid & ATTR_SIZE) {
125                 size[bufcount++] = sizeof(struct lustre_capa);
126         }
127
128         req->rq_replen = lustre_msg_size(bufcount, size);
129
130         rc = mdc_reint(req, rpc_lock, LUSTRE_IMP_FULL);
131         *request = req;
132         if (rc == -ERESTARTSYS)
133                 rc = 0;
134
135         RETURN(rc);
136 }
137
138 int mdc_create(struct obd_export *exp, struct mdc_op_data *op_data,
139                const void *data, int datalen, int mode, __u32 uid,
140                __u32 gid, __u64 rdev, struct ptlrpc_request **request)
141 {
142         struct obd_device *obd = exp->exp_obd;
143         struct ptlrpc_request *req;
144         int rc, size[4] = {0, sizeof(struct mds_rec_create),
145                            op_data->namelen + 1};
146         int level, bufcount = 3;
147         ENTRY;
148
149         size[0] = lustre_secdesc_size();
150         if (data && datalen) {
151                 size[bufcount] = datalen;
152                 bufcount++;
153         }
154
155         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
156                               MDS_REINT, bufcount, size, NULL);
157         if (req == NULL)
158                 RETURN(-ENOMEM);
159
160         lustre_pack_secdesc(req, size[0]);
161
162         /*
163          * mdc_create_pack() fills msg->bufs[1] with name and msg->bufs[2] with
164          * tgt, for symlinks or lov MD data.
165          */
166         mdc_create_pack(req->rq_reqmsg, 1, op_data, mode, rdev, data, datalen);
167
168         size[0] = sizeof(struct mds_body);
169         req->rq_replen = lustre_msg_size(1, size);
170
171         level = LUSTRE_IMP_FULL;
172  resend:
173         rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, level);
174
175         /* resend if we were told to. */
176         if (rc == -ERESTARTSYS) {
177                 level = LUSTRE_IMP_RECOVER;
178                 goto resend;
179         }
180
181         if (!rc)
182                 mdc_store_inode_generation(exp, req, MDS_REQ_REC_OFF, 0);
183
184         *request = req;
185         RETURN(rc);
186 }
187
188 int mdc_unlink(struct obd_export *exp, struct mdc_op_data *data,
189                struct ptlrpc_request **request)
190 {
191         struct obd_device *obd = class_exp2obd(exp);
192         struct ptlrpc_request *req = *request;
193         int rc, size[4] = {0, sizeof(struct mds_rec_unlink),
194                            data->namelen + 1,
195                            obd->u.cli.cl_max_mds_cookiesize};
196         ENTRY;
197         LASSERT(req == NULL);
198
199         size[0] = lustre_secdesc_size();
200
201         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
202                               MDS_REINT, 4, size, NULL);
203         if (req == NULL)
204                 RETURN(-ENOMEM);
205
206         lustre_pack_secdesc(req, size[0]);
207         *request = req;
208
209         size[0] = sizeof(struct mds_body);
210         size[1] = obd->u.cli.cl_max_mds_easize;
211         size[2] = obd->u.cli.cl_max_mds_cookiesize;
212
213         req->rq_replen = lustre_msg_size(3, size);
214
215         mdc_unlink_pack(req->rq_reqmsg, 1, data);
216
217         rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
218         if (rc == -ERESTARTSYS)
219                 rc = 0;
220         RETURN(rc);
221 }
222
223 int mdc_link(struct obd_export *exp, struct mdc_op_data *data,
224              struct ptlrpc_request **request)
225 {
226         struct obd_device *obd = exp->exp_obd;
227         struct ptlrpc_request *req;
228         int rc, size[3] = {0, sizeof(struct mds_rec_link), data->namelen + 1};
229         ENTRY;
230
231         size[0] = lustre_secdesc_size();
232
233         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
234                               MDS_REINT, 3, size, NULL);
235         if (req == NULL)
236                 RETURN(-ENOMEM);
237
238         lustre_pack_secdesc(req, size[0]);
239
240         mdc_link_pack(req->rq_reqmsg, 1, data);
241
242         size[0] = sizeof(struct mds_body);
243         req->rq_replen = lustre_msg_size(1, size);
244
245         rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
246         *request = req;
247         if (rc == -ERESTARTSYS)
248                 rc = 0;
249
250         RETURN(rc);
251 }
252
253 int mdc_rename(struct obd_export *exp, struct mdc_op_data *data,
254                const char *old, int oldlen, const char *new, int newlen,
255                struct ptlrpc_request **request)
256 {
257         struct obd_device *obd = exp->exp_obd;
258         struct ptlrpc_request *req;
259         int rc, size[5] = {0, sizeof(struct mds_rec_rename), oldlen + 1,
260                            newlen + 1, obd->u.cli.cl_max_mds_cookiesize};
261         ENTRY;
262
263         size[0] = lustre_secdesc_size();
264
265         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
266                               MDS_REINT, 5, size, NULL);
267         if (req == NULL)
268                 RETURN(-ENOMEM);
269
270         lustre_pack_secdesc(req, size[0]);
271
272         mdc_rename_pack(req->rq_reqmsg, 1, data, old, oldlen, new, newlen);
273
274         size[0] = sizeof(struct mds_body);
275         size[1] = obd->u.cli.cl_max_mds_easize;
276         size[2] = obd->u.cli.cl_max_mds_cookiesize;
277         req->rq_replen = lustre_msg_size(3, size);
278
279         rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
280         *request = req;
281         if (rc == -ERESTARTSYS)
282                 rc = 0;
283
284         RETURN(rc);
285 }