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