Whamcloud - gitweb
- unland b_fid to 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 /* 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 *obd = 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                            obd->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] = obd->u.cli.cl_max_mds_easize;
191         size[2] = obd->u.cli.cl_max_mds_cookiesize;
192
193         req->rq_replen = lustre_msg_size(3, size);
194
195         mdc_unlink_pack(req->rq_reqmsg, 1, data);
196
197         rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
198         if (rc == -ERESTARTSYS)
199                 rc = 0;
200         RETURN(rc);
201 }
202
203 int mdc_link(struct obd_export *exp, struct mdc_op_data *data,
204              struct ptlrpc_request **request)
205 {
206         struct obd_device *obd = exp->exp_obd;
207         struct ptlrpc_request *req;
208         int rc, size[3] = {0, sizeof(struct mds_rec_link), data->namelen + 1};
209         ENTRY;
210
211         size[0] = mdc_get_secdesc_size();
212
213         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
214                               MDS_REINT, 3, size, NULL);
215         if (req == NULL)
216                 RETURN(-ENOMEM);
217
218         mdc_pack_secdesc(req, size[0]);
219
220         mdc_link_pack(req->rq_reqmsg, 1, data);
221
222         size[0] = sizeof(struct mds_body);
223         req->rq_replen = lustre_msg_size(1, size);
224
225         rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
226         *request = req;
227         if (rc == -ERESTARTSYS)
228                 rc = 0;
229
230         RETURN(rc);
231 }
232
233 int mdc_rename(struct obd_export *exp, struct mdc_op_data *data,
234                const char *old, int oldlen, const char *new, int newlen,
235                struct ptlrpc_request **request)
236 {
237         struct obd_device *obd = exp->exp_obd;
238         struct ptlrpc_request *req;
239         int rc, size[5] = {0, sizeof(struct mds_rec_rename), oldlen + 1,
240                            newlen + 1, obd->u.cli.cl_max_mds_cookiesize};
241         ENTRY;
242
243         size[0] = mdc_get_secdesc_size();
244
245         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
246                               MDS_REINT, 5, size, NULL);
247         if (req == NULL)
248                 RETURN(-ENOMEM);
249
250         mdc_pack_secdesc(req, size[0]);
251
252         mdc_rename_pack(req->rq_reqmsg, 1, data, old, oldlen, new, newlen);
253
254         size[0] = sizeof(struct mds_body);
255         size[1] = obd->u.cli.cl_max_mds_easize;
256         size[2] = obd->u.cli.cl_max_mds_cookiesize;
257         req->rq_replen = lustre_msg_size(3, size);
258
259         rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
260         *request = req;
261         if (rc == -ERESTARTSYS)
262                 rc = 0;
263
264         RETURN(rc);
265 }