blob: f1b18b99a3cd17bc1a06085653e714d752b80d59 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Gleixner182ec4e2005-11-07 11:16:07 +000010 * $Id: dir.c,v 1.90 2005/11/07 11:14:39 gleixner Exp $
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
12 */
13
14#include <linux/kernel.h>
15#include <linux/slab.h>
16#include <linux/sched.h>
17#include <linux/fs.h>
18#include <linux/crc32.h>
19#include <linux/jffs2.h>
David Woodhousecbb9a562006-05-03 13:07:27 +010020#include "jffs2_fs_i.h"
21#include "jffs2_fs_sb.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/time.h>
23#include "nodelist.h"
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025static int jffs2_readdir (struct file *, void *, filldir_t);
26
27static int jffs2_create (struct inode *,struct dentry *,int,
28 struct nameidata *);
29static struct dentry *jffs2_lookup (struct inode *,struct dentry *,
30 struct nameidata *);
31static int jffs2_link (struct dentry *,struct inode *,struct dentry *);
32static int jffs2_unlink (struct inode *,struct dentry *);
33static int jffs2_symlink (struct inode *,struct dentry *,const char *);
34static int jffs2_mkdir (struct inode *,struct dentry *,int);
35static int jffs2_rmdir (struct inode *,struct dentry *);
David Woodhouse265489f2005-07-06 13:13:13 +010036static int jffs2_mknod (struct inode *,struct dentry *,int,dev_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037static int jffs2_rename (struct inode *, struct dentry *,
38 struct inode *, struct dentry *);
39
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080040const struct file_operations jffs2_dir_operations =
Linus Torvalds1da177e2005-04-16 15:20:36 -070041{
42 .read = generic_read_dir,
43 .readdir = jffs2_readdir,
44 .ioctl = jffs2_ioctl,
45 .fsync = jffs2_fsync
46};
47
48
49struct inode_operations jffs2_dir_inode_operations =
50{
David Woodhouse265489f2005-07-06 13:13:13 +010051 .create = jffs2_create,
52 .lookup = jffs2_lookup,
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 .link = jffs2_link,
54 .unlink = jffs2_unlink,
55 .symlink = jffs2_symlink,
56 .mkdir = jffs2_mkdir,
57 .rmdir = jffs2_rmdir,
58 .mknod = jffs2_mknod,
59 .rename = jffs2_rename,
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +090060 .permission = jffs2_permission,
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 .setattr = jffs2_setattr,
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +090062 .setxattr = jffs2_setxattr,
63 .getxattr = jffs2_getxattr,
64 .listxattr = jffs2_listxattr,
65 .removexattr = jffs2_removexattr
Linus Torvalds1da177e2005-04-16 15:20:36 -070066};
67
68/***********************************************************************/
69
70
71/* We keep the dirent list sorted in increasing order of name hash,
Thomas Gleixner182ec4e2005-11-07 11:16:07 +000072 and we use the same hash function as the dentries. Makes this
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 nice and simple
74*/
75static struct dentry *jffs2_lookup(struct inode *dir_i, struct dentry *target,
76 struct nameidata *nd)
77{
78 struct jffs2_inode_info *dir_f;
79 struct jffs2_sb_info *c;
80 struct jffs2_full_dirent *fd = NULL, *fd_list;
81 uint32_t ino = 0;
82 struct inode *inode = NULL;
83
84 D1(printk(KERN_DEBUG "jffs2_lookup()\n"));
85
Richard Purdie373d5e72006-04-18 02:05:46 +010086 if (target->d_name.len > JFFS2_MAX_NAME_LEN)
87 return ERR_PTR(-ENAMETOOLONG);
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 dir_f = JFFS2_INODE_INFO(dir_i);
90 c = JFFS2_SB_INFO(dir_i->i_sb);
91
92 down(&dir_f->sem);
93
94 /* NB: The 2.2 backport will need to explicitly check for '.' and '..' here */
95 for (fd_list = dir_f->dents; fd_list && fd_list->nhash <= target->d_name.hash; fd_list = fd_list->next) {
Thomas Gleixner182ec4e2005-11-07 11:16:07 +000096 if (fd_list->nhash == target->d_name.hash &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 (!fd || fd_list->version > fd->version) &&
98 strlen(fd_list->name) == target->d_name.len &&
99 !strncmp(fd_list->name, target->d_name.name, target->d_name.len)) {
100 fd = fd_list;
101 }
102 }
103 if (fd)
104 ino = fd->ino;
105 up(&dir_f->sem);
106 if (ino) {
107 inode = iget(dir_i->i_sb, ino);
108 if (!inode) {
109 printk(KERN_WARNING "iget() failed for ino #%u\n", ino);
110 return (ERR_PTR(-EIO));
111 }
112 }
113
114 d_add(target, inode);
115
116 return NULL;
117}
118
119/***********************************************************************/
120
121
122static int jffs2_readdir(struct file *filp, void *dirent, filldir_t filldir)
123{
124 struct jffs2_inode_info *f;
125 struct jffs2_sb_info *c;
126 struct inode *inode = filp->f_dentry->d_inode;
127 struct jffs2_full_dirent *fd;
128 unsigned long offset, curofs;
129
130 D1(printk(KERN_DEBUG "jffs2_readdir() for dir_i #%lu\n", filp->f_dentry->d_inode->i_ino));
131
132 f = JFFS2_INODE_INFO(inode);
133 c = JFFS2_SB_INFO(inode->i_sb);
134
135 offset = filp->f_pos;
136
137 if (offset == 0) {
138 D1(printk(KERN_DEBUG "Dirent 0: \".\", ino #%lu\n", inode->i_ino));
139 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
140 goto out;
141 offset++;
142 }
143 if (offset == 1) {
144 unsigned long pino = parent_ino(filp->f_dentry);
145 D1(printk(KERN_DEBUG "Dirent 1: \"..\", ino #%lu\n", pino));
146 if (filldir(dirent, "..", 2, 1, pino, DT_DIR) < 0)
147 goto out;
148 offset++;
149 }
150
151 curofs=1;
152 down(&f->sem);
153 for (fd = f->dents; fd; fd = fd->next) {
154
155 curofs++;
156 /* First loop: curofs = 2; offset = 2 */
157 if (curofs < offset) {
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000158 D2(printk(KERN_DEBUG "Skipping dirent: \"%s\", ino #%u, type %d, because curofs %ld < offset %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 fd->name, fd->ino, fd->type, curofs, offset));
160 continue;
161 }
162 if (!fd->ino) {
163 D2(printk(KERN_DEBUG "Skipping deletion dirent \"%s\"\n", fd->name));
164 offset++;
165 continue;
166 }
167 D2(printk(KERN_DEBUG "Dirent %ld: \"%s\", ino #%u, type %d\n", offset, fd->name, fd->ino, fd->type));
168 if (filldir(dirent, fd->name, strlen(fd->name), offset, fd->ino, fd->type) < 0)
169 break;
170 offset++;
171 }
172 up(&f->sem);
173 out:
174 filp->f_pos = offset;
175 return 0;
176}
177
178/***********************************************************************/
179
180
181static int jffs2_create(struct inode *dir_i, struct dentry *dentry, int mode,
182 struct nameidata *nd)
183{
184 struct jffs2_raw_inode *ri;
185 struct jffs2_inode_info *f, *dir_f;
186 struct jffs2_sb_info *c;
187 struct inode *inode;
188 int ret;
189
190 ri = jffs2_alloc_raw_inode();
191 if (!ri)
192 return -ENOMEM;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 c = JFFS2_SB_INFO(dir_i->i_sb);
195
196 D1(printk(KERN_DEBUG "jffs2_create()\n"));
197
198 inode = jffs2_new_inode(dir_i, mode, ri);
199
200 if (IS_ERR(inode)) {
201 D1(printk(KERN_DEBUG "jffs2_new_inode() failed\n"));
202 jffs2_free_raw_inode(ri);
203 return PTR_ERR(inode);
204 }
205
206 inode->i_op = &jffs2_file_inode_operations;
207 inode->i_fop = &jffs2_file_operations;
208 inode->i_mapping->a_ops = &jffs2_file_address_operations;
209 inode->i_mapping->nrpages = 0;
210
211 f = JFFS2_INODE_INFO(inode);
212 dir_f = JFFS2_INODE_INFO(dir_i);
213
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000214 ret = jffs2_do_create(c, dir_f, f, ri,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 dentry->d_name.name, dentry->d_name.len);
216
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900217 if (ret)
218 goto fail;
219
220 ret = jffs2_init_security(inode, dir_i);
221 if (ret)
222 goto fail;
223 ret = jffs2_init_acl(inode, dir_i);
224 if (ret)
225 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227 dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(ri->ctime));
228
229 jffs2_free_raw_inode(ri);
230 d_instantiate(dentry, inode);
231
232 D1(printk(KERN_DEBUG "jffs2_create: Created ino #%lu with mode %o, nlink %d(%d). nrpages %ld\n",
233 inode->i_ino, inode->i_mode, inode->i_nlink, f->inocache->nlink, inode->i_mapping->nrpages));
234 return 0;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900235
236 fail:
237 make_bad_inode(inode);
238 iput(inode);
239 jffs2_free_raw_inode(ri);
240 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241}
242
243/***********************************************************************/
244
245
246static int jffs2_unlink(struct inode *dir_i, struct dentry *dentry)
247{
248 struct jffs2_sb_info *c = JFFS2_SB_INFO(dir_i->i_sb);
249 struct jffs2_inode_info *dir_f = JFFS2_INODE_INFO(dir_i);
250 struct jffs2_inode_info *dead_f = JFFS2_INODE_INFO(dentry->d_inode);
251 int ret;
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100252 uint32_t now = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000254 ret = jffs2_do_unlink(c, dir_f, dentry->d_name.name,
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100255 dentry->d_name.len, dead_f, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 if (dead_f->inocache)
257 dentry->d_inode->i_nlink = dead_f->inocache->nlink;
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100258 if (!ret)
259 dir_i->i_mtime = dir_i->i_ctime = ITIME(now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 return ret;
261}
262/***********************************************************************/
263
264
265static int jffs2_link (struct dentry *old_dentry, struct inode *dir_i, struct dentry *dentry)
266{
267 struct jffs2_sb_info *c = JFFS2_SB_INFO(old_dentry->d_inode->i_sb);
268 struct jffs2_inode_info *f = JFFS2_INODE_INFO(old_dentry->d_inode);
269 struct jffs2_inode_info *dir_f = JFFS2_INODE_INFO(dir_i);
270 int ret;
271 uint8_t type;
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100272 uint32_t now;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
274 /* Don't let people make hard links to bad inodes. */
275 if (!f->inocache)
276 return -EIO;
277
278 if (S_ISDIR(old_dentry->d_inode->i_mode))
279 return -EPERM;
280
281 /* XXX: This is ugly */
282 type = (old_dentry->d_inode->i_mode & S_IFMT) >> 12;
283 if (!type) type = DT_REG;
284
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100285 now = get_seconds();
286 ret = jffs2_do_link(c, dir_f, f->inocache->ino, type, dentry->d_name.name, dentry->d_name.len, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288 if (!ret) {
289 down(&f->sem);
290 old_dentry->d_inode->i_nlink = ++f->inocache->nlink;
291 up(&f->sem);
292 d_instantiate(dentry, old_dentry->d_inode);
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100293 dir_i->i_mtime = dir_i->i_ctime = ITIME(now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 atomic_inc(&old_dentry->d_inode->i_count);
295 }
296 return ret;
297}
298
299/***********************************************************************/
300
301static int jffs2_symlink (struct inode *dir_i, struct dentry *dentry, const char *target)
302{
303 struct jffs2_inode_info *f, *dir_f;
304 struct jffs2_sb_info *c;
305 struct inode *inode;
306 struct jffs2_raw_inode *ri;
307 struct jffs2_raw_dirent *rd;
308 struct jffs2_full_dnode *fn;
309 struct jffs2_full_dirent *fd;
310 int namelen;
311 uint32_t alloclen, phys_ofs;
Artem B. Bityuckiy32f1a952005-03-01 10:50:52 +0000312 int ret, targetlen = strlen(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314 /* FIXME: If you care. We'd need to use frags for the target
315 if it grows much more than this */
Artem B. Bityuckiy32f1a952005-03-01 10:50:52 +0000316 if (targetlen > 254)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 return -EINVAL;
318
319 ri = jffs2_alloc_raw_inode();
320
321 if (!ri)
322 return -ENOMEM;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 c = JFFS2_SB_INFO(dir_i->i_sb);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000325
326 /* Try to reserve enough space for both node and dirent.
327 * Just the node will do for now, though
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 */
329 namelen = dentry->d_name.len;
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100330 ret = jffs2_reserve_space(c, sizeof(*ri) + targetlen, &phys_ofs, &alloclen,
331 ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
333 if (ret) {
334 jffs2_free_raw_inode(ri);
335 return ret;
336 }
337
338 inode = jffs2_new_inode(dir_i, S_IFLNK | S_IRWXUGO, ri);
339
340 if (IS_ERR(inode)) {
341 jffs2_free_raw_inode(ri);
342 jffs2_complete_reservation(c);
343 return PTR_ERR(inode);
344 }
345
346 inode->i_op = &jffs2_symlink_inode_operations;
347
348 f = JFFS2_INODE_INFO(inode);
349
Artem B. Bityuckiy32f1a952005-03-01 10:50:52 +0000350 inode->i_size = targetlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 ri->isize = ri->dsize = ri->csize = cpu_to_je32(inode->i_size);
352 ri->totlen = cpu_to_je32(sizeof(*ri) + inode->i_size);
353 ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4));
354
355 ri->compr = JFFS2_COMPR_NONE;
Artem B. Bityuckiy32f1a952005-03-01 10:50:52 +0000356 ri->data_crc = cpu_to_je32(crc32(0, target, targetlen));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000358
Artem B. Bityuckiy32f1a952005-03-01 10:50:52 +0000359 fn = jffs2_write_dnode(c, f, ri, target, targetlen, phys_ofs, ALLOC_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
361 jffs2_free_raw_inode(ri);
362
363 if (IS_ERR(fn)) {
364 /* Eeek. Wave bye bye */
365 up(&f->sem);
366 jffs2_complete_reservation(c);
367 jffs2_clear_inode(inode);
368 return PTR_ERR(fn);
369 }
Artem B. Bityuckiy32f1a952005-03-01 10:50:52 +0000370
Artem B. Bityutskiy2b79adc2005-07-17 12:13:51 +0100371 /* We use f->target field to store the target path. */
372 f->target = kmalloc(targetlen + 1, GFP_KERNEL);
373 if (!f->target) {
Artem B. Bityuckiy32f1a952005-03-01 10:50:52 +0000374 printk(KERN_WARNING "Can't allocate %d bytes of memory\n", targetlen + 1);
375 up(&f->sem);
376 jffs2_complete_reservation(c);
377 jffs2_clear_inode(inode);
378 return -ENOMEM;
379 }
380
Artem B. Bityutskiy2b79adc2005-07-17 12:13:51 +0100381 memcpy(f->target, target, targetlen + 1);
382 D1(printk(KERN_DEBUG "jffs2_symlink: symlink's target '%s' cached\n", (char *)f->target));
Artem B. Bityuckiy32f1a952005-03-01 10:50:52 +0000383
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000384 /* No data here. Only a metadata node, which will be
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 obsoleted by the first data write
386 */
387 f->metadata = fn;
388 up(&f->sem);
389
390 jffs2_complete_reservation(c);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900391
392 ret = jffs2_init_security(inode, dir_i);
393 if (ret) {
394 jffs2_clear_inode(inode);
395 return ret;
396 }
397 ret = jffs2_init_acl(inode, dir_i);
398 if (ret) {
399 jffs2_clear_inode(inode);
400 return ret;
401 }
402
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100403 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &phys_ofs, &alloclen,
404 ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 if (ret) {
406 /* Eep. */
407 jffs2_clear_inode(inode);
408 return ret;
409 }
410
411 rd = jffs2_alloc_raw_dirent();
412 if (!rd) {
413 /* Argh. Now we treat it like a normal delete */
414 jffs2_complete_reservation(c);
415 jffs2_clear_inode(inode);
416 return -ENOMEM;
417 }
418
419 dir_f = JFFS2_INODE_INFO(dir_i);
420 down(&dir_f->sem);
421
422 rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
423 rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
424 rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
425 rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
426
427 rd->pino = cpu_to_je32(dir_i->i_ino);
428 rd->version = cpu_to_je32(++dir_f->highest_version);
429 rd->ino = cpu_to_je32(inode->i_ino);
430 rd->mctime = cpu_to_je32(get_seconds());
431 rd->nsize = namelen;
432 rd->type = DT_LNK;
433 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
434 rd->name_crc = cpu_to_je32(crc32(0, dentry->d_name.name, namelen));
435
436 fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, phys_ofs, ALLOC_NORMAL);
437
438 if (IS_ERR(fd)) {
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000439 /* dirent failed to write. Delete the inode normally
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 as if it were the final unlink() */
441 jffs2_complete_reservation(c);
442 jffs2_free_raw_dirent(rd);
443 up(&dir_f->sem);
444 jffs2_clear_inode(inode);
445 return PTR_ERR(fd);
446 }
447
448 dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(rd->mctime));
449
450 jffs2_free_raw_dirent(rd);
451
452 /* Link the fd into the inode's list, obsoleting an old
453 one if necessary. */
454 jffs2_add_fd_to_list(c, fd, &dir_f->dents);
455
456 up(&dir_f->sem);
457 jffs2_complete_reservation(c);
458
459 d_instantiate(dentry, inode);
460 return 0;
461}
462
463
464static int jffs2_mkdir (struct inode *dir_i, struct dentry *dentry, int mode)
465{
466 struct jffs2_inode_info *f, *dir_f;
467 struct jffs2_sb_info *c;
468 struct inode *inode;
469 struct jffs2_raw_inode *ri;
470 struct jffs2_raw_dirent *rd;
471 struct jffs2_full_dnode *fn;
472 struct jffs2_full_dirent *fd;
473 int namelen;
474 uint32_t alloclen, phys_ofs;
475 int ret;
476
477 mode |= S_IFDIR;
478
479 ri = jffs2_alloc_raw_inode();
480 if (!ri)
481 return -ENOMEM;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 c = JFFS2_SB_INFO(dir_i->i_sb);
484
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000485 /* Try to reserve enough space for both node and dirent.
486 * Just the node will do for now, though
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 */
488 namelen = dentry->d_name.len;
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100489 ret = jffs2_reserve_space(c, sizeof(*ri), &phys_ofs, &alloclen, ALLOC_NORMAL,
490 JFFS2_SUMMARY_INODE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
492 if (ret) {
493 jffs2_free_raw_inode(ri);
494 return ret;
495 }
496
497 inode = jffs2_new_inode(dir_i, mode, ri);
498
499 if (IS_ERR(inode)) {
500 jffs2_free_raw_inode(ri);
501 jffs2_complete_reservation(c);
502 return PTR_ERR(inode);
503 }
504
505 inode->i_op = &jffs2_dir_inode_operations;
506 inode->i_fop = &jffs2_dir_operations;
507 /* Directories get nlink 2 at start */
508 inode->i_nlink = 2;
509
510 f = JFFS2_INODE_INFO(inode);
511
512 ri->data_crc = cpu_to_je32(0);
513 ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 fn = jffs2_write_dnode(c, f, ri, NULL, 0, phys_ofs, ALLOC_NORMAL);
516
517 jffs2_free_raw_inode(ri);
518
519 if (IS_ERR(fn)) {
520 /* Eeek. Wave bye bye */
521 up(&f->sem);
522 jffs2_complete_reservation(c);
523 jffs2_clear_inode(inode);
524 return PTR_ERR(fn);
525 }
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000526 /* No data here. Only a metadata node, which will be
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 obsoleted by the first data write
528 */
529 f->metadata = fn;
530 up(&f->sem);
531
532 jffs2_complete_reservation(c);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900533
534 ret = jffs2_init_security(inode, dir_i);
535 if (ret) {
536 jffs2_clear_inode(inode);
537 return ret;
538 }
539 ret = jffs2_init_acl(inode, dir_i);
540 if (ret) {
541 jffs2_clear_inode(inode);
542 return ret;
543 }
544
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100545 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &phys_ofs, &alloclen,
546 ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 if (ret) {
548 /* Eep. */
549 jffs2_clear_inode(inode);
550 return ret;
551 }
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 rd = jffs2_alloc_raw_dirent();
554 if (!rd) {
555 /* Argh. Now we treat it like a normal delete */
556 jffs2_complete_reservation(c);
557 jffs2_clear_inode(inode);
558 return -ENOMEM;
559 }
560
561 dir_f = JFFS2_INODE_INFO(dir_i);
562 down(&dir_f->sem);
563
564 rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
565 rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
566 rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
567 rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
568
569 rd->pino = cpu_to_je32(dir_i->i_ino);
570 rd->version = cpu_to_je32(++dir_f->highest_version);
571 rd->ino = cpu_to_je32(inode->i_ino);
572 rd->mctime = cpu_to_je32(get_seconds());
573 rd->nsize = namelen;
574 rd->type = DT_DIR;
575 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
576 rd->name_crc = cpu_to_je32(crc32(0, dentry->d_name.name, namelen));
577
578 fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, phys_ofs, ALLOC_NORMAL);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000579
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 if (IS_ERR(fd)) {
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000581 /* dirent failed to write. Delete the inode normally
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 as if it were the final unlink() */
583 jffs2_complete_reservation(c);
584 jffs2_free_raw_dirent(rd);
585 up(&dir_f->sem);
586 jffs2_clear_inode(inode);
587 return PTR_ERR(fd);
588 }
589
590 dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(rd->mctime));
591 dir_i->i_nlink++;
592
593 jffs2_free_raw_dirent(rd);
594
595 /* Link the fd into the inode's list, obsoleting an old
596 one if necessary. */
597 jffs2_add_fd_to_list(c, fd, &dir_f->dents);
598
599 up(&dir_f->sem);
600 jffs2_complete_reservation(c);
601
602 d_instantiate(dentry, inode);
603 return 0;
604}
605
606static int jffs2_rmdir (struct inode *dir_i, struct dentry *dentry)
607{
608 struct jffs2_inode_info *f = JFFS2_INODE_INFO(dentry->d_inode);
609 struct jffs2_full_dirent *fd;
610 int ret;
611
612 for (fd = f->dents ; fd; fd = fd->next) {
613 if (fd->ino)
614 return -ENOTEMPTY;
615 }
616 ret = jffs2_unlink(dir_i, dentry);
617 if (!ret)
618 dir_i->i_nlink--;
619 return ret;
620}
621
David Woodhouse265489f2005-07-06 13:13:13 +0100622static int jffs2_mknod (struct inode *dir_i, struct dentry *dentry, int mode, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
624 struct jffs2_inode_info *f, *dir_f;
625 struct jffs2_sb_info *c;
626 struct inode *inode;
627 struct jffs2_raw_inode *ri;
628 struct jffs2_raw_dirent *rd;
629 struct jffs2_full_dnode *fn;
630 struct jffs2_full_dirent *fd;
631 int namelen;
632 jint16_t dev;
633 int devlen = 0;
634 uint32_t alloclen, phys_ofs;
635 int ret;
636
637 if (!old_valid_dev(rdev))
638 return -EINVAL;
639
640 ri = jffs2_alloc_raw_inode();
641 if (!ri)
642 return -ENOMEM;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000643
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 c = JFFS2_SB_INFO(dir_i->i_sb);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 if (S_ISBLK(mode) || S_ISCHR(mode)) {
647 dev = cpu_to_je16(old_encode_dev(rdev));
648 devlen = sizeof(dev);
649 }
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000650
651 /* Try to reserve enough space for both node and dirent.
652 * Just the node will do for now, though
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 */
654 namelen = dentry->d_name.len;
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100655 ret = jffs2_reserve_space(c, sizeof(*ri) + devlen, &phys_ofs, &alloclen,
656 ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
658 if (ret) {
659 jffs2_free_raw_inode(ri);
660 return ret;
661 }
662
663 inode = jffs2_new_inode(dir_i, mode, ri);
664
665 if (IS_ERR(inode)) {
666 jffs2_free_raw_inode(ri);
667 jffs2_complete_reservation(c);
668 return PTR_ERR(inode);
669 }
670 inode->i_op = &jffs2_file_inode_operations;
671 init_special_inode(inode, inode->i_mode, rdev);
672
673 f = JFFS2_INODE_INFO(inode);
674
675 ri->dsize = ri->csize = cpu_to_je32(devlen);
676 ri->totlen = cpu_to_je32(sizeof(*ri) + devlen);
677 ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4));
678
679 ri->compr = JFFS2_COMPR_NONE;
680 ri->data_crc = cpu_to_je32(crc32(0, &dev, devlen));
681 ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000682
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 fn = jffs2_write_dnode(c, f, ri, (char *)&dev, devlen, phys_ofs, ALLOC_NORMAL);
684
685 jffs2_free_raw_inode(ri);
686
687 if (IS_ERR(fn)) {
688 /* Eeek. Wave bye bye */
689 up(&f->sem);
690 jffs2_complete_reservation(c);
691 jffs2_clear_inode(inode);
692 return PTR_ERR(fn);
693 }
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000694 /* No data here. Only a metadata node, which will be
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 obsoleted by the first data write
696 */
697 f->metadata = fn;
698 up(&f->sem);
699
700 jffs2_complete_reservation(c);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900701
702 ret = jffs2_init_security(inode, dir_i);
703 if (ret) {
704 jffs2_clear_inode(inode);
705 return ret;
706 }
707 ret = jffs2_init_acl(inode, dir_i);
708 if (ret) {
709 jffs2_clear_inode(inode);
710 return ret;
711 }
712
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100713 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &phys_ofs, &alloclen,
714 ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 if (ret) {
716 /* Eep. */
717 jffs2_clear_inode(inode);
718 return ret;
719 }
720
721 rd = jffs2_alloc_raw_dirent();
722 if (!rd) {
723 /* Argh. Now we treat it like a normal delete */
724 jffs2_complete_reservation(c);
725 jffs2_clear_inode(inode);
726 return -ENOMEM;
727 }
728
729 dir_f = JFFS2_INODE_INFO(dir_i);
730 down(&dir_f->sem);
731
732 rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
733 rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
734 rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
735 rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
736
737 rd->pino = cpu_to_je32(dir_i->i_ino);
738 rd->version = cpu_to_je32(++dir_f->highest_version);
739 rd->ino = cpu_to_je32(inode->i_ino);
740 rd->mctime = cpu_to_je32(get_seconds());
741 rd->nsize = namelen;
742
743 /* XXX: This is ugly. */
744 rd->type = (mode & S_IFMT) >> 12;
745
746 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
747 rd->name_crc = cpu_to_je32(crc32(0, dentry->d_name.name, namelen));
748
749 fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, phys_ofs, ALLOC_NORMAL);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000750
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 if (IS_ERR(fd)) {
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000752 /* dirent failed to write. Delete the inode normally
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 as if it were the final unlink() */
754 jffs2_complete_reservation(c);
755 jffs2_free_raw_dirent(rd);
756 up(&dir_f->sem);
757 jffs2_clear_inode(inode);
758 return PTR_ERR(fd);
759 }
760
761 dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(rd->mctime));
762
763 jffs2_free_raw_dirent(rd);
764
765 /* Link the fd into the inode's list, obsoleting an old
766 one if necessary. */
767 jffs2_add_fd_to_list(c, fd, &dir_f->dents);
768
769 up(&dir_f->sem);
770 jffs2_complete_reservation(c);
771
772 d_instantiate(dentry, inode);
773
774 return 0;
775}
776
777static int jffs2_rename (struct inode *old_dir_i, struct dentry *old_dentry,
778 struct inode *new_dir_i, struct dentry *new_dentry)
779{
780 int ret;
781 struct jffs2_sb_info *c = JFFS2_SB_INFO(old_dir_i->i_sb);
782 struct jffs2_inode_info *victim_f = NULL;
783 uint8_t type;
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100784 uint32_t now;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000786 /* The VFS will check for us and prevent trying to rename a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 * file over a directory and vice versa, but if it's a directory,
788 * the VFS can't check whether the victim is empty. The filesystem
789 * needs to do that for itself.
790 */
791 if (new_dentry->d_inode) {
792 victim_f = JFFS2_INODE_INFO(new_dentry->d_inode);
793 if (S_ISDIR(new_dentry->d_inode->i_mode)) {
794 struct jffs2_full_dirent *fd;
795
796 down(&victim_f->sem);
797 for (fd = victim_f->dents; fd; fd = fd->next) {
798 if (fd->ino) {
799 up(&victim_f->sem);
800 return -ENOTEMPTY;
801 }
802 }
803 up(&victim_f->sem);
804 }
805 }
806
807 /* XXX: We probably ought to alloc enough space for
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000808 both nodes at the same time. Writing the new link,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 then getting -ENOSPC, is quite bad :)
810 */
811
812 /* Make a hard link */
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 /* XXX: This is ugly */
815 type = (old_dentry->d_inode->i_mode & S_IFMT) >> 12;
816 if (!type) type = DT_REG;
817
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100818 now = get_seconds();
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000819 ret = jffs2_do_link(c, JFFS2_INODE_INFO(new_dir_i),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 old_dentry->d_inode->i_ino, type,
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100821 new_dentry->d_name.name, new_dentry->d_name.len, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
823 if (ret)
824 return ret;
825
826 if (victim_f) {
827 /* There was a victim. Kill it off nicely */
828 new_dentry->d_inode->i_nlink--;
829 /* Don't oops if the victim was a dirent pointing to an
830 inode which didn't exist. */
831 if (victim_f->inocache) {
832 down(&victim_f->sem);
833 victim_f->inocache->nlink--;
834 up(&victim_f->sem);
835 }
836 }
837
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000838 /* If it was a directory we moved, and there was no victim,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 increase i_nlink on its new parent */
840 if (S_ISDIR(old_dentry->d_inode->i_mode) && !victim_f)
841 new_dir_i->i_nlink++;
842
843 /* Unlink the original */
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000844 ret = jffs2_do_unlink(c, JFFS2_INODE_INFO(old_dir_i),
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100845 old_dentry->d_name.name, old_dentry->d_name.len, NULL, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
847 /* We don't touch inode->i_nlink */
848
849 if (ret) {
850 /* Oh shit. We really ought to make a single node which can do both atomically */
851 struct jffs2_inode_info *f = JFFS2_INODE_INFO(old_dentry->d_inode);
852 down(&f->sem);
853 old_dentry->d_inode->i_nlink++;
854 if (f->inocache)
855 f->inocache->nlink++;
856 up(&f->sem);
857
858 printk(KERN_NOTICE "jffs2_rename(): Link succeeded, unlink failed (err %d). You now have a hard link\n", ret);
859 /* Might as well let the VFS know */
860 d_instantiate(new_dentry, old_dentry->d_inode);
861 atomic_inc(&old_dentry->d_inode->i_count);
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100862 new_dir_i->i_mtime = new_dir_i->i_ctime = ITIME(now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 return ret;
864 }
865
866 if (S_ISDIR(old_dentry->d_inode->i_mode))
867 old_dir_i->i_nlink--;
868
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100869 new_dir_i->i_mtime = new_dir_i->i_ctime = old_dir_i->i_mtime = old_dir_i->i_ctime = ITIME(now);
870
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 return 0;
872}
873