vmt.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  1. /*
  2. * Copyright (c) International Business Machines Corp., 2006
  3. *
  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.
  8. *
  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.
  13. *
  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
  17. *
  18. * Author: Artem Bityutskiy (Битюцкий Артём)
  19. */
  20. /*
  21. * This file contains implementation of volume creation, deletion, updating and
  22. * resizing.
  23. */
  24. #include <linux/err.h>
  25. #include <linux/math64.h>
  26. #include <linux/slab.h>
  27. #include <linux/export.h>
  28. #include "ubi.h"
  29. static int self_check_volumes(struct ubi_device *ubi);
  30. static ssize_t vol_attribute_show(struct device *dev,
  31. struct device_attribute *attr, char *buf);
  32. /* Device attributes corresponding to files in '/<sysfs>/class/ubi/ubiX_Y' */
  33. static struct device_attribute attr_vol_reserved_ebs =
  34. __ATTR(reserved_ebs, S_IRUGO, vol_attribute_show, NULL);
  35. static struct device_attribute attr_vol_type =
  36. __ATTR(type, S_IRUGO, vol_attribute_show, NULL);
  37. static struct device_attribute attr_vol_name =
  38. __ATTR(name, S_IRUGO, vol_attribute_show, NULL);
  39. static struct device_attribute attr_vol_corrupted =
  40. __ATTR(corrupted, S_IRUGO, vol_attribute_show, NULL);
  41. static struct device_attribute attr_vol_alignment =
  42. __ATTR(alignment, S_IRUGO, vol_attribute_show, NULL);
  43. static struct device_attribute attr_vol_usable_eb_size =
  44. __ATTR(usable_eb_size, S_IRUGO, vol_attribute_show, NULL);
  45. static struct device_attribute attr_vol_data_bytes =
  46. __ATTR(data_bytes, S_IRUGO, vol_attribute_show, NULL);
  47. static struct device_attribute attr_vol_upd_marker =
  48. __ATTR(upd_marker, S_IRUGO, vol_attribute_show, NULL);
  49. /*
  50. * "Show" method for files in '/<sysfs>/class/ubi/ubiX_Y/'.
  51. *
  52. * Consider a situation:
  53. * A. process 1 opens a sysfs file related to volume Y, say
  54. * /<sysfs>/class/ubi/ubiX_Y/reserved_ebs;
  55. * B. process 2 removes volume Y;
  56. * C. process 1 starts reading the /<sysfs>/class/ubi/ubiX_Y/reserved_ebs file;
  57. *
  58. * In this situation, this function will return %-ENODEV because it will find
  59. * out that the volume was removed from the @ubi->volumes array.
  60. */
  61. static ssize_t vol_attribute_show(struct device *dev,
  62. struct device_attribute *attr, char *buf)
  63. {
  64. int ret;
  65. struct ubi_volume *vol = container_of(dev, struct ubi_volume, dev);
  66. struct ubi_device *ubi;
  67. ubi = ubi_get_device(vol->ubi->ubi_num);
  68. if (!ubi)
  69. return -ENODEV;
  70. spin_lock(&ubi->volumes_lock);
  71. if (!ubi->volumes[vol->vol_id]) {
  72. spin_unlock(&ubi->volumes_lock);
  73. ubi_put_device(ubi);
  74. return -ENODEV;
  75. }
  76. /* Take a reference to prevent volume removal */
  77. vol->ref_count += 1;
  78. spin_unlock(&ubi->volumes_lock);
  79. if (attr == &attr_vol_reserved_ebs)
  80. ret = sprintf(buf, "%d\n", vol->reserved_pebs);
  81. else if (attr == &attr_vol_type) {
  82. const char *tp;
  83. if (vol->vol_type == UBI_DYNAMIC_VOLUME)
  84. tp = "dynamic";
  85. else
  86. tp = "static";
  87. ret = sprintf(buf, "%s\n", tp);
  88. } else if (attr == &attr_vol_name)
  89. ret = sprintf(buf, "%s\n", vol->name);
  90. else if (attr == &attr_vol_corrupted)
  91. ret = sprintf(buf, "%d\n", vol->corrupted);
  92. else if (attr == &attr_vol_alignment)
  93. ret = sprintf(buf, "%d\n", vol->alignment);
  94. else if (attr == &attr_vol_usable_eb_size)
  95. ret = sprintf(buf, "%d\n", vol->usable_leb_size);
  96. else if (attr == &attr_vol_data_bytes)
  97. ret = sprintf(buf, "%lld\n", vol->used_bytes);
  98. else if (attr == &attr_vol_upd_marker)
  99. ret = sprintf(buf, "%d\n", vol->upd_marker);
  100. else
  101. /* This must be a bug */
  102. ret = -EINVAL;
  103. /* We've done the operation, drop volume and UBI device references */
  104. spin_lock(&ubi->volumes_lock);
  105. vol->ref_count -= 1;
  106. ubi_assert(vol->ref_count >= 0);
  107. spin_unlock(&ubi->volumes_lock);
  108. ubi_put_device(ubi);
  109. return ret;
  110. }
  111. /* Release method for volume devices */
  112. static void vol_release(struct device *dev)
  113. {
  114. struct ubi_volume *vol = container_of(dev, struct ubi_volume, dev);
  115. kfree(vol->eba_tbl);
  116. kfree(vol);
  117. }
  118. /**
  119. * volume_sysfs_init - initialize sysfs for new volume.
  120. * @ubi: UBI device description object
  121. * @vol: volume description object
  122. *
  123. * This function returns zero in case of success and a negative error code in
  124. * case of failure.
  125. *
  126. * Note, this function does not free allocated resources in case of failure -
  127. * the caller does it. This is because this would cause release() here and the
  128. * caller would oops.
  129. */
  130. static int volume_sysfs_init(struct ubi_device *ubi, struct ubi_volume *vol)
  131. {
  132. int err;
  133. err = device_create_file(&vol->dev, &attr_vol_reserved_ebs);
  134. if (err)
  135. return err;
  136. err = device_create_file(&vol->dev, &attr_vol_type);
  137. if (err)
  138. return err;
  139. err = device_create_file(&vol->dev, &attr_vol_name);
  140. if (err)
  141. return err;
  142. err = device_create_file(&vol->dev, &attr_vol_corrupted);
  143. if (err)
  144. return err;
  145. err = device_create_file(&vol->dev, &attr_vol_alignment);
  146. if (err)
  147. return err;
  148. err = device_create_file(&vol->dev, &attr_vol_usable_eb_size);
  149. if (err)
  150. return err;
  151. err = device_create_file(&vol->dev, &attr_vol_data_bytes);
  152. if (err)
  153. return err;
  154. err = device_create_file(&vol->dev, &attr_vol_upd_marker);
  155. return err;
  156. }
  157. /**
  158. * volume_sysfs_close - close sysfs for a volume.
  159. * @vol: volume description object
  160. */
  161. static void volume_sysfs_close(struct ubi_volume *vol)
  162. {
  163. device_remove_file(&vol->dev, &attr_vol_upd_marker);
  164. device_remove_file(&vol->dev, &attr_vol_data_bytes);
  165. device_remove_file(&vol->dev, &attr_vol_usable_eb_size);
  166. device_remove_file(&vol->dev, &attr_vol_alignment);
  167. device_remove_file(&vol->dev, &attr_vol_corrupted);
  168. device_remove_file(&vol->dev, &attr_vol_name);
  169. device_remove_file(&vol->dev, &attr_vol_type);
  170. device_remove_file(&vol->dev, &attr_vol_reserved_ebs);
  171. device_unregister(&vol->dev);
  172. }
  173. /**
  174. * ubi_create_volume - create volume.
  175. * @ubi: UBI device description object
  176. * @req: volume creation request
  177. *
  178. * This function creates volume described by @req. If @req->vol_id id
  179. * %UBI_VOL_NUM_AUTO, this function automatically assign ID to the new volume
  180. * and saves it in @req->vol_id. Returns zero in case of success and a negative
  181. * error code in case of failure. Note, the caller has to have the
  182. * @ubi->device_mutex locked.
  183. */
  184. int ubi_create_volume(struct ubi_device *ubi, struct ubi_mkvol_req *req)
  185. {
  186. int i, err, vol_id = req->vol_id, do_free = 1;
  187. struct ubi_volume *vol;
  188. struct ubi_vtbl_record vtbl_rec;
  189. dev_t dev;
  190. if (ubi->ro_mode)
  191. return -EROFS;
  192. vol = kzalloc(sizeof(struct ubi_volume), GFP_KERNEL);
  193. if (!vol)
  194. return -ENOMEM;
  195. spin_lock(&ubi->volumes_lock);
  196. if (vol_id == UBI_VOL_NUM_AUTO) {
  197. /* Find unused volume ID */
  198. dbg_gen("search for vacant volume ID");
  199. for (i = 0; i < ubi->vtbl_slots; i++)
  200. if (!ubi->volumes[i]) {
  201. vol_id = i;
  202. break;
  203. }
  204. if (vol_id == UBI_VOL_NUM_AUTO) {
  205. ubi_err("out of volume IDs");
  206. err = -ENFILE;
  207. goto out_unlock;
  208. }
  209. req->vol_id = vol_id;
  210. }
  211. dbg_gen("create device %d, volume %d, %llu bytes, type %d, name %s",
  212. ubi->ubi_num, vol_id, (unsigned long long)req->bytes,
  213. (int)req->vol_type, req->name);
  214. /* Ensure that this volume does not exist */
  215. err = -EEXIST;
  216. if (ubi->volumes[vol_id]) {
  217. ubi_err("volume %d already exists", vol_id);
  218. goto out_unlock;
  219. }
  220. /* Ensure that the name is unique */
  221. for (i = 0; i < ubi->vtbl_slots; i++)
  222. if (ubi->volumes[i] &&
  223. ubi->volumes[i]->name_len == req->name_len &&
  224. !strcmp(ubi->volumes[i]->name, req->name)) {
  225. ubi_err("volume \"%s\" exists (ID %d)", req->name, i);
  226. goto out_unlock;
  227. }
  228. /* Calculate how many eraseblocks are requested */
  229. vol->usable_leb_size = ubi->leb_size - ubi->leb_size % req->alignment;
  230. vol->reserved_pebs += div_u64(req->bytes + vol->usable_leb_size - 1,
  231. vol->usable_leb_size);
  232. /* Reserve physical eraseblocks */
  233. if (vol->reserved_pebs > ubi->avail_pebs) {
  234. ubi_err("not enough PEBs, only %d available", ubi->avail_pebs);
  235. if (ubi->corr_peb_count)
  236. ubi_err("%d PEBs are corrupted and not used",
  237. ubi->corr_peb_count);
  238. err = -ENOSPC;
  239. goto out_unlock;
  240. }
  241. ubi->avail_pebs -= vol->reserved_pebs;
  242. ubi->rsvd_pebs += vol->reserved_pebs;
  243. spin_unlock(&ubi->volumes_lock);
  244. vol->vol_id = vol_id;
  245. vol->alignment = req->alignment;
  246. vol->data_pad = ubi->leb_size % vol->alignment;
  247. vol->vol_type = req->vol_type;
  248. vol->name_len = req->name_len;
  249. memcpy(vol->name, req->name, vol->name_len);
  250. vol->ubi = ubi;
  251. /*
  252. * Finish all pending erases because there may be some LEBs belonging
  253. * to the same volume ID.
  254. */
  255. err = ubi_wl_flush(ubi, vol_id, UBI_ALL);
  256. if (err)
  257. goto out_acc;
  258. vol->eba_tbl = kmalloc(vol->reserved_pebs * sizeof(int), GFP_KERNEL);
  259. if (!vol->eba_tbl) {
  260. err = -ENOMEM;
  261. goto out_acc;
  262. }
  263. for (i = 0; i < vol->reserved_pebs; i++)
  264. vol->eba_tbl[i] = UBI_LEB_UNMAPPED;
  265. if (vol->vol_type == UBI_DYNAMIC_VOLUME) {
  266. vol->used_ebs = vol->reserved_pebs;
  267. vol->last_eb_bytes = vol->usable_leb_size;
  268. vol->used_bytes =
  269. (long long)vol->used_ebs * vol->usable_leb_size;
  270. } else {
  271. vol->used_ebs = div_u64_rem(vol->used_bytes,
  272. vol->usable_leb_size,
  273. &vol->last_eb_bytes);
  274. if (vol->last_eb_bytes != 0)
  275. vol->used_ebs += 1;
  276. else
  277. vol->last_eb_bytes = vol->usable_leb_size;
  278. }
  279. /* Register character device for the volume */
  280. cdev_init(&vol->cdev, &ubi_vol_cdev_operations);
  281. vol->cdev.owner = THIS_MODULE;
  282. dev = MKDEV(MAJOR(ubi->cdev.dev), vol_id + 1);
  283. err = cdev_add(&vol->cdev, dev, 1);
  284. if (err) {
  285. ubi_err("cannot add character device");
  286. goto out_mapping;
  287. }
  288. vol->dev.release = vol_release;
  289. vol->dev.parent = &ubi->dev;
  290. vol->dev.devt = dev;
  291. vol->dev.class = ubi_class;
  292. dev_set_name(&vol->dev, "%s_%d", ubi->ubi_name, vol->vol_id);
  293. err = device_register(&vol->dev);
  294. if (err) {
  295. ubi_err("cannot register device");
  296. goto out_cdev;
  297. }
  298. err = volume_sysfs_init(ubi, vol);
  299. if (err)
  300. goto out_sysfs;
  301. /* Fill volume table record */
  302. memset(&vtbl_rec, 0, sizeof(struct ubi_vtbl_record));
  303. vtbl_rec.reserved_pebs = cpu_to_be32(vol->reserved_pebs);
  304. vtbl_rec.alignment = cpu_to_be32(vol->alignment);
  305. vtbl_rec.data_pad = cpu_to_be32(vol->data_pad);
  306. vtbl_rec.name_len = cpu_to_be16(vol->name_len);
  307. if (vol->vol_type == UBI_DYNAMIC_VOLUME)
  308. vtbl_rec.vol_type = UBI_VID_DYNAMIC;
  309. else
  310. vtbl_rec.vol_type = UBI_VID_STATIC;
  311. memcpy(vtbl_rec.name, vol->name, vol->name_len);
  312. err = ubi_change_vtbl_record(ubi, vol_id, &vtbl_rec);
  313. if (err)
  314. goto out_sysfs;
  315. spin_lock(&ubi->volumes_lock);
  316. ubi->volumes[vol_id] = vol;
  317. ubi->vol_count += 1;
  318. spin_unlock(&ubi->volumes_lock);
  319. ubi_volume_notify(ubi, vol, UBI_VOLUME_ADDED);
  320. self_check_volumes(ubi);
  321. return err;
  322. out_sysfs:
  323. /*
  324. * We have registered our device, we should not free the volume
  325. * description object in this function in case of an error - it is
  326. * freed by the release function.
  327. *
  328. * Get device reference to prevent the release function from being
  329. * called just after sysfs has been closed.
  330. */
  331. do_free = 0;
  332. get_device(&vol->dev);
  333. volume_sysfs_close(vol);
  334. out_cdev:
  335. cdev_del(&vol->cdev);
  336. out_mapping:
  337. if (do_free)
  338. kfree(vol->eba_tbl);
  339. out_acc:
  340. spin_lock(&ubi->volumes_lock);
  341. ubi->rsvd_pebs -= vol->reserved_pebs;
  342. ubi->avail_pebs += vol->reserved_pebs;
  343. out_unlock:
  344. spin_unlock(&ubi->volumes_lock);
  345. if (do_free)
  346. kfree(vol);
  347. else
  348. put_device(&vol->dev);
  349. ubi_err("cannot create volume %d, error %d", vol_id, err);
  350. return err;
  351. }
  352. /**
  353. * ubi_remove_volume - remove volume.
  354. * @desc: volume descriptor
  355. * @no_vtbl: do not change volume table if not zero
  356. *
  357. * This function removes volume described by @desc. The volume has to be opened
  358. * in "exclusive" mode. Returns zero in case of success and a negative error
  359. * code in case of failure. The caller has to have the @ubi->device_mutex
  360. * locked.
  361. */
  362. int ubi_remove_volume(struct ubi_volume_desc *desc, int no_vtbl)
  363. {
  364. struct ubi_volume *vol = desc->vol;
  365. struct ubi_device *ubi = vol->ubi;
  366. int i, err, vol_id = vol->vol_id, reserved_pebs = vol->reserved_pebs;
  367. dbg_gen("remove device %d, volume %d", ubi->ubi_num, vol_id);
  368. ubi_assert(desc->mode == UBI_EXCLUSIVE);
  369. ubi_assert(vol == ubi->volumes[vol_id]);
  370. if (ubi->ro_mode)
  371. return -EROFS;
  372. spin_lock(&ubi->volumes_lock);
  373. if (vol->ref_count > 1) {
  374. /*
  375. * The volume is busy, probably someone is reading one of its
  376. * sysfs files.
  377. */
  378. err = -EBUSY;
  379. goto out_unlock;
  380. }
  381. ubi->volumes[vol_id] = NULL;
  382. spin_unlock(&ubi->volumes_lock);
  383. if (!no_vtbl) {
  384. err = ubi_change_vtbl_record(ubi, vol_id, NULL);
  385. if (err)
  386. goto out_err;
  387. }
  388. for (i = 0; i < vol->reserved_pebs; i++) {
  389. err = ubi_eba_unmap_leb(ubi, vol, i);
  390. if (err)
  391. goto out_err;
  392. }
  393. cdev_del(&vol->cdev);
  394. volume_sysfs_close(vol);
  395. spin_lock(&ubi->volumes_lock);
  396. ubi->rsvd_pebs -= reserved_pebs;
  397. ubi->avail_pebs += reserved_pebs;
  398. i = ubi->beb_rsvd_level - ubi->beb_rsvd_pebs;
  399. if (i > 0) {
  400. i = ubi->avail_pebs >= i ? i : ubi->avail_pebs;
  401. ubi->avail_pebs -= i;
  402. ubi->rsvd_pebs += i;
  403. ubi->beb_rsvd_pebs += i;
  404. if (i > 0)
  405. ubi_msg("reserve more %d PEBs", i);
  406. }
  407. ubi->vol_count -= 1;
  408. spin_unlock(&ubi->volumes_lock);
  409. ubi_volume_notify(ubi, vol, UBI_VOLUME_REMOVED);
  410. if (!no_vtbl)
  411. self_check_volumes(ubi);
  412. return err;
  413. out_err:
  414. ubi_err("cannot remove volume %d, error %d", vol_id, err);
  415. spin_lock(&ubi->volumes_lock);
  416. ubi->volumes[vol_id] = vol;
  417. out_unlock:
  418. spin_unlock(&ubi->volumes_lock);
  419. return err;
  420. }
  421. /**
  422. * ubi_resize_volume - re-size volume.
  423. * @desc: volume descriptor
  424. * @reserved_pebs: new size in physical eraseblocks
  425. *
  426. * This function re-sizes the volume and returns zero in case of success, and a
  427. * negative error code in case of failure. The caller has to have the
  428. * @ubi->device_mutex locked.
  429. */
  430. int ubi_resize_volume(struct ubi_volume_desc *desc, int reserved_pebs)
  431. {
  432. int i, err, pebs, *new_mapping;
  433. struct ubi_volume *vol = desc->vol;
  434. struct ubi_device *ubi = vol->ubi;
  435. struct ubi_vtbl_record vtbl_rec;
  436. int vol_id = vol->vol_id;
  437. if (ubi->ro_mode)
  438. return -EROFS;
  439. dbg_gen("re-size device %d, volume %d to from %d to %d PEBs",
  440. ubi->ubi_num, vol_id, vol->reserved_pebs, reserved_pebs);
  441. if (vol->vol_type == UBI_STATIC_VOLUME &&
  442. reserved_pebs < vol->used_ebs) {
  443. ubi_err("too small size %d, %d LEBs contain data",
  444. reserved_pebs, vol->used_ebs);
  445. return -EINVAL;
  446. }
  447. /* If the size is the same, we have nothing to do */
  448. if (reserved_pebs == vol->reserved_pebs)
  449. return 0;
  450. new_mapping = kmalloc(reserved_pebs * sizeof(int), GFP_KERNEL);
  451. if (!new_mapping)
  452. return -ENOMEM;
  453. for (i = 0; i < reserved_pebs; i++)
  454. new_mapping[i] = UBI_LEB_UNMAPPED;
  455. spin_lock(&ubi->volumes_lock);
  456. if (vol->ref_count > 1) {
  457. spin_unlock(&ubi->volumes_lock);
  458. err = -EBUSY;
  459. goto out_free;
  460. }
  461. spin_unlock(&ubi->volumes_lock);
  462. /* Reserve physical eraseblocks */
  463. pebs = reserved_pebs - vol->reserved_pebs;
  464. if (pebs > 0) {
  465. spin_lock(&ubi->volumes_lock);
  466. if (pebs > ubi->avail_pebs) {
  467. ubi_err("not enough PEBs: requested %d, available %d",
  468. pebs, ubi->avail_pebs);
  469. if (ubi->corr_peb_count)
  470. ubi_err("%d PEBs are corrupted and not used",
  471. ubi->corr_peb_count);
  472. spin_unlock(&ubi->volumes_lock);
  473. err = -ENOSPC;
  474. goto out_free;
  475. }
  476. ubi->avail_pebs -= pebs;
  477. ubi->rsvd_pebs += pebs;
  478. for (i = 0; i < vol->reserved_pebs; i++)
  479. new_mapping[i] = vol->eba_tbl[i];
  480. kfree(vol->eba_tbl);
  481. vol->eba_tbl = new_mapping;
  482. spin_unlock(&ubi->volumes_lock);
  483. }
  484. /* Change volume table record */
  485. memcpy(&vtbl_rec, &ubi->vtbl[vol_id], sizeof(struct ubi_vtbl_record));
  486. vtbl_rec.reserved_pebs = cpu_to_be32(reserved_pebs);
  487. err = ubi_change_vtbl_record(ubi, vol_id, &vtbl_rec);
  488. if (err)
  489. goto out_acc;
  490. if (pebs < 0) {
  491. for (i = 0; i < -pebs; i++) {
  492. err = ubi_eba_unmap_leb(ubi, vol, reserved_pebs + i);
  493. if (err)
  494. goto out_acc;
  495. }
  496. spin_lock(&ubi->volumes_lock);
  497. ubi->rsvd_pebs += pebs;
  498. ubi->avail_pebs -= pebs;
  499. pebs = ubi->beb_rsvd_level - ubi->beb_rsvd_pebs;
  500. if (pebs > 0) {
  501. pebs = ubi->avail_pebs >= pebs ? pebs : ubi->avail_pebs;
  502. ubi->avail_pebs -= pebs;
  503. ubi->rsvd_pebs += pebs;
  504. ubi->beb_rsvd_pebs += pebs;
  505. if (pebs > 0)
  506. ubi_msg("reserve more %d PEBs", pebs);
  507. }
  508. for (i = 0; i < reserved_pebs; i++)
  509. new_mapping[i] = vol->eba_tbl[i];
  510. kfree(vol->eba_tbl);
  511. vol->eba_tbl = new_mapping;
  512. spin_unlock(&ubi->volumes_lock);
  513. }
  514. vol->reserved_pebs = reserved_pebs;
  515. if (vol->vol_type == UBI_DYNAMIC_VOLUME) {
  516. vol->used_ebs = reserved_pebs;
  517. vol->last_eb_bytes = vol->usable_leb_size;
  518. vol->used_bytes =
  519. (long long)vol->used_ebs * vol->usable_leb_size;
  520. }
  521. ubi_volume_notify(ubi, vol, UBI_VOLUME_RESIZED);
  522. self_check_volumes(ubi);
  523. return err;
  524. out_acc:
  525. if (pebs > 0) {
  526. spin_lock(&ubi->volumes_lock);
  527. ubi->rsvd_pebs -= pebs;
  528. ubi->avail_pebs += pebs;
  529. spin_unlock(&ubi->volumes_lock);
  530. }
  531. out_free:
  532. kfree(new_mapping);
  533. return err;
  534. }
  535. /**
  536. * ubi_rename_volumes - re-name UBI volumes.
  537. * @ubi: UBI device description object
  538. * @rename_list: list of &struct ubi_rename_entry objects
  539. *
  540. * This function re-names or removes volumes specified in the re-name list.
  541. * Returns zero in case of success and a negative error code in case of
  542. * failure.
  543. */
  544. int ubi_rename_volumes(struct ubi_device *ubi, struct list_head *rename_list)
  545. {
  546. int err;
  547. struct ubi_rename_entry *re;
  548. err = ubi_vtbl_rename_volumes(ubi, rename_list);
  549. if (err)
  550. return err;
  551. list_for_each_entry(re, rename_list, list) {
  552. if (re->remove) {
  553. err = ubi_remove_volume(re->desc, 1);
  554. if (err)
  555. break;
  556. } else {
  557. struct ubi_volume *vol = re->desc->vol;
  558. spin_lock(&ubi->volumes_lock);
  559. vol->name_len = re->new_name_len;
  560. memcpy(vol->name, re->new_name, re->new_name_len + 1);
  561. spin_unlock(&ubi->volumes_lock);
  562. ubi_volume_notify(ubi, vol, UBI_VOLUME_RENAMED);
  563. }
  564. }
  565. if (!err)
  566. self_check_volumes(ubi);
  567. return err;
  568. }
  569. /**
  570. * ubi_add_volume - add volume.
  571. * @ubi: UBI device description object
  572. * @vol: volume description object
  573. *
  574. * This function adds an existing volume and initializes all its data
  575. * structures. Returns zero in case of success and a negative error code in
  576. * case of failure.
  577. */
  578. int ubi_add_volume(struct ubi_device *ubi, struct ubi_volume *vol)
  579. {
  580. int err, vol_id = vol->vol_id;
  581. dev_t dev;
  582. dbg_gen("add volume %d", vol_id);
  583. /* Register character device for the volume */
  584. cdev_init(&vol->cdev, &ubi_vol_cdev_operations);
  585. vol->cdev.owner = THIS_MODULE;
  586. dev = MKDEV(MAJOR(ubi->cdev.dev), vol->vol_id + 1);
  587. err = cdev_add(&vol->cdev, dev, 1);
  588. if (err) {
  589. ubi_err("cannot add character device for volume %d, error %d",
  590. vol_id, err);
  591. return err;
  592. }
  593. vol->dev.release = vol_release;
  594. vol->dev.parent = &ubi->dev;
  595. vol->dev.devt = dev;
  596. vol->dev.class = ubi_class;
  597. dev_set_name(&vol->dev, "%s_%d", ubi->ubi_name, vol->vol_id);
  598. err = device_register(&vol->dev);
  599. if (err)
  600. goto out_cdev;
  601. err = volume_sysfs_init(ubi, vol);
  602. if (err) {
  603. cdev_del(&vol->cdev);
  604. volume_sysfs_close(vol);
  605. return err;
  606. }
  607. self_check_volumes(ubi);
  608. return err;
  609. out_cdev:
  610. cdev_del(&vol->cdev);
  611. return err;
  612. }
  613. /**
  614. * ubi_free_volume - free volume.
  615. * @ubi: UBI device description object
  616. * @vol: volume description object
  617. *
  618. * This function frees all resources for volume @vol but does not remove it.
  619. * Used only when the UBI device is detached.
  620. */
  621. void ubi_free_volume(struct ubi_device *ubi, struct ubi_volume *vol)
  622. {
  623. dbg_gen("free volume %d", vol->vol_id);
  624. ubi->volumes[vol->vol_id] = NULL;
  625. cdev_del(&vol->cdev);
  626. volume_sysfs_close(vol);
  627. }
  628. /**
  629. * self_check_volume - check volume information.
  630. * @ubi: UBI device description object
  631. * @vol_id: volume ID
  632. *
  633. * Returns zero if volume is all right and a a negative error code if not.
  634. */
  635. static int self_check_volume(struct ubi_device *ubi, int vol_id)
  636. {
  637. int idx = vol_id2idx(ubi, vol_id);
  638. int reserved_pebs, alignment, data_pad, vol_type, name_len, upd_marker;
  639. const struct ubi_volume *vol;
  640. long long n;
  641. const char *name;
  642. spin_lock(&ubi->volumes_lock);
  643. reserved_pebs = be32_to_cpu(ubi->vtbl[vol_id].reserved_pebs);
  644. vol = ubi->volumes[idx];
  645. if (!vol) {
  646. if (reserved_pebs) {
  647. ubi_err("no volume info, but volume exists");
  648. goto fail;
  649. }
  650. spin_unlock(&ubi->volumes_lock);
  651. return 0;
  652. }
  653. if (vol->reserved_pebs < 0 || vol->alignment < 0 || vol->data_pad < 0 ||
  654. vol->name_len < 0) {
  655. ubi_err("negative values");
  656. goto fail;
  657. }
  658. if (vol->alignment > ubi->leb_size || vol->alignment == 0) {
  659. ubi_err("bad alignment");
  660. goto fail;
  661. }
  662. n = vol->alignment & (ubi->min_io_size - 1);
  663. if (vol->alignment != 1 && n) {
  664. ubi_err("alignment is not multiple of min I/O unit");
  665. goto fail;
  666. }
  667. n = ubi->leb_size % vol->alignment;
  668. if (vol->data_pad != n) {
  669. ubi_err("bad data_pad, has to be %lld", n);
  670. goto fail;
  671. }
  672. if (vol->vol_type != UBI_DYNAMIC_VOLUME &&
  673. vol->vol_type != UBI_STATIC_VOLUME) {
  674. ubi_err("bad vol_type");
  675. goto fail;
  676. }
  677. if (vol->upd_marker && vol->corrupted) {
  678. ubi_err("update marker and corrupted simultaneously");
  679. goto fail;
  680. }
  681. if (vol->reserved_pebs > ubi->good_peb_count) {
  682. ubi_err("too large reserved_pebs");
  683. goto fail;
  684. }
  685. n = ubi->leb_size - vol->data_pad;
  686. if (vol->usable_leb_size != ubi->leb_size - vol->data_pad) {
  687. ubi_err("bad usable_leb_size, has to be %lld", n);
  688. goto fail;
  689. }
  690. if (vol->name_len > UBI_VOL_NAME_MAX) {
  691. ubi_err("too long volume name, max is %d", UBI_VOL_NAME_MAX);
  692. goto fail;
  693. }
  694. n = strnlen(vol->name, vol->name_len + 1);
  695. if (n != vol->name_len) {
  696. ubi_err("bad name_len %lld", n);
  697. goto fail;
  698. }
  699. n = (long long)vol->used_ebs * vol->usable_leb_size;
  700. if (vol->vol_type == UBI_DYNAMIC_VOLUME) {
  701. if (vol->corrupted) {
  702. ubi_err("corrupted dynamic volume");
  703. goto fail;
  704. }
  705. if (vol->used_ebs != vol->reserved_pebs) {
  706. ubi_err("bad used_ebs");
  707. goto fail;
  708. }
  709. if (vol->last_eb_bytes != vol->usable_leb_size) {
  710. ubi_err("bad last_eb_bytes");
  711. goto fail;
  712. }
  713. if (vol->used_bytes != n) {
  714. ubi_err("bad used_bytes");
  715. goto fail;
  716. }
  717. } else {
  718. if (vol->used_ebs < 0 || vol->used_ebs > vol->reserved_pebs) {
  719. ubi_err("bad used_ebs");
  720. goto fail;
  721. }
  722. if (vol->last_eb_bytes < 0 ||
  723. vol->last_eb_bytes > vol->usable_leb_size) {
  724. ubi_err("bad last_eb_bytes");
  725. goto fail;
  726. }
  727. if (vol->used_bytes < 0 || vol->used_bytes > n ||
  728. vol->used_bytes < n - vol->usable_leb_size) {
  729. ubi_err("bad used_bytes");
  730. goto fail;
  731. }
  732. }
  733. alignment = be32_to_cpu(ubi->vtbl[vol_id].alignment);
  734. data_pad = be32_to_cpu(ubi->vtbl[vol_id].data_pad);
  735. name_len = be16_to_cpu(ubi->vtbl[vol_id].name_len);
  736. upd_marker = ubi->vtbl[vol_id].upd_marker;
  737. name = &ubi->vtbl[vol_id].name[0];
  738. if (ubi->vtbl[vol_id].vol_type == UBI_VID_DYNAMIC)
  739. vol_type = UBI_DYNAMIC_VOLUME;
  740. else
  741. vol_type = UBI_STATIC_VOLUME;
  742. if (alignment != vol->alignment || data_pad != vol->data_pad ||
  743. upd_marker != vol->upd_marker || vol_type != vol->vol_type ||
  744. name_len != vol->name_len || strncmp(name, vol->name, name_len)) {
  745. ubi_err("volume info is different");
  746. goto fail;
  747. }
  748. spin_unlock(&ubi->volumes_lock);
  749. return 0;
  750. fail:
  751. ubi_err("self-check failed for volume %d", vol_id);
  752. if (vol)
  753. ubi_dump_vol_info(vol);
  754. ubi_dump_vtbl_record(&ubi->vtbl[vol_id], vol_id);
  755. dump_stack();
  756. spin_unlock(&ubi->volumes_lock);
  757. return -EINVAL;
  758. }
  759. /**
  760. * self_check_volumes - check information about all volumes.
  761. * @ubi: UBI device description object
  762. *
  763. * Returns zero if volumes are all right and a a negative error code if not.
  764. */
  765. static int self_check_volumes(struct ubi_device *ubi)
  766. {
  767. int i, err = 0;
  768. if (!ubi->dbg->chk_gen)
  769. return 0;
  770. for (i = 0; i < ubi->vtbl_slots; i++) {
  771. err = self_check_volume(ubi, i);
  772. if (err)
  773. break;
  774. }
  775. return err;
  776. }