super.c 82 KB

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