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