yaffs_fs.c 56 KB

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