Whamcloud - gitweb
Large commit which implements more "intelligent" offsets for stripe
[fs/lustre-release.git] / lustre / mds / mds_lov.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  linux/mds/mds_lov.c
5  *
6  *  Lustre Metadata Server (mds) handling of striped file data
7  *
8  *  Copyright (C) 2001, 2002 Cluster File Systems, Inc.
9  *
10  *  This code is issued under the GNU General Public License.
11  *  See the file COPYING in this distribution
12  *
13  *  by Peter Braam <braam@clusterfs.com> &
14  *
15  */
16
17 #define EXPORT_SYMTAB
18 #define DEBUG_SUBSYSTEM S_MDS
19
20 #include <linux/module.h>
21 #include <linux/lustre_mds.h>
22 #include <linux/lustre_idl.h>
23 #include <linux/obd_class.h>
24 #include <linux/obd_lov.h>
25 #include <linux/lustre_lib.h>
26
27 int mds_configure_lov(struct obd_device *obd, struct lov_desc *desc,
28                       uuid_t *uuidarray)
29 {
30         struct mds_obd *mds = &obd->u.mds;
31         struct obd_run_ctxt saved;
32         struct file *f;
33         int tgt_count;
34         int rc;
35         int i;
36
37         tgt_count = desc->ld_tgt_count;
38         lov_packdesc(desc);
39
40         push_ctxt(&saved, &mds->mds_ctxt);
41         f = filp_open("LOVDESC", O_CREAT|O_RDWR, 0644);
42         if (IS_ERR(f)) {
43                 CERROR("Cannot open/create LOVDESC file\n");
44                 GOTO(out, rc = PTR_ERR(f));
45         }
46
47         rc = lustre_fwrite(f, (char *)desc, sizeof(*desc), &f->f_pos);
48         if (filp_close(f, 0))
49                 CERROR("Error closing LOVDESC file\n");
50         if (rc != sizeof(*desc)) {
51                 CERROR("Cannot open/create LOVDESC file\n");
52                 GOTO(out, rc = PTR_ERR(f));
53         }
54
55         f = filp_open("LOVTGTS", O_CREAT|O_RDWR, 0644);
56         if (IS_ERR(f)) {
57                 CERROR("Cannot open/create LOVTGTS file\n");
58                 GOTO(out, rc = PTR_ERR(f));
59         }
60
61         rc = 0;
62         for (i = 0; i < tgt_count ; i++) {
63                 rc = lustre_fwrite(f, uuidarray[i],
64                                    sizeof(uuidarray[i]), &f->f_pos);
65                 if (rc != sizeof(uuidarray[i])) {
66                         CERROR("cannot write LOV UUID %s (%d)\n",
67                                uuidarray[i], i);
68                         if (rc >= 0)
69                                 rc = -EIO;
70                         break;
71                 } else
72                         rc = 0;
73         }
74         if (filp_close(f, 0))
75                 CERROR("Error closing LOVTGTS file\n");
76
77 out:
78         pop_ctxt(&saved);
79         RETURN(rc);
80 }
81
82 int mds_get_lovdesc(struct obd_device *obd, struct lov_desc *desc)
83 {
84         struct mds_obd *mds = &obd->u.mds;
85         struct obd_run_ctxt saved;
86         struct file *f;
87         int rc;
88
89         push_ctxt(&saved, &mds->mds_ctxt);
90         f = filp_open("LOVDESC", O_RDONLY, 0644);
91         if (!f || IS_ERR(f)) {
92                 CERROR("Cannot open LOVDESC file\n");
93                 pop_ctxt(&saved);
94                 RETURN(-EIO);
95         }
96
97         rc = lustre_fread(f, (char *)desc, sizeof(*desc), &f->f_pos);
98         if (filp_close(f, 0))
99                 CERROR("Error closing LOVDESC file\n");
100
101         if (rc != sizeof(*desc)) {
102                 CERROR("Cannot read LOVDESC file\n");
103                 pop_ctxt(&saved);
104                 RETURN(-EIO);
105         }
106         pop_ctxt(&saved);
107
108         RETURN(0);
109 }
110
111 int mds_get_lovtgts(struct obd_device *obd, int tgt_count, uuid_t *uuidarray)
112 {
113         struct mds_obd *mds = &obd->u.mds;
114         struct obd_run_ctxt saved;
115         struct file *f;
116         int rc;
117         int rc2;
118
119         push_ctxt(&saved, &mds->mds_ctxt);
120         f = filp_open("LOVTGTS", O_RDONLY, 0644);
121         if (IS_ERR(f)) {
122                 CERROR("Cannot open LOVTGTS file\n");
123                 GOTO(out, rc = PTR_ERR(f));
124         }
125
126         rc = lustre_fread(f, (char *)uuidarray, tgt_count * sizeof(*uuidarray),
127                           &f->f_pos);
128         rc2 = filp_close(f, 0);
129         if (rc2)
130                 CERROR("Error closing LOVTGTS file: rc = %d\n", rc2);
131
132         if (rc != tgt_count * sizeof(*uuidarray)) {
133                 CERROR("Error reading LOVTGTS file: rc = %d\n", rc);
134                 if (rc >= 0)
135                         rc = -EIO;
136                 GOTO(out, rc);
137         } else 
138                 rc = 0;
139         EXIT;
140 out:
141         pop_ctxt(&saved);
142
143         RETURN(rc);
144 }
145
146 int mds_iocontrol(long cmd, struct lustre_handle *conn,
147                           int len, void *karg, void *uarg)
148 {
149         struct obd_device *obd = class_conn2obd(conn);
150         struct obd_ioctl_data *data = karg;
151         struct lov_desc *desc;
152         int count;
153         int rc;
154
155
156         switch (cmd) {
157         case OBD_IOC_LOV_CONFIG:
158                 desc = (struct lov_desc *)data->ioc_inlbuf1;
159                 if (sizeof(*desc) > data->ioc_inllen1) {
160                         CERROR("descriptor size wrong\n");
161                         RETURN(-EINVAL);
162                 }
163
164                 count = desc->ld_tgt_count;
165                 if (sizeof(uuid_t) * count != data->ioc_inllen2) {
166                         CERROR("UUID array size wrong\n");
167                         RETURN(-EINVAL);
168                 }
169                 rc = mds_configure_lov(obd, desc, (uuid_t *)data->ioc_inlbuf2);
170
171                 RETURN(rc);
172         default:
173                 RETURN(-EINVAL);
174         }
175
176         RETURN(0);
177 }