*** 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 #ifdef 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;
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
449 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n, loff_t *pos)
450 {
451         yaffs_Object *obj;
452         int nWritten,ipos;
453         struct inode *inode;
454         
455         obj  = yaffs_DentryToObject(f->f_dentry);
456         inode = f->f_dentry->d_inode;
457
458         if(!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND)
459         {
460                 ipos = inode->i_size;
461         }
462         else
463         {
464                 ipos = *pos;
465         }
466         
467         
468         if(!obj)
469         {
470                 T((KERN_DEBUG"yaffs_file_write: hey obj is null!\n"));
471         }
472         else
473         {
474                 T((KERN_DEBUG"yaffs_file_write about to write writing %d bytes to object %d at %d\n",n,obj->objectId,ipos));
475         }
476
477         nWritten = yaffs_WriteDataToFile(obj,buf,ipos,n);
478
479         T((KERN_DEBUG"yaffs_file_write writing %d bytes, %d written at %d\n",n,nWritten,ipos));
480         if(nWritten > 0)
481         {
482                 ipos += nWritten;
483                 *pos = ipos;
484                 if(ipos > inode->i_size)
485                 {
486                         inode->i_size = ipos;
487                         inode->i_blocks = (ipos + inode->i_blksize - 1)/ inode->i_blksize;
488                         
489                         T((KERN_DEBUG"yaffs_file_write size updated to %d bytes, %d blocks\n",ipos,inode->i_blocks));
490                 }
491                 
492         }
493         return nWritten != n ? -ENOSPC : nWritten;      
494 }
495
496
497 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
498 {
499         yaffs_Object *obj;
500         struct inode *inode = f->f_dentry->d_inode;
501         unsigned long offset, curoffs;
502         struct list_head *i;    
503         yaffs_Object *l;
504         
505         char name[YAFFS_MAX_NAME_LENGTH +1];
506                 
507         obj =  yaffs_DentryToObject(f->f_dentry);
508         
509         offset = f->f_pos;
510         
511         T(("yaffs_readdir: starting at %d\n",(int)offset));
512         
513         if(offset == 0)
514         {
515         T((KERN_DEBUG"yaffs_readdir: entry . ino %d \n",(int)inode->i_ino));
516                 if(filldir(dirent,".",1,offset,inode->i_ino,DT_DIR) < 0)
517                 {
518                         goto out;
519                 }
520                 offset++;
521                 f->f_pos++;
522         }
523         if(offset == 1)
524         {
525                 T((KERN_DEBUG"yaffs_readdir: entry .. ino %d \n",(int)f->f_dentry->d_parent->d_inode->i_ino));
526                 if(filldir(dirent,"..",2,offset,f->f_dentry->d_parent->d_inode->i_ino,DT_DIR) < 0)
527                 {
528                         goto out;
529                 }
530                 offset++;
531                 f->f_pos++;
532         }
533         
534         curoffs = 1;
535         
536         //down(&obj->sem);
537
538         
539         list_for_each(i,&obj->variant.directoryVariant.children)
540         {
541                 curoffs++;
542                 if(curoffs >= offset)
543                 {               
544                         l = list_entry(i, yaffs_Object,siblings);
545                         
546                         yaffs_GetObjectName(l,name,YAFFS_MAX_NAME_LENGTH+1); 
547                         T((KERN_DEBUG"yaffs_readdir: %s inode %d\n",name,yaffs_GetObjectInode(l)));
548                         
549                         if(filldir(dirent,
550                                            name,
551                                            strlen(name),
552                                            offset,
553                                            yaffs_GetObjectInode(l),
554                                            yaffs_GetObjectType(l))
555                                            < 0)
556                         {
557                                 goto up_and_out;
558                         }
559                         
560                         offset++;
561                         f->f_pos++;        
562                 }
563         }
564
565   up_and_out:
566
567         //up(&obj->sem);
568         
569   out:
570         return 0;
571 }
572
573
574 /*
575  * File creation. Allocate an inode, and we're done..
576  */
577 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode, int dev)
578 {
579         struct inode *inode;
580         
581         yaffs_Object *obj = NULL;
582         yaffs_Object *parent = yaffs_InodeToObject(dir);
583         
584         int error = -ENOSPC;
585
586         if(parent)
587         {
588                 T((KERN_DEBUG"yaffs_mknod: parent object %d type %d\n",
589                                          parent->objectId,parent->variantType));
590         }
591         else
592         {
593                 T((KERN_DEBUG"yaffs_mknod: could not get parent object\n"));
594                 return -EPERM;
595         }
596         
597         T(("yaffs_mknod: making oject for %s, mode %x dev %x\n",
598                                 dentry->d_name.name, mode,dev));
599
600         switch (mode & S_IFMT) 
601         {
602                 default:
603                         // Special (socket, fifo, device...)
604                         T((KERN_DEBUG"yaffs_mknod: making special\n"));
605                         obj = yaffs_MknodSpecial(parent,dentry->d_name.name,mode,current->uid, current->gid,dev);
606                         break;
607                 case S_IFREG:   // file         
608                         T((KERN_DEBUG"yaffs_mknod: making file\n"));
609                         obj = yaffs_MknodFile(parent,dentry->d_name.name,mode,current->uid, current->gid);
610                         break;
611                 case S_IFDIR:   // directory
612                         T((KERN_DEBUG"yaffs_mknod: making directory\n"));
613                         obj = yaffs_MknodDirectory(parent,dentry->d_name.name,mode,current->uid, current->gid);
614                         break;
615                 case S_IFLNK:   // symlink
616                         T((KERN_DEBUG"yaffs_mknod: making file\n"));
617                         obj = NULL; // Do we ever get here?
618                         break;
619         }
620         
621         if(obj)
622         {
623                 inode = yaffs_get_inode(dir->i_sb, mode, dev, obj);
624                 d_instantiate(dentry, inode);
625                 T((KERN_DEBUG"yaffs_mknod created object %d count = %d\n",obj->objectId,atomic_read(&inode->i_count)));
626                 error = 0;
627         }
628         else
629         {
630                 T((KERN_DEBUG"yaffs_mknod failed making object\n"));
631                 error = -ENOMEM;
632         }
633         
634
635         return error;
636 }
637
638 static int yaffs_mkdir(struct inode * dir, struct dentry * dentry, int mode)
639 {
640         int retVal;
641         T((KERN_DEBUG"yaffs_mkdir\n"));
642         retVal =  yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
643 #if 0
644  // attempt to fix dir bug - didn't work
645         if(!retVal)
646         {
647                 dget(dentry);
648         }
649 #endif
650         return retVal;
651 }
652
653 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode)
654 {
655         T((KERN_DEBUG"yaffs_create\n"));
656         return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
657 }
658
659
660 static int yaffs_unlink(struct inode * dir, struct dentry *dentry)
661 {
662         
663         T((KERN_DEBUG"yaffs_unlink\n"));
664         
665         if(yaffs_Unlink(yaffs_InodeToObject(dir),dentry->d_name.name) == YAFFS_OK)
666         {
667                 return 0;
668         }
669         else
670         {
671                 return -ENOTEMPTY;
672         }
673 }
674
675
676 /*
677  * Create a link...
678  */
679 static int yaffs_link(struct dentry *old_dentry, struct inode * dir, struct dentry * dentry)
680 {
681         struct inode *inode = old_dentry->d_inode;
682         yaffs_Object *obj = NULL;
683         yaffs_Object *link=NULL;
684         
685         T((KERN_DEBUG"yaffs_link\n"));
686         
687         obj = yaffs_InodeToObject(inode);
688         
689         link = yaffs_Link(yaffs_InodeToObject(dir),dentry->d_name.name,obj);
690         
691         if(link)
692         {
693                 return 0;
694         }
695         
696         
697         return -EPERM; 
698 }
699
700
701 static int yaffs_symlink(struct inode * dir, struct dentry *dentry, const char * symname)
702 {
703         yaffs_Object *obj;
704         
705         T((KERN_DEBUG"yaffs_symlink\n"));
706         
707         obj = yaffs_MknodSymLink(yaffs_InodeToObject(dir), dentry->d_name.name, 
708                                                          S_IFLNK | S_IRWXUGO, current->uid, current->gid,
709                                                          symname);
710
711         if(obj)
712         {
713                 T((KERN_DEBUG"symlink created OK\n"));
714                 return 0;
715         }
716         else
717         {
718                 T((KERN_DEBUG"symlink not created\n"));
719
720         }
721         
722         return -ENOMEM;
723 }
724
725 static int yaffs_sync_object(struct file * file, struct dentry *dentry, int datasync)
726 {
727
728         T((KERN_DEBUG"yaffs_sync_object\n"));
729         yaffs_FlushFile(yaffs_DentryToObject(dentry));
730         return 0;
731 }
732
733 /*
734  * The VFS layer already does all the dentry stuff for rename.
735  */
736 static int yaffs_rename(struct inode * old_dir, struct dentry *old_dentry, struct inode * new_dir,struct dentry *new_dentry)
737 {
738         
739         
740         if( yaffs_RenameObject(yaffs_InodeToObject(old_dir),old_dentry->d_name.name,
741                                yaffs_InodeToObject(new_dir),new_dentry->d_name.name) == YAFFS_OK)
742         {
743                 return 0;
744         }
745         else
746         {
747                 return -ENOTEMPTY;
748         }
749         
750
751 }
752
753 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
754 {
755         struct inode *inode = dentry->d_inode;
756         int error;
757         
758         T((KERN_DEBUG"yaffs_setattr\n"));
759         
760         if((error = inode_change_ok(inode,attr)) == 0)
761         {
762         
763                 if(yaffs_SetAttributes(yaffs_InodeToObject(inode),attr) == YAFFS_OK)
764                 {
765                         error = 0;
766                 }
767                 else
768                 {
769                         error = -EPERM;
770                 }
771
772                 inode_setattr(inode,attr);
773         }
774         return error;
775 }
776
777 static int yaffs_statfs(struct super_block *sb, struct statfs *buf)
778 {
779         T((KERN_DEBUG"yaffs_statfs\n"));
780
781         buf->f_type = YAFFS_MAGIC;
782         buf->f_bsize = sb->s_blocksize;
783         buf->f_namelen = 255;
784         buf->f_blocks = yaffs_SuperToDevice(sb)->nBlocks * YAFFS_CHUNKS_PER_BLOCK/
785                                                 (sb->s_blocksize/YAFFS_BYTES_PER_CHUNK);
786         buf->f_files = 0;
787         buf->f_ffree = 0;
788         buf->f_bfree = yaffs_GetNumberOfFreeChunks(yaffs_SuperToDevice(sb))/
789                                                 (sb->s_blocksize/YAFFS_BYTES_PER_CHUNK);
790         buf->f_bavail =  buf->f_bfree;
791         return 0;
792 }
793
794 static void yaffs_read_inode (struct inode *inode)
795 {
796
797         yaffs_Object *obj ; 
798         
799         T((KERN_DEBUG"yaffs_read_inode for %d\n",(int)inode->i_ino));
800
801         obj  = yaffs_FindObjectByNumber(yaffs_SuperToDevice(inode->i_sb),inode->i_ino);
802         
803         yaffs_FillInodeFromObject(inode,obj);
804 }
805
806
807 static void yaffs_put_super(struct super_block *sb)
808 {
809         yaffs_Device *dev = yaffs_SuperToDevice(sb);
810         
811         if(dev->putSuperFunc)
812         {
813                  dev->putSuperFunc(sb);
814         }
815 }
816
817
818 #ifdef YAFFS_MTD_ENABLED
819
820 static void  yaffs_MTDPutSuper(struct super_block *sb)
821 {
822         
823         struct mtd_info *mtd = yaffs_SuperToDevice(sb)->genericDevice;
824         
825         if(mtd->sync)
826         {
827                 mtd->sync(mtd);
828         }
829         
830         put_mtd_device(mtd);
831 }
832
833 #endif
834
835 static struct super_block *yaffs_internal_read_super(int useRam, struct super_block * sb, void * data, int silent)
836 {
837         struct inode * inode;
838         struct dentry * root;
839         yaffs_Device *dev;
840         
841         sb->s_magic = YAFFS_MAGIC;
842         sb->s_op = &yaffs_super_ops;
843         
844         if(!sb)
845                 printk(KERN_INFO"yaffs: sb is NULL\n");
846         else if(!sb->s_dev)
847                 printk(KERN_INFO"yaffs: sb->s_dev is NULL\n");
848         else if(! kdevname(sb->s_dev))
849                 printk(KERN_INFO"yaffs: kdevname is NULL\n");
850         else
851                 printk(KERN_INFO"yaffs: dev is %d name is \"%s\"\n", sb->s_dev, kdevname(sb->s_dev));
852
853         
854
855 #if CONFIG_YAFFS_USE_CHUNK_SIZE
856         sb->s_blocksize = YAFFS_BYTES_PER_CHUNK;
857         sb->s_blocksize_bits = YAFFS_CHUNK_SIZE_SHIFT;
858 #else
859         sb->s_blocksize = PAGE_CACHE_SIZE;
860         sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
861 #endif
862         T(("yaffs_read_super: %s block size %d\n", useRam ? "RAM" : "MTD",sb->s_blocksize));
863
864         
865         if(useRam)
866         {
867
868 #ifdef YAFFS_RAM_ENABLED
869                 // Set the yaffs_Device up for ram emulation
870
871                 sb->u.generic_sbp =     dev = kmalloc(sizeof(yaffs_Device),GFP_KERNEL);
872                 if(!dev)
873                 {
874                         // Deep shit could not allocate device structure
875                         printk(KERN_DEBUG"yaffs_read_super: Failed trying to allocate yaffs_Device. Terminating debug.\n");
876                         return NULL;
877                 }
878
879                 memset(dev,0,sizeof(yaffs_Device));
880                 dev->genericDevice = NULL; // Not used for RAM emulation.
881
882                 dev->nBlocks = YAFFS_RAM_EMULATION_SIZE / (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK);
883                 dev->startBlock = 1;  // Don't use block 0
884                 dev->endBlock = dev->nBlocks - 1;
885
886                 dev->writeChunkToNAND = nandemul_WriteChunkToNAND;
887                 dev->readChunkFromNAND = nandemul_ReadChunkFromNAND;
888                 dev->eraseBlockInNAND = nandemul_EraseBlockInNAND;
889                 dev->initialiseNAND = nandemul_InitialiseNAND;
890                 
891 #endif
892
893         }
894         else
895         {       
896 #ifdef YAFFS_MTD_ENABLED
897                 struct mtd_info *mtd;
898                 
899                 printk(KERN_DEBUG "yaffs: Attempting MTD mount on %u.%u, \"%s\"\n",
900                  MAJOR(sb->s_dev),MINOR(sb->s_dev),kdevname(sb->s_dev));
901                         
902                 // Hope it's a NAND mtd
903                 mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
904                 if (!mtd) 
905                 {
906                         printk(KERN_DEBUG "yaffs: MTD device #%u doesn't appear to exist\n", MINOR(sb->s_dev));
907                         return NULL;
908                 }
909                 
910                 if(mtd->type != MTD_NANDFLASH)
911                 {
912                         printk(KERN_DEBUG "yaffs: MTD device is not NAND it's type %d\n", mtd->type);
913                         return NULL;
914                 }
915
916                 printk(KERN_DEBUG" erase %x\n",mtd->erase);
917                 printk(KERN_DEBUG" read %x\n",mtd->read);
918                 printk(KERN_DEBUG" write %x\n",mtd->write);
919                 printk(KERN_DEBUG" readoob %x\n",mtd->read_oob);
920                 printk(KERN_DEBUG" writeoob %x\n",mtd->write_oob);
921                 printk(KERN_DEBUG" oobblock %x\n",mtd->oobblock);
922                 printk(KERN_DEBUG" oobsize %x\n",mtd->oobsize);
923
924
925                 if(!mtd->erase ||
926                    !mtd->read  ||
927                    !mtd->write ||
928                    !mtd->read_oob ||
929                    !mtd->write_oob)
930                 {
931                         printk(KERN_DEBUG "yaffs: MTD device does not support required functions\n");
932                         return NULL;
933                 }
934                 
935                 if(mtd->oobblock != YAFFS_BYTES_PER_CHUNK ||
936                    mtd->oobsize != YAFFS_BYTES_PER_SPARE)
937                 {
938                         printk(KERN_DEBUG "yaffs: MTD device does not support have the right page sizes\n");
939                         return NULL;
940                 }
941                    
942
943                 // OK, so if we got here, we have an MTD that's NAND and looks 
944                 // like it has the right capabilities
945                 // Set the yaffs_Device up for ram emulation
946
947                 sb->u.generic_sbp =     dev = kmalloc(sizeof(yaffs_Device),GFP_KERNEL);
948                 if(!dev)
949                 {
950                         // Deep shit could not allocate device structure
951                         printk(KERN_DEBUG"yaffs_read_super: Failed trying to allocate yaffs_Device. Terminating debug.\n");
952                         return NULL;
953                 }
954
955                 memset(dev,0,sizeof(yaffs_Device));
956                 dev->genericDevice = mtd; 
957
958                 // Set up the memory size parameters....
959                 
960 // NCB          dev->nBlocks = YAFFS_RAM_EMULATION_SIZE / (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK);
961                 dev->nBlocks = mtd->size / (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK);
962                 dev->startBlock = 1;  // Don't use block 0
963                 dev->endBlock = dev->nBlocks - 1;
964
965                 // ... and the functions.
966                 dev->writeChunkToNAND = nandmtd_WriteChunkToNAND;
967                 dev->readChunkFromNAND = nandmtd_ReadChunkFromNAND;
968                 dev->eraseBlockInNAND = nandmtd_EraseBlockInNAND;
969                 dev->initialiseNAND = nandmtd_InitialiseNAND;
970                 
971                 dev->putSuperFunc = yaffs_MTDPutSuper;
972 #endif
973         }
974
975
976
977         yaffs_GutsInitialise(yaffs_SuperToDevice(sb));
978         T(("yaffs_read_super: guts initialised\n"));
979
980         // Create root inode
981         inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0,yaffs_Root(yaffs_SuperToDevice(sb)));
982         if (!inode)
983                 return NULL;
984
985         T(("yaffs_read_super: got root inode\n"));
986                 
987
988         root = d_alloc_root(inode);
989
990         T(("yaffs_read_super: d_alloc_root done\n"));
991
992         if (!root) {
993                 iput(inode);
994                 return NULL;
995         }
996         sb->s_root = root;
997
998         T(("yaffs_read_super: done\n"));
999         return sb;
1000 }
1001
1002 #ifdef YAFFS_MTD_ENABLED
1003 static struct super_block *yaffs_read_super(struct super_block * sb, void * data, int silent)
1004 {
1005         return yaffs_internal_read_super(0,sb,data,silent);
1006 }
1007
1008 static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super, FS_REQUIRES_DEV);
1009 #endif
1010
1011 #ifdef YAFFS_RAM_ENABLED
1012
1013 static struct super_block *yaffs_ram_read_super(struct super_block * sb, void * data, int silent)
1014 {
1015         return yaffs_internal_read_super(1,sb,data,silent);
1016 }
1017
1018 static DECLARE_FSTYPE(yaffs_ram_fs_type, "yaffsram", yaffs_ram_read_super, FS_SINGLE);
1019 #endif // YAFFS_RAM_ENABLED
1020
1021
1022 static struct proc_dir_entry *my_proc_entry;
1023
1024
1025 static int  yaffs_proc_read(
1026         char *page,
1027         char **start,
1028         off_t offset,
1029         int count,
1030         int *eof,
1031         void *data
1032         )
1033 {
1034
1035         static char my_buffer[1000];
1036
1037         if (offset > 0) return 0;
1038
1039         /* Fill the buffer and get its length */
1040         sprintf( my_buffer, 
1041                 "YAFFS built:"__DATE__ " "__TIME__"\n"
1042                 
1043         );
1044
1045         strcpy(page,my_buffer);
1046         return strlen(my_buffer);
1047 }
1048
1049 static int __init init_yaffs_fs(void)
1050 {
1051         int error = 0;
1052         
1053         printk(KERN_DEBUG "yaffs " __DATE__ " " __TIME__ " Initialisation\n");
1054 #if CONFIG_YAFFS_USE_GENERIC_RW
1055         printk(KERN_DEBUG "yaffs is using generic read/write (caching)\n");
1056 #else
1057         printk(KERN_DEBUG "yaffs is using direct read/write (uncached)\n");
1058 #endif
1059
1060
1061     /* Install the proc_fs entry */
1062     my_proc_entry = create_proc_read_entry("yaffs",
1063                                            S_IRUGO | S_IFREG,
1064                                            &proc_root,
1065                                            yaffs_proc_read,
1066                                            NULL);
1067     if(!my_proc_entry)
1068     {
1069        return -ENOMEM;
1070     }
1071
1072 #ifdef YAFFS_RAM_ENABLED
1073
1074     error = register_filesystem(&yaffs_ram_fs_type);
1075     if(error)
1076     {
1077         return error;
1078     }
1079 #endif //YAFFS_RAM_ENABLED
1080
1081 #ifdef YAFFS_MTD_ENABLED
1082         error = register_filesystem(&yaffs_fs_type);
1083         if(error)
1084         {
1085 #ifdef YAFFS_RAM_ENABLED
1086                 unregister_filesystem(&yaffs_ram_fs_type);
1087 #endif //YAFFS_RAM_ENABLED
1088         }
1089 #endif // YAFFS_MTD_ENABLED
1090
1091         return error;
1092 }
1093
1094 static void __exit exit_yaffs_fs(void)
1095 {
1096         printk(KERN_DEBUG "yaffs " __DATE__ " " __TIME__ " Clean up\n");
1097
1098     remove_proc_entry("yaffs",&proc_root);
1099     
1100 #ifdef YAFFS_RAM_ENABLED
1101         unregister_filesystem(&yaffs_ram_fs_type);
1102 #endif
1103 #ifdef YAFFS_MTD_ENABLED
1104         unregister_filesystem(&yaffs_fs_type);
1105 #endif
1106
1107 }
1108
1109 module_init(init_yaffs_fs)
1110 module_exit(exit_yaffs_fs)
1111
1112 MODULE_DESCRIPTION("YAFFS - a NAND specific flash file system");
1113 MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002");
1114 MODULE_LICENSE("GPL");
1115