super.c 68 KB

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