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