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