Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Copyright (c) International Business Machines Corp., 2006
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Author: Artem Bityutskiy (Битюцкий Артём)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) /* This file mostly implements UBI kernel API functions */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/namei.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <asm/div64.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include "ubi.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  * ubi_do_get_device_info - get information about UBI device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  * @ubi: UBI device description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  * @di: the information is stored here
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  * This function is the same as 'ubi_get_device_info()', but it assumes the UBI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  * device is locked and cannot disappear.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) void ubi_do_get_device_info(struct ubi_device *ubi, struct ubi_device_info *di)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	di->ubi_num = ubi->ubi_num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	di->leb_size = ubi->leb_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	di->leb_start = ubi->leb_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	di->min_io_size = ubi->min_io_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	di->max_write_size = ubi->max_write_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	di->ro_mode = ubi->ro_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	di->cdev = ubi->cdev.dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) EXPORT_SYMBOL_GPL(ubi_do_get_device_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39)  * ubi_get_device_info - get information about UBI device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40)  * @ubi_num: UBI device number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41)  * @di: the information is stored here
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43)  * This function returns %0 in case of success, %-EINVAL if the UBI device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44)  * number is invalid, and %-ENODEV if there is no such UBI device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) int ubi_get_device_info(int ubi_num, struct ubi_device_info *di)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	struct ubi_device *ubi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	ubi = ubi_get_device(ubi_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	if (!ubi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	ubi_do_get_device_info(ubi, di);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	ubi_put_device(ubi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) EXPORT_SYMBOL_GPL(ubi_get_device_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62)  * ubi_do_get_volume_info - get information about UBI volume.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63)  * @ubi: UBI device description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64)  * @vol: volume description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65)  * @vi: the information is stored here
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) void ubi_do_get_volume_info(struct ubi_device *ubi, struct ubi_volume *vol,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 			    struct ubi_volume_info *vi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	vi->vol_id = vol->vol_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	vi->ubi_num = ubi->ubi_num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	vi->size = vol->reserved_pebs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	vi->used_bytes = vol->used_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	vi->vol_type = vol->vol_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	vi->corrupted = vol->corrupted;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	vi->upd_marker = vol->upd_marker;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	vi->alignment = vol->alignment;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	vi->usable_leb_size = vol->usable_leb_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	vi->name_len = vol->name_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	vi->name = vol->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	vi->cdev = vol->cdev.dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85)  * ubi_get_volume_info - get information about UBI volume.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86)  * @desc: volume descriptor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87)  * @vi: the information is stored here
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) void ubi_get_volume_info(struct ubi_volume_desc *desc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 			 struct ubi_volume_info *vi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	ubi_do_get_volume_info(desc->vol->ubi, desc->vol, vi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) EXPORT_SYMBOL_GPL(ubi_get_volume_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97)  * ubi_open_volume - open UBI volume.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98)  * @ubi_num: UBI device number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99)  * @vol_id: volume ID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)  * @mode: open mode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)  * The @mode parameter specifies if the volume should be opened in read-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)  * mode, read-write mode, or exclusive mode. The exclusive mode guarantees that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)  * nobody else will be able to open this volume. UBI allows to have many volume
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)  * readers and one writer at a time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)  * If a static volume is being opened for the first time since boot, it will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)  * checked by this function, which means it will be fully read and the CRC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)  * checksum of each logical eraseblock will be checked.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)  * This function returns volume descriptor in case of success and a negative
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)  * error code in case of failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) struct ubi_volume_desc *ubi_open_volume(int ubi_num, int vol_id, int mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	struct ubi_volume_desc *desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	struct ubi_device *ubi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	struct ubi_volume *vol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	dbg_gen("open device %d, volume %d, mode %d", ubi_num, vol_id, mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	if (mode != UBI_READONLY && mode != UBI_READWRITE &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	    mode != UBI_EXCLUSIVE && mode != UBI_METAONLY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	 * First of all, we have to get the UBI device to prevent its removal.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	ubi = ubi_get_device(ubi_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	if (!ubi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		return ERR_PTR(-ENODEV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	if (vol_id < 0 || vol_id >= ubi->vtbl_slots) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 		goto out_put_ubi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	desc = kmalloc(sizeof(struct ubi_volume_desc), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	if (!desc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 		err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 		goto out_put_ubi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	err = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	if (!try_module_get(THIS_MODULE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	spin_lock(&ubi->volumes_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	vol = ubi->volumes[vol_id];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	if (!vol)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	err = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	switch (mode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	case UBI_READONLY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 		if (vol->exclusive)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 			goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 		vol->readers += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	case UBI_READWRITE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 		if (vol->exclusive || vol->writers > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 			goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 		vol->writers += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	case UBI_EXCLUSIVE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 		if (vol->exclusive || vol->writers || vol->readers ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 		    vol->metaonly)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 			goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 		vol->exclusive = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	case UBI_METAONLY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 		if (vol->metaonly || vol->exclusive)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 			goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 		vol->metaonly = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	get_device(&vol->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	vol->ref_count += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	spin_unlock(&ubi->volumes_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	desc->vol = vol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	desc->mode = mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	mutex_lock(&ubi->ckvol_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	if (!vol->checked && !vol->skip_check) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 		/* This is the first open - check the volume */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 		err = ubi_check_volume(ubi, vol_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 		if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 			mutex_unlock(&ubi->ckvol_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 			ubi_close_volume(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 			return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 		if (err == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 			ubi_warn(ubi, "volume %d on UBI device %d is corrupted",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 				 vol_id, ubi->ubi_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 			vol->corrupted = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 		vol->checked = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	mutex_unlock(&ubi->ckvol_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	return desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	spin_unlock(&ubi->volumes_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	module_put(THIS_MODULE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) out_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	kfree(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) out_put_ubi:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	ubi_err(ubi, "cannot open device %d, volume %d, error %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 		ubi_num, vol_id, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	ubi_put_device(ubi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) EXPORT_SYMBOL_GPL(ubi_open_volume);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225)  * ubi_open_volume_nm - open UBI volume by name.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)  * @ubi_num: UBI device number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)  * @name: volume name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)  * @mode: open mode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)  * This function is similar to 'ubi_open_volume()', but opens a volume by name.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) struct ubi_volume_desc *ubi_open_volume_nm(int ubi_num, const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 					   int mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	int i, vol_id = -1, len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	struct ubi_device *ubi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	struct ubi_volume_desc *ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	dbg_gen("open device %d, volume %s, mode %d", ubi_num, name, mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	if (!name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 		return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	len = strnlen(name, UBI_VOL_NAME_MAX + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	if (len > UBI_VOL_NAME_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 		return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 		return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	ubi = ubi_get_device(ubi_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	if (!ubi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 		return ERR_PTR(-ENODEV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	spin_lock(&ubi->volumes_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	/* Walk all volumes of this UBI device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	for (i = 0; i < ubi->vtbl_slots; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 		struct ubi_volume *vol = ubi->volumes[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 		if (vol && len == vol->name_len && !strcmp(name, vol->name)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 			vol_id = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	spin_unlock(&ubi->volumes_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	if (vol_id >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 		ret = ubi_open_volume(ubi_num, vol_id, mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 		ret = ERR_PTR(-ENODEV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	 * We should put the UBI device even in case of success, because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	 * 'ubi_open_volume()' took a reference as well.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	ubi_put_device(ubi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) EXPORT_SYMBOL_GPL(ubi_open_volume_nm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)  * ubi_open_volume_path - open UBI volume by its character device node path.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283)  * @pathname: volume character device node path
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284)  * @mode: open mode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)  * This function is similar to 'ubi_open_volume()', but opens a volume the path
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287)  * to its character device node.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) struct ubi_volume_desc *ubi_open_volume_path(const char *pathname, int mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	int error, ubi_num, vol_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	struct path path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	struct kstat stat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	dbg_gen("open volume %s, mode %d", pathname, mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	if (!pathname || !*pathname)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 		return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	error = kern_path(pathname, LOOKUP_FOLLOW, &path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 		return ERR_PTR(error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	error = vfs_getattr(&path, &stat, STATX_TYPE, AT_STATX_SYNC_AS_STAT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	path_put(&path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 		return ERR_PTR(error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	if (!S_ISCHR(stat.mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 		return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	ubi_num = ubi_major2num(MAJOR(stat.rdev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	vol_id = MINOR(stat.rdev) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	if (vol_id >= 0 && ubi_num >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 		return ubi_open_volume(ubi_num, vol_id, mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	return ERR_PTR(-ENODEV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) EXPORT_SYMBOL_GPL(ubi_open_volume_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322)  * ubi_close_volume - close UBI volume.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323)  * @desc: volume descriptor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) void ubi_close_volume(struct ubi_volume_desc *desc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	struct ubi_volume *vol = desc->vol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	struct ubi_device *ubi = vol->ubi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	dbg_gen("close device %d, volume %d, mode %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 		ubi->ubi_num, vol->vol_id, desc->mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	spin_lock(&ubi->volumes_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	switch (desc->mode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	case UBI_READONLY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 		vol->readers -= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	case UBI_READWRITE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 		vol->writers -= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	case UBI_EXCLUSIVE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 		vol->exclusive = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	case UBI_METAONLY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 		vol->metaonly = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	vol->ref_count -= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	spin_unlock(&ubi->volumes_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	kfree(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	put_device(&vol->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	ubi_put_device(ubi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	module_put(THIS_MODULE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) EXPORT_SYMBOL_GPL(ubi_close_volume);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359)  * leb_read_sanity_check - does sanity checks on read requests.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360)  * @desc: volume descriptor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361)  * @lnum: logical eraseblock number to read from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362)  * @offset: offset within the logical eraseblock to read from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363)  * @len: how many bytes to read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365)  * This function is used by ubi_leb_read() and ubi_leb_read_sg()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366)  * to perform sanity checks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) static int leb_read_sanity_check(struct ubi_volume_desc *desc, int lnum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 				 int offset, int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	struct ubi_volume *vol = desc->vol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	struct ubi_device *ubi = vol->ubi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	int vol_id = vol->vol_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	if (vol_id < 0 || vol_id >= ubi->vtbl_slots || lnum < 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	    lnum >= vol->used_ebs || offset < 0 || len < 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	    offset + len > vol->usable_leb_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	if (vol->vol_type == UBI_STATIC_VOLUME) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 		if (vol->used_ebs == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 			/* Empty static UBI volume */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 		if (lnum == vol->used_ebs - 1 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 		    offset + len > vol->last_eb_bytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	if (vol->upd_marker)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 		return -EBADF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396)  * ubi_leb_read - read data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397)  * @desc: volume descriptor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398)  * @lnum: logical eraseblock number to read from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399)  * @buf: buffer where to store the read data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400)  * @offset: offset within the logical eraseblock to read from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401)  * @len: how many bytes to read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402)  * @check: whether UBI has to check the read data's CRC or not.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404)  * This function reads data from offset @offset of logical eraseblock @lnum and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405)  * stores the data at @buf. When reading from static volumes, @check specifies
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406)  * whether the data has to be checked or not. If yes, the whole logical
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407)  * eraseblock will be read and its CRC checksum will be checked (i.e., the CRC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408)  * checksum is per-eraseblock). So checking may substantially slow down the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409)  * read speed. The @check argument is ignored for dynamic volumes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411)  * In case of success, this function returns zero. In case of failure, this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412)  * function returns a negative error code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414)  * %-EBADMSG error code is returned:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415)  * o for both static and dynamic volumes if MTD driver has detected a data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416)  *   integrity problem (unrecoverable ECC checksum mismatch in case of NAND);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417)  * o for static volumes in case of data CRC mismatch.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419)  * If the volume is damaged because of an interrupted update this function just
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420)  * returns immediately with %-EBADF error code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) int ubi_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 		 int len, int check)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	struct ubi_volume *vol = desc->vol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	struct ubi_device *ubi = vol->ubi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	int err, vol_id = vol->vol_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	dbg_gen("read %d bytes from LEB %d:%d:%d", len, vol_id, lnum, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 	err = leb_read_sanity_check(desc, lnum, offset, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 	if (len == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 	err = ubi_eba_read_leb(ubi, vol, lnum, buf, offset, len, check);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 	if (err && mtd_is_eccerr(err) && vol->vol_type == UBI_STATIC_VOLUME) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 		ubi_warn(ubi, "mark volume %d as corrupted", vol_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 		vol->corrupted = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) EXPORT_SYMBOL_GPL(ubi_leb_read);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450)  * ubi_leb_read_sg - read data into a scatter gather list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451)  * @desc: volume descriptor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452)  * @lnum: logical eraseblock number to read from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453)  * @buf: buffer where to store the read data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454)  * @offset: offset within the logical eraseblock to read from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455)  * @len: how many bytes to read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456)  * @check: whether UBI has to check the read data's CRC or not.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458)  * This function works exactly like ubi_leb_read_sg(). But instead of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459)  * storing the read data into a buffer it writes to an UBI scatter gather
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460)  * list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) int ubi_leb_read_sg(struct ubi_volume_desc *desc, int lnum, struct ubi_sgl *sgl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 		    int offset, int len, int check)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 	struct ubi_volume *vol = desc->vol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 	struct ubi_device *ubi = vol->ubi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 	int err, vol_id = vol->vol_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	dbg_gen("read %d bytes from LEB %d:%d:%d", len, vol_id, lnum, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 	err = leb_read_sanity_check(desc, lnum, offset, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 	if (len == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 	err = ubi_eba_read_leb_sg(ubi, vol, sgl, lnum, offset, len, check);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 	if (err && mtd_is_eccerr(err) && vol->vol_type == UBI_STATIC_VOLUME) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 		ubi_warn(ubi, "mark volume %d as corrupted", vol_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 		vol->corrupted = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) EXPORT_SYMBOL_GPL(ubi_leb_read_sg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489)  * ubi_leb_write - write data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490)  * @desc: volume descriptor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491)  * @lnum: logical eraseblock number to write to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492)  * @buf: data to write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493)  * @offset: offset within the logical eraseblock where to write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494)  * @len: how many bytes to write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496)  * This function writes @len bytes of data from @buf to offset @offset of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497)  * logical eraseblock @lnum.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499)  * This function takes care of physical eraseblock write failures. If write to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500)  * the physical eraseblock write operation fails, the logical eraseblock is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501)  * re-mapped to another physical eraseblock, the data is recovered, and the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502)  * write finishes. UBI has a pool of reserved physical eraseblocks for this.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504)  * If all the data were successfully written, zero is returned. If an error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505)  * occurred and UBI has not been able to recover from it, this function returns
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506)  * a negative error code. Note, in case of an error, it is possible that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507)  * something was still written to the flash media, but that may be some
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508)  * garbage.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510)  * If the volume is damaged because of an interrupted update this function just
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511)  * returns immediately with %-EBADF code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) int ubi_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 		  int offset, int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 	struct ubi_volume *vol = desc->vol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 	struct ubi_device *ubi = vol->ubi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 	int vol_id = vol->vol_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 	dbg_gen("write %d bytes to LEB %d:%d:%d", len, vol_id, lnum, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 	if (vol_id < 0 || vol_id >= ubi->vtbl_slots)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 	if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 		return -EROFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 	if (!ubi_leb_valid(vol, lnum) || offset < 0 || len < 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 	    offset + len > vol->usable_leb_size ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 	    offset & (ubi->min_io_size - 1) || len & (ubi->min_io_size - 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 	if (vol->upd_marker)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 		return -EBADF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 	if (len == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 	return ubi_eba_write_leb(ubi, vol, lnum, buf, offset, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) EXPORT_SYMBOL_GPL(ubi_leb_write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544)  * ubi_leb_change - change logical eraseblock atomically.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545)  * @desc: volume descriptor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546)  * @lnum: logical eraseblock number to change
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547)  * @buf: data to write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548)  * @len: how many bytes to write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550)  * This function changes the contents of a logical eraseblock atomically. @buf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551)  * has to contain new logical eraseblock data, and @len - the length of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552)  * data, which has to be aligned. The length may be shorter than the logical
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553)  * eraseblock size, ant the logical eraseblock may be appended to more times
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554)  * later on. This function guarantees that in case of an unclean reboot the old
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555)  * contents is preserved. Returns zero in case of success and a negative error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556)  * code in case of failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) int ubi_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 		   int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 	struct ubi_volume *vol = desc->vol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 	struct ubi_device *ubi = vol->ubi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 	int vol_id = vol->vol_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 	dbg_gen("atomically write %d bytes to LEB %d:%d", len, vol_id, lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 	if (vol_id < 0 || vol_id >= ubi->vtbl_slots)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 	if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 		return -EROFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 	if (!ubi_leb_valid(vol, lnum) || len < 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 	    len > vol->usable_leb_size || len & (ubi->min_io_size - 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 	if (vol->upd_marker)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 		return -EBADF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 	if (len == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 	return ubi_eba_atomic_leb_change(ubi, vol, lnum, buf, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) EXPORT_SYMBOL_GPL(ubi_leb_change);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588)  * ubi_leb_erase - erase logical eraseblock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589)  * @desc: volume descriptor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590)  * @lnum: logical eraseblock number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592)  * This function un-maps logical eraseblock @lnum and synchronously erases the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593)  * correspondent physical eraseblock. Returns zero in case of success and a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594)  * negative error code in case of failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596)  * If the volume is damaged because of an interrupted update this function just
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597)  * returns immediately with %-EBADF code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) int ubi_leb_erase(struct ubi_volume_desc *desc, int lnum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 	struct ubi_volume *vol = desc->vol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 	struct ubi_device *ubi = vol->ubi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) 	dbg_gen("erase LEB %d:%d", vol->vol_id, lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) 	if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 		return -EROFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) 	if (!ubi_leb_valid(vol, lnum))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) 	if (vol->upd_marker)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 		return -EBADF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) 	err = ubi_eba_unmap_leb(ubi, vol, lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) 	return ubi_wl_flush(ubi, vol->vol_id, lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) EXPORT_SYMBOL_GPL(ubi_leb_erase);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625)  * ubi_leb_unmap - un-map logical eraseblock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626)  * @desc: volume descriptor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627)  * @lnum: logical eraseblock number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629)  * This function un-maps logical eraseblock @lnum and schedules the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630)  * corresponding physical eraseblock for erasure, so that it will eventually be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631)  * physically erased in background. This operation is much faster than the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632)  * erase operation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634)  * Unlike erase, the un-map operation does not guarantee that the logical
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635)  * eraseblock will contain all 0xFF bytes when UBI is initialized again. For
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636)  * example, if several logical eraseblocks are un-mapped, and an unclean reboot
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637)  * happens after this, the logical eraseblocks will not necessarily be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638)  * un-mapped again when this MTD device is attached. They may actually be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639)  * mapped to the same physical eraseblocks again. So, this function has to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640)  * used with care.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642)  * In other words, when un-mapping a logical eraseblock, UBI does not store
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643)  * any information about this on the flash media, it just marks the logical
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644)  * eraseblock as "un-mapped" in RAM. If UBI is detached before the physical
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645)  * eraseblock is physically erased, it will be mapped again to the same logical
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646)  * eraseblock when the MTD device is attached again.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648)  * The main and obvious use-case of this function is when the contents of a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649)  * logical eraseblock has to be re-written. Then it is much more efficient to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650)  * first un-map it, then write new data, rather than first erase it, then write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651)  * new data. Note, once new data has been written to the logical eraseblock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652)  * UBI guarantees that the old contents has gone forever. In other words, if an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653)  * unclean reboot happens after the logical eraseblock has been un-mapped and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654)  * then written to, it will contain the last written data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656)  * This function returns zero in case of success and a negative error code in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657)  * case of failure. If the volume is damaged because of an interrupted update
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658)  * this function just returns immediately with %-EBADF code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) int ubi_leb_unmap(struct ubi_volume_desc *desc, int lnum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) 	struct ubi_volume *vol = desc->vol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) 	struct ubi_device *ubi = vol->ubi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) 	dbg_gen("unmap LEB %d:%d", vol->vol_id, lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) 	if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) 		return -EROFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) 	if (!ubi_leb_valid(vol, lnum))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) 	if (vol->upd_marker)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) 		return -EBADF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) 	return ubi_eba_unmap_leb(ubi, vol, lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) EXPORT_SYMBOL_GPL(ubi_leb_unmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681)  * ubi_leb_map - map logical eraseblock to a physical eraseblock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682)  * @desc: volume descriptor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683)  * @lnum: logical eraseblock number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685)  * This function maps an un-mapped logical eraseblock @lnum to a physical
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686)  * eraseblock. This means, that after a successful invocation of this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687)  * function the logical eraseblock @lnum will be empty (contain only %0xFF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688)  * bytes) and be mapped to a physical eraseblock, even if an unclean reboot
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689)  * happens.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691)  * This function returns zero in case of success, %-EBADF if the volume is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692)  * damaged because of an interrupted update, %-EBADMSG if the logical
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693)  * eraseblock is already mapped, and other negative error codes in case of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694)  * other failures.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) int ubi_leb_map(struct ubi_volume_desc *desc, int lnum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) 	struct ubi_volume *vol = desc->vol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) 	struct ubi_device *ubi = vol->ubi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) 	dbg_gen("map LEB %d:%d", vol->vol_id, lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) 	if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) 		return -EROFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) 	if (!ubi_leb_valid(vol, lnum))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) 	if (vol->upd_marker)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) 		return -EBADF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) 	if (ubi_eba_is_mapped(vol, lnum))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) 		return -EBADMSG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) 	return ubi_eba_write_leb(ubi, vol, lnum, NULL, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) EXPORT_SYMBOL_GPL(ubi_leb_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720)  * ubi_is_mapped - check if logical eraseblock is mapped.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721)  * @desc: volume descriptor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722)  * @lnum: logical eraseblock number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724)  * This function checks if logical eraseblock @lnum is mapped to a physical
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725)  * eraseblock. If a logical eraseblock is un-mapped, this does not necessarily
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726)  * mean it will still be un-mapped after the UBI device is re-attached. The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727)  * logical eraseblock may become mapped to the physical eraseblock it was last
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728)  * mapped to.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730)  * This function returns %1 if the LEB is mapped, %0 if not, and a negative
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731)  * error code in case of failure. If the volume is damaged because of an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732)  * interrupted update this function just returns immediately with %-EBADF error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733)  * code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) int ubi_is_mapped(struct ubi_volume_desc *desc, int lnum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) 	struct ubi_volume *vol = desc->vol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) 	dbg_gen("test LEB %d:%d", vol->vol_id, lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) 	if (!ubi_leb_valid(vol, lnum))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) 	if (vol->upd_marker)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) 		return -EBADF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) 	return ubi_eba_is_mapped(vol, lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) EXPORT_SYMBOL_GPL(ubi_is_mapped);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752)  * ubi_sync - synchronize UBI device buffers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753)  * @ubi_num: UBI device to synchronize
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755)  * The underlying MTD device may cache data in hardware or in software. This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756)  * function ensures the caches are flushed. Returns zero in case of success and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757)  * a negative error code in case of failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) int ubi_sync(int ubi_num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) 	struct ubi_device *ubi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) 	ubi = ubi_get_device(ubi_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) 	if (!ubi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) 	mtd_sync(ubi->mtd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) 	ubi_put_device(ubi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) EXPORT_SYMBOL_GPL(ubi_sync);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774)  * ubi_flush - flush UBI work queue.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775)  * @ubi_num: UBI device to flush work queue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776)  * @vol_id: volume id to flush for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777)  * @lnum: logical eraseblock number to flush for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779)  * This function executes all pending works for a particular volume id / logical
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780)  * eraseblock number pair. If either value is set to %UBI_ALL, then it acts as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781)  * a wildcard for all of the corresponding volume numbers or logical
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782)  * eraseblock numbers. It returns zero in case of success and a negative error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783)  * code in case of failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) int ubi_flush(int ubi_num, int vol_id, int lnum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) 	struct ubi_device *ubi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) 	ubi = ubi_get_device(ubi_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) 	if (!ubi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) 	err = ubi_wl_flush(ubi, vol_id, lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) 	ubi_put_device(ubi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) EXPORT_SYMBOL_GPL(ubi_flush);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) BLOCKING_NOTIFIER_HEAD(ubi_notifiers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803)  * ubi_register_volume_notifier - register a volume notifier.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804)  * @nb: the notifier description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805)  * @ignore_existing: if non-zero, do not send "added" notification for all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806)  *                   already existing volumes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808)  * This function registers a volume notifier, which means that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809)  * 'nb->notifier_call()' will be invoked when an UBI  volume is created,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810)  * removed, re-sized, re-named, or updated. The first argument of the function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811)  * is the notification type. The second argument is pointer to a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812)  * &struct ubi_notification object which describes the notification event.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813)  * Using UBI API from the volume notifier is prohibited.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815)  * This function returns zero in case of success and a negative error code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816)  * in case of failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) int ubi_register_volume_notifier(struct notifier_block *nb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) 				 int ignore_existing)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) 	err = blocking_notifier_chain_register(&ubi_notifiers, nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) 	if (err != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) 	if (ignore_existing)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) 	 * We are going to walk all UBI devices and all volumes, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) 	 * notify the user about existing volumes by the %UBI_VOLUME_ADDED
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) 	 * event. We have to lock the @ubi_devices_mutex to make sure UBI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) 	 * devices do not disappear.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) 	mutex_lock(&ubi_devices_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) 	ubi_enumerate_volumes(nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) 	mutex_unlock(&ubi_devices_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) EXPORT_SYMBOL_GPL(ubi_register_volume_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844)  * ubi_unregister_volume_notifier - unregister the volume notifier.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845)  * @nb: the notifier description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847)  * This function unregisters volume notifier @nm and returns zero in case of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848)  * success and a negative error code in case of failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) int ubi_unregister_volume_notifier(struct notifier_block *nb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) 	return blocking_notifier_chain_unregister(&ubi_notifiers, nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) EXPORT_SYMBOL_GPL(ubi_unregister_volume_notifier);