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