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