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