super.c 77 KB

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