1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2021 LG Electronics.
5 * Author(s): Hyunchul Lee <hyc.lee@gmail.com>
13 #include "exfat_ondisk.h"
17 #include "exfat_dir.h"
19 struct exfat_inode *exfat_alloc_inode(__u16 attr)
21 struct exfat_inode *node;
24 size = offsetof(struct exfat_inode, name) + NAME_BUFFER_SIZE;
25 node = (struct exfat_inode *)calloc(1, size);
27 exfat_err("failed to allocate exfat_node\n");
32 INIT_LIST_HEAD(&node->children);
33 INIT_LIST_HEAD(&node->sibling);
34 INIT_LIST_HEAD(&node->list);
40 void exfat_free_inode(struct exfat_inode *node)
44 free(node->dentry_set);
49 void exfat_free_children(struct exfat_inode *dir, bool file_only)
51 struct exfat_inode *node, *i;
53 list_for_each_entry_safe(node, i, &dir->children, sibling) {
55 if (!(node->attr & ATTR_SUBDIR)) {
56 list_del(&node->sibling);
57 exfat_free_inode(node);
60 list_del(&node->sibling);
61 list_del(&node->list);
62 exfat_free_inode(node);
67 void exfat_free_file_children(struct exfat_inode *dir)
69 exfat_free_children(dir, true);
72 /* delete @child and all ancestors that does not have
75 void exfat_free_ancestors(struct exfat_inode *child)
77 struct exfat_inode *parent;
79 while (child && list_empty(&child->children)) {
80 if (!child->parent || !(child->attr & ATTR_SUBDIR))
83 parent = child->parent;
84 list_del(&child->sibling);
85 exfat_free_inode(child);
92 void exfat_free_dir_list(struct exfat *exfat)
94 struct exfat_inode *dir, *i;
96 list_for_each_entry_safe(dir, i, &exfat->dir_list, list) {
99 exfat_free_file_children(dir);
100 list_del(&dir->list);
101 exfat_free_inode(dir);
105 void exfat_free_exfat(struct exfat *exfat)
110 if (exfat->alloc_bitmap)
111 free(exfat->alloc_bitmap);
112 if (exfat->disk_bitmap)
113 free(exfat->disk_bitmap);
114 if (exfat->ohead_bitmap)
115 free(exfat->ohead_bitmap);
116 if (exfat->upcase_table)
117 free(exfat->upcase_table);
119 exfat_free_inode(exfat->root);
120 if (exfat->zero_cluster)
121 free(exfat->zero_cluster);
126 struct exfat *exfat_alloc_exfat(struct exfat_blk_dev *blk_dev, struct pbr *bs)
130 exfat = (struct exfat *)calloc(1, sizeof(*exfat));
134 INIT_LIST_HEAD(&exfat->dir_list);
135 exfat->blk_dev = blk_dev;
137 exfat->clus_count = le32_to_cpu(bs->bsx.clu_count);
138 exfat->clus_size = EXFAT_CLUSTER_SIZE(bs);
139 exfat->sect_size = EXFAT_SECTOR_SIZE(bs);
141 /* TODO: bitmap could be very large. */
142 exfat->alloc_bitmap = (char *)calloc(1,
143 EXFAT_BITMAP_SIZE(exfat->clus_count));
144 if (!exfat->alloc_bitmap) {
145 exfat_err("failed to allocate bitmap\n");
149 exfat->ohead_bitmap =
150 calloc(1, EXFAT_BITMAP_SIZE(exfat->clus_count));
151 if (!exfat->ohead_bitmap) {
152 exfat_err("failed to allocate bitmap\n");
157 calloc(1, EXFAT_BITMAP_SIZE(exfat->clus_count));
158 if (!exfat->disk_bitmap) {
159 exfat_err("failed to allocate bitmap\n");
163 exfat->zero_cluster = calloc(1, exfat->clus_size);
164 if (!exfat->zero_cluster) {
165 exfat_err("failed to allocate a zero-filled cluster buffer\n");
169 exfat->start_clu = EXFAT_FIRST_CLUSTER;
172 exfat_free_exfat(exfat);
176 struct buffer_desc *exfat_alloc_buffer(int count,
177 unsigned int clu_size, unsigned int sect_size)
179 struct buffer_desc *bd;
182 bd = (struct buffer_desc *)calloc(sizeof(*bd), count);
186 for (i = 0; i < count; i++) {
187 bd[i].buffer = (char *)malloc(clu_size);
190 bd[i].dirty = (char *)calloc(clu_size / sect_size, 1);
196 exfat_free_buffer(bd, count);
200 void exfat_free_buffer(struct buffer_desc *bd, int count)
204 for (i = 0; i < count; i++) {
214 * get references of ancestors that include @child until the count of
215 * ancesters is not larger than @count and the count of characters of
216 * their names is not larger than @max_char_len.
217 * return true if root is reached.
219 static bool get_ancestors(struct exfat_inode *child,
220 struct exfat_inode **ancestors, int count,
224 struct exfat_inode *dir;
225 int name_len, char_len;
226 int root_depth, depth, i;
234 name_len = exfat_utf16_len(dir->name, NAME_BUFFER_SIZE);
235 if (char_len + name_len > max_char_len)
239 char_len += name_len + 1;
245 depth = MIN(root_depth, count);
247 for (dir = child, i = depth - 1; i >= 0; dir = dir->parent, i--)
250 *ancestor_count = depth;
254 int exfat_resolve_path(struct path_resolve_ctx *ctx, struct exfat_inode *child)
259 static const __le16 utf16_slash = cpu_to_le16(0x002F);
260 static const __le16 utf16_null = cpu_to_le16(0x0000);
263 ctx->local_path[0] = '\0';
267 sizeof(ctx->ancestors) / sizeof(ctx->ancestors[0]),
271 utf16_path = ctx->utf16_path;
272 for (i = 0; i < depth; i++) {
273 name_len = exfat_utf16_len(ctx->ancestors[i]->name,
275 memcpy((char *)utf16_path, (char *)ctx->ancestors[i]->name,
277 utf16_path += name_len;
278 memcpy((char *)utf16_path, &utf16_slash, sizeof(utf16_slash));
284 memcpy((char *)utf16_path, &utf16_null, sizeof(utf16_null));
287 in_size = (utf16_path - ctx->utf16_path) * sizeof(__le16);
288 return exfat_utf16_dec(ctx->utf16_path, in_size,
289 ctx->local_path, sizeof(ctx->local_path));
292 int exfat_resolve_path_parent(struct path_resolve_ctx *ctx,
293 struct exfat_inode *parent, struct exfat_inode *child)
296 struct exfat_inode *old;
299 child->parent = parent;
301 ret = exfat_resolve_path(ctx, child);