super.c 63 KB

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