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