super.c 61 KB

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