Coverage Report

Created: 2026-11-13 06:22

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/barebox/fs/ramfs.c
Line
Count
Source
1
/*
2
 * ramfs.c - a malloc based filesystem
3
 *
4
 * Copyright (c) 2007 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
5
 *
6
 * This program is free software; you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License version 2
8
 * as published by the Free Software Foundation.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 */
16
17
#include <common.h>
18
#include <driver.h>
19
#include <init.h>
20
#include <malloc.h>
21
#include <fs.h>
22
#include <command.h>
23
#include <errno.h>
24
#include <linux/stat.h>
25
#include <xfuncs.h>
26
#include <linux/sizes.h>
27
28
#define CHUNK_SIZE  (4096 * 2)
29
30
struct ramfs_chunk {
31
  unsigned long ofs;
32
  int size;
33
  struct list_head list;
34
  char data[];
35
};
36
37
struct ramfs_inode {
38
  struct inode inode;
39
  char *name;
40
  char *symlink;
41
  ulong mode;
42
43
  /* bytes used in this inode */
44
  unsigned long size;
45
  /* bytes currently allocated for this inode */
46
  unsigned long alloc_size;
47
48
  struct list_head data;
49
50
  struct ramfs_chunk *current_chunk;
51
};
52
53
static inline struct ramfs_inode *to_ramfs_inode(struct inode *inode)
54
188k
{
55
188k
  return container_of(inode, struct ramfs_inode, inode);
56
188k
}
57
58
/* ---------------------------------------------------------------*/
59
60
static const struct super_operations ramfs_ops;
61
static const struct inode_operations ramfs_dir_inode_operations;
62
static const struct inode_operations ramfs_file_inode_operations;
63
static const struct inode_operations ramfs_symlink_inode_operations;
64
static const struct file_operations ramfs_file_operations;
65
66
static struct inode *ramfs_get_inode(struct super_block *sb, const struct inode *dir,
67
             umode_t mode)
68
54.0k
{
69
54.0k
  struct inode *inode = new_inode(sb);
70
71
54.0k
  if (!inode)
72
0
    return NULL;
73
74
54.0k
  inode->i_ino = get_next_ino();
75
54.0k
  inode->i_mode = mode;
76
77
54.0k
  switch (mode & S_IFMT) {
78
0
  default:
79
0
    return NULL;
80
14.0k
  case S_IFREG:
81
14.0k
    inode->i_op = &ramfs_file_inode_operations;
82
14.0k
    inode->i_fop = &ramfs_file_operations;
83
14.0k
    break;
84
16.1k
  case S_IFDIR:
85
16.1k
    inode->i_op = &ramfs_dir_inode_operations;
86
16.1k
    inode->i_fop = &simple_dir_operations;
87
16.1k
    inc_nlink(inode);
88
16.1k
    break;
89
8.43k
  case S_IFLNK:
90
8.43k
    inode->i_op = &ramfs_symlink_inode_operations;
91
8.43k
    break;
92
0
  case S_IFBLK:
93
15.5k
  case S_IFCHR:
94
15.5k
    break;
95
54.0k
  }
96
97
54.0k
  return inode;
98
54.0k
}
99
100
69.8k
#define MIN_SIZE SZ_8K
101
102
static struct ramfs_chunk *ramfs_get_chunk(unsigned long size)
103
66.8k
{
104
66.8k
  struct ramfs_chunk *data;
105
106
66.8k
  if (size < MIN_SIZE)
107
3.02k
    size = MIN_SIZE;
108
109
66.8k
  data = calloc(struct_size(data, data, size), 1);
110
66.8k
  if (!data)
111
0
    return NULL;
112
113
66.8k
  data->size = size;
114
115
66.8k
  return data;
116
66.8k
}
117
118
static void ramfs_put_chunk(struct ramfs_chunk *data)
119
65.6k
{
120
65.6k
  free(data);
121
65.6k
}
122
123
/* ---------------------------------------------------------------*/
124
125
static int
126
ramfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode)
127
33.4k
{
128
33.4k
  struct inode *inode = ramfs_get_inode(dir->i_sb, dir, mode);
129
130
33.4k
  if (!inode)
131
0
    return -ENOSPC;
132
133
33.4k
  d_instantiate(dentry, inode);
134
33.4k
  dget(dentry);   /* Extra count - pin the dentry in core */
135
136
33.4k
  return 0;
137
33.4k
}
138
139
static int ramfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
140
16.0k
{
141
16.0k
  int ret;
142
143
16.0k
  ret = ramfs_mknod(dir, dentry, mode | S_IFDIR);
144
16.0k
  if (!ret)
145
16.0k
    inc_nlink(dir);
146
147
16.0k
  return ret;
148
16.0k
}
149
150
static int ramfs_create(struct inode *dir, struct dentry *dentry, umode_t mode)
151
1.79k
{
152
1.79k
  return ramfs_mknod(dir, dentry, mode | S_IFREG);
153
1.79k
}
154
155
static int __ramfs_mknod(struct inode *dir,struct dentry *dentry, umode_t mode,
156
       const char *cdevname)
