blob: 9fa2e27f06413f7d490ecc552213d99dbe432f73 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/fs.h>
17#include <linux/crc32.h>
18#include <linux/jffs2.h>
David Woodhousecbb9a562006-05-03 13:07:27 +010019#include "jffs2_fs_i.h"
20#include "jffs2_fs_sb.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/time.h>
22#include "nodelist.h"
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024static int jffs2_readdir (struct file *, void *, filldir_t);
25
26static int jffs2_create (struct inode *,struct dentry *,int,
27 struct nameidata *);
28static struct dentry *jffs2_lookup (struct inode *,struct dentry *,
29 struct nameidata *);
30static int jffs2_link (struct dentry *,struct inode *,struct dentry *);
31static int jffs2_unlink (struct inode *,struct dentry *);
32static int jffs2_symlink (struct inode *,struct dentry *,const char *);
33static int jffs2_mkdir (struct inode *,struct dentry *,int);
34static int jffs2_rmdir (struct inode *,struct dentry *);
David Woodhouse265489f2005-07-06 13:13:13 +010035static int jffs2_mknod (struct inode *,struct dentry *,int,dev_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -070036static int jffs2_rename (struct inode *, struct dentry *,
37 struct inode *, struct dentry *);
38
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080039const struct file_operations jffs2_dir_operations =
Linus Torvalds1da177e2005-04-16 15:20:36 -070040{
41 .read = generic_read_dir,
42 .readdir = jffs2_readdir,
43 .ioctl = jffs2_ioctl,
44 .fsync = jffs2_fsync
45};
46
47
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -080048const struct inode_operations jffs2_dir_inode_operations =
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
David Woodhouse265489f2005-07-06 13:13:13 +010050 .create = jffs2_create,
51 .lookup = jffs2_lookup,
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 .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,
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +090059 .permission = jffs2_permission,
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 .setattr = jffs2_setattr,
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +090061 .setxattr = jffs2_setxattr,
62 .getxattr = jffs2_getxattr,
63 .listxattr = jffs2_listxattr,
64 .removexattr = jffs2_removexattr
Linus Torvalds1da177e2005-04-16 15:20:36 -070065};
66
67/***********************************************************************/
68
69
70/* We keep the dirent list sorted in increasing order of name hash,
Thomas Gleixner182ec4e2005-11-07 11:16:07 +000071 and we use the same hash function as the dentries. Makes this
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 nice and simple
73*/
74static 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
Richard Purdie373d5e72006-04-18 02:05:46 +010085 if (target->d_name.len > JFFS2_MAX_NAME_LEN)
86 return ERR_PTR(-ENAMETOOLONG);
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 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) {
Thomas Gleixner182ec4e2005-11-07 11:16:07 +000095 if (fd_list->nhash == target->d_name.hash &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 (!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
121static int jffs2_readdir(struct file *filp, void *dirent, filldir_t filldir)
122{
123 struct jffs2_inode_info *f;
124 struct jffs2_sb_info *c;
Josef Sipekec2e2032006-12-08 02:37:16 -0800125 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 struct jffs2_full_dirent *fd;
127 unsigned long offset, curofs;
128
Josef Sipekec2e2032006-12-08 02:37:16 -0800129 D1(printk(KERN_DEBUG "jffs2_readdir() for dir_i #%lu\n", filp->f_path.dentry->d_inode->i_ino));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
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) {
Josef Sipekec2e2032006-12-08 02:37:16 -0800143 unsigned long pino = parent_ino(filp->f_path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 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) {
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000157 D2(printk(KERN_DEBUG "Skipping dirent: \"%s\", ino #%u, type %d, because curofs %ld < offset %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 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
180static 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;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 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
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000213 ret = jffs2_do_create(c, dir_f, f, ri,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 dentry->d_name.name, dentry->d_name.len);
215
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900216 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
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;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900234
235 fail:
236 make_bad_inode(inode);
237 iput(inode);
238 jffs2_free_raw_inode(ri);
239 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240}
241
242/***********************************************************************/
243
244
245static 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;
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100251 uint32_t now = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000253 ret = jffs2_do_unlink(c, dir_f, dentry->d_name.name,
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100254 dentry->d_name.len, dead_f, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 if (dead_f->inocache)
256 dentry->d_inode->i_nlink = dead_f->inocache->nlink;
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100257 if (!ret)
258 dir_i->i_mtime = dir_i->i_ctime = ITIME(now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 return ret;
260}
261/***********************************************************************/
262
263
264static 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;
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100271 uint32_t now;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
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
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100284 now = get_seconds();
285 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 -0700286
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);
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100292 dir_i->i_mtime = dir_i->i_ctime = ITIME(now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 atomic_inc(&old_dentry->d_inode->i_count);
294 }
295 return ret;
296}
297
298/***********************************************************************/
299
300static 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;
David Woodhouse9fe48542006-05-23 00:38:06 +0100310 uint32_t alloclen;
Artem B. Bityuckiy32f1a952005-03-01 10:50:52 +0000311 int ret, targetlen = strlen(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
313 /* FIXME: If you care. We'd need to use frags for the target
314 if it grows much more than this */
Artem B. Bityuckiy32f1a952005-03-01 10:50:52 +0000315 if (targetlen > 254)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 return -EINVAL;
317
318 ri = jffs2_alloc_raw_inode();
319
320 if (!ri)
321 return -ENOMEM;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 c = JFFS2_SB_INFO(dir_i->i_sb);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000324
325 /* Try to reserve enough space for both node and dirent.
326 * Just the node will do for now, though
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 */
328 namelen = dentry->d_name.len;
David Woodhouse9fe48542006-05-23 00:38:06 +0100329 ret = jffs2_reserve_space(c, sizeof(*ri) + targetlen, &alloclen,
330 ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
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
Artem B. Bityuckiy32f1a952005-03-01 10:50:52 +0000349 inode->i_size = targetlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 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;
Artem B. Bityuckiy32f1a952005-03-01 10:50:52 +0000355 ri->data_crc = cpu_to_je32(crc32(0, target, targetlen));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000357
David Woodhouse9fe48542006-05-23 00:38:06 +0100358 fn = jffs2_write_dnode(c, f, ri, target, targetlen, ALLOC_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
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 }
Artem B. Bityuckiy32f1a952005-03-01 10:50:52 +0000369
Artem B. Bityutskiy2b79adc2005-07-17 12:13:51 +0100370 /* We use f->target field to store the target path. */
371 f->target = kmalloc(targetlen + 1, GFP_KERNEL);
372 if (!f->target) {
Artem B. Bityuckiy32f1a952005-03-01 10:50:52 +0000373 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
Artem B. Bityutskiy2b79adc2005-07-17 12:13:51 +0100380 memcpy(f->target, target, targetlen + 1);
381 D1(printk(KERN_DEBUG "jffs2_symlink: symlink's target '%s' cached\n", (char *)f->target));
Artem B. Bityuckiy32f1a952005-03-01 10:50:52 +0000382
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000383 /* No data here. Only a metadata node, which will be
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 obsoleted by the first data write
385 */
386 f->metadata = fn;
387 up(&f->sem);
388
389 jffs2_complete_reservation(c);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900390
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
David Woodhouse9fe48542006-05-23 00:38:06 +0100402 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
403 ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 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
David Woodhouse9fe48542006-05-23 00:38:06 +0100435 fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, ALLOC_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
437 if (IS_ERR(fd)) {
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000438 /* dirent failed to write. Delete the inode normally
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 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
463static 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;
David Woodhouse9fe48542006-05-23 00:38:06 +0100473 uint32_t alloclen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 int ret;
475
476 mode |= S_IFDIR;
477
478 ri = jffs2_alloc_raw_inode();
479 if (!ri)
480 return -ENOMEM;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 c = JFFS2_SB_INFO(dir_i->i_sb);
483
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000484 /* Try to reserve enough space for both node and dirent.
485 * Just the node will do for now, though
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 */
487 namelen = dentry->d_name.len;
David Woodhouse9fe48542006-05-23 00:38:06 +0100488 ret = jffs2_reserve_space(c, sizeof(*ri), &alloclen, ALLOC_NORMAL,
489 JFFS2_SUMMARY_INODE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
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));
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000513
David Woodhouse9fe48542006-05-23 00:38:06 +0100514 fn = jffs2_write_dnode(c, f, ri, NULL, 0, ALLOC_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
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 }
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000525 /* No data here. Only a metadata node, which will be
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 obsoleted by the first data write
527 */
528 f->metadata = fn;
529 up(&f->sem);
530
531 jffs2_complete_reservation(c);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900532
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
David Woodhouse9fe48542006-05-23 00:38:06 +0100544 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
545 ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 if (ret) {
547 /* Eep. */
548 jffs2_clear_inode(inode);
549 return ret;
550 }
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 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
David Woodhouse9fe48542006-05-23 00:38:06 +0100577 fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, ALLOC_NORMAL);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 if (IS_ERR(fd)) {
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000580 /* dirent failed to write. Delete the inode normally
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 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));
Dave Hansend8c76e62006-09-30 23:29:04 -0700590 inc_nlink(dir_i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
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
605static 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)
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700617 drop_nlink(dir_i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 return ret;
619}
620
David Woodhouse265489f2005-07-06 13:13:13 +0100621static int jffs2_mknod (struct inode *dir_i, struct dentry *dentry, int mode, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
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;
David Woodhouseaef9ab42006-05-19 00:28:49 +0100631 union jffs2_device_node dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 int devlen = 0;
David Woodhouse9fe48542006-05-23 00:38:06 +0100633 uint32_t alloclen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 int ret;
635
David Woodhouseaef9ab42006-05-19 00:28:49 +0100636 if (!new_valid_dev(rdev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 return -EINVAL;
638
639 ri = jffs2_alloc_raw_inode();
640 if (!ri)
641 return -ENOMEM;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000642
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 c = JFFS2_SB_INFO(dir_i->i_sb);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000644
David Woodhouseaef9ab42006-05-19 00:28:49 +0100645 if (S_ISBLK(mode) || S_ISCHR(mode))
646 devlen = jffs2_encode_dev(&dev, rdev);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000647
648 /* Try to reserve enough space for both node and dirent.
649 * Just the node will do for now, though
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 */
651 namelen = dentry->d_name.len;
David Woodhouse9fe48542006-05-23 00:38:06 +0100652 ret = jffs2_reserve_space(c, sizeof(*ri) + devlen, &alloclen,
David Woodhouseaef9ab42006-05-19 00:28:49 +0100653 ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
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));
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000679
David Woodhouse9fe48542006-05-23 00:38:06 +0100680 fn = jffs2_write_dnode(c, f, ri, (char *)&dev, devlen, ALLOC_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
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 }
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000691 /* No data here. Only a metadata node, which will be
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 obsoleted by the first data write
693 */
694 f->metadata = fn;
695 up(&f->sem);
696
697 jffs2_complete_reservation(c);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900698
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
David Woodhouse9fe48542006-05-23 00:38:06 +0100710 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
711 ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 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
David Woodhouse9fe48542006-05-23 00:38:06 +0100746 fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, ALLOC_NORMAL);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 if (IS_ERR(fd)) {
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000749 /* dirent failed to write. Delete the inode normally
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 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
774static 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;
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100781 uint32_t now;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000783 /* The VFS will check for us and prevent trying to rename a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 * 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
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000805 both nodes at the same time. Writing the new link,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 then getting -ENOSPC, is quite bad :)
807 */
808
809 /* Make a hard link */
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000810
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 /* XXX: This is ugly */
812 type = (old_dentry->d_inode->i_mode & S_IFMT) >> 12;
813 if (!type) type = DT_REG;
814
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100815 now = get_seconds();
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000816 ret = jffs2_do_link(c, JFFS2_INODE_INFO(new_dir_i),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 old_dentry->d_inode->i_ino, type,
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100818 new_dentry->d_name.name, new_dentry->d_name.len, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
820 if (ret)
821 return ret;
822
823 if (victim_f) {
824 /* There was a victim. Kill it off nicely */
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700825 drop_nlink(new_dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 /* 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
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000835 /* If it was a directory we moved, and there was no victim,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 increase i_nlink on its new parent */
837 if (S_ISDIR(old_dentry->d_inode->i_mode) && !victim_f)
Dave Hansend8c76e62006-09-30 23:29:04 -0700838 inc_nlink(new_dir_i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
840 /* Unlink the original */
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000841 ret = jffs2_do_unlink(c, JFFS2_INODE_INFO(old_dir_i),
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100842 old_dentry->d_name.name, old_dentry->d_name.len, NULL, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
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);
Dave Hansend8c76e62006-09-30 23:29:04 -0700850 inc_nlink(old_dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 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);
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100859 new_dir_i->i_mtime = new_dir_i->i_ctime = ITIME(now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 return ret;
861 }
862
863 if (S_ISDIR(old_dentry->d_inode->i_mode))
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700864 drop_nlink(old_dir_i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100866 new_dir_i->i_mtime = new_dir_i->i_ctime = old_dir_i->i_mtime = old_dir_i->i_ctime = ITIME(now);
867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 return 0;
869}
870