source: rtems/cpukit/libfs/src/jffs2/src/dir-rtems.c

Last change on this file was bcef89f2, checked in by Sebastian Huber <sebastian.huber@…>, on 05/19/23 at 06:18:25

Update company name

The embedded brains GmbH & Co. KG is the legal successor of embedded
brains GmbH.

  • Property mode set to 100644
File size: 11.0 KB
Line 
1#include "rtems-jffs2-config.h"
2
3/*
4 * JFFS2 -- Journalling Flash File System, Version 2.
5 *
6 * Copyright © 2001-2003 Free Software Foundation, Inc.
7 * Copyright © 2001-2007 Red Hat, Inc.
8 * Copyright © 2004-2010 David Woodhouse <dwmw2@infradead.org>
9 * Copyright © 2013 embedded brains GmbH & Co. KG
10 *
11 * Created by David Woodhouse <dwmw2@cambridge.redhat.com>
12 *
13 * Port to the RTEMS by embedded brains GmbH & Co. KG
14 *
15 * For licensing information, see the file 'LICENCE' in this directory.
16 *
17 * $Id: dir-ecos.c,v 1.11 2005/02/08 19:36:27 lunn Exp $
18 *
19 */
20
21#include <linux/kernel.h>
22#include <linux/crc32.h>
23#include "nodelist.h"
24
25/***********************************************************************/
26
27/* Takes length argument because it can be either NUL-terminated or '/'-terminated */
28struct _inode *jffs2_lookup(struct _inode *dir_i, const unsigned char *name, size_t namelen)
29{
30        struct jffs2_inode_info *dir_f;
31        struct jffs2_full_dirent *fd = NULL, *fd_list;
32        uint32_t ino = 0;
33        uint32_t hash = full_name_hash(NULL, name, namelen);
34        struct _inode *inode = NULL;
35
36        D1(printk("jffs2_lookup()\n"));
37
38        dir_f = JFFS2_INODE_INFO(dir_i);
39
40        mutex_lock(&dir_f->sem);
41
42        /* NB: The 2.2 backport will need to explicitly check for '.' and '..' here */
43        for (fd_list = dir_f->dents; fd_list && fd_list->nhash <= hash; fd_list = fd_list->next) {
44                if (fd_list->nhash == hash &&
45                    (!fd || fd_list->version > fd->version) &&
46                    strlen((char *)fd_list->name) == namelen &&
47                    !strncmp((char *)fd_list->name, (char *)name, namelen)) {
48                        fd = fd_list;
49                }
50        }
51        if (fd)
52                ino = fd->ino;
53        mutex_unlock(&dir_f->sem);
54        if (ino) {
55                inode = jffs2_iget(dir_i->i_sb, ino);
56                if (IS_ERR(inode)) {
57                        printk("jffs2_iget() failed for ino #%lu\n", ino);
58                        return inode;
59                } else {
60                        inode->i_fd = fd;
61                }
62        }
63
64        return inode;
65}
66
67/***********************************************************************/
68
69
70
71int jffs2_create(struct _inode *dir_i, const char *d_name, size_t d_namelen, int mode)
72{
73        struct jffs2_raw_inode *ri;
74        struct jffs2_inode_info *f, *dir_f;
75        struct jffs2_sb_info *c;
76        struct _inode *inode;
77        int ret;
78        struct qstr qstr;
79
80        qstr.name = d_name;
81        qstr.len = d_namelen;
82
83        ri = jffs2_alloc_raw_inode();
84        if (!ri)
85                return -ENOMEM;
86
87        c = JFFS2_SB_INFO(dir_i->i_sb);
88
89        D1(printk(KERN_DEBUG "jffs2_create()\n"));
90
91        inode = jffs2_new_inode(dir_i, mode, ri);
92
93        if (IS_ERR(inode)) {
94                D1(printk(KERN_DEBUG "jffs2_new_inode() failed\n"));
95                jffs2_free_raw_inode(ri);
96                return PTR_ERR(inode);
97        }
98
99        f = JFFS2_INODE_INFO(inode);
100        dir_f = JFFS2_INODE_INFO(dir_i);
101
102        ret = jffs2_do_create(c, dir_f, f, ri, &qstr);
103
104        if (ret) {
105                inode->i_nlink = 0;
106                jffs2_iput(inode);
107                jffs2_free_raw_inode(ri);
108                return ret;
109        }
110
111        dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(ri->ctime));
112
113        jffs2_free_raw_inode(ri);
114
115        D1(printk(KERN_DEBUG "jffs2_create: Created ino #%lu with mode %o, nlink %d(%d)\n",
116                  inode->i_ino, inode->i_mode, inode->i_nlink, f->inocache->pino_nlink));
117        jffs2_iput(inode);
118        return 0;
119}
120
121/***********************************************************************/
122
123
124int jffs2_unlink(struct _inode *dir_i, struct _inode *d_inode, const unsigned char *d_name, size_t d_namelen)
125{
126        struct jffs2_sb_info *c = JFFS2_SB_INFO(dir_i->i_sb);
127        struct jffs2_inode_info *dir_f = JFFS2_INODE_INFO(dir_i);
128        struct jffs2_inode_info *dead_f = JFFS2_INODE_INFO(d_inode);
129        int ret;
130
131        ret = jffs2_do_unlink(c, dir_f, (const char *)d_name,
132                               d_namelen, dead_f, get_seconds());
133        if (dead_f->inocache)
134                d_inode->i_nlink = dead_f->inocache->pino_nlink;
135        return ret;
136}
137/***********************************************************************/
138
139
140int jffs2_link (struct _inode *old_d_inode, struct _inode *dir_i, const unsigned char *d_name, size_t d_namelen)
141{
142        struct jffs2_sb_info *c = JFFS2_SB_INFO(old_d_inode->i_sb);
143        struct jffs2_inode_info *f = JFFS2_INODE_INFO(old_d_inode);
144        struct jffs2_inode_info *dir_f = JFFS2_INODE_INFO(dir_i);
145        int ret;
146
147        /* XXX: This is ugly */
148        uint8_t type = (old_d_inode->i_mode & S_IFMT) >> 12;
149        if (!type) type = DT_REG;
150
151        ret = jffs2_do_link(c, dir_f, f->inocache->ino, type,
152                            (const char * )d_name,
153                            d_namelen, get_seconds());
154
155        if (!ret) {
156                mutex_lock(&f->sem);
157                old_d_inode->i_nlink = ++f->inocache->pino_nlink;
158                mutex_unlock(&f->sem);
159        }
160        return ret;
161}
162
163/***********************************************************************/
164
165int jffs2_mknod(
166        struct _inode *dir_i,
167        const unsigned char *d_name,
168        size_t d_namelen,
169        int mode,
170        const unsigned char *data,
171        size_t datalen
172)
173{
174        struct jffs2_inode_info *f, *dir_f;
175        struct jffs2_sb_info *c;
176        struct _inode *inode;
177        struct jffs2_raw_inode *ri;
178        struct jffs2_raw_dirent *rd;
179        struct jffs2_full_dnode *fn;
180        struct jffs2_full_dirent *fd;
181        uint32_t alloclen;
182        int ret;
183
184        /* FIXME: If you care. We'd need to use frags for the data
185           if it grows much more than this */
186        if (datalen > 254)
187                return -ENAMETOOLONG;
188
189        ri = jffs2_alloc_raw_inode();
190
191        if (!ri)
192                return -ENOMEM;
193
194        c = JFFS2_SB_INFO(dir_i->i_sb);
195
196        /* Try to reserve enough space for both node and dirent.
197         * Just the node will do for now, though
198         */
199        ret = jffs2_reserve_space(c, sizeof(*ri) + datalen, &alloclen,
200                                  ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE);
201
202        if (ret) {
203                jffs2_free_raw_inode(ri);
204                return ret;
205        }
206
207        inode = jffs2_new_inode(dir_i, mode, ri);
208
209        if (IS_ERR(inode)) {
210                jffs2_free_raw_inode(ri);
211                jffs2_complete_reservation(c);
212                return PTR_ERR(inode);
213        }
214
215        f = JFFS2_INODE_INFO(inode);
216
217        mutex_lock(&f->sem);
218
219        inode->i_size = datalen;
220        ri->isize = ri->dsize = ri->csize = cpu_to_je32(inode->i_size);
221        ri->totlen = cpu_to_je32(sizeof(*ri) + inode->i_size);
222        ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4));
223
224        ri->compr = JFFS2_COMPR_NONE;
225        ri->data_crc = cpu_to_je32(crc32(0, data, datalen));
226        ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
227
228        fn = jffs2_write_dnode(c, f, ri, data, datalen, ALLOC_NORMAL);
229
230        jffs2_free_raw_inode(ri);
231
232        if (IS_ERR(fn)) {
233                /* Eeek. Wave bye bye */
234                mutex_unlock(&f->sem);
235                jffs2_complete_reservation(c);
236                ret = PTR_ERR(fn);
237                goto fail;
238        }
239
240        if (S_ISLNK(mode)) {
241                /* We use f->target field to store the target path. */
242                f->target = kmemdup(data, datalen + 1, GFP_KERNEL);
243                if (!f->target) {
244                        pr_warn("Can't allocate %d bytes of memory\n", datalen + 1);
245                        mutex_unlock(&f->sem);
246                        jffs2_complete_reservation(c);
247                        ret = -ENOMEM;
248                        goto fail;
249                }
250
251                jffs2_dbg(1, "%s(): symlink's target '%s' cached\n",
252                          __func__, (char *)f->target);
253        }
254
255        /* No data here. Only a metadata node, which will be
256           obsoleted by the first data write
257        */
258        f->metadata = fn;
259        mutex_unlock(&f->sem);
260
261        jffs2_complete_reservation(c);
262
263        ret = jffs2_reserve_space(c, sizeof(*rd)+d_namelen, &alloclen,
264                                  ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(d_namelen));
265        if (ret)
266                goto fail;
267
268        rd = jffs2_alloc_raw_dirent();
269        if (!rd) {
270                /* Argh. Now we treat it like a normal delete */
271                jffs2_complete_reservation(c);
272                ret = -ENOMEM;
273                goto fail;
274        }
275
276        dir_f = JFFS2_INODE_INFO(dir_i);
277        mutex_lock(&dir_f->sem);
278
279        rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
280        rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
281        rd->totlen = cpu_to_je32(sizeof(*rd) + d_namelen);
282        rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
283
284        rd->pino = cpu_to_je32(dir_i->i_ino);
285        rd->version = cpu_to_je32(++dir_f->highest_version);
286        rd->ino = cpu_to_je32(inode->i_ino);
287        rd->mctime = cpu_to_je32(get_seconds());
288        rd->nsize = d_namelen;
289
290        /* XXX: This is ugly. */
291        rd->type = (mode & S_IFMT) >> 12;
292
293        rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
294        rd->name_crc = cpu_to_je32(crc32(0, d_name, d_namelen));
295
296        fd = jffs2_write_dirent(c, dir_f, rd, d_name, d_namelen, ALLOC_NORMAL);
297
298        if (IS_ERR(fd)) {
299                /* dirent failed to write. Delete the inode normally
300                   as if it were the final unlink() */
301                jffs2_complete_reservation(c);
302                jffs2_free_raw_dirent(rd);
303                mutex_unlock(&dir_f->sem);
304                ret = PTR_ERR(fd);
305                goto fail;
306        }
307
308        dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(rd->mctime));
309
310        jffs2_free_raw_dirent(rd);
311
312        /* Link the fd into the inode's list, obsoleting an old
313           one if necessary. */
314        jffs2_add_fd_to_list(c, fd, &dir_f->dents);
315
316        mutex_unlock(&dir_f->sem);
317        jffs2_complete_reservation(c);
318
319 fail:
320        jffs2_iput(inode);
321
322        return ret;
323}
324
325int jffs2_rmdir (struct _inode *dir_i, struct _inode *d_inode, const unsigned char *d_name, size_t d_namelen)
326{
327        struct jffs2_inode_info *f = JFFS2_INODE_INFO(d_inode);
328        struct jffs2_full_dirent *fd;
329
330        for (fd = f->dents ; fd; fd = fd->next) {
331                if (fd->ino)
332                        return -ENOTEMPTY;
333        }
334        return jffs2_unlink(dir_i, d_inode, d_name, d_namelen);
335}
336
337int jffs2_rename (struct _inode *old_dir_i, struct _inode *d_inode, const unsigned char *old_d_name, size_t old_d_namelen,
338                  struct _inode *new_dir_i, const unsigned char *new_d_name, size_t new_d_namelen)
339{
340        int ret;
341        struct jffs2_sb_info *c = JFFS2_SB_INFO(old_dir_i->i_sb);
342        struct jffs2_inode_info *victim_f = NULL;
343        uint8_t type;
344        uint32_t now;
345
346#if 0 /* FIXME -- this really doesn't belong in individual file systems.
347         The fileio code ought to do this for us, or at least part of it */
348        if (new_dentry->d_inode) {
349                if (S_ISDIR(d_inode->i_mode) &&
350                    !S_ISDIR(new_dentry->d_inode->i_mode)) {
351                        /* Cannot rename directory over non-directory */
352                        return -EINVAL;
353                }
354
355                victim_f = JFFS2_INODE_INFO(new_dentry->d_inode);
356
357                if (S_ISDIR(new_dentry->d_inode->i_mode)) {
358                        struct jffs2_full_dirent *fd;
359
360                        if (!S_ISDIR(d_inode->i_mode)) {
361                                /* Cannot rename non-directory over directory */
362                                return -EINVAL;
363                        }
364                        mutex_lock(&victim_f->sem);
365                        for (fd = victim_f->dents; fd; fd = fd->next) {
366                                if (fd->ino) {
367                                        mutex_unlock(&victim_f->sem);
368                                        return -ENOTEMPTY;
369                                }
370                        }
371                        mutex_unlock(&victim_f->sem);
372                }
373        }
374#endif
375
376        /* XXX: We probably ought to alloc enough space for
377           both nodes at the same time. Writing the new link,
378           then getting -ENOSPC, is quite bad :)
379        */
380
381        /* Make a hard link */
382
383        /* XXX: This is ugly */
384        type = (d_inode->i_mode & S_IFMT) >> 12;
385        if (!type) type = DT_REG;
386
387        now = get_seconds();
388        ret = jffs2_do_link(c, JFFS2_INODE_INFO(new_dir_i),
389                            d_inode->i_ino, type,
390                            (const char *)new_d_name,
391                            new_d_namelen, now);
392
393        if (ret)
394                return ret;
395
396        if (victim_f) {
397                /* There was a victim. Kill it off nicely */
398                /* Don't oops if the victim was a dirent pointing to an
399                   inode which didn't exist. */
400                if (victim_f->inocache) {
401                        mutex_lock(&victim_f->sem);
402                        victim_f->inocache->pino_nlink--;
403                        mutex_unlock(&victim_f->sem);
404                }
405        }
406
407        /* Unlink the original */
408        ret = jffs2_do_unlink(c, JFFS2_INODE_INFO(old_dir_i),
409                              (const char *)old_d_name,
410                              old_d_namelen, NULL, now);
411
412        if (ret) {
413                /* Oh shit. We really ought to make a single node which can do both atomically */
414                struct jffs2_inode_info *f = JFFS2_INODE_INFO(d_inode);
415                mutex_lock(&f->sem);
416                if (f->inocache)
417                        d_inode->i_nlink = f->inocache->pino_nlink++;
418                mutex_unlock(&f->sem);
419
420                printk(KERN_NOTICE "jffs2_rename(): Link succeeded, unlink failed (err %d). You now have a hard link\n", ret);
421        }
422        return ret;
423}
Note: See TracBrowser for help on using the repository browser.