super.c 69 KB

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