super.c 70 KB

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