yaffs_fs.c 55 KB

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