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