Whamcloud - gitweb
Do proper setup/cleanup of MDS exports and client data.
[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, 2002 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
23 #define EXPORT_SYMTAB
24
25 #include <linux/config.h>
26 #include <linux/module.h>
27 #include <linux/kernel.h>
28
29 #define DEBUG_SUBSYSTEM S_MDC
30
31 #include <linux/obd_class.h>
32 #include <linux/lustre_mds.h>
33
34 static int mdc_reint(struct ptlrpc_request *request, int level)
35 {
36         int rc;
37         request->rq_level = level;
38
39         rc = ptlrpc_queue_wait(request);
40         rc = ptlrpc_check_status(request, rc);
41
42         if (rc)
43                 CERROR("error in handling %d\n", rc);
44
45         return rc;
46 }
47
48 int mdc_setattr(struct lustre_handle *conn,
49                 struct inode *inode, struct iattr *iattr,
50                 struct ptlrpc_request **request)
51 {
52         struct ptlrpc_client *cl;
53         struct ptlrpc_connection *connection;
54         struct lustre_handle *rconn;
55         struct mds_rec_setattr *rec;
56         struct ptlrpc_request *req;
57         int rc, size = sizeof(*rec);
58         ENTRY;
59
60         mdc_con2cl(conn, &cl, &connection, &rconn);
61         req = ptlrpc_prep_req2(conn, MDS_REINT, 1, &size, NULL);
62         if (!req)
63                 RETURN(-ENOMEM);
64
65         mds_setattr_pack(req, 0, inode, iattr, NULL, 0);
66
67         size = sizeof(struct mds_body);
68         req->rq_replen = lustre_msg_size(1, &size);
69
70         rc = mdc_reint(req, LUSTRE_CONN_FULL);
71         *request = req;
72         if (rc == -ERESTARTSYS )
73                 rc = 0;
74
75         RETURN(rc);
76 }
77
78 int mdc_create(struct lustre_handle *conn,
79                struct inode *dir, const char *name, int namelen,
80                const char *tgt, int tgtlen, int mode, __u32 uid,
81                __u32 gid, __u64 time, __u64 rdev, struct lov_stripe_md *smd,
82                struct ptlrpc_request **request)
83 {
84         struct mds_rec_create *rec;
85         struct ptlrpc_request *req;
86         int rc, size[3] = {sizeof(struct mds_rec_create), namelen + 1, 0};
87         char *tmp, *bufs[3] = {NULL, NULL, NULL};
88         int level, bufcount = 2;
89         ENTRY;
90
91         if (S_ISREG(mode)) {
92                 if (!smd) {
93                         CERROR("File create, but no md (%ld, %*s)\n",
94                                dir->i_ino, namelen, name); 
95                         LBUG();
96                 }
97                 size[2] = smd->lmd_easize;
98                 bufs[2] = (char *)smd;
99                 bufcount = 3;
100         } else if (S_ISLNK(mode)) {
101                 size[2] = tgtlen + 1;
102                 bufcount = 3;
103         }
104
105         req = ptlrpc_prep_req2(conn, MDS_REINT, bufcount, size, bufs);
106         if (!req)
107                 RETURN(-ENOMEM);
108
109         /* mds_create_pack fills bufs[1] with name */
110         rec = lustre_msg_buf(req->rq_reqmsg, 0);
111         mds_create_pack(req, 0, dir, mode, rdev, uid, gid, time,
112                         name, namelen, NULL, 0);
113
114         if (S_ISREG(mode)) {
115                 tmp = lustre_msg_buf(req->rq_reqmsg, 2);
116                 memcpy(tmp, smd, smd->lmd_easize);
117         } else if (S_ISLNK(mode)) {
118                 tmp = lustre_msg_buf(req->rq_reqmsg, 2);
119                 LOGL0(tgt, tgtlen, tmp);
120         }
121
122         size[0] = sizeof(struct mds_body);
123         req->rq_replen = lustre_msg_size(1, size);
124
125         level = LUSTRE_CONN_FULL;
126  resend:
127         rc = mdc_reint(req, level);
128         if (rc == -ERESTARTSYS) {
129                 struct mds_update_record_hdr *hdr =
130                         lustre_msg_buf(req->rq_reqmsg, 0);
131                 level = LUSTRE_CONN_RECOVD;
132                 CERROR("Lost reply: re-create rep.\n");
133                 req->rq_flags = 0;
134                 hdr->ur_opcode = NTOH__u32(REINT_RECREATE);
135                 goto resend;
136         }
137
138         *request = req;
139         RETURN(rc);
140 }
141
142 int mdc_unlink(struct lustre_handle *conn,
143                struct inode *dir, struct inode *child, const char *name,
144                int namelen, struct ptlrpc_request **request)
145 {
146         struct ptlrpc_request *req;
147         int rc, size[2] = {sizeof(struct mds_rec_unlink), namelen + 1};
148         ENTRY;
149
150         req = ptlrpc_prep_req2(conn, MDS_REINT, 2, size, NULL);
151         if (!req)
152                 RETURN(-ENOMEM);
153
154         mds_unlink_pack(req, 0, dir, child, name, namelen);
155
156         size[0] = sizeof(struct mds_body);
157         req->rq_replen = lustre_msg_size(1, size);
158
159         rc = mdc_reint(req, LUSTRE_CONN_FULL);
160         *request = req;
161         if (rc == -ERESTARTSYS )
162                 rc = 0;
163
164         RETURN(rc);
165 }
166
167 int mdc_link(struct lustre_handle *conn,
168              struct dentry *src, struct inode *dir, const char *name,
169              int namelen, struct ptlrpc_request **request)
170 {
171         struct ptlrpc_request *req;
172         int rc, size[2] = {sizeof(struct mds_rec_link), namelen + 1};
173         ENTRY;
174
175         req = ptlrpc_prep_req2(conn, MDS_REINT, 2, size, NULL);
176         if (!req)
177                 RETURN(-ENOMEM);
178
179         mds_link_pack(req, 0, src->d_inode, dir, name, namelen);
180
181         size[0] = sizeof(struct mds_body);
182         req->rq_replen = lustre_msg_size(1, size);
183
184         rc = mdc_reint(req, LUSTRE_CONN_FULL);
185         *request = req;
186         if (rc == -ERESTARTSYS )
187                 rc = 0;
188
189         RETURN(rc);
190 }
191
192 int mdc_rename(struct lustre_handle *conn,
193                struct inode *src, struct inode *tgt, const char *old,
194                int oldlen, const char *new, int newlen,
195                struct ptlrpc_request **request)
196 {
197         struct ptlrpc_request *req;
198         int rc, size[3] = {sizeof(struct mds_rec_rename), oldlen + 1,
199                            newlen + 1};
200         ENTRY;
201
202         req = ptlrpc_prep_req2(conn, MDS_REINT, 3, size, 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 }