*** empty log message ***
[yaffs/.git] / yaffs_fs.c
1 /*
2  * YAFFS: Yet another FFS. A NAND-flash specific file system.
3  * yaffs_fs.c
4  *
5  * Copyright (C) 2002 Aleph One Ltd.
6  *   for Toby Churchill Ltd and Brightstar Engineering
7  *
8  * Created by Charles Manning <charles@aleph1.co.uk>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License version 2 as
12  * published by the Free Software Foundation.
13  *
14  * This is the file system front-end to YAFFS that hooks it up to
15  * the VFS.
16  *
17  * Special notes: 
18  * >> sb->u.generic_sbp points to the yaffs_Device associated with this superblock
19  * >> inode->u.generic_ip points to the associated yaffs_Object.
20  *
21  *
22  * Acknowledgements:
23  * * Luc van OostenRyck for O_APPEND patch.
24  * * Nick Bane for patches marked NCB.
25  * * Some code bodily lifted from JFFS2.
26  */
27
28
29 #include <linux/config.h>
30 #include <linux/kernel.h>
31 #include <linux/module.h>
32 #include <linux/version.h>
33 #include <linux/slab.h>
34 #include <linux/init.h>
35 #include <linux/list.h>
36 #include <linux/fs.h>
37 #include <linux/proc_fs.h>
38 #include <linux/pagemap.h>
39 #include <linux/mtd/mtd.h>
40 #include <linux/interrupt.h>
41 #include <linux/string.h>
42 #include <linux/locks.h>
43
44 #include <asm/uaccess.h>
45
46 #include "yaffs_guts.h"
47
48 #ifdef YAFFS_RAM_ENABLED
49 #include "yaffs_nandemul.h" 
50 // 2 MB of RAM for emulation
51 #define YAFFS_RAM_EMULATION_SIZE  0x200000
52 #endif //YAFFS_RAM_ENABLED
53
54 #ifdef YAFFS_MTD_ENABLED
55 #include <linux/mtd/mtd.h>
56 #include "yaffs_mtdif.h"
57 #endif //YAFFS_MTD_ENABLED
58
59 #define T(x) printk x
60
61
62
63 #define yaffs_InodeToObject(iptr) ((yaffs_Object *)((iptr)->u.generic_ip))
64 #define yaffs_DentryToObject(dptr) yaffs_InodeToObject((dptr)->d_inode)
65 #define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->u.generic_sbp)
66
67
68
69 static void yaffs_put_super(struct super_block *sb);
70
71 static ssize_t yaffs_file_read(struct file *f, char *buf, size_t n, loff_t *pos);
72 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n, loff_t *pos);
73
74 static int yaffs_sync_object(struct file * file, struct dentry *dentry, int datasync);
75
76 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir);
77
78 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode);
79 static struct dentry * yaffs_lookup(struct inode *dir, struct dentry *dentry);
80 static int yaffs_link(struct dentry *old_dentry, struct inode * dir, struct dentry * dentry);
81 static int yaffs_unlink(struct inode * dir, struct dentry *dentry);
82 static int yaffs_symlink(struct inode * dir, struct dentry *dentry, const char * symname);
83 static int yaffs_mkdir(struct inode * dir, struct dentry * dentry, int mode);
84 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode, int dev);
85 static int yaffs_rename(struct inode * old_dir, struct dentry *old_dentry, struct inode * new_dir,struct dentry *new_dentry);
86 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr);
87
88 static int yaffs_statfs(struct super_block *sb, struct statfs *buf);
89 static void yaffs_read_inode (struct inode *inode);
90 static struct super_block *yaffs_read_super(struct super_block * sb, void * data, int silent);
91 static void yaffs_put_inode (struct inode *inode);
92 static int yaffs_readpage(struct file *file, struct page * page);
93
94 static int yaffs_prepare_write(struct file *f, struct page *pg, unsigned offset, unsigned to);
95 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset, unsigned to);
96
97 static int yaffs_readlink(struct dentry *dentry, char *buffer, int buflen);
98 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
99
100
101
102
103
104 static struct address_space_operations yaffs_file_address_operations = {
105         readpage:       yaffs_readpage,
106         prepare_write:  yaffs_prepare_write,
107         commit_write:   yaffs_commit_write
108 };
109
110
111 static struct file_operations yaffs_file_operations = {
112 #if CONFIG_YAFFS_USE_GENERIC_RW
113         read:           generic_file_read,
114         write:          generic_file_write,
115 #else
116         read:           yaffs_file_read,
117         write:          yaffs_file_write,
118 #endif
119         mmap:           generic_file_mmap,
120         fsync:          yaffs_sync_object,
121 };
122
123
124 static struct inode_operations yaffs_file_inode_operations = {
125         setattr:        yaffs_setattr,
126 };
127
128
129 struct inode_operations yaffs_symlink_inode_operations =
130 {       
131         readlink:       yaffs_readlink,
132         follow_link:    yaffs_follow_link,
133         setattr:        yaffs_setattr
134 };
135
136 static struct inode_operations yaffs_dir_inode_operations = {
137         create:         yaffs_create,
138         lookup:         yaffs_lookup,
139         link:           yaffs_link,
140         unlink:         yaffs_unlink,   
141         symlink:        yaffs_symlink,
142         mkdir:          yaffs_mkdir,
143         rmdir:          yaffs_unlink,
144         mknod:          yaffs_mknod,
145         rename:         yaffs_rename,
146         setattr:        yaffs_setattr,
147 };
148
149 static struct file_operations yaffs_dir_operations = {
150         read:           generic_read_dir,
151         readdir:        yaffs_readdir,
152         fsync:          yaffs_sync_object,
153 };
154
155
156 static struct super_operations yaffs_super_ops = {
157         statfs:                 yaffs_statfs,
158         read_inode:             yaffs_read_inode,
159         put_inode:              yaffs_put_inode,
160         put_super:              yaffs_put_super,
161 //      read_inode:
162 //      remount_fs:
163 //      clear_inode:
164 };
165
166
167
168
169
170 static int yaffs_readlink(struct dentry *dentry, char *buffer, int buflen)
171 {
172         unsigned char *alias;
173         int ret;
174
175         alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
176         
177         if(!alias)
178                 return -ENOMEM;
179
180         ret = vfs_readlink(dentry, buffer, buflen, alias);
181         kfree(alias);
182         return ret;
183 }
184
185 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
186 {
187         unsigned char *alias;
188         int ret;
189         alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
190         
191         if(!alias)
192                 return -ENOMEM;
193
194         ret = vfs_follow_link(nd,alias);
195         kfree(alias);
196         return ret;
197 }
198
199
200 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,yaffs_Object *obj);
201
202 /*
203  * Lookup is used to find objects in the fs
204  */
205 static struct dentry * yaffs_lookup(struct inode *dir, struct dentry *dentry)
206 {
207         yaffs_Object *obj;
208         struct inode *inode;
209         
210         
211         T((KERN_DEBUG"yaffs_lookup for %d:%s\n",yaffs_InodeToObject(dir)->objectId,dentry->d_name.name));
212         
213         obj = yaffs_FindObjectByName(yaffs_InodeToObject(dir),dentry->d_name.name);
214         
215         obj = yaffs_GetEquivalentObject(obj); // in case it was a hardlink
216         
217         if(obj)
218         {
219                 T((KERN_DEBUG"yaffs_lookup found %d\n",obj->objectId));
220                 
221                 inode = yaffs_get_inode(dir->i_sb, obj->st_mode,0,obj);
222                 
223                 if(inode)
224                 {
225                         T((KERN_DEBUG"yaffs_loookup looks good\n"));
226                         dget(dentry); // try to solve directory bug
227                         d_add(dentry,inode);
228                         return dentry;
229                 }
230
231         }
232         else
233         {
234                 T((KERN_DEBUG"yaffs_lookup not found\n"));
235                 
236         }
237         return NULL;
238         
239 }
240
241 // For now put inode is just for debugging
242 static void yaffs_put_inode(struct inode *inode)
243 {
244         T(("yaffs_put_inode: ino %d, count %d\n",(int)inode->i_ino, atomic_read(&inode->i_count)));
245         
246         yaffs_FlushFile(yaffs_InodeToObject(inode));
247         
248 }
249
250
251
252 static int yaffs_readpage_nolock(struct file *f, struct page * pg)
253 {
254         // Lifted from jffs2
255         
256         yaffs_Object *obj;
257         unsigned char *pg_buf;  
258         int ret;
259         
260         T((KERN_DEBUG"yaffs_readpage at %08x, size %08x\n", 
261                       pg->index << PAGE_CACHE_SHIFT, PAGE_CACHE_SIZE));
262
263         obj  = yaffs_DentryToObject(f->f_dentry);
264
265         //down(obj->sem);
266         
267         if (!PageLocked(pg))
268                 PAGE_BUG(pg);
269
270         pg_buf = kmap(pg);
271         /* FIXME: Can kmap fail? */
272
273         ret = yaffs_ReadDataFromFile(obj, pg_buf, pg->index << PAGE_CACHE_SHIFT, PAGE_CACHE_SIZE);
274
275         if(ret >= 0) ret = 0;
276
277         if (ret) {
278                 ClearPageUptodate(pg);
279                 SetPageError(pg);
280         } else {
281                 SetPageUptodate(pg);
282                 ClearPageError(pg);
283         }
284
285         flush_dcache_page(pg);
286         kunmap(pg);
287
288
289
290         //up(&obj->sem);
291
292         T((KERN_DEBUG"yaffs_readpage done\n"));
293         return ret;
294 }
295
296 static int yaffs_readpage_unlock(struct file *f, struct page *pg)
297 {
298         int ret = yaffs_readpage_nolock(f,pg);
299         UnlockPage(pg);
300         return ret;
301 }
302
303 static int yaffs_readpage(struct file *f, struct page * pg)
304 {
305         return yaffs_readpage_unlock(f,pg);
306 }
307
308
309 static int yaffs_prepare_write(struct file *f, struct page *pg, unsigned offset, unsigned to)
310 {
311
312         T((KERN_DEBUG"yaffs_prepair_write\n"));
313         if(!Page_Uptodate(pg) && (offset || to < PAGE_CACHE_SIZE))
314                 return  yaffs_readpage_nolock(f,pg);    
315
316         return 0;
317         
318 }
319
320 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset, unsigned to)
321 {
322
323         void *addr = page_address(pg) + offset;
324         loff_t pos = (((loff_t)pg->index) << PAGE_CACHE_SHIFT) + offset;
325         int nBytes = to - offset;
326         int nWritten;
327         
328         unsigned spos = pos;
329         unsigned saddr = addr;
330
331         T((KERN_DEBUG"yaffs_commit_write addr %x pos %x nBytes %d\n",saddr,spos,nBytes));
332         
333         nWritten = yaffs_file_write(f,addr, nBytes, &pos);
334         
335         if(nWritten != nBytes)
336         {
337                 T((KERN_DEBUG"yaffs_commit_write not same size nWritten %d  nBytes %d\n",nWritten,nBytes));
338                 SetPageError(pg);
339                 ClearPageUptodate(pg);
340         }
341
342         T((KERN_DEBUG"yaffs_commit_write returning %d\n",nWritten));
343         
344         return nWritten /* != nBytes ? -ENOMEM : 0 */ ;
345
346 }
347
348
349
350 static void yaffs_FillInodeFromObject(struct inode *inode, yaffs_Object *obj)
351 {
352         if (inode && obj) 
353         {
354                 inode->i_ino = obj->objectId;
355                 inode->i_mode = obj->st_mode;
356                 inode->i_uid = obj->st_uid;
357                 inode->i_gid = obj->st_gid;
358                 inode->i_blksize = inode->i_sb->s_blocksize;
359                 inode->i_blocks = 0;
360                 inode->i_rdev = obj->st_rdev;;
361                 inode->i_atime = obj->st_atime;
362                 inode->i_mtime = obj->st_mtime;
363                 inode->i_ctime = obj->st_ctime;
364                 inode->i_size = yaffs_GetObjectFileLength(obj);
365                 inode->i_nlink = yaffs_GetObjectLinkCount(obj);
366                 
367                 T((KERN_DEBUG"yaffs_FillInode mode %x uid %d gid %d size %d count %d\n",
368                                 inode->i_mode, inode->i_uid, inode->i_gid, (int)inode->i_size, atomic_read(&inode->i_count)));
369                 
370                 switch (obj->st_mode & S_IFMT) 
371                 {
372                         default: // fifo, device or socket
373                                 init_special_inode(inode, obj->st_mode, obj->st_rdev);
374                                 break;
375                         case S_IFREG:   // file         
376                                 inode->i_op = &yaffs_file_inode_operations;
377                                 inode->i_fop = &yaffs_file_operations;
378                                 inode->i_mapping->a_ops = &yaffs_file_address_operations;
379                                 break;
380                         case S_IFDIR:   // directory
381                                 inode->i_op = &yaffs_dir_inode_operations;
382                                 inode->i_fop = &yaffs_dir_operations;
383                                 break;
384                         case S_IFLNK:   // symlink
385                                 inode->i_op = &yaffs_symlink_inode_operations;
386                                 break;
387                 }
388                 
389                 
390                 inode->u.generic_ip = obj;
391                 
392         }
393         else
394         {
395                 T((KERN_DEBUG"yaffs_FileInode invalid parameters\n"));
396         }
397
398 }
399
400 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,yaffs_Object *obj)
401 {
402         struct inode * inode;
403         
404         T((KERN_DEBUG"yaffs_get_inode for object %d\n",obj->objectId));
405
406         inode = iget(sb,obj->objectId);
407
408         // No need to call this since iget calls it via the read_inode callback
409         // yaffs_FillInodeFromObject(inode,obj);
410
411         return inode;
412 }
413
414 static ssize_t yaffs_file_read(struct file *f, char *buf, size_t n, loff_t *pos)
415 {
416         yaffs_Object *obj;
417         int nRead,ipos;
418         struct inode *inode;
419         
420         T((KERN_DEBUG"yaffs_file_read\n"));
421
422         obj  = yaffs_DentryToObject(f->f_dentry);
423         inode = f->f_dentry->d_inode;
424         
425         if (*pos < inode->i_size) 
426         {
427                         if (*pos + n > inode->i_size)
428                         {
429                                 n = inode->i_size - *pos;
430                         }
431         }
432         else
433         {
434                 n = 0;
435         }
436         
437         nRead = yaffs_ReadDataFromFile(obj,buf,*pos,n);
438         if(nRead > 0)
439         {
440                 f->f_pos += nRead;
441         }
442         ipos = *pos;
443         T((KERN_DEBUG"yaffs_file_read read %d bytes, %d read at %d\n",n,nRead,ipos));
444         return nRead;
445         
446 }
447
448 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n, loff_t *pos)
449 {
450         yaffs_Object *obj;
451         int nWritten,ipos;
452         struct inode *inode;
453         
454         obj  = yaffs_DentryToObject(f->f_dentry);
455         inode = f->f_dentry->d_inode;
456
457         if(!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND)
458         {
459                 ipos = inode->i_size;
460         }
461         else
462         {
463                 ipos = *pos;
464         }
465         
466         
467         if(!obj)
468         {
469                 T((KERN_DEBUG"yaffs_file_write: hey obj is null!\n"));
470         }
471         else
472         {
473                 T((KERN_DEBUG"yaffs_file_write about to write writing %d bytes to object %d at %d\n",n,obj->objectId,ipos));
474         }
475
476         nWritten = yaffs_WriteDataToFile(obj,buf,ipos,n);
477
478         T((KERN_DEBUG"yaffs_file_write writing %d bytes, %d written at %d\n",n,nWritten,ipos));
479         if(nWritten > 0)
480         {
481                 ipos += nWritten;
482                 *pos = ipos;
483                 if(ipos > inode->i_size)
484                 {
485                         inode->i_size = ipos;
486                         inode->i_blocks = (ipos + inode->i_blksize - 1)/ inode->i_blksize;
487                         
488                         T((KERN_DEBUG"yaffs_file_write size updated to %d bytes, %d blocks\n",ipos,inode->i_blocks));
489                 }
490                 
491         }
492         return nWritten;        
493 }
494
495
496 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
497 {
498         yaffs_Object *obj;
499         struct inode *inode = f->f_dentry->d_inode;
500         unsigned long offset, curoffs;
501         struct list_head *i;    
502         yaffs_Object *l;
503         
504         char name[YAFFS_MAX_NAME_LENGTH +1];
505                 
506         obj =  yaffs_DentryToObject(f->f_dentry);
507         
508         offset = f->f_pos;
509         
510         T(("yaffs_readdir: starting at %d\n",(int)offset));
511         
512         if(offset == 0)
513         {
514         T((KERN_DEBUG"yaffs_readdir: entry . ino %d \n",(int)inode->i_ino));
515                 if(filldir(dirent,".",1,offset,inode->i_ino,DT_DIR) < 0)
516                 {
517                         goto out;
518                 }
519                 offset++;
520                 f->f_pos++;
521         }
522         if(offset == 1)
523         {
524                 T((KERN_DEBUG"yaffs_readdir: entry .. ino %d \n",(int)f->f_dentry->d_parent->d_inode->i_ino));
525                 if(filldir(dirent,"..",2,offset,f->f_dentry->d_parent->d_inode->i_ino,DT_DIR) < 0)
526                 {
527                         goto out;
528                 }
529                 offset++;
530                 f->f_pos++;
531         }
532         
533         curoffs = 1;
534         
535         //down(&obj->sem);
536
537         
538         list_for_each(i,&obj->variant.directoryVariant.children)
539         {
540                 curoffs++;
541                 if(curoffs >= offset)
542                 {               
543                         l = list_entry(i, yaffs_Object,siblings);
544                         
545                         yaffs_GetObjectName(l,name,YAFFS_MAX_NAME_LENGTH+1); 
546                         T((KERN_DEBUG"yaffs_readdir: %s inode %d\n",name,yaffs_GetObjectInode(l)));
547                         
548                         if(filldir(dirent,
549                                            name,
550                                            strlen(name),
551                                            offset,
552                                            yaffs_GetObjectInode(l),
553                                            yaffs_GetObjectType(l))
554                                            < 0)
555                         {
556                                 goto up_and_out;
557                         }
558                         
559                         offset++;
560                         f->f_pos++;        
561                 }
562         }
563
564   up_and_out:
565
566         //up(&obj->sem);
567         
568   out:
569         return 0;
570 }
571
572
573 /*
574  * File creation. Allocate an inode, and we're done..
575  */
576 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode, int dev)
577 {
578         struct inode *inode;
579         
580         yaffs_Object *obj = NULL;
581         yaffs_Object *parent = yaffs_InodeToObject(dir);
582         
583         int error = -ENOSPC;
584
585         if(parent)
586         {
587                 T((KERN_DEBUG"yaffs_mknod: parent object %d type %d\n",
588                                          parent->objectId,parent->variantType));
589         }
590         else
591         {
592                 T((KERN_DEBUG"yaffs_mknod: could not get parent object\n"));
593                 return -EPERM;
594         }
595         
596         T(("yaffs_mknod: making oject for %s, mode %x dev %x\n",
597                                 dentry->d_name.name, mode,dev));
598
599         switch (mode & S_IFMT) 
600         {
601                 default:
602                         // Special (socket, fifo, device...)
603                         T((KERN_DEBUG"yaffs_mknod: making special\n"));
604                         obj = yaffs_MknodSpecial(parent,dentry->d_name.name,mode,current->uid, current->gid,dev);
605                         break;
606                 case S_IFREG:   // file         
607                         T((KERN_DEBUG"yaffs_mknod: making file\n"));
608                         obj = yaffs_MknodFile(parent,dentry->d_name.name,mode,current->uid, current->gid);
609                         break;
610                 case S_IFDIR:   // directory
611                         T((KERN_DEBUG"yaffs_mknod: making directory\n"));
612                         obj = yaffs_MknodDirectory(parent,dentry->d_name.name,mode,current->uid, current->gid);
613                         break;
614                 case S_IFLNK:   // symlink
615                         T((KERN_DEBUG"yaffs_mknod: making file\n"));
616                         obj = NULL; // Do we ever get here?
617                         break;
618         }
619         
620         if(obj)
621         {
622                 inode = yaffs_get_inode(dir->i_sb, mode, dev, obj);
623                 d_instantiate(dentry, inode);
624                 T((KERN_DEBUG"yaffs_mknod created object %d count = %d\n",obj->objectId,atomic_read(&inode->i_count)));
625                 error = 0;
626         }
627         else
628         {
629                 T((KERN_DEBUG"yaffs_mknod failed making object\n"));
630                 error = -ENOMEM;
631         }
632         
633
634         return error;
635 }
636
637 static int yaffs_mkdir(struct inode * dir, struct dentry * dentry, int mode)
638 {
639         int retVal;
640         T((KERN_DEBUG"yaffs_mkdir\n"));
641         retVal =  yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
642 #if 0
643  // attempt to fix dir bug - didn't work
644         if(!retVal)
645         {
646                 dget(dentry);
647         }
648 #endif
649         return retVal;
650 }
651
652 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode)
653 {
654         T((KERN_DEBUG"yaffs_create\n"));
655         return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
656 }
657
658
659 static int yaffs_unlink(struct inode * dir, struct dentry *dentry)
660 {
661         
662         T((KERN_DEBUG"yaffs_unlink\n"));
663         
664         if(yaffs_Unlink(yaffs_InodeToObject(dir),dentry->d_name.name) == YAFFS_OK)
665         {
666                 return 0;
667         }
668         else
669         {
670                 return -ENOTEMPTY;
671         }
672 }
673
674
675 /*
676  * Create a link...
677  */
678 static int yaffs_link(struct dentry *old_dentry, struct inode * dir, struct dentry * dentry)
679 {
680         struct inode *inode = old_dentry->d_inode;
681         yaffs_Object *obj = NULL;
682         yaffs_Object *link=NULL;
683         
684         T((KERN_DEBUG"yaffs_link\n"));
685         
686         obj = yaffs_InodeToObject(inode);
687         
688         link = yaffs_Link(yaffs_InodeToObject(dir),dentry->d_name.name,obj);
689         
690         if(link)
691         {
692                 return 0;
693         }
694         
695         
696         return -EPERM; 
697 }
698
699
700 static int yaffs_symlink(struct inode * dir, struct dentry *dentry, const char * symname)
701 {
702         yaffs_Object *obj;
703         
704         T((KERN_DEBUG"yaffs_symlink\n"));
705         
706         obj = yaffs_MknodSymLink(yaffs_InodeToObject(dir), dentry->d_name.name, 
707                                                          S_IFLNK | S_IRWXUGO, current->uid, current->gid,
708                                                          symname);
709
710         if(obj)
711         {
712                 T((KERN_DEBUG"symlink created OK\n"));
713                 return 0;
714         }
715         else
716         {
717                 T((KERN_DEBUG"symlink not created\n"));
718
719         }
720         
721         return -ENOMEM;
722 }
723
724 static int yaffs_sync_object(struct file * file, struct dentry *dentry, int datasync)
725 {
726
727         T((KERN_DEBUG"yaffs_sync_object\n"));
728         yaffs_FlushFile(yaffs_DentryToObject(dentry));
729         return 0;
730 }
731
732 /*
733  * The VFS layer already does all the dentry stuff for rename.
734  */
735 static int yaffs_rename(struct inode * old_dir, struct dentry *old_dentry, struct inode * new_dir,struct dentry *new_dentry)
736 {
737         
738         
739         if( yaffs_RenameObject(yaffs_InodeToObject(old_dir),old_dentry->d_name.name,
740                                yaffs_InodeToObject(new_dir),new_dentry->d_name.name) == YAFFS_OK)
741         {
742                 return 0;
743         }
744         else
745         {
746                 return -ENOTEMPTY;
747         }
748         
749
750 }
751
752 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
753 {
754         struct inode *inode = dentry->d_inode;
755         int error;
756         
757         T((KERN_DEBUG"yaffs_setattr\n"));
758         
759         if((error = inode_change_ok(inode,attr)) == 0)
760         {
761         
762                 if(yaffs_SetAttributes(yaffs_InodeToObject(inode),attr) == YAFFS_OK)
763                 {
764                         error = 0;
765                 }
766                 else
767                 {
768                         error = -EPERM;
769                 }
770
771                 inode_setattr(inode,attr);
772         }
773         return error;
774 }
775
776 static int yaffs_statfs(struct super_block *sb, struct statfs *buf)
777 {
778         T((KERN_DEBUG"yaffs_statfs\n"));
779
780         buf->f_type = YAFFS_MAGIC;
781         buf->f_bsize = sb->s_blocksize;
782         buf->f_namelen = 255;
783         buf->f_blocks = yaffs_SuperToDevice(sb)->nBlocks * YAFFS_CHUNKS_PER_BLOCK/
784                                                 (sb->s_blocksize/YAFFS_BYTES_PER_CHUNK);
785         buf->f_files = 0;
786         buf->f_ffree = 0;
787         buf->f_bfree = yaffs_GetNumberOfFreeChunks(yaffs_SuperToDevice(sb))/
788                                                 (sb->s_blocksize/YAFFS_BYTES_PER_CHUNK);
789         buf->f_bavail =  buf->f_bfree;
790         return 0;
791 }
792
793 static void yaffs_read_inode (struct inode *inode)
794 {
795
796         yaffs_Object *obj ; 
797         
798         T((KERN_DEBUG"yaffs_read_inode for %d\n",(int)inode->i_ino));
799
800         obj  = yaffs_FindObjectByNumber(yaffs_SuperToDevice(inode->i_sb),inode->i_ino);
801         
802         yaffs_FillInodeFromObject(inode,obj);
803 }
804
805
806 static void yaffs_put_super(struct super_block *sb)
807 {
808         yaffs_Device *dev = yaffs_SuperToDevice(sb);
809         
810         if(dev->putSuperFunc)
811         {
812                  dev->putSuperFunc(sb);
813         }
814 }
815
816
817 #ifdef YAFFS_MTD_ENABLED
818
819 static void  yaffs_MTDPutSuper(struct super_block *sb)
820 {
821         
822         struct mtd_info *mtd = yaffs_SuperToDevice(sb)->genericDevice;
823         
824         if(mtd->sync)
825         {
826                 mtd->sync(mtd);
827         }
828         
829         put_mtd_device(mtd);
830 }
831
832 #endif
833
834 static struct super_block *yaffs_internal_read_super(int useRam, struct super_block * sb, void * data, int silent)
835 {
836         struct inode * inode;
837         struct dentry * root;
838         yaffs_Device *dev;
839         
840         sb->s_magic = YAFFS_MAGIC;
841         sb->s_op = &yaffs_super_ops;
842         
843         if(!sb)
844                 printk(KERN_INFO"yaffs: sb is NULL\n");
845         else if(!sb->s_dev)
846                 printk(KERN_INFO"yaffs: sb->s_dev is NULL\n");
847         else if(! kdevname(sb->s_dev))
848                 printk(KERN_INFO"yaffs: kdevname is NULL\n");
849         else
850                 printk(KERN_INFO"yaffs: dev is %d name is \"%s\"\n", sb->s_dev, kdevname(sb->s_dev));
851
852         
853
854 #if CONFIG_YAFFS_USE_CHUNK_SIZE
855         sb->s_blocksize = YAFFS_BYTES_PER_CHUNK;
856         sb->s_blocksize_bits = YAFFS_CHUNK_SIZE_SHIFT;
857 #else
858         sb->s_blocksize = PAGE_CACHE_SIZE;
859         sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
860 #endif
861         T(("yaffs_read_super: %s block size %d\n", useRam ? "RAM" : "MTD",sb->s_blocksize));
862
863         
864         if(useRam)
865         {
866
867 #ifdef YAFFS_RAM_ENABLED
868                 // Set the yaffs_Device up for ram emulation
869
870                 sb->u.generic_sbp =     dev = kmalloc(sizeof(yaffs_Device),GFP_KERNEL);
871                 if(!dev)
872                 {
873                         // Deep shit could not allocate device structure
874                         printk(KERN_DEBUG"yaffs_read_super: Failed trying to allocate yaffs_Device. Terminating debug.\n");
875                         return NULL;
876                 }
877
878                 memset(dev,0,sizeof(yaffs_Device));
879                 dev->genericDevice = NULL; // Not used for RAM emulation.
880
881                 dev->nBlocks = YAFFS_RAM_EMULATION_SIZE / (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK);
882                 dev->startBlock = 1;  // Don't use block 0
883                 dev->endBlock = dev->nBlocks - 1;
884
885                 dev->writeChunkToNAND = nandemul_WriteChunkToNAND;
886                 dev->readChunkFromNAND = nandemul_ReadChunkFromNAND;
887                 dev->eraseBlockInNAND = nandemul_EraseBlockInNAND;
888                 dev->initialiseNAND = nandemul_InitialiseNAND;
889                 
890 #endif
891
892         }
893         else
894         {       
895 #ifdef YAFFS_MTD_ENABLED
896                 struct mtd_info *mtd;
897                 
898                 printk(KERN_DEBUG "yaffs: Attempting MTD mount on %u.%u, \"%s\"\n",
899                  MAJOR(sb->s_dev),MINOR(sb->s_dev),kdevname(sb->s_dev));
900                         
901                 // Hope it's a NAND mtd
902                 mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
903                 if (!mtd) 
904                 {
905                         printk(KERN_DEBUG "yaffs: MTD device #%u doesn't appear to exist\n", MINOR(sb->s_dev));
906                         return NULL;
907                 }
908                 
909                 if(mtd->type != MTD_NANDFLASH)
910                 {
911                         printk(KERN_DEBUG "yaffs: MTD device is not NAND it's type %d\n", mtd->type);
912                         return NULL;
913                 }
914
915                 printk(KERN_DEBUG" erase %x\n",mtd->erase);
916                 printk(KERN_DEBUG" read %x\n",mtd->read);
917                 printk(KERN_DEBUG" write %x\n",mtd->write);
918                 printk(KERN_DEBUG" readoob %x\n",mtd->read_oob);
919                 printk(KERN_DEBUG" writeoob %x\n",mtd->write_oob);
920                 printk(KERN_DEBUG" oobblock %x\n",mtd->oobblock);
921                 printk(KERN_DEBUG" oobsize %x\n",mtd->oobsize);
922
923
924                 if(!mtd->erase ||
925                    !mtd->read  ||
926                    !mtd->write ||
927                    !mtd->read_oob ||
928                    !mtd->write_oob)
929                 {
930                         printk(KERN_DEBUG "yaffs: MTD device does not support required functions\n");
931                         return NULL;
932                 }
933                 
934                 if(mtd->oobblock != YAFFS_BYTES_PER_CHUNK ||
935                    mtd->oobsize != YAFFS_BYTES_PER_SPARE)
936                 {
937                         printk(KERN_DEBUG "yaffs: MTD device does not support have the right page sizes\n");
938                         return NULL;
939                 }
940                    
941
942                 // OK, so if we got here, we have an MTD that's NAND and looks 
943                 // like it has the right capabilities
944                 // Set the yaffs_Device up for ram emulation
945
946                 sb->u.generic_sbp =     dev = kmalloc(sizeof(yaffs_Device),GFP_KERNEL);
947                 if(!dev)
948                 {
949                         // Deep shit could not allocate device structure
950                         printk(KERN_DEBUG"yaffs_read_super: Failed trying to allocate yaffs_Device. Terminating debug.\n");
951                         return NULL;
952                 }
953
954                 memset(dev,0,sizeof(yaffs_Device));
955                 dev->genericDevice = mtd; 
956
957                 // Set up the memory size parameters....
958                 
959 // NCB          dev->nBlocks = YAFFS_RAM_EMULATION_SIZE / (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK);
960                 dev->nBlocks = mtd->size / (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK);
961                 dev->startBlock = 1;  // Don't use block 0
962                 dev->endBlock = dev->nBlocks - 1;
963
964                 // ... and the functions.
965                 dev->writeChunkToNAND = nandmtd_WriteChunkToNAND;
966                 dev->readChunkFromNAND = nandmtd_ReadChunkFromNAND;
967                 dev->eraseBlockInNAND = nandmtd_EraseBlockInNAND;
968                 dev->initialiseNAND = nandmtd_InitialiseNAND;
969                 
970                 dev->putSuperFunc = yaffs_MTDPutSuper;
971 #endif
972         }
973
974
975
976         yaffs_GutsInitialise(yaffs_SuperToDevice(sb));
977         T(("yaffs_read_super: guts initialised\n"));
978
979         // Create root inode
980         inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0,yaffs_Root(yaffs_SuperToDevice(sb)));
981         if (!inode)
982                 return NULL;
983
984         T(("yaffs_read_super: got root inode\n"));
985                 
986
987         root = d_alloc_root(inode);
988
989         T(("yaffs_read_super: d_alloc_root done\n"));
990
991         if (!root) {
992                 iput(inode);
993                 return NULL;
994         }
995         sb->s_root = root;
996
997         T(("yaffs_read_super: done\n"));
998         return sb;
999 }
1000
1001 #ifdef YAFFS_MTD_ENABLED
1002 static struct super_block *yaffs_read_super(struct super_block * sb, void * data, int silent)
1003 {
1004         return yaffs_internal_read_super(0,sb,data,silent);
1005 }
1006
1007 static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super, FS_REQUIRES_DEV);
1008 #endif
1009
1010 #ifdef YAFFS_RAM_ENABLED
1011
1012 static struct super_block *yaffs_ram_read_super(struct super_block * sb, void * data, int silent)
1013 {
1014         return yaffs_internal_read_super(1,sb,data,silent);
1015 }
1016
1017 static DECLARE_FSTYPE(yaffs_ram_fs_type, "yaffsram", yaffs_ram_read_super, FS_SINGLE);
1018 #endif // YAFFS_RAM_ENABLED
1019
1020
1021 static struct proc_dir_entry *my_proc_entry;
1022
1023
1024 static int  yaffs_proc_read(
1025         char *page,
1026         char **start,
1027         off_t offset,
1028         int count,
1029         int *eof,
1030         void *data
1031         )
1032 {
1033
1034         static char my_buffer[1000];
1035
1036         if (offset > 0) return 0;
1037
1038         /* Fill the buffer and get its length */
1039         sprintf( my_buffer, 
1040                 "YAFFS built:"__DATE__ " "__TIME__"\n"
1041                 
1042         );
1043
1044         strcpy(page,my_buffer);
1045         return strlen(my_buffer);
1046 }
1047
1048 static int __init init_yaffs_fs(void)
1049 {
1050         int error = 0;
1051         
1052         printk(KERN_DEBUG "yaffs " __DATE__ " " __TIME__ " Initialisation\n");
1053 #if CONFIG_YAFFS_USE_GENERIC_RW
1054         printk(KERN_DEBUG "yaffs is using generic read/write (caching)\n");
1055 #else
1056         printk(KERN_DEBUG "yaffs is using direct read/write (uncached)\n");
1057 #endif
1058
1059
1060     /* Install the proc_fs entry */
1061     my_proc_entry = create_proc_read_entry("yaffs",
1062                                            S_IRUGO | S_IFREG,
1063                                            &proc_root,
1064                                            yaffs_proc_read,
1065                                            NULL);
1066     if(!my_proc_entry)
1067     {
1068        return -ENOMEM;
1069     }
1070
1071 #ifdef YAFFS_RAM_ENABLED
1072
1073     error = register_filesystem(&yaffs_ram_fs_type);
1074     if(error)
1075     {
1076         return error;
1077     }
1078 #endif //YAFFS_RAM_ENABLED
1079
1080 #ifdef YAFFS_MTD_ENABLED
1081         error = register_filesystem(&yaffs_fs_type);
1082         if(error)
1083         {
1084 #ifdef YAFFS_RAM_ENABLED
1085                 unregister_filesystem(&yaffs_ram_fs_type);
1086 #endif //YAFFS_RAM_ENABLED
1087         }
1088 #endif // YAFFS_MTD_ENABLED
1089
1090         return error;
1091 }
1092
1093 static void __exit exit_yaffs_fs(void)
1094 {
1095         printk(KERN_DEBUG "yaffs " __DATE__ " " __TIME__ " Clean up\n");
1096
1097     remove_proc_entry("yaffs",&proc_root);
1098     
1099 #ifdef YAFFS_RAM_ENABLED
1100         unregister_filesystem(&yaffs_ram_fs_type);
1101 #endif
1102 #ifdef YAFFS_MTD_ENABLED
1103         unregister_filesystem(&yaffs_fs_type);
1104 #endif
1105
1106 }
1107
1108 module_init(init_yaffs_fs)
1109 module_exit(exit_yaffs_fs)
1110
1111 MODULE_DESCRIPTION("YAFFS - a NAND specific flash file system");
1112 MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002");
1113 MODULE_LICENSE("GPL");
1114