Whamcloud - gitweb
- move the peter branch changes to the 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, 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, 
62                                MDS_REINT, 1, &size, NULL);
63         if (!req)
64                 RETURN(-ENOMEM);
65
66         mds_setattr_pack(req, 0, inode, iattr, NULL, 0);
67
68         size = sizeof(struct mds_body);
69         req->rq_replen = lustre_msg_size(1, &size);
70
71         rc = mdc_reint(req, LUSTRE_CONN_FULL);
72         *request = req;
73         if (rc == -ERESTARTSYS )
74                 rc = 0;
75
76         RETURN(rc);
77 }
78
79 int mdc_create(struct lustre_handle *conn,
80                struct inode *dir, const char *name, int namelen,
81                const char *tgt, int tgtlen, int mode, __u32 uid,
82                __u32 gid, __u64 time, __u64 rdev, struct lov_stripe_md *smd,
83                struct ptlrpc_request **request)
84 {
85         struct mds_rec_create *rec;
86         struct ptlrpc_request *req;
87         int rc, size[3] = {sizeof(struct mds_rec_create), namelen + 1, 0};
88         char *tmp, *bufs[3] = {NULL, NULL, NULL};
89         int level, bufcount = 2;
90         ENTRY;
91
92         if (S_ISREG(mode)) {
93                 if (!smd) {
94                         CERROR("File create, but no md (%ld, %*s)\n",
95                                dir->i_ino, namelen, name); 
96                         LBUG();
97                 }
98                 size[2] = smd->lmd_easize;
99                 bufs[2] = (char *)smd;
100                 bufcount = 3;
101         } else if (S_ISLNK(mode)) {
102                 size[2] = tgtlen + 1;
103                 bufcount = 3;
104         }
105
106         req = ptlrpc_prep_req2(conn, MDS_REINT, bufcount, size, bufs);
107         if (!req)
108                 RETURN(-ENOMEM);
109
110         /* mds_create_pack fills bufs[1] with name */
111         rec = lustre_msg_buf(req->rq_reqmsg, 0);
112         mds_create_pack(req, 0, dir, mode, rdev, uid, gid, time,
113                         name, namelen, NULL, 0);
114
115         if (S_ISREG(mode)) {
116                 tmp = lustre_msg_buf(req->rq_reqmsg, 2);
117                 memcpy(tmp, smd, smd->lmd_easize);
118         } else if (S_ISLNK(mode)) {
119                 tmp = lustre_msg_buf(req->rq_reqmsg, 2);
120                 LOGL0(tgt, tgtlen, tmp);
121         }
122
123         size[0] = sizeof(struct mds_body);
124         req->rq_replen = lustre_msg_size(1, size);
125
126         level = LUSTRE_CONN_FULL;
127  resend:
128         rc = mdc_reint(req, level);
129         if (rc == -ERESTARTSYS) {
130                 struct mds_update_record_hdr *hdr =
131                         lustre_msg_buf(req->rq_reqmsg, 0);
132                 level = LUSTRE_CONN_RECOVD;
133                 CERROR("Lost reply: re-create rep.\n");
134                 req->rq_flags = 0;
135                 hdr->ur_opcode = NTOH__u32(REINT_RECREATE);
136                 goto resend;
137         }
138
139         *request = req;
140         RETURN(rc);
141 }
142
143 int mdc_unlink(struct lustre_handle *conn,
144                struct inode *dir, struct inode *child, const char *name,
145                int namelen, struct ptlrpc_request **request)
146 {
147         struct ptlrpc_request *req;
148         int rc, size[2] = {sizeof(struct mds_rec_unlink), namelen + 1};
149         ENTRY;
150
151         req = ptlrpc_prep_req2(conn, MDS_REINT, 2, size, NULL);
152         if (!req)
153                 RETURN(-ENOMEM);
154
155         mds_unlink_pack(req, 0, dir, child, name, namelen);
156
157         size[0] = sizeof(struct mds_body);
158         req->rq_replen = lustre_msg_size(1, size);
159
160         rc = mdc_reint(req, LUSTRE_CONN_FULL);
161         *request = req;
162         if (rc == -ERESTARTSYS )
163                 rc = 0;
164
165         RETURN(rc);
166 }
167
168 int mdc_link(struct lustre_handle *conn,
169              struct dentry *src, struct inode *dir, const char *name,
170              int namelen, struct ptlrpc_request **request)
171 {
172         struct ptlrpc_request *req;
173         int rc, size[2] = {sizeof(struct mds_rec_link), namelen + 1};
174         ENTRY;
175
176         req = ptlrpc_prep_req2(conn, MDS_REINT, 2, size, NULL);
177         if (!req)
178                 RETURN(-ENOMEM);
179
180         mds_link_pack(req, 0, src->d_inode, dir, name, namelen);
181
182         size[0] = sizeof(struct mds_body);
183         req->rq_replen = lustre_msg_size(1, size);
184
185         rc = mdc_reint(req, LUSTRE_CONN_FULL);
186         *request = req;
187         if (rc == -ERESTARTSYS )
188                 rc = 0;
189
190         RETURN(rc);
191 }
192
193 int mdc_rename(struct lustre_handle *conn,
194                struct inode *src, struct inode *tgt, const char *old,
195                int oldlen, const char *new, int newlen,
196                struct ptlrpc_request **request)
197 {
198         struct ptlrpc_request *req;
199         int rc, size[3] = {sizeof(struct mds_rec_rename), oldlen + 1,
200                            newlen + 1};
201         ENTRY;
202
203         req = ptlrpc_prep_req2(conn, MDS_REINT, 3, size, NULL);
204         if (!req)
205                 RETURN(-ENOMEM);
206
207         mds_rename_pack(req, 0, src, tgt, old, oldlen, new, newlen);
208
209         size[0] = sizeof(struct mds_body);
210         req->rq_replen = lustre_msg_size(1, size);
211
212         rc = mdc_reint(req, LUSTRE_CONN_FULL);
213         *request = req;
214         if (rc == -ERESTARTSYS )
215                 rc = 0;
216
217         RETURN(rc);
218 }