Mercurial > yaffs-ecoscentric
annotate yaffs_fs.c @ 107:f5445121de97
Hook wide tnode support into Linux
| author | charles |
|---|---|
| date | Wed, 07 Dec 2005 22:19:26 +0000 |
| parents | 0e8b8a695278 |
| children | 1cbbe1e003ac |
| rev | line source |
|---|---|
| 7 | 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: | |
| 78 | 18 * >> 2.4: sb->u.generic_sbp points to the yaffs_Device associated with |
| 19 * this superblock | |
| 20 * >> 2.6: sb->s_fs_info points to the yaffs_Device associated with this | |
| 21 * superblock | |
| 7 | 22 * >> inode->u.generic_ip points to the associated yaffs_Object. |
| 23 * | |
| 24 * Acknowledgements: | |
| 25 * * Luc van OostenRyck for numerous patches. | |
| 26 * * Nick Bane for numerous patches. | |
| 27 * * Nick Bane for 2.5/2.6 integration. | |
| 28 * * Andras Toth for mknod rdev issue. | |
| 29 * * Michael Fischer for finding the problem with inode inconsistency. | |
| 30 * * Some code bodily lifted from JFFS2. | |
| 31 */ | |
| 32 | |
| 78 | 33 const char *yaffs_fs_c_version = |
| 34 "$Id$"; | |
| 7 | 35 extern const char *yaffs_guts_c_version; |
| 36 | |
| 37 #include <linux/config.h> | |
| 38 #include <linux/kernel.h> | |
| 39 #include <linux/module.h> | |
| 40 #include <linux/version.h> | |
| 41 #include <linux/slab.h> | |
| 42 #include <linux/init.h> | |
| 43 #include <linux/list.h> | |
| 44 #include <linux/fs.h> | |
| 45 #include <linux/proc_fs.h> | |
| 29 | 46 #include <linux/smp_lock.h> |
| 7 | 47 #include <linux/pagemap.h> |
| 48 #include <linux/mtd/mtd.h> | |
| 49 #include <linux/interrupt.h> | |
| 50 #include <linux/string.h> | |
| 51 | |
| 52 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0)) | |
| 53 | |
| 54 #include <linux/statfs.h> /* Added NCB 15-8-2003 */ | |
| 55 #include <asm/statfs.h> | |
| 56 #define UnlockPage(p) unlock_page(p) | |
| 57 #define Page_Uptodate(page) test_bit(PG_uptodate, &(page)->flags) | |
| 78 | 58 |
| 59 /* FIXME: use sb->s_id instead ? */ | |
| 60 #define yaffs_devname(sb, buf) bdevname(sb->s_bdev, buf) | |
| 7 | 61 |
| 62 #else | |
| 63 | |
| 64 #include <linux/locks.h> | |
|
39
48513a2b5762
Fix devname which was faked by always using "(unavailable)".
luc
parents:
38
diff
changeset
|
65 #define BDEVNAME_SIZE 0 |
|
48513a2b5762
Fix devname which was faked by always using "(unavailable)".
luc
parents:
38
diff
changeset
|
66 #define yaffs_devname(sb, buf) kdevname(sb->s_dev) |
| 7 | 67 |
| 68 #endif | |
| 69 | |
| 70 #include <asm/uaccess.h> | |
| 71 | |
| 72 #include "yportenv.h" | |
| 73 #include "yaffs_guts.h" | |
| 74 | |
| 86 | 75 unsigned yaffs_traceMask = YAFFS_TRACE_ALWAYS | YAFFS_TRACE_BAD_BLOCKS /* | 0xFFFFFFFF */; |
| 7 | 76 |
| 77 #include <linux/mtd/mtd.h> | |
| 78 #include "yaffs_mtdif.h" | |
| 79 #include "yaffs_mtdif2.h" | |
| 80 | |
| 78 | 81 /*#define T(x) printk x */ |
| 7 | 82 |
| 83 #define yaffs_InodeToObject(iptr) ((yaffs_Object *)((iptr)->u.generic_ip)) | |
| 84 #define yaffs_DentryToObject(dptr) yaffs_InodeToObject((dptr)->d_inode) | |
| 85 | |
| 86 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0)) | |
| 87 #define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->s_fs_info) | |
| 88 #else | |
| 89 #define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->u.generic_sbp) | |
| 90 #endif | |
| 91 | |
| 92 static void yaffs_put_super(struct super_block *sb); | |
| 93 | |
| 78 | 94 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n, |
| 95 loff_t * pos); | |
| 7 | 96 |
| 78 | 97 static int yaffs_file_flush(struct file *file); |
| 7 | 98 |
| 78 | 99 static int yaffs_sync_object(struct file *file, struct dentry *dentry, |
| 100 int datasync); | |
| 7 | 101 |
| 102 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir); | |
| 103 | |
| 104 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0)) | |
| 78 | 105 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode, |
| 106 struct nameidata *n); | |
| 107 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry, | |
| 108 struct nameidata *n); | |
| 7 | 109 #else |
| 110 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode); | |
| 78 | 111 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry); |
| 7 | 112 #endif |
| 78 | 113 static int yaffs_link(struct dentry *old_dentry, struct inode *dir, |
| 114 struct dentry *dentry); | |
| 115 static int yaffs_unlink(struct inode *dir, struct dentry *dentry); | |
| 116 static int yaffs_symlink(struct inode *dir, struct dentry *dentry, | |
| 117 const char *symname); | |
| 118 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode); | |
| 7 | 119 |
| 120 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0)) | |
| 78 | 121 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode, |
| 122 dev_t dev); | |
| 7 | 123 #else |
| 78 | 124 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode, |
| 125 int dev); | |
| 7 | 126 #endif |
| 78 | 127 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 128 struct inode *new_dir, struct dentry *new_dentry); | |
| 7 | 129 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr); |
| 130 | |
| 131 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0)) | |
| 132 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf); | |
| 133 #else | |
| 134 static int yaffs_statfs(struct super_block *sb, struct statfs *buf); | |
| 135 #endif | |
| 78 | 136 static void yaffs_read_inode(struct inode *inode); |
| 7 | 137 |
| 78 | 138 static void yaffs_put_inode(struct inode *inode); |
| 7 | 139 static void yaffs_delete_inode(struct inode *); |
| 140 static void yaffs_clear_inode(struct inode *); | |
| 141 | |
| 78 | 142 static int yaffs_readpage(struct file *file, struct page *page); |
| 25 | 143 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0)) |
| 144 static int yaffs_writepage(struct page *page, struct writeback_control *wbc); | |
| 145 #else | |
| 7 | 146 static int yaffs_writepage(struct page *page); |
| 25 | 147 #endif |
| 78 | 148 static int yaffs_prepare_write(struct file *f, struct page *pg, |
| 149 unsigned offset, unsigned to); | |
| 150 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset, | |
| 151 unsigned to); | |
| 7 | 152 |
| 78 | 153 static int yaffs_readlink(struct dentry *dentry, char __user * buffer, |
| 154 int buflen); | |
| 102 | 155 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13)) |
| 156 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd); | |
| 157 #else | |
| 7 | 158 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd); |
| 102 | 159 #endif |
| 7 | 160 |
| 161 static struct address_space_operations yaffs_file_address_operations = { | |
| 78 | 162 .readpage = yaffs_readpage, |
| 163 .writepage = yaffs_writepage, | |
| 164 .prepare_write = yaffs_prepare_write, | |
| 165 .commit_write = yaffs_commit_write, | |
| 7 | 166 }; |
| 167 | |
| 168 static struct file_operations yaffs_file_operations = { | |
| 78 | 169 .read = generic_file_read, |
| 170 .write = generic_file_write, | |
| 171 .mmap = generic_file_mmap, | |
| 172 .flush = yaffs_file_flush, | |
| 173 .fsync = yaffs_sync_object, | |
| 7 | 174 }; |
| 175 | |
| 176 static struct inode_operations yaffs_file_inode_operations = { | |
| 78 | 177 .setattr = yaffs_setattr, |
| 7 | 178 }; |
| 179 | |
| 78 | 180 static struct inode_operations yaffs_symlink_inode_operations = { |
| 181 .readlink = yaffs_readlink, | |
| 182 .follow_link = yaffs_follow_link, | |
| 183 .setattr = yaffs_setattr, | |
| 7 | 184 }; |
| 185 | |
| 186 static struct inode_operations yaffs_dir_inode_operations = { | |
| 78 | 187 .create = yaffs_create, |
| 188 .lookup = yaffs_lookup, | |
| 189 .link = yaffs_link, | |
| 190 .unlink = yaffs_unlink, | |
| 191 .symlink = yaffs_symlink, | |
| 192 .mkdir = yaffs_mkdir, | |
| 193 .rmdir = yaffs_unlink, | |
| 194 .mknod = yaffs_mknod, | |
| 195 .rename = yaffs_rename, | |
| 196 .setattr = yaffs_setattr, | |
| 7 | 197 }; |
| 198 | |
| 199 static struct file_operations yaffs_dir_operations = { | |
| 78 | 200 .read = generic_read_dir, |
| 201 .readdir = yaffs_readdir, | |
| 202 .fsync = yaffs_sync_object, | |
| 7 | 203 }; |
| 204 | |
| 205 static struct super_operations yaffs_super_ops = { | |
| 78 | 206 .statfs = yaffs_statfs, |
| 207 .read_inode = yaffs_read_inode, | |
| 208 .put_inode = yaffs_put_inode, | |
| 209 .put_super = yaffs_put_super, | |
| 210 .delete_inode = yaffs_delete_inode, | |
| 211 .clear_inode = yaffs_clear_inode, | |
| 7 | 212 }; |
| 213 | |
| 78 | 214 static void yaffs_GrossLock(yaffs_Device * dev) |
| 7 | 215 { |
| 78 | 216 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs locking\n")); |
| 7 | 217 |
| 218 down(&dev->grossLock); | |
| 219 } | |
| 220 | |
| 78 | 221 static void yaffs_GrossUnlock(yaffs_Device * dev) |
| 7 | 222 { |
| 78 | 223 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs unlocking\n")); |
| 7 | 224 up(&dev->grossLock); |
| 225 | |
| 226 } | |
| 227 | |
| 78 | 228 static int yaffs_readlink(struct dentry *dentry, char __user * buffer, |
| 229 int buflen) | |
| 7 | 230 { |
| 231 unsigned char *alias; | |
| 232 int ret; | |
| 233 | |
| 234 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev; | |
| 235 | |
| 78 | 236 yaffs_GrossLock(dev); |
| 7 | 237 |
| 238 alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry)); | |
| 78 | 239 |
| 7 | 240 yaffs_GrossUnlock(dev); |
| 78 | 241 |
| 242 if (!alias) | |
| 7 | 243 return -ENOMEM; |
| 244 | |
| 245 ret = vfs_readlink(dentry, buffer, buflen, alias); | |
| 246 kfree(alias); | |
| 247 return ret; | |
| 248 } | |
| 249 | |
| 102 | 250 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13)) |
| 251 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd) | |
| 252 #else | |
| 7 | 253 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd) |
| 102 | 254 #endif |
| 7 | 255 { |
| 256 unsigned char *alias; | |
| 257 int ret; | |
| 258 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev; | |
| 259 | |
| 260 yaffs_GrossLock(dev); | |
| 261 | |
| 262 alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry)); | |
| 78 | 263 |
| 7 | 264 yaffs_GrossUnlock(dev); |
| 78 | 265 |
| 266 if (!alias) | |
| 102 | 267 { |
| 268 ret = -ENOMEM; | |
| 269 goto out; | |
| 270 } | |
| 7 | 271 |
| 78 | 272 ret = vfs_follow_link(nd, alias); |
| 7 | 273 kfree(alias); |
| 102 | 274 out: |
| 275 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13)) | |
| 276 return ERR_PTR (ret); | |
| 277 #else | |
| 7 | 278 return ret; |
| 102 | 279 #endif |
| 7 | 280 } |
| 281 | |
| 78 | 282 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev, |
| 283 yaffs_Object * obj); | |
| 7 | 284 |
| 285 /* | |
| 286 * Lookup is used to find objects in the fs | |
| 287 */ | |
| 288 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0)) | |
| 289 | |
| 78 | 290 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry, |
| 291 struct nameidata *n) | |
| 7 | 292 #else |
| 78 | 293 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry) |
| 7 | 294 #endif |
| 295 { | |
| 296 yaffs_Object *obj; | |
| 78 | 297 struct inode *inode = NULL; /* NCB 2.5/2.6 needs NULL here */ |
| 298 | |
| 7 | 299 yaffs_Device *dev = yaffs_InodeToObject(dir)->myDev; |
| 300 | |
| 301 yaffs_GrossLock(dev); | |
| 302 | |
| 78 | 303 T(YAFFS_TRACE_OS, |
| 304 (KERN_DEBUG "yaffs_lookup for %d:%s\n", | |
| 305 yaffs_InodeToObject(dir)->objectId, dentry->d_name.name)); | |
| 306 | |
| 307 obj = | |
| 308 yaffs_FindObjectByName(yaffs_InodeToObject(dir), | |
| 309 dentry->d_name.name); | |
| 310 | |
| 311 obj = yaffs_GetEquivalentObject(obj); /* in case it was a hardlink */ | |
| 7 | 312 |
| 78 | 313 if (obj) { |
| 314 T(YAFFS_TRACE_OS, | |
| 315 (KERN_DEBUG "yaffs_lookup found %d\n", obj->objectId)); | |
| 316 | |
| 317 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj); | |
| 318 | |
| 319 if (inode) { | |
| 320 T(YAFFS_TRACE_OS, | |
| 321 (KERN_DEBUG "yaffs_loookup dentry \n")); | |
| 322 /* #if 0 asserted by NCB for 2.5/6 compatability - falls through to | |
| 323 * d_add even if NULL inode */ | |
| 7 | 324 #if 0 |
| 78 | 325 /*dget(dentry); // try to solve directory bug */ |
| 326 d_add(dentry, inode); | |
| 327 | |
| 7 | 328 yaffs_GrossUnlock(dev); |
| 329 | |
| 78 | 330 /* return dentry; */ |
| 7 | 331 return NULL; |
| 332 #endif | |
| 333 } | |
| 334 | |
| 78 | 335 } else { |
| 336 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_lookup not found\n")); | |
| 337 | |
| 7 | 338 } |
| 339 yaffs_GrossUnlock(dev); | |
| 340 | |
| 78 | 341 /* added NCB for 2.5/6 compatability - forces add even if inode is |
| 342 * NULL which creates dentry hash */ | |
| 343 d_add(dentry, inode); | |
| 344 | |
| 7 | 345 return NULL; |
| 78 | 346 /* return (ERR_PTR(-EIO)); */ |
| 347 | |
| 7 | 348 } |
| 349 | |
| 78 | 350 /* For now put inode is just for debugging |
| 351 * Put inode is called when the inode **structure** is put. | |
| 352 */ | |
| 7 | 353 static void yaffs_put_inode(struct inode *inode) |
| 354 { | |
| 78 | 355 T(YAFFS_TRACE_OS, |
| 356 ("yaffs_put_inode: ino %d, count %d\n", (int)inode->i_ino, | |
| 357 atomic_read(&inode->i_count))); | |
| 358 | |
| 7 | 359 } |
| 360 | |
| 78 | 361 /* clear is called to tell the fs to release any per-inode data it holds */ |
| 7 | 362 static void yaffs_clear_inode(struct inode *inode) |
| 363 { | |
| 364 yaffs_Object *obj; | |
| 365 yaffs_Device *dev; | |
| 78 | 366 |
| 7 | 367 obj = yaffs_InodeToObject(inode); |
| 368 | |
| 78 | 369 T(YAFFS_TRACE_OS, |
| 370 ("yaffs_clear_inode: ino %d, count %d %s\n", (int)inode->i_ino, | |
| 371 atomic_read(&inode->i_count), | |
| 372 obj ? "object exists" : "null object")); | |
| 373 | |
| 374 if (obj) { | |
| 7 | 375 dev = obj->myDev; |
| 376 yaffs_GrossLock(dev); | |
| 78 | 377 |
| 378 /* Clear the association between the inode and | |
| 379 * the yaffs_Object. | |
| 380 */ | |
| 7 | 381 obj->myInode = NULL; |
| 382 inode->u.generic_ip = NULL; | |
| 78 | 383 |
| 384 /* If the object freeing was deferred, then the real | |
| 385 * free happens now. | |
| 386 * This should fix the inode inconsistency problem. | |
| 387 */ | |
| 388 | |
| 7 | 389 yaffs_HandleDeferedFree(obj); |
| 78 | 390 |
| 7 | 391 yaffs_GrossUnlock(dev); |
| 392 } | |
| 78 | 393 |
| 7 | 394 } |
| 395 | |
| 78 | 396 /* delete is called when the link count is zero and the inode |
| 397 * is put (ie. nobody wants to know about it anymore, time to | |
| 398 * delete the file). | |
| 399 * NB Must call clear_inode() | |
| 400 */ | |
| 7 | 401 static void yaffs_delete_inode(struct inode *inode) |
| 402 { | |
| 403 yaffs_Object *obj = yaffs_InodeToObject(inode); | |
| 404 yaffs_Device *dev; | |
| 405 | |
| 78 | 406 T(YAFFS_TRACE_OS, |
| 407 ("yaffs_delete_inode: ino %d, count %d %s\n", (int)inode->i_ino, | |
| 408 atomic_read(&inode->i_count), | |
| 409 obj ? "object exists" : "null object")); | |
| 410 | |
| 411 if (obj) { | |
| 412 dev = obj->myDev; | |
| 7 | 413 yaffs_GrossLock(dev); |
| 414 yaffs_DeleteFile(obj); | |
| 415 yaffs_GrossUnlock(dev); | |
| 416 } | |
| 102 | 417 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13)) |
| 418 truncate_inode_pages (&inode->i_data, 0); | |
| 419 #endif | |
| 7 | 420 clear_inode(inode); |
| 421 } | |
| 422 | |
| 78 | 423 static int yaffs_file_flush(struct file *file) |
| 7 | 424 { |
| 425 yaffs_Object *obj = yaffs_DentryToObject(file->f_dentry); | |
| 78 | 426 |
| 7 | 427 yaffs_Device *dev = obj->myDev; |
| 78 | 428 |
| 429 T(YAFFS_TRACE_OS, | |
| 430 (KERN_DEBUG "yaffs_file_flush object %d (%s)\n", obj->objectId, | |
| 431 obj->dirty ? "dirty" : "clean")); | |
| 7 | 432 |
| 433 yaffs_GrossLock(dev); | |
| 78 | 434 |
| 435 yaffs_FlushFile(obj, 1); | |
| 7 | 436 |
| 437 yaffs_GrossUnlock(dev); | |
| 438 | |
| 78 | 439 return 0; |
| 7 | 440 } |
| 441 | |
| 78 | 442 static int yaffs_readpage_nolock(struct file *f, struct page *pg) |
| 443 { | |
| 444 /* Lifted from jffs2 */ | |
| 7 | 445 |
| 446 yaffs_Object *obj; | |
| 447 unsigned char *pg_buf; | |
| 448 int ret; | |
| 449 | |
| 450 yaffs_Device *dev; | |
| 451 | |
| 78 | 452 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_readpage at %08x, size %08x\n", |
| 453 (unsigned)(pg->index << PAGE_CACHE_SHIFT), | |
| 454 (unsigned)PAGE_CACHE_SIZE)); | |
| 7 | 455 |
| 78 | 456 obj = yaffs_DentryToObject(f->f_dentry); |
| 7 | 457 |
| 458 dev = obj->myDev; | |
| 78 | 459 |
| 25 | 460 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0)) |
| 78 | 461 BUG_ON(!PageLocked(pg)); |
| 25 | 462 #else |
| 7 | 463 if (!PageLocked(pg)) |
| 78 | 464 PAGE_BUG(pg); |
| 25 | 465 #endif |
| 7 | 466 |
| 467 pg_buf = kmap(pg); | |
| 468 /* FIXME: Can kmap fail? */ | |
| 469 | |
| 470 yaffs_GrossLock(dev); | |
| 78 | 471 |
| 472 ret = | |
| 473 yaffs_ReadDataFromFile(obj, pg_buf, pg->index << PAGE_CACHE_SHIFT, | |
| 474 PAGE_CACHE_SIZE); | |
| 7 | 475 |
| 476 yaffs_GrossUnlock(dev); | |
| 78 | 477 |
| 478 if (ret >= 0) | |
| 479 ret = 0; | |
| 7 | 480 |
| 481 if (ret) { | |
| 482 ClearPageUptodate(pg); | |
| 483 SetPageError(pg); | |
| 484 } else { | |
| 485 SetPageUptodate(pg); | |
| 486 ClearPageError(pg); | |
| 487 } | |
| 488 | |
| 489 flush_dcache_page(pg); | |
| 490 kunmap(pg); | |
| 491 | |
| 78 | 492 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_readpage done\n")); |
| 7 | 493 return ret; |
| 494 } | |
| 495 | |
| 496 static int yaffs_readpage_unlock(struct file *f, struct page *pg) | |
| 497 { | |
| 78 | 498 int ret = yaffs_readpage_nolock(f, pg); |
| 7 | 499 UnlockPage(pg); |
| 500 return ret; | |
| 501 } | |
| 502 | |
| 78 | 503 static int yaffs_readpage(struct file *f, struct page *pg) |
| 7 | 504 { |
| 78 | 505 return yaffs_readpage_unlock(f, pg); |
| 7 | 506 } |
| 507 | |
| 78 | 508 /* writepage inspired by/stolen from smbfs */ |
| 7 | 509 |
| 25 | 510 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0)) |
| 511 static int yaffs_writepage(struct page *page, struct writeback_control *wbc) | |
| 512 #else | |
| 7 | 513 static int yaffs_writepage(struct page *page) |
| 25 | 514 #endif |
| 7 | 515 { |
| 516 struct address_space *mapping = page->mapping; | |
| 78 | 517 loff_t offset = (loff_t) page->index << PAGE_CACHE_SHIFT; |
| 7 | 518 struct inode *inode; |
| 519 unsigned long end_index; | |
| 520 char *buffer; | |
| 521 yaffs_Object *obj; | |
| 522 int nWritten = 0; | |
| 523 unsigned nBytes; | |
| 524 | |
| 525 if (!mapping) | |
| 526 BUG(); | |
| 527 inode = mapping->host; | |
| 528 if (!inode) | |
| 529 BUG(); | |
| 530 | |
| 78 | 531 if (offset > inode->i_size) { |
| 532 T(YAFFS_TRACE_OS, | |
| 533 (KERN_DEBUG | |
| 534 "yaffs_writepage at %08x, inode size = %08x!!!\n", | |
| 535 (unsigned)(page->index << PAGE_CACHE_SHIFT), | |
| 536 (unsigned)inode->i_size)); | |
| 537 T(YAFFS_TRACE_OS, | |
| 538 (KERN_DEBUG " -> don't care!!\n")); | |
| 69 | 539 unlock_page(page); |
| 540 return 0; | |
| 541 } | |
| 542 | |
| 7 | 543 end_index = inode->i_size >> PAGE_CACHE_SHIFT; |
| 544 | |
| 545 /* easy case */ | |
| 78 | 546 if (page->index < end_index) { |
| 7 | 547 nBytes = PAGE_CACHE_SIZE; |
| 78 | 548 } else { |
| 549 nBytes = inode->i_size & (PAGE_CACHE_SIZE - 1); | |
| 7 | 550 } |
| 551 | |
| 552 get_page(page); | |
| 553 | |
| 554 buffer = kmap(page); | |
| 555 | |
| 556 obj = yaffs_InodeToObject(inode); | |
| 557 yaffs_GrossLock(obj->myDev); | |
| 558 | |
| 78 | 559 T(YAFFS_TRACE_OS, |
| 560 (KERN_DEBUG "yaffs_writepage at %08x, size %08x\n", | |
| 561 (unsigned)(page->index << PAGE_CACHE_SHIFT), nBytes)); | |
| 562 T(YAFFS_TRACE_OS, | |
| 563 (KERN_DEBUG "writepag0: obj = %05x, ino = %05x\n", | |
| 564 (int)obj->variant.fileVariant.fileSize, (int)inode->i_size)); | |
| 7 | 565 |
| 78 | 566 nWritten = |
| 567 yaffs_WriteDataToFile(obj, buffer, page->index << PAGE_CACHE_SHIFT, | |
| 568 nBytes, 0); | |
| 7 | 569 |
| 78 | 570 T(YAFFS_TRACE_OS, |
| 571 (KERN_DEBUG "writepag1: obj = %05x, ino = %05x\n", | |
| 572 (int)obj->variant.fileVariant.fileSize, (int)inode->i_size)); | |
| 69 | 573 |
| 7 | 574 yaffs_GrossUnlock(obj->myDev); |
| 78 | 575 |
| 7 | 576 kunmap(page); |
| 577 SetPageUptodate(page); | |
| 578 UnlockPage(page); | |
| 579 put_page(page); | |
| 580 | |
| 78 | 581 return (nWritten == nBytes) ? 0 : -ENOSPC; |
| 7 | 582 } |
| 583 | |
| 78 | 584 static int yaffs_prepare_write(struct file *f, struct page *pg, |
| 585 unsigned offset, unsigned to) | |
| 7 | 586 { |
| 587 | |
| 78 | 588 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_prepair_write\n")); |
| 589 if (!Page_Uptodate(pg) && (offset || to < PAGE_CACHE_SIZE)) | |
| 590 return yaffs_readpage_nolock(f, pg); | |
| 7 | 591 |
| 592 return 0; | |
| 78 | 593 |
| 7 | 594 } |
| 595 | |
| 78 | 596 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset, |
| 597 unsigned to) | |
| 7 | 598 { |
| 599 | |
| 600 void *addr = page_address(pg) + offset; | |
| 78 | 601 loff_t pos = (((loff_t) pg->index) << PAGE_CACHE_SHIFT) + offset; |
| 7 | 602 int nBytes = to - offset; |
| 603 int nWritten; | |
| 78 | 604 |
| 7 | 605 unsigned spos = pos; |
| 606 unsigned saddr = (unsigned)addr; | |
| 607 | |
| 78 | 608 T(YAFFS_TRACE_OS, |
| 609 (KERN_DEBUG "yaffs_commit_write addr %x pos %x nBytes %d\n", saddr, | |
| 610 spos, nBytes)); | |
| 611 | |
| 612 nWritten = yaffs_file_write(f, addr, nBytes, &pos); | |
| 613 | |
| 614 if (nWritten != nBytes) { | |
| 615 T(YAFFS_TRACE_OS, | |
| 616 (KERN_DEBUG | |
| 617 "yaffs_commit_write not same size nWritten %d nBytes %d\n", | |
| 618 nWritten, nBytes)); | |
| 7 | 619 SetPageError(pg); |
| 620 ClearPageUptodate(pg); | |
| 78 | 621 } else { |
| 7 | 622 SetPageUptodate(pg); |
| 623 } | |
| 624 | |
| 78 | 625 T(YAFFS_TRACE_OS, |
| 97 | 626 (KERN_DEBUG "yaffs_commit_write returning %d\n", |
| 627 nWritten == nBytes ? 0 : -1)); | |
| 78 | 628 |
| 97 | 629 return nWritten == nBytes ? 0 : -1; |
| 7 | 630 |
| 631 } | |
| 632 | |
| 78 | 633 static void yaffs_FillInodeFromObject(struct inode *inode, yaffs_Object * obj) |
| 7 | 634 { |
| 78 | 635 if (inode && obj) { |
| 7 | 636 inode->i_ino = obj->objectId; |
|
24
4ebabd859f38
Some improvements to garbage collection and st_xxx to yst_xxx changes
charles
parents:
21
diff
changeset
|
637 inode->i_mode = obj->yst_mode; |
|
4ebabd859f38
Some improvements to garbage collection and st_xxx to yst_xxx changes
charles
parents:
21
diff
changeset
|
638 inode->i_uid = obj->yst_uid; |
|
4ebabd859f38
Some improvements to garbage collection and st_xxx to yst_xxx changes
charles
parents:
21
diff
changeset
|
639 inode->i_gid = obj->yst_gid; |
| 7 | 640 inode->i_blksize = inode->i_sb->s_blocksize; |
| 641 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0)) | |
| 642 | |
|
24
4ebabd859f38
Some improvements to garbage collection and st_xxx to yst_xxx changes
charles
parents:
21
diff
changeset
|
643 inode->i_rdev = old_decode_dev(obj->yst_rdev); |
| 78 | 644 inode->i_atime.tv_sec = (time_t) (obj->yst_atime); |
| 7 | 645 inode->i_atime.tv_nsec = 0; |
| 78 | 646 inode->i_mtime.tv_sec = (time_t) obj->yst_mtime; |
| 647 inode->i_mtime.tv_nsec = 0; | |
| 648 inode->i_ctime.tv_sec = (time_t) obj->yst_ctime; | |
| 7 | 649 inode->i_ctime.tv_nsec = 0; |
| 650 #else | |
|
24
4ebabd859f38
Some improvements to garbage collection and st_xxx to yst_xxx changes
charles
parents:
21
diff
changeset
|
651 inode->i_rdev = obj->yst_rdev; |
|
4ebabd859f38
Some improvements to garbage collection and st_xxx to yst_xxx changes
charles
parents:
21
diff
changeset
|
652 inode->i_atime = obj->yst_atime; |
|
4ebabd859f38
Some improvements to garbage collection and st_xxx to yst_xxx changes
charles
parents:
21
diff
changeset
|
653 inode->i_mtime = obj->yst_mtime; |
|
4ebabd859f38
Some improvements to garbage collection and st_xxx to yst_xxx changes
charles
parents:
21
diff
changeset
|
654 inode->i_ctime = obj->yst_ctime; |
| 7 | 655 #endif |
| 656 inode->i_size = yaffs_GetObjectFileLength(obj); | |
| 657 inode->i_blocks = (inode->i_size + 511) >> 9; | |
| 658 | |
| 659 inode->i_nlink = yaffs_GetObjectLinkCount(obj); | |
| 78 | 660 |
| 661 T(YAFFS_TRACE_OS, | |
| 662 (KERN_DEBUG | |
| 663 "yaffs_FillInode mode %x uid %d gid %d size %d count %d\n", | |
| 664 inode->i_mode, inode->i_uid, inode->i_gid, | |
| 665 (int)inode->i_size, atomic_read(&inode->i_count))); | |
| 666 | |
| 667 switch (obj->yst_mode & S_IFMT) { | |
| 668 default: /* fifo, device or socket */ | |
| 13 | 669 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0)) |
| 78 | 670 init_special_inode(inode, obj->yst_mode, |
| 671 old_decode_dev(obj->yst_rdev)); | |
| 13 | 672 #else |
| 78 | 673 init_special_inode(inode, obj->yst_mode, |
| 674 (dev_t) (obj->yst_rdev)); | |
| 675 #endif | |
| 676 break; | |
| 677 case S_IFREG: /* file */ | |
| 678 inode->i_op = &yaffs_file_inode_operations; | |
| 679 inode->i_fop = &yaffs_file_operations; | |
| 680 inode->i_mapping->a_ops = | |
| 681 &yaffs_file_address_operations; | |
| 682 break; | |
| 683 case S_IFDIR: /* directory */ | |
| 684 inode->i_op = &yaffs_dir_inode_operations; | |
| 685 inode->i_fop = &yaffs_dir_operations; | |
| 686 break; | |
| 687 case S_IFLNK: /* symlink */ | |
| 688 inode->i_op = &yaffs_symlink_inode_operations; | |
| 689 break; | |
| 7 | 690 } |
| 78 | 691 |
| 7 | 692 inode->u.generic_ip = obj; |
| 693 obj->myInode = inode; | |
| 78 | 694 |
| 695 } else { | |
| 696 T(YAFFS_TRACE_OS, | |
| 697 (KERN_DEBUG "yaffs_FileInode invalid parameters\n")); | |
| 7 | 698 } |
| 699 | |
| 700 } | |
| 701 | |
| 78 | 702 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev, |
| 703 yaffs_Object * obj) | |
| 7 | 704 { |
| 78 | 705 struct inode *inode; |
| 706 | |
| 707 if (!sb) { | |
| 708 T(YAFFS_TRACE_OS, | |
| 709 (KERN_DEBUG "yaffs_get_inode for NULL super_block!!\n")); | |
| 710 return NULL; | |
| 711 | |
| 7 | 712 } |
| 78 | 713 |
| 714 if (!obj) { | |
| 715 T(YAFFS_TRACE_OS, | |
| 716 (KERN_DEBUG "yaffs_get_inode for NULL object!!\n")); | |
| 717 return NULL; | |
| 718 | |
| 7 | 719 } |
| 78 | 720 |
| 721 T(YAFFS_TRACE_OS, | |
| 722 (KERN_DEBUG "yaffs_get_inode for object %d\n", obj->objectId)); | |
| 7 | 723 |
| 78 | 724 inode = iget(sb, obj->objectId); |
| 7 | 725 |
| 78 | 726 /* NB Side effect: iget calls back to yaffs_read_inode(). */ |
| 727 /* iget also increments the inode's i_count */ | |
| 728 | |
| 7 | 729 return inode; |
| 730 } | |
| 731 | |
| 78 | 732 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n, |
| 733 loff_t * pos) | |
| 7 | 734 { |
| 735 yaffs_Object *obj; | |
| 78 | 736 int nWritten, ipos; |
| 7 | 737 struct inode *inode; |
| 738 yaffs_Device *dev; | |
| 78 | 739 |
| 740 obj = yaffs_DentryToObject(f->f_dentry); | |
| 741 | |
| 7 | 742 dev = obj->myDev; |
| 78 | 743 |
| 7 | 744 yaffs_GrossLock(dev); |
| 745 | |
| 746 inode = f->f_dentry->d_inode; | |
| 747 | |
| 78 | 748 if (!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND) { |
| 7 | 749 ipos = inode->i_size; |
| 78 | 750 } else { |
| 7 | 751 ipos = *pos; |
| 752 } | |
| 78 | 753 |
| 754 if (!obj) { | |
| 755 T(YAFFS_TRACE_OS, | |
| 756 (KERN_DEBUG "yaffs_file_write: hey obj is null!\n")); | |
| 757 } else { | |
| 758 T(YAFFS_TRACE_OS, | |
| 759 (KERN_DEBUG | |
| 760 "yaffs_file_write about to write writing %d bytes" | |
| 761 "to object %d at %d\n", | |
| 762 n, obj->objectId, ipos)); | |
| 7 | 763 } |
| 764 | |
| 78 | 765 nWritten = yaffs_WriteDataToFile(obj, buf, ipos, n, 0); |
| 7 | 766 |
| 78 | 767 T(YAFFS_TRACE_OS, |
| 768 (KERN_DEBUG "yaffs_file_write writing %d bytes, %d written at %d\n", | |
| 769 n, nWritten, ipos)); | |
| 770 if (nWritten > 0) { | |
| 7 | 771 ipos += nWritten; |
| 772 *pos = ipos; | |
| 78 | 773 if (ipos > inode->i_size) { |
| 7 | 774 inode->i_size = ipos; |
| 78 | 775 inode->i_blocks = (ipos + 511) >> 9; |
| 776 | |
| 777 T(YAFFS_TRACE_OS, | |
| 778 (KERN_DEBUG | |
| 779 "yaffs_file_write size updated to %d bytes, " | |
| 780 "%d blocks\n", | |
| 781 ipos, (int)(inode->i_blocks))); | |
| 7 | 782 } |
| 78 | 783 |
| 7 | 784 } |
| 785 yaffs_GrossUnlock(dev); | |
| 78 | 786 |
| 7 | 787 return nWritten != n ? -ENOSPC : nWritten; |
| 788 } | |
| 789 | |
| 790 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir) | |
| 791 { | |
| 792 yaffs_Object *obj; | |
| 793 yaffs_Device *dev; | |
| 794 struct inode *inode = f->f_dentry->d_inode; | |
| 795 unsigned long offset, curoffs; | |
| 78 | 796 struct list_head *i; |
| 7 | 797 yaffs_Object *l; |
| 78 | 798 |
| 799 char name[YAFFS_MAX_NAME_LENGTH + 1]; | |
| 800 | |
| 801 obj = yaffs_DentryToObject(f->f_dentry); | |
| 7 | 802 dev = obj->myDev; |
| 78 | 803 |
| 7 | 804 yaffs_GrossLock(dev); |
| 78 | 805 |
| 7 | 806 offset = f->f_pos; |
| 78 | 807 |
| 808 T(YAFFS_TRACE_OS, ("yaffs_readdir: starting at %d\n", (int)offset)); | |
| 809 | |
| 810 if (offset == 0) { | |
| 811 T(YAFFS_TRACE_OS, | |
| 812 (KERN_DEBUG "yaffs_readdir: entry . ino %d \n", | |
| 813 (int)inode->i_ino)); | |
| 814 if (filldir(dirent, ".", 1, offset, inode->i_ino, DT_DIR) | |
| 815 < 0) { | |
| 7 | 816 goto out; |
| 817 } | |
| 818 offset++; | |
| 819 f->f_pos++; | |
| 820 } | |
| 78 | 821 if (offset == 1) { |
| 822 T(YAFFS_TRACE_OS, | |
| 823 (KERN_DEBUG "yaffs_readdir: entry .. ino %d \n", | |
| 824 (int)f->f_dentry->d_parent->d_inode->i_ino)); | |
| 825 if (filldir | |
| 826 (dirent, "..", 2, offset, | |
| 827 f->f_dentry->d_parent->d_inode->i_ino, DT_DIR) < 0) { | |
| 7 | 828 goto out; |
| 829 } | |
| 830 offset++; | |
| 831 f->f_pos++; | |
| 832 } | |
| 78 | 833 |
| 7 | 834 curoffs = 1; |
| 78 | 835 |
| 836 list_for_each(i, &obj->variant.directoryVariant.children) { | |
| 7 | 837 curoffs++; |
| 78 | 838 if (curoffs >= offset) { |
| 839 l = list_entry(i, yaffs_Object, siblings); | |
| 840 | |
| 841 yaffs_GetObjectName(l, name, | |
| 842 YAFFS_MAX_NAME_LENGTH + 1); | |
| 843 T(YAFFS_TRACE_OS, | |
| 844 (KERN_DEBUG "yaffs_readdir: %s inode %d\n", name, | |
| 845 yaffs_GetObjectInode(l))); | |
| 846 | |
| 847 if (filldir(dirent, | |
| 848 name, | |
| 849 strlen(name), | |
| 850 offset, | |
| 851 yaffs_GetObjectInode(l), | |
| 852 yaffs_GetObjectType(l)) | |
| 853 < 0) { | |
| 7 | 854 goto up_and_out; |
| 855 } | |
| 78 | 856 |
| 7 | 857 offset++; |
| 78 | 858 f->f_pos++; |
| 7 | 859 } |
| 860 } | |
| 861 | |
| 78 | 862 up_and_out: |
| 863 out: | |
| 864 | |
| 865 yaffs_GrossUnlock(dev); | |
| 866 | |
| 7 | 867 return 0; |
| 868 } | |
| 869 | |
| 870 /* | |
| 871 * File creation. Allocate an inode, and we're done.. | |
| 872 */ | |
| 873 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0)) | |
| 78 | 874 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode, |
| 875 dev_t rdev) | |
| 7 | 876 #else |
| 78 | 877 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode, |
| 878 int rdev) | |
| 7 | 879 #endif |
| 880 { | |
| 881 struct inode *inode; | |
| 78 | 882 |
| 7 | 883 yaffs_Object *obj = NULL; |
| 884 yaffs_Device *dev; | |
| 78 | 885 |
| 7 | 886 yaffs_Object *parent = yaffs_InodeToObject(dir); |
| 78 | 887 |
| 7 | 888 int error = -ENOSPC; |
|
38
14315c63b7a5
Set the correct uid/gid of newly created files when: use fsuid & fsgid and take care when the SGID bit is set on the parent directory.
luc
parents:
34
diff
changeset
|
889 uid_t uid = current->fsuid; |
|
14315c63b7a5
Set the correct uid/gid of newly created files when: use fsuid & fsgid and take care when the SGID bit is set on the parent directory.
luc
parents:
34
diff
changeset
|
890 gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : current->fsgid; |
| 7 | 891 |
| 78 | 892 if (parent) { |
| 893 T(YAFFS_TRACE_OS, | |
| 894 (KERN_DEBUG "yaffs_mknod: parent object %d type %d\n", | |
| 895 parent->objectId, parent->variantType)); | |
| 896 } else { | |
| 897 T(YAFFS_TRACE_OS, | |
| 898 (KERN_DEBUG "yaffs_mknod: could not get parent object\n")); | |
| 7 | 899 return -EPERM; |
| 900 } | |
| 78 | 901 |
| 902 T(YAFFS_TRACE_OS, ("yaffs_mknod: making oject for %s, " | |
| 903 "mode %x dev %x\n", | |
| 904 dentry->d_name.name, mode, rdev)); | |
| 7 | 905 |
| 906 dev = parent->myDev; | |
| 78 | 907 |
| 7 | 908 yaffs_GrossLock(dev); |
| 909 | |
| 78 | 910 switch (mode & S_IFMT) { |
| 911 default: | |
| 912 /* Special (socket, fifo, device...) */ | |
| 913 T(YAFFS_TRACE_OS, (KERN_DEBUG | |
| 914 "yaffs_mknod: making special\n")); | |
| 13 | 915 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0)) |
| 78 | 916 obj = |
| 917 yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid, | |
| 918 gid, old_encode_dev(rdev)); | |
| 13 | 919 #else |
| 78 | 920 obj = |
| 921 yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid, | |
| 922 gid, rdev); | |
| 923 #endif | |
| 924 break; | |
| 925 case S_IFREG: /* file */ | |
| 926 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_mknod: making file\n")); | |
| 927 obj = | |
| 928 yaffs_MknodFile(parent, dentry->d_name.name, mode, uid, | |
| 929 gid); | |
| 930 break; | |
| 931 case S_IFDIR: /* directory */ | |
| 932 T(YAFFS_TRACE_OS, | |
| 933 (KERN_DEBUG "yaffs_mknod: making directory\n")); | |
| 934 obj = | |
| 935 yaffs_MknodDirectory(parent, dentry->d_name.name, mode, | |
| 936 uid, gid); | |
| 937 break; | |
| 938 case S_IFLNK: /* symlink */ | |
| 939 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_mknod: making file\n")); | |
| 940 obj = NULL; /* Do we ever get here? */ | |
| 941 break; | |
| 7 | 942 } |
| 78 | 943 |
| 944 if (obj) { | |
| 7 | 945 inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj); |
| 946 d_instantiate(dentry, inode); | |
| 78 | 947 T(YAFFS_TRACE_OS, |
| 948 (KERN_DEBUG "yaffs_mknod created object %d count = %d\n", | |
| 949 obj->objectId, atomic_read(&inode->i_count))); | |
| 7 | 950 error = 0; |
| 78 | 951 } else { |
| 952 T(YAFFS_TRACE_OS, | |
| 953 (KERN_DEBUG "yaffs_mknod failed making object\n")); | |
| 7 | 954 error = -ENOMEM; |
| 955 } | |
| 956 | |
| 957 yaffs_GrossUnlock(dev); | |
| 958 | |
| 959 return error; | |
| 960 } | |
| 961 | |
| 78 | 962 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode) |
| 7 | 963 { |
| 964 int retVal; | |
| 78 | 965 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_mkdir\n")); |
| 966 retVal = yaffs_mknod(dir, dentry, mode | S_IFDIR, 0); | |
| 7 | 967 #if 0 |
| 78 | 968 /* attempt to fix dir bug - didn't work */ |
| 969 if (!retVal) { | |
| 7 | 970 dget(dentry); |
| 971 } | |
| 972 #endif | |
| 973 return retVal; | |
| 974 } | |
| 975 | |
| 976 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0)) | |
| 78 | 977 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode, |
| 978 struct nameidata *n) | |
| 7 | 979 #else |
| 980 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode) | |
| 981 #endif | |
| 982 { | |
| 78 | 983 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_create\n")); |
| 7 | 984 return yaffs_mknod(dir, dentry, mode | S_IFREG, 0); |
| 985 } | |
| 986 | |
| 78 | 987 static int yaffs_unlink(struct inode *dir, struct dentry *dentry) |
| 7 | 988 { |
| 989 int retVal; | |
| 78 | 990 |
| 7 | 991 yaffs_Device *dev; |
| 78 | 992 |
| 993 T(YAFFS_TRACE_OS, | |
| 994 (KERN_DEBUG "yaffs_unlink %d:%s\n", (int)(dir->i_ino), | |
| 995 dentry->d_name.name)); | |
| 996 | |
| 7 | 997 dev = yaffs_InodeToObject(dir)->myDev; |
| 78 | 998 |
| 7 | 999 yaffs_GrossLock(dev); |
| 78 | 1000 |
| 1001 retVal = yaffs_Unlink(yaffs_InodeToObject(dir), dentry->d_name.name); | |
| 1002 | |
| 7 | 1003 yaffs_GrossUnlock(dev); |
| 78 | 1004 |
| 1005 if (retVal == YAFFS_OK) { | |
| 7 | 1006 dentry->d_inode->i_nlink--; |
| 1007 mark_inode_dirty(dentry->d_inode); | |
| 1008 return 0; | |
| 78 | 1009 } else { |
| 7 | 1010 return -ENOTEMPTY; |
| 1011 } | |
| 1012 } | |
| 1013 | |
| 1014 /* | |
| 1015 * Create a link... | |
| 1016 */ | |
| 78 | 1017 static int yaffs_link(struct dentry *old_dentry, struct inode *dir, |
| 1018 struct dentry *dentry) | |
| 7 | 1019 { |
| 1020 struct inode *inode = old_dentry->d_inode; | |
| 1021 yaffs_Object *obj = NULL; | |
| 78 | 1022 yaffs_Object *link = NULL; |
| 7 | 1023 yaffs_Device *dev; |
| 78 | 1024 |
| 1025 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_link\n")); | |
| 1026 | |
| 7 | 1027 obj = yaffs_InodeToObject(inode); |
| 1028 dev = obj->myDev; | |
| 78 | 1029 |
| 7 | 1030 yaffs_GrossLock(dev); |
| 1031 | |
| 78 | 1032 if (!S_ISDIR(inode->i_mode)) /* Don't link directories */ |
| 7 | 1033 { |
| 78 | 1034 link = |
| 1035 yaffs_Link(yaffs_InodeToObject(dir), dentry->d_name.name, | |
| 1036 obj); | |
| 7 | 1037 } |
| 1038 | |
| 78 | 1039 if (link) { |
| 1040 old_dentry->d_inode->i_nlink = yaffs_GetObjectLinkCount(obj); | |
| 7 | 1041 d_instantiate(dentry, old_dentry->d_inode); |
| 1042 atomic_inc(&old_dentry->d_inode->i_count); | |
| 78 | 1043 T(YAFFS_TRACE_OS, |
| 1044 (KERN_DEBUG "yaffs_link link count %d i_count %d\n", | |
| 1045 old_dentry->d_inode->i_nlink, | |
| 1046 atomic_read(&old_dentry->d_inode->i_count))); | |
| 1047 | |
| 7 | 1048 } |
| 78 | 1049 |
| 7 | 1050 yaffs_GrossUnlock(dev); |
| 1051 | |
| 78 | 1052 if (link) { |
| 1053 | |
| 7 | 1054 return 0; |
| 1055 } | |
| 78 | 1056 |
| 1057 return -EPERM; | |
| 7 | 1058 } |
| 1059 | |
| 78 | 1060 static int yaffs_symlink(struct inode *dir, struct dentry *dentry, |
| 1061 const char *symname) | |
| 7 | 1062 { |
| 1063 yaffs_Object *obj; | |
| 1064 yaffs_Device *dev; | |
|
38
14315c63b7a5
Set the correct uid/gid of newly created files when: use fsuid & fsgid and take care when the SGID bit is set on the parent directory.
luc
parents:
34
diff
changeset
|
1065 uid_t uid = current->fsuid; |
|
14315c63b7a5
Set the correct uid/gid of newly created files when: use fsuid & fsgid and take care when the SGID bit is set on the parent directory.
luc
parents:
34
diff
changeset
|
1066 gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : current->fsgid; |
| 78 | 1067 |
| 1068 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_symlink\n")); | |
| 1069 | |
| 7 | 1070 dev = yaffs_InodeToObject(dir)->myDev; |
| 1071 yaffs_GrossLock(dev); | |
| 78 | 1072 obj = yaffs_MknodSymLink(yaffs_InodeToObject(dir), dentry->d_name.name, |
| 1073 S_IFLNK | S_IRWXUGO, uid, gid, symname); | |
| 7 | 1074 yaffs_GrossUnlock(dev); |
| 1075 | |
| 78 | 1076 if (obj) { |
| 7 | 1077 |
| 78 | 1078 struct inode *inode; |
| 1079 | |
|
24
4ebabd859f38
Some improvements to garbage collection and st_xxx to yst_xxx changes
charles
parents:
21
diff
changeset
|
1080 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj); |
| 7 | 1081 d_instantiate(dentry, inode); |
| 78 | 1082 T(YAFFS_TRACE_OS, (KERN_DEBUG "symlink created OK\n")); |
| 7 | 1083 return 0; |
| 78 | 1084 } else { |
| 1085 T(YAFFS_TRACE_OS, (KERN_DEBUG "symlink not created\n")); | |
| 7 | 1086 |
| 1087 } | |
| 78 | 1088 |
| 7 | 1089 return -ENOMEM; |
| 1090 } | |
| 1091 | |
| 78 | 1092 static int yaffs_sync_object(struct file *file, struct dentry *dentry, |
| 1093 int datasync) | |
| 7 | 1094 { |
| 1095 | |
| 1096 yaffs_Object *obj; | |
| 1097 yaffs_Device *dev; | |
| 78 | 1098 |
| 7 | 1099 obj = yaffs_DentryToObject(dentry); |
| 1100 | |
| 1101 dev = obj->myDev; | |
| 78 | 1102 |
| 1103 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_sync_object\n")); | |
| 7 | 1104 yaffs_GrossLock(dev); |
| 78 | 1105 yaffs_FlushFile(obj, 1); |
| 7 | 1106 yaffs_GrossUnlock(dev); |
| 1107 return 0; | |
| 1108 } | |
| 1109 | |
| 1110 /* | |
| 1111 * The VFS layer already does all the dentry stuff for rename. | |
| 1112 * | |
| 1113 * NB: POSIX says you can rename an object over an old object of the same name | |
| 1114 */ | |
| 78 | 1115 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 1116 struct inode *new_dir, struct dentry *new_dentry) | |
| 7 | 1117 { |
| 1118 yaffs_Device *dev; | |
| 1119 int retVal = YAFFS_FAIL; | |
| 1120 yaffs_Object *target; | |
| 78 | 1121 |
| 86 | 1122 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_rename\n")); |
| 7 | 1123 dev = yaffs_InodeToObject(old_dir)->myDev; |
| 1124 | |
| 1125 yaffs_GrossLock(dev); | |
| 78 | 1126 |
| 1127 /* Check if the target is an existing directory that is not empty. */ | |
| 1128 target = | |
| 1129 yaffs_FindObjectByName(yaffs_InodeToObject(new_dir), | |
| 1130 new_dentry->d_name.name); | |
| 86 | 1131 |
| 1132 | |
| 78 | 1133 |
| 1134 if (target && | |
| 1135 target->variantType == YAFFS_OBJECT_TYPE_DIRECTORY && | |
| 1136 !list_empty(&target->variant.directoryVariant.children)) { | |
| 86 | 1137 |
| 1138 T(YAFFS_TRACE_OS, (KERN_DEBUG "target is non-empty dir\n")); | |
| 1139 | |
| 7 | 1140 retVal = YAFFS_FAIL; |
| 78 | 1141 } else { |
| 1142 | |
| 1143 /* Now does unlinking internally using shadowing mechanism */ | |
| 86 | 1144 T(YAFFS_TRACE_OS, (KERN_DEBUG "calling yaffs_RenameObject\n")); |
| 1145 | |
| 78 | 1146 retVal = |
| 1147 yaffs_RenameObject(yaffs_InodeToObject(old_dir), | |
| 1148 old_dentry->d_name.name, | |
| 1149 yaffs_InodeToObject(new_dir), | |
| 1150 new_dentry->d_name.name); | |
| 1151 | |
| 7 | 1152 } |
| 1153 yaffs_GrossUnlock(dev); | |
| 78 | 1154 |
| 1155 if (retVal == YAFFS_OK) { | |
| 91 | 1156 if(target) { |
| 1157 new_dentry->d_inode->i_nlink--; | |
| 1158 mark_inode_dirty(new_dentry->d_inode); | |
| 1159 } | |
| 78 | 1160 |
| 7 | 1161 return 0; |
| 78 | 1162 } else { |
| 7 | 1163 return -ENOTEMPTY; |
| 1164 } | |
| 1165 | |
| 1166 } | |
| 1167 | |
| 1168 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr) | |
| 1169 { | |
| 1170 struct inode *inode = dentry->d_inode; | |
| 1171 int error; | |
| 1172 yaffs_Device *dev; | |
| 78 | 1173 |
| 1174 T(YAFFS_TRACE_OS, | |
| 1175 (KERN_DEBUG "yaffs_setattr of object %d\n", | |
| 1176 yaffs_InodeToObject(inode)->objectId)); | |
| 1177 | |
| 1178 if ((error = inode_change_ok(inode, attr)) == 0) { | |
| 1179 | |
| 7 | 1180 dev = yaffs_InodeToObject(inode)->myDev; |
| 1181 yaffs_GrossLock(dev); | |
| 78 | 1182 if (yaffs_SetAttributes(yaffs_InodeToObject(inode), attr) == |
| 1183 YAFFS_OK) { | |
| 7 | 1184 error = 0; |
| 78 | 1185 } else { |
| 7 | 1186 error = -EPERM; |
| 1187 } | |
| 1188 yaffs_GrossUnlock(dev); | |
| 25 | 1189 if (!error) |
| 78 | 1190 error = inode_setattr(inode, attr); |
| 7 | 1191 } |
| 1192 return error; | |
| 1193 } | |
| 1194 | |
| 1195 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0)) | |
| 1196 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf) | |
| 1197 #else | |
| 1198 static int yaffs_statfs(struct super_block *sb, struct statfs *buf) | |
| 1199 #endif | |
| 1200 { | |
| 16 | 1201 |
| 7 | 1202 yaffs_Device *dev = yaffs_SuperToDevice(sb); |
| 78 | 1203 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_statfs\n")); |
| 7 | 1204 |
| 1205 yaffs_GrossLock(dev); | |
| 78 | 1206 |
| 7 | 1207 buf->f_type = YAFFS_MAGIC; |
| 1208 buf->f_bsize = sb->s_blocksize; | |
| 1209 buf->f_namelen = 255; | |
| 78 | 1210 if (sb->s_blocksize > dev->nBytesPerChunk) { |
| 1211 | |
| 1212 buf->f_blocks = | |
| 1213 (dev->endBlock - dev->startBlock + | |
| 1214 1) * dev->nChunksPerBlock / (sb->s_blocksize / | |
| 1215 dev->nBytesPerChunk); | |
| 1216 buf->f_bfree = | |
| 1217 yaffs_GetNumberOfFreeChunks(dev) / (sb->s_blocksize / | |
| 1218 dev->nBytesPerChunk); | |
| 1219 } else { | |
| 1220 | |
| 1221 buf->f_blocks = | |
| 1222 (dev->endBlock - dev->startBlock + | |
| 1223 1) * dev->nChunksPerBlock * (dev->nBytesPerChunk / | |
| 1224 sb->s_blocksize); | |
| 1225 buf->f_bfree = | |
| 1226 yaffs_GetNumberOfFreeChunks(dev) * (dev->nBytesPerChunk / | |
| 1227 sb->s_blocksize); | |
| 16 | 1228 } |
| 7 | 1229 buf->f_files = 0; |
| 1230 buf->f_ffree = 0; | |
| 78 | 1231 buf->f_bavail = buf->f_bfree; |
| 1232 | |
| 7 | 1233 yaffs_GrossUnlock(dev); |
| 1234 return 0; | |
| 1235 } | |
| 1236 | |
| 78 | 1237 static void yaffs_read_inode(struct inode *inode) |
| 7 | 1238 { |
| 78 | 1239 /* NB This is called as a side effect of other functions and |
| 1240 * thus gross locking should always be in place already. | |
| 1241 */ | |
| 1242 | |
| 1243 yaffs_Object *obj; | |
| 7 | 1244 yaffs_Device *dev = yaffs_SuperToDevice(inode->i_sb); |
| 78 | 1245 |
| 1246 T(YAFFS_TRACE_OS, | |
| 1247 (KERN_DEBUG "yaffs_read_inode for %d\n", (int)inode->i_ino)); | |
| 7 | 1248 |
| 78 | 1249 obj = yaffs_FindObjectByNumber(dev, inode->i_ino); |
| 1250 | |
| 1251 yaffs_FillInodeFromObject(inode, obj); | |
| 7 | 1252 |
| 1253 } | |
| 1254 | |
| 29 | 1255 static LIST_HEAD(yaffs_dev_list); |
| 7 | 1256 |
| 1257 static void yaffs_put_super(struct super_block *sb) | |
| 1258 { | |
| 1259 yaffs_Device *dev = yaffs_SuperToDevice(sb); | |
| 78 | 1260 |
| 7 | 1261 yaffs_GrossLock(dev); |
| 78 | 1262 if (dev->putSuperFunc) { |
| 1263 dev->putSuperFunc(sb); | |
| 7 | 1264 } |
| 1265 yaffs_Deinitialise(dev); | |
| 1266 yaffs_GrossUnlock(dev); | |
| 1267 | |
| 29 | 1268 /* we assume this is protected by lock_kernel() in mount/umount */ |
| 1269 list_del(&dev->devList); | |
| 1270 | |
| 7 | 1271 kfree(dev); |
| 1272 } | |
| 1273 | |
| 1274 | |
| 78 | 1275 static void yaffs_MTDPutSuper(struct super_block *sb) |
| 7 | 1276 { |
| 78 | 1277 |
| 7 | 1278 struct mtd_info *mtd = yaffs_SuperToDevice(sb)->genericDevice; |
| 78 | 1279 |
| 1280 if (mtd->sync) { | |
| 7 | 1281 mtd->sync(mtd); |
| 1282 } | |
| 78 | 1283 |
| 7 | 1284 put_mtd_device(mtd); |
| 1285 } | |
| 1286 | |
| 1287 | |
| 78 | 1288 static struct super_block *yaffs_internal_read_super(int yaffsVersion, |
| 1289 struct super_block *sb, | |
| 1290 void *data, int silent) | |
| 7 | 1291 { |
| 1292 int nBlocks; | |
| 78 | 1293 struct inode *inode = NULL; |
| 1294 struct dentry *root; | |
| 29 | 1295 yaffs_Device *dev = 0; |
| 78 | 1296 char devname_buf[BDEVNAME_SIZE + 1]; |
|
61
c2fdd493a8f8
Adapt the code structure after removing of the yaffsram support.
luc
parents:
60
diff
changeset
|
1297 struct mtd_info *mtd; |
| 7 | 1298 int err; |
| 78 | 1299 |
| 7 | 1300 sb->s_magic = YAFFS_MAGIC; |
| 1301 sb->s_op = &yaffs_super_ops; | |
| 78 | 1302 |
| 1303 if (!sb) | |
| 1304 printk(KERN_INFO "yaffs: sb is NULL\n"); | |
| 1305 else if (!sb->s_dev) | |
| 1306 printk(KERN_INFO "yaffs: sb->s_dev is NULL\n"); | |
| 1307 else if (!yaffs_devname(sb, devname_buf)) | |
| 1308 printk(KERN_INFO "yaffs: devname is NULL\n"); | |
| 7 | 1309 else |
| 78 | 1310 printk(KERN_INFO "yaffs: dev is %d name is \"%s\"\n", |
| 1311 sb->s_dev, | |
| 1312 yaffs_devname(sb, devname_buf)); | |
| 7 | 1313 |
| 1314 sb->s_blocksize = PAGE_CACHE_SIZE; | |
| 1315 sb->s_blocksize_bits = PAGE_CACHE_SHIFT; | |
| 78 | 1316 T(YAFFS_TRACE_OS, ("yaffs_read_super: Using yaffs%d\n", yaffsVersion)); |
| 1317 T(YAFFS_TRACE_OS, | |
| 1318 ("yaffs_read_super: block size %d\n", (int)(sb->s_blocksize))); | |
| 7 | 1319 |
| 1320 #ifdef CONFIG_YAFFS_DISABLE_WRITE_VERIFY | |
| 78 | 1321 T(YAFFS_TRACE_OS, |
| 1322 ("yaffs: Write verification disabled. All guarantees " | |
| 1323 "null and void\n")); | |
| 7 | 1324 #endif |
| 1325 | |
| 78 | 1326 T(YAFFS_TRACE_ALWAYS, ("yaffs: Attempting MTD mount on %u.%u, " |
| 1327 "\"%s\"\n", | |
| 1328 MAJOR(sb->s_dev), MINOR(sb->s_dev), | |
| 1329 yaffs_devname(sb, devname_buf))); | |
| 7 | 1330 |
| 78 | 1331 /* Check it's an mtd device..... */ |
| 1332 if (MAJOR(sb->s_dev) != MTD_BLOCK_MAJOR) { | |
| 1333 return NULL; /* This isn't an mtd device */ | |
| 1334 } | |
| 1335 /* Get the device */ | |
|
61
c2fdd493a8f8
Adapt the code structure after removing of the yaffsram support.
luc
parents:
60
diff
changeset
|
1336 mtd = get_mtd_device(NULL, MINOR(sb->s_dev)); |
| 78 | 1337 if (!mtd) { |
| 1338 T(YAFFS_TRACE_ALWAYS, | |
| 1339 ("yaffs: MTD device #%u doesn't appear to exist\n", | |
| 1340 MINOR(sb->s_dev))); | |
|
61
c2fdd493a8f8
Adapt the code structure after removing of the yaffsram support.
luc
parents:
60
diff
changeset
|
1341 return NULL; |
|
c2fdd493a8f8
Adapt the code structure after removing of the yaffsram support.
luc
parents:
60
diff
changeset
|
1342 } |
| 78 | 1343 /* Check it's NAND */ |
| 1344 if (mtd->type != MTD_NANDFLASH) { | |
| 1345 T(YAFFS_TRACE_ALWAYS, | |
| 1346 ("yaffs: MTD device is not NAND it's type %d\n", mtd->type)); | |
|
61
c2fdd493a8f8
Adapt the code structure after removing of the yaffsram support.
luc
parents:
60
diff
changeset
|
1347 return NULL; |
|
c2fdd493a8f8
Adapt the code structure after removing of the yaffsram support.
luc
parents:
60
diff
changeset
|
1348 } |
|
c2fdd493a8f8
Adapt the code structure after removing of the yaffsram support.
luc
parents:
60
diff
changeset
|
1349 |
| 78 | 1350 T(YAFFS_TRACE_OS, (" erase %p\n", mtd->erase)); |
| 1351 T(YAFFS_TRACE_OS, (" read %p\n", mtd->read)); | |
| 1352 T(YAFFS_TRACE_OS, (" write %p\n", mtd->write)); | |
| 1353 T(YAFFS_TRACE_OS, (" readoob %p\n", mtd->read_oob)); | |
| 1354 T(YAFFS_TRACE_OS, (" writeoob %p\n", mtd->write_oob)); | |
| 1355 T(YAFFS_TRACE_OS, (" block_isbad %p\n", mtd->block_isbad)); | |
| 1356 T(YAFFS_TRACE_OS, (" block_markbad %p\n", mtd->block_markbad)); | |
| 1357 T(YAFFS_TRACE_OS, (" oobblock %d\n", mtd->oobblock)); | |
| 1358 T(YAFFS_TRACE_OS, (" oobsize %d\n", mtd->oobsize)); | |
| 1359 T(YAFFS_TRACE_OS, (" erasesize %d\n", mtd->erasesize)); | |
| 1360 T(YAFFS_TRACE_OS, (" size %d\n", mtd->size)); | |
|
61
c2fdd493a8f8
Adapt the code structure after removing of the yaffsram support.
luc
parents:
60
diff
changeset
|
1361 |
| 78 | 1362 if (yaffsVersion == 2) { |
| 1363 /* Check for version 2 style functions */ | |
| 1364 if (!mtd->erase || | |
| 1365 !mtd->block_isbad || | |
| 1366 !mtd->block_markbad || | |
| 1367 !mtd->read || | |
| 1368 !mtd->write || | |
| 1369 !mtd->write_ecc || | |
| 1370 !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) { | |
| 1371 T(YAFFS_TRACE_ALWAYS, | |
| 1372 ("yaffs: MTD device does not support required " | |
| 1373 "functions\n"));; | |
| 7 | 1374 return NULL; |
| 1375 } | |
| 78 | 1376 |
| 1377 if (mtd->oobblock < YAFFS_MIN_YAFFS2_CHUNK_SIZE || | |
| 1378 mtd->oobsize < YAFFS_MIN_YAFFS2_SPARE_SIZE) { | |
| 1379 T(YAFFS_TRACE_ALWAYS, | |
| 1380 ("yaffs: MTD device does not support have the " | |
| 1381 "right page sizes\n")); | |
|
61
c2fdd493a8f8
Adapt the code structure after removing of the yaffsram support.
luc
parents:
60
diff
changeset
|
1382 return NULL; |
| 7 | 1383 } |
| 78 | 1384 } else { |
| 1385 /* Check for V1 style functions */ | |
| 1386 if (!mtd->erase || | |
| 1387 !mtd->read || | |
| 1388 !mtd->write || | |
| 1389 !mtd->write_ecc || | |
| 1390 !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) { | |
| 1391 T(YAFFS_TRACE_ALWAYS, | |
| 1392 ("yaffs: MTD device does not support required " | |
| 1393 "functions\n"));; | |
| 1394 return NULL; | |
| 1395 } | |
| 1396 | |
| 1397 if (mtd->oobblock != YAFFS_BYTES_PER_CHUNK || | |
| 1398 mtd->oobsize != YAFFS_BYTES_PER_SPARE) { | |
| 1399 T(YAFFS_TRACE_ALWAYS, | |
| 1400 ("yaffs: MTD device does not support have the " | |
| 1401 "right page sizes\n")); | |
|
61
c2fdd493a8f8
Adapt the code structure after removing of the yaffsram support.
luc
parents:
60
diff
changeset
|
1402 return NULL; |
|
c2fdd493a8f8
Adapt the code structure after removing of the yaffsram support.
luc
parents:
60
diff
changeset
|
1403 } |
|
c2fdd493a8f8
Adapt the code structure after removing of the yaffsram support.
luc
parents:
60
diff
changeset
|
1404 } |
| 7 | 1405 |
| 78 | 1406 /* OK, so if we got here, we have an MTD that's NAND and looks |
| 1407 * like it has the right capabilities | |
| 1408 * Set the yaffs_Device up for mtd | |
| 1409 */ | |
| 7 | 1410 |
| 1411 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0)) | |
| 78 | 1412 sb->s_fs_info = dev = kmalloc(sizeof(yaffs_Device), GFP_KERNEL); |
| 7 | 1413 #else |
| 78 | 1414 sb->u.generic_sbp = dev = kmalloc(sizeof(yaffs_Device), GFP_KERNEL); |
| 7 | 1415 #endif |
| 78 | 1416 if (!dev) { |
| 1417 /* Deep shit could not allocate device structure */ | |
| 1418 T(YAFFS_TRACE_ALWAYS, | |
| 1419 ("yaffs_read_super: Failed trying to allocate " | |
| 1420 "yaffs_Device. \n")); | |
|
61
c2fdd493a8f8
Adapt the code structure after removing of the yaffsram support.
luc
parents:
60
diff
changeset
|
1421 return NULL; |
|
c2fdd493a8f8
Adapt the code structure after removing of the yaffsram support.
luc
parents:
60
diff
changeset
|
1422 } |
|
c2fdd493a8f8
Adapt the code structure after removing of the yaffsram support.
luc
parents:
60
diff
changeset
|
1423 |
| 78 | 1424 memset(dev, 0, sizeof(yaffs_Device)); |
| 1425 dev->genericDevice = mtd; | |
|
61
c2fdd493a8f8
Adapt the code structure after removing of the yaffsram support.
luc
parents:
60
diff
changeset
|
1426 dev->name = mtd->name; |
| 7 | 1427 |
| 78 | 1428 /* Set up the memory size parameters.... */ |
| 1429 | |
|
61
c2fdd493a8f8
Adapt the code structure after removing of the yaffsram support.
luc
parents:
60
diff
changeset
|
1430 nBlocks = mtd->size / (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK); |
|
c2fdd493a8f8
Adapt the code structure after removing of the yaffsram support.
luc
parents:
60
diff
changeset
|
1431 dev->startBlock = 0; |
|
c2fdd493a8f8
Adapt the code structure after removing of the yaffsram support.
luc
parents:
60
diff
changeset
|
1432 dev->endBlock = nBlocks - 1; |
|
c2fdd493a8f8
Adapt the code structure after removing of the yaffsram support.
luc
parents:
60
diff
changeset
|
1433 dev->nChunksPerBlock = YAFFS_CHUNKS_PER_BLOCK; |
|
c2fdd493a8f8
Adapt the code structure after removing of the yaffsram support.
luc
parents:
60
diff
changeset
|
1434 dev->nBytesPerChunk = YAFFS_BYTES_PER_CHUNK; |
|
c2fdd493a8f8
Adapt the code structure after removing of the yaffsram support.
luc
parents:
60
diff
changeset
|
1435 dev->nReservedBlocks = 5; |
| 78 | 1436 dev->nShortOpCaches = 10; /* Enable short op caching */ |
| 7 | 1437 |
| 78 | 1438 /* ... and the functions. */ |
| 1439 if (yaffsVersion == 2) { | |
| 1440 dev->writeChunkWithTagsToNAND = | |
| 1441 nandmtd2_WriteChunkWithTagsToNAND; | |
| 1442 dev->readChunkWithTagsFromNAND = | |
| 1443 nandmtd2_ReadChunkWithTagsFromNAND; | |
|
61
c2fdd493a8f8
Adapt the code structure after removing of the yaffsram support.
luc
parents:
60
diff
changeset
|
1444 dev->markNANDBlockBad = nandmtd2_MarkNANDBlockBad; |
|
c2fdd493a8f8
Adapt the code structure after removing of the yaffsram support.
luc
parents:
60
diff
changeset
|
1445 dev->queryNANDBlock = nandmtd2_QueryNANDBlock; |
|
c2fdd493a8f8
Adapt the code structure after removing of the yaffsram support.
luc
parents:
60
diff
changeset
|
1446 dev->spareBuffer = YMALLOC(mtd->oobsize); |
|
c2fdd493a8f8
Adapt the code structure after removing of the yaffsram support.
luc
parents:
60
diff
changeset
|
1447 dev->isYaffs2 = 1; |
|
c2fdd493a8f8
Adapt the code structure after removing of the yaffsram support.
luc
parents:
60
diff
changeset
|
1448 dev->nBytesPerChunk = mtd->oobblock; |
|
c2fdd493a8f8
Adapt the code structure after removing of the yaffsram support.
luc
parents:
60
diff
changeset
|
1449 dev->nChunksPerBlock = mtd->erasesize / mtd->oobblock; |
|
c2fdd493a8f8
Adapt the code structure after removing of the yaffsram support.
luc
parents:
60
diff
changeset
|
1450 nBlocks = mtd->size / mtd->erasesize; |
| 21 | 1451 dev->startBlock = 0; |
| 7 | 1452 dev->endBlock = nBlocks - 1; |
| 78 | 1453 } else { |
|
61
c2fdd493a8f8
Adapt the code structure after removing of the yaffsram support.
luc
parents:
60
diff
changeset
|
1454 dev->writeChunkToNAND = nandmtd_WriteChunkToNAND; |
|
c2fdd493a8f8
Adapt the code structure after removing of the yaffsram support.
luc
parents:
60
diff
changeset
|
1455 dev->readChunkFromNAND = nandmtd_ReadChunkFromNAND; |
|
c2fdd493a8f8
Adapt the code structure after removing of the yaffsram support.
luc
parents:
60
diff
changeset
|
1456 dev->isYaffs2 = 0; |
|
c2fdd493a8f8
Adapt the code structure after removing of the yaffsram support.
luc
parents:
60
diff
changeset
|
1457 } |
| 78 | 1458 /* ... and common functions */ |
|
61
c2fdd493a8f8
Adapt the code structure after removing of the yaffsram support.
luc
parents:
60
diff
changeset
|
1459 dev->eraseBlockInNAND = nandmtd_EraseBlockInNAND; |
|
c2fdd493a8f8
Adapt the code structure after removing of the yaffsram support.
luc
parents:
60
diff
changeset
|
1460 dev->initialiseNAND = nandmtd_InitialiseNAND; |
| 78 | 1461 |
|
61
c2fdd493a8f8
Adapt the code structure after removing of the yaffsram support.
luc
parents:
60
diff
changeset
|
1462 dev->putSuperFunc = yaffs_MTDPutSuper; |
| 78 | 1463 |
| 63 | 1464 #ifndef CONFIG_YAFFS_DOES_ECC |
|
61
c2fdd493a8f8
Adapt the code structure after removing of the yaffsram support.
luc
parents:
60
diff
changeset
|
1465 dev->useNANDECC = 1; |
| 7 | 1466 #endif |
| 1467 | |
| 107 | 1468 #ifdef CONFIG_YAFFS_DISABLE_WIDE_TNODES |
| 1469 dev->wideTnodesDisabled = 1; | |
| 1470 #endif | |
| 1471 | |
| 29 | 1472 /* we assume this is protected by lock_kernel() in mount/umount */ |
| 1473 list_add_tail(&dev->devList, &yaffs_dev_list); | |
| 1474 | |
| 7 | 1475 init_MUTEX(&dev->grossLock); |
| 78 | 1476 |
| 7 | 1477 yaffs_GrossLock(dev); |
| 78 | 1478 |
| 7 | 1479 err = yaffs_GutsInitialise(dev); |
| 1480 | |
| 78 | 1481 T(YAFFS_TRACE_OS, |
| 1482 ("yaffs_read_super: guts initialised %s\n", | |
| 1483 (err == YAFFS_OK) ? "OK" : "FAILED")); | |
| 7 | 1484 |
| 78 | 1485 /* Create root inode */ |
| 1486 if (err == YAFFS_OK) | |
| 1487 inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0, | |
| 1488 yaffs_Root(dev)); | |
| 7 | 1489 |
| 1490 yaffs_GrossUnlock(dev); | |
| 1491 | |
| 1492 if (!inode) | |
| 1493 return NULL; | |
| 1494 | |
| 78 | 1495 inode->i_op = &yaffs_dir_inode_operations; |
| 1496 inode->i_fop = &yaffs_dir_operations; | |
| 1497 | |
| 1498 T(YAFFS_TRACE_OS, ("yaffs_read_super: got root inode\n")); | |
| 7 | 1499 |
| 1500 root = d_alloc_root(inode); | |
| 1501 | |
| 78 | 1502 T(YAFFS_TRACE_OS, ("yaffs_read_super: d_alloc_root done\n")); |
| 7 | 1503 |
| 1504 if (!root) { | |
| 1505 iput(inode); | |
| 1506 return NULL; | |
| 1507 } | |
| 1508 sb->s_root = root; | |
| 1509 | |
| 78 | 1510 T(YAFFS_TRACE_OS, ("yaffs_read_super: done\n")); |
| 7 | 1511 return sb; |
| 1512 } | |
| 1513 | |
|
34
95f802b86a29
Move the internals read_super() functions near the declaration of
luc
parents:
31
diff
changeset
|
1514 |
|
95f802b86a29
Move the internals read_super() functions near the declaration of
luc
parents:
31
diff
changeset
|
1515 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0)) |
| 78 | 1516 static int yaffs_internal_read_super_mtd(struct super_block *sb, void *data, |
| 1517 int silent) | |
| 7 | 1518 { |
| 98 | 1519 return yaffs_internal_read_super(1, sb, data, silent) ? 0 : -EINVAL; |
| 7 | 1520 } |
| 1521 | |
| 78 | 1522 static struct super_block *yaffs_read_super(struct file_system_type *fs, |
| 1523 int flags, const char *dev_name, | |
| 1524 void *data) | |
| 7 | 1525 { |
| 1526 | |
| 78 | 1527 return get_sb_bdev(fs, flags, dev_name, data, |
| 1528 yaffs_internal_read_super_mtd); | |
| 7 | 1529 } |
| 1530 | |
| 1531 static struct file_system_type yaffs_fs_type = { | |
| 78 | 1532 .owner = THIS_MODULE, |
| 1533 .name = "yaffs", | |
| 1534 .get_sb = yaffs_read_super, | |
| 1535 .kill_sb = kill_block_super, | |
| 1536 .fs_flags = FS_REQUIRES_DEV, | |
| 7 | 1537 }; |
| 1538 #else | |
| 78 | 1539 static struct super_block *yaffs_read_super(struct super_block *sb, void *data, |
| 1540 int silent) | |
| 7 | 1541 { |
| 78 | 1542 return yaffs_internal_read_super(1, sb, data, silent); |
| 7 | 1543 } |
| 1544 | |
| 78 | 1545 static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super, |
| 1546 FS_REQUIRES_DEV); | |
| 7 | 1547 #endif |
| 1548 | |
| 1549 | |
| 51 | 1550 #ifdef CONFIG_YAFFS_YAFFS2 |
| 7 | 1551 |
| 1552 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0)) | |
| 78 | 1553 static int yaffs2_internal_read_super_mtd(struct super_block *sb, void *data, |
| 1554 int silent) | |
|
34
95f802b86a29
Move the internals read_super() functions near the declaration of
luc
parents:
31
diff
changeset
|
1555 { |
| 98 | 1556 return yaffs_internal_read_super(2, sb, data, silent) ? 0 : -EINVAL; |
|
34
95f802b86a29
Move the internals read_super() functions near the declaration of
luc
parents:
31
diff
changeset
|
1557 } |
|
95f802b86a29
Move the internals read_super() functions near the declaration of
luc
parents:
31
diff
changeset
|
1558 |
| 78 | 1559 static struct super_block *yaffs2_read_super(struct file_system_type *fs, |
| 1560 int flags, const char *dev_name, | |
| 1561 void *data) | |
| 7 | 1562 { |
| 1563 | |
| 78 | 1564 return get_sb_bdev(fs, flags, dev_name, data, |
| 1565 yaffs2_internal_read_super_mtd); | |
| 7 | 1566 } |
| 1567 | |
| 1568 static struct file_system_type yaffs2_fs_type = { | |
| 78 | 1569 .owner = THIS_MODULE, |
| 1570 .name = "yaffs2", | |
| 1571 .get_sb = yaffs2_read_super, | |
| 1572 .kill_sb = kill_block_super, | |
| 1573 .fs_flags = FS_REQUIRES_DEV, | |
| 7 | 1574 }; |
| 1575 #else | |
| 78 | 1576 static struct super_block *yaffs2_read_super(struct super_block *sb, |
| 1577 void *data, int silent) | |
| 7 | 1578 { |
| 78 | 1579 return yaffs_internal_read_super(2, sb, data, silent); |
| 7 | 1580 } |
| 1581 | |
| 78 | 1582 static DECLARE_FSTYPE(yaffs2_fs_type, "yaffs2", yaffs2_read_super, |
| 1583 FS_REQUIRES_DEV); | |
| 7 | 1584 #endif |
| 1585 | |
| 78 | 1586 #endif /* CONFIG_YAFFS_YAFFS2 */ |
| 7 | 1587 |
| 1588 static struct proc_dir_entry *my_proc_entry; | |
| 1589 | |
| 78 | 1590 static char *yaffs_dump_dev(char *buf, yaffs_Device * dev) |
| 7 | 1591 { |
| 78 | 1592 buf += sprintf(buf, "startBlock......... %d\n", dev->startBlock); |
| 1593 buf += sprintf(buf, "endBlock........... %d\n", dev->endBlock); | |
| 1594 buf += sprintf(buf, "chunkGroupBits..... %d\n", dev->chunkGroupBits); | |
| 1595 buf += sprintf(buf, "chunkGroupSize..... %d\n", dev->chunkGroupSize); | |
| 1596 buf += sprintf(buf, "nErasedBlocks...... %d\n", dev->nErasedBlocks); | |
| 1597 buf += sprintf(buf, "nTnodesCreated..... %d\n", dev->nTnodesCreated); | |
| 1598 buf += sprintf(buf, "nFreeTnodes........ %d\n", dev->nFreeTnodes); | |
| 1599 buf += sprintf(buf, "nObjectsCreated.... %d\n", dev->nObjectsCreated); | |
| 1600 buf += sprintf(buf, "nFreeObjects....... %d\n", dev->nFreeObjects); | |
| 1601 buf += sprintf(buf, "nFreeChunks........ %d\n", dev->nFreeChunks); | |
| 1602 buf += sprintf(buf, "nPageWrites........ %d\n", dev->nPageWrites); | |
| 1603 buf += sprintf(buf, "nPageReads......... %d\n", dev->nPageReads); | |
| 1604 buf += sprintf(buf, "nBlockErasures..... %d\n", dev->nBlockErasures); | |
| 1605 buf += sprintf(buf, "nGCCopies.......... %d\n", dev->nGCCopies); | |
| 1606 buf += | |
| 1607 sprintf(buf, "garbageCollections. %d\n", dev->garbageCollections); | |
| 1608 buf += | |
| 1609 sprintf(buf, "passiveGCs......... %d\n", | |
| 1610 dev->passiveGarbageCollections); | |
| 1611 buf += sprintf(buf, "nRetriedWrites..... %d\n", dev->nRetriedWrites); | |
| 1612 buf += sprintf(buf, "nRetireBlocks...... %d\n", dev->nRetiredBlocks); | |
| 1613 buf += sprintf(buf, "eccFixed........... %d\n", dev->eccFixed); | |
| 1614 buf += sprintf(buf, "eccUnfixed......... %d\n", dev->eccUnfixed); | |
| 1615 buf += sprintf(buf, "tagsEccFixed....... %d\n", dev->tagsEccFixed); | |
| 1616 buf += sprintf(buf, "tagsEccUnfixed..... %d\n", dev->tagsEccUnfixed); | |
| 1617 buf += sprintf(buf, "cacheHits.......... %d\n", dev->cacheHits); | |
| 1618 buf += sprintf(buf, "nDeletedFiles...... %d\n", dev->nDeletedFiles); | |
| 1619 buf += sprintf(buf, "nUnlinkedFiles..... %d\n", dev->nUnlinkedFiles); | |
| 1620 buf += | |
| 1621 sprintf(buf, "nBackgroudDeletions %d\n", dev->nBackgroundDeletions); | |
| 1622 buf += sprintf(buf, "useNANDECC......... %d\n", dev->useNANDECC); | |
| 1623 buf += sprintf(buf, "isYaffs2........... %d\n", dev->isYaffs2); | |
| 7 | 1624 |
| 78 | 1625 return buf; |
| 7 | 1626 } |
| 1627 | |
| 78 | 1628 static int yaffs_proc_read(char *page, |
| 1629 char **start, | |
| 1630 off_t offset, int count, int *eof, void *data) | |
| 7 | 1631 { |
| 29 | 1632 struct list_head *item; |
| 1633 char *buf = page; | |
| 1634 int step = offset; | |
| 1635 int n = 0; | |
| 7 | 1636 |
| 29 | 1637 /* Get proc_file_read() to step 'offset' by one on each sucessive call. |
| 1638 * We use 'offset' (*ppos) to indicate where we are in devList. | |
| 1639 * This also assumes the user has posted a read buffer large | |
| 1640 * enough to hold the complete output; but that's life in /proc. | |
| 1641 */ | |
| 7 | 1642 |
| 29 | 1643 *(int *)start = 1; |
| 1644 | |
| 1645 /* Print header first */ | |
| 1646 if (step == 0) { | |
| 78 | 1647 buf += sprintf(buf, "YAFFS built:" __DATE__ " " __TIME__ |
| 1648 "\n%s\n%s\n", yaffs_fs_c_version, | |
| 1649 yaffs_guts_c_version); | |
| 29 | 1650 } |
| 7 | 1651 |
| 29 | 1652 /* hold lock_kernel while traversing yaffs_dev_list */ |
| 1653 lock_kernel(); | |
| 7 | 1654 |
| 29 | 1655 /* Locate and print the Nth entry. Order N-squared but N is small. */ |
| 1656 list_for_each(item, &yaffs_dev_list) { | |
| 1657 yaffs_Device *dev = list_entry(item, yaffs_Device, devList); | |
| 1658 if (n < step) { | |
| 1659 n++; | |
| 1660 continue; | |
| 1661 } | |
| 78 | 1662 buf += sprintf(buf, "\nDevice %d \"%s\"\n", n, dev->name); |
| 29 | 1663 buf = yaffs_dump_dev(buf, dev); |
| 1664 break; | |
| 1665 } | |
| 1666 unlock_kernel(); | |
| 1667 | |
| 78 | 1668 return buf - page < count ? buf - page : count; |
| 7 | 1669 } |
| 1670 | |
| 78 | 1671 /* Stuff to handle installation of file systems */ |
| 1672 struct file_system_to_install { | |
| 1673 struct file_system_type *fst; | |
| 1674 int installed; | |
| 7 | 1675 }; |
| 1676 | |
| 78 | 1677 static struct file_system_to_install fs_to_install[] = { |
| 86 | 1678 //#ifdef CONFIG_YAFFS_YAFFS1 |
| 78 | 1679 {&yaffs_fs_type, 0}, |
| 86 | 1680 //#endif |
| 1681 //#ifdef CONFIG_YAFFS_YAFFS2 | |
| 78 | 1682 {&yaffs2_fs_type, 0}, |
| 86 | 1683 //#endif |
| 78 | 1684 {NULL, 0} |
| 7 | 1685 }; |
| 1686 | |
| 1687 static int __init init_yaffs_fs(void) | |
| 1688 { | |
| 1689 int error = 0; | |
| 78 | 1690 struct file_system_to_install *fsinst; |
| 7 | 1691 |
| 78 | 1692 T(YAFFS_TRACE_ALWAYS, |
| 1693 ("yaffs " __DATE__ " " __TIME__ " Installing. \n")); | |
| 7 | 1694 |
| 78 | 1695 /* Install the proc_fs entry */ |
| 1696 my_proc_entry = create_proc_read_entry("yaffs", | |
| 1697 S_IRUGO | S_IFREG, | |
| 1698 &proc_root, | |
| 1699 yaffs_proc_read, NULL); | |
| 1700 if (!my_proc_entry) { | |
| 1701 return -ENOMEM; | |
| 1702 } | |
| 1703 | |
| 1704 /* Now add the file system entries */ | |
| 1705 | |
| 1706 fsinst = fs_to_install; | |
| 7 | 1707 |
| 78 | 1708 while (fsinst->fst && !error) { |
| 1709 error = register_filesystem(fsinst->fst); | |
| 1710 if (!error) { | |
| 1711 fsinst->installed = 1; | |
| 1712 } | |
| 1713 fsinst++; | |
| 1714 } | |
| 1715 | |
| 1716 /* Any errors? uninstall */ | |
| 1717 if (error) { | |
| 1718 fsinst = fs_to_install; | |
| 1719 | |
| 1720 while (fsinst->fst) { | |
| 1721 if (fsinst->installed) { | |
| 1722 unregister_filesystem(fsinst->fst); | |
| 1723 fsinst->installed = 0; | |
| 1724 } | |
| 1725 fsinst++; | |
| 1726 } | |
| 1727 } | |
| 1728 | |
| 1729 return error; | |
| 7 | 1730 } |
| 1731 | |
| 1732 static void __exit exit_yaffs_fs(void) | |
| 1733 { | |
| 1734 | |
| 78 | 1735 struct file_system_to_install *fsinst; |
| 1736 | |
| 1737 T(YAFFS_TRACE_ALWAYS, ("yaffs " __DATE__ " " __TIME__ | |
| 1738 " removing. \n")); | |
| 1739 | |
| 1740 remove_proc_entry("yaffs", &proc_root); | |
| 7 | 1741 |
| 78 | 1742 fsinst = fs_to_install; |
| 1743 | |
| 1744 while (fsinst->fst) { | |
| 1745 if (fsinst->installed) { | |
| 1746 unregister_filesystem(fsinst->fst); | |
| 1747 fsinst->installed = 0; | |
| 1748 } | |
| 1749 fsinst++; | |
| 1750 } | |
| 7 | 1751 |
| 1752 } | |
| 1753 | |
| 1754 module_init(init_yaffs_fs) | |
| 1755 module_exit(exit_yaffs_fs) | |
| 1756 | |
| 1757 MODULE_DESCRIPTION("YAFFS2 - a NAND specific flash file system"); | |
| 1758 MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002,2003,2004"); | |
| 1759 MODULE_LICENSE("GPL"); |
