blob: edfd74f9f7e4db8d84c63135b331591f7be5d661 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/init.h>
22#include <linux/blkdev.h>
23#include <linux/buffer_head.h>
Christoph Hellwiga5694252007-07-17 04:04:28 -070024#include <linux/exportfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#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 Lameter4ba9b9d2007-10-16 23:25:51 -0700511static void init_once(struct kmem_cache * cachep, void *foo)
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 Lametera35afb82007-05-16 22:10:57 -0700515 INIT_LIST_HEAD(&ei->i_prealloc_list);
516 inode_init_once(&ei->vfs_inode);
Alexey Dobriyancfe14672006-09-29 02:00:00 -0700517#ifdef CONFIG_REISERFS_FS_POSIX_ACL
Christoph Lametera35afb82007-05-16 22:10:57 -0700518 ei->i_acl_access = NULL;
519 ei->i_acl_default = NULL;
Alexey Dobriyancfe14672006-09-29 02:00:00 -0700520#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521}
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523static int init_inodecache(void)
524{
525 reiserfs_inode_cachep = kmem_cache_create("reiser_inode_cache",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700526 sizeof(struct
527 reiserfs_inode_info),
Paul Jacksonfffb60f2006-03-24 03:16:06 -0800528 0, (SLAB_RECLAIM_ACCOUNT|
529 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +0900530 init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 if (reiserfs_inode_cachep == NULL)
532 return -ENOMEM;
533 return 0;
534}
535
536static void destroy_inodecache(void)
537{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -0700538 kmem_cache_destroy(reiserfs_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539}
540
541/* we don't mark inodes dirty, we just log them */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700542static void reiserfs_dirty_inode(struct inode *inode)
543{
544 struct reiserfs_transaction_handle th;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700546 int err = 0;
547 if (inode->i_sb->s_flags & MS_RDONLY) {
548 reiserfs_warning(inode->i_sb,
549 "clm-6006: writing inode %lu on readonly FS",
550 inode->i_ino);
551 return;
552 }
553 reiserfs_write_lock(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700555 /* this is really only used for atime updates, so they don't have
556 ** to be included in O_SYNC or fsync
557 */
558 err = journal_begin(&th, inode->i_sb, 1);
559 if (err) {
560 reiserfs_write_unlock(inode->i_sb);
561 return;
562 }
563 reiserfs_update_sd(&th, inode);
564 journal_end(&th, inode->i_sb, 1);
565 reiserfs_write_unlock(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566}
567
Alexey Dobriyancfe14672006-09-29 02:00:00 -0700568#ifdef CONFIG_REISERFS_FS_POSIX_ACL
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700569static void reiserfs_clear_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700571 struct posix_acl *acl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700573 acl = REISERFS_I(inode)->i_acl_access;
574 if (acl && !IS_ERR(acl))
575 posix_acl_release(acl);
576 REISERFS_I(inode)->i_acl_access = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700578 acl = REISERFS_I(inode)->i_acl_default;
579 if (acl && !IS_ERR(acl))
580 posix_acl_release(acl);
581 REISERFS_I(inode)->i_acl_default = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582}
Alexey Dobriyancfe14672006-09-29 02:00:00 -0700583#else
584#define reiserfs_clear_inode NULL
585#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
587#ifdef CONFIG_QUOTA
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700588static ssize_t reiserfs_quota_write(struct super_block *, int, const char *,
589 size_t, loff_t);
590static ssize_t reiserfs_quota_read(struct super_block *, int, char *, size_t,
591 loff_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592#endif
593
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800594static const struct super_operations reiserfs_sops = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700595 .alloc_inode = reiserfs_alloc_inode,
596 .destroy_inode = reiserfs_destroy_inode,
597 .write_inode = reiserfs_write_inode,
598 .dirty_inode = reiserfs_dirty_inode,
599 .delete_inode = reiserfs_delete_inode,
600 .clear_inode = reiserfs_clear_inode,
601 .put_super = reiserfs_put_super,
602 .write_super = reiserfs_write_super,
603 .sync_fs = reiserfs_sync_fs,
604 .write_super_lockfs = reiserfs_write_super_lockfs,
605 .unlockfs = reiserfs_unlockfs,
606 .statfs = reiserfs_statfs,
607 .remount_fs = reiserfs_remount,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608#ifdef CONFIG_QUOTA
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700609 .quota_read = reiserfs_quota_read,
610 .quota_write = reiserfs_quota_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611#endif
612};
613
614#ifdef CONFIG_QUOTA
615#define QTYPE2NAME(t) ((t)==USRQUOTA?"user":"group")
616
617static int reiserfs_dquot_initialize(struct inode *, int);
618static int reiserfs_dquot_drop(struct inode *);
619static int reiserfs_write_dquot(struct dquot *);
620static int reiserfs_acquire_dquot(struct dquot *);
621static int reiserfs_release_dquot(struct dquot *);
622static int reiserfs_mark_dquot_dirty(struct dquot *);
623static int reiserfs_write_info(struct super_block *, int);
624static int reiserfs_quota_on(struct super_block *, int, int, char *);
625
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700626static struct dquot_operations reiserfs_quota_operations = {
627 .initialize = reiserfs_dquot_initialize,
628 .drop = reiserfs_dquot_drop,
629 .alloc_space = dquot_alloc_space,
630 .alloc_inode = dquot_alloc_inode,
631 .free_space = dquot_free_space,
632 .free_inode = dquot_free_inode,
633 .transfer = dquot_transfer,
634 .write_dquot = reiserfs_write_dquot,
635 .acquire_dquot = reiserfs_acquire_dquot,
636 .release_dquot = reiserfs_release_dquot,
637 .mark_dirty = reiserfs_mark_dquot_dirty,
638 .write_info = reiserfs_write_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639};
640
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700641static struct quotactl_ops reiserfs_qctl_operations = {
642 .quota_on = reiserfs_quota_on,
643 .quota_off = vfs_quota_off,
644 .quota_sync = vfs_quota_sync,
645 .get_info = vfs_get_dqinfo,
646 .set_info = vfs_set_dqinfo,
647 .get_dqblk = vfs_get_dqblk,
648 .set_dqblk = vfs_set_dqblk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649};
650#endif
651
652static struct export_operations reiserfs_export_ops = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700653 .encode_fh = reiserfs_encode_fh,
654 .decode_fh = reiserfs_decode_fh,
655 .get_parent = reiserfs_get_parent,
656 .get_dentry = reiserfs_get_dentry,
657};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
659/* this struct is used in reiserfs_getopt () for containing the value for those
660 mount options that have values rather than being toggles. */
661typedef struct {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700662 char *value;
663 int setmask; /* bitmask which is to set on mount_options bitmask when this
664 value is found, 0 is no bits are to be changed. */
665 int clrmask; /* bitmask which is to clear on mount_options bitmask when this
666 value is found, 0 is no bits are to be changed. This is
667 applied BEFORE setmask */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668} arg_desc_t;
669
670/* Set this bit in arg_required to allow empty arguments */
671#define REISERFS_OPT_ALLOWEMPTY 31
672
673/* this struct is used in reiserfs_getopt() for describing the set of reiserfs
674 mount options */
675typedef struct {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700676 char *option_name;
677 int arg_required; /* 0 if argument is not required, not 0 otherwise */
678 const arg_desc_t *values; /* list of values accepted by an option */
679 int setmask; /* bitmask which is to set on mount_options bitmask when this
680 value is found, 0 is no bits are to be changed. */
681 int clrmask; /* bitmask which is to clear on mount_options bitmask when this
682 value is found, 0 is no bits are to be changed. This is
683 applied BEFORE setmask */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684} opt_desc_t;
685
686/* possible values for -o data= */
687static const arg_desc_t logging_mode[] = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700688 {"ordered", 1 << REISERFS_DATA_ORDERED,
689 (1 << REISERFS_DATA_LOG | 1 << REISERFS_DATA_WRITEBACK)},
690 {"journal", 1 << REISERFS_DATA_LOG,
691 (1 << REISERFS_DATA_ORDERED | 1 << REISERFS_DATA_WRITEBACK)},
692 {"writeback", 1 << REISERFS_DATA_WRITEBACK,
693 (1 << REISERFS_DATA_ORDERED | 1 << REISERFS_DATA_LOG)},
Vladimir V. Savelievcd02b962006-03-25 03:07:15 -0800694 {.value = NULL}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695};
696
697/* possible values for -o barrier= */
698static const arg_desc_t barrier_mode[] = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700699 {"none", 1 << REISERFS_BARRIER_NONE, 1 << REISERFS_BARRIER_FLUSH},
700 {"flush", 1 << REISERFS_BARRIER_FLUSH, 1 << REISERFS_BARRIER_NONE},
Vladimir V. Savelievcd02b962006-03-25 03:07:15 -0800701 {.value = NULL}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702};
703
704/* possible values for "-o block-allocator=" and bits which are to be set in
705 s_mount_opt of reiserfs specific part of in-core super block */
706static const arg_desc_t balloc[] = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700707 {"noborder", 1 << REISERFS_NO_BORDER, 0},
708 {"border", 0, 1 << REISERFS_NO_BORDER},
709 {"no_unhashed_relocation", 1 << REISERFS_NO_UNHASHED_RELOCATION, 0},
710 {"hashed_relocation", 1 << REISERFS_HASHED_RELOCATION, 0},
711 {"test4", 1 << REISERFS_TEST4, 0},
712 {"notest4", 0, 1 << REISERFS_TEST4},
713 {NULL, 0, 0}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714};
715
716static const arg_desc_t tails[] = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700717 {"on", 1 << REISERFS_LARGETAIL, 1 << REISERFS_SMALLTAIL},
718 {"off", 0, (1 << REISERFS_LARGETAIL) | (1 << REISERFS_SMALLTAIL)},
719 {"small", 1 << REISERFS_SMALLTAIL, 1 << REISERFS_LARGETAIL},
720 {NULL, 0, 0}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721};
722
723static const arg_desc_t error_actions[] = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700724 {"panic", 1 << REISERFS_ERROR_PANIC,
725 (1 << REISERFS_ERROR_RO | 1 << REISERFS_ERROR_CONTINUE)},
726 {"ro-remount", 1 << REISERFS_ERROR_RO,
727 (1 << REISERFS_ERROR_PANIC | 1 << REISERFS_ERROR_CONTINUE)},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728#ifdef REISERFS_JOURNAL_ERROR_ALLOWS_NO_LOG
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700729 {"continue", 1 << REISERFS_ERROR_CONTINUE,
730 (1 << REISERFS_ERROR_PANIC | 1 << REISERFS_ERROR_RO)},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700732 {NULL, 0, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733};
734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735/* proceed only one option from a list *cur - string containing of mount options
736 opts - array of options which are accepted
737 opt_arg - if option is found and requires an argument and if it is specifed
738 in the input - pointer to the argument is stored here
739 bit_flags - if option requires to set a certain bit - it is set here
740 return -1 if unknown option is found, opt->arg_required otherwise */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700741static int reiserfs_getopt(struct super_block *s, char **cur, opt_desc_t * opts,
742 char **opt_arg, unsigned long *bit_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700744 char *p;
745 /* foo=bar,
746 ^ ^ ^
747 | | +-- option_end
748 | +-- arg_start
749 +-- option_start
750 */
751 const opt_desc_t *opt;
752 const arg_desc_t *arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700754 p = *cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700756 /* assume argument cannot contain commas */
757 *cur = strchr(p, ',');
758 if (*cur) {
759 *(*cur) = '\0';
760 (*cur)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700763 if (!strncmp(p, "alloc=", 6)) {
764 /* Ugly special case, probably we should redo options parser so that
765 it can understand several arguments for some options, also so that
766 it can fill several bitfields with option values. */
767 if (reiserfs_parse_alloc_options(s, p + 6)) {
768 return -1;
769 } else {
770 return 0;
771 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700773
774 /* for every option in the list */
775 for (opt = opts; opt->option_name; opt++) {
776 if (!strncmp(p, opt->option_name, strlen(opt->option_name))) {
777 if (bit_flags) {
778 if (opt->clrmask ==
779 (1 << REISERFS_UNSUPPORTED_OPT))
780 reiserfs_warning(s, "%s not supported.",
781 p);
782 else
783 *bit_flags &= ~opt->clrmask;
784 if (opt->setmask ==
785 (1 << REISERFS_UNSUPPORTED_OPT))
786 reiserfs_warning(s, "%s not supported.",
787 p);
788 else
789 *bit_flags |= opt->setmask;
790 }
791 break;
792 }
793 }
794 if (!opt->option_name) {
795 reiserfs_warning(s, "unknown mount option \"%s\"", p);
796 return -1;
797 }
798
799 p += strlen(opt->option_name);
800 switch (*p) {
801 case '=':
802 if (!opt->arg_required) {
803 reiserfs_warning(s,
804 "the option \"%s\" does not require an argument",
805 opt->option_name);
806 return -1;
807 }
808 break;
809
810 case 0:
811 if (opt->arg_required) {
812 reiserfs_warning(s,
813 "the option \"%s\" requires an argument",
814 opt->option_name);
815 return -1;
816 }
817 break;
818 default:
819 reiserfs_warning(s, "head of option \"%s\" is only correct",
820 opt->option_name);
821 return -1;
822 }
823
824 /* move to the argument, or to next option if argument is not required */
825 p++;
826
827 if (opt->arg_required
828 && !(opt->arg_required & (1 << REISERFS_OPT_ALLOWEMPTY))
829 && !strlen(p)) {
830 /* this catches "option=," if not allowed */
831 reiserfs_warning(s, "empty argument for \"%s\"",
832 opt->option_name);
833 return -1;
834 }
835
836 if (!opt->values) {
837 /* *=NULLopt_arg contains pointer to argument */
838 *opt_arg = p;
839 return opt->arg_required & ~(1 << REISERFS_OPT_ALLOWEMPTY);
840 }
841
842 /* values possible for this option are listed in opt->values */
843 for (arg = opt->values; arg->value; arg++) {
844 if (!strcmp(p, arg->value)) {
845 if (bit_flags) {
846 *bit_flags &= ~arg->clrmask;
847 *bit_flags |= arg->setmask;
848 }
849 return opt->arg_required;
850 }
851 }
852
853 reiserfs_warning(s, "bad value \"%s\" for option \"%s\"", p,
854 opt->option_name);
855 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856}
857
858/* returns 0 if something is wrong in option string, 1 - otherwise */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700859static int reiserfs_parse_options(struct super_block *s, char *options, /* string given via mount's -o */
860 unsigned long *mount_options,
861 /* after the parsing phase, contains the
862 collection of bitflags defining what
863 mount options were selected. */
864 unsigned long *blocks, /* strtol-ed from NNN of resize=NNN */
865 char **jdev_name,
866 unsigned int *commit_max_age)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700868 int c;
869 char *arg = NULL;
870 char *pos;
871 opt_desc_t opts[] = {
872 /* Compatibility stuff, so that -o notail for old setups still work */
873 {"tails",.arg_required = 't',.values = tails},
874 {"notail",.clrmask =
875 (1 << REISERFS_LARGETAIL) | (1 << REISERFS_SMALLTAIL)},
876 {"conv",.setmask = 1 << REISERFS_CONVERT},
877 {"attrs",.setmask = 1 << REISERFS_ATTRS},
878 {"noattrs",.clrmask = 1 << REISERFS_ATTRS},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879#ifdef CONFIG_REISERFS_FS_XATTR
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700880 {"user_xattr",.setmask = 1 << REISERFS_XATTRS_USER},
881 {"nouser_xattr",.clrmask = 1 << REISERFS_XATTRS_USER},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882#else
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700883 {"user_xattr",.setmask = 1 << REISERFS_UNSUPPORTED_OPT},
884 {"nouser_xattr",.clrmask = 1 << REISERFS_UNSUPPORTED_OPT},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885#endif
886#ifdef CONFIG_REISERFS_FS_POSIX_ACL
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700887 {"acl",.setmask = 1 << REISERFS_POSIXACL},
888 {"noacl",.clrmask = 1 << REISERFS_POSIXACL},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889#else
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700890 {"acl",.setmask = 1 << REISERFS_UNSUPPORTED_OPT},
891 {"noacl",.clrmask = 1 << REISERFS_UNSUPPORTED_OPT},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892#endif
Vladimir V. Savelievcd02b962006-03-25 03:07:15 -0800893 {.option_name = "nolog"},
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700894 {"replayonly",.setmask = 1 << REPLAYONLY},
895 {"block-allocator",.arg_required = 'a',.values = balloc},
896 {"data",.arg_required = 'd',.values = logging_mode},
897 {"barrier",.arg_required = 'b',.values = barrier_mode},
898 {"resize",.arg_required = 'r',.values = NULL},
899 {"jdev",.arg_required = 'j',.values = NULL},
900 {"nolargeio",.arg_required = 'w',.values = NULL},
901 {"commit",.arg_required = 'c',.values = NULL},
902 {"usrquota",.setmask = 1 << REISERFS_QUOTA},
903 {"grpquota",.setmask = 1 << REISERFS_QUOTA},
904 {"noquota",.clrmask = 1 << REISERFS_QUOTA},
905 {"errors",.arg_required = 'e',.values = error_actions},
906 {"usrjquota",.arg_required =
907 'u' | (1 << REISERFS_OPT_ALLOWEMPTY),.values = NULL},
908 {"grpjquota",.arg_required =
909 'g' | (1 << REISERFS_OPT_ALLOWEMPTY),.values = NULL},
910 {"jqfmt",.arg_required = 'f',.values = NULL},
Vladimir V. Savelievcd02b962006-03-25 03:07:15 -0800911 {.option_name = NULL}
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700912 };
913
914 *blocks = 0;
915 if (!options || !*options)
916 /* use default configuration: create tails, journaling on, no
917 conversion to newest format */
918 return 1;
919
920 for (pos = options; pos;) {
921 c = reiserfs_getopt(s, &pos, opts, &arg, mount_options);
922 if (c == -1)
923 /* wrong option is given */
924 return 0;
925
926 if (c == 'r') {
927 char *p;
928
929 p = NULL;
930 /* "resize=NNN" or "resize=auto" */
931
932 if (!strcmp(arg, "auto")) {
933 /* From JFS code, to auto-get the size. */
934 *blocks =
935 s->s_bdev->bd_inode->i_size >> s->
936 s_blocksize_bits;
937 } else {
938 *blocks = simple_strtoul(arg, &p, 0);
939 if (*p != '\0') {
940 /* NNN does not look like a number */
941 reiserfs_warning(s,
942 "reiserfs_parse_options: bad value %s",
943 arg);
944 return 0;
945 }
946 }
947 }
948
949 if (c == 'c') {
950 char *p = NULL;
951 unsigned long val = simple_strtoul(arg, &p, 0);
952 /* commit=NNN (time in seconds) */
953 if (*p != '\0' || val >= (unsigned int)-1) {
954 reiserfs_warning(s,
955 "reiserfs_parse_options: bad value %s",
956 arg);
957 return 0;
958 }
959 *commit_max_age = (unsigned int)val;
960 }
961
962 if (c == 'w') {
Adrian Bunk36b756f2006-09-27 01:50:50 -0700963 reiserfs_warning(s, "reiserfs: nolargeio option is no longer supported");
964 return 0;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700965 }
966
967 if (c == 'j') {
968 if (arg && *arg && jdev_name) {
969 if (*jdev_name) { //Hm, already assigned?
970 reiserfs_warning(s,
971 "reiserfs_parse_options: journal device was already specified to be %s",
972 *jdev_name);
973 return 0;
974 }
975 *jdev_name = arg;
976 }
977 }
978#ifdef CONFIG_QUOTA
979 if (c == 'u' || c == 'g') {
980 int qtype = c == 'u' ? USRQUOTA : GRPQUOTA;
981
982 if (sb_any_quota_enabled(s)) {
983 reiserfs_warning(s,
984 "reiserfs_parse_options: cannot change journalled quota options when quota turned on.");
985 return 0;
986 }
987 if (*arg) { /* Some filename specified? */
988 if (REISERFS_SB(s)->s_qf_names[qtype]
989 && strcmp(REISERFS_SB(s)->s_qf_names[qtype],
990 arg)) {
991 reiserfs_warning(s,
992 "reiserfs_parse_options: %s quota file already specified.",
993 QTYPE2NAME(qtype));
994 return 0;
995 }
996 if (strchr(arg, '/')) {
997 reiserfs_warning(s,
998 "reiserfs_parse_options: quotafile must be on filesystem root.");
999 return 0;
1000 }
1001 REISERFS_SB(s)->s_qf_names[qtype] =
1002 kmalloc(strlen(arg) + 1, GFP_KERNEL);
1003 if (!REISERFS_SB(s)->s_qf_names[qtype]) {
1004 reiserfs_warning(s,
1005 "reiserfs_parse_options: not enough memory for storing quotafile name.");
1006 return 0;
1007 }
1008 strcpy(REISERFS_SB(s)->s_qf_names[qtype], arg);
1009 *mount_options |= 1 << REISERFS_QUOTA;
1010 } else {
James Lamanna833d3042005-10-30 15:00:16 -08001011 kfree(REISERFS_SB(s)->s_qf_names[qtype]);
1012 REISERFS_SB(s)->s_qf_names[qtype] = NULL;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001013 }
1014 }
1015 if (c == 'f') {
1016 if (!strcmp(arg, "vfsold"))
1017 REISERFS_SB(s)->s_jquota_fmt = QFMT_VFS_OLD;
1018 else if (!strcmp(arg, "vfsv0"))
1019 REISERFS_SB(s)->s_jquota_fmt = QFMT_VFS_V0;
1020 else {
1021 reiserfs_warning(s,
1022 "reiserfs_parse_options: unknown quota format specified.");
1023 return 0;
1024 }
1025 }
1026#else
1027 if (c == 'u' || c == 'g' || c == 'f') {
1028 reiserfs_warning(s,
1029 "reiserfs_parse_options: journalled quota options not supported.");
1030 return 0;
1031 }
1032#endif
1033 }
1034
1035#ifdef CONFIG_QUOTA
1036 if (!REISERFS_SB(s)->s_jquota_fmt
1037 && (REISERFS_SB(s)->s_qf_names[USRQUOTA]
1038 || REISERFS_SB(s)->s_qf_names[GRPQUOTA])) {
1039 reiserfs_warning(s,
1040 "reiserfs_parse_options: journalled quota format not specified.");
1041 return 0;
1042 }
1043 /* This checking is not precise wrt the quota type but for our purposes it is sufficient */
1044 if (!(*mount_options & (1 << REISERFS_QUOTA))
1045 && sb_any_quota_enabled(s)) {
1046 reiserfs_warning(s,
1047 "reiserfs_parse_options: quota options must be present when quota is turned on.");
1048 return 0;
1049 }
1050#endif
1051
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053}
1054
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001055static void switch_data_mode(struct super_block *s, unsigned long mode)
1056{
1057 REISERFS_SB(s)->s_mount_opt &= ~((1 << REISERFS_DATA_LOG) |
1058 (1 << REISERFS_DATA_ORDERED) |
1059 (1 << REISERFS_DATA_WRITEBACK));
1060 REISERFS_SB(s)->s_mount_opt |= (1 << mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061}
1062
1063static void handle_data_mode(struct super_block *s, unsigned long mount_options)
1064{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001065 if (mount_options & (1 << REISERFS_DATA_LOG)) {
1066 if (!reiserfs_data_log(s)) {
1067 switch_data_mode(s, REISERFS_DATA_LOG);
1068 reiserfs_info(s, "switching to journaled data mode\n");
1069 }
1070 } else if (mount_options & (1 << REISERFS_DATA_ORDERED)) {
1071 if (!reiserfs_data_ordered(s)) {
1072 switch_data_mode(s, REISERFS_DATA_ORDERED);
1073 reiserfs_info(s, "switching to ordered data mode\n");
1074 }
1075 } else if (mount_options & (1 << REISERFS_DATA_WRITEBACK)) {
1076 if (!reiserfs_data_writeback(s)) {
1077 switch_data_mode(s, REISERFS_DATA_WRITEBACK);
1078 reiserfs_info(s, "switching to writeback data mode\n");
1079 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081}
1082
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001083static void handle_barrier_mode(struct super_block *s, unsigned long bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001085 int flush = (1 << REISERFS_BARRIER_FLUSH);
1086 int none = (1 << REISERFS_BARRIER_NONE);
1087 int all_barrier = flush | none;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001089 if (bits & all_barrier) {
1090 REISERFS_SB(s)->s_mount_opt &= ~all_barrier;
1091 if (bits & flush) {
1092 REISERFS_SB(s)->s_mount_opt |= flush;
1093 printk("reiserfs: enabling write barrier flush mode\n");
1094 } else if (bits & none) {
1095 REISERFS_SB(s)->s_mount_opt |= none;
1096 printk("reiserfs: write barriers turned off\n");
1097 }
1098 }
1099}
1100
1101static void handle_attrs(struct super_block *s)
1102{
1103 struct reiserfs_super_block *rs = SB_DISK_SUPER_BLOCK(s);
1104
1105 if (reiserfs_attrs(s)) {
1106 if (old_format_only(s)) {
1107 reiserfs_warning(s,
1108 "reiserfs: cannot support attributes on 3.5.x disk format");
1109 REISERFS_SB(s)->s_mount_opt &= ~(1 << REISERFS_ATTRS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 return;
1111 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001112 if (!(le32_to_cpu(rs->s_flags) & reiserfs_attrs_cleared)) {
1113 reiserfs_warning(s,
1114 "reiserfs: cannot support attributes until flag is set in super-block");
1115 REISERFS_SB(s)->s_mount_opt &= ~(1 << REISERFS_ATTRS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 }
1117 }
1118}
1119
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001120static int reiserfs_remount(struct super_block *s, int *mount_flags, char *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001122 struct reiserfs_super_block *rs;
1123 struct reiserfs_transaction_handle th;
1124 unsigned long blocks;
1125 unsigned long mount_options = REISERFS_SB(s)->s_mount_opt;
1126 unsigned long safe_mask = 0;
1127 unsigned int commit_max_age = (unsigned int)-1;
1128 struct reiserfs_journal *journal = SB_JOURNAL(s);
1129 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130#ifdef CONFIG_QUOTA
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001131 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132#endif
1133
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001134 rs = SB_DISK_SUPER_BLOCK(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001136 if (!reiserfs_parse_options
1137 (s, arg, &mount_options, &blocks, NULL, &commit_max_age)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138#ifdef CONFIG_QUOTA
James Lamanna833d3042005-10-30 15:00:16 -08001139 for (i = 0; i < MAXQUOTAS; i++) {
1140 kfree(REISERFS_SB(s)->s_qf_names[i]);
1141 REISERFS_SB(s)->s_qf_names[i] = NULL;
1142 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001143#endif
1144 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001147 handle_attrs(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001149 /* Add options that are safe here */
1150 safe_mask |= 1 << REISERFS_SMALLTAIL;
1151 safe_mask |= 1 << REISERFS_LARGETAIL;
1152 safe_mask |= 1 << REISERFS_NO_BORDER;
1153 safe_mask |= 1 << REISERFS_NO_UNHASHED_RELOCATION;
1154 safe_mask |= 1 << REISERFS_HASHED_RELOCATION;
1155 safe_mask |= 1 << REISERFS_TEST4;
1156 safe_mask |= 1 << REISERFS_ATTRS;
1157 safe_mask |= 1 << REISERFS_XATTRS_USER;
1158 safe_mask |= 1 << REISERFS_POSIXACL;
1159 safe_mask |= 1 << REISERFS_BARRIER_FLUSH;
1160 safe_mask |= 1 << REISERFS_BARRIER_NONE;
1161 safe_mask |= 1 << REISERFS_ERROR_RO;
1162 safe_mask |= 1 << REISERFS_ERROR_CONTINUE;
1163 safe_mask |= 1 << REISERFS_ERROR_PANIC;
1164 safe_mask |= 1 << REISERFS_QUOTA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001166 /* Update the bitmask, taking care to keep
1167 * the bits we're not allowed to change here */
1168 REISERFS_SB(s)->s_mount_opt =
1169 (REISERFS_SB(s)->
1170 s_mount_opt & ~safe_mask) | (mount_options & safe_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001172 if (commit_max_age != 0 && commit_max_age != (unsigned int)-1) {
1173 journal->j_max_commit_age = commit_max_age;
1174 journal->j_max_trans_age = commit_max_age;
1175 } else if (commit_max_age == 0) {
1176 /* 0 means restore defaults. */
1177 journal->j_max_commit_age = journal->j_default_max_commit_age;
1178 journal->j_max_trans_age = JOURNAL_MAX_TRANS_AGE;
1179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001181 if (blocks) {
1182 int rc = reiserfs_resize(s, blocks);
1183 if (rc != 0)
1184 return rc;
1185 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001187 if (*mount_flags & MS_RDONLY) {
1188 reiserfs_xattr_init(s, *mount_flags);
1189 /* remount read-only */
1190 if (s->s_flags & MS_RDONLY)
1191 /* it is read-only already */
1192 return 0;
1193 /* try to remount file system with read-only permissions */
1194 if (sb_umount_state(rs) == REISERFS_VALID_FS
1195 || REISERFS_SB(s)->s_mount_state != REISERFS_VALID_FS) {
1196 return 0;
1197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001199 err = journal_begin(&th, s, 10);
1200 if (err)
1201 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001203 /* Mounting a rw partition read-only. */
1204 reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s), 1);
1205 set_sb_umount_state(rs, REISERFS_SB(s)->s_mount_state);
1206 journal_mark_dirty(&th, s, SB_BUFFER_WITH_SB(s));
1207 } else {
1208 /* remount read-write */
1209 if (!(s->s_flags & MS_RDONLY)) {
1210 reiserfs_xattr_init(s, *mount_flags);
1211 return 0; /* We are read-write already */
1212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001214 if (reiserfs_is_journal_aborted(journal))
1215 return journal->j_errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001217 handle_data_mode(s, mount_options);
1218 handle_barrier_mode(s, mount_options);
1219 REISERFS_SB(s)->s_mount_state = sb_umount_state(rs);
1220 s->s_flags &= ~MS_RDONLY; /* now it is safe to call journal_begin */
1221 err = journal_begin(&th, s, 10);
1222 if (err)
1223 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001225 /* Mount a partition which is read-only, read-write */
1226 reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s), 1);
1227 REISERFS_SB(s)->s_mount_state = sb_umount_state(rs);
1228 s->s_flags &= ~MS_RDONLY;
1229 set_sb_umount_state(rs, REISERFS_ERROR_FS);
1230 /* mark_buffer_dirty (SB_BUFFER_WITH_SB (s), 1); */
1231 journal_mark_dirty(&th, s, SB_BUFFER_WITH_SB(s));
1232 REISERFS_SB(s)->s_mount_state = REISERFS_VALID_FS;
1233 }
1234 /* this will force a full flush of all journal lists */
1235 SB_JOURNAL(s)->j_must_wait = 1;
1236 err = journal_end(&th, s, 10);
1237 if (err)
1238 return err;
1239 s->s_dirt = 0;
1240
1241 if (!(*mount_flags & MS_RDONLY)) {
1242 finish_unfinished(s);
1243 reiserfs_xattr_init(s, *mount_flags);
1244 }
1245
1246 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247}
1248
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001249static int read_super_block(struct super_block *s, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001251 struct buffer_head *bh;
1252 struct reiserfs_super_block *rs;
1253 int fs_blocksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001255 bh = sb_bread(s, offset / s->s_blocksize);
1256 if (!bh) {
1257 reiserfs_warning(s, "sh-2006: read_super_block: "
1258 "bread failed (dev %s, block %lu, size %lu)",
1259 reiserfs_bdevname(s), offset / s->s_blocksize,
1260 s->s_blocksize);
1261 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001264 rs = (struct reiserfs_super_block *)bh->b_data;
1265 if (!is_any_reiserfs_magic_string(rs)) {
1266 brelse(bh);
1267 return 1;
1268 }
1269 //
1270 // ok, reiserfs signature (old or new) found in at the given offset
1271 //
1272 fs_blocksize = sb_blocksize(rs);
1273 brelse(bh);
1274 sb_set_blocksize(s, fs_blocksize);
1275
1276 bh = sb_bread(s, offset / s->s_blocksize);
1277 if (!bh) {
1278 reiserfs_warning(s, "sh-2007: read_super_block: "
1279 "bread failed (dev %s, block %lu, size %lu)\n",
1280 reiserfs_bdevname(s), offset / s->s_blocksize,
1281 s->s_blocksize);
1282 return 1;
1283 }
1284
1285 rs = (struct reiserfs_super_block *)bh->b_data;
1286 if (sb_blocksize(rs) != s->s_blocksize) {
1287 reiserfs_warning(s, "sh-2011: read_super_block: "
1288 "can't find a reiserfs filesystem on (dev %s, block %Lu, size %lu)\n",
1289 reiserfs_bdevname(s),
1290 (unsigned long long)bh->b_blocknr,
1291 s->s_blocksize);
1292 brelse(bh);
1293 return 1;
1294 }
1295
1296 if (rs->s_v1.s_root_block == cpu_to_le32(-1)) {
1297 brelse(bh);
1298 reiserfs_warning(s,
1299 "Unfinished reiserfsck --rebuild-tree run detected. Please run\n"
1300 "reiserfsck --rebuild-tree and wait for a completion. If that fails\n"
1301 "get newer reiserfsprogs package");
1302 return 1;
1303 }
1304
1305 SB_BUFFER_WITH_SB(s) = bh;
1306 SB_DISK_SUPER_BLOCK(s) = rs;
1307
1308 if (is_reiserfs_jr(rs)) {
1309 /* magic is of non-standard journal filesystem, look at s_version to
1310 find which format is in use */
1311 if (sb_version(rs) == REISERFS_VERSION_2)
1312 reiserfs_warning(s,
1313 "read_super_block: found reiserfs format \"3.6\""
1314 " with non-standard journal");
1315 else if (sb_version(rs) == REISERFS_VERSION_1)
1316 reiserfs_warning(s,
1317 "read_super_block: found reiserfs format \"3.5\""
1318 " with non-standard journal");
1319 else {
1320 reiserfs_warning(s,
1321 "sh-2012: read_super_block: found unknown "
1322 "format \"%u\" of reiserfs with non-standard magic",
1323 sb_version(rs));
1324 return 1;
1325 }
1326 } else
1327 /* s_version of standard format may contain incorrect information,
1328 so we just look at the magic string */
1329 reiserfs_info(s,
1330 "found reiserfs format \"%s\" with standard journal\n",
1331 is_reiserfs_3_5(rs) ? "3.5" : "3.6");
1332
1333 s->s_op = &reiserfs_sops;
1334 s->s_export_op = &reiserfs_export_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335#ifdef CONFIG_QUOTA
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001336 s->s_qcop = &reiserfs_qctl_operations;
1337 s->dq_op = &reiserfs_quota_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338#endif
1339
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001340 /* new format is limited by the 32 bit wide i_blocks field, want to
1341 ** be one full block below that.
1342 */
1343 s->s_maxbytes = (512LL << 32) - s->s_blocksize;
1344 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345}
1346
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347/* after journal replay, reread all bitmap and super blocks */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001348static int reread_meta_blocks(struct super_block *s)
1349{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001350 ll_rw_block(READ, 1, &(SB_BUFFER_WITH_SB(s)));
1351 wait_on_buffer(SB_BUFFER_WITH_SB(s));
1352 if (!buffer_uptodate(SB_BUFFER_WITH_SB(s))) {
1353 reiserfs_warning(s,
1354 "reread_meta_blocks, error reading the super");
1355 return 1;
1356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001358 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359}
1360
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361/////////////////////////////////////////////////////
1362// hash detection stuff
1363
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364// if root directory is empty - we set default - Yura's - hash and
1365// warn about it
1366// FIXME: we look for only one name in a directory. If tea and yura
1367// bith have the same value - we ask user to send report to the
1368// mailing list
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001369static __u32 find_hash_out(struct super_block *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001371 int retval;
1372 struct inode *inode;
1373 struct cpu_key key;
1374 INITIALIZE_PATH(path);
1375 struct reiserfs_dir_entry de;
1376 __u32 hash = DEFAULT_HASH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001378 inode = s->s_root->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001380 do { // Some serious "goto"-hater was there ;)
1381 u32 teahash, r5hash, yurahash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001383 make_cpu_key(&key, inode, ~0, TYPE_DIRENTRY, 3);
1384 retval = search_by_entry_key(s, &key, &path, &de);
1385 if (retval == IO_ERROR) {
1386 pathrelse(&path);
1387 return UNSET_HASH;
1388 }
1389 if (retval == NAME_NOT_FOUND)
1390 de.de_entry_num--;
1391 set_de_name_and_namelen(&de);
1392 if (deh_offset(&(de.de_deh[de.de_entry_num])) == DOT_DOT_OFFSET) {
1393 /* allow override in this case */
1394 if (reiserfs_rupasov_hash(s)) {
1395 hash = YURA_HASH;
1396 }
1397 reiserfs_warning(s, "FS seems to be empty, autodetect "
1398 "is using the default hash");
1399 break;
1400 }
1401 r5hash = GET_HASH_VALUE(r5_hash(de.de_name, de.de_namelen));
1402 teahash = GET_HASH_VALUE(keyed_hash(de.de_name, de.de_namelen));
1403 yurahash = GET_HASH_VALUE(yura_hash(de.de_name, de.de_namelen));
1404 if (((teahash == r5hash)
1405 &&
1406 (GET_HASH_VALUE(deh_offset(&(de.de_deh[de.de_entry_num])))
1407 == r5hash)) || ((teahash == yurahash)
1408 && (yurahash ==
1409 GET_HASH_VALUE(deh_offset
1410 (&
1411 (de.
1412 de_deh[de.
1413 de_entry_num])))))
1414 || ((r5hash == yurahash)
1415 && (yurahash ==
1416 GET_HASH_VALUE(deh_offset
1417 (&(de.de_deh[de.de_entry_num])))))) {
1418 reiserfs_warning(s,
1419 "Unable to automatically detect hash function. "
1420 "Please mount with -o hash={tea,rupasov,r5}",
1421 reiserfs_bdevname(s));
1422 hash = UNSET_HASH;
1423 break;
1424 }
1425 if (GET_HASH_VALUE(deh_offset(&(de.de_deh[de.de_entry_num]))) ==
1426 yurahash)
1427 hash = YURA_HASH;
1428 else if (GET_HASH_VALUE
1429 (deh_offset(&(de.de_deh[de.de_entry_num]))) == teahash)
1430 hash = TEA_HASH;
1431 else if (GET_HASH_VALUE
1432 (deh_offset(&(de.de_deh[de.de_entry_num]))) == r5hash)
1433 hash = R5_HASH;
1434 else {
1435 reiserfs_warning(s, "Unrecognised hash function");
1436 hash = UNSET_HASH;
1437 }
1438 } while (0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001440 pathrelse(&path);
1441 return hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442}
1443
1444// finds out which hash names are sorted with
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001445static int what_hash(struct super_block *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001447 __u32 code;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001449 code = sb_hash_function_code(SB_DISK_SUPER_BLOCK(s));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001451 /* reiserfs_hash_detect() == true if any of the hash mount options
1452 ** were used. We must check them to make sure the user isn't
1453 ** using a bad hash value
1454 */
1455 if (code == UNSET_HASH || reiserfs_hash_detect(s))
1456 code = find_hash_out(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001458 if (code != UNSET_HASH && reiserfs_hash_detect(s)) {
1459 /* detection has found the hash, and we must check against the
1460 ** mount options
1461 */
1462 if (reiserfs_rupasov_hash(s) && code != YURA_HASH) {
1463 reiserfs_warning(s, "Error, %s hash detected, "
1464 "unable to force rupasov hash",
1465 reiserfs_hashname(code));
1466 code = UNSET_HASH;
1467 } else if (reiserfs_tea_hash(s) && code != TEA_HASH) {
1468 reiserfs_warning(s, "Error, %s hash detected, "
1469 "unable to force tea hash",
1470 reiserfs_hashname(code));
1471 code = UNSET_HASH;
1472 } else if (reiserfs_r5_hash(s) && code != R5_HASH) {
1473 reiserfs_warning(s, "Error, %s hash detected, "
1474 "unable to force r5 hash",
1475 reiserfs_hashname(code));
1476 code = UNSET_HASH;
1477 }
1478 } else {
1479 /* find_hash_out was not called or could not determine the hash */
1480 if (reiserfs_rupasov_hash(s)) {
1481 code = YURA_HASH;
1482 } else if (reiserfs_tea_hash(s)) {
1483 code = TEA_HASH;
1484 } else if (reiserfs_r5_hash(s)) {
1485 code = R5_HASH;
1486 }
1487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001489 /* if we are mounted RW, and we have a new valid hash code, update
1490 ** the super
1491 */
1492 if (code != UNSET_HASH &&
1493 !(s->s_flags & MS_RDONLY) &&
1494 code != sb_hash_function_code(SB_DISK_SUPER_BLOCK(s))) {
1495 set_sb_hash_function_code(SB_DISK_SUPER_BLOCK(s), code);
1496 }
1497 return code;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498}
1499
1500// return pointer to appropriate function
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001501static hashf_t hash_function(struct super_block *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001503 switch (what_hash(s)) {
1504 case TEA_HASH:
1505 reiserfs_info(s, "Using tea hash to sort names\n");
1506 return keyed_hash;
1507 case YURA_HASH:
1508 reiserfs_info(s, "Using rupasov hash to sort names\n");
1509 return yura_hash;
1510 case R5_HASH:
1511 reiserfs_info(s, "Using r5 hash to sort names\n");
1512 return r5_hash;
1513 }
1514 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515}
1516
1517// this is used to set up correct value for old partitions
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001518static int function2code(hashf_t func)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001520 if (func == keyed_hash)
1521 return TEA_HASH;
1522 if (func == yura_hash)
1523 return YURA_HASH;
1524 if (func == r5_hash)
1525 return R5_HASH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001527 BUG(); // should never happen
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001529 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530}
1531
1532#define SWARN(silent, s, ...) \
1533 if (!(silent)) \
1534 reiserfs_warning (s, __VA_ARGS__)
1535
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001536static int reiserfs_fill_super(struct super_block *s, void *data, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001538 struct inode *root_inode;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001539 struct reiserfs_transaction_handle th;
1540 int old_format = 0;
1541 unsigned long blocks;
1542 unsigned int commit_max_age = 0;
1543 int jinit_done = 0;
1544 struct reiserfs_iget_args args;
1545 struct reiserfs_super_block *rs;
1546 char *jdev_name;
1547 struct reiserfs_sb_info *sbi;
1548 int errval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
Yan Burman01afb212006-12-06 20:39:01 -08001550 sbi = kzalloc(sizeof(struct reiserfs_sb_info), GFP_KERNEL);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001551 if (!sbi) {
1552 errval = -ENOMEM;
1553 goto error;
1554 }
1555 s->s_fs_info = sbi;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001556 /* Set default values for options: non-aggressive tails, RO on errors */
1557 REISERFS_SB(s)->s_mount_opt |= (1 << REISERFS_SMALLTAIL);
1558 REISERFS_SB(s)->s_mount_opt |= (1 << REISERFS_ERROR_RO);
1559 /* no preallocation minimum, be smart in
1560 reiserfs_file_write instead */
1561 REISERFS_SB(s)->s_alloc_options.preallocmin = 0;
1562 /* Preallocate by 16 blocks (17-1) at once */
1563 REISERFS_SB(s)->s_alloc_options.preallocsize = 17;
Alexey Dobriyanfe08a9d2007-05-08 00:24:55 -07001564#ifdef CONFIG_REISERFS_FS_XATTR
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001565 /* Initialize the rwsem for xattr dir */
1566 init_rwsem(&REISERFS_SB(s)->xattr_dir_sem);
Alexey Dobriyanfe08a9d2007-05-08 00:24:55 -07001567#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001568 /* setup default block allocator options */
1569 reiserfs_init_alloc_options(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001571 jdev_name = NULL;
1572 if (reiserfs_parse_options
1573 (s, (char *)data, &(sbi->s_mount_opt), &blocks, &jdev_name,
1574 &commit_max_age) == 0) {
1575 goto error;
1576 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001578 if (blocks) {
1579 SWARN(silent, s, "jmacd-7: reiserfs_fill_super: resize option "
1580 "for remount only");
1581 goto error;
1582 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001584 /* try old format (undistributed bitmap, super block in 8-th 1k block of a device) */
1585 if (!read_super_block(s, REISERFS_OLD_DISK_OFFSET_IN_BYTES))
1586 old_format = 1;
1587 /* try new format (64-th 1k block), which can contain reiserfs super block */
1588 else if (read_super_block(s, REISERFS_DISK_OFFSET_IN_BYTES)) {
1589 SWARN(silent, s,
1590 "sh-2021: reiserfs_fill_super: can not find reiserfs on %s",
1591 reiserfs_bdevname(s));
1592 goto error;
1593 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001595 rs = SB_DISK_SUPER_BLOCK(s);
1596 /* Let's do basic sanity check to verify that underlying device is not
1597 smaller than the filesystem. If the check fails then abort and scream,
1598 because bad stuff will happen otherwise. */
1599 if (s->s_bdev && s->s_bdev->bd_inode
1600 && i_size_read(s->s_bdev->bd_inode) <
1601 sb_block_count(rs) * sb_blocksize(rs)) {
1602 SWARN(silent, s,
1603 "Filesystem on %s cannot be mounted because it is bigger than the device",
1604 reiserfs_bdevname(s));
1605 SWARN(silent, s,
1606 "You may need to run fsck or increase size of your LVM partition");
1607 SWARN(silent, s,
1608 "Or may be you forgot to reboot after fdisk when it told you to");
1609 goto error;
1610 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001612 sbi->s_mount_state = SB_REISERFS_STATE(s);
1613 sbi->s_mount_state = REISERFS_VALID_FS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614
Jeff Mahoney6f010462006-09-30 23:28:43 -07001615 if ((errval = reiserfs_init_bitmap_cache(s))) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001616 SWARN(silent, s,
1617 "jmacd-8: reiserfs_fill_super: unable to read bitmap");
1618 goto error;
1619 }
Jeff Mahoneyd2c89a42006-11-02 22:07:20 -08001620 errval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621#ifdef CONFIG_REISERFS_CHECK
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001622 SWARN(silent, s, "CONFIG_REISERFS_CHECK is set ON");
1623 SWARN(silent, s, "- it is slow mode for debugging.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624#endif
1625
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001626 /* make data=ordered the default */
1627 if (!reiserfs_data_log(s) && !reiserfs_data_ordered(s) &&
1628 !reiserfs_data_writeback(s)) {
1629 REISERFS_SB(s)->s_mount_opt |= (1 << REISERFS_DATA_ORDERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 }
1631
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001632 if (reiserfs_data_log(s)) {
1633 reiserfs_info(s, "using journaled data mode\n");
1634 } else if (reiserfs_data_ordered(s)) {
1635 reiserfs_info(s, "using ordered data mode\n");
1636 } else {
1637 reiserfs_info(s, "using writeback data mode\n");
1638 }
1639 if (reiserfs_barrier_flush(s)) {
1640 printk("reiserfs: using flush barriers\n");
1641 }
1642 // set_device_ro(s->s_dev, 1) ;
1643 if (journal_init(s, jdev_name, old_format, commit_max_age)) {
1644 SWARN(silent, s,
1645 "sh-2022: reiserfs_fill_super: unable to initialize journal space");
1646 goto error;
1647 } else {
1648 jinit_done = 1; /* once this is set, journal_release must be called
1649 ** if we error out of the mount
1650 */
1651 }
1652 if (reread_meta_blocks(s)) {
1653 SWARN(silent, s,
1654 "jmacd-9: reiserfs_fill_super: unable to reread meta blocks after journal init");
1655 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 }
1657
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001658 if (replay_only(s))
1659 goto error;
1660
1661 if (bdev_read_only(s->s_bdev) && !(s->s_flags & MS_RDONLY)) {
1662 SWARN(silent, s,
1663 "clm-7000: Detected readonly device, marking FS readonly");
1664 s->s_flags |= MS_RDONLY;
1665 }
1666 args.objectid = REISERFS_ROOT_OBJECTID;
1667 args.dirid = REISERFS_ROOT_PARENT_OBJECTID;
1668 root_inode =
1669 iget5_locked(s, REISERFS_ROOT_OBJECTID, reiserfs_find_actor,
1670 reiserfs_init_locked_inode, (void *)(&args));
1671 if (!root_inode) {
1672 SWARN(silent, s,
1673 "jmacd-10: reiserfs_fill_super: get root inode failed");
1674 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 }
1676
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001677 if (root_inode->i_state & I_NEW) {
1678 reiserfs_read_locked_inode(root_inode, &args);
1679 unlock_new_inode(root_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 }
1681
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001682 s->s_root = d_alloc_root(root_inode);
1683 if (!s->s_root) {
1684 iput(root_inode);
1685 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001687 // define and initialize hash function
1688 sbi->s_hash_function = hash_function(s);
1689 if (sbi->s_hash_function == NULL) {
1690 dput(s->s_root);
1691 s->s_root = NULL;
1692 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001694
1695 if (is_reiserfs_3_5(rs)
1696 || (is_reiserfs_jr(rs) && SB_VERSION(s) == REISERFS_VERSION_1))
1697 set_bit(REISERFS_3_5, &(sbi->s_properties));
Jeff Mahoneye1fabd32006-09-30 23:28:40 -07001698 else if (old_format)
1699 set_bit(REISERFS_OLD_FORMAT, &(sbi->s_properties));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001700 else
1701 set_bit(REISERFS_3_6, &(sbi->s_properties));
1702
1703 if (!(s->s_flags & MS_RDONLY)) {
1704
1705 errval = journal_begin(&th, s, 1);
1706 if (errval) {
1707 dput(s->s_root);
1708 s->s_root = NULL;
1709 goto error;
1710 }
1711 reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s), 1);
1712
1713 set_sb_umount_state(rs, REISERFS_ERROR_FS);
1714 set_sb_fs_state(rs, 0);
1715
1716 if (old_format_only(s)) {
1717 /* filesystem of format 3.5 either with standard or non-standard
1718 journal */
1719 if (convert_reiserfs(s)) {
1720 /* and -o conv is given */
1721 if (!silent)
1722 reiserfs_info(s,
1723 "converting 3.5 filesystem to the 3.6 format");
1724
1725 if (is_reiserfs_3_5(rs))
1726 /* put magic string of 3.6 format. 2.2 will not be able to
1727 mount this filesystem anymore */
1728 memcpy(rs->s_v1.s_magic,
1729 reiserfs_3_6_magic_string,
1730 sizeof
1731 (reiserfs_3_6_magic_string));
1732
1733 set_sb_version(rs, REISERFS_VERSION_2);
1734 reiserfs_convert_objectid_map_v1(s);
1735 set_bit(REISERFS_3_6, &(sbi->s_properties));
1736 clear_bit(REISERFS_3_5, &(sbi->s_properties));
1737 } else if (!silent) {
1738 reiserfs_info(s, "using 3.5.x disk format\n");
1739 }
1740 }
1741
1742 journal_mark_dirty(&th, s, SB_BUFFER_WITH_SB(s));
1743 errval = journal_end(&th, s, 1);
1744 if (errval) {
1745 dput(s->s_root);
1746 s->s_root = NULL;
1747 goto error;
1748 }
1749
1750 if ((errval = reiserfs_xattr_init(s, s->s_flags))) {
1751 dput(s->s_root);
1752 s->s_root = NULL;
1753 goto error;
1754 }
1755
1756 /* look for files which were to be removed in previous session */
1757 finish_unfinished(s);
1758 } else {
1759 if (old_format_only(s) && !silent) {
1760 reiserfs_info(s, "using 3.5.x disk format\n");
1761 }
1762
1763 if ((errval = reiserfs_xattr_init(s, s->s_flags))) {
1764 dput(s->s_root);
1765 s->s_root = NULL;
1766 goto error;
1767 }
1768 }
1769 // mark hash in super block: it could be unset. overwrite should be ok
1770 set_sb_hash_function_code(rs, function2code(sbi->s_hash_function));
1771
1772 handle_attrs(s);
1773
1774 reiserfs_proc_info_init(s);
1775
1776 init_waitqueue_head(&(sbi->s_wait));
1777 spin_lock_init(&sbi->bitmap_lock);
1778
1779 return (0);
1780
1781 error:
1782 if (jinit_done) { /* kill the commit thread, free journal ram */
1783 journal_release_error(NULL, s);
1784 }
Jeff Mahoney50652272006-09-30 23:28:44 -07001785
1786 reiserfs_free_bitmap_cache(s);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001787 if (SB_BUFFER_WITH_SB(s))
1788 brelse(SB_BUFFER_WITH_SB(s));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789#ifdef CONFIG_QUOTA
Jeff Mahoney50652272006-09-30 23:28:44 -07001790 {
1791 int j;
1792 for (j = 0; j < MAXQUOTAS; j++) {
1793 kfree(sbi->s_qf_names[j]);
1794 sbi->s_qf_names[j] = NULL;
1795 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001796 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797#endif
James Lamanna833d3042005-10-30 15:00:16 -08001798 kfree(sbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001800 s->s_fs_info = NULL;
1801 return errval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802}
1803
David Howells726c3342006-06-23 02:02:58 -07001804static int reiserfs_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805{
David Howells726c3342006-06-23 02:02:58 -07001806 struct reiserfs_super_block *rs = SB_DISK_SUPER_BLOCK(dentry->d_sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001807
1808 buf->f_namelen = (REISERFS_MAX_NAME(s->s_blocksize));
1809 buf->f_bfree = sb_free_blocks(rs);
1810 buf->f_bavail = buf->f_bfree;
1811 buf->f_blocks = sb_block_count(rs) - sb_bmap_nr(rs) - 1;
David Howells726c3342006-06-23 02:02:58 -07001812 buf->f_bsize = dentry->d_sb->s_blocksize;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001813 /* changed to accommodate gcc folks. */
1814 buf->f_type = REISERFS_SUPER_MAGIC;
1815 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816}
1817
1818#ifdef CONFIG_QUOTA
1819static int reiserfs_dquot_initialize(struct inode *inode, int type)
1820{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001821 struct reiserfs_transaction_handle th;
1822 int ret, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001824 /* We may create quota structure so we need to reserve enough blocks */
1825 reiserfs_write_lock(inode->i_sb);
1826 ret =
1827 journal_begin(&th, inode->i_sb,
1828 2 * REISERFS_QUOTA_INIT_BLOCKS(inode->i_sb));
1829 if (ret)
1830 goto out;
1831 ret = dquot_initialize(inode, type);
1832 err =
1833 journal_end(&th, inode->i_sb,
1834 2 * REISERFS_QUOTA_INIT_BLOCKS(inode->i_sb));
1835 if (!ret && err)
1836 ret = err;
1837 out:
1838 reiserfs_write_unlock(inode->i_sb);
1839 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840}
1841
1842static int reiserfs_dquot_drop(struct inode *inode)
1843{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001844 struct reiserfs_transaction_handle th;
1845 int ret, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001847 /* We may delete quota structure so we need to reserve enough blocks */
1848 reiserfs_write_lock(inode->i_sb);
1849 ret =
1850 journal_begin(&th, inode->i_sb,
1851 2 * REISERFS_QUOTA_DEL_BLOCKS(inode->i_sb));
1852 if (ret)
1853 goto out;
1854 ret = dquot_drop(inode);
1855 err =
1856 journal_end(&th, inode->i_sb,
1857 2 * REISERFS_QUOTA_DEL_BLOCKS(inode->i_sb));
1858 if (!ret && err)
1859 ret = err;
1860 out:
1861 reiserfs_write_unlock(inode->i_sb);
1862 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863}
1864
1865static int reiserfs_write_dquot(struct dquot *dquot)
1866{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001867 struct reiserfs_transaction_handle th;
1868 int ret, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001870 reiserfs_write_lock(dquot->dq_sb);
1871 ret =
1872 journal_begin(&th, dquot->dq_sb,
1873 REISERFS_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
1874 if (ret)
1875 goto out;
1876 ret = dquot_commit(dquot);
1877 err =
1878 journal_end(&th, dquot->dq_sb,
1879 REISERFS_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
1880 if (!ret && err)
1881 ret = err;
1882 out:
1883 reiserfs_write_unlock(dquot->dq_sb);
1884 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885}
1886
1887static int reiserfs_acquire_dquot(struct dquot *dquot)
1888{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001889 struct reiserfs_transaction_handle th;
1890 int ret, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001892 reiserfs_write_lock(dquot->dq_sb);
1893 ret =
1894 journal_begin(&th, dquot->dq_sb,
1895 REISERFS_QUOTA_INIT_BLOCKS(dquot->dq_sb));
1896 if (ret)
1897 goto out;
1898 ret = dquot_acquire(dquot);
1899 err =
1900 journal_end(&th, dquot->dq_sb,
1901 REISERFS_QUOTA_INIT_BLOCKS(dquot->dq_sb));
1902 if (!ret && err)
1903 ret = err;
1904 out:
1905 reiserfs_write_unlock(dquot->dq_sb);
1906 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907}
1908
1909static int reiserfs_release_dquot(struct dquot *dquot)
1910{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001911 struct reiserfs_transaction_handle th;
1912 int ret, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001914 reiserfs_write_lock(dquot->dq_sb);
1915 ret =
1916 journal_begin(&th, dquot->dq_sb,
1917 REISERFS_QUOTA_DEL_BLOCKS(dquot->dq_sb));
Jan Kara9c3013e2007-09-11 15:23:29 -07001918 if (ret) {
1919 /* Release dquot anyway to avoid endless cycle in dqput() */
1920 dquot_release(dquot);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001921 goto out;
Jan Kara9c3013e2007-09-11 15:23:29 -07001922 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001923 ret = dquot_release(dquot);
1924 err =
1925 journal_end(&th, dquot->dq_sb,
1926 REISERFS_QUOTA_DEL_BLOCKS(dquot->dq_sb));
1927 if (!ret && err)
1928 ret = err;
1929 out:
1930 reiserfs_write_unlock(dquot->dq_sb);
1931 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932}
1933
1934static int reiserfs_mark_dquot_dirty(struct dquot *dquot)
1935{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001936 /* Are we journalling quotas? */
1937 if (REISERFS_SB(dquot->dq_sb)->s_qf_names[USRQUOTA] ||
1938 REISERFS_SB(dquot->dq_sb)->s_qf_names[GRPQUOTA]) {
1939 dquot_mark_dquot_dirty(dquot);
1940 return reiserfs_write_dquot(dquot);
1941 } else
1942 return dquot_mark_dquot_dirty(dquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943}
1944
1945static int reiserfs_write_info(struct super_block *sb, int type)
1946{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001947 struct reiserfs_transaction_handle th;
1948 int ret, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001950 /* Data block + inode block */
1951 reiserfs_write_lock(sb);
1952 ret = journal_begin(&th, sb, 2);
1953 if (ret)
1954 goto out;
1955 ret = dquot_commit_info(sb, type);
1956 err = journal_end(&th, sb, 2);
1957 if (!ret && err)
1958 ret = err;
1959 out:
1960 reiserfs_write_unlock(sb);
1961 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962}
1963
1964/*
Christoph Hellwig84de8562005-06-23 00:09:16 -07001965 * Turn on quotas during mount time - we need to find the quota file and such...
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 */
1967static int reiserfs_quota_on_mount(struct super_block *sb, int type)
1968{
Christoph Hellwig84de8562005-06-23 00:09:16 -07001969 return vfs_quota_on_mount(sb, REISERFS_SB(sb)->s_qf_names[type],
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001970 REISERFS_SB(sb)->s_jquota_fmt, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971}
1972
1973/*
1974 * Standard function to be called on quota_on
1975 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001976static int reiserfs_quota_on(struct super_block *sb, int type, int format_id,
1977 char *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001979 int err;
1980 struct nameidata nd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001982 if (!(REISERFS_SB(sb)->s_mount_opt & (1 << REISERFS_QUOTA)))
1983 return -EINVAL;
1984 err = path_lookup(path, LOOKUP_FOLLOW, &nd);
1985 if (err)
1986 return err;
1987 /* Quotafile not on the same filesystem? */
1988 if (nd.mnt->mnt_sb != sb) {
1989 path_release(&nd);
1990 return -EXDEV;
1991 }
1992 /* We must not pack tails for quota files on reiserfs for quota IO to work */
1993 if (!REISERFS_I(nd.dentry->d_inode)->i_flags & i_nopack_mask) {
1994 reiserfs_warning(sb,
1995 "reiserfs: Quota file must have tail packing disabled.");
1996 path_release(&nd);
1997 return -EINVAL;
1998 }
1999 /* Not journalling quota? No more tests needed... */
2000 if (!REISERFS_SB(sb)->s_qf_names[USRQUOTA] &&
2001 !REISERFS_SB(sb)->s_qf_names[GRPQUOTA]) {
2002 path_release(&nd);
2003 return vfs_quota_on(sb, type, format_id, path);
2004 }
2005 /* Quotafile not of fs root? */
2006 if (nd.dentry->d_parent->d_inode != sb->s_root->d_inode)
2007 reiserfs_warning(sb,
2008 "reiserfs: Quota file not on filesystem root. "
2009 "Journalled quota will not work.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 path_release(&nd);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002011 return vfs_quota_on(sb, type, format_id, path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012}
2013
2014/* Read data from quotafile - avoid pagecache and such because we cannot afford
2015 * acquiring the locks... As quota files are never truncated and quota code
2016 * itself serializes the operations (and noone else should touch the files)
2017 * we don't have to be afraid of races */
2018static ssize_t reiserfs_quota_read(struct super_block *sb, int type, char *data,
2019 size_t len, loff_t off)
2020{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002021 struct inode *inode = sb_dqopt(sb)->files[type];
2022 unsigned long blk = off >> sb->s_blocksize_bits;
2023 int err = 0, offset = off & (sb->s_blocksize - 1), tocopy;
2024 size_t toread;
2025 struct buffer_head tmp_bh, *bh;
2026 loff_t i_size = i_size_read(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002028 if (off > i_size)
2029 return 0;
2030 if (off + len > i_size)
2031 len = i_size - off;
2032 toread = len;
2033 while (toread > 0) {
2034 tocopy =
2035 sb->s_blocksize - offset <
2036 toread ? sb->s_blocksize - offset : toread;
2037 tmp_bh.b_state = 0;
2038 /* Quota files are without tails so we can safely use this function */
2039 reiserfs_write_lock(sb);
2040 err = reiserfs_get_block(inode, blk, &tmp_bh, 0);
2041 reiserfs_write_unlock(sb);
2042 if (err)
2043 return err;
2044 if (!buffer_mapped(&tmp_bh)) /* A hole? */
2045 memset(data, 0, tocopy);
2046 else {
2047 bh = sb_bread(sb, tmp_bh.b_blocknr);
2048 if (!bh)
2049 return -EIO;
2050 memcpy(data, bh->b_data + offset, tocopy);
2051 brelse(bh);
2052 }
2053 offset = 0;
2054 toread -= tocopy;
2055 data += tocopy;
2056 blk++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002058 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059}
2060
2061/* Write to quotafile (we know the transaction is already started and has
2062 * enough credits) */
2063static ssize_t reiserfs_quota_write(struct super_block *sb, int type,
2064 const char *data, size_t len, loff_t off)
2065{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002066 struct inode *inode = sb_dqopt(sb)->files[type];
2067 unsigned long blk = off >> sb->s_blocksize_bits;
2068 int err = 0, offset = off & (sb->s_blocksize - 1), tocopy;
2069 int journal_quota = REISERFS_SB(sb)->s_qf_names[type] != NULL;
2070 size_t towrite = len;
2071 struct buffer_head tmp_bh, *bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
Jan Kara9c3013e2007-09-11 15:23:29 -07002073 if (!current->journal_info) {
2074 printk(KERN_WARNING "reiserfs: Quota write (off=%Lu, len=%Lu)"
2075 " cancelled because transaction is not started.\n",
2076 (unsigned long long)off, (unsigned long long)len);
2077 return -EIO;
2078 }
Arjan van de Ven5c81a412006-07-03 00:25:20 -07002079 mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002080 while (towrite > 0) {
2081 tocopy = sb->s_blocksize - offset < towrite ?
2082 sb->s_blocksize - offset : towrite;
2083 tmp_bh.b_state = 0;
2084 err = reiserfs_get_block(inode, blk, &tmp_bh, GET_BLOCK_CREATE);
2085 if (err)
2086 goto out;
2087 if (offset || tocopy != sb->s_blocksize)
2088 bh = sb_bread(sb, tmp_bh.b_blocknr);
2089 else
2090 bh = sb_getblk(sb, tmp_bh.b_blocknr);
2091 if (!bh) {
2092 err = -EIO;
2093 goto out;
2094 }
2095 lock_buffer(bh);
2096 memcpy(bh->b_data + offset, data, tocopy);
2097 flush_dcache_page(bh->b_page);
2098 set_buffer_uptodate(bh);
2099 unlock_buffer(bh);
2100 reiserfs_prepare_for_journal(sb, bh, 1);
2101 journal_mark_dirty(current->journal_info, sb, bh);
2102 if (!journal_quota)
2103 reiserfs_add_ordered_list(inode, bh);
2104 brelse(bh);
2105 offset = 0;
2106 towrite -= tocopy;
2107 data += tocopy;
2108 blk++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 }
Jan Kara9c3013e2007-09-11 15:23:29 -07002110out:
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002111 if (len == towrite)
2112 return err;
2113 if (inode->i_size < off + len - towrite)
2114 i_size_write(inode, off + len - towrite);
2115 inode->i_version++;
2116 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
2117 mark_inode_dirty(inode);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002118 mutex_unlock(&inode->i_mutex);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002119 return len - towrite;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120}
2121
2122#endif
2123
David Howells454e2392006-06-23 02:02:57 -07002124static int get_super_block(struct file_system_type *fs_type,
2125 int flags, const char *dev_name,
2126 void *data, struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127{
David Howells454e2392006-06-23 02:02:57 -07002128 return get_sb_bdev(fs_type, flags, dev_name, data, reiserfs_fill_super,
2129 mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130}
2131
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002132static int __init init_reiserfs_fs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133{
2134 int ret;
2135
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002136 if ((ret = init_inodecache())) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 return ret;
2138 }
2139
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002140 if ((ret = reiserfs_xattr_register_handlers()))
2141 goto failed_reiserfs_xattr_register_handlers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002143 reiserfs_proc_info_global_init();
2144 reiserfs_proc_register_global("version",
2145 reiserfs_global_version_in_proc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002147 ret = register_filesystem(&reiserfs_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
2149 if (ret == 0) {
2150 return 0;
2151 }
2152
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002153 reiserfs_xattr_unregister_handlers();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002155 failed_reiserfs_xattr_register_handlers:
2156 reiserfs_proc_unregister_global("version");
2157 reiserfs_proc_info_global_done();
2158 destroy_inodecache();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159
2160 return ret;
2161}
2162
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002163static void __exit exit_reiserfs_fs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002165 reiserfs_xattr_unregister_handlers();
2166 reiserfs_proc_unregister_global("version");
2167 reiserfs_proc_info_global_done();
2168 unregister_filesystem(&reiserfs_fs_type);
2169 destroy_inodecache();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170}
2171
2172struct file_system_type reiserfs_fs_type = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002173 .owner = THIS_MODULE,
2174 .name = "reiserfs",
2175 .get_sb = get_super_block,
David Howellsedc666e2006-10-11 01:22:14 -07002176 .kill_sb = reiserfs_kill_sb,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002177 .fs_flags = FS_REQUIRES_DEV,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178};
2179
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002180MODULE_DESCRIPTION("ReiserFS journaled filesystem");
2181MODULE_AUTHOR("Hans Reiser <reiser@namesys.com>");
2182MODULE_LICENSE("GPL");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002184module_init(init_reiserfs_fs);
2185module_exit(exit_reiserfs_fs);