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