super.c 85 KB

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