super.c 70 KB

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