Whamcloud - gitweb
merge b_devel into HEAD (20030703)
[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 #define EXPORT_SYMTAB
23 #define DEBUG_SUBSYSTEM S_MDC
24
25 #ifdef __KERNEL__
26 # include <linux/config.h>
27 # include <linux/module.h>
28 # include <linux/kernel.h>
29 #else
30 # include <liblustre.h>
31 #endif
32
33 #include <linux/obd_class.h>
34 #include <linux/lustre_mds.h>
35 #include "mdc_internal.h"
36
37 /* mdc_setattr does its own semaphore handling */
38 static int mdc_reint(struct ptlrpc_request *request, int level)
39 {
40         int rc;
41         __u32 *opcodeptr;
42
43         opcodeptr = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*opcodeptr));
44         request->rq_level = level;
45
46         if (!(*opcodeptr == REINT_SETATTR))
47                 mdc_get_rpc_lock(&mdc_rpc_lock, NULL);
48         rc = ptlrpc_queue_wait(request);
49         if (!(*opcodeptr == REINT_SETATTR))
50                 mdc_put_rpc_lock(&mdc_rpc_lock, NULL);
51
52         if (rc)
53                 CDEBUG(D_INFO, "error in handling %d\n", rc);
54         return rc;
55 }
56
57 /* If mdc_setattr is called with an 'iattr', then it is a normal RPC that
58  * should take the normal semaphore and go to the normal portal.
59  *
60  * If it is called with iattr->ia_valid & ATTR_FROM_OPEN, then it is a
61  * magic open-path setattr that should take the setattr semaphore and
62  * go to the setattr portal. */
63 int mdc_setattr(struct lustre_handle *conn,
64                 struct mdc_op_data *data,
65                 struct iattr *iattr, void *ea, int ealen,
66                 struct ptlrpc_request **request)
67 {
68         struct ptlrpc_request *req;
69         struct mds_rec_setattr *rec;
70         struct mdc_rpc_lock *rpc_lock;
71         int rc, bufcount = 1, size[2] = {sizeof(*rec), ealen};
72         ENTRY;
73
74         LASSERT(iattr != NULL);
75
76         if (ealen > 0)
77                 bufcount = 2;
78
79         req = ptlrpc_prep_req(class_conn2cliimp(conn), MDS_REINT, bufcount,
80                               size, NULL);
81         if (!req)
82                 RETURN(-ENOMEM);
83
84         if (iattr->ia_valid & ATTR_FROM_OPEN) {
85                 req->rq_request_portal = MDS_SETATTR_PORTAL; //XXX FIXME bug 249
86                 rpc_lock = &mdc_setattr_lock;
87         } else
88                 rpc_lock = &mdc_rpc_lock;
89
90         mds_setattr_pack(req, data, iattr, ea, ealen);
91
92         size[0] = sizeof(struct mds_body);
93         req->rq_replen = lustre_msg_size(1, size);
94
95         mdc_get_rpc_lock(rpc_lock, NULL);
96         rc = mdc_reint(req, LUSTRE_CONN_FULL);
97         mdc_put_rpc_lock(rpc_lock, NULL);
98
99         *request = req;
100         if (rc == -ERESTARTSYS)
101                 rc = 0;
102
103         RETURN(rc);
104 }
105
106 int mdc_create(struct lustre_handle *conn,
107                struct mdc_op_data *op_data,
108                const void *data, int datalen,
109                int mode, __u32 uid, __u32 gid, __u64 time, __u64 rdev,
110                struct ptlrpc_request **request)
111 {
112         struct ptlrpc_request *req;
113         int rc, size[3] = {sizeof(struct mds_rec_create),
114                            op_data->namelen + 1, 0};
115         int level, bufcount = 2;
116 //        ENTRY;
117
118         if (data && datalen) {
119                 size[bufcount] = datalen;
120                 bufcount++;
121         }
122
123         req = ptlrpc_prep_req(class_conn2cliimp(conn), MDS_REINT, bufcount,
124                               size, NULL);
125         if (!req)
126                 return -ENOMEM;
127 //                RETURN(-ENOMEM);
128
129         /* mds_create_pack fills msg->bufs[1] with name
130          * and msg->bufs[2] with tgt, for symlinks or lov MD data */
131         mds_create_pack(req, 0, op_data,
132                         mode, rdev, uid, gid, time,
133                         data, datalen);
134
135         size[0] = sizeof(struct mds_body);
136         req->rq_replen = lustre_msg_size(1, size);
137
138         level = LUSTRE_CONN_FULL;
139  resend:
140         rc = mdc_reint(req, level);
141         /* Resend if we were told to. */
142         if (rc == -ERESTARTSYS) {
143                 level = LUSTRE_CONN_RECOVER;
144                 goto resend;
145         }
146
147         if (!rc)
148                 mdc_store_inode_generation(req, 0, 0);
149
150         *request = req;
151         return rc;
152 //        RETURN(rc);
153 }
154
155 int mdc_unlink(struct lustre_handle *conn,
156                struct mdc_op_data *data,
157                struct ptlrpc_request **request)
158 {
159         struct obd_device *obddev = class_conn2obd(conn);
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
166         req = ptlrpc_prep_req(class_conn2cliimp(conn), MDS_REINT, 2, size,
167                               NULL);
168         if (!req)
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         req->rq_replen = lustre_msg_size(2, size);
175
176         mds_unlink_pack(req, 0, data);
177
178         rc = mdc_reint(req, LUSTRE_CONN_FULL);
179         if (rc == -ERESTARTSYS)
180                 rc = 0;
181         RETURN(rc);
182 }
183
184 int mdc_link(struct lustre_handle *conn,
185              struct mdc_op_data *data,
186              struct ptlrpc_request **request)
187 {
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_conn2cliimp(conn), MDS_REINT, 2, size,
193                               NULL);
194         if (!req)
195                 RETURN(-ENOMEM);
196
197         mds_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, LUSTRE_CONN_FULL);
203         *request = req;
204         if (rc == -ERESTARTSYS)
205                 rc = 0;
206
207         RETURN(rc);
208 }
209
210 int mdc_rename(struct lustre_handle *conn,
211                struct mdc_op_data *data,
212                const char *old, int oldlen,
213                const char *new, int newlen,
214                struct ptlrpc_request **request)
215 {
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_conn2cliimp(conn), MDS_REINT, 3, size,
222                               NULL);
223         if (!req)
224                 RETURN(-ENOMEM);
225
226         mds_rename_pack(req, 0, data, old, oldlen, new, newlen);
227
228         size[0] = sizeof(struct mds_body);
229         req->rq_replen = lustre_msg_size(1, size);
230
231         rc = mdc_reint(req, LUSTRE_CONN_FULL);
232         *request = req;
233         if (rc == -ERESTARTSYS)
234                 rc = 0;
235
236         RETURN(rc);
237 }