super.c 91 KB

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