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 | * | |
2f0077e0 | 10 | * $Id: fs.c,v 1.66 2005/09/27 13:17:29 dedekind Exp $ |
1da177e4 LT |
11 | * |
12 | */ | |
13 | ||
16f7e0fe | 14 | #include <linux/capability.h> |
1da177e4 LT |
15 | #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 | ||
28 | static int jffs2_flash_setup(struct jffs2_sb_info *c); | |
29 | ||
30 | static 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); | |
182ec4ee | 44 | if (ret) |
1da177e4 LT |
45 | 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 | } | |
182ec4ee | 77 | |
e631ddba FH |
78 | ret = jffs2_reserve_space(c, sizeof(*ri) + mdatalen, &phys_ofs, &alloclen, |
79 | ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE); | |
1da177e4 LT |
80 | 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; | |
182ec4ee | 88 | |
1da177e4 LT |
89 | 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); | |
182ec4ee | 104 | else |
1da177e4 LT |
105 | 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); | |
182ec4ee | 133 | |
1da177e4 LT |
134 | 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) | |
f302cd02 | 152 | jffs2_truncate_fragtree (c, &f->fragtree, iattr->ia_size); |
1da177e4 LT |
153 | |
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 | |
182ec4ee | 171 | some pages may be locked and waiting for it in readpage(). |
1da177e4 LT |
172 | 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 | ||
181 | int jffs2_setattr(struct dentry *dentry, struct iattr *iattr) | |
182 | { | |
183 | return jffs2_do_setattr(dentry->d_inode, iattr); | |
184 | } | |
185 | ||
186 | int 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); | |
1da177e4 LT |
199 | 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; | |
e0c8e42f | 204 | spin_unlock(&c->erase_completion_lock); |
1da177e4 LT |
205 | |
206 | buf->f_bavail = buf->f_bfree = avail >> PAGE_SHIFT; | |
207 | ||
1da177e4 LT |
208 | return 0; |
209 | } | |
210 | ||
211 | ||
212 | void jffs2_clear_inode (struct inode *inode) | |
213 | { | |
182ec4ee | 214 | /* We can forget about this inode for now - drop all |
1da177e4 LT |
215 | * 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); | |
182ec4ee | 219 | |
1da177e4 LT |
220 | 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 | ||
225 | void 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); | |
21eeb7aa | 238 | down(&f->sem); |
182ec4ee | 239 | |
1da177e4 LT |
240 | 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; | |
182ec4ee | 259 | |
1da177e4 LT |
260 | 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; | |
182ec4ee | 266 | |
1da177e4 LT |
267 | 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; | |
182ec4ee | 303 | } |
1da177e4 LT |
304 | |
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 | ||
321 | void 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 | ||
343 | int 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); | |
182ec4ee | 359 | } |
1da177e4 LT |
360 | |
361 | if (!(*flags & MS_RDONLY)) | |
362 | jffs2_start_garbage_collect_thread(c); | |
182ec4ee | 363 | |
1da177e4 LT |
364 | *flags |= MS_NOATIME; |
365 | ||
366 | return 0; | |
367 | } | |
368 | ||
369 | void 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. */ | |
386 | struct 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); | |
182ec4ee | 397 | |
1da177e4 | 398 | inode = new_inode(sb); |
182ec4ee | 399 | |
1da177e4 LT |
400 | if (!inode) |
401 | return ERR_PTR(-ENOMEM); | |
402 | ||
403 | f = JFFS2_INODE_INFO(inode); | |
404 | jffs2_init_inode_info(f); | |
21eeb7aa | 405 | down(&f->sem); |
1da177e4 LT |
406 | |
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 | ||
443 | int 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 | ||
2f82ce1e | 452 | #ifndef CONFIG_JFFS2_FS_WRITEBUFFER |
1da177e4 LT |
453 | 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 | } | |
8f15fd55 AV |
457 | 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 | |
1da177e4 LT |
462 | |
463 | c->flash_size = c->mtd->size; | |
182ec4ee | 464 | c->sector_size = c->mtd->erasesize; |
1da177e4 | 465 | blocks = c->flash_size / c->sector_size; |
1da177e4 LT |
466 | |
467 | /* | |
468 | * Size alignment check | |
469 | */ | |
470 | if ((c->sector_size * blocks) != c->flash_size) { | |
182ec4ee | 471 | c->flash_size = c->sector_size * blocks; |
1da177e4 LT |
472 | printk(KERN_INFO "jffs2: Flash size not aligned to erasesize, reducing to %dKiB\n", |
473 | c->flash_size / 1024); | |
474 | } | |
475 | ||
1da177e4 LT |
476 | 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); | |
1da177e4 LT |
482 | |
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")); | |
6dac02a5 | 504 | goto out_root_i; |
1da177e4 LT |
505 | } |
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 | ||
1da177e4 | 512 | sb->s_maxbytes = 0xFFFFFFFF; |
1da177e4 LT |
513 | 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); | |
1da177e4 LT |
522 | jffs2_free_ino_caches(c); |
523 | jffs2_free_raw_node_refs(c); | |
4ce1f562 | 524 | if (jffs2_blocks_use_vmalloc(c)) |
1da177e4 LT |
525 | 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 | ||
536 | void jffs2_gc_release_inode(struct jffs2_sb_info *c, | |
537 | struct jffs2_inode_info *f) | |
538 | { | |
539 | iput(OFNI_EDONI_2SFFJ(f)); | |
540 | } | |
541 | ||
542 | struct 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 | |
182ec4ee | 549 | obsolete. This has to be because we're still waiting for |
1da177e4 LT |
550 | the final (close() and) iput() to happen. |
551 | ||
182ec4ee | 552 | There's a possibility that the final iput() could have |
1da177e4 LT |
553 | 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 | ||
182ec4ee | 558 | The nlink can't _become_ zero at this point because we're |
1da177e4 LT |
559 | 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 | ||
182ec4ee TG |
605 | unsigned char *jffs2_gc_fetch_page(struct jffs2_sb_info *c, |
606 | struct jffs2_inode_info *f, | |
1da177e4 LT |
607 | unsigned long offset, |
608 | unsigned long *priv) | |
609 | { | |
610 | struct inode *inode = OFNI_EDONI_2SFFJ(f); | |
611 | struct page *pg; | |
612 | ||
182ec4ee | 613 | pg = read_cache_page(inode->i_mapping, offset >> PAGE_CACHE_SHIFT, |
1da177e4 LT |
614 | (void *)jffs2_do_readpage_unlock, inode); |
615 | if (IS_ERR(pg)) | |
616 | return (void *)pg; | |
182ec4ee | 617 | |
1da177e4 LT |
618 | *priv = (unsigned long)pg; |
619 | return kmap(pg); | |
620 | } | |
621 | ||
622 | void 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 | ||
632 | static int jffs2_flash_setup(struct jffs2_sb_info *c) { | |
633 | int ret = 0; | |
182ec4ee | 634 | |
1da177e4 LT |
635 | 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 | } | |
182ec4ee | 648 | |
8f15fd55 AV |
649 | /* and Dataflash */ |
650 | if (jffs2_dataflash(c)) { | |
651 | ret = jffs2_dataflash_setup(c); | |
652 | if (ret) | |
653 | return ret; | |
654 | } | |
59da721a NP |
655 | |
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 | ||
1da177e4 LT |
663 | return ret; |
664 | } | |
665 | ||
666 | void 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 | } | |
182ec4ee | 676 | |
8f15fd55 AV |
677 | /* and DataFlash */ |
678 | if (jffs2_dataflash(c)) { | |
679 | jffs2_dataflash_cleanup(c); | |
680 | } | |
59da721a NP |
681 | |
682 | /* and Intel "Sibley" flash */ | |
683 | if (jffs2_nor_wbuf_flash(c)) { | |
684 | jffs2_nor_wbuf_flash_cleanup(c); | |
685 | } | |
1da177e4 | 686 | } |