super.c 81 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937
  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. sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
  1345. if (!sbi)
  1346. return -ENOMEM;
  1347. sb->s_fs_info = sbi;
  1348. sbi->s_mount_opt = 0;
  1349. sbi->s_resuid = EXT4_DEF_RESUID;
  1350. sbi->s_resgid = EXT4_DEF_RESGID;
  1351. unlock_kernel();
  1352. blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE);
  1353. if (!blocksize) {
  1354. printk(KERN_ERR "EXT4-fs: unable to set blocksize\n");
  1355. goto out_fail;
  1356. }
  1357. /*
  1358. * The ext4 superblock will not be buffer aligned for other than 1kB
  1359. * block sizes. We need to calculate the offset from buffer start.
  1360. */
  1361. if (blocksize != EXT4_MIN_BLOCK_SIZE) {
  1362. logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
  1363. offset = do_div(logical_sb_block, blocksize);
  1364. } else {
  1365. logical_sb_block = sb_block;
  1366. }
  1367. if (!(bh = sb_bread(sb, logical_sb_block))) {
  1368. printk (KERN_ERR "EXT4-fs: unable to read superblock\n");
  1369. goto out_fail;
  1370. }
  1371. /*
  1372. * Note: s_es must be initialized as soon as possible because
  1373. * some ext4 macro-instructions depend on its value
  1374. */
  1375. es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
  1376. sbi->s_es = es;
  1377. sb->s_magic = le16_to_cpu(es->s_magic);
  1378. if (sb->s_magic != EXT4_SUPER_MAGIC)
  1379. goto cantfind_ext4;
  1380. /* Set defaults before we parse the mount options */
  1381. def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
  1382. if (def_mount_opts & EXT4_DEFM_DEBUG)
  1383. set_opt(sbi->s_mount_opt, DEBUG);
  1384. if (def_mount_opts & EXT4_DEFM_BSDGROUPS)
  1385. set_opt(sbi->s_mount_opt, GRPID);
  1386. if (def_mount_opts & EXT4_DEFM_UID16)
  1387. set_opt(sbi->s_mount_opt, NO_UID32);
  1388. #ifdef CONFIG_EXT4DEV_FS_XATTR
  1389. if (def_mount_opts & EXT4_DEFM_XATTR_USER)
  1390. set_opt(sbi->s_mount_opt, XATTR_USER);
  1391. #endif
  1392. #ifdef CONFIG_EXT4DEV_FS_POSIX_ACL
  1393. if (def_mount_opts & EXT4_DEFM_ACL)
  1394. set_opt(sbi->s_mount_opt, POSIX_ACL);
  1395. #endif
  1396. if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA)
  1397. sbi->s_mount_opt |= EXT4_MOUNT_JOURNAL_DATA;
  1398. else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED)
  1399. sbi->s_mount_opt |= EXT4_MOUNT_ORDERED_DATA;
  1400. else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK)
  1401. sbi->s_mount_opt |= EXT4_MOUNT_WRITEBACK_DATA;
  1402. if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_PANIC)
  1403. set_opt(sbi->s_mount_opt, ERRORS_PANIC);
  1404. else if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_RO)
  1405. set_opt(sbi->s_mount_opt, ERRORS_RO);
  1406. else
  1407. set_opt(sbi->s_mount_opt, ERRORS_CONT);
  1408. sbi->s_resuid = le16_to_cpu(es->s_def_resuid);
  1409. sbi->s_resgid = le16_to_cpu(es->s_def_resgid);
  1410. set_opt(sbi->s_mount_opt, RESERVATION);
  1411. /*
  1412. * turn on extents feature by default in ext4 filesystem
  1413. * User -o noextents to turn it off
  1414. */
  1415. set_opt(sbi->s_mount_opt, EXTENTS);
  1416. if (!parse_options ((char *) data, sb, &journal_inum, &journal_devnum,
  1417. NULL, 0))
  1418. goto failed_mount;
  1419. sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
  1420. ((sbi->s_mount_opt & EXT4_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
  1421. if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
  1422. (EXT4_HAS_COMPAT_FEATURE(sb, ~0U) ||
  1423. EXT4_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
  1424. EXT4_HAS_INCOMPAT_FEATURE(sb, ~0U)))
  1425. printk(KERN_WARNING
  1426. "EXT4-fs warning: feature flags set on rev 0 fs, "
  1427. "running e2fsck is recommended\n");
  1428. /*
  1429. * Check feature flags regardless of the revision level, since we
  1430. * previously didn't change the revision level when setting the flags,
  1431. * so there is a chance incompat flags are set on a rev 0 filesystem.
  1432. */
  1433. features = EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT4_FEATURE_INCOMPAT_SUPP);
  1434. if (features) {
  1435. printk(KERN_ERR "EXT4-fs: %s: couldn't mount because of "
  1436. "unsupported optional features (%x).\n",
  1437. sb->s_id, le32_to_cpu(features));
  1438. goto failed_mount;
  1439. }
  1440. features = EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT4_FEATURE_RO_COMPAT_SUPP);
  1441. if (!(sb->s_flags & MS_RDONLY) && features) {
  1442. printk(KERN_ERR "EXT4-fs: %s: couldn't mount RDWR because of "
  1443. "unsupported optional features (%x).\n",
  1444. sb->s_id, le32_to_cpu(features));
  1445. goto failed_mount;
  1446. }
  1447. blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
  1448. if (blocksize < EXT4_MIN_BLOCK_SIZE ||
  1449. blocksize > EXT4_MAX_BLOCK_SIZE) {
  1450. printk(KERN_ERR
  1451. "EXT4-fs: Unsupported filesystem blocksize %d on %s.\n",
  1452. blocksize, sb->s_id);
  1453. goto failed_mount;
  1454. }
  1455. hblock = bdev_hardsect_size(sb->s_bdev);
  1456. if (sb->s_blocksize != blocksize) {
  1457. /*
  1458. * Make sure the blocksize for the filesystem is larger
  1459. * than the hardware sectorsize for the machine.
  1460. */
  1461. if (blocksize < hblock) {
  1462. printk(KERN_ERR "EXT4-fs: blocksize %d too small for "
  1463. "device blocksize %d.\n", blocksize, hblock);
  1464. goto failed_mount;
  1465. }
  1466. brelse (bh);
  1467. sb_set_blocksize(sb, blocksize);
  1468. logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
  1469. offset = do_div(logical_sb_block, blocksize);
  1470. bh = sb_bread(sb, logical_sb_block);
  1471. if (!bh) {
  1472. printk(KERN_ERR
  1473. "EXT4-fs: Can't read superblock on 2nd try.\n");
  1474. goto failed_mount;
  1475. }
  1476. es = (struct ext4_super_block *)(((char *)bh->b_data) + offset);
  1477. sbi->s_es = es;
  1478. if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
  1479. printk (KERN_ERR
  1480. "EXT4-fs: Magic mismatch, very weird !\n");
  1481. goto failed_mount;
  1482. }
  1483. }
  1484. sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits);
  1485. if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
  1486. sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
  1487. sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
  1488. } else {
  1489. sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
  1490. sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
  1491. if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
  1492. (!is_power_of_2(sbi->s_inode_size)) ||
  1493. (sbi->s_inode_size > blocksize)) {
  1494. printk (KERN_ERR
  1495. "EXT4-fs: unsupported inode size: %d\n",
  1496. sbi->s_inode_size);
  1497. goto failed_mount;
  1498. }
  1499. if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE)
  1500. sb->s_time_gran = 1 << (EXT4_EPOCH_BITS - 2);
  1501. }
  1502. sbi->s_frag_size = EXT4_MIN_FRAG_SIZE <<
  1503. le32_to_cpu(es->s_log_frag_size);
  1504. if (blocksize != sbi->s_frag_size) {
  1505. printk(KERN_ERR
  1506. "EXT4-fs: fragsize %lu != blocksize %u (unsupported)\n",
  1507. sbi->s_frag_size, blocksize);
  1508. goto failed_mount;
  1509. }
  1510. sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
  1511. if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT)) {
  1512. if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
  1513. sbi->s_desc_size > EXT4_MAX_DESC_SIZE ||
  1514. sbi->s_desc_size & (sbi->s_desc_size - 1)) {
  1515. printk(KERN_ERR
  1516. "EXT4-fs: unsupported descriptor size %lu\n",
  1517. sbi->s_desc_size);
  1518. goto failed_mount;
  1519. }
  1520. } else
  1521. sbi->s_desc_size = EXT4_MIN_DESC_SIZE;
  1522. sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
  1523. sbi->s_frags_per_group = le32_to_cpu(es->s_frags_per_group);
  1524. sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
  1525. if (EXT4_INODE_SIZE(sb) == 0)
  1526. goto cantfind_ext4;
  1527. sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb);
  1528. if (sbi->s_inodes_per_block == 0)
  1529. goto cantfind_ext4;
  1530. sbi->s_itb_per_group = sbi->s_inodes_per_group /
  1531. sbi->s_inodes_per_block;
  1532. sbi->s_desc_per_block = blocksize / EXT4_DESC_SIZE(sb);
  1533. sbi->s_sbh = bh;
  1534. sbi->s_mount_state = le16_to_cpu(es->s_state);
  1535. sbi->s_addr_per_block_bits = log2(EXT4_ADDR_PER_BLOCK(sb));
  1536. sbi->s_desc_per_block_bits = log2(EXT4_DESC_PER_BLOCK(sb));
  1537. for (i=0; i < 4; i++)
  1538. sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
  1539. sbi->s_def_hash_version = es->s_def_hash_version;
  1540. if (sbi->s_blocks_per_group > blocksize * 8) {
  1541. printk (KERN_ERR
  1542. "EXT4-fs: #blocks per group too big: %lu\n",
  1543. sbi->s_blocks_per_group);
  1544. goto failed_mount;
  1545. }
  1546. if (sbi->s_frags_per_group > blocksize * 8) {
  1547. printk (KERN_ERR
  1548. "EXT4-fs: #fragments per group too big: %lu\n",
  1549. sbi->s_frags_per_group);
  1550. goto failed_mount;
  1551. }
  1552. if (sbi->s_inodes_per_group > blocksize * 8) {
  1553. printk (KERN_ERR
  1554. "EXT4-fs: #inodes per group too big: %lu\n",
  1555. sbi->s_inodes_per_group);
  1556. goto failed_mount;
  1557. }
  1558. if (ext4_blocks_count(es) >
  1559. (sector_t)(~0ULL) >> (sb->s_blocksize_bits - 9)) {
  1560. printk(KERN_ERR "EXT4-fs: filesystem on %s:"
  1561. " too large to mount safely\n", sb->s_id);
  1562. if (sizeof(sector_t) < 8)
  1563. printk(KERN_WARNING "EXT4-fs: CONFIG_LBD not "
  1564. "enabled\n");
  1565. goto failed_mount;
  1566. }
  1567. if (EXT4_BLOCKS_PER_GROUP(sb) == 0)
  1568. goto cantfind_ext4;
  1569. blocks_count = (ext4_blocks_count(es) -
  1570. le32_to_cpu(es->s_first_data_block) +
  1571. EXT4_BLOCKS_PER_GROUP(sb) - 1);
  1572. do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
  1573. sbi->s_groups_count = blocks_count;
  1574. db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
  1575. EXT4_DESC_PER_BLOCK(sb);
  1576. sbi->s_group_desc = kmalloc(db_count * sizeof (struct buffer_head *),
  1577. GFP_KERNEL);
  1578. if (sbi->s_group_desc == NULL) {
  1579. printk (KERN_ERR "EXT4-fs: not enough memory\n");
  1580. goto failed_mount;
  1581. }
  1582. bgl_lock_init(&sbi->s_blockgroup_lock);
  1583. for (i = 0; i < db_count; i++) {
  1584. block = descriptor_loc(sb, logical_sb_block, i);
  1585. sbi->s_group_desc[i] = sb_bread(sb, block);
  1586. if (!sbi->s_group_desc[i]) {
  1587. printk (KERN_ERR "EXT4-fs: "
  1588. "can't read group descriptor %d\n", i);
  1589. db_count = i;
  1590. goto failed_mount2;
  1591. }
  1592. }
  1593. if (!ext4_check_descriptors (sb)) {
  1594. printk(KERN_ERR "EXT4-fs: group descriptors corrupted!\n");
  1595. goto failed_mount2;
  1596. }
  1597. sbi->s_gdb_count = db_count;
  1598. get_random_bytes(&sbi->s_next_generation, sizeof(u32));
  1599. spin_lock_init(&sbi->s_next_gen_lock);
  1600. percpu_counter_init(&sbi->s_freeblocks_counter,
  1601. ext4_count_free_blocks(sb));
  1602. percpu_counter_init(&sbi->s_freeinodes_counter,
  1603. ext4_count_free_inodes(sb));
  1604. percpu_counter_init(&sbi->s_dirs_counter,
  1605. ext4_count_dirs(sb));
  1606. /* per fileystem reservation list head & lock */
  1607. spin_lock_init(&sbi->s_rsv_window_lock);
  1608. sbi->s_rsv_window_root = RB_ROOT;
  1609. /* Add a single, static dummy reservation to the start of the
  1610. * reservation window list --- it gives us a placeholder for
  1611. * append-at-start-of-list which makes the allocation logic
  1612. * _much_ simpler. */
  1613. sbi->s_rsv_window_head.rsv_start = EXT4_RESERVE_WINDOW_NOT_ALLOCATED;
  1614. sbi->s_rsv_window_head.rsv_end = EXT4_RESERVE_WINDOW_NOT_ALLOCATED;
  1615. sbi->s_rsv_window_head.rsv_alloc_hit = 0;
  1616. sbi->s_rsv_window_head.rsv_goal_size = 0;
  1617. ext4_rsv_window_add(sb, &sbi->s_rsv_window_head);
  1618. /*
  1619. * set up enough so that it can read an inode
  1620. */
  1621. sb->s_op = &ext4_sops;
  1622. sb->s_export_op = &ext4_export_ops;
  1623. sb->s_xattr = ext4_xattr_handlers;
  1624. #ifdef CONFIG_QUOTA
  1625. sb->s_qcop = &ext4_qctl_operations;
  1626. sb->dq_op = &ext4_quota_operations;
  1627. #endif
  1628. INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
  1629. sb->s_root = NULL;
  1630. needs_recovery = (es->s_last_orphan != 0 ||
  1631. EXT4_HAS_INCOMPAT_FEATURE(sb,
  1632. EXT4_FEATURE_INCOMPAT_RECOVER));
  1633. /*
  1634. * The first inode we look at is the journal inode. Don't try
  1635. * root first: it may be modified in the journal!
  1636. */
  1637. if (!test_opt(sb, NOLOAD) &&
  1638. EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
  1639. if (ext4_load_journal(sb, es, journal_devnum))
  1640. goto failed_mount3;
  1641. } else if (journal_inum) {
  1642. if (ext4_create_journal(sb, es, journal_inum))
  1643. goto failed_mount3;
  1644. } else {
  1645. if (!silent)
  1646. printk (KERN_ERR
  1647. "ext4: No journal on filesystem on %s\n",
  1648. sb->s_id);
  1649. goto failed_mount3;
  1650. }
  1651. if (ext4_blocks_count(es) > 0xffffffffULL &&
  1652. !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
  1653. JBD2_FEATURE_INCOMPAT_64BIT)) {
  1654. printk(KERN_ERR "ext4: Failed to set 64-bit journal feature\n");
  1655. goto failed_mount4;
  1656. }
  1657. /* We have now updated the journal if required, so we can
  1658. * validate the data journaling mode. */
  1659. switch (test_opt(sb, DATA_FLAGS)) {
  1660. case 0:
  1661. /* No mode set, assume a default based on the journal
  1662. * capabilities: ORDERED_DATA if the journal can
  1663. * cope, else JOURNAL_DATA
  1664. */
  1665. if (jbd2_journal_check_available_features
  1666. (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE))
  1667. set_opt(sbi->s_mount_opt, ORDERED_DATA);
  1668. else
  1669. set_opt(sbi->s_mount_opt, JOURNAL_DATA);
  1670. break;
  1671. case EXT4_MOUNT_ORDERED_DATA:
  1672. case EXT4_MOUNT_WRITEBACK_DATA:
  1673. if (!jbd2_journal_check_available_features
  1674. (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
  1675. printk(KERN_ERR "EXT4-fs: Journal does not support "
  1676. "requested data journaling mode\n");
  1677. goto failed_mount4;
  1678. }
  1679. default:
  1680. break;
  1681. }
  1682. if (test_opt(sb, NOBH)) {
  1683. if (!(test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)) {
  1684. printk(KERN_WARNING "EXT4-fs: Ignoring nobh option - "
  1685. "its supported only with writeback mode\n");
  1686. clear_opt(sbi->s_mount_opt, NOBH);
  1687. }
  1688. }
  1689. /*
  1690. * The jbd2_journal_load will have done any necessary log recovery,
  1691. * so we can safely mount the rest of the filesystem now.
  1692. */
  1693. root = iget(sb, EXT4_ROOT_INO);
  1694. sb->s_root = d_alloc_root(root);
  1695. if (!sb->s_root) {
  1696. printk(KERN_ERR "EXT4-fs: get root inode failed\n");
  1697. iput(root);
  1698. goto failed_mount4;
  1699. }
  1700. if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
  1701. dput(sb->s_root);
  1702. sb->s_root = NULL;
  1703. printk(KERN_ERR "EXT4-fs: corrupt root inode, run e2fsck\n");
  1704. goto failed_mount4;
  1705. }
  1706. ext4_setup_super (sb, es, sb->s_flags & MS_RDONLY);
  1707. /* determine the minimum size of new large inodes, if present */
  1708. if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
  1709. sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
  1710. EXT4_GOOD_OLD_INODE_SIZE;
  1711. if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
  1712. EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)) {
  1713. if (sbi->s_want_extra_isize <
  1714. le16_to_cpu(es->s_want_extra_isize))
  1715. sbi->s_want_extra_isize =
  1716. le16_to_cpu(es->s_want_extra_isize);
  1717. if (sbi->s_want_extra_isize <
  1718. le16_to_cpu(es->s_min_extra_isize))
  1719. sbi->s_want_extra_isize =
  1720. le16_to_cpu(es->s_min_extra_isize);
  1721. }
  1722. }
  1723. /* Check if enough inode space is available */
  1724. if (EXT4_GOOD_OLD_INODE_SIZE + sbi->s_want_extra_isize >
  1725. sbi->s_inode_size) {
  1726. sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
  1727. EXT4_GOOD_OLD_INODE_SIZE;
  1728. printk(KERN_INFO "EXT4-fs: required extra inode space not"
  1729. "available.\n");
  1730. }
  1731. /*
  1732. * akpm: core read_super() calls in here with the superblock locked.
  1733. * That deadlocks, because orphan cleanup needs to lock the superblock
  1734. * in numerous places. Here we just pop the lock - it's relatively
  1735. * harmless, because we are now ready to accept write_super() requests,
  1736. * and aviro says that's the only reason for hanging onto the
  1737. * superblock lock.
  1738. */
  1739. EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
  1740. ext4_orphan_cleanup(sb, es);
  1741. EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
  1742. if (needs_recovery)
  1743. printk (KERN_INFO "EXT4-fs: recovery complete.\n");
  1744. ext4_mark_recovery_complete(sb, es);
  1745. printk (KERN_INFO "EXT4-fs: mounted filesystem with %s data mode.\n",
  1746. test_opt(sb,DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA ? "journal":
  1747. test_opt(sb,DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA ? "ordered":
  1748. "writeback");
  1749. ext4_ext_init(sb);
  1750. lock_kernel();
  1751. return 0;
  1752. cantfind_ext4:
  1753. if (!silent)
  1754. printk(KERN_ERR "VFS: Can't find ext4 filesystem on dev %s.\n",
  1755. sb->s_id);
  1756. goto failed_mount;
  1757. failed_mount4:
  1758. jbd2_journal_destroy(sbi->s_journal);
  1759. failed_mount3:
  1760. percpu_counter_destroy(&sbi->s_freeblocks_counter);
  1761. percpu_counter_destroy(&sbi->s_freeinodes_counter);
  1762. percpu_counter_destroy(&sbi->s_dirs_counter);
  1763. failed_mount2:
  1764. for (i = 0; i < db_count; i++)
  1765. brelse(sbi->s_group_desc[i]);
  1766. kfree(sbi->s_group_desc);
  1767. failed_mount:
  1768. #ifdef CONFIG_QUOTA
  1769. for (i = 0; i < MAXQUOTAS; i++)
  1770. kfree(sbi->s_qf_names[i]);
  1771. #endif
  1772. ext4_blkdev_remove(sbi);
  1773. brelse(bh);
  1774. out_fail:
  1775. sb->s_fs_info = NULL;
  1776. kfree(sbi);
  1777. lock_kernel();
  1778. return -EINVAL;
  1779. }
  1780. /*
  1781. * Setup any per-fs journal parameters now. We'll do this both on
  1782. * initial mount, once the journal has been initialised but before we've
  1783. * done any recovery; and again on any subsequent remount.
  1784. */
  1785. static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
  1786. {
  1787. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1788. if (sbi->s_commit_interval)
  1789. journal->j_commit_interval = sbi->s_commit_interval;
  1790. /* We could also set up an ext4-specific default for the commit
  1791. * interval here, but for now we'll just fall back to the jbd
  1792. * default. */
  1793. spin_lock(&journal->j_state_lock);
  1794. if (test_opt(sb, BARRIER))
  1795. journal->j_flags |= JBD2_BARRIER;
  1796. else
  1797. journal->j_flags &= ~JBD2_BARRIER;
  1798. spin_unlock(&journal->j_state_lock);
  1799. }
  1800. static journal_t *ext4_get_journal(struct super_block *sb,
  1801. unsigned int journal_inum)
  1802. {
  1803. struct inode *journal_inode;
  1804. journal_t *journal;
  1805. /* First, test for the existence of a valid inode on disk. Bad
  1806. * things happen if we iget() an unused inode, as the subsequent
  1807. * iput() will try to delete it. */
  1808. journal_inode = iget(sb, journal_inum);
  1809. if (!journal_inode) {
  1810. printk(KERN_ERR "EXT4-fs: no journal found.\n");
  1811. return NULL;
  1812. }
  1813. if (!journal_inode->i_nlink) {
  1814. make_bad_inode(journal_inode);
  1815. iput(journal_inode);
  1816. printk(KERN_ERR "EXT4-fs: journal inode is deleted.\n");
  1817. return NULL;
  1818. }
  1819. jbd_debug(2, "Journal inode found at %p: %Ld bytes\n",
  1820. journal_inode, journal_inode->i_size);
  1821. if (is_bad_inode(journal_inode) || !S_ISREG(journal_inode->i_mode)) {
  1822. printk(KERN_ERR "EXT4-fs: invalid journal inode.\n");
  1823. iput(journal_inode);
  1824. return NULL;
  1825. }
  1826. journal = jbd2_journal_init_inode(journal_inode);
  1827. if (!journal) {
  1828. printk(KERN_ERR "EXT4-fs: Could not load journal inode\n");
  1829. iput(journal_inode);
  1830. return NULL;
  1831. }
  1832. journal->j_private = sb;
  1833. ext4_init_journal_params(sb, journal);
  1834. return journal;
  1835. }
  1836. static journal_t *ext4_get_dev_journal(struct super_block *sb,
  1837. dev_t j_dev)
  1838. {
  1839. struct buffer_head * bh;
  1840. journal_t *journal;
  1841. ext4_fsblk_t start;
  1842. ext4_fsblk_t len;
  1843. int hblock, blocksize;
  1844. ext4_fsblk_t sb_block;
  1845. unsigned long offset;
  1846. struct ext4_super_block * es;
  1847. struct block_device *bdev;
  1848. bdev = ext4_blkdev_get(j_dev);
  1849. if (bdev == NULL)
  1850. return NULL;
  1851. if (bd_claim(bdev, sb)) {
  1852. printk(KERN_ERR
  1853. "EXT4: failed to claim external journal device.\n");
  1854. blkdev_put(bdev);
  1855. return NULL;
  1856. }
  1857. blocksize = sb->s_blocksize;
  1858. hblock = bdev_hardsect_size(bdev);
  1859. if (blocksize < hblock) {
  1860. printk(KERN_ERR
  1861. "EXT4-fs: blocksize too small for journal device.\n");
  1862. goto out_bdev;
  1863. }
  1864. sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
  1865. offset = EXT4_MIN_BLOCK_SIZE % blocksize;
  1866. set_blocksize(bdev, blocksize);
  1867. if (!(bh = __bread(bdev, sb_block, blocksize))) {
  1868. printk(KERN_ERR "EXT4-fs: couldn't read superblock of "
  1869. "external journal\n");
  1870. goto out_bdev;
  1871. }
  1872. es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
  1873. if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
  1874. !(le32_to_cpu(es->s_feature_incompat) &
  1875. EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
  1876. printk(KERN_ERR "EXT4-fs: external journal has "
  1877. "bad superblock\n");
  1878. brelse(bh);
  1879. goto out_bdev;
  1880. }
  1881. if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
  1882. printk(KERN_ERR "EXT4-fs: journal UUID does not match\n");
  1883. brelse(bh);
  1884. goto out_bdev;
  1885. }
  1886. len = ext4_blocks_count(es);
  1887. start = sb_block + 1;
  1888. brelse(bh); /* we're done with the superblock */
  1889. journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
  1890. start, len, blocksize);
  1891. if (!journal) {
  1892. printk(KERN_ERR "EXT4-fs: failed to create device journal\n");
  1893. goto out_bdev;
  1894. }
  1895. journal->j_private = sb;
  1896. ll_rw_block(READ, 1, &journal->j_sb_buffer);
  1897. wait_on_buffer(journal->j_sb_buffer);
  1898. if (!buffer_uptodate(journal->j_sb_buffer)) {
  1899. printk(KERN_ERR "EXT4-fs: I/O error on journal device\n");
  1900. goto out_journal;
  1901. }
  1902. if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
  1903. printk(KERN_ERR "EXT4-fs: External journal has more than one "
  1904. "user (unsupported) - %d\n",
  1905. be32_to_cpu(journal->j_superblock->s_nr_users));
  1906. goto out_journal;
  1907. }
  1908. EXT4_SB(sb)->journal_bdev = bdev;
  1909. ext4_init_journal_params(sb, journal);
  1910. return journal;
  1911. out_journal:
  1912. jbd2_journal_destroy(journal);
  1913. out_bdev:
  1914. ext4_blkdev_put(bdev);
  1915. return NULL;
  1916. }
  1917. static int ext4_load_journal(struct super_block *sb,
  1918. struct ext4_super_block *es,
  1919. unsigned long journal_devnum)
  1920. {
  1921. journal_t *journal;
  1922. unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
  1923. dev_t journal_dev;
  1924. int err = 0;
  1925. int really_read_only;
  1926. if (journal_devnum &&
  1927. journal_devnum != le32_to_cpu(es->s_journal_dev)) {
  1928. printk(KERN_INFO "EXT4-fs: external journal device major/minor "
  1929. "numbers have changed\n");
  1930. journal_dev = new_decode_dev(journal_devnum);
  1931. } else
  1932. journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
  1933. really_read_only = bdev_read_only(sb->s_bdev);
  1934. /*
  1935. * Are we loading a blank journal or performing recovery after a
  1936. * crash? For recovery, we need to check in advance whether we
  1937. * can get read-write access to the device.
  1938. */
  1939. if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
  1940. if (sb->s_flags & MS_RDONLY) {
  1941. printk(KERN_INFO "EXT4-fs: INFO: recovery "
  1942. "required on readonly filesystem.\n");
  1943. if (really_read_only) {
  1944. printk(KERN_ERR "EXT4-fs: write access "
  1945. "unavailable, cannot proceed.\n");
  1946. return -EROFS;
  1947. }
  1948. printk (KERN_INFO "EXT4-fs: write access will "
  1949. "be enabled during recovery.\n");
  1950. }
  1951. }
  1952. if (journal_inum && journal_dev) {
  1953. printk(KERN_ERR "EXT4-fs: filesystem has both journal "
  1954. "and inode journals!\n");
  1955. return -EINVAL;
  1956. }
  1957. if (journal_inum) {
  1958. if (!(journal = ext4_get_journal(sb, journal_inum)))
  1959. return -EINVAL;
  1960. } else {
  1961. if (!(journal = ext4_get_dev_journal(sb, journal_dev)))
  1962. return -EINVAL;
  1963. }
  1964. if (!really_read_only && test_opt(sb, UPDATE_JOURNAL)) {
  1965. err = jbd2_journal_update_format(journal);
  1966. if (err) {
  1967. printk(KERN_ERR "EXT4-fs: error updating journal.\n");
  1968. jbd2_journal_destroy(journal);
  1969. return err;
  1970. }
  1971. }
  1972. if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER))
  1973. err = jbd2_journal_wipe(journal, !really_read_only);
  1974. if (!err)
  1975. err = jbd2_journal_load(journal);
  1976. if (err) {
  1977. printk(KERN_ERR "EXT4-fs: error loading journal.\n");
  1978. jbd2_journal_destroy(journal);
  1979. return err;
  1980. }
  1981. EXT4_SB(sb)->s_journal = journal;
  1982. ext4_clear_journal_err(sb, es);
  1983. if (journal_devnum &&
  1984. journal_devnum != le32_to_cpu(es->s_journal_dev)) {
  1985. es->s_journal_dev = cpu_to_le32(journal_devnum);
  1986. sb->s_dirt = 1;
  1987. /* Make sure we flush the recovery flag to disk. */
  1988. ext4_commit_super(sb, es, 1);
  1989. }
  1990. return 0;
  1991. }
  1992. static int ext4_create_journal(struct super_block * sb,
  1993. struct ext4_super_block * es,
  1994. unsigned int journal_inum)
  1995. {
  1996. journal_t *journal;
  1997. int err;
  1998. if (sb->s_flags & MS_RDONLY) {
  1999. printk(KERN_ERR "EXT4-fs: readonly filesystem when trying to "
  2000. "create journal.\n");
  2001. return -EROFS;
  2002. }
  2003. journal = ext4_get_journal(sb, journal_inum);
  2004. if (!journal)
  2005. return -EINVAL;
  2006. printk(KERN_INFO "EXT4-fs: creating new journal on inode %u\n",
  2007. journal_inum);
  2008. err = jbd2_journal_create(journal);
  2009. if (err) {
  2010. printk(KERN_ERR "EXT4-fs: error creating journal.\n");
  2011. jbd2_journal_destroy(journal);
  2012. return -EIO;
  2013. }
  2014. EXT4_SB(sb)->s_journal = journal;
  2015. ext4_update_dynamic_rev(sb);
  2016. EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
  2017. EXT4_SET_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL);
  2018. es->s_journal_inum = cpu_to_le32(journal_inum);
  2019. sb->s_dirt = 1;
  2020. /* Make sure we flush the recovery flag to disk. */
  2021. ext4_commit_super(sb, es, 1);
  2022. return 0;
  2023. }
  2024. static void ext4_commit_super (struct super_block * sb,
  2025. struct ext4_super_block * es,
  2026. int sync)
  2027. {
  2028. struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
  2029. if (!sbh)
  2030. return;
  2031. es->s_wtime = cpu_to_le32(get_seconds());
  2032. ext4_free_blocks_count_set(es, ext4_count_free_blocks(sb));
  2033. es->s_free_inodes_count = cpu_to_le32(ext4_count_free_inodes(sb));
  2034. BUFFER_TRACE(sbh, "marking dirty");
  2035. mark_buffer_dirty(sbh);
  2036. if (sync)
  2037. sync_dirty_buffer(sbh);
  2038. }
  2039. /*
  2040. * Have we just finished recovery? If so, and if we are mounting (or
  2041. * remounting) the filesystem readonly, then we will end up with a
  2042. * consistent fs on disk. Record that fact.
  2043. */
  2044. static void ext4_mark_recovery_complete(struct super_block * sb,
  2045. struct ext4_super_block * es)
  2046. {
  2047. journal_t *journal = EXT4_SB(sb)->s_journal;
  2048. jbd2_journal_lock_updates(journal);
  2049. jbd2_journal_flush(journal);
  2050. lock_super(sb);
  2051. if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER) &&
  2052. sb->s_flags & MS_RDONLY) {
  2053. EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
  2054. sb->s_dirt = 0;
  2055. ext4_commit_super(sb, es, 1);
  2056. }
  2057. unlock_super(sb);
  2058. jbd2_journal_unlock_updates(journal);
  2059. }
  2060. /*
  2061. * If we are mounting (or read-write remounting) a filesystem whose journal
  2062. * has recorded an error from a previous lifetime, move that error to the
  2063. * main filesystem now.
  2064. */
  2065. static void ext4_clear_journal_err(struct super_block * sb,
  2066. struct ext4_super_block * es)
  2067. {
  2068. journal_t *journal;
  2069. int j_errno;
  2070. const char *errstr;
  2071. journal = EXT4_SB(sb)->s_journal;
  2072. /*
  2073. * Now check for any error status which may have been recorded in the
  2074. * journal by a prior ext4_error() or ext4_abort()
  2075. */
  2076. j_errno = jbd2_journal_errno(journal);
  2077. if (j_errno) {
  2078. char nbuf[16];
  2079. errstr = ext4_decode_error(sb, j_errno, nbuf);
  2080. ext4_warning(sb, __FUNCTION__, "Filesystem error recorded "
  2081. "from previous mount: %s", errstr);
  2082. ext4_warning(sb, __FUNCTION__, "Marking fs in need of "
  2083. "filesystem check.");
  2084. EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
  2085. es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
  2086. ext4_commit_super (sb, es, 1);
  2087. jbd2_journal_clear_err(journal);
  2088. }
  2089. }
  2090. /*
  2091. * Force the running and committing transactions to commit,
  2092. * and wait on the commit.
  2093. */
  2094. int ext4_force_commit(struct super_block *sb)
  2095. {
  2096. journal_t *journal;
  2097. int ret;
  2098. if (sb->s_flags & MS_RDONLY)
  2099. return 0;
  2100. journal = EXT4_SB(sb)->s_journal;
  2101. sb->s_dirt = 0;
  2102. ret = ext4_journal_force_commit(journal);
  2103. return ret;
  2104. }
  2105. /*
  2106. * Ext4 always journals updates to the superblock itself, so we don't
  2107. * have to propagate any other updates to the superblock on disk at this
  2108. * point. Just start an async writeback to get the buffers on their way
  2109. * to the disk.
  2110. *
  2111. * This implicitly triggers the writebehind on sync().
  2112. */
  2113. static void ext4_write_super (struct super_block * sb)
  2114. {
  2115. if (mutex_trylock(&sb->s_lock) != 0)
  2116. BUG();
  2117. sb->s_dirt = 0;
  2118. }
  2119. static int ext4_sync_fs(struct super_block *sb, int wait)
  2120. {
  2121. tid_t target;
  2122. sb->s_dirt = 0;
  2123. if (jbd2_journal_start_commit(EXT4_SB(sb)->s_journal, &target)) {
  2124. if (wait)
  2125. jbd2_log_wait_commit(EXT4_SB(sb)->s_journal, target);
  2126. }
  2127. return 0;
  2128. }
  2129. /*
  2130. * LVM calls this function before a (read-only) snapshot is created. This
  2131. * gives us a chance to flush the journal completely and mark the fs clean.
  2132. */
  2133. static void ext4_write_super_lockfs(struct super_block *sb)
  2134. {
  2135. sb->s_dirt = 0;
  2136. if (!(sb->s_flags & MS_RDONLY)) {
  2137. journal_t *journal = EXT4_SB(sb)->s_journal;
  2138. /* Now we set up the journal barrier. */
  2139. jbd2_journal_lock_updates(journal);
  2140. jbd2_journal_flush(journal);
  2141. /* Journal blocked and flushed, clear needs_recovery flag. */
  2142. EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
  2143. ext4_commit_super(sb, EXT4_SB(sb)->s_es, 1);
  2144. }
  2145. }
  2146. /*
  2147. * Called by LVM after the snapshot is done. We need to reset the RECOVER
  2148. * flag here, even though the filesystem is not technically dirty yet.
  2149. */
  2150. static void ext4_unlockfs(struct super_block *sb)
  2151. {
  2152. if (!(sb->s_flags & MS_RDONLY)) {
  2153. lock_super(sb);
  2154. /* Reser the needs_recovery flag before the fs is unlocked. */
  2155. EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
  2156. ext4_commit_super(sb, EXT4_SB(sb)->s_es, 1);
  2157. unlock_super(sb);
  2158. jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
  2159. }
  2160. }
  2161. static int ext4_remount (struct super_block * sb, int * flags, char * data)
  2162. {
  2163. struct ext4_super_block * es;
  2164. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2165. ext4_fsblk_t n_blocks_count = 0;
  2166. unsigned long old_sb_flags;
  2167. struct ext4_mount_options old_opts;
  2168. int err;
  2169. #ifdef CONFIG_QUOTA
  2170. int i;
  2171. #endif
  2172. /* Store the original options */
  2173. old_sb_flags = sb->s_flags;
  2174. old_opts.s_mount_opt = sbi->s_mount_opt;
  2175. old_opts.s_resuid = sbi->s_resuid;
  2176. old_opts.s_resgid = sbi->s_resgid;
  2177. old_opts.s_commit_interval = sbi->s_commit_interval;
  2178. #ifdef CONFIG_QUOTA
  2179. old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
  2180. for (i = 0; i < MAXQUOTAS; i++)
  2181. old_opts.s_qf_names[i] = sbi->s_qf_names[i];
  2182. #endif
  2183. /*
  2184. * Allow the "check" option to be passed as a remount option.
  2185. */
  2186. if (!parse_options(data, sb, NULL, NULL, &n_blocks_count, 1)) {
  2187. err = -EINVAL;
  2188. goto restore_opts;
  2189. }
  2190. if (sbi->s_mount_opt & EXT4_MOUNT_ABORT)
  2191. ext4_abort(sb, __FUNCTION__, "Abort forced by user");
  2192. sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
  2193. ((sbi->s_mount_opt & EXT4_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
  2194. es = sbi->s_es;
  2195. ext4_init_journal_params(sb, sbi->s_journal);
  2196. if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY) ||
  2197. n_blocks_count > ext4_blocks_count(es)) {
  2198. if (sbi->s_mount_opt & EXT4_MOUNT_ABORT) {
  2199. err = -EROFS;
  2200. goto restore_opts;
  2201. }
  2202. if (*flags & MS_RDONLY) {
  2203. /*
  2204. * First of all, the unconditional stuff we have to do
  2205. * to disable replay of the journal when we next remount
  2206. */
  2207. sb->s_flags |= MS_RDONLY;
  2208. /*
  2209. * OK, test if we are remounting a valid rw partition
  2210. * readonly, and if so set the rdonly flag and then
  2211. * mark the partition as valid again.
  2212. */
  2213. if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
  2214. (sbi->s_mount_state & EXT4_VALID_FS))
  2215. es->s_state = cpu_to_le16(sbi->s_mount_state);
  2216. /*
  2217. * We have to unlock super so that we can wait for
  2218. * transactions.
  2219. */
  2220. unlock_super(sb);
  2221. ext4_mark_recovery_complete(sb, es);
  2222. lock_super(sb);
  2223. } else {
  2224. __le32 ret;
  2225. if ((ret = EXT4_HAS_RO_COMPAT_FEATURE(sb,
  2226. ~EXT4_FEATURE_RO_COMPAT_SUPP))) {
  2227. printk(KERN_WARNING "EXT4-fs: %s: couldn't "
  2228. "remount RDWR because of unsupported "
  2229. "optional features (%x).\n",
  2230. sb->s_id, le32_to_cpu(ret));
  2231. err = -EROFS;
  2232. goto restore_opts;
  2233. }
  2234. /*
  2235. * If we have an unprocessed orphan list hanging
  2236. * around from a previously readonly bdev mount,
  2237. * require a full umount/remount for now.
  2238. */
  2239. if (es->s_last_orphan) {
  2240. printk(KERN_WARNING "EXT4-fs: %s: couldn't "
  2241. "remount RDWR because of unprocessed "
  2242. "orphan inode list. Please "
  2243. "umount/remount instead.\n",
  2244. sb->s_id);
  2245. err = -EINVAL;
  2246. goto restore_opts;
  2247. }
  2248. /*
  2249. * Mounting a RDONLY partition read-write, so reread
  2250. * and store the current valid flag. (It may have
  2251. * been changed by e2fsck since we originally mounted
  2252. * the partition.)
  2253. */
  2254. ext4_clear_journal_err(sb, es);
  2255. sbi->s_mount_state = le16_to_cpu(es->s_state);
  2256. if ((err = ext4_group_extend(sb, es, n_blocks_count)))
  2257. goto restore_opts;
  2258. if (!ext4_setup_super (sb, es, 0))
  2259. sb->s_flags &= ~MS_RDONLY;
  2260. }
  2261. }
  2262. #ifdef CONFIG_QUOTA
  2263. /* Release old quota file names */
  2264. for (i = 0; i < MAXQUOTAS; i++)
  2265. if (old_opts.s_qf_names[i] &&
  2266. old_opts.s_qf_names[i] != sbi->s_qf_names[i])
  2267. kfree(old_opts.s_qf_names[i]);
  2268. #endif
  2269. return 0;
  2270. restore_opts:
  2271. sb->s_flags = old_sb_flags;
  2272. sbi->s_mount_opt = old_opts.s_mount_opt;
  2273. sbi->s_resuid = old_opts.s_resuid;
  2274. sbi->s_resgid = old_opts.s_resgid;
  2275. sbi->s_commit_interval = old_opts.s_commit_interval;
  2276. #ifdef CONFIG_QUOTA
  2277. sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
  2278. for (i = 0; i < MAXQUOTAS; i++) {
  2279. if (sbi->s_qf_names[i] &&
  2280. old_opts.s_qf_names[i] != sbi->s_qf_names[i])
  2281. kfree(sbi->s_qf_names[i]);
  2282. sbi->s_qf_names[i] = old_opts.s_qf_names[i];
  2283. }
  2284. #endif
  2285. return err;
  2286. }
  2287. static int ext4_statfs (struct dentry * dentry, struct kstatfs * buf)
  2288. {
  2289. struct super_block *sb = dentry->d_sb;
  2290. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2291. struct ext4_super_block *es = sbi->s_es;
  2292. u64 fsid;
  2293. if (test_opt(sb, MINIX_DF)) {
  2294. sbi->s_overhead_last = 0;
  2295. } else if (sbi->s_blocks_last != le32_to_cpu(es->s_blocks_count)) {
  2296. unsigned long ngroups = sbi->s_groups_count, i;
  2297. ext4_fsblk_t overhead = 0;
  2298. smp_rmb();
  2299. /*
  2300. * Compute the overhead (FS structures). This is constant
  2301. * for a given filesystem unless the number of block groups
  2302. * changes so we cache the previous value until it does.
  2303. */
  2304. /*
  2305. * All of the blocks before first_data_block are
  2306. * overhead
  2307. */
  2308. overhead = le32_to_cpu(es->s_first_data_block);
  2309. /*
  2310. * Add the overhead attributed to the superblock and
  2311. * block group descriptors. If the sparse superblocks
  2312. * feature is turned on, then not all groups have this.
  2313. */
  2314. for (i = 0; i < ngroups; i++) {
  2315. overhead += ext4_bg_has_super(sb, i) +
  2316. ext4_bg_num_gdb(sb, i);
  2317. cond_resched();
  2318. }
  2319. /*
  2320. * Every block group has an inode bitmap, a block
  2321. * bitmap, and an inode table.
  2322. */
  2323. overhead += ngroups * (2 + sbi->s_itb_per_group);
  2324. sbi->s_overhead_last = overhead;
  2325. smp_wmb();
  2326. sbi->s_blocks_last = le32_to_cpu(es->s_blocks_count);
  2327. }
  2328. buf->f_type = EXT4_SUPER_MAGIC;
  2329. buf->f_bsize = sb->s_blocksize;
  2330. buf->f_blocks = ext4_blocks_count(es) - sbi->s_overhead_last;
  2331. buf->f_bfree = percpu_counter_sum(&sbi->s_freeblocks_counter);
  2332. es->s_free_blocks_count = cpu_to_le32(buf->f_bfree);
  2333. buf->f_bavail = buf->f_bfree - ext4_r_blocks_count(es);
  2334. if (buf->f_bfree < ext4_r_blocks_count(es))
  2335. buf->f_bavail = 0;
  2336. buf->f_files = le32_to_cpu(es->s_inodes_count);
  2337. buf->f_ffree = percpu_counter_sum(&sbi->s_freeinodes_counter);
  2338. es->s_free_inodes_count = cpu_to_le32(buf->f_ffree);
  2339. buf->f_namelen = EXT4_NAME_LEN;
  2340. fsid = le64_to_cpup((void *)es->s_uuid) ^
  2341. le64_to_cpup((void *)es->s_uuid + sizeof(u64));
  2342. buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
  2343. buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
  2344. return 0;
  2345. }
  2346. /* Helper function for writing quotas on sync - we need to start transaction before quota file
  2347. * is locked for write. Otherwise the are possible deadlocks:
  2348. * Process 1 Process 2
  2349. * ext4_create() quota_sync()
  2350. * jbd2_journal_start() write_dquot()
  2351. * DQUOT_INIT() down(dqio_mutex)
  2352. * down(dqio_mutex) jbd2_journal_start()
  2353. *
  2354. */
  2355. #ifdef CONFIG_QUOTA
  2356. static inline struct inode *dquot_to_inode(struct dquot *dquot)
  2357. {
  2358. return sb_dqopt(dquot->dq_sb)->files[dquot->dq_type];
  2359. }
  2360. static int ext4_dquot_initialize(struct inode *inode, int type)
  2361. {
  2362. handle_t *handle;
  2363. int ret, err;
  2364. /* We may create quota structure so we need to reserve enough blocks */
  2365. handle = ext4_journal_start(inode, 2*EXT4_QUOTA_INIT_BLOCKS(inode->i_sb));
  2366. if (IS_ERR(handle))
  2367. return PTR_ERR(handle);
  2368. ret = dquot_initialize(inode, type);
  2369. err = ext4_journal_stop(handle);
  2370. if (!ret)
  2371. ret = err;
  2372. return ret;
  2373. }
  2374. static int ext4_dquot_drop(struct inode *inode)
  2375. {
  2376. handle_t *handle;
  2377. int ret, err;
  2378. /* We may delete quota structure so we need to reserve enough blocks */
  2379. handle = ext4_journal_start(inode, 2*EXT4_QUOTA_DEL_BLOCKS(inode->i_sb));
  2380. if (IS_ERR(handle))
  2381. return PTR_ERR(handle);
  2382. ret = dquot_drop(inode);
  2383. err = ext4_journal_stop(handle);
  2384. if (!ret)
  2385. ret = err;
  2386. return ret;
  2387. }
  2388. static int ext4_write_dquot(struct dquot *dquot)
  2389. {
  2390. int ret, err;
  2391. handle_t *handle;
  2392. struct inode *inode;
  2393. inode = dquot_to_inode(dquot);
  2394. handle = ext4_journal_start(inode,
  2395. EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
  2396. if (IS_ERR(handle))
  2397. return PTR_ERR(handle);
  2398. ret = dquot_commit(dquot);
  2399. err = ext4_journal_stop(handle);
  2400. if (!ret)
  2401. ret = err;
  2402. return ret;
  2403. }
  2404. static int ext4_acquire_dquot(struct dquot *dquot)
  2405. {
  2406. int ret, err;
  2407. handle_t *handle;
  2408. handle = ext4_journal_start(dquot_to_inode(dquot),
  2409. EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
  2410. if (IS_ERR(handle))
  2411. return PTR_ERR(handle);
  2412. ret = dquot_acquire(dquot);
  2413. err = ext4_journal_stop(handle);
  2414. if (!ret)
  2415. ret = err;
  2416. return ret;
  2417. }
  2418. static int ext4_release_dquot(struct dquot *dquot)
  2419. {
  2420. int ret, err;
  2421. handle_t *handle;
  2422. handle = ext4_journal_start(dquot_to_inode(dquot),
  2423. EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
  2424. if (IS_ERR(handle)) {
  2425. /* Release dquot anyway to avoid endless cycle in dqput() */
  2426. dquot_release(dquot);
  2427. return PTR_ERR(handle);
  2428. }
  2429. ret = dquot_release(dquot);
  2430. err = ext4_journal_stop(handle);
  2431. if (!ret)
  2432. ret = err;
  2433. return ret;
  2434. }
  2435. static int ext4_mark_dquot_dirty(struct dquot *dquot)
  2436. {
  2437. /* Are we journalling quotas? */
  2438. if (EXT4_SB(dquot->dq_sb)->s_qf_names[USRQUOTA] ||
  2439. EXT4_SB(dquot->dq_sb)->s_qf_names[GRPQUOTA]) {
  2440. dquot_mark_dquot_dirty(dquot);
  2441. return ext4_write_dquot(dquot);
  2442. } else {
  2443. return dquot_mark_dquot_dirty(dquot);
  2444. }
  2445. }
  2446. static int ext4_write_info(struct super_block *sb, int type)
  2447. {
  2448. int ret, err;
  2449. handle_t *handle;
  2450. /* Data block + inode block */
  2451. handle = ext4_journal_start(sb->s_root->d_inode, 2);
  2452. if (IS_ERR(handle))
  2453. return PTR_ERR(handle);
  2454. ret = dquot_commit_info(sb, type);
  2455. err = ext4_journal_stop(handle);
  2456. if (!ret)
  2457. ret = err;
  2458. return ret;
  2459. }
  2460. /*
  2461. * Turn on quotas during mount time - we need to find
  2462. * the quota file and such...
  2463. */
  2464. static int ext4_quota_on_mount(struct super_block *sb, int type)
  2465. {
  2466. return vfs_quota_on_mount(sb, EXT4_SB(sb)->s_qf_names[type],
  2467. EXT4_SB(sb)->s_jquota_fmt, type);
  2468. }
  2469. /*
  2470. * Standard function to be called on quota_on
  2471. */
  2472. static int ext4_quota_on(struct super_block *sb, int type, int format_id,
  2473. char *path)
  2474. {
  2475. int err;
  2476. struct nameidata nd;
  2477. if (!test_opt(sb, QUOTA))
  2478. return -EINVAL;
  2479. /* Not journalling quota? */
  2480. if (!EXT4_SB(sb)->s_qf_names[USRQUOTA] &&
  2481. !EXT4_SB(sb)->s_qf_names[GRPQUOTA])
  2482. return vfs_quota_on(sb, type, format_id, path);
  2483. err = path_lookup(path, LOOKUP_FOLLOW, &nd);
  2484. if (err)
  2485. return err;
  2486. /* Quotafile not on the same filesystem? */
  2487. if (nd.mnt->mnt_sb != sb) {
  2488. path_release(&nd);
  2489. return -EXDEV;
  2490. }
  2491. /* Quotafile not of fs root? */
  2492. if (nd.dentry->d_parent->d_inode != sb->s_root->d_inode)
  2493. printk(KERN_WARNING
  2494. "EXT4-fs: Quota file not on filesystem root. "
  2495. "Journalled quota will not work.\n");
  2496. path_release(&nd);
  2497. return vfs_quota_on(sb, type, format_id, path);
  2498. }
  2499. /* Read data from quotafile - avoid pagecache and such because we cannot afford
  2500. * acquiring the locks... As quota files are never truncated and quota code
  2501. * itself serializes the operations (and noone else should touch the files)
  2502. * we don't have to be afraid of races */
  2503. static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
  2504. size_t len, loff_t off)
  2505. {
  2506. struct inode *inode = sb_dqopt(sb)->files[type];
  2507. sector_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
  2508. int err = 0;
  2509. int offset = off & (sb->s_blocksize - 1);
  2510. int tocopy;
  2511. size_t toread;
  2512. struct buffer_head *bh;
  2513. loff_t i_size = i_size_read(inode);
  2514. if (off > i_size)
  2515. return 0;
  2516. if (off+len > i_size)
  2517. len = i_size-off;
  2518. toread = len;
  2519. while (toread > 0) {
  2520. tocopy = sb->s_blocksize - offset < toread ?
  2521. sb->s_blocksize - offset : toread;
  2522. bh = ext4_bread(NULL, inode, blk, 0, &err);
  2523. if (err)
  2524. return err;
  2525. if (!bh) /* A hole? */
  2526. memset(data, 0, tocopy);
  2527. else
  2528. memcpy(data, bh->b_data+offset, tocopy);
  2529. brelse(bh);
  2530. offset = 0;
  2531. toread -= tocopy;
  2532. data += tocopy;
  2533. blk++;
  2534. }
  2535. return len;
  2536. }
  2537. /* Write to quotafile (we know the transaction is already started and has
  2538. * enough credits) */
  2539. static ssize_t ext4_quota_write(struct super_block *sb, int type,
  2540. const char *data, size_t len, loff_t off)
  2541. {
  2542. struct inode *inode = sb_dqopt(sb)->files[type];
  2543. sector_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
  2544. int err = 0;
  2545. int offset = off & (sb->s_blocksize - 1);
  2546. int tocopy;
  2547. int journal_quota = EXT4_SB(sb)->s_qf_names[type] != NULL;
  2548. size_t towrite = len;
  2549. struct buffer_head *bh;
  2550. handle_t *handle = journal_current_handle();
  2551. if (!handle) {
  2552. printk(KERN_WARNING "EXT4-fs: Quota write (off=%Lu, len=%Lu)"
  2553. " cancelled because transaction is not started.\n",
  2554. (unsigned long long)off, (unsigned long long)len);
  2555. return -EIO;
  2556. }
  2557. mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
  2558. while (towrite > 0) {
  2559. tocopy = sb->s_blocksize - offset < towrite ?
  2560. sb->s_blocksize - offset : towrite;
  2561. bh = ext4_bread(handle, inode, blk, 1, &err);
  2562. if (!bh)
  2563. goto out;
  2564. if (journal_quota) {
  2565. err = ext4_journal_get_write_access(handle, bh);
  2566. if (err) {
  2567. brelse(bh);
  2568. goto out;
  2569. }
  2570. }
  2571. lock_buffer(bh);
  2572. memcpy(bh->b_data+offset, data, tocopy);
  2573. flush_dcache_page(bh->b_page);
  2574. unlock_buffer(bh);
  2575. if (journal_quota)
  2576. err = ext4_journal_dirty_metadata(handle, bh);
  2577. else {
  2578. /* Always do at least ordered writes for quotas */
  2579. err = ext4_journal_dirty_data(handle, bh);
  2580. mark_buffer_dirty(bh);
  2581. }
  2582. brelse(bh);
  2583. if (err)
  2584. goto out;
  2585. offset = 0;
  2586. towrite -= tocopy;
  2587. data += tocopy;
  2588. blk++;
  2589. }
  2590. out:
  2591. if (len == towrite)
  2592. return err;
  2593. if (inode->i_size < off+len-towrite) {
  2594. i_size_write(inode, off+len-towrite);
  2595. EXT4_I(inode)->i_disksize = inode->i_size;
  2596. }
  2597. inode->i_version++;
  2598. inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  2599. ext4_mark_inode_dirty(handle, inode);
  2600. mutex_unlock(&inode->i_mutex);
  2601. return len - towrite;
  2602. }
  2603. #endif
  2604. static int ext4_get_sb(struct file_system_type *fs_type,
  2605. int flags, const char *dev_name, void *data, struct vfsmount *mnt)
  2606. {
  2607. return get_sb_bdev(fs_type, flags, dev_name, data, ext4_fill_super, mnt);
  2608. }
  2609. static struct file_system_type ext4dev_fs_type = {
  2610. .owner = THIS_MODULE,
  2611. .name = "ext4dev",
  2612. .get_sb = ext4_get_sb,
  2613. .kill_sb = kill_block_super,
  2614. .fs_flags = FS_REQUIRES_DEV,
  2615. };
  2616. static int __init init_ext4_fs(void)
  2617. {
  2618. int err = init_ext4_xattr();
  2619. if (err)
  2620. return err;
  2621. err = init_inodecache();
  2622. if (err)
  2623. goto out1;
  2624. err = register_filesystem(&ext4dev_fs_type);
  2625. if (err)
  2626. goto out;
  2627. return 0;
  2628. out:
  2629. destroy_inodecache();
  2630. out1:
  2631. exit_ext4_xattr();
  2632. return err;
  2633. }
  2634. static void __exit exit_ext4_fs(void)
  2635. {
  2636. unregister_filesystem(&ext4dev_fs_type);
  2637. destroy_inodecache();
  2638. exit_ext4_xattr();
  2639. }
  2640. MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
  2641. MODULE_DESCRIPTION("Fourth Extended Filesystem with extents");
  2642. MODULE_LICENSE("GPL");
  2643. module_init(init_ext4_fs)
  2644. module_exit(exit_ext4_fs)