Merge branch 'release' of git://git.kernel.org/pub/scm/linux/kernel/git/aegl/linux-2.6
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / mtd / ubi / gluebi.c
blob49cd55ade9c80961ea0d1a622ea37c8726b937e2
1 /*
2 * Copyright (c) International Business Machines Corp., 2006
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
12 * the GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * Author: Artem Bityutskiy (Битюцкий Артём), Joern Engel
22 * This file includes implementation of fake MTD devices for each UBI volume.
23 * This sounds strange, but it is in fact quite useful to make MTD-oriented
24 * software (including all the legacy software) to work on top of UBI.
26 * Gluebi emulates MTD devices of "MTD_UBIVOLUME" type. Their minimal I/O unit
27 * size (mtd->writesize) is equivalent to the UBI minimal I/O unit. The
28 * eraseblock size is equivalent to the logical eraseblock size of the volume.
31 #include <linux/math64.h>
32 #include "ubi.h"
34 /**
35 * gluebi_get_device - get MTD device reference.
36 * @mtd: the MTD device description object
38 * This function is called every time the MTD device is being opened and
39 * implements the MTD get_device() operation. Returns zero in case of success
40 * and a negative error code in case of failure.
42 static int gluebi_get_device(struct mtd_info *mtd)
44 struct ubi_volume *vol;
46 vol = container_of(mtd, struct ubi_volume, gluebi_mtd);
49 * We do not introduce locks for gluebi reference count because the
50 * get_device()/put_device() calls are already serialized at MTD.
52 if (vol->gluebi_refcount > 0) {
54 * The MTD device is already referenced and this is just one
55 * more reference. MTD allows many users to open the same
56 * volume simultaneously and do not distinguish between
57 * readers/writers/exclusive openers as UBI does. So we do not
58 * open the UBI volume again - just increase the reference
59 * counter and return.
61 vol->gluebi_refcount += 1;
62 return 0;
66 * This is the first reference to this UBI volume via the MTD device
67 * interface. Open the corresponding volume in read-write mode.
69 vol->gluebi_desc = ubi_open_volume(vol->ubi->ubi_num, vol->vol_id,
70 UBI_READWRITE);
71 if (IS_ERR(vol->gluebi_desc))
72 return PTR_ERR(vol->gluebi_desc);
73 vol->gluebi_refcount += 1;
74 return 0;
77 /**
78 * gluebi_put_device - put MTD device reference.
79 * @mtd: the MTD device description object
81 * This function is called every time the MTD device is being put. Returns
82 * zero in case of success and a negative error code in case of failure.
84 static void gluebi_put_device(struct mtd_info *mtd)
86 struct ubi_volume *vol;
88 vol = container_of(mtd, struct ubi_volume, gluebi_mtd);
89 vol->gluebi_refcount -= 1;
90 ubi_assert(vol->gluebi_refcount >= 0);
91 if (vol->gluebi_refcount == 0)
92 ubi_close_volume(vol->gluebi_desc);
95 /**
96 * gluebi_read - read operation of emulated MTD devices.
97 * @mtd: MTD device description object
98 * @from: absolute offset from where to read
99 * @len: how many bytes to read
100 * @retlen: count of read bytes is returned here
101 * @buf: buffer to store the read data
103 * This function returns zero in case of success and a negative error code in
104 * case of failure.
106 static int gluebi_read(struct mtd_info *mtd, loff_t from, size_t len,
107 size_t *retlen, unsigned char *buf)
109 int err = 0, lnum, offs, total_read;
110 struct ubi_volume *vol;
111 struct ubi_device *ubi;
113 dbg_gen("read %zd bytes from offset %lld", len, from);
115 if (len < 0 || from < 0 || from + len > mtd->size)
116 return -EINVAL;
118 vol = container_of(mtd, struct ubi_volume, gluebi_mtd);
119 ubi = vol->ubi;
121 lnum = div_u64_rem(from, mtd->erasesize, &offs);
122 total_read = len;
123 while (total_read) {
124 size_t to_read = mtd->erasesize - offs;
126 if (to_read > total_read)
127 to_read = total_read;
129 err = ubi_eba_read_leb(ubi, vol, lnum, buf, offs, to_read, 0);
130 if (err)
131 break;
133 lnum += 1;
134 offs = 0;
135 total_read -= to_read;
136 buf += to_read;
139 *retlen = len - total_read;
140 return err;
144 * gluebi_write - write operation of emulated MTD devices.
145 * @mtd: MTD device description object
146 * @to: absolute offset where to write
147 * @len: how many bytes to write
148 * @retlen: count of written bytes is returned here
149 * @buf: buffer with data to write
151 * This function returns zero in case of success and a negative error code in
152 * case of failure.
154 static int gluebi_write(struct mtd_info *mtd, loff_t to, size_t len,
155 size_t *retlen, const u_char *buf)
157 int err = 0, lnum, offs, total_written;
158 struct ubi_volume *vol;
159 struct ubi_device *ubi;
161 dbg_gen("write %zd bytes to offset %lld", len, to);
163 if (len < 0 || to < 0 || len + to > mtd->size)
164 return -EINVAL;
166 vol = container_of(mtd, struct ubi_volume, gluebi_mtd);
167 ubi = vol->ubi;
169 if (ubi->ro_mode)
170 return -EROFS;
172 lnum = div_u64_rem(to, mtd->erasesize, &offs);
174 if (len % mtd->writesize || offs % mtd->writesize)
175 return -EINVAL;
177 total_written = len;
178 while (total_written) {
179 size_t to_write = mtd->erasesize - offs;
181 if (to_write > total_written)
182 to_write = total_written;
184 err = ubi_eba_write_leb(ubi, vol, lnum, buf, offs, to_write,
185 UBI_UNKNOWN);
186 if (err)
187 break;
189 lnum += 1;
190 offs = 0;
191 total_written -= to_write;
192 buf += to_write;
195 *retlen = len - total_written;
196 return err;
200 * gluebi_erase - erase operation of emulated MTD devices.
201 * @mtd: the MTD device description object
202 * @instr: the erase operation description
204 * This function calls the erase callback when finishes. Returns zero in case
205 * of success and a negative error code in case of failure.
207 static int gluebi_erase(struct mtd_info *mtd, struct erase_info *instr)
209 int err, i, lnum, count;
210 struct ubi_volume *vol;
211 struct ubi_device *ubi;
213 dbg_gen("erase %llu bytes at offset %llu", (unsigned long long)instr->len,
214 (unsigned long long)instr->addr);
216 if (instr->addr < 0 || instr->addr > mtd->size - mtd->erasesize)
217 return -EINVAL;
219 if (instr->len < 0 || instr->addr + instr->len > mtd->size)
220 return -EINVAL;
222 if (mtd_mod_by_ws(instr->addr, mtd) || mtd_mod_by_ws(instr->len, mtd))
223 return -EINVAL;
225 lnum = mtd_div_by_eb(instr->addr, mtd);
226 count = mtd_div_by_eb(instr->len, mtd);
228 vol = container_of(mtd, struct ubi_volume, gluebi_mtd);
229 ubi = vol->ubi;
231 if (ubi->ro_mode)
232 return -EROFS;
234 for (i = 0; i < count; i++) {
235 err = ubi_eba_unmap_leb(ubi, vol, lnum + i);
236 if (err)
237 goto out_err;
241 * MTD erase operations are synchronous, so we have to make sure the
242 * physical eraseblock is wiped out.
244 err = ubi_wl_flush(ubi);
245 if (err)
246 goto out_err;
248 instr->state = MTD_ERASE_DONE;
249 mtd_erase_callback(instr);
250 return 0;
252 out_err:
253 instr->state = MTD_ERASE_FAILED;
254 instr->fail_addr = (long long)lnum * mtd->erasesize;
255 return err;
259 * ubi_create_gluebi - initialize gluebi for an UBI volume.
260 * @ubi: UBI device description object
261 * @vol: volume description object
263 * This function is called when an UBI volume is created in order to create
264 * corresponding fake MTD device. Returns zero in case of success and a
265 * negative error code in case of failure.
267 int ubi_create_gluebi(struct ubi_device *ubi, struct ubi_volume *vol)
269 struct mtd_info *mtd = &vol->gluebi_mtd;
271 mtd->name = kmemdup(vol->name, vol->name_len + 1, GFP_KERNEL);
272 if (!mtd->name)
273 return -ENOMEM;
275 mtd->type = MTD_UBIVOLUME;
276 if (!ubi->ro_mode)
277 mtd->flags = MTD_WRITEABLE;
278 mtd->writesize = ubi->min_io_size;
279 mtd->owner = THIS_MODULE;
280 mtd->erasesize = vol->usable_leb_size;
281 mtd->read = gluebi_read;
282 mtd->write = gluebi_write;
283 mtd->erase = gluebi_erase;
284 mtd->get_device = gluebi_get_device;
285 mtd->put_device = gluebi_put_device;
288 * In case of dynamic volume, MTD device size is just volume size. In
289 * case of a static volume the size is equivalent to the amount of data
290 * bytes.
292 if (vol->vol_type == UBI_DYNAMIC_VOLUME)
293 mtd->size = (long long)vol->usable_leb_size * vol->reserved_pebs;
294 else
295 mtd->size = vol->used_bytes;
297 if (add_mtd_device(mtd)) {
298 ubi_err("cannot not add MTD device");
299 kfree(mtd->name);
300 return -ENFILE;
303 dbg_gen("added mtd%d (\"%s\"), size %llu, EB size %u",
304 mtd->index, mtd->name, (unsigned long long)mtd->size, mtd->erasesize);
305 return 0;
309 * ubi_destroy_gluebi - close gluebi for an UBI volume.
310 * @vol: volume description object
312 * This function is called when an UBI volume is removed in order to remove
313 * corresponding fake MTD device. Returns zero in case of success and a
314 * negative error code in case of failure.
316 int ubi_destroy_gluebi(struct ubi_volume *vol)
318 int err;
319 struct mtd_info *mtd = &vol->gluebi_mtd;
321 dbg_gen("remove mtd%d", mtd->index);
322 err = del_mtd_device(mtd);
323 if (err)
324 return err;
325 kfree(mtd->name);
326 return 0;
330 * ubi_gluebi_updated - UBI volume was updated notifier.
331 * @vol: volume description object
333 * This function is called every time an UBI volume is updated. This function
334 * does nothing if volume @vol is dynamic, and changes MTD device size if the
335 * volume is static. This is needed because static volumes cannot be read past
336 * data they contain.
338 void ubi_gluebi_updated(struct ubi_volume *vol)
340 struct mtd_info *mtd = &vol->gluebi_mtd;
342 if (vol->vol_type == UBI_STATIC_VOLUME)
343 mtd->size = vol->used_bytes;