super.c 68 KB

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