157
15.5k
{
158
15.5k
  int ret;
159
160
15.5k
  ret = ramfs_mknod(dir, dentry, mode);
161
15.5k
  if (ret)
162
0
    return ret;
163
164
15.5k
  init_special_inode(dentry->d_inode, mode, cdevname);
165
166
15.5k
  return 0;
167
15.5k
}
168
169
static int ramfs_symlink(struct inode *dir, struct dentry *dentry,
170
       const char *symname)
171
8.43k
{
172
8.43k
  struct inode *inode;
173
174
8.43k
  inode = ramfs_get_inode(dir->i_sb, dir, S_IFLNK | S_IRWXG);
175
8.43k
  if (!inode)
176
0
    return -ENOSPC;
177
178
8.43k
  inode->i_link = xstrdup(symname);
179
8.43k
  d_instantiate(dentry, inode);
180
181
8.43k
  return 0;
182
8.43k
}
183
184
static const char *ramfs_get_link(struct dentry *dentry, struct inode *inode)
185
0
{
186
0
  return inode->i_link;
187
0
}
188
189
static const struct inode_operations ramfs_symlink_inode_operations =
190
{
191
  .get_link = ramfs_get_link,
192
};
193
194
static int ramfs_tmpfile(struct inode *dir, struct file *file, umode_t mode)
195
12.2k
{
196
12.2k
  struct inode *inode;
197
198
12.2k
  inode = ramfs_get_inode(dir->i_sb, dir, mode);
199
12.2k
  if (!inode)
200
0
    return -ENOSPC;
201
202
12.2k
  d_tmpfile(file, inode);
203
204
12.2k
  return finish_open_simple(file, 0);
205
12.2k
}
206
207
static const struct inode_operations ramfs_dir_inode_operations =
208
{
209
  .lookup = simple_lookup,
210
  .symlink = ramfs_symlink,
211
  .mkdir = ramfs_mkdir,
212
  .rmdir = simple_rmdir,
213
  .unlink = simple_unlink,
214
  .create = ramfs_create,
215
  .tmpfile = ramfs_tmpfile,
216
  .mknod = __ramfs_mknod,
217
};
218
219
static struct ramfs_chunk *ramfs_find_chunk(struct ramfs_inode *node,
220
              unsigned long pos, int *ofs, int *len)
