Whamcloud - gitweb
Merge b_md into 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 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
24 #include <linux/config.h>
25 #include <linux/module.h>
26 #include <linux/kernel.h>
27
28 #define DEBUG_SUBSYSTEM S_MDC
29
30 #include <linux/obd_class.h>
31 #include <linux/lustre_mds.h>
32
33 extern struct semaphore mdc_sem;
34
35 static int mdc_reint(struct ptlrpc_request *request, int level)
36 {
37         int rc;
38         __u32 *opcodeptr = lustre_msg_buf(request->rq_reqmsg, 0);
39
40         request->rq_level = level;
41
42         if (!(*opcodeptr == REINT_SETATTR))
43                 mdc_get_rpc_lock(&mdc_rpc_lock, NULL);
44
45         rc = ptlrpc_queue_wait(request);
46         if (!(*opcodeptr == REINT_SETATTR))
47                 mdc_put_rpc_lock(&mdc_rpc_lock, NULL);
48
49         if (rc) {
50                 CDEBUG(D_INFO, "error in handling %d\n", rc);
51         } else {
52                 /* For future resend/replays. */
53                 *opcodeptr |= REINT_REPLAYING;
54         }
55         return rc;
56 }
57
58 int mdc_setattr(struct lustre_handle *conn, struct inode *inode,
59                 struct iattr *iattr, void *ea, int ealen,
60                 struct ptlrpc_request **request)
61 {
62         struct ptlrpc_request *req;
63         struct mds_rec_setattr *rec;
64         int rc, bufcount = 1, size[2] = {sizeof(*rec), ealen};
65         ENTRY;
66
67         if (ealen > 0)
68                 bufcount = 2;
69
70         req = ptlrpc_prep_req(class_conn2cliimp(conn), MDS_REINT, bufcount,
71                               size, NULL);
72         if (!req)
73                 RETURN(-ENOMEM);
74
75         /* XXX FIXME bug 249 */
76         req->rq_request_portal = MDS_GETATTR_PORTAL;
77
78         mds_setattr_pack(req, inode, iattr, ea, ealen);
79
80         size[0] = sizeof(struct mds_body);
81         req->rq_replen = lustre_msg_size(1, size);
82
83         rc = mdc_reint(req, LUSTRE_CONN_FULL);
84         *request = req;
85         if (rc == -ERESTARTSYS)
86                 rc = 0;
87
88         RETURN(rc);
89 }
90
91 int mdc_create(struct lustre_handle *conn, struct inode *dir,
92                const char *name, int namelen, const void *data, int datalen,
93                int mode, __u32 uid, __u32 gid, __u64 time, __u64 rdev,
94                struct ptlrpc_request **request)
95 {
96         struct ptlrpc_request *req;
97         int rc, size[3] = {sizeof(struct mds_rec_create), namelen + 1, 0};
98         int level, bufcount = 2;
99         ENTRY;
100
101         if (data && datalen) {
102                 size[bufcount] = datalen;
103                 bufcount++;
104         }
105
106         req = ptlrpc_prep_req(class_conn2cliimp(conn), MDS_REINT, bufcount,
107                               size, NULL);
108         if (!req)
109                 RETURN(-ENOMEM);
110
111         /* mds_create_pack fills msg->bufs[1] with name
112          * and msg->bufs[2] with tgt, for symlinks or lov MD data */
113         mds_create_pack(req, 0, dir, mode, rdev, uid, gid, time,
114                         name, namelen, data, datalen);
115
116         size[0] = sizeof(struct mds_body);
117         req->rq_replen = lustre_msg_size(1, size);
118
119         level = LUSTRE_CONN_FULL;
120  resend:
121         rc = mdc_reint(req, level);
122         /* Resend if we were told to. */
123         if (rc == -ERESTARTSYS) {
124                 level = LUSTRE_CONN_RECOVD;
125                 req->rq_flags = 0;
126                 goto resend;
127         }
128
129         if (!rc)
130                 mdc_store_inode_generation(req, 0, 0);
131
132         *request = req;
133         RETURN(rc);
134 }
135
136 int mdc_unlink(struct lustre_handle *conn, struct inode *dir,
137                struct inode *child, __u32 mode, const char *name, int namelen,
138                struct ptlrpc_request **request)
139 {
140         struct obd_device *obddev = class_conn2obd(conn);
141         struct ptlrpc_request *req = *request;
142         int rc, size[2] = {sizeof(struct mds_rec_unlink), namelen + 1};
143         ENTRY;
144
145         LASSERT(req == NULL);
146
147         req = ptlrpc_prep_req(class_conn2cliimp(conn), MDS_REINT, 2, size,
148                               NULL);
149         if (!req)
150                 RETURN(-ENOMEM);
151         *request = req;
152
153         size[0] = sizeof(struct mds_body);
154         size[1] = obddev->u.cli.cl_max_mds_easize;
155         req->rq_replen = lustre_msg_size(2, size);
156
157         mds_unlink_pack(req, 0, dir, child, mode, name, namelen);
158
159         rc = mdc_reint(req, LUSTRE_CONN_FULL);
160         if (rc == -ERESTARTSYS)
161                 rc = 0;
162         RETURN(rc);
163 }
164
165 int mdc_link(struct lustre_handle *conn,
166              struct inode *src, struct inode *dir, const char *name,
167              int namelen, struct ptlrpc_request **request)
168 {
169         struct ptlrpc_request *req;
170         int rc, size[2] = {sizeof(struct mds_rec_link), namelen + 1};
171         ENTRY;
172
173         req = ptlrpc_prep_req(class_conn2cliimp(conn), MDS_REINT, 2, size,
174                               NULL);
175         if (!req)
176                 RETURN(-ENOMEM);
177
178         mds_link_pack(req, 0, src, dir, name, namelen);
179
180         size[0] = sizeof(struct mds_body);
181         req->rq_replen = lustre_msg_size(1, size);
182
183         rc = mdc_reint(req, LUSTRE_CONN_FULL);
184         *request = req;
185         if (rc == -ERESTARTSYS)
186                 rc = 0;
187
188         RETURN(rc);
189 }
190
191 int mdc_rename(struct lustre_handle *conn,
192                struct inode *src, struct inode *tgt, const char *old,
193                int oldlen, const char *new, int newlen,
194                struct ptlrpc_request **request)
195 {
196         struct ptlrpc_request *req;
197         int rc, size[3] = {sizeof(struct mds_rec_rename), oldlen + 1,
198                            newlen + 1};
199         ENTRY;
200
201         req = ptlrpc_prep_req(class_conn2cliimp(conn), MDS_REINT, 3, size,
202                               NULL);
203         if (!req)
204                 RETURN(-ENOMEM);
205
206         mds_rename_pack(req, 0, src, tgt, old, oldlen, new, newlen);
207
208         size[0] = sizeof(struct mds_body);
209         req->rq_replen = lustre_msg_size(1, size);
210
211         rc = mdc_reint(req, LUSTRE_CONN_FULL);
212         *request = req;
213         if (rc == -ERESTARTSYS)
214                 rc = 0;
215
216         RETURN(rc);
217 }