super.c 68 KB

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