blob: e26ec564758f8f4599cfe29f99a4c187d7e6b4b8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/vmalloc.h>
17#include <linux/time.h>
18#include <asm/uaccess.h>
Al Virof466c6f2012-03-17 01:16:43 -040019#include "reiserfs.h"
Al Viroa3063ab2012-03-17 01:03:10 -040020#include "acl.h"
Al Viroc45ac882012-03-17 00:59:06 -040021#include "xattr.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/init.h>
23#include <linux/blkdev.h>
24#include <linux/buffer_head.h>
Christoph Hellwiga5694252007-07-17 04:04:28 -070025#include <linux/exportfs.h>
Jan Kara74abb982008-07-25 01:46:51 -070026#include <linux/quotaops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/vfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/mount.h>
29#include <linux/namei.h>
Coly Li651d0622009-04-02 16:59:41 -070030#include <linux/crc32.h>
Jan Karac3aa0772011-12-21 20:17:10 +010031#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33struct file_system_type reiserfs_fs_type;
34
35static const char reiserfs_3_5_magic_string[] = REISERFS_SUPER_MAGIC_STRING;
36static const char reiserfs_3_6_magic_string[] = REISER2FS_SUPER_MAGIC_STRING;
37static const char reiserfs_jr_magic_string[] = REISER2FS_JR_SUPER_MAGIC_STRING;
38
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070039int is_reiserfs_3_5(struct reiserfs_super_block *rs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070041 return !strncmp(rs->s_v1.s_magic, reiserfs_3_5_magic_string,
42 strlen(reiserfs_3_5_magic_string));
Linus Torvalds1da177e2005-04-16 15:20:36 -070043}
44
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070045int is_reiserfs_3_6(struct reiserfs_super_block *rs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070047 return !strncmp(rs->s_v1.s_magic, reiserfs_3_6_magic_string,
48 strlen(reiserfs_3_6_magic_string));
Linus Torvalds1da177e2005-04-16 15:20:36 -070049}
50
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070051int is_reiserfs_jr(struct reiserfs_super_block *rs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070053 return !strncmp(rs->s_v1.s_magic, reiserfs_jr_magic_string,
54 strlen(reiserfs_jr_magic_string));
Linus Torvalds1da177e2005-04-16 15:20:36 -070055}
56
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070057static int is_any_reiserfs_magic_string(struct reiserfs_super_block *rs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070059 return (is_reiserfs_3_5(rs) || is_reiserfs_3_6(rs) ||
60 is_reiserfs_jr(rs));
Linus Torvalds1da177e2005-04-16 15:20:36 -070061}
62
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070063static int reiserfs_remount(struct super_block *s, int *flags, char *data);
David Howells726c3342006-06-23 02:02:58 -070064static int reiserfs_statfs(struct dentry *dentry, struct kstatfs *buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070066static int reiserfs_sync_fs(struct super_block *s, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067{
Christoph Hellwig5af79262009-05-05 15:41:25 +020068 struct reiserfs_transaction_handle th;
69
Jan Karaa1177822012-07-03 16:45:29 +020070 /*
71 * Writeback quota in non-journalled quota case - journalled quota has
72 * no dirty dquots
73 */
74 dquot_writeback_dquots(s, -1);
Christoph Hellwig5af79262009-05-05 15:41:25 +020075 reiserfs_write_lock(s);
76 if (!journal_begin(&th, s, 1))
Jeff Mahoney58d85422014-04-23 10:00:38 -040077 if (!journal_end_sync(&th))
Christoph Hellwig5af79262009-05-05 15:41:25 +020078 reiserfs_flush_old_commits(s);
Christoph Hellwig5af79262009-05-05 15:41:25 +020079 reiserfs_write_unlock(s);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070080 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081}
82
Artem Bityutskiy033369d2012-06-01 17:18:08 +030083static void flush_old_commits(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084{
Artem Bityutskiy033369d2012-06-01 17:18:08 +030085 struct reiserfs_sb_info *sbi;
86 struct super_block *s;
87
88 sbi = container_of(work, struct reiserfs_sb_info, old_work.work);
89 s = sbi->s_journal->j_work_sb;
90
91 spin_lock(&sbi->old_work_lock);
92 sbi->work_queued = 0;
93 spin_unlock(&sbi->old_work_lock);
94
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070095 reiserfs_sync_fs(s, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096}
97
Artem Bityutskiy033369d2012-06-01 17:18:08 +030098void reiserfs_schedule_old_flush(struct super_block *s)
99{
100 struct reiserfs_sb_info *sbi = REISERFS_SB(s);
101 unsigned long delay;
102
103 if (s->s_flags & MS_RDONLY)
104 return;
105
106 spin_lock(&sbi->old_work_lock);
107 if (!sbi->work_queued) {
108 delay = msecs_to_jiffies(dirty_writeback_interval * 10);
109 queue_delayed_work(system_long_wq, &sbi->old_work, delay);
110 sbi->work_queued = 1;
111 }
112 spin_unlock(&sbi->old_work_lock);
113}
114
115static void cancel_old_flush(struct super_block *s)
116{
117 struct reiserfs_sb_info *sbi = REISERFS_SB(s);
118
119 cancel_delayed_work_sync(&REISERFS_SB(s)->old_work);
120 spin_lock(&sbi->old_work_lock);
121 sbi->work_queued = 0;
122 spin_unlock(&sbi->old_work_lock);
123}
124
Takashi Satoc4be0c12009-01-09 16:40:58 -0800125static int reiserfs_freeze(struct super_block *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700127 struct reiserfs_transaction_handle th;
Artem Bityutskiy033369d2012-06-01 17:18:08 +0300128
129 cancel_old_flush(s);
130
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700131 reiserfs_write_lock(s);
132 if (!(s->s_flags & MS_RDONLY)) {
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);
Jeff Mahoney09f1b802014-04-23 10:00:39 -0400139 journal_mark_dirty(&th, SB_BUFFER_WITH_SB(s));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700140 reiserfs_block_writes(&th);
Jeff Mahoney58d85422014-04-23 10:00:38 -0400141 journal_end_sync(&th);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700142 }
143 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700144 reiserfs_write_unlock(s);
Takashi Satoc4be0c12009-01-09 16:40:58 -0800145 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146}
147
Takashi Satoc4be0c12009-01-09 16:40:58 -0800148static int reiserfs_unfreeze(struct super_block *s)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700149{
150 reiserfs_allow_writes(s);
Takashi Satoc4be0c12009-01-09 16:40:58 -0800151 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152}
153
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700154extern const struct in_core_key MAX_IN_CORE_KEY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Jeff Mahoney098297b2014-04-23 10:00:36 -0400156/*
157 * this is used to delete "save link" when there are no items of a
158 * file it points to. It can either happen if unlink is completed but
159 * "save unlink" removal, or if file has both unlink and truncate
160 * pending and as unlink completes first (because key of "save link"
161 * protecting unlink is bigger that a key lf "save link" which
162 * protects truncate), so there left no items to make truncate
163 * completion on
164 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700165static int remove_save_link_only(struct super_block *s,
166 struct reiserfs_key *key, int oid_free)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700168 struct reiserfs_transaction_handle th;
169 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700171 /* we are going to do one balancing */
172 err = journal_begin(&th, s, JOURNAL_PER_BALANCE_CNT);
173 if (err)
174 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700176 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
Jeff Mahoney58d85422014-04-23 10:00:38 -0400181 return journal_end(&th);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182}
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184#ifdef CONFIG_QUOTA
185static int reiserfs_quota_on_mount(struct super_block *, int);
186#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188/* look for uncompleted unlinks and truncates and complete them */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700189static int finish_unfinished(struct super_block *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700191 INITIALIZE_PATH(path);
192 struct cpu_key max_cpu_key, obj_key;
Lepton Wufb46f342007-10-16 23:30:04 -0700193 struct reiserfs_key save_link_key, last_inode_key;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700194 int retval = 0;
195 struct item_head *ih;
196 struct buffer_head *bh;
197 int item_pos;
198 char *item;
199 int done;
200 struct inode *inode;
201 int truncate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202#ifdef CONFIG_QUOTA
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700203 int i;
204 int ms_active_set;
Jan Karaf4b113a2010-05-21 19:12:51 +0200205 int quota_enabled[MAXQUOTAS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700207
208 /* compose key to look for "save" links */
209 max_cpu_key.version = KEY_FORMAT_3_5;
210 max_cpu_key.on_disk_key.k_dir_id = ~0U;
211 max_cpu_key.on_disk_key.k_objectid = ~0U;
212 set_cpu_key_k_offset(&max_cpu_key, ~0U);
213 max_cpu_key.key_length = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Lepton Wufb46f342007-10-16 23:30:04 -0700215 memset(&last_inode_key, 0, sizeof(last_inode_key));
216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217#ifdef CONFIG_QUOTA
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700218 /* Needed for iput() to work correctly and not trash data */
219 if (s->s_flags & MS_ACTIVE) {
220 ms_active_set = 0;
221 } else {
222 ms_active_set = 1;
223 s->s_flags |= MS_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700225 /* Turn on quotas so that they are updated correctly */
226 for (i = 0; i < MAXQUOTAS; i++) {
Jan Karaf4b113a2010-05-21 19:12:51 +0200227 quota_enabled[i] = 1;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700228 if (REISERFS_SB(s)->s_qf_names[i]) {
Jan Karaf4b113a2010-05-21 19:12:51 +0200229 int ret;
230
231 if (sb_has_quota_active(s, i)) {
232 quota_enabled[i] = 0;
233 continue;
234 }
235 ret = reiserfs_quota_on_mount(s, i);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700236 if (ret < 0)
Jeff Mahoney45b03d52009-03-30 14:02:21 -0400237 reiserfs_warning(s, "reiserfs-2500",
238 "cannot turn on journaled "
239 "quota: error %d", ret);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700240 }
241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700244 done = 0;
245 REISERFS_SB(s)->s_is_unlinked_ok = 1;
246 while (!retval) {
Jeff Mahoneyd2d03952013-08-08 17:34:47 -0400247 int depth;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700248 retval = search_item(s, &max_cpu_key, &path);
249 if (retval != ITEM_NOT_FOUND) {
Jeff Mahoney0030b642009-03-30 14:02:28 -0400250 reiserfs_error(s, "vs-2140",
251 "search_by_key returned %d", retval);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700252 break;
253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700255 bh = get_last_bh(&path);
256 item_pos = get_item_pos(&path);
257 if (item_pos != B_NR_ITEMS(bh)) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -0400258 reiserfs_warning(s, "vs-2060",
259 "wrong position found");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700260 break;
261 }
262 item_pos--;
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400263 ih = item_head(bh, item_pos);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700264
265 if (le32_to_cpu(ih->ih_key.k_dir_id) != MAX_KEY_OBJECTID)
266 /* there are no "save" links anymore */
267 break;
268
269 save_link_key = ih->ih_key;
270 if (is_indirect_le_ih(ih))
271 truncate = 1;
272 else
273 truncate = 0;
274
275 /* reiserfs_iget needs k_dirid and k_objectid only */
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400276 item = ih_item_body(bh, ih);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700277 obj_key.on_disk_key.k_dir_id = le32_to_cpu(*(__le32 *) item);
278 obj_key.on_disk_key.k_objectid =
279 le32_to_cpu(ih->ih_key.k_objectid);
280 obj_key.on_disk_key.k_offset = 0;
281 obj_key.on_disk_key.k_type = 0;
282
283 pathrelse(&path);
284
285 inode = reiserfs_iget(s, &obj_key);
286 if (!inode) {
Jeff Mahoney098297b2014-04-23 10:00:36 -0400287 /*
288 * the unlink almost completed, it just did not
289 * manage to remove "save" link and release objectid
290 */
Jeff Mahoney45b03d52009-03-30 14:02:21 -0400291 reiserfs_warning(s, "vs-2180", "iget failed for %K",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700292 &obj_key);
293 retval = remove_save_link_only(s, &save_link_key, 1);
294 continue;
295 }
296
297 if (!truncate && inode->i_nlink) {
298 /* file is not unlinked */
Jeff Mahoney45b03d52009-03-30 14:02:21 -0400299 reiserfs_warning(s, "vs-2185",
300 "file %K is not unlinked",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700301 &obj_key);
302 retval = remove_save_link_only(s, &save_link_key, 0);
303 continue;
304 }
Jeff Mahoneyd2d03952013-08-08 17:34:47 -0400305 depth = reiserfs_write_unlock_nested(inode->i_sb);
Christoph Hellwig871a2932010-03-03 09:05:07 -0500306 dquot_initialize(inode);
Jeff Mahoneyd2d03952013-08-08 17:34:47 -0400307 reiserfs_write_lock_nested(inode->i_sb, depth);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700308
309 if (truncate && S_ISDIR(inode->i_mode)) {
Jeff Mahoney098297b2014-04-23 10:00:36 -0400310 /*
311 * We got a truncate request for a dir which
312 * is impossible. The only imaginable way is to
313 * execute unfinished truncate request then boot
314 * into old kernel, remove the file and create dir
315 * with the same key.
316 */
Jeff Mahoney45b03d52009-03-30 14:02:21 -0400317 reiserfs_warning(s, "green-2101",
318 "impossible truncate on a "
319 "directory %k. Please report",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700320 INODE_PKEY(inode));
321 retval = remove_save_link_only(s, &save_link_key, 0);
322 truncate = 0;
323 iput(inode);
324 continue;
325 }
326
327 if (truncate) {
328 REISERFS_I(inode)->i_flags |=
329 i_link_saved_truncate_mask;
Jeff Mahoney098297b2014-04-23 10:00:36 -0400330 /*
331 * not completed truncate found. New size was
332 * committed together with "save" link
333 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700334 reiserfs_info(s, "Truncating %k to %Ld ..",
335 INODE_PKEY(inode), inode->i_size);
Jeff Mahoney098297b2014-04-23 10:00:36 -0400336
337 /* don't update modification time */
338 reiserfs_truncate_file(inode, 0);
339
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700340 retval = remove_save_link(inode, truncate);
341 } else {
342 REISERFS_I(inode)->i_flags |= i_link_saved_unlink_mask;
343 /* not completed unlink (rmdir) found */
344 reiserfs_info(s, "Removing %k..", INODE_PKEY(inode));
Lepton Wufb46f342007-10-16 23:30:04 -0700345 if (memcmp(&last_inode_key, INODE_PKEY(inode),
346 sizeof(last_inode_key))){
347 last_inode_key = *INODE_PKEY(inode);
348 /* removal gets completed in iput */
349 retval = 0;
350 } else {
Jeff Mahoney45b03d52009-03-30 14:02:21 -0400351 reiserfs_warning(s, "super-2189", "Dead loop "
352 "in finish_unfinished "
353 "detected, just remove "
354 "save link\n");
Lepton Wufb46f342007-10-16 23:30:04 -0700355 retval = remove_save_link_only(s,
356 &save_link_key, 0);
357 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700358 }
359
360 iput(inode);
361 printk("done\n");
362 done++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700364 REISERFS_SB(s)->s_is_unlinked_ok = 0;
365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366#ifdef CONFIG_QUOTA
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700367 /* Turn quotas off */
Jeff Mahoneyd2d03952013-08-08 17:34:47 -0400368 reiserfs_write_unlock(s);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700369 for (i = 0; i < MAXQUOTAS; i++) {
Jan Karaf4b113a2010-05-21 19:12:51 +0200370 if (sb_dqopt(s)->files[i] && quota_enabled[i])
Christoph Hellwig287a8092010-05-19 07:16:45 -0400371 dquot_quota_off(s, i);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700372 }
Jeff Mahoneyd2d03952013-08-08 17:34:47 -0400373 reiserfs_write_lock(s);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700374 if (ms_active_set)
375 /* Restore the flag back */
376 s->s_flags &= ~MS_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700378 pathrelse(&path);
379 if (done)
380 reiserfs_info(s, "There were %d uncompleted unlinks/truncates. "
381 "Completed\n", done);
382 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383}
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700384
Jeff Mahoney098297b2014-04-23 10:00:36 -0400385/*
386 * to protect file being unlinked from getting lost we "safe" link files
387 * being unlinked. This link will be deleted in the same transaction with last
388 * item of file. mounting the filesystem we scan all these links and remove
389 * files which almost got lost
390 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700391void add_save_link(struct reiserfs_transaction_handle *th,
392 struct inode *inode, int truncate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700394 INITIALIZE_PATH(path);
395 int retval;
396 struct cpu_key key;
397 struct item_head ih;
398 __le32 link;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700400 BUG_ON(!th->t_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700402 /* file can only get one "save link" of each kind */
403 RFALSE(truncate &&
404 (REISERFS_I(inode)->i_flags & i_link_saved_truncate_mask),
405 "saved link already exists for truncated inode %lx",
406 (long)inode->i_ino);
407 RFALSE(!truncate &&
408 (REISERFS_I(inode)->i_flags & i_link_saved_unlink_mask),
409 "saved link already exists for unlinked inode %lx",
410 (long)inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700412 /* setup key of "save" link */
413 key.version = KEY_FORMAT_3_5;
414 key.on_disk_key.k_dir_id = MAX_KEY_OBJECTID;
415 key.on_disk_key.k_objectid = inode->i_ino;
416 if (!truncate) {
417 /* unlink, rmdir, rename */
418 set_cpu_key_k_offset(&key, 1 + inode->i_sb->s_blocksize);
419 set_cpu_key_k_type(&key, TYPE_DIRECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700421 /* item head of "safe" link */
422 make_le_item_head(&ih, &key, key.version,
423 1 + inode->i_sb->s_blocksize, TYPE_DIRECT,
424 4 /*length */ , 0xffff /*free space */ );
425 } else {
426 /* truncate */
427 if (S_ISDIR(inode->i_mode))
Jeff Mahoney45b03d52009-03-30 14:02:21 -0400428 reiserfs_warning(inode->i_sb, "green-2102",
429 "Adding a truncate savelink for "
430 "a directory %k! Please report",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700431 INODE_PKEY(inode));
432 set_cpu_key_k_offset(&key, 1);
433 set_cpu_key_k_type(&key, TYPE_INDIRECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700435 /* item head of "safe" link */
436 make_le_item_head(&ih, &key, key.version, 1, TYPE_INDIRECT,
437 4 /*length */ , 0 /*free space */ );
438 }
439 key.key_length = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700441 /* look for its place in the tree */
442 retval = search_item(inode->i_sb, &key, &path);
443 if (retval != ITEM_NOT_FOUND) {
444 if (retval != -ENOSPC)
Jeff Mahoney0030b642009-03-30 14:02:28 -0400445 reiserfs_error(inode->i_sb, "vs-2100",
446 "search_by_key (%K) returned %d", &key,
447 retval);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700448 pathrelse(&path);
449 return;
450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700452 /* body of "save" link */
453 link = INODE_PKEY(inode)->k_dir_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300455 /* put "save" link into tree, don't charge quota to anyone */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700456 retval =
457 reiserfs_insert_item(th, &path, &key, &ih, NULL, (char *)&link);
458 if (retval) {
459 if (retval != -ENOSPC)
Jeff Mahoney0030b642009-03-30 14:02:28 -0400460 reiserfs_error(inode->i_sb, "vs-2120",
461 "insert_item returned %d", retval);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700462 } else {
463 if (truncate)
464 REISERFS_I(inode)->i_flags |=
465 i_link_saved_truncate_mask;
466 else
467 REISERFS_I(inode)->i_flags |= i_link_saved_unlink_mask;
468 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469}
470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471/* this opens transaction unlike add_save_link */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700472int remove_save_link(struct inode *inode, int truncate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700474 struct reiserfs_transaction_handle th;
475 struct reiserfs_key key;
476 int err;
477
478 /* we are going to do one balancing only */
479 err = journal_begin(&th, inode->i_sb, JOURNAL_PER_BALANCE_CNT);
480 if (err)
481 return err;
482
483 /* setup key of "save" link */
484 key.k_dir_id = cpu_to_le32(MAX_KEY_OBJECTID);
485 key.k_objectid = INODE_PKEY(inode)->k_objectid;
486 if (!truncate) {
487 /* unlink, rmdir, rename */
488 set_le_key_k_offset(KEY_FORMAT_3_5, &key,
489 1 + inode->i_sb->s_blocksize);
490 set_le_key_k_type(KEY_FORMAT_3_5, &key, TYPE_DIRECT);
491 } else {
492 /* truncate */
493 set_le_key_k_offset(KEY_FORMAT_3_5, &key, 1);
494 set_le_key_k_type(KEY_FORMAT_3_5, &key, TYPE_INDIRECT);
495 }
496
497 if ((truncate &&
498 (REISERFS_I(inode)->i_flags & i_link_saved_truncate_mask)) ||
499 (!truncate &&
500 (REISERFS_I(inode)->i_flags & i_link_saved_unlink_mask)))
501 /* don't take quota bytes from anywhere */
502 reiserfs_delete_solid_item(&th, NULL, &key);
503 if (!truncate) {
504 reiserfs_release_objectid(&th, inode->i_ino);
505 REISERFS_I(inode)->i_flags &= ~i_link_saved_unlink_mask;
506 } else
507 REISERFS_I(inode)->i_flags &= ~i_link_saved_truncate_mask;
508
Jeff Mahoney58d85422014-04-23 10:00:38 -0400509 return journal_end(&th);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510}
511
David Howellsedc666e2006-10-11 01:22:14 -0700512static void reiserfs_kill_sb(struct super_block *s)
513{
514 if (REISERFS_SB(s)) {
Al Viro672fe152013-08-05 17:37:37 +0400515 reiserfs_proc_info_done(s);
Jeff Mahoneya9e36da2011-12-21 21:18:43 +0100516 /*
517 * Force any pending inode evictions to occur now. Any
518 * inodes to be removed that have extended attributes
519 * associated with them need to clean them up before
520 * we can release the extended attribute root dentries.
521 * shrink_dcache_for_umount will BUG if we don't release
522 * those before it's called so ->put_super is too late.
523 */
524 shrink_dcache_sb(s);
525
526 dput(REISERFS_SB(s)->xattr_root);
527 REISERFS_SB(s)->xattr_root = NULL;
528 dput(REISERFS_SB(s)->priv_root);
529 REISERFS_SB(s)->priv_root = NULL;
David Howellsedc666e2006-10-11 01:22:14 -0700530 }
531
532 kill_block_super(s);
533}
534
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700535static void reiserfs_put_super(struct super_block *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700537 struct reiserfs_transaction_handle th;
538 th.t_trans_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Christoph Hellwige0ccfd92010-05-19 07:16:42 -0400540 dquot_disable(s, -1, DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
541
Frederic Weisbecker8ebc4232009-04-07 04:19:49 +0200542 reiserfs_write_lock(s);
Christoph Hellwig6cfd0142009-05-05 15:40:36 +0200543
Jeff Mahoney098297b2014-04-23 10:00:36 -0400544 /*
545 * change file system state to current state if it was mounted
546 * with read-write permissions
547 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700548 if (!(s->s_flags & MS_RDONLY)) {
549 if (!journal_begin(&th, s, 10)) {
550 reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s),
551 1);
552 set_sb_umount_state(SB_DISK_SUPER_BLOCK(s),
553 REISERFS_SB(s)->s_mount_state);
Jeff Mahoney09f1b802014-04-23 10:00:39 -0400554 journal_mark_dirty(&th, SB_BUFFER_WITH_SB(s));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700555 }
556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Jeff Mahoney098297b2014-04-23 10:00:36 -0400558 /*
559 * note, journal_release checks for readonly mount, and can
560 * decide not to do a journal_end
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700561 */
562 journal_release(&th, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Jeff Mahoney50652272006-09-30 23:28:44 -0700564 reiserfs_free_bitmap_cache(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700566 brelse(SB_BUFFER_WITH_SB(s));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700568 print_statistics(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700570 if (REISERFS_SB(s)->reserved_blocks != 0) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -0400571 reiserfs_warning(s, "green-2005", "reserved blocks left %d",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700572 REISERFS_SB(s)->reserved_blocks);
573 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Frederic Weisbecker8ebc4232009-04-07 04:19:49 +0200575 reiserfs_write_unlock(s);
576 mutex_destroy(&REISERFS_SB(s)->lock);
Jeff Mahoney797d9012014-04-23 10:00:34 -0400577 destroy_workqueue(REISERFS_SB(s)->commit_wq);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700578 kfree(s->s_fs_info);
579 s->s_fs_info = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580}
581
Christoph Lametere18b8902006-12-06 20:33:20 -0800582static struct kmem_cache *reiserfs_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
584static struct inode *reiserfs_alloc_inode(struct super_block *sb)
585{
586 struct reiserfs_inode_info *ei;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700587 ei = (struct reiserfs_inode_info *)
Christoph Lametere94b1762006-12-06 20:33:17 -0800588 kmem_cache_alloc(reiserfs_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 if (!ei)
590 return NULL;
Al Viro0e4f6a72010-07-04 12:18:57 +0400591 atomic_set(&ei->openers, 0);
592 mutex_init(&ei->tailpack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 return &ei->vfs_inode;
594}
595
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100596static void reiserfs_i_callback(struct rcu_head *head)
597{
598 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100599 kmem_cache_free(reiserfs_inode_cachep, REISERFS_I(inode));
600}
601
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602static void reiserfs_destroy_inode(struct inode *inode)
603{
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100604 call_rcu(&inode->i_rcu, reiserfs_i_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605}
606
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700607static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700609 struct reiserfs_inode_info *ei = (struct reiserfs_inode_info *)foo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Christoph Lametera35afb82007-05-16 22:10:57 -0700611 INIT_LIST_HEAD(&ei->i_prealloc_list);
612 inode_init_once(&ei->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613}
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700614
Fabian Frederick31e14362014-04-03 14:50:37 -0700615static int __init init_inodecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
617 reiserfs_inode_cachep = kmem_cache_create("reiser_inode_cache",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700618 sizeof(struct
619 reiserfs_inode_info),
Paul Jacksonfffb60f2006-03-24 03:16:06 -0800620 0, (SLAB_RECLAIM_ACCOUNT|
621 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +0900622 init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 if (reiserfs_inode_cachep == NULL)
624 return -ENOMEM;
625 return 0;
626}
627
628static void destroy_inodecache(void)
629{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +1000630 /*
631 * Make sure all delayed rcu free inodes are flushed before we
632 * destroy cache.
633 */
634 rcu_barrier();
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -0700635 kmem_cache_destroy(reiserfs_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636}
637
638/* we don't mark inodes dirty, we just log them */
Christoph Hellwigaa385722011-05-27 06:53:02 -0400639static void reiserfs_dirty_inode(struct inode *inode, int flags)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700640{
641 struct reiserfs_transaction_handle th;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700643 int err = 0;
Frederic Weisbeckerdc8f6d82009-04-14 05:34:25 +0200644
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700645 if (inode->i_sb->s_flags & MS_RDONLY) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -0400646 reiserfs_warning(inode->i_sb, "clm-6006",
647 "writing inode %lu on readonly FS",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700648 inode->i_ino);
649 return;
650 }
Jeff Mahoney278f6672013-08-08 17:34:46 -0400651 reiserfs_write_lock(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Jeff Mahoney098297b2014-04-23 10:00:36 -0400653 /*
654 * this is really only used for atime updates, so they don't have
655 * to be included in O_SYNC or fsync
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700656 */
657 err = journal_begin(&th, inode->i_sb, 1);
Frederic Weisbeckerdc8f6d82009-04-14 05:34:25 +0200658 if (err)
659 goto out;
660
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700661 reiserfs_update_sd(&th, inode);
Jeff Mahoney58d85422014-04-23 10:00:38 -0400662 journal_end(&th);
Frederic Weisbeckerdc8f6d82009-04-14 05:34:25 +0200663
664out:
Jeff Mahoney278f6672013-08-08 17:34:46 -0400665 reiserfs_write_unlock(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666}
667
Jan Karac3aa0772011-12-21 20:17:10 +0100668static int reiserfs_show_options(struct seq_file *seq, struct dentry *root)
669{
670 struct super_block *s = root->d_sb;
671 struct reiserfs_journal *journal = SB_JOURNAL(s);
672 long opts = REISERFS_SB(s)->s_mount_opt;
673
674 if (opts & (1 << REISERFS_LARGETAIL))
675 seq_puts(seq, ",tails=on");
676 else if (!(opts & (1 << REISERFS_SMALLTAIL)))
677 seq_puts(seq, ",notail");
678 /* tails=small is default so we don't show it */
679
680 if (!(opts & (1 << REISERFS_BARRIER_FLUSH)))
681 seq_puts(seq, ",barrier=none");
682 /* barrier=flush is default so we don't show it */
683
684 if (opts & (1 << REISERFS_ERROR_CONTINUE))
685 seq_puts(seq, ",errors=continue");
686 else if (opts & (1 << REISERFS_ERROR_PANIC))
687 seq_puts(seq, ",errors=panic");
688 /* errors=ro is default so we don't show it */
689
690 if (opts & (1 << REISERFS_DATA_LOG))
691 seq_puts(seq, ",data=journal");
692 else if (opts & (1 << REISERFS_DATA_WRITEBACK))
693 seq_puts(seq, ",data=writeback");
694 /* data=ordered is default so we don't show it */
695
696 if (opts & (1 << REISERFS_ATTRS))
697 seq_puts(seq, ",attrs");
698
699 if (opts & (1 << REISERFS_XATTRS_USER))
700 seq_puts(seq, ",user_xattr");
701
702 if (opts & (1 << REISERFS_EXPOSE_PRIVROOT))
703 seq_puts(seq, ",expose_privroot");
704
705 if (opts & (1 << REISERFS_POSIXACL))
706 seq_puts(seq, ",acl");
707
708 if (REISERFS_SB(s)->s_jdev)
709 seq_printf(seq, ",jdev=%s", REISERFS_SB(s)->s_jdev);
710
711 if (journal->j_max_commit_age != journal->j_default_max_commit_age)
712 seq_printf(seq, ",commit=%d", journal->j_max_commit_age);
713
714#ifdef CONFIG_QUOTA
715 if (REISERFS_SB(s)->s_qf_names[USRQUOTA])
716 seq_printf(seq, ",usrjquota=%s", REISERFS_SB(s)->s_qf_names[USRQUOTA]);
717 else if (opts & (1 << REISERFS_USRQUOTA))
718 seq_puts(seq, ",usrquota");
719 if (REISERFS_SB(s)->s_qf_names[GRPQUOTA])
720 seq_printf(seq, ",grpjquota=%s", REISERFS_SB(s)->s_qf_names[GRPQUOTA]);
721 else if (opts & (1 << REISERFS_GRPQUOTA))
722 seq_puts(seq, ",grpquota");
723 if (REISERFS_SB(s)->s_jquota_fmt) {
724 if (REISERFS_SB(s)->s_jquota_fmt == QFMT_VFS_OLD)
725 seq_puts(seq, ",jqfmt=vfsold");
726 else if (REISERFS_SB(s)->s_jquota_fmt == QFMT_VFS_V0)
727 seq_puts(seq, ",jqfmt=vfsv0");
728 }
729#endif
730
731 /* Block allocator options */
732 if (opts & (1 << REISERFS_NO_BORDER))
733 seq_puts(seq, ",block-allocator=noborder");
734 if (opts & (1 << REISERFS_NO_UNHASHED_RELOCATION))
735 seq_puts(seq, ",block-allocator=no_unhashed_relocation");
736 if (opts & (1 << REISERFS_HASHED_RELOCATION))
737 seq_puts(seq, ",block-allocator=hashed_relocation");
738 if (opts & (1 << REISERFS_TEST4))
739 seq_puts(seq, ",block-allocator=test4");
740 show_alloc_options(seq, s);
741 return 0;
742}
743
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744#ifdef CONFIG_QUOTA
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700745static ssize_t reiserfs_quota_write(struct super_block *, int, const char *,
746 size_t, loff_t);
747static ssize_t reiserfs_quota_read(struct super_block *, int, char *, size_t,
748 loff_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749#endif
750
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800751static const struct super_operations reiserfs_sops = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700752 .alloc_inode = reiserfs_alloc_inode,
753 .destroy_inode = reiserfs_destroy_inode,
754 .write_inode = reiserfs_write_inode,
755 .dirty_inode = reiserfs_dirty_inode,
Al Viro845a2cc2010-06-07 11:37:37 -0400756 .evict_inode = reiserfs_evict_inode,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700757 .put_super = reiserfs_put_super,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700758 .sync_fs = reiserfs_sync_fs,
Takashi Satoc4be0c12009-01-09 16:40:58 -0800759 .freeze_fs = reiserfs_freeze,
760 .unfreeze_fs = reiserfs_unfreeze,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700761 .statfs = reiserfs_statfs,
762 .remount_fs = reiserfs_remount,
Jan Karac3aa0772011-12-21 20:17:10 +0100763 .show_options = reiserfs_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764#ifdef CONFIG_QUOTA
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700765 .quota_read = reiserfs_quota_read,
766 .quota_write = reiserfs_quota_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767#endif
768};
769
770#ifdef CONFIG_QUOTA
771#define QTYPE2NAME(t) ((t)==USRQUOTA?"user":"group")
772
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773static int reiserfs_write_dquot(struct dquot *);
774static int reiserfs_acquire_dquot(struct dquot *);
775static int reiserfs_release_dquot(struct dquot *);
776static int reiserfs_mark_dquot_dirty(struct dquot *);
777static int reiserfs_write_info(struct super_block *, int);
Jan Karaf00c9e42010-09-15 17:38:58 +0200778static int reiserfs_quota_on(struct super_block *, int, int, struct path *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
Alexey Dobriyan61e225d2009-09-21 17:01:08 -0700780static const struct dquot_operations reiserfs_quota_operations = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700781 .write_dquot = reiserfs_write_dquot,
782 .acquire_dquot = reiserfs_acquire_dquot,
783 .release_dquot = reiserfs_release_dquot,
784 .mark_dirty = reiserfs_mark_dquot_dirty,
785 .write_info = reiserfs_write_info,
Jan Kara41030032008-11-25 15:31:33 +0100786 .alloc_dquot = dquot_alloc,
787 .destroy_dquot = dquot_destroy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788};
789
Alexey Dobriyan0d54b212009-09-21 17:01:09 -0700790static const struct quotactl_ops reiserfs_qctl_operations = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700791 .quota_on = reiserfs_quota_on,
Christoph Hellwig287a8092010-05-19 07:16:45 -0400792 .quota_off = dquot_quota_off,
793 .quota_sync = dquot_quota_sync,
794 .get_info = dquot_get_dqinfo,
795 .set_info = dquot_set_dqinfo,
796 .get_dqblk = dquot_get_dqblk,
797 .set_dqblk = dquot_set_dqblk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798};
799#endif
800
Christoph Hellwig39655162007-10-21 16:42:17 -0700801static const struct export_operations reiserfs_export_ops = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700802 .encode_fh = reiserfs_encode_fh,
Christoph Hellwigbe55caf12007-10-21 16:42:13 -0700803 .fh_to_dentry = reiserfs_fh_to_dentry,
804 .fh_to_parent = reiserfs_fh_to_parent,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700805 .get_parent = reiserfs_get_parent,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700806};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Jeff Mahoney098297b2014-04-23 10:00:36 -0400808/*
809 * this struct is used in reiserfs_getopt () for containing the value for
810 * those mount options that have values rather than being toggles.
811 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812typedef struct {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700813 char *value;
Jeff Mahoney098297b2014-04-23 10:00:36 -0400814 /*
815 * bitmask which is to set on mount_options bitmask
816 * when this value is found, 0 is no bits are to be changed.
817 */
818 int setmask;
819 /*
820 * bitmask which is to clear on mount_options bitmask
821 * when this value is found, 0 is no bits are to be changed.
822 * This is applied BEFORE setmask
823 */
824 int clrmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825} arg_desc_t;
826
827/* Set this bit in arg_required to allow empty arguments */
828#define REISERFS_OPT_ALLOWEMPTY 31
829
Jeff Mahoney098297b2014-04-23 10:00:36 -0400830/*
831 * this struct is used in reiserfs_getopt() for describing the
832 * set of reiserfs mount options
833 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834typedef struct {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700835 char *option_name;
Jeff Mahoney098297b2014-04-23 10:00:36 -0400836
837 /* 0 if argument is not required, not 0 otherwise */
838 int arg_required;
839
840 /* list of values accepted by an option */
841 const arg_desc_t *values;
842
843 /*
844 * bitmask which is to set on mount_options bitmask
845 * when this value is found, 0 is no bits are to be changed.
846 */
847 int setmask;
848
849 /*
850 * bitmask which is to clear on mount_options bitmask
851 * when this value is found, 0 is no bits are to be changed.
852 * This is applied BEFORE setmask
853 */
854 int clrmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855} opt_desc_t;
856
857/* possible values for -o data= */
858static const arg_desc_t logging_mode[] = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700859 {"ordered", 1 << REISERFS_DATA_ORDERED,
860 (1 << REISERFS_DATA_LOG | 1 << REISERFS_DATA_WRITEBACK)},
861 {"journal", 1 << REISERFS_DATA_LOG,
862 (1 << REISERFS_DATA_ORDERED | 1 << REISERFS_DATA_WRITEBACK)},
863 {"writeback", 1 << REISERFS_DATA_WRITEBACK,
864 (1 << REISERFS_DATA_ORDERED | 1 << REISERFS_DATA_LOG)},
Vladimir V. Savelievcd02b962006-03-25 03:07:15 -0800865 {.value = NULL}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866};
867
868/* possible values for -o barrier= */
869static const arg_desc_t barrier_mode[] = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700870 {"none", 1 << REISERFS_BARRIER_NONE, 1 << REISERFS_BARRIER_FLUSH},
871 {"flush", 1 << REISERFS_BARRIER_FLUSH, 1 << REISERFS_BARRIER_NONE},
Vladimir V. Savelievcd02b962006-03-25 03:07:15 -0800872 {.value = NULL}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873};
874
Jeff Mahoney098297b2014-04-23 10:00:36 -0400875/*
876 * possible values for "-o block-allocator=" and bits which are to be set in
877 * s_mount_opt of reiserfs specific part of in-core super block
878 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879static const arg_desc_t balloc[] = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700880 {"noborder", 1 << REISERFS_NO_BORDER, 0},
881 {"border", 0, 1 << REISERFS_NO_BORDER},
882 {"no_unhashed_relocation", 1 << REISERFS_NO_UNHASHED_RELOCATION, 0},
883 {"hashed_relocation", 1 << REISERFS_HASHED_RELOCATION, 0},
884 {"test4", 1 << REISERFS_TEST4, 0},
885 {"notest4", 0, 1 << REISERFS_TEST4},
886 {NULL, 0, 0}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887};
888
889static const arg_desc_t tails[] = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700890 {"on", 1 << REISERFS_LARGETAIL, 1 << REISERFS_SMALLTAIL},
891 {"off", 0, (1 << REISERFS_LARGETAIL) | (1 << REISERFS_SMALLTAIL)},
892 {"small", 1 << REISERFS_SMALLTAIL, 1 << REISERFS_LARGETAIL},
893 {NULL, 0, 0}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894};
895
896static const arg_desc_t error_actions[] = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700897 {"panic", 1 << REISERFS_ERROR_PANIC,
898 (1 << REISERFS_ERROR_RO | 1 << REISERFS_ERROR_CONTINUE)},
899 {"ro-remount", 1 << REISERFS_ERROR_RO,
900 (1 << REISERFS_ERROR_PANIC | 1 << REISERFS_ERROR_CONTINUE)},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901#ifdef REISERFS_JOURNAL_ERROR_ALLOWS_NO_LOG
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700902 {"continue", 1 << REISERFS_ERROR_CONTINUE,
903 (1 << REISERFS_ERROR_PANIC | 1 << REISERFS_ERROR_RO)},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700905 {NULL, 0, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906};
907
Jeff Mahoney098297b2014-04-23 10:00:36 -0400908/*
909 * proceed only one option from a list *cur - string containing of mount
910 * options
911 * opts - array of options which are accepted
912 * opt_arg - if option is found and requires an argument and if it is specifed
913 * in the input - pointer to the argument is stored here
914 * bit_flags - if option requires to set a certain bit - it is set here
915 * return -1 if unknown option is found, opt->arg_required otherwise
916 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700917static int reiserfs_getopt(struct super_block *s, char **cur, opt_desc_t * opts,
918 char **opt_arg, unsigned long *bit_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700920 char *p;
Jeff Mahoney098297b2014-04-23 10:00:36 -0400921 /*
922 * foo=bar,
923 * ^ ^ ^
924 * | | +-- option_end
925 * | +-- arg_start
926 * +-- option_start
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700927 */
928 const opt_desc_t *opt;
929 const arg_desc_t *arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700931 p = *cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700933 /* assume argument cannot contain commas */
934 *cur = strchr(p, ',');
935 if (*cur) {
936 *(*cur) = '\0';
937 (*cur)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700940 if (!strncmp(p, "alloc=", 6)) {
Jeff Mahoney098297b2014-04-23 10:00:36 -0400941 /*
942 * Ugly special case, probably we should redo options
943 * parser so that it can understand several arguments for
944 * some options, also so that it can fill several bitfields
945 * with option values.
946 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700947 if (reiserfs_parse_alloc_options(s, p + 6)) {
948 return -1;
949 } else {
950 return 0;
951 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700953
954 /* for every option in the list */
955 for (opt = opts; opt->option_name; opt++) {
956 if (!strncmp(p, opt->option_name, strlen(opt->option_name))) {
957 if (bit_flags) {
958 if (opt->clrmask ==
959 (1 << REISERFS_UNSUPPORTED_OPT))
Jeff Mahoney45b03d52009-03-30 14:02:21 -0400960 reiserfs_warning(s, "super-6500",
961 "%s not supported.\n",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700962 p);
963 else
964 *bit_flags &= ~opt->clrmask;
965 if (opt->setmask ==
966 (1 << REISERFS_UNSUPPORTED_OPT))
Jeff Mahoney45b03d52009-03-30 14:02:21 -0400967 reiserfs_warning(s, "super-6501",
968 "%s not supported.\n",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700969 p);
970 else
971 *bit_flags |= opt->setmask;
972 }
973 break;
974 }
975 }
976 if (!opt->option_name) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -0400977 reiserfs_warning(s, "super-6502",
978 "unknown mount option \"%s\"", p);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700979 return -1;
980 }
981
982 p += strlen(opt->option_name);
983 switch (*p) {
984 case '=':
985 if (!opt->arg_required) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -0400986 reiserfs_warning(s, "super-6503",
987 "the option \"%s\" does not "
988 "require an argument\n",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700989 opt->option_name);
990 return -1;
991 }
992 break;
993
994 case 0:
995 if (opt->arg_required) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -0400996 reiserfs_warning(s, "super-6504",
997 "the option \"%s\" requires an "
998 "argument\n", opt->option_name);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700999 return -1;
1000 }
1001 break;
1002 default:
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001003 reiserfs_warning(s, "super-6505",
1004 "head of option \"%s\" is only correct\n",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001005 opt->option_name);
1006 return -1;
1007 }
1008
Jeff Mahoney098297b2014-04-23 10:00:36 -04001009 /*
1010 * move to the argument, or to next option if argument is not
1011 * required
1012 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001013 p++;
1014
1015 if (opt->arg_required
1016 && !(opt->arg_required & (1 << REISERFS_OPT_ALLOWEMPTY))
1017 && !strlen(p)) {
1018 /* this catches "option=," if not allowed */
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001019 reiserfs_warning(s, "super-6506",
1020 "empty argument for \"%s\"\n",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001021 opt->option_name);
1022 return -1;
1023 }
1024
1025 if (!opt->values) {
1026 /* *=NULLopt_arg contains pointer to argument */
1027 *opt_arg = p;
1028 return opt->arg_required & ~(1 << REISERFS_OPT_ALLOWEMPTY);
1029 }
1030
1031 /* values possible for this option are listed in opt->values */
1032 for (arg = opt->values; arg->value; arg++) {
1033 if (!strcmp(p, arg->value)) {
1034 if (bit_flags) {
1035 *bit_flags &= ~arg->clrmask;
1036 *bit_flags |= arg->setmask;
1037 }
1038 return opt->arg_required;
1039 }
1040 }
1041
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001042 reiserfs_warning(s, "super-6506",
1043 "bad value \"%s\" for option \"%s\"\n", p,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001044 opt->option_name);
1045 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046}
1047
1048/* returns 0 if something is wrong in option string, 1 - otherwise */
Jeff Mahoney098297b2014-04-23 10:00:36 -04001049static int reiserfs_parse_options(struct super_block *s,
1050
1051 /* string given via mount's -o */
1052 char *options,
1053
1054 /*
1055 * after the parsing phase, contains the
1056 * collection of bitflags defining what
1057 * mount options were selected.
1058 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001059 unsigned long *mount_options,
Jeff Mahoney098297b2014-04-23 10:00:36 -04001060
1061 /* strtol-ed from NNN of resize=NNN */
1062 unsigned long *blocks,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001063 char **jdev_name,
Jan Kara00b44192008-07-25 01:46:38 -07001064 unsigned int *commit_max_age,
1065 char **qf_names,
1066 unsigned int *qfmt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001068 int c;
1069 char *arg = NULL;
1070 char *pos;
1071 opt_desc_t opts[] = {
Jeff Mahoney098297b2014-04-23 10:00:36 -04001072 /*
1073 * Compatibility stuff, so that -o notail for old
1074 * setups still work
1075 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001076 {"tails",.arg_required = 't',.values = tails},
1077 {"notail",.clrmask =
1078 (1 << REISERFS_LARGETAIL) | (1 << REISERFS_SMALLTAIL)},
1079 {"conv",.setmask = 1 << REISERFS_CONVERT},
1080 {"attrs",.setmask = 1 << REISERFS_ATTRS},
1081 {"noattrs",.clrmask = 1 << REISERFS_ATTRS},
Jeff Mahoney73422812009-05-10 16:05:39 -04001082 {"expose_privroot", .setmask = 1 << REISERFS_EXPOSE_PRIVROOT},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083#ifdef CONFIG_REISERFS_FS_XATTR
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001084 {"user_xattr",.setmask = 1 << REISERFS_XATTRS_USER},
1085 {"nouser_xattr",.clrmask = 1 << REISERFS_XATTRS_USER},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086#else
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001087 {"user_xattr",.setmask = 1 << REISERFS_UNSUPPORTED_OPT},
1088 {"nouser_xattr",.clrmask = 1 << REISERFS_UNSUPPORTED_OPT},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089#endif
1090#ifdef CONFIG_REISERFS_FS_POSIX_ACL
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001091 {"acl",.setmask = 1 << REISERFS_POSIXACL},
1092 {"noacl",.clrmask = 1 << REISERFS_POSIXACL},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093#else
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001094 {"acl",.setmask = 1 << REISERFS_UNSUPPORTED_OPT},
1095 {"noacl",.clrmask = 1 << REISERFS_UNSUPPORTED_OPT},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096#endif
Vladimir V. Savelievcd02b962006-03-25 03:07:15 -08001097 {.option_name = "nolog"},
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001098 {"replayonly",.setmask = 1 << REPLAYONLY},
1099 {"block-allocator",.arg_required = 'a',.values = balloc},
1100 {"data",.arg_required = 'd',.values = logging_mode},
1101 {"barrier",.arg_required = 'b',.values = barrier_mode},
1102 {"resize",.arg_required = 'r',.values = NULL},
1103 {"jdev",.arg_required = 'j',.values = NULL},
1104 {"nolargeio",.arg_required = 'w',.values = NULL},
1105 {"commit",.arg_required = 'c',.values = NULL},
Jan Karac3aa0772011-12-21 20:17:10 +01001106 {"usrquota",.setmask = 1 << REISERFS_USRQUOTA},
1107 {"grpquota",.setmask = 1 << REISERFS_GRPQUOTA},
1108 {"noquota",.clrmask = 1 << REISERFS_USRQUOTA | 1 << REISERFS_GRPQUOTA},
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001109 {"errors",.arg_required = 'e',.values = error_actions},
1110 {"usrjquota",.arg_required =
1111 'u' | (1 << REISERFS_OPT_ALLOWEMPTY),.values = NULL},
1112 {"grpjquota",.arg_required =
1113 'g' | (1 << REISERFS_OPT_ALLOWEMPTY),.values = NULL},
1114 {"jqfmt",.arg_required = 'f',.values = NULL},
Vladimir V. Savelievcd02b962006-03-25 03:07:15 -08001115 {.option_name = NULL}
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001116 };
1117
1118 *blocks = 0;
1119 if (!options || !*options)
Jeff Mahoney098297b2014-04-23 10:00:36 -04001120 /*
1121 * use default configuration: create tails, journaling on, no
1122 * conversion to newest format
1123 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001124 return 1;
1125
1126 for (pos = options; pos;) {
1127 c = reiserfs_getopt(s, &pos, opts, &arg, mount_options);
1128 if (c == -1)
1129 /* wrong option is given */
1130 return 0;
1131
1132 if (c == 'r') {
1133 char *p;
1134
1135 p = NULL;
1136 /* "resize=NNN" or "resize=auto" */
1137
1138 if (!strcmp(arg, "auto")) {
1139 /* From JFS code, to auto-get the size. */
1140 *blocks =
1141 s->s_bdev->bd_inode->i_size >> s->
1142 s_blocksize_bits;
1143 } else {
1144 *blocks = simple_strtoul(arg, &p, 0);
1145 if (*p != '\0') {
1146 /* NNN does not look like a number */
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001147 reiserfs_warning(s, "super-6507",
1148 "bad value %s for "
1149 "-oresize\n", arg);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001150 return 0;
1151 }
1152 }
1153 }
1154
1155 if (c == 'c') {
1156 char *p = NULL;
1157 unsigned long val = simple_strtoul(arg, &p, 0);
1158 /* commit=NNN (time in seconds) */
1159 if (*p != '\0' || val >= (unsigned int)-1) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001160 reiserfs_warning(s, "super-6508",
1161 "bad value %s for -ocommit\n",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001162 arg);
1163 return 0;
1164 }
1165 *commit_max_age = (unsigned int)val;
1166 }
1167
1168 if (c == 'w') {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001169 reiserfs_warning(s, "super-6509", "nolargeio option "
1170 "is no longer supported");
Adrian Bunk36b756f2006-09-27 01:50:50 -07001171 return 0;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001172 }
1173
1174 if (c == 'j') {
1175 if (arg && *arg && jdev_name) {
Jeff Mahoney098297b2014-04-23 10:00:36 -04001176 /* Hm, already assigned? */
1177 if (*jdev_name) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001178 reiserfs_warning(s, "super-6510",
1179 "journal device was "
1180 "already specified to "
1181 "be %s", *jdev_name);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001182 return 0;
1183 }
1184 *jdev_name = arg;
1185 }
1186 }
1187#ifdef CONFIG_QUOTA
1188 if (c == 'u' || c == 'g') {
1189 int qtype = c == 'u' ? USRQUOTA : GRPQUOTA;
1190
Jan Kara6929f892008-08-20 18:16:36 +02001191 if (sb_any_quota_loaded(s) &&
Jan Kara00b44192008-07-25 01:46:38 -07001192 (!*arg != !REISERFS_SB(s)->s_qf_names[qtype])) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001193 reiserfs_warning(s, "super-6511",
1194 "cannot change journaled "
1195 "quota options when quota "
1196 "turned on.");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001197 return 0;
1198 }
1199 if (*arg) { /* Some filename specified? */
1200 if (REISERFS_SB(s)->s_qf_names[qtype]
1201 && strcmp(REISERFS_SB(s)->s_qf_names[qtype],
1202 arg)) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001203 reiserfs_warning(s, "super-6512",
1204 "%s quota file "
1205 "already specified.",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001206 QTYPE2NAME(qtype));
1207 return 0;
1208 }
1209 if (strchr(arg, '/')) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001210 reiserfs_warning(s, "super-6513",
1211 "quotafile must be "
1212 "on filesystem root.");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001213 return 0;
1214 }
Ionut-Gabriel Raduaf591ad2013-03-10 15:06:23 +02001215 qf_names[qtype] = kstrdup(arg, GFP_KERNEL);
Jan Kara00b44192008-07-25 01:46:38 -07001216 if (!qf_names[qtype]) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001217 reiserfs_warning(s, "reiserfs-2502",
1218 "not enough memory "
1219 "for storing "
1220 "quotafile name.");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001221 return 0;
1222 }
Jan Karac3aa0772011-12-21 20:17:10 +01001223 if (qtype == USRQUOTA)
1224 *mount_options |= 1 << REISERFS_USRQUOTA;
1225 else
1226 *mount_options |= 1 << REISERFS_GRPQUOTA;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001227 } else {
Jan Kara00b44192008-07-25 01:46:38 -07001228 if (qf_names[qtype] !=
1229 REISERFS_SB(s)->s_qf_names[qtype])
1230 kfree(qf_names[qtype]);
1231 qf_names[qtype] = NULL;
Jan Karac3aa0772011-12-21 20:17:10 +01001232 if (qtype == USRQUOTA)
1233 *mount_options &= ~(1 << REISERFS_USRQUOTA);
1234 else
1235 *mount_options &= ~(1 << REISERFS_GRPQUOTA);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001236 }
1237 }
1238 if (c == 'f') {
1239 if (!strcmp(arg, "vfsold"))
Jan Kara00b44192008-07-25 01:46:38 -07001240 *qfmt = QFMT_VFS_OLD;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001241 else if (!strcmp(arg, "vfsv0"))
Jan Kara00b44192008-07-25 01:46:38 -07001242 *qfmt = QFMT_VFS_V0;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001243 else {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001244 reiserfs_warning(s, "super-6514",
1245 "unknown quota format "
1246 "specified.");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001247 return 0;
1248 }
Jan Kara6929f892008-08-20 18:16:36 +02001249 if (sb_any_quota_loaded(s) &&
Jan Kara00b44192008-07-25 01:46:38 -07001250 *qfmt != REISERFS_SB(s)->s_jquota_fmt) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001251 reiserfs_warning(s, "super-6515",
1252 "cannot change journaled "
1253 "quota options when quota "
1254 "turned on.");
Jan Kara00b44192008-07-25 01:46:38 -07001255 return 0;
1256 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001257 }
1258#else
1259 if (c == 'u' || c == 'g' || c == 'f') {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001260 reiserfs_warning(s, "reiserfs-2503", "journaled "
1261 "quota options not supported.");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001262 return 0;
1263 }
1264#endif
1265 }
1266
1267#ifdef CONFIG_QUOTA
Jan Kara00b44192008-07-25 01:46:38 -07001268 if (!REISERFS_SB(s)->s_jquota_fmt && !*qfmt
1269 && (qf_names[USRQUOTA] || qf_names[GRPQUOTA])) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001270 reiserfs_warning(s, "super-6515",
1271 "journaled quota format not specified.");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001272 return 0;
1273 }
Jan Karac3aa0772011-12-21 20:17:10 +01001274 if ((!(*mount_options & (1 << REISERFS_USRQUOTA)) &&
1275 sb_has_quota_loaded(s, USRQUOTA)) ||
1276 (!(*mount_options & (1 << REISERFS_GRPQUOTA)) &&
1277 sb_has_quota_loaded(s, GRPQUOTA))) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001278 reiserfs_warning(s, "super-6516", "quota options must "
1279 "be present when quota is turned on.");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001280 return 0;
1281 }
1282#endif
1283
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285}
1286
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001287static void switch_data_mode(struct super_block *s, unsigned long mode)
1288{
1289 REISERFS_SB(s)->s_mount_opt &= ~((1 << REISERFS_DATA_LOG) |
1290 (1 << REISERFS_DATA_ORDERED) |
1291 (1 << REISERFS_DATA_WRITEBACK));
1292 REISERFS_SB(s)->s_mount_opt |= (1 << mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293}
1294
1295static void handle_data_mode(struct super_block *s, unsigned long mount_options)
1296{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001297 if (mount_options & (1 << REISERFS_DATA_LOG)) {
1298 if (!reiserfs_data_log(s)) {
1299 switch_data_mode(s, REISERFS_DATA_LOG);
1300 reiserfs_info(s, "switching to journaled data mode\n");
1301 }
1302 } else if (mount_options & (1 << REISERFS_DATA_ORDERED)) {
1303 if (!reiserfs_data_ordered(s)) {
1304 switch_data_mode(s, REISERFS_DATA_ORDERED);
1305 reiserfs_info(s, "switching to ordered data mode\n");
1306 }
1307 } else if (mount_options & (1 << REISERFS_DATA_WRITEBACK)) {
1308 if (!reiserfs_data_writeback(s)) {
1309 switch_data_mode(s, REISERFS_DATA_WRITEBACK);
1310 reiserfs_info(s, "switching to writeback data mode\n");
1311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313}
1314
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001315static void handle_barrier_mode(struct super_block *s, unsigned long bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001317 int flush = (1 << REISERFS_BARRIER_FLUSH);
1318 int none = (1 << REISERFS_BARRIER_NONE);
1319 int all_barrier = flush | none;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001321 if (bits & all_barrier) {
1322 REISERFS_SB(s)->s_mount_opt &= ~all_barrier;
1323 if (bits & flush) {
1324 REISERFS_SB(s)->s_mount_opt |= flush;
1325 printk("reiserfs: enabling write barrier flush mode\n");
1326 } else if (bits & none) {
1327 REISERFS_SB(s)->s_mount_opt |= none;
1328 printk("reiserfs: write barriers turned off\n");
1329 }
1330 }
1331}
1332
1333static void handle_attrs(struct super_block *s)
1334{
1335 struct reiserfs_super_block *rs = SB_DISK_SUPER_BLOCK(s);
1336
1337 if (reiserfs_attrs(s)) {
1338 if (old_format_only(s)) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001339 reiserfs_warning(s, "super-6517", "cannot support "
1340 "attributes on 3.5.x disk format");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001341 REISERFS_SB(s)->s_mount_opt &= ~(1 << REISERFS_ATTRS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 return;
1343 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001344 if (!(le32_to_cpu(rs->s_flags) & reiserfs_attrs_cleared)) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001345 reiserfs_warning(s, "super-6518", "cannot support "
1346 "attributes until flag is set in "
1347 "super-block");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001348 REISERFS_SB(s)->s_mount_opt &= ~(1 << REISERFS_ATTRS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 }
1350 }
1351}
1352
Jan Kara00b44192008-07-25 01:46:38 -07001353#ifdef CONFIG_QUOTA
1354static void handle_quota_files(struct super_block *s, char **qf_names,
1355 unsigned int *qfmt)
1356{
1357 int i;
1358
1359 for (i = 0; i < MAXQUOTAS; i++) {
1360 if (qf_names[i] != REISERFS_SB(s)->s_qf_names[i])
1361 kfree(REISERFS_SB(s)->s_qf_names[i]);
1362 REISERFS_SB(s)->s_qf_names[i] = qf_names[i];
1363 }
Jan Karaa06d7892011-12-21 17:35:34 +01001364 if (*qfmt)
1365 REISERFS_SB(s)->s_jquota_fmt = *qfmt;
Jan Kara00b44192008-07-25 01:46:38 -07001366}
1367#endif
1368
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001369static int reiserfs_remount(struct super_block *s, int *mount_flags, char *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001371 struct reiserfs_super_block *rs;
1372 struct reiserfs_transaction_handle th;
1373 unsigned long blocks;
1374 unsigned long mount_options = REISERFS_SB(s)->s_mount_opt;
1375 unsigned long safe_mask = 0;
1376 unsigned int commit_max_age = (unsigned int)-1;
1377 struct reiserfs_journal *journal = SB_JOURNAL(s);
Miklos Szeredicdf6ccc2008-02-08 04:21:47 -08001378 char *new_opts = kstrdup(arg, GFP_KERNEL);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001379 int err;
Jan Kara00b44192008-07-25 01:46:38 -07001380 char *qf_names[MAXQUOTAS];
1381 unsigned int qfmt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382#ifdef CONFIG_QUOTA
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001383 int i;
Frederic Weisbecker8ebc4232009-04-07 04:19:49 +02001384#endif
Jan Kara00b44192008-07-25 01:46:38 -07001385
Theodore Ts'o02b99842014-03-13 10:14:33 -04001386 sync_filesystem(s);
Frederic Weisbecker8ebc4232009-04-07 04:19:49 +02001387 reiserfs_write_lock(s);
1388
1389#ifdef CONFIG_QUOTA
Jan Kara00b44192008-07-25 01:46:38 -07001390 memcpy(qf_names, REISERFS_SB(s)->s_qf_names, sizeof(qf_names));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391#endif
1392
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001393 rs = SB_DISK_SUPER_BLOCK(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001395 if (!reiserfs_parse_options
Jan Kara00b44192008-07-25 01:46:38 -07001396 (s, arg, &mount_options, &blocks, NULL, &commit_max_age,
1397 qf_names, &qfmt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398#ifdef CONFIG_QUOTA
Jan Kara00b44192008-07-25 01:46:38 -07001399 for (i = 0; i < MAXQUOTAS; i++)
1400 if (qf_names[i] != REISERFS_SB(s)->s_qf_names[i])
1401 kfree(qf_names[i]);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001402#endif
Miklos Szeredicdf6ccc2008-02-08 04:21:47 -08001403 err = -EINVAL;
Jeff Mahoney4c051412013-08-08 17:27:19 -04001404 goto out_err_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 }
Jan Kara00b44192008-07-25 01:46:38 -07001406#ifdef CONFIG_QUOTA
1407 handle_quota_files(s, qf_names, &qfmt);
1408#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001410 handle_attrs(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001412 /* Add options that are safe here */
1413 safe_mask |= 1 << REISERFS_SMALLTAIL;
1414 safe_mask |= 1 << REISERFS_LARGETAIL;
1415 safe_mask |= 1 << REISERFS_NO_BORDER;
1416 safe_mask |= 1 << REISERFS_NO_UNHASHED_RELOCATION;
1417 safe_mask |= 1 << REISERFS_HASHED_RELOCATION;
1418 safe_mask |= 1 << REISERFS_TEST4;
1419 safe_mask |= 1 << REISERFS_ATTRS;
1420 safe_mask |= 1 << REISERFS_XATTRS_USER;
1421 safe_mask |= 1 << REISERFS_POSIXACL;
1422 safe_mask |= 1 << REISERFS_BARRIER_FLUSH;
1423 safe_mask |= 1 << REISERFS_BARRIER_NONE;
1424 safe_mask |= 1 << REISERFS_ERROR_RO;
1425 safe_mask |= 1 << REISERFS_ERROR_CONTINUE;
1426 safe_mask |= 1 << REISERFS_ERROR_PANIC;
Jan Karac3aa0772011-12-21 20:17:10 +01001427 safe_mask |= 1 << REISERFS_USRQUOTA;
1428 safe_mask |= 1 << REISERFS_GRPQUOTA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
Jeff Mahoney098297b2014-04-23 10:00:36 -04001430 /*
1431 * Update the bitmask, taking care to keep
1432 * the bits we're not allowed to change here
1433 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001434 REISERFS_SB(s)->s_mount_opt =
1435 (REISERFS_SB(s)->
1436 s_mount_opt & ~safe_mask) | (mount_options & safe_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001438 if (commit_max_age != 0 && commit_max_age != (unsigned int)-1) {
1439 journal->j_max_commit_age = commit_max_age;
1440 journal->j_max_trans_age = commit_max_age;
1441 } else if (commit_max_age == 0) {
1442 /* 0 means restore defaults. */
1443 journal->j_max_commit_age = journal->j_default_max_commit_age;
1444 journal->j_max_trans_age = JOURNAL_MAX_TRANS_AGE;
1445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001447 if (blocks) {
Miklos Szeredicdf6ccc2008-02-08 04:21:47 -08001448 err = reiserfs_resize(s, blocks);
1449 if (err != 0)
Jeff Mahoney4c051412013-08-08 17:27:19 -04001450 goto out_err_unlock;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001451 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001453 if (*mount_flags & MS_RDONLY) {
Jeff Mahoney4c051412013-08-08 17:27:19 -04001454 reiserfs_write_unlock(s);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001455 reiserfs_xattr_init(s, *mount_flags);
1456 /* remount read-only */
1457 if (s->s_flags & MS_RDONLY)
1458 /* it is read-only already */
Jeff Mahoney4c051412013-08-08 17:27:19 -04001459 goto out_ok_unlocked;
Christoph Hellwigc79d9672010-05-19 07:16:40 -04001460
Christoph Hellwig0f0dd622010-05-19 07:16:41 -04001461 err = dquot_suspend(s, -1);
1462 if (err < 0)
Christoph Hellwigc79d9672010-05-19 07:16:40 -04001463 goto out_err;
Christoph Hellwigc79d9672010-05-19 07:16:40 -04001464
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001465 /* try to remount file system with read-only permissions */
1466 if (sb_umount_state(rs) == REISERFS_VALID_FS
1467 || REISERFS_SB(s)->s_mount_state != REISERFS_VALID_FS) {
Jeff Mahoney4c051412013-08-08 17:27:19 -04001468 goto out_ok_unlocked;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001469 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
Jeff Mahoney4c051412013-08-08 17:27:19 -04001471 reiserfs_write_lock(s);
1472
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001473 err = journal_begin(&th, s, 10);
1474 if (err)
Jeff Mahoney4c051412013-08-08 17:27:19 -04001475 goto out_err_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001477 /* Mounting a rw partition read-only. */
1478 reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s), 1);
1479 set_sb_umount_state(rs, REISERFS_SB(s)->s_mount_state);
Jeff Mahoney09f1b802014-04-23 10:00:39 -04001480 journal_mark_dirty(&th, SB_BUFFER_WITH_SB(s));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001481 } else {
1482 /* remount read-write */
1483 if (!(s->s_flags & MS_RDONLY)) {
Jeff Mahoney4c051412013-08-08 17:27:19 -04001484 reiserfs_write_unlock(s);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001485 reiserfs_xattr_init(s, *mount_flags);
Jeff Mahoney4c051412013-08-08 17:27:19 -04001486 goto out_ok_unlocked; /* We are read-write already */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
Miklos Szeredicdf6ccc2008-02-08 04:21:47 -08001489 if (reiserfs_is_journal_aborted(journal)) {
1490 err = journal->j_errno;
Jeff Mahoney4c051412013-08-08 17:27:19 -04001491 goto out_err_unlock;
Miklos Szeredicdf6ccc2008-02-08 04:21:47 -08001492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001494 handle_data_mode(s, mount_options);
1495 handle_barrier_mode(s, mount_options);
1496 REISERFS_SB(s)->s_mount_state = sb_umount_state(rs);
Jeff Mahoney098297b2014-04-23 10:00:36 -04001497
1498 /* now it is safe to call journal_begin */
1499 s->s_flags &= ~MS_RDONLY;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001500 err = journal_begin(&th, s, 10);
1501 if (err)
Jeff Mahoney4c051412013-08-08 17:27:19 -04001502 goto out_err_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001504 /* Mount a partition which is read-only, read-write */
1505 reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s), 1);
1506 REISERFS_SB(s)->s_mount_state = sb_umount_state(rs);
1507 s->s_flags &= ~MS_RDONLY;
1508 set_sb_umount_state(rs, REISERFS_ERROR_FS);
Jeff Mahoney702d21c2009-03-30 14:02:16 -04001509 if (!old_format_only(s))
1510 set_sb_mnt_count(rs, sb_mnt_count(rs) + 1);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001511 /* mark_buffer_dirty (SB_BUFFER_WITH_SB (s), 1); */
Jeff Mahoney09f1b802014-04-23 10:00:39 -04001512 journal_mark_dirty(&th, SB_BUFFER_WITH_SB(s));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001513 REISERFS_SB(s)->s_mount_state = REISERFS_VALID_FS;
1514 }
1515 /* this will force a full flush of all journal lists */
1516 SB_JOURNAL(s)->j_must_wait = 1;
Jeff Mahoney58d85422014-04-23 10:00:38 -04001517 err = journal_end(&th);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001518 if (err)
Jeff Mahoney4c051412013-08-08 17:27:19 -04001519 goto out_err_unlock;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001520
Jeff Mahoney4c051412013-08-08 17:27:19 -04001521 reiserfs_write_unlock(s);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001522 if (!(*mount_flags & MS_RDONLY)) {
Christoph Hellwig0f0dd622010-05-19 07:16:41 -04001523 dquot_resume(s, -1);
Jan Kara3bb3e1f2012-11-13 14:55:52 +01001524 reiserfs_write_lock(s);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001525 finish_unfinished(s);
Jeff Mahoney4c051412013-08-08 17:27:19 -04001526 reiserfs_write_unlock(s);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001527 reiserfs_xattr_init(s, *mount_flags);
1528 }
1529
Jeff Mahoney4c051412013-08-08 17:27:19 -04001530out_ok_unlocked:
Al Viro2a32ceb2009-05-08 16:05:57 -04001531 replace_mount_options(s, new_opts);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001532 return 0;
Miklos Szeredicdf6ccc2008-02-08 04:21:47 -08001533
Jeff Mahoney4c051412013-08-08 17:27:19 -04001534out_err_unlock:
Jan Kara3bb3e1f2012-11-13 14:55:52 +01001535 reiserfs_write_unlock(s);
Miklos Szeredicdf6ccc2008-02-08 04:21:47 -08001536out_err:
1537 kfree(new_opts);
1538 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539}
1540
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001541static int read_super_block(struct super_block *s, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001543 struct buffer_head *bh;
1544 struct reiserfs_super_block *rs;
1545 int fs_blocksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001547 bh = sb_bread(s, offset / s->s_blocksize);
1548 if (!bh) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001549 reiserfs_warning(s, "sh-2006",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001550 "bread failed (dev %s, block %lu, size %lu)",
Al Viro2ccdc412013-11-22 13:25:39 -05001551 s->s_id, offset / s->s_blocksize,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001552 s->s_blocksize);
1553 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001556 rs = (struct reiserfs_super_block *)bh->b_data;
1557 if (!is_any_reiserfs_magic_string(rs)) {
1558 brelse(bh);
1559 return 1;
1560 }
Jeff Mahoney098297b2014-04-23 10:00:36 -04001561 /*
1562 * ok, reiserfs signature (old or new) found in at the given offset
1563 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001564 fs_blocksize = sb_blocksize(rs);
1565 brelse(bh);
1566 sb_set_blocksize(s, fs_blocksize);
1567
1568 bh = sb_bread(s, offset / s->s_blocksize);
1569 if (!bh) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001570 reiserfs_warning(s, "sh-2007",
1571 "bread failed (dev %s, block %lu, size %lu)",
Al Viro2ccdc412013-11-22 13:25:39 -05001572 s->s_id, offset / s->s_blocksize,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001573 s->s_blocksize);
1574 return 1;
1575 }
1576
1577 rs = (struct reiserfs_super_block *)bh->b_data;
1578 if (sb_blocksize(rs) != s->s_blocksize) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001579 reiserfs_warning(s, "sh-2011", "can't find a reiserfs "
1580 "filesystem on (dev %s, block %Lu, size %lu)",
Al Viro2ccdc412013-11-22 13:25:39 -05001581 s->s_id,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001582 (unsigned long long)bh->b_blocknr,
1583 s->s_blocksize);
1584 brelse(bh);
1585 return 1;
1586 }
1587
1588 if (rs->s_v1.s_root_block == cpu_to_le32(-1)) {
1589 brelse(bh);
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001590 reiserfs_warning(s, "super-6519", "Unfinished reiserfsck "
1591 "--rebuild-tree run detected. Please run\n"
1592 "reiserfsck --rebuild-tree and wait for a "
1593 "completion. If that fails\n"
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001594 "get newer reiserfsprogs package");
1595 return 1;
1596 }
1597
1598 SB_BUFFER_WITH_SB(s) = bh;
1599 SB_DISK_SUPER_BLOCK(s) = rs;
1600
Jeff Mahoney098297b2014-04-23 10:00:36 -04001601 /*
1602 * magic is of non-standard journal filesystem, look at s_version to
1603 * find which format is in use
1604 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001605 if (is_reiserfs_jr(rs)) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001606 if (sb_version(rs) == REISERFS_VERSION_2)
Jeff Mahoney1d889d92009-03-30 14:02:20 -04001607 reiserfs_info(s, "found reiserfs format \"3.6\""
1608 " with non-standard journal\n");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001609 else if (sb_version(rs) == REISERFS_VERSION_1)
Jeff Mahoney1d889d92009-03-30 14:02:20 -04001610 reiserfs_info(s, "found reiserfs format \"3.5\""
1611 " with non-standard journal\n");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001612 else {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001613 reiserfs_warning(s, "sh-2012", "found unknown "
1614 "format \"%u\" of reiserfs with "
1615 "non-standard magic", sb_version(rs));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001616 return 1;
1617 }
1618 } else
Jeff Mahoney098297b2014-04-23 10:00:36 -04001619 /*
1620 * s_version of standard format may contain incorrect
1621 * information, so we just look at the magic string
1622 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001623 reiserfs_info(s,
1624 "found reiserfs format \"%s\" with standard journal\n",
1625 is_reiserfs_3_5(rs) ? "3.5" : "3.6");
1626
1627 s->s_op = &reiserfs_sops;
1628 s->s_export_op = &reiserfs_export_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629#ifdef CONFIG_QUOTA
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001630 s->s_qcop = &reiserfs_qctl_operations;
1631 s->dq_op = &reiserfs_quota_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632#endif
1633
Jeff Mahoney098297b2014-04-23 10:00:36 -04001634 /*
1635 * new format is limited by the 32 bit wide i_blocks field, want to
1636 * be one full block below that.
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001637 */
1638 s->s_maxbytes = (512LL << 32) - s->s_blocksize;
1639 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640}
1641
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642/* after journal replay, reread all bitmap and super blocks */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001643static int reread_meta_blocks(struct super_block *s)
1644{
Jeff Mahoneya228bf82014-04-23 10:00:42 -04001645 ll_rw_block(READ, 1, &SB_BUFFER_WITH_SB(s));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001646 wait_on_buffer(SB_BUFFER_WITH_SB(s));
1647 if (!buffer_uptodate(SB_BUFFER_WITH_SB(s))) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001648 reiserfs_warning(s, "reiserfs-2504", "error reading the super");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001649 return 1;
1650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001652 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653}
1654
Jeff Mahoney098297b2014-04-23 10:00:36 -04001655/* hash detection stuff */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
Jeff Mahoney098297b2014-04-23 10:00:36 -04001657/*
1658 * if root directory is empty - we set default - Yura's - hash and
1659 * warn about it
1660 * FIXME: we look for only one name in a directory. If tea and yura
1661 * both have the same value - we ask user to send report to the
1662 * mailing list
1663 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001664static __u32 find_hash_out(struct super_block *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001666 int retval;
1667 struct inode *inode;
1668 struct cpu_key key;
1669 INITIALIZE_PATH(path);
1670 struct reiserfs_dir_entry de;
1671 __u32 hash = DEFAULT_HASH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001673 inode = s->s_root->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
Jeff Mahoney098297b2014-04-23 10:00:36 -04001675 do { /* Some serious "goto"-hater was there ;) */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001676 u32 teahash, r5hash, yurahash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001678 make_cpu_key(&key, inode, ~0, TYPE_DIRENTRY, 3);
1679 retval = search_by_entry_key(s, &key, &path, &de);
1680 if (retval == IO_ERROR) {
1681 pathrelse(&path);
1682 return UNSET_HASH;
1683 }
1684 if (retval == NAME_NOT_FOUND)
1685 de.de_entry_num--;
1686 set_de_name_and_namelen(&de);
1687 if (deh_offset(&(de.de_deh[de.de_entry_num])) == DOT_DOT_OFFSET) {
1688 /* allow override in this case */
1689 if (reiserfs_rupasov_hash(s)) {
1690 hash = YURA_HASH;
1691 }
Jeff Mahoney1d889d92009-03-30 14:02:20 -04001692 reiserfs_info(s, "FS seems to be empty, autodetect "
1693 "is using the default hash\n");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001694 break;
1695 }
1696 r5hash = GET_HASH_VALUE(r5_hash(de.de_name, de.de_namelen));
1697 teahash = GET_HASH_VALUE(keyed_hash(de.de_name, de.de_namelen));
1698 yurahash = GET_HASH_VALUE(yura_hash(de.de_name, de.de_namelen));
1699 if (((teahash == r5hash)
1700 &&
1701 (GET_HASH_VALUE(deh_offset(&(de.de_deh[de.de_entry_num])))
1702 == r5hash)) || ((teahash == yurahash)
1703 && (yurahash ==
1704 GET_HASH_VALUE(deh_offset
1705 (&
1706 (de.
1707 de_deh[de.
1708 de_entry_num])))))
1709 || ((r5hash == yurahash)
1710 && (yurahash ==
1711 GET_HASH_VALUE(deh_offset
1712 (&(de.de_deh[de.de_entry_num])))))) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001713 reiserfs_warning(s, "reiserfs-2506", "Unable to "
1714 "automatically detect hash function. "
1715 "Please mount with -o "
1716 "hash={tea,rupasov,r5}");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001717 hash = UNSET_HASH;
1718 break;
1719 }
1720 if (GET_HASH_VALUE(deh_offset(&(de.de_deh[de.de_entry_num]))) ==
1721 yurahash)
1722 hash = YURA_HASH;
1723 else if (GET_HASH_VALUE
1724 (deh_offset(&(de.de_deh[de.de_entry_num]))) == teahash)
1725 hash = TEA_HASH;
1726 else if (GET_HASH_VALUE
1727 (deh_offset(&(de.de_deh[de.de_entry_num]))) == r5hash)
1728 hash = R5_HASH;
1729 else {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001730 reiserfs_warning(s, "reiserfs-2506",
1731 "Unrecognised hash function");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001732 hash = UNSET_HASH;
1733 }
1734 } while (0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001736 pathrelse(&path);
1737 return hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738}
1739
Jeff Mahoney098297b2014-04-23 10:00:36 -04001740/* finds out which hash names are sorted with */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001741static int what_hash(struct super_block *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001743 __u32 code;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001745 code = sb_hash_function_code(SB_DISK_SUPER_BLOCK(s));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
Jeff Mahoney098297b2014-04-23 10:00:36 -04001747 /*
1748 * reiserfs_hash_detect() == true if any of the hash mount options
1749 * were used. We must check them to make sure the user isn't
1750 * using a bad hash value
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001751 */
1752 if (code == UNSET_HASH || reiserfs_hash_detect(s))
1753 code = find_hash_out(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001755 if (code != UNSET_HASH && reiserfs_hash_detect(s)) {
Jeff Mahoney098297b2014-04-23 10:00:36 -04001756 /*
1757 * detection has found the hash, and we must check against the
1758 * mount options
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001759 */
1760 if (reiserfs_rupasov_hash(s) && code != YURA_HASH) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001761 reiserfs_warning(s, "reiserfs-2507",
1762 "Error, %s hash detected, "
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001763 "unable to force rupasov hash",
1764 reiserfs_hashname(code));
1765 code = UNSET_HASH;
1766 } else if (reiserfs_tea_hash(s) && code != TEA_HASH) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001767 reiserfs_warning(s, "reiserfs-2508",
1768 "Error, %s hash detected, "
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001769 "unable to force tea hash",
1770 reiserfs_hashname(code));
1771 code = UNSET_HASH;
1772 } else if (reiserfs_r5_hash(s) && code != R5_HASH) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001773 reiserfs_warning(s, "reiserfs-2509",
1774 "Error, %s hash detected, "
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001775 "unable to force r5 hash",
1776 reiserfs_hashname(code));
1777 code = UNSET_HASH;
1778 }
1779 } else {
Jeff Mahoney098297b2014-04-23 10:00:36 -04001780 /*
1781 * find_hash_out was not called or
1782 * could not determine the hash
1783 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001784 if (reiserfs_rupasov_hash(s)) {
1785 code = YURA_HASH;
1786 } else if (reiserfs_tea_hash(s)) {
1787 code = TEA_HASH;
1788 } else if (reiserfs_r5_hash(s)) {
1789 code = R5_HASH;
1790 }
1791 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
Jeff Mahoney098297b2014-04-23 10:00:36 -04001793 /*
1794 * if we are mounted RW, and we have a new valid hash code, update
1795 * the super
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001796 */
1797 if (code != UNSET_HASH &&
1798 !(s->s_flags & MS_RDONLY) &&
1799 code != sb_hash_function_code(SB_DISK_SUPER_BLOCK(s))) {
1800 set_sb_hash_function_code(SB_DISK_SUPER_BLOCK(s), code);
1801 }
1802 return code;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803}
1804
Jeff Mahoney098297b2014-04-23 10:00:36 -04001805/* return pointer to appropriate function */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001806static hashf_t hash_function(struct super_block *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001808 switch (what_hash(s)) {
1809 case TEA_HASH:
1810 reiserfs_info(s, "Using tea hash to sort names\n");
1811 return keyed_hash;
1812 case YURA_HASH:
1813 reiserfs_info(s, "Using rupasov hash to sort names\n");
1814 return yura_hash;
1815 case R5_HASH:
1816 reiserfs_info(s, "Using r5 hash to sort names\n");
1817 return r5_hash;
1818 }
1819 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820}
1821
Jeff Mahoney098297b2014-04-23 10:00:36 -04001822/* this is used to set up correct value for old partitions */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001823static int function2code(hashf_t func)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001825 if (func == keyed_hash)
1826 return TEA_HASH;
1827 if (func == yura_hash)
1828 return YURA_HASH;
1829 if (func == r5_hash)
1830 return R5_HASH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831
Jeff Mahoney098297b2014-04-23 10:00:36 -04001832 BUG(); /* should never happen */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001834 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835}
1836
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001837#define SWARN(silent, s, id, ...) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 if (!(silent)) \
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001839 reiserfs_warning(s, id, __VA_ARGS__)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001841static int reiserfs_fill_super(struct super_block *s, void *data, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001843 struct inode *root_inode;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001844 struct reiserfs_transaction_handle th;
1845 int old_format = 0;
1846 unsigned long blocks;
1847 unsigned int commit_max_age = 0;
1848 int jinit_done = 0;
1849 struct reiserfs_iget_args args;
1850 struct reiserfs_super_block *rs;
1851 char *jdev_name;
1852 struct reiserfs_sb_info *sbi;
1853 int errval = -EINVAL;
Jan Kara00b44192008-07-25 01:46:38 -07001854 char *qf_names[MAXQUOTAS] = {};
1855 unsigned int qfmt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856
Miklos Szeredicdf6ccc2008-02-08 04:21:47 -08001857 save_mount_options(s, data);
1858
Yan Burman01afb212006-12-06 20:39:01 -08001859 sbi = kzalloc(sizeof(struct reiserfs_sb_info), GFP_KERNEL);
Jeff Mahoneyb7b7fa42010-03-29 15:12:39 -04001860 if (!sbi)
1861 return -ENOMEM;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001862 s->s_fs_info = sbi;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001863 /* Set default values for options: non-aggressive tails, RO on errors */
Artem Bityutskiyefaa33e2012-06-01 17:18:04 +03001864 sbi->s_mount_opt |= (1 << REISERFS_SMALLTAIL);
1865 sbi->s_mount_opt |= (1 << REISERFS_ERROR_RO);
1866 sbi->s_mount_opt |= (1 << REISERFS_BARRIER_FLUSH);
Jeff Mahoney098297b2014-04-23 10:00:36 -04001867 /* no preallocation minimum, be smart in reiserfs_file_write instead */
Artem Bityutskiyefaa33e2012-06-01 17:18:04 +03001868 sbi->s_alloc_options.preallocmin = 0;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001869 /* Preallocate by 16 blocks (17-1) at once */
Artem Bityutskiyefaa33e2012-06-01 17:18:04 +03001870 sbi->s_alloc_options.preallocsize = 17;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001871 /* setup default block allocator options */
1872 reiserfs_init_alloc_options(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873
Artem Bityutskiy033369d2012-06-01 17:18:08 +03001874 spin_lock_init(&sbi->old_work_lock);
1875 INIT_DELAYED_WORK(&sbi->old_work, flush_old_commits);
Artem Bityutskiyefaa33e2012-06-01 17:18:04 +03001876 mutex_init(&sbi->lock);
1877 sbi->lock_depth = -1;
Frederic Weisbecker8ebc4232009-04-07 04:19:49 +02001878
Jeff Mahoney797d9012014-04-23 10:00:34 -04001879 sbi->commit_wq = alloc_workqueue("reiserfs/%s", WQ_MEM_RECLAIM, 0,
1880 s->s_id);
1881 if (!sbi->commit_wq) {
1882 SWARN(silent, s, "", "Cannot allocate commit workqueue");
1883 errval = -ENOMEM;
1884 goto error_unlocked;
1885 }
1886
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001887 jdev_name = NULL;
1888 if (reiserfs_parse_options
Jeff Mahoneya228bf82014-04-23 10:00:42 -04001889 (s, (char *)data, &sbi->s_mount_opt, &blocks, &jdev_name,
Jan Kara00b44192008-07-25 01:46:38 -07001890 &commit_max_age, qf_names, &qfmt) == 0) {
Frederic Weisbeckerf32485b2012-01-10 15:11:07 -08001891 goto error_unlocked;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001892 }
Jan Karac3aa0772011-12-21 20:17:10 +01001893 if (jdev_name && jdev_name[0]) {
Artem Bityutskiyefaa33e2012-06-01 17:18:04 +03001894 sbi->s_jdev = kstrdup(jdev_name, GFP_KERNEL);
1895 if (!sbi->s_jdev) {
Jan Karac3aa0772011-12-21 20:17:10 +01001896 SWARN(silent, s, "", "Cannot allocate memory for "
1897 "journal device name");
1898 goto error;
1899 }
1900 }
Jan Kara00b44192008-07-25 01:46:38 -07001901#ifdef CONFIG_QUOTA
1902 handle_quota_files(s, qf_names, &qfmt);
1903#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001905 if (blocks) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001906 SWARN(silent, s, "jmacd-7", "resize option for remount only");
Frederic Weisbeckerf32485b2012-01-10 15:11:07 -08001907 goto error_unlocked;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001908 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909
Jeff Mahoney098297b2014-04-23 10:00:36 -04001910 /*
1911 * try old format (undistributed bitmap, super block in 8-th 1k
1912 * block of a device)
1913 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001914 if (!read_super_block(s, REISERFS_OLD_DISK_OFFSET_IN_BYTES))
1915 old_format = 1;
Jeff Mahoney098297b2014-04-23 10:00:36 -04001916
1917 /*
1918 * try new format (64-th 1k block), which can contain reiserfs
1919 * super block
1920 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001921 else if (read_super_block(s, REISERFS_DISK_OFFSET_IN_BYTES)) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001922 SWARN(silent, s, "sh-2021", "can not find reiserfs on %s",
Al Viro2ccdc412013-11-22 13:25:39 -05001923 s->s_id);
Frederic Weisbeckerf32485b2012-01-10 15:11:07 -08001924 goto error_unlocked;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001925 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001927 rs = SB_DISK_SUPER_BLOCK(s);
Jeff Mahoney098297b2014-04-23 10:00:36 -04001928 /*
1929 * Let's do basic sanity check to verify that underlying device is not
1930 * smaller than the filesystem. If the check fails then abort and
1931 * scream, because bad stuff will happen otherwise.
1932 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001933 if (s->s_bdev && s->s_bdev->bd_inode
1934 && i_size_read(s->s_bdev->bd_inode) <
1935 sb_block_count(rs) * sb_blocksize(rs)) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001936 SWARN(silent, s, "", "Filesystem cannot be "
1937 "mounted because it is bigger than the device");
1938 SWARN(silent, s, "", "You may need to run fsck "
1939 "or increase size of your LVM partition");
1940 SWARN(silent, s, "", "Or may be you forgot to "
1941 "reboot after fdisk when it told you to");
Frederic Weisbeckerf32485b2012-01-10 15:11:07 -08001942 goto error_unlocked;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001945 sbi->s_mount_state = SB_REISERFS_STATE(s);
1946 sbi->s_mount_state = REISERFS_VALID_FS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947
Jeff Mahoney6f010462006-09-30 23:28:43 -07001948 if ((errval = reiserfs_init_bitmap_cache(s))) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001949 SWARN(silent, s, "jmacd-8", "unable to read bitmap");
Frederic Weisbeckerf32485b2012-01-10 15:11:07 -08001950 goto error_unlocked;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001951 }
Frederic Weisbeckerf32485b2012-01-10 15:11:07 -08001952
Jeff Mahoneyd2c89a42006-11-02 22:07:20 -08001953 errval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954#ifdef CONFIG_REISERFS_CHECK
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001955 SWARN(silent, s, "", "CONFIG_REISERFS_CHECK is set ON");
1956 SWARN(silent, s, "", "- it is slow mode for debugging.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957#endif
1958
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001959 /* make data=ordered the default */
1960 if (!reiserfs_data_log(s) && !reiserfs_data_ordered(s) &&
1961 !reiserfs_data_writeback(s)) {
Artem Bityutskiyefaa33e2012-06-01 17:18:04 +03001962 sbi->s_mount_opt |= (1 << REISERFS_DATA_ORDERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 }
1964
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001965 if (reiserfs_data_log(s)) {
1966 reiserfs_info(s, "using journaled data mode\n");
1967 } else if (reiserfs_data_ordered(s)) {
1968 reiserfs_info(s, "using ordered data mode\n");
1969 } else {
1970 reiserfs_info(s, "using writeback data mode\n");
1971 }
1972 if (reiserfs_barrier_flush(s)) {
1973 printk("reiserfs: using flush barriers\n");
1974 }
Frederic Weisbeckerf32485b2012-01-10 15:11:07 -08001975
Frederic Weisbecker37c69b92012-01-10 15:11:09 -08001976 if (journal_init(s, jdev_name, old_format, commit_max_age)) {
1977 SWARN(silent, s, "sh-2022",
1978 "unable to initialize journal space");
1979 goto error_unlocked;
1980 } else {
Jeff Mahoney098297b2014-04-23 10:00:36 -04001981 /*
1982 * once this is set, journal_release must be called
1983 * if we error out of the mount
1984 */
1985 jinit_done = 1;
Frederic Weisbecker37c69b92012-01-10 15:11:09 -08001986 }
1987
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001988 if (reread_meta_blocks(s)) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001989 SWARN(silent, s, "jmacd-9",
1990 "unable to reread meta blocks after journal init");
Frederic Weisbecker9b467e62012-01-10 15:11:11 -08001991 goto error_unlocked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 }
1993
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001994 if (replay_only(s))
Frederic Weisbecker9b467e62012-01-10 15:11:11 -08001995 goto error_unlocked;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001996
1997 if (bdev_read_only(s->s_bdev) && !(s->s_flags & MS_RDONLY)) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001998 SWARN(silent, s, "clm-7000",
1999 "Detected readonly device, marking FS readonly");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002000 s->s_flags |= MS_RDONLY;
2001 }
2002 args.objectid = REISERFS_ROOT_OBJECTID;
2003 args.dirid = REISERFS_ROOT_PARENT_OBJECTID;
2004 root_inode =
2005 iget5_locked(s, REISERFS_ROOT_OBJECTID, reiserfs_find_actor,
Jeff Mahoneya228bf82014-04-23 10:00:42 -04002006 reiserfs_init_locked_inode, (void *)&args);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002007 if (!root_inode) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04002008 SWARN(silent, s, "jmacd-10", "get root inode failed");
Frederic Weisbecker9b467e62012-01-10 15:11:11 -08002009 goto error_unlocked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 }
2011
Frederic Weisbecker9b467e62012-01-10 15:11:11 -08002012 /*
2013 * This path assumed to be called with the BKL in the old times.
2014 * Now we have inherited the big reiserfs lock from it and many
2015 * reiserfs helpers called in the mount path and elsewhere require
2016 * this lock to be held even if it's not always necessary. Let's be
2017 * conservative and hold it early. The window can be reduced after
2018 * careful review of the code.
2019 */
2020 reiserfs_write_lock(s);
2021
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002022 if (root_inode->i_state & I_NEW) {
2023 reiserfs_read_locked_inode(root_inode, &args);
2024 unlock_new_inode(root_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 }
2026
Al Viro48fde702012-01-08 22:15:13 -05002027 s->s_root = d_make_root(root_inode);
2028 if (!s->s_root)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002029 goto error;
Jeff Mahoney098297b2014-04-23 10:00:36 -04002030 /* define and initialize hash function */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002031 sbi->s_hash_function = hash_function(s);
2032 if (sbi->s_hash_function == NULL) {
2033 dput(s->s_root);
2034 s->s_root = NULL;
2035 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002037
2038 if (is_reiserfs_3_5(rs)
2039 || (is_reiserfs_jr(rs) && SB_VERSION(s) == REISERFS_VERSION_1))
Jeff Mahoneya228bf82014-04-23 10:00:42 -04002040 set_bit(REISERFS_3_5, &sbi->s_properties);
Jeff Mahoneye1fabd32006-09-30 23:28:40 -07002041 else if (old_format)
Jeff Mahoneya228bf82014-04-23 10:00:42 -04002042 set_bit(REISERFS_OLD_FORMAT, &sbi->s_properties);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002043 else
Jeff Mahoneya228bf82014-04-23 10:00:42 -04002044 set_bit(REISERFS_3_6, &sbi->s_properties);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002045
2046 if (!(s->s_flags & MS_RDONLY)) {
2047
2048 errval = journal_begin(&th, s, 1);
2049 if (errval) {
2050 dput(s->s_root);
2051 s->s_root = NULL;
2052 goto error;
2053 }
2054 reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s), 1);
2055
2056 set_sb_umount_state(rs, REISERFS_ERROR_FS);
2057 set_sb_fs_state(rs, 0);
2058
Jeff Mahoney098297b2014-04-23 10:00:36 -04002059 /*
2060 * Clear out s_bmap_nr if it would wrap. We can handle this
Jeff Mahoneycb680c12007-10-18 23:39:27 -07002061 * case, but older revisions can't. This will cause the
2062 * file system to fail mount on those older implementations,
Jeff Mahoney098297b2014-04-23 10:00:36 -04002063 * avoiding corruption. -jeffm
2064 */
Jeff Mahoneycb680c12007-10-18 23:39:27 -07002065 if (bmap_would_wrap(reiserfs_bmap_count(s)) &&
2066 sb_bmap_nr(rs) != 0) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04002067 reiserfs_warning(s, "super-2030", "This file system "
Jeff Mahoneycb680c12007-10-18 23:39:27 -07002068 "claims to use %u bitmap blocks in "
2069 "its super block, but requires %u. "
2070 "Clearing to zero.", sb_bmap_nr(rs),
2071 reiserfs_bmap_count(s));
2072
2073 set_sb_bmap_nr(rs, 0);
2074 }
2075
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002076 if (old_format_only(s)) {
Jeff Mahoney098297b2014-04-23 10:00:36 -04002077 /*
2078 * filesystem of format 3.5 either with standard
2079 * or non-standard journal
2080 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002081 if (convert_reiserfs(s)) {
2082 /* and -o conv is given */
2083 if (!silent)
2084 reiserfs_info(s,
2085 "converting 3.5 filesystem to the 3.6 format");
2086
2087 if (is_reiserfs_3_5(rs))
Jeff Mahoney098297b2014-04-23 10:00:36 -04002088 /*
2089 * put magic string of 3.6 format.
2090 * 2.2 will not be able to
2091 * mount this filesystem anymore
2092 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002093 memcpy(rs->s_v1.s_magic,
2094 reiserfs_3_6_magic_string,
2095 sizeof
2096 (reiserfs_3_6_magic_string));
2097
2098 set_sb_version(rs, REISERFS_VERSION_2);
2099 reiserfs_convert_objectid_map_v1(s);
Jeff Mahoneya228bf82014-04-23 10:00:42 -04002100 set_bit(REISERFS_3_6, &sbi->s_properties);
2101 clear_bit(REISERFS_3_5, &sbi->s_properties);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002102 } else if (!silent) {
2103 reiserfs_info(s, "using 3.5.x disk format\n");
2104 }
Jeff Mahoney702d21c2009-03-30 14:02:16 -04002105 } else
2106 set_sb_mnt_count(rs, sb_mnt_count(rs) + 1);
2107
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002108
Jeff Mahoney09f1b802014-04-23 10:00:39 -04002109 journal_mark_dirty(&th, SB_BUFFER_WITH_SB(s));
Jeff Mahoney58d85422014-04-23 10:00:38 -04002110 errval = journal_end(&th);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002111 if (errval) {
2112 dput(s->s_root);
2113 s->s_root = NULL;
2114 goto error;
2115 }
2116
Jeff Mahoney4c051412013-08-08 17:27:19 -04002117 reiserfs_write_unlock(s);
Al Viroedcc37a2009-05-03 06:00:05 -04002118 if ((errval = reiserfs_lookup_privroot(s)) ||
2119 (errval = reiserfs_xattr_init(s, s->s_flags))) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002120 dput(s->s_root);
2121 s->s_root = NULL;
Jeff Mahoney4c051412013-08-08 17:27:19 -04002122 goto error_unlocked;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002123 }
Jeff Mahoney4c051412013-08-08 17:27:19 -04002124 reiserfs_write_lock(s);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002125
Jeff Mahoney098297b2014-04-23 10:00:36 -04002126 /*
2127 * look for files which were to be removed in previous session
2128 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002129 finish_unfinished(s);
2130 } else {
2131 if (old_format_only(s) && !silent) {
2132 reiserfs_info(s, "using 3.5.x disk format\n");
2133 }
2134
Jeff Mahoney4c051412013-08-08 17:27:19 -04002135 reiserfs_write_unlock(s);
Al Viroedcc37a2009-05-03 06:00:05 -04002136 if ((errval = reiserfs_lookup_privroot(s)) ||
2137 (errval = reiserfs_xattr_init(s, s->s_flags))) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002138 dput(s->s_root);
2139 s->s_root = NULL;
Jeff Mahoney4c051412013-08-08 17:27:19 -04002140 goto error_unlocked;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002141 }
Jeff Mahoney4c051412013-08-08 17:27:19 -04002142 reiserfs_write_lock(s);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002143 }
Jeff Mahoney098297b2014-04-23 10:00:36 -04002144 /*
2145 * mark hash in super block: it could be unset. overwrite should be ok
2146 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002147 set_sb_hash_function_code(rs, function2code(sbi->s_hash_function));
2148
2149 handle_attrs(s);
2150
2151 reiserfs_proc_info_init(s);
2152
2153 init_waitqueue_head(&(sbi->s_wait));
2154 spin_lock_init(&sbi->bitmap_lock);
2155
Frederic Weisbecker8ebc4232009-04-07 04:19:49 +02002156 reiserfs_write_unlock(s);
2157
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002158 return (0);
2159
Jan Kara00b44192008-07-25 01:46:38 -07002160error:
Jeff Mahoneyb7b7fa42010-03-29 15:12:39 -04002161 reiserfs_write_unlock(s);
2162
Frederic Weisbeckerf32485b2012-01-10 15:11:07 -08002163error_unlocked:
2164 /* kill the commit thread, free journal ram */
2165 if (jinit_done) {
2166 reiserfs_write_lock(s);
2167 journal_release_error(NULL, s);
2168 reiserfs_write_unlock(s);
2169 }
2170
Artem Bityutskiy033369d2012-06-01 17:18:08 +03002171 cancel_delayed_work_sync(&REISERFS_SB(s)->old_work);
2172
Jeff Mahoney50652272006-09-30 23:28:44 -07002173 reiserfs_free_bitmap_cache(s);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002174 if (SB_BUFFER_WITH_SB(s))
2175 brelse(SB_BUFFER_WITH_SB(s));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176#ifdef CONFIG_QUOTA
Jeff Mahoney50652272006-09-30 23:28:44 -07002177 {
2178 int j;
Jan Kara00b44192008-07-25 01:46:38 -07002179 for (j = 0; j < MAXQUOTAS; j++)
2180 kfree(qf_names[j]);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002181 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182#endif
James Lamanna833d3042005-10-30 15:00:16 -08002183 kfree(sbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002185 s->s_fs_info = NULL;
2186 return errval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187}
2188
David Howells726c3342006-06-23 02:02:58 -07002189static int reiserfs_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190{
David Howells726c3342006-06-23 02:02:58 -07002191 struct reiserfs_super_block *rs = SB_DISK_SUPER_BLOCK(dentry->d_sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002192
2193 buf->f_namelen = (REISERFS_MAX_NAME(s->s_blocksize));
2194 buf->f_bfree = sb_free_blocks(rs);
2195 buf->f_bavail = buf->f_bfree;
2196 buf->f_blocks = sb_block_count(rs) - sb_bmap_nr(rs) - 1;
David Howells726c3342006-06-23 02:02:58 -07002197 buf->f_bsize = dentry->d_sb->s_blocksize;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002198 /* changed to accommodate gcc folks. */
2199 buf->f_type = REISERFS_SUPER_MAGIC;
Coly Li651d0622009-04-02 16:59:41 -07002200 buf->f_fsid.val[0] = (u32)crc32_le(0, rs->s_uuid, sizeof(rs->s_uuid)/2);
2201 buf->f_fsid.val[1] = (u32)crc32_le(0, rs->s_uuid + sizeof(rs->s_uuid)/2,
2202 sizeof(rs->s_uuid)/2);
2203
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002204 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205}
2206
2207#ifdef CONFIG_QUOTA
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208static int reiserfs_write_dquot(struct dquot *dquot)
2209{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002210 struct reiserfs_transaction_handle th;
2211 int ret, err;
Jeff Mahoneyd2d03952013-08-08 17:34:47 -04002212 int depth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002214 reiserfs_write_lock(dquot->dq_sb);
2215 ret =
2216 journal_begin(&th, dquot->dq_sb,
2217 REISERFS_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
2218 if (ret)
2219 goto out;
Jeff Mahoneyd2d03952013-08-08 17:34:47 -04002220 depth = reiserfs_write_unlock_nested(dquot->dq_sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002221 ret = dquot_commit(dquot);
Jeff Mahoneyd2d03952013-08-08 17:34:47 -04002222 reiserfs_write_lock_nested(dquot->dq_sb, depth);
Jeff Mahoney58d85422014-04-23 10:00:38 -04002223 err = journal_end(&th);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002224 if (!ret && err)
2225 ret = err;
Jan Kara7af11682012-11-13 17:05:14 +01002226out:
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002227 reiserfs_write_unlock(dquot->dq_sb);
2228 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229}
2230
2231static int reiserfs_acquire_dquot(struct dquot *dquot)
2232{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002233 struct reiserfs_transaction_handle th;
2234 int ret, err;
Jeff Mahoneyd2d03952013-08-08 17:34:47 -04002235 int depth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002237 reiserfs_write_lock(dquot->dq_sb);
2238 ret =
2239 journal_begin(&th, dquot->dq_sb,
2240 REISERFS_QUOTA_INIT_BLOCKS(dquot->dq_sb));
2241 if (ret)
2242 goto out;
Jeff Mahoneyd2d03952013-08-08 17:34:47 -04002243 depth = reiserfs_write_unlock_nested(dquot->dq_sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002244 ret = dquot_acquire(dquot);
Jeff Mahoneyd2d03952013-08-08 17:34:47 -04002245 reiserfs_write_lock_nested(dquot->dq_sb, depth);
Jeff Mahoney58d85422014-04-23 10:00:38 -04002246 err = journal_end(&th);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002247 if (!ret && err)
2248 ret = err;
Jan Kara7af11682012-11-13 17:05:14 +01002249out:
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002250 reiserfs_write_unlock(dquot->dq_sb);
2251 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252}
2253
2254static int reiserfs_release_dquot(struct dquot *dquot)
2255{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002256 struct reiserfs_transaction_handle th;
2257 int ret, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002259 reiserfs_write_lock(dquot->dq_sb);
2260 ret =
2261 journal_begin(&th, dquot->dq_sb,
2262 REISERFS_QUOTA_DEL_BLOCKS(dquot->dq_sb));
Jan Kara7af11682012-11-13 17:05:14 +01002263 reiserfs_write_unlock(dquot->dq_sb);
Jan Kara9c3013e2007-09-11 15:23:29 -07002264 if (ret) {
2265 /* Release dquot anyway to avoid endless cycle in dqput() */
2266 dquot_release(dquot);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002267 goto out;
Jan Kara9c3013e2007-09-11 15:23:29 -07002268 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002269 ret = dquot_release(dquot);
Jan Kara7af11682012-11-13 17:05:14 +01002270 reiserfs_write_lock(dquot->dq_sb);
Jeff Mahoney58d85422014-04-23 10:00:38 -04002271 err = journal_end(&th);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002272 if (!ret && err)
2273 ret = err;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002274 reiserfs_write_unlock(dquot->dq_sb);
Jan Kara7af11682012-11-13 17:05:14 +01002275out:
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002276 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277}
2278
2279static int reiserfs_mark_dquot_dirty(struct dquot *dquot)
2280{
Jan Kara45065672008-07-25 01:46:37 -07002281 /* Are we journaling quotas? */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002282 if (REISERFS_SB(dquot->dq_sb)->s_qf_names[USRQUOTA] ||
2283 REISERFS_SB(dquot->dq_sb)->s_qf_names[GRPQUOTA]) {
2284 dquot_mark_dquot_dirty(dquot);
2285 return reiserfs_write_dquot(dquot);
2286 } else
2287 return dquot_mark_dquot_dirty(dquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288}
2289
2290static int reiserfs_write_info(struct super_block *sb, int type)
2291{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002292 struct reiserfs_transaction_handle th;
2293 int ret, err;
Jeff Mahoneyd2d03952013-08-08 17:34:47 -04002294 int depth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002296 /* Data block + inode block */
2297 reiserfs_write_lock(sb);
2298 ret = journal_begin(&th, sb, 2);
2299 if (ret)
2300 goto out;
Jeff Mahoneyd2d03952013-08-08 17:34:47 -04002301 depth = reiserfs_write_unlock_nested(sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002302 ret = dquot_commit_info(sb, type);
Jeff Mahoneyd2d03952013-08-08 17:34:47 -04002303 reiserfs_write_lock_nested(sb, depth);
Jeff Mahoney58d85422014-04-23 10:00:38 -04002304 err = journal_end(&th);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002305 if (!ret && err)
2306 ret = err;
Jan Kara7af11682012-11-13 17:05:14 +01002307out:
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002308 reiserfs_write_unlock(sb);
2309 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310}
2311
2312/*
Christoph Hellwig84de8562005-06-23 00:09:16 -07002313 * Turn on quotas during mount time - we need to find the quota file and such...
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 */
2315static int reiserfs_quota_on_mount(struct super_block *sb, int type)
2316{
Christoph Hellwig287a8092010-05-19 07:16:45 -04002317 return dquot_quota_on_mount(sb, REISERFS_SB(sb)->s_qf_names[type],
2318 REISERFS_SB(sb)->s_jquota_fmt, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319}
2320
2321/*
2322 * Standard function to be called on quota_on
2323 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002324static int reiserfs_quota_on(struct super_block *sb, int type, int format_id,
Jan Karaf00c9e42010-09-15 17:38:58 +02002325 struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002327 int err;
Jan Karad5dee5c2008-04-28 02:16:23 -07002328 struct inode *inode;
Jan Kara5d4f7fd2008-07-25 01:46:36 -07002329 struct reiserfs_transaction_handle th;
Jan Karac3aa0772011-12-21 20:17:10 +01002330 int opt = type == USRQUOTA ? REISERFS_USRQUOTA : REISERFS_GRPQUOTA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331
Jan Karab9e06ef2012-11-13 16:34:17 +01002332 reiserfs_write_lock(sb);
2333 if (!(REISERFS_SB(sb)->s_mount_opt & (1 << opt))) {
2334 err = -EINVAL;
2335 goto out;
2336 }
Christoph Hellwig307ae182010-05-19 07:16:43 -04002337
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002338 /* Quotafile not on the same filesystem? */
Al Virod8c95842011-12-07 18:16:57 -05002339 if (path->dentry->d_sb != sb) {
Al Viro77e69da2008-08-01 04:29:18 -04002340 err = -EXDEV;
2341 goto out;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002342 }
Jan Karaf00c9e42010-09-15 17:38:58 +02002343 inode = path->dentry->d_inode;
Jeff Mahoney098297b2014-04-23 10:00:36 -04002344 /*
2345 * We must not pack tails for quota files on reiserfs for quota
2346 * IO to work
2347 */
Jan Karad5dee5c2008-04-28 02:16:23 -07002348 if (!(REISERFS_I(inode)->i_flags & i_nopack_mask)) {
2349 err = reiserfs_unpack(inode, NULL);
2350 if (err) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04002351 reiserfs_warning(sb, "super-6520",
2352 "Unpacking tail of quota file failed"
Jan Karad5dee5c2008-04-28 02:16:23 -07002353 " (%d). Cannot turn on quotas.", err);
Al Viro77e69da2008-08-01 04:29:18 -04002354 err = -EINVAL;
2355 goto out;
Jan Karad5dee5c2008-04-28 02:16:23 -07002356 }
2357 mark_inode_dirty(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002358 }
Jan Kara5d4f7fd2008-07-25 01:46:36 -07002359 /* Journaling quota? */
2360 if (REISERFS_SB(sb)->s_qf_names[type]) {
2361 /* Quotafile not of fs root? */
Jan Karaf00c9e42010-09-15 17:38:58 +02002362 if (path->dentry->d_parent != sb->s_root)
Jeff Mahoney45b03d52009-03-30 14:02:21 -04002363 reiserfs_warning(sb, "super-6521",
2364 "Quota file not on filesystem root. "
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002365 "Journalled quota will not work.");
Jan Kara5d4f7fd2008-07-25 01:46:36 -07002366 }
2367
2368 /*
2369 * When we journal data on quota file, we have to flush journal to see
2370 * all updates to the file when we bypass pagecache...
2371 */
2372 if (reiserfs_file_data_log(inode)) {
2373 /* Just start temporary transaction and finish it */
2374 err = journal_begin(&th, sb, 1);
2375 if (err)
Al Viro77e69da2008-08-01 04:29:18 -04002376 goto out;
Jeff Mahoney58d85422014-04-23 10:00:38 -04002377 err = journal_end_sync(&th);
Jan Kara5d4f7fd2008-07-25 01:46:36 -07002378 if (err)
Al Viro77e69da2008-08-01 04:29:18 -04002379 goto out;
Jan Kara5d4f7fd2008-07-25 01:46:36 -07002380 }
Jan Karab9e06ef2012-11-13 16:34:17 +01002381 reiserfs_write_unlock(sb);
2382 return dquot_quota_on(sb, type, format_id, path);
Al Viro77e69da2008-08-01 04:29:18 -04002383out:
Jan Karab9e06ef2012-11-13 16:34:17 +01002384 reiserfs_write_unlock(sb);
Al Viro77e69da2008-08-01 04:29:18 -04002385 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386}
2387
Jeff Mahoney098297b2014-04-23 10:00:36 -04002388/*
2389 * Read data from quotafile - avoid pagecache and such because we cannot afford
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 * acquiring the locks... As quota files are never truncated and quota code
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002391 * itself serializes the operations (and no one else should touch the files)
Jeff Mahoney098297b2014-04-23 10:00:36 -04002392 * we don't have to be afraid of races
2393 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394static ssize_t reiserfs_quota_read(struct super_block *sb, int type, char *data,
2395 size_t len, loff_t off)
2396{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002397 struct inode *inode = sb_dqopt(sb)->files[type];
2398 unsigned long blk = off >> sb->s_blocksize_bits;
2399 int err = 0, offset = off & (sb->s_blocksize - 1), tocopy;
2400 size_t toread;
2401 struct buffer_head tmp_bh, *bh;
2402 loff_t i_size = i_size_read(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002404 if (off > i_size)
2405 return 0;
2406 if (off + len > i_size)
2407 len = i_size - off;
2408 toread = len;
2409 while (toread > 0) {
2410 tocopy =
2411 sb->s_blocksize - offset <
2412 toread ? sb->s_blocksize - offset : toread;
2413 tmp_bh.b_state = 0;
Jeff Mahoney098297b2014-04-23 10:00:36 -04002414 /*
2415 * Quota files are without tails so we can safely
2416 * use this function
2417 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002418 reiserfs_write_lock(sb);
2419 err = reiserfs_get_block(inode, blk, &tmp_bh, 0);
2420 reiserfs_write_unlock(sb);
2421 if (err)
2422 return err;
2423 if (!buffer_mapped(&tmp_bh)) /* A hole? */
2424 memset(data, 0, tocopy);
2425 else {
2426 bh = sb_bread(sb, tmp_bh.b_blocknr);
2427 if (!bh)
2428 return -EIO;
2429 memcpy(data, bh->b_data + offset, tocopy);
2430 brelse(bh);
2431 }
2432 offset = 0;
2433 toread -= tocopy;
2434 data += tocopy;
2435 blk++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002437 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438}
2439
Jeff Mahoney098297b2014-04-23 10:00:36 -04002440/*
2441 * Write to quotafile (we know the transaction is already started and has
2442 * enough credits)
2443 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444static ssize_t reiserfs_quota_write(struct super_block *sb, int type,
2445 const char *data, size_t len, loff_t off)
2446{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002447 struct inode *inode = sb_dqopt(sb)->files[type];
2448 unsigned long blk = off >> sb->s_blocksize_bits;
2449 int err = 0, offset = off & (sb->s_blocksize - 1), tocopy;
2450 int journal_quota = REISERFS_SB(sb)->s_qf_names[type] != NULL;
2451 size_t towrite = len;
2452 struct buffer_head tmp_bh, *bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453
Jan Kara9c3013e2007-09-11 15:23:29 -07002454 if (!current->journal_info) {
2455 printk(KERN_WARNING "reiserfs: Quota write (off=%Lu, len=%Lu)"
2456 " cancelled because transaction is not started.\n",
2457 (unsigned long long)off, (unsigned long long)len);
2458 return -EIO;
2459 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002460 while (towrite > 0) {
2461 tocopy = sb->s_blocksize - offset < towrite ?
2462 sb->s_blocksize - offset : towrite;
2463 tmp_bh.b_state = 0;
Jan Kara361d94a2012-11-13 18:25:38 +01002464 reiserfs_write_lock(sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002465 err = reiserfs_get_block(inode, blk, &tmp_bh, GET_BLOCK_CREATE);
Jan Kara361d94a2012-11-13 18:25:38 +01002466 reiserfs_write_unlock(sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002467 if (err)
2468 goto out;
2469 if (offset || tocopy != sb->s_blocksize)
2470 bh = sb_bread(sb, tmp_bh.b_blocknr);
2471 else
2472 bh = sb_getblk(sb, tmp_bh.b_blocknr);
2473 if (!bh) {
2474 err = -EIO;
2475 goto out;
2476 }
2477 lock_buffer(bh);
2478 memcpy(bh->b_data + offset, data, tocopy);
2479 flush_dcache_page(bh->b_page);
2480 set_buffer_uptodate(bh);
2481 unlock_buffer(bh);
Jan Kara361d94a2012-11-13 18:25:38 +01002482 reiserfs_write_lock(sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002483 reiserfs_prepare_for_journal(sb, bh, 1);
Jeff Mahoney09f1b802014-04-23 10:00:39 -04002484 journal_mark_dirty(current->journal_info, bh);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002485 if (!journal_quota)
2486 reiserfs_add_ordered_list(inode, bh);
Jan Kara361d94a2012-11-13 18:25:38 +01002487 reiserfs_write_unlock(sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002488 brelse(bh);
2489 offset = 0;
2490 towrite -= tocopy;
2491 data += tocopy;
2492 blk++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 }
Jan Kara9c3013e2007-09-11 15:23:29 -07002494out:
Jan Kara67f16482012-04-25 21:28:12 +02002495 if (len == towrite)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002496 return err;
2497 if (inode->i_size < off + len - towrite)
2498 i_size_write(inode, off + len - towrite);
2499 inode->i_version++;
2500 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
2501 mark_inode_dirty(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002502 return len - towrite;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503}
2504
2505#endif
2506
Al Viro152a0832010-07-25 00:46:55 +04002507static struct dentry *get_super_block(struct file_system_type *fs_type,
David Howells454e2392006-06-23 02:02:57 -07002508 int flags, const char *dev_name,
Al Viro152a0832010-07-25 00:46:55 +04002509 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510{
Al Viro152a0832010-07-25 00:46:55 +04002511 return mount_bdev(fs_type, flags, dev_name, data, reiserfs_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512}
2513
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002514static int __init init_reiserfs_fs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515{
2516 int ret;
2517
Jeff Mahoney797d9012014-04-23 10:00:34 -04002518 ret = init_inodecache();
2519 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002522 reiserfs_proc_info_global_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002524 ret = register_filesystem(&reiserfs_fs_type);
Jeff Mahoney797d9012014-04-23 10:00:34 -04002525 if (ret)
2526 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527
Jeff Mahoney797d9012014-04-23 10:00:34 -04002528 return 0;
2529out:
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002530 reiserfs_proc_info_global_done();
2531 destroy_inodecache();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532
2533 return ret;
2534}
2535
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002536static void __exit exit_reiserfs_fs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002538 reiserfs_proc_info_global_done();
2539 unregister_filesystem(&reiserfs_fs_type);
2540 destroy_inodecache();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541}
2542
2543struct file_system_type reiserfs_fs_type = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002544 .owner = THIS_MODULE,
2545 .name = "reiserfs",
Al Viro152a0832010-07-25 00:46:55 +04002546 .mount = get_super_block,
David Howellsedc666e2006-10-11 01:22:14 -07002547 .kill_sb = reiserfs_kill_sb,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002548 .fs_flags = FS_REQUIRES_DEV,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549};
Eric W. Biederman7f78e032013-03-02 19:39:14 -08002550MODULE_ALIAS_FS("reiserfs");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002552MODULE_DESCRIPTION("ReiserFS journaled filesystem");
2553MODULE_AUTHOR("Hans Reiser <reiser@namesys.com>");
2554MODULE_LICENSE("GPL");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002556module_init(init_reiserfs_fs);
2557module_exit(exit_reiserfs_fs);