super.c 68 KB

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