super.c 78 KB

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