Whamcloud - gitweb
land b1_5 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 the Lustre file system, http://www.lustre.org
7  *   Lustre is a trademark of Cluster File Systems, Inc.
8  *
9  *   You may have signed or agreed to another license before downloading
10  *   this software.  If so, you are bound by the terms and conditions
11  *   of that agreement, and the following does not apply to you.  See the
12  *   LICENSE file included with this distribution for more information.
13  *
14  *   If you did not agree to a different license, then this copy of Lustre
15  *   is open source software; you can redistribute it and/or modify it
16  *   under the terms of version 2 of the GNU General Public License as
17  *   published by the Free Software Foundation.
18  *
19  *   In either case, Lustre is distributed in the hope that it will be
20  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
21  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  *   license text for more details.
23  */
24
25 #ifndef EXPORT_SYMTAB
26 # define EXPORT_SYMTAB
27 #endif
28 #define DEBUG_SUBSYSTEM S_MDC
29
30 #ifdef __KERNEL__
31 #ifdef HAVE_KERNEL_CONFIG_H
32 # include <linux/config.h>
33 #endif
34 # include <linux/module.h>
35 # include <linux/kernel.h>
36 #else
37 # include <liblustre.h>
38 #endif
39
40 #include <obd_class.h>
41 #include "mdc_internal.h"
42
43 /* mdc_setattr does its own semaphore handling */
44 static int mdc_reint(struct ptlrpc_request *request,
45                      struct mdc_rpc_lock *rpc_lock, int level)
46 {
47         int rc;
48
49         request->rq_send_state = level;
50
51         mdc_get_rpc_lock(rpc_lock, NULL);
52         rc = ptlrpc_queue_wait(request);
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, REPLY_REC_OFF,
57                                      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
66  * should 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
69  * magic open-path setattr that should take the setattr semaphore and
70  * go to the 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, int ea2len,
73                 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 size[4] = { sizeof(struct ptlrpc_body),
80                         sizeof(*rec), ealen, ea2len };
81         int bufcount = 2, rc;
82         ENTRY;
83
84         LASSERT(iattr != NULL);
85
86         if (ealen > 0) {
87                 bufcount++;
88                 if (ea2len > 0)
89                         bufcount++;
90         }
91
92         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
93                               MDS_REINT, bufcount, size, NULL);
94         if (req == NULL)
95                 RETURN(-ENOMEM);
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, REQ_REC_OFF, data, iattr, ea, ealen, ea2, ea2len);
108
109         size[REPLY_REC_OFF] = sizeof(struct mds_body);
110         ptlrpc_req_set_repsize(req, 2, size);
111
112         rc = mdc_reint(req, rpc_lock, LUSTRE_IMP_FULL);
113         *request = req;
114         if (rc == -ERESTARTSYS)
115                 rc = 0;
116
117         RETURN(rc);
118 }
119
120 int mdc_create(struct obd_export *exp, struct mdc_op_data *op_data,
121                const void *data, int datalen, int mode, __u32 uid, __u32 gid,
122                __u32 cap_effective, __u64 rdev, struct ptlrpc_request **request)
123 {
124         struct obd_device *obd = exp->exp_obd;
125         struct ptlrpc_request *req;
126         int level, bufcount = 3, rc;
127         int size[4] = { sizeof(struct ptlrpc_body),
128                         sizeof(struct mds_rec_create),
129                         op_data->namelen + 1 };
130         ENTRY;
131
132         if (data && datalen) {
133                 size[bufcount] = datalen;
134                 bufcount++;
135         }
136
137         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
138                               MDS_REINT, bufcount, size, NULL);
139         if (req == NULL)
140                 RETURN(-ENOMEM);
141
142         /* mdc_create_pack fills msg->bufs[1] with name
143          * and msg->bufs[2] with tgt, for symlinks or lov MD data */
144         mdc_create_pack(req, REQ_REC_OFF, op_data, data, datalen, mode, uid,
145                         gid, cap_effective, rdev);
146
147         size[REPLY_REC_OFF] = sizeof(struct mds_body);
148         ptlrpc_req_set_repsize(req, 2, size);
149
150         level = LUSTRE_IMP_FULL;
151  resend:
152         rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, level);
153         /* Resend if we were told to. */
154         if (rc == -ERESTARTSYS) {
155                 level = LUSTRE_IMP_RECOVER;
156                 goto resend;
157         }
158
159         if (!rc)
160                 mdc_store_inode_generation(req, REQ_REC_OFF, REPLY_REC_OFF);
161
162         *request = req;
163         RETURN(rc);
164 }
165
166 int mdc_unlink(struct obd_export *exp, struct mdc_op_data *data,
167                struct ptlrpc_request **request)
168 {
169         struct obd_device *obd = class_exp2obd(exp);
170         struct ptlrpc_request *req = *request;
171         int size[4] = { sizeof(struct ptlrpc_body),
172                         sizeof(struct mds_rec_unlink),
173                         data->namelen + 1 };
174         int rc;
175         ENTRY;
176
177         LASSERT(req == NULL);
178         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
179                               MDS_REINT, 3, size, NULL);
180         if (req == NULL)
181                 RETURN(-ENOMEM);
182         *request = req;
183
184         size[REPLY_REC_OFF] = sizeof(struct mds_body);
185         size[REPLY_REC_OFF + 1] = obd->u.cli.cl_max_mds_easize;
186         size[REPLY_REC_OFF + 2] = obd->u.cli.cl_max_mds_cookiesize;
187         ptlrpc_req_set_repsize(req, 4, size);
188
189         mdc_unlink_pack(req, REQ_REC_OFF, data);
190
191         rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
192         if (rc == -ERESTARTSYS)
193                 rc = 0;
194         RETURN(rc);
195 }
196
197 int mdc_link(struct obd_export *exp, struct mdc_op_data *data,
198              struct ptlrpc_request **request)
199 {
200         struct obd_device *obd = exp->exp_obd;
201         struct ptlrpc_request *req;
202         int size[3] = { sizeof(struct ptlrpc_body),
203                         sizeof(struct mds_rec_link),
204                         data->namelen + 1 };
205         int rc;
206         ENTRY;
207
208         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
209                               MDS_REINT, 3, size, NULL);
210         if (req == NULL)
211                 RETURN(-ENOMEM);
212
213         mdc_link_pack(req, REQ_REC_OFF, data);
214
215         size[REPLY_REC_OFF] = sizeof(struct mds_body);
216         ptlrpc_req_set_repsize(req, 2, size);
217
218         rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
219         *request = req;
220         if (rc == -ERESTARTSYS)
221                 rc = 0;
222
223         RETURN(rc);
224 }
225
226 int mdc_rename(struct obd_export *exp, struct mdc_op_data *data,
227                const char *old, int oldlen, const char *new, int newlen,
228                struct ptlrpc_request **request)
229 {
230         struct obd_device *obd = exp->exp_obd;
231         struct ptlrpc_request *req;
232         int size[4] = { sizeof(struct ptlrpc_body),
233                         sizeof(struct mds_rec_rename),
234                         oldlen + 1,
235                         newlen + 1 };
236         int rc;
237         ENTRY;
238
239         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
240                               MDS_REINT, 4, size, NULL);
241         if (req == NULL)
242                 RETURN(-ENOMEM);
243
244         mdc_rename_pack(req, REQ_REC_OFF, data, old, oldlen, new, newlen);
245
246         size[REPLY_REC_OFF] = sizeof(struct mds_body);
247         size[REPLY_REC_OFF + 1] = obd->u.cli.cl_max_mds_easize;
248         size[REPLY_REC_OFF + 2] = obd->u.cli.cl_max_mds_cookiesize;
249         ptlrpc_req_set_repsize(req, 4, size);
250
251         rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
252         *request = req;
253         if (rc == -ERESTARTSYS)
254                 rc = 0;
255
256         RETURN(rc);
257 }