super.c 79 KB

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