super.c 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177
  1. /*
  2. * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
  3. *
  4. * Trivial changes by Alan Cox to add the LFS fixes
  5. *
  6. * Trivial Changes:
  7. * Rights granted to Hans Reiser to redistribute under other terms providing
  8. * he accepts all liability including but not limited to patent, fitness
  9. * for purpose, and direct or indirect claims arising from failure to perform.
  10. *
  11. * NO WARRANTY
  12. */
  13. #include <linux/module.h>
  14. #include <linux/vmalloc.h>
  15. #include <linux/time.h>
  16. #include <asm/uaccess.h>
  17. #include <linux/reiserfs_fs.h>
  18. #include <linux/reiserfs_acl.h>
  19. #include <linux/reiserfs_xattr.h>
  20. #include <linux/smp_lock.h>
  21. #include <linux/init.h>
  22. #include <linux/blkdev.h>
  23. #include <linux/buffer_head.h>
  24. #include <linux/vfs.h>
  25. #include <linux/mnt_namespace.h>
  26. #include <linux/mount.h>
  27. #include <linux/namei.h>
  28. #include <linux/quotaops.h>
  29. struct file_system_type reiserfs_fs_type;
  30. static const char reiserfs_3_5_magic_string[] = REISERFS_SUPER_MAGIC_STRING;
  31. static const char reiserfs_3_6_magic_string[] = REISER2FS_SUPER_MAGIC_STRING;
  32. static const char reiserfs_jr_magic_string[] = REISER2FS_JR_SUPER_MAGIC_STRING;
  33. int is_reiserfs_3_5(struct reiserfs_super_block *rs)
  34. {
  35. return !strncmp(rs->s_v1.s_magic, reiserfs_3_5_magic_string,
  36. strlen(reiserfs_3_5_magic_string));
  37. }
  38. int is_reiserfs_3_6(struct reiserfs_super_block *rs)
  39. {
  40. return !strncmp(rs->s_v1.s_magic, reiserfs_3_6_magic_string,
  41. strlen(reiserfs_3_6_magic_string));
  42. }
  43. int is_reiserfs_jr(struct reiserfs_super_block *rs)
  44. {
  45. return !strncmp(rs->s_v1.s_magic, reiserfs_jr_magic_string,
  46. strlen(reiserfs_jr_magic_string));
  47. }
  48. static int is_any_reiserfs_magic_string(struct reiserfs_super_block *rs)
  49. {
  50. return (is_reiserfs_3_5(rs) || is_reiserfs_3_6(rs) ||
  51. is_reiserfs_jr(rs));
  52. }
  53. static int reiserfs_remount(struct super_block *s, int *flags, char *data);
  54. static int reiserfs_statfs(struct dentry *dentry, struct kstatfs *buf);
  55. static int reiserfs_sync_fs(struct super_block *s, int wait)
  56. {
  57. if (!(s->s_flags & MS_RDONLY)) {
  58. struct reiserfs_transaction_handle th;
  59. reiserfs_write_lock(s);
  60. if (!journal_begin(&th, s, 1))
  61. if (!journal_end_sync(&th, s, 1))
  62. reiserfs_flush_old_commits(s);
  63. s->s_dirt = 0; /* Even if it's not true.
  64. * We'll loop forever in sync_supers otherwise */
  65. reiserfs_write_unlock(s);
  66. } else {
  67. s->s_dirt = 0;
  68. }
  69. return 0;
  70. }
  71. static void reiserfs_write_super(struct super_block *s)
  72. {
  73. reiserfs_sync_fs(s, 1);
  74. }
  75. static void reiserfs_write_super_lockfs(struct super_block *s)
  76. {
  77. struct reiserfs_transaction_handle th;
  78. reiserfs_write_lock(s);
  79. if (!(s->s_flags & MS_RDONLY)) {
  80. int err = journal_begin(&th, s, 1);
  81. if (err) {
  82. reiserfs_block_writes(&th);
  83. } else {
  84. reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s),
  85. 1);
  86. journal_mark_dirty(&th, s, SB_BUFFER_WITH_SB(s));
  87. reiserfs_block_writes(&th);
  88. journal_end_sync(&th, s, 1);
  89. }
  90. }
  91. s->s_dirt = 0;
  92. reiserfs_write_unlock(s);
  93. }
  94. static void reiserfs_unlockfs(struct super_block *s)
  95. {
  96. reiserfs_allow_writes(s);
  97. }
  98. extern const struct in_core_key MAX_IN_CORE_KEY;
  99. /* this is used to delete "save link" when there are no items of a
  100. file it points to. It can either happen if unlink is completed but
  101. "save unlink" removal, or if file has both unlink and truncate
  102. pending and as unlink completes first (because key of "save link"
  103. protecting unlink is bigger that a key lf "save link" which
  104. protects truncate), so there left no items to make truncate
  105. completion on */
  106. static int remove_save_link_only(struct super_block *s,
  107. struct reiserfs_key *key, int oid_free)
  108. {
  109. struct reiserfs_transaction_handle th;
  110. int err;
  111. /* we are going to do one balancing */
  112. err = journal_begin(&th, s, JOURNAL_PER_BALANCE_CNT);
  113. if (err)
  114. return err;
  115. reiserfs_delete_solid_item(&th, NULL, key);
  116. if (oid_free)
  117. /* removals are protected by direct items */
  118. reiserfs_release_objectid(&th, le32_to_cpu(key->k_objectid));
  119. return journal_end(&th, s, JOURNAL_PER_BALANCE_CNT);
  120. }
  121. #ifdef CONFIG_QUOTA
  122. static int reiserfs_quota_on_mount(struct super_block *, int);
  123. #endif
  124. /* look for uncompleted unlinks and truncates and complete them */
  125. static int finish_unfinished(struct super_block *s)
  126. {
  127. INITIALIZE_PATH(path);
  128. struct cpu_key max_cpu_key, obj_key;
  129. struct reiserfs_key save_link_key;
  130. int retval = 0;
  131. struct item_head *ih;
  132. struct buffer_head *bh;
  133. int item_pos;
  134. char *item;
  135. int done;
  136. struct inode *inode;
  137. int truncate;
  138. #ifdef CONFIG_QUOTA
  139. int i;
  140. int ms_active_set;
  141. #endif
  142. /* compose key to look for "save" links */
  143. max_cpu_key.version = KEY_FORMAT_3_5;
  144. max_cpu_key.on_disk_key.k_dir_id = ~0U;
  145. max_cpu_key.on_disk_key.k_objectid = ~0U;
  146. set_cpu_key_k_offset(&max_cpu_key, ~0U);
  147. max_cpu_key.key_length = 3;
  148. #ifdef CONFIG_QUOTA
  149. /* Needed for iput() to work correctly and not trash data */
  150. if (s->s_flags & MS_ACTIVE) {
  151. ms_active_set = 0;
  152. } else {
  153. ms_active_set = 1;
  154. s->s_flags |= MS_ACTIVE;
  155. }
  156. /* Turn on quotas so that they are updated correctly */
  157. for (i = 0; i < MAXQUOTAS; i++) {
  158. if (REISERFS_SB(s)->s_qf_names[i]) {
  159. int ret = reiserfs_quota_on_mount(s, i);
  160. if (ret < 0)
  161. reiserfs_warning(s,
  162. "reiserfs: cannot turn on journalled quota: error %d",
  163. ret);
  164. }
  165. }
  166. #endif
  167. done = 0;
  168. REISERFS_SB(s)->s_is_unlinked_ok = 1;
  169. while (!retval) {
  170. retval = search_item(s, &max_cpu_key, &path);
  171. if (retval != ITEM_NOT_FOUND) {
  172. reiserfs_warning(s,
  173. "vs-2140: finish_unfinished: search_by_key returned %d",
  174. retval);
  175. break;
  176. }
  177. bh = get_last_bh(&path);
  178. item_pos = get_item_pos(&path);
  179. if (item_pos != B_NR_ITEMS(bh)) {
  180. reiserfs_warning(s,
  181. "vs-2060: finish_unfinished: wrong position found");
  182. break;
  183. }
  184. item_pos--;
  185. ih = B_N_PITEM_HEAD(bh, item_pos);
  186. if (le32_to_cpu(ih->ih_key.k_dir_id) != MAX_KEY_OBJECTID)
  187. /* there are no "save" links anymore */
  188. break;
  189. save_link_key = ih->ih_key;
  190. if (is_indirect_le_ih(ih))
  191. truncate = 1;
  192. else
  193. truncate = 0;
  194. /* reiserfs_iget needs k_dirid and k_objectid only */
  195. item = B_I_PITEM(bh, ih);
  196. obj_key.on_disk_key.k_dir_id = le32_to_cpu(*(__le32 *) item);
  197. obj_key.on_disk_key.k_objectid =
  198. le32_to_cpu(ih->ih_key.k_objectid);
  199. obj_key.on_disk_key.k_offset = 0;
  200. obj_key.on_disk_key.k_type = 0;
  201. pathrelse(&path);
  202. inode = reiserfs_iget(s, &obj_key);
  203. if (!inode) {
  204. /* the unlink almost completed, it just did not manage to remove
  205. "save" link and release objectid */
  206. reiserfs_warning(s,
  207. "vs-2180: finish_unfinished: iget failed for %K",
  208. &obj_key);
  209. retval = remove_save_link_only(s, &save_link_key, 1);
  210. continue;
  211. }
  212. if (!truncate && inode->i_nlink) {
  213. /* file is not unlinked */
  214. reiserfs_warning(s,
  215. "vs-2185: finish_unfinished: file %K is not unlinked",
  216. &obj_key);
  217. retval = remove_save_link_only(s, &save_link_key, 0);
  218. continue;
  219. }
  220. DQUOT_INIT(inode);
  221. if (truncate && S_ISDIR(inode->i_mode)) {
  222. /* We got a truncate request for a dir which is impossible.
  223. The only imaginable way is to execute unfinished truncate request
  224. then boot into old kernel, remove the file and create dir with
  225. the same key. */
  226. reiserfs_warning(s,
  227. "green-2101: impossible truncate on a directory %k. Please report",
  228. INODE_PKEY(inode));
  229. retval = remove_save_link_only(s, &save_link_key, 0);
  230. truncate = 0;
  231. iput(inode);
  232. continue;
  233. }
  234. if (truncate) {
  235. REISERFS_I(inode)->i_flags |=
  236. i_link_saved_truncate_mask;
  237. /* not completed truncate found. New size was committed together
  238. with "save" link */
  239. reiserfs_info(s, "Truncating %k to %Ld ..",
  240. INODE_PKEY(inode), inode->i_size);
  241. reiserfs_truncate_file(inode,
  242. 0
  243. /*don't update modification time */
  244. );
  245. retval = remove_save_link(inode, truncate);
  246. } else {
  247. REISERFS_I(inode)->i_flags |= i_link_saved_unlink_mask;
  248. /* not completed unlink (rmdir) found */
  249. reiserfs_info(s, "Removing %k..", INODE_PKEY(inode));
  250. /* removal gets completed in iput */
  251. retval = 0;
  252. }
  253. iput(inode);
  254. printk("done\n");
  255. done++;
  256. }
  257. REISERFS_SB(s)->s_is_unlinked_ok = 0;
  258. #ifdef CONFIG_QUOTA
  259. /* Turn quotas off */
  260. for (i = 0; i < MAXQUOTAS; i++) {
  261. if (sb_dqopt(s)->files[i])
  262. vfs_quota_off_mount(s, i);
  263. }
  264. if (ms_active_set)
  265. /* Restore the flag back */
  266. s->s_flags &= ~MS_ACTIVE;
  267. #endif
  268. pathrelse(&path);
  269. if (done)
  270. reiserfs_info(s, "There were %d uncompleted unlinks/truncates. "
  271. "Completed\n", done);
  272. return retval;
  273. }
  274. /* to protect file being unlinked from getting lost we "safe" link files
  275. being unlinked. This link will be deleted in the same transaction with last
  276. item of file. mounting the filesytem we scan all these links and remove
  277. files which almost got lost */
  278. void add_save_link(struct reiserfs_transaction_handle *th,
  279. struct inode *inode, int truncate)
  280. {
  281. INITIALIZE_PATH(path);
  282. int retval;
  283. struct cpu_key key;
  284. struct item_head ih;
  285. __le32 link;
  286. BUG_ON(!th->t_trans_id);
  287. /* file can only get one "save link" of each kind */
  288. RFALSE(truncate &&
  289. (REISERFS_I(inode)->i_flags & i_link_saved_truncate_mask),
  290. "saved link already exists for truncated inode %lx",
  291. (long)inode->i_ino);
  292. RFALSE(!truncate &&
  293. (REISERFS_I(inode)->i_flags & i_link_saved_unlink_mask),
  294. "saved link already exists for unlinked inode %lx",
  295. (long)inode->i_ino);
  296. /* setup key of "save" link */
  297. key.version = KEY_FORMAT_3_5;
  298. key.on_disk_key.k_dir_id = MAX_KEY_OBJECTID;
  299. key.on_disk_key.k_objectid = inode->i_ino;
  300. if (!truncate) {
  301. /* unlink, rmdir, rename */
  302. set_cpu_key_k_offset(&key, 1 + inode->i_sb->s_blocksize);
  303. set_cpu_key_k_type(&key, TYPE_DIRECT);
  304. /* item head of "safe" link */
  305. make_le_item_head(&ih, &key, key.version,
  306. 1 + inode->i_sb->s_blocksize, TYPE_DIRECT,
  307. 4 /*length */ , 0xffff /*free space */ );
  308. } else {
  309. /* truncate */
  310. if (S_ISDIR(inode->i_mode))
  311. reiserfs_warning(inode->i_sb,
  312. "green-2102: Adding a truncate savelink for a directory %k! Please report",
  313. INODE_PKEY(inode));
  314. set_cpu_key_k_offset(&key, 1);
  315. set_cpu_key_k_type(&key, TYPE_INDIRECT);
  316. /* item head of "safe" link */
  317. make_le_item_head(&ih, &key, key.version, 1, TYPE_INDIRECT,
  318. 4 /*length */ , 0 /*free space */ );
  319. }
  320. key.key_length = 3;
  321. /* look for its place in the tree */
  322. retval = search_item(inode->i_sb, &key, &path);
  323. if (retval != ITEM_NOT_FOUND) {
  324. if (retval != -ENOSPC)
  325. reiserfs_warning(inode->i_sb, "vs-2100: add_save_link:"
  326. "search_by_key (%K) returned %d", &key,
  327. retval);
  328. pathrelse(&path);
  329. return;
  330. }
  331. /* body of "save" link */
  332. link = INODE_PKEY(inode)->k_dir_id;
  333. /* put "save" link inot tree, don't charge quota to anyone */
  334. retval =
  335. reiserfs_insert_item(th, &path, &key, &ih, NULL, (char *)&link);
  336. if (retval) {
  337. if (retval != -ENOSPC)
  338. reiserfs_warning(inode->i_sb,
  339. "vs-2120: add_save_link: insert_item returned %d",
  340. retval);
  341. } else {
  342. if (truncate)
  343. REISERFS_I(inode)->i_flags |=
  344. i_link_saved_truncate_mask;
  345. else
  346. REISERFS_I(inode)->i_flags |= i_link_saved_unlink_mask;
  347. }
  348. }
  349. /* this opens transaction unlike add_save_link */
  350. int remove_save_link(struct inode *inode, int truncate)
  351. {
  352. struct reiserfs_transaction_handle th;
  353. struct reiserfs_key key;
  354. int err;
  355. /* we are going to do one balancing only */
  356. err = journal_begin(&th, inode->i_sb, JOURNAL_PER_BALANCE_CNT);
  357. if (err)
  358. return err;
  359. /* setup key of "save" link */
  360. key.k_dir_id = cpu_to_le32(MAX_KEY_OBJECTID);
  361. key.k_objectid = INODE_PKEY(inode)->k_objectid;
  362. if (!truncate) {
  363. /* unlink, rmdir, rename */
  364. set_le_key_k_offset(KEY_FORMAT_3_5, &key,
  365. 1 + inode->i_sb->s_blocksize);
  366. set_le_key_k_type(KEY_FORMAT_3_5, &key, TYPE_DIRECT);
  367. } else {
  368. /* truncate */
  369. set_le_key_k_offset(KEY_FORMAT_3_5, &key, 1);
  370. set_le_key_k_type(KEY_FORMAT_3_5, &key, TYPE_INDIRECT);
  371. }
  372. if ((truncate &&
  373. (REISERFS_I(inode)->i_flags & i_link_saved_truncate_mask)) ||
  374. (!truncate &&
  375. (REISERFS_I(inode)->i_flags & i_link_saved_unlink_mask)))
  376. /* don't take quota bytes from anywhere */
  377. reiserfs_delete_solid_item(&th, NULL, &key);
  378. if (!truncate) {
  379. reiserfs_release_objectid(&th, inode->i_ino);
  380. REISERFS_I(inode)->i_flags &= ~i_link_saved_unlink_mask;
  381. } else
  382. REISERFS_I(inode)->i_flags &= ~i_link_saved_truncate_mask;
  383. return journal_end(&th, inode->i_sb, JOURNAL_PER_BALANCE_CNT);
  384. }
  385. static void reiserfs_kill_sb(struct super_block *s)
  386. {
  387. if (REISERFS_SB(s)) {
  388. if (REISERFS_SB(s)->xattr_root) {
  389. d_invalidate(REISERFS_SB(s)->xattr_root);
  390. dput(REISERFS_SB(s)->xattr_root);
  391. REISERFS_SB(s)->xattr_root = NULL;
  392. }
  393. if (REISERFS_SB(s)->priv_root) {
  394. d_invalidate(REISERFS_SB(s)->priv_root);
  395. dput(REISERFS_SB(s)->priv_root);
  396. REISERFS_SB(s)->priv_root = NULL;
  397. }
  398. }
  399. kill_block_super(s);
  400. }
  401. static void reiserfs_put_super(struct super_block *s)
  402. {
  403. struct reiserfs_transaction_handle th;
  404. th.t_trans_id = 0;
  405. /* change file system state to current state if it was mounted with read-write permissions */
  406. if (!(s->s_flags & MS_RDONLY)) {
  407. if (!journal_begin(&th, s, 10)) {
  408. reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s),
  409. 1);
  410. set_sb_umount_state(SB_DISK_SUPER_BLOCK(s),
  411. REISERFS_SB(s)->s_mount_state);
  412. journal_mark_dirty(&th, s, SB_BUFFER_WITH_SB(s));
  413. }
  414. }
  415. /* note, journal_release checks for readonly mount, and can decide not
  416. ** to do a journal_end
  417. */
  418. journal_release(&th, s);
  419. reiserfs_free_bitmap_cache(s);
  420. brelse(SB_BUFFER_WITH_SB(s));
  421. print_statistics(s);
  422. if (REISERFS_SB(s)->reserved_blocks != 0) {
  423. reiserfs_warning(s,
  424. "green-2005: reiserfs_put_super: reserved blocks left %d",
  425. REISERFS_SB(s)->reserved_blocks);
  426. }
  427. reiserfs_proc_info_done(s);
  428. kfree(s->s_fs_info);
  429. s->s_fs_info = NULL;
  430. return;
  431. }
  432. static struct kmem_cache *reiserfs_inode_cachep;
  433. static struct inode *reiserfs_alloc_inode(struct super_block *sb)
  434. {
  435. struct reiserfs_inode_info *ei;
  436. ei = (struct reiserfs_inode_info *)
  437. kmem_cache_alloc(reiserfs_inode_cachep, GFP_KERNEL);
  438. if (!ei)
  439. return NULL;
  440. return &ei->vfs_inode;
  441. }
  442. static void reiserfs_destroy_inode(struct inode *inode)
  443. {
  444. kmem_cache_free(reiserfs_inode_cachep, REISERFS_I(inode));
  445. }
  446. static void init_once(void *foo, struct kmem_cache * cachep, unsigned long flags)
  447. {
  448. struct reiserfs_inode_info *ei = (struct reiserfs_inode_info *)foo;
  449. if ((flags & (SLAB_CTOR_VERIFY | SLAB_CTOR_CONSTRUCTOR)) ==
  450. SLAB_CTOR_CONSTRUCTOR) {
  451. INIT_LIST_HEAD(&ei->i_prealloc_list);
  452. inode_init_once(&ei->vfs_inode);
  453. #ifdef CONFIG_REISERFS_FS_POSIX_ACL
  454. ei->i_acl_access = NULL;
  455. ei->i_acl_default = NULL;
  456. #endif
  457. }
  458. }
  459. static int init_inodecache(void)
  460. {
  461. reiserfs_inode_cachep = kmem_cache_create("reiser_inode_cache",
  462. sizeof(struct
  463. reiserfs_inode_info),
  464. 0, (SLAB_RECLAIM_ACCOUNT|
  465. SLAB_MEM_SPREAD),
  466. init_once, NULL);
  467. if (reiserfs_inode_cachep == NULL)
  468. return -ENOMEM;
  469. return 0;
  470. }
  471. static void destroy_inodecache(void)
  472. {
  473. kmem_cache_destroy(reiserfs_inode_cachep);
  474. }
  475. /* we don't mark inodes dirty, we just log them */
  476. static void reiserfs_dirty_inode(struct inode *inode)
  477. {
  478. struct reiserfs_transaction_handle th;
  479. int err = 0;
  480. if (inode->i_sb->s_flags & MS_RDONLY) {
  481. reiserfs_warning(inode->i_sb,
  482. "clm-6006: writing inode %lu on readonly FS",
  483. inode->i_ino);
  484. return;
  485. }
  486. reiserfs_write_lock(inode->i_sb);
  487. /* this is really only used for atime updates, so they don't have
  488. ** to be included in O_SYNC or fsync
  489. */
  490. err = journal_begin(&th, inode->i_sb, 1);
  491. if (err) {
  492. reiserfs_write_unlock(inode->i_sb);
  493. return;
  494. }
  495. reiserfs_update_sd(&th, inode);
  496. journal_end(&th, inode->i_sb, 1);
  497. reiserfs_write_unlock(inode->i_sb);
  498. }
  499. #ifdef CONFIG_REISERFS_FS_POSIX_ACL
  500. static void reiserfs_clear_inode(struct inode *inode)
  501. {
  502. struct posix_acl *acl;
  503. acl = REISERFS_I(inode)->i_acl_access;
  504. if (acl && !IS_ERR(acl))
  505. posix_acl_release(acl);
  506. REISERFS_I(inode)->i_acl_access = NULL;
  507. acl = REISERFS_I(inode)->i_acl_default;
  508. if (acl && !IS_ERR(acl))
  509. posix_acl_release(acl);
  510. REISERFS_I(inode)->i_acl_default = NULL;
  511. }
  512. #else
  513. #define reiserfs_clear_inode NULL
  514. #endif
  515. #ifdef CONFIG_QUOTA
  516. static ssize_t reiserfs_quota_write(struct super_block *, int, const char *,
  517. size_t, loff_t);
  518. static ssize_t reiserfs_quota_read(struct super_block *, int, char *, size_t,
  519. loff_t);
  520. #endif
  521. static const struct super_operations reiserfs_sops = {
  522. .alloc_inode = reiserfs_alloc_inode,
  523. .destroy_inode = reiserfs_destroy_inode,
  524. .write_inode = reiserfs_write_inode,
  525. .dirty_inode = reiserfs_dirty_inode,
  526. .delete_inode = reiserfs_delete_inode,
  527. .clear_inode = reiserfs_clear_inode,
  528. .put_super = reiserfs_put_super,
  529. .write_super = reiserfs_write_super,
  530. .sync_fs = reiserfs_sync_fs,
  531. .write_super_lockfs = reiserfs_write_super_lockfs,
  532. .unlockfs = reiserfs_unlockfs,
  533. .statfs = reiserfs_statfs,
  534. .remount_fs = reiserfs_remount,
  535. #ifdef CONFIG_QUOTA
  536. .quota_read = reiserfs_quota_read,
  537. .quota_write = reiserfs_quota_write,
  538. #endif
  539. };
  540. #ifdef CONFIG_QUOTA
  541. #define QTYPE2NAME(t) ((t)==USRQUOTA?"user":"group")
  542. static int reiserfs_dquot_initialize(struct inode *, int);
  543. static int reiserfs_dquot_drop(struct inode *);
  544. static int reiserfs_write_dquot(struct dquot *);
  545. static int reiserfs_acquire_dquot(struct dquot *);
  546. static int reiserfs_release_dquot(struct dquot *);
  547. static int reiserfs_mark_dquot_dirty(struct dquot *);
  548. static int reiserfs_write_info(struct super_block *, int);
  549. static int reiserfs_quota_on(struct super_block *, int, int, char *);
  550. static struct dquot_operations reiserfs_quota_operations = {
  551. .initialize = reiserfs_dquot_initialize,
  552. .drop = reiserfs_dquot_drop,
  553. .alloc_space = dquot_alloc_space,
  554. .alloc_inode = dquot_alloc_inode,
  555. .free_space = dquot_free_space,
  556. .free_inode = dquot_free_inode,
  557. .transfer = dquot_transfer,
  558. .write_dquot = reiserfs_write_dquot,
  559. .acquire_dquot = reiserfs_acquire_dquot,
  560. .release_dquot = reiserfs_release_dquot,
  561. .mark_dirty = reiserfs_mark_dquot_dirty,
  562. .write_info = reiserfs_write_info,
  563. };
  564. static struct quotactl_ops reiserfs_qctl_operations = {
  565. .quota_on = reiserfs_quota_on,
  566. .quota_off = vfs_quota_off,
  567. .quota_sync = vfs_quota_sync,
  568. .get_info = vfs_get_dqinfo,
  569. .set_info = vfs_set_dqinfo,
  570. .get_dqblk = vfs_get_dqblk,
  571. .set_dqblk = vfs_set_dqblk,
  572. };
  573. #endif
  574. static struct export_operations reiserfs_export_ops = {
  575. .encode_fh = reiserfs_encode_fh,
  576. .decode_fh = reiserfs_decode_fh,
  577. .get_parent = reiserfs_get_parent,
  578. .get_dentry = reiserfs_get_dentry,
  579. };
  580. /* this struct is used in reiserfs_getopt () for containing the value for those
  581. mount options that have values rather than being toggles. */
  582. typedef struct {
  583. char *value;
  584. int setmask; /* bitmask which is to set on mount_options bitmask when this
  585. value is found, 0 is no bits are to be changed. */
  586. int clrmask; /* bitmask which is to clear on mount_options bitmask when this
  587. value is found, 0 is no bits are to be changed. This is
  588. applied BEFORE setmask */
  589. } arg_desc_t;
  590. /* Set this bit in arg_required to allow empty arguments */
  591. #define REISERFS_OPT_ALLOWEMPTY 31
  592. /* this struct is used in reiserfs_getopt() for describing the set of reiserfs
  593. mount options */
  594. typedef struct {
  595. char *option_name;
  596. int arg_required; /* 0 if argument is not required, not 0 otherwise */
  597. const arg_desc_t *values; /* list of values accepted by an option */
  598. int setmask; /* bitmask which is to set on mount_options bitmask when this
  599. value is found, 0 is no bits are to be changed. */
  600. int clrmask; /* bitmask which is to clear on mount_options bitmask when this
  601. value is found, 0 is no bits are to be changed. This is
  602. applied BEFORE setmask */
  603. } opt_desc_t;
  604. /* possible values for -o data= */
  605. static const arg_desc_t logging_mode[] = {
  606. {"ordered", 1 << REISERFS_DATA_ORDERED,
  607. (1 << REISERFS_DATA_LOG | 1 << REISERFS_DATA_WRITEBACK)},
  608. {"journal", 1 << REISERFS_DATA_LOG,
  609. (1 << REISERFS_DATA_ORDERED | 1 << REISERFS_DATA_WRITEBACK)},
  610. {"writeback", 1 << REISERFS_DATA_WRITEBACK,
  611. (1 << REISERFS_DATA_ORDERED | 1 << REISERFS_DATA_LOG)},
  612. {.value = NULL}
  613. };
  614. /* possible values for -o barrier= */
  615. static const arg_desc_t barrier_mode[] = {
  616. {"none", 1 << REISERFS_BARRIER_NONE, 1 << REISERFS_BARRIER_FLUSH},
  617. {"flush", 1 << REISERFS_BARRIER_FLUSH, 1 << REISERFS_BARRIER_NONE},
  618. {.value = NULL}
  619. };
  620. /* possible values for "-o block-allocator=" and bits which are to be set in
  621. s_mount_opt of reiserfs specific part of in-core super block */
  622. static const arg_desc_t balloc[] = {
  623. {"noborder", 1 << REISERFS_NO_BORDER, 0},
  624. {"border", 0, 1 << REISERFS_NO_BORDER},
  625. {"no_unhashed_relocation", 1 << REISERFS_NO_UNHASHED_RELOCATION, 0},
  626. {"hashed_relocation", 1 << REISERFS_HASHED_RELOCATION, 0},
  627. {"test4", 1 << REISERFS_TEST4, 0},
  628. {"notest4", 0, 1 << REISERFS_TEST4},
  629. {NULL, 0, 0}
  630. };
  631. static const arg_desc_t tails[] = {
  632. {"on", 1 << REISERFS_LARGETAIL, 1 << REISERFS_SMALLTAIL},
  633. {"off", 0, (1 << REISERFS_LARGETAIL) | (1 << REISERFS_SMALLTAIL)},
  634. {"small", 1 << REISERFS_SMALLTAIL, 1 << REISERFS_LARGETAIL},
  635. {NULL, 0, 0}
  636. };
  637. static const arg_desc_t error_actions[] = {
  638. {"panic", 1 << REISERFS_ERROR_PANIC,
  639. (1 << REISERFS_ERROR_RO | 1 << REISERFS_ERROR_CONTINUE)},
  640. {"ro-remount", 1 << REISERFS_ERROR_RO,
  641. (1 << REISERFS_ERROR_PANIC | 1 << REISERFS_ERROR_CONTINUE)},
  642. #ifdef REISERFS_JOURNAL_ERROR_ALLOWS_NO_LOG
  643. {"continue", 1 << REISERFS_ERROR_CONTINUE,
  644. (1 << REISERFS_ERROR_PANIC | 1 << REISERFS_ERROR_RO)},
  645. #endif
  646. {NULL, 0, 0},
  647. };
  648. /* proceed only one option from a list *cur - string containing of mount options
  649. opts - array of options which are accepted
  650. opt_arg - if option is found and requires an argument and if it is specifed
  651. in the input - pointer to the argument is stored here
  652. bit_flags - if option requires to set a certain bit - it is set here
  653. return -1 if unknown option is found, opt->arg_required otherwise */
  654. static int reiserfs_getopt(struct super_block *s, char **cur, opt_desc_t * opts,
  655. char **opt_arg, unsigned long *bit_flags)
  656. {
  657. char *p;
  658. /* foo=bar,
  659. ^ ^ ^
  660. | | +-- option_end
  661. | +-- arg_start
  662. +-- option_start
  663. */
  664. const opt_desc_t *opt;
  665. const arg_desc_t *arg;
  666. p = *cur;
  667. /* assume argument cannot contain commas */
  668. *cur = strchr(p, ',');
  669. if (*cur) {
  670. *(*cur) = '\0';
  671. (*cur)++;
  672. }
  673. if (!strncmp(p, "alloc=", 6)) {
  674. /* Ugly special case, probably we should redo options parser so that
  675. it can understand several arguments for some options, also so that
  676. it can fill several bitfields with option values. */
  677. if (reiserfs_parse_alloc_options(s, p + 6)) {
  678. return -1;
  679. } else {
  680. return 0;
  681. }
  682. }
  683. /* for every option in the list */
  684. for (opt = opts; opt->option_name; opt++) {
  685. if (!strncmp(p, opt->option_name, strlen(opt->option_name))) {
  686. if (bit_flags) {
  687. if (opt->clrmask ==
  688. (1 << REISERFS_UNSUPPORTED_OPT))
  689. reiserfs_warning(s, "%s not supported.",
  690. p);
  691. else
  692. *bit_flags &= ~opt->clrmask;
  693. if (opt->setmask ==
  694. (1 << REISERFS_UNSUPPORTED_OPT))
  695. reiserfs_warning(s, "%s not supported.",
  696. p);
  697. else
  698. *bit_flags |= opt->setmask;
  699. }
  700. break;
  701. }
  702. }
  703. if (!opt->option_name) {
  704. reiserfs_warning(s, "unknown mount option \"%s\"", p);
  705. return -1;
  706. }
  707. p += strlen(opt->option_name);
  708. switch (*p) {
  709. case '=':
  710. if (!opt->arg_required) {
  711. reiserfs_warning(s,
  712. "the option \"%s\" does not require an argument",
  713. opt->option_name);
  714. return -1;
  715. }
  716. break;
  717. case 0:
  718. if (opt->arg_required) {
  719. reiserfs_warning(s,
  720. "the option \"%s\" requires an argument",
  721. opt->option_name);
  722. return -1;
  723. }
  724. break;
  725. default:
  726. reiserfs_warning(s, "head of option \"%s\" is only correct",
  727. opt->option_name);
  728. return -1;
  729. }
  730. /* move to the argument, or to next option if argument is not required */
  731. p++;
  732. if (opt->arg_required
  733. && !(opt->arg_required & (1 << REISERFS_OPT_ALLOWEMPTY))
  734. && !strlen(p)) {
  735. /* this catches "option=," if not allowed */
  736. reiserfs_warning(s, "empty argument for \"%s\"",
  737. opt->option_name);
  738. return -1;
  739. }
  740. if (!opt->values) {
  741. /* *=NULLopt_arg contains pointer to argument */
  742. *opt_arg = p;
  743. return opt->arg_required & ~(1 << REISERFS_OPT_ALLOWEMPTY);
  744. }
  745. /* values possible for this option are listed in opt->values */
  746. for (arg = opt->values; arg->value; arg++) {
  747. if (!strcmp(p, arg->value)) {
  748. if (bit_flags) {
  749. *bit_flags &= ~arg->clrmask;
  750. *bit_flags |= arg->setmask;
  751. }
  752. return opt->arg_required;
  753. }
  754. }
  755. reiserfs_warning(s, "bad value \"%s\" for option \"%s\"", p,
  756. opt->option_name);
  757. return -1;
  758. }
  759. /* returns 0 if something is wrong in option string, 1 - otherwise */
  760. static int reiserfs_parse_options(struct super_block *s, char *options, /* string given via mount's -o */
  761. unsigned long *mount_options,
  762. /* after the parsing phase, contains the
  763. collection of bitflags defining what
  764. mount options were selected. */
  765. unsigned long *blocks, /* strtol-ed from NNN of resize=NNN */
  766. char **jdev_name,
  767. unsigned int *commit_max_age)
  768. {
  769. int c;
  770. char *arg = NULL;
  771. char *pos;
  772. opt_desc_t opts[] = {
  773. /* Compatibility stuff, so that -o notail for old setups still work */
  774. {"tails",.arg_required = 't',.values = tails},
  775. {"notail",.clrmask =
  776. (1 << REISERFS_LARGETAIL) | (1 << REISERFS_SMALLTAIL)},
  777. {"conv",.setmask = 1 << REISERFS_CONVERT},
  778. {"attrs",.setmask = 1 << REISERFS_ATTRS},
  779. {"noattrs",.clrmask = 1 << REISERFS_ATTRS},
  780. #ifdef CONFIG_REISERFS_FS_XATTR
  781. {"user_xattr",.setmask = 1 << REISERFS_XATTRS_USER},
  782. {"nouser_xattr",.clrmask = 1 << REISERFS_XATTRS_USER},
  783. #else
  784. {"user_xattr",.setmask = 1 << REISERFS_UNSUPPORTED_OPT},
  785. {"nouser_xattr",.clrmask = 1 << REISERFS_UNSUPPORTED_OPT},
  786. #endif
  787. #ifdef CONFIG_REISERFS_FS_POSIX_ACL
  788. {"acl",.setmask = 1 << REISERFS_POSIXACL},
  789. {"noacl",.clrmask = 1 << REISERFS_POSIXACL},
  790. #else
  791. {"acl",.setmask = 1 << REISERFS_UNSUPPORTED_OPT},
  792. {"noacl",.clrmask = 1 << REISERFS_UNSUPPORTED_OPT},
  793. #endif
  794. {.option_name = "nolog"},
  795. {"replayonly",.setmask = 1 << REPLAYONLY},
  796. {"block-allocator",.arg_required = 'a',.values = balloc},
  797. {"data",.arg_required = 'd',.values = logging_mode},
  798. {"barrier",.arg_required = 'b',.values = barrier_mode},
  799. {"resize",.arg_required = 'r',.values = NULL},
  800. {"jdev",.arg_required = 'j',.values = NULL},
  801. {"nolargeio",.arg_required = 'w',.values = NULL},
  802. {"commit",.arg_required = 'c',.values = NULL},
  803. {"usrquota",.setmask = 1 << REISERFS_QUOTA},
  804. {"grpquota",.setmask = 1 << REISERFS_QUOTA},
  805. {"noquota",.clrmask = 1 << REISERFS_QUOTA},
  806. {"errors",.arg_required = 'e',.values = error_actions},
  807. {"usrjquota",.arg_required =
  808. 'u' | (1 << REISERFS_OPT_ALLOWEMPTY),.values = NULL},
  809. {"grpjquota",.arg_required =
  810. 'g' | (1 << REISERFS_OPT_ALLOWEMPTY),.values = NULL},
  811. {"jqfmt",.arg_required = 'f',.values = NULL},
  812. {.option_name = NULL}
  813. };
  814. *blocks = 0;
  815. if (!options || !*options)
  816. /* use default configuration: create tails, journaling on, no
  817. conversion to newest format */
  818. return 1;
  819. for (pos = options; pos;) {
  820. c = reiserfs_getopt(s, &pos, opts, &arg, mount_options);
  821. if (c == -1)
  822. /* wrong option is given */
  823. return 0;
  824. if (c == 'r') {
  825. char *p;
  826. p = NULL;
  827. /* "resize=NNN" or "resize=auto" */
  828. if (!strcmp(arg, "auto")) {
  829. /* From JFS code, to auto-get the size. */
  830. *blocks =
  831. s->s_bdev->bd_inode->i_size >> s->
  832. s_blocksize_bits;
  833. } else {
  834. *blocks = simple_strtoul(arg, &p, 0);
  835. if (*p != '\0') {
  836. /* NNN does not look like a number */
  837. reiserfs_warning(s,
  838. "reiserfs_parse_options: bad value %s",
  839. arg);
  840. return 0;
  841. }
  842. }
  843. }
  844. if (c == 'c') {
  845. char *p = NULL;
  846. unsigned long val = simple_strtoul(arg, &p, 0);
  847. /* commit=NNN (time in seconds) */
  848. if (*p != '\0' || val >= (unsigned int)-1) {
  849. reiserfs_warning(s,
  850. "reiserfs_parse_options: bad value %s",
  851. arg);
  852. return 0;
  853. }
  854. *commit_max_age = (unsigned int)val;
  855. }
  856. if (c == 'w') {
  857. reiserfs_warning(s, "reiserfs: nolargeio option is no longer supported");
  858. return 0;
  859. }
  860. if (c == 'j') {
  861. if (arg && *arg && jdev_name) {
  862. if (*jdev_name) { //Hm, already assigned?
  863. reiserfs_warning(s,
  864. "reiserfs_parse_options: journal device was already specified to be %s",
  865. *jdev_name);
  866. return 0;
  867. }
  868. *jdev_name = arg;
  869. }
  870. }
  871. #ifdef CONFIG_QUOTA
  872. if (c == 'u' || c == 'g') {
  873. int qtype = c == 'u' ? USRQUOTA : GRPQUOTA;
  874. if (sb_any_quota_enabled(s)) {
  875. reiserfs_warning(s,
  876. "reiserfs_parse_options: cannot change journalled quota options when quota turned on.");
  877. return 0;
  878. }
  879. if (*arg) { /* Some filename specified? */
  880. if (REISERFS_SB(s)->s_qf_names[qtype]
  881. && strcmp(REISERFS_SB(s)->s_qf_names[qtype],
  882. arg)) {
  883. reiserfs_warning(s,
  884. "reiserfs_parse_options: %s quota file already specified.",
  885. QTYPE2NAME(qtype));
  886. return 0;
  887. }
  888. if (strchr(arg, '/')) {
  889. reiserfs_warning(s,
  890. "reiserfs_parse_options: quotafile must be on filesystem root.");
  891. return 0;
  892. }
  893. REISERFS_SB(s)->s_qf_names[qtype] =
  894. kmalloc(strlen(arg) + 1, GFP_KERNEL);
  895. if (!REISERFS_SB(s)->s_qf_names[qtype]) {
  896. reiserfs_warning(s,
  897. "reiserfs_parse_options: not enough memory for storing quotafile name.");
  898. return 0;
  899. }
  900. strcpy(REISERFS_SB(s)->s_qf_names[qtype], arg);
  901. *mount_options |= 1 << REISERFS_QUOTA;
  902. } else {
  903. kfree(REISERFS_SB(s)->s_qf_names[qtype]);
  904. REISERFS_SB(s)->s_qf_names[qtype] = NULL;
  905. }
  906. }
  907. if (c == 'f') {
  908. if (!strcmp(arg, "vfsold"))
  909. REISERFS_SB(s)->s_jquota_fmt = QFMT_VFS_OLD;
  910. else if (!strcmp(arg, "vfsv0"))
  911. REISERFS_SB(s)->s_jquota_fmt = QFMT_VFS_V0;
  912. else {
  913. reiserfs_warning(s,
  914. "reiserfs_parse_options: unknown quota format specified.");
  915. return 0;
  916. }
  917. }
  918. #else
  919. if (c == 'u' || c == 'g' || c == 'f') {
  920. reiserfs_warning(s,
  921. "reiserfs_parse_options: journalled quota options not supported.");
  922. return 0;
  923. }
  924. #endif
  925. }
  926. #ifdef CONFIG_QUOTA
  927. if (!REISERFS_SB(s)->s_jquota_fmt
  928. && (REISERFS_SB(s)->s_qf_names[USRQUOTA]
  929. || REISERFS_SB(s)->s_qf_names[GRPQUOTA])) {
  930. reiserfs_warning(s,
  931. "reiserfs_parse_options: journalled quota format not specified.");
  932. return 0;
  933. }
  934. /* This checking is not precise wrt the quota type but for our purposes it is sufficient */
  935. if (!(*mount_options & (1 << REISERFS_QUOTA))
  936. && sb_any_quota_enabled(s)) {
  937. reiserfs_warning(s,
  938. "reiserfs_parse_options: quota options must be present when quota is turned on.");
  939. return 0;
  940. }
  941. #endif
  942. return 1;
  943. }
  944. static void switch_data_mode(struct super_block *s, unsigned long mode)
  945. {
  946. REISERFS_SB(s)->s_mount_opt &= ~((1 << REISERFS_DATA_LOG) |
  947. (1 << REISERFS_DATA_ORDERED) |
  948. (1 << REISERFS_DATA_WRITEBACK));
  949. REISERFS_SB(s)->s_mount_opt |= (1 << mode);
  950. }
  951. static void handle_data_mode(struct super_block *s, unsigned long mount_options)
  952. {
  953. if (mount_options & (1 << REISERFS_DATA_LOG)) {
  954. if (!reiserfs_data_log(s)) {
  955. switch_data_mode(s, REISERFS_DATA_LOG);
  956. reiserfs_info(s, "switching to journaled data mode\n");
  957. }
  958. } else if (mount_options & (1 << REISERFS_DATA_ORDERED)) {
  959. if (!reiserfs_data_ordered(s)) {
  960. switch_data_mode(s, REISERFS_DATA_ORDERED);
  961. reiserfs_info(s, "switching to ordered data mode\n");
  962. }
  963. } else if (mount_options & (1 << REISERFS_DATA_WRITEBACK)) {
  964. if (!reiserfs_data_writeback(s)) {
  965. switch_data_mode(s, REISERFS_DATA_WRITEBACK);
  966. reiserfs_info(s, "switching to writeback data mode\n");
  967. }
  968. }
  969. }
  970. static void handle_barrier_mode(struct super_block *s, unsigned long bits)
  971. {
  972. int flush = (1 << REISERFS_BARRIER_FLUSH);
  973. int none = (1 << REISERFS_BARRIER_NONE);
  974. int all_barrier = flush | none;
  975. if (bits & all_barrier) {
  976. REISERFS_SB(s)->s_mount_opt &= ~all_barrier;
  977. if (bits & flush) {
  978. REISERFS_SB(s)->s_mount_opt |= flush;
  979. printk("reiserfs: enabling write barrier flush mode\n");
  980. } else if (bits & none) {
  981. REISERFS_SB(s)->s_mount_opt |= none;
  982. printk("reiserfs: write barriers turned off\n");
  983. }
  984. }
  985. }
  986. static void handle_attrs(struct super_block *s)
  987. {
  988. struct reiserfs_super_block *rs = SB_DISK_SUPER_BLOCK(s);
  989. if (reiserfs_attrs(s)) {
  990. if (old_format_only(s)) {
  991. reiserfs_warning(s,
  992. "reiserfs: cannot support attributes on 3.5.x disk format");
  993. REISERFS_SB(s)->s_mount_opt &= ~(1 << REISERFS_ATTRS);
  994. return;
  995. }
  996. if (!(le32_to_cpu(rs->s_flags) & reiserfs_attrs_cleared)) {
  997. reiserfs_warning(s,
  998. "reiserfs: cannot support attributes until flag is set in super-block");
  999. REISERFS_SB(s)->s_mount_opt &= ~(1 << REISERFS_ATTRS);
  1000. }
  1001. }
  1002. }
  1003. static int reiserfs_remount(struct super_block *s, int *mount_flags, char *arg)
  1004. {
  1005. struct reiserfs_super_block *rs;
  1006. struct reiserfs_transaction_handle th;
  1007. unsigned long blocks;
  1008. unsigned long mount_options = REISERFS_SB(s)->s_mount_opt;
  1009. unsigned long safe_mask = 0;
  1010. unsigned int commit_max_age = (unsigned int)-1;
  1011. struct reiserfs_journal *journal = SB_JOURNAL(s);
  1012. int err;
  1013. #ifdef CONFIG_QUOTA
  1014. int i;
  1015. #endif
  1016. rs = SB_DISK_SUPER_BLOCK(s);
  1017. if (!reiserfs_parse_options
  1018. (s, arg, &mount_options, &blocks, NULL, &commit_max_age)) {
  1019. #ifdef CONFIG_QUOTA
  1020. for (i = 0; i < MAXQUOTAS; i++) {
  1021. kfree(REISERFS_SB(s)->s_qf_names[i]);
  1022. REISERFS_SB(s)->s_qf_names[i] = NULL;
  1023. }
  1024. #endif
  1025. return -EINVAL;
  1026. }
  1027. handle_attrs(s);
  1028. /* Add options that are safe here */
  1029. safe_mask |= 1 << REISERFS_SMALLTAIL;
  1030. safe_mask |= 1 << REISERFS_LARGETAIL;
  1031. safe_mask |= 1 << REISERFS_NO_BORDER;
  1032. safe_mask |= 1 << REISERFS_NO_UNHASHED_RELOCATION;
  1033. safe_mask |= 1 << REISERFS_HASHED_RELOCATION;
  1034. safe_mask |= 1 << REISERFS_TEST4;
  1035. safe_mask |= 1 << REISERFS_ATTRS;
  1036. safe_mask |= 1 << REISERFS_XATTRS_USER;
  1037. safe_mask |= 1 << REISERFS_POSIXACL;
  1038. safe_mask |= 1 << REISERFS_BARRIER_FLUSH;
  1039. safe_mask |= 1 << REISERFS_BARRIER_NONE;
  1040. safe_mask |= 1 << REISERFS_ERROR_RO;
  1041. safe_mask |= 1 << REISERFS_ERROR_CONTINUE;
  1042. safe_mask |= 1 << REISERFS_ERROR_PANIC;
  1043. safe_mask |= 1 << REISERFS_QUOTA;
  1044. /* Update the bitmask, taking care to keep
  1045. * the bits we're not allowed to change here */
  1046. REISERFS_SB(s)->s_mount_opt =
  1047. (REISERFS_SB(s)->
  1048. s_mount_opt & ~safe_mask) | (mount_options & safe_mask);
  1049. if (commit_max_age != 0 && commit_max_age != (unsigned int)-1) {
  1050. journal->j_max_commit_age = commit_max_age;
  1051. journal->j_max_trans_age = commit_max_age;
  1052. } else if (commit_max_age == 0) {
  1053. /* 0 means restore defaults. */
  1054. journal->j_max_commit_age = journal->j_default_max_commit_age;
  1055. journal->j_max_trans_age = JOURNAL_MAX_TRANS_AGE;
  1056. }
  1057. if (blocks) {
  1058. int rc = reiserfs_resize(s, blocks);
  1059. if (rc != 0)
  1060. return rc;
  1061. }
  1062. if (*mount_flags & MS_RDONLY) {
  1063. reiserfs_xattr_init(s, *mount_flags);
  1064. /* remount read-only */
  1065. if (s->s_flags & MS_RDONLY)
  1066. /* it is read-only already */
  1067. return 0;
  1068. /* try to remount file system with read-only permissions */
  1069. if (sb_umount_state(rs) == REISERFS_VALID_FS
  1070. || REISERFS_SB(s)->s_mount_state != REISERFS_VALID_FS) {
  1071. return 0;
  1072. }
  1073. err = journal_begin(&th, s, 10);
  1074. if (err)
  1075. return err;
  1076. /* Mounting a rw partition read-only. */
  1077. reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s), 1);
  1078. set_sb_umount_state(rs, REISERFS_SB(s)->s_mount_state);
  1079. journal_mark_dirty(&th, s, SB_BUFFER_WITH_SB(s));
  1080. } else {
  1081. /* remount read-write */
  1082. if (!(s->s_flags & MS_RDONLY)) {
  1083. reiserfs_xattr_init(s, *mount_flags);
  1084. return 0; /* We are read-write already */
  1085. }
  1086. if (reiserfs_is_journal_aborted(journal))
  1087. return journal->j_errno;
  1088. handle_data_mode(s, mount_options);
  1089. handle_barrier_mode(s, mount_options);
  1090. REISERFS_SB(s)->s_mount_state = sb_umount_state(rs);
  1091. s->s_flags &= ~MS_RDONLY; /* now it is safe to call journal_begin */
  1092. err = journal_begin(&th, s, 10);
  1093. if (err)
  1094. return err;
  1095. /* Mount a partition which is read-only, read-write */
  1096. reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s), 1);
  1097. REISERFS_SB(s)->s_mount_state = sb_umount_state(rs);
  1098. s->s_flags &= ~MS_RDONLY;
  1099. set_sb_umount_state(rs, REISERFS_ERROR_FS);
  1100. /* mark_buffer_dirty (SB_BUFFER_WITH_SB (s), 1); */
  1101. journal_mark_dirty(&th, s, SB_BUFFER_WITH_SB(s));
  1102. REISERFS_SB(s)->s_mount_state = REISERFS_VALID_FS;
  1103. }
  1104. /* this will force a full flush of all journal lists */
  1105. SB_JOURNAL(s)->j_must_wait = 1;
  1106. err = journal_end(&th, s, 10);
  1107. if (err)
  1108. return err;
  1109. s->s_dirt = 0;
  1110. if (!(*mount_flags & MS_RDONLY)) {
  1111. finish_unfinished(s);
  1112. reiserfs_xattr_init(s, *mount_flags);
  1113. }
  1114. return 0;
  1115. }
  1116. static int read_super_block(struct super_block *s, int offset)
  1117. {
  1118. struct buffer_head *bh;
  1119. struct reiserfs_super_block *rs;
  1120. int fs_blocksize;
  1121. bh = sb_bread(s, offset / s->s_blocksize);
  1122. if (!bh) {
  1123. reiserfs_warning(s, "sh-2006: read_super_block: "
  1124. "bread failed (dev %s, block %lu, size %lu)",
  1125. reiserfs_bdevname(s), offset / s->s_blocksize,
  1126. s->s_blocksize);
  1127. return 1;
  1128. }
  1129. rs = (struct reiserfs_super_block *)bh->b_data;
  1130. if (!is_any_reiserfs_magic_string(rs)) {
  1131. brelse(bh);
  1132. return 1;
  1133. }
  1134. //
  1135. // ok, reiserfs signature (old or new) found in at the given offset
  1136. //
  1137. fs_blocksize = sb_blocksize(rs);
  1138. brelse(bh);
  1139. sb_set_blocksize(s, fs_blocksize);
  1140. bh = sb_bread(s, offset / s->s_blocksize);
  1141. if (!bh) {
  1142. reiserfs_warning(s, "sh-2007: read_super_block: "
  1143. "bread failed (dev %s, block %lu, size %lu)\n",
  1144. reiserfs_bdevname(s), offset / s->s_blocksize,
  1145. s->s_blocksize);
  1146. return 1;
  1147. }
  1148. rs = (struct reiserfs_super_block *)bh->b_data;
  1149. if (sb_blocksize(rs) != s->s_blocksize) {
  1150. reiserfs_warning(s, "sh-2011: read_super_block: "
  1151. "can't find a reiserfs filesystem on (dev %s, block %Lu, size %lu)\n",
  1152. reiserfs_bdevname(s),
  1153. (unsigned long long)bh->b_blocknr,
  1154. s->s_blocksize);
  1155. brelse(bh);
  1156. return 1;
  1157. }
  1158. if (rs->s_v1.s_root_block == cpu_to_le32(-1)) {
  1159. brelse(bh);
  1160. reiserfs_warning(s,
  1161. "Unfinished reiserfsck --rebuild-tree run detected. Please run\n"
  1162. "reiserfsck --rebuild-tree and wait for a completion. If that fails\n"
  1163. "get newer reiserfsprogs package");
  1164. return 1;
  1165. }
  1166. SB_BUFFER_WITH_SB(s) = bh;
  1167. SB_DISK_SUPER_BLOCK(s) = rs;
  1168. if (is_reiserfs_jr(rs)) {
  1169. /* magic is of non-standard journal filesystem, look at s_version to
  1170. find which format is in use */
  1171. if (sb_version(rs) == REISERFS_VERSION_2)
  1172. reiserfs_warning(s,
  1173. "read_super_block: found reiserfs format \"3.6\""
  1174. " with non-standard journal");
  1175. else if (sb_version(rs) == REISERFS_VERSION_1)
  1176. reiserfs_warning(s,
  1177. "read_super_block: found reiserfs format \"3.5\""
  1178. " with non-standard journal");
  1179. else {
  1180. reiserfs_warning(s,
  1181. "sh-2012: read_super_block: found unknown "
  1182. "format \"%u\" of reiserfs with non-standard magic",
  1183. sb_version(rs));
  1184. return 1;
  1185. }
  1186. } else
  1187. /* s_version of standard format may contain incorrect information,
  1188. so we just look at the magic string */
  1189. reiserfs_info(s,
  1190. "found reiserfs format \"%s\" with standard journal\n",
  1191. is_reiserfs_3_5(rs) ? "3.5" : "3.6");
  1192. s->s_op = &reiserfs_sops;
  1193. s->s_export_op = &reiserfs_export_ops;
  1194. #ifdef CONFIG_QUOTA
  1195. s->s_qcop = &reiserfs_qctl_operations;
  1196. s->dq_op = &reiserfs_quota_operations;
  1197. #endif
  1198. /* new format is limited by the 32 bit wide i_blocks field, want to
  1199. ** be one full block below that.
  1200. */
  1201. s->s_maxbytes = (512LL << 32) - s->s_blocksize;
  1202. return 0;
  1203. }
  1204. /* after journal replay, reread all bitmap and super blocks */
  1205. static int reread_meta_blocks(struct super_block *s)
  1206. {
  1207. ll_rw_block(READ, 1, &(SB_BUFFER_WITH_SB(s)));
  1208. wait_on_buffer(SB_BUFFER_WITH_SB(s));
  1209. if (!buffer_uptodate(SB_BUFFER_WITH_SB(s))) {
  1210. reiserfs_warning(s,
  1211. "reread_meta_blocks, error reading the super");
  1212. return 1;
  1213. }
  1214. return 0;
  1215. }
  1216. /////////////////////////////////////////////////////
  1217. // hash detection stuff
  1218. // if root directory is empty - we set default - Yura's - hash and
  1219. // warn about it
  1220. // FIXME: we look for only one name in a directory. If tea and yura
  1221. // bith have the same value - we ask user to send report to the
  1222. // mailing list
  1223. static __u32 find_hash_out(struct super_block *s)
  1224. {
  1225. int retval;
  1226. struct inode *inode;
  1227. struct cpu_key key;
  1228. INITIALIZE_PATH(path);
  1229. struct reiserfs_dir_entry de;
  1230. __u32 hash = DEFAULT_HASH;
  1231. inode = s->s_root->d_inode;
  1232. do { // Some serious "goto"-hater was there ;)
  1233. u32 teahash, r5hash, yurahash;
  1234. make_cpu_key(&key, inode, ~0, TYPE_DIRENTRY, 3);
  1235. retval = search_by_entry_key(s, &key, &path, &de);
  1236. if (retval == IO_ERROR) {
  1237. pathrelse(&path);
  1238. return UNSET_HASH;
  1239. }
  1240. if (retval == NAME_NOT_FOUND)
  1241. de.de_entry_num--;
  1242. set_de_name_and_namelen(&de);
  1243. if (deh_offset(&(de.de_deh[de.de_entry_num])) == DOT_DOT_OFFSET) {
  1244. /* allow override in this case */
  1245. if (reiserfs_rupasov_hash(s)) {
  1246. hash = YURA_HASH;
  1247. }
  1248. reiserfs_warning(s, "FS seems to be empty, autodetect "
  1249. "is using the default hash");
  1250. break;
  1251. }
  1252. r5hash = GET_HASH_VALUE(r5_hash(de.de_name, de.de_namelen));
  1253. teahash = GET_HASH_VALUE(keyed_hash(de.de_name, de.de_namelen));
  1254. yurahash = GET_HASH_VALUE(yura_hash(de.de_name, de.de_namelen));
  1255. if (((teahash == r5hash)
  1256. &&
  1257. (GET_HASH_VALUE(deh_offset(&(de.de_deh[de.de_entry_num])))
  1258. == r5hash)) || ((teahash == yurahash)
  1259. && (yurahash ==
  1260. GET_HASH_VALUE(deh_offset
  1261. (&
  1262. (de.
  1263. de_deh[de.
  1264. de_entry_num])))))
  1265. || ((r5hash == yurahash)
  1266. && (yurahash ==
  1267. GET_HASH_VALUE(deh_offset
  1268. (&(de.de_deh[de.de_entry_num])))))) {
  1269. reiserfs_warning(s,
  1270. "Unable to automatically detect hash function. "
  1271. "Please mount with -o hash={tea,rupasov,r5}",
  1272. reiserfs_bdevname(s));
  1273. hash = UNSET_HASH;
  1274. break;
  1275. }
  1276. if (GET_HASH_VALUE(deh_offset(&(de.de_deh[de.de_entry_num]))) ==
  1277. yurahash)
  1278. hash = YURA_HASH;
  1279. else if (GET_HASH_VALUE
  1280. (deh_offset(&(de.de_deh[de.de_entry_num]))) == teahash)
  1281. hash = TEA_HASH;
  1282. else if (GET_HASH_VALUE
  1283. (deh_offset(&(de.de_deh[de.de_entry_num]))) == r5hash)
  1284. hash = R5_HASH;
  1285. else {
  1286. reiserfs_warning(s, "Unrecognised hash function");
  1287. hash = UNSET_HASH;
  1288. }
  1289. } while (0);
  1290. pathrelse(&path);
  1291. return hash;
  1292. }
  1293. // finds out which hash names are sorted with
  1294. static int what_hash(struct super_block *s)
  1295. {
  1296. __u32 code;
  1297. code = sb_hash_function_code(SB_DISK_SUPER_BLOCK(s));
  1298. /* reiserfs_hash_detect() == true if any of the hash mount options
  1299. ** were used. We must check them to make sure the user isn't
  1300. ** using a bad hash value
  1301. */
  1302. if (code == UNSET_HASH || reiserfs_hash_detect(s))
  1303. code = find_hash_out(s);
  1304. if (code != UNSET_HASH && reiserfs_hash_detect(s)) {
  1305. /* detection has found the hash, and we must check against the
  1306. ** mount options
  1307. */
  1308. if (reiserfs_rupasov_hash(s) && code != YURA_HASH) {
  1309. reiserfs_warning(s, "Error, %s hash detected, "
  1310. "unable to force rupasov hash",
  1311. reiserfs_hashname(code));
  1312. code = UNSET_HASH;
  1313. } else if (reiserfs_tea_hash(s) && code != TEA_HASH) {
  1314. reiserfs_warning(s, "Error, %s hash detected, "
  1315. "unable to force tea hash",
  1316. reiserfs_hashname(code));
  1317. code = UNSET_HASH;
  1318. } else if (reiserfs_r5_hash(s) && code != R5_HASH) {
  1319. reiserfs_warning(s, "Error, %s hash detected, "
  1320. "unable to force r5 hash",
  1321. reiserfs_hashname(code));
  1322. code = UNSET_HASH;
  1323. }
  1324. } else {
  1325. /* find_hash_out was not called or could not determine the hash */
  1326. if (reiserfs_rupasov_hash(s)) {
  1327. code = YURA_HASH;
  1328. } else if (reiserfs_tea_hash(s)) {
  1329. code = TEA_HASH;
  1330. } else if (reiserfs_r5_hash(s)) {
  1331. code = R5_HASH;
  1332. }
  1333. }
  1334. /* if we are mounted RW, and we have a new valid hash code, update
  1335. ** the super
  1336. */
  1337. if (code != UNSET_HASH &&
  1338. !(s->s_flags & MS_RDONLY) &&
  1339. code != sb_hash_function_code(SB_DISK_SUPER_BLOCK(s))) {
  1340. set_sb_hash_function_code(SB_DISK_SUPER_BLOCK(s), code);
  1341. }
  1342. return code;
  1343. }
  1344. // return pointer to appropriate function
  1345. static hashf_t hash_function(struct super_block *s)
  1346. {
  1347. switch (what_hash(s)) {
  1348. case TEA_HASH:
  1349. reiserfs_info(s, "Using tea hash to sort names\n");
  1350. return keyed_hash;
  1351. case YURA_HASH:
  1352. reiserfs_info(s, "Using rupasov hash to sort names\n");
  1353. return yura_hash;
  1354. case R5_HASH:
  1355. reiserfs_info(s, "Using r5 hash to sort names\n");
  1356. return r5_hash;
  1357. }
  1358. return NULL;
  1359. }
  1360. // this is used to set up correct value for old partitions
  1361. static int function2code(hashf_t func)
  1362. {
  1363. if (func == keyed_hash)
  1364. return TEA_HASH;
  1365. if (func == yura_hash)
  1366. return YURA_HASH;
  1367. if (func == r5_hash)
  1368. return R5_HASH;
  1369. BUG(); // should never happen
  1370. return 0;
  1371. }
  1372. #define SWARN(silent, s, ...) \
  1373. if (!(silent)) \
  1374. reiserfs_warning (s, __VA_ARGS__)
  1375. static int reiserfs_fill_super(struct super_block *s, void *data, int silent)
  1376. {
  1377. struct inode *root_inode;
  1378. struct reiserfs_transaction_handle th;
  1379. int old_format = 0;
  1380. unsigned long blocks;
  1381. unsigned int commit_max_age = 0;
  1382. int jinit_done = 0;
  1383. struct reiserfs_iget_args args;
  1384. struct reiserfs_super_block *rs;
  1385. char *jdev_name;
  1386. struct reiserfs_sb_info *sbi;
  1387. int errval = -EINVAL;
  1388. sbi = kzalloc(sizeof(struct reiserfs_sb_info), GFP_KERNEL);
  1389. if (!sbi) {
  1390. errval = -ENOMEM;
  1391. goto error;
  1392. }
  1393. s->s_fs_info = sbi;
  1394. /* Set default values for options: non-aggressive tails, RO on errors */
  1395. REISERFS_SB(s)->s_mount_opt |= (1 << REISERFS_SMALLTAIL);
  1396. REISERFS_SB(s)->s_mount_opt |= (1 << REISERFS_ERROR_RO);
  1397. /* no preallocation minimum, be smart in
  1398. reiserfs_file_write instead */
  1399. REISERFS_SB(s)->s_alloc_options.preallocmin = 0;
  1400. /* Preallocate by 16 blocks (17-1) at once */
  1401. REISERFS_SB(s)->s_alloc_options.preallocsize = 17;
  1402. /* Initialize the rwsem for xattr dir */
  1403. init_rwsem(&REISERFS_SB(s)->xattr_dir_sem);
  1404. /* setup default block allocator options */
  1405. reiserfs_init_alloc_options(s);
  1406. jdev_name = NULL;
  1407. if (reiserfs_parse_options
  1408. (s, (char *)data, &(sbi->s_mount_opt), &blocks, &jdev_name,
  1409. &commit_max_age) == 0) {
  1410. goto error;
  1411. }
  1412. if (blocks) {
  1413. SWARN(silent, s, "jmacd-7: reiserfs_fill_super: resize option "
  1414. "for remount only");
  1415. goto error;
  1416. }
  1417. /* try old format (undistributed bitmap, super block in 8-th 1k block of a device) */
  1418. if (!read_super_block(s, REISERFS_OLD_DISK_OFFSET_IN_BYTES))
  1419. old_format = 1;
  1420. /* try new format (64-th 1k block), which can contain reiserfs super block */
  1421. else if (read_super_block(s, REISERFS_DISK_OFFSET_IN_BYTES)) {
  1422. SWARN(silent, s,
  1423. "sh-2021: reiserfs_fill_super: can not find reiserfs on %s",
  1424. reiserfs_bdevname(s));
  1425. goto error;
  1426. }
  1427. rs = SB_DISK_SUPER_BLOCK(s);
  1428. /* Let's do basic sanity check to verify that underlying device is not
  1429. smaller than the filesystem. If the check fails then abort and scream,
  1430. because bad stuff will happen otherwise. */
  1431. if (s->s_bdev && s->s_bdev->bd_inode
  1432. && i_size_read(s->s_bdev->bd_inode) <
  1433. sb_block_count(rs) * sb_blocksize(rs)) {
  1434. SWARN(silent, s,
  1435. "Filesystem on %s cannot be mounted because it is bigger than the device",
  1436. reiserfs_bdevname(s));
  1437. SWARN(silent, s,
  1438. "You may need to run fsck or increase size of your LVM partition");
  1439. SWARN(silent, s,
  1440. "Or may be you forgot to reboot after fdisk when it told you to");
  1441. goto error;
  1442. }
  1443. sbi->s_mount_state = SB_REISERFS_STATE(s);
  1444. sbi->s_mount_state = REISERFS_VALID_FS;
  1445. if ((errval = reiserfs_init_bitmap_cache(s))) {
  1446. SWARN(silent, s,
  1447. "jmacd-8: reiserfs_fill_super: unable to read bitmap");
  1448. goto error;
  1449. }
  1450. errval = -EINVAL;
  1451. #ifdef CONFIG_REISERFS_CHECK
  1452. SWARN(silent, s, "CONFIG_REISERFS_CHECK is set ON");
  1453. SWARN(silent, s, "- it is slow mode for debugging.");
  1454. #endif
  1455. /* make data=ordered the default */
  1456. if (!reiserfs_data_log(s) && !reiserfs_data_ordered(s) &&
  1457. !reiserfs_data_writeback(s)) {
  1458. REISERFS_SB(s)->s_mount_opt |= (1 << REISERFS_DATA_ORDERED);
  1459. }
  1460. if (reiserfs_data_log(s)) {
  1461. reiserfs_info(s, "using journaled data mode\n");
  1462. } else if (reiserfs_data_ordered(s)) {
  1463. reiserfs_info(s, "using ordered data mode\n");
  1464. } else {
  1465. reiserfs_info(s, "using writeback data mode\n");
  1466. }
  1467. if (reiserfs_barrier_flush(s)) {
  1468. printk("reiserfs: using flush barriers\n");
  1469. }
  1470. // set_device_ro(s->s_dev, 1) ;
  1471. if (journal_init(s, jdev_name, old_format, commit_max_age)) {
  1472. SWARN(silent, s,
  1473. "sh-2022: reiserfs_fill_super: unable to initialize journal space");
  1474. goto error;
  1475. } else {
  1476. jinit_done = 1; /* once this is set, journal_release must be called
  1477. ** if we error out of the mount
  1478. */
  1479. }
  1480. if (reread_meta_blocks(s)) {
  1481. SWARN(silent, s,
  1482. "jmacd-9: reiserfs_fill_super: unable to reread meta blocks after journal init");
  1483. goto error;
  1484. }
  1485. if (replay_only(s))
  1486. goto error;
  1487. if (bdev_read_only(s->s_bdev) && !(s->s_flags & MS_RDONLY)) {
  1488. SWARN(silent, s,
  1489. "clm-7000: Detected readonly device, marking FS readonly");
  1490. s->s_flags |= MS_RDONLY;
  1491. }
  1492. args.objectid = REISERFS_ROOT_OBJECTID;
  1493. args.dirid = REISERFS_ROOT_PARENT_OBJECTID;
  1494. root_inode =
  1495. iget5_locked(s, REISERFS_ROOT_OBJECTID, reiserfs_find_actor,
  1496. reiserfs_init_locked_inode, (void *)(&args));
  1497. if (!root_inode) {
  1498. SWARN(silent, s,
  1499. "jmacd-10: reiserfs_fill_super: get root inode failed");
  1500. goto error;
  1501. }
  1502. if (root_inode->i_state & I_NEW) {
  1503. reiserfs_read_locked_inode(root_inode, &args);
  1504. unlock_new_inode(root_inode);
  1505. }
  1506. s->s_root = d_alloc_root(root_inode);
  1507. if (!s->s_root) {
  1508. iput(root_inode);
  1509. goto error;
  1510. }
  1511. // define and initialize hash function
  1512. sbi->s_hash_function = hash_function(s);
  1513. if (sbi->s_hash_function == NULL) {
  1514. dput(s->s_root);
  1515. s->s_root = NULL;
  1516. goto error;
  1517. }
  1518. if (is_reiserfs_3_5(rs)
  1519. || (is_reiserfs_jr(rs) && SB_VERSION(s) == REISERFS_VERSION_1))
  1520. set_bit(REISERFS_3_5, &(sbi->s_properties));
  1521. else if (old_format)
  1522. set_bit(REISERFS_OLD_FORMAT, &(sbi->s_properties));
  1523. else
  1524. set_bit(REISERFS_3_6, &(sbi->s_properties));
  1525. if (!(s->s_flags & MS_RDONLY)) {
  1526. errval = journal_begin(&th, s, 1);
  1527. if (errval) {
  1528. dput(s->s_root);
  1529. s->s_root = NULL;
  1530. goto error;
  1531. }
  1532. reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s), 1);
  1533. set_sb_umount_state(rs, REISERFS_ERROR_FS);
  1534. set_sb_fs_state(rs, 0);
  1535. if (old_format_only(s)) {
  1536. /* filesystem of format 3.5 either with standard or non-standard
  1537. journal */
  1538. if (convert_reiserfs(s)) {
  1539. /* and -o conv is given */
  1540. if (!silent)
  1541. reiserfs_info(s,
  1542. "converting 3.5 filesystem to the 3.6 format");
  1543. if (is_reiserfs_3_5(rs))
  1544. /* put magic string of 3.6 format. 2.2 will not be able to
  1545. mount this filesystem anymore */
  1546. memcpy(rs->s_v1.s_magic,
  1547. reiserfs_3_6_magic_string,
  1548. sizeof
  1549. (reiserfs_3_6_magic_string));
  1550. set_sb_version(rs, REISERFS_VERSION_2);
  1551. reiserfs_convert_objectid_map_v1(s);
  1552. set_bit(REISERFS_3_6, &(sbi->s_properties));
  1553. clear_bit(REISERFS_3_5, &(sbi->s_properties));
  1554. } else if (!silent) {
  1555. reiserfs_info(s, "using 3.5.x disk format\n");
  1556. }
  1557. }
  1558. journal_mark_dirty(&th, s, SB_BUFFER_WITH_SB(s));
  1559. errval = journal_end(&th, s, 1);
  1560. if (errval) {
  1561. dput(s->s_root);
  1562. s->s_root = NULL;
  1563. goto error;
  1564. }
  1565. if ((errval = reiserfs_xattr_init(s, s->s_flags))) {
  1566. dput(s->s_root);
  1567. s->s_root = NULL;
  1568. goto error;
  1569. }
  1570. /* look for files which were to be removed in previous session */
  1571. finish_unfinished(s);
  1572. } else {
  1573. if (old_format_only(s) && !silent) {
  1574. reiserfs_info(s, "using 3.5.x disk format\n");
  1575. }
  1576. if ((errval = reiserfs_xattr_init(s, s->s_flags))) {
  1577. dput(s->s_root);
  1578. s->s_root = NULL;
  1579. goto error;
  1580. }
  1581. }
  1582. // mark hash in super block: it could be unset. overwrite should be ok
  1583. set_sb_hash_function_code(rs, function2code(sbi->s_hash_function));
  1584. handle_attrs(s);
  1585. reiserfs_proc_info_init(s);
  1586. init_waitqueue_head(&(sbi->s_wait));
  1587. spin_lock_init(&sbi->bitmap_lock);
  1588. return (0);
  1589. error:
  1590. if (jinit_done) { /* kill the commit thread, free journal ram */
  1591. journal_release_error(NULL, s);
  1592. }
  1593. reiserfs_free_bitmap_cache(s);
  1594. if (SB_BUFFER_WITH_SB(s))
  1595. brelse(SB_BUFFER_WITH_SB(s));
  1596. #ifdef CONFIG_QUOTA
  1597. {
  1598. int j;
  1599. for (j = 0; j < MAXQUOTAS; j++) {
  1600. kfree(sbi->s_qf_names[j]);
  1601. sbi->s_qf_names[j] = NULL;
  1602. }
  1603. }
  1604. #endif
  1605. kfree(sbi);
  1606. s->s_fs_info = NULL;
  1607. return errval;
  1608. }
  1609. static int reiserfs_statfs(struct dentry *dentry, struct kstatfs *buf)
  1610. {
  1611. struct reiserfs_super_block *rs = SB_DISK_SUPER_BLOCK(dentry->d_sb);
  1612. buf->f_namelen = (REISERFS_MAX_NAME(s->s_blocksize));
  1613. buf->f_bfree = sb_free_blocks(rs);
  1614. buf->f_bavail = buf->f_bfree;
  1615. buf->f_blocks = sb_block_count(rs) - sb_bmap_nr(rs) - 1;
  1616. buf->f_bsize = dentry->d_sb->s_blocksize;
  1617. /* changed to accommodate gcc folks. */
  1618. buf->f_type = REISERFS_SUPER_MAGIC;
  1619. return 0;
  1620. }
  1621. #ifdef CONFIG_QUOTA
  1622. static int reiserfs_dquot_initialize(struct inode *inode, int type)
  1623. {
  1624. struct reiserfs_transaction_handle th;
  1625. int ret, err;
  1626. /* We may create quota structure so we need to reserve enough blocks */
  1627. reiserfs_write_lock(inode->i_sb);
  1628. ret =
  1629. journal_begin(&th, inode->i_sb,
  1630. 2 * REISERFS_QUOTA_INIT_BLOCKS(inode->i_sb));
  1631. if (ret)
  1632. goto out;
  1633. ret = dquot_initialize(inode, type);
  1634. err =
  1635. journal_end(&th, inode->i_sb,
  1636. 2 * REISERFS_QUOTA_INIT_BLOCKS(inode->i_sb));
  1637. if (!ret && err)
  1638. ret = err;
  1639. out:
  1640. reiserfs_write_unlock(inode->i_sb);
  1641. return ret;
  1642. }
  1643. static int reiserfs_dquot_drop(struct inode *inode)
  1644. {
  1645. struct reiserfs_transaction_handle th;
  1646. int ret, err;
  1647. /* We may delete quota structure so we need to reserve enough blocks */
  1648. reiserfs_write_lock(inode->i_sb);
  1649. ret =
  1650. journal_begin(&th, inode->i_sb,
  1651. 2 * REISERFS_QUOTA_DEL_BLOCKS(inode->i_sb));
  1652. if (ret)
  1653. goto out;
  1654. ret = dquot_drop(inode);
  1655. err =
  1656. journal_end(&th, inode->i_sb,
  1657. 2 * REISERFS_QUOTA_DEL_BLOCKS(inode->i_sb));
  1658. if (!ret && err)
  1659. ret = err;
  1660. out:
  1661. reiserfs_write_unlock(inode->i_sb);
  1662. return ret;
  1663. }
  1664. static int reiserfs_write_dquot(struct dquot *dquot)
  1665. {
  1666. struct reiserfs_transaction_handle th;
  1667. int ret, err;
  1668. reiserfs_write_lock(dquot->dq_sb);
  1669. ret =
  1670. journal_begin(&th, dquot->dq_sb,
  1671. REISERFS_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
  1672. if (ret)
  1673. goto out;
  1674. ret = dquot_commit(dquot);
  1675. err =
  1676. journal_end(&th, dquot->dq_sb,
  1677. REISERFS_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
  1678. if (!ret && err)
  1679. ret = err;
  1680. out:
  1681. reiserfs_write_unlock(dquot->dq_sb);
  1682. return ret;
  1683. }
  1684. static int reiserfs_acquire_dquot(struct dquot *dquot)
  1685. {
  1686. struct reiserfs_transaction_handle th;
  1687. int ret, err;
  1688. reiserfs_write_lock(dquot->dq_sb);
  1689. ret =
  1690. journal_begin(&th, dquot->dq_sb,
  1691. REISERFS_QUOTA_INIT_BLOCKS(dquot->dq_sb));
  1692. if (ret)
  1693. goto out;
  1694. ret = dquot_acquire(dquot);
  1695. err =
  1696. journal_end(&th, dquot->dq_sb,
  1697. REISERFS_QUOTA_INIT_BLOCKS(dquot->dq_sb));
  1698. if (!ret && err)
  1699. ret = err;
  1700. out:
  1701. reiserfs_write_unlock(dquot->dq_sb);
  1702. return ret;
  1703. }
  1704. static int reiserfs_release_dquot(struct dquot *dquot)
  1705. {
  1706. struct reiserfs_transaction_handle th;
  1707. int ret, err;
  1708. reiserfs_write_lock(dquot->dq_sb);
  1709. ret =
  1710. journal_begin(&th, dquot->dq_sb,
  1711. REISERFS_QUOTA_DEL_BLOCKS(dquot->dq_sb));
  1712. if (ret)
  1713. goto out;
  1714. ret = dquot_release(dquot);
  1715. err =
  1716. journal_end(&th, dquot->dq_sb,
  1717. REISERFS_QUOTA_DEL_BLOCKS(dquot->dq_sb));
  1718. if (!ret && err)
  1719. ret = err;
  1720. out:
  1721. reiserfs_write_unlock(dquot->dq_sb);
  1722. return ret;
  1723. }
  1724. static int reiserfs_mark_dquot_dirty(struct dquot *dquot)
  1725. {
  1726. /* Are we journalling quotas? */
  1727. if (REISERFS_SB(dquot->dq_sb)->s_qf_names[USRQUOTA] ||
  1728. REISERFS_SB(dquot->dq_sb)->s_qf_names[GRPQUOTA]) {
  1729. dquot_mark_dquot_dirty(dquot);
  1730. return reiserfs_write_dquot(dquot);
  1731. } else
  1732. return dquot_mark_dquot_dirty(dquot);
  1733. }
  1734. static int reiserfs_write_info(struct super_block *sb, int type)
  1735. {
  1736. struct reiserfs_transaction_handle th;
  1737. int ret, err;
  1738. /* Data block + inode block */
  1739. reiserfs_write_lock(sb);
  1740. ret = journal_begin(&th, sb, 2);
  1741. if (ret)
  1742. goto out;
  1743. ret = dquot_commit_info(sb, type);
  1744. err = journal_end(&th, sb, 2);
  1745. if (!ret && err)
  1746. ret = err;
  1747. out:
  1748. reiserfs_write_unlock(sb);
  1749. return ret;
  1750. }
  1751. /*
  1752. * Turn on quotas during mount time - we need to find the quota file and such...
  1753. */
  1754. static int reiserfs_quota_on_mount(struct super_block *sb, int type)
  1755. {
  1756. return vfs_quota_on_mount(sb, REISERFS_SB(sb)->s_qf_names[type],
  1757. REISERFS_SB(sb)->s_jquota_fmt, type);
  1758. }
  1759. /*
  1760. * Standard function to be called on quota_on
  1761. */
  1762. static int reiserfs_quota_on(struct super_block *sb, int type, int format_id,
  1763. char *path)
  1764. {
  1765. int err;
  1766. struct nameidata nd;
  1767. if (!(REISERFS_SB(sb)->s_mount_opt & (1 << REISERFS_QUOTA)))
  1768. return -EINVAL;
  1769. err = path_lookup(path, LOOKUP_FOLLOW, &nd);
  1770. if (err)
  1771. return err;
  1772. /* Quotafile not on the same filesystem? */
  1773. if (nd.mnt->mnt_sb != sb) {
  1774. path_release(&nd);
  1775. return -EXDEV;
  1776. }
  1777. /* We must not pack tails for quota files on reiserfs for quota IO to work */
  1778. if (!REISERFS_I(nd.dentry->d_inode)->i_flags & i_nopack_mask) {
  1779. reiserfs_warning(sb,
  1780. "reiserfs: Quota file must have tail packing disabled.");
  1781. path_release(&nd);
  1782. return -EINVAL;
  1783. }
  1784. /* Not journalling quota? No more tests needed... */
  1785. if (!REISERFS_SB(sb)->s_qf_names[USRQUOTA] &&
  1786. !REISERFS_SB(sb)->s_qf_names[GRPQUOTA]) {
  1787. path_release(&nd);
  1788. return vfs_quota_on(sb, type, format_id, path);
  1789. }
  1790. /* Quotafile not of fs root? */
  1791. if (nd.dentry->d_parent->d_inode != sb->s_root->d_inode)
  1792. reiserfs_warning(sb,
  1793. "reiserfs: Quota file not on filesystem root. "
  1794. "Journalled quota will not work.");
  1795. path_release(&nd);
  1796. return vfs_quota_on(sb, type, format_id, path);
  1797. }
  1798. /* Read data from quotafile - avoid pagecache and such because we cannot afford
  1799. * acquiring the locks... As quota files are never truncated and quota code
  1800. * itself serializes the operations (and noone else should touch the files)
  1801. * we don't have to be afraid of races */
  1802. static ssize_t reiserfs_quota_read(struct super_block *sb, int type, char *data,
  1803. size_t len, loff_t off)
  1804. {
  1805. struct inode *inode = sb_dqopt(sb)->files[type];
  1806. unsigned long blk = off >> sb->s_blocksize_bits;
  1807. int err = 0, offset = off & (sb->s_blocksize - 1), tocopy;
  1808. size_t toread;
  1809. struct buffer_head tmp_bh, *bh;
  1810. loff_t i_size = i_size_read(inode);
  1811. if (off > i_size)
  1812. return 0;
  1813. if (off + len > i_size)
  1814. len = i_size - off;
  1815. toread = len;
  1816. while (toread > 0) {
  1817. tocopy =
  1818. sb->s_blocksize - offset <
  1819. toread ? sb->s_blocksize - offset : toread;
  1820. tmp_bh.b_state = 0;
  1821. /* Quota files are without tails so we can safely use this function */
  1822. reiserfs_write_lock(sb);
  1823. err = reiserfs_get_block(inode, blk, &tmp_bh, 0);
  1824. reiserfs_write_unlock(sb);
  1825. if (err)
  1826. return err;
  1827. if (!buffer_mapped(&tmp_bh)) /* A hole? */
  1828. memset(data, 0, tocopy);
  1829. else {
  1830. bh = sb_bread(sb, tmp_bh.b_blocknr);
  1831. if (!bh)
  1832. return -EIO;
  1833. memcpy(data, bh->b_data + offset, tocopy);
  1834. brelse(bh);
  1835. }
  1836. offset = 0;
  1837. toread -= tocopy;
  1838. data += tocopy;
  1839. blk++;
  1840. }
  1841. return len;
  1842. }
  1843. /* Write to quotafile (we know the transaction is already started and has
  1844. * enough credits) */
  1845. static ssize_t reiserfs_quota_write(struct super_block *sb, int type,
  1846. const char *data, size_t len, loff_t off)
  1847. {
  1848. struct inode *inode = sb_dqopt(sb)->files[type];
  1849. unsigned long blk = off >> sb->s_blocksize_bits;
  1850. int err = 0, offset = off & (sb->s_blocksize - 1), tocopy;
  1851. int journal_quota = REISERFS_SB(sb)->s_qf_names[type] != NULL;
  1852. size_t towrite = len;
  1853. struct buffer_head tmp_bh, *bh;
  1854. mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
  1855. while (towrite > 0) {
  1856. tocopy = sb->s_blocksize - offset < towrite ?
  1857. sb->s_blocksize - offset : towrite;
  1858. tmp_bh.b_state = 0;
  1859. err = reiserfs_get_block(inode, blk, &tmp_bh, GET_BLOCK_CREATE);
  1860. if (err)
  1861. goto out;
  1862. if (offset || tocopy != sb->s_blocksize)
  1863. bh = sb_bread(sb, tmp_bh.b_blocknr);
  1864. else
  1865. bh = sb_getblk(sb, tmp_bh.b_blocknr);
  1866. if (!bh) {
  1867. err = -EIO;
  1868. goto out;
  1869. }
  1870. lock_buffer(bh);
  1871. memcpy(bh->b_data + offset, data, tocopy);
  1872. flush_dcache_page(bh->b_page);
  1873. set_buffer_uptodate(bh);
  1874. unlock_buffer(bh);
  1875. reiserfs_prepare_for_journal(sb, bh, 1);
  1876. journal_mark_dirty(current->journal_info, sb, bh);
  1877. if (!journal_quota)
  1878. reiserfs_add_ordered_list(inode, bh);
  1879. brelse(bh);
  1880. offset = 0;
  1881. towrite -= tocopy;
  1882. data += tocopy;
  1883. blk++;
  1884. }
  1885. out:
  1886. if (len == towrite)
  1887. return err;
  1888. if (inode->i_size < off + len - towrite)
  1889. i_size_write(inode, off + len - towrite);
  1890. inode->i_version++;
  1891. inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  1892. mark_inode_dirty(inode);
  1893. mutex_unlock(&inode->i_mutex);
  1894. return len - towrite;
  1895. }
  1896. #endif
  1897. static int get_super_block(struct file_system_type *fs_type,
  1898. int flags, const char *dev_name,
  1899. void *data, struct vfsmount *mnt)
  1900. {
  1901. return get_sb_bdev(fs_type, flags, dev_name, data, reiserfs_fill_super,
  1902. mnt);
  1903. }
  1904. static int __init init_reiserfs_fs(void)
  1905. {
  1906. int ret;
  1907. if ((ret = init_inodecache())) {
  1908. return ret;
  1909. }
  1910. if ((ret = reiserfs_xattr_register_handlers()))
  1911. goto failed_reiserfs_xattr_register_handlers;
  1912. reiserfs_proc_info_global_init();
  1913. reiserfs_proc_register_global("version",
  1914. reiserfs_global_version_in_proc);
  1915. ret = register_filesystem(&reiserfs_fs_type);
  1916. if (ret == 0) {
  1917. return 0;
  1918. }
  1919. reiserfs_xattr_unregister_handlers();
  1920. failed_reiserfs_xattr_register_handlers:
  1921. reiserfs_proc_unregister_global("version");
  1922. reiserfs_proc_info_global_done();
  1923. destroy_inodecache();
  1924. return ret;
  1925. }
  1926. static void __exit exit_reiserfs_fs(void)
  1927. {
  1928. reiserfs_xattr_unregister_handlers();
  1929. reiserfs_proc_unregister_global("version");
  1930. reiserfs_proc_info_global_done();
  1931. unregister_filesystem(&reiserfs_fs_type);
  1932. destroy_inodecache();
  1933. }
  1934. struct file_system_type reiserfs_fs_type = {
  1935. .owner = THIS_MODULE,
  1936. .name = "reiserfs",
  1937. .get_sb = get_super_block,
  1938. .kill_sb = reiserfs_kill_sb,
  1939. .fs_flags = FS_REQUIRES_DEV,
  1940. };
  1941. MODULE_DESCRIPTION("ReiserFS journaled filesystem");
  1942. MODULE_AUTHOR("Hans Reiser <reiser@namesys.com>");
  1943. MODULE_LICENSE("GPL");
  1944. module_init(init_reiserfs_fs);
  1945. module_exit(exit_reiserfs_fs);