yaffs_vfs.c 68 KB

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