blob: 5c58b419f4ee1adbe1fcbc2ebf57aa6bc1031ae7 [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>
15#include <linux/vmalloc.h>
16#include <linux/time.h>
17#include <asm/uaccess.h>
18#include <linux/reiserfs_fs.h>
19#include <linux/reiserfs_acl.h>
20#include <linux/reiserfs_xattr.h>
21#include <linux/smp_lock.h>
22#include <linux/init.h>
23#include <linux/blkdev.h>
24#include <linux/buffer_head.h>
25#include <linux/vfs.h>
Kirill Korotaev6b3286e2006-12-08 02:37:56 -080026#include <linux/mnt_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/mount.h>
28#include <linux/namei.h>
29#include <linux/quotaops.h>
30
31struct file_system_type reiserfs_fs_type;
32
33static const char reiserfs_3_5_magic_string[] = REISERFS_SUPER_MAGIC_STRING;
34static const char reiserfs_3_6_magic_string[] = REISER2FS_SUPER_MAGIC_STRING;
35static const char reiserfs_jr_magic_string[] = REISER2FS_JR_SUPER_MAGIC_STRING;
36
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070037int is_reiserfs_3_5(struct reiserfs_super_block *rs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070038{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070039 return !strncmp(rs->s_v1.s_magic, reiserfs_3_5_magic_string,
40 strlen(reiserfs_3_5_magic_string));
Linus Torvalds1da177e2005-04-16 15:20:36 -070041}
42
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070043int is_reiserfs_3_6(struct reiserfs_super_block *rs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070045 return !strncmp(rs->s_v1.s_magic, reiserfs_3_6_magic_string,
46 strlen(reiserfs_3_6_magic_string));
Linus Torvalds1da177e2005-04-16 15:20:36 -070047}
48
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070049int is_reiserfs_jr(struct reiserfs_super_block *rs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070051 return !strncmp(rs->s_v1.s_magic, reiserfs_jr_magic_string,
52 strlen(reiserfs_jr_magic_string));
Linus Torvalds1da177e2005-04-16 15:20:36 -070053}
54
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070055static int is_any_reiserfs_magic_string(struct reiserfs_super_block *rs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070057 return (is_reiserfs_3_5(rs) || is_reiserfs_3_6(rs) ||
58 is_reiserfs_jr(rs));
Linus Torvalds1da177e2005-04-16 15:20:36 -070059}
60
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070061static int reiserfs_remount(struct super_block *s, int *flags, char *data);
David Howells726c3342006-06-23 02:02:58 -070062static int reiserfs_statfs(struct dentry *dentry, struct kstatfs *buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070064static int reiserfs_sync_fs(struct super_block *s, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070066 if (!(s->s_flags & MS_RDONLY)) {
67 struct reiserfs_transaction_handle th;
68 reiserfs_write_lock(s);
69 if (!journal_begin(&th, s, 1))
70 if (!journal_end_sync(&th, s, 1))
71 reiserfs_flush_old_commits(s);
72 s->s_dirt = 0; /* Even if it's not true.
73 * We'll loop forever in sync_supers otherwise */
74 reiserfs_write_unlock(s);
75 } else {
76 s->s_dirt = 0;
77 }
78 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079}
80
81static void reiserfs_write_super(struct super_block *s)
82{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070083 reiserfs_sync_fs(s, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084}
85
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070086static void reiserfs_write_super_lockfs(struct super_block *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070088 struct reiserfs_transaction_handle th;
89 reiserfs_write_lock(s);
90 if (!(s->s_flags & MS_RDONLY)) {
91 int err = journal_begin(&th, s, 1);
92 if (err) {
93 reiserfs_block_writes(&th);
94 } else {
95 reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s),
96 1);
97 journal_mark_dirty(&th, s, SB_BUFFER_WITH_SB(s));
98 reiserfs_block_writes(&th);
99 journal_end_sync(&th, s, 1);
100 }
101 }
102 s->s_dirt = 0;
103 reiserfs_write_unlock(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
105
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700106static void reiserfs_unlockfs(struct super_block *s)
107{
108 reiserfs_allow_writes(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109}
110
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700111extern const struct in_core_key MAX_IN_CORE_KEY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113/* this is used to delete "save link" when there are no items of a
114 file it points to. It can either happen if unlink is completed but
115 "save unlink" removal, or if file has both unlink and truncate
116 pending and as unlink completes first (because key of "save link"
117 protecting unlink is bigger that a key lf "save link" which
118 protects truncate), so there left no items to make truncate
119 completion on */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700120static int remove_save_link_only(struct super_block *s,
121 struct reiserfs_key *key, int oid_free)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700123 struct reiserfs_transaction_handle th;
124 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700126 /* we are going to do one balancing */
127 err = journal_begin(&th, s, JOURNAL_PER_BALANCE_CNT);
128 if (err)
129 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700131 reiserfs_delete_solid_item(&th, NULL, key);
132 if (oid_free)
133 /* removals are protected by direct items */
134 reiserfs_release_objectid(&th, le32_to_cpu(key->k_objectid));
135
136 return journal_end(&th, s, JOURNAL_PER_BALANCE_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139#ifdef CONFIG_QUOTA
140static int reiserfs_quota_on_mount(struct super_block *, int);
141#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143/* look for uncompleted unlinks and truncates and complete them */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700144static int finish_unfinished(struct super_block *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700146 INITIALIZE_PATH(path);
147 struct cpu_key max_cpu_key, obj_key;
148 struct reiserfs_key save_link_key;
149 int retval = 0;
150 struct item_head *ih;
151 struct buffer_head *bh;
152 int item_pos;
153 char *item;
154 int done;
155 struct inode *inode;
156 int truncate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157#ifdef CONFIG_QUOTA
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700158 int i;
159 int ms_active_set;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700161
162 /* compose key to look for "save" links */
163 max_cpu_key.version = KEY_FORMAT_3_5;
164 max_cpu_key.on_disk_key.k_dir_id = ~0U;
165 max_cpu_key.on_disk_key.k_objectid = ~0U;
166 set_cpu_key_k_offset(&max_cpu_key, ~0U);
167 max_cpu_key.key_length = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
169#ifdef CONFIG_QUOTA
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700170 /* Needed for iput() to work correctly and not trash data */
171 if (s->s_flags & MS_ACTIVE) {
172 ms_active_set = 0;
173 } else {
174 ms_active_set = 1;
175 s->s_flags |= MS_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700177 /* Turn on quotas so that they are updated correctly */
178 for (i = 0; i < MAXQUOTAS; i++) {
179 if (REISERFS_SB(s)->s_qf_names[i]) {
180 int ret = reiserfs_quota_on_mount(s, i);
181 if (ret < 0)
182 reiserfs_warning(s,
183 "reiserfs: cannot turn on journalled quota: error %d",
184 ret);
185 }
186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700189 done = 0;
190 REISERFS_SB(s)->s_is_unlinked_ok = 1;
191 while (!retval) {
192 retval = search_item(s, &max_cpu_key, &path);
193 if (retval != ITEM_NOT_FOUND) {
194 reiserfs_warning(s,
195 "vs-2140: finish_unfinished: search_by_key returned %d",
196 retval);
197 break;
198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700200 bh = get_last_bh(&path);
201 item_pos = get_item_pos(&path);
202 if (item_pos != B_NR_ITEMS(bh)) {
203 reiserfs_warning(s,
204 "vs-2060: finish_unfinished: wrong position found");
205 break;
206 }
207 item_pos--;
208 ih = B_N_PITEM_HEAD(bh, item_pos);
209
210 if (le32_to_cpu(ih->ih_key.k_dir_id) != MAX_KEY_OBJECTID)
211 /* there are no "save" links anymore */
212 break;
213
214 save_link_key = ih->ih_key;
215 if (is_indirect_le_ih(ih))
216 truncate = 1;
217 else
218 truncate = 0;
219
220 /* reiserfs_iget needs k_dirid and k_objectid only */
221 item = B_I_PITEM(bh, ih);
222 obj_key.on_disk_key.k_dir_id = le32_to_cpu(*(__le32 *) item);
223 obj_key.on_disk_key.k_objectid =
224 le32_to_cpu(ih->ih_key.k_objectid);
225 obj_key.on_disk_key.k_offset = 0;
226 obj_key.on_disk_key.k_type = 0;
227
228 pathrelse(&path);
229
230 inode = reiserfs_iget(s, &obj_key);
231 if (!inode) {
232 /* the unlink almost completed, it just did not manage to remove
233 "save" link and release objectid */
234 reiserfs_warning(s,
235 "vs-2180: finish_unfinished: iget failed for %K",
236 &obj_key);
237 retval = remove_save_link_only(s, &save_link_key, 1);
238 continue;
239 }
240
241 if (!truncate && inode->i_nlink) {
242 /* file is not unlinked */
243 reiserfs_warning(s,
244 "vs-2185: finish_unfinished: file %K is not unlinked",
245 &obj_key);
246 retval = remove_save_link_only(s, &save_link_key, 0);
247 continue;
248 }
249 DQUOT_INIT(inode);
250
251 if (truncate && S_ISDIR(inode->i_mode)) {
252 /* We got a truncate request for a dir which is impossible.
253 The only imaginable way is to execute unfinished truncate request
254 then boot into old kernel, remove the file and create dir with
255 the same key. */
256 reiserfs_warning(s,
257 "green-2101: impossible truncate on a directory %k. Please report",
258 INODE_PKEY(inode));
259 retval = remove_save_link_only(s, &save_link_key, 0);
260 truncate = 0;
261 iput(inode);
262 continue;
263 }
264
265 if (truncate) {
266 REISERFS_I(inode)->i_flags |=
267 i_link_saved_truncate_mask;
268 /* not completed truncate found. New size was committed together
269 with "save" link */
270 reiserfs_info(s, "Truncating %k to %Ld ..",
271 INODE_PKEY(inode), inode->i_size);
272 reiserfs_truncate_file(inode,
273 0
274 /*don't update modification time */
275 );
276 retval = remove_save_link(inode, truncate);
277 } else {
278 REISERFS_I(inode)->i_flags |= i_link_saved_unlink_mask;
279 /* not completed unlink (rmdir) found */
280 reiserfs_info(s, "Removing %k..", INODE_PKEY(inode));
281 /* removal gets completed in iput */
282 retval = 0;
283 }
284
285 iput(inode);
286 printk("done\n");
287 done++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700289 REISERFS_SB(s)->s_is_unlinked_ok = 0;
290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291#ifdef CONFIG_QUOTA
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700292 /* Turn quotas off */
293 for (i = 0; i < MAXQUOTAS; i++) {
294 if (sb_dqopt(s)->files[i])
295 vfs_quota_off_mount(s, i);
296 }
297 if (ms_active_set)
298 /* Restore the flag back */
299 s->s_flags &= ~MS_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700301 pathrelse(&path);
302 if (done)
303 reiserfs_info(s, "There were %d uncompleted unlinks/truncates. "
304 "Completed\n", done);
305 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306}
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308/* to protect file being unlinked from getting lost we "safe" link files
309 being unlinked. This link will be deleted in the same transaction with last
310 item of file. mounting the filesytem we scan all these links and remove
311 files which almost got lost */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700312void add_save_link(struct reiserfs_transaction_handle *th,
313 struct inode *inode, int truncate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700315 INITIALIZE_PATH(path);
316 int retval;
317 struct cpu_key key;
318 struct item_head ih;
319 __le32 link;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700321 BUG_ON(!th->t_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700323 /* file can only get one "save link" of each kind */
324 RFALSE(truncate &&
325 (REISERFS_I(inode)->i_flags & i_link_saved_truncate_mask),
326 "saved link already exists for truncated inode %lx",
327 (long)inode->i_ino);
328 RFALSE(!truncate &&
329 (REISERFS_I(inode)->i_flags & i_link_saved_unlink_mask),
330 "saved link already exists for unlinked inode %lx",
331 (long)inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700333 /* setup key of "save" link */
334 key.version = KEY_FORMAT_3_5;
335 key.on_disk_key.k_dir_id = MAX_KEY_OBJECTID;
336 key.on_disk_key.k_objectid = inode->i_ino;
337 if (!truncate) {
338 /* unlink, rmdir, rename */
339 set_cpu_key_k_offset(&key, 1 + inode->i_sb->s_blocksize);
340 set_cpu_key_k_type(&key, TYPE_DIRECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700342 /* item head of "safe" link */
343 make_le_item_head(&ih, &key, key.version,
344 1 + inode->i_sb->s_blocksize, TYPE_DIRECT,
345 4 /*length */ , 0xffff /*free space */ );
346 } else {
347 /* truncate */
348 if (S_ISDIR(inode->i_mode))
349 reiserfs_warning(inode->i_sb,
350 "green-2102: Adding a truncate savelink for a directory %k! Please report",
351 INODE_PKEY(inode));
352 set_cpu_key_k_offset(&key, 1);
353 set_cpu_key_k_type(&key, TYPE_INDIRECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700355 /* item head of "safe" link */
356 make_le_item_head(&ih, &key, key.version, 1, TYPE_INDIRECT,
357 4 /*length */ , 0 /*free space */ );
358 }
359 key.key_length = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700361 /* look for its place in the tree */
362 retval = search_item(inode->i_sb, &key, &path);
363 if (retval != ITEM_NOT_FOUND) {
364 if (retval != -ENOSPC)
365 reiserfs_warning(inode->i_sb, "vs-2100: add_save_link:"
366 "search_by_key (%K) returned %d", &key,
367 retval);
368 pathrelse(&path);
369 return;
370 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700372 /* body of "save" link */
373 link = INODE_PKEY(inode)->k_dir_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700375 /* put "save" link inot tree, don't charge quota to anyone */
376 retval =
377 reiserfs_insert_item(th, &path, &key, &ih, NULL, (char *)&link);
378 if (retval) {
379 if (retval != -ENOSPC)
380 reiserfs_warning(inode->i_sb,
381 "vs-2120: add_save_link: insert_item returned %d",
382 retval);
383 } else {
384 if (truncate)
385 REISERFS_I(inode)->i_flags |=
386 i_link_saved_truncate_mask;
387 else
388 REISERFS_I(inode)->i_flags |= i_link_saved_unlink_mask;
389 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390}
391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392/* this opens transaction unlike add_save_link */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700393int remove_save_link(struct inode *inode, int truncate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700395 struct reiserfs_transaction_handle th;
396 struct reiserfs_key key;
397 int err;
398
399 /* we are going to do one balancing only */
400 err = journal_begin(&th, inode->i_sb, JOURNAL_PER_BALANCE_CNT);
401 if (err)
402 return err;
403
404 /* setup key of "save" link */
405 key.k_dir_id = cpu_to_le32(MAX_KEY_OBJECTID);
406 key.k_objectid = INODE_PKEY(inode)->k_objectid;
407 if (!truncate) {
408 /* unlink, rmdir, rename */
409 set_le_key_k_offset(KEY_FORMAT_3_5, &key,
410 1 + inode->i_sb->s_blocksize);
411 set_le_key_k_type(KEY_FORMAT_3_5, &key, TYPE_DIRECT);
412 } else {
413 /* truncate */
414 set_le_key_k_offset(KEY_FORMAT_3_5, &key, 1);
415 set_le_key_k_type(KEY_FORMAT_3_5, &key, TYPE_INDIRECT);
416 }
417
418 if ((truncate &&
419 (REISERFS_I(inode)->i_flags & i_link_saved_truncate_mask)) ||
420 (!truncate &&
421 (REISERFS_I(inode)->i_flags & i_link_saved_unlink_mask)))
422 /* don't take quota bytes from anywhere */
423 reiserfs_delete_solid_item(&th, NULL, &key);
424 if (!truncate) {
425 reiserfs_release_objectid(&th, inode->i_ino);
426 REISERFS_I(inode)->i_flags &= ~i_link_saved_unlink_mask;
427 } else
428 REISERFS_I(inode)->i_flags &= ~i_link_saved_truncate_mask;
429
430 return journal_end(&th, inode->i_sb, JOURNAL_PER_BALANCE_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431}
432
David Howellsedc666e2006-10-11 01:22:14 -0700433static void reiserfs_kill_sb(struct super_block *s)
434{
435 if (REISERFS_SB(s)) {
Alexey Dobriyanfe08a9d2007-05-08 00:24:55 -0700436#ifdef CONFIG_REISERFS_FS_XATTR
David Howellsedc666e2006-10-11 01:22:14 -0700437 if (REISERFS_SB(s)->xattr_root) {
438 d_invalidate(REISERFS_SB(s)->xattr_root);
439 dput(REISERFS_SB(s)->xattr_root);
440 REISERFS_SB(s)->xattr_root = NULL;
441 }
Alexey Dobriyanfe08a9d2007-05-08 00:24:55 -0700442#endif
David Howellsedc666e2006-10-11 01:22:14 -0700443 if (REISERFS_SB(s)->priv_root) {
444 d_invalidate(REISERFS_SB(s)->priv_root);
445 dput(REISERFS_SB(s)->priv_root);
446 REISERFS_SB(s)->priv_root = NULL;
447 }
448 }
449
450 kill_block_super(s);
451}
452
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700453static void reiserfs_put_super(struct super_block *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700455 struct reiserfs_transaction_handle th;
456 th.t_trans_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700458 /* change file system state to current state if it was mounted with read-write permissions */
459 if (!(s->s_flags & MS_RDONLY)) {
460 if (!journal_begin(&th, s, 10)) {
461 reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s),
462 1);
463 set_sb_umount_state(SB_DISK_SUPER_BLOCK(s),
464 REISERFS_SB(s)->s_mount_state);
465 journal_mark_dirty(&th, s, SB_BUFFER_WITH_SB(s));
466 }
467 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700469 /* note, journal_release checks for readonly mount, and can decide not
470 ** to do a journal_end
471 */
472 journal_release(&th, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Jeff Mahoney50652272006-09-30 23:28:44 -0700474 reiserfs_free_bitmap_cache(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700476 brelse(SB_BUFFER_WITH_SB(s));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700478 print_statistics(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700480 if (REISERFS_SB(s)->reserved_blocks != 0) {
481 reiserfs_warning(s,
482 "green-2005: reiserfs_put_super: reserved blocks left %d",
483 REISERFS_SB(s)->reserved_blocks);
484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700486 reiserfs_proc_info_done(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700488 kfree(s->s_fs_info);
489 s->s_fs_info = NULL;
490
491 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492}
493
Christoph Lametere18b8902006-12-06 20:33:20 -0800494static struct kmem_cache *reiserfs_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
496static struct inode *reiserfs_alloc_inode(struct super_block *sb)
497{
498 struct reiserfs_inode_info *ei;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700499 ei = (struct reiserfs_inode_info *)
Christoph Lametere94b1762006-12-06 20:33:17 -0800500 kmem_cache_alloc(reiserfs_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 if (!ei)
502 return NULL;
503 return &ei->vfs_inode;
504}
505
506static void reiserfs_destroy_inode(struct inode *inode)
507{
508 kmem_cache_free(reiserfs_inode_cachep, REISERFS_I(inode));
509}
510
Christoph Lametere18b8902006-12-06 20:33:20 -0800511static void init_once(void *foo, struct kmem_cache * cachep, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700513 struct reiserfs_inode_info *ei = (struct reiserfs_inode_info *)foo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Christoph Lameter50953fe2007-05-06 14:50:16 -0700515 if (flags & SLAB_CTOR_CONSTRUCTOR) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700516 INIT_LIST_HEAD(&ei->i_prealloc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 inode_init_once(&ei->vfs_inode);
Alexey Dobriyancfe14672006-09-29 02:00:00 -0700518#ifdef CONFIG_REISERFS_FS_POSIX_ACL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 ei->i_acl_access = NULL;
520 ei->i_acl_default = NULL;
Alexey Dobriyancfe14672006-09-29 02:00:00 -0700521#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 }
523}
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700524
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525static int init_inodecache(void)
526{
527 reiserfs_inode_cachep = kmem_cache_create("reiser_inode_cache",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700528 sizeof(struct
529 reiserfs_inode_info),
Paul Jacksonfffb60f2006-03-24 03:16:06 -0800530 0, (SLAB_RECLAIM_ACCOUNT|
531 SLAB_MEM_SPREAD),
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700532 init_once, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 if (reiserfs_inode_cachep == NULL)
534 return -ENOMEM;
535 return 0;
536}
537
538static void destroy_inodecache(void)
539{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -0700540 kmem_cache_destroy(reiserfs_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541}
542
543/* we don't mark inodes dirty, we just log them */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700544static void reiserfs_dirty_inode(struct inode *inode)
545{
546 struct reiserfs_transaction_handle th;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700548 int err = 0;
549 if (inode->i_sb->s_flags & MS_RDONLY) {
550 reiserfs_warning(inode->i_sb,
551 "clm-6006: writing inode %lu on readonly FS",
552 inode->i_ino);
553 return;
554 }
555 reiserfs_write_lock(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700557 /* this is really only used for atime updates, so they don't have
558 ** to be included in O_SYNC or fsync
559 */
560 err = journal_begin(&th, inode->i_sb, 1);
561 if (err) {
562 reiserfs_write_unlock(inode->i_sb);
563 return;
564 }
565 reiserfs_update_sd(&th, inode);
566 journal_end(&th, inode->i_sb, 1);
567 reiserfs_write_unlock(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568}
569
Alexey Dobriyancfe14672006-09-29 02:00:00 -0700570#ifdef CONFIG_REISERFS_FS_POSIX_ACL
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700571static void reiserfs_clear_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700573 struct posix_acl *acl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700575 acl = REISERFS_I(inode)->i_acl_access;
576 if (acl && !IS_ERR(acl))
577 posix_acl_release(acl);
578 REISERFS_I(inode)->i_acl_access = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700580 acl = REISERFS_I(inode)->i_acl_default;
581 if (acl && !IS_ERR(acl))
582 posix_acl_release(acl);
583 REISERFS_I(inode)->i_acl_default = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584}
Alexey Dobriyancfe14672006-09-29 02:00:00 -0700585#else
586#define reiserfs_clear_inode NULL
587#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
589#ifdef CONFIG_QUOTA
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700590static ssize_t reiserfs_quota_write(struct super_block *, int, const char *,
591 size_t, loff_t);
592static ssize_t reiserfs_quota_read(struct super_block *, int, char *, size_t,
593 loff_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594#endif
595
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800596static const struct super_operations reiserfs_sops = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700597 .alloc_inode = reiserfs_alloc_inode,
598 .destroy_inode = reiserfs_destroy_inode,
599 .write_inode = reiserfs_write_inode,
600 .dirty_inode = reiserfs_dirty_inode,
601 .delete_inode = reiserfs_delete_inode,
602 .clear_inode = reiserfs_clear_inode,
603 .put_super = reiserfs_put_super,
604 .write_super = reiserfs_write_super,
605 .sync_fs = reiserfs_sync_fs,
606 .write_super_lockfs = reiserfs_write_super_lockfs,
607 .unlockfs = reiserfs_unlockfs,
608 .statfs = reiserfs_statfs,
609 .remount_fs = reiserfs_remount,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610#ifdef CONFIG_QUOTA
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700611 .quota_read = reiserfs_quota_read,
612 .quota_write = reiserfs_quota_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613#endif
614};
615
616#ifdef CONFIG_QUOTA
617#define QTYPE2NAME(t) ((t)==USRQUOTA?"user":"group")
618
619static int reiserfs_dquot_initialize(struct inode *, int);
620static int reiserfs_dquot_drop(struct inode *);
621static int reiserfs_write_dquot(struct dquot *);
622static int reiserfs_acquire_dquot(struct dquot *);
623static int reiserfs_release_dquot(struct dquot *);
624static int reiserfs_mark_dquot_dirty(struct dquot *);
625static int reiserfs_write_info(struct super_block *, int);
626static int reiserfs_quota_on(struct super_block *, int, int, char *);
627
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700628static struct dquot_operations reiserfs_quota_operations = {
629 .initialize = reiserfs_dquot_initialize,
630 .drop = reiserfs_dquot_drop,
631 .alloc_space = dquot_alloc_space,
632 .alloc_inode = dquot_alloc_inode,
633 .free_space = dquot_free_space,
634 .free_inode = dquot_free_inode,
635 .transfer = dquot_transfer,
636 .write_dquot = reiserfs_write_dquot,
637 .acquire_dquot = reiserfs_acquire_dquot,
638 .release_dquot = reiserfs_release_dquot,
639 .mark_dirty = reiserfs_mark_dquot_dirty,
640 .write_info = reiserfs_write_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641};
642
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700643static struct quotactl_ops reiserfs_qctl_operations = {
644 .quota_on = reiserfs_quota_on,
645 .quota_off = vfs_quota_off,
646 .quota_sync = vfs_quota_sync,
647 .get_info = vfs_get_dqinfo,
648 .set_info = vfs_set_dqinfo,
649 .get_dqblk = vfs_get_dqblk,
650 .set_dqblk = vfs_set_dqblk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651};
652#endif
653
654static struct export_operations reiserfs_export_ops = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700655 .encode_fh = reiserfs_encode_fh,
656 .decode_fh = reiserfs_decode_fh,
657 .get_parent = reiserfs_get_parent,
658 .get_dentry = reiserfs_get_dentry,
659};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
661/* this struct is used in reiserfs_getopt () for containing the value for those
662 mount options that have values rather than being toggles. */
663typedef struct {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700664 char *value;
665 int setmask; /* bitmask which is to set on mount_options bitmask when this
666 value is found, 0 is no bits are to be changed. */
667 int clrmask; /* bitmask which is to clear on mount_options bitmask when this
668 value is found, 0 is no bits are to be changed. This is
669 applied BEFORE setmask */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670} arg_desc_t;
671
672/* Set this bit in arg_required to allow empty arguments */
673#define REISERFS_OPT_ALLOWEMPTY 31
674
675/* this struct is used in reiserfs_getopt() for describing the set of reiserfs
676 mount options */
677typedef struct {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700678 char *option_name;
679 int arg_required; /* 0 if argument is not required, not 0 otherwise */
680 const arg_desc_t *values; /* list of values accepted by an option */
681 int setmask; /* bitmask which is to set on mount_options bitmask when this
682 value is found, 0 is no bits are to be changed. */
683 int clrmask; /* bitmask which is to clear on mount_options bitmask when this
684 value is found, 0 is no bits are to be changed. This is
685 applied BEFORE setmask */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686} opt_desc_t;
687
688/* possible values for -o data= */
689static const arg_desc_t logging_mode[] = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700690 {"ordered", 1 << REISERFS_DATA_ORDERED,
691 (1 << REISERFS_DATA_LOG | 1 << REISERFS_DATA_WRITEBACK)},
692 {"journal", 1 << REISERFS_DATA_LOG,
693 (1 << REISERFS_DATA_ORDERED | 1 << REISERFS_DATA_WRITEBACK)},
694 {"writeback", 1 << REISERFS_DATA_WRITEBACK,
695 (1 << REISERFS_DATA_ORDERED | 1 << REISERFS_DATA_LOG)},
Vladimir V. Savelievcd02b962006-03-25 03:07:15 -0800696 {.value = NULL}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697};
698
699/* possible values for -o barrier= */
700static const arg_desc_t barrier_mode[] = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700701 {"none", 1 << REISERFS_BARRIER_NONE, 1 << REISERFS_BARRIER_FLUSH},
702 {"flush", 1 << REISERFS_BARRIER_FLUSH, 1 << REISERFS_BARRIER_NONE},
Vladimir V. Savelievcd02b962006-03-25 03:07:15 -0800703 {.value = NULL}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704};
705
706/* possible values for "-o block-allocator=" and bits which are to be set in
707 s_mount_opt of reiserfs specific part of in-core super block */
708static const arg_desc_t balloc[] = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700709 {"noborder", 1 << REISERFS_NO_BORDER, 0},
710 {"border", 0, 1 << REISERFS_NO_BORDER},
711 {"no_unhashed_relocation", 1 << REISERFS_NO_UNHASHED_RELOCATION, 0},
712 {"hashed_relocation", 1 << REISERFS_HASHED_RELOCATION, 0},
713 {"test4", 1 << REISERFS_TEST4, 0},
714 {"notest4", 0, 1 << REISERFS_TEST4},
715 {NULL, 0, 0}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716};
717
718static const arg_desc_t tails[] = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700719 {"on", 1 << REISERFS_LARGETAIL, 1 << REISERFS_SMALLTAIL},
720 {"off", 0, (1 << REISERFS_LARGETAIL) | (1 << REISERFS_SMALLTAIL)},
721 {"small", 1 << REISERFS_SMALLTAIL, 1 << REISERFS_LARGETAIL},
722 {NULL, 0, 0}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723};
724
725static const arg_desc_t error_actions[] = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700726 {"panic", 1 << REISERFS_ERROR_PANIC,
727 (1 << REISERFS_ERROR_RO | 1 << REISERFS_ERROR_CONTINUE)},
728 {"ro-remount", 1 << REISERFS_ERROR_RO,
729 (1 << REISERFS_ERROR_PANIC | 1 << REISERFS_ERROR_CONTINUE)},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730#ifdef REISERFS_JOURNAL_ERROR_ALLOWS_NO_LOG
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700731 {"continue", 1 << REISERFS_ERROR_CONTINUE,
732 (1 << REISERFS_ERROR_PANIC | 1 << REISERFS_ERROR_RO)},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700734 {NULL, 0, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735};
736
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737/* proceed only one option from a list *cur - string containing of mount options
738 opts - array of options which are accepted
739 opt_arg - if option is found and requires an argument and if it is specifed
740 in the input - pointer to the argument is stored here
741 bit_flags - if option requires to set a certain bit - it is set here
742 return -1 if unknown option is found, opt->arg_required otherwise */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700743static int reiserfs_getopt(struct super_block *s, char **cur, opt_desc_t * opts,
744 char **opt_arg, unsigned long *bit_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700746 char *p;
747 /* foo=bar,
748 ^ ^ ^
749 | | +-- option_end
750 | +-- arg_start
751 +-- option_start
752 */
753 const opt_desc_t *opt;
754 const arg_desc_t *arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700756 p = *cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700758 /* assume argument cannot contain commas */
759 *cur = strchr(p, ',');
760 if (*cur) {
761 *(*cur) = '\0';
762 (*cur)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700765 if (!strncmp(p, "alloc=", 6)) {
766 /* Ugly special case, probably we should redo options parser so that
767 it can understand several arguments for some options, also so that
768 it can fill several bitfields with option values. */
769 if (reiserfs_parse_alloc_options(s, p + 6)) {
770 return -1;
771 } else {
772 return 0;
773 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700775
776 /* for every option in the list */
777 for (opt = opts; opt->option_name; opt++) {
778 if (!strncmp(p, opt->option_name, strlen(opt->option_name))) {
779 if (bit_flags) {
780 if (opt->clrmask ==
781 (1 << REISERFS_UNSUPPORTED_OPT))
782 reiserfs_warning(s, "%s not supported.",
783 p);
784 else
785 *bit_flags &= ~opt->clrmask;
786 if (opt->setmask ==
787 (1 << REISERFS_UNSUPPORTED_OPT))
788 reiserfs_warning(s, "%s not supported.",
789 p);
790 else
791 *bit_flags |= opt->setmask;
792 }
793 break;
794 }
795 }
796 if (!opt->option_name) {
797 reiserfs_warning(s, "unknown mount option \"%s\"", p);
798 return -1;
799 }
800
801 p += strlen(opt->option_name);
802 switch (*p) {
803 case '=':
804 if (!opt->arg_required) {
805 reiserfs_warning(s,
806 "the option \"%s\" does not require an argument",
807 opt->option_name);
808 return -1;
809 }
810 break;
811
812 case 0:
813 if (opt->arg_required) {
814 reiserfs_warning(s,
815 "the option \"%s\" requires an argument",
816 opt->option_name);
817 return -1;
818 }
819 break;
820 default:
821 reiserfs_warning(s, "head of option \"%s\" is only correct",
822 opt->option_name);
823 return -1;
824 }
825
826 /* move to the argument, or to next option if argument is not required */
827 p++;
828
829 if (opt->arg_required
830 && !(opt->arg_required & (1 << REISERFS_OPT_ALLOWEMPTY))
831 && !strlen(p)) {
832 /* this catches "option=," if not allowed */
833 reiserfs_warning(s, "empty argument for \"%s\"",
834 opt->option_name);
835 return -1;
836 }
837
838 if (!opt->values) {
839 /* *=NULLopt_arg contains pointer to argument */
840 *opt_arg = p;
841 return opt->arg_required & ~(1 << REISERFS_OPT_ALLOWEMPTY);
842 }
843
844 /* values possible for this option are listed in opt->values */
845 for (arg = opt->values; arg->value; arg++) {
846 if (!strcmp(p, arg->value)) {
847 if (bit_flags) {
848 *bit_flags &= ~arg->clrmask;
849 *bit_flags |= arg->setmask;
850 }
851 return opt->arg_required;
852 }
853 }
854
855 reiserfs_warning(s, "bad value \"%s\" for option \"%s\"", p,
856 opt->option_name);
857 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858}
859
860/* returns 0 if something is wrong in option string, 1 - otherwise */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700861static int reiserfs_parse_options(struct super_block *s, char *options, /* string given via mount's -o */
862 unsigned long *mount_options,
863 /* after the parsing phase, contains the
864 collection of bitflags defining what
865 mount options were selected. */
866 unsigned long *blocks, /* strtol-ed from NNN of resize=NNN */
867 char **jdev_name,
868 unsigned int *commit_max_age)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700870 int c;
871 char *arg = NULL;
872 char *pos;
873 opt_desc_t opts[] = {
874 /* Compatibility stuff, so that -o notail for old setups still work */
875 {"tails",.arg_required = 't',.values = tails},
876 {"notail",.clrmask =
877 (1 << REISERFS_LARGETAIL) | (1 << REISERFS_SMALLTAIL)},
878 {"conv",.setmask = 1 << REISERFS_CONVERT},
879 {"attrs",.setmask = 1 << REISERFS_ATTRS},
880 {"noattrs",.clrmask = 1 << REISERFS_ATTRS},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881#ifdef CONFIG_REISERFS_FS_XATTR
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700882 {"user_xattr",.setmask = 1 << REISERFS_XATTRS_USER},
883 {"nouser_xattr",.clrmask = 1 << REISERFS_XATTRS_USER},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884#else
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700885 {"user_xattr",.setmask = 1 << REISERFS_UNSUPPORTED_OPT},
886 {"nouser_xattr",.clrmask = 1 << REISERFS_UNSUPPORTED_OPT},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887#endif
888#ifdef CONFIG_REISERFS_FS_POSIX_ACL
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700889 {"acl",.setmask = 1 << REISERFS_POSIXACL},
890 {"noacl",.clrmask = 1 << REISERFS_POSIXACL},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891#else
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700892 {"acl",.setmask = 1 << REISERFS_UNSUPPORTED_OPT},
893 {"noacl",.clrmask = 1 << REISERFS_UNSUPPORTED_OPT},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894#endif
Vladimir V. Savelievcd02b962006-03-25 03:07:15 -0800895 {.option_name = "nolog"},
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700896 {"replayonly",.setmask = 1 << REPLAYONLY},
897 {"block-allocator",.arg_required = 'a',.values = balloc},
898 {"data",.arg_required = 'd',.values = logging_mode},
899 {"barrier",.arg_required = 'b',.values = barrier_mode},
900 {"resize",.arg_required = 'r',.values = NULL},
901 {"jdev",.arg_required = 'j',.values = NULL},
902 {"nolargeio",.arg_required = 'w',.values = NULL},
903 {"commit",.arg_required = 'c',.values = NULL},
904 {"usrquota",.setmask = 1 << REISERFS_QUOTA},
905 {"grpquota",.setmask = 1 << REISERFS_QUOTA},
906 {"noquota",.clrmask = 1 << REISERFS_QUOTA},
907 {"errors",.arg_required = 'e',.values = error_actions},
908 {"usrjquota",.arg_required =
909 'u' | (1 << REISERFS_OPT_ALLOWEMPTY),.values = NULL},
910 {"grpjquota",.arg_required =
911 'g' | (1 << REISERFS_OPT_ALLOWEMPTY),.values = NULL},
912 {"jqfmt",.arg_required = 'f',.values = NULL},
Vladimir V. Savelievcd02b962006-03-25 03:07:15 -0800913 {.option_name = NULL}
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700914 };
915
916 *blocks = 0;
917 if (!options || !*options)
918 /* use default configuration: create tails, journaling on, no
919 conversion to newest format */
920 return 1;
921
922 for (pos = options; pos;) {
923 c = reiserfs_getopt(s, &pos, opts, &arg, mount_options);
924 if (c == -1)
925 /* wrong option is given */
926 return 0;
927
928 if (c == 'r') {
929 char *p;
930
931 p = NULL;
932 /* "resize=NNN" or "resize=auto" */
933
934 if (!strcmp(arg, "auto")) {
935 /* From JFS code, to auto-get the size. */
936 *blocks =
937 s->s_bdev->bd_inode->i_size >> s->
938 s_blocksize_bits;
939 } else {
940 *blocks = simple_strtoul(arg, &p, 0);
941 if (*p != '\0') {
942 /* NNN does not look like a number */
943 reiserfs_warning(s,
944 "reiserfs_parse_options: bad value %s",
945 arg);
946 return 0;
947 }
948 }
949 }
950
951 if (c == 'c') {
952 char *p = NULL;
953 unsigned long val = simple_strtoul(arg, &p, 0);
954 /* commit=NNN (time in seconds) */
955 if (*p != '\0' || val >= (unsigned int)-1) {
956 reiserfs_warning(s,
957 "reiserfs_parse_options: bad value %s",
958 arg);
959 return 0;
960 }
961 *commit_max_age = (unsigned int)val;
962 }
963
964 if (c == 'w') {
Adrian Bunk36b756f2006-09-27 01:50:50 -0700965 reiserfs_warning(s, "reiserfs: nolargeio option is no longer supported");
966 return 0;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700967 }
968
969 if (c == 'j') {
970 if (arg && *arg && jdev_name) {
971 if (*jdev_name) { //Hm, already assigned?
972 reiserfs_warning(s,
973 "reiserfs_parse_options: journal device was already specified to be %s",
974 *jdev_name);
975 return 0;
976 }
977 *jdev_name = arg;
978 }
979 }
980#ifdef CONFIG_QUOTA
981 if (c == 'u' || c == 'g') {
982 int qtype = c == 'u' ? USRQUOTA : GRPQUOTA;
983
984 if (sb_any_quota_enabled(s)) {
985 reiserfs_warning(s,
986 "reiserfs_parse_options: cannot change journalled quota options when quota turned on.");
987 return 0;
988 }
989 if (*arg) { /* Some filename specified? */
990 if (REISERFS_SB(s)->s_qf_names[qtype]
991 && strcmp(REISERFS_SB(s)->s_qf_names[qtype],
992 arg)) {
993 reiserfs_warning(s,
994 "reiserfs_parse_options: %s quota file already specified.",
995 QTYPE2NAME(qtype));
996 return 0;
997 }
998 if (strchr(arg, '/')) {
999 reiserfs_warning(s,
1000 "reiserfs_parse_options: quotafile must be on filesystem root.");
1001 return 0;
1002 }
1003 REISERFS_SB(s)->s_qf_names[qtype] =
1004 kmalloc(strlen(arg) + 1, GFP_KERNEL);
1005 if (!REISERFS_SB(s)->s_qf_names[qtype]) {
1006 reiserfs_warning(s,
1007 "reiserfs_parse_options: not enough memory for storing quotafile name.");
1008 return 0;
1009 }
1010 strcpy(REISERFS_SB(s)->s_qf_names[qtype], arg);
1011 *mount_options |= 1 << REISERFS_QUOTA;
1012 } else {
James Lamanna833d3042005-10-30 15:00:16 -08001013 kfree(REISERFS_SB(s)->s_qf_names[qtype]);
1014 REISERFS_SB(s)->s_qf_names[qtype] = NULL;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001015 }
1016 }
1017 if (c == 'f') {
1018 if (!strcmp(arg, "vfsold"))
1019 REISERFS_SB(s)->s_jquota_fmt = QFMT_VFS_OLD;
1020 else if (!strcmp(arg, "vfsv0"))
1021 REISERFS_SB(s)->s_jquota_fmt = QFMT_VFS_V0;
1022 else {
1023 reiserfs_warning(s,
1024 "reiserfs_parse_options: unknown quota format specified.");
1025 return 0;
1026 }
1027 }
1028#else
1029 if (c == 'u' || c == 'g' || c == 'f') {
1030 reiserfs_warning(s,
1031 "reiserfs_parse_options: journalled quota options not supported.");
1032 return 0;
1033 }
1034#endif
1035 }
1036
1037#ifdef CONFIG_QUOTA
1038 if (!REISERFS_SB(s)->s_jquota_fmt
1039 && (REISERFS_SB(s)->s_qf_names[USRQUOTA]
1040 || REISERFS_SB(s)->s_qf_names[GRPQUOTA])) {
1041 reiserfs_warning(s,
1042 "reiserfs_parse_options: journalled quota format not specified.");
1043 return 0;
1044 }
1045 /* This checking is not precise wrt the quota type but for our purposes it is sufficient */
1046 if (!(*mount_options & (1 << REISERFS_QUOTA))
1047 && sb_any_quota_enabled(s)) {
1048 reiserfs_warning(s,
1049 "reiserfs_parse_options: quota options must be present when quota is turned on.");
1050 return 0;
1051 }
1052#endif
1053
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055}
1056
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001057static void switch_data_mode(struct super_block *s, unsigned long mode)
1058{
1059 REISERFS_SB(s)->s_mount_opt &= ~((1 << REISERFS_DATA_LOG) |
1060 (1 << REISERFS_DATA_ORDERED) |
1061 (1 << REISERFS_DATA_WRITEBACK));
1062 REISERFS_SB(s)->s_mount_opt |= (1 << mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063}
1064
1065static void handle_data_mode(struct super_block *s, unsigned long mount_options)
1066{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001067 if (mount_options & (1 << REISERFS_DATA_LOG)) {
1068 if (!reiserfs_data_log(s)) {
1069 switch_data_mode(s, REISERFS_DATA_LOG);
1070 reiserfs_info(s, "switching to journaled data mode\n");
1071 }
1072 } else if (mount_options & (1 << REISERFS_DATA_ORDERED)) {
1073 if (!reiserfs_data_ordered(s)) {
1074 switch_data_mode(s, REISERFS_DATA_ORDERED);
1075 reiserfs_info(s, "switching to ordered data mode\n");
1076 }
1077 } else if (mount_options & (1 << REISERFS_DATA_WRITEBACK)) {
1078 if (!reiserfs_data_writeback(s)) {
1079 switch_data_mode(s, REISERFS_DATA_WRITEBACK);
1080 reiserfs_info(s, "switching to writeback data mode\n");
1081 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083}
1084
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001085static void handle_barrier_mode(struct super_block *s, unsigned long bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001087 int flush = (1 << REISERFS_BARRIER_FLUSH);
1088 int none = (1 << REISERFS_BARRIER_NONE);
1089 int all_barrier = flush | none;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001091 if (bits & all_barrier) {
1092 REISERFS_SB(s)->s_mount_opt &= ~all_barrier;
1093 if (bits & flush) {
1094 REISERFS_SB(s)->s_mount_opt |= flush;
1095 printk("reiserfs: enabling write barrier flush mode\n");
1096 } else if (bits & none) {
1097 REISERFS_SB(s)->s_mount_opt |= none;
1098 printk("reiserfs: write barriers turned off\n");
1099 }
1100 }
1101}
1102
1103static void handle_attrs(struct super_block *s)
1104{
1105 struct reiserfs_super_block *rs = SB_DISK_SUPER_BLOCK(s);
1106
1107 if (reiserfs_attrs(s)) {
1108 if (old_format_only(s)) {
1109 reiserfs_warning(s,
1110 "reiserfs: cannot support attributes on 3.5.x disk format");
1111 REISERFS_SB(s)->s_mount_opt &= ~(1 << REISERFS_ATTRS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 return;
1113 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001114 if (!(le32_to_cpu(rs->s_flags) & reiserfs_attrs_cleared)) {
1115 reiserfs_warning(s,
1116 "reiserfs: cannot support attributes until flag is set in super-block");
1117 REISERFS_SB(s)->s_mount_opt &= ~(1 << REISERFS_ATTRS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 }
1119 }
1120}
1121
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001122static int reiserfs_remount(struct super_block *s, int *mount_flags, char *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001124 struct reiserfs_super_block *rs;
1125 struct reiserfs_transaction_handle th;
1126 unsigned long blocks;
1127 unsigned long mount_options = REISERFS_SB(s)->s_mount_opt;
1128 unsigned long safe_mask = 0;
1129 unsigned int commit_max_age = (unsigned int)-1;
1130 struct reiserfs_journal *journal = SB_JOURNAL(s);
1131 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132#ifdef CONFIG_QUOTA
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001133 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134#endif
1135
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001136 rs = SB_DISK_SUPER_BLOCK(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001138 if (!reiserfs_parse_options
1139 (s, arg, &mount_options, &blocks, NULL, &commit_max_age)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140#ifdef CONFIG_QUOTA
James Lamanna833d3042005-10-30 15:00:16 -08001141 for (i = 0; i < MAXQUOTAS; i++) {
1142 kfree(REISERFS_SB(s)->s_qf_names[i]);
1143 REISERFS_SB(s)->s_qf_names[i] = NULL;
1144 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001145#endif
1146 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001149 handle_attrs(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001151 /* Add options that are safe here */
1152 safe_mask |= 1 << REISERFS_SMALLTAIL;
1153 safe_mask |= 1 << REISERFS_LARGETAIL;
1154 safe_mask |= 1 << REISERFS_NO_BORDER;
1155 safe_mask |= 1 << REISERFS_NO_UNHASHED_RELOCATION;
1156 safe_mask |= 1 << REISERFS_HASHED_RELOCATION;
1157 safe_mask |= 1 << REISERFS_TEST4;
1158 safe_mask |= 1 << REISERFS_ATTRS;
1159 safe_mask |= 1 << REISERFS_XATTRS_USER;
1160 safe_mask |= 1 << REISERFS_POSIXACL;
1161 safe_mask |= 1 << REISERFS_BARRIER_FLUSH;
1162 safe_mask |= 1 << REISERFS_BARRIER_NONE;
1163 safe_mask |= 1 << REISERFS_ERROR_RO;
1164 safe_mask |= 1 << REISERFS_ERROR_CONTINUE;
1165 safe_mask |= 1 << REISERFS_ERROR_PANIC;
1166 safe_mask |= 1 << REISERFS_QUOTA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001168 /* Update the bitmask, taking care to keep
1169 * the bits we're not allowed to change here */
1170 REISERFS_SB(s)->s_mount_opt =
1171 (REISERFS_SB(s)->
1172 s_mount_opt & ~safe_mask) | (mount_options & safe_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001174 if (commit_max_age != 0 && commit_max_age != (unsigned int)-1) {
1175 journal->j_max_commit_age = commit_max_age;
1176 journal->j_max_trans_age = commit_max_age;
1177 } else if (commit_max_age == 0) {
1178 /* 0 means restore defaults. */
1179 journal->j_max_commit_age = journal->j_default_max_commit_age;
1180 journal->j_max_trans_age = JOURNAL_MAX_TRANS_AGE;
1181 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001183 if (blocks) {
1184 int rc = reiserfs_resize(s, blocks);
1185 if (rc != 0)
1186 return rc;
1187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001189 if (*mount_flags & MS_RDONLY) {
1190 reiserfs_xattr_init(s, *mount_flags);
1191 /* remount read-only */
1192 if (s->s_flags & MS_RDONLY)
1193 /* it is read-only already */
1194 return 0;
1195 /* try to remount file system with read-only permissions */
1196 if (sb_umount_state(rs) == REISERFS_VALID_FS
1197 || REISERFS_SB(s)->s_mount_state != REISERFS_VALID_FS) {
1198 return 0;
1199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001201 err = journal_begin(&th, s, 10);
1202 if (err)
1203 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001205 /* Mounting a rw partition read-only. */
1206 reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s), 1);
1207 set_sb_umount_state(rs, REISERFS_SB(s)->s_mount_state);
1208 journal_mark_dirty(&th, s, SB_BUFFER_WITH_SB(s));
1209 } else {
1210 /* remount read-write */
1211 if (!(s->s_flags & MS_RDONLY)) {
1212 reiserfs_xattr_init(s, *mount_flags);
1213 return 0; /* We are read-write already */
1214 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001216 if (reiserfs_is_journal_aborted(journal))
1217 return journal->j_errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001219 handle_data_mode(s, mount_options);
1220 handle_barrier_mode(s, mount_options);
1221 REISERFS_SB(s)->s_mount_state = sb_umount_state(rs);
1222 s->s_flags &= ~MS_RDONLY; /* now it is safe to call journal_begin */
1223 err = journal_begin(&th, s, 10);
1224 if (err)
1225 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001227 /* Mount a partition which is read-only, read-write */
1228 reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s), 1);
1229 REISERFS_SB(s)->s_mount_state = sb_umount_state(rs);
1230 s->s_flags &= ~MS_RDONLY;
1231 set_sb_umount_state(rs, REISERFS_ERROR_FS);
1232 /* mark_buffer_dirty (SB_BUFFER_WITH_SB (s), 1); */
1233 journal_mark_dirty(&th, s, SB_BUFFER_WITH_SB(s));
1234 REISERFS_SB(s)->s_mount_state = REISERFS_VALID_FS;
1235 }
1236 /* this will force a full flush of all journal lists */
1237 SB_JOURNAL(s)->j_must_wait = 1;
1238 err = journal_end(&th, s, 10);
1239 if (err)
1240 return err;
1241 s->s_dirt = 0;
1242
1243 if (!(*mount_flags & MS_RDONLY)) {
1244 finish_unfinished(s);
1245 reiserfs_xattr_init(s, *mount_flags);
1246 }
1247
1248 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249}
1250
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001251static int read_super_block(struct super_block *s, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001253 struct buffer_head *bh;
1254 struct reiserfs_super_block *rs;
1255 int fs_blocksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001257 bh = sb_bread(s, offset / s->s_blocksize);
1258 if (!bh) {
1259 reiserfs_warning(s, "sh-2006: read_super_block: "
1260 "bread failed (dev %s, block %lu, size %lu)",
1261 reiserfs_bdevname(s), offset / s->s_blocksize,
1262 s->s_blocksize);
1263 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001266 rs = (struct reiserfs_super_block *)bh->b_data;
1267 if (!is_any_reiserfs_magic_string(rs)) {
1268 brelse(bh);
1269 return 1;
1270 }
1271 //
1272 // ok, reiserfs signature (old or new) found in at the given offset
1273 //
1274 fs_blocksize = sb_blocksize(rs);
1275 brelse(bh);
1276 sb_set_blocksize(s, fs_blocksize);
1277
1278 bh = sb_bread(s, offset / s->s_blocksize);
1279 if (!bh) {
1280 reiserfs_warning(s, "sh-2007: read_super_block: "
1281 "bread failed (dev %s, block %lu, size %lu)\n",
1282 reiserfs_bdevname(s), offset / s->s_blocksize,
1283 s->s_blocksize);
1284 return 1;
1285 }
1286
1287 rs = (struct reiserfs_super_block *)bh->b_data;
1288 if (sb_blocksize(rs) != s->s_blocksize) {
1289 reiserfs_warning(s, "sh-2011: read_super_block: "
1290 "can't find a reiserfs filesystem on (dev %s, block %Lu, size %lu)\n",
1291 reiserfs_bdevname(s),
1292 (unsigned long long)bh->b_blocknr,
1293 s->s_blocksize);
1294 brelse(bh);
1295 return 1;
1296 }
1297
1298 if (rs->s_v1.s_root_block == cpu_to_le32(-1)) {
1299 brelse(bh);
1300 reiserfs_warning(s,
1301 "Unfinished reiserfsck --rebuild-tree run detected. Please run\n"
1302 "reiserfsck --rebuild-tree and wait for a completion. If that fails\n"
1303 "get newer reiserfsprogs package");
1304 return 1;
1305 }
1306
1307 SB_BUFFER_WITH_SB(s) = bh;
1308 SB_DISK_SUPER_BLOCK(s) = rs;
1309
1310 if (is_reiserfs_jr(rs)) {
1311 /* magic is of non-standard journal filesystem, look at s_version to
1312 find which format is in use */
1313 if (sb_version(rs) == REISERFS_VERSION_2)
1314 reiserfs_warning(s,
1315 "read_super_block: found reiserfs format \"3.6\""
1316 " with non-standard journal");
1317 else if (sb_version(rs) == REISERFS_VERSION_1)
1318 reiserfs_warning(s,
1319 "read_super_block: found reiserfs format \"3.5\""
1320 " with non-standard journal");
1321 else {
1322 reiserfs_warning(s,
1323 "sh-2012: read_super_block: found unknown "
1324 "format \"%u\" of reiserfs with non-standard magic",
1325 sb_version(rs));
1326 return 1;
1327 }
1328 } else
1329 /* s_version of standard format may contain incorrect information,
1330 so we just look at the magic string */
1331 reiserfs_info(s,
1332 "found reiserfs format \"%s\" with standard journal\n",
1333 is_reiserfs_3_5(rs) ? "3.5" : "3.6");
1334
1335 s->s_op = &reiserfs_sops;
1336 s->s_export_op = &reiserfs_export_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337#ifdef CONFIG_QUOTA
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001338 s->s_qcop = &reiserfs_qctl_operations;
1339 s->dq_op = &reiserfs_quota_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340#endif
1341
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001342 /* new format is limited by the 32 bit wide i_blocks field, want to
1343 ** be one full block below that.
1344 */
1345 s->s_maxbytes = (512LL << 32) - s->s_blocksize;
1346 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347}
1348
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349/* after journal replay, reread all bitmap and super blocks */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001350static int reread_meta_blocks(struct super_block *s)
1351{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001352 ll_rw_block(READ, 1, &(SB_BUFFER_WITH_SB(s)));
1353 wait_on_buffer(SB_BUFFER_WITH_SB(s));
1354 if (!buffer_uptodate(SB_BUFFER_WITH_SB(s))) {
1355 reiserfs_warning(s,
1356 "reread_meta_blocks, error reading the super");
1357 return 1;
1358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001360 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361}
1362
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363/////////////////////////////////////////////////////
1364// hash detection stuff
1365
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366// if root directory is empty - we set default - Yura's - hash and
1367// warn about it
1368// FIXME: we look for only one name in a directory. If tea and yura
1369// bith have the same value - we ask user to send report to the
1370// mailing list
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001371static __u32 find_hash_out(struct super_block *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001373 int retval;
1374 struct inode *inode;
1375 struct cpu_key key;
1376 INITIALIZE_PATH(path);
1377 struct reiserfs_dir_entry de;
1378 __u32 hash = DEFAULT_HASH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001380 inode = s->s_root->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001382 do { // Some serious "goto"-hater was there ;)
1383 u32 teahash, r5hash, yurahash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001385 make_cpu_key(&key, inode, ~0, TYPE_DIRENTRY, 3);
1386 retval = search_by_entry_key(s, &key, &path, &de);
1387 if (retval == IO_ERROR) {
1388 pathrelse(&path);
1389 return UNSET_HASH;
1390 }
1391 if (retval == NAME_NOT_FOUND)
1392 de.de_entry_num--;
1393 set_de_name_and_namelen(&de);
1394 if (deh_offset(&(de.de_deh[de.de_entry_num])) == DOT_DOT_OFFSET) {
1395 /* allow override in this case */
1396 if (reiserfs_rupasov_hash(s)) {
1397 hash = YURA_HASH;
1398 }
1399 reiserfs_warning(s, "FS seems to be empty, autodetect "
1400 "is using the default hash");
1401 break;
1402 }
1403 r5hash = GET_HASH_VALUE(r5_hash(de.de_name, de.de_namelen));
1404 teahash = GET_HASH_VALUE(keyed_hash(de.de_name, de.de_namelen));
1405 yurahash = GET_HASH_VALUE(yura_hash(de.de_name, de.de_namelen));
1406 if (((teahash == r5hash)
1407 &&
1408 (GET_HASH_VALUE(deh_offset(&(de.de_deh[de.de_entry_num])))
1409 == r5hash)) || ((teahash == yurahash)
1410 && (yurahash ==
1411 GET_HASH_VALUE(deh_offset
1412 (&
1413 (de.
1414 de_deh[de.
1415 de_entry_num])))))
1416 || ((r5hash == yurahash)
1417 && (yurahash ==
1418 GET_HASH_VALUE(deh_offset
1419 (&(de.de_deh[de.de_entry_num])))))) {
1420 reiserfs_warning(s,
1421 "Unable to automatically detect hash function. "
1422 "Please mount with -o hash={tea,rupasov,r5}",
1423 reiserfs_bdevname(s));
1424 hash = UNSET_HASH;
1425 break;
1426 }
1427 if (GET_HASH_VALUE(deh_offset(&(de.de_deh[de.de_entry_num]))) ==
1428 yurahash)
1429 hash = YURA_HASH;
1430 else if (GET_HASH_VALUE
1431 (deh_offset(&(de.de_deh[de.de_entry_num]))) == teahash)
1432 hash = TEA_HASH;
1433 else if (GET_HASH_VALUE
1434 (deh_offset(&(de.de_deh[de.de_entry_num]))) == r5hash)
1435 hash = R5_HASH;
1436 else {
1437 reiserfs_warning(s, "Unrecognised hash function");
1438 hash = UNSET_HASH;
1439 }
1440 } while (0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001442 pathrelse(&path);
1443 return hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444}
1445
1446// finds out which hash names are sorted with
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001447static int what_hash(struct super_block *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001449 __u32 code;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001451 code = sb_hash_function_code(SB_DISK_SUPER_BLOCK(s));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001453 /* reiserfs_hash_detect() == true if any of the hash mount options
1454 ** were used. We must check them to make sure the user isn't
1455 ** using a bad hash value
1456 */
1457 if (code == UNSET_HASH || reiserfs_hash_detect(s))
1458 code = find_hash_out(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001460 if (code != UNSET_HASH && reiserfs_hash_detect(s)) {
1461 /* detection has found the hash, and we must check against the
1462 ** mount options
1463 */
1464 if (reiserfs_rupasov_hash(s) && code != YURA_HASH) {
1465 reiserfs_warning(s, "Error, %s hash detected, "
1466 "unable to force rupasov hash",
1467 reiserfs_hashname(code));
1468 code = UNSET_HASH;
1469 } else if (reiserfs_tea_hash(s) && code != TEA_HASH) {
1470 reiserfs_warning(s, "Error, %s hash detected, "
1471 "unable to force tea hash",
1472 reiserfs_hashname(code));
1473 code = UNSET_HASH;
1474 } else if (reiserfs_r5_hash(s) && code != R5_HASH) {
1475 reiserfs_warning(s, "Error, %s hash detected, "
1476 "unable to force r5 hash",
1477 reiserfs_hashname(code));
1478 code = UNSET_HASH;
1479 }
1480 } else {
1481 /* find_hash_out was not called or could not determine the hash */
1482 if (reiserfs_rupasov_hash(s)) {
1483 code = YURA_HASH;
1484 } else if (reiserfs_tea_hash(s)) {
1485 code = TEA_HASH;
1486 } else if (reiserfs_r5_hash(s)) {
1487 code = R5_HASH;
1488 }
1489 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001491 /* if we are mounted RW, and we have a new valid hash code, update
1492 ** the super
1493 */
1494 if (code != UNSET_HASH &&
1495 !(s->s_flags & MS_RDONLY) &&
1496 code != sb_hash_function_code(SB_DISK_SUPER_BLOCK(s))) {
1497 set_sb_hash_function_code(SB_DISK_SUPER_BLOCK(s), code);
1498 }
1499 return code;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500}
1501
1502// return pointer to appropriate function
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001503static hashf_t hash_function(struct super_block *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001505 switch (what_hash(s)) {
1506 case TEA_HASH:
1507 reiserfs_info(s, "Using tea hash to sort names\n");
1508 return keyed_hash;
1509 case YURA_HASH:
1510 reiserfs_info(s, "Using rupasov hash to sort names\n");
1511 return yura_hash;
1512 case R5_HASH:
1513 reiserfs_info(s, "Using r5 hash to sort names\n");
1514 return r5_hash;
1515 }
1516 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517}
1518
1519// this is used to set up correct value for old partitions
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001520static int function2code(hashf_t func)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001522 if (func == keyed_hash)
1523 return TEA_HASH;
1524 if (func == yura_hash)
1525 return YURA_HASH;
1526 if (func == r5_hash)
1527 return R5_HASH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001529 BUG(); // should never happen
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001531 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532}
1533
1534#define SWARN(silent, s, ...) \
1535 if (!(silent)) \
1536 reiserfs_warning (s, __VA_ARGS__)
1537
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001538static int reiserfs_fill_super(struct super_block *s, void *data, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001540 struct inode *root_inode;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001541 struct reiserfs_transaction_handle th;
1542 int old_format = 0;
1543 unsigned long blocks;
1544 unsigned int commit_max_age = 0;
1545 int jinit_done = 0;
1546 struct reiserfs_iget_args args;
1547 struct reiserfs_super_block *rs;
1548 char *jdev_name;
1549 struct reiserfs_sb_info *sbi;
1550 int errval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
Yan Burman01afb212006-12-06 20:39:01 -08001552 sbi = kzalloc(sizeof(struct reiserfs_sb_info), GFP_KERNEL);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001553 if (!sbi) {
1554 errval = -ENOMEM;
1555 goto error;
1556 }
1557 s->s_fs_info = sbi;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001558 /* Set default values for options: non-aggressive tails, RO on errors */
1559 REISERFS_SB(s)->s_mount_opt |= (1 << REISERFS_SMALLTAIL);
1560 REISERFS_SB(s)->s_mount_opt |= (1 << REISERFS_ERROR_RO);
1561 /* no preallocation minimum, be smart in
1562 reiserfs_file_write instead */
1563 REISERFS_SB(s)->s_alloc_options.preallocmin = 0;
1564 /* Preallocate by 16 blocks (17-1) at once */
1565 REISERFS_SB(s)->s_alloc_options.preallocsize = 17;
Alexey Dobriyanfe08a9d2007-05-08 00:24:55 -07001566#ifdef CONFIG_REISERFS_FS_XATTR
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001567 /* Initialize the rwsem for xattr dir */
1568 init_rwsem(&REISERFS_SB(s)->xattr_dir_sem);
Alexey Dobriyanfe08a9d2007-05-08 00:24:55 -07001569#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001570 /* setup default block allocator options */
1571 reiserfs_init_alloc_options(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001573 jdev_name = NULL;
1574 if (reiserfs_parse_options
1575 (s, (char *)data, &(sbi->s_mount_opt), &blocks, &jdev_name,
1576 &commit_max_age) == 0) {
1577 goto error;
1578 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001580 if (blocks) {
1581 SWARN(silent, s, "jmacd-7: reiserfs_fill_super: resize option "
1582 "for remount only");
1583 goto error;
1584 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001586 /* try old format (undistributed bitmap, super block in 8-th 1k block of a device) */
1587 if (!read_super_block(s, REISERFS_OLD_DISK_OFFSET_IN_BYTES))
1588 old_format = 1;
1589 /* try new format (64-th 1k block), which can contain reiserfs super block */
1590 else if (read_super_block(s, REISERFS_DISK_OFFSET_IN_BYTES)) {
1591 SWARN(silent, s,
1592 "sh-2021: reiserfs_fill_super: can not find reiserfs on %s",
1593 reiserfs_bdevname(s));
1594 goto error;
1595 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001597 rs = SB_DISK_SUPER_BLOCK(s);
1598 /* Let's do basic sanity check to verify that underlying device is not
1599 smaller than the filesystem. If the check fails then abort and scream,
1600 because bad stuff will happen otherwise. */
1601 if (s->s_bdev && s->s_bdev->bd_inode
1602 && i_size_read(s->s_bdev->bd_inode) <
1603 sb_block_count(rs) * sb_blocksize(rs)) {
1604 SWARN(silent, s,
1605 "Filesystem on %s cannot be mounted because it is bigger than the device",
1606 reiserfs_bdevname(s));
1607 SWARN(silent, s,
1608 "You may need to run fsck or increase size of your LVM partition");
1609 SWARN(silent, s,
1610 "Or may be you forgot to reboot after fdisk when it told you to");
1611 goto error;
1612 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001614 sbi->s_mount_state = SB_REISERFS_STATE(s);
1615 sbi->s_mount_state = REISERFS_VALID_FS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616
Jeff Mahoney6f010462006-09-30 23:28:43 -07001617 if ((errval = reiserfs_init_bitmap_cache(s))) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001618 SWARN(silent, s,
1619 "jmacd-8: reiserfs_fill_super: unable to read bitmap");
1620 goto error;
1621 }
Jeff Mahoneyd2c89a42006-11-02 22:07:20 -08001622 errval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623#ifdef CONFIG_REISERFS_CHECK
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001624 SWARN(silent, s, "CONFIG_REISERFS_CHECK is set ON");
1625 SWARN(silent, s, "- it is slow mode for debugging.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626#endif
1627
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001628 /* make data=ordered the default */
1629 if (!reiserfs_data_log(s) && !reiserfs_data_ordered(s) &&
1630 !reiserfs_data_writeback(s)) {
1631 REISERFS_SB(s)->s_mount_opt |= (1 << REISERFS_DATA_ORDERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 }
1633
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001634 if (reiserfs_data_log(s)) {
1635 reiserfs_info(s, "using journaled data mode\n");
1636 } else if (reiserfs_data_ordered(s)) {
1637 reiserfs_info(s, "using ordered data mode\n");
1638 } else {
1639 reiserfs_info(s, "using writeback data mode\n");
1640 }
1641 if (reiserfs_barrier_flush(s)) {
1642 printk("reiserfs: using flush barriers\n");
1643 }
1644 // set_device_ro(s->s_dev, 1) ;
1645 if (journal_init(s, jdev_name, old_format, commit_max_age)) {
1646 SWARN(silent, s,
1647 "sh-2022: reiserfs_fill_super: unable to initialize journal space");
1648 goto error;
1649 } else {
1650 jinit_done = 1; /* once this is set, journal_release must be called
1651 ** if we error out of the mount
1652 */
1653 }
1654 if (reread_meta_blocks(s)) {
1655 SWARN(silent, s,
1656 "jmacd-9: reiserfs_fill_super: unable to reread meta blocks after journal init");
1657 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 }
1659
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001660 if (replay_only(s))
1661 goto error;
1662
1663 if (bdev_read_only(s->s_bdev) && !(s->s_flags & MS_RDONLY)) {
1664 SWARN(silent, s,
1665 "clm-7000: Detected readonly device, marking FS readonly");
1666 s->s_flags |= MS_RDONLY;
1667 }
1668 args.objectid = REISERFS_ROOT_OBJECTID;
1669 args.dirid = REISERFS_ROOT_PARENT_OBJECTID;
1670 root_inode =
1671 iget5_locked(s, REISERFS_ROOT_OBJECTID, reiserfs_find_actor,
1672 reiserfs_init_locked_inode, (void *)(&args));
1673 if (!root_inode) {
1674 SWARN(silent, s,
1675 "jmacd-10: reiserfs_fill_super: get root inode failed");
1676 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 }
1678
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001679 if (root_inode->i_state & I_NEW) {
1680 reiserfs_read_locked_inode(root_inode, &args);
1681 unlock_new_inode(root_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 }
1683
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001684 s->s_root = d_alloc_root(root_inode);
1685 if (!s->s_root) {
1686 iput(root_inode);
1687 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001689 // define and initialize hash function
1690 sbi->s_hash_function = hash_function(s);
1691 if (sbi->s_hash_function == NULL) {
1692 dput(s->s_root);
1693 s->s_root = NULL;
1694 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001696
1697 if (is_reiserfs_3_5(rs)
1698 || (is_reiserfs_jr(rs) && SB_VERSION(s) == REISERFS_VERSION_1))
1699 set_bit(REISERFS_3_5, &(sbi->s_properties));
Jeff Mahoneye1fabd32006-09-30 23:28:40 -07001700 else if (old_format)
1701 set_bit(REISERFS_OLD_FORMAT, &(sbi->s_properties));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001702 else
1703 set_bit(REISERFS_3_6, &(sbi->s_properties));
1704
1705 if (!(s->s_flags & MS_RDONLY)) {
1706
1707 errval = journal_begin(&th, s, 1);
1708 if (errval) {
1709 dput(s->s_root);
1710 s->s_root = NULL;
1711 goto error;
1712 }
1713 reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s), 1);
1714
1715 set_sb_umount_state(rs, REISERFS_ERROR_FS);
1716 set_sb_fs_state(rs, 0);
1717
1718 if (old_format_only(s)) {
1719 /* filesystem of format 3.5 either with standard or non-standard
1720 journal */
1721 if (convert_reiserfs(s)) {
1722 /* and -o conv is given */
1723 if (!silent)
1724 reiserfs_info(s,
1725 "converting 3.5 filesystem to the 3.6 format");
1726
1727 if (is_reiserfs_3_5(rs))
1728 /* put magic string of 3.6 format. 2.2 will not be able to
1729 mount this filesystem anymore */
1730 memcpy(rs->s_v1.s_magic,
1731 reiserfs_3_6_magic_string,
1732 sizeof
1733 (reiserfs_3_6_magic_string));
1734
1735 set_sb_version(rs, REISERFS_VERSION_2);
1736 reiserfs_convert_objectid_map_v1(s);
1737 set_bit(REISERFS_3_6, &(sbi->s_properties));
1738 clear_bit(REISERFS_3_5, &(sbi->s_properties));
1739 } else if (!silent) {
1740 reiserfs_info(s, "using 3.5.x disk format\n");
1741 }
1742 }
1743
1744 journal_mark_dirty(&th, s, SB_BUFFER_WITH_SB(s));
1745 errval = journal_end(&th, s, 1);
1746 if (errval) {
1747 dput(s->s_root);
1748 s->s_root = NULL;
1749 goto error;
1750 }
1751
1752 if ((errval = reiserfs_xattr_init(s, s->s_flags))) {
1753 dput(s->s_root);
1754 s->s_root = NULL;
1755 goto error;
1756 }
1757
1758 /* look for files which were to be removed in previous session */
1759 finish_unfinished(s);
1760 } else {
1761 if (old_format_only(s) && !silent) {
1762 reiserfs_info(s, "using 3.5.x disk format\n");
1763 }
1764
1765 if ((errval = reiserfs_xattr_init(s, s->s_flags))) {
1766 dput(s->s_root);
1767 s->s_root = NULL;
1768 goto error;
1769 }
1770 }
1771 // mark hash in super block: it could be unset. overwrite should be ok
1772 set_sb_hash_function_code(rs, function2code(sbi->s_hash_function));
1773
1774 handle_attrs(s);
1775
1776 reiserfs_proc_info_init(s);
1777
1778 init_waitqueue_head(&(sbi->s_wait));
1779 spin_lock_init(&sbi->bitmap_lock);
1780
1781 return (0);
1782
1783 error:
1784 if (jinit_done) { /* kill the commit thread, free journal ram */
1785 journal_release_error(NULL, s);
1786 }
Jeff Mahoney50652272006-09-30 23:28:44 -07001787
1788 reiserfs_free_bitmap_cache(s);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001789 if (SB_BUFFER_WITH_SB(s))
1790 brelse(SB_BUFFER_WITH_SB(s));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791#ifdef CONFIG_QUOTA
Jeff Mahoney50652272006-09-30 23:28:44 -07001792 {
1793 int j;
1794 for (j = 0; j < MAXQUOTAS; j++) {
1795 kfree(sbi->s_qf_names[j]);
1796 sbi->s_qf_names[j] = NULL;
1797 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001798 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799#endif
James Lamanna833d3042005-10-30 15:00:16 -08001800 kfree(sbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001802 s->s_fs_info = NULL;
1803 return errval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804}
1805
David Howells726c3342006-06-23 02:02:58 -07001806static int reiserfs_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807{
David Howells726c3342006-06-23 02:02:58 -07001808 struct reiserfs_super_block *rs = SB_DISK_SUPER_BLOCK(dentry->d_sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001809
1810 buf->f_namelen = (REISERFS_MAX_NAME(s->s_blocksize));
1811 buf->f_bfree = sb_free_blocks(rs);
1812 buf->f_bavail = buf->f_bfree;
1813 buf->f_blocks = sb_block_count(rs) - sb_bmap_nr(rs) - 1;
David Howells726c3342006-06-23 02:02:58 -07001814 buf->f_bsize = dentry->d_sb->s_blocksize;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001815 /* changed to accommodate gcc folks. */
1816 buf->f_type = REISERFS_SUPER_MAGIC;
1817 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818}
1819
1820#ifdef CONFIG_QUOTA
1821static int reiserfs_dquot_initialize(struct inode *inode, int type)
1822{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001823 struct reiserfs_transaction_handle th;
1824 int ret, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001826 /* We may create quota structure so we need to reserve enough blocks */
1827 reiserfs_write_lock(inode->i_sb);
1828 ret =
1829 journal_begin(&th, inode->i_sb,
1830 2 * REISERFS_QUOTA_INIT_BLOCKS(inode->i_sb));
1831 if (ret)
1832 goto out;
1833 ret = dquot_initialize(inode, type);
1834 err =
1835 journal_end(&th, inode->i_sb,
1836 2 * REISERFS_QUOTA_INIT_BLOCKS(inode->i_sb));
1837 if (!ret && err)
1838 ret = err;
1839 out:
1840 reiserfs_write_unlock(inode->i_sb);
1841 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842}
1843
1844static int reiserfs_dquot_drop(struct inode *inode)
1845{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001846 struct reiserfs_transaction_handle th;
1847 int ret, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001849 /* We may delete quota structure so we need to reserve enough blocks */
1850 reiserfs_write_lock(inode->i_sb);
1851 ret =
1852 journal_begin(&th, inode->i_sb,
1853 2 * REISERFS_QUOTA_DEL_BLOCKS(inode->i_sb));
1854 if (ret)
1855 goto out;
1856 ret = dquot_drop(inode);
1857 err =
1858 journal_end(&th, inode->i_sb,
1859 2 * REISERFS_QUOTA_DEL_BLOCKS(inode->i_sb));
1860 if (!ret && err)
1861 ret = err;
1862 out:
1863 reiserfs_write_unlock(inode->i_sb);
1864 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865}
1866
1867static int reiserfs_write_dquot(struct dquot *dquot)
1868{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001869 struct reiserfs_transaction_handle th;
1870 int ret, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001872 reiserfs_write_lock(dquot->dq_sb);
1873 ret =
1874 journal_begin(&th, dquot->dq_sb,
1875 REISERFS_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
1876 if (ret)
1877 goto out;
1878 ret = dquot_commit(dquot);
1879 err =
1880 journal_end(&th, dquot->dq_sb,
1881 REISERFS_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
1882 if (!ret && err)
1883 ret = err;
1884 out:
1885 reiserfs_write_unlock(dquot->dq_sb);
1886 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887}
1888
1889static int reiserfs_acquire_dquot(struct dquot *dquot)
1890{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001891 struct reiserfs_transaction_handle th;
1892 int ret, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001894 reiserfs_write_lock(dquot->dq_sb);
1895 ret =
1896 journal_begin(&th, dquot->dq_sb,
1897 REISERFS_QUOTA_INIT_BLOCKS(dquot->dq_sb));
1898 if (ret)
1899 goto out;
1900 ret = dquot_acquire(dquot);
1901 err =
1902 journal_end(&th, dquot->dq_sb,
1903 REISERFS_QUOTA_INIT_BLOCKS(dquot->dq_sb));
1904 if (!ret && err)
1905 ret = err;
1906 out:
1907 reiserfs_write_unlock(dquot->dq_sb);
1908 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909}
1910
1911static int reiserfs_release_dquot(struct dquot *dquot)
1912{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001913 struct reiserfs_transaction_handle th;
1914 int ret, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001916 reiserfs_write_lock(dquot->dq_sb);
1917 ret =
1918 journal_begin(&th, dquot->dq_sb,
1919 REISERFS_QUOTA_DEL_BLOCKS(dquot->dq_sb));
1920 if (ret)
1921 goto out;
1922 ret = dquot_release(dquot);
1923 err =
1924 journal_end(&th, dquot->dq_sb,
1925 REISERFS_QUOTA_DEL_BLOCKS(dquot->dq_sb));
1926 if (!ret && err)
1927 ret = err;
1928 out:
1929 reiserfs_write_unlock(dquot->dq_sb);
1930 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931}
1932
1933static int reiserfs_mark_dquot_dirty(struct dquot *dquot)
1934{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001935 /* Are we journalling quotas? */
1936 if (REISERFS_SB(dquot->dq_sb)->s_qf_names[USRQUOTA] ||
1937 REISERFS_SB(dquot->dq_sb)->s_qf_names[GRPQUOTA]) {
1938 dquot_mark_dquot_dirty(dquot);
1939 return reiserfs_write_dquot(dquot);
1940 } else
1941 return dquot_mark_dquot_dirty(dquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942}
1943
1944static int reiserfs_write_info(struct super_block *sb, int type)
1945{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001946 struct reiserfs_transaction_handle th;
1947 int ret, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001949 /* Data block + inode block */
1950 reiserfs_write_lock(sb);
1951 ret = journal_begin(&th, sb, 2);
1952 if (ret)
1953 goto out;
1954 ret = dquot_commit_info(sb, type);
1955 err = journal_end(&th, sb, 2);
1956 if (!ret && err)
1957 ret = err;
1958 out:
1959 reiserfs_write_unlock(sb);
1960 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961}
1962
1963/*
Christoph Hellwig84de8562005-06-23 00:09:16 -07001964 * Turn on quotas during mount time - we need to find the quota file and such...
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 */
1966static int reiserfs_quota_on_mount(struct super_block *sb, int type)
1967{
Christoph Hellwig84de8562005-06-23 00:09:16 -07001968 return vfs_quota_on_mount(sb, REISERFS_SB(sb)->s_qf_names[type],
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001969 REISERFS_SB(sb)->s_jquota_fmt, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970}
1971
1972/*
1973 * Standard function to be called on quota_on
1974 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001975static int reiserfs_quota_on(struct super_block *sb, int type, int format_id,
1976 char *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001978 int err;
1979 struct nameidata nd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001981 if (!(REISERFS_SB(sb)->s_mount_opt & (1 << REISERFS_QUOTA)))
1982 return -EINVAL;
1983 err = path_lookup(path, LOOKUP_FOLLOW, &nd);
1984 if (err)
1985 return err;
1986 /* Quotafile not on the same filesystem? */
1987 if (nd.mnt->mnt_sb != sb) {
1988 path_release(&nd);
1989 return -EXDEV;
1990 }
1991 /* We must not pack tails for quota files on reiserfs for quota IO to work */
1992 if (!REISERFS_I(nd.dentry->d_inode)->i_flags & i_nopack_mask) {
1993 reiserfs_warning(sb,
1994 "reiserfs: Quota file must have tail packing disabled.");
1995 path_release(&nd);
1996 return -EINVAL;
1997 }
1998 /* Not journalling quota? No more tests needed... */
1999 if (!REISERFS_SB(sb)->s_qf_names[USRQUOTA] &&
2000 !REISERFS_SB(sb)->s_qf_names[GRPQUOTA]) {
2001 path_release(&nd);
2002 return vfs_quota_on(sb, type, format_id, path);
2003 }
2004 /* Quotafile not of fs root? */
2005 if (nd.dentry->d_parent->d_inode != sb->s_root->d_inode)
2006 reiserfs_warning(sb,
2007 "reiserfs: Quota file not on filesystem root. "
2008 "Journalled quota will not work.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 path_release(&nd);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002010 return vfs_quota_on(sb, type, format_id, path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011}
2012
2013/* Read data from quotafile - avoid pagecache and such because we cannot afford
2014 * acquiring the locks... As quota files are never truncated and quota code
2015 * itself serializes the operations (and noone else should touch the files)
2016 * we don't have to be afraid of races */
2017static ssize_t reiserfs_quota_read(struct super_block *sb, int type, char *data,
2018 size_t len, loff_t off)
2019{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002020 struct inode *inode = sb_dqopt(sb)->files[type];
2021 unsigned long blk = off >> sb->s_blocksize_bits;
2022 int err = 0, offset = off & (sb->s_blocksize - 1), tocopy;
2023 size_t toread;
2024 struct buffer_head tmp_bh, *bh;
2025 loff_t i_size = i_size_read(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002027 if (off > i_size)
2028 return 0;
2029 if (off + len > i_size)
2030 len = i_size - off;
2031 toread = len;
2032 while (toread > 0) {
2033 tocopy =
2034 sb->s_blocksize - offset <
2035 toread ? sb->s_blocksize - offset : toread;
2036 tmp_bh.b_state = 0;
2037 /* Quota files are without tails so we can safely use this function */
2038 reiserfs_write_lock(sb);
2039 err = reiserfs_get_block(inode, blk, &tmp_bh, 0);
2040 reiserfs_write_unlock(sb);
2041 if (err)
2042 return err;
2043 if (!buffer_mapped(&tmp_bh)) /* A hole? */
2044 memset(data, 0, tocopy);
2045 else {
2046 bh = sb_bread(sb, tmp_bh.b_blocknr);
2047 if (!bh)
2048 return -EIO;
2049 memcpy(data, bh->b_data + offset, tocopy);
2050 brelse(bh);
2051 }
2052 offset = 0;
2053 toread -= tocopy;
2054 data += tocopy;
2055 blk++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002057 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058}
2059
2060/* Write to quotafile (we know the transaction is already started and has
2061 * enough credits) */
2062static ssize_t reiserfs_quota_write(struct super_block *sb, int type,
2063 const char *data, size_t len, loff_t off)
2064{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002065 struct inode *inode = sb_dqopt(sb)->files[type];
2066 unsigned long blk = off >> sb->s_blocksize_bits;
2067 int err = 0, offset = off & (sb->s_blocksize - 1), tocopy;
2068 int journal_quota = REISERFS_SB(sb)->s_qf_names[type] != NULL;
2069 size_t towrite = len;
2070 struct buffer_head tmp_bh, *bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071
Arjan van de Ven5c81a412006-07-03 00:25:20 -07002072 mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002073 while (towrite > 0) {
2074 tocopy = sb->s_blocksize - offset < towrite ?
2075 sb->s_blocksize - offset : towrite;
2076 tmp_bh.b_state = 0;
2077 err = reiserfs_get_block(inode, blk, &tmp_bh, GET_BLOCK_CREATE);
2078 if (err)
2079 goto out;
2080 if (offset || tocopy != sb->s_blocksize)
2081 bh = sb_bread(sb, tmp_bh.b_blocknr);
2082 else
2083 bh = sb_getblk(sb, tmp_bh.b_blocknr);
2084 if (!bh) {
2085 err = -EIO;
2086 goto out;
2087 }
2088 lock_buffer(bh);
2089 memcpy(bh->b_data + offset, data, tocopy);
2090 flush_dcache_page(bh->b_page);
2091 set_buffer_uptodate(bh);
2092 unlock_buffer(bh);
2093 reiserfs_prepare_for_journal(sb, bh, 1);
2094 journal_mark_dirty(current->journal_info, sb, bh);
2095 if (!journal_quota)
2096 reiserfs_add_ordered_list(inode, bh);
2097 brelse(bh);
2098 offset = 0;
2099 towrite -= tocopy;
2100 data += tocopy;
2101 blk++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002103 out:
2104 if (len == towrite)
2105 return err;
2106 if (inode->i_size < off + len - towrite)
2107 i_size_write(inode, off + len - towrite);
2108 inode->i_version++;
2109 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
2110 mark_inode_dirty(inode);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002111 mutex_unlock(&inode->i_mutex);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002112 return len - towrite;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113}
2114
2115#endif
2116
David Howells454e2392006-06-23 02:02:57 -07002117static int get_super_block(struct file_system_type *fs_type,
2118 int flags, const char *dev_name,
2119 void *data, struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120{
David Howells454e2392006-06-23 02:02:57 -07002121 return get_sb_bdev(fs_type, flags, dev_name, data, reiserfs_fill_super,
2122 mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123}
2124
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002125static int __init init_reiserfs_fs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126{
2127 int ret;
2128
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002129 if ((ret = init_inodecache())) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 return ret;
2131 }
2132
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002133 if ((ret = reiserfs_xattr_register_handlers()))
2134 goto failed_reiserfs_xattr_register_handlers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002136 reiserfs_proc_info_global_init();
2137 reiserfs_proc_register_global("version",
2138 reiserfs_global_version_in_proc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002140 ret = register_filesystem(&reiserfs_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141
2142 if (ret == 0) {
2143 return 0;
2144 }
2145
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002146 reiserfs_xattr_unregister_handlers();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002148 failed_reiserfs_xattr_register_handlers:
2149 reiserfs_proc_unregister_global("version");
2150 reiserfs_proc_info_global_done();
2151 destroy_inodecache();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152
2153 return ret;
2154}
2155
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002156static void __exit exit_reiserfs_fs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002158 reiserfs_xattr_unregister_handlers();
2159 reiserfs_proc_unregister_global("version");
2160 reiserfs_proc_info_global_done();
2161 unregister_filesystem(&reiserfs_fs_type);
2162 destroy_inodecache();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163}
2164
2165struct file_system_type reiserfs_fs_type = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002166 .owner = THIS_MODULE,
2167 .name = "reiserfs",
2168 .get_sb = get_super_block,
David Howellsedc666e2006-10-11 01:22:14 -07002169 .kill_sb = reiserfs_kill_sb,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002170 .fs_flags = FS_REQUIRES_DEV,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171};
2172
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002173MODULE_DESCRIPTION("ReiserFS journaled filesystem");
2174MODULE_AUTHOR("Hans Reiser <reiser@namesys.com>");
2175MODULE_LICENSE("GPL");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002177module_init(init_reiserfs_fs);
2178module_exit(exit_reiserfs_fs);