]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * JFFS2 -- Journalling Flash File System, Version 2. | |
3 | * | |
c00c310e | 4 | * Copyright © 2001-2007 Red Hat, Inc. |
1da177e4 LT |
5 | * |
6 | * Created by David Woodhouse <dwmw2@infradead.org> | |
7 | * | |
8 | * For licensing information, see the file 'LICENCE' in this directory. | |
9 | * | |
1da177e4 LT |
10 | */ |
11 | ||
5a528957 JP |
12 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
13 | ||
1da177e4 LT |
14 | #include <linux/kernel.h> |
15 | #include <linux/fs.h> | |
16 | #include <linux/crc32.h> | |
1da177e4 LT |
17 | #include <linux/pagemap.h> |
18 | #include <linux/mtd/mtd.h> | |
19 | #include "nodelist.h" | |
20 | #include "compr.h" | |
21 | ||
22 | ||
27c72b04 DW |
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) | |
1da177e4 LT |
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; | |
27c72b04 | 36 | f->inocache->pino_nlink = 1; /* Will be overwritten shortly for directories */ |
1da177e4 | 37 | f->inocache->nodes = (struct jffs2_raw_node_ref *)f->inocache; |
1da177e4 | 38 | f->inocache->state = INO_STATE_PRESENT; |
1da177e4 | 39 | |
1da177e4 | 40 | jffs2_add_ino_cache(c, f->inocache); |
9c261b33 | 41 | jffs2_dbg(1, "%s(): Assigned ino# %d\n", __func__, f->inocache->ino); |
7d200960 | 42 | ri->ino = cpu_to_je32(f->inocache->ino); |
1da177e4 LT |
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 | ||
182ec4ee | 56 | /* jffs2_write_dnode - given a raw_inode, allocate a full_dnode for it, |
1da177e4 LT |
57 | write it to the flash, link it into the existing inode/fragment list */ |
58 | ||
9fe4854c DW |
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) | |
1da177e4 LT |
62 | |
63 | { | |
1da177e4 LT |
64 | struct jffs2_full_dnode *fn; |
65 | size_t retlen; | |
9fe4854c | 66 | uint32_t flash_ofs; |
1da177e4 LT |
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)) { | |
da320f05 | 73 | pr_crit("Eep. CRC not correct in jffs2_write_dnode()\n"); |
1da177e4 LT |
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 | ||
1da177e4 | 82 | if (je32_to_cpu(ri->totlen) != sizeof(*ri) + datalen) { |
da320f05 JP |
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); | |
1da177e4 | 86 | } |
182ec4ee | 87 | |
1da177e4 | 88 | fn = jffs2_alloc_full_dnode(); |
2f785402 | 89 | if (!fn) |
1da177e4 | 90 | return ERR_PTR(-ENOMEM); |
1da177e4 LT |
91 | |
92 | /* check number of valid vecs */ | |
93 | if (!datalen || !data) | |
94 | cnt = 1; | |
95 | retry: | |
2f785402 | 96 | flash_ofs = write_ofs(c); |
9fe4854c DW |
97 | |
98 | jffs2_dbg_prewrite_paranoia_check(c, flash_ofs, vecs[0].iov_len + vecs[1].iov_len); | |
1da177e4 | 99 | |
9b88f473 EH |
100 | if ((alloc_mode!=ALLOC_GC) && (je32_to_cpu(ri->version) < f->highest_version)) { |
101 | BUG_ON(!retried); | |
9c261b33 JP |
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); | |
9b88f473 EH |
105 | ri->version = cpu_to_je32(++f->highest_version); |
106 | ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8)); | |
e4803c30 EH |
107 | } |
108 | ||
1da177e4 LT |
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)) { | |
da320f05 JP |
113 | pr_notice("Write of %zd bytes at 0x%08x failed. returned %d, retlen %zd\n", |
114 | sizeof(*ri) + datalen, flash_ofs, ret, retlen); | |
1da177e4 LT |
115 | |
116 | /* Mark the space as dirtied */ | |
117 | if (retlen) { | |
182ec4ee | 118 | /* Don't change raw->size to match retlen. We may have |
1da177e4 LT |
119 | written the node header already, and only the data will |
120 | seem corrupted, in which case the scan would skip over | |
182ec4ee | 121 | any node we write before the original intended end of |
1da177e4 | 122 | this node */ |
2f785402 | 123 | jffs2_add_physical_node_ref(c, flash_ofs | REF_OBSOLETE, PAD(sizeof(*ri)+datalen), NULL); |
1da177e4 | 124 | } else { |
da320f05 JP |
125 | pr_notice("Not marking the space at 0x%08x as dirty because the flash driver returned retlen zero\n", |
126 | flash_ofs); | |
1da177e4 | 127 | } |
2f785402 | 128 | if (!retried && alloc_mode != ALLOC_NORETRY) { |
1da177e4 LT |
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 | ||
9c261b33 | 135 | jffs2_dbg(1, "Retrying failed write.\n"); |
182ec4ee | 136 | |
730554d9 AB |
137 | jffs2_dbg_acct_sanity_check(c,jeb); |
138 | jffs2_dbg_acct_paranoia_check(c, jeb); | |
1da177e4 LT |
139 | |
140 | if (alloc_mode == ALLOC_GC) { | |
9fe4854c DW |
141 | ret = jffs2_reserve_space_gc(c, sizeof(*ri) + datalen, &dummy, |
142 | JFFS2_SUMMARY_INODE_SIZE); | |
1da177e4 LT |
143 | } else { |
144 | /* Locking pain */ | |
ced22070 | 145 | mutex_unlock(&f->sem); |
1da177e4 | 146 | jffs2_complete_reservation(c); |
182ec4ee | 147 | |
9fe4854c DW |
148 | ret = jffs2_reserve_space(c, sizeof(*ri) + datalen, &dummy, |
149 | alloc_mode, JFFS2_SUMMARY_INODE_SIZE); | |
ced22070 | 150 | mutex_lock(&f->sem); |
1da177e4 LT |
151 | } |
152 | ||
153 | if (!ret) { | |
9fe4854c | 154 | flash_ofs = write_ofs(c); |
9c261b33 JP |
155 | jffs2_dbg(1, "Allocated space at 0x%08x to retry failed write.\n", |
156 | flash_ofs); | |
1da177e4 | 157 | |
730554d9 AB |
158 | jffs2_dbg_acct_sanity_check(c,jeb); |
159 | jffs2_dbg_acct_paranoia_check(c, jeb); | |
1da177e4 LT |
160 | |
161 | goto retry; | |
162 | } | |
9c261b33 JP |
163 | jffs2_dbg(1, "Failed to allocate space to retry failed write: %d!\n", |
164 | ret); | |
1da177e4 LT |
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 */ | |
182ec4ee TG |
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. | |
1da177e4 | 174 | */ |
09cbfeaf KS |
175 | if ((je32_to_cpu(ri->dsize) >= PAGE_SIZE) || |
176 | ( ((je32_to_cpu(ri->offset)&(PAGE_SIZE-1))==0) && | |
1da177e4 | 177 | (je32_to_cpu(ri->dsize)+je32_to_cpu(ri->offset) == je32_to_cpu(ri->isize)))) { |
2f785402 | 178 | flash_ofs |= REF_PRISTINE; |
1da177e4 | 179 | } else { |
2f785402 | 180 | flash_ofs |= REF_NORMAL; |
1da177e4 | 181 | } |
2f785402 | 182 | fn->raw = jffs2_add_physical_node_ref(c, flash_ofs, PAD(sizeof(*ri)+datalen), f->inocache); |
5bd5c03c JT |
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); | |
e231c2ee | 187 | return ERR_CAST(hold_err); |
5bd5c03c | 188 | } |
2f785402 DW |
189 | fn->ofs = je32_to_cpu(ri->offset); |
190 | fn->size = je32_to_cpu(ri->dsize); | |
191 | fn->frags = 0; | |
1da177e4 | 192 | |
9c261b33 | 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", |
2f785402 | 194 | flash_ofs & ~3, flash_ofs & 3, je32_to_cpu(ri->dsize), |
1da177e4 | 195 | je32_to_cpu(ri->csize), je32_to_cpu(ri->node_crc), |
9c261b33 | 196 | je32_to_cpu(ri->data_crc), je32_to_cpu(ri->totlen)); |
1da177e4 LT |
197 | |
198 | if (retried) { | |
730554d9 | 199 | jffs2_dbg_acct_sanity_check(c,NULL); |
1da177e4 LT |
200 | } |
201 | ||
202 | return fn; | |
203 | } | |
204 | ||
9fe4854c DW |
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) | |
1da177e4 | 208 | { |
1da177e4 LT |
209 | struct jffs2_full_dirent *fd; |
210 | size_t retlen; | |
211 | struct kvec vecs[2]; | |
2f785402 | 212 | uint32_t flash_ofs; |
1da177e4 LT |
213 | int retried = 0; |
214 | int ret; | |
215 | ||
9c261b33 JP |
216 | jffs2_dbg(1, "%s(ino #%u, name at *0x%p \"%s\"->ino #%u, name_crc 0x%08x)\n", |
217 | __func__, | |
1da177e4 | 218 | je32_to_cpu(rd->pino), name, name, je32_to_cpu(rd->ino), |
9c261b33 | 219 | je32_to_cpu(rd->name_crc)); |
730554d9 | 220 | |
1da177e4 | 221 | D1(if(je32_to_cpu(rd->hdr_crc) != crc32(0, rd, sizeof(struct jffs2_unknown_node)-4)) { |
da320f05 | 222 | pr_crit("Eep. CRC not correct in jffs2_write_dirent()\n"); |
1da177e4 | 223 | BUG(); |
2f785402 | 224 | }); |
1da177e4 | 225 | |
69ca4378 DW |
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 */ | |
da320f05 JP |
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)); | |
69ca4378 DW |
233 | WARN_ON(1); |
234 | return ERR_PTR(-EIO); | |
235 | } | |
236 | ||
1da177e4 LT |
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; | |
182ec4ee | 241 | |
1da177e4 | 242 | fd = jffs2_alloc_full_dirent(namelen+1); |
2f785402 | 243 | if (!fd) |
1da177e4 | 244 | return ERR_PTR(-ENOMEM); |
1da177e4 LT |
245 | |
246 | fd->version = je32_to_cpu(rd->version); | |
247 | fd->ino = je32_to_cpu(rd->ino); | |
8387ff25 | 248 | fd->nhash = full_name_hash(NULL, name, namelen); |
1da177e4 LT |
249 | fd->type = rd->type; |
250 | memcpy(fd->name, name, namelen); | |
251 | fd->name[namelen]=0; | |
252 | ||
253 | retry: | |
2f785402 | 254 | flash_ofs = write_ofs(c); |
1da177e4 | 255 | |
2f785402 | 256 | jffs2_dbg_prewrite_paranoia_check(c, flash_ofs, vecs[0].iov_len + vecs[1].iov_len); |
1da177e4 | 257 | |
9b88f473 EH |
258 | if ((alloc_mode!=ALLOC_GC) && (je32_to_cpu(rd->version) < f->highest_version)) { |
259 | BUG_ON(!retried); | |
9c261b33 JP |
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); | |
9b88f473 EH |
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)); | |
e4803c30 EH |
266 | } |
267 | ||
1da177e4 LT |
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)) { | |
da320f05 JP |
271 | pr_notice("Write of %zd bytes at 0x%08x failed. returned %d, retlen %zd\n", |
272 | sizeof(*rd) + namelen, flash_ofs, ret, retlen); | |
1da177e4 LT |
273 | /* Mark the space as dirtied */ |
274 | if (retlen) { | |
2f785402 | 275 | jffs2_add_physical_node_ref(c, flash_ofs | REF_OBSOLETE, PAD(sizeof(*rd)+namelen), NULL); |
1da177e4 | 276 | } else { |
da320f05 JP |
277 | pr_notice("Not marking the space at 0x%08x as dirty because the flash driver returned retlen zero\n", |
278 | flash_ofs); | |
1da177e4 | 279 | } |
2f785402 | 280 | if (!retried) { |
1da177e4 LT |
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 | ||
9c261b33 | 287 | jffs2_dbg(1, "Retrying failed write.\n"); |
1da177e4 | 288 | |
730554d9 AB |
289 | jffs2_dbg_acct_sanity_check(c,jeb); |
290 | jffs2_dbg_acct_paranoia_check(c, jeb); | |
1da177e4 LT |
291 | |
292 | if (alloc_mode == ALLOC_GC) { | |
9fe4854c DW |
293 | ret = jffs2_reserve_space_gc(c, sizeof(*rd) + namelen, &dummy, |
294 | JFFS2_SUMMARY_DIRENT_SIZE(namelen)); | |
1da177e4 LT |
295 | } else { |
296 | /* Locking pain */ | |
ced22070 | 297 | mutex_unlock(&f->sem); |
1da177e4 | 298 | jffs2_complete_reservation(c); |
182ec4ee | 299 | |
9fe4854c DW |
300 | ret = jffs2_reserve_space(c, sizeof(*rd) + namelen, &dummy, |
301 | alloc_mode, JFFS2_SUMMARY_DIRENT_SIZE(namelen)); | |
ced22070 | 302 | mutex_lock(&f->sem); |
1da177e4 LT |
303 | } |
304 | ||
305 | if (!ret) { | |
9fe4854c | 306 | flash_ofs = write_ofs(c); |
9c261b33 JP |
307 | jffs2_dbg(1, "Allocated space at 0x%08x to retry failed write\n", |
308 | flash_ofs); | |
730554d9 AB |
309 | jffs2_dbg_acct_sanity_check(c,jeb); |
310 | jffs2_dbg_acct_paranoia_check(c, jeb); | |
1da177e4 LT |
311 | goto retry; |
312 | } | |
9c261b33 JP |
313 | jffs2_dbg(1, "Failed to allocate space to retry failed write: %d!\n", |
314 | ret); | |
1da177e4 LT |
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 */ | |
71c23397 DW |
321 | fd->raw = jffs2_add_physical_node_ref(c, flash_ofs | dirent_node_state(rd), |
322 | PAD(sizeof(*rd)+namelen), f->inocache); | |
5bd5c03c JT |
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); | |
e231c2ee | 327 | return ERR_CAST(hold_err); |
5bd5c03c | 328 | } |
1da177e4 LT |
329 | |
330 | if (retried) { | |
730554d9 | 331 | jffs2_dbg_acct_sanity_check(c,NULL); |
1da177e4 LT |
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, | |
182ec4ee | 341 | struct jffs2_raw_inode *ri, unsigned char *buf, |
1da177e4 LT |
342 | uint32_t offset, uint32_t writelen, uint32_t *retlen) |
343 | { | |
344 | int ret = 0; | |
345 | uint32_t writtenlen = 0; | |
346 | ||
9c261b33 JP |
347 | jffs2_dbg(1, "%s(): Ino #%u, ofs 0x%x, len 0x%x\n", |
348 | __func__, f->inocache->ino, offset, writelen); | |
182ec4ee | 349 | |
1da177e4 LT |
350 | while(writelen) { |
351 | struct jffs2_full_dnode *fn; | |
352 | unsigned char *comprbuf = NULL; | |
353 | uint16_t comprtype = JFFS2_COMPR_NONE; | |
9fe4854c | 354 | uint32_t alloclen; |
1da177e4 LT |
355 | uint32_t datalen, cdatalen; |
356 | int retried = 0; | |
357 | ||
358 | retry: | |
9c261b33 JP |
359 | jffs2_dbg(2, "jffs2_commit_write() loop: 0x%x to write to 0x%x\n", |
360 | writelen, offset); | |
1da177e4 | 361 | |
9fe4854c | 362 | ret = jffs2_reserve_space(c, sizeof(*ri) + JFFS2_MIN_DATA_LEN, |
e631ddba | 363 | &alloclen, ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE); |
1da177e4 | 364 | if (ret) { |
9c261b33 | 365 | jffs2_dbg(1, "jffs2_reserve_space returned %d\n", ret); |
1da177e4 LT |
366 | break; |
367 | } | |
ced22070 | 368 | mutex_lock(&f->sem); |
09cbfeaf KS |
369 | datalen = min_t(uint32_t, writelen, |
370 | PAGE_SIZE - (offset & (PAGE_SIZE-1))); | |
1da177e4 LT |
371 | cdatalen = min_t(uint32_t, alloclen - sizeof(*ri), datalen); |
372 | ||
373 | comprtype = jffs2_compress(c, f, buf, &comprbuf, &datalen, &cdatalen); | |
374 | ||
375 | ri->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK); | |
376 | ri->nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE); | |
377 | ri->totlen = cpu_to_je32(sizeof(*ri) + cdatalen); | |
378 | ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4)); | |
379 | ||
380 | ri->ino = cpu_to_je32(f->inocache->ino); | |
381 | ri->version = cpu_to_je32(++f->highest_version); | |
382 | ri->isize = cpu_to_je32(max(je32_to_cpu(ri->isize), offset + datalen)); | |
383 | ri->offset = cpu_to_je32(offset); | |
384 | ri->csize = cpu_to_je32(cdatalen); | |
385 | ri->dsize = cpu_to_je32(datalen); | |
386 | ri->compr = comprtype & 0xff; | |
387 | ri->usercompr = (comprtype >> 8 ) & 0xff; | |
388 | ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8)); | |
389 | ri->data_crc = cpu_to_je32(crc32(0, comprbuf, cdatalen)); | |
390 | ||
9fe4854c | 391 | fn = jffs2_write_dnode(c, f, ri, comprbuf, cdatalen, ALLOC_NORETRY); |
1da177e4 LT |
392 | |
393 | jffs2_free_comprbuf(comprbuf, buf); | |
394 | ||
395 | if (IS_ERR(fn)) { | |
396 | ret = PTR_ERR(fn); | |
ced22070 | 397 | mutex_unlock(&f->sem); |
1da177e4 LT |
398 | jffs2_complete_reservation(c); |
399 | if (!retried) { | |
400 | /* Write error to be retried */ | |
401 | retried = 1; | |
9c261b33 | 402 | jffs2_dbg(1, "Retrying node write in jffs2_write_inode_range()\n"); |
1da177e4 LT |
403 | goto retry; |
404 | } | |
405 | break; | |
406 | } | |
407 | ret = jffs2_add_full_dnode_to_inode(c, f, fn); | |
408 | if (f->metadata) { | |
409 | jffs2_mark_node_obsolete(c, f->metadata->raw); | |
410 | jffs2_free_full_dnode(f->metadata); | |
411 | f->metadata = NULL; | |
412 | } | |
413 | if (ret) { | |
414 | /* Eep */ | |
9c261b33 JP |
415 | jffs2_dbg(1, "Eep. add_full_dnode_to_inode() failed in commit_write, returned %d\n", |
416 | ret); | |
1da177e4 LT |
417 | jffs2_mark_node_obsolete(c, fn->raw); |
418 | jffs2_free_full_dnode(fn); | |
419 | ||
ced22070 | 420 | mutex_unlock(&f->sem); |
1da177e4 LT |
421 | jffs2_complete_reservation(c); |
422 | break; | |
423 | } | |
ced22070 | 424 | mutex_unlock(&f->sem); |
1da177e4 LT |
425 | jffs2_complete_reservation(c); |
426 | if (!datalen) { | |
da320f05 | 427 | pr_warn("Eep. We didn't actually write any data in jffs2_write_inode_range()\n"); |
1da177e4 LT |
428 | ret = -EIO; |
429 | break; | |
430 | } | |
9c261b33 | 431 | jffs2_dbg(1, "increasing writtenlen by %d\n", datalen); |
1da177e4 LT |
432 | writtenlen += datalen; |
433 | offset += datalen; | |
434 | writelen -= datalen; | |
435 | buf += datalen; | |
436 | } | |
437 | *retlen = writtenlen; | |
438 | return ret; | |
439 | } | |
440 | ||
2a7dba39 EP |
441 | int jffs2_do_create(struct jffs2_sb_info *c, struct jffs2_inode_info *dir_f, |
442 | struct jffs2_inode_info *f, struct jffs2_raw_inode *ri, | |
443 | const struct qstr *qstr) | |
1da177e4 LT |
444 | { |
445 | struct jffs2_raw_dirent *rd; | |
446 | struct jffs2_full_dnode *fn; | |
447 | struct jffs2_full_dirent *fd; | |
9fe4854c | 448 | uint32_t alloclen; |
1da177e4 LT |
449 | int ret; |
450 | ||
182ec4ee TG |
451 | /* Try to reserve enough space for both node and dirent. |
452 | * Just the node will do for now, though | |
1da177e4 | 453 | */ |
9fe4854c | 454 | ret = jffs2_reserve_space(c, sizeof(*ri), &alloclen, ALLOC_NORMAL, |
e631ddba | 455 | JFFS2_SUMMARY_INODE_SIZE); |
9c261b33 | 456 | jffs2_dbg(1, "%s(): reserved 0x%x bytes\n", __func__, alloclen); |
590fe34c | 457 | if (ret) |
1da177e4 | 458 | return ret; |
590fe34c DW |
459 | |
460 | mutex_lock(&f->sem); | |
1da177e4 LT |
461 | |
462 | ri->data_crc = cpu_to_je32(0); | |
463 | ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8)); | |
464 | ||
9fe4854c | 465 | fn = jffs2_write_dnode(c, f, ri, NULL, 0, ALLOC_NORMAL); |
1da177e4 | 466 | |
9c261b33 JP |
467 | jffs2_dbg(1, "jffs2_do_create created file with mode 0x%x\n", |
468 | jemode_to_cpu(ri->mode)); | |
1da177e4 LT |
469 | |
470 | if (IS_ERR(fn)) { | |
9c261b33 | 471 | jffs2_dbg(1, "jffs2_write_dnode() failed\n"); |
1da177e4 | 472 | /* Eeek. Wave bye bye */ |
ced22070 | 473 | mutex_unlock(&f->sem); |
1da177e4 LT |
474 | jffs2_complete_reservation(c); |
475 | return PTR_ERR(fn); | |
476 | } | |
182ec4ee | 477 | /* No data here. Only a metadata node, which will be |
1da177e4 LT |
478 | obsoleted by the first data write |
479 | */ | |
480 | f->metadata = fn; | |
481 | ||
ced22070 | 482 | mutex_unlock(&f->sem); |
1da177e4 | 483 | jffs2_complete_reservation(c); |
cfc8dc6f | 484 | |
2a7dba39 | 485 | ret = jffs2_init_security(&f->vfs_inode, &dir_f->vfs_inode, qstr); |
cfc8dc6f KK |
486 | if (ret) |
487 | return ret; | |
488 | ret = jffs2_init_acl_post(&f->vfs_inode); | |
489 | if (ret) | |
490 | return ret; | |
491 | ||
2a7dba39 EP |
492 | ret = jffs2_reserve_space(c, sizeof(*rd)+qstr->len, &alloclen, |
493 | ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(qstr->len)); | |
182ec4ee | 494 | |
1da177e4 LT |
495 | if (ret) { |
496 | /* Eep. */ | |
9c261b33 | 497 | jffs2_dbg(1, "jffs2_reserve_space() for dirent failed\n"); |
1da177e4 LT |
498 | return ret; |
499 | } | |
500 | ||
501 | rd = jffs2_alloc_raw_dirent(); | |
502 | if (!rd) { | |
503 | /* Argh. Now we treat it like a normal delete */ | |
504 | jffs2_complete_reservation(c); | |
505 | return -ENOMEM; | |
506 | } | |
507 | ||
ced22070 | 508 | mutex_lock(&dir_f->sem); |
1da177e4 LT |
509 | |
510 | rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK); | |
511 | rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT); | |
2a7dba39 | 512 | rd->totlen = cpu_to_je32(sizeof(*rd) + qstr->len); |
1da177e4 LT |
513 | rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4)); |
514 | ||
515 | rd->pino = cpu_to_je32(dir_f->inocache->ino); | |
516 | rd->version = cpu_to_je32(++dir_f->highest_version); | |
517 | rd->ino = ri->ino; | |
518 | rd->mctime = ri->ctime; | |
2a7dba39 | 519 | rd->nsize = qstr->len; |
1da177e4 LT |
520 | rd->type = DT_REG; |
521 | rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8)); | |
2a7dba39 | 522 | rd->name_crc = cpu_to_je32(crc32(0, qstr->name, qstr->len)); |
1da177e4 | 523 | |
2a7dba39 | 524 | fd = jffs2_write_dirent(c, dir_f, rd, qstr->name, qstr->len, ALLOC_NORMAL); |
1da177e4 LT |
525 | |
526 | jffs2_free_raw_dirent(rd); | |
182ec4ee | 527 | |
1da177e4 | 528 | if (IS_ERR(fd)) { |
182ec4ee | 529 | /* dirent failed to write. Delete the inode normally |
1da177e4 LT |
530 | as if it were the final unlink() */ |
531 | jffs2_complete_reservation(c); | |
ced22070 | 532 | mutex_unlock(&dir_f->sem); |
1da177e4 LT |
533 | return PTR_ERR(fd); |
534 | } | |
535 | ||
536 | /* Link the fd into the inode's list, obsoleting an old | |
537 | one if necessary. */ | |
538 | jffs2_add_fd_to_list(c, fd, &dir_f->dents); | |
539 | ||
540 | jffs2_complete_reservation(c); | |
ced22070 | 541 | mutex_unlock(&dir_f->sem); |
1da177e4 LT |
542 | |
543 | return 0; | |
544 | } | |
545 | ||
546 | ||
547 | int jffs2_do_unlink(struct jffs2_sb_info *c, struct jffs2_inode_info *dir_f, | |
3a69e0cd AB |
548 | const char *name, int namelen, struct jffs2_inode_info *dead_f, |
549 | uint32_t time) | |
1da177e4 LT |
550 | { |
551 | struct jffs2_raw_dirent *rd; | |
552 | struct jffs2_full_dirent *fd; | |
9fe4854c | 553 | uint32_t alloclen; |
1da177e4 LT |
554 | int ret; |
555 | ||
a491486a | 556 | if (!jffs2_can_mark_obsolete(c)) { |
1da177e4 LT |
557 | /* We can't mark stuff obsolete on the medium. We need to write a deletion dirent */ |
558 | ||
559 | rd = jffs2_alloc_raw_dirent(); | |
560 | if (!rd) | |
561 | return -ENOMEM; | |
562 | ||
9fe4854c | 563 | ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen, |
e631ddba | 564 | ALLOC_DELETION, JFFS2_SUMMARY_DIRENT_SIZE(namelen)); |
1da177e4 LT |
565 | if (ret) { |
566 | jffs2_free_raw_dirent(rd); | |
567 | return ret; | |
568 | } | |
569 | ||
ced22070 | 570 | mutex_lock(&dir_f->sem); |
1da177e4 LT |
571 | |
572 | /* Build a deletion node */ | |
573 | rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK); | |
574 | rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT); | |
575 | rd->totlen = cpu_to_je32(sizeof(*rd) + namelen); | |
576 | rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4)); | |
182ec4ee | 577 | |
1da177e4 LT |
578 | rd->pino = cpu_to_je32(dir_f->inocache->ino); |
579 | rd->version = cpu_to_je32(++dir_f->highest_version); | |
580 | rd->ino = cpu_to_je32(0); | |
3a69e0cd | 581 | rd->mctime = cpu_to_je32(time); |
1da177e4 LT |
582 | rd->nsize = namelen; |
583 | rd->type = DT_UNKNOWN; | |
584 | rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8)); | |
585 | rd->name_crc = cpu_to_je32(crc32(0, name, namelen)); | |
586 | ||
9fe4854c | 587 | fd = jffs2_write_dirent(c, dir_f, rd, name, namelen, ALLOC_DELETION); |
182ec4ee | 588 | |
1da177e4 LT |
589 | jffs2_free_raw_dirent(rd); |
590 | ||
591 | if (IS_ERR(fd)) { | |
592 | jffs2_complete_reservation(c); | |
ced22070 | 593 | mutex_unlock(&dir_f->sem); |
1da177e4 LT |
594 | return PTR_ERR(fd); |
595 | } | |
596 | ||
597 | /* File it. This will mark the old one obsolete. */ | |
598 | jffs2_add_fd_to_list(c, fd, &dir_f->dents); | |
ced22070 | 599 | mutex_unlock(&dir_f->sem); |
1da177e4 | 600 | } else { |
8387ff25 | 601 | uint32_t nhash = full_name_hash(NULL, name, namelen); |
1da177e4 | 602 | |
bf66737c | 603 | fd = dir_f->dents; |
b5748643 DW |
604 | /* We don't actually want to reserve any space, but we do |
605 | want to be holding the alloc_sem when we write to flash */ | |
ced22070 DW |
606 | mutex_lock(&c->alloc_sem); |
607 | mutex_lock(&dir_f->sem); | |
1da177e4 | 608 | |
15953580 DW |
609 | for (fd = dir_f->dents; fd; fd = fd->next) { |
610 | if (fd->nhash == nhash && | |
611 | !memcmp(fd->name, name, namelen) && | |
612 | !fd->name[namelen]) { | |
1da177e4 | 613 | |
9c261b33 JP |
614 | jffs2_dbg(1, "Marking old dirent node (ino #%u) @%08x obsolete\n", |
615 | fd->ino, ref_offset(fd->raw)); | |
15953580 DW |
616 | jffs2_mark_node_obsolete(c, fd->raw); |
617 | /* We don't want to remove it from the list immediately, | |
618 | because that screws up getdents()/seek() semantics even | |
619 | more than they're screwed already. Turn it into a | |
620 | node-less deletion dirent instead -- a placeholder */ | |
621 | fd->raw = NULL; | |
622 | fd->ino = 0; | |
1da177e4 LT |
623 | break; |
624 | } | |
1da177e4 | 625 | } |
ced22070 | 626 | mutex_unlock(&dir_f->sem); |
1da177e4 LT |
627 | } |
628 | ||
629 | /* dead_f is NULL if this was a rename not a real unlink */ | |
630 | /* Also catch the !f->inocache case, where there was a dirent | |
631 | pointing to an inode which didn't exist. */ | |
182ec4ee | 632 | if (dead_f && dead_f->inocache) { |
1da177e4 | 633 | |
ced22070 | 634 | mutex_lock(&dead_f->sem); |
1da177e4 | 635 | |
32f1a95d AB |
636 | if (S_ISDIR(OFNI_EDONI_2SFFJ(dead_f)->i_mode)) { |
637 | while (dead_f->dents) { | |
638 | /* There can be only deleted ones */ | |
639 | fd = dead_f->dents; | |
182ec4ee | 640 | |
32f1a95d | 641 | dead_f->dents = fd->next; |
182ec4ee | 642 | |
32f1a95d | 643 | if (fd->ino) { |
da320f05 JP |
644 | pr_warn("Deleting inode #%u with active dentry \"%s\"->ino #%u\n", |
645 | dead_f->inocache->ino, | |
646 | fd->name, fd->ino); | |
32f1a95d | 647 | } else { |
9c261b33 JP |
648 | jffs2_dbg(1, "Removing deletion dirent for \"%s\" from dir ino #%u\n", |
649 | fd->name, | |
650 | dead_f->inocache->ino); | |
32f1a95d | 651 | } |
15953580 DW |
652 | if (fd->raw) |
653 | jffs2_mark_node_obsolete(c, fd->raw); | |
32f1a95d | 654 | jffs2_free_full_dirent(fd); |
1da177e4 | 655 | } |
27c72b04 DW |
656 | dead_f->inocache->pino_nlink = 0; |
657 | } else | |
658 | dead_f->inocache->pino_nlink--; | |
1da177e4 | 659 | /* NB: Caller must set inode nlink if appropriate */ |
ced22070 | 660 | mutex_unlock(&dead_f->sem); |
1da177e4 LT |
661 | } |
662 | ||
663 | jffs2_complete_reservation(c); | |
664 | ||
665 | return 0; | |
666 | } | |
667 | ||
668 | ||
3a69e0cd | 669 | 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) |
1da177e4 LT |
670 | { |
671 | struct jffs2_raw_dirent *rd; | |
672 | struct jffs2_full_dirent *fd; | |
9fe4854c | 673 | uint32_t alloclen; |
1da177e4 LT |
674 | int ret; |
675 | ||
676 | rd = jffs2_alloc_raw_dirent(); | |
677 | if (!rd) | |
678 | return -ENOMEM; | |
679 | ||
9fe4854c | 680 | ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen, |
e631ddba | 681 | ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen)); |
1da177e4 LT |
682 | if (ret) { |
683 | jffs2_free_raw_dirent(rd); | |
684 | return ret; | |
685 | } | |
182ec4ee | 686 | |
ced22070 | 687 | mutex_lock(&dir_f->sem); |
1da177e4 LT |
688 | |
689 | /* Build a deletion node */ | |
690 | rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK); | |
691 | rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT); | |
692 | rd->totlen = cpu_to_je32(sizeof(*rd) + namelen); | |
693 | rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4)); | |
694 | ||
695 | rd->pino = cpu_to_je32(dir_f->inocache->ino); | |
696 | rd->version = cpu_to_je32(++dir_f->highest_version); | |
697 | rd->ino = cpu_to_je32(ino); | |
3a69e0cd | 698 | rd->mctime = cpu_to_je32(time); |
1da177e4 LT |
699 | rd->nsize = namelen; |
700 | ||
701 | rd->type = type; | |
702 | ||
703 | rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8)); | |
704 | rd->name_crc = cpu_to_je32(crc32(0, name, namelen)); | |
705 | ||
9fe4854c | 706 | fd = jffs2_write_dirent(c, dir_f, rd, name, namelen, ALLOC_NORMAL); |
182ec4ee | 707 | |
1da177e4 LT |
708 | jffs2_free_raw_dirent(rd); |
709 | ||
710 | if (IS_ERR(fd)) { | |
711 | jffs2_complete_reservation(c); | |
ced22070 | 712 | mutex_unlock(&dir_f->sem); |
1da177e4 LT |
713 | return PTR_ERR(fd); |
714 | } | |
715 | ||
716 | /* File it. This will mark the old one obsolete. */ | |
717 | jffs2_add_fd_to_list(c, fd, &dir_f->dents); | |
718 | ||
719 | jffs2_complete_reservation(c); | |
ced22070 | 720 | mutex_unlock(&dir_f->sem); |
1da177e4 LT |
721 | |
722 | return 0; | |
723 | } |