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