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

4.115
Last change on this file since 0282e83 was 3c96bee, checked in by Sebastian Huber <sebastian.huber@…>, on 09/12/13 at 13:32:07

JFFS2: Add RTEMS support

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