Merge branch 'upstream' into upstream-jgarzik
[deliverable/linux.git] / fs / jffs2 / dir.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: dir.c,v 1.90 2005/11/07 11:14:39 gleixner Exp $
11 *
12 */
13
14 #include <linux/kernel.h>
15 #include <linux/slab.h>
16 #include <linux/fs.h>
17 #include <linux/crc32.h>
18 #include <linux/jffs2.h>
19 #include "jffs2_fs_i.h"
20 #include "jffs2_fs_sb.h"
21 #include <linux/time.h>
22 #include "nodelist.h"
23
24 static int jffs2_readdir (struct file *, void *, filldir_t);
25
26 static int jffs2_create (struct inode *,struct dentry *,int,
27 struct nameidata *);
28 static struct dentry *jffs2_lookup (struct inode *,struct dentry *,
29 struct nameidata *);
30 static int jffs2_link (struct dentry *,struct inode *,struct dentry *);
31 static int jffs2_unlink (struct inode *,struct dentry *);
32 static int jffs2_symlink (struct inode *,struct dentry *,const char *);
33 static int jffs2_mkdir (struct inode *,struct dentry *,int);
34 static int jffs2_rmdir (struct inode *,struct dentry *);
35 static int jffs2_mknod (struct inode *,struct dentry *,int,dev_t);
36 static int jffs2_rename (struct inode *, struct dentry *,
37 struct inode *, struct dentry *);
38
39 const struct file_operations jffs2_dir_operations =
40 {
41 .read = generic_read_dir,
42 .readdir = jffs2_readdir,
43 .ioctl = jffs2_ioctl,
44 .fsync = jffs2_fsync
45 };
46
47
48 const struct inode_operations jffs2_dir_inode_operations =
49 {
50 .create = jffs2_create,
51 .lookup = jffs2_lookup,
52 .link = jffs2_link,
53 .unlink = jffs2_unlink,
54 .symlink = jffs2_symlink,
55 .mkdir = jffs2_mkdir,
56 .rmdir = jffs2_rmdir,
57 .mknod = jffs2_mknod,
58 .rename = jffs2_rename,
59 .permission = jffs2_permission,
60 .setattr = jffs2_setattr,
61 .setxattr = jffs2_setxattr,
62 .getxattr = jffs2_getxattr,
63 .listxattr = jffs2_listxattr,
64 .removexattr = jffs2_removexattr
65 };
66
67 /***********************************************************************/
68
69
70 /* We keep the dirent list sorted in increasing order of name hash,
71 and we use the same hash function as the dentries. Makes this
72 nice and simple
73 */
74 static struct dentry *jffs2_lookup(struct inode *dir_i, struct dentry *target,
75 struct nameidata *nd)
76 {
77 struct jffs2_inode_info *dir_f;
78 struct jffs2_sb_info *c;
79 struct jffs2_full_dirent *fd = NULL, *fd_list;
80 uint32_t ino = 0;
81 struct inode *inode = NULL;
82
83 D1(printk(KERN_DEBUG "jffs2_lookup()\n"));
84
85 if (target->d_name.len > JFFS2_MAX_NAME_LEN)
86 return ERR_PTR(-ENAMETOOLONG);
87
88 dir_f = JFFS2_INODE_INFO(dir_i);
89 c = JFFS2_SB_INFO(dir_i->i_sb);
90
91 down(&dir_f->sem);
92
93 /* NB: The 2.2 backport will need to explicitly check for '.' and '..' here */
94 for (fd_list = dir_f->dents; fd_list && fd_list->nhash <= target->d_name.hash; fd_list = fd_list->next) {
95 if (fd_list->nhash == target->d_name.hash &&
96 (!fd || fd_list->version > fd->version) &&
97 strlen(fd_list->name) == target->d_name.len &&
98 !strncmp(fd_list->name, target->d_name.name, target->d_name.len)) {
99 fd = fd_list;
100 }
101 }
102 if (fd)
103 ino = fd->ino;
104 up(&dir_f->sem);
105 if (ino) {
106 inode = iget(dir_i->i_sb, ino);
107 if (!inode) {
108 printk(KERN_WARNING "iget() failed for ino #%u\n", ino);
109 return (ERR_PTR(-EIO));
110 }
111 }
112
113 d_add(target, inode);
114
115 return NULL;
116 }
117
118 /***********************************************************************/
119
120
121 static int jffs2_readdir(struct file *filp, void *dirent, filldir_t filldir)
122 {
123 struct jffs2_inode_info *f;
124 struct jffs2_sb_info *c;
125 struct inode *inode = filp->f_path.dentry->d_inode;
126 struct jffs2_full_dirent *fd;
127 unsigned long offset, curofs;
128
129 D1(printk(KERN_DEBUG "jffs2_readdir() for dir_i #%lu\n", filp->f_path.dentry->d_inode->i_ino));
130
131 f = JFFS2_INODE_INFO(inode);
132 c = JFFS2_SB_INFO(inode->i_sb);
133
134 offset = filp->f_pos;
135
136 if (offset == 0) {
137 D1(printk(KERN_DEBUG "Dirent 0: \".\", ino #%lu\n", inode->i_ino));
138 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
139 goto out;
140 offset++;
141 }
142 if (offset == 1) {
143 unsigned long pino = parent_ino(filp->f_path.dentry);
144 D1(printk(KERN_DEBUG "Dirent 1: \"..\", ino #%lu\n", pino));
145 if (filldir(dirent, "..", 2, 1, pino, DT_DIR) < 0)
146 goto out;
147 offset++;
148 }
149
150 curofs=1;
151 down(&f->sem);
152 for (fd = f->dents; fd; fd = fd->next) {
153
154 curofs++;
155 /* First loop: curofs = 2; offset = 2 */
156 if (curofs < offset) {
157 D2(printk(KERN_DEBUG "Skipping dirent: \"%s\", ino #%u, type %d, because curofs %ld < offset %ld\n",
158 fd->name, fd->ino, fd->type, curofs, offset));
159 continue;
160 }
161 if (!fd->ino) {
162 D2(printk(KERN_DEBUG "Skipping deletion dirent \"%s\"\n", fd->name));
163 offset++;
164 continue;
165 }
166 D2(printk(KERN_DEBUG "Dirent %ld: \"%s\", ino #%u, type %d\n", offset, fd->name, fd->ino, fd->type));
167 if (filldir(dirent, fd->name, strlen(fd->name), offset, fd->ino, fd->type) < 0)
168 break;
169 offset++;
170 }
171 up(&f->sem);
172 out:
173 filp->f_pos = offset;
174 return 0;
175 }
176
177 /***********************************************************************/
178
179
180 static int jffs2_create(struct inode *dir_i, struct dentry *dentry, int mode,
181 struct nameidata *nd)
182 {
183 struct jffs2_raw_inode *ri;
184 struct jffs2_inode_info *f, *dir_f;
185 struct jffs2_sb_info *c;
186 struct inode *inode;
187 int ret;
188
189 ri = jffs2_alloc_raw_inode();
190 if (!ri)
191 return -ENOMEM;
192
193 c = JFFS2_SB_INFO(dir_i->i_sb);
194
195 D1(printk(KERN_DEBUG "jffs2_create()\n"));
196
197 inode = jffs2_new_inode(dir_i, mode, ri);
198
199 if (IS_ERR(inode)) {
200 D1(printk(KERN_DEBUG "jffs2_new_inode() failed\n"));
201 jffs2_free_raw_inode(ri);
202 return PTR_ERR(inode);
203 }
204
205 inode->i_op = &jffs2_file_inode_operations;
206 inode->i_fop = &jffs2_file_operations;
207 inode->i_mapping->a_ops = &jffs2_file_address_operations;
208 inode->i_mapping->nrpages = 0;
209
210 f = JFFS2_INODE_INFO(inode);
211 dir_f = JFFS2_INODE_INFO(dir_i);
212
213 ret = jffs2_do_create(c, dir_f, f, ri,
214 dentry->d_name.name, dentry->d_name.len);
215
216 if (ret)
217 goto fail;
218
219 ret = jffs2_init_security(inode, dir_i);
220 if (ret)
221 goto fail;
222 ret = jffs2_init_acl(inode, dir_i);
223 if (ret)
224 goto fail;
225
226 dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(ri->ctime));
227
228 jffs2_free_raw_inode(ri);
229 d_instantiate(dentry, inode);
230
231 D1(printk(KERN_DEBUG "jffs2_create: Created ino #%lu with mode %o, nlink %d(%d). nrpages %ld\n",
232 inode->i_ino, inode->i_mode, inode->i_nlink, f->inocache->nlink, inode->i_mapping->nrpages));
233 return 0;
234
235 fail:
236 make_bad_inode(inode);
237 iput(inode);
238 jffs2_free_raw_inode(ri);
239 return ret;
240 }
241
242 /***********************************************************************/
243
244
245 static int jffs2_unlink(struct inode *dir_i, struct dentry *dentry)
246 {
247 struct jffs2_sb_info *c = JFFS2_SB_INFO(dir_i->i_sb);
248 struct jffs2_inode_info *dir_f = JFFS2_INODE_INFO(dir_i);
249 struct jffs2_inode_info *dead_f = JFFS2_INODE_INFO(dentry->d_inode);
250 int ret;
251 uint32_t now = get_seconds();
252
253 ret = jffs2_do_unlink(c, dir_f, dentry->d_name.name,
254 dentry->d_name.len, dead_f, now);
255 if (dead_f->inocache)
256 dentry->d_inode->i_nlink = dead_f->inocache->nlink;
257 if (!ret)
258 dir_i->i_mtime = dir_i->i_ctime = ITIME(now);
259 return ret;
260 }
261 /***********************************************************************/
262
263
264 static int jffs2_link (struct dentry *old_dentry, struct inode *dir_i, struct dentry *dentry)
265 {
266 struct jffs2_sb_info *c = JFFS2_SB_INFO(old_dentry->d_inode->i_sb);
267 struct jffs2_inode_info *f = JFFS2_INODE_INFO(old_dentry->d_inode);
268 struct jffs2_inode_info *dir_f = JFFS2_INODE_INFO(dir_i);
269 int ret;
270 uint8_t type;
271 uint32_t now;
272
273 /* Don't let people make hard links to bad inodes. */
274 if (!f->inocache)
275 return -EIO;
276
277 if (S_ISDIR(old_dentry->d_inode->i_mode))
278 return -EPERM;
279
280 /* XXX: This is ugly */
281 type = (old_dentry->d_inode->i_mode & S_IFMT) >> 12;
282 if (!type) type = DT_REG;
283
284 now = get_seconds();
285 ret = jffs2_do_link(c, dir_f, f->inocache->ino, type, dentry->d_name.name, dentry->d_name.len, now);
286
287 if (!ret) {
288 down(&f->sem);
289 old_dentry->d_inode->i_nlink = ++f->inocache->nlink;
290 up(&f->sem);
291 d_instantiate(dentry, old_dentry->d_inode);
292 dir_i->i_mtime = dir_i->i_ctime = ITIME(now);
293 atomic_inc(&old_dentry->d_inode->i_count);
294 }
295 return ret;
296 }
297
298 /***********************************************************************/
299
300 static int jffs2_symlink (struct inode *dir_i, struct dentry *dentry, const char *target)
301 {
302 struct jffs2_inode_info *f, *dir_f;
303 struct jffs2_sb_info *c;
304 struct inode *inode;
305 struct jffs2_raw_inode *ri;
306 struct jffs2_raw_dirent *rd;
307 struct jffs2_full_dnode *fn;
308 struct jffs2_full_dirent *fd;
309 int namelen;
310 uint32_t alloclen;
311 int ret, targetlen = strlen(target);
312
313 /* FIXME: If you care. We'd need to use frags for the target
314 if it grows much more than this */
315 if (targetlen > 254)
316 return -EINVAL;
317
318 ri = jffs2_alloc_raw_inode();
319
320 if (!ri)
321 return -ENOMEM;
322
323 c = JFFS2_SB_INFO(dir_i->i_sb);
324
325 /* Try to reserve enough space for both node and dirent.
326 * Just the node will do for now, though
327 */
328 namelen = dentry->d_name.len;
329 ret = jffs2_reserve_space(c, sizeof(*ri) + targetlen, &alloclen,
330 ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE);
331
332 if (ret) {
333 jffs2_free_raw_inode(ri);
334 return ret;
335 }
336
337 inode = jffs2_new_inode(dir_i, S_IFLNK | S_IRWXUGO, ri);
338
339 if (IS_ERR(inode)) {
340 jffs2_free_raw_inode(ri);
341 jffs2_complete_reservation(c);
342 return PTR_ERR(inode);
343 }
344
345 inode->i_op = &jffs2_symlink_inode_operations;
346
347 f = JFFS2_INODE_INFO(inode);
348
349 inode->i_size = targetlen;
350 ri->isize = ri->dsize = ri->csize = cpu_to_je32(inode->i_size);
351 ri->totlen = cpu_to_je32(sizeof(*ri) + inode->i_size);
352 ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4));
353
354 ri->compr = JFFS2_COMPR_NONE;
355 ri->data_crc = cpu_to_je32(crc32(0, target, targetlen));
356 ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
357
358 fn = jffs2_write_dnode(c, f, ri, target, targetlen, ALLOC_NORMAL);
359
360 jffs2_free_raw_inode(ri);
361
362 if (IS_ERR(fn)) {
363 /* Eeek. Wave bye bye */
364 up(&f->sem);
365 jffs2_complete_reservation(c);
366 jffs2_clear_inode(inode);
367 return PTR_ERR(fn);
368 }
369
370 /* We use f->target field to store the target path. */
371 f->target = kmalloc(targetlen + 1, GFP_KERNEL);
372 if (!f->target) {
373 printk(KERN_WARNING "Can't allocate %d bytes of memory\n", targetlen + 1);
374 up(&f->sem);
375 jffs2_complete_reservation(c);
376 jffs2_clear_inode(inode);
377 return -ENOMEM;
378 }
379
380 memcpy(f->target, target, targetlen + 1);
381 D1(printk(KERN_DEBUG "jffs2_symlink: symlink's target '%s' cached\n", (char *)f->target));
382
383 /* No data here. Only a metadata node, which will be
384 obsoleted by the first data write
385 */
386 f->metadata = fn;
387 up(&f->sem);
388
389 jffs2_complete_reservation(c);
390
391 ret = jffs2_init_security(inode, dir_i);
392 if (ret) {
393 jffs2_clear_inode(inode);
394 return ret;
395 }
396 ret = jffs2_init_acl(inode, dir_i);
397 if (ret) {
398 jffs2_clear_inode(inode);
399 return ret;
400 }
401
402 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
403 ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
404 if (ret) {
405 /* Eep. */
406 jffs2_clear_inode(inode);
407 return ret;
408 }
409
410 rd = jffs2_alloc_raw_dirent();
411 if (!rd) {
412 /* Argh. Now we treat it like a normal delete */
413 jffs2_complete_reservation(c);
414 jffs2_clear_inode(inode);
415 return -ENOMEM;
416 }
417
418 dir_f = JFFS2_INODE_INFO(dir_i);
419 down(&dir_f->sem);
420
421 rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
422 rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
423 rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
424 rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
425
426 rd->pino = cpu_to_je32(dir_i->i_ino);
427 rd->version = cpu_to_je32(++dir_f->highest_version);
428 rd->ino = cpu_to_je32(inode->i_ino);
429 rd->mctime = cpu_to_je32(get_seconds());
430 rd->nsize = namelen;
431 rd->type = DT_LNK;
432 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
433 rd->name_crc = cpu_to_je32(crc32(0, dentry->d_name.name, namelen));
434
435 fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, ALLOC_NORMAL);
436
437 if (IS_ERR(fd)) {
438 /* dirent failed to write. Delete the inode normally
439 as if it were the final unlink() */
440 jffs2_complete_reservation(c);
441 jffs2_free_raw_dirent(rd);
442 up(&dir_f->sem);
443 jffs2_clear_inode(inode);
444 return PTR_ERR(fd);
445 }
446
447 dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(rd->mctime));
448
449 jffs2_free_raw_dirent(rd);
450
451 /* Link the fd into the inode's list, obsoleting an old
452 one if necessary. */
453 jffs2_add_fd_to_list(c, fd, &dir_f->dents);
454
455 up(&dir_f->sem);
456 jffs2_complete_reservation(c);
457
458 d_instantiate(dentry, inode);
459 return 0;
460 }
461
462
463 static int jffs2_mkdir (struct inode *dir_i, struct dentry *dentry, int mode)
464 {
465 struct jffs2_inode_info *f, *dir_f;
466 struct jffs2_sb_info *c;
467 struct inode *inode;
468 struct jffs2_raw_inode *ri;
469 struct jffs2_raw_dirent *rd;
470 struct jffs2_full_dnode *fn;
471 struct jffs2_full_dirent *fd;
472 int namelen;
473 uint32_t alloclen;
474 int ret;
475
476 mode |= S_IFDIR;
477
478 ri = jffs2_alloc_raw_inode();
479 if (!ri)
480 return -ENOMEM;
481
482 c = JFFS2_SB_INFO(dir_i->i_sb);
483
484 /* Try to reserve enough space for both node and dirent.
485 * Just the node will do for now, though
486 */
487 namelen = dentry->d_name.len;
488 ret = jffs2_reserve_space(c, sizeof(*ri), &alloclen, ALLOC_NORMAL,
489 JFFS2_SUMMARY_INODE_SIZE);
490
491 if (ret) {
492 jffs2_free_raw_inode(ri);
493 return ret;
494 }
495
496 inode = jffs2_new_inode(dir_i, mode, ri);
497
498 if (IS_ERR(inode)) {
499 jffs2_free_raw_inode(ri);
500 jffs2_complete_reservation(c);
501 return PTR_ERR(inode);
502 }
503
504 inode->i_op = &jffs2_dir_inode_operations;
505 inode->i_fop = &jffs2_dir_operations;
506 /* Directories get nlink 2 at start */
507 inode->i_nlink = 2;
508
509 f = JFFS2_INODE_INFO(inode);
510
511 ri->data_crc = cpu_to_je32(0);
512 ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
513
514 fn = jffs2_write_dnode(c, f, ri, NULL, 0, ALLOC_NORMAL);
515
516 jffs2_free_raw_inode(ri);
517
518 if (IS_ERR(fn)) {
519 /* Eeek. Wave bye bye */
520 up(&f->sem);
521 jffs2_complete_reservation(c);
522 jffs2_clear_inode(inode);
523 return PTR_ERR(fn);
524 }
525 /* No data here. Only a metadata node, which will be
526 obsoleted by the first data write
527 */
528 f->metadata = fn;
529 up(&f->sem);
530
531 jffs2_complete_reservation(c);
532
533 ret = jffs2_init_security(inode, dir_i);
534 if (ret) {
535 jffs2_clear_inode(inode);
536 return ret;
537 }
538 ret = jffs2_init_acl(inode, dir_i);
539 if (ret) {
540 jffs2_clear_inode(inode);
541 return ret;
542 }
543
544 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
545 ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
546 if (ret) {
547 /* Eep. */
548 jffs2_clear_inode(inode);
549 return ret;
550 }
551
552 rd = jffs2_alloc_raw_dirent();
553 if (!rd) {
554 /* Argh. Now we treat it like a normal delete */
555 jffs2_complete_reservation(c);
556 jffs2_clear_inode(inode);
557 return -ENOMEM;
558 }
559
560 dir_f = JFFS2_INODE_INFO(dir_i);
561 down(&dir_f->sem);
562
563 rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
564 rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
565 rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
566 rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
567
568 rd->pino = cpu_to_je32(dir_i->i_ino);
569 rd->version = cpu_to_je32(++dir_f->highest_version);
570 rd->ino = cpu_to_je32(inode->i_ino);
571 rd->mctime = cpu_to_je32(get_seconds());
572 rd->nsize = namelen;
573 rd->type = DT_DIR;
574 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
575 rd->name_crc = cpu_to_je32(crc32(0, dentry->d_name.name, namelen));
576
577 fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, ALLOC_NORMAL);
578
579 if (IS_ERR(fd)) {
580 /* dirent failed to write. Delete the inode normally
581 as if it were the final unlink() */
582 jffs2_complete_reservation(c);
583 jffs2_free_raw_dirent(rd);
584 up(&dir_f->sem);
585 jffs2_clear_inode(inode);
586 return PTR_ERR(fd);
587 }
588
589 dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(rd->mctime));
590 inc_nlink(dir_i);
591
592 jffs2_free_raw_dirent(rd);
593
594 /* Link the fd into the inode's list, obsoleting an old
595 one if necessary. */
596 jffs2_add_fd_to_list(c, fd, &dir_f->dents);
597
598 up(&dir_f->sem);
599 jffs2_complete_reservation(c);
600
601 d_instantiate(dentry, inode);
602 return 0;
603 }
604
605 static int jffs2_rmdir (struct inode *dir_i, struct dentry *dentry)
606 {
607 struct jffs2_inode_info *f = JFFS2_INODE_INFO(dentry->d_inode);
608 struct jffs2_full_dirent *fd;
609 int ret;
610
611 for (fd = f->dents ; fd; fd = fd->next) {
612 if (fd->ino)
613 return -ENOTEMPTY;
614 }
615 ret = jffs2_unlink(dir_i, dentry);
616 if (!ret)
617 drop_nlink(dir_i);
618 return ret;
619 }
620
621 static int jffs2_mknod (struct inode *dir_i, struct dentry *dentry, int mode, dev_t rdev)
622 {
623 struct jffs2_inode_info *f, *dir_f;
624 struct jffs2_sb_info *c;
625 struct inode *inode;
626 struct jffs2_raw_inode *ri;
627 struct jffs2_raw_dirent *rd;
628 struct jffs2_full_dnode *fn;
629 struct jffs2_full_dirent *fd;
630 int namelen;
631 union jffs2_device_node dev;
632 int devlen = 0;
633 uint32_t alloclen;
634 int ret;
635
636 if (!new_valid_dev(rdev))
637 return -EINVAL;
638
639 ri = jffs2_alloc_raw_inode();
640 if (!ri)
641 return -ENOMEM;
642
643 c = JFFS2_SB_INFO(dir_i->i_sb);
644
645 if (S_ISBLK(mode) || S_ISCHR(mode))
646 devlen = jffs2_encode_dev(&dev, rdev);
647
648 /* Try to reserve enough space for both node and dirent.
649 * Just the node will do for now, though
650 */
651 namelen = dentry->d_name.len;
652 ret = jffs2_reserve_space(c, sizeof(*ri) + devlen, &alloclen,
653 ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE);
654
655 if (ret) {
656 jffs2_free_raw_inode(ri);
657 return ret;
658 }
659
660 inode = jffs2_new_inode(dir_i, mode, ri);
661
662 if (IS_ERR(inode)) {
663 jffs2_free_raw_inode(ri);
664 jffs2_complete_reservation(c);
665 return PTR_ERR(inode);
666 }
667 inode->i_op = &jffs2_file_inode_operations;
668 init_special_inode(inode, inode->i_mode, rdev);
669
670 f = JFFS2_INODE_INFO(inode);
671
672 ri->dsize = ri->csize = cpu_to_je32(devlen);
673 ri->totlen = cpu_to_je32(sizeof(*ri) + devlen);
674 ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4));
675
676 ri->compr = JFFS2_COMPR_NONE;
677 ri->data_crc = cpu_to_je32(crc32(0, &dev, devlen));
678 ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
679
680 fn = jffs2_write_dnode(c, f, ri, (char *)&dev, devlen, ALLOC_NORMAL);
681
682 jffs2_free_raw_inode(ri);
683
684 if (IS_ERR(fn)) {
685 /* Eeek. Wave bye bye */
686 up(&f->sem);
687 jffs2_complete_reservation(c);
688 jffs2_clear_inode(inode);
689 return PTR_ERR(fn);
690 }
691 /* No data here. Only a metadata node, which will be
692 obsoleted by the first data write
693 */
694 f->metadata = fn;
695 up(&f->sem);
696
697 jffs2_complete_reservation(c);
698
699 ret = jffs2_init_security(inode, dir_i);
700 if (ret) {
701 jffs2_clear_inode(inode);
702 return ret;
703 }
704 ret = jffs2_init_acl(inode, dir_i);
705 if (ret) {
706 jffs2_clear_inode(inode);
707 return ret;
708 }
709
710 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
711 ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
712 if (ret) {
713 /* Eep. */
714 jffs2_clear_inode(inode);
715 return ret;
716 }
717
718 rd = jffs2_alloc_raw_dirent();
719 if (!rd) {
720 /* Argh. Now we treat it like a normal delete */
721 jffs2_complete_reservation(c);
722 jffs2_clear_inode(inode);
723 return -ENOMEM;
724 }
725
726 dir_f = JFFS2_INODE_INFO(dir_i);
727 down(&dir_f->sem);
728
729 rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
730 rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
731 rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
732 rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
733
734 rd->pino = cpu_to_je32(dir_i->i_ino);
735 rd->version = cpu_to_je32(++dir_f->highest_version);
736 rd->ino = cpu_to_je32(inode->i_ino);
737 rd->mctime = cpu_to_je32(get_seconds());
738 rd->nsize = namelen;
739
740 /* XXX: This is ugly. */
741 rd->type = (mode & S_IFMT) >> 12;
742
743 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
744 rd->name_crc = cpu_to_je32(crc32(0, dentry->d_name.name, namelen));
745
746 fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, ALLOC_NORMAL);
747
748 if (IS_ERR(fd)) {
749 /* dirent failed to write. Delete the inode normally
750 as if it were the final unlink() */
751 jffs2_complete_reservation(c);
752 jffs2_free_raw_dirent(rd);
753 up(&dir_f->sem);
754 jffs2_clear_inode(inode);
755 return PTR_ERR(fd);
756 }
757
758 dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(rd->mctime));
759
760 jffs2_free_raw_dirent(rd);
761
762 /* Link the fd into the inode's list, obsoleting an old
763 one if necessary. */
764 jffs2_add_fd_to_list(c, fd, &dir_f->dents);
765
766 up(&dir_f->sem);
767 jffs2_complete_reservation(c);
768
769 d_instantiate(dentry, inode);
770
771 return 0;
772 }
773
774 static int jffs2_rename (struct inode *old_dir_i, struct dentry *old_dentry,
775 struct inode *new_dir_i, struct dentry *new_dentry)
776 {
777 int ret;
778 struct jffs2_sb_info *c = JFFS2_SB_INFO(old_dir_i->i_sb);
779 struct jffs2_inode_info *victim_f = NULL;
780 uint8_t type;
781 uint32_t now;
782
783 /* The VFS will check for us and prevent trying to rename a
784 * file over a directory and vice versa, but if it's a directory,
785 * the VFS can't check whether the victim is empty. The filesystem
786 * needs to do that for itself.
787 */
788 if (new_dentry->d_inode) {
789 victim_f = JFFS2_INODE_INFO(new_dentry->d_inode);
790 if (S_ISDIR(new_dentry->d_inode->i_mode)) {
791 struct jffs2_full_dirent *fd;
792
793 down(&victim_f->sem);
794 for (fd = victim_f->dents; fd; fd = fd->next) {
795 if (fd->ino) {
796 up(&victim_f->sem);
797 return -ENOTEMPTY;
798 }
799 }
800 up(&victim_f->sem);
801 }
802 }
803
804 /* XXX: We probably ought to alloc enough space for
805 both nodes at the same time. Writing the new link,
806 then getting -ENOSPC, is quite bad :)
807 */
808
809 /* Make a hard link */
810
811 /* XXX: This is ugly */
812 type = (old_dentry->d_inode->i_mode & S_IFMT) >> 12;
813 if (!type) type = DT_REG;
814
815 now = get_seconds();
816 ret = jffs2_do_link(c, JFFS2_INODE_INFO(new_dir_i),
817 old_dentry->d_inode->i_ino, type,
818 new_dentry->d_name.name, new_dentry->d_name.len, now);
819
820 if (ret)
821 return ret;
822
823 if (victim_f) {
824 /* There was a victim. Kill it off nicely */
825 drop_nlink(new_dentry->d_inode);
826 /* Don't oops if the victim was a dirent pointing to an
827 inode which didn't exist. */
828 if (victim_f->inocache) {
829 down(&victim_f->sem);
830 victim_f->inocache->nlink--;
831 up(&victim_f->sem);
832 }
833 }
834
835 /* If it was a directory we moved, and there was no victim,
836 increase i_nlink on its new parent */
837 if (S_ISDIR(old_dentry->d_inode->i_mode) && !victim_f)
838 inc_nlink(new_dir_i);
839
840 /* Unlink the original */
841 ret = jffs2_do_unlink(c, JFFS2_INODE_INFO(old_dir_i),
842 old_dentry->d_name.name, old_dentry->d_name.len, NULL, now);
843
844 /* We don't touch inode->i_nlink */
845
846 if (ret) {
847 /* Oh shit. We really ought to make a single node which can do both atomically */
848 struct jffs2_inode_info *f = JFFS2_INODE_INFO(old_dentry->d_inode);
849 down(&f->sem);
850 inc_nlink(old_dentry->d_inode);
851 if (f->inocache)
852 f->inocache->nlink++;
853 up(&f->sem);
854
855 printk(KERN_NOTICE "jffs2_rename(): Link succeeded, unlink failed (err %d). You now have a hard link\n", ret);
856 /* Might as well let the VFS know */
857 d_instantiate(new_dentry, old_dentry->d_inode);
858 atomic_inc(&old_dentry->d_inode->i_count);
859 new_dir_i->i_mtime = new_dir_i->i_ctime = ITIME(now);
860 return ret;
861 }
862
863 if (S_ISDIR(old_dentry->d_inode->i_mode))
864 drop_nlink(old_dir_i);
865
866 new_dir_i->i_mtime = new_dir_i->i_ctime = old_dir_i->i_mtime = old_dir_i->i_ctime = ITIME(now);
867
868 return 0;
869 }
870
This page took 0.052556 seconds and 6 git commands to generate.