blob: 09e5d10b88401ca736bbd70d099d847fd9698bed [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * JFFS2 -- Journalling Flash File System, Version 2.
3 *
4 * Copyright (C) 2001-2003 Red Hat, Inc.
5 *
6 * Created by David Woodhouse <dwmw2@infradead.org>
7 *
8 * For licensing information, see the file 'LICENCE' in this directory.
9 *
Artem B. Bityutskiy2f0077e2005-09-27 14:17:32 +010010 * $Id: fs.c,v 1.66 2005/09/27 13:17:29 dedekind Exp $
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
12 */
13
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080014#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/config.h>
16#include <linux/kernel.h>
17#include <linux/sched.h>
18#include <linux/fs.h>
19#include <linux/list.h>
20#include <linux/mtd/mtd.h>
21#include <linux/pagemap.h>
22#include <linux/slab.h>
23#include <linux/vmalloc.h>
24#include <linux/vfs.h>
25#include <linux/crc32.h>
26#include "nodelist.h"
27
28static int jffs2_flash_setup(struct jffs2_sb_info *c);
29
30static int jffs2_do_setattr (struct inode *inode, struct iattr *iattr)
31{
32 struct jffs2_full_dnode *old_metadata, *new_metadata;
33 struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
34 struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
35 struct jffs2_raw_inode *ri;
36 unsigned short dev;
37 unsigned char *mdata = NULL;
38 int mdatalen = 0;
39 unsigned int ivalid;
40 uint32_t phys_ofs, alloclen;
41 int ret;
42 D1(printk(KERN_DEBUG "jffs2_setattr(): ino #%lu\n", inode->i_ino));
43 ret = inode_change_ok(inode, iattr);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +000044 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 return ret;
46
47 /* Special cases - we don't want more than one data node
48 for these types on the medium at any time. So setattr
49 must read the original data associated with the node
50 (i.e. the device numbers or the target name) and write
51 it out again with the appropriate data attached */
52 if (S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode)) {
53 /* For these, we don't actually need to read the old node */
54 dev = old_encode_dev(inode->i_rdev);
55 mdata = (char *)&dev;
56 mdatalen = sizeof(dev);
57 D1(printk(KERN_DEBUG "jffs2_setattr(): Writing %d bytes of kdev_t\n", mdatalen));
58 } else if (S_ISLNK(inode->i_mode)) {
59 mdatalen = f->metadata->size;
60 mdata = kmalloc(f->metadata->size, GFP_USER);
61 if (!mdata)
62 return -ENOMEM;
63 ret = jffs2_read_dnode(c, f, f->metadata, mdata, 0, mdatalen);
64 if (ret) {
65 kfree(mdata);
66 return ret;
67 }
68 D1(printk(KERN_DEBUG "jffs2_setattr(): Writing %d bytes of symlink target\n", mdatalen));
69 }
70
71 ri = jffs2_alloc_raw_inode();
72 if (!ri) {
73 if (S_ISLNK(inode->i_mode))
74 kfree(mdata);
75 return -ENOMEM;
76 }
Thomas Gleixner182ec4e2005-11-07 11:16:07 +000077
Ferenc Havasie631ddb2005-09-07 09:35:26 +010078 ret = jffs2_reserve_space(c, sizeof(*ri) + mdatalen, &phys_ofs, &alloclen,
79 ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 if (ret) {
81 jffs2_free_raw_inode(ri);
82 if (S_ISLNK(inode->i_mode & S_IFMT))
83 kfree(mdata);
84 return ret;
85 }
86 down(&f->sem);
87 ivalid = iattr->ia_valid;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +000088
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 ri->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
90 ri->nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE);
91 ri->totlen = cpu_to_je32(sizeof(*ri) + mdatalen);
92 ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4));
93
94 ri->ino = cpu_to_je32(inode->i_ino);
95 ri->version = cpu_to_je32(++f->highest_version);
96
97 ri->uid = cpu_to_je16((ivalid & ATTR_UID)?iattr->ia_uid:inode->i_uid);
98 ri->gid = cpu_to_je16((ivalid & ATTR_GID)?iattr->ia_gid:inode->i_gid);
99
100 if (ivalid & ATTR_MODE)
101 if (iattr->ia_mode & S_ISGID &&
102 !in_group_p(je16_to_cpu(ri->gid)) && !capable(CAP_FSETID))
103 ri->mode = cpu_to_jemode(iattr->ia_mode & ~S_ISGID);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000104 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 ri->mode = cpu_to_jemode(iattr->ia_mode);
106 else
107 ri->mode = cpu_to_jemode(inode->i_mode);
108
109
110 ri->isize = cpu_to_je32((ivalid & ATTR_SIZE)?iattr->ia_size:inode->i_size);
111 ri->atime = cpu_to_je32(I_SEC((ivalid & ATTR_ATIME)?iattr->ia_atime:inode->i_atime));
112 ri->mtime = cpu_to_je32(I_SEC((ivalid & ATTR_MTIME)?iattr->ia_mtime:inode->i_mtime));
113 ri->ctime = cpu_to_je32(I_SEC((ivalid & ATTR_CTIME)?iattr->ia_ctime:inode->i_ctime));
114
115 ri->offset = cpu_to_je32(0);
116 ri->csize = ri->dsize = cpu_to_je32(mdatalen);
117 ri->compr = JFFS2_COMPR_NONE;
118 if (ivalid & ATTR_SIZE && inode->i_size < iattr->ia_size) {
119 /* It's an extension. Make it a hole node */
120 ri->compr = JFFS2_COMPR_ZERO;
121 ri->dsize = cpu_to_je32(iattr->ia_size - inode->i_size);
122 ri->offset = cpu_to_je32(inode->i_size);
123 }
124 ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
125 if (mdatalen)
126 ri->data_crc = cpu_to_je32(crc32(0, mdata, mdatalen));
127 else
128 ri->data_crc = cpu_to_je32(0);
129
130 new_metadata = jffs2_write_dnode(c, f, ri, mdata, mdatalen, phys_ofs, ALLOC_NORMAL);
131 if (S_ISLNK(inode->i_mode))
132 kfree(mdata);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 if (IS_ERR(new_metadata)) {
135 jffs2_complete_reservation(c);
136 jffs2_free_raw_inode(ri);
137 up(&f->sem);
138 return PTR_ERR(new_metadata);
139 }
140 /* It worked. Update the inode */
141 inode->i_atime = ITIME(je32_to_cpu(ri->atime));
142 inode->i_ctime = ITIME(je32_to_cpu(ri->ctime));
143 inode->i_mtime = ITIME(je32_to_cpu(ri->mtime));
144 inode->i_mode = jemode_to_cpu(ri->mode);
145 inode->i_uid = je16_to_cpu(ri->uid);
146 inode->i_gid = je16_to_cpu(ri->gid);
147
148
149 old_metadata = f->metadata;
150
151 if (ivalid & ATTR_SIZE && inode->i_size > iattr->ia_size)
Artem B. Bityutskiyf302cd02005-07-24 16:29:59 +0100152 jffs2_truncate_fragtree (c, &f->fragtree, iattr->ia_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154 if (ivalid & ATTR_SIZE && inode->i_size < iattr->ia_size) {
155 jffs2_add_full_dnode_to_inode(c, f, new_metadata);
156 inode->i_size = iattr->ia_size;
157 f->metadata = NULL;
158 } else {
159 f->metadata = new_metadata;
160 }
161 if (old_metadata) {
162 jffs2_mark_node_obsolete(c, old_metadata->raw);
163 jffs2_free_full_dnode(old_metadata);
164 }
165 jffs2_free_raw_inode(ri);
166
167 up(&f->sem);
168 jffs2_complete_reservation(c);
169
170 /* We have to do the vmtruncate() without f->sem held, since
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000171 some pages may be locked and waiting for it in readpage().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 We are protected from a simultaneous write() extending i_size
173 back past iattr->ia_size, because do_truncate() holds the
174 generic inode semaphore. */
175 if (ivalid & ATTR_SIZE && inode->i_size > iattr->ia_size)
176 vmtruncate(inode, iattr->ia_size);
177
178 return 0;
179}
180
181int jffs2_setattr(struct dentry *dentry, struct iattr *iattr)
182{
183 return jffs2_do_setattr(dentry->d_inode, iattr);
184}
185
186int jffs2_statfs(struct super_block *sb, struct kstatfs *buf)
187{
188 struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
189 unsigned long avail;
190
191 buf->f_type = JFFS2_SUPER_MAGIC;
192 buf->f_bsize = 1 << PAGE_SHIFT;
193 buf->f_blocks = c->flash_size >> PAGE_SHIFT;
194 buf->f_files = 0;
195 buf->f_ffree = 0;
196 buf->f_namelen = JFFS2_MAX_NAME_LEN;
197
198 spin_lock(&c->erase_completion_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 avail = c->dirty_size + c->free_size;
200 if (avail > c->sector_size * c->resv_blocks_write)
201 avail -= c->sector_size * c->resv_blocks_write;
202 else
203 avail = 0;
Artem B. Bityutskiye0c8e422005-07-24 16:14:17 +0100204 spin_unlock(&c->erase_completion_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206 buf->f_bavail = buf->f_bfree = avail >> PAGE_SHIFT;
207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 return 0;
209}
210
211
212void jffs2_clear_inode (struct inode *inode)
213{
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000214 /* We can forget about this inode for now - drop all
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 * the nodelists associated with it, etc.
216 */
217 struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
218 struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 D1(printk(KERN_DEBUG "jffs2_clear_inode(): ino #%lu mode %o\n", inode->i_ino, inode->i_mode));
221
222 jffs2_do_clear_inode(c, f);
223}
224
225void jffs2_read_inode (struct inode *inode)
226{
227 struct jffs2_inode_info *f;
228 struct jffs2_sb_info *c;
229 struct jffs2_raw_inode latest_node;
230 int ret;
231
232 D1(printk(KERN_DEBUG "jffs2_read_inode(): inode->i_ino == %lu\n", inode->i_ino));
233
234 f = JFFS2_INODE_INFO(inode);
235 c = JFFS2_SB_INFO(inode->i_sb);
236
237 jffs2_init_inode_info(f);
Thomas Gleixner21eeb7a2005-11-29 16:57:17 +0100238 down(&f->sem);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 ret = jffs2_do_read_inode(c, f, inode->i_ino, &latest_node);
241
242 if (ret) {
243 make_bad_inode(inode);
244 up(&f->sem);
245 return;
246 }
247 inode->i_mode = jemode_to_cpu(latest_node.mode);
248 inode->i_uid = je16_to_cpu(latest_node.uid);
249 inode->i_gid = je16_to_cpu(latest_node.gid);
250 inode->i_size = je32_to_cpu(latest_node.isize);
251 inode->i_atime = ITIME(je32_to_cpu(latest_node.atime));
252 inode->i_mtime = ITIME(je32_to_cpu(latest_node.mtime));
253 inode->i_ctime = ITIME(je32_to_cpu(latest_node.ctime));
254
255 inode->i_nlink = f->inocache->nlink;
256
257 inode->i_blksize = PAGE_SIZE;
258 inode->i_blocks = (inode->i_size + 511) >> 9;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 switch (inode->i_mode & S_IFMT) {
261 jint16_t rdev;
262
263 case S_IFLNK:
264 inode->i_op = &jffs2_symlink_inode_operations;
265 break;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 case S_IFDIR:
268 {
269 struct jffs2_full_dirent *fd;
270
271 for (fd=f->dents; fd; fd = fd->next) {
272 if (fd->type == DT_DIR && fd->ino)
273 inode->i_nlink++;
274 }
275 /* and '..' */
276 inode->i_nlink++;
277 /* Root dir gets i_nlink 3 for some reason */
278 if (inode->i_ino == 1)
279 inode->i_nlink++;
280
281 inode->i_op = &jffs2_dir_inode_operations;
282 inode->i_fop = &jffs2_dir_operations;
283 break;
284 }
285 case S_IFREG:
286 inode->i_op = &jffs2_file_inode_operations;
287 inode->i_fop = &jffs2_file_operations;
288 inode->i_mapping->a_ops = &jffs2_file_address_operations;
289 inode->i_mapping->nrpages = 0;
290 break;
291
292 case S_IFBLK:
293 case S_IFCHR:
294 /* Read the device numbers from the media */
295 D1(printk(KERN_DEBUG "Reading device numbers from flash\n"));
296 if (jffs2_read_dnode(c, f, f->metadata, (char *)&rdev, 0, sizeof(rdev)) < 0) {
297 /* Eep */
298 printk(KERN_NOTICE "Read device numbers for inode %lu failed\n", (unsigned long)inode->i_ino);
299 up(&f->sem);
300 jffs2_do_clear_inode(c, f);
301 make_bad_inode(inode);
302 return;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000303 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305 case S_IFSOCK:
306 case S_IFIFO:
307 inode->i_op = &jffs2_file_inode_operations;
308 init_special_inode(inode, inode->i_mode,
309 old_decode_dev((je16_to_cpu(rdev))));
310 break;
311
312 default:
313 printk(KERN_WARNING "jffs2_read_inode(): Bogus imode %o for ino %lu\n", inode->i_mode, (unsigned long)inode->i_ino);
314 }
315
316 up(&f->sem);
317
318 D1(printk(KERN_DEBUG "jffs2_read_inode() returning\n"));
319}
320
321void jffs2_dirty_inode(struct inode *inode)
322{
323 struct iattr iattr;
324
325 if (!(inode->i_state & I_DIRTY_DATASYNC)) {
326 D2(printk(KERN_DEBUG "jffs2_dirty_inode() not calling setattr() for ino #%lu\n", inode->i_ino));
327 return;
328 }
329
330 D1(printk(KERN_DEBUG "jffs2_dirty_inode() calling setattr() for ino #%lu\n", inode->i_ino));
331
332 iattr.ia_valid = ATTR_MODE|ATTR_UID|ATTR_GID|ATTR_ATIME|ATTR_MTIME|ATTR_CTIME;
333 iattr.ia_mode = inode->i_mode;
334 iattr.ia_uid = inode->i_uid;
335 iattr.ia_gid = inode->i_gid;
336 iattr.ia_atime = inode->i_atime;
337 iattr.ia_mtime = inode->i_mtime;
338 iattr.ia_ctime = inode->i_ctime;
339
340 jffs2_do_setattr(inode, &iattr);
341}
342
343int jffs2_remount_fs (struct super_block *sb, int *flags, char *data)
344{
345 struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
346
347 if (c->flags & JFFS2_SB_FLAG_RO && !(sb->s_flags & MS_RDONLY))
348 return -EROFS;
349
350 /* We stop if it was running, then restart if it needs to.
351 This also catches the case where it was stopped and this
352 is just a remount to restart it.
353 Flush the writebuffer, if neccecary, else we loose it */
354 if (!(sb->s_flags & MS_RDONLY)) {
355 jffs2_stop_garbage_collect_thread(c);
356 down(&c->alloc_sem);
357 jffs2_flush_wbuf_pad(c);
358 up(&c->alloc_sem);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
361 if (!(*flags & MS_RDONLY))
362 jffs2_start_garbage_collect_thread(c);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 *flags |= MS_NOATIME;
365
366 return 0;
367}
368
369void jffs2_write_super (struct super_block *sb)
370{
371 struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
372 sb->s_dirt = 0;
373
374 if (sb->s_flags & MS_RDONLY)
375 return;
376
377 D1(printk(KERN_DEBUG "jffs2_write_super()\n"));
378 jffs2_garbage_collect_trigger(c);
379 jffs2_erase_pending_blocks(c, 0);
380 jffs2_flush_wbuf_gc(c, 0);
381}
382
383
384/* jffs2_new_inode: allocate a new inode and inocache, add it to the hash,
385 fill in the raw_inode while you're at it. */
386struct inode *jffs2_new_inode (struct inode *dir_i, int mode, struct jffs2_raw_inode *ri)
387{
388 struct inode *inode;
389 struct super_block *sb = dir_i->i_sb;
390 struct jffs2_sb_info *c;
391 struct jffs2_inode_info *f;
392 int ret;
393
394 D1(printk(KERN_DEBUG "jffs2_new_inode(): dir_i %ld, mode 0x%x\n", dir_i->i_ino, mode));
395
396 c = JFFS2_SB_INFO(sb);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 inode = new_inode(sb);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000399
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 if (!inode)
401 return ERR_PTR(-ENOMEM);
402
403 f = JFFS2_INODE_INFO(inode);
404 jffs2_init_inode_info(f);
Thomas Gleixner21eeb7a2005-11-29 16:57:17 +0100405 down(&f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
407 memset(ri, 0, sizeof(*ri));
408 /* Set OS-specific defaults for new inodes */
409 ri->uid = cpu_to_je16(current->fsuid);
410
411 if (dir_i->i_mode & S_ISGID) {
412 ri->gid = cpu_to_je16(dir_i->i_gid);
413 if (S_ISDIR(mode))
414 mode |= S_ISGID;
415 } else {
416 ri->gid = cpu_to_je16(current->fsgid);
417 }
418 ri->mode = cpu_to_jemode(mode);
419 ret = jffs2_do_new_inode (c, f, mode, ri);
420 if (ret) {
421 make_bad_inode(inode);
422 iput(inode);
423 return ERR_PTR(ret);
424 }
425 inode->i_nlink = 1;
426 inode->i_ino = je32_to_cpu(ri->ino);
427 inode->i_mode = jemode_to_cpu(ri->mode);
428 inode->i_gid = je16_to_cpu(ri->gid);
429 inode->i_uid = je16_to_cpu(ri->uid);
430 inode->i_atime = inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
431 ri->atime = ri->mtime = ri->ctime = cpu_to_je32(I_SEC(inode->i_mtime));
432
433 inode->i_blksize = PAGE_SIZE;
434 inode->i_blocks = 0;
435 inode->i_size = 0;
436
437 insert_inode_hash(inode);
438
439 return inode;
440}
441
442
443int jffs2_do_fill_super(struct super_block *sb, void *data, int silent)
444{
445 struct jffs2_sb_info *c;
446 struct inode *root_i;
447 int ret;
448 size_t blocks;
449
450 c = JFFS2_SB_INFO(sb);
451
Andrew Victor2f82ce12005-02-09 09:24:26 +0000452#ifndef CONFIG_JFFS2_FS_WRITEBUFFER
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 if (c->mtd->type == MTD_NANDFLASH) {
454 printk(KERN_ERR "jffs2: Cannot operate on NAND flash unless jffs2 NAND support is compiled in.\n");
455 return -EINVAL;
456 }
Andrew Victor8f15fd52005-02-09 09:17:45 +0000457 if (c->mtd->type == MTD_DATAFLASH) {
458 printk(KERN_ERR "jffs2: Cannot operate on DataFlash unless jffs2 DataFlash support is compiled in.\n");
459 return -EINVAL;
460 }
461#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
463 c->flash_size = c->mtd->size;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000464 c->sector_size = c->mtd->erasesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 blocks = c->flash_size / c->sector_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
467 /*
468 * Size alignment check
469 */
470 if ((c->sector_size * blocks) != c->flash_size) {
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000471 c->flash_size = c->sector_size * blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 printk(KERN_INFO "jffs2: Flash size not aligned to erasesize, reducing to %dKiB\n",
473 c->flash_size / 1024);
474 }
475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 if (c->flash_size < 5*c->sector_size) {
477 printk(KERN_ERR "jffs2: Too few erase blocks (%d)\n", c->flash_size / c->sector_size);
478 return -EINVAL;
479 }
480
481 c->cleanmarker_size = sizeof(struct jffs2_unknown_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
483 /* NAND (or other bizarre) flash... do setup accordingly */
484 ret = jffs2_flash_setup(c);
485 if (ret)
486 return ret;
487
488 c->inocache_list = kmalloc(INOCACHE_HASHSIZE * sizeof(struct jffs2_inode_cache *), GFP_KERNEL);
489 if (!c->inocache_list) {
490 ret = -ENOMEM;
491 goto out_wbuf;
492 }
493 memset(c->inocache_list, 0, INOCACHE_HASHSIZE * sizeof(struct jffs2_inode_cache *));
494
495 if ((ret = jffs2_do_mount_fs(c)))
496 goto out_inohash;
497
498 ret = -EINVAL;
499
500 D1(printk(KERN_DEBUG "jffs2_do_fill_super(): Getting root inode\n"));
501 root_i = iget(sb, 1);
502 if (is_bad_inode(root_i)) {
503 D1(printk(KERN_WARNING "get root inode failed\n"));
Artem B. Bityutskiy6dac02a2005-07-18 12:21:23 +0100504 goto out_root_i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 }
506
507 D1(printk(KERN_DEBUG "jffs2_do_fill_super(): d_alloc_root()\n"));
508 sb->s_root = d_alloc_root(root_i);
509 if (!sb->s_root)
510 goto out_root_i;
511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 sb->s_maxbytes = 0xFFFFFFFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 sb->s_blocksize = PAGE_CACHE_SIZE;
514 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
515 sb->s_magic = JFFS2_SUPER_MAGIC;
516 if (!(sb->s_flags & MS_RDONLY))
517 jffs2_start_garbage_collect_thread(c);
518 return 0;
519
520 out_root_i:
521 iput(root_i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 jffs2_free_ino_caches(c);
523 jffs2_free_raw_node_refs(c);
Ferenc Havasi4ce1f562005-08-31 14:51:04 +0100524 if (jffs2_blocks_use_vmalloc(c))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 vfree(c->blocks);
526 else
527 kfree(c->blocks);
528 out_inohash:
529 kfree(c->inocache_list);
530 out_wbuf:
531 jffs2_flash_cleanup(c);
532
533 return ret;
534}
535
536void jffs2_gc_release_inode(struct jffs2_sb_info *c,
537 struct jffs2_inode_info *f)
538{
539 iput(OFNI_EDONI_2SFFJ(f));
540}
541
542struct jffs2_inode_info *jffs2_gc_fetch_inode(struct jffs2_sb_info *c,
543 int inum, int nlink)
544{
545 struct inode *inode;
546 struct jffs2_inode_cache *ic;
547 if (!nlink) {
548 /* The inode has zero nlink but its nodes weren't yet marked
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000549 obsolete. This has to be because we're still waiting for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 the final (close() and) iput() to happen.
551
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000552 There's a possibility that the final iput() could have
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 happened while we were contemplating. In order to ensure
554 that we don't cause a new read_inode() (which would fail)
555 for the inode in question, we use ilookup() in this case
556 instead of iget().
557
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000558 The nlink can't _become_ zero at this point because we're
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 holding the alloc_sem, and jffs2_do_unlink() would also
560 need that while decrementing nlink on any inode.
561 */
562 inode = ilookup(OFNI_BS_2SFFJ(c), inum);
563 if (!inode) {
564 D1(printk(KERN_DEBUG "ilookup() failed for ino #%u; inode is probably deleted.\n",
565 inum));
566
567 spin_lock(&c->inocache_lock);
568 ic = jffs2_get_ino_cache(c, inum);
569 if (!ic) {
570 D1(printk(KERN_DEBUG "Inode cache for ino #%u is gone.\n", inum));
571 spin_unlock(&c->inocache_lock);
572 return NULL;
573 }
574 if (ic->state != INO_STATE_CHECKEDABSENT) {
575 /* Wait for progress. Don't just loop */
576 D1(printk(KERN_DEBUG "Waiting for ino #%u in state %d\n",
577 ic->ino, ic->state));
578 sleep_on_spinunlock(&c->inocache_wq, &c->inocache_lock);
579 } else {
580 spin_unlock(&c->inocache_lock);
581 }
582
583 return NULL;
584 }
585 } else {
586 /* Inode has links to it still; they're not going away because
587 jffs2_do_unlink() would need the alloc_sem and we have it.
588 Just iget() it, and if read_inode() is necessary that's OK.
589 */
590 inode = iget(OFNI_BS_2SFFJ(c), inum);
591 if (!inode)
592 return ERR_PTR(-ENOMEM);
593 }
594 if (is_bad_inode(inode)) {
595 printk(KERN_NOTICE "Eep. read_inode() failed for ino #%u. nlink %d\n",
596 inum, nlink);
597 /* NB. This will happen again. We need to do something appropriate here. */
598 iput(inode);
599 return ERR_PTR(-EIO);
600 }
601
602 return JFFS2_INODE_INFO(inode);
603}
604
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000605unsigned char *jffs2_gc_fetch_page(struct jffs2_sb_info *c,
606 struct jffs2_inode_info *f,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 unsigned long offset,
608 unsigned long *priv)
609{
610 struct inode *inode = OFNI_EDONI_2SFFJ(f);
611 struct page *pg;
612
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000613 pg = read_cache_page(inode->i_mapping, offset >> PAGE_CACHE_SHIFT,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 (void *)jffs2_do_readpage_unlock, inode);
615 if (IS_ERR(pg))
616 return (void *)pg;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 *priv = (unsigned long)pg;
619 return kmap(pg);
620}
621
622void jffs2_gc_release_page(struct jffs2_sb_info *c,
623 unsigned char *ptr,
624 unsigned long *priv)
625{
626 struct page *pg = (void *)*priv;
627
628 kunmap(pg);
629 page_cache_release(pg);
630}
631
632static int jffs2_flash_setup(struct jffs2_sb_info *c) {
633 int ret = 0;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 if (jffs2_cleanmarker_oob(c)) {
636 /* NAND flash... do setup accordingly */
637 ret = jffs2_nand_flash_setup(c);
638 if (ret)
639 return ret;
640 }
641
642 /* add setups for other bizarre flashes here... */
643 if (jffs2_nor_ecc(c)) {
644 ret = jffs2_nor_ecc_flash_setup(c);
645 if (ret)
646 return ret;
647 }
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000648
Andrew Victor8f15fd52005-02-09 09:17:45 +0000649 /* and Dataflash */
650 if (jffs2_dataflash(c)) {
651 ret = jffs2_dataflash_setup(c);
652 if (ret)
653 return ret;
654 }
Nicolas Pitre59da7212005-08-06 05:51:33 +0100655
656 /* and Intel "Sibley" flash */
657 if (jffs2_nor_wbuf_flash(c)) {
658 ret = jffs2_nor_wbuf_flash_setup(c);
659 if (ret)
660 return ret;
661 }
662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 return ret;
664}
665
666void jffs2_flash_cleanup(struct jffs2_sb_info *c) {
667
668 if (jffs2_cleanmarker_oob(c)) {
669 jffs2_nand_flash_cleanup(c);
670 }
671
672 /* add cleanups for other bizarre flashes here... */
673 if (jffs2_nor_ecc(c)) {
674 jffs2_nor_ecc_flash_cleanup(c);
675 }
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000676
Andrew Victor8f15fd52005-02-09 09:17:45 +0000677 /* and DataFlash */
678 if (jffs2_dataflash(c)) {
679 jffs2_dataflash_cleanup(c);
680 }
Nicolas Pitre59da7212005-08-06 05:51:33 +0100681
682 /* and Intel "Sibley" flash */
683 if (jffs2_nor_wbuf_flash(c)) {
684 jffs2_nor_wbuf_flash_cleanup(c);
685 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686}