Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * JFFS2 -- Journalling Flash File System, Version 2.
0003  *
0004  * Copyright © 2001-2007 Red Hat, Inc.
0005  *
0006  * Created by David Woodhouse <dwmw2@infradead.org>
0007  *
0008  * For licensing information, see the file 'LICENCE' in this directory.
0009  *
0010  */
0011 
0012 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0013 
0014 #include <linux/kernel.h>
0015 #include <linux/fs.h>
0016 #include <linux/crc32.h>
0017 #include <linux/pagemap.h>
0018 #include <linux/mtd/mtd.h>
0019 #include "nodelist.h"
0020 #include "compr.h"
0021 
0022 
0023 int jffs2_do_new_inode(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
0024                uint32_t mode, struct jffs2_raw_inode *ri)
0025 {
0026     struct jffs2_inode_cache *ic;
0027 
0028     ic = jffs2_alloc_inode_cache();
0029     if (!ic) {
0030         return -ENOMEM;
0031     }
0032 
0033     memset(ic, 0, sizeof(*ic));
0034 
0035     f->inocache = ic;
0036     f->inocache->pino_nlink = 1; /* Will be overwritten shortly for directories */
0037     f->inocache->nodes = (struct jffs2_raw_node_ref *)f->inocache;
0038     f->inocache->state = INO_STATE_PRESENT;
0039 
0040     jffs2_add_ino_cache(c, f->inocache);
0041     jffs2_dbg(1, "%s(): Assigned ino# %d\n", __func__, f->inocache->ino);
0042     ri->ino = cpu_to_je32(f->inocache->ino);
0043 
0044     ri->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
0045     ri->nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE);
0046     ri->totlen = cpu_to_je32(PAD(sizeof(*ri)));
0047     ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4));
0048     ri->mode = cpu_to_jemode(mode);
0049 
0050     f->highest_version = 1;
0051     ri->version = cpu_to_je32(f->highest_version);
0052 
0053     return 0;
0054 }
0055 
0056 /* jffs2_write_dnode - given a raw_inode, allocate a full_dnode for it,
0057    write it to the flash, link it into the existing inode/fragment list */
0058 
0059 struct jffs2_full_dnode *jffs2_write_dnode(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
0060                        struct jffs2_raw_inode *ri, const unsigned char *data,
0061                        uint32_t datalen, int alloc_mode)
0062 
0063 {
0064     struct jffs2_full_dnode *fn;
0065     size_t retlen;
0066     uint32_t flash_ofs;
0067     struct kvec vecs[2];
0068     int ret;
0069     int retried = 0;
0070     unsigned long cnt = 2;
0071 
0072     D1(if(je32_to_cpu(ri->hdr_crc) != crc32(0, ri, sizeof(struct jffs2_unknown_node)-4)) {
0073         pr_crit("Eep. CRC not correct in jffs2_write_dnode()\n");
0074         BUG();
0075     }
0076        );
0077     vecs[0].iov_base = ri;
0078     vecs[0].iov_len = sizeof(*ri);
0079     vecs[1].iov_base = (unsigned char *)data;
0080     vecs[1].iov_len = datalen;
0081 
0082     if (je32_to_cpu(ri->totlen) != sizeof(*ri) + datalen) {
0083         pr_warn("%s(): ri->totlen (0x%08x) != sizeof(*ri) (0x%08zx) + datalen (0x%08x)\n",
0084             __func__, je32_to_cpu(ri->totlen),
0085             sizeof(*ri), datalen);
0086     }
0087 
0088     fn = jffs2_alloc_full_dnode();
0089     if (!fn)
0090         return ERR_PTR(-ENOMEM);
0091 
0092     /* check number of valid vecs */
0093     if (!datalen || !data)
0094         cnt = 1;
0095  retry:
0096     flash_ofs = write_ofs(c);
0097 
0098     jffs2_dbg_prewrite_paranoia_check(c, flash_ofs, vecs[0].iov_len + vecs[1].iov_len);
0099 
0100     if ((alloc_mode!=ALLOC_GC) && (je32_to_cpu(ri->version) < f->highest_version)) {
0101         BUG_ON(!retried);
0102         jffs2_dbg(1, "%s(): dnode_version %d, highest version %d -> updating dnode\n",
0103               __func__,
0104               je32_to_cpu(ri->version), f->highest_version);
0105         ri->version = cpu_to_je32(++f->highest_version);
0106         ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
0107     }
0108 
0109     ret = jffs2_flash_writev(c, vecs, cnt, flash_ofs, &retlen,
0110                  (alloc_mode==ALLOC_GC)?0:f->inocache->ino);
0111 
0112     if (ret || (retlen != sizeof(*ri) + datalen)) {
0113         pr_notice("Write of %zd bytes at 0x%08x failed. returned %d, retlen %zd\n",
0114               sizeof(*ri) + datalen, flash_ofs, ret, retlen);
0115 
0116         /* Mark the space as dirtied */
0117         if (retlen) {
0118             /* Don't change raw->size to match retlen. We may have
0119                written the node header already, and only the data will
0120                seem corrupted, in which case the scan would skip over
0121                any node we write before the original intended end of
0122                this node */
0123             jffs2_add_physical_node_ref(c, flash_ofs | REF_OBSOLETE, PAD(sizeof(*ri)+datalen), NULL);
0124         } else {
0125             pr_notice("Not marking the space at 0x%08x as dirty because the flash driver returned retlen zero\n",
0126                   flash_ofs);
0127         }
0128         if (!retried && alloc_mode != ALLOC_NORETRY) {
0129             /* Try to reallocate space and retry */
0130             uint32_t dummy;
0131             struct jffs2_eraseblock *jeb = &c->blocks[flash_ofs / c->sector_size];
0132 
0133             retried = 1;
0134 
0135             jffs2_dbg(1, "Retrying failed write.\n");
0136 
0137             jffs2_dbg_acct_sanity_check(c,jeb);
0138             jffs2_dbg_acct_paranoia_check(c, jeb);
0139 
0140             if (alloc_mode == ALLOC_GC) {
0141                 ret = jffs2_reserve_space_gc(c, sizeof(*ri) + datalen, &dummy,
0142                                  JFFS2_SUMMARY_INODE_SIZE);
0143             } else {
0144                 /* Locking pain */
0145                 mutex_unlock(&f->sem);
0146                 jffs2_complete_reservation(c);
0147 
0148                 ret = jffs2_reserve_space(c, sizeof(*ri) + datalen, &dummy,
0149                               alloc_mode, JFFS2_SUMMARY_INODE_SIZE);
0150                 mutex_lock(&f->sem);
0151             }
0152 
0153             if (!ret) {
0154                 flash_ofs = write_ofs(c);
0155                 jffs2_dbg(1, "Allocated space at 0x%08x to retry failed write.\n",
0156                       flash_ofs);
0157 
0158                 jffs2_dbg_acct_sanity_check(c,jeb);
0159                 jffs2_dbg_acct_paranoia_check(c, jeb);
0160 
0161                 goto retry;
0162             }
0163             jffs2_dbg(1, "Failed to allocate space to retry failed write: %d!\n",
0164                   ret);
0165         }
0166         /* Release the full_dnode which is now useless, and return */
0167         jffs2_free_full_dnode(fn);
0168         return ERR_PTR(ret?ret:-EIO);
0169     }
0170     /* Mark the space used */
0171     /* If node covers at least a whole page, or if it starts at the
0172        beginning of a page and runs to the end of the file, or if
0173        it's a hole node, mark it REF_PRISTINE, else REF_NORMAL.
0174     */
0175     if ((je32_to_cpu(ri->dsize) >= PAGE_SIZE) ||
0176         ( ((je32_to_cpu(ri->offset)&(PAGE_SIZE-1))==0) &&
0177           (je32_to_cpu(ri->dsize)+je32_to_cpu(ri->offset) ==  je32_to_cpu(ri->isize)))) {
0178         flash_ofs |= REF_PRISTINE;
0179     } else {
0180         flash_ofs |= REF_NORMAL;
0181     }
0182     fn->raw = jffs2_add_physical_node_ref(c, flash_ofs, PAD(sizeof(*ri)+datalen), f->inocache);
0183     if (IS_ERR(fn->raw)) {
0184         void *hold_err = fn->raw;
0185         /* Release the full_dnode which is now useless, and return */
0186         jffs2_free_full_dnode(fn);
0187         return ERR_CAST(hold_err);
0188     }
0189     fn->ofs = je32_to_cpu(ri->offset);
0190     fn->size = je32_to_cpu(ri->dsize);
0191     fn->frags = 0;
0192 
0193     jffs2_dbg(1, "jffs2_write_dnode wrote node at 0x%08x(%d) with dsize 0x%x, csize 0x%x, node_crc 0x%08x, data_crc 0x%08x, totlen 0x%08x\n",
0194           flash_ofs & ~3, flash_ofs & 3, je32_to_cpu(ri->dsize),
0195           je32_to_cpu(ri->csize), je32_to_cpu(ri->node_crc),
0196           je32_to_cpu(ri->data_crc), je32_to_cpu(ri->totlen));
0197 
0198     if (retried) {
0199         jffs2_dbg_acct_sanity_check(c,NULL);
0200     }
0201 
0202     return fn;
0203 }
0204 
0205 struct jffs2_full_dirent *jffs2_write_dirent(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
0206                          struct jffs2_raw_dirent *rd, const unsigned char *name,
0207                          uint32_t namelen, int alloc_mode)
0208 {
0209     struct jffs2_full_dirent *fd;
0210     size_t retlen;
0211     struct kvec vecs[2];
0212     uint32_t flash_ofs;
0213     int retried = 0;
0214     int ret;
0215 
0216     jffs2_dbg(1, "%s(ino #%u, name at *0x%p \"%s\"->ino #%u, name_crc 0x%08x)\n",
0217           __func__,
0218           je32_to_cpu(rd->pino), name, name, je32_to_cpu(rd->ino),
0219           je32_to_cpu(rd->name_crc));
0220 
0221     D1(if(je32_to_cpu(rd->hdr_crc) != crc32(0, rd, sizeof(struct jffs2_unknown_node)-4)) {
0222         pr_crit("Eep. CRC not correct in jffs2_write_dirent()\n");
0223         BUG();
0224        });
0225 
0226     if (strnlen(name, namelen) != namelen) {
0227         /* This should never happen, but seems to have done on at least one
0228            occasion: https://dev.laptop.org/ticket/4184 */
0229         pr_crit("Error in jffs2_write_dirent() -- name contains zero bytes!\n");
0230         pr_crit("Directory inode #%u, name at *0x%p \"%s\"->ino #%u, name_crc 0x%08x\n",
0231             je32_to_cpu(rd->pino), name, name, je32_to_cpu(rd->ino),
0232             je32_to_cpu(rd->name_crc));
0233         WARN_ON(1);
0234         return ERR_PTR(-EIO);
0235     }
0236 
0237     vecs[0].iov_base = rd;
0238     vecs[0].iov_len = sizeof(*rd);
0239     vecs[1].iov_base = (unsigned char *)name;
0240     vecs[1].iov_len = namelen;
0241 
0242     fd = jffs2_alloc_full_dirent(namelen+1);
0243     if (!fd)
0244         return ERR_PTR(-ENOMEM);
0245 
0246     fd->version = je32_to_cpu(rd->version);
0247     fd->ino = je32_to_cpu(rd->ino);
0248     fd->nhash = full_name_hash(NULL, name, namelen);
0249     fd->type = rd->type;
0250     memcpy(fd->name, name, namelen);
0251     fd->name[namelen]=0;
0252 
0253  retry:
0254     flash_ofs = write_ofs(c);
0255 
0256     jffs2_dbg_prewrite_paranoia_check(c, flash_ofs, vecs[0].iov_len + vecs[1].iov_len);
0257 
0258     if ((alloc_mode!=ALLOC_GC) && (je32_to_cpu(rd->version) < f->highest_version)) {
0259         BUG_ON(!retried);
0260         jffs2_dbg(1, "%s(): dirent_version %d, highest version %d -> updating dirent\n",
0261               __func__,
0262               je32_to_cpu(rd->version), f->highest_version);
0263         rd->version = cpu_to_je32(++f->highest_version);
0264         fd->version = je32_to_cpu(rd->version);
0265         rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
0266     }
0267 
0268     ret = jffs2_flash_writev(c, vecs, 2, flash_ofs, &retlen,
0269                  (alloc_mode==ALLOC_GC)?0:je32_to_cpu(rd->pino));
0270     if (ret || (retlen != sizeof(*rd) + namelen)) {
0271         pr_notice("Write of %zd bytes at 0x%08x failed. returned %d, retlen %zd\n",
0272               sizeof(*rd) + namelen, flash_ofs, ret, retlen);
0273         /* Mark the space as dirtied */
0274         if (retlen) {
0275             jffs2_add_physical_node_ref(c, flash_ofs | REF_OBSOLETE, PAD(sizeof(*rd)+namelen), NULL);
0276         } else {
0277             pr_notice("Not marking the space at 0x%08x as dirty because the flash driver returned retlen zero\n",
0278                   flash_ofs);
0279         }
0280         if (!retried) {
0281             /* Try to reallocate space and retry */
0282             uint32_t dummy;
0283             struct jffs2_eraseblock *jeb = &c->blocks[flash_ofs / c->sector_size];
0284 
0285             retried = 1;
0286 
0287             jffs2_dbg(1, "Retrying failed write.\n");
0288 
0289             jffs2_dbg_acct_sanity_check(c,jeb);
0290             jffs2_dbg_acct_paranoia_check(c, jeb);
0291 
0292             if (alloc_mode == ALLOC_GC) {
0293                 ret = jffs2_reserve_space_gc(c, sizeof(*rd) + namelen, &dummy,
0294                                  JFFS2_SUMMARY_DIRENT_SIZE(namelen));
0295             } else {
0296                 /* Locking pain */
0297                 mutex_unlock(&f->sem);
0298                 jffs2_complete_reservation(c);
0299 
0300                 ret = jffs2_reserve_space(c, sizeof(*rd) + namelen, &dummy,
0301                               alloc_mode, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
0302                 mutex_lock(&f->sem);
0303             }
0304 
0305             if (!ret) {
0306                 flash_ofs = write_ofs(c);
0307                 jffs2_dbg(1, "Allocated space at 0x%08x to retry failed write\n",
0308                       flash_ofs);
0309                 jffs2_dbg_acct_sanity_check(c,jeb);
0310                 jffs2_dbg_acct_paranoia_check(c, jeb);
0311                 goto retry;
0312             }
0313             jffs2_dbg(1, "Failed to allocate space to retry failed write: %d!\n",
0314                   ret);
0315         }
0316         /* Release the full_dnode which is now useless, and return */
0317         jffs2_free_full_dirent(fd);
0318         return ERR_PTR(ret?ret:-EIO);
0319     }
0320     /* Mark the space used */
0321     fd->raw = jffs2_add_physical_node_ref(c, flash_ofs | dirent_node_state(rd),
0322                           PAD(sizeof(*rd)+namelen), f->inocache);
0323     if (IS_ERR(fd->raw)) {
0324         void *hold_err = fd->raw;
0325         /* Release the full_dirent which is now useless, and return */
0326         jffs2_free_full_dirent(fd);
0327         return ERR_CAST(hold_err);
0328     }
0329 
0330     if (retried) {
0331         jffs2_dbg_acct_sanity_check(c,NULL);
0332     }
0333 
0334     return fd;
0335 }
0336 
0337 /* The OS-specific code fills in the metadata in the jffs2_raw_inode for us, so that
0338    we don't have to go digging in struct inode or its equivalent. It should set:
0339    mode, uid, gid, (starting)isize, atime, ctime, mtime */
0340 int jffs2_write_inode_range(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
0341                 struct jffs2_raw_inode *ri, unsigned char *buf,
0342                 uint32_t offset, uint32_t writelen, uint32_t *retlen)
0343 {
0344     int ret = 0;
0345     uint32_t writtenlen = 0;
0346 
0347     jffs2_dbg(1, "%s(): Ino #%u, ofs 0x%x, len 0x%x\n",
0348           __func__, f->inocache->ino, offset, writelen);
0349 
0350     while(writelen) {
0351         struct jffs2_full_dnode *fn;
0352         unsigned char *comprbuf = NULL;
0353         uint16_t comprtype = JFFS2_COMPR_NONE;
0354         uint32_t alloclen;
0355         uint32_t datalen, cdatalen;
0356         int retried = 0;
0357 
0358     retry:
0359         jffs2_dbg(2, "jffs2_commit_write() loop: 0x%x to write to 0x%x\n",
0360               writelen, offset);
0361 
0362         ret = jffs2_reserve_space(c, sizeof(*ri) + JFFS2_MIN_DATA_LEN,
0363                     &alloclen, ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE);
0364         if (ret) {
0365             jffs2_dbg(1, "jffs2_reserve_space returned %d\n", ret);
0366             break;
0367         }
0368         mutex_lock(&f->sem);
0369         datalen = min_t(uint32_t, writelen,
0370                 PAGE_SIZE - (offset & (PAGE_SIZE-1)));
0371         cdatalen = min_t(uint32_t, alloclen - sizeof(*ri), datalen);
0372 
0373         comprtype = jffs2_compress(c, f, buf, &comprbuf, &datalen, &cdatalen);
0374 
0375         ri->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
0376         ri->nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE);
0377         ri->totlen = cpu_to_je32(sizeof(*ri) + cdatalen);
0378         ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4));
0379 
0380         ri->ino = cpu_to_je32(f->inocache->ino);
0381         ri->version = cpu_to_je32(++f->highest_version);
0382         ri->isize = cpu_to_je32(max(je32_to_cpu(ri->isize), offset + datalen));
0383         ri->offset = cpu_to_je32(offset);
0384         ri->csize = cpu_to_je32(cdatalen);
0385         ri->dsize = cpu_to_je32(datalen);
0386         ri->compr = comprtype & 0xff;
0387         ri->usercompr = (comprtype >> 8 ) & 0xff;
0388         ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
0389         ri->data_crc = cpu_to_je32(crc32(0, comprbuf, cdatalen));
0390 
0391         fn = jffs2_write_dnode(c, f, ri, comprbuf, cdatalen, ALLOC_NORETRY);
0392 
0393         jffs2_free_comprbuf(comprbuf, buf);
0394 
0395         if (IS_ERR(fn)) {
0396             ret = PTR_ERR(fn);
0397             mutex_unlock(&f->sem);
0398             jffs2_complete_reservation(c);
0399             if (!retried) {
0400                 /* Write error to be retried */
0401                 retried = 1;
0402                 jffs2_dbg(1, "Retrying node write in jffs2_write_inode_range()\n");
0403                 goto retry;
0404             }
0405             break;
0406         }
0407         ret = jffs2_add_full_dnode_to_inode(c, f, fn);
0408         if (f->metadata) {
0409             jffs2_mark_node_obsolete(c, f->metadata->raw);
0410             jffs2_free_full_dnode(f->metadata);
0411             f->metadata = NULL;
0412         }
0413         if (ret) {
0414             /* Eep */
0415             jffs2_dbg(1, "Eep. add_full_dnode_to_inode() failed in commit_write, returned %d\n",
0416                   ret);
0417             jffs2_mark_node_obsolete(c, fn->raw);
0418             jffs2_free_full_dnode(fn);
0419 
0420             mutex_unlock(&f->sem);
0421             jffs2_complete_reservation(c);
0422             break;
0423         }
0424         mutex_unlock(&f->sem);
0425         jffs2_complete_reservation(c);
0426         if (!datalen) {
0427             pr_warn("Eep. We didn't actually write any data in jffs2_write_inode_range()\n");
0428             ret = -EIO;
0429             break;
0430         }
0431         jffs2_dbg(1, "increasing writtenlen by %d\n", datalen);
0432         writtenlen += datalen;
0433         offset += datalen;
0434         writelen -= datalen;
0435         buf += datalen;
0436     }
0437     *retlen = writtenlen;
0438     return ret;
0439 }
0440 
0441 int jffs2_do_create(struct jffs2_sb_info *c, struct jffs2_inode_info *dir_f,
0442             struct jffs2_inode_info *f, struct jffs2_raw_inode *ri,
0443             const struct qstr *qstr)
0444 {
0445     struct jffs2_raw_dirent *rd;
0446     struct jffs2_full_dnode *fn;
0447     struct jffs2_full_dirent *fd;
0448     uint32_t alloclen;
0449     int ret;
0450 
0451     /* Try to reserve enough space for both node and dirent.
0452      * Just the node will do for now, though
0453      */
0454     ret = jffs2_reserve_space(c, sizeof(*ri), &alloclen, ALLOC_NORMAL,
0455                 JFFS2_SUMMARY_INODE_SIZE);
0456     jffs2_dbg(1, "%s(): reserved 0x%x bytes\n", __func__, alloclen);
0457     if (ret)
0458         return ret;
0459 
0460     mutex_lock(&f->sem);
0461 
0462     ri->data_crc = cpu_to_je32(0);
0463     ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
0464 
0465     fn = jffs2_write_dnode(c, f, ri, NULL, 0, ALLOC_NORMAL);
0466 
0467     jffs2_dbg(1, "jffs2_do_create created file with mode 0x%x\n",
0468           jemode_to_cpu(ri->mode));
0469 
0470     if (IS_ERR(fn)) {
0471         jffs2_dbg(1, "jffs2_write_dnode() failed\n");
0472         /* Eeek. Wave bye bye */
0473         mutex_unlock(&f->sem);
0474         jffs2_complete_reservation(c);
0475         return PTR_ERR(fn);
0476     }
0477     /* No data here. Only a metadata node, which will be
0478        obsoleted by the first data write
0479     */
0480     f->metadata = fn;
0481 
0482     mutex_unlock(&f->sem);
0483     jffs2_complete_reservation(c);
0484 
0485     ret = jffs2_init_security(&f->vfs_inode, &dir_f->vfs_inode, qstr);
0486     if (ret)
0487         return ret;
0488     ret = jffs2_init_acl_post(&f->vfs_inode);
0489     if (ret)
0490         return ret;
0491 
0492     ret = jffs2_reserve_space(c, sizeof(*rd)+qstr->len, &alloclen,
0493                 ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(qstr->len));
0494 
0495     if (ret) {
0496         /* Eep. */
0497         jffs2_dbg(1, "jffs2_reserve_space() for dirent failed\n");
0498         return ret;
0499     }
0500 
0501     rd = jffs2_alloc_raw_dirent();
0502     if (!rd) {
0503         /* Argh. Now we treat it like a normal delete */
0504         jffs2_complete_reservation(c);
0505         return -ENOMEM;
0506     }
0507 
0508     mutex_lock(&dir_f->sem);
0509 
0510     rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
0511     rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
0512     rd->totlen = cpu_to_je32(sizeof(*rd) + qstr->len);
0513     rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
0514 
0515     rd->pino = cpu_to_je32(dir_f->inocache->ino);
0516     rd->version = cpu_to_je32(++dir_f->highest_version);
0517     rd->ino = ri->ino;
0518     rd->mctime = ri->ctime;
0519     rd->nsize = qstr->len;
0520     rd->type = DT_REG;
0521     rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
0522     rd->name_crc = cpu_to_je32(crc32(0, qstr->name, qstr->len));
0523 
0524     fd = jffs2_write_dirent(c, dir_f, rd, qstr->name, qstr->len, ALLOC_NORMAL);
0525 
0526     jffs2_free_raw_dirent(rd);
0527 
0528     if (IS_ERR(fd)) {
0529         /* dirent failed to write. Delete the inode normally
0530            as if it were the final unlink() */
0531         jffs2_complete_reservation(c);
0532         mutex_unlock(&dir_f->sem);
0533         return PTR_ERR(fd);
0534     }
0535 
0536     /* Link the fd into the inode's list, obsoleting an old
0537        one if necessary. */
0538     jffs2_add_fd_to_list(c, fd, &dir_f->dents);
0539 
0540     jffs2_complete_reservation(c);
0541     mutex_unlock(&dir_f->sem);
0542 
0543     return 0;
0544 }
0545 
0546 
0547 int jffs2_do_unlink(struct jffs2_sb_info *c, struct jffs2_inode_info *dir_f,
0548             const char *name, int namelen, struct jffs2_inode_info *dead_f,
0549             uint32_t time)
0550 {
0551     struct jffs2_raw_dirent *rd;
0552     struct jffs2_full_dirent *fd;
0553     uint32_t alloclen;
0554     int ret;
0555 
0556     if (!jffs2_can_mark_obsolete(c)) {
0557         /* We can't mark stuff obsolete on the medium. We need to write a deletion dirent */
0558 
0559         rd = jffs2_alloc_raw_dirent();
0560         if (!rd)
0561             return -ENOMEM;
0562 
0563         ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
0564                     ALLOC_DELETION, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
0565         if (ret) {
0566             jffs2_free_raw_dirent(rd);
0567             return ret;
0568         }
0569 
0570         mutex_lock(&dir_f->sem);
0571 
0572         /* Build a deletion node */
0573         rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
0574         rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
0575         rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
0576         rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
0577 
0578         rd->pino = cpu_to_je32(dir_f->inocache->ino);
0579         rd->version = cpu_to_je32(++dir_f->highest_version);
0580         rd->ino = cpu_to_je32(0);
0581         rd->mctime = cpu_to_je32(time);
0582         rd->nsize = namelen;
0583         rd->type = DT_UNKNOWN;
0584         rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
0585         rd->name_crc = cpu_to_je32(crc32(0, name, namelen));
0586 
0587         fd = jffs2_write_dirent(c, dir_f, rd, name, namelen, ALLOC_DELETION);
0588 
0589         jffs2_free_raw_dirent(rd);
0590 
0591         if (IS_ERR(fd)) {
0592             jffs2_complete_reservation(c);
0593             mutex_unlock(&dir_f->sem);
0594             return PTR_ERR(fd);
0595         }
0596 
0597         /* File it. This will mark the old one obsolete. */
0598         jffs2_add_fd_to_list(c, fd, &dir_f->dents);
0599         mutex_unlock(&dir_f->sem);
0600     } else {
0601         uint32_t nhash = full_name_hash(NULL, name, namelen);
0602 
0603         fd = dir_f->dents;
0604         /* We don't actually want to reserve any space, but we do
0605            want to be holding the alloc_sem when we write to flash */
0606         mutex_lock(&c->alloc_sem);
0607         mutex_lock(&dir_f->sem);
0608 
0609         for (fd = dir_f->dents; fd; fd = fd->next) {
0610             if (fd->nhash == nhash &&
0611                 !memcmp(fd->name, name, namelen) &&
0612                 !fd->name[namelen]) {
0613 
0614                 jffs2_dbg(1, "Marking old dirent node (ino #%u) @%08x obsolete\n",
0615                       fd->ino, ref_offset(fd->raw));
0616                 jffs2_mark_node_obsolete(c, fd->raw);
0617                 /* We don't want to remove it from the list immediately,
0618                    because that screws up getdents()/seek() semantics even
0619                    more than they're screwed already. Turn it into a
0620                    node-less deletion dirent instead -- a placeholder */
0621                 fd->raw = NULL;
0622                 fd->ino = 0;
0623                 break;
0624             }
0625         }
0626         mutex_unlock(&dir_f->sem);
0627     }
0628 
0629     /* dead_f is NULL if this was a rename not a real unlink */
0630     /* Also catch the !f->inocache case, where there was a dirent
0631        pointing to an inode which didn't exist. */
0632     if (dead_f && dead_f->inocache) {
0633 
0634         mutex_lock(&dead_f->sem);
0635 
0636         if (S_ISDIR(OFNI_EDONI_2SFFJ(dead_f)->i_mode)) {
0637             while (dead_f->dents) {
0638                 /* There can be only deleted ones */
0639                 fd = dead_f->dents;
0640 
0641                 dead_f->dents = fd->next;
0642 
0643                 if (fd->ino) {
0644                     pr_warn("Deleting inode #%u with active dentry \"%s\"->ino #%u\n",
0645                         dead_f->inocache->ino,
0646                         fd->name, fd->ino);
0647                 } else {
0648                     jffs2_dbg(1, "Removing deletion dirent for \"%s\" from dir ino #%u\n",
0649                           fd->name,
0650                           dead_f->inocache->ino);
0651                 }
0652                 if (fd->raw)
0653                     jffs2_mark_node_obsolete(c, fd->raw);
0654                 jffs2_free_full_dirent(fd);
0655             }
0656             dead_f->inocache->pino_nlink = 0;
0657         } else
0658             dead_f->inocache->pino_nlink--;
0659         /* NB: Caller must set inode nlink if appropriate */
0660         mutex_unlock(&dead_f->sem);
0661     }
0662 
0663     jffs2_complete_reservation(c);
0664 
0665     return 0;
0666 }
0667 
0668 
0669 int jffs2_do_link (struct jffs2_sb_info *c, struct jffs2_inode_info *dir_f, uint32_t ino, uint8_t type, const char *name, int namelen, uint32_t time)
0670 {
0671     struct jffs2_raw_dirent *rd;
0672     struct jffs2_full_dirent *fd;
0673     uint32_t alloclen;
0674     int ret;
0675 
0676     rd = jffs2_alloc_raw_dirent();
0677     if (!rd)
0678         return -ENOMEM;
0679 
0680     ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
0681                 ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
0682     if (ret) {
0683         jffs2_free_raw_dirent(rd);
0684         return ret;
0685     }
0686 
0687     mutex_lock(&dir_f->sem);
0688 
0689     /* Build a deletion node */
0690     rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
0691     rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
0692     rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
0693     rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
0694 
0695     rd->pino = cpu_to_je32(dir_f->inocache->ino);
0696     rd->version = cpu_to_je32(++dir_f->highest_version);
0697     rd->ino = cpu_to_je32(ino);
0698     rd->mctime = cpu_to_je32(time);
0699     rd->nsize = namelen;
0700 
0701     rd->type = type;
0702 
0703     rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
0704     rd->name_crc = cpu_to_je32(crc32(0, name, namelen));
0705 
0706     fd = jffs2_write_dirent(c, dir_f, rd, name, namelen, ALLOC_NORMAL);
0707 
0708     jffs2_free_raw_dirent(rd);
0709 
0710     if (IS_ERR(fd)) {
0711         jffs2_complete_reservation(c);
0712         mutex_unlock(&dir_f->sem);
0713         return PTR_ERR(fd);
0714     }
0715 
0716     /* File it. This will mark the old one obsolete. */
0717     jffs2_add_fd_to_list(c, fd, &dir_f->dents);
0718 
0719     jffs2_complete_reservation(c);
0720     mutex_unlock(&dir_f->sem);
0721 
0722     return 0;
0723 }