blob: 9938a518c5ef3622b33e4d5912367b4968880038 [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>
Fabian Frederick170939912014-08-08 14:21:12 -070018#include <linux/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>
Tejun Heo66114ca2015-05-22 17:13:32 -040024#include <linux/backing-dev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/buffer_head.h>
Christoph Hellwiga5694252007-07-17 04:04:28 -070026#include <linux/exportfs.h>
Jan Kara74abb982008-07-25 01:46:51 -070027#include <linux/quotaops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/vfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/mount.h>
30#include <linux/namei.h>
Coly Li651d0622009-04-02 16:59:41 -070031#include <linux/crc32.h>
Jan Karac3aa0772011-12-21 20:17:10 +010032#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34struct file_system_type reiserfs_fs_type;
35
36static const char reiserfs_3_5_magic_string[] = REISERFS_SUPER_MAGIC_STRING;
37static const char reiserfs_3_6_magic_string[] = REISER2FS_SUPER_MAGIC_STRING;
38static const char reiserfs_jr_magic_string[] = REISER2FS_JR_SUPER_MAGIC_STRING;
39
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070040int is_reiserfs_3_5(struct reiserfs_super_block *rs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070042 return !strncmp(rs->s_v1.s_magic, reiserfs_3_5_magic_string,
43 strlen(reiserfs_3_5_magic_string));
Linus Torvalds1da177e2005-04-16 15:20:36 -070044}
45
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070046int is_reiserfs_3_6(struct reiserfs_super_block *rs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070048 return !strncmp(rs->s_v1.s_magic, reiserfs_3_6_magic_string,
49 strlen(reiserfs_3_6_magic_string));
Linus Torvalds1da177e2005-04-16 15:20:36 -070050}
51
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070052int is_reiserfs_jr(struct reiserfs_super_block *rs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070054 return !strncmp(rs->s_v1.s_magic, reiserfs_jr_magic_string,
55 strlen(reiserfs_jr_magic_string));
Linus Torvalds1da177e2005-04-16 15:20:36 -070056}
57
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070058static int is_any_reiserfs_magic_string(struct reiserfs_super_block *rs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070060 return (is_reiserfs_3_5(rs) || is_reiserfs_3_6(rs) ||
61 is_reiserfs_jr(rs));
Linus Torvalds1da177e2005-04-16 15:20:36 -070062}
63
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070064static int reiserfs_remount(struct super_block *s, int *flags, char *data);
David Howells726c3342006-06-23 02:02:58 -070065static int reiserfs_statfs(struct dentry *dentry, struct kstatfs *buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070067static int reiserfs_sync_fs(struct super_block *s, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
Christoph Hellwig5af79262009-05-05 15:41:25 +020069 struct reiserfs_transaction_handle th;
70
Jan Karaa1177822012-07-03 16:45:29 +020071 /*
72 * Writeback quota in non-journalled quota case - journalled quota has
73 * no dirty dquots
74 */
75 dquot_writeback_dquots(s, -1);
Christoph Hellwig5af79262009-05-05 15:41:25 +020076 reiserfs_write_lock(s);
77 if (!journal_begin(&th, s, 1))
Jeff Mahoney58d85422014-04-23 10:00:38 -040078 if (!journal_end_sync(&th))
Christoph Hellwig5af79262009-05-05 15:41:25 +020079 reiserfs_flush_old_commits(s);
Christoph Hellwig5af79262009-05-05 15:41:25 +020080 reiserfs_write_unlock(s);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070081 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082}
83
Artem Bityutskiy033369d2012-06-01 17:18:08 +030084static void flush_old_commits(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
Artem Bityutskiy033369d2012-06-01 17:18:08 +030086 struct reiserfs_sb_info *sbi;
87 struct super_block *s;
88
89 sbi = container_of(work, struct reiserfs_sb_info, old_work.work);
90 s = sbi->s_journal->j_work_sb;
91
92 spin_lock(&sbi->old_work_lock);
93 sbi->work_queued = 0;
94 spin_unlock(&sbi->old_work_lock);
95
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070096 reiserfs_sync_fs(s, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097}
98
Artem Bityutskiy033369d2012-06-01 17:18:08 +030099void reiserfs_schedule_old_flush(struct super_block *s)
100{
101 struct reiserfs_sb_info *sbi = REISERFS_SB(s);
102 unsigned long delay;
103
Jan Kara01777832014-08-06 19:43:56 +0200104 /*
105 * Avoid scheduling flush when sb is being shut down. It can race
106 * with journal shutdown and free still queued delayed work.
107 */
108 if (s->s_flags & MS_RDONLY || !(s->s_flags & MS_ACTIVE))
Artem Bityutskiy033369d2012-06-01 17:18:08 +0300109 return;
110
111 spin_lock(&sbi->old_work_lock);
112 if (!sbi->work_queued) {
113 delay = msecs_to_jiffies(dirty_writeback_interval * 10);
114 queue_delayed_work(system_long_wq, &sbi->old_work, delay);
115 sbi->work_queued = 1;
116 }
117 spin_unlock(&sbi->old_work_lock);
118}
119
120static void cancel_old_flush(struct super_block *s)
121{
122 struct reiserfs_sb_info *sbi = REISERFS_SB(s);
123
124 cancel_delayed_work_sync(&REISERFS_SB(s)->old_work);
125 spin_lock(&sbi->old_work_lock);
126 sbi->work_queued = 0;
127 spin_unlock(&sbi->old_work_lock);
128}
129
Takashi Satoc4be0c12009-01-09 16:40:58 -0800130static int reiserfs_freeze(struct super_block *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700132 struct reiserfs_transaction_handle th;
Artem Bityutskiy033369d2012-06-01 17:18:08 +0300133
134 cancel_old_flush(s);
135
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700136 reiserfs_write_lock(s);
137 if (!(s->s_flags & MS_RDONLY)) {
138 int err = journal_begin(&th, s, 1);
139 if (err) {
140 reiserfs_block_writes(&th);
141 } else {
142 reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s),
143 1);
Jeff Mahoney09f1b802014-04-23 10:00:39 -0400144 journal_mark_dirty(&th, SB_BUFFER_WITH_SB(s));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700145 reiserfs_block_writes(&th);
Jeff Mahoney58d85422014-04-23 10:00:38 -0400146 journal_end_sync(&th);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700147 }
148 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700149 reiserfs_write_unlock(s);
Takashi Satoc4be0c12009-01-09 16:40:58 -0800150 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151}
152
Takashi Satoc4be0c12009-01-09 16:40:58 -0800153static int reiserfs_unfreeze(struct super_block *s)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700154{
155 reiserfs_allow_writes(s);
Takashi Satoc4be0c12009-01-09 16:40:58 -0800156 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157}
158
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700159extern const struct in_core_key MAX_IN_CORE_KEY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Jeff Mahoney098297b2014-04-23 10:00:36 -0400161/*
162 * this is used to delete "save link" when there are no items of a
163 * file it points to. It can either happen if unlink is completed but
164 * "save unlink" removal, or if file has both unlink and truncate
165 * pending and as unlink completes first (because key of "save link"
166 * protecting unlink is bigger that a key lf "save link" which
167 * protects truncate), so there left no items to make truncate
168 * completion on
169 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700170static int remove_save_link_only(struct super_block *s,
171 struct reiserfs_key *key, int oid_free)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700173 struct reiserfs_transaction_handle th;
174 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700176 /* we are going to do one balancing */
177 err = journal_begin(&th, s, JOURNAL_PER_BALANCE_CNT);
178 if (err)
179 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700181 reiserfs_delete_solid_item(&th, NULL, key);
182 if (oid_free)
183 /* removals are protected by direct items */
184 reiserfs_release_objectid(&th, le32_to_cpu(key->k_objectid));
185
Jeff Mahoney58d85422014-04-23 10:00:38 -0400186 return journal_end(&th);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187}
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189#ifdef CONFIG_QUOTA
190static int reiserfs_quota_on_mount(struct super_block *, int);
191#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193/* look for uncompleted unlinks and truncates and complete them */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700194static int finish_unfinished(struct super_block *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700196 INITIALIZE_PATH(path);
197 struct cpu_key max_cpu_key, obj_key;
Lepton Wufb46f342007-10-16 23:30:04 -0700198 struct reiserfs_key save_link_key, last_inode_key;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700199 int retval = 0;
200 struct item_head *ih;
201 struct buffer_head *bh;
202 int item_pos;
203 char *item;
204 int done;
205 struct inode *inode;
206 int truncate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207#ifdef CONFIG_QUOTA
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700208 int i;
209 int ms_active_set;
Jan Karaaca60612014-09-10 20:57:45 +0200210 int quota_enabled[REISERFS_MAXQUOTAS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700212
213 /* compose key to look for "save" links */
214 max_cpu_key.version = KEY_FORMAT_3_5;
215 max_cpu_key.on_disk_key.k_dir_id = ~0U;
216 max_cpu_key.on_disk_key.k_objectid = ~0U;
217 set_cpu_key_k_offset(&max_cpu_key, ~0U);
218 max_cpu_key.key_length = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Lepton Wufb46f342007-10-16 23:30:04 -0700220 memset(&last_inode_key, 0, sizeof(last_inode_key));
221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222#ifdef CONFIG_QUOTA
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700223 /* Needed for iput() to work correctly and not trash data */
224 if (s->s_flags & MS_ACTIVE) {
225 ms_active_set = 0;
226 } else {
227 ms_active_set = 1;
228 s->s_flags |= MS_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700230 /* Turn on quotas so that they are updated correctly */
Jan Karaaca60612014-09-10 20:57:45 +0200231 for (i = 0; i < REISERFS_MAXQUOTAS; i++) {
Jan Karaf4b113a2010-05-21 19:12:51 +0200232 quota_enabled[i] = 1;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700233 if (REISERFS_SB(s)->s_qf_names[i]) {
Jan Karaf4b113a2010-05-21 19:12:51 +0200234 int ret;
235
236 if (sb_has_quota_active(s, i)) {
237 quota_enabled[i] = 0;
238 continue;
239 }
240 ret = reiserfs_quota_on_mount(s, i);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700241 if (ret < 0)
Jeff Mahoney45b03d52009-03-30 14:02:21 -0400242 reiserfs_warning(s, "reiserfs-2500",
243 "cannot turn on journaled "
244 "quota: error %d", ret);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700245 }
246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700249 done = 0;
250 REISERFS_SB(s)->s_is_unlinked_ok = 1;
251 while (!retval) {
Jeff Mahoneyd2d03952013-08-08 17:34:47 -0400252 int depth;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700253 retval = search_item(s, &max_cpu_key, &path);
254 if (retval != ITEM_NOT_FOUND) {
Jeff Mahoney0030b642009-03-30 14:02:28 -0400255 reiserfs_error(s, "vs-2140",
256 "search_by_key returned %d", retval);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700257 break;
258 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700260 bh = get_last_bh(&path);
261 item_pos = get_item_pos(&path);
262 if (item_pos != B_NR_ITEMS(bh)) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -0400263 reiserfs_warning(s, "vs-2060",
264 "wrong position found");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700265 break;
266 }
267 item_pos--;
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400268 ih = item_head(bh, item_pos);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700269
270 if (le32_to_cpu(ih->ih_key.k_dir_id) != MAX_KEY_OBJECTID)
271 /* there are no "save" links anymore */
272 break;
273
274 save_link_key = ih->ih_key;
275 if (is_indirect_le_ih(ih))
276 truncate = 1;
277 else
278 truncate = 0;
279
280 /* reiserfs_iget needs k_dirid and k_objectid only */
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400281 item = ih_item_body(bh, ih);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700282 obj_key.on_disk_key.k_dir_id = le32_to_cpu(*(__le32 *) item);
283 obj_key.on_disk_key.k_objectid =
284 le32_to_cpu(ih->ih_key.k_objectid);
285 obj_key.on_disk_key.k_offset = 0;
286 obj_key.on_disk_key.k_type = 0;
287
288 pathrelse(&path);
289
290 inode = reiserfs_iget(s, &obj_key);
Sudip Mukherjeeff7d0802016-01-21 16:40:36 -0800291 if (IS_ERR_OR_NULL(inode)) {
Jeff Mahoney098297b2014-04-23 10:00:36 -0400292 /*
293 * the unlink almost completed, it just did not
294 * manage to remove "save" link and release objectid
295 */
Jeff Mahoney45b03d52009-03-30 14:02:21 -0400296 reiserfs_warning(s, "vs-2180", "iget failed for %K",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700297 &obj_key);
298 retval = remove_save_link_only(s, &save_link_key, 1);
299 continue;
300 }
301
302 if (!truncate && inode->i_nlink) {
303 /* file is not unlinked */
Jeff Mahoney45b03d52009-03-30 14:02:21 -0400304 reiserfs_warning(s, "vs-2185",
305 "file %K is not unlinked",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700306 &obj_key);
307 retval = remove_save_link_only(s, &save_link_key, 0);
308 continue;
309 }
Jeff Mahoneyd2d03952013-08-08 17:34:47 -0400310 depth = reiserfs_write_unlock_nested(inode->i_sb);
Christoph Hellwig871a2932010-03-03 09:05:07 -0500311 dquot_initialize(inode);
Jeff Mahoneyd2d03952013-08-08 17:34:47 -0400312 reiserfs_write_lock_nested(inode->i_sb, depth);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700313
314 if (truncate && S_ISDIR(inode->i_mode)) {
Jeff Mahoney098297b2014-04-23 10:00:36 -0400315 /*
316 * We got a truncate request for a dir which
317 * is impossible. The only imaginable way is to
318 * execute unfinished truncate request then boot
319 * into old kernel, remove the file and create dir
320 * with the same key.
321 */
Jeff Mahoney45b03d52009-03-30 14:02:21 -0400322 reiserfs_warning(s, "green-2101",
323 "impossible truncate on a "
324 "directory %k. Please report",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700325 INODE_PKEY(inode));
326 retval = remove_save_link_only(s, &save_link_key, 0);
327 truncate = 0;
328 iput(inode);
329 continue;
330 }
331
332 if (truncate) {
333 REISERFS_I(inode)->i_flags |=
334 i_link_saved_truncate_mask;
Jeff Mahoney098297b2014-04-23 10:00:36 -0400335 /*
336 * not completed truncate found. New size was
337 * committed together with "save" link
338 */
Fabian Frederick53872ed2014-08-08 14:21:10 -0700339 reiserfs_info(s, "Truncating %k to %lld ..",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700340 INODE_PKEY(inode), inode->i_size);
Jeff Mahoney098297b2014-04-23 10:00:36 -0400341
342 /* don't update modification time */
343 reiserfs_truncate_file(inode, 0);
344
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700345 retval = remove_save_link(inode, truncate);
346 } else {
347 REISERFS_I(inode)->i_flags |= i_link_saved_unlink_mask;
348 /* not completed unlink (rmdir) found */
349 reiserfs_info(s, "Removing %k..", INODE_PKEY(inode));
Lepton Wufb46f342007-10-16 23:30:04 -0700350 if (memcmp(&last_inode_key, INODE_PKEY(inode),
351 sizeof(last_inode_key))){
352 last_inode_key = *INODE_PKEY(inode);
353 /* removal gets completed in iput */
354 retval = 0;
355 } else {
Jeff Mahoney45b03d52009-03-30 14:02:21 -0400356 reiserfs_warning(s, "super-2189", "Dead loop "
357 "in finish_unfinished "
358 "detected, just remove "
359 "save link\n");
Lepton Wufb46f342007-10-16 23:30:04 -0700360 retval = remove_save_link_only(s,
361 &save_link_key, 0);
362 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700363 }
364
365 iput(inode);
366 printk("done\n");
367 done++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700369 REISERFS_SB(s)->s_is_unlinked_ok = 0;
370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371#ifdef CONFIG_QUOTA
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700372 /* Turn quotas off */
Jeff Mahoneyd2d03952013-08-08 17:34:47 -0400373 reiserfs_write_unlock(s);
Jan Karaaca60612014-09-10 20:57:45 +0200374 for (i = 0; i < REISERFS_MAXQUOTAS; i++) {
Jan Karaf4b113a2010-05-21 19:12:51 +0200375 if (sb_dqopt(s)->files[i] && quota_enabled[i])
Christoph Hellwig287a8092010-05-19 07:16:45 -0400376 dquot_quota_off(s, i);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700377 }
Jeff Mahoneyd2d03952013-08-08 17:34:47 -0400378 reiserfs_write_lock(s);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700379 if (ms_active_set)
380 /* Restore the flag back */
381 s->s_flags &= ~MS_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700383 pathrelse(&path);
384 if (done)
385 reiserfs_info(s, "There were %d uncompleted unlinks/truncates. "
386 "Completed\n", done);
387 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388}
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700389
Jeff Mahoney098297b2014-04-23 10:00:36 -0400390/*
391 * to protect file being unlinked from getting lost we "safe" link files
392 * being unlinked. This link will be deleted in the same transaction with last
393 * item of file. mounting the filesystem we scan all these links and remove
394 * files which almost got lost
395 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700396void add_save_link(struct reiserfs_transaction_handle *th,
397 struct inode *inode, int truncate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700399 INITIALIZE_PATH(path);
400 int retval;
401 struct cpu_key key;
402 struct item_head ih;
403 __le32 link;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700405 BUG_ON(!th->t_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700407 /* file can only get one "save link" of each kind */
408 RFALSE(truncate &&
409 (REISERFS_I(inode)->i_flags & i_link_saved_truncate_mask),
410 "saved link already exists for truncated inode %lx",
411 (long)inode->i_ino);
412 RFALSE(!truncate &&
413 (REISERFS_I(inode)->i_flags & i_link_saved_unlink_mask),
414 "saved link already exists for unlinked inode %lx",
415 (long)inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700417 /* setup key of "save" link */
418 key.version = KEY_FORMAT_3_5;
419 key.on_disk_key.k_dir_id = MAX_KEY_OBJECTID;
420 key.on_disk_key.k_objectid = inode->i_ino;
421 if (!truncate) {
422 /* unlink, rmdir, rename */
423 set_cpu_key_k_offset(&key, 1 + inode->i_sb->s_blocksize);
424 set_cpu_key_k_type(&key, TYPE_DIRECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700426 /* item head of "safe" link */
427 make_le_item_head(&ih, &key, key.version,
428 1 + inode->i_sb->s_blocksize, TYPE_DIRECT,
429 4 /*length */ , 0xffff /*free space */ );
430 } else {
431 /* truncate */
432 if (S_ISDIR(inode->i_mode))
Jeff Mahoney45b03d52009-03-30 14:02:21 -0400433 reiserfs_warning(inode->i_sb, "green-2102",
434 "Adding a truncate savelink for "
435 "a directory %k! Please report",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700436 INODE_PKEY(inode));
437 set_cpu_key_k_offset(&key, 1);
438 set_cpu_key_k_type(&key, TYPE_INDIRECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700440 /* item head of "safe" link */
441 make_le_item_head(&ih, &key, key.version, 1, TYPE_INDIRECT,
442 4 /*length */ , 0 /*free space */ );
443 }
444 key.key_length = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700446 /* look for its place in the tree */
447 retval = search_item(inode->i_sb, &key, &path);
448 if (retval != ITEM_NOT_FOUND) {
449 if (retval != -ENOSPC)
Jeff Mahoney0030b642009-03-30 14:02:28 -0400450 reiserfs_error(inode->i_sb, "vs-2100",
451 "search_by_key (%K) returned %d", &key,
452 retval);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700453 pathrelse(&path);
454 return;
455 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700457 /* body of "save" link */
458 link = INODE_PKEY(inode)->k_dir_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300460 /* put "save" link into tree, don't charge quota to anyone */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700461 retval =
462 reiserfs_insert_item(th, &path, &key, &ih, NULL, (char *)&link);
463 if (retval) {
464 if (retval != -ENOSPC)
Jeff Mahoney0030b642009-03-30 14:02:28 -0400465 reiserfs_error(inode->i_sb, "vs-2120",
466 "insert_item returned %d", retval);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700467 } else {
468 if (truncate)
469 REISERFS_I(inode)->i_flags |=
470 i_link_saved_truncate_mask;
471 else
472 REISERFS_I(inode)->i_flags |= i_link_saved_unlink_mask;
473 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474}
475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476/* this opens transaction unlike add_save_link */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700477int remove_save_link(struct inode *inode, int truncate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700479 struct reiserfs_transaction_handle th;
480 struct reiserfs_key key;
481 int err;
482
483 /* we are going to do one balancing only */
484 err = journal_begin(&th, inode->i_sb, JOURNAL_PER_BALANCE_CNT);
485 if (err)
486 return err;
487
488 /* setup key of "save" link */
489 key.k_dir_id = cpu_to_le32(MAX_KEY_OBJECTID);
490 key.k_objectid = INODE_PKEY(inode)->k_objectid;
491 if (!truncate) {
492 /* unlink, rmdir, rename */
493 set_le_key_k_offset(KEY_FORMAT_3_5, &key,
494 1 + inode->i_sb->s_blocksize);
495 set_le_key_k_type(KEY_FORMAT_3_5, &key, TYPE_DIRECT);
496 } else {
497 /* truncate */
498 set_le_key_k_offset(KEY_FORMAT_3_5, &key, 1);
499 set_le_key_k_type(KEY_FORMAT_3_5, &key, TYPE_INDIRECT);
500 }
501
502 if ((truncate &&
503 (REISERFS_I(inode)->i_flags & i_link_saved_truncate_mask)) ||
504 (!truncate &&
505 (REISERFS_I(inode)->i_flags & i_link_saved_unlink_mask)))
506 /* don't take quota bytes from anywhere */
507 reiserfs_delete_solid_item(&th, NULL, &key);
508 if (!truncate) {
509 reiserfs_release_objectid(&th, inode->i_ino);
510 REISERFS_I(inode)->i_flags &= ~i_link_saved_unlink_mask;
511 } else
512 REISERFS_I(inode)->i_flags &= ~i_link_saved_truncate_mask;
513
Jeff Mahoney58d85422014-04-23 10:00:38 -0400514 return journal_end(&th);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515}
516
David Howellsedc666e2006-10-11 01:22:14 -0700517static void reiserfs_kill_sb(struct super_block *s)
518{
519 if (REISERFS_SB(s)) {
Al Viro672fe152013-08-05 17:37:37 +0400520 reiserfs_proc_info_done(s);
Jeff Mahoneya9e36da2011-12-21 21:18:43 +0100521 /*
522 * Force any pending inode evictions to occur now. Any
523 * inodes to be removed that have extended attributes
524 * associated with them need to clean them up before
525 * we can release the extended attribute root dentries.
526 * shrink_dcache_for_umount will BUG if we don't release
527 * those before it's called so ->put_super is too late.
528 */
529 shrink_dcache_sb(s);
530
531 dput(REISERFS_SB(s)->xattr_root);
532 REISERFS_SB(s)->xattr_root = NULL;
533 dput(REISERFS_SB(s)->priv_root);
534 REISERFS_SB(s)->priv_root = NULL;
David Howellsedc666e2006-10-11 01:22:14 -0700535 }
536
537 kill_block_super(s);
538}
539
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700540static void reiserfs_put_super(struct super_block *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700542 struct reiserfs_transaction_handle th;
543 th.t_trans_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
Christoph Hellwige0ccfd92010-05-19 07:16:42 -0400545 dquot_disable(s, -1, DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
546
Frederic Weisbecker8ebc4232009-04-07 04:19:49 +0200547 reiserfs_write_lock(s);
Christoph Hellwig6cfd0142009-05-05 15:40:36 +0200548
Jeff Mahoney098297b2014-04-23 10:00:36 -0400549 /*
550 * change file system state to current state if it was mounted
551 * with read-write permissions
552 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700553 if (!(s->s_flags & MS_RDONLY)) {
554 if (!journal_begin(&th, s, 10)) {
555 reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s),
556 1);
557 set_sb_umount_state(SB_DISK_SUPER_BLOCK(s),
558 REISERFS_SB(s)->s_mount_state);
Jeff Mahoney09f1b802014-04-23 10:00:39 -0400559 journal_mark_dirty(&th, SB_BUFFER_WITH_SB(s));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700560 }
561 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Jeff Mahoney098297b2014-04-23 10:00:36 -0400563 /*
564 * note, journal_release checks for readonly mount, and can
565 * decide not to do a journal_end
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700566 */
567 journal_release(&th, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Jeff Mahoney50652272006-09-30 23:28:44 -0700569 reiserfs_free_bitmap_cache(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700571 brelse(SB_BUFFER_WITH_SB(s));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700573 print_statistics(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700575 if (REISERFS_SB(s)->reserved_blocks != 0) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -0400576 reiserfs_warning(s, "green-2005", "reserved blocks left %d",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700577 REISERFS_SB(s)->reserved_blocks);
578 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Frederic Weisbecker8ebc4232009-04-07 04:19:49 +0200580 reiserfs_write_unlock(s);
581 mutex_destroy(&REISERFS_SB(s)->lock);
Jeff Mahoney797d9012014-04-23 10:00:34 -0400582 destroy_workqueue(REISERFS_SB(s)->commit_wq);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700583 kfree(s->s_fs_info);
584 s->s_fs_info = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585}
586
Christoph Lametere18b8902006-12-06 20:33:20 -0800587static struct kmem_cache *reiserfs_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
589static struct inode *reiserfs_alloc_inode(struct super_block *sb)
590{
591 struct reiserfs_inode_info *ei;
Firo Yang89bfae42015-06-25 15:03:48 -0700592 ei = kmem_cache_alloc(reiserfs_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 if (!ei)
594 return NULL;
Al Viro0e4f6a72010-07-04 12:18:57 +0400595 atomic_set(&ei->openers, 0);
596 mutex_init(&ei->tailpack);
Jan Kara53873632014-09-29 15:08:19 +0200597#ifdef CONFIG_QUOTA
598 memset(&ei->i_dquot, 0, sizeof(ei->i_dquot));
599#endif
600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 return &ei->vfs_inode;
602}
603
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100604static void reiserfs_i_callback(struct rcu_head *head)
605{
606 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100607 kmem_cache_free(reiserfs_inode_cachep, REISERFS_I(inode));
608}
609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610static void reiserfs_destroy_inode(struct inode *inode)
611{
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100612 call_rcu(&inode->i_rcu, reiserfs_i_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613}
614
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700615static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700617 struct reiserfs_inode_info *ei = (struct reiserfs_inode_info *)foo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Christoph Lametera35afb82007-05-16 22:10:57 -0700619 INIT_LIST_HEAD(&ei->i_prealloc_list);
620 inode_init_once(&ei->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621}
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700622
Fabian Frederick31e14362014-04-03 14:50:37 -0700623static int __init init_inodecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624{
625 reiserfs_inode_cachep = kmem_cache_create("reiser_inode_cache",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700626 sizeof(struct
627 reiserfs_inode_info),
Paul Jacksonfffb60f2006-03-24 03:16:06 -0800628 0, (SLAB_RECLAIM_ACCOUNT|
Vladimir Davydov5d097052016-01-14 15:18:21 -0800629 SLAB_MEM_SPREAD|
630 SLAB_ACCOUNT),
Paul Mundt20c2df82007-07-20 10:11:58 +0900631 init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 if (reiserfs_inode_cachep == NULL)
633 return -ENOMEM;
634 return 0;
635}
636
637static void destroy_inodecache(void)
638{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +1000639 /*
640 * Make sure all delayed rcu free inodes are flushed before we
641 * destroy cache.
642 */
643 rcu_barrier();
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -0700644 kmem_cache_destroy(reiserfs_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645}
646
647/* we don't mark inodes dirty, we just log them */
Christoph Hellwigaa385722011-05-27 06:53:02 -0400648static void reiserfs_dirty_inode(struct inode *inode, int flags)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700649{
650 struct reiserfs_transaction_handle th;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700652 int err = 0;
Frederic Weisbeckerdc8f6d82009-04-14 05:34:25 +0200653
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700654 if (inode->i_sb->s_flags & MS_RDONLY) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -0400655 reiserfs_warning(inode->i_sb, "clm-6006",
656 "writing inode %lu on readonly FS",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700657 inode->i_ino);
658 return;
659 }
Jeff Mahoney278f6672013-08-08 17:34:46 -0400660 reiserfs_write_lock(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Jeff Mahoney098297b2014-04-23 10:00:36 -0400662 /*
663 * this is really only used for atime updates, so they don't have
664 * to be included in O_SYNC or fsync
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700665 */
666 err = journal_begin(&th, inode->i_sb, 1);
Frederic Weisbeckerdc8f6d82009-04-14 05:34:25 +0200667 if (err)
668 goto out;
669
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700670 reiserfs_update_sd(&th, inode);
Jeff Mahoney58d85422014-04-23 10:00:38 -0400671 journal_end(&th);
Frederic Weisbeckerdc8f6d82009-04-14 05:34:25 +0200672
673out:
Jeff Mahoney278f6672013-08-08 17:34:46 -0400674 reiserfs_write_unlock(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675}
676
Jan Karac3aa0772011-12-21 20:17:10 +0100677static int reiserfs_show_options(struct seq_file *seq, struct dentry *root)
678{
679 struct super_block *s = root->d_sb;
680 struct reiserfs_journal *journal = SB_JOURNAL(s);
681 long opts = REISERFS_SB(s)->s_mount_opt;
682
683 if (opts & (1 << REISERFS_LARGETAIL))
684 seq_puts(seq, ",tails=on");
685 else if (!(opts & (1 << REISERFS_SMALLTAIL)))
686 seq_puts(seq, ",notail");
687 /* tails=small is default so we don't show it */
688
689 if (!(opts & (1 << REISERFS_BARRIER_FLUSH)))
690 seq_puts(seq, ",barrier=none");
691 /* barrier=flush is default so we don't show it */
692
693 if (opts & (1 << REISERFS_ERROR_CONTINUE))
694 seq_puts(seq, ",errors=continue");
695 else if (opts & (1 << REISERFS_ERROR_PANIC))
696 seq_puts(seq, ",errors=panic");
697 /* errors=ro is default so we don't show it */
698
699 if (opts & (1 << REISERFS_DATA_LOG))
700 seq_puts(seq, ",data=journal");
701 else if (opts & (1 << REISERFS_DATA_WRITEBACK))
702 seq_puts(seq, ",data=writeback");
703 /* data=ordered is default so we don't show it */
704
705 if (opts & (1 << REISERFS_ATTRS))
706 seq_puts(seq, ",attrs");
707
708 if (opts & (1 << REISERFS_XATTRS_USER))
709 seq_puts(seq, ",user_xattr");
710
711 if (opts & (1 << REISERFS_EXPOSE_PRIVROOT))
712 seq_puts(seq, ",expose_privroot");
713
714 if (opts & (1 << REISERFS_POSIXACL))
715 seq_puts(seq, ",acl");
716
717 if (REISERFS_SB(s)->s_jdev)
Kees Cooka068acf2015-09-04 15:44:57 -0700718 seq_show_option(seq, "jdev", REISERFS_SB(s)->s_jdev);
Jan Karac3aa0772011-12-21 20:17:10 +0100719
720 if (journal->j_max_commit_age != journal->j_default_max_commit_age)
721 seq_printf(seq, ",commit=%d", journal->j_max_commit_age);
722
723#ifdef CONFIG_QUOTA
724 if (REISERFS_SB(s)->s_qf_names[USRQUOTA])
Kees Cooka068acf2015-09-04 15:44:57 -0700725 seq_show_option(seq, "usrjquota",
726 REISERFS_SB(s)->s_qf_names[USRQUOTA]);
Jan Karac3aa0772011-12-21 20:17:10 +0100727 else if (opts & (1 << REISERFS_USRQUOTA))
728 seq_puts(seq, ",usrquota");
729 if (REISERFS_SB(s)->s_qf_names[GRPQUOTA])
Kees Cooka068acf2015-09-04 15:44:57 -0700730 seq_show_option(seq, "grpjquota",
731 REISERFS_SB(s)->s_qf_names[GRPQUOTA]);
Jan Karac3aa0772011-12-21 20:17:10 +0100732 else if (opts & (1 << REISERFS_GRPQUOTA))
733 seq_puts(seq, ",grpquota");
734 if (REISERFS_SB(s)->s_jquota_fmt) {
735 if (REISERFS_SB(s)->s_jquota_fmt == QFMT_VFS_OLD)
736 seq_puts(seq, ",jqfmt=vfsold");
737 else if (REISERFS_SB(s)->s_jquota_fmt == QFMT_VFS_V0)
738 seq_puts(seq, ",jqfmt=vfsv0");
739 }
740#endif
741
742 /* Block allocator options */
743 if (opts & (1 << REISERFS_NO_BORDER))
744 seq_puts(seq, ",block-allocator=noborder");
745 if (opts & (1 << REISERFS_NO_UNHASHED_RELOCATION))
746 seq_puts(seq, ",block-allocator=no_unhashed_relocation");
747 if (opts & (1 << REISERFS_HASHED_RELOCATION))
748 seq_puts(seq, ",block-allocator=hashed_relocation");
749 if (opts & (1 << REISERFS_TEST4))
750 seq_puts(seq, ",block-allocator=test4");
751 show_alloc_options(seq, s);
752 return 0;
753}
754
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755#ifdef CONFIG_QUOTA
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700756static ssize_t reiserfs_quota_write(struct super_block *, int, const char *,
757 size_t, loff_t);
758static ssize_t reiserfs_quota_read(struct super_block *, int, char *, size_t,
759 loff_t);
Jan Kara53873632014-09-29 15:08:19 +0200760
761static struct dquot **reiserfs_get_dquots(struct inode *inode)
762{
763 return REISERFS_I(inode)->i_dquot;
764}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765#endif
766
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800767static const struct super_operations reiserfs_sops = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700768 .alloc_inode = reiserfs_alloc_inode,
769 .destroy_inode = reiserfs_destroy_inode,
770 .write_inode = reiserfs_write_inode,
771 .dirty_inode = reiserfs_dirty_inode,
Al Viro845a2cc2010-06-07 11:37:37 -0400772 .evict_inode = reiserfs_evict_inode,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700773 .put_super = reiserfs_put_super,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700774 .sync_fs = reiserfs_sync_fs,
Takashi Satoc4be0c12009-01-09 16:40:58 -0800775 .freeze_fs = reiserfs_freeze,
776 .unfreeze_fs = reiserfs_unfreeze,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700777 .statfs = reiserfs_statfs,
778 .remount_fs = reiserfs_remount,
Jan Karac3aa0772011-12-21 20:17:10 +0100779 .show_options = reiserfs_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780#ifdef CONFIG_QUOTA
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700781 .quota_read = reiserfs_quota_read,
782 .quota_write = reiserfs_quota_write,
Jan Kara53873632014-09-29 15:08:19 +0200783 .get_dquots = reiserfs_get_dquots,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784#endif
785};
786
787#ifdef CONFIG_QUOTA
788#define QTYPE2NAME(t) ((t)==USRQUOTA?"user":"group")
789
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790static int reiserfs_write_dquot(struct dquot *);
791static int reiserfs_acquire_dquot(struct dquot *);
792static int reiserfs_release_dquot(struct dquot *);
793static int reiserfs_mark_dquot_dirty(struct dquot *);
794static int reiserfs_write_info(struct super_block *, int);
Jan Karaf00c9e42010-09-15 17:38:58 +0200795static int reiserfs_quota_on(struct super_block *, int, int, struct path *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Alexey Dobriyan61e225d2009-09-21 17:01:08 -0700797static const struct dquot_operations reiserfs_quota_operations = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700798 .write_dquot = reiserfs_write_dquot,
799 .acquire_dquot = reiserfs_acquire_dquot,
800 .release_dquot = reiserfs_release_dquot,
801 .mark_dirty = reiserfs_mark_dquot_dirty,
802 .write_info = reiserfs_write_info,
Jan Kara41030032008-11-25 15:31:33 +0100803 .alloc_dquot = dquot_alloc,
804 .destroy_dquot = dquot_destroy,
Jan Karabe6257b2016-01-25 19:24:50 +0100805 .get_next_id = dquot_get_next_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806};
807
Alexey Dobriyan0d54b212009-09-21 17:01:09 -0700808static const struct quotactl_ops reiserfs_qctl_operations = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700809 .quota_on = reiserfs_quota_on,
Christoph Hellwig287a8092010-05-19 07:16:45 -0400810 .quota_off = dquot_quota_off,
811 .quota_sync = dquot_quota_sync,
Jan Kara0a2403392014-11-19 00:42:09 +0100812 .get_state = dquot_get_state,
Christoph Hellwig287a8092010-05-19 07:16:45 -0400813 .set_info = dquot_set_dqinfo,
814 .get_dqblk = dquot_get_dqblk,
815 .set_dqblk = dquot_set_dqblk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816};
817#endif
818
Christoph Hellwig39655162007-10-21 16:42:17 -0700819static const struct export_operations reiserfs_export_ops = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700820 .encode_fh = reiserfs_encode_fh,
Christoph Hellwigbe55caf2007-10-21 16:42:13 -0700821 .fh_to_dentry = reiserfs_fh_to_dentry,
822 .fh_to_parent = reiserfs_fh_to_parent,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700823 .get_parent = reiserfs_get_parent,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700824};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
Jeff Mahoney098297b2014-04-23 10:00:36 -0400826/*
827 * this struct is used in reiserfs_getopt () for containing the value for
828 * those mount options that have values rather than being toggles.
829 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830typedef struct {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700831 char *value;
Jeff Mahoney098297b2014-04-23 10:00:36 -0400832 /*
833 * bitmask which is to set on mount_options bitmask
834 * when this value is found, 0 is no bits are to be changed.
835 */
836 int setmask;
837 /*
838 * bitmask which is to clear on mount_options bitmask
839 * when this value is found, 0 is no bits are to be changed.
840 * This is applied BEFORE setmask
841 */
842 int clrmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843} arg_desc_t;
844
845/* Set this bit in arg_required to allow empty arguments */
846#define REISERFS_OPT_ALLOWEMPTY 31
847
Jeff Mahoney098297b2014-04-23 10:00:36 -0400848/*
849 * this struct is used in reiserfs_getopt() for describing the
850 * set of reiserfs mount options
851 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852typedef struct {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700853 char *option_name;
Jeff Mahoney098297b2014-04-23 10:00:36 -0400854
855 /* 0 if argument is not required, not 0 otherwise */
856 int arg_required;
857
858 /* list of values accepted by an option */
859 const arg_desc_t *values;
860
861 /*
862 * bitmask which is to set on mount_options bitmask
863 * when this value is found, 0 is no bits are to be changed.
864 */
865 int setmask;
866
867 /*
868 * bitmask which is to clear on mount_options bitmask
869 * when this value is found, 0 is no bits are to be changed.
870 * This is applied BEFORE setmask
871 */
872 int clrmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873} opt_desc_t;
874
875/* possible values for -o data= */
876static const arg_desc_t logging_mode[] = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700877 {"ordered", 1 << REISERFS_DATA_ORDERED,
878 (1 << REISERFS_DATA_LOG | 1 << REISERFS_DATA_WRITEBACK)},
879 {"journal", 1 << REISERFS_DATA_LOG,
880 (1 << REISERFS_DATA_ORDERED | 1 << REISERFS_DATA_WRITEBACK)},
881 {"writeback", 1 << REISERFS_DATA_WRITEBACK,
882 (1 << REISERFS_DATA_ORDERED | 1 << REISERFS_DATA_LOG)},
Vladimir V. Savelievcd02b962006-03-25 03:07:15 -0800883 {.value = NULL}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884};
885
886/* possible values for -o barrier= */
887static const arg_desc_t barrier_mode[] = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700888 {"none", 1 << REISERFS_BARRIER_NONE, 1 << REISERFS_BARRIER_FLUSH},
889 {"flush", 1 << REISERFS_BARRIER_FLUSH, 1 << REISERFS_BARRIER_NONE},
Vladimir V. Savelievcd02b962006-03-25 03:07:15 -0800890 {.value = NULL}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891};
892
Jeff Mahoney098297b2014-04-23 10:00:36 -0400893/*
894 * possible values for "-o block-allocator=" and bits which are to be set in
895 * s_mount_opt of reiserfs specific part of in-core super block
896 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897static const arg_desc_t balloc[] = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700898 {"noborder", 1 << REISERFS_NO_BORDER, 0},
899 {"border", 0, 1 << REISERFS_NO_BORDER},
900 {"no_unhashed_relocation", 1 << REISERFS_NO_UNHASHED_RELOCATION, 0},
901 {"hashed_relocation", 1 << REISERFS_HASHED_RELOCATION, 0},
902 {"test4", 1 << REISERFS_TEST4, 0},
903 {"notest4", 0, 1 << REISERFS_TEST4},
904 {NULL, 0, 0}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905};
906
907static const arg_desc_t tails[] = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700908 {"on", 1 << REISERFS_LARGETAIL, 1 << REISERFS_SMALLTAIL},
909 {"off", 0, (1 << REISERFS_LARGETAIL) | (1 << REISERFS_SMALLTAIL)},
910 {"small", 1 << REISERFS_SMALLTAIL, 1 << REISERFS_LARGETAIL},
911 {NULL, 0, 0}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912};
913
914static const arg_desc_t error_actions[] = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700915 {"panic", 1 << REISERFS_ERROR_PANIC,
916 (1 << REISERFS_ERROR_RO | 1 << REISERFS_ERROR_CONTINUE)},
917 {"ro-remount", 1 << REISERFS_ERROR_RO,
918 (1 << REISERFS_ERROR_PANIC | 1 << REISERFS_ERROR_CONTINUE)},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919#ifdef REISERFS_JOURNAL_ERROR_ALLOWS_NO_LOG
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700920 {"continue", 1 << REISERFS_ERROR_CONTINUE,
921 (1 << REISERFS_ERROR_PANIC | 1 << REISERFS_ERROR_RO)},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700923 {NULL, 0, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924};
925
Jeff Mahoney098297b2014-04-23 10:00:36 -0400926/*
927 * proceed only one option from a list *cur - string containing of mount
928 * options
929 * opts - array of options which are accepted
930 * opt_arg - if option is found and requires an argument and if it is specifed
931 * in the input - pointer to the argument is stored here
932 * bit_flags - if option requires to set a certain bit - it is set here
933 * return -1 if unknown option is found, opt->arg_required otherwise
934 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700935static int reiserfs_getopt(struct super_block *s, char **cur, opt_desc_t * opts,
936 char **opt_arg, unsigned long *bit_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700938 char *p;
Jeff Mahoney098297b2014-04-23 10:00:36 -0400939 /*
940 * foo=bar,
941 * ^ ^ ^
942 * | | +-- option_end
943 * | +-- arg_start
944 * +-- option_start
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700945 */
946 const opt_desc_t *opt;
947 const arg_desc_t *arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700949 p = *cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700951 /* assume argument cannot contain commas */
952 *cur = strchr(p, ',');
953 if (*cur) {
954 *(*cur) = '\0';
955 (*cur)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700958 if (!strncmp(p, "alloc=", 6)) {
Jeff Mahoney098297b2014-04-23 10:00:36 -0400959 /*
960 * Ugly special case, probably we should redo options
961 * parser so that it can understand several arguments for
962 * some options, also so that it can fill several bitfields
963 * with option values.
964 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700965 if (reiserfs_parse_alloc_options(s, p + 6)) {
966 return -1;
967 } else {
968 return 0;
969 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700971
972 /* for every option in the list */
973 for (opt = opts; opt->option_name; opt++) {
974 if (!strncmp(p, opt->option_name, strlen(opt->option_name))) {
975 if (bit_flags) {
976 if (opt->clrmask ==
977 (1 << REISERFS_UNSUPPORTED_OPT))
Jeff Mahoney45b03d52009-03-30 14:02:21 -0400978 reiserfs_warning(s, "super-6500",
979 "%s not supported.\n",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700980 p);
981 else
982 *bit_flags &= ~opt->clrmask;
983 if (opt->setmask ==
984 (1 << REISERFS_UNSUPPORTED_OPT))
Jeff Mahoney45b03d52009-03-30 14:02:21 -0400985 reiserfs_warning(s, "super-6501",
986 "%s not supported.\n",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700987 p);
988 else
989 *bit_flags |= opt->setmask;
990 }
991 break;
992 }
993 }
994 if (!opt->option_name) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -0400995 reiserfs_warning(s, "super-6502",
996 "unknown mount option \"%s\"", p);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700997 return -1;
998 }
999
1000 p += strlen(opt->option_name);
1001 switch (*p) {
1002 case '=':
1003 if (!opt->arg_required) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001004 reiserfs_warning(s, "super-6503",
1005 "the option \"%s\" does not "
1006 "require an argument\n",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001007 opt->option_name);
1008 return -1;
1009 }
1010 break;
1011
1012 case 0:
1013 if (opt->arg_required) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001014 reiserfs_warning(s, "super-6504",
1015 "the option \"%s\" requires an "
1016 "argument\n", opt->option_name);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001017 return -1;
1018 }
1019 break;
1020 default:
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001021 reiserfs_warning(s, "super-6505",
1022 "head of option \"%s\" is only correct\n",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001023 opt->option_name);
1024 return -1;
1025 }
1026
Jeff Mahoney098297b2014-04-23 10:00:36 -04001027 /*
1028 * move to the argument, or to next option if argument is not
1029 * required
1030 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001031 p++;
1032
1033 if (opt->arg_required
1034 && !(opt->arg_required & (1 << REISERFS_OPT_ALLOWEMPTY))
1035 && !strlen(p)) {
1036 /* this catches "option=," if not allowed */
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001037 reiserfs_warning(s, "super-6506",
1038 "empty argument for \"%s\"\n",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001039 opt->option_name);
1040 return -1;
1041 }
1042
1043 if (!opt->values) {
1044 /* *=NULLopt_arg contains pointer to argument */
1045 *opt_arg = p;
1046 return opt->arg_required & ~(1 << REISERFS_OPT_ALLOWEMPTY);
1047 }
1048
1049 /* values possible for this option are listed in opt->values */
1050 for (arg = opt->values; arg->value; arg++) {
1051 if (!strcmp(p, arg->value)) {
1052 if (bit_flags) {
1053 *bit_flags &= ~arg->clrmask;
1054 *bit_flags |= arg->setmask;
1055 }
1056 return opt->arg_required;
1057 }
1058 }
1059
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001060 reiserfs_warning(s, "super-6506",
1061 "bad value \"%s\" for option \"%s\"\n", p,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001062 opt->option_name);
1063 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064}
1065
1066/* returns 0 if something is wrong in option string, 1 - otherwise */
Jeff Mahoney098297b2014-04-23 10:00:36 -04001067static int reiserfs_parse_options(struct super_block *s,
1068
1069 /* string given via mount's -o */
1070 char *options,
1071
1072 /*
1073 * after the parsing phase, contains the
1074 * collection of bitflags defining what
1075 * mount options were selected.
1076 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001077 unsigned long *mount_options,
Jeff Mahoney098297b2014-04-23 10:00:36 -04001078
1079 /* strtol-ed from NNN of resize=NNN */
1080 unsigned long *blocks,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001081 char **jdev_name,
Jan Kara00b441972008-07-25 01:46:38 -07001082 unsigned int *commit_max_age,
1083 char **qf_names,
1084 unsigned int *qfmt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001086 int c;
1087 char *arg = NULL;
1088 char *pos;
1089 opt_desc_t opts[] = {
Jeff Mahoney098297b2014-04-23 10:00:36 -04001090 /*
1091 * Compatibility stuff, so that -o notail for old
1092 * setups still work
1093 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001094 {"tails",.arg_required = 't',.values = tails},
1095 {"notail",.clrmask =
1096 (1 << REISERFS_LARGETAIL) | (1 << REISERFS_SMALLTAIL)},
1097 {"conv",.setmask = 1 << REISERFS_CONVERT},
1098 {"attrs",.setmask = 1 << REISERFS_ATTRS},
1099 {"noattrs",.clrmask = 1 << REISERFS_ATTRS},
Jeff Mahoney73422812009-05-10 16:05:39 -04001100 {"expose_privroot", .setmask = 1 << REISERFS_EXPOSE_PRIVROOT},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101#ifdef CONFIG_REISERFS_FS_XATTR
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001102 {"user_xattr",.setmask = 1 << REISERFS_XATTRS_USER},
1103 {"nouser_xattr",.clrmask = 1 << REISERFS_XATTRS_USER},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104#else
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001105 {"user_xattr",.setmask = 1 << REISERFS_UNSUPPORTED_OPT},
1106 {"nouser_xattr",.clrmask = 1 << REISERFS_UNSUPPORTED_OPT},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107#endif
1108#ifdef CONFIG_REISERFS_FS_POSIX_ACL
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001109 {"acl",.setmask = 1 << REISERFS_POSIXACL},
1110 {"noacl",.clrmask = 1 << REISERFS_POSIXACL},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111#else
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001112 {"acl",.setmask = 1 << REISERFS_UNSUPPORTED_OPT},
1113 {"noacl",.clrmask = 1 << REISERFS_UNSUPPORTED_OPT},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114#endif
Vladimir V. Savelievcd02b962006-03-25 03:07:15 -08001115 {.option_name = "nolog"},
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001116 {"replayonly",.setmask = 1 << REPLAYONLY},
1117 {"block-allocator",.arg_required = 'a',.values = balloc},
1118 {"data",.arg_required = 'd',.values = logging_mode},
1119 {"barrier",.arg_required = 'b',.values = barrier_mode},
1120 {"resize",.arg_required = 'r',.values = NULL},
1121 {"jdev",.arg_required = 'j',.values = NULL},
1122 {"nolargeio",.arg_required = 'w',.values = NULL},
1123 {"commit",.arg_required = 'c',.values = NULL},
Jan Karac3aa0772011-12-21 20:17:10 +01001124 {"usrquota",.setmask = 1 << REISERFS_USRQUOTA},
1125 {"grpquota",.setmask = 1 << REISERFS_GRPQUOTA},
1126 {"noquota",.clrmask = 1 << REISERFS_USRQUOTA | 1 << REISERFS_GRPQUOTA},
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001127 {"errors",.arg_required = 'e',.values = error_actions},
1128 {"usrjquota",.arg_required =
1129 'u' | (1 << REISERFS_OPT_ALLOWEMPTY),.values = NULL},
1130 {"grpjquota",.arg_required =
1131 'g' | (1 << REISERFS_OPT_ALLOWEMPTY),.values = NULL},
1132 {"jqfmt",.arg_required = 'f',.values = NULL},
Vladimir V. Savelievcd02b962006-03-25 03:07:15 -08001133 {.option_name = NULL}
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001134 };
1135
1136 *blocks = 0;
1137 if (!options || !*options)
Jeff Mahoney098297b2014-04-23 10:00:36 -04001138 /*
1139 * use default configuration: create tails, journaling on, no
1140 * conversion to newest format
1141 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001142 return 1;
1143
1144 for (pos = options; pos;) {
1145 c = reiserfs_getopt(s, &pos, opts, &arg, mount_options);
1146 if (c == -1)
1147 /* wrong option is given */
1148 return 0;
1149
1150 if (c == 'r') {
1151 char *p;
1152
1153 p = NULL;
1154 /* "resize=NNN" or "resize=auto" */
1155
1156 if (!strcmp(arg, "auto")) {
1157 /* From JFS code, to auto-get the size. */
1158 *blocks =
1159 s->s_bdev->bd_inode->i_size >> s->
1160 s_blocksize_bits;
1161 } else {
1162 *blocks = simple_strtoul(arg, &p, 0);
1163 if (*p != '\0') {
1164 /* NNN does not look like a number */
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001165 reiserfs_warning(s, "super-6507",
1166 "bad value %s for "
1167 "-oresize\n", arg);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001168 return 0;
1169 }
1170 }
1171 }
1172
1173 if (c == 'c') {
1174 char *p = NULL;
1175 unsigned long val = simple_strtoul(arg, &p, 0);
1176 /* commit=NNN (time in seconds) */
1177 if (*p != '\0' || val >= (unsigned int)-1) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001178 reiserfs_warning(s, "super-6508",
1179 "bad value %s for -ocommit\n",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001180 arg);
1181 return 0;
1182 }
1183 *commit_max_age = (unsigned int)val;
1184 }
1185
1186 if (c == 'w') {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001187 reiserfs_warning(s, "super-6509", "nolargeio option "
1188 "is no longer supported");
Adrian Bunk36b756f2006-09-27 01:50:50 -07001189 return 0;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001190 }
1191
1192 if (c == 'j') {
1193 if (arg && *arg && jdev_name) {
Jeff Mahoney098297b2014-04-23 10:00:36 -04001194 /* Hm, already assigned? */
1195 if (*jdev_name) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001196 reiserfs_warning(s, "super-6510",
1197 "journal device was "
1198 "already specified to "
1199 "be %s", *jdev_name);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001200 return 0;
1201 }
1202 *jdev_name = arg;
1203 }
1204 }
1205#ifdef CONFIG_QUOTA
1206 if (c == 'u' || c == 'g') {
1207 int qtype = c == 'u' ? USRQUOTA : GRPQUOTA;
1208
Jan Kara6929f892008-08-20 18:16:36 +02001209 if (sb_any_quota_loaded(s) &&
Jan Kara00b441972008-07-25 01:46:38 -07001210 (!*arg != !REISERFS_SB(s)->s_qf_names[qtype])) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001211 reiserfs_warning(s, "super-6511",
1212 "cannot change journaled "
1213 "quota options when quota "
1214 "turned on.");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001215 return 0;
1216 }
1217 if (*arg) { /* Some filename specified? */
1218 if (REISERFS_SB(s)->s_qf_names[qtype]
1219 && strcmp(REISERFS_SB(s)->s_qf_names[qtype],
1220 arg)) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001221 reiserfs_warning(s, "super-6512",
1222 "%s quota file "
1223 "already specified.",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001224 QTYPE2NAME(qtype));
1225 return 0;
1226 }
1227 if (strchr(arg, '/')) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001228 reiserfs_warning(s, "super-6513",
1229 "quotafile must be "
1230 "on filesystem root.");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001231 return 0;
1232 }
Ionut-Gabriel Raduaf591ad2013-03-10 15:06:23 +02001233 qf_names[qtype] = kstrdup(arg, GFP_KERNEL);
Jan Kara00b441972008-07-25 01:46:38 -07001234 if (!qf_names[qtype]) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001235 reiserfs_warning(s, "reiserfs-2502",
1236 "not enough memory "
1237 "for storing "
1238 "quotafile name.");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001239 return 0;
1240 }
Jan Karac3aa0772011-12-21 20:17:10 +01001241 if (qtype == USRQUOTA)
1242 *mount_options |= 1 << REISERFS_USRQUOTA;
1243 else
1244 *mount_options |= 1 << REISERFS_GRPQUOTA;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001245 } else {
Jan Kara00b441972008-07-25 01:46:38 -07001246 if (qf_names[qtype] !=
1247 REISERFS_SB(s)->s_qf_names[qtype])
1248 kfree(qf_names[qtype]);
1249 qf_names[qtype] = NULL;
Jan Karac3aa0772011-12-21 20:17:10 +01001250 if (qtype == USRQUOTA)
1251 *mount_options &= ~(1 << REISERFS_USRQUOTA);
1252 else
1253 *mount_options &= ~(1 << REISERFS_GRPQUOTA);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001254 }
1255 }
1256 if (c == 'f') {
1257 if (!strcmp(arg, "vfsold"))
Jan Kara00b441972008-07-25 01:46:38 -07001258 *qfmt = QFMT_VFS_OLD;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001259 else if (!strcmp(arg, "vfsv0"))
Jan Kara00b441972008-07-25 01:46:38 -07001260 *qfmt = QFMT_VFS_V0;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001261 else {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001262 reiserfs_warning(s, "super-6514",
1263 "unknown quota format "
1264 "specified.");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001265 return 0;
1266 }
Jan Kara6929f892008-08-20 18:16:36 +02001267 if (sb_any_quota_loaded(s) &&
Jan Kara00b441972008-07-25 01:46:38 -07001268 *qfmt != REISERFS_SB(s)->s_jquota_fmt) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001269 reiserfs_warning(s, "super-6515",
1270 "cannot change journaled "
1271 "quota options when quota "
1272 "turned on.");
Jan Kara00b441972008-07-25 01:46:38 -07001273 return 0;
1274 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001275 }
1276#else
1277 if (c == 'u' || c == 'g' || c == 'f') {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001278 reiserfs_warning(s, "reiserfs-2503", "journaled "
1279 "quota options not supported.");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001280 return 0;
1281 }
1282#endif
1283 }
1284
1285#ifdef CONFIG_QUOTA
Jan Kara00b441972008-07-25 01:46:38 -07001286 if (!REISERFS_SB(s)->s_jquota_fmt && !*qfmt
1287 && (qf_names[USRQUOTA] || qf_names[GRPQUOTA])) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001288 reiserfs_warning(s, "super-6515",
1289 "journaled quota format not specified.");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001290 return 0;
1291 }
Jan Karac3aa0772011-12-21 20:17:10 +01001292 if ((!(*mount_options & (1 << REISERFS_USRQUOTA)) &&
1293 sb_has_quota_loaded(s, USRQUOTA)) ||
1294 (!(*mount_options & (1 << REISERFS_GRPQUOTA)) &&
1295 sb_has_quota_loaded(s, GRPQUOTA))) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001296 reiserfs_warning(s, "super-6516", "quota options must "
1297 "be present when quota is turned on.");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001298 return 0;
1299 }
1300#endif
1301
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303}
1304
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001305static void switch_data_mode(struct super_block *s, unsigned long mode)
1306{
1307 REISERFS_SB(s)->s_mount_opt &= ~((1 << REISERFS_DATA_LOG) |
1308 (1 << REISERFS_DATA_ORDERED) |
1309 (1 << REISERFS_DATA_WRITEBACK));
1310 REISERFS_SB(s)->s_mount_opt |= (1 << mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311}
1312
1313static void handle_data_mode(struct super_block *s, unsigned long mount_options)
1314{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001315 if (mount_options & (1 << REISERFS_DATA_LOG)) {
1316 if (!reiserfs_data_log(s)) {
1317 switch_data_mode(s, REISERFS_DATA_LOG);
1318 reiserfs_info(s, "switching to journaled data mode\n");
1319 }
1320 } else if (mount_options & (1 << REISERFS_DATA_ORDERED)) {
1321 if (!reiserfs_data_ordered(s)) {
1322 switch_data_mode(s, REISERFS_DATA_ORDERED);
1323 reiserfs_info(s, "switching to ordered data mode\n");
1324 }
1325 } else if (mount_options & (1 << REISERFS_DATA_WRITEBACK)) {
1326 if (!reiserfs_data_writeback(s)) {
1327 switch_data_mode(s, REISERFS_DATA_WRITEBACK);
1328 reiserfs_info(s, "switching to writeback data mode\n");
1329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331}
1332
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001333static void handle_barrier_mode(struct super_block *s, unsigned long bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001335 int flush = (1 << REISERFS_BARRIER_FLUSH);
1336 int none = (1 << REISERFS_BARRIER_NONE);
1337 int all_barrier = flush | none;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001339 if (bits & all_barrier) {
1340 REISERFS_SB(s)->s_mount_opt &= ~all_barrier;
1341 if (bits & flush) {
1342 REISERFS_SB(s)->s_mount_opt |= flush;
1343 printk("reiserfs: enabling write barrier flush mode\n");
1344 } else if (bits & none) {
1345 REISERFS_SB(s)->s_mount_opt |= none;
1346 printk("reiserfs: write barriers turned off\n");
1347 }
1348 }
1349}
1350
1351static void handle_attrs(struct super_block *s)
1352{
1353 struct reiserfs_super_block *rs = SB_DISK_SUPER_BLOCK(s);
1354
1355 if (reiserfs_attrs(s)) {
1356 if (old_format_only(s)) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001357 reiserfs_warning(s, "super-6517", "cannot support "
1358 "attributes on 3.5.x disk format");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001359 REISERFS_SB(s)->s_mount_opt &= ~(1 << REISERFS_ATTRS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 return;
1361 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001362 if (!(le32_to_cpu(rs->s_flags) & reiserfs_attrs_cleared)) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001363 reiserfs_warning(s, "super-6518", "cannot support "
1364 "attributes until flag is set in "
1365 "super-block");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001366 REISERFS_SB(s)->s_mount_opt &= ~(1 << REISERFS_ATTRS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 }
1368 }
1369}
1370
Jan Kara00b441972008-07-25 01:46:38 -07001371#ifdef CONFIG_QUOTA
1372static void handle_quota_files(struct super_block *s, char **qf_names,
1373 unsigned int *qfmt)
1374{
1375 int i;
1376
Jan Karaaca60612014-09-10 20:57:45 +02001377 for (i = 0; i < REISERFS_MAXQUOTAS; i++) {
Jan Kara00b441972008-07-25 01:46:38 -07001378 if (qf_names[i] != REISERFS_SB(s)->s_qf_names[i])
1379 kfree(REISERFS_SB(s)->s_qf_names[i]);
1380 REISERFS_SB(s)->s_qf_names[i] = qf_names[i];
1381 }
Jan Karaa06d7892011-12-21 17:35:34 +01001382 if (*qfmt)
1383 REISERFS_SB(s)->s_jquota_fmt = *qfmt;
Jan Kara00b441972008-07-25 01:46:38 -07001384}
1385#endif
1386
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001387static int reiserfs_remount(struct super_block *s, int *mount_flags, char *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001389 struct reiserfs_super_block *rs;
1390 struct reiserfs_transaction_handle th;
1391 unsigned long blocks;
1392 unsigned long mount_options = REISERFS_SB(s)->s_mount_opt;
1393 unsigned long safe_mask = 0;
1394 unsigned int commit_max_age = (unsigned int)-1;
1395 struct reiserfs_journal *journal = SB_JOURNAL(s);
Miklos Szeredicdf6ccc2008-02-08 04:21:47 -08001396 char *new_opts = kstrdup(arg, GFP_KERNEL);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001397 int err;
Jan Karaaca60612014-09-10 20:57:45 +02001398 char *qf_names[REISERFS_MAXQUOTAS];
Jan Kara00b441972008-07-25 01:46:38 -07001399 unsigned int qfmt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400#ifdef CONFIG_QUOTA
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001401 int i;
Frederic Weisbecker8ebc4232009-04-07 04:19:49 +02001402#endif
Jan Kara00b441972008-07-25 01:46:38 -07001403
Theodore Ts'o02b99842014-03-13 10:14:33 -04001404 sync_filesystem(s);
Frederic Weisbecker8ebc4232009-04-07 04:19:49 +02001405 reiserfs_write_lock(s);
1406
1407#ifdef CONFIG_QUOTA
Jan Kara00b441972008-07-25 01:46:38 -07001408 memcpy(qf_names, REISERFS_SB(s)->s_qf_names, sizeof(qf_names));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409#endif
1410
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001411 rs = SB_DISK_SUPER_BLOCK(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001413 if (!reiserfs_parse_options
Jan Kara00b441972008-07-25 01:46:38 -07001414 (s, arg, &mount_options, &blocks, NULL, &commit_max_age,
1415 qf_names, &qfmt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416#ifdef CONFIG_QUOTA
Jan Karaaca60612014-09-10 20:57:45 +02001417 for (i = 0; i < REISERFS_MAXQUOTAS; i++)
Jan Kara00b441972008-07-25 01:46:38 -07001418 if (qf_names[i] != REISERFS_SB(s)->s_qf_names[i])
1419 kfree(qf_names[i]);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001420#endif
Miklos Szeredicdf6ccc2008-02-08 04:21:47 -08001421 err = -EINVAL;
Jeff Mahoney4c051412013-08-08 17:27:19 -04001422 goto out_err_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 }
Jan Kara00b441972008-07-25 01:46:38 -07001424#ifdef CONFIG_QUOTA
1425 handle_quota_files(s, qf_names, &qfmt);
1426#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001428 handle_attrs(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001430 /* Add options that are safe here */
1431 safe_mask |= 1 << REISERFS_SMALLTAIL;
1432 safe_mask |= 1 << REISERFS_LARGETAIL;
1433 safe_mask |= 1 << REISERFS_NO_BORDER;
1434 safe_mask |= 1 << REISERFS_NO_UNHASHED_RELOCATION;
1435 safe_mask |= 1 << REISERFS_HASHED_RELOCATION;
1436 safe_mask |= 1 << REISERFS_TEST4;
1437 safe_mask |= 1 << REISERFS_ATTRS;
1438 safe_mask |= 1 << REISERFS_XATTRS_USER;
1439 safe_mask |= 1 << REISERFS_POSIXACL;
1440 safe_mask |= 1 << REISERFS_BARRIER_FLUSH;
1441 safe_mask |= 1 << REISERFS_BARRIER_NONE;
1442 safe_mask |= 1 << REISERFS_ERROR_RO;
1443 safe_mask |= 1 << REISERFS_ERROR_CONTINUE;
1444 safe_mask |= 1 << REISERFS_ERROR_PANIC;
Jan Karac3aa0772011-12-21 20:17:10 +01001445 safe_mask |= 1 << REISERFS_USRQUOTA;
1446 safe_mask |= 1 << REISERFS_GRPQUOTA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
Jeff Mahoney098297b2014-04-23 10:00:36 -04001448 /*
1449 * Update the bitmask, taking care to keep
1450 * the bits we're not allowed to change here
1451 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001452 REISERFS_SB(s)->s_mount_opt =
1453 (REISERFS_SB(s)->
1454 s_mount_opt & ~safe_mask) | (mount_options & safe_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001456 if (commit_max_age != 0 && commit_max_age != (unsigned int)-1) {
1457 journal->j_max_commit_age = commit_max_age;
1458 journal->j_max_trans_age = commit_max_age;
1459 } else if (commit_max_age == 0) {
1460 /* 0 means restore defaults. */
1461 journal->j_max_commit_age = journal->j_default_max_commit_age;
1462 journal->j_max_trans_age = JOURNAL_MAX_TRANS_AGE;
1463 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001465 if (blocks) {
Miklos Szeredicdf6ccc2008-02-08 04:21:47 -08001466 err = reiserfs_resize(s, blocks);
1467 if (err != 0)
Jeff Mahoney4c051412013-08-08 17:27:19 -04001468 goto out_err_unlock;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001469 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001471 if (*mount_flags & MS_RDONLY) {
Jeff Mahoney4c051412013-08-08 17:27:19 -04001472 reiserfs_write_unlock(s);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001473 reiserfs_xattr_init(s, *mount_flags);
1474 /* remount read-only */
1475 if (s->s_flags & MS_RDONLY)
1476 /* it is read-only already */
Jeff Mahoney4c051412013-08-08 17:27:19 -04001477 goto out_ok_unlocked;
Christoph Hellwigc79d9672010-05-19 07:16:40 -04001478
Christoph Hellwig0f0dd622010-05-19 07:16:41 -04001479 err = dquot_suspend(s, -1);
1480 if (err < 0)
Christoph Hellwigc79d9672010-05-19 07:16:40 -04001481 goto out_err;
Christoph Hellwigc79d9672010-05-19 07:16:40 -04001482
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001483 /* try to remount file system with read-only permissions */
1484 if (sb_umount_state(rs) == REISERFS_VALID_FS
1485 || REISERFS_SB(s)->s_mount_state != REISERFS_VALID_FS) {
Jeff Mahoney4c051412013-08-08 17:27:19 -04001486 goto out_ok_unlocked;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
Jeff Mahoney4c051412013-08-08 17:27:19 -04001489 reiserfs_write_lock(s);
1490
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001491 err = journal_begin(&th, s, 10);
1492 if (err)
Jeff Mahoney4c051412013-08-08 17:27:19 -04001493 goto out_err_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001495 /* Mounting a rw partition read-only. */
1496 reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s), 1);
1497 set_sb_umount_state(rs, REISERFS_SB(s)->s_mount_state);
Jeff Mahoney09f1b802014-04-23 10:00:39 -04001498 journal_mark_dirty(&th, SB_BUFFER_WITH_SB(s));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001499 } else {
1500 /* remount read-write */
1501 if (!(s->s_flags & MS_RDONLY)) {
Jeff Mahoney4c051412013-08-08 17:27:19 -04001502 reiserfs_write_unlock(s);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001503 reiserfs_xattr_init(s, *mount_flags);
Jeff Mahoney4c051412013-08-08 17:27:19 -04001504 goto out_ok_unlocked; /* We are read-write already */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001505 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
Miklos Szeredicdf6ccc2008-02-08 04:21:47 -08001507 if (reiserfs_is_journal_aborted(journal)) {
1508 err = journal->j_errno;
Jeff Mahoney4c051412013-08-08 17:27:19 -04001509 goto out_err_unlock;
Miklos Szeredicdf6ccc2008-02-08 04:21:47 -08001510 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001512 handle_data_mode(s, mount_options);
1513 handle_barrier_mode(s, mount_options);
1514 REISERFS_SB(s)->s_mount_state = sb_umount_state(rs);
Jeff Mahoney098297b2014-04-23 10:00:36 -04001515
1516 /* now it is safe to call journal_begin */
1517 s->s_flags &= ~MS_RDONLY;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001518 err = journal_begin(&th, s, 10);
1519 if (err)
Jeff Mahoney4c051412013-08-08 17:27:19 -04001520 goto out_err_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001522 /* Mount a partition which is read-only, read-write */
1523 reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s), 1);
1524 REISERFS_SB(s)->s_mount_state = sb_umount_state(rs);
1525 s->s_flags &= ~MS_RDONLY;
1526 set_sb_umount_state(rs, REISERFS_ERROR_FS);
Jeff Mahoney702d21c2009-03-30 14:02:16 -04001527 if (!old_format_only(s))
1528 set_sb_mnt_count(rs, sb_mnt_count(rs) + 1);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001529 /* mark_buffer_dirty (SB_BUFFER_WITH_SB (s), 1); */
Jeff Mahoney09f1b802014-04-23 10:00:39 -04001530 journal_mark_dirty(&th, SB_BUFFER_WITH_SB(s));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001531 REISERFS_SB(s)->s_mount_state = REISERFS_VALID_FS;
1532 }
1533 /* this will force a full flush of all journal lists */
1534 SB_JOURNAL(s)->j_must_wait = 1;
Jeff Mahoney58d85422014-04-23 10:00:38 -04001535 err = journal_end(&th);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001536 if (err)
Jeff Mahoney4c051412013-08-08 17:27:19 -04001537 goto out_err_unlock;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001538
Jeff Mahoney4c051412013-08-08 17:27:19 -04001539 reiserfs_write_unlock(s);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001540 if (!(*mount_flags & MS_RDONLY)) {
Christoph Hellwig0f0dd622010-05-19 07:16:41 -04001541 dquot_resume(s, -1);
Jan Kara3bb3e1f2012-11-13 14:55:52 +01001542 reiserfs_write_lock(s);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001543 finish_unfinished(s);
Jeff Mahoney4c051412013-08-08 17:27:19 -04001544 reiserfs_write_unlock(s);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001545 reiserfs_xattr_init(s, *mount_flags);
1546 }
1547
Jeff Mahoney4c051412013-08-08 17:27:19 -04001548out_ok_unlocked:
Al Viro2a32ceb2009-05-08 16:05:57 -04001549 replace_mount_options(s, new_opts);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001550 return 0;
Miklos Szeredicdf6ccc2008-02-08 04:21:47 -08001551
Jeff Mahoney4c051412013-08-08 17:27:19 -04001552out_err_unlock:
Jan Kara3bb3e1f2012-11-13 14:55:52 +01001553 reiserfs_write_unlock(s);
Miklos Szeredicdf6ccc2008-02-08 04:21:47 -08001554out_err:
1555 kfree(new_opts);
1556 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557}
1558
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001559static int read_super_block(struct super_block *s, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001561 struct buffer_head *bh;
1562 struct reiserfs_super_block *rs;
1563 int fs_blocksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001565 bh = sb_bread(s, offset / s->s_blocksize);
1566 if (!bh) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001567 reiserfs_warning(s, "sh-2006",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001568 "bread failed (dev %s, block %lu, size %lu)",
Al Viro2ccdc412013-11-22 13:25:39 -05001569 s->s_id, offset / s->s_blocksize,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001570 s->s_blocksize);
1571 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001574 rs = (struct reiserfs_super_block *)bh->b_data;
1575 if (!is_any_reiserfs_magic_string(rs)) {
1576 brelse(bh);
1577 return 1;
1578 }
Jeff Mahoney098297b2014-04-23 10:00:36 -04001579 /*
1580 * ok, reiserfs signature (old or new) found in at the given offset
1581 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001582 fs_blocksize = sb_blocksize(rs);
1583 brelse(bh);
1584 sb_set_blocksize(s, fs_blocksize);
1585
1586 bh = sb_bread(s, offset / s->s_blocksize);
1587 if (!bh) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001588 reiserfs_warning(s, "sh-2007",
1589 "bread failed (dev %s, block %lu, size %lu)",
Al Viro2ccdc412013-11-22 13:25:39 -05001590 s->s_id, offset / s->s_blocksize,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001591 s->s_blocksize);
1592 return 1;
1593 }
1594
1595 rs = (struct reiserfs_super_block *)bh->b_data;
1596 if (sb_blocksize(rs) != s->s_blocksize) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001597 reiserfs_warning(s, "sh-2011", "can't find a reiserfs "
Fabian Frederick53872ed2014-08-08 14:21:10 -07001598 "filesystem on (dev %s, block %llu, size %lu)",
Al Viro2ccdc412013-11-22 13:25:39 -05001599 s->s_id,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001600 (unsigned long long)bh->b_blocknr,
1601 s->s_blocksize);
1602 brelse(bh);
1603 return 1;
1604 }
1605
1606 if (rs->s_v1.s_root_block == cpu_to_le32(-1)) {
1607 brelse(bh);
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001608 reiserfs_warning(s, "super-6519", "Unfinished reiserfsck "
1609 "--rebuild-tree run detected. Please run\n"
1610 "reiserfsck --rebuild-tree and wait for a "
1611 "completion. If that fails\n"
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001612 "get newer reiserfsprogs package");
1613 return 1;
1614 }
1615
1616 SB_BUFFER_WITH_SB(s) = bh;
1617 SB_DISK_SUPER_BLOCK(s) = rs;
1618
Jeff Mahoney098297b2014-04-23 10:00:36 -04001619 /*
1620 * magic is of non-standard journal filesystem, look at s_version to
1621 * find which format is in use
1622 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001623 if (is_reiserfs_jr(rs)) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001624 if (sb_version(rs) == REISERFS_VERSION_2)
Jeff Mahoney1d889d92009-03-30 14:02:20 -04001625 reiserfs_info(s, "found reiserfs format \"3.6\""
1626 " with non-standard journal\n");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001627 else if (sb_version(rs) == REISERFS_VERSION_1)
Jeff Mahoney1d889d92009-03-30 14:02:20 -04001628 reiserfs_info(s, "found reiserfs format \"3.5\""
1629 " with non-standard journal\n");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001630 else {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001631 reiserfs_warning(s, "sh-2012", "found unknown "
1632 "format \"%u\" of reiserfs with "
1633 "non-standard magic", sb_version(rs));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001634 return 1;
1635 }
1636 } else
Jeff Mahoney098297b2014-04-23 10:00:36 -04001637 /*
1638 * s_version of standard format may contain incorrect
1639 * information, so we just look at the magic string
1640 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001641 reiserfs_info(s,
1642 "found reiserfs format \"%s\" with standard journal\n",
1643 is_reiserfs_3_5(rs) ? "3.5" : "3.6");
1644
1645 s->s_op = &reiserfs_sops;
1646 s->s_export_op = &reiserfs_export_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647#ifdef CONFIG_QUOTA
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001648 s->s_qcop = &reiserfs_qctl_operations;
1649 s->dq_op = &reiserfs_quota_operations;
Jan Kara53873632014-09-29 15:08:19 +02001650 s->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651#endif
1652
Jeff Mahoney098297b2014-04-23 10:00:36 -04001653 /*
1654 * new format is limited by the 32 bit wide i_blocks field, want to
1655 * be one full block below that.
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001656 */
1657 s->s_maxbytes = (512LL << 32) - s->s_blocksize;
1658 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659}
1660
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661/* after journal replay, reread all bitmap and super blocks */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001662static int reread_meta_blocks(struct super_block *s)
1663{
Mike Christiedfec8a12016-06-05 14:31:44 -05001664 ll_rw_block(REQ_OP_READ, 0, 1, &SB_BUFFER_WITH_SB(s));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001665 wait_on_buffer(SB_BUFFER_WITH_SB(s));
1666 if (!buffer_uptodate(SB_BUFFER_WITH_SB(s))) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001667 reiserfs_warning(s, "reiserfs-2504", "error reading the super");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001668 return 1;
1669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001671 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672}
1673
Jeff Mahoney098297b2014-04-23 10:00:36 -04001674/* hash detection stuff */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
Jeff Mahoney098297b2014-04-23 10:00:36 -04001676/*
1677 * if root directory is empty - we set default - Yura's - hash and
1678 * warn about it
1679 * FIXME: we look for only one name in a directory. If tea and yura
1680 * both have the same value - we ask user to send report to the
1681 * mailing list
1682 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001683static __u32 find_hash_out(struct super_block *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001685 int retval;
1686 struct inode *inode;
1687 struct cpu_key key;
1688 INITIALIZE_PATH(path);
1689 struct reiserfs_dir_entry de;
Jeff Mahoneyc48138c2014-04-23 10:00:44 -04001690 struct reiserfs_de_head *deh;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001691 __u32 hash = DEFAULT_HASH;
Jeff Mahoneyc48138c2014-04-23 10:00:44 -04001692 __u32 deh_hashval, teahash, r5hash, yurahash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
David Howells2b0143b2015-03-17 22:25:59 +00001694 inode = d_inode(s->s_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
Jeff Mahoneyc48138c2014-04-23 10:00:44 -04001696 make_cpu_key(&key, inode, ~0, TYPE_DIRENTRY, 3);
1697 retval = search_by_entry_key(s, &key, &path, &de);
1698 if (retval == IO_ERROR) {
1699 pathrelse(&path);
1700 return UNSET_HASH;
1701 }
1702 if (retval == NAME_NOT_FOUND)
1703 de.de_entry_num--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704
Jeff Mahoneyc48138c2014-04-23 10:00:44 -04001705 set_de_name_and_namelen(&de);
1706 deh = de.de_deh + de.de_entry_num;
1707
1708 if (deh_offset(deh) == DOT_DOT_OFFSET) {
1709 /* allow override in this case */
1710 if (reiserfs_rupasov_hash(s))
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001711 hash = YURA_HASH;
Jeff Mahoneyc48138c2014-04-23 10:00:44 -04001712 reiserfs_info(s, "FS seems to be empty, autodetect is using the default hash\n");
1713 goto out;
1714 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715
Jeff Mahoneyc48138c2014-04-23 10:00:44 -04001716 deh_hashval = GET_HASH_VALUE(deh_offset(deh));
1717 r5hash = GET_HASH_VALUE(r5_hash(de.de_name, de.de_namelen));
1718 teahash = GET_HASH_VALUE(keyed_hash(de.de_name, de.de_namelen));
1719 yurahash = GET_HASH_VALUE(yura_hash(de.de_name, de.de_namelen));
1720
1721 if ((teahash == r5hash && deh_hashval == r5hash) ||
1722 (teahash == yurahash && deh_hashval == yurahash) ||
1723 (r5hash == yurahash && deh_hashval == yurahash)) {
1724 reiserfs_warning(s, "reiserfs-2506",
1725 "Unable to automatically detect hash "
1726 "function. Please mount with -o "
1727 "hash={tea,rupasov,r5}");
1728 hash = UNSET_HASH;
1729 goto out;
1730 }
1731
1732 if (deh_hashval == yurahash)
1733 hash = YURA_HASH;
1734 else if (deh_hashval == teahash)
1735 hash = TEA_HASH;
1736 else if (deh_hashval == r5hash)
1737 hash = R5_HASH;
1738 else {
1739 reiserfs_warning(s, "reiserfs-2506",
1740 "Unrecognised hash function");
1741 hash = UNSET_HASH;
1742 }
1743out:
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001744 pathrelse(&path);
1745 return hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746}
1747
Jeff Mahoney098297b2014-04-23 10:00:36 -04001748/* finds out which hash names are sorted with */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001749static int what_hash(struct super_block *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001751 __u32 code;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001753 code = sb_hash_function_code(SB_DISK_SUPER_BLOCK(s));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
Jeff Mahoney098297b2014-04-23 10:00:36 -04001755 /*
1756 * reiserfs_hash_detect() == true if any of the hash mount options
1757 * were used. We must check them to make sure the user isn't
1758 * using a bad hash value
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001759 */
1760 if (code == UNSET_HASH || reiserfs_hash_detect(s))
1761 code = find_hash_out(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001763 if (code != UNSET_HASH && reiserfs_hash_detect(s)) {
Jeff Mahoney098297b2014-04-23 10:00:36 -04001764 /*
1765 * detection has found the hash, and we must check against the
1766 * mount options
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001767 */
1768 if (reiserfs_rupasov_hash(s) && code != YURA_HASH) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001769 reiserfs_warning(s, "reiserfs-2507",
1770 "Error, %s hash detected, "
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001771 "unable to force rupasov hash",
1772 reiserfs_hashname(code));
1773 code = UNSET_HASH;
1774 } else if (reiserfs_tea_hash(s) && code != TEA_HASH) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001775 reiserfs_warning(s, "reiserfs-2508",
1776 "Error, %s hash detected, "
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001777 "unable to force tea hash",
1778 reiserfs_hashname(code));
1779 code = UNSET_HASH;
1780 } else if (reiserfs_r5_hash(s) && code != R5_HASH) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001781 reiserfs_warning(s, "reiserfs-2509",
1782 "Error, %s hash detected, "
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001783 "unable to force r5 hash",
1784 reiserfs_hashname(code));
1785 code = UNSET_HASH;
1786 }
1787 } else {
Jeff Mahoney098297b2014-04-23 10:00:36 -04001788 /*
1789 * find_hash_out was not called or
1790 * could not determine the hash
1791 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001792 if (reiserfs_rupasov_hash(s)) {
1793 code = YURA_HASH;
1794 } else if (reiserfs_tea_hash(s)) {
1795 code = TEA_HASH;
1796 } else if (reiserfs_r5_hash(s)) {
1797 code = R5_HASH;
1798 }
1799 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800
Jeff Mahoney098297b2014-04-23 10:00:36 -04001801 /*
1802 * if we are mounted RW, and we have a new valid hash code, update
1803 * the super
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001804 */
1805 if (code != UNSET_HASH &&
1806 !(s->s_flags & MS_RDONLY) &&
1807 code != sb_hash_function_code(SB_DISK_SUPER_BLOCK(s))) {
1808 set_sb_hash_function_code(SB_DISK_SUPER_BLOCK(s), code);
1809 }
1810 return code;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811}
1812
Jeff Mahoney098297b2014-04-23 10:00:36 -04001813/* return pointer to appropriate function */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001814static hashf_t hash_function(struct super_block *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001816 switch (what_hash(s)) {
1817 case TEA_HASH:
1818 reiserfs_info(s, "Using tea hash to sort names\n");
1819 return keyed_hash;
1820 case YURA_HASH:
1821 reiserfs_info(s, "Using rupasov hash to sort names\n");
1822 return yura_hash;
1823 case R5_HASH:
1824 reiserfs_info(s, "Using r5 hash to sort names\n");
1825 return r5_hash;
1826 }
1827 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828}
1829
Jeff Mahoney098297b2014-04-23 10:00:36 -04001830/* this is used to set up correct value for old partitions */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001831static int function2code(hashf_t func)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001833 if (func == keyed_hash)
1834 return TEA_HASH;
1835 if (func == yura_hash)
1836 return YURA_HASH;
1837 if (func == r5_hash)
1838 return R5_HASH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
Jeff Mahoney098297b2014-04-23 10:00:36 -04001840 BUG(); /* should never happen */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001842 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843}
1844
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001845#define SWARN(silent, s, id, ...) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 if (!(silent)) \
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001847 reiserfs_warning(s, id, __VA_ARGS__)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001849static int reiserfs_fill_super(struct super_block *s, void *data, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001851 struct inode *root_inode;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001852 struct reiserfs_transaction_handle th;
1853 int old_format = 0;
1854 unsigned long blocks;
1855 unsigned int commit_max_age = 0;
1856 int jinit_done = 0;
1857 struct reiserfs_iget_args args;
1858 struct reiserfs_super_block *rs;
1859 char *jdev_name;
1860 struct reiserfs_sb_info *sbi;
1861 int errval = -EINVAL;
Jan Karaaca60612014-09-10 20:57:45 +02001862 char *qf_names[REISERFS_MAXQUOTAS] = {};
Jan Kara00b441972008-07-25 01:46:38 -07001863 unsigned int qfmt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864
Miklos Szeredicdf6ccc2008-02-08 04:21:47 -08001865 save_mount_options(s, data);
1866
Yan Burman01afb212006-12-06 20:39:01 -08001867 sbi = kzalloc(sizeof(struct reiserfs_sb_info), GFP_KERNEL);
Jeff Mahoneyb7b7fa42010-03-29 15:12:39 -04001868 if (!sbi)
1869 return -ENOMEM;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001870 s->s_fs_info = sbi;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001871 /* Set default values for options: non-aggressive tails, RO on errors */
Artem Bityutskiyefaa33e2012-06-01 17:18:04 +03001872 sbi->s_mount_opt |= (1 << REISERFS_SMALLTAIL);
1873 sbi->s_mount_opt |= (1 << REISERFS_ERROR_RO);
1874 sbi->s_mount_opt |= (1 << REISERFS_BARRIER_FLUSH);
Jeff Mahoney098297b2014-04-23 10:00:36 -04001875 /* no preallocation minimum, be smart in reiserfs_file_write instead */
Artem Bityutskiyefaa33e2012-06-01 17:18:04 +03001876 sbi->s_alloc_options.preallocmin = 0;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001877 /* Preallocate by 16 blocks (17-1) at once */
Artem Bityutskiyefaa33e2012-06-01 17:18:04 +03001878 sbi->s_alloc_options.preallocsize = 17;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001879 /* setup default block allocator options */
1880 reiserfs_init_alloc_options(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
Artem Bityutskiy033369d2012-06-01 17:18:08 +03001882 spin_lock_init(&sbi->old_work_lock);
1883 INIT_DELAYED_WORK(&sbi->old_work, flush_old_commits);
Artem Bityutskiyefaa33e2012-06-01 17:18:04 +03001884 mutex_init(&sbi->lock);
1885 sbi->lock_depth = -1;
Frederic Weisbecker8ebc4232009-04-07 04:19:49 +02001886
Jeff Mahoney797d9012014-04-23 10:00:34 -04001887 sbi->commit_wq = alloc_workqueue("reiserfs/%s", WQ_MEM_RECLAIM, 0,
1888 s->s_id);
1889 if (!sbi->commit_wq) {
1890 SWARN(silent, s, "", "Cannot allocate commit workqueue");
1891 errval = -ENOMEM;
1892 goto error_unlocked;
1893 }
1894
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001895 jdev_name = NULL;
1896 if (reiserfs_parse_options
Jeff Mahoneya228bf82014-04-23 10:00:42 -04001897 (s, (char *)data, &sbi->s_mount_opt, &blocks, &jdev_name,
Jan Kara00b441972008-07-25 01:46:38 -07001898 &commit_max_age, qf_names, &qfmt) == 0) {
Frederic Weisbeckerf32485b2012-01-10 15:11:07 -08001899 goto error_unlocked;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001900 }
Jan Karac3aa0772011-12-21 20:17:10 +01001901 if (jdev_name && jdev_name[0]) {
Artem Bityutskiyefaa33e2012-06-01 17:18:04 +03001902 sbi->s_jdev = kstrdup(jdev_name, GFP_KERNEL);
1903 if (!sbi->s_jdev) {
Jan Karac3aa0772011-12-21 20:17:10 +01001904 SWARN(silent, s, "", "Cannot allocate memory for "
1905 "journal device name");
1906 goto error;
1907 }
1908 }
Jan Kara00b441972008-07-25 01:46:38 -07001909#ifdef CONFIG_QUOTA
1910 handle_quota_files(s, qf_names, &qfmt);
1911#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001913 if (blocks) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001914 SWARN(silent, s, "jmacd-7", "resize option for remount only");
Frederic Weisbeckerf32485b2012-01-10 15:11:07 -08001915 goto error_unlocked;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001916 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
Jeff Mahoney098297b2014-04-23 10:00:36 -04001918 /*
1919 * try old format (undistributed bitmap, super block in 8-th 1k
1920 * block of a device)
1921 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001922 if (!read_super_block(s, REISERFS_OLD_DISK_OFFSET_IN_BYTES))
1923 old_format = 1;
Jeff Mahoney098297b2014-04-23 10:00:36 -04001924
1925 /*
1926 * try new format (64-th 1k block), which can contain reiserfs
1927 * super block
1928 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001929 else if (read_super_block(s, REISERFS_DISK_OFFSET_IN_BYTES)) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001930 SWARN(silent, s, "sh-2021", "can not find reiserfs on %s",
Al Viro2ccdc412013-11-22 13:25:39 -05001931 s->s_id);
Frederic Weisbeckerf32485b2012-01-10 15:11:07 -08001932 goto error_unlocked;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001933 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001935 rs = SB_DISK_SUPER_BLOCK(s);
Jeff Mahoney098297b2014-04-23 10:00:36 -04001936 /*
1937 * Let's do basic sanity check to verify that underlying device is not
1938 * smaller than the filesystem. If the check fails then abort and
1939 * scream, because bad stuff will happen otherwise.
1940 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001941 if (s->s_bdev && s->s_bdev->bd_inode
1942 && i_size_read(s->s_bdev->bd_inode) <
1943 sb_block_count(rs) * sb_blocksize(rs)) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001944 SWARN(silent, s, "", "Filesystem cannot be "
1945 "mounted because it is bigger than the device");
1946 SWARN(silent, s, "", "You may need to run fsck "
1947 "or increase size of your LVM partition");
1948 SWARN(silent, s, "", "Or may be you forgot to "
1949 "reboot after fdisk when it told you to");
Frederic Weisbeckerf32485b2012-01-10 15:11:07 -08001950 goto error_unlocked;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001951 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001953 sbi->s_mount_state = SB_REISERFS_STATE(s);
1954 sbi->s_mount_state = REISERFS_VALID_FS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955
Jeff Mahoney6f010462006-09-30 23:28:43 -07001956 if ((errval = reiserfs_init_bitmap_cache(s))) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001957 SWARN(silent, s, "jmacd-8", "unable to read bitmap");
Frederic Weisbeckerf32485b2012-01-10 15:11:07 -08001958 goto error_unlocked;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001959 }
Frederic Weisbeckerf32485b2012-01-10 15:11:07 -08001960
Jeff Mahoneyd2c89a422006-11-02 22:07:20 -08001961 errval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962#ifdef CONFIG_REISERFS_CHECK
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001963 SWARN(silent, s, "", "CONFIG_REISERFS_CHECK is set ON");
1964 SWARN(silent, s, "", "- it is slow mode for debugging.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965#endif
1966
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001967 /* make data=ordered the default */
1968 if (!reiserfs_data_log(s) && !reiserfs_data_ordered(s) &&
1969 !reiserfs_data_writeback(s)) {
Artem Bityutskiyefaa33e2012-06-01 17:18:04 +03001970 sbi->s_mount_opt |= (1 << REISERFS_DATA_ORDERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 }
1972
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001973 if (reiserfs_data_log(s)) {
1974 reiserfs_info(s, "using journaled data mode\n");
1975 } else if (reiserfs_data_ordered(s)) {
1976 reiserfs_info(s, "using ordered data mode\n");
1977 } else {
1978 reiserfs_info(s, "using writeback data mode\n");
1979 }
1980 if (reiserfs_barrier_flush(s)) {
1981 printk("reiserfs: using flush barriers\n");
1982 }
Frederic Weisbeckerf32485b2012-01-10 15:11:07 -08001983
Frederic Weisbecker37c69b92012-01-10 15:11:09 -08001984 if (journal_init(s, jdev_name, old_format, commit_max_age)) {
1985 SWARN(silent, s, "sh-2022",
1986 "unable to initialize journal space");
1987 goto error_unlocked;
1988 } else {
Jeff Mahoney098297b2014-04-23 10:00:36 -04001989 /*
1990 * once this is set, journal_release must be called
1991 * if we error out of the mount
1992 */
1993 jinit_done = 1;
Frederic Weisbecker37c69b92012-01-10 15:11:09 -08001994 }
1995
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001996 if (reread_meta_blocks(s)) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001997 SWARN(silent, s, "jmacd-9",
1998 "unable to reread meta blocks after journal init");
Frederic Weisbecker9b467e62012-01-10 15:11:11 -08001999 goto error_unlocked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 }
2001
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002002 if (replay_only(s))
Frederic Weisbecker9b467e62012-01-10 15:11:11 -08002003 goto error_unlocked;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002004
2005 if (bdev_read_only(s->s_bdev) && !(s->s_flags & MS_RDONLY)) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04002006 SWARN(silent, s, "clm-7000",
2007 "Detected readonly device, marking FS readonly");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002008 s->s_flags |= MS_RDONLY;
2009 }
2010 args.objectid = REISERFS_ROOT_OBJECTID;
2011 args.dirid = REISERFS_ROOT_PARENT_OBJECTID;
2012 root_inode =
2013 iget5_locked(s, REISERFS_ROOT_OBJECTID, reiserfs_find_actor,
Jeff Mahoneya228bf82014-04-23 10:00:42 -04002014 reiserfs_init_locked_inode, (void *)&args);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002015 if (!root_inode) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04002016 SWARN(silent, s, "jmacd-10", "get root inode failed");
Frederic Weisbecker9b467e62012-01-10 15:11:11 -08002017 goto error_unlocked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 }
2019
Frederic Weisbecker9b467e62012-01-10 15:11:11 -08002020 /*
2021 * This path assumed to be called with the BKL in the old times.
2022 * Now we have inherited the big reiserfs lock from it and many
2023 * reiserfs helpers called in the mount path and elsewhere require
2024 * this lock to be held even if it's not always necessary. Let's be
2025 * conservative and hold it early. The window can be reduced after
2026 * careful review of the code.
2027 */
2028 reiserfs_write_lock(s);
2029
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002030 if (root_inode->i_state & I_NEW) {
2031 reiserfs_read_locked_inode(root_inode, &args);
2032 unlock_new_inode(root_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 }
2034
Al Viro48fde702012-01-08 22:15:13 -05002035 s->s_root = d_make_root(root_inode);
2036 if (!s->s_root)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002037 goto error;
Jeff Mahoney098297b2014-04-23 10:00:36 -04002038 /* define and initialize hash function */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002039 sbi->s_hash_function = hash_function(s);
2040 if (sbi->s_hash_function == NULL) {
2041 dput(s->s_root);
2042 s->s_root = NULL;
2043 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002045
2046 if (is_reiserfs_3_5(rs)
2047 || (is_reiserfs_jr(rs) && SB_VERSION(s) == REISERFS_VERSION_1))
Jeff Mahoneya228bf82014-04-23 10:00:42 -04002048 set_bit(REISERFS_3_5, &sbi->s_properties);
Jeff Mahoneye1fabd32006-09-30 23:28:40 -07002049 else if (old_format)
Jeff Mahoneya228bf82014-04-23 10:00:42 -04002050 set_bit(REISERFS_OLD_FORMAT, &sbi->s_properties);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002051 else
Jeff Mahoneya228bf82014-04-23 10:00:42 -04002052 set_bit(REISERFS_3_6, &sbi->s_properties);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002053
2054 if (!(s->s_flags & MS_RDONLY)) {
2055
2056 errval = journal_begin(&th, s, 1);
2057 if (errval) {
2058 dput(s->s_root);
2059 s->s_root = NULL;
2060 goto error;
2061 }
2062 reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s), 1);
2063
2064 set_sb_umount_state(rs, REISERFS_ERROR_FS);
2065 set_sb_fs_state(rs, 0);
2066
Jeff Mahoney098297b2014-04-23 10:00:36 -04002067 /*
2068 * Clear out s_bmap_nr if it would wrap. We can handle this
Jeff Mahoneycb680c12007-10-18 23:39:27 -07002069 * case, but older revisions can't. This will cause the
2070 * file system to fail mount on those older implementations,
Jeff Mahoney098297b2014-04-23 10:00:36 -04002071 * avoiding corruption. -jeffm
2072 */
Jeff Mahoneycb680c12007-10-18 23:39:27 -07002073 if (bmap_would_wrap(reiserfs_bmap_count(s)) &&
2074 sb_bmap_nr(rs) != 0) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04002075 reiserfs_warning(s, "super-2030", "This file system "
Jeff Mahoneycb680c12007-10-18 23:39:27 -07002076 "claims to use %u bitmap blocks in "
2077 "its super block, but requires %u. "
2078 "Clearing to zero.", sb_bmap_nr(rs),
2079 reiserfs_bmap_count(s));
2080
2081 set_sb_bmap_nr(rs, 0);
2082 }
2083
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002084 if (old_format_only(s)) {
Jeff Mahoney098297b2014-04-23 10:00:36 -04002085 /*
2086 * filesystem of format 3.5 either with standard
2087 * or non-standard journal
2088 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002089 if (convert_reiserfs(s)) {
2090 /* and -o conv is given */
2091 if (!silent)
2092 reiserfs_info(s,
2093 "converting 3.5 filesystem to the 3.6 format");
2094
2095 if (is_reiserfs_3_5(rs))
Jeff Mahoney098297b2014-04-23 10:00:36 -04002096 /*
2097 * put magic string of 3.6 format.
2098 * 2.2 will not be able to
2099 * mount this filesystem anymore
2100 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002101 memcpy(rs->s_v1.s_magic,
2102 reiserfs_3_6_magic_string,
2103 sizeof
2104 (reiserfs_3_6_magic_string));
2105
2106 set_sb_version(rs, REISERFS_VERSION_2);
2107 reiserfs_convert_objectid_map_v1(s);
Jeff Mahoneya228bf82014-04-23 10:00:42 -04002108 set_bit(REISERFS_3_6, &sbi->s_properties);
2109 clear_bit(REISERFS_3_5, &sbi->s_properties);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002110 } else if (!silent) {
2111 reiserfs_info(s, "using 3.5.x disk format\n");
2112 }
Jeff Mahoney702d21c2009-03-30 14:02:16 -04002113 } else
2114 set_sb_mnt_count(rs, sb_mnt_count(rs) + 1);
2115
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002116
Jeff Mahoney09f1b802014-04-23 10:00:39 -04002117 journal_mark_dirty(&th, SB_BUFFER_WITH_SB(s));
Jeff Mahoney58d85422014-04-23 10:00:38 -04002118 errval = journal_end(&th);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002119 if (errval) {
2120 dput(s->s_root);
2121 s->s_root = NULL;
2122 goto error;
2123 }
2124
Jeff Mahoney4c051412013-08-08 17:27:19 -04002125 reiserfs_write_unlock(s);
Al Viroedcc37a2009-05-03 06:00:05 -04002126 if ((errval = reiserfs_lookup_privroot(s)) ||
2127 (errval = reiserfs_xattr_init(s, s->s_flags))) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002128 dput(s->s_root);
2129 s->s_root = NULL;
Jeff Mahoney4c051412013-08-08 17:27:19 -04002130 goto error_unlocked;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002131 }
Jeff Mahoney4c051412013-08-08 17:27:19 -04002132 reiserfs_write_lock(s);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002133
Jeff Mahoney098297b2014-04-23 10:00:36 -04002134 /*
2135 * look for files which were to be removed in previous session
2136 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002137 finish_unfinished(s);
2138 } else {
2139 if (old_format_only(s) && !silent) {
2140 reiserfs_info(s, "using 3.5.x disk format\n");
2141 }
2142
Jeff Mahoney4c051412013-08-08 17:27:19 -04002143 reiserfs_write_unlock(s);
Al Viroedcc37a2009-05-03 06:00:05 -04002144 if ((errval = reiserfs_lookup_privroot(s)) ||
2145 (errval = reiserfs_xattr_init(s, s->s_flags))) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002146 dput(s->s_root);
2147 s->s_root = NULL;
Jeff Mahoney4c051412013-08-08 17:27:19 -04002148 goto error_unlocked;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002149 }
Jeff Mahoney4c051412013-08-08 17:27:19 -04002150 reiserfs_write_lock(s);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002151 }
Jeff Mahoney098297b2014-04-23 10:00:36 -04002152 /*
2153 * mark hash in super block: it could be unset. overwrite should be ok
2154 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002155 set_sb_hash_function_code(rs, function2code(sbi->s_hash_function));
2156
2157 handle_attrs(s);
2158
2159 reiserfs_proc_info_init(s);
2160
2161 init_waitqueue_head(&(sbi->s_wait));
2162 spin_lock_init(&sbi->bitmap_lock);
2163
Frederic Weisbecker8ebc4232009-04-07 04:19:49 +02002164 reiserfs_write_unlock(s);
2165
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002166 return (0);
2167
Jan Kara00b441972008-07-25 01:46:38 -07002168error:
Jeff Mahoneyb7b7fa42010-03-29 15:12:39 -04002169 reiserfs_write_unlock(s);
2170
Frederic Weisbeckerf32485b2012-01-10 15:11:07 -08002171error_unlocked:
2172 /* kill the commit thread, free journal ram */
2173 if (jinit_done) {
2174 reiserfs_write_lock(s);
2175 journal_release_error(NULL, s);
2176 reiserfs_write_unlock(s);
2177 }
2178
Jiri Slabyfa0c5542014-12-12 16:29:29 +01002179 if (sbi->commit_wq)
2180 destroy_workqueue(sbi->commit_wq);
2181
Artem Bityutskiy033369d2012-06-01 17:18:08 +03002182 cancel_delayed_work_sync(&REISERFS_SB(s)->old_work);
2183
Jeff Mahoney50652272006-09-30 23:28:44 -07002184 reiserfs_free_bitmap_cache(s);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002185 if (SB_BUFFER_WITH_SB(s))
2186 brelse(SB_BUFFER_WITH_SB(s));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187#ifdef CONFIG_QUOTA
Jeff Mahoney50652272006-09-30 23:28:44 -07002188 {
2189 int j;
Jan Karaaca60612014-09-10 20:57:45 +02002190 for (j = 0; j < REISERFS_MAXQUOTAS; j++)
Jan Kara00b441972008-07-25 01:46:38 -07002191 kfree(qf_names[j]);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193#endif
James Lamanna833d3042005-10-30 15:00:16 -08002194 kfree(sbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002196 s->s_fs_info = NULL;
2197 return errval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198}
2199
David Howells726c3342006-06-23 02:02:58 -07002200static int reiserfs_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201{
David Howells726c3342006-06-23 02:02:58 -07002202 struct reiserfs_super_block *rs = SB_DISK_SUPER_BLOCK(dentry->d_sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002203
2204 buf->f_namelen = (REISERFS_MAX_NAME(s->s_blocksize));
2205 buf->f_bfree = sb_free_blocks(rs);
2206 buf->f_bavail = buf->f_bfree;
2207 buf->f_blocks = sb_block_count(rs) - sb_bmap_nr(rs) - 1;
David Howells726c3342006-06-23 02:02:58 -07002208 buf->f_bsize = dentry->d_sb->s_blocksize;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002209 /* changed to accommodate gcc folks. */
2210 buf->f_type = REISERFS_SUPER_MAGIC;
Coly Li651d0622009-04-02 16:59:41 -07002211 buf->f_fsid.val[0] = (u32)crc32_le(0, rs->s_uuid, sizeof(rs->s_uuid)/2);
2212 buf->f_fsid.val[1] = (u32)crc32_le(0, rs->s_uuid + sizeof(rs->s_uuid)/2,
2213 sizeof(rs->s_uuid)/2);
2214
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002215 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216}
2217
2218#ifdef CONFIG_QUOTA
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219static int reiserfs_write_dquot(struct dquot *dquot)
2220{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002221 struct reiserfs_transaction_handle th;
2222 int ret, err;
Jeff Mahoneyd2d03952013-08-08 17:34:47 -04002223 int depth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002225 reiserfs_write_lock(dquot->dq_sb);
2226 ret =
2227 journal_begin(&th, dquot->dq_sb,
2228 REISERFS_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
2229 if (ret)
2230 goto out;
Jeff Mahoneyd2d03952013-08-08 17:34:47 -04002231 depth = reiserfs_write_unlock_nested(dquot->dq_sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002232 ret = dquot_commit(dquot);
Jeff Mahoneyd2d03952013-08-08 17:34:47 -04002233 reiserfs_write_lock_nested(dquot->dq_sb, depth);
Jeff Mahoney58d85422014-04-23 10:00:38 -04002234 err = journal_end(&th);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002235 if (!ret && err)
2236 ret = err;
Jan Kara7af11682012-11-13 17:05:14 +01002237out:
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002238 reiserfs_write_unlock(dquot->dq_sb);
2239 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240}
2241
2242static int reiserfs_acquire_dquot(struct dquot *dquot)
2243{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002244 struct reiserfs_transaction_handle th;
2245 int ret, err;
Jeff Mahoneyd2d03952013-08-08 17:34:47 -04002246 int depth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002248 reiserfs_write_lock(dquot->dq_sb);
2249 ret =
2250 journal_begin(&th, dquot->dq_sb,
2251 REISERFS_QUOTA_INIT_BLOCKS(dquot->dq_sb));
2252 if (ret)
2253 goto out;
Jeff Mahoneyd2d03952013-08-08 17:34:47 -04002254 depth = reiserfs_write_unlock_nested(dquot->dq_sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002255 ret = dquot_acquire(dquot);
Jeff Mahoneyd2d03952013-08-08 17:34:47 -04002256 reiserfs_write_lock_nested(dquot->dq_sb, depth);
Jeff Mahoney58d85422014-04-23 10:00:38 -04002257 err = journal_end(&th);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002258 if (!ret && err)
2259 ret = err;
Jan Kara7af11682012-11-13 17:05:14 +01002260out:
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002261 reiserfs_write_unlock(dquot->dq_sb);
2262 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263}
2264
2265static int reiserfs_release_dquot(struct dquot *dquot)
2266{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002267 struct reiserfs_transaction_handle th;
2268 int ret, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002270 reiserfs_write_lock(dquot->dq_sb);
2271 ret =
2272 journal_begin(&th, dquot->dq_sb,
2273 REISERFS_QUOTA_DEL_BLOCKS(dquot->dq_sb));
Jan Kara7af11682012-11-13 17:05:14 +01002274 reiserfs_write_unlock(dquot->dq_sb);
Jan Kara9c3013e2007-09-11 15:23:29 -07002275 if (ret) {
2276 /* Release dquot anyway to avoid endless cycle in dqput() */
2277 dquot_release(dquot);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002278 goto out;
Jan Kara9c3013e2007-09-11 15:23:29 -07002279 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002280 ret = dquot_release(dquot);
Jan Kara7af11682012-11-13 17:05:14 +01002281 reiserfs_write_lock(dquot->dq_sb);
Jeff Mahoney58d85422014-04-23 10:00:38 -04002282 err = journal_end(&th);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002283 if (!ret && err)
2284 ret = err;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002285 reiserfs_write_unlock(dquot->dq_sb);
Jan Kara7af11682012-11-13 17:05:14 +01002286out:
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002287 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288}
2289
2290static int reiserfs_mark_dquot_dirty(struct dquot *dquot)
2291{
Jan Kara45065672008-07-25 01:46:37 -07002292 /* Are we journaling quotas? */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002293 if (REISERFS_SB(dquot->dq_sb)->s_qf_names[USRQUOTA] ||
2294 REISERFS_SB(dquot->dq_sb)->s_qf_names[GRPQUOTA]) {
2295 dquot_mark_dquot_dirty(dquot);
2296 return reiserfs_write_dquot(dquot);
2297 } else
2298 return dquot_mark_dquot_dirty(dquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299}
2300
2301static int reiserfs_write_info(struct super_block *sb, int type)
2302{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002303 struct reiserfs_transaction_handle th;
2304 int ret, err;
Jeff Mahoneyd2d03952013-08-08 17:34:47 -04002305 int depth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002307 /* Data block + inode block */
2308 reiserfs_write_lock(sb);
2309 ret = journal_begin(&th, sb, 2);
2310 if (ret)
2311 goto out;
Jeff Mahoneyd2d03952013-08-08 17:34:47 -04002312 depth = reiserfs_write_unlock_nested(sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002313 ret = dquot_commit_info(sb, type);
Jeff Mahoneyd2d03952013-08-08 17:34:47 -04002314 reiserfs_write_lock_nested(sb, depth);
Jeff Mahoney58d85422014-04-23 10:00:38 -04002315 err = journal_end(&th);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002316 if (!ret && err)
2317 ret = err;
Jan Kara7af11682012-11-13 17:05:14 +01002318out:
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002319 reiserfs_write_unlock(sb);
2320 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321}
2322
2323/*
Christoph Hellwig84de8562005-06-23 00:09:16 -07002324 * Turn on quotas during mount time - we need to find the quota file and such...
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 */
2326static int reiserfs_quota_on_mount(struct super_block *sb, int type)
2327{
Christoph Hellwig287a8092010-05-19 07:16:45 -04002328 return dquot_quota_on_mount(sb, REISERFS_SB(sb)->s_qf_names[type],
2329 REISERFS_SB(sb)->s_jquota_fmt, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330}
2331
2332/*
2333 * Standard function to be called on quota_on
2334 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002335static int reiserfs_quota_on(struct super_block *sb, int type, int format_id,
Jan Karaf00c9e42010-09-15 17:38:58 +02002336 struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002338 int err;
Jan Karad5dee5c2008-04-28 02:16:23 -07002339 struct inode *inode;
Jan Kara5d4f7fd2008-07-25 01:46:36 -07002340 struct reiserfs_transaction_handle th;
Jan Karac3aa0772011-12-21 20:17:10 +01002341 int opt = type == USRQUOTA ? REISERFS_USRQUOTA : REISERFS_GRPQUOTA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342
Jan Karab9e06ef2012-11-13 16:34:17 +01002343 reiserfs_write_lock(sb);
2344 if (!(REISERFS_SB(sb)->s_mount_opt & (1 << opt))) {
2345 err = -EINVAL;
2346 goto out;
2347 }
Christoph Hellwig307ae182010-05-19 07:16:43 -04002348
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002349 /* Quotafile not on the same filesystem? */
Al Virod8c95842011-12-07 18:16:57 -05002350 if (path->dentry->d_sb != sb) {
Al Viro77e69da2008-08-01 04:29:18 -04002351 err = -EXDEV;
2352 goto out;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002353 }
David Howells2b0143b2015-03-17 22:25:59 +00002354 inode = d_inode(path->dentry);
Jeff Mahoney098297b2014-04-23 10:00:36 -04002355 /*
2356 * We must not pack tails for quota files on reiserfs for quota
2357 * IO to work
2358 */
Jan Karad5dee5c2008-04-28 02:16:23 -07002359 if (!(REISERFS_I(inode)->i_flags & i_nopack_mask)) {
2360 err = reiserfs_unpack(inode, NULL);
2361 if (err) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04002362 reiserfs_warning(sb, "super-6520",
2363 "Unpacking tail of quota file failed"
Jan Karad5dee5c2008-04-28 02:16:23 -07002364 " (%d). Cannot turn on quotas.", err);
Al Viro77e69da2008-08-01 04:29:18 -04002365 err = -EINVAL;
2366 goto out;
Jan Karad5dee5c2008-04-28 02:16:23 -07002367 }
2368 mark_inode_dirty(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002369 }
Jan Kara5d4f7fd2008-07-25 01:46:36 -07002370 /* Journaling quota? */
2371 if (REISERFS_SB(sb)->s_qf_names[type]) {
2372 /* Quotafile not of fs root? */
Jan Karaf00c9e42010-09-15 17:38:58 +02002373 if (path->dentry->d_parent != sb->s_root)
Jeff Mahoney45b03d52009-03-30 14:02:21 -04002374 reiserfs_warning(sb, "super-6521",
2375 "Quota file not on filesystem root. "
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002376 "Journalled quota will not work.");
Jan Kara5d4f7fd2008-07-25 01:46:36 -07002377 }
2378
2379 /*
2380 * When we journal data on quota file, we have to flush journal to see
2381 * all updates to the file when we bypass pagecache...
2382 */
2383 if (reiserfs_file_data_log(inode)) {
2384 /* Just start temporary transaction and finish it */
2385 err = journal_begin(&th, sb, 1);
2386 if (err)
Al Viro77e69da2008-08-01 04:29:18 -04002387 goto out;
Jeff Mahoney58d85422014-04-23 10:00:38 -04002388 err = journal_end_sync(&th);
Jan Kara5d4f7fd2008-07-25 01:46:36 -07002389 if (err)
Al Viro77e69da2008-08-01 04:29:18 -04002390 goto out;
Jan Kara5d4f7fd2008-07-25 01:46:36 -07002391 }
Jan Karab9e06ef2012-11-13 16:34:17 +01002392 reiserfs_write_unlock(sb);
2393 return dquot_quota_on(sb, type, format_id, path);
Al Viro77e69da2008-08-01 04:29:18 -04002394out:
Jan Karab9e06ef2012-11-13 16:34:17 +01002395 reiserfs_write_unlock(sb);
Al Viro77e69da2008-08-01 04:29:18 -04002396 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397}
2398
Jeff Mahoney098297b2014-04-23 10:00:36 -04002399/*
2400 * Read data from quotafile - avoid pagecache and such because we cannot afford
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 * acquiring the locks... As quota files are never truncated and quota code
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002402 * itself serializes the operations (and no one else should touch the files)
Jeff Mahoney098297b2014-04-23 10:00:36 -04002403 * we don't have to be afraid of races
2404 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405static ssize_t reiserfs_quota_read(struct super_block *sb, int type, char *data,
2406 size_t len, loff_t off)
2407{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002408 struct inode *inode = sb_dqopt(sb)->files[type];
2409 unsigned long blk = off >> sb->s_blocksize_bits;
2410 int err = 0, offset = off & (sb->s_blocksize - 1), tocopy;
2411 size_t toread;
2412 struct buffer_head tmp_bh, *bh;
2413 loff_t i_size = i_size_read(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002415 if (off > i_size)
2416 return 0;
2417 if (off + len > i_size)
2418 len = i_size - off;
2419 toread = len;
2420 while (toread > 0) {
2421 tocopy =
2422 sb->s_blocksize - offset <
2423 toread ? sb->s_blocksize - offset : toread;
2424 tmp_bh.b_state = 0;
Jeff Mahoney098297b2014-04-23 10:00:36 -04002425 /*
2426 * Quota files are without tails so we can safely
2427 * use this function
2428 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002429 reiserfs_write_lock(sb);
2430 err = reiserfs_get_block(inode, blk, &tmp_bh, 0);
2431 reiserfs_write_unlock(sb);
2432 if (err)
2433 return err;
2434 if (!buffer_mapped(&tmp_bh)) /* A hole? */
2435 memset(data, 0, tocopy);
2436 else {
2437 bh = sb_bread(sb, tmp_bh.b_blocknr);
2438 if (!bh)
2439 return -EIO;
2440 memcpy(data, bh->b_data + offset, tocopy);
2441 brelse(bh);
2442 }
2443 offset = 0;
2444 toread -= tocopy;
2445 data += tocopy;
2446 blk++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002448 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449}
2450
Jeff Mahoney098297b2014-04-23 10:00:36 -04002451/*
2452 * Write to quotafile (we know the transaction is already started and has
2453 * enough credits)
2454 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455static ssize_t reiserfs_quota_write(struct super_block *sb, int type,
2456 const char *data, size_t len, loff_t off)
2457{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002458 struct inode *inode = sb_dqopt(sb)->files[type];
2459 unsigned long blk = off >> sb->s_blocksize_bits;
2460 int err = 0, offset = off & (sb->s_blocksize - 1), tocopy;
2461 int journal_quota = REISERFS_SB(sb)->s_qf_names[type] != NULL;
2462 size_t towrite = len;
2463 struct buffer_head tmp_bh, *bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464
Jan Kara9c3013e2007-09-11 15:23:29 -07002465 if (!current->journal_info) {
Fabian Frederick53872ed2014-08-08 14:21:10 -07002466 printk(KERN_WARNING "reiserfs: Quota write (off=%llu, len=%llu) cancelled because transaction is not started.\n",
Jan Kara9c3013e2007-09-11 15:23:29 -07002467 (unsigned long long)off, (unsigned long long)len);
2468 return -EIO;
2469 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002470 while (towrite > 0) {
2471 tocopy = sb->s_blocksize - offset < towrite ?
2472 sb->s_blocksize - offset : towrite;
2473 tmp_bh.b_state = 0;
Jan Kara361d94a2012-11-13 18:25:38 +01002474 reiserfs_write_lock(sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002475 err = reiserfs_get_block(inode, blk, &tmp_bh, GET_BLOCK_CREATE);
Jan Kara361d94a2012-11-13 18:25:38 +01002476 reiserfs_write_unlock(sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002477 if (err)
2478 goto out;
2479 if (offset || tocopy != sb->s_blocksize)
2480 bh = sb_bread(sb, tmp_bh.b_blocknr);
2481 else
2482 bh = sb_getblk(sb, tmp_bh.b_blocknr);
2483 if (!bh) {
2484 err = -EIO;
2485 goto out;
2486 }
2487 lock_buffer(bh);
2488 memcpy(bh->b_data + offset, data, tocopy);
2489 flush_dcache_page(bh->b_page);
2490 set_buffer_uptodate(bh);
2491 unlock_buffer(bh);
Jan Kara361d94a2012-11-13 18:25:38 +01002492 reiserfs_write_lock(sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002493 reiserfs_prepare_for_journal(sb, bh, 1);
Jeff Mahoney09f1b802014-04-23 10:00:39 -04002494 journal_mark_dirty(current->journal_info, bh);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002495 if (!journal_quota)
2496 reiserfs_add_ordered_list(inode, bh);
Jan Kara361d94a2012-11-13 18:25:38 +01002497 reiserfs_write_unlock(sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002498 brelse(bh);
2499 offset = 0;
2500 towrite -= tocopy;
2501 data += tocopy;
2502 blk++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 }
Jan Kara9c3013e2007-09-11 15:23:29 -07002504out:
Jan Kara67f16482012-04-25 21:28:12 +02002505 if (len == towrite)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002506 return err;
2507 if (inode->i_size < off + len - towrite)
2508 i_size_write(inode, off + len - towrite);
2509 inode->i_version++;
2510 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
2511 mark_inode_dirty(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002512 return len - towrite;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513}
2514
2515#endif
2516
Al Viro152a0832010-07-25 00:46:55 +04002517static struct dentry *get_super_block(struct file_system_type *fs_type,
David Howells454e2392006-06-23 02:02:57 -07002518 int flags, const char *dev_name,
Al Viro152a0832010-07-25 00:46:55 +04002519 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520{
Al Viro152a0832010-07-25 00:46:55 +04002521 return mount_bdev(fs_type, flags, dev_name, data, reiserfs_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522}
2523
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002524static int __init init_reiserfs_fs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525{
2526 int ret;
2527
Jeff Mahoney797d9012014-04-23 10:00:34 -04002528 ret = init_inodecache();
2529 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002532 reiserfs_proc_info_global_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002534 ret = register_filesystem(&reiserfs_fs_type);
Jeff Mahoney797d9012014-04-23 10:00:34 -04002535 if (ret)
2536 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537
Jeff Mahoney797d9012014-04-23 10:00:34 -04002538 return 0;
2539out:
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002540 reiserfs_proc_info_global_done();
2541 destroy_inodecache();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542
2543 return ret;
2544}
2545
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002546static void __exit exit_reiserfs_fs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002548 reiserfs_proc_info_global_done();
2549 unregister_filesystem(&reiserfs_fs_type);
2550 destroy_inodecache();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551}
2552
2553struct file_system_type reiserfs_fs_type = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002554 .owner = THIS_MODULE,
2555 .name = "reiserfs",
Al Viro152a0832010-07-25 00:46:55 +04002556 .mount = get_super_block,
David Howellsedc666e2006-10-11 01:22:14 -07002557 .kill_sb = reiserfs_kill_sb,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002558 .fs_flags = FS_REQUIRES_DEV,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559};
Eric W. Biederman7f78e032013-03-02 19:39:14 -08002560MODULE_ALIAS_FS("reiserfs");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002562MODULE_DESCRIPTION("ReiserFS journaled filesystem");
2563MODULE_AUTHOR("Hans Reiser <reiser@namesys.com>");
2564MODULE_LICENSE("GPL");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002566module_init(init_reiserfs_fs);
2567module_exit(exit_reiserfs_fs);