super.c 70 KB

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