yaffs_vfs.c 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607
  1. /*
  2. * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  3. *
  4. * Copyright (C) 2002-2010 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 struct yaffs_dev associated with
  27. * this superblock
  28. * >> 2.6: sb->s_fs_info points to the struct yaffs_dev associated with this
  29. * superblock
  30. * >> inode->u.generic_ip points to the associated struct yaffs_obj.
  31. */
  32. /*
  33. * NB There are two variants of Linux VFS glue code. This variant supports
  34. * a single version and should not include any multi-version code.
  35. */
  36. #include <linux/version.h>
  37. #include <linux/kernel.h>
  38. #include <linux/module.h>
  39. #include <linux/slab.h>
  40. #include <linux/init.h>
  41. #include <linux/fs.h>
  42. #include <linux/proc_fs.h>
  43. #include <linux/pagemap.h>
  44. #include <linux/mtd/mtd.h>
  45. #include <linux/interrupt.h>
  46. #include <linux/string.h>
  47. #include <linux/ctype.h>
  48. #include <linux/namei.h>
  49. #include <linux/exportfs.h>
  50. #include <linux/kthread.h>
  51. #include <linux/delay.h>
  52. #include <linux/freezer.h>
  53. #include <asm/div64.h>
  54. #include <linux/statfs.h>
  55. #include <linux/mount.h>
  56. #include <linux/security.h>
  57. #include <linux/major.h>
  58. #include <linux/uidgid.h>
  59. #define UnlockPage(p) unlock_page(p)
  60. #define Page_Uptodate(page) test_bit(PG_uptodate, &(page)->flags)
  61. #define yaffs_devname(sb, buf) bdevname(sb->s_bdev, buf)
  62. #define YPROC_ROOT NULL
  63. #define Y_INIT_TIMER(a) init_timer_on_stack(a)
  64. #define WRITE_SIZE_STR "writesize"
  65. #define WRITE_SIZE(mtd) ((mtd)->writesize)
  66. static uint32_t YCALCBLOCKS(uint64_t partition_size, uint32_t block_size)
  67. {
  68. uint64_t result = partition_size;
  69. do_div(result, block_size);
  70. return (uint32_t) result;
  71. }
  72. #include <linux/uaccess.h>
  73. #include <linux/mtd/mtd.h>
  74. #include "yportenv.h"
  75. #include "yaffs_trace.h"
  76. #include "yaffs_guts.h"
  77. #include "yaffs_attribs.h"
  78. #include "yaffs_linux.h"
  79. #include "yaffs_mtdif.h"
  80. #include "yaffs_mtdif1.h"
  81. #include "yaffs_mtdif2.h"
  82. unsigned int yaffs_trace_mask = YAFFS_TRACE_BAD_BLOCKS | YAFFS_TRACE_ALWAYS | YAFFS_TRACE_ERROR;
  83. unsigned int yaffs_wr_attempts = YAFFS_WR_ATTEMPTS;
  84. unsigned int yaffs_auto_checkpoint = 1;
  85. unsigned int yaffs_gc_control = 1;
  86. unsigned int yaffs_bg_enable = 1;
  87. /* Module Parameters */
  88. module_param(yaffs_trace_mask, uint, 0644);
  89. module_param(yaffs_wr_attempts, uint, 0644);
  90. module_param(yaffs_auto_checkpoint, uint, 0644);
  91. module_param(yaffs_gc_control, uint, 0644);
  92. module_param(yaffs_bg_enable, uint, 0644);
  93. #define yaffs_inode_to_obj_lv(iptr) ((iptr)->i_private)
  94. #define yaffs_inode_to_obj(iptr) ((struct yaffs_obj *)(yaffs_inode_to_obj_lv(iptr)))
  95. #define yaffs_dentry_to_obj(dptr) yaffs_inode_to_obj((dptr)->d_inode)
  96. #define yaffs_super_to_dev(sb) ((struct yaffs_dev *)sb->s_fs_info)
  97. #define update_dir_time(dir) ((dir)->i_ctime = (dir)->i_mtime = CURRENT_TIME)
  98. static int __setxattr(struct inode *inode, const char *name,
  99. const void *value, size_t size, int flags);
  100. static unsigned yaffs_gc_control_callback(struct yaffs_dev *dev)
  101. {
  102. return yaffs_gc_control;
  103. }
  104. static void yaffs_gross_lock(struct yaffs_dev *dev)
  105. {
  106. yaffs_trace(YAFFS_TRACE_LOCK, "yaffs locking %p", current);
  107. mutex_lock(&(yaffs_dev_to_lc(dev)->gross_lock));
  108. yaffs_trace(YAFFS_TRACE_LOCK, "yaffs locked %p", current);
  109. }
  110. static void yaffs_gross_unlock(struct yaffs_dev *dev)
  111. {
  112. yaffs_trace(YAFFS_TRACE_LOCK, "yaffs unlocking %p", current);
  113. mutex_unlock(&(yaffs_dev_to_lc(dev)->gross_lock));
  114. }
  115. static void yaffs_fill_inode_from_obj(struct inode *inode,
  116. struct yaffs_obj *obj);
  117. static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino)
  118. {
  119. struct inode *inode;
  120. struct yaffs_obj *obj;
  121. struct yaffs_dev *dev = yaffs_super_to_dev(sb);
  122. gfp_t gfp_mask;
  123. yaffs_trace(YAFFS_TRACE_OS, "yaffs_iget for %lu", ino);
  124. inode = iget_locked(sb, ino);
  125. if (!inode)
  126. return ERR_PTR(-ENOMEM);
  127. gfp_mask = mapping_gfp_mask(inode->i_mapping);
  128. gfp_mask &= ~__GFP_HIGHMEM;
  129. mapping_set_gfp_mask(inode->i_mapping, gfp_mask);
  130. if (!(inode->i_state & I_NEW))
  131. return inode;
  132. /* NB This is called as a side effect of other functions, but
  133. * we had to release the lock to prevent deadlocks, so
  134. * need to lock again.
  135. */
  136. yaffs_gross_lock(dev);
  137. obj = yaffs_find_by_number(dev, inode->i_ino);
  138. yaffs_fill_inode_from_obj(inode, obj);
  139. yaffs_gross_unlock(dev);
  140. unlock_new_inode(inode);
  141. return inode;
  142. }
  143. struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
  144. struct yaffs_obj *obj)
  145. {
  146. struct inode *inode;
  147. if (!sb) {
  148. yaffs_trace(YAFFS_TRACE_OS,
  149. "yaffs_get_inode for NULL super_block!!");
  150. return NULL;
  151. }
  152. if (!obj) {
  153. yaffs_trace(YAFFS_TRACE_OS,
  154. "yaffs_get_inode for NULL object!!");
  155. return NULL;
  156. }
  157. yaffs_trace(YAFFS_TRACE_OS,
  158. "yaffs_get_inode for object %d",
  159. obj->obj_id);
  160. inode = yaffs_iget(sb, obj->obj_id);
  161. if (IS_ERR(inode))
  162. return NULL;
  163. /* NB Side effect: iget calls back to yaffs_read_inode(). */
  164. /* iget also increments the inode's i_count */
  165. /* NB You can't be holding gross_lock or deadlock will happen! */
  166. return inode;
  167. }
  168. static int init_xattrs(struct inode *inode, const struct xattr *xattr_array,
  169. void *fs_info)
  170. {
  171. const struct xattr *xattr;
  172. char *name;
  173. int err = 0;
  174. for (xattr = xattr_array; xattr->name != NULL; xattr++) {
  175. name = kmalloc(XATTR_SECURITY_PREFIX_LEN +
  176. strlen(xattr->name) + 1, GFP_NOFS);
  177. if (!name) {
  178. err = -ENOMEM;
  179. break;
  180. }
  181. strcpy(name, XATTR_SECURITY_PREFIX);
  182. strcpy(name + XATTR_SECURITY_PREFIX_LEN, xattr->name);
  183. err = __setxattr(inode, name, xattr->value, xattr->value_len, 0);
  184. kfree(name);
  185. if (err < 0)
  186. break;
  187. }
  188. return err;
  189. }
  190. /*This is necessary to assign types to newly created files/dirs*/
  191. static int yaffs_init_security(struct inode *inode, struct inode *dir,
  192. const struct qstr *qstr)
  193. {
  194. int err;
  195. err = security_inode_init_security(inode, dir, qstr, &init_xattrs, 0);
  196. if (err) {
  197. if (err == -EOPNOTSUPP)
  198. return 0;
  199. return err;
  200. }
  201. return err;
  202. }
  203. static int yaffs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
  204. dev_t rdev)
  205. {
  206. struct inode *inode;
  207. struct yaffs_obj *obj = NULL;
  208. struct yaffs_dev *dev;
  209. struct yaffs_obj *parent = yaffs_inode_to_obj(dir);
  210. int error = -ENOSPC;
  211. uid_t uid = current->cred->fsuid.val;
  212. gid_t gid =
  213. (dir->i_mode & S_ISGID) ? dir->i_gid.val : current->cred->fsgid.val;
  214. if ((dir->i_mode & S_ISGID) && S_ISDIR(mode))
  215. mode |= S_ISGID;
  216. if (parent) {
  217. yaffs_trace(YAFFS_TRACE_OS,
  218. "yaffs_mknod: parent object %d type %d",
  219. parent->obj_id, parent->variant_type);
  220. } else {
  221. yaffs_trace(YAFFS_TRACE_OS,
  222. "yaffs_mknod: could not get parent object");
  223. return -EPERM;
  224. }
  225. yaffs_trace(YAFFS_TRACE_OS,
  226. "yaffs_mknod: making oject for %s, mode %x dev %x",
  227. dentry->d_name.name, mode, rdev);
  228. dev = parent->my_dev;
  229. yaffs_gross_lock(dev);
  230. switch (mode & S_IFMT) {
  231. default:
  232. /* Special (socket, fifo, device...) */
  233. yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making special");
  234. obj =
  235. yaffs_create_special(parent, dentry->d_name.name, mode, uid,
  236. gid, old_encode_dev(rdev));
  237. break;
  238. case S_IFREG: /* file */
  239. yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making file");
  240. obj = yaffs_create_file(parent, dentry->d_name.name, mode, uid,
  241. gid);
  242. break;
  243. case S_IFDIR: /* directory */
  244. yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making directory");
  245. obj = yaffs_create_dir(parent, dentry->d_name.name, mode,
  246. uid, gid);
  247. break;
  248. case S_IFLNK: /* symlink */
  249. yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making symlink");
  250. obj = NULL; /* Do we ever get here? */
  251. break;
  252. }
  253. /* Can not call yaffs_get_inode() with gross lock held */
  254. yaffs_gross_unlock(dev);
  255. if (obj) {
  256. inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj);
  257. d_instantiate(dentry, inode);
  258. update_dir_time(dir);
  259. yaffs_trace(YAFFS_TRACE_OS,
  260. "yaffs_mknod created object %d count = %d",
  261. obj->obj_id, atomic_read(&inode->i_count));
  262. error = 0;
  263. yaffs_init_security(dentry->d_inode, dir, &dentry->d_name);
  264. yaffs_fill_inode_from_obj(dir, parent);
  265. } else {
  266. yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod failed making object");
  267. error = -ENOMEM;
  268. }
  269. return error;
  270. }
  271. static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  272. {
  273. return yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
  274. }
  275. static int yaffs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  276. bool n)
  277. {
  278. return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
  279. }
  280. static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
  281. struct dentry *dentry)
  282. {
  283. struct inode *inode = old_dentry->d_inode;
  284. struct yaffs_obj *obj = NULL;
  285. struct yaffs_obj *link = NULL;
  286. struct yaffs_dev *dev;
  287. yaffs_trace(YAFFS_TRACE_OS, "yaffs_link");
  288. obj = yaffs_inode_to_obj(inode);
  289. dev = obj->my_dev;
  290. yaffs_gross_lock(dev);
  291. if (!S_ISDIR(inode->i_mode)) /* Don't link directories */
  292. link =
  293. yaffs_link_obj(yaffs_inode_to_obj(dir), dentry->d_name.name,
  294. obj);
  295. if (link) {
  296. old_dentry->d_inode->__i_nlink = yaffs_get_obj_link_count(obj);
  297. d_instantiate(dentry, old_dentry->d_inode);
  298. atomic_inc(&old_dentry->d_inode->i_count);
  299. yaffs_trace(YAFFS_TRACE_OS,
  300. "yaffs_link link count %d i_count %d",
  301. old_dentry->d_inode->__i_nlink,
  302. atomic_read(&old_dentry->d_inode->i_count));
  303. }
  304. yaffs_gross_unlock(dev);
  305. if (link) {
  306. update_dir_time(dir);
  307. return 0;
  308. }
  309. return -EPERM;
  310. }
  311. static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
  312. const char *symname)
  313. {
  314. struct yaffs_obj *obj;
  315. struct yaffs_dev *dev;
  316. uid_t uid = current->cred->fsuid.val;
  317. gid_t gid =
  318. (dir->i_mode & S_ISGID) ? dir->i_gid.val : current->cred->fsgid.val;
  319. yaffs_trace(YAFFS_TRACE_OS, "yaffs_symlink");
  320. dev = yaffs_inode_to_obj(dir)->my_dev;
  321. yaffs_gross_lock(dev);
  322. obj = yaffs_create_symlink(yaffs_inode_to_obj(dir), dentry->d_name.name,
  323. S_IFLNK | S_IRWXUGO, uid, gid, symname);
  324. yaffs_gross_unlock(dev);
  325. if (obj) {
  326. struct inode *inode;
  327. inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
  328. d_instantiate(dentry, inode);
  329. update_dir_time(dir);
  330. yaffs_trace(YAFFS_TRACE_OS, "symlink created OK");
  331. yaffs_init_security(dentry->d_inode, dir, &dentry->d_name);
  332. return 0;
  333. }
  334. yaffs_trace(YAFFS_TRACE_OS, "symlink not created");
  335. return -ENOMEM;
  336. }
  337. static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
  338. unsigned int flags)
  339. {
  340. struct yaffs_obj *obj;
  341. struct inode *inode = NULL;
  342. struct yaffs_dev *dev = yaffs_inode_to_obj(dir)->my_dev;
  343. if (current != yaffs_dev_to_lc(dev)->readdir_process)
  344. yaffs_gross_lock(dev);
  345. yaffs_trace(YAFFS_TRACE_OS,
  346. "yaffs_lookup for %d:%s",
  347. yaffs_inode_to_obj(dir)->obj_id, dentry->d_name.name);
  348. obj = yaffs_find_by_name(yaffs_inode_to_obj(dir), dentry->d_name.name);
  349. obj = yaffs_get_equivalent_obj(obj); /* in case it was a hardlink */
  350. /* Can't hold gross lock when calling yaffs_get_inode() */
  351. if (current != yaffs_dev_to_lc(dev)->readdir_process)
  352. yaffs_gross_unlock(dev);
  353. if (obj) {
  354. yaffs_trace(YAFFS_TRACE_OS,
  355. "yaffs_lookup found %d", obj->obj_id);
  356. inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
  357. if (inode) {
  358. yaffs_trace(YAFFS_TRACE_OS, "yaffs_loookup dentry");
  359. d_add(dentry, inode);
  360. /* return dentry; */
  361. return NULL;
  362. }
  363. } else {
  364. yaffs_trace(YAFFS_TRACE_OS, "yaffs_lookup not found");
  365. }
  366. d_add(dentry, inode);
  367. return NULL;
  368. }
  369. static int yaffs_unlink(struct inode *dir, struct dentry *dentry)
  370. {
  371. int ret_val;
  372. struct yaffs_dev *dev;
  373. struct yaffs_obj *obj;
  374. yaffs_trace(YAFFS_TRACE_OS,
  375. "yaffs_unlink %d:%s",
  376. (int)(dir->i_ino), dentry->d_name.name);
  377. obj = yaffs_inode_to_obj(dir);
  378. dev = obj->my_dev;
  379. yaffs_gross_lock(dev);
  380. ret_val = yaffs_unlinker(obj, dentry->d_name.name);
  381. if (ret_val == YAFFS_OK) {
  382. dentry->d_inode->__i_nlink--;
  383. dir->i_version++;
  384. yaffs_gross_unlock(dev);
  385. mark_inode_dirty(dentry->d_inode);
  386. update_dir_time(dir);
  387. return 0;
  388. }
  389. yaffs_gross_unlock(dev);
  390. return -ENOTEMPTY;
  391. }
  392. static int yaffs_sync_object(struct file *file, loff_t start, loff_t end, int datasync)
  393. {
  394. struct yaffs_obj *obj;
  395. struct yaffs_dev *dev;
  396. struct dentry *dentry = file->f_path.dentry;
  397. obj = yaffs_dentry_to_obj(dentry);
  398. dev = obj->my_dev;
  399. yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC, "yaffs_sync_object");
  400. yaffs_gross_lock(dev);
  401. yaffs_flush_file(obj, 1, datasync);
  402. yaffs_gross_unlock(dev);
  403. return 0;
  404. }
  405. /*
  406. * The VFS layer already does all the dentry stuff for rename.
  407. *
  408. * NB: POSIX says you can rename an object over an old object of the same name
  409. */
  410. static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
  411. struct inode *new_dir, struct dentry *new_dentry)
  412. {
  413. struct yaffs_dev *dev;
  414. int ret_val = YAFFS_FAIL;
  415. struct yaffs_obj *target;
  416. yaffs_trace(YAFFS_TRACE_OS, "yaffs_rename");
  417. dev = yaffs_inode_to_obj(old_dir)->my_dev;
  418. yaffs_gross_lock(dev);
  419. /* Check if the target is an existing directory that is not empty. */
  420. target = yaffs_find_by_name(yaffs_inode_to_obj(new_dir),
  421. new_dentry->d_name.name);
  422. if (target && target->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY &&
  423. !list_empty(&target->variant.dir_variant.children)) {
  424. yaffs_trace(YAFFS_TRACE_OS, "target is non-empty dir");
  425. ret_val = YAFFS_FAIL;
  426. } else {
  427. /* Now does unlinking internally using shadowing mechanism */
  428. yaffs_trace(YAFFS_TRACE_OS, "calling yaffs_rename_obj");
  429. ret_val = yaffs_rename_obj(yaffs_inode_to_obj(old_dir),
  430. old_dentry->d_name.name,
  431. yaffs_inode_to_obj(new_dir),
  432. new_dentry->d_name.name);
  433. }
  434. yaffs_gross_unlock(dev);
  435. if (ret_val == YAFFS_OK) {
  436. if (target) {
  437. new_dentry->d_inode->__i_nlink--;
  438. mark_inode_dirty(new_dentry->d_inode);
  439. }
  440. update_dir_time(old_dir);
  441. if (old_dir != new_dir)
  442. update_dir_time(new_dir);
  443. return 0;
  444. } else {
  445. return -ENOTEMPTY;
  446. }
  447. }
  448. static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
  449. {
  450. struct inode *inode = dentry->d_inode;
  451. int error = 0;
  452. struct yaffs_dev *dev;
  453. yaffs_trace(YAFFS_TRACE_OS,
  454. "yaffs_setattr of object %d",
  455. yaffs_inode_to_obj(inode)->obj_id);
  456. /* Fail if a requested resize >= 2GB */
  457. if (attr->ia_valid & ATTR_SIZE && (attr->ia_size >> 32)) {
  458. pr_err("[yaffs_setattr] length %llx beyond 4GB\n", attr->ia_size);
  459. error = -EINVAL;
  460. }
  461. if (error == 0)
  462. error = inode_change_ok(inode, attr);
  463. if (error == 0) {
  464. int result;
  465. if (!error) {
  466. setattr_copy(inode, attr);
  467. yaffs_trace(YAFFS_TRACE_OS, "inode_setattr called");
  468. if (attr->ia_valid & ATTR_SIZE) {
  469. truncate_setsize(inode, attr->ia_size);
  470. inode->i_blocks = (inode->i_size + 511) >> 9;
  471. }
  472. }
  473. dev = yaffs_inode_to_obj(inode)->my_dev;
  474. if (attr->ia_valid & ATTR_SIZE) {
  475. yaffs_trace(YAFFS_TRACE_OS, "resize to %d(%x)",
  476. (int)(attr->ia_size),
  477. (int)(attr->ia_size));
  478. }
  479. yaffs_gross_lock(dev);
  480. result = yaffs_set_attribs(yaffs_inode_to_obj(inode), attr);
  481. if (result == YAFFS_OK)
  482. error = 0;
  483. else
  484. error = -EPERM;
  485. yaffs_gross_unlock(dev);
  486. }
  487. yaffs_trace(YAFFS_TRACE_OS, "yaffs_setattr done returning %d", error);
  488. return error;
  489. }
  490. #ifdef CONFIG_YAFFS_XATTR
  491. static int yaffs_setxattr(struct dentry *dentry, const char *name,
  492. const void *value, size_t size, int flags)
  493. {
  494. struct inode *inode = dentry->d_inode;
  495. return __setxattr(inode, name, value, size, flags);
  496. }
  497. static int __setxattr(struct inode *inode, const char *name,
  498. const void *value, size_t size, int flags)
  499. {
  500. int error = 0;
  501. struct yaffs_dev *dev;
  502. struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
  503. yaffs_trace(YAFFS_TRACE_OS, "yaffs_setxattr of object %d", obj->obj_id);
  504. if (error == 0) {
  505. int result;
  506. dev = obj->my_dev;
  507. yaffs_gross_lock(dev);
  508. result = yaffs_set_xattrib(obj, name, value, size, flags);
  509. if (result == YAFFS_OK)
  510. error = 0;
  511. else if (result < 0)
  512. error = result;
  513. yaffs_gross_unlock(dev);
  514. }
  515. yaffs_trace(YAFFS_TRACE_OS, "yaffs_setxattr done returning %d", error);
  516. return error;
  517. }
  518. static ssize_t yaffs_getxattr(struct dentry *dentry, const char *name, void *buff,
  519. size_t size)
  520. {
  521. struct inode *inode = dentry->d_inode;
  522. int error = 0;
  523. struct yaffs_dev *dev;
  524. struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
  525. yaffs_trace(YAFFS_TRACE_OS,
  526. "yaffs_getxattr \"%s\" from object %d",
  527. name, obj->obj_id);
  528. if (error == 0) {
  529. dev = obj->my_dev;
  530. yaffs_gross_lock(dev);
  531. error = yaffs_get_xattrib(obj, name, buff, size);
  532. yaffs_gross_unlock(dev);
  533. }
  534. yaffs_trace(YAFFS_TRACE_OS, "yaffs_getxattr done returning %d", error);
  535. return error;
  536. }
  537. static int yaffs_removexattr(struct dentry *dentry, const char *name)
  538. {
  539. struct inode *inode = dentry->d_inode;
  540. int error = 0;
  541. struct yaffs_dev *dev;
  542. struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
  543. yaffs_trace(YAFFS_TRACE_OS,
  544. "yaffs_removexattr of object %d", obj->obj_id);
  545. if (error == 0) {
  546. int result;
  547. dev = obj->my_dev;
  548. yaffs_gross_lock(dev);
  549. result = yaffs_remove_xattrib(obj, name);
  550. if (result == YAFFS_OK)
  551. error = 0;
  552. else if (result < 0)
  553. error = result;
  554. yaffs_gross_unlock(dev);
  555. }
  556. yaffs_trace(YAFFS_TRACE_OS,
  557. "yaffs_removexattr done returning %d", error);
  558. return error;
  559. }
  560. static ssize_t yaffs_listxattr(struct dentry *dentry, char *buff, size_t size)
  561. {
  562. struct inode *inode = dentry->d_inode;
  563. int error = 0;
  564. struct yaffs_dev *dev;
  565. struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
  566. yaffs_trace(YAFFS_TRACE_OS,
  567. "yaffs_listxattr of object %d", obj->obj_id);
  568. if (error == 0) {
  569. dev = obj->my_dev;
  570. yaffs_gross_lock(dev);
  571. error = yaffs_list_xattrib(obj, buff, size);
  572. yaffs_gross_unlock(dev);
  573. }
  574. yaffs_trace(YAFFS_TRACE_OS,
  575. "yaffs_listxattr done returning %d", error);
  576. return error;
  577. }
  578. #endif
  579. static const struct inode_operations yaffs_dir_inode_operations = {
  580. .create = yaffs_create,
  581. .lookup = yaffs_lookup,
  582. .link = yaffs_link,
  583. .unlink = yaffs_unlink,
  584. .symlink = yaffs_symlink,
  585. .mkdir = yaffs_mkdir,
  586. .rmdir = yaffs_unlink,
  587. .mknod = yaffs_mknod,
  588. .rename = yaffs_rename,
  589. .setattr = yaffs_setattr,
  590. #ifdef CONFIG_YAFFS_XATTR
  591. .setxattr = yaffs_setxattr,
  592. .getxattr = yaffs_getxattr,
  593. .listxattr = yaffs_listxattr,
  594. .removexattr = yaffs_removexattr,
  595. #endif
  596. };
  597. /*-----------------------------------------------------------------*/
  598. /* Directory search context allows us to unlock access to yaffs during
  599. * filldir without causing problems with the directory being modified.
  600. * This is similar to the tried and tested mechanism used in yaffs direct.
  601. *
  602. * A search context iterates along a doubly linked list of siblings in the
  603. * directory. If the iterating object is deleted then this would corrupt
  604. * the list iteration, likely causing a crash. The search context avoids
  605. * this by using the remove_obj_fn to move the search context to the
  606. * next object before the object is deleted.
  607. *
  608. * Many readdirs (and thus search conexts) may be alive simulateously so
  609. * each struct yaffs_dev has a list of these.
  610. *
  611. * A search context lives for the duration of a readdir.
  612. *
  613. * All these functions must be called while yaffs is locked.
  614. */
  615. struct yaffs_search_context {
  616. struct yaffs_dev *dev;
  617. struct yaffs_obj *dir_obj;
  618. struct yaffs_obj *next_return;
  619. struct list_head others;
  620. };
  621. /*
  622. * yaffs_new_search() creates a new search context, initialises it and
  623. * adds it to the device's search context list.
  624. *
  625. * Called at start of readdir.
  626. */
  627. static struct yaffs_search_context *yaffs_new_search(struct yaffs_obj *dir)
  628. {
  629. struct yaffs_dev *dev = dir->my_dev;
  630. struct yaffs_search_context *sc =
  631. kmalloc(sizeof(struct yaffs_search_context), GFP_NOFS);
  632. if (sc) {
  633. sc->dir_obj = dir;
  634. sc->dev = dev;
  635. if (list_empty(&sc->dir_obj->variant.dir_variant.children))
  636. sc->next_return = NULL;
  637. else
  638. sc->next_return =
  639. list_entry(dir->variant.dir_variant.children.next,
  640. struct yaffs_obj, siblings);
  641. INIT_LIST_HEAD(&sc->others);
  642. list_add(&sc->others, &(yaffs_dev_to_lc(dev)->search_contexts));
  643. }
  644. return sc;
  645. }
  646. /*
  647. * yaffs_search_end() disposes of a search context and cleans up.
  648. */
  649. static void yaffs_search_end(struct yaffs_search_context *sc)
  650. {
  651. if (sc) {
  652. list_del(&sc->others);
  653. kfree(sc);
  654. }
  655. }
  656. /*
  657. * yaffs_search_advance() moves a search context to the next object.
  658. * Called when the search iterates or when an object removal causes
  659. * the search context to be moved to the next object.
  660. */
  661. static void yaffs_search_advance(struct yaffs_search_context *sc)
  662. {
  663. if (!sc)
  664. return;
  665. if (sc->next_return == NULL ||
  666. list_empty(&sc->dir_obj->variant.dir_variant.children))
  667. sc->next_return = NULL;
  668. else {
  669. struct list_head *next = sc->next_return->siblings.next;
  670. if (next == &sc->dir_obj->variant.dir_variant.children)
  671. sc->next_return = NULL; /* end of list */
  672. else
  673. sc->next_return =
  674. list_entry(next, struct yaffs_obj, siblings);
  675. }
  676. }
  677. /*
  678. * yaffs_remove_obj_callback() is called when an object is unlinked.
  679. * We check open search contexts and advance any which are currently
  680. * on the object being iterated.
  681. */
  682. static void yaffs_remove_obj_callback(struct yaffs_obj *obj)
  683. {
  684. struct list_head *i;
  685. struct yaffs_search_context *sc;
  686. struct list_head *search_contexts =
  687. &(yaffs_dev_to_lc(obj->my_dev)->search_contexts);
  688. /* Iterate through the directory search contexts.
  689. * If any are currently on the object being removed, then advance
  690. * the search context to the next object to prevent a hanging pointer.
  691. */
  692. list_for_each(i, search_contexts) {
  693. if (i) {
  694. sc = list_entry(i, struct yaffs_search_context, others);
  695. if (sc->next_return == obj)
  696. yaffs_search_advance(sc);
  697. }
  698. }
  699. }
  700. static int yaffs_iterate(struct file *file, struct dir_context *ctx)
  701. {
  702. struct yaffs_obj *obj;
  703. struct yaffs_dev *dev;
  704. struct yaffs_search_context *sc;
  705. struct inode *inode = file->f_dentry->d_inode;
  706. unsigned long offset, curoffs;
  707. struct yaffs_obj *l;
  708. int ret_val = 0;
  709. char name[YAFFS_MAX_NAME_LENGTH + 1];
  710. obj = yaffs_dentry_to_obj(file->f_dentry);
  711. dev = obj->my_dev;
  712. yaffs_gross_lock(dev);
  713. yaffs_dev_to_lc(dev)->readdir_process = current;
  714. offset = ctx->pos;
  715. sc = yaffs_new_search(obj);
  716. if (!sc) {
  717. ret_val = -ENOMEM;
  718. goto out;
  719. }
  720. yaffs_trace(YAFFS_TRACE_OS,
  721. "yaffs_readdir: starting at %d", (int)offset);
  722. if (offset == 0) {
  723. yaffs_trace(YAFFS_TRACE_OS,
  724. "yaffs_readdir: entry . ino %d",
  725. (int)inode->i_ino);
  726. yaffs_gross_unlock(dev);
  727. if (!dir_emit_dot(file, ctx)) {
  728. yaffs_gross_lock(dev);
  729. goto out;
  730. }
  731. yaffs_gross_lock(dev);
  732. offset++;
  733. ctx->pos++;
  734. }
  735. if (offset == 1) {
  736. yaffs_trace(YAFFS_TRACE_OS,
  737. "yaffs_readdir: entry .. ino %d",
  738. (int)file->f_dentry->d_parent->d_inode->i_ino);
  739. yaffs_gross_unlock(dev);
  740. if (!dir_emit_dotdot(file, ctx)) {
  741. yaffs_gross_lock(dev);
  742. goto out;
  743. }
  744. yaffs_gross_lock(dev);
  745. offset++;
  746. ctx->pos++;
  747. }
  748. curoffs = 1;
  749. /* If the directory has changed since the open or last call to
  750. readdir, rewind to after the 2 canned entries. */
  751. if (file->f_version != inode->i_version) {
  752. offset = 2;
  753. ctx->pos = offset;
  754. file->f_version = inode->i_version;
  755. }
  756. while (sc->next_return) {
  757. curoffs++;
  758. l = sc->next_return;
  759. if (curoffs >= offset) {
  760. int this_inode = yaffs_get_obj_inode(l);
  761. int this_type = yaffs_get_obj_type(l);
  762. yaffs_get_obj_name(l, name, YAFFS_MAX_NAME_LENGTH + 1);
  763. yaffs_trace(YAFFS_TRACE_OS,
  764. "yaffs_readdir: %s inode %d",
  765. name, yaffs_get_obj_inode(l));
  766. yaffs_gross_unlock(dev);
  767. if (!dir_emit(ctx, name, strlen(name),
  768. this_inode, this_type) < 0) {
  769. yaffs_gross_lock(dev);
  770. goto out;
  771. }
  772. yaffs_gross_lock(dev);
  773. offset++;
  774. ctx->pos++;
  775. }
  776. yaffs_search_advance(sc);
  777. }
  778. out:
  779. yaffs_search_end(sc);
  780. yaffs_dev_to_lc(dev)->readdir_process = NULL;
  781. yaffs_gross_unlock(dev);
  782. return ret_val;
  783. }
  784. static const struct file_operations yaffs_dir_operations = {
  785. .read = generic_read_dir,
  786. .iterate = yaffs_iterate,
  787. .fsync = yaffs_sync_object,
  788. .llseek = generic_file_llseek,
  789. };
  790. static int yaffs_file_flush(struct file *file, fl_owner_t id)
  791. {
  792. struct yaffs_obj *obj = yaffs_dentry_to_obj(file->f_dentry);
  793. struct yaffs_dev *dev = obj->my_dev;
  794. yaffs_trace(YAFFS_TRACE_OS,
  795. "yaffs_file_flush object %d (%s)",
  796. obj->obj_id, obj->dirty ? "dirty" : "clean");
  797. yaffs_gross_lock(dev);
  798. yaffs_flush_file(obj, 1, 0);
  799. yaffs_gross_unlock(dev);
  800. return 0;
  801. }
  802. static const struct file_operations yaffs_file_operations = {
  803. .read = new_sync_read,
  804. .write = new_sync_write,
  805. .read_iter = generic_file_read_iter,
  806. .write_iter = generic_file_write_iter,
  807. .mmap = generic_file_mmap,
  808. .flush = yaffs_file_flush,
  809. .fsync = yaffs_sync_object,
  810. .splice_read = generic_file_splice_read,
  811. .splice_write = iter_file_splice_write,
  812. .llseek = generic_file_llseek,
  813. };
  814. /* ExportFS support */
  815. static struct inode *yaffs2_nfs_get_inode(struct super_block *sb, uint64_t ino,
  816. uint32_t generation)
  817. {
  818. return yaffs_iget(sb, ino);
  819. }
  820. static struct dentry *yaffs2_fh_to_dentry(struct super_block *sb,
  821. struct fid *fid, int fh_len,
  822. int fh_type)
  823. {
  824. return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
  825. yaffs2_nfs_get_inode);
  826. }
  827. static struct dentry *yaffs2_fh_to_parent(struct super_block *sb,
  828. struct fid *fid, int fh_len,
  829. int fh_type)
  830. {
  831. return generic_fh_to_parent(sb, fid, fh_len, fh_type,
  832. yaffs2_nfs_get_inode);
  833. }
  834. struct dentry *yaffs2_get_parent(struct dentry *dentry)
  835. {
  836. struct super_block *sb = dentry->d_inode->i_sb;
  837. struct dentry *parent = ERR_PTR(-ENOENT);
  838. struct inode *inode;
  839. unsigned long parent_ino;
  840. struct yaffs_obj *d_obj;
  841. struct yaffs_obj *parent_obj;
  842. d_obj = yaffs_inode_to_obj(dentry->d_inode);
  843. if (d_obj) {
  844. parent_obj = d_obj->parent;
  845. if (parent_obj) {
  846. parent_ino = yaffs_get_obj_inode(parent_obj);
  847. inode = yaffs_iget(sb, parent_ino);
  848. if (IS_ERR(inode)) {
  849. parent = ERR_CAST(inode);
  850. } else {
  851. parent = d_obtain_alias(inode);
  852. if (!IS_ERR(parent)) {
  853. parent = ERR_PTR(-ENOMEM);
  854. iput(inode);
  855. }
  856. }
  857. }
  858. }
  859. return parent;
  860. }
  861. /* Just declare a zero structure as a NULL value implies
  862. * using the default functions of exportfs.
  863. */
  864. static struct export_operations yaffs_export_ops = {
  865. .fh_to_dentry = yaffs2_fh_to_dentry,
  866. .fh_to_parent = yaffs2_fh_to_parent,
  867. .get_parent = yaffs2_get_parent,
  868. };
  869. /*-----------------------------------------------------------------*/
  870. /*
  871. static int yaffs_readlink(struct dentry *dentry, char __user *buffer,
  872. int buflen)
  873. {
  874. unsigned char *alias;
  875. int ret;
  876. struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev;
  877. yaffs_gross_lock(dev);
  878. alias = yaffs_get_symlink_alias(yaffs_dentry_to_obj(dentry));
  879. yaffs_gross_unlock(dev);
  880. if (!alias)
  881. return -ENOMEM;
  882. ret = readlink_copy(buffer, buflen, alias);
  883. kfree(alias);
  884. return ret;
  885. }
  886. */
  887. static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
  888. {
  889. unsigned char *alias;
  890. void *ret;
  891. struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev;
  892. yaffs_gross_lock(dev);
  893. alias = yaffs_get_symlink_alias(yaffs_dentry_to_obj(dentry));
  894. yaffs_gross_unlock(dev);
  895. if (!alias) {
  896. ret = ERR_PTR(-ENOMEM);
  897. goto out;
  898. }
  899. nd_set_link(nd, alias);
  900. ret = (void *)alias;
  901. out:
  902. return ret;
  903. }
  904. void yaffs_put_link(struct dentry *dentry, struct nameidata *nd, void *alias)
  905. {
  906. kfree(alias);
  907. }
  908. static void yaffs_unstitch_obj(struct inode *inode, struct yaffs_obj *obj)
  909. {
  910. /* Clear the association between the inode and
  911. * the struct yaffs_obj.
  912. */
  913. obj->my_inode = NULL;
  914. yaffs_inode_to_obj_lv(inode) = NULL;
  915. /* If the object freeing was deferred, then the real
  916. * free happens now.
  917. * This should fix the inode inconsistency problem.
  918. */
  919. yaffs_handle_defered_free(obj);
  920. }
  921. /* yaffs_evict_inode combines into one operation what was previously done in
  922. * yaffs_clear_inode() and yaffs_delete_inode()
  923. *
  924. */
  925. static void yaffs_evict_inode(struct inode *inode)
  926. {
  927. struct yaffs_obj *obj;
  928. struct yaffs_dev *dev;
  929. int deleteme = 0;
  930. obj = yaffs_inode_to_obj(inode);
  931. yaffs_trace(YAFFS_TRACE_OS,
  932. "yaffs_evict_inode: ino %d, count %d %s",
  933. (int)inode->i_ino,
  934. atomic_read(&inode->i_count),
  935. obj ? "object exists" : "null object");
  936. if (!inode->__i_nlink && !is_bad_inode(inode))
  937. deleteme = 1;
  938. truncate_inode_pages(&inode->i_data, 0);
  939. clear_inode(inode);
  940. if (deleteme && obj) {
  941. dev = obj->my_dev;
  942. yaffs_gross_lock(dev);
  943. yaffs_del_obj(obj);
  944. yaffs_gross_unlock(dev);
  945. }
  946. if (obj) {
  947. dev = obj->my_dev;
  948. yaffs_gross_lock(dev);
  949. yaffs_unstitch_obj(inode, obj);
  950. yaffs_gross_unlock(dev);
  951. }
  952. }
  953. static void yaffs_touch_super(struct yaffs_dev *dev)
  954. {
  955. yaffs_trace(YAFFS_TRACE_OS, "yaffs_touch_super() dev = %p", dev);
  956. dev->s_dirt = 1;
  957. }
  958. static int yaffs_readpage_nolock(struct file *f, struct page *pg)
  959. {
  960. /* Lifted from jffs2 */
  961. struct yaffs_obj *obj;
  962. unsigned char *pg_buf;
  963. int ret;
  964. struct yaffs_dev *dev;
  965. yaffs_trace(YAFFS_TRACE_OS,
  966. "yaffs_readpage_nolock at %08x, size %08x",
  967. (unsigned)(pg->index << PAGE_CACHE_SHIFT),
  968. (unsigned)PAGE_CACHE_SIZE);
  969. obj = yaffs_dentry_to_obj(f->f_dentry);
  970. dev = obj->my_dev;
  971. BUG_ON(!PageLocked(pg));
  972. pg_buf = kmap(pg);
  973. /* FIXME: Can kmap fail? */
  974. yaffs_gross_lock(dev);
  975. ret = yaffs_file_rd(obj, pg_buf,
  976. pg->index << PAGE_CACHE_SHIFT, PAGE_CACHE_SIZE);
  977. yaffs_gross_unlock(dev);
  978. if (ret >= 0)
  979. ret = 0;
  980. if (ret) {
  981. ClearPageUptodate(pg);
  982. SetPageError(pg);
  983. } else {
  984. SetPageUptodate(pg);
  985. ClearPageError(pg);
  986. }
  987. flush_dcache_page(pg);
  988. kunmap(pg);
  989. yaffs_trace(YAFFS_TRACE_OS, "yaffs_readpage_nolock done");
  990. return ret;
  991. }
  992. static int yaffs_readpage_unlock(struct file *f, struct page *pg)
  993. {
  994. int ret = yaffs_readpage_nolock(f, pg);
  995. UnlockPage(pg);
  996. return ret;
  997. }
  998. static int yaffs_readpage(struct file *f, struct page *pg)
  999. {
  1000. int ret;
  1001. yaffs_trace(YAFFS_TRACE_OS, "yaffs_readpage");
  1002. ret = yaffs_readpage_unlock(f, pg);
  1003. yaffs_trace(YAFFS_TRACE_OS, "yaffs_readpage done");
  1004. return ret;
  1005. }
  1006. /* writepage inspired by/stolen from smbfs */
  1007. static int yaffs_writepage(struct page *page, struct writeback_control *wbc)
  1008. {
  1009. struct yaffs_dev *dev;
  1010. struct address_space *mapping = page->mapping;
  1011. struct inode *inode;
  1012. unsigned long end_index;
  1013. char *buffer;
  1014. struct yaffs_obj *obj;
  1015. int n_written = 0;
  1016. unsigned n_bytes;
  1017. loff_t i_size;
  1018. if (!mapping)
  1019. BUG();
  1020. inode = mapping->host;
  1021. if (!inode)
  1022. BUG();
  1023. i_size = i_size_read(inode);
  1024. end_index = i_size >> PAGE_CACHE_SHIFT;
  1025. if (page->index < end_index)
  1026. n_bytes = PAGE_CACHE_SIZE;
  1027. else {
  1028. n_bytes = i_size & (PAGE_CACHE_SIZE - 1);
  1029. if (page->index > end_index || !n_bytes) {
  1030. yaffs_trace(YAFFS_TRACE_OS,
  1031. "yaffs_writepage at %08x, inode size = %08x!!!",
  1032. (unsigned)(page->index << PAGE_CACHE_SHIFT),
  1033. (unsigned)inode->i_size);
  1034. yaffs_trace(YAFFS_TRACE_OS,
  1035. " -> don't care!!");
  1036. zero_user_segment(page, 0, PAGE_CACHE_SIZE);
  1037. set_page_writeback(page);
  1038. unlock_page(page);
  1039. end_page_writeback(page);
  1040. return 0;
  1041. }
  1042. }
  1043. if (n_bytes != PAGE_CACHE_SIZE)
  1044. zero_user_segment(page, n_bytes, PAGE_CACHE_SIZE);
  1045. get_page(page);
  1046. buffer = kmap(page);
  1047. obj = yaffs_inode_to_obj(inode);
  1048. dev = obj->my_dev;
  1049. yaffs_gross_lock(dev);
  1050. yaffs_trace(YAFFS_TRACE_OS,
  1051. "yaffs_writepage at %08x, size %08x",
  1052. (unsigned)(page->index << PAGE_CACHE_SHIFT), n_bytes);
  1053. yaffs_trace(YAFFS_TRACE_OS,
  1054. "writepag0: obj = %05x, ino = %05x",
  1055. (int)obj->variant.file_variant.file_size, (int)inode->i_size);
  1056. n_written = yaffs_wr_file(obj, buffer,
  1057. page->index << PAGE_CACHE_SHIFT, n_bytes, 0);
  1058. yaffs_touch_super(dev);
  1059. yaffs_trace(YAFFS_TRACE_OS,
  1060. "writepag1: obj = %05x, ino = %05x",
  1061. (int)obj->variant.file_variant.file_size, (int)inode->i_size);
  1062. yaffs_gross_unlock(dev);
  1063. kunmap(page);
  1064. set_page_writeback(page);
  1065. unlock_page(page);
  1066. end_page_writeback(page);
  1067. put_page(page);
  1068. return (n_written == n_bytes) ? 0 : -ENOSPC;
  1069. }
  1070. /* Space holding and freeing is done to ensure we have space available for
  1071. * write_begin/end.
  1072. * For now we just assume few parallel writes and check against a small
  1073. * number.
  1074. * Todo: need to do this with a counter to handle parallel reads better.
  1075. */
  1076. static ssize_t yaffs_hold_space(struct file *f)
  1077. {
  1078. struct yaffs_obj *obj;
  1079. struct yaffs_dev *dev;
  1080. int n_free_chunks;
  1081. obj = yaffs_dentry_to_obj(f->f_dentry);
  1082. dev = obj->my_dev;
  1083. yaffs_gross_lock(dev);
  1084. n_free_chunks = yaffs_get_n_free_chunks(dev);
  1085. yaffs_gross_unlock(dev);
  1086. return (n_free_chunks > 20) ? 1 : 0;
  1087. }
  1088. static void yaffs_release_space(struct file *f)
  1089. {
  1090. struct yaffs_obj *obj;
  1091. struct yaffs_dev *dev;
  1092. obj = yaffs_dentry_to_obj(f->f_dentry);
  1093. dev = obj->my_dev;
  1094. yaffs_gross_lock(dev);
  1095. yaffs_gross_unlock(dev);
  1096. }
  1097. static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
  1098. loff_t pos, unsigned len, unsigned flags,
  1099. struct page **pagep, void **fsdata)
  1100. {
  1101. struct page *pg = NULL;
  1102. pgoff_t index = pos >> PAGE_CACHE_SHIFT;
  1103. int ret = 0;
  1104. int space_held = 0;
  1105. /* Get a page */
  1106. pg = grab_cache_page_write_begin(mapping, index, flags);
  1107. *pagep = pg;
  1108. if (!pg) {
  1109. ret = -ENOMEM;
  1110. goto out;
  1111. }
  1112. yaffs_trace(YAFFS_TRACE_OS,
  1113. "start yaffs_write_begin index %d(%x) uptodate %d",
  1114. (int)index, (int)index, Page_Uptodate(pg) ? 1 : 0);
  1115. /* Get fs space */
  1116. space_held = yaffs_hold_space(filp);
  1117. if (!space_held) {
  1118. ret = -ENOSPC;
  1119. goto out;
  1120. }
  1121. /* Update page if required */
  1122. if (!Page_Uptodate(pg))
  1123. ret = yaffs_readpage_nolock(filp, pg);
  1124. if (ret)
  1125. goto out;
  1126. /* Happy path return */
  1127. yaffs_trace(YAFFS_TRACE_OS, "end yaffs_write_begin - ok");
  1128. return 0;
  1129. out:
  1130. yaffs_trace(YAFFS_TRACE_OS,
  1131. "end yaffs_write_begin fail returning %d", ret);
  1132. if (space_held)
  1133. yaffs_release_space(filp);
  1134. if (pg) {
  1135. unlock_page(pg);
  1136. page_cache_release(pg);
  1137. }
  1138. return ret;
  1139. }
  1140. static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
  1141. loff_t *pos)
  1142. {
  1143. struct yaffs_obj *obj;
  1144. int n_written, ipos;
  1145. struct inode *inode;
  1146. struct yaffs_dev *dev;
  1147. obj = yaffs_dentry_to_obj(f->f_dentry);
  1148. dev = obj->my_dev;
  1149. yaffs_gross_lock(dev);
  1150. inode = f->f_dentry->d_inode;
  1151. if (!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND)
  1152. ipos = inode->i_size;
  1153. else
  1154. ipos = *pos;
  1155. if (!obj)
  1156. yaffs_trace(YAFFS_TRACE_OS,
  1157. "yaffs_file_write: hey obj is null!");
  1158. else
  1159. yaffs_trace(YAFFS_TRACE_OS,
  1160. "yaffs_file_write about to write writing %u(%x) bytes to object %d at %d(%x)",
  1161. (unsigned)n, (unsigned)n, obj->obj_id, ipos, ipos);
  1162. n_written = yaffs_wr_file(obj, buf, ipos, n, 0);
  1163. yaffs_touch_super(dev);
  1164. yaffs_trace(YAFFS_TRACE_OS,
  1165. "yaffs_file_write: %d(%x) bytes written",
  1166. (unsigned)n, (unsigned)n);
  1167. if (n_written > 0) {
  1168. ipos += n_written;
  1169. *pos = ipos;
  1170. if (ipos > inode->i_size) {
  1171. inode->i_size = ipos;
  1172. inode->i_blocks = (ipos + 511) >> 9;
  1173. yaffs_trace(YAFFS_TRACE_OS,
  1174. "yaffs_file_write size updated to %d bytes, %d blocks",
  1175. ipos, (int)(inode->i_blocks));
  1176. }
  1177. }
  1178. yaffs_gross_unlock(dev);
  1179. return (n_written == 0) && (n > 0) ? -ENOSPC : n_written;
  1180. }
  1181. static int yaffs_write_end(struct file *filp, struct address_space *mapping,
  1182. loff_t pos, unsigned len, unsigned copied,
  1183. struct page *pg, void *fsdadata)
  1184. {
  1185. int ret = 0;
  1186. void *addr, *kva;
  1187. uint32_t offset_into_page = pos & (PAGE_CACHE_SIZE - 1);
  1188. kva = kmap(pg);
  1189. addr = kva + offset_into_page;
  1190. yaffs_trace(YAFFS_TRACE_OS,
  1191. "yaffs_write_end addr %p pos %x n_bytes %d",
  1192. addr, (unsigned)pos, copied);
  1193. ret = yaffs_file_write(filp, addr, copied, &pos);
  1194. if (ret != copied) {
  1195. yaffs_trace(YAFFS_TRACE_OS,
  1196. "yaffs_write_end not same size ret %d copied %d",
  1197. ret, copied);
  1198. SetPageError(pg);
  1199. }
  1200. kunmap(pg);
  1201. yaffs_release_space(filp);
  1202. unlock_page(pg);
  1203. page_cache_release(pg);
  1204. return ret;
  1205. }
  1206. static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf)
  1207. {
  1208. struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev;
  1209. struct super_block *sb = dentry->d_sb;
  1210. yaffs_trace(YAFFS_TRACE_OS, "yaffs_statfs");
  1211. yaffs_gross_lock(dev);
  1212. buf->f_type = YAFFS_MAGIC;
  1213. buf->f_bsize = sb->s_blocksize;
  1214. buf->f_namelen = 255;
  1215. if (dev->data_bytes_per_chunk & (dev->data_bytes_per_chunk - 1)) {
  1216. /* Do this if chunk size is not a power of 2 */
  1217. uint64_t bytes_in_dev;
  1218. uint64_t bytes_free;
  1219. bytes_in_dev =
  1220. ((uint64_t)
  1221. ((dev->param.end_block - dev->param.start_block +
  1222. 1))) * ((uint64_t) (dev->param.chunks_per_block *
  1223. dev->data_bytes_per_chunk));
  1224. do_div(bytes_in_dev, sb->s_blocksize); /* bytes_in_dev becomes the number of blocks */
  1225. buf->f_blocks = bytes_in_dev;
  1226. bytes_free = ((uint64_t) (yaffs_get_n_free_chunks(dev))) *
  1227. ((uint64_t) (dev->data_bytes_per_chunk));
  1228. do_div(bytes_free, sb->s_blocksize);
  1229. buf->f_bfree = bytes_free;
  1230. } else if (sb->s_blocksize > dev->data_bytes_per_chunk) {
  1231. buf->f_blocks =
  1232. (dev->param.end_block - dev->param.start_block + 1) *
  1233. dev->param.chunks_per_block /
  1234. (sb->s_blocksize / dev->data_bytes_per_chunk);
  1235. buf->f_bfree =
  1236. yaffs_get_n_free_chunks(dev) /
  1237. (sb->s_blocksize / dev->data_bytes_per_chunk);
  1238. } else {
  1239. buf->f_blocks =
  1240. (dev->param.end_block - dev->param.start_block + 1) *
  1241. dev->param.chunks_per_block *
  1242. (dev->data_bytes_per_chunk / sb->s_blocksize);
  1243. buf->f_bfree =
  1244. yaffs_get_n_free_chunks(dev) *
  1245. (dev->data_bytes_per_chunk / sb->s_blocksize);
  1246. }
  1247. buf->f_files = 0;
  1248. buf->f_ffree = 0;
  1249. buf->f_bavail = buf->f_bfree;
  1250. yaffs_gross_unlock(dev);
  1251. return 0;
  1252. }
  1253. static void yaffs_flush_inodes(struct super_block *sb)
  1254. {
  1255. struct inode *iptr;
  1256. struct yaffs_obj *obj;
  1257. list_for_each_entry(iptr, &sb->s_inodes, i_sb_list) {
  1258. obj = yaffs_inode_to_obj(iptr);
  1259. if (obj) {
  1260. yaffs_trace(YAFFS_TRACE_OS,
  1261. "flushing obj %d", obj->obj_id);
  1262. yaffs_flush_file(obj, 1, 0);
  1263. }
  1264. }
  1265. }
  1266. static void yaffs_flush_super(struct super_block *sb, int do_checkpoint)
  1267. {
  1268. struct yaffs_dev *dev = yaffs_super_to_dev(sb);
  1269. if (!dev)
  1270. return;
  1271. yaffs_flush_inodes(sb);
  1272. yaffs_update_dirty_dirs(dev);
  1273. yaffs_flush_whole_cache(dev);
  1274. if (do_checkpoint)
  1275. yaffs_checkpoint_save(dev);
  1276. }
  1277. static unsigned yaffs_bg_gc_urgency(struct yaffs_dev *dev)
  1278. {
  1279. unsigned erased_chunks =
  1280. dev->n_erased_blocks * dev->param.chunks_per_block;
  1281. struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
  1282. unsigned scattered = 0; /* Free chunks not in an erased block */
  1283. if (erased_chunks < dev->n_free_chunks)
  1284. scattered = (dev->n_free_chunks - erased_chunks);
  1285. if (!context->bg_running)
  1286. return 0;
  1287. else if (scattered < (dev->param.chunks_per_block * 2))
  1288. return 0;
  1289. else if (erased_chunks > dev->n_free_chunks / 2)
  1290. return 0;
  1291. else if (erased_chunks > dev->n_free_chunks / 4)
  1292. return 1;
  1293. else
  1294. return 2;
  1295. }
  1296. static int yaffs_do_sync_fs(struct super_block *sb, int request_checkpoint)
  1297. {
  1298. struct yaffs_dev *dev = yaffs_super_to_dev(sb);
  1299. unsigned int oneshot_checkpoint = (yaffs_auto_checkpoint & 4);
  1300. unsigned gc_urgent = yaffs_bg_gc_urgency(dev);
  1301. int do_checkpoint;
  1302. yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND,
  1303. "yaffs_do_sync_fs: gc-urgency %d %s %s%s",
  1304. gc_urgent,
  1305. dev->s_dirt ? "dirty" : "clean",
  1306. request_checkpoint ? "checkpoint requested" : "no checkpoint",
  1307. oneshot_checkpoint ? " one-shot" : "");
  1308. yaffs_gross_lock(dev);
  1309. do_checkpoint = ((request_checkpoint && !gc_urgent) ||
  1310. oneshot_checkpoint) && !dev->is_checkpointed;
  1311. if (dev->s_dirt || do_checkpoint) {
  1312. yaffs_flush_super(sb, !dev->is_checkpointed && do_checkpoint);
  1313. dev->s_dirt = 0;
  1314. if (oneshot_checkpoint)
  1315. yaffs_auto_checkpoint &= ~4;
  1316. }
  1317. yaffs_gross_unlock(dev);
  1318. return 0;
  1319. }
  1320. /*
  1321. * yaffs background thread functions .
  1322. * yaffs_bg_thread_fn() the thread function
  1323. * yaffs_bg_start() launches the background thread.
  1324. * yaffs_bg_stop() cleans up the background thread.
  1325. *
  1326. * NB:
  1327. * The thread should only run after the yaffs is initialised
  1328. * The thread should be stopped before yaffs is unmounted.
  1329. * The thread should not do any writing while the fs is in read only.
  1330. */
  1331. void yaffs_background_waker(unsigned long data)
  1332. {
  1333. wake_up_process((struct task_struct *)data);
  1334. }
  1335. static int yaffs_bg_thread_fn(void *data)
  1336. {
  1337. struct yaffs_dev *dev = (struct yaffs_dev *)data;
  1338. struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
  1339. unsigned long now = jiffies;
  1340. unsigned long next_dir_update = now;
  1341. unsigned long next_gc = now;
  1342. unsigned long expires;
  1343. unsigned int urgency;
  1344. int gc_result;
  1345. struct timer_list timer;
  1346. yaffs_trace(YAFFS_TRACE_BACKGROUND,
  1347. "yaffs_background starting for dev %p", (void *)dev);
  1348. set_freezable();
  1349. while (context->bg_running) {
  1350. yaffs_trace(YAFFS_TRACE_BACKGROUND, "yaffs_background");
  1351. if (kthread_should_stop())
  1352. break;
  1353. if (try_to_freeze())
  1354. continue;
  1355. yaffs_gross_lock(dev);
  1356. now = jiffies;
  1357. if (time_after(now, next_dir_update) && yaffs_bg_enable) {
  1358. yaffs_update_dirty_dirs(dev);
  1359. next_dir_update = now + HZ;
  1360. }
  1361. if (time_after(now, next_gc) && yaffs_bg_enable) {
  1362. if (!dev->is_checkpointed) {
  1363. urgency = yaffs_bg_gc_urgency(dev);
  1364. gc_result = yaffs_bg_gc(dev, urgency);
  1365. if (urgency > 1)
  1366. next_gc = now + HZ / 20 + 1;
  1367. else if (urgency > 0)
  1368. next_gc = now + HZ / 10 + 1;
  1369. else
  1370. next_gc = now + HZ * 2;
  1371. } else {
  1372. /*
  1373. * gc not running so set to next_dir_update
  1374. * to cut down on wake ups
  1375. */
  1376. next_gc = next_dir_update;
  1377. }
  1378. }
  1379. yaffs_gross_unlock(dev);
  1380. expires = next_dir_update;
  1381. if (time_before(next_gc, expires))
  1382. expires = next_gc;
  1383. if (time_before(expires, now))
  1384. expires = now + HZ;
  1385. Y_INIT_TIMER(&timer);
  1386. timer.expires = expires + 1;
  1387. timer.data = (unsigned long)current;
  1388. timer.function = yaffs_background_waker;
  1389. set_current_state(TASK_INTERRUPTIBLE);
  1390. add_timer(&timer);
  1391. schedule();
  1392. del_timer_sync(&timer);
  1393. }
  1394. return 0;
  1395. }
  1396. static int yaffs_bg_start(struct yaffs_dev *dev)
  1397. {
  1398. int retval = 0;
  1399. struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
  1400. if (dev->read_only)
  1401. return -1;
  1402. context->bg_running = 1;
  1403. context->bg_thread = kthread_run(yaffs_bg_thread_fn,
  1404. (void *)dev, "yaffs-bg-%d",
  1405. context->mount_id);
  1406. if (IS_ERR(context->bg_thread)) {
  1407. retval = PTR_ERR(context->bg_thread);
  1408. context->bg_thread = NULL;
  1409. context->bg_running = 0;
  1410. }
  1411. return retval;
  1412. }
  1413. static void yaffs_bg_stop(struct yaffs_dev *dev)
  1414. {
  1415. struct yaffs_linux_context *ctxt = yaffs_dev_to_lc(dev);
  1416. ctxt->bg_running = 0;
  1417. if (ctxt->bg_thread) {
  1418. kthread_stop(ctxt->bg_thread);
  1419. ctxt->bg_thread = NULL;
  1420. }
  1421. }
  1422. /*
  1423. static void yaffs_write_super(struct super_block *sb)
  1424. {
  1425. unsigned request_checkpoint = (yaffs_auto_checkpoint >= 2);
  1426. yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND,
  1427. "yaffs_write_super%s",
  1428. request_checkpoint ? " checkpt" : "");
  1429. yaffs_do_sync_fs(sb, request_checkpoint);
  1430. }
  1431. */
  1432. static int yaffs_sync_fs(struct super_block *sb, int wait)
  1433. {
  1434. unsigned request_checkpoint = (yaffs_auto_checkpoint >= 1);
  1435. yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC,
  1436. "yaffs_sync_fs%s", request_checkpoint ? " checkpt" : "");
  1437. yaffs_do_sync_fs(sb, request_checkpoint);
  1438. return 0;
  1439. }
  1440. static LIST_HEAD(yaffs_context_list);
  1441. struct mutex yaffs_context_lock;
  1442. struct yaffs_options {
  1443. int inband_tags;
  1444. int skip_checkpoint_read;
  1445. int skip_checkpoint_write;
  1446. int no_cache;
  1447. int tags_ecc_on;
  1448. int tags_ecc_overridden;
  1449. int lazy_loading_enabled;
  1450. int lazy_loading_overridden;
  1451. int empty_lost_and_found;
  1452. int empty_lost_and_found_overridden;
  1453. };
  1454. #define MAX_OPT_LEN 30
  1455. static int yaffs_parse_options(struct yaffs_options *options,
  1456. const char *options_str)
  1457. {
  1458. char cur_opt[MAX_OPT_LEN + 1];
  1459. int p;
  1460. int error = 0;
  1461. /* Parse through the options which is a comma separated list */
  1462. while (options_str && *options_str && !error) {
  1463. memset(cur_opt, 0, MAX_OPT_LEN + 1);
  1464. p = 0;
  1465. while (*options_str == ',')
  1466. options_str++;
  1467. while (*options_str && *options_str != ',') {
  1468. if (p < MAX_OPT_LEN) {
  1469. cur_opt[p] = *options_str;
  1470. p++;
  1471. }
  1472. options_str++;
  1473. }
  1474. if (!strcmp(cur_opt, "inband-tags")) {
  1475. options->inband_tags = 1;
  1476. } else if (!strcmp(cur_opt, "tags-ecc-off")) {
  1477. options->tags_ecc_on = 0;
  1478. options->tags_ecc_overridden = 1;
  1479. } else if (!strcmp(cur_opt, "tags-ecc-on")) {
  1480. options->tags_ecc_on = 1;
  1481. options->tags_ecc_overridden = 1;
  1482. } else if (!strcmp(cur_opt, "lazy-loading-off")) {
  1483. options->lazy_loading_enabled = 0;
  1484. options->lazy_loading_overridden = 1;
  1485. } else if (!strcmp(cur_opt, "lazy-loading-on")) {
  1486. options->lazy_loading_enabled = 1;
  1487. options->lazy_loading_overridden = 1;
  1488. } else if (!strcmp(cur_opt, "empty-lost-and-found-off")) {
  1489. options->empty_lost_and_found = 0;
  1490. options->empty_lost_and_found_overridden = 1;
  1491. } else if (!strcmp(cur_opt, "empty-lost-and-found-on")) {
  1492. options->empty_lost_and_found = 1;
  1493. options->empty_lost_and_found_overridden = 1;
  1494. } else if (!strcmp(cur_opt, "no-cache")) {
  1495. options->no_cache = 1;
  1496. } else if (!strcmp(cur_opt, "no-checkpoint-read")) {
  1497. options->skip_checkpoint_read = 1;
  1498. } else if (!strcmp(cur_opt, "no-checkpoint-write")) {
  1499. options->skip_checkpoint_write = 1;
  1500. } else if (!strcmp(cur_opt, "no-checkpoint")) {
  1501. options->skip_checkpoint_read = 1;
  1502. options->skip_checkpoint_write = 1;
  1503. } else {
  1504. pr_info("yaffs: Bad mount option \"%s\"\n",
  1505. cur_opt);
  1506. error = 1;
  1507. }
  1508. }
  1509. return error;
  1510. }
  1511. static const struct address_space_operations yaffs_file_address_operations = {
  1512. .readpage = yaffs_readpage,
  1513. .writepage = yaffs_writepage,
  1514. .write_begin = yaffs_write_begin,
  1515. .write_end = yaffs_write_end,
  1516. };
  1517. static const struct inode_operations yaffs_file_inode_operations = {
  1518. .setattr = yaffs_setattr,
  1519. #ifdef CONFIG_YAFFS_XATTR
  1520. .setxattr = yaffs_setxattr,
  1521. .getxattr = yaffs_getxattr,
  1522. .listxattr = yaffs_listxattr,
  1523. .removexattr = yaffs_removexattr,
  1524. #endif
  1525. };
  1526. static const struct inode_operations yaffs_symlink_inode_operations = {
  1527. .readlink = generic_readlink,
  1528. .follow_link = yaffs_follow_link,
  1529. .put_link = yaffs_put_link,
  1530. .setattr = yaffs_setattr,
  1531. #ifdef CONFIG_YAFFS_XATTR
  1532. .setxattr = yaffs_setxattr,
  1533. .getxattr = yaffs_getxattr,
  1534. .listxattr = yaffs_listxattr,
  1535. .removexattr = yaffs_removexattr,
  1536. #endif
  1537. };
  1538. static void yaffs_fill_inode_from_obj(struct inode *inode,
  1539. struct yaffs_obj *obj)
  1540. {
  1541. if (inode && obj) {
  1542. /* Check mode against the variant type and attempt to repair if broken. */
  1543. u32 mode = obj->yst_mode;
  1544. switch (obj->variant_type) {
  1545. case YAFFS_OBJECT_TYPE_FILE:
  1546. if (!S_ISREG(mode)) {
  1547. obj->yst_mode &= ~S_IFMT;
  1548. obj->yst_mode |= S_IFREG;
  1549. }
  1550. break;
  1551. case YAFFS_OBJECT_TYPE_SYMLINK:
  1552. if (!S_ISLNK(mode)) {
  1553. obj->yst_mode &= ~S_IFMT;
  1554. obj->yst_mode |= S_IFLNK;
  1555. }
  1556. break;
  1557. case YAFFS_OBJECT_TYPE_DIRECTORY:
  1558. if (!S_ISDIR(mode)) {
  1559. obj->yst_mode &= ~S_IFMT;
  1560. obj->yst_mode |= S_IFDIR;
  1561. }
  1562. break;
  1563. case YAFFS_OBJECT_TYPE_UNKNOWN:
  1564. case YAFFS_OBJECT_TYPE_HARDLINK:
  1565. case YAFFS_OBJECT_TYPE_SPECIAL:
  1566. default:
  1567. /* TODO? */
  1568. break;
  1569. }
  1570. inode->i_flags |= S_NOATIME;
  1571. inode->i_ino = obj->obj_id;
  1572. inode->i_mode = obj->yst_mode;
  1573. inode->i_uid = KUIDT_INIT(obj->yst_uid);
  1574. inode->i_gid = KGIDT_INIT(obj->yst_gid);
  1575. inode->i_rdev = old_decode_dev(obj->yst_rdev);
  1576. inode->i_atime.tv_sec = (time_t) (obj->yst_atime);
  1577. inode->i_atime.tv_nsec = 0;
  1578. inode->i_mtime.tv_sec = (time_t) obj->yst_mtime;
  1579. inode->i_mtime.tv_nsec = 0;
  1580. inode->i_ctime.tv_sec = (time_t) obj->yst_ctime;
  1581. inode->i_ctime.tv_nsec = 0;
  1582. inode->i_size = yaffs_get_obj_length(obj);
  1583. inode->i_blocks = (inode->i_size + 511) >> 9;
  1584. inode->__i_nlink = yaffs_get_obj_link_count(obj);
  1585. yaffs_trace(YAFFS_TRACE_OS,
  1586. "yaffs_fill_inode mode %x uid %d gid %d size %d count %d",
  1587. inode->i_mode, inode->i_uid.val, inode->i_gid.val,
  1588. (int)inode->i_size, atomic_read(&inode->i_count));
  1589. switch (obj->yst_mode & S_IFMT) {
  1590. default: /* fifo, device or socket */
  1591. init_special_inode(inode, obj->yst_mode,
  1592. old_decode_dev(obj->yst_rdev));
  1593. break;
  1594. case S_IFREG: /* file */
  1595. inode->i_op = &yaffs_file_inode_operations;
  1596. inode->i_fop = &yaffs_file_operations;
  1597. inode->i_mapping->a_ops =
  1598. &yaffs_file_address_operations;
  1599. break;
  1600. case S_IFDIR: /* directory */
  1601. inode->i_op = &yaffs_dir_inode_operations;
  1602. inode->i_fop = &yaffs_dir_operations;
  1603. break;
  1604. case S_IFLNK: /* symlink */
  1605. inode->i_op = &yaffs_symlink_inode_operations;
  1606. break;
  1607. }
  1608. yaffs_inode_to_obj_lv(inode) = obj;
  1609. obj->my_inode = inode;
  1610. } else {
  1611. yaffs_trace(YAFFS_TRACE_OS,
  1612. "yaffs_fill_inode invalid parameters");
  1613. }
  1614. }
  1615. static void yaffs_put_super(struct super_block *sb)
  1616. {
  1617. struct yaffs_dev *dev = yaffs_super_to_dev(sb);
  1618. yaffs_trace(YAFFS_TRACE_OS, "yaffs_put_super");
  1619. yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND,
  1620. "Shutting down yaffs background thread");
  1621. yaffs_bg_stop(dev);
  1622. yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND,
  1623. "yaffs background thread shut down");
  1624. yaffs_gross_lock(dev);
  1625. yaffs_flush_super(sb, 1);
  1626. if (yaffs_dev_to_lc(dev)->put_super_fn)
  1627. yaffs_dev_to_lc(dev)->put_super_fn(sb);
  1628. yaffs_deinitialise(dev);
  1629. yaffs_gross_unlock(dev);
  1630. mutex_lock(&yaffs_context_lock);
  1631. list_del_init(&(yaffs_dev_to_lc(dev)->context_list));
  1632. mutex_unlock(&yaffs_context_lock);
  1633. if (yaffs_dev_to_lc(dev)->spare_buffer) {
  1634. kfree(yaffs_dev_to_lc(dev)->spare_buffer);
  1635. yaffs_dev_to_lc(dev)->spare_buffer = NULL;
  1636. }
  1637. kfree(dev);
  1638. }
  1639. static void yaffs_mtd_put_super(struct super_block *sb)
  1640. {
  1641. struct mtd_info *mtd = yaffs_dev_to_mtd(yaffs_super_to_dev(sb));
  1642. mtd_sync(mtd);
  1643. put_mtd_device(mtd);
  1644. }
  1645. static const struct super_operations yaffs_super_ops = {
  1646. .statfs = yaffs_statfs,
  1647. .put_super = yaffs_put_super,
  1648. .evict_inode = yaffs_evict_inode,
  1649. .sync_fs = yaffs_sync_fs,
  1650. /*.write_super = yaffs_write_super,*/
  1651. };
  1652. static struct super_block *yaffs_internal_read_super(int yaffs_version,
  1653. struct super_block *sb,
  1654. void *data, int silent)
  1655. {
  1656. int n_blocks;
  1657. struct inode *inode = NULL;
  1658. struct dentry *root;
  1659. struct yaffs_dev *dev = 0;
  1660. char devname_buf[BDEVNAME_SIZE + 1];
  1661. struct mtd_info *mtd;
  1662. int err;
  1663. char *data_str = (char *)data;
  1664. struct yaffs_linux_context *context = NULL;
  1665. struct yaffs_param *param;
  1666. int yaffs_mvg_test_debug_mode = 0;
  1667. int read_only = 0;
  1668. struct yaffs_options options;
  1669. unsigned mount_id;
  1670. int found;
  1671. struct yaffs_linux_context *context_iterator;
  1672. struct list_head *l;
  1673. sb->s_magic = YAFFS_MAGIC;
  1674. sb->s_op = &yaffs_super_ops;
  1675. sb->s_flags |= MS_NOATIME;
  1676. read_only = ((sb->s_flags & MS_RDONLY) != 0);
  1677. sb->s_export_op = &yaffs_export_ops;
  1678. if (!sb)
  1679. pr_info("yaffs: sb is NULL\n");
  1680. else if (!sb->s_dev)
  1681. pr_info("yaffs: sb->s_dev is NULL\n");
  1682. else if (!yaffs_devname(sb, devname_buf))
  1683. pr_info("yaffs: devname is NULL\n");
  1684. else
  1685. pr_info("yaffs: dev is %d name is \"%s\" %s\n",
  1686. sb->s_dev,
  1687. yaffs_devname(sb, devname_buf), read_only ? "ro" : "rw");
  1688. if (!data_str)
  1689. data_str = "";
  1690. pr_info("yaffs: passed flags \"%s\"\n", data_str);
  1691. memset(&options, 0, sizeof(options));
  1692. if (yaffs_parse_options(&options, data_str)) {
  1693. /* Option parsing failed */
  1694. return NULL;
  1695. }
  1696. sb->s_blocksize = PAGE_CACHE_SIZE;
  1697. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  1698. pr_info("[yaffs_read_super]sb maxbytes %llx", sb->s_maxbytes);
  1699. sb->s_maxbytes = 1LL<<32;
  1700. yaffs_trace(YAFFS_TRACE_OS,
  1701. "yaffs_read_super: Using yaffs%d", yaffs_version);
  1702. yaffs_trace(YAFFS_TRACE_OS,
  1703. "yaffs_read_super: block size %d", (int)(sb->s_blocksize));
  1704. #ifdef YAFFS_MVG_TEST_DEBUG_LOG
  1705. yaffs_mvg_test_debug_mode |= 0x1;
  1706. #endif
  1707. #ifdef YAFFS_MVG_TEST_DUMP_SCAN_SEQ /*dump seq num in yaffs2_ScanBackwards*/
  1708. yaffs_mvg_test_debug_mode |= 0x2;
  1709. #endif
  1710. #ifdef YAFFS_MVG_TEST_ERASECHEKFF /*when erase block,check if or not the block becomes 0xff*/
  1711. yaffs_mvg_test_debug_mode |= 0x4;
  1712. #endif
  1713. #ifdef YAFFS_MVG_TEST_DEBUG_WRITECHECK /*check 0xff ,before write every chunk .*/
  1714. yaffs_mvg_test_debug_mode |= 0x8;
  1715. #endif
  1716. #ifdef YAFFS_MVG_TEST_DEBUG_WRITEHECK_FIXERROR /*fixerror when chunk is not ,before write every chunk .*/
  1717. yaffs_mvg_test_debug_mode |= 0x10;
  1718. #endif
  1719. yaffs_trace(YAFFS_TRACE_ALWAYS,
  1720. "Attempting MTD mount of %u.%u,\"%s\" debug_mode:0x%x\n",
  1721. MAJOR(sb->s_dev), MINOR(sb->s_dev),
  1722. yaffs_devname(sb, devname_buf), yaffs_mvg_test_debug_mode);
  1723. /* Check it's an mtd device..... */
  1724. if (MAJOR(sb->s_dev) != MTD_BLOCK_MAJOR)
  1725. return NULL; /* This isn't an mtd device */
  1726. /* Get the device */
  1727. mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
  1728. if (!mtd) {
  1729. yaffs_trace(YAFFS_TRACE_ALWAYS,
  1730. "MTD device #%u doesn't appear to exist",
  1731. MINOR(sb->s_dev));
  1732. return NULL;
  1733. }
  1734. /* Check it's NAND */
  1735. if (!(mtd->type == MTD_NANDFLASH || mtd->type == MTD_MLCNANDFLASH)) {
  1736. yaffs_trace(YAFFS_TRACE_ALWAYS,
  1737. "MTD device is not NAND it's type %d",
  1738. mtd->type);
  1739. return NULL;
  1740. }
  1741. yaffs_trace(YAFFS_TRACE_OS, " erase %p", mtd->_erase);
  1742. yaffs_trace(YAFFS_TRACE_OS, " read %p", mtd->_read);
  1743. yaffs_trace(YAFFS_TRACE_OS, " write %p", mtd->_write);
  1744. yaffs_trace(YAFFS_TRACE_OS, " readoob %p", mtd->_read_oob);
  1745. yaffs_trace(YAFFS_TRACE_OS, " writeoob %p", mtd->_write_oob);
  1746. yaffs_trace(YAFFS_TRACE_OS, " block_isbad %p", mtd->_block_isbad);
  1747. yaffs_trace(YAFFS_TRACE_OS, " block_markbad %p", mtd->_block_markbad);
  1748. yaffs_trace(YAFFS_TRACE_OS, " %s %d", WRITE_SIZE_STR, WRITE_SIZE(mtd));
  1749. yaffs_trace(YAFFS_TRACE_OS, " oobsize %d", mtd->oobsize);
  1750. yaffs_trace(YAFFS_TRACE_OS, " erasesize %d", mtd->erasesize);
  1751. yaffs_trace(YAFFS_TRACE_OS, " size %lld", mtd->size);
  1752. #ifdef CONFIG_YAFFS_AUTO_YAFFS2
  1753. if (yaffs_version == 1 && WRITE_SIZE(mtd) >= 2048) {
  1754. yaffs_trace(YAFFS_TRACE_ALWAYS, "auto selecting yaffs2");
  1755. yaffs_version = 2;
  1756. }
  1757. /* Added NCB 26/5/2006 for completeness */
  1758. if (yaffs_version == 2 && !options.inband_tags
  1759. && WRITE_SIZE(mtd) == 512) {
  1760. yaffs_trace(YAFFS_TRACE_ALWAYS, "auto selecting yaffs1");
  1761. yaffs_version = 1;
  1762. }
  1763. #endif
  1764. if (yaffs_version == 2) {
  1765. /* Check for version 2 style functions */
  1766. if (!mtd->_erase ||
  1767. !mtd->_block_isbad ||
  1768. !mtd->_block_markbad ||
  1769. !mtd->_read ||
  1770. !mtd->_write || !mtd->_read_oob || !mtd->_write_oob) {
  1771. yaffs_trace(YAFFS_TRACE_ALWAYS,
  1772. "MTD device does not support required functions");
  1773. return NULL;
  1774. }
  1775. if ((WRITE_SIZE(mtd) < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
  1776. mtd->oobsize < YAFFS_MIN_YAFFS2_SPARE_SIZE) &&
  1777. !options.inband_tags) {
  1778. yaffs_trace(YAFFS_TRACE_ALWAYS,
  1779. "MTD device does not have the right page sizes");
  1780. return NULL;
  1781. }
  1782. } else {
  1783. /* Check for V1 style functions */
  1784. if (!mtd->_erase ||
  1785. !mtd->_read ||
  1786. !mtd->_write || !mtd->_read_oob || !mtd->_write_oob) {
  1787. yaffs_trace(YAFFS_TRACE_ALWAYS,
  1788. "MTD device does not support required functions");
  1789. return NULL;
  1790. }
  1791. if (WRITE_SIZE(mtd) < YAFFS_BYTES_PER_CHUNK ||
  1792. mtd->oobsize != YAFFS_BYTES_PER_SPARE) {
  1793. yaffs_trace(YAFFS_TRACE_ALWAYS,
  1794. "MTD device does not support have the right page sizes");
  1795. return NULL;
  1796. }
  1797. }
  1798. /* OK, so if we got here, we have an MTD that's NAND and looks
  1799. * like it has the right capabilities
  1800. * Set the struct yaffs_dev up for mtd
  1801. */
  1802. if (!read_only && !(mtd->flags & MTD_WRITEABLE)) {
  1803. read_only = 1;
  1804. pr_info(
  1805. "yaffs: mtd is read only, setting superblock read only");
  1806. sb->s_flags |= MS_RDONLY;
  1807. }
  1808. dev = kmalloc(sizeof(struct yaffs_dev), GFP_KERNEL);
  1809. context = kmalloc(sizeof(struct yaffs_linux_context), GFP_KERNEL);
  1810. if (!dev || !context) {
  1811. kfree(dev);
  1812. kfree(context);
  1813. dev = NULL;
  1814. context = NULL;
  1815. }
  1816. if (!dev) {
  1817. /* Deep shit could not allocate device structure */
  1818. yaffs_trace(YAFFS_TRACE_ALWAYS,
  1819. "yaffs_read_super failed trying to allocate yaffs_dev");
  1820. return NULL;
  1821. }
  1822. memset(dev, 0, sizeof(struct yaffs_dev));
  1823. param = &(dev->param);
  1824. memset(context, 0, sizeof(struct yaffs_linux_context));
  1825. dev->os_context = context;
  1826. INIT_LIST_HEAD(&(context->context_list));
  1827. context->dev = dev;
  1828. context->super = sb;
  1829. dev->read_only = read_only;
  1830. sb->s_fs_info = dev;
  1831. dev->driver_context = mtd;
  1832. param->name = mtd->name;
  1833. /* Set up the memory size parameters.... */
  1834. n_blocks =
  1835. YCALCBLOCKS(mtd->size,
  1836. (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK));
  1837. param->start_block = 0;
  1838. param->end_block = n_blocks - 1;
  1839. param->chunks_per_block = YAFFS_CHUNKS_PER_BLOCK;
  1840. param->total_bytes_per_chunk = YAFFS_BYTES_PER_CHUNK;
  1841. param->n_reserved_blocks = 5;
  1842. param->n_caches = (options.no_cache) ? 0 : 10;
  1843. param->inband_tags = options.inband_tags;
  1844. #ifdef CONFIG_YAFFS_DISABLE_LAZY_LOAD
  1845. param->disable_lazy_load = 1;
  1846. #endif
  1847. #ifdef CONFIG_YAFFS_XATTR
  1848. param->enable_xattr = 1;
  1849. #endif
  1850. if (options.lazy_loading_overridden)
  1851. param->disable_lazy_load = !options.lazy_loading_enabled;
  1852. #ifdef CONFIG_YAFFS_DISABLE_TAGS_ECC
  1853. param->no_tags_ecc = 1;
  1854. #endif
  1855. #ifdef CONFIG_YAFFS_DISABLE_BACKGROUND
  1856. #else
  1857. param->defered_dir_update = 1;
  1858. #endif
  1859. if (options.tags_ecc_overridden)
  1860. param->no_tags_ecc = !options.tags_ecc_on;
  1861. #ifdef CONFIG_YAFFS_EMPTY_LOST_AND_FOUND
  1862. param->empty_lost_n_found = 1;
  1863. #endif
  1864. #ifdef CONFIG_YAFFS_DISABLE_BLOCK_REFRESHING
  1865. param->refresh_period = 0;
  1866. #else
  1867. param->refresh_period = 500;
  1868. #endif
  1869. #ifdef CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED
  1870. param->always_check_erased = 1;
  1871. #endif
  1872. if (options.empty_lost_and_found_overridden)
  1873. param->empty_lost_n_found = options.empty_lost_and_found;
  1874. /* ... and the functions. */
  1875. if (yaffs_version == 2) {
  1876. param->write_chunk_tags_fn = nandmtd2_write_chunk_tags;
  1877. param->read_chunk_tags_fn = nandmtd2_read_chunk_tags;
  1878. param->bad_block_fn = nandmtd2_mark_block_bad;
  1879. param->query_block_fn = nandmtd2_query_block;
  1880. yaffs_dev_to_lc(dev)->spare_buffer =
  1881. kmalloc(mtd->oobsize, GFP_NOFS);
  1882. param->is_yaffs2 = 1;
  1883. param->total_bytes_per_chunk = mtd->writesize;
  1884. param->chunks_per_block = mtd->erasesize / mtd->writesize;
  1885. n_blocks = YCALCBLOCKS(mtd->size, mtd->erasesize);
  1886. param->start_block = 0;
  1887. param->end_block = n_blocks - 1;
  1888. } else {
  1889. /* use the MTD interface in yaffs_mtdif1.c */
  1890. param->write_chunk_tags_fn = nandmtd1_write_chunk_tags;
  1891. param->read_chunk_tags_fn = nandmtd1_read_chunk_tags;
  1892. param->bad_block_fn = nandmtd1_mark_block_bad;
  1893. param->query_block_fn = nandmtd1_query_block;
  1894. param->is_yaffs2 = 0;
  1895. }
  1896. /* ... and common functions */
  1897. param->erase_fn = nandmtd_erase_block;
  1898. param->initialise_flash_fn = nandmtd_initialise;
  1899. yaffs_dev_to_lc(dev)->put_super_fn = yaffs_mtd_put_super;
  1900. param->sb_dirty_fn = yaffs_touch_super;
  1901. param->gc_control = yaffs_gc_control_callback;
  1902. yaffs_dev_to_lc(dev)->super = sb;
  1903. #ifndef CONFIG_YAFFS_DOES_ECC
  1904. param->use_nand_ecc = 1;
  1905. #endif
  1906. param->skip_checkpt_rd = options.skip_checkpoint_read;
  1907. param->skip_checkpt_wr = options.skip_checkpoint_write;
  1908. mutex_lock(&yaffs_context_lock);
  1909. /* Get a mount id */
  1910. found = 0;
  1911. for (mount_id = 0; !found; mount_id++) {
  1912. found = 1;
  1913. list_for_each(l, &yaffs_context_list) {
  1914. context_iterator =
  1915. list_entry(l, struct yaffs_linux_context,
  1916. context_list);
  1917. if (context_iterator->mount_id == mount_id)
  1918. found = 0;
  1919. }
  1920. }
  1921. context->mount_id = mount_id;
  1922. list_add_tail(&(yaffs_dev_to_lc(dev)->context_list),
  1923. &yaffs_context_list);
  1924. mutex_unlock(&yaffs_context_lock);
  1925. /* Directory search handling... */
  1926. INIT_LIST_HEAD(&(yaffs_dev_to_lc(dev)->search_contexts));
  1927. param->remove_obj_fn = yaffs_remove_obj_callback;
  1928. mutex_init(&(yaffs_dev_to_lc(dev)->gross_lock));
  1929. yaffs_gross_lock(dev);
  1930. err = yaffs_guts_initialise(dev);
  1931. yaffs_trace(YAFFS_TRACE_OS,
  1932. "yaffs_read_super: guts initialised %s",
  1933. (err == YAFFS_OK) ? "OK" : "FAILED");
  1934. if (err == YAFFS_OK)
  1935. yaffs_bg_start(dev);
  1936. if (!context->bg_thread)
  1937. param->defered_dir_update = 0;
  1938. /* Release lock before yaffs_get_inode() */
  1939. yaffs_gross_unlock(dev);
  1940. /* Create root inode */
  1941. if (err == YAFFS_OK)
  1942. inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0, yaffs_root(dev));
  1943. if (!inode)
  1944. return NULL;
  1945. inode->i_op = &yaffs_dir_inode_operations;
  1946. inode->i_fop = &yaffs_dir_operations;
  1947. yaffs_trace(YAFFS_TRACE_OS, "yaffs_read_super: got root inode");
  1948. root = d_make_root(inode);
  1949. yaffs_trace(YAFFS_TRACE_OS, "yaffs_read_super: d_alloc_root done");
  1950. if (!root) {
  1951. iput(inode);
  1952. return NULL;
  1953. }
  1954. sb->s_root = root;
  1955. dev->s_dirt = !dev->is_checkpointed;
  1956. yaffs_trace(YAFFS_TRACE_ALWAYS,
  1957. "yaffs_read_super: is_checkpointed %d",
  1958. dev->is_checkpointed);
  1959. yaffs_trace(YAFFS_TRACE_OS, "yaffs_read_super: done");
  1960. return sb;
  1961. }
  1962. static int yaffs_internal_read_super_mtd(struct super_block *sb, void *data,
  1963. int silent)
  1964. {
  1965. return yaffs_internal_read_super(1, sb, data, silent) ? 0 : -EINVAL;
  1966. }
  1967. static struct dentry *yaffs_read_super(struct file_system_type *fs,
  1968. int flags, const char *dev_name,
  1969. void *data)
  1970. {
  1971. return mount_bdev(fs, flags, dev_name, data,
  1972. yaffs_internal_read_super_mtd);
  1973. }
  1974. static struct file_system_type yaffs_fs_type = {
  1975. .owner = THIS_MODULE,
  1976. .name = "yaffs",
  1977. .mount = yaffs_read_super,
  1978. .kill_sb = kill_block_super,
  1979. .fs_flags = FS_REQUIRES_DEV,
  1980. };
  1981. #ifdef CONFIG_YAFFS_YAFFS2
  1982. static int yaffs2_internal_read_super_mtd(struct super_block *sb, void *data,
  1983. int silent)
  1984. {
  1985. return yaffs_internal_read_super(2, sb, data, silent) ? 0 : -EINVAL;
  1986. }
  1987. static struct dentry *yaffs2_read_super(struct file_system_type *fs,
  1988. int flags, const char *dev_name, void *data)
  1989. {
  1990. return mount_bdev(fs, flags, dev_name, data,
  1991. yaffs2_internal_read_super_mtd);
  1992. }
  1993. static struct file_system_type yaffs2_fs_type = {
  1994. .owner = THIS_MODULE,
  1995. .name = "yaffs2",
  1996. .mount = yaffs2_read_super,
  1997. .kill_sb = kill_block_super,
  1998. .fs_flags = FS_REQUIRES_DEV,
  1999. };
  2000. #endif /* CONFIG_YAFFS_YAFFS2 */
  2001. /* Stuff to handle installation of file systems */
  2002. struct file_system_to_install {
  2003. struct file_system_type *fst;
  2004. int installed;
  2005. };
  2006. static struct file_system_to_install fs_to_install[] = {
  2007. {&yaffs_fs_type, 0},
  2008. {&yaffs2_fs_type, 0},
  2009. {NULL, 0}
  2010. };
  2011. /*
  2012. static const struct file_operations yaffs_fops = {
  2013. .owner = THIS_MODULE,
  2014. .read = yaffs_proc_read,
  2015. .write = yaffs_proc_write,
  2016. };
  2017. */
  2018. static int __init init_yaffs_fs(void)
  2019. {
  2020. int error = 0;
  2021. struct file_system_to_install *fsinst;
  2022. yaffs_trace(YAFFS_TRACE_ALWAYS,
  2023. "yaffs built Installing.");
  2024. #ifdef CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED
  2025. yaffs_trace(YAFFS_TRACE_ALWAYS,
  2026. "\n\nYAFFS-WARNING CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED selected.\n\n\n");
  2027. #endif
  2028. mutex_init(&yaffs_context_lock);
  2029. /* Now add the file system entries */
  2030. fsinst = fs_to_install;
  2031. while (fsinst->fst && !error) {
  2032. error = register_filesystem(fsinst->fst);
  2033. if (!error)
  2034. fsinst->installed = 1;
  2035. fsinst++;
  2036. }
  2037. /* Any errors? uninstall */
  2038. if (error) {
  2039. fsinst = fs_to_install;
  2040. while (fsinst->fst) {
  2041. if (fsinst->installed) {
  2042. unregister_filesystem(fsinst->fst);
  2043. fsinst->installed = 0;
  2044. }
  2045. fsinst++;
  2046. }
  2047. }
  2048. return error;
  2049. }
  2050. static void __exit exit_yaffs_fs(void)
  2051. {
  2052. struct file_system_to_install *fsinst;
  2053. yaffs_trace(YAFFS_TRACE_ALWAYS,
  2054. "yaffs built removing.");
  2055. fsinst = fs_to_install;
  2056. while (fsinst->fst) {
  2057. if (fsinst->installed) {
  2058. unregister_filesystem(fsinst->fst);
  2059. fsinst->installed = 0;
  2060. }
  2061. fsinst++;
  2062. }
  2063. }
  2064. module_init(init_yaffs_fs)
  2065. module_exit(exit_yaffs_fs)
  2066. MODULE_DESCRIPTION("YAFFS2 - a NAND specific flash file system");
  2067. MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002-2010");
  2068. MODULE_LICENSE("GPL");