11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds * JFFS2 -- Journalling Flash File System, Version 2.
31da177e4SLinus Torvalds *
4c00c310eSDavid Woodhouse * Copyright © 2001-2007 Red Hat, Inc.
51da177e4SLinus Torvalds *
61da177e4SLinus Torvalds * Created by David Woodhouse <dwmw2@infradead.org>
71da177e4SLinus Torvalds *
81da177e4SLinus Torvalds * For licensing information, see the file 'LICENCE' in this directory.
91da177e4SLinus Torvalds *
101da177e4SLinus Torvalds */
111da177e4SLinus Torvalds
125a528957SJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
135a528957SJoe Perches
141da177e4SLinus Torvalds #include <linux/kernel.h>
151da177e4SLinus Torvalds #include <linux/fs.h>
161da177e4SLinus Torvalds #include <linux/crc32.h>
171da177e4SLinus Torvalds #include <linux/pagemap.h>
181da177e4SLinus Torvalds #include <linux/mtd/mtd.h>
191da177e4SLinus Torvalds #include "nodelist.h"
201da177e4SLinus Torvalds #include "compr.h"
211da177e4SLinus Torvalds
221da177e4SLinus Torvalds
jffs2_do_new_inode(struct jffs2_sb_info * c,struct jffs2_inode_info * f,uint32_t mode,struct jffs2_raw_inode * ri)2327c72b04SDavid Woodhouse int jffs2_do_new_inode(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
2427c72b04SDavid Woodhouse uint32_t mode, struct jffs2_raw_inode *ri)
251da177e4SLinus Torvalds {
261da177e4SLinus Torvalds struct jffs2_inode_cache *ic;
271da177e4SLinus Torvalds
281da177e4SLinus Torvalds ic = jffs2_alloc_inode_cache();
291da177e4SLinus Torvalds if (!ic) {
301da177e4SLinus Torvalds return -ENOMEM;
311da177e4SLinus Torvalds }
321da177e4SLinus Torvalds
331da177e4SLinus Torvalds memset(ic, 0, sizeof(*ic));
341da177e4SLinus Torvalds
351da177e4SLinus Torvalds f->inocache = ic;
3627c72b04SDavid Woodhouse f->inocache->pino_nlink = 1; /* Will be overwritten shortly for directories */
371da177e4SLinus Torvalds f->inocache->nodes = (struct jffs2_raw_node_ref *)f->inocache;
381da177e4SLinus Torvalds f->inocache->state = INO_STATE_PRESENT;
391da177e4SLinus Torvalds
401da177e4SLinus Torvalds jffs2_add_ino_cache(c, f->inocache);
419c261b33SJoe Perches jffs2_dbg(1, "%s(): Assigned ino# %d\n", __func__, f->inocache->ino);
427d200960SDavid Woodhouse ri->ino = cpu_to_je32(f->inocache->ino);
431da177e4SLinus Torvalds
441da177e4SLinus Torvalds ri->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
451da177e4SLinus Torvalds ri->nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE);
461da177e4SLinus Torvalds ri->totlen = cpu_to_je32(PAD(sizeof(*ri)));
471da177e4SLinus Torvalds ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4));
481da177e4SLinus Torvalds ri->mode = cpu_to_jemode(mode);
491da177e4SLinus Torvalds
501da177e4SLinus Torvalds f->highest_version = 1;
511da177e4SLinus Torvalds ri->version = cpu_to_je32(f->highest_version);
521da177e4SLinus Torvalds
531da177e4SLinus Torvalds return 0;
541da177e4SLinus Torvalds }
551da177e4SLinus Torvalds
561da177e4SLinus Torvalds /* jffs2_write_dnode - given a raw_inode, allocate a full_dnode for it,
571da177e4SLinus Torvalds write it to the flash, link it into the existing inode/fragment list */
581da177e4SLinus Torvalds
jffs2_write_dnode(struct jffs2_sb_info * c,struct jffs2_inode_info * f,struct jffs2_raw_inode * ri,const unsigned char * data,uint32_t datalen,int alloc_mode)599fe4854cSDavid Woodhouse struct jffs2_full_dnode *jffs2_write_dnode(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
609fe4854cSDavid Woodhouse struct jffs2_raw_inode *ri, const unsigned char *data,
619fe4854cSDavid Woodhouse uint32_t datalen, int alloc_mode)
621da177e4SLinus Torvalds
631da177e4SLinus Torvalds {
641da177e4SLinus Torvalds struct jffs2_full_dnode *fn;
651da177e4SLinus Torvalds size_t retlen;
669fe4854cSDavid Woodhouse uint32_t flash_ofs;
671da177e4SLinus Torvalds struct kvec vecs[2];
681da177e4SLinus Torvalds int ret;
691da177e4SLinus Torvalds int retried = 0;
701da177e4SLinus Torvalds unsigned long cnt = 2;
711da177e4SLinus Torvalds
721da177e4SLinus Torvalds D1(if(je32_to_cpu(ri->hdr_crc) != crc32(0, ri, sizeof(struct jffs2_unknown_node)-4)) {
73da320f05SJoe Perches pr_crit("Eep. CRC not correct in jffs2_write_dnode()\n");
741da177e4SLinus Torvalds BUG();
751da177e4SLinus Torvalds }
761da177e4SLinus Torvalds );
771da177e4SLinus Torvalds vecs[0].iov_base = ri;
781da177e4SLinus Torvalds vecs[0].iov_len = sizeof(*ri);
791da177e4SLinus Torvalds vecs[1].iov_base = (unsigned char *)data;
801da177e4SLinus Torvalds vecs[1].iov_len = datalen;
811da177e4SLinus Torvalds
821da177e4SLinus Torvalds if (je32_to_cpu(ri->totlen) != sizeof(*ri) + datalen) {
83da320f05SJoe Perches pr_warn("%s(): ri->totlen (0x%08x) != sizeof(*ri) (0x%08zx) + datalen (0x%08x)\n",
84da320f05SJoe Perches __func__, je32_to_cpu(ri->totlen),
85da320f05SJoe Perches sizeof(*ri), datalen);
861da177e4SLinus Torvalds }
871da177e4SLinus Torvalds
881da177e4SLinus Torvalds fn = jffs2_alloc_full_dnode();
892f785402SDavid Woodhouse if (!fn)
901da177e4SLinus Torvalds return ERR_PTR(-ENOMEM);
911da177e4SLinus Torvalds
921da177e4SLinus Torvalds /* check number of valid vecs */
931da177e4SLinus Torvalds if (!datalen || !data)
941da177e4SLinus Torvalds cnt = 1;
951da177e4SLinus Torvalds retry:
962f785402SDavid Woodhouse flash_ofs = write_ofs(c);
979fe4854cSDavid Woodhouse
989fe4854cSDavid Woodhouse jffs2_dbg_prewrite_paranoia_check(c, flash_ofs, vecs[0].iov_len + vecs[1].iov_len);
991da177e4SLinus Torvalds
1009b88f473SEstelle Hammache if ((alloc_mode!=ALLOC_GC) && (je32_to_cpu(ri->version) < f->highest_version)) {
1019b88f473SEstelle Hammache BUG_ON(!retried);
1029c261b33SJoe Perches jffs2_dbg(1, "%s(): dnode_version %d, highest version %d -> updating dnode\n",
1039c261b33SJoe Perches __func__,
1049c261b33SJoe Perches je32_to_cpu(ri->version), f->highest_version);
105e4803c30SEstelle Hammache ri->version = cpu_to_je32(++f->highest_version);
106e4803c30SEstelle Hammache ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
107e4803c30SEstelle Hammache }
108e4803c30SEstelle Hammache
1091da177e4SLinus Torvalds ret = jffs2_flash_writev(c, vecs, cnt, flash_ofs, &retlen,
1101da177e4SLinus Torvalds (alloc_mode==ALLOC_GC)?0:f->inocache->ino);
1111da177e4SLinus Torvalds
1121da177e4SLinus Torvalds if (ret || (retlen != sizeof(*ri) + datalen)) {
113da320f05SJoe Perches pr_notice("Write of %zd bytes at 0x%08x failed. returned %d, retlen %zd\n",
1141da177e4SLinus Torvalds sizeof(*ri) + datalen, flash_ofs, ret, retlen);
1151da177e4SLinus Torvalds
1161da177e4SLinus Torvalds /* Mark the space as dirtied */
1171da177e4SLinus Torvalds if (retlen) {
1181da177e4SLinus Torvalds /* Don't change raw->size to match retlen. We may have
1191da177e4SLinus Torvalds written the node header already, and only the data will
1201da177e4SLinus Torvalds seem corrupted, in which case the scan would skip over
1211da177e4SLinus Torvalds any node we write before the original intended end of
1221da177e4SLinus Torvalds this node */
1232f785402SDavid Woodhouse jffs2_add_physical_node_ref(c, flash_ofs | REF_OBSOLETE, PAD(sizeof(*ri)+datalen), NULL);
1241da177e4SLinus Torvalds } else {
125da320f05SJoe Perches pr_notice("Not marking the space at 0x%08x as dirty because the flash driver returned retlen zero\n",
126da320f05SJoe Perches flash_ofs);
1271da177e4SLinus Torvalds }
1282f785402SDavid Woodhouse if (!retried && alloc_mode != ALLOC_NORETRY) {
1291da177e4SLinus Torvalds /* Try to reallocate space and retry */
1301da177e4SLinus Torvalds uint32_t dummy;
1311da177e4SLinus Torvalds struct jffs2_eraseblock *jeb = &c->blocks[flash_ofs / c->sector_size];
1321da177e4SLinus Torvalds
1331da177e4SLinus Torvalds retried = 1;
1341da177e4SLinus Torvalds
1359c261b33SJoe Perches jffs2_dbg(1, "Retrying failed write.\n");
1361da177e4SLinus Torvalds
137730554d9SArtem B. Bityutskiy jffs2_dbg_acct_sanity_check(c,jeb);
138730554d9SArtem B. Bityutskiy jffs2_dbg_acct_paranoia_check(c, jeb);
1391da177e4SLinus Torvalds
1401da177e4SLinus Torvalds if (alloc_mode == ALLOC_GC) {
1419fe4854cSDavid Woodhouse ret = jffs2_reserve_space_gc(c, sizeof(*ri) + datalen, &dummy,
1429fe4854cSDavid Woodhouse JFFS2_SUMMARY_INODE_SIZE);
1431da177e4SLinus Torvalds } else {
1441da177e4SLinus Torvalds /* Locking pain */
145ced22070SDavid Woodhouse mutex_unlock(&f->sem);
1461da177e4SLinus Torvalds jffs2_complete_reservation(c);
1471da177e4SLinus Torvalds
1489fe4854cSDavid Woodhouse ret = jffs2_reserve_space(c, sizeof(*ri) + datalen, &dummy,
1499fe4854cSDavid Woodhouse alloc_mode, JFFS2_SUMMARY_INODE_SIZE);
150ced22070SDavid Woodhouse mutex_lock(&f->sem);
1511da177e4SLinus Torvalds }
1521da177e4SLinus Torvalds
1531da177e4SLinus Torvalds if (!ret) {
1549fe4854cSDavid Woodhouse flash_ofs = write_ofs(c);
1559c261b33SJoe Perches jffs2_dbg(1, "Allocated space at 0x%08x to retry failed write.\n",
1569c261b33SJoe Perches flash_ofs);
1571da177e4SLinus Torvalds
158730554d9SArtem B. Bityutskiy jffs2_dbg_acct_sanity_check(c,jeb);
159730554d9SArtem B. Bityutskiy jffs2_dbg_acct_paranoia_check(c, jeb);
1601da177e4SLinus Torvalds
1611da177e4SLinus Torvalds goto retry;
1621da177e4SLinus Torvalds }
1639c261b33SJoe Perches jffs2_dbg(1, "Failed to allocate space to retry failed write: %d!\n",
1649c261b33SJoe Perches ret);
1651da177e4SLinus Torvalds }
1661da177e4SLinus Torvalds /* Release the full_dnode which is now useless, and return */
1671da177e4SLinus Torvalds jffs2_free_full_dnode(fn);
1681da177e4SLinus Torvalds return ERR_PTR(ret?ret:-EIO);
1691da177e4SLinus Torvalds }
1701da177e4SLinus Torvalds /* Mark the space used */
1711da177e4SLinus Torvalds /* If node covers at least a whole page, or if it starts at the
1721da177e4SLinus Torvalds beginning of a page and runs to the end of the file, or if
1731da177e4SLinus Torvalds it's a hole node, mark it REF_PRISTINE, else REF_NORMAL.
1741da177e4SLinus Torvalds */
17509cbfeafSKirill A. Shutemov if ((je32_to_cpu(ri->dsize) >= PAGE_SIZE) ||
17609cbfeafSKirill A. Shutemov ( ((je32_to_cpu(ri->offset)&(PAGE_SIZE-1))==0) &&
1771da177e4SLinus Torvalds (je32_to_cpu(ri->dsize)+je32_to_cpu(ri->offset) == je32_to_cpu(ri->isize)))) {
1782f785402SDavid Woodhouse flash_ofs |= REF_PRISTINE;
1791da177e4SLinus Torvalds } else {
1802f785402SDavid Woodhouse flash_ofs |= REF_NORMAL;
1811da177e4SLinus Torvalds }
1822f785402SDavid Woodhouse fn->raw = jffs2_add_physical_node_ref(c, flash_ofs, PAD(sizeof(*ri)+datalen), f->inocache);
1835bd5c03cSJoakim Tjernlund if (IS_ERR(fn->raw)) {
1845bd5c03cSJoakim Tjernlund void *hold_err = fn->raw;
1855bd5c03cSJoakim Tjernlund /* Release the full_dnode which is now useless, and return */
1865bd5c03cSJoakim Tjernlund jffs2_free_full_dnode(fn);
187e231c2eeSDavid Howells return ERR_CAST(hold_err);
1885bd5c03cSJoakim Tjernlund }
1892f785402SDavid Woodhouse fn->ofs = je32_to_cpu(ri->offset);
1902f785402SDavid Woodhouse fn->size = je32_to_cpu(ri->dsize);
1912f785402SDavid Woodhouse fn->frags = 0;
1921da177e4SLinus Torvalds
1939c261b33SJoe Perches 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",
1942f785402SDavid Woodhouse flash_ofs & ~3, flash_ofs & 3, je32_to_cpu(ri->dsize),
1951da177e4SLinus Torvalds je32_to_cpu(ri->csize), je32_to_cpu(ri->node_crc),
1969c261b33SJoe Perches je32_to_cpu(ri->data_crc), je32_to_cpu(ri->totlen));
1971da177e4SLinus Torvalds
1981da177e4SLinus Torvalds if (retried) {
199730554d9SArtem B. Bityutskiy jffs2_dbg_acct_sanity_check(c,NULL);
2001da177e4SLinus Torvalds }
2011da177e4SLinus Torvalds
2021da177e4SLinus Torvalds return fn;
2031da177e4SLinus Torvalds }
2041da177e4SLinus Torvalds
jffs2_write_dirent(struct jffs2_sb_info * c,struct jffs2_inode_info * f,struct jffs2_raw_dirent * rd,const unsigned char * name,uint32_t namelen,int alloc_mode)2059fe4854cSDavid Woodhouse struct jffs2_full_dirent *jffs2_write_dirent(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
2069fe4854cSDavid Woodhouse struct jffs2_raw_dirent *rd, const unsigned char *name,
2079fe4854cSDavid Woodhouse uint32_t namelen, int alloc_mode)
2081da177e4SLinus Torvalds {
2091da177e4SLinus Torvalds struct jffs2_full_dirent *fd;
2101da177e4SLinus Torvalds size_t retlen;
2111da177e4SLinus Torvalds struct kvec vecs[2];
2122f785402SDavid Woodhouse uint32_t flash_ofs;
2131da177e4SLinus Torvalds int retried = 0;
2141da177e4SLinus Torvalds int ret;
2151da177e4SLinus Torvalds
2169c261b33SJoe Perches jffs2_dbg(1, "%s(ino #%u, name at *0x%p \"%s\"->ino #%u, name_crc 0x%08x)\n",
2179c261b33SJoe Perches __func__,
2181da177e4SLinus Torvalds je32_to_cpu(rd->pino), name, name, je32_to_cpu(rd->ino),
2199c261b33SJoe Perches je32_to_cpu(rd->name_crc));
220730554d9SArtem B. Bityutskiy
2211da177e4SLinus Torvalds D1(if(je32_to_cpu(rd->hdr_crc) != crc32(0, rd, sizeof(struct jffs2_unknown_node)-4)) {
222da320f05SJoe Perches pr_crit("Eep. CRC not correct in jffs2_write_dirent()\n");
2231da177e4SLinus Torvalds BUG();
2242f785402SDavid Woodhouse });
2251da177e4SLinus Torvalds
22669ca4378SDavid Woodhouse if (strnlen(name, namelen) != namelen) {
22769ca4378SDavid Woodhouse /* This should never happen, but seems to have done on at least one
22869ca4378SDavid Woodhouse occasion: https://dev.laptop.org/ticket/4184 */
229da320f05SJoe Perches pr_crit("Error in jffs2_write_dirent() -- name contains zero bytes!\n");
230da320f05SJoe Perches pr_crit("Directory inode #%u, name at *0x%p \"%s\"->ino #%u, name_crc 0x%08x\n",
23169ca4378SDavid Woodhouse je32_to_cpu(rd->pino), name, name, je32_to_cpu(rd->ino),
23269ca4378SDavid Woodhouse je32_to_cpu(rd->name_crc));
23369ca4378SDavid Woodhouse WARN_ON(1);
23469ca4378SDavid Woodhouse return ERR_PTR(-EIO);
23569ca4378SDavid Woodhouse }
23669ca4378SDavid Woodhouse
2371da177e4SLinus Torvalds vecs[0].iov_base = rd;
2381da177e4SLinus Torvalds vecs[0].iov_len = sizeof(*rd);
2391da177e4SLinus Torvalds vecs[1].iov_base = (unsigned char *)name;
2401da177e4SLinus Torvalds vecs[1].iov_len = namelen;
2411da177e4SLinus Torvalds
2421da177e4SLinus Torvalds fd = jffs2_alloc_full_dirent(namelen+1);
2432f785402SDavid Woodhouse if (!fd)
2441da177e4SLinus Torvalds return ERR_PTR(-ENOMEM);
2451da177e4SLinus Torvalds
2461da177e4SLinus Torvalds fd->version = je32_to_cpu(rd->version);
2471da177e4SLinus Torvalds fd->ino = je32_to_cpu(rd->ino);
248*8387ff25SLinus Torvalds fd->nhash = full_name_hash(NULL, name, namelen);
2491da177e4SLinus Torvalds fd->type = rd->type;
2501da177e4SLinus Torvalds memcpy(fd->name, name, namelen);
2511da177e4SLinus Torvalds fd->name[namelen]=0;
2521da177e4SLinus Torvalds
2531da177e4SLinus Torvalds retry:
2542f785402SDavid Woodhouse flash_ofs = write_ofs(c);
2551da177e4SLinus Torvalds
2562f785402SDavid Woodhouse jffs2_dbg_prewrite_paranoia_check(c, flash_ofs, vecs[0].iov_len + vecs[1].iov_len);
2571da177e4SLinus Torvalds
2589b88f473SEstelle Hammache if ((alloc_mode!=ALLOC_GC) && (je32_to_cpu(rd->version) < f->highest_version)) {
2599b88f473SEstelle Hammache BUG_ON(!retried);
2609c261b33SJoe Perches jffs2_dbg(1, "%s(): dirent_version %d, highest version %d -> updating dirent\n",
2619c261b33SJoe Perches __func__,
2629c261b33SJoe Perches je32_to_cpu(rd->version), f->highest_version);
263e4803c30SEstelle Hammache rd->version = cpu_to_je32(++f->highest_version);
264e4803c30SEstelle Hammache fd->version = je32_to_cpu(rd->version);
265e4803c30SEstelle Hammache rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
266e4803c30SEstelle Hammache }
267e4803c30SEstelle Hammache
2681da177e4SLinus Torvalds ret = jffs2_flash_writev(c, vecs, 2, flash_ofs, &retlen,
2691da177e4SLinus Torvalds (alloc_mode==ALLOC_GC)?0:je32_to_cpu(rd->pino));
2701da177e4SLinus Torvalds if (ret || (retlen != sizeof(*rd) + namelen)) {
271da320f05SJoe Perches pr_notice("Write of %zd bytes at 0x%08x failed. returned %d, retlen %zd\n",
2721da177e4SLinus Torvalds sizeof(*rd) + namelen, flash_ofs, ret, retlen);
2731da177e4SLinus Torvalds /* Mark the space as dirtied */
2741da177e4SLinus Torvalds if (retlen) {
2752f785402SDavid Woodhouse jffs2_add_physical_node_ref(c, flash_ofs | REF_OBSOLETE, PAD(sizeof(*rd)+namelen), NULL);
2761da177e4SLinus Torvalds } else {
277da320f05SJoe Perches pr_notice("Not marking the space at 0x%08x as dirty because the flash driver returned retlen zero\n",
278da320f05SJoe Perches flash_ofs);
2791da177e4SLinus Torvalds }
2802f785402SDavid Woodhouse if (!retried) {
2811da177e4SLinus Torvalds /* Try to reallocate space and retry */
2821da177e4SLinus Torvalds uint32_t dummy;
2831da177e4SLinus Torvalds struct jffs2_eraseblock *jeb = &c->blocks[flash_ofs / c->sector_size];
2841da177e4SLinus Torvalds
2851da177e4SLinus Torvalds retried = 1;
2861da177e4SLinus Torvalds
2879c261b33SJoe Perches jffs2_dbg(1, "Retrying failed write.\n");
2881da177e4SLinus Torvalds
289730554d9SArtem B. Bityutskiy jffs2_dbg_acct_sanity_check(c,jeb);
290730554d9SArtem B. Bityutskiy jffs2_dbg_acct_paranoia_check(c, jeb);
2911da177e4SLinus Torvalds
2921da177e4SLinus Torvalds if (alloc_mode == ALLOC_GC) {
2939fe4854cSDavid Woodhouse ret = jffs2_reserve_space_gc(c, sizeof(*rd) + namelen, &dummy,
2949fe4854cSDavid Woodhouse JFFS2_SUMMARY_DIRENT_SIZE(namelen));
2951da177e4SLinus Torvalds } else {
2961da177e4SLinus Torvalds /* Locking pain */
297ced22070SDavid Woodhouse mutex_unlock(&f->sem);
2981da177e4SLinus Torvalds jffs2_complete_reservation(c);
2991da177e4SLinus Torvalds
3009fe4854cSDavid Woodhouse ret = jffs2_reserve_space(c, sizeof(*rd) + namelen, &dummy,
3019fe4854cSDavid Woodhouse alloc_mode, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
302ced22070SDavid Woodhouse mutex_lock(&f->sem);
3031da177e4SLinus Torvalds }
3041da177e4SLinus Torvalds
3051da177e4SLinus Torvalds if (!ret) {
3069fe4854cSDavid Woodhouse flash_ofs = write_ofs(c);
3079c261b33SJoe Perches jffs2_dbg(1, "Allocated space at 0x%08x to retry failed write\n",
3089c261b33SJoe Perches flash_ofs);
309730554d9SArtem B. Bityutskiy jffs2_dbg_acct_sanity_check(c,jeb);
310730554d9SArtem B. Bityutskiy jffs2_dbg_acct_paranoia_check(c, jeb);
3111da177e4SLinus Torvalds goto retry;
3121da177e4SLinus Torvalds }
3139c261b33SJoe Perches jffs2_dbg(1, "Failed to allocate space to retry failed write: %d!\n",
3149c261b33SJoe Perches ret);
3151da177e4SLinus Torvalds }
3161da177e4SLinus Torvalds /* Release the full_dnode which is now useless, and return */
3171da177e4SLinus Torvalds jffs2_free_full_dirent(fd);
3181da177e4SLinus Torvalds return ERR_PTR(ret?ret:-EIO);
3191da177e4SLinus Torvalds }
3201da177e4SLinus Torvalds /* Mark the space used */
32171c23397SDavid Woodhouse fd->raw = jffs2_add_physical_node_ref(c, flash_ofs | dirent_node_state(rd),
32271c23397SDavid Woodhouse PAD(sizeof(*rd)+namelen), f->inocache);
3235bd5c03cSJoakim Tjernlund if (IS_ERR(fd->raw)) {
3245bd5c03cSJoakim Tjernlund void *hold_err = fd->raw;
3255bd5c03cSJoakim Tjernlund /* Release the full_dirent which is now useless, and return */
3265bd5c03cSJoakim Tjernlund jffs2_free_full_dirent(fd);
327e231c2eeSDavid Howells return ERR_CAST(hold_err);
3285bd5c03cSJoakim Tjernlund }
3291da177e4SLinus Torvalds
3301da177e4SLinus Torvalds if (retried) {
331730554d9SArtem B. Bityutskiy jffs2_dbg_acct_sanity_check(c,NULL);
3321da177e4SLinus Torvalds }
3331da177e4SLinus Torvalds
3341da177e4SLinus Torvalds return fd;
3351da177e4SLinus Torvalds }
3361da177e4SLinus Torvalds
3371da177e4SLinus Torvalds /* The OS-specific code fills in the metadata in the jffs2_raw_inode for us, so that
3381da177e4SLinus Torvalds we don't have to go digging in struct inode or its equivalent. It should set:
3391da177e4SLinus Torvalds mode, uid, gid, (starting)isize, atime, ctime, mtime */
jffs2_write_inode_range(struct jffs2_sb_info * c,struct jffs2_inode_info * f,struct jffs2_raw_inode * ri,unsigned char * buf,uint32_t offset,uint32_t writelen,uint32_t * retlen)3401da177e4SLinus Torvalds int jffs2_write_inode_range(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
3411da177e4SLinus Torvalds struct jffs2_raw_inode *ri, unsigned char *buf,
3421da177e4SLinus Torvalds uint32_t offset, uint32_t writelen, uint32_t *retlen)
3431da177e4SLinus Torvalds {
3441da177e4SLinus Torvalds int ret = 0;
3451da177e4SLinus Torvalds uint32_t writtenlen = 0;
3461da177e4SLinus Torvalds
3479c261b33SJoe Perches jffs2_dbg(1, "%s(): Ino #%u, ofs 0x%x, len 0x%x\n",
3489c261b33SJoe Perches __func__, f->inocache->ino, offset, writelen);
3491da177e4SLinus Torvalds
3501da177e4SLinus Torvalds while(writelen) {
3511da177e4SLinus Torvalds struct jffs2_full_dnode *fn;
3521da177e4SLinus Torvalds unsigned char *comprbuf = NULL;
3531da177e4SLinus Torvalds uint16_t comprtype = JFFS2_COMPR_NONE;
3549fe4854cSDavid Woodhouse uint32_t alloclen;
3551da177e4SLinus Torvalds uint32_t datalen, cdatalen;
3561da177e4SLinus Torvalds int retried = 0;
3571da177e4SLinus Torvalds
3581da177e4SLinus Torvalds retry:
3599c261b33SJoe Perches jffs2_dbg(2, "jffs2_commit_write() loop: 0x%x to write to 0x%x\n",
3609c261b33SJoe Perches writelen, offset);
3611da177e4SLinus Torvalds
3629fe4854cSDavid Woodhouse ret = jffs2_reserve_space(c, sizeof(*ri) + JFFS2_MIN_DATA_LEN,
363e631ddbaSFerenc Havasi &alloclen, ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE);
3641da177e4SLinus Torvalds if (ret) {
3659c261b33SJoe Perches jffs2_dbg(1, "jffs2_reserve_space returned %d\n", ret);
3661da177e4SLinus Torvalds break;
3671da177e4SLinus Torvalds }
368ced22070SDavid Woodhouse mutex_lock(&f->sem);
36909cbfeafSKirill A. Shutemov datalen = min_t(uint32_t, writelen,
37009cbfeafSKirill A. Shutemov PAGE_SIZE - (offset & (PAGE_SIZE-1)));
3711da177e4SLinus Torvalds cdatalen = min_t(uint32_t, alloclen - sizeof(*ri), datalen);
3721da177e4SLinus Torvalds
3731da177e4SLinus Torvalds comprtype = jffs2_compress(c, f, buf, &comprbuf, &datalen, &cdatalen);
3741da177e4SLinus Torvalds
3751da177e4SLinus Torvalds ri->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
3761da177e4SLinus Torvalds ri->nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE);
3771da177e4SLinus Torvalds ri->totlen = cpu_to_je32(sizeof(*ri) + cdatalen);
3781da177e4SLinus Torvalds ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4));
3791da177e4SLinus Torvalds
3801da177e4SLinus Torvalds ri->ino = cpu_to_je32(f->inocache->ino);
3811da177e4SLinus Torvalds ri->version = cpu_to_je32(++f->highest_version);
3821da177e4SLinus Torvalds ri->isize = cpu_to_je32(max(je32_to_cpu(ri->isize), offset + datalen));
3831da177e4SLinus Torvalds ri->offset = cpu_to_je32(offset);
3841da177e4SLinus Torvalds ri->csize = cpu_to_je32(cdatalen);
3851da177e4SLinus Torvalds ri->dsize = cpu_to_je32(datalen);
3861da177e4SLinus Torvalds ri->compr = comprtype & 0xff;
3871da177e4SLinus Torvalds ri->usercompr = (comprtype >> 8 ) & 0xff;
3881da177e4SLinus Torvalds ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
3891da177e4SLinus Torvalds ri->data_crc = cpu_to_je32(crc32(0, comprbuf, cdatalen));
3901da177e4SLinus Torvalds
3919fe4854cSDavid Woodhouse fn = jffs2_write_dnode(c, f, ri, comprbuf, cdatalen, ALLOC_NORETRY);
3921da177e4SLinus Torvalds
3931da177e4SLinus Torvalds jffs2_free_comprbuf(comprbuf, buf);
3941da177e4SLinus Torvalds
3951da177e4SLinus Torvalds if (IS_ERR(fn)) {
3961da177e4SLinus Torvalds ret = PTR_ERR(fn);
397ced22070SDavid Woodhouse mutex_unlock(&f->sem);
3981da177e4SLinus Torvalds jffs2_complete_reservation(c);
3991da177e4SLinus Torvalds if (!retried) {
4001da177e4SLinus Torvalds /* Write error to be retried */
4011da177e4SLinus Torvalds retried = 1;
4029c261b33SJoe Perches jffs2_dbg(1, "Retrying node write in jffs2_write_inode_range()\n");
4031da177e4SLinus Torvalds goto retry;
4041da177e4SLinus Torvalds }
4051da177e4SLinus Torvalds break;
4061da177e4SLinus Torvalds }
4071da177e4SLinus Torvalds ret = jffs2_add_full_dnode_to_inode(c, f, fn);
4081da177e4SLinus Torvalds if (f->metadata) {
4091da177e4SLinus Torvalds jffs2_mark_node_obsolete(c, f->metadata->raw);
4101da177e4SLinus Torvalds jffs2_free_full_dnode(f->metadata);
4111da177e4SLinus Torvalds f->metadata = NULL;
4121da177e4SLinus Torvalds }
4131da177e4SLinus Torvalds if (ret) {
4141da177e4SLinus Torvalds /* Eep */
4159c261b33SJoe Perches jffs2_dbg(1, "Eep. add_full_dnode_to_inode() failed in commit_write, returned %d\n",
4169c261b33SJoe Perches ret);
4171da177e4SLinus Torvalds jffs2_mark_node_obsolete(c, fn->raw);
4181da177e4SLinus Torvalds jffs2_free_full_dnode(fn);
4191da177e4SLinus Torvalds
420ced22070SDavid Woodhouse mutex_unlock(&f->sem);
4211da177e4SLinus Torvalds jffs2_complete_reservation(c);
4221da177e4SLinus Torvalds break;
4231da177e4SLinus Torvalds }
424ced22070SDavid Woodhouse mutex_unlock(&f->sem);
4251da177e4SLinus Torvalds jffs2_complete_reservation(c);
4261da177e4SLinus Torvalds if (!datalen) {
427da320f05SJoe Perches pr_warn("Eep. We didn't actually write any data in jffs2_write_inode_range()\n");
4281da177e4SLinus Torvalds ret = -EIO;
4291da177e4SLinus Torvalds break;
4301da177e4SLinus Torvalds }
4319c261b33SJoe Perches jffs2_dbg(1, "increasing writtenlen by %d\n", datalen);
4321da177e4SLinus Torvalds writtenlen += datalen;
4331da177e4SLinus Torvalds offset += datalen;
4341da177e4SLinus Torvalds writelen -= datalen;
4351da177e4SLinus Torvalds buf += datalen;
4361da177e4SLinus Torvalds }
4371da177e4SLinus Torvalds *retlen = writtenlen;
4381da177e4SLinus Torvalds return ret;
4391da177e4SLinus Torvalds }
4401da177e4SLinus Torvalds
jffs2_do_create(struct jffs2_sb_info * c,struct jffs2_inode_info * dir_f,struct jffs2_inode_info * f,struct jffs2_raw_inode * ri,const struct qstr * qstr)4412a7dba39SEric Paris int jffs2_do_create(struct jffs2_sb_info *c, struct jffs2_inode_info *dir_f,
4422a7dba39SEric Paris struct jffs2_inode_info *f, struct jffs2_raw_inode *ri,
4432a7dba39SEric Paris const struct qstr *qstr)
4441da177e4SLinus Torvalds {
4451da177e4SLinus Torvalds struct jffs2_raw_dirent *rd;
4461da177e4SLinus Torvalds struct jffs2_full_dnode *fn;
4471da177e4SLinus Torvalds struct jffs2_full_dirent *fd;
4489fe4854cSDavid Woodhouse uint32_t alloclen;
4491da177e4SLinus Torvalds int ret;
4501da177e4SLinus Torvalds
4511da177e4SLinus Torvalds /* Try to reserve enough space for both node and dirent.
4521da177e4SLinus Torvalds * Just the node will do for now, though
4531da177e4SLinus Torvalds */
4549fe4854cSDavid Woodhouse ret = jffs2_reserve_space(c, sizeof(*ri), &alloclen, ALLOC_NORMAL,
455e631ddbaSFerenc Havasi JFFS2_SUMMARY_INODE_SIZE);
4569c261b33SJoe Perches jffs2_dbg(1, "%s(): reserved 0x%x bytes\n", __func__, alloclen);
457590fe34cSDavid Woodhouse if (ret)
4581da177e4SLinus Torvalds return ret;
459590fe34cSDavid Woodhouse
460590fe34cSDavid Woodhouse mutex_lock(&f->sem);
4611da177e4SLinus Torvalds
4621da177e4SLinus Torvalds ri->data_crc = cpu_to_je32(0);
4631da177e4SLinus Torvalds ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
4641da177e4SLinus Torvalds
4659fe4854cSDavid Woodhouse fn = jffs2_write_dnode(c, f, ri, NULL, 0, ALLOC_NORMAL);
4661da177e4SLinus Torvalds
4679c261b33SJoe Perches jffs2_dbg(1, "jffs2_do_create created file with mode 0x%x\n",
4689c261b33SJoe Perches jemode_to_cpu(ri->mode));
4691da177e4SLinus Torvalds
4701da177e4SLinus Torvalds if (IS_ERR(fn)) {
4719c261b33SJoe Perches jffs2_dbg(1, "jffs2_write_dnode() failed\n");
4721da177e4SLinus Torvalds /* Eeek. Wave bye bye */
473ced22070SDavid Woodhouse mutex_unlock(&f->sem);
4741da177e4SLinus Torvalds jffs2_complete_reservation(c);
4751da177e4SLinus Torvalds return PTR_ERR(fn);
4761da177e4SLinus Torvalds }
4771da177e4SLinus Torvalds /* No data here. Only a metadata node, which will be
4781da177e4SLinus Torvalds obsoleted by the first data write
4791da177e4SLinus Torvalds */
4801da177e4SLinus Torvalds f->metadata = fn;
4811da177e4SLinus Torvalds
482ced22070SDavid Woodhouse mutex_unlock(&f->sem);
4831da177e4SLinus Torvalds jffs2_complete_reservation(c);
484cfc8dc6fSKaiGai Kohei
4852a7dba39SEric Paris ret = jffs2_init_security(&f->vfs_inode, &dir_f->vfs_inode, qstr);
486cfc8dc6fSKaiGai Kohei if (ret)
487cfc8dc6fSKaiGai Kohei return ret;
488cfc8dc6fSKaiGai Kohei ret = jffs2_init_acl_post(&f->vfs_inode);
489cfc8dc6fSKaiGai Kohei if (ret)
490cfc8dc6fSKaiGai Kohei return ret;
491cfc8dc6fSKaiGai Kohei
4922a7dba39SEric Paris ret = jffs2_reserve_space(c, sizeof(*rd)+qstr->len, &alloclen,
4932a7dba39SEric Paris ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(qstr->len));
4941da177e4SLinus Torvalds
4951da177e4SLinus Torvalds if (ret) {
4961da177e4SLinus Torvalds /* Eep. */
4979c261b33SJoe Perches jffs2_dbg(1, "jffs2_reserve_space() for dirent failed\n");
4981da177e4SLinus Torvalds return ret;
4991da177e4SLinus Torvalds }
5001da177e4SLinus Torvalds
5011da177e4SLinus Torvalds rd = jffs2_alloc_raw_dirent();
5021da177e4SLinus Torvalds if (!rd) {
5031da177e4SLinus Torvalds /* Argh. Now we treat it like a normal delete */
5041da177e4SLinus Torvalds jffs2_complete_reservation(c);
5051da177e4SLinus Torvalds return -ENOMEM;
5061da177e4SLinus Torvalds }
5071da177e4SLinus Torvalds
508ced22070SDavid Woodhouse mutex_lock(&dir_f->sem);
5091da177e4SLinus Torvalds
5101da177e4SLinus Torvalds rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
5111da177e4SLinus Torvalds rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
5122a7dba39SEric Paris rd->totlen = cpu_to_je32(sizeof(*rd) + qstr->len);
5131da177e4SLinus Torvalds rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
5141da177e4SLinus Torvalds
5151da177e4SLinus Torvalds rd->pino = cpu_to_je32(dir_f->inocache->ino);
5161da177e4SLinus Torvalds rd->version = cpu_to_je32(++dir_f->highest_version);
5171da177e4SLinus Torvalds rd->ino = ri->ino;
5181da177e4SLinus Torvalds rd->mctime = ri->ctime;
5192a7dba39SEric Paris rd->nsize = qstr->len;
5201da177e4SLinus Torvalds rd->type = DT_REG;
5211da177e4SLinus Torvalds rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
5222a7dba39SEric Paris rd->name_crc = cpu_to_je32(crc32(0, qstr->name, qstr->len));
5231da177e4SLinus Torvalds
5242a7dba39SEric Paris fd = jffs2_write_dirent(c, dir_f, rd, qstr->name, qstr->len, ALLOC_NORMAL);
5251da177e4SLinus Torvalds
5261da177e4SLinus Torvalds jffs2_free_raw_dirent(rd);
5271da177e4SLinus Torvalds
5281da177e4SLinus Torvalds if (IS_ERR(fd)) {
5291da177e4SLinus Torvalds /* dirent failed to write. Delete the inode normally
5301da177e4SLinus Torvalds as if it were the final unlink() */
5311da177e4SLinus Torvalds jffs2_complete_reservation(c);
532ced22070SDavid Woodhouse mutex_unlock(&dir_f->sem);
5331da177e4SLinus Torvalds return PTR_ERR(fd);
5341da177e4SLinus Torvalds }
5351da177e4SLinus Torvalds
5361da177e4SLinus Torvalds /* Link the fd into the inode's list, obsoleting an old
5371da177e4SLinus Torvalds one if necessary. */
5381da177e4SLinus Torvalds jffs2_add_fd_to_list(c, fd, &dir_f->dents);
5391da177e4SLinus Torvalds
5401da177e4SLinus Torvalds jffs2_complete_reservation(c);
541ced22070SDavid Woodhouse mutex_unlock(&dir_f->sem);
5421da177e4SLinus Torvalds
5431da177e4SLinus Torvalds return 0;
5441da177e4SLinus Torvalds }
5451da177e4SLinus Torvalds
5461da177e4SLinus Torvalds
jffs2_do_unlink(struct jffs2_sb_info * c,struct jffs2_inode_info * dir_f,const char * name,int namelen,struct jffs2_inode_info * dead_f,uint32_t time)5471da177e4SLinus Torvalds int jffs2_do_unlink(struct jffs2_sb_info *c, struct jffs2_inode_info *dir_f,
5483a69e0cdSArtem B. Bityutskiy const char *name, int namelen, struct jffs2_inode_info *dead_f,
5493a69e0cdSArtem B. Bityutskiy uint32_t time)
5501da177e4SLinus Torvalds {
5511da177e4SLinus Torvalds struct jffs2_raw_dirent *rd;
5521da177e4SLinus Torvalds struct jffs2_full_dirent *fd;
5539fe4854cSDavid Woodhouse uint32_t alloclen;
5541da177e4SLinus Torvalds int ret;
5551da177e4SLinus Torvalds
556a491486aSJoakim Tjernlund if (!jffs2_can_mark_obsolete(c)) {
5571da177e4SLinus Torvalds /* We can't mark stuff obsolete on the medium. We need to write a deletion dirent */
5581da177e4SLinus Torvalds
5591da177e4SLinus Torvalds rd = jffs2_alloc_raw_dirent();
5601da177e4SLinus Torvalds if (!rd)
5611da177e4SLinus Torvalds return -ENOMEM;
5621da177e4SLinus Torvalds
5639fe4854cSDavid Woodhouse ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
564e631ddbaSFerenc Havasi ALLOC_DELETION, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
5651da177e4SLinus Torvalds if (ret) {
5661da177e4SLinus Torvalds jffs2_free_raw_dirent(rd);
5671da177e4SLinus Torvalds return ret;
5681da177e4SLinus Torvalds }
5691da177e4SLinus Torvalds
570ced22070SDavid Woodhouse mutex_lock(&dir_f->sem);
5711da177e4SLinus Torvalds
5721da177e4SLinus Torvalds /* Build a deletion node */
5731da177e4SLinus Torvalds rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
5741da177e4SLinus Torvalds rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
5751da177e4SLinus Torvalds rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
5761da177e4SLinus Torvalds rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
5771da177e4SLinus Torvalds
5781da177e4SLinus Torvalds rd->pino = cpu_to_je32(dir_f->inocache->ino);
5791da177e4SLinus Torvalds rd->version = cpu_to_je32(++dir_f->highest_version);
5801da177e4SLinus Torvalds rd->ino = cpu_to_je32(0);
5813a69e0cdSArtem B. Bityutskiy rd->mctime = cpu_to_je32(time);
5821da177e4SLinus Torvalds rd->nsize = namelen;
5831da177e4SLinus Torvalds rd->type = DT_UNKNOWN;
5841da177e4SLinus Torvalds rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
5851da177e4SLinus Torvalds rd->name_crc = cpu_to_je32(crc32(0, name, namelen));
5861da177e4SLinus Torvalds
5879fe4854cSDavid Woodhouse fd = jffs2_write_dirent(c, dir_f, rd, name, namelen, ALLOC_DELETION);
5881da177e4SLinus Torvalds
5891da177e4SLinus Torvalds jffs2_free_raw_dirent(rd);
5901da177e4SLinus Torvalds
5911da177e4SLinus Torvalds if (IS_ERR(fd)) {
5921da177e4SLinus Torvalds jffs2_complete_reservation(c);
593ced22070SDavid Woodhouse mutex_unlock(&dir_f->sem);
5941da177e4SLinus Torvalds return PTR_ERR(fd);
5951da177e4SLinus Torvalds }
5961da177e4SLinus Torvalds
5971da177e4SLinus Torvalds /* File it. This will mark the old one obsolete. */
5981da177e4SLinus Torvalds jffs2_add_fd_to_list(c, fd, &dir_f->dents);
599ced22070SDavid Woodhouse mutex_unlock(&dir_f->sem);
6001da177e4SLinus Torvalds } else {
601*8387ff25SLinus Torvalds uint32_t nhash = full_name_hash(NULL, name, namelen);
6021da177e4SLinus Torvalds
603bf66737cSHarvey Harrison fd = dir_f->dents;
604b5748643SDavid Woodhouse /* We don't actually want to reserve any space, but we do
605b5748643SDavid Woodhouse want to be holding the alloc_sem when we write to flash */
606ced22070SDavid Woodhouse mutex_lock(&c->alloc_sem);
607ced22070SDavid Woodhouse mutex_lock(&dir_f->sem);
6081da177e4SLinus Torvalds
60915953580SDavid Woodhouse for (fd = dir_f->dents; fd; fd = fd->next) {
61015953580SDavid Woodhouse if (fd->nhash == nhash &&
61115953580SDavid Woodhouse !memcmp(fd->name, name, namelen) &&
61215953580SDavid Woodhouse !fd->name[namelen]) {
6131da177e4SLinus Torvalds
6149c261b33SJoe Perches jffs2_dbg(1, "Marking old dirent node (ino #%u) @%08x obsolete\n",
6159c261b33SJoe Perches fd->ino, ref_offset(fd->raw));
61615953580SDavid Woodhouse jffs2_mark_node_obsolete(c, fd->raw);
61715953580SDavid Woodhouse /* We don't want to remove it from the list immediately,
61815953580SDavid Woodhouse because that screws up getdents()/seek() semantics even
61915953580SDavid Woodhouse more than they're screwed already. Turn it into a
62015953580SDavid Woodhouse node-less deletion dirent instead -- a placeholder */
62115953580SDavid Woodhouse fd->raw = NULL;
62215953580SDavid Woodhouse fd->ino = 0;
6231da177e4SLinus Torvalds break;
6241da177e4SLinus Torvalds }
6251da177e4SLinus Torvalds }
626ced22070SDavid Woodhouse mutex_unlock(&dir_f->sem);
6271da177e4SLinus Torvalds }
6281da177e4SLinus Torvalds
6291da177e4SLinus Torvalds /* dead_f is NULL if this was a rename not a real unlink */
6301da177e4SLinus Torvalds /* Also catch the !f->inocache case, where there was a dirent
6311da177e4SLinus Torvalds pointing to an inode which didn't exist. */
6321da177e4SLinus Torvalds if (dead_f && dead_f->inocache) {
6331da177e4SLinus Torvalds
634ced22070SDavid Woodhouse mutex_lock(&dead_f->sem);
6351da177e4SLinus Torvalds
63632f1a95dSArtem B. Bityuckiy if (S_ISDIR(OFNI_EDONI_2SFFJ(dead_f)->i_mode)) {
6371da177e4SLinus Torvalds while (dead_f->dents) {
6381da177e4SLinus Torvalds /* There can be only deleted ones */
6391da177e4SLinus Torvalds fd = dead_f->dents;
6401da177e4SLinus Torvalds
6411da177e4SLinus Torvalds dead_f->dents = fd->next;
6421da177e4SLinus Torvalds
6431da177e4SLinus Torvalds if (fd->ino) {
644da320f05SJoe Perches pr_warn("Deleting inode #%u with active dentry \"%s\"->ino #%u\n",
645da320f05SJoe Perches dead_f->inocache->ino,
646da320f05SJoe Perches fd->name, fd->ino);
6471da177e4SLinus Torvalds } else {
6489c261b33SJoe Perches jffs2_dbg(1, "Removing deletion dirent for \"%s\" from dir ino #%u\n",
6499c261b33SJoe Perches fd->name,
6509c261b33SJoe Perches dead_f->inocache->ino);
6511da177e4SLinus Torvalds }
65215953580SDavid Woodhouse if (fd->raw)
6531da177e4SLinus Torvalds jffs2_mark_node_obsolete(c, fd->raw);
6541da177e4SLinus Torvalds jffs2_free_full_dirent(fd);
6551da177e4SLinus Torvalds }
65627c72b04SDavid Woodhouse dead_f->inocache->pino_nlink = 0;
65727c72b04SDavid Woodhouse } else
65827c72b04SDavid Woodhouse dead_f->inocache->pino_nlink--;
6591da177e4SLinus Torvalds /* NB: Caller must set inode nlink if appropriate */
660ced22070SDavid Woodhouse mutex_unlock(&dead_f->sem);
6611da177e4SLinus Torvalds }
6621da177e4SLinus Torvalds
6631da177e4SLinus Torvalds jffs2_complete_reservation(c);
6641da177e4SLinus Torvalds
6651da177e4SLinus Torvalds return 0;
6661da177e4SLinus Torvalds }
6671da177e4SLinus Torvalds
6681da177e4SLinus Torvalds
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)6693a69e0cdSArtem B. Bityutskiy 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)
6701da177e4SLinus Torvalds {
6711da177e4SLinus Torvalds struct jffs2_raw_dirent *rd;
6721da177e4SLinus Torvalds struct jffs2_full_dirent *fd;
6739fe4854cSDavid Woodhouse uint32_t alloclen;
6741da177e4SLinus Torvalds int ret;
6751da177e4SLinus Torvalds
6761da177e4SLinus Torvalds rd = jffs2_alloc_raw_dirent();
6771da177e4SLinus Torvalds if (!rd)
6781da177e4SLinus Torvalds return -ENOMEM;
6791da177e4SLinus Torvalds
6809fe4854cSDavid Woodhouse ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
681e631ddbaSFerenc Havasi ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
6821da177e4SLinus Torvalds if (ret) {
6831da177e4SLinus Torvalds jffs2_free_raw_dirent(rd);
6841da177e4SLinus Torvalds return ret;
6851da177e4SLinus Torvalds }
6861da177e4SLinus Torvalds
687ced22070SDavid Woodhouse mutex_lock(&dir_f->sem);
6881da177e4SLinus Torvalds
6891da177e4SLinus Torvalds /* Build a deletion node */
6901da177e4SLinus Torvalds rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
6911da177e4SLinus Torvalds rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
6921da177e4SLinus Torvalds rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
6931da177e4SLinus Torvalds rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
6941da177e4SLinus Torvalds
6951da177e4SLinus Torvalds rd->pino = cpu_to_je32(dir_f->inocache->ino);
6961da177e4SLinus Torvalds rd->version = cpu_to_je32(++dir_f->highest_version);
6971da177e4SLinus Torvalds rd->ino = cpu_to_je32(ino);
6983a69e0cdSArtem B. Bityutskiy rd->mctime = cpu_to_je32(time);
6991da177e4SLinus Torvalds rd->nsize = namelen;
7001da177e4SLinus Torvalds
7011da177e4SLinus Torvalds rd->type = type;
7021da177e4SLinus Torvalds
7031da177e4SLinus Torvalds rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
7041da177e4SLinus Torvalds rd->name_crc = cpu_to_je32(crc32(0, name, namelen));
7051da177e4SLinus Torvalds
7069fe4854cSDavid Woodhouse fd = jffs2_write_dirent(c, dir_f, rd, name, namelen, ALLOC_NORMAL);
7071da177e4SLinus Torvalds
7081da177e4SLinus Torvalds jffs2_free_raw_dirent(rd);
7091da177e4SLinus Torvalds
7101da177e4SLinus Torvalds if (IS_ERR(fd)) {
7111da177e4SLinus Torvalds jffs2_complete_reservation(c);
712ced22070SDavid Woodhouse mutex_unlock(&dir_f->sem);
7131da177e4SLinus Torvalds return PTR_ERR(fd);
7141da177e4SLinus Torvalds }
7151da177e4SLinus Torvalds
7161da177e4SLinus Torvalds /* File it. This will mark the old one obsolete. */
7171da177e4SLinus Torvalds jffs2_add_fd_to_list(c, fd, &dir_f->dents);
7181da177e4SLinus Torvalds
7191da177e4SLinus Torvalds jffs2_complete_reservation(c);
720ced22070SDavid Woodhouse mutex_unlock(&dir_f->sem);
7211da177e4SLinus Torvalds
7221da177e4SLinus Torvalds return 0;
7231da177e4SLinus Torvalds }
724