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