^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) * JFFS2 -- Journalling Flash File System, Version 2.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright © 2001-2007 Red Hat, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Created by David Woodhouse <dwmw2@infradead.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * For licensing information, see the file 'LICENCE' in this directory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/crc32.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/pagemap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/mtd/mtd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include "nodelist.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include "compr.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) int jffs2_do_new_inode(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) uint32_t mode, struct jffs2_raw_inode *ri)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) struct jffs2_inode_cache *ic;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) ic = jffs2_alloc_inode_cache();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) if (!ic) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) memset(ic, 0, sizeof(*ic));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) f->inocache = ic;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) f->inocache->pino_nlink = 1; /* Will be overwritten shortly for directories */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) f->inocache->nodes = (struct jffs2_raw_node_ref *)f->inocache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) f->inocache->state = INO_STATE_PRESENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) jffs2_add_ino_cache(c, f->inocache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) jffs2_dbg(1, "%s(): Assigned ino# %d\n", __func__, f->inocache->ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) ri->ino = cpu_to_je32(f->inocache->ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) ri->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) ri->nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) ri->totlen = cpu_to_je32(PAD(sizeof(*ri)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) ri->mode = cpu_to_jemode(mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) f->highest_version = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) ri->version = cpu_to_je32(f->highest_version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) /* jffs2_write_dnode - given a raw_inode, allocate a full_dnode for it,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) write it to the flash, link it into the existing inode/fragment list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) struct jffs2_full_dnode *jffs2_write_dnode(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) struct jffs2_raw_inode *ri, const unsigned char *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) uint32_t datalen, int alloc_mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) struct jffs2_full_dnode *fn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) size_t retlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) uint32_t flash_ofs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) struct kvec vecs[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) int retried = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) unsigned long cnt = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) D1(if(je32_to_cpu(ri->hdr_crc) != crc32(0, ri, sizeof(struct jffs2_unknown_node)-4)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) pr_crit("Eep. CRC not correct in jffs2_write_dnode()\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) vecs[0].iov_base = ri;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) vecs[0].iov_len = sizeof(*ri);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) vecs[1].iov_base = (unsigned char *)data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) vecs[1].iov_len = datalen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) if (je32_to_cpu(ri->totlen) != sizeof(*ri) + datalen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) pr_warn("%s(): ri->totlen (0x%08x) != sizeof(*ri) (0x%08zx) + datalen (0x%08x)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) __func__, je32_to_cpu(ri->totlen),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) sizeof(*ri), datalen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) fn = jffs2_alloc_full_dnode();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) if (!fn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) /* check number of valid vecs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) if (!datalen || !data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) cnt = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) retry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) flash_ofs = write_ofs(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) jffs2_dbg_prewrite_paranoia_check(c, flash_ofs, vecs[0].iov_len + vecs[1].iov_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) if ((alloc_mode!=ALLOC_GC) && (je32_to_cpu(ri->version) < f->highest_version)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) BUG_ON(!retried);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) jffs2_dbg(1, "%s(): dnode_version %d, highest version %d -> updating dnode\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) je32_to_cpu(ri->version), f->highest_version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) ri->version = cpu_to_je32(++f->highest_version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) ret = jffs2_flash_writev(c, vecs, cnt, flash_ofs, &retlen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) (alloc_mode==ALLOC_GC)?0:f->inocache->ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) if (ret || (retlen != sizeof(*ri) + datalen)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) pr_notice("Write of %zd bytes at 0x%08x failed. returned %d, retlen %zd\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) sizeof(*ri) + datalen, flash_ofs, ret, retlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) /* Mark the space as dirtied */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) if (retlen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) /* Don't change raw->size to match retlen. We may have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) written the node header already, and only the data will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) seem corrupted, in which case the scan would skip over
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) any node we write before the original intended end of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) this node */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) jffs2_add_physical_node_ref(c, flash_ofs | REF_OBSOLETE, PAD(sizeof(*ri)+datalen), NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) pr_notice("Not marking the space at 0x%08x as dirty because the flash driver returned retlen zero\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) flash_ofs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) if (!retried && alloc_mode != ALLOC_NORETRY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) /* Try to reallocate space and retry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) uint32_t dummy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) struct jffs2_eraseblock *jeb = &c->blocks[flash_ofs / c->sector_size];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) retried = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) jffs2_dbg(1, "Retrying failed write.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) jffs2_dbg_acct_sanity_check(c,jeb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) jffs2_dbg_acct_paranoia_check(c, jeb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) if (alloc_mode == ALLOC_GC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) ret = jffs2_reserve_space_gc(c, sizeof(*ri) + datalen, &dummy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) JFFS2_SUMMARY_INODE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) /* Locking pain */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) mutex_unlock(&f->sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) jffs2_complete_reservation(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) ret = jffs2_reserve_space(c, sizeof(*ri) + datalen, &dummy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) alloc_mode, JFFS2_SUMMARY_INODE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) mutex_lock(&f->sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) flash_ofs = write_ofs(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) jffs2_dbg(1, "Allocated space at 0x%08x to retry failed write.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) flash_ofs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) jffs2_dbg_acct_sanity_check(c,jeb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) jffs2_dbg_acct_paranoia_check(c, jeb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) goto retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) jffs2_dbg(1, "Failed to allocate space to retry failed write: %d!\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) /* Release the full_dnode which is now useless, and return */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) jffs2_free_full_dnode(fn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) return ERR_PTR(ret?ret:-EIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) /* Mark the space used */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) /* If node covers at least a whole page, or if it starts at the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) beginning of a page and runs to the end of the file, or if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) it's a hole node, mark it REF_PRISTINE, else REF_NORMAL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) if ((je32_to_cpu(ri->dsize) >= PAGE_SIZE) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) ( ((je32_to_cpu(ri->offset)&(PAGE_SIZE-1))==0) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) (je32_to_cpu(ri->dsize)+je32_to_cpu(ri->offset) == je32_to_cpu(ri->isize)))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) flash_ofs |= REF_PRISTINE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) flash_ofs |= REF_NORMAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) fn->raw = jffs2_add_physical_node_ref(c, flash_ofs, PAD(sizeof(*ri)+datalen), f->inocache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) if (IS_ERR(fn->raw)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) void *hold_err = fn->raw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) /* Release the full_dnode which is now useless, and return */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) jffs2_free_full_dnode(fn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) return ERR_CAST(hold_err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) fn->ofs = je32_to_cpu(ri->offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) fn->size = je32_to_cpu(ri->dsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) fn->frags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 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",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) flash_ofs & ~3, flash_ofs & 3, je32_to_cpu(ri->dsize),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) je32_to_cpu(ri->csize), je32_to_cpu(ri->node_crc),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) je32_to_cpu(ri->data_crc), je32_to_cpu(ri->totlen));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) if (retried) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) jffs2_dbg_acct_sanity_check(c,NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) return fn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) struct jffs2_full_dirent *jffs2_write_dirent(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) struct jffs2_raw_dirent *rd, const unsigned char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) uint32_t namelen, int alloc_mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) struct jffs2_full_dirent *fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) size_t retlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) struct kvec vecs[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) uint32_t flash_ofs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) int retried = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) jffs2_dbg(1, "%s(ino #%u, name at *0x%p \"%s\"->ino #%u, name_crc 0x%08x)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) je32_to_cpu(rd->pino), name, name, je32_to_cpu(rd->ino),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) je32_to_cpu(rd->name_crc));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) D1(if(je32_to_cpu(rd->hdr_crc) != crc32(0, rd, sizeof(struct jffs2_unknown_node)-4)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) pr_crit("Eep. CRC not correct in jffs2_write_dirent()\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) });
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) if (strnlen(name, namelen) != namelen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) /* This should never happen, but seems to have done on at least one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) occasion: https://dev.laptop.org/ticket/4184 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) pr_crit("Error in jffs2_write_dirent() -- name contains zero bytes!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) pr_crit("Directory inode #%u, name at *0x%p \"%s\"->ino #%u, name_crc 0x%08x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) je32_to_cpu(rd->pino), name, name, je32_to_cpu(rd->ino),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) je32_to_cpu(rd->name_crc));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) WARN_ON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) return ERR_PTR(-EIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) vecs[0].iov_base = rd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) vecs[0].iov_len = sizeof(*rd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) vecs[1].iov_base = (unsigned char *)name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) vecs[1].iov_len = namelen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) fd = jffs2_alloc_full_dirent(namelen+1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) if (!fd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) fd->version = je32_to_cpu(rd->version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) fd->ino = je32_to_cpu(rd->ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) fd->nhash = full_name_hash(NULL, name, namelen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) fd->type = rd->type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) memcpy(fd->name, name, namelen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) fd->name[namelen]=0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) retry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) flash_ofs = write_ofs(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) jffs2_dbg_prewrite_paranoia_check(c, flash_ofs, vecs[0].iov_len + vecs[1].iov_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) if ((alloc_mode!=ALLOC_GC) && (je32_to_cpu(rd->version) < f->highest_version)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) BUG_ON(!retried);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) jffs2_dbg(1, "%s(): dirent_version %d, highest version %d -> updating dirent\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) je32_to_cpu(rd->version), f->highest_version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) rd->version = cpu_to_je32(++f->highest_version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) fd->version = je32_to_cpu(rd->version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) ret = jffs2_flash_writev(c, vecs, 2, flash_ofs, &retlen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) (alloc_mode==ALLOC_GC)?0:je32_to_cpu(rd->pino));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) if (ret || (retlen != sizeof(*rd) + namelen)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) pr_notice("Write of %zd bytes at 0x%08x failed. returned %d, retlen %zd\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) sizeof(*rd) + namelen, flash_ofs, ret, retlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) /* Mark the space as dirtied */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) if (retlen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) jffs2_add_physical_node_ref(c, flash_ofs | REF_OBSOLETE, PAD(sizeof(*rd)+namelen), NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) pr_notice("Not marking the space at 0x%08x as dirty because the flash driver returned retlen zero\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) flash_ofs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) if (!retried) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) /* Try to reallocate space and retry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) uint32_t dummy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) struct jffs2_eraseblock *jeb = &c->blocks[flash_ofs / c->sector_size];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) retried = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) jffs2_dbg(1, "Retrying failed write.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) jffs2_dbg_acct_sanity_check(c,jeb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) jffs2_dbg_acct_paranoia_check(c, jeb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) if (alloc_mode == ALLOC_GC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) ret = jffs2_reserve_space_gc(c, sizeof(*rd) + namelen, &dummy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) JFFS2_SUMMARY_DIRENT_SIZE(namelen));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) /* Locking pain */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) mutex_unlock(&f->sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) jffs2_complete_reservation(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) ret = jffs2_reserve_space(c, sizeof(*rd) + namelen, &dummy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) alloc_mode, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) mutex_lock(&f->sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) flash_ofs = write_ofs(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) jffs2_dbg(1, "Allocated space at 0x%08x to retry failed write\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) flash_ofs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) jffs2_dbg_acct_sanity_check(c,jeb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) jffs2_dbg_acct_paranoia_check(c, jeb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) goto retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) jffs2_dbg(1, "Failed to allocate space to retry failed write: %d!\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) /* Release the full_dnode which is now useless, and return */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) jffs2_free_full_dirent(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) return ERR_PTR(ret?ret:-EIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) /* Mark the space used */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) fd->raw = jffs2_add_physical_node_ref(c, flash_ofs | dirent_node_state(rd),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) PAD(sizeof(*rd)+namelen), f->inocache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) if (IS_ERR(fd->raw)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) void *hold_err = fd->raw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) /* Release the full_dirent which is now useless, and return */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) jffs2_free_full_dirent(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) return ERR_CAST(hold_err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) if (retried) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) jffs2_dbg_acct_sanity_check(c,NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) return fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) /* The OS-specific code fills in the metadata in the jffs2_raw_inode for us, so that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) we don't have to go digging in struct inode or its equivalent. It should set:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) mode, uid, gid, (starting)isize, atime, ctime, mtime */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) int jffs2_write_inode_range(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) struct jffs2_raw_inode *ri, unsigned char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) uint32_t offset, uint32_t writelen, uint32_t *retlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) uint32_t writtenlen = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) jffs2_dbg(1, "%s(): Ino #%u, ofs 0x%x, len 0x%x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) __func__, f->inocache->ino, offset, writelen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) while(writelen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) struct jffs2_full_dnode *fn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) unsigned char *comprbuf = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) uint16_t comprtype = JFFS2_COMPR_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) uint32_t alloclen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) uint32_t datalen, cdatalen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) int retried = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) retry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) jffs2_dbg(2, "jffs2_commit_write() loop: 0x%x to write to 0x%x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) writelen, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) ret = jffs2_reserve_space(c, sizeof(*ri) + JFFS2_MIN_DATA_LEN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) &alloclen, ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) jffs2_dbg(1, "jffs2_reserve_space returned %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) mutex_lock(&f->sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) datalen = min_t(uint32_t, writelen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) PAGE_SIZE - (offset & (PAGE_SIZE-1)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) cdatalen = min_t(uint32_t, alloclen - sizeof(*ri), datalen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) comprtype = jffs2_compress(c, f, buf, &comprbuf, &datalen, &cdatalen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) ri->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) ri->nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) ri->totlen = cpu_to_je32(sizeof(*ri) + cdatalen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) ri->ino = cpu_to_je32(f->inocache->ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) ri->version = cpu_to_je32(++f->highest_version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) ri->isize = cpu_to_je32(max(je32_to_cpu(ri->isize), offset + datalen));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) ri->offset = cpu_to_je32(offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) ri->csize = cpu_to_je32(cdatalen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) ri->dsize = cpu_to_je32(datalen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) ri->compr = comprtype & 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) ri->usercompr = (comprtype >> 8 ) & 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) ri->data_crc = cpu_to_je32(crc32(0, comprbuf, cdatalen));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) fn = jffs2_write_dnode(c, f, ri, comprbuf, cdatalen, ALLOC_NORETRY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) jffs2_free_comprbuf(comprbuf, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) if (IS_ERR(fn)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) ret = PTR_ERR(fn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) mutex_unlock(&f->sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) jffs2_complete_reservation(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) if (!retried) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) /* Write error to be retried */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) retried = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) jffs2_dbg(1, "Retrying node write in jffs2_write_inode_range()\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) goto retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) ret = jffs2_add_full_dnode_to_inode(c, f, fn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) if (f->metadata) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) jffs2_mark_node_obsolete(c, f->metadata->raw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) jffs2_free_full_dnode(f->metadata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) f->metadata = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) /* Eep */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) jffs2_dbg(1, "Eep. add_full_dnode_to_inode() failed in commit_write, returned %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) jffs2_mark_node_obsolete(c, fn->raw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) jffs2_free_full_dnode(fn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) mutex_unlock(&f->sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) jffs2_complete_reservation(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) mutex_unlock(&f->sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) jffs2_complete_reservation(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) if (!datalen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) pr_warn("Eep. We didn't actually write any data in jffs2_write_inode_range()\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) jffs2_dbg(1, "increasing writtenlen by %d\n", datalen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) writtenlen += datalen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) offset += datalen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) writelen -= datalen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) buf += datalen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) *retlen = writtenlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) int jffs2_do_create(struct jffs2_sb_info *c, struct jffs2_inode_info *dir_f,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) struct jffs2_inode_info *f, struct jffs2_raw_inode *ri,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) const struct qstr *qstr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) struct jffs2_raw_dirent *rd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) struct jffs2_full_dnode *fn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) struct jffs2_full_dirent *fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) uint32_t alloclen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) /* Try to reserve enough space for both node and dirent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) * Just the node will do for now, though
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) ret = jffs2_reserve_space(c, sizeof(*ri), &alloclen, ALLOC_NORMAL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) JFFS2_SUMMARY_INODE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) jffs2_dbg(1, "%s(): reserved 0x%x bytes\n", __func__, alloclen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) mutex_lock(&f->sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) ri->data_crc = cpu_to_je32(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) fn = jffs2_write_dnode(c, f, ri, NULL, 0, ALLOC_NORMAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) jffs2_dbg(1, "jffs2_do_create created file with mode 0x%x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) jemode_to_cpu(ri->mode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) if (IS_ERR(fn)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) jffs2_dbg(1, "jffs2_write_dnode() failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) /* Eeek. Wave bye bye */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) mutex_unlock(&f->sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) jffs2_complete_reservation(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) return PTR_ERR(fn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) /* No data here. Only a metadata node, which will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) obsoleted by the first data write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) f->metadata = fn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) mutex_unlock(&f->sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) jffs2_complete_reservation(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) ret = jffs2_init_security(&f->vfs_inode, &dir_f->vfs_inode, qstr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) ret = jffs2_init_acl_post(&f->vfs_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) ret = jffs2_reserve_space(c, sizeof(*rd)+qstr->len, &alloclen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(qstr->len));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) /* Eep. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) jffs2_dbg(1, "jffs2_reserve_space() for dirent failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) rd = jffs2_alloc_raw_dirent();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) if (!rd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) /* Argh. Now we treat it like a normal delete */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) jffs2_complete_reservation(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) mutex_lock(&dir_f->sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) rd->totlen = cpu_to_je32(sizeof(*rd) + qstr->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) rd->pino = cpu_to_je32(dir_f->inocache->ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) rd->version = cpu_to_je32(++dir_f->highest_version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) rd->ino = ri->ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) rd->mctime = ri->ctime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) rd->nsize = qstr->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) rd->type = DT_REG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) rd->name_crc = cpu_to_je32(crc32(0, qstr->name, qstr->len));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) fd = jffs2_write_dirent(c, dir_f, rd, qstr->name, qstr->len, ALLOC_NORMAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) jffs2_free_raw_dirent(rd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) if (IS_ERR(fd)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) /* dirent failed to write. Delete the inode normally
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) as if it were the final unlink() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) jffs2_complete_reservation(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) mutex_unlock(&dir_f->sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) return PTR_ERR(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) /* Link the fd into the inode's list, obsoleting an old
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) one if necessary. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) jffs2_add_fd_to_list(c, fd, &dir_f->dents);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) jffs2_complete_reservation(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) mutex_unlock(&dir_f->sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) int jffs2_do_unlink(struct jffs2_sb_info *c, struct jffs2_inode_info *dir_f,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) const char *name, int namelen, struct jffs2_inode_info *dead_f,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) uint32_t time)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) struct jffs2_raw_dirent *rd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) struct jffs2_full_dirent *fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) uint32_t alloclen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) if (!jffs2_can_mark_obsolete(c)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) /* We can't mark stuff obsolete on the medium. We need to write a deletion dirent */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) rd = jffs2_alloc_raw_dirent();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) if (!rd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) ALLOC_DELETION, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) jffs2_free_raw_dirent(rd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) mutex_lock(&dir_f->sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) /* Build a deletion node */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) rd->pino = cpu_to_je32(dir_f->inocache->ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) rd->version = cpu_to_je32(++dir_f->highest_version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) rd->ino = cpu_to_je32(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) rd->mctime = cpu_to_je32(time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) rd->nsize = namelen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) rd->type = DT_UNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) rd->name_crc = cpu_to_je32(crc32(0, name, namelen));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) fd = jffs2_write_dirent(c, dir_f, rd, name, namelen, ALLOC_DELETION);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) jffs2_free_raw_dirent(rd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) if (IS_ERR(fd)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) jffs2_complete_reservation(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) mutex_unlock(&dir_f->sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) return PTR_ERR(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) /* File it. This will mark the old one obsolete. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) jffs2_add_fd_to_list(c, fd, &dir_f->dents);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) mutex_unlock(&dir_f->sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) uint32_t nhash = full_name_hash(NULL, name, namelen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) fd = dir_f->dents;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) /* We don't actually want to reserve any space, but we do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) want to be holding the alloc_sem when we write to flash */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) mutex_lock(&c->alloc_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) mutex_lock(&dir_f->sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) for (fd = dir_f->dents; fd; fd = fd->next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) if (fd->nhash == nhash &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) !memcmp(fd->name, name, namelen) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) !fd->name[namelen]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) jffs2_dbg(1, "Marking old dirent node (ino #%u) @%08x obsolete\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) fd->ino, ref_offset(fd->raw));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) jffs2_mark_node_obsolete(c, fd->raw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) /* We don't want to remove it from the list immediately,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) because that screws up getdents()/seek() semantics even
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) more than they're screwed already. Turn it into a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) node-less deletion dirent instead -- a placeholder */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) fd->raw = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) fd->ino = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) mutex_unlock(&dir_f->sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) /* dead_f is NULL if this was a rename not a real unlink */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) /* Also catch the !f->inocache case, where there was a dirent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) pointing to an inode which didn't exist. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) if (dead_f && dead_f->inocache) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) mutex_lock(&dead_f->sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) if (S_ISDIR(OFNI_EDONI_2SFFJ(dead_f)->i_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) while (dead_f->dents) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) /* There can be only deleted ones */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) fd = dead_f->dents;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) dead_f->dents = fd->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) if (fd->ino) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) pr_warn("Deleting inode #%u with active dentry \"%s\"->ino #%u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) dead_f->inocache->ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) fd->name, fd->ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) jffs2_dbg(1, "Removing deletion dirent for \"%s\" from dir ino #%u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) fd->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) dead_f->inocache->ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) if (fd->raw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) jffs2_mark_node_obsolete(c, fd->raw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) jffs2_free_full_dirent(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) dead_f->inocache->pino_nlink = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) dead_f->inocache->pino_nlink--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) /* NB: Caller must set inode nlink if appropriate */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) mutex_unlock(&dead_f->sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) jffs2_complete_reservation(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) 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)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) struct jffs2_raw_dirent *rd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) struct jffs2_full_dirent *fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) uint32_t alloclen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) rd = jffs2_alloc_raw_dirent();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) if (!rd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) jffs2_free_raw_dirent(rd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) mutex_lock(&dir_f->sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) /* Build a deletion node */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) rd->pino = cpu_to_je32(dir_f->inocache->ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) rd->version = cpu_to_je32(++dir_f->highest_version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) rd->ino = cpu_to_je32(ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) rd->mctime = cpu_to_je32(time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) rd->nsize = namelen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) rd->type = type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) rd->name_crc = cpu_to_je32(crc32(0, name, namelen));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) fd = jffs2_write_dirent(c, dir_f, rd, name, namelen, ALLOC_NORMAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) jffs2_free_raw_dirent(rd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) if (IS_ERR(fd)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) jffs2_complete_reservation(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) mutex_unlock(&dir_f->sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) return PTR_ERR(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) /* File it. This will mark the old one obsolete. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) jffs2_add_fd_to_list(c, fd, &dir_f->dents);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) jffs2_complete_reservation(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) mutex_unlock(&dir_f->sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) }