blob: c78e99e196fad1a30bbd8d0eaa1c97f5d7c60d65 [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>
26#include <linux/namespace.h>
27#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
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700433static void reiserfs_put_super(struct super_block *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700435 int i;
436 struct reiserfs_transaction_handle th;
437 th.t_trans_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700439 if (REISERFS_SB(s)->xattr_root) {
440 d_invalidate(REISERFS_SB(s)->xattr_root);
441 dput(REISERFS_SB(s)->xattr_root);
442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700444 if (REISERFS_SB(s)->priv_root) {
445 d_invalidate(REISERFS_SB(s)->priv_root);
446 dput(REISERFS_SB(s)->priv_root);
447 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700449 /* change file system state to current state if it was mounted with read-write permissions */
450 if (!(s->s_flags & MS_RDONLY)) {
451 if (!journal_begin(&th, s, 10)) {
452 reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s),
453 1);
454 set_sb_umount_state(SB_DISK_SUPER_BLOCK(s),
455 REISERFS_SB(s)->s_mount_state);
456 journal_mark_dirty(&th, s, SB_BUFFER_WITH_SB(s));
457 }
458 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700460 /* note, journal_release checks for readonly mount, and can decide not
461 ** to do a journal_end
462 */
463 journal_release(&th, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700465 for (i = 0; i < SB_BMAP_NR(s); i++)
466 brelse(SB_AP_BITMAP(s)[i].bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700468 vfree(SB_AP_BITMAP(s));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700470 brelse(SB_BUFFER_WITH_SB(s));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700472 print_statistics(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700474 if (REISERFS_SB(s)->reserved_blocks != 0) {
475 reiserfs_warning(s,
476 "green-2005: reiserfs_put_super: reserved blocks left %d",
477 REISERFS_SB(s)->reserved_blocks);
478 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700480 reiserfs_proc_info_done(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700482 kfree(s->s_fs_info);
483 s->s_fs_info = NULL;
484
485 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486}
487
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700488static kmem_cache_t *reiserfs_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490static struct inode *reiserfs_alloc_inode(struct super_block *sb)
491{
492 struct reiserfs_inode_info *ei;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700493 ei = (struct reiserfs_inode_info *)
494 kmem_cache_alloc(reiserfs_inode_cachep, SLAB_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 if (!ei)
496 return NULL;
497 return &ei->vfs_inode;
498}
499
500static void reiserfs_destroy_inode(struct inode *inode)
501{
502 kmem_cache_free(reiserfs_inode_cachep, REISERFS_I(inode));
503}
504
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700505static void init_once(void *foo, kmem_cache_t * cachep, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700507 struct reiserfs_inode_info *ei = (struct reiserfs_inode_info *)foo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700509 if ((flags & (SLAB_CTOR_VERIFY | SLAB_CTOR_CONSTRUCTOR)) ==
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 SLAB_CTOR_CONSTRUCTOR) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700511 INIT_LIST_HEAD(&ei->i_prealloc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 inode_init_once(&ei->vfs_inode);
Alexey Dobriyancfe14672006-09-29 02:00:00 -0700513#ifdef CONFIG_REISERFS_FS_POSIX_ACL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 ei->i_acl_access = NULL;
515 ei->i_acl_default = NULL;
Alexey Dobriyancfe14672006-09-29 02:00:00 -0700516#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 }
518}
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520static int init_inodecache(void)
521{
522 reiserfs_inode_cachep = kmem_cache_create("reiser_inode_cache",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700523 sizeof(struct
524 reiserfs_inode_info),
Paul Jacksonfffb60f2006-03-24 03:16:06 -0800525 0, (SLAB_RECLAIM_ACCOUNT|
526 SLAB_MEM_SPREAD),
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700527 init_once, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 if (reiserfs_inode_cachep == NULL)
529 return -ENOMEM;
530 return 0;
531}
532
533static void destroy_inodecache(void)
534{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -0700535 kmem_cache_destroy(reiserfs_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536}
537
538/* we don't mark inodes dirty, we just log them */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700539static void reiserfs_dirty_inode(struct inode *inode)
540{
541 struct reiserfs_transaction_handle th;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700543 int err = 0;
544 if (inode->i_sb->s_flags & MS_RDONLY) {
545 reiserfs_warning(inode->i_sb,
546 "clm-6006: writing inode %lu on readonly FS",
547 inode->i_ino);
548 return;
549 }
550 reiserfs_write_lock(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700552 /* this is really only used for atime updates, so they don't have
553 ** to be included in O_SYNC or fsync
554 */
555 err = journal_begin(&th, inode->i_sb, 1);
556 if (err) {
557 reiserfs_write_unlock(inode->i_sb);
558 return;
559 }
560 reiserfs_update_sd(&th, inode);
561 journal_end(&th, inode->i_sb, 1);
562 reiserfs_write_unlock(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563}
564
Alexey Dobriyancfe14672006-09-29 02:00:00 -0700565#ifdef CONFIG_REISERFS_FS_POSIX_ACL
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700566static void reiserfs_clear_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700568 struct posix_acl *acl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700570 acl = REISERFS_I(inode)->i_acl_access;
571 if (acl && !IS_ERR(acl))
572 posix_acl_release(acl);
573 REISERFS_I(inode)->i_acl_access = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700575 acl = REISERFS_I(inode)->i_acl_default;
576 if (acl && !IS_ERR(acl))
577 posix_acl_release(acl);
578 REISERFS_I(inode)->i_acl_default = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579}
Alexey Dobriyancfe14672006-09-29 02:00:00 -0700580#else
581#define reiserfs_clear_inode NULL
582#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
584#ifdef CONFIG_QUOTA
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700585static ssize_t reiserfs_quota_write(struct super_block *, int, const char *,
586 size_t, loff_t);
587static ssize_t reiserfs_quota_read(struct super_block *, int, char *, size_t,
588 loff_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589#endif
590
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700591static struct super_operations reiserfs_sops = {
592 .alloc_inode = reiserfs_alloc_inode,
593 .destroy_inode = reiserfs_destroy_inode,
594 .write_inode = reiserfs_write_inode,
595 .dirty_inode = reiserfs_dirty_inode,
596 .delete_inode = reiserfs_delete_inode,
597 .clear_inode = reiserfs_clear_inode,
598 .put_super = reiserfs_put_super,
599 .write_super = reiserfs_write_super,
600 .sync_fs = reiserfs_sync_fs,
601 .write_super_lockfs = reiserfs_write_super_lockfs,
602 .unlockfs = reiserfs_unlockfs,
603 .statfs = reiserfs_statfs,
604 .remount_fs = reiserfs_remount,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605#ifdef CONFIG_QUOTA
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700606 .quota_read = reiserfs_quota_read,
607 .quota_write = reiserfs_quota_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608#endif
609};
610
611#ifdef CONFIG_QUOTA
612#define QTYPE2NAME(t) ((t)==USRQUOTA?"user":"group")
613
614static int reiserfs_dquot_initialize(struct inode *, int);
615static int reiserfs_dquot_drop(struct inode *);
616static int reiserfs_write_dquot(struct dquot *);
617static int reiserfs_acquire_dquot(struct dquot *);
618static int reiserfs_release_dquot(struct dquot *);
619static int reiserfs_mark_dquot_dirty(struct dquot *);
620static int reiserfs_write_info(struct super_block *, int);
621static int reiserfs_quota_on(struct super_block *, int, int, char *);
622
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700623static struct dquot_operations reiserfs_quota_operations = {
624 .initialize = reiserfs_dquot_initialize,
625 .drop = reiserfs_dquot_drop,
626 .alloc_space = dquot_alloc_space,
627 .alloc_inode = dquot_alloc_inode,
628 .free_space = dquot_free_space,
629 .free_inode = dquot_free_inode,
630 .transfer = dquot_transfer,
631 .write_dquot = reiserfs_write_dquot,
632 .acquire_dquot = reiserfs_acquire_dquot,
633 .release_dquot = reiserfs_release_dquot,
634 .mark_dirty = reiserfs_mark_dquot_dirty,
635 .write_info = reiserfs_write_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636};
637
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700638static struct quotactl_ops reiserfs_qctl_operations = {
639 .quota_on = reiserfs_quota_on,
640 .quota_off = vfs_quota_off,
641 .quota_sync = vfs_quota_sync,
642 .get_info = vfs_get_dqinfo,
643 .set_info = vfs_set_dqinfo,
644 .get_dqblk = vfs_get_dqblk,
645 .set_dqblk = vfs_set_dqblk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646};
647#endif
648
649static struct export_operations reiserfs_export_ops = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700650 .encode_fh = reiserfs_encode_fh,
651 .decode_fh = reiserfs_decode_fh,
652 .get_parent = reiserfs_get_parent,
653 .get_dentry = reiserfs_get_dentry,
654};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
656/* this struct is used in reiserfs_getopt () for containing the value for those
657 mount options that have values rather than being toggles. */
658typedef struct {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700659 char *value;
660 int setmask; /* bitmask which is to set on mount_options bitmask when this
661 value is found, 0 is no bits are to be changed. */
662 int clrmask; /* bitmask which is to clear on mount_options bitmask when this
663 value is found, 0 is no bits are to be changed. This is
664 applied BEFORE setmask */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665} arg_desc_t;
666
667/* Set this bit in arg_required to allow empty arguments */
668#define REISERFS_OPT_ALLOWEMPTY 31
669
670/* this struct is used in reiserfs_getopt() for describing the set of reiserfs
671 mount options */
672typedef struct {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700673 char *option_name;
674 int arg_required; /* 0 if argument is not required, not 0 otherwise */
675 const arg_desc_t *values; /* list of values accepted by an option */
676 int setmask; /* bitmask which is to set on mount_options bitmask when this
677 value is found, 0 is no bits are to be changed. */
678 int clrmask; /* bitmask which is to clear on mount_options bitmask when this
679 value is found, 0 is no bits are to be changed. This is
680 applied BEFORE setmask */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681} opt_desc_t;
682
683/* possible values for -o data= */
684static const arg_desc_t logging_mode[] = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700685 {"ordered", 1 << REISERFS_DATA_ORDERED,
686 (1 << REISERFS_DATA_LOG | 1 << REISERFS_DATA_WRITEBACK)},
687 {"journal", 1 << REISERFS_DATA_LOG,
688 (1 << REISERFS_DATA_ORDERED | 1 << REISERFS_DATA_WRITEBACK)},
689 {"writeback", 1 << REISERFS_DATA_WRITEBACK,
690 (1 << REISERFS_DATA_ORDERED | 1 << REISERFS_DATA_LOG)},
Vladimir V. Savelievcd02b962006-03-25 03:07:15 -0800691 {.value = NULL}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692};
693
694/* possible values for -o barrier= */
695static const arg_desc_t barrier_mode[] = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700696 {"none", 1 << REISERFS_BARRIER_NONE, 1 << REISERFS_BARRIER_FLUSH},
697 {"flush", 1 << REISERFS_BARRIER_FLUSH, 1 << REISERFS_BARRIER_NONE},
Vladimir V. Savelievcd02b962006-03-25 03:07:15 -0800698 {.value = NULL}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699};
700
701/* possible values for "-o block-allocator=" and bits which are to be set in
702 s_mount_opt of reiserfs specific part of in-core super block */
703static const arg_desc_t balloc[] = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700704 {"noborder", 1 << REISERFS_NO_BORDER, 0},
705 {"border", 0, 1 << REISERFS_NO_BORDER},
706 {"no_unhashed_relocation", 1 << REISERFS_NO_UNHASHED_RELOCATION, 0},
707 {"hashed_relocation", 1 << REISERFS_HASHED_RELOCATION, 0},
708 {"test4", 1 << REISERFS_TEST4, 0},
709 {"notest4", 0, 1 << REISERFS_TEST4},
710 {NULL, 0, 0}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711};
712
713static const arg_desc_t tails[] = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700714 {"on", 1 << REISERFS_LARGETAIL, 1 << REISERFS_SMALLTAIL},
715 {"off", 0, (1 << REISERFS_LARGETAIL) | (1 << REISERFS_SMALLTAIL)},
716 {"small", 1 << REISERFS_SMALLTAIL, 1 << REISERFS_LARGETAIL},
717 {NULL, 0, 0}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718};
719
720static const arg_desc_t error_actions[] = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700721 {"panic", 1 << REISERFS_ERROR_PANIC,
722 (1 << REISERFS_ERROR_RO | 1 << REISERFS_ERROR_CONTINUE)},
723 {"ro-remount", 1 << REISERFS_ERROR_RO,
724 (1 << REISERFS_ERROR_PANIC | 1 << REISERFS_ERROR_CONTINUE)},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725#ifdef REISERFS_JOURNAL_ERROR_ALLOWS_NO_LOG
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700726 {"continue", 1 << REISERFS_ERROR_CONTINUE,
727 (1 << REISERFS_ERROR_PANIC | 1 << REISERFS_ERROR_RO)},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700729 {NULL, 0, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730};
731
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732/* proceed only one option from a list *cur - string containing of mount options
733 opts - array of options which are accepted
734 opt_arg - if option is found and requires an argument and if it is specifed
735 in the input - pointer to the argument is stored here
736 bit_flags - if option requires to set a certain bit - it is set here
737 return -1 if unknown option is found, opt->arg_required otherwise */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700738static int reiserfs_getopt(struct super_block *s, char **cur, opt_desc_t * opts,
739 char **opt_arg, unsigned long *bit_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700741 char *p;
742 /* foo=bar,
743 ^ ^ ^
744 | | +-- option_end
745 | +-- arg_start
746 +-- option_start
747 */
748 const opt_desc_t *opt;
749 const arg_desc_t *arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700751 p = *cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700753 /* assume argument cannot contain commas */
754 *cur = strchr(p, ',');
755 if (*cur) {
756 *(*cur) = '\0';
757 (*cur)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700760 if (!strncmp(p, "alloc=", 6)) {
761 /* Ugly special case, probably we should redo options parser so that
762 it can understand several arguments for some options, also so that
763 it can fill several bitfields with option values. */
764 if (reiserfs_parse_alloc_options(s, p + 6)) {
765 return -1;
766 } else {
767 return 0;
768 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700770
771 /* for every option in the list */
772 for (opt = opts; opt->option_name; opt++) {
773 if (!strncmp(p, opt->option_name, strlen(opt->option_name))) {
774 if (bit_flags) {
775 if (opt->clrmask ==
776 (1 << REISERFS_UNSUPPORTED_OPT))
777 reiserfs_warning(s, "%s not supported.",
778 p);
779 else
780 *bit_flags &= ~opt->clrmask;
781 if (opt->setmask ==
782 (1 << REISERFS_UNSUPPORTED_OPT))
783 reiserfs_warning(s, "%s not supported.",
784 p);
785 else
786 *bit_flags |= opt->setmask;
787 }
788 break;
789 }
790 }
791 if (!opt->option_name) {
792 reiserfs_warning(s, "unknown mount option \"%s\"", p);
793 return -1;
794 }
795
796 p += strlen(opt->option_name);
797 switch (*p) {
798 case '=':
799 if (!opt->arg_required) {
800 reiserfs_warning(s,
801 "the option \"%s\" does not require an argument",
802 opt->option_name);
803 return -1;
804 }
805 break;
806
807 case 0:
808 if (opt->arg_required) {
809 reiserfs_warning(s,
810 "the option \"%s\" requires an argument",
811 opt->option_name);
812 return -1;
813 }
814 break;
815 default:
816 reiserfs_warning(s, "head of option \"%s\" is only correct",
817 opt->option_name);
818 return -1;
819 }
820
821 /* move to the argument, or to next option if argument is not required */
822 p++;
823
824 if (opt->arg_required
825 && !(opt->arg_required & (1 << REISERFS_OPT_ALLOWEMPTY))
826 && !strlen(p)) {
827 /* this catches "option=," if not allowed */
828 reiserfs_warning(s, "empty argument for \"%s\"",
829 opt->option_name);
830 return -1;
831 }
832
833 if (!opt->values) {
834 /* *=NULLopt_arg contains pointer to argument */
835 *opt_arg = p;
836 return opt->arg_required & ~(1 << REISERFS_OPT_ALLOWEMPTY);
837 }
838
839 /* values possible for this option are listed in opt->values */
840 for (arg = opt->values; arg->value; arg++) {
841 if (!strcmp(p, arg->value)) {
842 if (bit_flags) {
843 *bit_flags &= ~arg->clrmask;
844 *bit_flags |= arg->setmask;
845 }
846 return opt->arg_required;
847 }
848 }
849
850 reiserfs_warning(s, "bad value \"%s\" for option \"%s\"", p,
851 opt->option_name);
852 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853}
854
855/* returns 0 if something is wrong in option string, 1 - otherwise */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700856static int reiserfs_parse_options(struct super_block *s, char *options, /* string given via mount's -o */
857 unsigned long *mount_options,
858 /* after the parsing phase, contains the
859 collection of bitflags defining what
860 mount options were selected. */
861 unsigned long *blocks, /* strtol-ed from NNN of resize=NNN */
862 char **jdev_name,
863 unsigned int *commit_max_age)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700865 int c;
866 char *arg = NULL;
867 char *pos;
868 opt_desc_t opts[] = {
869 /* Compatibility stuff, so that -o notail for old setups still work */
870 {"tails",.arg_required = 't',.values = tails},
871 {"notail",.clrmask =
872 (1 << REISERFS_LARGETAIL) | (1 << REISERFS_SMALLTAIL)},
873 {"conv",.setmask = 1 << REISERFS_CONVERT},
874 {"attrs",.setmask = 1 << REISERFS_ATTRS},
875 {"noattrs",.clrmask = 1 << REISERFS_ATTRS},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876#ifdef CONFIG_REISERFS_FS_XATTR
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700877 {"user_xattr",.setmask = 1 << REISERFS_XATTRS_USER},
878 {"nouser_xattr",.clrmask = 1 << REISERFS_XATTRS_USER},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879#else
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700880 {"user_xattr",.setmask = 1 << REISERFS_UNSUPPORTED_OPT},
881 {"nouser_xattr",.clrmask = 1 << REISERFS_UNSUPPORTED_OPT},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882#endif
883#ifdef CONFIG_REISERFS_FS_POSIX_ACL
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700884 {"acl",.setmask = 1 << REISERFS_POSIXACL},
885 {"noacl",.clrmask = 1 << REISERFS_POSIXACL},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886#else
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700887 {"acl",.setmask = 1 << REISERFS_UNSUPPORTED_OPT},
888 {"noacl",.clrmask = 1 << REISERFS_UNSUPPORTED_OPT},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889#endif
Vladimir V. Savelievcd02b962006-03-25 03:07:15 -0800890 {.option_name = "nolog"},
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700891 {"replayonly",.setmask = 1 << REPLAYONLY},
892 {"block-allocator",.arg_required = 'a',.values = balloc},
893 {"data",.arg_required = 'd',.values = logging_mode},
894 {"barrier",.arg_required = 'b',.values = barrier_mode},
895 {"resize",.arg_required = 'r',.values = NULL},
896 {"jdev",.arg_required = 'j',.values = NULL},
897 {"nolargeio",.arg_required = 'w',.values = NULL},
898 {"commit",.arg_required = 'c',.values = NULL},
899 {"usrquota",.setmask = 1 << REISERFS_QUOTA},
900 {"grpquota",.setmask = 1 << REISERFS_QUOTA},
901 {"noquota",.clrmask = 1 << REISERFS_QUOTA},
902 {"errors",.arg_required = 'e',.values = error_actions},
903 {"usrjquota",.arg_required =
904 'u' | (1 << REISERFS_OPT_ALLOWEMPTY),.values = NULL},
905 {"grpjquota",.arg_required =
906 'g' | (1 << REISERFS_OPT_ALLOWEMPTY),.values = NULL},
907 {"jqfmt",.arg_required = 'f',.values = NULL},
Vladimir V. Savelievcd02b962006-03-25 03:07:15 -0800908 {.option_name = NULL}
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700909 };
910
911 *blocks = 0;
912 if (!options || !*options)
913 /* use default configuration: create tails, journaling on, no
914 conversion to newest format */
915 return 1;
916
917 for (pos = options; pos;) {
918 c = reiserfs_getopt(s, &pos, opts, &arg, mount_options);
919 if (c == -1)
920 /* wrong option is given */
921 return 0;
922
923 if (c == 'r') {
924 char *p;
925
926 p = NULL;
927 /* "resize=NNN" or "resize=auto" */
928
929 if (!strcmp(arg, "auto")) {
930 /* From JFS code, to auto-get the size. */
931 *blocks =
932 s->s_bdev->bd_inode->i_size >> s->
933 s_blocksize_bits;
934 } else {
935 *blocks = simple_strtoul(arg, &p, 0);
936 if (*p != '\0') {
937 /* NNN does not look like a number */
938 reiserfs_warning(s,
939 "reiserfs_parse_options: bad value %s",
940 arg);
941 return 0;
942 }
943 }
944 }
945
946 if (c == 'c') {
947 char *p = NULL;
948 unsigned long val = simple_strtoul(arg, &p, 0);
949 /* commit=NNN (time in seconds) */
950 if (*p != '\0' || val >= (unsigned int)-1) {
951 reiserfs_warning(s,
952 "reiserfs_parse_options: bad value %s",
953 arg);
954 return 0;
955 }
956 *commit_max_age = (unsigned int)val;
957 }
958
959 if (c == 'w') {
Adrian Bunk36b756f2006-09-27 01:50:50 -0700960 reiserfs_warning(s, "reiserfs: nolargeio option is no longer supported");
961 return 0;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700962 }
963
964 if (c == 'j') {
965 if (arg && *arg && jdev_name) {
966 if (*jdev_name) { //Hm, already assigned?
967 reiserfs_warning(s,
968 "reiserfs_parse_options: journal device was already specified to be %s",
969 *jdev_name);
970 return 0;
971 }
972 *jdev_name = arg;
973 }
974 }
975#ifdef CONFIG_QUOTA
976 if (c == 'u' || c == 'g') {
977 int qtype = c == 'u' ? USRQUOTA : GRPQUOTA;
978
979 if (sb_any_quota_enabled(s)) {
980 reiserfs_warning(s,
981 "reiserfs_parse_options: cannot change journalled quota options when quota turned on.");
982 return 0;
983 }
984 if (*arg) { /* Some filename specified? */
985 if (REISERFS_SB(s)->s_qf_names[qtype]
986 && strcmp(REISERFS_SB(s)->s_qf_names[qtype],
987 arg)) {
988 reiserfs_warning(s,
989 "reiserfs_parse_options: %s quota file already specified.",
990 QTYPE2NAME(qtype));
991 return 0;
992 }
993 if (strchr(arg, '/')) {
994 reiserfs_warning(s,
995 "reiserfs_parse_options: quotafile must be on filesystem root.");
996 return 0;
997 }
998 REISERFS_SB(s)->s_qf_names[qtype] =
999 kmalloc(strlen(arg) + 1, GFP_KERNEL);
1000 if (!REISERFS_SB(s)->s_qf_names[qtype]) {
1001 reiserfs_warning(s,
1002 "reiserfs_parse_options: not enough memory for storing quotafile name.");
1003 return 0;
1004 }
1005 strcpy(REISERFS_SB(s)->s_qf_names[qtype], arg);
1006 *mount_options |= 1 << REISERFS_QUOTA;
1007 } else {
James Lamanna833d3042005-10-30 15:00:16 -08001008 kfree(REISERFS_SB(s)->s_qf_names[qtype]);
1009 REISERFS_SB(s)->s_qf_names[qtype] = NULL;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001010 }
1011 }
1012 if (c == 'f') {
1013 if (!strcmp(arg, "vfsold"))
1014 REISERFS_SB(s)->s_jquota_fmt = QFMT_VFS_OLD;
1015 else if (!strcmp(arg, "vfsv0"))
1016 REISERFS_SB(s)->s_jquota_fmt = QFMT_VFS_V0;
1017 else {
1018 reiserfs_warning(s,
1019 "reiserfs_parse_options: unknown quota format specified.");
1020 return 0;
1021 }
1022 }
1023#else
1024 if (c == 'u' || c == 'g' || c == 'f') {
1025 reiserfs_warning(s,
1026 "reiserfs_parse_options: journalled quota options not supported.");
1027 return 0;
1028 }
1029#endif
1030 }
1031
1032#ifdef CONFIG_QUOTA
1033 if (!REISERFS_SB(s)->s_jquota_fmt
1034 && (REISERFS_SB(s)->s_qf_names[USRQUOTA]
1035 || REISERFS_SB(s)->s_qf_names[GRPQUOTA])) {
1036 reiserfs_warning(s,
1037 "reiserfs_parse_options: journalled quota format not specified.");
1038 return 0;
1039 }
1040 /* This checking is not precise wrt the quota type but for our purposes it is sufficient */
1041 if (!(*mount_options & (1 << REISERFS_QUOTA))
1042 && sb_any_quota_enabled(s)) {
1043 reiserfs_warning(s,
1044 "reiserfs_parse_options: quota options must be present when quota is turned on.");
1045 return 0;
1046 }
1047#endif
1048
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050}
1051
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001052static void switch_data_mode(struct super_block *s, unsigned long mode)
1053{
1054 REISERFS_SB(s)->s_mount_opt &= ~((1 << REISERFS_DATA_LOG) |
1055 (1 << REISERFS_DATA_ORDERED) |
1056 (1 << REISERFS_DATA_WRITEBACK));
1057 REISERFS_SB(s)->s_mount_opt |= (1 << mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058}
1059
1060static void handle_data_mode(struct super_block *s, unsigned long mount_options)
1061{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001062 if (mount_options & (1 << REISERFS_DATA_LOG)) {
1063 if (!reiserfs_data_log(s)) {
1064 switch_data_mode(s, REISERFS_DATA_LOG);
1065 reiserfs_info(s, "switching to journaled data mode\n");
1066 }
1067 } else if (mount_options & (1 << REISERFS_DATA_ORDERED)) {
1068 if (!reiserfs_data_ordered(s)) {
1069 switch_data_mode(s, REISERFS_DATA_ORDERED);
1070 reiserfs_info(s, "switching to ordered data mode\n");
1071 }
1072 } else if (mount_options & (1 << REISERFS_DATA_WRITEBACK)) {
1073 if (!reiserfs_data_writeback(s)) {
1074 switch_data_mode(s, REISERFS_DATA_WRITEBACK);
1075 reiserfs_info(s, "switching to writeback data mode\n");
1076 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078}
1079
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001080static void handle_barrier_mode(struct super_block *s, unsigned long bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001082 int flush = (1 << REISERFS_BARRIER_FLUSH);
1083 int none = (1 << REISERFS_BARRIER_NONE);
1084 int all_barrier = flush | none;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001086 if (bits & all_barrier) {
1087 REISERFS_SB(s)->s_mount_opt &= ~all_barrier;
1088 if (bits & flush) {
1089 REISERFS_SB(s)->s_mount_opt |= flush;
1090 printk("reiserfs: enabling write barrier flush mode\n");
1091 } else if (bits & none) {
1092 REISERFS_SB(s)->s_mount_opt |= none;
1093 printk("reiserfs: write barriers turned off\n");
1094 }
1095 }
1096}
1097
1098static void handle_attrs(struct super_block *s)
1099{
1100 struct reiserfs_super_block *rs = SB_DISK_SUPER_BLOCK(s);
1101
1102 if (reiserfs_attrs(s)) {
1103 if (old_format_only(s)) {
1104 reiserfs_warning(s,
1105 "reiserfs: cannot support attributes on 3.5.x disk format");
1106 REISERFS_SB(s)->s_mount_opt &= ~(1 << REISERFS_ATTRS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 return;
1108 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001109 if (!(le32_to_cpu(rs->s_flags) & reiserfs_attrs_cleared)) {
1110 reiserfs_warning(s,
1111 "reiserfs: cannot support attributes until flag is set in super-block");
1112 REISERFS_SB(s)->s_mount_opt &= ~(1 << REISERFS_ATTRS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 }
1114 }
1115}
1116
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001117static int reiserfs_remount(struct super_block *s, int *mount_flags, char *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001119 struct reiserfs_super_block *rs;
1120 struct reiserfs_transaction_handle th;
1121 unsigned long blocks;
1122 unsigned long mount_options = REISERFS_SB(s)->s_mount_opt;
1123 unsigned long safe_mask = 0;
1124 unsigned int commit_max_age = (unsigned int)-1;
1125 struct reiserfs_journal *journal = SB_JOURNAL(s);
1126 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127#ifdef CONFIG_QUOTA
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001128 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129#endif
1130
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001131 rs = SB_DISK_SUPER_BLOCK(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001133 if (!reiserfs_parse_options
1134 (s, arg, &mount_options, &blocks, NULL, &commit_max_age)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135#ifdef CONFIG_QUOTA
James Lamanna833d3042005-10-30 15:00:16 -08001136 for (i = 0; i < MAXQUOTAS; i++) {
1137 kfree(REISERFS_SB(s)->s_qf_names[i]);
1138 REISERFS_SB(s)->s_qf_names[i] = NULL;
1139 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001140#endif
1141 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001144 handle_attrs(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001146 /* Add options that are safe here */
1147 safe_mask |= 1 << REISERFS_SMALLTAIL;
1148 safe_mask |= 1 << REISERFS_LARGETAIL;
1149 safe_mask |= 1 << REISERFS_NO_BORDER;
1150 safe_mask |= 1 << REISERFS_NO_UNHASHED_RELOCATION;
1151 safe_mask |= 1 << REISERFS_HASHED_RELOCATION;
1152 safe_mask |= 1 << REISERFS_TEST4;
1153 safe_mask |= 1 << REISERFS_ATTRS;
1154 safe_mask |= 1 << REISERFS_XATTRS_USER;
1155 safe_mask |= 1 << REISERFS_POSIXACL;
1156 safe_mask |= 1 << REISERFS_BARRIER_FLUSH;
1157 safe_mask |= 1 << REISERFS_BARRIER_NONE;
1158 safe_mask |= 1 << REISERFS_ERROR_RO;
1159 safe_mask |= 1 << REISERFS_ERROR_CONTINUE;
1160 safe_mask |= 1 << REISERFS_ERROR_PANIC;
1161 safe_mask |= 1 << REISERFS_QUOTA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001163 /* Update the bitmask, taking care to keep
1164 * the bits we're not allowed to change here */
1165 REISERFS_SB(s)->s_mount_opt =
1166 (REISERFS_SB(s)->
1167 s_mount_opt & ~safe_mask) | (mount_options & safe_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001169 if (commit_max_age != 0 && commit_max_age != (unsigned int)-1) {
1170 journal->j_max_commit_age = commit_max_age;
1171 journal->j_max_trans_age = commit_max_age;
1172 } else if (commit_max_age == 0) {
1173 /* 0 means restore defaults. */
1174 journal->j_max_commit_age = journal->j_default_max_commit_age;
1175 journal->j_max_trans_age = JOURNAL_MAX_TRANS_AGE;
1176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001178 if (blocks) {
1179 int rc = reiserfs_resize(s, blocks);
1180 if (rc != 0)
1181 return rc;
1182 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001184 if (*mount_flags & MS_RDONLY) {
1185 reiserfs_xattr_init(s, *mount_flags);
1186 /* remount read-only */
1187 if (s->s_flags & MS_RDONLY)
1188 /* it is read-only already */
1189 return 0;
1190 /* try to remount file system with read-only permissions */
1191 if (sb_umount_state(rs) == REISERFS_VALID_FS
1192 || REISERFS_SB(s)->s_mount_state != REISERFS_VALID_FS) {
1193 return 0;
1194 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001196 err = journal_begin(&th, s, 10);
1197 if (err)
1198 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001200 /* Mounting a rw partition read-only. */
1201 reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s), 1);
1202 set_sb_umount_state(rs, REISERFS_SB(s)->s_mount_state);
1203 journal_mark_dirty(&th, s, SB_BUFFER_WITH_SB(s));
1204 } else {
1205 /* remount read-write */
1206 if (!(s->s_flags & MS_RDONLY)) {
1207 reiserfs_xattr_init(s, *mount_flags);
1208 return 0; /* We are read-write already */
1209 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001211 if (reiserfs_is_journal_aborted(journal))
1212 return journal->j_errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001214 handle_data_mode(s, mount_options);
1215 handle_barrier_mode(s, mount_options);
1216 REISERFS_SB(s)->s_mount_state = sb_umount_state(rs);
1217 s->s_flags &= ~MS_RDONLY; /* now it is safe to call journal_begin */
1218 err = journal_begin(&th, s, 10);
1219 if (err)
1220 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001222 /* Mount a partition which is read-only, read-write */
1223 reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s), 1);
1224 REISERFS_SB(s)->s_mount_state = sb_umount_state(rs);
1225 s->s_flags &= ~MS_RDONLY;
1226 set_sb_umount_state(rs, REISERFS_ERROR_FS);
1227 /* mark_buffer_dirty (SB_BUFFER_WITH_SB (s), 1); */
1228 journal_mark_dirty(&th, s, SB_BUFFER_WITH_SB(s));
1229 REISERFS_SB(s)->s_mount_state = REISERFS_VALID_FS;
1230 }
1231 /* this will force a full flush of all journal lists */
1232 SB_JOURNAL(s)->j_must_wait = 1;
1233 err = journal_end(&th, s, 10);
1234 if (err)
1235 return err;
1236 s->s_dirt = 0;
1237
1238 if (!(*mount_flags & MS_RDONLY)) {
1239 finish_unfinished(s);
1240 reiserfs_xattr_init(s, *mount_flags);
1241 }
1242
1243 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244}
1245
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001246static int read_super_block(struct super_block *s, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001248 struct buffer_head *bh;
1249 struct reiserfs_super_block *rs;
1250 int fs_blocksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001252 bh = sb_bread(s, offset / s->s_blocksize);
1253 if (!bh) {
1254 reiserfs_warning(s, "sh-2006: read_super_block: "
1255 "bread failed (dev %s, block %lu, size %lu)",
1256 reiserfs_bdevname(s), offset / s->s_blocksize,
1257 s->s_blocksize);
1258 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001261 rs = (struct reiserfs_super_block *)bh->b_data;
1262 if (!is_any_reiserfs_magic_string(rs)) {
1263 brelse(bh);
1264 return 1;
1265 }
1266 //
1267 // ok, reiserfs signature (old or new) found in at the given offset
1268 //
1269 fs_blocksize = sb_blocksize(rs);
1270 brelse(bh);
1271 sb_set_blocksize(s, fs_blocksize);
1272
1273 bh = sb_bread(s, offset / s->s_blocksize);
1274 if (!bh) {
1275 reiserfs_warning(s, "sh-2007: read_super_block: "
1276 "bread failed (dev %s, block %lu, size %lu)\n",
1277 reiserfs_bdevname(s), offset / s->s_blocksize,
1278 s->s_blocksize);
1279 return 1;
1280 }
1281
1282 rs = (struct reiserfs_super_block *)bh->b_data;
1283 if (sb_blocksize(rs) != s->s_blocksize) {
1284 reiserfs_warning(s, "sh-2011: read_super_block: "
1285 "can't find a reiserfs filesystem on (dev %s, block %Lu, size %lu)\n",
1286 reiserfs_bdevname(s),
1287 (unsigned long long)bh->b_blocknr,
1288 s->s_blocksize);
1289 brelse(bh);
1290 return 1;
1291 }
1292
1293 if (rs->s_v1.s_root_block == cpu_to_le32(-1)) {
1294 brelse(bh);
1295 reiserfs_warning(s,
1296 "Unfinished reiserfsck --rebuild-tree run detected. Please run\n"
1297 "reiserfsck --rebuild-tree and wait for a completion. If that fails\n"
1298 "get newer reiserfsprogs package");
1299 return 1;
1300 }
1301
1302 SB_BUFFER_WITH_SB(s) = bh;
1303 SB_DISK_SUPER_BLOCK(s) = rs;
1304
1305 if (is_reiserfs_jr(rs)) {
1306 /* magic is of non-standard journal filesystem, look at s_version to
1307 find which format is in use */
1308 if (sb_version(rs) == REISERFS_VERSION_2)
1309 reiserfs_warning(s,
1310 "read_super_block: found reiserfs format \"3.6\""
1311 " with non-standard journal");
1312 else if (sb_version(rs) == REISERFS_VERSION_1)
1313 reiserfs_warning(s,
1314 "read_super_block: found reiserfs format \"3.5\""
1315 " with non-standard journal");
1316 else {
1317 reiserfs_warning(s,
1318 "sh-2012: read_super_block: found unknown "
1319 "format \"%u\" of reiserfs with non-standard magic",
1320 sb_version(rs));
1321 return 1;
1322 }
1323 } else
1324 /* s_version of standard format may contain incorrect information,
1325 so we just look at the magic string */
1326 reiserfs_info(s,
1327 "found reiserfs format \"%s\" with standard journal\n",
1328 is_reiserfs_3_5(rs) ? "3.5" : "3.6");
1329
1330 s->s_op = &reiserfs_sops;
1331 s->s_export_op = &reiserfs_export_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332#ifdef CONFIG_QUOTA
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001333 s->s_qcop = &reiserfs_qctl_operations;
1334 s->dq_op = &reiserfs_quota_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335#endif
1336
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001337 /* new format is limited by the 32 bit wide i_blocks field, want to
1338 ** be one full block below that.
1339 */
1340 s->s_maxbytes = (512LL << 32) - s->s_blocksize;
1341 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342}
1343
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344/* after journal replay, reread all bitmap and super blocks */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001345static int reread_meta_blocks(struct super_block *s)
1346{
1347 int i;
1348 ll_rw_block(READ, 1, &(SB_BUFFER_WITH_SB(s)));
1349 wait_on_buffer(SB_BUFFER_WITH_SB(s));
1350 if (!buffer_uptodate(SB_BUFFER_WITH_SB(s))) {
1351 reiserfs_warning(s,
1352 "reread_meta_blocks, error reading the super");
1353 return 1;
1354 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001356 for (i = 0; i < SB_BMAP_NR(s); i++) {
1357 ll_rw_block(READ, 1, &(SB_AP_BITMAP(s)[i].bh));
1358 wait_on_buffer(SB_AP_BITMAP(s)[i].bh);
1359 if (!buffer_uptodate(SB_AP_BITMAP(s)[i].bh)) {
1360 reiserfs_warning(s,
1361 "reread_meta_blocks, error reading bitmap block number %d at %llu",
1362 i,
1363 (unsigned long long)SB_AP_BITMAP(s)[i].
1364 bh->b_blocknr);
1365 return 1;
1366 }
1367 }
1368 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
1370}
1371
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372/////////////////////////////////////////////////////
1373// hash detection stuff
1374
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375// if root directory is empty - we set default - Yura's - hash and
1376// warn about it
1377// FIXME: we look for only one name in a directory. If tea and yura
1378// bith have the same value - we ask user to send report to the
1379// mailing list
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001380static __u32 find_hash_out(struct super_block *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001382 int retval;
1383 struct inode *inode;
1384 struct cpu_key key;
1385 INITIALIZE_PATH(path);
1386 struct reiserfs_dir_entry de;
1387 __u32 hash = DEFAULT_HASH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001389 inode = s->s_root->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001391 do { // Some serious "goto"-hater was there ;)
1392 u32 teahash, r5hash, yurahash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001394 make_cpu_key(&key, inode, ~0, TYPE_DIRENTRY, 3);
1395 retval = search_by_entry_key(s, &key, &path, &de);
1396 if (retval == IO_ERROR) {
1397 pathrelse(&path);
1398 return UNSET_HASH;
1399 }
1400 if (retval == NAME_NOT_FOUND)
1401 de.de_entry_num--;
1402 set_de_name_and_namelen(&de);
1403 if (deh_offset(&(de.de_deh[de.de_entry_num])) == DOT_DOT_OFFSET) {
1404 /* allow override in this case */
1405 if (reiserfs_rupasov_hash(s)) {
1406 hash = YURA_HASH;
1407 }
1408 reiserfs_warning(s, "FS seems to be empty, autodetect "
1409 "is using the default hash");
1410 break;
1411 }
1412 r5hash = GET_HASH_VALUE(r5_hash(de.de_name, de.de_namelen));
1413 teahash = GET_HASH_VALUE(keyed_hash(de.de_name, de.de_namelen));
1414 yurahash = GET_HASH_VALUE(yura_hash(de.de_name, de.de_namelen));
1415 if (((teahash == r5hash)
1416 &&
1417 (GET_HASH_VALUE(deh_offset(&(de.de_deh[de.de_entry_num])))
1418 == r5hash)) || ((teahash == yurahash)
1419 && (yurahash ==
1420 GET_HASH_VALUE(deh_offset
1421 (&
1422 (de.
1423 de_deh[de.
1424 de_entry_num])))))
1425 || ((r5hash == yurahash)
1426 && (yurahash ==
1427 GET_HASH_VALUE(deh_offset
1428 (&(de.de_deh[de.de_entry_num])))))) {
1429 reiserfs_warning(s,
1430 "Unable to automatically detect hash function. "
1431 "Please mount with -o hash={tea,rupasov,r5}",
1432 reiserfs_bdevname(s));
1433 hash = UNSET_HASH;
1434 break;
1435 }
1436 if (GET_HASH_VALUE(deh_offset(&(de.de_deh[de.de_entry_num]))) ==
1437 yurahash)
1438 hash = YURA_HASH;
1439 else if (GET_HASH_VALUE
1440 (deh_offset(&(de.de_deh[de.de_entry_num]))) == teahash)
1441 hash = TEA_HASH;
1442 else if (GET_HASH_VALUE
1443 (deh_offset(&(de.de_deh[de.de_entry_num]))) == r5hash)
1444 hash = R5_HASH;
1445 else {
1446 reiserfs_warning(s, "Unrecognised hash function");
1447 hash = UNSET_HASH;
1448 }
1449 } while (0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001451 pathrelse(&path);
1452 return hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453}
1454
1455// finds out which hash names are sorted with
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001456static int what_hash(struct super_block *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001458 __u32 code;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001460 code = sb_hash_function_code(SB_DISK_SUPER_BLOCK(s));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001462 /* reiserfs_hash_detect() == true if any of the hash mount options
1463 ** were used. We must check them to make sure the user isn't
1464 ** using a bad hash value
1465 */
1466 if (code == UNSET_HASH || reiserfs_hash_detect(s))
1467 code = find_hash_out(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001469 if (code != UNSET_HASH && reiserfs_hash_detect(s)) {
1470 /* detection has found the hash, and we must check against the
1471 ** mount options
1472 */
1473 if (reiserfs_rupasov_hash(s) && code != YURA_HASH) {
1474 reiserfs_warning(s, "Error, %s hash detected, "
1475 "unable to force rupasov hash",
1476 reiserfs_hashname(code));
1477 code = UNSET_HASH;
1478 } else if (reiserfs_tea_hash(s) && code != TEA_HASH) {
1479 reiserfs_warning(s, "Error, %s hash detected, "
1480 "unable to force tea hash",
1481 reiserfs_hashname(code));
1482 code = UNSET_HASH;
1483 } else if (reiserfs_r5_hash(s) && code != R5_HASH) {
1484 reiserfs_warning(s, "Error, %s hash detected, "
1485 "unable to force r5 hash",
1486 reiserfs_hashname(code));
1487 code = UNSET_HASH;
1488 }
1489 } else {
1490 /* find_hash_out was not called or could not determine the hash */
1491 if (reiserfs_rupasov_hash(s)) {
1492 code = YURA_HASH;
1493 } else if (reiserfs_tea_hash(s)) {
1494 code = TEA_HASH;
1495 } else if (reiserfs_r5_hash(s)) {
1496 code = R5_HASH;
1497 }
1498 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001500 /* if we are mounted RW, and we have a new valid hash code, update
1501 ** the super
1502 */
1503 if (code != UNSET_HASH &&
1504 !(s->s_flags & MS_RDONLY) &&
1505 code != sb_hash_function_code(SB_DISK_SUPER_BLOCK(s))) {
1506 set_sb_hash_function_code(SB_DISK_SUPER_BLOCK(s), code);
1507 }
1508 return code;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509}
1510
1511// return pointer to appropriate function
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001512static hashf_t hash_function(struct super_block *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001514 switch (what_hash(s)) {
1515 case TEA_HASH:
1516 reiserfs_info(s, "Using tea hash to sort names\n");
1517 return keyed_hash;
1518 case YURA_HASH:
1519 reiserfs_info(s, "Using rupasov hash to sort names\n");
1520 return yura_hash;
1521 case R5_HASH:
1522 reiserfs_info(s, "Using r5 hash to sort names\n");
1523 return r5_hash;
1524 }
1525 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526}
1527
1528// this is used to set up correct value for old partitions
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001529static int function2code(hashf_t func)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001531 if (func == keyed_hash)
1532 return TEA_HASH;
1533 if (func == yura_hash)
1534 return YURA_HASH;
1535 if (func == r5_hash)
1536 return R5_HASH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001538 BUG(); // should never happen
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001540 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541}
1542
1543#define SWARN(silent, s, ...) \
1544 if (!(silent)) \
1545 reiserfs_warning (s, __VA_ARGS__)
1546
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001547static int reiserfs_fill_super(struct super_block *s, void *data, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001549 struct inode *root_inode;
1550 int j;
1551 struct reiserfs_transaction_handle th;
1552 int old_format = 0;
1553 unsigned long blocks;
1554 unsigned int commit_max_age = 0;
1555 int jinit_done = 0;
1556 struct reiserfs_iget_args args;
1557 struct reiserfs_super_block *rs;
1558 char *jdev_name;
1559 struct reiserfs_sb_info *sbi;
1560 int errval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001562 sbi = kmalloc(sizeof(struct reiserfs_sb_info), GFP_KERNEL);
1563 if (!sbi) {
1564 errval = -ENOMEM;
1565 goto error;
1566 }
1567 s->s_fs_info = sbi;
1568 memset(sbi, 0, sizeof(struct reiserfs_sb_info));
1569 /* Set default values for options: non-aggressive tails, RO on errors */
1570 REISERFS_SB(s)->s_mount_opt |= (1 << REISERFS_SMALLTAIL);
1571 REISERFS_SB(s)->s_mount_opt |= (1 << REISERFS_ERROR_RO);
1572 /* no preallocation minimum, be smart in
1573 reiserfs_file_write instead */
1574 REISERFS_SB(s)->s_alloc_options.preallocmin = 0;
1575 /* Preallocate by 16 blocks (17-1) at once */
1576 REISERFS_SB(s)->s_alloc_options.preallocsize = 17;
1577 /* Initialize the rwsem for xattr dir */
1578 init_rwsem(&REISERFS_SB(s)->xattr_dir_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001580 /* setup default block allocator options */
1581 reiserfs_init_alloc_options(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001583 jdev_name = NULL;
1584 if (reiserfs_parse_options
1585 (s, (char *)data, &(sbi->s_mount_opt), &blocks, &jdev_name,
1586 &commit_max_age) == 0) {
1587 goto error;
1588 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001590 if (blocks) {
1591 SWARN(silent, s, "jmacd-7: reiserfs_fill_super: resize option "
1592 "for remount only");
1593 goto error;
1594 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001596 /* try old format (undistributed bitmap, super block in 8-th 1k block of a device) */
1597 if (!read_super_block(s, REISERFS_OLD_DISK_OFFSET_IN_BYTES))
1598 old_format = 1;
1599 /* try new format (64-th 1k block), which can contain reiserfs super block */
1600 else if (read_super_block(s, REISERFS_DISK_OFFSET_IN_BYTES)) {
1601 SWARN(silent, s,
1602 "sh-2021: reiserfs_fill_super: can not find reiserfs on %s",
1603 reiserfs_bdevname(s));
1604 goto error;
1605 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001607 rs = SB_DISK_SUPER_BLOCK(s);
1608 /* Let's do basic sanity check to verify that underlying device is not
1609 smaller than the filesystem. If the check fails then abort and scream,
1610 because bad stuff will happen otherwise. */
1611 if (s->s_bdev && s->s_bdev->bd_inode
1612 && i_size_read(s->s_bdev->bd_inode) <
1613 sb_block_count(rs) * sb_blocksize(rs)) {
1614 SWARN(silent, s,
1615 "Filesystem on %s cannot be mounted because it is bigger than the device",
1616 reiserfs_bdevname(s));
1617 SWARN(silent, s,
1618 "You may need to run fsck or increase size of your LVM partition");
1619 SWARN(silent, s,
1620 "Or may be you forgot to reboot after fdisk when it told you to");
1621 goto error;
1622 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001624 sbi->s_mount_state = SB_REISERFS_STATE(s);
1625 sbi->s_mount_state = REISERFS_VALID_FS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626
Jeff Mahoney6f010462006-09-30 23:28:43 -07001627 if ((errval = reiserfs_init_bitmap_cache(s))) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001628 SWARN(silent, s,
1629 "jmacd-8: reiserfs_fill_super: unable to read bitmap");
1630 goto error;
1631 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632#ifdef CONFIG_REISERFS_CHECK
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001633 SWARN(silent, s, "CONFIG_REISERFS_CHECK is set ON");
1634 SWARN(silent, s, "- it is slow mode for debugging.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635#endif
1636
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001637 /* make data=ordered the default */
1638 if (!reiserfs_data_log(s) && !reiserfs_data_ordered(s) &&
1639 !reiserfs_data_writeback(s)) {
1640 REISERFS_SB(s)->s_mount_opt |= (1 << REISERFS_DATA_ORDERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 }
1642
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001643 if (reiserfs_data_log(s)) {
1644 reiserfs_info(s, "using journaled data mode\n");
1645 } else if (reiserfs_data_ordered(s)) {
1646 reiserfs_info(s, "using ordered data mode\n");
1647 } else {
1648 reiserfs_info(s, "using writeback data mode\n");
1649 }
1650 if (reiserfs_barrier_flush(s)) {
1651 printk("reiserfs: using flush barriers\n");
1652 }
1653 // set_device_ro(s->s_dev, 1) ;
1654 if (journal_init(s, jdev_name, old_format, commit_max_age)) {
1655 SWARN(silent, s,
1656 "sh-2022: reiserfs_fill_super: unable to initialize journal space");
1657 goto error;
1658 } else {
1659 jinit_done = 1; /* once this is set, journal_release must be called
1660 ** if we error out of the mount
1661 */
1662 }
1663 if (reread_meta_blocks(s)) {
1664 SWARN(silent, s,
1665 "jmacd-9: reiserfs_fill_super: unable to reread meta blocks after journal init");
1666 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 }
1668
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001669 if (replay_only(s))
1670 goto error;
1671
1672 if (bdev_read_only(s->s_bdev) && !(s->s_flags & MS_RDONLY)) {
1673 SWARN(silent, s,
1674 "clm-7000: Detected readonly device, marking FS readonly");
1675 s->s_flags |= MS_RDONLY;
1676 }
1677 args.objectid = REISERFS_ROOT_OBJECTID;
1678 args.dirid = REISERFS_ROOT_PARENT_OBJECTID;
1679 root_inode =
1680 iget5_locked(s, REISERFS_ROOT_OBJECTID, reiserfs_find_actor,
1681 reiserfs_init_locked_inode, (void *)(&args));
1682 if (!root_inode) {
1683 SWARN(silent, s,
1684 "jmacd-10: reiserfs_fill_super: get root inode failed");
1685 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 }
1687
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001688 if (root_inode->i_state & I_NEW) {
1689 reiserfs_read_locked_inode(root_inode, &args);
1690 unlock_new_inode(root_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 }
1692
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001693 s->s_root = d_alloc_root(root_inode);
1694 if (!s->s_root) {
1695 iput(root_inode);
1696 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001698 // define and initialize hash function
1699 sbi->s_hash_function = hash_function(s);
1700 if (sbi->s_hash_function == NULL) {
1701 dput(s->s_root);
1702 s->s_root = NULL;
1703 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001705
1706 if (is_reiserfs_3_5(rs)
1707 || (is_reiserfs_jr(rs) && SB_VERSION(s) == REISERFS_VERSION_1))
1708 set_bit(REISERFS_3_5, &(sbi->s_properties));
Jeff Mahoneye1fabd32006-09-30 23:28:40 -07001709 else if (old_format)
1710 set_bit(REISERFS_OLD_FORMAT, &(sbi->s_properties));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001711 else
1712 set_bit(REISERFS_3_6, &(sbi->s_properties));
1713
1714 if (!(s->s_flags & MS_RDONLY)) {
1715
1716 errval = journal_begin(&th, s, 1);
1717 if (errval) {
1718 dput(s->s_root);
1719 s->s_root = NULL;
1720 goto error;
1721 }
1722 reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s), 1);
1723
1724 set_sb_umount_state(rs, REISERFS_ERROR_FS);
1725 set_sb_fs_state(rs, 0);
1726
1727 if (old_format_only(s)) {
1728 /* filesystem of format 3.5 either with standard or non-standard
1729 journal */
1730 if (convert_reiserfs(s)) {
1731 /* and -o conv is given */
1732 if (!silent)
1733 reiserfs_info(s,
1734 "converting 3.5 filesystem to the 3.6 format");
1735
1736 if (is_reiserfs_3_5(rs))
1737 /* put magic string of 3.6 format. 2.2 will not be able to
1738 mount this filesystem anymore */
1739 memcpy(rs->s_v1.s_magic,
1740 reiserfs_3_6_magic_string,
1741 sizeof
1742 (reiserfs_3_6_magic_string));
1743
1744 set_sb_version(rs, REISERFS_VERSION_2);
1745 reiserfs_convert_objectid_map_v1(s);
1746 set_bit(REISERFS_3_6, &(sbi->s_properties));
1747 clear_bit(REISERFS_3_5, &(sbi->s_properties));
1748 } else if (!silent) {
1749 reiserfs_info(s, "using 3.5.x disk format\n");
1750 }
1751 }
1752
1753 journal_mark_dirty(&th, s, SB_BUFFER_WITH_SB(s));
1754 errval = journal_end(&th, s, 1);
1755 if (errval) {
1756 dput(s->s_root);
1757 s->s_root = NULL;
1758 goto error;
1759 }
1760
1761 if ((errval = reiserfs_xattr_init(s, s->s_flags))) {
1762 dput(s->s_root);
1763 s->s_root = NULL;
1764 goto error;
1765 }
1766
1767 /* look for files which were to be removed in previous session */
1768 finish_unfinished(s);
1769 } else {
1770 if (old_format_only(s) && !silent) {
1771 reiserfs_info(s, "using 3.5.x disk format\n");
1772 }
1773
1774 if ((errval = reiserfs_xattr_init(s, s->s_flags))) {
1775 dput(s->s_root);
1776 s->s_root = NULL;
1777 goto error;
1778 }
1779 }
1780 // mark hash in super block: it could be unset. overwrite should be ok
1781 set_sb_hash_function_code(rs, function2code(sbi->s_hash_function));
1782
1783 handle_attrs(s);
1784
1785 reiserfs_proc_info_init(s);
1786
1787 init_waitqueue_head(&(sbi->s_wait));
1788 spin_lock_init(&sbi->bitmap_lock);
1789
1790 return (0);
1791
1792 error:
1793 if (jinit_done) { /* kill the commit thread, free journal ram */
1794 journal_release_error(NULL, s);
1795 }
1796 if (SB_DISK_SUPER_BLOCK(s)) {
1797 for (j = 0; j < SB_BMAP_NR(s); j++) {
1798 if (SB_AP_BITMAP(s))
1799 brelse(SB_AP_BITMAP(s)[j].bh);
1800 }
James Lamannaea0e0a42005-09-10 00:27:16 -07001801 vfree(SB_AP_BITMAP(s));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001802 }
1803 if (SB_BUFFER_WITH_SB(s))
1804 brelse(SB_BUFFER_WITH_SB(s));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805#ifdef CONFIG_QUOTA
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001806 for (j = 0; j < MAXQUOTAS; j++) {
James Lamanna833d3042005-10-30 15:00:16 -08001807 kfree(sbi->s_qf_names[j]);
1808 sbi->s_qf_names[j] = NULL;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001809 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810#endif
James Lamanna833d3042005-10-30 15:00:16 -08001811 kfree(sbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001813 s->s_fs_info = NULL;
1814 return errval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815}
1816
David Howells726c3342006-06-23 02:02:58 -07001817static int reiserfs_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818{
David Howells726c3342006-06-23 02:02:58 -07001819 struct reiserfs_super_block *rs = SB_DISK_SUPER_BLOCK(dentry->d_sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001820
1821 buf->f_namelen = (REISERFS_MAX_NAME(s->s_blocksize));
1822 buf->f_bfree = sb_free_blocks(rs);
1823 buf->f_bavail = buf->f_bfree;
1824 buf->f_blocks = sb_block_count(rs) - sb_bmap_nr(rs) - 1;
David Howells726c3342006-06-23 02:02:58 -07001825 buf->f_bsize = dentry->d_sb->s_blocksize;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001826 /* changed to accommodate gcc folks. */
1827 buf->f_type = REISERFS_SUPER_MAGIC;
1828 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829}
1830
1831#ifdef CONFIG_QUOTA
1832static int reiserfs_dquot_initialize(struct inode *inode, int type)
1833{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001834 struct reiserfs_transaction_handle th;
1835 int ret, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001837 /* We may create quota structure so we need to reserve enough blocks */
1838 reiserfs_write_lock(inode->i_sb);
1839 ret =
1840 journal_begin(&th, inode->i_sb,
1841 2 * REISERFS_QUOTA_INIT_BLOCKS(inode->i_sb));
1842 if (ret)
1843 goto out;
1844 ret = dquot_initialize(inode, type);
1845 err =
1846 journal_end(&th, inode->i_sb,
1847 2 * REISERFS_QUOTA_INIT_BLOCKS(inode->i_sb));
1848 if (!ret && err)
1849 ret = err;
1850 out:
1851 reiserfs_write_unlock(inode->i_sb);
1852 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853}
1854
1855static int reiserfs_dquot_drop(struct inode *inode)
1856{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001857 struct reiserfs_transaction_handle th;
1858 int ret, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001860 /* We may delete quota structure so we need to reserve enough blocks */
1861 reiserfs_write_lock(inode->i_sb);
1862 ret =
1863 journal_begin(&th, inode->i_sb,
1864 2 * REISERFS_QUOTA_DEL_BLOCKS(inode->i_sb));
1865 if (ret)
1866 goto out;
1867 ret = dquot_drop(inode);
1868 err =
1869 journal_end(&th, inode->i_sb,
1870 2 * REISERFS_QUOTA_DEL_BLOCKS(inode->i_sb));
1871 if (!ret && err)
1872 ret = err;
1873 out:
1874 reiserfs_write_unlock(inode->i_sb);
1875 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876}
1877
1878static int reiserfs_write_dquot(struct dquot *dquot)
1879{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001880 struct reiserfs_transaction_handle th;
1881 int ret, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001883 reiserfs_write_lock(dquot->dq_sb);
1884 ret =
1885 journal_begin(&th, dquot->dq_sb,
1886 REISERFS_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
1887 if (ret)
1888 goto out;
1889 ret = dquot_commit(dquot);
1890 err =
1891 journal_end(&th, dquot->dq_sb,
1892 REISERFS_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
1893 if (!ret && err)
1894 ret = err;
1895 out:
1896 reiserfs_write_unlock(dquot->dq_sb);
1897 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898}
1899
1900static int reiserfs_acquire_dquot(struct dquot *dquot)
1901{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001902 struct reiserfs_transaction_handle th;
1903 int ret, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001905 reiserfs_write_lock(dquot->dq_sb);
1906 ret =
1907 journal_begin(&th, dquot->dq_sb,
1908 REISERFS_QUOTA_INIT_BLOCKS(dquot->dq_sb));
1909 if (ret)
1910 goto out;
1911 ret = dquot_acquire(dquot);
1912 err =
1913 journal_end(&th, dquot->dq_sb,
1914 REISERFS_QUOTA_INIT_BLOCKS(dquot->dq_sb));
1915 if (!ret && err)
1916 ret = err;
1917 out:
1918 reiserfs_write_unlock(dquot->dq_sb);
1919 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920}
1921
1922static int reiserfs_release_dquot(struct dquot *dquot)
1923{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001924 struct reiserfs_transaction_handle th;
1925 int ret, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001927 reiserfs_write_lock(dquot->dq_sb);
1928 ret =
1929 journal_begin(&th, dquot->dq_sb,
1930 REISERFS_QUOTA_DEL_BLOCKS(dquot->dq_sb));
1931 if (ret)
1932 goto out;
1933 ret = dquot_release(dquot);
1934 err =
1935 journal_end(&th, dquot->dq_sb,
1936 REISERFS_QUOTA_DEL_BLOCKS(dquot->dq_sb));
1937 if (!ret && err)
1938 ret = err;
1939 out:
1940 reiserfs_write_unlock(dquot->dq_sb);
1941 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942}
1943
1944static int reiserfs_mark_dquot_dirty(struct dquot *dquot)
1945{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001946 /* Are we journalling quotas? */
1947 if (REISERFS_SB(dquot->dq_sb)->s_qf_names[USRQUOTA] ||
1948 REISERFS_SB(dquot->dq_sb)->s_qf_names[GRPQUOTA]) {
1949 dquot_mark_dquot_dirty(dquot);
1950 return reiserfs_write_dquot(dquot);
1951 } else
1952 return dquot_mark_dquot_dirty(dquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953}
1954
1955static int reiserfs_write_info(struct super_block *sb, int type)
1956{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001957 struct reiserfs_transaction_handle th;
1958 int ret, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001960 /* Data block + inode block */
1961 reiserfs_write_lock(sb);
1962 ret = journal_begin(&th, sb, 2);
1963 if (ret)
1964 goto out;
1965 ret = dquot_commit_info(sb, type);
1966 err = journal_end(&th, sb, 2);
1967 if (!ret && err)
1968 ret = err;
1969 out:
1970 reiserfs_write_unlock(sb);
1971 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972}
1973
1974/*
Christoph Hellwig84de8562005-06-23 00:09:16 -07001975 * Turn on quotas during mount time - we need to find the quota file and such...
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 */
1977static int reiserfs_quota_on_mount(struct super_block *sb, int type)
1978{
Christoph Hellwig84de8562005-06-23 00:09:16 -07001979 return vfs_quota_on_mount(sb, REISERFS_SB(sb)->s_qf_names[type],
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001980 REISERFS_SB(sb)->s_jquota_fmt, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981}
1982
1983/*
1984 * Standard function to be called on quota_on
1985 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001986static int reiserfs_quota_on(struct super_block *sb, int type, int format_id,
1987 char *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001989 int err;
1990 struct nameidata nd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001992 if (!(REISERFS_SB(sb)->s_mount_opt & (1 << REISERFS_QUOTA)))
1993 return -EINVAL;
1994 err = path_lookup(path, LOOKUP_FOLLOW, &nd);
1995 if (err)
1996 return err;
1997 /* Quotafile not on the same filesystem? */
1998 if (nd.mnt->mnt_sb != sb) {
1999 path_release(&nd);
2000 return -EXDEV;
2001 }
2002 /* We must not pack tails for quota files on reiserfs for quota IO to work */
2003 if (!REISERFS_I(nd.dentry->d_inode)->i_flags & i_nopack_mask) {
2004 reiserfs_warning(sb,
2005 "reiserfs: Quota file must have tail packing disabled.");
2006 path_release(&nd);
2007 return -EINVAL;
2008 }
2009 /* Not journalling quota? No more tests needed... */
2010 if (!REISERFS_SB(sb)->s_qf_names[USRQUOTA] &&
2011 !REISERFS_SB(sb)->s_qf_names[GRPQUOTA]) {
2012 path_release(&nd);
2013 return vfs_quota_on(sb, type, format_id, path);
2014 }
2015 /* Quotafile not of fs root? */
2016 if (nd.dentry->d_parent->d_inode != sb->s_root->d_inode)
2017 reiserfs_warning(sb,
2018 "reiserfs: Quota file not on filesystem root. "
2019 "Journalled quota will not work.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 path_release(&nd);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002021 return vfs_quota_on(sb, type, format_id, path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022}
2023
2024/* Read data from quotafile - avoid pagecache and such because we cannot afford
2025 * acquiring the locks... As quota files are never truncated and quota code
2026 * itself serializes the operations (and noone else should touch the files)
2027 * we don't have to be afraid of races */
2028static ssize_t reiserfs_quota_read(struct super_block *sb, int type, char *data,
2029 size_t len, loff_t off)
2030{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002031 struct inode *inode = sb_dqopt(sb)->files[type];
2032 unsigned long blk = off >> sb->s_blocksize_bits;
2033 int err = 0, offset = off & (sb->s_blocksize - 1), tocopy;
2034 size_t toread;
2035 struct buffer_head tmp_bh, *bh;
2036 loff_t i_size = i_size_read(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002038 if (off > i_size)
2039 return 0;
2040 if (off + len > i_size)
2041 len = i_size - off;
2042 toread = len;
2043 while (toread > 0) {
2044 tocopy =
2045 sb->s_blocksize - offset <
2046 toread ? sb->s_blocksize - offset : toread;
2047 tmp_bh.b_state = 0;
2048 /* Quota files are without tails so we can safely use this function */
2049 reiserfs_write_lock(sb);
2050 err = reiserfs_get_block(inode, blk, &tmp_bh, 0);
2051 reiserfs_write_unlock(sb);
2052 if (err)
2053 return err;
2054 if (!buffer_mapped(&tmp_bh)) /* A hole? */
2055 memset(data, 0, tocopy);
2056 else {
2057 bh = sb_bread(sb, tmp_bh.b_blocknr);
2058 if (!bh)
2059 return -EIO;
2060 memcpy(data, bh->b_data + offset, tocopy);
2061 brelse(bh);
2062 }
2063 offset = 0;
2064 toread -= tocopy;
2065 data += tocopy;
2066 blk++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002068 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069}
2070
2071/* Write to quotafile (we know the transaction is already started and has
2072 * enough credits) */
2073static ssize_t reiserfs_quota_write(struct super_block *sb, int type,
2074 const char *data, size_t len, loff_t off)
2075{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002076 struct inode *inode = sb_dqopt(sb)->files[type];
2077 unsigned long blk = off >> sb->s_blocksize_bits;
2078 int err = 0, offset = off & (sb->s_blocksize - 1), tocopy;
2079 int journal_quota = REISERFS_SB(sb)->s_qf_names[type] != NULL;
2080 size_t towrite = len;
2081 struct buffer_head tmp_bh, *bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082
Arjan van de Ven5c81a412006-07-03 00:25:20 -07002083 mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002084 while (towrite > 0) {
2085 tocopy = sb->s_blocksize - offset < towrite ?
2086 sb->s_blocksize - offset : towrite;
2087 tmp_bh.b_state = 0;
2088 err = reiserfs_get_block(inode, blk, &tmp_bh, GET_BLOCK_CREATE);
2089 if (err)
2090 goto out;
2091 if (offset || tocopy != sb->s_blocksize)
2092 bh = sb_bread(sb, tmp_bh.b_blocknr);
2093 else
2094 bh = sb_getblk(sb, tmp_bh.b_blocknr);
2095 if (!bh) {
2096 err = -EIO;
2097 goto out;
2098 }
2099 lock_buffer(bh);
2100 memcpy(bh->b_data + offset, data, tocopy);
2101 flush_dcache_page(bh->b_page);
2102 set_buffer_uptodate(bh);
2103 unlock_buffer(bh);
2104 reiserfs_prepare_for_journal(sb, bh, 1);
2105 journal_mark_dirty(current->journal_info, sb, bh);
2106 if (!journal_quota)
2107 reiserfs_add_ordered_list(inode, bh);
2108 brelse(bh);
2109 offset = 0;
2110 towrite -= tocopy;
2111 data += tocopy;
2112 blk++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002114 out:
2115 if (len == towrite)
2116 return err;
2117 if (inode->i_size < off + len - towrite)
2118 i_size_write(inode, off + len - towrite);
2119 inode->i_version++;
2120 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
2121 mark_inode_dirty(inode);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002122 mutex_unlock(&inode->i_mutex);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002123 return len - towrite;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124}
2125
2126#endif
2127
David Howells454e2392006-06-23 02:02:57 -07002128static int get_super_block(struct file_system_type *fs_type,
2129 int flags, const char *dev_name,
2130 void *data, struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131{
David Howells454e2392006-06-23 02:02:57 -07002132 return get_sb_bdev(fs_type, flags, dev_name, data, reiserfs_fill_super,
2133 mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134}
2135
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002136static int __init init_reiserfs_fs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137{
2138 int ret;
2139
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002140 if ((ret = init_inodecache())) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 return ret;
2142 }
2143
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002144 if ((ret = reiserfs_xattr_register_handlers()))
2145 goto failed_reiserfs_xattr_register_handlers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002147 reiserfs_proc_info_global_init();
2148 reiserfs_proc_register_global("version",
2149 reiserfs_global_version_in_proc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002151 ret = register_filesystem(&reiserfs_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152
2153 if (ret == 0) {
2154 return 0;
2155 }
2156
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002157 reiserfs_xattr_unregister_handlers();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002159 failed_reiserfs_xattr_register_handlers:
2160 reiserfs_proc_unregister_global("version");
2161 reiserfs_proc_info_global_done();
2162 destroy_inodecache();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163
2164 return ret;
2165}
2166
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002167static void __exit exit_reiserfs_fs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002169 reiserfs_xattr_unregister_handlers();
2170 reiserfs_proc_unregister_global("version");
2171 reiserfs_proc_info_global_done();
2172 unregister_filesystem(&reiserfs_fs_type);
2173 destroy_inodecache();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174}
2175
2176struct file_system_type reiserfs_fs_type = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002177 .owner = THIS_MODULE,
2178 .name = "reiserfs",
2179 .get_sb = get_super_block,
2180 .kill_sb = kill_block_super,
2181 .fs_flags = FS_REQUIRES_DEV,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182};
2183
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002184MODULE_DESCRIPTION("ReiserFS journaled filesystem");
2185MODULE_AUTHOR("Hans Reiser <reiser@namesys.com>");
2186MODULE_LICENSE("GPL");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002188module_init(init_reiserfs_fs);
2189module_exit(exit_reiserfs_fs);