]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - fs/jffs2/write.c
Merge branch 'for-linus' of git://git.o-hand.com/linux-rpurdie-leds
[mirror_ubuntu-zesty-kernel.git] / fs / jffs2 / write.c
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 *
10 * $Id: write.c,v 1.97 2005/11/07 11:14:42 gleixner Exp $
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;
38 f->inocache->state = INO_STATE_PRESENT;
39
40 jffs2_add_ino_cache(c, f->inocache);
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);
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
56 /* jffs2_write_dnode - given a raw_inode, allocate a full_dnode for it,
57 write it to the flash, link it into the existing inode/fragment list */
58
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)
62
63 {
64 struct jffs2_full_dnode *fn;
65 size_t retlen;
66 uint32_t flash_ofs;
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
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 }
85
86 fn = jffs2_alloc_full_dnode();
87 if (!fn)
88 return ERR_PTR(-ENOMEM);
89
90 /* check number of valid vecs */
91 if (!datalen || !data)
92 cnt = 1;
93 retry:
94 flash_ofs = write_ofs(c);
95
96 jffs2_dbg_prewrite_paranoia_check(c, flash_ofs, vecs[0].iov_len + vecs[1].iov_len);
97
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, "
101 "highest version %d -> updating dnode\n",
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));
105 }
106
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)) {
111 printk(KERN_NOTICE "Write of %zd bytes at 0x%08x failed. returned %d, retlen %zd\n",
112 sizeof(*ri)+datalen, flash_ofs, ret, retlen);
113
114 /* Mark the space as dirtied */
115 if (retlen) {
116 /* Don't change raw->size to match retlen. We may have
117 written the node header already, and only the data will
118 seem corrupted, in which case the scan would skip over
119 any node we write before the original intended end of
120 this node */
121 jffs2_add_physical_node_ref(c, flash_ofs | REF_OBSOLETE, PAD(sizeof(*ri)+datalen), NULL);
122 } else {
123 printk(KERN_NOTICE "Not marking the space at 0x%08x as dirty because the flash driver returned retlen zero\n", flash_ofs);
124 }
125 if (!retried && alloc_mode != ALLOC_NORETRY) {
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"));
133
134 jffs2_dbg_acct_sanity_check(c,jeb);
135 jffs2_dbg_acct_paranoia_check(c, jeb);
136
137 if (alloc_mode == ALLOC_GC) {
138 ret = jffs2_reserve_space_gc(c, sizeof(*ri) + datalen, &dummy,
139 JFFS2_SUMMARY_INODE_SIZE);
140 } else {
141 /* Locking pain */
142 up(&f->sem);
143 jffs2_complete_reservation(c);
144
145 ret = jffs2_reserve_space(c, sizeof(*ri) + datalen, &dummy,
146 alloc_mode, JFFS2_SUMMARY_INODE_SIZE);
147 down(&f->sem);
148 }
149
150 if (!ret) {
151 flash_ofs = write_ofs(c);
152 D1(printk(KERN_DEBUG "Allocated space at 0x%08x to retry failed write.\n", flash_ofs));
153
154 jffs2_dbg_acct_sanity_check(c,jeb);
155 jffs2_dbg_acct_paranoia_check(c, jeb);
156
157 goto retry;
158 }
159 D1(printk(KERN_DEBUG "Failed to allocate space to retry failed write: %d!\n", ret));
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 */
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.
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)))) {
173 flash_ofs |= REF_PRISTINE;
174 } else {
175 flash_ofs |= REF_NORMAL;
176 }
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;
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",
183 flash_ofs & ~3, flash_ofs & 3, je32_to_cpu(ri->dsize),
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) {
188 jffs2_dbg_acct_sanity_check(c,NULL);
189 }
190
191 return fn;
192 }
193
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)
197 {
198 struct jffs2_full_dirent *fd;
199 size_t retlen;
200 struct kvec vecs[2];
201 uint32_t flash_ofs;
202 int retried = 0;
203 int ret;
204
205 D1(printk(KERN_DEBUG "jffs2_write_dirent(ino #%u, name at *0x%p \"%s\"->ino #%u, name_crc 0x%08x)\n",
206 je32_to_cpu(rd->pino), name, name, je32_to_cpu(rd->ino),
207 je32_to_cpu(rd->name_crc)));
208
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();
212 });
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;
218
219 fd = jffs2_alloc_full_dirent(namelen+1);
220 if (!fd)
221 return ERR_PTR(-ENOMEM);
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:
231 flash_ofs = write_ofs(c);
232
233 jffs2_dbg_prewrite_paranoia_check(c, flash_ofs, vecs[0].iov_len + vecs[1].iov_len);
234
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));
243 }
244
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)) {
248 printk(KERN_NOTICE "Write of %zd bytes at 0x%08x failed. returned %d, retlen %zd\n",
249 sizeof(*rd)+namelen, flash_ofs, ret, retlen);
250 /* Mark the space as dirtied */
251 if (retlen) {
252 jffs2_add_physical_node_ref(c, flash_ofs | REF_OBSOLETE, PAD(sizeof(*rd)+namelen), NULL);
253 } else {
254 printk(KERN_NOTICE "Not marking the space at 0x%08x as dirty because the flash driver returned retlen zero\n", flash_ofs);
255 }
256 if (!retried) {
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
265 jffs2_dbg_acct_sanity_check(c,jeb);
266 jffs2_dbg_acct_paranoia_check(c, jeb);
267
268 if (alloc_mode == ALLOC_GC) {
269 ret = jffs2_reserve_space_gc(c, sizeof(*rd) + namelen, &dummy,
270 JFFS2_SUMMARY_DIRENT_SIZE(namelen));
271 } else {
272 /* Locking pain */
273 up(&f->sem);
274 jffs2_complete_reservation(c);
275
276 ret = jffs2_reserve_space(c, sizeof(*rd) + namelen, &dummy,
277 alloc_mode, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
278 down(&f->sem);
279 }
280
281 if (!ret) {
282 flash_ofs = write_ofs(c);
283 D1(printk(KERN_DEBUG "Allocated space at 0x%08x to retry failed write.\n", flash_ofs));
284 jffs2_dbg_acct_sanity_check(c,jeb);
285 jffs2_dbg_acct_paranoia_check(c, jeb);
286 goto retry;
287 }
288 D1(printk(KERN_DEBUG "Failed to allocate space to retry failed write: %d!\n", ret));
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 */
295 fd->raw = jffs2_add_physical_node_ref(c, flash_ofs | REF_PRISTINE, PAD(sizeof(*rd)+namelen), f->inocache);
296
297 if (retried) {
298 jffs2_dbg_acct_sanity_check(c,NULL);
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,
308 struct jffs2_raw_inode *ri, unsigned char *buf,
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));
316
317 while(writelen) {
318 struct jffs2_full_dnode *fn;
319 unsigned char *comprbuf = NULL;
320 uint16_t comprtype = JFFS2_COMPR_NONE;
321 uint32_t alloclen;
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
328 ret = jffs2_reserve_space(c, sizeof(*ri) + JFFS2_MIN_DATA_LEN,
329 &alloclen, ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE);
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
356 fn = jffs2_write_dnode(c, f, ri, comprbuf, cdatalen, ALLOC_NORETRY);
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;
410 uint32_t alloclen;
411 int ret;
412
413 /* Try to reserve enough space for both node and dirent.
414 * Just the node will do for now, though
415 */
416 ret = jffs2_reserve_space(c, sizeof(*ri), &alloclen, ALLOC_NORMAL,
417 JFFS2_SUMMARY_INODE_SIZE);
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
427 fn = jffs2_write_dnode(c, f, ri, NULL, 0, ALLOC_NORMAL);
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 }
439 /* No data here. Only a metadata node, which will be
440 obsoleted by the first data write
441 */
442 f->metadata = fn;
443
444 up(&f->sem);
445 jffs2_complete_reservation(c);
446 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
447 ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
448
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
478 fd = jffs2_write_dirent(c, dir_f, rd, name, namelen, ALLOC_NORMAL);
479
480 jffs2_free_raw_dirent(rd);
481
482 if (IS_ERR(fd)) {
483 /* dirent failed to write. Delete the inode normally
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,
502 const char *name, int namelen, struct jffs2_inode_info *dead_f,
503 uint32_t time)
504 {
505 struct jffs2_raw_dirent *rd;
506 struct jffs2_full_dirent *fd;
507 uint32_t alloclen;
508 int ret;
509
510 if (1 /* alternative branch needs testing */ ||
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
518 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
519 ALLOC_DELETION, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
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));
532
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);
536 rd->mctime = cpu_to_je32(time);
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
542 fd = jffs2_write_dirent(c, dir_f, rd, name, namelen, ALLOC_DELETION);
543
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) {
562 if ((*prev)->nhash == nhash &&
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. */
583 if (dead_f && dead_f->inocache) {
584
585 down(&dead_f->sem);
586
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;
591
592 dead_f->dents = fd->next;
593
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);
603 }
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
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)
618 {
619 struct jffs2_raw_dirent *rd;
620 struct jffs2_full_dirent *fd;
621 uint32_t alloclen;
622 int ret;
623
624 rd = jffs2_alloc_raw_dirent();
625 if (!rd)
626 return -ENOMEM;
627
628 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
629 ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
630 if (ret) {
631 jffs2_free_raw_dirent(rd);
632 return ret;
633 }
634
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);
646 rd->mctime = cpu_to_je32(time);
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
654 fd = jffs2_write_dirent(c, dir_f, rd, name, namelen, ALLOC_NORMAL);
655
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 }