blob: 621bdfa994e71268d0f85f27e938765e12937c8e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * JFFS2 -- Journalling Flash File System, Version 2.
3 *
David Woodhousec00c3102007-04-25 14:16:47 +01004 * Copyright © 2001-2007 Red Hat, Inc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * Created by David Woodhouse <dwmw2@infradead.org>
7 *
8 * For licensing information, see the file 'LICENCE' in this directory.
9 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
11
12#include <linux/kernel.h>
13#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/fs.h>
15#include <linux/crc32.h>
16#include <linux/jffs2.h>
David Woodhousecbb9a562006-05-03 13:07:27 +010017#include "jffs2_fs_i.h"
18#include "jffs2_fs_sb.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/time.h>
20#include "nodelist.h"
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022static int jffs2_readdir (struct file *, void *, filldir_t);
23
24static int jffs2_create (struct inode *,struct dentry *,int,
25 struct nameidata *);
26static struct dentry *jffs2_lookup (struct inode *,struct dentry *,
27 struct nameidata *);
28static int jffs2_link (struct dentry *,struct inode *,struct dentry *);
29static int jffs2_unlink (struct inode *,struct dentry *);
30static int jffs2_symlink (struct inode *,struct dentry *,const char *);
31static int jffs2_mkdir (struct inode *,struct dentry *,int);
32static int jffs2_rmdir (struct inode *,struct dentry *);
David Woodhouse265489f2005-07-06 13:13:13 +010033static int jffs2_mknod (struct inode *,struct dentry *,int,dev_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034static int jffs2_rename (struct inode *, struct dentry *,
David Woodhouseef53cb02007-07-10 10:01:22 +010035 struct inode *, struct dentry *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080037const struct file_operations jffs2_dir_operations =
Linus Torvalds1da177e2005-04-16 15:20:36 -070038{
39 .read = generic_read_dir,
40 .readdir = jffs2_readdir,
Stoyan Gaydarov05334002008-07-07 07:45:59 -050041 .unlocked_ioctl=jffs2_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 .fsync = jffs2_fsync
43};
44
45
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -080046const struct inode_operations jffs2_dir_inode_operations =
Linus Torvalds1da177e2005-04-16 15:20:36 -070047{
David Woodhouse265489f2005-07-06 13:13:13 +010048 .create = jffs2_create,
49 .lookup = jffs2_lookup,
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 .link = jffs2_link,
51 .unlink = jffs2_unlink,
52 .symlink = jffs2_symlink,
53 .mkdir = jffs2_mkdir,
54 .rmdir = jffs2_rmdir,
55 .mknod = jffs2_mknod,
56 .rename = jffs2_rename,
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +090057 .permission = jffs2_permission,
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 .setattr = jffs2_setattr,
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +090059 .setxattr = jffs2_setxattr,
60 .getxattr = jffs2_getxattr,
61 .listxattr = jffs2_listxattr,
62 .removexattr = jffs2_removexattr
Linus Torvalds1da177e2005-04-16 15:20:36 -070063};
64
65/***********************************************************************/
66
67
68/* We keep the dirent list sorted in increasing order of name hash,
Thomas Gleixner182ec4e2005-11-07 11:16:07 +000069 and we use the same hash function as the dentries. Makes this
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 nice and simple
71*/
72static struct dentry *jffs2_lookup(struct inode *dir_i, struct dentry *target,
73 struct nameidata *nd)
74{
75 struct jffs2_inode_info *dir_f;
76 struct jffs2_sb_info *c;
77 struct jffs2_full_dirent *fd = NULL, *fd_list;
78 uint32_t ino = 0;
79 struct inode *inode = NULL;
80
81 D1(printk(KERN_DEBUG "jffs2_lookup()\n"));
82
Richard Purdie373d5e72006-04-18 02:05:46 +010083 if (target->d_name.len > JFFS2_MAX_NAME_LEN)
84 return ERR_PTR(-ENAMETOOLONG);
85
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 dir_f = JFFS2_INODE_INFO(dir_i);
87 c = JFFS2_SB_INFO(dir_i->i_sb);
88
David Woodhouseced22072008-04-22 15:13:40 +010089 mutex_lock(&dir_f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91 /* NB: The 2.2 backport will need to explicitly check for '.' and '..' here */
92 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 +000093 if (fd_list->nhash == target->d_name.hash &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 (!fd || fd_list->version > fd->version) &&
95 strlen(fd_list->name) == target->d_name.len &&
96 !strncmp(fd_list->name, target->d_name.name, target->d_name.len)) {
97 fd = fd_list;
98 }
99 }
100 if (fd)
101 ino = fd->ino;
David Woodhouseced22072008-04-22 15:13:40 +0100102 mutex_unlock(&dir_f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 if (ino) {
David Howells5451f792008-02-07 00:15:42 -0800104 inode = jffs2_iget(dir_i->i_sb, ino);
105 if (IS_ERR(inode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 printk(KERN_WARNING "iget() failed for ino #%u\n", ino);
David Howells5451f792008-02-07 00:15:42 -0800107 return ERR_CAST(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 }
109 }
110
David Woodhouse8966c5e2008-08-18 15:36:47 +0100111 return d_splice_alias(inode, target);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112}
113
114/***********************************************************************/
115
116
117static int jffs2_readdir(struct file *filp, void *dirent, filldir_t filldir)
118{
119 struct jffs2_inode_info *f;
120 struct jffs2_sb_info *c;
Josef Sipekec2e2032006-12-08 02:37:16 -0800121 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 struct jffs2_full_dirent *fd;
123 unsigned long offset, curofs;
124
Josef Sipekec2e2032006-12-08 02:37:16 -0800125 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 -0700126
127 f = JFFS2_INODE_INFO(inode);
128 c = JFFS2_SB_INFO(inode->i_sb);
129
130 offset = filp->f_pos;
131
132 if (offset == 0) {
133 D1(printk(KERN_DEBUG "Dirent 0: \".\", ino #%lu\n", inode->i_ino));
134 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
135 goto out;
136 offset++;
137 }
138 if (offset == 1) {
Josef Sipekec2e2032006-12-08 02:37:16 -0800139 unsigned long pino = parent_ino(filp->f_path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 D1(printk(KERN_DEBUG "Dirent 1: \"..\", ino #%lu\n", pino));
141 if (filldir(dirent, "..", 2, 1, pino, DT_DIR) < 0)
142 goto out;
143 offset++;
144 }
145
146 curofs=1;
David Woodhouseced22072008-04-22 15:13:40 +0100147 mutex_lock(&f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 for (fd = f->dents; fd; fd = fd->next) {
149
150 curofs++;
151 /* First loop: curofs = 2; offset = 2 */
152 if (curofs < offset) {
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000153 D2(printk(KERN_DEBUG "Skipping dirent: \"%s\", ino #%u, type %d, because curofs %ld < offset %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 fd->name, fd->ino, fd->type, curofs, offset));
155 continue;
156 }
157 if (!fd->ino) {
158 D2(printk(KERN_DEBUG "Skipping deletion dirent \"%s\"\n", fd->name));
159 offset++;
160 continue;
161 }
162 D2(printk(KERN_DEBUG "Dirent %ld: \"%s\", ino #%u, type %d\n", offset, fd->name, fd->ino, fd->type));
163 if (filldir(dirent, fd->name, strlen(fd->name), offset, fd->ino, fd->type) < 0)
164 break;
165 offset++;
166 }
David Woodhouseced22072008-04-22 15:13:40 +0100167 mutex_unlock(&f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 out:
169 filp->f_pos = offset;
170 return 0;
171}
172
173/***********************************************************************/
174
175
176static int jffs2_create(struct inode *dir_i, struct dentry *dentry, int mode,
177 struct nameidata *nd)
178{
179 struct jffs2_raw_inode *ri;
180 struct jffs2_inode_info *f, *dir_f;
181 struct jffs2_sb_info *c;
182 struct inode *inode;
183 int ret;
184
185 ri = jffs2_alloc_raw_inode();
186 if (!ri)
187 return -ENOMEM;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 c = JFFS2_SB_INFO(dir_i->i_sb);
190
191 D1(printk(KERN_DEBUG "jffs2_create()\n"));
192
KaiGai Koheicfc8dc62007-09-14 15:16:35 +0900193 inode = jffs2_new_inode(dir_i, mode, ri);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
195 if (IS_ERR(inode)) {
196 D1(printk(KERN_DEBUG "jffs2_new_inode() failed\n"));
197 jffs2_free_raw_inode(ri);
198 return PTR_ERR(inode);
199 }
200
201 inode->i_op = &jffs2_file_inode_operations;
202 inode->i_fop = &jffs2_file_operations;
203 inode->i_mapping->a_ops = &jffs2_file_address_operations;
204 inode->i_mapping->nrpages = 0;
205
206 f = JFFS2_INODE_INFO(inode);
207 dir_f = JFFS2_INODE_INFO(dir_i);
208
David Woodhouse590fe342008-05-01 15:53:28 +0100209 /* jffs2_do_create() will want to lock it, _after_ reserving
210 space and taking c-alloc_sem. If we keep it locked here,
211 lockdep gets unhappy (although it's a false positive;
212 nothing else will be looking at this inode yet so there's
213 no chance of AB-BA deadlock involving its f->sem). */
214 mutex_unlock(&f->sem);
215
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000216 ret = jffs2_do_create(c, dir_f, f, ri,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 dentry->d_name.name, dentry->d_name.len);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900218 if (ret)
219 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221 dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(ri->ctime));
222
223 jffs2_free_raw_inode(ri);
224 d_instantiate(dentry, inode);
225
226 D1(printk(KERN_DEBUG "jffs2_create: Created ino #%lu with mode %o, nlink %d(%d). nrpages %ld\n",
David Woodhouse27c72b02008-05-01 18:47:17 +0100227 inode->i_ino, inode->i_mode, inode->i_nlink,
228 f->inocache->pino_nlink, inode->i_mapping->nrpages));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 return 0;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900230
231 fail:
232 make_bad_inode(inode);
233 iput(inode);
234 jffs2_free_raw_inode(ri);
235 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236}
237
238/***********************************************************************/
239
240
241static int jffs2_unlink(struct inode *dir_i, struct dentry *dentry)
242{
243 struct jffs2_sb_info *c = JFFS2_SB_INFO(dir_i->i_sb);
244 struct jffs2_inode_info *dir_f = JFFS2_INODE_INFO(dir_i);
245 struct jffs2_inode_info *dead_f = JFFS2_INODE_INFO(dentry->d_inode);
246 int ret;
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100247 uint32_t now = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000249 ret = jffs2_do_unlink(c, dir_f, dentry->d_name.name,
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100250 dentry->d_name.len, dead_f, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 if (dead_f->inocache)
David Woodhouse27c72b02008-05-01 18:47:17 +0100252 dentry->d_inode->i_nlink = dead_f->inocache->pino_nlink;
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100253 if (!ret)
254 dir_i->i_mtime = dir_i->i_ctime = ITIME(now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 return ret;
256}
257/***********************************************************************/
258
259
260static int jffs2_link (struct dentry *old_dentry, struct inode *dir_i, struct dentry *dentry)
261{
262 struct jffs2_sb_info *c = JFFS2_SB_INFO(old_dentry->d_inode->i_sb);
263 struct jffs2_inode_info *f = JFFS2_INODE_INFO(old_dentry->d_inode);
264 struct jffs2_inode_info *dir_f = JFFS2_INODE_INFO(dir_i);
265 int ret;
266 uint8_t type;
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100267 uint32_t now;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269 /* Don't let people make hard links to bad inodes. */
270 if (!f->inocache)
271 return -EIO;
272
273 if (S_ISDIR(old_dentry->d_inode->i_mode))
274 return -EPERM;
275
276 /* XXX: This is ugly */
277 type = (old_dentry->d_inode->i_mode & S_IFMT) >> 12;
278 if (!type) type = DT_REG;
279
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100280 now = get_seconds();
281 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 -0700282
283 if (!ret) {
David Woodhouseced22072008-04-22 15:13:40 +0100284 mutex_lock(&f->sem);
David Woodhouse27c72b02008-05-01 18:47:17 +0100285 old_dentry->d_inode->i_nlink = ++f->inocache->pino_nlink;
David Woodhouseced22072008-04-22 15:13:40 +0100286 mutex_unlock(&f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 d_instantiate(dentry, old_dentry->d_inode);
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100288 dir_i->i_mtime = dir_i->i_ctime = ITIME(now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 atomic_inc(&old_dentry->d_inode->i_count);
290 }
291 return ret;
292}
293
294/***********************************************************************/
295
296static int jffs2_symlink (struct inode *dir_i, struct dentry *dentry, const char *target)
297{
298 struct jffs2_inode_info *f, *dir_f;
299 struct jffs2_sb_info *c;
300 struct inode *inode;
301 struct jffs2_raw_inode *ri;
302 struct jffs2_raw_dirent *rd;
303 struct jffs2_full_dnode *fn;
304 struct jffs2_full_dirent *fd;
305 int namelen;
David Woodhouse9fe48542006-05-23 00:38:06 +0100306 uint32_t alloclen;
Artem B. Bityuckiy32f1a952005-03-01 10:50:52 +0000307 int ret, targetlen = strlen(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309 /* FIXME: If you care. We'd need to use frags for the target
310 if it grows much more than this */
Artem B. Bityuckiy32f1a952005-03-01 10:50:52 +0000311 if (targetlen > 254)
Adrian Hunterbde86fe2008-08-14 11:57:45 +0300312 return -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314 ri = jffs2_alloc_raw_inode();
315
316 if (!ri)
317 return -ENOMEM;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 c = JFFS2_SB_INFO(dir_i->i_sb);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000320
321 /* Try to reserve enough space for both node and dirent.
322 * Just the node will do for now, though
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 */
324 namelen = dentry->d_name.len;
David Woodhouse9fe48542006-05-23 00:38:06 +0100325 ret = jffs2_reserve_space(c, sizeof(*ri) + targetlen, &alloclen,
326 ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328 if (ret) {
329 jffs2_free_raw_inode(ri);
330 return ret;
331 }
332
KaiGai Koheicfc8dc62007-09-14 15:16:35 +0900333 inode = jffs2_new_inode(dir_i, S_IFLNK | S_IRWXUGO, ri);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
335 if (IS_ERR(inode)) {
336 jffs2_free_raw_inode(ri);
337 jffs2_complete_reservation(c);
338 return PTR_ERR(inode);
339 }
340
341 inode->i_op = &jffs2_symlink_inode_operations;
342
343 f = JFFS2_INODE_INFO(inode);
344
Artem B. Bityuckiy32f1a952005-03-01 10:50:52 +0000345 inode->i_size = targetlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 ri->isize = ri->dsize = ri->csize = cpu_to_je32(inode->i_size);
347 ri->totlen = cpu_to_je32(sizeof(*ri) + inode->i_size);
348 ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4));
349
350 ri->compr = JFFS2_COMPR_NONE;
Artem B. Bityuckiy32f1a952005-03-01 10:50:52 +0000351 ri->data_crc = cpu_to_je32(crc32(0, target, targetlen));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000353
David Woodhouse9fe48542006-05-23 00:38:06 +0100354 fn = jffs2_write_dnode(c, f, ri, target, targetlen, ALLOC_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
356 jffs2_free_raw_inode(ri);
357
358 if (IS_ERR(fn)) {
359 /* Eeek. Wave bye bye */
David Woodhouseced22072008-04-22 15:13:40 +0100360 mutex_unlock(&f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 jffs2_complete_reservation(c);
362 jffs2_clear_inode(inode);
363 return PTR_ERR(fn);
364 }
Artem B. Bityuckiy32f1a952005-03-01 10:50:52 +0000365
Artem B. Bityutskiy2b79adc2005-07-17 12:13:51 +0100366 /* We use f->target field to store the target path. */
367 f->target = kmalloc(targetlen + 1, GFP_KERNEL);
368 if (!f->target) {
Artem B. Bityuckiy32f1a952005-03-01 10:50:52 +0000369 printk(KERN_WARNING "Can't allocate %d bytes of memory\n", targetlen + 1);
David Woodhouseced22072008-04-22 15:13:40 +0100370 mutex_unlock(&f->sem);
Artem B. Bityuckiy32f1a952005-03-01 10:50:52 +0000371 jffs2_complete_reservation(c);
372 jffs2_clear_inode(inode);
373 return -ENOMEM;
374 }
375
Artem B. Bityutskiy2b79adc2005-07-17 12:13:51 +0100376 memcpy(f->target, target, targetlen + 1);
377 D1(printk(KERN_DEBUG "jffs2_symlink: symlink's target '%s' cached\n", (char *)f->target));
Artem B. Bityuckiy32f1a952005-03-01 10:50:52 +0000378
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000379 /* No data here. Only a metadata node, which will be
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 obsoleted by the first data write
381 */
382 f->metadata = fn;
David Woodhouseced22072008-04-22 15:13:40 +0100383 mutex_unlock(&f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
385 jffs2_complete_reservation(c);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900386
387 ret = jffs2_init_security(inode, dir_i);
388 if (ret) {
389 jffs2_clear_inode(inode);
390 return ret;
391 }
KaiGai Koheicfc8dc62007-09-14 15:16:35 +0900392 ret = jffs2_init_acl_post(inode);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900393 if (ret) {
394 jffs2_clear_inode(inode);
395 return ret;
396 }
397
David Woodhouse9fe48542006-05-23 00:38:06 +0100398 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
399 ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 if (ret) {
401 /* Eep. */
402 jffs2_clear_inode(inode);
403 return ret;
404 }
405
406 rd = jffs2_alloc_raw_dirent();
407 if (!rd) {
408 /* Argh. Now we treat it like a normal delete */
409 jffs2_complete_reservation(c);
410 jffs2_clear_inode(inode);
411 return -ENOMEM;
412 }
413
414 dir_f = JFFS2_INODE_INFO(dir_i);
David Woodhouseced22072008-04-22 15:13:40 +0100415 mutex_lock(&dir_f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
417 rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
418 rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
419 rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
420 rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
421
422 rd->pino = cpu_to_je32(dir_i->i_ino);
423 rd->version = cpu_to_je32(++dir_f->highest_version);
424 rd->ino = cpu_to_je32(inode->i_ino);
425 rd->mctime = cpu_to_je32(get_seconds());
426 rd->nsize = namelen;
427 rd->type = DT_LNK;
428 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
429 rd->name_crc = cpu_to_je32(crc32(0, dentry->d_name.name, namelen));
430
David Woodhouse9fe48542006-05-23 00:38:06 +0100431 fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, ALLOC_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433 if (IS_ERR(fd)) {
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000434 /* dirent failed to write. Delete the inode normally
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 as if it were the final unlink() */
436 jffs2_complete_reservation(c);
437 jffs2_free_raw_dirent(rd);
David Woodhouseced22072008-04-22 15:13:40 +0100438 mutex_unlock(&dir_f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 jffs2_clear_inode(inode);
440 return PTR_ERR(fd);
441 }
442
443 dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(rd->mctime));
444
445 jffs2_free_raw_dirent(rd);
446
447 /* Link the fd into the inode's list, obsoleting an old
448 one if necessary. */
449 jffs2_add_fd_to_list(c, fd, &dir_f->dents);
450
David Woodhouseced22072008-04-22 15:13:40 +0100451 mutex_unlock(&dir_f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 jffs2_complete_reservation(c);
453
454 d_instantiate(dentry, inode);
455 return 0;
456}
457
458
459static int jffs2_mkdir (struct inode *dir_i, struct dentry *dentry, int mode)
460{
461 struct jffs2_inode_info *f, *dir_f;
462 struct jffs2_sb_info *c;
463 struct inode *inode;
464 struct jffs2_raw_inode *ri;
465 struct jffs2_raw_dirent *rd;
466 struct jffs2_full_dnode *fn;
467 struct jffs2_full_dirent *fd;
468 int namelen;
David Woodhouse9fe48542006-05-23 00:38:06 +0100469 uint32_t alloclen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 int ret;
471
472 mode |= S_IFDIR;
473
474 ri = jffs2_alloc_raw_inode();
475 if (!ri)
476 return -ENOMEM;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 c = JFFS2_SB_INFO(dir_i->i_sb);
479
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000480 /* Try to reserve enough space for both node and dirent.
481 * Just the node will do for now, though
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 */
483 namelen = dentry->d_name.len;
David Woodhouse9fe48542006-05-23 00:38:06 +0100484 ret = jffs2_reserve_space(c, sizeof(*ri), &alloclen, ALLOC_NORMAL,
485 JFFS2_SUMMARY_INODE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
487 if (ret) {
488 jffs2_free_raw_inode(ri);
489 return ret;
490 }
491
KaiGai Koheicfc8dc62007-09-14 15:16:35 +0900492 inode = jffs2_new_inode(dir_i, mode, ri);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 if (IS_ERR(inode)) {
495 jffs2_free_raw_inode(ri);
496 jffs2_complete_reservation(c);
497 return PTR_ERR(inode);
498 }
499
500 inode->i_op = &jffs2_dir_inode_operations;
501 inode->i_fop = &jffs2_dir_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
503 f = JFFS2_INODE_INFO(inode);
504
David Woodhouse27c72b02008-05-01 18:47:17 +0100505 /* Directories get nlink 2 at start */
506 inode->i_nlink = 2;
507 /* but ic->pino_nlink is the parent ino# */
508 f->inocache->pino_nlink = dir_i->i_ino;
509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 ri->data_crc = cpu_to_je32(0);
511 ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000512
David Woodhouse9fe48542006-05-23 00:38:06 +0100513 fn = jffs2_write_dnode(c, f, ri, NULL, 0, ALLOC_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515 jffs2_free_raw_inode(ri);
516
517 if (IS_ERR(fn)) {
518 /* Eeek. Wave bye bye */
David Woodhouseced22072008-04-22 15:13:40 +0100519 mutex_unlock(&f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 jffs2_complete_reservation(c);
521 jffs2_clear_inode(inode);
522 return PTR_ERR(fn);
523 }
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000524 /* No data here. Only a metadata node, which will be
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 obsoleted by the first data write
526 */
527 f->metadata = fn;
David Woodhouseced22072008-04-22 15:13:40 +0100528 mutex_unlock(&f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
530 jffs2_complete_reservation(c);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900531
532 ret = jffs2_init_security(inode, dir_i);
533 if (ret) {
534 jffs2_clear_inode(inode);
535 return ret;
536 }
KaiGai Koheicfc8dc62007-09-14 15:16:35 +0900537 ret = jffs2_init_acl_post(inode);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900538 if (ret) {
539 jffs2_clear_inode(inode);
540 return ret;
541 }
542
David Woodhouse9fe48542006-05-23 00:38:06 +0100543 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
544 ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 if (ret) {
546 /* Eep. */
547 jffs2_clear_inode(inode);
548 return ret;
549 }
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 rd = jffs2_alloc_raw_dirent();
552 if (!rd) {
553 /* Argh. Now we treat it like a normal delete */
554 jffs2_complete_reservation(c);
555 jffs2_clear_inode(inode);
556 return -ENOMEM;
557 }
558
559 dir_f = JFFS2_INODE_INFO(dir_i);
David Woodhouseced22072008-04-22 15:13:40 +0100560 mutex_lock(&dir_f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
562 rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
563 rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
564 rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
565 rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
566
567 rd->pino = cpu_to_je32(dir_i->i_ino);
568 rd->version = cpu_to_je32(++dir_f->highest_version);
569 rd->ino = cpu_to_je32(inode->i_ino);
570 rd->mctime = cpu_to_je32(get_seconds());
571 rd->nsize = namelen;
572 rd->type = DT_DIR;
573 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
574 rd->name_crc = cpu_to_je32(crc32(0, dentry->d_name.name, namelen));
575
David Woodhouse9fe48542006-05-23 00:38:06 +0100576 fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, ALLOC_NORMAL);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 if (IS_ERR(fd)) {
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000579 /* dirent failed to write. Delete the inode normally
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 as if it were the final unlink() */
581 jffs2_complete_reservation(c);
582 jffs2_free_raw_dirent(rd);
David Woodhouseced22072008-04-22 15:13:40 +0100583 mutex_unlock(&dir_f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 jffs2_clear_inode(inode);
585 return PTR_ERR(fd);
586 }
587
588 dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(rd->mctime));
Dave Hansend8c76e62006-09-30 23:29:04 -0700589 inc_nlink(dir_i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
591 jffs2_free_raw_dirent(rd);
592
593 /* Link the fd into the inode's list, obsoleting an old
594 one if necessary. */
595 jffs2_add_fd_to_list(c, fd, &dir_f->dents);
596
David Woodhouseced22072008-04-22 15:13:40 +0100597 mutex_unlock(&dir_f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 jffs2_complete_reservation(c);
599
600 d_instantiate(dentry, inode);
601 return 0;
602}
603
604static int jffs2_rmdir (struct inode *dir_i, struct dentry *dentry)
605{
David Woodhouse27c72b02008-05-01 18:47:17 +0100606 struct jffs2_sb_info *c = JFFS2_SB_INFO(dir_i->i_sb);
607 struct jffs2_inode_info *dir_f = JFFS2_INODE_INFO(dir_i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 struct jffs2_inode_info *f = JFFS2_INODE_INFO(dentry->d_inode);
609 struct jffs2_full_dirent *fd;
610 int ret;
David Woodhouse27c72b02008-05-01 18:47:17 +0100611 uint32_t now = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
613 for (fd = f->dents ; fd; fd = fd->next) {
614 if (fd->ino)
615 return -ENOTEMPTY;
616 }
David Woodhouse27c72b02008-05-01 18:47:17 +0100617
618 ret = jffs2_do_unlink(c, dir_f, dentry->d_name.name,
619 dentry->d_name.len, f, now);
620 if (!ret) {
621 dir_i->i_mtime = dir_i->i_ctime = ITIME(now);
622 clear_nlink(dentry->d_inode);
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700623 drop_nlink(dir_i);
David Woodhouse27c72b02008-05-01 18:47:17 +0100624 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 return ret;
626}
627
David Woodhouse265489f2005-07-06 13:13:13 +0100628static int jffs2_mknod (struct inode *dir_i, struct dentry *dentry, int mode, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629{
630 struct jffs2_inode_info *f, *dir_f;
631 struct jffs2_sb_info *c;
632 struct inode *inode;
633 struct jffs2_raw_inode *ri;
634 struct jffs2_raw_dirent *rd;
635 struct jffs2_full_dnode *fn;
636 struct jffs2_full_dirent *fd;
637 int namelen;
David Woodhouseaef9ab42006-05-19 00:28:49 +0100638 union jffs2_device_node dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 int devlen = 0;
David Woodhouse9fe48542006-05-23 00:38:06 +0100640 uint32_t alloclen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 int ret;
642
David Woodhouseaef9ab42006-05-19 00:28:49 +0100643 if (!new_valid_dev(rdev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 return -EINVAL;
645
646 ri = jffs2_alloc_raw_inode();
647 if (!ri)
648 return -ENOMEM;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000649
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 c = JFFS2_SB_INFO(dir_i->i_sb);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000651
David Woodhouseaef9ab42006-05-19 00:28:49 +0100652 if (S_ISBLK(mode) || S_ISCHR(mode))
653 devlen = jffs2_encode_dev(&dev, rdev);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000654
655 /* Try to reserve enough space for both node and dirent.
656 * Just the node will do for now, though
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 */
658 namelen = dentry->d_name.len;
David Woodhouse9fe48542006-05-23 00:38:06 +0100659 ret = jffs2_reserve_space(c, sizeof(*ri) + devlen, &alloclen,
David Woodhouseaef9ab42006-05-19 00:28:49 +0100660 ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
662 if (ret) {
663 jffs2_free_raw_inode(ri);
664 return ret;
665 }
666
KaiGai Koheicfc8dc62007-09-14 15:16:35 +0900667 inode = jffs2_new_inode(dir_i, mode, ri);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
669 if (IS_ERR(inode)) {
670 jffs2_free_raw_inode(ri);
671 jffs2_complete_reservation(c);
672 return PTR_ERR(inode);
673 }
674 inode->i_op = &jffs2_file_inode_operations;
675 init_special_inode(inode, inode->i_mode, rdev);
676
677 f = JFFS2_INODE_INFO(inode);
678
679 ri->dsize = ri->csize = cpu_to_je32(devlen);
680 ri->totlen = cpu_to_je32(sizeof(*ri) + devlen);
681 ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4));
682
683 ri->compr = JFFS2_COMPR_NONE;
684 ri->data_crc = cpu_to_je32(crc32(0, &dev, devlen));
685 ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000686
David Woodhouse9fe48542006-05-23 00:38:06 +0100687 fn = jffs2_write_dnode(c, f, ri, (char *)&dev, devlen, ALLOC_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
689 jffs2_free_raw_inode(ri);
690
691 if (IS_ERR(fn)) {
692 /* Eeek. Wave bye bye */
David Woodhouseced22072008-04-22 15:13:40 +0100693 mutex_unlock(&f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 jffs2_complete_reservation(c);
695 jffs2_clear_inode(inode);
696 return PTR_ERR(fn);
697 }
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000698 /* No data here. Only a metadata node, which will be
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 obsoleted by the first data write
700 */
701 f->metadata = fn;
David Woodhouseced22072008-04-22 15:13:40 +0100702 mutex_unlock(&f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
704 jffs2_complete_reservation(c);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900705
706 ret = jffs2_init_security(inode, dir_i);
707 if (ret) {
708 jffs2_clear_inode(inode);
709 return ret;
710 }
KaiGai Koheicfc8dc62007-09-14 15:16:35 +0900711 ret = jffs2_init_acl_post(inode);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900712 if (ret) {
713 jffs2_clear_inode(inode);
714 return ret;
715 }
716
David Woodhouse9fe48542006-05-23 00:38:06 +0100717 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
718 ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 if (ret) {
720 /* Eep. */
721 jffs2_clear_inode(inode);
722 return ret;
723 }
724
725 rd = jffs2_alloc_raw_dirent();
726 if (!rd) {
727 /* Argh. Now we treat it like a normal delete */
728 jffs2_complete_reservation(c);
729 jffs2_clear_inode(inode);
730 return -ENOMEM;
731 }
732
733 dir_f = JFFS2_INODE_INFO(dir_i);
David Woodhouseced22072008-04-22 15:13:40 +0100734 mutex_lock(&dir_f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
736 rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
737 rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
738 rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
739 rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
740
741 rd->pino = cpu_to_je32(dir_i->i_ino);
742 rd->version = cpu_to_je32(++dir_f->highest_version);
743 rd->ino = cpu_to_je32(inode->i_ino);
744 rd->mctime = cpu_to_je32(get_seconds());
745 rd->nsize = namelen;
746
747 /* XXX: This is ugly. */
748 rd->type = (mode & S_IFMT) >> 12;
749
750 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
751 rd->name_crc = cpu_to_je32(crc32(0, dentry->d_name.name, namelen));
752
David Woodhouse9fe48542006-05-23 00:38:06 +0100753 fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, ALLOC_NORMAL);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000754
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 if (IS_ERR(fd)) {
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000756 /* dirent failed to write. Delete the inode normally
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 as if it were the final unlink() */
758 jffs2_complete_reservation(c);
759 jffs2_free_raw_dirent(rd);
David Woodhouseced22072008-04-22 15:13:40 +0100760 mutex_unlock(&dir_f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 jffs2_clear_inode(inode);
762 return PTR_ERR(fd);
763 }
764
765 dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(rd->mctime));
766
767 jffs2_free_raw_dirent(rd);
768
769 /* Link the fd into the inode's list, obsoleting an old
770 one if necessary. */
771 jffs2_add_fd_to_list(c, fd, &dir_f->dents);
772
David Woodhouseced22072008-04-22 15:13:40 +0100773 mutex_unlock(&dir_f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 jffs2_complete_reservation(c);
775
776 d_instantiate(dentry, inode);
777
778 return 0;
779}
780
781static int jffs2_rename (struct inode *old_dir_i, struct dentry *old_dentry,
David Woodhouseef53cb02007-07-10 10:01:22 +0100782 struct inode *new_dir_i, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783{
784 int ret;
785 struct jffs2_sb_info *c = JFFS2_SB_INFO(old_dir_i->i_sb);
786 struct jffs2_inode_info *victim_f = NULL;
787 uint8_t type;
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100788 uint32_t now;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000790 /* The VFS will check for us and prevent trying to rename a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 * file over a directory and vice versa, but if it's a directory,
792 * the VFS can't check whether the victim is empty. The filesystem
793 * needs to do that for itself.
794 */
795 if (new_dentry->d_inode) {
796 victim_f = JFFS2_INODE_INFO(new_dentry->d_inode);
797 if (S_ISDIR(new_dentry->d_inode->i_mode)) {
798 struct jffs2_full_dirent *fd;
799
David Woodhouseced22072008-04-22 15:13:40 +0100800 mutex_lock(&victim_f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 for (fd = victim_f->dents; fd; fd = fd->next) {
802 if (fd->ino) {
David Woodhouseced22072008-04-22 15:13:40 +0100803 mutex_unlock(&victim_f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 return -ENOTEMPTY;
805 }
806 }
David Woodhouseced22072008-04-22 15:13:40 +0100807 mutex_unlock(&victim_f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 }
809 }
810
811 /* XXX: We probably ought to alloc enough space for
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000812 both nodes at the same time. Writing the new link,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 then getting -ENOSPC, is quite bad :)
814 */
815
816 /* Make a hard link */
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000817
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 /* XXX: This is ugly */
819 type = (old_dentry->d_inode->i_mode & S_IFMT) >> 12;
820 if (!type) type = DT_REG;
821
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100822 now = get_seconds();
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000823 ret = jffs2_do_link(c, JFFS2_INODE_INFO(new_dir_i),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 old_dentry->d_inode->i_ino, type,
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100825 new_dentry->d_name.name, new_dentry->d_name.len, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
827 if (ret)
828 return ret;
829
830 if (victim_f) {
831 /* There was a victim. Kill it off nicely */
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700832 drop_nlink(new_dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 /* Don't oops if the victim was a dirent pointing to an
834 inode which didn't exist. */
835 if (victim_f->inocache) {
David Woodhouseced22072008-04-22 15:13:40 +0100836 mutex_lock(&victim_f->sem);
David Woodhouse27c72b02008-05-01 18:47:17 +0100837 if (S_ISDIR(new_dentry->d_inode->i_mode))
838 victim_f->inocache->pino_nlink = 0;
839 else
840 victim_f->inocache->pino_nlink--;
David Woodhouseced22072008-04-22 15:13:40 +0100841 mutex_unlock(&victim_f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 }
843 }
844
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000845 /* If it was a directory we moved, and there was no victim,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 increase i_nlink on its new parent */
847 if (S_ISDIR(old_dentry->d_inode->i_mode) && !victim_f)
Dave Hansend8c76e62006-09-30 23:29:04 -0700848 inc_nlink(new_dir_i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
850 /* Unlink the original */
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000851 ret = jffs2_do_unlink(c, JFFS2_INODE_INFO(old_dir_i),
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100852 old_dentry->d_name.name, old_dentry->d_name.len, NULL, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
854 /* We don't touch inode->i_nlink */
855
856 if (ret) {
857 /* Oh shit. We really ought to make a single node which can do both atomically */
858 struct jffs2_inode_info *f = JFFS2_INODE_INFO(old_dentry->d_inode);
David Woodhouseced22072008-04-22 15:13:40 +0100859 mutex_lock(&f->sem);
Dave Hansend8c76e62006-09-30 23:29:04 -0700860 inc_nlink(old_dentry->d_inode);
David Woodhouse27c72b02008-05-01 18:47:17 +0100861 if (f->inocache && !S_ISDIR(old_dentry->d_inode->i_mode))
862 f->inocache->pino_nlink++;
David Woodhouseced22072008-04-22 15:13:40 +0100863 mutex_unlock(&f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
865 printk(KERN_NOTICE "jffs2_rename(): Link succeeded, unlink failed (err %d). You now have a hard link\n", ret);
866 /* Might as well let the VFS know */
867 d_instantiate(new_dentry, old_dentry->d_inode);
868 atomic_inc(&old_dentry->d_inode->i_count);
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100869 new_dir_i->i_mtime = new_dir_i->i_ctime = ITIME(now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 return ret;
871 }
872
873 if (S_ISDIR(old_dentry->d_inode->i_mode))
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700874 drop_nlink(old_dir_i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100876 new_dir_i->i_mtime = new_dir_i->i_ctime = old_dir_i->i_mtime = old_dir_i->i_ctime = ITIME(now);
877
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 return 0;
879}
880