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