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