super.c 78 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829
  1. /*
  2. * linux/fs/ext4/super.c
  3. *
  4. * Copyright (C) 1992, 1993, 1994, 1995
  5. * Remy Card (card@masi.ibp.fr)
  6. * Laboratoire MASI - Institut Blaise Pascal
  7. * Universite Pierre et Marie Curie (Paris VI)
  8. *
  9. * from
  10. *
  11. * linux/fs/minix/inode.c
  12. *
  13. * Copyright (C) 1991, 1992 Linus Torvalds
  14. *
  15. * Big-endian to little-endian byte-swapping/bitmaps by
  16. * David S. Miller (davem@caip.rutgers.edu), 1995
  17. */
  18. #include <linux/module.h>
  19. #include <linux/string.h>
  20. #include <linux/fs.h>
  21. #include <linux/time.h>
  22. #include <linux/jbd2.h>
  23. #include <linux/ext4_fs.h>
  24. #include <linux/ext4_jbd2.h>
  25. #include <linux/slab.h>
  26. #include <linux/init.h>
  27. #include <linux/blkdev.h>
  28. #include <linux/parser.h>
  29. #include <linux/smp_lock.h>
  30. #include <linux/buffer_head.h>
  31. #include <linux/vfs.h>
  32. #include <linux/random.h>
  33. #include <linux/mount.h>
  34. #include <linux/namei.h>
  35. #include <linux/quotaops.h>
  36. #include <linux/seq_file.h>
  37. #include <asm/uaccess.h>
  38. #include "xattr.h"
  39. #include "acl.h"
  40. #include "namei.h"
  41. static int ext4_load_journal(struct super_block *, struct ext4_super_block *,
  42. unsigned long journal_devnum);
  43. static int ext4_create_journal(struct super_block *, struct ext4_super_block *,
  44. unsigned int);
  45. static void ext4_commit_super (struct super_block * sb,
  46. struct ext4_super_block * es,
  47. int sync);
  48. static void ext4_mark_recovery_complete(struct super_block * sb,
  49. struct ext4_super_block * es);
  50. static void ext4_clear_journal_err(struct super_block * sb,
  51. struct ext4_super_block * es);
  52. static int ext4_sync_fs(struct super_block *sb, int wait);
  53. static const char *ext4_decode_error(struct super_block * sb, int errno,
  54. char nbuf[16]);
  55. static int ext4_remount (struct super_block * sb, int * flags, char * data);
  56. static int ext4_statfs (struct dentry * dentry, struct kstatfs * buf);
  57. static void ext4_unlockfs(struct super_block *sb);
  58. static void ext4_write_super (struct super_block * sb);
  59. static void ext4_write_super_lockfs(struct super_block *sb);
  60. ext4_fsblk_t ext4_block_bitmap(struct super_block *sb,
  61. struct ext4_group_desc *bg)
  62. {
  63. return le32_to_cpu(bg->bg_block_bitmap) |
  64. (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
  65. (ext4_fsblk_t)le32_to_cpu(bg->bg_block_bitmap_hi) << 32 : 0);
  66. }
  67. ext4_fsblk_t ext4_inode_bitmap(struct super_block *sb,
  68. struct ext4_group_desc *bg)
  69. {
  70. return le32_to_cpu(bg->bg_inode_bitmap) |
  71. (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
  72. (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_bitmap_hi) << 32 : 0);
  73. }
  74. ext4_fsblk_t ext4_inode_table(struct super_block *sb,
  75. struct ext4_group_desc *bg)
  76. {
  77. return le32_to_cpu(bg->bg_inode_table) |
  78. (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
  79. (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_table_hi) << 32 : 0);
  80. }
  81. void ext4_block_bitmap_set(struct super_block *sb,
  82. struct ext4_group_desc *bg, ext4_fsblk_t blk)
  83. {
  84. bg->bg_block_bitmap = cpu_to_le32((u32)blk);
  85. if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
  86. bg->bg_block_bitmap_hi = cpu_to_le32(blk >> 32);
  87. }
  88. void ext4_inode_bitmap_set(struct super_block *sb,
  89. struct ext4_group_desc *bg, ext4_fsblk_t blk)
  90. {
  91. bg->bg_inode_bitmap = cpu_to_le32((u32)blk);
  92. if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
  93. bg->bg_inode_bitmap_hi = cpu_to_le32(blk >> 32);
  94. }
  95. void ext4_inode_table_set(struct super_block *sb,
  96. struct ext4_group_desc *bg, ext4_fsblk_t blk)
  97. {
  98. bg->bg_inode_table = cpu_to_le32((u32)blk);
  99. if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
  100. bg->bg_inode_table_hi = cpu_to_le32(blk >> 32);
  101. }
  102. /*
  103. * Wrappers for jbd2_journal_start/end.
  104. *
  105. * The only special thing we need to do here is to make sure that all
  106. * journal_end calls result in the superblock being marked dirty, so
  107. * that sync() will call the filesystem's write_super callback if
  108. * appropriate.
  109. */
  110. handle_t *ext4_journal_start_sb(struct super_block *sb, int nblocks)
  111. {
  112. journal_t *journal;
  113. if (sb->s_flags & MS_RDONLY)
  114. return ERR_PTR(-EROFS);
  115. /* Special case here: if the journal has aborted behind our
  116. * backs (eg. EIO in the commit thread), then we still need to
  117. * take the FS itself readonly cleanly. */
  118. journal = EXT4_SB(sb)->s_journal;
  119. if (is_journal_aborted(journal)) {
  120. ext4_abort(sb, __FUNCTION__,
  121. "Detected aborted journal");
  122. return ERR_PTR(-EROFS);
  123. }
  124. return jbd2_journal_start(journal, nblocks);
  125. }
  126. /*
  127. * The only special thing we need to do here is to make sure that all
  128. * jbd2_journal_stop calls result in the superblock being marked dirty, so
  129. * that sync() will call the filesystem's write_super callback if
  130. * appropriate.
  131. */
  132. int __ext4_journal_stop(const char *where, handle_t *handle)
  133. {
  134. struct super_block *sb;
  135. int err;
  136. int rc;
  137. sb = handle->h_transaction->t_journal->j_private;
  138. err = handle->h_err;
  139. rc = jbd2_journal_stop(handle);
  140. if (!err)
  141. err = rc;
  142. if (err)
  143. __ext4_std_error(sb, where, err);
  144. return err;
  145. }
  146. void ext4_journal_abort_handle(const char *caller, const char *err_fn,
  147. struct buffer_head *bh, handle_t *handle, int err)
  148. {
  149. char nbuf[16];
  150. const char *errstr = ext4_decode_error(NULL, err, nbuf);
  151. if (bh)
  152. BUFFER_TRACE(bh, "abort");
  153. if (!handle->h_err)
  154. handle->h_err = err;
  155. if (is_handle_aborted(handle))
  156. return;
  157. printk(KERN_ERR "%s: aborting transaction: %s in %s\n",
  158. caller, errstr, err_fn);
  159. jbd2_journal_abort_handle(handle);
  160. }
  161. /* Deal with the reporting of failure conditions on a filesystem such as
  162. * inconsistencies detected or read IO failures.
  163. *
  164. * On ext2, we can store the error state of the filesystem in the
  165. * superblock. That is not possible on ext4, because we may have other
  166. * write ordering constraints on the superblock which prevent us from
  167. * writing it out straight away; and given that the journal is about to
  168. * be aborted, we can't rely on the current, or future, transactions to
  169. * write out the superblock safely.
  170. *
  171. * We'll just use the jbd2_journal_abort() error code to record an error in
  172. * the journal instead. On recovery, the journal will compain about
  173. * that error until we've noted it down and cleared it.
  174. */
  175. static void ext4_handle_error(struct super_block *sb)
  176. {
  177. struct ext4_super_block *es = EXT4_SB(sb)->s_es;
  178. EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
  179. es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
  180. if (sb->s_flags & MS_RDONLY)
  181. return;
  182. if (!test_opt (sb, ERRORS_CONT)) {
  183. journal_t *journal = EXT4_SB(sb)->s_journal;
  184. EXT4_SB(sb)->s_mount_opt |= EXT4_MOUNT_ABORT;
  185. if (journal)
  186. jbd2_journal_abort(journal, -EIO);
  187. }
  188. if (test_opt (sb, ERRORS_RO)) {
  189. printk (KERN_CRIT "Remounting filesystem read-only\n");
  190. sb->s_flags |= MS_RDONLY;
  191. }
  192. ext4_commit_super(sb, es, 1);
  193. if (test_opt(sb, ERRORS_PANIC))
  194. panic("EXT4-fs (device %s): panic forced after error\n",
  195. sb->s_id);
  196. }
  197. void ext4_error (struct super_block * sb, const char * function,
  198. const char * fmt, ...)
  199. {
  200. va_list args;
  201. va_start(args, fmt);
  202. printk(KERN_CRIT "EXT4-fs error (device %s): %s: ",sb->s_id, function);
  203. vprintk(fmt, args);
  204. printk("\n");
  205. va_end(args);
  206. ext4_handle_error(sb);
  207. }
  208. static const char *ext4_decode_error(struct super_block * sb, int errno,
  209. char nbuf[16])
  210. {
  211. char *errstr = NULL;
  212. switch (errno) {
  213. case -EIO:
  214. errstr = "IO failure";
  215. break;
  216. case -ENOMEM:
  217. errstr = "Out of memory";
  218. break;
  219. case -EROFS:
  220. if (!sb || EXT4_SB(sb)->s_journal->j_flags & JBD2_ABORT)
  221. errstr = "Journal has aborted";
  222. else
  223. errstr = "Readonly filesystem";
  224. break;
  225. default:
  226. /* If the caller passed in an extra buffer for unknown
  227. * errors, textualise them now. Else we just return
  228. * NULL. */
  229. if (nbuf) {
  230. /* Check for truncated error codes... */
  231. if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
  232. errstr = nbuf;
  233. }
  234. break;
  235. }
  236. return errstr;
  237. }
  238. /* __ext4_std_error decodes expected errors from journaling functions
  239. * automatically and invokes the appropriate error response. */
  240. void __ext4_std_error (struct super_block * sb, const char * function,
  241. int errno)
  242. {
  243. char nbuf[16];
  244. const char *errstr;
  245. /* Special case: if the error is EROFS, and we're not already
  246. * inside a transaction, then there's really no point in logging
  247. * an error. */
  248. if (errno == -EROFS && journal_current_handle() == NULL &&
  249. (sb->s_flags & MS_RDONLY))
  250. return;
  251. errstr = ext4_decode_error(sb, errno, nbuf);
  252. printk (KERN_CRIT "EXT4-fs error (device %s) in %s: %s\n",
  253. sb->s_id, function, errstr);
  254. ext4_handle_error(sb);
  255. }
  256. /*
  257. * ext4_abort is a much stronger failure handler than ext4_error. The
  258. * abort function may be used to deal with unrecoverable failures such
  259. * as journal IO errors or ENOMEM at a critical moment in log management.
  260. *
  261. * We unconditionally force the filesystem into an ABORT|READONLY state,
  262. * unless the error response on the fs has been set to panic in which
  263. * case we take the easy way out and panic immediately.
  264. */
  265. void ext4_abort (struct super_block * sb, const char * function,
  266. const char * fmt, ...)
  267. {
  268. va_list args;
  269. printk (KERN_CRIT "ext4_abort called.\n");
  270. va_start(args, fmt);
  271. printk(KERN_CRIT "EXT4-fs error (device %s): %s: ",sb->s_id, function);
  272. vprintk(fmt, args);
  273. printk("\n");
  274. va_end(args);
  275. if (test_opt(sb, ERRORS_PANIC))
  276. panic("EXT4-fs panic from previous error\n");
  277. if (sb->s_flags & MS_RDONLY)
  278. return;
  279. printk(KERN_CRIT "Remounting filesystem read-only\n");
  280. EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
  281. sb->s_flags |= MS_RDONLY;
  282. EXT4_SB(sb)->s_mount_opt |= EXT4_MOUNT_ABORT;
  283. jbd2_journal_abort(EXT4_SB(sb)->s_journal, -EIO);
  284. }
  285. void ext4_warning (struct super_block * sb, const char * function,
  286. const char * fmt, ...)
  287. {
  288. va_list args;
  289. va_start(args, fmt);
  290. printk(KERN_WARNING "EXT4-fs warning (device %s): %s: ",
  291. sb->s_id, function);
  292. vprintk(fmt, args);
  293. printk("\n");
  294. va_end(args);
  295. }
  296. void ext4_update_dynamic_rev(struct super_block *sb)
  297. {
  298. struct ext4_super_block *es = EXT4_SB(sb)->s_es;
  299. if (le32_to_cpu(es->s_rev_level) > EXT4_GOOD_OLD_REV)
  300. return;
  301. ext4_warning(sb, __FUNCTION__,
  302. "updating to rev %d because of new feature flag, "
  303. "running e2fsck is recommended",
  304. EXT4_DYNAMIC_REV);
  305. es->s_first_ino = cpu_to_le32(EXT4_GOOD_OLD_FIRST_INO);
  306. es->s_inode_size = cpu_to_le16(EXT4_GOOD_OLD_INODE_SIZE);
  307. es->s_rev_level = cpu_to_le32(EXT4_DYNAMIC_REV);
  308. /* leave es->s_feature_*compat flags alone */
  309. /* es->s_uuid will be set by e2fsck if empty */
  310. /*
  311. * The rest of the superblock fields should be zero, and if not it
  312. * means they are likely already in use, so leave them alone. We
  313. * can leave it up to e2fsck to clean up any inconsistencies there.
  314. */
  315. }
  316. /*
  317. * Open the external journal device
  318. */
  319. static struct block_device *ext4_blkdev_get(dev_t dev)
  320. {
  321. struct block_device *bdev;
  322. char b[BDEVNAME_SIZE];
  323. bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
  324. if (IS_ERR(bdev))
  325. goto fail;
  326. return bdev;
  327. fail:
  328. printk(KERN_ERR "EXT4: failed to open journal device %s: %ld\n",
  329. __bdevname(dev, b), PTR_ERR(bdev));
  330. return NULL;
  331. }
  332. /*
  333. * Release the journal device
  334. */
  335. static int ext4_blkdev_put(struct block_device *bdev)
  336. {
  337. bd_release(bdev);
  338. return blkdev_put(bdev);
  339. }
  340. static int ext4_blkdev_remove(struct ext4_sb_info *sbi)
  341. {
  342. struct block_device *bdev;
  343. int ret = -ENODEV;
  344. bdev = sbi->journal_bdev;
  345. if (bdev) {
  346. ret = ext4_blkdev_put(bdev);
  347. sbi->journal_bdev = NULL;
  348. }
  349. return ret;
  350. }
  351. static inline struct inode *orphan_list_entry(struct list_head *l)
  352. {
  353. return &list_entry(l, struct ext4_inode_info, i_orphan)->vfs_inode;
  354. }
  355. static void dump_orphan_list(struct super_block *sb, struct ext4_sb_info *sbi)
  356. {
  357. struct list_head *l;
  358. printk(KERN_ERR "sb orphan head is %d\n",
  359. le32_to_cpu(sbi->s_es->s_last_orphan));
  360. printk(KERN_ERR "sb_info orphan list:\n");
  361. list_for_each(l, &sbi->s_orphan) {
  362. struct inode *inode = orphan_list_entry(l);
  363. printk(KERN_ERR " "
  364. "inode %s:%lu at %p: mode %o, nlink %d, next %d\n",
  365. inode->i_sb->s_id, inode->i_ino, inode,
  366. inode->i_mode, inode->i_nlink,
  367. NEXT_ORPHAN(inode));
  368. }
  369. }
  370. static void ext4_put_super (struct super_block * sb)
  371. {
  372. struct ext4_sb_info *sbi = EXT4_SB(sb);
  373. struct ext4_super_block *es = sbi->s_es;
  374. int i;
  375. ext4_ext_release(sb);
  376. ext4_xattr_put_super(sb);
  377. jbd2_journal_destroy(sbi->s_journal);
  378. if (!(sb->s_flags & MS_RDONLY)) {
  379. EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
  380. es->s_state = cpu_to_le16(sbi->s_mount_state);
  381. BUFFER_TRACE(sbi->s_sbh, "marking dirty");
  382. mark_buffer_dirty(sbi->s_sbh);
  383. ext4_commit_super(sb, es, 1);
  384. }
  385. for (i = 0; i < sbi->s_gdb_count; i++)
  386. brelse(sbi->s_group_desc[i]);
  387. kfree(sbi->s_group_desc);
  388. percpu_counter_destroy(&sbi->s_freeblocks_counter);
  389. percpu_counter_destroy(&sbi->s_freeinodes_counter);
  390. percpu_counter_destroy(&sbi->s_dirs_counter);
  391. brelse(sbi->s_sbh);
  392. #ifdef CONFIG_QUOTA
  393. for (i = 0; i < MAXQUOTAS; i++)
  394. kfree(sbi->s_qf_names[i]);
  395. #endif
  396. /* Debugging code just in case the in-memory inode orphan list
  397. * isn't empty. The on-disk one can be non-empty if we've
  398. * detected an error and taken the fs readonly, but the
  399. * in-memory list had better be clean by this point. */
  400. if (!list_empty(&sbi->s_orphan))
  401. dump_orphan_list(sb, sbi);
  402. J_ASSERT(list_empty(&sbi->s_orphan));
  403. invalidate_bdev(sb->s_bdev, 0);
  404. if (sbi->journal_bdev && sbi->journal_bdev != sb->s_bdev) {
  405. /*
  406. * Invalidate the journal device's buffers. We don't want them
  407. * floating about in memory - the physical journal device may
  408. * hotswapped, and it breaks the `ro-after' testing code.
  409. */
  410. sync_blockdev(sbi->journal_bdev);
  411. invalidate_bdev(sbi->journal_bdev, 0);
  412. ext4_blkdev_remove(sbi);
  413. }
  414. sb->s_fs_info = NULL;
  415. kfree(sbi);
  416. return;
  417. }
  418. static kmem_cache_t *ext4_inode_cachep;
  419. /*
  420. * Called inside transaction, so use GFP_NOFS
  421. */
  422. static struct inode *ext4_alloc_inode(struct super_block *sb)
  423. {
  424. struct ext4_inode_info *ei;
  425. ei = kmem_cache_alloc(ext4_inode_cachep, SLAB_NOFS);
  426. if (!ei)
  427. return NULL;
  428. #ifdef CONFIG_EXT4DEV_FS_POSIX_ACL
  429. ei->i_acl = EXT4_ACL_NOT_CACHED;
  430. ei->i_default_acl = EXT4_ACL_NOT_CACHED;
  431. #endif
  432. ei->i_block_alloc_info = NULL;
  433. ei->vfs_inode.i_version = 1;
  434. memset(&ei->i_cached_extent, 0, sizeof(struct ext4_ext_cache));
  435. return &ei->vfs_inode;
  436. }
  437. static void ext4_destroy_inode(struct inode *inode)
  438. {
  439. kmem_cache_free(ext4_inode_cachep, EXT4_I(inode));
  440. }
  441. static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
  442. {
  443. struct ext4_inode_info *ei = (struct ext4_inode_info *) foo;
  444. if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
  445. SLAB_CTOR_CONSTRUCTOR) {
  446. INIT_LIST_HEAD(&ei->i_orphan);
  447. #ifdef CONFIG_EXT4DEV_FS_XATTR
  448. init_rwsem(&ei->xattr_sem);
  449. #endif
  450. mutex_init(&ei->truncate_mutex);
  451. inode_init_once(&ei->vfs_inode);
  452. }
  453. }
  454. static int init_inodecache(void)
  455. {
  456. ext4_inode_cachep = kmem_cache_create("ext4_inode_cache",
  457. sizeof(struct ext4_inode_info),
  458. 0, (SLAB_RECLAIM_ACCOUNT|
  459. SLAB_MEM_SPREAD),
  460. init_once, NULL);
  461. if (ext4_inode_cachep == NULL)
  462. return -ENOMEM;
  463. return 0;
  464. }
  465. static void destroy_inodecache(void)
  466. {
  467. kmem_cache_destroy(ext4_inode_cachep);
  468. }
  469. static void ext4_clear_inode(struct inode *inode)
  470. {
  471. struct ext4_block_alloc_info *rsv = EXT4_I(inode)->i_block_alloc_info;
  472. #ifdef CONFIG_EXT4DEV_FS_POSIX_ACL
  473. if (EXT4_I(inode)->i_acl &&
  474. EXT4_I(inode)->i_acl != EXT4_ACL_NOT_CACHED) {
  475. posix_acl_release(EXT4_I(inode)->i_acl);
  476. EXT4_I(inode)->i_acl = EXT4_ACL_NOT_CACHED;
  477. }
  478. if (EXT4_I(inode)->i_default_acl &&
  479. EXT4_I(inode)->i_default_acl != EXT4_ACL_NOT_CACHED) {
  480. posix_acl_release(EXT4_I(inode)->i_default_acl);
  481. EXT4_I(inode)->i_default_acl = EXT4_ACL_NOT_CACHED;
  482. }
  483. #endif
  484. ext4_discard_reservation(inode);
  485. EXT4_I(inode)->i_block_alloc_info = NULL;
  486. if (unlikely(rsv))
  487. kfree(rsv);
  488. }
  489. static inline void ext4_show_quota_options(struct seq_file *seq, struct super_block *sb)
  490. {
  491. #if defined(CONFIG_QUOTA)
  492. struct ext4_sb_info *sbi = EXT4_SB(sb);
  493. if (sbi->s_jquota_fmt)
  494. seq_printf(seq, ",jqfmt=%s",
  495. (sbi->s_jquota_fmt == QFMT_VFS_OLD) ? "vfsold": "vfsv0");
  496. if (sbi->s_qf_names[USRQUOTA])
  497. seq_printf(seq, ",usrjquota=%s", sbi->s_qf_names[USRQUOTA]);
  498. if (sbi->s_qf_names[GRPQUOTA])
  499. seq_printf(seq, ",grpjquota=%s", sbi->s_qf_names[GRPQUOTA]);
  500. if (sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA)
  501. seq_puts(seq, ",usrquota");
  502. if (sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA)
  503. seq_puts(seq, ",grpquota");
  504. #endif
  505. }
  506. static int ext4_show_options(struct seq_file *seq, struct vfsmount *vfs)
  507. {
  508. struct super_block *sb = vfs->mnt_sb;
  509. if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
  510. seq_puts(seq, ",data=journal");
  511. else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
  512. seq_puts(seq, ",data=ordered");
  513. else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)
  514. seq_puts(seq, ",data=writeback");
  515. ext4_show_quota_options(seq, sb);
  516. return 0;
  517. }
  518. static struct dentry *ext4_get_dentry(struct super_block *sb, void *vobjp)
  519. {
  520. __u32 *objp = vobjp;
  521. unsigned long ino = objp[0];
  522. __u32 generation = objp[1];
  523. struct inode *inode;
  524. struct dentry *result;
  525. if (ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO)
  526. return ERR_PTR(-ESTALE);
  527. if (ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))
  528. return ERR_PTR(-ESTALE);
  529. /* iget isn't really right if the inode is currently unallocated!!
  530. *
  531. * ext4_read_inode will return a bad_inode if the inode had been
  532. * deleted, so we should be safe.
  533. *
  534. * Currently we don't know the generation for parent directory, so
  535. * a generation of 0 means "accept any"
  536. */
  537. inode = iget(sb, ino);
  538. if (inode == NULL)
  539. return ERR_PTR(-ENOMEM);
  540. if (is_bad_inode(inode) ||
  541. (generation && inode->i_generation != generation)) {
  542. iput(inode);
  543. return ERR_PTR(-ESTALE);
  544. }
  545. /* now to find a dentry.
  546. * If possible, get a well-connected one
  547. */
  548. result = d_alloc_anon(inode);
  549. if (!result) {
  550. iput(inode);
  551. return ERR_PTR(-ENOMEM);
  552. }
  553. return result;
  554. }
  555. #ifdef CONFIG_QUOTA
  556. #define QTYPE2NAME(t) ((t)==USRQUOTA?"user":"group")
  557. #define QTYPE2MOPT(on, t) ((t)==USRQUOTA?((on)##USRJQUOTA):((on)##GRPJQUOTA))
  558. static int ext4_dquot_initialize(struct inode *inode, int type);
  559. static int ext4_dquot_drop(struct inode *inode);
  560. static int ext4_write_dquot(struct dquot *dquot);
  561. static int ext4_acquire_dquot(struct dquot *dquot);
  562. static int ext4_release_dquot(struct dquot *dquot);
  563. static int ext4_mark_dquot_dirty(struct dquot *dquot);
  564. static int ext4_write_info(struct super_block *sb, int type);
  565. static int ext4_quota_on(struct super_block *sb, int type, int format_id, char *path);
  566. static int ext4_quota_on_mount(struct super_block *sb, int type);
  567. static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
  568. size_t len, loff_t off);
  569. static ssize_t ext4_quota_write(struct super_block *sb, int type,
  570. const char *data, size_t len, loff_t off);
  571. static struct dquot_operations ext4_quota_operations = {
  572. .initialize = ext4_dquot_initialize,
  573. .drop = ext4_dquot_drop,
  574. .alloc_space = dquot_alloc_space,
  575. .alloc_inode = dquot_alloc_inode,
  576. .free_space = dquot_free_space,
  577. .free_inode = dquot_free_inode,
  578. .transfer = dquot_transfer,
  579. .write_dquot = ext4_write_dquot,
  580. .acquire_dquot = ext4_acquire_dquot,
  581. .release_dquot = ext4_release_dquot,
  582. .mark_dirty = ext4_mark_dquot_dirty,
  583. .write_info = ext4_write_info
  584. };
  585. static struct quotactl_ops ext4_qctl_operations = {
  586. .quota_on = ext4_quota_on,
  587. .quota_off = vfs_quota_off,
  588. .quota_sync = vfs_quota_sync,
  589. .get_info = vfs_get_dqinfo,
  590. .set_info = vfs_set_dqinfo,
  591. .get_dqblk = vfs_get_dqblk,
  592. .set_dqblk = vfs_set_dqblk
  593. };
  594. #endif
  595. static struct super_operations ext4_sops = {
  596. .alloc_inode = ext4_alloc_inode,
  597. .destroy_inode = ext4_destroy_inode,
  598. .read_inode = ext4_read_inode,
  599. .write_inode = ext4_write_inode,
  600. .dirty_inode = ext4_dirty_inode,
  601. .delete_inode = ext4_delete_inode,
  602. .put_super = ext4_put_super,
  603. .write_super = ext4_write_super,
  604. .sync_fs = ext4_sync_fs,
  605. .write_super_lockfs = ext4_write_super_lockfs,
  606. .unlockfs = ext4_unlockfs,
  607. .statfs = ext4_statfs,
  608. .remount_fs = ext4_remount,
  609. .clear_inode = ext4_clear_inode,
  610. .show_options = ext4_show_options,
  611. #ifdef CONFIG_QUOTA
  612. .quota_read = ext4_quota_read,
  613. .quota_write = ext4_quota_write,
  614. #endif
  615. };
  616. static struct export_operations ext4_export_ops = {
  617. .get_parent = ext4_get_parent,
  618. .get_dentry = ext4_get_dentry,
  619. };
  620. enum {
  621. Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
  622. Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
  623. Opt_nouid32, Opt_nocheck, Opt_debug, Opt_oldalloc, Opt_orlov,
  624. Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl,
  625. Opt_reservation, Opt_noreservation, Opt_noload, Opt_nobh, Opt_bh,
  626. Opt_commit, Opt_journal_update, Opt_journal_inum, Opt_journal_dev,
  627. Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
  628. Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
  629. Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_quota, Opt_noquota,
  630. Opt_ignore, Opt_barrier, Opt_err, Opt_resize, Opt_usrquota,
  631. Opt_grpquota, Opt_extents,
  632. };
  633. static match_table_t tokens = {
  634. {Opt_bsd_df, "bsddf"},
  635. {Opt_minix_df, "minixdf"},
  636. {Opt_grpid, "grpid"},
  637. {Opt_grpid, "bsdgroups"},
  638. {Opt_nogrpid, "nogrpid"},
  639. {Opt_nogrpid, "sysvgroups"},
  640. {Opt_resgid, "resgid=%u"},
  641. {Opt_resuid, "resuid=%u"},
  642. {Opt_sb, "sb=%u"},
  643. {Opt_err_cont, "errors=continue"},
  644. {Opt_err_panic, "errors=panic"},
  645. {Opt_err_ro, "errors=remount-ro"},
  646. {Opt_nouid32, "nouid32"},
  647. {Opt_nocheck, "nocheck"},
  648. {Opt_nocheck, "check=none"},
  649. {Opt_debug, "debug"},
  650. {Opt_oldalloc, "oldalloc"},
  651. {Opt_orlov, "orlov"},
  652. {Opt_user_xattr, "user_xattr"},
  653. {Opt_nouser_xattr, "nouser_xattr"},
  654. {Opt_acl, "acl"},
  655. {Opt_noacl, "noacl"},
  656. {Opt_reservation, "reservation"},
  657. {Opt_noreservation, "noreservation"},
  658. {Opt_noload, "noload"},
  659. {Opt_nobh, "nobh"},
  660. {Opt_bh, "bh"},
  661. {Opt_commit, "commit=%u"},
  662. {Opt_journal_update, "journal=update"},
  663. {Opt_journal_inum, "journal=%u"},
  664. {Opt_journal_dev, "journal_dev=%u"},
  665. {Opt_abort, "abort"},
  666. {Opt_data_journal, "data=journal"},
  667. {Opt_data_ordered, "data=ordered"},
  668. {Opt_data_writeback, "data=writeback"},
  669. {Opt_offusrjquota, "usrjquota="},
  670. {Opt_usrjquota, "usrjquota=%s"},
  671. {Opt_offgrpjquota, "grpjquota="},
  672. {Opt_grpjquota, "grpjquota=%s"},
  673. {Opt_jqfmt_vfsold, "jqfmt=vfsold"},
  674. {Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
  675. {Opt_grpquota, "grpquota"},
  676. {Opt_noquota, "noquota"},
  677. {Opt_quota, "quota"},
  678. {Opt_usrquota, "usrquota"},
  679. {Opt_barrier, "barrier=%u"},
  680. {Opt_extents, "extents"},
  681. {Opt_err, NULL},
  682. {Opt_resize, "resize"},
  683. };
  684. static ext4_fsblk_t get_sb_block(void **data)
  685. {
  686. ext4_fsblk_t sb_block;
  687. char *options = (char *) *data;
  688. if (!options || strncmp(options, "sb=", 3) != 0)
  689. return 1; /* Default location */
  690. options += 3;
  691. /*todo: use simple_strtoll with >32bit ext4 */
  692. sb_block = simple_strtoul(options, &options, 0);
  693. if (*options && *options != ',') {
  694. printk("EXT4-fs: Invalid sb specification: %s\n",
  695. (char *) *data);
  696. return 1;
  697. }
  698. if (*options == ',')
  699. options++;
  700. *data = (void *) options;
  701. return sb_block;
  702. }
  703. static int parse_options (char *options, struct super_block *sb,
  704. unsigned int *inum, unsigned long *journal_devnum,
  705. ext4_fsblk_t *n_blocks_count, int is_remount)
  706. {
  707. struct ext4_sb_info *sbi = EXT4_SB(sb);
  708. char * p;
  709. substring_t args[MAX_OPT_ARGS];
  710. int data_opt = 0;
  711. int option;
  712. #ifdef CONFIG_QUOTA
  713. int qtype;
  714. char *qname;
  715. #endif
  716. if (!options)
  717. return 1;
  718. while ((p = strsep (&options, ",")) != NULL) {
  719. int token;
  720. if (!*p)
  721. continue;
  722. token = match_token(p, tokens, args);
  723. switch (token) {
  724. case Opt_bsd_df:
  725. clear_opt (sbi->s_mount_opt, MINIX_DF);
  726. break;
  727. case Opt_minix_df:
  728. set_opt (sbi->s_mount_opt, MINIX_DF);
  729. break;
  730. case Opt_grpid:
  731. set_opt (sbi->s_mount_opt, GRPID);
  732. break;
  733. case Opt_nogrpid:
  734. clear_opt (sbi->s_mount_opt, GRPID);
  735. break;
  736. case Opt_resuid:
  737. if (match_int(&args[0], &option))
  738. return 0;
  739. sbi->s_resuid = option;
  740. break;
  741. case Opt_resgid:
  742. if (match_int(&args[0], &option))
  743. return 0;
  744. sbi->s_resgid = option;
  745. break;
  746. case Opt_sb:
  747. /* handled by get_sb_block() instead of here */
  748. /* *sb_block = match_int(&args[0]); */
  749. break;
  750. case Opt_err_panic:
  751. clear_opt (sbi->s_mount_opt, ERRORS_CONT);
  752. clear_opt (sbi->s_mount_opt, ERRORS_RO);
  753. set_opt (sbi->s_mount_opt, ERRORS_PANIC);
  754. break;
  755. case Opt_err_ro:
  756. clear_opt (sbi->s_mount_opt, ERRORS_CONT);
  757. clear_opt (sbi->s_mount_opt, ERRORS_PANIC);
  758. set_opt (sbi->s_mount_opt, ERRORS_RO);
  759. break;
  760. case Opt_err_cont:
  761. clear_opt (sbi->s_mount_opt, ERRORS_RO);
  762. clear_opt (sbi->s_mount_opt, ERRORS_PANIC);
  763. set_opt (sbi->s_mount_opt, ERRORS_CONT);
  764. break;
  765. case Opt_nouid32:
  766. set_opt (sbi->s_mount_opt, NO_UID32);
  767. break;
  768. case Opt_nocheck:
  769. clear_opt (sbi->s_mount_opt, CHECK);
  770. break;
  771. case Opt_debug:
  772. set_opt (sbi->s_mount_opt, DEBUG);
  773. break;
  774. case Opt_oldalloc:
  775. set_opt (sbi->s_mount_opt, OLDALLOC);
  776. break;
  777. case Opt_orlov:
  778. clear_opt (sbi->s_mount_opt, OLDALLOC);
  779. break;
  780. #ifdef CONFIG_EXT4DEV_FS_XATTR
  781. case Opt_user_xattr:
  782. set_opt (sbi->s_mount_opt, XATTR_USER);
  783. break;
  784. case Opt_nouser_xattr:
  785. clear_opt (sbi->s_mount_opt, XATTR_USER);
  786. break;
  787. #else
  788. case Opt_user_xattr:
  789. case Opt_nouser_xattr:
  790. printk("EXT4 (no)user_xattr options not supported\n");
  791. break;
  792. #endif
  793. #ifdef CONFIG_EXT4DEV_FS_POSIX_ACL
  794. case Opt_acl:
  795. set_opt(sbi->s_mount_opt, POSIX_ACL);
  796. break;
  797. case Opt_noacl:
  798. clear_opt(sbi->s_mount_opt, POSIX_ACL);
  799. break;
  800. #else
  801. case Opt_acl:
  802. case Opt_noacl:
  803. printk("EXT4 (no)acl options not supported\n");
  804. break;
  805. #endif
  806. case Opt_reservation:
  807. set_opt(sbi->s_mount_opt, RESERVATION);
  808. break;
  809. case Opt_noreservation:
  810. clear_opt(sbi->s_mount_opt, RESERVATION);
  811. break;
  812. case Opt_journal_update:
  813. /* @@@ FIXME */
  814. /* Eventually we will want to be able to create
  815. a journal file here. For now, only allow the
  816. user to specify an existing inode to be the
  817. journal file. */
  818. if (is_remount) {
  819. printk(KERN_ERR "EXT4-fs: cannot specify "
  820. "journal on remount\n");
  821. return 0;
  822. }
  823. set_opt (sbi->s_mount_opt, UPDATE_JOURNAL);
  824. break;
  825. case Opt_journal_inum:
  826. if (is_remount) {
  827. printk(KERN_ERR "EXT4-fs: cannot specify "
  828. "journal on remount\n");
  829. return 0;
  830. }
  831. if (match_int(&args[0], &option))
  832. return 0;
  833. *inum = option;
  834. break;
  835. case Opt_journal_dev:
  836. if (is_remount) {
  837. printk(KERN_ERR "EXT4-fs: cannot specify "
  838. "journal on remount\n");
  839. return 0;
  840. }
  841. if (match_int(&args[0], &option))
  842. return 0;
  843. *journal_devnum = option;
  844. break;
  845. case Opt_noload:
  846. set_opt (sbi->s_mount_opt, NOLOAD);
  847. break;
  848. case Opt_commit:
  849. if (match_int(&args[0], &option))
  850. return 0;
  851. if (option < 0)
  852. return 0;
  853. if (option == 0)
  854. option = JBD_DEFAULT_MAX_COMMIT_AGE;
  855. sbi->s_commit_interval = HZ * option;
  856. break;
  857. case Opt_data_journal:
  858. data_opt = EXT4_MOUNT_JOURNAL_DATA;
  859. goto datacheck;
  860. case Opt_data_ordered:
  861. data_opt = EXT4_MOUNT_ORDERED_DATA;
  862. goto datacheck;
  863. case Opt_data_writeback:
  864. data_opt = EXT4_MOUNT_WRITEBACK_DATA;
  865. datacheck:
  866. if (is_remount) {
  867. if ((sbi->s_mount_opt & EXT4_MOUNT_DATA_FLAGS)
  868. != data_opt) {
  869. printk(KERN_ERR
  870. "EXT4-fs: cannot change data "
  871. "mode on remount\n");
  872. return 0;
  873. }
  874. } else {
  875. sbi->s_mount_opt &= ~EXT4_MOUNT_DATA_FLAGS;
  876. sbi->s_mount_opt |= data_opt;
  877. }
  878. break;
  879. #ifdef CONFIG_QUOTA
  880. case Opt_usrjquota:
  881. qtype = USRQUOTA;
  882. goto set_qf_name;
  883. case Opt_grpjquota:
  884. qtype = GRPQUOTA;
  885. set_qf_name:
  886. if (sb_any_quota_enabled(sb)) {
  887. printk(KERN_ERR
  888. "EXT4-fs: Cannot change journalled "
  889. "quota options when quota turned on.\n");
  890. return 0;
  891. }
  892. qname = match_strdup(&args[0]);
  893. if (!qname) {
  894. printk(KERN_ERR
  895. "EXT4-fs: not enough memory for "
  896. "storing quotafile name.\n");
  897. return 0;
  898. }
  899. if (sbi->s_qf_names[qtype] &&
  900. strcmp(sbi->s_qf_names[qtype], qname)) {
  901. printk(KERN_ERR
  902. "EXT4-fs: %s quota file already "
  903. "specified.\n", QTYPE2NAME(qtype));
  904. kfree(qname);
  905. return 0;
  906. }
  907. sbi->s_qf_names[qtype] = qname;
  908. if (strchr(sbi->s_qf_names[qtype], '/')) {
  909. printk(KERN_ERR
  910. "EXT4-fs: quotafile must be on "
  911. "filesystem root.\n");
  912. kfree(sbi->s_qf_names[qtype]);
  913. sbi->s_qf_names[qtype] = NULL;
  914. return 0;
  915. }
  916. set_opt(sbi->s_mount_opt, QUOTA);
  917. break;
  918. case Opt_offusrjquota:
  919. qtype = USRQUOTA;
  920. goto clear_qf_name;
  921. case Opt_offgrpjquota:
  922. qtype = GRPQUOTA;
  923. clear_qf_name:
  924. if (sb_any_quota_enabled(sb)) {
  925. printk(KERN_ERR "EXT4-fs: Cannot change "
  926. "journalled quota options when "
  927. "quota turned on.\n");
  928. return 0;
  929. }
  930. /*
  931. * The space will be released later when all options
  932. * are confirmed to be correct
  933. */
  934. sbi->s_qf_names[qtype] = NULL;
  935. break;
  936. case Opt_jqfmt_vfsold:
  937. sbi->s_jquota_fmt = QFMT_VFS_OLD;
  938. break;
  939. case Opt_jqfmt_vfsv0:
  940. sbi->s_jquota_fmt = QFMT_VFS_V0;
  941. break;
  942. case Opt_quota:
  943. case Opt_usrquota:
  944. set_opt(sbi->s_mount_opt, QUOTA);
  945. set_opt(sbi->s_mount_opt, USRQUOTA);
  946. break;
  947. case Opt_grpquota:
  948. set_opt(sbi->s_mount_opt, QUOTA);
  949. set_opt(sbi->s_mount_opt, GRPQUOTA);
  950. break;
  951. case Opt_noquota:
  952. if (sb_any_quota_enabled(sb)) {
  953. printk(KERN_ERR "EXT4-fs: Cannot change quota "
  954. "options when quota turned on.\n");
  955. return 0;
  956. }
  957. clear_opt(sbi->s_mount_opt, QUOTA);
  958. clear_opt(sbi->s_mount_opt, USRQUOTA);
  959. clear_opt(sbi->s_mount_opt, GRPQUOTA);
  960. break;
  961. #else
  962. case Opt_quota:
  963. case Opt_usrquota:
  964. case Opt_grpquota:
  965. case Opt_usrjquota:
  966. case Opt_grpjquota:
  967. case Opt_offusrjquota:
  968. case Opt_offgrpjquota:
  969. case Opt_jqfmt_vfsold:
  970. case Opt_jqfmt_vfsv0:
  971. printk(KERN_ERR
  972. "EXT4-fs: journalled quota options not "
  973. "supported.\n");
  974. break;
  975. case Opt_noquota:
  976. break;
  977. #endif
  978. case Opt_abort:
  979. set_opt(sbi->s_mount_opt, ABORT);
  980. break;
  981. case Opt_barrier:
  982. if (match_int(&args[0], &option))
  983. return 0;
  984. if (option)
  985. set_opt(sbi->s_mount_opt, BARRIER);
  986. else
  987. clear_opt(sbi->s_mount_opt, BARRIER);
  988. break;
  989. case Opt_ignore:
  990. break;
  991. case Opt_resize:
  992. if (!is_remount) {
  993. printk("EXT4-fs: resize option only available "
  994. "for remount\n");
  995. return 0;
  996. }
  997. if (match_int(&args[0], &option) != 0)
  998. return 0;
  999. *n_blocks_count = option;
  1000. break;
  1001. case Opt_nobh:
  1002. set_opt(sbi->s_mount_opt, NOBH);
  1003. break;
  1004. case Opt_bh:
  1005. clear_opt(sbi->s_mount_opt, NOBH);
  1006. break;
  1007. case Opt_extents:
  1008. set_opt (sbi->s_mount_opt, EXTENTS);
  1009. break;
  1010. default:
  1011. printk (KERN_ERR
  1012. "EXT4-fs: Unrecognized mount option \"%s\" "
  1013. "or missing value\n", p);
  1014. return 0;
  1015. }
  1016. }
  1017. #ifdef CONFIG_QUOTA
  1018. if (sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
  1019. if ((sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA) &&
  1020. sbi->s_qf_names[USRQUOTA])
  1021. clear_opt(sbi->s_mount_opt, USRQUOTA);
  1022. if ((sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA) &&
  1023. sbi->s_qf_names[GRPQUOTA])
  1024. clear_opt(sbi->s_mount_opt, GRPQUOTA);
  1025. if ((sbi->s_qf_names[USRQUOTA] &&
  1026. (sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA)) ||
  1027. (sbi->s_qf_names[GRPQUOTA] &&
  1028. (sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA))) {
  1029. printk(KERN_ERR "EXT4-fs: old and new quota "
  1030. "format mixing.\n");
  1031. return 0;
  1032. }
  1033. if (!sbi->s_jquota_fmt) {
  1034. printk(KERN_ERR "EXT4-fs: journalled quota format "
  1035. "not specified.\n");
  1036. return 0;
  1037. }
  1038. } else {
  1039. if (sbi->s_jquota_fmt) {
  1040. printk(KERN_ERR "EXT4-fs: journalled quota format "
  1041. "specified with no journalling "
  1042. "enabled.\n");
  1043. return 0;
  1044. }
  1045. }
  1046. #endif
  1047. return 1;
  1048. }
  1049. static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
  1050. int read_only)
  1051. {
  1052. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1053. int res = 0;
  1054. if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) {
  1055. printk (KERN_ERR "EXT4-fs warning: revision level too high, "
  1056. "forcing read-only mode\n");
  1057. res = MS_RDONLY;
  1058. }
  1059. if (read_only)
  1060. return res;
  1061. if (!(sbi->s_mount_state & EXT4_VALID_FS))
  1062. printk (KERN_WARNING "EXT4-fs warning: mounting unchecked fs, "
  1063. "running e2fsck is recommended\n");
  1064. else if ((sbi->s_mount_state & EXT4_ERROR_FS))
  1065. printk (KERN_WARNING
  1066. "EXT4-fs warning: mounting fs with errors, "
  1067. "running e2fsck is recommended\n");
  1068. else if ((__s16) le16_to_cpu(es->s_max_mnt_count) >= 0 &&
  1069. le16_to_cpu(es->s_mnt_count) >=
  1070. (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
  1071. printk (KERN_WARNING
  1072. "EXT4-fs warning: maximal mount count reached, "
  1073. "running e2fsck is recommended\n");
  1074. else if (le32_to_cpu(es->s_checkinterval) &&
  1075. (le32_to_cpu(es->s_lastcheck) +
  1076. le32_to_cpu(es->s_checkinterval) <= get_seconds()))
  1077. printk (KERN_WARNING
  1078. "EXT4-fs warning: checktime reached, "
  1079. "running e2fsck is recommended\n");
  1080. #if 0
  1081. /* @@@ We _will_ want to clear the valid bit if we find
  1082. * inconsistencies, to force a fsck at reboot. But for
  1083. * a plain journaled filesystem we can keep it set as
  1084. * valid forever! :)
  1085. */
  1086. es->s_state = cpu_to_le16(le16_to_cpu(es->s_state) & ~EXT4_VALID_FS);
  1087. #endif
  1088. if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
  1089. es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT);
  1090. es->s_mnt_count=cpu_to_le16(le16_to_cpu(es->s_mnt_count) + 1);
  1091. es->s_mtime = cpu_to_le32(get_seconds());
  1092. ext4_update_dynamic_rev(sb);
  1093. EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
  1094. ext4_commit_super(sb, es, 1);
  1095. if (test_opt(sb, DEBUG))
  1096. printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%lu, "
  1097. "bpg=%lu, ipg=%lu, mo=%04lx]\n",
  1098. sb->s_blocksize,
  1099. sbi->s_groups_count,
  1100. EXT4_BLOCKS_PER_GROUP(sb),
  1101. EXT4_INODES_PER_GROUP(sb),
  1102. sbi->s_mount_opt);
  1103. printk(KERN_INFO "EXT4 FS on %s, ", sb->s_id);
  1104. if (EXT4_SB(sb)->s_journal->j_inode == NULL) {
  1105. char b[BDEVNAME_SIZE];
  1106. printk("external journal on %s\n",
  1107. bdevname(EXT4_SB(sb)->s_journal->j_dev, b));
  1108. } else {
  1109. printk("internal journal\n");
  1110. }
  1111. return res;
  1112. }
  1113. /* Called at mount-time, super-block is locked */
  1114. static int ext4_check_descriptors (struct super_block * sb)
  1115. {
  1116. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1117. ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block);
  1118. ext4_fsblk_t last_block;
  1119. ext4_fsblk_t block_bitmap;
  1120. ext4_fsblk_t inode_bitmap;
  1121. ext4_fsblk_t inode_table;
  1122. struct ext4_group_desc * gdp = NULL;
  1123. int desc_block = 0;
  1124. int i;
  1125. ext4_debug ("Checking group descriptors");
  1126. for (i = 0; i < sbi->s_groups_count; i++)
  1127. {
  1128. if (i == sbi->s_groups_count - 1)
  1129. last_block = ext4_blocks_count(sbi->s_es) - 1;
  1130. else
  1131. last_block = first_block +
  1132. (EXT4_BLOCKS_PER_GROUP(sb) - 1);
  1133. if ((i % EXT4_DESC_PER_BLOCK(sb)) == 0)
  1134. gdp = (struct ext4_group_desc *)
  1135. sbi->s_group_desc[desc_block++]->b_data;
  1136. block_bitmap = ext4_block_bitmap(sb, gdp);
  1137. if (block_bitmap < first_block || block_bitmap > last_block)
  1138. {
  1139. ext4_error (sb, "ext4_check_descriptors",
  1140. "Block bitmap for group %d"
  1141. " not in group (block %llu)!",
  1142. i, block_bitmap);
  1143. return 0;
  1144. }
  1145. inode_bitmap = ext4_inode_bitmap(sb, gdp);
  1146. if (inode_bitmap < first_block || inode_bitmap > last_block)
  1147. {
  1148. ext4_error (sb, "ext4_check_descriptors",
  1149. "Inode bitmap for group %d"
  1150. " not in group (block %llu)!",
  1151. i, inode_bitmap);
  1152. return 0;
  1153. }
  1154. inode_table = ext4_inode_table(sb, gdp);
  1155. if (inode_table < first_block ||
  1156. inode_table + sbi->s_itb_per_group > last_block)
  1157. {
  1158. ext4_error (sb, "ext4_check_descriptors",
  1159. "Inode table for group %d"
  1160. " not in group (block %llu)!",
  1161. i, inode_table);
  1162. return 0;
  1163. }
  1164. first_block += EXT4_BLOCKS_PER_GROUP(sb);
  1165. gdp = (struct ext4_group_desc *)
  1166. ((__u8 *)gdp + EXT4_DESC_SIZE(sb));
  1167. }
  1168. ext4_free_blocks_count_set(sbi->s_es, ext4_count_free_blocks(sb));
  1169. sbi->s_es->s_free_inodes_count=cpu_to_le32(ext4_count_free_inodes(sb));
  1170. return 1;
  1171. }
  1172. /* ext4_orphan_cleanup() walks a singly-linked list of inodes (starting at
  1173. * the superblock) which were deleted from all directories, but held open by
  1174. * a process at the time of a crash. We walk the list and try to delete these
  1175. * inodes at recovery time (only with a read-write filesystem).
  1176. *
  1177. * In order to keep the orphan inode chain consistent during traversal (in
  1178. * case of crash during recovery), we link each inode into the superblock
  1179. * orphan list_head and handle it the same way as an inode deletion during
  1180. * normal operation (which journals the operations for us).
  1181. *
  1182. * We only do an iget() and an iput() on each inode, which is very safe if we
  1183. * accidentally point at an in-use or already deleted inode. The worst that
  1184. * can happen in this case is that we get a "bit already cleared" message from
  1185. * ext4_free_inode(). The only reason we would point at a wrong inode is if
  1186. * e2fsck was run on this filesystem, and it must have already done the orphan
  1187. * inode cleanup for us, so we can safely abort without any further action.
  1188. */
  1189. static void ext4_orphan_cleanup (struct super_block * sb,
  1190. struct ext4_super_block * es)
  1191. {
  1192. unsigned int s_flags = sb->s_flags;
  1193. int nr_orphans = 0, nr_truncates = 0;
  1194. #ifdef CONFIG_QUOTA
  1195. int i;
  1196. #endif
  1197. if (!es->s_last_orphan) {
  1198. jbd_debug(4, "no orphan inodes to clean up\n");
  1199. return;
  1200. }
  1201. if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
  1202. if (es->s_last_orphan)
  1203. jbd_debug(1, "Errors on filesystem, "
  1204. "clearing orphan list.\n");
  1205. es->s_last_orphan = 0;
  1206. jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
  1207. return;
  1208. }
  1209. if (s_flags & MS_RDONLY) {
  1210. printk(KERN_INFO "EXT4-fs: %s: orphan cleanup on readonly fs\n",
  1211. sb->s_id);
  1212. sb->s_flags &= ~MS_RDONLY;
  1213. }
  1214. #ifdef CONFIG_QUOTA
  1215. /* Needed for iput() to work correctly and not trash data */
  1216. sb->s_flags |= MS_ACTIVE;
  1217. /* Turn on quotas so that they are updated correctly */
  1218. for (i = 0; i < MAXQUOTAS; i++) {
  1219. if (EXT4_SB(sb)->s_qf_names[i]) {
  1220. int ret = ext4_quota_on_mount(sb, i);
  1221. if (ret < 0)
  1222. printk(KERN_ERR
  1223. "EXT4-fs: Cannot turn on journalled "
  1224. "quota: error %d\n", ret);
  1225. }
  1226. }
  1227. #endif
  1228. while (es->s_last_orphan) {
  1229. struct inode *inode;
  1230. if (!(inode =
  1231. ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan)))) {
  1232. es->s_last_orphan = 0;
  1233. break;
  1234. }
  1235. list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
  1236. DQUOT_INIT(inode);
  1237. if (inode->i_nlink) {
  1238. printk(KERN_DEBUG
  1239. "%s: truncating inode %lu to %Ld bytes\n",
  1240. __FUNCTION__, inode->i_ino, inode->i_size);
  1241. jbd_debug(2, "truncating inode %lu to %Ld bytes\n",
  1242. inode->i_ino, inode->i_size);
  1243. ext4_truncate(inode);
  1244. nr_truncates++;
  1245. } else {
  1246. printk(KERN_DEBUG
  1247. "%s: deleting unreferenced inode %lu\n",
  1248. __FUNCTION__, inode->i_ino);
  1249. jbd_debug(2, "deleting unreferenced inode %lu\n",
  1250. inode->i_ino);
  1251. nr_orphans++;
  1252. }
  1253. iput(inode); /* The delete magic happens here! */
  1254. }
  1255. #define PLURAL(x) (x), ((x)==1) ? "" : "s"
  1256. if (nr_orphans)
  1257. printk(KERN_INFO "EXT4-fs: %s: %d orphan inode%s deleted\n",
  1258. sb->s_id, PLURAL(nr_orphans));
  1259. if (nr_truncates)
  1260. printk(KERN_INFO "EXT4-fs: %s: %d truncate%s cleaned up\n",
  1261. sb->s_id, PLURAL(nr_truncates));
  1262. #ifdef CONFIG_QUOTA
  1263. /* Turn quotas off */
  1264. for (i = 0; i < MAXQUOTAS; i++) {
  1265. if (sb_dqopt(sb)->files[i])
  1266. vfs_quota_off(sb, i);
  1267. }
  1268. #endif
  1269. sb->s_flags = s_flags; /* Restore MS_RDONLY status */
  1270. }
  1271. #define log2(n) ffz(~(n))
  1272. /*
  1273. * Maximal file size. There is a direct, and {,double-,triple-}indirect
  1274. * block limit, and also a limit of (2^32 - 1) 512-byte sectors in i_blocks.
  1275. * We need to be 1 filesystem block less than the 2^32 sector limit.
  1276. */
  1277. static loff_t ext4_max_size(int bits)
  1278. {
  1279. loff_t res = EXT4_NDIR_BLOCKS;
  1280. /* This constant is calculated to be the largest file size for a
  1281. * dense, 4k-blocksize file such that the total number of
  1282. * sectors in the file, including data and all indirect blocks,
  1283. * does not exceed 2^32. */
  1284. const loff_t upper_limit = 0x1ff7fffd000LL;
  1285. res += 1LL << (bits-2);
  1286. res += 1LL << (2*(bits-2));
  1287. res += 1LL << (3*(bits-2));
  1288. res <<= bits;
  1289. if (res > upper_limit)
  1290. res = upper_limit;
  1291. return res;
  1292. }
  1293. static ext4_fsblk_t descriptor_loc(struct super_block *sb,
  1294. ext4_fsblk_t logical_sb_block, int nr)
  1295. {
  1296. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1297. unsigned long bg, first_meta_bg;
  1298. int has_super = 0;
  1299. first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
  1300. if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_META_BG) ||
  1301. nr < first_meta_bg)
  1302. return logical_sb_block + nr + 1;
  1303. bg = sbi->s_desc_per_block * nr;
  1304. if (ext4_bg_has_super(sb, bg))
  1305. has_super = 1;
  1306. return (has_super + ext4_group_first_block_no(sb, bg));
  1307. }
  1308. static int ext4_fill_super (struct super_block *sb, void *data, int silent)
  1309. {
  1310. struct buffer_head * bh;
  1311. struct ext4_super_block *es = NULL;
  1312. struct ext4_sb_info *sbi;
  1313. ext4_fsblk_t block;
  1314. ext4_fsblk_t sb_block = get_sb_block(&data);
  1315. ext4_fsblk_t logical_sb_block;
  1316. unsigned long offset = 0;
  1317. unsigned int journal_inum = 0;
  1318. unsigned long journal_devnum = 0;
  1319. unsigned long def_mount_opts;
  1320. struct inode *root;
  1321. int blocksize;
  1322. int hblock;
  1323. int db_count;
  1324. int i;
  1325. int needs_recovery;
  1326. __le32 features;
  1327. __u64 blocks_count;
  1328. sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
  1329. if (!sbi)
  1330. return -ENOMEM;
  1331. sb->s_fs_info = sbi;
  1332. sbi->s_mount_opt = 0;
  1333. sbi->s_resuid = EXT4_DEF_RESUID;
  1334. sbi->s_resgid = EXT4_DEF_RESGID;
  1335. unlock_kernel();
  1336. blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE);
  1337. if (!blocksize) {
  1338. printk(KERN_ERR "EXT4-fs: unable to set blocksize\n");
  1339. goto out_fail;
  1340. }
  1341. /*
  1342. * The ext4 superblock will not be buffer aligned for other than 1kB
  1343. * block sizes. We need to calculate the offset from buffer start.
  1344. */
  1345. if (blocksize != EXT4_MIN_BLOCK_SIZE) {
  1346. logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
  1347. offset = do_div(logical_sb_block, blocksize);
  1348. } else {
  1349. logical_sb_block = sb_block;
  1350. }
  1351. if (!(bh = sb_bread(sb, logical_sb_block))) {
  1352. printk (KERN_ERR "EXT4-fs: unable to read superblock\n");
  1353. goto out_fail;
  1354. }
  1355. /*
  1356. * Note: s_es must be initialized as soon as possible because
  1357. * some ext4 macro-instructions depend on its value
  1358. */
  1359. es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
  1360. sbi->s_es = es;
  1361. sb->s_magic = le16_to_cpu(es->s_magic);
  1362. if (sb->s_magic != EXT4_SUPER_MAGIC)
  1363. goto cantfind_ext4;
  1364. /* Set defaults before we parse the mount options */
  1365. def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
  1366. if (def_mount_opts & EXT4_DEFM_DEBUG)
  1367. set_opt(sbi->s_mount_opt, DEBUG);
  1368. if (def_mount_opts & EXT4_DEFM_BSDGROUPS)
  1369. set_opt(sbi->s_mount_opt, GRPID);
  1370. if (def_mount_opts & EXT4_DEFM_UID16)
  1371. set_opt(sbi->s_mount_opt, NO_UID32);
  1372. if (def_mount_opts & EXT4_DEFM_XATTR_USER)
  1373. set_opt(sbi->s_mount_opt, XATTR_USER);
  1374. if (def_mount_opts & EXT4_DEFM_ACL)
  1375. set_opt(sbi->s_mount_opt, POSIX_ACL);
  1376. if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA)
  1377. sbi->s_mount_opt |= EXT4_MOUNT_JOURNAL_DATA;
  1378. else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED)
  1379. sbi->s_mount_opt |= EXT4_MOUNT_ORDERED_DATA;
  1380. else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK)
  1381. sbi->s_mount_opt |= EXT4_MOUNT_WRITEBACK_DATA;
  1382. if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_PANIC)
  1383. set_opt(sbi->s_mount_opt, ERRORS_PANIC);
  1384. else if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_RO)
  1385. set_opt(sbi->s_mount_opt, ERRORS_RO);
  1386. else
  1387. set_opt(sbi->s_mount_opt, ERRORS_CONT);
  1388. sbi->s_resuid = le16_to_cpu(es->s_def_resuid);
  1389. sbi->s_resgid = le16_to_cpu(es->s_def_resgid);
  1390. set_opt(sbi->s_mount_opt, RESERVATION);
  1391. if (!parse_options ((char *) data, sb, &journal_inum, &journal_devnum,
  1392. NULL, 0))
  1393. goto failed_mount;
  1394. sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
  1395. ((sbi->s_mount_opt & EXT4_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
  1396. if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
  1397. (EXT4_HAS_COMPAT_FEATURE(sb, ~0U) ||
  1398. EXT4_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
  1399. EXT4_HAS_INCOMPAT_FEATURE(sb, ~0U)))
  1400. printk(KERN_WARNING
  1401. "EXT4-fs warning: feature flags set on rev 0 fs, "
  1402. "running e2fsck is recommended\n");
  1403. /*
  1404. * Check feature flags regardless of the revision level, since we
  1405. * previously didn't change the revision level when setting the flags,
  1406. * so there is a chance incompat flags are set on a rev 0 filesystem.
  1407. */
  1408. features = EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT4_FEATURE_INCOMPAT_SUPP);
  1409. if (features) {
  1410. printk(KERN_ERR "EXT4-fs: %s: couldn't mount because of "
  1411. "unsupported optional features (%x).\n",
  1412. sb->s_id, le32_to_cpu(features));
  1413. goto failed_mount;
  1414. }
  1415. features = EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT4_FEATURE_RO_COMPAT_SUPP);
  1416. if (!(sb->s_flags & MS_RDONLY) && features) {
  1417. printk(KERN_ERR "EXT4-fs: %s: couldn't mount RDWR because of "
  1418. "unsupported optional features (%x).\n",
  1419. sb->s_id, le32_to_cpu(features));
  1420. goto failed_mount;
  1421. }
  1422. blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
  1423. if (blocksize < EXT4_MIN_BLOCK_SIZE ||
  1424. blocksize > EXT4_MAX_BLOCK_SIZE) {
  1425. printk(KERN_ERR
  1426. "EXT4-fs: Unsupported filesystem blocksize %d on %s.\n",
  1427. blocksize, sb->s_id);
  1428. goto failed_mount;
  1429. }
  1430. hblock = bdev_hardsect_size(sb->s_bdev);
  1431. if (sb->s_blocksize != blocksize) {
  1432. /*
  1433. * Make sure the blocksize for the filesystem is larger
  1434. * than the hardware sectorsize for the machine.
  1435. */
  1436. if (blocksize < hblock) {
  1437. printk(KERN_ERR "EXT4-fs: blocksize %d too small for "
  1438. "device blocksize %d.\n", blocksize, hblock);
  1439. goto failed_mount;
  1440. }
  1441. brelse (bh);
  1442. sb_set_blocksize(sb, blocksize);
  1443. logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
  1444. offset = do_div(logical_sb_block, blocksize);
  1445. bh = sb_bread(sb, logical_sb_block);
  1446. if (!bh) {
  1447. printk(KERN_ERR
  1448. "EXT4-fs: Can't read superblock on 2nd try.\n");
  1449. goto failed_mount;
  1450. }
  1451. es = (struct ext4_super_block *)(((char *)bh->b_data) + offset);
  1452. sbi->s_es = es;
  1453. if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
  1454. printk (KERN_ERR
  1455. "EXT4-fs: Magic mismatch, very weird !\n");
  1456. goto failed_mount;
  1457. }
  1458. }
  1459. sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits);
  1460. if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
  1461. sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
  1462. sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
  1463. } else {
  1464. sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
  1465. sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
  1466. if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
  1467. (sbi->s_inode_size & (sbi->s_inode_size - 1)) ||
  1468. (sbi->s_inode_size > blocksize)) {
  1469. printk (KERN_ERR
  1470. "EXT4-fs: unsupported inode size: %d\n",
  1471. sbi->s_inode_size);
  1472. goto failed_mount;
  1473. }
  1474. }
  1475. sbi->s_frag_size = EXT4_MIN_FRAG_SIZE <<
  1476. le32_to_cpu(es->s_log_frag_size);
  1477. if (blocksize != sbi->s_frag_size) {
  1478. printk(KERN_ERR
  1479. "EXT4-fs: fragsize %lu != blocksize %u (unsupported)\n",
  1480. sbi->s_frag_size, blocksize);
  1481. goto failed_mount;
  1482. }
  1483. sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
  1484. if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT)) {
  1485. if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
  1486. sbi->s_desc_size > EXT4_MAX_DESC_SIZE ||
  1487. sbi->s_desc_size & (sbi->s_desc_size - 1)) {
  1488. printk(KERN_ERR
  1489. "EXT4-fs: unsupported descriptor size %lu\n",
  1490. sbi->s_desc_size);
  1491. goto failed_mount;
  1492. }
  1493. } else
  1494. sbi->s_desc_size = EXT4_MIN_DESC_SIZE;
  1495. sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
  1496. sbi->s_frags_per_group = le32_to_cpu(es->s_frags_per_group);
  1497. sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
  1498. if (EXT4_INODE_SIZE(sb) == 0)
  1499. goto cantfind_ext4;
  1500. sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb);
  1501. if (sbi->s_inodes_per_block == 0)
  1502. goto cantfind_ext4;
  1503. sbi->s_itb_per_group = sbi->s_inodes_per_group /
  1504. sbi->s_inodes_per_block;
  1505. sbi->s_desc_per_block = blocksize / EXT4_DESC_SIZE(sb);
  1506. sbi->s_sbh = bh;
  1507. sbi->s_mount_state = le16_to_cpu(es->s_state);
  1508. sbi->s_addr_per_block_bits = log2(EXT4_ADDR_PER_BLOCK(sb));
  1509. sbi->s_desc_per_block_bits = log2(EXT4_DESC_PER_BLOCK(sb));
  1510. for (i=0; i < 4; i++)
  1511. sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
  1512. sbi->s_def_hash_version = es->s_def_hash_version;
  1513. if (sbi->s_blocks_per_group > blocksize * 8) {
  1514. printk (KERN_ERR
  1515. "EXT4-fs: #blocks per group too big: %lu\n",
  1516. sbi->s_blocks_per_group);
  1517. goto failed_mount;
  1518. }
  1519. if (sbi->s_frags_per_group > blocksize * 8) {
  1520. printk (KERN_ERR
  1521. "EXT4-fs: #fragments per group too big: %lu\n",
  1522. sbi->s_frags_per_group);
  1523. goto failed_mount;
  1524. }
  1525. if (sbi->s_inodes_per_group > blocksize * 8) {
  1526. printk (KERN_ERR
  1527. "EXT4-fs: #inodes per group too big: %lu\n",
  1528. sbi->s_inodes_per_group);
  1529. goto failed_mount;
  1530. }
  1531. if (ext4_blocks_count(es) >
  1532. (sector_t)(~0ULL) >> (sb->s_blocksize_bits - 9)) {
  1533. printk(KERN_ERR "EXT4-fs: filesystem on %s:"
  1534. " too large to mount safely\n", sb->s_id);
  1535. if (sizeof(sector_t) < 8)
  1536. printk(KERN_WARNING "EXT4-fs: CONFIG_LBD not "
  1537. "enabled\n");
  1538. goto failed_mount;
  1539. }
  1540. if (EXT4_BLOCKS_PER_GROUP(sb) == 0)
  1541. goto cantfind_ext4;
  1542. blocks_count = (ext4_blocks_count(es) -
  1543. le32_to_cpu(es->s_first_data_block) +
  1544. EXT4_BLOCKS_PER_GROUP(sb) - 1);
  1545. do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
  1546. sbi->s_groups_count = blocks_count;
  1547. db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
  1548. EXT4_DESC_PER_BLOCK(sb);
  1549. sbi->s_group_desc = kmalloc(db_count * sizeof (struct buffer_head *),
  1550. GFP_KERNEL);
  1551. if (sbi->s_group_desc == NULL) {
  1552. printk (KERN_ERR "EXT4-fs: not enough memory\n");
  1553. goto failed_mount;
  1554. }
  1555. bgl_lock_init(&sbi->s_blockgroup_lock);
  1556. for (i = 0; i < db_count; i++) {
  1557. block = descriptor_loc(sb, logical_sb_block, i);
  1558. sbi->s_group_desc[i] = sb_bread(sb, block);
  1559. if (!sbi->s_group_desc[i]) {
  1560. printk (KERN_ERR "EXT4-fs: "
  1561. "can't read group descriptor %d\n", i);
  1562. db_count = i;
  1563. goto failed_mount2;
  1564. }
  1565. }
  1566. if (!ext4_check_descriptors (sb)) {
  1567. printk(KERN_ERR "EXT4-fs: group descriptors corrupted!\n");
  1568. goto failed_mount2;
  1569. }
  1570. sbi->s_gdb_count = db_count;
  1571. get_random_bytes(&sbi->s_next_generation, sizeof(u32));
  1572. spin_lock_init(&sbi->s_next_gen_lock);
  1573. percpu_counter_init(&sbi->s_freeblocks_counter,
  1574. ext4_count_free_blocks(sb));
  1575. percpu_counter_init(&sbi->s_freeinodes_counter,
  1576. ext4_count_free_inodes(sb));
  1577. percpu_counter_init(&sbi->s_dirs_counter,
  1578. ext4_count_dirs(sb));
  1579. /* per fileystem reservation list head & lock */
  1580. spin_lock_init(&sbi->s_rsv_window_lock);
  1581. sbi->s_rsv_window_root = RB_ROOT;
  1582. /* Add a single, static dummy reservation to the start of the
  1583. * reservation window list --- it gives us a placeholder for
  1584. * append-at-start-of-list which makes the allocation logic
  1585. * _much_ simpler. */
  1586. sbi->s_rsv_window_head.rsv_start = EXT4_RESERVE_WINDOW_NOT_ALLOCATED;
  1587. sbi->s_rsv_window_head.rsv_end = EXT4_RESERVE_WINDOW_NOT_ALLOCATED;
  1588. sbi->s_rsv_window_head.rsv_alloc_hit = 0;
  1589. sbi->s_rsv_window_head.rsv_goal_size = 0;
  1590. ext4_rsv_window_add(sb, &sbi->s_rsv_window_head);
  1591. /*
  1592. * set up enough so that it can read an inode
  1593. */
  1594. sb->s_op = &ext4_sops;
  1595. sb->s_export_op = &ext4_export_ops;
  1596. sb->s_xattr = ext4_xattr_handlers;
  1597. #ifdef CONFIG_QUOTA
  1598. sb->s_qcop = &ext4_qctl_operations;
  1599. sb->dq_op = &ext4_quota_operations;
  1600. #endif
  1601. INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
  1602. sb->s_root = NULL;
  1603. needs_recovery = (es->s_last_orphan != 0 ||
  1604. EXT4_HAS_INCOMPAT_FEATURE(sb,
  1605. EXT4_FEATURE_INCOMPAT_RECOVER));
  1606. /*
  1607. * The first inode we look at is the journal inode. Don't try
  1608. * root first: it may be modified in the journal!
  1609. */
  1610. if (!test_opt(sb, NOLOAD) &&
  1611. EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
  1612. if (ext4_load_journal(sb, es, journal_devnum))
  1613. goto failed_mount3;
  1614. } else if (journal_inum) {
  1615. if (ext4_create_journal(sb, es, journal_inum))
  1616. goto failed_mount3;
  1617. } else {
  1618. if (!silent)
  1619. printk (KERN_ERR
  1620. "ext4: No journal on filesystem on %s\n",
  1621. sb->s_id);
  1622. goto failed_mount3;
  1623. }
  1624. /* We have now updated the journal if required, so we can
  1625. * validate the data journaling mode. */
  1626. switch (test_opt(sb, DATA_FLAGS)) {
  1627. case 0:
  1628. /* No mode set, assume a default based on the journal
  1629. * capabilities: ORDERED_DATA if the journal can
  1630. * cope, else JOURNAL_DATA
  1631. */
  1632. if (jbd2_journal_check_available_features
  1633. (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE))
  1634. set_opt(sbi->s_mount_opt, ORDERED_DATA);
  1635. else
  1636. set_opt(sbi->s_mount_opt, JOURNAL_DATA);
  1637. break;
  1638. case EXT4_MOUNT_ORDERED_DATA:
  1639. case EXT4_MOUNT_WRITEBACK_DATA:
  1640. if (!jbd2_journal_check_available_features
  1641. (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
  1642. printk(KERN_ERR "EXT4-fs: Journal does not support "
  1643. "requested data journaling mode\n");
  1644. goto failed_mount4;
  1645. }
  1646. default:
  1647. break;
  1648. }
  1649. if (test_opt(sb, NOBH)) {
  1650. if (!(test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)) {
  1651. printk(KERN_WARNING "EXT4-fs: Ignoring nobh option - "
  1652. "its supported only with writeback mode\n");
  1653. clear_opt(sbi->s_mount_opt, NOBH);
  1654. }
  1655. }
  1656. /*
  1657. * The jbd2_journal_load will have done any necessary log recovery,
  1658. * so we can safely mount the rest of the filesystem now.
  1659. */
  1660. root = iget(sb, EXT4_ROOT_INO);
  1661. sb->s_root = d_alloc_root(root);
  1662. if (!sb->s_root) {
  1663. printk(KERN_ERR "EXT4-fs: get root inode failed\n");
  1664. iput(root);
  1665. goto failed_mount4;
  1666. }
  1667. if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
  1668. dput(sb->s_root);
  1669. sb->s_root = NULL;
  1670. printk(KERN_ERR "EXT4-fs: corrupt root inode, run e2fsck\n");
  1671. goto failed_mount4;
  1672. }
  1673. ext4_setup_super (sb, es, sb->s_flags & MS_RDONLY);
  1674. /*
  1675. * akpm: core read_super() calls in here with the superblock locked.
  1676. * That deadlocks, because orphan cleanup needs to lock the superblock
  1677. * in numerous places. Here we just pop the lock - it's relatively
  1678. * harmless, because we are now ready to accept write_super() requests,
  1679. * and aviro says that's the only reason for hanging onto the
  1680. * superblock lock.
  1681. */
  1682. EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
  1683. ext4_orphan_cleanup(sb, es);
  1684. EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
  1685. if (needs_recovery)
  1686. printk (KERN_INFO "EXT4-fs: recovery complete.\n");
  1687. ext4_mark_recovery_complete(sb, es);
  1688. printk (KERN_INFO "EXT4-fs: mounted filesystem with %s data mode.\n",
  1689. test_opt(sb,DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA ? "journal":
  1690. test_opt(sb,DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA ? "ordered":
  1691. "writeback");
  1692. ext4_ext_init(sb);
  1693. lock_kernel();
  1694. return 0;
  1695. cantfind_ext4:
  1696. if (!silent)
  1697. printk(KERN_ERR "VFS: Can't find ext4 filesystem on dev %s.\n",
  1698. sb->s_id);
  1699. goto failed_mount;
  1700. failed_mount4:
  1701. jbd2_journal_destroy(sbi->s_journal);
  1702. failed_mount3:
  1703. percpu_counter_destroy(&sbi->s_freeblocks_counter);
  1704. percpu_counter_destroy(&sbi->s_freeinodes_counter);
  1705. percpu_counter_destroy(&sbi->s_dirs_counter);
  1706. failed_mount2:
  1707. for (i = 0; i < db_count; i++)
  1708. brelse(sbi->s_group_desc[i]);
  1709. kfree(sbi->s_group_desc);
  1710. failed_mount:
  1711. #ifdef CONFIG_QUOTA
  1712. for (i = 0; i < MAXQUOTAS; i++)
  1713. kfree(sbi->s_qf_names[i]);
  1714. #endif
  1715. ext4_blkdev_remove(sbi);
  1716. brelse(bh);
  1717. out_fail:
  1718. sb->s_fs_info = NULL;
  1719. kfree(sbi);
  1720. lock_kernel();
  1721. return -EINVAL;
  1722. }
  1723. /*
  1724. * Setup any per-fs journal parameters now. We'll do this both on
  1725. * initial mount, once the journal has been initialised but before we've
  1726. * done any recovery; and again on any subsequent remount.
  1727. */
  1728. static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
  1729. {
  1730. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1731. if (sbi->s_commit_interval)
  1732. journal->j_commit_interval = sbi->s_commit_interval;
  1733. /* We could also set up an ext4-specific default for the commit
  1734. * interval here, but for now we'll just fall back to the jbd
  1735. * default. */
  1736. spin_lock(&journal->j_state_lock);
  1737. if (test_opt(sb, BARRIER))
  1738. journal->j_flags |= JBD2_BARRIER;
  1739. else
  1740. journal->j_flags &= ~JBD2_BARRIER;
  1741. spin_unlock(&journal->j_state_lock);
  1742. }
  1743. static journal_t *ext4_get_journal(struct super_block *sb,
  1744. unsigned int journal_inum)
  1745. {
  1746. struct inode *journal_inode;
  1747. journal_t *journal;
  1748. /* First, test for the existence of a valid inode on disk. Bad
  1749. * things happen if we iget() an unused inode, as the subsequent
  1750. * iput() will try to delete it. */
  1751. journal_inode = iget(sb, journal_inum);
  1752. if (!journal_inode) {
  1753. printk(KERN_ERR "EXT4-fs: no journal found.\n");
  1754. return NULL;
  1755. }
  1756. if (!journal_inode->i_nlink) {
  1757. make_bad_inode(journal_inode);
  1758. iput(journal_inode);
  1759. printk(KERN_ERR "EXT4-fs: journal inode is deleted.\n");
  1760. return NULL;
  1761. }
  1762. jbd_debug(2, "Journal inode found at %p: %Ld bytes\n",
  1763. journal_inode, journal_inode->i_size);
  1764. if (is_bad_inode(journal_inode) || !S_ISREG(journal_inode->i_mode)) {
  1765. printk(KERN_ERR "EXT4-fs: invalid journal inode.\n");
  1766. iput(journal_inode);
  1767. return NULL;
  1768. }
  1769. journal = jbd2_journal_init_inode(journal_inode);
  1770. if (!journal) {
  1771. printk(KERN_ERR "EXT4-fs: Could not load journal inode\n");
  1772. iput(journal_inode);
  1773. return NULL;
  1774. }
  1775. journal->j_private = sb;
  1776. ext4_init_journal_params(sb, journal);
  1777. return journal;
  1778. }
  1779. static journal_t *ext4_get_dev_journal(struct super_block *sb,
  1780. dev_t j_dev)
  1781. {
  1782. struct buffer_head * bh;
  1783. journal_t *journal;
  1784. ext4_fsblk_t start;
  1785. ext4_fsblk_t len;
  1786. int hblock, blocksize;
  1787. ext4_fsblk_t sb_block;
  1788. unsigned long offset;
  1789. struct ext4_super_block * es;
  1790. struct block_device *bdev;
  1791. bdev = ext4_blkdev_get(j_dev);
  1792. if (bdev == NULL)
  1793. return NULL;
  1794. if (bd_claim(bdev, sb)) {
  1795. printk(KERN_ERR
  1796. "EXT4: failed to claim external journal device.\n");
  1797. blkdev_put(bdev);
  1798. return NULL;
  1799. }
  1800. blocksize = sb->s_blocksize;
  1801. hblock = bdev_hardsect_size(bdev);
  1802. if (blocksize < hblock) {
  1803. printk(KERN_ERR
  1804. "EXT4-fs: blocksize too small for journal device.\n");
  1805. goto out_bdev;
  1806. }
  1807. sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
  1808. offset = EXT4_MIN_BLOCK_SIZE % blocksize;
  1809. set_blocksize(bdev, blocksize);
  1810. if (!(bh = __bread(bdev, sb_block, blocksize))) {
  1811. printk(KERN_ERR "EXT4-fs: couldn't read superblock of "
  1812. "external journal\n");
  1813. goto out_bdev;
  1814. }
  1815. es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
  1816. if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
  1817. !(le32_to_cpu(es->s_feature_incompat) &
  1818. EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
  1819. printk(KERN_ERR "EXT4-fs: external journal has "
  1820. "bad superblock\n");
  1821. brelse(bh);
  1822. goto out_bdev;
  1823. }
  1824. if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
  1825. printk(KERN_ERR "EXT4-fs: journal UUID does not match\n");
  1826. brelse(bh);
  1827. goto out_bdev;
  1828. }
  1829. len = ext4_blocks_count(es);
  1830. start = sb_block + 1;
  1831. brelse(bh); /* we're done with the superblock */
  1832. journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
  1833. start, len, blocksize);
  1834. if (!journal) {
  1835. printk(KERN_ERR "EXT4-fs: failed to create device journal\n");
  1836. goto out_bdev;
  1837. }
  1838. journal->j_private = sb;
  1839. ll_rw_block(READ, 1, &journal->j_sb_buffer);
  1840. wait_on_buffer(journal->j_sb_buffer);
  1841. if (!buffer_uptodate(journal->j_sb_buffer)) {
  1842. printk(KERN_ERR "EXT4-fs: I/O error on journal device\n");
  1843. goto out_journal;
  1844. }
  1845. if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
  1846. printk(KERN_ERR "EXT4-fs: External journal has more than one "
  1847. "user (unsupported) - %d\n",
  1848. be32_to_cpu(journal->j_superblock->s_nr_users));
  1849. goto out_journal;
  1850. }
  1851. EXT4_SB(sb)->journal_bdev = bdev;
  1852. ext4_init_journal_params(sb, journal);
  1853. return journal;
  1854. out_journal:
  1855. jbd2_journal_destroy(journal);
  1856. out_bdev:
  1857. ext4_blkdev_put(bdev);
  1858. return NULL;
  1859. }
  1860. static int ext4_load_journal(struct super_block *sb,
  1861. struct ext4_super_block *es,
  1862. unsigned long journal_devnum)
  1863. {
  1864. journal_t *journal;
  1865. unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
  1866. dev_t journal_dev;
  1867. int err = 0;
  1868. int really_read_only;
  1869. if (journal_devnum &&
  1870. journal_devnum != le32_to_cpu(es->s_journal_dev)) {
  1871. printk(KERN_INFO "EXT4-fs: external journal device major/minor "
  1872. "numbers have changed\n");
  1873. journal_dev = new_decode_dev(journal_devnum);
  1874. } else
  1875. journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
  1876. really_read_only = bdev_read_only(sb->s_bdev);
  1877. /*
  1878. * Are we loading a blank journal or performing recovery after a
  1879. * crash? For recovery, we need to check in advance whether we
  1880. * can get read-write access to the device.
  1881. */
  1882. if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
  1883. if (sb->s_flags & MS_RDONLY) {
  1884. printk(KERN_INFO "EXT4-fs: INFO: recovery "
  1885. "required on readonly filesystem.\n");
  1886. if (really_read_only) {
  1887. printk(KERN_ERR "EXT4-fs: write access "
  1888. "unavailable, cannot proceed.\n");
  1889. return -EROFS;
  1890. }
  1891. printk (KERN_INFO "EXT4-fs: write access will "
  1892. "be enabled during recovery.\n");
  1893. }
  1894. }
  1895. if (journal_inum && journal_dev) {
  1896. printk(KERN_ERR "EXT4-fs: filesystem has both journal "
  1897. "and inode journals!\n");
  1898. return -EINVAL;
  1899. }
  1900. if (journal_inum) {
  1901. if (!(journal = ext4_get_journal(sb, journal_inum)))
  1902. return -EINVAL;
  1903. } else {
  1904. if (!(journal = ext4_get_dev_journal(sb, journal_dev)))
  1905. return -EINVAL;
  1906. }
  1907. if (!really_read_only && test_opt(sb, UPDATE_JOURNAL)) {
  1908. err = jbd2_journal_update_format(journal);
  1909. if (err) {
  1910. printk(KERN_ERR "EXT4-fs: error updating journal.\n");
  1911. jbd2_journal_destroy(journal);
  1912. return err;
  1913. }
  1914. }
  1915. if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER))
  1916. err = jbd2_journal_wipe(journal, !really_read_only);
  1917. if (!err)
  1918. err = jbd2_journal_load(journal);
  1919. if (err) {
  1920. printk(KERN_ERR "EXT4-fs: error loading journal.\n");
  1921. jbd2_journal_destroy(journal);
  1922. return err;
  1923. }
  1924. EXT4_SB(sb)->s_journal = journal;
  1925. ext4_clear_journal_err(sb, es);
  1926. if (journal_devnum &&
  1927. journal_devnum != le32_to_cpu(es->s_journal_dev)) {
  1928. es->s_journal_dev = cpu_to_le32(journal_devnum);
  1929. sb->s_dirt = 1;
  1930. /* Make sure we flush the recovery flag to disk. */
  1931. ext4_commit_super(sb, es, 1);
  1932. }
  1933. return 0;
  1934. }
  1935. static int ext4_create_journal(struct super_block * sb,
  1936. struct ext4_super_block * es,
  1937. unsigned int journal_inum)
  1938. {
  1939. journal_t *journal;
  1940. if (sb->s_flags & MS_RDONLY) {
  1941. printk(KERN_ERR "EXT4-fs: readonly filesystem when trying to "
  1942. "create journal.\n");
  1943. return -EROFS;
  1944. }
  1945. if (!(journal = ext4_get_journal(sb, journal_inum)))
  1946. return -EINVAL;
  1947. printk(KERN_INFO "EXT4-fs: creating new journal on inode %u\n",
  1948. journal_inum);
  1949. if (jbd2_journal_create(journal)) {
  1950. printk(KERN_ERR "EXT4-fs: error creating journal.\n");
  1951. jbd2_journal_destroy(journal);
  1952. return -EIO;
  1953. }
  1954. EXT4_SB(sb)->s_journal = journal;
  1955. ext4_update_dynamic_rev(sb);
  1956. EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
  1957. EXT4_SET_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL);
  1958. es->s_journal_inum = cpu_to_le32(journal_inum);
  1959. sb->s_dirt = 1;
  1960. /* Make sure we flush the recovery flag to disk. */
  1961. ext4_commit_super(sb, es, 1);
  1962. return 0;
  1963. }
  1964. static void ext4_commit_super (struct super_block * sb,
  1965. struct ext4_super_block * es,
  1966. int sync)
  1967. {
  1968. struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
  1969. if (!sbh)
  1970. return;
  1971. es->s_wtime = cpu_to_le32(get_seconds());
  1972. ext4_free_blocks_count_set(es, ext4_count_free_blocks(sb));
  1973. es->s_free_inodes_count = cpu_to_le32(ext4_count_free_inodes(sb));
  1974. BUFFER_TRACE(sbh, "marking dirty");
  1975. mark_buffer_dirty(sbh);
  1976. if (sync)
  1977. sync_dirty_buffer(sbh);
  1978. }
  1979. /*
  1980. * Have we just finished recovery? If so, and if we are mounting (or
  1981. * remounting) the filesystem readonly, then we will end up with a
  1982. * consistent fs on disk. Record that fact.
  1983. */
  1984. static void ext4_mark_recovery_complete(struct super_block * sb,
  1985. struct ext4_super_block * es)
  1986. {
  1987. journal_t *journal = EXT4_SB(sb)->s_journal;
  1988. jbd2_journal_lock_updates(journal);
  1989. jbd2_journal_flush(journal);
  1990. if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER) &&
  1991. sb->s_flags & MS_RDONLY) {
  1992. EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
  1993. sb->s_dirt = 0;
  1994. ext4_commit_super(sb, es, 1);
  1995. }
  1996. jbd2_journal_unlock_updates(journal);
  1997. }
  1998. /*
  1999. * If we are mounting (or read-write remounting) a filesystem whose journal
  2000. * has recorded an error from a previous lifetime, move that error to the
  2001. * main filesystem now.
  2002. */
  2003. static void ext4_clear_journal_err(struct super_block * sb,
  2004. struct ext4_super_block * es)
  2005. {
  2006. journal_t *journal;
  2007. int j_errno;
  2008. const char *errstr;
  2009. journal = EXT4_SB(sb)->s_journal;
  2010. /*
  2011. * Now check for any error status which may have been recorded in the
  2012. * journal by a prior ext4_error() or ext4_abort()
  2013. */
  2014. j_errno = jbd2_journal_errno(journal);
  2015. if (j_errno) {
  2016. char nbuf[16];
  2017. errstr = ext4_decode_error(sb, j_errno, nbuf);
  2018. ext4_warning(sb, __FUNCTION__, "Filesystem error recorded "
  2019. "from previous mount: %s", errstr);
  2020. ext4_warning(sb, __FUNCTION__, "Marking fs in need of "
  2021. "filesystem check.");
  2022. EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
  2023. es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
  2024. ext4_commit_super (sb, es, 1);
  2025. jbd2_journal_clear_err(journal);
  2026. }
  2027. }
  2028. /*
  2029. * Force the running and committing transactions to commit,
  2030. * and wait on the commit.
  2031. */
  2032. int ext4_force_commit(struct super_block *sb)
  2033. {
  2034. journal_t *journal;
  2035. int ret;
  2036. if (sb->s_flags & MS_RDONLY)
  2037. return 0;
  2038. journal = EXT4_SB(sb)->s_journal;
  2039. sb->s_dirt = 0;
  2040. ret = ext4_journal_force_commit(journal);
  2041. return ret;
  2042. }
  2043. /*
  2044. * Ext4 always journals updates to the superblock itself, so we don't
  2045. * have to propagate any other updates to the superblock on disk at this
  2046. * point. Just start an async writeback to get the buffers on their way
  2047. * to the disk.
  2048. *
  2049. * This implicitly triggers the writebehind on sync().
  2050. */
  2051. static void ext4_write_super (struct super_block * sb)
  2052. {
  2053. if (mutex_trylock(&sb->s_lock) != 0)
  2054. BUG();
  2055. sb->s_dirt = 0;
  2056. }
  2057. static int ext4_sync_fs(struct super_block *sb, int wait)
  2058. {
  2059. tid_t target;
  2060. sb->s_dirt = 0;
  2061. if (jbd2_journal_start_commit(EXT4_SB(sb)->s_journal, &target)) {
  2062. if (wait)
  2063. jbd2_log_wait_commit(EXT4_SB(sb)->s_journal, target);
  2064. }
  2065. return 0;
  2066. }
  2067. /*
  2068. * LVM calls this function before a (read-only) snapshot is created. This
  2069. * gives us a chance to flush the journal completely and mark the fs clean.
  2070. */
  2071. static void ext4_write_super_lockfs(struct super_block *sb)
  2072. {
  2073. sb->s_dirt = 0;
  2074. if (!(sb->s_flags & MS_RDONLY)) {
  2075. journal_t *journal = EXT4_SB(sb)->s_journal;
  2076. /* Now we set up the journal barrier. */
  2077. jbd2_journal_lock_updates(journal);
  2078. jbd2_journal_flush(journal);
  2079. /* Journal blocked and flushed, clear needs_recovery flag. */
  2080. EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
  2081. ext4_commit_super(sb, EXT4_SB(sb)->s_es, 1);
  2082. }
  2083. }
  2084. /*
  2085. * Called by LVM after the snapshot is done. We need to reset the RECOVER
  2086. * flag here, even though the filesystem is not technically dirty yet.
  2087. */
  2088. static void ext4_unlockfs(struct super_block *sb)
  2089. {
  2090. if (!(sb->s_flags & MS_RDONLY)) {
  2091. lock_super(sb);
  2092. /* Reser the needs_recovery flag before the fs is unlocked. */
  2093. EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
  2094. ext4_commit_super(sb, EXT4_SB(sb)->s_es, 1);
  2095. unlock_super(sb);
  2096. jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
  2097. }
  2098. }
  2099. static int ext4_remount (struct super_block * sb, int * flags, char * data)
  2100. {
  2101. struct ext4_super_block * es;
  2102. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2103. ext4_fsblk_t n_blocks_count = 0;
  2104. unsigned long old_sb_flags;
  2105. struct ext4_mount_options old_opts;
  2106. int err;
  2107. #ifdef CONFIG_QUOTA
  2108. int i;
  2109. #endif
  2110. /* Store the original options */
  2111. old_sb_flags = sb->s_flags;
  2112. old_opts.s_mount_opt = sbi->s_mount_opt;
  2113. old_opts.s_resuid = sbi->s_resuid;
  2114. old_opts.s_resgid = sbi->s_resgid;
  2115. old_opts.s_commit_interval = sbi->s_commit_interval;
  2116. #ifdef CONFIG_QUOTA
  2117. old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
  2118. for (i = 0; i < MAXQUOTAS; i++)
  2119. old_opts.s_qf_names[i] = sbi->s_qf_names[i];
  2120. #endif
  2121. /*
  2122. * Allow the "check" option to be passed as a remount option.
  2123. */
  2124. if (!parse_options(data, sb, NULL, NULL, &n_blocks_count, 1)) {
  2125. err = -EINVAL;
  2126. goto restore_opts;
  2127. }
  2128. if (sbi->s_mount_opt & EXT4_MOUNT_ABORT)
  2129. ext4_abort(sb, __FUNCTION__, "Abort forced by user");
  2130. sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
  2131. ((sbi->s_mount_opt & EXT4_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
  2132. es = sbi->s_es;
  2133. ext4_init_journal_params(sb, sbi->s_journal);
  2134. if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY) ||
  2135. n_blocks_count > ext4_blocks_count(es)) {
  2136. if (sbi->s_mount_opt & EXT4_MOUNT_ABORT) {
  2137. err = -EROFS;
  2138. goto restore_opts;
  2139. }
  2140. if (*flags & MS_RDONLY) {
  2141. /*
  2142. * First of all, the unconditional stuff we have to do
  2143. * to disable replay of the journal when we next remount
  2144. */
  2145. sb->s_flags |= MS_RDONLY;
  2146. /*
  2147. * OK, test if we are remounting a valid rw partition
  2148. * readonly, and if so set the rdonly flag and then
  2149. * mark the partition as valid again.
  2150. */
  2151. if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
  2152. (sbi->s_mount_state & EXT4_VALID_FS))
  2153. es->s_state = cpu_to_le16(sbi->s_mount_state);
  2154. ext4_mark_recovery_complete(sb, es);
  2155. } else {
  2156. __le32 ret;
  2157. if ((ret = EXT4_HAS_RO_COMPAT_FEATURE(sb,
  2158. ~EXT4_FEATURE_RO_COMPAT_SUPP))) {
  2159. printk(KERN_WARNING "EXT4-fs: %s: couldn't "
  2160. "remount RDWR because of unsupported "
  2161. "optional features (%x).\n",
  2162. sb->s_id, le32_to_cpu(ret));
  2163. err = -EROFS;
  2164. goto restore_opts;
  2165. }
  2166. /*
  2167. * Mounting a RDONLY partition read-write, so reread
  2168. * and store the current valid flag. (It may have
  2169. * been changed by e2fsck since we originally mounted
  2170. * the partition.)
  2171. */
  2172. ext4_clear_journal_err(sb, es);
  2173. sbi->s_mount_state = le16_to_cpu(es->s_state);
  2174. if ((err = ext4_group_extend(sb, es, n_blocks_count)))
  2175. goto restore_opts;
  2176. if (!ext4_setup_super (sb, es, 0))
  2177. sb->s_flags &= ~MS_RDONLY;
  2178. }
  2179. }
  2180. #ifdef CONFIG_QUOTA
  2181. /* Release old quota file names */
  2182. for (i = 0; i < MAXQUOTAS; i++)
  2183. if (old_opts.s_qf_names[i] &&
  2184. old_opts.s_qf_names[i] != sbi->s_qf_names[i])
  2185. kfree(old_opts.s_qf_names[i]);
  2186. #endif
  2187. return 0;
  2188. restore_opts:
  2189. sb->s_flags = old_sb_flags;
  2190. sbi->s_mount_opt = old_opts.s_mount_opt;
  2191. sbi->s_resuid = old_opts.s_resuid;
  2192. sbi->s_resgid = old_opts.s_resgid;
  2193. sbi->s_commit_interval = old_opts.s_commit_interval;
  2194. #ifdef CONFIG_QUOTA
  2195. sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
  2196. for (i = 0; i < MAXQUOTAS; i++) {
  2197. if (sbi->s_qf_names[i] &&
  2198. old_opts.s_qf_names[i] != sbi->s_qf_names[i])
  2199. kfree(sbi->s_qf_names[i]);
  2200. sbi->s_qf_names[i] = old_opts.s_qf_names[i];
  2201. }
  2202. #endif
  2203. return err;
  2204. }
  2205. static int ext4_statfs (struct dentry * dentry, struct kstatfs * buf)
  2206. {
  2207. struct super_block *sb = dentry->d_sb;
  2208. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2209. struct ext4_super_block *es = sbi->s_es;
  2210. ext4_fsblk_t overhead;
  2211. int i;
  2212. if (test_opt (sb, MINIX_DF))
  2213. overhead = 0;
  2214. else {
  2215. unsigned long ngroups;
  2216. ngroups = EXT4_SB(sb)->s_groups_count;
  2217. smp_rmb();
  2218. /*
  2219. * Compute the overhead (FS structures)
  2220. */
  2221. /*
  2222. * All of the blocks before first_data_block are
  2223. * overhead
  2224. */
  2225. overhead = le32_to_cpu(es->s_first_data_block);
  2226. /*
  2227. * Add the overhead attributed to the superblock and
  2228. * block group descriptors. If the sparse superblocks
  2229. * feature is turned on, then not all groups have this.
  2230. */
  2231. for (i = 0; i < ngroups; i++) {
  2232. overhead += ext4_bg_has_super(sb, i) +
  2233. ext4_bg_num_gdb(sb, i);
  2234. cond_resched();
  2235. }
  2236. /*
  2237. * Every block group has an inode bitmap, a block
  2238. * bitmap, and an inode table.
  2239. */
  2240. overhead += (ngroups * (2 + EXT4_SB(sb)->s_itb_per_group));
  2241. }
  2242. buf->f_type = EXT4_SUPER_MAGIC;
  2243. buf->f_bsize = sb->s_blocksize;
  2244. buf->f_blocks = ext4_blocks_count(es) - overhead;
  2245. buf->f_bfree = percpu_counter_sum(&sbi->s_freeblocks_counter);
  2246. buf->f_bavail = buf->f_bfree - ext4_r_blocks_count(es);
  2247. if (buf->f_bfree < ext4_r_blocks_count(es))
  2248. buf->f_bavail = 0;
  2249. buf->f_files = le32_to_cpu(es->s_inodes_count);
  2250. buf->f_ffree = percpu_counter_sum(&sbi->s_freeinodes_counter);
  2251. buf->f_namelen = EXT4_NAME_LEN;
  2252. return 0;
  2253. }
  2254. /* Helper function for writing quotas on sync - we need to start transaction before quota file
  2255. * is locked for write. Otherwise the are possible deadlocks:
  2256. * Process 1 Process 2
  2257. * ext4_create() quota_sync()
  2258. * jbd2_journal_start() write_dquot()
  2259. * DQUOT_INIT() down(dqio_mutex)
  2260. * down(dqio_mutex) jbd2_journal_start()
  2261. *
  2262. */
  2263. #ifdef CONFIG_QUOTA
  2264. static inline struct inode *dquot_to_inode(struct dquot *dquot)
  2265. {
  2266. return sb_dqopt(dquot->dq_sb)->files[dquot->dq_type];
  2267. }
  2268. static int ext4_dquot_initialize(struct inode *inode, int type)
  2269. {
  2270. handle_t *handle;
  2271. int ret, err;
  2272. /* We may create quota structure so we need to reserve enough blocks */
  2273. handle = ext4_journal_start(inode, 2*EXT4_QUOTA_INIT_BLOCKS(inode->i_sb));
  2274. if (IS_ERR(handle))
  2275. return PTR_ERR(handle);
  2276. ret = dquot_initialize(inode, type);
  2277. err = ext4_journal_stop(handle);
  2278. if (!ret)
  2279. ret = err;
  2280. return ret;
  2281. }
  2282. static int ext4_dquot_drop(struct inode *inode)
  2283. {
  2284. handle_t *handle;
  2285. int ret, err;
  2286. /* We may delete quota structure so we need to reserve enough blocks */
  2287. handle = ext4_journal_start(inode, 2*EXT4_QUOTA_DEL_BLOCKS(inode->i_sb));
  2288. if (IS_ERR(handle))
  2289. return PTR_ERR(handle);
  2290. ret = dquot_drop(inode);
  2291. err = ext4_journal_stop(handle);
  2292. if (!ret)
  2293. ret = err;
  2294. return ret;
  2295. }
  2296. static int ext4_write_dquot(struct dquot *dquot)
  2297. {
  2298. int ret, err;
  2299. handle_t *handle;
  2300. struct inode *inode;
  2301. inode = dquot_to_inode(dquot);
  2302. handle = ext4_journal_start(inode,
  2303. EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
  2304. if (IS_ERR(handle))
  2305. return PTR_ERR(handle);
  2306. ret = dquot_commit(dquot);
  2307. err = ext4_journal_stop(handle);
  2308. if (!ret)
  2309. ret = err;
  2310. return ret;
  2311. }
  2312. static int ext4_acquire_dquot(struct dquot *dquot)
  2313. {
  2314. int ret, err;
  2315. handle_t *handle;
  2316. handle = ext4_journal_start(dquot_to_inode(dquot),
  2317. EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
  2318. if (IS_ERR(handle))
  2319. return PTR_ERR(handle);
  2320. ret = dquot_acquire(dquot);
  2321. err = ext4_journal_stop(handle);
  2322. if (!ret)
  2323. ret = err;
  2324. return ret;
  2325. }
  2326. static int ext4_release_dquot(struct dquot *dquot)
  2327. {
  2328. int ret, err;
  2329. handle_t *handle;
  2330. handle = ext4_journal_start(dquot_to_inode(dquot),
  2331. EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
  2332. if (IS_ERR(handle))
  2333. return PTR_ERR(handle);
  2334. ret = dquot_release(dquot);
  2335. err = ext4_journal_stop(handle);
  2336. if (!ret)
  2337. ret = err;
  2338. return ret;
  2339. }
  2340. static int ext4_mark_dquot_dirty(struct dquot *dquot)
  2341. {
  2342. /* Are we journalling quotas? */
  2343. if (EXT4_SB(dquot->dq_sb)->s_qf_names[USRQUOTA] ||
  2344. EXT4_SB(dquot->dq_sb)->s_qf_names[GRPQUOTA]) {
  2345. dquot_mark_dquot_dirty(dquot);
  2346. return ext4_write_dquot(dquot);
  2347. } else {
  2348. return dquot_mark_dquot_dirty(dquot);
  2349. }
  2350. }
  2351. static int ext4_write_info(struct super_block *sb, int type)
  2352. {
  2353. int ret, err;
  2354. handle_t *handle;
  2355. /* Data block + inode block */
  2356. handle = ext4_journal_start(sb->s_root->d_inode, 2);
  2357. if (IS_ERR(handle))
  2358. return PTR_ERR(handle);
  2359. ret = dquot_commit_info(sb, type);
  2360. err = ext4_journal_stop(handle);
  2361. if (!ret)
  2362. ret = err;
  2363. return ret;
  2364. }
  2365. /*
  2366. * Turn on quotas during mount time - we need to find
  2367. * the quota file and such...
  2368. */
  2369. static int ext4_quota_on_mount(struct super_block *sb, int type)
  2370. {
  2371. return vfs_quota_on_mount(sb, EXT4_SB(sb)->s_qf_names[type],
  2372. EXT4_SB(sb)->s_jquota_fmt, type);
  2373. }
  2374. /*
  2375. * Standard function to be called on quota_on
  2376. */
  2377. static int ext4_quota_on(struct super_block *sb, int type, int format_id,
  2378. char *path)
  2379. {
  2380. int err;
  2381. struct nameidata nd;
  2382. if (!test_opt(sb, QUOTA))
  2383. return -EINVAL;
  2384. /* Not journalling quota? */
  2385. if (!EXT4_SB(sb)->s_qf_names[USRQUOTA] &&
  2386. !EXT4_SB(sb)->s_qf_names[GRPQUOTA])
  2387. return vfs_quota_on(sb, type, format_id, path);
  2388. err = path_lookup(path, LOOKUP_FOLLOW, &nd);
  2389. if (err)
  2390. return err;
  2391. /* Quotafile not on the same filesystem? */
  2392. if (nd.mnt->mnt_sb != sb) {
  2393. path_release(&nd);
  2394. return -EXDEV;
  2395. }
  2396. /* Quotafile not of fs root? */
  2397. if (nd.dentry->d_parent->d_inode != sb->s_root->d_inode)
  2398. printk(KERN_WARNING
  2399. "EXT4-fs: Quota file not on filesystem root. "
  2400. "Journalled quota will not work.\n");
  2401. path_release(&nd);
  2402. return vfs_quota_on(sb, type, format_id, path);
  2403. }
  2404. /* Read data from quotafile - avoid pagecache and such because we cannot afford
  2405. * acquiring the locks... As quota files are never truncated and quota code
  2406. * itself serializes the operations (and noone else should touch the files)
  2407. * we don't have to be afraid of races */
  2408. static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
  2409. size_t len, loff_t off)
  2410. {
  2411. struct inode *inode = sb_dqopt(sb)->files[type];
  2412. sector_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
  2413. int err = 0;
  2414. int offset = off & (sb->s_blocksize - 1);
  2415. int tocopy;
  2416. size_t toread;
  2417. struct buffer_head *bh;
  2418. loff_t i_size = i_size_read(inode);
  2419. if (off > i_size)
  2420. return 0;
  2421. if (off+len > i_size)
  2422. len = i_size-off;
  2423. toread = len;
  2424. while (toread > 0) {
  2425. tocopy = sb->s_blocksize - offset < toread ?
  2426. sb->s_blocksize - offset : toread;
  2427. bh = ext4_bread(NULL, inode, blk, 0, &err);
  2428. if (err)
  2429. return err;
  2430. if (!bh) /* A hole? */
  2431. memset(data, 0, tocopy);
  2432. else
  2433. memcpy(data, bh->b_data+offset, tocopy);
  2434. brelse(bh);
  2435. offset = 0;
  2436. toread -= tocopy;
  2437. data += tocopy;
  2438. blk++;
  2439. }
  2440. return len;
  2441. }
  2442. /* Write to quotafile (we know the transaction is already started and has
  2443. * enough credits) */
  2444. static ssize_t ext4_quota_write(struct super_block *sb, int type,
  2445. const char *data, size_t len, loff_t off)
  2446. {
  2447. struct inode *inode = sb_dqopt(sb)->files[type];
  2448. sector_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
  2449. int err = 0;
  2450. int offset = off & (sb->s_blocksize - 1);
  2451. int tocopy;
  2452. int journal_quota = EXT4_SB(sb)->s_qf_names[type] != NULL;
  2453. size_t towrite = len;
  2454. struct buffer_head *bh;
  2455. handle_t *handle = journal_current_handle();
  2456. mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
  2457. while (towrite > 0) {
  2458. tocopy = sb->s_blocksize - offset < towrite ?
  2459. sb->s_blocksize - offset : towrite;
  2460. bh = ext4_bread(handle, inode, blk, 1, &err);
  2461. if (!bh)
  2462. goto out;
  2463. if (journal_quota) {
  2464. err = ext4_journal_get_write_access(handle, bh);
  2465. if (err) {
  2466. brelse(bh);
  2467. goto out;
  2468. }
  2469. }
  2470. lock_buffer(bh);
  2471. memcpy(bh->b_data+offset, data, tocopy);
  2472. flush_dcache_page(bh->b_page);
  2473. unlock_buffer(bh);
  2474. if (journal_quota)
  2475. err = ext4_journal_dirty_metadata(handle, bh);
  2476. else {
  2477. /* Always do at least ordered writes for quotas */
  2478. err = ext4_journal_dirty_data(handle, bh);
  2479. mark_buffer_dirty(bh);
  2480. }
  2481. brelse(bh);
  2482. if (err)
  2483. goto out;
  2484. offset = 0;
  2485. towrite -= tocopy;
  2486. data += tocopy;
  2487. blk++;
  2488. }
  2489. out:
  2490. if (len == towrite)
  2491. return err;
  2492. if (inode->i_size < off+len-towrite) {
  2493. i_size_write(inode, off+len-towrite);
  2494. EXT4_I(inode)->i_disksize = inode->i_size;
  2495. }
  2496. inode->i_version++;
  2497. inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  2498. ext4_mark_inode_dirty(handle, inode);
  2499. mutex_unlock(&inode->i_mutex);
  2500. return len - towrite;
  2501. }
  2502. #endif
  2503. static int ext4_get_sb(struct file_system_type *fs_type,
  2504. int flags, const char *dev_name, void *data, struct vfsmount *mnt)
  2505. {
  2506. return get_sb_bdev(fs_type, flags, dev_name, data, ext4_fill_super, mnt);
  2507. }
  2508. static struct file_system_type ext4dev_fs_type = {
  2509. .owner = THIS_MODULE,
  2510. .name = "ext4dev",
  2511. .get_sb = ext4_get_sb,
  2512. .kill_sb = kill_block_super,
  2513. .fs_flags = FS_REQUIRES_DEV,
  2514. };
  2515. static int __init init_ext4_fs(void)
  2516. {
  2517. int err = init_ext4_xattr();
  2518. if (err)
  2519. return err;
  2520. err = init_inodecache();
  2521. if (err)
  2522. goto out1;
  2523. err = register_filesystem(&ext4dev_fs_type);
  2524. if (err)
  2525. goto out;
  2526. return 0;
  2527. out:
  2528. destroy_inodecache();
  2529. out1:
  2530. exit_ext4_xattr();
  2531. return err;
  2532. }
  2533. static void __exit exit_ext4_fs(void)
  2534. {
  2535. unregister_filesystem(&ext4dev_fs_type);
  2536. destroy_inodecache();
  2537. exit_ext4_xattr();
  2538. }
  2539. MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
  2540. MODULE_DESCRIPTION("Fourth Extended Filesystem with extents");
  2541. MODULE_LICENSE("GPL");
  2542. module_init(init_ext4_fs)
  2543. module_exit(exit_ext4_fs)