super.c 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564
  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 (!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. init_once);
  548. if (reiserfs_inode_cachep == NULL)
  549. return -ENOMEM;
  550. return 0;
  551. }
  552. static void destroy_inodecache(void)
  553. {
  554. /*
  555. * Make sure all delayed rcu free inodes are flushed before we
  556. * destroy cache.
  557. */
  558. rcu_barrier();
  559. kmem_cache_destroy(reiserfs_inode_cachep);
  560. }
  561. /* we don't mark inodes dirty, we just log them */
  562. static void reiserfs_dirty_inode(struct inode *inode, int flags)
  563. {
  564. struct reiserfs_transaction_handle th;
  565. int err = 0;
  566. if (inode->i_sb->s_flags & MS_RDONLY) {
  567. reiserfs_warning(inode->i_sb, "clm-6006",
  568. "writing inode %lu on readonly FS",
  569. inode->i_ino);
  570. return;
  571. }
  572. reiserfs_write_lock(inode->i_sb);
  573. /*
  574. * this is really only used for atime updates, so they don't have
  575. * to be included in O_SYNC or fsync
  576. */
  577. err = journal_begin(&th, inode->i_sb, 1);
  578. if (err)
  579. goto out;
  580. reiserfs_update_sd(&th, inode);
  581. journal_end(&th);
  582. out:
  583. reiserfs_write_unlock(inode->i_sb);
  584. }
  585. static int reiserfs_show_options(struct seq_file *seq, struct dentry *root)
  586. {
  587. struct super_block *s = root->d_sb;
  588. struct reiserfs_journal *journal = SB_JOURNAL(s);
  589. long opts = REISERFS_SB(s)->s_mount_opt;
  590. if (opts & (1 << REISERFS_LARGETAIL))
  591. seq_puts(seq, ",tails=on");
  592. else if (!(opts & (1 << REISERFS_SMALLTAIL)))
  593. seq_puts(seq, ",notail");
  594. /* tails=small is default so we don't show it */
  595. if (!(opts & (1 << REISERFS_BARRIER_FLUSH)))
  596. seq_puts(seq, ",barrier=none");
  597. /* barrier=flush is default so we don't show it */
  598. if (opts & (1 << REISERFS_ERROR_CONTINUE))
  599. seq_puts(seq, ",errors=continue");
  600. else if (opts & (1 << REISERFS_ERROR_PANIC))
  601. seq_puts(seq, ",errors=panic");
  602. /* errors=ro is default so we don't show it */
  603. if (opts & (1 << REISERFS_DATA_LOG))
  604. seq_puts(seq, ",data=journal");
  605. else if (opts & (1 << REISERFS_DATA_WRITEBACK))
  606. seq_puts(seq, ",data=writeback");
  607. /* data=ordered is default so we don't show it */
  608. if (opts & (1 << REISERFS_ATTRS))
  609. seq_puts(seq, ",attrs");
  610. if (opts & (1 << REISERFS_XATTRS_USER))
  611. seq_puts(seq, ",user_xattr");
  612. if (opts & (1 << REISERFS_EXPOSE_PRIVROOT))
  613. seq_puts(seq, ",expose_privroot");
  614. if (opts & (1 << REISERFS_POSIXACL))
  615. seq_puts(seq, ",acl");
  616. if (REISERFS_SB(s)->s_jdev)
  617. seq_printf(seq, ",jdev=%s", REISERFS_SB(s)->s_jdev);
  618. if (journal->j_max_commit_age != journal->j_default_max_commit_age)
  619. seq_printf(seq, ",commit=%d", journal->j_max_commit_age);
  620. #ifdef CONFIG_QUOTA
  621. if (REISERFS_SB(s)->s_qf_names[USRQUOTA])
  622. seq_printf(seq, ",usrjquota=%s", REISERFS_SB(s)->s_qf_names[USRQUOTA]);
  623. else if (opts & (1 << REISERFS_USRQUOTA))
  624. seq_puts(seq, ",usrquota");
  625. if (REISERFS_SB(s)->s_qf_names[GRPQUOTA])
  626. seq_printf(seq, ",grpjquota=%s", REISERFS_SB(s)->s_qf_names[GRPQUOTA]);
  627. else if (opts & (1 << REISERFS_GRPQUOTA))
  628. seq_puts(seq, ",grpquota");
  629. if (REISERFS_SB(s)->s_jquota_fmt) {
  630. if (REISERFS_SB(s)->s_jquota_fmt == QFMT_VFS_OLD)
  631. seq_puts(seq, ",jqfmt=vfsold");
  632. else if (REISERFS_SB(s)->s_jquota_fmt == QFMT_VFS_V0)
  633. seq_puts(seq, ",jqfmt=vfsv0");
  634. }
  635. #endif
  636. /* Block allocator options */
  637. if (opts & (1 << REISERFS_NO_BORDER))
  638. seq_puts(seq, ",block-allocator=noborder");
  639. if (opts & (1 << REISERFS_NO_UNHASHED_RELOCATION))
  640. seq_puts(seq, ",block-allocator=no_unhashed_relocation");
  641. if (opts & (1 << REISERFS_HASHED_RELOCATION))
  642. seq_puts(seq, ",block-allocator=hashed_relocation");
  643. if (opts & (1 << REISERFS_TEST4))
  644. seq_puts(seq, ",block-allocator=test4");
  645. show_alloc_options(seq, s);
  646. return 0;
  647. }
  648. #ifdef CONFIG_QUOTA
  649. static ssize_t reiserfs_quota_write(struct super_block *, int, const char *,
  650. size_t, loff_t);
  651. static ssize_t reiserfs_quota_read(struct super_block *, int, char *, size_t,
  652. loff_t);
  653. static struct dquot **reiserfs_get_dquots(struct inode *inode)
  654. {
  655. return REISERFS_I(inode)->i_dquot;
  656. }
  657. #endif
  658. static const struct super_operations reiserfs_sops = {
  659. .alloc_inode = reiserfs_alloc_inode,
  660. .destroy_inode = reiserfs_destroy_inode,
  661. .write_inode = reiserfs_write_inode,
  662. .dirty_inode = reiserfs_dirty_inode,
  663. .evict_inode = reiserfs_evict_inode,
  664. .put_super = reiserfs_put_super,
  665. .sync_fs = reiserfs_sync_fs,
  666. .freeze_fs = reiserfs_freeze,
  667. .unfreeze_fs = reiserfs_unfreeze,
  668. .statfs = reiserfs_statfs,
  669. .remount_fs = reiserfs_remount,
  670. .show_options = reiserfs_show_options,
  671. #ifdef CONFIG_QUOTA
  672. .quota_read = reiserfs_quota_read,
  673. .quota_write = reiserfs_quota_write,
  674. .get_dquots = reiserfs_get_dquots,
  675. #endif
  676. };
  677. #ifdef CONFIG_QUOTA
  678. #define QTYPE2NAME(t) ((t)==USRQUOTA?"user":"group")
  679. static int reiserfs_write_dquot(struct dquot *);
  680. static int reiserfs_acquire_dquot(struct dquot *);
  681. static int reiserfs_release_dquot(struct dquot *);
  682. static int reiserfs_mark_dquot_dirty(struct dquot *);
  683. static int reiserfs_write_info(struct super_block *, int);
  684. static int reiserfs_quota_on(struct super_block *, int, int, struct path *);
  685. static const struct dquot_operations reiserfs_quota_operations = {
  686. .write_dquot = reiserfs_write_dquot,
  687. .acquire_dquot = reiserfs_acquire_dquot,
  688. .release_dquot = reiserfs_release_dquot,
  689. .mark_dirty = reiserfs_mark_dquot_dirty,
  690. .write_info = reiserfs_write_info,
  691. .alloc_dquot = dquot_alloc,
  692. .destroy_dquot = dquot_destroy,
  693. };
  694. static const struct quotactl_ops reiserfs_qctl_operations = {
  695. .quota_on = reiserfs_quota_on,
  696. .quota_off = dquot_quota_off,
  697. .quota_sync = dquot_quota_sync,
  698. .get_state = dquot_get_state,
  699. .set_info = dquot_set_dqinfo,
  700. .get_dqblk = dquot_get_dqblk,
  701. .set_dqblk = dquot_set_dqblk,
  702. };
  703. #endif
  704. static const struct export_operations reiserfs_export_ops = {
  705. .encode_fh = reiserfs_encode_fh,
  706. .fh_to_dentry = reiserfs_fh_to_dentry,
  707. .fh_to_parent = reiserfs_fh_to_parent,
  708. .get_parent = reiserfs_get_parent,
  709. };
  710. /*
  711. * this struct is used in reiserfs_getopt () for containing the value for
  712. * those mount options that have values rather than being toggles.
  713. */
  714. typedef struct {
  715. char *value;
  716. /*
  717. * bitmask which is to set on mount_options bitmask
  718. * when this value is found, 0 is no bits are to be changed.
  719. */
  720. int setmask;
  721. /*
  722. * bitmask which is to clear on mount_options bitmask
  723. * when this value is found, 0 is no bits are to be changed.
  724. * This is applied BEFORE setmask
  725. */
  726. int clrmask;
  727. } arg_desc_t;
  728. /* Set this bit in arg_required to allow empty arguments */
  729. #define REISERFS_OPT_ALLOWEMPTY 31
  730. /*
  731. * this struct is used in reiserfs_getopt() for describing the
  732. * set of reiserfs mount options
  733. */
  734. typedef struct {
  735. char *option_name;
  736. /* 0 if argument is not required, not 0 otherwise */
  737. int arg_required;
  738. /* list of values accepted by an option */
  739. const arg_desc_t *values;
  740. /*
  741. * bitmask which is to set on mount_options bitmask
  742. * when this value is found, 0 is no bits are to be changed.
  743. */
  744. int setmask;
  745. /*
  746. * bitmask which is to clear on mount_options bitmask
  747. * when this value is found, 0 is no bits are to be changed.
  748. * This is applied BEFORE setmask
  749. */
  750. int clrmask;
  751. } opt_desc_t;
  752. /* possible values for -o data= */
  753. static const arg_desc_t logging_mode[] = {
  754. {"ordered", 1 << REISERFS_DATA_ORDERED,
  755. (1 << REISERFS_DATA_LOG | 1 << REISERFS_DATA_WRITEBACK)},
  756. {"journal", 1 << REISERFS_DATA_LOG,
  757. (1 << REISERFS_DATA_ORDERED | 1 << REISERFS_DATA_WRITEBACK)},
  758. {"writeback", 1 << REISERFS_DATA_WRITEBACK,
  759. (1 << REISERFS_DATA_ORDERED | 1 << REISERFS_DATA_LOG)},
  760. {.value = NULL}
  761. };
  762. /* possible values for -o barrier= */
  763. static const arg_desc_t barrier_mode[] = {
  764. {"none", 1 << REISERFS_BARRIER_NONE, 1 << REISERFS_BARRIER_FLUSH},
  765. {"flush", 1 << REISERFS_BARRIER_FLUSH, 1 << REISERFS_BARRIER_NONE},
  766. {.value = NULL}
  767. };
  768. /*
  769. * possible values for "-o block-allocator=" and bits which are to be set in
  770. * s_mount_opt of reiserfs specific part of in-core super block
  771. */
  772. static const arg_desc_t balloc[] = {
  773. {"noborder", 1 << REISERFS_NO_BORDER, 0},
  774. {"border", 0, 1 << REISERFS_NO_BORDER},
  775. {"no_unhashed_relocation", 1 << REISERFS_NO_UNHASHED_RELOCATION, 0},
  776. {"hashed_relocation", 1 << REISERFS_HASHED_RELOCATION, 0},
  777. {"test4", 1 << REISERFS_TEST4, 0},
  778. {"notest4", 0, 1 << REISERFS_TEST4},
  779. {NULL, 0, 0}
  780. };
  781. static const arg_desc_t tails[] = {
  782. {"on", 1 << REISERFS_LARGETAIL, 1 << REISERFS_SMALLTAIL},
  783. {"off", 0, (1 << REISERFS_LARGETAIL) | (1 << REISERFS_SMALLTAIL)},
  784. {"small", 1 << REISERFS_SMALLTAIL, 1 << REISERFS_LARGETAIL},
  785. {NULL, 0, 0}
  786. };
  787. static const arg_desc_t error_actions[] = {
  788. {"panic", 1 << REISERFS_ERROR_PANIC,
  789. (1 << REISERFS_ERROR_RO | 1 << REISERFS_ERROR_CONTINUE)},
  790. {"ro-remount", 1 << REISERFS_ERROR_RO,
  791. (1 << REISERFS_ERROR_PANIC | 1 << REISERFS_ERROR_CONTINUE)},
  792. #ifdef REISERFS_JOURNAL_ERROR_ALLOWS_NO_LOG
  793. {"continue", 1 << REISERFS_ERROR_CONTINUE,
  794. (1 << REISERFS_ERROR_PANIC | 1 << REISERFS_ERROR_RO)},
  795. #endif
  796. {NULL, 0, 0},
  797. };
  798. /*
  799. * proceed only one option from a list *cur - string containing of mount
  800. * options
  801. * opts - array of options which are accepted
  802. * opt_arg - if option is found and requires an argument and if it is specifed
  803. * in the input - pointer to the argument is stored here
  804. * bit_flags - if option requires to set a certain bit - it is set here
  805. * return -1 if unknown option is found, opt->arg_required otherwise
  806. */
  807. static int reiserfs_getopt(struct super_block *s, char **cur, opt_desc_t * opts,
  808. char **opt_arg, unsigned long *bit_flags)
  809. {
  810. char *p;
  811. /*
  812. * foo=bar,
  813. * ^ ^ ^
  814. * | | +-- option_end
  815. * | +-- arg_start
  816. * +-- option_start
  817. */
  818. const opt_desc_t *opt;
  819. const arg_desc_t *arg;
  820. p = *cur;
  821. /* assume argument cannot contain commas */
  822. *cur = strchr(p, ',');
  823. if (*cur) {
  824. *(*cur) = '\0';
  825. (*cur)++;
  826. }
  827. if (!strncmp(p, "alloc=", 6)) {
  828. /*
  829. * Ugly special case, probably we should redo options
  830. * parser so that it can understand several arguments for
  831. * some options, also so that it can fill several bitfields
  832. * with option values.
  833. */
  834. if (reiserfs_parse_alloc_options(s, p + 6)) {
  835. return -1;
  836. } else {
  837. return 0;
  838. }
  839. }
  840. /* for every option in the list */
  841. for (opt = opts; opt->option_name; opt++) {
  842. if (!strncmp(p, opt->option_name, strlen(opt->option_name))) {
  843. if (bit_flags) {
  844. if (opt->clrmask ==
  845. (1 << REISERFS_UNSUPPORTED_OPT))
  846. reiserfs_warning(s, "super-6500",
  847. "%s not supported.\n",
  848. p);
  849. else
  850. *bit_flags &= ~opt->clrmask;
  851. if (opt->setmask ==
  852. (1 << REISERFS_UNSUPPORTED_OPT))
  853. reiserfs_warning(s, "super-6501",
  854. "%s not supported.\n",
  855. p);
  856. else
  857. *bit_flags |= opt->setmask;
  858. }
  859. break;
  860. }
  861. }
  862. if (!opt->option_name) {
  863. reiserfs_warning(s, "super-6502",
  864. "unknown mount option \"%s\"", p);
  865. return -1;
  866. }
  867. p += strlen(opt->option_name);
  868. switch (*p) {
  869. case '=':
  870. if (!opt->arg_required) {
  871. reiserfs_warning(s, "super-6503",
  872. "the option \"%s\" does not "
  873. "require an argument\n",
  874. opt->option_name);
  875. return -1;
  876. }
  877. break;
  878. case 0:
  879. if (opt->arg_required) {
  880. reiserfs_warning(s, "super-6504",
  881. "the option \"%s\" requires an "
  882. "argument\n", opt->option_name);
  883. return -1;
  884. }
  885. break;
  886. default:
  887. reiserfs_warning(s, "super-6505",
  888. "head of option \"%s\" is only correct\n",
  889. opt->option_name);
  890. return -1;
  891. }
  892. /*
  893. * move to the argument, or to next option if argument is not
  894. * required
  895. */
  896. p++;
  897. if (opt->arg_required
  898. && !(opt->arg_required & (1 << REISERFS_OPT_ALLOWEMPTY))
  899. && !strlen(p)) {
  900. /* this catches "option=," if not allowed */
  901. reiserfs_warning(s, "super-6506",
  902. "empty argument for \"%s\"\n",
  903. opt->option_name);
  904. return -1;
  905. }
  906. if (!opt->values) {
  907. /* *=NULLopt_arg contains pointer to argument */
  908. *opt_arg = p;
  909. return opt->arg_required & ~(1 << REISERFS_OPT_ALLOWEMPTY);
  910. }
  911. /* values possible for this option are listed in opt->values */
  912. for (arg = opt->values; arg->value; arg++) {
  913. if (!strcmp(p, arg->value)) {
  914. if (bit_flags) {
  915. *bit_flags &= ~arg->clrmask;
  916. *bit_flags |= arg->setmask;
  917. }
  918. return opt->arg_required;
  919. }
  920. }
  921. reiserfs_warning(s, "super-6506",
  922. "bad value \"%s\" for option \"%s\"\n", p,
  923. opt->option_name);
  924. return -1;
  925. }
  926. /* returns 0 if something is wrong in option string, 1 - otherwise */
  927. static int reiserfs_parse_options(struct super_block *s,
  928. /* string given via mount's -o */
  929. char *options,
  930. /*
  931. * after the parsing phase, contains the
  932. * collection of bitflags defining what
  933. * mount options were selected.
  934. */
  935. unsigned long *mount_options,
  936. /* strtol-ed from NNN of resize=NNN */
  937. unsigned long *blocks,
  938. char **jdev_name,
  939. unsigned int *commit_max_age,
  940. char **qf_names,
  941. unsigned int *qfmt)
  942. {
  943. int c;
  944. char *arg = NULL;
  945. char *pos;
  946. opt_desc_t opts[] = {
  947. /*
  948. * Compatibility stuff, so that -o notail for old
  949. * setups still work
  950. */
  951. {"tails",.arg_required = 't',.values = tails},
  952. {"notail",.clrmask =
  953. (1 << REISERFS_LARGETAIL) | (1 << REISERFS_SMALLTAIL)},
  954. {"conv",.setmask = 1 << REISERFS_CONVERT},
  955. {"attrs",.setmask = 1 << REISERFS_ATTRS},
  956. {"noattrs",.clrmask = 1 << REISERFS_ATTRS},
  957. {"expose_privroot", .setmask = 1 << REISERFS_EXPOSE_PRIVROOT},
  958. #ifdef CONFIG_REISERFS_FS_XATTR
  959. {"user_xattr",.setmask = 1 << REISERFS_XATTRS_USER},
  960. {"nouser_xattr",.clrmask = 1 << REISERFS_XATTRS_USER},
  961. #else
  962. {"user_xattr",.setmask = 1 << REISERFS_UNSUPPORTED_OPT},
  963. {"nouser_xattr",.clrmask = 1 << REISERFS_UNSUPPORTED_OPT},
  964. #endif
  965. #ifdef CONFIG_REISERFS_FS_POSIX_ACL
  966. {"acl",.setmask = 1 << REISERFS_POSIXACL},
  967. {"noacl",.clrmask = 1 << REISERFS_POSIXACL},
  968. #else
  969. {"acl",.setmask = 1 << REISERFS_UNSUPPORTED_OPT},
  970. {"noacl",.clrmask = 1 << REISERFS_UNSUPPORTED_OPT},
  971. #endif
  972. {.option_name = "nolog"},
  973. {"replayonly",.setmask = 1 << REPLAYONLY},
  974. {"block-allocator",.arg_required = 'a',.values = balloc},
  975. {"data",.arg_required = 'd',.values = logging_mode},
  976. {"barrier",.arg_required = 'b',.values = barrier_mode},
  977. {"resize",.arg_required = 'r',.values = NULL},
  978. {"jdev",.arg_required = 'j',.values = NULL},
  979. {"nolargeio",.arg_required = 'w',.values = NULL},
  980. {"commit",.arg_required = 'c',.values = NULL},
  981. {"usrquota",.setmask = 1 << REISERFS_USRQUOTA},
  982. {"grpquota",.setmask = 1 << REISERFS_GRPQUOTA},
  983. {"noquota",.clrmask = 1 << REISERFS_USRQUOTA | 1 << REISERFS_GRPQUOTA},
  984. {"errors",.arg_required = 'e',.values = error_actions},
  985. {"usrjquota",.arg_required =
  986. 'u' | (1 << REISERFS_OPT_ALLOWEMPTY),.values = NULL},
  987. {"grpjquota",.arg_required =
  988. 'g' | (1 << REISERFS_OPT_ALLOWEMPTY),.values = NULL},
  989. {"jqfmt",.arg_required = 'f',.values = NULL},
  990. {.option_name = NULL}
  991. };
  992. *blocks = 0;
  993. if (!options || !*options)
  994. /*
  995. * use default configuration: create tails, journaling on, no
  996. * conversion to newest format
  997. */
  998. return 1;
  999. for (pos = options; pos;) {
  1000. c = reiserfs_getopt(s, &pos, opts, &arg, mount_options);
  1001. if (c == -1)
  1002. /* wrong option is given */
  1003. return 0;
  1004. if (c == 'r') {
  1005. char *p;
  1006. p = NULL;
  1007. /* "resize=NNN" or "resize=auto" */
  1008. if (!strcmp(arg, "auto")) {
  1009. /* From JFS code, to auto-get the size. */
  1010. *blocks =
  1011. s->s_bdev->bd_inode->i_size >> s->
  1012. s_blocksize_bits;
  1013. } else {
  1014. *blocks = simple_strtoul(arg, &p, 0);
  1015. if (*p != '\0') {
  1016. /* NNN does not look like a number */
  1017. reiserfs_warning(s, "super-6507",
  1018. "bad value %s for "
  1019. "-oresize\n", arg);
  1020. return 0;
  1021. }
  1022. }
  1023. }
  1024. if (c == 'c') {
  1025. char *p = NULL;
  1026. unsigned long val = simple_strtoul(arg, &p, 0);
  1027. /* commit=NNN (time in seconds) */
  1028. if (*p != '\0' || val >= (unsigned int)-1) {
  1029. reiserfs_warning(s, "super-6508",
  1030. "bad value %s for -ocommit\n",
  1031. arg);
  1032. return 0;
  1033. }
  1034. *commit_max_age = (unsigned int)val;
  1035. }
  1036. if (c == 'w') {
  1037. reiserfs_warning(s, "super-6509", "nolargeio option "
  1038. "is no longer supported");
  1039. return 0;
  1040. }
  1041. if (c == 'j') {
  1042. if (arg && *arg && jdev_name) {
  1043. /* Hm, already assigned? */
  1044. if (*jdev_name) {
  1045. reiserfs_warning(s, "super-6510",
  1046. "journal device was "
  1047. "already specified to "
  1048. "be %s", *jdev_name);
  1049. return 0;
  1050. }
  1051. *jdev_name = arg;
  1052. }
  1053. }
  1054. #ifdef CONFIG_QUOTA
  1055. if (c == 'u' || c == 'g') {
  1056. int qtype = c == 'u' ? USRQUOTA : GRPQUOTA;
  1057. if (sb_any_quota_loaded(s) &&
  1058. (!*arg != !REISERFS_SB(s)->s_qf_names[qtype])) {
  1059. reiserfs_warning(s, "super-6511",
  1060. "cannot change journaled "
  1061. "quota options when quota "
  1062. "turned on.");
  1063. return 0;
  1064. }
  1065. if (*arg) { /* Some filename specified? */
  1066. if (REISERFS_SB(s)->s_qf_names[qtype]
  1067. && strcmp(REISERFS_SB(s)->s_qf_names[qtype],
  1068. arg)) {
  1069. reiserfs_warning(s, "super-6512",
  1070. "%s quota file "
  1071. "already specified.",
  1072. QTYPE2NAME(qtype));
  1073. return 0;
  1074. }
  1075. if (strchr(arg, '/')) {
  1076. reiserfs_warning(s, "super-6513",
  1077. "quotafile must be "
  1078. "on filesystem root.");
  1079. return 0;
  1080. }
  1081. qf_names[qtype] = kstrdup(arg, GFP_KERNEL);
  1082. if (!qf_names[qtype]) {
  1083. reiserfs_warning(s, "reiserfs-2502",
  1084. "not enough memory "
  1085. "for storing "
  1086. "quotafile name.");
  1087. return 0;
  1088. }
  1089. if (qtype == USRQUOTA)
  1090. *mount_options |= 1 << REISERFS_USRQUOTA;
  1091. else
  1092. *mount_options |= 1 << REISERFS_GRPQUOTA;
  1093. } else {
  1094. if (qf_names[qtype] !=
  1095. REISERFS_SB(s)->s_qf_names[qtype])
  1096. kfree(qf_names[qtype]);
  1097. qf_names[qtype] = NULL;
  1098. if (qtype == USRQUOTA)
  1099. *mount_options &= ~(1 << REISERFS_USRQUOTA);
  1100. else
  1101. *mount_options &= ~(1 << REISERFS_GRPQUOTA);
  1102. }
  1103. }
  1104. if (c == 'f') {
  1105. if (!strcmp(arg, "vfsold"))
  1106. *qfmt = QFMT_VFS_OLD;
  1107. else if (!strcmp(arg, "vfsv0"))
  1108. *qfmt = QFMT_VFS_V0;
  1109. else {
  1110. reiserfs_warning(s, "super-6514",
  1111. "unknown quota format "
  1112. "specified.");
  1113. return 0;
  1114. }
  1115. if (sb_any_quota_loaded(s) &&
  1116. *qfmt != REISERFS_SB(s)->s_jquota_fmt) {
  1117. reiserfs_warning(s, "super-6515",
  1118. "cannot change journaled "
  1119. "quota options when quota "
  1120. "turned on.");
  1121. return 0;
  1122. }
  1123. }
  1124. #else
  1125. if (c == 'u' || c == 'g' || c == 'f') {
  1126. reiserfs_warning(s, "reiserfs-2503", "journaled "
  1127. "quota options not supported.");
  1128. return 0;
  1129. }
  1130. #endif
  1131. }
  1132. #ifdef CONFIG_QUOTA
  1133. if (!REISERFS_SB(s)->s_jquota_fmt && !*qfmt
  1134. && (qf_names[USRQUOTA] || qf_names[GRPQUOTA])) {
  1135. reiserfs_warning(s, "super-6515",
  1136. "journaled quota format not specified.");
  1137. return 0;
  1138. }
  1139. if ((!(*mount_options & (1 << REISERFS_USRQUOTA)) &&
  1140. sb_has_quota_loaded(s, USRQUOTA)) ||
  1141. (!(*mount_options & (1 << REISERFS_GRPQUOTA)) &&
  1142. sb_has_quota_loaded(s, GRPQUOTA))) {
  1143. reiserfs_warning(s, "super-6516", "quota options must "
  1144. "be present when quota is turned on.");
  1145. return 0;
  1146. }
  1147. #endif
  1148. return 1;
  1149. }
  1150. static void switch_data_mode(struct super_block *s, unsigned long mode)
  1151. {
  1152. REISERFS_SB(s)->s_mount_opt &= ~((1 << REISERFS_DATA_LOG) |
  1153. (1 << REISERFS_DATA_ORDERED) |
  1154. (1 << REISERFS_DATA_WRITEBACK));
  1155. REISERFS_SB(s)->s_mount_opt |= (1 << mode);
  1156. }
  1157. static void handle_data_mode(struct super_block *s, unsigned long mount_options)
  1158. {
  1159. if (mount_options & (1 << REISERFS_DATA_LOG)) {
  1160. if (!reiserfs_data_log(s)) {
  1161. switch_data_mode(s, REISERFS_DATA_LOG);
  1162. reiserfs_info(s, "switching to journaled data mode\n");
  1163. }
  1164. } else if (mount_options & (1 << REISERFS_DATA_ORDERED)) {
  1165. if (!reiserfs_data_ordered(s)) {
  1166. switch_data_mode(s, REISERFS_DATA_ORDERED);
  1167. reiserfs_info(s, "switching to ordered data mode\n");
  1168. }
  1169. } else if (mount_options & (1 << REISERFS_DATA_WRITEBACK)) {
  1170. if (!reiserfs_data_writeback(s)) {
  1171. switch_data_mode(s, REISERFS_DATA_WRITEBACK);
  1172. reiserfs_info(s, "switching to writeback data mode\n");
  1173. }
  1174. }
  1175. }
  1176. static void handle_barrier_mode(struct super_block *s, unsigned long bits)
  1177. {
  1178. int flush = (1 << REISERFS_BARRIER_FLUSH);
  1179. int none = (1 << REISERFS_BARRIER_NONE);
  1180. int all_barrier = flush | none;
  1181. if (bits & all_barrier) {
  1182. REISERFS_SB(s)->s_mount_opt &= ~all_barrier;
  1183. if (bits & flush) {
  1184. REISERFS_SB(s)->s_mount_opt |= flush;
  1185. printk("reiserfs: enabling write barrier flush mode\n");
  1186. } else if (bits & none) {
  1187. REISERFS_SB(s)->s_mount_opt |= none;
  1188. printk("reiserfs: write barriers turned off\n");
  1189. }
  1190. }
  1191. }
  1192. static void handle_attrs(struct super_block *s)
  1193. {
  1194. struct reiserfs_super_block *rs = SB_DISK_SUPER_BLOCK(s);
  1195. if (reiserfs_attrs(s)) {
  1196. if (old_format_only(s)) {
  1197. reiserfs_warning(s, "super-6517", "cannot support "
  1198. "attributes on 3.5.x disk format");
  1199. REISERFS_SB(s)->s_mount_opt &= ~(1 << REISERFS_ATTRS);
  1200. return;
  1201. }
  1202. if (!(le32_to_cpu(rs->s_flags) & reiserfs_attrs_cleared)) {
  1203. reiserfs_warning(s, "super-6518", "cannot support "
  1204. "attributes until flag is set in "
  1205. "super-block");
  1206. REISERFS_SB(s)->s_mount_opt &= ~(1 << REISERFS_ATTRS);
  1207. }
  1208. }
  1209. }
  1210. #ifdef CONFIG_QUOTA
  1211. static void handle_quota_files(struct super_block *s, char **qf_names,
  1212. unsigned int *qfmt)
  1213. {
  1214. int i;
  1215. for (i = 0; i < REISERFS_MAXQUOTAS; i++) {
  1216. if (qf_names[i] != REISERFS_SB(s)->s_qf_names[i])
  1217. kfree(REISERFS_SB(s)->s_qf_names[i]);
  1218. REISERFS_SB(s)->s_qf_names[i] = qf_names[i];
  1219. }
  1220. if (*qfmt)
  1221. REISERFS_SB(s)->s_jquota_fmt = *qfmt;
  1222. }
  1223. #endif
  1224. static int reiserfs_remount(struct super_block *s, int *mount_flags, char *arg)
  1225. {
  1226. struct reiserfs_super_block *rs;
  1227. struct reiserfs_transaction_handle th;
  1228. unsigned long blocks;
  1229. unsigned long mount_options = REISERFS_SB(s)->s_mount_opt;
  1230. unsigned long safe_mask = 0;
  1231. unsigned int commit_max_age = (unsigned int)-1;
  1232. struct reiserfs_journal *journal = SB_JOURNAL(s);
  1233. char *new_opts = kstrdup(arg, GFP_KERNEL);
  1234. int err;
  1235. char *qf_names[REISERFS_MAXQUOTAS];
  1236. unsigned int qfmt = 0;
  1237. #ifdef CONFIG_QUOTA
  1238. int i;
  1239. #endif
  1240. sync_filesystem(s);
  1241. reiserfs_write_lock(s);
  1242. #ifdef CONFIG_QUOTA
  1243. memcpy(qf_names, REISERFS_SB(s)->s_qf_names, sizeof(qf_names));
  1244. #endif
  1245. rs = SB_DISK_SUPER_BLOCK(s);
  1246. if (!reiserfs_parse_options
  1247. (s, arg, &mount_options, &blocks, NULL, &commit_max_age,
  1248. qf_names, &qfmt)) {
  1249. #ifdef CONFIG_QUOTA
  1250. for (i = 0; i < REISERFS_MAXQUOTAS; i++)
  1251. if (qf_names[i] != REISERFS_SB(s)->s_qf_names[i])
  1252. kfree(qf_names[i]);
  1253. #endif
  1254. err = -EINVAL;
  1255. goto out_err_unlock;
  1256. }
  1257. #ifdef CONFIG_QUOTA
  1258. handle_quota_files(s, qf_names, &qfmt);
  1259. #endif
  1260. handle_attrs(s);
  1261. /* Add options that are safe here */
  1262. safe_mask |= 1 << REISERFS_SMALLTAIL;
  1263. safe_mask |= 1 << REISERFS_LARGETAIL;
  1264. safe_mask |= 1 << REISERFS_NO_BORDER;
  1265. safe_mask |= 1 << REISERFS_NO_UNHASHED_RELOCATION;
  1266. safe_mask |= 1 << REISERFS_HASHED_RELOCATION;
  1267. safe_mask |= 1 << REISERFS_TEST4;
  1268. safe_mask |= 1 << REISERFS_ATTRS;
  1269. safe_mask |= 1 << REISERFS_XATTRS_USER;
  1270. safe_mask |= 1 << REISERFS_POSIXACL;
  1271. safe_mask |= 1 << REISERFS_BARRIER_FLUSH;
  1272. safe_mask |= 1 << REISERFS_BARRIER_NONE;
  1273. safe_mask |= 1 << REISERFS_ERROR_RO;
  1274. safe_mask |= 1 << REISERFS_ERROR_CONTINUE;
  1275. safe_mask |= 1 << REISERFS_ERROR_PANIC;
  1276. safe_mask |= 1 << REISERFS_USRQUOTA;
  1277. safe_mask |= 1 << REISERFS_GRPQUOTA;
  1278. /*
  1279. * Update the bitmask, taking care to keep
  1280. * the bits we're not allowed to change here
  1281. */
  1282. REISERFS_SB(s)->s_mount_opt =
  1283. (REISERFS_SB(s)->
  1284. s_mount_opt & ~safe_mask) | (mount_options & safe_mask);
  1285. if (commit_max_age != 0 && commit_max_age != (unsigned int)-1) {
  1286. journal->j_max_commit_age = commit_max_age;
  1287. journal->j_max_trans_age = commit_max_age;
  1288. } else if (commit_max_age == 0) {
  1289. /* 0 means restore defaults. */
  1290. journal->j_max_commit_age = journal->j_default_max_commit_age;
  1291. journal->j_max_trans_age = JOURNAL_MAX_TRANS_AGE;
  1292. }
  1293. if (blocks) {
  1294. err = reiserfs_resize(s, blocks);
  1295. if (err != 0)
  1296. goto out_err_unlock;
  1297. }
  1298. if (*mount_flags & MS_RDONLY) {
  1299. reiserfs_write_unlock(s);
  1300. reiserfs_xattr_init(s, *mount_flags);
  1301. /* remount read-only */
  1302. if (s->s_flags & MS_RDONLY)
  1303. /* it is read-only already */
  1304. goto out_ok_unlocked;
  1305. err = dquot_suspend(s, -1);
  1306. if (err < 0)
  1307. goto out_err;
  1308. /* try to remount file system with read-only permissions */
  1309. if (sb_umount_state(rs) == REISERFS_VALID_FS
  1310. || REISERFS_SB(s)->s_mount_state != REISERFS_VALID_FS) {
  1311. goto out_ok_unlocked;
  1312. }
  1313. reiserfs_write_lock(s);
  1314. err = journal_begin(&th, s, 10);
  1315. if (err)
  1316. goto out_err_unlock;
  1317. /* Mounting a rw partition read-only. */
  1318. reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s), 1);
  1319. set_sb_umount_state(rs, REISERFS_SB(s)->s_mount_state);
  1320. journal_mark_dirty(&th, SB_BUFFER_WITH_SB(s));
  1321. } else {
  1322. /* remount read-write */
  1323. if (!(s->s_flags & MS_RDONLY)) {
  1324. reiserfs_write_unlock(s);
  1325. reiserfs_xattr_init(s, *mount_flags);
  1326. goto out_ok_unlocked; /* We are read-write already */
  1327. }
  1328. if (reiserfs_is_journal_aborted(journal)) {
  1329. err = journal->j_errno;
  1330. goto out_err_unlock;
  1331. }
  1332. handle_data_mode(s, mount_options);
  1333. handle_barrier_mode(s, mount_options);
  1334. REISERFS_SB(s)->s_mount_state = sb_umount_state(rs);
  1335. /* now it is safe to call journal_begin */
  1336. s->s_flags &= ~MS_RDONLY;
  1337. err = journal_begin(&th, s, 10);
  1338. if (err)
  1339. goto out_err_unlock;
  1340. /* Mount a partition which is read-only, read-write */
  1341. reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s), 1);
  1342. REISERFS_SB(s)->s_mount_state = sb_umount_state(rs);
  1343. s->s_flags &= ~MS_RDONLY;
  1344. set_sb_umount_state(rs, REISERFS_ERROR_FS);
  1345. if (!old_format_only(s))
  1346. set_sb_mnt_count(rs, sb_mnt_count(rs) + 1);
  1347. /* mark_buffer_dirty (SB_BUFFER_WITH_SB (s), 1); */
  1348. journal_mark_dirty(&th, SB_BUFFER_WITH_SB(s));
  1349. REISERFS_SB(s)->s_mount_state = REISERFS_VALID_FS;
  1350. }
  1351. /* this will force a full flush of all journal lists */
  1352. SB_JOURNAL(s)->j_must_wait = 1;
  1353. err = journal_end(&th);
  1354. if (err)
  1355. goto out_err_unlock;
  1356. reiserfs_write_unlock(s);
  1357. if (!(*mount_flags & MS_RDONLY)) {
  1358. dquot_resume(s, -1);
  1359. reiserfs_write_lock(s);
  1360. finish_unfinished(s);
  1361. reiserfs_write_unlock(s);
  1362. reiserfs_xattr_init(s, *mount_flags);
  1363. }
  1364. out_ok_unlocked:
  1365. replace_mount_options(s, new_opts);
  1366. return 0;
  1367. out_err_unlock:
  1368. reiserfs_write_unlock(s);
  1369. out_err:
  1370. kfree(new_opts);
  1371. return err;
  1372. }
  1373. static int read_super_block(struct super_block *s, int offset)
  1374. {
  1375. struct buffer_head *bh;
  1376. struct reiserfs_super_block *rs;
  1377. int fs_blocksize;
  1378. bh = sb_bread(s, offset / s->s_blocksize);
  1379. if (!bh) {
  1380. reiserfs_warning(s, "sh-2006",
  1381. "bread failed (dev %s, block %lu, size %lu)",
  1382. s->s_id, offset / s->s_blocksize,
  1383. s->s_blocksize);
  1384. return 1;
  1385. }
  1386. rs = (struct reiserfs_super_block *)bh->b_data;
  1387. if (!is_any_reiserfs_magic_string(rs)) {
  1388. brelse(bh);
  1389. return 1;
  1390. }
  1391. /*
  1392. * ok, reiserfs signature (old or new) found in at the given offset
  1393. */
  1394. fs_blocksize = sb_blocksize(rs);
  1395. brelse(bh);
  1396. sb_set_blocksize(s, fs_blocksize);
  1397. bh = sb_bread(s, offset / s->s_blocksize);
  1398. if (!bh) {
  1399. reiserfs_warning(s, "sh-2007",
  1400. "bread failed (dev %s, block %lu, size %lu)",
  1401. s->s_id, offset / s->s_blocksize,
  1402. s->s_blocksize);
  1403. return 1;
  1404. }
  1405. rs = (struct reiserfs_super_block *)bh->b_data;
  1406. if (sb_blocksize(rs) != s->s_blocksize) {
  1407. reiserfs_warning(s, "sh-2011", "can't find a reiserfs "
  1408. "filesystem on (dev %s, block %llu, size %lu)",
  1409. s->s_id,
  1410. (unsigned long long)bh->b_blocknr,
  1411. s->s_blocksize);
  1412. brelse(bh);
  1413. return 1;
  1414. }
  1415. if (rs->s_v1.s_root_block == cpu_to_le32(-1)) {
  1416. brelse(bh);
  1417. reiserfs_warning(s, "super-6519", "Unfinished reiserfsck "
  1418. "--rebuild-tree run detected. Please run\n"
  1419. "reiserfsck --rebuild-tree and wait for a "
  1420. "completion. If that fails\n"
  1421. "get newer reiserfsprogs package");
  1422. return 1;
  1423. }
  1424. SB_BUFFER_WITH_SB(s) = bh;
  1425. SB_DISK_SUPER_BLOCK(s) = rs;
  1426. /*
  1427. * magic is of non-standard journal filesystem, look at s_version to
  1428. * find which format is in use
  1429. */
  1430. if (is_reiserfs_jr(rs)) {
  1431. if (sb_version(rs) == REISERFS_VERSION_2)
  1432. reiserfs_info(s, "found reiserfs format \"3.6\""
  1433. " with non-standard journal\n");
  1434. else if (sb_version(rs) == REISERFS_VERSION_1)
  1435. reiserfs_info(s, "found reiserfs format \"3.5\""
  1436. " with non-standard journal\n");
  1437. else {
  1438. reiserfs_warning(s, "sh-2012", "found unknown "
  1439. "format \"%u\" of reiserfs with "
  1440. "non-standard magic", sb_version(rs));
  1441. return 1;
  1442. }
  1443. } else
  1444. /*
  1445. * s_version of standard format may contain incorrect
  1446. * information, so we just look at the magic string
  1447. */
  1448. reiserfs_info(s,
  1449. "found reiserfs format \"%s\" with standard journal\n",
  1450. is_reiserfs_3_5(rs) ? "3.5" : "3.6");
  1451. s->s_op = &reiserfs_sops;
  1452. s->s_export_op = &reiserfs_export_ops;
  1453. #ifdef CONFIG_QUOTA
  1454. s->s_qcop = &reiserfs_qctl_operations;
  1455. s->dq_op = &reiserfs_quota_operations;
  1456. s->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP;
  1457. #endif
  1458. /*
  1459. * new format is limited by the 32 bit wide i_blocks field, want to
  1460. * be one full block below that.
  1461. */
  1462. s->s_maxbytes = (512LL << 32) - s->s_blocksize;
  1463. return 0;
  1464. }
  1465. /* after journal replay, reread all bitmap and super blocks */
  1466. static int reread_meta_blocks(struct super_block *s)
  1467. {
  1468. ll_rw_block(READ, 1, &SB_BUFFER_WITH_SB(s));
  1469. wait_on_buffer(SB_BUFFER_WITH_SB(s));
  1470. if (!buffer_uptodate(SB_BUFFER_WITH_SB(s))) {
  1471. reiserfs_warning(s, "reiserfs-2504", "error reading the super");
  1472. return 1;
  1473. }
  1474. return 0;
  1475. }
  1476. /* hash detection stuff */
  1477. /*
  1478. * if root directory is empty - we set default - Yura's - hash and
  1479. * warn about it
  1480. * FIXME: we look for only one name in a directory. If tea and yura
  1481. * both have the same value - we ask user to send report to the
  1482. * mailing list
  1483. */
  1484. static __u32 find_hash_out(struct super_block *s)
  1485. {
  1486. int retval;
  1487. struct inode *inode;
  1488. struct cpu_key key;
  1489. INITIALIZE_PATH(path);
  1490. struct reiserfs_dir_entry de;
  1491. struct reiserfs_de_head *deh;
  1492. __u32 hash = DEFAULT_HASH;
  1493. __u32 deh_hashval, teahash, r5hash, yurahash;
  1494. inode = d_inode(s->s_root);
  1495. make_cpu_key(&key, inode, ~0, TYPE_DIRENTRY, 3);
  1496. retval = search_by_entry_key(s, &key, &path, &de);
  1497. if (retval == IO_ERROR) {
  1498. pathrelse(&path);
  1499. return UNSET_HASH;
  1500. }
  1501. if (retval == NAME_NOT_FOUND)
  1502. de.de_entry_num--;
  1503. set_de_name_and_namelen(&de);
  1504. deh = de.de_deh + de.de_entry_num;
  1505. if (deh_offset(deh) == DOT_DOT_OFFSET) {
  1506. /* allow override in this case */
  1507. if (reiserfs_rupasov_hash(s))
  1508. hash = YURA_HASH;
  1509. reiserfs_info(s, "FS seems to be empty, autodetect is using the default hash\n");
  1510. goto out;
  1511. }
  1512. deh_hashval = GET_HASH_VALUE(deh_offset(deh));
  1513. r5hash = GET_HASH_VALUE(r5_hash(de.de_name, de.de_namelen));
  1514. teahash = GET_HASH_VALUE(keyed_hash(de.de_name, de.de_namelen));
  1515. yurahash = GET_HASH_VALUE(yura_hash(de.de_name, de.de_namelen));
  1516. if ((teahash == r5hash && deh_hashval == r5hash) ||
  1517. (teahash == yurahash && deh_hashval == yurahash) ||
  1518. (r5hash == yurahash && deh_hashval == yurahash)) {
  1519. reiserfs_warning(s, "reiserfs-2506",
  1520. "Unable to automatically detect hash "
  1521. "function. Please mount with -o "
  1522. "hash={tea,rupasov,r5}");
  1523. hash = UNSET_HASH;
  1524. goto out;
  1525. }
  1526. if (deh_hashval == yurahash)
  1527. hash = YURA_HASH;
  1528. else if (deh_hashval == teahash)
  1529. hash = TEA_HASH;
  1530. else if (deh_hashval == r5hash)
  1531. hash = R5_HASH;
  1532. else {
  1533. reiserfs_warning(s, "reiserfs-2506",
  1534. "Unrecognised hash function");
  1535. hash = UNSET_HASH;
  1536. }
  1537. out:
  1538. pathrelse(&path);
  1539. return hash;
  1540. }
  1541. /* finds out which hash names are sorted with */
  1542. static int what_hash(struct super_block *s)
  1543. {
  1544. __u32 code;
  1545. code = sb_hash_function_code(SB_DISK_SUPER_BLOCK(s));
  1546. /*
  1547. * reiserfs_hash_detect() == true if any of the hash mount options
  1548. * were used. We must check them to make sure the user isn't
  1549. * using a bad hash value
  1550. */
  1551. if (code == UNSET_HASH || reiserfs_hash_detect(s))
  1552. code = find_hash_out(s);
  1553. if (code != UNSET_HASH && reiserfs_hash_detect(s)) {
  1554. /*
  1555. * detection has found the hash, and we must check against the
  1556. * mount options
  1557. */
  1558. if (reiserfs_rupasov_hash(s) && code != YURA_HASH) {
  1559. reiserfs_warning(s, "reiserfs-2507",
  1560. "Error, %s hash detected, "
  1561. "unable to force rupasov hash",
  1562. reiserfs_hashname(code));
  1563. code = UNSET_HASH;
  1564. } else if (reiserfs_tea_hash(s) && code != TEA_HASH) {
  1565. reiserfs_warning(s, "reiserfs-2508",
  1566. "Error, %s hash detected, "
  1567. "unable to force tea hash",
  1568. reiserfs_hashname(code));
  1569. code = UNSET_HASH;
  1570. } else if (reiserfs_r5_hash(s) && code != R5_HASH) {
  1571. reiserfs_warning(s, "reiserfs-2509",
  1572. "Error, %s hash detected, "
  1573. "unable to force r5 hash",
  1574. reiserfs_hashname(code));
  1575. code = UNSET_HASH;
  1576. }
  1577. } else {
  1578. /*
  1579. * find_hash_out was not called or
  1580. * could not determine the hash
  1581. */
  1582. if (reiserfs_rupasov_hash(s)) {
  1583. code = YURA_HASH;
  1584. } else if (reiserfs_tea_hash(s)) {
  1585. code = TEA_HASH;
  1586. } else if (reiserfs_r5_hash(s)) {
  1587. code = R5_HASH;
  1588. }
  1589. }
  1590. /*
  1591. * if we are mounted RW, and we have a new valid hash code, update
  1592. * the super
  1593. */
  1594. if (code != UNSET_HASH &&
  1595. !(s->s_flags & MS_RDONLY) &&
  1596. code != sb_hash_function_code(SB_DISK_SUPER_BLOCK(s))) {
  1597. set_sb_hash_function_code(SB_DISK_SUPER_BLOCK(s), code);
  1598. }
  1599. return code;
  1600. }
  1601. /* return pointer to appropriate function */
  1602. static hashf_t hash_function(struct super_block *s)
  1603. {
  1604. switch (what_hash(s)) {
  1605. case TEA_HASH:
  1606. reiserfs_info(s, "Using tea hash to sort names\n");
  1607. return keyed_hash;
  1608. case YURA_HASH:
  1609. reiserfs_info(s, "Using rupasov hash to sort names\n");
  1610. return yura_hash;
  1611. case R5_HASH:
  1612. reiserfs_info(s, "Using r5 hash to sort names\n");
  1613. return r5_hash;
  1614. }
  1615. return NULL;
  1616. }
  1617. /* this is used to set up correct value for old partitions */
  1618. static int function2code(hashf_t func)
  1619. {
  1620. if (func == keyed_hash)
  1621. return TEA_HASH;
  1622. if (func == yura_hash)
  1623. return YURA_HASH;
  1624. if (func == r5_hash)
  1625. return R5_HASH;
  1626. BUG(); /* should never happen */
  1627. return 0;
  1628. }
  1629. #define SWARN(silent, s, id, ...) \
  1630. if (!(silent)) \
  1631. reiserfs_warning(s, id, __VA_ARGS__)
  1632. static int reiserfs_fill_super(struct super_block *s, void *data, int silent)
  1633. {
  1634. struct inode *root_inode;
  1635. struct reiserfs_transaction_handle th;
  1636. int old_format = 0;
  1637. unsigned long blocks;
  1638. unsigned int commit_max_age = 0;
  1639. int jinit_done = 0;
  1640. struct reiserfs_iget_args args;
  1641. struct reiserfs_super_block *rs;
  1642. char *jdev_name;
  1643. struct reiserfs_sb_info *sbi;
  1644. int errval = -EINVAL;
  1645. char *qf_names[REISERFS_MAXQUOTAS] = {};
  1646. unsigned int qfmt = 0;
  1647. save_mount_options(s, data);
  1648. sbi = kzalloc(sizeof(struct reiserfs_sb_info), GFP_KERNEL);
  1649. if (!sbi)
  1650. return -ENOMEM;
  1651. s->s_fs_info = sbi;
  1652. /* Set default values for options: non-aggressive tails, RO on errors */
  1653. sbi->s_mount_opt |= (1 << REISERFS_SMALLTAIL);
  1654. sbi->s_mount_opt |= (1 << REISERFS_ERROR_RO);
  1655. sbi->s_mount_opt |= (1 << REISERFS_BARRIER_FLUSH);
  1656. /* no preallocation minimum, be smart in reiserfs_file_write instead */
  1657. sbi->s_alloc_options.preallocmin = 0;
  1658. /* Preallocate by 16 blocks (17-1) at once */
  1659. sbi->s_alloc_options.preallocsize = 17;
  1660. /* setup default block allocator options */
  1661. reiserfs_init_alloc_options(s);
  1662. spin_lock_init(&sbi->old_work_lock);
  1663. INIT_DELAYED_WORK(&sbi->old_work, flush_old_commits);
  1664. mutex_init(&sbi->lock);
  1665. sbi->lock_depth = -1;
  1666. sbi->commit_wq = alloc_workqueue("reiserfs/%s", WQ_MEM_RECLAIM, 0,
  1667. s->s_id);
  1668. if (!sbi->commit_wq) {
  1669. SWARN(silent, s, "", "Cannot allocate commit workqueue");
  1670. errval = -ENOMEM;
  1671. goto error_unlocked;
  1672. }
  1673. jdev_name = NULL;
  1674. if (reiserfs_parse_options
  1675. (s, (char *)data, &sbi->s_mount_opt, &blocks, &jdev_name,
  1676. &commit_max_age, qf_names, &qfmt) == 0) {
  1677. goto error_unlocked;
  1678. }
  1679. if (jdev_name && jdev_name[0]) {
  1680. sbi->s_jdev = kstrdup(jdev_name, GFP_KERNEL);
  1681. if (!sbi->s_jdev) {
  1682. SWARN(silent, s, "", "Cannot allocate memory for "
  1683. "journal device name");
  1684. goto error;
  1685. }
  1686. }
  1687. #ifdef CONFIG_QUOTA
  1688. handle_quota_files(s, qf_names, &qfmt);
  1689. #endif
  1690. if (blocks) {
  1691. SWARN(silent, s, "jmacd-7", "resize option for remount only");
  1692. goto error_unlocked;
  1693. }
  1694. /*
  1695. * try old format (undistributed bitmap, super block in 8-th 1k
  1696. * block of a device)
  1697. */
  1698. if (!read_super_block(s, REISERFS_OLD_DISK_OFFSET_IN_BYTES))
  1699. old_format = 1;
  1700. /*
  1701. * try new format (64-th 1k block), which can contain reiserfs
  1702. * super block
  1703. */
  1704. else if (read_super_block(s, REISERFS_DISK_OFFSET_IN_BYTES)) {
  1705. SWARN(silent, s, "sh-2021", "can not find reiserfs on %s",
  1706. s->s_id);
  1707. goto error_unlocked;
  1708. }
  1709. rs = SB_DISK_SUPER_BLOCK(s);
  1710. /*
  1711. * Let's do basic sanity check to verify that underlying device is not
  1712. * smaller than the filesystem. If the check fails then abort and
  1713. * scream, because bad stuff will happen otherwise.
  1714. */
  1715. if (s->s_bdev && s->s_bdev->bd_inode
  1716. && i_size_read(s->s_bdev->bd_inode) <
  1717. sb_block_count(rs) * sb_blocksize(rs)) {
  1718. SWARN(silent, s, "", "Filesystem cannot be "
  1719. "mounted because it is bigger than the device");
  1720. SWARN(silent, s, "", "You may need to run fsck "
  1721. "or increase size of your LVM partition");
  1722. SWARN(silent, s, "", "Or may be you forgot to "
  1723. "reboot after fdisk when it told you to");
  1724. goto error_unlocked;
  1725. }
  1726. sbi->s_mount_state = SB_REISERFS_STATE(s);
  1727. sbi->s_mount_state = REISERFS_VALID_FS;
  1728. if ((errval = reiserfs_init_bitmap_cache(s))) {
  1729. SWARN(silent, s, "jmacd-8", "unable to read bitmap");
  1730. goto error_unlocked;
  1731. }
  1732. errval = -EINVAL;
  1733. #ifdef CONFIG_REISERFS_CHECK
  1734. SWARN(silent, s, "", "CONFIG_REISERFS_CHECK is set ON");
  1735. SWARN(silent, s, "", "- it is slow mode for debugging.");
  1736. #endif
  1737. /* make data=ordered the default */
  1738. if (!reiserfs_data_log(s) && !reiserfs_data_ordered(s) &&
  1739. !reiserfs_data_writeback(s)) {
  1740. sbi->s_mount_opt |= (1 << REISERFS_DATA_ORDERED);
  1741. }
  1742. if (reiserfs_data_log(s)) {
  1743. reiserfs_info(s, "using journaled data mode\n");
  1744. } else if (reiserfs_data_ordered(s)) {
  1745. reiserfs_info(s, "using ordered data mode\n");
  1746. } else {
  1747. reiserfs_info(s, "using writeback data mode\n");
  1748. }
  1749. if (reiserfs_barrier_flush(s)) {
  1750. printk("reiserfs: using flush barriers\n");
  1751. }
  1752. if (journal_init(s, jdev_name, old_format, commit_max_age)) {
  1753. SWARN(silent, s, "sh-2022",
  1754. "unable to initialize journal space");
  1755. goto error_unlocked;
  1756. } else {
  1757. /*
  1758. * once this is set, journal_release must be called
  1759. * if we error out of the mount
  1760. */
  1761. jinit_done = 1;
  1762. }
  1763. if (reread_meta_blocks(s)) {
  1764. SWARN(silent, s, "jmacd-9",
  1765. "unable to reread meta blocks after journal init");
  1766. goto error_unlocked;
  1767. }
  1768. if (replay_only(s))
  1769. goto error_unlocked;
  1770. if (bdev_read_only(s->s_bdev) && !(s->s_flags & MS_RDONLY)) {
  1771. SWARN(silent, s, "clm-7000",
  1772. "Detected readonly device, marking FS readonly");
  1773. s->s_flags |= MS_RDONLY;
  1774. }
  1775. args.objectid = REISERFS_ROOT_OBJECTID;
  1776. args.dirid = REISERFS_ROOT_PARENT_OBJECTID;
  1777. root_inode =
  1778. iget5_locked(s, REISERFS_ROOT_OBJECTID, reiserfs_find_actor,
  1779. reiserfs_init_locked_inode, (void *)&args);
  1780. if (!root_inode) {
  1781. SWARN(silent, s, "jmacd-10", "get root inode failed");
  1782. goto error_unlocked;
  1783. }
  1784. /*
  1785. * This path assumed to be called with the BKL in the old times.
  1786. * Now we have inherited the big reiserfs lock from it and many
  1787. * reiserfs helpers called in the mount path and elsewhere require
  1788. * this lock to be held even if it's not always necessary. Let's be
  1789. * conservative and hold it early. The window can be reduced after
  1790. * careful review of the code.
  1791. */
  1792. reiserfs_write_lock(s);
  1793. if (root_inode->i_state & I_NEW) {
  1794. reiserfs_read_locked_inode(root_inode, &args);
  1795. unlock_new_inode(root_inode);
  1796. }
  1797. s->s_root = d_make_root(root_inode);
  1798. if (!s->s_root)
  1799. goto error;
  1800. /* define and initialize hash function */
  1801. sbi->s_hash_function = hash_function(s);
  1802. if (sbi->s_hash_function == NULL) {
  1803. dput(s->s_root);
  1804. s->s_root = NULL;
  1805. goto error;
  1806. }
  1807. if (is_reiserfs_3_5(rs)
  1808. || (is_reiserfs_jr(rs) && SB_VERSION(s) == REISERFS_VERSION_1))
  1809. set_bit(REISERFS_3_5, &sbi->s_properties);
  1810. else if (old_format)
  1811. set_bit(REISERFS_OLD_FORMAT, &sbi->s_properties);
  1812. else
  1813. set_bit(REISERFS_3_6, &sbi->s_properties);
  1814. if (!(s->s_flags & MS_RDONLY)) {
  1815. errval = journal_begin(&th, s, 1);
  1816. if (errval) {
  1817. dput(s->s_root);
  1818. s->s_root = NULL;
  1819. goto error;
  1820. }
  1821. reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s), 1);
  1822. set_sb_umount_state(rs, REISERFS_ERROR_FS);
  1823. set_sb_fs_state(rs, 0);
  1824. /*
  1825. * Clear out s_bmap_nr if it would wrap. We can handle this
  1826. * case, but older revisions can't. This will cause the
  1827. * file system to fail mount on those older implementations,
  1828. * avoiding corruption. -jeffm
  1829. */
  1830. if (bmap_would_wrap(reiserfs_bmap_count(s)) &&
  1831. sb_bmap_nr(rs) != 0) {
  1832. reiserfs_warning(s, "super-2030", "This file system "
  1833. "claims to use %u bitmap blocks in "
  1834. "its super block, but requires %u. "
  1835. "Clearing to zero.", sb_bmap_nr(rs),
  1836. reiserfs_bmap_count(s));
  1837. set_sb_bmap_nr(rs, 0);
  1838. }
  1839. if (old_format_only(s)) {
  1840. /*
  1841. * filesystem of format 3.5 either with standard
  1842. * or non-standard journal
  1843. */
  1844. if (convert_reiserfs(s)) {
  1845. /* and -o conv is given */
  1846. if (!silent)
  1847. reiserfs_info(s,
  1848. "converting 3.5 filesystem to the 3.6 format");
  1849. if (is_reiserfs_3_5(rs))
  1850. /*
  1851. * put magic string of 3.6 format.
  1852. * 2.2 will not be able to
  1853. * mount this filesystem anymore
  1854. */
  1855. memcpy(rs->s_v1.s_magic,
  1856. reiserfs_3_6_magic_string,
  1857. sizeof
  1858. (reiserfs_3_6_magic_string));
  1859. set_sb_version(rs, REISERFS_VERSION_2);
  1860. reiserfs_convert_objectid_map_v1(s);
  1861. set_bit(REISERFS_3_6, &sbi->s_properties);
  1862. clear_bit(REISERFS_3_5, &sbi->s_properties);
  1863. } else if (!silent) {
  1864. reiserfs_info(s, "using 3.5.x disk format\n");
  1865. }
  1866. } else
  1867. set_sb_mnt_count(rs, sb_mnt_count(rs) + 1);
  1868. journal_mark_dirty(&th, SB_BUFFER_WITH_SB(s));
  1869. errval = journal_end(&th);
  1870. if (errval) {
  1871. dput(s->s_root);
  1872. s->s_root = NULL;
  1873. goto error;
  1874. }
  1875. reiserfs_write_unlock(s);
  1876. if ((errval = reiserfs_lookup_privroot(s)) ||
  1877. (errval = reiserfs_xattr_init(s, s->s_flags))) {
  1878. dput(s->s_root);
  1879. s->s_root = NULL;
  1880. goto error_unlocked;
  1881. }
  1882. reiserfs_write_lock(s);
  1883. /*
  1884. * look for files which were to be removed in previous session
  1885. */
  1886. finish_unfinished(s);
  1887. } else {
  1888. if (old_format_only(s) && !silent) {
  1889. reiserfs_info(s, "using 3.5.x disk format\n");
  1890. }
  1891. reiserfs_write_unlock(s);
  1892. if ((errval = reiserfs_lookup_privroot(s)) ||
  1893. (errval = reiserfs_xattr_init(s, s->s_flags))) {
  1894. dput(s->s_root);
  1895. s->s_root = NULL;
  1896. goto error_unlocked;
  1897. }
  1898. reiserfs_write_lock(s);
  1899. }
  1900. /*
  1901. * mark hash in super block: it could be unset. overwrite should be ok
  1902. */
  1903. set_sb_hash_function_code(rs, function2code(sbi->s_hash_function));
  1904. handle_attrs(s);
  1905. reiserfs_proc_info_init(s);
  1906. init_waitqueue_head(&(sbi->s_wait));
  1907. spin_lock_init(&sbi->bitmap_lock);
  1908. reiserfs_write_unlock(s);
  1909. return (0);
  1910. error:
  1911. reiserfs_write_unlock(s);
  1912. error_unlocked:
  1913. /* kill the commit thread, free journal ram */
  1914. if (jinit_done) {
  1915. reiserfs_write_lock(s);
  1916. journal_release_error(NULL, s);
  1917. reiserfs_write_unlock(s);
  1918. }
  1919. if (sbi->commit_wq)
  1920. destroy_workqueue(sbi->commit_wq);
  1921. cancel_delayed_work_sync(&REISERFS_SB(s)->old_work);
  1922. reiserfs_free_bitmap_cache(s);
  1923. if (SB_BUFFER_WITH_SB(s))
  1924. brelse(SB_BUFFER_WITH_SB(s));
  1925. #ifdef CONFIG_QUOTA
  1926. {
  1927. int j;
  1928. for (j = 0; j < REISERFS_MAXQUOTAS; j++)
  1929. kfree(qf_names[j]);
  1930. }
  1931. #endif
  1932. kfree(sbi);
  1933. s->s_fs_info = NULL;
  1934. return errval;
  1935. }
  1936. static int reiserfs_statfs(struct dentry *dentry, struct kstatfs *buf)
  1937. {
  1938. struct reiserfs_super_block *rs = SB_DISK_SUPER_BLOCK(dentry->d_sb);
  1939. buf->f_namelen = (REISERFS_MAX_NAME(s->s_blocksize));
  1940. buf->f_bfree = sb_free_blocks(rs);
  1941. buf->f_bavail = buf->f_bfree;
  1942. buf->f_blocks = sb_block_count(rs) - sb_bmap_nr(rs) - 1;
  1943. buf->f_bsize = dentry->d_sb->s_blocksize;
  1944. /* changed to accommodate gcc folks. */
  1945. buf->f_type = REISERFS_SUPER_MAGIC;
  1946. buf->f_fsid.val[0] = (u32)crc32_le(0, rs->s_uuid, sizeof(rs->s_uuid)/2);
  1947. buf->f_fsid.val[1] = (u32)crc32_le(0, rs->s_uuid + sizeof(rs->s_uuid)/2,
  1948. sizeof(rs->s_uuid)/2);
  1949. return 0;
  1950. }
  1951. #ifdef CONFIG_QUOTA
  1952. static int reiserfs_write_dquot(struct dquot *dquot)
  1953. {
  1954. struct reiserfs_transaction_handle th;
  1955. int ret, err;
  1956. int depth;
  1957. reiserfs_write_lock(dquot->dq_sb);
  1958. ret =
  1959. journal_begin(&th, dquot->dq_sb,
  1960. REISERFS_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
  1961. if (ret)
  1962. goto out;
  1963. depth = reiserfs_write_unlock_nested(dquot->dq_sb);
  1964. ret = dquot_commit(dquot);
  1965. reiserfs_write_lock_nested(dquot->dq_sb, depth);
  1966. err = journal_end(&th);
  1967. if (!ret && err)
  1968. ret = err;
  1969. out:
  1970. reiserfs_write_unlock(dquot->dq_sb);
  1971. return ret;
  1972. }
  1973. static int reiserfs_acquire_dquot(struct dquot *dquot)
  1974. {
  1975. struct reiserfs_transaction_handle th;
  1976. int ret, err;
  1977. int depth;
  1978. reiserfs_write_lock(dquot->dq_sb);
  1979. ret =
  1980. journal_begin(&th, dquot->dq_sb,
  1981. REISERFS_QUOTA_INIT_BLOCKS(dquot->dq_sb));
  1982. if (ret)
  1983. goto out;
  1984. depth = reiserfs_write_unlock_nested(dquot->dq_sb);
  1985. ret = dquot_acquire(dquot);
  1986. reiserfs_write_lock_nested(dquot->dq_sb, depth);
  1987. err = journal_end(&th);
  1988. if (!ret && err)
  1989. ret = err;
  1990. out:
  1991. reiserfs_write_unlock(dquot->dq_sb);
  1992. return ret;
  1993. }
  1994. static int reiserfs_release_dquot(struct dquot *dquot)
  1995. {
  1996. struct reiserfs_transaction_handle th;
  1997. int ret, err;
  1998. reiserfs_write_lock(dquot->dq_sb);
  1999. ret =
  2000. journal_begin(&th, dquot->dq_sb,
  2001. REISERFS_QUOTA_DEL_BLOCKS(dquot->dq_sb));
  2002. reiserfs_write_unlock(dquot->dq_sb);
  2003. if (ret) {
  2004. /* Release dquot anyway to avoid endless cycle in dqput() */
  2005. dquot_release(dquot);
  2006. goto out;
  2007. }
  2008. ret = dquot_release(dquot);
  2009. reiserfs_write_lock(dquot->dq_sb);
  2010. err = journal_end(&th);
  2011. if (!ret && err)
  2012. ret = err;
  2013. reiserfs_write_unlock(dquot->dq_sb);
  2014. out:
  2015. return ret;
  2016. }
  2017. static int reiserfs_mark_dquot_dirty(struct dquot *dquot)
  2018. {
  2019. /* Are we journaling quotas? */
  2020. if (REISERFS_SB(dquot->dq_sb)->s_qf_names[USRQUOTA] ||
  2021. REISERFS_SB(dquot->dq_sb)->s_qf_names[GRPQUOTA]) {
  2022. dquot_mark_dquot_dirty(dquot);
  2023. return reiserfs_write_dquot(dquot);
  2024. } else
  2025. return dquot_mark_dquot_dirty(dquot);
  2026. }
  2027. static int reiserfs_write_info(struct super_block *sb, int type)
  2028. {
  2029. struct reiserfs_transaction_handle th;
  2030. int ret, err;
  2031. int depth;
  2032. /* Data block + inode block */
  2033. reiserfs_write_lock(sb);
  2034. ret = journal_begin(&th, sb, 2);
  2035. if (ret)
  2036. goto out;
  2037. depth = reiserfs_write_unlock_nested(sb);
  2038. ret = dquot_commit_info(sb, type);
  2039. reiserfs_write_lock_nested(sb, depth);
  2040. err = journal_end(&th);
  2041. if (!ret && err)
  2042. ret = err;
  2043. out:
  2044. reiserfs_write_unlock(sb);
  2045. return ret;
  2046. }
  2047. /*
  2048. * Turn on quotas during mount time - we need to find the quota file and such...
  2049. */
  2050. static int reiserfs_quota_on_mount(struct super_block *sb, int type)
  2051. {
  2052. return dquot_quota_on_mount(sb, REISERFS_SB(sb)->s_qf_names[type],
  2053. REISERFS_SB(sb)->s_jquota_fmt, type);
  2054. }
  2055. /*
  2056. * Standard function to be called on quota_on
  2057. */
  2058. static int reiserfs_quota_on(struct super_block *sb, int type, int format_id,
  2059. struct path *path)
  2060. {
  2061. int err;
  2062. struct inode *inode;
  2063. struct reiserfs_transaction_handle th;
  2064. int opt = type == USRQUOTA ? REISERFS_USRQUOTA : REISERFS_GRPQUOTA;
  2065. reiserfs_write_lock(sb);
  2066. if (!(REISERFS_SB(sb)->s_mount_opt & (1 << opt))) {
  2067. err = -EINVAL;
  2068. goto out;
  2069. }
  2070. /* Quotafile not on the same filesystem? */
  2071. if (path->dentry->d_sb != sb) {
  2072. err = -EXDEV;
  2073. goto out;
  2074. }
  2075. inode = d_inode(path->dentry);
  2076. /*
  2077. * We must not pack tails for quota files on reiserfs for quota
  2078. * IO to work
  2079. */
  2080. if (!(REISERFS_I(inode)->i_flags & i_nopack_mask)) {
  2081. err = reiserfs_unpack(inode, NULL);
  2082. if (err) {
  2083. reiserfs_warning(sb, "super-6520",
  2084. "Unpacking tail of quota file failed"
  2085. " (%d). Cannot turn on quotas.", err);
  2086. err = -EINVAL;
  2087. goto out;
  2088. }
  2089. mark_inode_dirty(inode);
  2090. }
  2091. /* Journaling quota? */
  2092. if (REISERFS_SB(sb)->s_qf_names[type]) {
  2093. /* Quotafile not of fs root? */
  2094. if (path->dentry->d_parent != sb->s_root)
  2095. reiserfs_warning(sb, "super-6521",
  2096. "Quota file not on filesystem root. "
  2097. "Journalled quota will not work.");
  2098. }
  2099. /*
  2100. * When we journal data on quota file, we have to flush journal to see
  2101. * all updates to the file when we bypass pagecache...
  2102. */
  2103. if (reiserfs_file_data_log(inode)) {
  2104. /* Just start temporary transaction and finish it */
  2105. err = journal_begin(&th, sb, 1);
  2106. if (err)
  2107. goto out;
  2108. err = journal_end_sync(&th);
  2109. if (err)
  2110. goto out;
  2111. }
  2112. reiserfs_write_unlock(sb);
  2113. return dquot_quota_on(sb, type, format_id, path);
  2114. out:
  2115. reiserfs_write_unlock(sb);
  2116. return err;
  2117. }
  2118. /*
  2119. * Read data from quotafile - avoid pagecache and such because we cannot afford
  2120. * acquiring the locks... As quota files are never truncated and quota code
  2121. * itself serializes the operations (and no one else should touch the files)
  2122. * we don't have to be afraid of races
  2123. */
  2124. static ssize_t reiserfs_quota_read(struct super_block *sb, int type, char *data,
  2125. size_t len, loff_t off)
  2126. {
  2127. struct inode *inode = sb_dqopt(sb)->files[type];
  2128. unsigned long blk = off >> sb->s_blocksize_bits;
  2129. int err = 0, offset = off & (sb->s_blocksize - 1), tocopy;
  2130. size_t toread;
  2131. struct buffer_head tmp_bh, *bh;
  2132. loff_t i_size = i_size_read(inode);
  2133. if (off > i_size)
  2134. return 0;
  2135. if (off + len > i_size)
  2136. len = i_size - off;
  2137. toread = len;
  2138. while (toread > 0) {
  2139. tocopy =
  2140. sb->s_blocksize - offset <
  2141. toread ? sb->s_blocksize - offset : toread;
  2142. tmp_bh.b_state = 0;
  2143. /*
  2144. * Quota files are without tails so we can safely
  2145. * use this function
  2146. */
  2147. reiserfs_write_lock(sb);
  2148. err = reiserfs_get_block(inode, blk, &tmp_bh, 0);
  2149. reiserfs_write_unlock(sb);
  2150. if (err)
  2151. return err;
  2152. if (!buffer_mapped(&tmp_bh)) /* A hole? */
  2153. memset(data, 0, tocopy);
  2154. else {
  2155. bh = sb_bread(sb, tmp_bh.b_blocknr);
  2156. if (!bh)
  2157. return -EIO;
  2158. memcpy(data, bh->b_data + offset, tocopy);
  2159. brelse(bh);
  2160. }
  2161. offset = 0;
  2162. toread -= tocopy;
  2163. data += tocopy;
  2164. blk++;
  2165. }
  2166. return len;
  2167. }
  2168. /*
  2169. * Write to quotafile (we know the transaction is already started and has
  2170. * enough credits)
  2171. */
  2172. static ssize_t reiserfs_quota_write(struct super_block *sb, int type,
  2173. const char *data, size_t len, loff_t off)
  2174. {
  2175. struct inode *inode = sb_dqopt(sb)->files[type];
  2176. unsigned long blk = off >> sb->s_blocksize_bits;
  2177. int err = 0, offset = off & (sb->s_blocksize - 1), tocopy;
  2178. int journal_quota = REISERFS_SB(sb)->s_qf_names[type] != NULL;
  2179. size_t towrite = len;
  2180. struct buffer_head tmp_bh, *bh;
  2181. if (!current->journal_info) {
  2182. printk(KERN_WARNING "reiserfs: Quota write (off=%llu, len=%llu) cancelled because transaction is not started.\n",
  2183. (unsigned long long)off, (unsigned long long)len);
  2184. return -EIO;
  2185. }
  2186. while (towrite > 0) {
  2187. tocopy = sb->s_blocksize - offset < towrite ?
  2188. sb->s_blocksize - offset : towrite;
  2189. tmp_bh.b_state = 0;
  2190. reiserfs_write_lock(sb);
  2191. err = reiserfs_get_block(inode, blk, &tmp_bh, GET_BLOCK_CREATE);
  2192. reiserfs_write_unlock(sb);
  2193. if (err)
  2194. goto out;
  2195. if (offset || tocopy != sb->s_blocksize)
  2196. bh = sb_bread(sb, tmp_bh.b_blocknr);
  2197. else
  2198. bh = sb_getblk(sb, tmp_bh.b_blocknr);
  2199. if (!bh) {
  2200. err = -EIO;
  2201. goto out;
  2202. }
  2203. lock_buffer(bh);
  2204. memcpy(bh->b_data + offset, data, tocopy);
  2205. flush_dcache_page(bh->b_page);
  2206. set_buffer_uptodate(bh);
  2207. unlock_buffer(bh);
  2208. reiserfs_write_lock(sb);
  2209. reiserfs_prepare_for_journal(sb, bh, 1);
  2210. journal_mark_dirty(current->journal_info, bh);
  2211. if (!journal_quota)
  2212. reiserfs_add_ordered_list(inode, bh);
  2213. reiserfs_write_unlock(sb);
  2214. brelse(bh);
  2215. offset = 0;
  2216. towrite -= tocopy;
  2217. data += tocopy;
  2218. blk++;
  2219. }
  2220. out:
  2221. if (len == towrite)
  2222. return err;
  2223. if (inode->i_size < off + len - towrite)
  2224. i_size_write(inode, off + len - towrite);
  2225. inode->i_version++;
  2226. inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  2227. mark_inode_dirty(inode);
  2228. return len - towrite;
  2229. }
  2230. #endif
  2231. static struct dentry *get_super_block(struct file_system_type *fs_type,
  2232. int flags, const char *dev_name,
  2233. void *data)
  2234. {
  2235. return mount_bdev(fs_type, flags, dev_name, data, reiserfs_fill_super);
  2236. }
  2237. static int __init init_reiserfs_fs(void)
  2238. {
  2239. int ret;
  2240. ret = init_inodecache();
  2241. if (ret)
  2242. return ret;
  2243. reiserfs_proc_info_global_init();
  2244. ret = register_filesystem(&reiserfs_fs_type);
  2245. if (ret)
  2246. goto out;
  2247. return 0;
  2248. out:
  2249. reiserfs_proc_info_global_done();
  2250. destroy_inodecache();
  2251. return ret;
  2252. }
  2253. static void __exit exit_reiserfs_fs(void)
  2254. {
  2255. reiserfs_proc_info_global_done();
  2256. unregister_filesystem(&reiserfs_fs_type);
  2257. destroy_inodecache();
  2258. }
  2259. struct file_system_type reiserfs_fs_type = {
  2260. .owner = THIS_MODULE,
  2261. .name = "reiserfs",
  2262. .mount = get_super_block,
  2263. .kill_sb = reiserfs_kill_sb,
  2264. .fs_flags = FS_REQUIRES_DEV,
  2265. };
  2266. MODULE_ALIAS_FS("reiserfs");
  2267. MODULE_DESCRIPTION("ReiserFS journaled filesystem");
  2268. MODULE_AUTHOR("Hans Reiser <reiser@namesys.com>");
  2269. MODULE_LICENSE("GPL");
  2270. module_init(init_reiserfs_fs);
  2271. module_exit(exit_reiserfs_fs);