blob: a7a3c1ac91216b49fe7b2383fff1b74c9d6fd22d [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.
David Woodhouse6088c052010-08-08 14:15:22 +01005 * Copyright © 2004-2010 David Woodhouse <dwmw2@infradead.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * Created by David Woodhouse <dwmw2@infradead.org>
8 *
9 * For licensing information, see the file 'LICENCE' in this directory.
10 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
12
13#include <linux/kernel.h>
14#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/fs.h>
16#include <linux/crc32.h>
17#include <linux/jffs2.h>
David Woodhousecbb9a562006-05-03 13:07:27 +010018#include "jffs2_fs_i.h"
19#include "jffs2_fs_sb.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/time.h>
21#include "nodelist.h"
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023static int jffs2_readdir (struct file *, void *, filldir_t);
24
Al Viro4acdaf22011-07-26 01:42:34 -040025static int jffs2_create (struct inode *,struct dentry *,umode_t,
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 struct nameidata *);
27static struct dentry *jffs2_lookup (struct inode *,struct dentry *,
28 struct nameidata *);
29static int jffs2_link (struct dentry *,struct inode *,struct dentry *);
30static int jffs2_unlink (struct inode *,struct dentry *);
31static int jffs2_symlink (struct inode *,struct dentry *,const char *);
Al Viro18bb1db2011-07-26 01:41:39 -040032static int jffs2_mkdir (struct inode *,struct dentry *,umode_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -070033static int jffs2_rmdir (struct inode *,struct dentry *);
Al Viro1a67aaf2011-07-26 01:52:52 -040034static int jffs2_mknod (struct inode *,struct dentry *,umode_t,dev_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -070035static int jffs2_rename (struct inode *, struct dentry *,
David Woodhouseef53cb02007-07-10 10:01:22 +010036 struct inode *, struct dentry *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080038const struct file_operations jffs2_dir_operations =
Linus Torvalds1da177e2005-04-16 15:20:36 -070039{
40 .read = generic_read_dir,
41 .readdir = jffs2_readdir,
Stoyan Gaydarov05334002008-07-07 07:45:59 -050042 .unlocked_ioctl=jffs2_ioctl,
Christoph Hellwig3222a3e2008-09-03 21:53:01 +020043 .fsync = jffs2_fsync,
44 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -070045};
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,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020059 .get_acl = jffs2_get_acl,
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 struct jffs2_full_dirent *fd = NULL, *fd_list;
79 uint32_t ino = 0;
80 struct inode *inode = NULL;
81
Joe Perches9c261b32012-02-15 15:56:43 -080082 jffs2_dbg(1, "jffs2_lookup()\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Richard Purdie373d5e72006-04-18 02:05:46 +010084 if (target->d_name.len > JFFS2_MAX_NAME_LEN)
85 return ERR_PTR(-ENAMETOOLONG);
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 dir_f = JFFS2_INODE_INFO(dir_i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
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);
Al Viroa9049372011-07-08 21:20:11 -0400105 if (IS_ERR(inode))
Joe Perchesda320f02012-02-15 15:56:44 -0800106 pr_warn("iget() failed for ino #%u\n", ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 }
108
David Woodhouse8966c5e2008-08-18 15:36:47 +0100109 return d_splice_alias(inode, target);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110}
111
112/***********************************************************************/
113
114
115static int jffs2_readdir(struct file *filp, void *dirent, filldir_t filldir)
116{
117 struct jffs2_inode_info *f;
Josef Sipekec2e2032006-12-08 02:37:16 -0800118 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 struct jffs2_full_dirent *fd;
120 unsigned long offset, curofs;
121
Joe Perches9c261b32012-02-15 15:56:43 -0800122 jffs2_dbg(1, "jffs2_readdir() for dir_i #%lu\n",
123 filp->f_path.dentry->d_inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
125 f = JFFS2_INODE_INFO(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127 offset = filp->f_pos;
128
129 if (offset == 0) {
Joe Perches9c261b32012-02-15 15:56:43 -0800130 jffs2_dbg(1, "Dirent 0: \".\", ino #%lu\n", inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
132 goto out;
133 offset++;
134 }
135 if (offset == 1) {
Josef Sipekec2e2032006-12-08 02:37:16 -0800136 unsigned long pino = parent_ino(filp->f_path.dentry);
Joe Perches9c261b32012-02-15 15:56:43 -0800137 jffs2_dbg(1, "Dirent 1: \"..\", ino #%lu\n", pino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 if (filldir(dirent, "..", 2, 1, pino, DT_DIR) < 0)
139 goto out;
140 offset++;
141 }
142
143 curofs=1;
David Woodhouseced22072008-04-22 15:13:40 +0100144 mutex_lock(&f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 for (fd = f->dents; fd; fd = fd->next) {
146
147 curofs++;
148 /* First loop: curofs = 2; offset = 2 */
149 if (curofs < offset) {
Joe Perches9c261b32012-02-15 15:56:43 -0800150 jffs2_dbg(2, "Skipping dirent: \"%s\", ino #%u, type %d, because curofs %ld < offset %ld\n",
151 fd->name, fd->ino, fd->type, curofs, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 continue;
153 }
154 if (!fd->ino) {
Joe Perches9c261b32012-02-15 15:56:43 -0800155 jffs2_dbg(2, "Skipping deletion dirent \"%s\"\n",
156 fd->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 offset++;
158 continue;
159 }
Joe Perches9c261b32012-02-15 15:56:43 -0800160 jffs2_dbg(2, "Dirent %ld: \"%s\", ino #%u, type %d\n",
161 offset, fd->name, fd->ino, fd->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 if (filldir(dirent, fd->name, strlen(fd->name), offset, fd->ino, fd->type) < 0)
163 break;
164 offset++;
165 }
David Woodhouseced22072008-04-22 15:13:40 +0100166 mutex_unlock(&f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 out:
168 filp->f_pos = offset;
169 return 0;
170}
171
172/***********************************************************************/
173
174
Al Viro4acdaf22011-07-26 01:42:34 -0400175static int jffs2_create(struct inode *dir_i, struct dentry *dentry,
176 umode_t mode, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
178 struct jffs2_raw_inode *ri;
179 struct jffs2_inode_info *f, *dir_f;
180 struct jffs2_sb_info *c;
181 struct inode *inode;
182 int ret;
183
184 ri = jffs2_alloc_raw_inode();
185 if (!ri)
186 return -ENOMEM;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 c = JFFS2_SB_INFO(dir_i->i_sb);
189
Joe Perches9c261b32012-02-15 15:56:43 -0800190 jffs2_dbg(1, "%s()\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
KaiGai Koheicfc8dc62007-09-14 15:16:35 +0900192 inode = jffs2_new_inode(dir_i, mode, ri);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194 if (IS_ERR(inode)) {
Joe Perches9c261b32012-02-15 15:56:43 -0800195 jffs2_dbg(1, "jffs2_new_inode() failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 jffs2_free_raw_inode(ri);
197 return PTR_ERR(inode);
198 }
199
200 inode->i_op = &jffs2_file_inode_operations;
201 inode->i_fop = &jffs2_file_operations;
202 inode->i_mapping->a_ops = &jffs2_file_address_operations;
203 inode->i_mapping->nrpages = 0;
204
205 f = JFFS2_INODE_INFO(inode);
206 dir_f = JFFS2_INODE_INFO(dir_i);
207
David Woodhouse590fe342008-05-01 15:53:28 +0100208 /* jffs2_do_create() will want to lock it, _after_ reserving
209 space and taking c-alloc_sem. If we keep it locked here,
210 lockdep gets unhappy (although it's a false positive;
211 nothing else will be looking at this inode yet so there's
212 no chance of AB-BA deadlock involving its f->sem). */
213 mutex_unlock(&f->sem);
214
Eric Paris2a7dba32011-02-01 11:05:39 -0500215 ret = jffs2_do_create(c, dir_f, f, ri, &dentry->d_name);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900216 if (ret)
217 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
219 dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(ri->ctime));
220
221 jffs2_free_raw_inode(ri);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Joe Perches9c261b32012-02-15 15:56:43 -0800223 jffs2_dbg(1, "%s(): Created ino #%lu with mode %o, nlink %d(%d). nrpages %ld\n",
224 __func__, inode->i_ino, inode->i_mode, inode->i_nlink,
225 f->inocache->pino_nlink, inode->i_mapping->nrpages);
David Woodhousee72e6492010-06-03 08:09:12 +0100226
227 d_instantiate(dentry, inode);
228 unlock_new_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 return 0;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900230
231 fail:
Al Viro41cce642010-06-08 13:24:56 -0400232 iget_failed(inode);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900233 jffs2_free_raw_inode(ri);
234 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235}
236
237/***********************************************************************/
238
239
240static int jffs2_unlink(struct inode *dir_i, struct dentry *dentry)
241{
242 struct jffs2_sb_info *c = JFFS2_SB_INFO(dir_i->i_sb);
243 struct jffs2_inode_info *dir_f = JFFS2_INODE_INFO(dir_i);
244 struct jffs2_inode_info *dead_f = JFFS2_INODE_INFO(dentry->d_inode);
245 int ret;
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100246 uint32_t now = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000248 ret = jffs2_do_unlink(c, dir_f, dentry->d_name.name,
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100249 dentry->d_name.len, dead_f, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 if (dead_f->inocache)
Miklos Szeredibfe86842011-10-28 14:13:29 +0200251 set_nlink(dentry->d_inode, dead_f->inocache->pino_nlink);
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100252 if (!ret)
253 dir_i->i_mtime = dir_i->i_ctime = ITIME(now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 return ret;
255}
256/***********************************************************************/
257
258
259static int jffs2_link (struct dentry *old_dentry, struct inode *dir_i, struct dentry *dentry)
260{
261 struct jffs2_sb_info *c = JFFS2_SB_INFO(old_dentry->d_inode->i_sb);
262 struct jffs2_inode_info *f = JFFS2_INODE_INFO(old_dentry->d_inode);
263 struct jffs2_inode_info *dir_f = JFFS2_INODE_INFO(dir_i);
264 int ret;
265 uint8_t type;
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100266 uint32_t now;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268 /* Don't let people make hard links to bad inodes. */
269 if (!f->inocache)
270 return -EIO;
271
272 if (S_ISDIR(old_dentry->d_inode->i_mode))
273 return -EPERM;
274
275 /* XXX: This is ugly */
276 type = (old_dentry->d_inode->i_mode & S_IFMT) >> 12;
277 if (!type) type = DT_REG;
278
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100279 now = get_seconds();
280 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 -0700281
282 if (!ret) {
David Woodhouseced22072008-04-22 15:13:40 +0100283 mutex_lock(&f->sem);
Miklos Szeredibfe86842011-10-28 14:13:29 +0200284 set_nlink(old_dentry->d_inode, ++f->inocache->pino_nlink);
David Woodhouseced22072008-04-22 15:13:40 +0100285 mutex_unlock(&f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 d_instantiate(dentry, old_dentry->d_inode);
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100287 dir_i->i_mtime = dir_i->i_ctime = ITIME(now);
Al Viro7de9c6ee2010-10-23 11:11:40 -0400288 ihold(old_dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 }
290 return ret;
291}
292
293/***********************************************************************/
294
295static int jffs2_symlink (struct inode *dir_i, struct dentry *dentry, const char *target)
296{
297 struct jffs2_inode_info *f, *dir_f;
298 struct jffs2_sb_info *c;
299 struct inode *inode;
300 struct jffs2_raw_inode *ri;
301 struct jffs2_raw_dirent *rd;
302 struct jffs2_full_dnode *fn;
303 struct jffs2_full_dirent *fd;
304 int namelen;
David Woodhouse9fe48542006-05-23 00:38:06 +0100305 uint32_t alloclen;
Artem B. Bityuckiy32f1a952005-03-01 10:50:52 +0000306 int ret, targetlen = strlen(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
308 /* FIXME: If you care. We'd need to use frags for the target
309 if it grows much more than this */
Artem B. Bityuckiy32f1a952005-03-01 10:50:52 +0000310 if (targetlen > 254)
Adrian Hunterbde86fe2008-08-14 11:57:45 +0300311 return -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
313 ri = jffs2_alloc_raw_inode();
314
315 if (!ri)
316 return -ENOMEM;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 c = JFFS2_SB_INFO(dir_i->i_sb);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000319
320 /* Try to reserve enough space for both node and dirent.
321 * Just the node will do for now, though
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 */
323 namelen = dentry->d_name.len;
David Woodhouse9fe48542006-05-23 00:38:06 +0100324 ret = jffs2_reserve_space(c, sizeof(*ri) + targetlen, &alloclen,
325 ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
327 if (ret) {
328 jffs2_free_raw_inode(ri);
329 return ret;
330 }
331
KaiGai Koheicfc8dc62007-09-14 15:16:35 +0900332 inode = jffs2_new_inode(dir_i, S_IFLNK | S_IRWXUGO, ri);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334 if (IS_ERR(inode)) {
335 jffs2_free_raw_inode(ri);
336 jffs2_complete_reservation(c);
337 return PTR_ERR(inode);
338 }
339
340 inode->i_op = &jffs2_symlink_inode_operations;
341
342 f = JFFS2_INODE_INFO(inode);
343
Artem B. Bityuckiy32f1a952005-03-01 10:50:52 +0000344 inode->i_size = targetlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 ri->isize = ri->dsize = ri->csize = cpu_to_je32(inode->i_size);
346 ri->totlen = cpu_to_je32(sizeof(*ri) + inode->i_size);
347 ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4));
348
349 ri->compr = JFFS2_COMPR_NONE;
Artem B. Bityuckiy32f1a952005-03-01 10:50:52 +0000350 ri->data_crc = cpu_to_je32(crc32(0, target, targetlen));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000352
David Woodhouse9fe48542006-05-23 00:38:06 +0100353 fn = jffs2_write_dnode(c, f, ri, target, targetlen, ALLOC_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
355 jffs2_free_raw_inode(ri);
356
357 if (IS_ERR(fn)) {
358 /* Eeek. Wave bye bye */
David Woodhouseced22072008-04-22 15:13:40 +0100359 mutex_unlock(&f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 jffs2_complete_reservation(c);
David Woodhousef324e4c2010-06-03 08:03:39 +0100361 ret = PTR_ERR(fn);
362 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 }
Artem B. Bityuckiy32f1a952005-03-01 10:50:52 +0000364
Artem B. Bityutskiy2b79adc2005-07-17 12:13:51 +0100365 /* We use f->target field to store the target path. */
Julia Lawall04aadf32010-10-17 21:56:15 +0200366 f->target = kmemdup(target, targetlen + 1, GFP_KERNEL);
Artem B. Bityutskiy2b79adc2005-07-17 12:13:51 +0100367 if (!f->target) {
Joe Perchesda320f02012-02-15 15:56:44 -0800368 pr_warn("Can't allocate %d bytes of memory\n", targetlen + 1);
David Woodhouseced22072008-04-22 15:13:40 +0100369 mutex_unlock(&f->sem);
Artem B. Bityuckiy32f1a952005-03-01 10:50:52 +0000370 jffs2_complete_reservation(c);
David Woodhousef324e4c2010-06-03 08:03:39 +0100371 ret = -ENOMEM;
372 goto fail;
Artem B. Bityuckiy32f1a952005-03-01 10:50:52 +0000373 }
374
Joe Perches9c261b32012-02-15 15:56:43 -0800375 jffs2_dbg(1, "%s(): symlink's target '%s' cached\n",
376 __func__, (char *)f->target);
Artem B. Bityuckiy32f1a952005-03-01 10:50:52 +0000377
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000378 /* No data here. Only a metadata node, which will be
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 obsoleted by the first data write
380 */
381 f->metadata = fn;
David Woodhouseced22072008-04-22 15:13:40 +0100382 mutex_unlock(&f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
384 jffs2_complete_reservation(c);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900385
Eric Paris2a7dba32011-02-01 11:05:39 -0500386 ret = jffs2_init_security(inode, dir_i, &dentry->d_name);
David Woodhousef324e4c2010-06-03 08:03:39 +0100387 if (ret)
388 goto fail;
389
KaiGai Koheicfc8dc62007-09-14 15:16:35 +0900390 ret = jffs2_init_acl_post(inode);
David Woodhousef324e4c2010-06-03 08:03:39 +0100391 if (ret)
392 goto fail;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900393
David Woodhouse9fe48542006-05-23 00:38:06 +0100394 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
395 ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
David Woodhousef324e4c2010-06-03 08:03:39 +0100396 if (ret)
397 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399 rd = jffs2_alloc_raw_dirent();
400 if (!rd) {
401 /* Argh. Now we treat it like a normal delete */
402 jffs2_complete_reservation(c);
David Woodhousef324e4c2010-06-03 08:03:39 +0100403 ret = -ENOMEM;
404 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 }
406
407 dir_f = JFFS2_INODE_INFO(dir_i);
David Woodhouseced22072008-04-22 15:13:40 +0100408 mutex_lock(&dir_f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
410 rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
411 rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
412 rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
413 rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
414
415 rd->pino = cpu_to_je32(dir_i->i_ino);
416 rd->version = cpu_to_je32(++dir_f->highest_version);
417 rd->ino = cpu_to_je32(inode->i_ino);
418 rd->mctime = cpu_to_je32(get_seconds());
419 rd->nsize = namelen;
420 rd->type = DT_LNK;
421 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
422 rd->name_crc = cpu_to_je32(crc32(0, dentry->d_name.name, namelen));
423
David Woodhouse9fe48542006-05-23 00:38:06 +0100424 fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, ALLOC_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
426 if (IS_ERR(fd)) {
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000427 /* dirent failed to write. Delete the inode normally
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 as if it were the final unlink() */
429 jffs2_complete_reservation(c);
430 jffs2_free_raw_dirent(rd);
David Woodhouseced22072008-04-22 15:13:40 +0100431 mutex_unlock(&dir_f->sem);
David Woodhousef324e4c2010-06-03 08:03:39 +0100432 ret = PTR_ERR(fd);
433 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 }
435
436 dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(rd->mctime));
437
438 jffs2_free_raw_dirent(rd);
439
440 /* Link the fd into the inode's list, obsoleting an old
441 one if necessary. */
442 jffs2_add_fd_to_list(c, fd, &dir_f->dents);
443
David Woodhouseced22072008-04-22 15:13:40 +0100444 mutex_unlock(&dir_f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 jffs2_complete_reservation(c);
446
447 d_instantiate(dentry, inode);
David Woodhousee72e6492010-06-03 08:09:12 +0100448 unlock_new_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 return 0;
David Woodhousef324e4c2010-06-03 08:03:39 +0100450
451 fail:
Al Viro41cce642010-06-08 13:24:56 -0400452 iget_failed(inode);
David Woodhousef324e4c2010-06-03 08:03:39 +0100453 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454}
455
456
Al Viro18bb1db2011-07-26 01:41:39 -0400457static int jffs2_mkdir (struct inode *dir_i, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
459 struct jffs2_inode_info *f, *dir_f;
460 struct jffs2_sb_info *c;
461 struct inode *inode;
462 struct jffs2_raw_inode *ri;
463 struct jffs2_raw_dirent *rd;
464 struct jffs2_full_dnode *fn;
465 struct jffs2_full_dirent *fd;
466 int namelen;
David Woodhouse9fe48542006-05-23 00:38:06 +0100467 uint32_t alloclen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 int ret;
469
470 mode |= S_IFDIR;
471
472 ri = jffs2_alloc_raw_inode();
473 if (!ri)
474 return -ENOMEM;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 c = JFFS2_SB_INFO(dir_i->i_sb);
477
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000478 /* Try to reserve enough space for both node and dirent.
479 * Just the node will do for now, though
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 */
481 namelen = dentry->d_name.len;
David Woodhouse9fe48542006-05-23 00:38:06 +0100482 ret = jffs2_reserve_space(c, sizeof(*ri), &alloclen, ALLOC_NORMAL,
483 JFFS2_SUMMARY_INODE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485 if (ret) {
486 jffs2_free_raw_inode(ri);
487 return ret;
488 }
489
KaiGai Koheicfc8dc62007-09-14 15:16:35 +0900490 inode = jffs2_new_inode(dir_i, mode, ri);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
492 if (IS_ERR(inode)) {
493 jffs2_free_raw_inode(ri);
494 jffs2_complete_reservation(c);
495 return PTR_ERR(inode);
496 }
497
498 inode->i_op = &jffs2_dir_inode_operations;
499 inode->i_fop = &jffs2_dir_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
501 f = JFFS2_INODE_INFO(inode);
502
David Woodhouse27c72b02008-05-01 18:47:17 +0100503 /* Directories get nlink 2 at start */
Miklos Szeredibfe86842011-10-28 14:13:29 +0200504 set_nlink(inode, 2);
David Woodhouse27c72b02008-05-01 18:47:17 +0100505 /* but ic->pino_nlink is the parent ino# */
506 f->inocache->pino_nlink = dir_i->i_ino;
507
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 ri->data_crc = cpu_to_je32(0);
509 ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000510
David Woodhouse9fe48542006-05-23 00:38:06 +0100511 fn = jffs2_write_dnode(c, f, ri, NULL, 0, ALLOC_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
513 jffs2_free_raw_inode(ri);
514
515 if (IS_ERR(fn)) {
516 /* Eeek. Wave bye bye */
David Woodhouseced22072008-04-22 15:13:40 +0100517 mutex_unlock(&f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 jffs2_complete_reservation(c);
David Woodhousef324e4c2010-06-03 08:03:39 +0100519 ret = PTR_ERR(fn);
520 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 }
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000522 /* No data here. Only a metadata node, which will be
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 obsoleted by the first data write
524 */
525 f->metadata = fn;
David Woodhouseced22072008-04-22 15:13:40 +0100526 mutex_unlock(&f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
528 jffs2_complete_reservation(c);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900529
Eric Paris2a7dba32011-02-01 11:05:39 -0500530 ret = jffs2_init_security(inode, dir_i, &dentry->d_name);
David Woodhousef324e4c2010-06-03 08:03:39 +0100531 if (ret)
532 goto fail;
533
KaiGai Koheicfc8dc62007-09-14 15:16:35 +0900534 ret = jffs2_init_acl_post(inode);
David Woodhousef324e4c2010-06-03 08:03:39 +0100535 if (ret)
536 goto fail;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900537
David Woodhouse9fe48542006-05-23 00:38:06 +0100538 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
539 ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
David Woodhousef324e4c2010-06-03 08:03:39 +0100540 if (ret)
541 goto fail;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 rd = jffs2_alloc_raw_dirent();
544 if (!rd) {
545 /* Argh. Now we treat it like a normal delete */
546 jffs2_complete_reservation(c);
David Woodhousef324e4c2010-06-03 08:03:39 +0100547 ret = -ENOMEM;
548 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 }
550
551 dir_f = JFFS2_INODE_INFO(dir_i);
David Woodhouseced22072008-04-22 15:13:40 +0100552 mutex_lock(&dir_f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
554 rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
555 rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
556 rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
557 rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
558
559 rd->pino = cpu_to_je32(dir_i->i_ino);
560 rd->version = cpu_to_je32(++dir_f->highest_version);
561 rd->ino = cpu_to_je32(inode->i_ino);
562 rd->mctime = cpu_to_je32(get_seconds());
563 rd->nsize = namelen;
564 rd->type = DT_DIR;
565 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
566 rd->name_crc = cpu_to_je32(crc32(0, dentry->d_name.name, namelen));
567
David Woodhouse9fe48542006-05-23 00:38:06 +0100568 fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, ALLOC_NORMAL);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 if (IS_ERR(fd)) {
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000571 /* dirent failed to write. Delete the inode normally
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 as if it were the final unlink() */
573 jffs2_complete_reservation(c);
574 jffs2_free_raw_dirent(rd);
David Woodhouseced22072008-04-22 15:13:40 +0100575 mutex_unlock(&dir_f->sem);
David Woodhousef324e4c2010-06-03 08:03:39 +0100576 ret = PTR_ERR(fd);
577 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 }
579
580 dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(rd->mctime));
Dave Hansend8c76e62006-09-30 23:29:04 -0700581 inc_nlink(dir_i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
583 jffs2_free_raw_dirent(rd);
584
585 /* Link the fd into the inode's list, obsoleting an old
586 one if necessary. */
587 jffs2_add_fd_to_list(c, fd, &dir_f->dents);
588
David Woodhouseced22072008-04-22 15:13:40 +0100589 mutex_unlock(&dir_f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 jffs2_complete_reservation(c);
591
592 d_instantiate(dentry, inode);
David Woodhousee72e6492010-06-03 08:09:12 +0100593 unlock_new_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 return 0;
David Woodhousef324e4c2010-06-03 08:03:39 +0100595
596 fail:
Al Viro41cce642010-06-08 13:24:56 -0400597 iget_failed(inode);
David Woodhousef324e4c2010-06-03 08:03:39 +0100598 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599}
600
601static int jffs2_rmdir (struct inode *dir_i, struct dentry *dentry)
602{
David Woodhouse27c72b02008-05-01 18:47:17 +0100603 struct jffs2_sb_info *c = JFFS2_SB_INFO(dir_i->i_sb);
604 struct jffs2_inode_info *dir_f = JFFS2_INODE_INFO(dir_i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 struct jffs2_inode_info *f = JFFS2_INODE_INFO(dentry->d_inode);
606 struct jffs2_full_dirent *fd;
607 int ret;
David Woodhouse27c72b02008-05-01 18:47:17 +0100608 uint32_t now = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
610 for (fd = f->dents ; fd; fd = fd->next) {
611 if (fd->ino)
612 return -ENOTEMPTY;
613 }
David Woodhouse27c72b02008-05-01 18:47:17 +0100614
615 ret = jffs2_do_unlink(c, dir_f, dentry->d_name.name,
616 dentry->d_name.len, f, now);
617 if (!ret) {
618 dir_i->i_mtime = dir_i->i_ctime = ITIME(now);
619 clear_nlink(dentry->d_inode);
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700620 drop_nlink(dir_i);
David Woodhouse27c72b02008-05-01 18:47:17 +0100621 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 return ret;
623}
624
Al Viro1a67aaf2011-07-26 01:52:52 -0400625static int jffs2_mknod (struct inode *dir_i, struct dentry *dentry, umode_t mode, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626{
627 struct jffs2_inode_info *f, *dir_f;
628 struct jffs2_sb_info *c;
629 struct inode *inode;
630 struct jffs2_raw_inode *ri;
631 struct jffs2_raw_dirent *rd;
632 struct jffs2_full_dnode *fn;
633 struct jffs2_full_dirent *fd;
634 int namelen;
David Woodhouseaef9ab42006-05-19 00:28:49 +0100635 union jffs2_device_node dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 int devlen = 0;
David Woodhouse9fe48542006-05-23 00:38:06 +0100637 uint32_t alloclen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 int ret;
639
David Woodhouseaef9ab42006-05-19 00:28:49 +0100640 if (!new_valid_dev(rdev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 return -EINVAL;
642
643 ri = jffs2_alloc_raw_inode();
644 if (!ri)
645 return -ENOMEM;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000646
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 c = JFFS2_SB_INFO(dir_i->i_sb);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000648
David Woodhouseaef9ab42006-05-19 00:28:49 +0100649 if (S_ISBLK(mode) || S_ISCHR(mode))
650 devlen = jffs2_encode_dev(&dev, rdev);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000651
652 /* Try to reserve enough space for both node and dirent.
653 * Just the node will do for now, though
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 */
655 namelen = dentry->d_name.len;
David Woodhouse9fe48542006-05-23 00:38:06 +0100656 ret = jffs2_reserve_space(c, sizeof(*ri) + devlen, &alloclen,
David Woodhouseaef9ab42006-05-19 00:28:49 +0100657 ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
659 if (ret) {
660 jffs2_free_raw_inode(ri);
661 return ret;
662 }
663
KaiGai Koheicfc8dc62007-09-14 15:16:35 +0900664 inode = jffs2_new_inode(dir_i, mode, ri);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
666 if (IS_ERR(inode)) {
667 jffs2_free_raw_inode(ri);
668 jffs2_complete_reservation(c);
669 return PTR_ERR(inode);
670 }
671 inode->i_op = &jffs2_file_inode_operations;
672 init_special_inode(inode, inode->i_mode, rdev);
673
674 f = JFFS2_INODE_INFO(inode);
675
676 ri->dsize = ri->csize = cpu_to_je32(devlen);
677 ri->totlen = cpu_to_je32(sizeof(*ri) + devlen);
678 ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4));
679
680 ri->compr = JFFS2_COMPR_NONE;
681 ri->data_crc = cpu_to_je32(crc32(0, &dev, devlen));
682 ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000683
David Woodhouse9fe48542006-05-23 00:38:06 +0100684 fn = jffs2_write_dnode(c, f, ri, (char *)&dev, devlen, ALLOC_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
686 jffs2_free_raw_inode(ri);
687
688 if (IS_ERR(fn)) {
689 /* Eeek. Wave bye bye */
David Woodhouseced22072008-04-22 15:13:40 +0100690 mutex_unlock(&f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 jffs2_complete_reservation(c);
David Woodhousef324e4c2010-06-03 08:03:39 +0100692 ret = PTR_ERR(fn);
693 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 }
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000695 /* No data here. Only a metadata node, which will be
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 obsoleted by the first data write
697 */
698 f->metadata = fn;
David Woodhouseced22072008-04-22 15:13:40 +0100699 mutex_unlock(&f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
701 jffs2_complete_reservation(c);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900702
Eric Paris2a7dba32011-02-01 11:05:39 -0500703 ret = jffs2_init_security(inode, dir_i, &dentry->d_name);
David Woodhousef324e4c2010-06-03 08:03:39 +0100704 if (ret)
705 goto fail;
706
KaiGai Koheicfc8dc62007-09-14 15:16:35 +0900707 ret = jffs2_init_acl_post(inode);
David Woodhousef324e4c2010-06-03 08:03:39 +0100708 if (ret)
709 goto fail;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900710
David Woodhouse9fe48542006-05-23 00:38:06 +0100711 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
712 ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
David Woodhousef324e4c2010-06-03 08:03:39 +0100713 if (ret)
714 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
716 rd = jffs2_alloc_raw_dirent();
717 if (!rd) {
718 /* Argh. Now we treat it like a normal delete */
719 jffs2_complete_reservation(c);
David Woodhousef324e4c2010-06-03 08:03:39 +0100720 ret = -ENOMEM;
721 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 }
723
724 dir_f = JFFS2_INODE_INFO(dir_i);
David Woodhouseced22072008-04-22 15:13:40 +0100725 mutex_lock(&dir_f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
727 rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
728 rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
729 rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
730 rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
731
732 rd->pino = cpu_to_je32(dir_i->i_ino);
733 rd->version = cpu_to_je32(++dir_f->highest_version);
734 rd->ino = cpu_to_je32(inode->i_ino);
735 rd->mctime = cpu_to_je32(get_seconds());
736 rd->nsize = namelen;
737
738 /* XXX: This is ugly. */
739 rd->type = (mode & S_IFMT) >> 12;
740
741 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
742 rd->name_crc = cpu_to_je32(crc32(0, dentry->d_name.name, namelen));
743
David Woodhouse9fe48542006-05-23 00:38:06 +0100744 fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, ALLOC_NORMAL);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000745
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 if (IS_ERR(fd)) {
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000747 /* dirent failed to write. Delete the inode normally
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 as if it were the final unlink() */
749 jffs2_complete_reservation(c);
750 jffs2_free_raw_dirent(rd);
David Woodhouseced22072008-04-22 15:13:40 +0100751 mutex_unlock(&dir_f->sem);
David Woodhousef324e4c2010-06-03 08:03:39 +0100752 ret = PTR_ERR(fd);
753 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 }
755
756 dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(rd->mctime));
757
758 jffs2_free_raw_dirent(rd);
759
760 /* Link the fd into the inode's list, obsoleting an old
761 one if necessary. */
762 jffs2_add_fd_to_list(c, fd, &dir_f->dents);
763
David Woodhouseced22072008-04-22 15:13:40 +0100764 mutex_unlock(&dir_f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 jffs2_complete_reservation(c);
766
767 d_instantiate(dentry, inode);
David Woodhousee72e6492010-06-03 08:09:12 +0100768 unlock_new_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 return 0;
David Woodhousef324e4c2010-06-03 08:03:39 +0100770
771 fail:
Al Viro41cce642010-06-08 13:24:56 -0400772 iget_failed(inode);
David Woodhousef324e4c2010-06-03 08:03:39 +0100773 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774}
775
776static int jffs2_rename (struct inode *old_dir_i, struct dentry *old_dentry,
David Woodhouseef53cb02007-07-10 10:01:22 +0100777 struct inode *new_dir_i, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778{
779 int ret;
780 struct jffs2_sb_info *c = JFFS2_SB_INFO(old_dir_i->i_sb);
781 struct jffs2_inode_info *victim_f = NULL;
782 uint8_t type;
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100783 uint32_t now;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000785 /* The VFS will check for us and prevent trying to rename a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 * file over a directory and vice versa, but if it's a directory,
787 * the VFS can't check whether the victim is empty. The filesystem
788 * needs to do that for itself.
789 */
790 if (new_dentry->d_inode) {
791 victim_f = JFFS2_INODE_INFO(new_dentry->d_inode);
792 if (S_ISDIR(new_dentry->d_inode->i_mode)) {
793 struct jffs2_full_dirent *fd;
794
David Woodhouseced22072008-04-22 15:13:40 +0100795 mutex_lock(&victim_f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 for (fd = victim_f->dents; fd; fd = fd->next) {
797 if (fd->ino) {
David Woodhouseced22072008-04-22 15:13:40 +0100798 mutex_unlock(&victim_f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 return -ENOTEMPTY;
800 }
801 }
David Woodhouseced22072008-04-22 15:13:40 +0100802 mutex_unlock(&victim_f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 }
804 }
805
806 /* XXX: We probably ought to alloc enough space for
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000807 both nodes at the same time. Writing the new link,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 then getting -ENOSPC, is quite bad :)
809 */
810
811 /* Make a hard link */
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 /* XXX: This is ugly */
814 type = (old_dentry->d_inode->i_mode & S_IFMT) >> 12;
815 if (!type) type = DT_REG;
816
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100817 now = get_seconds();
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000818 ret = jffs2_do_link(c, JFFS2_INODE_INFO(new_dir_i),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 old_dentry->d_inode->i_ino, type,
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100820 new_dentry->d_name.name, new_dentry->d_name.len, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
822 if (ret)
823 return ret;
824
825 if (victim_f) {
826 /* There was a victim. Kill it off nicely */
Al Viro22ba7472011-07-21 15:57:47 -0400827 if (S_ISDIR(new_dentry->d_inode->i_mode))
828 clear_nlink(new_dentry->d_inode);
829 else
830 drop_nlink(new_dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 /* Don't oops if the victim was a dirent pointing to an
832 inode which didn't exist. */
833 if (victim_f->inocache) {
David Woodhouseced22072008-04-22 15:13:40 +0100834 mutex_lock(&victim_f->sem);
David Woodhouse27c72b02008-05-01 18:47:17 +0100835 if (S_ISDIR(new_dentry->d_inode->i_mode))
836 victim_f->inocache->pino_nlink = 0;
837 else
838 victim_f->inocache->pino_nlink--;
David Woodhouseced22072008-04-22 15:13:40 +0100839 mutex_unlock(&victim_f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 }
841 }
842
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000843 /* If it was a directory we moved, and there was no victim,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 increase i_nlink on its new parent */
845 if (S_ISDIR(old_dentry->d_inode->i_mode) && !victim_f)
Dave Hansend8c76e62006-09-30 23:29:04 -0700846 inc_nlink(new_dir_i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
848 /* Unlink the original */
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000849 ret = jffs2_do_unlink(c, JFFS2_INODE_INFO(old_dir_i),
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100850 old_dentry->d_name.name, old_dentry->d_name.len, NULL, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
852 /* We don't touch inode->i_nlink */
853
854 if (ret) {
855 /* Oh shit. We really ought to make a single node which can do both atomically */
856 struct jffs2_inode_info *f = JFFS2_INODE_INFO(old_dentry->d_inode);
David Woodhouseced22072008-04-22 15:13:40 +0100857 mutex_lock(&f->sem);
Dave Hansend8c76e62006-09-30 23:29:04 -0700858 inc_nlink(old_dentry->d_inode);
David Woodhouse27c72b02008-05-01 18:47:17 +0100859 if (f->inocache && !S_ISDIR(old_dentry->d_inode->i_mode))
860 f->inocache->pino_nlink++;
David Woodhouseced22072008-04-22 15:13:40 +0100861 mutex_unlock(&f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
Joe Perchesda320f02012-02-15 15:56:44 -0800863 pr_notice("%s(): Link succeeded, unlink failed (err %d). You now have a hard link\n",
864 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 /* Might as well let the VFS know */
866 d_instantiate(new_dentry, old_dentry->d_inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -0400867 ihold(old_dentry->d_inode);
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100868 new_dir_i->i_mtime = new_dir_i->i_ctime = ITIME(now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 return ret;
870 }
871
872 if (S_ISDIR(old_dentry->d_inode->i_mode))
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700873 drop_nlink(old_dir_i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100875 new_dir_i->i_mtime = new_dir_i->i_ctime = old_dir_i->i_mtime = old_dir_i->i_ctime = ITIME(now);
876
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 return 0;
878}
879