super.c 68 KB

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