221
97.6k
{
222
97.6k
  struct ramfs_chunk *data, *cur = node->current_chunk;
223
224
97.6k
  if (cur && pos >= cur->ofs)
225
93.9k
    data = cur;
226
3.74k
  else
227
3.74k
    data = list_first_entry(&node->data, struct ramfs_chunk, list);
228
229
190k
  list_for_each_entry_from(data, &node->data, list) {
230
190k
    if (data->ofs + data->size > pos) {
231
97.6k
      *ofs = pos - data->ofs;
232
97.6k
      *len = data->size - *ofs;
233
234
97.6k
      node->current_chunk = data;
235
236
97.6k
      return data;
237
97.6k
    }
238
190k
  }
239
240
0
  pr_err("%s: no chunk for pos %ld found\n", __func__, pos);
241
242
0
  return NULL;
243
97.6k
}
244
245
static int ramfs_read(struct file *f, void *buf, size_t insize)
246
1.46k
{
247
1.46k
  struct inode *inode = f->f_inode;
248
1.46k
  struct ramfs_inode *node = to_ramfs_inode(inode);
249
1.46k
  struct ramfs_chunk *data;
250
1.46k
  int ofs, len, now;
251
1.46k
  unsigned long pos = f->f_pos;
252
1.46k
  int size = insize;
253
254
1.46k
  pr_vdebug("%s: %p %zu @ %lld\n", __func__, node, insize, f->f_pos);
255
256
32.2k
  while (size) {
257
30.7k
    data = ramfs_find_chunk(node, pos, &ofs, &len);
258
30.7k
    if (!data)
259
0
      return -EINVAL;
260
261
30.7k
    pr_vdebug("%s: pos: %lu ofs: %d len: %d\n", __func__, pos, ofs, len);
262
263
30.7k
    now = min(size, len);
264
265
30.7k
    memcpy(buf, data->data + ofs, now);
266
267
30.7k
    size -= now;
268
30.7k
    buf += now;
269
30.7k
    pos += now;
270
30.7k
  }
271
272
1.46k
  return insize;
273
1.46k
}
274
275
static int ramfs_write(struct file *f, const void *buf, size_t insize)
276
66.9k
{
277
66.9k
  struct inode *inode = f->f_inode;
278
66.9k
  struct ramfs_inode *node = to_ramfs_inode(inode);
279
66.9k
  struct ramfs_chunk *data;
280
66.9k
  int ofs, len, now;
281
66.9k
  unsigned long pos = f->f_pos;
282
66.9k
  int size = insize;
283
284
66.9k
  pr_vdebug("%s: %p %zu @ %lld\n", __func__, node, insize, f->f_pos);
285
286
133k
  while (size) {
287
66.8k
    data = ramfs_find_chunk(node, pos, &ofs, &len);
288
66.8k
    if (!data)
289
0
      return -EINVAL;
290
291
66.8k
    pr_vdebug("%s: pos: %lu ofs: %d len: %d\n", __func__, pos, ofs, len);
292
293
66.8k
    now = min(size, len);
294
295
66.8k
    memcpy(data->data + ofs, buf, now);
296
297
66.8k
    size -= now;
298
66.8k
    buf += now;
299
66.8k
    pos += now;
300
66.8k
  }
301
302
66.9k
  return insize;
303
66.9k
}
304
305
static void ramfs_truncate_down(struct ramfs_inode *node, unsigned long size)
306
51.7k
{
307
51.7k
  struct ramfs_chunk *data, *tmp;
308
309
65.6k
  list_for_each_entry_safe(data, tmp, &node->data, list) {
310
65.6k
    if (data->ofs >= size) {
311
65.6k
      list_del(&data->list);
312
65.6k
      node->alloc_size -= data->size;
313
65.6k
      ramfs_put_chunk(data);
314
65.6k
    }
315
65.6k
  }
316
317
51.7k
  node->current_chunk = NULL;
318
51.7k
}
319
320
static int ramfs_truncate_up(struct ramfs_inode *node, unsigned long size)
321
66.8k
{
322
66.8k
  struct ramfs_chunk *data, *tmp;
323
66.8k
  LIST_HEAD(list);
324
66.8k
  unsigned long add = size - node->alloc_size;
325
66.8k
  unsigned long chunksize = add;
326
66.8k
  unsigned long alloc_size = 0;
327
328
66.8k
  if (node->alloc_size >= size)
329
38
    return 0;
330
331
  /*
332
   * We first try to allocate all space we need in a single chunk.
333
   * This may fail because of fragmented memory, so in case we cannot
334
   * allocate memory we successively decrease the chunk size until
335
   * we have enough allocations made.
336
   */
337
66.8k
  while (1) {
338
66.8k
    unsigned long now = min(chunksize, add);
339
340
66.8k
    data = ramfs_get_chunk(now);
341
66.8k
    if (!data) {
342
      /* No luck, try with smaller chunk size */
343
0
      chunksize >>= 1;
344
345
      /* If we do not have even 128KiB then go out */
346
0
      if (chunksize < SZ_128K)
347
0
        goto out;
348
349
0
      continue;
350
0
    }
351
352
66.8k
    data->ofs = node->alloc_size + alloc_size;
353
354
66.8k
    alloc_size += data->size;
355
356
66.8k
    list_add_tail(&data->list, &list);
357
358
66.8k
    if (add <= data->size)
359
66.8k
      break;
360
361
0
    add -= data->size;
362
0
  }
363
364
66.8k
  list_splice_tail(&list, &node->data);
365
366
66.8k
  node->alloc_size += alloc_size;
367
368
66.8k
  return 0;
369
370
0
out:
371
0
  list_for_each_entry_safe(data, tmp, &list, list) {
372
0
    list_del(&data->list);
373
0
    ramfs_put_chunk(data);
374
0
  }
375
376
0
  return -ENOSPC;
377
66.8k
}
378
379
static int ramfs_truncate(struct file *f, loff_t size)
380
68.7k
{
381
68.7k
  struct inode *inode = f->f_inode;
382
68.7k
  struct ramfs_inode *node = to_ramfs_inode(inode);
383
68.7k
  int ret;
384
385
  /*
386
   * This is a malloc based filesystem, no need to support more
387
   * memory than fits into unsigned long.
388
   */
389
68.7k
  if (size > ULONG_MAX)
390
0
    return -ENOSPC;
391
392
68.7k
  pr_vdebug("%s: %p cur: %ld new: %lld alloc: %ld\n", __func__, node,
393
68.7k
         node->size, size, node->alloc_size);
394
395
68.7k
  if (size == node->size)
396
1.79k
    return 0;
397
398
66.9k
  if (size < node->size) {
399
60
    ramfs_truncate_down(node, size);
400
66.8k
  } else {
401
66.8k
    ret = ramfs_truncate_up(node, size);
402
66.8k
    if (ret)
403
0
      return ret;
404
66.8k
  }
405
406
66.9k
  node->size = size;
407
408
66.9k
  return 0;
409
66.9k
}
410
411
static int ramfs_memmap(struct file *f, void **map, int flags)
412
0
{
413
0
  struct inode *inode = f->f_inode;
414
0
  struct ramfs_inode *node = to_ramfs_inode(inode);
415
0
  struct ramfs_chunk *data;
416
417
0
  if (list_empty(&node->data))
418
0
    return -EINVAL;
419
420
0
  if (!list_is_singular(&node->data))
421
0
    return -EINVAL;
422
423
0
  data = list_first_entry(&node->data, struct ramfs_chunk, list);
424
425
0
  *map = data->data;
426
427
0
  return 0;
428
0
}
429
430
static const struct file_operations ramfs_file_operations = {
431
  .read      = ramfs_read,
432
  .write     = ramfs_write,
433
  .memmap    = ramfs_memmap,
434
  .truncate  = ramfs_truncate,
435
};
436
437
static struct inode *ramfs_alloc_inode(struct super_block *sb)
438
54.0k
{
439
54.0k
  struct ramfs_inode *node;
440
441
54.0k
  node = xzalloc(sizeof(*node));
442
443
54.0k
  INIT_LIST_HEAD(&node->data);
444
445
54.0k
  return &node->inode;
446
54.0k
}
447
448
static void ramfs_destroy_inode(struct inode *inode)
449
51.6k
{
450
51.6k
  struct ramfs_inode *node = to_ramfs_inode(inode);
451
452
51.6k
  ramfs_truncate_down(node, 0);
453
454
51.6k
  free(node);
455
51.6k
}
456
457
static const struct super_operations ramfs_ops = {
458
  .alloc_inode = ramfs_alloc_inode,
459
  .destroy_inode = ramfs_destroy_inode,
460
};
461
462
static int ramfs_probe(struct device *dev)
463
30
{
464
30
  struct inode *inode;
465
30
  struct fs_device *fsdev = dev_to_fs_device(dev);
466
30
  struct super_block *sb = &fsdev->sb;
467
468
30
  sb->s_op = &ramfs_ops;
469
470
30
  inode = ramfs_get_inode(sb, NULL, S_IFDIR);
471
30
  sb->s_root = d_make_root(inode);
472
473
30
  return 0;
474
30
}
475
476
static struct fs_driver ramfs_driver = {
477
  .drv = {
478
    .probe  = ramfs_probe,
479
    .name = "ramfs",
480
  }
481
};
482
483
static int ramfs_init(void)
484
30
{
485
30
  return register_fs_driver(&ramfs_driver);
486
30
}
487
488
coredevice_initcall(ramfs_init);