super.c 69 KB

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